wdenk | 2d96695 | 2002-10-31 22:12:35 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copied from Linux Monitor (LiMon) - Networking. |
| 3 | * |
| 4 | * Copyright 1994 - 2000 Neil Russell. |
| 5 | * (See License) |
| 6 | * Copyright 2000 Roland Borde |
| 7 | * Copyright 2000 Paolo Scaffardi |
| 8 | * Copyright 2000-2002 Wolfgang Denk, wd@denx.de |
| 9 | */ |
| 10 | |
| 11 | /* |
| 12 | * General Desription: |
| 13 | * |
| 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: |
| 17 | * |
| 18 | * BOOTP: |
| 19 | * |
| 20 | * Prerequisites: - own ethernet address |
| 21 | * We want: - own IP address |
| 22 | * - TFTP server IP address |
| 23 | * - name of bootfile |
| 24 | * Next step: ARP |
| 25 | * |
| 26 | * RARP: |
| 27 | * |
| 28 | * Prerequisites: - own ethernet address |
| 29 | * We want: - own IP address |
| 30 | * - TFTP server IP address |
| 31 | * Next step: ARP |
| 32 | * |
| 33 | * ARP: |
| 34 | * |
| 35 | * Prerequisites: - own ethernet address |
| 36 | * - own IP address |
| 37 | * - TFTP server IP address |
| 38 | * We want: - TFTP server ethernet address |
| 39 | * Next step: TFTP |
| 40 | * |
| 41 | * DHCP: |
| 42 | * |
Wolfgang Denk | b2f5080 | 2005-08-12 23:43:12 +0200 | [diff] [blame] | 43 | * Prerequisites: - own ethernet address |
| 44 | * We want: - IP, Netmask, ServerIP, Gateway IP |
| 45 | * - bootfilename, lease time |
| 46 | * Next step: - TFTP |
wdenk | 2d96695 | 2002-10-31 22:12:35 +0000 | [diff] [blame] | 47 | * |
| 48 | * TFTP: |
| 49 | * |
| 50 | * Prerequisites: - own ethernet address |
| 51 | * - own IP 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 |
| 57 | * Next step: none |
wdenk | cbd8a35 | 2004-02-24 02:00:03 +0000 | [diff] [blame] | 58 | * |
| 59 | * NFS: |
| 60 | * |
| 61 | * Prerequisites: - own ethernet address |
| 62 | * - own IP 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 |
| 66 | * Next step: none |
wdenk | ea287de | 2005-04-01 00:25:43 +0000 | [diff] [blame] | 67 | * |
| 68 | * SNTP: |
| 69 | * |
Wolfgang Denk | b2f5080 | 2005-08-12 23:43:12 +0200 | [diff] [blame] | 70 | * Prerequisites: - own ethernet address |
wdenk | ea287de | 2005-04-01 00:25:43 +0000 | [diff] [blame] | 71 | * - own IP address |
| 72 | * We want: - network time |
| 73 | * Next step: none |
wdenk | 2d96695 | 2002-10-31 22:12:35 +0000 | [diff] [blame] | 74 | */ |
| 75 | |
| 76 | |
| 77 | #include <common.h> |
| 78 | #include <watchdog.h> |
| 79 | #include <command.h> |
| 80 | #include <net.h> |
| 81 | #include "bootp.h" |
| 82 | #include "tftp.h" |
| 83 | #include "rarp.h" |
wdenk | cbd8a35 | 2004-02-24 02:00:03 +0000 | [diff] [blame] | 84 | #include "nfs.h" |
wdenk | fc3e216 | 2003-10-08 22:33:00 +0000 | [diff] [blame] | 85 | #ifdef CONFIG_STATUS_LED |
| 86 | #include <status_led.h> |
| 87 | #include <miiphy.h> |
| 88 | #endif |
Jon Loeliger | 643d1ab | 2007-07-09 17:45:14 -0500 | [diff] [blame] | 89 | #if defined(CONFIG_CMD_SNTP) |
wdenk | ea287de | 2005-04-01 00:25:43 +0000 | [diff] [blame] | 90 | #include "sntp.h" |
| 91 | #endif |
wdenk | 2d96695 | 2002-10-31 22:12:35 +0000 | [diff] [blame] | 92 | |
Jon Loeliger | 643d1ab | 2007-07-09 17:45:14 -0500 | [diff] [blame] | 93 | #if defined(CONFIG_CMD_NET) |
wdenk | 2d96695 | 2002-10-31 22:12:35 +0000 | [diff] [blame] | 94 | |
Wolfgang Denk | d87080b | 2006-03-31 18:32:53 +0200 | [diff] [blame] | 95 | DECLARE_GLOBAL_DATA_PTR; |
| 96 | |
Jean-Christophe PLAGNIOL-VILLARD | 079c2c4 | 2007-11-17 11:31:10 +0100 | [diff] [blame] | 97 | #define ARP_TIMEOUT 5UL /* Seconds before trying ARP again */ |
wdenk | 73a8b27 | 2003-06-05 19:27:42 +0000 | [diff] [blame] | 98 | #ifndef CONFIG_NET_RETRY_COUNT |
| 99 | # define ARP_TIMEOUT_COUNT 5 /* # of timeouts before giving up */ |
| 100 | #else |
| 101 | # define ARP_TIMEOUT_COUNT (CONFIG_NET_RETRY_COUNT) |
| 102 | #endif |
| 103 | |
wdenk | 2d96695 | 2002-10-31 22:12:35 +0000 | [diff] [blame] | 104 | #if 0 |
| 105 | #define ET_DEBUG |
| 106 | #endif |
| 107 | |
| 108 | /** BOOTP EXTENTIONS **/ |
| 109 | |
| 110 | IPaddr_t NetOurSubnetMask=0; /* Our subnet mask (0=unknown) */ |
| 111 | IPaddr_t NetOurGatewayIP=0; /* Our gateways IP address */ |
| 112 | IPaddr_t NetOurDNSIP=0; /* Our DNS IP address */ |
Jon Loeliger | 1fe80d7 | 2007-07-09 22:08:34 -0500 | [diff] [blame] | 113 | #if defined(CONFIG_BOOTP_DNS2) |
stroese | fe389a8 | 2003-08-28 14:17:32 +0000 | [diff] [blame] | 114 | IPaddr_t NetOurDNS2IP=0; /* Our 2nd DNS IP address */ |
| 115 | #endif |
wdenk | 2d96695 | 2002-10-31 22:12:35 +0000 | [diff] [blame] | 116 | char NetOurNISDomain[32]={0,}; /* Our NIS domain */ |
| 117 | char NetOurHostName[32]={0,}; /* Our hostname */ |
| 118 | char NetOurRootPath[64]={0,}; /* Our bootpath */ |
| 119 | ushort NetBootFileSize=0; /* Our bootfile size in blocks */ |
| 120 | |
David Updegraff | 53a5c42 | 2007-06-11 10:41:07 -0500 | [diff] [blame] | 121 | #ifdef CONFIG_MCAST_TFTP /* Multicast TFTP */ |
| 122 | IPaddr_t Mcast_addr; |
| 123 | #endif |
| 124 | |
wdenk | 2d96695 | 2002-10-31 22:12:35 +0000 | [diff] [blame] | 125 | /** END OF BOOTP EXTENTIONS **/ |
| 126 | |
| 127 | ulong NetBootFileXferSize; /* The actual transferred size of the bootfile (in bytes) */ |
| 128 | uchar NetOurEther[6]; /* Our ethernet address */ |
| 129 | uchar NetServerEther[6] = /* Boot server enet address */ |
wdenk | 73a8b27 | 2003-06-05 19:27:42 +0000 | [diff] [blame] | 130 | { 0, 0, 0, 0, 0, 0 }; |
wdenk | 2d96695 | 2002-10-31 22:12:35 +0000 | [diff] [blame] | 131 | IPaddr_t NetOurIP; /* Our IP addr (0 = unknown) */ |
| 132 | IPaddr_t NetServerIP; /* Our IP addr (0 = unknown) */ |
| 133 | volatile uchar *NetRxPkt; /* Current receive packet */ |
| 134 | int NetRxPktLen; /* Current rx packet length */ |
| 135 | unsigned NetIPID; /* IP packet ID */ |
| 136 | uchar NetBcastAddr[6] = /* Ethernet bcast address */ |
| 137 | { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff }; |
wdenk | 73a8b27 | 2003-06-05 19:27:42 +0000 | [diff] [blame] | 138 | uchar NetEtherNullAddr[6] = |
| 139 | { 0, 0, 0, 0, 0, 0 }; |
Rafal Jaworowski | f85b607 | 2007-12-27 18:19:02 +0100 | [diff] [blame] | 140 | #ifdef CONFIG_API |
| 141 | void (*push_packet)(volatile void *, int len) = 0; |
| 142 | #endif |
Jon Loeliger | 643d1ab | 2007-07-09 17:45:14 -0500 | [diff] [blame] | 143 | #if defined(CONFIG_CMD_CDP) |
wdenk | 6e59238 | 2004-04-18 17:39:38 +0000 | [diff] [blame] | 144 | uchar NetCDPAddr[6] = /* Ethernet bcast address */ |
wdenk | a3d991b | 2004-04-15 21:48:45 +0000 | [diff] [blame] | 145 | { 0x01, 0x00, 0x0c, 0xcc, 0xcc, 0xcc }; |
| 146 | #endif |
wdenk | 2d96695 | 2002-10-31 22:12:35 +0000 | [diff] [blame] | 147 | int NetState; /* Network loop state */ |
| 148 | #ifdef CONFIG_NET_MULTI |
| 149 | int NetRestartWrap = 0; /* Tried all network devices */ |
| 150 | static int NetRestarted = 0; /* Network loop restarted */ |
| 151 | static int NetDevExists = 0; /* At least one device configured */ |
| 152 | #endif |
| 153 | |
wdenk | 6e59238 | 2004-04-18 17:39:38 +0000 | [diff] [blame] | 154 | /* XXX in both little & big endian machines 0xFFFF == ntohs(-1) */ |
| 155 | ushort NetOurVLAN = 0xFFFF; /* default is without VLAN */ |
| 156 | ushort NetOurNativeVLAN = 0xFFFF; /* ditto */ |
wdenk | a3d991b | 2004-04-15 21:48:45 +0000 | [diff] [blame] | 157 | |
wdenk | 2d96695 | 2002-10-31 22:12:35 +0000 | [diff] [blame] | 158 | char BootFile[128]; /* Boot File name */ |
| 159 | |
Jon Loeliger | 643d1ab | 2007-07-09 17:45:14 -0500 | [diff] [blame] | 160 | #if defined(CONFIG_CMD_PING) |
wdenk | 73a8b27 | 2003-06-05 19:27:42 +0000 | [diff] [blame] | 161 | IPaddr_t NetPingIP; /* the ip address to ping */ |
| 162 | |
| 163 | static void PingStart(void); |
| 164 | #endif |
| 165 | |
Jon Loeliger | 643d1ab | 2007-07-09 17:45:14 -0500 | [diff] [blame] | 166 | #if defined(CONFIG_CMD_CDP) |
wdenk | a3d991b | 2004-04-15 21:48:45 +0000 | [diff] [blame] | 167 | static void CDPStart(void); |
| 168 | #endif |
| 169 | |
Jon Loeliger | 643d1ab | 2007-07-09 17:45:14 -0500 | [diff] [blame] | 170 | #if defined(CONFIG_CMD_SNTP) |
wdenk | ea287de | 2005-04-01 00:25:43 +0000 | [diff] [blame] | 171 | IPaddr_t NetNtpServerIP; /* NTP server IP address */ |
| 172 | int NetTimeOffset=0; /* offset time from UTC */ |
| 173 | #endif |
| 174 | |
wdenk | 68ceb29 | 2004-08-02 21:11:11 +0000 | [diff] [blame] | 175 | #ifdef CONFIG_NETCONSOLE |
| 176 | void NcStart(void); |
| 177 | int nc_input_packet(uchar *pkt, unsigned dest, unsigned src, unsigned len); |
| 178 | #endif |
| 179 | |
wdenk | 2d96695 | 2002-10-31 22:12:35 +0000 | [diff] [blame] | 180 | volatile uchar PktBuf[(PKTBUFSRX+1) * PKTSIZE_ALIGN + PKTALIGN]; |
| 181 | |
| 182 | volatile uchar *NetRxPackets[PKTBUFSRX]; /* Receive packets */ |
| 183 | |
| 184 | static rxhand_f *packetHandler; /* Current RX packet handler */ |
| 185 | static thand_f *timeHandler; /* Current timeout handler */ |
wdenk | e0ac62d | 2003-08-17 18:55:18 +0000 | [diff] [blame] | 186 | static ulong timeStart; /* Time base value */ |
| 187 | static ulong timeDelta; /* Current timeout value */ |
wdenk | 2d96695 | 2002-10-31 22:12:35 +0000 | [diff] [blame] | 188 | volatile uchar *NetTxPacket = 0; /* THE transmit packet */ |
| 189 | |
| 190 | static int net_check_prereq (proto_t protocol); |
| 191 | |
| 192 | /**********************************************************************/ |
wdenk | 73a8b27 | 2003-06-05 19:27:42 +0000 | [diff] [blame] | 193 | |
| 194 | IPaddr_t NetArpWaitPacketIP; |
| 195 | IPaddr_t NetArpWaitReplyIP; |
| 196 | uchar *NetArpWaitPacketMAC; /* MAC address of waiting packet's destination */ |
Wolfgang Denk | b2f5080 | 2005-08-12 23:43:12 +0200 | [diff] [blame] | 197 | uchar *NetArpWaitTxPacket; /* THE transmit packet */ |
wdenk | 73a8b27 | 2003-06-05 19:27:42 +0000 | [diff] [blame] | 198 | int NetArpWaitTxPacketSize; |
| 199 | uchar NetArpWaitPacketBuf[PKTSIZE_ALIGN + PKTALIGN]; |
| 200 | ulong NetArpWaitTimerStart; |
| 201 | int NetArpWaitTry; |
| 202 | |
wdenk | 6e59238 | 2004-04-18 17:39:38 +0000 | [diff] [blame] | 203 | void ArpRequest (void) |
wdenk | 73a8b27 | 2003-06-05 19:27:42 +0000 | [diff] [blame] | 204 | { |
| 205 | int i; |
| 206 | volatile uchar *pkt; |
wdenk | 6e59238 | 2004-04-18 17:39:38 +0000 | [diff] [blame] | 207 | ARP_t *arp; |
wdenk | 73a8b27 | 2003-06-05 19:27:42 +0000 | [diff] [blame] | 208 | |
| 209 | #ifdef ET_DEBUG |
wdenk | 6e59238 | 2004-04-18 17:39:38 +0000 | [diff] [blame] | 210 | printf ("ARP broadcast %d\n", NetArpWaitTry); |
wdenk | 73a8b27 | 2003-06-05 19:27:42 +0000 | [diff] [blame] | 211 | #endif |
| 212 | pkt = NetTxPacket; |
| 213 | |
wdenk | 6e59238 | 2004-04-18 17:39:38 +0000 | [diff] [blame] | 214 | pkt += NetSetEther (pkt, NetBcastAddr, PROT_ARP); |
wdenk | 73a8b27 | 2003-06-05 19:27:42 +0000 | [diff] [blame] | 215 | |
wdenk | 6e59238 | 2004-04-18 17:39:38 +0000 | [diff] [blame] | 216 | arp = (ARP_t *) pkt; |
wdenk | 73a8b27 | 2003-06-05 19:27:42 +0000 | [diff] [blame] | 217 | |
wdenk | 6e59238 | 2004-04-18 17:39:38 +0000 | [diff] [blame] | 218 | arp->ar_hrd = htons (ARP_ETHER); |
| 219 | arp->ar_pro = htons (PROT_IP); |
wdenk | 73a8b27 | 2003-06-05 19:27:42 +0000 | [diff] [blame] | 220 | arp->ar_hln = 6; |
| 221 | arp->ar_pln = 4; |
wdenk | 6e59238 | 2004-04-18 17:39:38 +0000 | [diff] [blame] | 222 | arp->ar_op = htons (ARPOP_REQUEST); |
wdenk | 73a8b27 | 2003-06-05 19:27:42 +0000 | [diff] [blame] | 223 | |
Wolfgang Denk | b2f5080 | 2005-08-12 23:43:12 +0200 | [diff] [blame] | 224 | memcpy (&arp->ar_data[0], NetOurEther, 6); /* source ET addr */ |
| 225 | NetWriteIP ((uchar *) & arp->ar_data[6], NetOurIP); /* source IP addr */ |
wdenk | 6e59238 | 2004-04-18 17:39:38 +0000 | [diff] [blame] | 226 | for (i = 10; i < 16; ++i) { |
| 227 | arp->ar_data[i] = 0; /* dest ET addr = 0 */ |
wdenk | 73a8b27 | 2003-06-05 19:27:42 +0000 | [diff] [blame] | 228 | } |
| 229 | |
wdenk | 6e59238 | 2004-04-18 17:39:38 +0000 | [diff] [blame] | 230 | if ((NetArpWaitPacketIP & NetOurSubnetMask) != |
| 231 | (NetOurIP & NetOurSubnetMask)) { |
| 232 | if (NetOurGatewayIP == 0) { |
| 233 | puts ("## Warning: gatewayip needed but not set\n"); |
Wolfgang Denk | d509b81 | 2006-03-12 01:13:30 +0100 | [diff] [blame] | 234 | NetArpWaitReplyIP = NetArpWaitPacketIP; |
| 235 | } else { |
| 236 | NetArpWaitReplyIP = NetOurGatewayIP; |
wdenk | 6e59238 | 2004-04-18 17:39:38 +0000 | [diff] [blame] | 237 | } |
wdenk | 6e59238 | 2004-04-18 17:39:38 +0000 | [diff] [blame] | 238 | } else { |
| 239 | NetArpWaitReplyIP = NetArpWaitPacketIP; |
| 240 | } |
wdenk | 73a8b27 | 2003-06-05 19:27:42 +0000 | [diff] [blame] | 241 | |
wdenk | 6e59238 | 2004-04-18 17:39:38 +0000 | [diff] [blame] | 242 | NetWriteIP ((uchar *) & arp->ar_data[16], NetArpWaitReplyIP); |
| 243 | (void) eth_send (NetTxPacket, (pkt - NetTxPacket) + ARP_HDR_SIZE); |
wdenk | 73a8b27 | 2003-06-05 19:27:42 +0000 | [diff] [blame] | 244 | } |
| 245 | |
| 246 | void ArpTimeoutCheck(void) |
| 247 | { |
| 248 | ulong t; |
| 249 | |
| 250 | if (!NetArpWaitPacketIP) |
| 251 | return; |
| 252 | |
| 253 | t = get_timer(0); |
| 254 | |
| 255 | /* check for arp timeout */ |
| 256 | if ((t - NetArpWaitTimerStart) > ARP_TIMEOUT * CFG_HZ) { |
| 257 | NetArpWaitTry++; |
| 258 | |
| 259 | if (NetArpWaitTry >= ARP_TIMEOUT_COUNT) { |
| 260 | puts ("\nARP Retry count exceeded; starting again\n"); |
| 261 | NetArpWaitTry = 0; |
| 262 | NetStartAgain(); |
| 263 | } else { |
| 264 | NetArpWaitTimerStart = t; |
| 265 | ArpRequest(); |
| 266 | } |
| 267 | } |
| 268 | } |
| 269 | |
| 270 | /**********************************************************************/ |
wdenk | 2d96695 | 2002-10-31 22:12:35 +0000 | [diff] [blame] | 271 | /* |
| 272 | * Main network processing loop. |
| 273 | */ |
| 274 | |
| 275 | int |
| 276 | NetLoop(proto_t protocol) |
| 277 | { |
wdenk | 2d96695 | 2002-10-31 22:12:35 +0000 | [diff] [blame] | 278 | bd_t *bd = gd->bd; |
| 279 | |
| 280 | #ifdef CONFIG_NET_MULTI |
| 281 | NetRestarted = 0; |
| 282 | NetDevExists = 0; |
| 283 | #endif |
| 284 | |
wdenk | 73a8b27 | 2003-06-05 19:27:42 +0000 | [diff] [blame] | 285 | /* XXX problem with bss workaround */ |
| 286 | NetArpWaitPacketMAC = NULL; |
| 287 | NetArpWaitTxPacket = NULL; |
| 288 | NetArpWaitPacketIP = 0; |
| 289 | NetArpWaitReplyIP = 0; |
| 290 | NetArpWaitTxPacket = NULL; |
| 291 | NetTxPacket = NULL; |
| 292 | |
wdenk | 2d96695 | 2002-10-31 22:12:35 +0000 | [diff] [blame] | 293 | if (!NetTxPacket) { |
| 294 | int i; |
wdenk | 2d96695 | 2002-10-31 22:12:35 +0000 | [diff] [blame] | 295 | /* |
| 296 | * Setup packet buffers, aligned correctly. |
| 297 | */ |
| 298 | NetTxPacket = &PktBuf[0] + (PKTALIGN - 1); |
| 299 | NetTxPacket -= (ulong)NetTxPacket % PKTALIGN; |
| 300 | for (i = 0; i < PKTBUFSRX; i++) { |
| 301 | NetRxPackets[i] = NetTxPacket + (i+1)*PKTSIZE_ALIGN; |
| 302 | } |
wdenk | 73a8b27 | 2003-06-05 19:27:42 +0000 | [diff] [blame] | 303 | } |
| 304 | |
| 305 | if (!NetArpWaitTxPacket) { |
| 306 | NetArpWaitTxPacket = &NetArpWaitPacketBuf[0] + (PKTALIGN - 1); |
| 307 | NetArpWaitTxPacket -= (ulong)NetArpWaitTxPacket % PKTALIGN; |
| 308 | NetArpWaitTxPacketSize = 0; |
wdenk | 2d96695 | 2002-10-31 22:12:35 +0000 | [diff] [blame] | 309 | } |
| 310 | |
| 311 | eth_halt(); |
wdenk | 6e59238 | 2004-04-18 17:39:38 +0000 | [diff] [blame] | 312 | #ifdef CONFIG_NET_MULTI |
wdenk | a3d991b | 2004-04-15 21:48:45 +0000 | [diff] [blame] | 313 | eth_set_current(); |
wdenk | 6e59238 | 2004-04-18 17:39:38 +0000 | [diff] [blame] | 314 | #endif |
wdenk | b1bf6f2 | 2005-04-03 14:52:59 +0000 | [diff] [blame] | 315 | if (eth_init(bd) < 0) { |
| 316 | eth_halt(); |
wdenk | 6e59238 | 2004-04-18 17:39:38 +0000 | [diff] [blame] | 317 | return(-1); |
wdenk | b1bf6f2 | 2005-04-03 14:52:59 +0000 | [diff] [blame] | 318 | } |
wdenk | 2d96695 | 2002-10-31 22:12:35 +0000 | [diff] [blame] | 319 | |
| 320 | restart: |
| 321 | #ifdef CONFIG_NET_MULTI |
| 322 | memcpy (NetOurEther, eth_get_dev()->enetaddr, 6); |
| 323 | #else |
| 324 | memcpy (NetOurEther, bd->bi_enetaddr, 6); |
| 325 | #endif |
| 326 | |
| 327 | NetState = NETLOOP_CONTINUE; |
| 328 | |
| 329 | /* |
| 330 | * Start the ball rolling with the given start function. From |
| 331 | * here on, this code is a state machine driven by received |
| 332 | * packets and timer events. |
| 333 | */ |
| 334 | |
| 335 | switch (protocol) { |
Jon Loeliger | 643d1ab | 2007-07-09 17:45:14 -0500 | [diff] [blame] | 336 | #if defined(CONFIG_CMD_NFS) |
wdenk | cbd8a35 | 2004-02-24 02:00:03 +0000 | [diff] [blame] | 337 | case NFS: |
| 338 | #endif |
Jon Loeliger | 643d1ab | 2007-07-09 17:45:14 -0500 | [diff] [blame] | 339 | #if defined(CONFIG_CMD_PING) |
wdenk | 73a8b27 | 2003-06-05 19:27:42 +0000 | [diff] [blame] | 340 | case PING: |
| 341 | #endif |
Jon Loeliger | 643d1ab | 2007-07-09 17:45:14 -0500 | [diff] [blame] | 342 | #if defined(CONFIG_CMD_SNTP) |
wdenk | ea287de | 2005-04-01 00:25:43 +0000 | [diff] [blame] | 343 | case SNTP: |
| 344 | #endif |
wdenk | 68ceb29 | 2004-08-02 21:11:11 +0000 | [diff] [blame] | 345 | case NETCONS: |
wdenk | 2d96695 | 2002-10-31 22:12:35 +0000 | [diff] [blame] | 346 | case TFTP: |
| 347 | NetCopyIP(&NetOurIP, &bd->bi_ip_addr); |
wdenk | 2d96695 | 2002-10-31 22:12:35 +0000 | [diff] [blame] | 348 | NetOurGatewayIP = getenv_IPaddr ("gatewayip"); |
| 349 | NetOurSubnetMask= getenv_IPaddr ("netmask"); |
wdenk | a3d991b | 2004-04-15 21:48:45 +0000 | [diff] [blame] | 350 | NetOurVLAN = getenv_VLAN("vlan"); |
| 351 | NetOurNativeVLAN = getenv_VLAN("nvlan"); |
wdenk | 73a8b27 | 2003-06-05 19:27:42 +0000 | [diff] [blame] | 352 | |
| 353 | switch (protocol) { |
Jon Loeliger | 643d1ab | 2007-07-09 17:45:14 -0500 | [diff] [blame] | 354 | #if defined(CONFIG_CMD_NFS) |
wdenk | cbd8a35 | 2004-02-24 02:00:03 +0000 | [diff] [blame] | 355 | case NFS: |
| 356 | #endif |
wdenk | 68ceb29 | 2004-08-02 21:11:11 +0000 | [diff] [blame] | 357 | case NETCONS: |
wdenk | 73a8b27 | 2003-06-05 19:27:42 +0000 | [diff] [blame] | 358 | case TFTP: |
| 359 | NetServerIP = getenv_IPaddr ("serverip"); |
| 360 | break; |
Jon Loeliger | 643d1ab | 2007-07-09 17:45:14 -0500 | [diff] [blame] | 361 | #if defined(CONFIG_CMD_PING) |
wdenk | 73a8b27 | 2003-06-05 19:27:42 +0000 | [diff] [blame] | 362 | case PING: |
| 363 | /* nothing */ |
| 364 | break; |
| 365 | #endif |
Jon Loeliger | 643d1ab | 2007-07-09 17:45:14 -0500 | [diff] [blame] | 366 | #if defined(CONFIG_CMD_SNTP) |
wdenk | ea287de | 2005-04-01 00:25:43 +0000 | [diff] [blame] | 367 | case SNTP: |
| 368 | /* nothing */ |
| 369 | break; |
| 370 | #endif |
wdenk | 73a8b27 | 2003-06-05 19:27:42 +0000 | [diff] [blame] | 371 | default: |
| 372 | break; |
| 373 | } |
| 374 | |
wdenk | 2d96695 | 2002-10-31 22:12:35 +0000 | [diff] [blame] | 375 | break; |
| 376 | case BOOTP: |
| 377 | case RARP: |
| 378 | /* |
wdenk | 8bde7f7 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 379 | * initialize our IP addr to 0 in order to accept ANY |
| 380 | * IP addr assigned to us by the BOOTP / RARP server |
wdenk | 2d96695 | 2002-10-31 22:12:35 +0000 | [diff] [blame] | 381 | */ |
| 382 | NetOurIP = 0; |
wdenk | 3d3befa | 2004-03-14 15:06:13 +0000 | [diff] [blame] | 383 | NetServerIP = getenv_IPaddr ("serverip"); |
Wolfgang Denk | b2f5080 | 2005-08-12 23:43:12 +0200 | [diff] [blame] | 384 | NetOurVLAN = getenv_VLAN("vlan"); /* VLANs must be read */ |
| 385 | NetOurNativeVLAN = getenv_VLAN("nvlan"); |
| 386 | case CDP: |
| 387 | NetOurVLAN = getenv_VLAN("vlan"); /* VLANs must be read */ |
| 388 | NetOurNativeVLAN = getenv_VLAN("nvlan"); |
wdenk | 2d96695 | 2002-10-31 22:12:35 +0000 | [diff] [blame] | 389 | break; |
| 390 | default: |
| 391 | break; |
| 392 | } |
| 393 | |
| 394 | switch (net_check_prereq (protocol)) { |
| 395 | case 1: |
| 396 | /* network not configured */ |
wdenk | b1bf6f2 | 2005-04-03 14:52:59 +0000 | [diff] [blame] | 397 | eth_halt(); |
wdenk | 1d0350e | 2002-11-11 21:14:20 +0000 | [diff] [blame] | 398 | return (-1); |
wdenk | 2d96695 | 2002-10-31 22:12:35 +0000 | [diff] [blame] | 399 | |
| 400 | #ifdef CONFIG_NET_MULTI |
| 401 | case 2: |
| 402 | /* network device not configured */ |
| 403 | break; |
| 404 | #endif /* CONFIG_NET_MULTI */ |
| 405 | |
| 406 | case 0: |
| 407 | #ifdef CONFIG_NET_MULTI |
| 408 | NetDevExists = 1; |
| 409 | #endif |
| 410 | switch (protocol) { |
| 411 | case TFTP: |
| 412 | /* always use ARP to get server ethernet address */ |
wdenk | 73a8b27 | 2003-06-05 19:27:42 +0000 | [diff] [blame] | 413 | TftpStart(); |
wdenk | 2d96695 | 2002-10-31 22:12:35 +0000 | [diff] [blame] | 414 | break; |
| 415 | |
Jon Loeliger | 643d1ab | 2007-07-09 17:45:14 -0500 | [diff] [blame] | 416 | #if defined(CONFIG_CMD_DHCP) |
wdenk | 2d96695 | 2002-10-31 22:12:35 +0000 | [diff] [blame] | 417 | case DHCP: |
| 418 | /* Start with a clean slate... */ |
wdenk | d407bf5 | 2004-10-11 22:51:13 +0000 | [diff] [blame] | 419 | BootpTry = 0; |
wdenk | 2d96695 | 2002-10-31 22:12:35 +0000 | [diff] [blame] | 420 | NetOurIP = 0; |
wdenk | 3d3befa | 2004-03-14 15:06:13 +0000 | [diff] [blame] | 421 | NetServerIP = getenv_IPaddr ("serverip"); |
wdenk | 2d96695 | 2002-10-31 22:12:35 +0000 | [diff] [blame] | 422 | DhcpRequest(); /* Basically same as BOOTP */ |
| 423 | break; |
Jon Loeliger | 610f2e9 | 2007-07-10 11:05:02 -0500 | [diff] [blame] | 424 | #endif |
wdenk | 2d96695 | 2002-10-31 22:12:35 +0000 | [diff] [blame] | 425 | |
| 426 | case BOOTP: |
| 427 | BootpTry = 0; |
| 428 | BootpRequest (); |
| 429 | break; |
| 430 | |
| 431 | case RARP: |
| 432 | RarpTry = 0; |
| 433 | RarpRequest (); |
| 434 | break; |
Jon Loeliger | 643d1ab | 2007-07-09 17:45:14 -0500 | [diff] [blame] | 435 | #if defined(CONFIG_CMD_PING) |
wdenk | 73a8b27 | 2003-06-05 19:27:42 +0000 | [diff] [blame] | 436 | case PING: |
| 437 | PingStart(); |
| 438 | break; |
| 439 | #endif |
Jon Loeliger | 643d1ab | 2007-07-09 17:45:14 -0500 | [diff] [blame] | 440 | #if defined(CONFIG_CMD_NFS) |
wdenk | cbd8a35 | 2004-02-24 02:00:03 +0000 | [diff] [blame] | 441 | case NFS: |
| 442 | NfsStart(); |
| 443 | break; |
| 444 | #endif |
Jon Loeliger | 643d1ab | 2007-07-09 17:45:14 -0500 | [diff] [blame] | 445 | #if defined(CONFIG_CMD_CDP) |
wdenk | a3d991b | 2004-04-15 21:48:45 +0000 | [diff] [blame] | 446 | case CDP: |
| 447 | CDPStart(); |
| 448 | break; |
| 449 | #endif |
wdenk | 68ceb29 | 2004-08-02 21:11:11 +0000 | [diff] [blame] | 450 | #ifdef CONFIG_NETCONSOLE |
| 451 | case NETCONS: |
| 452 | NcStart(); |
| 453 | break; |
| 454 | #endif |
Jon Loeliger | 643d1ab | 2007-07-09 17:45:14 -0500 | [diff] [blame] | 455 | #if defined(CONFIG_CMD_SNTP) |
wdenk | ea287de | 2005-04-01 00:25:43 +0000 | [diff] [blame] | 456 | case SNTP: |
| 457 | SntpStart(); |
| 458 | break; |
| 459 | #endif |
wdenk | 2d96695 | 2002-10-31 22:12:35 +0000 | [diff] [blame] | 460 | default: |
| 461 | break; |
| 462 | } |
| 463 | |
| 464 | NetBootFileXferSize = 0; |
| 465 | break; |
| 466 | } |
| 467 | |
Jon Loeliger | 643d1ab | 2007-07-09 17:45:14 -0500 | [diff] [blame] | 468 | #if defined(CONFIG_MII) || defined(CONFIG_CMD_MII) |
wdenk | fc3e216 | 2003-10-08 22:33:00 +0000 | [diff] [blame] | 469 | #if defined(CFG_FAULT_ECHO_LINK_DOWN) && defined(CONFIG_STATUS_LED) && defined(STATUS_LED_RED) |
| 470 | /* |
wdenk | 42d1f03 | 2003-10-15 23:53:47 +0000 | [diff] [blame] | 471 | * Echo the inverted link state to the fault LED. |
wdenk | fc3e216 | 2003-10-08 22:33:00 +0000 | [diff] [blame] | 472 | */ |
Marian Balakowicz | 63ff004 | 2005-10-28 22:30:33 +0200 | [diff] [blame] | 473 | if(miiphy_link(eth_get_dev()->name, CFG_FAULT_MII_ADDR)) { |
wdenk | fc3e216 | 2003-10-08 22:33:00 +0000 | [diff] [blame] | 474 | status_led_set (STATUS_LED_RED, STATUS_LED_OFF); |
| 475 | } else { |
| 476 | status_led_set (STATUS_LED_RED, STATUS_LED_ON); |
| 477 | } |
| 478 | #endif /* CFG_FAULT_ECHO_LINK_DOWN, ... */ |
| 479 | #endif /* CONFIG_MII, ... */ |
wdenk | 2d96695 | 2002-10-31 22:12:35 +0000 | [diff] [blame] | 480 | |
| 481 | /* |
| 482 | * Main packet reception loop. Loop receiving packets until |
wdenk | 59acc29 | 2005-04-03 14:18:51 +0000 | [diff] [blame] | 483 | * someone sets `NetState' to a state that terminates. |
wdenk | 2d96695 | 2002-10-31 22:12:35 +0000 | [diff] [blame] | 484 | */ |
| 485 | for (;;) { |
| 486 | WATCHDOG_RESET(); |
| 487 | #ifdef CONFIG_SHOW_ACTIVITY |
| 488 | { |
| 489 | extern void show_activity(int arg); |
| 490 | show_activity(1); |
| 491 | } |
| 492 | #endif |
| 493 | /* |
| 494 | * Check the ethernet for a new packet. The ethernet |
| 495 | * receive routine will process it. |
| 496 | */ |
| 497 | eth_rx(); |
| 498 | |
| 499 | /* |
| 500 | * Abort if ctrl-c was pressed. |
| 501 | */ |
| 502 | if (ctrlc()) { |
wdenk | 8bde7f7 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 503 | eth_halt(); |
wdenk | 4b9206e | 2004-03-23 22:14:11 +0000 | [diff] [blame] | 504 | puts ("\nAbort\n"); |
wdenk | 1d0350e | 2002-11-11 21:14:20 +0000 | [diff] [blame] | 505 | return (-1); |
wdenk | 2d96695 | 2002-10-31 22:12:35 +0000 | [diff] [blame] | 506 | } |
| 507 | |
wdenk | 73a8b27 | 2003-06-05 19:27:42 +0000 | [diff] [blame] | 508 | ArpTimeoutCheck(); |
wdenk | 2d96695 | 2002-10-31 22:12:35 +0000 | [diff] [blame] | 509 | |
| 510 | /* |
| 511 | * Check for a timeout, and run the timeout handler |
| 512 | * if we have one. |
| 513 | */ |
wdenk | e0ac62d | 2003-08-17 18:55:18 +0000 | [diff] [blame] | 514 | if (timeHandler && ((get_timer(0) - timeStart) > timeDelta)) { |
wdenk | 2d96695 | 2002-10-31 22:12:35 +0000 | [diff] [blame] | 515 | thand_f *x; |
| 516 | |
Jon Loeliger | 643d1ab | 2007-07-09 17:45:14 -0500 | [diff] [blame] | 517 | #if defined(CONFIG_MII) || defined(CONFIG_CMD_MII) |
wdenk | 6315349 | 2005-04-03 20:55:38 +0000 | [diff] [blame] | 518 | # if defined(CFG_FAULT_ECHO_LINK_DOWN) && \ |
| 519 | defined(CONFIG_STATUS_LED) && \ |
wdenk | 59acc29 | 2005-04-03 14:18:51 +0000 | [diff] [blame] | 520 | defined(STATUS_LED_RED) |
wdenk | fc3e216 | 2003-10-08 22:33:00 +0000 | [diff] [blame] | 521 | /* |
wdenk | 42d1f03 | 2003-10-15 23:53:47 +0000 | [diff] [blame] | 522 | * Echo the inverted link state to the fault LED. |
wdenk | fc3e216 | 2003-10-08 22:33:00 +0000 | [diff] [blame] | 523 | */ |
Marian Balakowicz | 63ff004 | 2005-10-28 22:30:33 +0200 | [diff] [blame] | 524 | if(miiphy_link(eth_get_dev()->name, CFG_FAULT_MII_ADDR)) { |
wdenk | fc3e216 | 2003-10-08 22:33:00 +0000 | [diff] [blame] | 525 | status_led_set (STATUS_LED_RED, STATUS_LED_OFF); |
| 526 | } else { |
| 527 | status_led_set (STATUS_LED_RED, STATUS_LED_ON); |
| 528 | } |
wdenk | 59acc29 | 2005-04-03 14:18:51 +0000 | [diff] [blame] | 529 | # endif /* CFG_FAULT_ECHO_LINK_DOWN, ... */ |
wdenk | fc3e216 | 2003-10-08 22:33:00 +0000 | [diff] [blame] | 530 | #endif /* CONFIG_MII, ... */ |
wdenk | 2d96695 | 2002-10-31 22:12:35 +0000 | [diff] [blame] | 531 | x = timeHandler; |
| 532 | timeHandler = (thand_f *)0; |
| 533 | (*x)(); |
| 534 | } |
| 535 | |
| 536 | |
| 537 | switch (NetState) { |
| 538 | |
| 539 | case NETLOOP_RESTART: |
| 540 | #ifdef CONFIG_NET_MULTI |
| 541 | NetRestarted = 1; |
| 542 | #endif |
| 543 | goto restart; |
| 544 | |
| 545 | case NETLOOP_SUCCESS: |
| 546 | if (NetBootFileXferSize > 0) { |
Wolfgang Denk | f34024d | 2007-09-12 00:48:57 +0200 | [diff] [blame] | 547 | char buf[20]; |
wdenk | 2d96695 | 2002-10-31 22:12:35 +0000 | [diff] [blame] | 548 | printf("Bytes transferred = %ld (%lx hex)\n", |
| 549 | NetBootFileXferSize, |
| 550 | NetBootFileXferSize); |
Wolfgang Denk | f34024d | 2007-09-12 00:48:57 +0200 | [diff] [blame] | 551 | sprintf(buf, "%lX", NetBootFileXferSize); |
wdenk | 2d96695 | 2002-10-31 22:12:35 +0000 | [diff] [blame] | 552 | setenv("filesize", buf); |
wdenk | a3d991b | 2004-04-15 21:48:45 +0000 | [diff] [blame] | 553 | |
| 554 | sprintf(buf, "%lX", (unsigned long)load_addr); |
| 555 | setenv("fileaddr", buf); |
wdenk | 2d96695 | 2002-10-31 22:12:35 +0000 | [diff] [blame] | 556 | } |
| 557 | eth_halt(); |
| 558 | return NetBootFileXferSize; |
| 559 | |
| 560 | case NETLOOP_FAIL: |
wdenk | 1d0350e | 2002-11-11 21:14:20 +0000 | [diff] [blame] | 561 | return (-1); |
wdenk | 2d96695 | 2002-10-31 22:12:35 +0000 | [diff] [blame] | 562 | } |
| 563 | } |
| 564 | } |
| 565 | |
| 566 | /**********************************************************************/ |
| 567 | |
| 568 | static void |
| 569 | startAgainTimeout(void) |
| 570 | { |
| 571 | NetState = NETLOOP_RESTART; |
| 572 | } |
| 573 | |
| 574 | static void |
| 575 | startAgainHandler(uchar * pkt, unsigned dest, unsigned src, unsigned len) |
| 576 | { |
| 577 | /* Totally ignore the packet */ |
| 578 | } |
| 579 | |
wdenk | 6e59238 | 2004-04-18 17:39:38 +0000 | [diff] [blame] | 580 | void NetStartAgain (void) |
wdenk | 2d96695 | 2002-10-31 22:12:35 +0000 | [diff] [blame] | 581 | { |
wdenk | 6e59238 | 2004-04-18 17:39:38 +0000 | [diff] [blame] | 582 | char *nretry; |
| 583 | int noretry = 0, once = 0; |
wdenk | a3d991b | 2004-04-15 21:48:45 +0000 | [diff] [blame] | 584 | |
wdenk | 6e59238 | 2004-04-18 17:39:38 +0000 | [diff] [blame] | 585 | if ((nretry = getenv ("netretry")) != NULL) { |
| 586 | noretry = (strcmp (nretry, "no") == 0); |
| 587 | once = (strcmp (nretry, "once") == 0); |
| 588 | } |
| 589 | if (noretry) { |
| 590 | eth_halt (); |
wdenk | a3d991b | 2004-04-15 21:48:45 +0000 | [diff] [blame] | 591 | NetState = NETLOOP_FAIL; |
| 592 | return; |
| 593 | } |
wdenk | 2d96695 | 2002-10-31 22:12:35 +0000 | [diff] [blame] | 594 | #ifndef CONFIG_NET_MULTI |
Jean-Christophe PLAGNIOL-VILLARD | 079c2c4 | 2007-11-17 11:31:10 +0100 | [diff] [blame] | 595 | NetSetTimeout (10UL * CFG_HZ, startAgainTimeout); |
wdenk | 6e59238 | 2004-04-18 17:39:38 +0000 | [diff] [blame] | 596 | NetSetHandler (startAgainHandler); |
| 597 | #else /* !CONFIG_NET_MULTI*/ |
| 598 | eth_halt (); |
Matthias Fuchs | 8b0c5c1 | 2007-12-27 16:58:41 +0100 | [diff] [blame] | 599 | #if !defined(CONFIG_NET_DO_NOT_TRY_ANOTHER) |
wdenk | 6e59238 | 2004-04-18 17:39:38 +0000 | [diff] [blame] | 600 | eth_try_another (!NetRestarted); |
Matthias Fuchs | 8b0c5c1 | 2007-12-27 16:58:41 +0100 | [diff] [blame] | 601 | #endif |
wdenk | 6e59238 | 2004-04-18 17:39:38 +0000 | [diff] [blame] | 602 | eth_init (gd->bd); |
| 603 | if (NetRestartWrap) { |
wdenk | 2d96695 | 2002-10-31 22:12:35 +0000 | [diff] [blame] | 604 | NetRestartWrap = 0; |
wdenk | 6e59238 | 2004-04-18 17:39:38 +0000 | [diff] [blame] | 605 | if (NetDevExists && !once) { |
Jean-Christophe PLAGNIOL-VILLARD | 079c2c4 | 2007-11-17 11:31:10 +0100 | [diff] [blame] | 606 | NetSetTimeout (10UL * CFG_HZ, startAgainTimeout); |
wdenk | 6e59238 | 2004-04-18 17:39:38 +0000 | [diff] [blame] | 607 | NetSetHandler (startAgainHandler); |
| 608 | } else { |
wdenk | 2d96695 | 2002-10-31 22:12:35 +0000 | [diff] [blame] | 609 | NetState = NETLOOP_FAIL; |
| 610 | } |
wdenk | 6e59238 | 2004-04-18 17:39:38 +0000 | [diff] [blame] | 611 | } else { |
wdenk | 2d96695 | 2002-10-31 22:12:35 +0000 | [diff] [blame] | 612 | NetState = NETLOOP_RESTART; |
| 613 | } |
wdenk | 6e59238 | 2004-04-18 17:39:38 +0000 | [diff] [blame] | 614 | #endif /* CONFIG_NET_MULTI */ |
wdenk | 2d96695 | 2002-10-31 22:12:35 +0000 | [diff] [blame] | 615 | } |
| 616 | |
| 617 | /**********************************************************************/ |
| 618 | /* |
| 619 | * Miscelaneous bits. |
| 620 | */ |
| 621 | |
| 622 | void |
| 623 | NetSetHandler(rxhand_f * f) |
| 624 | { |
| 625 | packetHandler = f; |
| 626 | } |
| 627 | |
| 628 | |
| 629 | void |
wdenk | 3e01d75 | 2004-10-09 21:56:21 +0000 | [diff] [blame] | 630 | NetSetTimeout(ulong iv, thand_f * f) |
wdenk | 2d96695 | 2002-10-31 22:12:35 +0000 | [diff] [blame] | 631 | { |
| 632 | if (iv == 0) { |
| 633 | timeHandler = (thand_f *)0; |
| 634 | } else { |
| 635 | timeHandler = f; |
wdenk | e0ac62d | 2003-08-17 18:55:18 +0000 | [diff] [blame] | 636 | timeStart = get_timer(0); |
| 637 | timeDelta = iv; |
wdenk | 2d96695 | 2002-10-31 22:12:35 +0000 | [diff] [blame] | 638 | } |
| 639 | } |
| 640 | |
| 641 | |
| 642 | void |
| 643 | NetSendPacket(volatile uchar * pkt, int len) |
| 644 | { |
| 645 | (void) eth_send(pkt, len); |
| 646 | } |
| 647 | |
wdenk | 73a8b27 | 2003-06-05 19:27:42 +0000 | [diff] [blame] | 648 | int |
| 649 | NetSendUDPPacket(uchar *ether, IPaddr_t dest, int dport, int sport, int len) |
| 650 | { |
wdenk | a3d991b | 2004-04-15 21:48:45 +0000 | [diff] [blame] | 651 | uchar *pkt; |
| 652 | |
wdenk | 73a8b27 | 2003-06-05 19:27:42 +0000 | [diff] [blame] | 653 | /* convert to new style broadcast */ |
| 654 | if (dest == 0) |
| 655 | dest = 0xFFFFFFFF; |
wdenk | 2d96695 | 2002-10-31 22:12:35 +0000 | [diff] [blame] | 656 | |
wdenk | 73a8b27 | 2003-06-05 19:27:42 +0000 | [diff] [blame] | 657 | /* if broadcast, make the ether address a broadcast and don't do ARP */ |
| 658 | if (dest == 0xFFFFFFFF) |
| 659 | ether = NetBcastAddr; |
| 660 | |
| 661 | /* if MAC address was not discovered yet, save the packet and do an ARP request */ |
| 662 | if (memcmp(ether, NetEtherNullAddr, 6) == 0) { |
| 663 | |
| 664 | #ifdef ET_DEBUG |
| 665 | printf("sending ARP for %08lx\n", dest); |
| 666 | #endif |
wdenk | 73a8b27 | 2003-06-05 19:27:42 +0000 | [diff] [blame] | 667 | NetArpWaitPacketIP = dest; |
| 668 | NetArpWaitPacketMAC = ether; |
wdenk | a3d991b | 2004-04-15 21:48:45 +0000 | [diff] [blame] | 669 | |
| 670 | pkt = NetArpWaitTxPacket; |
| 671 | pkt += NetSetEther (pkt, NetArpWaitPacketMAC, PROT_IP); |
| 672 | |
| 673 | NetSetIP (pkt, dest, dport, sport, len); |
| 674 | memcpy(pkt + IP_HDR_SIZE, (uchar *)NetTxPacket + (pkt - (uchar *)NetArpWaitTxPacket) + IP_HDR_SIZE, len); |
wdenk | 73a8b27 | 2003-06-05 19:27:42 +0000 | [diff] [blame] | 675 | |
| 676 | /* size of the waiting packet */ |
wdenk | a3d991b | 2004-04-15 21:48:45 +0000 | [diff] [blame] | 677 | NetArpWaitTxPacketSize = (pkt - NetArpWaitTxPacket) + IP_HDR_SIZE + len; |
wdenk | 73a8b27 | 2003-06-05 19:27:42 +0000 | [diff] [blame] | 678 | |
| 679 | /* and do the ARP request */ |
| 680 | NetArpWaitTry = 1; |
| 681 | NetArpWaitTimerStart = get_timer(0); |
| 682 | ArpRequest(); |
| 683 | return 1; /* waiting */ |
| 684 | } |
| 685 | |
| 686 | #ifdef ET_DEBUG |
| 687 | printf("sending UDP to %08lx/%02x:%02x:%02x:%02x:%02x:%02x\n", |
wdenk | 6e59238 | 2004-04-18 17:39:38 +0000 | [diff] [blame] | 688 | dest, ether[0], ether[1], ether[2], ether[3], ether[4], ether[5]); |
wdenk | 73a8b27 | 2003-06-05 19:27:42 +0000 | [diff] [blame] | 689 | #endif |
| 690 | |
wdenk | a3d991b | 2004-04-15 21:48:45 +0000 | [diff] [blame] | 691 | pkt = (uchar *)NetTxPacket; |
| 692 | pkt += NetSetEther (pkt, ether, PROT_IP); |
| 693 | NetSetIP (pkt, dest, dport, sport, len); |
| 694 | (void) eth_send(NetTxPacket, (pkt - NetTxPacket) + IP_HDR_SIZE + len); |
wdenk | 73a8b27 | 2003-06-05 19:27:42 +0000 | [diff] [blame] | 695 | |
wdenk | 6e59238 | 2004-04-18 17:39:38 +0000 | [diff] [blame] | 696 | return 0; /* transmitted */ |
wdenk | 73a8b27 | 2003-06-05 19:27:42 +0000 | [diff] [blame] | 697 | } |
| 698 | |
Jon Loeliger | 643d1ab | 2007-07-09 17:45:14 -0500 | [diff] [blame] | 699 | #if defined(CONFIG_CMD_PING) |
wdenk | 73a8b27 | 2003-06-05 19:27:42 +0000 | [diff] [blame] | 700 | static ushort PingSeqNo; |
| 701 | |
| 702 | int PingSend(void) |
| 703 | { |
| 704 | static uchar mac[6]; |
| 705 | volatile IP_t *ip; |
| 706 | volatile ushort *s; |
wdenk | a3d991b | 2004-04-15 21:48:45 +0000 | [diff] [blame] | 707 | uchar *pkt; |
wdenk | 73a8b27 | 2003-06-05 19:27:42 +0000 | [diff] [blame] | 708 | |
| 709 | /* XXX always send arp request */ |
| 710 | |
| 711 | memcpy(mac, NetEtherNullAddr, 6); |
| 712 | |
| 713 | #ifdef ET_DEBUG |
| 714 | printf("sending ARP for %08lx\n", NetPingIP); |
| 715 | #endif |
| 716 | |
| 717 | NetArpWaitPacketIP = NetPingIP; |
| 718 | NetArpWaitPacketMAC = mac; |
| 719 | |
wdenk | a3d991b | 2004-04-15 21:48:45 +0000 | [diff] [blame] | 720 | pkt = NetArpWaitTxPacket; |
| 721 | pkt += NetSetEther(pkt, mac, PROT_IP); |
wdenk | 73a8b27 | 2003-06-05 19:27:42 +0000 | [diff] [blame] | 722 | |
wdenk | a3d991b | 2004-04-15 21:48:45 +0000 | [diff] [blame] | 723 | ip = (volatile IP_t *)pkt; |
wdenk | 73a8b27 | 2003-06-05 19:27:42 +0000 | [diff] [blame] | 724 | |
| 725 | /* |
| 726 | * Construct an IP and ICMP header. (need to set no fragment bit - XXX) |
| 727 | */ |
| 728 | ip->ip_hl_v = 0x45; /* IP_HDR_SIZE / 4 (not including UDP) */ |
| 729 | ip->ip_tos = 0; |
| 730 | ip->ip_len = htons(IP_HDR_SIZE_NO_UDP + 8); |
| 731 | ip->ip_id = htons(NetIPID++); |
| 732 | ip->ip_off = htons(0x4000); /* No fragmentation */ |
| 733 | ip->ip_ttl = 255; |
| 734 | ip->ip_p = 0x01; /* ICMP */ |
| 735 | ip->ip_sum = 0; |
| 736 | NetCopyIP((void*)&ip->ip_src, &NetOurIP); /* already in network byte order */ |
| 737 | NetCopyIP((void*)&ip->ip_dst, &NetPingIP); /* - "" - */ |
| 738 | ip->ip_sum = ~NetCksum((uchar *)ip, IP_HDR_SIZE_NO_UDP / 2); |
| 739 | |
| 740 | s = &ip->udp_src; /* XXX ICMP starts here */ |
| 741 | s[0] = htons(0x0800); /* echo-request, code */ |
| 742 | s[1] = 0; /* checksum */ |
| 743 | s[2] = 0; /* identifier */ |
| 744 | s[3] = htons(PingSeqNo++); /* sequence number */ |
| 745 | s[1] = ~NetCksum((uchar *)s, 8/2); |
| 746 | |
| 747 | /* size of the waiting packet */ |
wdenk | a3d991b | 2004-04-15 21:48:45 +0000 | [diff] [blame] | 748 | NetArpWaitTxPacketSize = (pkt - NetArpWaitTxPacket) + IP_HDR_SIZE_NO_UDP + 8; |
wdenk | 73a8b27 | 2003-06-05 19:27:42 +0000 | [diff] [blame] | 749 | |
| 750 | /* and do the ARP request */ |
| 751 | NetArpWaitTry = 1; |
| 752 | NetArpWaitTimerStart = get_timer(0); |
| 753 | ArpRequest(); |
| 754 | return 1; /* waiting */ |
| 755 | } |
| 756 | |
| 757 | static void |
| 758 | PingTimeout (void) |
| 759 | { |
| 760 | eth_halt(); |
| 761 | NetState = NETLOOP_FAIL; /* we did not get the reply */ |
| 762 | } |
| 763 | |
| 764 | static void |
| 765 | PingHandler (uchar * pkt, unsigned dest, unsigned src, unsigned len) |
| 766 | { |
| 767 | IPaddr_t tmp; |
| 768 | volatile IP_t *ip = (volatile IP_t *)pkt; |
| 769 | |
| 770 | tmp = NetReadIP((void *)&ip->ip_src); |
| 771 | if (tmp != NetPingIP) |
| 772 | return; |
| 773 | |
| 774 | NetState = NETLOOP_SUCCESS; |
| 775 | } |
| 776 | |
| 777 | static void PingStart(void) |
| 778 | { |
wdenk | a3d991b | 2004-04-15 21:48:45 +0000 | [diff] [blame] | 779 | #if defined(CONFIG_NET_MULTI) |
| 780 | printf ("Using %s device\n", eth_get_name()); |
wdenk | 6e59238 | 2004-04-18 17:39:38 +0000 | [diff] [blame] | 781 | #endif /* CONFIG_NET_MULTI */ |
Jean-Christophe PLAGNIOL-VILLARD | 079c2c4 | 2007-11-17 11:31:10 +0100 | [diff] [blame] | 782 | NetSetTimeout (10UL * CFG_HZ, PingTimeout); |
wdenk | 73a8b27 | 2003-06-05 19:27:42 +0000 | [diff] [blame] | 783 | NetSetHandler (PingHandler); |
| 784 | |
| 785 | PingSend(); |
| 786 | } |
Jon Loeliger | 610f2e9 | 2007-07-10 11:05:02 -0500 | [diff] [blame] | 787 | #endif |
wdenk | 2d96695 | 2002-10-31 22:12:35 +0000 | [diff] [blame] | 788 | |
Jon Loeliger | 643d1ab | 2007-07-09 17:45:14 -0500 | [diff] [blame] | 789 | #if defined(CONFIG_CMD_CDP) |
wdenk | a3d991b | 2004-04-15 21:48:45 +0000 | [diff] [blame] | 790 | |
| 791 | #define CDP_DEVICE_ID_TLV 0x0001 |
| 792 | #define CDP_ADDRESS_TLV 0x0002 |
| 793 | #define CDP_PORT_ID_TLV 0x0003 |
| 794 | #define CDP_CAPABILITIES_TLV 0x0004 |
| 795 | #define CDP_VERSION_TLV 0x0005 |
| 796 | #define CDP_PLATFORM_TLV 0x0006 |
| 797 | #define CDP_NATIVE_VLAN_TLV 0x000a |
| 798 | #define CDP_APPLIANCE_VLAN_TLV 0x000e |
| 799 | #define CDP_TRIGGER_TLV 0x000f |
| 800 | #define CDP_POWER_CONSUMPTION_TLV 0x0010 |
| 801 | #define CDP_SYSNAME_TLV 0x0014 |
| 802 | #define CDP_SYSOBJECT_TLV 0x0015 |
| 803 | #define CDP_MANAGEMENT_ADDRESS_TLV 0x0016 |
| 804 | |
| 805 | #define CDP_TIMEOUT (CFG_HZ/4) /* one packet every 250ms */ |
| 806 | |
| 807 | static int CDPSeq; |
| 808 | static int CDPOK; |
| 809 | |
| 810 | ushort CDPNativeVLAN; |
| 811 | ushort CDPApplianceVLAN; |
| 812 | |
| 813 | static const uchar CDP_SNAP_hdr[8] = { 0xAA, 0xAA, 0x03, 0x00, 0x00, 0x0C, 0x20, 0x00 }; |
| 814 | |
| 815 | static ushort CDP_compute_csum(const uchar *buff, ushort len) |
| 816 | { |
| 817 | ushort csum; |
| 818 | int odd; |
| 819 | ulong result = 0; |
| 820 | ushort leftover; |
Wolfgang Denk | 77ddac9 | 2005-10-13 16:45:02 +0200 | [diff] [blame] | 821 | ushort *p; |
wdenk | a3d991b | 2004-04-15 21:48:45 +0000 | [diff] [blame] | 822 | |
| 823 | if (len > 0) { |
| 824 | odd = 1 & (ulong)buff; |
| 825 | if (odd) { |
| 826 | result = *buff << 8; |
| 827 | len--; |
| 828 | buff++; |
| 829 | } |
| 830 | while (len > 1) { |
Wolfgang Denk | 77ddac9 | 2005-10-13 16:45:02 +0200 | [diff] [blame] | 831 | p = (ushort *)buff; |
| 832 | result += *p++; |
| 833 | buff = (uchar *)p; |
wdenk | a3d991b | 2004-04-15 21:48:45 +0000 | [diff] [blame] | 834 | if (result & 0x80000000) |
| 835 | result = (result & 0xFFFF) + (result >> 16); |
| 836 | len -= 2; |
| 837 | } |
| 838 | if (len) { |
| 839 | leftover = (signed short)(*(const signed char *)buff); |
Wolfgang Denk | 3ada834 | 2005-11-10 20:59:46 +0100 | [diff] [blame] | 840 | /* CISCO SUCKS big time! (and blows too): |
| 841 | * CDP uses the IP checksum algorithm with a twist; |
| 842 | * for the last byte it *sign* extends and sums. |
| 843 | */ |
wdenk | a3d991b | 2004-04-15 21:48:45 +0000 | [diff] [blame] | 844 | result = (result & 0xffff0000) | ((result + leftover) & 0x0000ffff); |
| 845 | } |
| 846 | while (result >> 16) |
| 847 | result = (result & 0xFFFF) + (result >> 16); |
| 848 | |
| 849 | if (odd) |
| 850 | result = ((result >> 8) & 0xff) | ((result & 0xff) << 8); |
| 851 | } |
| 852 | |
| 853 | /* add up 16-bit and 17-bit words for 17+c bits */ |
| 854 | result = (result & 0xffff) + (result >> 16); |
| 855 | /* add up 16-bit and 2-bit for 16+c bit */ |
| 856 | result = (result & 0xffff) + (result >> 16); |
| 857 | /* add up carry.. */ |
| 858 | result = (result & 0xffff) + (result >> 16); |
| 859 | |
| 860 | /* negate */ |
| 861 | csum = ~(ushort)result; |
| 862 | |
| 863 | /* run time endian detection */ |
| 864 | if (csum != htons(csum)) /* little endian */ |
| 865 | csum = htons(csum); |
| 866 | |
| 867 | return csum; |
| 868 | } |
| 869 | |
| 870 | int CDPSendTrigger(void) |
| 871 | { |
| 872 | volatile uchar *pkt; |
| 873 | volatile ushort *s; |
| 874 | volatile ushort *cp; |
| 875 | Ethernet_t *et; |
wdenk | a3d991b | 2004-04-15 21:48:45 +0000 | [diff] [blame] | 876 | int len; |
| 877 | ushort chksum; |
wdenk | 6e59238 | 2004-04-18 17:39:38 +0000 | [diff] [blame] | 878 | #if defined(CONFIG_CDP_DEVICE_ID) || defined(CONFIG_CDP_PORT_ID) || \ |
| 879 | defined(CONFIG_CDP_VERSION) || defined(CONFIG_CDP_PLATFORM) |
| 880 | char buf[32]; |
| 881 | #endif |
wdenk | a3d991b | 2004-04-15 21:48:45 +0000 | [diff] [blame] | 882 | |
| 883 | pkt = NetTxPacket; |
| 884 | et = (Ethernet_t *)pkt; |
| 885 | |
| 886 | /* NOTE: trigger sent not on any VLAN */ |
| 887 | |
| 888 | /* form ethernet header */ |
| 889 | memcpy(et->et_dest, NetCDPAddr, 6); |
| 890 | memcpy(et->et_src, NetOurEther, 6); |
| 891 | |
| 892 | pkt += ETHER_HDR_SIZE; |
| 893 | |
| 894 | /* SNAP header */ |
| 895 | memcpy((uchar *)pkt, CDP_SNAP_hdr, sizeof(CDP_SNAP_hdr)); |
| 896 | pkt += sizeof(CDP_SNAP_hdr); |
| 897 | |
| 898 | /* CDP header */ |
| 899 | *pkt++ = 0x02; /* CDP version 2 */ |
| 900 | *pkt++ = 180; /* TTL */ |
| 901 | s = (volatile ushort *)pkt; |
| 902 | cp = s; |
| 903 | *s++ = htons(0); /* checksum (0 for later calculation) */ |
| 904 | |
| 905 | /* CDP fields */ |
| 906 | #ifdef CONFIG_CDP_DEVICE_ID |
| 907 | *s++ = htons(CDP_DEVICE_ID_TLV); |
| 908 | *s++ = htons(CONFIG_CDP_DEVICE_ID); |
| 909 | memset(buf, 0, sizeof(buf)); |
| 910 | sprintf(buf, CONFIG_CDP_DEVICE_ID_PREFIX "%02X%02X%02X%02X%02X%02X", |
| 911 | NetOurEther[0] & 0xff, NetOurEther[1] & 0xff, |
| 912 | NetOurEther[2] & 0xff, NetOurEther[3] & 0xff, |
| 913 | NetOurEther[4] & 0xff, NetOurEther[5] & 0xff); |
| 914 | memcpy((uchar *)s, buf, 16); |
| 915 | s += 16 / 2; |
| 916 | #endif |
| 917 | |
| 918 | #ifdef CONFIG_CDP_PORT_ID |
| 919 | *s++ = htons(CDP_PORT_ID_TLV); |
| 920 | memset(buf, 0, sizeof(buf)); |
| 921 | sprintf(buf, CONFIG_CDP_PORT_ID, eth_get_dev_index()); |
| 922 | len = strlen(buf); |
| 923 | if (len & 1) /* make it even */ |
| 924 | len++; |
| 925 | *s++ = htons(len + 4); |
| 926 | memcpy((uchar *)s, buf, len); |
| 927 | s += len / 2; |
| 928 | #endif |
| 929 | |
| 930 | #ifdef CONFIG_CDP_CAPABILITIES |
| 931 | *s++ = htons(CDP_CAPABILITIES_TLV); |
| 932 | *s++ = htons(8); |
| 933 | *(ulong *)s = htonl(CONFIG_CDP_CAPABILITIES); |
| 934 | s += 2; |
| 935 | #endif |
| 936 | |
| 937 | #ifdef CONFIG_CDP_VERSION |
| 938 | *s++ = htons(CDP_VERSION_TLV); |
| 939 | memset(buf, 0, sizeof(buf)); |
| 940 | strcpy(buf, CONFIG_CDP_VERSION); |
| 941 | len = strlen(buf); |
| 942 | if (len & 1) /* make it even */ |
| 943 | len++; |
| 944 | *s++ = htons(len + 4); |
| 945 | memcpy((uchar *)s, buf, len); |
| 946 | s += len / 2; |
| 947 | #endif |
| 948 | |
| 949 | #ifdef CONFIG_CDP_PLATFORM |
| 950 | *s++ = htons(CDP_PLATFORM_TLV); |
| 951 | memset(buf, 0, sizeof(buf)); |
| 952 | strcpy(buf, CONFIG_CDP_PLATFORM); |
| 953 | len = strlen(buf); |
| 954 | if (len & 1) /* make it even */ |
| 955 | len++; |
| 956 | *s++ = htons(len + 4); |
| 957 | memcpy((uchar *)s, buf, len); |
| 958 | s += len / 2; |
| 959 | #endif |
| 960 | |
| 961 | #ifdef CONFIG_CDP_TRIGGER |
| 962 | *s++ = htons(CDP_TRIGGER_TLV); |
| 963 | *s++ = htons(8); |
| 964 | *(ulong *)s = htonl(CONFIG_CDP_TRIGGER); |
| 965 | s += 2; |
| 966 | #endif |
| 967 | |
| 968 | #ifdef CONFIG_CDP_POWER_CONSUMPTION |
| 969 | *s++ = htons(CDP_POWER_CONSUMPTION_TLV); |
| 970 | *s++ = htons(6); |
| 971 | *s++ = htons(CONFIG_CDP_POWER_CONSUMPTION); |
| 972 | #endif |
| 973 | |
| 974 | /* length of ethernet packet */ |
| 975 | len = (uchar *)s - ((uchar *)NetTxPacket + ETHER_HDR_SIZE); |
| 976 | et->et_protlen = htons(len); |
| 977 | |
| 978 | len = ETHER_HDR_SIZE + sizeof(CDP_SNAP_hdr); |
| 979 | chksum = CDP_compute_csum((uchar *)NetTxPacket + len, (uchar *)s - (NetTxPacket + len)); |
| 980 | if (chksum == 0) |
| 981 | chksum = 0xFFFF; |
| 982 | *cp = htons(chksum); |
| 983 | |
| 984 | (void) eth_send(NetTxPacket, (uchar *)s - NetTxPacket); |
| 985 | return 0; |
| 986 | } |
| 987 | |
| 988 | static void |
| 989 | CDPTimeout (void) |
| 990 | { |
| 991 | CDPSeq++; |
| 992 | |
| 993 | if (CDPSeq < 3) { |
| 994 | NetSetTimeout (CDP_TIMEOUT, CDPTimeout); |
| 995 | CDPSendTrigger(); |
| 996 | return; |
| 997 | } |
| 998 | |
| 999 | /* if not OK try again */ |
| 1000 | if (!CDPOK) |
| 1001 | NetStartAgain(); |
| 1002 | else |
| 1003 | NetState = NETLOOP_SUCCESS; |
| 1004 | } |
| 1005 | |
| 1006 | static void |
| 1007 | CDPDummyHandler (uchar * pkt, unsigned dest, unsigned src, unsigned len) |
| 1008 | { |
| 1009 | /* nothing */ |
| 1010 | } |
| 1011 | |
| 1012 | static void |
| 1013 | CDPHandler(const uchar * pkt, unsigned len) |
| 1014 | { |
| 1015 | const uchar *t; |
| 1016 | const ushort *ss; |
| 1017 | ushort type, tlen; |
| 1018 | uchar applid; |
| 1019 | ushort vlan, nvlan; |
| 1020 | |
| 1021 | /* minimum size? */ |
| 1022 | if (len < sizeof(CDP_SNAP_hdr) + 4) |
| 1023 | goto pkt_short; |
| 1024 | |
| 1025 | /* check for valid CDP SNAP header */ |
| 1026 | if (memcmp(pkt, CDP_SNAP_hdr, sizeof(CDP_SNAP_hdr)) != 0) |
| 1027 | return; |
| 1028 | |
| 1029 | pkt += sizeof(CDP_SNAP_hdr); |
| 1030 | len -= sizeof(CDP_SNAP_hdr); |
| 1031 | |
| 1032 | /* Version of CDP protocol must be >= 2 and TTL != 0 */ |
| 1033 | if (pkt[0] < 0x02 || pkt[1] == 0) |
| 1034 | return; |
| 1035 | |
| 1036 | /* if version is greater than 0x02 maybe we'll have a problem; output a warning */ |
| 1037 | if (pkt[0] != 0x02) |
| 1038 | printf("** WARNING: CDP packet received with a protocol version %d > 2\n", |
| 1039 | pkt[0] & 0xff); |
| 1040 | |
| 1041 | if (CDP_compute_csum(pkt, len) != 0) |
| 1042 | return; |
| 1043 | |
| 1044 | pkt += 4; |
| 1045 | len -= 4; |
| 1046 | |
| 1047 | vlan = htons(-1); |
| 1048 | nvlan = htons(-1); |
| 1049 | while (len > 0) { |
| 1050 | if (len < 4) |
| 1051 | goto pkt_short; |
| 1052 | |
| 1053 | ss = (const ushort *)pkt; |
| 1054 | type = ntohs(ss[0]); |
| 1055 | tlen = ntohs(ss[1]); |
| 1056 | if (tlen > len) { |
| 1057 | goto pkt_short; |
| 1058 | } |
| 1059 | |
| 1060 | pkt += tlen; |
| 1061 | len -= tlen; |
| 1062 | |
| 1063 | ss += 2; /* point ss to the data of the TLV */ |
| 1064 | tlen -= 4; |
| 1065 | |
| 1066 | switch (type) { |
| 1067 | case CDP_DEVICE_ID_TLV: |
| 1068 | break; |
| 1069 | case CDP_ADDRESS_TLV: |
| 1070 | break; |
| 1071 | case CDP_PORT_ID_TLV: |
| 1072 | break; |
| 1073 | case CDP_CAPABILITIES_TLV: |
| 1074 | break; |
| 1075 | case CDP_VERSION_TLV: |
| 1076 | break; |
| 1077 | case CDP_PLATFORM_TLV: |
| 1078 | break; |
| 1079 | case CDP_NATIVE_VLAN_TLV: |
| 1080 | nvlan = *ss; |
| 1081 | break; |
| 1082 | case CDP_APPLIANCE_VLAN_TLV: |
| 1083 | t = (const uchar *)ss; |
| 1084 | while (tlen > 0) { |
| 1085 | if (tlen < 3) |
| 1086 | goto pkt_short; |
| 1087 | |
| 1088 | applid = t[0]; |
| 1089 | ss = (const ushort *)(t + 1); |
| 1090 | |
| 1091 | #ifdef CONFIG_CDP_APPLIANCE_VLAN_TYPE |
| 1092 | if (applid == CONFIG_CDP_APPLIANCE_VLAN_TYPE) |
| 1093 | vlan = *ss; |
| 1094 | #else |
| 1095 | vlan = ntohs(*ss); /* XXX will this work; dunno */ |
| 1096 | #endif |
| 1097 | t += 3; tlen -= 3; |
| 1098 | } |
| 1099 | break; |
| 1100 | case CDP_TRIGGER_TLV: |
| 1101 | break; |
| 1102 | case CDP_POWER_CONSUMPTION_TLV: |
| 1103 | break; |
| 1104 | case CDP_SYSNAME_TLV: |
| 1105 | break; |
| 1106 | case CDP_SYSOBJECT_TLV: |
| 1107 | break; |
| 1108 | case CDP_MANAGEMENT_ADDRESS_TLV: |
| 1109 | break; |
| 1110 | } |
| 1111 | } |
| 1112 | |
| 1113 | CDPApplianceVLAN = vlan; |
| 1114 | CDPNativeVLAN = nvlan; |
| 1115 | |
| 1116 | CDPOK = 1; |
| 1117 | return; |
| 1118 | |
| 1119 | pkt_short: |
| 1120 | printf("** CDP packet is too short\n"); |
| 1121 | return; |
| 1122 | } |
| 1123 | |
| 1124 | static void CDPStart(void) |
| 1125 | { |
| 1126 | #if defined(CONFIG_NET_MULTI) |
| 1127 | printf ("Using %s device\n", eth_get_name()); |
| 1128 | #endif |
| 1129 | CDPSeq = 0; |
| 1130 | CDPOK = 0; |
| 1131 | |
| 1132 | CDPNativeVLAN = htons(-1); |
| 1133 | CDPApplianceVLAN = htons(-1); |
| 1134 | |
| 1135 | NetSetTimeout (CDP_TIMEOUT, CDPTimeout); |
| 1136 | NetSetHandler (CDPDummyHandler); |
| 1137 | |
| 1138 | CDPSendTrigger(); |
| 1139 | } |
Jon Loeliger | 610f2e9 | 2007-07-10 11:05:02 -0500 | [diff] [blame] | 1140 | #endif |
wdenk | a3d991b | 2004-04-15 21:48:45 +0000 | [diff] [blame] | 1141 | |
| 1142 | |
wdenk | 2d96695 | 2002-10-31 22:12:35 +0000 | [diff] [blame] | 1143 | void |
wdenk | a3d991b | 2004-04-15 21:48:45 +0000 | [diff] [blame] | 1144 | NetReceive(volatile uchar * inpkt, int len) |
wdenk | 2d96695 | 2002-10-31 22:12:35 +0000 | [diff] [blame] | 1145 | { |
| 1146 | Ethernet_t *et; |
| 1147 | IP_t *ip; |
| 1148 | ARP_t *arp; |
| 1149 | IPaddr_t tmp; |
| 1150 | int x; |
wdenk | a3d991b | 2004-04-15 21:48:45 +0000 | [diff] [blame] | 1151 | uchar *pkt; |
Jon Loeliger | 643d1ab | 2007-07-09 17:45:14 -0500 | [diff] [blame] | 1152 | #if defined(CONFIG_CMD_CDP) |
wdenk | a3d991b | 2004-04-15 21:48:45 +0000 | [diff] [blame] | 1153 | int iscdp; |
| 1154 | #endif |
| 1155 | ushort cti = 0, vlanid = VLAN_NONE, myvlanid, mynvlanid; |
wdenk | 2d96695 | 2002-10-31 22:12:35 +0000 | [diff] [blame] | 1156 | |
wdenk | a3d991b | 2004-04-15 21:48:45 +0000 | [diff] [blame] | 1157 | #ifdef ET_DEBUG |
| 1158 | printf("packet received\n"); |
| 1159 | #endif |
| 1160 | |
| 1161 | NetRxPkt = inpkt; |
wdenk | 2d96695 | 2002-10-31 22:12:35 +0000 | [diff] [blame] | 1162 | NetRxPktLen = len; |
wdenk | a3d991b | 2004-04-15 21:48:45 +0000 | [diff] [blame] | 1163 | et = (Ethernet_t *)inpkt; |
| 1164 | |
| 1165 | /* too small packet? */ |
| 1166 | if (len < ETHER_HDR_SIZE) |
| 1167 | return; |
| 1168 | |
Rafal Jaworowski | f85b607 | 2007-12-27 18:19:02 +0100 | [diff] [blame] | 1169 | #ifdef CONFIG_API |
| 1170 | if (push_packet) { |
| 1171 | (*push_packet)(inpkt, len); |
| 1172 | return; |
| 1173 | } |
| 1174 | #endif |
| 1175 | |
Jon Loeliger | 643d1ab | 2007-07-09 17:45:14 -0500 | [diff] [blame] | 1176 | #if defined(CONFIG_CMD_CDP) |
wdenk | a3d991b | 2004-04-15 21:48:45 +0000 | [diff] [blame] | 1177 | /* keep track if packet is CDP */ |
| 1178 | iscdp = memcmp(et->et_dest, NetCDPAddr, 6) == 0; |
| 1179 | #endif |
| 1180 | |
| 1181 | myvlanid = ntohs(NetOurVLAN); |
| 1182 | if (myvlanid == (ushort)-1) |
| 1183 | myvlanid = VLAN_NONE; |
| 1184 | mynvlanid = ntohs(NetOurNativeVLAN); |
| 1185 | if (mynvlanid == (ushort)-1) |
| 1186 | mynvlanid = VLAN_NONE; |
wdenk | 2d96695 | 2002-10-31 22:12:35 +0000 | [diff] [blame] | 1187 | |
| 1188 | x = ntohs(et->et_protlen); |
| 1189 | |
wdenk | a3d991b | 2004-04-15 21:48:45 +0000 | [diff] [blame] | 1190 | #ifdef ET_DEBUG |
| 1191 | printf("packet received\n"); |
| 1192 | #endif |
| 1193 | |
wdenk | 2d96695 | 2002-10-31 22:12:35 +0000 | [diff] [blame] | 1194 | if (x < 1514) { |
| 1195 | /* |
| 1196 | * Got a 802 packet. Check the other protocol field. |
| 1197 | */ |
| 1198 | x = ntohs(et->et_prot); |
wdenk | a3d991b | 2004-04-15 21:48:45 +0000 | [diff] [blame] | 1199 | |
| 1200 | ip = (IP_t *)(inpkt + E802_HDR_SIZE); |
wdenk | 2d96695 | 2002-10-31 22:12:35 +0000 | [diff] [blame] | 1201 | len -= E802_HDR_SIZE; |
wdenk | a3d991b | 2004-04-15 21:48:45 +0000 | [diff] [blame] | 1202 | |
| 1203 | } else if (x != PROT_VLAN) { /* normal packet */ |
| 1204 | ip = (IP_t *)(inpkt + ETHER_HDR_SIZE); |
wdenk | 2d96695 | 2002-10-31 22:12:35 +0000 | [diff] [blame] | 1205 | len -= ETHER_HDR_SIZE; |
wdenk | a3d991b | 2004-04-15 21:48:45 +0000 | [diff] [blame] | 1206 | |
| 1207 | } else { /* VLAN packet */ |
| 1208 | VLAN_Ethernet_t *vet = (VLAN_Ethernet_t *)et; |
| 1209 | |
| 1210 | #ifdef ET_DEBUG |
| 1211 | printf("VLAN packet received\n"); |
| 1212 | #endif |
| 1213 | /* too small packet? */ |
| 1214 | if (len < VLAN_ETHER_HDR_SIZE) |
| 1215 | return; |
| 1216 | |
| 1217 | /* if no VLAN active */ |
| 1218 | if ((ntohs(NetOurVLAN) & VLAN_IDMASK) == VLAN_NONE |
Jon Loeliger | 643d1ab | 2007-07-09 17:45:14 -0500 | [diff] [blame] | 1219 | #if defined(CONFIG_CMD_CDP) |
wdenk | a3d991b | 2004-04-15 21:48:45 +0000 | [diff] [blame] | 1220 | && iscdp == 0 |
| 1221 | #endif |
| 1222 | ) |
| 1223 | return; |
| 1224 | |
| 1225 | cti = ntohs(vet->vet_tag); |
| 1226 | vlanid = cti & VLAN_IDMASK; |
| 1227 | x = ntohs(vet->vet_type); |
| 1228 | |
| 1229 | ip = (IP_t *)(inpkt + VLAN_ETHER_HDR_SIZE); |
| 1230 | len -= VLAN_ETHER_HDR_SIZE; |
wdenk | 2d96695 | 2002-10-31 22:12:35 +0000 | [diff] [blame] | 1231 | } |
| 1232 | |
| 1233 | #ifdef ET_DEBUG |
| 1234 | printf("Receive from protocol 0x%x\n", x); |
| 1235 | #endif |
| 1236 | |
Jon Loeliger | 643d1ab | 2007-07-09 17:45:14 -0500 | [diff] [blame] | 1237 | #if defined(CONFIG_CMD_CDP) |
wdenk | a3d991b | 2004-04-15 21:48:45 +0000 | [diff] [blame] | 1238 | if (iscdp) { |
| 1239 | CDPHandler((uchar *)ip, len); |
| 1240 | return; |
| 1241 | } |
| 1242 | #endif |
| 1243 | |
| 1244 | if ((myvlanid & VLAN_IDMASK) != VLAN_NONE) { |
| 1245 | if (vlanid == VLAN_NONE) |
| 1246 | vlanid = (mynvlanid & VLAN_IDMASK); |
| 1247 | /* not matched? */ |
| 1248 | if (vlanid != (myvlanid & VLAN_IDMASK)) |
| 1249 | return; |
| 1250 | } |
| 1251 | |
wdenk | 2d96695 | 2002-10-31 22:12:35 +0000 | [diff] [blame] | 1252 | switch (x) { |
| 1253 | |
| 1254 | case PROT_ARP: |
| 1255 | /* |
| 1256 | * We have to deal with two types of ARP packets: |
wdenk | 8bde7f7 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 1257 | * - REQUEST packets will be answered by sending our |
| 1258 | * IP address - if we know it. |
| 1259 | * - REPLY packates are expected only after we asked |
| 1260 | * for the TFTP server's or the gateway's ethernet |
| 1261 | * address; so if we receive such a packet, we set |
| 1262 | * the server ethernet address |
wdenk | 2d96695 | 2002-10-31 22:12:35 +0000 | [diff] [blame] | 1263 | */ |
| 1264 | #ifdef ET_DEBUG |
wdenk | 4b9206e | 2004-03-23 22:14:11 +0000 | [diff] [blame] | 1265 | puts ("Got ARP\n"); |
wdenk | 2d96695 | 2002-10-31 22:12:35 +0000 | [diff] [blame] | 1266 | #endif |
| 1267 | arp = (ARP_t *)ip; |
| 1268 | if (len < ARP_HDR_SIZE) { |
| 1269 | printf("bad length %d < %d\n", len, ARP_HDR_SIZE); |
| 1270 | return; |
| 1271 | } |
| 1272 | if (ntohs(arp->ar_hrd) != ARP_ETHER) { |
| 1273 | return; |
| 1274 | } |
| 1275 | if (ntohs(arp->ar_pro) != PROT_IP) { |
| 1276 | return; |
| 1277 | } |
| 1278 | if (arp->ar_hln != 6) { |
| 1279 | return; |
| 1280 | } |
| 1281 | if (arp->ar_pln != 4) { |
| 1282 | return; |
| 1283 | } |
| 1284 | |
| 1285 | if (NetOurIP == 0) { |
| 1286 | return; |
| 1287 | } |
| 1288 | |
| 1289 | if (NetReadIP(&arp->ar_data[16]) != NetOurIP) { |
| 1290 | return; |
| 1291 | } |
| 1292 | |
| 1293 | switch (ntohs(arp->ar_op)) { |
| 1294 | case ARPOP_REQUEST: /* reply with our IP address */ |
| 1295 | #ifdef ET_DEBUG |
wdenk | 4b9206e | 2004-03-23 22:14:11 +0000 | [diff] [blame] | 1296 | puts ("Got ARP REQUEST, return our IP\n"); |
wdenk | 2d96695 | 2002-10-31 22:12:35 +0000 | [diff] [blame] | 1297 | #endif |
wdenk | a3d991b | 2004-04-15 21:48:45 +0000 | [diff] [blame] | 1298 | pkt = (uchar *)et; |
| 1299 | pkt += NetSetEther(pkt, et->et_src, PROT_ARP); |
wdenk | 2d96695 | 2002-10-31 22:12:35 +0000 | [diff] [blame] | 1300 | arp->ar_op = htons(ARPOP_REPLY); |
| 1301 | memcpy (&arp->ar_data[10], &arp->ar_data[0], 6); |
| 1302 | NetCopyIP(&arp->ar_data[16], &arp->ar_data[6]); |
| 1303 | memcpy (&arp->ar_data[ 0], NetOurEther, 6); |
| 1304 | NetCopyIP(&arp->ar_data[ 6], &NetOurIP); |
wdenk | a3d991b | 2004-04-15 21:48:45 +0000 | [diff] [blame] | 1305 | (void) eth_send((uchar *)et, (pkt - (uchar *)et) + ARP_HDR_SIZE); |
wdenk | 2d96695 | 2002-10-31 22:12:35 +0000 | [diff] [blame] | 1306 | return; |
wdenk | 73a8b27 | 2003-06-05 19:27:42 +0000 | [diff] [blame] | 1307 | |
| 1308 | case ARPOP_REPLY: /* arp reply */ |
| 1309 | /* are we waiting for a reply */ |
| 1310 | if (!NetArpWaitPacketIP || !NetArpWaitPacketMAC) |
| 1311 | break; |
wdenk | 2d96695 | 2002-10-31 22:12:35 +0000 | [diff] [blame] | 1312 | #ifdef ET_DEBUG |
wdenk | 73a8b27 | 2003-06-05 19:27:42 +0000 | [diff] [blame] | 1313 | printf("Got ARP REPLY, set server/gtwy eth addr (%02x:%02x:%02x:%02x:%02x:%02x)\n", |
| 1314 | arp->ar_data[0], arp->ar_data[1], |
| 1315 | arp->ar_data[2], arp->ar_data[3], |
| 1316 | arp->ar_data[4], arp->ar_data[5]); |
wdenk | 2d96695 | 2002-10-31 22:12:35 +0000 | [diff] [blame] | 1317 | #endif |
wdenk | 73a8b27 | 2003-06-05 19:27:42 +0000 | [diff] [blame] | 1318 | |
| 1319 | tmp = NetReadIP(&arp->ar_data[6]); |
| 1320 | |
| 1321 | /* matched waiting packet's address */ |
| 1322 | if (tmp == NetArpWaitReplyIP) { |
| 1323 | #ifdef ET_DEBUG |
wdenk | 4b9206e | 2004-03-23 22:14:11 +0000 | [diff] [blame] | 1324 | puts ("Got it\n"); |
wdenk | 73a8b27 | 2003-06-05 19:27:42 +0000 | [diff] [blame] | 1325 | #endif |
| 1326 | /* save address for later use */ |
| 1327 | memcpy(NetArpWaitPacketMAC, &arp->ar_data[0], 6); |
| 1328 | |
wdenk | 68ceb29 | 2004-08-02 21:11:11 +0000 | [diff] [blame] | 1329 | #ifdef CONFIG_NETCONSOLE |
| 1330 | (*packetHandler)(0,0,0,0); |
| 1331 | #endif |
wdenk | 73a8b27 | 2003-06-05 19:27:42 +0000 | [diff] [blame] | 1332 | /* modify header, and transmit it */ |
| 1333 | memcpy(((Ethernet_t *)NetArpWaitTxPacket)->et_dest, NetArpWaitPacketMAC, 6); |
| 1334 | (void) eth_send(NetArpWaitTxPacket, NetArpWaitTxPacketSize); |
| 1335 | |
| 1336 | /* no arp request pending now */ |
| 1337 | NetArpWaitPacketIP = 0; |
| 1338 | NetArpWaitTxPacketSize = 0; |
| 1339 | NetArpWaitPacketMAC = NULL; |
| 1340 | |
| 1341 | } |
wdenk | 2d96695 | 2002-10-31 22:12:35 +0000 | [diff] [blame] | 1342 | return; |
| 1343 | default: |
| 1344 | #ifdef ET_DEBUG |
| 1345 | printf("Unexpected ARP opcode 0x%x\n", ntohs(arp->ar_op)); |
| 1346 | #endif |
| 1347 | return; |
| 1348 | } |
wdenk | 289f932 | 2005-01-12 00:15:14 +0000 | [diff] [blame] | 1349 | break; |
wdenk | 2d96695 | 2002-10-31 22:12:35 +0000 | [diff] [blame] | 1350 | |
| 1351 | case PROT_RARP: |
| 1352 | #ifdef ET_DEBUG |
wdenk | 4b9206e | 2004-03-23 22:14:11 +0000 | [diff] [blame] | 1353 | puts ("Got RARP\n"); |
wdenk | 2d96695 | 2002-10-31 22:12:35 +0000 | [diff] [blame] | 1354 | #endif |
| 1355 | arp = (ARP_t *)ip; |
| 1356 | if (len < ARP_HDR_SIZE) { |
| 1357 | printf("bad length %d < %d\n", len, ARP_HDR_SIZE); |
| 1358 | return; |
| 1359 | } |
| 1360 | |
| 1361 | if ((ntohs(arp->ar_op) != RARPOP_REPLY) || |
| 1362 | (ntohs(arp->ar_hrd) != ARP_ETHER) || |
| 1363 | (ntohs(arp->ar_pro) != PROT_IP) || |
| 1364 | (arp->ar_hln != 6) || (arp->ar_pln != 4)) { |
| 1365 | |
wdenk | 4b9206e | 2004-03-23 22:14:11 +0000 | [diff] [blame] | 1366 | puts ("invalid RARP header\n"); |
wdenk | 2d96695 | 2002-10-31 22:12:35 +0000 | [diff] [blame] | 1367 | } else { |
| 1368 | NetCopyIP(&NetOurIP, &arp->ar_data[16]); |
wdenk | 3d3befa | 2004-03-14 15:06:13 +0000 | [diff] [blame] | 1369 | if (NetServerIP == 0) |
| 1370 | NetCopyIP(&NetServerIP, &arp->ar_data[ 6]); |
wdenk | 2d96695 | 2002-10-31 22:12:35 +0000 | [diff] [blame] | 1371 | memcpy (NetServerEther, &arp->ar_data[ 0], 6); |
| 1372 | |
| 1373 | (*packetHandler)(0,0,0,0); |
| 1374 | } |
| 1375 | break; |
| 1376 | |
| 1377 | case PROT_IP: |
| 1378 | #ifdef ET_DEBUG |
wdenk | 4b9206e | 2004-03-23 22:14:11 +0000 | [diff] [blame] | 1379 | puts ("Got IP\n"); |
wdenk | 2d96695 | 2002-10-31 22:12:35 +0000 | [diff] [blame] | 1380 | #endif |
| 1381 | if (len < IP_HDR_SIZE) { |
| 1382 | debug ("len bad %d < %d\n", len, IP_HDR_SIZE); |
| 1383 | return; |
| 1384 | } |
| 1385 | if (len < ntohs(ip->ip_len)) { |
| 1386 | printf("len bad %d < %d\n", len, ntohs(ip->ip_len)); |
| 1387 | return; |
| 1388 | } |
| 1389 | len = ntohs(ip->ip_len); |
| 1390 | #ifdef ET_DEBUG |
| 1391 | printf("len=%d, v=%02x\n", len, ip->ip_hl_v & 0xff); |
| 1392 | #endif |
| 1393 | if ((ip->ip_hl_v & 0xf0) != 0x40) { |
| 1394 | return; |
| 1395 | } |
| 1396 | if (ip->ip_off & htons(0x1fff)) { /* Can't deal w/ fragments */ |
| 1397 | return; |
| 1398 | } |
| 1399 | if (!NetCksumOk((uchar *)ip, IP_HDR_SIZE_NO_UDP / 2)) { |
wdenk | 4b9206e | 2004-03-23 22:14:11 +0000 | [diff] [blame] | 1400 | puts ("checksum bad\n"); |
wdenk | 2d96695 | 2002-10-31 22:12:35 +0000 | [diff] [blame] | 1401 | return; |
| 1402 | } |
| 1403 | tmp = NetReadIP(&ip->ip_dst); |
| 1404 | if (NetOurIP && tmp != NetOurIP && tmp != 0xFFFFFFFF) { |
David Updegraff | 53a5c42 | 2007-06-11 10:41:07 -0500 | [diff] [blame] | 1405 | #ifdef CONFIG_MCAST_TFTP |
Wolfgang Denk | 85eb5ca | 2007-08-14 09:47:27 +0200 | [diff] [blame] | 1406 | if (Mcast_addr != tmp) |
David Updegraff | 53a5c42 | 2007-06-11 10:41:07 -0500 | [diff] [blame] | 1407 | #endif |
wdenk | 2d96695 | 2002-10-31 22:12:35 +0000 | [diff] [blame] | 1408 | return; |
| 1409 | } |
| 1410 | /* |
| 1411 | * watch for ICMP host redirects |
| 1412 | * |
wdenk | 8bde7f7 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 1413 | * There is no real handler code (yet). We just watch |
| 1414 | * for ICMP host redirect messages. In case anybody |
| 1415 | * sees these messages: please contact me |
| 1416 | * (wd@denx.de), or - even better - send me the |
| 1417 | * necessary fixes :-) |
wdenk | 2d96695 | 2002-10-31 22:12:35 +0000 | [diff] [blame] | 1418 | * |
wdenk | 8bde7f7 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 1419 | * Note: in all cases where I have seen this so far |
| 1420 | * it was a problem with the router configuration, |
| 1421 | * for instance when a router was configured in the |
| 1422 | * BOOTP reply, but the TFTP server was on the same |
| 1423 | * subnet. So this is probably a warning that your |
| 1424 | * configuration might be wrong. But I'm not really |
| 1425 | * sure if there aren't any other situations. |
wdenk | 2d96695 | 2002-10-31 22:12:35 +0000 | [diff] [blame] | 1426 | */ |
| 1427 | if (ip->ip_p == IPPROTO_ICMP) { |
| 1428 | ICMP_t *icmph = (ICMP_t *)&(ip->udp_src); |
| 1429 | |
wdenk | 73a8b27 | 2003-06-05 19:27:42 +0000 | [diff] [blame] | 1430 | switch (icmph->type) { |
| 1431 | case ICMP_REDIRECT: |
wdenk | 90dc670 | 2005-05-03 14:12:25 +0000 | [diff] [blame] | 1432 | if (icmph->code != ICMP_REDIR_HOST) |
| 1433 | return; |
| 1434 | puts (" ICMP Host Redirect to "); |
| 1435 | print_IPaddr(icmph->un.gateway); |
| 1436 | putc(' '); |
Stefan Roese | 8534bf9 | 2005-08-12 20:06:52 +0200 | [diff] [blame] | 1437 | return; |
Jon Loeliger | 643d1ab | 2007-07-09 17:45:14 -0500 | [diff] [blame] | 1438 | #if defined(CONFIG_CMD_PING) |
wdenk | 73a8b27 | 2003-06-05 19:27:42 +0000 | [diff] [blame] | 1439 | case ICMP_ECHO_REPLY: |
| 1440 | /* |
| 1441 | * IP header OK. Pass the packet to the current handler. |
| 1442 | */ |
| 1443 | /* XXX point to ip packet */ |
| 1444 | (*packetHandler)((uchar *)ip, 0, 0, 0); |
Stefan Roese | 8534bf9 | 2005-08-12 20:06:52 +0200 | [diff] [blame] | 1445 | return; |
Ed Swarthout | 8385317 | 2007-03-07 12:14:50 -0600 | [diff] [blame] | 1446 | case ICMP_ECHO_REQUEST: |
| 1447 | #ifdef ET_DEBUG |
| 1448 | printf ("Got ICMP ECHO REQUEST, return %d bytes \n", |
| 1449 | ETHER_HDR_SIZE + len); |
| 1450 | #endif |
| 1451 | memcpy (&et->et_dest[0], &et->et_src[0], 6); |
| 1452 | memcpy (&et->et_src[ 0], NetOurEther, 6); |
| 1453 | |
| 1454 | ip->ip_sum = 0; |
| 1455 | ip->ip_off = 0; |
| 1456 | NetCopyIP((void*)&ip->ip_dst, &ip->ip_src); |
| 1457 | NetCopyIP((void*)&ip->ip_src, &NetOurIP); |
| 1458 | ip->ip_sum = ~NetCksum((uchar *)ip, IP_HDR_SIZE_NO_UDP >> 1); |
| 1459 | |
| 1460 | icmph->type = ICMP_ECHO_REPLY; |
| 1461 | icmph->checksum = 0; |
| 1462 | icmph->checksum = ~NetCksum((uchar *)icmph, |
| 1463 | (len - IP_HDR_SIZE_NO_UDP) >> 1); |
| 1464 | (void) eth_send((uchar *)et, ETHER_HDR_SIZE + len); |
| 1465 | return; |
wdenk | 73a8b27 | 2003-06-05 19:27:42 +0000 | [diff] [blame] | 1466 | #endif |
| 1467 | default: |
| 1468 | return; |
| 1469 | } |
wdenk | 2d96695 | 2002-10-31 22:12:35 +0000 | [diff] [blame] | 1470 | } else if (ip->ip_p != IPPROTO_UDP) { /* Only UDP packets */ |
| 1471 | return; |
| 1472 | } |
| 1473 | |
Stefan Roese | 8534bf9 | 2005-08-12 20:06:52 +0200 | [diff] [blame] | 1474 | #ifdef CONFIG_UDP_CHECKSUM |
| 1475 | if (ip->udp_xsum != 0) { |
Wolfgang Denk | b2f5080 | 2005-08-12 23:43:12 +0200 | [diff] [blame] | 1476 | ulong xsum; |
Stefan Roese | 8534bf9 | 2005-08-12 20:06:52 +0200 | [diff] [blame] | 1477 | ushort *sumptr; |
| 1478 | ushort sumlen; |
| 1479 | |
| 1480 | xsum = ip->ip_p; |
| 1481 | xsum += (ntohs(ip->udp_len)); |
| 1482 | xsum += (ntohl(ip->ip_src) >> 16) & 0x0000ffff; |
| 1483 | xsum += (ntohl(ip->ip_src) >> 0) & 0x0000ffff; |
| 1484 | xsum += (ntohl(ip->ip_dst) >> 16) & 0x0000ffff; |
| 1485 | xsum += (ntohl(ip->ip_dst) >> 0) & 0x0000ffff; |
| 1486 | |
| 1487 | sumlen = ntohs(ip->udp_len); |
| 1488 | sumptr = (ushort *) &(ip->udp_src); |
| 1489 | |
| 1490 | while (sumlen > 1) { |
Wolfgang Denk | b2f5080 | 2005-08-12 23:43:12 +0200 | [diff] [blame] | 1491 | ushort sumdata; |
Stefan Roese | 8534bf9 | 2005-08-12 20:06:52 +0200 | [diff] [blame] | 1492 | |
| 1493 | sumdata = *sumptr++; |
| 1494 | xsum += ntohs(sumdata); |
| 1495 | sumlen -= 2; |
| 1496 | } |
| 1497 | if (sumlen > 0) { |
Wolfgang Denk | b2f5080 | 2005-08-12 23:43:12 +0200 | [diff] [blame] | 1498 | ushort sumdata; |
Stefan Roese | 8534bf9 | 2005-08-12 20:06:52 +0200 | [diff] [blame] | 1499 | |
| 1500 | sumdata = *(unsigned char *) sumptr; |
Wolfgang Denk | b2f5080 | 2005-08-12 23:43:12 +0200 | [diff] [blame] | 1501 | sumdata = (sumdata << 8) & 0xff00; |
Stefan Roese | 8534bf9 | 2005-08-12 20:06:52 +0200 | [diff] [blame] | 1502 | xsum += sumdata; |
| 1503 | } |
| 1504 | while ((xsum >> 16) != 0) { |
Wolfgang Denk | b2f5080 | 2005-08-12 23:43:12 +0200 | [diff] [blame] | 1505 | xsum = (xsum & 0x0000ffff) + ((xsum >> 16) & 0x0000ffff); |
Stefan Roese | 8534bf9 | 2005-08-12 20:06:52 +0200 | [diff] [blame] | 1506 | } |
| 1507 | if ((xsum != 0x00000000) && (xsum != 0x0000ffff)) { |
Wolfgang Denk | b2f5080 | 2005-08-12 23:43:12 +0200 | [diff] [blame] | 1508 | printf(" UDP wrong checksum %08x %08x\n", xsum, ntohs(ip->udp_xsum)); |
Stefan Roese | 8534bf9 | 2005-08-12 20:06:52 +0200 | [diff] [blame] | 1509 | return; |
| 1510 | } |
| 1511 | } |
| 1512 | #endif |
| 1513 | |
David Updegraff | 53a5c42 | 2007-06-11 10:41:07 -0500 | [diff] [blame] | 1514 | |
wdenk | 68ceb29 | 2004-08-02 21:11:11 +0000 | [diff] [blame] | 1515 | #ifdef CONFIG_NETCONSOLE |
| 1516 | nc_input_packet((uchar *)ip +IP_HDR_SIZE, |
| 1517 | ntohs(ip->udp_dst), |
| 1518 | ntohs(ip->udp_src), |
| 1519 | ntohs(ip->udp_len) - 8); |
| 1520 | #endif |
wdenk | 2d96695 | 2002-10-31 22:12:35 +0000 | [diff] [blame] | 1521 | /* |
| 1522 | * IP header OK. Pass the packet to the current handler. |
| 1523 | */ |
| 1524 | (*packetHandler)((uchar *)ip +IP_HDR_SIZE, |
| 1525 | ntohs(ip->udp_dst), |
| 1526 | ntohs(ip->udp_src), |
| 1527 | ntohs(ip->udp_len) - 8); |
wdenk | 2d96695 | 2002-10-31 22:12:35 +0000 | [diff] [blame] | 1528 | break; |
| 1529 | } |
| 1530 | } |
| 1531 | |
| 1532 | |
| 1533 | /**********************************************************************/ |
| 1534 | |
| 1535 | static int net_check_prereq (proto_t protocol) |
| 1536 | { |
| 1537 | switch (protocol) { |
wdenk | 6e59238 | 2004-04-18 17:39:38 +0000 | [diff] [blame] | 1538 | /* Fall through */ |
Jon Loeliger | 643d1ab | 2007-07-09 17:45:14 -0500 | [diff] [blame] | 1539 | #if defined(CONFIG_CMD_PING) |
wdenk | 73a8b27 | 2003-06-05 19:27:42 +0000 | [diff] [blame] | 1540 | case PING: |
wdenk | 6e59238 | 2004-04-18 17:39:38 +0000 | [diff] [blame] | 1541 | if (NetPingIP == 0) { |
| 1542 | puts ("*** ERROR: ping address not given\n"); |
| 1543 | return (1); |
| 1544 | } |
| 1545 | goto common; |
wdenk | 73a8b27 | 2003-06-05 19:27:42 +0000 | [diff] [blame] | 1546 | #endif |
Jon Loeliger | 643d1ab | 2007-07-09 17:45:14 -0500 | [diff] [blame] | 1547 | #if defined(CONFIG_CMD_SNTP) |
wdenk | ea287de | 2005-04-01 00:25:43 +0000 | [diff] [blame] | 1548 | case SNTP: |
| 1549 | if (NetNtpServerIP == 0) { |
| 1550 | puts ("*** ERROR: NTP server address not given\n"); |
| 1551 | return (1); |
| 1552 | } |
| 1553 | goto common; |
| 1554 | #endif |
Jon Loeliger | 643d1ab | 2007-07-09 17:45:14 -0500 | [diff] [blame] | 1555 | #if defined(CONFIG_CMD_NFS) |
wdenk | cbd8a35 | 2004-02-24 02:00:03 +0000 | [diff] [blame] | 1556 | case NFS: |
| 1557 | #endif |
wdenk | 68ceb29 | 2004-08-02 21:11:11 +0000 | [diff] [blame] | 1558 | case NETCONS: |
wdenk | 2d96695 | 2002-10-31 22:12:35 +0000 | [diff] [blame] | 1559 | case TFTP: |
wdenk | 6e59238 | 2004-04-18 17:39:38 +0000 | [diff] [blame] | 1560 | if (NetServerIP == 0) { |
| 1561 | puts ("*** ERROR: `serverip' not set\n"); |
| 1562 | return (1); |
| 1563 | } |
Jon Loeliger | 643d1ab | 2007-07-09 17:45:14 -0500 | [diff] [blame] | 1564 | #if defined(CONFIG_CMD_PING) || defined(CONFIG_CMD_SNTP) |
Wolfgang Denk | b2f5080 | 2005-08-12 23:43:12 +0200 | [diff] [blame] | 1565 | common: |
wdenk | 73a8b27 | 2003-06-05 19:27:42 +0000 | [diff] [blame] | 1566 | #endif |
| 1567 | |
wdenk | 6e59238 | 2004-04-18 17:39:38 +0000 | [diff] [blame] | 1568 | if (NetOurIP == 0) { |
| 1569 | puts ("*** ERROR: `ipaddr' not set\n"); |
| 1570 | return (1); |
| 1571 | } |
| 1572 | /* Fall through */ |
wdenk | 2d96695 | 2002-10-31 22:12:35 +0000 | [diff] [blame] | 1573 | |
| 1574 | case DHCP: |
| 1575 | case RARP: |
| 1576 | case BOOTP: |
wdenk | a3d991b | 2004-04-15 21:48:45 +0000 | [diff] [blame] | 1577 | case CDP: |
wdenk | 6e59238 | 2004-04-18 17:39:38 +0000 | [diff] [blame] | 1578 | if (memcmp (NetOurEther, "\0\0\0\0\0\0", 6) == 0) { |
wdenk | 2d96695 | 2002-10-31 22:12:35 +0000 | [diff] [blame] | 1579 | #ifdef CONFIG_NET_MULTI |
wdenk | 6e59238 | 2004-04-18 17:39:38 +0000 | [diff] [blame] | 1580 | extern int eth_get_dev_index (void); |
| 1581 | int num = eth_get_dev_index (); |
wdenk | 2d96695 | 2002-10-31 22:12:35 +0000 | [diff] [blame] | 1582 | |
wdenk | 6e59238 | 2004-04-18 17:39:38 +0000 | [diff] [blame] | 1583 | switch (num) { |
| 1584 | case -1: |
wdenk | 2d96695 | 2002-10-31 22:12:35 +0000 | [diff] [blame] | 1585 | puts ("*** ERROR: No ethernet found.\n"); |
| 1586 | return (1); |
wdenk | 6e59238 | 2004-04-18 17:39:38 +0000 | [diff] [blame] | 1587 | case 0: |
wdenk | 2d96695 | 2002-10-31 22:12:35 +0000 | [diff] [blame] | 1588 | puts ("*** ERROR: `ethaddr' not set\n"); |
| 1589 | break; |
wdenk | 6e59238 | 2004-04-18 17:39:38 +0000 | [diff] [blame] | 1590 | default: |
wdenk | 8bde7f7 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 1591 | printf ("*** ERROR: `eth%daddr' not set\n", |
wdenk | 2d96695 | 2002-10-31 22:12:35 +0000 | [diff] [blame] | 1592 | num); |
| 1593 | break; |
wdenk | 2d96695 | 2002-10-31 22:12:35 +0000 | [diff] [blame] | 1594 | } |
wdenk | 6e59238 | 2004-04-18 17:39:38 +0000 | [diff] [blame] | 1595 | |
| 1596 | NetStartAgain (); |
| 1597 | return (2); |
| 1598 | #else |
| 1599 | puts ("*** ERROR: `ethaddr' not set\n"); |
| 1600 | return (1); |
| 1601 | #endif |
| 1602 | } |
| 1603 | /* Fall through */ |
| 1604 | default: |
| 1605 | return (0); |
wdenk | 2d96695 | 2002-10-31 22:12:35 +0000 | [diff] [blame] | 1606 | } |
wdenk | 6e59238 | 2004-04-18 17:39:38 +0000 | [diff] [blame] | 1607 | return (0); /* OK */ |
wdenk | 2d96695 | 2002-10-31 22:12:35 +0000 | [diff] [blame] | 1608 | } |
| 1609 | /**********************************************************************/ |
| 1610 | |
| 1611 | int |
| 1612 | NetCksumOk(uchar * ptr, int len) |
| 1613 | { |
| 1614 | return !((NetCksum(ptr, len) + 1) & 0xfffe); |
| 1615 | } |
| 1616 | |
| 1617 | |
| 1618 | unsigned |
| 1619 | NetCksum(uchar * ptr, int len) |
| 1620 | { |
| 1621 | ulong xsum; |
Stefan Roese | 9d2a873 | 2005-08-31 12:55:50 +0200 | [diff] [blame] | 1622 | ushort *p = (ushort *)ptr; |
wdenk | 2d96695 | 2002-10-31 22:12:35 +0000 | [diff] [blame] | 1623 | |
| 1624 | xsum = 0; |
| 1625 | while (len-- > 0) |
Wolfgang Denk | 7bc5ee0 | 2005-08-26 01:36:03 +0200 | [diff] [blame] | 1626 | xsum += *p++; |
wdenk | 2d96695 | 2002-10-31 22:12:35 +0000 | [diff] [blame] | 1627 | xsum = (xsum & 0xffff) + (xsum >> 16); |
| 1628 | xsum = (xsum & 0xffff) + (xsum >> 16); |
| 1629 | return (xsum & 0xffff); |
| 1630 | } |
| 1631 | |
wdenk | a3d991b | 2004-04-15 21:48:45 +0000 | [diff] [blame] | 1632 | int |
| 1633 | NetEthHdrSize(void) |
| 1634 | { |
| 1635 | ushort myvlanid; |
wdenk | 2d96695 | 2002-10-31 22:12:35 +0000 | [diff] [blame] | 1636 | |
wdenk | a3d991b | 2004-04-15 21:48:45 +0000 | [diff] [blame] | 1637 | myvlanid = ntohs(NetOurVLAN); |
| 1638 | if (myvlanid == (ushort)-1) |
| 1639 | myvlanid = VLAN_NONE; |
| 1640 | |
| 1641 | return ((myvlanid & VLAN_IDMASK) == VLAN_NONE) ? ETHER_HDR_SIZE : VLAN_ETHER_HDR_SIZE; |
| 1642 | } |
| 1643 | |
| 1644 | int |
wdenk | 2d96695 | 2002-10-31 22:12:35 +0000 | [diff] [blame] | 1645 | NetSetEther(volatile uchar * xet, uchar * addr, uint prot) |
| 1646 | { |
| 1647 | Ethernet_t *et = (Ethernet_t *)xet; |
wdenk | a3d991b | 2004-04-15 21:48:45 +0000 | [diff] [blame] | 1648 | ushort myvlanid; |
| 1649 | |
| 1650 | myvlanid = ntohs(NetOurVLAN); |
| 1651 | if (myvlanid == (ushort)-1) |
| 1652 | myvlanid = VLAN_NONE; |
wdenk | 2d96695 | 2002-10-31 22:12:35 +0000 | [diff] [blame] | 1653 | |
| 1654 | memcpy (et->et_dest, addr, 6); |
| 1655 | memcpy (et->et_src, NetOurEther, 6); |
wdenk | a3d991b | 2004-04-15 21:48:45 +0000 | [diff] [blame] | 1656 | if ((myvlanid & VLAN_IDMASK) == VLAN_NONE) { |
wdenk | 2d96695 | 2002-10-31 22:12:35 +0000 | [diff] [blame] | 1657 | et->et_protlen = htons(prot); |
wdenk | a3d991b | 2004-04-15 21:48:45 +0000 | [diff] [blame] | 1658 | return ETHER_HDR_SIZE; |
| 1659 | } else { |
| 1660 | VLAN_Ethernet_t *vet = (VLAN_Ethernet_t *)xet; |
wdenk | 2d96695 | 2002-10-31 22:12:35 +0000 | [diff] [blame] | 1661 | |
wdenk | a3d991b | 2004-04-15 21:48:45 +0000 | [diff] [blame] | 1662 | vet->vet_vlan_type = htons(PROT_VLAN); |
| 1663 | vet->vet_tag = htons((0 << 5) | (myvlanid & VLAN_IDMASK)); |
| 1664 | vet->vet_type = htons(prot); |
| 1665 | return VLAN_ETHER_HDR_SIZE; |
| 1666 | } |
| 1667 | } |
wdenk | 2d96695 | 2002-10-31 22:12:35 +0000 | [diff] [blame] | 1668 | |
| 1669 | void |
| 1670 | NetSetIP(volatile uchar * xip, IPaddr_t dest, int dport, int sport, int len) |
| 1671 | { |
| 1672 | volatile IP_t *ip = (IP_t *)xip; |
| 1673 | |
| 1674 | /* |
| 1675 | * If the data is an odd number of bytes, zero the |
| 1676 | * byte after the last byte so that the checksum |
| 1677 | * will work. |
| 1678 | */ |
| 1679 | if (len & 1) |
| 1680 | xip[IP_HDR_SIZE + len] = 0; |
| 1681 | |
| 1682 | /* |
| 1683 | * Construct an IP and UDP header. |
wdenk | 6e59238 | 2004-04-18 17:39:38 +0000 | [diff] [blame] | 1684 | * (need to set no fragment bit - XXX) |
wdenk | 2d96695 | 2002-10-31 22:12:35 +0000 | [diff] [blame] | 1685 | */ |
| 1686 | ip->ip_hl_v = 0x45; /* IP_HDR_SIZE / 4 (not including UDP) */ |
| 1687 | ip->ip_tos = 0; |
| 1688 | ip->ip_len = htons(IP_HDR_SIZE + len); |
| 1689 | ip->ip_id = htons(NetIPID++); |
| 1690 | ip->ip_off = htons(0x4000); /* No fragmentation */ |
| 1691 | ip->ip_ttl = 255; |
| 1692 | ip->ip_p = 17; /* UDP */ |
| 1693 | ip->ip_sum = 0; |
| 1694 | NetCopyIP((void*)&ip->ip_src, &NetOurIP); /* already in network byte order */ |
| 1695 | NetCopyIP((void*)&ip->ip_dst, &dest); /* - "" - */ |
| 1696 | ip->udp_src = htons(sport); |
| 1697 | ip->udp_dst = htons(dport); |
| 1698 | ip->udp_len = htons(8 + len); |
| 1699 | ip->udp_xsum = 0; |
| 1700 | ip->ip_sum = ~NetCksum((uchar *)ip, IP_HDR_SIZE_NO_UDP / 2); |
| 1701 | } |
| 1702 | |
Wolfgang Denk | 77ddac9 | 2005-10-13 16:45:02 +0200 | [diff] [blame] | 1703 | void copy_filename (char *dst, char *src, int size) |
wdenk | 2d96695 | 2002-10-31 22:12:35 +0000 | [diff] [blame] | 1704 | { |
| 1705 | if (*src && (*src == '"')) { |
| 1706 | ++src; |
| 1707 | --size; |
| 1708 | } |
| 1709 | |
| 1710 | while ((--size > 0) && *src && (*src != '"')) { |
| 1711 | *dst++ = *src++; |
| 1712 | } |
| 1713 | *dst = '\0'; |
| 1714 | } |
| 1715 | |
Jon Loeliger | 610f2e9 | 2007-07-10 11:05:02 -0500 | [diff] [blame] | 1716 | #endif |
wdenk | 2d96695 | 2002-10-31 22:12:35 +0000 | [diff] [blame] | 1717 | |
| 1718 | void ip_to_string (IPaddr_t x, char *s) |
| 1719 | { |
wdenk | a3d991b | 2004-04-15 21:48:45 +0000 | [diff] [blame] | 1720 | x = ntohl (x); |
| 1721 | sprintf (s, "%d.%d.%d.%d", |
| 1722 | (int) ((x >> 24) & 0xff), |
| 1723 | (int) ((x >> 16) & 0xff), |
| 1724 | (int) ((x >> 8) & 0xff), (int) ((x >> 0) & 0xff) |
wdenk | 6e59238 | 2004-04-18 17:39:38 +0000 | [diff] [blame] | 1725 | ); |
wdenk | 2d96695 | 2002-10-31 22:12:35 +0000 | [diff] [blame] | 1726 | } |
| 1727 | |
wdenk | 73a8b27 | 2003-06-05 19:27:42 +0000 | [diff] [blame] | 1728 | IPaddr_t string_to_ip(char *s) |
wdenk | 2d96695 | 2002-10-31 22:12:35 +0000 | [diff] [blame] | 1729 | { |
| 1730 | IPaddr_t addr; |
wdenk | 73a8b27 | 2003-06-05 19:27:42 +0000 | [diff] [blame] | 1731 | char *e; |
wdenk | 2d96695 | 2002-10-31 22:12:35 +0000 | [diff] [blame] | 1732 | int i; |
| 1733 | |
wdenk | 73a8b27 | 2003-06-05 19:27:42 +0000 | [diff] [blame] | 1734 | if (s == NULL) |
| 1735 | return(0); |
wdenk | 2d96695 | 2002-10-31 22:12:35 +0000 | [diff] [blame] | 1736 | |
| 1737 | for (addr=0, i=0; i<4; ++i) { |
| 1738 | ulong val = s ? simple_strtoul(s, &e, 10) : 0; |
| 1739 | addr <<= 8; |
| 1740 | addr |= (val & 0xFF); |
| 1741 | if (s) { |
| 1742 | s = (*e) ? e+1 : e; |
| 1743 | } |
| 1744 | } |
| 1745 | |
| 1746 | return (htonl(addr)); |
| 1747 | } |
wdenk | 73a8b27 | 2003-06-05 19:27:42 +0000 | [diff] [blame] | 1748 | |
wdenk | a3d991b | 2004-04-15 21:48:45 +0000 | [diff] [blame] | 1749 | void VLAN_to_string(ushort x, char *s) |
| 1750 | { |
| 1751 | x = ntohs(x); |
| 1752 | |
| 1753 | if (x == (ushort)-1) |
| 1754 | x = VLAN_NONE; |
| 1755 | |
| 1756 | if (x == VLAN_NONE) |
| 1757 | strcpy(s, "none"); |
| 1758 | else |
| 1759 | sprintf(s, "%d", x & VLAN_IDMASK); |
| 1760 | } |
| 1761 | |
| 1762 | ushort string_to_VLAN(char *s) |
| 1763 | { |
| 1764 | ushort id; |
| 1765 | |
| 1766 | if (s == NULL) |
wdenk | b9711de | 2004-04-25 13:18:40 +0000 | [diff] [blame] | 1767 | return htons(VLAN_NONE); |
wdenk | a3d991b | 2004-04-15 21:48:45 +0000 | [diff] [blame] | 1768 | |
| 1769 | if (*s < '0' || *s > '9') |
| 1770 | id = VLAN_NONE; |
| 1771 | else |
| 1772 | id = (ushort)simple_strtoul(s, NULL, 10); |
| 1773 | |
wdenk | b9711de | 2004-04-25 13:18:40 +0000 | [diff] [blame] | 1774 | return htons(id); |
wdenk | a3d991b | 2004-04-15 21:48:45 +0000 | [diff] [blame] | 1775 | } |
| 1776 | |
wdenk | 73a8b27 | 2003-06-05 19:27:42 +0000 | [diff] [blame] | 1777 | void print_IPaddr (IPaddr_t x) |
| 1778 | { |
wdenk | a3d991b | 2004-04-15 21:48:45 +0000 | [diff] [blame] | 1779 | char tmp[16]; |
wdenk | 73a8b27 | 2003-06-05 19:27:42 +0000 | [diff] [blame] | 1780 | |
wdenk | a3d991b | 2004-04-15 21:48:45 +0000 | [diff] [blame] | 1781 | ip_to_string (x, tmp); |
wdenk | 73a8b27 | 2003-06-05 19:27:42 +0000 | [diff] [blame] | 1782 | |
wdenk | a3d991b | 2004-04-15 21:48:45 +0000 | [diff] [blame] | 1783 | puts (tmp); |
wdenk | 73a8b27 | 2003-06-05 19:27:42 +0000 | [diff] [blame] | 1784 | } |
| 1785 | |
| 1786 | IPaddr_t getenv_IPaddr (char *var) |
| 1787 | { |
| 1788 | return (string_to_ip(getenv(var))); |
| 1789 | } |
wdenk | a3d991b | 2004-04-15 21:48:45 +0000 | [diff] [blame] | 1790 | |
| 1791 | ushort getenv_VLAN(char *var) |
| 1792 | { |
| 1793 | return (string_to_VLAN(getenv(var))); |
| 1794 | } |