blob: da3f4cddb16c2418e9b74e0d992c586ac7b473a3 [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>
80#include <net.h>
81#include "bootp.h"
82#include "tftp.h"
83#include "rarp.h"
wdenkcbd8a352004-02-24 02:00:03 +000084#include "nfs.h"
wdenkfc3e2162003-10-08 22:33:00 +000085#ifdef CONFIG_STATUS_LED
86#include <status_led.h>
87#include <miiphy.h>
88#endif
Jon Loeliger643d1ab2007-07-09 17:45:14 -050089#if defined(CONFIG_CMD_SNTP)
wdenkea287de2005-04-01 00:25:43 +000090#include "sntp.h"
91#endif
Peter Tyser561858e2008-11-03 09:30:59 -060092#if defined(CONFIG_CDP_VERSION)
93#include <timestamp.h>
94#endif
wdenk2d966952002-10-31 22:12:35 +000095
Jon Loeliger643d1ab2007-07-09 17:45:14 -050096#if defined(CONFIG_CMD_NET)
wdenk2d966952002-10-31 22:12:35 +000097
Wolfgang Denkd87080b2006-03-31 18:32:53 +020098DECLARE_GLOBAL_DATA_PTR;
99
Guennadi Liakhovetski40cb90e2008-04-03 17:04:19 +0200100#ifndef CONFIG_ARP_TIMEOUT
Bartlomiej Sieka49f3bdb2008-10-01 15:26:28 +0200101# define ARP_TIMEOUT 5000UL /* Milliseconds before trying ARP again */
wdenk73a8b272003-06-05 19:27:42 +0000102#else
Bartlomiej Sieka49f3bdb2008-10-01 15:26:28 +0200103# define ARP_TIMEOUT CONFIG_ARP_TIMEOUT
Guennadi Liakhovetski40cb90e2008-04-03 17:04:19 +0200104#endif
105
106
107#ifndef CONFIG_NET_RETRY_COUNT
108# define ARP_TIMEOUT_COUNT 5 /* # of timeouts before giving up */
109#else
110# define ARP_TIMEOUT_COUNT CONFIG_NET_RETRY_COUNT
wdenk73a8b272003-06-05 19:27:42 +0000111#endif
112
wdenk2d966952002-10-31 22:12:35 +0000113#if 0
114#define ET_DEBUG
115#endif
116
117/** BOOTP EXTENTIONS **/
118
119IPaddr_t NetOurSubnetMask=0; /* Our subnet mask (0=unknown) */
120IPaddr_t NetOurGatewayIP=0; /* Our gateways IP address */
121IPaddr_t NetOurDNSIP=0; /* Our DNS IP address */
Jon Loeliger1fe80d72007-07-09 22:08:34 -0500122#if defined(CONFIG_BOOTP_DNS2)
stroesefe389a82003-08-28 14:17:32 +0000123IPaddr_t NetOurDNS2IP=0; /* Our 2nd DNS IP address */
124#endif
wdenk2d966952002-10-31 22:12:35 +0000125char NetOurNISDomain[32]={0,}; /* Our NIS domain */
126char NetOurHostName[32]={0,}; /* Our hostname */
127char NetOurRootPath[64]={0,}; /* Our bootpath */
128ushort NetBootFileSize=0; /* Our bootfile size in blocks */
129
David Updegraff53a5c422007-06-11 10:41:07 -0500130#ifdef CONFIG_MCAST_TFTP /* Multicast TFTP */
131IPaddr_t Mcast_addr;
132#endif
133
wdenk2d966952002-10-31 22:12:35 +0000134/** END OF BOOTP EXTENTIONS **/
135
136ulong NetBootFileXferSize; /* The actual transferred size of the bootfile (in bytes) */
137uchar NetOurEther[6]; /* Our ethernet address */
138uchar NetServerEther[6] = /* Boot server enet address */
wdenk73a8b272003-06-05 19:27:42 +0000139 { 0, 0, 0, 0, 0, 0 };
wdenk2d966952002-10-31 22:12:35 +0000140IPaddr_t NetOurIP; /* Our IP addr (0 = unknown) */
Guennadi Liakhovetski40cb90e2008-04-03 17:04:19 +0200141IPaddr_t NetServerIP; /* Server IP addr (0 = unknown) */
wdenk2d966952002-10-31 22:12:35 +0000142volatile uchar *NetRxPkt; /* Current receive packet */
143int NetRxPktLen; /* Current rx packet length */
144unsigned NetIPID; /* IP packet ID */
145uchar NetBcastAddr[6] = /* Ethernet bcast address */
146 { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
wdenk73a8b272003-06-05 19:27:42 +0000147uchar NetEtherNullAddr[6] =
148 { 0, 0, 0, 0, 0, 0 };
Rafal Jaworowskif85b6072007-12-27 18:19:02 +0100149#ifdef CONFIG_API
150void (*push_packet)(volatile void *, int len) = 0;
151#endif
Jon Loeliger643d1ab2007-07-09 17:45:14 -0500152#if defined(CONFIG_CMD_CDP)
wdenk6e592382004-04-18 17:39:38 +0000153uchar NetCDPAddr[6] = /* Ethernet bcast address */
wdenka3d991b2004-04-15 21:48:45 +0000154 { 0x01, 0x00, 0x0c, 0xcc, 0xcc, 0xcc };
155#endif
wdenk2d966952002-10-31 22:12:35 +0000156int NetState; /* Network loop state */
157#ifdef CONFIG_NET_MULTI
158int NetRestartWrap = 0; /* Tried all network devices */
159static int NetRestarted = 0; /* Network loop restarted */
160static int NetDevExists = 0; /* At least one device configured */
161#endif
162
wdenk6e592382004-04-18 17:39:38 +0000163/* XXX in both little & big endian machines 0xFFFF == ntohs(-1) */
164ushort NetOurVLAN = 0xFFFF; /* default is without VLAN */
165ushort NetOurNativeVLAN = 0xFFFF; /* ditto */
wdenka3d991b2004-04-15 21:48:45 +0000166
wdenk2d966952002-10-31 22:12:35 +0000167char BootFile[128]; /* Boot File name */
168
Jon Loeliger643d1ab2007-07-09 17:45:14 -0500169#if defined(CONFIG_CMD_PING)
Wolfgang Denk53677ef2008-05-20 16:00:29 +0200170IPaddr_t NetPingIP; /* the ip address to ping */
wdenk73a8b272003-06-05 19:27:42 +0000171
172static void PingStart(void);
173#endif
174
Jon Loeliger643d1ab2007-07-09 17:45:14 -0500175#if defined(CONFIG_CMD_CDP)
wdenka3d991b2004-04-15 21:48:45 +0000176static void CDPStart(void);
177#endif
178
Jon Loeliger643d1ab2007-07-09 17:45:14 -0500179#if defined(CONFIG_CMD_SNTP)
wdenkea287de2005-04-01 00:25:43 +0000180IPaddr_t NetNtpServerIP; /* NTP server IP address */
181int NetTimeOffset=0; /* offset time from UTC */
182#endif
183
wdenk68ceb292004-08-02 21:11:11 +0000184#ifdef CONFIG_NETCONSOLE
185void NcStart(void);
186int nc_input_packet(uchar *pkt, unsigned dest, unsigned src, unsigned len);
187#endif
188
wdenk2d966952002-10-31 22:12:35 +0000189volatile uchar PktBuf[(PKTBUFSRX+1) * PKTSIZE_ALIGN + PKTALIGN];
190
191volatile uchar *NetRxPackets[PKTBUFSRX]; /* Receive packets */
192
193static rxhand_f *packetHandler; /* Current RX packet handler */
194static thand_f *timeHandler; /* Current timeout handler */
wdenke0ac62d2003-08-17 18:55:18 +0000195static ulong timeStart; /* Time base value */
196static ulong timeDelta; /* Current timeout value */
wdenk2d966952002-10-31 22:12:35 +0000197volatile uchar *NetTxPacket = 0; /* THE transmit packet */
198
199static int net_check_prereq (proto_t protocol);
200
201/**********************************************************************/
wdenk73a8b272003-06-05 19:27:42 +0000202
203IPaddr_t NetArpWaitPacketIP;
204IPaddr_t NetArpWaitReplyIP;
205uchar *NetArpWaitPacketMAC; /* MAC address of waiting packet's destination */
Wolfgang Denkb2f50802005-08-12 23:43:12 +0200206uchar *NetArpWaitTxPacket; /* THE transmit packet */
wdenk73a8b272003-06-05 19:27:42 +0000207int NetArpWaitTxPacketSize;
Wolfgang Denk53677ef2008-05-20 16:00:29 +0200208uchar NetArpWaitPacketBuf[PKTSIZE_ALIGN + PKTALIGN];
wdenk73a8b272003-06-05 19:27:42 +0000209ulong NetArpWaitTimerStart;
210int NetArpWaitTry;
211
Heiko Schocher2f70c492009-02-10 09:38:52 +0100212int env_changed_id = 0;
213
wdenk6e592382004-04-18 17:39:38 +0000214void ArpRequest (void)
wdenk73a8b272003-06-05 19:27:42 +0000215{
216 int i;
217 volatile uchar *pkt;
wdenk6e592382004-04-18 17:39:38 +0000218 ARP_t *arp;
wdenk73a8b272003-06-05 19:27:42 +0000219
220#ifdef ET_DEBUG
wdenk6e592382004-04-18 17:39:38 +0000221 printf ("ARP broadcast %d\n", NetArpWaitTry);
wdenk73a8b272003-06-05 19:27:42 +0000222#endif
223 pkt = NetTxPacket;
224
wdenk6e592382004-04-18 17:39:38 +0000225 pkt += NetSetEther (pkt, NetBcastAddr, PROT_ARP);
wdenk73a8b272003-06-05 19:27:42 +0000226
wdenk6e592382004-04-18 17:39:38 +0000227 arp = (ARP_t *) pkt;
wdenk73a8b272003-06-05 19:27:42 +0000228
wdenk6e592382004-04-18 17:39:38 +0000229 arp->ar_hrd = htons (ARP_ETHER);
230 arp->ar_pro = htons (PROT_IP);
wdenk73a8b272003-06-05 19:27:42 +0000231 arp->ar_hln = 6;
232 arp->ar_pln = 4;
wdenk6e592382004-04-18 17:39:38 +0000233 arp->ar_op = htons (ARPOP_REQUEST);
wdenk73a8b272003-06-05 19:27:42 +0000234
Wolfgang Denkb2f50802005-08-12 23:43:12 +0200235 memcpy (&arp->ar_data[0], NetOurEther, 6); /* source ET addr */
236 NetWriteIP ((uchar *) & arp->ar_data[6], NetOurIP); /* source IP addr */
wdenk6e592382004-04-18 17:39:38 +0000237 for (i = 10; i < 16; ++i) {
238 arp->ar_data[i] = 0; /* dest ET addr = 0 */
wdenk73a8b272003-06-05 19:27:42 +0000239 }
240
wdenk6e592382004-04-18 17:39:38 +0000241 if ((NetArpWaitPacketIP & NetOurSubnetMask) !=
242 (NetOurIP & NetOurSubnetMask)) {
243 if (NetOurGatewayIP == 0) {
244 puts ("## Warning: gatewayip needed but not set\n");
Wolfgang Denkd509b812006-03-12 01:13:30 +0100245 NetArpWaitReplyIP = NetArpWaitPacketIP;
246 } else {
247 NetArpWaitReplyIP = NetOurGatewayIP;
wdenk6e592382004-04-18 17:39:38 +0000248 }
wdenk6e592382004-04-18 17:39:38 +0000249 } else {
250 NetArpWaitReplyIP = NetArpWaitPacketIP;
251 }
wdenk73a8b272003-06-05 19:27:42 +0000252
wdenk6e592382004-04-18 17:39:38 +0000253 NetWriteIP ((uchar *) & arp->ar_data[16], NetArpWaitReplyIP);
254 (void) eth_send (NetTxPacket, (pkt - NetTxPacket) + ARP_HDR_SIZE);
wdenk73a8b272003-06-05 19:27:42 +0000255}
256
257void ArpTimeoutCheck(void)
258{
259 ulong t;
260
261 if (!NetArpWaitPacketIP)
262 return;
263
264 t = get_timer(0);
265
266 /* check for arp timeout */
Bartlomiej Sieka49f3bdb2008-10-01 15:26:28 +0200267 if ((t - NetArpWaitTimerStart) > ARP_TIMEOUT) {
wdenk73a8b272003-06-05 19:27:42 +0000268 NetArpWaitTry++;
269
270 if (NetArpWaitTry >= ARP_TIMEOUT_COUNT) {
271 puts ("\nARP Retry count exceeded; starting again\n");
272 NetArpWaitTry = 0;
273 NetStartAgain();
274 } else {
275 NetArpWaitTimerStart = t;
276 ArpRequest();
277 }
278 }
279}
280
Heiko Schocher2f70c492009-02-10 09:38:52 +0100281int
282NetInitLoop(proto_t protocol)
283{
284 bd_t *bd = gd->bd;
285 int env_id = get_env_id ();
286
287 /* update only when the environment has changed */
288 if (env_changed_id == env_id)
289 return 0;
290
291 switch (protocol) {
292#if defined(CONFIG_CMD_NFS)
293 case NFS:
294#endif
295#if defined(CONFIG_CMD_PING)
296 case PING:
297#endif
298#if defined(CONFIG_CMD_SNTP)
299 case SNTP:
300#endif
301 case NETCONS:
302 case TFTP:
303 NetCopyIP(&NetOurIP, &bd->bi_ip_addr);
304 NetOurGatewayIP = getenv_IPaddr ("gatewayip");
305 NetOurSubnetMask= getenv_IPaddr ("netmask");
306 NetOurVLAN = getenv_VLAN("vlan");
307 NetOurNativeVLAN = getenv_VLAN("nvlan");
308
309 switch (protocol) {
310#if defined(CONFIG_CMD_NFS)
311 case NFS:
312#endif
313 case NETCONS:
314 case TFTP:
315 NetServerIP = getenv_IPaddr ("serverip");
316 break;
317#if defined(CONFIG_CMD_PING)
318 case PING:
319 /* nothing */
320 break;
321#endif
322#if defined(CONFIG_CMD_SNTP)
323 case SNTP:
324 /* nothing */
325 break;
326#endif
327 default:
328 break;
329 }
330
331 break;
332 case BOOTP:
333 case RARP:
334 /*
335 * initialize our IP addr to 0 in order to accept ANY
336 * IP addr assigned to us by the BOOTP / RARP server
337 */
338 NetOurIP = 0;
339 NetServerIP = getenv_IPaddr ("serverip");
340 NetOurVLAN = getenv_VLAN("vlan"); /* VLANs must be read */
341 NetOurNativeVLAN = getenv_VLAN("nvlan");
342 case CDP:
343 NetOurVLAN = getenv_VLAN("vlan"); /* VLANs must be read */
344 NetOurNativeVLAN = getenv_VLAN("nvlan");
345 break;
346 default:
347 break;
348 }
349 env_changed_id = env_id;
350 return 0;
351}
352
wdenk73a8b272003-06-05 19:27:42 +0000353/**********************************************************************/
wdenk2d966952002-10-31 22:12:35 +0000354/*
355 * Main network processing loop.
356 */
357
358int
359NetLoop(proto_t protocol)
360{
wdenk2d966952002-10-31 22:12:35 +0000361 bd_t *bd = gd->bd;
362
363#ifdef CONFIG_NET_MULTI
364 NetRestarted = 0;
365 NetDevExists = 0;
366#endif
367
wdenk73a8b272003-06-05 19:27:42 +0000368 /* XXX problem with bss workaround */
369 NetArpWaitPacketMAC = NULL;
370 NetArpWaitTxPacket = NULL;
371 NetArpWaitPacketIP = 0;
372 NetArpWaitReplyIP = 0;
373 NetArpWaitTxPacket = NULL;
374 NetTxPacket = NULL;
375
wdenk2d966952002-10-31 22:12:35 +0000376 if (!NetTxPacket) {
377 int i;
wdenk2d966952002-10-31 22:12:35 +0000378 /*
379 * Setup packet buffers, aligned correctly.
380 */
381 NetTxPacket = &PktBuf[0] + (PKTALIGN - 1);
382 NetTxPacket -= (ulong)NetTxPacket % PKTALIGN;
383 for (i = 0; i < PKTBUFSRX; i++) {
384 NetRxPackets[i] = NetTxPacket + (i+1)*PKTSIZE_ALIGN;
385 }
wdenk73a8b272003-06-05 19:27:42 +0000386 }
387
388 if (!NetArpWaitTxPacket) {
389 NetArpWaitTxPacket = &NetArpWaitPacketBuf[0] + (PKTALIGN - 1);
390 NetArpWaitTxPacket -= (ulong)NetArpWaitTxPacket % PKTALIGN;
391 NetArpWaitTxPacketSize = 0;
wdenk2d966952002-10-31 22:12:35 +0000392 }
393
394 eth_halt();
wdenk6e592382004-04-18 17:39:38 +0000395#ifdef CONFIG_NET_MULTI
wdenka3d991b2004-04-15 21:48:45 +0000396 eth_set_current();
wdenk6e592382004-04-18 17:39:38 +0000397#endif
wdenkb1bf6f22005-04-03 14:52:59 +0000398 if (eth_init(bd) < 0) {
399 eth_halt();
wdenk6e592382004-04-18 17:39:38 +0000400 return(-1);
wdenkb1bf6f22005-04-03 14:52:59 +0000401 }
wdenk2d966952002-10-31 22:12:35 +0000402
403restart:
404#ifdef CONFIG_NET_MULTI
405 memcpy (NetOurEther, eth_get_dev()->enetaddr, 6);
406#else
407 memcpy (NetOurEther, bd->bi_enetaddr, 6);
408#endif
409
410 NetState = NETLOOP_CONTINUE;
411
412 /*
413 * Start the ball rolling with the given start function. From
414 * here on, this code is a state machine driven by received
415 * packets and timer events.
416 */
Heiko Schocher2f70c492009-02-10 09:38:52 +0100417 NetInitLoop(protocol);
wdenk2d966952002-10-31 22:12:35 +0000418
419 switch (net_check_prereq (protocol)) {
420 case 1:
421 /* network not configured */
wdenkb1bf6f22005-04-03 14:52:59 +0000422 eth_halt();
wdenk1d0350e2002-11-11 21:14:20 +0000423 return (-1);
wdenk2d966952002-10-31 22:12:35 +0000424
425#ifdef CONFIG_NET_MULTI
426 case 2:
427 /* network device not configured */
428 break;
429#endif /* CONFIG_NET_MULTI */
430
431 case 0:
432#ifdef CONFIG_NET_MULTI
433 NetDevExists = 1;
434#endif
435 switch (protocol) {
436 case TFTP:
437 /* always use ARP to get server ethernet address */
wdenk73a8b272003-06-05 19:27:42 +0000438 TftpStart();
wdenk2d966952002-10-31 22:12:35 +0000439 break;
440
Jon Loeliger643d1ab2007-07-09 17:45:14 -0500441#if defined(CONFIG_CMD_DHCP)
wdenk2d966952002-10-31 22:12:35 +0000442 case DHCP:
443 /* Start with a clean slate... */
wdenkd407bf52004-10-11 22:51:13 +0000444 BootpTry = 0;
wdenk2d966952002-10-31 22:12:35 +0000445 NetOurIP = 0;
wdenk3d3befa2004-03-14 15:06:13 +0000446 NetServerIP = getenv_IPaddr ("serverip");
wdenk2d966952002-10-31 22:12:35 +0000447 DhcpRequest(); /* Basically same as BOOTP */
448 break;
Jon Loeliger610f2e92007-07-10 11:05:02 -0500449#endif
wdenk2d966952002-10-31 22:12:35 +0000450
451 case BOOTP:
452 BootpTry = 0;
453 BootpRequest ();
454 break;
455
456 case RARP:
457 RarpTry = 0;
458 RarpRequest ();
459 break;
Jon Loeliger643d1ab2007-07-09 17:45:14 -0500460#if defined(CONFIG_CMD_PING)
wdenk73a8b272003-06-05 19:27:42 +0000461 case PING:
462 PingStart();
463 break;
464#endif
Jon Loeliger643d1ab2007-07-09 17:45:14 -0500465#if defined(CONFIG_CMD_NFS)
wdenkcbd8a352004-02-24 02:00:03 +0000466 case NFS:
467 NfsStart();
468 break;
469#endif
Jon Loeliger643d1ab2007-07-09 17:45:14 -0500470#if defined(CONFIG_CMD_CDP)
wdenka3d991b2004-04-15 21:48:45 +0000471 case CDP:
472 CDPStart();
473 break;
474#endif
wdenk68ceb292004-08-02 21:11:11 +0000475#ifdef CONFIG_NETCONSOLE
476 case NETCONS:
477 NcStart();
478 break;
479#endif
Jon Loeliger643d1ab2007-07-09 17:45:14 -0500480#if defined(CONFIG_CMD_SNTP)
wdenkea287de2005-04-01 00:25:43 +0000481 case SNTP:
482 SntpStart();
483 break;
484#endif
wdenk2d966952002-10-31 22:12:35 +0000485 default:
486 break;
487 }
488
489 NetBootFileXferSize = 0;
490 break;
491 }
492
Jon Loeliger643d1ab2007-07-09 17:45:14 -0500493#if defined(CONFIG_MII) || defined(CONFIG_CMD_MII)
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200494#if defined(CONFIG_SYS_FAULT_ECHO_LINK_DOWN) && defined(CONFIG_STATUS_LED) && defined(STATUS_LED_RED)
wdenkfc3e2162003-10-08 22:33:00 +0000495 /*
wdenk42d1f032003-10-15 23:53:47 +0000496 * Echo the inverted link state to the fault LED.
wdenkfc3e2162003-10-08 22:33:00 +0000497 */
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200498 if(miiphy_link(eth_get_dev()->name, CONFIG_SYS_FAULT_MII_ADDR)) {
wdenkfc3e2162003-10-08 22:33:00 +0000499 status_led_set (STATUS_LED_RED, STATUS_LED_OFF);
500 } else {
501 status_led_set (STATUS_LED_RED, STATUS_LED_ON);
502 }
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200503#endif /* CONFIG_SYS_FAULT_ECHO_LINK_DOWN, ... */
wdenkfc3e2162003-10-08 22:33:00 +0000504#endif /* CONFIG_MII, ... */
wdenk2d966952002-10-31 22:12:35 +0000505
506 /*
507 * Main packet reception loop. Loop receiving packets until
wdenk59acc292005-04-03 14:18:51 +0000508 * someone sets `NetState' to a state that terminates.
wdenk2d966952002-10-31 22:12:35 +0000509 */
510 for (;;) {
511 WATCHDOG_RESET();
512#ifdef CONFIG_SHOW_ACTIVITY
513 {
514 extern void show_activity(int arg);
515 show_activity(1);
516 }
517#endif
518 /*
519 * Check the ethernet for a new packet. The ethernet
520 * receive routine will process it.
521 */
Guennadi Liakhovetski40cb90e2008-04-03 17:04:19 +0200522 eth_rx();
wdenk2d966952002-10-31 22:12:35 +0000523
524 /*
525 * Abort if ctrl-c was pressed.
526 */
527 if (ctrlc()) {
wdenk8bde7f72003-06-27 21:31:46 +0000528 eth_halt();
wdenk4b9206e2004-03-23 22:14:11 +0000529 puts ("\nAbort\n");
wdenk1d0350e2002-11-11 21:14:20 +0000530 return (-1);
wdenk2d966952002-10-31 22:12:35 +0000531 }
532
wdenk73a8b272003-06-05 19:27:42 +0000533 ArpTimeoutCheck();
wdenk2d966952002-10-31 22:12:35 +0000534
535 /*
536 * Check for a timeout, and run the timeout handler
537 * if we have one.
538 */
wdenke0ac62d2003-08-17 18:55:18 +0000539 if (timeHandler && ((get_timer(0) - timeStart) > timeDelta)) {
wdenk2d966952002-10-31 22:12:35 +0000540 thand_f *x;
541
Jon Loeliger643d1ab2007-07-09 17:45:14 -0500542#if defined(CONFIG_MII) || defined(CONFIG_CMD_MII)
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200543# if defined(CONFIG_SYS_FAULT_ECHO_LINK_DOWN) && \
wdenk63153492005-04-03 20:55:38 +0000544 defined(CONFIG_STATUS_LED) && \
wdenk59acc292005-04-03 14:18:51 +0000545 defined(STATUS_LED_RED)
wdenkfc3e2162003-10-08 22:33:00 +0000546 /*
wdenk42d1f032003-10-15 23:53:47 +0000547 * Echo the inverted link state to the fault LED.
wdenkfc3e2162003-10-08 22:33:00 +0000548 */
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200549 if(miiphy_link(eth_get_dev()->name, CONFIG_SYS_FAULT_MII_ADDR)) {
wdenkfc3e2162003-10-08 22:33:00 +0000550 status_led_set (STATUS_LED_RED, STATUS_LED_OFF);
551 } else {
552 status_led_set (STATUS_LED_RED, STATUS_LED_ON);
553 }
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200554# endif /* CONFIG_SYS_FAULT_ECHO_LINK_DOWN, ... */
wdenkfc3e2162003-10-08 22:33:00 +0000555#endif /* CONFIG_MII, ... */
wdenk2d966952002-10-31 22:12:35 +0000556 x = timeHandler;
557 timeHandler = (thand_f *)0;
558 (*x)();
559 }
560
561
562 switch (NetState) {
563
564 case NETLOOP_RESTART:
565#ifdef CONFIG_NET_MULTI
566 NetRestarted = 1;
567#endif
568 goto restart;
569
570 case NETLOOP_SUCCESS:
571 if (NetBootFileXferSize > 0) {
Wolfgang Denkf34024d2007-09-12 00:48:57 +0200572 char buf[20];
wdenk2d966952002-10-31 22:12:35 +0000573 printf("Bytes transferred = %ld (%lx hex)\n",
574 NetBootFileXferSize,
575 NetBootFileXferSize);
Wolfgang Denkf34024d2007-09-12 00:48:57 +0200576 sprintf(buf, "%lX", NetBootFileXferSize);
wdenk2d966952002-10-31 22:12:35 +0000577 setenv("filesize", buf);
wdenka3d991b2004-04-15 21:48:45 +0000578
579 sprintf(buf, "%lX", (unsigned long)load_addr);
580 setenv("fileaddr", buf);
wdenk2d966952002-10-31 22:12:35 +0000581 }
582 eth_halt();
583 return NetBootFileXferSize;
584
585 case NETLOOP_FAIL:
wdenk1d0350e2002-11-11 21:14:20 +0000586 return (-1);
wdenk2d966952002-10-31 22:12:35 +0000587 }
588 }
589}
590
591/**********************************************************************/
592
593static void
594startAgainTimeout(void)
595{
596 NetState = NETLOOP_RESTART;
597}
598
599static void
600startAgainHandler(uchar * pkt, unsigned dest, unsigned src, unsigned len)
601{
602 /* Totally ignore the packet */
603}
604
wdenk6e592382004-04-18 17:39:38 +0000605void NetStartAgain (void)
wdenk2d966952002-10-31 22:12:35 +0000606{
wdenk6e592382004-04-18 17:39:38 +0000607 char *nretry;
608 int noretry = 0, once = 0;
wdenka3d991b2004-04-15 21:48:45 +0000609
wdenk6e592382004-04-18 17:39:38 +0000610 if ((nretry = getenv ("netretry")) != NULL) {
611 noretry = (strcmp (nretry, "no") == 0);
612 once = (strcmp (nretry, "once") == 0);
613 }
614 if (noretry) {
615 eth_halt ();
wdenka3d991b2004-04-15 21:48:45 +0000616 NetState = NETLOOP_FAIL;
617 return;
618 }
wdenk2d966952002-10-31 22:12:35 +0000619#ifndef CONFIG_NET_MULTI
Bartlomiej Sieka49f3bdb2008-10-01 15:26:28 +0200620 NetSetTimeout (10000UL, startAgainTimeout);
wdenk6e592382004-04-18 17:39:38 +0000621 NetSetHandler (startAgainHandler);
622#else /* !CONFIG_NET_MULTI*/
623 eth_halt ();
Matthias Fuchs8b0c5c12007-12-27 16:58:41 +0100624#if !defined(CONFIG_NET_DO_NOT_TRY_ANOTHER)
wdenk6e592382004-04-18 17:39:38 +0000625 eth_try_another (!NetRestarted);
Matthias Fuchs8b0c5c12007-12-27 16:58:41 +0100626#endif
wdenk6e592382004-04-18 17:39:38 +0000627 eth_init (gd->bd);
628 if (NetRestartWrap) {
wdenk2d966952002-10-31 22:12:35 +0000629 NetRestartWrap = 0;
wdenk6e592382004-04-18 17:39:38 +0000630 if (NetDevExists && !once) {
Bartlomiej Sieka49f3bdb2008-10-01 15:26:28 +0200631 NetSetTimeout (10000UL, startAgainTimeout);
wdenk6e592382004-04-18 17:39:38 +0000632 NetSetHandler (startAgainHandler);
633 } else {
wdenk2d966952002-10-31 22:12:35 +0000634 NetState = NETLOOP_FAIL;
635 }
wdenk6e592382004-04-18 17:39:38 +0000636 } else {
wdenk2d966952002-10-31 22:12:35 +0000637 NetState = NETLOOP_RESTART;
638 }
wdenk6e592382004-04-18 17:39:38 +0000639#endif /* CONFIG_NET_MULTI */
wdenk2d966952002-10-31 22:12:35 +0000640}
641
642/**********************************************************************/
643/*
644 * Miscelaneous bits.
645 */
646
647void
648NetSetHandler(rxhand_f * f)
649{
650 packetHandler = f;
651}
652
653
654void
wdenk3e01d752004-10-09 21:56:21 +0000655NetSetTimeout(ulong iv, thand_f * f)
wdenk2d966952002-10-31 22:12:35 +0000656{
657 if (iv == 0) {
658 timeHandler = (thand_f *)0;
659 } else {
660 timeHandler = f;
wdenke0ac62d2003-08-17 18:55:18 +0000661 timeStart = get_timer(0);
662 timeDelta = iv;
wdenk2d966952002-10-31 22:12:35 +0000663 }
664}
665
666
667void
668NetSendPacket(volatile uchar * pkt, int len)
669{
670 (void) eth_send(pkt, len);
671}
672
wdenk73a8b272003-06-05 19:27:42 +0000673int
674NetSendUDPPacket(uchar *ether, IPaddr_t dest, int dport, int sport, int len)
675{
wdenka3d991b2004-04-15 21:48:45 +0000676 uchar *pkt;
677
wdenk73a8b272003-06-05 19:27:42 +0000678 /* convert to new style broadcast */
679 if (dest == 0)
680 dest = 0xFFFFFFFF;
wdenk2d966952002-10-31 22:12:35 +0000681
wdenk73a8b272003-06-05 19:27:42 +0000682 /* if broadcast, make the ether address a broadcast and don't do ARP */
683 if (dest == 0xFFFFFFFF)
684 ether = NetBcastAddr;
685
686 /* if MAC address was not discovered yet, save the packet and do an ARP request */
687 if (memcmp(ether, NetEtherNullAddr, 6) == 0) {
688
689#ifdef ET_DEBUG
690 printf("sending ARP for %08lx\n", dest);
691#endif
wdenk73a8b272003-06-05 19:27:42 +0000692 NetArpWaitPacketIP = dest;
693 NetArpWaitPacketMAC = ether;
wdenka3d991b2004-04-15 21:48:45 +0000694
695 pkt = NetArpWaitTxPacket;
696 pkt += NetSetEther (pkt, NetArpWaitPacketMAC, PROT_IP);
697
698 NetSetIP (pkt, dest, dport, sport, len);
699 memcpy(pkt + IP_HDR_SIZE, (uchar *)NetTxPacket + (pkt - (uchar *)NetArpWaitTxPacket) + IP_HDR_SIZE, len);
wdenk73a8b272003-06-05 19:27:42 +0000700
701 /* size of the waiting packet */
wdenka3d991b2004-04-15 21:48:45 +0000702 NetArpWaitTxPacketSize = (pkt - NetArpWaitTxPacket) + IP_HDR_SIZE + len;
wdenk73a8b272003-06-05 19:27:42 +0000703
704 /* and do the ARP request */
705 NetArpWaitTry = 1;
706 NetArpWaitTimerStart = get_timer(0);
707 ArpRequest();
708 return 1; /* waiting */
709 }
710
711#ifdef ET_DEBUG
712 printf("sending UDP to %08lx/%02x:%02x:%02x:%02x:%02x:%02x\n",
wdenk6e592382004-04-18 17:39:38 +0000713 dest, ether[0], ether[1], ether[2], ether[3], ether[4], ether[5]);
wdenk73a8b272003-06-05 19:27:42 +0000714#endif
715
wdenka3d991b2004-04-15 21:48:45 +0000716 pkt = (uchar *)NetTxPacket;
717 pkt += NetSetEther (pkt, ether, PROT_IP);
718 NetSetIP (pkt, dest, dport, sport, len);
719 (void) eth_send(NetTxPacket, (pkt - NetTxPacket) + IP_HDR_SIZE + len);
wdenk73a8b272003-06-05 19:27:42 +0000720
wdenk6e592382004-04-18 17:39:38 +0000721 return 0; /* transmitted */
wdenk73a8b272003-06-05 19:27:42 +0000722}
723
Jon Loeliger643d1ab2007-07-09 17:45:14 -0500724#if defined(CONFIG_CMD_PING)
wdenk73a8b272003-06-05 19:27:42 +0000725static ushort PingSeqNo;
726
727int PingSend(void)
728{
729 static uchar mac[6];
730 volatile IP_t *ip;
731 volatile ushort *s;
wdenka3d991b2004-04-15 21:48:45 +0000732 uchar *pkt;
wdenk73a8b272003-06-05 19:27:42 +0000733
734 /* XXX always send arp request */
735
736 memcpy(mac, NetEtherNullAddr, 6);
737
738#ifdef ET_DEBUG
739 printf("sending ARP for %08lx\n", NetPingIP);
740#endif
741
742 NetArpWaitPacketIP = NetPingIP;
743 NetArpWaitPacketMAC = mac;
744
wdenka3d991b2004-04-15 21:48:45 +0000745 pkt = NetArpWaitTxPacket;
746 pkt += NetSetEther(pkt, mac, PROT_IP);
wdenk73a8b272003-06-05 19:27:42 +0000747
wdenka3d991b2004-04-15 21:48:45 +0000748 ip = (volatile IP_t *)pkt;
wdenk73a8b272003-06-05 19:27:42 +0000749
750 /*
751 * Construct an IP and ICMP header. (need to set no fragment bit - XXX)
752 */
753 ip->ip_hl_v = 0x45; /* IP_HDR_SIZE / 4 (not including UDP) */
754 ip->ip_tos = 0;
755 ip->ip_len = htons(IP_HDR_SIZE_NO_UDP + 8);
756 ip->ip_id = htons(NetIPID++);
Peter Tysere0c07b82008-12-01 16:26:20 -0600757 ip->ip_off = htons(IP_FLAGS_DFRAG); /* Don't fragment */
wdenk73a8b272003-06-05 19:27:42 +0000758 ip->ip_ttl = 255;
759 ip->ip_p = 0x01; /* ICMP */
760 ip->ip_sum = 0;
761 NetCopyIP((void*)&ip->ip_src, &NetOurIP); /* already in network byte order */
762 NetCopyIP((void*)&ip->ip_dst, &NetPingIP); /* - "" - */
763 ip->ip_sum = ~NetCksum((uchar *)ip, IP_HDR_SIZE_NO_UDP / 2);
764
765 s = &ip->udp_src; /* XXX ICMP starts here */
766 s[0] = htons(0x0800); /* echo-request, code */
767 s[1] = 0; /* checksum */
Wolfgang Denk53677ef2008-05-20 16:00:29 +0200768 s[2] = 0; /* identifier */
wdenk73a8b272003-06-05 19:27:42 +0000769 s[3] = htons(PingSeqNo++); /* sequence number */
770 s[1] = ~NetCksum((uchar *)s, 8/2);
771
772 /* size of the waiting packet */
wdenka3d991b2004-04-15 21:48:45 +0000773 NetArpWaitTxPacketSize = (pkt - NetArpWaitTxPacket) + IP_HDR_SIZE_NO_UDP + 8;
wdenk73a8b272003-06-05 19:27:42 +0000774
775 /* and do the ARP request */
776 NetArpWaitTry = 1;
777 NetArpWaitTimerStart = get_timer(0);
778 ArpRequest();
779 return 1; /* waiting */
780}
781
782static void
783PingTimeout (void)
784{
785 eth_halt();
786 NetState = NETLOOP_FAIL; /* we did not get the reply */
787}
788
789static void
790PingHandler (uchar * pkt, unsigned dest, unsigned src, unsigned len)
791{
792 IPaddr_t tmp;
793 volatile IP_t *ip = (volatile IP_t *)pkt;
794
795 tmp = NetReadIP((void *)&ip->ip_src);
796 if (tmp != NetPingIP)
797 return;
798
799 NetState = NETLOOP_SUCCESS;
800}
801
802static void PingStart(void)
803{
wdenka3d991b2004-04-15 21:48:45 +0000804#if defined(CONFIG_NET_MULTI)
805 printf ("Using %s device\n", eth_get_name());
wdenk6e592382004-04-18 17:39:38 +0000806#endif /* CONFIG_NET_MULTI */
Bartlomiej Sieka49f3bdb2008-10-01 15:26:28 +0200807 NetSetTimeout (10000UL, PingTimeout);
wdenk73a8b272003-06-05 19:27:42 +0000808 NetSetHandler (PingHandler);
809
810 PingSend();
811}
Jon Loeliger610f2e92007-07-10 11:05:02 -0500812#endif
wdenk2d966952002-10-31 22:12:35 +0000813
Jon Loeliger643d1ab2007-07-09 17:45:14 -0500814#if defined(CONFIG_CMD_CDP)
wdenka3d991b2004-04-15 21:48:45 +0000815
816#define CDP_DEVICE_ID_TLV 0x0001
817#define CDP_ADDRESS_TLV 0x0002
818#define CDP_PORT_ID_TLV 0x0003
819#define CDP_CAPABILITIES_TLV 0x0004
820#define CDP_VERSION_TLV 0x0005
821#define CDP_PLATFORM_TLV 0x0006
822#define CDP_NATIVE_VLAN_TLV 0x000a
823#define CDP_APPLIANCE_VLAN_TLV 0x000e
824#define CDP_TRIGGER_TLV 0x000f
825#define CDP_POWER_CONSUMPTION_TLV 0x0010
826#define CDP_SYSNAME_TLV 0x0014
827#define CDP_SYSOBJECT_TLV 0x0015
828#define CDP_MANAGEMENT_ADDRESS_TLV 0x0016
829
Bartlomiej Sieka49f3bdb2008-10-01 15:26:28 +0200830#define CDP_TIMEOUT 250UL /* one packet every 250ms */
wdenka3d991b2004-04-15 21:48:45 +0000831
832static int CDPSeq;
833static int CDPOK;
834
835ushort CDPNativeVLAN;
836ushort CDPApplianceVLAN;
837
838static const uchar CDP_SNAP_hdr[8] = { 0xAA, 0xAA, 0x03, 0x00, 0x00, 0x0C, 0x20, 0x00 };
839
840static ushort CDP_compute_csum(const uchar *buff, ushort len)
841{
842 ushort csum;
843 int odd;
844 ulong result = 0;
845 ushort leftover;
Wolfgang Denk77ddac92005-10-13 16:45:02 +0200846 ushort *p;
wdenka3d991b2004-04-15 21:48:45 +0000847
848 if (len > 0) {
849 odd = 1 & (ulong)buff;
850 if (odd) {
851 result = *buff << 8;
852 len--;
853 buff++;
854 }
855 while (len > 1) {
Wolfgang Denk77ddac92005-10-13 16:45:02 +0200856 p = (ushort *)buff;
857 result += *p++;
858 buff = (uchar *)p;
wdenka3d991b2004-04-15 21:48:45 +0000859 if (result & 0x80000000)
860 result = (result & 0xFFFF) + (result >> 16);
861 len -= 2;
862 }
863 if (len) {
864 leftover = (signed short)(*(const signed char *)buff);
Wolfgang Denk3ada8342005-11-10 20:59:46 +0100865 /* CISCO SUCKS big time! (and blows too):
866 * CDP uses the IP checksum algorithm with a twist;
867 * for the last byte it *sign* extends and sums.
868 */
wdenka3d991b2004-04-15 21:48:45 +0000869 result = (result & 0xffff0000) | ((result + leftover) & 0x0000ffff);
870 }
871 while (result >> 16)
872 result = (result & 0xFFFF) + (result >> 16);
873
874 if (odd)
875 result = ((result >> 8) & 0xff) | ((result & 0xff) << 8);
876 }
877
878 /* add up 16-bit and 17-bit words for 17+c bits */
879 result = (result & 0xffff) + (result >> 16);
880 /* add up 16-bit and 2-bit for 16+c bit */
881 result = (result & 0xffff) + (result >> 16);
882 /* add up carry.. */
883 result = (result & 0xffff) + (result >> 16);
884
885 /* negate */
886 csum = ~(ushort)result;
887
888 /* run time endian detection */
889 if (csum != htons(csum)) /* little endian */
890 csum = htons(csum);
891
892 return csum;
893}
894
895int CDPSendTrigger(void)
896{
897 volatile uchar *pkt;
898 volatile ushort *s;
899 volatile ushort *cp;
900 Ethernet_t *et;
wdenka3d991b2004-04-15 21:48:45 +0000901 int len;
902 ushort chksum;
wdenk6e592382004-04-18 17:39:38 +0000903#if defined(CONFIG_CDP_DEVICE_ID) || defined(CONFIG_CDP_PORT_ID) || \
904 defined(CONFIG_CDP_VERSION) || defined(CONFIG_CDP_PLATFORM)
905 char buf[32];
906#endif
wdenka3d991b2004-04-15 21:48:45 +0000907
908 pkt = NetTxPacket;
909 et = (Ethernet_t *)pkt;
910
911 /* NOTE: trigger sent not on any VLAN */
912
913 /* form ethernet header */
914 memcpy(et->et_dest, NetCDPAddr, 6);
915 memcpy(et->et_src, NetOurEther, 6);
916
917 pkt += ETHER_HDR_SIZE;
918
919 /* SNAP header */
920 memcpy((uchar *)pkt, CDP_SNAP_hdr, sizeof(CDP_SNAP_hdr));
921 pkt += sizeof(CDP_SNAP_hdr);
922
923 /* CDP header */
924 *pkt++ = 0x02; /* CDP version 2 */
925 *pkt++ = 180; /* TTL */
926 s = (volatile ushort *)pkt;
927 cp = s;
928 *s++ = htons(0); /* checksum (0 for later calculation) */
929
930 /* CDP fields */
931#ifdef CONFIG_CDP_DEVICE_ID
932 *s++ = htons(CDP_DEVICE_ID_TLV);
933 *s++ = htons(CONFIG_CDP_DEVICE_ID);
934 memset(buf, 0, sizeof(buf));
935 sprintf(buf, CONFIG_CDP_DEVICE_ID_PREFIX "%02X%02X%02X%02X%02X%02X",
936 NetOurEther[0] & 0xff, NetOurEther[1] & 0xff,
937 NetOurEther[2] & 0xff, NetOurEther[3] & 0xff,
938 NetOurEther[4] & 0xff, NetOurEther[5] & 0xff);
939 memcpy((uchar *)s, buf, 16);
940 s += 16 / 2;
941#endif
942
943#ifdef CONFIG_CDP_PORT_ID
944 *s++ = htons(CDP_PORT_ID_TLV);
945 memset(buf, 0, sizeof(buf));
946 sprintf(buf, CONFIG_CDP_PORT_ID, eth_get_dev_index());
947 len = strlen(buf);
948 if (len & 1) /* make it even */
949 len++;
950 *s++ = htons(len + 4);
951 memcpy((uchar *)s, buf, len);
952 s += len / 2;
953#endif
954
955#ifdef CONFIG_CDP_CAPABILITIES
956 *s++ = htons(CDP_CAPABILITIES_TLV);
957 *s++ = htons(8);
958 *(ulong *)s = htonl(CONFIG_CDP_CAPABILITIES);
959 s += 2;
960#endif
961
962#ifdef CONFIG_CDP_VERSION
963 *s++ = htons(CDP_VERSION_TLV);
964 memset(buf, 0, sizeof(buf));
965 strcpy(buf, CONFIG_CDP_VERSION);
966 len = strlen(buf);
967 if (len & 1) /* make it even */
968 len++;
969 *s++ = htons(len + 4);
970 memcpy((uchar *)s, buf, len);
971 s += len / 2;
972#endif
973
974#ifdef CONFIG_CDP_PLATFORM
975 *s++ = htons(CDP_PLATFORM_TLV);
976 memset(buf, 0, sizeof(buf));
977 strcpy(buf, CONFIG_CDP_PLATFORM);
978 len = strlen(buf);
979 if (len & 1) /* make it even */
980 len++;
981 *s++ = htons(len + 4);
982 memcpy((uchar *)s, buf, len);
983 s += len / 2;
984#endif
985
986#ifdef CONFIG_CDP_TRIGGER
987 *s++ = htons(CDP_TRIGGER_TLV);
988 *s++ = htons(8);
989 *(ulong *)s = htonl(CONFIG_CDP_TRIGGER);
990 s += 2;
991#endif
992
993#ifdef CONFIG_CDP_POWER_CONSUMPTION
994 *s++ = htons(CDP_POWER_CONSUMPTION_TLV);
995 *s++ = htons(6);
996 *s++ = htons(CONFIG_CDP_POWER_CONSUMPTION);
997#endif
998
999 /* length of ethernet packet */
1000 len = (uchar *)s - ((uchar *)NetTxPacket + ETHER_HDR_SIZE);
1001 et->et_protlen = htons(len);
1002
1003 len = ETHER_HDR_SIZE + sizeof(CDP_SNAP_hdr);
1004 chksum = CDP_compute_csum((uchar *)NetTxPacket + len, (uchar *)s - (NetTxPacket + len));
1005 if (chksum == 0)
1006 chksum = 0xFFFF;
1007 *cp = htons(chksum);
1008
1009 (void) eth_send(NetTxPacket, (uchar *)s - NetTxPacket);
1010 return 0;
1011}
1012
1013static void
1014CDPTimeout (void)
1015{
1016 CDPSeq++;
1017
1018 if (CDPSeq < 3) {
1019 NetSetTimeout (CDP_TIMEOUT, CDPTimeout);
1020 CDPSendTrigger();
1021 return;
1022 }
1023
1024 /* if not OK try again */
1025 if (!CDPOK)
1026 NetStartAgain();
1027 else
1028 NetState = NETLOOP_SUCCESS;
1029}
1030
1031static void
1032CDPDummyHandler (uchar * pkt, unsigned dest, unsigned src, unsigned len)
1033{
1034 /* nothing */
1035}
1036
1037static void
1038CDPHandler(const uchar * pkt, unsigned len)
1039{
1040 const uchar *t;
1041 const ushort *ss;
1042 ushort type, tlen;
1043 uchar applid;
1044 ushort vlan, nvlan;
1045
1046 /* minimum size? */
1047 if (len < sizeof(CDP_SNAP_hdr) + 4)
1048 goto pkt_short;
1049
1050 /* check for valid CDP SNAP header */
1051 if (memcmp(pkt, CDP_SNAP_hdr, sizeof(CDP_SNAP_hdr)) != 0)
1052 return;
1053
1054 pkt += sizeof(CDP_SNAP_hdr);
1055 len -= sizeof(CDP_SNAP_hdr);
1056
1057 /* Version of CDP protocol must be >= 2 and TTL != 0 */
1058 if (pkt[0] < 0x02 || pkt[1] == 0)
1059 return;
1060
1061 /* if version is greater than 0x02 maybe we'll have a problem; output a warning */
1062 if (pkt[0] != 0x02)
1063 printf("** WARNING: CDP packet received with a protocol version %d > 2\n",
1064 pkt[0] & 0xff);
1065
1066 if (CDP_compute_csum(pkt, len) != 0)
1067 return;
1068
1069 pkt += 4;
1070 len -= 4;
1071
1072 vlan = htons(-1);
1073 nvlan = htons(-1);
1074 while (len > 0) {
1075 if (len < 4)
1076 goto pkt_short;
1077
1078 ss = (const ushort *)pkt;
1079 type = ntohs(ss[0]);
1080 tlen = ntohs(ss[1]);
1081 if (tlen > len) {
1082 goto pkt_short;
1083 }
1084
1085 pkt += tlen;
1086 len -= tlen;
1087
1088 ss += 2; /* point ss to the data of the TLV */
1089 tlen -= 4;
1090
1091 switch (type) {
1092 case CDP_DEVICE_ID_TLV:
1093 break;
1094 case CDP_ADDRESS_TLV:
1095 break;
1096 case CDP_PORT_ID_TLV:
1097 break;
1098 case CDP_CAPABILITIES_TLV:
1099 break;
1100 case CDP_VERSION_TLV:
1101 break;
1102 case CDP_PLATFORM_TLV:
1103 break;
1104 case CDP_NATIVE_VLAN_TLV:
1105 nvlan = *ss;
1106 break;
1107 case CDP_APPLIANCE_VLAN_TLV:
1108 t = (const uchar *)ss;
1109 while (tlen > 0) {
1110 if (tlen < 3)
1111 goto pkt_short;
1112
1113 applid = t[0];
1114 ss = (const ushort *)(t + 1);
1115
1116#ifdef CONFIG_CDP_APPLIANCE_VLAN_TYPE
1117 if (applid == CONFIG_CDP_APPLIANCE_VLAN_TYPE)
1118 vlan = *ss;
1119#else
1120 vlan = ntohs(*ss); /* XXX will this work; dunno */
1121#endif
1122 t += 3; tlen -= 3;
1123 }
1124 break;
1125 case CDP_TRIGGER_TLV:
1126 break;
1127 case CDP_POWER_CONSUMPTION_TLV:
1128 break;
1129 case CDP_SYSNAME_TLV:
1130 break;
1131 case CDP_SYSOBJECT_TLV:
1132 break;
1133 case CDP_MANAGEMENT_ADDRESS_TLV:
1134 break;
1135 }
1136 }
1137
1138 CDPApplianceVLAN = vlan;
1139 CDPNativeVLAN = nvlan;
1140
1141 CDPOK = 1;
1142 return;
1143
1144 pkt_short:
1145 printf("** CDP packet is too short\n");
1146 return;
1147}
1148
1149static void CDPStart(void)
1150{
1151#if defined(CONFIG_NET_MULTI)
1152 printf ("Using %s device\n", eth_get_name());
1153#endif
1154 CDPSeq = 0;
1155 CDPOK = 0;
1156
1157 CDPNativeVLAN = htons(-1);
1158 CDPApplianceVLAN = htons(-1);
1159
1160 NetSetTimeout (CDP_TIMEOUT, CDPTimeout);
1161 NetSetHandler (CDPDummyHandler);
1162
1163 CDPSendTrigger();
1164}
Jon Loeliger610f2e92007-07-10 11:05:02 -05001165#endif
wdenka3d991b2004-04-15 21:48:45 +00001166
1167
wdenk2d966952002-10-31 22:12:35 +00001168void
wdenka3d991b2004-04-15 21:48:45 +00001169NetReceive(volatile uchar * inpkt, int len)
wdenk2d966952002-10-31 22:12:35 +00001170{
1171 Ethernet_t *et;
1172 IP_t *ip;
1173 ARP_t *arp;
1174 IPaddr_t tmp;
1175 int x;
wdenka3d991b2004-04-15 21:48:45 +00001176 uchar *pkt;
Jon Loeliger643d1ab2007-07-09 17:45:14 -05001177#if defined(CONFIG_CMD_CDP)
wdenka3d991b2004-04-15 21:48:45 +00001178 int iscdp;
1179#endif
1180 ushort cti = 0, vlanid = VLAN_NONE, myvlanid, mynvlanid;
wdenk2d966952002-10-31 22:12:35 +00001181
wdenka3d991b2004-04-15 21:48:45 +00001182#ifdef ET_DEBUG
1183 printf("packet received\n");
1184#endif
1185
1186 NetRxPkt = inpkt;
wdenk2d966952002-10-31 22:12:35 +00001187 NetRxPktLen = len;
wdenka3d991b2004-04-15 21:48:45 +00001188 et = (Ethernet_t *)inpkt;
1189
1190 /* too small packet? */
1191 if (len < ETHER_HDR_SIZE)
1192 return;
1193
Rafal Jaworowskif85b6072007-12-27 18:19:02 +01001194#ifdef CONFIG_API
1195 if (push_packet) {
1196 (*push_packet)(inpkt, len);
1197 return;
1198 }
1199#endif
1200
Jon Loeliger643d1ab2007-07-09 17:45:14 -05001201#if defined(CONFIG_CMD_CDP)
wdenka3d991b2004-04-15 21:48:45 +00001202 /* keep track if packet is CDP */
1203 iscdp = memcmp(et->et_dest, NetCDPAddr, 6) == 0;
1204#endif
1205
1206 myvlanid = ntohs(NetOurVLAN);
1207 if (myvlanid == (ushort)-1)
1208 myvlanid = VLAN_NONE;
1209 mynvlanid = ntohs(NetOurNativeVLAN);
1210 if (mynvlanid == (ushort)-1)
1211 mynvlanid = VLAN_NONE;
wdenk2d966952002-10-31 22:12:35 +00001212
1213 x = ntohs(et->et_protlen);
1214
wdenka3d991b2004-04-15 21:48:45 +00001215#ifdef ET_DEBUG
1216 printf("packet received\n");
1217#endif
1218
wdenk2d966952002-10-31 22:12:35 +00001219 if (x < 1514) {
1220 /*
1221 * Got a 802 packet. Check the other protocol field.
1222 */
1223 x = ntohs(et->et_prot);
wdenka3d991b2004-04-15 21:48:45 +00001224
1225 ip = (IP_t *)(inpkt + E802_HDR_SIZE);
wdenk2d966952002-10-31 22:12:35 +00001226 len -= E802_HDR_SIZE;
wdenka3d991b2004-04-15 21:48:45 +00001227
1228 } else if (x != PROT_VLAN) { /* normal packet */
1229 ip = (IP_t *)(inpkt + ETHER_HDR_SIZE);
wdenk2d966952002-10-31 22:12:35 +00001230 len -= ETHER_HDR_SIZE;
wdenka3d991b2004-04-15 21:48:45 +00001231
1232 } else { /* VLAN packet */
1233 VLAN_Ethernet_t *vet = (VLAN_Ethernet_t *)et;
1234
1235#ifdef ET_DEBUG
1236 printf("VLAN packet received\n");
1237#endif
1238 /* too small packet? */
1239 if (len < VLAN_ETHER_HDR_SIZE)
1240 return;
1241
1242 /* if no VLAN active */
1243 if ((ntohs(NetOurVLAN) & VLAN_IDMASK) == VLAN_NONE
Jon Loeliger643d1ab2007-07-09 17:45:14 -05001244#if defined(CONFIG_CMD_CDP)
wdenka3d991b2004-04-15 21:48:45 +00001245 && iscdp == 0
1246#endif
1247 )
1248 return;
1249
1250 cti = ntohs(vet->vet_tag);
1251 vlanid = cti & VLAN_IDMASK;
1252 x = ntohs(vet->vet_type);
1253
1254 ip = (IP_t *)(inpkt + VLAN_ETHER_HDR_SIZE);
1255 len -= VLAN_ETHER_HDR_SIZE;
wdenk2d966952002-10-31 22:12:35 +00001256 }
1257
1258#ifdef ET_DEBUG
1259 printf("Receive from protocol 0x%x\n", x);
1260#endif
1261
Jon Loeliger643d1ab2007-07-09 17:45:14 -05001262#if defined(CONFIG_CMD_CDP)
wdenka3d991b2004-04-15 21:48:45 +00001263 if (iscdp) {
1264 CDPHandler((uchar *)ip, len);
1265 return;
1266 }
1267#endif
1268
1269 if ((myvlanid & VLAN_IDMASK) != VLAN_NONE) {
1270 if (vlanid == VLAN_NONE)
1271 vlanid = (mynvlanid & VLAN_IDMASK);
1272 /* not matched? */
1273 if (vlanid != (myvlanid & VLAN_IDMASK))
1274 return;
1275 }
1276
wdenk2d966952002-10-31 22:12:35 +00001277 switch (x) {
1278
1279 case PROT_ARP:
1280 /*
1281 * We have to deal with two types of ARP packets:
wdenk8bde7f72003-06-27 21:31:46 +00001282 * - REQUEST packets will be answered by sending our
1283 * IP address - if we know it.
1284 * - REPLY packates are expected only after we asked
1285 * for the TFTP server's or the gateway's ethernet
1286 * address; so if we receive such a packet, we set
1287 * the server ethernet address
wdenk2d966952002-10-31 22:12:35 +00001288 */
1289#ifdef ET_DEBUG
wdenk4b9206e2004-03-23 22:14:11 +00001290 puts ("Got ARP\n");
wdenk2d966952002-10-31 22:12:35 +00001291#endif
1292 arp = (ARP_t *)ip;
1293 if (len < ARP_HDR_SIZE) {
1294 printf("bad length %d < %d\n", len, ARP_HDR_SIZE);
1295 return;
1296 }
1297 if (ntohs(arp->ar_hrd) != ARP_ETHER) {
1298 return;
1299 }
1300 if (ntohs(arp->ar_pro) != PROT_IP) {
1301 return;
1302 }
1303 if (arp->ar_hln != 6) {
1304 return;
1305 }
1306 if (arp->ar_pln != 4) {
1307 return;
1308 }
1309
1310 if (NetOurIP == 0) {
1311 return;
1312 }
1313
1314 if (NetReadIP(&arp->ar_data[16]) != NetOurIP) {
1315 return;
1316 }
1317
1318 switch (ntohs(arp->ar_op)) {
1319 case ARPOP_REQUEST: /* reply with our IP address */
1320#ifdef ET_DEBUG
wdenk4b9206e2004-03-23 22:14:11 +00001321 puts ("Got ARP REQUEST, return our IP\n");
wdenk2d966952002-10-31 22:12:35 +00001322#endif
wdenka3d991b2004-04-15 21:48:45 +00001323 pkt = (uchar *)et;
1324 pkt += NetSetEther(pkt, et->et_src, PROT_ARP);
wdenk2d966952002-10-31 22:12:35 +00001325 arp->ar_op = htons(ARPOP_REPLY);
1326 memcpy (&arp->ar_data[10], &arp->ar_data[0], 6);
1327 NetCopyIP(&arp->ar_data[16], &arp->ar_data[6]);
1328 memcpy (&arp->ar_data[ 0], NetOurEther, 6);
1329 NetCopyIP(&arp->ar_data[ 6], &NetOurIP);
wdenka3d991b2004-04-15 21:48:45 +00001330 (void) eth_send((uchar *)et, (pkt - (uchar *)et) + ARP_HDR_SIZE);
wdenk2d966952002-10-31 22:12:35 +00001331 return;
wdenk73a8b272003-06-05 19:27:42 +00001332
1333 case ARPOP_REPLY: /* arp reply */
1334 /* are we waiting for a reply */
1335 if (!NetArpWaitPacketIP || !NetArpWaitPacketMAC)
1336 break;
wdenk2d966952002-10-31 22:12:35 +00001337#ifdef ET_DEBUG
wdenk73a8b272003-06-05 19:27:42 +00001338 printf("Got ARP REPLY, set server/gtwy eth addr (%02x:%02x:%02x:%02x:%02x:%02x)\n",
1339 arp->ar_data[0], arp->ar_data[1],
1340 arp->ar_data[2], arp->ar_data[3],
1341 arp->ar_data[4], arp->ar_data[5]);
wdenk2d966952002-10-31 22:12:35 +00001342#endif
wdenk73a8b272003-06-05 19:27:42 +00001343
1344 tmp = NetReadIP(&arp->ar_data[6]);
1345
1346 /* matched waiting packet's address */
1347 if (tmp == NetArpWaitReplyIP) {
1348#ifdef ET_DEBUG
wdenk4b9206e2004-03-23 22:14:11 +00001349 puts ("Got it\n");
wdenk73a8b272003-06-05 19:27:42 +00001350#endif
1351 /* save address for later use */
1352 memcpy(NetArpWaitPacketMAC, &arp->ar_data[0], 6);
1353
wdenk68ceb292004-08-02 21:11:11 +00001354#ifdef CONFIG_NETCONSOLE
1355 (*packetHandler)(0,0,0,0);
1356#endif
wdenk73a8b272003-06-05 19:27:42 +00001357 /* modify header, and transmit it */
1358 memcpy(((Ethernet_t *)NetArpWaitTxPacket)->et_dest, NetArpWaitPacketMAC, 6);
1359 (void) eth_send(NetArpWaitTxPacket, NetArpWaitTxPacketSize);
1360
1361 /* no arp request pending now */
1362 NetArpWaitPacketIP = 0;
1363 NetArpWaitTxPacketSize = 0;
1364 NetArpWaitPacketMAC = NULL;
1365
1366 }
wdenk2d966952002-10-31 22:12:35 +00001367 return;
1368 default:
1369#ifdef ET_DEBUG
1370 printf("Unexpected ARP opcode 0x%x\n", ntohs(arp->ar_op));
1371#endif
1372 return;
1373 }
wdenk289f9322005-01-12 00:15:14 +00001374 break;
wdenk2d966952002-10-31 22:12:35 +00001375
1376 case PROT_RARP:
1377#ifdef ET_DEBUG
wdenk4b9206e2004-03-23 22:14:11 +00001378 puts ("Got RARP\n");
wdenk2d966952002-10-31 22:12:35 +00001379#endif
1380 arp = (ARP_t *)ip;
1381 if (len < ARP_HDR_SIZE) {
1382 printf("bad length %d < %d\n", len, ARP_HDR_SIZE);
1383 return;
1384 }
1385
1386 if ((ntohs(arp->ar_op) != RARPOP_REPLY) ||
1387 (ntohs(arp->ar_hrd) != ARP_ETHER) ||
1388 (ntohs(arp->ar_pro) != PROT_IP) ||
1389 (arp->ar_hln != 6) || (arp->ar_pln != 4)) {
1390
wdenk4b9206e2004-03-23 22:14:11 +00001391 puts ("invalid RARP header\n");
wdenk2d966952002-10-31 22:12:35 +00001392 } else {
1393 NetCopyIP(&NetOurIP, &arp->ar_data[16]);
wdenk3d3befa2004-03-14 15:06:13 +00001394 if (NetServerIP == 0)
1395 NetCopyIP(&NetServerIP, &arp->ar_data[ 6]);
wdenk2d966952002-10-31 22:12:35 +00001396 memcpy (NetServerEther, &arp->ar_data[ 0], 6);
1397
1398 (*packetHandler)(0,0,0,0);
1399 }
1400 break;
1401
1402 case PROT_IP:
1403#ifdef ET_DEBUG
wdenk4b9206e2004-03-23 22:14:11 +00001404 puts ("Got IP\n");
wdenk2d966952002-10-31 22:12:35 +00001405#endif
1406 if (len < IP_HDR_SIZE) {
Wolfgang Denk25dbe982008-07-13 23:07:35 +02001407 debug ("len bad %d < %lu\n", len, (ulong)IP_HDR_SIZE);
wdenk2d966952002-10-31 22:12:35 +00001408 return;
1409 }
1410 if (len < ntohs(ip->ip_len)) {
1411 printf("len bad %d < %d\n", len, ntohs(ip->ip_len));
1412 return;
1413 }
1414 len = ntohs(ip->ip_len);
1415#ifdef ET_DEBUG
1416 printf("len=%d, v=%02x\n", len, ip->ip_hl_v & 0xff);
1417#endif
1418 if ((ip->ip_hl_v & 0xf0) != 0x40) {
1419 return;
1420 }
Peter Tyserd32c5be2008-12-01 16:26:21 -06001421 /* Can't deal with fragments */
1422 if (ip->ip_off & htons(IP_OFFS | IP_FLAGS_MFRAG)) {
wdenk2d966952002-10-31 22:12:35 +00001423 return;
1424 }
Remy Bohmer6b52cfe2008-06-03 15:48:17 +02001425 /* can't deal with headers > 20 bytes */
1426 if ((ip->ip_hl_v & 0x0f) > 0x05) {
1427 return;
1428 }
wdenk2d966952002-10-31 22:12:35 +00001429 if (!NetCksumOk((uchar *)ip, IP_HDR_SIZE_NO_UDP / 2)) {
wdenk4b9206e2004-03-23 22:14:11 +00001430 puts ("checksum bad\n");
wdenk2d966952002-10-31 22:12:35 +00001431 return;
1432 }
1433 tmp = NetReadIP(&ip->ip_dst);
1434 if (NetOurIP && tmp != NetOurIP && tmp != 0xFFFFFFFF) {
David Updegraff53a5c422007-06-11 10:41:07 -05001435#ifdef CONFIG_MCAST_TFTP
Wolfgang Denk85eb5ca2007-08-14 09:47:27 +02001436 if (Mcast_addr != tmp)
David Updegraff53a5c422007-06-11 10:41:07 -05001437#endif
wdenk2d966952002-10-31 22:12:35 +00001438 return;
1439 }
1440 /*
1441 * watch for ICMP host redirects
1442 *
wdenk8bde7f72003-06-27 21:31:46 +00001443 * There is no real handler code (yet). We just watch
1444 * for ICMP host redirect messages. In case anybody
1445 * sees these messages: please contact me
1446 * (wd@denx.de), or - even better - send me the
1447 * necessary fixes :-)
wdenk2d966952002-10-31 22:12:35 +00001448 *
wdenk8bde7f72003-06-27 21:31:46 +00001449 * Note: in all cases where I have seen this so far
1450 * it was a problem with the router configuration,
1451 * for instance when a router was configured in the
1452 * BOOTP reply, but the TFTP server was on the same
1453 * subnet. So this is probably a warning that your
1454 * configuration might be wrong. But I'm not really
1455 * sure if there aren't any other situations.
wdenk2d966952002-10-31 22:12:35 +00001456 */
1457 if (ip->ip_p == IPPROTO_ICMP) {
1458 ICMP_t *icmph = (ICMP_t *)&(ip->udp_src);
1459
wdenk73a8b272003-06-05 19:27:42 +00001460 switch (icmph->type) {
1461 case ICMP_REDIRECT:
wdenk90dc6702005-05-03 14:12:25 +00001462 if (icmph->code != ICMP_REDIR_HOST)
1463 return;
Mike Frysingerb6446b62009-02-17 00:00:53 -05001464 printf (" ICMP Host Redirect to %pI4 ", &icmph->un.gateway);
Stefan Roese8534bf92005-08-12 20:06:52 +02001465 return;
Jon Loeliger643d1ab2007-07-09 17:45:14 -05001466#if defined(CONFIG_CMD_PING)
wdenk73a8b272003-06-05 19:27:42 +00001467 case ICMP_ECHO_REPLY:
1468 /*
1469 * IP header OK. Pass the packet to the current handler.
1470 */
1471 /* XXX point to ip packet */
1472 (*packetHandler)((uchar *)ip, 0, 0, 0);
Stefan Roese8534bf92005-08-12 20:06:52 +02001473 return;
Ed Swarthout83853172007-03-07 12:14:50 -06001474 case ICMP_ECHO_REQUEST:
1475#ifdef ET_DEBUG
1476 printf ("Got ICMP ECHO REQUEST, return %d bytes \n",
1477 ETHER_HDR_SIZE + len);
1478#endif
1479 memcpy (&et->et_dest[0], &et->et_src[0], 6);
1480 memcpy (&et->et_src[ 0], NetOurEther, 6);
1481
1482 ip->ip_sum = 0;
1483 ip->ip_off = 0;
1484 NetCopyIP((void*)&ip->ip_dst, &ip->ip_src);
1485 NetCopyIP((void*)&ip->ip_src, &NetOurIP);
1486 ip->ip_sum = ~NetCksum((uchar *)ip, IP_HDR_SIZE_NO_UDP >> 1);
1487
1488 icmph->type = ICMP_ECHO_REPLY;
1489 icmph->checksum = 0;
1490 icmph->checksum = ~NetCksum((uchar *)icmph,
1491 (len - IP_HDR_SIZE_NO_UDP) >> 1);
1492 (void) eth_send((uchar *)et, ETHER_HDR_SIZE + len);
1493 return;
wdenk73a8b272003-06-05 19:27:42 +00001494#endif
1495 default:
1496 return;
1497 }
wdenk2d966952002-10-31 22:12:35 +00001498 } else if (ip->ip_p != IPPROTO_UDP) { /* Only UDP packets */
1499 return;
1500 }
1501
Stefan Roese8534bf92005-08-12 20:06:52 +02001502#ifdef CONFIG_UDP_CHECKSUM
1503 if (ip->udp_xsum != 0) {
Wolfgang Denkb2f50802005-08-12 23:43:12 +02001504 ulong xsum;
Stefan Roese8534bf92005-08-12 20:06:52 +02001505 ushort *sumptr;
1506 ushort sumlen;
1507
1508 xsum = ip->ip_p;
1509 xsum += (ntohs(ip->udp_len));
1510 xsum += (ntohl(ip->ip_src) >> 16) & 0x0000ffff;
1511 xsum += (ntohl(ip->ip_src) >> 0) & 0x0000ffff;
1512 xsum += (ntohl(ip->ip_dst) >> 16) & 0x0000ffff;
1513 xsum += (ntohl(ip->ip_dst) >> 0) & 0x0000ffff;
1514
1515 sumlen = ntohs(ip->udp_len);
1516 sumptr = (ushort *) &(ip->udp_src);
1517
1518 while (sumlen > 1) {
Wolfgang Denkb2f50802005-08-12 23:43:12 +02001519 ushort sumdata;
Stefan Roese8534bf92005-08-12 20:06:52 +02001520
1521 sumdata = *sumptr++;
1522 xsum += ntohs(sumdata);
1523 sumlen -= 2;
1524 }
1525 if (sumlen > 0) {
Wolfgang Denkb2f50802005-08-12 23:43:12 +02001526 ushort sumdata;
Stefan Roese8534bf92005-08-12 20:06:52 +02001527
1528 sumdata = *(unsigned char *) sumptr;
Wolfgang Denkb2f50802005-08-12 23:43:12 +02001529 sumdata = (sumdata << 8) & 0xff00;
Stefan Roese8534bf92005-08-12 20:06:52 +02001530 xsum += sumdata;
1531 }
1532 while ((xsum >> 16) != 0) {
Wolfgang Denkb2f50802005-08-12 23:43:12 +02001533 xsum = (xsum & 0x0000ffff) + ((xsum >> 16) & 0x0000ffff);
Stefan Roese8534bf92005-08-12 20:06:52 +02001534 }
1535 if ((xsum != 0x00000000) && (xsum != 0x0000ffff)) {
Wolfgang Denk9b55a252008-07-11 01:16:00 +02001536 printf(" UDP wrong checksum %08lx %08x\n",
1537 xsum, ntohs(ip->udp_xsum));
Stefan Roese8534bf92005-08-12 20:06:52 +02001538 return;
1539 }
1540 }
1541#endif
1542
David Updegraff53a5c422007-06-11 10:41:07 -05001543
wdenk68ceb292004-08-02 21:11:11 +00001544#ifdef CONFIG_NETCONSOLE
1545 nc_input_packet((uchar *)ip +IP_HDR_SIZE,
1546 ntohs(ip->udp_dst),
1547 ntohs(ip->udp_src),
1548 ntohs(ip->udp_len) - 8);
1549#endif
wdenk2d966952002-10-31 22:12:35 +00001550 /*
1551 * IP header OK. Pass the packet to the current handler.
1552 */
1553 (*packetHandler)((uchar *)ip +IP_HDR_SIZE,
1554 ntohs(ip->udp_dst),
1555 ntohs(ip->udp_src),
1556 ntohs(ip->udp_len) - 8);
wdenk2d966952002-10-31 22:12:35 +00001557 break;
1558 }
1559}
1560
1561
1562/**********************************************************************/
1563
1564static int net_check_prereq (proto_t protocol)
1565{
1566 switch (protocol) {
wdenk6e592382004-04-18 17:39:38 +00001567 /* Fall through */
Jon Loeliger643d1ab2007-07-09 17:45:14 -05001568#if defined(CONFIG_CMD_PING)
wdenk73a8b272003-06-05 19:27:42 +00001569 case PING:
wdenk6e592382004-04-18 17:39:38 +00001570 if (NetPingIP == 0) {
1571 puts ("*** ERROR: ping address not given\n");
1572 return (1);
1573 }
1574 goto common;
wdenk73a8b272003-06-05 19:27:42 +00001575#endif
Jon Loeliger643d1ab2007-07-09 17:45:14 -05001576#if defined(CONFIG_CMD_SNTP)
wdenkea287de2005-04-01 00:25:43 +00001577 case SNTP:
1578 if (NetNtpServerIP == 0) {
1579 puts ("*** ERROR: NTP server address not given\n");
1580 return (1);
1581 }
1582 goto common;
1583#endif
Jon Loeliger643d1ab2007-07-09 17:45:14 -05001584#if defined(CONFIG_CMD_NFS)
wdenkcbd8a352004-02-24 02:00:03 +00001585 case NFS:
1586#endif
wdenk68ceb292004-08-02 21:11:11 +00001587 case NETCONS:
wdenk2d966952002-10-31 22:12:35 +00001588 case TFTP:
wdenk6e592382004-04-18 17:39:38 +00001589 if (NetServerIP == 0) {
1590 puts ("*** ERROR: `serverip' not set\n");
1591 return (1);
1592 }
Jon Loeliger643d1ab2007-07-09 17:45:14 -05001593#if defined(CONFIG_CMD_PING) || defined(CONFIG_CMD_SNTP)
Wolfgang Denkb2f50802005-08-12 23:43:12 +02001594 common:
wdenk73a8b272003-06-05 19:27:42 +00001595#endif
1596
wdenk6e592382004-04-18 17:39:38 +00001597 if (NetOurIP == 0) {
1598 puts ("*** ERROR: `ipaddr' not set\n");
1599 return (1);
1600 }
1601 /* Fall through */
wdenk2d966952002-10-31 22:12:35 +00001602
1603 case DHCP:
1604 case RARP:
1605 case BOOTP:
wdenka3d991b2004-04-15 21:48:45 +00001606 case CDP:
wdenk6e592382004-04-18 17:39:38 +00001607 if (memcmp (NetOurEther, "\0\0\0\0\0\0", 6) == 0) {
wdenk2d966952002-10-31 22:12:35 +00001608#ifdef CONFIG_NET_MULTI
wdenk6e592382004-04-18 17:39:38 +00001609 extern int eth_get_dev_index (void);
1610 int num = eth_get_dev_index ();
wdenk2d966952002-10-31 22:12:35 +00001611
wdenk6e592382004-04-18 17:39:38 +00001612 switch (num) {
1613 case -1:
wdenk2d966952002-10-31 22:12:35 +00001614 puts ("*** ERROR: No ethernet found.\n");
1615 return (1);
wdenk6e592382004-04-18 17:39:38 +00001616 case 0:
wdenk2d966952002-10-31 22:12:35 +00001617 puts ("*** ERROR: `ethaddr' not set\n");
1618 break;
wdenk6e592382004-04-18 17:39:38 +00001619 default:
wdenk8bde7f72003-06-27 21:31:46 +00001620 printf ("*** ERROR: `eth%daddr' not set\n",
wdenk2d966952002-10-31 22:12:35 +00001621 num);
1622 break;
wdenk2d966952002-10-31 22:12:35 +00001623 }
wdenk6e592382004-04-18 17:39:38 +00001624
1625 NetStartAgain ();
1626 return (2);
1627#else
1628 puts ("*** ERROR: `ethaddr' not set\n");
1629 return (1);
1630#endif
1631 }
1632 /* Fall through */
1633 default:
1634 return (0);
wdenk2d966952002-10-31 22:12:35 +00001635 }
wdenk6e592382004-04-18 17:39:38 +00001636 return (0); /* OK */
wdenk2d966952002-10-31 22:12:35 +00001637}
1638/**********************************************************************/
1639
1640int
1641NetCksumOk(uchar * ptr, int len)
1642{
1643 return !((NetCksum(ptr, len) + 1) & 0xfffe);
1644}
1645
1646
1647unsigned
1648NetCksum(uchar * ptr, int len)
1649{
1650 ulong xsum;
Stefan Roese9d2a8732005-08-31 12:55:50 +02001651 ushort *p = (ushort *)ptr;
wdenk2d966952002-10-31 22:12:35 +00001652
1653 xsum = 0;
1654 while (len-- > 0)
Wolfgang Denk7bc5ee02005-08-26 01:36:03 +02001655 xsum += *p++;
wdenk2d966952002-10-31 22:12:35 +00001656 xsum = (xsum & 0xffff) + (xsum >> 16);
1657 xsum = (xsum & 0xffff) + (xsum >> 16);
1658 return (xsum & 0xffff);
1659}
1660
wdenka3d991b2004-04-15 21:48:45 +00001661int
1662NetEthHdrSize(void)
1663{
1664 ushort myvlanid;
wdenk2d966952002-10-31 22:12:35 +00001665
wdenka3d991b2004-04-15 21:48:45 +00001666 myvlanid = ntohs(NetOurVLAN);
1667 if (myvlanid == (ushort)-1)
1668 myvlanid = VLAN_NONE;
1669
1670 return ((myvlanid & VLAN_IDMASK) == VLAN_NONE) ? ETHER_HDR_SIZE : VLAN_ETHER_HDR_SIZE;
1671}
1672
1673int
wdenk2d966952002-10-31 22:12:35 +00001674NetSetEther(volatile uchar * xet, uchar * addr, uint prot)
1675{
1676 Ethernet_t *et = (Ethernet_t *)xet;
wdenka3d991b2004-04-15 21:48:45 +00001677 ushort myvlanid;
1678
1679 myvlanid = ntohs(NetOurVLAN);
1680 if (myvlanid == (ushort)-1)
1681 myvlanid = VLAN_NONE;
wdenk2d966952002-10-31 22:12:35 +00001682
1683 memcpy (et->et_dest, addr, 6);
1684 memcpy (et->et_src, NetOurEther, 6);
wdenka3d991b2004-04-15 21:48:45 +00001685 if ((myvlanid & VLAN_IDMASK) == VLAN_NONE) {
wdenk2d966952002-10-31 22:12:35 +00001686 et->et_protlen = htons(prot);
wdenka3d991b2004-04-15 21:48:45 +00001687 return ETHER_HDR_SIZE;
1688 } else {
1689 VLAN_Ethernet_t *vet = (VLAN_Ethernet_t *)xet;
wdenk2d966952002-10-31 22:12:35 +00001690
wdenka3d991b2004-04-15 21:48:45 +00001691 vet->vet_vlan_type = htons(PROT_VLAN);
1692 vet->vet_tag = htons((0 << 5) | (myvlanid & VLAN_IDMASK));
1693 vet->vet_type = htons(prot);
1694 return VLAN_ETHER_HDR_SIZE;
1695 }
1696}
wdenk2d966952002-10-31 22:12:35 +00001697
1698void
1699NetSetIP(volatile uchar * xip, IPaddr_t dest, int dport, int sport, int len)
1700{
Olav Morkenaf8626e2009-01-23 12:56:26 +01001701 IP_t *ip = (IP_t *)xip;
wdenk2d966952002-10-31 22:12:35 +00001702
1703 /*
1704 * If the data is an odd number of bytes, zero the
1705 * byte after the last byte so that the checksum
1706 * will work.
1707 */
1708 if (len & 1)
1709 xip[IP_HDR_SIZE + len] = 0;
1710
1711 /*
1712 * Construct an IP and UDP header.
wdenk6e592382004-04-18 17:39:38 +00001713 * (need to set no fragment bit - XXX)
wdenk2d966952002-10-31 22:12:35 +00001714 */
1715 ip->ip_hl_v = 0x45; /* IP_HDR_SIZE / 4 (not including UDP) */
1716 ip->ip_tos = 0;
1717 ip->ip_len = htons(IP_HDR_SIZE + len);
1718 ip->ip_id = htons(NetIPID++);
Peter Tysere0c07b82008-12-01 16:26:20 -06001719 ip->ip_off = htons(IP_FLAGS_DFRAG); /* Don't fragment */
wdenk2d966952002-10-31 22:12:35 +00001720 ip->ip_ttl = 255;
1721 ip->ip_p = 17; /* UDP */
1722 ip->ip_sum = 0;
1723 NetCopyIP((void*)&ip->ip_src, &NetOurIP); /* already in network byte order */
1724 NetCopyIP((void*)&ip->ip_dst, &dest); /* - "" - */
1725 ip->udp_src = htons(sport);
1726 ip->udp_dst = htons(dport);
1727 ip->udp_len = htons(8 + len);
1728 ip->udp_xsum = 0;
1729 ip->ip_sum = ~NetCksum((uchar *)ip, IP_HDR_SIZE_NO_UDP / 2);
1730}
1731
Wolfgang Denk77ddac92005-10-13 16:45:02 +02001732void copy_filename (char *dst, char *src, int size)
wdenk2d966952002-10-31 22:12:35 +00001733{
1734 if (*src && (*src == '"')) {
1735 ++src;
1736 --size;
1737 }
1738
1739 while ((--size > 0) && *src && (*src != '"')) {
1740 *dst++ = *src++;
1741 }
1742 *dst = '\0';
1743}
1744
Jon Loeliger610f2e92007-07-10 11:05:02 -05001745#endif
wdenk2d966952002-10-31 22:12:35 +00001746
1747void ip_to_string (IPaddr_t x, char *s)
1748{
wdenka3d991b2004-04-15 21:48:45 +00001749 x = ntohl (x);
1750 sprintf (s, "%d.%d.%d.%d",
1751 (int) ((x >> 24) & 0xff),
1752 (int) ((x >> 16) & 0xff),
1753 (int) ((x >> 8) & 0xff), (int) ((x >> 0) & 0xff)
wdenk6e592382004-04-18 17:39:38 +00001754 );
wdenk2d966952002-10-31 22:12:35 +00001755}
1756
wdenk73a8b272003-06-05 19:27:42 +00001757IPaddr_t string_to_ip(char *s)
wdenk2d966952002-10-31 22:12:35 +00001758{
1759 IPaddr_t addr;
wdenk73a8b272003-06-05 19:27:42 +00001760 char *e;
wdenk2d966952002-10-31 22:12:35 +00001761 int i;
1762
wdenk73a8b272003-06-05 19:27:42 +00001763 if (s == NULL)
1764 return(0);
wdenk2d966952002-10-31 22:12:35 +00001765
1766 for (addr=0, i=0; i<4; ++i) {
1767 ulong val = s ? simple_strtoul(s, &e, 10) : 0;
1768 addr <<= 8;
1769 addr |= (val & 0xFF);
1770 if (s) {
1771 s = (*e) ? e+1 : e;
1772 }
1773 }
1774
1775 return (htonl(addr));
1776}
wdenk73a8b272003-06-05 19:27:42 +00001777
wdenka3d991b2004-04-15 21:48:45 +00001778void VLAN_to_string(ushort x, char *s)
1779{
1780 x = ntohs(x);
1781
1782 if (x == (ushort)-1)
1783 x = VLAN_NONE;
1784
1785 if (x == VLAN_NONE)
1786 strcpy(s, "none");
1787 else
1788 sprintf(s, "%d", x & VLAN_IDMASK);
1789}
1790
1791ushort string_to_VLAN(char *s)
1792{
1793 ushort id;
1794
1795 if (s == NULL)
wdenkb9711de2004-04-25 13:18:40 +00001796 return htons(VLAN_NONE);
wdenka3d991b2004-04-15 21:48:45 +00001797
1798 if (*s < '0' || *s > '9')
1799 id = VLAN_NONE;
1800 else
1801 id = (ushort)simple_strtoul(s, NULL, 10);
1802
wdenkb9711de2004-04-25 13:18:40 +00001803 return htons(id);
wdenka3d991b2004-04-15 21:48:45 +00001804}
1805
wdenk73a8b272003-06-05 19:27:42 +00001806IPaddr_t getenv_IPaddr (char *var)
1807{
1808 return (string_to_ip(getenv(var)));
1809}
wdenka3d991b2004-04-15 21:48:45 +00001810
1811ushort getenv_VLAN(char *var)
1812{
1813 return (string_to_VLAN(getenv(var)));
1814}