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