wdenk | 2d96695 | 2002-10-31 22:12:35 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copied from Linux Monitor (LiMon) - Networking. |
| 3 | * |
| 4 | * Copyright 1994 - 2000 Neil Russell. |
| 5 | * (See License) |
| 6 | * Copyright 2000 Roland Borde |
| 7 | * Copyright 2000 Paolo Scaffardi |
| 8 | * Copyright 2000-2002 Wolfgang Denk, wd@denx.de |
| 9 | */ |
| 10 | |
| 11 | /* |
| 12 | * General Desription: |
| 13 | * |
| 14 | * The user interface supports commands for BOOTP, RARP, and TFTP. |
| 15 | * Also, we support ARP internally. Depending on available data, |
| 16 | * these interact as follows: |
| 17 | * |
| 18 | * BOOTP: |
| 19 | * |
| 20 | * Prerequisites: - own ethernet address |
| 21 | * We want: - own IP address |
| 22 | * - TFTP server IP address |
| 23 | * - name of bootfile |
| 24 | * Next step: ARP |
| 25 | * |
| 26 | * RARP: |
| 27 | * |
| 28 | * Prerequisites: - own ethernet address |
| 29 | * We want: - own IP address |
| 30 | * - TFTP server IP address |
| 31 | * Next step: ARP |
| 32 | * |
| 33 | * ARP: |
| 34 | * |
| 35 | * Prerequisites: - own ethernet address |
| 36 | * - own IP address |
| 37 | * - TFTP server IP address |
| 38 | * We want: - TFTP server ethernet address |
| 39 | * Next step: TFTP |
| 40 | * |
| 41 | * DHCP: |
| 42 | * |
Wolfgang Denk | 30b8732 | 2005-08-12 23:43:12 +0200 | [diff] [blame] | 43 | * Prerequisites: - own ethernet address |
| 44 | * We want: - IP, Netmask, ServerIP, Gateway IP |
| 45 | * - bootfilename, lease time |
| 46 | * Next step: - TFTP |
wdenk | 2d96695 | 2002-10-31 22:12:35 +0000 | [diff] [blame] | 47 | * |
| 48 | * TFTP: |
| 49 | * |
| 50 | * Prerequisites: - own ethernet address |
| 51 | * - own IP address |
| 52 | * - TFTP server IP address |
| 53 | * - TFTP server ethernet address |
| 54 | * - name of bootfile (if unknown, we use a default name |
| 55 | * derived from our own IP address) |
| 56 | * We want: - load the boot file |
| 57 | * Next step: none |
wdenk | be9c1cb | 2004-02-24 02:00:03 +0000 | [diff] [blame] | 58 | * |
| 59 | * NFS: |
| 60 | * |
| 61 | * Prerequisites: - own ethernet address |
| 62 | * - own IP address |
| 63 | * - name of bootfile (if unknown, we use a default name |
| 64 | * derived from our own IP address) |
| 65 | * We want: - load the boot file |
| 66 | * Next step: none |
wdenk | b4ad962 | 2005-04-01 00:25:43 +0000 | [diff] [blame] | 67 | * |
| 68 | * SNTP: |
| 69 | * |
Wolfgang Denk | 30b8732 | 2005-08-12 23:43:12 +0200 | [diff] [blame] | 70 | * Prerequisites: - own ethernet address |
wdenk | b4ad962 | 2005-04-01 00:25:43 +0000 | [diff] [blame] | 71 | * - own IP address |
| 72 | * We want: - network time |
| 73 | * Next step: none |
wdenk | 2d96695 | 2002-10-31 22:12:35 +0000 | [diff] [blame] | 74 | */ |
| 75 | |
| 76 | |
| 77 | #include <common.h> |
| 78 | #include <watchdog.h> |
| 79 | #include <command.h> |
| 80 | #include <net.h> |
| 81 | #include "bootp.h" |
| 82 | #include "tftp.h" |
Peter Tyser | f7a48ca | 2010-09-30 11:25:48 -0500 | [diff] [blame] | 83 | #ifdef CONFIG_CMD_RARP |
wdenk | 2d96695 | 2002-10-31 22:12:35 +0000 | [diff] [blame] | 84 | #include "rarp.h" |
Peter Tyser | f7a48ca | 2010-09-30 11:25:48 -0500 | [diff] [blame] | 85 | #endif |
wdenk | be9c1cb | 2004-02-24 02:00:03 +0000 | [diff] [blame] | 86 | #include "nfs.h" |
wdenk | 49c3f67 | 2003-10-08 22:33:00 +0000 | [diff] [blame] | 87 | #ifdef CONFIG_STATUS_LED |
| 88 | #include <status_led.h> |
| 89 | #include <miiphy.h> |
| 90 | #endif |
Jon Loeliger | 54f35c2 | 2007-07-09 17:45:14 -0500 | [diff] [blame] | 91 | #if defined(CONFIG_CMD_SNTP) |
wdenk | b4ad962 | 2005-04-01 00:25:43 +0000 | [diff] [blame] | 92 | #include "sntp.h" |
| 93 | #endif |
Peter Tyser | 6294850 | 2008-11-03 09:30:59 -0600 | [diff] [blame] | 94 | #if defined(CONFIG_CDP_VERSION) |
| 95 | #include <timestamp.h> |
| 96 | #endif |
Robin Getz | 82f0d23 | 2009-07-20 14:53:54 -0400 | [diff] [blame] | 97 | #if defined(CONFIG_CMD_DNS) |
| 98 | #include "dns.h" |
| 99 | #endif |
wdenk | 2d96695 | 2002-10-31 22:12:35 +0000 | [diff] [blame] | 100 | |
Wolfgang Denk | 6405a15 | 2006-03-31 18:32:53 +0200 | [diff] [blame] | 101 | DECLARE_GLOBAL_DATA_PTR; |
| 102 | |
Guennadi Liakhovetski | b38c2b3 | 2008-04-03 17:04:19 +0200 | [diff] [blame] | 103 | #ifndef CONFIG_ARP_TIMEOUT |
Bartlomiej Sieka | 5666846 | 2008-10-01 15:26:28 +0200 | [diff] [blame] | 104 | # define ARP_TIMEOUT 5000UL /* Milliseconds before trying ARP again */ |
Guennadi Liakhovetski | b38c2b3 | 2008-04-03 17:04:19 +0200 | [diff] [blame] | 105 | #else |
Bartlomiej Sieka | 5666846 | 2008-10-01 15:26:28 +0200 | [diff] [blame] | 106 | # define ARP_TIMEOUT CONFIG_ARP_TIMEOUT |
Guennadi Liakhovetski | b38c2b3 | 2008-04-03 17:04:19 +0200 | [diff] [blame] | 107 | #endif |
| 108 | |
| 109 | |
wdenk | e6466f6 | 2003-06-05 19:27:42 +0000 | [diff] [blame] | 110 | #ifndef CONFIG_NET_RETRY_COUNT |
Guennadi Liakhovetski | b38c2b3 | 2008-04-03 17:04:19 +0200 | [diff] [blame] | 111 | # define ARP_TIMEOUT_COUNT 5 /* # of timeouts before giving up */ |
wdenk | e6466f6 | 2003-06-05 19:27:42 +0000 | [diff] [blame] | 112 | #else |
Guennadi Liakhovetski | b38c2b3 | 2008-04-03 17:04:19 +0200 | [diff] [blame] | 113 | # define ARP_TIMEOUT_COUNT CONFIG_NET_RETRY_COUNT |
wdenk | e6466f6 | 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 | 5336a76 | 2007-07-09 22:08:34 -0500 | [diff] [blame] | 121 | #if defined(CONFIG_BOOTP_DNS2) |
stroese | e0aadfb | 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 | 7280da7 | 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 | e6466f6 | 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 | b38c2b3 | 2008-04-03 17:04:19 +0200 | [diff] [blame] | 140 | IPaddr_t NetServerIP; /* Server IP addr (0 = unknown) */ |
Mike Frysinger | dc16603 | 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 | e6466f6 | 2003-06-05 19:27:42 +0000 | [diff] [blame] | 146 | uchar NetEtherNullAddr[6] = |
| 147 | { 0, 0, 0, 0, 0, 0 }; |
Rafal Jaworowski | 1f2c9a4 | 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 | 54f35c2 | 2007-07-09 17:45:14 -0500 | [diff] [blame] | 151 | #if defined(CONFIG_CMD_CDP) |
wdenk | 0593920 | 2004-04-18 17:39:38 +0000 | [diff] [blame] | 152 | uchar NetCDPAddr[6] = /* Ethernet bcast address */ |
wdenk | 145d2c1 | 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 | 0593920 | 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 | 145d2c1 | 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 | 54f35c2 | 2007-07-09 17:45:14 -0500 | [diff] [blame] | 168 | #if defined(CONFIG_CMD_PING) |
Wolfgang Denk | a1be476 | 2008-05-20 16:00:29 +0200 | [diff] [blame] | 169 | IPaddr_t NetPingIP; /* the ip address to ping */ |
wdenk | e6466f6 | 2003-06-05 19:27:42 +0000 | [diff] [blame] | 170 | |
| 171 | static void PingStart(void); |
| 172 | #endif |
| 173 | |
Jon Loeliger | 54f35c2 | 2007-07-09 17:45:14 -0500 | [diff] [blame] | 174 | #if defined(CONFIG_CMD_CDP) |
wdenk | 145d2c1 | 2004-04-15 21:48:45 +0000 | [diff] [blame] | 175 | static void CDPStart(void); |
| 176 | #endif |
| 177 | |
Jon Loeliger | 54f35c2 | 2007-07-09 17:45:14 -0500 | [diff] [blame] | 178 | #if defined(CONFIG_CMD_SNTP) |
wdenk | b4ad962 | 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 | b8fb619 | 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 | 5d84173 | 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 | 0ddb8e8 | 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 | e6466f6 | 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 | 30b8732 | 2005-08-12 23:43:12 +0200 | [diff] [blame] | 207 | uchar *NetArpWaitTxPacket; /* THE transmit packet */ |
wdenk | e6466f6 | 2003-06-05 19:27:42 +0000 | [diff] [blame] | 208 | int NetArpWaitTxPacketSize; |
Wolfgang Denk | a1be476 | 2008-05-20 16:00:29 +0200 | [diff] [blame] | 209 | uchar NetArpWaitPacketBuf[PKTSIZE_ALIGN + PKTALIGN]; |
wdenk | e6466f6 | 2003-06-05 19:27:42 +0000 | [diff] [blame] | 210 | ulong NetArpWaitTimerStart; |
| 211 | int NetArpWaitTry; |
| 212 | |
wdenk | 0593920 | 2004-04-18 17:39:38 +0000 | [diff] [blame] | 213 | void ArpRequest (void) |
wdenk | e6466f6 | 2003-06-05 19:27:42 +0000 | [diff] [blame] | 214 | { |
| 215 | int i; |
| 216 | volatile uchar *pkt; |
wdenk | 0593920 | 2004-04-18 17:39:38 +0000 | [diff] [blame] | 217 | ARP_t *arp; |
wdenk | e6466f6 | 2003-06-05 19:27:42 +0000 | [diff] [blame] | 218 | |
Robin Getz | 9e0a4d6 | 2009-07-23 03:01:03 -0400 | [diff] [blame] | 219 | debug("ARP broadcast %d\n", NetArpWaitTry); |
| 220 | |
wdenk | e6466f6 | 2003-06-05 19:27:42 +0000 | [diff] [blame] | 221 | pkt = NetTxPacket; |
| 222 | |
wdenk | 0593920 | 2004-04-18 17:39:38 +0000 | [diff] [blame] | 223 | pkt += NetSetEther (pkt, NetBcastAddr, PROT_ARP); |
wdenk | e6466f6 | 2003-06-05 19:27:42 +0000 | [diff] [blame] | 224 | |
wdenk | 0593920 | 2004-04-18 17:39:38 +0000 | [diff] [blame] | 225 | arp = (ARP_t *) pkt; |
wdenk | e6466f6 | 2003-06-05 19:27:42 +0000 | [diff] [blame] | 226 | |
wdenk | 0593920 | 2004-04-18 17:39:38 +0000 | [diff] [blame] | 227 | arp->ar_hrd = htons (ARP_ETHER); |
| 228 | arp->ar_pro = htons (PROT_IP); |
wdenk | e6466f6 | 2003-06-05 19:27:42 +0000 | [diff] [blame] | 229 | arp->ar_hln = 6; |
| 230 | arp->ar_pln = 4; |
wdenk | 0593920 | 2004-04-18 17:39:38 +0000 | [diff] [blame] | 231 | arp->ar_op = htons (ARPOP_REQUEST); |
wdenk | e6466f6 | 2003-06-05 19:27:42 +0000 | [diff] [blame] | 232 | |
Wolfgang Denk | 30b8732 | 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 | 0593920 | 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 | e6466f6 | 2003-06-05 19:27:42 +0000 | [diff] [blame] | 237 | } |
| 238 | |
wdenk | 0593920 | 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 | 98ceffb | 2006-03-12 01:13:30 +0100 | [diff] [blame] | 243 | NetArpWaitReplyIP = NetArpWaitPacketIP; |
| 244 | } else { |
| 245 | NetArpWaitReplyIP = NetOurGatewayIP; |
wdenk | 0593920 | 2004-04-18 17:39:38 +0000 | [diff] [blame] | 246 | } |
wdenk | 0593920 | 2004-04-18 17:39:38 +0000 | [diff] [blame] | 247 | } else { |
| 248 | NetArpWaitReplyIP = NetArpWaitPacketIP; |
| 249 | } |
wdenk | e6466f6 | 2003-06-05 19:27:42 +0000 | [diff] [blame] | 250 | |
wdenk | 0593920 | 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 | e6466f6 | 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 | 5666846 | 2008-10-01 15:26:28 +0200 | [diff] [blame] | 265 | if ((t - NetArpWaitTimerStart) > ARP_TIMEOUT) { |
wdenk | e6466f6 | 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 | 3b195ff | 2009-04-28 08:36:11 +0200 | [diff] [blame] | 279 | static void |
Heiko Schocher | 0c303fa | 2009-02-10 09:38:52 +0100 | [diff] [blame] | 280 | NetInitLoop(proto_t protocol) |
| 281 | { |
Heiko Schocher | 3b195ff | 2009-04-28 08:36:11 +0200 | [diff] [blame] | 282 | static int env_changed_id = 0; |
Heiko Schocher | 0c303fa | 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 | b97bfe4 | 2009-04-04 01:43:00 +0300 | [diff] [blame] | 287 | if (env_changed_id != env_id) { |
Heiko Schocher | 0c303fa | 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 | 0c303fa | 2009-02-10 09:38:52 +0100 | [diff] [blame] | 291 | NetServerIP = getenv_IPaddr ("serverip"); |
Heiko Schocher | 0c303fa | 2009-02-10 09:38:52 +0100 | [diff] [blame] | 292 | NetOurNativeVLAN = getenv_VLAN("nvlan"); |
Michael Zaidman | b97bfe4 | 2009-04-04 01:43:00 +0300 | [diff] [blame] | 293 | NetOurVLAN = getenv_VLAN("vlan"); |
Robin Getz | 82f0d23 | 2009-07-20 14:53:54 -0400 | [diff] [blame] | 294 | #if defined(CONFIG_CMD_DNS) |
| 295 | NetOurDNSIP = getenv_IPaddr("dnsip"); |
| 296 | #endif |
Michael Zaidman | b97bfe4 | 2009-04-04 01:43:00 +0300 | [diff] [blame] | 297 | env_changed_id = env_id; |
Heiko Schocher | 0c303fa | 2009-02-10 09:38:52 +0100 | [diff] [blame] | 298 | } |
Michael Zaidman | b97bfe4 | 2009-04-04 01:43:00 +0300 | [diff] [blame] | 299 | |
Heiko Schocher | 3b195ff | 2009-04-28 08:36:11 +0200 | [diff] [blame] | 300 | return; |
Heiko Schocher | 0c303fa | 2009-02-10 09:38:52 +0100 | [diff] [blame] | 301 | } |
| 302 | |
wdenk | e6466f6 | 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 | e6466f6 | 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 | 0ddb8e8 | 2009-10-28 22:13:39 +0100 | [diff] [blame] | 325 | NetTryCount = 1; |
wdenk | e6466f6 | 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 | e6466f6 | 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 | 0593920 | 2004-04-18 17:39:38 +0000 | [diff] [blame] | 346 | #ifdef CONFIG_NET_MULTI |
wdenk | 145d2c1 | 2004-04-15 21:48:45 +0000 | [diff] [blame] | 347 | eth_set_current(); |
wdenk | 0593920 | 2004-04-18 17:39:38 +0000 | [diff] [blame] | 348 | #endif |
wdenk | fa66e93 | 2005-04-03 14:52:59 +0000 | [diff] [blame] | 349 | if (eth_init(bd) < 0) { |
| 350 | eth_halt(); |
wdenk | 0593920 | 2004-04-18 17:39:38 +0000 | [diff] [blame] | 351 | return(-1); |
wdenk | fa66e93 | 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 | 79ca1b9 | 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 | 0c303fa | 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 | fa66e93 | 2005-04-03 14:52:59 +0000 | [diff] [blame] | 373 | eth_halt(); |
wdenk | 2582f6b | 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 | e6466f6 | 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 | 54f35c2 | 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 | c391953 | 2004-10-11 22:51:13 +0000 | [diff] [blame] | 394 | BootpTry = 0; |
Michael Zaidman | 16bb21d | 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 | a9807e5 | 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 | 16bb21d | 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 | |
Peter Tyser | f7a48ca | 2010-09-30 11:25:48 -0500 | [diff] [blame] | 406 | #if defined(CONFIG_CMD_RARP) |
wdenk | 2d96695 | 2002-10-31 22:12:35 +0000 | [diff] [blame] | 407 | case RARP: |
| 408 | RarpTry = 0; |
Michael Zaidman | 16bb21d | 2009-07-14 23:37:12 +0300 | [diff] [blame] | 409 | NetOurIP = 0; |
wdenk | 2d96695 | 2002-10-31 22:12:35 +0000 | [diff] [blame] | 410 | RarpRequest (); |
| 411 | break; |
Peter Tyser | f7a48ca | 2010-09-30 11:25:48 -0500 | [diff] [blame] | 412 | #endif |
Jon Loeliger | 54f35c2 | 2007-07-09 17:45:14 -0500 | [diff] [blame] | 413 | #if defined(CONFIG_CMD_PING) |
wdenk | e6466f6 | 2003-06-05 19:27:42 +0000 | [diff] [blame] | 414 | case PING: |
| 415 | PingStart(); |
| 416 | break; |
| 417 | #endif |
Jon Loeliger | 54f35c2 | 2007-07-09 17:45:14 -0500 | [diff] [blame] | 418 | #if defined(CONFIG_CMD_NFS) |
wdenk | be9c1cb | 2004-02-24 02:00:03 +0000 | [diff] [blame] | 419 | case NFS: |
| 420 | NfsStart(); |
| 421 | break; |
| 422 | #endif |
Jon Loeliger | 54f35c2 | 2007-07-09 17:45:14 -0500 | [diff] [blame] | 423 | #if defined(CONFIG_CMD_CDP) |
wdenk | 145d2c1 | 2004-04-15 21:48:45 +0000 | [diff] [blame] | 424 | case CDP: |
| 425 | CDPStart(); |
| 426 | break; |
| 427 | #endif |
wdenk | b8fb619 | 2004-08-02 21:11:11 +0000 | [diff] [blame] | 428 | #ifdef CONFIG_NETCONSOLE |
| 429 | case NETCONS: |
| 430 | NcStart(); |
| 431 | break; |
| 432 | #endif |
Jon Loeliger | 54f35c2 | 2007-07-09 17:45:14 -0500 | [diff] [blame] | 433 | #if defined(CONFIG_CMD_SNTP) |
wdenk | b4ad962 | 2005-04-01 00:25:43 +0000 | [diff] [blame] | 434 | case SNTP: |
| 435 | SntpStart(); |
| 436 | break; |
| 437 | #endif |
Robin Getz | 82f0d23 | 2009-07-20 14:53:54 -0400 | [diff] [blame] | 438 | #if defined(CONFIG_CMD_DNS) |
| 439 | case DNS: |
| 440 | DnsStart(); |
| 441 | break; |
| 442 | #endif |
wdenk | 2d96695 | 2002-10-31 22:12:35 +0000 | [diff] [blame] | 443 | default: |
| 444 | break; |
| 445 | } |
| 446 | |
| 447 | NetBootFileXferSize = 0; |
| 448 | break; |
| 449 | } |
| 450 | |
Jon Loeliger | 54f35c2 | 2007-07-09 17:45:14 -0500 | [diff] [blame] | 451 | #if defined(CONFIG_MII) || defined(CONFIG_CMD_MII) |
Jean-Christophe PLAGNIOL-VILLARD | 0383694 | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 452 | #if defined(CONFIG_SYS_FAULT_ECHO_LINK_DOWN) && defined(CONFIG_STATUS_LED) && defined(STATUS_LED_RED) |
wdenk | 49c3f67 | 2003-10-08 22:33:00 +0000 | [diff] [blame] | 453 | /* |
wdenk | 9c53f40 | 2003-10-15 23:53:47 +0000 | [diff] [blame] | 454 | * Echo the inverted link state to the fault LED. |
wdenk | 49c3f67 | 2003-10-08 22:33:00 +0000 | [diff] [blame] | 455 | */ |
Jean-Christophe PLAGNIOL-VILLARD | 0383694 | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 456 | if(miiphy_link(eth_get_dev()->name, CONFIG_SYS_FAULT_MII_ADDR)) { |
wdenk | 49c3f67 | 2003-10-08 22:33:00 +0000 | [diff] [blame] | 457 | status_led_set (STATUS_LED_RED, STATUS_LED_OFF); |
| 458 | } else { |
| 459 | status_led_set (STATUS_LED_RED, STATUS_LED_ON); |
| 460 | } |
Jean-Christophe PLAGNIOL-VILLARD | 0383694 | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 461 | #endif /* CONFIG_SYS_FAULT_ECHO_LINK_DOWN, ... */ |
wdenk | 49c3f67 | 2003-10-08 22:33:00 +0000 | [diff] [blame] | 462 | #endif /* CONFIG_MII, ... */ |
wdenk | 2d96695 | 2002-10-31 22:12:35 +0000 | [diff] [blame] | 463 | |
| 464 | /* |
| 465 | * Main packet reception loop. Loop receiving packets until |
wdenk | 27fa585 | 2005-04-03 14:18:51 +0000 | [diff] [blame] | 466 | * someone sets `NetState' to a state that terminates. |
wdenk | 2d96695 | 2002-10-31 22:12:35 +0000 | [diff] [blame] | 467 | */ |
| 468 | for (;;) { |
| 469 | WATCHDOG_RESET(); |
| 470 | #ifdef CONFIG_SHOW_ACTIVITY |
| 471 | { |
| 472 | extern void show_activity(int arg); |
| 473 | show_activity(1); |
| 474 | } |
| 475 | #endif |
| 476 | /* |
| 477 | * Check the ethernet for a new packet. The ethernet |
| 478 | * receive routine will process it. |
| 479 | */ |
Guennadi Liakhovetski | b38c2b3 | 2008-04-03 17:04:19 +0200 | [diff] [blame] | 480 | eth_rx(); |
wdenk | 2d96695 | 2002-10-31 22:12:35 +0000 | [diff] [blame] | 481 | |
| 482 | /* |
| 483 | * Abort if ctrl-c was pressed. |
| 484 | */ |
| 485 | if (ctrlc()) { |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 486 | eth_halt(); |
wdenk | 42c0547 | 2004-03-23 22:14:11 +0000 | [diff] [blame] | 487 | puts ("\nAbort\n"); |
wdenk | 2582f6b | 2002-11-11 21:14:20 +0000 | [diff] [blame] | 488 | return (-1); |
wdenk | 2d96695 | 2002-10-31 22:12:35 +0000 | [diff] [blame] | 489 | } |
| 490 | |
wdenk | e6466f6 | 2003-06-05 19:27:42 +0000 | [diff] [blame] | 491 | ArpTimeoutCheck(); |
wdenk | 2d96695 | 2002-10-31 22:12:35 +0000 | [diff] [blame] | 492 | |
| 493 | /* |
| 494 | * Check for a timeout, and run the timeout handler |
| 495 | * if we have one. |
| 496 | */ |
wdenk | 5d84173 | 2003-08-17 18:55:18 +0000 | [diff] [blame] | 497 | if (timeHandler && ((get_timer(0) - timeStart) > timeDelta)) { |
wdenk | 2d96695 | 2002-10-31 22:12:35 +0000 | [diff] [blame] | 498 | thand_f *x; |
| 499 | |
Jon Loeliger | 54f35c2 | 2007-07-09 17:45:14 -0500 | [diff] [blame] | 500 | #if defined(CONFIG_MII) || defined(CONFIG_CMD_MII) |
Jean-Christophe PLAGNIOL-VILLARD | 0383694 | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 501 | # if defined(CONFIG_SYS_FAULT_ECHO_LINK_DOWN) && \ |
wdenk | 6148e74 | 2005-04-03 20:55:38 +0000 | [diff] [blame] | 502 | defined(CONFIG_STATUS_LED) && \ |
wdenk | 27fa585 | 2005-04-03 14:18:51 +0000 | [diff] [blame] | 503 | defined(STATUS_LED_RED) |
wdenk | 49c3f67 | 2003-10-08 22:33:00 +0000 | [diff] [blame] | 504 | /* |
wdenk | 9c53f40 | 2003-10-15 23:53:47 +0000 | [diff] [blame] | 505 | * Echo the inverted link state to the fault LED. |
wdenk | 49c3f67 | 2003-10-08 22:33:00 +0000 | [diff] [blame] | 506 | */ |
Jean-Christophe PLAGNIOL-VILLARD | 0383694 | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 507 | if(miiphy_link(eth_get_dev()->name, CONFIG_SYS_FAULT_MII_ADDR)) { |
wdenk | 49c3f67 | 2003-10-08 22:33:00 +0000 | [diff] [blame] | 508 | status_led_set (STATUS_LED_RED, STATUS_LED_OFF); |
| 509 | } else { |
| 510 | status_led_set (STATUS_LED_RED, STATUS_LED_ON); |
| 511 | } |
Jean-Christophe PLAGNIOL-VILLARD | 0383694 | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 512 | # endif /* CONFIG_SYS_FAULT_ECHO_LINK_DOWN, ... */ |
wdenk | 49c3f67 | 2003-10-08 22:33:00 +0000 | [diff] [blame] | 513 | #endif /* CONFIG_MII, ... */ |
wdenk | 2d96695 | 2002-10-31 22:12:35 +0000 | [diff] [blame] | 514 | x = timeHandler; |
| 515 | timeHandler = (thand_f *)0; |
| 516 | (*x)(); |
| 517 | } |
| 518 | |
| 519 | |
| 520 | switch (NetState) { |
| 521 | |
| 522 | case NETLOOP_RESTART: |
| 523 | #ifdef CONFIG_NET_MULTI |
| 524 | NetRestarted = 1; |
| 525 | #endif |
| 526 | goto restart; |
| 527 | |
| 528 | case NETLOOP_SUCCESS: |
| 529 | if (NetBootFileXferSize > 0) { |
Wolfgang Denk | 084d0ce | 2007-09-12 00:48:57 +0200 | [diff] [blame] | 530 | char buf[20]; |
wdenk | 2d96695 | 2002-10-31 22:12:35 +0000 | [diff] [blame] | 531 | printf("Bytes transferred = %ld (%lx hex)\n", |
| 532 | NetBootFileXferSize, |
| 533 | NetBootFileXferSize); |
Wolfgang Denk | 084d0ce | 2007-09-12 00:48:57 +0200 | [diff] [blame] | 534 | sprintf(buf, "%lX", NetBootFileXferSize); |
wdenk | 2d96695 | 2002-10-31 22:12:35 +0000 | [diff] [blame] | 535 | setenv("filesize", buf); |
wdenk | 145d2c1 | 2004-04-15 21:48:45 +0000 | [diff] [blame] | 536 | |
| 537 | sprintf(buf, "%lX", (unsigned long)load_addr); |
| 538 | setenv("fileaddr", buf); |
wdenk | 2d96695 | 2002-10-31 22:12:35 +0000 | [diff] [blame] | 539 | } |
| 540 | eth_halt(); |
| 541 | return NetBootFileXferSize; |
| 542 | |
| 543 | case NETLOOP_FAIL: |
wdenk | 2582f6b | 2002-11-11 21:14:20 +0000 | [diff] [blame] | 544 | return (-1); |
wdenk | 2d96695 | 2002-10-31 22:12:35 +0000 | [diff] [blame] | 545 | } |
| 546 | } |
| 547 | } |
| 548 | |
| 549 | /**********************************************************************/ |
| 550 | |
| 551 | static void |
| 552 | startAgainTimeout(void) |
| 553 | { |
| 554 | NetState = NETLOOP_RESTART; |
| 555 | } |
| 556 | |
| 557 | static void |
Luca Ceresoli | 428ab36 | 2011-04-18 06:19:50 +0000 | [diff] [blame^] | 558 | startAgainHandler(uchar *pkt, unsigned dest, IPaddr_t sip, |
| 559 | unsigned src, unsigned len) |
wdenk | 2d96695 | 2002-10-31 22:12:35 +0000 | [diff] [blame] | 560 | { |
| 561 | /* Totally ignore the packet */ |
| 562 | } |
| 563 | |
wdenk | 0593920 | 2004-04-18 17:39:38 +0000 | [diff] [blame] | 564 | void NetStartAgain (void) |
wdenk | 2d96695 | 2002-10-31 22:12:35 +0000 | [diff] [blame] | 565 | { |
wdenk | 0593920 | 2004-04-18 17:39:38 +0000 | [diff] [blame] | 566 | char *nretry; |
Remy Bohmer | 0ddb8e8 | 2009-10-28 22:13:39 +0100 | [diff] [blame] | 567 | int retry_forever = 0; |
| 568 | unsigned long retrycnt = 0; |
wdenk | 145d2c1 | 2004-04-15 21:48:45 +0000 | [diff] [blame] | 569 | |
Remy Bohmer | 0ddb8e8 | 2009-10-28 22:13:39 +0100 | [diff] [blame] | 570 | nretry = getenv("netretry"); |
| 571 | if (nretry) { |
| 572 | if (!strcmp(nretry, "yes")) |
| 573 | retry_forever = 1; |
| 574 | else if (!strcmp(nretry, "no")) |
| 575 | retrycnt = 0; |
| 576 | else if (!strcmp(nretry, "once")) |
| 577 | retrycnt = 1; |
| 578 | else |
| 579 | retrycnt = simple_strtoul(nretry, NULL, 0); |
| 580 | } else |
| 581 | retry_forever = 1; |
| 582 | |
| 583 | if ((!retry_forever) && (NetTryCount >= retrycnt)) { |
| 584 | eth_halt(); |
wdenk | 145d2c1 | 2004-04-15 21:48:45 +0000 | [diff] [blame] | 585 | NetState = NETLOOP_FAIL; |
| 586 | return; |
| 587 | } |
Remy Bohmer | 0ddb8e8 | 2009-10-28 22:13:39 +0100 | [diff] [blame] | 588 | |
| 589 | NetTryCount++; |
| 590 | |
wdenk | 2d96695 | 2002-10-31 22:12:35 +0000 | [diff] [blame] | 591 | #ifndef CONFIG_NET_MULTI |
Bartlomiej Sieka | 5666846 | 2008-10-01 15:26:28 +0200 | [diff] [blame] | 592 | NetSetTimeout (10000UL, startAgainTimeout); |
wdenk | 0593920 | 2004-04-18 17:39:38 +0000 | [diff] [blame] | 593 | NetSetHandler (startAgainHandler); |
| 594 | #else /* !CONFIG_NET_MULTI*/ |
| 595 | eth_halt (); |
Matthias Fuchs | a02d62b | 2007-12-27 16:58:41 +0100 | [diff] [blame] | 596 | #if !defined(CONFIG_NET_DO_NOT_TRY_ANOTHER) |
wdenk | 0593920 | 2004-04-18 17:39:38 +0000 | [diff] [blame] | 597 | eth_try_another (!NetRestarted); |
Matthias Fuchs | a02d62b | 2007-12-27 16:58:41 +0100 | [diff] [blame] | 598 | #endif |
wdenk | 0593920 | 2004-04-18 17:39:38 +0000 | [diff] [blame] | 599 | eth_init (gd->bd); |
| 600 | if (NetRestartWrap) { |
wdenk | 2d96695 | 2002-10-31 22:12:35 +0000 | [diff] [blame] | 601 | NetRestartWrap = 0; |
Remy Bohmer | 0ddb8e8 | 2009-10-28 22:13:39 +0100 | [diff] [blame] | 602 | if (NetDevExists) { |
Bartlomiej Sieka | 5666846 | 2008-10-01 15:26:28 +0200 | [diff] [blame] | 603 | NetSetTimeout (10000UL, startAgainTimeout); |
wdenk | 0593920 | 2004-04-18 17:39:38 +0000 | [diff] [blame] | 604 | NetSetHandler (startAgainHandler); |
| 605 | } else { |
wdenk | 2d96695 | 2002-10-31 22:12:35 +0000 | [diff] [blame] | 606 | NetState = NETLOOP_FAIL; |
| 607 | } |
wdenk | 0593920 | 2004-04-18 17:39:38 +0000 | [diff] [blame] | 608 | } else { |
wdenk | 2d96695 | 2002-10-31 22:12:35 +0000 | [diff] [blame] | 609 | NetState = NETLOOP_RESTART; |
| 610 | } |
wdenk | 0593920 | 2004-04-18 17:39:38 +0000 | [diff] [blame] | 611 | #endif /* CONFIG_NET_MULTI */ |
wdenk | 2d96695 | 2002-10-31 22:12:35 +0000 | [diff] [blame] | 612 | } |
| 613 | |
| 614 | /**********************************************************************/ |
| 615 | /* |
| 616 | * Miscelaneous bits. |
| 617 | */ |
| 618 | |
| 619 | void |
| 620 | NetSetHandler(rxhand_f * f) |
| 621 | { |
| 622 | packetHandler = f; |
| 623 | } |
| 624 | |
| 625 | |
| 626 | void |
wdenk | d6a3dd4 | 2004-10-09 21:56:21 +0000 | [diff] [blame] | 627 | NetSetTimeout(ulong iv, thand_f * f) |
wdenk | 2d96695 | 2002-10-31 22:12:35 +0000 | [diff] [blame] | 628 | { |
| 629 | if (iv == 0) { |
| 630 | timeHandler = (thand_f *)0; |
| 631 | } else { |
| 632 | timeHandler = f; |
wdenk | 5d84173 | 2003-08-17 18:55:18 +0000 | [diff] [blame] | 633 | timeStart = get_timer(0); |
| 634 | timeDelta = iv; |
wdenk | 2d96695 | 2002-10-31 22:12:35 +0000 | [diff] [blame] | 635 | } |
| 636 | } |
| 637 | |
| 638 | |
| 639 | void |
| 640 | NetSendPacket(volatile uchar * pkt, int len) |
| 641 | { |
| 642 | (void) eth_send(pkt, len); |
| 643 | } |
| 644 | |
wdenk | e6466f6 | 2003-06-05 19:27:42 +0000 | [diff] [blame] | 645 | int |
| 646 | NetSendUDPPacket(uchar *ether, IPaddr_t dest, int dport, int sport, int len) |
| 647 | { |
wdenk | 145d2c1 | 2004-04-15 21:48:45 +0000 | [diff] [blame] | 648 | uchar *pkt; |
| 649 | |
wdenk | e6466f6 | 2003-06-05 19:27:42 +0000 | [diff] [blame] | 650 | /* convert to new style broadcast */ |
| 651 | if (dest == 0) |
| 652 | dest = 0xFFFFFFFF; |
| 653 | |
| 654 | /* if broadcast, make the ether address a broadcast and don't do ARP */ |
| 655 | if (dest == 0xFFFFFFFF) |
| 656 | ether = NetBcastAddr; |
| 657 | |
| 658 | /* if MAC address was not discovered yet, save the packet and do an ARP request */ |
| 659 | if (memcmp(ether, NetEtherNullAddr, 6) == 0) { |
| 660 | |
Robin Getz | 9e0a4d6 | 2009-07-23 03:01:03 -0400 | [diff] [blame] | 661 | debug("sending ARP for %08lx\n", dest); |
| 662 | |
wdenk | e6466f6 | 2003-06-05 19:27:42 +0000 | [diff] [blame] | 663 | NetArpWaitPacketIP = dest; |
| 664 | NetArpWaitPacketMAC = ether; |
wdenk | 145d2c1 | 2004-04-15 21:48:45 +0000 | [diff] [blame] | 665 | |
| 666 | pkt = NetArpWaitTxPacket; |
| 667 | pkt += NetSetEther (pkt, NetArpWaitPacketMAC, PROT_IP); |
| 668 | |
| 669 | NetSetIP (pkt, dest, dport, sport, len); |
| 670 | memcpy(pkt + IP_HDR_SIZE, (uchar *)NetTxPacket + (pkt - (uchar *)NetArpWaitTxPacket) + IP_HDR_SIZE, len); |
wdenk | e6466f6 | 2003-06-05 19:27:42 +0000 | [diff] [blame] | 671 | |
| 672 | /* size of the waiting packet */ |
wdenk | 145d2c1 | 2004-04-15 21:48:45 +0000 | [diff] [blame] | 673 | NetArpWaitTxPacketSize = (pkt - NetArpWaitTxPacket) + IP_HDR_SIZE + len; |
wdenk | e6466f6 | 2003-06-05 19:27:42 +0000 | [diff] [blame] | 674 | |
| 675 | /* and do the ARP request */ |
| 676 | NetArpWaitTry = 1; |
| 677 | NetArpWaitTimerStart = get_timer(0); |
| 678 | ArpRequest(); |
| 679 | return 1; /* waiting */ |
| 680 | } |
| 681 | |
Robin Getz | 9e0a4d6 | 2009-07-23 03:01:03 -0400 | [diff] [blame] | 682 | debug("sending UDP to %08lx/%pM\n", dest, ether); |
wdenk | e6466f6 | 2003-06-05 19:27:42 +0000 | [diff] [blame] | 683 | |
wdenk | 145d2c1 | 2004-04-15 21:48:45 +0000 | [diff] [blame] | 684 | pkt = (uchar *)NetTxPacket; |
| 685 | pkt += NetSetEther (pkt, ether, PROT_IP); |
| 686 | NetSetIP (pkt, dest, dport, sport, len); |
| 687 | (void) eth_send(NetTxPacket, (pkt - NetTxPacket) + IP_HDR_SIZE + len); |
wdenk | e6466f6 | 2003-06-05 19:27:42 +0000 | [diff] [blame] | 688 | |
wdenk | 0593920 | 2004-04-18 17:39:38 +0000 | [diff] [blame] | 689 | return 0; /* transmitted */ |
wdenk | e6466f6 | 2003-06-05 19:27:42 +0000 | [diff] [blame] | 690 | } |
| 691 | |
Jon Loeliger | 54f35c2 | 2007-07-09 17:45:14 -0500 | [diff] [blame] | 692 | #if defined(CONFIG_CMD_PING) |
wdenk | e6466f6 | 2003-06-05 19:27:42 +0000 | [diff] [blame] | 693 | static ushort PingSeqNo; |
| 694 | |
| 695 | int PingSend(void) |
| 696 | { |
| 697 | static uchar mac[6]; |
| 698 | volatile IP_t *ip; |
| 699 | volatile ushort *s; |
wdenk | 145d2c1 | 2004-04-15 21:48:45 +0000 | [diff] [blame] | 700 | uchar *pkt; |
wdenk | e6466f6 | 2003-06-05 19:27:42 +0000 | [diff] [blame] | 701 | |
| 702 | /* XXX always send arp request */ |
| 703 | |
| 704 | memcpy(mac, NetEtherNullAddr, 6); |
| 705 | |
Robin Getz | 9e0a4d6 | 2009-07-23 03:01:03 -0400 | [diff] [blame] | 706 | debug("sending ARP for %08lx\n", NetPingIP); |
wdenk | e6466f6 | 2003-06-05 19:27:42 +0000 | [diff] [blame] | 707 | |
| 708 | NetArpWaitPacketIP = NetPingIP; |
| 709 | NetArpWaitPacketMAC = mac; |
| 710 | |
wdenk | 145d2c1 | 2004-04-15 21:48:45 +0000 | [diff] [blame] | 711 | pkt = NetArpWaitTxPacket; |
| 712 | pkt += NetSetEther(pkt, mac, PROT_IP); |
wdenk | e6466f6 | 2003-06-05 19:27:42 +0000 | [diff] [blame] | 713 | |
wdenk | 145d2c1 | 2004-04-15 21:48:45 +0000 | [diff] [blame] | 714 | ip = (volatile IP_t *)pkt; |
wdenk | e6466f6 | 2003-06-05 19:27:42 +0000 | [diff] [blame] | 715 | |
| 716 | /* |
| 717 | * Construct an IP and ICMP header. (need to set no fragment bit - XXX) |
| 718 | */ |
| 719 | ip->ip_hl_v = 0x45; /* IP_HDR_SIZE / 4 (not including UDP) */ |
| 720 | ip->ip_tos = 0; |
| 721 | ip->ip_len = htons(IP_HDR_SIZE_NO_UDP + 8); |
| 722 | ip->ip_id = htons(NetIPID++); |
Peter Tyser | 0885bf0 | 2008-12-01 16:26:20 -0600 | [diff] [blame] | 723 | ip->ip_off = htons(IP_FLAGS_DFRAG); /* Don't fragment */ |
wdenk | e6466f6 | 2003-06-05 19:27:42 +0000 | [diff] [blame] | 724 | ip->ip_ttl = 255; |
| 725 | ip->ip_p = 0x01; /* ICMP */ |
| 726 | ip->ip_sum = 0; |
| 727 | NetCopyIP((void*)&ip->ip_src, &NetOurIP); /* already in network byte order */ |
| 728 | NetCopyIP((void*)&ip->ip_dst, &NetPingIP); /* - "" - */ |
| 729 | ip->ip_sum = ~NetCksum((uchar *)ip, IP_HDR_SIZE_NO_UDP / 2); |
| 730 | |
| 731 | s = &ip->udp_src; /* XXX ICMP starts here */ |
| 732 | s[0] = htons(0x0800); /* echo-request, code */ |
| 733 | s[1] = 0; /* checksum */ |
Wolfgang Denk | a1be476 | 2008-05-20 16:00:29 +0200 | [diff] [blame] | 734 | s[2] = 0; /* identifier */ |
wdenk | e6466f6 | 2003-06-05 19:27:42 +0000 | [diff] [blame] | 735 | s[3] = htons(PingSeqNo++); /* sequence number */ |
| 736 | s[1] = ~NetCksum((uchar *)s, 8/2); |
| 737 | |
| 738 | /* size of the waiting packet */ |
wdenk | 145d2c1 | 2004-04-15 21:48:45 +0000 | [diff] [blame] | 739 | NetArpWaitTxPacketSize = (pkt - NetArpWaitTxPacket) + IP_HDR_SIZE_NO_UDP + 8; |
wdenk | e6466f6 | 2003-06-05 19:27:42 +0000 | [diff] [blame] | 740 | |
| 741 | /* and do the ARP request */ |
| 742 | NetArpWaitTry = 1; |
| 743 | NetArpWaitTimerStart = get_timer(0); |
| 744 | ArpRequest(); |
| 745 | return 1; /* waiting */ |
| 746 | } |
| 747 | |
| 748 | static void |
| 749 | PingTimeout (void) |
| 750 | { |
| 751 | eth_halt(); |
| 752 | NetState = NETLOOP_FAIL; /* we did not get the reply */ |
| 753 | } |
| 754 | |
| 755 | static void |
Luca Ceresoli | 428ab36 | 2011-04-18 06:19:50 +0000 | [diff] [blame^] | 756 | PingHandler(uchar *pkt, unsigned dest, IPaddr_t sip, unsigned src, |
| 757 | unsigned len) |
wdenk | e6466f6 | 2003-06-05 19:27:42 +0000 | [diff] [blame] | 758 | { |
Luca Ceresoli | 428ab36 | 2011-04-18 06:19:50 +0000 | [diff] [blame^] | 759 | if (sip != NetPingIP) |
wdenk | e6466f6 | 2003-06-05 19:27:42 +0000 | [diff] [blame] | 760 | return; |
| 761 | |
| 762 | NetState = NETLOOP_SUCCESS; |
| 763 | } |
| 764 | |
| 765 | static void PingStart(void) |
| 766 | { |
wdenk | 145d2c1 | 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 | 0593920 | 2004-04-18 17:39:38 +0000 | [diff] [blame] | 769 | #endif /* CONFIG_NET_MULTI */ |
Bartlomiej Sieka | 5666846 | 2008-10-01 15:26:28 +0200 | [diff] [blame] | 770 | NetSetTimeout (10000UL, PingTimeout); |
wdenk | e6466f6 | 2003-06-05 19:27:42 +0000 | [diff] [blame] | 771 | NetSetHandler (PingHandler); |
| 772 | |
| 773 | PingSend(); |
| 774 | } |
Jon Loeliger | a9807e5 | 2007-07-10 11:05:02 -0500 | [diff] [blame] | 775 | #endif |
wdenk | 145d2c1 | 2004-04-15 21:48:45 +0000 | [diff] [blame] | 776 | |
Jon Loeliger | 54f35c2 | 2007-07-09 17:45:14 -0500 | [diff] [blame] | 777 | #if defined(CONFIG_CMD_CDP) |
wdenk | 145d2c1 | 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 | 5666846 | 2008-10-01 15:26:28 +0200 | [diff] [blame] | 793 | #define CDP_TIMEOUT 250UL /* one packet every 250ms */ |
wdenk | 145d2c1 | 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 | 7fb5266 | 2005-10-13 16:45:02 +0200 | [diff] [blame] | 809 | ushort *p; |
wdenk | 145d2c1 | 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 | 7fb5266 | 2005-10-13 16:45:02 +0200 | [diff] [blame] | 819 | p = (ushort *)buff; |
| 820 | result += *p++; |
| 821 | buff = (uchar *)p; |
wdenk | 145d2c1 | 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 | f071113 | 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 | 145d2c1 | 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 | 145d2c1 | 2004-04-15 21:48:45 +0000 | [diff] [blame] | 864 | int len; |
| 865 | ushort chksum; |
wdenk | 0593920 | 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 | 145d2c1 | 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 | 79ca1b9 | 2009-02-11 18:23:48 -0500 | [diff] [blame] | 897 | sprintf(buf, CONFIG_CDP_DEVICE_ID_PREFIX "%pm", NetOurEther); |
wdenk | 145d2c1 | 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 |
Luca Ceresoli | 428ab36 | 2011-04-18 06:19:50 +0000 | [diff] [blame^] | 991 | CDPDummyHandler(uchar *pkt, unsigned dest, IPaddr_t sip, unsigned src, |
| 992 | unsigned len) |
wdenk | 145d2c1 | 2004-04-15 21:48:45 +0000 | [diff] [blame] | 993 | { |
| 994 | /* nothing */ |
| 995 | } |
| 996 | |
| 997 | static void |
| 998 | CDPHandler(const uchar * pkt, unsigned len) |
| 999 | { |
| 1000 | const uchar *t; |
| 1001 | const ushort *ss; |
| 1002 | ushort type, tlen; |
| 1003 | uchar applid; |
| 1004 | ushort vlan, nvlan; |
| 1005 | |
| 1006 | /* minimum size? */ |
| 1007 | if (len < sizeof(CDP_SNAP_hdr) + 4) |
| 1008 | goto pkt_short; |
| 1009 | |
| 1010 | /* check for valid CDP SNAP header */ |
| 1011 | if (memcmp(pkt, CDP_SNAP_hdr, sizeof(CDP_SNAP_hdr)) != 0) |
| 1012 | return; |
| 1013 | |
| 1014 | pkt += sizeof(CDP_SNAP_hdr); |
| 1015 | len -= sizeof(CDP_SNAP_hdr); |
| 1016 | |
| 1017 | /* Version of CDP protocol must be >= 2 and TTL != 0 */ |
| 1018 | if (pkt[0] < 0x02 || pkt[1] == 0) |
| 1019 | return; |
| 1020 | |
| 1021 | /* if version is greater than 0x02 maybe we'll have a problem; output a warning */ |
| 1022 | if (pkt[0] != 0x02) |
| 1023 | printf("** WARNING: CDP packet received with a protocol version %d > 2\n", |
| 1024 | pkt[0] & 0xff); |
| 1025 | |
| 1026 | if (CDP_compute_csum(pkt, len) != 0) |
| 1027 | return; |
| 1028 | |
| 1029 | pkt += 4; |
| 1030 | len -= 4; |
| 1031 | |
| 1032 | vlan = htons(-1); |
| 1033 | nvlan = htons(-1); |
| 1034 | while (len > 0) { |
| 1035 | if (len < 4) |
| 1036 | goto pkt_short; |
| 1037 | |
| 1038 | ss = (const ushort *)pkt; |
| 1039 | type = ntohs(ss[0]); |
| 1040 | tlen = ntohs(ss[1]); |
| 1041 | if (tlen > len) { |
| 1042 | goto pkt_short; |
| 1043 | } |
| 1044 | |
| 1045 | pkt += tlen; |
| 1046 | len -= tlen; |
| 1047 | |
| 1048 | ss += 2; /* point ss to the data of the TLV */ |
| 1049 | tlen -= 4; |
| 1050 | |
| 1051 | switch (type) { |
| 1052 | case CDP_DEVICE_ID_TLV: |
| 1053 | break; |
| 1054 | case CDP_ADDRESS_TLV: |
| 1055 | break; |
| 1056 | case CDP_PORT_ID_TLV: |
| 1057 | break; |
| 1058 | case CDP_CAPABILITIES_TLV: |
| 1059 | break; |
| 1060 | case CDP_VERSION_TLV: |
| 1061 | break; |
| 1062 | case CDP_PLATFORM_TLV: |
| 1063 | break; |
| 1064 | case CDP_NATIVE_VLAN_TLV: |
| 1065 | nvlan = *ss; |
| 1066 | break; |
| 1067 | case CDP_APPLIANCE_VLAN_TLV: |
| 1068 | t = (const uchar *)ss; |
| 1069 | while (tlen > 0) { |
| 1070 | if (tlen < 3) |
| 1071 | goto pkt_short; |
| 1072 | |
| 1073 | applid = t[0]; |
| 1074 | ss = (const ushort *)(t + 1); |
| 1075 | |
| 1076 | #ifdef CONFIG_CDP_APPLIANCE_VLAN_TYPE |
| 1077 | if (applid == CONFIG_CDP_APPLIANCE_VLAN_TYPE) |
| 1078 | vlan = *ss; |
| 1079 | #else |
| 1080 | vlan = ntohs(*ss); /* XXX will this work; dunno */ |
| 1081 | #endif |
| 1082 | t += 3; tlen -= 3; |
| 1083 | } |
| 1084 | break; |
| 1085 | case CDP_TRIGGER_TLV: |
| 1086 | break; |
| 1087 | case CDP_POWER_CONSUMPTION_TLV: |
| 1088 | break; |
| 1089 | case CDP_SYSNAME_TLV: |
| 1090 | break; |
| 1091 | case CDP_SYSOBJECT_TLV: |
| 1092 | break; |
| 1093 | case CDP_MANAGEMENT_ADDRESS_TLV: |
| 1094 | break; |
| 1095 | } |
| 1096 | } |
| 1097 | |
| 1098 | CDPApplianceVLAN = vlan; |
| 1099 | CDPNativeVLAN = nvlan; |
| 1100 | |
| 1101 | CDPOK = 1; |
| 1102 | return; |
| 1103 | |
| 1104 | pkt_short: |
| 1105 | printf("** CDP packet is too short\n"); |
| 1106 | return; |
| 1107 | } |
| 1108 | |
| 1109 | static void CDPStart(void) |
| 1110 | { |
| 1111 | #if defined(CONFIG_NET_MULTI) |
| 1112 | printf ("Using %s device\n", eth_get_name()); |
wdenk | e6466f6 | 2003-06-05 19:27:42 +0000 | [diff] [blame] | 1113 | #endif |
wdenk | 145d2c1 | 2004-04-15 21:48:45 +0000 | [diff] [blame] | 1114 | CDPSeq = 0; |
| 1115 | CDPOK = 0; |
| 1116 | |
| 1117 | CDPNativeVLAN = htons(-1); |
| 1118 | CDPApplianceVLAN = htons(-1); |
| 1119 | |
| 1120 | NetSetTimeout (CDP_TIMEOUT, CDPTimeout); |
| 1121 | NetSetHandler (CDPDummyHandler); |
| 1122 | |
| 1123 | CDPSendTrigger(); |
| 1124 | } |
Jon Loeliger | a9807e5 | 2007-07-10 11:05:02 -0500 | [diff] [blame] | 1125 | #endif |
wdenk | 145d2c1 | 2004-04-15 21:48:45 +0000 | [diff] [blame] | 1126 | |
Alessandro Rubini | f119e3d | 2009-08-07 13:58:56 +0200 | [diff] [blame] | 1127 | #ifdef CONFIG_IP_DEFRAG |
| 1128 | /* |
| 1129 | * This function collects fragments in a single packet, according |
| 1130 | * to the algorithm in RFC815. It returns NULL or the pointer to |
| 1131 | * a complete packet, in static storage |
| 1132 | */ |
| 1133 | #ifndef CONFIG_NET_MAXDEFRAG |
| 1134 | #define CONFIG_NET_MAXDEFRAG 16384 |
| 1135 | #endif |
| 1136 | /* |
| 1137 | * MAXDEFRAG, above, is chosen in the config file and is real data |
| 1138 | * so we need to add the NFS overhead, which is more than TFTP. |
| 1139 | * To use sizeof in the internal unnamed structures, we need a real |
| 1140 | * instance (can't do "sizeof(struct rpc_t.u.reply))", unfortunately). |
| 1141 | * The compiler doesn't complain nor allocates the actual structure |
| 1142 | */ |
| 1143 | static struct rpc_t rpc_specimen; |
| 1144 | #define IP_PKTSIZE (CONFIG_NET_MAXDEFRAG + sizeof(rpc_specimen.u.reply)) |
| 1145 | |
| 1146 | #define IP_MAXUDP (IP_PKTSIZE - IP_HDR_SIZE_NO_UDP) |
| 1147 | |
| 1148 | /* |
| 1149 | * this is the packet being assembled, either data or frag control. |
| 1150 | * Fragments go by 8 bytes, so this union must be 8 bytes long |
| 1151 | */ |
| 1152 | struct hole { |
| 1153 | /* first_byte is address of this structure */ |
| 1154 | u16 last_byte; /* last byte in this hole + 1 (begin of next hole) */ |
| 1155 | u16 next_hole; /* index of next (in 8-b blocks), 0 == none */ |
| 1156 | u16 prev_hole; /* index of prev, 0 == none */ |
| 1157 | u16 unused; |
| 1158 | }; |
| 1159 | |
| 1160 | static IP_t *__NetDefragment(IP_t *ip, int *lenp) |
| 1161 | { |
| 1162 | static uchar pkt_buff[IP_PKTSIZE] __attribute__((aligned(PKTALIGN))); |
| 1163 | static u16 first_hole, total_len; |
| 1164 | struct hole *payload, *thisfrag, *h, *newh; |
| 1165 | IP_t *localip = (IP_t *)pkt_buff; |
| 1166 | uchar *indata = (uchar *)ip; |
| 1167 | int offset8, start, len, done = 0; |
| 1168 | u16 ip_off = ntohs(ip->ip_off); |
| 1169 | |
| 1170 | /* payload starts after IP header, this fragment is in there */ |
| 1171 | payload = (struct hole *)(pkt_buff + IP_HDR_SIZE_NO_UDP); |
| 1172 | offset8 = (ip_off & IP_OFFS); |
| 1173 | thisfrag = payload + offset8; |
| 1174 | start = offset8 * 8; |
| 1175 | len = ntohs(ip->ip_len) - IP_HDR_SIZE_NO_UDP; |
| 1176 | |
| 1177 | if (start + len > IP_MAXUDP) /* fragment extends too far */ |
| 1178 | return NULL; |
| 1179 | |
| 1180 | if (!total_len || localip->ip_id != ip->ip_id) { |
| 1181 | /* new (or different) packet, reset structs */ |
| 1182 | total_len = 0xffff; |
| 1183 | payload[0].last_byte = ~0; |
| 1184 | payload[0].next_hole = 0; |
| 1185 | payload[0].prev_hole = 0; |
| 1186 | first_hole = 0; |
| 1187 | /* any IP header will work, copy the first we received */ |
| 1188 | memcpy(localip, ip, IP_HDR_SIZE_NO_UDP); |
| 1189 | } |
| 1190 | |
| 1191 | /* |
| 1192 | * What follows is the reassembly algorithm. We use the payload |
| 1193 | * array as a linked list of hole descriptors, as each hole starts |
| 1194 | * at a multiple of 8 bytes. However, last byte can be whatever value, |
| 1195 | * so it is represented as byte count, not as 8-byte blocks. |
| 1196 | */ |
| 1197 | |
| 1198 | h = payload + first_hole; |
| 1199 | while (h->last_byte < start) { |
| 1200 | if (!h->next_hole) { |
| 1201 | /* no hole that far away */ |
| 1202 | return NULL; |
| 1203 | } |
| 1204 | h = payload + h->next_hole; |
| 1205 | } |
| 1206 | |
Fillod Stephane | e7ade8b | 2010-06-11 19:26:43 +0200 | [diff] [blame] | 1207 | /* last fragment may be 1..7 bytes, the "+7" forces acceptance */ |
| 1208 | if (offset8 + ((len + 7) / 8) <= h - payload) { |
Alessandro Rubini | f119e3d | 2009-08-07 13:58:56 +0200 | [diff] [blame] | 1209 | /* no overlap with holes (dup fragment?) */ |
| 1210 | return NULL; |
| 1211 | } |
| 1212 | |
| 1213 | if (!(ip_off & IP_FLAGS_MFRAG)) { |
| 1214 | /* no more fragmentss: truncate this (last) hole */ |
| 1215 | total_len = start + len; |
| 1216 | h->last_byte = start + len; |
| 1217 | } |
| 1218 | |
| 1219 | /* |
| 1220 | * There is some overlap: fix the hole list. This code doesn't |
| 1221 | * deal with a fragment that overlaps with two different holes |
| 1222 | * (thus being a superset of a previously-received fragment). |
| 1223 | */ |
| 1224 | |
| 1225 | if ( (h >= thisfrag) && (h->last_byte <= start + len) ) { |
| 1226 | /* complete overlap with hole: remove hole */ |
| 1227 | if (!h->prev_hole && !h->next_hole) { |
| 1228 | /* last remaining hole */ |
| 1229 | done = 1; |
| 1230 | } else if (!h->prev_hole) { |
| 1231 | /* first hole */ |
| 1232 | first_hole = h->next_hole; |
| 1233 | payload[h->next_hole].prev_hole = 0; |
| 1234 | } else if (!h->next_hole) { |
| 1235 | /* last hole */ |
| 1236 | payload[h->prev_hole].next_hole = 0; |
| 1237 | } else { |
| 1238 | /* in the middle of the list */ |
| 1239 | payload[h->next_hole].prev_hole = h->prev_hole; |
| 1240 | payload[h->prev_hole].next_hole = h->next_hole; |
| 1241 | } |
| 1242 | |
| 1243 | } else if (h->last_byte <= start + len) { |
| 1244 | /* overlaps with final part of the hole: shorten this hole */ |
| 1245 | h->last_byte = start; |
| 1246 | |
| 1247 | } else if (h >= thisfrag) { |
| 1248 | /* overlaps with initial part of the hole: move this hole */ |
| 1249 | newh = thisfrag + (len / 8); |
| 1250 | *newh = *h; |
| 1251 | h = newh; |
| 1252 | if (h->next_hole) |
| 1253 | payload[h->next_hole].prev_hole = (h - payload); |
| 1254 | if (h->prev_hole) |
| 1255 | payload[h->prev_hole].next_hole = (h - payload); |
| 1256 | else |
| 1257 | first_hole = (h - payload); |
| 1258 | |
| 1259 | } else { |
| 1260 | /* fragment sits in the middle: split the hole */ |
| 1261 | newh = thisfrag + (len / 8); |
| 1262 | *newh = *h; |
| 1263 | h->last_byte = start; |
| 1264 | h->next_hole = (newh - payload); |
| 1265 | newh->prev_hole = (h - payload); |
| 1266 | if (newh->next_hole) |
| 1267 | payload[newh->next_hole].prev_hole = (newh - payload); |
| 1268 | } |
| 1269 | |
| 1270 | /* finally copy this fragment and possibly return whole packet */ |
| 1271 | memcpy((uchar *)thisfrag, indata + IP_HDR_SIZE_NO_UDP, len); |
| 1272 | if (!done) |
| 1273 | return NULL; |
| 1274 | |
| 1275 | localip->ip_len = htons(total_len); |
| 1276 | *lenp = total_len + IP_HDR_SIZE_NO_UDP; |
| 1277 | return localip; |
| 1278 | } |
| 1279 | |
| 1280 | static inline IP_t *NetDefragment(IP_t *ip, int *lenp) |
| 1281 | { |
| 1282 | u16 ip_off = ntohs(ip->ip_off); |
| 1283 | if (!(ip_off & (IP_OFFS | IP_FLAGS_MFRAG))) |
| 1284 | return ip; /* not a fragment */ |
| 1285 | return __NetDefragment(ip, lenp); |
| 1286 | } |
| 1287 | |
| 1288 | #else /* !CONFIG_IP_DEFRAG */ |
| 1289 | |
| 1290 | static inline IP_t *NetDefragment(IP_t *ip, int *lenp) |
| 1291 | { |
| 1292 | u16 ip_off = ntohs(ip->ip_off); |
| 1293 | if (!(ip_off & (IP_OFFS | IP_FLAGS_MFRAG))) |
| 1294 | return ip; /* not a fragment */ |
| 1295 | return NULL; |
| 1296 | } |
| 1297 | #endif |
wdenk | 2d96695 | 2002-10-31 22:12:35 +0000 | [diff] [blame] | 1298 | |
| 1299 | void |
wdenk | 145d2c1 | 2004-04-15 21:48:45 +0000 | [diff] [blame] | 1300 | NetReceive(volatile uchar * inpkt, int len) |
wdenk | 2d96695 | 2002-10-31 22:12:35 +0000 | [diff] [blame] | 1301 | { |
| 1302 | Ethernet_t *et; |
| 1303 | IP_t *ip; |
| 1304 | ARP_t *arp; |
| 1305 | IPaddr_t tmp; |
Luca Ceresoli | 428ab36 | 2011-04-18 06:19:50 +0000 | [diff] [blame^] | 1306 | IPaddr_t src_ip; |
wdenk | 2d96695 | 2002-10-31 22:12:35 +0000 | [diff] [blame] | 1307 | int x; |
wdenk | 145d2c1 | 2004-04-15 21:48:45 +0000 | [diff] [blame] | 1308 | uchar *pkt; |
Jon Loeliger | 54f35c2 | 2007-07-09 17:45:14 -0500 | [diff] [blame] | 1309 | #if defined(CONFIG_CMD_CDP) |
wdenk | 145d2c1 | 2004-04-15 21:48:45 +0000 | [diff] [blame] | 1310 | int iscdp; |
| 1311 | #endif |
| 1312 | ushort cti = 0, vlanid = VLAN_NONE, myvlanid, mynvlanid; |
wdenk | 2d96695 | 2002-10-31 22:12:35 +0000 | [diff] [blame] | 1313 | |
Robin Getz | 9e0a4d6 | 2009-07-23 03:01:03 -0400 | [diff] [blame] | 1314 | debug("packet received\n"); |
wdenk | 145d2c1 | 2004-04-15 21:48:45 +0000 | [diff] [blame] | 1315 | |
Mike Frysinger | dc16603 | 2009-07-18 21:04:08 -0400 | [diff] [blame] | 1316 | NetRxPacket = inpkt; |
| 1317 | NetRxPacketLen = len; |
wdenk | 145d2c1 | 2004-04-15 21:48:45 +0000 | [diff] [blame] | 1318 | et = (Ethernet_t *)inpkt; |
| 1319 | |
| 1320 | /* too small packet? */ |
| 1321 | if (len < ETHER_HDR_SIZE) |
| 1322 | return; |
Rafal Jaworowski | 1f2c9a4 | 2007-12-27 18:19:02 +0100 | [diff] [blame] | 1323 | |
| 1324 | #ifdef CONFIG_API |
| 1325 | if (push_packet) { |
| 1326 | (*push_packet)(inpkt, len); |
| 1327 | return; |
| 1328 | } |
| 1329 | #endif |
wdenk | 145d2c1 | 2004-04-15 21:48:45 +0000 | [diff] [blame] | 1330 | |
Jon Loeliger | 54f35c2 | 2007-07-09 17:45:14 -0500 | [diff] [blame] | 1331 | #if defined(CONFIG_CMD_CDP) |
wdenk | 145d2c1 | 2004-04-15 21:48:45 +0000 | [diff] [blame] | 1332 | /* keep track if packet is CDP */ |
| 1333 | iscdp = memcmp(et->et_dest, NetCDPAddr, 6) == 0; |
| 1334 | #endif |
| 1335 | |
| 1336 | myvlanid = ntohs(NetOurVLAN); |
| 1337 | if (myvlanid == (ushort)-1) |
| 1338 | myvlanid = VLAN_NONE; |
| 1339 | mynvlanid = ntohs(NetOurNativeVLAN); |
| 1340 | if (mynvlanid == (ushort)-1) |
| 1341 | mynvlanid = VLAN_NONE; |
wdenk | 2d96695 | 2002-10-31 22:12:35 +0000 | [diff] [blame] | 1342 | |
| 1343 | x = ntohs(et->et_protlen); |
| 1344 | |
Robin Getz | 9e0a4d6 | 2009-07-23 03:01:03 -0400 | [diff] [blame] | 1345 | debug("packet received\n"); |
wdenk | 145d2c1 | 2004-04-15 21:48:45 +0000 | [diff] [blame] | 1346 | |
wdenk | 2d96695 | 2002-10-31 22:12:35 +0000 | [diff] [blame] | 1347 | if (x < 1514) { |
| 1348 | /* |
| 1349 | * Got a 802 packet. Check the other protocol field. |
| 1350 | */ |
| 1351 | x = ntohs(et->et_prot); |
wdenk | 145d2c1 | 2004-04-15 21:48:45 +0000 | [diff] [blame] | 1352 | |
| 1353 | ip = (IP_t *)(inpkt + E802_HDR_SIZE); |
wdenk | 2d96695 | 2002-10-31 22:12:35 +0000 | [diff] [blame] | 1354 | len -= E802_HDR_SIZE; |
wdenk | 145d2c1 | 2004-04-15 21:48:45 +0000 | [diff] [blame] | 1355 | |
| 1356 | } else if (x != PROT_VLAN) { /* normal packet */ |
| 1357 | ip = (IP_t *)(inpkt + ETHER_HDR_SIZE); |
wdenk | 2d96695 | 2002-10-31 22:12:35 +0000 | [diff] [blame] | 1358 | len -= ETHER_HDR_SIZE; |
wdenk | 145d2c1 | 2004-04-15 21:48:45 +0000 | [diff] [blame] | 1359 | |
| 1360 | } else { /* VLAN packet */ |
| 1361 | VLAN_Ethernet_t *vet = (VLAN_Ethernet_t *)et; |
| 1362 | |
Robin Getz | 9e0a4d6 | 2009-07-23 03:01:03 -0400 | [diff] [blame] | 1363 | debug("VLAN packet received\n"); |
| 1364 | |
wdenk | 145d2c1 | 2004-04-15 21:48:45 +0000 | [diff] [blame] | 1365 | /* too small packet? */ |
| 1366 | if (len < VLAN_ETHER_HDR_SIZE) |
| 1367 | return; |
| 1368 | |
| 1369 | /* if no VLAN active */ |
| 1370 | if ((ntohs(NetOurVLAN) & VLAN_IDMASK) == VLAN_NONE |
Jon Loeliger | 54f35c2 | 2007-07-09 17:45:14 -0500 | [diff] [blame] | 1371 | #if defined(CONFIG_CMD_CDP) |
wdenk | 145d2c1 | 2004-04-15 21:48:45 +0000 | [diff] [blame] | 1372 | && iscdp == 0 |
| 1373 | #endif |
| 1374 | ) |
| 1375 | return; |
| 1376 | |
| 1377 | cti = ntohs(vet->vet_tag); |
| 1378 | vlanid = cti & VLAN_IDMASK; |
| 1379 | x = ntohs(vet->vet_type); |
| 1380 | |
| 1381 | ip = (IP_t *)(inpkt + VLAN_ETHER_HDR_SIZE); |
| 1382 | len -= VLAN_ETHER_HDR_SIZE; |
wdenk | 2d96695 | 2002-10-31 22:12:35 +0000 | [diff] [blame] | 1383 | } |
| 1384 | |
Robin Getz | 9e0a4d6 | 2009-07-23 03:01:03 -0400 | [diff] [blame] | 1385 | debug("Receive from protocol 0x%x\n", x); |
wdenk | 2d96695 | 2002-10-31 22:12:35 +0000 | [diff] [blame] | 1386 | |
Jon Loeliger | 54f35c2 | 2007-07-09 17:45:14 -0500 | [diff] [blame] | 1387 | #if defined(CONFIG_CMD_CDP) |
wdenk | 145d2c1 | 2004-04-15 21:48:45 +0000 | [diff] [blame] | 1388 | if (iscdp) { |
| 1389 | CDPHandler((uchar *)ip, len); |
| 1390 | return; |
| 1391 | } |
| 1392 | #endif |
| 1393 | |
| 1394 | if ((myvlanid & VLAN_IDMASK) != VLAN_NONE) { |
| 1395 | if (vlanid == VLAN_NONE) |
| 1396 | vlanid = (mynvlanid & VLAN_IDMASK); |
| 1397 | /* not matched? */ |
| 1398 | if (vlanid != (myvlanid & VLAN_IDMASK)) |
| 1399 | return; |
| 1400 | } |
| 1401 | |
wdenk | 2d96695 | 2002-10-31 22:12:35 +0000 | [diff] [blame] | 1402 | switch (x) { |
| 1403 | |
| 1404 | case PROT_ARP: |
| 1405 | /* |
| 1406 | * We have to deal with two types of ARP packets: |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 1407 | * - REQUEST packets will be answered by sending our |
| 1408 | * IP address - if we know it. |
| 1409 | * - REPLY packates are expected only after we asked |
| 1410 | * for the TFTP server's or the gateway's ethernet |
| 1411 | * address; so if we receive such a packet, we set |
| 1412 | * the server ethernet address |
wdenk | 2d96695 | 2002-10-31 22:12:35 +0000 | [diff] [blame] | 1413 | */ |
Robin Getz | 9e0a4d6 | 2009-07-23 03:01:03 -0400 | [diff] [blame] | 1414 | debug("Got ARP\n"); |
| 1415 | |
wdenk | 2d96695 | 2002-10-31 22:12:35 +0000 | [diff] [blame] | 1416 | arp = (ARP_t *)ip; |
| 1417 | if (len < ARP_HDR_SIZE) { |
| 1418 | printf("bad length %d < %d\n", len, ARP_HDR_SIZE); |
| 1419 | return; |
| 1420 | } |
| 1421 | if (ntohs(arp->ar_hrd) != ARP_ETHER) { |
| 1422 | return; |
| 1423 | } |
| 1424 | if (ntohs(arp->ar_pro) != PROT_IP) { |
| 1425 | return; |
| 1426 | } |
| 1427 | if (arp->ar_hln != 6) { |
| 1428 | return; |
| 1429 | } |
| 1430 | if (arp->ar_pln != 4) { |
| 1431 | return; |
| 1432 | } |
| 1433 | |
| 1434 | if (NetOurIP == 0) { |
| 1435 | return; |
| 1436 | } |
| 1437 | |
| 1438 | if (NetReadIP(&arp->ar_data[16]) != NetOurIP) { |
| 1439 | return; |
| 1440 | } |
| 1441 | |
| 1442 | switch (ntohs(arp->ar_op)) { |
| 1443 | case ARPOP_REQUEST: /* reply with our IP address */ |
Robin Getz | 9e0a4d6 | 2009-07-23 03:01:03 -0400 | [diff] [blame] | 1444 | debug("Got ARP REQUEST, return our IP\n"); |
wdenk | 145d2c1 | 2004-04-15 21:48:45 +0000 | [diff] [blame] | 1445 | pkt = (uchar *)et; |
| 1446 | pkt += NetSetEther(pkt, et->et_src, PROT_ARP); |
wdenk | 2d96695 | 2002-10-31 22:12:35 +0000 | [diff] [blame] | 1447 | arp->ar_op = htons(ARPOP_REPLY); |
| 1448 | memcpy (&arp->ar_data[10], &arp->ar_data[0], 6); |
| 1449 | NetCopyIP(&arp->ar_data[16], &arp->ar_data[6]); |
| 1450 | memcpy (&arp->ar_data[ 0], NetOurEther, 6); |
| 1451 | NetCopyIP(&arp->ar_data[ 6], &NetOurIP); |
wdenk | 145d2c1 | 2004-04-15 21:48:45 +0000 | [diff] [blame] | 1452 | (void) eth_send((uchar *)et, (pkt - (uchar *)et) + ARP_HDR_SIZE); |
wdenk | 2d96695 | 2002-10-31 22:12:35 +0000 | [diff] [blame] | 1453 | return; |
wdenk | e6466f6 | 2003-06-05 19:27:42 +0000 | [diff] [blame] | 1454 | |
| 1455 | case ARPOP_REPLY: /* arp reply */ |
| 1456 | /* are we waiting for a reply */ |
| 1457 | if (!NetArpWaitPacketIP || !NetArpWaitPacketMAC) |
| 1458 | break; |
Robin Getz | 470a6d4 | 2009-07-21 12:15:28 -0400 | [diff] [blame] | 1459 | |
| 1460 | #ifdef CONFIG_KEEP_SERVERADDR |
| 1461 | if (NetServerIP == NetArpWaitPacketIP) { |
| 1462 | char buf[20]; |
| 1463 | sprintf(buf, "%pM", arp->ar_data); |
| 1464 | setenv("serveraddr", buf); |
| 1465 | } |
| 1466 | #endif |
| 1467 | |
Robin Getz | 9e0a4d6 | 2009-07-23 03:01:03 -0400 | [diff] [blame] | 1468 | debug("Got ARP REPLY, set server/gtwy eth addr (%pM)\n", |
Mike Frysinger | 79ca1b9 | 2009-02-11 18:23:48 -0500 | [diff] [blame] | 1469 | arp->ar_data); |
wdenk | e6466f6 | 2003-06-05 19:27:42 +0000 | [diff] [blame] | 1470 | |
| 1471 | tmp = NetReadIP(&arp->ar_data[6]); |
| 1472 | |
| 1473 | /* matched waiting packet's address */ |
| 1474 | if (tmp == NetArpWaitReplyIP) { |
Robin Getz | 9e0a4d6 | 2009-07-23 03:01:03 -0400 | [diff] [blame] | 1475 | debug("Got it\n"); |
wdenk | e6466f6 | 2003-06-05 19:27:42 +0000 | [diff] [blame] | 1476 | /* save address for later use */ |
| 1477 | memcpy(NetArpWaitPacketMAC, &arp->ar_data[0], 6); |
| 1478 | |
wdenk | b8fb619 | 2004-08-02 21:11:11 +0000 | [diff] [blame] | 1479 | #ifdef CONFIG_NETCONSOLE |
Luca Ceresoli | 428ab36 | 2011-04-18 06:19:50 +0000 | [diff] [blame^] | 1480 | (*packetHandler)(0, 0, 0, 0, 0); |
wdenk | b8fb619 | 2004-08-02 21:11:11 +0000 | [diff] [blame] | 1481 | #endif |
wdenk | e6466f6 | 2003-06-05 19:27:42 +0000 | [diff] [blame] | 1482 | /* modify header, and transmit it */ |
| 1483 | memcpy(((Ethernet_t *)NetArpWaitTxPacket)->et_dest, NetArpWaitPacketMAC, 6); |
| 1484 | (void) eth_send(NetArpWaitTxPacket, NetArpWaitTxPacketSize); |
| 1485 | |
| 1486 | /* no arp request pending now */ |
| 1487 | NetArpWaitPacketIP = 0; |
| 1488 | NetArpWaitTxPacketSize = 0; |
| 1489 | NetArpWaitPacketMAC = NULL; |
| 1490 | |
| 1491 | } |
wdenk | 2d96695 | 2002-10-31 22:12:35 +0000 | [diff] [blame] | 1492 | return; |
| 1493 | default: |
Robin Getz | 9e0a4d6 | 2009-07-23 03:01:03 -0400 | [diff] [blame] | 1494 | debug("Unexpected ARP opcode 0x%x\n", ntohs(arp->ar_op)); |
wdenk | 2d96695 | 2002-10-31 22:12:35 +0000 | [diff] [blame] | 1495 | return; |
| 1496 | } |
wdenk | cb99da5 | 2005-01-12 00:15:14 +0000 | [diff] [blame] | 1497 | break; |
wdenk | 2d96695 | 2002-10-31 22:12:35 +0000 | [diff] [blame] | 1498 | |
Peter Tyser | f7a48ca | 2010-09-30 11:25:48 -0500 | [diff] [blame] | 1499 | #ifdef CONFIG_CMD_RARP |
wdenk | 2d96695 | 2002-10-31 22:12:35 +0000 | [diff] [blame] | 1500 | case PROT_RARP: |
Robin Getz | 9e0a4d6 | 2009-07-23 03:01:03 -0400 | [diff] [blame] | 1501 | debug("Got RARP\n"); |
wdenk | 2d96695 | 2002-10-31 22:12:35 +0000 | [diff] [blame] | 1502 | arp = (ARP_t *)ip; |
| 1503 | if (len < ARP_HDR_SIZE) { |
| 1504 | printf("bad length %d < %d\n", len, ARP_HDR_SIZE); |
| 1505 | return; |
| 1506 | } |
| 1507 | |
| 1508 | if ((ntohs(arp->ar_op) != RARPOP_REPLY) || |
| 1509 | (ntohs(arp->ar_hrd) != ARP_ETHER) || |
| 1510 | (ntohs(arp->ar_pro) != PROT_IP) || |
| 1511 | (arp->ar_hln != 6) || (arp->ar_pln != 4)) { |
| 1512 | |
wdenk | 42c0547 | 2004-03-23 22:14:11 +0000 | [diff] [blame] | 1513 | puts ("invalid RARP header\n"); |
wdenk | 2d96695 | 2002-10-31 22:12:35 +0000 | [diff] [blame] | 1514 | } else { |
| 1515 | NetCopyIP(&NetOurIP, &arp->ar_data[16]); |
wdenk | 4989f87 | 2004-03-14 15:06:13 +0000 | [diff] [blame] | 1516 | if (NetServerIP == 0) |
| 1517 | NetCopyIP(&NetServerIP, &arp->ar_data[ 6]); |
wdenk | 2d96695 | 2002-10-31 22:12:35 +0000 | [diff] [blame] | 1518 | memcpy (NetServerEther, &arp->ar_data[ 0], 6); |
| 1519 | |
Luca Ceresoli | 428ab36 | 2011-04-18 06:19:50 +0000 | [diff] [blame^] | 1520 | (*packetHandler)(0, 0, 0, 0, 0); |
wdenk | 2d96695 | 2002-10-31 22:12:35 +0000 | [diff] [blame] | 1521 | } |
| 1522 | break; |
Peter Tyser | f7a48ca | 2010-09-30 11:25:48 -0500 | [diff] [blame] | 1523 | #endif |
wdenk | 2d96695 | 2002-10-31 22:12:35 +0000 | [diff] [blame] | 1524 | case PROT_IP: |
Robin Getz | 9e0a4d6 | 2009-07-23 03:01:03 -0400 | [diff] [blame] | 1525 | debug("Got IP\n"); |
Alessandro Rubini | f119e3d | 2009-08-07 13:58:56 +0200 | [diff] [blame] | 1526 | /* Before we start poking the header, make sure it is there */ |
wdenk | 2d96695 | 2002-10-31 22:12:35 +0000 | [diff] [blame] | 1527 | if (len < IP_HDR_SIZE) { |
Robin Getz | 9e0a4d6 | 2009-07-23 03:01:03 -0400 | [diff] [blame] | 1528 | debug("len bad %d < %lu\n", len, (ulong)IP_HDR_SIZE); |
wdenk | 2d96695 | 2002-10-31 22:12:35 +0000 | [diff] [blame] | 1529 | return; |
| 1530 | } |
Alessandro Rubini | f119e3d | 2009-08-07 13:58:56 +0200 | [diff] [blame] | 1531 | /* Check the packet length */ |
wdenk | 2d96695 | 2002-10-31 22:12:35 +0000 | [diff] [blame] | 1532 | if (len < ntohs(ip->ip_len)) { |
| 1533 | printf("len bad %d < %d\n", len, ntohs(ip->ip_len)); |
| 1534 | return; |
| 1535 | } |
| 1536 | len = ntohs(ip->ip_len); |
Robin Getz | 9e0a4d6 | 2009-07-23 03:01:03 -0400 | [diff] [blame] | 1537 | debug("len=%d, v=%02x\n", len, ip->ip_hl_v & 0xff); |
| 1538 | |
Alessandro Rubini | f119e3d | 2009-08-07 13:58:56 +0200 | [diff] [blame] | 1539 | /* Can't deal with anything except IPv4 */ |
wdenk | 2d96695 | 2002-10-31 22:12:35 +0000 | [diff] [blame] | 1540 | if ((ip->ip_hl_v & 0xf0) != 0x40) { |
| 1541 | return; |
| 1542 | } |
Alessandro Rubini | f119e3d | 2009-08-07 13:58:56 +0200 | [diff] [blame] | 1543 | /* Can't deal with IP options (headers != 20 bytes) */ |
Remy Bohmer | b953578 | 2008-06-03 15:48:17 +0200 | [diff] [blame] | 1544 | if ((ip->ip_hl_v & 0x0f) > 0x05) { |
| 1545 | return; |
| 1546 | } |
Alessandro Rubini | f119e3d | 2009-08-07 13:58:56 +0200 | [diff] [blame] | 1547 | /* Check the Checksum of the header */ |
wdenk | 2d96695 | 2002-10-31 22:12:35 +0000 | [diff] [blame] | 1548 | if (!NetCksumOk((uchar *)ip, IP_HDR_SIZE_NO_UDP / 2)) { |
wdenk | 42c0547 | 2004-03-23 22:14:11 +0000 | [diff] [blame] | 1549 | puts ("checksum bad\n"); |
wdenk | 2d96695 | 2002-10-31 22:12:35 +0000 | [diff] [blame] | 1550 | return; |
| 1551 | } |
Alessandro Rubini | f119e3d | 2009-08-07 13:58:56 +0200 | [diff] [blame] | 1552 | /* If it is not for us, ignore it */ |
wdenk | 2d96695 | 2002-10-31 22:12:35 +0000 | [diff] [blame] | 1553 | tmp = NetReadIP(&ip->ip_dst); |
| 1554 | if (NetOurIP && tmp != NetOurIP && tmp != 0xFFFFFFFF) { |
David Updegraff | 7280da7 | 2007-06-11 10:41:07 -0500 | [diff] [blame] | 1555 | #ifdef CONFIG_MCAST_TFTP |
Wolfgang Denk | 627f5c3 | 2007-08-14 09:47:27 +0200 | [diff] [blame] | 1556 | if (Mcast_addr != tmp) |
David Updegraff | 7280da7 | 2007-06-11 10:41:07 -0500 | [diff] [blame] | 1557 | #endif |
wdenk | 2d96695 | 2002-10-31 22:12:35 +0000 | [diff] [blame] | 1558 | return; |
| 1559 | } |
Luca Ceresoli | 428ab36 | 2011-04-18 06:19:50 +0000 | [diff] [blame^] | 1560 | /* Read source IP address for later use */ |
| 1561 | src_ip = NetReadIP(&ip->ip_src); |
wdenk | 2d96695 | 2002-10-31 22:12:35 +0000 | [diff] [blame] | 1562 | /* |
Alessandro Rubini | f119e3d | 2009-08-07 13:58:56 +0200 | [diff] [blame] | 1563 | * The function returns the unchanged packet if it's not |
| 1564 | * a fragment, and either the complete packet or NULL if |
| 1565 | * it is a fragment (if !CONFIG_IP_DEFRAG, it returns NULL) |
| 1566 | */ |
| 1567 | if (!(ip = NetDefragment(ip, &len))) |
| 1568 | return; |
| 1569 | /* |
wdenk | 2d96695 | 2002-10-31 22:12:35 +0000 | [diff] [blame] | 1570 | * watch for ICMP host redirects |
| 1571 | * |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 1572 | * There is no real handler code (yet). We just watch |
| 1573 | * for ICMP host redirect messages. In case anybody |
| 1574 | * sees these messages: please contact me |
| 1575 | * (wd@denx.de), or - even better - send me the |
| 1576 | * necessary fixes :-) |
wdenk | 2d96695 | 2002-10-31 22:12:35 +0000 | [diff] [blame] | 1577 | * |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 1578 | * Note: in all cases where I have seen this so far |
| 1579 | * it was a problem with the router configuration, |
| 1580 | * for instance when a router was configured in the |
| 1581 | * BOOTP reply, but the TFTP server was on the same |
| 1582 | * subnet. So this is probably a warning that your |
| 1583 | * configuration might be wrong. But I'm not really |
| 1584 | * sure if there aren't any other situations. |
wdenk | 2d96695 | 2002-10-31 22:12:35 +0000 | [diff] [blame] | 1585 | */ |
| 1586 | if (ip->ip_p == IPPROTO_ICMP) { |
| 1587 | ICMP_t *icmph = (ICMP_t *)&(ip->udp_src); |
| 1588 | |
wdenk | e6466f6 | 2003-06-05 19:27:42 +0000 | [diff] [blame] | 1589 | switch (icmph->type) { |
| 1590 | case ICMP_REDIRECT: |
wdenk | cbc49a5 | 2005-05-03 14:12:25 +0000 | [diff] [blame] | 1591 | if (icmph->code != ICMP_REDIR_HOST) |
| 1592 | return; |
Mike Frysinger | d5695f4 | 2009-02-17 00:00:53 -0500 | [diff] [blame] | 1593 | printf (" ICMP Host Redirect to %pI4 ", &icmph->un.gateway); |
Stefan Roese | dfced81 | 2005-08-12 20:06:52 +0200 | [diff] [blame] | 1594 | return; |
Jon Loeliger | 54f35c2 | 2007-07-09 17:45:14 -0500 | [diff] [blame] | 1595 | #if defined(CONFIG_CMD_PING) |
wdenk | e6466f6 | 2003-06-05 19:27:42 +0000 | [diff] [blame] | 1596 | case ICMP_ECHO_REPLY: |
| 1597 | /* |
| 1598 | * IP header OK. Pass the packet to the current handler. |
| 1599 | */ |
| 1600 | /* XXX point to ip packet */ |
Luca Ceresoli | 428ab36 | 2011-04-18 06:19:50 +0000 | [diff] [blame^] | 1601 | (*packetHandler)((uchar *)ip, 0, src_ip, 0, 0); |
Stefan Roese | dfced81 | 2005-08-12 20:06:52 +0200 | [diff] [blame] | 1602 | return; |
Ed Swarthout | 8310919 | 2007-03-07 12:14:50 -0600 | [diff] [blame] | 1603 | case ICMP_ECHO_REQUEST: |
Luca Ceresoli | 428ab36 | 2011-04-18 06:19:50 +0000 | [diff] [blame^] | 1604 | debug("Got ICMP ECHO REQUEST, " |
| 1605 | "return %d bytes\n", |
| 1606 | ETHER_HDR_SIZE + len); |
Robin Getz | 9e0a4d6 | 2009-07-23 03:01:03 -0400 | [diff] [blame] | 1607 | |
Ed Swarthout | 8310919 | 2007-03-07 12:14:50 -0600 | [diff] [blame] | 1608 | memcpy (&et->et_dest[0], &et->et_src[0], 6); |
| 1609 | memcpy (&et->et_src[ 0], NetOurEther, 6); |
| 1610 | |
| 1611 | ip->ip_sum = 0; |
| 1612 | ip->ip_off = 0; |
| 1613 | NetCopyIP((void*)&ip->ip_dst, &ip->ip_src); |
| 1614 | NetCopyIP((void*)&ip->ip_src, &NetOurIP); |
| 1615 | ip->ip_sum = ~NetCksum((uchar *)ip, IP_HDR_SIZE_NO_UDP >> 1); |
| 1616 | |
| 1617 | icmph->type = ICMP_ECHO_REPLY; |
| 1618 | icmph->checksum = 0; |
| 1619 | icmph->checksum = ~NetCksum((uchar *)icmph, |
| 1620 | (len - IP_HDR_SIZE_NO_UDP) >> 1); |
| 1621 | (void) eth_send((uchar *)et, ETHER_HDR_SIZE + len); |
| 1622 | return; |
wdenk | e6466f6 | 2003-06-05 19:27:42 +0000 | [diff] [blame] | 1623 | #endif |
| 1624 | default: |
| 1625 | return; |
| 1626 | } |
wdenk | 2d96695 | 2002-10-31 22:12:35 +0000 | [diff] [blame] | 1627 | } else if (ip->ip_p != IPPROTO_UDP) { /* Only UDP packets */ |
| 1628 | return; |
| 1629 | } |
| 1630 | |
Stefan Roese | dfced81 | 2005-08-12 20:06:52 +0200 | [diff] [blame] | 1631 | #ifdef CONFIG_UDP_CHECKSUM |
| 1632 | if (ip->udp_xsum != 0) { |
Wolfgang Denk | 30b8732 | 2005-08-12 23:43:12 +0200 | [diff] [blame] | 1633 | ulong xsum; |
Stefan Roese | dfced81 | 2005-08-12 20:06:52 +0200 | [diff] [blame] | 1634 | ushort *sumptr; |
| 1635 | ushort sumlen; |
| 1636 | |
| 1637 | xsum = ip->ip_p; |
| 1638 | xsum += (ntohs(ip->udp_len)); |
| 1639 | xsum += (ntohl(ip->ip_src) >> 16) & 0x0000ffff; |
| 1640 | xsum += (ntohl(ip->ip_src) >> 0) & 0x0000ffff; |
| 1641 | xsum += (ntohl(ip->ip_dst) >> 16) & 0x0000ffff; |
| 1642 | xsum += (ntohl(ip->ip_dst) >> 0) & 0x0000ffff; |
| 1643 | |
| 1644 | sumlen = ntohs(ip->udp_len); |
| 1645 | sumptr = (ushort *) &(ip->udp_src); |
| 1646 | |
| 1647 | while (sumlen > 1) { |
Wolfgang Denk | 30b8732 | 2005-08-12 23:43:12 +0200 | [diff] [blame] | 1648 | ushort sumdata; |
Stefan Roese | dfced81 | 2005-08-12 20:06:52 +0200 | [diff] [blame] | 1649 | |
| 1650 | sumdata = *sumptr++; |
| 1651 | xsum += ntohs(sumdata); |
| 1652 | sumlen -= 2; |
| 1653 | } |
| 1654 | if (sumlen > 0) { |
Wolfgang Denk | 30b8732 | 2005-08-12 23:43:12 +0200 | [diff] [blame] | 1655 | ushort sumdata; |
Stefan Roese | dfced81 | 2005-08-12 20:06:52 +0200 | [diff] [blame] | 1656 | |
| 1657 | sumdata = *(unsigned char *) sumptr; |
Wolfgang Denk | 30b8732 | 2005-08-12 23:43:12 +0200 | [diff] [blame] | 1658 | sumdata = (sumdata << 8) & 0xff00; |
Stefan Roese | dfced81 | 2005-08-12 20:06:52 +0200 | [diff] [blame] | 1659 | xsum += sumdata; |
| 1660 | } |
| 1661 | while ((xsum >> 16) != 0) { |
Wolfgang Denk | 30b8732 | 2005-08-12 23:43:12 +0200 | [diff] [blame] | 1662 | xsum = (xsum & 0x0000ffff) + ((xsum >> 16) & 0x0000ffff); |
Stefan Roese | dfced81 | 2005-08-12 20:06:52 +0200 | [diff] [blame] | 1663 | } |
| 1664 | if ((xsum != 0x00000000) && (xsum != 0x0000ffff)) { |
Wolfgang Denk | 12cec0a | 2008-07-11 01:16:00 +0200 | [diff] [blame] | 1665 | printf(" UDP wrong checksum %08lx %08x\n", |
| 1666 | xsum, ntohs(ip->udp_xsum)); |
Stefan Roese | dfced81 | 2005-08-12 20:06:52 +0200 | [diff] [blame] | 1667 | return; |
| 1668 | } |
| 1669 | } |
| 1670 | #endif |
| 1671 | |
David Updegraff | 7280da7 | 2007-06-11 10:41:07 -0500 | [diff] [blame] | 1672 | |
wdenk | b8fb619 | 2004-08-02 21:11:11 +0000 | [diff] [blame] | 1673 | #ifdef CONFIG_NETCONSOLE |
| 1674 | nc_input_packet((uchar *)ip +IP_HDR_SIZE, |
| 1675 | ntohs(ip->udp_dst), |
| 1676 | ntohs(ip->udp_src), |
| 1677 | ntohs(ip->udp_len) - 8); |
| 1678 | #endif |
wdenk | 2d96695 | 2002-10-31 22:12:35 +0000 | [diff] [blame] | 1679 | /* |
| 1680 | * IP header OK. Pass the packet to the current handler. |
| 1681 | */ |
| 1682 | (*packetHandler)((uchar *)ip +IP_HDR_SIZE, |
| 1683 | ntohs(ip->udp_dst), |
Luca Ceresoli | 428ab36 | 2011-04-18 06:19:50 +0000 | [diff] [blame^] | 1684 | src_ip, |
wdenk | 2d96695 | 2002-10-31 22:12:35 +0000 | [diff] [blame] | 1685 | ntohs(ip->udp_src), |
| 1686 | ntohs(ip->udp_len) - 8); |
wdenk | 2d96695 | 2002-10-31 22:12:35 +0000 | [diff] [blame] | 1687 | break; |
| 1688 | } |
| 1689 | } |
| 1690 | |
| 1691 | |
| 1692 | /**********************************************************************/ |
| 1693 | |
| 1694 | static int net_check_prereq (proto_t protocol) |
| 1695 | { |
| 1696 | switch (protocol) { |
wdenk | 0593920 | 2004-04-18 17:39:38 +0000 | [diff] [blame] | 1697 | /* Fall through */ |
Jon Loeliger | 54f35c2 | 2007-07-09 17:45:14 -0500 | [diff] [blame] | 1698 | #if defined(CONFIG_CMD_PING) |
wdenk | e6466f6 | 2003-06-05 19:27:42 +0000 | [diff] [blame] | 1699 | case PING: |
wdenk | 0593920 | 2004-04-18 17:39:38 +0000 | [diff] [blame] | 1700 | if (NetPingIP == 0) { |
| 1701 | puts ("*** ERROR: ping address not given\n"); |
| 1702 | return (1); |
| 1703 | } |
| 1704 | goto common; |
wdenk | e6466f6 | 2003-06-05 19:27:42 +0000 | [diff] [blame] | 1705 | #endif |
Jon Loeliger | 54f35c2 | 2007-07-09 17:45:14 -0500 | [diff] [blame] | 1706 | #if defined(CONFIG_CMD_SNTP) |
wdenk | b4ad962 | 2005-04-01 00:25:43 +0000 | [diff] [blame] | 1707 | case SNTP: |
| 1708 | if (NetNtpServerIP == 0) { |
| 1709 | puts ("*** ERROR: NTP server address not given\n"); |
| 1710 | return (1); |
| 1711 | } |
| 1712 | goto common; |
| 1713 | #endif |
Robin Getz | 82f0d23 | 2009-07-20 14:53:54 -0400 | [diff] [blame] | 1714 | #if defined(CONFIG_CMD_DNS) |
| 1715 | case DNS: |
| 1716 | if (NetOurDNSIP == 0) { |
| 1717 | puts("*** ERROR: DNS server address not given\n"); |
| 1718 | return 1; |
| 1719 | } |
| 1720 | goto common; |
| 1721 | #endif |
Jon Loeliger | 54f35c2 | 2007-07-09 17:45:14 -0500 | [diff] [blame] | 1722 | #if defined(CONFIG_CMD_NFS) |
wdenk | be9c1cb | 2004-02-24 02:00:03 +0000 | [diff] [blame] | 1723 | case NFS: |
| 1724 | #endif |
wdenk | 2d96695 | 2002-10-31 22:12:35 +0000 | [diff] [blame] | 1725 | case TFTP: |
wdenk | 0593920 | 2004-04-18 17:39:38 +0000 | [diff] [blame] | 1726 | if (NetServerIP == 0) { |
| 1727 | puts ("*** ERROR: `serverip' not set\n"); |
| 1728 | return (1); |
| 1729 | } |
Gray Remlin | fa51d4c | 2011-03-29 10:21:32 +0000 | [diff] [blame] | 1730 | #if defined(CONFIG_CMD_PING) || defined(CONFIG_CMD_SNTP) || \ |
| 1731 | defined(CONFIG_CMD_DNS) |
Wolfgang Denk | 30b8732 | 2005-08-12 23:43:12 +0200 | [diff] [blame] | 1732 | common: |
wdenk | e6466f6 | 2003-06-05 19:27:42 +0000 | [diff] [blame] | 1733 | #endif |
Simon Guinot | 00dceba | 2011-05-01 23:38:40 +0000 | [diff] [blame] | 1734 | /* Fall through */ |
wdenk | e6466f6 | 2003-06-05 19:27:42 +0000 | [diff] [blame] | 1735 | |
Simon Guinot | 00dceba | 2011-05-01 23:38:40 +0000 | [diff] [blame] | 1736 | case NETCONS: |
wdenk | 0593920 | 2004-04-18 17:39:38 +0000 | [diff] [blame] | 1737 | if (NetOurIP == 0) { |
| 1738 | puts ("*** ERROR: `ipaddr' not set\n"); |
| 1739 | return (1); |
| 1740 | } |
| 1741 | /* Fall through */ |
wdenk | 2d96695 | 2002-10-31 22:12:35 +0000 | [diff] [blame] | 1742 | |
Peter Tyser | f7a48ca | 2010-09-30 11:25:48 -0500 | [diff] [blame] | 1743 | #ifdef CONFIG_CMD_RARP |
wdenk | 2d96695 | 2002-10-31 22:12:35 +0000 | [diff] [blame] | 1744 | case RARP: |
Peter Tyser | f7a48ca | 2010-09-30 11:25:48 -0500 | [diff] [blame] | 1745 | #endif |
wdenk | 2d96695 | 2002-10-31 22:12:35 +0000 | [diff] [blame] | 1746 | case BOOTP: |
wdenk | 145d2c1 | 2004-04-15 21:48:45 +0000 | [diff] [blame] | 1747 | case CDP: |
Peter Tyser | f7a48ca | 2010-09-30 11:25:48 -0500 | [diff] [blame] | 1748 | case DHCP: |
wdenk | 0593920 | 2004-04-18 17:39:38 +0000 | [diff] [blame] | 1749 | if (memcmp (NetOurEther, "\0\0\0\0\0\0", 6) == 0) { |
wdenk | 2d96695 | 2002-10-31 22:12:35 +0000 | [diff] [blame] | 1750 | #ifdef CONFIG_NET_MULTI |
wdenk | 0593920 | 2004-04-18 17:39:38 +0000 | [diff] [blame] | 1751 | extern int eth_get_dev_index (void); |
| 1752 | int num = eth_get_dev_index (); |
wdenk | 2d96695 | 2002-10-31 22:12:35 +0000 | [diff] [blame] | 1753 | |
wdenk | 0593920 | 2004-04-18 17:39:38 +0000 | [diff] [blame] | 1754 | switch (num) { |
| 1755 | case -1: |
wdenk | 2d96695 | 2002-10-31 22:12:35 +0000 | [diff] [blame] | 1756 | puts ("*** ERROR: No ethernet found.\n"); |
| 1757 | return (1); |
wdenk | 0593920 | 2004-04-18 17:39:38 +0000 | [diff] [blame] | 1758 | case 0: |
wdenk | 2d96695 | 2002-10-31 22:12:35 +0000 | [diff] [blame] | 1759 | puts ("*** ERROR: `ethaddr' not set\n"); |
| 1760 | break; |
wdenk | 0593920 | 2004-04-18 17:39:38 +0000 | [diff] [blame] | 1761 | default: |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 1762 | printf ("*** ERROR: `eth%daddr' not set\n", |
wdenk | 2d96695 | 2002-10-31 22:12:35 +0000 | [diff] [blame] | 1763 | num); |
| 1764 | break; |
wdenk | 0593920 | 2004-04-18 17:39:38 +0000 | [diff] [blame] | 1765 | } |
wdenk | 2d96695 | 2002-10-31 22:12:35 +0000 | [diff] [blame] | 1766 | |
wdenk | 0593920 | 2004-04-18 17:39:38 +0000 | [diff] [blame] | 1767 | NetStartAgain (); |
| 1768 | return (2); |
wdenk | 2d96695 | 2002-10-31 22:12:35 +0000 | [diff] [blame] | 1769 | #else |
wdenk | 0593920 | 2004-04-18 17:39:38 +0000 | [diff] [blame] | 1770 | puts ("*** ERROR: `ethaddr' not set\n"); |
| 1771 | return (1); |
wdenk | 2d96695 | 2002-10-31 22:12:35 +0000 | [diff] [blame] | 1772 | #endif |
wdenk | 0593920 | 2004-04-18 17:39:38 +0000 | [diff] [blame] | 1773 | } |
| 1774 | /* Fall through */ |
| 1775 | default: |
| 1776 | return (0); |
wdenk | 2d96695 | 2002-10-31 22:12:35 +0000 | [diff] [blame] | 1777 | } |
wdenk | 0593920 | 2004-04-18 17:39:38 +0000 | [diff] [blame] | 1778 | return (0); /* OK */ |
wdenk | 2d96695 | 2002-10-31 22:12:35 +0000 | [diff] [blame] | 1779 | } |
| 1780 | /**********************************************************************/ |
| 1781 | |
| 1782 | int |
| 1783 | NetCksumOk(uchar * ptr, int len) |
| 1784 | { |
| 1785 | return !((NetCksum(ptr, len) + 1) & 0xfffe); |
| 1786 | } |
| 1787 | |
| 1788 | |
| 1789 | unsigned |
| 1790 | NetCksum(uchar * ptr, int len) |
| 1791 | { |
| 1792 | ulong xsum; |
Stefan Roese | ac55328 | 2005-08-31 12:55:50 +0200 | [diff] [blame] | 1793 | ushort *p = (ushort *)ptr; |
wdenk | 2d96695 | 2002-10-31 22:12:35 +0000 | [diff] [blame] | 1794 | |
| 1795 | xsum = 0; |
| 1796 | while (len-- > 0) |
Wolfgang Denk | 3135c54 | 2005-08-26 01:36:03 +0200 | [diff] [blame] | 1797 | xsum += *p++; |
wdenk | 2d96695 | 2002-10-31 22:12:35 +0000 | [diff] [blame] | 1798 | xsum = (xsum & 0xffff) + (xsum >> 16); |
| 1799 | xsum = (xsum & 0xffff) + (xsum >> 16); |
| 1800 | return (xsum & 0xffff); |
| 1801 | } |
| 1802 | |
wdenk | 145d2c1 | 2004-04-15 21:48:45 +0000 | [diff] [blame] | 1803 | int |
| 1804 | NetEthHdrSize(void) |
| 1805 | { |
| 1806 | ushort myvlanid; |
| 1807 | |
| 1808 | myvlanid = ntohs(NetOurVLAN); |
| 1809 | if (myvlanid == (ushort)-1) |
| 1810 | myvlanid = VLAN_NONE; |
wdenk | 2d96695 | 2002-10-31 22:12:35 +0000 | [diff] [blame] | 1811 | |
wdenk | 145d2c1 | 2004-04-15 21:48:45 +0000 | [diff] [blame] | 1812 | return ((myvlanid & VLAN_IDMASK) == VLAN_NONE) ? ETHER_HDR_SIZE : VLAN_ETHER_HDR_SIZE; |
| 1813 | } |
| 1814 | |
| 1815 | int |
wdenk | 2d96695 | 2002-10-31 22:12:35 +0000 | [diff] [blame] | 1816 | NetSetEther(volatile uchar * xet, uchar * addr, uint prot) |
| 1817 | { |
| 1818 | Ethernet_t *et = (Ethernet_t *)xet; |
wdenk | 145d2c1 | 2004-04-15 21:48:45 +0000 | [diff] [blame] | 1819 | ushort myvlanid; |
| 1820 | |
| 1821 | myvlanid = ntohs(NetOurVLAN); |
| 1822 | if (myvlanid == (ushort)-1) |
| 1823 | myvlanid = VLAN_NONE; |
wdenk | 2d96695 | 2002-10-31 22:12:35 +0000 | [diff] [blame] | 1824 | |
| 1825 | memcpy (et->et_dest, addr, 6); |
| 1826 | memcpy (et->et_src, NetOurEther, 6); |
wdenk | 145d2c1 | 2004-04-15 21:48:45 +0000 | [diff] [blame] | 1827 | if ((myvlanid & VLAN_IDMASK) == VLAN_NONE) { |
wdenk | 2d96695 | 2002-10-31 22:12:35 +0000 | [diff] [blame] | 1828 | et->et_protlen = htons(prot); |
wdenk | 145d2c1 | 2004-04-15 21:48:45 +0000 | [diff] [blame] | 1829 | return ETHER_HDR_SIZE; |
| 1830 | } else { |
| 1831 | VLAN_Ethernet_t *vet = (VLAN_Ethernet_t *)xet; |
wdenk | 2d96695 | 2002-10-31 22:12:35 +0000 | [diff] [blame] | 1832 | |
wdenk | 145d2c1 | 2004-04-15 21:48:45 +0000 | [diff] [blame] | 1833 | vet->vet_vlan_type = htons(PROT_VLAN); |
| 1834 | vet->vet_tag = htons((0 << 5) | (myvlanid & VLAN_IDMASK)); |
| 1835 | vet->vet_type = htons(prot); |
| 1836 | return VLAN_ETHER_HDR_SIZE; |
| 1837 | } |
| 1838 | } |
wdenk | 2d96695 | 2002-10-31 22:12:35 +0000 | [diff] [blame] | 1839 | |
| 1840 | void |
| 1841 | NetSetIP(volatile uchar * xip, IPaddr_t dest, int dport, int sport, int len) |
| 1842 | { |
Olav Morken | e8a0277 | 2009-01-23 12:56:26 +0100 | [diff] [blame] | 1843 | IP_t *ip = (IP_t *)xip; |
wdenk | 2d96695 | 2002-10-31 22:12:35 +0000 | [diff] [blame] | 1844 | |
| 1845 | /* |
| 1846 | * If the data is an odd number of bytes, zero the |
| 1847 | * byte after the last byte so that the checksum |
| 1848 | * will work. |
| 1849 | */ |
| 1850 | if (len & 1) |
| 1851 | xip[IP_HDR_SIZE + len] = 0; |
| 1852 | |
| 1853 | /* |
| 1854 | * Construct an IP and UDP header. |
wdenk | 0593920 | 2004-04-18 17:39:38 +0000 | [diff] [blame] | 1855 | * (need to set no fragment bit - XXX) |
wdenk | 2d96695 | 2002-10-31 22:12:35 +0000 | [diff] [blame] | 1856 | */ |
| 1857 | ip->ip_hl_v = 0x45; /* IP_HDR_SIZE / 4 (not including UDP) */ |
| 1858 | ip->ip_tos = 0; |
| 1859 | ip->ip_len = htons(IP_HDR_SIZE + len); |
| 1860 | ip->ip_id = htons(NetIPID++); |
Peter Tyser | 0885bf0 | 2008-12-01 16:26:20 -0600 | [diff] [blame] | 1861 | ip->ip_off = htons(IP_FLAGS_DFRAG); /* Don't fragment */ |
wdenk | 2d96695 | 2002-10-31 22:12:35 +0000 | [diff] [blame] | 1862 | ip->ip_ttl = 255; |
| 1863 | ip->ip_p = 17; /* UDP */ |
| 1864 | ip->ip_sum = 0; |
| 1865 | NetCopyIP((void*)&ip->ip_src, &NetOurIP); /* already in network byte order */ |
| 1866 | NetCopyIP((void*)&ip->ip_dst, &dest); /* - "" - */ |
| 1867 | ip->udp_src = htons(sport); |
| 1868 | ip->udp_dst = htons(dport); |
| 1869 | ip->udp_len = htons(8 + len); |
| 1870 | ip->udp_xsum = 0; |
| 1871 | ip->ip_sum = ~NetCksum((uchar *)ip, IP_HDR_SIZE_NO_UDP / 2); |
| 1872 | } |
| 1873 | |
Mike Frysinger | c8ecaa3 | 2010-10-20 07:16:40 -0400 | [diff] [blame] | 1874 | void copy_filename (char *dst, const char *src, int size) |
wdenk | 2d96695 | 2002-10-31 22:12:35 +0000 | [diff] [blame] | 1875 | { |
| 1876 | if (*src && (*src == '"')) { |
| 1877 | ++src; |
| 1878 | --size; |
| 1879 | } |
| 1880 | |
| 1881 | while ((--size > 0) && *src && (*src != '"')) { |
| 1882 | *dst++ = *src++; |
| 1883 | } |
| 1884 | *dst = '\0'; |
| 1885 | } |
| 1886 | |
Robin Getz | 82f0d23 | 2009-07-20 14:53:54 -0400 | [diff] [blame] | 1887 | #if defined(CONFIG_CMD_NFS) || defined(CONFIG_CMD_SNTP) || defined(CONFIG_CMD_DNS) |
| 1888 | /* |
Robin Getz | 55b50e9 | 2010-03-08 14:07:00 -0500 | [diff] [blame] | 1889 | * make port a little random (1024-17407) |
| 1890 | * This keeps the math somewhat trivial to compute, and seems to work with |
| 1891 | * all supported protocols/clients/servers |
Robin Getz | 82f0d23 | 2009-07-20 14:53:54 -0400 | [diff] [blame] | 1892 | */ |
| 1893 | unsigned int random_port(void) |
| 1894 | { |
Robin Getz | 55b50e9 | 2010-03-08 14:07:00 -0500 | [diff] [blame] | 1895 | return 1024 + (get_timer(0) % 0x4000); |
Robin Getz | 82f0d23 | 2009-07-20 14:53:54 -0400 | [diff] [blame] | 1896 | } |
| 1897 | #endif |
| 1898 | |
wdenk | 2d96695 | 2002-10-31 22:12:35 +0000 | [diff] [blame] | 1899 | void ip_to_string (IPaddr_t x, char *s) |
| 1900 | { |
wdenk | 145d2c1 | 2004-04-15 21:48:45 +0000 | [diff] [blame] | 1901 | x = ntohl (x); |
| 1902 | sprintf (s, "%d.%d.%d.%d", |
| 1903 | (int) ((x >> 24) & 0xff), |
| 1904 | (int) ((x >> 16) & 0xff), |
| 1905 | (int) ((x >> 8) & 0xff), (int) ((x >> 0) & 0xff) |
wdenk | 0593920 | 2004-04-18 17:39:38 +0000 | [diff] [blame] | 1906 | ); |
wdenk | 2d96695 | 2002-10-31 22:12:35 +0000 | [diff] [blame] | 1907 | } |
| 1908 | |
wdenk | 145d2c1 | 2004-04-15 21:48:45 +0000 | [diff] [blame] | 1909 | void VLAN_to_string(ushort x, char *s) |
| 1910 | { |
| 1911 | x = ntohs(x); |
| 1912 | |
| 1913 | if (x == (ushort)-1) |
| 1914 | x = VLAN_NONE; |
| 1915 | |
| 1916 | if (x == VLAN_NONE) |
| 1917 | strcpy(s, "none"); |
| 1918 | else |
| 1919 | sprintf(s, "%d", x & VLAN_IDMASK); |
| 1920 | } |
| 1921 | |
Mike Frysinger | 89c7392 | 2010-10-20 07:16:48 -0400 | [diff] [blame] | 1922 | ushort string_to_VLAN(const char *s) |
wdenk | 145d2c1 | 2004-04-15 21:48:45 +0000 | [diff] [blame] | 1923 | { |
| 1924 | ushort id; |
| 1925 | |
| 1926 | if (s == NULL) |
wdenk | 656140b | 2004-04-25 13:18:40 +0000 | [diff] [blame] | 1927 | return htons(VLAN_NONE); |
wdenk | 145d2c1 | 2004-04-15 21:48:45 +0000 | [diff] [blame] | 1928 | |
| 1929 | if (*s < '0' || *s > '9') |
| 1930 | id = VLAN_NONE; |
| 1931 | else |
| 1932 | id = (ushort)simple_strtoul(s, NULL, 10); |
| 1933 | |
wdenk | 656140b | 2004-04-25 13:18:40 +0000 | [diff] [blame] | 1934 | return htons(id); |
wdenk | 145d2c1 | 2004-04-15 21:48:45 +0000 | [diff] [blame] | 1935 | } |
| 1936 | |
wdenk | 145d2c1 | 2004-04-15 21:48:45 +0000 | [diff] [blame] | 1937 | ushort getenv_VLAN(char *var) |
| 1938 | { |
| 1939 | return (string_to_VLAN(getenv(var))); |
| 1940 | } |