1 // SPDX-License-Identifier: GPL-2.0
3 * Copied from Linux Monitor (LiMon) - Networking.
5 * Copyright 1994 - 2000 Neil Russell.
7 * Copyright 2000 Roland Borde
8 * Copyright 2000 Paolo Scaffardi
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:
21 * Prerequisites: - own ethernet address
22 * We want: - own IP address
23 * - TFTP server IP address
29 * Prerequisites: - own ethernet address
30 * We want: - own IP address
35 * Prerequisites: - own ethernet address
36 * We want: - own IP address
37 * - TFTP server IP address
42 * Prerequisites: - own ethernet address
44 * - TFTP server IP address
45 * We want: - TFTP server ethernet address
50 * Prerequisites: - own ethernet address
51 * We want: - IP, Netmask, ServerIP, Gateway IP
52 * - bootfilename, lease time
57 * Prerequisites: - own ethernet 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
68 * Prerequisites: - own ethernet 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
78 * Prerequisites: - own ethernet address
79 * We want: - magic packet or timeout
85 #include <bootstage.h>
89 #include <env_internal.h>
96 #include <net/fastboot.h>
99 #if defined(CONFIG_CMD_PCAP)
100 #include <net/pcap.h>
103 #if defined(CONFIG_LED_STATUS)
105 #include <status_led.h>
107 #include <watchdog.h>
108 #include <linux/compiler.h>
109 #include <test/test.h>
113 #if defined(CONFIG_CMD_DNS)
116 #include "link_local.h"
120 #if defined(CONFIG_CMD_WOL)
124 #include <net/wget.h>
126 /** BOOTP EXTENTIONS **/
128 /* Our subnet mask (0=unknown) */
129 struct in_addr net_netmask;
130 /* Our gateways IP address */
131 struct in_addr net_gateway;
132 /* Our DNS IP address */
133 struct in_addr net_dns_server;
134 #if defined(CONFIG_BOOTP_DNS2)
135 /* Our 2nd DNS IP address */
136 struct in_addr net_dns_server2;
139 /** END OF BOOTP EXTENTIONS **/
141 /* Our ethernet address */
143 /* Boot server enet address */
144 u8 net_server_ethaddr[6];
145 /* Our IP addr (0 = unknown) */
146 struct in_addr net_ip;
147 /* Server IP addr (0 = unknown) */
148 struct in_addr net_server_ip;
149 /* Current receive packet */
150 uchar *net_rx_packet;
151 /* Current rx packet length */
152 int net_rx_packet_len;
154 static unsigned net_ip_id;
155 /* Ethernet bcast address */
156 const u8 net_bcast_ethaddr[6] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
157 const u8 net_null_ethaddr[6];
158 #if defined(CONFIG_API) || defined(CONFIG_EFI_LOADER)
159 void (*push_packet)(void *, int len) = 0;
161 /* Network loop state */
162 enum net_loop_state net_state;
163 /* Tried all network devices */
164 int net_restart_wrap;
165 /* Network loop restarted */
166 static int net_restarted;
167 /* At least one device configured */
168 static int net_dev_exists;
170 /* XXX in both little & big endian machines 0xFFFF == ntohs(-1) */
171 /* default is without VLAN */
172 ushort net_our_vlan = 0xFFFF;
174 ushort net_native_vlan = 0xFFFF;
177 char net_boot_file_name[1024];
178 /* Indicates whether the file name was specified on the command line */
179 bool net_boot_file_name_explicit;
180 /* The actual transferred size of the bootfile (in bytes) */
181 u32 net_boot_file_size;
182 /* Boot file size in blocks as reported by the DHCP server */
183 u32 net_boot_file_expected_size_in_blocks;
185 static uchar net_pkt_buf[(PKTBUFSRX+1) * PKTSIZE_ALIGN + PKTALIGN];
186 /* Receive packets */
187 uchar *net_rx_packets[PKTBUFSRX];
188 /* Current UDP RX packet handler */
189 static rxhand_f *udp_packet_handler;
190 /* Current ARP RX packet handler */
191 static rxhand_f *arp_packet_handler;
192 #ifdef CONFIG_CMD_TFTPPUT
193 /* Current ICMP rx handler */
194 static rxhand_icmp_f *packet_icmp_handler;
196 /* Current timeout handler */
197 static thand_f *time_handler;
198 /* Time base value */
199 static ulong time_start;
200 /* Current timeout value */
201 static ulong time_delta;
202 /* THE transmit packet */
203 uchar *net_tx_packet;
205 static int net_check_prereq(enum proto_t protocol);
207 static int net_try_count;
209 int __maybe_unused net_busy_flag;
211 /**********************************************************************/
213 static int on_ipaddr(const char *name, const char *value, enum env_op op,
216 if (flags & H_PROGRAMMATIC)
219 net_ip = string_to_ip(value);
223 U_BOOT_ENV_CALLBACK(ipaddr, on_ipaddr);
225 static int on_gatewayip(const char *name, const char *value, enum env_op op,
228 if (flags & H_PROGRAMMATIC)
231 net_gateway = string_to_ip(value);
235 U_BOOT_ENV_CALLBACK(gatewayip, on_gatewayip);
237 static int on_netmask(const char *name, const char *value, enum env_op op,
240 if (flags & H_PROGRAMMATIC)
243 net_netmask = string_to_ip(value);
247 U_BOOT_ENV_CALLBACK(netmask, on_netmask);
249 static int on_serverip(const char *name, const char *value, enum env_op op,
252 if (flags & H_PROGRAMMATIC)
255 net_server_ip = string_to_ip(value);
259 U_BOOT_ENV_CALLBACK(serverip, on_serverip);
261 static int on_nvlan(const char *name, const char *value, enum env_op op,
264 if (flags & H_PROGRAMMATIC)
267 net_native_vlan = string_to_vlan(value);
271 U_BOOT_ENV_CALLBACK(nvlan, on_nvlan);
273 static int on_vlan(const char *name, const char *value, enum env_op op,
276 if (flags & H_PROGRAMMATIC)
279 net_our_vlan = string_to_vlan(value);
283 U_BOOT_ENV_CALLBACK(vlan, on_vlan);
285 #if defined(CONFIG_CMD_DNS)
286 static int on_dnsip(const char *name, const char *value, enum env_op op,
289 if (flags & H_PROGRAMMATIC)
292 net_dns_server = string_to_ip(value);
296 U_BOOT_ENV_CALLBACK(dnsip, on_dnsip);
300 * Check if autoload is enabled. If so, use either NFS or TFTP to download
303 void net_auto_load(void)
305 #if defined(CONFIG_CMD_NFS) && !defined(CONFIG_SPL_BUILD)
306 const char *s = env_get("autoload");
308 if (s != NULL && strcmp(s, "NFS") == 0) {
309 if (net_check_prereq(NFS)) {
310 /* We aren't expecting to get a serverip, so just accept the assigned IP */
311 if (IS_ENABLED(CONFIG_BOOTP_SERVERIP)) {
312 net_set_state(NETLOOP_SUCCESS);
314 printf("Cannot autoload with NFS\n");
315 net_set_state(NETLOOP_FAIL);
320 * Use NFS to load the bootfile.
326 if (env_get_yesno("autoload") == 0) {
328 * Just use BOOTP/RARP to configure system;
329 * Do not use TFTP to load the bootfile.
331 net_set_state(NETLOOP_SUCCESS);
334 if (net_check_prereq(TFTPGET)) {
335 /* We aren't expecting to get a serverip, so just accept the assigned IP */
336 if (IS_ENABLED(CONFIG_BOOTP_SERVERIP)) {
337 net_set_state(NETLOOP_SUCCESS);
339 printf("Cannot autoload with TFTPGET\n");
340 net_set_state(NETLOOP_FAIL);
347 static int net_init_loop(void)
350 memcpy(net_ethaddr, eth_get_ethaddr(), 6);
352 if (IS_ENABLED(CONFIG_IPV6)) {
353 ip6_make_lladdr(&net_link_local_ip6, net_ethaddr);
354 if (!memcmp(&net_ip6, &net_null_addr_ip6,
355 sizeof(struct in6_addr)))
356 memcpy(&net_ip6, &net_link_local_ip6,
357 sizeof(struct in6_addr));
362 * Not ideal, but there's no way to get the actual error, and I
363 * don't feel like fixing all the users of eth_get_dev to deal
371 static void net_clear_handlers(void)
373 net_set_udp_handler(NULL);
374 net_set_arp_handler(NULL);
375 net_set_timeout_handler(0, NULL);
378 static void net_cleanup_loop(void)
380 net_clear_handlers();
385 static int first_call = 1;
389 * Setup packet buffers, aligned correctly.
393 net_tx_packet = &net_pkt_buf[0] + (PKTALIGN - 1);
394 net_tx_packet -= (ulong)net_tx_packet % PKTALIGN;
395 for (i = 0; i < PKTBUFSRX; i++) {
396 net_rx_packets[i] = net_tx_packet +
397 (i + 1) * PKTSIZE_ALIGN;
401 net_clear_handlers();
403 /* Only need to setup buffer pointers once. */
405 if (IS_ENABLED(CONFIG_PROT_TCP))
406 tcp_set_tcp_state(TCP_CLOSED);
409 return net_init_loop();
412 /**********************************************************************/
414 * Main network processing loop.
417 int net_loop(enum proto_t protocol)
420 enum net_loop_state prev_net_state = net_state;
422 #if defined(CONFIG_CMD_PING)
423 if (protocol != PING)
424 net_ping_ip.s_addr = 0;
429 debug_cond(DEBUG_INT_STATE, "--- net_loop Entry\n");
431 #ifdef CONFIG_PHY_NCSI
432 if (phy_interface_is_ncsi() && protocol != NCSI && !ncsi_active()) {
433 printf("%s: configuring NCSI first\n", __func__);
434 if (net_loop(NCSI) < 0)
436 eth_init_state_only();
441 bootstage_mark_name(BOOTSTAGE_ID_ETH_START, "eth_start");
443 if (eth_is_on_demand_init()) {
452 eth_init_state_only();
456 #ifdef CONFIG_USB_KEYBOARD
459 net_set_state(NETLOOP_CONTINUE);
462 * Start the ball rolling with the given start function. From
463 * here on, this code is a state machine driven by received
464 * packets and timer events.
466 debug_cond(DEBUG_INT_STATE, "--- net_loop Init\n");
469 if (!test_eth_enabled())
472 switch (net_check_prereq(protocol)) {
474 /* network not configured */
476 net_set_state(prev_net_state);
480 /* network device not configured */
485 net_boot_file_size = 0;
487 #ifdef CONFIG_CMD_TFTPBOOT
489 #ifdef CONFIG_CMD_TFTPPUT
492 /* always use ARP to get server ethernet address */
493 tftp_start(protocol);
496 #ifdef CONFIG_CMD_TFTPSRV
501 #ifdef CONFIG_UDP_FUNCTION_FASTBOOT
503 fastboot_start_server();
506 #if defined(CONFIG_CMD_DHCP)
510 dhcp_request(); /* Basically same as BOOTP */
513 #if defined(CONFIG_CMD_BOOTP)
520 #if defined(CONFIG_CMD_RARP)
527 #if defined(CONFIG_CMD_PING)
532 #if defined(CONFIG_CMD_PING6)
537 #if defined(CONFIG_CMD_NFS) && !defined(CONFIG_SPL_BUILD)
542 #if defined(CONFIG_CMD_WGET)
547 #if defined(CONFIG_CMD_CDP)
552 #if defined(CONFIG_NETCONSOLE) && !defined(CONFIG_SPL_BUILD)
557 #if defined(CONFIG_CMD_DNS)
562 #if defined(CONFIG_CMD_LINK_LOCAL)
567 #if defined(CONFIG_CMD_WOL)
572 #if defined(CONFIG_PHY_NCSI)
574 ncsi_probe_packages();
581 if (IS_ENABLED(CONFIG_PROT_UDP) && protocol == UDP)
587 #if defined(CONFIG_MII) || defined(CONFIG_CMD_MII)
588 #if defined(CONFIG_SYS_FAULT_ECHO_LINK_DOWN) && \
589 defined(CONFIG_LED_STATUS) && \
590 defined(CONFIG_LED_STATUS_RED)
592 * Echo the inverted link state to the fault LED.
594 if (miiphy_link(eth_get_dev()->name, CONFIG_SYS_FAULT_MII_ADDR))
595 status_led_set(CONFIG_LED_STATUS_RED, CONFIG_LED_STATUS_OFF);
597 status_led_set(CONFIG_LED_STATUS_RED, CONFIG_LED_STATUS_ON);
598 #endif /* CONFIG_SYS_FAULT_ECHO_LINK_DOWN, ... */
599 #endif /* CONFIG_MII, ... */
600 #ifdef CONFIG_USB_KEYBOARD
605 * Main packet reception loop. Loop receiving packets until
606 * someone sets `net_state' to a state that terminates.
610 if (arp_timeout_check() > 0)
611 time_start = get_timer(0);
613 if (IS_ENABLED(CONFIG_IPV6)) {
614 if (use_ip6 && (ndisc_timeout_check() > 0))
615 time_start = get_timer(0);
619 * Check the ethernet for a new packet. The ethernet
620 * receive routine will process it.
621 * Most drivers return the most recent packet size, but not
622 * errors that may have happened.
627 * Abort if ctrl-c was pressed.
630 /* cancel any ARP that may not have completed */
631 net_arp_wait_packet_ip.s_addr = 0;
635 /* Invalidate the last protocol */
636 eth_set_last_protocol(BOOTP);
639 /* include a debug print as well incase the debug
640 messages are directed to stderr */
641 debug_cond(DEBUG_INT_STATE, "--- net_loop Abort!\n");
647 * Check for a timeout, and run the timeout handler
651 ((get_timer(0) - time_start) > time_delta)) {
654 #if defined(CONFIG_MII) || defined(CONFIG_CMD_MII)
655 #if defined(CONFIG_SYS_FAULT_ECHO_LINK_DOWN) && \
656 defined(CONFIG_LED_STATUS) && \
657 defined(CONFIG_LED_STATUS_RED)
659 * Echo the inverted link state to the fault LED.
661 if (miiphy_link(eth_get_dev()->name,
662 CONFIG_SYS_FAULT_MII_ADDR))
663 status_led_set(CONFIG_LED_STATUS_RED,
664 CONFIG_LED_STATUS_OFF);
666 status_led_set(CONFIG_LED_STATUS_RED,
667 CONFIG_LED_STATUS_ON);
668 #endif /* CONFIG_SYS_FAULT_ECHO_LINK_DOWN, ... */
669 #endif /* CONFIG_MII, ... */
670 debug_cond(DEBUG_INT_STATE, "--- net_loop timeout\n");
672 time_handler = (thand_f *)0;
676 if (net_state == NETLOOP_FAIL)
677 ret = net_start_again();
680 case NETLOOP_RESTART:
684 case NETLOOP_SUCCESS:
686 if (net_boot_file_size > 0) {
687 printf("Bytes transferred = %d (%x hex)\n",
688 net_boot_file_size, net_boot_file_size);
689 env_set_hex("filesize", net_boot_file_size);
690 env_set_hex("fileaddr", image_load_addr);
692 if (protocol != NETCONS && protocol != NCSI)
695 eth_halt_state_only();
697 eth_set_last_protocol(protocol);
699 ret = net_boot_file_size;
700 debug_cond(DEBUG_INT_STATE, "--- net_loop Success!\n");
705 /* Invalidate the last protocol */
706 eth_set_last_protocol(BOOTP);
707 debug_cond(DEBUG_INT_STATE, "--- net_loop Fail!\n");
711 case NETLOOP_CONTINUE:
717 #ifdef CONFIG_USB_KEYBOARD
720 #ifdef CONFIG_CMD_TFTPPUT
721 /* Clear out the handlers */
722 net_set_udp_handler(NULL);
723 net_set_icmp_handler(NULL);
725 net_set_state(prev_net_state);
727 #if defined(CONFIG_CMD_PCAP)
734 /**********************************************************************/
736 static void start_again_timeout_handler(void)
738 net_set_state(NETLOOP_RESTART);
741 int net_start_again(void)
744 int retry_forever = 0;
745 unsigned long retrycnt = 0;
748 nretry = env_get("netretry");
750 if (!strcmp(nretry, "yes"))
752 else if (!strcmp(nretry, "no"))
754 else if (!strcmp(nretry, "once"))
757 retrycnt = simple_strtoul(nretry, NULL, 0);
763 if ((!retry_forever) && (net_try_count > retrycnt)) {
765 net_set_state(NETLOOP_FAIL);
767 * We don't provide a way for the protocol to return an error,
768 * but this is almost always the reason.
776 #if !defined(CONFIG_NET_DO_NOT_TRY_ANOTHER)
777 eth_try_another(!net_restarted);
780 if (net_restart_wrap) {
781 net_restart_wrap = 0;
782 if (net_dev_exists) {
783 net_set_timeout_handler(10000UL,
784 start_again_timeout_handler);
785 net_set_udp_handler(NULL);
787 net_set_state(NETLOOP_FAIL);
790 net_set_state(NETLOOP_RESTART);
795 /**********************************************************************/
800 static void dummy_handler(uchar *pkt, unsigned dport,
801 struct in_addr sip, unsigned sport,
806 rxhand_f *net_get_udp_handler(void)
808 return udp_packet_handler;
811 void net_set_udp_handler(rxhand_f *f)
813 debug_cond(DEBUG_INT_STATE, "--- net_loop UDP handler set (%p)\n", f);
815 udp_packet_handler = dummy_handler;
817 udp_packet_handler = f;
820 rxhand_f *net_get_arp_handler(void)
822 return arp_packet_handler;
825 void net_set_arp_handler(rxhand_f *f)
827 debug_cond(DEBUG_INT_STATE, "--- net_loop ARP handler set (%p)\n", f);
829 arp_packet_handler = dummy_handler;
831 arp_packet_handler = f;
834 #ifdef CONFIG_CMD_TFTPPUT
835 void net_set_icmp_handler(rxhand_icmp_f *f)
837 packet_icmp_handler = f;
841 void net_set_timeout_handler(ulong iv, thand_f *f)
844 debug_cond(DEBUG_INT_STATE,
845 "--- net_loop timeout handler cancelled\n");
846 time_handler = (thand_f *)0;
848 debug_cond(DEBUG_INT_STATE,
849 "--- net_loop timeout handler set (%p)\n", f);
851 time_start = get_timer(0);
852 time_delta = iv * CONFIG_SYS_HZ / 1000;
856 uchar *net_get_async_tx_pkt_buf(void)
858 if (arp_is_waiting())
859 return arp_tx_packet; /* If we are waiting, we already sent */
861 return net_tx_packet;
864 int net_send_udp_packet(uchar *ether, struct in_addr dest, int dport, int sport,
867 return net_send_ip_packet(ether, dest, dport, sport, payload_len,
868 IPPROTO_UDP, 0, 0, 0);
871 #if defined(CONFIG_PROT_TCP)
872 int net_send_tcp_packet(int payload_len, int dport, int sport, u8 action,
873 u32 tcp_seq_num, u32 tcp_ack_num)
875 return net_send_ip_packet(net_server_ethaddr, net_server_ip, dport,
876 sport, payload_len, IPPROTO_TCP, action,
877 tcp_seq_num, tcp_ack_num);
881 int net_send_ip_packet(uchar *ether, struct in_addr dest, int dport, int sport,
882 int payload_len, int proto, u8 action, u32 tcp_seq_num,
889 /* make sure the net_tx_packet is initialized (net_init() was called) */
890 assert(net_tx_packet != NULL);
891 if (net_tx_packet == NULL)
894 /* convert to new style broadcast */
895 if (dest.s_addr == 0)
896 dest.s_addr = 0xFFFFFFFF;
898 /* if broadcast, make the ether address a broadcast and don't do ARP */
899 if (dest.s_addr == 0xFFFFFFFF)
900 ether = (uchar *)net_bcast_ethaddr;
902 pkt = (uchar *)net_tx_packet;
904 eth_hdr_size = net_set_ether(pkt, ether, PROT_IP);
908 net_set_udp_header(pkt + eth_hdr_size, dest, dport, sport,
910 pkt_hdr_size = eth_hdr_size + IP_UDP_HDR_SIZE;
912 #if defined(CONFIG_PROT_TCP)
914 pkt_hdr_size = eth_hdr_size
915 + tcp_set_tcp_header(pkt + eth_hdr_size, dport, sport,
916 payload_len, action, tcp_seq_num,
924 /* if MAC address was not discovered yet, do an ARP request */
925 if (memcmp(ether, net_null_ethaddr, 6) == 0) {
926 debug_cond(DEBUG_DEV_PKT, "sending ARP for %pI4\n", &dest);
928 /* save the ip and eth addr for the packet to send after arp */
929 net_arp_wait_packet_ip = dest;
930 arp_wait_packet_ethaddr = ether;
932 /* size of the waiting packet */
933 arp_wait_tx_packet_size = pkt_hdr_size + payload_len;
935 /* and do the ARP request */
937 arp_wait_timer_start = get_timer(0);
939 return 1; /* waiting */
941 debug_cond(DEBUG_DEV_PKT, "sending UDP to %pI4/%pM\n",
943 net_send_packet(net_tx_packet, pkt_hdr_size + payload_len);
944 return 0; /* transmitted */
948 #ifdef CONFIG_IP_DEFRAG
950 * This function collects fragments in a single packet, according
951 * to the algorithm in RFC815. It returns NULL or the pointer to
952 * a complete packet, in static storage
954 #define IP_PKTSIZE (CONFIG_NET_MAXDEFRAG)
956 #define IP_MAXUDP (IP_PKTSIZE - IP_HDR_SIZE)
959 * this is the packet being assembled, either data or frag control.
960 * Fragments go by 8 bytes, so this union must be 8 bytes long
963 /* first_byte is address of this structure */
964 u16 last_byte; /* last byte in this hole + 1 (begin of next hole) */
965 u16 next_hole; /* index of next (in 8-b blocks), 0 == none */
966 u16 prev_hole; /* index of prev, 0 == none */
970 static struct ip_udp_hdr *__net_defragment(struct ip_udp_hdr *ip, int *lenp)
972 static uchar pkt_buff[IP_PKTSIZE] __aligned(PKTALIGN);
973 static u16 first_hole, total_len;
974 struct hole *payload, *thisfrag, *h, *newh;
975 struct ip_udp_hdr *localip = (struct ip_udp_hdr *)pkt_buff;
976 uchar *indata = (uchar *)ip;
977 int offset8, start, len, done = 0;
978 u16 ip_off = ntohs(ip->ip_off);
981 * Calling code already rejected <, but we don't have to deal
982 * with an IP fragment with no payload.
984 if (ntohs(ip->ip_len) <= IP_HDR_SIZE)
987 /* payload starts after IP header, this fragment is in there */
988 payload = (struct hole *)(pkt_buff + IP_HDR_SIZE);
989 offset8 = (ip_off & IP_OFFS);
990 thisfrag = payload + offset8;
992 len = ntohs(ip->ip_len) - IP_HDR_SIZE;
994 /* All but last fragment must have a multiple-of-8 payload. */
995 if ((len & 7) && (ip_off & IP_FLAGS_MFRAG))
998 if (start + len > IP_MAXUDP) /* fragment extends too far */
1001 if (!total_len || localip->ip_id != ip->ip_id) {
1002 /* new (or different) packet, reset structs */
1004 payload[0].last_byte = ~0;
1005 payload[0].next_hole = 0;
1006 payload[0].prev_hole = 0;
1008 /* any IP header will work, copy the first we received */
1009 memcpy(localip, ip, IP_HDR_SIZE);
1013 * What follows is the reassembly algorithm. We use the payload
1014 * array as a linked list of hole descriptors, as each hole starts
1015 * at a multiple of 8 bytes. However, last byte can be whatever value,
1016 * so it is represented as byte count, not as 8-byte blocks.
1019 h = payload + first_hole;
1020 while (h->last_byte < start) {
1021 if (!h->next_hole) {
1022 /* no hole that far away */
1025 h = payload + h->next_hole;
1028 /* last fragment may be 1..7 bytes, the "+7" forces acceptance */
1029 if (offset8 + ((len + 7) / 8) <= h - payload) {
1030 /* no overlap with holes (dup fragment?) */
1034 if (!(ip_off & IP_FLAGS_MFRAG)) {
1035 /* no more fragmentss: truncate this (last) hole */
1036 total_len = start + len;
1037 h->last_byte = start + len;
1041 * There is some overlap: fix the hole list. This code deals
1042 * with a fragment that overlaps with two different holes
1043 * (thus being a superset of a previously-received fragment)
1044 * by only using the part of the fragment that fits in the
1047 if (h->last_byte < start + len)
1048 len = h->last_byte - start;
1050 if ((h >= thisfrag) && (h->last_byte <= start + len)) {
1051 /* complete overlap with hole: remove hole */
1052 if (!h->prev_hole && !h->next_hole) {
1053 /* last remaining hole */
1055 } else if (!h->prev_hole) {
1057 first_hole = h->next_hole;
1058 payload[h->next_hole].prev_hole = 0;
1059 } else if (!h->next_hole) {
1061 payload[h->prev_hole].next_hole = 0;
1063 /* in the middle of the list */
1064 payload[h->next_hole].prev_hole = h->prev_hole;
1065 payload[h->prev_hole].next_hole = h->next_hole;
1068 } else if (h->last_byte <= start + len) {
1069 /* overlaps with final part of the hole: shorten this hole */
1070 h->last_byte = start;
1072 } else if (h >= thisfrag) {
1073 /* overlaps with initial part of the hole: move this hole */
1074 newh = thisfrag + (len / 8);
1078 payload[h->next_hole].prev_hole = (h - payload);
1080 payload[h->prev_hole].next_hole = (h - payload);
1082 first_hole = (h - payload);
1085 /* fragment sits in the middle: split the hole */
1086 newh = thisfrag + (len / 8);
1088 h->last_byte = start;
1089 h->next_hole = (newh - payload);
1090 newh->prev_hole = (h - payload);
1091 if (newh->next_hole)
1092 payload[newh->next_hole].prev_hole = (newh - payload);
1095 /* finally copy this fragment and possibly return whole packet */
1096 memcpy((uchar *)thisfrag, indata + IP_HDR_SIZE, len);
1100 *lenp = total_len + IP_HDR_SIZE;
1101 localip->ip_len = htons(*lenp);
1105 static inline struct ip_udp_hdr *net_defragment(struct ip_udp_hdr *ip,
1108 u16 ip_off = ntohs(ip->ip_off);
1109 if (!(ip_off & (IP_OFFS | IP_FLAGS_MFRAG)))
1110 return ip; /* not a fragment */
1111 return __net_defragment(ip, lenp);
1114 #else /* !CONFIG_IP_DEFRAG */
1116 static inline struct ip_udp_hdr *net_defragment(struct ip_udp_hdr *ip,
1119 u16 ip_off = ntohs(ip->ip_off);
1120 if (!(ip_off & (IP_OFFS | IP_FLAGS_MFRAG)))
1121 return ip; /* not a fragment */
1127 * Receive an ICMP packet. We deal with REDIRECT and PING here, and silently
1130 * @parma ip IP packet containing the ICMP
1132 static void receive_icmp(struct ip_udp_hdr *ip, int len,
1133 struct in_addr src_ip, struct ethernet_hdr *et)
1135 struct icmp_hdr *icmph = (struct icmp_hdr *)&ip->udp_src;
1137 switch (icmph->type) {
1139 if (icmph->code != ICMP_REDIR_HOST)
1141 printf(" ICMP Host Redirect to %pI4 ",
1142 &icmph->un.gateway);
1145 #if defined(CONFIG_CMD_PING)
1146 ping_receive(et, ip, len);
1148 #ifdef CONFIG_CMD_TFTPPUT
1149 if (packet_icmp_handler)
1150 packet_icmp_handler(icmph->type, icmph->code,
1151 ntohs(ip->udp_dst), src_ip,
1152 ntohs(ip->udp_src), icmph->un.data,
1153 ntohs(ip->udp_len));
1159 void net_process_received_packet(uchar *in_packet, int len)
1161 struct ethernet_hdr *et;
1162 struct ip_udp_hdr *ip;
1163 struct in_addr dst_ip;
1164 struct in_addr src_ip;
1166 #if defined(CONFIG_CMD_CDP)
1169 ushort cti = 0, vlanid = VLAN_NONE, myvlanid, mynvlanid;
1171 debug_cond(DEBUG_NET_PKT, "packet received\n");
1173 #if defined(CONFIG_CMD_PCAP)
1174 pcap_post(in_packet, len, false);
1176 net_rx_packet = in_packet;
1177 net_rx_packet_len = len;
1178 et = (struct ethernet_hdr *)in_packet;
1180 /* too small packet? */
1181 if (len < ETHER_HDR_SIZE)
1184 #if defined(CONFIG_API) || defined(CONFIG_EFI_LOADER)
1186 (*push_packet)(in_packet, len);
1191 #if defined(CONFIG_CMD_CDP)
1192 /* keep track if packet is CDP */
1193 iscdp = is_cdp_packet(et->et_dest);
1196 myvlanid = ntohs(net_our_vlan);
1197 if (myvlanid == (ushort)-1)
1198 myvlanid = VLAN_NONE;
1199 mynvlanid = ntohs(net_native_vlan);
1200 if (mynvlanid == (ushort)-1)
1201 mynvlanid = VLAN_NONE;
1203 eth_proto = ntohs(et->et_protlen);
1205 if (eth_proto < 1514) {
1206 struct e802_hdr *et802 = (struct e802_hdr *)et;
1208 * Got a 802.2 packet. Check the other protocol field.
1209 * XXX VLAN over 802.2+SNAP not implemented!
1211 eth_proto = ntohs(et802->et_prot);
1213 ip = (struct ip_udp_hdr *)(in_packet + E802_HDR_SIZE);
1214 len -= E802_HDR_SIZE;
1216 } else if (eth_proto != PROT_VLAN) { /* normal packet */
1217 ip = (struct ip_udp_hdr *)(in_packet + ETHER_HDR_SIZE);
1218 len -= ETHER_HDR_SIZE;
1220 } else { /* VLAN packet */
1221 struct vlan_ethernet_hdr *vet =
1222 (struct vlan_ethernet_hdr *)et;
1224 debug_cond(DEBUG_NET_PKT, "VLAN packet received\n");
1226 /* too small packet? */
1227 if (len < VLAN_ETHER_HDR_SIZE)
1230 /* if no VLAN active */
1231 if ((ntohs(net_our_vlan) & VLAN_IDMASK) == VLAN_NONE
1232 #if defined(CONFIG_CMD_CDP)
1238 cti = ntohs(vet->vet_tag);
1239 vlanid = cti & VLAN_IDMASK;
1240 eth_proto = ntohs(vet->vet_type);
1242 ip = (struct ip_udp_hdr *)(in_packet + VLAN_ETHER_HDR_SIZE);
1243 len -= VLAN_ETHER_HDR_SIZE;
1246 debug_cond(DEBUG_NET_PKT, "Receive from protocol 0x%x\n", eth_proto);
1248 #if defined(CONFIG_CMD_CDP)
1250 cdp_receive((uchar *)ip, len);
1255 if ((myvlanid & VLAN_IDMASK) != VLAN_NONE) {
1256 if (vlanid == VLAN_NONE)
1257 vlanid = (mynvlanid & VLAN_IDMASK);
1259 if (vlanid != (myvlanid & VLAN_IDMASK))
1263 switch (eth_proto) {
1265 arp_receive(et, ip, len);
1268 #ifdef CONFIG_CMD_RARP
1270 rarp_receive(ip, len);
1273 #if IS_ENABLED(CONFIG_IPV6)
1275 net_ip6_handler(et, (struct ip6_hdr *)ip, len);
1279 debug_cond(DEBUG_NET_PKT, "Got IP\n");
1280 /* Before we start poking the header, make sure it is there */
1281 if (len < IP_HDR_SIZE) {
1282 debug("len bad %d < %lu\n", len,
1283 (ulong)IP_HDR_SIZE);
1286 /* Check the packet length */
1287 if (len < ntohs(ip->ip_len)) {
1288 debug("len bad %d < %d\n", len, ntohs(ip->ip_len));
1291 len = ntohs(ip->ip_len);
1292 if (len < IP_HDR_SIZE) {
1293 debug("bad ip->ip_len %d < %d\n", len, (int)IP_HDR_SIZE);
1296 debug_cond(DEBUG_NET_PKT, "len=%d, v=%02x\n",
1297 len, ip->ip_hl_v & 0xff);
1299 /* Can't deal with anything except IPv4 */
1300 if ((ip->ip_hl_v & 0xf0) != 0x40)
1302 /* Can't deal with IP options (headers != 20 bytes) */
1303 if ((ip->ip_hl_v & 0x0f) != 0x05)
1305 /* Check the Checksum of the header */
1306 if (!ip_checksum_ok((uchar *)ip, IP_HDR_SIZE)) {
1307 debug("checksum bad\n");
1310 /* If it is not for us, ignore it */
1311 dst_ip = net_read_ip(&ip->ip_dst);
1312 if (net_ip.s_addr && dst_ip.s_addr != net_ip.s_addr &&
1313 dst_ip.s_addr != 0xFFFFFFFF) {
1316 /* Read source IP address for later use */
1317 src_ip = net_read_ip(&ip->ip_src);
1319 * The function returns the unchanged packet if it's not
1320 * a fragment, and either the complete packet or NULL if
1321 * it is a fragment (if !CONFIG_IP_DEFRAG, it returns NULL)
1323 ip = net_defragment(ip, &len);
1327 * watch for ICMP host redirects
1329 * There is no real handler code (yet). We just watch
1330 * for ICMP host redirect messages. In case anybody
1331 * sees these messages: please contact me
1333 * necessary fixes :-)
1335 * Note: in all cases where I have seen this so far
1336 * it was a problem with the router configuration,
1337 * for instance when a router was configured in the
1338 * BOOTP reply, but the TFTP server was on the same
1339 * subnet. So this is probably a warning that your
1340 * configuration might be wrong. But I'm not really
1341 * sure if there aren't any other situations.
1344 * we send a tftp packet to a dead connection, or when
1345 * there is no server at the other end.
1347 if (ip->ip_p == IPPROTO_ICMP) {
1348 receive_icmp(ip, len, src_ip, et);
1350 #if defined(CONFIG_PROT_TCP)
1351 } else if (ip->ip_p == IPPROTO_TCP) {
1352 debug_cond(DEBUG_DEV_PKT,
1353 "TCP PH (to=%pI4, from=%pI4, len=%d)\n",
1354 &dst_ip, &src_ip, len);
1356 rxhand_tcp_f((union tcp_build_pkt *)ip, len);
1359 } else if (ip->ip_p != IPPROTO_UDP) { /* Only UDP packets */
1363 if (ntohs(ip->udp_len) < UDP_HDR_SIZE || ntohs(ip->udp_len) > len - IP_HDR_SIZE)
1366 debug_cond(DEBUG_DEV_PKT,
1367 "received UDP (to=%pI4, from=%pI4, len=%d)\n",
1368 &dst_ip, &src_ip, len);
1370 if (IS_ENABLED(CONFIG_UDP_CHECKSUM) && ip->udp_xsum != 0) {
1376 xsum += (ntohs(ip->udp_len));
1377 xsum += (ntohl(ip->ip_src.s_addr) >> 16) & 0x0000ffff;
1378 xsum += (ntohl(ip->ip_src.s_addr) >> 0) & 0x0000ffff;
1379 xsum += (ntohl(ip->ip_dst.s_addr) >> 16) & 0x0000ffff;
1380 xsum += (ntohl(ip->ip_dst.s_addr) >> 0) & 0x0000ffff;
1382 sumlen = ntohs(ip->udp_len);
1383 sumptr = (u8 *)&ip->udp_src;
1385 while (sumlen > 1) {
1386 /* inlined ntohs() to avoid alignment errors */
1387 xsum += (sumptr[0] << 8) + sumptr[1];
1392 xsum += (sumptr[0] << 8) + sumptr[0];
1393 while ((xsum >> 16) != 0) {
1394 xsum = (xsum & 0x0000ffff) +
1395 ((xsum >> 16) & 0x0000ffff);
1397 if ((xsum != 0x00000000) && (xsum != 0x0000ffff)) {
1398 printf(" UDP wrong checksum %08lx %08x\n",
1399 xsum, ntohs(ip->udp_xsum));
1404 #if defined(CONFIG_NETCONSOLE) && !defined(CONFIG_SPL_BUILD)
1405 nc_input_packet((uchar *)ip + IP_UDP_HDR_SIZE,
1409 ntohs(ip->udp_len) - UDP_HDR_SIZE);
1412 * IP header OK. Pass the packet to the current handler.
1414 (*udp_packet_handler)((uchar *)ip + IP_UDP_HDR_SIZE,
1418 ntohs(ip->udp_len) - UDP_HDR_SIZE);
1420 #ifdef CONFIG_CMD_WOL
1422 wol_receive(ip, len);
1425 #ifdef CONFIG_PHY_NCSI
1427 ncsi_receive(et, ip, len);
1433 /**********************************************************************/
1435 static int net_check_prereq(enum proto_t protocol)
1439 #if defined(CONFIG_CMD_PING)
1441 if (net_ping_ip.s_addr == 0) {
1442 puts("*** ERROR: ping address not given\n");
1447 #if defined(CONFIG_CMD_PING6)
1449 if (ip6_is_unspecified_addr(&net_ping_ip6)) {
1450 puts("*** ERROR: ping address not given\n");
1455 #if defined(CONFIG_CMD_DNS)
1457 if (net_dns_server.s_addr == 0) {
1458 puts("*** ERROR: DNS server address not given\n");
1463 #if defined(CONFIG_PROT_UDP)
1470 #if defined(CONFIG_CMD_NFS)
1476 if (IS_ENABLED(CONFIG_IPV6) && use_ip6) {
1477 if (!memcmp(&net_server_ip6, &net_null_addr_ip6,
1478 sizeof(struct in6_addr)) &&
1479 !strchr(net_boot_file_name, '[')) {
1480 puts("*** ERROR: `serverip6' not set\n");
1483 } else if (net_server_ip.s_addr == 0 && !is_serverip_in_cmd()) {
1484 puts("*** ERROR: `serverip' not set\n");
1487 #if defined(CONFIG_CMD_PING) || \
1488 defined(CONFIG_CMD_DNS) || defined(CONFIG_PROT_UDP)
1496 if (IS_ENABLED(CONFIG_IPV6) && use_ip6) {
1497 if (!memcmp(&net_link_local_ip6, &net_null_addr_ip6,
1498 sizeof(struct in6_addr))) {
1499 puts("*** ERROR: `ip6addr` not set\n");
1502 } else if (net_ip.s_addr == 0) {
1503 puts("*** ERROR: `ipaddr' not set\n");
1508 #ifdef CONFIG_CMD_RARP
1511 #ifdef CONFIG_PHY_NCSI
1518 if (memcmp(net_ethaddr, "\0\0\0\0\0\0", 6) == 0) {
1519 int num = eth_get_dev_index();
1523 puts("*** ERROR: No ethernet found.\n");
1526 puts("*** ERROR: `ethaddr' not set\n");
1529 printf("*** ERROR: `eth%daddr' not set\n",
1543 /**********************************************************************/
1546 net_eth_hdr_size(void)
1550 myvlanid = ntohs(net_our_vlan);
1551 if (myvlanid == (ushort)-1)
1552 myvlanid = VLAN_NONE;
1554 return ((myvlanid & VLAN_IDMASK) == VLAN_NONE) ? ETHER_HDR_SIZE :
1555 VLAN_ETHER_HDR_SIZE;
1558 int net_set_ether(uchar *xet, const uchar *dest_ethaddr, uint prot)
1560 struct ethernet_hdr *et = (struct ethernet_hdr *)xet;
1563 myvlanid = ntohs(net_our_vlan);
1564 if (myvlanid == (ushort)-1)
1565 myvlanid = VLAN_NONE;
1567 memcpy(et->et_dest, dest_ethaddr, 6);
1568 memcpy(et->et_src, net_ethaddr, 6);
1569 if ((myvlanid & VLAN_IDMASK) == VLAN_NONE) {
1570 et->et_protlen = htons(prot);
1571 return ETHER_HDR_SIZE;
1573 struct vlan_ethernet_hdr *vet =
1574 (struct vlan_ethernet_hdr *)xet;
1576 vet->vet_vlan_type = htons(PROT_VLAN);
1577 vet->vet_tag = htons((0 << 5) | (myvlanid & VLAN_IDMASK));
1578 vet->vet_type = htons(prot);
1579 return VLAN_ETHER_HDR_SIZE;
1583 int net_update_ether(struct ethernet_hdr *et, uchar *addr, uint prot)
1587 memcpy(et->et_dest, addr, 6);
1588 memcpy(et->et_src, net_ethaddr, 6);
1589 protlen = ntohs(et->et_protlen);
1590 if (protlen == PROT_VLAN) {
1591 struct vlan_ethernet_hdr *vet =
1592 (struct vlan_ethernet_hdr *)et;
1593 vet->vet_type = htons(prot);
1594 return VLAN_ETHER_HDR_SIZE;
1595 } else if (protlen > 1514) {
1596 et->et_protlen = htons(prot);
1597 return ETHER_HDR_SIZE;
1600 struct e802_hdr *et802 = (struct e802_hdr *)et;
1601 et802->et_prot = htons(prot);
1602 return E802_HDR_SIZE;
1606 void net_set_ip_header(uchar *pkt, struct in_addr dest, struct in_addr source,
1607 u16 pkt_len, u8 proto)
1609 struct ip_udp_hdr *ip = (struct ip_udp_hdr *)pkt;
1612 * Construct an IP header.
1614 /* IP_HDR_SIZE / 4 (not including UDP) */
1617 ip->ip_len = htons(pkt_len);
1619 ip->ip_id = htons(net_ip_id++);
1620 ip->ip_off = htons(IP_FLAGS_DFRAG); /* Don't fragment */
1623 /* already in network byte order */
1624 net_copy_ip((void *)&ip->ip_src, &source);
1625 /* already in network byte order */
1626 net_copy_ip((void *)&ip->ip_dst, &dest);
1628 ip->ip_sum = compute_ip_checksum(ip, IP_HDR_SIZE);
1631 void net_set_udp_header(uchar *pkt, struct in_addr dest, int dport, int sport,
1634 struct ip_udp_hdr *ip = (struct ip_udp_hdr *)pkt;
1637 * If the data is an odd number of bytes, zero the
1638 * byte after the last byte so that the checksum
1642 pkt[IP_UDP_HDR_SIZE + len] = 0;
1644 net_set_ip_header(pkt, dest, net_ip, IP_UDP_HDR_SIZE + len,
1647 ip->udp_src = htons(sport);
1648 ip->udp_dst = htons(dport);
1649 ip->udp_len = htons(UDP_HDR_SIZE + len);
1653 void copy_filename(char *dst, const char *src, int size)
1655 if (src && *src && (*src == '"')) {
1660 while ((--size > 0) && src && *src && (*src != '"'))
1665 int is_serverip_in_cmd(void)
1667 return !!strchr(net_boot_file_name, ':');
1670 int net_parse_bootfile(struct in_addr *ipaddr, char *filename, int max_len)
1676 if (net_boot_file_name[0] == '\0')
1679 colon = strchr(net_boot_file_name, ':');
1681 ip = string_to_ip(net_boot_file_name);
1682 if (ipaddr && ip.s_addr)
1686 strncpy(filename, colon + 1, max_len);
1688 strncpy(filename, net_boot_file_name, max_len);
1690 filename[max_len - 1] = '\0';
1695 void ip_to_string(struct in_addr x, char *s)
1697 x.s_addr = ntohl(x.s_addr);
1698 sprintf(s, "%d.%d.%d.%d",
1699 (int) ((x.s_addr >> 24) & 0xff),
1700 (int) ((x.s_addr >> 16) & 0xff),
1701 (int) ((x.s_addr >> 8) & 0xff),
1702 (int) ((x.s_addr >> 0) & 0xff)
1706 void vlan_to_string(ushort x, char *s)
1710 if (x == (ushort)-1)
1716 sprintf(s, "%d", x & VLAN_IDMASK);
1719 ushort string_to_vlan(const char *s)
1724 return htons(VLAN_NONE);
1726 if (*s < '0' || *s > '9')
1729 id = (ushort)dectoul(s, NULL);
1734 ushort env_get_vlan(char *var)
1736 return string_to_vlan(env_get(var));