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];
187 /* Receive packets */
188 uchar *net_rx_packets[PKTBUFSRX];
191 uchar *NetRxPackets[PKTBUFSRX];
193 /* Current UDP RX packet handler */
194 static rxhand_f *udp_packet_handler;
195 /* Current ARP RX packet handler */
196 static rxhand_f *arp_packet_handler;
197 #ifdef CONFIG_CMD_TFTPPUT
198 /* Current ICMP rx handler */
199 static rxhand_icmp_f *packet_icmp_handler;
201 /* Current timeout handler */
202 static thand_f *timeHandler;
203 /* Time base value */
204 static ulong timeStart;
205 /* Current timeout value */
206 static ulong timeDelta;
207 /* THE transmit packet */
210 static int net_check_prereq(enum proto_t protocol);
212 static int NetTryCount;
214 int __maybe_unused net_busy_flag;
216 /**********************************************************************/
218 static int on_bootfile(const char *name, const char *value, enum env_op op,
223 case env_op_overwrite:
224 copy_filename(BootFile, value, sizeof(BootFile));
232 U_BOOT_ENV_CALLBACK(bootfile, on_bootfile);
235 * Check if autoload is enabled. If so, use either NFS or TFTP to download
238 void net_auto_load(void)
240 #if defined(CONFIG_CMD_NFS)
241 const char *s = getenv("autoload");
243 if (s != NULL && strcmp(s, "NFS") == 0) {
245 * Use NFS to load the bootfile.
251 if (getenv_yesno("autoload") == 0) {
253 * Just use BOOTP/RARP to configure system;
254 * Do not use TFTP to load the bootfile.
256 net_set_state(NETLOOP_SUCCESS);
262 static void NetInitLoop(void)
264 static int env_changed_id;
265 int env_id = get_env_id();
267 /* update only when the environment has changed */
268 if (env_changed_id != env_id) {
269 NetOurIP = getenv_IPaddr("ipaddr");
270 NetOurGatewayIP = getenv_IPaddr("gatewayip");
271 NetOurSubnetMask = getenv_IPaddr("netmask");
272 NetServerIP = getenv_IPaddr("serverip");
273 NetOurNativeVLAN = getenv_VLAN("nvlan");
274 NetOurVLAN = getenv_VLAN("vlan");
275 #if defined(CONFIG_CMD_DNS)
276 NetOurDNSIP = getenv_IPaddr("dnsip");
278 env_changed_id = env_id;
281 memcpy(NetOurEther, eth_get_ethaddr(), 6);
286 static void net_clear_handlers(void)
288 net_set_udp_handler(NULL);
289 net_set_arp_handler(NULL);
290 NetSetTimeout(0, NULL);
293 static void net_cleanup_loop(void)
295 net_clear_handlers();
300 static int first_call = 1;
304 * Setup packet buffers, aligned correctly.
308 NetTxPacket = &PktBuf[0] + (PKTALIGN - 1);
309 NetTxPacket -= (ulong)NetTxPacket % PKTALIGN;
311 for (i = 0; i < PKTBUFSRX; i++) {
312 net_rx_packets[i] = NetTxPacket + (i + 1) *
316 for (i = 0; i < PKTBUFSRX; i++)
317 NetRxPackets[i] = NetTxPacket + (i + 1) * PKTSIZE_ALIGN;
320 net_clear_handlers();
322 /* Only need to setup buffer pointers once. */
329 /**********************************************************************/
331 * Main network processing loop.
334 int NetLoop(enum proto_t protocol)
341 debug_cond(DEBUG_INT_STATE, "--- NetLoop Entry\n");
343 bootstage_mark_name(BOOTSTAGE_ID_ETH_START, "eth_start");
345 if (eth_is_on_demand_init() || protocol != NETCONS) {
348 if (eth_init() < 0) {
353 eth_init_state_only();
356 #ifdef CONFIG_USB_KEYBOARD
359 net_set_state(NETLOOP_CONTINUE);
362 * Start the ball rolling with the given start function. From
363 * here on, this code is a state machine driven by received
364 * packets and timer events.
366 debug_cond(DEBUG_INT_STATE, "--- NetLoop Init\n");
369 switch (net_check_prereq(protocol)) {
371 /* network not configured */
376 /* network device not configured */
381 NetBootFileXferSize = 0;
384 #ifdef CONFIG_CMD_TFTPPUT
387 /* always use ARP to get server ethernet address */
390 #ifdef CONFIG_CMD_TFTPSRV
395 #if defined(CONFIG_CMD_DHCP)
399 DhcpRequest(); /* Basically same as BOOTP */
409 #if defined(CONFIG_CMD_RARP)
416 #if defined(CONFIG_CMD_PING)
421 #if defined(CONFIG_CMD_NFS)
426 #if defined(CONFIG_CMD_CDP)
431 #if defined (CONFIG_NETCONSOLE) && !(CONFIG_SPL_BUILD)
436 #if defined(CONFIG_CMD_SNTP)
441 #if defined(CONFIG_CMD_DNS)
446 #if defined(CONFIG_CMD_LINK_LOCAL)
451 #if defined(CONFIG_CMD_BOOTCE)
463 #if defined(CONFIG_MII) || defined(CONFIG_CMD_MII)
464 #if defined(CONFIG_SYS_FAULT_ECHO_LINK_DOWN) && \
465 defined(CONFIG_STATUS_LED) && \
466 defined(STATUS_LED_RED)
468 * Echo the inverted link state to the fault LED.
470 if (miiphy_link(eth_get_dev()->name, CONFIG_SYS_FAULT_MII_ADDR))
471 status_led_set(STATUS_LED_RED, STATUS_LED_OFF);
473 status_led_set(STATUS_LED_RED, STATUS_LED_ON);
474 #endif /* CONFIG_SYS_FAULT_ECHO_LINK_DOWN, ... */
475 #endif /* CONFIG_MII, ... */
476 #ifdef CONFIG_USB_KEYBOARD
481 * Main packet reception loop. Loop receiving packets until
482 * someone sets `net_state' to a state that terminates.
486 #ifdef CONFIG_SHOW_ACTIVITY
490 * Check the ethernet for a new packet. The ethernet
491 * receive routine will process it.
496 * Abort if ctrl-c was pressed.
499 /* cancel any ARP that may not have completed */
500 NetArpWaitPacketIP = 0;
504 /* Invalidate the last protocol */
505 eth_set_last_protocol(BOOTP);
508 /* include a debug print as well incase the debug
509 messages are directed to stderr */
510 debug_cond(DEBUG_INT_STATE, "--- NetLoop Abort!\n");
517 * Check for a timeout, and run the timeout handler
520 if (timeHandler && ((get_timer(timeStart)) > timeDelta)) {
523 #if defined(CONFIG_MII) || defined(CONFIG_CMD_MII)
524 #if defined(CONFIG_SYS_FAULT_ECHO_LINK_DOWN) && \
525 defined(CONFIG_STATUS_LED) && \
526 defined(STATUS_LED_RED)
528 * Echo the inverted link state to the fault LED.
530 if (miiphy_link(eth_get_dev()->name,
531 CONFIG_SYS_FAULT_MII_ADDR)) {
532 status_led_set(STATUS_LED_RED, STATUS_LED_OFF);
534 status_led_set(STATUS_LED_RED, STATUS_LED_ON);
536 #endif /* CONFIG_SYS_FAULT_ECHO_LINK_DOWN, ... */
537 #endif /* CONFIG_MII, ... */
538 debug_cond(DEBUG_INT_STATE, "--- NetLoop timeout\n");
540 timeHandler = (thand_f *)0;
544 if (net_state == NETLOOP_FAIL)
549 case NETLOOP_RESTART:
553 case NETLOOP_SUCCESS:
555 if (NetBootFileXferSize > 0) {
556 printf("Bytes transferred = %ld (%lx hex)\n",
558 NetBootFileXferSize);
559 setenv_hex("filesize", NetBootFileXferSize);
560 setenv_hex("fileaddr", load_addr);
562 if (protocol != NETCONS) {
565 eth_halt_state_only();
568 eth_set_last_protocol(protocol);
570 ret = NetBootFileXferSize;
571 debug_cond(DEBUG_INT_STATE, "--- NetLoop Success!\n");
576 /* Invalidate the last protocol */
577 eth_set_last_protocol(BOOTP);
578 debug_cond(DEBUG_INT_STATE, "--- NetLoop Fail!\n");
581 case NETLOOP_CONTINUE:
587 #ifdef CONFIG_USB_KEYBOARD
590 #ifdef CONFIG_CMD_TFTPPUT
591 /* Clear out the handlers */
592 net_set_udp_handler(NULL);
593 net_set_icmp_handler(NULL);
598 /**********************************************************************/
601 startAgainTimeout(void)
603 net_set_state(NETLOOP_RESTART);
606 void NetStartAgain(void)
609 int retry_forever = 0;
610 unsigned long retrycnt = 0;
612 nretry = getenv("netretry");
614 if (!strcmp(nretry, "yes"))
616 else if (!strcmp(nretry, "no"))
618 else if (!strcmp(nretry, "once"))
621 retrycnt = simple_strtoul(nretry, NULL, 0);
627 if ((!retry_forever) && (NetTryCount >= retrycnt)) {
629 net_set_state(NETLOOP_FAIL);
636 #if !defined(CONFIG_NET_DO_NOT_TRY_ANOTHER)
637 eth_try_another(!NetRestarted);
640 if (NetRestartWrap) {
643 NetSetTimeout(10000UL, startAgainTimeout);
644 net_set_udp_handler(NULL);
646 net_set_state(NETLOOP_FAIL);
649 net_set_state(NETLOOP_RESTART);
653 /**********************************************************************/
658 static void dummy_handler(uchar *pkt, unsigned dport,
659 IPaddr_t sip, unsigned sport,
664 rxhand_f *net_get_udp_handler(void)
666 return udp_packet_handler;
669 void net_set_udp_handler(rxhand_f *f)
671 debug_cond(DEBUG_INT_STATE, "--- NetLoop UDP handler set (%p)\n", f);
673 udp_packet_handler = dummy_handler;
675 udp_packet_handler = f;
678 rxhand_f *net_get_arp_handler(void)
680 return arp_packet_handler;
683 void net_set_arp_handler(rxhand_f *f)
685 debug_cond(DEBUG_INT_STATE, "--- NetLoop ARP handler set (%p)\n", f);
687 arp_packet_handler = dummy_handler;
689 arp_packet_handler = f;
692 #ifdef CONFIG_CMD_TFTPPUT
693 void net_set_icmp_handler(rxhand_icmp_f *f)
695 packet_icmp_handler = f;
700 NetSetTimeout(ulong iv, thand_f *f)
703 debug_cond(DEBUG_INT_STATE,
704 "--- NetLoop timeout handler cancelled\n");
705 timeHandler = (thand_f *)0;
707 debug_cond(DEBUG_INT_STATE,
708 "--- NetLoop timeout handler set (%p)\n", f);
710 timeStart = get_timer(0);
711 timeDelta = iv * CONFIG_SYS_HZ / 1000;
715 int NetSendUDPPacket(uchar *ether, IPaddr_t dest, int dport, int sport,
722 /* make sure the NetTxPacket is initialized (NetInit() was called) */
723 assert(NetTxPacket != NULL);
724 if (NetTxPacket == NULL)
727 /* convert to new style broadcast */
731 /* if broadcast, make the ether address a broadcast and don't do ARP */
732 if (dest == 0xFFFFFFFF)
733 ether = NetBcastAddr;
735 pkt = (uchar *)NetTxPacket;
737 eth_hdr_size = NetSetEther(pkt, ether, PROT_IP);
739 net_set_udp_header(pkt, dest, dport, sport, payload_len);
740 pkt_hdr_size = eth_hdr_size + IP_UDP_HDR_SIZE;
742 /* if MAC address was not discovered yet, do an ARP request */
743 if (memcmp(ether, NetEtherNullAddr, 6) == 0) {
744 debug_cond(DEBUG_DEV_PKT, "sending ARP for %pI4\n", &dest);
746 /* save the ip and eth addr for the packet to send after arp */
747 NetArpWaitPacketIP = dest;
748 NetArpWaitPacketMAC = ether;
750 /* size of the waiting packet */
751 NetArpWaitTxPacketSize = pkt_hdr_size + payload_len;
753 /* and do the ARP request */
755 NetArpWaitTimerStart = get_timer(0);
757 return 1; /* waiting */
759 debug_cond(DEBUG_DEV_PKT, "sending UDP to %pI4/%pM\n",
761 NetSendPacket(NetTxPacket, pkt_hdr_size + payload_len);
762 return 0; /* transmitted */
766 #ifdef CONFIG_IP_DEFRAG
768 * This function collects fragments in a single packet, according
769 * to the algorithm in RFC815. It returns NULL or the pointer to
770 * a complete packet, in static storage
772 #ifndef CONFIG_NET_MAXDEFRAG
773 #define CONFIG_NET_MAXDEFRAG 16384
776 * MAXDEFRAG, above, is chosen in the config file and is real data
777 * so we need to add the NFS overhead, which is more than TFTP.
778 * To use sizeof in the internal unnamed structures, we need a real
779 * instance (can't do "sizeof(struct rpc_t.u.reply))", unfortunately).
780 * The compiler doesn't complain nor allocates the actual structure
782 static struct rpc_t rpc_specimen;
783 #define IP_PKTSIZE (CONFIG_NET_MAXDEFRAG + sizeof(rpc_specimen.u.reply))
785 #define IP_MAXUDP (IP_PKTSIZE - IP_HDR_SIZE)
788 * this is the packet being assembled, either data or frag control.
789 * Fragments go by 8 bytes, so this union must be 8 bytes long
792 /* first_byte is address of this structure */
793 u16 last_byte; /* last byte in this hole + 1 (begin of next hole) */
794 u16 next_hole; /* index of next (in 8-b blocks), 0 == none */
795 u16 prev_hole; /* index of prev, 0 == none */
799 static struct ip_udp_hdr *__NetDefragment(struct ip_udp_hdr *ip, int *lenp)
801 static uchar pkt_buff[IP_PKTSIZE] __aligned(PKTALIGN);
802 static u16 first_hole, total_len;
803 struct hole *payload, *thisfrag, *h, *newh;
804 struct ip_udp_hdr *localip = (struct ip_udp_hdr *)pkt_buff;
805 uchar *indata = (uchar *)ip;
806 int offset8, start, len, done = 0;
807 u16 ip_off = ntohs(ip->ip_off);
809 /* payload starts after IP header, this fragment is in there */
810 payload = (struct hole *)(pkt_buff + IP_HDR_SIZE);
811 offset8 = (ip_off & IP_OFFS);
812 thisfrag = payload + offset8;
814 len = ntohs(ip->ip_len) - IP_HDR_SIZE;
816 if (start + len > IP_MAXUDP) /* fragment extends too far */
819 if (!total_len || localip->ip_id != ip->ip_id) {
820 /* new (or different) packet, reset structs */
822 payload[0].last_byte = ~0;
823 payload[0].next_hole = 0;
824 payload[0].prev_hole = 0;
826 /* any IP header will work, copy the first we received */
827 memcpy(localip, ip, IP_HDR_SIZE);
831 * What follows is the reassembly algorithm. We use the payload
832 * array as a linked list of hole descriptors, as each hole starts
833 * at a multiple of 8 bytes. However, last byte can be whatever value,
834 * so it is represented as byte count, not as 8-byte blocks.
837 h = payload + first_hole;
838 while (h->last_byte < start) {
840 /* no hole that far away */
843 h = payload + h->next_hole;
846 /* last fragment may be 1..7 bytes, the "+7" forces acceptance */
847 if (offset8 + ((len + 7) / 8) <= h - payload) {
848 /* no overlap with holes (dup fragment?) */
852 if (!(ip_off & IP_FLAGS_MFRAG)) {
853 /* no more fragmentss: truncate this (last) hole */
854 total_len = start + len;
855 h->last_byte = start + len;
859 * There is some overlap: fix the hole list. This code doesn't
860 * deal with a fragment that overlaps with two different holes
861 * (thus being a superset of a previously-received fragment).
864 if ((h >= thisfrag) && (h->last_byte <= start + len)) {
865 /* complete overlap with hole: remove hole */
866 if (!h->prev_hole && !h->next_hole) {
867 /* last remaining hole */
869 } else if (!h->prev_hole) {
871 first_hole = h->next_hole;
872 payload[h->next_hole].prev_hole = 0;
873 } else if (!h->next_hole) {
875 payload[h->prev_hole].next_hole = 0;
877 /* in the middle of the list */
878 payload[h->next_hole].prev_hole = h->prev_hole;
879 payload[h->prev_hole].next_hole = h->next_hole;
882 } else if (h->last_byte <= start + len) {
883 /* overlaps with final part of the hole: shorten this hole */
884 h->last_byte = start;
886 } else if (h >= thisfrag) {
887 /* overlaps with initial part of the hole: move this hole */
888 newh = thisfrag + (len / 8);
892 payload[h->next_hole].prev_hole = (h - payload);
894 payload[h->prev_hole].next_hole = (h - payload);
896 first_hole = (h - payload);
899 /* fragment sits in the middle: split the hole */
900 newh = thisfrag + (len / 8);
902 h->last_byte = start;
903 h->next_hole = (newh - payload);
904 newh->prev_hole = (h - payload);
906 payload[newh->next_hole].prev_hole = (newh - payload);
909 /* finally copy this fragment and possibly return whole packet */
910 memcpy((uchar *)thisfrag, indata + IP_HDR_SIZE, len);
914 localip->ip_len = htons(total_len);
915 *lenp = total_len + IP_HDR_SIZE;
919 static inline struct ip_udp_hdr *NetDefragment(struct ip_udp_hdr *ip, int *lenp)
921 u16 ip_off = ntohs(ip->ip_off);
922 if (!(ip_off & (IP_OFFS | IP_FLAGS_MFRAG)))
923 return ip; /* not a fragment */
924 return __NetDefragment(ip, lenp);
927 #else /* !CONFIG_IP_DEFRAG */
929 static inline struct ip_udp_hdr *NetDefragment(struct ip_udp_hdr *ip, int *lenp)
931 u16 ip_off = ntohs(ip->ip_off);
932 if (!(ip_off & (IP_OFFS | IP_FLAGS_MFRAG)))
933 return ip; /* not a fragment */
939 * Receive an ICMP packet. We deal with REDIRECT and PING here, and silently
942 * @parma ip IP packet containing the ICMP
944 static void receive_icmp(struct ip_udp_hdr *ip, int len,
945 IPaddr_t src_ip, struct ethernet_hdr *et)
947 struct icmp_hdr *icmph = (struct icmp_hdr *)&ip->udp_src;
949 switch (icmph->type) {
951 if (icmph->code != ICMP_REDIR_HOST)
953 printf(" ICMP Host Redirect to %pI4 ",
957 #if defined(CONFIG_CMD_PING)
958 ping_receive(et, ip, len);
960 #ifdef CONFIG_CMD_TFTPPUT
961 if (packet_icmp_handler)
962 packet_icmp_handler(icmph->type, icmph->code,
963 ntohs(ip->udp_dst), src_ip, ntohs(ip->udp_src),
964 icmph->un.data, ntohs(ip->udp_len));
970 void net_process_received_packet(uchar *in_packet, int len)
972 struct ethernet_hdr *et;
973 struct ip_udp_hdr *ip;
977 #if defined(CONFIG_CMD_CDP)
980 ushort cti = 0, vlanid = VLAN_NONE, myvlanid, mynvlanid;
982 debug_cond(DEBUG_NET_PKT, "packet received\n");
984 NetRxPacket = in_packet;
985 NetRxPacketLen = len;
986 et = (struct ethernet_hdr *)in_packet;
988 /* too small packet? */
989 if (len < ETHER_HDR_SIZE)
994 (*push_packet)(in_packet, len);
999 #if defined(CONFIG_CMD_CDP)
1000 /* keep track if packet is CDP */
1001 iscdp = is_cdp_packet(et->et_dest);
1004 myvlanid = ntohs(NetOurVLAN);
1005 if (myvlanid == (ushort)-1)
1006 myvlanid = VLAN_NONE;
1007 mynvlanid = ntohs(NetOurNativeVLAN);
1008 if (mynvlanid == (ushort)-1)
1009 mynvlanid = VLAN_NONE;
1011 eth_proto = ntohs(et->et_protlen);
1013 if (eth_proto < 1514) {
1014 struct e802_hdr *et802 = (struct e802_hdr *)et;
1016 * Got a 802.2 packet. Check the other protocol field.
1017 * XXX VLAN over 802.2+SNAP not implemented!
1019 eth_proto = ntohs(et802->et_prot);
1021 ip = (struct ip_udp_hdr *)(in_packet + E802_HDR_SIZE);
1022 len -= E802_HDR_SIZE;
1024 } else if (eth_proto != PROT_VLAN) { /* normal packet */
1025 ip = (struct ip_udp_hdr *)(in_packet + ETHER_HDR_SIZE);
1026 len -= ETHER_HDR_SIZE;
1028 } else { /* VLAN packet */
1029 struct vlan_ethernet_hdr *vet =
1030 (struct vlan_ethernet_hdr *)et;
1032 debug_cond(DEBUG_NET_PKT, "VLAN packet received\n");
1034 /* too small packet? */
1035 if (len < VLAN_ETHER_HDR_SIZE)
1038 /* if no VLAN active */
1039 if ((ntohs(NetOurVLAN) & VLAN_IDMASK) == VLAN_NONE
1040 #if defined(CONFIG_CMD_CDP)
1046 cti = ntohs(vet->vet_tag);
1047 vlanid = cti & VLAN_IDMASK;
1048 eth_proto = ntohs(vet->vet_type);
1050 ip = (struct ip_udp_hdr *)(in_packet + VLAN_ETHER_HDR_SIZE);
1051 len -= VLAN_ETHER_HDR_SIZE;
1054 debug_cond(DEBUG_NET_PKT, "Receive from protocol 0x%x\n", eth_proto);
1056 #if defined(CONFIG_CMD_CDP)
1058 cdp_receive((uchar *)ip, len);
1063 if ((myvlanid & VLAN_IDMASK) != VLAN_NONE) {
1064 if (vlanid == VLAN_NONE)
1065 vlanid = (mynvlanid & VLAN_IDMASK);
1067 if (vlanid != (myvlanid & VLAN_IDMASK))
1071 switch (eth_proto) {
1074 ArpReceive(et, ip, len);
1077 #ifdef CONFIG_CMD_RARP
1079 rarp_receive(ip, len);
1083 debug_cond(DEBUG_NET_PKT, "Got IP\n");
1084 /* Before we start poking the header, make sure it is there */
1085 if (len < IP_UDP_HDR_SIZE) {
1086 debug("len bad %d < %lu\n", len,
1087 (ulong)IP_UDP_HDR_SIZE);
1090 /* Check the packet length */
1091 if (len < ntohs(ip->ip_len)) {
1092 debug("len bad %d < %d\n", len, ntohs(ip->ip_len));
1095 len = ntohs(ip->ip_len);
1096 debug_cond(DEBUG_NET_PKT, "len=%d, v=%02x\n",
1097 len, ip->ip_hl_v & 0xff);
1099 /* Can't deal with anything except IPv4 */
1100 if ((ip->ip_hl_v & 0xf0) != 0x40)
1102 /* Can't deal with IP options (headers != 20 bytes) */
1103 if ((ip->ip_hl_v & 0x0f) > 0x05)
1105 /* Check the Checksum of the header */
1106 if (!ip_checksum_ok((uchar *)ip, IP_HDR_SIZE)) {
1107 debug("checksum bad\n");
1110 /* If it is not for us, ignore it */
1111 dst_ip = NetReadIP(&ip->ip_dst);
1112 if (NetOurIP && dst_ip != NetOurIP && dst_ip != 0xFFFFFFFF) {
1113 #ifdef CONFIG_MCAST_TFTP
1114 if (Mcast_addr != dst_ip)
1118 /* Read source IP address for later use */
1119 src_ip = NetReadIP(&ip->ip_src);
1121 * The function returns the unchanged packet if it's not
1122 * a fragment, and either the complete packet or NULL if
1123 * it is a fragment (if !CONFIG_IP_DEFRAG, it returns NULL)
1125 ip = NetDefragment(ip, &len);
1129 * watch for ICMP host redirects
1131 * There is no real handler code (yet). We just watch
1132 * for ICMP host redirect messages. In case anybody
1133 * sees these messages: please contact me
1134 * (wd@denx.de), or - even better - send me the
1135 * necessary fixes :-)
1137 * Note: in all cases where I have seen this so far
1138 * it was a problem with the router configuration,
1139 * for instance when a router was configured in the
1140 * BOOTP reply, but the TFTP server was on the same
1141 * subnet. So this is probably a warning that your
1142 * configuration might be wrong. But I'm not really
1143 * sure if there aren't any other situations.
1145 * Simon Glass <sjg@chromium.org>: We get an ICMP when
1146 * we send a tftp packet to a dead connection, or when
1147 * there is no server at the other end.
1149 if (ip->ip_p == IPPROTO_ICMP) {
1150 receive_icmp(ip, len, src_ip, et);
1152 } else if (ip->ip_p != IPPROTO_UDP) { /* Only UDP packets */
1156 debug_cond(DEBUG_DEV_PKT,
1157 "received UDP (to=%pI4, from=%pI4, len=%d)\n",
1158 &dst_ip, &src_ip, len);
1160 #ifdef CONFIG_UDP_CHECKSUM
1161 if (ip->udp_xsum != 0) {
1167 xsum += (ntohs(ip->udp_len));
1168 xsum += (ntohl(ip->ip_src) >> 16) & 0x0000ffff;
1169 xsum += (ntohl(ip->ip_src) >> 0) & 0x0000ffff;
1170 xsum += (ntohl(ip->ip_dst) >> 16) & 0x0000ffff;
1171 xsum += (ntohl(ip->ip_dst) >> 0) & 0x0000ffff;
1173 sumlen = ntohs(ip->udp_len);
1174 sumptr = (ushort *) &(ip->udp_src);
1176 while (sumlen > 1) {
1179 sumdata = *sumptr++;
1180 xsum += ntohs(sumdata);
1186 sumdata = *(unsigned char *) sumptr;
1187 sumdata = (sumdata << 8) & 0xff00;
1190 while ((xsum >> 16) != 0) {
1191 xsum = (xsum & 0x0000ffff) +
1192 ((xsum >> 16) & 0x0000ffff);
1194 if ((xsum != 0x00000000) && (xsum != 0x0000ffff)) {
1195 printf(" UDP wrong checksum %08lx %08x\n",
1196 xsum, ntohs(ip->udp_xsum));
1203 #if defined (CONFIG_NETCONSOLE) && !(CONFIG_SPL_BUILD)
1204 nc_input_packet((uchar *)ip + IP_UDP_HDR_SIZE,
1208 ntohs(ip->udp_len) - UDP_HDR_SIZE);
1211 * IP header OK. Pass the packet to the current handler.
1213 (*udp_packet_handler)((uchar *)ip + IP_UDP_HDR_SIZE,
1217 ntohs(ip->udp_len) - UDP_HDR_SIZE);
1223 /**********************************************************************/
1225 static int net_check_prereq(enum proto_t protocol)
1228 #if defined(CONFIG_CMD_PING)
1230 if (NetPingIP == 0) {
1231 puts("*** ERROR: ping address not given\n");
1236 #if defined(CONFIG_CMD_SNTP)
1238 if (NetNtpServerIP == 0) {
1239 puts("*** ERROR: NTP server address not given\n");
1244 #if defined(CONFIG_CMD_DNS)
1246 if (NetOurDNSIP == 0) {
1247 puts("*** ERROR: DNS server address not given\n");
1252 #if defined(CONFIG_CMD_NFS)
1257 if (NetServerIP == 0) {
1258 puts("*** ERROR: `serverip' not set\n");
1261 #if defined(CONFIG_CMD_PING) || defined(CONFIG_CMD_SNTP) || \
1262 defined(CONFIG_CMD_DNS)
1270 if (NetOurIP == 0) {
1271 puts("*** ERROR: `ipaddr' not set\n");
1276 #ifdef CONFIG_CMD_RARP
1283 if (memcmp(NetOurEther, "\0\0\0\0\0\0", 6) == 0) {
1284 int num = eth_get_dev_index();
1288 puts("*** ERROR: No ethernet found.\n");
1291 puts("*** ERROR: `ethaddr' not set\n");
1294 printf("*** ERROR: `eth%daddr' not set\n",
1308 /**********************************************************************/
1315 myvlanid = ntohs(NetOurVLAN);
1316 if (myvlanid == (ushort)-1)
1317 myvlanid = VLAN_NONE;
1319 return ((myvlanid & VLAN_IDMASK) == VLAN_NONE) ? ETHER_HDR_SIZE :
1320 VLAN_ETHER_HDR_SIZE;
1324 NetSetEther(uchar *xet, uchar * addr, uint prot)
1326 struct ethernet_hdr *et = (struct ethernet_hdr *)xet;
1329 myvlanid = ntohs(NetOurVLAN);
1330 if (myvlanid == (ushort)-1)
1331 myvlanid = VLAN_NONE;
1333 memcpy(et->et_dest, addr, 6);
1334 memcpy(et->et_src, NetOurEther, 6);
1335 if ((myvlanid & VLAN_IDMASK) == VLAN_NONE) {
1336 et->et_protlen = htons(prot);
1337 return ETHER_HDR_SIZE;
1339 struct vlan_ethernet_hdr *vet =
1340 (struct vlan_ethernet_hdr *)xet;
1342 vet->vet_vlan_type = htons(PROT_VLAN);
1343 vet->vet_tag = htons((0 << 5) | (myvlanid & VLAN_IDMASK));
1344 vet->vet_type = htons(prot);
1345 return VLAN_ETHER_HDR_SIZE;
1349 int net_update_ether(struct ethernet_hdr *et, uchar *addr, uint prot)
1353 memcpy(et->et_dest, addr, 6);
1354 memcpy(et->et_src, NetOurEther, 6);
1355 protlen = ntohs(et->et_protlen);
1356 if (protlen == PROT_VLAN) {
1357 struct vlan_ethernet_hdr *vet =
1358 (struct vlan_ethernet_hdr *)et;
1359 vet->vet_type = htons(prot);
1360 return VLAN_ETHER_HDR_SIZE;
1361 } else if (protlen > 1514) {
1362 et->et_protlen = htons(prot);
1363 return ETHER_HDR_SIZE;
1366 struct e802_hdr *et802 = (struct e802_hdr *)et;
1367 et802->et_prot = htons(prot);
1368 return E802_HDR_SIZE;
1372 void net_set_ip_header(uchar *pkt, IPaddr_t dest, IPaddr_t source)
1374 struct ip_udp_hdr *ip = (struct ip_udp_hdr *)pkt;
1377 * Construct an IP header.
1379 /* IP_HDR_SIZE / 4 (not including UDP) */
1382 ip->ip_len = htons(IP_HDR_SIZE);
1383 ip->ip_id = htons(NetIPID++);
1384 ip->ip_off = htons(IP_FLAGS_DFRAG); /* Don't fragment */
1387 /* already in network byte order */
1388 NetCopyIP((void *)&ip->ip_src, &source);
1389 /* already in network byte order */
1390 NetCopyIP((void *)&ip->ip_dst, &dest);
1393 void net_set_udp_header(uchar *pkt, IPaddr_t dest, int dport, int sport,
1396 struct ip_udp_hdr *ip = (struct ip_udp_hdr *)pkt;
1399 * If the data is an odd number of bytes, zero the
1400 * byte after the last byte so that the checksum
1404 pkt[IP_UDP_HDR_SIZE + len] = 0;
1406 net_set_ip_header(pkt, dest, NetOurIP);
1407 ip->ip_len = htons(IP_UDP_HDR_SIZE + len);
1408 ip->ip_p = IPPROTO_UDP;
1409 ip->ip_sum = compute_ip_checksum(ip, IP_HDR_SIZE);
1411 ip->udp_src = htons(sport);
1412 ip->udp_dst = htons(dport);
1413 ip->udp_len = htons(UDP_HDR_SIZE + len);
1417 void copy_filename(char *dst, const char *src, int size)
1419 if (*src && (*src == '"')) {
1424 while ((--size > 0) && *src && (*src != '"'))
1429 #if defined(CONFIG_CMD_NFS) || \
1430 defined(CONFIG_CMD_SNTP) || \
1431 defined(CONFIG_CMD_DNS)
1433 * make port a little random (1024-17407)
1434 * This keeps the math somewhat trivial to compute, and seems to work with
1435 * all supported protocols/clients/servers
1437 unsigned int random_port(void)
1439 return 1024 + (get_timer(0) % 0x4000);
1443 void ip_to_string(IPaddr_t x, char *s)
1446 sprintf(s, "%d.%d.%d.%d",
1447 (int) ((x >> 24) & 0xff),
1448 (int) ((x >> 16) & 0xff),
1449 (int) ((x >> 8) & 0xff), (int) ((x >> 0) & 0xff)
1453 void VLAN_to_string(ushort x, char *s)
1457 if (x == (ushort)-1)
1463 sprintf(s, "%d", x & VLAN_IDMASK);
1466 ushort string_to_VLAN(const char *s)
1471 return htons(VLAN_NONE);
1473 if (*s < '0' || *s > '9')
1476 id = (ushort)simple_strtoul(s, NULL, 10);
1481 ushort getenv_VLAN(char *var)
1483 return string_to_VLAN(getenv(var));