blob: 14352452719c51caddfc5e650a740f66be16aee9 [file] [log] [blame]
wdenk2d966952002-10-31 22:12:35 +00001/*
2 * Copied from Linux Monitor (LiMon) - Networking.
3 *
4 * Copyright 1994 - 2000 Neil Russell.
5 * (See License)
6 * Copyright 2000 Roland Borde
7 * Copyright 2000 Paolo Scaffardi
8 * Copyright 2000-2002 Wolfgang Denk, wd@denx.de
9 */
10
11/*
12 * General Desription:
13 *
14 * The user interface supports commands for BOOTP, RARP, and TFTP.
15 * Also, we support ARP internally. Depending on available data,
16 * these interact as follows:
17 *
18 * BOOTP:
19 *
20 * Prerequisites: - own ethernet address
21 * We want: - own IP address
22 * - TFTP server IP address
23 * - name of bootfile
24 * Next step: ARP
25 *
26 * RARP:
27 *
28 * Prerequisites: - own ethernet address
29 * We want: - own IP address
30 * - TFTP server IP address
31 * Next step: ARP
32 *
33 * ARP:
34 *
35 * Prerequisites: - own ethernet address
36 * - own IP address
37 * - TFTP server IP address
38 * We want: - TFTP server ethernet address
39 * Next step: TFTP
40 *
41 * DHCP:
42 *
Wolfgang Denkb2f50802005-08-12 23:43:12 +020043 * Prerequisites: - own ethernet address
44 * We want: - IP, Netmask, ServerIP, Gateway IP
45 * - bootfilename, lease time
46 * Next step: - TFTP
wdenk2d966952002-10-31 22:12:35 +000047 *
48 * TFTP:
49 *
50 * Prerequisites: - own ethernet address
51 * - own IP address
52 * - TFTP server IP address
53 * - TFTP server ethernet address
54 * - name of bootfile (if unknown, we use a default name
55 * derived from our own IP address)
56 * We want: - load the boot file
57 * Next step: none
wdenkcbd8a352004-02-24 02:00:03 +000058 *
59 * NFS:
60 *
61 * Prerequisites: - own ethernet address
62 * - own IP address
63 * - name of bootfile (if unknown, we use a default name
64 * derived from our own IP address)
65 * We want: - load the boot file
66 * Next step: none
wdenkea287de2005-04-01 00:25:43 +000067 *
68 * SNTP:
69 *
Wolfgang Denkb2f50802005-08-12 23:43:12 +020070 * Prerequisites: - own ethernet address
wdenkea287de2005-04-01 00:25:43 +000071 * - own IP address
72 * We want: - network time
73 * Next step: none
wdenk2d966952002-10-31 22:12:35 +000074 */
75
76
77#include <common.h>
78#include <watchdog.h>
79#include <command.h>
Joe Hershberger48522bb2012-05-15 08:59:08 +000080#include <linux/compiler.h>
wdenk2d966952002-10-31 22:12:35 +000081#include <net.h>
82#include "bootp.h"
83#include "tftp.h"
Peter Tyserbf6cb242010-09-30 11:25:48 -050084#ifdef CONFIG_CMD_RARP
wdenk2d966952002-10-31 22:12:35 +000085#include "rarp.h"
Peter Tyserbf6cb242010-09-30 11:25:48 -050086#endif
wdenkcbd8a352004-02-24 02:00:03 +000087#include "nfs.h"
wdenkfc3e2162003-10-08 22:33:00 +000088#ifdef CONFIG_STATUS_LED
89#include <status_led.h>
90#include <miiphy.h>
91#endif
Jon Loeliger643d1ab2007-07-09 17:45:14 -050092#if defined(CONFIG_CMD_SNTP)
wdenkea287de2005-04-01 00:25:43 +000093#include "sntp.h"
94#endif
Peter Tyser561858e2008-11-03 09:30:59 -060095#if defined(CONFIG_CDP_VERSION)
96#include <timestamp.h>
97#endif
Robin Getz1a32bf42009-07-20 14:53:54 -040098#if defined(CONFIG_CMD_DNS)
99#include "dns.h"
100#endif
wdenk2d966952002-10-31 22:12:35 +0000101
Wolfgang Denkd87080b2006-03-31 18:32:53 +0200102DECLARE_GLOBAL_DATA_PTR;
103
Guennadi Liakhovetski40cb90e2008-04-03 17:04:19 +0200104#ifndef CONFIG_ARP_TIMEOUT
Luca Ceresoli3e38e422011-05-11 03:59:54 +0000105/* Milliseconds before trying ARP again */
106# define ARP_TIMEOUT 5000UL
wdenk73a8b272003-06-05 19:27:42 +0000107#else
Bartlomiej Sieka49f3bdb2008-10-01 15:26:28 +0200108# define ARP_TIMEOUT CONFIG_ARP_TIMEOUT
Guennadi Liakhovetski40cb90e2008-04-03 17:04:19 +0200109#endif
110
111
112#ifndef CONFIG_NET_RETRY_COUNT
113# define ARP_TIMEOUT_COUNT 5 /* # of timeouts before giving up */
114#else
115# define ARP_TIMEOUT_COUNT CONFIG_NET_RETRY_COUNT
wdenk73a8b272003-06-05 19:27:42 +0000116#endif
117
wdenk2d966952002-10-31 22:12:35 +0000118/** BOOTP EXTENTIONS **/
119
Luca Ceresoli3e38e422011-05-11 03:59:54 +0000120/* Our subnet mask (0=unknown) */
Luca Ceresolic586ce62011-05-11 03:59:55 +0000121IPaddr_t NetOurSubnetMask;
Luca Ceresoli3e38e422011-05-11 03:59:54 +0000122/* Our gateways IP address */
Luca Ceresolic586ce62011-05-11 03:59:55 +0000123IPaddr_t NetOurGatewayIP;
Luca Ceresoli3e38e422011-05-11 03:59:54 +0000124/* Our DNS IP address */
Luca Ceresolic586ce62011-05-11 03:59:55 +0000125IPaddr_t NetOurDNSIP;
Jon Loeliger1fe80d72007-07-09 22:08:34 -0500126#if defined(CONFIG_BOOTP_DNS2)
Luca Ceresoli3e38e422011-05-11 03:59:54 +0000127/* Our 2nd DNS IP address */
Luca Ceresolic586ce62011-05-11 03:59:55 +0000128IPaddr_t NetOurDNS2IP;
stroesefe389a82003-08-28 14:17:32 +0000129#endif
Luca Ceresoli3e38e422011-05-11 03:59:54 +0000130/* Our NIS domain */
Luca Ceresolic586ce62011-05-11 03:59:55 +0000131char NetOurNISDomain[32] = {0,};
Luca Ceresoli3e38e422011-05-11 03:59:54 +0000132/* Our hostname */
Luca Ceresolic586ce62011-05-11 03:59:55 +0000133char NetOurHostName[32] = {0,};
Luca Ceresoli3e38e422011-05-11 03:59:54 +0000134/* Our bootpath */
Luca Ceresolic586ce62011-05-11 03:59:55 +0000135char NetOurRootPath[64] = {0,};
Luca Ceresoli3e38e422011-05-11 03:59:54 +0000136/* Our bootfile size in blocks */
Luca Ceresolic586ce62011-05-11 03:59:55 +0000137ushort NetBootFileSize;
wdenk2d966952002-10-31 22:12:35 +0000138
David Updegraff53a5c422007-06-11 10:41:07 -0500139#ifdef CONFIG_MCAST_TFTP /* Multicast TFTP */
140IPaddr_t Mcast_addr;
141#endif
142
wdenk2d966952002-10-31 22:12:35 +0000143/** END OF BOOTP EXTENTIONS **/
144
Luca Ceresoli3e38e422011-05-11 03:59:54 +0000145/* The actual transferred size of the bootfile (in bytes) */
146ulong NetBootFileXferSize;
147/* Our ethernet address */
148uchar NetOurEther[6];
149/* Boot server enet address */
Luca Ceresolic586ce62011-05-11 03:59:55 +0000150uchar NetServerEther[6];
Luca Ceresoli3e38e422011-05-11 03:59:54 +0000151/* Our IP addr (0 = unknown) */
152IPaddr_t NetOurIP;
153/* Server IP addr (0 = unknown) */
154IPaddr_t NetServerIP;
155/* Current receive packet */
Joe Hershbergerdb288a92012-05-15 08:59:04 +0000156uchar *NetRxPacket;
Luca Ceresoli3e38e422011-05-11 03:59:54 +0000157/* Current rx packet length */
158int NetRxPacketLen;
159/* IP packet ID */
160unsigned NetIPID;
161/* Ethernet bcast address */
Luca Ceresolic586ce62011-05-11 03:59:55 +0000162uchar NetBcastAddr[6] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
163uchar NetEtherNullAddr[6];
Rafal Jaworowskif85b6072007-12-27 18:19:02 +0100164#ifdef CONFIG_API
Joe Hershbergerdb288a92012-05-15 08:59:04 +0000165void (*push_packet)(void *, int len) = 0;
Rafal Jaworowskif85b6072007-12-27 18:19:02 +0100166#endif
Jon Loeliger643d1ab2007-07-09 17:45:14 -0500167#if defined(CONFIG_CMD_CDP)
Luca Ceresoli3e38e422011-05-11 03:59:54 +0000168/* Ethernet bcast address */
Luca Ceresolic586ce62011-05-11 03:59:55 +0000169uchar NetCDPAddr[6] = { 0x01, 0x00, 0x0c, 0xcc, 0xcc, 0xcc };
wdenka3d991b2004-04-15 21:48:45 +0000170#endif
Luca Ceresoli3e38e422011-05-11 03:59:54 +0000171/* Network loop state */
172int NetState;
Luca Ceresoli3e38e422011-05-11 03:59:54 +0000173/* Tried all network devices */
Luca Ceresolic586ce62011-05-11 03:59:55 +0000174int NetRestartWrap;
Luca Ceresoli3e38e422011-05-11 03:59:54 +0000175/* Network loop restarted */
Luca Ceresolic586ce62011-05-11 03:59:55 +0000176static int NetRestarted;
Luca Ceresoli3e38e422011-05-11 03:59:54 +0000177/* At least one device configured */
Luca Ceresolic586ce62011-05-11 03:59:55 +0000178static int NetDevExists;
wdenk2d966952002-10-31 22:12:35 +0000179
wdenk6e592382004-04-18 17:39:38 +0000180/* XXX in both little & big endian machines 0xFFFF == ntohs(-1) */
Luca Ceresoli3e38e422011-05-11 03:59:54 +0000181/* default is without VLAN */
182ushort NetOurVLAN = 0xFFFF;
183/* ditto */
184ushort NetOurNativeVLAN = 0xFFFF;
wdenka3d991b2004-04-15 21:48:45 +0000185
Luca Ceresoli3e38e422011-05-11 03:59:54 +0000186/* Boot File name */
187char BootFile[128];
wdenk2d966952002-10-31 22:12:35 +0000188
Jon Loeliger643d1ab2007-07-09 17:45:14 -0500189#if defined(CONFIG_CMD_PING)
Luca Ceresoli3e38e422011-05-11 03:59:54 +0000190/* the ip address to ping */
191IPaddr_t NetPingIP;
wdenk73a8b272003-06-05 19:27:42 +0000192
193static void PingStart(void);
194#endif
195
Jon Loeliger643d1ab2007-07-09 17:45:14 -0500196#if defined(CONFIG_CMD_CDP)
wdenka3d991b2004-04-15 21:48:45 +0000197static void CDPStart(void);
198#endif
199
Jon Loeliger643d1ab2007-07-09 17:45:14 -0500200#if defined(CONFIG_CMD_SNTP)
Luca Ceresoli3e38e422011-05-11 03:59:54 +0000201/* NTP server IP address */
202IPaddr_t NetNtpServerIP;
203/* offset time from UTC */
Luca Ceresolic586ce62011-05-11 03:59:55 +0000204int NetTimeOffset;
wdenkea287de2005-04-01 00:25:43 +0000205#endif
206
Joe Hershbergerdb288a92012-05-15 08:59:04 +0000207uchar PktBuf[(PKTBUFSRX+1) * PKTSIZE_ALIGN + PKTALIGN];
wdenk2d966952002-10-31 22:12:35 +0000208
Luca Ceresoli3e38e422011-05-11 03:59:54 +0000209/* Receive packet */
Joe Hershbergerdb288a92012-05-15 08:59:04 +0000210uchar *NetRxPackets[PKTBUFSRX];
wdenk2d966952002-10-31 22:12:35 +0000211
Luca Ceresoli3e38e422011-05-11 03:59:54 +0000212/* Current RX packet handler */
213static rxhand_f *packetHandler;
Simon Glass39bccd22011-10-26 14:18:38 +0000214#ifdef CONFIG_CMD_TFTPPUT
Simon Glass4793ee62011-10-24 18:00:01 +0000215static rxhand_icmp_f *packet_icmp_handler; /* Current ICMP rx handler */
Simon Glass39bccd22011-10-26 14:18:38 +0000216#endif
Luca Ceresoli3e38e422011-05-11 03:59:54 +0000217/* Current timeout handler */
218static thand_f *timeHandler;
219/* Time base value */
220static ulong timeStart;
221/* Current timeout value */
222static ulong timeDelta;
223/* THE transmit packet */
Joe Hershbergerdb288a92012-05-15 08:59:04 +0000224uchar *NetTxPacket;
wdenk2d966952002-10-31 22:12:35 +0000225
Simon Glasse4bf0c52011-10-24 18:00:02 +0000226static int net_check_prereq(enum proto_t protocol);
wdenk2d966952002-10-31 22:12:35 +0000227
Remy Bohmer67b96e82009-10-28 22:13:39 +0100228static int NetTryCount;
229
wdenk2d966952002-10-31 22:12:35 +0000230/**********************************************************************/
wdenk73a8b272003-06-05 19:27:42 +0000231
232IPaddr_t NetArpWaitPacketIP;
233IPaddr_t NetArpWaitReplyIP;
Luca Ceresoli3e38e422011-05-11 03:59:54 +0000234/* MAC address of waiting packet's destination */
235uchar *NetArpWaitPacketMAC;
236/* THE transmit packet */
237uchar *NetArpWaitTxPacket;
wdenk73a8b272003-06-05 19:27:42 +0000238int NetArpWaitTxPacketSize;
Wolfgang Denk53677ef2008-05-20 16:00:29 +0200239uchar NetArpWaitPacketBuf[PKTSIZE_ALIGN + PKTALIGN];
wdenk73a8b272003-06-05 19:27:42 +0000240ulong NetArpWaitTimerStart;
241int NetArpWaitTry;
242
Luca Ceresoli4f63acd2011-05-11 03:59:56 +0000243void ArpRequest(void)
wdenk73a8b272003-06-05 19:27:42 +0000244{
Joe Hershbergerdb288a92012-05-15 08:59:04 +0000245 uchar *pkt;
wdenk6e592382004-04-18 17:39:38 +0000246 ARP_t *arp;
wdenk73a8b272003-06-05 19:27:42 +0000247
Robin Getz0ebf04c2009-07-23 03:01:03 -0400248 debug("ARP broadcast %d\n", NetArpWaitTry);
249
wdenk73a8b272003-06-05 19:27:42 +0000250 pkt = NetTxPacket;
251
Luca Ceresoli4f63acd2011-05-11 03:59:56 +0000252 pkt += NetSetEther(pkt, NetBcastAddr, PROT_ARP);
wdenk73a8b272003-06-05 19:27:42 +0000253
wdenk6e592382004-04-18 17:39:38 +0000254 arp = (ARP_t *) pkt;
wdenk73a8b272003-06-05 19:27:42 +0000255
Luca Ceresoli4f63acd2011-05-11 03:59:56 +0000256 arp->ar_hrd = htons(ARP_ETHER);
257 arp->ar_pro = htons(PROT_IP);
wdenk73a8b272003-06-05 19:27:42 +0000258 arp->ar_hln = 6;
259 arp->ar_pln = 4;
Luca Ceresoli4f63acd2011-05-11 03:59:56 +0000260 arp->ar_op = htons(ARPOP_REQUEST);
wdenk73a8b272003-06-05 19:27:42 +0000261
Luca Ceresoli3e38e422011-05-11 03:59:54 +0000262 /* source ET addr */
Luca Ceresoli4f63acd2011-05-11 03:59:56 +0000263 memcpy(&arp->ar_data[0], NetOurEther, 6);
Luca Ceresoli3e38e422011-05-11 03:59:54 +0000264 /* source IP addr */
Luca Ceresoli4f63acd2011-05-11 03:59:56 +0000265 NetWriteIP((uchar *) &arp->ar_data[6], NetOurIP);
Simon Glassed1ada72011-10-26 14:18:39 +0000266 /* dest ET addr = 0 */
267 memset(&arp->ar_data[10], '\0', 6);
wdenk6e592382004-04-18 17:39:38 +0000268 if ((NetArpWaitPacketIP & NetOurSubnetMask) !=
269 (NetOurIP & NetOurSubnetMask)) {
270 if (NetOurGatewayIP == 0) {
Luca Ceresoli4f63acd2011-05-11 03:59:56 +0000271 puts("## Warning: gatewayip needed but not set\n");
Wolfgang Denkd509b812006-03-12 01:13:30 +0100272 NetArpWaitReplyIP = NetArpWaitPacketIP;
273 } else {
274 NetArpWaitReplyIP = NetOurGatewayIP;
wdenk6e592382004-04-18 17:39:38 +0000275 }
wdenk6e592382004-04-18 17:39:38 +0000276 } else {
277 NetArpWaitReplyIP = NetArpWaitPacketIP;
278 }
wdenk73a8b272003-06-05 19:27:42 +0000279
Luca Ceresoli4f63acd2011-05-11 03:59:56 +0000280 NetWriteIP((uchar *) &arp->ar_data[16], NetArpWaitReplyIP);
281 (void) eth_send(NetTxPacket, (pkt - NetTxPacket) + ARP_HDR_SIZE);
wdenk73a8b272003-06-05 19:27:42 +0000282}
283
284void ArpTimeoutCheck(void)
285{
286 ulong t;
287
288 if (!NetArpWaitPacketIP)
289 return;
290
291 t = get_timer(0);
292
293 /* check for arp timeout */
Bartlomiej Sieka49f3bdb2008-10-01 15:26:28 +0200294 if ((t - NetArpWaitTimerStart) > ARP_TIMEOUT) {
wdenk73a8b272003-06-05 19:27:42 +0000295 NetArpWaitTry++;
296
297 if (NetArpWaitTry >= ARP_TIMEOUT_COUNT) {
Luca Ceresoli4f63acd2011-05-11 03:59:56 +0000298 puts("\nARP Retry count exceeded; starting again\n");
wdenk73a8b272003-06-05 19:27:42 +0000299 NetArpWaitTry = 0;
300 NetStartAgain();
301 } else {
302 NetArpWaitTimerStart = t;
303 ArpRequest();
304 }
305 }
306}
307
Simon Glasse4a3d572011-10-27 06:24:32 +0000308/*
309 * Check if autoload is enabled. If so, use either NFS or TFTP to download
310 * the boot file.
311 */
312void net_auto_load(void)
313{
314 const char *s = getenv("autoload");
315
316 if (s != NULL) {
317 if (*s == 'n') {
318 /*
319 * Just use BOOTP/RARP to configure system;
320 * Do not use TFTP to load the bootfile.
321 */
322 NetState = NETLOOP_SUCCESS;
323 return;
324 }
325#if defined(CONFIG_CMD_NFS)
326 if (strcmp(s, "NFS") == 0) {
327 /*
328 * Use NFS to load the bootfile.
329 */
330 NfsStart();
331 return;
332 }
333#endif
334 }
335 TftpStart(TFTPGET);
336}
337
Simon Glasse4bf0c52011-10-24 18:00:02 +0000338static void NetInitLoop(enum proto_t protocol)
Heiko Schocher2f70c492009-02-10 09:38:52 +0100339{
Luca Ceresolic586ce62011-05-11 03:59:55 +0000340 static int env_changed_id;
Heiko Schocher2f70c492009-02-10 09:38:52 +0100341 bd_t *bd = gd->bd;
Luca Ceresoli4f63acd2011-05-11 03:59:56 +0000342 int env_id = get_env_id();
Heiko Schocher2f70c492009-02-10 09:38:52 +0100343
344 /* update only when the environment has changed */
Michael Zaidman3c172c42009-04-04 01:43:00 +0300345 if (env_changed_id != env_id) {
Enric Balletbo i Serra23a70bf2011-05-31 21:01:47 +0000346 NetOurIP = getenv_IPaddr("ipaddr");
347 NetCopyIP(&bd->bi_ip_addr, &NetOurIP);
Luca Ceresoli4f63acd2011-05-11 03:59:56 +0000348 NetOurGatewayIP = getenv_IPaddr("gatewayip");
349 NetOurSubnetMask = getenv_IPaddr("netmask");
350 NetServerIP = getenv_IPaddr("serverip");
Heiko Schocher2f70c492009-02-10 09:38:52 +0100351 NetOurNativeVLAN = getenv_VLAN("nvlan");
Michael Zaidman3c172c42009-04-04 01:43:00 +0300352 NetOurVLAN = getenv_VLAN("vlan");
Robin Getz1a32bf42009-07-20 14:53:54 -0400353#if defined(CONFIG_CMD_DNS)
354 NetOurDNSIP = getenv_IPaddr("dnsip");
355#endif
Michael Zaidman3c172c42009-04-04 01:43:00 +0300356 env_changed_id = env_id;
Heiko Schocher2f70c492009-02-10 09:38:52 +0100357 }
Michael Zaidman3c172c42009-04-04 01:43:00 +0300358
Heiko Schocherda954272009-04-28 08:36:11 +0200359 return;
Heiko Schocher2f70c492009-02-10 09:38:52 +0100360}
361
wdenk73a8b272003-06-05 19:27:42 +0000362/**********************************************************************/
wdenk2d966952002-10-31 22:12:35 +0000363/*
364 * Main network processing loop.
365 */
366
Simon Glasse4bf0c52011-10-24 18:00:02 +0000367int NetLoop(enum proto_t protocol)
wdenk2d966952002-10-31 22:12:35 +0000368{
wdenk2d966952002-10-31 22:12:35 +0000369 bd_t *bd = gd->bd;
Simon Glass4793ee62011-10-24 18:00:01 +0000370 int ret = -1;
wdenk2d966952002-10-31 22:12:35 +0000371
wdenk2d966952002-10-31 22:12:35 +0000372 NetRestarted = 0;
373 NetDevExists = 0;
wdenk2d966952002-10-31 22:12:35 +0000374
wdenk73a8b272003-06-05 19:27:42 +0000375 /* XXX problem with bss workaround */
376 NetArpWaitPacketMAC = NULL;
377 NetArpWaitTxPacket = NULL;
378 NetArpWaitPacketIP = 0;
379 NetArpWaitReplyIP = 0;
380 NetArpWaitTxPacket = NULL;
381 NetTxPacket = NULL;
Remy Bohmer67b96e82009-10-28 22:13:39 +0100382 NetTryCount = 1;
wdenk73a8b272003-06-05 19:27:42 +0000383
wdenk2d966952002-10-31 22:12:35 +0000384 if (!NetTxPacket) {
385 int i;
wdenk2d966952002-10-31 22:12:35 +0000386 /*
387 * Setup packet buffers, aligned correctly.
388 */
389 NetTxPacket = &PktBuf[0] + (PKTALIGN - 1);
390 NetTxPacket -= (ulong)NetTxPacket % PKTALIGN;
Luca Ceresolid3c65b02011-05-04 02:40:43 +0000391 for (i = 0; i < PKTBUFSRX; i++)
wdenk2d966952002-10-31 22:12:35 +0000392 NetRxPackets[i] = NetTxPacket + (i+1)*PKTSIZE_ALIGN;
wdenk73a8b272003-06-05 19:27:42 +0000393 }
394
395 if (!NetArpWaitTxPacket) {
396 NetArpWaitTxPacket = &NetArpWaitPacketBuf[0] + (PKTALIGN - 1);
397 NetArpWaitTxPacket -= (ulong)NetArpWaitTxPacket % PKTALIGN;
398 NetArpWaitTxPacketSize = 0;
wdenk2d966952002-10-31 22:12:35 +0000399 }
400
Simon Glass573f14f2011-12-10 11:08:06 +0000401 bootstage_mark_name(BOOTSTAGE_ID_ETH_START, "eth_start");
wdenk2d966952002-10-31 22:12:35 +0000402 eth_halt();
wdenka3d991b2004-04-15 21:48:45 +0000403 eth_set_current();
wdenkb1bf6f22005-04-03 14:52:59 +0000404 if (eth_init(bd) < 0) {
405 eth_halt();
Luca Ceresoli92895de2011-05-04 02:40:45 +0000406 return -1;
wdenkb1bf6f22005-04-03 14:52:59 +0000407 }
wdenk2d966952002-10-31 22:12:35 +0000408
409restart:
Luca Ceresoli4f63acd2011-05-11 03:59:56 +0000410 memcpy(NetOurEther, eth_get_dev()->enetaddr, 6);
wdenk2d966952002-10-31 22:12:35 +0000411
412 NetState = NETLOOP_CONTINUE;
413
414 /*
415 * Start the ball rolling with the given start function. From
416 * here on, this code is a state machine driven by received
417 * packets and timer events.
418 */
Heiko Schocher2f70c492009-02-10 09:38:52 +0100419 NetInitLoop(protocol);
wdenk2d966952002-10-31 22:12:35 +0000420
Luca Ceresoli4f63acd2011-05-11 03:59:56 +0000421 switch (net_check_prereq(protocol)) {
wdenk2d966952002-10-31 22:12:35 +0000422 case 1:
423 /* network not configured */
wdenkb1bf6f22005-04-03 14:52:59 +0000424 eth_halt();
Luca Ceresoli92895de2011-05-04 02:40:45 +0000425 return -1;
wdenk2d966952002-10-31 22:12:35 +0000426
wdenk2d966952002-10-31 22:12:35 +0000427 case 2:
428 /* network device not configured */
429 break;
wdenk2d966952002-10-31 22:12:35 +0000430
431 case 0:
wdenk2d966952002-10-31 22:12:35 +0000432 NetDevExists = 1;
Simon Glasse4bf0c52011-10-24 18:00:02 +0000433 NetBootFileXferSize = 0;
wdenk2d966952002-10-31 22:12:35 +0000434 switch (protocol) {
Simon Glasse4bf0c52011-10-24 18:00:02 +0000435 case TFTPGET:
Simon Glass1fb7cd42011-10-24 18:00:07 +0000436#ifdef CONFIG_CMD_TFTPPUT
437 case TFTPPUT:
438#endif
wdenk2d966952002-10-31 22:12:35 +0000439 /* always use ARP to get server ethernet address */
Simon Glasse4bf0c52011-10-24 18:00:02 +0000440 TftpStart(protocol);
wdenk2d966952002-10-31 22:12:35 +0000441 break;
Luca Ceresoli7a83af02011-05-17 00:03:40 +0000442#ifdef CONFIG_CMD_TFTPSRV
443 case TFTPSRV:
444 TftpStartServer();
445 break;
446#endif
Jon Loeliger643d1ab2007-07-09 17:45:14 -0500447#if defined(CONFIG_CMD_DHCP)
wdenk2d966952002-10-31 22:12:35 +0000448 case DHCP:
wdenkd407bf52004-10-11 22:51:13 +0000449 BootpTry = 0;
Michael Zaidman09133f82009-07-14 23:37:12 +0300450 NetOurIP = 0;
wdenk2d966952002-10-31 22:12:35 +0000451 DhcpRequest(); /* Basically same as BOOTP */
452 break;
Jon Loeliger610f2e92007-07-10 11:05:02 -0500453#endif
wdenk2d966952002-10-31 22:12:35 +0000454
455 case BOOTP:
456 BootpTry = 0;
Michael Zaidman09133f82009-07-14 23:37:12 +0300457 NetOurIP = 0;
Luca Ceresoli4f63acd2011-05-11 03:59:56 +0000458 BootpRequest();
wdenk2d966952002-10-31 22:12:35 +0000459 break;
460
Peter Tyserbf6cb242010-09-30 11:25:48 -0500461#if defined(CONFIG_CMD_RARP)
wdenk2d966952002-10-31 22:12:35 +0000462 case RARP:
463 RarpTry = 0;
Michael Zaidman09133f82009-07-14 23:37:12 +0300464 NetOurIP = 0;
Luca Ceresoli4f63acd2011-05-11 03:59:56 +0000465 RarpRequest();
wdenk2d966952002-10-31 22:12:35 +0000466 break;
Peter Tyserbf6cb242010-09-30 11:25:48 -0500467#endif
Jon Loeliger643d1ab2007-07-09 17:45:14 -0500468#if defined(CONFIG_CMD_PING)
wdenk73a8b272003-06-05 19:27:42 +0000469 case PING:
470 PingStart();
471 break;
472#endif
Jon Loeliger643d1ab2007-07-09 17:45:14 -0500473#if defined(CONFIG_CMD_NFS)
wdenkcbd8a352004-02-24 02:00:03 +0000474 case NFS:
475 NfsStart();
476 break;
477#endif
Jon Loeliger643d1ab2007-07-09 17:45:14 -0500478#if defined(CONFIG_CMD_CDP)
wdenka3d991b2004-04-15 21:48:45 +0000479 case CDP:
480 CDPStart();
481 break;
482#endif
wdenk68ceb292004-08-02 21:11:11 +0000483#ifdef CONFIG_NETCONSOLE
484 case NETCONS:
485 NcStart();
486 break;
487#endif
Jon Loeliger643d1ab2007-07-09 17:45:14 -0500488#if defined(CONFIG_CMD_SNTP)
wdenkea287de2005-04-01 00:25:43 +0000489 case SNTP:
490 SntpStart();
491 break;
492#endif
Robin Getz1a32bf42009-07-20 14:53:54 -0400493#if defined(CONFIG_CMD_DNS)
494 case DNS:
495 DnsStart();
496 break;
497#endif
wdenk2d966952002-10-31 22:12:35 +0000498 default:
499 break;
500 }
501
wdenk2d966952002-10-31 22:12:35 +0000502 break;
503 }
504
Jon Loeliger643d1ab2007-07-09 17:45:14 -0500505#if defined(CONFIG_MII) || defined(CONFIG_CMD_MII)
Luca Ceresoli3e38e422011-05-11 03:59:54 +0000506#if defined(CONFIG_SYS_FAULT_ECHO_LINK_DOWN) && \
507 defined(CONFIG_STATUS_LED) && \
508 defined(STATUS_LED_RED)
wdenkfc3e2162003-10-08 22:33:00 +0000509 /*
wdenk42d1f032003-10-15 23:53:47 +0000510 * Echo the inverted link state to the fault LED.
wdenkfc3e2162003-10-08 22:33:00 +0000511 */
Luca Ceresolid3c65b02011-05-04 02:40:43 +0000512 if (miiphy_link(eth_get_dev()->name, CONFIG_SYS_FAULT_MII_ADDR))
Luca Ceresoli4f63acd2011-05-11 03:59:56 +0000513 status_led_set(STATUS_LED_RED, STATUS_LED_OFF);
Luca Ceresolid3c65b02011-05-04 02:40:43 +0000514 else
Luca Ceresoli4f63acd2011-05-11 03:59:56 +0000515 status_led_set(STATUS_LED_RED, STATUS_LED_ON);
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200516#endif /* CONFIG_SYS_FAULT_ECHO_LINK_DOWN, ... */
wdenkfc3e2162003-10-08 22:33:00 +0000517#endif /* CONFIG_MII, ... */
wdenk2d966952002-10-31 22:12:35 +0000518
519 /*
520 * Main packet reception loop. Loop receiving packets until
wdenk59acc292005-04-03 14:18:51 +0000521 * someone sets `NetState' to a state that terminates.
wdenk2d966952002-10-31 22:12:35 +0000522 */
523 for (;;) {
524 WATCHDOG_RESET();
525#ifdef CONFIG_SHOW_ACTIVITY
Joe Hershberger48522bb2012-05-15 08:59:08 +0000526 show_activity(1);
wdenk2d966952002-10-31 22:12:35 +0000527#endif
528 /*
529 * Check the ethernet for a new packet. The ethernet
530 * receive routine will process it.
531 */
Guennadi Liakhovetski40cb90e2008-04-03 17:04:19 +0200532 eth_rx();
wdenk2d966952002-10-31 22:12:35 +0000533
534 /*
535 * Abort if ctrl-c was pressed.
536 */
537 if (ctrlc()) {
wdenk8bde7f72003-06-27 21:31:46 +0000538 eth_halt();
Luca Ceresoli4f63acd2011-05-11 03:59:56 +0000539 puts("\nAbort\n");
Simon Glass4793ee62011-10-24 18:00:01 +0000540 goto done;
wdenk2d966952002-10-31 22:12:35 +0000541 }
542
wdenk73a8b272003-06-05 19:27:42 +0000543 ArpTimeoutCheck();
wdenk2d966952002-10-31 22:12:35 +0000544
545 /*
546 * Check for a timeout, and run the timeout handler
547 * if we have one.
548 */
wdenke0ac62d2003-08-17 18:55:18 +0000549 if (timeHandler && ((get_timer(0) - timeStart) > timeDelta)) {
wdenk2d966952002-10-31 22:12:35 +0000550 thand_f *x;
551
Jon Loeliger643d1ab2007-07-09 17:45:14 -0500552#if defined(CONFIG_MII) || defined(CONFIG_CMD_MII)
Luca Ceresoli4f63acd2011-05-11 03:59:56 +0000553#if defined(CONFIG_SYS_FAULT_ECHO_LINK_DOWN) && \
554 defined(CONFIG_STATUS_LED) && \
555 defined(STATUS_LED_RED)
wdenkfc3e2162003-10-08 22:33:00 +0000556 /*
wdenk42d1f032003-10-15 23:53:47 +0000557 * Echo the inverted link state to the fault LED.
wdenkfc3e2162003-10-08 22:33:00 +0000558 */
Luca Ceresoli4f63acd2011-05-11 03:59:56 +0000559 if (miiphy_link(eth_get_dev()->name,
Luca Ceresoli3e38e422011-05-11 03:59:54 +0000560 CONFIG_SYS_FAULT_MII_ADDR)) {
Luca Ceresoli4f63acd2011-05-11 03:59:56 +0000561 status_led_set(STATUS_LED_RED, STATUS_LED_OFF);
wdenkfc3e2162003-10-08 22:33:00 +0000562 } else {
Luca Ceresoli4f63acd2011-05-11 03:59:56 +0000563 status_led_set(STATUS_LED_RED, STATUS_LED_ON);
wdenkfc3e2162003-10-08 22:33:00 +0000564 }
Luca Ceresoli4f63acd2011-05-11 03:59:56 +0000565#endif /* CONFIG_SYS_FAULT_ECHO_LINK_DOWN, ... */
wdenkfc3e2162003-10-08 22:33:00 +0000566#endif /* CONFIG_MII, ... */
wdenk2d966952002-10-31 22:12:35 +0000567 x = timeHandler;
568 timeHandler = (thand_f *)0;
569 (*x)();
570 }
571
572
573 switch (NetState) {
574
575 case NETLOOP_RESTART:
wdenk2d966952002-10-31 22:12:35 +0000576 NetRestarted = 1;
wdenk2d966952002-10-31 22:12:35 +0000577 goto restart;
578
579 case NETLOOP_SUCCESS:
580 if (NetBootFileXferSize > 0) {
Wolfgang Denkf34024d2007-09-12 00:48:57 +0200581 char buf[20];
wdenk2d966952002-10-31 22:12:35 +0000582 printf("Bytes transferred = %ld (%lx hex)\n",
583 NetBootFileXferSize,
584 NetBootFileXferSize);
Wolfgang Denkf34024d2007-09-12 00:48:57 +0200585 sprintf(buf, "%lX", NetBootFileXferSize);
wdenk2d966952002-10-31 22:12:35 +0000586 setenv("filesize", buf);
wdenka3d991b2004-04-15 21:48:45 +0000587
588 sprintf(buf, "%lX", (unsigned long)load_addr);
589 setenv("fileaddr", buf);
wdenk2d966952002-10-31 22:12:35 +0000590 }
591 eth_halt();
Simon Glass4793ee62011-10-24 18:00:01 +0000592 ret = NetBootFileXferSize;
593 goto done;
wdenk2d966952002-10-31 22:12:35 +0000594
595 case NETLOOP_FAIL:
Simon Glass4793ee62011-10-24 18:00:01 +0000596 goto done;
wdenk2d966952002-10-31 22:12:35 +0000597 }
598 }
Simon Glass4793ee62011-10-24 18:00:01 +0000599
600done:
Simon Glass39bccd22011-10-26 14:18:38 +0000601#ifdef CONFIG_CMD_TFTPPUT
Simon Glass4793ee62011-10-24 18:00:01 +0000602 /* Clear out the handlers */
603 NetSetHandler(NULL);
604 net_set_icmp_handler(NULL);
Simon Glass39bccd22011-10-26 14:18:38 +0000605#endif
Simon Glass4793ee62011-10-24 18:00:01 +0000606 return ret;
wdenk2d966952002-10-31 22:12:35 +0000607}
608
609/**********************************************************************/
610
611static void
612startAgainTimeout(void)
613{
614 NetState = NETLOOP_RESTART;
615}
616
617static void
Luca Ceresoli03eb1292011-04-18 06:19:50 +0000618startAgainHandler(uchar *pkt, unsigned dest, IPaddr_t sip,
619 unsigned src, unsigned len)
wdenk2d966952002-10-31 22:12:35 +0000620{
621 /* Totally ignore the packet */
622}
623
Luca Ceresoli4f63acd2011-05-11 03:59:56 +0000624void NetStartAgain(void)
wdenk2d966952002-10-31 22:12:35 +0000625{
wdenk6e592382004-04-18 17:39:38 +0000626 char *nretry;
Remy Bohmer67b96e82009-10-28 22:13:39 +0100627 int retry_forever = 0;
628 unsigned long retrycnt = 0;
wdenka3d991b2004-04-15 21:48:45 +0000629
Remy Bohmer67b96e82009-10-28 22:13:39 +0100630 nretry = getenv("netretry");
631 if (nretry) {
632 if (!strcmp(nretry, "yes"))
633 retry_forever = 1;
634 else if (!strcmp(nretry, "no"))
635 retrycnt = 0;
636 else if (!strcmp(nretry, "once"))
637 retrycnt = 1;
638 else
639 retrycnt = simple_strtoul(nretry, NULL, 0);
640 } else
641 retry_forever = 1;
642
643 if ((!retry_forever) && (NetTryCount >= retrycnt)) {
644 eth_halt();
wdenka3d991b2004-04-15 21:48:45 +0000645 NetState = NETLOOP_FAIL;
646 return;
647 }
Remy Bohmer67b96e82009-10-28 22:13:39 +0100648
649 NetTryCount++;
650
Luca Ceresoli4f63acd2011-05-11 03:59:56 +0000651 eth_halt();
Matthias Fuchs8b0c5c12007-12-27 16:58:41 +0100652#if !defined(CONFIG_NET_DO_NOT_TRY_ANOTHER)
Luca Ceresoli4f63acd2011-05-11 03:59:56 +0000653 eth_try_another(!NetRestarted);
Matthias Fuchs8b0c5c12007-12-27 16:58:41 +0100654#endif
Luca Ceresoli4f63acd2011-05-11 03:59:56 +0000655 eth_init(gd->bd);
wdenk6e592382004-04-18 17:39:38 +0000656 if (NetRestartWrap) {
wdenk2d966952002-10-31 22:12:35 +0000657 NetRestartWrap = 0;
Remy Bohmer67b96e82009-10-28 22:13:39 +0100658 if (NetDevExists) {
Luca Ceresoli4f63acd2011-05-11 03:59:56 +0000659 NetSetTimeout(10000UL, startAgainTimeout);
660 NetSetHandler(startAgainHandler);
wdenk6e592382004-04-18 17:39:38 +0000661 } else {
wdenk2d966952002-10-31 22:12:35 +0000662 NetState = NETLOOP_FAIL;
663 }
wdenk6e592382004-04-18 17:39:38 +0000664 } else {
wdenk2d966952002-10-31 22:12:35 +0000665 NetState = NETLOOP_RESTART;
666 }
wdenk2d966952002-10-31 22:12:35 +0000667}
668
669/**********************************************************************/
670/*
671 * Miscelaneous bits.
672 */
673
674void
Luca Ceresoli6b147d12011-05-04 02:40:44 +0000675NetSetHandler(rxhand_f *f)
wdenk2d966952002-10-31 22:12:35 +0000676{
677 packetHandler = f;
678}
679
Simon Glass39bccd22011-10-26 14:18:38 +0000680#ifdef CONFIG_CMD_TFTPPUT
Simon Glass4793ee62011-10-24 18:00:01 +0000681void net_set_icmp_handler(rxhand_icmp_f *f)
682{
683 packet_icmp_handler = f;
684}
Simon Glass39bccd22011-10-26 14:18:38 +0000685#endif
wdenk2d966952002-10-31 22:12:35 +0000686
687void
Luca Ceresoli6b147d12011-05-04 02:40:44 +0000688NetSetTimeout(ulong iv, thand_f *f)
wdenk2d966952002-10-31 22:12:35 +0000689{
690 if (iv == 0) {
691 timeHandler = (thand_f *)0;
692 } else {
693 timeHandler = f;
wdenke0ac62d2003-08-17 18:55:18 +0000694 timeStart = get_timer(0);
695 timeDelta = iv;
wdenk2d966952002-10-31 22:12:35 +0000696 }
697}
698
699
700void
Joe Hershbergerdb288a92012-05-15 08:59:04 +0000701NetSendPacket(uchar *pkt, int len)
wdenk2d966952002-10-31 22:12:35 +0000702{
703 (void) eth_send(pkt, len);
704}
705
wdenk73a8b272003-06-05 19:27:42 +0000706int
707NetSendUDPPacket(uchar *ether, IPaddr_t dest, int dport, int sport, int len)
708{
wdenka3d991b2004-04-15 21:48:45 +0000709 uchar *pkt;
710
wdenk73a8b272003-06-05 19:27:42 +0000711 /* convert to new style broadcast */
712 if (dest == 0)
713 dest = 0xFFFFFFFF;
wdenk2d966952002-10-31 22:12:35 +0000714
wdenk73a8b272003-06-05 19:27:42 +0000715 /* if broadcast, make the ether address a broadcast and don't do ARP */
716 if (dest == 0xFFFFFFFF)
717 ether = NetBcastAddr;
718
Luca Ceresoli3e38e422011-05-11 03:59:54 +0000719 /*
720 * if MAC address was not discovered yet, save the packet and do
721 * an ARP request
722 */
wdenk73a8b272003-06-05 19:27:42 +0000723 if (memcmp(ether, NetEtherNullAddr, 6) == 0) {
724
Matthias Weisserea45cb02011-12-03 03:29:44 +0000725 debug("sending ARP for %08x\n", dest);
Robin Getz0ebf04c2009-07-23 03:01:03 -0400726
wdenk73a8b272003-06-05 19:27:42 +0000727 NetArpWaitPacketIP = dest;
728 NetArpWaitPacketMAC = ether;
wdenka3d991b2004-04-15 21:48:45 +0000729
730 pkt = NetArpWaitTxPacket;
Luca Ceresoli4f63acd2011-05-11 03:59:56 +0000731 pkt += NetSetEther(pkt, NetArpWaitPacketMAC, PROT_IP);
wdenka3d991b2004-04-15 21:48:45 +0000732
Luca Ceresoli4f63acd2011-05-11 03:59:56 +0000733 NetSetIP(pkt, dest, dport, sport, len);
Luca Ceresoli3e38e422011-05-11 03:59:54 +0000734 memcpy(pkt + IP_HDR_SIZE, (uchar *)NetTxPacket +
735 (pkt - (uchar *)NetArpWaitTxPacket) + IP_HDR_SIZE, len);
wdenk73a8b272003-06-05 19:27:42 +0000736
737 /* size of the waiting packet */
Luca Ceresoli3e38e422011-05-11 03:59:54 +0000738 NetArpWaitTxPacketSize = (pkt - NetArpWaitTxPacket) +
739 IP_HDR_SIZE + len;
wdenk73a8b272003-06-05 19:27:42 +0000740
741 /* and do the ARP request */
742 NetArpWaitTry = 1;
743 NetArpWaitTimerStart = get_timer(0);
744 ArpRequest();
745 return 1; /* waiting */
746 }
747
Matthias Weisserea45cb02011-12-03 03:29:44 +0000748 debug("sending UDP to %08x/%pM\n", dest, ether);
wdenk73a8b272003-06-05 19:27:42 +0000749
wdenka3d991b2004-04-15 21:48:45 +0000750 pkt = (uchar *)NetTxPacket;
Luca Ceresoli4f63acd2011-05-11 03:59:56 +0000751 pkt += NetSetEther(pkt, ether, PROT_IP);
752 NetSetIP(pkt, dest, dport, sport, len);
wdenka3d991b2004-04-15 21:48:45 +0000753 (void) eth_send(NetTxPacket, (pkt - NetTxPacket) + IP_HDR_SIZE + len);
wdenk73a8b272003-06-05 19:27:42 +0000754
wdenk6e592382004-04-18 17:39:38 +0000755 return 0; /* transmitted */
wdenk73a8b272003-06-05 19:27:42 +0000756}
757
Jon Loeliger643d1ab2007-07-09 17:45:14 -0500758#if defined(CONFIG_CMD_PING)
wdenk73a8b272003-06-05 19:27:42 +0000759static ushort PingSeqNo;
760
761int PingSend(void)
762{
763 static uchar mac[6];
Joe Hershbergerdb288a92012-05-15 08:59:04 +0000764 IP_t *ip;
765 ushort *s;
wdenka3d991b2004-04-15 21:48:45 +0000766 uchar *pkt;
wdenk73a8b272003-06-05 19:27:42 +0000767
768 /* XXX always send arp request */
769
770 memcpy(mac, NetEtherNullAddr, 6);
771
Matthias Weisserea45cb02011-12-03 03:29:44 +0000772 debug("sending ARP for %08x\n", NetPingIP);
wdenk73a8b272003-06-05 19:27:42 +0000773
774 NetArpWaitPacketIP = NetPingIP;
775 NetArpWaitPacketMAC = mac;
776
wdenka3d991b2004-04-15 21:48:45 +0000777 pkt = NetArpWaitTxPacket;
778 pkt += NetSetEther(pkt, mac, PROT_IP);
wdenk73a8b272003-06-05 19:27:42 +0000779
Joe Hershbergerdb288a92012-05-15 08:59:04 +0000780 ip = (IP_t *)pkt;
wdenk73a8b272003-06-05 19:27:42 +0000781
782 /*
Luca Ceresoli3e38e422011-05-11 03:59:54 +0000783 * Construct an IP and ICMP header.
784 * (need to set no fragment bit - XXX)
wdenk73a8b272003-06-05 19:27:42 +0000785 */
Luca Ceresoli3e38e422011-05-11 03:59:54 +0000786 /* IP_HDR_SIZE / 4 (not including UDP) */
787 ip->ip_hl_v = 0x45;
wdenk73a8b272003-06-05 19:27:42 +0000788 ip->ip_tos = 0;
789 ip->ip_len = htons(IP_HDR_SIZE_NO_UDP + 8);
790 ip->ip_id = htons(NetIPID++);
Peter Tysere0c07b82008-12-01 16:26:20 -0600791 ip->ip_off = htons(IP_FLAGS_DFRAG); /* Don't fragment */
wdenk73a8b272003-06-05 19:27:42 +0000792 ip->ip_ttl = 255;
793 ip->ip_p = 0x01; /* ICMP */
794 ip->ip_sum = 0;
Luca Ceresoli3e38e422011-05-11 03:59:54 +0000795 /* already in network byte order */
Luca Ceresoli6b147d12011-05-04 02:40:44 +0000796 NetCopyIP((void *)&ip->ip_src, &NetOurIP);
Luca Ceresoli3e38e422011-05-11 03:59:54 +0000797 /* - "" - */
Luca Ceresoli6b147d12011-05-04 02:40:44 +0000798 NetCopyIP((void *)&ip->ip_dst, &NetPingIP);
wdenk73a8b272003-06-05 19:27:42 +0000799 ip->ip_sum = ~NetCksum((uchar *)ip, IP_HDR_SIZE_NO_UDP / 2);
800
801 s = &ip->udp_src; /* XXX ICMP starts here */
802 s[0] = htons(0x0800); /* echo-request, code */
803 s[1] = 0; /* checksum */
Wolfgang Denk53677ef2008-05-20 16:00:29 +0200804 s[2] = 0; /* identifier */
wdenk73a8b272003-06-05 19:27:42 +0000805 s[3] = htons(PingSeqNo++); /* sequence number */
806 s[1] = ~NetCksum((uchar *)s, 8/2);
807
808 /* size of the waiting packet */
Luca Ceresoli3e38e422011-05-11 03:59:54 +0000809 NetArpWaitTxPacketSize =
810 (pkt - NetArpWaitTxPacket) + IP_HDR_SIZE_NO_UDP + 8;
wdenk73a8b272003-06-05 19:27:42 +0000811
812 /* and do the ARP request */
813 NetArpWaitTry = 1;
814 NetArpWaitTimerStart = get_timer(0);
815 ArpRequest();
816 return 1; /* waiting */
817}
818
819static void
Luca Ceresoli4f63acd2011-05-11 03:59:56 +0000820PingTimeout(void)
wdenk73a8b272003-06-05 19:27:42 +0000821{
822 eth_halt();
823 NetState = NETLOOP_FAIL; /* we did not get the reply */
824}
825
826static void
Luca Ceresoli03eb1292011-04-18 06:19:50 +0000827PingHandler(uchar *pkt, unsigned dest, IPaddr_t sip, unsigned src,
828 unsigned len)
wdenk73a8b272003-06-05 19:27:42 +0000829{
Luca Ceresoli03eb1292011-04-18 06:19:50 +0000830 if (sip != NetPingIP)
wdenk73a8b272003-06-05 19:27:42 +0000831 return;
832
833 NetState = NETLOOP_SUCCESS;
834}
835
836static void PingStart(void)
837{
Luca Ceresoli4f63acd2011-05-11 03:59:56 +0000838 printf("Using %s device\n", eth_get_name());
Luca Ceresoli4f63acd2011-05-11 03:59:56 +0000839 NetSetTimeout(10000UL, PingTimeout);
840 NetSetHandler(PingHandler);
wdenk73a8b272003-06-05 19:27:42 +0000841
842 PingSend();
843}
Jon Loeliger610f2e92007-07-10 11:05:02 -0500844#endif
wdenk2d966952002-10-31 22:12:35 +0000845
Jon Loeliger643d1ab2007-07-09 17:45:14 -0500846#if defined(CONFIG_CMD_CDP)
wdenka3d991b2004-04-15 21:48:45 +0000847
848#define CDP_DEVICE_ID_TLV 0x0001
849#define CDP_ADDRESS_TLV 0x0002
850#define CDP_PORT_ID_TLV 0x0003
851#define CDP_CAPABILITIES_TLV 0x0004
852#define CDP_VERSION_TLV 0x0005
853#define CDP_PLATFORM_TLV 0x0006
854#define CDP_NATIVE_VLAN_TLV 0x000a
855#define CDP_APPLIANCE_VLAN_TLV 0x000e
856#define CDP_TRIGGER_TLV 0x000f
857#define CDP_POWER_CONSUMPTION_TLV 0x0010
858#define CDP_SYSNAME_TLV 0x0014
859#define CDP_SYSOBJECT_TLV 0x0015
860#define CDP_MANAGEMENT_ADDRESS_TLV 0x0016
861
Bartlomiej Sieka49f3bdb2008-10-01 15:26:28 +0200862#define CDP_TIMEOUT 250UL /* one packet every 250ms */
wdenka3d991b2004-04-15 21:48:45 +0000863
864static int CDPSeq;
865static int CDPOK;
866
867ushort CDPNativeVLAN;
868ushort CDPApplianceVLAN;
869
Luca Ceresoli3e38e422011-05-11 03:59:54 +0000870static const uchar CDP_SNAP_hdr[8] = { 0xAA, 0xAA, 0x03, 0x00, 0x00, 0x0C, 0x20,
871 0x00 };
wdenka3d991b2004-04-15 21:48:45 +0000872
873static ushort CDP_compute_csum(const uchar *buff, ushort len)
874{
875 ushort csum;
876 int odd;
877 ulong result = 0;
878 ushort leftover;
Wolfgang Denk77ddac92005-10-13 16:45:02 +0200879 ushort *p;
wdenka3d991b2004-04-15 21:48:45 +0000880
881 if (len > 0) {
882 odd = 1 & (ulong)buff;
883 if (odd) {
884 result = *buff << 8;
885 len--;
886 buff++;
887 }
888 while (len > 1) {
Wolfgang Denk77ddac92005-10-13 16:45:02 +0200889 p = (ushort *)buff;
890 result += *p++;
891 buff = (uchar *)p;
wdenka3d991b2004-04-15 21:48:45 +0000892 if (result & 0x80000000)
893 result = (result & 0xFFFF) + (result >> 16);
894 len -= 2;
895 }
896 if (len) {
897 leftover = (signed short)(*(const signed char *)buff);
Wolfgang Denk3ada8342005-11-10 20:59:46 +0100898 /* CISCO SUCKS big time! (and blows too):
899 * CDP uses the IP checksum algorithm with a twist;
900 * for the last byte it *sign* extends and sums.
901 */
Luca Ceresoli3e38e422011-05-11 03:59:54 +0000902 result = (result & 0xffff0000) |
903 ((result + leftover) & 0x0000ffff);
wdenka3d991b2004-04-15 21:48:45 +0000904 }
905 while (result >> 16)
906 result = (result & 0xFFFF) + (result >> 16);
907
908 if (odd)
Luca Ceresoli3e38e422011-05-11 03:59:54 +0000909 result = ((result >> 8) & 0xff) |
910 ((result & 0xff) << 8);
wdenka3d991b2004-04-15 21:48:45 +0000911 }
912
913 /* add up 16-bit and 17-bit words for 17+c bits */
914 result = (result & 0xffff) + (result >> 16);
915 /* add up 16-bit and 2-bit for 16+c bit */
916 result = (result & 0xffff) + (result >> 16);
917 /* add up carry.. */
918 result = (result & 0xffff) + (result >> 16);
919
920 /* negate */
921 csum = ~(ushort)result;
922
923 /* run time endian detection */
924 if (csum != htons(csum)) /* little endian */
925 csum = htons(csum);
926
927 return csum;
928}
929
930int CDPSendTrigger(void)
931{
Joe Hershbergerdb288a92012-05-15 08:59:04 +0000932 uchar *pkt;
933 ushort *s;
934 ushort *cp;
wdenka3d991b2004-04-15 21:48:45 +0000935 Ethernet_t *et;
wdenka3d991b2004-04-15 21:48:45 +0000936 int len;
937 ushort chksum;
Luca Ceresoli4f63acd2011-05-11 03:59:56 +0000938#if defined(CONFIG_CDP_DEVICE_ID) || defined(CONFIG_CDP_PORT_ID) || \
939 defined(CONFIG_CDP_VERSION) || defined(CONFIG_CDP_PLATFORM)
wdenk6e592382004-04-18 17:39:38 +0000940 char buf[32];
941#endif
wdenka3d991b2004-04-15 21:48:45 +0000942
943 pkt = NetTxPacket;
944 et = (Ethernet_t *)pkt;
945
946 /* NOTE: trigger sent not on any VLAN */
947
948 /* form ethernet header */
949 memcpy(et->et_dest, NetCDPAddr, 6);
950 memcpy(et->et_src, NetOurEther, 6);
951
952 pkt += ETHER_HDR_SIZE;
953
954 /* SNAP header */
955 memcpy((uchar *)pkt, CDP_SNAP_hdr, sizeof(CDP_SNAP_hdr));
956 pkt += sizeof(CDP_SNAP_hdr);
957
958 /* CDP header */
959 *pkt++ = 0x02; /* CDP version 2 */
960 *pkt++ = 180; /* TTL */
Joe Hershbergerdb288a92012-05-15 08:59:04 +0000961 s = (ushort *)pkt;
wdenka3d991b2004-04-15 21:48:45 +0000962 cp = s;
Luca Ceresoli3e38e422011-05-11 03:59:54 +0000963 /* checksum (0 for later calculation) */
964 *s++ = htons(0);
wdenka3d991b2004-04-15 21:48:45 +0000965
966 /* CDP fields */
967#ifdef CONFIG_CDP_DEVICE_ID
968 *s++ = htons(CDP_DEVICE_ID_TLV);
969 *s++ = htons(CONFIG_CDP_DEVICE_ID);
Mike Frysinger95823ca2009-02-11 18:23:48 -0500970 sprintf(buf, CONFIG_CDP_DEVICE_ID_PREFIX "%pm", NetOurEther);
wdenka3d991b2004-04-15 21:48:45 +0000971 memcpy((uchar *)s, buf, 16);
972 s += 16 / 2;
973#endif
974
975#ifdef CONFIG_CDP_PORT_ID
976 *s++ = htons(CDP_PORT_ID_TLV);
977 memset(buf, 0, sizeof(buf));
978 sprintf(buf, CONFIG_CDP_PORT_ID, eth_get_dev_index());
979 len = strlen(buf);
980 if (len & 1) /* make it even */
981 len++;
982 *s++ = htons(len + 4);
983 memcpy((uchar *)s, buf, len);
984 s += len / 2;
985#endif
986
987#ifdef CONFIG_CDP_CAPABILITIES
988 *s++ = htons(CDP_CAPABILITIES_TLV);
989 *s++ = htons(8);
990 *(ulong *)s = htonl(CONFIG_CDP_CAPABILITIES);
991 s += 2;
992#endif
993
994#ifdef CONFIG_CDP_VERSION
995 *s++ = htons(CDP_VERSION_TLV);
996 memset(buf, 0, sizeof(buf));
997 strcpy(buf, CONFIG_CDP_VERSION);
998 len = strlen(buf);
999 if (len & 1) /* make it even */
1000 len++;
1001 *s++ = htons(len + 4);
1002 memcpy((uchar *)s, buf, len);
1003 s += len / 2;
1004#endif
1005
1006#ifdef CONFIG_CDP_PLATFORM
1007 *s++ = htons(CDP_PLATFORM_TLV);
1008 memset(buf, 0, sizeof(buf));
1009 strcpy(buf, CONFIG_CDP_PLATFORM);
1010 len = strlen(buf);
1011 if (len & 1) /* make it even */
1012 len++;
1013 *s++ = htons(len + 4);
1014 memcpy((uchar *)s, buf, len);
1015 s += len / 2;
1016#endif
1017
1018#ifdef CONFIG_CDP_TRIGGER
1019 *s++ = htons(CDP_TRIGGER_TLV);
1020 *s++ = htons(8);
1021 *(ulong *)s = htonl(CONFIG_CDP_TRIGGER);
1022 s += 2;
1023#endif
1024
1025#ifdef CONFIG_CDP_POWER_CONSUMPTION
1026 *s++ = htons(CDP_POWER_CONSUMPTION_TLV);
1027 *s++ = htons(6);
1028 *s++ = htons(CONFIG_CDP_POWER_CONSUMPTION);
1029#endif
1030
1031 /* length of ethernet packet */
1032 len = (uchar *)s - ((uchar *)NetTxPacket + ETHER_HDR_SIZE);
1033 et->et_protlen = htons(len);
1034
1035 len = ETHER_HDR_SIZE + sizeof(CDP_SNAP_hdr);
Luca Ceresoli3e38e422011-05-11 03:59:54 +00001036 chksum = CDP_compute_csum((uchar *)NetTxPacket + len,
1037 (uchar *)s - (NetTxPacket + len));
wdenka3d991b2004-04-15 21:48:45 +00001038 if (chksum == 0)
1039 chksum = 0xFFFF;
1040 *cp = htons(chksum);
1041
1042 (void) eth_send(NetTxPacket, (uchar *)s - NetTxPacket);
1043 return 0;
1044}
1045
1046static void
Luca Ceresoli4f63acd2011-05-11 03:59:56 +00001047CDPTimeout(void)
wdenka3d991b2004-04-15 21:48:45 +00001048{
1049 CDPSeq++;
1050
1051 if (CDPSeq < 3) {
Luca Ceresoli4f63acd2011-05-11 03:59:56 +00001052 NetSetTimeout(CDP_TIMEOUT, CDPTimeout);
wdenka3d991b2004-04-15 21:48:45 +00001053 CDPSendTrigger();
1054 return;
1055 }
1056
1057 /* if not OK try again */
1058 if (!CDPOK)
1059 NetStartAgain();
1060 else
1061 NetState = NETLOOP_SUCCESS;
1062}
1063
1064static void
Luca Ceresoli03eb1292011-04-18 06:19:50 +00001065CDPDummyHandler(uchar *pkt, unsigned dest, IPaddr_t sip, unsigned src,
1066 unsigned len)
wdenka3d991b2004-04-15 21:48:45 +00001067{
1068 /* nothing */
1069}
1070
1071static void
Luca Ceresoli6b147d12011-05-04 02:40:44 +00001072CDPHandler(const uchar *pkt, unsigned len)
wdenka3d991b2004-04-15 21:48:45 +00001073{
1074 const uchar *t;
1075 const ushort *ss;
1076 ushort type, tlen;
wdenka3d991b2004-04-15 21:48:45 +00001077 ushort vlan, nvlan;
1078
1079 /* minimum size? */
1080 if (len < sizeof(CDP_SNAP_hdr) + 4)
1081 goto pkt_short;
1082
1083 /* check for valid CDP SNAP header */
1084 if (memcmp(pkt, CDP_SNAP_hdr, sizeof(CDP_SNAP_hdr)) != 0)
1085 return;
1086
1087 pkt += sizeof(CDP_SNAP_hdr);
1088 len -= sizeof(CDP_SNAP_hdr);
1089
1090 /* Version of CDP protocol must be >= 2 and TTL != 0 */
1091 if (pkt[0] < 0x02 || pkt[1] == 0)
1092 return;
1093
Luca Ceresoli3e38e422011-05-11 03:59:54 +00001094 /*
1095 * if version is greater than 0x02 maybe we'll have a problem;
1096 * output a warning
1097 */
wdenka3d991b2004-04-15 21:48:45 +00001098 if (pkt[0] != 0x02)
Joe Hershberger48522bb2012-05-15 08:59:08 +00001099 printf("**WARNING: CDP packet received with a protocol version "
1100 "%d > 2\n", pkt[0] & 0xff);
wdenka3d991b2004-04-15 21:48:45 +00001101
1102 if (CDP_compute_csum(pkt, len) != 0)
1103 return;
1104
1105 pkt += 4;
1106 len -= 4;
1107
1108 vlan = htons(-1);
1109 nvlan = htons(-1);
1110 while (len > 0) {
1111 if (len < 4)
1112 goto pkt_short;
1113
1114 ss = (const ushort *)pkt;
1115 type = ntohs(ss[0]);
1116 tlen = ntohs(ss[1]);
Luca Ceresolid3c65b02011-05-04 02:40:43 +00001117 if (tlen > len)
wdenka3d991b2004-04-15 21:48:45 +00001118 goto pkt_short;
wdenka3d991b2004-04-15 21:48:45 +00001119
1120 pkt += tlen;
1121 len -= tlen;
1122
1123 ss += 2; /* point ss to the data of the TLV */
1124 tlen -= 4;
1125
1126 switch (type) {
Luca Ceresolic819abe2011-05-04 02:40:46 +00001127 case CDP_DEVICE_ID_TLV:
1128 break;
1129 case CDP_ADDRESS_TLV:
1130 break;
1131 case CDP_PORT_ID_TLV:
1132 break;
1133 case CDP_CAPABILITIES_TLV:
1134 break;
1135 case CDP_VERSION_TLV:
1136 break;
1137 case CDP_PLATFORM_TLV:
1138 break;
1139 case CDP_NATIVE_VLAN_TLV:
1140 nvlan = *ss;
1141 break;
1142 case CDP_APPLIANCE_VLAN_TLV:
1143 t = (const uchar *)ss;
1144 while (tlen > 0) {
1145 if (tlen < 3)
1146 goto pkt_short;
wdenka3d991b2004-04-15 21:48:45 +00001147
Luca Ceresolic819abe2011-05-04 02:40:46 +00001148 ss = (const ushort *)(t + 1);
wdenka3d991b2004-04-15 21:48:45 +00001149
1150#ifdef CONFIG_CDP_APPLIANCE_VLAN_TYPE
Wolfgang Denk5c104192011-11-04 15:55:42 +00001151 if (t[0] == CONFIG_CDP_APPLIANCE_VLAN_TYPE)
Luca Ceresolic819abe2011-05-04 02:40:46 +00001152 vlan = *ss;
wdenka3d991b2004-04-15 21:48:45 +00001153#else
Luca Ceresolic819abe2011-05-04 02:40:46 +00001154 /* XXX will this work; dunno */
1155 vlan = ntohs(*ss);
wdenka3d991b2004-04-15 21:48:45 +00001156#endif
Luca Ceresolic819abe2011-05-04 02:40:46 +00001157 t += 3; tlen -= 3;
1158 }
1159 break;
1160 case CDP_TRIGGER_TLV:
1161 break;
1162 case CDP_POWER_CONSUMPTION_TLV:
1163 break;
1164 case CDP_SYSNAME_TLV:
1165 break;
1166 case CDP_SYSOBJECT_TLV:
1167 break;
1168 case CDP_MANAGEMENT_ADDRESS_TLV:
1169 break;
wdenka3d991b2004-04-15 21:48:45 +00001170 }
1171 }
1172
1173 CDPApplianceVLAN = vlan;
1174 CDPNativeVLAN = nvlan;
1175
1176 CDPOK = 1;
1177 return;
1178
1179 pkt_short:
1180 printf("** CDP packet is too short\n");
1181 return;
1182}
1183
1184static void CDPStart(void)
1185{
Luca Ceresoli4f63acd2011-05-11 03:59:56 +00001186 printf("Using %s device\n", eth_get_name());
wdenka3d991b2004-04-15 21:48:45 +00001187 CDPSeq = 0;
1188 CDPOK = 0;
1189
1190 CDPNativeVLAN = htons(-1);
1191 CDPApplianceVLAN = htons(-1);
1192
Luca Ceresoli4f63acd2011-05-11 03:59:56 +00001193 NetSetTimeout(CDP_TIMEOUT, CDPTimeout);
1194 NetSetHandler(CDPDummyHandler);
wdenka3d991b2004-04-15 21:48:45 +00001195
1196 CDPSendTrigger();
1197}
Jon Loeliger610f2e92007-07-10 11:05:02 -05001198#endif
wdenka3d991b2004-04-15 21:48:45 +00001199
Alessandro Rubini5cfaa4e2009-08-07 13:58:56 +02001200#ifdef CONFIG_IP_DEFRAG
1201/*
1202 * This function collects fragments in a single packet, according
1203 * to the algorithm in RFC815. It returns NULL or the pointer to
1204 * a complete packet, in static storage
1205 */
1206#ifndef CONFIG_NET_MAXDEFRAG
1207#define CONFIG_NET_MAXDEFRAG 16384
1208#endif
1209/*
1210 * MAXDEFRAG, above, is chosen in the config file and is real data
1211 * so we need to add the NFS overhead, which is more than TFTP.
1212 * To use sizeof in the internal unnamed structures, we need a real
1213 * instance (can't do "sizeof(struct rpc_t.u.reply))", unfortunately).
1214 * The compiler doesn't complain nor allocates the actual structure
1215 */
1216static struct rpc_t rpc_specimen;
1217#define IP_PKTSIZE (CONFIG_NET_MAXDEFRAG + sizeof(rpc_specimen.u.reply))
1218
1219#define IP_MAXUDP (IP_PKTSIZE - IP_HDR_SIZE_NO_UDP)
1220
1221/*
1222 * this is the packet being assembled, either data or frag control.
1223 * Fragments go by 8 bytes, so this union must be 8 bytes long
1224 */
1225struct hole {
1226 /* first_byte is address of this structure */
1227 u16 last_byte; /* last byte in this hole + 1 (begin of next hole) */
1228 u16 next_hole; /* index of next (in 8-b blocks), 0 == none */
1229 u16 prev_hole; /* index of prev, 0 == none */
1230 u16 unused;
1231};
1232
1233static IP_t *__NetDefragment(IP_t *ip, int *lenp)
1234{
Joe Hershberger48522bb2012-05-15 08:59:08 +00001235 static uchar pkt_buff[IP_PKTSIZE] __aligned(PKTALIGN);
Alessandro Rubini5cfaa4e2009-08-07 13:58:56 +02001236 static u16 first_hole, total_len;
1237 struct hole *payload, *thisfrag, *h, *newh;
1238 IP_t *localip = (IP_t *)pkt_buff;
1239 uchar *indata = (uchar *)ip;
1240 int offset8, start, len, done = 0;
1241 u16 ip_off = ntohs(ip->ip_off);
1242
1243 /* payload starts after IP header, this fragment is in there */
1244 payload = (struct hole *)(pkt_buff + IP_HDR_SIZE_NO_UDP);
1245 offset8 = (ip_off & IP_OFFS);
1246 thisfrag = payload + offset8;
1247 start = offset8 * 8;
1248 len = ntohs(ip->ip_len) - IP_HDR_SIZE_NO_UDP;
1249
1250 if (start + len > IP_MAXUDP) /* fragment extends too far */
1251 return NULL;
1252
1253 if (!total_len || localip->ip_id != ip->ip_id) {
1254 /* new (or different) packet, reset structs */
1255 total_len = 0xffff;
1256 payload[0].last_byte = ~0;
1257 payload[0].next_hole = 0;
1258 payload[0].prev_hole = 0;
1259 first_hole = 0;
1260 /* any IP header will work, copy the first we received */
1261 memcpy(localip, ip, IP_HDR_SIZE_NO_UDP);
1262 }
1263
1264 /*
1265 * What follows is the reassembly algorithm. We use the payload
1266 * array as a linked list of hole descriptors, as each hole starts
1267 * at a multiple of 8 bytes. However, last byte can be whatever value,
1268 * so it is represented as byte count, not as 8-byte blocks.
1269 */
1270
1271 h = payload + first_hole;
1272 while (h->last_byte < start) {
1273 if (!h->next_hole) {
1274 /* no hole that far away */
1275 return NULL;
1276 }
1277 h = payload + h->next_hole;
1278 }
1279
Fillod Stephanee397e592010-06-11 19:26:43 +02001280 /* last fragment may be 1..7 bytes, the "+7" forces acceptance */
1281 if (offset8 + ((len + 7) / 8) <= h - payload) {
Alessandro Rubini5cfaa4e2009-08-07 13:58:56 +02001282 /* no overlap with holes (dup fragment?) */
1283 return NULL;
1284 }
1285
1286 if (!(ip_off & IP_FLAGS_MFRAG)) {
1287 /* no more fragmentss: truncate this (last) hole */
1288 total_len = start + len;
1289 h->last_byte = start + len;
1290 }
1291
1292 /*
1293 * There is some overlap: fix the hole list. This code doesn't
1294 * deal with a fragment that overlaps with two different holes
1295 * (thus being a superset of a previously-received fragment).
1296 */
1297
Luca Ceresoli4f63acd2011-05-11 03:59:56 +00001298 if ((h >= thisfrag) && (h->last_byte <= start + len)) {
Alessandro Rubini5cfaa4e2009-08-07 13:58:56 +02001299 /* complete overlap with hole: remove hole */
1300 if (!h->prev_hole && !h->next_hole) {
1301 /* last remaining hole */
1302 done = 1;
1303 } else if (!h->prev_hole) {
1304 /* first hole */
1305 first_hole = h->next_hole;
1306 payload[h->next_hole].prev_hole = 0;
1307 } else if (!h->next_hole) {
1308 /* last hole */
1309 payload[h->prev_hole].next_hole = 0;
1310 } else {
1311 /* in the middle of the list */
1312 payload[h->next_hole].prev_hole = h->prev_hole;
1313 payload[h->prev_hole].next_hole = h->next_hole;
1314 }
1315
1316 } else if (h->last_byte <= start + len) {
1317 /* overlaps with final part of the hole: shorten this hole */
1318 h->last_byte = start;
1319
1320 } else if (h >= thisfrag) {
1321 /* overlaps with initial part of the hole: move this hole */
1322 newh = thisfrag + (len / 8);
1323 *newh = *h;
1324 h = newh;
1325 if (h->next_hole)
1326 payload[h->next_hole].prev_hole = (h - payload);
1327 if (h->prev_hole)
1328 payload[h->prev_hole].next_hole = (h - payload);
1329 else
1330 first_hole = (h - payload);
1331
1332 } else {
1333 /* fragment sits in the middle: split the hole */
1334 newh = thisfrag + (len / 8);
1335 *newh = *h;
1336 h->last_byte = start;
1337 h->next_hole = (newh - payload);
1338 newh->prev_hole = (h - payload);
1339 if (newh->next_hole)
1340 payload[newh->next_hole].prev_hole = (newh - payload);
1341 }
1342
1343 /* finally copy this fragment and possibly return whole packet */
1344 memcpy((uchar *)thisfrag, indata + IP_HDR_SIZE_NO_UDP, len);
1345 if (!done)
1346 return NULL;
1347
1348 localip->ip_len = htons(total_len);
1349 *lenp = total_len + IP_HDR_SIZE_NO_UDP;
1350 return localip;
1351}
1352
1353static inline IP_t *NetDefragment(IP_t *ip, int *lenp)
1354{
1355 u16 ip_off = ntohs(ip->ip_off);
1356 if (!(ip_off & (IP_OFFS | IP_FLAGS_MFRAG)))
1357 return ip; /* not a fragment */
1358 return __NetDefragment(ip, lenp);
1359}
1360
1361#else /* !CONFIG_IP_DEFRAG */
1362
1363static inline IP_t *NetDefragment(IP_t *ip, int *lenp)
1364{
1365 u16 ip_off = ntohs(ip->ip_off);
1366 if (!(ip_off & (IP_OFFS | IP_FLAGS_MFRAG)))
1367 return ip; /* not a fragment */
1368 return NULL;
1369}
1370#endif
wdenka3d991b2004-04-15 21:48:45 +00001371
Simon Glass8f79bb12011-10-24 18:00:00 +00001372/**
1373 * Receive an ICMP packet. We deal with REDIRECT and PING here, and silently
1374 * drop others.
1375 *
1376 * @parma ip IP packet containing the ICMP
1377 */
1378static void receive_icmp(IP_t *ip, int len, IPaddr_t src_ip, Ethernet_t *et)
1379{
1380 ICMP_t *icmph = (ICMP_t *)&ip->udp_src;
1381
1382 switch (icmph->type) {
1383 case ICMP_REDIRECT:
1384 if (icmph->code != ICMP_REDIR_HOST)
1385 return;
1386 printf(" ICMP Host Redirect to %pI4 ",
1387 &icmph->un.gateway);
1388 break;
1389#if defined(CONFIG_CMD_PING)
1390 case ICMP_ECHO_REPLY:
1391 /*
1392 * IP header OK. Pass the packet to the
1393 * current handler.
1394 */
1395 /*
1396 * XXX point to ip packet - should this use
1397 * packet_icmp_handler?
1398 */
1399 (*packetHandler)((uchar *)ip, 0, src_ip, 0, 0);
1400 break;
1401 case ICMP_ECHO_REQUEST:
1402 debug("Got ICMP ECHO REQUEST, return %d bytes\n",
1403 ETHER_HDR_SIZE + len);
1404
1405 memcpy(&et->et_dest[0], &et->et_src[0], 6);
1406 memcpy(&et->et_src[0], NetOurEther, 6);
1407
1408 ip->ip_sum = 0;
1409 ip->ip_off = 0;
1410 NetCopyIP((void *)&ip->ip_dst, &ip->ip_src);
1411 NetCopyIP((void *)&ip->ip_src, &NetOurIP);
1412 ip->ip_sum = ~NetCksum((uchar *)ip,
1413 IP_HDR_SIZE_NO_UDP >> 1);
1414
1415 icmph->type = ICMP_ECHO_REPLY;
1416 icmph->checksum = 0;
1417 icmph->checksum = ~NetCksum((uchar *)icmph,
1418 (len - IP_HDR_SIZE_NO_UDP) >> 1);
1419 (void) eth_send((uchar *)et,
1420 ETHER_HDR_SIZE + len);
1421 break;
1422#endif
1423 default:
Simon Glass39bccd22011-10-26 14:18:38 +00001424#ifdef CONFIG_CMD_TFTPPUT
Simon Glass4793ee62011-10-24 18:00:01 +00001425 if (packet_icmp_handler)
1426 packet_icmp_handler(icmph->type, icmph->code,
1427 ntohs(ip->udp_dst), src_ip, ntohs(ip->udp_src),
1428 icmph->un.data, ntohs(ip->udp_len));
Simon Glass39bccd22011-10-26 14:18:38 +00001429#endif
Simon Glass8f79bb12011-10-24 18:00:00 +00001430 break;
1431 }
1432}
1433
wdenk2d966952002-10-31 22:12:35 +00001434void
Joe Hershbergerdb288a92012-05-15 08:59:04 +00001435NetReceive(uchar *inpkt, int len)
wdenk2d966952002-10-31 22:12:35 +00001436{
1437 Ethernet_t *et;
1438 IP_t *ip;
1439 ARP_t *arp;
1440 IPaddr_t tmp;
Luca Ceresoli03eb1292011-04-18 06:19:50 +00001441 IPaddr_t src_ip;
wdenk2d966952002-10-31 22:12:35 +00001442 int x;
wdenka3d991b2004-04-15 21:48:45 +00001443 uchar *pkt;
Jon Loeliger643d1ab2007-07-09 17:45:14 -05001444#if defined(CONFIG_CMD_CDP)
wdenka3d991b2004-04-15 21:48:45 +00001445 int iscdp;
1446#endif
1447 ushort cti = 0, vlanid = VLAN_NONE, myvlanid, mynvlanid;
wdenk2d966952002-10-31 22:12:35 +00001448
Robin Getz0ebf04c2009-07-23 03:01:03 -04001449 debug("packet received\n");
wdenka3d991b2004-04-15 21:48:45 +00001450
Mike Frysingerd9bec9f2009-07-18 21:04:08 -04001451 NetRxPacket = inpkt;
1452 NetRxPacketLen = len;
wdenka3d991b2004-04-15 21:48:45 +00001453 et = (Ethernet_t *)inpkt;
1454
1455 /* too small packet? */
1456 if (len < ETHER_HDR_SIZE)
1457 return;
1458
Rafal Jaworowskif85b6072007-12-27 18:19:02 +01001459#ifdef CONFIG_API
1460 if (push_packet) {
1461 (*push_packet)(inpkt, len);
1462 return;
1463 }
1464#endif
1465
Jon Loeliger643d1ab2007-07-09 17:45:14 -05001466#if defined(CONFIG_CMD_CDP)
wdenka3d991b2004-04-15 21:48:45 +00001467 /* keep track if packet is CDP */
1468 iscdp = memcmp(et->et_dest, NetCDPAddr, 6) == 0;
1469#endif
1470
1471 myvlanid = ntohs(NetOurVLAN);
1472 if (myvlanid == (ushort)-1)
1473 myvlanid = VLAN_NONE;
1474 mynvlanid = ntohs(NetOurNativeVLAN);
1475 if (mynvlanid == (ushort)-1)
1476 mynvlanid = VLAN_NONE;
wdenk2d966952002-10-31 22:12:35 +00001477
1478 x = ntohs(et->et_protlen);
1479
Robin Getz0ebf04c2009-07-23 03:01:03 -04001480 debug("packet received\n");
wdenka3d991b2004-04-15 21:48:45 +00001481
wdenk2d966952002-10-31 22:12:35 +00001482 if (x < 1514) {
1483 /*
1484 * Got a 802 packet. Check the other protocol field.
1485 */
1486 x = ntohs(et->et_prot);
wdenka3d991b2004-04-15 21:48:45 +00001487
1488 ip = (IP_t *)(inpkt + E802_HDR_SIZE);
wdenk2d966952002-10-31 22:12:35 +00001489 len -= E802_HDR_SIZE;
wdenka3d991b2004-04-15 21:48:45 +00001490
1491 } else if (x != PROT_VLAN) { /* normal packet */
1492 ip = (IP_t *)(inpkt + ETHER_HDR_SIZE);
wdenk2d966952002-10-31 22:12:35 +00001493 len -= ETHER_HDR_SIZE;
wdenka3d991b2004-04-15 21:48:45 +00001494
1495 } else { /* VLAN packet */
1496 VLAN_Ethernet_t *vet = (VLAN_Ethernet_t *)et;
1497
Robin Getz0ebf04c2009-07-23 03:01:03 -04001498 debug("VLAN packet received\n");
1499
wdenka3d991b2004-04-15 21:48:45 +00001500 /* too small packet? */
1501 if (len < VLAN_ETHER_HDR_SIZE)
1502 return;
1503
1504 /* if no VLAN active */
1505 if ((ntohs(NetOurVLAN) & VLAN_IDMASK) == VLAN_NONE
Jon Loeliger643d1ab2007-07-09 17:45:14 -05001506#if defined(CONFIG_CMD_CDP)
wdenka3d991b2004-04-15 21:48:45 +00001507 && iscdp == 0
1508#endif
1509 )
1510 return;
1511
1512 cti = ntohs(vet->vet_tag);
1513 vlanid = cti & VLAN_IDMASK;
1514 x = ntohs(vet->vet_type);
1515
1516 ip = (IP_t *)(inpkt + VLAN_ETHER_HDR_SIZE);
1517 len -= VLAN_ETHER_HDR_SIZE;
wdenk2d966952002-10-31 22:12:35 +00001518 }
1519
Robin Getz0ebf04c2009-07-23 03:01:03 -04001520 debug("Receive from protocol 0x%x\n", x);
wdenk2d966952002-10-31 22:12:35 +00001521
Jon Loeliger643d1ab2007-07-09 17:45:14 -05001522#if defined(CONFIG_CMD_CDP)
wdenka3d991b2004-04-15 21:48:45 +00001523 if (iscdp) {
1524 CDPHandler((uchar *)ip, len);
1525 return;
1526 }
1527#endif
1528
1529 if ((myvlanid & VLAN_IDMASK) != VLAN_NONE) {
1530 if (vlanid == VLAN_NONE)
1531 vlanid = (mynvlanid & VLAN_IDMASK);
1532 /* not matched? */
1533 if (vlanid != (myvlanid & VLAN_IDMASK))
1534 return;
1535 }
1536
wdenk2d966952002-10-31 22:12:35 +00001537 switch (x) {
1538
1539 case PROT_ARP:
1540 /*
1541 * We have to deal with two types of ARP packets:
wdenk8bde7f72003-06-27 21:31:46 +00001542 * - REQUEST packets will be answered by sending our
1543 * IP address - if we know it.
1544 * - REPLY packates are expected only after we asked
1545 * for the TFTP server's or the gateway's ethernet
1546 * address; so if we receive such a packet, we set
1547 * the server ethernet address
wdenk2d966952002-10-31 22:12:35 +00001548 */
Robin Getz0ebf04c2009-07-23 03:01:03 -04001549 debug("Got ARP\n");
1550
wdenk2d966952002-10-31 22:12:35 +00001551 arp = (ARP_t *)ip;
1552 if (len < ARP_HDR_SIZE) {
1553 printf("bad length %d < %d\n", len, ARP_HDR_SIZE);
1554 return;
1555 }
Luca Ceresolid3c65b02011-05-04 02:40:43 +00001556 if (ntohs(arp->ar_hrd) != ARP_ETHER)
wdenk2d966952002-10-31 22:12:35 +00001557 return;
Luca Ceresolid3c65b02011-05-04 02:40:43 +00001558 if (ntohs(arp->ar_pro) != PROT_IP)
wdenk2d966952002-10-31 22:12:35 +00001559 return;
Luca Ceresolid3c65b02011-05-04 02:40:43 +00001560 if (arp->ar_hln != 6)
wdenk2d966952002-10-31 22:12:35 +00001561 return;
Luca Ceresolid3c65b02011-05-04 02:40:43 +00001562 if (arp->ar_pln != 4)
wdenk2d966952002-10-31 22:12:35 +00001563 return;
wdenk2d966952002-10-31 22:12:35 +00001564
Luca Ceresolid3c65b02011-05-04 02:40:43 +00001565 if (NetOurIP == 0)
wdenk2d966952002-10-31 22:12:35 +00001566 return;
wdenk2d966952002-10-31 22:12:35 +00001567
Luca Ceresolid3c65b02011-05-04 02:40:43 +00001568 if (NetReadIP(&arp->ar_data[16]) != NetOurIP)
wdenk2d966952002-10-31 22:12:35 +00001569 return;
wdenk2d966952002-10-31 22:12:35 +00001570
1571 switch (ntohs(arp->ar_op)) {
Luca Ceresoli3e38e422011-05-11 03:59:54 +00001572 case ARPOP_REQUEST:
1573 /* reply with our IP address */
Robin Getz0ebf04c2009-07-23 03:01:03 -04001574 debug("Got ARP REQUEST, return our IP\n");
wdenka3d991b2004-04-15 21:48:45 +00001575 pkt = (uchar *)et;
1576 pkt += NetSetEther(pkt, et->et_src, PROT_ARP);
wdenk2d966952002-10-31 22:12:35 +00001577 arp->ar_op = htons(ARPOP_REPLY);
Luca Ceresoli4f63acd2011-05-11 03:59:56 +00001578 memcpy(&arp->ar_data[10], &arp->ar_data[0], 6);
wdenk2d966952002-10-31 22:12:35 +00001579 NetCopyIP(&arp->ar_data[16], &arp->ar_data[6]);
Luca Ceresoli4f63acd2011-05-11 03:59:56 +00001580 memcpy(&arp->ar_data[0], NetOurEther, 6);
1581 NetCopyIP(&arp->ar_data[6], &NetOurIP);
Luca Ceresoli3e38e422011-05-11 03:59:54 +00001582 (void) eth_send((uchar *)et,
1583 (pkt - (uchar *)et) + ARP_HDR_SIZE);
wdenk2d966952002-10-31 22:12:35 +00001584 return;
wdenk73a8b272003-06-05 19:27:42 +00001585
1586 case ARPOP_REPLY: /* arp reply */
1587 /* are we waiting for a reply */
1588 if (!NetArpWaitPacketIP || !NetArpWaitPacketMAC)
1589 break;
Robin Getz97cfe862009-07-21 12:15:28 -04001590
1591#ifdef CONFIG_KEEP_SERVERADDR
1592 if (NetServerIP == NetArpWaitPacketIP) {
1593 char buf[20];
1594 sprintf(buf, "%pM", arp->ar_data);
1595 setenv("serveraddr", buf);
1596 }
1597#endif
1598
Robin Getz0ebf04c2009-07-23 03:01:03 -04001599 debug("Got ARP REPLY, set server/gtwy eth addr (%pM)\n",
Mike Frysinger95823ca2009-02-11 18:23:48 -05001600 arp->ar_data);
wdenk73a8b272003-06-05 19:27:42 +00001601
1602 tmp = NetReadIP(&arp->ar_data[6]);
1603
1604 /* matched waiting packet's address */
1605 if (tmp == NetArpWaitReplyIP) {
Robin Getz0ebf04c2009-07-23 03:01:03 -04001606 debug("Got it\n");
Joe Hershberger48522bb2012-05-15 08:59:08 +00001607
wdenk73a8b272003-06-05 19:27:42 +00001608 /* save address for later use */
Luca Ceresoli3e38e422011-05-11 03:59:54 +00001609 memcpy(NetArpWaitPacketMAC,
1610 &arp->ar_data[0], 6);
wdenk73a8b272003-06-05 19:27:42 +00001611
wdenk68ceb292004-08-02 21:11:11 +00001612#ifdef CONFIG_NETCONSOLE
Luca Ceresoli03eb1292011-04-18 06:19:50 +00001613 (*packetHandler)(0, 0, 0, 0, 0);
wdenk68ceb292004-08-02 21:11:11 +00001614#endif
wdenk73a8b272003-06-05 19:27:42 +00001615 /* modify header, and transmit it */
Joe Hershberger48522bb2012-05-15 08:59:08 +00001616 memcpy(((Ethernet_t *)NetArpWaitTxPacket)->
1617 et_dest, NetArpWaitPacketMAC, 6);
Luca Ceresoli3e38e422011-05-11 03:59:54 +00001618 (void) eth_send(NetArpWaitTxPacket,
1619 NetArpWaitTxPacketSize);
wdenk73a8b272003-06-05 19:27:42 +00001620
1621 /* no arp request pending now */
1622 NetArpWaitPacketIP = 0;
1623 NetArpWaitTxPacketSize = 0;
1624 NetArpWaitPacketMAC = NULL;
1625
1626 }
wdenk2d966952002-10-31 22:12:35 +00001627 return;
1628 default:
Luca Ceresoli3e38e422011-05-11 03:59:54 +00001629 debug("Unexpected ARP opcode 0x%x\n",
1630 ntohs(arp->ar_op));
wdenk2d966952002-10-31 22:12:35 +00001631 return;
1632 }
wdenk289f9322005-01-12 00:15:14 +00001633 break;
wdenk2d966952002-10-31 22:12:35 +00001634
Peter Tyserbf6cb242010-09-30 11:25:48 -05001635#ifdef CONFIG_CMD_RARP
wdenk2d966952002-10-31 22:12:35 +00001636 case PROT_RARP:
Robin Getz0ebf04c2009-07-23 03:01:03 -04001637 debug("Got RARP\n");
wdenk2d966952002-10-31 22:12:35 +00001638 arp = (ARP_t *)ip;
1639 if (len < ARP_HDR_SIZE) {
1640 printf("bad length %d < %d\n", len, ARP_HDR_SIZE);
1641 return;
1642 }
1643
1644 if ((ntohs(arp->ar_op) != RARPOP_REPLY) ||
1645 (ntohs(arp->ar_hrd) != ARP_ETHER) ||
1646 (ntohs(arp->ar_pro) != PROT_IP) ||
1647 (arp->ar_hln != 6) || (arp->ar_pln != 4)) {
1648
Luca Ceresoli4f63acd2011-05-11 03:59:56 +00001649 puts("invalid RARP header\n");
wdenk2d966952002-10-31 22:12:35 +00001650 } else {
Luca Ceresoli4f63acd2011-05-11 03:59:56 +00001651 NetCopyIP(&NetOurIP, &arp->ar_data[16]);
wdenk3d3befa2004-03-14 15:06:13 +00001652 if (NetServerIP == 0)
Luca Ceresoli4f63acd2011-05-11 03:59:56 +00001653 NetCopyIP(&NetServerIP, &arp->ar_data[6]);
1654 memcpy(NetServerEther, &arp->ar_data[0], 6);
wdenk2d966952002-10-31 22:12:35 +00001655
Luca Ceresoli03eb1292011-04-18 06:19:50 +00001656 (*packetHandler)(0, 0, 0, 0, 0);
wdenk2d966952002-10-31 22:12:35 +00001657 }
1658 break;
Peter Tyserbf6cb242010-09-30 11:25:48 -05001659#endif
wdenk2d966952002-10-31 22:12:35 +00001660 case PROT_IP:
Robin Getz0ebf04c2009-07-23 03:01:03 -04001661 debug("Got IP\n");
Alessandro Rubini5cfaa4e2009-08-07 13:58:56 +02001662 /* Before we start poking the header, make sure it is there */
wdenk2d966952002-10-31 22:12:35 +00001663 if (len < IP_HDR_SIZE) {
Robin Getz0ebf04c2009-07-23 03:01:03 -04001664 debug("len bad %d < %lu\n", len, (ulong)IP_HDR_SIZE);
wdenk2d966952002-10-31 22:12:35 +00001665 return;
1666 }
Alessandro Rubini5cfaa4e2009-08-07 13:58:56 +02001667 /* Check the packet length */
wdenk2d966952002-10-31 22:12:35 +00001668 if (len < ntohs(ip->ip_len)) {
1669 printf("len bad %d < %d\n", len, ntohs(ip->ip_len));
1670 return;
1671 }
1672 len = ntohs(ip->ip_len);
Robin Getz0ebf04c2009-07-23 03:01:03 -04001673 debug("len=%d, v=%02x\n", len, ip->ip_hl_v & 0xff);
1674
Alessandro Rubini5cfaa4e2009-08-07 13:58:56 +02001675 /* Can't deal with anything except IPv4 */
Luca Ceresolid3c65b02011-05-04 02:40:43 +00001676 if ((ip->ip_hl_v & 0xf0) != 0x40)
wdenk2d966952002-10-31 22:12:35 +00001677 return;
Alessandro Rubini5cfaa4e2009-08-07 13:58:56 +02001678 /* Can't deal with IP options (headers != 20 bytes) */
Luca Ceresolid3c65b02011-05-04 02:40:43 +00001679 if ((ip->ip_hl_v & 0x0f) > 0x05)
Remy Bohmer6b52cfe2008-06-03 15:48:17 +02001680 return;
Alessandro Rubini5cfaa4e2009-08-07 13:58:56 +02001681 /* Check the Checksum of the header */
wdenk2d966952002-10-31 22:12:35 +00001682 if (!NetCksumOk((uchar *)ip, IP_HDR_SIZE_NO_UDP / 2)) {
Luca Ceresoli4f63acd2011-05-11 03:59:56 +00001683 puts("checksum bad\n");
wdenk2d966952002-10-31 22:12:35 +00001684 return;
1685 }
Alessandro Rubini5cfaa4e2009-08-07 13:58:56 +02001686 /* If it is not for us, ignore it */
wdenk2d966952002-10-31 22:12:35 +00001687 tmp = NetReadIP(&ip->ip_dst);
1688 if (NetOurIP && tmp != NetOurIP && tmp != 0xFFFFFFFF) {
David Updegraff53a5c422007-06-11 10:41:07 -05001689#ifdef CONFIG_MCAST_TFTP
Wolfgang Denk85eb5ca2007-08-14 09:47:27 +02001690 if (Mcast_addr != tmp)
David Updegraff53a5c422007-06-11 10:41:07 -05001691#endif
Luca Ceresolic819abe2011-05-04 02:40:46 +00001692 return;
wdenk2d966952002-10-31 22:12:35 +00001693 }
Luca Ceresoli03eb1292011-04-18 06:19:50 +00001694 /* Read source IP address for later use */
1695 src_ip = NetReadIP(&ip->ip_src);
wdenk2d966952002-10-31 22:12:35 +00001696 /*
Alessandro Rubini5cfaa4e2009-08-07 13:58:56 +02001697 * The function returns the unchanged packet if it's not
1698 * a fragment, and either the complete packet or NULL if
1699 * it is a fragment (if !CONFIG_IP_DEFRAG, it returns NULL)
1700 */
Luca Ceresoliccb9ebe2011-05-04 02:40:47 +00001701 ip = NetDefragment(ip, &len);
1702 if (!ip)
Alessandro Rubini5cfaa4e2009-08-07 13:58:56 +02001703 return;
1704 /*
wdenk2d966952002-10-31 22:12:35 +00001705 * watch for ICMP host redirects
1706 *
wdenk8bde7f72003-06-27 21:31:46 +00001707 * There is no real handler code (yet). We just watch
1708 * for ICMP host redirect messages. In case anybody
1709 * sees these messages: please contact me
1710 * (wd@denx.de), or - even better - send me the
1711 * necessary fixes :-)
wdenk2d966952002-10-31 22:12:35 +00001712 *
wdenk8bde7f72003-06-27 21:31:46 +00001713 * Note: in all cases where I have seen this so far
1714 * it was a problem with the router configuration,
1715 * for instance when a router was configured in the
1716 * BOOTP reply, but the TFTP server was on the same
1717 * subnet. So this is probably a warning that your
1718 * configuration might be wrong. But I'm not really
1719 * sure if there aren't any other situations.
Simon Glass4793ee62011-10-24 18:00:01 +00001720 *
1721 * Simon Glass <sjg@chromium.org>: We get an ICMP when
1722 * we send a tftp packet to a dead connection, or when
1723 * there is no server at the other end.
wdenk2d966952002-10-31 22:12:35 +00001724 */
1725 if (ip->ip_p == IPPROTO_ICMP) {
Simon Glass8f79bb12011-10-24 18:00:00 +00001726 receive_icmp(ip, len, src_ip, et);
1727 return;
wdenk2d966952002-10-31 22:12:35 +00001728 } else if (ip->ip_p != IPPROTO_UDP) { /* Only UDP packets */
1729 return;
1730 }
1731
Stefan Roese8534bf92005-08-12 20:06:52 +02001732#ifdef CONFIG_UDP_CHECKSUM
1733 if (ip->udp_xsum != 0) {
Wolfgang Denkb2f50802005-08-12 23:43:12 +02001734 ulong xsum;
Stefan Roese8534bf92005-08-12 20:06:52 +02001735 ushort *sumptr;
1736 ushort sumlen;
1737
1738 xsum = ip->ip_p;
1739 xsum += (ntohs(ip->udp_len));
1740 xsum += (ntohl(ip->ip_src) >> 16) & 0x0000ffff;
1741 xsum += (ntohl(ip->ip_src) >> 0) & 0x0000ffff;
1742 xsum += (ntohl(ip->ip_dst) >> 16) & 0x0000ffff;
1743 xsum += (ntohl(ip->ip_dst) >> 0) & 0x0000ffff;
1744
1745 sumlen = ntohs(ip->udp_len);
1746 sumptr = (ushort *) &(ip->udp_src);
1747
1748 while (sumlen > 1) {
Wolfgang Denkb2f50802005-08-12 23:43:12 +02001749 ushort sumdata;
Stefan Roese8534bf92005-08-12 20:06:52 +02001750
1751 sumdata = *sumptr++;
1752 xsum += ntohs(sumdata);
1753 sumlen -= 2;
1754 }
1755 if (sumlen > 0) {
Wolfgang Denkb2f50802005-08-12 23:43:12 +02001756 ushort sumdata;
Stefan Roese8534bf92005-08-12 20:06:52 +02001757
1758 sumdata = *(unsigned char *) sumptr;
Wolfgang Denkb2f50802005-08-12 23:43:12 +02001759 sumdata = (sumdata << 8) & 0xff00;
Stefan Roese8534bf92005-08-12 20:06:52 +02001760 xsum += sumdata;
1761 }
1762 while ((xsum >> 16) != 0) {
Luca Ceresoli3e38e422011-05-11 03:59:54 +00001763 xsum = (xsum & 0x0000ffff) +
1764 ((xsum >> 16) & 0x0000ffff);
Stefan Roese8534bf92005-08-12 20:06:52 +02001765 }
1766 if ((xsum != 0x00000000) && (xsum != 0x0000ffff)) {
Wolfgang Denk9b55a252008-07-11 01:16:00 +02001767 printf(" UDP wrong checksum %08lx %08x\n",
1768 xsum, ntohs(ip->udp_xsum));
Stefan Roese8534bf92005-08-12 20:06:52 +02001769 return;
1770 }
1771 }
1772#endif
1773
David Updegraff53a5c422007-06-11 10:41:07 -05001774
wdenk68ceb292004-08-02 21:11:11 +00001775#ifdef CONFIG_NETCONSOLE
Luca Ceresoli4f63acd2011-05-11 03:59:56 +00001776 nc_input_packet((uchar *)ip + IP_HDR_SIZE,
wdenk68ceb292004-08-02 21:11:11 +00001777 ntohs(ip->udp_dst),
1778 ntohs(ip->udp_src),
1779 ntohs(ip->udp_len) - 8);
1780#endif
wdenk2d966952002-10-31 22:12:35 +00001781 /*
1782 * IP header OK. Pass the packet to the current handler.
1783 */
Luca Ceresoli4f63acd2011-05-11 03:59:56 +00001784 (*packetHandler)((uchar *)ip + IP_HDR_SIZE,
wdenk2d966952002-10-31 22:12:35 +00001785 ntohs(ip->udp_dst),
Luca Ceresoli03eb1292011-04-18 06:19:50 +00001786 src_ip,
wdenk2d966952002-10-31 22:12:35 +00001787 ntohs(ip->udp_src),
1788 ntohs(ip->udp_len) - 8);
wdenk2d966952002-10-31 22:12:35 +00001789 break;
1790 }
1791}
1792
1793
1794/**********************************************************************/
1795
Simon Glasse4bf0c52011-10-24 18:00:02 +00001796static int net_check_prereq(enum proto_t protocol)
wdenk2d966952002-10-31 22:12:35 +00001797{
1798 switch (protocol) {
wdenk6e592382004-04-18 17:39:38 +00001799 /* Fall through */
Jon Loeliger643d1ab2007-07-09 17:45:14 -05001800#if defined(CONFIG_CMD_PING)
wdenk73a8b272003-06-05 19:27:42 +00001801 case PING:
wdenk6e592382004-04-18 17:39:38 +00001802 if (NetPingIP == 0) {
Luca Ceresoli4f63acd2011-05-11 03:59:56 +00001803 puts("*** ERROR: ping address not given\n");
Luca Ceresoli92895de2011-05-04 02:40:45 +00001804 return 1;
wdenk6e592382004-04-18 17:39:38 +00001805 }
1806 goto common;
wdenk73a8b272003-06-05 19:27:42 +00001807#endif
Jon Loeliger643d1ab2007-07-09 17:45:14 -05001808#if defined(CONFIG_CMD_SNTP)
wdenkea287de2005-04-01 00:25:43 +00001809 case SNTP:
1810 if (NetNtpServerIP == 0) {
Luca Ceresoli4f63acd2011-05-11 03:59:56 +00001811 puts("*** ERROR: NTP server address not given\n");
Luca Ceresoli92895de2011-05-04 02:40:45 +00001812 return 1;
wdenkea287de2005-04-01 00:25:43 +00001813 }
1814 goto common;
1815#endif
Robin Getz1a32bf42009-07-20 14:53:54 -04001816#if defined(CONFIG_CMD_DNS)
1817 case DNS:
1818 if (NetOurDNSIP == 0) {
1819 puts("*** ERROR: DNS server address not given\n");
1820 return 1;
1821 }
1822 goto common;
1823#endif
Jon Loeliger643d1ab2007-07-09 17:45:14 -05001824#if defined(CONFIG_CMD_NFS)
wdenkcbd8a352004-02-24 02:00:03 +00001825 case NFS:
1826#endif
Simon Glasse4bf0c52011-10-24 18:00:02 +00001827 case TFTPGET:
Simon Glass1fb7cd42011-10-24 18:00:07 +00001828 case TFTPPUT:
wdenk6e592382004-04-18 17:39:38 +00001829 if (NetServerIP == 0) {
Luca Ceresoli4f63acd2011-05-11 03:59:56 +00001830 puts("*** ERROR: `serverip' not set\n");
Luca Ceresoli92895de2011-05-04 02:40:45 +00001831 return 1;
wdenk6e592382004-04-18 17:39:38 +00001832 }
Luca Ceresoli4f63acd2011-05-11 03:59:56 +00001833#if defined(CONFIG_CMD_PING) || defined(CONFIG_CMD_SNTP) || \
1834 defined(CONFIG_CMD_DNS)
1835common:
wdenk73a8b272003-06-05 19:27:42 +00001836#endif
Simon Guinot8b6bbe12011-05-01 23:38:40 +00001837 /* Fall through */
wdenk73a8b272003-06-05 19:27:42 +00001838
Simon Guinot8b6bbe12011-05-01 23:38:40 +00001839 case NETCONS:
Luca Ceresoli7a83af02011-05-17 00:03:40 +00001840 case TFTPSRV:
wdenk6e592382004-04-18 17:39:38 +00001841 if (NetOurIP == 0) {
Luca Ceresoli4f63acd2011-05-11 03:59:56 +00001842 puts("*** ERROR: `ipaddr' not set\n");
Luca Ceresoli92895de2011-05-04 02:40:45 +00001843 return 1;
wdenk6e592382004-04-18 17:39:38 +00001844 }
1845 /* Fall through */
wdenk2d966952002-10-31 22:12:35 +00001846
Peter Tyserbf6cb242010-09-30 11:25:48 -05001847#ifdef CONFIG_CMD_RARP
wdenk2d966952002-10-31 22:12:35 +00001848 case RARP:
Peter Tyserbf6cb242010-09-30 11:25:48 -05001849#endif
wdenk2d966952002-10-31 22:12:35 +00001850 case BOOTP:
wdenka3d991b2004-04-15 21:48:45 +00001851 case CDP:
Peter Tyserbf6cb242010-09-30 11:25:48 -05001852 case DHCP:
Luca Ceresoli4f63acd2011-05-11 03:59:56 +00001853 if (memcmp(NetOurEther, "\0\0\0\0\0\0", 6) == 0) {
Luca Ceresoli4f63acd2011-05-11 03:59:56 +00001854 int num = eth_get_dev_index();
wdenk2d966952002-10-31 22:12:35 +00001855
wdenk6e592382004-04-18 17:39:38 +00001856 switch (num) {
1857 case -1:
Luca Ceresoli4f63acd2011-05-11 03:59:56 +00001858 puts("*** ERROR: No ethernet found.\n");
Luca Ceresoli92895de2011-05-04 02:40:45 +00001859 return 1;
wdenk6e592382004-04-18 17:39:38 +00001860 case 0:
Luca Ceresoli4f63acd2011-05-11 03:59:56 +00001861 puts("*** ERROR: `ethaddr' not set\n");
wdenk2d966952002-10-31 22:12:35 +00001862 break;
wdenk6e592382004-04-18 17:39:38 +00001863 default:
Luca Ceresoli4f63acd2011-05-11 03:59:56 +00001864 printf("*** ERROR: `eth%daddr' not set\n",
wdenk2d966952002-10-31 22:12:35 +00001865 num);
1866 break;
wdenk2d966952002-10-31 22:12:35 +00001867 }
wdenk6e592382004-04-18 17:39:38 +00001868
Luca Ceresoli4f63acd2011-05-11 03:59:56 +00001869 NetStartAgain();
Luca Ceresoli92895de2011-05-04 02:40:45 +00001870 return 2;
wdenk6e592382004-04-18 17:39:38 +00001871 }
1872 /* Fall through */
1873 default:
Luca Ceresoli92895de2011-05-04 02:40:45 +00001874 return 0;
wdenk2d966952002-10-31 22:12:35 +00001875 }
Luca Ceresoli92895de2011-05-04 02:40:45 +00001876 return 0; /* OK */
wdenk2d966952002-10-31 22:12:35 +00001877}
1878/**********************************************************************/
1879
1880int
Luca Ceresoli6b147d12011-05-04 02:40:44 +00001881NetCksumOk(uchar *ptr, int len)
wdenk2d966952002-10-31 22:12:35 +00001882{
1883 return !((NetCksum(ptr, len) + 1) & 0xfffe);
1884}
1885
1886
1887unsigned
Luca Ceresoli6b147d12011-05-04 02:40:44 +00001888NetCksum(uchar *ptr, int len)
wdenk2d966952002-10-31 22:12:35 +00001889{
1890 ulong xsum;
Stefan Roese9d2a8732005-08-31 12:55:50 +02001891 ushort *p = (ushort *)ptr;
wdenk2d966952002-10-31 22:12:35 +00001892
1893 xsum = 0;
1894 while (len-- > 0)
Wolfgang Denk7bc5ee02005-08-26 01:36:03 +02001895 xsum += *p++;
wdenk2d966952002-10-31 22:12:35 +00001896 xsum = (xsum & 0xffff) + (xsum >> 16);
1897 xsum = (xsum & 0xffff) + (xsum >> 16);
Luca Ceresoli92895de2011-05-04 02:40:45 +00001898 return xsum & 0xffff;
wdenk2d966952002-10-31 22:12:35 +00001899}
1900
wdenka3d991b2004-04-15 21:48:45 +00001901int
1902NetEthHdrSize(void)
1903{
1904 ushort myvlanid;
wdenk2d966952002-10-31 22:12:35 +00001905
wdenka3d991b2004-04-15 21:48:45 +00001906 myvlanid = ntohs(NetOurVLAN);
1907 if (myvlanid == (ushort)-1)
1908 myvlanid = VLAN_NONE;
1909
Luca Ceresoli3e38e422011-05-11 03:59:54 +00001910 return ((myvlanid & VLAN_IDMASK) == VLAN_NONE) ? ETHER_HDR_SIZE :
1911 VLAN_ETHER_HDR_SIZE;
wdenka3d991b2004-04-15 21:48:45 +00001912}
1913
1914int
Joe Hershbergerdb288a92012-05-15 08:59:04 +00001915NetSetEther(uchar *xet, uchar * addr, uint prot)
wdenk2d966952002-10-31 22:12:35 +00001916{
1917 Ethernet_t *et = (Ethernet_t *)xet;
wdenka3d991b2004-04-15 21:48:45 +00001918 ushort myvlanid;
1919
1920 myvlanid = ntohs(NetOurVLAN);
1921 if (myvlanid == (ushort)-1)
1922 myvlanid = VLAN_NONE;
wdenk2d966952002-10-31 22:12:35 +00001923
Luca Ceresoli4f63acd2011-05-11 03:59:56 +00001924 memcpy(et->et_dest, addr, 6);
1925 memcpy(et->et_src, NetOurEther, 6);
wdenka3d991b2004-04-15 21:48:45 +00001926 if ((myvlanid & VLAN_IDMASK) == VLAN_NONE) {
Luca Ceresolic819abe2011-05-04 02:40:46 +00001927 et->et_protlen = htons(prot);
wdenka3d991b2004-04-15 21:48:45 +00001928 return ETHER_HDR_SIZE;
1929 } else {
1930 VLAN_Ethernet_t *vet = (VLAN_Ethernet_t *)xet;
wdenk2d966952002-10-31 22:12:35 +00001931
wdenka3d991b2004-04-15 21:48:45 +00001932 vet->vet_vlan_type = htons(PROT_VLAN);
1933 vet->vet_tag = htons((0 << 5) | (myvlanid & VLAN_IDMASK));
1934 vet->vet_type = htons(prot);
1935 return VLAN_ETHER_HDR_SIZE;
1936 }
1937}
wdenk2d966952002-10-31 22:12:35 +00001938
1939void
Joe Hershbergerdb288a92012-05-15 08:59:04 +00001940NetSetIP(uchar *xip, IPaddr_t dest, int dport, int sport, int len)
wdenk2d966952002-10-31 22:12:35 +00001941{
Olav Morkenaf8626e2009-01-23 12:56:26 +01001942 IP_t *ip = (IP_t *)xip;
wdenk2d966952002-10-31 22:12:35 +00001943
1944 /*
1945 * If the data is an odd number of bytes, zero the
1946 * byte after the last byte so that the checksum
1947 * will work.
1948 */
1949 if (len & 1)
1950 xip[IP_HDR_SIZE + len] = 0;
1951
1952 /*
1953 * Construct an IP and UDP header.
wdenk6e592382004-04-18 17:39:38 +00001954 * (need to set no fragment bit - XXX)
wdenk2d966952002-10-31 22:12:35 +00001955 */
Luca Ceresoli3e38e422011-05-11 03:59:54 +00001956 /* IP_HDR_SIZE / 4 (not including UDP) */
1957 ip->ip_hl_v = 0x45;
wdenk2d966952002-10-31 22:12:35 +00001958 ip->ip_tos = 0;
1959 ip->ip_len = htons(IP_HDR_SIZE + len);
1960 ip->ip_id = htons(NetIPID++);
Peter Tysere0c07b82008-12-01 16:26:20 -06001961 ip->ip_off = htons(IP_FLAGS_DFRAG); /* Don't fragment */
wdenk2d966952002-10-31 22:12:35 +00001962 ip->ip_ttl = 255;
1963 ip->ip_p = 17; /* UDP */
1964 ip->ip_sum = 0;
Luca Ceresoli3e38e422011-05-11 03:59:54 +00001965 /* already in network byte order */
Luca Ceresoli6b147d12011-05-04 02:40:44 +00001966 NetCopyIP((void *)&ip->ip_src, &NetOurIP);
Luca Ceresoli3e38e422011-05-11 03:59:54 +00001967 /* - "" - */
Luca Ceresoli6b147d12011-05-04 02:40:44 +00001968 NetCopyIP((void *)&ip->ip_dst, &dest);
wdenk2d966952002-10-31 22:12:35 +00001969 ip->udp_src = htons(sport);
1970 ip->udp_dst = htons(dport);
1971 ip->udp_len = htons(8 + len);
1972 ip->udp_xsum = 0;
1973 ip->ip_sum = ~NetCksum((uchar *)ip, IP_HDR_SIZE_NO_UDP / 2);
1974}
1975
Luca Ceresoli4f63acd2011-05-11 03:59:56 +00001976void copy_filename(char *dst, const char *src, int size)
wdenk2d966952002-10-31 22:12:35 +00001977{
1978 if (*src && (*src == '"')) {
1979 ++src;
1980 --size;
1981 }
1982
Luca Ceresolid3c65b02011-05-04 02:40:43 +00001983 while ((--size > 0) && *src && (*src != '"'))
wdenk2d966952002-10-31 22:12:35 +00001984 *dst++ = *src++;
wdenk2d966952002-10-31 22:12:35 +00001985 *dst = '\0';
1986}
1987
Luca Ceresoli3e38e422011-05-11 03:59:54 +00001988#if defined(CONFIG_CMD_NFS) || \
1989 defined(CONFIG_CMD_SNTP) || \
1990 defined(CONFIG_CMD_DNS)
Robin Getz1a32bf42009-07-20 14:53:54 -04001991/*
Robin Getz97399462010-03-08 14:07:00 -05001992 * make port a little random (1024-17407)
1993 * This keeps the math somewhat trivial to compute, and seems to work with
1994 * all supported protocols/clients/servers
Robin Getz1a32bf42009-07-20 14:53:54 -04001995 */
1996unsigned int random_port(void)
1997{
Robin Getz97399462010-03-08 14:07:00 -05001998 return 1024 + (get_timer(0) % 0x4000);
Robin Getz1a32bf42009-07-20 14:53:54 -04001999}
2000#endif
2001
Luca Ceresoli4f63acd2011-05-11 03:59:56 +00002002void ip_to_string(IPaddr_t x, char *s)
wdenk2d966952002-10-31 22:12:35 +00002003{
Luca Ceresoli4f63acd2011-05-11 03:59:56 +00002004 x = ntohl(x);
2005 sprintf(s, "%d.%d.%d.%d",
2006 (int) ((x >> 24) & 0xff),
2007 (int) ((x >> 16) & 0xff),
2008 (int) ((x >> 8) & 0xff), (int) ((x >> 0) & 0xff)
wdenk6e592382004-04-18 17:39:38 +00002009 );
wdenk2d966952002-10-31 22:12:35 +00002010}
2011
wdenka3d991b2004-04-15 21:48:45 +00002012void VLAN_to_string(ushort x, char *s)
2013{
2014 x = ntohs(x);
2015
2016 if (x == (ushort)-1)
2017 x = VLAN_NONE;
2018
2019 if (x == VLAN_NONE)
2020 strcpy(s, "none");
2021 else
2022 sprintf(s, "%d", x & VLAN_IDMASK);
2023}
2024
Mike Frysinger2e3ef6e2010-10-20 07:16:48 -04002025ushort string_to_VLAN(const char *s)
wdenka3d991b2004-04-15 21:48:45 +00002026{
2027 ushort id;
2028
2029 if (s == NULL)
wdenkb9711de2004-04-25 13:18:40 +00002030 return htons(VLAN_NONE);
wdenka3d991b2004-04-15 21:48:45 +00002031
2032 if (*s < '0' || *s > '9')
2033 id = VLAN_NONE;
2034 else
2035 id = (ushort)simple_strtoul(s, NULL, 10);
2036
wdenkb9711de2004-04-25 13:18:40 +00002037 return htons(id);
wdenka3d991b2004-04-15 21:48:45 +00002038}
2039
wdenka3d991b2004-04-15 21:48:45 +00002040ushort getenv_VLAN(char *var)
2041{
Luca Ceresoli92895de2011-05-04 02:40:45 +00002042 return string_to_VLAN(getenv(var));
wdenka3d991b2004-04-15 21:48:45 +00002043}