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
9 * Copyright 2000-2002 Wolfgang Denk, wd@denx.de
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>
95 #include <net/fastboot_udp.h>
96 #include <net/fastboot_tcp.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>
111 #include <net/wget.h>
115 #if defined(CONFIG_CMD_DNS)
118 #include "link_local.h"
122 #if defined(CONFIG_CMD_WOL)
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
;
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 */
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
;
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;
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;
178 ushort net_native_vlan
= 0xFFFF;
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
;
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
,
220 if (flags
& H_PROGRAMMATIC
)
223 net_ip
= string_to_ip(value
);
227 U_BOOT_ENV_CALLBACK(ipaddr
, on_ipaddr
);
229 static int on_gatewayip(const char *name
, const char *value
, enum env_op op
,
232 if (flags
& H_PROGRAMMATIC
)
235 net_gateway
= string_to_ip(value
);
239 U_BOOT_ENV_CALLBACK(gatewayip
, on_gatewayip
);
241 static int on_netmask(const char *name
, const char *value
, enum env_op op
,
244 if (flags
& H_PROGRAMMATIC
)
247 net_netmask
= string_to_ip(value
);
251 U_BOOT_ENV_CALLBACK(netmask
, on_netmask
);
253 static int on_serverip(const char *name
, const char *value
, enum env_op op
,
256 if (flags
& H_PROGRAMMATIC
)
259 net_server_ip
= string_to_ip(value
);
263 U_BOOT_ENV_CALLBACK(serverip
, on_serverip
);
265 static int on_nvlan(const char *name
, const char *value
, enum env_op op
,
268 if (flags
& H_PROGRAMMATIC
)
271 net_native_vlan
= string_to_vlan(value
);
275 U_BOOT_ENV_CALLBACK(nvlan
, on_nvlan
);
277 static int on_vlan(const char *name
, const char *value
, enum env_op op
,
280 if (flags
& H_PROGRAMMATIC
)
283 net_our_vlan
= string_to_vlan(value
);
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
,
293 if (flags
& H_PROGRAMMATIC
)
296 net_dns_server
= string_to_ip(value
);
300 U_BOOT_ENV_CALLBACK(dnsip
, on_dnsip
);
304 * Check if autoload is enabled. If so, use either NFS or TFTP to download
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
);
318 printf("Cannot autoload with NFS\n");
319 net_set_state(NETLOOP_FAIL
);
324 * Use NFS to load the bootfile.
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
);
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
);
347 printf("Cannot autoload with TFTPGET\n");
348 net_set_state(NETLOOP_FAIL
);
356 static int net_init_loop(void)
358 static bool first_call
= true;
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
));
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
379 if (IS_ENABLED(CONFIG_IPV6_ROUTER_DISCOVERY
))
380 if (first_call
&& use_ip6
) {
382 srand_mac(); /* This is for rand used in ip6_send_rs. */
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();
402 static int first_call
= 1;
406 * Setup packet buffers, aligned correctly.
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
;
418 net_clear_handlers();
420 /* Only need to setup buffer pointers once. */
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
)
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;
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)
453 eth_init_state_only();
458 bootstage_mark_name(BOOTSTAGE_ID_ETH_START
, "eth_start");
460 if (eth_is_on_demand_init()) {
469 eth_init_state_only();
473 #ifdef CONFIG_USB_KEYBOARD
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");
486 if (!test_eth_enabled())
489 switch (net_check_prereq(protocol
)) {
491 /* network not configured */
493 net_set_state(prev_net_state
);
497 /* network device not configured */
502 net_boot_file_size
= 0;
504 #ifdef CONFIG_CMD_TFTPBOOT
506 #ifdef CONFIG_CMD_TFTPPUT
509 /* always use ARP to get server ethernet address */
510 tftp_start(protocol
);
513 #ifdef CONFIG_CMD_TFTPSRV
518 #if CONFIG_IS_ENABLED(UDP_FUNCTION_FASTBOOT)
520 fastboot_udp_start_server();
523 #if CONFIG_IS_ENABLED(TCP_FUNCTION_FASTBOOT)
525 fastboot_tcp_start_server();
528 #if defined(CONFIG_CMD_DHCP)
532 dhcp_request(); /* Basically same as BOOTP */
536 if (IS_ENABLED(CONFIG_CMD_DHCP6
))
539 #if defined(CONFIG_CMD_BOOTP)
546 #if defined(CONFIG_CMD_RARP)
553 #if defined(CONFIG_CMD_PING)
558 #if defined(CONFIG_CMD_PING6)
563 #if defined(CONFIG_CMD_NFS) && !defined(CONFIG_XPL_BUILD)
568 #if defined(CONFIG_CMD_WGET)
573 #if defined(CONFIG_CMD_CDP)
578 #if defined(CONFIG_NETCONSOLE) && !defined(CONFIG_XPL_BUILD)
583 #if defined(CONFIG_CMD_DNS)
588 #if defined(CONFIG_CMD_LINK_LOCAL)
593 #if defined(CONFIG_CMD_WOL)
598 #if defined(CONFIG_PHY_NCSI)
600 ncsi_probe_packages();
604 if (IS_ENABLED(CONFIG_IPV6_ROUTER_DISCOVERY
))
611 if (IS_ENABLED(CONFIG_PROT_UDP
) && protocol
== UDP
)
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
);
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
635 * Main packet reception loop. Loop receiving packets until
636 * someone sets `net_state' to a state that terminates.
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.
657 * Abort if ctrl-c was pressed.
660 /* cancel any ARP that may not have completed */
661 net_arp_wait_packet_ip
.s_addr
= 0;
665 /* Invalidate the last protocol */
666 eth_set_last_protocol(BOOTP
);
668 /* Turn off activity LED if triggered */
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");
680 * Check for a timeout, and run the timeout handler
684 ((get_timer(0) - time_start
) > time_delta
)) {
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
);
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");
705 time_handler
= (thand_f
*)0;
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();
719 case NETLOOP_RESTART
:
723 case NETLOOP_SUCCESS
:
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
)
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");
744 /* Invalidate the last protocol */
745 eth_set_last_protocol(BOOTP
);
746 debug_cond(DEBUG_INT_STATE
, "--- net_loop Fail!\n");
750 case NETLOOP_CONTINUE
:
756 #ifdef CONFIG_USB_KEYBOARD
759 #ifdef CONFIG_CMD_TFTPPUT
760 /* Clear out the handlers */
761 net_set_udp_handler(NULL
);
762 net_set_icmp_handler(NULL
);
764 net_set_state(prev_net_state
);
766 #if defined(CONFIG_CMD_PCAP)
773 /**********************************************************************/
775 static void start_again_timeout_handler(void)
777 net_set_state(NETLOOP_RESTART
);
780 int net_start_again(void)
783 int retry_forever
= 0;
784 unsigned long retrycnt
= 0;
787 nretry
= env_get("netretry");
789 if (!strcmp(nretry
, "yes"))
791 else if (!strcmp(nretry
, "no"))
793 else if (!strcmp(nretry
, "once"))
796 retrycnt
= simple_strtoul(nretry
, NULL
, 0);
802 if ((!retry_forever
) && (net_try_count
> retrycnt
)) {
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.
815 #if !defined(CONFIG_NET_DO_NOT_TRY_ANOTHER)
816 eth_try_another(!net_restarted
);
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
);
826 net_set_state(NETLOOP_FAIL
);
829 net_set_state(NETLOOP_RESTART
);
834 /**********************************************************************/
839 static void dummy_handler(uchar
*pkt
, unsigned dport
,
840 struct in_addr sip
, unsigned sport
,
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
);
854 udp_packet_handler
= dummy_handler
;
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
);
868 arp_packet_handler
= dummy_handler
;
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
;
880 void net_set_timeout_handler(ulong iv
, thand_f
*f
)
883 debug_cond(DEBUG_INT_STATE
,
884 "--- net_loop timeout handler cancelled\n");
885 time_handler
= (thand_f
*)0;
887 debug_cond(DEBUG_INT_STATE
,
888 "--- net_loop timeout handler set (%p)\n", 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 */
900 return net_tx_packet
;
903 int net_send_udp_packet(uchar
*ether
, struct in_addr dest
, int dport
, int sport
,
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
);
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
,
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
)
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
);
947 net_set_udp_header(pkt
+ eth_hdr_size
, dest
, dport
, sport
,
949 pkt_hdr_size
= eth_hdr_size
+ IP_UDP_HDR_SIZE
;
951 #if defined(CONFIG_PROT_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
,
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 */
976 arp_wait_timer_start
= get_timer(0);
978 return 1; /* waiting */
980 debug_cond(DEBUG_DEV_PKT
, "sending UDP to %pI4/%pM\n",
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
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 */
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
)
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
))
1037 if (start
+ len
> IP_MAXUDP
) /* fragment extends too far */
1040 if (!total_len
|| localip
->ip_id
!= ip
->ip_id
) {
1041 /* new (or different) packet, reset structs */
1043 payload
[0].last_byte
= ~0;
1044 payload
[0].next_hole
= 0;
1045 payload
[0].prev_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 */
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?) */
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
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 */
1094 } else if (!h
->prev_hole
) {
1096 first_hole
= h
->next_hole
;
1097 payload
[h
->next_hole
].prev_hole
= 0;
1098 } else if (!h
->next_hole
) {
1100 payload
[h
->prev_hole
].next_hole
= 0;
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);
1117 payload
[h
->next_hole
].prev_hole
= (h
- payload
);
1119 payload
[h
->prev_hole
].next_hole
= (h
- payload
);
1121 first_hole
= (h
- payload
);
1124 /* fragment sits in the middle: split the hole */
1125 newh
= thisfrag
+ (len
/ 8);
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
);
1139 *lenp
= total_len
+ IP_HDR_SIZE
;
1140 localip
->ip_len
= htons(*lenp
);
1144 static inline struct ip_udp_hdr
*net_defragment(struct ip_udp_hdr
*ip
,
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
,
1158 u16 ip_off
= ntohs(ip
->ip_off
);
1159 if (!(ip_off
& (IP_OFFS
| IP_FLAGS_MFRAG
)))
1160 return ip
; /* not a fragment */
1166 * Receive an ICMP packet. We deal with REDIRECT and PING here, and silently
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
) {
1178 if (icmph
->code
!= ICMP_REDIR_HOST
)
1180 printf(" ICMP Host Redirect to %pI4 ",
1181 &icmph
->un
.gateway
);
1184 #if defined(CONFIG_CMD_PING)
1185 ping_receive(et
, ip
, len
);
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
));
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
;
1205 #if defined(CONFIG_CMD_CDP)
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
,
1215 #if defined(CONFIG_CMD_PCAP)
1216 pcap_post(in_packet
, len
, false);
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
)
1226 #if defined(CONFIG_API) || defined(CONFIG_EFI_LOADER)
1228 (*push_packet
)(in_packet
, len
);
1233 #if defined(CONFIG_CMD_CDP)
1234 /* keep track if packet is CDP */
1235 iscdp
= is_cdp_packet(et
->et_dest
);
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
)
1272 /* if no VLAN active */
1273 if ((ntohs(net_our_vlan
) & VLAN_IDMASK
) == VLAN_NONE
1274 #if defined(CONFIG_CMD_CDP)
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)
1292 cdp_receive((uchar
*)ip
, len
);
1297 if ((myvlanid
& VLAN_IDMASK
) != VLAN_NONE
) {
1298 if (vlanid
== VLAN_NONE
)
1299 vlanid
= (mynvlanid
& VLAN_IDMASK
);
1301 if (vlanid
!= (myvlanid
& VLAN_IDMASK
))
1305 switch (eth_proto
) {
1307 arp_receive(et
, ip
, len
);
1310 #ifdef CONFIG_CMD_RARP
1312 rarp_receive(ip
, len
);
1315 #if IS_ENABLED(CONFIG_IPV6)
1317 net_ip6_handler(et
, (struct ip6_hdr
*)ip
, len
);
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
);
1328 /* Check the packet length */
1329 if (len
< ntohs(ip
->ip_len
)) {
1330 debug("len bad %d < %d\n", len
, ntohs(ip
->ip_len
));
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
);
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)
1344 /* Can't deal with IP options (headers != 20 bytes) */
1345 if ((ip
->ip_hl_v
& 0x0f) != 0x05)
1347 /* Check the Checksum of the header */
1348 if (!ip_checksum_ok((uchar
*)ip
, IP_HDR_SIZE
)) {
1349 debug("checksum bad\n");
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) {
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
);
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
);
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
);
1401 } else if (ip
->ip_p
!= IPPROTO_UDP
) { /* Only UDP packets */
1405 if (ntohs(ip
->udp_len
) < UDP_HDR_SIZE
|| ntohs(ip
->udp_len
) > len
- IP_HDR_SIZE
)
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) {
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];
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
));
1446 #if defined(CONFIG_NETCONSOLE) && !defined(CONFIG_XPL_BUILD)
1447 nc_input_packet((uchar
*)ip
+ IP_UDP_HDR_SIZE
,
1451 ntohs(ip
->udp_len
) - UDP_HDR_SIZE
);
1454 * IP header OK. Pass the packet to the current handler.
1456 (*udp_packet_handler
)((uchar
*)ip
+ IP_UDP_HDR_SIZE
,
1460 ntohs(ip
->udp_len
) - UDP_HDR_SIZE
);
1462 #ifdef CONFIG_CMD_WOL
1464 wol_receive(ip
, len
);
1467 #ifdef CONFIG_PHY_NCSI
1469 ncsi_receive(et
, ip
, len
);
1475 /**********************************************************************/
1477 static int net_check_prereq(enum proto_t protocol
)
1481 #if defined(CONFIG_CMD_PING)
1483 if (net_ping_ip
.s_addr
== 0) {
1484 puts("*** ERROR: ping address not given\n");
1489 #if defined(CONFIG_CMD_PING6)
1491 if (ip6_is_unspecified_addr(&net_ping_ip6
)) {
1492 puts("*** ERROR: ping address not given\n");
1497 #if defined(CONFIG_CMD_DNS)
1499 if (net_dns_server
.s_addr
== 0) {
1500 puts("*** ERROR: DNS server address not given\n");
1505 #if defined(CONFIG_PROT_UDP)
1512 #if defined(CONFIG_CMD_NFS)
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");
1525 } else if (net_server_ip
.s_addr
== 0 && !is_serverip_in_cmd()) {
1526 puts("*** ERROR: `serverip' not set\n");
1529 #if defined(CONFIG_CMD_PING) || \
1530 defined(CONFIG_CMD_DNS) || defined(CONFIG_PROT_UDP)
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");
1545 } else if (net_ip
.s_addr
== 0) {
1546 puts("*** ERROR: `ipaddr' not set\n");
1551 #ifdef CONFIG_CMD_RARP
1554 #ifdef CONFIG_PHY_NCSI
1561 if (memcmp(net_ethaddr
, "\0\0\0\0\0\0", 6) == 0) {
1562 int num
= eth_get_dev_index();
1566 puts("*** ERROR: No ethernet found.\n");
1569 puts("*** ERROR: `ethaddr' not set\n");
1572 printf("*** ERROR: `eth%daddr' not set\n",
1586 /**********************************************************************/
1589 net_eth_hdr_size(void)
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
;
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
;
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
)
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
;
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) */
1660 ip
->ip_len
= htons(pkt_len
);
1662 ip
->ip_id
= htons(net_ip_id
++);
1663 ip
->ip_off
= htons(IP_FLAGS_DFRAG
); /* Don't fragment */
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
,
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
1685 pkt
[IP_UDP_HDR_SIZE
+ len
] = 0;
1687 net_set_ip_header(pkt
, dest
, net_ip
, IP_UDP_HDR_SIZE
+ len
,
1690 ip
->udp_src
= htons(sport
);
1691 ip
->udp_dst
= htons(dport
);
1692 ip
->udp_len
= htons(UDP_HDR_SIZE
+ len
);
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
)
1707 if (net_boot_file_name
[0] == '\0')
1710 colon
= strchr(net_boot_file_name
, ':');
1712 ip
= string_to_ip(net_boot_file_name
);
1713 if (ipaddr
&& ip
.s_addr
)
1717 strncpy(filename
, colon
+ 1, max_len
);
1719 strncpy(filename
, net_boot_file_name
, max_len
);
1721 filename
[max_len
- 1] = '\0';
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
)
1741 if (x
== (ushort
)-1)
1747 sprintf(s
, "%d", x
& VLAN_IDMASK
);
1750 ushort
string_to_vlan(const char *s
)
1755 return htons(VLAN_NONE
);
1757 if (*s
< '0' || *s
> '9')
1760 id
= (ushort
)dectoul(s
, NULL
);
1765 ushort
env_get_vlan(char *var
)
1767 return string_to_vlan(env_get(var
));