blob: 37b4aab34d455a7eb9cf3af97a1ead417c792d62 [file] [log] [blame]
wdenk2d966952002-10-31 22:12:35 +00001/*
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 Denkdb065922014-09-30 10:44:01 +02009 * SPDX-License-Identifier: GPL-2.0
wdenk2d966952002-10-31 22:12:35 +000010 */
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 Hershbergerb35a3a62012-05-23 08:00:12 +000027 * LINK_LOCAL:
28 *
29 * Prerequisites: - own ethernet address
30 * We want: - own IP address
31 * Next step: ARP
32 *
wdenk2d966952002-10-31 22:12:35 +000033 * 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 Denk30b87322005-08-12 23:43:12 +020050 * Prerequisites: - own ethernet address
51 * We want: - IP, Netmask, ServerIP, Gateway IP
52 * - bootfilename, lease time
53 * Next step: - TFTP
wdenk2d966952002-10-31 22:12:35 +000054 *
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
wdenkbe9c1cb2004-02-24 02:00:03 +000065 *
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
wdenkb4ad9622005-04-01 00:25:43 +000074 *
75 * SNTP:
76 *
Wolfgang Denk30b87322005-08-12 23:43:12 +020077 * Prerequisites: - own ethernet address
wdenkb4ad9622005-04-01 00:25:43 +000078 * - own IP address
79 * We want: - network time
80 * Next step: none
wdenk2d966952002-10-31 22:12:35 +000081 */
82
83
84#include <common.h>
wdenk2d966952002-10-31 22:12:35 +000085#include <command.h>
Joe Hershbergerdc9d1e52012-12-11 22:16:26 -060086#include <environment.h>
wdenk2d966952002-10-31 22:12:35 +000087#include <net.h>
Joe Hershbergerfee076e2012-05-23 07:58:15 +000088#if defined(CONFIG_STATUS_LED)
wdenk49c3f672003-10-08 22:33:00 +000089#include <miiphy.h>
Joe Hershbergerfee076e2012-05-23 07:58:15 +000090#include <status_led.h>
wdenkb4ad9622005-04-01 00:25:43 +000091#endif
Joe Hershbergerfee076e2012-05-23 07:58:15 +000092#include <watchdog.h>
93#include <linux/compiler.h>
94#include "arp.h"
95#include "bootp.h"
Joe Hershbergera4215b02012-05-23 07:57:59 +000096#include "cdp.h"
Robin Getz82f0d232009-07-20 14:53:54 -040097#if defined(CONFIG_CMD_DNS)
98#include "dns.h"
99#endif
Joe Hershbergerb35a3a62012-05-23 08:00:12 +0000100#include "link_local.h"
Joe Hershbergerfee076e2012-05-23 07:58:15 +0000101#include "nfs.h"
Joe Hershbergerc21bf372012-05-23 07:58:02 +0000102#include "ping.h"
Joe Hershbergerfee076e2012-05-23 07:58:15 +0000103#include "rarp.h"
104#if defined(CONFIG_CMD_SNTP)
105#include "sntp.h"
106#endif
107#include "tftp.h"
wdenk2d966952002-10-31 22:12:35 +0000108
Wolfgang Denk6405a152006-03-31 18:32:53 +0200109DECLARE_GLOBAL_DATA_PTR;
110
wdenk2d966952002-10-31 22:12:35 +0000111/** BOOTP EXTENTIONS **/
112
Luca Ceresoli5c83a962011-05-11 03:59:54 +0000113/* Our subnet mask (0=unknown) */
Luca Ceresolid01bc1c2011-05-11 03:59:55 +0000114IPaddr_t NetOurSubnetMask;
Luca Ceresoli5c83a962011-05-11 03:59:54 +0000115/* Our gateways IP address */
Luca Ceresolid01bc1c2011-05-11 03:59:55 +0000116IPaddr_t NetOurGatewayIP;
Luca Ceresoli5c83a962011-05-11 03:59:54 +0000117/* Our DNS IP address */
Luca Ceresolid01bc1c2011-05-11 03:59:55 +0000118IPaddr_t NetOurDNSIP;
Jon Loeliger5336a762007-07-09 22:08:34 -0500119#if defined(CONFIG_BOOTP_DNS2)
Luca Ceresoli5c83a962011-05-11 03:59:54 +0000120/* Our 2nd DNS IP address */
Luca Ceresolid01bc1c2011-05-11 03:59:55 +0000121IPaddr_t NetOurDNS2IP;
stroesee0aadfb2003-08-28 14:17:32 +0000122#endif
Luca Ceresoli5c83a962011-05-11 03:59:54 +0000123/* Our NIS domain */
Luca Ceresolid01bc1c2011-05-11 03:59:55 +0000124char NetOurNISDomain[32] = {0,};
Luca Ceresoli5c83a962011-05-11 03:59:54 +0000125/* Our hostname */
Luca Ceresolid01bc1c2011-05-11 03:59:55 +0000126char NetOurHostName[32] = {0,};
Luca Ceresoli5c83a962011-05-11 03:59:54 +0000127/* Our bootpath */
Luca Ceresolid01bc1c2011-05-11 03:59:55 +0000128char NetOurRootPath[64] = {0,};
Luca Ceresoli5c83a962011-05-11 03:59:54 +0000129/* Our bootfile size in blocks */
Luca Ceresolid01bc1c2011-05-11 03:59:55 +0000130ushort NetBootFileSize;
wdenk2d966952002-10-31 22:12:35 +0000131
David Updegraff7280da72007-06-11 10:41:07 -0500132#ifdef CONFIG_MCAST_TFTP /* Multicast TFTP */
133IPaddr_t Mcast_addr;
134#endif
135
wdenk2d966952002-10-31 22:12:35 +0000136/** END OF BOOTP EXTENTIONS **/
137
Luca Ceresoli5c83a962011-05-11 03:59:54 +0000138/* The actual transferred size of the bootfile (in bytes) */
139ulong NetBootFileXferSize;
140/* Our ethernet address */
141uchar NetOurEther[6];
142/* Boot server enet address */
Luca Ceresolid01bc1c2011-05-11 03:59:55 +0000143uchar NetServerEther[6];
Luca Ceresoli5c83a962011-05-11 03:59:54 +0000144/* Our IP addr (0 = unknown) */
145IPaddr_t NetOurIP;
146/* Server IP addr (0 = unknown) */
147IPaddr_t NetServerIP;
148/* Current receive packet */
Joe Hershberger4b7747e2012-05-15 08:59:04 +0000149uchar *NetRxPacket;
Luca Ceresoli5c83a962011-05-11 03:59:54 +0000150/* Current rx packet length */
151int NetRxPacketLen;
152/* IP packet ID */
153unsigned NetIPID;
154/* Ethernet bcast address */
Luca Ceresolid01bc1c2011-05-11 03:59:55 +0000155uchar NetBcastAddr[6] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
156uchar NetEtherNullAddr[6];
Rafal Jaworowski1f2c9a42007-12-27 18:19:02 +0100157#ifdef CONFIG_API
Joe Hershberger4b7747e2012-05-15 08:59:04 +0000158void (*push_packet)(void *, int len) = 0;
Rafal Jaworowski1f2c9a42007-12-27 18:19:02 +0100159#endif
Luca Ceresoli5c83a962011-05-11 03:59:54 +0000160/* Network loop state */
Joe Hershbergerd4bb76a2012-05-23 07:59:14 +0000161enum net_loop_state net_state;
Luca Ceresoli5c83a962011-05-11 03:59:54 +0000162/* Tried all network devices */
Luca Ceresolid01bc1c2011-05-11 03:59:55 +0000163int NetRestartWrap;
Luca Ceresoli5c83a962011-05-11 03:59:54 +0000164/* Network loop restarted */
Luca Ceresolid01bc1c2011-05-11 03:59:55 +0000165static int NetRestarted;
Luca Ceresoli5c83a962011-05-11 03:59:54 +0000166/* At least one device configured */
Luca Ceresolid01bc1c2011-05-11 03:59:55 +0000167static int NetDevExists;
wdenk2d966952002-10-31 22:12:35 +0000168
wdenk05939202004-04-18 17:39:38 +0000169/* XXX in both little & big endian machines 0xFFFF == ntohs(-1) */
Luca Ceresoli5c83a962011-05-11 03:59:54 +0000170/* default is without VLAN */
171ushort NetOurVLAN = 0xFFFF;
172/* ditto */
173ushort NetOurNativeVLAN = 0xFFFF;
wdenk145d2c12004-04-15 21:48:45 +0000174
Luca Ceresoli5c83a962011-05-11 03:59:54 +0000175/* Boot File name */
176char BootFile[128];
wdenk2d966952002-10-31 22:12:35 +0000177
Jon Loeliger54f35c22007-07-09 17:45:14 -0500178#if defined(CONFIG_CMD_SNTP)
Luca Ceresoli5c83a962011-05-11 03:59:54 +0000179/* NTP server IP address */
180IPaddr_t NetNtpServerIP;
181/* offset time from UTC */
Luca Ceresolid01bc1c2011-05-11 03:59:55 +0000182int NetTimeOffset;
wdenkb4ad9622005-04-01 00:25:43 +0000183#endif
184
Kim Phillips40c2c032012-10-29 13:34:33 +0000185static uchar PktBuf[(PKTBUFSRX+1) * PKTSIZE_ALIGN + PKTALIGN];
wdenk2d966952002-10-31 22:12:35 +0000186
Luca Ceresoli5c83a962011-05-11 03:59:54 +0000187/* Receive packet */
Joe Hershberger4b7747e2012-05-15 08:59:04 +0000188uchar *NetRxPackets[PKTBUFSRX];
wdenk2d966952002-10-31 22:12:35 +0000189
Joe Hershbergerf50357b2012-05-23 07:59:15 +0000190/* Current UDP RX packet handler */
191static rxhand_f *udp_packet_handler;
192/* Current ARP RX packet handler */
193static rxhand_f *arp_packet_handler;
Simon Glass2928cd82011-10-26 14:18:38 +0000194#ifdef CONFIG_CMD_TFTPPUT
Joe Hershbergerf50357b2012-05-23 07:59:15 +0000195/* Current ICMP rx handler */
196static rxhand_icmp_f *packet_icmp_handler;
Simon Glass2928cd82011-10-26 14:18:38 +0000197#endif
Luca Ceresoli5c83a962011-05-11 03:59:54 +0000198/* Current timeout handler */
199static thand_f *timeHandler;
200/* Time base value */
201static ulong timeStart;
202/* Current timeout value */
203static ulong timeDelta;
204/* THE transmit packet */
Joe Hershberger4b7747e2012-05-15 08:59:04 +0000205uchar *NetTxPacket;
wdenk2d966952002-10-31 22:12:35 +0000206
Simon Glassd6c5f552011-10-24 18:00:02 +0000207static int net_check_prereq(enum proto_t protocol);
wdenk2d966952002-10-31 22:12:35 +0000208
Remy Bohmer0ddb8e82009-10-28 22:13:39 +0100209static int NetTryCount;
210
Jim Lin6c8921f2013-08-13 19:03:05 +0800211int __maybe_unused net_busy_flag;
212
wdenk2d966952002-10-31 22:12:35 +0000213/**********************************************************************/
wdenke6466f62003-06-05 19:27:42 +0000214
Joe Hershbergerdc9d1e52012-12-11 22:16:26 -0600215static 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}
229U_BOOT_ENV_CALLBACK(bootfile, on_bootfile);
230
Simon Glass5234ad12011-10-27 06:24:32 +0000231/*
232 * Check if autoload is enabled. If so, use either NFS or TFTP to download
233 * the boot file.
234 */
235void net_auto_load(void)
236{
Joe Hershberger864ec562012-12-11 22:16:22 -0600237#if defined(CONFIG_CMD_NFS)
Simon Glass5234ad12011-10-27 06:24:32 +0000238 const char *s = getenv("autoload");
239
Joe Hershberger864ec562012-12-11 22:16:22 -0600240 if (s != NULL && strcmp(s, "NFS") == 0) {
241 /*
242 * Use NFS to load the bootfile.
243 */
244 NfsStart();
245 return;
246 }
Simon Glass5234ad12011-10-27 06:24:32 +0000247#endif
Joe Hershberger864ec562012-12-11 22:16:22 -0600248 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 Glass5234ad12011-10-27 06:24:32 +0000255 }
256 TftpStart(TFTPGET);
257}
258
Joe Hershberger202c6002012-05-23 07:59:21 +0000259static void NetInitLoop(void)
Heiko Schocher0c303fa2009-02-10 09:38:52 +0100260{
Luca Ceresolid01bc1c2011-05-11 03:59:55 +0000261 static int env_changed_id;
Luca Ceresoli7cbd7482011-05-11 03:59:56 +0000262 int env_id = get_env_id();
Heiko Schocher0c303fa2009-02-10 09:38:52 +0100263
264 /* update only when the environment has changed */
Michael Zaidmanb97bfe42009-04-04 01:43:00 +0300265 if (env_changed_id != env_id) {
Enric Balletbo i Serra7019d252011-05-31 21:01:47 +0000266 NetOurIP = getenv_IPaddr("ipaddr");
Luca Ceresoli7cbd7482011-05-11 03:59:56 +0000267 NetOurGatewayIP = getenv_IPaddr("gatewayip");
268 NetOurSubnetMask = getenv_IPaddr("netmask");
269 NetServerIP = getenv_IPaddr("serverip");
Heiko Schocher0c303fa2009-02-10 09:38:52 +0100270 NetOurNativeVLAN = getenv_VLAN("nvlan");
Michael Zaidmanb97bfe42009-04-04 01:43:00 +0300271 NetOurVLAN = getenv_VLAN("vlan");
Robin Getz82f0d232009-07-20 14:53:54 -0400272#if defined(CONFIG_CMD_DNS)
273 NetOurDNSIP = getenv_IPaddr("dnsip");
274#endif
Michael Zaidmanb97bfe42009-04-04 01:43:00 +0300275 env_changed_id = env_id;
Heiko Schocher0c303fa2009-02-10 09:38:52 +0100276 }
Jim Lin09a2bb82013-05-17 17:41:03 +0800277 if (eth_get_dev())
Joe Hershberger11cd5a02015-03-22 17:09:00 -0500278 memcpy(NetOurEther, eth_get_ethaddr(), 6);
Michael Zaidmanb97bfe42009-04-04 01:43:00 +0300279
Heiko Schocher3b195ff2009-04-28 08:36:11 +0200280 return;
Heiko Schocher0c303fa2009-02-10 09:38:52 +0100281}
282
Joe Hershbergerf50357b2012-05-23 07:59:15 +0000283static void net_clear_handlers(void)
284{
285 net_set_udp_handler(NULL);
286 net_set_arp_handler(NULL);
287 NetSetTimeout(0, NULL);
288}
289
290static void net_cleanup_loop(void)
291{
292 net_clear_handlers();
293}
294
Joe Hershberger017e5c42012-05-23 07:59:22 +0000295void 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
wdenke6466f62003-06-05 19:27:42 +0000320/**********************************************************************/
wdenk2d966952002-10-31 22:12:35 +0000321/*
322 * Main network processing loop.
323 */
324
Simon Glassd6c5f552011-10-24 18:00:02 +0000325int NetLoop(enum proto_t protocol)
wdenk2d966952002-10-31 22:12:35 +0000326{
Simon Glass230467c2011-10-24 18:00:01 +0000327 int ret = -1;
wdenk2d966952002-10-31 22:12:35 +0000328
wdenk2d966952002-10-31 22:12:35 +0000329 NetRestarted = 0;
330 NetDevExists = 0;
Remy Bohmer0ddb8e82009-10-28 22:13:39 +0100331 NetTryCount = 1;
Joe Hershberger05a377b2012-05-23 08:01:04 +0000332 debug_cond(DEBUG_INT_STATE, "--- NetLoop Entry\n");
wdenke6466f62003-06-05 19:27:42 +0000333
Simon Glass768cbf02011-12-10 11:08:06 +0000334 bootstage_mark_name(BOOTSTAGE_ID_ETH_START, "eth_start");
Joe Hershberger017e5c42012-05-23 07:59:22 +0000335 net_init();
Joe Hershberger9f374062012-08-03 10:59:08 +0000336 if (eth_is_on_demand_init() || protocol != NETCONS) {
wdenkfa66e932005-04-03 14:52:59 +0000337 eth_halt();
Joe Hershberger9f374062012-08-03 10:59:08 +0000338 eth_set_current();
Joe Hershberger3dbe17e2015-03-22 17:09:06 -0500339 if (eth_init() < 0) {
Joe Hershberger9f374062012-08-03 10:59:08 +0000340 eth_halt();
341 return -1;
342 }
343 } else
Joe Hershberger3dbe17e2015-03-22 17:09:06 -0500344 eth_init_state_only();
wdenk2d966952002-10-31 22:12:35 +0000345
346restart:
Jim Lin6c8921f2013-08-13 19:03:05 +0800347#ifdef CONFIG_USB_KEYBOARD
348 net_busy_flag = 0;
349#endif
Joe Hershbergerd4bb76a2012-05-23 07:59:14 +0000350 net_set_state(NETLOOP_CONTINUE);
wdenk2d966952002-10-31 22:12:35 +0000351
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 Hershberger05a377b2012-05-23 08:01:04 +0000357 debug_cond(DEBUG_INT_STATE, "--- NetLoop Init\n");
Joe Hershberger202c6002012-05-23 07:59:21 +0000358 NetInitLoop();
wdenk2d966952002-10-31 22:12:35 +0000359
Luca Ceresoli7cbd7482011-05-11 03:59:56 +0000360 switch (net_check_prereq(protocol)) {
wdenk2d966952002-10-31 22:12:35 +0000361 case 1:
362 /* network not configured */
wdenkfa66e932005-04-03 14:52:59 +0000363 eth_halt();
Luca Ceresoli5fe6de22011-05-04 02:40:45 +0000364 return -1;
wdenk2d966952002-10-31 22:12:35 +0000365
wdenk2d966952002-10-31 22:12:35 +0000366 case 2:
367 /* network device not configured */
368 break;
wdenk2d966952002-10-31 22:12:35 +0000369
370 case 0:
wdenk2d966952002-10-31 22:12:35 +0000371 NetDevExists = 1;
Simon Glassd6c5f552011-10-24 18:00:02 +0000372 NetBootFileXferSize = 0;
wdenk2d966952002-10-31 22:12:35 +0000373 switch (protocol) {
Simon Glassd6c5f552011-10-24 18:00:02 +0000374 case TFTPGET:
Simon Glass6a398d22011-10-24 18:00:07 +0000375#ifdef CONFIG_CMD_TFTPPUT
376 case TFTPPUT:
377#endif
wdenk2d966952002-10-31 22:12:35 +0000378 /* always use ARP to get server ethernet address */
Simon Glassd6c5f552011-10-24 18:00:02 +0000379 TftpStart(protocol);
wdenk2d966952002-10-31 22:12:35 +0000380 break;
Luca Ceresoli7aa81a42011-05-17 00:03:40 +0000381#ifdef CONFIG_CMD_TFTPSRV
382 case TFTPSRV:
383 TftpStartServer();
384 break;
385#endif
Jon Loeliger54f35c22007-07-09 17:45:14 -0500386#if defined(CONFIG_CMD_DHCP)
wdenk2d966952002-10-31 22:12:35 +0000387 case DHCP:
Stephen Warren69e1e352014-07-25 17:30:48 -0600388 BootpReset();
Michael Zaidman16bb21d2009-07-14 23:37:12 +0300389 NetOurIP = 0;
wdenk2d966952002-10-31 22:12:35 +0000390 DhcpRequest(); /* Basically same as BOOTP */
391 break;
Jon Loeligera9807e52007-07-10 11:05:02 -0500392#endif
wdenk2d966952002-10-31 22:12:35 +0000393
394 case BOOTP:
Stephen Warren69e1e352014-07-25 17:30:48 -0600395 BootpReset();
Michael Zaidman16bb21d2009-07-14 23:37:12 +0300396 NetOurIP = 0;
Luca Ceresoli7cbd7482011-05-11 03:59:56 +0000397 BootpRequest();
wdenk2d966952002-10-31 22:12:35 +0000398 break;
399
Peter Tyserf7a48ca2010-09-30 11:25:48 -0500400#if defined(CONFIG_CMD_RARP)
wdenk2d966952002-10-31 22:12:35 +0000401 case RARP:
402 RarpTry = 0;
Michael Zaidman16bb21d2009-07-14 23:37:12 +0300403 NetOurIP = 0;
Luca Ceresoli7cbd7482011-05-11 03:59:56 +0000404 RarpRequest();
wdenk2d966952002-10-31 22:12:35 +0000405 break;
Peter Tyserf7a48ca2010-09-30 11:25:48 -0500406#endif
Jon Loeliger54f35c22007-07-09 17:45:14 -0500407#if defined(CONFIG_CMD_PING)
wdenke6466f62003-06-05 19:27:42 +0000408 case PING:
Joe Hershbergerc21bf372012-05-23 07:58:02 +0000409 ping_start();
wdenke6466f62003-06-05 19:27:42 +0000410 break;
411#endif
Jon Loeliger54f35c22007-07-09 17:45:14 -0500412#if defined(CONFIG_CMD_NFS)
wdenkbe9c1cb2004-02-24 02:00:03 +0000413 case NFS:
414 NfsStart();
415 break;
416#endif
Jon Loeliger54f35c22007-07-09 17:45:14 -0500417#if defined(CONFIG_CMD_CDP)
wdenk145d2c12004-04-15 21:48:45 +0000418 case CDP:
419 CDPStart();
420 break;
421#endif
Hannes Petermaierea070422014-06-13 06:25:29 +0200422#if defined (CONFIG_NETCONSOLE) && !(CONFIG_SPL_BUILD)
wdenkb8fb6192004-08-02 21:11:11 +0000423 case NETCONS:
424 NcStart();
425 break;
426#endif
Jon Loeliger54f35c22007-07-09 17:45:14 -0500427#if defined(CONFIG_CMD_SNTP)
wdenkb4ad9622005-04-01 00:25:43 +0000428 case SNTP:
429 SntpStart();
430 break;
431#endif
Robin Getz82f0d232009-07-20 14:53:54 -0400432#if defined(CONFIG_CMD_DNS)
433 case DNS:
434 DnsStart();
435 break;
436#endif
Joe Hershbergerb35a3a62012-05-23 08:00:12 +0000437#if defined(CONFIG_CMD_LINK_LOCAL)
438 case LINKLOCAL:
439 link_local_start();
440 break;
441#endif
wdenk2d966952002-10-31 22:12:35 +0000442 default:
443 break;
444 }
445
wdenk2d966952002-10-31 22:12:35 +0000446 break;
447 }
448
Jon Loeliger54f35c22007-07-09 17:45:14 -0500449#if defined(CONFIG_MII) || defined(CONFIG_CMD_MII)
Luca Ceresoli5c83a962011-05-11 03:59:54 +0000450#if defined(CONFIG_SYS_FAULT_ECHO_LINK_DOWN) && \
451 defined(CONFIG_STATUS_LED) && \
452 defined(STATUS_LED_RED)
wdenk49c3f672003-10-08 22:33:00 +0000453 /*
wdenk9c53f402003-10-15 23:53:47 +0000454 * Echo the inverted link state to the fault LED.
wdenk49c3f672003-10-08 22:33:00 +0000455 */
Luca Ceresolie8ccbb02011-05-04 02:40:43 +0000456 if (miiphy_link(eth_get_dev()->name, CONFIG_SYS_FAULT_MII_ADDR))
Luca Ceresoli7cbd7482011-05-11 03:59:56 +0000457 status_led_set(STATUS_LED_RED, STATUS_LED_OFF);
Luca Ceresolie8ccbb02011-05-04 02:40:43 +0000458 else
Luca Ceresoli7cbd7482011-05-11 03:59:56 +0000459 status_led_set(STATUS_LED_RED, STATUS_LED_ON);
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200460#endif /* CONFIG_SYS_FAULT_ECHO_LINK_DOWN, ... */
wdenk49c3f672003-10-08 22:33:00 +0000461#endif /* CONFIG_MII, ... */
Jim Lin6c8921f2013-08-13 19:03:05 +0800462#ifdef CONFIG_USB_KEYBOARD
463 net_busy_flag = 1;
464#endif
wdenk2d966952002-10-31 22:12:35 +0000465
466 /*
467 * Main packet reception loop. Loop receiving packets until
Joe Hershbergerd4bb76a2012-05-23 07:59:14 +0000468 * someone sets `net_state' to a state that terminates.
wdenk2d966952002-10-31 22:12:35 +0000469 */
470 for (;;) {
471 WATCHDOG_RESET();
472#ifdef CONFIG_SHOW_ACTIVITY
Joe Hershberger77001b432012-05-15 08:59:08 +0000473 show_activity(1);
wdenk2d966952002-10-31 22:12:35 +0000474#endif
475 /*
476 * Check the ethernet for a new packet. The ethernet
477 * receive routine will process it.
478 */
Guennadi Liakhovetskib38c2b32008-04-03 17:04:19 +0200479 eth_rx();
wdenk2d966952002-10-31 22:12:35 +0000480
481 /*
482 * Abort if ctrl-c was pressed.
483 */
484 if (ctrlc()) {
Joe Hershbergerde8205a2012-05-23 07:59:24 +0000485 /* cancel any ARP that may not have completed */
486 NetArpWaitPacketIP = 0;
487
Joe Hershbergerf50357b2012-05-23 07:59:15 +0000488 net_cleanup_loop();
wdenk57b2d802003-06-27 21:31:46 +0000489 eth_halt();
Joe Hershberger9f374062012-08-03 10:59:08 +0000490 /* Invalidate the last protocol */
491 eth_set_last_protocol(BOOTP);
492
Luca Ceresoli7cbd7482011-05-11 03:59:56 +0000493 puts("\nAbort\n");
Joe Hershberger05a377b2012-05-23 08:01:04 +0000494 /* 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 Glass230467c2011-10-24 18:00:01 +0000497 goto done;
wdenk2d966952002-10-31 22:12:35 +0000498 }
499
wdenke6466f62003-06-05 19:27:42 +0000500 ArpTimeoutCheck();
wdenk2d966952002-10-31 22:12:35 +0000501
502 /*
503 * Check for a timeout, and run the timeout handler
504 * if we have one.
505 */
wdenk5d841732003-08-17 18:55:18 +0000506 if (timeHandler && ((get_timer(0) - timeStart) > timeDelta)) {
wdenk2d966952002-10-31 22:12:35 +0000507 thand_f *x;
508
Jon Loeliger54f35c22007-07-09 17:45:14 -0500509#if defined(CONFIG_MII) || defined(CONFIG_CMD_MII)
Luca Ceresoli7cbd7482011-05-11 03:59:56 +0000510#if defined(CONFIG_SYS_FAULT_ECHO_LINK_DOWN) && \
511 defined(CONFIG_STATUS_LED) && \
512 defined(STATUS_LED_RED)
wdenk49c3f672003-10-08 22:33:00 +0000513 /*
wdenk9c53f402003-10-15 23:53:47 +0000514 * Echo the inverted link state to the fault LED.
wdenk49c3f672003-10-08 22:33:00 +0000515 */
Luca Ceresoli7cbd7482011-05-11 03:59:56 +0000516 if (miiphy_link(eth_get_dev()->name,
Luca Ceresoli5c83a962011-05-11 03:59:54 +0000517 CONFIG_SYS_FAULT_MII_ADDR)) {
Luca Ceresoli7cbd7482011-05-11 03:59:56 +0000518 status_led_set(STATUS_LED_RED, STATUS_LED_OFF);
wdenk49c3f672003-10-08 22:33:00 +0000519 } else {
Luca Ceresoli7cbd7482011-05-11 03:59:56 +0000520 status_led_set(STATUS_LED_RED, STATUS_LED_ON);
wdenk49c3f672003-10-08 22:33:00 +0000521 }
Luca Ceresoli7cbd7482011-05-11 03:59:56 +0000522#endif /* CONFIG_SYS_FAULT_ECHO_LINK_DOWN, ... */
wdenk49c3f672003-10-08 22:33:00 +0000523#endif /* CONFIG_MII, ... */
Joe Hershberger05a377b2012-05-23 08:01:04 +0000524 debug_cond(DEBUG_INT_STATE, "--- NetLoop timeout\n");
wdenk2d966952002-10-31 22:12:35 +0000525 x = timeHandler;
526 timeHandler = (thand_f *)0;
527 (*x)();
528 }
529
Joe Hershbergere44a0ea2015-03-22 17:09:07 -0500530 if (net_state == NETLOOP_FAIL)
531 NetStartAgain();
wdenk2d966952002-10-31 22:12:35 +0000532
Joe Hershbergerd4bb76a2012-05-23 07:59:14 +0000533 switch (net_state) {
wdenk2d966952002-10-31 22:12:35 +0000534
535 case NETLOOP_RESTART:
wdenk2d966952002-10-31 22:12:35 +0000536 NetRestarted = 1;
wdenk2d966952002-10-31 22:12:35 +0000537 goto restart;
538
539 case NETLOOP_SUCCESS:
Joe Hershbergerf50357b2012-05-23 07:59:15 +0000540 net_cleanup_loop();
wdenk2d966952002-10-31 22:12:35 +0000541 if (NetBootFileXferSize > 0) {
wdenk2d966952002-10-31 22:12:35 +0000542 printf("Bytes transferred = %ld (%lx hex)\n",
543 NetBootFileXferSize,
544 NetBootFileXferSize);
Simon Glass596295d2013-02-24 17:33:24 +0000545 setenv_hex("filesize", NetBootFileXferSize);
546 setenv_hex("fileaddr", load_addr);
wdenk2d966952002-10-31 22:12:35 +0000547 }
Joe Hershberger9f374062012-08-03 10:59:08 +0000548 if (protocol != NETCONS)
549 eth_halt();
550 else
551 eth_halt_state_only();
552
553 eth_set_last_protocol(protocol);
554
Simon Glass230467c2011-10-24 18:00:01 +0000555 ret = NetBootFileXferSize;
Joe Hershberger05a377b2012-05-23 08:01:04 +0000556 debug_cond(DEBUG_INT_STATE, "--- NetLoop Success!\n");
Simon Glass230467c2011-10-24 18:00:01 +0000557 goto done;
wdenk2d966952002-10-31 22:12:35 +0000558
559 case NETLOOP_FAIL:
Joe Hershbergerf50357b2012-05-23 07:59:15 +0000560 net_cleanup_loop();
Joe Hershberger9f374062012-08-03 10:59:08 +0000561 /* Invalidate the last protocol */
562 eth_set_last_protocol(BOOTP);
Joe Hershberger05a377b2012-05-23 08:01:04 +0000563 debug_cond(DEBUG_INT_STATE, "--- NetLoop Fail!\n");
Simon Glass230467c2011-10-24 18:00:01 +0000564 goto done;
Joe Hershbergerd4bb76a2012-05-23 07:59:14 +0000565
566 case NETLOOP_CONTINUE:
567 continue;
wdenk2d966952002-10-31 22:12:35 +0000568 }
569 }
Simon Glass230467c2011-10-24 18:00:01 +0000570
571done:
Jim Lin6c8921f2013-08-13 19:03:05 +0800572#ifdef CONFIG_USB_KEYBOARD
573 net_busy_flag = 0;
574#endif
Simon Glass2928cd82011-10-26 14:18:38 +0000575#ifdef CONFIG_CMD_TFTPPUT
Simon Glass230467c2011-10-24 18:00:01 +0000576 /* Clear out the handlers */
Joe Hershbergerf50357b2012-05-23 07:59:15 +0000577 net_set_udp_handler(NULL);
Simon Glass230467c2011-10-24 18:00:01 +0000578 net_set_icmp_handler(NULL);
Simon Glass2928cd82011-10-26 14:18:38 +0000579#endif
Simon Glass230467c2011-10-24 18:00:01 +0000580 return ret;
wdenk2d966952002-10-31 22:12:35 +0000581}
582
583/**********************************************************************/
584
585static void
586startAgainTimeout(void)
587{
Joe Hershbergerd4bb76a2012-05-23 07:59:14 +0000588 net_set_state(NETLOOP_RESTART);
wdenk2d966952002-10-31 22:12:35 +0000589}
590
Luca Ceresoli7cbd7482011-05-11 03:59:56 +0000591void NetStartAgain(void)
wdenk2d966952002-10-31 22:12:35 +0000592{
wdenk05939202004-04-18 17:39:38 +0000593 char *nretry;
Remy Bohmer0ddb8e82009-10-28 22:13:39 +0100594 int retry_forever = 0;
595 unsigned long retrycnt = 0;
wdenk145d2c12004-04-15 21:48:45 +0000596
Remy Bohmer0ddb8e82009-10-28 22:13:39 +0100597 nretry = getenv("netretry");
598 if (nretry) {
599 if (!strcmp(nretry, "yes"))
600 retry_forever = 1;
601 else if (!strcmp(nretry, "no"))
602 retrycnt = 0;
603 else if (!strcmp(nretry, "once"))
604 retrycnt = 1;
605 else
606 retrycnt = simple_strtoul(nretry, NULL, 0);
Joe Hershbergere44a0ea2015-03-22 17:09:07 -0500607 } else {
608 retrycnt = 0;
609 retry_forever = 0;
610 }
Remy Bohmer0ddb8e82009-10-28 22:13:39 +0100611
612 if ((!retry_forever) && (NetTryCount >= retrycnt)) {
613 eth_halt();
Joe Hershbergerd4bb76a2012-05-23 07:59:14 +0000614 net_set_state(NETLOOP_FAIL);
wdenk145d2c12004-04-15 21:48:45 +0000615 return;
616 }
Remy Bohmer0ddb8e82009-10-28 22:13:39 +0100617
618 NetTryCount++;
619
Luca Ceresoli7cbd7482011-05-11 03:59:56 +0000620 eth_halt();
Matthias Fuchsa02d62b2007-12-27 16:58:41 +0100621#if !defined(CONFIG_NET_DO_NOT_TRY_ANOTHER)
Luca Ceresoli7cbd7482011-05-11 03:59:56 +0000622 eth_try_another(!NetRestarted);
Matthias Fuchsa02d62b2007-12-27 16:58:41 +0100623#endif
Joe Hershberger3dbe17e2015-03-22 17:09:06 -0500624 eth_init();
wdenk05939202004-04-18 17:39:38 +0000625 if (NetRestartWrap) {
wdenk2d966952002-10-31 22:12:35 +0000626 NetRestartWrap = 0;
Remy Bohmer0ddb8e82009-10-28 22:13:39 +0100627 if (NetDevExists) {
Luca Ceresoli7cbd7482011-05-11 03:59:56 +0000628 NetSetTimeout(10000UL, startAgainTimeout);
Joe Hershbergerf50357b2012-05-23 07:59:15 +0000629 net_set_udp_handler(NULL);
wdenk05939202004-04-18 17:39:38 +0000630 } else {
Joe Hershbergerd4bb76a2012-05-23 07:59:14 +0000631 net_set_state(NETLOOP_FAIL);
wdenk2d966952002-10-31 22:12:35 +0000632 }
wdenk05939202004-04-18 17:39:38 +0000633 } else {
Joe Hershbergerd4bb76a2012-05-23 07:59:14 +0000634 net_set_state(NETLOOP_RESTART);
wdenk2d966952002-10-31 22:12:35 +0000635 }
wdenk2d966952002-10-31 22:12:35 +0000636}
637
638/**********************************************************************/
639/*
640 * Miscelaneous bits.
641 */
642
Joe Hershbergerf50357b2012-05-23 07:59:15 +0000643static void dummy_handler(uchar *pkt, unsigned dport,
644 IPaddr_t sip, unsigned sport,
645 unsigned len)
Joe Hershbergeraae05082012-05-23 07:58:01 +0000646{
Joe Hershbergeraae05082012-05-23 07:58:01 +0000647}
648
Joe Hershbergerf50357b2012-05-23 07:59:15 +0000649rxhand_f *net_get_udp_handler(void)
650{
651 return udp_packet_handler;
652}
Joe Hershbergeraae05082012-05-23 07:58:01 +0000653
Joe Hershbergerf50357b2012-05-23 07:59:15 +0000654void net_set_udp_handler(rxhand_f *f)
655{
Joe Hershberger05a377b2012-05-23 08:01:04 +0000656 debug_cond(DEBUG_INT_STATE, "--- NetLoop UDP handler set (%p)\n", f);
Joe Hershbergerf50357b2012-05-23 07:59:15 +0000657 if (f == NULL)
658 udp_packet_handler = dummy_handler;
659 else
660 udp_packet_handler = f;
661}
662
663rxhand_f *net_get_arp_handler(void)
wdenk2d966952002-10-31 22:12:35 +0000664{
Joe Hershbergerf50357b2012-05-23 07:59:15 +0000665 return arp_packet_handler;
wdenk2d966952002-10-31 22:12:35 +0000666}
667
Joe Hershbergerf50357b2012-05-23 07:59:15 +0000668void net_set_arp_handler(rxhand_f *f)
669{
Joe Hershberger05a377b2012-05-23 08:01:04 +0000670 debug_cond(DEBUG_INT_STATE, "--- NetLoop ARP handler set (%p)\n", f);
Joe Hershbergerf50357b2012-05-23 07:59:15 +0000671 if (f == NULL)
672 arp_packet_handler = dummy_handler;
673 else
674 arp_packet_handler = f;
675}
676
Simon Glass2928cd82011-10-26 14:18:38 +0000677#ifdef CONFIG_CMD_TFTPPUT
Simon Glass230467c2011-10-24 18:00:01 +0000678void net_set_icmp_handler(rxhand_icmp_f *f)
679{
680 packet_icmp_handler = f;
681}
Simon Glass2928cd82011-10-26 14:18:38 +0000682#endif
wdenk2d966952002-10-31 22:12:35 +0000683
684void
Luca Ceresolif27d91f2011-05-04 02:40:44 +0000685NetSetTimeout(ulong iv, thand_f *f)
wdenk2d966952002-10-31 22:12:35 +0000686{
687 if (iv == 0) {
Joe Hershberger05a377b2012-05-23 08:01:04 +0000688 debug_cond(DEBUG_INT_STATE,
689 "--- NetLoop timeout handler cancelled\n");
wdenk2d966952002-10-31 22:12:35 +0000690 timeHandler = (thand_f *)0;
691 } else {
Joe Hershberger05a377b2012-05-23 08:01:04 +0000692 debug_cond(DEBUG_INT_STATE,
693 "--- NetLoop timeout handler set (%p)\n", f);
wdenk2d966952002-10-31 22:12:35 +0000694 timeHandler = f;
wdenk5d841732003-08-17 18:55:18 +0000695 timeStart = get_timer(0);
Tetsuyuki Kobayashibf176182012-06-25 02:37:27 +0000696 timeDelta = iv * CONFIG_SYS_HZ / 1000;
wdenk2d966952002-10-31 22:12:35 +0000697 }
698}
699
Joe Hershberger1a6b8d82012-05-23 07:58:10 +0000700int NetSendUDPPacket(uchar *ether, IPaddr_t dest, int dport, int sport,
701 int payload_len)
wdenke6466f62003-06-05 19:27:42 +0000702{
wdenk145d2c12004-04-15 21:48:45 +0000703 uchar *pkt;
Joe Hershberger16be9cb2012-05-23 07:59:08 +0000704 int eth_hdr_size;
705 int pkt_hdr_size;
wdenk145d2c12004-04-15 21:48:45 +0000706
Joe Hershberger017e5c42012-05-23 07:59:22 +0000707 /* make sure the NetTxPacket is initialized (NetInit() was called) */
708 assert(NetTxPacket != NULL);
709 if (NetTxPacket == NULL)
710 return -1;
711
wdenke6466f62003-06-05 19:27:42 +0000712 /* convert to new style broadcast */
713 if (dest == 0)
714 dest = 0xFFFFFFFF;
715
716 /* if broadcast, make the ether address a broadcast and don't do ARP */
717 if (dest == 0xFFFFFFFF)
718 ether = NetBcastAddr;
719
Joe Hershbergerde8205a2012-05-23 07:59:24 +0000720 pkt = (uchar *)NetTxPacket;
Joe Hershberger16be9cb2012-05-23 07:59:08 +0000721
722 eth_hdr_size = NetSetEther(pkt, ether, PROT_IP);
723 pkt += eth_hdr_size;
724 net_set_udp_header(pkt, dest, dport, sport, payload_len);
725 pkt_hdr_size = eth_hdr_size + IP_UDP_HDR_SIZE;
wdenke6466f62003-06-05 19:27:42 +0000726
Joe Hershbergerde8205a2012-05-23 07:59:24 +0000727 /* if MAC address was not discovered yet, do an ARP request */
728 if (memcmp(ether, NetEtherNullAddr, 6) == 0) {
Joe Hershberger05a377b2012-05-23 08:01:04 +0000729 debug_cond(DEBUG_DEV_PKT, "sending ARP for %pI4\n", &dest);
Robin Getz9e0a4d62009-07-23 03:01:03 -0400730
Joe Hershberger16be9cb2012-05-23 07:59:08 +0000731 /* save the ip and eth addr for the packet to send after arp */
wdenke6466f62003-06-05 19:27:42 +0000732 NetArpWaitPacketIP = dest;
733 NetArpWaitPacketMAC = ether;
wdenk145d2c12004-04-15 21:48:45 +0000734
wdenke6466f62003-06-05 19:27:42 +0000735 /* size of the waiting packet */
Joe Hershberger16be9cb2012-05-23 07:59:08 +0000736 NetArpWaitTxPacketSize = pkt_hdr_size + payload_len;
wdenke6466f62003-06-05 19:27:42 +0000737
738 /* and do the ARP request */
739 NetArpWaitTry = 1;
740 NetArpWaitTimerStart = get_timer(0);
741 ArpRequest();
742 return 1; /* waiting */
Joe Hershberger16be9cb2012-05-23 07:59:08 +0000743 } else {
Joe Hershberger05a377b2012-05-23 08:01:04 +0000744 debug_cond(DEBUG_DEV_PKT, "sending UDP to %pI4/%pM\n",
745 &dest, ether);
Joe Hershbergerebe3ca82012-05-23 07:59:13 +0000746 NetSendPacket(NetTxPacket, pkt_hdr_size + payload_len);
Joe Hershberger16be9cb2012-05-23 07:59:08 +0000747 return 0; /* transmitted */
wdenke6466f62003-06-05 19:27:42 +0000748 }
wdenke6466f62003-06-05 19:27:42 +0000749}
wdenk145d2c12004-04-15 21:48:45 +0000750
Alessandro Rubinif119e3d2009-08-07 13:58:56 +0200751#ifdef CONFIG_IP_DEFRAG
752/*
753 * This function collects fragments in a single packet, according
754 * to the algorithm in RFC815. It returns NULL or the pointer to
755 * a complete packet, in static storage
756 */
757#ifndef CONFIG_NET_MAXDEFRAG
758#define CONFIG_NET_MAXDEFRAG 16384
759#endif
760/*
761 * MAXDEFRAG, above, is chosen in the config file and is real data
762 * so we need to add the NFS overhead, which is more than TFTP.
763 * To use sizeof in the internal unnamed structures, we need a real
764 * instance (can't do "sizeof(struct rpc_t.u.reply))", unfortunately).
765 * The compiler doesn't complain nor allocates the actual structure
766 */
767static struct rpc_t rpc_specimen;
768#define IP_PKTSIZE (CONFIG_NET_MAXDEFRAG + sizeof(rpc_specimen.u.reply))
769
Joe Hershbergerc686fa12012-05-23 07:58:05 +0000770#define IP_MAXUDP (IP_PKTSIZE - IP_HDR_SIZE)
Alessandro Rubinif119e3d2009-08-07 13:58:56 +0200771
772/*
773 * this is the packet being assembled, either data or frag control.
774 * Fragments go by 8 bytes, so this union must be 8 bytes long
775 */
776struct hole {
777 /* first_byte is address of this structure */
778 u16 last_byte; /* last byte in this hole + 1 (begin of next hole) */
779 u16 next_hole; /* index of next (in 8-b blocks), 0 == none */
780 u16 prev_hole; /* index of prev, 0 == none */
781 u16 unused;
782};
783
Joe Hershberger6fe8b452012-05-23 07:58:04 +0000784static struct ip_udp_hdr *__NetDefragment(struct ip_udp_hdr *ip, int *lenp)
Alessandro Rubinif119e3d2009-08-07 13:58:56 +0200785{
Joe Hershberger77001b432012-05-15 08:59:08 +0000786 static uchar pkt_buff[IP_PKTSIZE] __aligned(PKTALIGN);
Alessandro Rubinif119e3d2009-08-07 13:58:56 +0200787 static u16 first_hole, total_len;
788 struct hole *payload, *thisfrag, *h, *newh;
Joe Hershberger6fe8b452012-05-23 07:58:04 +0000789 struct ip_udp_hdr *localip = (struct ip_udp_hdr *)pkt_buff;
Alessandro Rubinif119e3d2009-08-07 13:58:56 +0200790 uchar *indata = (uchar *)ip;
791 int offset8, start, len, done = 0;
792 u16 ip_off = ntohs(ip->ip_off);
793
794 /* payload starts after IP header, this fragment is in there */
Joe Hershbergerc686fa12012-05-23 07:58:05 +0000795 payload = (struct hole *)(pkt_buff + IP_HDR_SIZE);
Alessandro Rubinif119e3d2009-08-07 13:58:56 +0200796 offset8 = (ip_off & IP_OFFS);
797 thisfrag = payload + offset8;
798 start = offset8 * 8;
Joe Hershbergerc686fa12012-05-23 07:58:05 +0000799 len = ntohs(ip->ip_len) - IP_HDR_SIZE;
Alessandro Rubinif119e3d2009-08-07 13:58:56 +0200800
801 if (start + len > IP_MAXUDP) /* fragment extends too far */
802 return NULL;
803
804 if (!total_len || localip->ip_id != ip->ip_id) {
805 /* new (or different) packet, reset structs */
806 total_len = 0xffff;
807 payload[0].last_byte = ~0;
808 payload[0].next_hole = 0;
809 payload[0].prev_hole = 0;
810 first_hole = 0;
811 /* any IP header will work, copy the first we received */
Joe Hershbergerc686fa12012-05-23 07:58:05 +0000812 memcpy(localip, ip, IP_HDR_SIZE);
Alessandro Rubinif119e3d2009-08-07 13:58:56 +0200813 }
814
815 /*
816 * What follows is the reassembly algorithm. We use the payload
817 * array as a linked list of hole descriptors, as each hole starts
818 * at a multiple of 8 bytes. However, last byte can be whatever value,
819 * so it is represented as byte count, not as 8-byte blocks.
820 */
821
822 h = payload + first_hole;
823 while (h->last_byte < start) {
824 if (!h->next_hole) {
825 /* no hole that far away */
826 return NULL;
827 }
828 h = payload + h->next_hole;
829 }
830
Fillod Stephanee7ade8b2010-06-11 19:26:43 +0200831 /* last fragment may be 1..7 bytes, the "+7" forces acceptance */
832 if (offset8 + ((len + 7) / 8) <= h - payload) {
Alessandro Rubinif119e3d2009-08-07 13:58:56 +0200833 /* no overlap with holes (dup fragment?) */
834 return NULL;
835 }
836
837 if (!(ip_off & IP_FLAGS_MFRAG)) {
838 /* no more fragmentss: truncate this (last) hole */
839 total_len = start + len;
840 h->last_byte = start + len;
841 }
842
843 /*
844 * There is some overlap: fix the hole list. This code doesn't
845 * deal with a fragment that overlaps with two different holes
846 * (thus being a superset of a previously-received fragment).
847 */
848
Luca Ceresoli7cbd7482011-05-11 03:59:56 +0000849 if ((h >= thisfrag) && (h->last_byte <= start + len)) {
Alessandro Rubinif119e3d2009-08-07 13:58:56 +0200850 /* complete overlap with hole: remove hole */
851 if (!h->prev_hole && !h->next_hole) {
852 /* last remaining hole */
853 done = 1;
854 } else if (!h->prev_hole) {
855 /* first hole */
856 first_hole = h->next_hole;
857 payload[h->next_hole].prev_hole = 0;
858 } else if (!h->next_hole) {
859 /* last hole */
860 payload[h->prev_hole].next_hole = 0;
861 } else {
862 /* in the middle of the list */
863 payload[h->next_hole].prev_hole = h->prev_hole;
864 payload[h->prev_hole].next_hole = h->next_hole;
865 }
866
867 } else if (h->last_byte <= start + len) {
868 /* overlaps with final part of the hole: shorten this hole */
869 h->last_byte = start;
870
871 } else if (h >= thisfrag) {
872 /* overlaps with initial part of the hole: move this hole */
873 newh = thisfrag + (len / 8);
874 *newh = *h;
875 h = newh;
876 if (h->next_hole)
877 payload[h->next_hole].prev_hole = (h - payload);
878 if (h->prev_hole)
879 payload[h->prev_hole].next_hole = (h - payload);
880 else
881 first_hole = (h - payload);
882
883 } else {
884 /* fragment sits in the middle: split the hole */
885 newh = thisfrag + (len / 8);
886 *newh = *h;
887 h->last_byte = start;
888 h->next_hole = (newh - payload);
889 newh->prev_hole = (h - payload);
890 if (newh->next_hole)
891 payload[newh->next_hole].prev_hole = (newh - payload);
892 }
893
894 /* finally copy this fragment and possibly return whole packet */
Joe Hershbergerc686fa12012-05-23 07:58:05 +0000895 memcpy((uchar *)thisfrag, indata + IP_HDR_SIZE, len);
Alessandro Rubinif119e3d2009-08-07 13:58:56 +0200896 if (!done)
897 return NULL;
898
899 localip->ip_len = htons(total_len);
Joe Hershbergerc686fa12012-05-23 07:58:05 +0000900 *lenp = total_len + IP_HDR_SIZE;
Alessandro Rubinif119e3d2009-08-07 13:58:56 +0200901 return localip;
902}
903
Joe Hershberger6fe8b452012-05-23 07:58:04 +0000904static inline struct ip_udp_hdr *NetDefragment(struct ip_udp_hdr *ip, int *lenp)
Alessandro Rubinif119e3d2009-08-07 13:58:56 +0200905{
906 u16 ip_off = ntohs(ip->ip_off);
907 if (!(ip_off & (IP_OFFS | IP_FLAGS_MFRAG)))
908 return ip; /* not a fragment */
909 return __NetDefragment(ip, lenp);
910}
911
912#else /* !CONFIG_IP_DEFRAG */
913
Joe Hershberger6fe8b452012-05-23 07:58:04 +0000914static inline struct ip_udp_hdr *NetDefragment(struct ip_udp_hdr *ip, int *lenp)
Alessandro Rubinif119e3d2009-08-07 13:58:56 +0200915{
916 u16 ip_off = ntohs(ip->ip_off);
917 if (!(ip_off & (IP_OFFS | IP_FLAGS_MFRAG)))
918 return ip; /* not a fragment */
919 return NULL;
920}
921#endif
wdenk2d966952002-10-31 22:12:35 +0000922
Simon Glass43c72962011-10-24 18:00:00 +0000923/**
924 * Receive an ICMP packet. We deal with REDIRECT and PING here, and silently
925 * drop others.
926 *
927 * @parma ip IP packet containing the ICMP
928 */
Joe Hershberger6fe8b452012-05-23 07:58:04 +0000929static void receive_icmp(struct ip_udp_hdr *ip, int len,
Joe Hershberger1178f412012-05-23 07:58:06 +0000930 IPaddr_t src_ip, struct ethernet_hdr *et)
Simon Glass43c72962011-10-24 18:00:00 +0000931{
Joe Hershberger78495612012-05-23 07:58:09 +0000932 struct icmp_hdr *icmph = (struct icmp_hdr *)&ip->udp_src;
Simon Glass43c72962011-10-24 18:00:00 +0000933
934 switch (icmph->type) {
935 case ICMP_REDIRECT:
936 if (icmph->code != ICMP_REDIR_HOST)
937 return;
938 printf(" ICMP Host Redirect to %pI4 ",
939 &icmph->un.gateway);
940 break;
Joe Hershbergerc21bf372012-05-23 07:58:02 +0000941 default:
Simon Glass43c72962011-10-24 18:00:00 +0000942#if defined(CONFIG_CMD_PING)
Joe Hershbergerc21bf372012-05-23 07:58:02 +0000943 ping_receive(et, ip, len);
Simon Glass43c72962011-10-24 18:00:00 +0000944#endif
Simon Glass2928cd82011-10-26 14:18:38 +0000945#ifdef CONFIG_CMD_TFTPPUT
Simon Glass230467c2011-10-24 18:00:01 +0000946 if (packet_icmp_handler)
947 packet_icmp_handler(icmph->type, icmph->code,
948 ntohs(ip->udp_dst), src_ip, ntohs(ip->udp_src),
949 icmph->un.data, ntohs(ip->udp_len));
Simon Glass2928cd82011-10-26 14:18:38 +0000950#endif
Simon Glass43c72962011-10-24 18:00:00 +0000951 break;
952 }
953}
954
wdenk2d966952002-10-31 22:12:35 +0000955void
Joe Hershberger4b7747e2012-05-15 08:59:04 +0000956NetReceive(uchar *inpkt, int len)
wdenk2d966952002-10-31 22:12:35 +0000957{
Joe Hershberger1178f412012-05-23 07:58:06 +0000958 struct ethernet_hdr *et;
Joe Hershberger6fe8b452012-05-23 07:58:04 +0000959 struct ip_udp_hdr *ip;
Joe Hershbergerfacf5352012-05-23 07:58:12 +0000960 IPaddr_t dst_ip;
Luca Ceresoli428ab362011-04-18 06:19:50 +0000961 IPaddr_t src_ip;
Joe Hershbergerfacf5352012-05-23 07:58:12 +0000962 int eth_proto;
Jon Loeliger54f35c22007-07-09 17:45:14 -0500963#if defined(CONFIG_CMD_CDP)
wdenk145d2c12004-04-15 21:48:45 +0000964 int iscdp;
965#endif
966 ushort cti = 0, vlanid = VLAN_NONE, myvlanid, mynvlanid;
wdenk2d966952002-10-31 22:12:35 +0000967
Joe Hershberger05a377b2012-05-23 08:01:04 +0000968 debug_cond(DEBUG_NET_PKT, "packet received\n");
wdenk145d2c12004-04-15 21:48:45 +0000969
Mike Frysingerdc166032009-07-18 21:04:08 -0400970 NetRxPacket = inpkt;
971 NetRxPacketLen = len;
Joe Hershberger1178f412012-05-23 07:58:06 +0000972 et = (struct ethernet_hdr *)inpkt;
wdenk145d2c12004-04-15 21:48:45 +0000973
974 /* too small packet? */
975 if (len < ETHER_HDR_SIZE)
976 return;
Rafal Jaworowski1f2c9a42007-12-27 18:19:02 +0100977
978#ifdef CONFIG_API
979 if (push_packet) {
980 (*push_packet)(inpkt, len);
981 return;
982 }
983#endif
wdenk145d2c12004-04-15 21:48:45 +0000984
Jon Loeliger54f35c22007-07-09 17:45:14 -0500985#if defined(CONFIG_CMD_CDP)
wdenk145d2c12004-04-15 21:48:45 +0000986 /* keep track if packet is CDP */
Joe Hershberger00c62a82012-05-23 07:58:00 +0000987 iscdp = is_cdp_packet(et->et_dest);
wdenk145d2c12004-04-15 21:48:45 +0000988#endif
989
990 myvlanid = ntohs(NetOurVLAN);
991 if (myvlanid == (ushort)-1)
992 myvlanid = VLAN_NONE;
993 mynvlanid = ntohs(NetOurNativeVLAN);
994 if (mynvlanid == (ushort)-1)
995 mynvlanid = VLAN_NONE;
wdenk2d966952002-10-31 22:12:35 +0000996
Joe Hershbergerfacf5352012-05-23 07:58:12 +0000997 eth_proto = ntohs(et->et_protlen);
wdenk2d966952002-10-31 22:12:35 +0000998
Joe Hershbergerfacf5352012-05-23 07:58:12 +0000999 if (eth_proto < 1514) {
Joe Hershberger1178f412012-05-23 07:58:06 +00001000 struct e802_hdr *et802 = (struct e802_hdr *)et;
wdenk2d966952002-10-31 22:12:35 +00001001 /*
Joe Hershbergerc17fa982012-05-23 07:58:11 +00001002 * Got a 802.2 packet. Check the other protocol field.
1003 * XXX VLAN over 802.2+SNAP not implemented!
wdenk2d966952002-10-31 22:12:35 +00001004 */
Joe Hershbergerfacf5352012-05-23 07:58:12 +00001005 eth_proto = ntohs(et802->et_prot);
wdenk145d2c12004-04-15 21:48:45 +00001006
Joe Hershberger6fe8b452012-05-23 07:58:04 +00001007 ip = (struct ip_udp_hdr *)(inpkt + E802_HDR_SIZE);
wdenk2d966952002-10-31 22:12:35 +00001008 len -= E802_HDR_SIZE;
wdenk145d2c12004-04-15 21:48:45 +00001009
Joe Hershbergerfacf5352012-05-23 07:58:12 +00001010 } else if (eth_proto != PROT_VLAN) { /* normal packet */
Joe Hershberger6fe8b452012-05-23 07:58:04 +00001011 ip = (struct ip_udp_hdr *)(inpkt + ETHER_HDR_SIZE);
wdenk2d966952002-10-31 22:12:35 +00001012 len -= ETHER_HDR_SIZE;
wdenk145d2c12004-04-15 21:48:45 +00001013
1014 } else { /* VLAN packet */
Joe Hershbergerb43784c2012-05-23 07:58:07 +00001015 struct vlan_ethernet_hdr *vet =
1016 (struct vlan_ethernet_hdr *)et;
wdenk145d2c12004-04-15 21:48:45 +00001017
Joe Hershberger05a377b2012-05-23 08:01:04 +00001018 debug_cond(DEBUG_NET_PKT, "VLAN packet received\n");
Robin Getz9e0a4d62009-07-23 03:01:03 -04001019
wdenk145d2c12004-04-15 21:48:45 +00001020 /* too small packet? */
1021 if (len < VLAN_ETHER_HDR_SIZE)
1022 return;
1023
1024 /* if no VLAN active */
1025 if ((ntohs(NetOurVLAN) & VLAN_IDMASK) == VLAN_NONE
Jon Loeliger54f35c22007-07-09 17:45:14 -05001026#if defined(CONFIG_CMD_CDP)
wdenk145d2c12004-04-15 21:48:45 +00001027 && iscdp == 0
1028#endif
1029 )
1030 return;
1031
1032 cti = ntohs(vet->vet_tag);
1033 vlanid = cti & VLAN_IDMASK;
Joe Hershbergerfacf5352012-05-23 07:58:12 +00001034 eth_proto = ntohs(vet->vet_type);
wdenk145d2c12004-04-15 21:48:45 +00001035
Joe Hershberger6fe8b452012-05-23 07:58:04 +00001036 ip = (struct ip_udp_hdr *)(inpkt + VLAN_ETHER_HDR_SIZE);
wdenk145d2c12004-04-15 21:48:45 +00001037 len -= VLAN_ETHER_HDR_SIZE;
wdenk2d966952002-10-31 22:12:35 +00001038 }
1039
Joe Hershberger05a377b2012-05-23 08:01:04 +00001040 debug_cond(DEBUG_NET_PKT, "Receive from protocol 0x%x\n", eth_proto);
wdenk2d966952002-10-31 22:12:35 +00001041
Jon Loeliger54f35c22007-07-09 17:45:14 -05001042#if defined(CONFIG_CMD_CDP)
wdenk145d2c12004-04-15 21:48:45 +00001043 if (iscdp) {
Joe Hershbergerd01a7a02012-05-23 07:58:13 +00001044 cdp_receive((uchar *)ip, len);
wdenk145d2c12004-04-15 21:48:45 +00001045 return;
1046 }
1047#endif
1048
1049 if ((myvlanid & VLAN_IDMASK) != VLAN_NONE) {
1050 if (vlanid == VLAN_NONE)
1051 vlanid = (mynvlanid & VLAN_IDMASK);
1052 /* not matched? */
1053 if (vlanid != (myvlanid & VLAN_IDMASK))
1054 return;
1055 }
1056
Joe Hershbergerfacf5352012-05-23 07:58:12 +00001057 switch (eth_proto) {
wdenk2d966952002-10-31 22:12:35 +00001058
1059 case PROT_ARP:
Joe Hershbergeraae05082012-05-23 07:58:01 +00001060 ArpReceive(et, ip, len);
wdenkcb99da52005-01-12 00:15:14 +00001061 break;
wdenk2d966952002-10-31 22:12:35 +00001062
Peter Tyserf7a48ca2010-09-30 11:25:48 -05001063#ifdef CONFIG_CMD_RARP
wdenk2d966952002-10-31 22:12:35 +00001064 case PROT_RARP:
Joe Hershberger61b4de62012-05-23 07:58:03 +00001065 rarp_receive(ip, len);
wdenk2d966952002-10-31 22:12:35 +00001066 break;
Peter Tyserf7a48ca2010-09-30 11:25:48 -05001067#endif
wdenk2d966952002-10-31 22:12:35 +00001068 case PROT_IP:
Joe Hershberger05a377b2012-05-23 08:01:04 +00001069 debug_cond(DEBUG_NET_PKT, "Got IP\n");
Alessandro Rubinif119e3d2009-08-07 13:58:56 +02001070 /* Before we start poking the header, make sure it is there */
Joe Hershberger6fe8b452012-05-23 07:58:04 +00001071 if (len < IP_UDP_HDR_SIZE) {
1072 debug("len bad %d < %lu\n", len,
1073 (ulong)IP_UDP_HDR_SIZE);
wdenk2d966952002-10-31 22:12:35 +00001074 return;
1075 }
Alessandro Rubinif119e3d2009-08-07 13:58:56 +02001076 /* Check the packet length */
wdenk2d966952002-10-31 22:12:35 +00001077 if (len < ntohs(ip->ip_len)) {
Joe Hershberger05a377b2012-05-23 08:01:04 +00001078 debug("len bad %d < %d\n", len, ntohs(ip->ip_len));
wdenk2d966952002-10-31 22:12:35 +00001079 return;
1080 }
1081 len = ntohs(ip->ip_len);
Joe Hershberger05a377b2012-05-23 08:01:04 +00001082 debug_cond(DEBUG_NET_PKT, "len=%d, v=%02x\n",
1083 len, ip->ip_hl_v & 0xff);
Robin Getz9e0a4d62009-07-23 03:01:03 -04001084
Alessandro Rubinif119e3d2009-08-07 13:58:56 +02001085 /* Can't deal with anything except IPv4 */
Luca Ceresolie8ccbb02011-05-04 02:40:43 +00001086 if ((ip->ip_hl_v & 0xf0) != 0x40)
wdenk2d966952002-10-31 22:12:35 +00001087 return;
Alessandro Rubinif119e3d2009-08-07 13:58:56 +02001088 /* Can't deal with IP options (headers != 20 bytes) */
Luca Ceresolie8ccbb02011-05-04 02:40:43 +00001089 if ((ip->ip_hl_v & 0x0f) > 0x05)
Remy Bohmerb9535782008-06-03 15:48:17 +02001090 return;
Alessandro Rubinif119e3d2009-08-07 13:58:56 +02001091 /* Check the Checksum of the header */
Simon Glassdfcdcee2015-01-19 22:16:08 -07001092 if (!ip_checksum_ok((uchar *)ip, IP_HDR_SIZE)) {
Joe Hershberger05a377b2012-05-23 08:01:04 +00001093 debug("checksum bad\n");
wdenk2d966952002-10-31 22:12:35 +00001094 return;
1095 }
Alessandro Rubinif119e3d2009-08-07 13:58:56 +02001096 /* If it is not for us, ignore it */
Joe Hershbergerfacf5352012-05-23 07:58:12 +00001097 dst_ip = NetReadIP(&ip->ip_dst);
1098 if (NetOurIP && dst_ip != NetOurIP && dst_ip != 0xFFFFFFFF) {
David Updegraff7280da72007-06-11 10:41:07 -05001099#ifdef CONFIG_MCAST_TFTP
Joe Hershbergerfacf5352012-05-23 07:58:12 +00001100 if (Mcast_addr != dst_ip)
David Updegraff7280da72007-06-11 10:41:07 -05001101#endif
Luca Ceresolib19d0b72011-05-04 02:40:46 +00001102 return;
wdenk2d966952002-10-31 22:12:35 +00001103 }
Luca Ceresoli428ab362011-04-18 06:19:50 +00001104 /* Read source IP address for later use */
1105 src_ip = NetReadIP(&ip->ip_src);
wdenk2d966952002-10-31 22:12:35 +00001106 /*
Alessandro Rubinif119e3d2009-08-07 13:58:56 +02001107 * The function returns the unchanged packet if it's not
1108 * a fragment, and either the complete packet or NULL if
1109 * it is a fragment (if !CONFIG_IP_DEFRAG, it returns NULL)
1110 */
Luca Ceresolidb210822011-05-04 02:40:47 +00001111 ip = NetDefragment(ip, &len);
1112 if (!ip)
Alessandro Rubinif119e3d2009-08-07 13:58:56 +02001113 return;
1114 /*
wdenk2d966952002-10-31 22:12:35 +00001115 * watch for ICMP host redirects
1116 *
wdenk57b2d802003-06-27 21:31:46 +00001117 * There is no real handler code (yet). We just watch
1118 * for ICMP host redirect messages. In case anybody
1119 * sees these messages: please contact me
1120 * (wd@denx.de), or - even better - send me the
1121 * necessary fixes :-)
wdenk2d966952002-10-31 22:12:35 +00001122 *
wdenk57b2d802003-06-27 21:31:46 +00001123 * Note: in all cases where I have seen this so far
1124 * it was a problem with the router configuration,
1125 * for instance when a router was configured in the
1126 * BOOTP reply, but the TFTP server was on the same
1127 * subnet. So this is probably a warning that your
1128 * configuration might be wrong. But I'm not really
1129 * sure if there aren't any other situations.
Simon Glass230467c2011-10-24 18:00:01 +00001130 *
1131 * Simon Glass <sjg@chromium.org>: We get an ICMP when
1132 * we send a tftp packet to a dead connection, or when
1133 * there is no server at the other end.
wdenk2d966952002-10-31 22:12:35 +00001134 */
1135 if (ip->ip_p == IPPROTO_ICMP) {
Simon Glass43c72962011-10-24 18:00:00 +00001136 receive_icmp(ip, len, src_ip, et);
1137 return;
wdenk2d966952002-10-31 22:12:35 +00001138 } else if (ip->ip_p != IPPROTO_UDP) { /* Only UDP packets */
1139 return;
1140 }
1141
Joe Hershberger05a377b2012-05-23 08:01:04 +00001142 debug_cond(DEBUG_DEV_PKT,
1143 "received UDP (to=%pI4, from=%pI4, len=%d)\n",
1144 &dst_ip, &src_ip, len);
1145
Stefan Roesedfced812005-08-12 20:06:52 +02001146#ifdef CONFIG_UDP_CHECKSUM
1147 if (ip->udp_xsum != 0) {
Wolfgang Denk30b87322005-08-12 23:43:12 +02001148 ulong xsum;
Stefan Roesedfced812005-08-12 20:06:52 +02001149 ushort *sumptr;
1150 ushort sumlen;
1151
1152 xsum = ip->ip_p;
1153 xsum += (ntohs(ip->udp_len));
1154 xsum += (ntohl(ip->ip_src) >> 16) & 0x0000ffff;
1155 xsum += (ntohl(ip->ip_src) >> 0) & 0x0000ffff;
1156 xsum += (ntohl(ip->ip_dst) >> 16) & 0x0000ffff;
1157 xsum += (ntohl(ip->ip_dst) >> 0) & 0x0000ffff;
1158
1159 sumlen = ntohs(ip->udp_len);
1160 sumptr = (ushort *) &(ip->udp_src);
1161
1162 while (sumlen > 1) {
Wolfgang Denk30b87322005-08-12 23:43:12 +02001163 ushort sumdata;
Stefan Roesedfced812005-08-12 20:06:52 +02001164
1165 sumdata = *sumptr++;
1166 xsum += ntohs(sumdata);
1167 sumlen -= 2;
1168 }
1169 if (sumlen > 0) {
Wolfgang Denk30b87322005-08-12 23:43:12 +02001170 ushort sumdata;
Stefan Roesedfced812005-08-12 20:06:52 +02001171
1172 sumdata = *(unsigned char *) sumptr;
Wolfgang Denk30b87322005-08-12 23:43:12 +02001173 sumdata = (sumdata << 8) & 0xff00;
Stefan Roesedfced812005-08-12 20:06:52 +02001174 xsum += sumdata;
1175 }
1176 while ((xsum >> 16) != 0) {
Luca Ceresoli5c83a962011-05-11 03:59:54 +00001177 xsum = (xsum & 0x0000ffff) +
1178 ((xsum >> 16) & 0x0000ffff);
Stefan Roesedfced812005-08-12 20:06:52 +02001179 }
1180 if ((xsum != 0x00000000) && (xsum != 0x0000ffff)) {
Wolfgang Denk12cec0a2008-07-11 01:16:00 +02001181 printf(" UDP wrong checksum %08lx %08x\n",
1182 xsum, ntohs(ip->udp_xsum));
Stefan Roesedfced812005-08-12 20:06:52 +02001183 return;
1184 }
1185 }
1186#endif
1187
David Updegraff7280da72007-06-11 10:41:07 -05001188
Hannes Petermaierea070422014-06-13 06:25:29 +02001189#if defined (CONFIG_NETCONSOLE) && !(CONFIG_SPL_BUILD)
Joe Hershberger6fe8b452012-05-23 07:58:04 +00001190 nc_input_packet((uchar *)ip + IP_UDP_HDR_SIZE,
Joe Hershberger19d6bc02012-10-03 09:14:03 +00001191 src_ip,
Joe Hershberger6fe8b452012-05-23 07:58:04 +00001192 ntohs(ip->udp_dst),
1193 ntohs(ip->udp_src),
1194 ntohs(ip->udp_len) - UDP_HDR_SIZE);
wdenkb8fb6192004-08-02 21:11:11 +00001195#endif
wdenk2d966952002-10-31 22:12:35 +00001196 /*
1197 * IP header OK. Pass the packet to the current handler.
1198 */
Joe Hershbergerf50357b2012-05-23 07:59:15 +00001199 (*udp_packet_handler)((uchar *)ip + IP_UDP_HDR_SIZE,
1200 ntohs(ip->udp_dst),
1201 src_ip,
1202 ntohs(ip->udp_src),
1203 ntohs(ip->udp_len) - UDP_HDR_SIZE);
wdenk2d966952002-10-31 22:12:35 +00001204 break;
1205 }
1206}
1207
1208
1209/**********************************************************************/
1210
Simon Glassd6c5f552011-10-24 18:00:02 +00001211static int net_check_prereq(enum proto_t protocol)
wdenk2d966952002-10-31 22:12:35 +00001212{
1213 switch (protocol) {
wdenk05939202004-04-18 17:39:38 +00001214 /* Fall through */
Jon Loeliger54f35c22007-07-09 17:45:14 -05001215#if defined(CONFIG_CMD_PING)
wdenke6466f62003-06-05 19:27:42 +00001216 case PING:
wdenk05939202004-04-18 17:39:38 +00001217 if (NetPingIP == 0) {
Luca Ceresoli7cbd7482011-05-11 03:59:56 +00001218 puts("*** ERROR: ping address not given\n");
Luca Ceresoli5fe6de22011-05-04 02:40:45 +00001219 return 1;
wdenk05939202004-04-18 17:39:38 +00001220 }
1221 goto common;
wdenke6466f62003-06-05 19:27:42 +00001222#endif
Jon Loeliger54f35c22007-07-09 17:45:14 -05001223#if defined(CONFIG_CMD_SNTP)
wdenkb4ad9622005-04-01 00:25:43 +00001224 case SNTP:
1225 if (NetNtpServerIP == 0) {
Luca Ceresoli7cbd7482011-05-11 03:59:56 +00001226 puts("*** ERROR: NTP server address not given\n");
Luca Ceresoli5fe6de22011-05-04 02:40:45 +00001227 return 1;
wdenkb4ad9622005-04-01 00:25:43 +00001228 }
1229 goto common;
1230#endif
Robin Getz82f0d232009-07-20 14:53:54 -04001231#if defined(CONFIG_CMD_DNS)
1232 case DNS:
1233 if (NetOurDNSIP == 0) {
1234 puts("*** ERROR: DNS server address not given\n");
1235 return 1;
1236 }
1237 goto common;
1238#endif
Jon Loeliger54f35c22007-07-09 17:45:14 -05001239#if defined(CONFIG_CMD_NFS)
wdenkbe9c1cb2004-02-24 02:00:03 +00001240 case NFS:
1241#endif
Simon Glassd6c5f552011-10-24 18:00:02 +00001242 case TFTPGET:
Simon Glass6a398d22011-10-24 18:00:07 +00001243 case TFTPPUT:
wdenk05939202004-04-18 17:39:38 +00001244 if (NetServerIP == 0) {
Luca Ceresoli7cbd7482011-05-11 03:59:56 +00001245 puts("*** ERROR: `serverip' not set\n");
Luca Ceresoli5fe6de22011-05-04 02:40:45 +00001246 return 1;
wdenk05939202004-04-18 17:39:38 +00001247 }
Luca Ceresoli7cbd7482011-05-11 03:59:56 +00001248#if defined(CONFIG_CMD_PING) || defined(CONFIG_CMD_SNTP) || \
1249 defined(CONFIG_CMD_DNS)
1250common:
wdenke6466f62003-06-05 19:27:42 +00001251#endif
Simon Guinot00dceba2011-05-01 23:38:40 +00001252 /* Fall through */
wdenke6466f62003-06-05 19:27:42 +00001253
Simon Guinot00dceba2011-05-01 23:38:40 +00001254 case NETCONS:
Luca Ceresoli7aa81a42011-05-17 00:03:40 +00001255 case TFTPSRV:
wdenk05939202004-04-18 17:39:38 +00001256 if (NetOurIP == 0) {
Luca Ceresoli7cbd7482011-05-11 03:59:56 +00001257 puts("*** ERROR: `ipaddr' not set\n");
Luca Ceresoli5fe6de22011-05-04 02:40:45 +00001258 return 1;
wdenk05939202004-04-18 17:39:38 +00001259 }
1260 /* Fall through */
wdenk2d966952002-10-31 22:12:35 +00001261
Peter Tyserf7a48ca2010-09-30 11:25:48 -05001262#ifdef CONFIG_CMD_RARP
wdenk2d966952002-10-31 22:12:35 +00001263 case RARP:
Peter Tyserf7a48ca2010-09-30 11:25:48 -05001264#endif
wdenk2d966952002-10-31 22:12:35 +00001265 case BOOTP:
wdenk145d2c12004-04-15 21:48:45 +00001266 case CDP:
Peter Tyserf7a48ca2010-09-30 11:25:48 -05001267 case DHCP:
Joe Hershbergerb35a3a62012-05-23 08:00:12 +00001268 case LINKLOCAL:
Luca Ceresoli7cbd7482011-05-11 03:59:56 +00001269 if (memcmp(NetOurEther, "\0\0\0\0\0\0", 6) == 0) {
Luca Ceresoli7cbd7482011-05-11 03:59:56 +00001270 int num = eth_get_dev_index();
wdenk2d966952002-10-31 22:12:35 +00001271
wdenk05939202004-04-18 17:39:38 +00001272 switch (num) {
1273 case -1:
Luca Ceresoli7cbd7482011-05-11 03:59:56 +00001274 puts("*** ERROR: No ethernet found.\n");
Luca Ceresoli5fe6de22011-05-04 02:40:45 +00001275 return 1;
wdenk05939202004-04-18 17:39:38 +00001276 case 0:
Luca Ceresoli7cbd7482011-05-11 03:59:56 +00001277 puts("*** ERROR: `ethaddr' not set\n");
wdenk2d966952002-10-31 22:12:35 +00001278 break;
wdenk05939202004-04-18 17:39:38 +00001279 default:
Luca Ceresoli7cbd7482011-05-11 03:59:56 +00001280 printf("*** ERROR: `eth%daddr' not set\n",
wdenk2d966952002-10-31 22:12:35 +00001281 num);
1282 break;
wdenk05939202004-04-18 17:39:38 +00001283 }
wdenk2d966952002-10-31 22:12:35 +00001284
Luca Ceresoli7cbd7482011-05-11 03:59:56 +00001285 NetStartAgain();
Luca Ceresoli5fe6de22011-05-04 02:40:45 +00001286 return 2;
wdenk05939202004-04-18 17:39:38 +00001287 }
1288 /* Fall through */
1289 default:
Luca Ceresoli5fe6de22011-05-04 02:40:45 +00001290 return 0;
wdenk2d966952002-10-31 22:12:35 +00001291 }
Luca Ceresoli5fe6de22011-05-04 02:40:45 +00001292 return 0; /* OK */
wdenk2d966952002-10-31 22:12:35 +00001293}
1294/**********************************************************************/
1295
1296int
wdenk145d2c12004-04-15 21:48:45 +00001297NetEthHdrSize(void)
1298{
1299 ushort myvlanid;
1300
1301 myvlanid = ntohs(NetOurVLAN);
1302 if (myvlanid == (ushort)-1)
1303 myvlanid = VLAN_NONE;
wdenk2d966952002-10-31 22:12:35 +00001304
Luca Ceresoli5c83a962011-05-11 03:59:54 +00001305 return ((myvlanid & VLAN_IDMASK) == VLAN_NONE) ? ETHER_HDR_SIZE :
1306 VLAN_ETHER_HDR_SIZE;
wdenk145d2c12004-04-15 21:48:45 +00001307}
1308
1309int
Joe Hershberger4b7747e2012-05-15 08:59:04 +00001310NetSetEther(uchar *xet, uchar * addr, uint prot)
wdenk2d966952002-10-31 22:12:35 +00001311{
Joe Hershberger1178f412012-05-23 07:58:06 +00001312 struct ethernet_hdr *et = (struct ethernet_hdr *)xet;
wdenk145d2c12004-04-15 21:48:45 +00001313 ushort myvlanid;
1314
1315 myvlanid = ntohs(NetOurVLAN);
1316 if (myvlanid == (ushort)-1)
1317 myvlanid = VLAN_NONE;
wdenk2d966952002-10-31 22:12:35 +00001318
Luca Ceresoli7cbd7482011-05-11 03:59:56 +00001319 memcpy(et->et_dest, addr, 6);
1320 memcpy(et->et_src, NetOurEther, 6);
wdenk145d2c12004-04-15 21:48:45 +00001321 if ((myvlanid & VLAN_IDMASK) == VLAN_NONE) {
Luca Ceresolib19d0b72011-05-04 02:40:46 +00001322 et->et_protlen = htons(prot);
wdenk145d2c12004-04-15 21:48:45 +00001323 return ETHER_HDR_SIZE;
1324 } else {
Joe Hershbergerb43784c2012-05-23 07:58:07 +00001325 struct vlan_ethernet_hdr *vet =
1326 (struct vlan_ethernet_hdr *)xet;
wdenk2d966952002-10-31 22:12:35 +00001327
wdenk145d2c12004-04-15 21:48:45 +00001328 vet->vet_vlan_type = htons(PROT_VLAN);
1329 vet->vet_tag = htons((0 << 5) | (myvlanid & VLAN_IDMASK));
1330 vet->vet_type = htons(prot);
1331 return VLAN_ETHER_HDR_SIZE;
1332 }
1333}
wdenk2d966952002-10-31 22:12:35 +00001334
Joe Hershberger530cd6b2012-05-23 07:59:16 +00001335int net_update_ether(struct ethernet_hdr *et, uchar *addr, uint prot)
1336{
1337 ushort protlen;
1338
1339 memcpy(et->et_dest, addr, 6);
1340 memcpy(et->et_src, NetOurEther, 6);
1341 protlen = ntohs(et->et_protlen);
1342 if (protlen == PROT_VLAN) {
1343 struct vlan_ethernet_hdr *vet =
1344 (struct vlan_ethernet_hdr *)et;
1345 vet->vet_type = htons(prot);
1346 return VLAN_ETHER_HDR_SIZE;
1347 } else if (protlen > 1514) {
1348 et->et_protlen = htons(prot);
1349 return ETHER_HDR_SIZE;
1350 } else {
1351 /* 802.2 + SNAP */
1352 struct e802_hdr *et802 = (struct e802_hdr *)et;
1353 et802->et_prot = htons(prot);
1354 return E802_HDR_SIZE;
1355 }
1356}
1357
Joe Hershberger2ed5b492012-05-23 07:59:07 +00001358void net_set_ip_header(uchar *pkt, IPaddr_t dest, IPaddr_t source)
wdenk2d966952002-10-31 22:12:35 +00001359{
Joe Hershberger2ed5b492012-05-23 07:59:07 +00001360 struct ip_udp_hdr *ip = (struct ip_udp_hdr *)pkt;
wdenk2d966952002-10-31 22:12:35 +00001361
1362 /*
Joe Hershberger2ed5b492012-05-23 07:59:07 +00001363 * Construct an IP header.
wdenk2d966952002-10-31 22:12:35 +00001364 */
Luca Ceresoli5c83a962011-05-11 03:59:54 +00001365 /* IP_HDR_SIZE / 4 (not including UDP) */
1366 ip->ip_hl_v = 0x45;
wdenk2d966952002-10-31 22:12:35 +00001367 ip->ip_tos = 0;
Joe Hershberger2ed5b492012-05-23 07:59:07 +00001368 ip->ip_len = htons(IP_HDR_SIZE);
wdenk2d966952002-10-31 22:12:35 +00001369 ip->ip_id = htons(NetIPID++);
Peter Tyser0885bf02008-12-01 16:26:20 -06001370 ip->ip_off = htons(IP_FLAGS_DFRAG); /* Don't fragment */
wdenk2d966952002-10-31 22:12:35 +00001371 ip->ip_ttl = 255;
wdenk2d966952002-10-31 22:12:35 +00001372 ip->ip_sum = 0;
Luca Ceresoli5c83a962011-05-11 03:59:54 +00001373 /* already in network byte order */
Joe Hershberger2ed5b492012-05-23 07:59:07 +00001374 NetCopyIP((void *)&ip->ip_src, &source);
1375 /* already in network byte order */
Luca Ceresolif27d91f2011-05-04 02:40:44 +00001376 NetCopyIP((void *)&ip->ip_dst, &dest);
Joe Hershberger2ed5b492012-05-23 07:59:07 +00001377}
1378
1379void net_set_udp_header(uchar *pkt, IPaddr_t dest, int dport, int sport,
1380 int len)
1381{
1382 struct ip_udp_hdr *ip = (struct ip_udp_hdr *)pkt;
1383
1384 /*
1385 * If the data is an odd number of bytes, zero the
1386 * byte after the last byte so that the checksum
1387 * will work.
1388 */
1389 if (len & 1)
1390 pkt[IP_UDP_HDR_SIZE + len] = 0;
1391
1392 net_set_ip_header(pkt, dest, NetOurIP);
1393 ip->ip_len = htons(IP_UDP_HDR_SIZE + len);
1394 ip->ip_p = IPPROTO_UDP;
Simon Glassdfcdcee2015-01-19 22:16:08 -07001395 ip->ip_sum = compute_ip_checksum(ip, IP_HDR_SIZE);
Joe Hershberger2ed5b492012-05-23 07:59:07 +00001396
wdenk2d966952002-10-31 22:12:35 +00001397 ip->udp_src = htons(sport);
1398 ip->udp_dst = htons(dport);
Joe Hershberger6fe8b452012-05-23 07:58:04 +00001399 ip->udp_len = htons(UDP_HDR_SIZE + len);
wdenk2d966952002-10-31 22:12:35 +00001400 ip->udp_xsum = 0;
wdenk2d966952002-10-31 22:12:35 +00001401}
1402
Luca Ceresoli7cbd7482011-05-11 03:59:56 +00001403void copy_filename(char *dst, const char *src, int size)
wdenk2d966952002-10-31 22:12:35 +00001404{
1405 if (*src && (*src == '"')) {
1406 ++src;
1407 --size;
1408 }
1409
Luca Ceresolie8ccbb02011-05-04 02:40:43 +00001410 while ((--size > 0) && *src && (*src != '"'))
wdenk2d966952002-10-31 22:12:35 +00001411 *dst++ = *src++;
wdenk2d966952002-10-31 22:12:35 +00001412 *dst = '\0';
1413}
1414
Luca Ceresoli5c83a962011-05-11 03:59:54 +00001415#if defined(CONFIG_CMD_NFS) || \
1416 defined(CONFIG_CMD_SNTP) || \
1417 defined(CONFIG_CMD_DNS)
Robin Getz82f0d232009-07-20 14:53:54 -04001418/*
Robin Getz55b50e92010-03-08 14:07:00 -05001419 * make port a little random (1024-17407)
1420 * This keeps the math somewhat trivial to compute, and seems to work with
1421 * all supported protocols/clients/servers
Robin Getz82f0d232009-07-20 14:53:54 -04001422 */
1423unsigned int random_port(void)
1424{
Robin Getz55b50e92010-03-08 14:07:00 -05001425 return 1024 + (get_timer(0) % 0x4000);
Robin Getz82f0d232009-07-20 14:53:54 -04001426}
1427#endif
1428
Luca Ceresoli7cbd7482011-05-11 03:59:56 +00001429void ip_to_string(IPaddr_t x, char *s)
wdenk2d966952002-10-31 22:12:35 +00001430{
Luca Ceresoli7cbd7482011-05-11 03:59:56 +00001431 x = ntohl(x);
1432 sprintf(s, "%d.%d.%d.%d",
1433 (int) ((x >> 24) & 0xff),
1434 (int) ((x >> 16) & 0xff),
1435 (int) ((x >> 8) & 0xff), (int) ((x >> 0) & 0xff)
wdenk05939202004-04-18 17:39:38 +00001436 );
wdenk2d966952002-10-31 22:12:35 +00001437}
1438
wdenk145d2c12004-04-15 21:48:45 +00001439void VLAN_to_string(ushort x, char *s)
1440{
1441 x = ntohs(x);
1442
1443 if (x == (ushort)-1)
1444 x = VLAN_NONE;
1445
1446 if (x == VLAN_NONE)
1447 strcpy(s, "none");
1448 else
1449 sprintf(s, "%d", x & VLAN_IDMASK);
1450}
1451
Mike Frysinger89c73922010-10-20 07:16:48 -04001452ushort string_to_VLAN(const char *s)
wdenk145d2c12004-04-15 21:48:45 +00001453{
1454 ushort id;
1455
1456 if (s == NULL)
wdenk656140b2004-04-25 13:18:40 +00001457 return htons(VLAN_NONE);
wdenk145d2c12004-04-15 21:48:45 +00001458
1459 if (*s < '0' || *s > '9')
1460 id = VLAN_NONE;
1461 else
1462 id = (ushort)simple_strtoul(s, NULL, 10);
1463
wdenk656140b2004-04-25 13:18:40 +00001464 return htons(id);
wdenk145d2c12004-04-15 21:48:45 +00001465}
1466
wdenk145d2c12004-04-15 21:48:45 +00001467ushort getenv_VLAN(char *var)
1468{
Luca Ceresoli5fe6de22011-05-04 02:40:45 +00001469 return string_to_VLAN(getenv(var));
wdenk145d2c12004-04-15 21:48:45 +00001470}