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
83 #ifdef CONFIG_CMD_RARP
87 #ifdef CONFIG_STATUS_LED
88 #include <status_led.h>
91 #if defined(CONFIG_CMD_SNTP)
94 #if defined(CONFIG_CDP_VERSION)
95 #include <timestamp.h>
97 #if defined(CONFIG_CMD_DNS)
101 DECLARE_GLOBAL_DATA_PTR;
103 #ifndef CONFIG_ARP_TIMEOUT
104 /* Milliseconds before trying ARP again */
105 # define ARP_TIMEOUT 5000UL
107 # define ARP_TIMEOUT CONFIG_ARP_TIMEOUT
111 #ifndef CONFIG_NET_RETRY_COUNT
112 # define ARP_TIMEOUT_COUNT 5 /* # of timeouts before giving up */
114 # define ARP_TIMEOUT_COUNT CONFIG_NET_RETRY_COUNT
117 /** BOOTP EXTENTIONS **/
119 /* Our subnet mask (0=unknown) */
120 IPaddr_t NetOurSubnetMask=0;
121 /* Our gateways IP address */
122 IPaddr_t NetOurGatewayIP=0;
123 /* Our DNS IP address */
124 IPaddr_t NetOurDNSIP=0;
125 #if defined(CONFIG_BOOTP_DNS2)
126 /* Our 2nd DNS IP address */
127 IPaddr_t NetOurDNS2IP=0;
130 char NetOurNISDomain[32]={0,};
132 char NetOurHostName[32]={0,};
134 char NetOurRootPath[64]={0,};
135 /* Our bootfile size in blocks */
136 ushort NetBootFileSize=0;
138 #ifdef CONFIG_MCAST_TFTP /* Multicast TFTP */
142 /** END OF BOOTP EXTENTIONS **/
144 /* The actual transferred size of the bootfile (in bytes) */
145 ulong NetBootFileXferSize;
146 /* Our ethernet address */
147 uchar NetOurEther[6];
148 /* Boot server enet address */
149 uchar NetServerEther[6] =
150 { 0, 0, 0, 0, 0, 0 };
151 /* Our IP addr (0 = unknown) */
153 /* Server IP addr (0 = unknown) */
154 IPaddr_t NetServerIP;
155 /* Current receive packet */
156 volatile uchar *NetRxPacket;
157 /* Current rx packet length */
161 /* Ethernet bcast address */
162 uchar NetBcastAddr[6] =
163 { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
164 uchar NetEtherNullAddr[6] =
165 { 0, 0, 0, 0, 0, 0 };
167 void (*push_packet)(volatile void *, int len) = 0;
169 #if defined(CONFIG_CMD_CDP)
170 /* Ethernet bcast address */
171 uchar NetCDPAddr[6] =
172 { 0x01, 0x00, 0x0c, 0xcc, 0xcc, 0xcc };
174 /* Network loop state */
176 #ifdef CONFIG_NET_MULTI
177 /* Tried all network devices */
178 int NetRestartWrap = 0;
179 /* Network loop restarted */
180 static int NetRestarted = 0;
181 /* At least one device configured */
182 static int NetDevExists = 0;
185 /* XXX in both little & big endian machines 0xFFFF == ntohs(-1) */
186 /* default is without VLAN */
187 ushort NetOurVLAN = 0xFFFF;
189 ushort NetOurNativeVLAN = 0xFFFF;
194 #if defined(CONFIG_CMD_PING)
195 /* the ip address to ping */
198 static void PingStart(void);
201 #if defined(CONFIG_CMD_CDP)
202 static void CDPStart(void);
205 #if defined(CONFIG_CMD_SNTP)
206 /* NTP server IP address */
207 IPaddr_t NetNtpServerIP;
208 /* offset time from UTC */
212 #ifdef CONFIG_NETCONSOLE
214 int nc_input_packet(uchar *pkt, unsigned dest, unsigned src, unsigned len);
217 volatile uchar PktBuf[(PKTBUFSRX+1) * PKTSIZE_ALIGN + PKTALIGN];
220 volatile uchar *NetRxPackets[PKTBUFSRX];
222 /* Current RX packet handler */
223 static rxhand_f *packetHandler;
224 /* Current timeout handler */
225 static thand_f *timeHandler;
226 /* Time base value */
227 static ulong timeStart;
228 /* Current timeout value */
229 static ulong timeDelta;
230 /* THE transmit packet */
231 volatile uchar *NetTxPacket = 0;
233 static int net_check_prereq (proto_t protocol);
235 static int NetTryCount;
237 /**********************************************************************/
239 IPaddr_t NetArpWaitPacketIP;
240 IPaddr_t NetArpWaitReplyIP;
241 /* MAC address of waiting packet's destination */
242 uchar *NetArpWaitPacketMAC;
243 /* THE transmit packet */
244 uchar *NetArpWaitTxPacket;
245 int NetArpWaitTxPacketSize;
246 uchar NetArpWaitPacketBuf[PKTSIZE_ALIGN + PKTALIGN];
247 ulong NetArpWaitTimerStart;
250 void ArpRequest (void)
256 debug("ARP broadcast %d\n", NetArpWaitTry);
260 pkt += NetSetEther (pkt, NetBcastAddr, PROT_ARP);
264 arp->ar_hrd = htons (ARP_ETHER);
265 arp->ar_pro = htons (PROT_IP);
268 arp->ar_op = htons (ARPOP_REQUEST);
271 memcpy (&arp->ar_data[0], NetOurEther, 6);
273 NetWriteIP ((uchar *) & arp->ar_data[6], NetOurIP);
274 for (i = 10; i < 16; ++i) {
275 /* dest ET addr = 0 */
279 if ((NetArpWaitPacketIP & NetOurSubnetMask) !=
280 (NetOurIP & NetOurSubnetMask)) {
281 if (NetOurGatewayIP == 0) {
282 puts ("## Warning: gatewayip needed but not set\n");
283 NetArpWaitReplyIP = NetArpWaitPacketIP;
285 NetArpWaitReplyIP = NetOurGatewayIP;
288 NetArpWaitReplyIP = NetArpWaitPacketIP;
291 NetWriteIP ((uchar *) & arp->ar_data[16], NetArpWaitReplyIP);
292 (void) eth_send (NetTxPacket, (pkt - NetTxPacket) + ARP_HDR_SIZE);
295 void ArpTimeoutCheck(void)
299 if (!NetArpWaitPacketIP)
304 /* check for arp timeout */
305 if ((t - NetArpWaitTimerStart) > ARP_TIMEOUT) {
308 if (NetArpWaitTry >= ARP_TIMEOUT_COUNT) {
309 puts ("\nARP Retry count exceeded; starting again\n");
313 NetArpWaitTimerStart = t;
320 NetInitLoop(proto_t protocol)
322 static int env_changed_id = 0;
324 int env_id = get_env_id ();
326 /* update only when the environment has changed */
327 if (env_changed_id != env_id) {
328 NetCopyIP(&NetOurIP, &bd->bi_ip_addr);
329 NetOurGatewayIP = getenv_IPaddr ("gatewayip");
330 NetOurSubnetMask= getenv_IPaddr ("netmask");
331 NetServerIP = getenv_IPaddr ("serverip");
332 NetOurNativeVLAN = getenv_VLAN("nvlan");
333 NetOurVLAN = getenv_VLAN("vlan");
334 #if defined(CONFIG_CMD_DNS)
335 NetOurDNSIP = getenv_IPaddr("dnsip");
337 env_changed_id = env_id;
343 /**********************************************************************/
345 * Main network processing loop.
349 NetLoop(proto_t protocol)
353 #ifdef CONFIG_NET_MULTI
358 /* XXX problem with bss workaround */
359 NetArpWaitPacketMAC = NULL;
360 NetArpWaitTxPacket = NULL;
361 NetArpWaitPacketIP = 0;
362 NetArpWaitReplyIP = 0;
363 NetArpWaitTxPacket = NULL;
370 * Setup packet buffers, aligned correctly.
372 NetTxPacket = &PktBuf[0] + (PKTALIGN - 1);
373 NetTxPacket -= (ulong)NetTxPacket % PKTALIGN;
374 for (i = 0; i < PKTBUFSRX; i++) {
375 NetRxPackets[i] = NetTxPacket + (i+1)*PKTSIZE_ALIGN;
379 if (!NetArpWaitTxPacket) {
380 NetArpWaitTxPacket = &NetArpWaitPacketBuf[0] + (PKTALIGN - 1);
381 NetArpWaitTxPacket -= (ulong)NetArpWaitTxPacket % PKTALIGN;
382 NetArpWaitTxPacketSize = 0;
386 #ifdef CONFIG_NET_MULTI
389 if (eth_init(bd) < 0) {
395 #ifdef CONFIG_NET_MULTI
396 memcpy (NetOurEther, eth_get_dev()->enetaddr, 6);
398 eth_getenv_enetaddr("ethaddr", NetOurEther);
401 NetState = NETLOOP_CONTINUE;
404 * Start the ball rolling with the given start function. From
405 * here on, this code is a state machine driven by received
406 * packets and timer events.
408 NetInitLoop(protocol);
410 switch (net_check_prereq (protocol)) {
412 /* network not configured */
416 #ifdef CONFIG_NET_MULTI
418 /* network device not configured */
420 #endif /* CONFIG_NET_MULTI */
423 #ifdef CONFIG_NET_MULTI
428 /* always use ARP to get server ethernet address */
432 #if defined(CONFIG_CMD_DHCP)
436 DhcpRequest(); /* Basically same as BOOTP */
446 #if defined(CONFIG_CMD_RARP)
453 #if defined(CONFIG_CMD_PING)
458 #if defined(CONFIG_CMD_NFS)
463 #if defined(CONFIG_CMD_CDP)
468 #ifdef CONFIG_NETCONSOLE
473 #if defined(CONFIG_CMD_SNTP)
478 #if defined(CONFIG_CMD_DNS)
487 NetBootFileXferSize = 0;
491 #if defined(CONFIG_MII) || defined(CONFIG_CMD_MII)
492 #if defined(CONFIG_SYS_FAULT_ECHO_LINK_DOWN) && \
493 defined(CONFIG_STATUS_LED) && \
494 defined(STATUS_LED_RED)
496 * Echo the inverted link state to the fault LED.
498 if(miiphy_link(eth_get_dev()->name, CONFIG_SYS_FAULT_MII_ADDR)) {
499 status_led_set (STATUS_LED_RED, STATUS_LED_OFF);
501 status_led_set (STATUS_LED_RED, STATUS_LED_ON);
503 #endif /* CONFIG_SYS_FAULT_ECHO_LINK_DOWN, ... */
504 #endif /* CONFIG_MII, ... */
507 * Main packet reception loop. Loop receiving packets until
508 * someone sets `NetState' to a state that terminates.
512 #ifdef CONFIG_SHOW_ACTIVITY
514 extern void show_activity(int arg);
519 * Check the ethernet for a new packet. The ethernet
520 * receive routine will process it.
525 * Abort if ctrl-c was pressed.
536 * Check for a timeout, and run the timeout handler
539 if (timeHandler && ((get_timer(0) - timeStart) > timeDelta)) {
542 #if defined(CONFIG_MII) || defined(CONFIG_CMD_MII)
543 # if defined(CONFIG_SYS_FAULT_ECHO_LINK_DOWN) && \
544 defined(CONFIG_STATUS_LED) && \
545 defined(STATUS_LED_RED)
547 * Echo the inverted link state to the fault LED.
549 if(miiphy_link(eth_get_dev()->name,
550 CONFIG_SYS_FAULT_MII_ADDR)) {
551 status_led_set (STATUS_LED_RED, STATUS_LED_OFF);
553 status_led_set (STATUS_LED_RED, STATUS_LED_ON);
555 # endif /* CONFIG_SYS_FAULT_ECHO_LINK_DOWN, ... */
556 #endif /* CONFIG_MII, ... */
558 timeHandler = (thand_f *)0;
565 case NETLOOP_RESTART:
566 #ifdef CONFIG_NET_MULTI
571 case NETLOOP_SUCCESS:
572 if (NetBootFileXferSize > 0) {
574 printf("Bytes transferred = %ld (%lx hex)\n",
576 NetBootFileXferSize);
577 sprintf(buf, "%lX", NetBootFileXferSize);
578 setenv("filesize", buf);
580 sprintf(buf, "%lX", (unsigned long)load_addr);
581 setenv("fileaddr", buf);
584 return NetBootFileXferSize;
592 /**********************************************************************/
595 startAgainTimeout(void)
597 NetState = NETLOOP_RESTART;
601 startAgainHandler(uchar *pkt, unsigned dest, IPaddr_t sip,
602 unsigned src, unsigned len)
604 /* Totally ignore the packet */
607 void NetStartAgain (void)
610 int retry_forever = 0;
611 unsigned long retrycnt = 0;
613 nretry = getenv("netretry");
615 if (!strcmp(nretry, "yes"))
617 else if (!strcmp(nretry, "no"))
619 else if (!strcmp(nretry, "once"))
622 retrycnt = simple_strtoul(nretry, NULL, 0);
626 if ((!retry_forever) && (NetTryCount >= retrycnt)) {
628 NetState = NETLOOP_FAIL;
634 #ifndef CONFIG_NET_MULTI
635 NetSetTimeout (10000UL, startAgainTimeout);
636 NetSetHandler (startAgainHandler);
637 #else /* !CONFIG_NET_MULTI*/
639 #if !defined(CONFIG_NET_DO_NOT_TRY_ANOTHER)
640 eth_try_another (!NetRestarted);
643 if (NetRestartWrap) {
646 NetSetTimeout (10000UL, startAgainTimeout);
647 NetSetHandler (startAgainHandler);
649 NetState = NETLOOP_FAIL;
652 NetState = NETLOOP_RESTART;
654 #endif /* CONFIG_NET_MULTI */
657 /**********************************************************************/
663 NetSetHandler(rxhand_f * f)
670 NetSetTimeout(ulong iv, thand_f * f)
673 timeHandler = (thand_f *)0;
676 timeStart = get_timer(0);
683 NetSendPacket(volatile uchar * pkt, int len)
685 (void) eth_send(pkt, len);
689 NetSendUDPPacket(uchar *ether, IPaddr_t dest, int dport, int sport, int len)
693 /* convert to new style broadcast */
697 /* if broadcast, make the ether address a broadcast and don't do ARP */
698 if (dest == 0xFFFFFFFF)
699 ether = NetBcastAddr;
702 * if MAC address was not discovered yet, save the packet and do
705 if (memcmp(ether, NetEtherNullAddr, 6) == 0) {
707 debug("sending ARP for %08lx\n", dest);
709 NetArpWaitPacketIP = dest;
710 NetArpWaitPacketMAC = ether;
712 pkt = NetArpWaitTxPacket;
713 pkt += NetSetEther (pkt, NetArpWaitPacketMAC, PROT_IP);
715 NetSetIP (pkt, dest, dport, sport, len);
716 memcpy(pkt + IP_HDR_SIZE, (uchar *)NetTxPacket +
717 (pkt - (uchar *)NetArpWaitTxPacket) + IP_HDR_SIZE, len);
719 /* size of the waiting packet */
720 NetArpWaitTxPacketSize = (pkt - NetArpWaitTxPacket) +
723 /* and do the ARP request */
725 NetArpWaitTimerStart = get_timer(0);
727 return 1; /* waiting */
730 debug("sending UDP to %08lx/%pM\n", dest, ether);
732 pkt = (uchar *)NetTxPacket;
733 pkt += NetSetEther (pkt, ether, PROT_IP);
734 NetSetIP (pkt, dest, dport, sport, len);
735 (void) eth_send(NetTxPacket, (pkt - NetTxPacket) + IP_HDR_SIZE + len);
737 return 0; /* transmitted */
740 #if defined(CONFIG_CMD_PING)
741 static ushort PingSeqNo;
750 /* XXX always send arp request */
752 memcpy(mac, NetEtherNullAddr, 6);
754 debug("sending ARP for %08lx\n", NetPingIP);
756 NetArpWaitPacketIP = NetPingIP;
757 NetArpWaitPacketMAC = mac;
759 pkt = NetArpWaitTxPacket;
760 pkt += NetSetEther(pkt, mac, PROT_IP);
762 ip = (volatile IP_t *)pkt;
765 * Construct an IP and ICMP header.
766 * (need to set no fragment bit - XXX)
768 /* IP_HDR_SIZE / 4 (not including UDP) */
771 ip->ip_len = htons(IP_HDR_SIZE_NO_UDP + 8);
772 ip->ip_id = htons(NetIPID++);
773 ip->ip_off = htons(IP_FLAGS_DFRAG); /* Don't fragment */
775 ip->ip_p = 0x01; /* ICMP */
777 /* already in network byte order */
778 NetCopyIP((void*)&ip->ip_src, &NetOurIP);
780 NetCopyIP((void*)&ip->ip_dst, &NetPingIP);
781 ip->ip_sum = ~NetCksum((uchar *)ip, IP_HDR_SIZE_NO_UDP / 2);
783 s = &ip->udp_src; /* XXX ICMP starts here */
784 s[0] = htons(0x0800); /* echo-request, code */
785 s[1] = 0; /* checksum */
786 s[2] = 0; /* identifier */
787 s[3] = htons(PingSeqNo++); /* sequence number */
788 s[1] = ~NetCksum((uchar *)s, 8/2);
790 /* size of the waiting packet */
791 NetArpWaitTxPacketSize =
792 (pkt - NetArpWaitTxPacket) + IP_HDR_SIZE_NO_UDP + 8;
794 /* and do the ARP request */
796 NetArpWaitTimerStart = get_timer(0);
798 return 1; /* waiting */
805 NetState = NETLOOP_FAIL; /* we did not get the reply */
809 PingHandler(uchar *pkt, unsigned dest, IPaddr_t sip, unsigned src,
812 if (sip != NetPingIP)
815 NetState = NETLOOP_SUCCESS;
818 static void PingStart(void)
820 #if defined(CONFIG_NET_MULTI)
821 printf ("Using %s device\n", eth_get_name());
822 #endif /* CONFIG_NET_MULTI */
823 NetSetTimeout (10000UL, PingTimeout);
824 NetSetHandler (PingHandler);
830 #if defined(CONFIG_CMD_CDP)
832 #define CDP_DEVICE_ID_TLV 0x0001
833 #define CDP_ADDRESS_TLV 0x0002
834 #define CDP_PORT_ID_TLV 0x0003
835 #define CDP_CAPABILITIES_TLV 0x0004
836 #define CDP_VERSION_TLV 0x0005
837 #define CDP_PLATFORM_TLV 0x0006
838 #define CDP_NATIVE_VLAN_TLV 0x000a
839 #define CDP_APPLIANCE_VLAN_TLV 0x000e
840 #define CDP_TRIGGER_TLV 0x000f
841 #define CDP_POWER_CONSUMPTION_TLV 0x0010
842 #define CDP_SYSNAME_TLV 0x0014
843 #define CDP_SYSOBJECT_TLV 0x0015
844 #define CDP_MANAGEMENT_ADDRESS_TLV 0x0016
846 #define CDP_TIMEOUT 250UL /* one packet every 250ms */
851 ushort CDPNativeVLAN;
852 ushort CDPApplianceVLAN;
854 static const uchar CDP_SNAP_hdr[8] = { 0xAA, 0xAA, 0x03, 0x00, 0x00, 0x0C, 0x20,
857 static ushort CDP_compute_csum(const uchar *buff, ushort len)
866 odd = 1 & (ulong)buff;
876 if (result & 0x80000000)
877 result = (result & 0xFFFF) + (result >> 16);
881 leftover = (signed short)(*(const signed char *)buff);
882 /* CISCO SUCKS big time! (and blows too):
883 * CDP uses the IP checksum algorithm with a twist;
884 * for the last byte it *sign* extends and sums.
886 result = (result & 0xffff0000) |
887 ((result + leftover) & 0x0000ffff);
890 result = (result & 0xFFFF) + (result >> 16);
893 result = ((result >> 8) & 0xff) |
894 ((result & 0xff) << 8);
897 /* add up 16-bit and 17-bit words for 17+c bits */
898 result = (result & 0xffff) + (result >> 16);
899 /* add up 16-bit and 2-bit for 16+c bit */
900 result = (result & 0xffff) + (result >> 16);
902 result = (result & 0xffff) + (result >> 16);
905 csum = ~(ushort)result;
907 /* run time endian detection */
908 if (csum != htons(csum)) /* little endian */
914 int CDPSendTrigger(void)
922 #if defined(CONFIG_CDP_DEVICE_ID) || defined(CONFIG_CDP_PORT_ID) || \
923 defined(CONFIG_CDP_VERSION) || defined(CONFIG_CDP_PLATFORM)
928 et = (Ethernet_t *)pkt;
930 /* NOTE: trigger sent not on any VLAN */
932 /* form ethernet header */
933 memcpy(et->et_dest, NetCDPAddr, 6);
934 memcpy(et->et_src, NetOurEther, 6);
936 pkt += ETHER_HDR_SIZE;
939 memcpy((uchar *)pkt, CDP_SNAP_hdr, sizeof(CDP_SNAP_hdr));
940 pkt += sizeof(CDP_SNAP_hdr);
943 *pkt++ = 0x02; /* CDP version 2 */
944 *pkt++ = 180; /* TTL */
945 s = (volatile ushort *)pkt;
947 /* checksum (0 for later calculation) */
951 #ifdef CONFIG_CDP_DEVICE_ID
952 *s++ = htons(CDP_DEVICE_ID_TLV);
953 *s++ = htons(CONFIG_CDP_DEVICE_ID);
954 sprintf(buf, CONFIG_CDP_DEVICE_ID_PREFIX "%pm", NetOurEther);
955 memcpy((uchar *)s, buf, 16);
959 #ifdef CONFIG_CDP_PORT_ID
960 *s++ = htons(CDP_PORT_ID_TLV);
961 memset(buf, 0, sizeof(buf));
962 sprintf(buf, CONFIG_CDP_PORT_ID, eth_get_dev_index());
964 if (len & 1) /* make it even */
966 *s++ = htons(len + 4);
967 memcpy((uchar *)s, buf, len);
971 #ifdef CONFIG_CDP_CAPABILITIES
972 *s++ = htons(CDP_CAPABILITIES_TLV);
974 *(ulong *)s = htonl(CONFIG_CDP_CAPABILITIES);
978 #ifdef CONFIG_CDP_VERSION
979 *s++ = htons(CDP_VERSION_TLV);
980 memset(buf, 0, sizeof(buf));
981 strcpy(buf, CONFIG_CDP_VERSION);
983 if (len & 1) /* make it even */
985 *s++ = htons(len + 4);
986 memcpy((uchar *)s, buf, len);
990 #ifdef CONFIG_CDP_PLATFORM
991 *s++ = htons(CDP_PLATFORM_TLV);
992 memset(buf, 0, sizeof(buf));
993 strcpy(buf, CONFIG_CDP_PLATFORM);
995 if (len & 1) /* make it even */
997 *s++ = htons(len + 4);
998 memcpy((uchar *)s, buf, len);
1002 #ifdef CONFIG_CDP_TRIGGER
1003 *s++ = htons(CDP_TRIGGER_TLV);
1005 *(ulong *)s = htonl(CONFIG_CDP_TRIGGER);
1009 #ifdef CONFIG_CDP_POWER_CONSUMPTION
1010 *s++ = htons(CDP_POWER_CONSUMPTION_TLV);
1012 *s++ = htons(CONFIG_CDP_POWER_CONSUMPTION);
1015 /* length of ethernet packet */
1016 len = (uchar *)s - ((uchar *)NetTxPacket + ETHER_HDR_SIZE);
1017 et->et_protlen = htons(len);
1019 len = ETHER_HDR_SIZE + sizeof(CDP_SNAP_hdr);
1020 chksum = CDP_compute_csum((uchar *)NetTxPacket + len,
1021 (uchar *)s - (NetTxPacket + len));
1024 *cp = htons(chksum);
1026 (void) eth_send(NetTxPacket, (uchar *)s - NetTxPacket);
1036 NetSetTimeout (CDP_TIMEOUT, CDPTimeout);
1041 /* if not OK try again */
1045 NetState = NETLOOP_SUCCESS;
1049 CDPDummyHandler(uchar *pkt, unsigned dest, IPaddr_t sip, unsigned src,
1056 CDPHandler(const uchar * pkt, unsigned len)
1065 if (len < sizeof(CDP_SNAP_hdr) + 4)
1068 /* check for valid CDP SNAP header */
1069 if (memcmp(pkt, CDP_SNAP_hdr, sizeof(CDP_SNAP_hdr)) != 0)
1072 pkt += sizeof(CDP_SNAP_hdr);
1073 len -= sizeof(CDP_SNAP_hdr);
1075 /* Version of CDP protocol must be >= 2 and TTL != 0 */
1076 if (pkt[0] < 0x02 || pkt[1] == 0)
1080 * if version is greater than 0x02 maybe we'll have a problem;
1084 printf("** WARNING: CDP packet received with a protocol version %d > 2\n",
1087 if (CDP_compute_csum(pkt, len) != 0)
1099 ss = (const ushort *)pkt;
1100 type = ntohs(ss[0]);
1101 tlen = ntohs(ss[1]);
1109 ss += 2; /* point ss to the data of the TLV */
1113 case CDP_DEVICE_ID_TLV:
1115 case CDP_ADDRESS_TLV:
1117 case CDP_PORT_ID_TLV:
1119 case CDP_CAPABILITIES_TLV:
1121 case CDP_VERSION_TLV:
1123 case CDP_PLATFORM_TLV:
1125 case CDP_NATIVE_VLAN_TLV:
1128 case CDP_APPLIANCE_VLAN_TLV:
1129 t = (const uchar *)ss;
1135 ss = (const ushort *)(t + 1);
1137 #ifdef CONFIG_CDP_APPLIANCE_VLAN_TYPE
1139 CONFIG_CDP_APPLIANCE_VLAN_TYPE)
1142 /* XXX will this work; dunno */
1148 case CDP_TRIGGER_TLV:
1150 case CDP_POWER_CONSUMPTION_TLV:
1152 case CDP_SYSNAME_TLV:
1154 case CDP_SYSOBJECT_TLV:
1156 case CDP_MANAGEMENT_ADDRESS_TLV:
1161 CDPApplianceVLAN = vlan;
1162 CDPNativeVLAN = nvlan;
1168 printf("** CDP packet is too short\n");
1172 static void CDPStart(void)
1174 #if defined(CONFIG_NET_MULTI)
1175 printf ("Using %s device\n", eth_get_name());
1180 CDPNativeVLAN = htons(-1);
1181 CDPApplianceVLAN = htons(-1);
1183 NetSetTimeout (CDP_TIMEOUT, CDPTimeout);
1184 NetSetHandler (CDPDummyHandler);
1190 #ifdef CONFIG_IP_DEFRAG
1192 * This function collects fragments in a single packet, according
1193 * to the algorithm in RFC815. It returns NULL or the pointer to
1194 * a complete packet, in static storage
1196 #ifndef CONFIG_NET_MAXDEFRAG
1197 #define CONFIG_NET_MAXDEFRAG 16384
1200 * MAXDEFRAG, above, is chosen in the config file and is real data
1201 * so we need to add the NFS overhead, which is more than TFTP.
1202 * To use sizeof in the internal unnamed structures, we need a real
1203 * instance (can't do "sizeof(struct rpc_t.u.reply))", unfortunately).
1204 * The compiler doesn't complain nor allocates the actual structure
1206 static struct rpc_t rpc_specimen;
1207 #define IP_PKTSIZE (CONFIG_NET_MAXDEFRAG + sizeof(rpc_specimen.u.reply))
1209 #define IP_MAXUDP (IP_PKTSIZE - IP_HDR_SIZE_NO_UDP)
1212 * this is the packet being assembled, either data or frag control.
1213 * Fragments go by 8 bytes, so this union must be 8 bytes long
1216 /* first_byte is address of this structure */
1217 u16 last_byte; /* last byte in this hole + 1 (begin of next hole) */
1218 u16 next_hole; /* index of next (in 8-b blocks), 0 == none */
1219 u16 prev_hole; /* index of prev, 0 == none */
1223 static IP_t *__NetDefragment(IP_t *ip, int *lenp)
1225 static uchar pkt_buff[IP_PKTSIZE] __attribute__((aligned(PKTALIGN)));
1226 static u16 first_hole, total_len;
1227 struct hole *payload, *thisfrag, *h, *newh;
1228 IP_t *localip = (IP_t *)pkt_buff;
1229 uchar *indata = (uchar *)ip;
1230 int offset8, start, len, done = 0;
1231 u16 ip_off = ntohs(ip->ip_off);
1233 /* payload starts after IP header, this fragment is in there */
1234 payload = (struct hole *)(pkt_buff + IP_HDR_SIZE_NO_UDP);
1235 offset8 = (ip_off & IP_OFFS);
1236 thisfrag = payload + offset8;
1237 start = offset8 * 8;
1238 len = ntohs(ip->ip_len) - IP_HDR_SIZE_NO_UDP;
1240 if (start + len > IP_MAXUDP) /* fragment extends too far */
1243 if (!total_len || localip->ip_id != ip->ip_id) {
1244 /* new (or different) packet, reset structs */
1246 payload[0].last_byte = ~0;
1247 payload[0].next_hole = 0;
1248 payload[0].prev_hole = 0;
1250 /* any IP header will work, copy the first we received */
1251 memcpy(localip, ip, IP_HDR_SIZE_NO_UDP);
1255 * What follows is the reassembly algorithm. We use the payload
1256 * array as a linked list of hole descriptors, as each hole starts
1257 * at a multiple of 8 bytes. However, last byte can be whatever value,
1258 * so it is represented as byte count, not as 8-byte blocks.
1261 h = payload + first_hole;
1262 while (h->last_byte < start) {
1263 if (!h->next_hole) {
1264 /* no hole that far away */
1267 h = payload + h->next_hole;
1270 /* last fragment may be 1..7 bytes, the "+7" forces acceptance */
1271 if (offset8 + ((len + 7) / 8) <= h - payload) {
1272 /* no overlap with holes (dup fragment?) */
1276 if (!(ip_off & IP_FLAGS_MFRAG)) {
1277 /* no more fragmentss: truncate this (last) hole */
1278 total_len = start + len;
1279 h->last_byte = start + len;
1283 * There is some overlap: fix the hole list. This code doesn't
1284 * deal with a fragment that overlaps with two different holes
1285 * (thus being a superset of a previously-received fragment).
1288 if ( (h >= thisfrag) && (h->last_byte <= start + len) ) {
1289 /* complete overlap with hole: remove hole */
1290 if (!h->prev_hole && !h->next_hole) {
1291 /* last remaining hole */
1293 } else if (!h->prev_hole) {
1295 first_hole = h->next_hole;
1296 payload[h->next_hole].prev_hole = 0;
1297 } else if (!h->next_hole) {
1299 payload[h->prev_hole].next_hole = 0;
1301 /* in the middle of the list */
1302 payload[h->next_hole].prev_hole = h->prev_hole;
1303 payload[h->prev_hole].next_hole = h->next_hole;
1306 } else if (h->last_byte <= start + len) {
1307 /* overlaps with final part of the hole: shorten this hole */
1308 h->last_byte = start;
1310 } else if (h >= thisfrag) {
1311 /* overlaps with initial part of the hole: move this hole */
1312 newh = thisfrag + (len / 8);
1316 payload[h->next_hole].prev_hole = (h - payload);
1318 payload[h->prev_hole].next_hole = (h - payload);
1320 first_hole = (h - payload);
1323 /* fragment sits in the middle: split the hole */
1324 newh = thisfrag + (len / 8);
1326 h->last_byte = start;
1327 h->next_hole = (newh - payload);
1328 newh->prev_hole = (h - payload);
1329 if (newh->next_hole)
1330 payload[newh->next_hole].prev_hole = (newh - payload);
1333 /* finally copy this fragment and possibly return whole packet */
1334 memcpy((uchar *)thisfrag, indata + IP_HDR_SIZE_NO_UDP, len);
1338 localip->ip_len = htons(total_len);
1339 *lenp = total_len + IP_HDR_SIZE_NO_UDP;
1343 static inline IP_t *NetDefragment(IP_t *ip, int *lenp)
1345 u16 ip_off = ntohs(ip->ip_off);
1346 if (!(ip_off & (IP_OFFS | IP_FLAGS_MFRAG)))
1347 return ip; /* not a fragment */
1348 return __NetDefragment(ip, lenp);
1351 #else /* !CONFIG_IP_DEFRAG */
1353 static inline IP_t *NetDefragment(IP_t *ip, int *lenp)
1355 u16 ip_off = ntohs(ip->ip_off);
1356 if (!(ip_off & (IP_OFFS | IP_FLAGS_MFRAG)))
1357 return ip; /* not a fragment */
1363 NetReceive(volatile uchar * inpkt, int len)
1372 #if defined(CONFIG_CMD_CDP)
1375 ushort cti = 0, vlanid = VLAN_NONE, myvlanid, mynvlanid;
1377 debug("packet received\n");
1379 NetRxPacket = inpkt;
1380 NetRxPacketLen = len;
1381 et = (Ethernet_t *)inpkt;
1383 /* too small packet? */
1384 if (len < ETHER_HDR_SIZE)
1389 (*push_packet)(inpkt, len);
1394 #if defined(CONFIG_CMD_CDP)
1395 /* keep track if packet is CDP */
1396 iscdp = memcmp(et->et_dest, NetCDPAddr, 6) == 0;
1399 myvlanid = ntohs(NetOurVLAN);
1400 if (myvlanid == (ushort)-1)
1401 myvlanid = VLAN_NONE;
1402 mynvlanid = ntohs(NetOurNativeVLAN);
1403 if (mynvlanid == (ushort)-1)
1404 mynvlanid = VLAN_NONE;
1406 x = ntohs(et->et_protlen);
1408 debug("packet received\n");
1412 * Got a 802 packet. Check the other protocol field.
1414 x = ntohs(et->et_prot);
1416 ip = (IP_t *)(inpkt + E802_HDR_SIZE);
1417 len -= E802_HDR_SIZE;
1419 } else if (x != PROT_VLAN) { /* normal packet */
1420 ip = (IP_t *)(inpkt + ETHER_HDR_SIZE);
1421 len -= ETHER_HDR_SIZE;
1423 } else { /* VLAN packet */
1424 VLAN_Ethernet_t *vet = (VLAN_Ethernet_t *)et;
1426 debug("VLAN packet received\n");
1428 /* too small packet? */
1429 if (len < VLAN_ETHER_HDR_SIZE)
1432 /* if no VLAN active */
1433 if ((ntohs(NetOurVLAN) & VLAN_IDMASK) == VLAN_NONE
1434 #if defined(CONFIG_CMD_CDP)
1440 cti = ntohs(vet->vet_tag);
1441 vlanid = cti & VLAN_IDMASK;
1442 x = ntohs(vet->vet_type);
1444 ip = (IP_t *)(inpkt + VLAN_ETHER_HDR_SIZE);
1445 len -= VLAN_ETHER_HDR_SIZE;
1448 debug("Receive from protocol 0x%x\n", x);
1450 #if defined(CONFIG_CMD_CDP)
1452 CDPHandler((uchar *)ip, len);
1457 if ((myvlanid & VLAN_IDMASK) != VLAN_NONE) {
1458 if (vlanid == VLAN_NONE)
1459 vlanid = (mynvlanid & VLAN_IDMASK);
1461 if (vlanid != (myvlanid & VLAN_IDMASK))
1469 * We have to deal with two types of ARP packets:
1470 * - REQUEST packets will be answered by sending our
1471 * IP address - if we know it.
1472 * - REPLY packates are expected only after we asked
1473 * for the TFTP server's or the gateway's ethernet
1474 * address; so if we receive such a packet, we set
1475 * the server ethernet address
1480 if (len < ARP_HDR_SIZE) {
1481 printf("bad length %d < %d\n", len, ARP_HDR_SIZE);
1484 if (ntohs(arp->ar_hrd) != ARP_ETHER) {
1487 if (ntohs(arp->ar_pro) != PROT_IP) {
1490 if (arp->ar_hln != 6) {
1493 if (arp->ar_pln != 4) {
1497 if (NetOurIP == 0) {
1501 if (NetReadIP(&arp->ar_data[16]) != NetOurIP) {
1505 switch (ntohs(arp->ar_op)) {
1507 /* reply with our IP address */
1508 debug("Got ARP REQUEST, return our IP\n");
1510 pkt += NetSetEther(pkt, et->et_src, PROT_ARP);
1511 arp->ar_op = htons(ARPOP_REPLY);
1512 memcpy (&arp->ar_data[10], &arp->ar_data[0], 6);
1513 NetCopyIP(&arp->ar_data[16], &arp->ar_data[6]);
1514 memcpy (&arp->ar_data[ 0], NetOurEther, 6);
1515 NetCopyIP(&arp->ar_data[ 6], &NetOurIP);
1516 (void) eth_send((uchar *)et,
1517 (pkt - (uchar *)et) + ARP_HDR_SIZE);
1520 case ARPOP_REPLY: /* arp reply */
1521 /* are we waiting for a reply */
1522 if (!NetArpWaitPacketIP || !NetArpWaitPacketMAC)
1525 #ifdef CONFIG_KEEP_SERVERADDR
1526 if (NetServerIP == NetArpWaitPacketIP) {
1528 sprintf(buf, "%pM", arp->ar_data);
1529 setenv("serveraddr", buf);
1533 debug("Got ARP REPLY, set server/gtwy eth addr (%pM)\n",
1536 tmp = NetReadIP(&arp->ar_data[6]);
1538 /* matched waiting packet's address */
1539 if (tmp == NetArpWaitReplyIP) {
1541 /* save address for later use */
1542 memcpy(NetArpWaitPacketMAC,
1543 &arp->ar_data[0], 6);
1545 #ifdef CONFIG_NETCONSOLE
1546 (*packetHandler)(0, 0, 0, 0, 0);
1548 /* modify header, and transmit it */
1549 memcpy(((Ethernet_t *)NetArpWaitTxPacket)->et_dest, NetArpWaitPacketMAC, 6);
1550 (void) eth_send(NetArpWaitTxPacket,
1551 NetArpWaitTxPacketSize);
1553 /* no arp request pending now */
1554 NetArpWaitPacketIP = 0;
1555 NetArpWaitTxPacketSize = 0;
1556 NetArpWaitPacketMAC = NULL;
1561 debug("Unexpected ARP opcode 0x%x\n",
1567 #ifdef CONFIG_CMD_RARP
1569 debug("Got RARP\n");
1571 if (len < ARP_HDR_SIZE) {
1572 printf("bad length %d < %d\n", len, ARP_HDR_SIZE);
1576 if ((ntohs(arp->ar_op) != RARPOP_REPLY) ||
1577 (ntohs(arp->ar_hrd) != ARP_ETHER) ||
1578 (ntohs(arp->ar_pro) != PROT_IP) ||
1579 (arp->ar_hln != 6) || (arp->ar_pln != 4)) {
1581 puts ("invalid RARP header\n");
1583 NetCopyIP(&NetOurIP, &arp->ar_data[16]);
1584 if (NetServerIP == 0)
1585 NetCopyIP(&NetServerIP, &arp->ar_data[ 6]);
1586 memcpy (NetServerEther, &arp->ar_data[ 0], 6);
1588 (*packetHandler)(0, 0, 0, 0, 0);
1594 /* Before we start poking the header, make sure it is there */
1595 if (len < IP_HDR_SIZE) {
1596 debug("len bad %d < %lu\n", len, (ulong)IP_HDR_SIZE);
1599 /* Check the packet length */
1600 if (len < ntohs(ip->ip_len)) {
1601 printf("len bad %d < %d\n", len, ntohs(ip->ip_len));
1604 len = ntohs(ip->ip_len);
1605 debug("len=%d, v=%02x\n", len, ip->ip_hl_v & 0xff);
1607 /* Can't deal with anything except IPv4 */
1608 if ((ip->ip_hl_v & 0xf0) != 0x40) {
1611 /* Can't deal with IP options (headers != 20 bytes) */
1612 if ((ip->ip_hl_v & 0x0f) > 0x05) {
1615 /* Check the Checksum of the header */
1616 if (!NetCksumOk((uchar *)ip, IP_HDR_SIZE_NO_UDP / 2)) {
1617 puts ("checksum bad\n");
1620 /* If it is not for us, ignore it */
1621 tmp = NetReadIP(&ip->ip_dst);
1622 if (NetOurIP && tmp != NetOurIP && tmp != 0xFFFFFFFF) {
1623 #ifdef CONFIG_MCAST_TFTP
1624 if (Mcast_addr != tmp)
1628 /* Read source IP address for later use */
1629 src_ip = NetReadIP(&ip->ip_src);
1631 * The function returns the unchanged packet if it's not
1632 * a fragment, and either the complete packet or NULL if
1633 * it is a fragment (if !CONFIG_IP_DEFRAG, it returns NULL)
1635 if (!(ip = NetDefragment(ip, &len)))
1638 * watch for ICMP host redirects
1640 * There is no real handler code (yet). We just watch
1641 * for ICMP host redirect messages. In case anybody
1642 * sees these messages: please contact me
1643 * (wd@denx.de), or - even better - send me the
1644 * necessary fixes :-)
1646 * Note: in all cases where I have seen this so far
1647 * it was a problem with the router configuration,
1648 * for instance when a router was configured in the
1649 * BOOTP reply, but the TFTP server was on the same
1650 * subnet. So this is probably a warning that your
1651 * configuration might be wrong. But I'm not really
1652 * sure if there aren't any other situations.
1654 if (ip->ip_p == IPPROTO_ICMP) {
1655 ICMP_t *icmph = (ICMP_t *)&(ip->udp_src);
1657 switch (icmph->type) {
1659 if (icmph->code != ICMP_REDIR_HOST)
1661 printf (" ICMP Host Redirect to %pI4 ",
1662 &icmph->un.gateway);
1664 #if defined(CONFIG_CMD_PING)
1665 case ICMP_ECHO_REPLY:
1667 * IP header OK. Pass the packet to the
1670 /* XXX point to ip packet */
1671 (*packetHandler)((uchar *)ip, 0, src_ip, 0, 0);
1673 case ICMP_ECHO_REQUEST:
1674 debug("Got ICMP ECHO REQUEST, return %d bytes\n",
1675 ETHER_HDR_SIZE + len);
1677 memcpy (&et->et_dest[0], &et->et_src[0], 6);
1678 memcpy (&et->et_src[ 0], NetOurEther, 6);
1682 NetCopyIP((void*)&ip->ip_dst, &ip->ip_src);
1683 NetCopyIP((void*)&ip->ip_src, &NetOurIP);
1684 ip->ip_sum = ~NetCksum((uchar *)ip,
1685 IP_HDR_SIZE_NO_UDP >> 1);
1687 icmph->type = ICMP_ECHO_REPLY;
1688 icmph->checksum = 0;
1689 icmph->checksum = ~NetCksum((uchar *)icmph,
1690 (len - IP_HDR_SIZE_NO_UDP) >> 1);
1691 (void) eth_send((uchar *)et,
1692 ETHER_HDR_SIZE + len);
1698 } else if (ip->ip_p != IPPROTO_UDP) { /* Only UDP packets */
1702 #ifdef CONFIG_UDP_CHECKSUM
1703 if (ip->udp_xsum != 0) {
1709 xsum += (ntohs(ip->udp_len));
1710 xsum += (ntohl(ip->ip_src) >> 16) & 0x0000ffff;
1711 xsum += (ntohl(ip->ip_src) >> 0) & 0x0000ffff;
1712 xsum += (ntohl(ip->ip_dst) >> 16) & 0x0000ffff;
1713 xsum += (ntohl(ip->ip_dst) >> 0) & 0x0000ffff;
1715 sumlen = ntohs(ip->udp_len);
1716 sumptr = (ushort *) &(ip->udp_src);
1718 while (sumlen > 1) {
1721 sumdata = *sumptr++;
1722 xsum += ntohs(sumdata);
1728 sumdata = *(unsigned char *) sumptr;
1729 sumdata = (sumdata << 8) & 0xff00;
1732 while ((xsum >> 16) != 0) {
1733 xsum = (xsum & 0x0000ffff) +
1734 ((xsum >> 16) & 0x0000ffff);
1736 if ((xsum != 0x00000000) && (xsum != 0x0000ffff)) {
1737 printf(" UDP wrong checksum %08lx %08x\n",
1738 xsum, ntohs(ip->udp_xsum));
1745 #ifdef CONFIG_NETCONSOLE
1746 nc_input_packet((uchar *)ip +IP_HDR_SIZE,
1749 ntohs(ip->udp_len) - 8);
1752 * IP header OK. Pass the packet to the current handler.
1754 (*packetHandler)((uchar *)ip +IP_HDR_SIZE,
1758 ntohs(ip->udp_len) - 8);
1764 /**********************************************************************/
1766 static int net_check_prereq (proto_t protocol)
1770 #if defined(CONFIG_CMD_PING)
1772 if (NetPingIP == 0) {
1773 puts ("*** ERROR: ping address not given\n");
1778 #if defined(CONFIG_CMD_SNTP)
1780 if (NetNtpServerIP == 0) {
1781 puts ("*** ERROR: NTP server address not given\n");
1786 #if defined(CONFIG_CMD_DNS)
1788 if (NetOurDNSIP == 0) {
1789 puts("*** ERROR: DNS server address not given\n");
1794 #if defined(CONFIG_CMD_NFS)
1798 if (NetServerIP == 0) {
1799 puts ("*** ERROR: `serverip' not set\n");
1802 #if defined(CONFIG_CMD_PING) || defined(CONFIG_CMD_SNTP) || \
1803 defined(CONFIG_CMD_DNS)
1809 if (NetOurIP == 0) {
1810 puts ("*** ERROR: `ipaddr' not set\n");
1815 #ifdef CONFIG_CMD_RARP
1821 if (memcmp (NetOurEther, "\0\0\0\0\0\0", 6) == 0) {
1822 #ifdef CONFIG_NET_MULTI
1823 extern int eth_get_dev_index (void);
1824 int num = eth_get_dev_index ();
1828 puts ("*** ERROR: No ethernet found.\n");
1831 puts ("*** ERROR: `ethaddr' not set\n");
1834 printf ("*** ERROR: `eth%daddr' not set\n",
1842 puts ("*** ERROR: `ethaddr' not set\n");
1850 return (0); /* OK */
1852 /**********************************************************************/
1855 NetCksumOk(uchar * ptr, int len)
1857 return !((NetCksum(ptr, len) + 1) & 0xfffe);
1862 NetCksum(uchar * ptr, int len)
1865 ushort *p = (ushort *)ptr;
1870 xsum = (xsum & 0xffff) + (xsum >> 16);
1871 xsum = (xsum & 0xffff) + (xsum >> 16);
1872 return (xsum & 0xffff);
1880 myvlanid = ntohs(NetOurVLAN);
1881 if (myvlanid == (ushort)-1)
1882 myvlanid = VLAN_NONE;
1884 return ((myvlanid & VLAN_IDMASK) == VLAN_NONE) ? ETHER_HDR_SIZE :
1885 VLAN_ETHER_HDR_SIZE;
1889 NetSetEther(volatile uchar * xet, uchar * addr, uint prot)
1891 Ethernet_t *et = (Ethernet_t *)xet;
1894 myvlanid = ntohs(NetOurVLAN);
1895 if (myvlanid == (ushort)-1)
1896 myvlanid = VLAN_NONE;
1898 memcpy (et->et_dest, addr, 6);
1899 memcpy (et->et_src, NetOurEther, 6);
1900 if ((myvlanid & VLAN_IDMASK) == VLAN_NONE) {
1901 et->et_protlen = htons(prot);
1902 return ETHER_HDR_SIZE;
1904 VLAN_Ethernet_t *vet = (VLAN_Ethernet_t *)xet;
1906 vet->vet_vlan_type = htons(PROT_VLAN);
1907 vet->vet_tag = htons((0 << 5) | (myvlanid & VLAN_IDMASK));
1908 vet->vet_type = htons(prot);
1909 return VLAN_ETHER_HDR_SIZE;
1914 NetSetIP(volatile uchar * xip, IPaddr_t dest, int dport, int sport, int len)
1916 IP_t *ip = (IP_t *)xip;
1919 * If the data is an odd number of bytes, zero the
1920 * byte after the last byte so that the checksum
1924 xip[IP_HDR_SIZE + len] = 0;
1927 * Construct an IP and UDP header.
1928 * (need to set no fragment bit - XXX)
1930 /* IP_HDR_SIZE / 4 (not including UDP) */
1933 ip->ip_len = htons(IP_HDR_SIZE + len);
1934 ip->ip_id = htons(NetIPID++);
1935 ip->ip_off = htons(IP_FLAGS_DFRAG); /* Don't fragment */
1937 ip->ip_p = 17; /* UDP */
1939 /* already in network byte order */
1940 NetCopyIP((void*)&ip->ip_src, &NetOurIP);
1942 NetCopyIP((void*)&ip->ip_dst, &dest);
1943 ip->udp_src = htons(sport);
1944 ip->udp_dst = htons(dport);
1945 ip->udp_len = htons(8 + len);
1947 ip->ip_sum = ~NetCksum((uchar *)ip, IP_HDR_SIZE_NO_UDP / 2);
1950 void copy_filename (char *dst, const char *src, int size)
1952 if (*src && (*src == '"')) {
1957 while ((--size > 0) && *src && (*src != '"')) {
1963 #if defined(CONFIG_CMD_NFS) || \
1964 defined(CONFIG_CMD_SNTP) || \
1965 defined(CONFIG_CMD_DNS)
1967 * make port a little random (1024-17407)
1968 * This keeps the math somewhat trivial to compute, and seems to work with
1969 * all supported protocols/clients/servers
1971 unsigned int random_port(void)
1973 return 1024 + (get_timer(0) % 0x4000);
1977 void ip_to_string (IPaddr_t x, char *s)
1980 sprintf (s, "%d.%d.%d.%d",
1981 (int) ((x >> 24) & 0xff),
1982 (int) ((x >> 16) & 0xff),
1983 (int) ((x >> 8) & 0xff), (int) ((x >> 0) & 0xff)
1987 void VLAN_to_string(ushort x, char *s)
1991 if (x == (ushort)-1)
1997 sprintf(s, "%d", x & VLAN_IDMASK);
2000 ushort string_to_VLAN(const char *s)
2005 return htons(VLAN_NONE);
2007 if (*s < '0' || *s > '9')
2010 id = (ushort)simple_strtoul(s, NULL, 10);
2015 ushort getenv_VLAN(char *var)
2017 return (string_to_VLAN(getenv(var)));