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