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 | * |
Joe Hershberger | b35a3a6 | 2012-05-23 08:00:12 +0000 | [diff] [blame] | 26 | * LINK_LOCAL: |
| 27 | * |
| 28 | * Prerequisites: - own ethernet address |
| 29 | * We want: - own IP address |
| 30 | * Next step: ARP |
| 31 | * |
wdenk | 2d96695 | 2002-10-31 22:12:35 +0000 | [diff] [blame] | 32 | * RARP: |
| 33 | * |
| 34 | * Prerequisites: - own ethernet address |
| 35 | * We want: - own IP address |
| 36 | * - TFTP server IP address |
| 37 | * Next step: ARP |
| 38 | * |
| 39 | * ARP: |
| 40 | * |
| 41 | * Prerequisites: - own ethernet address |
| 42 | * - own IP address |
| 43 | * - TFTP server IP address |
| 44 | * We want: - TFTP server ethernet address |
| 45 | * Next step: TFTP |
| 46 | * |
| 47 | * DHCP: |
| 48 | * |
Wolfgang Denk | 30b8732 | 2005-08-12 23:43:12 +0200 | [diff] [blame] | 49 | * Prerequisites: - own ethernet address |
| 50 | * We want: - IP, Netmask, ServerIP, Gateway IP |
| 51 | * - bootfilename, lease time |
| 52 | * Next step: - TFTP |
wdenk | 2d96695 | 2002-10-31 22:12:35 +0000 | [diff] [blame] | 53 | * |
| 54 | * TFTP: |
| 55 | * |
| 56 | * Prerequisites: - own ethernet address |
| 57 | * - own IP address |
| 58 | * - TFTP server IP address |
| 59 | * - TFTP server ethernet address |
| 60 | * - name of bootfile (if unknown, we use a default name |
| 61 | * derived from our own IP address) |
| 62 | * We want: - load the boot file |
| 63 | * Next step: none |
wdenk | be9c1cb | 2004-02-24 02:00:03 +0000 | [diff] [blame] | 64 | * |
| 65 | * NFS: |
| 66 | * |
| 67 | * Prerequisites: - own ethernet address |
| 68 | * - own IP address |
| 69 | * - name of bootfile (if unknown, we use a default name |
| 70 | * derived from our own IP address) |
| 71 | * We want: - load the boot file |
| 72 | * Next step: none |
wdenk | b4ad962 | 2005-04-01 00:25:43 +0000 | [diff] [blame] | 73 | * |
| 74 | * SNTP: |
| 75 | * |
Wolfgang Denk | 30b8732 | 2005-08-12 23:43:12 +0200 | [diff] [blame] | 76 | * Prerequisites: - own ethernet address |
wdenk | b4ad962 | 2005-04-01 00:25:43 +0000 | [diff] [blame] | 77 | * - own IP address |
| 78 | * We want: - network time |
| 79 | * Next step: none |
wdenk | 2d96695 | 2002-10-31 22:12:35 +0000 | [diff] [blame] | 80 | */ |
| 81 | |
| 82 | |
| 83 | #include <common.h> |
wdenk | 2d96695 | 2002-10-31 22:12:35 +0000 | [diff] [blame] | 84 | #include <command.h> |
Joe Hershberger | dc9d1e5 | 2012-12-11 22:16:26 -0600 | [diff] [blame] | 85 | #include <environment.h> |
wdenk | 2d96695 | 2002-10-31 22:12:35 +0000 | [diff] [blame] | 86 | #include <net.h> |
Joe Hershberger | fee076e | 2012-05-23 07:58:15 +0000 | [diff] [blame] | 87 | #if defined(CONFIG_STATUS_LED) |
wdenk | 49c3f67 | 2003-10-08 22:33:00 +0000 | [diff] [blame] | 88 | #include <miiphy.h> |
Joe Hershberger | fee076e | 2012-05-23 07:58:15 +0000 | [diff] [blame] | 89 | #include <status_led.h> |
wdenk | b4ad962 | 2005-04-01 00:25:43 +0000 | [diff] [blame] | 90 | #endif |
Joe Hershberger | fee076e | 2012-05-23 07:58:15 +0000 | [diff] [blame] | 91 | #include <watchdog.h> |
| 92 | #include <linux/compiler.h> |
| 93 | #include "arp.h" |
| 94 | #include "bootp.h" |
Joe Hershberger | a4215b0 | 2012-05-23 07:57:59 +0000 | [diff] [blame] | 95 | #include "cdp.h" |
Robin Getz | 82f0d23 | 2009-07-20 14:53:54 -0400 | [diff] [blame] | 96 | #if defined(CONFIG_CMD_DNS) |
| 97 | #include "dns.h" |
| 98 | #endif |
Joe Hershberger | b35a3a6 | 2012-05-23 08:00:12 +0000 | [diff] [blame] | 99 | #include "link_local.h" |
Joe Hershberger | fee076e | 2012-05-23 07:58:15 +0000 | [diff] [blame] | 100 | #include "nfs.h" |
Joe Hershberger | c21bf37 | 2012-05-23 07:58:02 +0000 | [diff] [blame] | 101 | #include "ping.h" |
Joe Hershberger | fee076e | 2012-05-23 07:58:15 +0000 | [diff] [blame] | 102 | #include "rarp.h" |
| 103 | #if defined(CONFIG_CMD_SNTP) |
| 104 | #include "sntp.h" |
| 105 | #endif |
| 106 | #include "tftp.h" |
wdenk | 2d96695 | 2002-10-31 22:12:35 +0000 | [diff] [blame] | 107 | |
Wolfgang Denk | 6405a15 | 2006-03-31 18:32:53 +0200 | [diff] [blame] | 108 | DECLARE_GLOBAL_DATA_PTR; |
| 109 | |
wdenk | 2d96695 | 2002-10-31 22:12:35 +0000 | [diff] [blame] | 110 | /** BOOTP EXTENTIONS **/ |
| 111 | |
Luca Ceresoli | 5c83a96 | 2011-05-11 03:59:54 +0000 | [diff] [blame] | 112 | /* Our subnet mask (0=unknown) */ |
Luca Ceresoli | d01bc1c | 2011-05-11 03:59:55 +0000 | [diff] [blame] | 113 | IPaddr_t NetOurSubnetMask; |
Luca Ceresoli | 5c83a96 | 2011-05-11 03:59:54 +0000 | [diff] [blame] | 114 | /* Our gateways IP address */ |
Luca Ceresoli | d01bc1c | 2011-05-11 03:59:55 +0000 | [diff] [blame] | 115 | IPaddr_t NetOurGatewayIP; |
Luca Ceresoli | 5c83a96 | 2011-05-11 03:59:54 +0000 | [diff] [blame] | 116 | /* Our DNS IP address */ |
Luca Ceresoli | d01bc1c | 2011-05-11 03:59:55 +0000 | [diff] [blame] | 117 | IPaddr_t NetOurDNSIP; |
Jon Loeliger | 5336a76 | 2007-07-09 22:08:34 -0500 | [diff] [blame] | 118 | #if defined(CONFIG_BOOTP_DNS2) |
Luca Ceresoli | 5c83a96 | 2011-05-11 03:59:54 +0000 | [diff] [blame] | 119 | /* Our 2nd DNS IP address */ |
Luca Ceresoli | d01bc1c | 2011-05-11 03:59:55 +0000 | [diff] [blame] | 120 | IPaddr_t NetOurDNS2IP; |
stroese | e0aadfb | 2003-08-28 14:17:32 +0000 | [diff] [blame] | 121 | #endif |
Luca Ceresoli | 5c83a96 | 2011-05-11 03:59:54 +0000 | [diff] [blame] | 122 | /* Our NIS domain */ |
Luca Ceresoli | d01bc1c | 2011-05-11 03:59:55 +0000 | [diff] [blame] | 123 | char NetOurNISDomain[32] = {0,}; |
Luca Ceresoli | 5c83a96 | 2011-05-11 03:59:54 +0000 | [diff] [blame] | 124 | /* Our hostname */ |
Luca Ceresoli | d01bc1c | 2011-05-11 03:59:55 +0000 | [diff] [blame] | 125 | char NetOurHostName[32] = {0,}; |
Luca Ceresoli | 5c83a96 | 2011-05-11 03:59:54 +0000 | [diff] [blame] | 126 | /* Our bootpath */ |
Luca Ceresoli | d01bc1c | 2011-05-11 03:59:55 +0000 | [diff] [blame] | 127 | char NetOurRootPath[64] = {0,}; |
Luca Ceresoli | 5c83a96 | 2011-05-11 03:59:54 +0000 | [diff] [blame] | 128 | /* Our bootfile size in blocks */ |
Luca Ceresoli | d01bc1c | 2011-05-11 03:59:55 +0000 | [diff] [blame] | 129 | ushort NetBootFileSize; |
wdenk | 2d96695 | 2002-10-31 22:12:35 +0000 | [diff] [blame] | 130 | |
David Updegraff | 7280da7 | 2007-06-11 10:41:07 -0500 | [diff] [blame] | 131 | #ifdef CONFIG_MCAST_TFTP /* Multicast TFTP */ |
| 132 | IPaddr_t Mcast_addr; |
| 133 | #endif |
| 134 | |
wdenk | 2d96695 | 2002-10-31 22:12:35 +0000 | [diff] [blame] | 135 | /** END OF BOOTP EXTENTIONS **/ |
| 136 | |
Luca Ceresoli | 5c83a96 | 2011-05-11 03:59:54 +0000 | [diff] [blame] | 137 | /* The actual transferred size of the bootfile (in bytes) */ |
| 138 | ulong NetBootFileXferSize; |
| 139 | /* Our ethernet address */ |
| 140 | uchar NetOurEther[6]; |
| 141 | /* Boot server enet address */ |
Luca Ceresoli | d01bc1c | 2011-05-11 03:59:55 +0000 | [diff] [blame] | 142 | uchar NetServerEther[6]; |
Luca Ceresoli | 5c83a96 | 2011-05-11 03:59:54 +0000 | [diff] [blame] | 143 | /* Our IP addr (0 = unknown) */ |
| 144 | IPaddr_t NetOurIP; |
| 145 | /* Server IP addr (0 = unknown) */ |
| 146 | IPaddr_t NetServerIP; |
| 147 | /* Current receive packet */ |
Joe Hershberger | 4b7747e | 2012-05-15 08:59:04 +0000 | [diff] [blame] | 148 | uchar *NetRxPacket; |
Luca Ceresoli | 5c83a96 | 2011-05-11 03:59:54 +0000 | [diff] [blame] | 149 | /* Current rx packet length */ |
| 150 | int NetRxPacketLen; |
| 151 | /* IP packet ID */ |
| 152 | unsigned NetIPID; |
| 153 | /* Ethernet bcast address */ |
Luca Ceresoli | d01bc1c | 2011-05-11 03:59:55 +0000 | [diff] [blame] | 154 | uchar NetBcastAddr[6] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff }; |
| 155 | uchar NetEtherNullAddr[6]; |
Rafal Jaworowski | 1f2c9a4 | 2007-12-27 18:19:02 +0100 | [diff] [blame] | 156 | #ifdef CONFIG_API |
Joe Hershberger | 4b7747e | 2012-05-15 08:59:04 +0000 | [diff] [blame] | 157 | void (*push_packet)(void *, int len) = 0; |
Rafal Jaworowski | 1f2c9a4 | 2007-12-27 18:19:02 +0100 | [diff] [blame] | 158 | #endif |
Luca Ceresoli | 5c83a96 | 2011-05-11 03:59:54 +0000 | [diff] [blame] | 159 | /* Network loop state */ |
Joe Hershberger | d4bb76a | 2012-05-23 07:59:14 +0000 | [diff] [blame] | 160 | enum net_loop_state net_state; |
Luca Ceresoli | 5c83a96 | 2011-05-11 03:59:54 +0000 | [diff] [blame] | 161 | /* Tried all network devices */ |
Luca Ceresoli | d01bc1c | 2011-05-11 03:59:55 +0000 | [diff] [blame] | 162 | int NetRestartWrap; |
Luca Ceresoli | 5c83a96 | 2011-05-11 03:59:54 +0000 | [diff] [blame] | 163 | /* Network loop restarted */ |
Luca Ceresoli | d01bc1c | 2011-05-11 03:59:55 +0000 | [diff] [blame] | 164 | static int NetRestarted; |
Luca Ceresoli | 5c83a96 | 2011-05-11 03:59:54 +0000 | [diff] [blame] | 165 | /* At least one device configured */ |
Luca Ceresoli | d01bc1c | 2011-05-11 03:59:55 +0000 | [diff] [blame] | 166 | static int NetDevExists; |
wdenk | 2d96695 | 2002-10-31 22:12:35 +0000 | [diff] [blame] | 167 | |
wdenk | 0593920 | 2004-04-18 17:39:38 +0000 | [diff] [blame] | 168 | /* XXX in both little & big endian machines 0xFFFF == ntohs(-1) */ |
Luca Ceresoli | 5c83a96 | 2011-05-11 03:59:54 +0000 | [diff] [blame] | 169 | /* default is without VLAN */ |
| 170 | ushort NetOurVLAN = 0xFFFF; |
| 171 | /* ditto */ |
| 172 | ushort NetOurNativeVLAN = 0xFFFF; |
wdenk | 145d2c1 | 2004-04-15 21:48:45 +0000 | [diff] [blame] | 173 | |
Luca Ceresoli | 5c83a96 | 2011-05-11 03:59:54 +0000 | [diff] [blame] | 174 | /* Boot File name */ |
| 175 | char BootFile[128]; |
wdenk | 2d96695 | 2002-10-31 22:12:35 +0000 | [diff] [blame] | 176 | |
Jon Loeliger | 54f35c2 | 2007-07-09 17:45:14 -0500 | [diff] [blame] | 177 | #if defined(CONFIG_CMD_SNTP) |
Luca Ceresoli | 5c83a96 | 2011-05-11 03:59:54 +0000 | [diff] [blame] | 178 | /* NTP server IP address */ |
| 179 | IPaddr_t NetNtpServerIP; |
| 180 | /* offset time from UTC */ |
Luca Ceresoli | d01bc1c | 2011-05-11 03:59:55 +0000 | [diff] [blame] | 181 | int NetTimeOffset; |
wdenk | b4ad962 | 2005-04-01 00:25:43 +0000 | [diff] [blame] | 182 | #endif |
| 183 | |
Kim Phillips | 40c2c03 | 2012-10-29 13:34:33 +0000 | [diff] [blame] | 184 | static uchar PktBuf[(PKTBUFSRX+1) * PKTSIZE_ALIGN + PKTALIGN]; |
wdenk | 2d96695 | 2002-10-31 22:12:35 +0000 | [diff] [blame] | 185 | |
Luca Ceresoli | 5c83a96 | 2011-05-11 03:59:54 +0000 | [diff] [blame] | 186 | /* Receive packet */ |
Joe Hershberger | 4b7747e | 2012-05-15 08:59:04 +0000 | [diff] [blame] | 187 | uchar *NetRxPackets[PKTBUFSRX]; |
wdenk | 2d96695 | 2002-10-31 22:12:35 +0000 | [diff] [blame] | 188 | |
Joe Hershberger | f50357b | 2012-05-23 07:59:15 +0000 | [diff] [blame] | 189 | /* Current UDP RX packet handler */ |
| 190 | static rxhand_f *udp_packet_handler; |
| 191 | /* Current ARP RX packet handler */ |
| 192 | static rxhand_f *arp_packet_handler; |
Simon Glass | 2928cd8 | 2011-10-26 14:18:38 +0000 | [diff] [blame] | 193 | #ifdef CONFIG_CMD_TFTPPUT |
Joe Hershberger | f50357b | 2012-05-23 07:59:15 +0000 | [diff] [blame] | 194 | /* Current ICMP rx handler */ |
| 195 | static rxhand_icmp_f *packet_icmp_handler; |
Simon Glass | 2928cd8 | 2011-10-26 14:18:38 +0000 | [diff] [blame] | 196 | #endif |
Luca Ceresoli | 5c83a96 | 2011-05-11 03:59:54 +0000 | [diff] [blame] | 197 | /* Current timeout handler */ |
| 198 | static thand_f *timeHandler; |
| 199 | /* Time base value */ |
| 200 | static ulong timeStart; |
| 201 | /* Current timeout value */ |
| 202 | static ulong timeDelta; |
| 203 | /* THE transmit packet */ |
Joe Hershberger | 4b7747e | 2012-05-15 08:59:04 +0000 | [diff] [blame] | 204 | uchar *NetTxPacket; |
wdenk | 2d96695 | 2002-10-31 22:12:35 +0000 | [diff] [blame] | 205 | |
Simon Glass | d6c5f55 | 2011-10-24 18:00:02 +0000 | [diff] [blame] | 206 | static int net_check_prereq(enum proto_t protocol); |
wdenk | 2d96695 | 2002-10-31 22:12:35 +0000 | [diff] [blame] | 207 | |
Remy Bohmer | 0ddb8e8 | 2009-10-28 22:13:39 +0100 | [diff] [blame] | 208 | static int NetTryCount; |
| 209 | |
wdenk | 2d96695 | 2002-10-31 22:12:35 +0000 | [diff] [blame] | 210 | /**********************************************************************/ |
wdenk | e6466f6 | 2003-06-05 19:27:42 +0000 | [diff] [blame] | 211 | |
Joe Hershberger | dc9d1e5 | 2012-12-11 22:16:26 -0600 | [diff] [blame] | 212 | static int on_bootfile(const char *name, const char *value, enum env_op op, |
| 213 | int flags) |
| 214 | { |
| 215 | switch (op) { |
| 216 | case env_op_create: |
| 217 | case env_op_overwrite: |
| 218 | copy_filename(BootFile, value, sizeof(BootFile)); |
| 219 | break; |
| 220 | default: |
| 221 | break; |
| 222 | } |
| 223 | |
| 224 | return 0; |
| 225 | } |
| 226 | U_BOOT_ENV_CALLBACK(bootfile, on_bootfile); |
| 227 | |
Simon Glass | 5234ad1 | 2011-10-27 06:24:32 +0000 | [diff] [blame] | 228 | /* |
| 229 | * Check if autoload is enabled. If so, use either NFS or TFTP to download |
| 230 | * the boot file. |
| 231 | */ |
| 232 | void net_auto_load(void) |
| 233 | { |
Joe Hershberger | 864ec56 | 2012-12-11 22:16:22 -0600 | [diff] [blame] | 234 | #if defined(CONFIG_CMD_NFS) |
Simon Glass | 5234ad1 | 2011-10-27 06:24:32 +0000 | [diff] [blame] | 235 | const char *s = getenv("autoload"); |
| 236 | |
Joe Hershberger | 864ec56 | 2012-12-11 22:16:22 -0600 | [diff] [blame] | 237 | if (s != NULL && strcmp(s, "NFS") == 0) { |
| 238 | /* |
| 239 | * Use NFS to load the bootfile. |
| 240 | */ |
| 241 | NfsStart(); |
| 242 | return; |
| 243 | } |
Simon Glass | 5234ad1 | 2011-10-27 06:24:32 +0000 | [diff] [blame] | 244 | #endif |
Joe Hershberger | 864ec56 | 2012-12-11 22:16:22 -0600 | [diff] [blame] | 245 | if (getenv_yesno("autoload") == 0) { |
| 246 | /* |
| 247 | * Just use BOOTP/RARP to configure system; |
| 248 | * Do not use TFTP to load the bootfile. |
| 249 | */ |
| 250 | net_set_state(NETLOOP_SUCCESS); |
| 251 | return; |
Simon Glass | 5234ad1 | 2011-10-27 06:24:32 +0000 | [diff] [blame] | 252 | } |
| 253 | TftpStart(TFTPGET); |
| 254 | } |
| 255 | |
Joe Hershberger | 202c600 | 2012-05-23 07:59:21 +0000 | [diff] [blame] | 256 | static void NetInitLoop(void) |
Heiko Schocher | 0c303fa | 2009-02-10 09:38:52 +0100 | [diff] [blame] | 257 | { |
Luca Ceresoli | d01bc1c | 2011-05-11 03:59:55 +0000 | [diff] [blame] | 258 | static int env_changed_id; |
Luca Ceresoli | 7cbd748 | 2011-05-11 03:59:56 +0000 | [diff] [blame] | 259 | int env_id = get_env_id(); |
Heiko Schocher | 0c303fa | 2009-02-10 09:38:52 +0100 | [diff] [blame] | 260 | |
| 261 | /* update only when the environment has changed */ |
Michael Zaidman | b97bfe4 | 2009-04-04 01:43:00 +0300 | [diff] [blame] | 262 | if (env_changed_id != env_id) { |
Enric Balletbo i Serra | 7019d25 | 2011-05-31 21:01:47 +0000 | [diff] [blame] | 263 | NetOurIP = getenv_IPaddr("ipaddr"); |
Luca Ceresoli | 7cbd748 | 2011-05-11 03:59:56 +0000 | [diff] [blame] | 264 | NetOurGatewayIP = getenv_IPaddr("gatewayip"); |
| 265 | NetOurSubnetMask = getenv_IPaddr("netmask"); |
| 266 | NetServerIP = getenv_IPaddr("serverip"); |
Heiko Schocher | 0c303fa | 2009-02-10 09:38:52 +0100 | [diff] [blame] | 267 | NetOurNativeVLAN = getenv_VLAN("nvlan"); |
Michael Zaidman | b97bfe4 | 2009-04-04 01:43:00 +0300 | [diff] [blame] | 268 | NetOurVLAN = getenv_VLAN("vlan"); |
Robin Getz | 82f0d23 | 2009-07-20 14:53:54 -0400 | [diff] [blame] | 269 | #if defined(CONFIG_CMD_DNS) |
| 270 | NetOurDNSIP = getenv_IPaddr("dnsip"); |
| 271 | #endif |
Michael Zaidman | b97bfe4 | 2009-04-04 01:43:00 +0300 | [diff] [blame] | 272 | env_changed_id = env_id; |
Heiko Schocher | 0c303fa | 2009-02-10 09:38:52 +0100 | [diff] [blame] | 273 | } |
Joe Hershberger | 3d905d2 | 2012-07-23 13:11:16 +0000 | [diff] [blame] | 274 | memcpy(NetOurEther, eth_get_dev()->enetaddr, 6); |
Michael Zaidman | b97bfe4 | 2009-04-04 01:43:00 +0300 | [diff] [blame] | 275 | |
Heiko Schocher | 3b195ff | 2009-04-28 08:36:11 +0200 | [diff] [blame] | 276 | return; |
Heiko Schocher | 0c303fa | 2009-02-10 09:38:52 +0100 | [diff] [blame] | 277 | } |
| 278 | |
Joe Hershberger | f50357b | 2012-05-23 07:59:15 +0000 | [diff] [blame] | 279 | static void net_clear_handlers(void) |
| 280 | { |
| 281 | net_set_udp_handler(NULL); |
| 282 | net_set_arp_handler(NULL); |
| 283 | NetSetTimeout(0, NULL); |
| 284 | } |
| 285 | |
| 286 | static void net_cleanup_loop(void) |
| 287 | { |
| 288 | net_clear_handlers(); |
| 289 | } |
| 290 | |
Joe Hershberger | 017e5c4 | 2012-05-23 07:59:22 +0000 | [diff] [blame] | 291 | void net_init(void) |
| 292 | { |
| 293 | static int first_call = 1; |
| 294 | |
| 295 | if (first_call) { |
| 296 | /* |
| 297 | * Setup packet buffers, aligned correctly. |
| 298 | */ |
| 299 | int i; |
| 300 | |
| 301 | NetTxPacket = &PktBuf[0] + (PKTALIGN - 1); |
| 302 | NetTxPacket -= (ulong)NetTxPacket % PKTALIGN; |
| 303 | for (i = 0; i < PKTBUFSRX; i++) |
| 304 | NetRxPackets[i] = NetTxPacket + (i + 1) * PKTSIZE_ALIGN; |
| 305 | |
| 306 | ArpInit(); |
| 307 | net_clear_handlers(); |
| 308 | |
| 309 | /* Only need to setup buffer pointers once. */ |
| 310 | first_call = 0; |
| 311 | } |
| 312 | |
| 313 | NetInitLoop(); |
| 314 | } |
| 315 | |
wdenk | e6466f6 | 2003-06-05 19:27:42 +0000 | [diff] [blame] | 316 | /**********************************************************************/ |
wdenk | 2d96695 | 2002-10-31 22:12:35 +0000 | [diff] [blame] | 317 | /* |
| 318 | * Main network processing loop. |
| 319 | */ |
| 320 | |
Simon Glass | d6c5f55 | 2011-10-24 18:00:02 +0000 | [diff] [blame] | 321 | int NetLoop(enum proto_t protocol) |
wdenk | 2d96695 | 2002-10-31 22:12:35 +0000 | [diff] [blame] | 322 | { |
wdenk | 2d96695 | 2002-10-31 22:12:35 +0000 | [diff] [blame] | 323 | bd_t *bd = gd->bd; |
Simon Glass | 230467c | 2011-10-24 18:00:01 +0000 | [diff] [blame] | 324 | int ret = -1; |
wdenk | 2d96695 | 2002-10-31 22:12:35 +0000 | [diff] [blame] | 325 | |
wdenk | 2d96695 | 2002-10-31 22:12:35 +0000 | [diff] [blame] | 326 | NetRestarted = 0; |
| 327 | NetDevExists = 0; |
Remy Bohmer | 0ddb8e8 | 2009-10-28 22:13:39 +0100 | [diff] [blame] | 328 | NetTryCount = 1; |
Joe Hershberger | 05a377b | 2012-05-23 08:01:04 +0000 | [diff] [blame] | 329 | debug_cond(DEBUG_INT_STATE, "--- NetLoop Entry\n"); |
wdenk | e6466f6 | 2003-06-05 19:27:42 +0000 | [diff] [blame] | 330 | |
Simon Glass | 768cbf0 | 2011-12-10 11:08:06 +0000 | [diff] [blame] | 331 | bootstage_mark_name(BOOTSTAGE_ID_ETH_START, "eth_start"); |
Joe Hershberger | 017e5c4 | 2012-05-23 07:59:22 +0000 | [diff] [blame] | 332 | net_init(); |
Joe Hershberger | 9f37406 | 2012-08-03 10:59:08 +0000 | [diff] [blame] | 333 | if (eth_is_on_demand_init() || protocol != NETCONS) { |
wdenk | fa66e93 | 2005-04-03 14:52:59 +0000 | [diff] [blame] | 334 | eth_halt(); |
Joe Hershberger | 9f37406 | 2012-08-03 10:59:08 +0000 | [diff] [blame] | 335 | eth_set_current(); |
| 336 | if (eth_init(bd) < 0) { |
| 337 | eth_halt(); |
| 338 | return -1; |
| 339 | } |
| 340 | } else |
| 341 | eth_init_state_only(bd); |
wdenk | 2d96695 | 2002-10-31 22:12:35 +0000 | [diff] [blame] | 342 | |
| 343 | restart: |
Joe Hershberger | d4bb76a | 2012-05-23 07:59:14 +0000 | [diff] [blame] | 344 | net_set_state(NETLOOP_CONTINUE); |
wdenk | 2d96695 | 2002-10-31 22:12:35 +0000 | [diff] [blame] | 345 | |
| 346 | /* |
| 347 | * Start the ball rolling with the given start function. From |
| 348 | * here on, this code is a state machine driven by received |
| 349 | * packets and timer events. |
| 350 | */ |
Joe Hershberger | 05a377b | 2012-05-23 08:01:04 +0000 | [diff] [blame] | 351 | debug_cond(DEBUG_INT_STATE, "--- NetLoop Init\n"); |
Joe Hershberger | 202c600 | 2012-05-23 07:59:21 +0000 | [diff] [blame] | 352 | NetInitLoop(); |
wdenk | 2d96695 | 2002-10-31 22:12:35 +0000 | [diff] [blame] | 353 | |
Luca Ceresoli | 7cbd748 | 2011-05-11 03:59:56 +0000 | [diff] [blame] | 354 | switch (net_check_prereq(protocol)) { |
wdenk | 2d96695 | 2002-10-31 22:12:35 +0000 | [diff] [blame] | 355 | case 1: |
| 356 | /* network not configured */ |
wdenk | fa66e93 | 2005-04-03 14:52:59 +0000 | [diff] [blame] | 357 | eth_halt(); |
Luca Ceresoli | 5fe6de2 | 2011-05-04 02:40:45 +0000 | [diff] [blame] | 358 | return -1; |
wdenk | 2d96695 | 2002-10-31 22:12:35 +0000 | [diff] [blame] | 359 | |
wdenk | 2d96695 | 2002-10-31 22:12:35 +0000 | [diff] [blame] | 360 | case 2: |
| 361 | /* network device not configured */ |
| 362 | break; |
wdenk | 2d96695 | 2002-10-31 22:12:35 +0000 | [diff] [blame] | 363 | |
| 364 | case 0: |
wdenk | 2d96695 | 2002-10-31 22:12:35 +0000 | [diff] [blame] | 365 | NetDevExists = 1; |
Simon Glass | d6c5f55 | 2011-10-24 18:00:02 +0000 | [diff] [blame] | 366 | NetBootFileXferSize = 0; |
wdenk | 2d96695 | 2002-10-31 22:12:35 +0000 | [diff] [blame] | 367 | switch (protocol) { |
Simon Glass | d6c5f55 | 2011-10-24 18:00:02 +0000 | [diff] [blame] | 368 | case TFTPGET: |
Simon Glass | 6a398d2 | 2011-10-24 18:00:07 +0000 | [diff] [blame] | 369 | #ifdef CONFIG_CMD_TFTPPUT |
| 370 | case TFTPPUT: |
| 371 | #endif |
wdenk | 2d96695 | 2002-10-31 22:12:35 +0000 | [diff] [blame] | 372 | /* always use ARP to get server ethernet address */ |
Simon Glass | d6c5f55 | 2011-10-24 18:00:02 +0000 | [diff] [blame] | 373 | TftpStart(protocol); |
wdenk | 2d96695 | 2002-10-31 22:12:35 +0000 | [diff] [blame] | 374 | break; |
Luca Ceresoli | 7aa81a4 | 2011-05-17 00:03:40 +0000 | [diff] [blame] | 375 | #ifdef CONFIG_CMD_TFTPSRV |
| 376 | case TFTPSRV: |
| 377 | TftpStartServer(); |
| 378 | break; |
| 379 | #endif |
Jon Loeliger | 54f35c2 | 2007-07-09 17:45:14 -0500 | [diff] [blame] | 380 | #if defined(CONFIG_CMD_DHCP) |
wdenk | 2d96695 | 2002-10-31 22:12:35 +0000 | [diff] [blame] | 381 | case DHCP: |
wdenk | c391953 | 2004-10-11 22:51:13 +0000 | [diff] [blame] | 382 | BootpTry = 0; |
Michael Zaidman | 16bb21d | 2009-07-14 23:37:12 +0300 | [diff] [blame] | 383 | NetOurIP = 0; |
wdenk | 2d96695 | 2002-10-31 22:12:35 +0000 | [diff] [blame] | 384 | DhcpRequest(); /* Basically same as BOOTP */ |
| 385 | break; |
Jon Loeliger | a9807e5 | 2007-07-10 11:05:02 -0500 | [diff] [blame] | 386 | #endif |
wdenk | 2d96695 | 2002-10-31 22:12:35 +0000 | [diff] [blame] | 387 | |
| 388 | case BOOTP: |
| 389 | BootpTry = 0; |
Michael Zaidman | 16bb21d | 2009-07-14 23:37:12 +0300 | [diff] [blame] | 390 | NetOurIP = 0; |
Luca Ceresoli | 7cbd748 | 2011-05-11 03:59:56 +0000 | [diff] [blame] | 391 | BootpRequest(); |
wdenk | 2d96695 | 2002-10-31 22:12:35 +0000 | [diff] [blame] | 392 | break; |
| 393 | |
Peter Tyser | f7a48ca | 2010-09-30 11:25:48 -0500 | [diff] [blame] | 394 | #if defined(CONFIG_CMD_RARP) |
wdenk | 2d96695 | 2002-10-31 22:12:35 +0000 | [diff] [blame] | 395 | case RARP: |
| 396 | RarpTry = 0; |
Michael Zaidman | 16bb21d | 2009-07-14 23:37:12 +0300 | [diff] [blame] | 397 | NetOurIP = 0; |
Luca Ceresoli | 7cbd748 | 2011-05-11 03:59:56 +0000 | [diff] [blame] | 398 | RarpRequest(); |
wdenk | 2d96695 | 2002-10-31 22:12:35 +0000 | [diff] [blame] | 399 | break; |
Peter Tyser | f7a48ca | 2010-09-30 11:25:48 -0500 | [diff] [blame] | 400 | #endif |
Jon Loeliger | 54f35c2 | 2007-07-09 17:45:14 -0500 | [diff] [blame] | 401 | #if defined(CONFIG_CMD_PING) |
wdenk | e6466f6 | 2003-06-05 19:27:42 +0000 | [diff] [blame] | 402 | case PING: |
Joe Hershberger | c21bf37 | 2012-05-23 07:58:02 +0000 | [diff] [blame] | 403 | ping_start(); |
wdenk | e6466f6 | 2003-06-05 19:27:42 +0000 | [diff] [blame] | 404 | break; |
| 405 | #endif |
Jon Loeliger | 54f35c2 | 2007-07-09 17:45:14 -0500 | [diff] [blame] | 406 | #if defined(CONFIG_CMD_NFS) |
wdenk | be9c1cb | 2004-02-24 02:00:03 +0000 | [diff] [blame] | 407 | case NFS: |
| 408 | NfsStart(); |
| 409 | break; |
| 410 | #endif |
Jon Loeliger | 54f35c2 | 2007-07-09 17:45:14 -0500 | [diff] [blame] | 411 | #if defined(CONFIG_CMD_CDP) |
wdenk | 145d2c1 | 2004-04-15 21:48:45 +0000 | [diff] [blame] | 412 | case CDP: |
| 413 | CDPStart(); |
| 414 | break; |
| 415 | #endif |
wdenk | b8fb619 | 2004-08-02 21:11:11 +0000 | [diff] [blame] | 416 | #ifdef CONFIG_NETCONSOLE |
| 417 | case NETCONS: |
| 418 | NcStart(); |
| 419 | break; |
| 420 | #endif |
Jon Loeliger | 54f35c2 | 2007-07-09 17:45:14 -0500 | [diff] [blame] | 421 | #if defined(CONFIG_CMD_SNTP) |
wdenk | b4ad962 | 2005-04-01 00:25:43 +0000 | [diff] [blame] | 422 | case SNTP: |
| 423 | SntpStart(); |
| 424 | break; |
| 425 | #endif |
Robin Getz | 82f0d23 | 2009-07-20 14:53:54 -0400 | [diff] [blame] | 426 | #if defined(CONFIG_CMD_DNS) |
| 427 | case DNS: |
| 428 | DnsStart(); |
| 429 | break; |
| 430 | #endif |
Joe Hershberger | b35a3a6 | 2012-05-23 08:00:12 +0000 | [diff] [blame] | 431 | #if defined(CONFIG_CMD_LINK_LOCAL) |
| 432 | case LINKLOCAL: |
| 433 | link_local_start(); |
| 434 | break; |
| 435 | #endif |
wdenk | 2d96695 | 2002-10-31 22:12:35 +0000 | [diff] [blame] | 436 | default: |
| 437 | break; |
| 438 | } |
| 439 | |
wdenk | 2d96695 | 2002-10-31 22:12:35 +0000 | [diff] [blame] | 440 | break; |
| 441 | } |
| 442 | |
Jon Loeliger | 54f35c2 | 2007-07-09 17:45:14 -0500 | [diff] [blame] | 443 | #if defined(CONFIG_MII) || defined(CONFIG_CMD_MII) |
Luca Ceresoli | 5c83a96 | 2011-05-11 03:59:54 +0000 | [diff] [blame] | 444 | #if defined(CONFIG_SYS_FAULT_ECHO_LINK_DOWN) && \ |
| 445 | defined(CONFIG_STATUS_LED) && \ |
| 446 | defined(STATUS_LED_RED) |
wdenk | 49c3f67 | 2003-10-08 22:33:00 +0000 | [diff] [blame] | 447 | /* |
wdenk | 9c53f40 | 2003-10-15 23:53:47 +0000 | [diff] [blame] | 448 | * Echo the inverted link state to the fault LED. |
wdenk | 49c3f67 | 2003-10-08 22:33:00 +0000 | [diff] [blame] | 449 | */ |
Luca Ceresoli | e8ccbb0 | 2011-05-04 02:40:43 +0000 | [diff] [blame] | 450 | if (miiphy_link(eth_get_dev()->name, CONFIG_SYS_FAULT_MII_ADDR)) |
Luca Ceresoli | 7cbd748 | 2011-05-11 03:59:56 +0000 | [diff] [blame] | 451 | status_led_set(STATUS_LED_RED, STATUS_LED_OFF); |
Luca Ceresoli | e8ccbb0 | 2011-05-04 02:40:43 +0000 | [diff] [blame] | 452 | else |
Luca Ceresoli | 7cbd748 | 2011-05-11 03:59:56 +0000 | [diff] [blame] | 453 | status_led_set(STATUS_LED_RED, STATUS_LED_ON); |
Jean-Christophe PLAGNIOL-VILLARD | 0383694 | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 454 | #endif /* CONFIG_SYS_FAULT_ECHO_LINK_DOWN, ... */ |
wdenk | 49c3f67 | 2003-10-08 22:33:00 +0000 | [diff] [blame] | 455 | #endif /* CONFIG_MII, ... */ |
wdenk | 2d96695 | 2002-10-31 22:12:35 +0000 | [diff] [blame] | 456 | |
| 457 | /* |
| 458 | * Main packet reception loop. Loop receiving packets until |
Joe Hershberger | d4bb76a | 2012-05-23 07:59:14 +0000 | [diff] [blame] | 459 | * someone sets `net_state' to a state that terminates. |
wdenk | 2d96695 | 2002-10-31 22:12:35 +0000 | [diff] [blame] | 460 | */ |
| 461 | for (;;) { |
| 462 | WATCHDOG_RESET(); |
| 463 | #ifdef CONFIG_SHOW_ACTIVITY |
Joe Hershberger | 77001b43 | 2012-05-15 08:59:08 +0000 | [diff] [blame] | 464 | show_activity(1); |
wdenk | 2d96695 | 2002-10-31 22:12:35 +0000 | [diff] [blame] | 465 | #endif |
| 466 | /* |
| 467 | * Check the ethernet for a new packet. The ethernet |
| 468 | * receive routine will process it. |
| 469 | */ |
Guennadi Liakhovetski | b38c2b3 | 2008-04-03 17:04:19 +0200 | [diff] [blame] | 470 | eth_rx(); |
wdenk | 2d96695 | 2002-10-31 22:12:35 +0000 | [diff] [blame] | 471 | |
| 472 | /* |
| 473 | * Abort if ctrl-c was pressed. |
| 474 | */ |
| 475 | if (ctrlc()) { |
Joe Hershberger | de8205a | 2012-05-23 07:59:24 +0000 | [diff] [blame] | 476 | /* cancel any ARP that may not have completed */ |
| 477 | NetArpWaitPacketIP = 0; |
| 478 | |
Joe Hershberger | f50357b | 2012-05-23 07:59:15 +0000 | [diff] [blame] | 479 | net_cleanup_loop(); |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 480 | eth_halt(); |
Joe Hershberger | 9f37406 | 2012-08-03 10:59:08 +0000 | [diff] [blame] | 481 | /* Invalidate the last protocol */ |
| 482 | eth_set_last_protocol(BOOTP); |
| 483 | |
Luca Ceresoli | 7cbd748 | 2011-05-11 03:59:56 +0000 | [diff] [blame] | 484 | puts("\nAbort\n"); |
Joe Hershberger | 05a377b | 2012-05-23 08:01:04 +0000 | [diff] [blame] | 485 | /* include a debug print as well incase the debug |
| 486 | messages are directed to stderr */ |
| 487 | debug_cond(DEBUG_INT_STATE, "--- NetLoop Abort!\n"); |
Simon Glass | 230467c | 2011-10-24 18:00:01 +0000 | [diff] [blame] | 488 | goto done; |
wdenk | 2d96695 | 2002-10-31 22:12:35 +0000 | [diff] [blame] | 489 | } |
| 490 | |
wdenk | e6466f6 | 2003-06-05 19:27:42 +0000 | [diff] [blame] | 491 | ArpTimeoutCheck(); |
wdenk | 2d96695 | 2002-10-31 22:12:35 +0000 | [diff] [blame] | 492 | |
| 493 | /* |
| 494 | * Check for a timeout, and run the timeout handler |
| 495 | * if we have one. |
| 496 | */ |
wdenk | 5d84173 | 2003-08-17 18:55:18 +0000 | [diff] [blame] | 497 | if (timeHandler && ((get_timer(0) - timeStart) > timeDelta)) { |
wdenk | 2d96695 | 2002-10-31 22:12:35 +0000 | [diff] [blame] | 498 | thand_f *x; |
| 499 | |
Jon Loeliger | 54f35c2 | 2007-07-09 17:45:14 -0500 | [diff] [blame] | 500 | #if defined(CONFIG_MII) || defined(CONFIG_CMD_MII) |
Luca Ceresoli | 7cbd748 | 2011-05-11 03:59:56 +0000 | [diff] [blame] | 501 | #if defined(CONFIG_SYS_FAULT_ECHO_LINK_DOWN) && \ |
| 502 | defined(CONFIG_STATUS_LED) && \ |
| 503 | defined(STATUS_LED_RED) |
wdenk | 49c3f67 | 2003-10-08 22:33:00 +0000 | [diff] [blame] | 504 | /* |
wdenk | 9c53f40 | 2003-10-15 23:53:47 +0000 | [diff] [blame] | 505 | * Echo the inverted link state to the fault LED. |
wdenk | 49c3f67 | 2003-10-08 22:33:00 +0000 | [diff] [blame] | 506 | */ |
Luca Ceresoli | 7cbd748 | 2011-05-11 03:59:56 +0000 | [diff] [blame] | 507 | if (miiphy_link(eth_get_dev()->name, |
Luca Ceresoli | 5c83a96 | 2011-05-11 03:59:54 +0000 | [diff] [blame] | 508 | CONFIG_SYS_FAULT_MII_ADDR)) { |
Luca Ceresoli | 7cbd748 | 2011-05-11 03:59:56 +0000 | [diff] [blame] | 509 | status_led_set(STATUS_LED_RED, STATUS_LED_OFF); |
wdenk | 49c3f67 | 2003-10-08 22:33:00 +0000 | [diff] [blame] | 510 | } else { |
Luca Ceresoli | 7cbd748 | 2011-05-11 03:59:56 +0000 | [diff] [blame] | 511 | status_led_set(STATUS_LED_RED, STATUS_LED_ON); |
wdenk | 49c3f67 | 2003-10-08 22:33:00 +0000 | [diff] [blame] | 512 | } |
Luca Ceresoli | 7cbd748 | 2011-05-11 03:59:56 +0000 | [diff] [blame] | 513 | #endif /* CONFIG_SYS_FAULT_ECHO_LINK_DOWN, ... */ |
wdenk | 49c3f67 | 2003-10-08 22:33:00 +0000 | [diff] [blame] | 514 | #endif /* CONFIG_MII, ... */ |
Joe Hershberger | 05a377b | 2012-05-23 08:01:04 +0000 | [diff] [blame] | 515 | debug_cond(DEBUG_INT_STATE, "--- NetLoop timeout\n"); |
wdenk | 2d96695 | 2002-10-31 22:12:35 +0000 | [diff] [blame] | 516 | x = timeHandler; |
| 517 | timeHandler = (thand_f *)0; |
| 518 | (*x)(); |
| 519 | } |
| 520 | |
| 521 | |
Joe Hershberger | d4bb76a | 2012-05-23 07:59:14 +0000 | [diff] [blame] | 522 | switch (net_state) { |
wdenk | 2d96695 | 2002-10-31 22:12:35 +0000 | [diff] [blame] | 523 | |
| 524 | case NETLOOP_RESTART: |
wdenk | 2d96695 | 2002-10-31 22:12:35 +0000 | [diff] [blame] | 525 | NetRestarted = 1; |
wdenk | 2d96695 | 2002-10-31 22:12:35 +0000 | [diff] [blame] | 526 | goto restart; |
| 527 | |
| 528 | case NETLOOP_SUCCESS: |
Joe Hershberger | f50357b | 2012-05-23 07:59:15 +0000 | [diff] [blame] | 529 | net_cleanup_loop(); |
wdenk | 2d96695 | 2002-10-31 22:12:35 +0000 | [diff] [blame] | 530 | if (NetBootFileXferSize > 0) { |
Wolfgang Denk | 084d0ce | 2007-09-12 00:48:57 +0200 | [diff] [blame] | 531 | char buf[20]; |
wdenk | 2d96695 | 2002-10-31 22:12:35 +0000 | [diff] [blame] | 532 | printf("Bytes transferred = %ld (%lx hex)\n", |
| 533 | NetBootFileXferSize, |
| 534 | NetBootFileXferSize); |
Wolfgang Denk | 084d0ce | 2007-09-12 00:48:57 +0200 | [diff] [blame] | 535 | sprintf(buf, "%lX", NetBootFileXferSize); |
wdenk | 2d96695 | 2002-10-31 22:12:35 +0000 | [diff] [blame] | 536 | setenv("filesize", buf); |
wdenk | 145d2c1 | 2004-04-15 21:48:45 +0000 | [diff] [blame] | 537 | |
| 538 | sprintf(buf, "%lX", (unsigned long)load_addr); |
| 539 | setenv("fileaddr", buf); |
wdenk | 2d96695 | 2002-10-31 22:12:35 +0000 | [diff] [blame] | 540 | } |
Joe Hershberger | 9f37406 | 2012-08-03 10:59:08 +0000 | [diff] [blame] | 541 | if (protocol != NETCONS) |
| 542 | eth_halt(); |
| 543 | else |
| 544 | eth_halt_state_only(); |
| 545 | |
| 546 | eth_set_last_protocol(protocol); |
| 547 | |
Simon Glass | 230467c | 2011-10-24 18:00:01 +0000 | [diff] [blame] | 548 | ret = NetBootFileXferSize; |
Joe Hershberger | 05a377b | 2012-05-23 08:01:04 +0000 | [diff] [blame] | 549 | debug_cond(DEBUG_INT_STATE, "--- NetLoop Success!\n"); |
Simon Glass | 230467c | 2011-10-24 18:00:01 +0000 | [diff] [blame] | 550 | goto done; |
wdenk | 2d96695 | 2002-10-31 22:12:35 +0000 | [diff] [blame] | 551 | |
| 552 | case NETLOOP_FAIL: |
Joe Hershberger | f50357b | 2012-05-23 07:59:15 +0000 | [diff] [blame] | 553 | net_cleanup_loop(); |
Joe Hershberger | 9f37406 | 2012-08-03 10:59:08 +0000 | [diff] [blame] | 554 | /* Invalidate the last protocol */ |
| 555 | eth_set_last_protocol(BOOTP); |
Joe Hershberger | 05a377b | 2012-05-23 08:01:04 +0000 | [diff] [blame] | 556 | debug_cond(DEBUG_INT_STATE, "--- NetLoop Fail!\n"); |
Simon Glass | 230467c | 2011-10-24 18:00:01 +0000 | [diff] [blame] | 557 | goto done; |
Joe Hershberger | d4bb76a | 2012-05-23 07:59:14 +0000 | [diff] [blame] | 558 | |
| 559 | case NETLOOP_CONTINUE: |
| 560 | continue; |
wdenk | 2d96695 | 2002-10-31 22:12:35 +0000 | [diff] [blame] | 561 | } |
| 562 | } |
Simon Glass | 230467c | 2011-10-24 18:00:01 +0000 | [diff] [blame] | 563 | |
| 564 | done: |
Simon Glass | 2928cd8 | 2011-10-26 14:18:38 +0000 | [diff] [blame] | 565 | #ifdef CONFIG_CMD_TFTPPUT |
Simon Glass | 230467c | 2011-10-24 18:00:01 +0000 | [diff] [blame] | 566 | /* Clear out the handlers */ |
Joe Hershberger | f50357b | 2012-05-23 07:59:15 +0000 | [diff] [blame] | 567 | net_set_udp_handler(NULL); |
Simon Glass | 230467c | 2011-10-24 18:00:01 +0000 | [diff] [blame] | 568 | net_set_icmp_handler(NULL); |
Simon Glass | 2928cd8 | 2011-10-26 14:18:38 +0000 | [diff] [blame] | 569 | #endif |
Simon Glass | 230467c | 2011-10-24 18:00:01 +0000 | [diff] [blame] | 570 | return ret; |
wdenk | 2d96695 | 2002-10-31 22:12:35 +0000 | [diff] [blame] | 571 | } |
| 572 | |
| 573 | /**********************************************************************/ |
| 574 | |
| 575 | static void |
| 576 | startAgainTimeout(void) |
| 577 | { |
Joe Hershberger | d4bb76a | 2012-05-23 07:59:14 +0000 | [diff] [blame] | 578 | net_set_state(NETLOOP_RESTART); |
wdenk | 2d96695 | 2002-10-31 22:12:35 +0000 | [diff] [blame] | 579 | } |
| 580 | |
Luca Ceresoli | 7cbd748 | 2011-05-11 03:59:56 +0000 | [diff] [blame] | 581 | void NetStartAgain(void) |
wdenk | 2d96695 | 2002-10-31 22:12:35 +0000 | [diff] [blame] | 582 | { |
wdenk | 0593920 | 2004-04-18 17:39:38 +0000 | [diff] [blame] | 583 | char *nretry; |
Remy Bohmer | 0ddb8e8 | 2009-10-28 22:13:39 +0100 | [diff] [blame] | 584 | int retry_forever = 0; |
| 585 | unsigned long retrycnt = 0; |
wdenk | 145d2c1 | 2004-04-15 21:48:45 +0000 | [diff] [blame] | 586 | |
Remy Bohmer | 0ddb8e8 | 2009-10-28 22:13:39 +0100 | [diff] [blame] | 587 | nretry = getenv("netretry"); |
| 588 | if (nretry) { |
| 589 | if (!strcmp(nretry, "yes")) |
| 590 | retry_forever = 1; |
| 591 | else if (!strcmp(nretry, "no")) |
| 592 | retrycnt = 0; |
| 593 | else if (!strcmp(nretry, "once")) |
| 594 | retrycnt = 1; |
| 595 | else |
| 596 | retrycnt = simple_strtoul(nretry, NULL, 0); |
| 597 | } else |
| 598 | retry_forever = 1; |
| 599 | |
| 600 | if ((!retry_forever) && (NetTryCount >= retrycnt)) { |
| 601 | eth_halt(); |
Joe Hershberger | d4bb76a | 2012-05-23 07:59:14 +0000 | [diff] [blame] | 602 | net_set_state(NETLOOP_FAIL); |
wdenk | 145d2c1 | 2004-04-15 21:48:45 +0000 | [diff] [blame] | 603 | return; |
| 604 | } |
Remy Bohmer | 0ddb8e8 | 2009-10-28 22:13:39 +0100 | [diff] [blame] | 605 | |
| 606 | NetTryCount++; |
| 607 | |
Luca Ceresoli | 7cbd748 | 2011-05-11 03:59:56 +0000 | [diff] [blame] | 608 | eth_halt(); |
Matthias Fuchs | a02d62b | 2007-12-27 16:58:41 +0100 | [diff] [blame] | 609 | #if !defined(CONFIG_NET_DO_NOT_TRY_ANOTHER) |
Luca Ceresoli | 7cbd748 | 2011-05-11 03:59:56 +0000 | [diff] [blame] | 610 | eth_try_another(!NetRestarted); |
Matthias Fuchs | a02d62b | 2007-12-27 16:58:41 +0100 | [diff] [blame] | 611 | #endif |
Luca Ceresoli | 7cbd748 | 2011-05-11 03:59:56 +0000 | [diff] [blame] | 612 | eth_init(gd->bd); |
wdenk | 0593920 | 2004-04-18 17:39:38 +0000 | [diff] [blame] | 613 | if (NetRestartWrap) { |
wdenk | 2d96695 | 2002-10-31 22:12:35 +0000 | [diff] [blame] | 614 | NetRestartWrap = 0; |
Remy Bohmer | 0ddb8e8 | 2009-10-28 22:13:39 +0100 | [diff] [blame] | 615 | if (NetDevExists) { |
Luca Ceresoli | 7cbd748 | 2011-05-11 03:59:56 +0000 | [diff] [blame] | 616 | NetSetTimeout(10000UL, startAgainTimeout); |
Joe Hershberger | f50357b | 2012-05-23 07:59:15 +0000 | [diff] [blame] | 617 | net_set_udp_handler(NULL); |
wdenk | 0593920 | 2004-04-18 17:39:38 +0000 | [diff] [blame] | 618 | } else { |
Joe Hershberger | d4bb76a | 2012-05-23 07:59:14 +0000 | [diff] [blame] | 619 | net_set_state(NETLOOP_FAIL); |
wdenk | 2d96695 | 2002-10-31 22:12:35 +0000 | [diff] [blame] | 620 | } |
wdenk | 0593920 | 2004-04-18 17:39:38 +0000 | [diff] [blame] | 621 | } else { |
Joe Hershberger | d4bb76a | 2012-05-23 07:59:14 +0000 | [diff] [blame] | 622 | net_set_state(NETLOOP_RESTART); |
wdenk | 2d96695 | 2002-10-31 22:12:35 +0000 | [diff] [blame] | 623 | } |
wdenk | 2d96695 | 2002-10-31 22:12:35 +0000 | [diff] [blame] | 624 | } |
| 625 | |
| 626 | /**********************************************************************/ |
| 627 | /* |
| 628 | * Miscelaneous bits. |
| 629 | */ |
| 630 | |
Joe Hershberger | f50357b | 2012-05-23 07:59:15 +0000 | [diff] [blame] | 631 | static void dummy_handler(uchar *pkt, unsigned dport, |
| 632 | IPaddr_t sip, unsigned sport, |
| 633 | unsigned len) |
Joe Hershberger | aae0508 | 2012-05-23 07:58:01 +0000 | [diff] [blame] | 634 | { |
Joe Hershberger | aae0508 | 2012-05-23 07:58:01 +0000 | [diff] [blame] | 635 | } |
| 636 | |
Joe Hershberger | f50357b | 2012-05-23 07:59:15 +0000 | [diff] [blame] | 637 | rxhand_f *net_get_udp_handler(void) |
| 638 | { |
| 639 | return udp_packet_handler; |
| 640 | } |
Joe Hershberger | aae0508 | 2012-05-23 07:58:01 +0000 | [diff] [blame] | 641 | |
Joe Hershberger | f50357b | 2012-05-23 07:59:15 +0000 | [diff] [blame] | 642 | void net_set_udp_handler(rxhand_f *f) |
| 643 | { |
Joe Hershberger | 05a377b | 2012-05-23 08:01:04 +0000 | [diff] [blame] | 644 | debug_cond(DEBUG_INT_STATE, "--- NetLoop UDP handler set (%p)\n", f); |
Joe Hershberger | f50357b | 2012-05-23 07:59:15 +0000 | [diff] [blame] | 645 | if (f == NULL) |
| 646 | udp_packet_handler = dummy_handler; |
| 647 | else |
| 648 | udp_packet_handler = f; |
| 649 | } |
| 650 | |
| 651 | rxhand_f *net_get_arp_handler(void) |
wdenk | 2d96695 | 2002-10-31 22:12:35 +0000 | [diff] [blame] | 652 | { |
Joe Hershberger | f50357b | 2012-05-23 07:59:15 +0000 | [diff] [blame] | 653 | return arp_packet_handler; |
wdenk | 2d96695 | 2002-10-31 22:12:35 +0000 | [diff] [blame] | 654 | } |
| 655 | |
Joe Hershberger | f50357b | 2012-05-23 07:59:15 +0000 | [diff] [blame] | 656 | void net_set_arp_handler(rxhand_f *f) |
| 657 | { |
Joe Hershberger | 05a377b | 2012-05-23 08:01:04 +0000 | [diff] [blame] | 658 | debug_cond(DEBUG_INT_STATE, "--- NetLoop ARP handler set (%p)\n", f); |
Joe Hershberger | f50357b | 2012-05-23 07:59:15 +0000 | [diff] [blame] | 659 | if (f == NULL) |
| 660 | arp_packet_handler = dummy_handler; |
| 661 | else |
| 662 | arp_packet_handler = f; |
| 663 | } |
| 664 | |
Simon Glass | 2928cd8 | 2011-10-26 14:18:38 +0000 | [diff] [blame] | 665 | #ifdef CONFIG_CMD_TFTPPUT |
Simon Glass | 230467c | 2011-10-24 18:00:01 +0000 | [diff] [blame] | 666 | void net_set_icmp_handler(rxhand_icmp_f *f) |
| 667 | { |
| 668 | packet_icmp_handler = f; |
| 669 | } |
Simon Glass | 2928cd8 | 2011-10-26 14:18:38 +0000 | [diff] [blame] | 670 | #endif |
wdenk | 2d96695 | 2002-10-31 22:12:35 +0000 | [diff] [blame] | 671 | |
| 672 | void |
Luca Ceresoli | f27d91f | 2011-05-04 02:40:44 +0000 | [diff] [blame] | 673 | NetSetTimeout(ulong iv, thand_f *f) |
wdenk | 2d96695 | 2002-10-31 22:12:35 +0000 | [diff] [blame] | 674 | { |
| 675 | if (iv == 0) { |
Joe Hershberger | 05a377b | 2012-05-23 08:01:04 +0000 | [diff] [blame] | 676 | debug_cond(DEBUG_INT_STATE, |
| 677 | "--- NetLoop timeout handler cancelled\n"); |
wdenk | 2d96695 | 2002-10-31 22:12:35 +0000 | [diff] [blame] | 678 | timeHandler = (thand_f *)0; |
| 679 | } else { |
Joe Hershberger | 05a377b | 2012-05-23 08:01:04 +0000 | [diff] [blame] | 680 | debug_cond(DEBUG_INT_STATE, |
| 681 | "--- NetLoop timeout handler set (%p)\n", f); |
wdenk | 2d96695 | 2002-10-31 22:12:35 +0000 | [diff] [blame] | 682 | timeHandler = f; |
wdenk | 5d84173 | 2003-08-17 18:55:18 +0000 | [diff] [blame] | 683 | timeStart = get_timer(0); |
Tetsuyuki Kobayashi | bf17618 | 2012-06-25 02:37:27 +0000 | [diff] [blame] | 684 | timeDelta = iv * CONFIG_SYS_HZ / 1000; |
wdenk | 2d96695 | 2002-10-31 22:12:35 +0000 | [diff] [blame] | 685 | } |
| 686 | } |
| 687 | |
Joe Hershberger | 1a6b8d8 | 2012-05-23 07:58:10 +0000 | [diff] [blame] | 688 | int NetSendUDPPacket(uchar *ether, IPaddr_t dest, int dport, int sport, |
| 689 | int payload_len) |
wdenk | e6466f6 | 2003-06-05 19:27:42 +0000 | [diff] [blame] | 690 | { |
wdenk | 145d2c1 | 2004-04-15 21:48:45 +0000 | [diff] [blame] | 691 | uchar *pkt; |
Joe Hershberger | 16be9cb | 2012-05-23 07:59:08 +0000 | [diff] [blame] | 692 | int eth_hdr_size; |
| 693 | int pkt_hdr_size; |
wdenk | 145d2c1 | 2004-04-15 21:48:45 +0000 | [diff] [blame] | 694 | |
Joe Hershberger | 017e5c4 | 2012-05-23 07:59:22 +0000 | [diff] [blame] | 695 | /* make sure the NetTxPacket is initialized (NetInit() was called) */ |
| 696 | assert(NetTxPacket != NULL); |
| 697 | if (NetTxPacket == NULL) |
| 698 | return -1; |
| 699 | |
wdenk | e6466f6 | 2003-06-05 19:27:42 +0000 | [diff] [blame] | 700 | /* convert to new style broadcast */ |
| 701 | if (dest == 0) |
| 702 | dest = 0xFFFFFFFF; |
| 703 | |
| 704 | /* if broadcast, make the ether address a broadcast and don't do ARP */ |
| 705 | if (dest == 0xFFFFFFFF) |
| 706 | ether = NetBcastAddr; |
| 707 | |
Joe Hershberger | de8205a | 2012-05-23 07:59:24 +0000 | [diff] [blame] | 708 | pkt = (uchar *)NetTxPacket; |
Joe Hershberger | 16be9cb | 2012-05-23 07:59:08 +0000 | [diff] [blame] | 709 | |
| 710 | eth_hdr_size = NetSetEther(pkt, ether, PROT_IP); |
| 711 | pkt += eth_hdr_size; |
| 712 | net_set_udp_header(pkt, dest, dport, sport, payload_len); |
| 713 | pkt_hdr_size = eth_hdr_size + IP_UDP_HDR_SIZE; |
wdenk | e6466f6 | 2003-06-05 19:27:42 +0000 | [diff] [blame] | 714 | |
Joe Hershberger | de8205a | 2012-05-23 07:59:24 +0000 | [diff] [blame] | 715 | /* if MAC address was not discovered yet, do an ARP request */ |
| 716 | if (memcmp(ether, NetEtherNullAddr, 6) == 0) { |
Joe Hershberger | 05a377b | 2012-05-23 08:01:04 +0000 | [diff] [blame] | 717 | debug_cond(DEBUG_DEV_PKT, "sending ARP for %pI4\n", &dest); |
Robin Getz | 9e0a4d6 | 2009-07-23 03:01:03 -0400 | [diff] [blame] | 718 | |
Joe Hershberger | 16be9cb | 2012-05-23 07:59:08 +0000 | [diff] [blame] | 719 | /* save the ip and eth addr for the packet to send after arp */ |
wdenk | e6466f6 | 2003-06-05 19:27:42 +0000 | [diff] [blame] | 720 | NetArpWaitPacketIP = dest; |
| 721 | NetArpWaitPacketMAC = ether; |
wdenk | 145d2c1 | 2004-04-15 21:48:45 +0000 | [diff] [blame] | 722 | |
wdenk | e6466f6 | 2003-06-05 19:27:42 +0000 | [diff] [blame] | 723 | /* size of the waiting packet */ |
Joe Hershberger | 16be9cb | 2012-05-23 07:59:08 +0000 | [diff] [blame] | 724 | NetArpWaitTxPacketSize = pkt_hdr_size + payload_len; |
wdenk | e6466f6 | 2003-06-05 19:27:42 +0000 | [diff] [blame] | 725 | |
| 726 | /* and do the ARP request */ |
| 727 | NetArpWaitTry = 1; |
| 728 | NetArpWaitTimerStart = get_timer(0); |
| 729 | ArpRequest(); |
| 730 | return 1; /* waiting */ |
Joe Hershberger | 16be9cb | 2012-05-23 07:59:08 +0000 | [diff] [blame] | 731 | } else { |
Joe Hershberger | 05a377b | 2012-05-23 08:01:04 +0000 | [diff] [blame] | 732 | debug_cond(DEBUG_DEV_PKT, "sending UDP to %pI4/%pM\n", |
| 733 | &dest, ether); |
Joe Hershberger | ebe3ca8 | 2012-05-23 07:59:13 +0000 | [diff] [blame] | 734 | NetSendPacket(NetTxPacket, pkt_hdr_size + payload_len); |
Joe Hershberger | 16be9cb | 2012-05-23 07:59:08 +0000 | [diff] [blame] | 735 | return 0; /* transmitted */ |
wdenk | e6466f6 | 2003-06-05 19:27:42 +0000 | [diff] [blame] | 736 | } |
wdenk | e6466f6 | 2003-06-05 19:27:42 +0000 | [diff] [blame] | 737 | } |
wdenk | 145d2c1 | 2004-04-15 21:48:45 +0000 | [diff] [blame] | 738 | |
Alessandro Rubini | f119e3d | 2009-08-07 13:58:56 +0200 | [diff] [blame] | 739 | #ifdef CONFIG_IP_DEFRAG |
| 740 | /* |
| 741 | * This function collects fragments in a single packet, according |
| 742 | * to the algorithm in RFC815. It returns NULL or the pointer to |
| 743 | * a complete packet, in static storage |
| 744 | */ |
| 745 | #ifndef CONFIG_NET_MAXDEFRAG |
| 746 | #define CONFIG_NET_MAXDEFRAG 16384 |
| 747 | #endif |
| 748 | /* |
| 749 | * MAXDEFRAG, above, is chosen in the config file and is real data |
| 750 | * so we need to add the NFS overhead, which is more than TFTP. |
| 751 | * To use sizeof in the internal unnamed structures, we need a real |
| 752 | * instance (can't do "sizeof(struct rpc_t.u.reply))", unfortunately). |
| 753 | * The compiler doesn't complain nor allocates the actual structure |
| 754 | */ |
| 755 | static struct rpc_t rpc_specimen; |
| 756 | #define IP_PKTSIZE (CONFIG_NET_MAXDEFRAG + sizeof(rpc_specimen.u.reply)) |
| 757 | |
Joe Hershberger | c686fa1 | 2012-05-23 07:58:05 +0000 | [diff] [blame] | 758 | #define IP_MAXUDP (IP_PKTSIZE - IP_HDR_SIZE) |
Alessandro Rubini | f119e3d | 2009-08-07 13:58:56 +0200 | [diff] [blame] | 759 | |
| 760 | /* |
| 761 | * this is the packet being assembled, either data or frag control. |
| 762 | * Fragments go by 8 bytes, so this union must be 8 bytes long |
| 763 | */ |
| 764 | struct hole { |
| 765 | /* first_byte is address of this structure */ |
| 766 | u16 last_byte; /* last byte in this hole + 1 (begin of next hole) */ |
| 767 | u16 next_hole; /* index of next (in 8-b blocks), 0 == none */ |
| 768 | u16 prev_hole; /* index of prev, 0 == none */ |
| 769 | u16 unused; |
| 770 | }; |
| 771 | |
Joe Hershberger | 6fe8b45 | 2012-05-23 07:58:04 +0000 | [diff] [blame] | 772 | static struct ip_udp_hdr *__NetDefragment(struct ip_udp_hdr *ip, int *lenp) |
Alessandro Rubini | f119e3d | 2009-08-07 13:58:56 +0200 | [diff] [blame] | 773 | { |
Joe Hershberger | 77001b43 | 2012-05-15 08:59:08 +0000 | [diff] [blame] | 774 | static uchar pkt_buff[IP_PKTSIZE] __aligned(PKTALIGN); |
Alessandro Rubini | f119e3d | 2009-08-07 13:58:56 +0200 | [diff] [blame] | 775 | static u16 first_hole, total_len; |
| 776 | struct hole *payload, *thisfrag, *h, *newh; |
Joe Hershberger | 6fe8b45 | 2012-05-23 07:58:04 +0000 | [diff] [blame] | 777 | struct ip_udp_hdr *localip = (struct ip_udp_hdr *)pkt_buff; |
Alessandro Rubini | f119e3d | 2009-08-07 13:58:56 +0200 | [diff] [blame] | 778 | uchar *indata = (uchar *)ip; |
| 779 | int offset8, start, len, done = 0; |
| 780 | u16 ip_off = ntohs(ip->ip_off); |
| 781 | |
| 782 | /* payload starts after IP header, this fragment is in there */ |
Joe Hershberger | c686fa1 | 2012-05-23 07:58:05 +0000 | [diff] [blame] | 783 | payload = (struct hole *)(pkt_buff + IP_HDR_SIZE); |
Alessandro Rubini | f119e3d | 2009-08-07 13:58:56 +0200 | [diff] [blame] | 784 | offset8 = (ip_off & IP_OFFS); |
| 785 | thisfrag = payload + offset8; |
| 786 | start = offset8 * 8; |
Joe Hershberger | c686fa1 | 2012-05-23 07:58:05 +0000 | [diff] [blame] | 787 | len = ntohs(ip->ip_len) - IP_HDR_SIZE; |
Alessandro Rubini | f119e3d | 2009-08-07 13:58:56 +0200 | [diff] [blame] | 788 | |
| 789 | if (start + len > IP_MAXUDP) /* fragment extends too far */ |
| 790 | return NULL; |
| 791 | |
| 792 | if (!total_len || localip->ip_id != ip->ip_id) { |
| 793 | /* new (or different) packet, reset structs */ |
| 794 | total_len = 0xffff; |
| 795 | payload[0].last_byte = ~0; |
| 796 | payload[0].next_hole = 0; |
| 797 | payload[0].prev_hole = 0; |
| 798 | first_hole = 0; |
| 799 | /* any IP header will work, copy the first we received */ |
Joe Hershberger | c686fa1 | 2012-05-23 07:58:05 +0000 | [diff] [blame] | 800 | memcpy(localip, ip, IP_HDR_SIZE); |
Alessandro Rubini | f119e3d | 2009-08-07 13:58:56 +0200 | [diff] [blame] | 801 | } |
| 802 | |
| 803 | /* |
| 804 | * What follows is the reassembly algorithm. We use the payload |
| 805 | * array as a linked list of hole descriptors, as each hole starts |
| 806 | * at a multiple of 8 bytes. However, last byte can be whatever value, |
| 807 | * so it is represented as byte count, not as 8-byte blocks. |
| 808 | */ |
| 809 | |
| 810 | h = payload + first_hole; |
| 811 | while (h->last_byte < start) { |
| 812 | if (!h->next_hole) { |
| 813 | /* no hole that far away */ |
| 814 | return NULL; |
| 815 | } |
| 816 | h = payload + h->next_hole; |
| 817 | } |
| 818 | |
Fillod Stephane | e7ade8b | 2010-06-11 19:26:43 +0200 | [diff] [blame] | 819 | /* last fragment may be 1..7 bytes, the "+7" forces acceptance */ |
| 820 | if (offset8 + ((len + 7) / 8) <= h - payload) { |
Alessandro Rubini | f119e3d | 2009-08-07 13:58:56 +0200 | [diff] [blame] | 821 | /* no overlap with holes (dup fragment?) */ |
| 822 | return NULL; |
| 823 | } |
| 824 | |
| 825 | if (!(ip_off & IP_FLAGS_MFRAG)) { |
| 826 | /* no more fragmentss: truncate this (last) hole */ |
| 827 | total_len = start + len; |
| 828 | h->last_byte = start + len; |
| 829 | } |
| 830 | |
| 831 | /* |
| 832 | * There is some overlap: fix the hole list. This code doesn't |
| 833 | * deal with a fragment that overlaps with two different holes |
| 834 | * (thus being a superset of a previously-received fragment). |
| 835 | */ |
| 836 | |
Luca Ceresoli | 7cbd748 | 2011-05-11 03:59:56 +0000 | [diff] [blame] | 837 | if ((h >= thisfrag) && (h->last_byte <= start + len)) { |
Alessandro Rubini | f119e3d | 2009-08-07 13:58:56 +0200 | [diff] [blame] | 838 | /* complete overlap with hole: remove hole */ |
| 839 | if (!h->prev_hole && !h->next_hole) { |
| 840 | /* last remaining hole */ |
| 841 | done = 1; |
| 842 | } else if (!h->prev_hole) { |
| 843 | /* first hole */ |
| 844 | first_hole = h->next_hole; |
| 845 | payload[h->next_hole].prev_hole = 0; |
| 846 | } else if (!h->next_hole) { |
| 847 | /* last hole */ |
| 848 | payload[h->prev_hole].next_hole = 0; |
| 849 | } else { |
| 850 | /* in the middle of the list */ |
| 851 | payload[h->next_hole].prev_hole = h->prev_hole; |
| 852 | payload[h->prev_hole].next_hole = h->next_hole; |
| 853 | } |
| 854 | |
| 855 | } else if (h->last_byte <= start + len) { |
| 856 | /* overlaps with final part of the hole: shorten this hole */ |
| 857 | h->last_byte = start; |
| 858 | |
| 859 | } else if (h >= thisfrag) { |
| 860 | /* overlaps with initial part of the hole: move this hole */ |
| 861 | newh = thisfrag + (len / 8); |
| 862 | *newh = *h; |
| 863 | h = newh; |
| 864 | if (h->next_hole) |
| 865 | payload[h->next_hole].prev_hole = (h - payload); |
| 866 | if (h->prev_hole) |
| 867 | payload[h->prev_hole].next_hole = (h - payload); |
| 868 | else |
| 869 | first_hole = (h - payload); |
| 870 | |
| 871 | } else { |
| 872 | /* fragment sits in the middle: split the hole */ |
| 873 | newh = thisfrag + (len / 8); |
| 874 | *newh = *h; |
| 875 | h->last_byte = start; |
| 876 | h->next_hole = (newh - payload); |
| 877 | newh->prev_hole = (h - payload); |
| 878 | if (newh->next_hole) |
| 879 | payload[newh->next_hole].prev_hole = (newh - payload); |
| 880 | } |
| 881 | |
| 882 | /* finally copy this fragment and possibly return whole packet */ |
Joe Hershberger | c686fa1 | 2012-05-23 07:58:05 +0000 | [diff] [blame] | 883 | memcpy((uchar *)thisfrag, indata + IP_HDR_SIZE, len); |
Alessandro Rubini | f119e3d | 2009-08-07 13:58:56 +0200 | [diff] [blame] | 884 | if (!done) |
| 885 | return NULL; |
| 886 | |
| 887 | localip->ip_len = htons(total_len); |
Joe Hershberger | c686fa1 | 2012-05-23 07:58:05 +0000 | [diff] [blame] | 888 | *lenp = total_len + IP_HDR_SIZE; |
Alessandro Rubini | f119e3d | 2009-08-07 13:58:56 +0200 | [diff] [blame] | 889 | return localip; |
| 890 | } |
| 891 | |
Joe Hershberger | 6fe8b45 | 2012-05-23 07:58:04 +0000 | [diff] [blame] | 892 | static inline struct ip_udp_hdr *NetDefragment(struct ip_udp_hdr *ip, int *lenp) |
Alessandro Rubini | f119e3d | 2009-08-07 13:58:56 +0200 | [diff] [blame] | 893 | { |
| 894 | u16 ip_off = ntohs(ip->ip_off); |
| 895 | if (!(ip_off & (IP_OFFS | IP_FLAGS_MFRAG))) |
| 896 | return ip; /* not a fragment */ |
| 897 | return __NetDefragment(ip, lenp); |
| 898 | } |
| 899 | |
| 900 | #else /* !CONFIG_IP_DEFRAG */ |
| 901 | |
Joe Hershberger | 6fe8b45 | 2012-05-23 07:58:04 +0000 | [diff] [blame] | 902 | static inline struct ip_udp_hdr *NetDefragment(struct ip_udp_hdr *ip, int *lenp) |
Alessandro Rubini | f119e3d | 2009-08-07 13:58:56 +0200 | [diff] [blame] | 903 | { |
| 904 | u16 ip_off = ntohs(ip->ip_off); |
| 905 | if (!(ip_off & (IP_OFFS | IP_FLAGS_MFRAG))) |
| 906 | return ip; /* not a fragment */ |
| 907 | return NULL; |
| 908 | } |
| 909 | #endif |
wdenk | 2d96695 | 2002-10-31 22:12:35 +0000 | [diff] [blame] | 910 | |
Simon Glass | 43c7296 | 2011-10-24 18:00:00 +0000 | [diff] [blame] | 911 | /** |
| 912 | * Receive an ICMP packet. We deal with REDIRECT and PING here, and silently |
| 913 | * drop others. |
| 914 | * |
| 915 | * @parma ip IP packet containing the ICMP |
| 916 | */ |
Joe Hershberger | 6fe8b45 | 2012-05-23 07:58:04 +0000 | [diff] [blame] | 917 | static void receive_icmp(struct ip_udp_hdr *ip, int len, |
Joe Hershberger | 1178f41 | 2012-05-23 07:58:06 +0000 | [diff] [blame] | 918 | IPaddr_t src_ip, struct ethernet_hdr *et) |
Simon Glass | 43c7296 | 2011-10-24 18:00:00 +0000 | [diff] [blame] | 919 | { |
Joe Hershberger | 7849561 | 2012-05-23 07:58:09 +0000 | [diff] [blame] | 920 | struct icmp_hdr *icmph = (struct icmp_hdr *)&ip->udp_src; |
Simon Glass | 43c7296 | 2011-10-24 18:00:00 +0000 | [diff] [blame] | 921 | |
| 922 | switch (icmph->type) { |
| 923 | case ICMP_REDIRECT: |
| 924 | if (icmph->code != ICMP_REDIR_HOST) |
| 925 | return; |
| 926 | printf(" ICMP Host Redirect to %pI4 ", |
| 927 | &icmph->un.gateway); |
| 928 | break; |
Joe Hershberger | c21bf37 | 2012-05-23 07:58:02 +0000 | [diff] [blame] | 929 | default: |
Simon Glass | 43c7296 | 2011-10-24 18:00:00 +0000 | [diff] [blame] | 930 | #if defined(CONFIG_CMD_PING) |
Joe Hershberger | c21bf37 | 2012-05-23 07:58:02 +0000 | [diff] [blame] | 931 | ping_receive(et, ip, len); |
Simon Glass | 43c7296 | 2011-10-24 18:00:00 +0000 | [diff] [blame] | 932 | #endif |
Simon Glass | 2928cd8 | 2011-10-26 14:18:38 +0000 | [diff] [blame] | 933 | #ifdef CONFIG_CMD_TFTPPUT |
Simon Glass | 230467c | 2011-10-24 18:00:01 +0000 | [diff] [blame] | 934 | if (packet_icmp_handler) |
| 935 | packet_icmp_handler(icmph->type, icmph->code, |
| 936 | ntohs(ip->udp_dst), src_ip, ntohs(ip->udp_src), |
| 937 | icmph->un.data, ntohs(ip->udp_len)); |
Simon Glass | 2928cd8 | 2011-10-26 14:18:38 +0000 | [diff] [blame] | 938 | #endif |
Simon Glass | 43c7296 | 2011-10-24 18:00:00 +0000 | [diff] [blame] | 939 | break; |
| 940 | } |
| 941 | } |
| 942 | |
wdenk | 2d96695 | 2002-10-31 22:12:35 +0000 | [diff] [blame] | 943 | void |
Joe Hershberger | 4b7747e | 2012-05-15 08:59:04 +0000 | [diff] [blame] | 944 | NetReceive(uchar *inpkt, int len) |
wdenk | 2d96695 | 2002-10-31 22:12:35 +0000 | [diff] [blame] | 945 | { |
Joe Hershberger | 1178f41 | 2012-05-23 07:58:06 +0000 | [diff] [blame] | 946 | struct ethernet_hdr *et; |
Joe Hershberger | 6fe8b45 | 2012-05-23 07:58:04 +0000 | [diff] [blame] | 947 | struct ip_udp_hdr *ip; |
Joe Hershberger | facf535 | 2012-05-23 07:58:12 +0000 | [diff] [blame] | 948 | IPaddr_t dst_ip; |
Luca Ceresoli | 428ab36 | 2011-04-18 06:19:50 +0000 | [diff] [blame] | 949 | IPaddr_t src_ip; |
Joe Hershberger | facf535 | 2012-05-23 07:58:12 +0000 | [diff] [blame] | 950 | int eth_proto; |
Jon Loeliger | 54f35c2 | 2007-07-09 17:45:14 -0500 | [diff] [blame] | 951 | #if defined(CONFIG_CMD_CDP) |
wdenk | 145d2c1 | 2004-04-15 21:48:45 +0000 | [diff] [blame] | 952 | int iscdp; |
| 953 | #endif |
| 954 | ushort cti = 0, vlanid = VLAN_NONE, myvlanid, mynvlanid; |
wdenk | 2d96695 | 2002-10-31 22:12:35 +0000 | [diff] [blame] | 955 | |
Joe Hershberger | 05a377b | 2012-05-23 08:01:04 +0000 | [diff] [blame] | 956 | debug_cond(DEBUG_NET_PKT, "packet received\n"); |
wdenk | 145d2c1 | 2004-04-15 21:48:45 +0000 | [diff] [blame] | 957 | |
Mike Frysinger | dc16603 | 2009-07-18 21:04:08 -0400 | [diff] [blame] | 958 | NetRxPacket = inpkt; |
| 959 | NetRxPacketLen = len; |
Joe Hershberger | 1178f41 | 2012-05-23 07:58:06 +0000 | [diff] [blame] | 960 | et = (struct ethernet_hdr *)inpkt; |
wdenk | 145d2c1 | 2004-04-15 21:48:45 +0000 | [diff] [blame] | 961 | |
| 962 | /* too small packet? */ |
| 963 | if (len < ETHER_HDR_SIZE) |
| 964 | return; |
Rafal Jaworowski | 1f2c9a4 | 2007-12-27 18:19:02 +0100 | [diff] [blame] | 965 | |
| 966 | #ifdef CONFIG_API |
| 967 | if (push_packet) { |
| 968 | (*push_packet)(inpkt, len); |
| 969 | return; |
| 970 | } |
| 971 | #endif |
wdenk | 145d2c1 | 2004-04-15 21:48:45 +0000 | [diff] [blame] | 972 | |
Jon Loeliger | 54f35c2 | 2007-07-09 17:45:14 -0500 | [diff] [blame] | 973 | #if defined(CONFIG_CMD_CDP) |
wdenk | 145d2c1 | 2004-04-15 21:48:45 +0000 | [diff] [blame] | 974 | /* keep track if packet is CDP */ |
Joe Hershberger | 00c62a8 | 2012-05-23 07:58:00 +0000 | [diff] [blame] | 975 | iscdp = is_cdp_packet(et->et_dest); |
wdenk | 145d2c1 | 2004-04-15 21:48:45 +0000 | [diff] [blame] | 976 | #endif |
| 977 | |
| 978 | myvlanid = ntohs(NetOurVLAN); |
| 979 | if (myvlanid == (ushort)-1) |
| 980 | myvlanid = VLAN_NONE; |
| 981 | mynvlanid = ntohs(NetOurNativeVLAN); |
| 982 | if (mynvlanid == (ushort)-1) |
| 983 | mynvlanid = VLAN_NONE; |
wdenk | 2d96695 | 2002-10-31 22:12:35 +0000 | [diff] [blame] | 984 | |
Joe Hershberger | facf535 | 2012-05-23 07:58:12 +0000 | [diff] [blame] | 985 | eth_proto = ntohs(et->et_protlen); |
wdenk | 2d96695 | 2002-10-31 22:12:35 +0000 | [diff] [blame] | 986 | |
Joe Hershberger | facf535 | 2012-05-23 07:58:12 +0000 | [diff] [blame] | 987 | if (eth_proto < 1514) { |
Joe Hershberger | 1178f41 | 2012-05-23 07:58:06 +0000 | [diff] [blame] | 988 | struct e802_hdr *et802 = (struct e802_hdr *)et; |
wdenk | 2d96695 | 2002-10-31 22:12:35 +0000 | [diff] [blame] | 989 | /* |
Joe Hershberger | c17fa98 | 2012-05-23 07:58:11 +0000 | [diff] [blame] | 990 | * Got a 802.2 packet. Check the other protocol field. |
| 991 | * XXX VLAN over 802.2+SNAP not implemented! |
wdenk | 2d96695 | 2002-10-31 22:12:35 +0000 | [diff] [blame] | 992 | */ |
Joe Hershberger | facf535 | 2012-05-23 07:58:12 +0000 | [diff] [blame] | 993 | eth_proto = ntohs(et802->et_prot); |
wdenk | 145d2c1 | 2004-04-15 21:48:45 +0000 | [diff] [blame] | 994 | |
Joe Hershberger | 6fe8b45 | 2012-05-23 07:58:04 +0000 | [diff] [blame] | 995 | ip = (struct ip_udp_hdr *)(inpkt + E802_HDR_SIZE); |
wdenk | 2d96695 | 2002-10-31 22:12:35 +0000 | [diff] [blame] | 996 | len -= E802_HDR_SIZE; |
wdenk | 145d2c1 | 2004-04-15 21:48:45 +0000 | [diff] [blame] | 997 | |
Joe Hershberger | facf535 | 2012-05-23 07:58:12 +0000 | [diff] [blame] | 998 | } else if (eth_proto != PROT_VLAN) { /* normal packet */ |
Joe Hershberger | 6fe8b45 | 2012-05-23 07:58:04 +0000 | [diff] [blame] | 999 | ip = (struct ip_udp_hdr *)(inpkt + ETHER_HDR_SIZE); |
wdenk | 2d96695 | 2002-10-31 22:12:35 +0000 | [diff] [blame] | 1000 | len -= ETHER_HDR_SIZE; |
wdenk | 145d2c1 | 2004-04-15 21:48:45 +0000 | [diff] [blame] | 1001 | |
| 1002 | } else { /* VLAN packet */ |
Joe Hershberger | b43784c | 2012-05-23 07:58:07 +0000 | [diff] [blame] | 1003 | struct vlan_ethernet_hdr *vet = |
| 1004 | (struct vlan_ethernet_hdr *)et; |
wdenk | 145d2c1 | 2004-04-15 21:48:45 +0000 | [diff] [blame] | 1005 | |
Joe Hershberger | 05a377b | 2012-05-23 08:01:04 +0000 | [diff] [blame] | 1006 | debug_cond(DEBUG_NET_PKT, "VLAN packet received\n"); |
Robin Getz | 9e0a4d6 | 2009-07-23 03:01:03 -0400 | [diff] [blame] | 1007 | |
wdenk | 145d2c1 | 2004-04-15 21:48:45 +0000 | [diff] [blame] | 1008 | /* too small packet? */ |
| 1009 | if (len < VLAN_ETHER_HDR_SIZE) |
| 1010 | return; |
| 1011 | |
| 1012 | /* if no VLAN active */ |
| 1013 | if ((ntohs(NetOurVLAN) & VLAN_IDMASK) == VLAN_NONE |
Jon Loeliger | 54f35c2 | 2007-07-09 17:45:14 -0500 | [diff] [blame] | 1014 | #if defined(CONFIG_CMD_CDP) |
wdenk | 145d2c1 | 2004-04-15 21:48:45 +0000 | [diff] [blame] | 1015 | && iscdp == 0 |
| 1016 | #endif |
| 1017 | ) |
| 1018 | return; |
| 1019 | |
| 1020 | cti = ntohs(vet->vet_tag); |
| 1021 | vlanid = cti & VLAN_IDMASK; |
Joe Hershberger | facf535 | 2012-05-23 07:58:12 +0000 | [diff] [blame] | 1022 | eth_proto = ntohs(vet->vet_type); |
wdenk | 145d2c1 | 2004-04-15 21:48:45 +0000 | [diff] [blame] | 1023 | |
Joe Hershberger | 6fe8b45 | 2012-05-23 07:58:04 +0000 | [diff] [blame] | 1024 | ip = (struct ip_udp_hdr *)(inpkt + VLAN_ETHER_HDR_SIZE); |
wdenk | 145d2c1 | 2004-04-15 21:48:45 +0000 | [diff] [blame] | 1025 | len -= VLAN_ETHER_HDR_SIZE; |
wdenk | 2d96695 | 2002-10-31 22:12:35 +0000 | [diff] [blame] | 1026 | } |
| 1027 | |
Joe Hershberger | 05a377b | 2012-05-23 08:01:04 +0000 | [diff] [blame] | 1028 | debug_cond(DEBUG_NET_PKT, "Receive from protocol 0x%x\n", eth_proto); |
wdenk | 2d96695 | 2002-10-31 22:12:35 +0000 | [diff] [blame] | 1029 | |
Jon Loeliger | 54f35c2 | 2007-07-09 17:45:14 -0500 | [diff] [blame] | 1030 | #if defined(CONFIG_CMD_CDP) |
wdenk | 145d2c1 | 2004-04-15 21:48:45 +0000 | [diff] [blame] | 1031 | if (iscdp) { |
Joe Hershberger | d01a7a0 | 2012-05-23 07:58:13 +0000 | [diff] [blame] | 1032 | cdp_receive((uchar *)ip, len); |
wdenk | 145d2c1 | 2004-04-15 21:48:45 +0000 | [diff] [blame] | 1033 | return; |
| 1034 | } |
| 1035 | #endif |
| 1036 | |
| 1037 | if ((myvlanid & VLAN_IDMASK) != VLAN_NONE) { |
| 1038 | if (vlanid == VLAN_NONE) |
| 1039 | vlanid = (mynvlanid & VLAN_IDMASK); |
| 1040 | /* not matched? */ |
| 1041 | if (vlanid != (myvlanid & VLAN_IDMASK)) |
| 1042 | return; |
| 1043 | } |
| 1044 | |
Joe Hershberger | facf535 | 2012-05-23 07:58:12 +0000 | [diff] [blame] | 1045 | switch (eth_proto) { |
wdenk | 2d96695 | 2002-10-31 22:12:35 +0000 | [diff] [blame] | 1046 | |
| 1047 | case PROT_ARP: |
Joe Hershberger | aae0508 | 2012-05-23 07:58:01 +0000 | [diff] [blame] | 1048 | ArpReceive(et, ip, len); |
wdenk | cb99da5 | 2005-01-12 00:15:14 +0000 | [diff] [blame] | 1049 | break; |
wdenk | 2d96695 | 2002-10-31 22:12:35 +0000 | [diff] [blame] | 1050 | |
Peter Tyser | f7a48ca | 2010-09-30 11:25:48 -0500 | [diff] [blame] | 1051 | #ifdef CONFIG_CMD_RARP |
wdenk | 2d96695 | 2002-10-31 22:12:35 +0000 | [diff] [blame] | 1052 | case PROT_RARP: |
Joe Hershberger | 61b4de6 | 2012-05-23 07:58:03 +0000 | [diff] [blame] | 1053 | rarp_receive(ip, len); |
wdenk | 2d96695 | 2002-10-31 22:12:35 +0000 | [diff] [blame] | 1054 | break; |
Peter Tyser | f7a48ca | 2010-09-30 11:25:48 -0500 | [diff] [blame] | 1055 | #endif |
wdenk | 2d96695 | 2002-10-31 22:12:35 +0000 | [diff] [blame] | 1056 | case PROT_IP: |
Joe Hershberger | 05a377b | 2012-05-23 08:01:04 +0000 | [diff] [blame] | 1057 | debug_cond(DEBUG_NET_PKT, "Got IP\n"); |
Alessandro Rubini | f119e3d | 2009-08-07 13:58:56 +0200 | [diff] [blame] | 1058 | /* Before we start poking the header, make sure it is there */ |
Joe Hershberger | 6fe8b45 | 2012-05-23 07:58:04 +0000 | [diff] [blame] | 1059 | if (len < IP_UDP_HDR_SIZE) { |
| 1060 | debug("len bad %d < %lu\n", len, |
| 1061 | (ulong)IP_UDP_HDR_SIZE); |
wdenk | 2d96695 | 2002-10-31 22:12:35 +0000 | [diff] [blame] | 1062 | return; |
| 1063 | } |
Alessandro Rubini | f119e3d | 2009-08-07 13:58:56 +0200 | [diff] [blame] | 1064 | /* Check the packet length */ |
wdenk | 2d96695 | 2002-10-31 22:12:35 +0000 | [diff] [blame] | 1065 | if (len < ntohs(ip->ip_len)) { |
Joe Hershberger | 05a377b | 2012-05-23 08:01:04 +0000 | [diff] [blame] | 1066 | debug("len bad %d < %d\n", len, ntohs(ip->ip_len)); |
wdenk | 2d96695 | 2002-10-31 22:12:35 +0000 | [diff] [blame] | 1067 | return; |
| 1068 | } |
| 1069 | len = ntohs(ip->ip_len); |
Joe Hershberger | 05a377b | 2012-05-23 08:01:04 +0000 | [diff] [blame] | 1070 | debug_cond(DEBUG_NET_PKT, "len=%d, v=%02x\n", |
| 1071 | len, ip->ip_hl_v & 0xff); |
Robin Getz | 9e0a4d6 | 2009-07-23 03:01:03 -0400 | [diff] [blame] | 1072 | |
Alessandro Rubini | f119e3d | 2009-08-07 13:58:56 +0200 | [diff] [blame] | 1073 | /* Can't deal with anything except IPv4 */ |
Luca Ceresoli | e8ccbb0 | 2011-05-04 02:40:43 +0000 | [diff] [blame] | 1074 | if ((ip->ip_hl_v & 0xf0) != 0x40) |
wdenk | 2d96695 | 2002-10-31 22:12:35 +0000 | [diff] [blame] | 1075 | return; |
Alessandro Rubini | f119e3d | 2009-08-07 13:58:56 +0200 | [diff] [blame] | 1076 | /* Can't deal with IP options (headers != 20 bytes) */ |
Luca Ceresoli | e8ccbb0 | 2011-05-04 02:40:43 +0000 | [diff] [blame] | 1077 | if ((ip->ip_hl_v & 0x0f) > 0x05) |
Remy Bohmer | b953578 | 2008-06-03 15:48:17 +0200 | [diff] [blame] | 1078 | return; |
Alessandro Rubini | f119e3d | 2009-08-07 13:58:56 +0200 | [diff] [blame] | 1079 | /* Check the Checksum of the header */ |
Joe Hershberger | c686fa1 | 2012-05-23 07:58:05 +0000 | [diff] [blame] | 1080 | if (!NetCksumOk((uchar *)ip, IP_HDR_SIZE / 2)) { |
Joe Hershberger | 05a377b | 2012-05-23 08:01:04 +0000 | [diff] [blame] | 1081 | debug("checksum bad\n"); |
wdenk | 2d96695 | 2002-10-31 22:12:35 +0000 | [diff] [blame] | 1082 | return; |
| 1083 | } |
Alessandro Rubini | f119e3d | 2009-08-07 13:58:56 +0200 | [diff] [blame] | 1084 | /* If it is not for us, ignore it */ |
Joe Hershberger | facf535 | 2012-05-23 07:58:12 +0000 | [diff] [blame] | 1085 | dst_ip = NetReadIP(&ip->ip_dst); |
| 1086 | if (NetOurIP && dst_ip != NetOurIP && dst_ip != 0xFFFFFFFF) { |
David Updegraff | 7280da7 | 2007-06-11 10:41:07 -0500 | [diff] [blame] | 1087 | #ifdef CONFIG_MCAST_TFTP |
Joe Hershberger | facf535 | 2012-05-23 07:58:12 +0000 | [diff] [blame] | 1088 | if (Mcast_addr != dst_ip) |
David Updegraff | 7280da7 | 2007-06-11 10:41:07 -0500 | [diff] [blame] | 1089 | #endif |
Luca Ceresoli | b19d0b7 | 2011-05-04 02:40:46 +0000 | [diff] [blame] | 1090 | return; |
wdenk | 2d96695 | 2002-10-31 22:12:35 +0000 | [diff] [blame] | 1091 | } |
Luca Ceresoli | 428ab36 | 2011-04-18 06:19:50 +0000 | [diff] [blame] | 1092 | /* Read source IP address for later use */ |
| 1093 | src_ip = NetReadIP(&ip->ip_src); |
wdenk | 2d96695 | 2002-10-31 22:12:35 +0000 | [diff] [blame] | 1094 | /* |
Alessandro Rubini | f119e3d | 2009-08-07 13:58:56 +0200 | [diff] [blame] | 1095 | * The function returns the unchanged packet if it's not |
| 1096 | * a fragment, and either the complete packet or NULL if |
| 1097 | * it is a fragment (if !CONFIG_IP_DEFRAG, it returns NULL) |
| 1098 | */ |
Luca Ceresoli | db21082 | 2011-05-04 02:40:47 +0000 | [diff] [blame] | 1099 | ip = NetDefragment(ip, &len); |
| 1100 | if (!ip) |
Alessandro Rubini | f119e3d | 2009-08-07 13:58:56 +0200 | [diff] [blame] | 1101 | return; |
| 1102 | /* |
wdenk | 2d96695 | 2002-10-31 22:12:35 +0000 | [diff] [blame] | 1103 | * watch for ICMP host redirects |
| 1104 | * |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 1105 | * There is no real handler code (yet). We just watch |
| 1106 | * for ICMP host redirect messages. In case anybody |
| 1107 | * sees these messages: please contact me |
| 1108 | * (wd@denx.de), or - even better - send me the |
| 1109 | * necessary fixes :-) |
wdenk | 2d96695 | 2002-10-31 22:12:35 +0000 | [diff] [blame] | 1110 | * |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 1111 | * Note: in all cases where I have seen this so far |
| 1112 | * it was a problem with the router configuration, |
| 1113 | * for instance when a router was configured in the |
| 1114 | * BOOTP reply, but the TFTP server was on the same |
| 1115 | * subnet. So this is probably a warning that your |
| 1116 | * configuration might be wrong. But I'm not really |
| 1117 | * sure if there aren't any other situations. |
Simon Glass | 230467c | 2011-10-24 18:00:01 +0000 | [diff] [blame] | 1118 | * |
| 1119 | * Simon Glass <sjg@chromium.org>: We get an ICMP when |
| 1120 | * we send a tftp packet to a dead connection, or when |
| 1121 | * there is no server at the other end. |
wdenk | 2d96695 | 2002-10-31 22:12:35 +0000 | [diff] [blame] | 1122 | */ |
| 1123 | if (ip->ip_p == IPPROTO_ICMP) { |
Simon Glass | 43c7296 | 2011-10-24 18:00:00 +0000 | [diff] [blame] | 1124 | receive_icmp(ip, len, src_ip, et); |
| 1125 | return; |
wdenk | 2d96695 | 2002-10-31 22:12:35 +0000 | [diff] [blame] | 1126 | } else if (ip->ip_p != IPPROTO_UDP) { /* Only UDP packets */ |
| 1127 | return; |
| 1128 | } |
| 1129 | |
Joe Hershberger | 05a377b | 2012-05-23 08:01:04 +0000 | [diff] [blame] | 1130 | debug_cond(DEBUG_DEV_PKT, |
| 1131 | "received UDP (to=%pI4, from=%pI4, len=%d)\n", |
| 1132 | &dst_ip, &src_ip, len); |
| 1133 | |
Stefan Roese | dfced81 | 2005-08-12 20:06:52 +0200 | [diff] [blame] | 1134 | #ifdef CONFIG_UDP_CHECKSUM |
| 1135 | if (ip->udp_xsum != 0) { |
Wolfgang Denk | 30b8732 | 2005-08-12 23:43:12 +0200 | [diff] [blame] | 1136 | ulong xsum; |
Stefan Roese | dfced81 | 2005-08-12 20:06:52 +0200 | [diff] [blame] | 1137 | ushort *sumptr; |
| 1138 | ushort sumlen; |
| 1139 | |
| 1140 | xsum = ip->ip_p; |
| 1141 | xsum += (ntohs(ip->udp_len)); |
| 1142 | xsum += (ntohl(ip->ip_src) >> 16) & 0x0000ffff; |
| 1143 | xsum += (ntohl(ip->ip_src) >> 0) & 0x0000ffff; |
| 1144 | xsum += (ntohl(ip->ip_dst) >> 16) & 0x0000ffff; |
| 1145 | xsum += (ntohl(ip->ip_dst) >> 0) & 0x0000ffff; |
| 1146 | |
| 1147 | sumlen = ntohs(ip->udp_len); |
| 1148 | sumptr = (ushort *) &(ip->udp_src); |
| 1149 | |
| 1150 | while (sumlen > 1) { |
Wolfgang Denk | 30b8732 | 2005-08-12 23:43:12 +0200 | [diff] [blame] | 1151 | ushort sumdata; |
Stefan Roese | dfced81 | 2005-08-12 20:06:52 +0200 | [diff] [blame] | 1152 | |
| 1153 | sumdata = *sumptr++; |
| 1154 | xsum += ntohs(sumdata); |
| 1155 | sumlen -= 2; |
| 1156 | } |
| 1157 | if (sumlen > 0) { |
Wolfgang Denk | 30b8732 | 2005-08-12 23:43:12 +0200 | [diff] [blame] | 1158 | ushort sumdata; |
Stefan Roese | dfced81 | 2005-08-12 20:06:52 +0200 | [diff] [blame] | 1159 | |
| 1160 | sumdata = *(unsigned char *) sumptr; |
Wolfgang Denk | 30b8732 | 2005-08-12 23:43:12 +0200 | [diff] [blame] | 1161 | sumdata = (sumdata << 8) & 0xff00; |
Stefan Roese | dfced81 | 2005-08-12 20:06:52 +0200 | [diff] [blame] | 1162 | xsum += sumdata; |
| 1163 | } |
| 1164 | while ((xsum >> 16) != 0) { |
Luca Ceresoli | 5c83a96 | 2011-05-11 03:59:54 +0000 | [diff] [blame] | 1165 | xsum = (xsum & 0x0000ffff) + |
| 1166 | ((xsum >> 16) & 0x0000ffff); |
Stefan Roese | dfced81 | 2005-08-12 20:06:52 +0200 | [diff] [blame] | 1167 | } |
| 1168 | if ((xsum != 0x00000000) && (xsum != 0x0000ffff)) { |
Wolfgang Denk | 12cec0a | 2008-07-11 01:16:00 +0200 | [diff] [blame] | 1169 | printf(" UDP wrong checksum %08lx %08x\n", |
| 1170 | xsum, ntohs(ip->udp_xsum)); |
Stefan Roese | dfced81 | 2005-08-12 20:06:52 +0200 | [diff] [blame] | 1171 | return; |
| 1172 | } |
| 1173 | } |
| 1174 | #endif |
| 1175 | |
David Updegraff | 7280da7 | 2007-06-11 10:41:07 -0500 | [diff] [blame] | 1176 | |
wdenk | b8fb619 | 2004-08-02 21:11:11 +0000 | [diff] [blame] | 1177 | #ifdef CONFIG_NETCONSOLE |
Joe Hershberger | 6fe8b45 | 2012-05-23 07:58:04 +0000 | [diff] [blame] | 1178 | nc_input_packet((uchar *)ip + IP_UDP_HDR_SIZE, |
Joe Hershberger | 19d6bc0 | 2012-10-03 09:14:03 +0000 | [diff] [blame] | 1179 | src_ip, |
Joe Hershberger | 6fe8b45 | 2012-05-23 07:58:04 +0000 | [diff] [blame] | 1180 | ntohs(ip->udp_dst), |
| 1181 | ntohs(ip->udp_src), |
| 1182 | ntohs(ip->udp_len) - UDP_HDR_SIZE); |
wdenk | b8fb619 | 2004-08-02 21:11:11 +0000 | [diff] [blame] | 1183 | #endif |
wdenk | 2d96695 | 2002-10-31 22:12:35 +0000 | [diff] [blame] | 1184 | /* |
| 1185 | * IP header OK. Pass the packet to the current handler. |
| 1186 | */ |
Joe Hershberger | f50357b | 2012-05-23 07:59:15 +0000 | [diff] [blame] | 1187 | (*udp_packet_handler)((uchar *)ip + IP_UDP_HDR_SIZE, |
| 1188 | ntohs(ip->udp_dst), |
| 1189 | src_ip, |
| 1190 | ntohs(ip->udp_src), |
| 1191 | ntohs(ip->udp_len) - UDP_HDR_SIZE); |
wdenk | 2d96695 | 2002-10-31 22:12:35 +0000 | [diff] [blame] | 1192 | break; |
| 1193 | } |
| 1194 | } |
| 1195 | |
| 1196 | |
| 1197 | /**********************************************************************/ |
| 1198 | |
Simon Glass | d6c5f55 | 2011-10-24 18:00:02 +0000 | [diff] [blame] | 1199 | static int net_check_prereq(enum proto_t protocol) |
wdenk | 2d96695 | 2002-10-31 22:12:35 +0000 | [diff] [blame] | 1200 | { |
| 1201 | switch (protocol) { |
wdenk | 0593920 | 2004-04-18 17:39:38 +0000 | [diff] [blame] | 1202 | /* Fall through */ |
Jon Loeliger | 54f35c2 | 2007-07-09 17:45:14 -0500 | [diff] [blame] | 1203 | #if defined(CONFIG_CMD_PING) |
wdenk | e6466f6 | 2003-06-05 19:27:42 +0000 | [diff] [blame] | 1204 | case PING: |
wdenk | 0593920 | 2004-04-18 17:39:38 +0000 | [diff] [blame] | 1205 | if (NetPingIP == 0) { |
Luca Ceresoli | 7cbd748 | 2011-05-11 03:59:56 +0000 | [diff] [blame] | 1206 | puts("*** ERROR: ping address not given\n"); |
Luca Ceresoli | 5fe6de2 | 2011-05-04 02:40:45 +0000 | [diff] [blame] | 1207 | return 1; |
wdenk | 0593920 | 2004-04-18 17:39:38 +0000 | [diff] [blame] | 1208 | } |
| 1209 | goto common; |
wdenk | e6466f6 | 2003-06-05 19:27:42 +0000 | [diff] [blame] | 1210 | #endif |
Jon Loeliger | 54f35c2 | 2007-07-09 17:45:14 -0500 | [diff] [blame] | 1211 | #if defined(CONFIG_CMD_SNTP) |
wdenk | b4ad962 | 2005-04-01 00:25:43 +0000 | [diff] [blame] | 1212 | case SNTP: |
| 1213 | if (NetNtpServerIP == 0) { |
Luca Ceresoli | 7cbd748 | 2011-05-11 03:59:56 +0000 | [diff] [blame] | 1214 | puts("*** ERROR: NTP server address not given\n"); |
Luca Ceresoli | 5fe6de2 | 2011-05-04 02:40:45 +0000 | [diff] [blame] | 1215 | return 1; |
wdenk | b4ad962 | 2005-04-01 00:25:43 +0000 | [diff] [blame] | 1216 | } |
| 1217 | goto common; |
| 1218 | #endif |
Robin Getz | 82f0d23 | 2009-07-20 14:53:54 -0400 | [diff] [blame] | 1219 | #if defined(CONFIG_CMD_DNS) |
| 1220 | case DNS: |
| 1221 | if (NetOurDNSIP == 0) { |
| 1222 | puts("*** ERROR: DNS server address not given\n"); |
| 1223 | return 1; |
| 1224 | } |
| 1225 | goto common; |
| 1226 | #endif |
Jon Loeliger | 54f35c2 | 2007-07-09 17:45:14 -0500 | [diff] [blame] | 1227 | #if defined(CONFIG_CMD_NFS) |
wdenk | be9c1cb | 2004-02-24 02:00:03 +0000 | [diff] [blame] | 1228 | case NFS: |
| 1229 | #endif |
Simon Glass | d6c5f55 | 2011-10-24 18:00:02 +0000 | [diff] [blame] | 1230 | case TFTPGET: |
Simon Glass | 6a398d2 | 2011-10-24 18:00:07 +0000 | [diff] [blame] | 1231 | case TFTPPUT: |
wdenk | 0593920 | 2004-04-18 17:39:38 +0000 | [diff] [blame] | 1232 | if (NetServerIP == 0) { |
Luca Ceresoli | 7cbd748 | 2011-05-11 03:59:56 +0000 | [diff] [blame] | 1233 | puts("*** ERROR: `serverip' not set\n"); |
Luca Ceresoli | 5fe6de2 | 2011-05-04 02:40:45 +0000 | [diff] [blame] | 1234 | return 1; |
wdenk | 0593920 | 2004-04-18 17:39:38 +0000 | [diff] [blame] | 1235 | } |
Luca Ceresoli | 7cbd748 | 2011-05-11 03:59:56 +0000 | [diff] [blame] | 1236 | #if defined(CONFIG_CMD_PING) || defined(CONFIG_CMD_SNTP) || \ |
| 1237 | defined(CONFIG_CMD_DNS) |
| 1238 | common: |
wdenk | e6466f6 | 2003-06-05 19:27:42 +0000 | [diff] [blame] | 1239 | #endif |
Simon Guinot | 00dceba | 2011-05-01 23:38:40 +0000 | [diff] [blame] | 1240 | /* Fall through */ |
wdenk | e6466f6 | 2003-06-05 19:27:42 +0000 | [diff] [blame] | 1241 | |
Simon Guinot | 00dceba | 2011-05-01 23:38:40 +0000 | [diff] [blame] | 1242 | case NETCONS: |
Luca Ceresoli | 7aa81a4 | 2011-05-17 00:03:40 +0000 | [diff] [blame] | 1243 | case TFTPSRV: |
wdenk | 0593920 | 2004-04-18 17:39:38 +0000 | [diff] [blame] | 1244 | if (NetOurIP == 0) { |
Luca Ceresoli | 7cbd748 | 2011-05-11 03:59:56 +0000 | [diff] [blame] | 1245 | puts("*** ERROR: `ipaddr' not set\n"); |
Luca Ceresoli | 5fe6de2 | 2011-05-04 02:40:45 +0000 | [diff] [blame] | 1246 | return 1; |
wdenk | 0593920 | 2004-04-18 17:39:38 +0000 | [diff] [blame] | 1247 | } |
| 1248 | /* Fall through */ |
wdenk | 2d96695 | 2002-10-31 22:12:35 +0000 | [diff] [blame] | 1249 | |
Peter Tyser | f7a48ca | 2010-09-30 11:25:48 -0500 | [diff] [blame] | 1250 | #ifdef CONFIG_CMD_RARP |
wdenk | 2d96695 | 2002-10-31 22:12:35 +0000 | [diff] [blame] | 1251 | case RARP: |
Peter Tyser | f7a48ca | 2010-09-30 11:25:48 -0500 | [diff] [blame] | 1252 | #endif |
wdenk | 2d96695 | 2002-10-31 22:12:35 +0000 | [diff] [blame] | 1253 | case BOOTP: |
wdenk | 145d2c1 | 2004-04-15 21:48:45 +0000 | [diff] [blame] | 1254 | case CDP: |
Peter Tyser | f7a48ca | 2010-09-30 11:25:48 -0500 | [diff] [blame] | 1255 | case DHCP: |
Joe Hershberger | b35a3a6 | 2012-05-23 08:00:12 +0000 | [diff] [blame] | 1256 | case LINKLOCAL: |
Luca Ceresoli | 7cbd748 | 2011-05-11 03:59:56 +0000 | [diff] [blame] | 1257 | if (memcmp(NetOurEther, "\0\0\0\0\0\0", 6) == 0) { |
Luca Ceresoli | 7cbd748 | 2011-05-11 03:59:56 +0000 | [diff] [blame] | 1258 | int num = eth_get_dev_index(); |
wdenk | 2d96695 | 2002-10-31 22:12:35 +0000 | [diff] [blame] | 1259 | |
wdenk | 0593920 | 2004-04-18 17:39:38 +0000 | [diff] [blame] | 1260 | switch (num) { |
| 1261 | case -1: |
Luca Ceresoli | 7cbd748 | 2011-05-11 03:59:56 +0000 | [diff] [blame] | 1262 | puts("*** ERROR: No ethernet found.\n"); |
Luca Ceresoli | 5fe6de2 | 2011-05-04 02:40:45 +0000 | [diff] [blame] | 1263 | return 1; |
wdenk | 0593920 | 2004-04-18 17:39:38 +0000 | [diff] [blame] | 1264 | case 0: |
Luca Ceresoli | 7cbd748 | 2011-05-11 03:59:56 +0000 | [diff] [blame] | 1265 | puts("*** ERROR: `ethaddr' not set\n"); |
wdenk | 2d96695 | 2002-10-31 22:12:35 +0000 | [diff] [blame] | 1266 | break; |
wdenk | 0593920 | 2004-04-18 17:39:38 +0000 | [diff] [blame] | 1267 | default: |
Luca Ceresoli | 7cbd748 | 2011-05-11 03:59:56 +0000 | [diff] [blame] | 1268 | printf("*** ERROR: `eth%daddr' not set\n", |
wdenk | 2d96695 | 2002-10-31 22:12:35 +0000 | [diff] [blame] | 1269 | num); |
| 1270 | break; |
wdenk | 0593920 | 2004-04-18 17:39:38 +0000 | [diff] [blame] | 1271 | } |
wdenk | 2d96695 | 2002-10-31 22:12:35 +0000 | [diff] [blame] | 1272 | |
Luca Ceresoli | 7cbd748 | 2011-05-11 03:59:56 +0000 | [diff] [blame] | 1273 | NetStartAgain(); |
Luca Ceresoli | 5fe6de2 | 2011-05-04 02:40:45 +0000 | [diff] [blame] | 1274 | return 2; |
wdenk | 0593920 | 2004-04-18 17:39:38 +0000 | [diff] [blame] | 1275 | } |
| 1276 | /* Fall through */ |
| 1277 | default: |
Luca Ceresoli | 5fe6de2 | 2011-05-04 02:40:45 +0000 | [diff] [blame] | 1278 | return 0; |
wdenk | 2d96695 | 2002-10-31 22:12:35 +0000 | [diff] [blame] | 1279 | } |
Luca Ceresoli | 5fe6de2 | 2011-05-04 02:40:45 +0000 | [diff] [blame] | 1280 | return 0; /* OK */ |
wdenk | 2d96695 | 2002-10-31 22:12:35 +0000 | [diff] [blame] | 1281 | } |
| 1282 | /**********************************************************************/ |
| 1283 | |
| 1284 | int |
Luca Ceresoli | f27d91f | 2011-05-04 02:40:44 +0000 | [diff] [blame] | 1285 | NetCksumOk(uchar *ptr, int len) |
wdenk | 2d96695 | 2002-10-31 22:12:35 +0000 | [diff] [blame] | 1286 | { |
| 1287 | return !((NetCksum(ptr, len) + 1) & 0xfffe); |
| 1288 | } |
| 1289 | |
| 1290 | |
| 1291 | unsigned |
Luca Ceresoli | f27d91f | 2011-05-04 02:40:44 +0000 | [diff] [blame] | 1292 | NetCksum(uchar *ptr, int len) |
wdenk | 2d96695 | 2002-10-31 22:12:35 +0000 | [diff] [blame] | 1293 | { |
| 1294 | ulong xsum; |
Stefan Roese | ac55328 | 2005-08-31 12:55:50 +0200 | [diff] [blame] | 1295 | ushort *p = (ushort *)ptr; |
wdenk | 2d96695 | 2002-10-31 22:12:35 +0000 | [diff] [blame] | 1296 | |
| 1297 | xsum = 0; |
| 1298 | while (len-- > 0) |
Wolfgang Denk | 3135c54 | 2005-08-26 01:36:03 +0200 | [diff] [blame] | 1299 | xsum += *p++; |
wdenk | 2d96695 | 2002-10-31 22:12:35 +0000 | [diff] [blame] | 1300 | xsum = (xsum & 0xffff) + (xsum >> 16); |
| 1301 | xsum = (xsum & 0xffff) + (xsum >> 16); |
Luca Ceresoli | 5fe6de2 | 2011-05-04 02:40:45 +0000 | [diff] [blame] | 1302 | return xsum & 0xffff; |
wdenk | 2d96695 | 2002-10-31 22:12:35 +0000 | [diff] [blame] | 1303 | } |
| 1304 | |
wdenk | 145d2c1 | 2004-04-15 21:48:45 +0000 | [diff] [blame] | 1305 | int |
| 1306 | NetEthHdrSize(void) |
| 1307 | { |
| 1308 | ushort myvlanid; |
| 1309 | |
| 1310 | myvlanid = ntohs(NetOurVLAN); |
| 1311 | if (myvlanid == (ushort)-1) |
| 1312 | myvlanid = VLAN_NONE; |
wdenk | 2d96695 | 2002-10-31 22:12:35 +0000 | [diff] [blame] | 1313 | |
Luca Ceresoli | 5c83a96 | 2011-05-11 03:59:54 +0000 | [diff] [blame] | 1314 | return ((myvlanid & VLAN_IDMASK) == VLAN_NONE) ? ETHER_HDR_SIZE : |
| 1315 | VLAN_ETHER_HDR_SIZE; |
wdenk | 145d2c1 | 2004-04-15 21:48:45 +0000 | [diff] [blame] | 1316 | } |
| 1317 | |
| 1318 | int |
Joe Hershberger | 4b7747e | 2012-05-15 08:59:04 +0000 | [diff] [blame] | 1319 | NetSetEther(uchar *xet, uchar * addr, uint prot) |
wdenk | 2d96695 | 2002-10-31 22:12:35 +0000 | [diff] [blame] | 1320 | { |
Joe Hershberger | 1178f41 | 2012-05-23 07:58:06 +0000 | [diff] [blame] | 1321 | struct ethernet_hdr *et = (struct ethernet_hdr *)xet; |
wdenk | 145d2c1 | 2004-04-15 21:48:45 +0000 | [diff] [blame] | 1322 | ushort myvlanid; |
| 1323 | |
| 1324 | myvlanid = ntohs(NetOurVLAN); |
| 1325 | if (myvlanid == (ushort)-1) |
| 1326 | myvlanid = VLAN_NONE; |
wdenk | 2d96695 | 2002-10-31 22:12:35 +0000 | [diff] [blame] | 1327 | |
Luca Ceresoli | 7cbd748 | 2011-05-11 03:59:56 +0000 | [diff] [blame] | 1328 | memcpy(et->et_dest, addr, 6); |
| 1329 | memcpy(et->et_src, NetOurEther, 6); |
wdenk | 145d2c1 | 2004-04-15 21:48:45 +0000 | [diff] [blame] | 1330 | if ((myvlanid & VLAN_IDMASK) == VLAN_NONE) { |
Luca Ceresoli | b19d0b7 | 2011-05-04 02:40:46 +0000 | [diff] [blame] | 1331 | et->et_protlen = htons(prot); |
wdenk | 145d2c1 | 2004-04-15 21:48:45 +0000 | [diff] [blame] | 1332 | return ETHER_HDR_SIZE; |
| 1333 | } else { |
Joe Hershberger | b43784c | 2012-05-23 07:58:07 +0000 | [diff] [blame] | 1334 | struct vlan_ethernet_hdr *vet = |
| 1335 | (struct vlan_ethernet_hdr *)xet; |
wdenk | 2d96695 | 2002-10-31 22:12:35 +0000 | [diff] [blame] | 1336 | |
wdenk | 145d2c1 | 2004-04-15 21:48:45 +0000 | [diff] [blame] | 1337 | vet->vet_vlan_type = htons(PROT_VLAN); |
| 1338 | vet->vet_tag = htons((0 << 5) | (myvlanid & VLAN_IDMASK)); |
| 1339 | vet->vet_type = htons(prot); |
| 1340 | return VLAN_ETHER_HDR_SIZE; |
| 1341 | } |
| 1342 | } |
wdenk | 2d96695 | 2002-10-31 22:12:35 +0000 | [diff] [blame] | 1343 | |
Joe Hershberger | 530cd6b | 2012-05-23 07:59:16 +0000 | [diff] [blame] | 1344 | int net_update_ether(struct ethernet_hdr *et, uchar *addr, uint prot) |
| 1345 | { |
| 1346 | ushort protlen; |
| 1347 | |
| 1348 | memcpy(et->et_dest, addr, 6); |
| 1349 | memcpy(et->et_src, NetOurEther, 6); |
| 1350 | protlen = ntohs(et->et_protlen); |
| 1351 | if (protlen == PROT_VLAN) { |
| 1352 | struct vlan_ethernet_hdr *vet = |
| 1353 | (struct vlan_ethernet_hdr *)et; |
| 1354 | vet->vet_type = htons(prot); |
| 1355 | return VLAN_ETHER_HDR_SIZE; |
| 1356 | } else if (protlen > 1514) { |
| 1357 | et->et_protlen = htons(prot); |
| 1358 | return ETHER_HDR_SIZE; |
| 1359 | } else { |
| 1360 | /* 802.2 + SNAP */ |
| 1361 | struct e802_hdr *et802 = (struct e802_hdr *)et; |
| 1362 | et802->et_prot = htons(prot); |
| 1363 | return E802_HDR_SIZE; |
| 1364 | } |
| 1365 | } |
| 1366 | |
Joe Hershberger | 2ed5b49 | 2012-05-23 07:59:07 +0000 | [diff] [blame] | 1367 | void net_set_ip_header(uchar *pkt, IPaddr_t dest, IPaddr_t source) |
wdenk | 2d96695 | 2002-10-31 22:12:35 +0000 | [diff] [blame] | 1368 | { |
Joe Hershberger | 2ed5b49 | 2012-05-23 07:59:07 +0000 | [diff] [blame] | 1369 | struct ip_udp_hdr *ip = (struct ip_udp_hdr *)pkt; |
wdenk | 2d96695 | 2002-10-31 22:12:35 +0000 | [diff] [blame] | 1370 | |
| 1371 | /* |
Joe Hershberger | 2ed5b49 | 2012-05-23 07:59:07 +0000 | [diff] [blame] | 1372 | * Construct an IP header. |
wdenk | 2d96695 | 2002-10-31 22:12:35 +0000 | [diff] [blame] | 1373 | */ |
Luca Ceresoli | 5c83a96 | 2011-05-11 03:59:54 +0000 | [diff] [blame] | 1374 | /* IP_HDR_SIZE / 4 (not including UDP) */ |
| 1375 | ip->ip_hl_v = 0x45; |
wdenk | 2d96695 | 2002-10-31 22:12:35 +0000 | [diff] [blame] | 1376 | ip->ip_tos = 0; |
Joe Hershberger | 2ed5b49 | 2012-05-23 07:59:07 +0000 | [diff] [blame] | 1377 | ip->ip_len = htons(IP_HDR_SIZE); |
wdenk | 2d96695 | 2002-10-31 22:12:35 +0000 | [diff] [blame] | 1378 | ip->ip_id = htons(NetIPID++); |
Peter Tyser | 0885bf0 | 2008-12-01 16:26:20 -0600 | [diff] [blame] | 1379 | ip->ip_off = htons(IP_FLAGS_DFRAG); /* Don't fragment */ |
wdenk | 2d96695 | 2002-10-31 22:12:35 +0000 | [diff] [blame] | 1380 | ip->ip_ttl = 255; |
wdenk | 2d96695 | 2002-10-31 22:12:35 +0000 | [diff] [blame] | 1381 | ip->ip_sum = 0; |
Luca Ceresoli | 5c83a96 | 2011-05-11 03:59:54 +0000 | [diff] [blame] | 1382 | /* already in network byte order */ |
Joe Hershberger | 2ed5b49 | 2012-05-23 07:59:07 +0000 | [diff] [blame] | 1383 | NetCopyIP((void *)&ip->ip_src, &source); |
| 1384 | /* already in network byte order */ |
Luca Ceresoli | f27d91f | 2011-05-04 02:40:44 +0000 | [diff] [blame] | 1385 | NetCopyIP((void *)&ip->ip_dst, &dest); |
Joe Hershberger | 2ed5b49 | 2012-05-23 07:59:07 +0000 | [diff] [blame] | 1386 | } |
| 1387 | |
| 1388 | void net_set_udp_header(uchar *pkt, IPaddr_t dest, int dport, int sport, |
| 1389 | int len) |
| 1390 | { |
| 1391 | struct ip_udp_hdr *ip = (struct ip_udp_hdr *)pkt; |
| 1392 | |
| 1393 | /* |
| 1394 | * If the data is an odd number of bytes, zero the |
| 1395 | * byte after the last byte so that the checksum |
| 1396 | * will work. |
| 1397 | */ |
| 1398 | if (len & 1) |
| 1399 | pkt[IP_UDP_HDR_SIZE + len] = 0; |
| 1400 | |
| 1401 | net_set_ip_header(pkt, dest, NetOurIP); |
| 1402 | ip->ip_len = htons(IP_UDP_HDR_SIZE + len); |
| 1403 | ip->ip_p = IPPROTO_UDP; |
| 1404 | ip->ip_sum = ~NetCksum((uchar *)ip, IP_HDR_SIZE >> 1); |
| 1405 | |
wdenk | 2d96695 | 2002-10-31 22:12:35 +0000 | [diff] [blame] | 1406 | ip->udp_src = htons(sport); |
| 1407 | ip->udp_dst = htons(dport); |
Joe Hershberger | 6fe8b45 | 2012-05-23 07:58:04 +0000 | [diff] [blame] | 1408 | ip->udp_len = htons(UDP_HDR_SIZE + len); |
wdenk | 2d96695 | 2002-10-31 22:12:35 +0000 | [diff] [blame] | 1409 | ip->udp_xsum = 0; |
wdenk | 2d96695 | 2002-10-31 22:12:35 +0000 | [diff] [blame] | 1410 | } |
| 1411 | |
Luca Ceresoli | 7cbd748 | 2011-05-11 03:59:56 +0000 | [diff] [blame] | 1412 | void copy_filename(char *dst, const char *src, int size) |
wdenk | 2d96695 | 2002-10-31 22:12:35 +0000 | [diff] [blame] | 1413 | { |
| 1414 | if (*src && (*src == '"')) { |
| 1415 | ++src; |
| 1416 | --size; |
| 1417 | } |
| 1418 | |
Luca Ceresoli | e8ccbb0 | 2011-05-04 02:40:43 +0000 | [diff] [blame] | 1419 | while ((--size > 0) && *src && (*src != '"')) |
wdenk | 2d96695 | 2002-10-31 22:12:35 +0000 | [diff] [blame] | 1420 | *dst++ = *src++; |
wdenk | 2d96695 | 2002-10-31 22:12:35 +0000 | [diff] [blame] | 1421 | *dst = '\0'; |
| 1422 | } |
| 1423 | |
Luca Ceresoli | 5c83a96 | 2011-05-11 03:59:54 +0000 | [diff] [blame] | 1424 | #if defined(CONFIG_CMD_NFS) || \ |
| 1425 | defined(CONFIG_CMD_SNTP) || \ |
| 1426 | defined(CONFIG_CMD_DNS) |
Robin Getz | 82f0d23 | 2009-07-20 14:53:54 -0400 | [diff] [blame] | 1427 | /* |
Robin Getz | 55b50e9 | 2010-03-08 14:07:00 -0500 | [diff] [blame] | 1428 | * make port a little random (1024-17407) |
| 1429 | * This keeps the math somewhat trivial to compute, and seems to work with |
| 1430 | * all supported protocols/clients/servers |
Robin Getz | 82f0d23 | 2009-07-20 14:53:54 -0400 | [diff] [blame] | 1431 | */ |
| 1432 | unsigned int random_port(void) |
| 1433 | { |
Robin Getz | 55b50e9 | 2010-03-08 14:07:00 -0500 | [diff] [blame] | 1434 | return 1024 + (get_timer(0) % 0x4000); |
Robin Getz | 82f0d23 | 2009-07-20 14:53:54 -0400 | [diff] [blame] | 1435 | } |
| 1436 | #endif |
| 1437 | |
Luca Ceresoli | 7cbd748 | 2011-05-11 03:59:56 +0000 | [diff] [blame] | 1438 | void ip_to_string(IPaddr_t x, char *s) |
wdenk | 2d96695 | 2002-10-31 22:12:35 +0000 | [diff] [blame] | 1439 | { |
Luca Ceresoli | 7cbd748 | 2011-05-11 03:59:56 +0000 | [diff] [blame] | 1440 | x = ntohl(x); |
| 1441 | sprintf(s, "%d.%d.%d.%d", |
| 1442 | (int) ((x >> 24) & 0xff), |
| 1443 | (int) ((x >> 16) & 0xff), |
| 1444 | (int) ((x >> 8) & 0xff), (int) ((x >> 0) & 0xff) |
wdenk | 0593920 | 2004-04-18 17:39:38 +0000 | [diff] [blame] | 1445 | ); |
wdenk | 2d96695 | 2002-10-31 22:12:35 +0000 | [diff] [blame] | 1446 | } |
| 1447 | |
wdenk | 145d2c1 | 2004-04-15 21:48:45 +0000 | [diff] [blame] | 1448 | void VLAN_to_string(ushort x, char *s) |
| 1449 | { |
| 1450 | x = ntohs(x); |
| 1451 | |
| 1452 | if (x == (ushort)-1) |
| 1453 | x = VLAN_NONE; |
| 1454 | |
| 1455 | if (x == VLAN_NONE) |
| 1456 | strcpy(s, "none"); |
| 1457 | else |
| 1458 | sprintf(s, "%d", x & VLAN_IDMASK); |
| 1459 | } |
| 1460 | |
Mike Frysinger | 89c7392 | 2010-10-20 07:16:48 -0400 | [diff] [blame] | 1461 | ushort string_to_VLAN(const char *s) |
wdenk | 145d2c1 | 2004-04-15 21:48:45 +0000 | [diff] [blame] | 1462 | { |
| 1463 | ushort id; |
| 1464 | |
| 1465 | if (s == NULL) |
wdenk | 656140b | 2004-04-25 13:18:40 +0000 | [diff] [blame] | 1466 | return htons(VLAN_NONE); |
wdenk | 145d2c1 | 2004-04-15 21:48:45 +0000 | [diff] [blame] | 1467 | |
| 1468 | if (*s < '0' || *s > '9') |
| 1469 | id = VLAN_NONE; |
| 1470 | else |
| 1471 | id = (ushort)simple_strtoul(s, NULL, 10); |
| 1472 | |
wdenk | 656140b | 2004-04-25 13:18:40 +0000 | [diff] [blame] | 1473 | return htons(id); |
wdenk | 145d2c1 | 2004-04-15 21:48:45 +0000 | [diff] [blame] | 1474 | } |
| 1475 | |
wdenk | 145d2c1 | 2004-04-15 21:48:45 +0000 | [diff] [blame] | 1476 | ushort getenv_VLAN(char *var) |
| 1477 | { |
Luca Ceresoli | 5fe6de2 | 2011-05-04 02:40:45 +0000 | [diff] [blame] | 1478 | return string_to_VLAN(getenv(var)); |
wdenk | 145d2c1 | 2004-04-15 21:48:45 +0000 | [diff] [blame] | 1479 | } |