Prepare v2025.01
[u-boot.git] / net / net.c
blobf47e9fbe33a7c61cf5d877cb6b1ae42bb110df1b
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * Copied from Linux Monitor (LiMon) - Networking.
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
13 * General Desription:
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:
19 * BOOTP:
21 * Prerequisites: - own ethernet address
22 * We want: - own IP address
23 * - TFTP server IP address
24 * - name of bootfile
25 * Next step: ARP
27 * LINKLOCAL:
29 * Prerequisites: - own ethernet address
30 * We want: - own IP address
31 * Next step: ARP
33 * RARP:
35 * Prerequisites: - own ethernet address
36 * We want: - own IP address
37 * - TFTP server IP address
38 * Next step: ARP
40 * ARP:
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
48 * DHCP:
50 * Prerequisites: - own ethernet address
51 * We want: - IP, Netmask, ServerIP, Gateway IP
52 * - bootfilename, lease time
53 * Next step: - TFTP
55 * TFTP:
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
66 * NFS:
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
76 * WOL:
78 * Prerequisites: - own ethernet address
79 * We want: - magic packet or timeout
80 * Next step: none
83 #include <bootstage.h>
84 #include <command.h>
85 #include <console.h>
86 #include <env.h>
87 #include <env_internal.h>
88 #include <errno.h>
89 #include <image.h>
90 #include <led.h>
91 #include <log.h>
92 #include <net.h>
93 #include <net6.h>
94 #include <ndisc.h>
95 #include <net/fastboot_udp.h>
96 #include <net/fastboot_tcp.h>
97 #include <net/tftp.h>
98 #include <net/ncsi.h>
99 #if defined(CONFIG_CMD_PCAP)
100 #include <net/pcap.h>
101 #endif
102 #include <net/udp.h>
103 #if defined(CONFIG_LED_STATUS)
104 #include <miiphy.h>
105 #include <status_led.h>
106 #endif
107 #include <watchdog.h>
108 #include <linux/compiler.h>
109 #include <test/test.h>
110 #include <net/tcp.h>
111 #include <net/wget.h>
112 #include "arp.h"
113 #include "bootp.h"
114 #include "cdp.h"
115 #if defined(CONFIG_CMD_DNS)
116 #include "dns.h"
117 #endif
118 #include "link_local.h"
119 #include "nfs.h"
120 #include "ping.h"
121 #include "rarp.h"
122 #if defined(CONFIG_CMD_WOL)
123 #include "wol.h"
124 #endif
125 #include "dhcpv6.h"
126 #include "net_rand.h"
128 /** BOOTP EXTENTIONS **/
130 /* Our subnet mask (0=unknown) */
131 struct in_addr net_netmask;
132 /* Our gateways IP address */
133 struct in_addr net_gateway;
134 /* Our DNS IP address */
135 struct in_addr net_dns_server;
136 #if defined(CONFIG_BOOTP_DNS2)
137 /* Our 2nd DNS IP address */
138 struct in_addr net_dns_server2;
139 #endif
140 /* Indicates whether the pxe path prefix / config file was specified in dhcp option */
141 char *pxelinux_configfile;
143 /** END OF BOOTP EXTENTIONS **/
145 /* Our ethernet address */
146 u8 net_ethaddr[6];
147 /* Boot server enet address */
148 u8 net_server_ethaddr[6];
149 /* Our IP addr (0 = unknown) */
150 struct in_addr net_ip;
151 /* Server IP addr (0 = unknown) */
152 struct in_addr net_server_ip;
153 /* Current receive packet */
154 uchar *net_rx_packet;
155 /* Current rx packet length */
156 int net_rx_packet_len;
157 /* IP packet ID */
158 static unsigned net_ip_id;
159 /* Ethernet bcast address */
160 const u8 net_bcast_ethaddr[6] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
161 const u8 net_null_ethaddr[6];
162 #if defined(CONFIG_API) || defined(CONFIG_EFI_LOADER)
163 void (*push_packet)(void *, int len) = 0;
164 #endif
165 /* Network loop state */
166 enum net_loop_state net_state;
167 /* Tried all network devices */
168 int net_restart_wrap;
169 /* Network loop restarted */
170 static int net_restarted;
171 /* At least one device configured */
172 static int net_dev_exists;
174 /* XXX in both little & big endian machines 0xFFFF == ntohs(-1) */
175 /* default is without VLAN */
176 ushort net_our_vlan = 0xFFFF;
177 /* ditto */
178 ushort net_native_vlan = 0xFFFF;
180 /* Boot File name */
181 char net_boot_file_name[1024];
182 /* Indicates whether the file name was specified on the command line */
183 bool net_boot_file_name_explicit;
184 /* The actual transferred size of the bootfile (in bytes) */
185 u32 net_boot_file_size;
186 /* Boot file size in blocks as reported by the DHCP server */
187 u32 net_boot_file_expected_size_in_blocks;
189 static uchar net_pkt_buf[(PKTBUFSRX+1) * PKTSIZE_ALIGN + PKTALIGN];
190 /* Receive packets */
191 uchar *net_rx_packets[PKTBUFSRX];
192 /* Current UDP RX packet handler */
193 static rxhand_f *udp_packet_handler;
194 /* Current ARP RX packet handler */
195 static rxhand_f *arp_packet_handler;
196 #ifdef CONFIG_CMD_TFTPPUT
197 /* Current ICMP rx handler */
198 static rxhand_icmp_f *packet_icmp_handler;
199 #endif
200 /* Current timeout handler */
201 static thand_f *time_handler;
202 /* Time base value */
203 static ulong time_start;
204 /* Current timeout value */
205 static ulong time_delta;
206 /* THE transmit packet */
207 uchar *net_tx_packet;
209 static int net_check_prereq(enum proto_t protocol);
211 static int net_try_count;
213 int __maybe_unused net_busy_flag;
215 /**********************************************************************/
217 static int on_ipaddr(const char *name, const char *value, enum env_op op,
218 int flags)
220 if (flags & H_PROGRAMMATIC)
221 return 0;
223 net_ip = string_to_ip(value);
225 return 0;
227 U_BOOT_ENV_CALLBACK(ipaddr, on_ipaddr);
229 static int on_gatewayip(const char *name, const char *value, enum env_op op,
230 int flags)
232 if (flags & H_PROGRAMMATIC)
233 return 0;
235 net_gateway = string_to_ip(value);
237 return 0;
239 U_BOOT_ENV_CALLBACK(gatewayip, on_gatewayip);
241 static int on_netmask(const char *name, const char *value, enum env_op op,
242 int flags)
244 if (flags & H_PROGRAMMATIC)
245 return 0;
247 net_netmask = string_to_ip(value);
249 return 0;
251 U_BOOT_ENV_CALLBACK(netmask, on_netmask);
253 static int on_serverip(const char *name, const char *value, enum env_op op,
254 int flags)
256 if (flags & H_PROGRAMMATIC)
257 return 0;
259 net_server_ip = string_to_ip(value);
261 return 0;
263 U_BOOT_ENV_CALLBACK(serverip, on_serverip);
265 static int on_nvlan(const char *name, const char *value, enum env_op op,
266 int flags)
268 if (flags & H_PROGRAMMATIC)
269 return 0;
271 net_native_vlan = string_to_vlan(value);
273 return 0;
275 U_BOOT_ENV_CALLBACK(nvlan, on_nvlan);
277 static int on_vlan(const char *name, const char *value, enum env_op op,
278 int flags)
280 if (flags & H_PROGRAMMATIC)
281 return 0;
283 net_our_vlan = string_to_vlan(value);
285 return 0;
287 U_BOOT_ENV_CALLBACK(vlan, on_vlan);
289 #if defined(CONFIG_CMD_DNS)
290 static int on_dnsip(const char *name, const char *value, enum env_op op,
291 int flags)
293 if (flags & H_PROGRAMMATIC)
294 return 0;
296 net_dns_server = string_to_ip(value);
298 return 0;
300 U_BOOT_ENV_CALLBACK(dnsip, on_dnsip);
301 #endif
304 * Check if autoload is enabled. If so, use either NFS or TFTP to download
305 * the boot file.
307 void net_auto_load(void)
309 #if defined(CONFIG_CMD_NFS) && !defined(CONFIG_XPL_BUILD)
310 const char *s = env_get("autoload");
312 if (s != NULL && strcmp(s, "NFS") == 0) {
313 if (net_check_prereq(NFS)) {
314 /* We aren't expecting to get a serverip, so just accept the assigned IP */
315 if (IS_ENABLED(CONFIG_BOOTP_SERVERIP)) {
316 net_set_state(NETLOOP_SUCCESS);
317 } else {
318 printf("Cannot autoload with NFS\n");
319 net_set_state(NETLOOP_FAIL);
321 return;
324 * Use NFS to load the bootfile.
326 nfs_start();
327 return;
329 #endif
330 if (env_get_yesno("autoload") == 0) {
332 * Just use BOOTP/RARP to configure system;
333 * Do not use TFTP to load the bootfile.
335 net_set_state(NETLOOP_SUCCESS);
336 return;
338 if (IS_ENABLED(CONFIG_CMD_TFTPBOOT)) {
339 if (net_check_prereq(TFTPGET)) {
341 * We aren't expecting to get a serverip, so just
342 * accept the assigned IP
344 if (IS_ENABLED(CONFIG_BOOTP_SERVERIP)) {
345 net_set_state(NETLOOP_SUCCESS);
346 } else {
347 printf("Cannot autoload with TFTPGET\n");
348 net_set_state(NETLOOP_FAIL);
350 return;
352 tftp_start(TFTPGET);
356 static int net_init_loop(void)
358 static bool first_call = true;
360 if (eth_get_dev()) {
361 memcpy(net_ethaddr, eth_get_ethaddr(), 6);
363 if (IS_ENABLED(CONFIG_IPV6)) {
364 ip6_make_lladdr(&net_link_local_ip6, net_ethaddr);
365 if (!memcmp(&net_ip6, &net_null_addr_ip6,
366 sizeof(struct in6_addr)))
367 memcpy(&net_ip6, &net_link_local_ip6,
368 sizeof(struct in6_addr));
371 else
373 * Not ideal, but there's no way to get the actual error, and I
374 * don't feel like fixing all the users of eth_get_dev to deal
375 * with errors.
377 return -ENONET;
379 if (IS_ENABLED(CONFIG_IPV6_ROUTER_DISCOVERY))
380 if (first_call && use_ip6) {
381 first_call = false;
382 srand_mac(); /* This is for rand used in ip6_send_rs. */
383 net_loop(RS);
385 return 0;
388 static void net_clear_handlers(void)
390 net_set_udp_handler(NULL);
391 net_set_arp_handler(NULL);
392 net_set_timeout_handler(0, NULL);
395 static void net_cleanup_loop(void)
397 net_clear_handlers();
400 int net_init(void)
402 static int first_call = 1;
404 if (first_call) {
406 * Setup packet buffers, aligned correctly.
408 int i;
410 net_tx_packet = &net_pkt_buf[0] + (PKTALIGN - 1);
411 net_tx_packet -= (ulong)net_tx_packet % PKTALIGN;
412 for (i = 0; i < PKTBUFSRX; i++) {
413 net_rx_packets[i] = net_tx_packet +
414 (i + 1) * PKTSIZE_ALIGN;
416 arp_init();
417 ndisc_init();
418 net_clear_handlers();
420 /* Only need to setup buffer pointers once. */
421 first_call = 0;
422 if (IS_ENABLED(CONFIG_PROT_TCP))
423 tcp_set_tcp_state(TCP_CLOSED);
426 return net_init_loop();
429 /**********************************************************************/
431 * Main network processing loop.
434 int net_loop(enum proto_t protocol)
436 int ret = -EINVAL;
437 enum net_loop_state prev_net_state = net_state;
439 #if defined(CONFIG_CMD_PING)
440 if (protocol != PING)
441 net_ping_ip.s_addr = 0;
442 #endif
443 net_restarted = 0;
444 net_dev_exists = 0;
445 net_try_count = 1;
446 debug_cond(DEBUG_INT_STATE, "--- net_loop Entry\n");
448 #ifdef CONFIG_PHY_NCSI
449 if (phy_interface_is_ncsi() && protocol != NCSI && !ncsi_active()) {
450 printf("%s: configuring NCSI first\n", __func__);
451 if (net_loop(NCSI) < 0)
452 return ret;
453 eth_init_state_only();
454 goto restart;
456 #endif
458 bootstage_mark_name(BOOTSTAGE_ID_ETH_START, "eth_start");
459 net_init();
460 if (eth_is_on_demand_init()) {
461 eth_halt();
462 eth_set_current();
463 ret = eth_init();
464 if (ret < 0) {
465 eth_halt();
466 return ret;
468 } else {
469 eth_init_state_only();
472 restart:
473 #ifdef CONFIG_USB_KEYBOARD
474 net_busy_flag = 0;
475 #endif
476 net_set_state(NETLOOP_CONTINUE);
479 * Start the ball rolling with the given start function. From
480 * here on, this code is a state machine driven by received
481 * packets and timer events.
483 debug_cond(DEBUG_INT_STATE, "--- net_loop Init\n");
484 net_init_loop();
486 if (!test_eth_enabled())
487 return 0;
489 switch (net_check_prereq(protocol)) {
490 case 1:
491 /* network not configured */
492 eth_halt();
493 net_set_state(prev_net_state);
494 return -ENODEV;
496 case 2:
497 /* network device not configured */
498 break;
500 case 0:
501 net_dev_exists = 1;
502 net_boot_file_size = 0;
503 switch (protocol) {
504 #ifdef CONFIG_CMD_TFTPBOOT
505 case TFTPGET:
506 #ifdef CONFIG_CMD_TFTPPUT
507 case TFTPPUT:
508 #endif
509 /* always use ARP to get server ethernet address */
510 tftp_start(protocol);
511 break;
512 #endif
513 #ifdef CONFIG_CMD_TFTPSRV
514 case TFTPSRV:
515 tftp_start_server();
516 break;
517 #endif
518 #if CONFIG_IS_ENABLED(UDP_FUNCTION_FASTBOOT)
519 case FASTBOOT_UDP:
520 fastboot_udp_start_server();
521 break;
522 #endif
523 #if CONFIG_IS_ENABLED(TCP_FUNCTION_FASTBOOT)
524 case FASTBOOT_TCP:
525 fastboot_tcp_start_server();
526 break;
527 #endif
528 #if defined(CONFIG_CMD_DHCP)
529 case DHCP:
530 bootp_reset();
531 net_ip.s_addr = 0;
532 dhcp_request(); /* Basically same as BOOTP */
533 break;
534 #endif
535 case DHCP6:
536 if (IS_ENABLED(CONFIG_CMD_DHCP6))
537 dhcp6_start();
538 break;
539 #if defined(CONFIG_CMD_BOOTP)
540 case BOOTP:
541 bootp_reset();
542 net_ip.s_addr = 0;
543 bootp_request();
544 break;
545 #endif
546 #if defined(CONFIG_CMD_RARP)
547 case RARP:
548 rarp_try = 0;
549 net_ip.s_addr = 0;
550 rarp_request();
551 break;
552 #endif
553 #if defined(CONFIG_CMD_PING)
554 case PING:
555 ping_start();
556 break;
557 #endif
558 #if defined(CONFIG_CMD_PING6)
559 case PING6:
560 ping6_start();
561 break;
562 #endif
563 #if defined(CONFIG_CMD_NFS) && !defined(CONFIG_XPL_BUILD)
564 case NFS:
565 nfs_start();
566 break;
567 #endif
568 #if defined(CONFIG_CMD_WGET)
569 case WGET:
570 wget_start();
571 break;
572 #endif
573 #if defined(CONFIG_CMD_CDP)
574 case CDP:
575 cdp_start();
576 break;
577 #endif
578 #if defined(CONFIG_NETCONSOLE) && !defined(CONFIG_XPL_BUILD)
579 case NETCONS:
580 nc_start();
581 break;
582 #endif
583 #if defined(CONFIG_CMD_DNS)
584 case DNS:
585 dns_start();
586 break;
587 #endif
588 #if defined(CONFIG_CMD_LINK_LOCAL)
589 case LINKLOCAL:
590 link_local_start();
591 break;
592 #endif
593 #if defined(CONFIG_CMD_WOL)
594 case WOL:
595 wol_start();
596 break;
597 #endif
598 #if defined(CONFIG_PHY_NCSI)
599 case NCSI:
600 ncsi_probe_packages();
601 break;
602 #endif
603 case RS:
604 if (IS_ENABLED(CONFIG_IPV6_ROUTER_DISCOVERY))
605 ip6_send_rs();
606 break;
607 default:
608 break;
611 if (IS_ENABLED(CONFIG_PROT_UDP) && protocol == UDP)
612 udp_start();
614 break;
617 #if defined(CONFIG_MII) || defined(CONFIG_CMD_MII)
618 #if defined(CONFIG_SYS_FAULT_ECHO_LINK_DOWN) && \
619 defined(CONFIG_LED_STATUS) && \
620 defined(CONFIG_LED_STATUS_RED)
622 * Echo the inverted link state to the fault LED.
624 if (miiphy_link(eth_get_dev()->name, CONFIG_SYS_FAULT_MII_ADDR))
625 status_led_set(CONFIG_LED_STATUS_RED, CONFIG_LED_STATUS_OFF);
626 else
627 status_led_set(CONFIG_LED_STATUS_RED, CONFIG_LED_STATUS_ON);
628 #endif /* CONFIG_SYS_FAULT_ECHO_LINK_DOWN, ... */
629 #endif /* CONFIG_MII, ... */
630 #ifdef CONFIG_USB_KEYBOARD
631 net_busy_flag = 1;
632 #endif
635 * Main packet reception loop. Loop receiving packets until
636 * someone sets `net_state' to a state that terminates.
638 for (;;) {
639 schedule();
640 if (arp_timeout_check() > 0)
641 time_start = get_timer(0);
643 if (IS_ENABLED(CONFIG_IPV6)) {
644 if (use_ip6 && (ndisc_timeout_check() > 0))
645 time_start = get_timer(0);
649 * Check the ethernet for a new packet. The ethernet
650 * receive routine will process it.
651 * Most drivers return the most recent packet size, but not
652 * errors that may have happened.
654 eth_rx();
657 * Abort if ctrl-c was pressed.
659 if (ctrlc()) {
660 /* cancel any ARP that may not have completed */
661 net_arp_wait_packet_ip.s_addr = 0;
663 net_cleanup_loop();
664 eth_halt();
665 /* Invalidate the last protocol */
666 eth_set_last_protocol(BOOTP);
668 /* Turn off activity LED if triggered */
669 led_activity_off();
671 puts("\nAbort\n");
672 /* include a debug print as well incase the debug
673 messages are directed to stderr */
674 debug_cond(DEBUG_INT_STATE, "--- net_loop Abort!\n");
675 ret = -EINTR;
676 goto done;
680 * Check for a timeout, and run the timeout handler
681 * if we have one.
683 if (time_handler &&
684 ((get_timer(0) - time_start) > time_delta)) {
685 thand_f *x;
687 #if defined(CONFIG_MII) || defined(CONFIG_CMD_MII)
688 #if defined(CONFIG_SYS_FAULT_ECHO_LINK_DOWN) && \
689 defined(CONFIG_LED_STATUS) && \
690 defined(CONFIG_LED_STATUS_RED)
692 * Echo the inverted link state to the fault LED.
694 if (miiphy_link(eth_get_dev()->name,
695 CONFIG_SYS_FAULT_MII_ADDR))
696 status_led_set(CONFIG_LED_STATUS_RED,
697 CONFIG_LED_STATUS_OFF);
698 else
699 status_led_set(CONFIG_LED_STATUS_RED,
700 CONFIG_LED_STATUS_ON);
701 #endif /* CONFIG_SYS_FAULT_ECHO_LINK_DOWN, ... */
702 #endif /* CONFIG_MII, ... */
703 debug_cond(DEBUG_INT_STATE, "--- net_loop timeout\n");
704 x = time_handler;
705 time_handler = (thand_f *)0;
706 (*x)();
707 } else if (IS_ENABLED(CONFIG_IPV6_ROUTER_DISCOVERY))
708 if (time_handler && protocol == RS)
709 if (!ip6_is_unspecified_addr(&net_gateway6) &&
710 net_prefix_length != 0) {
711 net_set_state(NETLOOP_SUCCESS);
712 net_set_timeout_handler(0, NULL);
715 if (net_state == NETLOOP_FAIL)
716 ret = net_start_again();
718 switch (net_state) {
719 case NETLOOP_RESTART:
720 net_restarted = 1;
721 goto restart;
723 case NETLOOP_SUCCESS:
724 net_cleanup_loop();
725 if (net_boot_file_size > 0) {
726 printf("Bytes transferred = %u (%x hex)\n",
727 net_boot_file_size, net_boot_file_size);
728 env_set_hex("filesize", net_boot_file_size);
729 env_set_hex("fileaddr", image_load_addr);
731 if (protocol != NETCONS && protocol != NCSI)
732 eth_halt();
733 else
734 eth_halt_state_only();
736 eth_set_last_protocol(protocol);
738 ret = net_boot_file_size;
739 debug_cond(DEBUG_INT_STATE, "--- net_loop Success!\n");
740 goto done;
742 case NETLOOP_FAIL:
743 net_cleanup_loop();
744 /* Invalidate the last protocol */
745 eth_set_last_protocol(BOOTP);
746 debug_cond(DEBUG_INT_STATE, "--- net_loop Fail!\n");
747 ret = -ENONET;
748 goto done;
750 case NETLOOP_CONTINUE:
751 continue;
755 done:
756 #ifdef CONFIG_USB_KEYBOARD
757 net_busy_flag = 0;
758 #endif
759 #ifdef CONFIG_CMD_TFTPPUT
760 /* Clear out the handlers */
761 net_set_udp_handler(NULL);
762 net_set_icmp_handler(NULL);
763 #endif
764 net_set_state(prev_net_state);
766 #if defined(CONFIG_CMD_PCAP)
767 if (pcap_active())
768 pcap_print_status();
769 #endif
770 return ret;
773 /**********************************************************************/
775 static void start_again_timeout_handler(void)
777 net_set_state(NETLOOP_RESTART);
780 int net_start_again(void)
782 char *nretry;
783 int retry_forever = 0;
784 unsigned long retrycnt = 0;
785 int ret;
787 nretry = env_get("netretry");
788 if (nretry) {
789 if (!strcmp(nretry, "yes"))
790 retry_forever = 1;
791 else if (!strcmp(nretry, "no"))
792 retrycnt = 0;
793 else if (!strcmp(nretry, "once"))
794 retrycnt = 1;
795 else
796 retrycnt = simple_strtoul(nretry, NULL, 0);
797 } else {
798 retrycnt = 0;
799 retry_forever = 0;
802 if ((!retry_forever) && (net_try_count > retrycnt)) {
803 eth_halt();
804 net_set_state(NETLOOP_FAIL);
806 * We don't provide a way for the protocol to return an error,
807 * but this is almost always the reason.
809 return -ETIMEDOUT;
812 net_try_count++;
814 eth_halt();
815 #if !defined(CONFIG_NET_DO_NOT_TRY_ANOTHER)
816 eth_try_another(!net_restarted);
817 #endif
818 ret = eth_init();
819 if (net_restart_wrap) {
820 net_restart_wrap = 0;
821 if (net_dev_exists) {
822 net_set_timeout_handler(10000UL,
823 start_again_timeout_handler);
824 net_set_udp_handler(NULL);
825 } else {
826 net_set_state(NETLOOP_FAIL);
828 } else {
829 net_set_state(NETLOOP_RESTART);
831 return ret;
834 /**********************************************************************/
836 * Miscelaneous bits.
839 static void dummy_handler(uchar *pkt, unsigned dport,
840 struct in_addr sip, unsigned sport,
841 unsigned len)
845 rxhand_f *net_get_udp_handler(void)
847 return udp_packet_handler;
850 void net_set_udp_handler(rxhand_f *f)
852 debug_cond(DEBUG_INT_STATE, "--- net_loop UDP handler set (%p)\n", f);
853 if (f == NULL)
854 udp_packet_handler = dummy_handler;
855 else
856 udp_packet_handler = f;
859 rxhand_f *net_get_arp_handler(void)
861 return arp_packet_handler;
864 void net_set_arp_handler(rxhand_f *f)
866 debug_cond(DEBUG_INT_STATE, "--- net_loop ARP handler set (%p)\n", f);
867 if (f == NULL)
868 arp_packet_handler = dummy_handler;
869 else
870 arp_packet_handler = f;
873 #ifdef CONFIG_CMD_TFTPPUT
874 void net_set_icmp_handler(rxhand_icmp_f *f)
876 packet_icmp_handler = f;
878 #endif
880 void net_set_timeout_handler(ulong iv, thand_f *f)
882 if (iv == 0) {
883 debug_cond(DEBUG_INT_STATE,
884 "--- net_loop timeout handler cancelled\n");
885 time_handler = (thand_f *)0;
886 } else {
887 debug_cond(DEBUG_INT_STATE,
888 "--- net_loop timeout handler set (%p)\n", f);
889 time_handler = f;
890 time_start = get_timer(0);
891 time_delta = iv * CONFIG_SYS_HZ / 1000;
895 uchar *net_get_async_tx_pkt_buf(void)
897 if (arp_is_waiting())
898 return arp_tx_packet; /* If we are waiting, we already sent */
899 else
900 return net_tx_packet;
903 int net_send_udp_packet(uchar *ether, struct in_addr dest, int dport, int sport,
904 int payload_len)
906 return net_send_ip_packet(ether, dest, dport, sport, payload_len,
907 IPPROTO_UDP, 0, 0, 0);
910 #if defined(CONFIG_PROT_TCP)
911 int net_send_tcp_packet(int payload_len, int dport, int sport, u8 action,
912 u32 tcp_seq_num, u32 tcp_ack_num)
914 return net_send_ip_packet(net_server_ethaddr, net_server_ip, dport,
915 sport, payload_len, IPPROTO_TCP, action,
916 tcp_seq_num, tcp_ack_num);
918 #endif
920 int net_send_ip_packet(uchar *ether, struct in_addr dest, int dport, int sport,
921 int payload_len, int proto, u8 action, u32 tcp_seq_num,
922 u32 tcp_ack_num)
924 uchar *pkt;
925 int eth_hdr_size;
926 int pkt_hdr_size;
928 /* make sure the net_tx_packet is initialized (net_init() was called) */
929 assert(net_tx_packet != NULL);
930 if (net_tx_packet == NULL)
931 return -1;
933 /* convert to new style broadcast */
934 if (dest.s_addr == 0)
935 dest.s_addr = 0xFFFFFFFF;
937 /* if broadcast, make the ether address a broadcast and don't do ARP */
938 if (dest.s_addr == 0xFFFFFFFF)
939 ether = (uchar *)net_bcast_ethaddr;
941 pkt = (uchar *)net_tx_packet;
943 eth_hdr_size = net_set_ether(pkt, ether, PROT_IP);
945 switch (proto) {
946 case IPPROTO_UDP:
947 net_set_udp_header(pkt + eth_hdr_size, dest, dport, sport,
948 payload_len);
949 pkt_hdr_size = eth_hdr_size + IP_UDP_HDR_SIZE;
950 break;
951 #if defined(CONFIG_PROT_TCP)
952 case IPPROTO_TCP:
953 pkt_hdr_size = eth_hdr_size
954 + tcp_set_tcp_header(pkt + eth_hdr_size, dport, sport,
955 payload_len, action, tcp_seq_num,
956 tcp_ack_num);
957 break;
958 #endif
959 default:
960 return -EINVAL;
963 /* if MAC address was not discovered yet, do an ARP request */
964 if (memcmp(ether, net_null_ethaddr, 6) == 0) {
965 debug_cond(DEBUG_DEV_PKT, "sending ARP for %pI4\n", &dest);
967 /* save the ip and eth addr for the packet to send after arp */
968 net_arp_wait_packet_ip = dest;
969 arp_wait_packet_ethaddr = ether;
971 /* size of the waiting packet */
972 arp_wait_tx_packet_size = pkt_hdr_size + payload_len;
974 /* and do the ARP request */
975 arp_wait_try = 1;
976 arp_wait_timer_start = get_timer(0);
977 arp_request();
978 return 1; /* waiting */
979 } else {
980 debug_cond(DEBUG_DEV_PKT, "sending UDP to %pI4/%pM\n",
981 &dest, ether);
982 net_send_packet(net_tx_packet, pkt_hdr_size + payload_len);
983 return 0; /* transmitted */
987 #ifdef CONFIG_IP_DEFRAG
989 * This function collects fragments in a single packet, according
990 * to the algorithm in RFC815. It returns NULL or the pointer to
991 * a complete packet, in static storage
993 #define IP_PKTSIZE (CONFIG_NET_MAXDEFRAG)
995 #define IP_MAXUDP (IP_PKTSIZE - IP_HDR_SIZE)
998 * this is the packet being assembled, either data or frag control.
999 * Fragments go by 8 bytes, so this union must be 8 bytes long
1001 struct hole {
1002 /* first_byte is address of this structure */
1003 u16 last_byte; /* last byte in this hole + 1 (begin of next hole) */
1004 u16 next_hole; /* index of next (in 8-b blocks), 0 == none */
1005 u16 prev_hole; /* index of prev, 0 == none */
1006 u16 unused;
1009 static struct ip_udp_hdr *__net_defragment(struct ip_udp_hdr *ip, int *lenp)
1011 static uchar pkt_buff[IP_PKTSIZE] __aligned(PKTALIGN);
1012 static u16 first_hole, total_len;
1013 struct hole *payload, *thisfrag, *h, *newh;
1014 struct ip_udp_hdr *localip = (struct ip_udp_hdr *)pkt_buff;
1015 uchar *indata = (uchar *)ip;
1016 int offset8, start, len, done = 0;
1017 u16 ip_off = ntohs(ip->ip_off);
1020 * Calling code already rejected <, but we don't have to deal
1021 * with an IP fragment with no payload.
1023 if (ntohs(ip->ip_len) <= IP_HDR_SIZE)
1024 return NULL;
1026 /* payload starts after IP header, this fragment is in there */
1027 payload = (struct hole *)(pkt_buff + IP_HDR_SIZE);
1028 offset8 = (ip_off & IP_OFFS);
1029 thisfrag = payload + offset8;
1030 start = offset8 * 8;
1031 len = ntohs(ip->ip_len) - IP_HDR_SIZE;
1033 /* All but last fragment must have a multiple-of-8 payload. */
1034 if ((len & 7) && (ip_off & IP_FLAGS_MFRAG))
1035 return NULL;
1037 if (start + len > IP_MAXUDP) /* fragment extends too far */
1038 return NULL;
1040 if (!total_len || localip->ip_id != ip->ip_id) {
1041 /* new (or different) packet, reset structs */
1042 total_len = 0xffff;
1043 payload[0].last_byte = ~0;
1044 payload[0].next_hole = 0;
1045 payload[0].prev_hole = 0;
1046 first_hole = 0;
1047 /* any IP header will work, copy the first we received */
1048 memcpy(localip, ip, IP_HDR_SIZE);
1052 * What follows is the reassembly algorithm. We use the payload
1053 * array as a linked list of hole descriptors, as each hole starts
1054 * at a multiple of 8 bytes. However, last byte can be whatever value,
1055 * so it is represented as byte count, not as 8-byte blocks.
1058 h = payload + first_hole;
1059 while (h->last_byte < start) {
1060 if (!h->next_hole) {
1061 /* no hole that far away */
1062 return NULL;
1064 h = payload + h->next_hole;
1067 /* last fragment may be 1..7 bytes, the "+7" forces acceptance */
1068 if (offset8 + ((len + 7) / 8) <= h - payload) {
1069 /* no overlap with holes (dup fragment?) */
1070 return NULL;
1073 if (!(ip_off & IP_FLAGS_MFRAG)) {
1074 /* no more fragmentss: truncate this (last) hole */
1075 total_len = start + len;
1076 h->last_byte = start + len;
1080 * There is some overlap: fix the hole list. This code deals
1081 * with a fragment that overlaps with two different holes
1082 * (thus being a superset of a previously-received fragment)
1083 * by only using the part of the fragment that fits in the
1084 * first hole.
1086 if (h->last_byte < start + len)
1087 len = h->last_byte - start;
1089 if ((h >= thisfrag) && (h->last_byte <= start + len)) {
1090 /* complete overlap with hole: remove hole */
1091 if (!h->prev_hole && !h->next_hole) {
1092 /* last remaining hole */
1093 done = 1;
1094 } else if (!h->prev_hole) {
1095 /* first hole */
1096 first_hole = h->next_hole;
1097 payload[h->next_hole].prev_hole = 0;
1098 } else if (!h->next_hole) {
1099 /* last hole */
1100 payload[h->prev_hole].next_hole = 0;
1101 } else {
1102 /* in the middle of the list */
1103 payload[h->next_hole].prev_hole = h->prev_hole;
1104 payload[h->prev_hole].next_hole = h->next_hole;
1107 } else if (h->last_byte <= start + len) {
1108 /* overlaps with final part of the hole: shorten this hole */
1109 h->last_byte = start;
1111 } else if (h >= thisfrag) {
1112 /* overlaps with initial part of the hole: move this hole */
1113 newh = thisfrag + (len / 8);
1114 *newh = *h;
1115 h = newh;
1116 if (h->next_hole)
1117 payload[h->next_hole].prev_hole = (h - payload);
1118 if (h->prev_hole)
1119 payload[h->prev_hole].next_hole = (h - payload);
1120 else
1121 first_hole = (h - payload);
1123 } else {
1124 /* fragment sits in the middle: split the hole */
1125 newh = thisfrag + (len / 8);
1126 *newh = *h;
1127 h->last_byte = start;
1128 h->next_hole = (newh - payload);
1129 newh->prev_hole = (h - payload);
1130 if (newh->next_hole)
1131 payload[newh->next_hole].prev_hole = (newh - payload);
1134 /* finally copy this fragment and possibly return whole packet */
1135 memcpy((uchar *)thisfrag, indata + IP_HDR_SIZE, len);
1136 if (!done)
1137 return NULL;
1139 *lenp = total_len + IP_HDR_SIZE;
1140 localip->ip_len = htons(*lenp);
1141 return localip;
1144 static inline struct ip_udp_hdr *net_defragment(struct ip_udp_hdr *ip,
1145 int *lenp)
1147 u16 ip_off = ntohs(ip->ip_off);
1148 if (!(ip_off & (IP_OFFS | IP_FLAGS_MFRAG)))
1149 return ip; /* not a fragment */
1150 return __net_defragment(ip, lenp);
1153 #else /* !CONFIG_IP_DEFRAG */
1155 static inline struct ip_udp_hdr *net_defragment(struct ip_udp_hdr *ip,
1156 int *lenp)
1158 u16 ip_off = ntohs(ip->ip_off);
1159 if (!(ip_off & (IP_OFFS | IP_FLAGS_MFRAG)))
1160 return ip; /* not a fragment */
1161 return NULL;
1163 #endif
1166 * Receive an ICMP packet. We deal with REDIRECT and PING here, and silently
1167 * drop others.
1169 * @parma ip IP packet containing the ICMP
1171 static void receive_icmp(struct ip_udp_hdr *ip, int len,
1172 struct in_addr src_ip, struct ethernet_hdr *et)
1174 struct icmp_hdr *icmph = (struct icmp_hdr *)&ip->udp_src;
1176 switch (icmph->type) {
1177 case ICMP_REDIRECT:
1178 if (icmph->code != ICMP_REDIR_HOST)
1179 return;
1180 printf(" ICMP Host Redirect to %pI4 ",
1181 &icmph->un.gateway);
1182 break;
1183 default:
1184 #if defined(CONFIG_CMD_PING)
1185 ping_receive(et, ip, len);
1186 #endif
1187 #ifdef CONFIG_CMD_TFTPPUT
1188 if (packet_icmp_handler)
1189 packet_icmp_handler(icmph->type, icmph->code,
1190 ntohs(ip->udp_dst), src_ip,
1191 ntohs(ip->udp_src), icmph->un.data,
1192 ntohs(ip->udp_len));
1193 #endif
1194 break;
1198 void net_process_received_packet(uchar *in_packet, int len)
1200 struct ethernet_hdr *et;
1201 struct ip_udp_hdr *ip;
1202 struct in_addr dst_ip;
1203 struct in_addr src_ip;
1204 int eth_proto;
1205 #if defined(CONFIG_CMD_CDP)
1206 int iscdp;
1207 #endif
1208 ushort cti = 0, vlanid = VLAN_NONE, myvlanid, mynvlanid;
1210 debug_cond(DEBUG_NET_PKT, "packet received\n");
1211 if (DEBUG_NET_PKT_TRACE)
1212 print_hex_dump_bytes("rx: ", DUMP_PREFIX_OFFSET, in_packet,
1213 len);
1215 #if defined(CONFIG_CMD_PCAP)
1216 pcap_post(in_packet, len, false);
1217 #endif
1218 net_rx_packet = in_packet;
1219 net_rx_packet_len = len;
1220 et = (struct ethernet_hdr *)in_packet;
1222 /* too small packet? */
1223 if (len < ETHER_HDR_SIZE)
1224 return;
1226 #if defined(CONFIG_API) || defined(CONFIG_EFI_LOADER)
1227 if (push_packet) {
1228 (*push_packet)(in_packet, len);
1229 return;
1231 #endif
1233 #if defined(CONFIG_CMD_CDP)
1234 /* keep track if packet is CDP */
1235 iscdp = is_cdp_packet(et->et_dest);
1236 #endif
1238 myvlanid = ntohs(net_our_vlan);
1239 if (myvlanid == (ushort)-1)
1240 myvlanid = VLAN_NONE;
1241 mynvlanid = ntohs(net_native_vlan);
1242 if (mynvlanid == (ushort)-1)
1243 mynvlanid = VLAN_NONE;
1245 eth_proto = ntohs(et->et_protlen);
1247 if (eth_proto < 1514) {
1248 struct e802_hdr *et802 = (struct e802_hdr *)et;
1250 * Got a 802.2 packet. Check the other protocol field.
1251 * XXX VLAN over 802.2+SNAP not implemented!
1253 eth_proto = ntohs(et802->et_prot);
1255 ip = (struct ip_udp_hdr *)(in_packet + E802_HDR_SIZE);
1256 len -= E802_HDR_SIZE;
1258 } else if (eth_proto != PROT_VLAN) { /* normal packet */
1259 ip = (struct ip_udp_hdr *)(in_packet + ETHER_HDR_SIZE);
1260 len -= ETHER_HDR_SIZE;
1262 } else { /* VLAN packet */
1263 struct vlan_ethernet_hdr *vet =
1264 (struct vlan_ethernet_hdr *)et;
1266 debug_cond(DEBUG_NET_PKT, "VLAN packet received\n");
1268 /* too small packet? */
1269 if (len < VLAN_ETHER_HDR_SIZE)
1270 return;
1272 /* if no VLAN active */
1273 if ((ntohs(net_our_vlan) & VLAN_IDMASK) == VLAN_NONE
1274 #if defined(CONFIG_CMD_CDP)
1275 && iscdp == 0
1276 #endif
1278 return;
1280 cti = ntohs(vet->vet_tag);
1281 vlanid = cti & VLAN_IDMASK;
1282 eth_proto = ntohs(vet->vet_type);
1284 ip = (struct ip_udp_hdr *)(in_packet + VLAN_ETHER_HDR_SIZE);
1285 len -= VLAN_ETHER_HDR_SIZE;
1288 debug_cond(DEBUG_NET_PKT, "Receive from protocol 0x%x\n", eth_proto);
1290 #if defined(CONFIG_CMD_CDP)
1291 if (iscdp) {
1292 cdp_receive((uchar *)ip, len);
1293 return;
1295 #endif
1297 if ((myvlanid & VLAN_IDMASK) != VLAN_NONE) {
1298 if (vlanid == VLAN_NONE)
1299 vlanid = (mynvlanid & VLAN_IDMASK);
1300 /* not matched? */
1301 if (vlanid != (myvlanid & VLAN_IDMASK))
1302 return;
1305 switch (eth_proto) {
1306 case PROT_ARP:
1307 arp_receive(et, ip, len);
1308 break;
1310 #ifdef CONFIG_CMD_RARP
1311 case PROT_RARP:
1312 rarp_receive(ip, len);
1313 break;
1314 #endif
1315 #if IS_ENABLED(CONFIG_IPV6)
1316 case PROT_IP6:
1317 net_ip6_handler(et, (struct ip6_hdr *)ip, len);
1318 break;
1319 #endif
1320 case PROT_IP:
1321 debug_cond(DEBUG_NET_PKT, "Got IP\n");
1322 /* Before we start poking the header, make sure it is there */
1323 if (len < IP_HDR_SIZE) {
1324 debug("len bad %d < %lu\n", len,
1325 (ulong)IP_HDR_SIZE);
1326 return;
1328 /* Check the packet length */
1329 if (len < ntohs(ip->ip_len)) {
1330 debug("len bad %d < %d\n", len, ntohs(ip->ip_len));
1331 return;
1333 len = ntohs(ip->ip_len);
1334 if (len < IP_HDR_SIZE) {
1335 debug("bad ip->ip_len %d < %d\n", len, (int)IP_HDR_SIZE);
1336 return;
1338 debug_cond(DEBUG_NET_PKT, "len=%d, v=%02x\n",
1339 len, ip->ip_hl_v & 0xff);
1341 /* Can't deal with anything except IPv4 */
1342 if ((ip->ip_hl_v & 0xf0) != 0x40)
1343 return;
1344 /* Can't deal with IP options (headers != 20 bytes) */
1345 if ((ip->ip_hl_v & 0x0f) != 0x05)
1346 return;
1347 /* Check the Checksum of the header */
1348 if (!ip_checksum_ok((uchar *)ip, IP_HDR_SIZE)) {
1349 debug("checksum bad\n");
1350 return;
1352 /* If it is not for us, ignore it */
1353 dst_ip = net_read_ip(&ip->ip_dst);
1354 if (net_ip.s_addr && dst_ip.s_addr != net_ip.s_addr &&
1355 dst_ip.s_addr != 0xFFFFFFFF) {
1356 return;
1358 /* Read source IP address for later use */
1359 src_ip = net_read_ip(&ip->ip_src);
1361 * The function returns the unchanged packet if it's not
1362 * a fragment, and either the complete packet or NULL if
1363 * it is a fragment (if !CONFIG_IP_DEFRAG, it returns NULL)
1365 ip = net_defragment(ip, &len);
1366 if (!ip)
1367 return;
1369 * watch for ICMP host redirects
1371 * There is no real handler code (yet). We just watch
1372 * for ICMP host redirect messages. In case anybody
1373 * sees these messages: please contact me
1374 * (wd@denx.de), or - even better - send me the
1375 * necessary fixes :-)
1377 * Note: in all cases where I have seen this so far
1378 * it was a problem with the router configuration,
1379 * for instance when a router was configured in the
1380 * BOOTP reply, but the TFTP server was on the same
1381 * subnet. So this is probably a warning that your
1382 * configuration might be wrong. But I'm not really
1383 * sure if there aren't any other situations.
1385 * Simon Glass <sjg@chromium.org>: We get an ICMP when
1386 * we send a tftp packet to a dead connection, or when
1387 * there is no server at the other end.
1389 if (ip->ip_p == IPPROTO_ICMP) {
1390 receive_icmp(ip, len, src_ip, et);
1391 return;
1392 #if defined(CONFIG_PROT_TCP)
1393 } else if (ip->ip_p == IPPROTO_TCP) {
1394 debug_cond(DEBUG_DEV_PKT,
1395 "TCP PH (to=%pI4, from=%pI4, len=%d)\n",
1396 &dst_ip, &src_ip, len);
1398 rxhand_tcp_f((union tcp_build_pkt *)ip, len);
1399 return;
1400 #endif
1401 } else if (ip->ip_p != IPPROTO_UDP) { /* Only UDP packets */
1402 return;
1405 if (ntohs(ip->udp_len) < UDP_HDR_SIZE || ntohs(ip->udp_len) > len - IP_HDR_SIZE)
1406 return;
1408 debug_cond(DEBUG_DEV_PKT,
1409 "received UDP (to=%pI4, from=%pI4, len=%d)\n",
1410 &dst_ip, &src_ip, len);
1412 if (IS_ENABLED(CONFIG_UDP_CHECKSUM) && ip->udp_xsum != 0) {
1413 ulong xsum;
1414 u8 *sumptr;
1415 ushort sumlen;
1417 xsum = ip->ip_p;
1418 xsum += (ntohs(ip->udp_len));
1419 xsum += (ntohl(ip->ip_src.s_addr) >> 16) & 0x0000ffff;
1420 xsum += (ntohl(ip->ip_src.s_addr) >> 0) & 0x0000ffff;
1421 xsum += (ntohl(ip->ip_dst.s_addr) >> 16) & 0x0000ffff;
1422 xsum += (ntohl(ip->ip_dst.s_addr) >> 0) & 0x0000ffff;
1424 sumlen = ntohs(ip->udp_len);
1425 sumptr = (u8 *)&ip->udp_src;
1427 while (sumlen > 1) {
1428 /* inlined ntohs() to avoid alignment errors */
1429 xsum += (sumptr[0] << 8) + sumptr[1];
1430 sumptr += 2;
1431 sumlen -= 2;
1433 if (sumlen > 0)
1434 xsum += (sumptr[0] << 8) + sumptr[0];
1435 while ((xsum >> 16) != 0) {
1436 xsum = (xsum & 0x0000ffff) +
1437 ((xsum >> 16) & 0x0000ffff);
1439 if ((xsum != 0x00000000) && (xsum != 0x0000ffff)) {
1440 printf(" UDP wrong checksum %08lx %08x\n",
1441 xsum, ntohs(ip->udp_xsum));
1442 return;
1446 #if defined(CONFIG_NETCONSOLE) && !defined(CONFIG_XPL_BUILD)
1447 nc_input_packet((uchar *)ip + IP_UDP_HDR_SIZE,
1448 src_ip,
1449 ntohs(ip->udp_dst),
1450 ntohs(ip->udp_src),
1451 ntohs(ip->udp_len) - UDP_HDR_SIZE);
1452 #endif
1454 * IP header OK. Pass the packet to the current handler.
1456 (*udp_packet_handler)((uchar *)ip + IP_UDP_HDR_SIZE,
1457 ntohs(ip->udp_dst),
1458 src_ip,
1459 ntohs(ip->udp_src),
1460 ntohs(ip->udp_len) - UDP_HDR_SIZE);
1461 break;
1462 #ifdef CONFIG_CMD_WOL
1463 case PROT_WOL:
1464 wol_receive(ip, len);
1465 break;
1466 #endif
1467 #ifdef CONFIG_PHY_NCSI
1468 case PROT_NCSI:
1469 ncsi_receive(et, ip, len);
1470 break;
1471 #endif
1475 /**********************************************************************/
1477 static int net_check_prereq(enum proto_t protocol)
1479 switch (protocol) {
1480 /* Fall through */
1481 #if defined(CONFIG_CMD_PING)
1482 case PING:
1483 if (net_ping_ip.s_addr == 0) {
1484 puts("*** ERROR: ping address not given\n");
1485 return 1;
1487 goto common;
1488 #endif
1489 #if defined(CONFIG_CMD_PING6)
1490 case PING6:
1491 if (ip6_is_unspecified_addr(&net_ping_ip6)) {
1492 puts("*** ERROR: ping address not given\n");
1493 return 1;
1495 goto common;
1496 #endif
1497 #if defined(CONFIG_CMD_DNS)
1498 case DNS:
1499 if (net_dns_server.s_addr == 0) {
1500 puts("*** ERROR: DNS server address not given\n");
1501 return 1;
1503 goto common;
1504 #endif
1505 #if defined(CONFIG_PROT_UDP)
1506 case UDP:
1507 if (udp_prereq())
1508 return 1;
1509 goto common;
1510 #endif
1512 #if defined(CONFIG_CMD_NFS)
1513 case NFS:
1514 #endif
1515 /* Fall through */
1516 case TFTPGET:
1517 case TFTPPUT:
1518 if (IS_ENABLED(CONFIG_IPV6) && use_ip6) {
1519 if (!memcmp(&net_server_ip6, &net_null_addr_ip6,
1520 sizeof(struct in6_addr)) &&
1521 !strchr(net_boot_file_name, '[')) {
1522 puts("*** ERROR: `serverip6' not set\n");
1523 return 1;
1525 } else if (net_server_ip.s_addr == 0 && !is_serverip_in_cmd()) {
1526 puts("*** ERROR: `serverip' not set\n");
1527 return 1;
1529 #if defined(CONFIG_CMD_PING) || \
1530 defined(CONFIG_CMD_DNS) || defined(CONFIG_PROT_UDP)
1531 common:
1532 #endif
1533 /* Fall through */
1535 case NETCONS:
1536 case FASTBOOT_UDP:
1537 case FASTBOOT_TCP:
1538 case TFTPSRV:
1539 if (IS_ENABLED(CONFIG_IPV6) && use_ip6) {
1540 if (!memcmp(&net_link_local_ip6, &net_null_addr_ip6,
1541 sizeof(struct in6_addr))) {
1542 puts("*** ERROR: `ip6addr` not set\n");
1543 return 1;
1545 } else if (net_ip.s_addr == 0) {
1546 puts("*** ERROR: `ipaddr' not set\n");
1547 return 1;
1549 /* Fall through */
1551 #ifdef CONFIG_CMD_RARP
1552 case RARP:
1553 #endif
1554 #ifdef CONFIG_PHY_NCSI
1555 case NCSI:
1556 #endif
1557 case BOOTP:
1558 case CDP:
1559 case DHCP:
1560 case LINKLOCAL:
1561 if (memcmp(net_ethaddr, "\0\0\0\0\0\0", 6) == 0) {
1562 int num = eth_get_dev_index();
1564 switch (num) {
1565 case -1:
1566 puts("*** ERROR: No ethernet found.\n");
1567 return 1;
1568 case 0:
1569 puts("*** ERROR: `ethaddr' not set\n");
1570 break;
1571 default:
1572 printf("*** ERROR: `eth%daddr' not set\n",
1573 num);
1574 break;
1577 net_start_again();
1578 return 2;
1580 /* Fall through */
1581 default:
1582 return 0;
1584 return 0; /* OK */
1586 /**********************************************************************/
1589 net_eth_hdr_size(void)
1591 ushort myvlanid;
1593 myvlanid = ntohs(net_our_vlan);
1594 if (myvlanid == (ushort)-1)
1595 myvlanid = VLAN_NONE;
1597 return ((myvlanid & VLAN_IDMASK) == VLAN_NONE) ? ETHER_HDR_SIZE :
1598 VLAN_ETHER_HDR_SIZE;
1601 int net_set_ether(uchar *xet, const uchar *dest_ethaddr, uint prot)
1603 struct ethernet_hdr *et = (struct ethernet_hdr *)xet;
1604 ushort myvlanid;
1606 myvlanid = ntohs(net_our_vlan);
1607 if (myvlanid == (ushort)-1)
1608 myvlanid = VLAN_NONE;
1610 memcpy(et->et_dest, dest_ethaddr, 6);
1611 memcpy(et->et_src, net_ethaddr, 6);
1612 if ((myvlanid & VLAN_IDMASK) == VLAN_NONE) {
1613 et->et_protlen = htons(prot);
1614 return ETHER_HDR_SIZE;
1615 } else {
1616 struct vlan_ethernet_hdr *vet =
1617 (struct vlan_ethernet_hdr *)xet;
1619 vet->vet_vlan_type = htons(PROT_VLAN);
1620 vet->vet_tag = htons((0 << 5) | (myvlanid & VLAN_IDMASK));
1621 vet->vet_type = htons(prot);
1622 return VLAN_ETHER_HDR_SIZE;
1626 int net_update_ether(struct ethernet_hdr *et, uchar *addr, uint prot)
1628 ushort protlen;
1630 memcpy(et->et_dest, addr, 6);
1631 memcpy(et->et_src, net_ethaddr, 6);
1632 protlen = ntohs(et->et_protlen);
1633 if (protlen == PROT_VLAN) {
1634 struct vlan_ethernet_hdr *vet =
1635 (struct vlan_ethernet_hdr *)et;
1636 vet->vet_type = htons(prot);
1637 return VLAN_ETHER_HDR_SIZE;
1638 } else if (protlen > 1514) {
1639 et->et_protlen = htons(prot);
1640 return ETHER_HDR_SIZE;
1641 } else {
1642 /* 802.2 + SNAP */
1643 struct e802_hdr *et802 = (struct e802_hdr *)et;
1644 et802->et_prot = htons(prot);
1645 return E802_HDR_SIZE;
1649 void net_set_ip_header(uchar *pkt, struct in_addr dest, struct in_addr source,
1650 u16 pkt_len, u8 proto)
1652 struct ip_udp_hdr *ip = (struct ip_udp_hdr *)pkt;
1655 * Construct an IP header.
1657 /* IP_HDR_SIZE / 4 (not including UDP) */
1658 ip->ip_hl_v = 0x45;
1659 ip->ip_tos = 0;
1660 ip->ip_len = htons(pkt_len);
1661 ip->ip_p = proto;
1662 ip->ip_id = htons(net_ip_id++);
1663 ip->ip_off = htons(IP_FLAGS_DFRAG); /* Don't fragment */
1664 ip->ip_ttl = 255;
1665 ip->ip_sum = 0;
1666 /* already in network byte order */
1667 net_copy_ip((void *)&ip->ip_src, &source);
1668 /* already in network byte order */
1669 net_copy_ip((void *)&ip->ip_dst, &dest);
1671 ip->ip_sum = compute_ip_checksum(ip, IP_HDR_SIZE);
1674 void net_set_udp_header(uchar *pkt, struct in_addr dest, int dport, int sport,
1675 int len)
1677 struct ip_udp_hdr *ip = (struct ip_udp_hdr *)pkt;
1680 * If the data is an odd number of bytes, zero the
1681 * byte after the last byte so that the checksum
1682 * will work.
1684 if (len & 1)
1685 pkt[IP_UDP_HDR_SIZE + len] = 0;
1687 net_set_ip_header(pkt, dest, net_ip, IP_UDP_HDR_SIZE + len,
1688 IPPROTO_UDP);
1690 ip->udp_src = htons(sport);
1691 ip->udp_dst = htons(dport);
1692 ip->udp_len = htons(UDP_HDR_SIZE + len);
1693 ip->udp_xsum = 0;
1696 int is_serverip_in_cmd(void)
1698 return !!strchr(net_boot_file_name, ':');
1701 int net_parse_bootfile(struct in_addr *ipaddr, char *filename, int max_len)
1703 char *colon;
1704 struct in_addr ip;
1705 ip.s_addr = 0;
1707 if (net_boot_file_name[0] == '\0')
1708 return 0;
1710 colon = strchr(net_boot_file_name, ':');
1711 if (colon) {
1712 ip = string_to_ip(net_boot_file_name);
1713 if (ipaddr && ip.s_addr)
1714 *ipaddr = ip;
1716 if (ip.s_addr) {
1717 strncpy(filename, colon + 1, max_len);
1718 } else {
1719 strncpy(filename, net_boot_file_name, max_len);
1721 filename[max_len - 1] = '\0';
1723 return 1;
1726 void ip_to_string(struct in_addr x, char *s)
1728 x.s_addr = ntohl(x.s_addr);
1729 sprintf(s, "%d.%d.%d.%d",
1730 (int) ((x.s_addr >> 24) & 0xff),
1731 (int) ((x.s_addr >> 16) & 0xff),
1732 (int) ((x.s_addr >> 8) & 0xff),
1733 (int) ((x.s_addr >> 0) & 0xff)
1737 void vlan_to_string(ushort x, char *s)
1739 x = ntohs(x);
1741 if (x == (ushort)-1)
1742 x = VLAN_NONE;
1744 if (x == VLAN_NONE)
1745 strcpy(s, "none");
1746 else
1747 sprintf(s, "%d", x & VLAN_IDMASK);
1750 ushort string_to_vlan(const char *s)
1752 ushort id;
1754 if (s == NULL)
1755 return htons(VLAN_NONE);
1757 if (*s < '0' || *s > '9')
1758 id = VLAN_NONE;
1759 else
1760 id = (ushort)dectoul(s, NULL);
1762 return htons(id);
1765 ushort env_get_vlan(char *var)
1767 return string_to_vlan(env_get(var));