blob: 2ff7bfc09f0bf63ae6ca488df6fabce7f44d32b7 [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 Denk30b87322005-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
wdenkbe9c1cb2004-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
wdenkb4ad9622005-04-01 00:25:43 +000067 *
68 * SNTP:
69 *
Wolfgang Denk30b87322005-08-12 23:43:12 +020070 * Prerequisites: - own ethernet address
wdenkb4ad9622005-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"
wdenkbe9c1cb2004-02-24 02:00:03 +000084#include "nfs.h"
wdenk49c3f672003-10-08 22:33:00 +000085#ifdef CONFIG_STATUS_LED
86#include <status_led.h>
87#include <miiphy.h>
88#endif
wdenkb4ad9622005-04-01 00:25:43 +000089#if (CONFIG_COMMANDS & CFG_CMD_SNTP)
90#include "sntp.h"
91#endif
wdenk2d966952002-10-31 22:12:35 +000092
93#if (CONFIG_COMMANDS & CFG_CMD_NET)
94
Wolfgang Denk6405a152006-03-31 18:32:53 +020095DECLARE_GLOBAL_DATA_PTR;
96
wdenke6466f62003-06-05 19:27:42 +000097#define ARP_TIMEOUT 5 /* Seconds before trying ARP again */
98#ifndef CONFIG_NET_RETRY_COUNT
99# define ARP_TIMEOUT_COUNT 5 /* # of timeouts before giving up */
100#else
101# define ARP_TIMEOUT_COUNT (CONFIG_NET_RETRY_COUNT)
102#endif
103
wdenk2d966952002-10-31 22:12:35 +0000104#if 0
105#define ET_DEBUG
106#endif
107
108/** BOOTP EXTENTIONS **/
109
110IPaddr_t NetOurSubnetMask=0; /* Our subnet mask (0=unknown) */
111IPaddr_t NetOurGatewayIP=0; /* Our gateways IP address */
112IPaddr_t NetOurDNSIP=0; /* Our DNS IP address */
stroesee0aadfb2003-08-28 14:17:32 +0000113#if (CONFIG_BOOTP_MASK & CONFIG_BOOTP_DNS2)
114IPaddr_t NetOurDNS2IP=0; /* Our 2nd DNS IP address */
115#endif
wdenk2d966952002-10-31 22:12:35 +0000116char NetOurNISDomain[32]={0,}; /* Our NIS domain */
117char NetOurHostName[32]={0,}; /* Our hostname */
118char NetOurRootPath[64]={0,}; /* Our bootpath */
119ushort NetBootFileSize=0; /* Our bootfile size in blocks */
120
121/** END OF BOOTP EXTENTIONS **/
122
123ulong NetBootFileXferSize; /* The actual transferred size of the bootfile (in bytes) */
124uchar NetOurEther[6]; /* Our ethernet address */
125uchar NetServerEther[6] = /* Boot server enet address */
wdenke6466f62003-06-05 19:27:42 +0000126 { 0, 0, 0, 0, 0, 0 };
wdenk2d966952002-10-31 22:12:35 +0000127IPaddr_t NetOurIP; /* Our IP addr (0 = unknown) */
128IPaddr_t NetServerIP; /* Our IP addr (0 = unknown) */
129volatile uchar *NetRxPkt; /* Current receive packet */
130int NetRxPktLen; /* Current rx packet length */
131unsigned NetIPID; /* IP packet ID */
132uchar NetBcastAddr[6] = /* Ethernet bcast address */
133 { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
wdenke6466f62003-06-05 19:27:42 +0000134uchar NetEtherNullAddr[6] =
135 { 0, 0, 0, 0, 0, 0 };
wdenk145d2c12004-04-15 21:48:45 +0000136#if (CONFIG_COMMANDS & CFG_CMD_CDP)
wdenk05939202004-04-18 17:39:38 +0000137uchar NetCDPAddr[6] = /* Ethernet bcast address */
wdenk145d2c12004-04-15 21:48:45 +0000138 { 0x01, 0x00, 0x0c, 0xcc, 0xcc, 0xcc };
139#endif
wdenk2d966952002-10-31 22:12:35 +0000140int NetState; /* Network loop state */
141#ifdef CONFIG_NET_MULTI
142int NetRestartWrap = 0; /* Tried all network devices */
143static int NetRestarted = 0; /* Network loop restarted */
144static int NetDevExists = 0; /* At least one device configured */
145#endif
146
wdenk05939202004-04-18 17:39:38 +0000147/* XXX in both little & big endian machines 0xFFFF == ntohs(-1) */
148ushort NetOurVLAN = 0xFFFF; /* default is without VLAN */
149ushort NetOurNativeVLAN = 0xFFFF; /* ditto */
wdenk145d2c12004-04-15 21:48:45 +0000150
wdenk2d966952002-10-31 22:12:35 +0000151char BootFile[128]; /* Boot File name */
152
wdenke6466f62003-06-05 19:27:42 +0000153#if (CONFIG_COMMANDS & CFG_CMD_PING)
154IPaddr_t NetPingIP; /* the ip address to ping */
155
156static void PingStart(void);
157#endif
158
wdenk145d2c12004-04-15 21:48:45 +0000159#if (CONFIG_COMMANDS & CFG_CMD_CDP)
160static void CDPStart(void);
161#endif
162
wdenkb4ad9622005-04-01 00:25:43 +0000163#if (CONFIG_COMMANDS & CFG_CMD_SNTP)
164IPaddr_t NetNtpServerIP; /* NTP server IP address */
165int NetTimeOffset=0; /* offset time from UTC */
166#endif
167
wdenkb8fb6192004-08-02 21:11:11 +0000168#ifdef CONFIG_NETCONSOLE
169void NcStart(void);
170int nc_input_packet(uchar *pkt, unsigned dest, unsigned src, unsigned len);
171#endif
172
wdenk2d966952002-10-31 22:12:35 +0000173volatile uchar PktBuf[(PKTBUFSRX+1) * PKTSIZE_ALIGN + PKTALIGN];
174
175volatile uchar *NetRxPackets[PKTBUFSRX]; /* Receive packets */
176
177static rxhand_f *packetHandler; /* Current RX packet handler */
178static thand_f *timeHandler; /* Current timeout handler */
wdenk5d841732003-08-17 18:55:18 +0000179static ulong timeStart; /* Time base value */
180static ulong timeDelta; /* Current timeout value */
wdenk2d966952002-10-31 22:12:35 +0000181volatile uchar *NetTxPacket = 0; /* THE transmit packet */
182
183static int net_check_prereq (proto_t protocol);
184
185/**********************************************************************/
wdenke6466f62003-06-05 19:27:42 +0000186
187IPaddr_t NetArpWaitPacketIP;
188IPaddr_t NetArpWaitReplyIP;
189uchar *NetArpWaitPacketMAC; /* MAC address of waiting packet's destination */
Wolfgang Denk30b87322005-08-12 23:43:12 +0200190uchar *NetArpWaitTxPacket; /* THE transmit packet */
wdenke6466f62003-06-05 19:27:42 +0000191int NetArpWaitTxPacketSize;
192uchar NetArpWaitPacketBuf[PKTSIZE_ALIGN + PKTALIGN];
193ulong NetArpWaitTimerStart;
194int NetArpWaitTry;
195
wdenk05939202004-04-18 17:39:38 +0000196void ArpRequest (void)
wdenke6466f62003-06-05 19:27:42 +0000197{
198 int i;
199 volatile uchar *pkt;
wdenk05939202004-04-18 17:39:38 +0000200 ARP_t *arp;
wdenke6466f62003-06-05 19:27:42 +0000201
202#ifdef ET_DEBUG
wdenk05939202004-04-18 17:39:38 +0000203 printf ("ARP broadcast %d\n", NetArpWaitTry);
wdenke6466f62003-06-05 19:27:42 +0000204#endif
205 pkt = NetTxPacket;
206
wdenk05939202004-04-18 17:39:38 +0000207 pkt += NetSetEther (pkt, NetBcastAddr, PROT_ARP);
wdenke6466f62003-06-05 19:27:42 +0000208
wdenk05939202004-04-18 17:39:38 +0000209 arp = (ARP_t *) pkt;
wdenke6466f62003-06-05 19:27:42 +0000210
wdenk05939202004-04-18 17:39:38 +0000211 arp->ar_hrd = htons (ARP_ETHER);
212 arp->ar_pro = htons (PROT_IP);
wdenke6466f62003-06-05 19:27:42 +0000213 arp->ar_hln = 6;
214 arp->ar_pln = 4;
wdenk05939202004-04-18 17:39:38 +0000215 arp->ar_op = htons (ARPOP_REQUEST);
wdenke6466f62003-06-05 19:27:42 +0000216
Wolfgang Denk30b87322005-08-12 23:43:12 +0200217 memcpy (&arp->ar_data[0], NetOurEther, 6); /* source ET addr */
218 NetWriteIP ((uchar *) & arp->ar_data[6], NetOurIP); /* source IP addr */
wdenk05939202004-04-18 17:39:38 +0000219 for (i = 10; i < 16; ++i) {
220 arp->ar_data[i] = 0; /* dest ET addr = 0 */
wdenke6466f62003-06-05 19:27:42 +0000221 }
222
wdenk05939202004-04-18 17:39:38 +0000223 if ((NetArpWaitPacketIP & NetOurSubnetMask) !=
224 (NetOurIP & NetOurSubnetMask)) {
225 if (NetOurGatewayIP == 0) {
226 puts ("## Warning: gatewayip needed but not set\n");
Wolfgang Denk98ceffb2006-03-12 01:13:30 +0100227 NetArpWaitReplyIP = NetArpWaitPacketIP;
228 } else {
229 NetArpWaitReplyIP = NetOurGatewayIP;
wdenk05939202004-04-18 17:39:38 +0000230 }
wdenk05939202004-04-18 17:39:38 +0000231 } else {
232 NetArpWaitReplyIP = NetArpWaitPacketIP;
233 }
wdenke6466f62003-06-05 19:27:42 +0000234
wdenk05939202004-04-18 17:39:38 +0000235 NetWriteIP ((uchar *) & arp->ar_data[16], NetArpWaitReplyIP);
236 (void) eth_send (NetTxPacket, (pkt - NetTxPacket) + ARP_HDR_SIZE);
wdenke6466f62003-06-05 19:27:42 +0000237}
238
239void ArpTimeoutCheck(void)
240{
241 ulong t;
242
243 if (!NetArpWaitPacketIP)
244 return;
245
246 t = get_timer(0);
247
248 /* check for arp timeout */
249 if ((t - NetArpWaitTimerStart) > ARP_TIMEOUT * CFG_HZ) {
250 NetArpWaitTry++;
251
252 if (NetArpWaitTry >= ARP_TIMEOUT_COUNT) {
253 puts ("\nARP Retry count exceeded; starting again\n");
254 NetArpWaitTry = 0;
255 NetStartAgain();
256 } else {
257 NetArpWaitTimerStart = t;
258 ArpRequest();
259 }
260 }
261}
262
263/**********************************************************************/
wdenk2d966952002-10-31 22:12:35 +0000264/*
265 * Main network processing loop.
266 */
267
268int
269NetLoop(proto_t protocol)
270{
wdenk2d966952002-10-31 22:12:35 +0000271 bd_t *bd = gd->bd;
272
273#ifdef CONFIG_NET_MULTI
274 NetRestarted = 0;
275 NetDevExists = 0;
276#endif
277
wdenke6466f62003-06-05 19:27:42 +0000278 /* XXX problem with bss workaround */
279 NetArpWaitPacketMAC = NULL;
280 NetArpWaitTxPacket = NULL;
281 NetArpWaitPacketIP = 0;
282 NetArpWaitReplyIP = 0;
283 NetArpWaitTxPacket = NULL;
284 NetTxPacket = NULL;
285
wdenk2d966952002-10-31 22:12:35 +0000286 if (!NetTxPacket) {
287 int i;
wdenk2d966952002-10-31 22:12:35 +0000288 /*
289 * Setup packet buffers, aligned correctly.
290 */
291 NetTxPacket = &PktBuf[0] + (PKTALIGN - 1);
292 NetTxPacket -= (ulong)NetTxPacket % PKTALIGN;
293 for (i = 0; i < PKTBUFSRX; i++) {
294 NetRxPackets[i] = NetTxPacket + (i+1)*PKTSIZE_ALIGN;
295 }
wdenke6466f62003-06-05 19:27:42 +0000296 }
297
298 if (!NetArpWaitTxPacket) {
299 NetArpWaitTxPacket = &NetArpWaitPacketBuf[0] + (PKTALIGN - 1);
300 NetArpWaitTxPacket -= (ulong)NetArpWaitTxPacket % PKTALIGN;
301 NetArpWaitTxPacketSize = 0;
wdenk2d966952002-10-31 22:12:35 +0000302 }
303
304 eth_halt();
wdenk05939202004-04-18 17:39:38 +0000305#ifdef CONFIG_NET_MULTI
wdenk145d2c12004-04-15 21:48:45 +0000306 eth_set_current();
wdenk05939202004-04-18 17:39:38 +0000307#endif
wdenkfa66e932005-04-03 14:52:59 +0000308 if (eth_init(bd) < 0) {
309 eth_halt();
wdenk05939202004-04-18 17:39:38 +0000310 return(-1);
wdenkfa66e932005-04-03 14:52:59 +0000311 }
wdenk2d966952002-10-31 22:12:35 +0000312
313restart:
314#ifdef CONFIG_NET_MULTI
315 memcpy (NetOurEther, eth_get_dev()->enetaddr, 6);
316#else
317 memcpy (NetOurEther, bd->bi_enetaddr, 6);
318#endif
319
320 NetState = NETLOOP_CONTINUE;
321
322 /*
323 * Start the ball rolling with the given start function. From
324 * here on, this code is a state machine driven by received
325 * packets and timer events.
326 */
327
328 switch (protocol) {
wdenkbe9c1cb2004-02-24 02:00:03 +0000329#if (CONFIG_COMMANDS & CFG_CMD_NFS)
330 case NFS:
331#endif
wdenke6466f62003-06-05 19:27:42 +0000332#if (CONFIG_COMMANDS & CFG_CMD_PING)
333 case PING:
334#endif
wdenkb4ad9622005-04-01 00:25:43 +0000335#if (CONFIG_COMMANDS & CFG_CMD_SNTP)
336 case SNTP:
337#endif
wdenkb8fb6192004-08-02 21:11:11 +0000338 case NETCONS:
wdenk2d966952002-10-31 22:12:35 +0000339 case TFTP:
340 NetCopyIP(&NetOurIP, &bd->bi_ip_addr);
wdenk2d966952002-10-31 22:12:35 +0000341 NetOurGatewayIP = getenv_IPaddr ("gatewayip");
342 NetOurSubnetMask= getenv_IPaddr ("netmask");
wdenk145d2c12004-04-15 21:48:45 +0000343 NetOurVLAN = getenv_VLAN("vlan");
344 NetOurNativeVLAN = getenv_VLAN("nvlan");
wdenke6466f62003-06-05 19:27:42 +0000345
346 switch (protocol) {
wdenkbe9c1cb2004-02-24 02:00:03 +0000347#if (CONFIG_COMMANDS & CFG_CMD_NFS)
348 case NFS:
349#endif
wdenkb8fb6192004-08-02 21:11:11 +0000350 case NETCONS:
wdenke6466f62003-06-05 19:27:42 +0000351 case TFTP:
352 NetServerIP = getenv_IPaddr ("serverip");
353 break;
354#if (CONFIG_COMMANDS & CFG_CMD_PING)
355 case PING:
356 /* nothing */
357 break;
358#endif
wdenkb4ad9622005-04-01 00:25:43 +0000359#if (CONFIG_COMMANDS & CFG_CMD_SNTP)
360 case SNTP:
361 /* nothing */
362 break;
363#endif
wdenke6466f62003-06-05 19:27:42 +0000364 default:
365 break;
366 }
367
wdenk2d966952002-10-31 22:12:35 +0000368 break;
369 case BOOTP:
370 case RARP:
371 /*
wdenk57b2d802003-06-27 21:31:46 +0000372 * initialize our IP addr to 0 in order to accept ANY
373 * IP addr assigned to us by the BOOTP / RARP server
wdenk2d966952002-10-31 22:12:35 +0000374 */
375 NetOurIP = 0;
wdenk4989f872004-03-14 15:06:13 +0000376 NetServerIP = getenv_IPaddr ("serverip");
Wolfgang Denk30b87322005-08-12 23:43:12 +0200377 NetOurVLAN = getenv_VLAN("vlan"); /* VLANs must be read */
378 NetOurNativeVLAN = getenv_VLAN("nvlan");
379 case CDP:
380 NetOurVLAN = getenv_VLAN("vlan"); /* VLANs must be read */
381 NetOurNativeVLAN = getenv_VLAN("nvlan");
wdenk2d966952002-10-31 22:12:35 +0000382 break;
383 default:
384 break;
385 }
386
387 switch (net_check_prereq (protocol)) {
388 case 1:
389 /* network not configured */
wdenkfa66e932005-04-03 14:52:59 +0000390 eth_halt();
wdenk2582f6b2002-11-11 21:14:20 +0000391 return (-1);
wdenk2d966952002-10-31 22:12:35 +0000392
393#ifdef CONFIG_NET_MULTI
394 case 2:
395 /* network device not configured */
396 break;
397#endif /* CONFIG_NET_MULTI */
398
399 case 0:
400#ifdef CONFIG_NET_MULTI
401 NetDevExists = 1;
402#endif
403 switch (protocol) {
404 case TFTP:
405 /* always use ARP to get server ethernet address */
wdenke6466f62003-06-05 19:27:42 +0000406 TftpStart();
wdenk2d966952002-10-31 22:12:35 +0000407 break;
408
409#if (CONFIG_COMMANDS & CFG_CMD_DHCP)
410 case DHCP:
411 /* Start with a clean slate... */
wdenkc3919532004-10-11 22:51:13 +0000412 BootpTry = 0;
wdenk2d966952002-10-31 22:12:35 +0000413 NetOurIP = 0;
wdenk4989f872004-03-14 15:06:13 +0000414 NetServerIP = getenv_IPaddr ("serverip");
wdenk2d966952002-10-31 22:12:35 +0000415 DhcpRequest(); /* Basically same as BOOTP */
416 break;
417#endif /* CFG_CMD_DHCP */
418
419 case BOOTP:
420 BootpTry = 0;
421 BootpRequest ();
422 break;
423
424 case RARP:
425 RarpTry = 0;
426 RarpRequest ();
427 break;
wdenke6466f62003-06-05 19:27:42 +0000428#if (CONFIG_COMMANDS & CFG_CMD_PING)
429 case PING:
430 PingStart();
431 break;
432#endif
wdenkbe9c1cb2004-02-24 02:00:03 +0000433#if (CONFIG_COMMANDS & CFG_CMD_NFS)
434 case NFS:
435 NfsStart();
436 break;
437#endif
wdenk145d2c12004-04-15 21:48:45 +0000438#if (CONFIG_COMMANDS & CFG_CMD_CDP)
439 case CDP:
440 CDPStart();
441 break;
442#endif
wdenkb8fb6192004-08-02 21:11:11 +0000443#ifdef CONFIG_NETCONSOLE
444 case NETCONS:
445 NcStart();
446 break;
447#endif
wdenkb4ad9622005-04-01 00:25:43 +0000448#if (CONFIG_COMMANDS & CFG_CMD_SNTP)
449 case SNTP:
450 SntpStart();
451 break;
452#endif
wdenk2d966952002-10-31 22:12:35 +0000453 default:
454 break;
455 }
456
457 NetBootFileXferSize = 0;
458 break;
459 }
460
wdenk49c3f672003-10-08 22:33:00 +0000461#if defined(CONFIG_MII) || (CONFIG_COMMANDS & CFG_CMD_MII)
462#if defined(CFG_FAULT_ECHO_LINK_DOWN) && defined(CONFIG_STATUS_LED) && defined(STATUS_LED_RED)
463 /*
wdenk9c53f402003-10-15 23:53:47 +0000464 * Echo the inverted link state to the fault LED.
wdenk49c3f672003-10-08 22:33:00 +0000465 */
Marian Balakowiczaab8c492005-10-28 22:30:33 +0200466 if(miiphy_link(eth_get_dev()->name, CFG_FAULT_MII_ADDR)) {
wdenk49c3f672003-10-08 22:33:00 +0000467 status_led_set (STATUS_LED_RED, STATUS_LED_OFF);
468 } else {
469 status_led_set (STATUS_LED_RED, STATUS_LED_ON);
470 }
471#endif /* CFG_FAULT_ECHO_LINK_DOWN, ... */
472#endif /* CONFIG_MII, ... */
wdenk2d966952002-10-31 22:12:35 +0000473
474 /*
475 * Main packet reception loop. Loop receiving packets until
wdenk27fa5852005-04-03 14:18:51 +0000476 * someone sets `NetState' to a state that terminates.
wdenk2d966952002-10-31 22:12:35 +0000477 */
478 for (;;) {
479 WATCHDOG_RESET();
480#ifdef CONFIG_SHOW_ACTIVITY
481 {
482 extern void show_activity(int arg);
483 show_activity(1);
484 }
485#endif
486 /*
487 * Check the ethernet for a new packet. The ethernet
488 * receive routine will process it.
489 */
490 eth_rx();
491
492 /*
493 * Abort if ctrl-c was pressed.
494 */
495 if (ctrlc()) {
wdenk57b2d802003-06-27 21:31:46 +0000496 eth_halt();
wdenk42c05472004-03-23 22:14:11 +0000497 puts ("\nAbort\n");
wdenk2582f6b2002-11-11 21:14:20 +0000498 return (-1);
wdenk2d966952002-10-31 22:12:35 +0000499 }
500
wdenke6466f62003-06-05 19:27:42 +0000501 ArpTimeoutCheck();
wdenk2d966952002-10-31 22:12:35 +0000502
503 /*
504 * Check for a timeout, and run the timeout handler
505 * if we have one.
506 */
wdenk5d841732003-08-17 18:55:18 +0000507 if (timeHandler && ((get_timer(0) - timeStart) > timeDelta)) {
wdenk2d966952002-10-31 22:12:35 +0000508 thand_f *x;
509
wdenk49c3f672003-10-08 22:33:00 +0000510#if defined(CONFIG_MII) || (CONFIG_COMMANDS & CFG_CMD_MII)
wdenk6148e742005-04-03 20:55:38 +0000511# if defined(CFG_FAULT_ECHO_LINK_DOWN) && \
512 defined(CONFIG_STATUS_LED) && \
wdenk27fa5852005-04-03 14:18:51 +0000513 defined(STATUS_LED_RED)
wdenk49c3f672003-10-08 22:33:00 +0000514 /*
wdenk9c53f402003-10-15 23:53:47 +0000515 * Echo the inverted link state to the fault LED.
wdenk49c3f672003-10-08 22:33:00 +0000516 */
Marian Balakowiczaab8c492005-10-28 22:30:33 +0200517 if(miiphy_link(eth_get_dev()->name, CFG_FAULT_MII_ADDR)) {
wdenk49c3f672003-10-08 22:33:00 +0000518 status_led_set (STATUS_LED_RED, STATUS_LED_OFF);
519 } else {
520 status_led_set (STATUS_LED_RED, STATUS_LED_ON);
521 }
wdenk27fa5852005-04-03 14:18:51 +0000522# endif /* CFG_FAULT_ECHO_LINK_DOWN, ... */
wdenk49c3f672003-10-08 22:33:00 +0000523#endif /* CONFIG_MII, ... */
wdenk2d966952002-10-31 22:12:35 +0000524 x = timeHandler;
525 timeHandler = (thand_f *)0;
526 (*x)();
527 }
528
529
530 switch (NetState) {
531
532 case NETLOOP_RESTART:
533#ifdef CONFIG_NET_MULTI
534 NetRestarted = 1;
535#endif
536 goto restart;
537
538 case NETLOOP_SUCCESS:
539 if (NetBootFileXferSize > 0) {
540 char buf[10];
541 printf("Bytes transferred = %ld (%lx hex)\n",
542 NetBootFileXferSize,
543 NetBootFileXferSize);
544 sprintf(buf, "%lx", NetBootFileXferSize);
545 setenv("filesize", buf);
wdenk145d2c12004-04-15 21:48:45 +0000546
547 sprintf(buf, "%lX", (unsigned long)load_addr);
548 setenv("fileaddr", buf);
wdenk2d966952002-10-31 22:12:35 +0000549 }
550 eth_halt();
551 return NetBootFileXferSize;
552
553 case NETLOOP_FAIL:
wdenk2582f6b2002-11-11 21:14:20 +0000554 return (-1);
wdenk2d966952002-10-31 22:12:35 +0000555 }
556 }
557}
558
559/**********************************************************************/
560
561static void
562startAgainTimeout(void)
563{
564 NetState = NETLOOP_RESTART;
565}
566
567static void
568startAgainHandler(uchar * pkt, unsigned dest, unsigned src, unsigned len)
569{
570 /* Totally ignore the packet */
571}
572
wdenk05939202004-04-18 17:39:38 +0000573void NetStartAgain (void)
wdenk2d966952002-10-31 22:12:35 +0000574{
wdenk05939202004-04-18 17:39:38 +0000575 char *nretry;
576 int noretry = 0, once = 0;
wdenk145d2c12004-04-15 21:48:45 +0000577
wdenk05939202004-04-18 17:39:38 +0000578 if ((nretry = getenv ("netretry")) != NULL) {
579 noretry = (strcmp (nretry, "no") == 0);
580 once = (strcmp (nretry, "once") == 0);
581 }
582 if (noretry) {
583 eth_halt ();
wdenk145d2c12004-04-15 21:48:45 +0000584 NetState = NETLOOP_FAIL;
585 return;
586 }
wdenk2d966952002-10-31 22:12:35 +0000587#ifndef CONFIG_NET_MULTI
wdenk05939202004-04-18 17:39:38 +0000588 NetSetTimeout (10 * CFG_HZ, startAgainTimeout);
589 NetSetHandler (startAgainHandler);
590#else /* !CONFIG_NET_MULTI*/
591 eth_halt ();
592 eth_try_another (!NetRestarted);
593 eth_init (gd->bd);
594 if (NetRestartWrap) {
wdenk2d966952002-10-31 22:12:35 +0000595 NetRestartWrap = 0;
wdenk05939202004-04-18 17:39:38 +0000596 if (NetDevExists && !once) {
597 NetSetTimeout (10 * CFG_HZ, startAgainTimeout);
598 NetSetHandler (startAgainHandler);
599 } else {
wdenk2d966952002-10-31 22:12:35 +0000600 NetState = NETLOOP_FAIL;
601 }
wdenk05939202004-04-18 17:39:38 +0000602 } else {
wdenk2d966952002-10-31 22:12:35 +0000603 NetState = NETLOOP_RESTART;
604 }
wdenk05939202004-04-18 17:39:38 +0000605#endif /* CONFIG_NET_MULTI */
wdenk2d966952002-10-31 22:12:35 +0000606}
607
608/**********************************************************************/
609/*
610 * Miscelaneous bits.
611 */
612
613void
614NetSetHandler(rxhand_f * f)
615{
616 packetHandler = f;
617}
618
619
620void
wdenkd6a3dd42004-10-09 21:56:21 +0000621NetSetTimeout(ulong iv, thand_f * f)
wdenk2d966952002-10-31 22:12:35 +0000622{
623 if (iv == 0) {
624 timeHandler = (thand_f *)0;
625 } else {
626 timeHandler = f;
wdenk5d841732003-08-17 18:55:18 +0000627 timeStart = get_timer(0);
628 timeDelta = iv;
wdenk2d966952002-10-31 22:12:35 +0000629 }
630}
631
632
633void
634NetSendPacket(volatile uchar * pkt, int len)
635{
636 (void) eth_send(pkt, len);
637}
638
wdenke6466f62003-06-05 19:27:42 +0000639int
640NetSendUDPPacket(uchar *ether, IPaddr_t dest, int dport, int sport, int len)
641{
wdenk145d2c12004-04-15 21:48:45 +0000642 uchar *pkt;
643
wdenke6466f62003-06-05 19:27:42 +0000644 /* convert to new style broadcast */
645 if (dest == 0)
646 dest = 0xFFFFFFFF;
647
648 /* if broadcast, make the ether address a broadcast and don't do ARP */
649 if (dest == 0xFFFFFFFF)
650 ether = NetBcastAddr;
651
652 /* if MAC address was not discovered yet, save the packet and do an ARP request */
653 if (memcmp(ether, NetEtherNullAddr, 6) == 0) {
654
655#ifdef ET_DEBUG
656 printf("sending ARP for %08lx\n", dest);
657#endif
wdenke6466f62003-06-05 19:27:42 +0000658 NetArpWaitPacketIP = dest;
659 NetArpWaitPacketMAC = ether;
wdenk145d2c12004-04-15 21:48:45 +0000660
661 pkt = NetArpWaitTxPacket;
662 pkt += NetSetEther (pkt, NetArpWaitPacketMAC, PROT_IP);
663
664 NetSetIP (pkt, dest, dport, sport, len);
665 memcpy(pkt + IP_HDR_SIZE, (uchar *)NetTxPacket + (pkt - (uchar *)NetArpWaitTxPacket) + IP_HDR_SIZE, len);
wdenke6466f62003-06-05 19:27:42 +0000666
667 /* size of the waiting packet */
wdenk145d2c12004-04-15 21:48:45 +0000668 NetArpWaitTxPacketSize = (pkt - NetArpWaitTxPacket) + IP_HDR_SIZE + len;
wdenke6466f62003-06-05 19:27:42 +0000669
670 /* and do the ARP request */
671 NetArpWaitTry = 1;
672 NetArpWaitTimerStart = get_timer(0);
673 ArpRequest();
674 return 1; /* waiting */
675 }
676
677#ifdef ET_DEBUG
678 printf("sending UDP to %08lx/%02x:%02x:%02x:%02x:%02x:%02x\n",
wdenk05939202004-04-18 17:39:38 +0000679 dest, ether[0], ether[1], ether[2], ether[3], ether[4], ether[5]);
wdenke6466f62003-06-05 19:27:42 +0000680#endif
681
wdenk145d2c12004-04-15 21:48:45 +0000682 pkt = (uchar *)NetTxPacket;
683 pkt += NetSetEther (pkt, ether, PROT_IP);
684 NetSetIP (pkt, dest, dport, sport, len);
685 (void) eth_send(NetTxPacket, (pkt - NetTxPacket) + IP_HDR_SIZE + len);
wdenke6466f62003-06-05 19:27:42 +0000686
wdenk05939202004-04-18 17:39:38 +0000687 return 0; /* transmitted */
wdenke6466f62003-06-05 19:27:42 +0000688}
689
690#if (CONFIG_COMMANDS & CFG_CMD_PING)
691static ushort PingSeqNo;
692
693int PingSend(void)
694{
695 static uchar mac[6];
696 volatile IP_t *ip;
697 volatile ushort *s;
wdenk145d2c12004-04-15 21:48:45 +0000698 uchar *pkt;
wdenke6466f62003-06-05 19:27:42 +0000699
700 /* XXX always send arp request */
701
702 memcpy(mac, NetEtherNullAddr, 6);
703
704#ifdef ET_DEBUG
705 printf("sending ARP for %08lx\n", NetPingIP);
706#endif
707
708 NetArpWaitPacketIP = NetPingIP;
709 NetArpWaitPacketMAC = mac;
710
wdenk145d2c12004-04-15 21:48:45 +0000711 pkt = NetArpWaitTxPacket;
712 pkt += NetSetEther(pkt, mac, PROT_IP);
wdenke6466f62003-06-05 19:27:42 +0000713
wdenk145d2c12004-04-15 21:48:45 +0000714 ip = (volatile IP_t *)pkt;
wdenke6466f62003-06-05 19:27:42 +0000715
716 /*
717 * Construct an IP and ICMP header. (need to set no fragment bit - XXX)
718 */
719 ip->ip_hl_v = 0x45; /* IP_HDR_SIZE / 4 (not including UDP) */
720 ip->ip_tos = 0;
721 ip->ip_len = htons(IP_HDR_SIZE_NO_UDP + 8);
722 ip->ip_id = htons(NetIPID++);
723 ip->ip_off = htons(0x4000); /* No fragmentation */
724 ip->ip_ttl = 255;
725 ip->ip_p = 0x01; /* ICMP */
726 ip->ip_sum = 0;
727 NetCopyIP((void*)&ip->ip_src, &NetOurIP); /* already in network byte order */
728 NetCopyIP((void*)&ip->ip_dst, &NetPingIP); /* - "" - */
729 ip->ip_sum = ~NetCksum((uchar *)ip, IP_HDR_SIZE_NO_UDP / 2);
730
731 s = &ip->udp_src; /* XXX ICMP starts here */
732 s[0] = htons(0x0800); /* echo-request, code */
733 s[1] = 0; /* checksum */
734 s[2] = 0; /* identifier */
735 s[3] = htons(PingSeqNo++); /* sequence number */
736 s[1] = ~NetCksum((uchar *)s, 8/2);
737
738 /* size of the waiting packet */
wdenk145d2c12004-04-15 21:48:45 +0000739 NetArpWaitTxPacketSize = (pkt - NetArpWaitTxPacket) + IP_HDR_SIZE_NO_UDP + 8;
wdenke6466f62003-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
748static void
749PingTimeout (void)
750{
751 eth_halt();
752 NetState = NETLOOP_FAIL; /* we did not get the reply */
753}
754
755static void
756PingHandler (uchar * pkt, unsigned dest, unsigned src, unsigned len)
757{
758 IPaddr_t tmp;
759 volatile IP_t *ip = (volatile IP_t *)pkt;
760
761 tmp = NetReadIP((void *)&ip->ip_src);
762 if (tmp != NetPingIP)
763 return;
764
765 NetState = NETLOOP_SUCCESS;
766}
767
768static void PingStart(void)
769{
wdenk145d2c12004-04-15 21:48:45 +0000770#if defined(CONFIG_NET_MULTI)
771 printf ("Using %s device\n", eth_get_name());
wdenk05939202004-04-18 17:39:38 +0000772#endif /* CONFIG_NET_MULTI */
wdenke6466f62003-06-05 19:27:42 +0000773 NetSetTimeout (10 * CFG_HZ, PingTimeout);
774 NetSetHandler (PingHandler);
775
776 PingSend();
777}
wdenk05939202004-04-18 17:39:38 +0000778#endif /* CFG_CMD_PING */
wdenk145d2c12004-04-15 21:48:45 +0000779
780#if (CONFIG_COMMANDS & CFG_CMD_CDP)
781
782#define CDP_DEVICE_ID_TLV 0x0001
783#define CDP_ADDRESS_TLV 0x0002
784#define CDP_PORT_ID_TLV 0x0003
785#define CDP_CAPABILITIES_TLV 0x0004
786#define CDP_VERSION_TLV 0x0005
787#define CDP_PLATFORM_TLV 0x0006
788#define CDP_NATIVE_VLAN_TLV 0x000a
789#define CDP_APPLIANCE_VLAN_TLV 0x000e
790#define CDP_TRIGGER_TLV 0x000f
791#define CDP_POWER_CONSUMPTION_TLV 0x0010
792#define CDP_SYSNAME_TLV 0x0014
793#define CDP_SYSOBJECT_TLV 0x0015
794#define CDP_MANAGEMENT_ADDRESS_TLV 0x0016
795
796#define CDP_TIMEOUT (CFG_HZ/4) /* one packet every 250ms */
797
798static int CDPSeq;
799static int CDPOK;
800
801ushort CDPNativeVLAN;
802ushort CDPApplianceVLAN;
803
804static const uchar CDP_SNAP_hdr[8] = { 0xAA, 0xAA, 0x03, 0x00, 0x00, 0x0C, 0x20, 0x00 };
805
806static ushort CDP_compute_csum(const uchar *buff, ushort len)
807{
808 ushort csum;
809 int odd;
810 ulong result = 0;
811 ushort leftover;
Wolfgang Denk7fb52662005-10-13 16:45:02 +0200812 ushort *p;
wdenk145d2c12004-04-15 21:48:45 +0000813
814 if (len > 0) {
815 odd = 1 & (ulong)buff;
816 if (odd) {
817 result = *buff << 8;
818 len--;
819 buff++;
820 }
821 while (len > 1) {
Wolfgang Denk7fb52662005-10-13 16:45:02 +0200822 p = (ushort *)buff;
823 result += *p++;
824 buff = (uchar *)p;
wdenk145d2c12004-04-15 21:48:45 +0000825 if (result & 0x80000000)
826 result = (result & 0xFFFF) + (result >> 16);
827 len -= 2;
828 }
829 if (len) {
830 leftover = (signed short)(*(const signed char *)buff);
Wolfgang Denkf0711132005-11-10 20:59:46 +0100831 /* CISCO SUCKS big time! (and blows too):
832 * CDP uses the IP checksum algorithm with a twist;
833 * for the last byte it *sign* extends and sums.
834 */
wdenk145d2c12004-04-15 21:48:45 +0000835 result = (result & 0xffff0000) | ((result + leftover) & 0x0000ffff);
836 }
837 while (result >> 16)
838 result = (result & 0xFFFF) + (result >> 16);
839
840 if (odd)
841 result = ((result >> 8) & 0xff) | ((result & 0xff) << 8);
842 }
843
844 /* add up 16-bit and 17-bit words for 17+c bits */
845 result = (result & 0xffff) + (result >> 16);
846 /* add up 16-bit and 2-bit for 16+c bit */
847 result = (result & 0xffff) + (result >> 16);
848 /* add up carry.. */
849 result = (result & 0xffff) + (result >> 16);
850
851 /* negate */
852 csum = ~(ushort)result;
853
854 /* run time endian detection */
855 if (csum != htons(csum)) /* little endian */
856 csum = htons(csum);
857
858 return csum;
859}
860
861int CDPSendTrigger(void)
862{
863 volatile uchar *pkt;
864 volatile ushort *s;
865 volatile ushort *cp;
866 Ethernet_t *et;
wdenk145d2c12004-04-15 21:48:45 +0000867 int len;
868 ushort chksum;
wdenk05939202004-04-18 17:39:38 +0000869#if defined(CONFIG_CDP_DEVICE_ID) || defined(CONFIG_CDP_PORT_ID) || \
870 defined(CONFIG_CDP_VERSION) || defined(CONFIG_CDP_PLATFORM)
871 char buf[32];
872#endif
wdenk145d2c12004-04-15 21:48:45 +0000873
874 pkt = NetTxPacket;
875 et = (Ethernet_t *)pkt;
876
877 /* NOTE: trigger sent not on any VLAN */
878
879 /* form ethernet header */
880 memcpy(et->et_dest, NetCDPAddr, 6);
881 memcpy(et->et_src, NetOurEther, 6);
882
883 pkt += ETHER_HDR_SIZE;
884
885 /* SNAP header */
886 memcpy((uchar *)pkt, CDP_SNAP_hdr, sizeof(CDP_SNAP_hdr));
887 pkt += sizeof(CDP_SNAP_hdr);
888
889 /* CDP header */
890 *pkt++ = 0x02; /* CDP version 2 */
891 *pkt++ = 180; /* TTL */
892 s = (volatile ushort *)pkt;
893 cp = s;
894 *s++ = htons(0); /* checksum (0 for later calculation) */
895
896 /* CDP fields */
897#ifdef CONFIG_CDP_DEVICE_ID
898 *s++ = htons(CDP_DEVICE_ID_TLV);
899 *s++ = htons(CONFIG_CDP_DEVICE_ID);
900 memset(buf, 0, sizeof(buf));
901 sprintf(buf, CONFIG_CDP_DEVICE_ID_PREFIX "%02X%02X%02X%02X%02X%02X",
902 NetOurEther[0] & 0xff, NetOurEther[1] & 0xff,
903 NetOurEther[2] & 0xff, NetOurEther[3] & 0xff,
904 NetOurEther[4] & 0xff, NetOurEther[5] & 0xff);
905 memcpy((uchar *)s, buf, 16);
906 s += 16 / 2;
907#endif
908
909#ifdef CONFIG_CDP_PORT_ID
910 *s++ = htons(CDP_PORT_ID_TLV);
911 memset(buf, 0, sizeof(buf));
912 sprintf(buf, CONFIG_CDP_PORT_ID, eth_get_dev_index());
913 len = strlen(buf);
914 if (len & 1) /* make it even */
915 len++;
916 *s++ = htons(len + 4);
917 memcpy((uchar *)s, buf, len);
918 s += len / 2;
919#endif
920
921#ifdef CONFIG_CDP_CAPABILITIES
922 *s++ = htons(CDP_CAPABILITIES_TLV);
923 *s++ = htons(8);
924 *(ulong *)s = htonl(CONFIG_CDP_CAPABILITIES);
925 s += 2;
926#endif
927
928#ifdef CONFIG_CDP_VERSION
929 *s++ = htons(CDP_VERSION_TLV);
930 memset(buf, 0, sizeof(buf));
931 strcpy(buf, CONFIG_CDP_VERSION);
932 len = strlen(buf);
933 if (len & 1) /* make it even */
934 len++;
935 *s++ = htons(len + 4);
936 memcpy((uchar *)s, buf, len);
937 s += len / 2;
938#endif
939
940#ifdef CONFIG_CDP_PLATFORM
941 *s++ = htons(CDP_PLATFORM_TLV);
942 memset(buf, 0, sizeof(buf));
943 strcpy(buf, CONFIG_CDP_PLATFORM);
944 len = strlen(buf);
945 if (len & 1) /* make it even */
946 len++;
947 *s++ = htons(len + 4);
948 memcpy((uchar *)s, buf, len);
949 s += len / 2;
950#endif
951
952#ifdef CONFIG_CDP_TRIGGER
953 *s++ = htons(CDP_TRIGGER_TLV);
954 *s++ = htons(8);
955 *(ulong *)s = htonl(CONFIG_CDP_TRIGGER);
956 s += 2;
957#endif
958
959#ifdef CONFIG_CDP_POWER_CONSUMPTION
960 *s++ = htons(CDP_POWER_CONSUMPTION_TLV);
961 *s++ = htons(6);
962 *s++ = htons(CONFIG_CDP_POWER_CONSUMPTION);
963#endif
964
965 /* length of ethernet packet */
966 len = (uchar *)s - ((uchar *)NetTxPacket + ETHER_HDR_SIZE);
967 et->et_protlen = htons(len);
968
969 len = ETHER_HDR_SIZE + sizeof(CDP_SNAP_hdr);
970 chksum = CDP_compute_csum((uchar *)NetTxPacket + len, (uchar *)s - (NetTxPacket + len));
971 if (chksum == 0)
972 chksum = 0xFFFF;
973 *cp = htons(chksum);
974
975 (void) eth_send(NetTxPacket, (uchar *)s - NetTxPacket);
976 return 0;
977}
978
979static void
980CDPTimeout (void)
981{
982 CDPSeq++;
983
984 if (CDPSeq < 3) {
985 NetSetTimeout (CDP_TIMEOUT, CDPTimeout);
986 CDPSendTrigger();
987 return;
988 }
989
990 /* if not OK try again */
991 if (!CDPOK)
992 NetStartAgain();
993 else
994 NetState = NETLOOP_SUCCESS;
995}
996
997static void
998CDPDummyHandler (uchar * pkt, unsigned dest, unsigned src, unsigned len)
999{
1000 /* nothing */
1001}
1002
1003static void
1004CDPHandler(const uchar * pkt, unsigned len)
1005{
1006 const uchar *t;
1007 const ushort *ss;
1008 ushort type, tlen;
1009 uchar applid;
1010 ushort vlan, nvlan;
1011
1012 /* minimum size? */
1013 if (len < sizeof(CDP_SNAP_hdr) + 4)
1014 goto pkt_short;
1015
1016 /* check for valid CDP SNAP header */
1017 if (memcmp(pkt, CDP_SNAP_hdr, sizeof(CDP_SNAP_hdr)) != 0)
1018 return;
1019
1020 pkt += sizeof(CDP_SNAP_hdr);
1021 len -= sizeof(CDP_SNAP_hdr);
1022
1023 /* Version of CDP protocol must be >= 2 and TTL != 0 */
1024 if (pkt[0] < 0x02 || pkt[1] == 0)
1025 return;
1026
1027 /* if version is greater than 0x02 maybe we'll have a problem; output a warning */
1028 if (pkt[0] != 0x02)
1029 printf("** WARNING: CDP packet received with a protocol version %d > 2\n",
1030 pkt[0] & 0xff);
1031
1032 if (CDP_compute_csum(pkt, len) != 0)
1033 return;
1034
1035 pkt += 4;
1036 len -= 4;
1037
1038 vlan = htons(-1);
1039 nvlan = htons(-1);
1040 while (len > 0) {
1041 if (len < 4)
1042 goto pkt_short;
1043
1044 ss = (const ushort *)pkt;
1045 type = ntohs(ss[0]);
1046 tlen = ntohs(ss[1]);
1047 if (tlen > len) {
1048 goto pkt_short;
1049 }
1050
1051 pkt += tlen;
1052 len -= tlen;
1053
1054 ss += 2; /* point ss to the data of the TLV */
1055 tlen -= 4;
1056
1057 switch (type) {
1058 case CDP_DEVICE_ID_TLV:
1059 break;
1060 case CDP_ADDRESS_TLV:
1061 break;
1062 case CDP_PORT_ID_TLV:
1063 break;
1064 case CDP_CAPABILITIES_TLV:
1065 break;
1066 case CDP_VERSION_TLV:
1067 break;
1068 case CDP_PLATFORM_TLV:
1069 break;
1070 case CDP_NATIVE_VLAN_TLV:
1071 nvlan = *ss;
1072 break;
1073 case CDP_APPLIANCE_VLAN_TLV:
1074 t = (const uchar *)ss;
1075 while (tlen > 0) {
1076 if (tlen < 3)
1077 goto pkt_short;
1078
1079 applid = t[0];
1080 ss = (const ushort *)(t + 1);
1081
1082#ifdef CONFIG_CDP_APPLIANCE_VLAN_TYPE
1083 if (applid == CONFIG_CDP_APPLIANCE_VLAN_TYPE)
1084 vlan = *ss;
1085#else
1086 vlan = ntohs(*ss); /* XXX will this work; dunno */
1087#endif
1088 t += 3; tlen -= 3;
1089 }
1090 break;
1091 case CDP_TRIGGER_TLV:
1092 break;
1093 case CDP_POWER_CONSUMPTION_TLV:
1094 break;
1095 case CDP_SYSNAME_TLV:
1096 break;
1097 case CDP_SYSOBJECT_TLV:
1098 break;
1099 case CDP_MANAGEMENT_ADDRESS_TLV:
1100 break;
1101 }
1102 }
1103
1104 CDPApplianceVLAN = vlan;
1105 CDPNativeVLAN = nvlan;
1106
1107 CDPOK = 1;
1108 return;
1109
1110 pkt_short:
1111 printf("** CDP packet is too short\n");
1112 return;
1113}
1114
1115static void CDPStart(void)
1116{
1117#if defined(CONFIG_NET_MULTI)
1118 printf ("Using %s device\n", eth_get_name());
wdenke6466f62003-06-05 19:27:42 +00001119#endif
wdenk145d2c12004-04-15 21:48:45 +00001120 CDPSeq = 0;
1121 CDPOK = 0;
1122
1123 CDPNativeVLAN = htons(-1);
1124 CDPApplianceVLAN = htons(-1);
1125
1126 NetSetTimeout (CDP_TIMEOUT, CDPTimeout);
1127 NetSetHandler (CDPDummyHandler);
1128
1129 CDPSendTrigger();
1130}
wdenk05939202004-04-18 17:39:38 +00001131#endif /* CFG_CMD_CDP */
wdenk145d2c12004-04-15 21:48:45 +00001132
wdenk2d966952002-10-31 22:12:35 +00001133
1134void
wdenk145d2c12004-04-15 21:48:45 +00001135NetReceive(volatile uchar * inpkt, int len)
wdenk2d966952002-10-31 22:12:35 +00001136{
1137 Ethernet_t *et;
1138 IP_t *ip;
1139 ARP_t *arp;
1140 IPaddr_t tmp;
1141 int x;
wdenk145d2c12004-04-15 21:48:45 +00001142 uchar *pkt;
1143#if (CONFIG_COMMANDS & CFG_CMD_CDP)
1144 int iscdp;
1145#endif
1146 ushort cti = 0, vlanid = VLAN_NONE, myvlanid, mynvlanid;
wdenk2d966952002-10-31 22:12:35 +00001147
wdenk145d2c12004-04-15 21:48:45 +00001148#ifdef ET_DEBUG
1149 printf("packet received\n");
1150#endif
1151
1152 NetRxPkt = inpkt;
wdenk2d966952002-10-31 22:12:35 +00001153 NetRxPktLen = len;
wdenk145d2c12004-04-15 21:48:45 +00001154 et = (Ethernet_t *)inpkt;
1155
1156 /* too small packet? */
1157 if (len < ETHER_HDR_SIZE)
1158 return;
1159
1160#if (CONFIG_COMMANDS & CFG_CMD_CDP)
1161 /* keep track if packet is CDP */
1162 iscdp = memcmp(et->et_dest, NetCDPAddr, 6) == 0;
1163#endif
1164
1165 myvlanid = ntohs(NetOurVLAN);
1166 if (myvlanid == (ushort)-1)
1167 myvlanid = VLAN_NONE;
1168 mynvlanid = ntohs(NetOurNativeVLAN);
1169 if (mynvlanid == (ushort)-1)
1170 mynvlanid = VLAN_NONE;
wdenk2d966952002-10-31 22:12:35 +00001171
1172 x = ntohs(et->et_protlen);
1173
wdenk145d2c12004-04-15 21:48:45 +00001174#ifdef ET_DEBUG
1175 printf("packet received\n");
1176#endif
1177
wdenk2d966952002-10-31 22:12:35 +00001178 if (x < 1514) {
1179 /*
1180 * Got a 802 packet. Check the other protocol field.
1181 */
1182 x = ntohs(et->et_prot);
wdenk145d2c12004-04-15 21:48:45 +00001183
1184 ip = (IP_t *)(inpkt + E802_HDR_SIZE);
wdenk2d966952002-10-31 22:12:35 +00001185 len -= E802_HDR_SIZE;
wdenk145d2c12004-04-15 21:48:45 +00001186
1187 } else if (x != PROT_VLAN) { /* normal packet */
1188 ip = (IP_t *)(inpkt + ETHER_HDR_SIZE);
wdenk2d966952002-10-31 22:12:35 +00001189 len -= ETHER_HDR_SIZE;
wdenk145d2c12004-04-15 21:48:45 +00001190
1191 } else { /* VLAN packet */
1192 VLAN_Ethernet_t *vet = (VLAN_Ethernet_t *)et;
1193
1194#ifdef ET_DEBUG
1195 printf("VLAN packet received\n");
1196#endif
1197 /* too small packet? */
1198 if (len < VLAN_ETHER_HDR_SIZE)
1199 return;
1200
1201 /* if no VLAN active */
1202 if ((ntohs(NetOurVLAN) & VLAN_IDMASK) == VLAN_NONE
1203#if (CONFIG_COMMANDS & CFG_CMD_CDP)
1204 && iscdp == 0
1205#endif
1206 )
1207 return;
1208
1209 cti = ntohs(vet->vet_tag);
1210 vlanid = cti & VLAN_IDMASK;
1211 x = ntohs(vet->vet_type);
1212
1213 ip = (IP_t *)(inpkt + VLAN_ETHER_HDR_SIZE);
1214 len -= VLAN_ETHER_HDR_SIZE;
wdenk2d966952002-10-31 22:12:35 +00001215 }
1216
1217#ifdef ET_DEBUG
1218 printf("Receive from protocol 0x%x\n", x);
1219#endif
1220
wdenk145d2c12004-04-15 21:48:45 +00001221#if (CONFIG_COMMANDS & CFG_CMD_CDP)
1222 if (iscdp) {
1223 CDPHandler((uchar *)ip, len);
1224 return;
1225 }
1226#endif
1227
1228 if ((myvlanid & VLAN_IDMASK) != VLAN_NONE) {
1229 if (vlanid == VLAN_NONE)
1230 vlanid = (mynvlanid & VLAN_IDMASK);
1231 /* not matched? */
1232 if (vlanid != (myvlanid & VLAN_IDMASK))
1233 return;
1234 }
1235
wdenk2d966952002-10-31 22:12:35 +00001236 switch (x) {
1237
1238 case PROT_ARP:
1239 /*
1240 * We have to deal with two types of ARP packets:
wdenk57b2d802003-06-27 21:31:46 +00001241 * - REQUEST packets will be answered by sending our
1242 * IP address - if we know it.
1243 * - REPLY packates are expected only after we asked
1244 * for the TFTP server's or the gateway's ethernet
1245 * address; so if we receive such a packet, we set
1246 * the server ethernet address
wdenk2d966952002-10-31 22:12:35 +00001247 */
1248#ifdef ET_DEBUG
wdenk42c05472004-03-23 22:14:11 +00001249 puts ("Got ARP\n");
wdenk2d966952002-10-31 22:12:35 +00001250#endif
1251 arp = (ARP_t *)ip;
1252 if (len < ARP_HDR_SIZE) {
1253 printf("bad length %d < %d\n", len, ARP_HDR_SIZE);
1254 return;
1255 }
1256 if (ntohs(arp->ar_hrd) != ARP_ETHER) {
1257 return;
1258 }
1259 if (ntohs(arp->ar_pro) != PROT_IP) {
1260 return;
1261 }
1262 if (arp->ar_hln != 6) {
1263 return;
1264 }
1265 if (arp->ar_pln != 4) {
1266 return;
1267 }
1268
1269 if (NetOurIP == 0) {
1270 return;
1271 }
1272
1273 if (NetReadIP(&arp->ar_data[16]) != NetOurIP) {
1274 return;
1275 }
1276
1277 switch (ntohs(arp->ar_op)) {
1278 case ARPOP_REQUEST: /* reply with our IP address */
1279#ifdef ET_DEBUG
wdenk42c05472004-03-23 22:14:11 +00001280 puts ("Got ARP REQUEST, return our IP\n");
wdenk2d966952002-10-31 22:12:35 +00001281#endif
wdenk145d2c12004-04-15 21:48:45 +00001282 pkt = (uchar *)et;
1283 pkt += NetSetEther(pkt, et->et_src, PROT_ARP);
wdenk2d966952002-10-31 22:12:35 +00001284 arp->ar_op = htons(ARPOP_REPLY);
1285 memcpy (&arp->ar_data[10], &arp->ar_data[0], 6);
1286 NetCopyIP(&arp->ar_data[16], &arp->ar_data[6]);
1287 memcpy (&arp->ar_data[ 0], NetOurEther, 6);
1288 NetCopyIP(&arp->ar_data[ 6], &NetOurIP);
wdenk145d2c12004-04-15 21:48:45 +00001289 (void) eth_send((uchar *)et, (pkt - (uchar *)et) + ARP_HDR_SIZE);
wdenk2d966952002-10-31 22:12:35 +00001290 return;
wdenke6466f62003-06-05 19:27:42 +00001291
1292 case ARPOP_REPLY: /* arp reply */
1293 /* are we waiting for a reply */
1294 if (!NetArpWaitPacketIP || !NetArpWaitPacketMAC)
1295 break;
1296#ifdef ET_DEBUG
1297 printf("Got ARP REPLY, set server/gtwy eth addr (%02x:%02x:%02x:%02x:%02x:%02x)\n",
1298 arp->ar_data[0], arp->ar_data[1],
1299 arp->ar_data[2], arp->ar_data[3],
1300 arp->ar_data[4], arp->ar_data[5]);
1301#endif
1302
1303 tmp = NetReadIP(&arp->ar_data[6]);
1304
1305 /* matched waiting packet's address */
1306 if (tmp == NetArpWaitReplyIP) {
wdenk2d966952002-10-31 22:12:35 +00001307#ifdef ET_DEBUG
wdenk42c05472004-03-23 22:14:11 +00001308 puts ("Got it\n");
wdenk2d966952002-10-31 22:12:35 +00001309#endif
wdenke6466f62003-06-05 19:27:42 +00001310 /* save address for later use */
1311 memcpy(NetArpWaitPacketMAC, &arp->ar_data[0], 6);
1312
wdenkb8fb6192004-08-02 21:11:11 +00001313#ifdef CONFIG_NETCONSOLE
1314 (*packetHandler)(0,0,0,0);
1315#endif
wdenke6466f62003-06-05 19:27:42 +00001316 /* modify header, and transmit it */
1317 memcpy(((Ethernet_t *)NetArpWaitTxPacket)->et_dest, NetArpWaitPacketMAC, 6);
1318 (void) eth_send(NetArpWaitTxPacket, NetArpWaitTxPacketSize);
1319
1320 /* no arp request pending now */
1321 NetArpWaitPacketIP = 0;
1322 NetArpWaitTxPacketSize = 0;
1323 NetArpWaitPacketMAC = NULL;
1324
1325 }
wdenk2d966952002-10-31 22:12:35 +00001326 return;
1327 default:
1328#ifdef ET_DEBUG
1329 printf("Unexpected ARP opcode 0x%x\n", ntohs(arp->ar_op));
1330#endif
1331 return;
1332 }
wdenkcb99da52005-01-12 00:15:14 +00001333 break;
wdenk2d966952002-10-31 22:12:35 +00001334
1335 case PROT_RARP:
1336#ifdef ET_DEBUG
wdenk42c05472004-03-23 22:14:11 +00001337 puts ("Got RARP\n");
wdenk2d966952002-10-31 22:12:35 +00001338#endif
1339 arp = (ARP_t *)ip;
1340 if (len < ARP_HDR_SIZE) {
1341 printf("bad length %d < %d\n", len, ARP_HDR_SIZE);
1342 return;
1343 }
1344
1345 if ((ntohs(arp->ar_op) != RARPOP_REPLY) ||
1346 (ntohs(arp->ar_hrd) != ARP_ETHER) ||
1347 (ntohs(arp->ar_pro) != PROT_IP) ||
1348 (arp->ar_hln != 6) || (arp->ar_pln != 4)) {
1349
wdenk42c05472004-03-23 22:14:11 +00001350 puts ("invalid RARP header\n");
wdenk2d966952002-10-31 22:12:35 +00001351 } else {
1352 NetCopyIP(&NetOurIP, &arp->ar_data[16]);
wdenk4989f872004-03-14 15:06:13 +00001353 if (NetServerIP == 0)
1354 NetCopyIP(&NetServerIP, &arp->ar_data[ 6]);
wdenk2d966952002-10-31 22:12:35 +00001355 memcpy (NetServerEther, &arp->ar_data[ 0], 6);
1356
1357 (*packetHandler)(0,0,0,0);
1358 }
1359 break;
1360
1361 case PROT_IP:
1362#ifdef ET_DEBUG
wdenk42c05472004-03-23 22:14:11 +00001363 puts ("Got IP\n");
wdenk2d966952002-10-31 22:12:35 +00001364#endif
1365 if (len < IP_HDR_SIZE) {
1366 debug ("len bad %d < %d\n", len, IP_HDR_SIZE);
1367 return;
1368 }
1369 if (len < ntohs(ip->ip_len)) {
1370 printf("len bad %d < %d\n", len, ntohs(ip->ip_len));
1371 return;
1372 }
1373 len = ntohs(ip->ip_len);
1374#ifdef ET_DEBUG
1375 printf("len=%d, v=%02x\n", len, ip->ip_hl_v & 0xff);
1376#endif
1377 if ((ip->ip_hl_v & 0xf0) != 0x40) {
1378 return;
1379 }
1380 if (ip->ip_off & htons(0x1fff)) { /* Can't deal w/ fragments */
1381 return;
1382 }
1383 if (!NetCksumOk((uchar *)ip, IP_HDR_SIZE_NO_UDP / 2)) {
wdenk42c05472004-03-23 22:14:11 +00001384 puts ("checksum bad\n");
wdenk2d966952002-10-31 22:12:35 +00001385 return;
1386 }
1387 tmp = NetReadIP(&ip->ip_dst);
1388 if (NetOurIP && tmp != NetOurIP && tmp != 0xFFFFFFFF) {
1389 return;
1390 }
1391 /*
1392 * watch for ICMP host redirects
1393 *
wdenk57b2d802003-06-27 21:31:46 +00001394 * There is no real handler code (yet). We just watch
1395 * for ICMP host redirect messages. In case anybody
1396 * sees these messages: please contact me
1397 * (wd@denx.de), or - even better - send me the
1398 * necessary fixes :-)
wdenk2d966952002-10-31 22:12:35 +00001399 *
wdenk57b2d802003-06-27 21:31:46 +00001400 * Note: in all cases where I have seen this so far
1401 * it was a problem with the router configuration,
1402 * for instance when a router was configured in the
1403 * BOOTP reply, but the TFTP server was on the same
1404 * subnet. So this is probably a warning that your
1405 * configuration might be wrong. But I'm not really
1406 * sure if there aren't any other situations.
wdenk2d966952002-10-31 22:12:35 +00001407 */
1408 if (ip->ip_p == IPPROTO_ICMP) {
1409 ICMP_t *icmph = (ICMP_t *)&(ip->udp_src);
1410
wdenke6466f62003-06-05 19:27:42 +00001411 switch (icmph->type) {
1412 case ICMP_REDIRECT:
wdenkcbc49a52005-05-03 14:12:25 +00001413 if (icmph->code != ICMP_REDIR_HOST)
1414 return;
1415 puts (" ICMP Host Redirect to ");
1416 print_IPaddr(icmph->un.gateway);
1417 putc(' ');
Stefan Roesedfced812005-08-12 20:06:52 +02001418 return;
wdenke6466f62003-06-05 19:27:42 +00001419#if (CONFIG_COMMANDS & CFG_CMD_PING)
1420 case ICMP_ECHO_REPLY:
1421 /*
1422 * IP header OK. Pass the packet to the current handler.
1423 */
1424 /* XXX point to ip packet */
1425 (*packetHandler)((uchar *)ip, 0, 0, 0);
Stefan Roesedfced812005-08-12 20:06:52 +02001426 return;
Ed Swarthout83109192007-03-07 12:14:50 -06001427 case ICMP_ECHO_REQUEST:
1428#ifdef ET_DEBUG
1429 printf ("Got ICMP ECHO REQUEST, return %d bytes \n",
1430 ETHER_HDR_SIZE + len);
1431#endif
1432 memcpy (&et->et_dest[0], &et->et_src[0], 6);
1433 memcpy (&et->et_src[ 0], NetOurEther, 6);
1434
1435 ip->ip_sum = 0;
1436 ip->ip_off = 0;
1437 NetCopyIP((void*)&ip->ip_dst, &ip->ip_src);
1438 NetCopyIP((void*)&ip->ip_src, &NetOurIP);
1439 ip->ip_sum = ~NetCksum((uchar *)ip, IP_HDR_SIZE_NO_UDP >> 1);
1440
1441 icmph->type = ICMP_ECHO_REPLY;
1442 icmph->checksum = 0;
1443 icmph->checksum = ~NetCksum((uchar *)icmph,
1444 (len - IP_HDR_SIZE_NO_UDP) >> 1);
1445 (void) eth_send((uchar *)et, ETHER_HDR_SIZE + len);
1446 return;
wdenke6466f62003-06-05 19:27:42 +00001447#endif
1448 default:
1449 return;
1450 }
wdenk2d966952002-10-31 22:12:35 +00001451 } else if (ip->ip_p != IPPROTO_UDP) { /* Only UDP packets */
1452 return;
1453 }
1454
Stefan Roesedfced812005-08-12 20:06:52 +02001455#ifdef CONFIG_UDP_CHECKSUM
1456 if (ip->udp_xsum != 0) {
Wolfgang Denk30b87322005-08-12 23:43:12 +02001457 ulong xsum;
Stefan Roesedfced812005-08-12 20:06:52 +02001458 ushort *sumptr;
1459 ushort sumlen;
1460
1461 xsum = ip->ip_p;
1462 xsum += (ntohs(ip->udp_len));
1463 xsum += (ntohl(ip->ip_src) >> 16) & 0x0000ffff;
1464 xsum += (ntohl(ip->ip_src) >> 0) & 0x0000ffff;
1465 xsum += (ntohl(ip->ip_dst) >> 16) & 0x0000ffff;
1466 xsum += (ntohl(ip->ip_dst) >> 0) & 0x0000ffff;
1467
1468 sumlen = ntohs(ip->udp_len);
1469 sumptr = (ushort *) &(ip->udp_src);
1470
1471 while (sumlen > 1) {
Wolfgang Denk30b87322005-08-12 23:43:12 +02001472 ushort sumdata;
Stefan Roesedfced812005-08-12 20:06:52 +02001473
1474 sumdata = *sumptr++;
1475 xsum += ntohs(sumdata);
1476 sumlen -= 2;
1477 }
1478 if (sumlen > 0) {
Wolfgang Denk30b87322005-08-12 23:43:12 +02001479 ushort sumdata;
Stefan Roesedfced812005-08-12 20:06:52 +02001480
1481 sumdata = *(unsigned char *) sumptr;
Wolfgang Denk30b87322005-08-12 23:43:12 +02001482 sumdata = (sumdata << 8) & 0xff00;
Stefan Roesedfced812005-08-12 20:06:52 +02001483 xsum += sumdata;
1484 }
1485 while ((xsum >> 16) != 0) {
Wolfgang Denk30b87322005-08-12 23:43:12 +02001486 xsum = (xsum & 0x0000ffff) + ((xsum >> 16) & 0x0000ffff);
Stefan Roesedfced812005-08-12 20:06:52 +02001487 }
1488 if ((xsum != 0x00000000) && (xsum != 0x0000ffff)) {
Wolfgang Denk30b87322005-08-12 23:43:12 +02001489 printf(" UDP wrong checksum %08x %08x\n", xsum, ntohs(ip->udp_xsum));
Stefan Roesedfced812005-08-12 20:06:52 +02001490 return;
1491 }
1492 }
1493#endif
1494
wdenkb8fb6192004-08-02 21:11:11 +00001495#ifdef CONFIG_NETCONSOLE
1496 nc_input_packet((uchar *)ip +IP_HDR_SIZE,
1497 ntohs(ip->udp_dst),
1498 ntohs(ip->udp_src),
1499 ntohs(ip->udp_len) - 8);
1500#endif
wdenk2d966952002-10-31 22:12:35 +00001501 /*
1502 * IP header OK. Pass the packet to the current handler.
1503 */
1504 (*packetHandler)((uchar *)ip +IP_HDR_SIZE,
1505 ntohs(ip->udp_dst),
1506 ntohs(ip->udp_src),
1507 ntohs(ip->udp_len) - 8);
wdenk2d966952002-10-31 22:12:35 +00001508 break;
1509 }
1510}
1511
1512
1513/**********************************************************************/
1514
1515static int net_check_prereq (proto_t protocol)
1516{
1517 switch (protocol) {
wdenk05939202004-04-18 17:39:38 +00001518 /* Fall through */
wdenke6466f62003-06-05 19:27:42 +00001519#if (CONFIG_COMMANDS & CFG_CMD_PING)
1520 case PING:
wdenk05939202004-04-18 17:39:38 +00001521 if (NetPingIP == 0) {
1522 puts ("*** ERROR: ping address not given\n");
1523 return (1);
1524 }
1525 goto common;
wdenke6466f62003-06-05 19:27:42 +00001526#endif
wdenkb4ad9622005-04-01 00:25:43 +00001527#if (CONFIG_COMMANDS & CFG_CMD_SNTP)
1528 case SNTP:
1529 if (NetNtpServerIP == 0) {
1530 puts ("*** ERROR: NTP server address not given\n");
1531 return (1);
1532 }
1533 goto common;
1534#endif
wdenkbe9c1cb2004-02-24 02:00:03 +00001535#if (CONFIG_COMMANDS & CFG_CMD_NFS)
1536 case NFS:
1537#endif
wdenkb8fb6192004-08-02 21:11:11 +00001538 case NETCONS:
wdenk2d966952002-10-31 22:12:35 +00001539 case TFTP:
wdenk05939202004-04-18 17:39:38 +00001540 if (NetServerIP == 0) {
1541 puts ("*** ERROR: `serverip' not set\n");
1542 return (1);
1543 }
wdenkb4ad9622005-04-01 00:25:43 +00001544#if (CONFIG_COMMANDS & (CFG_CMD_PING | CFG_CMD_SNTP))
Wolfgang Denk30b87322005-08-12 23:43:12 +02001545 common:
wdenke6466f62003-06-05 19:27:42 +00001546#endif
1547
wdenk05939202004-04-18 17:39:38 +00001548 if (NetOurIP == 0) {
1549 puts ("*** ERROR: `ipaddr' not set\n");
1550 return (1);
1551 }
1552 /* Fall through */
wdenk2d966952002-10-31 22:12:35 +00001553
1554 case DHCP:
1555 case RARP:
1556 case BOOTP:
wdenk145d2c12004-04-15 21:48:45 +00001557 case CDP:
wdenk05939202004-04-18 17:39:38 +00001558 if (memcmp (NetOurEther, "\0\0\0\0\0\0", 6) == 0) {
wdenk2d966952002-10-31 22:12:35 +00001559#ifdef CONFIG_NET_MULTI
wdenk05939202004-04-18 17:39:38 +00001560 extern int eth_get_dev_index (void);
1561 int num = eth_get_dev_index ();
wdenk2d966952002-10-31 22:12:35 +00001562
wdenk05939202004-04-18 17:39:38 +00001563 switch (num) {
1564 case -1:
wdenk2d966952002-10-31 22:12:35 +00001565 puts ("*** ERROR: No ethernet found.\n");
1566 return (1);
wdenk05939202004-04-18 17:39:38 +00001567 case 0:
wdenk2d966952002-10-31 22:12:35 +00001568 puts ("*** ERROR: `ethaddr' not set\n");
1569 break;
wdenk05939202004-04-18 17:39:38 +00001570 default:
wdenk57b2d802003-06-27 21:31:46 +00001571 printf ("*** ERROR: `eth%daddr' not set\n",
wdenk2d966952002-10-31 22:12:35 +00001572 num);
1573 break;
wdenk05939202004-04-18 17:39:38 +00001574 }
wdenk2d966952002-10-31 22:12:35 +00001575
wdenk05939202004-04-18 17:39:38 +00001576 NetStartAgain ();
1577 return (2);
wdenk2d966952002-10-31 22:12:35 +00001578#else
wdenk05939202004-04-18 17:39:38 +00001579 puts ("*** ERROR: `ethaddr' not set\n");
1580 return (1);
wdenk2d966952002-10-31 22:12:35 +00001581#endif
wdenk05939202004-04-18 17:39:38 +00001582 }
1583 /* Fall through */
1584 default:
1585 return (0);
wdenk2d966952002-10-31 22:12:35 +00001586 }
wdenk05939202004-04-18 17:39:38 +00001587 return (0); /* OK */
wdenk2d966952002-10-31 22:12:35 +00001588}
1589/**********************************************************************/
1590
1591int
1592NetCksumOk(uchar * ptr, int len)
1593{
1594 return !((NetCksum(ptr, len) + 1) & 0xfffe);
1595}
1596
1597
1598unsigned
1599NetCksum(uchar * ptr, int len)
1600{
1601 ulong xsum;
Stefan Roeseac553282005-08-31 12:55:50 +02001602 ushort *p = (ushort *)ptr;
wdenk2d966952002-10-31 22:12:35 +00001603
1604 xsum = 0;
1605 while (len-- > 0)
Wolfgang Denk3135c542005-08-26 01:36:03 +02001606 xsum += *p++;
wdenk2d966952002-10-31 22:12:35 +00001607 xsum = (xsum & 0xffff) + (xsum >> 16);
1608 xsum = (xsum & 0xffff) + (xsum >> 16);
1609 return (xsum & 0xffff);
1610}
1611
wdenk145d2c12004-04-15 21:48:45 +00001612int
1613NetEthHdrSize(void)
1614{
1615 ushort myvlanid;
1616
1617 myvlanid = ntohs(NetOurVLAN);
1618 if (myvlanid == (ushort)-1)
1619 myvlanid = VLAN_NONE;
wdenk2d966952002-10-31 22:12:35 +00001620
wdenk145d2c12004-04-15 21:48:45 +00001621 return ((myvlanid & VLAN_IDMASK) == VLAN_NONE) ? ETHER_HDR_SIZE : VLAN_ETHER_HDR_SIZE;
1622}
1623
1624int
wdenk2d966952002-10-31 22:12:35 +00001625NetSetEther(volatile uchar * xet, uchar * addr, uint prot)
1626{
1627 Ethernet_t *et = (Ethernet_t *)xet;
wdenk145d2c12004-04-15 21:48:45 +00001628 ushort myvlanid;
1629
1630 myvlanid = ntohs(NetOurVLAN);
1631 if (myvlanid == (ushort)-1)
1632 myvlanid = VLAN_NONE;
wdenk2d966952002-10-31 22:12:35 +00001633
1634 memcpy (et->et_dest, addr, 6);
1635 memcpy (et->et_src, NetOurEther, 6);
wdenk145d2c12004-04-15 21:48:45 +00001636 if ((myvlanid & VLAN_IDMASK) == VLAN_NONE) {
wdenk2d966952002-10-31 22:12:35 +00001637 et->et_protlen = htons(prot);
wdenk145d2c12004-04-15 21:48:45 +00001638 return ETHER_HDR_SIZE;
1639 } else {
1640 VLAN_Ethernet_t *vet = (VLAN_Ethernet_t *)xet;
wdenk2d966952002-10-31 22:12:35 +00001641
wdenk145d2c12004-04-15 21:48:45 +00001642 vet->vet_vlan_type = htons(PROT_VLAN);
1643 vet->vet_tag = htons((0 << 5) | (myvlanid & VLAN_IDMASK));
1644 vet->vet_type = htons(prot);
1645 return VLAN_ETHER_HDR_SIZE;
1646 }
1647}
wdenk2d966952002-10-31 22:12:35 +00001648
1649void
1650NetSetIP(volatile uchar * xip, IPaddr_t dest, int dport, int sport, int len)
1651{
1652 volatile IP_t *ip = (IP_t *)xip;
1653
1654 /*
1655 * If the data is an odd number of bytes, zero the
1656 * byte after the last byte so that the checksum
1657 * will work.
1658 */
1659 if (len & 1)
1660 xip[IP_HDR_SIZE + len] = 0;
1661
1662 /*
1663 * Construct an IP and UDP header.
wdenk05939202004-04-18 17:39:38 +00001664 * (need to set no fragment bit - XXX)
wdenk2d966952002-10-31 22:12:35 +00001665 */
1666 ip->ip_hl_v = 0x45; /* IP_HDR_SIZE / 4 (not including UDP) */
1667 ip->ip_tos = 0;
1668 ip->ip_len = htons(IP_HDR_SIZE + len);
1669 ip->ip_id = htons(NetIPID++);
1670 ip->ip_off = htons(0x4000); /* No fragmentation */
1671 ip->ip_ttl = 255;
1672 ip->ip_p = 17; /* UDP */
1673 ip->ip_sum = 0;
1674 NetCopyIP((void*)&ip->ip_src, &NetOurIP); /* already in network byte order */
1675 NetCopyIP((void*)&ip->ip_dst, &dest); /* - "" - */
1676 ip->udp_src = htons(sport);
1677 ip->udp_dst = htons(dport);
1678 ip->udp_len = htons(8 + len);
1679 ip->udp_xsum = 0;
1680 ip->ip_sum = ~NetCksum((uchar *)ip, IP_HDR_SIZE_NO_UDP / 2);
1681}
1682
Wolfgang Denk7fb52662005-10-13 16:45:02 +02001683void copy_filename (char *dst, char *src, int size)
wdenk2d966952002-10-31 22:12:35 +00001684{
1685 if (*src && (*src == '"')) {
1686 ++src;
1687 --size;
1688 }
1689
1690 while ((--size > 0) && *src && (*src != '"')) {
1691 *dst++ = *src++;
1692 }
1693 *dst = '\0';
1694}
1695
1696#endif /* CFG_CMD_NET */
1697
1698void ip_to_string (IPaddr_t x, char *s)
1699{
wdenk145d2c12004-04-15 21:48:45 +00001700 x = ntohl (x);
1701 sprintf (s, "%d.%d.%d.%d",
1702 (int) ((x >> 24) & 0xff),
1703 (int) ((x >> 16) & 0xff),
1704 (int) ((x >> 8) & 0xff), (int) ((x >> 0) & 0xff)
wdenk05939202004-04-18 17:39:38 +00001705 );
wdenk2d966952002-10-31 22:12:35 +00001706}
1707
wdenke6466f62003-06-05 19:27:42 +00001708IPaddr_t string_to_ip(char *s)
wdenk2d966952002-10-31 22:12:35 +00001709{
1710 IPaddr_t addr;
wdenke6466f62003-06-05 19:27:42 +00001711 char *e;
wdenk2d966952002-10-31 22:12:35 +00001712 int i;
1713
wdenke6466f62003-06-05 19:27:42 +00001714 if (s == NULL)
1715 return(0);
wdenk2d966952002-10-31 22:12:35 +00001716
1717 for (addr=0, i=0; i<4; ++i) {
1718 ulong val = s ? simple_strtoul(s, &e, 10) : 0;
1719 addr <<= 8;
1720 addr |= (val & 0xFF);
1721 if (s) {
1722 s = (*e) ? e+1 : e;
1723 }
1724 }
1725
1726 return (htonl(addr));
1727}
wdenke6466f62003-06-05 19:27:42 +00001728
wdenk145d2c12004-04-15 21:48:45 +00001729void VLAN_to_string(ushort x, char *s)
1730{
1731 x = ntohs(x);
1732
1733 if (x == (ushort)-1)
1734 x = VLAN_NONE;
1735
1736 if (x == VLAN_NONE)
1737 strcpy(s, "none");
1738 else
1739 sprintf(s, "%d", x & VLAN_IDMASK);
1740}
1741
1742ushort string_to_VLAN(char *s)
1743{
1744 ushort id;
1745
1746 if (s == NULL)
wdenk656140b2004-04-25 13:18:40 +00001747 return htons(VLAN_NONE);
wdenk145d2c12004-04-15 21:48:45 +00001748
1749 if (*s < '0' || *s > '9')
1750 id = VLAN_NONE;
1751 else
1752 id = (ushort)simple_strtoul(s, NULL, 10);
1753
wdenk656140b2004-04-25 13:18:40 +00001754 return htons(id);
wdenk145d2c12004-04-15 21:48:45 +00001755}
1756
wdenke6466f62003-06-05 19:27:42 +00001757void print_IPaddr (IPaddr_t x)
1758{
wdenk145d2c12004-04-15 21:48:45 +00001759 char tmp[16];
wdenke6466f62003-06-05 19:27:42 +00001760
wdenk145d2c12004-04-15 21:48:45 +00001761 ip_to_string (x, tmp);
wdenke6466f62003-06-05 19:27:42 +00001762
wdenk145d2c12004-04-15 21:48:45 +00001763 puts (tmp);
wdenke6466f62003-06-05 19:27:42 +00001764}
1765
1766IPaddr_t getenv_IPaddr (char *var)
1767{
1768 return (string_to_ip(getenv(var)));
1769}
wdenk145d2c12004-04-15 21:48:45 +00001770
1771ushort getenv_VLAN(char *var)
1772{
1773 return (string_to_VLAN(getenv(var)));
1774}