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
85 #ifdef CONFIG_STATUS_LED
86 #include <status_led.h>
89 #if (CONFIG_COMMANDS & CFG_CMD_SNTP)
93 #if (CONFIG_COMMANDS & CFG_CMD_NET)
95 #define ARP_TIMEOUT 5 /* Seconds before trying ARP again */
96 #ifndef CONFIG_NET_RETRY_COUNT
97 # define ARP_TIMEOUT_COUNT 5 /* # of timeouts before giving up */
99 # define ARP_TIMEOUT_COUNT (CONFIG_NET_RETRY_COUNT)
106 /** BOOTP EXTENTIONS **/
108 IPaddr_t NetOurSubnetMask
=0; /* Our subnet mask (0=unknown) */
109 IPaddr_t NetOurGatewayIP
=0; /* Our gateways IP address */
110 IPaddr_t NetOurDNSIP
=0; /* Our DNS IP address */
111 #if (CONFIG_BOOTP_MASK & CONFIG_BOOTP_DNS2)
112 IPaddr_t NetOurDNS2IP
=0; /* Our 2nd DNS IP address */
114 char NetOurNISDomain
[32]={0,}; /* Our NIS domain */
115 char NetOurHostName
[32]={0,}; /* Our hostname */
116 char NetOurRootPath
[64]={0,}; /* Our bootpath */
117 ushort NetBootFileSize
=0; /* Our bootfile size in blocks */
119 /** END OF BOOTP EXTENTIONS **/
121 ulong NetBootFileXferSize
; /* The actual transferred size of the bootfile (in bytes) */
122 uchar NetOurEther
[6]; /* Our ethernet address */
123 uchar NetServerEther
[6] = /* Boot server enet address */
124 { 0, 0, 0, 0, 0, 0 };
125 IPaddr_t NetOurIP
; /* Our IP addr (0 = unknown) */
126 IPaddr_t NetServerIP
; /* Our IP addr (0 = unknown) */
127 volatile uchar
*NetRxPkt
; /* Current receive packet */
128 int NetRxPktLen
; /* Current rx packet length */
129 unsigned NetIPID
; /* IP packet ID */
130 uchar NetBcastAddr
[6] = /* Ethernet bcast address */
131 { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
132 uchar NetEtherNullAddr
[6] =
133 { 0, 0, 0, 0, 0, 0 };
134 #if (CONFIG_COMMANDS & CFG_CMD_CDP)
135 uchar NetCDPAddr
[6] = /* Ethernet bcast address */
136 { 0x01, 0x00, 0x0c, 0xcc, 0xcc, 0xcc };
138 int NetState
; /* Network loop state */
139 #ifdef CONFIG_NET_MULTI
140 int NetRestartWrap
= 0; /* Tried all network devices */
141 static int NetRestarted
= 0; /* Network loop restarted */
142 static int NetDevExists
= 0; /* At least one device configured */
145 /* XXX in both little & big endian machines 0xFFFF == ntohs(-1) */
146 ushort NetOurVLAN
= 0xFFFF; /* default is without VLAN */
147 ushort NetOurNativeVLAN
= 0xFFFF; /* ditto */
149 char BootFile
[128]; /* Boot File name */
151 #if (CONFIG_COMMANDS & CFG_CMD_PING)
152 IPaddr_t NetPingIP
; /* the ip address to ping */
154 static void PingStart(void);
157 #if (CONFIG_COMMANDS & CFG_CMD_CDP)
158 static void CDPStart(void);
161 #if (CONFIG_COMMANDS & CFG_CMD_SNTP)
162 IPaddr_t NetNtpServerIP
; /* NTP server IP address */
163 int NetTimeOffset
=0; /* offset time from UTC */
166 #ifdef CONFIG_NETCONSOLE
168 int nc_input_packet(uchar
*pkt
, unsigned dest
, unsigned src
, unsigned len
);
171 volatile uchar PktBuf
[(PKTBUFSRX
+1) * PKTSIZE_ALIGN
+ PKTALIGN
];
173 volatile uchar
*NetRxPackets
[PKTBUFSRX
]; /* Receive packets */
175 static rxhand_f
*packetHandler
; /* Current RX packet handler */
176 static thand_f
*timeHandler
; /* Current timeout handler */
177 static ulong timeStart
; /* Time base value */
178 static ulong timeDelta
; /* Current timeout value */
179 volatile uchar
*NetTxPacket
= 0; /* THE transmit packet */
181 static int net_check_prereq (proto_t protocol
);
183 /**********************************************************************/
185 IPaddr_t NetArpWaitPacketIP
;
186 IPaddr_t NetArpWaitReplyIP
;
187 uchar
*NetArpWaitPacketMAC
; /* MAC address of waiting packet's destination */
188 uchar
*NetArpWaitTxPacket
; /* THE transmit packet */
189 int NetArpWaitTxPacketSize
;
190 uchar NetArpWaitPacketBuf
[PKTSIZE_ALIGN
+ PKTALIGN
];
191 ulong NetArpWaitTimerStart
;
194 void ArpRequest (void)
201 printf ("ARP broadcast %d\n", NetArpWaitTry
);
205 pkt
+= NetSetEther (pkt
, NetBcastAddr
, PROT_ARP
);
209 arp
->ar_hrd
= htons (ARP_ETHER
);
210 arp
->ar_pro
= htons (PROT_IP
);
213 arp
->ar_op
= htons (ARPOP_REQUEST
);
215 memcpy (&arp
->ar_data
[0], NetOurEther
, 6); /* source ET addr */
216 NetWriteIP ((uchar
*) & arp
->ar_data
[6], NetOurIP
); /* source IP addr */
217 for (i
= 10; i
< 16; ++i
) {
218 arp
->ar_data
[i
] = 0; /* dest ET addr = 0 */
221 if ((NetArpWaitPacketIP
& NetOurSubnetMask
) !=
222 (NetOurIP
& NetOurSubnetMask
)) {
223 if (NetOurGatewayIP
== 0) {
224 puts ("## Warning: gatewayip needed but not set\n");
226 NetArpWaitReplyIP
= NetOurGatewayIP
;
228 NetArpWaitReplyIP
= NetArpWaitPacketIP
;
231 NetWriteIP ((uchar
*) & arp
->ar_data
[16], NetArpWaitReplyIP
);
232 (void) eth_send (NetTxPacket
, (pkt
- NetTxPacket
) + ARP_HDR_SIZE
);
235 void ArpTimeoutCheck(void)
239 if (!NetArpWaitPacketIP
)
244 /* check for arp timeout */
245 if ((t
- NetArpWaitTimerStart
) > ARP_TIMEOUT
* CFG_HZ
) {
248 if (NetArpWaitTry
>= ARP_TIMEOUT_COUNT
) {
249 puts ("\nARP Retry count exceeded; starting again\n");
253 NetArpWaitTimerStart
= t
;
259 /**********************************************************************/
261 * Main network processing loop.
265 NetLoop(proto_t protocol
)
267 DECLARE_GLOBAL_DATA_PTR
;
271 #ifdef CONFIG_NET_MULTI
276 /* XXX problem with bss workaround */
277 NetArpWaitPacketMAC
= NULL
;
278 NetArpWaitTxPacket
= NULL
;
279 NetArpWaitPacketIP
= 0;
280 NetArpWaitReplyIP
= 0;
281 NetArpWaitTxPacket
= NULL
;
287 * Setup packet buffers, aligned correctly.
289 NetTxPacket
= &PktBuf
[0] + (PKTALIGN
- 1);
290 NetTxPacket
-= (ulong
)NetTxPacket
% PKTALIGN
;
291 for (i
= 0; i
< PKTBUFSRX
; i
++) {
292 NetRxPackets
[i
] = NetTxPacket
+ (i
+1)*PKTSIZE_ALIGN
;
296 if (!NetArpWaitTxPacket
) {
297 NetArpWaitTxPacket
= &NetArpWaitPacketBuf
[0] + (PKTALIGN
- 1);
298 NetArpWaitTxPacket
-= (ulong
)NetArpWaitTxPacket
% PKTALIGN
;
299 NetArpWaitTxPacketSize
= 0;
303 #ifdef CONFIG_NET_MULTI
306 if (eth_init(bd
) < 0)
310 #ifdef CONFIG_NET_MULTI
311 memcpy (NetOurEther
, eth_get_dev()->enetaddr
, 6);
313 memcpy (NetOurEther
, bd
->bi_enetaddr
, 6);
316 NetState
= NETLOOP_CONTINUE
;
319 * Start the ball rolling with the given start function. From
320 * here on, this code is a state machine driven by received
321 * packets and timer events.
325 #if (CONFIG_COMMANDS & CFG_CMD_NFS)
328 #if (CONFIG_COMMANDS & CFG_CMD_PING)
331 #if (CONFIG_COMMANDS & CFG_CMD_SNTP)
336 NetCopyIP(&NetOurIP
, &bd
->bi_ip_addr
);
337 NetOurGatewayIP
= getenv_IPaddr ("gatewayip");
338 NetOurSubnetMask
= getenv_IPaddr ("netmask");
339 NetOurVLAN
= getenv_VLAN("vlan");
340 NetOurNativeVLAN
= getenv_VLAN("nvlan");
343 #if (CONFIG_COMMANDS & CFG_CMD_NFS)
348 NetServerIP
= getenv_IPaddr ("serverip");
350 #if (CONFIG_COMMANDS & CFG_CMD_PING)
355 #if (CONFIG_COMMANDS & CFG_CMD_SNTP)
368 * initialize our IP addr to 0 in order to accept ANY
369 * IP addr assigned to us by the BOOTP / RARP server
372 NetServerIP
= getenv_IPaddr ("serverip");
373 NetOurVLAN
= getenv_VLAN("vlan"); /* VLANs must be read */
374 NetOurNativeVLAN
= getenv_VLAN("nvlan");
376 NetOurVLAN
= getenv_VLAN("vlan"); /* VLANs must be read */
377 NetOurNativeVLAN
= getenv_VLAN("nvlan");
383 switch (net_check_prereq (protocol
)) {
385 /* network not configured */
388 #ifdef CONFIG_NET_MULTI
390 /* network device not configured */
392 #endif /* CONFIG_NET_MULTI */
395 #ifdef CONFIG_NET_MULTI
400 /* always use ARP to get server ethernet address */
404 #if (CONFIG_COMMANDS & CFG_CMD_DHCP)
406 /* Start with a clean slate... */
409 NetServerIP
= getenv_IPaddr ("serverip");
410 DhcpRequest(); /* Basically same as BOOTP */
412 #endif /* CFG_CMD_DHCP */
423 #if (CONFIG_COMMANDS & CFG_CMD_PING)
428 #if (CONFIG_COMMANDS & CFG_CMD_NFS)
433 #if (CONFIG_COMMANDS & CFG_CMD_CDP)
438 #ifdef CONFIG_NETCONSOLE
443 #if (CONFIG_COMMANDS & CFG_CMD_SNTP)
452 NetBootFileXferSize
= 0;
456 #if defined(CONFIG_MII) || (CONFIG_COMMANDS & CFG_CMD_MII)
457 #if defined(CFG_FAULT_ECHO_LINK_DOWN) && defined(CONFIG_STATUS_LED) && defined(STATUS_LED_RED)
459 * Echo the inverted link state to the fault LED.
461 if(miiphy_link(CFG_FAULT_MII_ADDR
)) {
462 status_led_set (STATUS_LED_RED
, STATUS_LED_OFF
);
464 status_led_set (STATUS_LED_RED
, STATUS_LED_ON
);
466 #endif /* CFG_FAULT_ECHO_LINK_DOWN, ... */
467 #endif /* CONFIG_MII, ... */
470 * Main packet reception loop. Loop receiving packets until
471 * someone sets `NetQuit'.
475 #ifdef CONFIG_SHOW_ACTIVITY
477 extern void show_activity(int arg
);
482 * Check the ethernet for a new packet. The ethernet
483 * receive routine will process it.
488 * Abort if ctrl-c was pressed.
499 * Check for a timeout, and run the timeout handler
502 if (timeHandler
&& ((get_timer(0) - timeStart
) > timeDelta
)) {
505 #if defined(CONFIG_MII) || (CONFIG_COMMANDS & CFG_CMD_MII)
506 #if defined(CFG_FAULT_ECHO_LINK_DOWN) && defined(CONFIG_STATUS_LED) && defined(STATUS_LED_RED)
508 * Echo the inverted link state to the fault LED.
510 if(miiphy_link(CFG_FAULT_MII_ADDR
)) {
511 status_led_set (STATUS_LED_RED
, STATUS_LED_OFF
);
513 status_led_set (STATUS_LED_RED
, STATUS_LED_ON
);
515 #endif /* CFG_FAULT_ECHO_LINK_DOWN, ... */
516 #endif /* CONFIG_MII, ... */
518 timeHandler
= (thand_f
*)0;
525 case NETLOOP_RESTART
:
526 #ifdef CONFIG_NET_MULTI
531 case NETLOOP_SUCCESS
:
532 if (NetBootFileXferSize
> 0) {
534 printf("Bytes transferred = %ld (%lx hex)\n",
536 NetBootFileXferSize
);
537 sprintf(buf
, "%lx", NetBootFileXferSize
);
538 setenv("filesize", buf
);
540 sprintf(buf
, "%lX", (unsigned long)load_addr
);
541 setenv("fileaddr", buf
);
544 return NetBootFileXferSize
;
552 /**********************************************************************/
555 startAgainTimeout(void)
557 NetState
= NETLOOP_RESTART
;
561 startAgainHandler(uchar
* pkt
, unsigned dest
, unsigned src
, unsigned len
)
563 /* Totally ignore the packet */
566 void NetStartAgain (void)
568 #ifdef CONFIG_NET_MULTI
569 DECLARE_GLOBAL_DATA_PTR
;
572 int noretry
= 0, once
= 0;
574 if ((nretry
= getenv ("netretry")) != NULL
) {
575 noretry
= (strcmp (nretry
, "no") == 0);
576 once
= (strcmp (nretry
, "once") == 0);
580 NetState
= NETLOOP_FAIL
;
583 #ifndef CONFIG_NET_MULTI
584 NetSetTimeout (10 * CFG_HZ
, startAgainTimeout
);
585 NetSetHandler (startAgainHandler
);
586 #else /* !CONFIG_NET_MULTI*/
588 eth_try_another (!NetRestarted
);
590 if (NetRestartWrap
) {
592 if (NetDevExists
&& !once
) {
593 NetSetTimeout (10 * CFG_HZ
, startAgainTimeout
);
594 NetSetHandler (startAgainHandler
);
596 NetState
= NETLOOP_FAIL
;
599 NetState
= NETLOOP_RESTART
;
601 #endif /* CONFIG_NET_MULTI */
604 /**********************************************************************/
610 NetSetHandler(rxhand_f
* f
)
617 NetSetTimeout(ulong iv
, thand_f
* f
)
620 timeHandler
= (thand_f
*)0;
623 timeStart
= get_timer(0);
630 NetSendPacket(volatile uchar
* pkt
, int len
)
632 (void) eth_send(pkt
, len
);
636 NetSendUDPPacket(uchar
*ether
, IPaddr_t dest
, int dport
, int sport
, int len
)
640 /* convert to new style broadcast */
644 /* if broadcast, make the ether address a broadcast and don't do ARP */
645 if (dest
== 0xFFFFFFFF)
646 ether
= NetBcastAddr
;
648 /* if MAC address was not discovered yet, save the packet and do an ARP request */
649 if (memcmp(ether
, NetEtherNullAddr
, 6) == 0) {
652 printf("sending ARP for %08lx\n", dest
);
654 NetArpWaitPacketIP
= dest
;
655 NetArpWaitPacketMAC
= ether
;
657 pkt
= NetArpWaitTxPacket
;
658 pkt
+= NetSetEther (pkt
, NetArpWaitPacketMAC
, PROT_IP
);
660 NetSetIP (pkt
, dest
, dport
, sport
, len
);
661 memcpy(pkt
+ IP_HDR_SIZE
, (uchar
*)NetTxPacket
+ (pkt
- (uchar
*)NetArpWaitTxPacket
) + IP_HDR_SIZE
, len
);
663 /* size of the waiting packet */
664 NetArpWaitTxPacketSize
= (pkt
- NetArpWaitTxPacket
) + IP_HDR_SIZE
+ len
;
666 /* and do the ARP request */
668 NetArpWaitTimerStart
= get_timer(0);
670 return 1; /* waiting */
674 printf("sending UDP to %08lx/%02x:%02x:%02x:%02x:%02x:%02x\n",
675 dest
, ether
[0], ether
[1], ether
[2], ether
[3], ether
[4], ether
[5]);
678 pkt
= (uchar
*)NetTxPacket
;
679 pkt
+= NetSetEther (pkt
, ether
, PROT_IP
);
680 NetSetIP (pkt
, dest
, dport
, sport
, len
);
681 (void) eth_send(NetTxPacket
, (pkt
- NetTxPacket
) + IP_HDR_SIZE
+ len
);
683 return 0; /* transmitted */
686 #if (CONFIG_COMMANDS & CFG_CMD_PING)
687 static ushort PingSeqNo
;
696 /* XXX always send arp request */
698 memcpy(mac
, NetEtherNullAddr
, 6);
701 printf("sending ARP for %08lx\n", NetPingIP
);
704 NetArpWaitPacketIP
= NetPingIP
;
705 NetArpWaitPacketMAC
= mac
;
707 pkt
= NetArpWaitTxPacket
;
708 pkt
+= NetSetEther(pkt
, mac
, PROT_IP
);
710 ip
= (volatile IP_t
*)pkt
;
713 * Construct an IP and ICMP header. (need to set no fragment bit - XXX)
715 ip
->ip_hl_v
= 0x45; /* IP_HDR_SIZE / 4 (not including UDP) */
717 ip
->ip_len
= htons(IP_HDR_SIZE_NO_UDP
+ 8);
718 ip
->ip_id
= htons(NetIPID
++);
719 ip
->ip_off
= htons(0x4000); /* No fragmentation */
721 ip
->ip_p
= 0x01; /* ICMP */
723 NetCopyIP((void*)&ip
->ip_src
, &NetOurIP
); /* already in network byte order */
724 NetCopyIP((void*)&ip
->ip_dst
, &NetPingIP
); /* - "" - */
725 ip
->ip_sum
= ~NetCksum((uchar
*)ip
, IP_HDR_SIZE_NO_UDP
/ 2);
727 s
= &ip
->udp_src
; /* XXX ICMP starts here */
728 s
[0] = htons(0x0800); /* echo-request, code */
729 s
[1] = 0; /* checksum */
730 s
[2] = 0; /* identifier */
731 s
[3] = htons(PingSeqNo
++); /* sequence number */
732 s
[1] = ~NetCksum((uchar
*)s
, 8/2);
734 /* size of the waiting packet */
735 NetArpWaitTxPacketSize
= (pkt
- NetArpWaitTxPacket
) + IP_HDR_SIZE_NO_UDP
+ 8;
737 /* and do the ARP request */
739 NetArpWaitTimerStart
= get_timer(0);
741 return 1; /* waiting */
748 NetState
= NETLOOP_FAIL
; /* we did not get the reply */
752 PingHandler (uchar
* pkt
, unsigned dest
, unsigned src
, unsigned len
)
755 volatile IP_t
*ip
= (volatile IP_t
*)pkt
;
757 tmp
= NetReadIP((void *)&ip
->ip_src
);
758 if (tmp
!= NetPingIP
)
761 NetState
= NETLOOP_SUCCESS
;
764 static void PingStart(void)
766 #if defined(CONFIG_NET_MULTI)
767 printf ("Using %s device\n", eth_get_name());
768 #endif /* CONFIG_NET_MULTI */
769 NetSetTimeout (10 * CFG_HZ
, PingTimeout
);
770 NetSetHandler (PingHandler
);
774 #endif /* CFG_CMD_PING */
776 #if (CONFIG_COMMANDS & CFG_CMD_CDP)
778 #define CDP_DEVICE_ID_TLV 0x0001
779 #define CDP_ADDRESS_TLV 0x0002
780 #define CDP_PORT_ID_TLV 0x0003
781 #define CDP_CAPABILITIES_TLV 0x0004
782 #define CDP_VERSION_TLV 0x0005
783 #define CDP_PLATFORM_TLV 0x0006
784 #define CDP_NATIVE_VLAN_TLV 0x000a
785 #define CDP_APPLIANCE_VLAN_TLV 0x000e
786 #define CDP_TRIGGER_TLV 0x000f
787 #define CDP_POWER_CONSUMPTION_TLV 0x0010
788 #define CDP_SYSNAME_TLV 0x0014
789 #define CDP_SYSOBJECT_TLV 0x0015
790 #define CDP_MANAGEMENT_ADDRESS_TLV 0x0016
792 #define CDP_TIMEOUT (CFG_HZ/4) /* one packet every 250ms */
797 ushort CDPNativeVLAN
;
798 ushort CDPApplianceVLAN
;
800 static const uchar CDP_SNAP_hdr
[8] = { 0xAA, 0xAA, 0x03, 0x00, 0x00, 0x0C, 0x20, 0x00 };
802 static ushort
CDP_compute_csum(const uchar
*buff
, ushort len
)
810 odd
= 1 & (ulong
)buff
;
817 result
+= *((const ushort
*)buff
)++;
818 if (result
& 0x80000000)
819 result
= (result
& 0xFFFF) + (result
>> 16);
823 leftover
= (signed short)(*(const signed char *)buff
);
824 /* * XXX CISCO SUCKS big time! (and blows too) */
825 result
= (result
& 0xffff0000) | ((result
+ leftover
) & 0x0000ffff);
828 result
= (result
& 0xFFFF) + (result
>> 16);
831 result
= ((result
>> 8) & 0xff) | ((result
& 0xff) << 8);
834 /* add up 16-bit and 17-bit words for 17+c bits */
835 result
= (result
& 0xffff) + (result
>> 16);
836 /* add up 16-bit and 2-bit for 16+c bit */
837 result
= (result
& 0xffff) + (result
>> 16);
839 result
= (result
& 0xffff) + (result
>> 16);
842 csum
= ~(ushort
)result
;
844 /* run time endian detection */
845 if (csum
!= htons(csum
)) /* little endian */
851 int CDPSendTrigger(void)
859 #if defined(CONFIG_CDP_DEVICE_ID) || defined(CONFIG_CDP_PORT_ID) || \
860 defined(CONFIG_CDP_VERSION) || defined(CONFIG_CDP_PLATFORM)
865 et
= (Ethernet_t
*)pkt
;
867 /* NOTE: trigger sent not on any VLAN */
869 /* form ethernet header */
870 memcpy(et
->et_dest
, NetCDPAddr
, 6);
871 memcpy(et
->et_src
, NetOurEther
, 6);
873 pkt
+= ETHER_HDR_SIZE
;
876 memcpy((uchar
*)pkt
, CDP_SNAP_hdr
, sizeof(CDP_SNAP_hdr
));
877 pkt
+= sizeof(CDP_SNAP_hdr
);
880 *pkt
++ = 0x02; /* CDP version 2 */
881 *pkt
++ = 180; /* TTL */
882 s
= (volatile ushort
*)pkt
;
884 *s
++ = htons(0); /* checksum (0 for later calculation) */
887 #ifdef CONFIG_CDP_DEVICE_ID
888 *s
++ = htons(CDP_DEVICE_ID_TLV
);
889 *s
++ = htons(CONFIG_CDP_DEVICE_ID
);
890 memset(buf
, 0, sizeof(buf
));
891 sprintf(buf
, CONFIG_CDP_DEVICE_ID_PREFIX
"%02X%02X%02X%02X%02X%02X",
892 NetOurEther
[0] & 0xff, NetOurEther
[1] & 0xff,
893 NetOurEther
[2] & 0xff, NetOurEther
[3] & 0xff,
894 NetOurEther
[4] & 0xff, NetOurEther
[5] & 0xff);
895 memcpy((uchar
*)s
, buf
, 16);
899 #ifdef CONFIG_CDP_PORT_ID
900 *s
++ = htons(CDP_PORT_ID_TLV
);
901 memset(buf
, 0, sizeof(buf
));
902 sprintf(buf
, CONFIG_CDP_PORT_ID
, eth_get_dev_index());
904 if (len
& 1) /* make it even */
906 *s
++ = htons(len
+ 4);
907 memcpy((uchar
*)s
, buf
, len
);
911 #ifdef CONFIG_CDP_CAPABILITIES
912 *s
++ = htons(CDP_CAPABILITIES_TLV
);
914 *(ulong
*)s
= htonl(CONFIG_CDP_CAPABILITIES
);
918 #ifdef CONFIG_CDP_VERSION
919 *s
++ = htons(CDP_VERSION_TLV
);
920 memset(buf
, 0, sizeof(buf
));
921 strcpy(buf
, CONFIG_CDP_VERSION
);
923 if (len
& 1) /* make it even */
925 *s
++ = htons(len
+ 4);
926 memcpy((uchar
*)s
, buf
, len
);
930 #ifdef CONFIG_CDP_PLATFORM
931 *s
++ = htons(CDP_PLATFORM_TLV
);
932 memset(buf
, 0, sizeof(buf
));
933 strcpy(buf
, CONFIG_CDP_PLATFORM
);
935 if (len
& 1) /* make it even */
937 *s
++ = htons(len
+ 4);
938 memcpy((uchar
*)s
, buf
, len
);
942 #ifdef CONFIG_CDP_TRIGGER
943 *s
++ = htons(CDP_TRIGGER_TLV
);
945 *(ulong
*)s
= htonl(CONFIG_CDP_TRIGGER
);
949 #ifdef CONFIG_CDP_POWER_CONSUMPTION
950 *s
++ = htons(CDP_POWER_CONSUMPTION_TLV
);
952 *s
++ = htons(CONFIG_CDP_POWER_CONSUMPTION
);
955 /* length of ethernet packet */
956 len
= (uchar
*)s
- ((uchar
*)NetTxPacket
+ ETHER_HDR_SIZE
);
957 et
->et_protlen
= htons(len
);
959 len
= ETHER_HDR_SIZE
+ sizeof(CDP_SNAP_hdr
);
960 chksum
= CDP_compute_csum((uchar
*)NetTxPacket
+ len
, (uchar
*)s
- (NetTxPacket
+ len
));
965 (void) eth_send(NetTxPacket
, (uchar
*)s
- NetTxPacket
);
975 NetSetTimeout (CDP_TIMEOUT
, CDPTimeout
);
980 /* if not OK try again */
984 NetState
= NETLOOP_SUCCESS
;
988 CDPDummyHandler (uchar
* pkt
, unsigned dest
, unsigned src
, unsigned len
)
994 CDPHandler(const uchar
* pkt
, unsigned len
)
1003 if (len
< sizeof(CDP_SNAP_hdr
) + 4)
1006 /* check for valid CDP SNAP header */
1007 if (memcmp(pkt
, CDP_SNAP_hdr
, sizeof(CDP_SNAP_hdr
)) != 0)
1010 pkt
+= sizeof(CDP_SNAP_hdr
);
1011 len
-= sizeof(CDP_SNAP_hdr
);
1013 /* Version of CDP protocol must be >= 2 and TTL != 0 */
1014 if (pkt
[0] < 0x02 || pkt
[1] == 0)
1017 /* if version is greater than 0x02 maybe we'll have a problem; output a warning */
1019 printf("** WARNING: CDP packet received with a protocol version %d > 2\n",
1022 if (CDP_compute_csum(pkt
, len
) != 0)
1034 ss
= (const ushort
*)pkt
;
1035 type
= ntohs(ss
[0]);
1036 tlen
= ntohs(ss
[1]);
1044 ss
+= 2; /* point ss to the data of the TLV */
1048 case CDP_DEVICE_ID_TLV
:
1050 case CDP_ADDRESS_TLV
:
1052 case CDP_PORT_ID_TLV
:
1054 case CDP_CAPABILITIES_TLV
:
1056 case CDP_VERSION_TLV
:
1058 case CDP_PLATFORM_TLV
:
1060 case CDP_NATIVE_VLAN_TLV
:
1063 case CDP_APPLIANCE_VLAN_TLV
:
1064 t
= (const uchar
*)ss
;
1070 ss
= (const ushort
*)(t
+ 1);
1072 #ifdef CONFIG_CDP_APPLIANCE_VLAN_TYPE
1073 if (applid
== CONFIG_CDP_APPLIANCE_VLAN_TYPE
)
1076 vlan
= ntohs(*ss
); /* XXX will this work; dunno */
1081 case CDP_TRIGGER_TLV
:
1083 case CDP_POWER_CONSUMPTION_TLV
:
1085 case CDP_SYSNAME_TLV
:
1087 case CDP_SYSOBJECT_TLV
:
1089 case CDP_MANAGEMENT_ADDRESS_TLV
:
1094 CDPApplianceVLAN
= vlan
;
1095 CDPNativeVLAN
= nvlan
;
1101 printf("** CDP packet is too short\n");
1105 static void CDPStart(void)
1107 #if defined(CONFIG_NET_MULTI)
1108 printf ("Using %s device\n", eth_get_name());
1113 CDPNativeVLAN
= htons(-1);
1114 CDPApplianceVLAN
= htons(-1);
1116 NetSetTimeout (CDP_TIMEOUT
, CDPTimeout
);
1117 NetSetHandler (CDPDummyHandler
);
1121 #endif /* CFG_CMD_CDP */
1125 NetReceive(volatile uchar
* inpkt
, int len
)
1133 #if (CONFIG_COMMANDS & CFG_CMD_CDP)
1136 ushort cti
= 0, vlanid
= VLAN_NONE
, myvlanid
, mynvlanid
;
1139 printf("packet received\n");
1144 et
= (Ethernet_t
*)inpkt
;
1146 /* too small packet? */
1147 if (len
< ETHER_HDR_SIZE
)
1150 #if (CONFIG_COMMANDS & CFG_CMD_CDP)
1151 /* keep track if packet is CDP */
1152 iscdp
= memcmp(et
->et_dest
, NetCDPAddr
, 6) == 0;
1155 myvlanid
= ntohs(NetOurVLAN
);
1156 if (myvlanid
== (ushort
)-1)
1157 myvlanid
= VLAN_NONE
;
1158 mynvlanid
= ntohs(NetOurNativeVLAN
);
1159 if (mynvlanid
== (ushort
)-1)
1160 mynvlanid
= VLAN_NONE
;
1162 x
= ntohs(et
->et_protlen
);
1165 printf("packet received\n");
1170 * Got a 802 packet. Check the other protocol field.
1172 x
= ntohs(et
->et_prot
);
1174 ip
= (IP_t
*)(inpkt
+ E802_HDR_SIZE
);
1175 len
-= E802_HDR_SIZE
;
1177 } else if (x
!= PROT_VLAN
) { /* normal packet */
1178 ip
= (IP_t
*)(inpkt
+ ETHER_HDR_SIZE
);
1179 len
-= ETHER_HDR_SIZE
;
1181 } else { /* VLAN packet */
1182 VLAN_Ethernet_t
*vet
= (VLAN_Ethernet_t
*)et
;
1185 printf("VLAN packet received\n");
1187 /* too small packet? */
1188 if (len
< VLAN_ETHER_HDR_SIZE
)
1191 /* if no VLAN active */
1192 if ((ntohs(NetOurVLAN
) & VLAN_IDMASK
) == VLAN_NONE
1193 #if (CONFIG_COMMANDS & CFG_CMD_CDP)
1199 cti
= ntohs(vet
->vet_tag
);
1200 vlanid
= cti
& VLAN_IDMASK
;
1201 x
= ntohs(vet
->vet_type
);
1203 ip
= (IP_t
*)(inpkt
+ VLAN_ETHER_HDR_SIZE
);
1204 len
-= VLAN_ETHER_HDR_SIZE
;
1208 printf("Receive from protocol 0x%x\n", x
);
1211 #if (CONFIG_COMMANDS & CFG_CMD_CDP)
1213 CDPHandler((uchar
*)ip
, len
);
1218 if ((myvlanid
& VLAN_IDMASK
) != VLAN_NONE
) {
1219 if (vlanid
== VLAN_NONE
)
1220 vlanid
= (mynvlanid
& VLAN_IDMASK
);
1222 if (vlanid
!= (myvlanid
& VLAN_IDMASK
))
1230 * We have to deal with two types of ARP packets:
1231 * - REQUEST packets will be answered by sending our
1232 * IP address - if we know it.
1233 * - REPLY packates are expected only after we asked
1234 * for the TFTP server's or the gateway's ethernet
1235 * address; so if we receive such a packet, we set
1236 * the server ethernet address
1242 if (len
< ARP_HDR_SIZE
) {
1243 printf("bad length %d < %d\n", len
, ARP_HDR_SIZE
);
1246 if (ntohs(arp
->ar_hrd
) != ARP_ETHER
) {
1249 if (ntohs(arp
->ar_pro
) != PROT_IP
) {
1252 if (arp
->ar_hln
!= 6) {
1255 if (arp
->ar_pln
!= 4) {
1259 if (NetOurIP
== 0) {
1263 if (NetReadIP(&arp
->ar_data
[16]) != NetOurIP
) {
1267 switch (ntohs(arp
->ar_op
)) {
1268 case ARPOP_REQUEST
: /* reply with our IP address */
1270 puts ("Got ARP REQUEST, return our IP\n");
1273 pkt
+= NetSetEther(pkt
, et
->et_src
, PROT_ARP
);
1274 arp
->ar_op
= htons(ARPOP_REPLY
);
1275 memcpy (&arp
->ar_data
[10], &arp
->ar_data
[0], 6);
1276 NetCopyIP(&arp
->ar_data
[16], &arp
->ar_data
[6]);
1277 memcpy (&arp
->ar_data
[ 0], NetOurEther
, 6);
1278 NetCopyIP(&arp
->ar_data
[ 6], &NetOurIP
);
1279 (void) eth_send((uchar
*)et
, (pkt
- (uchar
*)et
) + ARP_HDR_SIZE
);
1282 case ARPOP_REPLY
: /* arp reply */
1283 /* are we waiting for a reply */
1284 if (!NetArpWaitPacketIP
|| !NetArpWaitPacketMAC
)
1287 printf("Got ARP REPLY, set server/gtwy eth addr (%02x:%02x:%02x:%02x:%02x:%02x)\n",
1288 arp
->ar_data
[0], arp
->ar_data
[1],
1289 arp
->ar_data
[2], arp
->ar_data
[3],
1290 arp
->ar_data
[4], arp
->ar_data
[5]);
1293 tmp
= NetReadIP(&arp
->ar_data
[6]);
1295 /* matched waiting packet's address */
1296 if (tmp
== NetArpWaitReplyIP
) {
1300 /* save address for later use */
1301 memcpy(NetArpWaitPacketMAC
, &arp
->ar_data
[0], 6);
1303 #ifdef CONFIG_NETCONSOLE
1304 (*packetHandler
)(0,0,0,0);
1306 /* modify header, and transmit it */
1307 memcpy(((Ethernet_t
*)NetArpWaitTxPacket
)->et_dest
, NetArpWaitPacketMAC
, 6);
1308 (void) eth_send(NetArpWaitTxPacket
, NetArpWaitTxPacketSize
);
1310 /* no arp request pending now */
1311 NetArpWaitPacketIP
= 0;
1312 NetArpWaitTxPacketSize
= 0;
1313 NetArpWaitPacketMAC
= NULL
;
1319 printf("Unexpected ARP opcode 0x%x\n", ntohs(arp
->ar_op
));
1327 puts ("Got RARP\n");
1330 if (len
< ARP_HDR_SIZE
) {
1331 printf("bad length %d < %d\n", len
, ARP_HDR_SIZE
);
1335 if ((ntohs(arp
->ar_op
) != RARPOP_REPLY
) ||
1336 (ntohs(arp
->ar_hrd
) != ARP_ETHER
) ||
1337 (ntohs(arp
->ar_pro
) != PROT_IP
) ||
1338 (arp
->ar_hln
!= 6) || (arp
->ar_pln
!= 4)) {
1340 puts ("invalid RARP header\n");
1342 NetCopyIP(&NetOurIP
, &arp
->ar_data
[16]);
1343 if (NetServerIP
== 0)
1344 NetCopyIP(&NetServerIP
, &arp
->ar_data
[ 6]);
1345 memcpy (NetServerEther
, &arp
->ar_data
[ 0], 6);
1347 (*packetHandler
)(0,0,0,0);
1355 if (len
< IP_HDR_SIZE
) {
1356 debug ("len bad %d < %d\n", len
, IP_HDR_SIZE
);
1359 if (len
< ntohs(ip
->ip_len
)) {
1360 printf("len bad %d < %d\n", len
, ntohs(ip
->ip_len
));
1363 len
= ntohs(ip
->ip_len
);
1365 printf("len=%d, v=%02x\n", len
, ip
->ip_hl_v
& 0xff);
1367 if ((ip
->ip_hl_v
& 0xf0) != 0x40) {
1370 if (ip
->ip_off
& htons(0x1fff)) { /* Can't deal w/ fragments */
1373 if (!NetCksumOk((uchar
*)ip
, IP_HDR_SIZE_NO_UDP
/ 2)) {
1374 puts ("checksum bad\n");
1377 tmp
= NetReadIP(&ip
->ip_dst
);
1378 if (NetOurIP
&& tmp
!= NetOurIP
&& tmp
!= 0xFFFFFFFF) {
1382 * watch for ICMP host redirects
1384 * There is no real handler code (yet). We just watch
1385 * for ICMP host redirect messages. In case anybody
1386 * sees these messages: please contact me
1387 * (wd@denx.de), or - even better - send me the
1388 * necessary fixes :-)
1390 * Note: in all cases where I have seen this so far
1391 * it was a problem with the router configuration,
1392 * for instance when a router was configured in the
1393 * BOOTP reply, but the TFTP server was on the same
1394 * subnet. So this is probably a warning that your
1395 * configuration might be wrong. But I'm not really
1396 * sure if there aren't any other situations.
1398 if (ip
->ip_p
== IPPROTO_ICMP
) {
1399 ICMP_t
*icmph
= (ICMP_t
*)&(ip
->udp_src
);
1401 switch (icmph
->type
) {
1403 if (icmph
->code
!= ICMP_REDIR_HOST
)
1405 puts (" ICMP Host Redirect to ");
1406 print_IPaddr(icmph
->un
.gateway
);
1409 #if (CONFIG_COMMANDS & CFG_CMD_PING)
1410 case ICMP_ECHO_REPLY
:
1412 * IP header OK. Pass the packet to the current handler.
1414 /* XXX point to ip packet */
1415 (*packetHandler
)((uchar
*)ip
, 0, 0, 0);
1421 } else if (ip
->ip_p
!= IPPROTO_UDP
) { /* Only UDP packets */
1425 #ifdef CONFIG_NETCONSOLE
1426 nc_input_packet((uchar
*)ip
+IP_HDR_SIZE
,
1429 ntohs(ip
->udp_len
) - 8);
1432 * IP header OK. Pass the packet to the current handler.
1434 (*packetHandler
)((uchar
*)ip
+IP_HDR_SIZE
,
1437 ntohs(ip
->udp_len
) - 8);
1443 /**********************************************************************/
1445 static int net_check_prereq (proto_t protocol
)
1449 #if (CONFIG_COMMANDS & CFG_CMD_PING)
1451 if (NetPingIP
== 0) {
1452 puts ("*** ERROR: ping address not given\n");
1457 #if (CONFIG_COMMANDS & CFG_CMD_SNTP)
1459 if (NetNtpServerIP
== 0) {
1460 puts ("*** ERROR: NTP server address not given\n");
1465 #if (CONFIG_COMMANDS & CFG_CMD_NFS)
1470 if (NetServerIP
== 0) {
1471 puts ("*** ERROR: `serverip' not set\n");
1474 #if (CONFIG_COMMANDS & (CFG_CMD_PING | CFG_CMD_SNTP))
1478 if (NetOurIP
== 0) {
1479 puts ("*** ERROR: `ipaddr' not set\n");
1488 if (memcmp (NetOurEther
, "\0\0\0\0\0\0", 6) == 0) {
1489 #ifdef CONFIG_NET_MULTI
1490 extern int eth_get_dev_index (void);
1491 int num
= eth_get_dev_index ();
1495 puts ("*** ERROR: No ethernet found.\n");
1498 puts ("*** ERROR: `ethaddr' not set\n");
1501 printf ("*** ERROR: `eth%daddr' not set\n",
1509 puts ("*** ERROR: `ethaddr' not set\n");
1517 return (0); /* OK */
1519 /**********************************************************************/
1522 NetCksumOk(uchar
* ptr
, int len
)
1524 return !((NetCksum(ptr
, len
) + 1) & 0xfffe);
1529 NetCksum(uchar
* ptr
, int len
)
1535 xsum
+= *((ushort
*)ptr
)++;
1536 xsum
= (xsum
& 0xffff) + (xsum
>> 16);
1537 xsum
= (xsum
& 0xffff) + (xsum
>> 16);
1538 return (xsum
& 0xffff);
1546 myvlanid
= ntohs(NetOurVLAN
);
1547 if (myvlanid
== (ushort
)-1)
1548 myvlanid
= VLAN_NONE
;
1550 return ((myvlanid
& VLAN_IDMASK
) == VLAN_NONE
) ? ETHER_HDR_SIZE
: VLAN_ETHER_HDR_SIZE
;
1554 NetSetEther(volatile uchar
* xet
, uchar
* addr
, uint prot
)
1556 Ethernet_t
*et
= (Ethernet_t
*)xet
;
1559 myvlanid
= ntohs(NetOurVLAN
);
1560 if (myvlanid
== (ushort
)-1)
1561 myvlanid
= VLAN_NONE
;
1563 memcpy (et
->et_dest
, addr
, 6);
1564 memcpy (et
->et_src
, NetOurEther
, 6);
1565 if ((myvlanid
& VLAN_IDMASK
) == VLAN_NONE
) {
1566 et
->et_protlen
= htons(prot
);
1567 return ETHER_HDR_SIZE
;
1569 VLAN_Ethernet_t
*vet
= (VLAN_Ethernet_t
*)xet
;
1571 vet
->vet_vlan_type
= htons(PROT_VLAN
);
1572 vet
->vet_tag
= htons((0 << 5) | (myvlanid
& VLAN_IDMASK
));
1573 vet
->vet_type
= htons(prot
);
1574 return VLAN_ETHER_HDR_SIZE
;
1579 NetSetIP(volatile uchar
* xip
, IPaddr_t dest
, int dport
, int sport
, int len
)
1581 volatile IP_t
*ip
= (IP_t
*)xip
;
1584 * If the data is an odd number of bytes, zero the
1585 * byte after the last byte so that the checksum
1589 xip
[IP_HDR_SIZE
+ len
] = 0;
1592 * Construct an IP and UDP header.
1593 * (need to set no fragment bit - XXX)
1595 ip
->ip_hl_v
= 0x45; /* IP_HDR_SIZE / 4 (not including UDP) */
1597 ip
->ip_len
= htons(IP_HDR_SIZE
+ len
);
1598 ip
->ip_id
= htons(NetIPID
++);
1599 ip
->ip_off
= htons(0x4000); /* No fragmentation */
1601 ip
->ip_p
= 17; /* UDP */
1603 NetCopyIP((void*)&ip
->ip_src
, &NetOurIP
); /* already in network byte order */
1604 NetCopyIP((void*)&ip
->ip_dst
, &dest
); /* - "" - */
1605 ip
->udp_src
= htons(sport
);
1606 ip
->udp_dst
= htons(dport
);
1607 ip
->udp_len
= htons(8 + len
);
1609 ip
->ip_sum
= ~NetCksum((uchar
*)ip
, IP_HDR_SIZE_NO_UDP
/ 2);
1612 void copy_filename (uchar
*dst
, uchar
*src
, int size
)
1614 if (*src
&& (*src
== '"')) {
1619 while ((--size
> 0) && *src
&& (*src
!= '"')) {
1625 #endif /* CFG_CMD_NET */
1627 void ip_to_string (IPaddr_t x
, char *s
)
1630 sprintf (s
, "%d.%d.%d.%d",
1631 (int) ((x
>> 24) & 0xff),
1632 (int) ((x
>> 16) & 0xff),
1633 (int) ((x
>> 8) & 0xff), (int) ((x
>> 0) & 0xff)
1637 IPaddr_t
string_to_ip(char *s
)
1646 for (addr
=0, i
=0; i
<4; ++i
) {
1647 ulong val
= s
? simple_strtoul(s
, &e
, 10) : 0;
1649 addr
|= (val
& 0xFF);
1655 return (htonl(addr
));
1658 void VLAN_to_string(ushort x
, char *s
)
1662 if (x
== (ushort
)-1)
1668 sprintf(s
, "%d", x
& VLAN_IDMASK
);
1671 ushort
string_to_VLAN(char *s
)
1676 return htons(VLAN_NONE
);
1678 if (*s
< '0' || *s
> '9')
1681 id
= (ushort
)simple_strtoul(s
, NULL
, 10);
1686 void print_IPaddr (IPaddr_t x
)
1690 ip_to_string (x
, tmp
);
1695 IPaddr_t
getenv_IPaddr (char *var
)
1697 return (string_to_ip(getenv(var
)));
1700 ushort
getenv_VLAN(char *var
)
1702 return (string_to_VLAN(getenv(var
)));