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