xref: /rk3399_rockchip-uboot/net/net.c (revision e94070c443bdc9c0231abeca920d9f9362701aec)
1 /*
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
58  *
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
67  *
68  * SNTP:
69  *
70  *	Prerequisites:	- own ethernet address
71  *			- own IP address
72  *	We want:	- network time
73  *	Next step:	none
74  */
75 
76 
77 #include <common.h>
78 #include <command.h>
79 #include <net.h>
80 #if defined(CONFIG_STATUS_LED)
81 #include <miiphy.h>
82 #include <status_led.h>
83 #endif
84 #include <watchdog.h>
85 #include <linux/compiler.h>
86 #include "arp.h"
87 #include "bootp.h"
88 #include "cdp.h"
89 #if defined(CONFIG_CMD_DNS)
90 #include "dns.h"
91 #endif
92 #include "nfs.h"
93 #include "ping.h"
94 #include "rarp.h"
95 #if defined(CONFIG_CMD_SNTP)
96 #include "sntp.h"
97 #endif
98 #include "tftp.h"
99 
100 DECLARE_GLOBAL_DATA_PTR;
101 
102 /** BOOTP EXTENTIONS **/
103 
104 /* Our subnet mask (0=unknown) */
105 IPaddr_t	NetOurSubnetMask;
106 /* Our gateways IP address */
107 IPaddr_t	NetOurGatewayIP;
108 /* Our DNS IP address */
109 IPaddr_t	NetOurDNSIP;
110 #if defined(CONFIG_BOOTP_DNS2)
111 /* Our 2nd DNS IP address */
112 IPaddr_t	NetOurDNS2IP;
113 #endif
114 /* Our NIS domain */
115 char		NetOurNISDomain[32] = {0,};
116 /* Our hostname */
117 char		NetOurHostName[32] = {0,};
118 /* Our bootpath */
119 char		NetOurRootPath[64] = {0,};
120 /* Our bootfile size in blocks */
121 ushort		NetBootFileSize;
122 
123 #ifdef CONFIG_MCAST_TFTP	/* Multicast TFTP */
124 IPaddr_t Mcast_addr;
125 #endif
126 
127 /** END OF BOOTP EXTENTIONS **/
128 
129 /* The actual transferred size of the bootfile (in bytes) */
130 ulong		NetBootFileXferSize;
131 /* Our ethernet address */
132 uchar		NetOurEther[6];
133 /* Boot server enet address */
134 uchar		NetServerEther[6];
135 /* Our IP addr (0 = unknown) */
136 IPaddr_t	NetOurIP;
137 /* Server IP addr (0 = unknown) */
138 IPaddr_t	NetServerIP;
139 /* Current receive packet */
140 uchar *NetRxPacket;
141 /* Current rx packet length */
142 int		NetRxPacketLen;
143 /* IP packet ID */
144 unsigned	NetIPID;
145 /* Ethernet bcast address */
146 uchar		NetBcastAddr[6] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
147 uchar		NetEtherNullAddr[6];
148 #ifdef CONFIG_API
149 void		(*push_packet)(void *, int len) = 0;
150 #endif
151 /* Network loop state */
152 enum net_loop_state net_state;
153 /* Tried all network devices */
154 int		NetRestartWrap;
155 /* Network loop restarted */
156 static int	NetRestarted;
157 /* At least one device configured */
158 static int	NetDevExists;
159 
160 /* XXX in both little & big endian machines 0xFFFF == ntohs(-1) */
161 /* default is without VLAN */
162 ushort		NetOurVLAN = 0xFFFF;
163 /* ditto */
164 ushort		NetOurNativeVLAN = 0xFFFF;
165 
166 /* Boot File name */
167 char		BootFile[128];
168 
169 #if defined(CONFIG_CMD_SNTP)
170 /* NTP server IP address */
171 IPaddr_t	NetNtpServerIP;
172 /* offset time from UTC */
173 int		NetTimeOffset;
174 #endif
175 
176 uchar PktBuf[(PKTBUFSRX+1) * PKTSIZE_ALIGN + PKTALIGN];
177 
178 /* Receive packet */
179 uchar *NetRxPackets[PKTBUFSRX];
180 
181 /* Current UDP RX packet handler */
182 static rxhand_f *udp_packet_handler;
183 /* Current ARP RX packet handler */
184 static rxhand_f *arp_packet_handler;
185 #ifdef CONFIG_CMD_TFTPPUT
186 /* Current ICMP rx handler */
187 static rxhand_icmp_f *packet_icmp_handler;
188 #endif
189 /* Current timeout handler */
190 static thand_f *timeHandler;
191 /* Time base value */
192 static ulong	timeStart;
193 /* Current timeout value */
194 static ulong	timeDelta;
195 /* THE transmit packet */
196 uchar *NetTxPacket;
197 
198 static int net_check_prereq(enum proto_t protocol);
199 
200 static int NetTryCount;
201 
202 /**********************************************************************/
203 
204 /*
205  * Check if autoload is enabled. If so, use either NFS or TFTP to download
206  * the boot file.
207  */
208 void net_auto_load(void)
209 {
210 	const char *s = getenv("autoload");
211 
212 	if (s != NULL) {
213 		if (*s == 'n') {
214 			/*
215 			 * Just use BOOTP/RARP to configure system;
216 			 * Do not use TFTP to load the bootfile.
217 			 */
218 			net_set_state(NETLOOP_SUCCESS);
219 			return;
220 		}
221 #if defined(CONFIG_CMD_NFS)
222 		if (strcmp(s, "NFS") == 0) {
223 			/*
224 			 * Use NFS to load the bootfile.
225 			 */
226 			NfsStart();
227 			return;
228 		}
229 #endif
230 	}
231 	TftpStart(TFTPGET);
232 }
233 
234 static void NetInitLoop(void)
235 {
236 	static int env_changed_id;
237 	int env_id = get_env_id();
238 
239 	/* update only when the environment has changed */
240 	if (env_changed_id != env_id) {
241 		NetOurIP = getenv_IPaddr("ipaddr");
242 		NetOurGatewayIP = getenv_IPaddr("gatewayip");
243 		NetOurSubnetMask = getenv_IPaddr("netmask");
244 		NetServerIP = getenv_IPaddr("serverip");
245 		NetOurNativeVLAN = getenv_VLAN("nvlan");
246 		NetOurVLAN = getenv_VLAN("vlan");
247 #if defined(CONFIG_CMD_DNS)
248 		NetOurDNSIP = getenv_IPaddr("dnsip");
249 #endif
250 		env_changed_id = env_id;
251 	}
252 
253 	return;
254 }
255 
256 static void net_clear_handlers(void)
257 {
258 	net_set_udp_handler(NULL);
259 	net_set_arp_handler(NULL);
260 	NetSetTimeout(0, NULL);
261 }
262 
263 static void net_cleanup_loop(void)
264 {
265 	net_clear_handlers();
266 }
267 
268 void net_init(void)
269 {
270 	static int first_call = 1;
271 
272 	if (first_call) {
273 		/*
274 		 *	Setup packet buffers, aligned correctly.
275 		 */
276 		int i;
277 
278 		NetTxPacket = &PktBuf[0] + (PKTALIGN - 1);
279 		NetTxPacket -= (ulong)NetTxPacket % PKTALIGN;
280 		for (i = 0; i < PKTBUFSRX; i++)
281 			NetRxPackets[i] = NetTxPacket + (i + 1) * PKTSIZE_ALIGN;
282 
283 		ArpInit();
284 		net_clear_handlers();
285 
286 		/* Only need to setup buffer pointers once. */
287 		first_call = 0;
288 	}
289 
290 	NetInitLoop();
291 }
292 
293 /**********************************************************************/
294 /*
295  *	Main network processing loop.
296  */
297 
298 int NetLoop(enum proto_t protocol)
299 {
300 	bd_t *bd = gd->bd;
301 	int ret = -1;
302 
303 	NetRestarted = 0;
304 	NetDevExists = 0;
305 	NetTryCount = 1;
306 
307 	bootstage_mark_name(BOOTSTAGE_ID_ETH_START, "eth_start");
308 	net_init();
309 	eth_halt();
310 	eth_set_current();
311 	if (eth_init(bd) < 0) {
312 		eth_halt();
313 		return -1;
314 	}
315 
316 restart:
317 	memcpy(NetOurEther, eth_get_dev()->enetaddr, 6);
318 
319 	net_set_state(NETLOOP_CONTINUE);
320 
321 	/*
322 	 *	Start the ball rolling with the given start function.  From
323 	 *	here on, this code is a state machine driven by received
324 	 *	packets and timer events.
325 	 */
326 	NetInitLoop();
327 
328 	switch (net_check_prereq(protocol)) {
329 	case 1:
330 		/* network not configured */
331 		eth_halt();
332 		return -1;
333 
334 	case 2:
335 		/* network device not configured */
336 		break;
337 
338 	case 0:
339 		NetDevExists = 1;
340 		NetBootFileXferSize = 0;
341 		switch (protocol) {
342 		case TFTPGET:
343 #ifdef CONFIG_CMD_TFTPPUT
344 		case TFTPPUT:
345 #endif
346 			/* always use ARP to get server ethernet address */
347 			TftpStart(protocol);
348 			break;
349 #ifdef CONFIG_CMD_TFTPSRV
350 		case TFTPSRV:
351 			TftpStartServer();
352 			break;
353 #endif
354 #if defined(CONFIG_CMD_DHCP)
355 		case DHCP:
356 			BootpTry = 0;
357 			NetOurIP = 0;
358 			DhcpRequest();		/* Basically same as BOOTP */
359 			break;
360 #endif
361 
362 		case BOOTP:
363 			BootpTry = 0;
364 			NetOurIP = 0;
365 			BootpRequest();
366 			break;
367 
368 #if defined(CONFIG_CMD_RARP)
369 		case RARP:
370 			RarpTry = 0;
371 			NetOurIP = 0;
372 			RarpRequest();
373 			break;
374 #endif
375 #if defined(CONFIG_CMD_PING)
376 		case PING:
377 			ping_start();
378 			break;
379 #endif
380 #if defined(CONFIG_CMD_NFS)
381 		case NFS:
382 			NfsStart();
383 			break;
384 #endif
385 #if defined(CONFIG_CMD_CDP)
386 		case CDP:
387 			CDPStart();
388 			break;
389 #endif
390 #ifdef CONFIG_NETCONSOLE
391 		case NETCONS:
392 			NcStart();
393 			break;
394 #endif
395 #if defined(CONFIG_CMD_SNTP)
396 		case SNTP:
397 			SntpStart();
398 			break;
399 #endif
400 #if defined(CONFIG_CMD_DNS)
401 		case DNS:
402 			DnsStart();
403 			break;
404 #endif
405 		default:
406 			break;
407 		}
408 
409 		break;
410 	}
411 
412 #if defined(CONFIG_MII) || defined(CONFIG_CMD_MII)
413 #if	defined(CONFIG_SYS_FAULT_ECHO_LINK_DOWN)	&& \
414 	defined(CONFIG_STATUS_LED)			&& \
415 	defined(STATUS_LED_RED)
416 	/*
417 	 * Echo the inverted link state to the fault LED.
418 	 */
419 	if (miiphy_link(eth_get_dev()->name, CONFIG_SYS_FAULT_MII_ADDR))
420 		status_led_set(STATUS_LED_RED, STATUS_LED_OFF);
421 	else
422 		status_led_set(STATUS_LED_RED, STATUS_LED_ON);
423 #endif /* CONFIG_SYS_FAULT_ECHO_LINK_DOWN, ... */
424 #endif /* CONFIG_MII, ... */
425 
426 	/*
427 	 *	Main packet reception loop.  Loop receiving packets until
428 	 *	someone sets `net_state' to a state that terminates.
429 	 */
430 	for (;;) {
431 		WATCHDOG_RESET();
432 #ifdef CONFIG_SHOW_ACTIVITY
433 		show_activity(1);
434 #endif
435 		/*
436 		 *	Check the ethernet for a new packet.  The ethernet
437 		 *	receive routine will process it.
438 		 */
439 		eth_rx();
440 
441 		/*
442 		 *	Abort if ctrl-c was pressed.
443 		 */
444 		if (ctrlc()) {
445 			/* cancel any ARP that may not have completed */
446 			NetArpWaitPacketIP = 0;
447 
448 			net_cleanup_loop();
449 			eth_halt();
450 			puts("\nAbort\n");
451 			goto done;
452 		}
453 
454 		ArpTimeoutCheck();
455 
456 		/*
457 		 *	Check for a timeout, and run the timeout handler
458 		 *	if we have one.
459 		 */
460 		if (timeHandler && ((get_timer(0) - timeStart) > timeDelta)) {
461 			thand_f *x;
462 
463 #if defined(CONFIG_MII) || defined(CONFIG_CMD_MII)
464 #if	defined(CONFIG_SYS_FAULT_ECHO_LINK_DOWN)	&& \
465 	defined(CONFIG_STATUS_LED)			&& \
466 	defined(STATUS_LED_RED)
467 			/*
468 			 * Echo the inverted link state to the fault LED.
469 			 */
470 			if (miiphy_link(eth_get_dev()->name,
471 				       CONFIG_SYS_FAULT_MII_ADDR)) {
472 				status_led_set(STATUS_LED_RED, STATUS_LED_OFF);
473 			} else {
474 				status_led_set(STATUS_LED_RED, STATUS_LED_ON);
475 			}
476 #endif /* CONFIG_SYS_FAULT_ECHO_LINK_DOWN, ... */
477 #endif /* CONFIG_MII, ... */
478 			x = timeHandler;
479 			timeHandler = (thand_f *)0;
480 			(*x)();
481 		}
482 
483 
484 		switch (net_state) {
485 
486 		case NETLOOP_RESTART:
487 			NetRestarted = 1;
488 			goto restart;
489 
490 		case NETLOOP_SUCCESS:
491 			net_cleanup_loop();
492 			if (NetBootFileXferSize > 0) {
493 				char buf[20];
494 				printf("Bytes transferred = %ld (%lx hex)\n",
495 					NetBootFileXferSize,
496 					NetBootFileXferSize);
497 				sprintf(buf, "%lX", NetBootFileXferSize);
498 				setenv("filesize", buf);
499 
500 				sprintf(buf, "%lX", (unsigned long)load_addr);
501 				setenv("fileaddr", buf);
502 			}
503 			eth_halt();
504 			ret = NetBootFileXferSize;
505 			goto done;
506 
507 		case NETLOOP_FAIL:
508 			net_cleanup_loop();
509 			goto done;
510 
511 		case NETLOOP_CONTINUE:
512 			continue;
513 		}
514 	}
515 
516 done:
517 #ifdef CONFIG_CMD_TFTPPUT
518 	/* Clear out the handlers */
519 	net_set_udp_handler(NULL);
520 	net_set_icmp_handler(NULL);
521 #endif
522 	return ret;
523 }
524 
525 /**********************************************************************/
526 
527 static void
528 startAgainTimeout(void)
529 {
530 	net_set_state(NETLOOP_RESTART);
531 }
532 
533 void NetStartAgain(void)
534 {
535 	char *nretry;
536 	int retry_forever = 0;
537 	unsigned long retrycnt = 0;
538 
539 	nretry = getenv("netretry");
540 	if (nretry) {
541 		if (!strcmp(nretry, "yes"))
542 			retry_forever = 1;
543 		else if (!strcmp(nretry, "no"))
544 			retrycnt = 0;
545 		else if (!strcmp(nretry, "once"))
546 			retrycnt = 1;
547 		else
548 			retrycnt = simple_strtoul(nretry, NULL, 0);
549 	} else
550 		retry_forever = 1;
551 
552 	if ((!retry_forever) && (NetTryCount >= retrycnt)) {
553 		eth_halt();
554 		net_set_state(NETLOOP_FAIL);
555 		return;
556 	}
557 
558 	NetTryCount++;
559 
560 	eth_halt();
561 #if !defined(CONFIG_NET_DO_NOT_TRY_ANOTHER)
562 	eth_try_another(!NetRestarted);
563 #endif
564 	eth_init(gd->bd);
565 	if (NetRestartWrap) {
566 		NetRestartWrap = 0;
567 		if (NetDevExists) {
568 			NetSetTimeout(10000UL, startAgainTimeout);
569 			net_set_udp_handler(NULL);
570 		} else {
571 			net_set_state(NETLOOP_FAIL);
572 		}
573 	} else {
574 		net_set_state(NETLOOP_RESTART);
575 	}
576 }
577 
578 /**********************************************************************/
579 /*
580  *	Miscelaneous bits.
581  */
582 
583 static void dummy_handler(uchar *pkt, unsigned dport,
584 			IPaddr_t sip, unsigned sport,
585 			unsigned len)
586 {
587 }
588 
589 rxhand_f *net_get_udp_handler(void)
590 {
591 	return udp_packet_handler;
592 }
593 
594 void net_set_udp_handler(rxhand_f *f)
595 {
596 	if (f == NULL)
597 		udp_packet_handler = dummy_handler;
598 	else
599 		udp_packet_handler = f;
600 }
601 
602 rxhand_f *net_get_arp_handler(void)
603 {
604 	return arp_packet_handler;
605 }
606 
607 void net_set_arp_handler(rxhand_f *f)
608 {
609 	if (f == NULL)
610 		arp_packet_handler = dummy_handler;
611 	else
612 		arp_packet_handler = f;
613 }
614 
615 #ifdef CONFIG_CMD_TFTPPUT
616 void net_set_icmp_handler(rxhand_icmp_f *f)
617 {
618 	packet_icmp_handler = f;
619 }
620 #endif
621 
622 void
623 NetSetTimeout(ulong iv, thand_f *f)
624 {
625 	if (iv == 0) {
626 		timeHandler = (thand_f *)0;
627 	} else {
628 		timeHandler = f;
629 		timeStart = get_timer(0);
630 		timeDelta = iv;
631 	}
632 }
633 
634 int NetSendUDPPacket(uchar *ether, IPaddr_t dest, int dport, int sport,
635 		int payload_len)
636 {
637 	uchar *pkt;
638 	int eth_hdr_size;
639 	int pkt_hdr_size;
640 
641 	/* make sure the NetTxPacket is initialized (NetInit() was called) */
642 	assert(NetTxPacket != NULL);
643 	if (NetTxPacket == NULL)
644 		return -1;
645 
646 	/* convert to new style broadcast */
647 	if (dest == 0)
648 		dest = 0xFFFFFFFF;
649 
650 	/* if broadcast, make the ether address a broadcast and don't do ARP */
651 	if (dest == 0xFFFFFFFF)
652 		ether = NetBcastAddr;
653 
654 	pkt = (uchar *)NetTxPacket;
655 
656 	eth_hdr_size = NetSetEther(pkt, ether, PROT_IP);
657 	pkt += eth_hdr_size;
658 	net_set_udp_header(pkt, dest, dport, sport, payload_len);
659 	pkt_hdr_size = eth_hdr_size + IP_UDP_HDR_SIZE;
660 
661 	/* if MAC address was not discovered yet, do an ARP request */
662 	if (memcmp(ether, NetEtherNullAddr, 6) == 0) {
663 		debug("sending ARP for %pI4\n", &dest);
664 
665 		/* save the ip and eth addr for the packet to send after arp */
666 		NetArpWaitPacketIP = dest;
667 		NetArpWaitPacketMAC = ether;
668 
669 		/* size of the waiting packet */
670 		NetArpWaitTxPacketSize = pkt_hdr_size + payload_len;
671 
672 		/* and do the ARP request */
673 		NetArpWaitTry = 1;
674 		NetArpWaitTimerStart = get_timer(0);
675 		ArpRequest();
676 		return 1;	/* waiting */
677 	} else {
678 		debug("sending UDP to %pI4/%pM\n", &dest, ether);
679 		NetSendPacket(NetTxPacket, pkt_hdr_size + payload_len);
680 		return 0;	/* transmitted */
681 	}
682 }
683 
684 #ifdef CONFIG_IP_DEFRAG
685 /*
686  * This function collects fragments in a single packet, according
687  * to the algorithm in RFC815. It returns NULL or the pointer to
688  * a complete packet, in static storage
689  */
690 #ifndef CONFIG_NET_MAXDEFRAG
691 #define CONFIG_NET_MAXDEFRAG 16384
692 #endif
693 /*
694  * MAXDEFRAG, above, is chosen in the config file and  is real data
695  * so we need to add the NFS overhead, which is more than TFTP.
696  * To use sizeof in the internal unnamed structures, we need a real
697  * instance (can't do "sizeof(struct rpc_t.u.reply))", unfortunately).
698  * The compiler doesn't complain nor allocates the actual structure
699  */
700 static struct rpc_t rpc_specimen;
701 #define IP_PKTSIZE (CONFIG_NET_MAXDEFRAG + sizeof(rpc_specimen.u.reply))
702 
703 #define IP_MAXUDP (IP_PKTSIZE - IP_HDR_SIZE)
704 
705 /*
706  * this is the packet being assembled, either data or frag control.
707  * Fragments go by 8 bytes, so this union must be 8 bytes long
708  */
709 struct hole {
710 	/* first_byte is address of this structure */
711 	u16 last_byte;	/* last byte in this hole + 1 (begin of next hole) */
712 	u16 next_hole;	/* index of next (in 8-b blocks), 0 == none */
713 	u16 prev_hole;	/* index of prev, 0 == none */
714 	u16 unused;
715 };
716 
717 static struct ip_udp_hdr *__NetDefragment(struct ip_udp_hdr *ip, int *lenp)
718 {
719 	static uchar pkt_buff[IP_PKTSIZE] __aligned(PKTALIGN);
720 	static u16 first_hole, total_len;
721 	struct hole *payload, *thisfrag, *h, *newh;
722 	struct ip_udp_hdr *localip = (struct ip_udp_hdr *)pkt_buff;
723 	uchar *indata = (uchar *)ip;
724 	int offset8, start, len, done = 0;
725 	u16 ip_off = ntohs(ip->ip_off);
726 
727 	/* payload starts after IP header, this fragment is in there */
728 	payload = (struct hole *)(pkt_buff + IP_HDR_SIZE);
729 	offset8 =  (ip_off & IP_OFFS);
730 	thisfrag = payload + offset8;
731 	start = offset8 * 8;
732 	len = ntohs(ip->ip_len) - IP_HDR_SIZE;
733 
734 	if (start + len > IP_MAXUDP) /* fragment extends too far */
735 		return NULL;
736 
737 	if (!total_len || localip->ip_id != ip->ip_id) {
738 		/* new (or different) packet, reset structs */
739 		total_len = 0xffff;
740 		payload[0].last_byte = ~0;
741 		payload[0].next_hole = 0;
742 		payload[0].prev_hole = 0;
743 		first_hole = 0;
744 		/* any IP header will work, copy the first we received */
745 		memcpy(localip, ip, IP_HDR_SIZE);
746 	}
747 
748 	/*
749 	 * What follows is the reassembly algorithm. We use the payload
750 	 * array as a linked list of hole descriptors, as each hole starts
751 	 * at a multiple of 8 bytes. However, last byte can be whatever value,
752 	 * so it is represented as byte count, not as 8-byte blocks.
753 	 */
754 
755 	h = payload + first_hole;
756 	while (h->last_byte < start) {
757 		if (!h->next_hole) {
758 			/* no hole that far away */
759 			return NULL;
760 		}
761 		h = payload + h->next_hole;
762 	}
763 
764 	/* last fragment may be 1..7 bytes, the "+7" forces acceptance */
765 	if (offset8 + ((len + 7) / 8) <= h - payload) {
766 		/* no overlap with holes (dup fragment?) */
767 		return NULL;
768 	}
769 
770 	if (!(ip_off & IP_FLAGS_MFRAG)) {
771 		/* no more fragmentss: truncate this (last) hole */
772 		total_len = start + len;
773 		h->last_byte = start + len;
774 	}
775 
776 	/*
777 	 * There is some overlap: fix the hole list. This code doesn't
778 	 * deal with a fragment that overlaps with two different holes
779 	 * (thus being a superset of a previously-received fragment).
780 	 */
781 
782 	if ((h >= thisfrag) && (h->last_byte <= start + len)) {
783 		/* complete overlap with hole: remove hole */
784 		if (!h->prev_hole && !h->next_hole) {
785 			/* last remaining hole */
786 			done = 1;
787 		} else if (!h->prev_hole) {
788 			/* first hole */
789 			first_hole = h->next_hole;
790 			payload[h->next_hole].prev_hole = 0;
791 		} else if (!h->next_hole) {
792 			/* last hole */
793 			payload[h->prev_hole].next_hole = 0;
794 		} else {
795 			/* in the middle of the list */
796 			payload[h->next_hole].prev_hole = h->prev_hole;
797 			payload[h->prev_hole].next_hole = h->next_hole;
798 		}
799 
800 	} else if (h->last_byte <= start + len) {
801 		/* overlaps with final part of the hole: shorten this hole */
802 		h->last_byte = start;
803 
804 	} else if (h >= thisfrag) {
805 		/* overlaps with initial part of the hole: move this hole */
806 		newh = thisfrag + (len / 8);
807 		*newh = *h;
808 		h = newh;
809 		if (h->next_hole)
810 			payload[h->next_hole].prev_hole = (h - payload);
811 		if (h->prev_hole)
812 			payload[h->prev_hole].next_hole = (h - payload);
813 		else
814 			first_hole = (h - payload);
815 
816 	} else {
817 		/* fragment sits in the middle: split the hole */
818 		newh = thisfrag + (len / 8);
819 		*newh = *h;
820 		h->last_byte = start;
821 		h->next_hole = (newh - payload);
822 		newh->prev_hole = (h - payload);
823 		if (newh->next_hole)
824 			payload[newh->next_hole].prev_hole = (newh - payload);
825 	}
826 
827 	/* finally copy this fragment and possibly return whole packet */
828 	memcpy((uchar *)thisfrag, indata + IP_HDR_SIZE, len);
829 	if (!done)
830 		return NULL;
831 
832 	localip->ip_len = htons(total_len);
833 	*lenp = total_len + IP_HDR_SIZE;
834 	return localip;
835 }
836 
837 static inline struct ip_udp_hdr *NetDefragment(struct ip_udp_hdr *ip, int *lenp)
838 {
839 	u16 ip_off = ntohs(ip->ip_off);
840 	if (!(ip_off & (IP_OFFS | IP_FLAGS_MFRAG)))
841 		return ip; /* not a fragment */
842 	return __NetDefragment(ip, lenp);
843 }
844 
845 #else /* !CONFIG_IP_DEFRAG */
846 
847 static inline struct ip_udp_hdr *NetDefragment(struct ip_udp_hdr *ip, int *lenp)
848 {
849 	u16 ip_off = ntohs(ip->ip_off);
850 	if (!(ip_off & (IP_OFFS | IP_FLAGS_MFRAG)))
851 		return ip; /* not a fragment */
852 	return NULL;
853 }
854 #endif
855 
856 /**
857  * Receive an ICMP packet. We deal with REDIRECT and PING here, and silently
858  * drop others.
859  *
860  * @parma ip	IP packet containing the ICMP
861  */
862 static void receive_icmp(struct ip_udp_hdr *ip, int len,
863 			IPaddr_t src_ip, struct ethernet_hdr *et)
864 {
865 	struct icmp_hdr *icmph = (struct icmp_hdr *)&ip->udp_src;
866 
867 	switch (icmph->type) {
868 	case ICMP_REDIRECT:
869 		if (icmph->code != ICMP_REDIR_HOST)
870 			return;
871 		printf(" ICMP Host Redirect to %pI4 ",
872 			&icmph->un.gateway);
873 		break;
874 	default:
875 #if defined(CONFIG_CMD_PING)
876 		ping_receive(et, ip, len);
877 #endif
878 #ifdef CONFIG_CMD_TFTPPUT
879 		if (packet_icmp_handler)
880 			packet_icmp_handler(icmph->type, icmph->code,
881 				ntohs(ip->udp_dst), src_ip, ntohs(ip->udp_src),
882 				icmph->un.data, ntohs(ip->udp_len));
883 #endif
884 		break;
885 	}
886 }
887 
888 void
889 NetReceive(uchar *inpkt, int len)
890 {
891 	struct ethernet_hdr *et;
892 	struct ip_udp_hdr *ip;
893 	IPaddr_t dst_ip;
894 	IPaddr_t src_ip;
895 	int eth_proto;
896 #if defined(CONFIG_CMD_CDP)
897 	int iscdp;
898 #endif
899 	ushort cti = 0, vlanid = VLAN_NONE, myvlanid, mynvlanid;
900 
901 	debug("packet received\n");
902 
903 	NetRxPacket = inpkt;
904 	NetRxPacketLen = len;
905 	et = (struct ethernet_hdr *)inpkt;
906 
907 	/* too small packet? */
908 	if (len < ETHER_HDR_SIZE)
909 		return;
910 
911 #ifdef CONFIG_API
912 	if (push_packet) {
913 		(*push_packet)(inpkt, len);
914 		return;
915 	}
916 #endif
917 
918 #if defined(CONFIG_CMD_CDP)
919 	/* keep track if packet is CDP */
920 	iscdp = is_cdp_packet(et->et_dest);
921 #endif
922 
923 	myvlanid = ntohs(NetOurVLAN);
924 	if (myvlanid == (ushort)-1)
925 		myvlanid = VLAN_NONE;
926 	mynvlanid = ntohs(NetOurNativeVLAN);
927 	if (mynvlanid == (ushort)-1)
928 		mynvlanid = VLAN_NONE;
929 
930 	eth_proto = ntohs(et->et_protlen);
931 
932 	debug("packet received\n");
933 
934 	if (eth_proto < 1514) {
935 		struct e802_hdr *et802 = (struct e802_hdr *)et;
936 		/*
937 		 *	Got a 802.2 packet.  Check the other protocol field.
938 		 *	XXX VLAN over 802.2+SNAP not implemented!
939 		 */
940 		eth_proto = ntohs(et802->et_prot);
941 
942 		ip = (struct ip_udp_hdr *)(inpkt + E802_HDR_SIZE);
943 		len -= E802_HDR_SIZE;
944 
945 	} else if (eth_proto != PROT_VLAN) {	/* normal packet */
946 		ip = (struct ip_udp_hdr *)(inpkt + ETHER_HDR_SIZE);
947 		len -= ETHER_HDR_SIZE;
948 
949 	} else {			/* VLAN packet */
950 		struct vlan_ethernet_hdr *vet =
951 			(struct vlan_ethernet_hdr *)et;
952 
953 		debug("VLAN packet received\n");
954 
955 		/* too small packet? */
956 		if (len < VLAN_ETHER_HDR_SIZE)
957 			return;
958 
959 		/* if no VLAN active */
960 		if ((ntohs(NetOurVLAN) & VLAN_IDMASK) == VLAN_NONE
961 #if defined(CONFIG_CMD_CDP)
962 				&& iscdp == 0
963 #endif
964 				)
965 			return;
966 
967 		cti = ntohs(vet->vet_tag);
968 		vlanid = cti & VLAN_IDMASK;
969 		eth_proto = ntohs(vet->vet_type);
970 
971 		ip = (struct ip_udp_hdr *)(inpkt + VLAN_ETHER_HDR_SIZE);
972 		len -= VLAN_ETHER_HDR_SIZE;
973 	}
974 
975 	debug("Receive from protocol 0x%x\n", eth_proto);
976 
977 #if defined(CONFIG_CMD_CDP)
978 	if (iscdp) {
979 		cdp_receive((uchar *)ip, len);
980 		return;
981 	}
982 #endif
983 
984 	if ((myvlanid & VLAN_IDMASK) != VLAN_NONE) {
985 		if (vlanid == VLAN_NONE)
986 			vlanid = (mynvlanid & VLAN_IDMASK);
987 		/* not matched? */
988 		if (vlanid != (myvlanid & VLAN_IDMASK))
989 			return;
990 	}
991 
992 	switch (eth_proto) {
993 
994 	case PROT_ARP:
995 		ArpReceive(et, ip, len);
996 		break;
997 
998 #ifdef CONFIG_CMD_RARP
999 	case PROT_RARP:
1000 		rarp_receive(ip, len);
1001 		break;
1002 #endif
1003 	case PROT_IP:
1004 		debug("Got IP\n");
1005 		/* Before we start poking the header, make sure it is there */
1006 		if (len < IP_UDP_HDR_SIZE) {
1007 			debug("len bad %d < %lu\n", len,
1008 				(ulong)IP_UDP_HDR_SIZE);
1009 			return;
1010 		}
1011 		/* Check the packet length */
1012 		if (len < ntohs(ip->ip_len)) {
1013 			printf("len bad %d < %d\n", len, ntohs(ip->ip_len));
1014 			return;
1015 		}
1016 		len = ntohs(ip->ip_len);
1017 		debug("len=%d, v=%02x\n", len, ip->ip_hl_v & 0xff);
1018 
1019 		/* Can't deal with anything except IPv4 */
1020 		if ((ip->ip_hl_v & 0xf0) != 0x40)
1021 			return;
1022 		/* Can't deal with IP options (headers != 20 bytes) */
1023 		if ((ip->ip_hl_v & 0x0f) > 0x05)
1024 			return;
1025 		/* Check the Checksum of the header */
1026 		if (!NetCksumOk((uchar *)ip, IP_HDR_SIZE / 2)) {
1027 			puts("checksum bad\n");
1028 			return;
1029 		}
1030 		/* If it is not for us, ignore it */
1031 		dst_ip = NetReadIP(&ip->ip_dst);
1032 		if (NetOurIP && dst_ip != NetOurIP && dst_ip != 0xFFFFFFFF) {
1033 #ifdef CONFIG_MCAST_TFTP
1034 			if (Mcast_addr != dst_ip)
1035 #endif
1036 				return;
1037 		}
1038 		/* Read source IP address for later use */
1039 		src_ip = NetReadIP(&ip->ip_src);
1040 		/*
1041 		 * The function returns the unchanged packet if it's not
1042 		 * a fragment, and either the complete packet or NULL if
1043 		 * it is a fragment (if !CONFIG_IP_DEFRAG, it returns NULL)
1044 		 */
1045 		ip = NetDefragment(ip, &len);
1046 		if (!ip)
1047 			return;
1048 		/*
1049 		 * watch for ICMP host redirects
1050 		 *
1051 		 * There is no real handler code (yet). We just watch
1052 		 * for ICMP host redirect messages. In case anybody
1053 		 * sees these messages: please contact me
1054 		 * (wd@denx.de), or - even better - send me the
1055 		 * necessary fixes :-)
1056 		 *
1057 		 * Note: in all cases where I have seen this so far
1058 		 * it was a problem with the router configuration,
1059 		 * for instance when a router was configured in the
1060 		 * BOOTP reply, but the TFTP server was on the same
1061 		 * subnet. So this is probably a warning that your
1062 		 * configuration might be wrong. But I'm not really
1063 		 * sure if there aren't any other situations.
1064 		 *
1065 		 * Simon Glass <sjg@chromium.org>: We get an ICMP when
1066 		 * we send a tftp packet to a dead connection, or when
1067 		 * there is no server at the other end.
1068 		 */
1069 		if (ip->ip_p == IPPROTO_ICMP) {
1070 			receive_icmp(ip, len, src_ip, et);
1071 			return;
1072 		} else if (ip->ip_p != IPPROTO_UDP) {	/* Only UDP packets */
1073 			return;
1074 		}
1075 
1076 #ifdef CONFIG_UDP_CHECKSUM
1077 		if (ip->udp_xsum != 0) {
1078 			ulong   xsum;
1079 			ushort *sumptr;
1080 			ushort  sumlen;
1081 
1082 			xsum  = ip->ip_p;
1083 			xsum += (ntohs(ip->udp_len));
1084 			xsum += (ntohl(ip->ip_src) >> 16) & 0x0000ffff;
1085 			xsum += (ntohl(ip->ip_src) >>  0) & 0x0000ffff;
1086 			xsum += (ntohl(ip->ip_dst) >> 16) & 0x0000ffff;
1087 			xsum += (ntohl(ip->ip_dst) >>  0) & 0x0000ffff;
1088 
1089 			sumlen = ntohs(ip->udp_len);
1090 			sumptr = (ushort *) &(ip->udp_src);
1091 
1092 			while (sumlen > 1) {
1093 				ushort sumdata;
1094 
1095 				sumdata = *sumptr++;
1096 				xsum += ntohs(sumdata);
1097 				sumlen -= 2;
1098 			}
1099 			if (sumlen > 0) {
1100 				ushort sumdata;
1101 
1102 				sumdata = *(unsigned char *) sumptr;
1103 				sumdata = (sumdata << 8) & 0xff00;
1104 				xsum += sumdata;
1105 			}
1106 			while ((xsum >> 16) != 0) {
1107 				xsum = (xsum & 0x0000ffff) +
1108 				       ((xsum >> 16) & 0x0000ffff);
1109 			}
1110 			if ((xsum != 0x00000000) && (xsum != 0x0000ffff)) {
1111 				printf(" UDP wrong checksum %08lx %08x\n",
1112 					xsum, ntohs(ip->udp_xsum));
1113 				return;
1114 			}
1115 		}
1116 #endif
1117 
1118 
1119 #ifdef CONFIG_NETCONSOLE
1120 		nc_input_packet((uchar *)ip + IP_UDP_HDR_SIZE,
1121 					ntohs(ip->udp_dst),
1122 					ntohs(ip->udp_src),
1123 					ntohs(ip->udp_len) - UDP_HDR_SIZE);
1124 #endif
1125 		/*
1126 		 *	IP header OK.  Pass the packet to the current handler.
1127 		 */
1128 		(*udp_packet_handler)((uchar *)ip + IP_UDP_HDR_SIZE,
1129 				ntohs(ip->udp_dst),
1130 				src_ip,
1131 				ntohs(ip->udp_src),
1132 				ntohs(ip->udp_len) - UDP_HDR_SIZE);
1133 		break;
1134 	}
1135 }
1136 
1137 
1138 /**********************************************************************/
1139 
1140 static int net_check_prereq(enum proto_t protocol)
1141 {
1142 	switch (protocol) {
1143 		/* Fall through */
1144 #if defined(CONFIG_CMD_PING)
1145 	case PING:
1146 		if (NetPingIP == 0) {
1147 			puts("*** ERROR: ping address not given\n");
1148 			return 1;
1149 		}
1150 		goto common;
1151 #endif
1152 #if defined(CONFIG_CMD_SNTP)
1153 	case SNTP:
1154 		if (NetNtpServerIP == 0) {
1155 			puts("*** ERROR: NTP server address not given\n");
1156 			return 1;
1157 		}
1158 		goto common;
1159 #endif
1160 #if defined(CONFIG_CMD_DNS)
1161 	case DNS:
1162 		if (NetOurDNSIP == 0) {
1163 			puts("*** ERROR: DNS server address not given\n");
1164 			return 1;
1165 		}
1166 		goto common;
1167 #endif
1168 #if defined(CONFIG_CMD_NFS)
1169 	case NFS:
1170 #endif
1171 	case TFTPGET:
1172 	case TFTPPUT:
1173 		if (NetServerIP == 0) {
1174 			puts("*** ERROR: `serverip' not set\n");
1175 			return 1;
1176 		}
1177 #if	defined(CONFIG_CMD_PING) || defined(CONFIG_CMD_SNTP) || \
1178 	defined(CONFIG_CMD_DNS)
1179 common:
1180 #endif
1181 		/* Fall through */
1182 
1183 	case NETCONS:
1184 	case TFTPSRV:
1185 		if (NetOurIP == 0) {
1186 			puts("*** ERROR: `ipaddr' not set\n");
1187 			return 1;
1188 		}
1189 		/* Fall through */
1190 
1191 #ifdef CONFIG_CMD_RARP
1192 	case RARP:
1193 #endif
1194 	case BOOTP:
1195 	case CDP:
1196 	case DHCP:
1197 		if (memcmp(NetOurEther, "\0\0\0\0\0\0", 6) == 0) {
1198 			int num = eth_get_dev_index();
1199 
1200 			switch (num) {
1201 			case -1:
1202 				puts("*** ERROR: No ethernet found.\n");
1203 				return 1;
1204 			case 0:
1205 				puts("*** ERROR: `ethaddr' not set\n");
1206 				break;
1207 			default:
1208 				printf("*** ERROR: `eth%daddr' not set\n",
1209 					num);
1210 				break;
1211 			}
1212 
1213 			NetStartAgain();
1214 			return 2;
1215 		}
1216 		/* Fall through */
1217 	default:
1218 		return 0;
1219 	}
1220 	return 0;		/* OK */
1221 }
1222 /**********************************************************************/
1223 
1224 int
1225 NetCksumOk(uchar *ptr, int len)
1226 {
1227 	return !((NetCksum(ptr, len) + 1) & 0xfffe);
1228 }
1229 
1230 
1231 unsigned
1232 NetCksum(uchar *ptr, int len)
1233 {
1234 	ulong	xsum;
1235 	ushort *p = (ushort *)ptr;
1236 
1237 	xsum = 0;
1238 	while (len-- > 0)
1239 		xsum += *p++;
1240 	xsum = (xsum & 0xffff) + (xsum >> 16);
1241 	xsum = (xsum & 0xffff) + (xsum >> 16);
1242 	return xsum & 0xffff;
1243 }
1244 
1245 int
1246 NetEthHdrSize(void)
1247 {
1248 	ushort myvlanid;
1249 
1250 	myvlanid = ntohs(NetOurVLAN);
1251 	if (myvlanid == (ushort)-1)
1252 		myvlanid = VLAN_NONE;
1253 
1254 	return ((myvlanid & VLAN_IDMASK) == VLAN_NONE) ? ETHER_HDR_SIZE :
1255 		VLAN_ETHER_HDR_SIZE;
1256 }
1257 
1258 int
1259 NetSetEther(uchar *xet, uchar * addr, uint prot)
1260 {
1261 	struct ethernet_hdr *et = (struct ethernet_hdr *)xet;
1262 	ushort myvlanid;
1263 
1264 	myvlanid = ntohs(NetOurVLAN);
1265 	if (myvlanid == (ushort)-1)
1266 		myvlanid = VLAN_NONE;
1267 
1268 	memcpy(et->et_dest, addr, 6);
1269 	memcpy(et->et_src, NetOurEther, 6);
1270 	if ((myvlanid & VLAN_IDMASK) == VLAN_NONE) {
1271 		et->et_protlen = htons(prot);
1272 		return ETHER_HDR_SIZE;
1273 	} else {
1274 		struct vlan_ethernet_hdr *vet =
1275 			(struct vlan_ethernet_hdr *)xet;
1276 
1277 		vet->vet_vlan_type = htons(PROT_VLAN);
1278 		vet->vet_tag = htons((0 << 5) | (myvlanid & VLAN_IDMASK));
1279 		vet->vet_type = htons(prot);
1280 		return VLAN_ETHER_HDR_SIZE;
1281 	}
1282 }
1283 
1284 int net_update_ether(struct ethernet_hdr *et, uchar *addr, uint prot)
1285 {
1286 	ushort protlen;
1287 
1288 	memcpy(et->et_dest, addr, 6);
1289 	memcpy(et->et_src, NetOurEther, 6);
1290 	protlen = ntohs(et->et_protlen);
1291 	if (protlen == PROT_VLAN) {
1292 		struct vlan_ethernet_hdr *vet =
1293 			(struct vlan_ethernet_hdr *)et;
1294 		vet->vet_type = htons(prot);
1295 		return VLAN_ETHER_HDR_SIZE;
1296 	} else if (protlen > 1514) {
1297 		et->et_protlen = htons(prot);
1298 		return ETHER_HDR_SIZE;
1299 	} else {
1300 		/* 802.2 + SNAP */
1301 		struct e802_hdr *et802 = (struct e802_hdr *)et;
1302 		et802->et_prot = htons(prot);
1303 		return E802_HDR_SIZE;
1304 	}
1305 }
1306 
1307 void net_set_ip_header(uchar *pkt, IPaddr_t dest, IPaddr_t source)
1308 {
1309 	struct ip_udp_hdr *ip = (struct ip_udp_hdr *)pkt;
1310 
1311 	/*
1312 	 *	Construct an IP header.
1313 	 */
1314 	/* IP_HDR_SIZE / 4 (not including UDP) */
1315 	ip->ip_hl_v  = 0x45;
1316 	ip->ip_tos   = 0;
1317 	ip->ip_len   = htons(IP_HDR_SIZE);
1318 	ip->ip_id    = htons(NetIPID++);
1319 	ip->ip_off   = htons(IP_FLAGS_DFRAG);	/* Don't fragment */
1320 	ip->ip_ttl   = 255;
1321 	ip->ip_sum   = 0;
1322 	/* already in network byte order */
1323 	NetCopyIP((void *)&ip->ip_src, &source);
1324 	/* already in network byte order */
1325 	NetCopyIP((void *)&ip->ip_dst, &dest);
1326 }
1327 
1328 void net_set_udp_header(uchar *pkt, IPaddr_t dest, int dport, int sport,
1329 			int len)
1330 {
1331 	struct ip_udp_hdr *ip = (struct ip_udp_hdr *)pkt;
1332 
1333 	/*
1334 	 *	If the data is an odd number of bytes, zero the
1335 	 *	byte after the last byte so that the checksum
1336 	 *	will work.
1337 	 */
1338 	if (len & 1)
1339 		pkt[IP_UDP_HDR_SIZE + len] = 0;
1340 
1341 	net_set_ip_header(pkt, dest, NetOurIP);
1342 	ip->ip_len   = htons(IP_UDP_HDR_SIZE + len);
1343 	ip->ip_p     = IPPROTO_UDP;
1344 	ip->ip_sum   = ~NetCksum((uchar *)ip, IP_HDR_SIZE >> 1);
1345 
1346 	ip->udp_src  = htons(sport);
1347 	ip->udp_dst  = htons(dport);
1348 	ip->udp_len  = htons(UDP_HDR_SIZE + len);
1349 	ip->udp_xsum = 0;
1350 }
1351 
1352 void copy_filename(char *dst, const char *src, int size)
1353 {
1354 	if (*src && (*src == '"')) {
1355 		++src;
1356 		--size;
1357 	}
1358 
1359 	while ((--size > 0) && *src && (*src != '"'))
1360 		*dst++ = *src++;
1361 	*dst = '\0';
1362 }
1363 
1364 #if	defined(CONFIG_CMD_NFS)		|| \
1365 	defined(CONFIG_CMD_SNTP)	|| \
1366 	defined(CONFIG_CMD_DNS)
1367 /*
1368  * make port a little random (1024-17407)
1369  * This keeps the math somewhat trivial to compute, and seems to work with
1370  * all supported protocols/clients/servers
1371  */
1372 unsigned int random_port(void)
1373 {
1374 	return 1024 + (get_timer(0) % 0x4000);
1375 }
1376 #endif
1377 
1378 void ip_to_string(IPaddr_t x, char *s)
1379 {
1380 	x = ntohl(x);
1381 	sprintf(s, "%d.%d.%d.%d",
1382 		(int) ((x >> 24) & 0xff),
1383 		(int) ((x >> 16) & 0xff),
1384 		(int) ((x >> 8) & 0xff), (int) ((x >> 0) & 0xff)
1385 	);
1386 }
1387 
1388 void VLAN_to_string(ushort x, char *s)
1389 {
1390 	x = ntohs(x);
1391 
1392 	if (x == (ushort)-1)
1393 		x = VLAN_NONE;
1394 
1395 	if (x == VLAN_NONE)
1396 		strcpy(s, "none");
1397 	else
1398 		sprintf(s, "%d", x & VLAN_IDMASK);
1399 }
1400 
1401 ushort string_to_VLAN(const char *s)
1402 {
1403 	ushort id;
1404 
1405 	if (s == NULL)
1406 		return htons(VLAN_NONE);
1407 
1408 	if (*s < '0' || *s > '9')
1409 		id = VLAN_NONE;
1410 	else
1411 		id = (ushort)simple_strtoul(s, NULL, 10);
1412 
1413 	return htons(id);
1414 }
1415 
1416 ushort getenv_VLAN(char *var)
1417 {
1418 	return string_to_VLAN(getenv(var));
1419 }
1420