blob: 00aa66150295784dc6970f8a6fb5e6a92a9c95b6 [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 *
43 * Prerequisites: - own ethernet address
44 * We want: - IP, Netmask, ServerIP, Gateway IP
45 * - bootfilename, lease time
46 * Next step: - TFTP
47 *
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 *
70 * Prerequisites: - own ethernet address
71 * - 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
wdenkea287de2005-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
wdenk73a8b272003-06-05 19:27:42 +000095#define ARP_TIMEOUT 5 /* Seconds before trying ARP again */
96#ifndef CONFIG_NET_RETRY_COUNT
97# define ARP_TIMEOUT_COUNT 5 /* # of timeouts before giving up */
98#else
99# define ARP_TIMEOUT_COUNT (CONFIG_NET_RETRY_COUNT)
100#endif
101
wdenk2d966952002-10-31 22:12:35 +0000102#if 0
103#define ET_DEBUG
104#endif
105
106/** BOOTP EXTENTIONS **/
107
108IPaddr_t NetOurSubnetMask=0; /* Our subnet mask (0=unknown) */
109IPaddr_t NetOurGatewayIP=0; /* Our gateways IP address */
110IPaddr_t NetOurDNSIP=0; /* Our DNS IP address */
stroesefe389a82003-08-28 14:17:32 +0000111#if (CONFIG_BOOTP_MASK & CONFIG_BOOTP_DNS2)
112IPaddr_t NetOurDNS2IP=0; /* Our 2nd DNS IP address */
113#endif
wdenk2d966952002-10-31 22:12:35 +0000114char NetOurNISDomain[32]={0,}; /* Our NIS domain */
115char NetOurHostName[32]={0,}; /* Our hostname */
116char NetOurRootPath[64]={0,}; /* Our bootpath */
117ushort NetBootFileSize=0; /* Our bootfile size in blocks */
118
119/** END OF BOOTP EXTENTIONS **/
120
121ulong NetBootFileXferSize; /* The actual transferred size of the bootfile (in bytes) */
122uchar NetOurEther[6]; /* Our ethernet address */
123uchar NetServerEther[6] = /* Boot server enet address */
wdenk73a8b272003-06-05 19:27:42 +0000124 { 0, 0, 0, 0, 0, 0 };
wdenk2d966952002-10-31 22:12:35 +0000125IPaddr_t NetOurIP; /* Our IP addr (0 = unknown) */
126IPaddr_t NetServerIP; /* Our IP addr (0 = unknown) */
127volatile uchar *NetRxPkt; /* Current receive packet */
128int NetRxPktLen; /* Current rx packet length */
129unsigned NetIPID; /* IP packet ID */
130uchar NetBcastAddr[6] = /* Ethernet bcast address */
131 { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
wdenk73a8b272003-06-05 19:27:42 +0000132uchar NetEtherNullAddr[6] =
133 { 0, 0, 0, 0, 0, 0 };
wdenka3d991b2004-04-15 21:48:45 +0000134#if (CONFIG_COMMANDS & CFG_CMD_CDP)
wdenk6e592382004-04-18 17:39:38 +0000135uchar NetCDPAddr[6] = /* Ethernet bcast address */
wdenka3d991b2004-04-15 21:48:45 +0000136 { 0x01, 0x00, 0x0c, 0xcc, 0xcc, 0xcc };
137#endif
wdenk2d966952002-10-31 22:12:35 +0000138int NetState; /* Network loop state */
139#ifdef CONFIG_NET_MULTI
140int NetRestartWrap = 0; /* Tried all network devices */
141static int NetRestarted = 0; /* Network loop restarted */
142static int NetDevExists = 0; /* At least one device configured */
143#endif
144
wdenk6e592382004-04-18 17:39:38 +0000145/* XXX in both little & big endian machines 0xFFFF == ntohs(-1) */
146ushort NetOurVLAN = 0xFFFF; /* default is without VLAN */
147ushort NetOurNativeVLAN = 0xFFFF; /* ditto */
wdenka3d991b2004-04-15 21:48:45 +0000148
wdenk2d966952002-10-31 22:12:35 +0000149char BootFile[128]; /* Boot File name */
150
wdenk73a8b272003-06-05 19:27:42 +0000151#if (CONFIG_COMMANDS & CFG_CMD_PING)
152IPaddr_t NetPingIP; /* the ip address to ping */
153
154static void PingStart(void);
155#endif
156
wdenka3d991b2004-04-15 21:48:45 +0000157#if (CONFIG_COMMANDS & CFG_CMD_CDP)
158static void CDPStart(void);
159#endif
160
wdenkea287de2005-04-01 00:25:43 +0000161#if (CONFIG_COMMANDS & CFG_CMD_SNTP)
162IPaddr_t NetNtpServerIP; /* NTP server IP address */
163int NetTimeOffset=0; /* offset time from UTC */
164#endif
165
wdenk68ceb292004-08-02 21:11:11 +0000166#ifdef CONFIG_NETCONSOLE
167void NcStart(void);
168int nc_input_packet(uchar *pkt, unsigned dest, unsigned src, unsigned len);
169#endif
170
wdenk2d966952002-10-31 22:12:35 +0000171volatile uchar PktBuf[(PKTBUFSRX+1) * PKTSIZE_ALIGN + PKTALIGN];
172
173volatile uchar *NetRxPackets[PKTBUFSRX]; /* Receive packets */
174
175static rxhand_f *packetHandler; /* Current RX packet handler */
176static thand_f *timeHandler; /* Current timeout handler */
wdenke0ac62d2003-08-17 18:55:18 +0000177static ulong timeStart; /* Time base value */
178static ulong timeDelta; /* Current timeout value */
wdenk2d966952002-10-31 22:12:35 +0000179volatile uchar *NetTxPacket = 0; /* THE transmit packet */
180
181static int net_check_prereq (proto_t protocol);
182
183/**********************************************************************/
wdenk73a8b272003-06-05 19:27:42 +0000184
185IPaddr_t NetArpWaitPacketIP;
186IPaddr_t NetArpWaitReplyIP;
187uchar *NetArpWaitPacketMAC; /* MAC address of waiting packet's destination */
188uchar *NetArpWaitTxPacket; /* THE transmit packet */
189int NetArpWaitTxPacketSize;
190uchar NetArpWaitPacketBuf[PKTSIZE_ALIGN + PKTALIGN];
191ulong NetArpWaitTimerStart;
192int NetArpWaitTry;
193
wdenk6e592382004-04-18 17:39:38 +0000194void ArpRequest (void)
wdenk73a8b272003-06-05 19:27:42 +0000195{
196 int i;
197 volatile uchar *pkt;
wdenk6e592382004-04-18 17:39:38 +0000198 ARP_t *arp;
wdenk73a8b272003-06-05 19:27:42 +0000199
200#ifdef ET_DEBUG
wdenk6e592382004-04-18 17:39:38 +0000201 printf ("ARP broadcast %d\n", NetArpWaitTry);
wdenk73a8b272003-06-05 19:27:42 +0000202#endif
203 pkt = NetTxPacket;
204
wdenk6e592382004-04-18 17:39:38 +0000205 pkt += NetSetEther (pkt, NetBcastAddr, PROT_ARP);
wdenk73a8b272003-06-05 19:27:42 +0000206
wdenk6e592382004-04-18 17:39:38 +0000207 arp = (ARP_t *) pkt;
wdenk73a8b272003-06-05 19:27:42 +0000208
wdenk6e592382004-04-18 17:39:38 +0000209 arp->ar_hrd = htons (ARP_ETHER);
210 arp->ar_pro = htons (PROT_IP);
wdenk73a8b272003-06-05 19:27:42 +0000211 arp->ar_hln = 6;
212 arp->ar_pln = 4;
wdenk6e592382004-04-18 17:39:38 +0000213 arp->ar_op = htons (ARPOP_REQUEST);
wdenk73a8b272003-06-05 19:27:42 +0000214
wdenk6e592382004-04-18 17:39:38 +0000215 memcpy (&arp->ar_data[0], NetOurEther, 6); /* source ET addr */
216 NetWriteIP ((uchar *) & arp->ar_data[6], NetOurIP); /* source IP addr */
217 for (i = 10; i < 16; ++i) {
218 arp->ar_data[i] = 0; /* dest ET addr = 0 */
wdenk73a8b272003-06-05 19:27:42 +0000219 }
220
wdenk6e592382004-04-18 17:39:38 +0000221 if ((NetArpWaitPacketIP & NetOurSubnetMask) !=
222 (NetOurIP & NetOurSubnetMask)) {
223 if (NetOurGatewayIP == 0) {
224 puts ("## Warning: gatewayip needed but not set\n");
225 }
226 NetArpWaitReplyIP = NetOurGatewayIP;
227 } else {
228 NetArpWaitReplyIP = NetArpWaitPacketIP;
229 }
wdenk73a8b272003-06-05 19:27:42 +0000230
wdenk6e592382004-04-18 17:39:38 +0000231 NetWriteIP ((uchar *) & arp->ar_data[16], NetArpWaitReplyIP);
232 (void) eth_send (NetTxPacket, (pkt - NetTxPacket) + ARP_HDR_SIZE);
wdenk73a8b272003-06-05 19:27:42 +0000233}
234
235void ArpTimeoutCheck(void)
236{
237 ulong t;
238
239 if (!NetArpWaitPacketIP)
240 return;
241
242 t = get_timer(0);
243
244 /* check for arp timeout */
245 if ((t - NetArpWaitTimerStart) > ARP_TIMEOUT * CFG_HZ) {
246 NetArpWaitTry++;
247
248 if (NetArpWaitTry >= ARP_TIMEOUT_COUNT) {
249 puts ("\nARP Retry count exceeded; starting again\n");
250 NetArpWaitTry = 0;
251 NetStartAgain();
252 } else {
253 NetArpWaitTimerStart = t;
254 ArpRequest();
255 }
256 }
257}
258
259/**********************************************************************/
wdenk2d966952002-10-31 22:12:35 +0000260/*
261 * Main network processing loop.
262 */
263
264int
265NetLoop(proto_t protocol)
266{
267 DECLARE_GLOBAL_DATA_PTR;
268
269 bd_t *bd = gd->bd;
270
271#ifdef CONFIG_NET_MULTI
272 NetRestarted = 0;
273 NetDevExists = 0;
274#endif
275
wdenk73a8b272003-06-05 19:27:42 +0000276 /* XXX problem with bss workaround */
277 NetArpWaitPacketMAC = NULL;
278 NetArpWaitTxPacket = NULL;
279 NetArpWaitPacketIP = 0;
280 NetArpWaitReplyIP = 0;
281 NetArpWaitTxPacket = NULL;
282 NetTxPacket = NULL;
283
wdenk2d966952002-10-31 22:12:35 +0000284 if (!NetTxPacket) {
285 int i;
wdenk2d966952002-10-31 22:12:35 +0000286 /*
287 * Setup packet buffers, aligned correctly.
288 */
289 NetTxPacket = &PktBuf[0] + (PKTALIGN - 1);
290 NetTxPacket -= (ulong)NetTxPacket % PKTALIGN;
291 for (i = 0; i < PKTBUFSRX; i++) {
292 NetRxPackets[i] = NetTxPacket + (i+1)*PKTSIZE_ALIGN;
293 }
wdenk73a8b272003-06-05 19:27:42 +0000294 }
295
296 if (!NetArpWaitTxPacket) {
297 NetArpWaitTxPacket = &NetArpWaitPacketBuf[0] + (PKTALIGN - 1);
298 NetArpWaitTxPacket -= (ulong)NetArpWaitTxPacket % PKTALIGN;
299 NetArpWaitTxPacketSize = 0;
wdenk2d966952002-10-31 22:12:35 +0000300 }
301
302 eth_halt();
wdenk6e592382004-04-18 17:39:38 +0000303#ifdef CONFIG_NET_MULTI
wdenka3d991b2004-04-15 21:48:45 +0000304 eth_set_current();
wdenk6e592382004-04-18 17:39:38 +0000305#endif
306 if (eth_init(bd) < 0)
307 return(-1);
wdenk2d966952002-10-31 22:12:35 +0000308
309restart:
310#ifdef CONFIG_NET_MULTI
311 memcpy (NetOurEther, eth_get_dev()->enetaddr, 6);
312#else
313 memcpy (NetOurEther, bd->bi_enetaddr, 6);
314#endif
315
316 NetState = NETLOOP_CONTINUE;
317
318 /*
319 * Start the ball rolling with the given start function. From
320 * here on, this code is a state machine driven by received
321 * packets and timer events.
322 */
323
324 switch (protocol) {
wdenkcbd8a352004-02-24 02:00:03 +0000325#if (CONFIG_COMMANDS & CFG_CMD_NFS)
326 case NFS:
327#endif
wdenk73a8b272003-06-05 19:27:42 +0000328#if (CONFIG_COMMANDS & CFG_CMD_PING)
329 case PING:
330#endif
wdenkea287de2005-04-01 00:25:43 +0000331#if (CONFIG_COMMANDS & CFG_CMD_SNTP)
332 case SNTP:
333#endif
wdenk68ceb292004-08-02 21:11:11 +0000334 case NETCONS:
wdenk2d966952002-10-31 22:12:35 +0000335 case TFTP:
336 NetCopyIP(&NetOurIP, &bd->bi_ip_addr);
wdenk2d966952002-10-31 22:12:35 +0000337 NetOurGatewayIP = getenv_IPaddr ("gatewayip");
338 NetOurSubnetMask= getenv_IPaddr ("netmask");
wdenka3d991b2004-04-15 21:48:45 +0000339 NetOurVLAN = getenv_VLAN("vlan");
340 NetOurNativeVLAN = getenv_VLAN("nvlan");
wdenk73a8b272003-06-05 19:27:42 +0000341
342 switch (protocol) {
wdenkcbd8a352004-02-24 02:00:03 +0000343#if (CONFIG_COMMANDS & CFG_CMD_NFS)
344 case NFS:
345#endif
wdenk68ceb292004-08-02 21:11:11 +0000346 case NETCONS:
wdenk73a8b272003-06-05 19:27:42 +0000347 case TFTP:
348 NetServerIP = getenv_IPaddr ("serverip");
349 break;
350#if (CONFIG_COMMANDS & CFG_CMD_PING)
351 case PING:
352 /* nothing */
353 break;
354#endif
wdenkea287de2005-04-01 00:25:43 +0000355#if (CONFIG_COMMANDS & CFG_CMD_SNTP)
356 case SNTP:
357 /* nothing */
358 break;
359#endif
wdenk73a8b272003-06-05 19:27:42 +0000360 default:
361 break;
362 }
363
wdenk2d966952002-10-31 22:12:35 +0000364 break;
365 case BOOTP:
366 case RARP:
367 /*
wdenk8bde7f72003-06-27 21:31:46 +0000368 * initialize our IP addr to 0 in order to accept ANY
369 * IP addr assigned to us by the BOOTP / RARP server
wdenk2d966952002-10-31 22:12:35 +0000370 */
371 NetOurIP = 0;
wdenk3d3befa2004-03-14 15:06:13 +0000372 NetServerIP = getenv_IPaddr ("serverip");
wdenka3d991b2004-04-15 21:48:45 +0000373 NetOurVLAN = getenv_VLAN("vlan"); /* VLANs must be read */
374 NetOurNativeVLAN = getenv_VLAN("nvlan");
375 case CDP:
376 NetOurVLAN = getenv_VLAN("vlan"); /* VLANs must be read */
377 NetOurNativeVLAN = getenv_VLAN("nvlan");
wdenk2d966952002-10-31 22:12:35 +0000378 break;
379 default:
380 break;
381 }
382
383 switch (net_check_prereq (protocol)) {
384 case 1:
385 /* network not configured */
wdenk1d0350e2002-11-11 21:14:20 +0000386 return (-1);
wdenk2d966952002-10-31 22:12:35 +0000387
388#ifdef CONFIG_NET_MULTI
389 case 2:
390 /* network device not configured */
391 break;
392#endif /* CONFIG_NET_MULTI */
393
394 case 0:
395#ifdef CONFIG_NET_MULTI
396 NetDevExists = 1;
397#endif
398 switch (protocol) {
399 case TFTP:
400 /* always use ARP to get server ethernet address */
wdenk73a8b272003-06-05 19:27:42 +0000401 TftpStart();
wdenk2d966952002-10-31 22:12:35 +0000402 break;
403
404#if (CONFIG_COMMANDS & CFG_CMD_DHCP)
405 case DHCP:
406 /* Start with a clean slate... */
wdenkd407bf52004-10-11 22:51:13 +0000407 BootpTry = 0;
wdenk2d966952002-10-31 22:12:35 +0000408 NetOurIP = 0;
wdenk3d3befa2004-03-14 15:06:13 +0000409 NetServerIP = getenv_IPaddr ("serverip");
wdenk2d966952002-10-31 22:12:35 +0000410 DhcpRequest(); /* Basically same as BOOTP */
411 break;
412#endif /* CFG_CMD_DHCP */
413
414 case BOOTP:
415 BootpTry = 0;
416 BootpRequest ();
417 break;
418
419 case RARP:
420 RarpTry = 0;
421 RarpRequest ();
422 break;
wdenk73a8b272003-06-05 19:27:42 +0000423#if (CONFIG_COMMANDS & CFG_CMD_PING)
424 case PING:
425 PingStart();
426 break;
427#endif
wdenkcbd8a352004-02-24 02:00:03 +0000428#if (CONFIG_COMMANDS & CFG_CMD_NFS)
429 case NFS:
430 NfsStart();
431 break;
432#endif
wdenka3d991b2004-04-15 21:48:45 +0000433#if (CONFIG_COMMANDS & CFG_CMD_CDP)
434 case CDP:
435 CDPStart();
436 break;
437#endif
wdenk68ceb292004-08-02 21:11:11 +0000438#ifdef CONFIG_NETCONSOLE
439 case NETCONS:
440 NcStart();
441 break;
442#endif
wdenkea287de2005-04-01 00:25:43 +0000443#if (CONFIG_COMMANDS & CFG_CMD_SNTP)
444 case SNTP:
445 SntpStart();
446 break;
447#endif
wdenk2d966952002-10-31 22:12:35 +0000448 default:
449 break;
450 }
451
452 NetBootFileXferSize = 0;
453 break;
454 }
455
wdenkfc3e2162003-10-08 22:33:00 +0000456#if defined(CONFIG_MII) || (CONFIG_COMMANDS & CFG_CMD_MII)
457#if defined(CFG_FAULT_ECHO_LINK_DOWN) && defined(CONFIG_STATUS_LED) && defined(STATUS_LED_RED)
458 /*
wdenk42d1f032003-10-15 23:53:47 +0000459 * Echo the inverted link state to the fault LED.
wdenkfc3e2162003-10-08 22:33:00 +0000460 */
461 if(miiphy_link(CFG_FAULT_MII_ADDR)) {
462 status_led_set (STATUS_LED_RED, STATUS_LED_OFF);
463 } else {
464 status_led_set (STATUS_LED_RED, STATUS_LED_ON);
465 }
466#endif /* CFG_FAULT_ECHO_LINK_DOWN, ... */
467#endif /* CONFIG_MII, ... */
wdenk2d966952002-10-31 22:12:35 +0000468
469 /*
470 * Main packet reception loop. Loop receiving packets until
wdenk59acc292005-04-03 14:18:51 +0000471 * someone sets `NetState' to a state that terminates.
wdenk2d966952002-10-31 22:12:35 +0000472 */
473 for (;;) {
474 WATCHDOG_RESET();
475#ifdef CONFIG_SHOW_ACTIVITY
476 {
477 extern void show_activity(int arg);
478 show_activity(1);
479 }
480#endif
481 /*
482 * Check the ethernet for a new packet. The ethernet
483 * receive routine will process it.
484 */
485 eth_rx();
486
487 /*
488 * Abort if ctrl-c was pressed.
489 */
490 if (ctrlc()) {
wdenk8bde7f72003-06-27 21:31:46 +0000491 eth_halt();
wdenk4b9206e2004-03-23 22:14:11 +0000492 puts ("\nAbort\n");
wdenk1d0350e2002-11-11 21:14:20 +0000493 return (-1);
wdenk2d966952002-10-31 22:12:35 +0000494 }
495
wdenk73a8b272003-06-05 19:27:42 +0000496 ArpTimeoutCheck();
wdenk2d966952002-10-31 22:12:35 +0000497
498 /*
499 * Check for a timeout, and run the timeout handler
500 * if we have one.
501 */
wdenke0ac62d2003-08-17 18:55:18 +0000502 if (timeHandler && ((get_timer(0) - timeStart) > timeDelta)) {
wdenk2d966952002-10-31 22:12:35 +0000503 thand_f *x;
504
wdenkfc3e2162003-10-08 22:33:00 +0000505#if defined(CONFIG_MII) || (CONFIG_COMMANDS & CFG_CMD_MII)
wdenk59acc292005-04-03 14:18:51 +0000506# if defined(CFG_FAULT_ECHO_LINK_DOWN) &&
507 defined(CONFIG_STATUS_LED) &&
508 defined(STATUS_LED_RED)
wdenkfc3e2162003-10-08 22:33:00 +0000509 /*
wdenk42d1f032003-10-15 23:53:47 +0000510 * Echo the inverted link state to the fault LED.
wdenkfc3e2162003-10-08 22:33:00 +0000511 */
512 if(miiphy_link(CFG_FAULT_MII_ADDR)) {
513 status_led_set (STATUS_LED_RED, STATUS_LED_OFF);
514 } else {
515 status_led_set (STATUS_LED_RED, STATUS_LED_ON);
516 }
wdenk59acc292005-04-03 14:18:51 +0000517# endif /* CFG_FAULT_ECHO_LINK_DOWN, ... */
wdenkfc3e2162003-10-08 22:33:00 +0000518#endif /* CONFIG_MII, ... */
wdenk2d966952002-10-31 22:12:35 +0000519 x = timeHandler;
520 timeHandler = (thand_f *)0;
521 (*x)();
522 }
523
524
525 switch (NetState) {
526
527 case NETLOOP_RESTART:
528#ifdef CONFIG_NET_MULTI
529 NetRestarted = 1;
530#endif
531 goto restart;
532
533 case NETLOOP_SUCCESS:
534 if (NetBootFileXferSize > 0) {
535 char buf[10];
536 printf("Bytes transferred = %ld (%lx hex)\n",
537 NetBootFileXferSize,
538 NetBootFileXferSize);
539 sprintf(buf, "%lx", NetBootFileXferSize);
540 setenv("filesize", buf);
wdenka3d991b2004-04-15 21:48:45 +0000541
542 sprintf(buf, "%lX", (unsigned long)load_addr);
543 setenv("fileaddr", buf);
wdenk2d966952002-10-31 22:12:35 +0000544 }
545 eth_halt();
546 return NetBootFileXferSize;
547
548 case NETLOOP_FAIL:
wdenk1d0350e2002-11-11 21:14:20 +0000549 return (-1);
wdenk2d966952002-10-31 22:12:35 +0000550 }
551 }
552}
553
554/**********************************************************************/
555
556static void
557startAgainTimeout(void)
558{
559 NetState = NETLOOP_RESTART;
560}
561
562static void
563startAgainHandler(uchar * pkt, unsigned dest, unsigned src, unsigned len)
564{
565 /* Totally ignore the packet */
566}
567
wdenk6e592382004-04-18 17:39:38 +0000568void NetStartAgain (void)
wdenk2d966952002-10-31 22:12:35 +0000569{
wdenk6e592382004-04-18 17:39:38 +0000570#ifdef CONFIG_NET_MULTI
wdenka3d991b2004-04-15 21:48:45 +0000571 DECLARE_GLOBAL_DATA_PTR;
wdenk6e592382004-04-18 17:39:38 +0000572#endif
573 char *nretry;
574 int noretry = 0, once = 0;
wdenka3d991b2004-04-15 21:48:45 +0000575
wdenk6e592382004-04-18 17:39:38 +0000576 if ((nretry = getenv ("netretry")) != NULL) {
577 noretry = (strcmp (nretry, "no") == 0);
578 once = (strcmp (nretry, "once") == 0);
579 }
580 if (noretry) {
581 eth_halt ();
wdenka3d991b2004-04-15 21:48:45 +0000582 NetState = NETLOOP_FAIL;
583 return;
584 }
wdenk2d966952002-10-31 22:12:35 +0000585#ifndef CONFIG_NET_MULTI
wdenk6e592382004-04-18 17:39:38 +0000586 NetSetTimeout (10 * CFG_HZ, startAgainTimeout);
587 NetSetHandler (startAgainHandler);
588#else /* !CONFIG_NET_MULTI*/
589 eth_halt ();
590 eth_try_another (!NetRestarted);
591 eth_init (gd->bd);
592 if (NetRestartWrap) {
wdenk2d966952002-10-31 22:12:35 +0000593 NetRestartWrap = 0;
wdenk6e592382004-04-18 17:39:38 +0000594 if (NetDevExists && !once) {
595 NetSetTimeout (10 * CFG_HZ, startAgainTimeout);
596 NetSetHandler (startAgainHandler);
597 } else {
wdenk2d966952002-10-31 22:12:35 +0000598 NetState = NETLOOP_FAIL;
599 }
wdenk6e592382004-04-18 17:39:38 +0000600 } else {
wdenk2d966952002-10-31 22:12:35 +0000601 NetState = NETLOOP_RESTART;
602 }
wdenk6e592382004-04-18 17:39:38 +0000603#endif /* CONFIG_NET_MULTI */
wdenk2d966952002-10-31 22:12:35 +0000604}
605
606/**********************************************************************/
607/*
608 * Miscelaneous bits.
609 */
610
611void
612NetSetHandler(rxhand_f * f)
613{
614 packetHandler = f;
615}
616
617
618void
wdenk3e01d752004-10-09 21:56:21 +0000619NetSetTimeout(ulong iv, thand_f * f)
wdenk2d966952002-10-31 22:12:35 +0000620{
621 if (iv == 0) {
622 timeHandler = (thand_f *)0;
623 } else {
624 timeHandler = f;
wdenke0ac62d2003-08-17 18:55:18 +0000625 timeStart = get_timer(0);
626 timeDelta = iv;
wdenk2d966952002-10-31 22:12:35 +0000627 }
628}
629
630
631void
632NetSendPacket(volatile uchar * pkt, int len)
633{
634 (void) eth_send(pkt, len);
635}
636
wdenk73a8b272003-06-05 19:27:42 +0000637int
638NetSendUDPPacket(uchar *ether, IPaddr_t dest, int dport, int sport, int len)
639{
wdenka3d991b2004-04-15 21:48:45 +0000640 uchar *pkt;
641
wdenk73a8b272003-06-05 19:27:42 +0000642 /* convert to new style broadcast */
643 if (dest == 0)
644 dest = 0xFFFFFFFF;
wdenk2d966952002-10-31 22:12:35 +0000645
wdenk73a8b272003-06-05 19:27:42 +0000646 /* if broadcast, make the ether address a broadcast and don't do ARP */
647 if (dest == 0xFFFFFFFF)
648 ether = NetBcastAddr;
649
650 /* if MAC address was not discovered yet, save the packet and do an ARP request */
651 if (memcmp(ether, NetEtherNullAddr, 6) == 0) {
652
653#ifdef ET_DEBUG
654 printf("sending ARP for %08lx\n", dest);
655#endif
wdenk73a8b272003-06-05 19:27:42 +0000656 NetArpWaitPacketIP = dest;
657 NetArpWaitPacketMAC = ether;
wdenka3d991b2004-04-15 21:48:45 +0000658
659 pkt = NetArpWaitTxPacket;
660 pkt += NetSetEther (pkt, NetArpWaitPacketMAC, PROT_IP);
661
662 NetSetIP (pkt, dest, dport, sport, len);
663 memcpy(pkt + IP_HDR_SIZE, (uchar *)NetTxPacket + (pkt - (uchar *)NetArpWaitTxPacket) + IP_HDR_SIZE, len);
wdenk73a8b272003-06-05 19:27:42 +0000664
665 /* size of the waiting packet */
wdenka3d991b2004-04-15 21:48:45 +0000666 NetArpWaitTxPacketSize = (pkt - NetArpWaitTxPacket) + IP_HDR_SIZE + len;
wdenk73a8b272003-06-05 19:27:42 +0000667
668 /* and do the ARP request */
669 NetArpWaitTry = 1;
670 NetArpWaitTimerStart = get_timer(0);
671 ArpRequest();
672 return 1; /* waiting */
673 }
674
675#ifdef ET_DEBUG
676 printf("sending UDP to %08lx/%02x:%02x:%02x:%02x:%02x:%02x\n",
wdenk6e592382004-04-18 17:39:38 +0000677 dest, ether[0], ether[1], ether[2], ether[3], ether[4], ether[5]);
wdenk73a8b272003-06-05 19:27:42 +0000678#endif
679
wdenka3d991b2004-04-15 21:48:45 +0000680 pkt = (uchar *)NetTxPacket;
681 pkt += NetSetEther (pkt, ether, PROT_IP);
682 NetSetIP (pkt, dest, dport, sport, len);
683 (void) eth_send(NetTxPacket, (pkt - NetTxPacket) + IP_HDR_SIZE + len);
wdenk73a8b272003-06-05 19:27:42 +0000684
wdenk6e592382004-04-18 17:39:38 +0000685 return 0; /* transmitted */
wdenk73a8b272003-06-05 19:27:42 +0000686}
687
688#if (CONFIG_COMMANDS & CFG_CMD_PING)
689static ushort PingSeqNo;
690
691int PingSend(void)
692{
693 static uchar mac[6];
694 volatile IP_t *ip;
695 volatile ushort *s;
wdenka3d991b2004-04-15 21:48:45 +0000696 uchar *pkt;
wdenk73a8b272003-06-05 19:27:42 +0000697
698 /* XXX always send arp request */
699
700 memcpy(mac, NetEtherNullAddr, 6);
701
702#ifdef ET_DEBUG
703 printf("sending ARP for %08lx\n", NetPingIP);
704#endif
705
706 NetArpWaitPacketIP = NetPingIP;
707 NetArpWaitPacketMAC = mac;
708
wdenka3d991b2004-04-15 21:48:45 +0000709 pkt = NetArpWaitTxPacket;
710 pkt += NetSetEther(pkt, mac, PROT_IP);
wdenk73a8b272003-06-05 19:27:42 +0000711
wdenka3d991b2004-04-15 21:48:45 +0000712 ip = (volatile IP_t *)pkt;
wdenk73a8b272003-06-05 19:27:42 +0000713
714 /*
715 * Construct an IP and ICMP header. (need to set no fragment bit - XXX)
716 */
717 ip->ip_hl_v = 0x45; /* IP_HDR_SIZE / 4 (not including UDP) */
718 ip->ip_tos = 0;
719 ip->ip_len = htons(IP_HDR_SIZE_NO_UDP + 8);
720 ip->ip_id = htons(NetIPID++);
721 ip->ip_off = htons(0x4000); /* No fragmentation */
722 ip->ip_ttl = 255;
723 ip->ip_p = 0x01; /* ICMP */
724 ip->ip_sum = 0;
725 NetCopyIP((void*)&ip->ip_src, &NetOurIP); /* already in network byte order */
726 NetCopyIP((void*)&ip->ip_dst, &NetPingIP); /* - "" - */
727 ip->ip_sum = ~NetCksum((uchar *)ip, IP_HDR_SIZE_NO_UDP / 2);
728
729 s = &ip->udp_src; /* XXX ICMP starts here */
730 s[0] = htons(0x0800); /* echo-request, code */
731 s[1] = 0; /* checksum */
732 s[2] = 0; /* identifier */
733 s[3] = htons(PingSeqNo++); /* sequence number */
734 s[1] = ~NetCksum((uchar *)s, 8/2);
735
736 /* size of the waiting packet */
wdenka3d991b2004-04-15 21:48:45 +0000737 NetArpWaitTxPacketSize = (pkt - NetArpWaitTxPacket) + IP_HDR_SIZE_NO_UDP + 8;
wdenk73a8b272003-06-05 19:27:42 +0000738
739 /* and do the ARP request */
740 NetArpWaitTry = 1;
741 NetArpWaitTimerStart = get_timer(0);
742 ArpRequest();
743 return 1; /* waiting */
744}
745
746static void
747PingTimeout (void)
748{
749 eth_halt();
750 NetState = NETLOOP_FAIL; /* we did not get the reply */
751}
752
753static void
754PingHandler (uchar * pkt, unsigned dest, unsigned src, unsigned len)
755{
756 IPaddr_t tmp;
757 volatile IP_t *ip = (volatile IP_t *)pkt;
758
759 tmp = NetReadIP((void *)&ip->ip_src);
760 if (tmp != NetPingIP)
761 return;
762
763 NetState = NETLOOP_SUCCESS;
764}
765
766static void PingStart(void)
767{
wdenka3d991b2004-04-15 21:48:45 +0000768#if defined(CONFIG_NET_MULTI)
769 printf ("Using %s device\n", eth_get_name());
wdenk6e592382004-04-18 17:39:38 +0000770#endif /* CONFIG_NET_MULTI */
wdenk73a8b272003-06-05 19:27:42 +0000771 NetSetTimeout (10 * CFG_HZ, PingTimeout);
772 NetSetHandler (PingHandler);
773
774 PingSend();
775}
wdenk6e592382004-04-18 17:39:38 +0000776#endif /* CFG_CMD_PING */
wdenk2d966952002-10-31 22:12:35 +0000777
wdenka3d991b2004-04-15 21:48:45 +0000778#if (CONFIG_COMMANDS & CFG_CMD_CDP)
779
780#define CDP_DEVICE_ID_TLV 0x0001
781#define CDP_ADDRESS_TLV 0x0002
782#define CDP_PORT_ID_TLV 0x0003
783#define CDP_CAPABILITIES_TLV 0x0004
784#define CDP_VERSION_TLV 0x0005
785#define CDP_PLATFORM_TLV 0x0006
786#define CDP_NATIVE_VLAN_TLV 0x000a
787#define CDP_APPLIANCE_VLAN_TLV 0x000e
788#define CDP_TRIGGER_TLV 0x000f
789#define CDP_POWER_CONSUMPTION_TLV 0x0010
790#define CDP_SYSNAME_TLV 0x0014
791#define CDP_SYSOBJECT_TLV 0x0015
792#define CDP_MANAGEMENT_ADDRESS_TLV 0x0016
793
794#define CDP_TIMEOUT (CFG_HZ/4) /* one packet every 250ms */
795
796static int CDPSeq;
797static int CDPOK;
798
799ushort CDPNativeVLAN;
800ushort CDPApplianceVLAN;
801
802static const uchar CDP_SNAP_hdr[8] = { 0xAA, 0xAA, 0x03, 0x00, 0x00, 0x0C, 0x20, 0x00 };
803
804static ushort CDP_compute_csum(const uchar *buff, ushort len)
805{
806 ushort csum;
807 int odd;
808 ulong result = 0;
809 ushort leftover;
810
811 if (len > 0) {
812 odd = 1 & (ulong)buff;
813 if (odd) {
814 result = *buff << 8;
815 len--;
816 buff++;
817 }
818 while (len > 1) {
819 result += *((const ushort *)buff)++;
820 if (result & 0x80000000)
821 result = (result & 0xFFFF) + (result >> 16);
822 len -= 2;
823 }
824 if (len) {
825 leftover = (signed short)(*(const signed char *)buff);
826 /* * XXX CISCO SUCKS big time! (and blows too) */
827 result = (result & 0xffff0000) | ((result + leftover) & 0x0000ffff);
828 }
829 while (result >> 16)
830 result = (result & 0xFFFF) + (result >> 16);
831
832 if (odd)
833 result = ((result >> 8) & 0xff) | ((result & 0xff) << 8);
834 }
835
836 /* add up 16-bit and 17-bit words for 17+c bits */
837 result = (result & 0xffff) + (result >> 16);
838 /* add up 16-bit and 2-bit for 16+c bit */
839 result = (result & 0xffff) + (result >> 16);
840 /* add up carry.. */
841 result = (result & 0xffff) + (result >> 16);
842
843 /* negate */
844 csum = ~(ushort)result;
845
846 /* run time endian detection */
847 if (csum != htons(csum)) /* little endian */
848 csum = htons(csum);
849
850 return csum;
851}
852
853int CDPSendTrigger(void)
854{
855 volatile uchar *pkt;
856 volatile ushort *s;
857 volatile ushort *cp;
858 Ethernet_t *et;
wdenka3d991b2004-04-15 21:48:45 +0000859 int len;
860 ushort chksum;
wdenk6e592382004-04-18 17:39:38 +0000861#if defined(CONFIG_CDP_DEVICE_ID) || defined(CONFIG_CDP_PORT_ID) || \
862 defined(CONFIG_CDP_VERSION) || defined(CONFIG_CDP_PLATFORM)
863 char buf[32];
864#endif
wdenka3d991b2004-04-15 21:48:45 +0000865
866 pkt = NetTxPacket;
867 et = (Ethernet_t *)pkt;
868
869 /* NOTE: trigger sent not on any VLAN */
870
871 /* form ethernet header */
872 memcpy(et->et_dest, NetCDPAddr, 6);
873 memcpy(et->et_src, NetOurEther, 6);
874
875 pkt += ETHER_HDR_SIZE;
876
877 /* SNAP header */
878 memcpy((uchar *)pkt, CDP_SNAP_hdr, sizeof(CDP_SNAP_hdr));
879 pkt += sizeof(CDP_SNAP_hdr);
880
881 /* CDP header */
882 *pkt++ = 0x02; /* CDP version 2 */
883 *pkt++ = 180; /* TTL */
884 s = (volatile ushort *)pkt;
885 cp = s;
886 *s++ = htons(0); /* checksum (0 for later calculation) */
887
888 /* CDP fields */
889#ifdef CONFIG_CDP_DEVICE_ID
890 *s++ = htons(CDP_DEVICE_ID_TLV);
891 *s++ = htons(CONFIG_CDP_DEVICE_ID);
892 memset(buf, 0, sizeof(buf));
893 sprintf(buf, CONFIG_CDP_DEVICE_ID_PREFIX "%02X%02X%02X%02X%02X%02X",
894 NetOurEther[0] & 0xff, NetOurEther[1] & 0xff,
895 NetOurEther[2] & 0xff, NetOurEther[3] & 0xff,
896 NetOurEther[4] & 0xff, NetOurEther[5] & 0xff);
897 memcpy((uchar *)s, buf, 16);
898 s += 16 / 2;
899#endif
900
901#ifdef CONFIG_CDP_PORT_ID
902 *s++ = htons(CDP_PORT_ID_TLV);
903 memset(buf, 0, sizeof(buf));
904 sprintf(buf, CONFIG_CDP_PORT_ID, eth_get_dev_index());
905 len = strlen(buf);
906 if (len & 1) /* make it even */
907 len++;
908 *s++ = htons(len + 4);
909 memcpy((uchar *)s, buf, len);
910 s += len / 2;
911#endif
912
913#ifdef CONFIG_CDP_CAPABILITIES
914 *s++ = htons(CDP_CAPABILITIES_TLV);
915 *s++ = htons(8);
916 *(ulong *)s = htonl(CONFIG_CDP_CAPABILITIES);
917 s += 2;
918#endif
919
920#ifdef CONFIG_CDP_VERSION
921 *s++ = htons(CDP_VERSION_TLV);
922 memset(buf, 0, sizeof(buf));
923 strcpy(buf, CONFIG_CDP_VERSION);
924 len = strlen(buf);
925 if (len & 1) /* make it even */
926 len++;
927 *s++ = htons(len + 4);
928 memcpy((uchar *)s, buf, len);
929 s += len / 2;
930#endif
931
932#ifdef CONFIG_CDP_PLATFORM
933 *s++ = htons(CDP_PLATFORM_TLV);
934 memset(buf, 0, sizeof(buf));
935 strcpy(buf, CONFIG_CDP_PLATFORM);
936 len = strlen(buf);
937 if (len & 1) /* make it even */
938 len++;
939 *s++ = htons(len + 4);
940 memcpy((uchar *)s, buf, len);
941 s += len / 2;
942#endif
943
944#ifdef CONFIG_CDP_TRIGGER
945 *s++ = htons(CDP_TRIGGER_TLV);
946 *s++ = htons(8);
947 *(ulong *)s = htonl(CONFIG_CDP_TRIGGER);
948 s += 2;
949#endif
950
951#ifdef CONFIG_CDP_POWER_CONSUMPTION
952 *s++ = htons(CDP_POWER_CONSUMPTION_TLV);
953 *s++ = htons(6);
954 *s++ = htons(CONFIG_CDP_POWER_CONSUMPTION);
955#endif
956
957 /* length of ethernet packet */
958 len = (uchar *)s - ((uchar *)NetTxPacket + ETHER_HDR_SIZE);
959 et->et_protlen = htons(len);
960
961 len = ETHER_HDR_SIZE + sizeof(CDP_SNAP_hdr);
962 chksum = CDP_compute_csum((uchar *)NetTxPacket + len, (uchar *)s - (NetTxPacket + len));
963 if (chksum == 0)
964 chksum = 0xFFFF;
965 *cp = htons(chksum);
966
967 (void) eth_send(NetTxPacket, (uchar *)s - NetTxPacket);
968 return 0;
969}
970
971static void
972CDPTimeout (void)
973{
974 CDPSeq++;
975
976 if (CDPSeq < 3) {
977 NetSetTimeout (CDP_TIMEOUT, CDPTimeout);
978 CDPSendTrigger();
979 return;
980 }
981
982 /* if not OK try again */
983 if (!CDPOK)
984 NetStartAgain();
985 else
986 NetState = NETLOOP_SUCCESS;
987}
988
989static void
990CDPDummyHandler (uchar * pkt, unsigned dest, unsigned src, unsigned len)
991{
992 /* nothing */
993}
994
995static void
996CDPHandler(const uchar * pkt, unsigned len)
997{
998 const uchar *t;
999 const ushort *ss;
1000 ushort type, tlen;
1001 uchar applid;
1002 ushort vlan, nvlan;
1003
1004 /* minimum size? */
1005 if (len < sizeof(CDP_SNAP_hdr) + 4)
1006 goto pkt_short;
1007
1008 /* check for valid CDP SNAP header */
1009 if (memcmp(pkt, CDP_SNAP_hdr, sizeof(CDP_SNAP_hdr)) != 0)
1010 return;
1011
1012 pkt += sizeof(CDP_SNAP_hdr);
1013 len -= sizeof(CDP_SNAP_hdr);
1014
1015 /* Version of CDP protocol must be >= 2 and TTL != 0 */
1016 if (pkt[0] < 0x02 || pkt[1] == 0)
1017 return;
1018
1019 /* if version is greater than 0x02 maybe we'll have a problem; output a warning */
1020 if (pkt[0] != 0x02)
1021 printf("** WARNING: CDP packet received with a protocol version %d > 2\n",
1022 pkt[0] & 0xff);
1023
1024 if (CDP_compute_csum(pkt, len) != 0)
1025 return;
1026
1027 pkt += 4;
1028 len -= 4;
1029
1030 vlan = htons(-1);
1031 nvlan = htons(-1);
1032 while (len > 0) {
1033 if (len < 4)
1034 goto pkt_short;
1035
1036 ss = (const ushort *)pkt;
1037 type = ntohs(ss[0]);
1038 tlen = ntohs(ss[1]);
1039 if (tlen > len) {
1040 goto pkt_short;
1041 }
1042
1043 pkt += tlen;
1044 len -= tlen;
1045
1046 ss += 2; /* point ss to the data of the TLV */
1047 tlen -= 4;
1048
1049 switch (type) {
1050 case CDP_DEVICE_ID_TLV:
1051 break;
1052 case CDP_ADDRESS_TLV:
1053 break;
1054 case CDP_PORT_ID_TLV:
1055 break;
1056 case CDP_CAPABILITIES_TLV:
1057 break;
1058 case CDP_VERSION_TLV:
1059 break;
1060 case CDP_PLATFORM_TLV:
1061 break;
1062 case CDP_NATIVE_VLAN_TLV:
1063 nvlan = *ss;
1064 break;
1065 case CDP_APPLIANCE_VLAN_TLV:
1066 t = (const uchar *)ss;
1067 while (tlen > 0) {
1068 if (tlen < 3)
1069 goto pkt_short;
1070
1071 applid = t[0];
1072 ss = (const ushort *)(t + 1);
1073
1074#ifdef CONFIG_CDP_APPLIANCE_VLAN_TYPE
1075 if (applid == CONFIG_CDP_APPLIANCE_VLAN_TYPE)
1076 vlan = *ss;
1077#else
1078 vlan = ntohs(*ss); /* XXX will this work; dunno */
1079#endif
1080 t += 3; tlen -= 3;
1081 }
1082 break;
1083 case CDP_TRIGGER_TLV:
1084 break;
1085 case CDP_POWER_CONSUMPTION_TLV:
1086 break;
1087 case CDP_SYSNAME_TLV:
1088 break;
1089 case CDP_SYSOBJECT_TLV:
1090 break;
1091 case CDP_MANAGEMENT_ADDRESS_TLV:
1092 break;
1093 }
1094 }
1095
1096 CDPApplianceVLAN = vlan;
1097 CDPNativeVLAN = nvlan;
1098
1099 CDPOK = 1;
1100 return;
1101
1102 pkt_short:
1103 printf("** CDP packet is too short\n");
1104 return;
1105}
1106
1107static void CDPStart(void)
1108{
1109#if defined(CONFIG_NET_MULTI)
1110 printf ("Using %s device\n", eth_get_name());
1111#endif
1112 CDPSeq = 0;
1113 CDPOK = 0;
1114
1115 CDPNativeVLAN = htons(-1);
1116 CDPApplianceVLAN = htons(-1);
1117
1118 NetSetTimeout (CDP_TIMEOUT, CDPTimeout);
1119 NetSetHandler (CDPDummyHandler);
1120
1121 CDPSendTrigger();
1122}
wdenk6e592382004-04-18 17:39:38 +00001123#endif /* CFG_CMD_CDP */
wdenka3d991b2004-04-15 21:48:45 +00001124
1125
wdenk2d966952002-10-31 22:12:35 +00001126void
wdenka3d991b2004-04-15 21:48:45 +00001127NetReceive(volatile uchar * inpkt, int len)
wdenk2d966952002-10-31 22:12:35 +00001128{
1129 Ethernet_t *et;
1130 IP_t *ip;
1131 ARP_t *arp;
1132 IPaddr_t tmp;
1133 int x;
wdenka3d991b2004-04-15 21:48:45 +00001134 uchar *pkt;
1135#if (CONFIG_COMMANDS & CFG_CMD_CDP)
1136 int iscdp;
1137#endif
1138 ushort cti = 0, vlanid = VLAN_NONE, myvlanid, mynvlanid;
wdenk2d966952002-10-31 22:12:35 +00001139
wdenka3d991b2004-04-15 21:48:45 +00001140#ifdef ET_DEBUG
1141 printf("packet received\n");
1142#endif
1143
1144 NetRxPkt = inpkt;
wdenk2d966952002-10-31 22:12:35 +00001145 NetRxPktLen = len;
wdenka3d991b2004-04-15 21:48:45 +00001146 et = (Ethernet_t *)inpkt;
1147
1148 /* too small packet? */
1149 if (len < ETHER_HDR_SIZE)
1150 return;
1151
1152#if (CONFIG_COMMANDS & CFG_CMD_CDP)
1153 /* keep track if packet is CDP */
1154 iscdp = memcmp(et->et_dest, NetCDPAddr, 6) == 0;
1155#endif
1156
1157 myvlanid = ntohs(NetOurVLAN);
1158 if (myvlanid == (ushort)-1)
1159 myvlanid = VLAN_NONE;
1160 mynvlanid = ntohs(NetOurNativeVLAN);
1161 if (mynvlanid == (ushort)-1)
1162 mynvlanid = VLAN_NONE;
wdenk2d966952002-10-31 22:12:35 +00001163
1164 x = ntohs(et->et_protlen);
1165
wdenka3d991b2004-04-15 21:48:45 +00001166#ifdef ET_DEBUG
1167 printf("packet received\n");
1168#endif
1169
wdenk2d966952002-10-31 22:12:35 +00001170 if (x < 1514) {
1171 /*
1172 * Got a 802 packet. Check the other protocol field.
1173 */
1174 x = ntohs(et->et_prot);
wdenka3d991b2004-04-15 21:48:45 +00001175
1176 ip = (IP_t *)(inpkt + E802_HDR_SIZE);
wdenk2d966952002-10-31 22:12:35 +00001177 len -= E802_HDR_SIZE;
wdenka3d991b2004-04-15 21:48:45 +00001178
1179 } else if (x != PROT_VLAN) { /* normal packet */
1180 ip = (IP_t *)(inpkt + ETHER_HDR_SIZE);
wdenk2d966952002-10-31 22:12:35 +00001181 len -= ETHER_HDR_SIZE;
wdenka3d991b2004-04-15 21:48:45 +00001182
1183 } else { /* VLAN packet */
1184 VLAN_Ethernet_t *vet = (VLAN_Ethernet_t *)et;
1185
1186#ifdef ET_DEBUG
1187 printf("VLAN packet received\n");
1188#endif
1189 /* too small packet? */
1190 if (len < VLAN_ETHER_HDR_SIZE)
1191 return;
1192
1193 /* if no VLAN active */
1194 if ((ntohs(NetOurVLAN) & VLAN_IDMASK) == VLAN_NONE
1195#if (CONFIG_COMMANDS & CFG_CMD_CDP)
1196 && iscdp == 0
1197#endif
1198 )
1199 return;
1200
1201 cti = ntohs(vet->vet_tag);
1202 vlanid = cti & VLAN_IDMASK;
1203 x = ntohs(vet->vet_type);
1204
1205 ip = (IP_t *)(inpkt + VLAN_ETHER_HDR_SIZE);
1206 len -= VLAN_ETHER_HDR_SIZE;
wdenk2d966952002-10-31 22:12:35 +00001207 }
1208
1209#ifdef ET_DEBUG
1210 printf("Receive from protocol 0x%x\n", x);
1211#endif
1212
wdenka3d991b2004-04-15 21:48:45 +00001213#if (CONFIG_COMMANDS & CFG_CMD_CDP)
1214 if (iscdp) {
1215 CDPHandler((uchar *)ip, len);
1216 return;
1217 }
1218#endif
1219
1220 if ((myvlanid & VLAN_IDMASK) != VLAN_NONE) {
1221 if (vlanid == VLAN_NONE)
1222 vlanid = (mynvlanid & VLAN_IDMASK);
1223 /* not matched? */
1224 if (vlanid != (myvlanid & VLAN_IDMASK))
1225 return;
1226 }
1227
wdenk2d966952002-10-31 22:12:35 +00001228 switch (x) {
1229
1230 case PROT_ARP:
1231 /*
1232 * We have to deal with two types of ARP packets:
wdenk8bde7f72003-06-27 21:31:46 +00001233 * - REQUEST packets will be answered by sending our
1234 * IP address - if we know it.
1235 * - REPLY packates are expected only after we asked
1236 * for the TFTP server's or the gateway's ethernet
1237 * address; so if we receive such a packet, we set
1238 * the server ethernet address
wdenk2d966952002-10-31 22:12:35 +00001239 */
1240#ifdef ET_DEBUG
wdenk4b9206e2004-03-23 22:14:11 +00001241 puts ("Got ARP\n");
wdenk2d966952002-10-31 22:12:35 +00001242#endif
1243 arp = (ARP_t *)ip;
1244 if (len < ARP_HDR_SIZE) {
1245 printf("bad length %d < %d\n", len, ARP_HDR_SIZE);
1246 return;
1247 }
1248 if (ntohs(arp->ar_hrd) != ARP_ETHER) {
1249 return;
1250 }
1251 if (ntohs(arp->ar_pro) != PROT_IP) {
1252 return;
1253 }
1254 if (arp->ar_hln != 6) {
1255 return;
1256 }
1257 if (arp->ar_pln != 4) {
1258 return;
1259 }
1260
1261 if (NetOurIP == 0) {
1262 return;
1263 }
1264
1265 if (NetReadIP(&arp->ar_data[16]) != NetOurIP) {
1266 return;
1267 }
1268
1269 switch (ntohs(arp->ar_op)) {
1270 case ARPOP_REQUEST: /* reply with our IP address */
1271#ifdef ET_DEBUG
wdenk4b9206e2004-03-23 22:14:11 +00001272 puts ("Got ARP REQUEST, return our IP\n");
wdenk2d966952002-10-31 22:12:35 +00001273#endif
wdenka3d991b2004-04-15 21:48:45 +00001274 pkt = (uchar *)et;
1275 pkt += NetSetEther(pkt, et->et_src, PROT_ARP);
wdenk2d966952002-10-31 22:12:35 +00001276 arp->ar_op = htons(ARPOP_REPLY);
1277 memcpy (&arp->ar_data[10], &arp->ar_data[0], 6);
1278 NetCopyIP(&arp->ar_data[16], &arp->ar_data[6]);
1279 memcpy (&arp->ar_data[ 0], NetOurEther, 6);
1280 NetCopyIP(&arp->ar_data[ 6], &NetOurIP);
wdenka3d991b2004-04-15 21:48:45 +00001281 (void) eth_send((uchar *)et, (pkt - (uchar *)et) + ARP_HDR_SIZE);
wdenk2d966952002-10-31 22:12:35 +00001282 return;
wdenk73a8b272003-06-05 19:27:42 +00001283
1284 case ARPOP_REPLY: /* arp reply */
1285 /* are we waiting for a reply */
1286 if (!NetArpWaitPacketIP || !NetArpWaitPacketMAC)
1287 break;
wdenk2d966952002-10-31 22:12:35 +00001288#ifdef ET_DEBUG
wdenk73a8b272003-06-05 19:27:42 +00001289 printf("Got ARP REPLY, set server/gtwy eth addr (%02x:%02x:%02x:%02x:%02x:%02x)\n",
1290 arp->ar_data[0], arp->ar_data[1],
1291 arp->ar_data[2], arp->ar_data[3],
1292 arp->ar_data[4], arp->ar_data[5]);
wdenk2d966952002-10-31 22:12:35 +00001293#endif
wdenk73a8b272003-06-05 19:27:42 +00001294
1295 tmp = NetReadIP(&arp->ar_data[6]);
1296
1297 /* matched waiting packet's address */
1298 if (tmp == NetArpWaitReplyIP) {
1299#ifdef ET_DEBUG
wdenk4b9206e2004-03-23 22:14:11 +00001300 puts ("Got it\n");
wdenk73a8b272003-06-05 19:27:42 +00001301#endif
1302 /* save address for later use */
1303 memcpy(NetArpWaitPacketMAC, &arp->ar_data[0], 6);
1304
wdenk68ceb292004-08-02 21:11:11 +00001305#ifdef CONFIG_NETCONSOLE
1306 (*packetHandler)(0,0,0,0);
1307#endif
wdenk73a8b272003-06-05 19:27:42 +00001308 /* modify header, and transmit it */
1309 memcpy(((Ethernet_t *)NetArpWaitTxPacket)->et_dest, NetArpWaitPacketMAC, 6);
1310 (void) eth_send(NetArpWaitTxPacket, NetArpWaitTxPacketSize);
1311
1312 /* no arp request pending now */
1313 NetArpWaitPacketIP = 0;
1314 NetArpWaitTxPacketSize = 0;
1315 NetArpWaitPacketMAC = NULL;
1316
1317 }
wdenk2d966952002-10-31 22:12:35 +00001318 return;
1319 default:
1320#ifdef ET_DEBUG
1321 printf("Unexpected ARP opcode 0x%x\n", ntohs(arp->ar_op));
1322#endif
1323 return;
1324 }
wdenk289f9322005-01-12 00:15:14 +00001325 break;
wdenk2d966952002-10-31 22:12:35 +00001326
1327 case PROT_RARP:
1328#ifdef ET_DEBUG
wdenk4b9206e2004-03-23 22:14:11 +00001329 puts ("Got RARP\n");
wdenk2d966952002-10-31 22:12:35 +00001330#endif
1331 arp = (ARP_t *)ip;
1332 if (len < ARP_HDR_SIZE) {
1333 printf("bad length %d < %d\n", len, ARP_HDR_SIZE);
1334 return;
1335 }
1336
1337 if ((ntohs(arp->ar_op) != RARPOP_REPLY) ||
1338 (ntohs(arp->ar_hrd) != ARP_ETHER) ||
1339 (ntohs(arp->ar_pro) != PROT_IP) ||
1340 (arp->ar_hln != 6) || (arp->ar_pln != 4)) {
1341
wdenk4b9206e2004-03-23 22:14:11 +00001342 puts ("invalid RARP header\n");
wdenk2d966952002-10-31 22:12:35 +00001343 } else {
1344 NetCopyIP(&NetOurIP, &arp->ar_data[16]);
wdenk3d3befa2004-03-14 15:06:13 +00001345 if (NetServerIP == 0)
1346 NetCopyIP(&NetServerIP, &arp->ar_data[ 6]);
wdenk2d966952002-10-31 22:12:35 +00001347 memcpy (NetServerEther, &arp->ar_data[ 0], 6);
1348
1349 (*packetHandler)(0,0,0,0);
1350 }
1351 break;
1352
1353 case PROT_IP:
1354#ifdef ET_DEBUG
wdenk4b9206e2004-03-23 22:14:11 +00001355 puts ("Got IP\n");
wdenk2d966952002-10-31 22:12:35 +00001356#endif
1357 if (len < IP_HDR_SIZE) {
1358 debug ("len bad %d < %d\n", len, IP_HDR_SIZE);
1359 return;
1360 }
1361 if (len < ntohs(ip->ip_len)) {
1362 printf("len bad %d < %d\n", len, ntohs(ip->ip_len));
1363 return;
1364 }
1365 len = ntohs(ip->ip_len);
1366#ifdef ET_DEBUG
1367 printf("len=%d, v=%02x\n", len, ip->ip_hl_v & 0xff);
1368#endif
1369 if ((ip->ip_hl_v & 0xf0) != 0x40) {
1370 return;
1371 }
1372 if (ip->ip_off & htons(0x1fff)) { /* Can't deal w/ fragments */
1373 return;
1374 }
1375 if (!NetCksumOk((uchar *)ip, IP_HDR_SIZE_NO_UDP / 2)) {
wdenk4b9206e2004-03-23 22:14:11 +00001376 puts ("checksum bad\n");
wdenk2d966952002-10-31 22:12:35 +00001377 return;
1378 }
1379 tmp = NetReadIP(&ip->ip_dst);
1380 if (NetOurIP && tmp != NetOurIP && tmp != 0xFFFFFFFF) {
1381 return;
1382 }
1383 /*
1384 * watch for ICMP host redirects
1385 *
wdenk8bde7f72003-06-27 21:31:46 +00001386 * There is no real handler code (yet). We just watch
1387 * for ICMP host redirect messages. In case anybody
1388 * sees these messages: please contact me
1389 * (wd@denx.de), or - even better - send me the
1390 * necessary fixes :-)
wdenk2d966952002-10-31 22:12:35 +00001391 *
wdenk8bde7f72003-06-27 21:31:46 +00001392 * Note: in all cases where I have seen this so far
1393 * it was a problem with the router configuration,
1394 * for instance when a router was configured in the
1395 * BOOTP reply, but the TFTP server was on the same
1396 * subnet. So this is probably a warning that your
1397 * configuration might be wrong. But I'm not really
1398 * sure if there aren't any other situations.
wdenk2d966952002-10-31 22:12:35 +00001399 */
1400 if (ip->ip_p == IPPROTO_ICMP) {
1401 ICMP_t *icmph = (ICMP_t *)&(ip->udp_src);
1402
wdenk73a8b272003-06-05 19:27:42 +00001403 switch (icmph->type) {
1404 case ICMP_REDIRECT:
wdenk2d966952002-10-31 22:12:35 +00001405 if (icmph->code != ICMP_REDIR_HOST)
1406 return;
1407 puts (" ICMP Host Redirect to ");
1408 print_IPaddr(icmph->un.gateway);
1409 putc(' ');
wdenk73a8b272003-06-05 19:27:42 +00001410 break;
1411#if (CONFIG_COMMANDS & CFG_CMD_PING)
1412 case ICMP_ECHO_REPLY:
1413 /*
1414 * IP header OK. Pass the packet to the current handler.
1415 */
1416 /* XXX point to ip packet */
1417 (*packetHandler)((uchar *)ip, 0, 0, 0);
1418 break;
1419#endif
1420 default:
1421 return;
1422 }
wdenk2d966952002-10-31 22:12:35 +00001423 } else if (ip->ip_p != IPPROTO_UDP) { /* Only UDP packets */
1424 return;
1425 }
1426
wdenk68ceb292004-08-02 21:11:11 +00001427#ifdef CONFIG_NETCONSOLE
1428 nc_input_packet((uchar *)ip +IP_HDR_SIZE,
1429 ntohs(ip->udp_dst),
1430 ntohs(ip->udp_src),
1431 ntohs(ip->udp_len) - 8);
1432#endif
wdenk2d966952002-10-31 22:12:35 +00001433 /*
1434 * IP header OK. Pass the packet to the current handler.
1435 */
1436 (*packetHandler)((uchar *)ip +IP_HDR_SIZE,
1437 ntohs(ip->udp_dst),
1438 ntohs(ip->udp_src),
1439 ntohs(ip->udp_len) - 8);
wdenk2d966952002-10-31 22:12:35 +00001440 break;
1441 }
1442}
1443
1444
1445/**********************************************************************/
1446
1447static int net_check_prereq (proto_t protocol)
1448{
1449 switch (protocol) {
wdenk6e592382004-04-18 17:39:38 +00001450 /* Fall through */
wdenk73a8b272003-06-05 19:27:42 +00001451#if (CONFIG_COMMANDS & CFG_CMD_PING)
1452 case PING:
wdenk6e592382004-04-18 17:39:38 +00001453 if (NetPingIP == 0) {
1454 puts ("*** ERROR: ping address not given\n");
1455 return (1);
1456 }
1457 goto common;
wdenk73a8b272003-06-05 19:27:42 +00001458#endif
wdenkea287de2005-04-01 00:25:43 +00001459#if (CONFIG_COMMANDS & CFG_CMD_SNTP)
1460 case SNTP:
1461 if (NetNtpServerIP == 0) {
1462 puts ("*** ERROR: NTP server address not given\n");
1463 return (1);
1464 }
1465 goto common;
1466#endif
wdenkcbd8a352004-02-24 02:00:03 +00001467#if (CONFIG_COMMANDS & CFG_CMD_NFS)
1468 case NFS:
1469#endif
wdenk68ceb292004-08-02 21:11:11 +00001470 case NETCONS:
wdenk2d966952002-10-31 22:12:35 +00001471 case TFTP:
wdenk6e592382004-04-18 17:39:38 +00001472 if (NetServerIP == 0) {
1473 puts ("*** ERROR: `serverip' not set\n");
1474 return (1);
1475 }
wdenkea287de2005-04-01 00:25:43 +00001476#if (CONFIG_COMMANDS & (CFG_CMD_PING | CFG_CMD_SNTP))
wdenk6e592382004-04-18 17:39:38 +00001477 common:
wdenk73a8b272003-06-05 19:27:42 +00001478#endif
1479
wdenk6e592382004-04-18 17:39:38 +00001480 if (NetOurIP == 0) {
1481 puts ("*** ERROR: `ipaddr' not set\n");
1482 return (1);
1483 }
1484 /* Fall through */
wdenk2d966952002-10-31 22:12:35 +00001485
1486 case DHCP:
1487 case RARP:
1488 case BOOTP:
wdenka3d991b2004-04-15 21:48:45 +00001489 case CDP:
wdenk6e592382004-04-18 17:39:38 +00001490 if (memcmp (NetOurEther, "\0\0\0\0\0\0", 6) == 0) {
wdenk2d966952002-10-31 22:12:35 +00001491#ifdef CONFIG_NET_MULTI
wdenk6e592382004-04-18 17:39:38 +00001492 extern int eth_get_dev_index (void);
1493 int num = eth_get_dev_index ();
wdenk2d966952002-10-31 22:12:35 +00001494
wdenk6e592382004-04-18 17:39:38 +00001495 switch (num) {
1496 case -1:
wdenk2d966952002-10-31 22:12:35 +00001497 puts ("*** ERROR: No ethernet found.\n");
1498 return (1);
wdenk6e592382004-04-18 17:39:38 +00001499 case 0:
wdenk2d966952002-10-31 22:12:35 +00001500 puts ("*** ERROR: `ethaddr' not set\n");
1501 break;
wdenk6e592382004-04-18 17:39:38 +00001502 default:
wdenk8bde7f72003-06-27 21:31:46 +00001503 printf ("*** ERROR: `eth%daddr' not set\n",
wdenk2d966952002-10-31 22:12:35 +00001504 num);
1505 break;
wdenk2d966952002-10-31 22:12:35 +00001506 }
wdenk6e592382004-04-18 17:39:38 +00001507
1508 NetStartAgain ();
1509 return (2);
1510#else
1511 puts ("*** ERROR: `ethaddr' not set\n");
1512 return (1);
1513#endif
1514 }
1515 /* Fall through */
1516 default:
1517 return (0);
wdenk2d966952002-10-31 22:12:35 +00001518 }
wdenk6e592382004-04-18 17:39:38 +00001519 return (0); /* OK */
wdenk2d966952002-10-31 22:12:35 +00001520}
1521/**********************************************************************/
1522
1523int
1524NetCksumOk(uchar * ptr, int len)
1525{
1526 return !((NetCksum(ptr, len) + 1) & 0xfffe);
1527}
1528
1529
1530unsigned
1531NetCksum(uchar * ptr, int len)
1532{
1533 ulong xsum;
1534
1535 xsum = 0;
1536 while (len-- > 0)
1537 xsum += *((ushort *)ptr)++;
1538 xsum = (xsum & 0xffff) + (xsum >> 16);
1539 xsum = (xsum & 0xffff) + (xsum >> 16);
1540 return (xsum & 0xffff);
1541}
1542
wdenka3d991b2004-04-15 21:48:45 +00001543int
1544NetEthHdrSize(void)
1545{
1546 ushort myvlanid;
wdenk2d966952002-10-31 22:12:35 +00001547
wdenka3d991b2004-04-15 21:48:45 +00001548 myvlanid = ntohs(NetOurVLAN);
1549 if (myvlanid == (ushort)-1)
1550 myvlanid = VLAN_NONE;
1551
1552 return ((myvlanid & VLAN_IDMASK) == VLAN_NONE) ? ETHER_HDR_SIZE : VLAN_ETHER_HDR_SIZE;
1553}
1554
1555int
wdenk2d966952002-10-31 22:12:35 +00001556NetSetEther(volatile uchar * xet, uchar * addr, uint prot)
1557{
1558 Ethernet_t *et = (Ethernet_t *)xet;
wdenka3d991b2004-04-15 21:48:45 +00001559 ushort myvlanid;
1560
1561 myvlanid = ntohs(NetOurVLAN);
1562 if (myvlanid == (ushort)-1)
1563 myvlanid = VLAN_NONE;
wdenk2d966952002-10-31 22:12:35 +00001564
1565 memcpy (et->et_dest, addr, 6);
1566 memcpy (et->et_src, NetOurEther, 6);
wdenka3d991b2004-04-15 21:48:45 +00001567 if ((myvlanid & VLAN_IDMASK) == VLAN_NONE) {
wdenk2d966952002-10-31 22:12:35 +00001568 et->et_protlen = htons(prot);
wdenka3d991b2004-04-15 21:48:45 +00001569 return ETHER_HDR_SIZE;
1570 } else {
1571 VLAN_Ethernet_t *vet = (VLAN_Ethernet_t *)xet;
wdenk2d966952002-10-31 22:12:35 +00001572
wdenka3d991b2004-04-15 21:48:45 +00001573 vet->vet_vlan_type = htons(PROT_VLAN);
1574 vet->vet_tag = htons((0 << 5) | (myvlanid & VLAN_IDMASK));
1575 vet->vet_type = htons(prot);
1576 return VLAN_ETHER_HDR_SIZE;
1577 }
1578}
wdenk2d966952002-10-31 22:12:35 +00001579
1580void
1581NetSetIP(volatile uchar * xip, IPaddr_t dest, int dport, int sport, int len)
1582{
1583 volatile IP_t *ip = (IP_t *)xip;
1584
1585 /*
1586 * If the data is an odd number of bytes, zero the
1587 * byte after the last byte so that the checksum
1588 * will work.
1589 */
1590 if (len & 1)
1591 xip[IP_HDR_SIZE + len] = 0;
1592
1593 /*
1594 * Construct an IP and UDP header.
wdenk6e592382004-04-18 17:39:38 +00001595 * (need to set no fragment bit - XXX)
wdenk2d966952002-10-31 22:12:35 +00001596 */
1597 ip->ip_hl_v = 0x45; /* IP_HDR_SIZE / 4 (not including UDP) */
1598 ip->ip_tos = 0;
1599 ip->ip_len = htons(IP_HDR_SIZE + len);
1600 ip->ip_id = htons(NetIPID++);
1601 ip->ip_off = htons(0x4000); /* No fragmentation */
1602 ip->ip_ttl = 255;
1603 ip->ip_p = 17; /* UDP */
1604 ip->ip_sum = 0;
1605 NetCopyIP((void*)&ip->ip_src, &NetOurIP); /* already in network byte order */
1606 NetCopyIP((void*)&ip->ip_dst, &dest); /* - "" - */
1607 ip->udp_src = htons(sport);
1608 ip->udp_dst = htons(dport);
1609 ip->udp_len = htons(8 + len);
1610 ip->udp_xsum = 0;
1611 ip->ip_sum = ~NetCksum((uchar *)ip, IP_HDR_SIZE_NO_UDP / 2);
1612}
1613
1614void copy_filename (uchar *dst, uchar *src, int size)
1615{
1616 if (*src && (*src == '"')) {
1617 ++src;
1618 --size;
1619 }
1620
1621 while ((--size > 0) && *src && (*src != '"')) {
1622 *dst++ = *src++;
1623 }
1624 *dst = '\0';
1625}
1626
1627#endif /* CFG_CMD_NET */
1628
1629void ip_to_string (IPaddr_t x, char *s)
1630{
wdenka3d991b2004-04-15 21:48:45 +00001631 x = ntohl (x);
1632 sprintf (s, "%d.%d.%d.%d",
1633 (int) ((x >> 24) & 0xff),
1634 (int) ((x >> 16) & 0xff),
1635 (int) ((x >> 8) & 0xff), (int) ((x >> 0) & 0xff)
wdenk6e592382004-04-18 17:39:38 +00001636 );
wdenk2d966952002-10-31 22:12:35 +00001637}
1638
wdenk73a8b272003-06-05 19:27:42 +00001639IPaddr_t string_to_ip(char *s)
wdenk2d966952002-10-31 22:12:35 +00001640{
1641 IPaddr_t addr;
wdenk73a8b272003-06-05 19:27:42 +00001642 char *e;
wdenk2d966952002-10-31 22:12:35 +00001643 int i;
1644
wdenk73a8b272003-06-05 19:27:42 +00001645 if (s == NULL)
1646 return(0);
wdenk2d966952002-10-31 22:12:35 +00001647
1648 for (addr=0, i=0; i<4; ++i) {
1649 ulong val = s ? simple_strtoul(s, &e, 10) : 0;
1650 addr <<= 8;
1651 addr |= (val & 0xFF);
1652 if (s) {
1653 s = (*e) ? e+1 : e;
1654 }
1655 }
1656
1657 return (htonl(addr));
1658}
wdenk73a8b272003-06-05 19:27:42 +00001659
wdenka3d991b2004-04-15 21:48:45 +00001660void VLAN_to_string(ushort x, char *s)
1661{
1662 x = ntohs(x);
1663
1664 if (x == (ushort)-1)
1665 x = VLAN_NONE;
1666
1667 if (x == VLAN_NONE)
1668 strcpy(s, "none");
1669 else
1670 sprintf(s, "%d", x & VLAN_IDMASK);
1671}
1672
1673ushort string_to_VLAN(char *s)
1674{
1675 ushort id;
1676
1677 if (s == NULL)
wdenkb9711de2004-04-25 13:18:40 +00001678 return htons(VLAN_NONE);
wdenka3d991b2004-04-15 21:48:45 +00001679
1680 if (*s < '0' || *s > '9')
1681 id = VLAN_NONE;
1682 else
1683 id = (ushort)simple_strtoul(s, NULL, 10);
1684
wdenkb9711de2004-04-25 13:18:40 +00001685 return htons(id);
wdenka3d991b2004-04-15 21:48:45 +00001686}
1687
wdenk73a8b272003-06-05 19:27:42 +00001688void print_IPaddr (IPaddr_t x)
1689{
wdenka3d991b2004-04-15 21:48:45 +00001690 char tmp[16];
wdenk73a8b272003-06-05 19:27:42 +00001691
wdenka3d991b2004-04-15 21:48:45 +00001692 ip_to_string (x, tmp);
wdenk73a8b272003-06-05 19:27:42 +00001693
wdenka3d991b2004-04-15 21:48:45 +00001694 puts (tmp);
wdenk73a8b272003-06-05 19:27:42 +00001695}
1696
1697IPaddr_t getenv_IPaddr (char *var)
1698{
1699 return (string_to_ip(getenv(var)));
1700}
wdenka3d991b2004-04-15 21:48:45 +00001701
1702ushort getenv_VLAN(char *var)
1703{
1704 return (string_to_VLAN(getenv(var)));
1705}