2 * Copied from Linux Monitor (LiMon) - Networking.
4 * Copyright 1994 - 2000 Neil Russell.
6 * Copyright 2000 Roland Borde
7 * Copyright 2000 Paolo Scaffardi
8 * Copyright 2000-2002 Wolfgang Denk, wd@denx.de
9 * SPDX-License-Identifier: GPL-2.0
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
77 * Prerequisites: - own ethernet address
79 * We want: - network time
86 #include <environment.h>
88 #if defined(CONFIG_STATUS_LED)
90 #include <status_led.h>
93 #include <linux/compiler.h>
97 #if defined(CONFIG_CMD_DNS)
100 #include "link_local.h"
104 #if defined(CONFIG_CMD_SNTP)
109 DECLARE_GLOBAL_DATA_PTR;
111 /** BOOTP EXTENTIONS **/
113 /* Our subnet mask (0=unknown) */
114 IPaddr_t NetOurSubnetMask;
115 /* Our gateways IP address */
116 IPaddr_t NetOurGatewayIP;
117 /* Our DNS IP address */
118 IPaddr_t NetOurDNSIP;
119 #if defined(CONFIG_BOOTP_DNS2)
120 /* Our 2nd DNS IP address */
121 IPaddr_t NetOurDNS2IP;
124 char NetOurNISDomain[32] = {0,};
126 char NetOurHostName[32] = {0,};
128 char NetOurRootPath[64] = {0,};
129 /* Our bootfile size in blocks */
130 ushort NetBootFileSize;
132 #ifdef CONFIG_MCAST_TFTP /* Multicast TFTP */
136 /** END OF BOOTP EXTENTIONS **/
138 /* The actual transferred size of the bootfile (in bytes) */
139 ulong NetBootFileXferSize;
140 /* Our ethernet address */
141 uchar NetOurEther[6];
142 /* Boot server enet address */
143 uchar NetServerEther[6];
144 /* Our IP addr (0 = unknown) */
146 /* Server IP addr (0 = unknown) */
147 IPaddr_t NetServerIP;
148 /* Current receive packet */
150 /* Current rx packet length */
154 /* Ethernet bcast address */
155 uchar NetBcastAddr[6] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
156 uchar NetEtherNullAddr[6];
158 void (*push_packet)(void *, int len) = 0;
160 /* Network loop state */
161 enum net_loop_state net_state;
162 /* Tried all network devices */
164 /* Network loop restarted */
165 static int NetRestarted;
166 /* At least one device configured */
167 static int NetDevExists;
169 /* XXX in both little & big endian machines 0xFFFF == ntohs(-1) */
170 /* default is without VLAN */
171 ushort NetOurVLAN = 0xFFFF;
173 ushort NetOurNativeVLAN = 0xFFFF;
178 #if defined(CONFIG_CMD_SNTP)
179 /* NTP server IP address */
180 IPaddr_t NetNtpServerIP;
181 /* offset time from UTC */
185 static uchar PktBuf[(PKTBUFSRX+1) * PKTSIZE_ALIGN + PKTALIGN];
188 uchar *NetRxPackets[PKTBUFSRX];
190 /* Current UDP RX packet handler */
191 static rxhand_f *udp_packet_handler;
192 /* Current ARP RX packet handler */
193 static rxhand_f *arp_packet_handler;
194 #ifdef CONFIG_CMD_TFTPPUT
195 /* Current ICMP rx handler */
196 static rxhand_icmp_f *packet_icmp_handler;
198 /* Current timeout handler */
199 static thand_f *timeHandler;
200 /* Time base value */
201 static ulong timeStart;
202 /* Current timeout value */
203 static ulong timeDelta;
204 /* THE transmit packet */
207 static int net_check_prereq(enum proto_t protocol);
209 static int NetTryCount;
211 int __maybe_unused net_busy_flag;
213 /**********************************************************************/
215 static int on_bootfile(const char *name, const char *value, enum env_op op,
220 case env_op_overwrite:
221 copy_filename(BootFile, value, sizeof(BootFile));
229 U_BOOT_ENV_CALLBACK(bootfile, on_bootfile);
232 * Check if autoload is enabled. If so, use either NFS or TFTP to download
235 void net_auto_load(void)
237 #if defined(CONFIG_CMD_NFS)
238 const char *s = getenv("autoload");
240 if (s != NULL && strcmp(s, "NFS") == 0) {
242 * Use NFS to load the bootfile.
248 if (getenv_yesno("autoload") == 0) {
250 * Just use BOOTP/RARP to configure system;
251 * Do not use TFTP to load the bootfile.
253 net_set_state(NETLOOP_SUCCESS);
259 static void NetInitLoop(void)
261 static int env_changed_id;
262 int env_id = get_env_id();
264 /* update only when the environment has changed */
265 if (env_changed_id != env_id) {
266 NetOurIP = getenv_IPaddr("ipaddr");
267 NetOurGatewayIP = getenv_IPaddr("gatewayip");
268 NetOurSubnetMask = getenv_IPaddr("netmask");
269 NetServerIP = getenv_IPaddr("serverip");
270 NetOurNativeVLAN = getenv_VLAN("nvlan");
271 NetOurVLAN = getenv_VLAN("vlan");
272 #if defined(CONFIG_CMD_DNS)
273 NetOurDNSIP = getenv_IPaddr("dnsip");
275 env_changed_id = env_id;
278 memcpy(NetOurEther, eth_get_dev()->enetaddr, 6);
283 static void net_clear_handlers(void)
285 net_set_udp_handler(NULL);
286 net_set_arp_handler(NULL);
287 NetSetTimeout(0, NULL);
290 static void net_cleanup_loop(void)
292 net_clear_handlers();
297 static int first_call = 1;
301 * Setup packet buffers, aligned correctly.
305 NetTxPacket = &PktBuf[0] + (PKTALIGN - 1);
306 NetTxPacket -= (ulong)NetTxPacket % PKTALIGN;
307 for (i = 0; i < PKTBUFSRX; i++)
308 NetRxPackets[i] = NetTxPacket + (i + 1) * PKTSIZE_ALIGN;
311 net_clear_handlers();
313 /* Only need to setup buffer pointers once. */
320 /**********************************************************************/
322 * Main network processing loop.
325 int NetLoop(enum proto_t protocol)
333 debug_cond(DEBUG_INT_STATE, "--- NetLoop Entry\n");
335 bootstage_mark_name(BOOTSTAGE_ID_ETH_START, "eth_start");
337 if (eth_is_on_demand_init() || protocol != NETCONS) {
340 if (eth_init(bd) < 0) {
345 eth_init_state_only(bd);
348 #ifdef CONFIG_USB_KEYBOARD
351 net_set_state(NETLOOP_CONTINUE);
354 * Start the ball rolling with the given start function. From
355 * here on, this code is a state machine driven by received
356 * packets and timer events.
358 debug_cond(DEBUG_INT_STATE, "--- NetLoop Init\n");
361 switch (net_check_prereq(protocol)) {
363 /* network not configured */
368 /* network device not configured */
373 NetBootFileXferSize = 0;
376 #ifdef CONFIG_CMD_TFTPPUT
379 /* always use ARP to get server ethernet address */
382 #ifdef CONFIG_CMD_TFTPSRV
387 #if defined(CONFIG_CMD_DHCP)
391 DhcpRequest(); /* Basically same as BOOTP */
401 #if defined(CONFIG_CMD_RARP)
408 #if defined(CONFIG_CMD_PING)
413 #if defined(CONFIG_CMD_NFS)
418 #if defined(CONFIG_CMD_CDP)
423 #if defined (CONFIG_NETCONSOLE) && !(CONFIG_SPL_BUILD)
428 #if defined(CONFIG_CMD_SNTP)
433 #if defined(CONFIG_CMD_DNS)
438 #if defined(CONFIG_CMD_LINK_LOCAL)
443 #if defined(CONFIG_CMD_BOOTCE)
455 #if defined(CONFIG_MII) || defined(CONFIG_CMD_MII)
456 #if defined(CONFIG_SYS_FAULT_ECHO_LINK_DOWN) && \
457 defined(CONFIG_STATUS_LED) && \
458 defined(STATUS_LED_RED)
460 * Echo the inverted link state to the fault LED.
462 if (miiphy_link(eth_get_dev()->name, CONFIG_SYS_FAULT_MII_ADDR))
463 status_led_set(STATUS_LED_RED, STATUS_LED_OFF);
465 status_led_set(STATUS_LED_RED, STATUS_LED_ON);
466 #endif /* CONFIG_SYS_FAULT_ECHO_LINK_DOWN, ... */
467 #endif /* CONFIG_MII, ... */
468 #ifdef CONFIG_USB_KEYBOARD
473 * Main packet reception loop. Loop receiving packets until
474 * someone sets `net_state' to a state that terminates.
478 #ifdef CONFIG_SHOW_ACTIVITY
482 * Check the ethernet for a new packet. The ethernet
483 * receive routine will process it.
488 * Abort if ctrl-c was pressed.
491 /* cancel any ARP that may not have completed */
492 NetArpWaitPacketIP = 0;
496 /* Invalidate the last protocol */
497 eth_set_last_protocol(BOOTP);
500 /* include a debug print as well incase the debug
501 messages are directed to stderr */
502 debug_cond(DEBUG_INT_STATE, "--- NetLoop Abort!\n");
509 * Check for a timeout, and run the timeout handler
512 if (timeHandler && ((get_timer(timeStart)) > timeDelta)) {
515 #if defined(CONFIG_MII) || defined(CONFIG_CMD_MII)
516 #if defined(CONFIG_SYS_FAULT_ECHO_LINK_DOWN) && \
517 defined(CONFIG_STATUS_LED) && \
518 defined(STATUS_LED_RED)
520 * Echo the inverted link state to the fault LED.
522 if (miiphy_link(eth_get_dev()->name,
523 CONFIG_SYS_FAULT_MII_ADDR)) {
524 status_led_set(STATUS_LED_RED, STATUS_LED_OFF);
526 status_led_set(STATUS_LED_RED, STATUS_LED_ON);
528 #endif /* CONFIG_SYS_FAULT_ECHO_LINK_DOWN, ... */
529 #endif /* CONFIG_MII, ... */
530 debug_cond(DEBUG_INT_STATE, "--- NetLoop timeout\n");
532 timeHandler = (thand_f *)0;
539 case NETLOOP_RESTART:
543 case NETLOOP_SUCCESS:
545 if (NetBootFileXferSize > 0) {
546 printf("Bytes transferred = %ld (%lx hex)\n",
548 NetBootFileXferSize);
549 setenv_hex("filesize", NetBootFileXferSize);
550 setenv_hex("fileaddr", load_addr);
552 if (protocol != NETCONS) {
555 eth_halt_state_only();
558 eth_set_last_protocol(protocol);
560 ret = NetBootFileXferSize;
561 debug_cond(DEBUG_INT_STATE, "--- NetLoop Success!\n");
566 /* Invalidate the last protocol */
567 eth_set_last_protocol(BOOTP);
568 debug_cond(DEBUG_INT_STATE, "--- NetLoop Fail!\n");
571 case NETLOOP_CONTINUE:
577 #ifdef CONFIG_USB_KEYBOARD
580 #ifdef CONFIG_CMD_TFTPPUT
581 /* Clear out the handlers */
582 net_set_udp_handler(NULL);
583 net_set_icmp_handler(NULL);
588 /**********************************************************************/
591 startAgainTimeout(void)
593 net_set_state(NETLOOP_RESTART);
596 void NetStartAgain(void)
599 int retry_forever = 0;
600 unsigned long retrycnt = 0;
602 nretry = getenv("netretry");
604 if (!strcmp(nretry, "yes"))
606 else if (!strcmp(nretry, "no"))
608 else if (!strcmp(nretry, "once"))
611 retrycnt = simple_strtoul(nretry, NULL, 0);
615 if ((!retry_forever) && (NetTryCount >= retrycnt)) {
617 net_set_state(NETLOOP_FAIL);
624 #if !defined(CONFIG_NET_DO_NOT_TRY_ANOTHER)
625 eth_try_another(!NetRestarted);
628 if (NetRestartWrap) {
631 NetSetTimeout(10000UL, startAgainTimeout);
632 net_set_udp_handler(NULL);
634 net_set_state(NETLOOP_FAIL);
637 net_set_state(NETLOOP_RESTART);
641 /**********************************************************************/
646 static void dummy_handler(uchar *pkt, unsigned dport,
647 IPaddr_t sip, unsigned sport,
652 rxhand_f *net_get_udp_handler(void)
654 return udp_packet_handler;
657 void net_set_udp_handler(rxhand_f *f)
659 debug_cond(DEBUG_INT_STATE, "--- NetLoop UDP handler set (%p)\n", f);
661 udp_packet_handler = dummy_handler;
663 udp_packet_handler = f;
666 rxhand_f *net_get_arp_handler(void)
668 return arp_packet_handler;
671 void net_set_arp_handler(rxhand_f *f)
673 debug_cond(DEBUG_INT_STATE, "--- NetLoop ARP handler set (%p)\n", f);
675 arp_packet_handler = dummy_handler;
677 arp_packet_handler = f;
680 #ifdef CONFIG_CMD_TFTPPUT
681 void net_set_icmp_handler(rxhand_icmp_f *f)
683 packet_icmp_handler = f;
688 NetSetTimeout(ulong iv, thand_f *f)
691 debug_cond(DEBUG_INT_STATE,
692 "--- NetLoop timeout handler cancelled\n");
693 timeHandler = (thand_f *)0;
695 debug_cond(DEBUG_INT_STATE,
696 "--- NetLoop timeout handler set (%p)\n", f);
698 timeStart = get_timer(0);
699 timeDelta = iv * CONFIG_SYS_HZ / 1000;
703 int NetSendUDPPacket(uchar *ether, IPaddr_t dest, int dport, int sport,
710 /* make sure the NetTxPacket is initialized (NetInit() was called) */
711 assert(NetTxPacket != NULL);
712 if (NetTxPacket == NULL)
715 /* convert to new style broadcast */
719 /* if broadcast, make the ether address a broadcast and don't do ARP */
720 if (dest == 0xFFFFFFFF)
721 ether = NetBcastAddr;
723 pkt = (uchar *)NetTxPacket;
725 eth_hdr_size = NetSetEther(pkt, ether, PROT_IP);
727 net_set_udp_header(pkt, dest, dport, sport, payload_len);
728 pkt_hdr_size = eth_hdr_size + IP_UDP_HDR_SIZE;
730 /* if MAC address was not discovered yet, do an ARP request */
731 if (memcmp(ether, NetEtherNullAddr, 6) == 0) {
732 debug_cond(DEBUG_DEV_PKT, "sending ARP for %pI4\n", &dest);
734 /* save the ip and eth addr for the packet to send after arp */
735 NetArpWaitPacketIP = dest;
736 NetArpWaitPacketMAC = ether;
738 /* size of the waiting packet */
739 NetArpWaitTxPacketSize = pkt_hdr_size + payload_len;
741 /* and do the ARP request */
743 NetArpWaitTimerStart = get_timer(0);
745 return 1; /* waiting */
747 debug_cond(DEBUG_DEV_PKT, "sending UDP to %pI4/%pM\n",
749 NetSendPacket(NetTxPacket, pkt_hdr_size + payload_len);
750 return 0; /* transmitted */
754 #ifdef CONFIG_IP_DEFRAG
756 * This function collects fragments in a single packet, according
757 * to the algorithm in RFC815. It returns NULL or the pointer to
758 * a complete packet, in static storage
760 #ifndef CONFIG_NET_MAXDEFRAG
761 #define CONFIG_NET_MAXDEFRAG 16384
764 * MAXDEFRAG, above, is chosen in the config file and is real data
765 * so we need to add the NFS overhead, which is more than TFTP.
766 * To use sizeof in the internal unnamed structures, we need a real
767 * instance (can't do "sizeof(struct rpc_t.u.reply))", unfortunately).
768 * The compiler doesn't complain nor allocates the actual structure
770 static struct rpc_t rpc_specimen;
771 #define IP_PKTSIZE (CONFIG_NET_MAXDEFRAG + sizeof(rpc_specimen.u.reply))
773 #define IP_MAXUDP (IP_PKTSIZE - IP_HDR_SIZE)
776 * this is the packet being assembled, either data or frag control.
777 * Fragments go by 8 bytes, so this union must be 8 bytes long
780 /* first_byte is address of this structure */
781 u16 last_byte; /* last byte in this hole + 1 (begin of next hole) */
782 u16 next_hole; /* index of next (in 8-b blocks), 0 == none */
783 u16 prev_hole; /* index of prev, 0 == none */
787 static struct ip_udp_hdr *__NetDefragment(struct ip_udp_hdr *ip, int *lenp)
789 static uchar pkt_buff[IP_PKTSIZE] __aligned(PKTALIGN);
790 static u16 first_hole, total_len;
791 struct hole *payload, *thisfrag, *h, *newh;
792 struct ip_udp_hdr *localip = (struct ip_udp_hdr *)pkt_buff;
793 uchar *indata = (uchar *)ip;
794 int offset8, start, len, done = 0;
795 u16 ip_off = ntohs(ip->ip_off);
797 /* payload starts after IP header, this fragment is in there */
798 payload = (struct hole *)(pkt_buff + IP_HDR_SIZE);
799 offset8 = (ip_off & IP_OFFS);
800 thisfrag = payload + offset8;
802 len = ntohs(ip->ip_len) - IP_HDR_SIZE;
804 if (start + len > IP_MAXUDP) /* fragment extends too far */
807 if (!total_len || localip->ip_id != ip->ip_id) {
808 /* new (or different) packet, reset structs */
810 payload[0].last_byte = ~0;
811 payload[0].next_hole = 0;
812 payload[0].prev_hole = 0;
814 /* any IP header will work, copy the first we received */
815 memcpy(localip, ip, IP_HDR_SIZE);
819 * What follows is the reassembly algorithm. We use the payload
820 * array as a linked list of hole descriptors, as each hole starts
821 * at a multiple of 8 bytes. However, last byte can be whatever value,
822 * so it is represented as byte count, not as 8-byte blocks.
825 h = payload + first_hole;
826 while (h->last_byte < start) {
828 /* no hole that far away */
831 h = payload + h->next_hole;
834 /* last fragment may be 1..7 bytes, the "+7" forces acceptance */
835 if (offset8 + ((len + 7) / 8) <= h - payload) {
836 /* no overlap with holes (dup fragment?) */
840 if (!(ip_off & IP_FLAGS_MFRAG)) {
841 /* no more fragmentss: truncate this (last) hole */
842 total_len = start + len;
843 h->last_byte = start + len;
847 * There is some overlap: fix the hole list. This code doesn't
848 * deal with a fragment that overlaps with two different holes
849 * (thus being a superset of a previously-received fragment).
852 if ((h >= thisfrag) && (h->last_byte <= start + len)) {
853 /* complete overlap with hole: remove hole */
854 if (!h->prev_hole && !h->next_hole) {
855 /* last remaining hole */
857 } else if (!h->prev_hole) {
859 first_hole = h->next_hole;
860 payload[h->next_hole].prev_hole = 0;
861 } else if (!h->next_hole) {
863 payload[h->prev_hole].next_hole = 0;
865 /* in the middle of the list */
866 payload[h->next_hole].prev_hole = h->prev_hole;
867 payload[h->prev_hole].next_hole = h->next_hole;
870 } else if (h->last_byte <= start + len) {
871 /* overlaps with final part of the hole: shorten this hole */
872 h->last_byte = start;
874 } else if (h >= thisfrag) {
875 /* overlaps with initial part of the hole: move this hole */
876 newh = thisfrag + (len / 8);
880 payload[h->next_hole].prev_hole = (h - payload);
882 payload[h->prev_hole].next_hole = (h - payload);
884 first_hole = (h - payload);
887 /* fragment sits in the middle: split the hole */
888 newh = thisfrag + (len / 8);
890 h->last_byte = start;
891 h->next_hole = (newh - payload);
892 newh->prev_hole = (h - payload);
894 payload[newh->next_hole].prev_hole = (newh - payload);
897 /* finally copy this fragment and possibly return whole packet */
898 memcpy((uchar *)thisfrag, indata + IP_HDR_SIZE, len);
902 localip->ip_len = htons(total_len);
903 *lenp = total_len + IP_HDR_SIZE;
907 static inline struct ip_udp_hdr *NetDefragment(struct ip_udp_hdr *ip, int *lenp)
909 u16 ip_off = ntohs(ip->ip_off);
910 if (!(ip_off & (IP_OFFS | IP_FLAGS_MFRAG)))
911 return ip; /* not a fragment */
912 return __NetDefragment(ip, lenp);
915 #else /* !CONFIG_IP_DEFRAG */
917 static inline struct ip_udp_hdr *NetDefragment(struct ip_udp_hdr *ip, int *lenp)
919 u16 ip_off = ntohs(ip->ip_off);
920 if (!(ip_off & (IP_OFFS | IP_FLAGS_MFRAG)))
921 return ip; /* not a fragment */
927 * Receive an ICMP packet. We deal with REDIRECT and PING here, and silently
930 * @parma ip IP packet containing the ICMP
932 static void receive_icmp(struct ip_udp_hdr *ip, int len,
933 IPaddr_t src_ip, struct ethernet_hdr *et)
935 struct icmp_hdr *icmph = (struct icmp_hdr *)&ip->udp_src;
937 switch (icmph->type) {
939 if (icmph->code != ICMP_REDIR_HOST)
941 printf(" ICMP Host Redirect to %pI4 ",
945 #if defined(CONFIG_CMD_PING)
946 ping_receive(et, ip, len);
948 #ifdef CONFIG_CMD_TFTPPUT
949 if (packet_icmp_handler)
950 packet_icmp_handler(icmph->type, icmph->code,
951 ntohs(ip->udp_dst), src_ip, ntohs(ip->udp_src),
952 icmph->un.data, ntohs(ip->udp_len));
959 NetReceive(uchar *inpkt, int len)
961 struct ethernet_hdr *et;
962 struct ip_udp_hdr *ip;
966 #if defined(CONFIG_CMD_CDP)
969 ushort cti = 0, vlanid = VLAN_NONE, myvlanid, mynvlanid;
971 debug_cond(DEBUG_NET_PKT, "packet received\n");
974 NetRxPacketLen = len;
975 et = (struct ethernet_hdr *)inpkt;
977 /* too small packet? */
978 if (len < ETHER_HDR_SIZE)
983 (*push_packet)(inpkt, len);
988 #if defined(CONFIG_CMD_CDP)
989 /* keep track if packet is CDP */
990 iscdp = is_cdp_packet(et->et_dest);
993 myvlanid = ntohs(NetOurVLAN);
994 if (myvlanid == (ushort)-1)
995 myvlanid = VLAN_NONE;
996 mynvlanid = ntohs(NetOurNativeVLAN);
997 if (mynvlanid == (ushort)-1)
998 mynvlanid = VLAN_NONE;
1000 eth_proto = ntohs(et->et_protlen);
1002 if (eth_proto < 1514) {
1003 struct e802_hdr *et802 = (struct e802_hdr *)et;
1005 * Got a 802.2 packet. Check the other protocol field.
1006 * XXX VLAN over 802.2+SNAP not implemented!
1008 eth_proto = ntohs(et802->et_prot);
1010 ip = (struct ip_udp_hdr *)(inpkt + E802_HDR_SIZE);
1011 len -= E802_HDR_SIZE;
1013 } else if (eth_proto != PROT_VLAN) { /* normal packet */
1014 ip = (struct ip_udp_hdr *)(inpkt + ETHER_HDR_SIZE);
1015 len -= ETHER_HDR_SIZE;
1017 } else { /* VLAN packet */
1018 struct vlan_ethernet_hdr *vet =
1019 (struct vlan_ethernet_hdr *)et;
1021 debug_cond(DEBUG_NET_PKT, "VLAN packet received\n");
1023 /* too small packet? */
1024 if (len < VLAN_ETHER_HDR_SIZE)
1027 /* if no VLAN active */
1028 if ((ntohs(NetOurVLAN) & VLAN_IDMASK) == VLAN_NONE
1029 #if defined(CONFIG_CMD_CDP)
1035 cti = ntohs(vet->vet_tag);
1036 vlanid = cti & VLAN_IDMASK;
1037 eth_proto = ntohs(vet->vet_type);
1039 ip = (struct ip_udp_hdr *)(inpkt + VLAN_ETHER_HDR_SIZE);
1040 len -= VLAN_ETHER_HDR_SIZE;
1043 debug_cond(DEBUG_NET_PKT, "Receive from protocol 0x%x\n", eth_proto);
1045 #if defined(CONFIG_CMD_CDP)
1047 cdp_receive((uchar *)ip, len);
1052 if ((myvlanid & VLAN_IDMASK) != VLAN_NONE) {
1053 if (vlanid == VLAN_NONE)
1054 vlanid = (mynvlanid & VLAN_IDMASK);
1056 if (vlanid != (myvlanid & VLAN_IDMASK))
1060 switch (eth_proto) {
1063 ArpReceive(et, ip, len);
1066 #ifdef CONFIG_CMD_RARP
1068 rarp_receive(ip, len);
1072 debug_cond(DEBUG_NET_PKT, "Got IP\n");
1073 /* Before we start poking the header, make sure it is there */
1074 if (len < IP_UDP_HDR_SIZE) {
1075 debug("len bad %d < %lu\n", len,
1076 (ulong)IP_UDP_HDR_SIZE);
1079 /* Check the packet length */
1080 if (len < ntohs(ip->ip_len)) {
1081 debug("len bad %d < %d\n", len, ntohs(ip->ip_len));
1084 len = ntohs(ip->ip_len);
1085 debug_cond(DEBUG_NET_PKT, "len=%d, v=%02x\n",
1086 len, ip->ip_hl_v & 0xff);
1088 /* Can't deal with anything except IPv4 */
1089 if ((ip->ip_hl_v & 0xf0) != 0x40)
1091 /* Can't deal with IP options (headers != 20 bytes) */
1092 if ((ip->ip_hl_v & 0x0f) > 0x05)
1094 /* Check the Checksum of the header */
1095 if (!NetCksumOk((uchar *)ip, IP_HDR_SIZE / 2)) {
1096 debug("checksum bad\n");
1099 /* If it is not for us, ignore it */
1100 dst_ip = NetReadIP(&ip->ip_dst);
1101 if (NetOurIP && dst_ip != NetOurIP && dst_ip != 0xFFFFFFFF) {
1102 #ifdef CONFIG_MCAST_TFTP
1103 if (Mcast_addr != dst_ip)
1107 /* Read source IP address for later use */
1108 src_ip = NetReadIP(&ip->ip_src);
1110 * The function returns the unchanged packet if it's not
1111 * a fragment, and either the complete packet or NULL if
1112 * it is a fragment (if !CONFIG_IP_DEFRAG, it returns NULL)
1114 ip = NetDefragment(ip, &len);
1118 * watch for ICMP host redirects
1120 * There is no real handler code (yet). We just watch
1121 * for ICMP host redirect messages. In case anybody
1122 * sees these messages: please contact me
1123 * (wd@denx.de), or - even better - send me the
1124 * necessary fixes :-)
1126 * Note: in all cases where I have seen this so far
1127 * it was a problem with the router configuration,
1128 * for instance when a router was configured in the
1129 * BOOTP reply, but the TFTP server was on the same
1130 * subnet. So this is probably a warning that your
1131 * configuration might be wrong. But I'm not really
1132 * sure if there aren't any other situations.
1134 * Simon Glass <sjg@chromium.org>: We get an ICMP when
1135 * we send a tftp packet to a dead connection, or when
1136 * there is no server at the other end.
1138 if (ip->ip_p == IPPROTO_ICMP) {
1139 receive_icmp(ip, len, src_ip, et);
1141 } else if (ip->ip_p != IPPROTO_UDP) { /* Only UDP packets */
1145 debug_cond(DEBUG_DEV_PKT,
1146 "received UDP (to=%pI4, from=%pI4, len=%d)\n",
1147 &dst_ip, &src_ip, len);
1149 #ifdef CONFIG_UDP_CHECKSUM
1150 if (ip->udp_xsum != 0) {
1156 xsum += (ntohs(ip->udp_len));
1157 xsum += (ntohl(ip->ip_src) >> 16) & 0x0000ffff;
1158 xsum += (ntohl(ip->ip_src) >> 0) & 0x0000ffff;
1159 xsum += (ntohl(ip->ip_dst) >> 16) & 0x0000ffff;
1160 xsum += (ntohl(ip->ip_dst) >> 0) & 0x0000ffff;
1162 sumlen = ntohs(ip->udp_len);
1163 sumptr = (ushort *) &(ip->udp_src);
1165 while (sumlen > 1) {
1168 sumdata = *sumptr++;
1169 xsum += ntohs(sumdata);
1175 sumdata = *(unsigned char *) sumptr;
1176 sumdata = (sumdata << 8) & 0xff00;
1179 while ((xsum >> 16) != 0) {
1180 xsum = (xsum & 0x0000ffff) +
1181 ((xsum >> 16) & 0x0000ffff);
1183 if ((xsum != 0x00000000) && (xsum != 0x0000ffff)) {
1184 printf(" UDP wrong checksum %08lx %08x\n",
1185 xsum, ntohs(ip->udp_xsum));
1192 #if defined (CONFIG_NETCONSOLE) && !(CONFIG_SPL_BUILD)
1193 nc_input_packet((uchar *)ip + IP_UDP_HDR_SIZE,
1197 ntohs(ip->udp_len) - UDP_HDR_SIZE);
1200 * IP header OK. Pass the packet to the current handler.
1202 (*udp_packet_handler)((uchar *)ip + IP_UDP_HDR_SIZE,
1206 ntohs(ip->udp_len) - UDP_HDR_SIZE);
1212 /**********************************************************************/
1214 static int net_check_prereq(enum proto_t protocol)
1217 #if defined(CONFIG_CMD_PING)
1219 if (NetPingIP == 0) {
1220 puts("*** ERROR: ping address not given\n");
1225 #if defined(CONFIG_CMD_SNTP)
1227 if (NetNtpServerIP == 0) {
1228 puts("*** ERROR: NTP server address not given\n");
1233 #if defined(CONFIG_CMD_DNS)
1235 if (NetOurDNSIP == 0) {
1236 puts("*** ERROR: DNS server address not given\n");
1241 #if defined(CONFIG_CMD_NFS)
1246 if (NetServerIP == 0) {
1247 puts("*** ERROR: `serverip' not set\n");
1250 #if defined(CONFIG_CMD_PING) || defined(CONFIG_CMD_SNTP) || \
1251 defined(CONFIG_CMD_DNS)
1259 if (NetOurIP == 0) {
1260 puts("*** ERROR: `ipaddr' not set\n");
1265 #ifdef CONFIG_CMD_RARP
1272 if (memcmp(NetOurEther, "\0\0\0\0\0\0", 6) == 0) {
1273 int num = eth_get_dev_index();
1277 puts("*** ERROR: No ethernet found.\n");
1280 puts("*** ERROR: `ethaddr' not set\n");
1283 printf("*** ERROR: `eth%daddr' not set\n",
1297 /**********************************************************************/
1300 NetCksumOk(uchar *ptr, int len)
1302 return !((NetCksum(ptr, len) + 1) & 0xfffe);
1307 NetCksum(uchar *ptr, int len)
1310 ushort *p = (ushort *)ptr;
1315 xsum = (xsum & 0xffff) + (xsum >> 16);
1316 xsum = (xsum & 0xffff) + (xsum >> 16);
1317 return xsum & 0xffff;
1325 myvlanid = ntohs(NetOurVLAN);
1326 if (myvlanid == (ushort)-1)
1327 myvlanid = VLAN_NONE;
1329 return ((myvlanid & VLAN_IDMASK) == VLAN_NONE) ? ETHER_HDR_SIZE :
1330 VLAN_ETHER_HDR_SIZE;
1334 NetSetEther(uchar *xet, uchar * addr, uint prot)
1336 struct ethernet_hdr *et = (struct ethernet_hdr *)xet;
1339 myvlanid = ntohs(NetOurVLAN);
1340 if (myvlanid == (ushort)-1)
1341 myvlanid = VLAN_NONE;
1343 memcpy(et->et_dest, addr, 6);
1344 memcpy(et->et_src, NetOurEther, 6);
1345 if ((myvlanid & VLAN_IDMASK) == VLAN_NONE) {
1346 et->et_protlen = htons(prot);
1347 return ETHER_HDR_SIZE;
1349 struct vlan_ethernet_hdr *vet =
1350 (struct vlan_ethernet_hdr *)xet;
1352 vet->vet_vlan_type = htons(PROT_VLAN);
1353 vet->vet_tag = htons((0 << 5) | (myvlanid & VLAN_IDMASK));
1354 vet->vet_type = htons(prot);
1355 return VLAN_ETHER_HDR_SIZE;
1359 int net_update_ether(struct ethernet_hdr *et, uchar *addr, uint prot)
1363 memcpy(et->et_dest, addr, 6);
1364 memcpy(et->et_src, NetOurEther, 6);
1365 protlen = ntohs(et->et_protlen);
1366 if (protlen == PROT_VLAN) {
1367 struct vlan_ethernet_hdr *vet =
1368 (struct vlan_ethernet_hdr *)et;
1369 vet->vet_type = htons(prot);
1370 return VLAN_ETHER_HDR_SIZE;
1371 } else if (protlen > 1514) {
1372 et->et_protlen = htons(prot);
1373 return ETHER_HDR_SIZE;
1376 struct e802_hdr *et802 = (struct e802_hdr *)et;
1377 et802->et_prot = htons(prot);
1378 return E802_HDR_SIZE;
1382 void net_set_ip_header(uchar *pkt, IPaddr_t dest, IPaddr_t source)
1384 struct ip_udp_hdr *ip = (struct ip_udp_hdr *)pkt;
1387 * Construct an IP header.
1389 /* IP_HDR_SIZE / 4 (not including UDP) */
1392 ip->ip_len = htons(IP_HDR_SIZE);
1393 ip->ip_id = htons(NetIPID++);
1394 ip->ip_off = htons(IP_FLAGS_DFRAG); /* Don't fragment */
1397 /* already in network byte order */
1398 NetCopyIP((void *)&ip->ip_src, &source);
1399 /* already in network byte order */
1400 NetCopyIP((void *)&ip->ip_dst, &dest);
1403 void net_set_udp_header(uchar *pkt, IPaddr_t dest, int dport, int sport,
1406 struct ip_udp_hdr *ip = (struct ip_udp_hdr *)pkt;
1409 * If the data is an odd number of bytes, zero the
1410 * byte after the last byte so that the checksum
1414 pkt[IP_UDP_HDR_SIZE + len] = 0;
1416 net_set_ip_header(pkt, dest, NetOurIP);
1417 ip->ip_len = htons(IP_UDP_HDR_SIZE + len);
1418 ip->ip_p = IPPROTO_UDP;
1419 ip->ip_sum = ~NetCksum((uchar *)ip, IP_HDR_SIZE >> 1);
1421 ip->udp_src = htons(sport);
1422 ip->udp_dst = htons(dport);
1423 ip->udp_len = htons(UDP_HDR_SIZE + len);
1427 void copy_filename(char *dst, const char *src, int size)
1429 if (*src && (*src == '"')) {
1434 while ((--size > 0) && *src && (*src != '"'))
1439 #if defined(CONFIG_CMD_NFS) || \
1440 defined(CONFIG_CMD_SNTP) || \
1441 defined(CONFIG_CMD_DNS)
1443 * make port a little random (1024-17407)
1444 * This keeps the math somewhat trivial to compute, and seems to work with
1445 * all supported protocols/clients/servers
1447 unsigned int random_port(void)
1449 return 1024 + (get_timer(0) % 0x4000);
1453 void ip_to_string(IPaddr_t x, char *s)
1456 sprintf(s, "%d.%d.%d.%d",
1457 (int) ((x >> 24) & 0xff),
1458 (int) ((x >> 16) & 0xff),
1459 (int) ((x >> 8) & 0xff), (int) ((x >> 0) & 0xff)
1463 void VLAN_to_string(ushort x, char *s)
1467 if (x == (ushort)-1)
1473 sprintf(s, "%d", x & VLAN_IDMASK);
1476 ushort string_to_VLAN(const char *s)
1481 return htons(VLAN_NONE);
1483 if (*s < '0' || *s > '9')
1486 id = (ushort)simple_strtoul(s, NULL, 10);
1491 ushort getenv_VLAN(char *var)
1493 return string_to_VLAN(getenv(var));