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
83 #include <bootstage.h>
87 #include <env_internal.h>
92 #if defined(CONFIG_LED_STATUS)
98 #if defined(CONFIG_LED_STATUS)
99 #include <status_led.h>
101 #include <watchdog.h>
102 #include <linux/compiler.h>
103 #include <net/fastboot_udp.h>
104 #include <net/fastboot_tcp.h>
105 #include <net/ncsi.h>
106 #if defined(CONFIG_CMD_PCAP)
107 #include <net/pcap.h>
110 #include <net/tftp.h>
112 #include <net/wget.h>
113 #include <test/test.h>
118 #if defined(CONFIG_CMD_DNS)
121 #include "link_local.h"
122 #include "net_rand.h"
126 #if defined(CONFIG_CMD_WOL)
130 /** BOOTP EXTENTIONS **/
132 /* Our subnet mask (0=unknown) */
133 struct in_addr net_netmask;
134 /* Our gateways IP address */
135 struct in_addr net_gateway;
136 /* Our DNS IP address */
137 struct in_addr net_dns_server;
138 #if defined(CONFIG_BOOTP_DNS2)
139 /* Our 2nd DNS IP address */
140 struct in_addr net_dns_server2;
142 /* Indicates whether the pxe path prefix / config file was specified in dhcp option */
143 char *pxelinux_configfile;
145 /** END OF BOOTP EXTENTIONS **/
147 /* Our ethernet address */
149 /* Boot server enet address */
150 u8 net_server_ethaddr[6];
151 /* Our IP addr (0 = unknown) */
152 struct in_addr net_ip;
153 /* Server IP addr (0 = unknown) */
154 struct in_addr net_server_ip;
155 /* Current receive packet */
156 uchar *net_rx_packet;
157 /* Current rx packet length */
158 int net_rx_packet_len;
160 static unsigned net_ip_id;
161 /* Ethernet bcast address */
162 const u8 net_bcast_ethaddr[6] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
163 const u8 net_null_ethaddr[6];
164 #if defined(CONFIG_API) || defined(CONFIG_EFI_LOADER)
165 void (*push_packet)(void *, int len) = 0;
167 /* Network loop state */
168 enum net_loop_state net_state;
169 /* Tried all network devices */
170 int net_restart_wrap;
171 /* Network loop restarted */
172 static int net_restarted;
173 /* At least one device configured */
174 static int net_dev_exists;
176 /* XXX in both little & big endian machines 0xFFFF == ntohs(-1) */
177 /* default is without VLAN */
178 ushort net_our_vlan = 0xFFFF;
180 ushort net_native_vlan = 0xFFFF;
183 char net_boot_file_name[1024];
184 /* Indicates whether the file name was specified on the command line */
185 bool net_boot_file_name_explicit;
186 /* The actual transferred size of the bootfile (in bytes) */
187 u32 net_boot_file_size;
188 /* Boot file size in blocks as reported by the DHCP server */
189 u32 net_boot_file_expected_size_in_blocks;
191 static uchar net_pkt_buf[(PKTBUFSRX+1) * PKTSIZE_ALIGN + PKTALIGN];
192 /* Receive packets */
193 uchar *net_rx_packets[PKTBUFSRX];
194 /* Current UDP RX packet handler */
195 static rxhand_f *udp_packet_handler;
196 /* Current ARP RX packet handler */
197 static rxhand_f *arp_packet_handler;
198 #ifdef CONFIG_CMD_TFTPPUT
199 /* Current ICMP rx handler */
200 static rxhand_icmp_f *packet_icmp_handler;
202 /* Current timeout handler */
203 static thand_f *time_handler;
204 /* Time base value */
205 static ulong time_start;
206 /* Current timeout value */
207 static ulong time_delta;
208 /* THE transmit packet */
209 uchar *net_tx_packet;
211 static int net_check_prereq(enum proto_t protocol);
213 static int net_try_count;
215 int __maybe_unused net_busy_flag;
217 /**********************************************************************/
219 static int on_ipaddr(const char *name, const char *value, enum env_op op,
222 if (flags & H_PROGRAMMATIC)
225 net_ip = string_to_ip(value);
229 U_BOOT_ENV_CALLBACK(ipaddr, on_ipaddr);
231 static int on_gatewayip(const char *name, const char *value, enum env_op op,
234 if (flags & H_PROGRAMMATIC)
237 net_gateway = string_to_ip(value);
241 U_BOOT_ENV_CALLBACK(gatewayip, on_gatewayip);
243 static int on_netmask(const char *name, const char *value, enum env_op op,
246 if (flags & H_PROGRAMMATIC)
249 net_netmask = string_to_ip(value);
253 U_BOOT_ENV_CALLBACK(netmask, on_netmask);
255 static int on_serverip(const char *name, const char *value, enum env_op op,
258 if (flags & H_PROGRAMMATIC)
261 net_server_ip = string_to_ip(value);
265 U_BOOT_ENV_CALLBACK(serverip, on_serverip);
267 static int on_nvlan(const char *name, const char *value, enum env_op op,
270 if (flags & H_PROGRAMMATIC)
273 net_native_vlan = string_to_vlan(value);
277 U_BOOT_ENV_CALLBACK(nvlan, on_nvlan);
279 static int on_vlan(const char *name, const char *value, enum env_op op,
282 if (flags & H_PROGRAMMATIC)
285 net_our_vlan = string_to_vlan(value);
289 U_BOOT_ENV_CALLBACK(vlan, on_vlan);
291 #if defined(CONFIG_CMD_DNS)
292 static int on_dnsip(const char *name, const char *value, enum env_op op,
295 if (flags & H_PROGRAMMATIC)
298 net_dns_server = string_to_ip(value);
302 U_BOOT_ENV_CALLBACK(dnsip, on_dnsip);
306 * Check if autoload is enabled. If so, use either NFS or TFTP to download
309 void net_auto_load(void)
311 #if defined(CONFIG_CMD_NFS) && !defined(CONFIG_XPL_BUILD)
312 const char *s = env_get("autoload");
314 if (s != NULL && strcmp(s, "NFS") == 0) {
315 if (net_check_prereq(NFS)) {
316 /* We aren't expecting to get a serverip, so just accept the assigned IP */
317 if (IS_ENABLED(CONFIG_BOOTP_SERVERIP)) {
318 net_set_state(NETLOOP_SUCCESS);
320 printf("Cannot autoload with NFS\n");
321 net_set_state(NETLOOP_FAIL);
326 * Use NFS to load the bootfile.
332 if (env_get_yesno("autoload") == 0) {
334 * Just use BOOTP/RARP to configure system;
335 * Do not use TFTP to load the bootfile.
337 net_set_state(NETLOOP_SUCCESS);
340 if (IS_ENABLED(CONFIG_CMD_TFTPBOOT)) {
341 if (net_check_prereq(TFTPGET)) {
343 * We aren't expecting to get a serverip, so just
344 * accept the assigned IP
346 if (IS_ENABLED(CONFIG_BOOTP_SERVERIP)) {
347 net_set_state(NETLOOP_SUCCESS);
349 printf("Cannot autoload with TFTPGET\n");
350 net_set_state(NETLOOP_FAIL);
358 static int net_init_loop(void)
360 static bool first_call = true;
363 memcpy(net_ethaddr, eth_get_ethaddr(), 6);
365 if (IS_ENABLED(CONFIG_IPV6)) {
366 ip6_make_lladdr(&net_link_local_ip6, net_ethaddr);
367 if (!memcmp(&net_ip6, &net_null_addr_ip6,
368 sizeof(struct in6_addr)))
369 memcpy(&net_ip6, &net_link_local_ip6,
370 sizeof(struct in6_addr));
375 * Not ideal, but there's no way to get the actual error, and I
376 * don't feel like fixing all the users of eth_get_dev to deal
381 if (IS_ENABLED(CONFIG_IPV6_ROUTER_DISCOVERY))
382 if (first_call && use_ip6) {
384 srand_mac(); /* This is for rand used in ip6_send_rs. */
390 static void net_clear_handlers(void)
392 net_set_udp_handler(NULL);
393 net_set_arp_handler(NULL);
394 net_set_timeout_handler(0, NULL);
397 static void net_cleanup_loop(void)
399 net_clear_handlers();
404 static int first_call = 1;
408 * Setup packet buffers, aligned correctly.
412 net_tx_packet = &net_pkt_buf[0] + (PKTALIGN - 1);
413 net_tx_packet -= (ulong)net_tx_packet % PKTALIGN;
414 for (i = 0; i < PKTBUFSRX; i++) {
415 net_rx_packets[i] = net_tx_packet +
416 (i + 1) * PKTSIZE_ALIGN;
420 net_clear_handlers();
422 /* Only need to setup buffer pointers once. */
424 if (IS_ENABLED(CONFIG_PROT_TCP))
428 return net_init_loop();
431 /**********************************************************************/
433 * Main network processing loop.
436 int net_loop(enum proto_t protocol)
439 enum net_loop_state prev_net_state = net_state;
441 #if defined(CONFIG_CMD_PING)
442 if (protocol != PING)
443 net_ping_ip.s_addr = 0;
448 debug_cond(DEBUG_INT_STATE, "--- net_loop Entry\n");
450 #ifdef CONFIG_PHY_NCSI
451 if (phy_interface_is_ncsi() && protocol != NCSI && !ncsi_active()) {
452 printf("%s: configuring NCSI first\n", __func__);
453 if (net_loop(NCSI) < 0)
455 eth_init_state_only();
460 bootstage_mark_name(BOOTSTAGE_ID_ETH_START, "eth_start");
462 if (eth_is_on_demand_init()) {
471 eth_init_state_only();
475 #ifdef CONFIG_USB_KEYBOARD
478 net_set_state(NETLOOP_CONTINUE);
481 * Start the ball rolling with the given start function. From
482 * here on, this code is a state machine driven by received
483 * packets and timer events.
485 debug_cond(DEBUG_INT_STATE, "--- net_loop Init\n");
488 if (!test_eth_enabled())
491 switch (net_check_prereq(protocol)) {
493 /* network not configured */
495 net_set_state(prev_net_state);
499 /* network device not configured */
504 net_boot_file_size = 0;
506 #ifdef CONFIG_CMD_TFTPBOOT
508 #ifdef CONFIG_CMD_TFTPPUT
511 /* always use ARP to get server ethernet address */
512 tftp_start(protocol);
515 #ifdef CONFIG_CMD_TFTPSRV
520 #if CONFIG_IS_ENABLED(UDP_FUNCTION_FASTBOOT)
522 fastboot_udp_start_server();
525 #if CONFIG_IS_ENABLED(TCP_FUNCTION_FASTBOOT)
527 fastboot_tcp_start_server();
530 #if defined(CONFIG_CMD_DHCP)
534 dhcp_request(); /* Basically same as BOOTP */
538 if (IS_ENABLED(CONFIG_CMD_DHCP6))
541 #if defined(CONFIG_CMD_BOOTP)
548 #if defined(CONFIG_CMD_RARP)
555 #if defined(CONFIG_CMD_PING)
560 #if defined(CONFIG_CMD_PING6)
565 #if defined(CONFIG_CMD_NFS) && !defined(CONFIG_XPL_BUILD)
570 #if defined(CONFIG_CMD_WGET)
575 #if defined(CONFIG_CMD_CDP)
580 #if defined(CONFIG_NETCONSOLE) && !defined(CONFIG_XPL_BUILD)
585 #if defined(CONFIG_CMD_DNS)
590 #if defined(CONFIG_CMD_LINK_LOCAL)
595 #if defined(CONFIG_CMD_WOL)
600 #if defined(CONFIG_PHY_NCSI)
602 ncsi_probe_packages();
606 if (IS_ENABLED(CONFIG_IPV6_ROUTER_DISCOVERY))
613 if (IS_ENABLED(CONFIG_PROT_UDP) && protocol == UDP)
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)
624 * Echo the inverted link state to the fault LED.
626 if (miiphy_link(eth_get_dev()->name, CONFIG_SYS_FAULT_MII_ADDR))
627 status_led_set(CONFIG_LED_STATUS_RED, CONFIG_LED_STATUS_OFF);
629 status_led_set(CONFIG_LED_STATUS_RED, CONFIG_LED_STATUS_ON);
630 #endif /* CONFIG_SYS_FAULT_ECHO_LINK_DOWN, ... */
631 #endif /* CONFIG_MII, ... */
632 #ifdef CONFIG_USB_KEYBOARD
637 * Main packet reception loop. Loop receiving packets until
638 * someone sets `net_state' to a state that terminates.
642 if (arp_timeout_check() > 0)
643 time_start = get_timer(0);
645 if (IS_ENABLED(CONFIG_IPV6)) {
646 if (use_ip6 && (ndisc_timeout_check() > 0))
647 time_start = get_timer(0);
651 * Check the ethernet for a new packet. The ethernet
652 * receive routine will process it.
653 * Most drivers return the most recent packet size, but not
654 * errors that may have happened.
657 #if defined(CONFIG_PROT_TCP)
662 * Abort if ctrl-c was pressed.
665 /* cancel any ARP that may not have completed */
666 net_arp_wait_packet_ip.s_addr = 0;
670 /* Invalidate the last protocol */
671 eth_set_last_protocol(BOOTP);
673 /* Turn off activity LED if triggered */
677 /* include a debug print as well incase the debug
678 messages are directed to stderr */
679 debug_cond(DEBUG_INT_STATE, "--- net_loop Abort!\n");
685 * Check for a timeout, and run the timeout handler
689 ((get_timer(0) - time_start) > time_delta)) {
692 #if defined(CONFIG_MII) || defined(CONFIG_CMD_MII)
693 #if defined(CONFIG_SYS_FAULT_ECHO_LINK_DOWN) && \
694 defined(CONFIG_LED_STATUS) && \
695 defined(CONFIG_LED_STATUS_RED)
697 * Echo the inverted link state to the fault LED.
699 if (miiphy_link(eth_get_dev()->name,
700 CONFIG_SYS_FAULT_MII_ADDR))
701 status_led_set(CONFIG_LED_STATUS_RED,
702 CONFIG_LED_STATUS_OFF);
704 status_led_set(CONFIG_LED_STATUS_RED,
705 CONFIG_LED_STATUS_ON);
706 #endif /* CONFIG_SYS_FAULT_ECHO_LINK_DOWN, ... */
707 #endif /* CONFIG_MII, ... */
708 debug_cond(DEBUG_INT_STATE, "--- net_loop timeout\n");
710 time_handler = (thand_f *)0;
712 } else if (IS_ENABLED(CONFIG_IPV6_ROUTER_DISCOVERY))
713 if (time_handler && protocol == RS)
714 if (!ip6_is_unspecified_addr(&net_gateway6) &&
715 net_prefix_length != 0) {
716 net_set_state(NETLOOP_SUCCESS);
717 net_set_timeout_handler(0, NULL);
720 if (net_state == NETLOOP_FAIL)
721 ret = net_start_again();
724 case NETLOOP_RESTART:
728 case NETLOOP_SUCCESS:
730 if (net_boot_file_size > 0) {
731 printf("Bytes transferred = %u (%x hex)\n",
732 net_boot_file_size, net_boot_file_size);
733 env_set_hex("filesize", net_boot_file_size);
734 env_set_hex("fileaddr", image_load_addr);
736 if (protocol != NETCONS && protocol != NCSI)
739 eth_halt_state_only();
741 eth_set_last_protocol(protocol);
743 ret = net_boot_file_size;
744 debug_cond(DEBUG_INT_STATE, "--- net_loop Success!\n");
749 /* Invalidate the last protocol */
750 eth_set_last_protocol(BOOTP);
751 debug_cond(DEBUG_INT_STATE, "--- net_loop Fail!\n");
755 case NETLOOP_CONTINUE:
761 #ifdef CONFIG_USB_KEYBOARD
764 #ifdef CONFIG_CMD_TFTPPUT
765 /* Clear out the handlers */
766 net_set_udp_handler(NULL);
767 net_set_icmp_handler(NULL);
769 net_set_state(prev_net_state);
771 #if defined(CONFIG_CMD_PCAP)
778 /**********************************************************************/
780 static void start_again_timeout_handler(void)
782 net_set_state(NETLOOP_RESTART);
785 int net_start_again(void)
788 int retry_forever = 0;
789 unsigned long retrycnt = 0;
792 nretry = env_get("netretry");
794 if (!strcmp(nretry, "yes"))
796 else if (!strcmp(nretry, "no"))
798 else if (!strcmp(nretry, "once"))
801 retrycnt = simple_strtoul(nretry, NULL, 0);
807 if ((!retry_forever) && (net_try_count > retrycnt)) {
809 net_set_state(NETLOOP_FAIL);
811 * We don't provide a way for the protocol to return an error,
812 * but this is almost always the reason.
820 #if !defined(CONFIG_NET_DO_NOT_TRY_ANOTHER)
821 eth_try_another(!net_restarted);
824 if (net_restart_wrap) {
825 net_restart_wrap = 0;
826 if (net_dev_exists) {
827 net_set_timeout_handler(10000UL,
828 start_again_timeout_handler);
829 net_set_udp_handler(NULL);
831 net_set_state(NETLOOP_FAIL);
834 net_set_state(NETLOOP_RESTART);
839 /**********************************************************************/
844 static void dummy_handler(uchar *pkt, unsigned dport,
845 struct in_addr sip, unsigned sport,
850 rxhand_f *net_get_udp_handler(void)
852 return udp_packet_handler;
855 void net_set_udp_handler(rxhand_f *f)
857 debug_cond(DEBUG_INT_STATE, "--- net_loop UDP handler set (%p)\n", f);
859 udp_packet_handler = dummy_handler;
861 udp_packet_handler = f;
864 rxhand_f *net_get_arp_handler(void)
866 return arp_packet_handler;
869 void net_set_arp_handler(rxhand_f *f)
871 debug_cond(DEBUG_INT_STATE, "--- net_loop ARP handler set (%p)\n", f);
873 arp_packet_handler = dummy_handler;
875 arp_packet_handler = f;
878 #ifdef CONFIG_CMD_TFTPPUT
879 void net_set_icmp_handler(rxhand_icmp_f *f)
881 packet_icmp_handler = f;
885 void net_set_timeout_handler(ulong iv, thand_f *f)
888 debug_cond(DEBUG_INT_STATE,
889 "--- net_loop timeout handler cancelled\n");
890 time_handler = (thand_f *)0;
892 debug_cond(DEBUG_INT_STATE,
893 "--- net_loop timeout handler set (%p)\n", f);
895 time_start = get_timer(0);
896 time_delta = iv * CONFIG_SYS_HZ / 1000;
900 uchar *net_get_async_tx_pkt_buf(void)
902 if (arp_is_waiting())
903 return arp_tx_packet; /* If we are waiting, we already sent */
905 return net_tx_packet;
908 int net_send_udp_packet(uchar *ether, struct in_addr dest, int dport, int sport,
911 return net_send_ip_packet(ether, dest, dport, sport, payload_len,
912 IPPROTO_UDP, 0, 0, 0);
915 #if defined(CONFIG_PROT_TCP)
916 int net_send_tcp_packet(int payload_len, struct in_addr dhost, int dport,
917 int sport, u8 action, u32 tcp_seq_num, u32 tcp_ack_num)
919 return net_send_ip_packet(net_server_ethaddr, dhost, dport,
920 sport, payload_len, IPPROTO_TCP, action,
921 tcp_seq_num, tcp_ack_num);
925 int net_send_ip_packet(uchar *ether, struct in_addr dest, int dport, int sport,
926 int payload_len, int proto, u8 action, u32 tcp_seq_num,
932 #if defined(CONFIG_PROT_TCP)
933 struct tcp_stream *tcp;
936 /* make sure the net_tx_packet is initialized (net_init() was called) */
937 assert(net_tx_packet != NULL);
938 if (net_tx_packet == NULL)
941 /* convert to new style broadcast */
942 if (dest.s_addr == 0)
943 dest.s_addr = 0xFFFFFFFF;
945 /* if broadcast, make the ether address a broadcast and don't do ARP */
946 if (dest.s_addr == 0xFFFFFFFF)
947 ether = (uchar *)net_bcast_ethaddr;
949 pkt = (uchar *)net_tx_packet;
951 eth_hdr_size = net_set_ether(pkt, ether, PROT_IP);
955 net_set_udp_header(pkt + eth_hdr_size, dest, dport, sport,
957 pkt_hdr_size = eth_hdr_size + IP_UDP_HDR_SIZE;
959 #if defined(CONFIG_PROT_TCP)
961 tcp = tcp_stream_get(0, dest, dport, sport);
965 pkt_hdr_size = eth_hdr_size
966 + tcp_set_tcp_header(tcp, pkt + eth_hdr_size,
967 payload_len, action, tcp_seq_num,
976 /* if MAC address was not discovered yet, do an ARP request */
977 if (memcmp(ether, net_null_ethaddr, 6) == 0) {
978 debug_cond(DEBUG_DEV_PKT, "sending ARP for %pI4\n", &dest);
980 /* save the ip and eth addr for the packet to send after arp */
981 net_arp_wait_packet_ip = dest;
982 arp_wait_packet_ethaddr = ether;
984 /* size of the waiting packet */
985 arp_wait_tx_packet_size = pkt_hdr_size + payload_len;
987 /* and do the ARP request */
989 arp_wait_timer_start = get_timer(0);
991 return 1; /* waiting */
993 debug_cond(DEBUG_DEV_PKT, "sending UDP to %pI4/%pM\n",
995 net_send_packet(net_tx_packet, pkt_hdr_size + payload_len);
996 return 0; /* transmitted */
1000 #ifdef CONFIG_IP_DEFRAG
1002 * This function collects fragments in a single packet, according
1003 * to the algorithm in RFC815. It returns NULL or the pointer to
1004 * a complete packet, in static storage
1006 #define IP_PKTSIZE (CONFIG_NET_MAXDEFRAG)
1008 #define IP_MAXUDP (IP_PKTSIZE - IP_HDR_SIZE)
1011 * this is the packet being assembled, either data or frag control.
1012 * Fragments go by 8 bytes, so this union must be 8 bytes long
1015 /* first_byte is address of this structure */
1016 u16 last_byte; /* last byte in this hole + 1 (begin of next hole) */
1017 u16 next_hole; /* index of next (in 8-b blocks), 0 == none */
1018 u16 prev_hole; /* index of prev, 0 == none */
1022 static struct ip_udp_hdr *__net_defragment(struct ip_udp_hdr *ip, int *lenp)
1024 static uchar pkt_buff[IP_PKTSIZE] __aligned(PKTALIGN);
1025 static u16 first_hole, total_len;
1026 struct hole *payload, *thisfrag, *h, *newh;
1027 struct ip_udp_hdr *localip = (struct ip_udp_hdr *)pkt_buff;
1028 uchar *indata = (uchar *)ip;
1029 int offset8, start, len, done = 0;
1030 u16 ip_off = ntohs(ip->ip_off);
1033 * Calling code already rejected <, but we don't have to deal
1034 * with an IP fragment with no payload.
1036 if (ntohs(ip->ip_len) <= IP_HDR_SIZE)
1039 /* payload starts after IP header, this fragment is in there */
1040 payload = (struct hole *)(pkt_buff + IP_HDR_SIZE);
1041 offset8 = (ip_off & IP_OFFS);
1042 thisfrag = payload + offset8;
1043 start = offset8 * 8;
1044 len = ntohs(ip->ip_len) - IP_HDR_SIZE;
1046 /* All but last fragment must have a multiple-of-8 payload. */
1047 if ((len & 7) && (ip_off & IP_FLAGS_MFRAG))
1050 if (start + len > IP_MAXUDP) /* fragment extends too far */
1053 if (!total_len || localip->ip_id != ip->ip_id) {
1054 /* new (or different) packet, reset structs */
1056 payload[0].last_byte = ~0;
1057 payload[0].next_hole = 0;
1058 payload[0].prev_hole = 0;
1060 /* any IP header will work, copy the first we received */
1061 memcpy(localip, ip, IP_HDR_SIZE);
1065 * What follows is the reassembly algorithm. We use the payload
1066 * array as a linked list of hole descriptors, as each hole starts
1067 * at a multiple of 8 bytes. However, last byte can be whatever value,
1068 * so it is represented as byte count, not as 8-byte blocks.
1071 h = payload + first_hole;
1072 while (h->last_byte < start) {
1073 if (!h->next_hole) {
1074 /* no hole that far away */
1077 h = payload + h->next_hole;
1080 /* last fragment may be 1..7 bytes, the "+7" forces acceptance */
1081 if (offset8 + ((len + 7) / 8) <= h - payload) {
1082 /* no overlap with holes (dup fragment?) */
1086 if (!(ip_off & IP_FLAGS_MFRAG)) {
1087 /* no more fragmentss: truncate this (last) hole */
1088 total_len = start + len;
1089 h->last_byte = start + len;
1093 * There is some overlap: fix the hole list. This code deals
1094 * with a fragment that overlaps with two different holes
1095 * (thus being a superset of a previously-received fragment)
1096 * by only using the part of the fragment that fits in the
1099 if (h->last_byte < start + len)
1100 len = h->last_byte - start;
1102 if ((h >= thisfrag) && (h->last_byte <= start + len)) {
1103 /* complete overlap with hole: remove hole */
1104 if (!h->prev_hole && !h->next_hole) {
1105 /* last remaining hole */
1107 } else if (!h->prev_hole) {
1109 first_hole = h->next_hole;
1110 payload[h->next_hole].prev_hole = 0;
1111 } else if (!h->next_hole) {
1113 payload[h->prev_hole].next_hole = 0;
1115 /* in the middle of the list */
1116 payload[h->next_hole].prev_hole = h->prev_hole;
1117 payload[h->prev_hole].next_hole = h->next_hole;
1120 } else if (h->last_byte <= start + len) {
1121 /* overlaps with final part of the hole: shorten this hole */
1122 h->last_byte = start;
1124 } else if (h >= thisfrag) {
1125 /* overlaps with initial part of the hole: move this hole */
1126 newh = thisfrag + (len / 8);
1130 payload[h->next_hole].prev_hole = (h - payload);
1132 payload[h->prev_hole].next_hole = (h - payload);
1134 first_hole = (h - payload);
1137 /* fragment sits in the middle: split the hole */
1138 newh = thisfrag + (len / 8);
1140 h->last_byte = start;
1141 h->next_hole = (newh - payload);
1142 newh->prev_hole = (h - payload);
1143 if (newh->next_hole)
1144 payload[newh->next_hole].prev_hole = (newh - payload);
1147 /* finally copy this fragment and possibly return whole packet */
1148 memcpy((uchar *)thisfrag, indata + IP_HDR_SIZE, len);
1152 *lenp = total_len + IP_HDR_SIZE;
1153 localip->ip_len = htons(*lenp);
1157 static inline struct ip_udp_hdr *net_defragment(struct ip_udp_hdr *ip,
1160 u16 ip_off = ntohs(ip->ip_off);
1161 if (!(ip_off & (IP_OFFS | IP_FLAGS_MFRAG)))
1162 return ip; /* not a fragment */
1163 return __net_defragment(ip, lenp);
1166 #else /* !CONFIG_IP_DEFRAG */
1168 static inline struct ip_udp_hdr *net_defragment(struct ip_udp_hdr *ip,
1171 u16 ip_off = ntohs(ip->ip_off);
1172 if (!(ip_off & (IP_OFFS | IP_FLAGS_MFRAG)))
1173 return ip; /* not a fragment */
1179 * Receive an ICMP packet. We deal with REDIRECT and PING here, and silently
1182 * @parma ip IP packet containing the ICMP
1184 static void receive_icmp(struct ip_udp_hdr *ip, int len,
1185 struct in_addr src_ip, struct ethernet_hdr *et)
1187 struct icmp_hdr *icmph = (struct icmp_hdr *)&ip->udp_src;
1189 switch (icmph->type) {
1191 if (icmph->code != ICMP_REDIR_HOST)
1193 printf(" ICMP Host Redirect to %pI4 ",
1194 &icmph->un.gateway);
1197 #if defined(CONFIG_CMD_PING)
1198 ping_receive(et, ip, len);
1200 #ifdef CONFIG_CMD_TFTPPUT
1201 if (packet_icmp_handler)
1202 packet_icmp_handler(icmph->type, icmph->code,
1203 ntohs(ip->udp_dst), src_ip,
1204 ntohs(ip->udp_src), icmph->un.data,
1205 ntohs(ip->udp_len));
1211 void net_process_received_packet(uchar *in_packet, int len)
1213 struct ethernet_hdr *et;
1214 struct ip_udp_hdr *ip;
1215 struct in_addr dst_ip;
1216 struct in_addr src_ip;
1218 #if defined(CONFIG_CMD_CDP)
1221 ushort cti = 0, vlanid = VLAN_NONE, myvlanid, mynvlanid;
1223 debug_cond(DEBUG_NET_PKT, "packet received\n");
1224 if (DEBUG_NET_PKT_TRACE)
1225 print_hex_dump_bytes("rx: ", DUMP_PREFIX_OFFSET, in_packet,
1228 #if defined(CONFIG_CMD_PCAP)
1229 pcap_post(in_packet, len, false);
1231 net_rx_packet = in_packet;
1232 net_rx_packet_len = len;
1233 et = (struct ethernet_hdr *)in_packet;
1235 /* too small packet? */
1236 if (len < ETHER_HDR_SIZE)
1239 #if defined(CONFIG_API) || defined(CONFIG_EFI_LOADER)
1241 (*push_packet)(in_packet, len);
1246 #if defined(CONFIG_CMD_CDP)
1247 /* keep track if packet is CDP */
1248 iscdp = is_cdp_packet(et->et_dest);
1251 myvlanid = ntohs(net_our_vlan);
1252 if (myvlanid == (ushort)-1)
1253 myvlanid = VLAN_NONE;
1254 mynvlanid = ntohs(net_native_vlan);
1255 if (mynvlanid == (ushort)-1)
1256 mynvlanid = VLAN_NONE;
1258 eth_proto = ntohs(et->et_protlen);
1260 if (eth_proto < 1514) {
1261 struct e802_hdr *et802 = (struct e802_hdr *)et;
1263 * Got a 802.2 packet. Check the other protocol field.
1264 * XXX VLAN over 802.2+SNAP not implemented!
1266 eth_proto = ntohs(et802->et_prot);
1268 ip = (struct ip_udp_hdr *)(in_packet + E802_HDR_SIZE);
1269 len -= E802_HDR_SIZE;
1271 } else if (eth_proto != PROT_VLAN) { /* normal packet */
1272 ip = (struct ip_udp_hdr *)(in_packet + ETHER_HDR_SIZE);
1273 len -= ETHER_HDR_SIZE;
1275 } else { /* VLAN packet */
1276 struct vlan_ethernet_hdr *vet =
1277 (struct vlan_ethernet_hdr *)et;
1279 debug_cond(DEBUG_NET_PKT, "VLAN packet received\n");
1281 /* too small packet? */
1282 if (len < VLAN_ETHER_HDR_SIZE)
1285 /* if no VLAN active */
1286 if ((ntohs(net_our_vlan) & VLAN_IDMASK) == VLAN_NONE
1287 #if defined(CONFIG_CMD_CDP)
1293 cti = ntohs(vet->vet_tag);
1294 vlanid = cti & VLAN_IDMASK;
1295 eth_proto = ntohs(vet->vet_type);
1297 ip = (struct ip_udp_hdr *)(in_packet + VLAN_ETHER_HDR_SIZE);
1298 len -= VLAN_ETHER_HDR_SIZE;
1301 debug_cond(DEBUG_NET_PKT, "Receive from protocol 0x%x\n", eth_proto);
1303 #if defined(CONFIG_CMD_CDP)
1305 cdp_receive((uchar *)ip, len);
1310 if ((myvlanid & VLAN_IDMASK) != VLAN_NONE) {
1311 if (vlanid == VLAN_NONE)
1312 vlanid = (mynvlanid & VLAN_IDMASK);
1314 if (vlanid != (myvlanid & VLAN_IDMASK))
1318 switch (eth_proto) {
1320 arp_receive(et, ip, len);
1323 #ifdef CONFIG_CMD_RARP
1325 rarp_receive(ip, len);
1328 #if IS_ENABLED(CONFIG_IPV6)
1330 net_ip6_handler(et, (struct ip6_hdr *)ip, len);
1334 debug_cond(DEBUG_NET_PKT, "Got IP\n");
1335 /* Before we start poking the header, make sure it is there */
1336 if (len < IP_HDR_SIZE) {
1337 debug("len bad %d < %lu\n", len,
1338 (ulong)IP_HDR_SIZE);
1341 /* Check the packet length */
1342 if (len < ntohs(ip->ip_len)) {
1343 debug("len bad %d < %d\n", len, ntohs(ip->ip_len));
1346 len = ntohs(ip->ip_len);
1347 if (len < IP_HDR_SIZE) {
1348 debug("bad ip->ip_len %d < %d\n", len, (int)IP_HDR_SIZE);
1351 debug_cond(DEBUG_NET_PKT, "len=%d, v=%02x\n",
1352 len, ip->ip_hl_v & 0xff);
1354 /* Can't deal with anything except IPv4 */
1355 if ((ip->ip_hl_v & 0xf0) != 0x40)
1357 /* Can't deal with IP options (headers != 20 bytes) */
1358 if ((ip->ip_hl_v & 0x0f) != 0x05)
1360 /* Check the Checksum of the header */
1361 if (!ip_checksum_ok((uchar *)ip, IP_HDR_SIZE)) {
1362 debug("checksum bad\n");
1365 /* If it is not for us, ignore it */
1366 dst_ip = net_read_ip(&ip->ip_dst);
1367 if (net_ip.s_addr && dst_ip.s_addr != net_ip.s_addr &&
1368 dst_ip.s_addr != 0xFFFFFFFF) {
1371 /* Read source IP address for later use */
1372 src_ip = net_read_ip(&ip->ip_src);
1374 * The function returns the unchanged packet if it's not
1375 * a fragment, and either the complete packet or NULL if
1376 * it is a fragment (if !CONFIG_IP_DEFRAG, it returns NULL)
1378 ip = net_defragment(ip, &len);
1382 * watch for ICMP host redirects
1384 * There is no real handler code (yet). We just watch
1385 * for ICMP host redirect messages. In case anybody
1386 * sees these messages: please contact me
1388 * necessary fixes :-)
1390 * Note: in all cases where I have seen this so far
1391 * it was a problem with the router configuration,
1392 * for instance when a router was configured in the
1393 * BOOTP reply, but the TFTP server was on the same
1394 * subnet. So this is probably a warning that your
1395 * configuration might be wrong. But I'm not really
1396 * sure if there aren't any other situations.
1399 * we send a tftp packet to a dead connection, or when
1400 * there is no server at the other end.
1402 if (ip->ip_p == IPPROTO_ICMP) {
1403 receive_icmp(ip, len, src_ip, et);
1405 #if defined(CONFIG_PROT_TCP)
1406 } else if (ip->ip_p == IPPROTO_TCP) {
1407 debug_cond(DEBUG_DEV_PKT,
1408 "TCP PH (to=%pI4, from=%pI4, len=%d)\n",
1409 &dst_ip, &src_ip, len);
1411 rxhand_tcp_f((union tcp_build_pkt *)ip, len);
1414 } else if (ip->ip_p != IPPROTO_UDP) { /* Only UDP packets */
1418 if (ntohs(ip->udp_len) < UDP_HDR_SIZE || ntohs(ip->udp_len) > len - IP_HDR_SIZE)
1421 debug_cond(DEBUG_DEV_PKT,
1422 "received UDP (to=%pI4, from=%pI4, len=%d)\n",
1423 &dst_ip, &src_ip, len);
1425 if (IS_ENABLED(CONFIG_UDP_CHECKSUM) && ip->udp_xsum != 0) {
1431 xsum += (ntohs(ip->udp_len));
1432 xsum += (ntohl(ip->ip_src.s_addr) >> 16) & 0x0000ffff;
1433 xsum += (ntohl(ip->ip_src.s_addr) >> 0) & 0x0000ffff;
1434 xsum += (ntohl(ip->ip_dst.s_addr) >> 16) & 0x0000ffff;
1435 xsum += (ntohl(ip->ip_dst.s_addr) >> 0) & 0x0000ffff;
1437 sumlen = ntohs(ip->udp_len);
1438 sumptr = (u8 *)&ip->udp_src;
1440 while (sumlen > 1) {
1441 /* inlined ntohs() to avoid alignment errors */
1442 xsum += (sumptr[0] << 8) + sumptr[1];
1447 xsum += (sumptr[0] << 8) + sumptr[0];
1448 while ((xsum >> 16) != 0) {
1449 xsum = (xsum & 0x0000ffff) +
1450 ((xsum >> 16) & 0x0000ffff);
1452 if ((xsum != 0x00000000) && (xsum != 0x0000ffff)) {
1453 printf(" UDP wrong checksum %08lx %08x\n",
1454 xsum, ntohs(ip->udp_xsum));
1459 #if defined(CONFIG_NETCONSOLE) && !defined(CONFIG_XPL_BUILD)
1460 nc_input_packet((uchar *)ip + IP_UDP_HDR_SIZE,
1464 ntohs(ip->udp_len) - UDP_HDR_SIZE);
1467 * IP header OK. Pass the packet to the current handler.
1469 (*udp_packet_handler)((uchar *)ip + IP_UDP_HDR_SIZE,
1473 ntohs(ip->udp_len) - UDP_HDR_SIZE);
1475 #ifdef CONFIG_CMD_WOL
1477 wol_receive(ip, len);
1480 #ifdef CONFIG_PHY_NCSI
1482 ncsi_receive(et, ip, len);
1488 /**********************************************************************/
1490 static int net_check_prereq(enum proto_t protocol)
1494 #if defined(CONFIG_CMD_PING)
1496 if (net_ping_ip.s_addr == 0) {
1497 puts("*** ERROR: ping address not given\n");
1502 #if defined(CONFIG_CMD_PING6)
1504 if (ip6_is_unspecified_addr(&net_ping_ip6)) {
1505 puts("*** ERROR: ping address not given\n");
1510 #if defined(CONFIG_CMD_DNS)
1512 if (net_dns_server.s_addr == 0) {
1513 puts("*** ERROR: DNS server address not given\n");
1518 #if defined(CONFIG_PROT_UDP)
1525 #if defined(CONFIG_CMD_NFS)
1531 if (IS_ENABLED(CONFIG_IPV6) && use_ip6) {
1532 if (!memcmp(&net_server_ip6, &net_null_addr_ip6,
1533 sizeof(struct in6_addr)) &&
1534 !strchr(net_boot_file_name, '[')) {
1535 puts("*** ERROR: `serverip6' not set\n");
1538 } else if (net_server_ip.s_addr == 0 && !is_serverip_in_cmd()) {
1539 puts("*** ERROR: `serverip' not set\n");
1542 #if defined(CONFIG_CMD_PING) || \
1543 defined(CONFIG_CMD_DNS) || defined(CONFIG_PROT_UDP)
1552 if (IS_ENABLED(CONFIG_IPV6) && use_ip6) {
1553 if (!memcmp(&net_link_local_ip6, &net_null_addr_ip6,
1554 sizeof(struct in6_addr))) {
1555 puts("*** ERROR: `ip6addr` not set\n");
1558 } else if (net_ip.s_addr == 0) {
1559 puts("*** ERROR: `ipaddr' not set\n");
1564 #ifdef CONFIG_CMD_RARP
1567 #ifdef CONFIG_PHY_NCSI
1574 if (memcmp(net_ethaddr, "\0\0\0\0\0\0", 6) == 0) {
1575 int num = eth_get_dev_index();
1579 puts("*** ERROR: No ethernet found.\n");
1582 puts("*** ERROR: `ethaddr' not set\n");
1585 printf("*** ERROR: `eth%daddr' not set\n",
1599 /**********************************************************************/
1602 net_eth_hdr_size(void)
1606 myvlanid = ntohs(net_our_vlan);
1607 if (myvlanid == (ushort)-1)
1608 myvlanid = VLAN_NONE;
1610 return ((myvlanid & VLAN_IDMASK) == VLAN_NONE) ? ETHER_HDR_SIZE :
1611 VLAN_ETHER_HDR_SIZE;
1614 int net_set_ether(uchar *xet, const uchar *dest_ethaddr, uint prot)
1616 struct ethernet_hdr *et = (struct ethernet_hdr *)xet;
1619 myvlanid = ntohs(net_our_vlan);
1620 if (myvlanid == (ushort)-1)
1621 myvlanid = VLAN_NONE;
1623 memcpy(et->et_dest, dest_ethaddr, 6);
1624 memcpy(et->et_src, net_ethaddr, 6);
1625 if ((myvlanid & VLAN_IDMASK) == VLAN_NONE) {
1626 et->et_protlen = htons(prot);
1627 return ETHER_HDR_SIZE;
1629 struct vlan_ethernet_hdr *vet =
1630 (struct vlan_ethernet_hdr *)xet;
1632 vet->vet_vlan_type = htons(PROT_VLAN);
1633 vet->vet_tag = htons((0 << 5) | (myvlanid & VLAN_IDMASK));
1634 vet->vet_type = htons(prot);
1635 return VLAN_ETHER_HDR_SIZE;
1639 int net_update_ether(struct ethernet_hdr *et, uchar *addr, uint prot)
1643 memcpy(et->et_dest, addr, 6);
1644 memcpy(et->et_src, net_ethaddr, 6);
1645 protlen = ntohs(et->et_protlen);
1646 if (protlen == PROT_VLAN) {
1647 struct vlan_ethernet_hdr *vet =
1648 (struct vlan_ethernet_hdr *)et;
1649 vet->vet_type = htons(prot);
1650 return VLAN_ETHER_HDR_SIZE;
1651 } else if (protlen > 1514) {
1652 et->et_protlen = htons(prot);
1653 return ETHER_HDR_SIZE;
1656 struct e802_hdr *et802 = (struct e802_hdr *)et;
1657 et802->et_prot = htons(prot);
1658 return E802_HDR_SIZE;
1662 void net_set_ip_header(uchar *pkt, struct in_addr dest, struct in_addr source,
1663 u16 pkt_len, u8 proto)
1665 struct ip_udp_hdr *ip = (struct ip_udp_hdr *)pkt;
1668 * Construct an IP header.
1670 /* IP_HDR_SIZE / 4 (not including UDP) */
1673 ip->ip_len = htons(pkt_len);
1675 ip->ip_id = htons(net_ip_id++);
1676 ip->ip_off = htons(IP_FLAGS_DFRAG); /* Don't fragment */
1679 /* already in network byte order */
1680 net_copy_ip((void *)&ip->ip_src, &source);
1681 /* already in network byte order */
1682 net_copy_ip((void *)&ip->ip_dst, &dest);
1684 ip->ip_sum = compute_ip_checksum(ip, IP_HDR_SIZE);
1687 void net_set_udp_header(uchar *pkt, struct in_addr dest, int dport, int sport,
1690 struct ip_udp_hdr *ip = (struct ip_udp_hdr *)pkt;
1693 * If the data is an odd number of bytes, zero the
1694 * byte after the last byte so that the checksum
1698 pkt[IP_UDP_HDR_SIZE + len] = 0;
1700 net_set_ip_header(pkt, dest, net_ip, IP_UDP_HDR_SIZE + len,
1703 ip->udp_src = htons(sport);
1704 ip->udp_dst = htons(dport);
1705 ip->udp_len = htons(UDP_HDR_SIZE + len);
1709 int is_serverip_in_cmd(void)
1711 return !!strchr(net_boot_file_name, ':');
1714 int net_parse_bootfile(struct in_addr *ipaddr, char *filename, int max_len)
1720 if (net_boot_file_name[0] == '\0')
1723 colon = strchr(net_boot_file_name, ':');
1725 ip = string_to_ip(net_boot_file_name);
1726 if (ipaddr && ip.s_addr)
1730 strncpy(filename, colon + 1, max_len);
1732 strncpy(filename, net_boot_file_name, max_len);
1734 filename[max_len - 1] = '\0';
1739 void vlan_to_string(ushort x, char *s)
1743 if (x == (ushort)-1)
1749 sprintf(s, "%d", x & VLAN_IDMASK);
1752 ushort string_to_vlan(const char *s)
1757 return htons(VLAN_NONE);
1759 if (*s < '0' || *s > '9')
1762 id = (ushort)dectoul(s, NULL);
1767 ushort env_get_vlan(char *var)
1769 return string_to_vlan(env_get(var));