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
30 * - TFTP server IP address
35 * Prerequisites: - own ethernet address
37 * - TFTP server IP address
38 * We want: - TFTP server ethernet address
43 * Prerequisites: - own ethernet address
44 * We want: - IP, Netmask, ServerIP, Gateway IP
45 * - bootfilename, lease time
50 * Prerequisites: - own ethernet address
52 * - TFTP server IP address
53 * - TFTP server ethernet address
54 * - name of bootfile (if unknown, we use a default name
55 * derived from our own IP address)
56 * We want: - load the boot file
61 * Prerequisites: - own ethernet address
63 * - name of bootfile (if unknown, we use a default name
64 * derived from our own IP address)
65 * We want: - load the boot file
70 * Prerequisites: - own ethernet address
72 * We want: - network time
80 #if defined(CONFIG_STATUS_LED)
82 #include <status_led.h>
85 #include <linux/compiler.h>
89 #if defined(CONFIG_CMD_DNS)
95 #if defined(CONFIG_CMD_SNTP)
100 DECLARE_GLOBAL_DATA_PTR;
102 /** BOOTP EXTENTIONS **/
104 /* Our subnet mask (0=unknown) */
105 IPaddr_t NetOurSubnetMask;
106 /* Our gateways IP address */
107 IPaddr_t NetOurGatewayIP;
108 /* Our DNS IP address */
109 IPaddr_t NetOurDNSIP;
110 #if defined(CONFIG_BOOTP_DNS2)
111 /* Our 2nd DNS IP address */
112 IPaddr_t NetOurDNS2IP;
115 char NetOurNISDomain[32] = {0,};
117 char NetOurHostName[32] = {0,};
119 char NetOurRootPath[64] = {0,};
120 /* Our bootfile size in blocks */
121 ushort NetBootFileSize;
123 #ifdef CONFIG_MCAST_TFTP /* Multicast TFTP */
127 /** END OF BOOTP EXTENTIONS **/
129 /* The actual transferred size of the bootfile (in bytes) */
130 ulong NetBootFileXferSize;
131 /* Our ethernet address */
132 uchar NetOurEther[6];
133 /* Boot server enet address */
134 uchar NetServerEther[6];
135 /* Our IP addr (0 = unknown) */
137 /* Server IP addr (0 = unknown) */
138 IPaddr_t NetServerIP;
139 /* Current receive packet */
141 /* Current rx packet length */
145 /* Ethernet bcast address */
146 uchar NetBcastAddr[6] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
147 uchar NetEtherNullAddr[6];
149 void (*push_packet)(void *, int len) = 0;
151 /* Network loop state */
152 enum net_loop_state net_state;
153 /* Tried all network devices */
155 /* Network loop restarted */
156 static int NetRestarted;
157 /* At least one device configured */
158 static int NetDevExists;
160 /* XXX in both little & big endian machines 0xFFFF == ntohs(-1) */
161 /* default is without VLAN */
162 ushort NetOurVLAN = 0xFFFF;
164 ushort NetOurNativeVLAN = 0xFFFF;
169 #if defined(CONFIG_CMD_SNTP)
170 /* NTP server IP address */
171 IPaddr_t NetNtpServerIP;
172 /* offset time from UTC */
176 uchar PktBuf[(PKTBUFSRX+1) * PKTSIZE_ALIGN + PKTALIGN];
179 uchar *NetRxPackets[PKTBUFSRX];
181 /* Current UDP RX packet handler */
182 static rxhand_f *udp_packet_handler;
183 /* Current ARP RX packet handler */
184 static rxhand_f *arp_packet_handler;
185 #ifdef CONFIG_CMD_TFTPPUT
186 /* Current ICMP rx handler */
187 static rxhand_icmp_f *packet_icmp_handler;
189 /* Current timeout handler */
190 static thand_f *timeHandler;
191 /* Time base value */
192 static ulong timeStart;
193 /* Current timeout value */
194 static ulong timeDelta;
195 /* THE transmit packet */
198 static int net_check_prereq(enum proto_t protocol);
200 static int NetTryCount;
202 /**********************************************************************/
205 * Check if autoload is enabled. If so, use either NFS or TFTP to download
208 void net_auto_load(void)
210 const char *s = getenv("autoload");
215 * Just use BOOTP/RARP to configure system;
216 * Do not use TFTP to load the bootfile.
218 net_set_state(NETLOOP_SUCCESS);
221 #if defined(CONFIG_CMD_NFS)
222 if (strcmp(s, "NFS") == 0) {
224 * Use NFS to load the bootfile.
234 static void NetInitLoop(void)
236 static int env_changed_id;
237 int env_id = get_env_id();
239 /* update only when the environment has changed */
240 if (env_changed_id != env_id) {
241 NetOurIP = getenv_IPaddr("ipaddr");
242 NetOurGatewayIP = getenv_IPaddr("gatewayip");
243 NetOurSubnetMask = getenv_IPaddr("netmask");
244 NetServerIP = getenv_IPaddr("serverip");
245 NetOurNativeVLAN = getenv_VLAN("nvlan");
246 NetOurVLAN = getenv_VLAN("vlan");
247 #if defined(CONFIG_CMD_DNS)
248 NetOurDNSIP = getenv_IPaddr("dnsip");
250 env_changed_id = env_id;
256 static void net_clear_handlers(void)
258 net_set_udp_handler(NULL);
259 net_set_arp_handler(NULL);
260 NetSetTimeout(0, NULL);
263 static void net_cleanup_loop(void)
265 net_clear_handlers();
270 static int first_call = 1;
274 * Setup packet buffers, aligned correctly.
278 NetTxPacket = &PktBuf[0] + (PKTALIGN - 1);
279 NetTxPacket -= (ulong)NetTxPacket % PKTALIGN;
280 for (i = 0; i < PKTBUFSRX; i++)
281 NetRxPackets[i] = NetTxPacket + (i + 1) * PKTSIZE_ALIGN;
284 net_clear_handlers();
286 /* Only need to setup buffer pointers once. */
293 /**********************************************************************/
295 * Main network processing loop.
298 int NetLoop(enum proto_t protocol)
307 bootstage_mark_name(BOOTSTAGE_ID_ETH_START, "eth_start");
311 if (eth_init(bd) < 0) {
317 memcpy(NetOurEther, eth_get_dev()->enetaddr, 6);
319 net_set_state(NETLOOP_CONTINUE);
322 * Start the ball rolling with the given start function. From
323 * here on, this code is a state machine driven by received
324 * packets and timer events.
328 switch (net_check_prereq(protocol)) {
330 /* network not configured */
335 /* network device not configured */
340 NetBootFileXferSize = 0;
343 #ifdef CONFIG_CMD_TFTPPUT
346 /* always use ARP to get server ethernet address */
349 #ifdef CONFIG_CMD_TFTPSRV
354 #if defined(CONFIG_CMD_DHCP)
358 DhcpRequest(); /* Basically same as BOOTP */
368 #if defined(CONFIG_CMD_RARP)
375 #if defined(CONFIG_CMD_PING)
380 #if defined(CONFIG_CMD_NFS)
385 #if defined(CONFIG_CMD_CDP)
390 #ifdef CONFIG_NETCONSOLE
395 #if defined(CONFIG_CMD_SNTP)
400 #if defined(CONFIG_CMD_DNS)
412 #if defined(CONFIG_MII) || defined(CONFIG_CMD_MII)
413 #if defined(CONFIG_SYS_FAULT_ECHO_LINK_DOWN) && \
414 defined(CONFIG_STATUS_LED) && \
415 defined(STATUS_LED_RED)
417 * Echo the inverted link state to the fault LED.
419 if (miiphy_link(eth_get_dev()->name, CONFIG_SYS_FAULT_MII_ADDR))
420 status_led_set(STATUS_LED_RED, STATUS_LED_OFF);
422 status_led_set(STATUS_LED_RED, STATUS_LED_ON);
423 #endif /* CONFIG_SYS_FAULT_ECHO_LINK_DOWN, ... */
424 #endif /* CONFIG_MII, ... */
427 * Main packet reception loop. Loop receiving packets until
428 * someone sets `net_state' to a state that terminates.
432 #ifdef CONFIG_SHOW_ACTIVITY
436 * Check the ethernet for a new packet. The ethernet
437 * receive routine will process it.
442 * Abort if ctrl-c was pressed.
445 /* cancel any ARP that may not have completed */
446 NetArpWaitPacketIP = 0;
457 * Check for a timeout, and run the timeout handler
460 if (timeHandler && ((get_timer(0) - timeStart) > timeDelta)) {
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,
471 CONFIG_SYS_FAULT_MII_ADDR)) {
472 status_led_set(STATUS_LED_RED, STATUS_LED_OFF);
474 status_led_set(STATUS_LED_RED, STATUS_LED_ON);
476 #endif /* CONFIG_SYS_FAULT_ECHO_LINK_DOWN, ... */
477 #endif /* CONFIG_MII, ... */
479 timeHandler = (thand_f *)0;
486 case NETLOOP_RESTART:
490 case NETLOOP_SUCCESS:
492 if (NetBootFileXferSize > 0) {
494 printf("Bytes transferred = %ld (%lx hex)\n",
496 NetBootFileXferSize);
497 sprintf(buf, "%lX", NetBootFileXferSize);
498 setenv("filesize", buf);
500 sprintf(buf, "%lX", (unsigned long)load_addr);
501 setenv("fileaddr", buf);
504 ret = NetBootFileXferSize;
511 case NETLOOP_CONTINUE:
517 #ifdef CONFIG_CMD_TFTPPUT
518 /* Clear out the handlers */
519 net_set_udp_handler(NULL);
520 net_set_icmp_handler(NULL);
525 /**********************************************************************/
528 startAgainTimeout(void)
530 net_set_state(NETLOOP_RESTART);
533 void NetStartAgain(void)
536 int retry_forever = 0;
537 unsigned long retrycnt = 0;
539 nretry = getenv("netretry");
541 if (!strcmp(nretry, "yes"))
543 else if (!strcmp(nretry, "no"))
545 else if (!strcmp(nretry, "once"))
548 retrycnt = simple_strtoul(nretry, NULL, 0);
552 if ((!retry_forever) && (NetTryCount >= retrycnt)) {
554 net_set_state(NETLOOP_FAIL);
561 #if !defined(CONFIG_NET_DO_NOT_TRY_ANOTHER)
562 eth_try_another(!NetRestarted);
565 if (NetRestartWrap) {
568 NetSetTimeout(10000UL, startAgainTimeout);
569 net_set_udp_handler(NULL);
571 net_set_state(NETLOOP_FAIL);
574 net_set_state(NETLOOP_RESTART);
578 /**********************************************************************/
583 static void dummy_handler(uchar *pkt, unsigned dport,
584 IPaddr_t sip, unsigned sport,
589 rxhand_f *net_get_udp_handler(void)
591 return udp_packet_handler;
594 void net_set_udp_handler(rxhand_f *f)
597 udp_packet_handler = dummy_handler;
599 udp_packet_handler = f;
602 rxhand_f *net_get_arp_handler(void)
604 return arp_packet_handler;
607 void net_set_arp_handler(rxhand_f *f)
610 arp_packet_handler = dummy_handler;
612 arp_packet_handler = f;
615 #ifdef CONFIG_CMD_TFTPPUT
616 void net_set_icmp_handler(rxhand_icmp_f *f)
618 packet_icmp_handler = f;
623 NetSetTimeout(ulong iv, thand_f *f)
626 timeHandler = (thand_f *)0;
629 timeStart = get_timer(0);
634 int NetSendUDPPacket(uchar *ether, IPaddr_t dest, int dport, int sport,
641 /* make sure the NetTxPacket is initialized (NetInit() was called) */
642 assert(NetTxPacket != NULL);
643 if (NetTxPacket == NULL)
646 /* convert to new style broadcast */
650 /* if broadcast, make the ether address a broadcast and don't do ARP */
651 if (dest == 0xFFFFFFFF)
652 ether = NetBcastAddr;
654 pkt = (uchar *)NetTxPacket;
656 eth_hdr_size = NetSetEther(pkt, ether, PROT_IP);
658 net_set_udp_header(pkt, dest, dport, sport, payload_len);
659 pkt_hdr_size = eth_hdr_size + IP_UDP_HDR_SIZE;
661 /* if MAC address was not discovered yet, do an ARP request */
662 if (memcmp(ether, NetEtherNullAddr, 6) == 0) {
663 debug("sending ARP for %pI4\n", &dest);
665 /* save the ip and eth addr for the packet to send after arp */
666 NetArpWaitPacketIP = dest;
667 NetArpWaitPacketMAC = ether;
669 /* size of the waiting packet */
670 NetArpWaitTxPacketSize = pkt_hdr_size + payload_len;
672 /* and do the ARP request */
674 NetArpWaitTimerStart = get_timer(0);
676 return 1; /* waiting */
678 debug("sending UDP to %pI4/%pM\n", &dest, ether);
679 NetSendPacket(NetTxPacket, pkt_hdr_size + payload_len);
680 return 0; /* transmitted */
684 #ifdef CONFIG_IP_DEFRAG
686 * This function collects fragments in a single packet, according
687 * to the algorithm in RFC815. It returns NULL or the pointer to
688 * a complete packet, in static storage
690 #ifndef CONFIG_NET_MAXDEFRAG
691 #define CONFIG_NET_MAXDEFRAG 16384
694 * MAXDEFRAG, above, is chosen in the config file and is real data
695 * so we need to add the NFS overhead, which is more than TFTP.
696 * To use sizeof in the internal unnamed structures, we need a real
697 * instance (can't do "sizeof(struct rpc_t.u.reply))", unfortunately).
698 * The compiler doesn't complain nor allocates the actual structure
700 static struct rpc_t rpc_specimen;
701 #define IP_PKTSIZE (CONFIG_NET_MAXDEFRAG + sizeof(rpc_specimen.u.reply))
703 #define IP_MAXUDP (IP_PKTSIZE - IP_HDR_SIZE)
706 * this is the packet being assembled, either data or frag control.
707 * Fragments go by 8 bytes, so this union must be 8 bytes long
710 /* first_byte is address of this structure */
711 u16 last_byte; /* last byte in this hole + 1 (begin of next hole) */
712 u16 next_hole; /* index of next (in 8-b blocks), 0 == none */
713 u16 prev_hole; /* index of prev, 0 == none */
717 static struct ip_udp_hdr *__NetDefragment(struct ip_udp_hdr *ip, int *lenp)
719 static uchar pkt_buff[IP_PKTSIZE] __aligned(PKTALIGN);
720 static u16 first_hole, total_len;
721 struct hole *payload, *thisfrag, *h, *newh;
722 struct ip_udp_hdr *localip = (struct ip_udp_hdr *)pkt_buff;
723 uchar *indata = (uchar *)ip;
724 int offset8, start, len, done = 0;
725 u16 ip_off = ntohs(ip->ip_off);
727 /* payload starts after IP header, this fragment is in there */
728 payload = (struct hole *)(pkt_buff + IP_HDR_SIZE);
729 offset8 = (ip_off & IP_OFFS);
730 thisfrag = payload + offset8;
732 len = ntohs(ip->ip_len) - IP_HDR_SIZE;
734 if (start + len > IP_MAXUDP) /* fragment extends too far */
737 if (!total_len || localip->ip_id != ip->ip_id) {
738 /* new (or different) packet, reset structs */
740 payload[0].last_byte = ~0;
741 payload[0].next_hole = 0;
742 payload[0].prev_hole = 0;
744 /* any IP header will work, copy the first we received */
745 memcpy(localip, ip, IP_HDR_SIZE);
749 * What follows is the reassembly algorithm. We use the payload
750 * array as a linked list of hole descriptors, as each hole starts
751 * at a multiple of 8 bytes. However, last byte can be whatever value,
752 * so it is represented as byte count, not as 8-byte blocks.
755 h = payload + first_hole;
756 while (h->last_byte < start) {
758 /* no hole that far away */
761 h = payload + h->next_hole;
764 /* last fragment may be 1..7 bytes, the "+7" forces acceptance */
765 if (offset8 + ((len + 7) / 8) <= h - payload) {
766 /* no overlap with holes (dup fragment?) */
770 if (!(ip_off & IP_FLAGS_MFRAG)) {
771 /* no more fragmentss: truncate this (last) hole */
772 total_len = start + len;
773 h->last_byte = start + len;
777 * There is some overlap: fix the hole list. This code doesn't
778 * deal with a fragment that overlaps with two different holes
779 * (thus being a superset of a previously-received fragment).
782 if ((h >= thisfrag) && (h->last_byte <= start + len)) {
783 /* complete overlap with hole: remove hole */
784 if (!h->prev_hole && !h->next_hole) {
785 /* last remaining hole */
787 } else if (!h->prev_hole) {
789 first_hole = h->next_hole;
790 payload[h->next_hole].prev_hole = 0;
791 } else if (!h->next_hole) {
793 payload[h->prev_hole].next_hole = 0;
795 /* in the middle of the list */
796 payload[h->next_hole].prev_hole = h->prev_hole;
797 payload[h->prev_hole].next_hole = h->next_hole;
800 } else if (h->last_byte <= start + len) {
801 /* overlaps with final part of the hole: shorten this hole */
802 h->last_byte = start;
804 } else if (h >= thisfrag) {
805 /* overlaps with initial part of the hole: move this hole */
806 newh = thisfrag + (len / 8);
810 payload[h->next_hole].prev_hole = (h - payload);
812 payload[h->prev_hole].next_hole = (h - payload);
814 first_hole = (h - payload);
817 /* fragment sits in the middle: split the hole */
818 newh = thisfrag + (len / 8);
820 h->last_byte = start;
821 h->next_hole = (newh - payload);
822 newh->prev_hole = (h - payload);
824 payload[newh->next_hole].prev_hole = (newh - payload);
827 /* finally copy this fragment and possibly return whole packet */
828 memcpy((uchar *)thisfrag, indata + IP_HDR_SIZE, len);
832 localip->ip_len = htons(total_len);
833 *lenp = total_len + IP_HDR_SIZE;
837 static inline struct ip_udp_hdr *NetDefragment(struct ip_udp_hdr *ip, int *lenp)
839 u16 ip_off = ntohs(ip->ip_off);
840 if (!(ip_off & (IP_OFFS | IP_FLAGS_MFRAG)))
841 return ip; /* not a fragment */
842 return __NetDefragment(ip, lenp);
845 #else /* !CONFIG_IP_DEFRAG */
847 static inline struct ip_udp_hdr *NetDefragment(struct ip_udp_hdr *ip, int *lenp)
849 u16 ip_off = ntohs(ip->ip_off);
850 if (!(ip_off & (IP_OFFS | IP_FLAGS_MFRAG)))
851 return ip; /* not a fragment */
857 * Receive an ICMP packet. We deal with REDIRECT and PING here, and silently
860 * @parma ip IP packet containing the ICMP
862 static void receive_icmp(struct ip_udp_hdr *ip, int len,
863 IPaddr_t src_ip, struct ethernet_hdr *et)
865 struct icmp_hdr *icmph = (struct icmp_hdr *)&ip->udp_src;
867 switch (icmph->type) {
869 if (icmph->code != ICMP_REDIR_HOST)
871 printf(" ICMP Host Redirect to %pI4 ",
875 #if defined(CONFIG_CMD_PING)
876 ping_receive(et, ip, len);
878 #ifdef CONFIG_CMD_TFTPPUT
879 if (packet_icmp_handler)
880 packet_icmp_handler(icmph->type, icmph->code,
881 ntohs(ip->udp_dst), src_ip, ntohs(ip->udp_src),
882 icmph->un.data, ntohs(ip->udp_len));
889 NetReceive(uchar *inpkt, int len)
891 struct ethernet_hdr *et;
892 struct ip_udp_hdr *ip;
896 #if defined(CONFIG_CMD_CDP)
899 ushort cti = 0, vlanid = VLAN_NONE, myvlanid, mynvlanid;
901 debug("packet received\n");
904 NetRxPacketLen = len;
905 et = (struct ethernet_hdr *)inpkt;
907 /* too small packet? */
908 if (len < ETHER_HDR_SIZE)
913 (*push_packet)(inpkt, len);
918 #if defined(CONFIG_CMD_CDP)
919 /* keep track if packet is CDP */
920 iscdp = is_cdp_packet(et->et_dest);
923 myvlanid = ntohs(NetOurVLAN);
924 if (myvlanid == (ushort)-1)
925 myvlanid = VLAN_NONE;
926 mynvlanid = ntohs(NetOurNativeVLAN);
927 if (mynvlanid == (ushort)-1)
928 mynvlanid = VLAN_NONE;
930 eth_proto = ntohs(et->et_protlen);
932 debug("packet received\n");
934 if (eth_proto < 1514) {
935 struct e802_hdr *et802 = (struct e802_hdr *)et;
937 * Got a 802.2 packet. Check the other protocol field.
938 * XXX VLAN over 802.2+SNAP not implemented!
940 eth_proto = ntohs(et802->et_prot);
942 ip = (struct ip_udp_hdr *)(inpkt + E802_HDR_SIZE);
943 len -= E802_HDR_SIZE;
945 } else if (eth_proto != PROT_VLAN) { /* normal packet */
946 ip = (struct ip_udp_hdr *)(inpkt + ETHER_HDR_SIZE);
947 len -= ETHER_HDR_SIZE;
949 } else { /* VLAN packet */
950 struct vlan_ethernet_hdr *vet =
951 (struct vlan_ethernet_hdr *)et;
953 debug("VLAN packet received\n");
955 /* too small packet? */
956 if (len < VLAN_ETHER_HDR_SIZE)
959 /* if no VLAN active */
960 if ((ntohs(NetOurVLAN) & VLAN_IDMASK) == VLAN_NONE
961 #if defined(CONFIG_CMD_CDP)
967 cti = ntohs(vet->vet_tag);
968 vlanid = cti & VLAN_IDMASK;
969 eth_proto = ntohs(vet->vet_type);
971 ip = (struct ip_udp_hdr *)(inpkt + VLAN_ETHER_HDR_SIZE);
972 len -= VLAN_ETHER_HDR_SIZE;
975 debug("Receive from protocol 0x%x\n", eth_proto);
977 #if defined(CONFIG_CMD_CDP)
979 cdp_receive((uchar *)ip, len);
984 if ((myvlanid & VLAN_IDMASK) != VLAN_NONE) {
985 if (vlanid == VLAN_NONE)
986 vlanid = (mynvlanid & VLAN_IDMASK);
988 if (vlanid != (myvlanid & VLAN_IDMASK))
995 ArpReceive(et, ip, len);
998 #ifdef CONFIG_CMD_RARP
1000 rarp_receive(ip, len);
1005 /* Before we start poking the header, make sure it is there */
1006 if (len < IP_UDP_HDR_SIZE) {
1007 debug("len bad %d < %lu\n", len,
1008 (ulong)IP_UDP_HDR_SIZE);
1011 /* Check the packet length */
1012 if (len < ntohs(ip->ip_len)) {
1013 printf("len bad %d < %d\n", len, ntohs(ip->ip_len));
1016 len = ntohs(ip->ip_len);
1017 debug("len=%d, v=%02x\n", len, ip->ip_hl_v & 0xff);
1019 /* Can't deal with anything except IPv4 */
1020 if ((ip->ip_hl_v & 0xf0) != 0x40)
1022 /* Can't deal with IP options (headers != 20 bytes) */
1023 if ((ip->ip_hl_v & 0x0f) > 0x05)
1025 /* Check the Checksum of the header */
1026 if (!NetCksumOk((uchar *)ip, IP_HDR_SIZE / 2)) {
1027 puts("checksum bad\n");
1030 /* If it is not for us, ignore it */
1031 dst_ip = NetReadIP(&ip->ip_dst);
1032 if (NetOurIP && dst_ip != NetOurIP && dst_ip != 0xFFFFFFFF) {
1033 #ifdef CONFIG_MCAST_TFTP
1034 if (Mcast_addr != dst_ip)
1038 /* Read source IP address for later use */
1039 src_ip = NetReadIP(&ip->ip_src);
1041 * The function returns the unchanged packet if it's not
1042 * a fragment, and either the complete packet or NULL if
1043 * it is a fragment (if !CONFIG_IP_DEFRAG, it returns NULL)
1045 ip = NetDefragment(ip, &len);
1049 * watch for ICMP host redirects
1051 * There is no real handler code (yet). We just watch
1052 * for ICMP host redirect messages. In case anybody
1053 * sees these messages: please contact me
1054 * (wd@denx.de), or - even better - send me the
1055 * necessary fixes :-)
1057 * Note: in all cases where I have seen this so far
1058 * it was a problem with the router configuration,
1059 * for instance when a router was configured in the
1060 * BOOTP reply, but the TFTP server was on the same
1061 * subnet. So this is probably a warning that your
1062 * configuration might be wrong. But I'm not really
1063 * sure if there aren't any other situations.
1065 * Simon Glass <sjg@chromium.org>: We get an ICMP when
1066 * we send a tftp packet to a dead connection, or when
1067 * there is no server at the other end.
1069 if (ip->ip_p == IPPROTO_ICMP) {
1070 receive_icmp(ip, len, src_ip, et);
1072 } else if (ip->ip_p != IPPROTO_UDP) { /* Only UDP packets */
1076 #ifdef CONFIG_UDP_CHECKSUM
1077 if (ip->udp_xsum != 0) {
1083 xsum += (ntohs(ip->udp_len));
1084 xsum += (ntohl(ip->ip_src) >> 16) & 0x0000ffff;
1085 xsum += (ntohl(ip->ip_src) >> 0) & 0x0000ffff;
1086 xsum += (ntohl(ip->ip_dst) >> 16) & 0x0000ffff;
1087 xsum += (ntohl(ip->ip_dst) >> 0) & 0x0000ffff;
1089 sumlen = ntohs(ip->udp_len);
1090 sumptr = (ushort *) &(ip->udp_src);
1092 while (sumlen > 1) {
1095 sumdata = *sumptr++;
1096 xsum += ntohs(sumdata);
1102 sumdata = *(unsigned char *) sumptr;
1103 sumdata = (sumdata << 8) & 0xff00;
1106 while ((xsum >> 16) != 0) {
1107 xsum = (xsum & 0x0000ffff) +
1108 ((xsum >> 16) & 0x0000ffff);
1110 if ((xsum != 0x00000000) && (xsum != 0x0000ffff)) {
1111 printf(" UDP wrong checksum %08lx %08x\n",
1112 xsum, ntohs(ip->udp_xsum));
1119 #ifdef CONFIG_NETCONSOLE
1120 nc_input_packet((uchar *)ip + IP_UDP_HDR_SIZE,
1123 ntohs(ip->udp_len) - UDP_HDR_SIZE);
1126 * IP header OK. Pass the packet to the current handler.
1128 (*udp_packet_handler)((uchar *)ip + IP_UDP_HDR_SIZE,
1132 ntohs(ip->udp_len) - UDP_HDR_SIZE);
1138 /**********************************************************************/
1140 static int net_check_prereq(enum proto_t protocol)
1144 #if defined(CONFIG_CMD_PING)
1146 if (NetPingIP == 0) {
1147 puts("*** ERROR: ping address not given\n");
1152 #if defined(CONFIG_CMD_SNTP)
1154 if (NetNtpServerIP == 0) {
1155 puts("*** ERROR: NTP server address not given\n");
1160 #if defined(CONFIG_CMD_DNS)
1162 if (NetOurDNSIP == 0) {
1163 puts("*** ERROR: DNS server address not given\n");
1168 #if defined(CONFIG_CMD_NFS)
1173 if (NetServerIP == 0) {
1174 puts("*** ERROR: `serverip' not set\n");
1177 #if defined(CONFIG_CMD_PING) || defined(CONFIG_CMD_SNTP) || \
1178 defined(CONFIG_CMD_DNS)
1185 if (NetOurIP == 0) {
1186 puts("*** ERROR: `ipaddr' not set\n");
1191 #ifdef CONFIG_CMD_RARP
1197 if (memcmp(NetOurEther, "\0\0\0\0\0\0", 6) == 0) {
1198 int num = eth_get_dev_index();
1202 puts("*** ERROR: No ethernet found.\n");
1205 puts("*** ERROR: `ethaddr' not set\n");
1208 printf("*** ERROR: `eth%daddr' not set\n",
1222 /**********************************************************************/
1225 NetCksumOk(uchar *ptr, int len)
1227 return !((NetCksum(ptr, len) + 1) & 0xfffe);
1232 NetCksum(uchar *ptr, int len)
1235 ushort *p = (ushort *)ptr;
1240 xsum = (xsum & 0xffff) + (xsum >> 16);
1241 xsum = (xsum & 0xffff) + (xsum >> 16);
1242 return xsum & 0xffff;
1250 myvlanid = ntohs(NetOurVLAN);
1251 if (myvlanid == (ushort)-1)
1252 myvlanid = VLAN_NONE;
1254 return ((myvlanid & VLAN_IDMASK) == VLAN_NONE) ? ETHER_HDR_SIZE :
1255 VLAN_ETHER_HDR_SIZE;
1259 NetSetEther(uchar *xet, uchar * addr, uint prot)
1261 struct ethernet_hdr *et = (struct ethernet_hdr *)xet;
1264 myvlanid = ntohs(NetOurVLAN);
1265 if (myvlanid == (ushort)-1)
1266 myvlanid = VLAN_NONE;
1268 memcpy(et->et_dest, addr, 6);
1269 memcpy(et->et_src, NetOurEther, 6);
1270 if ((myvlanid & VLAN_IDMASK) == VLAN_NONE) {
1271 et->et_protlen = htons(prot);
1272 return ETHER_HDR_SIZE;
1274 struct vlan_ethernet_hdr *vet =
1275 (struct vlan_ethernet_hdr *)xet;
1277 vet->vet_vlan_type = htons(PROT_VLAN);
1278 vet->vet_tag = htons((0 << 5) | (myvlanid & VLAN_IDMASK));
1279 vet->vet_type = htons(prot);
1280 return VLAN_ETHER_HDR_SIZE;
1284 int net_update_ether(struct ethernet_hdr *et, uchar *addr, uint prot)
1288 memcpy(et->et_dest, addr, 6);
1289 memcpy(et->et_src, NetOurEther, 6);
1290 protlen = ntohs(et->et_protlen);
1291 if (protlen == PROT_VLAN) {
1292 struct vlan_ethernet_hdr *vet =
1293 (struct vlan_ethernet_hdr *)et;
1294 vet->vet_type = htons(prot);
1295 return VLAN_ETHER_HDR_SIZE;
1296 } else if (protlen > 1514) {
1297 et->et_protlen = htons(prot);
1298 return ETHER_HDR_SIZE;
1301 struct e802_hdr *et802 = (struct e802_hdr *)et;
1302 et802->et_prot = htons(prot);
1303 return E802_HDR_SIZE;
1307 void net_set_ip_header(uchar *pkt, IPaddr_t dest, IPaddr_t source)
1309 struct ip_udp_hdr *ip = (struct ip_udp_hdr *)pkt;
1312 * Construct an IP header.
1314 /* IP_HDR_SIZE / 4 (not including UDP) */
1317 ip->ip_len = htons(IP_HDR_SIZE);
1318 ip->ip_id = htons(NetIPID++);
1319 ip->ip_off = htons(IP_FLAGS_DFRAG); /* Don't fragment */
1322 /* already in network byte order */
1323 NetCopyIP((void *)&ip->ip_src, &source);
1324 /* already in network byte order */
1325 NetCopyIP((void *)&ip->ip_dst, &dest);
1328 void net_set_udp_header(uchar *pkt, IPaddr_t dest, int dport, int sport,
1331 struct ip_udp_hdr *ip = (struct ip_udp_hdr *)pkt;
1334 * If the data is an odd number of bytes, zero the
1335 * byte after the last byte so that the checksum
1339 pkt[IP_UDP_HDR_SIZE + len] = 0;
1341 net_set_ip_header(pkt, dest, NetOurIP);
1342 ip->ip_len = htons(IP_UDP_HDR_SIZE + len);
1343 ip->ip_p = IPPROTO_UDP;
1344 ip->ip_sum = ~NetCksum((uchar *)ip, IP_HDR_SIZE >> 1);
1346 ip->udp_src = htons(sport);
1347 ip->udp_dst = htons(dport);
1348 ip->udp_len = htons(UDP_HDR_SIZE + len);
1352 void copy_filename(char *dst, const char *src, int size)
1354 if (*src && (*src == '"')) {
1359 while ((--size > 0) && *src && (*src != '"'))
1364 #if defined(CONFIG_CMD_NFS) || \
1365 defined(CONFIG_CMD_SNTP) || \
1366 defined(CONFIG_CMD_DNS)
1368 * make port a little random (1024-17407)
1369 * This keeps the math somewhat trivial to compute, and seems to work with
1370 * all supported protocols/clients/servers
1372 unsigned int random_port(void)
1374 return 1024 + (get_timer(0) % 0x4000);
1378 void ip_to_string(IPaddr_t x, char *s)
1381 sprintf(s, "%d.%d.%d.%d",
1382 (int) ((x >> 24) & 0xff),
1383 (int) ((x >> 16) & 0xff),
1384 (int) ((x >> 8) & 0xff), (int) ((x >> 0) & 0xff)
1388 void VLAN_to_string(ushort x, char *s)
1392 if (x == (ushort)-1)
1398 sprintf(s, "%d", x & VLAN_IDMASK);
1401 ushort string_to_VLAN(const char *s)
1406 return htons(VLAN_NONE);
1408 if (*s < '0' || *s > '9')
1411 id = (ushort)simple_strtoul(s, NULL, 10);
1416 ushort getenv_VLAN(char *var)
1418 return string_to_VLAN(getenv(var));