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