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