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