xref: /openbmc/u-boot/net/net.c (revision ec05ae58)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  *	Copied from Linux Monitor (LiMon) - Networking.
4  *
5  *	Copyright 1994 - 2000 Neil Russell.
6  *	(See License)
7  *	Copyright 2000 Roland Borde
8  *	Copyright 2000 Paolo Scaffardi
9  *	Copyright 2000-2002 Wolfgang Denk, wd@denx.de
10  */
11 
12 /*
13  * General Desription:
14  *
15  * The user interface supports commands for BOOTP, RARP, and TFTP.
16  * Also, we support ARP internally. Depending on available data,
17  * these interact as follows:
18  *
19  * BOOTP:
20  *
21  *	Prerequisites:	- own ethernet address
22  *	We want:	- own IP address
23  *			- TFTP server IP address
24  *			- name of bootfile
25  *	Next step:	ARP
26  *
27  * LINK_LOCAL:
28  *
29  *	Prerequisites:	- own ethernet address
30  *	We want:	- own IP address
31  *	Next step:	ARP
32  *
33  * RARP:
34  *
35  *	Prerequisites:	- own ethernet address
36  *	We want:	- own IP address
37  *			- TFTP server IP address
38  *	Next step:	ARP
39  *
40  * ARP:
41  *
42  *	Prerequisites:	- own ethernet address
43  *			- own IP address
44  *			- TFTP server IP address
45  *	We want:	- TFTP server ethernet address
46  *	Next step:	TFTP
47  *
48  * DHCP:
49  *
50  *     Prerequisites:	- own ethernet address
51  *     We want:		- IP, Netmask, ServerIP, Gateway IP
52  *			- bootfilename, lease time
53  *     Next step:	- TFTP
54  *
55  * TFTP:
56  *
57  *	Prerequisites:	- own ethernet address
58  *			- own IP address
59  *			- TFTP server IP address
60  *			- TFTP server ethernet address
61  *			- name of bootfile (if unknown, we use a default name
62  *			  derived from our own IP address)
63  *	We want:	- load the boot file
64  *	Next step:	none
65  *
66  * NFS:
67  *
68  *	Prerequisites:	- own ethernet address
69  *			- own IP address
70  *			- name of bootfile (if unknown, we use a default name
71  *			  derived from our own IP address)
72  *	We want:	- load the boot file
73  *	Next step:	none
74  *
75  * SNTP:
76  *
77  *	Prerequisites:	- own ethernet address
78  *			- own IP address
79  *	We want:	- network time
80  *	Next step:	none
81  *
82  * WOL:
83  *
84  *	Prerequisites:	- own ethernet address
85  *	We want:	- magic packet or timeout
86  *	Next step:	none
87  */
88 
89 
90 #include <common.h>
91 #include <command.h>
92 #include <console.h>
93 #include <environment.h>
94 #include <errno.h>
95 #include <net.h>
96 #include <net/fastboot.h>
97 #include <net/tftp.h>
98 #include <net/ncsi.h>
99 #if defined(CONFIG_LED_STATUS)
100 #include <miiphy.h>
101 #include <status_led.h>
102 #endif
103 #include <watchdog.h>
104 #include <linux/compiler.h>
105 #include "arp.h"
106 #include "bootp.h"
107 #include "cdp.h"
108 #if defined(CONFIG_CMD_DNS)
109 #include "dns.h"
110 #endif
111 #include "link_local.h"
112 #include "nfs.h"
113 #include "ping.h"
114 #include "rarp.h"
115 #if defined(CONFIG_CMD_SNTP)
116 #include "sntp.h"
117 #endif
118 #if defined(CONFIG_CMD_WOL)
119 #include "wol.h"
120 #endif
121 
122 /** BOOTP EXTENTIONS **/
123 
124 /* Our subnet mask (0=unknown) */
125 struct in_addr net_netmask;
126 /* Our gateways IP address */
127 struct in_addr net_gateway;
128 /* Our DNS IP address */
129 struct in_addr net_dns_server;
130 #if defined(CONFIG_BOOTP_DNS2)
131 /* Our 2nd DNS IP address */
132 struct in_addr net_dns_server2;
133 #endif
134 
135 /** END OF BOOTP EXTENTIONS **/
136 
137 /* Our ethernet address */
138 u8 net_ethaddr[6];
139 /* Boot server enet address */
140 u8 net_server_ethaddr[6];
141 /* Our IP addr (0 = unknown) */
142 struct in_addr	net_ip;
143 /* Server IP addr (0 = unknown) */
144 struct in_addr	net_server_ip;
145 /* Current receive packet */
146 uchar *net_rx_packet;
147 /* Current rx packet length */
148 int		net_rx_packet_len;
149 /* IP packet ID */
150 static unsigned	net_ip_id;
151 /* Ethernet bcast address */
152 const u8 net_bcast_ethaddr[6] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
153 const u8 net_null_ethaddr[6];
154 #if defined(CONFIG_API) || defined(CONFIG_EFI_LOADER)
155 void (*push_packet)(void *, int len) = 0;
156 #endif
157 /* Network loop state */
158 enum net_loop_state net_state;
159 /* Tried all network devices */
160 int		net_restart_wrap;
161 /* Network loop restarted */
162 static int	net_restarted;
163 /* At least one device configured */
164 static int	net_dev_exists;
165 
166 /* XXX in both little & big endian machines 0xFFFF == ntohs(-1) */
167 /* default is without VLAN */
168 ushort		net_our_vlan = 0xFFFF;
169 /* ditto */
170 ushort		net_native_vlan = 0xFFFF;
171 
172 /* Boot File name */
173 char net_boot_file_name[1024];
174 /* Indicates whether the file name was specified on the command line */
175 bool net_boot_file_name_explicit;
176 /* The actual transferred size of the bootfile (in bytes) */
177 u32 net_boot_file_size;
178 /* Boot file size in blocks as reported by the DHCP server */
179 u32 net_boot_file_expected_size_in_blocks;
180 
181 #if defined(CONFIG_CMD_SNTP)
182 /* NTP server IP address */
183 struct in_addr	net_ntp_server;
184 /* offset time from UTC */
185 int		net_ntp_time_offset;
186 #endif
187 
188 static uchar net_pkt_buf[(PKTBUFSRX+1) * PKTSIZE_ALIGN + PKTALIGN];
189 /* Receive packets */
190 uchar *net_rx_packets[PKTBUFSRX];
191 /* Current UDP RX packet handler */
192 static rxhand_f *udp_packet_handler;
193 /* Current ARP RX packet handler */
194 static rxhand_f *arp_packet_handler;
195 #ifdef CONFIG_CMD_TFTPPUT
196 /* Current ICMP rx handler */
197 static rxhand_icmp_f *packet_icmp_handler;
198 #endif
199 /* Current timeout handler */
200 static thand_f *time_handler;
201 /* Time base value */
202 static ulong	time_start;
203 /* Current timeout value */
204 static ulong	time_delta;
205 /* THE transmit packet */
206 uchar *net_tx_packet;
207 
208 static int net_check_prereq(enum proto_t protocol);
209 
210 static int net_try_count;
211 
212 int __maybe_unused net_busy_flag;
213 
214 /**********************************************************************/
215 
on_ipaddr(const char * name,const char * value,enum env_op op,int flags)216 static int on_ipaddr(const char *name, const char *value, enum env_op op,
217 	int flags)
218 {
219 	if (flags & H_PROGRAMMATIC)
220 		return 0;
221 
222 	net_ip = string_to_ip(value);
223 
224 	return 0;
225 }
226 U_BOOT_ENV_CALLBACK(ipaddr, on_ipaddr);
227 
on_gatewayip(const char * name,const char * value,enum env_op op,int flags)228 static int on_gatewayip(const char *name, const char *value, enum env_op op,
229 	int flags)
230 {
231 	if (flags & H_PROGRAMMATIC)
232 		return 0;
233 
234 	net_gateway = string_to_ip(value);
235 
236 	return 0;
237 }
238 U_BOOT_ENV_CALLBACK(gatewayip, on_gatewayip);
239 
on_netmask(const char * name,const char * value,enum env_op op,int flags)240 static int on_netmask(const char *name, const char *value, enum env_op op,
241 	int flags)
242 {
243 	if (flags & H_PROGRAMMATIC)
244 		return 0;
245 
246 	net_netmask = string_to_ip(value);
247 
248 	return 0;
249 }
250 U_BOOT_ENV_CALLBACK(netmask, on_netmask);
251 
on_serverip(const char * name,const char * value,enum env_op op,int flags)252 static int on_serverip(const char *name, const char *value, enum env_op op,
253 	int flags)
254 {
255 	if (flags & H_PROGRAMMATIC)
256 		return 0;
257 
258 	net_server_ip = string_to_ip(value);
259 
260 	return 0;
261 }
262 U_BOOT_ENV_CALLBACK(serverip, on_serverip);
263 
on_nvlan(const char * name,const char * value,enum env_op op,int flags)264 static int on_nvlan(const char *name, const char *value, enum env_op op,
265 	int flags)
266 {
267 	if (flags & H_PROGRAMMATIC)
268 		return 0;
269 
270 	net_native_vlan = string_to_vlan(value);
271 
272 	return 0;
273 }
274 U_BOOT_ENV_CALLBACK(nvlan, on_nvlan);
275 
on_vlan(const char * name,const char * value,enum env_op op,int flags)276 static int on_vlan(const char *name, const char *value, enum env_op op,
277 	int flags)
278 {
279 	if (flags & H_PROGRAMMATIC)
280 		return 0;
281 
282 	net_our_vlan = string_to_vlan(value);
283 
284 	return 0;
285 }
286 U_BOOT_ENV_CALLBACK(vlan, on_vlan);
287 
288 #if defined(CONFIG_CMD_DNS)
on_dnsip(const char * name,const char * value,enum env_op op,int flags)289 static int on_dnsip(const char *name, const char *value, enum env_op op,
290 	int flags)
291 {
292 	if (flags & H_PROGRAMMATIC)
293 		return 0;
294 
295 	net_dns_server = string_to_ip(value);
296 
297 	return 0;
298 }
299 U_BOOT_ENV_CALLBACK(dnsip, on_dnsip);
300 #endif
301 
302 /*
303  * Check if autoload is enabled. If so, use either NFS or TFTP to download
304  * the boot file.
305  */
net_auto_load(void)306 void net_auto_load(void)
307 {
308 #if defined(CONFIG_CMD_NFS)
309 	const char *s = env_get("autoload");
310 
311 	if (s != NULL && strcmp(s, "NFS") == 0) {
312 		if (net_check_prereq(NFS)) {
313 /* We aren't expecting to get a serverip, so just accept the assigned IP */
314 #ifdef CONFIG_BOOTP_SERVERIP
315 			net_set_state(NETLOOP_SUCCESS);
316 #else
317 			printf("Cannot autoload with NFS\n");
318 			net_set_state(NETLOOP_FAIL);
319 #endif
320 			return;
321 		}
322 		/*
323 		 * Use NFS to load the bootfile.
324 		 */
325 		nfs_start();
326 		return;
327 	}
328 #endif
329 	if (env_get_yesno("autoload") == 0) {
330 		/*
331 		 * Just use BOOTP/RARP to configure system;
332 		 * Do not use TFTP to load the bootfile.
333 		 */
334 		net_set_state(NETLOOP_SUCCESS);
335 		return;
336 	}
337 	if (net_check_prereq(TFTPGET)) {
338 /* We aren't expecting to get a serverip, so just accept the assigned IP */
339 #ifdef CONFIG_BOOTP_SERVERIP
340 		net_set_state(NETLOOP_SUCCESS);
341 #else
342 		printf("Cannot autoload with TFTPGET\n");
343 		net_set_state(NETLOOP_FAIL);
344 #endif
345 		return;
346 	}
347 	tftp_start(TFTPGET);
348 }
349 
net_init_loop(void)350 static void net_init_loop(void)
351 {
352 	if (eth_get_dev())
353 		memcpy(net_ethaddr, eth_get_ethaddr(), 6);
354 
355 	return;
356 }
357 
net_clear_handlers(void)358 static void net_clear_handlers(void)
359 {
360 	net_set_udp_handler(NULL);
361 	net_set_arp_handler(NULL);
362 	net_set_timeout_handler(0, NULL);
363 }
364 
net_cleanup_loop(void)365 static void net_cleanup_loop(void)
366 {
367 	net_clear_handlers();
368 }
369 
net_init(void)370 void net_init(void)
371 {
372 	static int first_call = 1;
373 
374 	if (first_call) {
375 		/*
376 		 *	Setup packet buffers, aligned correctly.
377 		 */
378 		int i;
379 
380 		net_tx_packet = &net_pkt_buf[0] + (PKTALIGN - 1);
381 		net_tx_packet -= (ulong)net_tx_packet % PKTALIGN;
382 		for (i = 0; i < PKTBUFSRX; i++) {
383 			net_rx_packets[i] = net_tx_packet +
384 				(i + 1) * PKTSIZE_ALIGN;
385 		}
386 		arp_init();
387 		net_clear_handlers();
388 
389 		/* Only need to setup buffer pointers once. */
390 		first_call = 0;
391 	}
392 
393 	net_init_loop();
394 }
395 
396 /**********************************************************************/
397 /*
398  *	Main network processing loop.
399  */
400 
net_loop(enum proto_t protocol)401 int net_loop(enum proto_t protocol)
402 {
403 	int ret = -EINVAL;
404 	enum net_loop_state prev_net_state = net_state;
405 
406 	net_restarted = 0;
407 	net_dev_exists = 0;
408 	net_try_count = 1;
409 	debug_cond(DEBUG_INT_STATE, "--- net_loop Entry\n");
410 
411 #ifdef CONFIG_PHY_NCSI
412 	if (phy_interface_is_ncsi() && protocol != NCSI && !ncsi_active()) {
413 		printf("Configuring NCSI\n");
414 		if (net_loop(NCSI) < 0)
415 			return ret;
416 		eth_init_state_only();
417 		goto restart;
418 	}
419 #endif
420 
421 	bootstage_mark_name(BOOTSTAGE_ID_ETH_START, "eth_start");
422 	net_init();
423 	if (eth_is_on_demand_init() || protocol != NETCONS) {
424 		eth_halt();
425 		eth_set_current();
426 		ret = eth_init();
427 		if (ret < 0) {
428 			eth_halt();
429 			return ret;
430 		}
431 	} else {
432 		eth_init_state_only();
433 	}
434 
435 restart:
436 #ifdef CONFIG_USB_KEYBOARD
437 	net_busy_flag = 0;
438 #endif
439 	net_set_state(NETLOOP_CONTINUE);
440 
441 	/*
442 	 *	Start the ball rolling with the given start function.  From
443 	 *	here on, this code is a state machine driven by received
444 	 *	packets and timer events.
445 	 */
446 	debug_cond(DEBUG_INT_STATE, "--- net_loop Init\n");
447 	net_init_loop();
448 
449 	switch (net_check_prereq(protocol)) {
450 	case 1:
451 		/* network not configured */
452 		eth_halt();
453 		net_set_state(prev_net_state);
454 		return -ENODEV;
455 
456 	case 2:
457 		/* network device not configured */
458 		break;
459 
460 	case 0:
461 		net_dev_exists = 1;
462 		net_boot_file_size = 0;
463 		switch (protocol) {
464 		case TFTPGET:
465 #ifdef CONFIG_CMD_TFTPPUT
466 		case TFTPPUT:
467 #endif
468 			/* always use ARP to get server ethernet address */
469 			tftp_start(protocol);
470 			break;
471 #ifdef CONFIG_CMD_TFTPSRV
472 		case TFTPSRV:
473 			tftp_start_server();
474 			break;
475 #endif
476 #ifdef CONFIG_UDP_FUNCTION_FASTBOOT
477 		case FASTBOOT:
478 			fastboot_start_server();
479 			break;
480 #endif
481 #if defined(CONFIG_CMD_DHCP)
482 		case DHCP:
483 			bootp_reset();
484 			net_ip.s_addr = 0;
485 			dhcp_request();		/* Basically same as BOOTP */
486 			break;
487 #endif
488 
489 		case BOOTP:
490 			bootp_reset();
491 			net_ip.s_addr = 0;
492 			bootp_request();
493 			break;
494 
495 #if defined(CONFIG_CMD_RARP)
496 		case RARP:
497 			rarp_try = 0;
498 			net_ip.s_addr = 0;
499 			rarp_request();
500 			break;
501 #endif
502 #if defined(CONFIG_CMD_PING)
503 		case PING:
504 			ping_start();
505 			break;
506 #endif
507 #if defined(CONFIG_CMD_NFS)
508 		case NFS:
509 			nfs_start();
510 			break;
511 #endif
512 #if defined(CONFIG_CMD_CDP)
513 		case CDP:
514 			cdp_start();
515 			break;
516 #endif
517 #if defined(CONFIG_NETCONSOLE) && !defined(CONFIG_SPL_BUILD)
518 		case NETCONS:
519 			nc_start();
520 			break;
521 #endif
522 #if defined(CONFIG_CMD_SNTP)
523 		case SNTP:
524 			sntp_start();
525 			break;
526 #endif
527 #if defined(CONFIG_CMD_DNS)
528 		case DNS:
529 			dns_start();
530 			break;
531 #endif
532 #if defined(CONFIG_CMD_LINK_LOCAL)
533 		case LINKLOCAL:
534 			link_local_start();
535 			break;
536 #endif
537 #if defined(CONFIG_CMD_WOL)
538 		case WOL:
539 			wol_start();
540 			break;
541 #endif
542 #if defined(CONFIG_CMD_NCSI)
543 		case NCSI:
544 			ncsi_probe_packages();
545 			break;
546 #endif
547 		default:
548 			break;
549 		}
550 
551 		break;
552 	}
553 
554 #if defined(CONFIG_MII) || defined(CONFIG_CMD_MII)
555 #if	defined(CONFIG_SYS_FAULT_ECHO_LINK_DOWN)	&& \
556 	defined(CONFIG_LED_STATUS)			&& \
557 	defined(CONFIG_LED_STATUS_RED)
558 	/*
559 	 * Echo the inverted link state to the fault LED.
560 	 */
561 	if (miiphy_link(eth_get_dev()->name, CONFIG_SYS_FAULT_MII_ADDR))
562 		status_led_set(CONFIG_LED_STATUS_RED, CONFIG_LED_STATUS_OFF);
563 	else
564 		status_led_set(CONFIG_LED_STATUS_RED, CONFIG_LED_STATUS_ON);
565 #endif /* CONFIG_SYS_FAULT_ECHO_LINK_DOWN, ... */
566 #endif /* CONFIG_MII, ... */
567 #ifdef CONFIG_USB_KEYBOARD
568 	net_busy_flag = 1;
569 #endif
570 
571 	/*
572 	 *	Main packet reception loop.  Loop receiving packets until
573 	 *	someone sets `net_state' to a state that terminates.
574 	 */
575 	for (;;) {
576 		WATCHDOG_RESET();
577 #ifdef CONFIG_SHOW_ACTIVITY
578 		show_activity(1);
579 #endif
580 		if (arp_timeout_check() > 0)
581 			time_start = get_timer(0);
582 
583 		/*
584 		 *	Check the ethernet for a new packet.  The ethernet
585 		 *	receive routine will process it.
586 		 *	Most drivers return the most recent packet size, but not
587 		 *	errors that may have happened.
588 		 */
589 		eth_rx();
590 
591 		/*
592 		 *	Abort if ctrl-c was pressed.
593 		 */
594 		if (ctrlc()) {
595 			/* cancel any ARP that may not have completed */
596 			net_arp_wait_packet_ip.s_addr = 0;
597 
598 			net_cleanup_loop();
599 			eth_halt();
600 			/* Invalidate the last protocol */
601 			eth_set_last_protocol(BOOTP);
602 
603 			puts("\nAbort\n");
604 			/* include a debug print as well incase the debug
605 			   messages are directed to stderr */
606 			debug_cond(DEBUG_INT_STATE, "--- net_loop Abort!\n");
607 			ret = -EINTR;
608 			goto done;
609 		}
610 
611 		/*
612 		 *	Check for a timeout, and run the timeout handler
613 		 *	if we have one.
614 		 */
615 		if (time_handler &&
616 		    ((get_timer(0) - time_start) > time_delta)) {
617 			thand_f *x;
618 
619 #if defined(CONFIG_MII) || defined(CONFIG_CMD_MII)
620 #if	defined(CONFIG_SYS_FAULT_ECHO_LINK_DOWN)	&& \
621 	defined(CONFIG_LED_STATUS)			&& \
622 	defined(CONFIG_LED_STATUS_RED)
623 			/*
624 			 * Echo the inverted link state to the fault LED.
625 			 */
626 			if (miiphy_link(eth_get_dev()->name,
627 					CONFIG_SYS_FAULT_MII_ADDR))
628 				status_led_set(CONFIG_LED_STATUS_RED,
629 					       CONFIG_LED_STATUS_OFF);
630 			else
631 				status_led_set(CONFIG_LED_STATUS_RED,
632 					       CONFIG_LED_STATUS_ON);
633 #endif /* CONFIG_SYS_FAULT_ECHO_LINK_DOWN, ... */
634 #endif /* CONFIG_MII, ... */
635 			debug_cond(DEBUG_INT_STATE, "--- net_loop timeout\n");
636 			x = time_handler;
637 			time_handler = (thand_f *)0;
638 			(*x)();
639 		}
640 
641 		if (net_state == NETLOOP_FAIL)
642 			ret = net_start_again();
643 
644 		switch (net_state) {
645 		case NETLOOP_RESTART:
646 			net_restarted = 1;
647 			goto restart;
648 
649 		case NETLOOP_SUCCESS:
650 			net_cleanup_loop();
651 			if (net_boot_file_size > 0) {
652 				printf("Bytes transferred = %d (%x hex)\n",
653 				       net_boot_file_size, net_boot_file_size);
654 				env_set_hex("filesize", net_boot_file_size);
655 				env_set_hex("fileaddr", load_addr);
656 			}
657 			if (protocol != NETCONS && protocol != NCSI)
658 				eth_halt();
659 			else
660 				eth_halt_state_only();
661 
662 			eth_set_last_protocol(protocol);
663 
664 			ret = net_boot_file_size;
665 			debug_cond(DEBUG_INT_STATE, "--- net_loop Success!\n");
666 			goto done;
667 
668 		case NETLOOP_FAIL:
669 			net_cleanup_loop();
670 			/* Invalidate the last protocol */
671 			eth_set_last_protocol(BOOTP);
672 			debug_cond(DEBUG_INT_STATE, "--- net_loop Fail!\n");
673 			ret = -ENONET;
674 			goto done;
675 
676 		case NETLOOP_CONTINUE:
677 			continue;
678 		}
679 	}
680 
681 done:
682 #ifdef CONFIG_USB_KEYBOARD
683 	net_busy_flag = 0;
684 #endif
685 #ifdef CONFIG_CMD_TFTPPUT
686 	/* Clear out the handlers */
687 	net_set_udp_handler(NULL);
688 	net_set_icmp_handler(NULL);
689 #endif
690 	net_set_state(prev_net_state);
691 	return ret;
692 }
693 
694 /**********************************************************************/
695 
start_again_timeout_handler(void)696 static void start_again_timeout_handler(void)
697 {
698 	net_set_state(NETLOOP_RESTART);
699 }
700 
net_start_again(void)701 int net_start_again(void)
702 {
703 	char *nretry;
704 	int retry_forever = 0;
705 	unsigned long retrycnt = 0;
706 	int ret;
707 
708 	nretry = env_get("netretry");
709 	if (nretry) {
710 		if (!strcmp(nretry, "yes"))
711 			retry_forever = 1;
712 		else if (!strcmp(nretry, "no"))
713 			retrycnt = 0;
714 		else if (!strcmp(nretry, "once"))
715 			retrycnt = 1;
716 		else
717 			retrycnt = simple_strtoul(nretry, NULL, 0);
718 	} else {
719 		retrycnt = 0;
720 		retry_forever = 0;
721 	}
722 
723 	if ((!retry_forever) && (net_try_count > retrycnt)) {
724 		eth_halt();
725 		net_set_state(NETLOOP_FAIL);
726 		/*
727 		 * We don't provide a way for the protocol to return an error,
728 		 * but this is almost always the reason.
729 		 */
730 		return -ETIMEDOUT;
731 	}
732 
733 	net_try_count++;
734 
735 	eth_halt();
736 #if !defined(CONFIG_NET_DO_NOT_TRY_ANOTHER)
737 	eth_try_another(!net_restarted);
738 #endif
739 	ret = eth_init();
740 	if (net_restart_wrap) {
741 		net_restart_wrap = 0;
742 		if (net_dev_exists) {
743 			net_set_timeout_handler(10000UL,
744 						start_again_timeout_handler);
745 			net_set_udp_handler(NULL);
746 		} else {
747 			net_set_state(NETLOOP_FAIL);
748 		}
749 	} else {
750 		net_set_state(NETLOOP_RESTART);
751 	}
752 	return ret;
753 }
754 
755 /**********************************************************************/
756 /*
757  *	Miscelaneous bits.
758  */
759 
dummy_handler(uchar * pkt,unsigned dport,struct in_addr sip,unsigned sport,unsigned len)760 static void dummy_handler(uchar *pkt, unsigned dport,
761 			struct in_addr sip, unsigned sport,
762 			unsigned len)
763 {
764 }
765 
net_get_udp_handler(void)766 rxhand_f *net_get_udp_handler(void)
767 {
768 	return udp_packet_handler;
769 }
770 
net_set_udp_handler(rxhand_f * f)771 void net_set_udp_handler(rxhand_f *f)
772 {
773 	debug_cond(DEBUG_INT_STATE, "--- net_loop UDP handler set (%p)\n", f);
774 	if (f == NULL)
775 		udp_packet_handler = dummy_handler;
776 	else
777 		udp_packet_handler = f;
778 }
779 
net_get_arp_handler(void)780 rxhand_f *net_get_arp_handler(void)
781 {
782 	return arp_packet_handler;
783 }
784 
net_set_arp_handler(rxhand_f * f)785 void net_set_arp_handler(rxhand_f *f)
786 {
787 	debug_cond(DEBUG_INT_STATE, "--- net_loop ARP handler set (%p)\n", f);
788 	if (f == NULL)
789 		arp_packet_handler = dummy_handler;
790 	else
791 		arp_packet_handler = f;
792 }
793 
794 #ifdef CONFIG_CMD_TFTPPUT
net_set_icmp_handler(rxhand_icmp_f * f)795 void net_set_icmp_handler(rxhand_icmp_f *f)
796 {
797 	packet_icmp_handler = f;
798 }
799 #endif
800 
net_set_timeout_handler(ulong iv,thand_f * f)801 void net_set_timeout_handler(ulong iv, thand_f *f)
802 {
803 	if (iv == 0) {
804 		debug_cond(DEBUG_INT_STATE,
805 			   "--- net_loop timeout handler cancelled\n");
806 		time_handler = (thand_f *)0;
807 	} else {
808 		debug_cond(DEBUG_INT_STATE,
809 			   "--- net_loop timeout handler set (%p)\n", f);
810 		time_handler = f;
811 		time_start = get_timer(0);
812 		time_delta = iv * CONFIG_SYS_HZ / 1000;
813 	}
814 }
815 
net_get_async_tx_pkt_buf(void)816 uchar *net_get_async_tx_pkt_buf(void)
817 {
818 	if (arp_is_waiting())
819 		return arp_tx_packet; /* If we are waiting, we already sent */
820 	else
821 		return net_tx_packet;
822 }
823 
net_send_udp_packet(uchar * ether,struct in_addr dest,int dport,int sport,int payload_len)824 int net_send_udp_packet(uchar *ether, struct in_addr dest, int dport, int sport,
825 		int payload_len)
826 {
827 	return net_send_ip_packet(ether, dest, dport, sport, payload_len,
828 				  IPPROTO_UDP, 0, 0, 0);
829 }
830 
net_send_ip_packet(uchar * ether,struct in_addr dest,int dport,int sport,int payload_len,int proto,u8 action,u32 tcp_seq_num,u32 tcp_ack_num)831 int net_send_ip_packet(uchar *ether, struct in_addr dest, int dport, int sport,
832 		       int payload_len, int proto, u8 action, u32 tcp_seq_num,
833 		       u32 tcp_ack_num)
834 {
835 	uchar *pkt;
836 	int eth_hdr_size;
837 	int pkt_hdr_size;
838 
839 	/* make sure the net_tx_packet is initialized (net_init() was called) */
840 	assert(net_tx_packet != NULL);
841 	if (net_tx_packet == NULL)
842 		return -1;
843 
844 	/* convert to new style broadcast */
845 	if (dest.s_addr == 0)
846 		dest.s_addr = 0xFFFFFFFF;
847 
848 	/* if broadcast, make the ether address a broadcast and don't do ARP */
849 	if (dest.s_addr == 0xFFFFFFFF)
850 		ether = (uchar *)net_bcast_ethaddr;
851 
852 	pkt = (uchar *)net_tx_packet;
853 
854 	eth_hdr_size = net_set_ether(pkt, ether, PROT_IP);
855 
856 	switch (proto) {
857 	case IPPROTO_UDP:
858 		net_set_udp_header(pkt + eth_hdr_size, dest, dport, sport,
859 				   payload_len);
860 		pkt_hdr_size = eth_hdr_size + IP_UDP_HDR_SIZE;
861 		break;
862 	default:
863 		return -EINVAL;
864 	}
865 
866 	/* if MAC address was not discovered yet, do an ARP request */
867 	if (memcmp(ether, net_null_ethaddr, 6) == 0) {
868 		debug_cond(DEBUG_DEV_PKT, "sending ARP for %pI4\n", &dest);
869 
870 		/* save the ip and eth addr for the packet to send after arp */
871 		net_arp_wait_packet_ip = dest;
872 		arp_wait_packet_ethaddr = ether;
873 
874 		/* size of the waiting packet */
875 		arp_wait_tx_packet_size = pkt_hdr_size + payload_len;
876 
877 		/* and do the ARP request */
878 		arp_wait_try = 1;
879 		arp_wait_timer_start = get_timer(0);
880 		arp_request();
881 		return 1;	/* waiting */
882 	} else {
883 		debug_cond(DEBUG_DEV_PKT, "sending UDP to %pI4/%pM\n",
884 			   &dest, ether);
885 		net_send_packet(net_tx_packet, pkt_hdr_size + payload_len);
886 		return 0;	/* transmitted */
887 	}
888 }
889 
890 #ifdef CONFIG_IP_DEFRAG
891 /*
892  * This function collects fragments in a single packet, according
893  * to the algorithm in RFC815. It returns NULL or the pointer to
894  * a complete packet, in static storage
895  */
896 #ifndef CONFIG_NET_MAXDEFRAG
897 #define CONFIG_NET_MAXDEFRAG 16384
898 #endif
899 #define IP_PKTSIZE (CONFIG_NET_MAXDEFRAG)
900 
901 #define IP_MAXUDP (IP_PKTSIZE - IP_HDR_SIZE)
902 
903 /*
904  * this is the packet being assembled, either data or frag control.
905  * Fragments go by 8 bytes, so this union must be 8 bytes long
906  */
907 struct hole {
908 	/* first_byte is address of this structure */
909 	u16 last_byte;	/* last byte in this hole + 1 (begin of next hole) */
910 	u16 next_hole;	/* index of next (in 8-b blocks), 0 == none */
911 	u16 prev_hole;	/* index of prev, 0 == none */
912 	u16 unused;
913 };
914 
__net_defragment(struct ip_udp_hdr * ip,int * lenp)915 static struct ip_udp_hdr *__net_defragment(struct ip_udp_hdr *ip, int *lenp)
916 {
917 	static uchar pkt_buff[IP_PKTSIZE] __aligned(PKTALIGN);
918 	static u16 first_hole, total_len;
919 	struct hole *payload, *thisfrag, *h, *newh;
920 	struct ip_udp_hdr *localip = (struct ip_udp_hdr *)pkt_buff;
921 	uchar *indata = (uchar *)ip;
922 	int offset8, start, len, done = 0;
923 	u16 ip_off = ntohs(ip->ip_off);
924 
925 	/* payload starts after IP header, this fragment is in there */
926 	payload = (struct hole *)(pkt_buff + IP_HDR_SIZE);
927 	offset8 =  (ip_off & IP_OFFS);
928 	thisfrag = payload + offset8;
929 	start = offset8 * 8;
930 	len = ntohs(ip->ip_len) - IP_HDR_SIZE;
931 
932 	if (start + len > IP_MAXUDP) /* fragment extends too far */
933 		return NULL;
934 
935 	if (!total_len || localip->ip_id != ip->ip_id) {
936 		/* new (or different) packet, reset structs */
937 		total_len = 0xffff;
938 		payload[0].last_byte = ~0;
939 		payload[0].next_hole = 0;
940 		payload[0].prev_hole = 0;
941 		first_hole = 0;
942 		/* any IP header will work, copy the first we received */
943 		memcpy(localip, ip, IP_HDR_SIZE);
944 	}
945 
946 	/*
947 	 * What follows is the reassembly algorithm. We use the payload
948 	 * array as a linked list of hole descriptors, as each hole starts
949 	 * at a multiple of 8 bytes. However, last byte can be whatever value,
950 	 * so it is represented as byte count, not as 8-byte blocks.
951 	 */
952 
953 	h = payload + first_hole;
954 	while (h->last_byte < start) {
955 		if (!h->next_hole) {
956 			/* no hole that far away */
957 			return NULL;
958 		}
959 		h = payload + h->next_hole;
960 	}
961 
962 	/* last fragment may be 1..7 bytes, the "+7" forces acceptance */
963 	if (offset8 + ((len + 7) / 8) <= h - payload) {
964 		/* no overlap with holes (dup fragment?) */
965 		return NULL;
966 	}
967 
968 	if (!(ip_off & IP_FLAGS_MFRAG)) {
969 		/* no more fragmentss: truncate this (last) hole */
970 		total_len = start + len;
971 		h->last_byte = start + len;
972 	}
973 
974 	/*
975 	 * There is some overlap: fix the hole list. This code doesn't
976 	 * deal with a fragment that overlaps with two different holes
977 	 * (thus being a superset of a previously-received fragment).
978 	 */
979 
980 	if ((h >= thisfrag) && (h->last_byte <= start + len)) {
981 		/* complete overlap with hole: remove hole */
982 		if (!h->prev_hole && !h->next_hole) {
983 			/* last remaining hole */
984 			done = 1;
985 		} else if (!h->prev_hole) {
986 			/* first hole */
987 			first_hole = h->next_hole;
988 			payload[h->next_hole].prev_hole = 0;
989 		} else if (!h->next_hole) {
990 			/* last hole */
991 			payload[h->prev_hole].next_hole = 0;
992 		} else {
993 			/* in the middle of the list */
994 			payload[h->next_hole].prev_hole = h->prev_hole;
995 			payload[h->prev_hole].next_hole = h->next_hole;
996 		}
997 
998 	} else if (h->last_byte <= start + len) {
999 		/* overlaps with final part of the hole: shorten this hole */
1000 		h->last_byte = start;
1001 
1002 	} else if (h >= thisfrag) {
1003 		/* overlaps with initial part of the hole: move this hole */
1004 		newh = thisfrag + (len / 8);
1005 		*newh = *h;
1006 		h = newh;
1007 		if (h->next_hole)
1008 			payload[h->next_hole].prev_hole = (h - payload);
1009 		if (h->prev_hole)
1010 			payload[h->prev_hole].next_hole = (h - payload);
1011 		else
1012 			first_hole = (h - payload);
1013 
1014 	} else {
1015 		/* fragment sits in the middle: split the hole */
1016 		newh = thisfrag + (len / 8);
1017 		*newh = *h;
1018 		h->last_byte = start;
1019 		h->next_hole = (newh - payload);
1020 		newh->prev_hole = (h - payload);
1021 		if (newh->next_hole)
1022 			payload[newh->next_hole].prev_hole = (newh - payload);
1023 	}
1024 
1025 	/* finally copy this fragment and possibly return whole packet */
1026 	memcpy((uchar *)thisfrag, indata + IP_HDR_SIZE, len);
1027 	if (!done)
1028 		return NULL;
1029 
1030 	localip->ip_len = htons(total_len);
1031 	*lenp = total_len + IP_HDR_SIZE;
1032 	return localip;
1033 }
1034 
net_defragment(struct ip_udp_hdr * ip,int * lenp)1035 static inline struct ip_udp_hdr *net_defragment(struct ip_udp_hdr *ip,
1036 	int *lenp)
1037 {
1038 	u16 ip_off = ntohs(ip->ip_off);
1039 	if (!(ip_off & (IP_OFFS | IP_FLAGS_MFRAG)))
1040 		return ip; /* not a fragment */
1041 	return __net_defragment(ip, lenp);
1042 }
1043 
1044 #else /* !CONFIG_IP_DEFRAG */
1045 
net_defragment(struct ip_udp_hdr * ip,int * lenp)1046 static inline struct ip_udp_hdr *net_defragment(struct ip_udp_hdr *ip,
1047 	int *lenp)
1048 {
1049 	u16 ip_off = ntohs(ip->ip_off);
1050 	if (!(ip_off & (IP_OFFS | IP_FLAGS_MFRAG)))
1051 		return ip; /* not a fragment */
1052 	return NULL;
1053 }
1054 #endif
1055 
1056 /**
1057  * Receive an ICMP packet. We deal with REDIRECT and PING here, and silently
1058  * drop others.
1059  *
1060  * @parma ip	IP packet containing the ICMP
1061  */
receive_icmp(struct ip_udp_hdr * ip,int len,struct in_addr src_ip,struct ethernet_hdr * et)1062 static void receive_icmp(struct ip_udp_hdr *ip, int len,
1063 			struct in_addr src_ip, struct ethernet_hdr *et)
1064 {
1065 	struct icmp_hdr *icmph = (struct icmp_hdr *)&ip->udp_src;
1066 
1067 	switch (icmph->type) {
1068 	case ICMP_REDIRECT:
1069 		if (icmph->code != ICMP_REDIR_HOST)
1070 			return;
1071 		printf(" ICMP Host Redirect to %pI4 ",
1072 		       &icmph->un.gateway);
1073 		break;
1074 	default:
1075 #if defined(CONFIG_CMD_PING)
1076 		ping_receive(et, ip, len);
1077 #endif
1078 #ifdef CONFIG_CMD_TFTPPUT
1079 		if (packet_icmp_handler)
1080 			packet_icmp_handler(icmph->type, icmph->code,
1081 					    ntohs(ip->udp_dst), src_ip,
1082 					    ntohs(ip->udp_src), icmph->un.data,
1083 					    ntohs(ip->udp_len));
1084 #endif
1085 		break;
1086 	}
1087 }
1088 
net_process_received_packet(uchar * in_packet,int len)1089 void net_process_received_packet(uchar *in_packet, int len)
1090 {
1091 	struct ethernet_hdr *et;
1092 	struct ip_udp_hdr *ip;
1093 	struct in_addr dst_ip;
1094 	struct in_addr src_ip;
1095 	int eth_proto;
1096 #if defined(CONFIG_CMD_CDP)
1097 	int iscdp;
1098 #endif
1099 	ushort cti = 0, vlanid = VLAN_NONE, myvlanid, mynvlanid;
1100 
1101 	debug_cond(DEBUG_NET_PKT, "packet received\n");
1102 
1103 	net_rx_packet = in_packet;
1104 	net_rx_packet_len = len;
1105 	et = (struct ethernet_hdr *)in_packet;
1106 
1107 	/* too small packet? */
1108 	if (len < ETHER_HDR_SIZE)
1109 		return;
1110 
1111 #if defined(CONFIG_API) || defined(CONFIG_EFI_LOADER)
1112 	if (push_packet) {
1113 		(*push_packet)(in_packet, len);
1114 		return;
1115 	}
1116 #endif
1117 
1118 #if defined(CONFIG_CMD_CDP)
1119 	/* keep track if packet is CDP */
1120 	iscdp = is_cdp_packet(et->et_dest);
1121 #endif
1122 
1123 	myvlanid = ntohs(net_our_vlan);
1124 	if (myvlanid == (ushort)-1)
1125 		myvlanid = VLAN_NONE;
1126 	mynvlanid = ntohs(net_native_vlan);
1127 	if (mynvlanid == (ushort)-1)
1128 		mynvlanid = VLAN_NONE;
1129 
1130 	eth_proto = ntohs(et->et_protlen);
1131 
1132 	if (eth_proto < 1514) {
1133 		struct e802_hdr *et802 = (struct e802_hdr *)et;
1134 		/*
1135 		 *	Got a 802.2 packet.  Check the other protocol field.
1136 		 *	XXX VLAN over 802.2+SNAP not implemented!
1137 		 */
1138 		eth_proto = ntohs(et802->et_prot);
1139 
1140 		ip = (struct ip_udp_hdr *)(in_packet + E802_HDR_SIZE);
1141 		len -= E802_HDR_SIZE;
1142 
1143 	} else if (eth_proto != PROT_VLAN) {	/* normal packet */
1144 		ip = (struct ip_udp_hdr *)(in_packet + ETHER_HDR_SIZE);
1145 		len -= ETHER_HDR_SIZE;
1146 
1147 	} else {			/* VLAN packet */
1148 		struct vlan_ethernet_hdr *vet =
1149 			(struct vlan_ethernet_hdr *)et;
1150 
1151 		debug_cond(DEBUG_NET_PKT, "VLAN packet received\n");
1152 
1153 		/* too small packet? */
1154 		if (len < VLAN_ETHER_HDR_SIZE)
1155 			return;
1156 
1157 		/* if no VLAN active */
1158 		if ((ntohs(net_our_vlan) & VLAN_IDMASK) == VLAN_NONE
1159 #if defined(CONFIG_CMD_CDP)
1160 				&& iscdp == 0
1161 #endif
1162 				)
1163 			return;
1164 
1165 		cti = ntohs(vet->vet_tag);
1166 		vlanid = cti & VLAN_IDMASK;
1167 		eth_proto = ntohs(vet->vet_type);
1168 
1169 		ip = (struct ip_udp_hdr *)(in_packet + VLAN_ETHER_HDR_SIZE);
1170 		len -= VLAN_ETHER_HDR_SIZE;
1171 	}
1172 
1173 	debug_cond(DEBUG_NET_PKT, "Receive from protocol 0x%x\n", eth_proto);
1174 
1175 #if defined(CONFIG_CMD_CDP)
1176 	if (iscdp) {
1177 		cdp_receive((uchar *)ip, len);
1178 		return;
1179 	}
1180 #endif
1181 
1182 	if ((myvlanid & VLAN_IDMASK) != VLAN_NONE) {
1183 		if (vlanid == VLAN_NONE)
1184 			vlanid = (mynvlanid & VLAN_IDMASK);
1185 		/* not matched? */
1186 		if (vlanid != (myvlanid & VLAN_IDMASK))
1187 			return;
1188 	}
1189 
1190 	switch (eth_proto) {
1191 	case PROT_ARP:
1192 		arp_receive(et, ip, len);
1193 		break;
1194 
1195 #ifdef CONFIG_CMD_RARP
1196 	case PROT_RARP:
1197 		rarp_receive(ip, len);
1198 		break;
1199 #endif
1200 	case PROT_IP:
1201 		debug_cond(DEBUG_NET_PKT, "Got IP\n");
1202 		/* Before we start poking the header, make sure it is there */
1203 		if (len < IP_UDP_HDR_SIZE) {
1204 			debug("len bad %d < %lu\n", len,
1205 			      (ulong)IP_UDP_HDR_SIZE);
1206 			return;
1207 		}
1208 		/* Check the packet length */
1209 		if (len < ntohs(ip->ip_len)) {
1210 			debug("len bad %d < %d\n", len, ntohs(ip->ip_len));
1211 			return;
1212 		}
1213 		len = ntohs(ip->ip_len);
1214 		debug_cond(DEBUG_NET_PKT, "len=%d, v=%02x\n",
1215 			   len, ip->ip_hl_v & 0xff);
1216 
1217 		/* Can't deal with anything except IPv4 */
1218 		if ((ip->ip_hl_v & 0xf0) != 0x40)
1219 			return;
1220 		/* Can't deal with IP options (headers != 20 bytes) */
1221 		if ((ip->ip_hl_v & 0x0f) > 0x05)
1222 			return;
1223 		/* Check the Checksum of the header */
1224 		if (!ip_checksum_ok((uchar *)ip, IP_HDR_SIZE)) {
1225 			debug("checksum bad\n");
1226 			return;
1227 		}
1228 		/* If it is not for us, ignore it */
1229 		dst_ip = net_read_ip(&ip->ip_dst);
1230 		if (net_ip.s_addr && dst_ip.s_addr != net_ip.s_addr &&
1231 		    dst_ip.s_addr != 0xFFFFFFFF) {
1232 				return;
1233 		}
1234 		/* Read source IP address for later use */
1235 		src_ip = net_read_ip(&ip->ip_src);
1236 		/*
1237 		 * The function returns the unchanged packet if it's not
1238 		 * a fragment, and either the complete packet or NULL if
1239 		 * it is a fragment (if !CONFIG_IP_DEFRAG, it returns NULL)
1240 		 */
1241 		ip = net_defragment(ip, &len);
1242 		if (!ip)
1243 			return;
1244 		/*
1245 		 * watch for ICMP host redirects
1246 		 *
1247 		 * There is no real handler code (yet). We just watch
1248 		 * for ICMP host redirect messages. In case anybody
1249 		 * sees these messages: please contact me
1250 		 * (wd@denx.de), or - even better - send me the
1251 		 * necessary fixes :-)
1252 		 *
1253 		 * Note: in all cases where I have seen this so far
1254 		 * it was a problem with the router configuration,
1255 		 * for instance when a router was configured in the
1256 		 * BOOTP reply, but the TFTP server was on the same
1257 		 * subnet. So this is probably a warning that your
1258 		 * configuration might be wrong. But I'm not really
1259 		 * sure if there aren't any other situations.
1260 		 *
1261 		 * Simon Glass <sjg@chromium.org>: We get an ICMP when
1262 		 * we send a tftp packet to a dead connection, or when
1263 		 * there is no server at the other end.
1264 		 */
1265 		if (ip->ip_p == IPPROTO_ICMP) {
1266 			receive_icmp(ip, len, src_ip, et);
1267 			return;
1268 		} else if (ip->ip_p != IPPROTO_UDP) {	/* Only UDP packets */
1269 			return;
1270 		}
1271 
1272 		if (ntohs(ip->udp_len) < UDP_HDR_SIZE || ntohs(ip->udp_len) > ntohs(ip->ip_len))
1273 			return;
1274 
1275 		debug_cond(DEBUG_DEV_PKT,
1276 			   "received UDP (to=%pI4, from=%pI4, len=%d)\n",
1277 			   &dst_ip, &src_ip, len);
1278 
1279 #ifdef CONFIG_UDP_CHECKSUM
1280 		if (ip->udp_xsum != 0) {
1281 			ulong   xsum;
1282 			ushort *sumptr;
1283 			ushort  sumlen;
1284 
1285 			xsum  = ip->ip_p;
1286 			xsum += (ntohs(ip->udp_len));
1287 			xsum += (ntohl(ip->ip_src.s_addr) >> 16) & 0x0000ffff;
1288 			xsum += (ntohl(ip->ip_src.s_addr) >>  0) & 0x0000ffff;
1289 			xsum += (ntohl(ip->ip_dst.s_addr) >> 16) & 0x0000ffff;
1290 			xsum += (ntohl(ip->ip_dst.s_addr) >>  0) & 0x0000ffff;
1291 
1292 			sumlen = ntohs(ip->udp_len);
1293 			sumptr = (ushort *)&(ip->udp_src);
1294 
1295 			while (sumlen > 1) {
1296 				ushort sumdata;
1297 
1298 				sumdata = *sumptr++;
1299 				xsum += ntohs(sumdata);
1300 				sumlen -= 2;
1301 			}
1302 			if (sumlen > 0) {
1303 				ushort sumdata;
1304 
1305 				sumdata = *(unsigned char *)sumptr;
1306 				sumdata = (sumdata << 8) & 0xff00;
1307 				xsum += sumdata;
1308 			}
1309 			while ((xsum >> 16) != 0) {
1310 				xsum = (xsum & 0x0000ffff) +
1311 				       ((xsum >> 16) & 0x0000ffff);
1312 			}
1313 			if ((xsum != 0x00000000) && (xsum != 0x0000ffff)) {
1314 				printf(" UDP wrong checksum %08lx %08x\n",
1315 				       xsum, ntohs(ip->udp_xsum));
1316 				return;
1317 			}
1318 		}
1319 #endif
1320 
1321 #if defined(CONFIG_NETCONSOLE) && !defined(CONFIG_SPL_BUILD)
1322 		nc_input_packet((uchar *)ip + IP_UDP_HDR_SIZE,
1323 				src_ip,
1324 				ntohs(ip->udp_dst),
1325 				ntohs(ip->udp_src),
1326 				ntohs(ip->udp_len) - UDP_HDR_SIZE);
1327 #endif
1328 		/*
1329 		 * IP header OK.  Pass the packet to the current handler.
1330 		 */
1331 		(*udp_packet_handler)((uchar *)ip + IP_UDP_HDR_SIZE,
1332 				      ntohs(ip->udp_dst),
1333 				      src_ip,
1334 				      ntohs(ip->udp_src),
1335 				      ntohs(ip->udp_len) - UDP_HDR_SIZE);
1336 		break;
1337 #ifdef CONFIG_CMD_WOL
1338 	case PROT_WOL:
1339 		wol_receive(ip, len);
1340 		break;
1341 #endif
1342 #ifdef CONFIG_PHY_NCSI
1343 	case PROT_NCSI:
1344 		ncsi_receive(et, ip, len);
1345 		break;
1346 #endif
1347 	}
1348 }
1349 
1350 /**********************************************************************/
1351 
net_check_prereq(enum proto_t protocol)1352 static int net_check_prereq(enum proto_t protocol)
1353 {
1354 	switch (protocol) {
1355 		/* Fall through */
1356 #if defined(CONFIG_CMD_PING)
1357 	case PING:
1358 		if (net_ping_ip.s_addr == 0) {
1359 			puts("*** ERROR: ping address not given\n");
1360 			return 1;
1361 		}
1362 		goto common;
1363 #endif
1364 #if defined(CONFIG_CMD_SNTP)
1365 	case SNTP:
1366 		if (net_ntp_server.s_addr == 0) {
1367 			puts("*** ERROR: NTP server address not given\n");
1368 			return 1;
1369 		}
1370 		goto common;
1371 #endif
1372 #if defined(CONFIG_CMD_DNS)
1373 	case DNS:
1374 		if (net_dns_server.s_addr == 0) {
1375 			puts("*** ERROR: DNS server address not given\n");
1376 			return 1;
1377 		}
1378 		goto common;
1379 #endif
1380 #if defined(CONFIG_CMD_NFS)
1381 	case NFS:
1382 #endif
1383 		/* Fall through */
1384 	case TFTPGET:
1385 	case TFTPPUT:
1386 		if (net_server_ip.s_addr == 0 && !is_serverip_in_cmd()) {
1387 			puts("*** ERROR: `serverip' not set\n");
1388 			return 1;
1389 		}
1390 #if	defined(CONFIG_CMD_PING) || defined(CONFIG_CMD_SNTP) || \
1391 	defined(CONFIG_CMD_DNS)
1392 common:
1393 #endif
1394 		/* Fall through */
1395 
1396 	case NETCONS:
1397 	case FASTBOOT:
1398 	case TFTPSRV:
1399 		if (net_ip.s_addr == 0) {
1400 			puts("*** ERROR: `ipaddr' not set\n");
1401 			return 1;
1402 		}
1403 		/* Fall through */
1404 
1405 #ifdef CONFIG_CMD_RARP
1406 	case RARP:
1407 #endif
1408 #ifdef CONFIG_CMD_NCSI
1409 	case NCSI:
1410 #endif
1411 	case BOOTP:
1412 	case CDP:
1413 	case DHCP:
1414 	case LINKLOCAL:
1415 		if (memcmp(net_ethaddr, "\0\0\0\0\0\0", 6) == 0) {
1416 			int num = eth_get_dev_index();
1417 
1418 			switch (num) {
1419 			case -1:
1420 				puts("*** ERROR: No ethernet found.\n");
1421 				return 1;
1422 			case 0:
1423 				puts("*** ERROR: `ethaddr' not set\n");
1424 				break;
1425 			default:
1426 				printf("*** ERROR: `eth%daddr' not set\n",
1427 				       num);
1428 				break;
1429 			}
1430 
1431 			net_start_again();
1432 			return 2;
1433 		}
1434 		/* Fall through */
1435 	default:
1436 		return 0;
1437 	}
1438 	return 0;		/* OK */
1439 }
1440 /**********************************************************************/
1441 
1442 int
net_eth_hdr_size(void)1443 net_eth_hdr_size(void)
1444 {
1445 	ushort myvlanid;
1446 
1447 	myvlanid = ntohs(net_our_vlan);
1448 	if (myvlanid == (ushort)-1)
1449 		myvlanid = VLAN_NONE;
1450 
1451 	return ((myvlanid & VLAN_IDMASK) == VLAN_NONE) ? ETHER_HDR_SIZE :
1452 		VLAN_ETHER_HDR_SIZE;
1453 }
1454 
net_set_ether(uchar * xet,const uchar * dest_ethaddr,uint prot)1455 int net_set_ether(uchar *xet, const uchar *dest_ethaddr, uint prot)
1456 {
1457 	struct ethernet_hdr *et = (struct ethernet_hdr *)xet;
1458 	ushort myvlanid;
1459 
1460 	myvlanid = ntohs(net_our_vlan);
1461 	if (myvlanid == (ushort)-1)
1462 		myvlanid = VLAN_NONE;
1463 
1464 	memcpy(et->et_dest, dest_ethaddr, 6);
1465 	memcpy(et->et_src, net_ethaddr, 6);
1466 	if ((myvlanid & VLAN_IDMASK) == VLAN_NONE) {
1467 		et->et_protlen = htons(prot);
1468 		return ETHER_HDR_SIZE;
1469 	} else {
1470 		struct vlan_ethernet_hdr *vet =
1471 			(struct vlan_ethernet_hdr *)xet;
1472 
1473 		vet->vet_vlan_type = htons(PROT_VLAN);
1474 		vet->vet_tag = htons((0 << 5) | (myvlanid & VLAN_IDMASK));
1475 		vet->vet_type = htons(prot);
1476 		return VLAN_ETHER_HDR_SIZE;
1477 	}
1478 }
1479 
net_update_ether(struct ethernet_hdr * et,uchar * addr,uint prot)1480 int net_update_ether(struct ethernet_hdr *et, uchar *addr, uint prot)
1481 {
1482 	ushort protlen;
1483 
1484 	memcpy(et->et_dest, addr, 6);
1485 	memcpy(et->et_src, net_ethaddr, 6);
1486 	protlen = ntohs(et->et_protlen);
1487 	if (protlen == PROT_VLAN) {
1488 		struct vlan_ethernet_hdr *vet =
1489 			(struct vlan_ethernet_hdr *)et;
1490 		vet->vet_type = htons(prot);
1491 		return VLAN_ETHER_HDR_SIZE;
1492 	} else if (protlen > 1514) {
1493 		et->et_protlen = htons(prot);
1494 		return ETHER_HDR_SIZE;
1495 	} else {
1496 		/* 802.2 + SNAP */
1497 		struct e802_hdr *et802 = (struct e802_hdr *)et;
1498 		et802->et_prot = htons(prot);
1499 		return E802_HDR_SIZE;
1500 	}
1501 }
1502 
net_set_ip_header(uchar * pkt,struct in_addr dest,struct in_addr source,u16 pkt_len,u8 proto)1503 void net_set_ip_header(uchar *pkt, struct in_addr dest, struct in_addr source,
1504 		       u16 pkt_len, u8 proto)
1505 {
1506 	struct ip_udp_hdr *ip = (struct ip_udp_hdr *)pkt;
1507 
1508 	/*
1509 	 *	Construct an IP header.
1510 	 */
1511 	/* IP_HDR_SIZE / 4 (not including UDP) */
1512 	ip->ip_hl_v  = 0x45;
1513 	ip->ip_tos   = 0;
1514 	ip->ip_len   = htons(pkt_len);
1515 	ip->ip_p     = proto;
1516 	ip->ip_id    = htons(net_ip_id++);
1517 	ip->ip_off   = htons(IP_FLAGS_DFRAG);	/* Don't fragment */
1518 	ip->ip_ttl   = 255;
1519 	ip->ip_sum   = 0;
1520 	/* already in network byte order */
1521 	net_copy_ip((void *)&ip->ip_src, &source);
1522 	/* already in network byte order */
1523 	net_copy_ip((void *)&ip->ip_dst, &dest);
1524 
1525 	ip->ip_sum   = compute_ip_checksum(ip, IP_HDR_SIZE);
1526 }
1527 
net_set_udp_header(uchar * pkt,struct in_addr dest,int dport,int sport,int len)1528 void net_set_udp_header(uchar *pkt, struct in_addr dest, int dport, int sport,
1529 			int len)
1530 {
1531 	struct ip_udp_hdr *ip = (struct ip_udp_hdr *)pkt;
1532 
1533 	/*
1534 	 *	If the data is an odd number of bytes, zero the
1535 	 *	byte after the last byte so that the checksum
1536 	 *	will work.
1537 	 */
1538 	if (len & 1)
1539 		pkt[IP_UDP_HDR_SIZE + len] = 0;
1540 
1541 	net_set_ip_header(pkt, dest, net_ip, IP_UDP_HDR_SIZE + len,
1542 			  IPPROTO_UDP);
1543 
1544 	ip->udp_src  = htons(sport);
1545 	ip->udp_dst  = htons(dport);
1546 	ip->udp_len  = htons(UDP_HDR_SIZE + len);
1547 	ip->udp_xsum = 0;
1548 }
1549 
copy_filename(char * dst,const char * src,int size)1550 void copy_filename(char *dst, const char *src, int size)
1551 {
1552 	if (src && *src && (*src == '"')) {
1553 		++src;
1554 		--size;
1555 	}
1556 
1557 	while ((--size > 0) && src && *src && (*src != '"'))
1558 		*dst++ = *src++;
1559 	*dst = '\0';
1560 }
1561 
is_serverip_in_cmd(void)1562 int is_serverip_in_cmd(void)
1563 {
1564 	return !!strchr(net_boot_file_name, ':');
1565 }
1566 
net_parse_bootfile(struct in_addr * ipaddr,char * filename,int max_len)1567 int net_parse_bootfile(struct in_addr *ipaddr, char *filename, int max_len)
1568 {
1569 	char *colon;
1570 
1571 	if (net_boot_file_name[0] == '\0')
1572 		return 0;
1573 
1574 	colon = strchr(net_boot_file_name, ':');
1575 	if (colon) {
1576 		if (ipaddr)
1577 			*ipaddr = string_to_ip(net_boot_file_name);
1578 		strncpy(filename, colon + 1, max_len);
1579 	} else {
1580 		strncpy(filename, net_boot_file_name, max_len);
1581 	}
1582 	filename[max_len - 1] = '\0';
1583 
1584 	return 1;
1585 }
1586 
1587 #if	defined(CONFIG_CMD_NFS)		|| \
1588 	defined(CONFIG_CMD_SNTP)	|| \
1589 	defined(CONFIG_CMD_DNS)
1590 /*
1591  * make port a little random (1024-17407)
1592  * This keeps the math somewhat trivial to compute, and seems to work with
1593  * all supported protocols/clients/servers
1594  */
random_port(void)1595 unsigned int random_port(void)
1596 {
1597 	return 1024 + (get_timer(0) % 0x4000);
1598 }
1599 #endif
1600 
ip_to_string(struct in_addr x,char * s)1601 void ip_to_string(struct in_addr x, char *s)
1602 {
1603 	x.s_addr = ntohl(x.s_addr);
1604 	sprintf(s, "%d.%d.%d.%d",
1605 		(int) ((x.s_addr >> 24) & 0xff),
1606 		(int) ((x.s_addr >> 16) & 0xff),
1607 		(int) ((x.s_addr >> 8) & 0xff),
1608 		(int) ((x.s_addr >> 0) & 0xff)
1609 	);
1610 }
1611 
vlan_to_string(ushort x,char * s)1612 void vlan_to_string(ushort x, char *s)
1613 {
1614 	x = ntohs(x);
1615 
1616 	if (x == (ushort)-1)
1617 		x = VLAN_NONE;
1618 
1619 	if (x == VLAN_NONE)
1620 		strcpy(s, "none");
1621 	else
1622 		sprintf(s, "%d", x & VLAN_IDMASK);
1623 }
1624 
string_to_vlan(const char * s)1625 ushort string_to_vlan(const char *s)
1626 {
1627 	ushort id;
1628 
1629 	if (s == NULL)
1630 		return htons(VLAN_NONE);
1631 
1632 	if (*s < '0' || *s > '9')
1633 		id = VLAN_NONE;
1634 	else
1635 		id = (ushort)simple_strtoul(s, NULL, 10);
1636 
1637 	return htons(id);
1638 }
1639 
env_get_vlan(char * var)1640 ushort env_get_vlan(char *var)
1641 {
1642 	return string_to_vlan(env_get(var));
1643 }
1644