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