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_ethaddr(), 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)
332 debug_cond(DEBUG_INT_STATE, "--- NetLoop Entry\n");
334 bootstage_mark_name(BOOTSTAGE_ID_ETH_START, "eth_start");
336 if (eth_is_on_demand_init() || protocol != NETCONS) {
339 if (eth_init() < 0) {
344 eth_init_state_only();
347 #ifdef CONFIG_USB_KEYBOARD
350 net_set_state(NETLOOP_CONTINUE);
353 * Start the ball rolling with the given start function. From
354 * here on, this code is a state machine driven by received
355 * packets and timer events.
357 debug_cond(DEBUG_INT_STATE, "--- NetLoop Init\n");
360 switch (net_check_prereq(protocol)) {
362 /* network not configured */
367 /* network device not configured */
372 NetBootFileXferSize = 0;
375 #ifdef CONFIG_CMD_TFTPPUT
378 /* always use ARP to get server ethernet address */
381 #ifdef CONFIG_CMD_TFTPSRV
386 #if defined(CONFIG_CMD_DHCP)
390 DhcpRequest(); /* Basically same as BOOTP */
400 #if defined(CONFIG_CMD_RARP)
407 #if defined(CONFIG_CMD_PING)
412 #if defined(CONFIG_CMD_NFS)
417 #if defined(CONFIG_CMD_CDP)
422 #if defined (CONFIG_NETCONSOLE) && !(CONFIG_SPL_BUILD)
427 #if defined(CONFIG_CMD_SNTP)
432 #if defined(CONFIG_CMD_DNS)
437 #if defined(CONFIG_CMD_LINK_LOCAL)
449 #if defined(CONFIG_MII) || defined(CONFIG_CMD_MII)
450 #if defined(CONFIG_SYS_FAULT_ECHO_LINK_DOWN) && \
451 defined(CONFIG_STATUS_LED) && \
452 defined(STATUS_LED_RED)
454 * Echo the inverted link state to the fault LED.
456 if (miiphy_link(eth_get_dev()->name, CONFIG_SYS_FAULT_MII_ADDR))
457 status_led_set(STATUS_LED_RED, STATUS_LED_OFF);
459 status_led_set(STATUS_LED_RED, STATUS_LED_ON);
460 #endif /* CONFIG_SYS_FAULT_ECHO_LINK_DOWN, ... */
461 #endif /* CONFIG_MII, ... */
462 #ifdef CONFIG_USB_KEYBOARD
467 * Main packet reception loop. Loop receiving packets until
468 * someone sets `net_state' to a state that terminates.
472 #ifdef CONFIG_SHOW_ACTIVITY
476 * Check the ethernet for a new packet. The ethernet
477 * receive routine will process it.
482 * Abort if ctrl-c was pressed.
485 /* cancel any ARP that may not have completed */
486 NetArpWaitPacketIP = 0;
490 /* Invalidate the last protocol */
491 eth_set_last_protocol(BOOTP);
494 /* include a debug print as well incase the debug
495 messages are directed to stderr */
496 debug_cond(DEBUG_INT_STATE, "--- NetLoop Abort!\n");
503 * Check for a timeout, and run the timeout handler
506 if (timeHandler && ((get_timer(0) - timeStart) > timeDelta)) {
509 #if defined(CONFIG_MII) || defined(CONFIG_CMD_MII)
510 #if defined(CONFIG_SYS_FAULT_ECHO_LINK_DOWN) && \
511 defined(CONFIG_STATUS_LED) && \
512 defined(STATUS_LED_RED)
514 * Echo the inverted link state to the fault LED.
516 if (miiphy_link(eth_get_dev()->name,
517 CONFIG_SYS_FAULT_MII_ADDR)) {
518 status_led_set(STATUS_LED_RED, STATUS_LED_OFF);
520 status_led_set(STATUS_LED_RED, STATUS_LED_ON);
522 #endif /* CONFIG_SYS_FAULT_ECHO_LINK_DOWN, ... */
523 #endif /* CONFIG_MII, ... */
524 debug_cond(DEBUG_INT_STATE, "--- NetLoop timeout\n");
526 timeHandler = (thand_f *)0;
530 if (net_state == NETLOOP_FAIL)
535 case NETLOOP_RESTART:
539 case NETLOOP_SUCCESS:
541 if (NetBootFileXferSize > 0) {
542 printf("Bytes transferred = %ld (%lx hex)\n",
544 NetBootFileXferSize);
545 setenv_hex("filesize", NetBootFileXferSize);
546 setenv_hex("fileaddr", load_addr);
548 if (protocol != NETCONS)
551 eth_halt_state_only();
553 eth_set_last_protocol(protocol);
555 ret = NetBootFileXferSize;
556 debug_cond(DEBUG_INT_STATE, "--- NetLoop Success!\n");
561 /* Invalidate the last protocol */
562 eth_set_last_protocol(BOOTP);
563 debug_cond(DEBUG_INT_STATE, "--- NetLoop Fail!\n");
566 case NETLOOP_CONTINUE:
572 #ifdef CONFIG_USB_KEYBOARD
575 #ifdef CONFIG_CMD_TFTPPUT
576 /* Clear out the handlers */
577 net_set_udp_handler(NULL);
578 net_set_icmp_handler(NULL);
583 /**********************************************************************/
586 startAgainTimeout(void)
588 net_set_state(NETLOOP_RESTART);
591 void NetStartAgain(void)
594 int retry_forever = 0;
595 unsigned long retrycnt = 0;
597 nretry = getenv("netretry");
599 if (!strcmp(nretry, "yes"))
601 else if (!strcmp(nretry, "no"))
603 else if (!strcmp(nretry, "once"))
606 retrycnt = simple_strtoul(nretry, NULL, 0);
612 if ((!retry_forever) && (NetTryCount >= retrycnt)) {
614 net_set_state(NETLOOP_FAIL);
621 #if !defined(CONFIG_NET_DO_NOT_TRY_ANOTHER)
622 eth_try_another(!NetRestarted);
625 if (NetRestartWrap) {
628 NetSetTimeout(10000UL, startAgainTimeout);
629 net_set_udp_handler(NULL);
631 net_set_state(NETLOOP_FAIL);
634 net_set_state(NETLOOP_RESTART);
638 /**********************************************************************/
643 static void dummy_handler(uchar *pkt, unsigned dport,
644 IPaddr_t sip, unsigned sport,
649 rxhand_f *net_get_udp_handler(void)
651 return udp_packet_handler;
654 void net_set_udp_handler(rxhand_f *f)
656 debug_cond(DEBUG_INT_STATE, "--- NetLoop UDP handler set (%p)\n", f);
658 udp_packet_handler = dummy_handler;
660 udp_packet_handler = f;
663 rxhand_f *net_get_arp_handler(void)
665 return arp_packet_handler;
668 void net_set_arp_handler(rxhand_f *f)
670 debug_cond(DEBUG_INT_STATE, "--- NetLoop ARP handler set (%p)\n", f);
672 arp_packet_handler = dummy_handler;
674 arp_packet_handler = f;
677 #ifdef CONFIG_CMD_TFTPPUT
678 void net_set_icmp_handler(rxhand_icmp_f *f)
680 packet_icmp_handler = f;
685 NetSetTimeout(ulong iv, thand_f *f)
688 debug_cond(DEBUG_INT_STATE,
689 "--- NetLoop timeout handler cancelled\n");
690 timeHandler = (thand_f *)0;
692 debug_cond(DEBUG_INT_STATE,
693 "--- NetLoop timeout handler set (%p)\n", f);
695 timeStart = get_timer(0);
696 timeDelta = iv * CONFIG_SYS_HZ / 1000;
700 int NetSendUDPPacket(uchar *ether, IPaddr_t dest, int dport, int sport,
707 /* make sure the NetTxPacket is initialized (NetInit() was called) */
708 assert(NetTxPacket != NULL);
709 if (NetTxPacket == NULL)
712 /* convert to new style broadcast */
716 /* if broadcast, make the ether address a broadcast and don't do ARP */
717 if (dest == 0xFFFFFFFF)
718 ether = NetBcastAddr;
720 pkt = (uchar *)NetTxPacket;
722 eth_hdr_size = NetSetEther(pkt, ether, PROT_IP);
724 net_set_udp_header(pkt, dest, dport, sport, payload_len);
725 pkt_hdr_size = eth_hdr_size + IP_UDP_HDR_SIZE;
727 /* if MAC address was not discovered yet, do an ARP request */
728 if (memcmp(ether, NetEtherNullAddr, 6) == 0) {
729 debug_cond(DEBUG_DEV_PKT, "sending ARP for %pI4\n", &dest);
731 /* save the ip and eth addr for the packet to send after arp */
732 NetArpWaitPacketIP = dest;
733 NetArpWaitPacketMAC = ether;
735 /* size of the waiting packet */
736 NetArpWaitTxPacketSize = pkt_hdr_size + payload_len;
738 /* and do the ARP request */
740 NetArpWaitTimerStart = get_timer(0);
742 return 1; /* waiting */
744 debug_cond(DEBUG_DEV_PKT, "sending UDP to %pI4/%pM\n",
746 NetSendPacket(NetTxPacket, pkt_hdr_size + payload_len);
747 return 0; /* transmitted */
751 #ifdef CONFIG_IP_DEFRAG
753 * This function collects fragments in a single packet, according
754 * to the algorithm in RFC815. It returns NULL or the pointer to
755 * a complete packet, in static storage
757 #ifndef CONFIG_NET_MAXDEFRAG
758 #define CONFIG_NET_MAXDEFRAG 16384
761 * MAXDEFRAG, above, is chosen in the config file and is real data
762 * so we need to add the NFS overhead, which is more than TFTP.
763 * To use sizeof in the internal unnamed structures, we need a real
764 * instance (can't do "sizeof(struct rpc_t.u.reply))", unfortunately).
765 * The compiler doesn't complain nor allocates the actual structure
767 static struct rpc_t rpc_specimen;
768 #define IP_PKTSIZE (CONFIG_NET_MAXDEFRAG + sizeof(rpc_specimen.u.reply))
770 #define IP_MAXUDP (IP_PKTSIZE - IP_HDR_SIZE)
773 * this is the packet being assembled, either data or frag control.
774 * Fragments go by 8 bytes, so this union must be 8 bytes long
777 /* first_byte is address of this structure */
778 u16 last_byte; /* last byte in this hole + 1 (begin of next hole) */
779 u16 next_hole; /* index of next (in 8-b blocks), 0 == none */
780 u16 prev_hole; /* index of prev, 0 == none */
784 static struct ip_udp_hdr *__NetDefragment(struct ip_udp_hdr *ip, int *lenp)
786 static uchar pkt_buff[IP_PKTSIZE] __aligned(PKTALIGN);
787 static u16 first_hole, total_len;
788 struct hole *payload, *thisfrag, *h, *newh;
789 struct ip_udp_hdr *localip = (struct ip_udp_hdr *)pkt_buff;
790 uchar *indata = (uchar *)ip;
791 int offset8, start, len, done = 0;
792 u16 ip_off = ntohs(ip->ip_off);
794 /* payload starts after IP header, this fragment is in there */
795 payload = (struct hole *)(pkt_buff + IP_HDR_SIZE);
796 offset8 = (ip_off & IP_OFFS);
797 thisfrag = payload + offset8;
799 len = ntohs(ip->ip_len) - IP_HDR_SIZE;
801 if (start + len > IP_MAXUDP) /* fragment extends too far */
804 if (!total_len || localip->ip_id != ip->ip_id) {
805 /* new (or different) packet, reset structs */
807 payload[0].last_byte = ~0;
808 payload[0].next_hole = 0;
809 payload[0].prev_hole = 0;
811 /* any IP header will work, copy the first we received */
812 memcpy(localip, ip, IP_HDR_SIZE);
816 * What follows is the reassembly algorithm. We use the payload
817 * array as a linked list of hole descriptors, as each hole starts
818 * at a multiple of 8 bytes. However, last byte can be whatever value,
819 * so it is represented as byte count, not as 8-byte blocks.
822 h = payload + first_hole;
823 while (h->last_byte < start) {
825 /* no hole that far away */
828 h = payload + h->next_hole;
831 /* last fragment may be 1..7 bytes, the "+7" forces acceptance */
832 if (offset8 + ((len + 7) / 8) <= h - payload) {
833 /* no overlap with holes (dup fragment?) */
837 if (!(ip_off & IP_FLAGS_MFRAG)) {
838 /* no more fragmentss: truncate this (last) hole */
839 total_len = start + len;
840 h->last_byte = start + len;
844 * There is some overlap: fix the hole list. This code doesn't
845 * deal with a fragment that overlaps with two different holes
846 * (thus being a superset of a previously-received fragment).
849 if ((h >= thisfrag) && (h->last_byte <= start + len)) {
850 /* complete overlap with hole: remove hole */
851 if (!h->prev_hole && !h->next_hole) {
852 /* last remaining hole */
854 } else if (!h->prev_hole) {
856 first_hole = h->next_hole;
857 payload[h->next_hole].prev_hole = 0;
858 } else if (!h->next_hole) {
860 payload[h->prev_hole].next_hole = 0;
862 /* in the middle of the list */
863 payload[h->next_hole].prev_hole = h->prev_hole;
864 payload[h->prev_hole].next_hole = h->next_hole;
867 } else if (h->last_byte <= start + len) {
868 /* overlaps with final part of the hole: shorten this hole */
869 h->last_byte = start;
871 } else if (h >= thisfrag) {
872 /* overlaps with initial part of the hole: move this hole */
873 newh = thisfrag + (len / 8);
877 payload[h->next_hole].prev_hole = (h - payload);
879 payload[h->prev_hole].next_hole = (h - payload);
881 first_hole = (h - payload);
884 /* fragment sits in the middle: split the hole */
885 newh = thisfrag + (len / 8);
887 h->last_byte = start;
888 h->next_hole = (newh - payload);
889 newh->prev_hole = (h - payload);
891 payload[newh->next_hole].prev_hole = (newh - payload);
894 /* finally copy this fragment and possibly return whole packet */
895 memcpy((uchar *)thisfrag, indata + IP_HDR_SIZE, len);
899 localip->ip_len = htons(total_len);
900 *lenp = total_len + IP_HDR_SIZE;
904 static inline struct ip_udp_hdr *NetDefragment(struct ip_udp_hdr *ip, int *lenp)
906 u16 ip_off = ntohs(ip->ip_off);
907 if (!(ip_off & (IP_OFFS | IP_FLAGS_MFRAG)))
908 return ip; /* not a fragment */
909 return __NetDefragment(ip, lenp);
912 #else /* !CONFIG_IP_DEFRAG */
914 static inline struct ip_udp_hdr *NetDefragment(struct ip_udp_hdr *ip, int *lenp)
916 u16 ip_off = ntohs(ip->ip_off);
917 if (!(ip_off & (IP_OFFS | IP_FLAGS_MFRAG)))
918 return ip; /* not a fragment */
924 * Receive an ICMP packet. We deal with REDIRECT and PING here, and silently
927 * @parma ip IP packet containing the ICMP
929 static void receive_icmp(struct ip_udp_hdr *ip, int len,
930 IPaddr_t src_ip, struct ethernet_hdr *et)
932 struct icmp_hdr *icmph = (struct icmp_hdr *)&ip->udp_src;
934 switch (icmph->type) {
936 if (icmph->code != ICMP_REDIR_HOST)
938 printf(" ICMP Host Redirect to %pI4 ",
942 #if defined(CONFIG_CMD_PING)
943 ping_receive(et, ip, len);
945 #ifdef CONFIG_CMD_TFTPPUT
946 if (packet_icmp_handler)
947 packet_icmp_handler(icmph->type, icmph->code,
948 ntohs(ip->udp_dst), src_ip, ntohs(ip->udp_src),
949 icmph->un.data, ntohs(ip->udp_len));
956 NetReceive(uchar *inpkt, int len)
958 struct ethernet_hdr *et;
959 struct ip_udp_hdr *ip;
963 #if defined(CONFIG_CMD_CDP)
966 ushort cti = 0, vlanid = VLAN_NONE, myvlanid, mynvlanid;
968 debug_cond(DEBUG_NET_PKT, "packet received\n");
971 NetRxPacketLen = len;
972 et = (struct ethernet_hdr *)inpkt;
974 /* too small packet? */
975 if (len < ETHER_HDR_SIZE)
980 (*push_packet)(inpkt, len);
985 #if defined(CONFIG_CMD_CDP)
986 /* keep track if packet is CDP */
987 iscdp = is_cdp_packet(et->et_dest);
990 myvlanid = ntohs(NetOurVLAN);
991 if (myvlanid == (ushort)-1)
992 myvlanid = VLAN_NONE;
993 mynvlanid = ntohs(NetOurNativeVLAN);
994 if (mynvlanid == (ushort)-1)
995 mynvlanid = VLAN_NONE;
997 eth_proto = ntohs(et->et_protlen);
999 if (eth_proto < 1514) {
1000 struct e802_hdr *et802 = (struct e802_hdr *)et;
1002 * Got a 802.2 packet. Check the other protocol field.
1003 * XXX VLAN over 802.2+SNAP not implemented!
1005 eth_proto = ntohs(et802->et_prot);
1007 ip = (struct ip_udp_hdr *)(inpkt + E802_HDR_SIZE);
1008 len -= E802_HDR_SIZE;
1010 } else if (eth_proto != PROT_VLAN) { /* normal packet */
1011 ip = (struct ip_udp_hdr *)(inpkt + ETHER_HDR_SIZE);
1012 len -= ETHER_HDR_SIZE;
1014 } else { /* VLAN packet */
1015 struct vlan_ethernet_hdr *vet =
1016 (struct vlan_ethernet_hdr *)et;
1018 debug_cond(DEBUG_NET_PKT, "VLAN packet received\n");
1020 /* too small packet? */
1021 if (len < VLAN_ETHER_HDR_SIZE)
1024 /* if no VLAN active */
1025 if ((ntohs(NetOurVLAN) & VLAN_IDMASK) == VLAN_NONE
1026 #if defined(CONFIG_CMD_CDP)
1032 cti = ntohs(vet->vet_tag);
1033 vlanid = cti & VLAN_IDMASK;
1034 eth_proto = ntohs(vet->vet_type);
1036 ip = (struct ip_udp_hdr *)(inpkt + VLAN_ETHER_HDR_SIZE);
1037 len -= VLAN_ETHER_HDR_SIZE;
1040 debug_cond(DEBUG_NET_PKT, "Receive from protocol 0x%x\n", eth_proto);
1042 #if defined(CONFIG_CMD_CDP)
1044 cdp_receive((uchar *)ip, len);
1049 if ((myvlanid & VLAN_IDMASK) != VLAN_NONE) {
1050 if (vlanid == VLAN_NONE)
1051 vlanid = (mynvlanid & VLAN_IDMASK);
1053 if (vlanid != (myvlanid & VLAN_IDMASK))
1057 switch (eth_proto) {
1060 ArpReceive(et, ip, len);
1063 #ifdef CONFIG_CMD_RARP
1065 rarp_receive(ip, len);
1069 debug_cond(DEBUG_NET_PKT, "Got IP\n");
1070 /* Before we start poking the header, make sure it is there */
1071 if (len < IP_UDP_HDR_SIZE) {
1072 debug("len bad %d < %lu\n", len,
1073 (ulong)IP_UDP_HDR_SIZE);
1076 /* Check the packet length */
1077 if (len < ntohs(ip->ip_len)) {
1078 debug("len bad %d < %d\n", len, ntohs(ip->ip_len));
1081 len = ntohs(ip->ip_len);
1082 debug_cond(DEBUG_NET_PKT, "len=%d, v=%02x\n",
1083 len, ip->ip_hl_v & 0xff);
1085 /* Can't deal with anything except IPv4 */
1086 if ((ip->ip_hl_v & 0xf0) != 0x40)
1088 /* Can't deal with IP options (headers != 20 bytes) */
1089 if ((ip->ip_hl_v & 0x0f) > 0x05)
1091 /* Check the Checksum of the header */
1092 if (!ip_checksum_ok((uchar *)ip, IP_HDR_SIZE)) {
1093 debug("checksum bad\n");
1096 /* If it is not for us, ignore it */
1097 dst_ip = NetReadIP(&ip->ip_dst);
1098 if (NetOurIP && dst_ip != NetOurIP && dst_ip != 0xFFFFFFFF) {
1099 #ifdef CONFIG_MCAST_TFTP
1100 if (Mcast_addr != dst_ip)
1104 /* Read source IP address for later use */
1105 src_ip = NetReadIP(&ip->ip_src);
1107 * The function returns the unchanged packet if it's not
1108 * a fragment, and either the complete packet or NULL if
1109 * it is a fragment (if !CONFIG_IP_DEFRAG, it returns NULL)
1111 ip = NetDefragment(ip, &len);
1115 * watch for ICMP host redirects
1117 * There is no real handler code (yet). We just watch
1118 * for ICMP host redirect messages. In case anybody
1119 * sees these messages: please contact me
1120 * (wd@denx.de), or - even better - send me the
1121 * necessary fixes :-)
1123 * Note: in all cases where I have seen this so far
1124 * it was a problem with the router configuration,
1125 * for instance when a router was configured in the
1126 * BOOTP reply, but the TFTP server was on the same
1127 * subnet. So this is probably a warning that your
1128 * configuration might be wrong. But I'm not really
1129 * sure if there aren't any other situations.
1131 * Simon Glass <sjg@chromium.org>: We get an ICMP when
1132 * we send a tftp packet to a dead connection, or when
1133 * there is no server at the other end.
1135 if (ip->ip_p == IPPROTO_ICMP) {
1136 receive_icmp(ip, len, src_ip, et);
1138 } else if (ip->ip_p != IPPROTO_UDP) { /* Only UDP packets */
1142 debug_cond(DEBUG_DEV_PKT,
1143 "received UDP (to=%pI4, from=%pI4, len=%d)\n",
1144 &dst_ip, &src_ip, len);
1146 #ifdef CONFIG_UDP_CHECKSUM
1147 if (ip->udp_xsum != 0) {
1153 xsum += (ntohs(ip->udp_len));
1154 xsum += (ntohl(ip->ip_src) >> 16) & 0x0000ffff;
1155 xsum += (ntohl(ip->ip_src) >> 0) & 0x0000ffff;
1156 xsum += (ntohl(ip->ip_dst) >> 16) & 0x0000ffff;
1157 xsum += (ntohl(ip->ip_dst) >> 0) & 0x0000ffff;
1159 sumlen = ntohs(ip->udp_len);
1160 sumptr = (ushort *) &(ip->udp_src);
1162 while (sumlen > 1) {
1165 sumdata = *sumptr++;
1166 xsum += ntohs(sumdata);
1172 sumdata = *(unsigned char *) sumptr;
1173 sumdata = (sumdata << 8) & 0xff00;
1176 while ((xsum >> 16) != 0) {
1177 xsum = (xsum & 0x0000ffff) +
1178 ((xsum >> 16) & 0x0000ffff);
1180 if ((xsum != 0x00000000) && (xsum != 0x0000ffff)) {
1181 printf(" UDP wrong checksum %08lx %08x\n",
1182 xsum, ntohs(ip->udp_xsum));
1189 #if defined (CONFIG_NETCONSOLE) && !(CONFIG_SPL_BUILD)
1190 nc_input_packet((uchar *)ip + IP_UDP_HDR_SIZE,
1194 ntohs(ip->udp_len) - UDP_HDR_SIZE);
1197 * IP header OK. Pass the packet to the current handler.
1199 (*udp_packet_handler)((uchar *)ip + IP_UDP_HDR_SIZE,
1203 ntohs(ip->udp_len) - UDP_HDR_SIZE);
1209 /**********************************************************************/
1211 static int net_check_prereq(enum proto_t protocol)
1215 #if defined(CONFIG_CMD_PING)
1217 if (NetPingIP == 0) {
1218 puts("*** ERROR: ping address not given\n");
1223 #if defined(CONFIG_CMD_SNTP)
1225 if (NetNtpServerIP == 0) {
1226 puts("*** ERROR: NTP server address not given\n");
1231 #if defined(CONFIG_CMD_DNS)
1233 if (NetOurDNSIP == 0) {
1234 puts("*** ERROR: DNS server address not given\n");
1239 #if defined(CONFIG_CMD_NFS)
1244 if (NetServerIP == 0) {
1245 puts("*** ERROR: `serverip' not set\n");
1248 #if defined(CONFIG_CMD_PING) || defined(CONFIG_CMD_SNTP) || \
1249 defined(CONFIG_CMD_DNS)
1256 if (NetOurIP == 0) {
1257 puts("*** ERROR: `ipaddr' not set\n");
1262 #ifdef CONFIG_CMD_RARP
1269 if (memcmp(NetOurEther, "\0\0\0\0\0\0", 6) == 0) {
1270 int num = eth_get_dev_index();
1274 puts("*** ERROR: No ethernet found.\n");
1277 puts("*** ERROR: `ethaddr' not set\n");
1280 printf("*** ERROR: `eth%daddr' not set\n",
1294 /**********************************************************************/
1301 myvlanid = ntohs(NetOurVLAN);
1302 if (myvlanid == (ushort)-1)
1303 myvlanid = VLAN_NONE;
1305 return ((myvlanid & VLAN_IDMASK) == VLAN_NONE) ? ETHER_HDR_SIZE :
1306 VLAN_ETHER_HDR_SIZE;
1310 NetSetEther(uchar *xet, uchar * addr, uint prot)
1312 struct ethernet_hdr *et = (struct ethernet_hdr *)xet;
1315 myvlanid = ntohs(NetOurVLAN);
1316 if (myvlanid == (ushort)-1)
1317 myvlanid = VLAN_NONE;
1319 memcpy(et->et_dest, addr, 6);
1320 memcpy(et->et_src, NetOurEther, 6);
1321 if ((myvlanid & VLAN_IDMASK) == VLAN_NONE) {
1322 et->et_protlen = htons(prot);
1323 return ETHER_HDR_SIZE;
1325 struct vlan_ethernet_hdr *vet =
1326 (struct vlan_ethernet_hdr *)xet;
1328 vet->vet_vlan_type = htons(PROT_VLAN);
1329 vet->vet_tag = htons((0 << 5) | (myvlanid & VLAN_IDMASK));
1330 vet->vet_type = htons(prot);
1331 return VLAN_ETHER_HDR_SIZE;
1335 int net_update_ether(struct ethernet_hdr *et, uchar *addr, uint prot)
1339 memcpy(et->et_dest, addr, 6);
1340 memcpy(et->et_src, NetOurEther, 6);
1341 protlen = ntohs(et->et_protlen);
1342 if (protlen == PROT_VLAN) {
1343 struct vlan_ethernet_hdr *vet =
1344 (struct vlan_ethernet_hdr *)et;
1345 vet->vet_type = htons(prot);
1346 return VLAN_ETHER_HDR_SIZE;
1347 } else if (protlen > 1514) {
1348 et->et_protlen = htons(prot);
1349 return ETHER_HDR_SIZE;
1352 struct e802_hdr *et802 = (struct e802_hdr *)et;
1353 et802->et_prot = htons(prot);
1354 return E802_HDR_SIZE;
1358 void net_set_ip_header(uchar *pkt, IPaddr_t dest, IPaddr_t source)
1360 struct ip_udp_hdr *ip = (struct ip_udp_hdr *)pkt;
1363 * Construct an IP header.
1365 /* IP_HDR_SIZE / 4 (not including UDP) */
1368 ip->ip_len = htons(IP_HDR_SIZE);
1369 ip->ip_id = htons(NetIPID++);
1370 ip->ip_off = htons(IP_FLAGS_DFRAG); /* Don't fragment */
1373 /* already in network byte order */
1374 NetCopyIP((void *)&ip->ip_src, &source);
1375 /* already in network byte order */
1376 NetCopyIP((void *)&ip->ip_dst, &dest);
1379 void net_set_udp_header(uchar *pkt, IPaddr_t dest, int dport, int sport,
1382 struct ip_udp_hdr *ip = (struct ip_udp_hdr *)pkt;
1385 * If the data is an odd number of bytes, zero the
1386 * byte after the last byte so that the checksum
1390 pkt[IP_UDP_HDR_SIZE + len] = 0;
1392 net_set_ip_header(pkt, dest, NetOurIP);
1393 ip->ip_len = htons(IP_UDP_HDR_SIZE + len);
1394 ip->ip_p = IPPROTO_UDP;
1395 ip->ip_sum = compute_ip_checksum(ip, IP_HDR_SIZE);
1397 ip->udp_src = htons(sport);
1398 ip->udp_dst = htons(dport);
1399 ip->udp_len = htons(UDP_HDR_SIZE + len);
1403 void copy_filename(char *dst, const char *src, int size)
1405 if (*src && (*src == '"')) {
1410 while ((--size > 0) && *src && (*src != '"'))
1415 #if defined(CONFIG_CMD_NFS) || \
1416 defined(CONFIG_CMD_SNTP) || \
1417 defined(CONFIG_CMD_DNS)
1419 * make port a little random (1024-17407)
1420 * This keeps the math somewhat trivial to compute, and seems to work with
1421 * all supported protocols/clients/servers
1423 unsigned int random_port(void)
1425 return 1024 + (get_timer(0) % 0x4000);
1429 void ip_to_string(IPaddr_t x, char *s)
1432 sprintf(s, "%d.%d.%d.%d",
1433 (int) ((x >> 24) & 0xff),
1434 (int) ((x >> 16) & 0xff),
1435 (int) ((x >> 8) & 0xff), (int) ((x >> 0) & 0xff)
1439 void VLAN_to_string(ushort x, char *s)
1443 if (x == (ushort)-1)
1449 sprintf(s, "%d", x & VLAN_IDMASK);
1452 ushort string_to_VLAN(const char *s)
1457 return htons(VLAN_NONE);
1459 if (*s < '0' || *s > '9')
1462 id = (ushort)simple_strtoul(s, NULL, 10);
1467 ushort getenv_VLAN(char *var)
1469 return string_to_VLAN(getenv(var));