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
14 * The user interface supports commands for BOOTP, RARP, and TFTP.
15 * Also, we support ARP internally. Depending on available data,
16 * these interact as follows:
20 * Prerequisites: - own ethernet address
21 * We want: - own IP address
22 * - TFTP server IP address
28 * Prerequisites: - own ethernet address
29 * We want: - own IP address
34 * Prerequisites: - own ethernet address
35 * We want: - own IP address
36 * - TFTP server IP address
41 * Prerequisites: - own ethernet address
43 * - TFTP server IP address
44 * We want: - TFTP server ethernet address
49 * Prerequisites: - own ethernet address
50 * We want: - IP, Netmask, ServerIP, Gateway IP
51 * - bootfilename, lease time
56 * Prerequisites: - own ethernet address
58 * - TFTP server IP address
59 * - TFTP server ethernet address
60 * - name of bootfile (if unknown, we use a default name
61 * derived from our own IP address)
62 * We want: - load the boot file
67 * Prerequisites: - own ethernet address
69 * - name of bootfile (if unknown, we use a default name
70 * derived from our own IP address)
71 * We want: - load the boot file
76 * Prerequisites: - own ethernet address
78 * We want: - network time
85 #include <environment.h>
87 #if defined(CONFIG_STATUS_LED)
89 #include <status_led.h>
92 #include <linux/compiler.h>
96 #if defined(CONFIG_CMD_DNS)
99 #include "link_local.h"
103 #if defined(CONFIG_CMD_SNTP)
108 DECLARE_GLOBAL_DATA_PTR;
110 /** BOOTP EXTENTIONS **/
112 /* Our subnet mask (0=unknown) */
113 IPaddr_t NetOurSubnetMask;
114 /* Our gateways IP address */
115 IPaddr_t NetOurGatewayIP;
116 /* Our DNS IP address */
117 IPaddr_t NetOurDNSIP;
118 #if defined(CONFIG_BOOTP_DNS2)
119 /* Our 2nd DNS IP address */
120 IPaddr_t NetOurDNS2IP;
123 char NetOurNISDomain[32] = {0,};
125 char NetOurHostName[32] = {0,};
127 char NetOurRootPath[64] = {0,};
128 /* Our bootfile size in blocks */
129 ushort NetBootFileSize;
131 #ifdef CONFIG_MCAST_TFTP /* Multicast TFTP */
135 /** END OF BOOTP EXTENTIONS **/
137 /* The actual transferred size of the bootfile (in bytes) */
138 ulong NetBootFileXferSize;
139 /* Our ethernet address */
140 uchar NetOurEther[6];
141 /* Boot server enet address */
142 uchar NetServerEther[6];
143 /* Our IP addr (0 = unknown) */
145 /* Server IP addr (0 = unknown) */
146 IPaddr_t NetServerIP;
147 /* Current receive packet */
149 /* Current rx packet length */
153 /* Ethernet bcast address */
154 uchar NetBcastAddr[6] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
155 uchar NetEtherNullAddr[6];
157 void (*push_packet)(void *, int len) = 0;
159 /* Network loop state */
160 enum net_loop_state net_state;
161 /* Tried all network devices */
163 /* Network loop restarted */
164 static int NetRestarted;
165 /* At least one device configured */
166 static int NetDevExists;
168 /* XXX in both little & big endian machines 0xFFFF == ntohs(-1) */
169 /* default is without VLAN */
170 ushort NetOurVLAN = 0xFFFF;
172 ushort NetOurNativeVLAN = 0xFFFF;
177 #if defined(CONFIG_CMD_SNTP)
178 /* NTP server IP address */
179 IPaddr_t NetNtpServerIP;
180 /* offset time from UTC */
184 static uchar PktBuf[(PKTBUFSRX+1) * PKTSIZE_ALIGN + PKTALIGN];
187 uchar *NetRxPackets[PKTBUFSRX];
189 /* Current UDP RX packet handler */
190 static rxhand_f *udp_packet_handler;
191 /* Current ARP RX packet handler */
192 static rxhand_f *arp_packet_handler;
193 #ifdef CONFIG_CMD_TFTPPUT
194 /* Current ICMP rx handler */
195 static rxhand_icmp_f *packet_icmp_handler;
197 /* Current timeout handler */
198 static thand_f *timeHandler;
199 /* Time base value */
200 static ulong timeStart;
201 /* Current timeout value */
202 static ulong timeDelta;
203 /* THE transmit packet */
206 static int net_check_prereq(enum proto_t protocol);
208 static int NetTryCount;
210 /**********************************************************************/
212 static int on_bootfile(const char *name, const char *value, enum env_op op,
217 case env_op_overwrite:
218 copy_filename(BootFile, value, sizeof(BootFile));
226 U_BOOT_ENV_CALLBACK(bootfile, on_bootfile);
229 * Check if autoload is enabled. If so, use either NFS or TFTP to download
232 void net_auto_load(void)
234 #if defined(CONFIG_CMD_NFS)
235 const char *s = getenv("autoload");
237 if (s != NULL && strcmp(s, "NFS") == 0) {
239 * Use NFS to load the bootfile.
245 if (getenv_yesno("autoload") == 0) {
247 * Just use BOOTP/RARP to configure system;
248 * Do not use TFTP to load the bootfile.
250 net_set_state(NETLOOP_SUCCESS);
256 static void NetInitLoop(void)
258 static int env_changed_id;
259 int env_id = get_env_id();
261 /* update only when the environment has changed */
262 if (env_changed_id != env_id) {
263 NetOurIP = getenv_IPaddr("ipaddr");
264 NetOurGatewayIP = getenv_IPaddr("gatewayip");
265 NetOurSubnetMask = getenv_IPaddr("netmask");
266 NetServerIP = getenv_IPaddr("serverip");
267 NetOurNativeVLAN = getenv_VLAN("nvlan");
268 NetOurVLAN = getenv_VLAN("vlan");
269 #if defined(CONFIG_CMD_DNS)
270 NetOurDNSIP = getenv_IPaddr("dnsip");
272 env_changed_id = env_id;
274 memcpy(NetOurEther, eth_get_dev()->enetaddr, 6);
279 static void net_clear_handlers(void)
281 net_set_udp_handler(NULL);
282 net_set_arp_handler(NULL);
283 NetSetTimeout(0, NULL);
286 static void net_cleanup_loop(void)
288 net_clear_handlers();
293 static int first_call = 1;
297 * Setup packet buffers, aligned correctly.
301 NetTxPacket = &PktBuf[0] + (PKTALIGN - 1);
302 NetTxPacket -= (ulong)NetTxPacket % PKTALIGN;
303 for (i = 0; i < PKTBUFSRX; i++)
304 NetRxPackets[i] = NetTxPacket + (i + 1) * PKTSIZE_ALIGN;
307 net_clear_handlers();
309 /* Only need to setup buffer pointers once. */
316 /**********************************************************************/
318 * Main network processing loop.
321 int NetLoop(enum proto_t protocol)
329 debug_cond(DEBUG_INT_STATE, "--- NetLoop Entry\n");
331 bootstage_mark_name(BOOTSTAGE_ID_ETH_START, "eth_start");
333 if (eth_is_on_demand_init() || protocol != NETCONS) {
336 if (eth_init(bd) < 0) {
341 eth_init_state_only(bd);
344 net_set_state(NETLOOP_CONTINUE);
347 * Start the ball rolling with the given start function. From
348 * here on, this code is a state machine driven by received
349 * packets and timer events.
351 debug_cond(DEBUG_INT_STATE, "--- NetLoop Init\n");
354 switch (net_check_prereq(protocol)) {
356 /* network not configured */
361 /* network device not configured */
366 NetBootFileXferSize = 0;
369 #ifdef CONFIG_CMD_TFTPPUT
372 /* always use ARP to get server ethernet address */
375 #ifdef CONFIG_CMD_TFTPSRV
380 #if defined(CONFIG_CMD_DHCP)
384 DhcpRequest(); /* Basically same as BOOTP */
394 #if defined(CONFIG_CMD_RARP)
401 #if defined(CONFIG_CMD_PING)
406 #if defined(CONFIG_CMD_NFS)
411 #if defined(CONFIG_CMD_CDP)
416 #ifdef CONFIG_NETCONSOLE
421 #if defined(CONFIG_CMD_SNTP)
426 #if defined(CONFIG_CMD_DNS)
431 #if defined(CONFIG_CMD_LINK_LOCAL)
443 #if defined(CONFIG_MII) || defined(CONFIG_CMD_MII)
444 #if defined(CONFIG_SYS_FAULT_ECHO_LINK_DOWN) && \
445 defined(CONFIG_STATUS_LED) && \
446 defined(STATUS_LED_RED)
448 * Echo the inverted link state to the fault LED.
450 if (miiphy_link(eth_get_dev()->name, CONFIG_SYS_FAULT_MII_ADDR))
451 status_led_set(STATUS_LED_RED, STATUS_LED_OFF);
453 status_led_set(STATUS_LED_RED, STATUS_LED_ON);
454 #endif /* CONFIG_SYS_FAULT_ECHO_LINK_DOWN, ... */
455 #endif /* CONFIG_MII, ... */
458 * Main packet reception loop. Loop receiving packets until
459 * someone sets `net_state' to a state that terminates.
463 #ifdef CONFIG_SHOW_ACTIVITY
467 * Check the ethernet for a new packet. The ethernet
468 * receive routine will process it.
473 * Abort if ctrl-c was pressed.
476 /* cancel any ARP that may not have completed */
477 NetArpWaitPacketIP = 0;
481 /* Invalidate the last protocol */
482 eth_set_last_protocol(BOOTP);
485 /* include a debug print as well incase the debug
486 messages are directed to stderr */
487 debug_cond(DEBUG_INT_STATE, "--- NetLoop Abort!\n");
494 * Check for a timeout, and run the timeout handler
497 if (timeHandler && ((get_timer(0) - timeStart) > timeDelta)) {
500 #if defined(CONFIG_MII) || defined(CONFIG_CMD_MII)
501 #if defined(CONFIG_SYS_FAULT_ECHO_LINK_DOWN) && \
502 defined(CONFIG_STATUS_LED) && \
503 defined(STATUS_LED_RED)
505 * Echo the inverted link state to the fault LED.
507 if (miiphy_link(eth_get_dev()->name,
508 CONFIG_SYS_FAULT_MII_ADDR)) {
509 status_led_set(STATUS_LED_RED, STATUS_LED_OFF);
511 status_led_set(STATUS_LED_RED, STATUS_LED_ON);
513 #endif /* CONFIG_SYS_FAULT_ECHO_LINK_DOWN, ... */
514 #endif /* CONFIG_MII, ... */
515 debug_cond(DEBUG_INT_STATE, "--- NetLoop timeout\n");
517 timeHandler = (thand_f *)0;
524 case NETLOOP_RESTART:
528 case NETLOOP_SUCCESS:
530 if (NetBootFileXferSize > 0) {
532 printf("Bytes transferred = %ld (%lx hex)\n",
534 NetBootFileXferSize);
535 sprintf(buf, "%lX", NetBootFileXferSize);
536 setenv("filesize", buf);
538 sprintf(buf, "%lX", (unsigned long)load_addr);
539 setenv("fileaddr", buf);
541 if (protocol != NETCONS)
544 eth_halt_state_only();
546 eth_set_last_protocol(protocol);
548 ret = NetBootFileXferSize;
549 debug_cond(DEBUG_INT_STATE, "--- NetLoop Success!\n");
554 /* Invalidate the last protocol */
555 eth_set_last_protocol(BOOTP);
556 debug_cond(DEBUG_INT_STATE, "--- NetLoop Fail!\n");
559 case NETLOOP_CONTINUE:
565 #ifdef CONFIG_CMD_TFTPPUT
566 /* Clear out the handlers */
567 net_set_udp_handler(NULL);
568 net_set_icmp_handler(NULL);
573 /**********************************************************************/
576 startAgainTimeout(void)
578 net_set_state(NETLOOP_RESTART);
581 void NetStartAgain(void)
584 int retry_forever = 0;
585 unsigned long retrycnt = 0;
587 nretry = getenv("netretry");
589 if (!strcmp(nretry, "yes"))
591 else if (!strcmp(nretry, "no"))
593 else if (!strcmp(nretry, "once"))
596 retrycnt = simple_strtoul(nretry, NULL, 0);
600 if ((!retry_forever) && (NetTryCount >= retrycnt)) {
602 net_set_state(NETLOOP_FAIL);
609 #if !defined(CONFIG_NET_DO_NOT_TRY_ANOTHER)
610 eth_try_another(!NetRestarted);
613 if (NetRestartWrap) {
616 NetSetTimeout(10000UL, startAgainTimeout);
617 net_set_udp_handler(NULL);
619 net_set_state(NETLOOP_FAIL);
622 net_set_state(NETLOOP_RESTART);
626 /**********************************************************************/
631 static void dummy_handler(uchar *pkt, unsigned dport,
632 IPaddr_t sip, unsigned sport,
637 rxhand_f *net_get_udp_handler(void)
639 return udp_packet_handler;
642 void net_set_udp_handler(rxhand_f *f)
644 debug_cond(DEBUG_INT_STATE, "--- NetLoop UDP handler set (%p)\n", f);
646 udp_packet_handler = dummy_handler;
648 udp_packet_handler = f;
651 rxhand_f *net_get_arp_handler(void)
653 return arp_packet_handler;
656 void net_set_arp_handler(rxhand_f *f)
658 debug_cond(DEBUG_INT_STATE, "--- NetLoop ARP handler set (%p)\n", f);
660 arp_packet_handler = dummy_handler;
662 arp_packet_handler = f;
665 #ifdef CONFIG_CMD_TFTPPUT
666 void net_set_icmp_handler(rxhand_icmp_f *f)
668 packet_icmp_handler = f;
673 NetSetTimeout(ulong iv, thand_f *f)
676 debug_cond(DEBUG_INT_STATE,
677 "--- NetLoop timeout handler cancelled\n");
678 timeHandler = (thand_f *)0;
680 debug_cond(DEBUG_INT_STATE,
681 "--- NetLoop timeout handler set (%p)\n", f);
683 timeStart = get_timer(0);
684 timeDelta = iv * CONFIG_SYS_HZ / 1000;
688 int NetSendUDPPacket(uchar *ether, IPaddr_t dest, int dport, int sport,
695 /* make sure the NetTxPacket is initialized (NetInit() was called) */
696 assert(NetTxPacket != NULL);
697 if (NetTxPacket == NULL)
700 /* convert to new style broadcast */
704 /* if broadcast, make the ether address a broadcast and don't do ARP */
705 if (dest == 0xFFFFFFFF)
706 ether = NetBcastAddr;
708 pkt = (uchar *)NetTxPacket;
710 eth_hdr_size = NetSetEther(pkt, ether, PROT_IP);
712 net_set_udp_header(pkt, dest, dport, sport, payload_len);
713 pkt_hdr_size = eth_hdr_size + IP_UDP_HDR_SIZE;
715 /* if MAC address was not discovered yet, do an ARP request */
716 if (memcmp(ether, NetEtherNullAddr, 6) == 0) {
717 debug_cond(DEBUG_DEV_PKT, "sending ARP for %pI4\n", &dest);
719 /* save the ip and eth addr for the packet to send after arp */
720 NetArpWaitPacketIP = dest;
721 NetArpWaitPacketMAC = ether;
723 /* size of the waiting packet */
724 NetArpWaitTxPacketSize = pkt_hdr_size + payload_len;
726 /* and do the ARP request */
728 NetArpWaitTimerStart = get_timer(0);
730 return 1; /* waiting */
732 debug_cond(DEBUG_DEV_PKT, "sending UDP to %pI4/%pM\n",
734 NetSendPacket(NetTxPacket, pkt_hdr_size + payload_len);
735 return 0; /* transmitted */
739 #ifdef CONFIG_IP_DEFRAG
741 * This function collects fragments in a single packet, according
742 * to the algorithm in RFC815. It returns NULL or the pointer to
743 * a complete packet, in static storage
745 #ifndef CONFIG_NET_MAXDEFRAG
746 #define CONFIG_NET_MAXDEFRAG 16384
749 * MAXDEFRAG, above, is chosen in the config file and is real data
750 * so we need to add the NFS overhead, which is more than TFTP.
751 * To use sizeof in the internal unnamed structures, we need a real
752 * instance (can't do "sizeof(struct rpc_t.u.reply))", unfortunately).
753 * The compiler doesn't complain nor allocates the actual structure
755 static struct rpc_t rpc_specimen;
756 #define IP_PKTSIZE (CONFIG_NET_MAXDEFRAG + sizeof(rpc_specimen.u.reply))
758 #define IP_MAXUDP (IP_PKTSIZE - IP_HDR_SIZE)
761 * this is the packet being assembled, either data or frag control.
762 * Fragments go by 8 bytes, so this union must be 8 bytes long
765 /* first_byte is address of this structure */
766 u16 last_byte; /* last byte in this hole + 1 (begin of next hole) */
767 u16 next_hole; /* index of next (in 8-b blocks), 0 == none */
768 u16 prev_hole; /* index of prev, 0 == none */
772 static struct ip_udp_hdr *__NetDefragment(struct ip_udp_hdr *ip, int *lenp)
774 static uchar pkt_buff[IP_PKTSIZE] __aligned(PKTALIGN);
775 static u16 first_hole, total_len;
776 struct hole *payload, *thisfrag, *h, *newh;
777 struct ip_udp_hdr *localip = (struct ip_udp_hdr *)pkt_buff;
778 uchar *indata = (uchar *)ip;
779 int offset8, start, len, done = 0;
780 u16 ip_off = ntohs(ip->ip_off);
782 /* payload starts after IP header, this fragment is in there */
783 payload = (struct hole *)(pkt_buff + IP_HDR_SIZE);
784 offset8 = (ip_off & IP_OFFS);
785 thisfrag = payload + offset8;
787 len = ntohs(ip->ip_len) - IP_HDR_SIZE;
789 if (start + len > IP_MAXUDP) /* fragment extends too far */
792 if (!total_len || localip->ip_id != ip->ip_id) {
793 /* new (or different) packet, reset structs */
795 payload[0].last_byte = ~0;
796 payload[0].next_hole = 0;
797 payload[0].prev_hole = 0;
799 /* any IP header will work, copy the first we received */
800 memcpy(localip, ip, IP_HDR_SIZE);
804 * What follows is the reassembly algorithm. We use the payload
805 * array as a linked list of hole descriptors, as each hole starts
806 * at a multiple of 8 bytes. However, last byte can be whatever value,
807 * so it is represented as byte count, not as 8-byte blocks.
810 h = payload + first_hole;
811 while (h->last_byte < start) {
813 /* no hole that far away */
816 h = payload + h->next_hole;
819 /* last fragment may be 1..7 bytes, the "+7" forces acceptance */
820 if (offset8 + ((len + 7) / 8) <= h - payload) {
821 /* no overlap with holes (dup fragment?) */
825 if (!(ip_off & IP_FLAGS_MFRAG)) {
826 /* no more fragmentss: truncate this (last) hole */
827 total_len = start + len;
828 h->last_byte = start + len;
832 * There is some overlap: fix the hole list. This code doesn't
833 * deal with a fragment that overlaps with two different holes
834 * (thus being a superset of a previously-received fragment).
837 if ((h >= thisfrag) && (h->last_byte <= start + len)) {
838 /* complete overlap with hole: remove hole */
839 if (!h->prev_hole && !h->next_hole) {
840 /* last remaining hole */
842 } else if (!h->prev_hole) {
844 first_hole = h->next_hole;
845 payload[h->next_hole].prev_hole = 0;
846 } else if (!h->next_hole) {
848 payload[h->prev_hole].next_hole = 0;
850 /* in the middle of the list */
851 payload[h->next_hole].prev_hole = h->prev_hole;
852 payload[h->prev_hole].next_hole = h->next_hole;
855 } else if (h->last_byte <= start + len) {
856 /* overlaps with final part of the hole: shorten this hole */
857 h->last_byte = start;
859 } else if (h >= thisfrag) {
860 /* overlaps with initial part of the hole: move this hole */
861 newh = thisfrag + (len / 8);
865 payload[h->next_hole].prev_hole = (h - payload);
867 payload[h->prev_hole].next_hole = (h - payload);
869 first_hole = (h - payload);
872 /* fragment sits in the middle: split the hole */
873 newh = thisfrag + (len / 8);
875 h->last_byte = start;
876 h->next_hole = (newh - payload);
877 newh->prev_hole = (h - payload);
879 payload[newh->next_hole].prev_hole = (newh - payload);
882 /* finally copy this fragment and possibly return whole packet */
883 memcpy((uchar *)thisfrag, indata + IP_HDR_SIZE, len);
887 localip->ip_len = htons(total_len);
888 *lenp = total_len + IP_HDR_SIZE;
892 static inline struct ip_udp_hdr *NetDefragment(struct ip_udp_hdr *ip, int *lenp)
894 u16 ip_off = ntohs(ip->ip_off);
895 if (!(ip_off & (IP_OFFS | IP_FLAGS_MFRAG)))
896 return ip; /* not a fragment */
897 return __NetDefragment(ip, lenp);
900 #else /* !CONFIG_IP_DEFRAG */
902 static inline struct ip_udp_hdr *NetDefragment(struct ip_udp_hdr *ip, int *lenp)
904 u16 ip_off = ntohs(ip->ip_off);
905 if (!(ip_off & (IP_OFFS | IP_FLAGS_MFRAG)))
906 return ip; /* not a fragment */
912 * Receive an ICMP packet. We deal with REDIRECT and PING here, and silently
915 * @parma ip IP packet containing the ICMP
917 static void receive_icmp(struct ip_udp_hdr *ip, int len,
918 IPaddr_t src_ip, struct ethernet_hdr *et)
920 struct icmp_hdr *icmph = (struct icmp_hdr *)&ip->udp_src;
922 switch (icmph->type) {
924 if (icmph->code != ICMP_REDIR_HOST)
926 printf(" ICMP Host Redirect to %pI4 ",
930 #if defined(CONFIG_CMD_PING)
931 ping_receive(et, ip, len);
933 #ifdef CONFIG_CMD_TFTPPUT
934 if (packet_icmp_handler)
935 packet_icmp_handler(icmph->type, icmph->code,
936 ntohs(ip->udp_dst), src_ip, ntohs(ip->udp_src),
937 icmph->un.data, ntohs(ip->udp_len));
944 NetReceive(uchar *inpkt, int len)
946 struct ethernet_hdr *et;
947 struct ip_udp_hdr *ip;
951 #if defined(CONFIG_CMD_CDP)
954 ushort cti = 0, vlanid = VLAN_NONE, myvlanid, mynvlanid;
956 debug_cond(DEBUG_NET_PKT, "packet received\n");
959 NetRxPacketLen = len;
960 et = (struct ethernet_hdr *)inpkt;
962 /* too small packet? */
963 if (len < ETHER_HDR_SIZE)
968 (*push_packet)(inpkt, len);
973 #if defined(CONFIG_CMD_CDP)
974 /* keep track if packet is CDP */
975 iscdp = is_cdp_packet(et->et_dest);
978 myvlanid = ntohs(NetOurVLAN);
979 if (myvlanid == (ushort)-1)
980 myvlanid = VLAN_NONE;
981 mynvlanid = ntohs(NetOurNativeVLAN);
982 if (mynvlanid == (ushort)-1)
983 mynvlanid = VLAN_NONE;
985 eth_proto = ntohs(et->et_protlen);
987 if (eth_proto < 1514) {
988 struct e802_hdr *et802 = (struct e802_hdr *)et;
990 * Got a 802.2 packet. Check the other protocol field.
991 * XXX VLAN over 802.2+SNAP not implemented!
993 eth_proto = ntohs(et802->et_prot);
995 ip = (struct ip_udp_hdr *)(inpkt + E802_HDR_SIZE);
996 len -= E802_HDR_SIZE;
998 } else if (eth_proto != PROT_VLAN) { /* normal packet */
999 ip = (struct ip_udp_hdr *)(inpkt + ETHER_HDR_SIZE);
1000 len -= ETHER_HDR_SIZE;
1002 } else { /* VLAN packet */
1003 struct vlan_ethernet_hdr *vet =
1004 (struct vlan_ethernet_hdr *)et;
1006 debug_cond(DEBUG_NET_PKT, "VLAN packet received\n");
1008 /* too small packet? */
1009 if (len < VLAN_ETHER_HDR_SIZE)
1012 /* if no VLAN active */
1013 if ((ntohs(NetOurVLAN) & VLAN_IDMASK) == VLAN_NONE
1014 #if defined(CONFIG_CMD_CDP)
1020 cti = ntohs(vet->vet_tag);
1021 vlanid = cti & VLAN_IDMASK;
1022 eth_proto = ntohs(vet->vet_type);
1024 ip = (struct ip_udp_hdr *)(inpkt + VLAN_ETHER_HDR_SIZE);
1025 len -= VLAN_ETHER_HDR_SIZE;
1028 debug_cond(DEBUG_NET_PKT, "Receive from protocol 0x%x\n", eth_proto);
1030 #if defined(CONFIG_CMD_CDP)
1032 cdp_receive((uchar *)ip, len);
1037 if ((myvlanid & VLAN_IDMASK) != VLAN_NONE) {
1038 if (vlanid == VLAN_NONE)
1039 vlanid = (mynvlanid & VLAN_IDMASK);
1041 if (vlanid != (myvlanid & VLAN_IDMASK))
1045 switch (eth_proto) {
1048 ArpReceive(et, ip, len);
1051 #ifdef CONFIG_CMD_RARP
1053 rarp_receive(ip, len);
1057 debug_cond(DEBUG_NET_PKT, "Got IP\n");
1058 /* Before we start poking the header, make sure it is there */
1059 if (len < IP_UDP_HDR_SIZE) {
1060 debug("len bad %d < %lu\n", len,
1061 (ulong)IP_UDP_HDR_SIZE);
1064 /* Check the packet length */
1065 if (len < ntohs(ip->ip_len)) {
1066 debug("len bad %d < %d\n", len, ntohs(ip->ip_len));
1069 len = ntohs(ip->ip_len);
1070 debug_cond(DEBUG_NET_PKT, "len=%d, v=%02x\n",
1071 len, ip->ip_hl_v & 0xff);
1073 /* Can't deal with anything except IPv4 */
1074 if ((ip->ip_hl_v & 0xf0) != 0x40)
1076 /* Can't deal with IP options (headers != 20 bytes) */
1077 if ((ip->ip_hl_v & 0x0f) > 0x05)
1079 /* Check the Checksum of the header */
1080 if (!NetCksumOk((uchar *)ip, IP_HDR_SIZE / 2)) {
1081 debug("checksum bad\n");
1084 /* If it is not for us, ignore it */
1085 dst_ip = NetReadIP(&ip->ip_dst);
1086 if (NetOurIP && dst_ip != NetOurIP && dst_ip != 0xFFFFFFFF) {
1087 #ifdef CONFIG_MCAST_TFTP
1088 if (Mcast_addr != dst_ip)
1092 /* Read source IP address for later use */
1093 src_ip = NetReadIP(&ip->ip_src);
1095 * The function returns the unchanged packet if it's not
1096 * a fragment, and either the complete packet or NULL if
1097 * it is a fragment (if !CONFIG_IP_DEFRAG, it returns NULL)
1099 ip = NetDefragment(ip, &len);
1103 * watch for ICMP host redirects
1105 * There is no real handler code (yet). We just watch
1106 * for ICMP host redirect messages. In case anybody
1107 * sees these messages: please contact me
1108 * (wd@denx.de), or - even better - send me the
1109 * necessary fixes :-)
1111 * Note: in all cases where I have seen this so far
1112 * it was a problem with the router configuration,
1113 * for instance when a router was configured in the
1114 * BOOTP reply, but the TFTP server was on the same
1115 * subnet. So this is probably a warning that your
1116 * configuration might be wrong. But I'm not really
1117 * sure if there aren't any other situations.
1119 * Simon Glass <sjg@chromium.org>: We get an ICMP when
1120 * we send a tftp packet to a dead connection, or when
1121 * there is no server at the other end.
1123 if (ip->ip_p == IPPROTO_ICMP) {
1124 receive_icmp(ip, len, src_ip, et);
1126 } else if (ip->ip_p != IPPROTO_UDP) { /* Only UDP packets */
1130 debug_cond(DEBUG_DEV_PKT,
1131 "received UDP (to=%pI4, from=%pI4, len=%d)\n",
1132 &dst_ip, &src_ip, len);
1134 #ifdef CONFIG_UDP_CHECKSUM
1135 if (ip->udp_xsum != 0) {
1141 xsum += (ntohs(ip->udp_len));
1142 xsum += (ntohl(ip->ip_src) >> 16) & 0x0000ffff;
1143 xsum += (ntohl(ip->ip_src) >> 0) & 0x0000ffff;
1144 xsum += (ntohl(ip->ip_dst) >> 16) & 0x0000ffff;
1145 xsum += (ntohl(ip->ip_dst) >> 0) & 0x0000ffff;
1147 sumlen = ntohs(ip->udp_len);
1148 sumptr = (ushort *) &(ip->udp_src);
1150 while (sumlen > 1) {
1153 sumdata = *sumptr++;
1154 xsum += ntohs(sumdata);
1160 sumdata = *(unsigned char *) sumptr;
1161 sumdata = (sumdata << 8) & 0xff00;
1164 while ((xsum >> 16) != 0) {
1165 xsum = (xsum & 0x0000ffff) +
1166 ((xsum >> 16) & 0x0000ffff);
1168 if ((xsum != 0x00000000) && (xsum != 0x0000ffff)) {
1169 printf(" UDP wrong checksum %08lx %08x\n",
1170 xsum, ntohs(ip->udp_xsum));
1177 #ifdef CONFIG_NETCONSOLE
1178 nc_input_packet((uchar *)ip + IP_UDP_HDR_SIZE,
1182 ntohs(ip->udp_len) - UDP_HDR_SIZE);
1185 * IP header OK. Pass the packet to the current handler.
1187 (*udp_packet_handler)((uchar *)ip + IP_UDP_HDR_SIZE,
1191 ntohs(ip->udp_len) - UDP_HDR_SIZE);
1197 /**********************************************************************/
1199 static int net_check_prereq(enum proto_t protocol)
1203 #if defined(CONFIG_CMD_PING)
1205 if (NetPingIP == 0) {
1206 puts("*** ERROR: ping address not given\n");
1211 #if defined(CONFIG_CMD_SNTP)
1213 if (NetNtpServerIP == 0) {
1214 puts("*** ERROR: NTP server address not given\n");
1219 #if defined(CONFIG_CMD_DNS)
1221 if (NetOurDNSIP == 0) {
1222 puts("*** ERROR: DNS server address not given\n");
1227 #if defined(CONFIG_CMD_NFS)
1232 if (NetServerIP == 0) {
1233 puts("*** ERROR: `serverip' not set\n");
1236 #if defined(CONFIG_CMD_PING) || defined(CONFIG_CMD_SNTP) || \
1237 defined(CONFIG_CMD_DNS)
1244 if (NetOurIP == 0) {
1245 puts("*** ERROR: `ipaddr' not set\n");
1250 #ifdef CONFIG_CMD_RARP
1257 if (memcmp(NetOurEther, "\0\0\0\0\0\0", 6) == 0) {
1258 int num = eth_get_dev_index();
1262 puts("*** ERROR: No ethernet found.\n");
1265 puts("*** ERROR: `ethaddr' not set\n");
1268 printf("*** ERROR: `eth%daddr' not set\n",
1282 /**********************************************************************/
1285 NetCksumOk(uchar *ptr, int len)
1287 return !((NetCksum(ptr, len) + 1) & 0xfffe);
1292 NetCksum(uchar *ptr, int len)
1295 ushort *p = (ushort *)ptr;
1300 xsum = (xsum & 0xffff) + (xsum >> 16);
1301 xsum = (xsum & 0xffff) + (xsum >> 16);
1302 return xsum & 0xffff;
1310 myvlanid = ntohs(NetOurVLAN);
1311 if (myvlanid == (ushort)-1)
1312 myvlanid = VLAN_NONE;
1314 return ((myvlanid & VLAN_IDMASK) == VLAN_NONE) ? ETHER_HDR_SIZE :
1315 VLAN_ETHER_HDR_SIZE;
1319 NetSetEther(uchar *xet, uchar * addr, uint prot)
1321 struct ethernet_hdr *et = (struct ethernet_hdr *)xet;
1324 myvlanid = ntohs(NetOurVLAN);
1325 if (myvlanid == (ushort)-1)
1326 myvlanid = VLAN_NONE;
1328 memcpy(et->et_dest, addr, 6);
1329 memcpy(et->et_src, NetOurEther, 6);
1330 if ((myvlanid & VLAN_IDMASK) == VLAN_NONE) {
1331 et->et_protlen = htons(prot);
1332 return ETHER_HDR_SIZE;
1334 struct vlan_ethernet_hdr *vet =
1335 (struct vlan_ethernet_hdr *)xet;
1337 vet->vet_vlan_type = htons(PROT_VLAN);
1338 vet->vet_tag = htons((0 << 5) | (myvlanid & VLAN_IDMASK));
1339 vet->vet_type = htons(prot);
1340 return VLAN_ETHER_HDR_SIZE;
1344 int net_update_ether(struct ethernet_hdr *et, uchar *addr, uint prot)
1348 memcpy(et->et_dest, addr, 6);
1349 memcpy(et->et_src, NetOurEther, 6);
1350 protlen = ntohs(et->et_protlen);
1351 if (protlen == PROT_VLAN) {
1352 struct vlan_ethernet_hdr *vet =
1353 (struct vlan_ethernet_hdr *)et;
1354 vet->vet_type = htons(prot);
1355 return VLAN_ETHER_HDR_SIZE;
1356 } else if (protlen > 1514) {
1357 et->et_protlen = htons(prot);
1358 return ETHER_HDR_SIZE;
1361 struct e802_hdr *et802 = (struct e802_hdr *)et;
1362 et802->et_prot = htons(prot);
1363 return E802_HDR_SIZE;
1367 void net_set_ip_header(uchar *pkt, IPaddr_t dest, IPaddr_t source)
1369 struct ip_udp_hdr *ip = (struct ip_udp_hdr *)pkt;
1372 * Construct an IP header.
1374 /* IP_HDR_SIZE / 4 (not including UDP) */
1377 ip->ip_len = htons(IP_HDR_SIZE);
1378 ip->ip_id = htons(NetIPID++);
1379 ip->ip_off = htons(IP_FLAGS_DFRAG); /* Don't fragment */
1382 /* already in network byte order */
1383 NetCopyIP((void *)&ip->ip_src, &source);
1384 /* already in network byte order */
1385 NetCopyIP((void *)&ip->ip_dst, &dest);
1388 void net_set_udp_header(uchar *pkt, IPaddr_t dest, int dport, int sport,
1391 struct ip_udp_hdr *ip = (struct ip_udp_hdr *)pkt;
1394 * If the data is an odd number of bytes, zero the
1395 * byte after the last byte so that the checksum
1399 pkt[IP_UDP_HDR_SIZE + len] = 0;
1401 net_set_ip_header(pkt, dest, NetOurIP);
1402 ip->ip_len = htons(IP_UDP_HDR_SIZE + len);
1403 ip->ip_p = IPPROTO_UDP;
1404 ip->ip_sum = ~NetCksum((uchar *)ip, IP_HDR_SIZE >> 1);
1406 ip->udp_src = htons(sport);
1407 ip->udp_dst = htons(dport);
1408 ip->udp_len = htons(UDP_HDR_SIZE + len);
1412 void copy_filename(char *dst, const char *src, int size)
1414 if (*src && (*src == '"')) {
1419 while ((--size > 0) && *src && (*src != '"'))
1424 #if defined(CONFIG_CMD_NFS) || \
1425 defined(CONFIG_CMD_SNTP) || \
1426 defined(CONFIG_CMD_DNS)
1428 * make port a little random (1024-17407)
1429 * This keeps the math somewhat trivial to compute, and seems to work with
1430 * all supported protocols/clients/servers
1432 unsigned int random_port(void)
1434 return 1024 + (get_timer(0) % 0x4000);
1438 void ip_to_string(IPaddr_t x, char *s)
1441 sprintf(s, "%d.%d.%d.%d",
1442 (int) ((x >> 24) & 0xff),
1443 (int) ((x >> 16) & 0xff),
1444 (int) ((x >> 8) & 0xff), (int) ((x >> 0) & 0xff)
1448 void VLAN_to_string(ushort x, char *s)
1452 if (x == (ushort)-1)
1458 sprintf(s, "%d", x & VLAN_IDMASK);
1461 ushort string_to_VLAN(const char *s)
1466 return htons(VLAN_NONE);
1468 if (*s < '0' || *s > '9')
1471 id = (ushort)simple_strtoul(s, NULL, 10);
1476 ushort getenv_VLAN(char *var)
1478 return string_to_VLAN(getenv(var));