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