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