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;
275 memcpy(NetOurEther, eth_get_dev()->enetaddr, 6);
280 static void net_clear_handlers(void)
282 net_set_udp_handler(NULL);
283 net_set_arp_handler(NULL);
284 NetSetTimeout(0, NULL);
287 static void net_cleanup_loop(void)
289 net_clear_handlers();
294 static int first_call = 1;
298 * Setup packet buffers, aligned correctly.
302 NetTxPacket = &PktBuf[0] + (PKTALIGN - 1);
303 NetTxPacket -= (ulong)NetTxPacket % PKTALIGN;
304 for (i = 0; i < PKTBUFSRX; i++)
305 NetRxPackets[i] = NetTxPacket + (i + 1) * PKTSIZE_ALIGN;
308 net_clear_handlers();
310 /* Only need to setup buffer pointers once. */
317 /**********************************************************************/
319 * Main network processing loop.
322 int NetLoop(enum proto_t protocol)
330 debug_cond(DEBUG_INT_STATE, "--- NetLoop Entry\n");
332 bootstage_mark_name(BOOTSTAGE_ID_ETH_START, "eth_start");
334 if (eth_is_on_demand_init() || protocol != NETCONS) {
337 if (eth_init(bd) < 0) {
342 eth_init_state_only(bd);
345 net_set_state(NETLOOP_CONTINUE);
348 * Start the ball rolling with the given start function. From
349 * here on, this code is a state machine driven by received
350 * packets and timer events.
352 debug_cond(DEBUG_INT_STATE, "--- NetLoop Init\n");
355 switch (net_check_prereq(protocol)) {
357 /* network not configured */
362 /* network device not configured */
367 NetBootFileXferSize = 0;
370 #ifdef CONFIG_CMD_TFTPPUT
373 /* always use ARP to get server ethernet address */
376 #ifdef CONFIG_CMD_TFTPSRV
381 #if defined(CONFIG_CMD_DHCP)
385 DhcpRequest(); /* Basically same as BOOTP */
395 #if defined(CONFIG_CMD_RARP)
402 #if defined(CONFIG_CMD_PING)
407 #if defined(CONFIG_CMD_NFS)
412 #if defined(CONFIG_CMD_CDP)
417 #ifdef CONFIG_NETCONSOLE
422 #if defined(CONFIG_CMD_SNTP)
427 #if defined(CONFIG_CMD_DNS)
432 #if defined(CONFIG_CMD_LINK_LOCAL)
444 #if defined(CONFIG_MII) || defined(CONFIG_CMD_MII)
445 #if defined(CONFIG_SYS_FAULT_ECHO_LINK_DOWN) && \
446 defined(CONFIG_STATUS_LED) && \
447 defined(STATUS_LED_RED)
449 * Echo the inverted link state to the fault LED.
451 if (miiphy_link(eth_get_dev()->name, CONFIG_SYS_FAULT_MII_ADDR))
452 status_led_set(STATUS_LED_RED, STATUS_LED_OFF);
454 status_led_set(STATUS_LED_RED, STATUS_LED_ON);
455 #endif /* CONFIG_SYS_FAULT_ECHO_LINK_DOWN, ... */
456 #endif /* CONFIG_MII, ... */
459 * Main packet reception loop. Loop receiving packets until
460 * someone sets `net_state' to a state that terminates.
464 #ifdef CONFIG_SHOW_ACTIVITY
468 * Check the ethernet for a new packet. The ethernet
469 * receive routine will process it.
474 * Abort if ctrl-c was pressed.
477 /* cancel any ARP that may not have completed */
478 NetArpWaitPacketIP = 0;
482 /* Invalidate the last protocol */
483 eth_set_last_protocol(BOOTP);
486 /* include a debug print as well incase the debug
487 messages are directed to stderr */
488 debug_cond(DEBUG_INT_STATE, "--- NetLoop Abort!\n");
495 * Check for a timeout, and run the timeout handler
498 if (timeHandler && ((get_timer(0) - timeStart) > timeDelta)) {
501 #if defined(CONFIG_MII) || defined(CONFIG_CMD_MII)
502 #if defined(CONFIG_SYS_FAULT_ECHO_LINK_DOWN) && \
503 defined(CONFIG_STATUS_LED) && \
504 defined(STATUS_LED_RED)
506 * Echo the inverted link state to the fault LED.
508 if (miiphy_link(eth_get_dev()->name,
509 CONFIG_SYS_FAULT_MII_ADDR)) {
510 status_led_set(STATUS_LED_RED, STATUS_LED_OFF);
512 status_led_set(STATUS_LED_RED, STATUS_LED_ON);
514 #endif /* CONFIG_SYS_FAULT_ECHO_LINK_DOWN, ... */
515 #endif /* CONFIG_MII, ... */
516 debug_cond(DEBUG_INT_STATE, "--- NetLoop timeout\n");
518 timeHandler = (thand_f *)0;
525 case NETLOOP_RESTART:
529 case NETLOOP_SUCCESS:
531 if (NetBootFileXferSize > 0) {
532 printf("Bytes transferred = %ld (%lx hex)\n",
534 NetBootFileXferSize);
535 setenv_hex("filesize", NetBootFileXferSize);
536 setenv_hex("fileaddr", load_addr);
538 if (protocol != NETCONS)
541 eth_halt_state_only();
543 eth_set_last_protocol(protocol);
545 ret = NetBootFileXferSize;
546 debug_cond(DEBUG_INT_STATE, "--- NetLoop Success!\n");
551 /* Invalidate the last protocol */
552 eth_set_last_protocol(BOOTP);
553 debug_cond(DEBUG_INT_STATE, "--- NetLoop Fail!\n");
556 case NETLOOP_CONTINUE:
562 #ifdef CONFIG_CMD_TFTPPUT
563 /* Clear out the handlers */
564 net_set_udp_handler(NULL);
565 net_set_icmp_handler(NULL);
570 /**********************************************************************/
573 startAgainTimeout(void)
575 net_set_state(NETLOOP_RESTART);
578 void NetStartAgain(void)
581 int retry_forever = 0;
582 unsigned long retrycnt = 0;
584 nretry = getenv("netretry");
586 if (!strcmp(nretry, "yes"))
588 else if (!strcmp(nretry, "no"))
590 else if (!strcmp(nretry, "once"))
593 retrycnt = simple_strtoul(nretry, NULL, 0);
597 if ((!retry_forever) && (NetTryCount >= retrycnt)) {
599 net_set_state(NETLOOP_FAIL);
606 #if !defined(CONFIG_NET_DO_NOT_TRY_ANOTHER)
607 eth_try_another(!NetRestarted);
610 if (NetRestartWrap) {
613 NetSetTimeout(10000UL, startAgainTimeout);
614 net_set_udp_handler(NULL);
616 net_set_state(NETLOOP_FAIL);
619 net_set_state(NETLOOP_RESTART);
623 /**********************************************************************/
628 static void dummy_handler(uchar *pkt, unsigned dport,
629 IPaddr_t sip, unsigned sport,
634 rxhand_f *net_get_udp_handler(void)
636 return udp_packet_handler;
639 void net_set_udp_handler(rxhand_f *f)
641 debug_cond(DEBUG_INT_STATE, "--- NetLoop UDP handler set (%p)\n", f);
643 udp_packet_handler = dummy_handler;
645 udp_packet_handler = f;
648 rxhand_f *net_get_arp_handler(void)
650 return arp_packet_handler;
653 void net_set_arp_handler(rxhand_f *f)
655 debug_cond(DEBUG_INT_STATE, "--- NetLoop ARP handler set (%p)\n", f);
657 arp_packet_handler = dummy_handler;
659 arp_packet_handler = f;
662 #ifdef CONFIG_CMD_TFTPPUT
663 void net_set_icmp_handler(rxhand_icmp_f *f)
665 packet_icmp_handler = f;
670 NetSetTimeout(ulong iv, thand_f *f)
673 debug_cond(DEBUG_INT_STATE,
674 "--- NetLoop timeout handler cancelled\n");
675 timeHandler = (thand_f *)0;
677 debug_cond(DEBUG_INT_STATE,
678 "--- NetLoop timeout handler set (%p)\n", f);
680 timeStart = get_timer(0);
681 timeDelta = iv * CONFIG_SYS_HZ / 1000;
685 int NetSendUDPPacket(uchar *ether, IPaddr_t dest, int dport, int sport,
692 /* make sure the NetTxPacket is initialized (NetInit() was called) */
693 assert(NetTxPacket != NULL);
694 if (NetTxPacket == NULL)
697 /* convert to new style broadcast */
701 /* if broadcast, make the ether address a broadcast and don't do ARP */
702 if (dest == 0xFFFFFFFF)
703 ether = NetBcastAddr;
705 pkt = (uchar *)NetTxPacket;
707 eth_hdr_size = NetSetEther(pkt, ether, PROT_IP);
709 net_set_udp_header(pkt, dest, dport, sport, payload_len);
710 pkt_hdr_size = eth_hdr_size + IP_UDP_HDR_SIZE;
712 /* if MAC address was not discovered yet, do an ARP request */
713 if (memcmp(ether, NetEtherNullAddr, 6) == 0) {
714 debug_cond(DEBUG_DEV_PKT, "sending ARP for %pI4\n", &dest);
716 /* save the ip and eth addr for the packet to send after arp */
717 NetArpWaitPacketIP = dest;
718 NetArpWaitPacketMAC = ether;
720 /* size of the waiting packet */
721 NetArpWaitTxPacketSize = pkt_hdr_size + payload_len;
723 /* and do the ARP request */
725 NetArpWaitTimerStart = get_timer(0);
727 return 1; /* waiting */
729 debug_cond(DEBUG_DEV_PKT, "sending UDP to %pI4/%pM\n",
731 NetSendPacket(NetTxPacket, pkt_hdr_size + payload_len);
732 return 0; /* transmitted */
736 #ifdef CONFIG_IP_DEFRAG
738 * This function collects fragments in a single packet, according
739 * to the algorithm in RFC815. It returns NULL or the pointer to
740 * a complete packet, in static storage
742 #ifndef CONFIG_NET_MAXDEFRAG
743 #define CONFIG_NET_MAXDEFRAG 16384
746 * MAXDEFRAG, above, is chosen in the config file and is real data
747 * so we need to add the NFS overhead, which is more than TFTP.
748 * To use sizeof in the internal unnamed structures, we need a real
749 * instance (can't do "sizeof(struct rpc_t.u.reply))", unfortunately).
750 * The compiler doesn't complain nor allocates the actual structure
752 static struct rpc_t rpc_specimen;
753 #define IP_PKTSIZE (CONFIG_NET_MAXDEFRAG + sizeof(rpc_specimen.u.reply))
755 #define IP_MAXUDP (IP_PKTSIZE - IP_HDR_SIZE)
758 * this is the packet being assembled, either data or frag control.
759 * Fragments go by 8 bytes, so this union must be 8 bytes long
762 /* first_byte is address of this structure */
763 u16 last_byte; /* last byte in this hole + 1 (begin of next hole) */
764 u16 next_hole; /* index of next (in 8-b blocks), 0 == none */
765 u16 prev_hole; /* index of prev, 0 == none */
769 static struct ip_udp_hdr *__NetDefragment(struct ip_udp_hdr *ip, int *lenp)
771 static uchar pkt_buff[IP_PKTSIZE] __aligned(PKTALIGN);
772 static u16 first_hole, total_len;
773 struct hole *payload, *thisfrag, *h, *newh;
774 struct ip_udp_hdr *localip = (struct ip_udp_hdr *)pkt_buff;
775 uchar *indata = (uchar *)ip;
776 int offset8, start, len, done = 0;
777 u16 ip_off = ntohs(ip->ip_off);
779 /* payload starts after IP header, this fragment is in there */
780 payload = (struct hole *)(pkt_buff + IP_HDR_SIZE);
781 offset8 = (ip_off & IP_OFFS);
782 thisfrag = payload + offset8;
784 len = ntohs(ip->ip_len) - IP_HDR_SIZE;
786 if (start + len > IP_MAXUDP) /* fragment extends too far */
789 if (!total_len || localip->ip_id != ip->ip_id) {
790 /* new (or different) packet, reset structs */
792 payload[0].last_byte = ~0;
793 payload[0].next_hole = 0;
794 payload[0].prev_hole = 0;
796 /* any IP header will work, copy the first we received */
797 memcpy(localip, ip, IP_HDR_SIZE);
801 * What follows is the reassembly algorithm. We use the payload
802 * array as a linked list of hole descriptors, as each hole starts
803 * at a multiple of 8 bytes. However, last byte can be whatever value,
804 * so it is represented as byte count, not as 8-byte blocks.
807 h = payload + first_hole;
808 while (h->last_byte < start) {
810 /* no hole that far away */
813 h = payload + h->next_hole;
816 /* last fragment may be 1..7 bytes, the "+7" forces acceptance */
817 if (offset8 + ((len + 7) / 8) <= h - payload) {
818 /* no overlap with holes (dup fragment?) */
822 if (!(ip_off & IP_FLAGS_MFRAG)) {
823 /* no more fragmentss: truncate this (last) hole */
824 total_len = start + len;
825 h->last_byte = start + len;
829 * There is some overlap: fix the hole list. This code doesn't
830 * deal with a fragment that overlaps with two different holes
831 * (thus being a superset of a previously-received fragment).
834 if ((h >= thisfrag) && (h->last_byte <= start + len)) {
835 /* complete overlap with hole: remove hole */
836 if (!h->prev_hole && !h->next_hole) {
837 /* last remaining hole */
839 } else if (!h->prev_hole) {
841 first_hole = h->next_hole;
842 payload[h->next_hole].prev_hole = 0;
843 } else if (!h->next_hole) {
845 payload[h->prev_hole].next_hole = 0;
847 /* in the middle of the list */
848 payload[h->next_hole].prev_hole = h->prev_hole;
849 payload[h->prev_hole].next_hole = h->next_hole;
852 } else if (h->last_byte <= start + len) {
853 /* overlaps with final part of the hole: shorten this hole */
854 h->last_byte = start;
856 } else if (h >= thisfrag) {
857 /* overlaps with initial part of the hole: move this hole */
858 newh = thisfrag + (len / 8);
862 payload[h->next_hole].prev_hole = (h - payload);
864 payload[h->prev_hole].next_hole = (h - payload);
866 first_hole = (h - payload);
869 /* fragment sits in the middle: split the hole */
870 newh = thisfrag + (len / 8);
872 h->last_byte = start;
873 h->next_hole = (newh - payload);
874 newh->prev_hole = (h - payload);
876 payload[newh->next_hole].prev_hole = (newh - payload);
879 /* finally copy this fragment and possibly return whole packet */
880 memcpy((uchar *)thisfrag, indata + IP_HDR_SIZE, len);
884 localip->ip_len = htons(total_len);
885 *lenp = total_len + IP_HDR_SIZE;
889 static inline struct ip_udp_hdr *NetDefragment(struct ip_udp_hdr *ip, int *lenp)
891 u16 ip_off = ntohs(ip->ip_off);
892 if (!(ip_off & (IP_OFFS | IP_FLAGS_MFRAG)))
893 return ip; /* not a fragment */
894 return __NetDefragment(ip, lenp);
897 #else /* !CONFIG_IP_DEFRAG */
899 static inline struct ip_udp_hdr *NetDefragment(struct ip_udp_hdr *ip, int *lenp)
901 u16 ip_off = ntohs(ip->ip_off);
902 if (!(ip_off & (IP_OFFS | IP_FLAGS_MFRAG)))
903 return ip; /* not a fragment */
909 * Receive an ICMP packet. We deal with REDIRECT and PING here, and silently
912 * @parma ip IP packet containing the ICMP
914 static void receive_icmp(struct ip_udp_hdr *ip, int len,
915 IPaddr_t src_ip, struct ethernet_hdr *et)
917 struct icmp_hdr *icmph = (struct icmp_hdr *)&ip->udp_src;
919 switch (icmph->type) {
921 if (icmph->code != ICMP_REDIR_HOST)
923 printf(" ICMP Host Redirect to %pI4 ",
927 #if defined(CONFIG_CMD_PING)
928 ping_receive(et, ip, len);
930 #ifdef CONFIG_CMD_TFTPPUT
931 if (packet_icmp_handler)
932 packet_icmp_handler(icmph->type, icmph->code,
933 ntohs(ip->udp_dst), src_ip, ntohs(ip->udp_src),
934 icmph->un.data, ntohs(ip->udp_len));
941 NetReceive(uchar *inpkt, int len)
943 struct ethernet_hdr *et;
944 struct ip_udp_hdr *ip;
948 #if defined(CONFIG_CMD_CDP)
951 ushort cti = 0, vlanid = VLAN_NONE, myvlanid, mynvlanid;
953 debug_cond(DEBUG_NET_PKT, "packet received\n");
956 NetRxPacketLen = len;
957 et = (struct ethernet_hdr *)inpkt;
959 /* too small packet? */
960 if (len < ETHER_HDR_SIZE)
965 (*push_packet)(inpkt, len);
970 #if defined(CONFIG_CMD_CDP)
971 /* keep track if packet is CDP */
972 iscdp = is_cdp_packet(et->et_dest);
975 myvlanid = ntohs(NetOurVLAN);
976 if (myvlanid == (ushort)-1)
977 myvlanid = VLAN_NONE;
978 mynvlanid = ntohs(NetOurNativeVLAN);
979 if (mynvlanid == (ushort)-1)
980 mynvlanid = VLAN_NONE;
982 eth_proto = ntohs(et->et_protlen);
984 if (eth_proto < 1514) {
985 struct e802_hdr *et802 = (struct e802_hdr *)et;
987 * Got a 802.2 packet. Check the other protocol field.
988 * XXX VLAN over 802.2+SNAP not implemented!
990 eth_proto = ntohs(et802->et_prot);
992 ip = (struct ip_udp_hdr *)(inpkt + E802_HDR_SIZE);
993 len -= E802_HDR_SIZE;
995 } else if (eth_proto != PROT_VLAN) { /* normal packet */
996 ip = (struct ip_udp_hdr *)(inpkt + ETHER_HDR_SIZE);
997 len -= ETHER_HDR_SIZE;
999 } else { /* VLAN packet */
1000 struct vlan_ethernet_hdr *vet =
1001 (struct vlan_ethernet_hdr *)et;
1003 debug_cond(DEBUG_NET_PKT, "VLAN packet received\n");
1005 /* too small packet? */
1006 if (len < VLAN_ETHER_HDR_SIZE)
1009 /* if no VLAN active */
1010 if ((ntohs(NetOurVLAN) & VLAN_IDMASK) == VLAN_NONE
1011 #if defined(CONFIG_CMD_CDP)
1017 cti = ntohs(vet->vet_tag);
1018 vlanid = cti & VLAN_IDMASK;
1019 eth_proto = ntohs(vet->vet_type);
1021 ip = (struct ip_udp_hdr *)(inpkt + VLAN_ETHER_HDR_SIZE);
1022 len -= VLAN_ETHER_HDR_SIZE;
1025 debug_cond(DEBUG_NET_PKT, "Receive from protocol 0x%x\n", eth_proto);
1027 #if defined(CONFIG_CMD_CDP)
1029 cdp_receive((uchar *)ip, len);
1034 if ((myvlanid & VLAN_IDMASK) != VLAN_NONE) {
1035 if (vlanid == VLAN_NONE)
1036 vlanid = (mynvlanid & VLAN_IDMASK);
1038 if (vlanid != (myvlanid & VLAN_IDMASK))
1042 switch (eth_proto) {
1045 ArpReceive(et, ip, len);
1048 #ifdef CONFIG_CMD_RARP
1050 rarp_receive(ip, len);
1054 debug_cond(DEBUG_NET_PKT, "Got IP\n");
1055 /* Before we start poking the header, make sure it is there */
1056 if (len < IP_UDP_HDR_SIZE) {
1057 debug("len bad %d < %lu\n", len,
1058 (ulong)IP_UDP_HDR_SIZE);
1061 /* Check the packet length */
1062 if (len < ntohs(ip->ip_len)) {
1063 debug("len bad %d < %d\n", len, ntohs(ip->ip_len));
1066 len = ntohs(ip->ip_len);
1067 debug_cond(DEBUG_NET_PKT, "len=%d, v=%02x\n",
1068 len, ip->ip_hl_v & 0xff);
1070 /* Can't deal with anything except IPv4 */
1071 if ((ip->ip_hl_v & 0xf0) != 0x40)
1073 /* Can't deal with IP options (headers != 20 bytes) */
1074 if ((ip->ip_hl_v & 0x0f) > 0x05)
1076 /* Check the Checksum of the header */
1077 if (!NetCksumOk((uchar *)ip, IP_HDR_SIZE / 2)) {
1078 debug("checksum bad\n");
1081 /* If it is not for us, ignore it */
1082 dst_ip = NetReadIP(&ip->ip_dst);
1083 if (NetOurIP && dst_ip != NetOurIP && dst_ip != 0xFFFFFFFF) {
1084 #ifdef CONFIG_MCAST_TFTP
1085 if (Mcast_addr != dst_ip)
1089 /* Read source IP address for later use */
1090 src_ip = NetReadIP(&ip->ip_src);
1092 * The function returns the unchanged packet if it's not
1093 * a fragment, and either the complete packet or NULL if
1094 * it is a fragment (if !CONFIG_IP_DEFRAG, it returns NULL)
1096 ip = NetDefragment(ip, &len);
1100 * watch for ICMP host redirects
1102 * There is no real handler code (yet). We just watch
1103 * for ICMP host redirect messages. In case anybody
1104 * sees these messages: please contact me
1105 * (wd@denx.de), or - even better - send me the
1106 * necessary fixes :-)
1108 * Note: in all cases where I have seen this so far
1109 * it was a problem with the router configuration,
1110 * for instance when a router was configured in the
1111 * BOOTP reply, but the TFTP server was on the same
1112 * subnet. So this is probably a warning that your
1113 * configuration might be wrong. But I'm not really
1114 * sure if there aren't any other situations.
1116 * Simon Glass <sjg@chromium.org>: We get an ICMP when
1117 * we send a tftp packet to a dead connection, or when
1118 * there is no server at the other end.
1120 if (ip->ip_p == IPPROTO_ICMP) {
1121 receive_icmp(ip, len, src_ip, et);
1123 } else if (ip->ip_p != IPPROTO_UDP) { /* Only UDP packets */
1127 debug_cond(DEBUG_DEV_PKT,
1128 "received UDP (to=%pI4, from=%pI4, len=%d)\n",
1129 &dst_ip, &src_ip, len);
1131 #ifdef CONFIG_UDP_CHECKSUM
1132 if (ip->udp_xsum != 0) {
1138 xsum += (ntohs(ip->udp_len));
1139 xsum += (ntohl(ip->ip_src) >> 16) & 0x0000ffff;
1140 xsum += (ntohl(ip->ip_src) >> 0) & 0x0000ffff;
1141 xsum += (ntohl(ip->ip_dst) >> 16) & 0x0000ffff;
1142 xsum += (ntohl(ip->ip_dst) >> 0) & 0x0000ffff;
1144 sumlen = ntohs(ip->udp_len);
1145 sumptr = (ushort *) &(ip->udp_src);
1147 while (sumlen > 1) {
1150 sumdata = *sumptr++;
1151 xsum += ntohs(sumdata);
1157 sumdata = *(unsigned char *) sumptr;
1158 sumdata = (sumdata << 8) & 0xff00;
1161 while ((xsum >> 16) != 0) {
1162 xsum = (xsum & 0x0000ffff) +
1163 ((xsum >> 16) & 0x0000ffff);
1165 if ((xsum != 0x00000000) && (xsum != 0x0000ffff)) {
1166 printf(" UDP wrong checksum %08lx %08x\n",
1167 xsum, ntohs(ip->udp_xsum));
1174 #ifdef CONFIG_NETCONSOLE
1175 nc_input_packet((uchar *)ip + IP_UDP_HDR_SIZE,
1179 ntohs(ip->udp_len) - UDP_HDR_SIZE);
1182 * IP header OK. Pass the packet to the current handler.
1184 (*udp_packet_handler)((uchar *)ip + IP_UDP_HDR_SIZE,
1188 ntohs(ip->udp_len) - UDP_HDR_SIZE);
1194 /**********************************************************************/
1196 static int net_check_prereq(enum proto_t protocol)
1200 #if defined(CONFIG_CMD_PING)
1202 if (NetPingIP == 0) {
1203 puts("*** ERROR: ping address not given\n");
1208 #if defined(CONFIG_CMD_SNTP)
1210 if (NetNtpServerIP == 0) {
1211 puts("*** ERROR: NTP server address not given\n");
1216 #if defined(CONFIG_CMD_DNS)
1218 if (NetOurDNSIP == 0) {
1219 puts("*** ERROR: DNS server address not given\n");
1224 #if defined(CONFIG_CMD_NFS)
1229 if (NetServerIP == 0) {
1230 puts("*** ERROR: `serverip' not set\n");
1233 #if defined(CONFIG_CMD_PING) || defined(CONFIG_CMD_SNTP) || \
1234 defined(CONFIG_CMD_DNS)
1241 if (NetOurIP == 0) {
1242 puts("*** ERROR: `ipaddr' not set\n");
1247 #ifdef CONFIG_CMD_RARP
1254 if (memcmp(NetOurEther, "\0\0\0\0\0\0", 6) == 0) {
1255 int num = eth_get_dev_index();
1259 puts("*** ERROR: No ethernet found.\n");
1262 puts("*** ERROR: `ethaddr' not set\n");
1265 printf("*** ERROR: `eth%daddr' not set\n",
1279 /**********************************************************************/
1282 NetCksumOk(uchar *ptr, int len)
1284 return !((NetCksum(ptr, len) + 1) & 0xfffe);
1289 NetCksum(uchar *ptr, int len)
1292 ushort *p = (ushort *)ptr;
1297 xsum = (xsum & 0xffff) + (xsum >> 16);
1298 xsum = (xsum & 0xffff) + (xsum >> 16);
1299 return xsum & 0xffff;
1307 myvlanid = ntohs(NetOurVLAN);
1308 if (myvlanid == (ushort)-1)
1309 myvlanid = VLAN_NONE;
1311 return ((myvlanid & VLAN_IDMASK) == VLAN_NONE) ? ETHER_HDR_SIZE :
1312 VLAN_ETHER_HDR_SIZE;
1316 NetSetEther(uchar *xet, uchar * addr, uint prot)
1318 struct ethernet_hdr *et = (struct ethernet_hdr *)xet;
1321 myvlanid = ntohs(NetOurVLAN);
1322 if (myvlanid == (ushort)-1)
1323 myvlanid = VLAN_NONE;
1325 memcpy(et->et_dest, addr, 6);
1326 memcpy(et->et_src, NetOurEther, 6);
1327 if ((myvlanid & VLAN_IDMASK) == VLAN_NONE) {
1328 et->et_protlen = htons(prot);
1329 return ETHER_HDR_SIZE;
1331 struct vlan_ethernet_hdr *vet =
1332 (struct vlan_ethernet_hdr *)xet;
1334 vet->vet_vlan_type = htons(PROT_VLAN);
1335 vet->vet_tag = htons((0 << 5) | (myvlanid & VLAN_IDMASK));
1336 vet->vet_type = htons(prot);
1337 return VLAN_ETHER_HDR_SIZE;
1341 int net_update_ether(struct ethernet_hdr *et, uchar *addr, uint prot)
1345 memcpy(et->et_dest, addr, 6);
1346 memcpy(et->et_src, NetOurEther, 6);
1347 protlen = ntohs(et->et_protlen);
1348 if (protlen == PROT_VLAN) {
1349 struct vlan_ethernet_hdr *vet =
1350 (struct vlan_ethernet_hdr *)et;
1351 vet->vet_type = htons(prot);
1352 return VLAN_ETHER_HDR_SIZE;
1353 } else if (protlen > 1514) {
1354 et->et_protlen = htons(prot);
1355 return ETHER_HDR_SIZE;
1358 struct e802_hdr *et802 = (struct e802_hdr *)et;
1359 et802->et_prot = htons(prot);
1360 return E802_HDR_SIZE;
1364 void net_set_ip_header(uchar *pkt, IPaddr_t dest, IPaddr_t source)
1366 struct ip_udp_hdr *ip = (struct ip_udp_hdr *)pkt;
1369 * Construct an IP header.
1371 /* IP_HDR_SIZE / 4 (not including UDP) */
1374 ip->ip_len = htons(IP_HDR_SIZE);
1375 ip->ip_id = htons(NetIPID++);
1376 ip->ip_off = htons(IP_FLAGS_DFRAG); /* Don't fragment */
1379 /* already in network byte order */
1380 NetCopyIP((void *)&ip->ip_src, &source);
1381 /* already in network byte order */
1382 NetCopyIP((void *)&ip->ip_dst, &dest);
1385 void net_set_udp_header(uchar *pkt, IPaddr_t dest, int dport, int sport,
1388 struct ip_udp_hdr *ip = (struct ip_udp_hdr *)pkt;
1391 * If the data is an odd number of bytes, zero the
1392 * byte after the last byte so that the checksum
1396 pkt[IP_UDP_HDR_SIZE + len] = 0;
1398 net_set_ip_header(pkt, dest, NetOurIP);
1399 ip->ip_len = htons(IP_UDP_HDR_SIZE + len);
1400 ip->ip_p = IPPROTO_UDP;
1401 ip->ip_sum = ~NetCksum((uchar *)ip, IP_HDR_SIZE >> 1);
1403 ip->udp_src = htons(sport);
1404 ip->udp_dst = htons(dport);
1405 ip->udp_len = htons(UDP_HDR_SIZE + len);
1409 void copy_filename(char *dst, const char *src, int size)
1411 if (*src && (*src == '"')) {
1416 while ((--size > 0) && *src && (*src != '"'))
1421 #if defined(CONFIG_CMD_NFS) || \
1422 defined(CONFIG_CMD_SNTP) || \
1423 defined(CONFIG_CMD_DNS)
1425 * make port a little random (1024-17407)
1426 * This keeps the math somewhat trivial to compute, and seems to work with
1427 * all supported protocols/clients/servers
1429 unsigned int random_port(void)
1431 return 1024 + (get_timer(0) % 0x4000);
1435 void ip_to_string(IPaddr_t x, char *s)
1438 sprintf(s, "%d.%d.%d.%d",
1439 (int) ((x >> 24) & 0xff),
1440 (int) ((x >> 16) & 0xff),
1441 (int) ((x >> 8) & 0xff), (int) ((x >> 0) & 0xff)
1445 void VLAN_to_string(ushort x, char *s)
1449 if (x == (ushort)-1)
1455 sprintf(s, "%d", x & VLAN_IDMASK);
1458 ushort string_to_VLAN(const char *s)
1463 return htons(VLAN_NONE);
1465 if (*s < '0' || *s > '9')
1468 id = (ushort)simple_strtoul(s, NULL, 10);
1473 ushort getenv_VLAN(char *var)
1475 return string_to_VLAN(getenv(var));