blob: 11a286b5783fd0dab7f7446fb00d835bfd4b0c97 [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
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 *
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"
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
wdenke6466f62003-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 */
stroesee0aadfb2003-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 */
wdenke6466f62003-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 };
wdenke6466f62003-06-05 19:27:42 +0000132uchar NetEtherNullAddr[6] =
133 { 0, 0, 0, 0, 0, 0 };
wdenk145d2c12004-04-15 21:48:45 +0000134#if (CONFIG_COMMANDS & CFG_CMD_CDP)
wdenk05939202004-04-18 17:39:38 +0000135uchar NetCDPAddr[6] = /* Ethernet bcast address */
wdenk145d2c12004-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
wdenk05939202004-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 */
wdenk145d2c12004-04-15 21:48:45 +0000148
wdenk2d966952002-10-31 22:12:35 +0000149char BootFile[128]; /* Boot File name */
150
wdenke6466f62003-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
wdenk145d2c12004-04-15 21:48:45 +0000157#if (CONFIG_COMMANDS & CFG_CMD_CDP)
158static void CDPStart(void);
159#endif
160
wdenkb4ad9622005-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
wdenkb8fb6192004-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 */
wdenk5d841732003-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/**********************************************************************/
wdenke6466f62003-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
wdenk05939202004-04-18 17:39:38 +0000194void ArpRequest (void)
wdenke6466f62003-06-05 19:27:42 +0000195{
196 int i;
197 volatile uchar *pkt;
wdenk05939202004-04-18 17:39:38 +0000198 ARP_t *arp;
wdenke6466f62003-06-05 19:27:42 +0000199
200#ifdef ET_DEBUG
wdenk05939202004-04-18 17:39:38 +0000201 printf ("ARP broadcast %d\n", NetArpWaitTry);
wdenke6466f62003-06-05 19:27:42 +0000202#endif
203 pkt = NetTxPacket;
204
wdenk05939202004-04-18 17:39:38 +0000205 pkt += NetSetEther (pkt, NetBcastAddr, PROT_ARP);
wdenke6466f62003-06-05 19:27:42 +0000206
wdenk05939202004-04-18 17:39:38 +0000207 arp = (ARP_t *) pkt;
wdenke6466f62003-06-05 19:27:42 +0000208
wdenk05939202004-04-18 17:39:38 +0000209 arp->ar_hrd = htons (ARP_ETHER);
210 arp->ar_pro = htons (PROT_IP);
wdenke6466f62003-06-05 19:27:42 +0000211 arp->ar_hln = 6;
212 arp->ar_pln = 4;
wdenk05939202004-04-18 17:39:38 +0000213 arp->ar_op = htons (ARPOP_REQUEST);
wdenke6466f62003-06-05 19:27:42 +0000214
wdenk05939202004-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 */
wdenke6466f62003-06-05 19:27:42 +0000219 }
220
wdenk05939202004-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 }
wdenke6466f62003-06-05 19:27:42 +0000230
wdenk05939202004-04-18 17:39:38 +0000231 NetWriteIP ((uchar *) & arp->ar_data[16], NetArpWaitReplyIP);
232 (void) eth_send (NetTxPacket, (pkt - NetTxPacket) + ARP_HDR_SIZE);
wdenke6466f62003-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
wdenke6466f62003-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 }
wdenke6466f62003-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();
wdenk05939202004-04-18 17:39:38 +0000303#ifdef CONFIG_NET_MULTI
wdenk145d2c12004-04-15 21:48:45 +0000304 eth_set_current();
wdenk05939202004-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) {
wdenkbe9c1cb2004-02-24 02:00:03 +0000325#if (CONFIG_COMMANDS & CFG_CMD_NFS)
326 case NFS:
327#endif
wdenke6466f62003-06-05 19:27:42 +0000328#if (CONFIG_COMMANDS & CFG_CMD_PING)
329 case PING:
330#endif
wdenkb4ad9622005-04-01 00:25:43 +0000331#if (CONFIG_COMMANDS & CFG_CMD_SNTP)
332 case SNTP:
333#endif
wdenkb8fb6192004-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");
wdenk145d2c12004-04-15 21:48:45 +0000339 NetOurVLAN = getenv_VLAN("vlan");
340 NetOurNativeVLAN = getenv_VLAN("nvlan");
wdenke6466f62003-06-05 19:27:42 +0000341
342 switch (protocol) {
wdenkbe9c1cb2004-02-24 02:00:03 +0000343#if (CONFIG_COMMANDS & CFG_CMD_NFS)
344 case NFS:
345#endif
wdenkb8fb6192004-08-02 21:11:11 +0000346 case NETCONS:
wdenke6466f62003-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
wdenkb4ad9622005-04-01 00:25:43 +0000355#if (CONFIG_COMMANDS & CFG_CMD_SNTP)
356 case SNTP:
357 /* nothing */
358 break;
359#endif
wdenke6466f62003-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 /*
wdenk57b2d802003-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;
wdenk4989f872004-03-14 15:06:13 +0000372 NetServerIP = getenv_IPaddr ("serverip");
wdenk145d2c12004-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 */
wdenk2582f6b2002-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 */
wdenke6466f62003-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... */
wdenkc3919532004-10-11 22:51:13 +0000407 BootpTry = 0;
wdenk2d966952002-10-31 22:12:35 +0000408 NetOurIP = 0;
wdenk4989f872004-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;
wdenke6466f62003-06-05 19:27:42 +0000423#if (CONFIG_COMMANDS & CFG_CMD_PING)
424 case PING:
425 PingStart();
426 break;
427#endif
wdenkbe9c1cb2004-02-24 02:00:03 +0000428#if (CONFIG_COMMANDS & CFG_CMD_NFS)
429 case NFS:
430 NfsStart();
431 break;
432#endif
wdenk145d2c12004-04-15 21:48:45 +0000433#if (CONFIG_COMMANDS & CFG_CMD_CDP)
434 case CDP:
435 CDPStart();
436 break;
437#endif
wdenkb8fb6192004-08-02 21:11:11 +0000438#ifdef CONFIG_NETCONSOLE
439 case NETCONS:
440 NcStart();
441 break;
442#endif
wdenkb4ad9622005-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
wdenk49c3f672003-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 /*
wdenk9c53f402003-10-15 23:53:47 +0000459 * Echo the inverted link state to the fault LED.
wdenk49c3f672003-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
471 * someone sets `NetQuit'.
472 */
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()) {
wdenk57b2d802003-06-27 21:31:46 +0000491 eth_halt();
wdenk42c05472004-03-23 22:14:11 +0000492 puts ("\nAbort\n");
wdenk2582f6b2002-11-11 21:14:20 +0000493 return (-1);
wdenk2d966952002-10-31 22:12:35 +0000494 }
495
wdenke6466f62003-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 */
wdenk5d841732003-08-17 18:55:18 +0000502 if (timeHandler && ((get_timer(0) - timeStart) > timeDelta)) {
wdenk2d966952002-10-31 22:12:35 +0000503 thand_f *x;
504
wdenk49c3f672003-10-08 22:33:00 +0000505#if defined(CONFIG_MII) || (CONFIG_COMMANDS & CFG_CMD_MII)
506#if defined(CFG_FAULT_ECHO_LINK_DOWN) && defined(CONFIG_STATUS_LED) && defined(STATUS_LED_RED)
507 /*
wdenk9c53f402003-10-15 23:53:47 +0000508 * Echo the inverted link state to the fault LED.
wdenk49c3f672003-10-08 22:33:00 +0000509 */
510 if(miiphy_link(CFG_FAULT_MII_ADDR)) {
511 status_led_set (STATUS_LED_RED, STATUS_LED_OFF);
512 } else {
513 status_led_set (STATUS_LED_RED, STATUS_LED_ON);
514 }
515#endif /* CFG_FAULT_ECHO_LINK_DOWN, ... */
516#endif /* CONFIG_MII, ... */
wdenk2d966952002-10-31 22:12:35 +0000517 x = timeHandler;
518 timeHandler = (thand_f *)0;
519 (*x)();
520 }
521
522
523 switch (NetState) {
524
525 case NETLOOP_RESTART:
526#ifdef CONFIG_NET_MULTI
527 NetRestarted = 1;
528#endif
529 goto restart;
530
531 case NETLOOP_SUCCESS:
532 if (NetBootFileXferSize > 0) {
533 char buf[10];
534 printf("Bytes transferred = %ld (%lx hex)\n",
535 NetBootFileXferSize,
536 NetBootFileXferSize);
537 sprintf(buf, "%lx", NetBootFileXferSize);
538 setenv("filesize", buf);
wdenk145d2c12004-04-15 21:48:45 +0000539
540 sprintf(buf, "%lX", (unsigned long)load_addr);
541 setenv("fileaddr", buf);
wdenk2d966952002-10-31 22:12:35 +0000542 }
543 eth_halt();
544 return NetBootFileXferSize;
545
546 case NETLOOP_FAIL:
wdenk2582f6b2002-11-11 21:14:20 +0000547 return (-1);
wdenk2d966952002-10-31 22:12:35 +0000548 }
549 }
550}
551
552/**********************************************************************/
553
554static void
555startAgainTimeout(void)
556{
557 NetState = NETLOOP_RESTART;
558}
559
560static void
561startAgainHandler(uchar * pkt, unsigned dest, unsigned src, unsigned len)
562{
563 /* Totally ignore the packet */
564}
565
wdenk05939202004-04-18 17:39:38 +0000566void NetStartAgain (void)
wdenk2d966952002-10-31 22:12:35 +0000567{
wdenk05939202004-04-18 17:39:38 +0000568#ifdef CONFIG_NET_MULTI
wdenk145d2c12004-04-15 21:48:45 +0000569 DECLARE_GLOBAL_DATA_PTR;
wdenk05939202004-04-18 17:39:38 +0000570#endif
571 char *nretry;
572 int noretry = 0, once = 0;
wdenk145d2c12004-04-15 21:48:45 +0000573
wdenk05939202004-04-18 17:39:38 +0000574 if ((nretry = getenv ("netretry")) != NULL) {
575 noretry = (strcmp (nretry, "no") == 0);
576 once = (strcmp (nretry, "once") == 0);
577 }
578 if (noretry) {
579 eth_halt ();
wdenk145d2c12004-04-15 21:48:45 +0000580 NetState = NETLOOP_FAIL;
581 return;
582 }
wdenk2d966952002-10-31 22:12:35 +0000583#ifndef CONFIG_NET_MULTI
wdenk05939202004-04-18 17:39:38 +0000584 NetSetTimeout (10 * CFG_HZ, startAgainTimeout);
585 NetSetHandler (startAgainHandler);
586#else /* !CONFIG_NET_MULTI*/
587 eth_halt ();
588 eth_try_another (!NetRestarted);
589 eth_init (gd->bd);
590 if (NetRestartWrap) {
wdenk2d966952002-10-31 22:12:35 +0000591 NetRestartWrap = 0;
wdenk05939202004-04-18 17:39:38 +0000592 if (NetDevExists && !once) {
593 NetSetTimeout (10 * CFG_HZ, startAgainTimeout);
594 NetSetHandler (startAgainHandler);
595 } else {
wdenk2d966952002-10-31 22:12:35 +0000596 NetState = NETLOOP_FAIL;
597 }
wdenk05939202004-04-18 17:39:38 +0000598 } else {
wdenk2d966952002-10-31 22:12:35 +0000599 NetState = NETLOOP_RESTART;
600 }
wdenk05939202004-04-18 17:39:38 +0000601#endif /* CONFIG_NET_MULTI */
wdenk2d966952002-10-31 22:12:35 +0000602}
603
604/**********************************************************************/
605/*
606 * Miscelaneous bits.
607 */
608
609void
610NetSetHandler(rxhand_f * f)
611{
612 packetHandler = f;
613}
614
615
616void
wdenkd6a3dd42004-10-09 21:56:21 +0000617NetSetTimeout(ulong iv, thand_f * f)
wdenk2d966952002-10-31 22:12:35 +0000618{
619 if (iv == 0) {
620 timeHandler = (thand_f *)0;
621 } else {
622 timeHandler = f;
wdenk5d841732003-08-17 18:55:18 +0000623 timeStart = get_timer(0);
624 timeDelta = iv;
wdenk2d966952002-10-31 22:12:35 +0000625 }
626}
627
628
629void
630NetSendPacket(volatile uchar * pkt, int len)
631{
632 (void) eth_send(pkt, len);
633}
634
wdenke6466f62003-06-05 19:27:42 +0000635int
636NetSendUDPPacket(uchar *ether, IPaddr_t dest, int dport, int sport, int len)
637{
wdenk145d2c12004-04-15 21:48:45 +0000638 uchar *pkt;
639
wdenke6466f62003-06-05 19:27:42 +0000640 /* convert to new style broadcast */
641 if (dest == 0)
642 dest = 0xFFFFFFFF;
643
644 /* if broadcast, make the ether address a broadcast and don't do ARP */
645 if (dest == 0xFFFFFFFF)
646 ether = NetBcastAddr;
647
648 /* if MAC address was not discovered yet, save the packet and do an ARP request */
649 if (memcmp(ether, NetEtherNullAddr, 6) == 0) {
650
651#ifdef ET_DEBUG
652 printf("sending ARP for %08lx\n", dest);
653#endif
wdenke6466f62003-06-05 19:27:42 +0000654 NetArpWaitPacketIP = dest;
655 NetArpWaitPacketMAC = ether;
wdenk145d2c12004-04-15 21:48:45 +0000656
657 pkt = NetArpWaitTxPacket;
658 pkt += NetSetEther (pkt, NetArpWaitPacketMAC, PROT_IP);
659
660 NetSetIP (pkt, dest, dport, sport, len);
661 memcpy(pkt + IP_HDR_SIZE, (uchar *)NetTxPacket + (pkt - (uchar *)NetArpWaitTxPacket) + IP_HDR_SIZE, len);
wdenke6466f62003-06-05 19:27:42 +0000662
663 /* size of the waiting packet */
wdenk145d2c12004-04-15 21:48:45 +0000664 NetArpWaitTxPacketSize = (pkt - NetArpWaitTxPacket) + IP_HDR_SIZE + len;
wdenke6466f62003-06-05 19:27:42 +0000665
666 /* and do the ARP request */
667 NetArpWaitTry = 1;
668 NetArpWaitTimerStart = get_timer(0);
669 ArpRequest();
670 return 1; /* waiting */
671 }
672
673#ifdef ET_DEBUG
674 printf("sending UDP to %08lx/%02x:%02x:%02x:%02x:%02x:%02x\n",
wdenk05939202004-04-18 17:39:38 +0000675 dest, ether[0], ether[1], ether[2], ether[3], ether[4], ether[5]);
wdenke6466f62003-06-05 19:27:42 +0000676#endif
677
wdenk145d2c12004-04-15 21:48:45 +0000678 pkt = (uchar *)NetTxPacket;
679 pkt += NetSetEther (pkt, ether, PROT_IP);
680 NetSetIP (pkt, dest, dport, sport, len);
681 (void) eth_send(NetTxPacket, (pkt - NetTxPacket) + IP_HDR_SIZE + len);
wdenke6466f62003-06-05 19:27:42 +0000682
wdenk05939202004-04-18 17:39:38 +0000683 return 0; /* transmitted */
wdenke6466f62003-06-05 19:27:42 +0000684}
685
686#if (CONFIG_COMMANDS & CFG_CMD_PING)
687static ushort PingSeqNo;
688
689int PingSend(void)
690{
691 static uchar mac[6];
692 volatile IP_t *ip;
693 volatile ushort *s;
wdenk145d2c12004-04-15 21:48:45 +0000694 uchar *pkt;
wdenke6466f62003-06-05 19:27:42 +0000695
696 /* XXX always send arp request */
697
698 memcpy(mac, NetEtherNullAddr, 6);
699
700#ifdef ET_DEBUG
701 printf("sending ARP for %08lx\n", NetPingIP);
702#endif
703
704 NetArpWaitPacketIP = NetPingIP;
705 NetArpWaitPacketMAC = mac;
706
wdenk145d2c12004-04-15 21:48:45 +0000707 pkt = NetArpWaitTxPacket;
708 pkt += NetSetEther(pkt, mac, PROT_IP);
wdenke6466f62003-06-05 19:27:42 +0000709
wdenk145d2c12004-04-15 21:48:45 +0000710 ip = (volatile IP_t *)pkt;
wdenke6466f62003-06-05 19:27:42 +0000711
712 /*
713 * Construct an IP and ICMP header. (need to set no fragment bit - XXX)
714 */
715 ip->ip_hl_v = 0x45; /* IP_HDR_SIZE / 4 (not including UDP) */
716 ip->ip_tos = 0;
717 ip->ip_len = htons(IP_HDR_SIZE_NO_UDP + 8);
718 ip->ip_id = htons(NetIPID++);
719 ip->ip_off = htons(0x4000); /* No fragmentation */
720 ip->ip_ttl = 255;
721 ip->ip_p = 0x01; /* ICMP */
722 ip->ip_sum = 0;
723 NetCopyIP((void*)&ip->ip_src, &NetOurIP); /* already in network byte order */
724 NetCopyIP((void*)&ip->ip_dst, &NetPingIP); /* - "" - */
725 ip->ip_sum = ~NetCksum((uchar *)ip, IP_HDR_SIZE_NO_UDP / 2);
726
727 s = &ip->udp_src; /* XXX ICMP starts here */
728 s[0] = htons(0x0800); /* echo-request, code */
729 s[1] = 0; /* checksum */
730 s[2] = 0; /* identifier */
731 s[3] = htons(PingSeqNo++); /* sequence number */
732 s[1] = ~NetCksum((uchar *)s, 8/2);
733
734 /* size of the waiting packet */
wdenk145d2c12004-04-15 21:48:45 +0000735 NetArpWaitTxPacketSize = (pkt - NetArpWaitTxPacket) + IP_HDR_SIZE_NO_UDP + 8;
wdenke6466f62003-06-05 19:27:42 +0000736
737 /* and do the ARP request */
738 NetArpWaitTry = 1;
739 NetArpWaitTimerStart = get_timer(0);
740 ArpRequest();
741 return 1; /* waiting */
742}
743
744static void
745PingTimeout (void)
746{
747 eth_halt();
748 NetState = NETLOOP_FAIL; /* we did not get the reply */
749}
750
751static void
752PingHandler (uchar * pkt, unsigned dest, unsigned src, unsigned len)
753{
754 IPaddr_t tmp;
755 volatile IP_t *ip = (volatile IP_t *)pkt;
756
757 tmp = NetReadIP((void *)&ip->ip_src);
758 if (tmp != NetPingIP)
759 return;
760
761 NetState = NETLOOP_SUCCESS;
762}
763
764static void PingStart(void)
765{
wdenk145d2c12004-04-15 21:48:45 +0000766#if defined(CONFIG_NET_MULTI)
767 printf ("Using %s device\n", eth_get_name());
wdenk05939202004-04-18 17:39:38 +0000768#endif /* CONFIG_NET_MULTI */
wdenke6466f62003-06-05 19:27:42 +0000769 NetSetTimeout (10 * CFG_HZ, PingTimeout);
770 NetSetHandler (PingHandler);
771
772 PingSend();
773}
wdenk05939202004-04-18 17:39:38 +0000774#endif /* CFG_CMD_PING */
wdenk145d2c12004-04-15 21:48:45 +0000775
776#if (CONFIG_COMMANDS & CFG_CMD_CDP)
777
778#define CDP_DEVICE_ID_TLV 0x0001
779#define CDP_ADDRESS_TLV 0x0002
780#define CDP_PORT_ID_TLV 0x0003
781#define CDP_CAPABILITIES_TLV 0x0004
782#define CDP_VERSION_TLV 0x0005
783#define CDP_PLATFORM_TLV 0x0006
784#define CDP_NATIVE_VLAN_TLV 0x000a
785#define CDP_APPLIANCE_VLAN_TLV 0x000e
786#define CDP_TRIGGER_TLV 0x000f
787#define CDP_POWER_CONSUMPTION_TLV 0x0010
788#define CDP_SYSNAME_TLV 0x0014
789#define CDP_SYSOBJECT_TLV 0x0015
790#define CDP_MANAGEMENT_ADDRESS_TLV 0x0016
791
792#define CDP_TIMEOUT (CFG_HZ/4) /* one packet every 250ms */
793
794static int CDPSeq;
795static int CDPOK;
796
797ushort CDPNativeVLAN;
798ushort CDPApplianceVLAN;
799
800static const uchar CDP_SNAP_hdr[8] = { 0xAA, 0xAA, 0x03, 0x00, 0x00, 0x0C, 0x20, 0x00 };
801
802static ushort CDP_compute_csum(const uchar *buff, ushort len)
803{
804 ushort csum;
805 int odd;
806 ulong result = 0;
807 ushort leftover;
808
809 if (len > 0) {
810 odd = 1 & (ulong)buff;
811 if (odd) {
812 result = *buff << 8;
813 len--;
814 buff++;
815 }
816 while (len > 1) {
817 result += *((const ushort *)buff)++;
818 if (result & 0x80000000)
819 result = (result & 0xFFFF) + (result >> 16);
820 len -= 2;
821 }
822 if (len) {
823 leftover = (signed short)(*(const signed char *)buff);
824 /* * XXX CISCO SUCKS big time! (and blows too) */
825 result = (result & 0xffff0000) | ((result + leftover) & 0x0000ffff);
826 }
827 while (result >> 16)
828 result = (result & 0xFFFF) + (result >> 16);
829
830 if (odd)
831 result = ((result >> 8) & 0xff) | ((result & 0xff) << 8);
832 }
833
834 /* add up 16-bit and 17-bit words for 17+c bits */
835 result = (result & 0xffff) + (result >> 16);
836 /* add up 16-bit and 2-bit for 16+c bit */
837 result = (result & 0xffff) + (result >> 16);
838 /* add up carry.. */
839 result = (result & 0xffff) + (result >> 16);
840
841 /* negate */
842 csum = ~(ushort)result;
843
844 /* run time endian detection */
845 if (csum != htons(csum)) /* little endian */
846 csum = htons(csum);
847
848 return csum;
849}
850
851int CDPSendTrigger(void)
852{
853 volatile uchar *pkt;
854 volatile ushort *s;
855 volatile ushort *cp;
856 Ethernet_t *et;
wdenk145d2c12004-04-15 21:48:45 +0000857 int len;
858 ushort chksum;
wdenk05939202004-04-18 17:39:38 +0000859#if defined(CONFIG_CDP_DEVICE_ID) || defined(CONFIG_CDP_PORT_ID) || \
860 defined(CONFIG_CDP_VERSION) || defined(CONFIG_CDP_PLATFORM)
861 char buf[32];
862#endif
wdenk145d2c12004-04-15 21:48:45 +0000863
864 pkt = NetTxPacket;
865 et = (Ethernet_t *)pkt;
866
867 /* NOTE: trigger sent not on any VLAN */
868
869 /* form ethernet header */
870 memcpy(et->et_dest, NetCDPAddr, 6);
871 memcpy(et->et_src, NetOurEther, 6);
872
873 pkt += ETHER_HDR_SIZE;
874
875 /* SNAP header */
876 memcpy((uchar *)pkt, CDP_SNAP_hdr, sizeof(CDP_SNAP_hdr));
877 pkt += sizeof(CDP_SNAP_hdr);
878
879 /* CDP header */
880 *pkt++ = 0x02; /* CDP version 2 */
881 *pkt++ = 180; /* TTL */
882 s = (volatile ushort *)pkt;
883 cp = s;
884 *s++ = htons(0); /* checksum (0 for later calculation) */
885
886 /* CDP fields */
887#ifdef CONFIG_CDP_DEVICE_ID
888 *s++ = htons(CDP_DEVICE_ID_TLV);
889 *s++ = htons(CONFIG_CDP_DEVICE_ID);
890 memset(buf, 0, sizeof(buf));
891 sprintf(buf, CONFIG_CDP_DEVICE_ID_PREFIX "%02X%02X%02X%02X%02X%02X",
892 NetOurEther[0] & 0xff, NetOurEther[1] & 0xff,
893 NetOurEther[2] & 0xff, NetOurEther[3] & 0xff,
894 NetOurEther[4] & 0xff, NetOurEther[5] & 0xff);
895 memcpy((uchar *)s, buf, 16);
896 s += 16 / 2;
897#endif
898
899#ifdef CONFIG_CDP_PORT_ID
900 *s++ = htons(CDP_PORT_ID_TLV);
901 memset(buf, 0, sizeof(buf));
902 sprintf(buf, CONFIG_CDP_PORT_ID, eth_get_dev_index());
903 len = strlen(buf);
904 if (len & 1) /* make it even */
905 len++;
906 *s++ = htons(len + 4);
907 memcpy((uchar *)s, buf, len);
908 s += len / 2;
909#endif
910
911#ifdef CONFIG_CDP_CAPABILITIES
912 *s++ = htons(CDP_CAPABILITIES_TLV);
913 *s++ = htons(8);
914 *(ulong *)s = htonl(CONFIG_CDP_CAPABILITIES);
915 s += 2;
916#endif
917
918#ifdef CONFIG_CDP_VERSION
919 *s++ = htons(CDP_VERSION_TLV);
920 memset(buf, 0, sizeof(buf));
921 strcpy(buf, CONFIG_CDP_VERSION);
922 len = strlen(buf);
923 if (len & 1) /* make it even */
924 len++;
925 *s++ = htons(len + 4);
926 memcpy((uchar *)s, buf, len);
927 s += len / 2;
928#endif
929
930#ifdef CONFIG_CDP_PLATFORM
931 *s++ = htons(CDP_PLATFORM_TLV);
932 memset(buf, 0, sizeof(buf));
933 strcpy(buf, CONFIG_CDP_PLATFORM);
934 len = strlen(buf);
935 if (len & 1) /* make it even */
936 len++;
937 *s++ = htons(len + 4);
938 memcpy((uchar *)s, buf, len);
939 s += len / 2;
940#endif
941
942#ifdef CONFIG_CDP_TRIGGER
943 *s++ = htons(CDP_TRIGGER_TLV);
944 *s++ = htons(8);
945 *(ulong *)s = htonl(CONFIG_CDP_TRIGGER);
946 s += 2;
947#endif
948
949#ifdef CONFIG_CDP_POWER_CONSUMPTION
950 *s++ = htons(CDP_POWER_CONSUMPTION_TLV);
951 *s++ = htons(6);
952 *s++ = htons(CONFIG_CDP_POWER_CONSUMPTION);
953#endif
954
955 /* length of ethernet packet */
956 len = (uchar *)s - ((uchar *)NetTxPacket + ETHER_HDR_SIZE);
957 et->et_protlen = htons(len);
958
959 len = ETHER_HDR_SIZE + sizeof(CDP_SNAP_hdr);
960 chksum = CDP_compute_csum((uchar *)NetTxPacket + len, (uchar *)s - (NetTxPacket + len));
961 if (chksum == 0)
962 chksum = 0xFFFF;
963 *cp = htons(chksum);
964
965 (void) eth_send(NetTxPacket, (uchar *)s - NetTxPacket);
966 return 0;
967}
968
969static void
970CDPTimeout (void)
971{
972 CDPSeq++;
973
974 if (CDPSeq < 3) {
975 NetSetTimeout (CDP_TIMEOUT, CDPTimeout);
976 CDPSendTrigger();
977 return;
978 }
979
980 /* if not OK try again */
981 if (!CDPOK)
982 NetStartAgain();
983 else
984 NetState = NETLOOP_SUCCESS;
985}
986
987static void
988CDPDummyHandler (uchar * pkt, unsigned dest, unsigned src, unsigned len)
989{
990 /* nothing */
991}
992
993static void
994CDPHandler(const uchar * pkt, unsigned len)
995{
996 const uchar *t;
997 const ushort *ss;
998 ushort type, tlen;
999 uchar applid;
1000 ushort vlan, nvlan;
1001
1002 /* minimum size? */
1003 if (len < sizeof(CDP_SNAP_hdr) + 4)
1004 goto pkt_short;
1005
1006 /* check for valid CDP SNAP header */
1007 if (memcmp(pkt, CDP_SNAP_hdr, sizeof(CDP_SNAP_hdr)) != 0)
1008 return;
1009
1010 pkt += sizeof(CDP_SNAP_hdr);
1011 len -= sizeof(CDP_SNAP_hdr);
1012
1013 /* Version of CDP protocol must be >= 2 and TTL != 0 */
1014 if (pkt[0] < 0x02 || pkt[1] == 0)
1015 return;
1016
1017 /* if version is greater than 0x02 maybe we'll have a problem; output a warning */
1018 if (pkt[0] != 0x02)
1019 printf("** WARNING: CDP packet received with a protocol version %d > 2\n",
1020 pkt[0] & 0xff);
1021
1022 if (CDP_compute_csum(pkt, len) != 0)
1023 return;
1024
1025 pkt += 4;
1026 len -= 4;
1027
1028 vlan = htons(-1);
1029 nvlan = htons(-1);
1030 while (len > 0) {
1031 if (len < 4)
1032 goto pkt_short;
1033
1034 ss = (const ushort *)pkt;
1035 type = ntohs(ss[0]);
1036 tlen = ntohs(ss[1]);
1037 if (tlen > len) {
1038 goto pkt_short;
1039 }
1040
1041 pkt += tlen;
1042 len -= tlen;
1043
1044 ss += 2; /* point ss to the data of the TLV */
1045 tlen -= 4;
1046
1047 switch (type) {
1048 case CDP_DEVICE_ID_TLV:
1049 break;
1050 case CDP_ADDRESS_TLV:
1051 break;
1052 case CDP_PORT_ID_TLV:
1053 break;
1054 case CDP_CAPABILITIES_TLV:
1055 break;
1056 case CDP_VERSION_TLV:
1057 break;
1058 case CDP_PLATFORM_TLV:
1059 break;
1060 case CDP_NATIVE_VLAN_TLV:
1061 nvlan = *ss;
1062 break;
1063 case CDP_APPLIANCE_VLAN_TLV:
1064 t = (const uchar *)ss;
1065 while (tlen > 0) {
1066 if (tlen < 3)
1067 goto pkt_short;
1068
1069 applid = t[0];
1070 ss = (const ushort *)(t + 1);
1071
1072#ifdef CONFIG_CDP_APPLIANCE_VLAN_TYPE
1073 if (applid == CONFIG_CDP_APPLIANCE_VLAN_TYPE)
1074 vlan = *ss;
1075#else
1076 vlan = ntohs(*ss); /* XXX will this work; dunno */
1077#endif
1078 t += 3; tlen -= 3;
1079 }
1080 break;
1081 case CDP_TRIGGER_TLV:
1082 break;
1083 case CDP_POWER_CONSUMPTION_TLV:
1084 break;
1085 case CDP_SYSNAME_TLV:
1086 break;
1087 case CDP_SYSOBJECT_TLV:
1088 break;
1089 case CDP_MANAGEMENT_ADDRESS_TLV:
1090 break;
1091 }
1092 }
1093
1094 CDPApplianceVLAN = vlan;
1095 CDPNativeVLAN = nvlan;
1096
1097 CDPOK = 1;
1098 return;
1099
1100 pkt_short:
1101 printf("** CDP packet is too short\n");
1102 return;
1103}
1104
1105static void CDPStart(void)
1106{
1107#if defined(CONFIG_NET_MULTI)
1108 printf ("Using %s device\n", eth_get_name());
wdenke6466f62003-06-05 19:27:42 +00001109#endif
wdenk145d2c12004-04-15 21:48:45 +00001110 CDPSeq = 0;
1111 CDPOK = 0;
1112
1113 CDPNativeVLAN = htons(-1);
1114 CDPApplianceVLAN = htons(-1);
1115
1116 NetSetTimeout (CDP_TIMEOUT, CDPTimeout);
1117 NetSetHandler (CDPDummyHandler);
1118
1119 CDPSendTrigger();
1120}
wdenk05939202004-04-18 17:39:38 +00001121#endif /* CFG_CMD_CDP */
wdenk145d2c12004-04-15 21:48:45 +00001122
wdenk2d966952002-10-31 22:12:35 +00001123
1124void
wdenk145d2c12004-04-15 21:48:45 +00001125NetReceive(volatile uchar * inpkt, int len)
wdenk2d966952002-10-31 22:12:35 +00001126{
1127 Ethernet_t *et;
1128 IP_t *ip;
1129 ARP_t *arp;
1130 IPaddr_t tmp;
1131 int x;
wdenk145d2c12004-04-15 21:48:45 +00001132 uchar *pkt;
1133#if (CONFIG_COMMANDS & CFG_CMD_CDP)
1134 int iscdp;
1135#endif
1136 ushort cti = 0, vlanid = VLAN_NONE, myvlanid, mynvlanid;
wdenk2d966952002-10-31 22:12:35 +00001137
wdenk145d2c12004-04-15 21:48:45 +00001138#ifdef ET_DEBUG
1139 printf("packet received\n");
1140#endif
1141
1142 NetRxPkt = inpkt;
wdenk2d966952002-10-31 22:12:35 +00001143 NetRxPktLen = len;
wdenk145d2c12004-04-15 21:48:45 +00001144 et = (Ethernet_t *)inpkt;
1145
1146 /* too small packet? */
1147 if (len < ETHER_HDR_SIZE)
1148 return;
1149
1150#if (CONFIG_COMMANDS & CFG_CMD_CDP)
1151 /* keep track if packet is CDP */
1152 iscdp = memcmp(et->et_dest, NetCDPAddr, 6) == 0;
1153#endif
1154
1155 myvlanid = ntohs(NetOurVLAN);
1156 if (myvlanid == (ushort)-1)
1157 myvlanid = VLAN_NONE;
1158 mynvlanid = ntohs(NetOurNativeVLAN);
1159 if (mynvlanid == (ushort)-1)
1160 mynvlanid = VLAN_NONE;
wdenk2d966952002-10-31 22:12:35 +00001161
1162 x = ntohs(et->et_protlen);
1163
wdenk145d2c12004-04-15 21:48:45 +00001164#ifdef ET_DEBUG
1165 printf("packet received\n");
1166#endif
1167
wdenk2d966952002-10-31 22:12:35 +00001168 if (x < 1514) {
1169 /*
1170 * Got a 802 packet. Check the other protocol field.
1171 */
1172 x = ntohs(et->et_prot);
wdenk145d2c12004-04-15 21:48:45 +00001173
1174 ip = (IP_t *)(inpkt + E802_HDR_SIZE);
wdenk2d966952002-10-31 22:12:35 +00001175 len -= E802_HDR_SIZE;
wdenk145d2c12004-04-15 21:48:45 +00001176
1177 } else if (x != PROT_VLAN) { /* normal packet */
1178 ip = (IP_t *)(inpkt + ETHER_HDR_SIZE);
wdenk2d966952002-10-31 22:12:35 +00001179 len -= ETHER_HDR_SIZE;
wdenk145d2c12004-04-15 21:48:45 +00001180
1181 } else { /* VLAN packet */
1182 VLAN_Ethernet_t *vet = (VLAN_Ethernet_t *)et;
1183
1184#ifdef ET_DEBUG
1185 printf("VLAN packet received\n");
1186#endif
1187 /* too small packet? */
1188 if (len < VLAN_ETHER_HDR_SIZE)
1189 return;
1190
1191 /* if no VLAN active */
1192 if ((ntohs(NetOurVLAN) & VLAN_IDMASK) == VLAN_NONE
1193#if (CONFIG_COMMANDS & CFG_CMD_CDP)
1194 && iscdp == 0
1195#endif
1196 )
1197 return;
1198
1199 cti = ntohs(vet->vet_tag);
1200 vlanid = cti & VLAN_IDMASK;
1201 x = ntohs(vet->vet_type);
1202
1203 ip = (IP_t *)(inpkt + VLAN_ETHER_HDR_SIZE);
1204 len -= VLAN_ETHER_HDR_SIZE;
wdenk2d966952002-10-31 22:12:35 +00001205 }
1206
1207#ifdef ET_DEBUG
1208 printf("Receive from protocol 0x%x\n", x);
1209#endif
1210
wdenk145d2c12004-04-15 21:48:45 +00001211#if (CONFIG_COMMANDS & CFG_CMD_CDP)
1212 if (iscdp) {
1213 CDPHandler((uchar *)ip, len);
1214 return;
1215 }
1216#endif
1217
1218 if ((myvlanid & VLAN_IDMASK) != VLAN_NONE) {
1219 if (vlanid == VLAN_NONE)
1220 vlanid = (mynvlanid & VLAN_IDMASK);
1221 /* not matched? */
1222 if (vlanid != (myvlanid & VLAN_IDMASK))
1223 return;
1224 }
1225
wdenk2d966952002-10-31 22:12:35 +00001226 switch (x) {
1227
1228 case PROT_ARP:
1229 /*
1230 * We have to deal with two types of ARP packets:
wdenk57b2d802003-06-27 21:31:46 +00001231 * - REQUEST packets will be answered by sending our
1232 * IP address - if we know it.
1233 * - REPLY packates are expected only after we asked
1234 * for the TFTP server's or the gateway's ethernet
1235 * address; so if we receive such a packet, we set
1236 * the server ethernet address
wdenk2d966952002-10-31 22:12:35 +00001237 */
1238#ifdef ET_DEBUG
wdenk42c05472004-03-23 22:14:11 +00001239 puts ("Got ARP\n");
wdenk2d966952002-10-31 22:12:35 +00001240#endif
1241 arp = (ARP_t *)ip;
1242 if (len < ARP_HDR_SIZE) {
1243 printf("bad length %d < %d\n", len, ARP_HDR_SIZE);
1244 return;
1245 }
1246 if (ntohs(arp->ar_hrd) != ARP_ETHER) {
1247 return;
1248 }
1249 if (ntohs(arp->ar_pro) != PROT_IP) {
1250 return;
1251 }
1252 if (arp->ar_hln != 6) {
1253 return;
1254 }
1255 if (arp->ar_pln != 4) {
1256 return;
1257 }
1258
1259 if (NetOurIP == 0) {
1260 return;
1261 }
1262
1263 if (NetReadIP(&arp->ar_data[16]) != NetOurIP) {
1264 return;
1265 }
1266
1267 switch (ntohs(arp->ar_op)) {
1268 case ARPOP_REQUEST: /* reply with our IP address */
1269#ifdef ET_DEBUG
wdenk42c05472004-03-23 22:14:11 +00001270 puts ("Got ARP REQUEST, return our IP\n");
wdenk2d966952002-10-31 22:12:35 +00001271#endif
wdenk145d2c12004-04-15 21:48:45 +00001272 pkt = (uchar *)et;
1273 pkt += NetSetEther(pkt, et->et_src, PROT_ARP);
wdenk2d966952002-10-31 22:12:35 +00001274 arp->ar_op = htons(ARPOP_REPLY);
1275 memcpy (&arp->ar_data[10], &arp->ar_data[0], 6);
1276 NetCopyIP(&arp->ar_data[16], &arp->ar_data[6]);
1277 memcpy (&arp->ar_data[ 0], NetOurEther, 6);
1278 NetCopyIP(&arp->ar_data[ 6], &NetOurIP);
wdenk145d2c12004-04-15 21:48:45 +00001279 (void) eth_send((uchar *)et, (pkt - (uchar *)et) + ARP_HDR_SIZE);
wdenk2d966952002-10-31 22:12:35 +00001280 return;
wdenke6466f62003-06-05 19:27:42 +00001281
1282 case ARPOP_REPLY: /* arp reply */
1283 /* are we waiting for a reply */
1284 if (!NetArpWaitPacketIP || !NetArpWaitPacketMAC)
1285 break;
1286#ifdef ET_DEBUG
1287 printf("Got ARP REPLY, set server/gtwy eth addr (%02x:%02x:%02x:%02x:%02x:%02x)\n",
1288 arp->ar_data[0], arp->ar_data[1],
1289 arp->ar_data[2], arp->ar_data[3],
1290 arp->ar_data[4], arp->ar_data[5]);
1291#endif
1292
1293 tmp = NetReadIP(&arp->ar_data[6]);
1294
1295 /* matched waiting packet's address */
1296 if (tmp == NetArpWaitReplyIP) {
wdenk2d966952002-10-31 22:12:35 +00001297#ifdef ET_DEBUG
wdenk42c05472004-03-23 22:14:11 +00001298 puts ("Got it\n");
wdenk2d966952002-10-31 22:12:35 +00001299#endif
wdenke6466f62003-06-05 19:27:42 +00001300 /* save address for later use */
1301 memcpy(NetArpWaitPacketMAC, &arp->ar_data[0], 6);
1302
wdenkb8fb6192004-08-02 21:11:11 +00001303#ifdef CONFIG_NETCONSOLE
1304 (*packetHandler)(0,0,0,0);
1305#endif
wdenke6466f62003-06-05 19:27:42 +00001306 /* modify header, and transmit it */
1307 memcpy(((Ethernet_t *)NetArpWaitTxPacket)->et_dest, NetArpWaitPacketMAC, 6);
1308 (void) eth_send(NetArpWaitTxPacket, NetArpWaitTxPacketSize);
1309
1310 /* no arp request pending now */
1311 NetArpWaitPacketIP = 0;
1312 NetArpWaitTxPacketSize = 0;
1313 NetArpWaitPacketMAC = NULL;
1314
1315 }
wdenk2d966952002-10-31 22:12:35 +00001316 return;
1317 default:
1318#ifdef ET_DEBUG
1319 printf("Unexpected ARP opcode 0x%x\n", ntohs(arp->ar_op));
1320#endif
1321 return;
1322 }
wdenkcb99da52005-01-12 00:15:14 +00001323 break;
wdenk2d966952002-10-31 22:12:35 +00001324
1325 case PROT_RARP:
1326#ifdef ET_DEBUG
wdenk42c05472004-03-23 22:14:11 +00001327 puts ("Got RARP\n");
wdenk2d966952002-10-31 22:12:35 +00001328#endif
1329 arp = (ARP_t *)ip;
1330 if (len < ARP_HDR_SIZE) {
1331 printf("bad length %d < %d\n", len, ARP_HDR_SIZE);
1332 return;
1333 }
1334
1335 if ((ntohs(arp->ar_op) != RARPOP_REPLY) ||
1336 (ntohs(arp->ar_hrd) != ARP_ETHER) ||
1337 (ntohs(arp->ar_pro) != PROT_IP) ||
1338 (arp->ar_hln != 6) || (arp->ar_pln != 4)) {
1339
wdenk42c05472004-03-23 22:14:11 +00001340 puts ("invalid RARP header\n");
wdenk2d966952002-10-31 22:12:35 +00001341 } else {
1342 NetCopyIP(&NetOurIP, &arp->ar_data[16]);
wdenk4989f872004-03-14 15:06:13 +00001343 if (NetServerIP == 0)
1344 NetCopyIP(&NetServerIP, &arp->ar_data[ 6]);
wdenk2d966952002-10-31 22:12:35 +00001345 memcpy (NetServerEther, &arp->ar_data[ 0], 6);
1346
1347 (*packetHandler)(0,0,0,0);
1348 }
1349 break;
1350
1351 case PROT_IP:
1352#ifdef ET_DEBUG
wdenk42c05472004-03-23 22:14:11 +00001353 puts ("Got IP\n");
wdenk2d966952002-10-31 22:12:35 +00001354#endif
1355 if (len < IP_HDR_SIZE) {
1356 debug ("len bad %d < %d\n", len, IP_HDR_SIZE);
1357 return;
1358 }
1359 if (len < ntohs(ip->ip_len)) {
1360 printf("len bad %d < %d\n", len, ntohs(ip->ip_len));
1361 return;
1362 }
1363 len = ntohs(ip->ip_len);
1364#ifdef ET_DEBUG
1365 printf("len=%d, v=%02x\n", len, ip->ip_hl_v & 0xff);
1366#endif
1367 if ((ip->ip_hl_v & 0xf0) != 0x40) {
1368 return;
1369 }
1370 if (ip->ip_off & htons(0x1fff)) { /* Can't deal w/ fragments */
1371 return;
1372 }
1373 if (!NetCksumOk((uchar *)ip, IP_HDR_SIZE_NO_UDP / 2)) {
wdenk42c05472004-03-23 22:14:11 +00001374 puts ("checksum bad\n");
wdenk2d966952002-10-31 22:12:35 +00001375 return;
1376 }
1377 tmp = NetReadIP(&ip->ip_dst);
1378 if (NetOurIP && tmp != NetOurIP && tmp != 0xFFFFFFFF) {
1379 return;
1380 }
1381 /*
1382 * watch for ICMP host redirects
1383 *
wdenk57b2d802003-06-27 21:31:46 +00001384 * There is no real handler code (yet). We just watch
1385 * for ICMP host redirect messages. In case anybody
1386 * sees these messages: please contact me
1387 * (wd@denx.de), or - even better - send me the
1388 * necessary fixes :-)
wdenk2d966952002-10-31 22:12:35 +00001389 *
wdenk57b2d802003-06-27 21:31:46 +00001390 * Note: in all cases where I have seen this so far
1391 * it was a problem with the router configuration,
1392 * for instance when a router was configured in the
1393 * BOOTP reply, but the TFTP server was on the same
1394 * subnet. So this is probably a warning that your
1395 * configuration might be wrong. But I'm not really
1396 * sure if there aren't any other situations.
wdenk2d966952002-10-31 22:12:35 +00001397 */
1398 if (ip->ip_p == IPPROTO_ICMP) {
1399 ICMP_t *icmph = (ICMP_t *)&(ip->udp_src);
1400
wdenke6466f62003-06-05 19:27:42 +00001401 switch (icmph->type) {
1402 case ICMP_REDIRECT:
wdenk2d966952002-10-31 22:12:35 +00001403 if (icmph->code != ICMP_REDIR_HOST)
1404 return;
1405 puts (" ICMP Host Redirect to ");
1406 print_IPaddr(icmph->un.gateway);
1407 putc(' ');
wdenke6466f62003-06-05 19:27:42 +00001408 break;
1409#if (CONFIG_COMMANDS & CFG_CMD_PING)
1410 case ICMP_ECHO_REPLY:
1411 /*
1412 * IP header OK. Pass the packet to the current handler.
1413 */
1414 /* XXX point to ip packet */
1415 (*packetHandler)((uchar *)ip, 0, 0, 0);
1416 break;
1417#endif
1418 default:
1419 return;
1420 }
wdenk2d966952002-10-31 22:12:35 +00001421 } else if (ip->ip_p != IPPROTO_UDP) { /* Only UDP packets */
1422 return;
1423 }
1424
wdenkb8fb6192004-08-02 21:11:11 +00001425#ifdef CONFIG_NETCONSOLE
1426 nc_input_packet((uchar *)ip +IP_HDR_SIZE,
1427 ntohs(ip->udp_dst),
1428 ntohs(ip->udp_src),
1429 ntohs(ip->udp_len) - 8);
1430#endif
wdenk2d966952002-10-31 22:12:35 +00001431 /*
1432 * IP header OK. Pass the packet to the current handler.
1433 */
1434 (*packetHandler)((uchar *)ip +IP_HDR_SIZE,
1435 ntohs(ip->udp_dst),
1436 ntohs(ip->udp_src),
1437 ntohs(ip->udp_len) - 8);
wdenk2d966952002-10-31 22:12:35 +00001438 break;
1439 }
1440}
1441
1442
1443/**********************************************************************/
1444
1445static int net_check_prereq (proto_t protocol)
1446{
1447 switch (protocol) {
wdenk05939202004-04-18 17:39:38 +00001448 /* Fall through */
wdenke6466f62003-06-05 19:27:42 +00001449#if (CONFIG_COMMANDS & CFG_CMD_PING)
1450 case PING:
wdenk05939202004-04-18 17:39:38 +00001451 if (NetPingIP == 0) {
1452 puts ("*** ERROR: ping address not given\n");
1453 return (1);
1454 }
1455 goto common;
wdenke6466f62003-06-05 19:27:42 +00001456#endif
wdenkb4ad9622005-04-01 00:25:43 +00001457#if (CONFIG_COMMANDS & CFG_CMD_SNTP)
1458 case SNTP:
1459 if (NetNtpServerIP == 0) {
1460 puts ("*** ERROR: NTP server address not given\n");
1461 return (1);
1462 }
1463 goto common;
1464#endif
wdenkbe9c1cb2004-02-24 02:00:03 +00001465#if (CONFIG_COMMANDS & CFG_CMD_NFS)
1466 case NFS:
1467#endif
wdenkb8fb6192004-08-02 21:11:11 +00001468 case NETCONS:
wdenk2d966952002-10-31 22:12:35 +00001469 case TFTP:
wdenk05939202004-04-18 17:39:38 +00001470 if (NetServerIP == 0) {
1471 puts ("*** ERROR: `serverip' not set\n");
1472 return (1);
1473 }
wdenkb4ad9622005-04-01 00:25:43 +00001474#if (CONFIG_COMMANDS & (CFG_CMD_PING | CFG_CMD_SNTP))
wdenk05939202004-04-18 17:39:38 +00001475 common:
wdenke6466f62003-06-05 19:27:42 +00001476#endif
1477
wdenk05939202004-04-18 17:39:38 +00001478 if (NetOurIP == 0) {
1479 puts ("*** ERROR: `ipaddr' not set\n");
1480 return (1);
1481 }
1482 /* Fall through */
wdenk2d966952002-10-31 22:12:35 +00001483
1484 case DHCP:
1485 case RARP:
1486 case BOOTP:
wdenk145d2c12004-04-15 21:48:45 +00001487 case CDP:
wdenk05939202004-04-18 17:39:38 +00001488 if (memcmp (NetOurEther, "\0\0\0\0\0\0", 6) == 0) {
wdenk2d966952002-10-31 22:12:35 +00001489#ifdef CONFIG_NET_MULTI
wdenk05939202004-04-18 17:39:38 +00001490 extern int eth_get_dev_index (void);
1491 int num = eth_get_dev_index ();
wdenk2d966952002-10-31 22:12:35 +00001492
wdenk05939202004-04-18 17:39:38 +00001493 switch (num) {
1494 case -1:
wdenk2d966952002-10-31 22:12:35 +00001495 puts ("*** ERROR: No ethernet found.\n");
1496 return (1);
wdenk05939202004-04-18 17:39:38 +00001497 case 0:
wdenk2d966952002-10-31 22:12:35 +00001498 puts ("*** ERROR: `ethaddr' not set\n");
1499 break;
wdenk05939202004-04-18 17:39:38 +00001500 default:
wdenk57b2d802003-06-27 21:31:46 +00001501 printf ("*** ERROR: `eth%daddr' not set\n",
wdenk2d966952002-10-31 22:12:35 +00001502 num);
1503 break;
wdenk05939202004-04-18 17:39:38 +00001504 }
wdenk2d966952002-10-31 22:12:35 +00001505
wdenk05939202004-04-18 17:39:38 +00001506 NetStartAgain ();
1507 return (2);
wdenk2d966952002-10-31 22:12:35 +00001508#else
wdenk05939202004-04-18 17:39:38 +00001509 puts ("*** ERROR: `ethaddr' not set\n");
1510 return (1);
wdenk2d966952002-10-31 22:12:35 +00001511#endif
wdenk05939202004-04-18 17:39:38 +00001512 }
1513 /* Fall through */
1514 default:
1515 return (0);
wdenk2d966952002-10-31 22:12:35 +00001516 }
wdenk05939202004-04-18 17:39:38 +00001517 return (0); /* OK */
wdenk2d966952002-10-31 22:12:35 +00001518}
1519/**********************************************************************/
1520
1521int
1522NetCksumOk(uchar * ptr, int len)
1523{
1524 return !((NetCksum(ptr, len) + 1) & 0xfffe);
1525}
1526
1527
1528unsigned
1529NetCksum(uchar * ptr, int len)
1530{
1531 ulong xsum;
1532
1533 xsum = 0;
1534 while (len-- > 0)
1535 xsum += *((ushort *)ptr)++;
1536 xsum = (xsum & 0xffff) + (xsum >> 16);
1537 xsum = (xsum & 0xffff) + (xsum >> 16);
1538 return (xsum & 0xffff);
1539}
1540
wdenk145d2c12004-04-15 21:48:45 +00001541int
1542NetEthHdrSize(void)
1543{
1544 ushort myvlanid;
1545
1546 myvlanid = ntohs(NetOurVLAN);
1547 if (myvlanid == (ushort)-1)
1548 myvlanid = VLAN_NONE;
wdenk2d966952002-10-31 22:12:35 +00001549
wdenk145d2c12004-04-15 21:48:45 +00001550 return ((myvlanid & VLAN_IDMASK) == VLAN_NONE) ? ETHER_HDR_SIZE : VLAN_ETHER_HDR_SIZE;
1551}
1552
1553int
wdenk2d966952002-10-31 22:12:35 +00001554NetSetEther(volatile uchar * xet, uchar * addr, uint prot)
1555{
1556 Ethernet_t *et = (Ethernet_t *)xet;
wdenk145d2c12004-04-15 21:48:45 +00001557 ushort myvlanid;
1558
1559 myvlanid = ntohs(NetOurVLAN);
1560 if (myvlanid == (ushort)-1)
1561 myvlanid = VLAN_NONE;
wdenk2d966952002-10-31 22:12:35 +00001562
1563 memcpy (et->et_dest, addr, 6);
1564 memcpy (et->et_src, NetOurEther, 6);
wdenk145d2c12004-04-15 21:48:45 +00001565 if ((myvlanid & VLAN_IDMASK) == VLAN_NONE) {
wdenk2d966952002-10-31 22:12:35 +00001566 et->et_protlen = htons(prot);
wdenk145d2c12004-04-15 21:48:45 +00001567 return ETHER_HDR_SIZE;
1568 } else {
1569 VLAN_Ethernet_t *vet = (VLAN_Ethernet_t *)xet;
wdenk2d966952002-10-31 22:12:35 +00001570
wdenk145d2c12004-04-15 21:48:45 +00001571 vet->vet_vlan_type = htons(PROT_VLAN);
1572 vet->vet_tag = htons((0 << 5) | (myvlanid & VLAN_IDMASK));
1573 vet->vet_type = htons(prot);
1574 return VLAN_ETHER_HDR_SIZE;
1575 }
1576}
wdenk2d966952002-10-31 22:12:35 +00001577
1578void
1579NetSetIP(volatile uchar * xip, IPaddr_t dest, int dport, int sport, int len)
1580{
1581 volatile IP_t *ip = (IP_t *)xip;
1582
1583 /*
1584 * If the data is an odd number of bytes, zero the
1585 * byte after the last byte so that the checksum
1586 * will work.
1587 */
1588 if (len & 1)
1589 xip[IP_HDR_SIZE + len] = 0;
1590
1591 /*
1592 * Construct an IP and UDP header.
wdenk05939202004-04-18 17:39:38 +00001593 * (need to set no fragment bit - XXX)
wdenk2d966952002-10-31 22:12:35 +00001594 */
1595 ip->ip_hl_v = 0x45; /* IP_HDR_SIZE / 4 (not including UDP) */
1596 ip->ip_tos = 0;
1597 ip->ip_len = htons(IP_HDR_SIZE + len);
1598 ip->ip_id = htons(NetIPID++);
1599 ip->ip_off = htons(0x4000); /* No fragmentation */
1600 ip->ip_ttl = 255;
1601 ip->ip_p = 17; /* UDP */
1602 ip->ip_sum = 0;
1603 NetCopyIP((void*)&ip->ip_src, &NetOurIP); /* already in network byte order */
1604 NetCopyIP((void*)&ip->ip_dst, &dest); /* - "" - */
1605 ip->udp_src = htons(sport);
1606 ip->udp_dst = htons(dport);
1607 ip->udp_len = htons(8 + len);
1608 ip->udp_xsum = 0;
1609 ip->ip_sum = ~NetCksum((uchar *)ip, IP_HDR_SIZE_NO_UDP / 2);
1610}
1611
1612void copy_filename (uchar *dst, uchar *src, int size)
1613{
1614 if (*src && (*src == '"')) {
1615 ++src;
1616 --size;
1617 }
1618
1619 while ((--size > 0) && *src && (*src != '"')) {
1620 *dst++ = *src++;
1621 }
1622 *dst = '\0';
1623}
1624
1625#endif /* CFG_CMD_NET */
1626
1627void ip_to_string (IPaddr_t x, char *s)
1628{
wdenk145d2c12004-04-15 21:48:45 +00001629 x = ntohl (x);
1630 sprintf (s, "%d.%d.%d.%d",
1631 (int) ((x >> 24) & 0xff),
1632 (int) ((x >> 16) & 0xff),
1633 (int) ((x >> 8) & 0xff), (int) ((x >> 0) & 0xff)
wdenk05939202004-04-18 17:39:38 +00001634 );
wdenk2d966952002-10-31 22:12:35 +00001635}
1636
wdenke6466f62003-06-05 19:27:42 +00001637IPaddr_t string_to_ip(char *s)
wdenk2d966952002-10-31 22:12:35 +00001638{
1639 IPaddr_t addr;
wdenke6466f62003-06-05 19:27:42 +00001640 char *e;
wdenk2d966952002-10-31 22:12:35 +00001641 int i;
1642
wdenke6466f62003-06-05 19:27:42 +00001643 if (s == NULL)
1644 return(0);
wdenk2d966952002-10-31 22:12:35 +00001645
1646 for (addr=0, i=0; i<4; ++i) {
1647 ulong val = s ? simple_strtoul(s, &e, 10) : 0;
1648 addr <<= 8;
1649 addr |= (val & 0xFF);
1650 if (s) {
1651 s = (*e) ? e+1 : e;
1652 }
1653 }
1654
1655 return (htonl(addr));
1656}
wdenke6466f62003-06-05 19:27:42 +00001657
wdenk145d2c12004-04-15 21:48:45 +00001658void VLAN_to_string(ushort x, char *s)
1659{
1660 x = ntohs(x);
1661
1662 if (x == (ushort)-1)
1663 x = VLAN_NONE;
1664
1665 if (x == VLAN_NONE)
1666 strcpy(s, "none");
1667 else
1668 sprintf(s, "%d", x & VLAN_IDMASK);
1669}
1670
1671ushort string_to_VLAN(char *s)
1672{
1673 ushort id;
1674
1675 if (s == NULL)
wdenk656140b2004-04-25 13:18:40 +00001676 return htons(VLAN_NONE);
wdenk145d2c12004-04-15 21:48:45 +00001677
1678 if (*s < '0' || *s > '9')
1679 id = VLAN_NONE;
1680 else
1681 id = (ushort)simple_strtoul(s, NULL, 10);
1682
wdenk656140b2004-04-25 13:18:40 +00001683 return htons(id);
wdenk145d2c12004-04-15 21:48:45 +00001684}
1685
wdenke6466f62003-06-05 19:27:42 +00001686void print_IPaddr (IPaddr_t x)
1687{
wdenk145d2c12004-04-15 21:48:45 +00001688 char tmp[16];
wdenke6466f62003-06-05 19:27:42 +00001689
wdenk145d2c12004-04-15 21:48:45 +00001690 ip_to_string (x, tmp);
wdenke6466f62003-06-05 19:27:42 +00001691
wdenk145d2c12004-04-15 21:48:45 +00001692 puts (tmp);
wdenke6466f62003-06-05 19:27:42 +00001693}
1694
1695IPaddr_t getenv_IPaddr (char *var)
1696{
1697 return (string_to_ip(getenv(var)));
1698}
wdenk145d2c12004-04-15 21:48:45 +00001699
1700ushort getenv_VLAN(char *var)
1701{
1702 return (string_to_VLAN(getenv(var)));
1703}