blob: e5ab07c81d53dc81b8d92ea620ebbcfc689ee5d0 [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 Denk2ea91032014-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 Hershbergerd22c3382012-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 Denkb2f50802005-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
wdenkcbd8a352004-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
wdenkea287de2005-04-01 00:25:43 +000074 *
75 * SNTP:
76 *
Wolfgang Denkb2f50802005-08-12 23:43:12 +020077 * Prerequisites: - own ethernet address
wdenkea287de2005-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 Hershbergera9f51c92012-12-11 22:16:26 -060086#include <environment.h>
wdenk2d966952002-10-31 22:12:35 +000087#include <net.h>
Joe Hershberger4545f4e2012-05-23 07:58:15 +000088#if defined(CONFIG_STATUS_LED)
89#include <miiphy.h>
90#include <status_led.h>
91#endif
92#include <watchdog.h>
93#include <linux/compiler.h>
Joe Hershbergerd280d3f2012-05-23 07:58:01 +000094#include "arp.h"
wdenk2d966952002-10-31 22:12:35 +000095#include "bootp.h"
Joe Hershbergerf575ae12012-05-23 07:57:59 +000096#include "cdp.h"
Robin Getz1a32bf42009-07-20 14:53:54 -040097#if defined(CONFIG_CMD_DNS)
98#include "dns.h"
99#endif
Joe Hershbergerd22c3382012-05-23 08:00:12 +0000100#include "link_local.h"
Joe Hershberger4545f4e2012-05-23 07:58:15 +0000101#include "nfs.h"
Joe Hershbergera36b12f2012-05-23 07:58:02 +0000102#include "ping.h"
Joe Hershberger4545f4e2012-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 Denkd87080b2006-03-31 18:32:53 +0200109DECLARE_GLOBAL_DATA_PTR;
110
wdenk2d966952002-10-31 22:12:35 +0000111/** BOOTP EXTENTIONS **/
112
Luca Ceresoli3e38e422011-05-11 03:59:54 +0000113/* Our subnet mask (0=unknown) */
Luca Ceresolic586ce62011-05-11 03:59:55 +0000114IPaddr_t NetOurSubnetMask;
Luca Ceresoli3e38e422011-05-11 03:59:54 +0000115/* Our gateways IP address */
Luca Ceresolic586ce62011-05-11 03:59:55 +0000116IPaddr_t NetOurGatewayIP;
Luca Ceresoli3e38e422011-05-11 03:59:54 +0000117/* Our DNS IP address */
Luca Ceresolic586ce62011-05-11 03:59:55 +0000118IPaddr_t NetOurDNSIP;
Jon Loeliger1fe80d72007-07-09 22:08:34 -0500119#if defined(CONFIG_BOOTP_DNS2)
Luca Ceresoli3e38e422011-05-11 03:59:54 +0000120/* Our 2nd DNS IP address */
Luca Ceresolic586ce62011-05-11 03:59:55 +0000121IPaddr_t NetOurDNS2IP;
stroesefe389a82003-08-28 14:17:32 +0000122#endif
Luca Ceresoli3e38e422011-05-11 03:59:54 +0000123/* Our NIS domain */
Luca Ceresolic586ce62011-05-11 03:59:55 +0000124char NetOurNISDomain[32] = {0,};
Luca Ceresoli3e38e422011-05-11 03:59:54 +0000125/* Our hostname */
Luca Ceresolic586ce62011-05-11 03:59:55 +0000126char NetOurHostName[32] = {0,};
Luca Ceresoli3e38e422011-05-11 03:59:54 +0000127/* Our bootpath */
Luca Ceresolic586ce62011-05-11 03:59:55 +0000128char NetOurRootPath[64] = {0,};
Luca Ceresoli3e38e422011-05-11 03:59:54 +0000129/* Our bootfile size in blocks */
Luca Ceresolic586ce62011-05-11 03:59:55 +0000130ushort NetBootFileSize;
wdenk2d966952002-10-31 22:12:35 +0000131
David Updegraff53a5c422007-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 Ceresoli3e38e422011-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 Ceresolic586ce62011-05-11 03:59:55 +0000143uchar NetServerEther[6];
Luca Ceresoli3e38e422011-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 Hershbergerdb288a92012-05-15 08:59:04 +0000149uchar *NetRxPacket;
Luca Ceresoli3e38e422011-05-11 03:59:54 +0000150/* Current rx packet length */
151int NetRxPacketLen;
152/* IP packet ID */
153unsigned NetIPID;
154/* Ethernet bcast address */
Luca Ceresolic586ce62011-05-11 03:59:55 +0000155uchar NetBcastAddr[6] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
156uchar NetEtherNullAddr[6];
Rafal Jaworowskif85b6072007-12-27 18:19:02 +0100157#ifdef CONFIG_API
Joe Hershbergerdb288a92012-05-15 08:59:04 +0000158void (*push_packet)(void *, int len) = 0;
Rafal Jaworowskif85b6072007-12-27 18:19:02 +0100159#endif
Luca Ceresoli3e38e422011-05-11 03:59:54 +0000160/* Network loop state */
Joe Hershberger22f6e992012-05-23 07:59:14 +0000161enum net_loop_state net_state;
Luca Ceresoli3e38e422011-05-11 03:59:54 +0000162/* Tried all network devices */
Luca Ceresolic586ce62011-05-11 03:59:55 +0000163int NetRestartWrap;
Luca Ceresoli3e38e422011-05-11 03:59:54 +0000164/* Network loop restarted */
Luca Ceresolic586ce62011-05-11 03:59:55 +0000165static int NetRestarted;
Luca Ceresoli3e38e422011-05-11 03:59:54 +0000166/* At least one device configured */
Luca Ceresolic586ce62011-05-11 03:59:55 +0000167static int NetDevExists;
wdenk2d966952002-10-31 22:12:35 +0000168
wdenk6e592382004-04-18 17:39:38 +0000169/* XXX in both little & big endian machines 0xFFFF == ntohs(-1) */
Luca Ceresoli3e38e422011-05-11 03:59:54 +0000170/* default is without VLAN */
171ushort NetOurVLAN = 0xFFFF;
172/* ditto */
173ushort NetOurNativeVLAN = 0xFFFF;
wdenka3d991b2004-04-15 21:48:45 +0000174
Luca Ceresoli3e38e422011-05-11 03:59:54 +0000175/* Boot File name */
176char BootFile[128];
wdenk2d966952002-10-31 22:12:35 +0000177
Jon Loeliger643d1ab2007-07-09 17:45:14 -0500178#if defined(CONFIG_CMD_SNTP)
Luca Ceresoli3e38e422011-05-11 03:59:54 +0000179/* NTP server IP address */
180IPaddr_t NetNtpServerIP;
181/* offset time from UTC */
Luca Ceresolic586ce62011-05-11 03:59:55 +0000182int NetTimeOffset;
wdenkea287de2005-04-01 00:25:43 +0000183#endif
184
Kim Phillips06370592012-10-29 13:34:33 +0000185static uchar PktBuf[(PKTBUFSRX+1) * PKTSIZE_ALIGN + PKTALIGN];
wdenk2d966952002-10-31 22:12:35 +0000186
Luca Ceresoli3e38e422011-05-11 03:59:54 +0000187/* Receive packet */
Joe Hershbergerdb288a92012-05-15 08:59:04 +0000188uchar *NetRxPackets[PKTBUFSRX];
wdenk2d966952002-10-31 22:12:35 +0000189
Joe Hershbergerece223b2012-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 Glass39bccd22011-10-26 14:18:38 +0000194#ifdef CONFIG_CMD_TFTPPUT
Joe Hershbergerece223b2012-05-23 07:59:15 +0000195/* Current ICMP rx handler */
196static rxhand_icmp_f *packet_icmp_handler;
Simon Glass39bccd22011-10-26 14:18:38 +0000197#endif
Luca Ceresoli3e38e422011-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 Hershbergerdb288a92012-05-15 08:59:04 +0000205uchar *NetTxPacket;
wdenk2d966952002-10-31 22:12:35 +0000206
Simon Glasse4bf0c52011-10-24 18:00:02 +0000207static int net_check_prereq(enum proto_t protocol);
wdenk2d966952002-10-31 22:12:35 +0000208
Remy Bohmer67b96e82009-10-28 22:13:39 +0100209static int NetTryCount;
210
Jim Linb63056d2013-08-13 19:03:05 +0800211int __maybe_unused net_busy_flag;
212
wdenk2d966952002-10-31 22:12:35 +0000213/**********************************************************************/
wdenk73a8b272003-06-05 19:27:42 +0000214
Joe Hershbergera9f51c92012-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 Glasse4a3d572011-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 Hershbergerec8a2522012-12-11 22:16:22 -0600237#if defined(CONFIG_CMD_NFS)
Simon Glasse4a3d572011-10-27 06:24:32 +0000238 const char *s = getenv("autoload");
239
Joe Hershbergerec8a2522012-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 Glasse4a3d572011-10-27 06:24:32 +0000247#endif
Joe Hershbergerec8a2522012-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 Glasse4a3d572011-10-27 06:24:32 +0000255 }
256 TftpStart(TFTPGET);
257}
258
Joe Hershbergercb1c9912012-05-23 07:59:21 +0000259static void NetInitLoop(void)
Heiko Schocher2f70c492009-02-10 09:38:52 +0100260{
Luca Ceresolic586ce62011-05-11 03:59:55 +0000261 static int env_changed_id;
Luca Ceresoli4f63acd2011-05-11 03:59:56 +0000262 int env_id = get_env_id();
Heiko Schocher2f70c492009-02-10 09:38:52 +0100263
264 /* update only when the environment has changed */
Michael Zaidman3c172c42009-04-04 01:43:00 +0300265 if (env_changed_id != env_id) {
Enric Balletbo i Serra23a70bf2011-05-31 21:01:47 +0000266 NetOurIP = getenv_IPaddr("ipaddr");
Luca Ceresoli4f63acd2011-05-11 03:59:56 +0000267 NetOurGatewayIP = getenv_IPaddr("gatewayip");
268 NetOurSubnetMask = getenv_IPaddr("netmask");
269 NetServerIP = getenv_IPaddr("serverip");
Heiko Schocher2f70c492009-02-10 09:38:52 +0100270 NetOurNativeVLAN = getenv_VLAN("nvlan");
Michael Zaidman3c172c42009-04-04 01:43:00 +0300271 NetOurVLAN = getenv_VLAN("vlan");
Robin Getz1a32bf42009-07-20 14:53:54 -0400272#if defined(CONFIG_CMD_DNS)
273 NetOurDNSIP = getenv_IPaddr("dnsip");
274#endif
Michael Zaidman3c172c42009-04-04 01:43:00 +0300275 env_changed_id = env_id;
Heiko Schocher2f70c492009-02-10 09:38:52 +0100276 }
Jim Lin7315cfd2013-05-17 17:41:03 +0800277 if (eth_get_dev())
Joe Hershberger8b2c9a72015-03-22 17:09:00 -0500278 memcpy(NetOurEther, eth_get_ethaddr(), 6);
Michael Zaidman3c172c42009-04-04 01:43:00 +0300279
Heiko Schocherda954272009-04-28 08:36:11 +0200280 return;
Heiko Schocher2f70c492009-02-10 09:38:52 +0100281}
282
Joe Hershbergerece223b2012-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 Hershberger46c495d2012-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
wdenk73a8b272003-06-05 19:27:42 +0000320/**********************************************************************/
wdenk2d966952002-10-31 22:12:35 +0000321/*
322 * Main network processing loop.
323 */
324
Simon Glasse4bf0c52011-10-24 18:00:02 +0000325int NetLoop(enum proto_t protocol)
wdenk2d966952002-10-31 22:12:35 +0000326{
Simon Glass4793ee62011-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 Bohmer67b96e82009-10-28 22:13:39 +0100331 NetTryCount = 1;
Joe Hershberger4ef8d532012-05-23 08:01:04 +0000332 debug_cond(DEBUG_INT_STATE, "--- NetLoop Entry\n");
wdenk73a8b272003-06-05 19:27:42 +0000333
Simon Glass573f14f2011-12-10 11:08:06 +0000334 bootstage_mark_name(BOOTSTAGE_ID_ETH_START, "eth_start");
Joe Hershberger46c495d2012-05-23 07:59:22 +0000335 net_init();
Joe Hershbergerf8be7d62012-08-03 10:59:08 +0000336 if (eth_is_on_demand_init() || protocol != NETCONS) {
wdenkb1bf6f22005-04-03 14:52:59 +0000337 eth_halt();
Joe Hershbergerf8be7d62012-08-03 10:59:08 +0000338 eth_set_current();
Joe Hershbergerd2eaec62015-03-22 17:09:06 -0500339 if (eth_init() < 0) {
Joe Hershbergerf8be7d62012-08-03 10:59:08 +0000340 eth_halt();
341 return -1;
342 }
343 } else
Joe Hershbergerd2eaec62015-03-22 17:09:06 -0500344 eth_init_state_only();
wdenk2d966952002-10-31 22:12:35 +0000345
346restart:
Jim Linb63056d2013-08-13 19:03:05 +0800347#ifdef CONFIG_USB_KEYBOARD
348 net_busy_flag = 0;
349#endif
Joe Hershberger22f6e992012-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 Hershberger4ef8d532012-05-23 08:01:04 +0000357 debug_cond(DEBUG_INT_STATE, "--- NetLoop Init\n");
Joe Hershbergercb1c9912012-05-23 07:59:21 +0000358 NetInitLoop();
wdenk2d966952002-10-31 22:12:35 +0000359
Luca Ceresoli4f63acd2011-05-11 03:59:56 +0000360 switch (net_check_prereq(protocol)) {
wdenk2d966952002-10-31 22:12:35 +0000361 case 1:
362 /* network not configured */
wdenkb1bf6f22005-04-03 14:52:59 +0000363 eth_halt();
Luca Ceresoli92895de2011-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 Glasse4bf0c52011-10-24 18:00:02 +0000372 NetBootFileXferSize = 0;
wdenk2d966952002-10-31 22:12:35 +0000373 switch (protocol) {
Simon Glasse4bf0c52011-10-24 18:00:02 +0000374 case TFTPGET:
Simon Glass1fb7cd42011-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 Glasse4bf0c52011-10-24 18:00:02 +0000379 TftpStart(protocol);
wdenk2d966952002-10-31 22:12:35 +0000380 break;
Luca Ceresoli7a83af02011-05-17 00:03:40 +0000381#ifdef CONFIG_CMD_TFTPSRV
382 case TFTPSRV:
383 TftpStartServer();
384 break;
385#endif
Jon Loeliger643d1ab2007-07-09 17:45:14 -0500386#if defined(CONFIG_CMD_DHCP)
wdenk2d966952002-10-31 22:12:35 +0000387 case DHCP:
Stephen Warrenf59be6e2014-07-25 17:30:48 -0600388 BootpReset();
Michael Zaidman09133f82009-07-14 23:37:12 +0300389 NetOurIP = 0;
wdenk2d966952002-10-31 22:12:35 +0000390 DhcpRequest(); /* Basically same as BOOTP */
391 break;
Jon Loeliger610f2e92007-07-10 11:05:02 -0500392#endif
wdenk2d966952002-10-31 22:12:35 +0000393
394 case BOOTP:
Stephen Warrenf59be6e2014-07-25 17:30:48 -0600395 BootpReset();
Michael Zaidman09133f82009-07-14 23:37:12 +0300396 NetOurIP = 0;
Luca Ceresoli4f63acd2011-05-11 03:59:56 +0000397 BootpRequest();
wdenk2d966952002-10-31 22:12:35 +0000398 break;
399
Peter Tyserbf6cb242010-09-30 11:25:48 -0500400#if defined(CONFIG_CMD_RARP)
wdenk2d966952002-10-31 22:12:35 +0000401 case RARP:
402 RarpTry = 0;
Michael Zaidman09133f82009-07-14 23:37:12 +0300403 NetOurIP = 0;
Luca Ceresoli4f63acd2011-05-11 03:59:56 +0000404 RarpRequest();
wdenk2d966952002-10-31 22:12:35 +0000405 break;
Peter Tyserbf6cb242010-09-30 11:25:48 -0500406#endif
Jon Loeliger643d1ab2007-07-09 17:45:14 -0500407#if defined(CONFIG_CMD_PING)
wdenk73a8b272003-06-05 19:27:42 +0000408 case PING:
Joe Hershbergera36b12f2012-05-23 07:58:02 +0000409 ping_start();
wdenk73a8b272003-06-05 19:27:42 +0000410 break;
411#endif
Jon Loeliger643d1ab2007-07-09 17:45:14 -0500412#if defined(CONFIG_CMD_NFS)
wdenkcbd8a352004-02-24 02:00:03 +0000413 case NFS:
414 NfsStart();
415 break;
416#endif
Jon Loeliger643d1ab2007-07-09 17:45:14 -0500417#if defined(CONFIG_CMD_CDP)
wdenka3d991b2004-04-15 21:48:45 +0000418 case CDP:
419 CDPStart();
420 break;
421#endif
Hannes Petermaier1f9ce302014-06-13 06:25:29 +0200422#if defined (CONFIG_NETCONSOLE) && !(CONFIG_SPL_BUILD)
wdenk68ceb292004-08-02 21:11:11 +0000423 case NETCONS:
424 NcStart();
425 break;
426#endif
Jon Loeliger643d1ab2007-07-09 17:45:14 -0500427#if defined(CONFIG_CMD_SNTP)
wdenkea287de2005-04-01 00:25:43 +0000428 case SNTP:
429 SntpStart();
430 break;
431#endif
Robin Getz1a32bf42009-07-20 14:53:54 -0400432#if defined(CONFIG_CMD_DNS)
433 case DNS:
434 DnsStart();
435 break;
436#endif
Joe Hershbergerd22c3382012-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 Loeliger643d1ab2007-07-09 17:45:14 -0500449#if defined(CONFIG_MII) || defined(CONFIG_CMD_MII)
Luca Ceresoli3e38e422011-05-11 03:59:54 +0000450#if defined(CONFIG_SYS_FAULT_ECHO_LINK_DOWN) && \
451 defined(CONFIG_STATUS_LED) && \
452 defined(STATUS_LED_RED)
wdenkfc3e2162003-10-08 22:33:00 +0000453 /*
wdenk42d1f032003-10-15 23:53:47 +0000454 * Echo the inverted link state to the fault LED.
wdenkfc3e2162003-10-08 22:33:00 +0000455 */
Luca Ceresolid3c65b02011-05-04 02:40:43 +0000456 if (miiphy_link(eth_get_dev()->name, CONFIG_SYS_FAULT_MII_ADDR))
Luca Ceresoli4f63acd2011-05-11 03:59:56 +0000457 status_led_set(STATUS_LED_RED, STATUS_LED_OFF);
Luca Ceresolid3c65b02011-05-04 02:40:43 +0000458 else
Luca Ceresoli4f63acd2011-05-11 03:59:56 +0000459 status_led_set(STATUS_LED_RED, STATUS_LED_ON);
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200460#endif /* CONFIG_SYS_FAULT_ECHO_LINK_DOWN, ... */
wdenkfc3e2162003-10-08 22:33:00 +0000461#endif /* CONFIG_MII, ... */
Jim Linb63056d2013-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 Hershberger22f6e992012-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 Hershberger48522bb2012-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 Liakhovetski40cb90e2008-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 Hershbergere94070c2012-05-23 07:59:24 +0000485 /* cancel any ARP that may not have completed */
486 NetArpWaitPacketIP = 0;
487
Joe Hershbergerece223b2012-05-23 07:59:15 +0000488 net_cleanup_loop();
wdenk8bde7f72003-06-27 21:31:46 +0000489 eth_halt();
Joe Hershbergerf8be7d62012-08-03 10:59:08 +0000490 /* Invalidate the last protocol */
491 eth_set_last_protocol(BOOTP);
492
Luca Ceresoli4f63acd2011-05-11 03:59:56 +0000493 puts("\nAbort\n");
Joe Hershberger4ef8d532012-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 Glass4793ee62011-10-24 18:00:01 +0000497 goto done;
wdenk2d966952002-10-31 22:12:35 +0000498 }
499
wdenk73a8b272003-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 */
wdenke0ac62d2003-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 Loeliger643d1ab2007-07-09 17:45:14 -0500509#if defined(CONFIG_MII) || defined(CONFIG_CMD_MII)
Luca Ceresoli4f63acd2011-05-11 03:59:56 +0000510#if defined(CONFIG_SYS_FAULT_ECHO_LINK_DOWN) && \
511 defined(CONFIG_STATUS_LED) && \
512 defined(STATUS_LED_RED)
wdenkfc3e2162003-10-08 22:33:00 +0000513 /*
wdenk42d1f032003-10-15 23:53:47 +0000514 * Echo the inverted link state to the fault LED.
wdenkfc3e2162003-10-08 22:33:00 +0000515 */
Luca Ceresoli4f63acd2011-05-11 03:59:56 +0000516 if (miiphy_link(eth_get_dev()->name,
Luca Ceresoli3e38e422011-05-11 03:59:54 +0000517 CONFIG_SYS_FAULT_MII_ADDR)) {
Luca Ceresoli4f63acd2011-05-11 03:59:56 +0000518 status_led_set(STATUS_LED_RED, STATUS_LED_OFF);
wdenkfc3e2162003-10-08 22:33:00 +0000519 } else {
Luca Ceresoli4f63acd2011-05-11 03:59:56 +0000520 status_led_set(STATUS_LED_RED, STATUS_LED_ON);
wdenkfc3e2162003-10-08 22:33:00 +0000521 }
Luca Ceresoli4f63acd2011-05-11 03:59:56 +0000522#endif /* CONFIG_SYS_FAULT_ECHO_LINK_DOWN, ... */
wdenkfc3e2162003-10-08 22:33:00 +0000523#endif /* CONFIG_MII, ... */
Joe Hershberger4ef8d532012-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
530
Joe Hershberger22f6e992012-05-23 07:59:14 +0000531 switch (net_state) {
wdenk2d966952002-10-31 22:12:35 +0000532
533 case NETLOOP_RESTART:
wdenk2d966952002-10-31 22:12:35 +0000534 NetRestarted = 1;
wdenk2d966952002-10-31 22:12:35 +0000535 goto restart;
536
537 case NETLOOP_SUCCESS:
Joe Hershbergerece223b2012-05-23 07:59:15 +0000538 net_cleanup_loop();
wdenk2d966952002-10-31 22:12:35 +0000539 if (NetBootFileXferSize > 0) {
wdenk2d966952002-10-31 22:12:35 +0000540 printf("Bytes transferred = %ld (%lx hex)\n",
541 NetBootFileXferSize,
542 NetBootFileXferSize);
Simon Glass978226d2013-02-24 17:33:24 +0000543 setenv_hex("filesize", NetBootFileXferSize);
544 setenv_hex("fileaddr", load_addr);
wdenk2d966952002-10-31 22:12:35 +0000545 }
Joe Hershbergerf8be7d62012-08-03 10:59:08 +0000546 if (protocol != NETCONS)
547 eth_halt();
548 else
549 eth_halt_state_only();
550
551 eth_set_last_protocol(protocol);
552
Simon Glass4793ee62011-10-24 18:00:01 +0000553 ret = NetBootFileXferSize;
Joe Hershberger4ef8d532012-05-23 08:01:04 +0000554 debug_cond(DEBUG_INT_STATE, "--- NetLoop Success!\n");
Simon Glass4793ee62011-10-24 18:00:01 +0000555 goto done;
wdenk2d966952002-10-31 22:12:35 +0000556
557 case NETLOOP_FAIL:
Joe Hershbergerece223b2012-05-23 07:59:15 +0000558 net_cleanup_loop();
Joe Hershbergerf8be7d62012-08-03 10:59:08 +0000559 /* Invalidate the last protocol */
560 eth_set_last_protocol(BOOTP);
Joe Hershberger4ef8d532012-05-23 08:01:04 +0000561 debug_cond(DEBUG_INT_STATE, "--- NetLoop Fail!\n");
Simon Glass4793ee62011-10-24 18:00:01 +0000562 goto done;
Joe Hershberger22f6e992012-05-23 07:59:14 +0000563
564 case NETLOOP_CONTINUE:
565 continue;
wdenk2d966952002-10-31 22:12:35 +0000566 }
567 }
Simon Glass4793ee62011-10-24 18:00:01 +0000568
569done:
Jim Linb63056d2013-08-13 19:03:05 +0800570#ifdef CONFIG_USB_KEYBOARD
571 net_busy_flag = 0;
572#endif
Simon Glass39bccd22011-10-26 14:18:38 +0000573#ifdef CONFIG_CMD_TFTPPUT
Simon Glass4793ee62011-10-24 18:00:01 +0000574 /* Clear out the handlers */
Joe Hershbergerece223b2012-05-23 07:59:15 +0000575 net_set_udp_handler(NULL);
Simon Glass4793ee62011-10-24 18:00:01 +0000576 net_set_icmp_handler(NULL);
Simon Glass39bccd22011-10-26 14:18:38 +0000577#endif
Simon Glass4793ee62011-10-24 18:00:01 +0000578 return ret;
wdenk2d966952002-10-31 22:12:35 +0000579}
580
581/**********************************************************************/
582
583static void
584startAgainTimeout(void)
585{
Joe Hershberger22f6e992012-05-23 07:59:14 +0000586 net_set_state(NETLOOP_RESTART);
wdenk2d966952002-10-31 22:12:35 +0000587}
588
Luca Ceresoli4f63acd2011-05-11 03:59:56 +0000589void NetStartAgain(void)
wdenk2d966952002-10-31 22:12:35 +0000590{
wdenk6e592382004-04-18 17:39:38 +0000591 char *nretry;
Remy Bohmer67b96e82009-10-28 22:13:39 +0100592 int retry_forever = 0;
593 unsigned long retrycnt = 0;
wdenka3d991b2004-04-15 21:48:45 +0000594
Remy Bohmer67b96e82009-10-28 22:13:39 +0100595 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 Hershberger22f6e992012-05-23 07:59:14 +0000610 net_set_state(NETLOOP_FAIL);
wdenka3d991b2004-04-15 21:48:45 +0000611 return;
612 }
Remy Bohmer67b96e82009-10-28 22:13:39 +0100613
614 NetTryCount++;
615
Luca Ceresoli4f63acd2011-05-11 03:59:56 +0000616 eth_halt();
Matthias Fuchs8b0c5c12007-12-27 16:58:41 +0100617#if !defined(CONFIG_NET_DO_NOT_TRY_ANOTHER)
Luca Ceresoli4f63acd2011-05-11 03:59:56 +0000618 eth_try_another(!NetRestarted);
Matthias Fuchs8b0c5c12007-12-27 16:58:41 +0100619#endif
Joe Hershbergerd2eaec62015-03-22 17:09:06 -0500620 eth_init();
wdenk6e592382004-04-18 17:39:38 +0000621 if (NetRestartWrap) {
wdenk2d966952002-10-31 22:12:35 +0000622 NetRestartWrap = 0;
Remy Bohmer67b96e82009-10-28 22:13:39 +0100623 if (NetDevExists) {
Luca Ceresoli4f63acd2011-05-11 03:59:56 +0000624 NetSetTimeout(10000UL, startAgainTimeout);
Joe Hershbergerece223b2012-05-23 07:59:15 +0000625 net_set_udp_handler(NULL);
wdenk6e592382004-04-18 17:39:38 +0000626 } else {
Joe Hershberger22f6e992012-05-23 07:59:14 +0000627 net_set_state(NETLOOP_FAIL);
wdenk2d966952002-10-31 22:12:35 +0000628 }
wdenk6e592382004-04-18 17:39:38 +0000629 } else {
Joe Hershberger22f6e992012-05-23 07:59:14 +0000630 net_set_state(NETLOOP_RESTART);
wdenk2d966952002-10-31 22:12:35 +0000631 }
wdenk2d966952002-10-31 22:12:35 +0000632}
633
634/**********************************************************************/
635/*
636 * Miscelaneous bits.
637 */
638
Joe Hershbergerece223b2012-05-23 07:59:15 +0000639static void dummy_handler(uchar *pkt, unsigned dport,
640 IPaddr_t sip, unsigned sport,
641 unsigned len)
Joe Hershbergerd280d3f2012-05-23 07:58:01 +0000642{
Joe Hershbergerd280d3f2012-05-23 07:58:01 +0000643}
644
Joe Hershbergerece223b2012-05-23 07:59:15 +0000645rxhand_f *net_get_udp_handler(void)
wdenk2d966952002-10-31 22:12:35 +0000646{
Joe Hershbergerece223b2012-05-23 07:59:15 +0000647 return udp_packet_handler;
648}
649
650void net_set_udp_handler(rxhand_f *f)
651{
Joe Hershberger4ef8d532012-05-23 08:01:04 +0000652 debug_cond(DEBUG_INT_STATE, "--- NetLoop UDP handler set (%p)\n", f);
Joe Hershbergerece223b2012-05-23 07:59:15 +0000653 if (f == NULL)
654 udp_packet_handler = dummy_handler;
655 else
656 udp_packet_handler = f;
657}
658
659rxhand_f *net_get_arp_handler(void)
660{
661 return arp_packet_handler;
662}
663
664void net_set_arp_handler(rxhand_f *f)
665{
Joe Hershberger4ef8d532012-05-23 08:01:04 +0000666 debug_cond(DEBUG_INT_STATE, "--- NetLoop ARP handler set (%p)\n", f);
Joe Hershbergerece223b2012-05-23 07:59:15 +0000667 if (f == NULL)
668 arp_packet_handler = dummy_handler;
669 else
670 arp_packet_handler = f;
wdenk2d966952002-10-31 22:12:35 +0000671}
672
Simon Glass39bccd22011-10-26 14:18:38 +0000673#ifdef CONFIG_CMD_TFTPPUT
Simon Glass4793ee62011-10-24 18:00:01 +0000674void net_set_icmp_handler(rxhand_icmp_f *f)
675{
676 packet_icmp_handler = f;
677}
Simon Glass39bccd22011-10-26 14:18:38 +0000678#endif
wdenk2d966952002-10-31 22:12:35 +0000679
680void
Luca Ceresoli6b147d12011-05-04 02:40:44 +0000681NetSetTimeout(ulong iv, thand_f *f)
wdenk2d966952002-10-31 22:12:35 +0000682{
683 if (iv == 0) {
Joe Hershberger4ef8d532012-05-23 08:01:04 +0000684 debug_cond(DEBUG_INT_STATE,
685 "--- NetLoop timeout handler cancelled\n");
wdenk2d966952002-10-31 22:12:35 +0000686 timeHandler = (thand_f *)0;
687 } else {
Joe Hershberger4ef8d532012-05-23 08:01:04 +0000688 debug_cond(DEBUG_INT_STATE,
689 "--- NetLoop timeout handler set (%p)\n", f);
wdenk2d966952002-10-31 22:12:35 +0000690 timeHandler = f;
wdenke0ac62d2003-08-17 18:55:18 +0000691 timeStart = get_timer(0);
Tetsuyuki Kobayashi1389f982012-06-25 02:37:27 +0000692 timeDelta = iv * CONFIG_SYS_HZ / 1000;
wdenk2d966952002-10-31 22:12:35 +0000693 }
694}
695
Joe Hershberger206d07f2012-05-23 07:58:10 +0000696int NetSendUDPPacket(uchar *ether, IPaddr_t dest, int dport, int sport,
697 int payload_len)
wdenk73a8b272003-06-05 19:27:42 +0000698{
wdenka3d991b2004-04-15 21:48:45 +0000699 uchar *pkt;
Joe Hershberger92146372012-05-23 07:59:08 +0000700 int eth_hdr_size;
701 int pkt_hdr_size;
wdenka3d991b2004-04-15 21:48:45 +0000702
Joe Hershberger46c495d2012-05-23 07:59:22 +0000703 /* make sure the NetTxPacket is initialized (NetInit() was called) */
704 assert(NetTxPacket != NULL);
705 if (NetTxPacket == NULL)
706 return -1;
707
wdenk73a8b272003-06-05 19:27:42 +0000708 /* convert to new style broadcast */
709 if (dest == 0)
710 dest = 0xFFFFFFFF;
wdenk2d966952002-10-31 22:12:35 +0000711
wdenk73a8b272003-06-05 19:27:42 +0000712 /* if broadcast, make the ether address a broadcast and don't do ARP */
713 if (dest == 0xFFFFFFFF)
714 ether = NetBcastAddr;
715
Joe Hershbergere94070c2012-05-23 07:59:24 +0000716 pkt = (uchar *)NetTxPacket;
wdenk73a8b272003-06-05 19:27:42 +0000717
Joe Hershberger92146372012-05-23 07:59:08 +0000718 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 Getz0ebf04c2009-07-23 03:01:03 -0400722
Joe Hershbergere94070c2012-05-23 07:59:24 +0000723 /* if MAC address was not discovered yet, do an ARP request */
724 if (memcmp(ether, NetEtherNullAddr, 6) == 0) {
Joe Hershberger4ef8d532012-05-23 08:01:04 +0000725 debug_cond(DEBUG_DEV_PKT, "sending ARP for %pI4\n", &dest);
Joe Hershberger92146372012-05-23 07:59:08 +0000726
727 /* save the ip and eth addr for the packet to send after arp */
wdenk73a8b272003-06-05 19:27:42 +0000728 NetArpWaitPacketIP = dest;
729 NetArpWaitPacketMAC = ether;
wdenka3d991b2004-04-15 21:48:45 +0000730
wdenk73a8b272003-06-05 19:27:42 +0000731 /* size of the waiting packet */
Joe Hershberger92146372012-05-23 07:59:08 +0000732 NetArpWaitTxPacketSize = pkt_hdr_size + payload_len;
wdenk73a8b272003-06-05 19:27:42 +0000733
734 /* and do the ARP request */
735 NetArpWaitTry = 1;
736 NetArpWaitTimerStart = get_timer(0);
737 ArpRequest();
738 return 1; /* waiting */
Joe Hershberger92146372012-05-23 07:59:08 +0000739 } else {
Joe Hershberger4ef8d532012-05-23 08:01:04 +0000740 debug_cond(DEBUG_DEV_PKT, "sending UDP to %pI4/%pM\n",
741 &dest, ether);
Joe Hershbergeradf5d932012-05-23 07:59:13 +0000742 NetSendPacket(NetTxPacket, pkt_hdr_size + payload_len);
Joe Hershberger92146372012-05-23 07:59:08 +0000743 return 0; /* transmitted */
wdenk73a8b272003-06-05 19:27:42 +0000744 }
wdenk73a8b272003-06-05 19:27:42 +0000745}
746
Alessandro Rubini5cfaa4e2009-08-07 13:58:56 +0200747#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 */
763static struct rpc_t rpc_specimen;
764#define IP_PKTSIZE (CONFIG_NET_MAXDEFRAG + sizeof(rpc_specimen.u.reply))
765
Joe Hershbergerc5c59df2012-05-23 07:58:05 +0000766#define IP_MAXUDP (IP_PKTSIZE - IP_HDR_SIZE)
Alessandro Rubini5cfaa4e2009-08-07 13:58:56 +0200767
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 */
772struct 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 Hershberger594c26f2012-05-23 07:58:04 +0000780static struct ip_udp_hdr *__NetDefragment(struct ip_udp_hdr *ip, int *lenp)
Alessandro Rubini5cfaa4e2009-08-07 13:58:56 +0200781{
Joe Hershberger48522bb2012-05-15 08:59:08 +0000782 static uchar pkt_buff[IP_PKTSIZE] __aligned(PKTALIGN);
Alessandro Rubini5cfaa4e2009-08-07 13:58:56 +0200783 static u16 first_hole, total_len;
784 struct hole *payload, *thisfrag, *h, *newh;
Joe Hershberger594c26f2012-05-23 07:58:04 +0000785 struct ip_udp_hdr *localip = (struct ip_udp_hdr *)pkt_buff;
Alessandro Rubini5cfaa4e2009-08-07 13:58:56 +0200786 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 Hershbergerc5c59df2012-05-23 07:58:05 +0000791 payload = (struct hole *)(pkt_buff + IP_HDR_SIZE);
Alessandro Rubini5cfaa4e2009-08-07 13:58:56 +0200792 offset8 = (ip_off & IP_OFFS);
793 thisfrag = payload + offset8;
794 start = offset8 * 8;
Joe Hershbergerc5c59df2012-05-23 07:58:05 +0000795 len = ntohs(ip->ip_len) - IP_HDR_SIZE;
Alessandro Rubini5cfaa4e2009-08-07 13:58:56 +0200796
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 Hershbergerc5c59df2012-05-23 07:58:05 +0000808 memcpy(localip, ip, IP_HDR_SIZE);
Alessandro Rubini5cfaa4e2009-08-07 13:58:56 +0200809 }
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 Stephanee397e592010-06-11 19:26:43 +0200827 /* last fragment may be 1..7 bytes, the "+7" forces acceptance */
828 if (offset8 + ((len + 7) / 8) <= h - payload) {
Alessandro Rubini5cfaa4e2009-08-07 13:58:56 +0200829 /* 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 Ceresoli4f63acd2011-05-11 03:59:56 +0000845 if ((h >= thisfrag) && (h->last_byte <= start + len)) {
Alessandro Rubini5cfaa4e2009-08-07 13:58:56 +0200846 /* 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 Hershbergerc5c59df2012-05-23 07:58:05 +0000891 memcpy((uchar *)thisfrag, indata + IP_HDR_SIZE, len);
Alessandro Rubini5cfaa4e2009-08-07 13:58:56 +0200892 if (!done)
893 return NULL;
894
895 localip->ip_len = htons(total_len);
Joe Hershbergerc5c59df2012-05-23 07:58:05 +0000896 *lenp = total_len + IP_HDR_SIZE;
Alessandro Rubini5cfaa4e2009-08-07 13:58:56 +0200897 return localip;
898}
899
Joe Hershberger594c26f2012-05-23 07:58:04 +0000900static inline struct ip_udp_hdr *NetDefragment(struct ip_udp_hdr *ip, int *lenp)
Alessandro Rubini5cfaa4e2009-08-07 13:58:56 +0200901{
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 Hershberger594c26f2012-05-23 07:58:04 +0000910static inline struct ip_udp_hdr *NetDefragment(struct ip_udp_hdr *ip, int *lenp)
Alessandro Rubini5cfaa4e2009-08-07 13:58:56 +0200911{
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
wdenka3d991b2004-04-15 21:48:45 +0000918
Simon Glass8f79bb12011-10-24 18:00:00 +0000919/**
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 Hershberger594c26f2012-05-23 07:58:04 +0000925static void receive_icmp(struct ip_udp_hdr *ip, int len,
Joe Hershbergercb487f52012-05-23 07:58:06 +0000926 IPaddr_t src_ip, struct ethernet_hdr *et)
Simon Glass8f79bb12011-10-24 18:00:00 +0000927{
Joe Hershbergere0a63072012-05-23 07:58:09 +0000928 struct icmp_hdr *icmph = (struct icmp_hdr *)&ip->udp_src;
Simon Glass8f79bb12011-10-24 18:00:00 +0000929
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 Glass8f79bb12011-10-24 18:00:00 +0000937 default:
Joe Hershbergera36b12f2012-05-23 07:58:02 +0000938#if defined(CONFIG_CMD_PING)
939 ping_receive(et, ip, len);
940#endif
Simon Glass39bccd22011-10-26 14:18:38 +0000941#ifdef CONFIG_CMD_TFTPPUT
Simon Glass4793ee62011-10-24 18:00:01 +0000942 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 Glass39bccd22011-10-26 14:18:38 +0000946#endif
Simon Glass8f79bb12011-10-24 18:00:00 +0000947 break;
948 }
949}
950
wdenk2d966952002-10-31 22:12:35 +0000951void
Joe Hershbergerdb288a92012-05-15 08:59:04 +0000952NetReceive(uchar *inpkt, int len)
wdenk2d966952002-10-31 22:12:35 +0000953{
Joe Hershbergercb487f52012-05-23 07:58:06 +0000954 struct ethernet_hdr *et;
Joe Hershberger594c26f2012-05-23 07:58:04 +0000955 struct ip_udp_hdr *ip;
Joe Hershberger8d353eb2012-05-23 07:58:12 +0000956 IPaddr_t dst_ip;
Luca Ceresoli03eb1292011-04-18 06:19:50 +0000957 IPaddr_t src_ip;
Joe Hershberger8d353eb2012-05-23 07:58:12 +0000958 int eth_proto;
Jon Loeliger643d1ab2007-07-09 17:45:14 -0500959#if defined(CONFIG_CMD_CDP)
wdenka3d991b2004-04-15 21:48:45 +0000960 int iscdp;
961#endif
962 ushort cti = 0, vlanid = VLAN_NONE, myvlanid, mynvlanid;
wdenk2d966952002-10-31 22:12:35 +0000963
Joe Hershberger4ef8d532012-05-23 08:01:04 +0000964 debug_cond(DEBUG_NET_PKT, "packet received\n");
wdenka3d991b2004-04-15 21:48:45 +0000965
Mike Frysingerd9bec9f2009-07-18 21:04:08 -0400966 NetRxPacket = inpkt;
967 NetRxPacketLen = len;
Joe Hershbergercb487f52012-05-23 07:58:06 +0000968 et = (struct ethernet_hdr *)inpkt;
wdenka3d991b2004-04-15 21:48:45 +0000969
970 /* too small packet? */
971 if (len < ETHER_HDR_SIZE)
972 return;
973
Rafal Jaworowskif85b6072007-12-27 18:19:02 +0100974#ifdef CONFIG_API
975 if (push_packet) {
976 (*push_packet)(inpkt, len);
977 return;
978 }
979#endif
980
Jon Loeliger643d1ab2007-07-09 17:45:14 -0500981#if defined(CONFIG_CMD_CDP)
wdenka3d991b2004-04-15 21:48:45 +0000982 /* keep track if packet is CDP */
Joe Hershberger17351882012-05-23 07:58:00 +0000983 iscdp = is_cdp_packet(et->et_dest);
wdenka3d991b2004-04-15 21:48:45 +0000984#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;
wdenk2d966952002-10-31 22:12:35 +0000992
Joe Hershberger8d353eb2012-05-23 07:58:12 +0000993 eth_proto = ntohs(et->et_protlen);
wdenk2d966952002-10-31 22:12:35 +0000994
Joe Hershberger8d353eb2012-05-23 07:58:12 +0000995 if (eth_proto < 1514) {
Joe Hershbergercb487f52012-05-23 07:58:06 +0000996 struct e802_hdr *et802 = (struct e802_hdr *)et;
wdenk2d966952002-10-31 22:12:35 +0000997 /*
Joe Hershbergerda5ebe22012-05-23 07:58:11 +0000998 * Got a 802.2 packet. Check the other protocol field.
999 * XXX VLAN over 802.2+SNAP not implemented!
wdenk2d966952002-10-31 22:12:35 +00001000 */
Joe Hershberger8d353eb2012-05-23 07:58:12 +00001001 eth_proto = ntohs(et802->et_prot);
wdenka3d991b2004-04-15 21:48:45 +00001002
Joe Hershberger594c26f2012-05-23 07:58:04 +00001003 ip = (struct ip_udp_hdr *)(inpkt + E802_HDR_SIZE);
wdenk2d966952002-10-31 22:12:35 +00001004 len -= E802_HDR_SIZE;
wdenka3d991b2004-04-15 21:48:45 +00001005
Joe Hershberger8d353eb2012-05-23 07:58:12 +00001006 } else if (eth_proto != PROT_VLAN) { /* normal packet */
Joe Hershberger594c26f2012-05-23 07:58:04 +00001007 ip = (struct ip_udp_hdr *)(inpkt + ETHER_HDR_SIZE);
wdenk2d966952002-10-31 22:12:35 +00001008 len -= ETHER_HDR_SIZE;
wdenka3d991b2004-04-15 21:48:45 +00001009
1010 } else { /* VLAN packet */
Joe Hershbergerc68cca32012-05-23 07:58:07 +00001011 struct vlan_ethernet_hdr *vet =
1012 (struct vlan_ethernet_hdr *)et;
wdenka3d991b2004-04-15 21:48:45 +00001013
Joe Hershberger4ef8d532012-05-23 08:01:04 +00001014 debug_cond(DEBUG_NET_PKT, "VLAN packet received\n");
Robin Getz0ebf04c2009-07-23 03:01:03 -04001015
wdenka3d991b2004-04-15 21:48:45 +00001016 /* 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 Loeliger643d1ab2007-07-09 17:45:14 -05001022#if defined(CONFIG_CMD_CDP)
wdenka3d991b2004-04-15 21:48:45 +00001023 && iscdp == 0
1024#endif
1025 )
1026 return;
1027
1028 cti = ntohs(vet->vet_tag);
1029 vlanid = cti & VLAN_IDMASK;
Joe Hershberger8d353eb2012-05-23 07:58:12 +00001030 eth_proto = ntohs(vet->vet_type);
wdenka3d991b2004-04-15 21:48:45 +00001031
Joe Hershberger594c26f2012-05-23 07:58:04 +00001032 ip = (struct ip_udp_hdr *)(inpkt + VLAN_ETHER_HDR_SIZE);
wdenka3d991b2004-04-15 21:48:45 +00001033 len -= VLAN_ETHER_HDR_SIZE;
wdenk2d966952002-10-31 22:12:35 +00001034 }
1035
Joe Hershberger4ef8d532012-05-23 08:01:04 +00001036 debug_cond(DEBUG_NET_PKT, "Receive from protocol 0x%x\n", eth_proto);
wdenk2d966952002-10-31 22:12:35 +00001037
Jon Loeliger643d1ab2007-07-09 17:45:14 -05001038#if defined(CONFIG_CMD_CDP)
wdenka3d991b2004-04-15 21:48:45 +00001039 if (iscdp) {
Joe Hershberger0b4c5ff2012-05-23 07:58:13 +00001040 cdp_receive((uchar *)ip, len);
wdenka3d991b2004-04-15 21:48:45 +00001041 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 Hershberger8d353eb2012-05-23 07:58:12 +00001053 switch (eth_proto) {
wdenk2d966952002-10-31 22:12:35 +00001054
1055 case PROT_ARP:
Joe Hershbergerd280d3f2012-05-23 07:58:01 +00001056 ArpReceive(et, ip, len);
wdenk289f9322005-01-12 00:15:14 +00001057 break;
wdenk2d966952002-10-31 22:12:35 +00001058
Peter Tyserbf6cb242010-09-30 11:25:48 -05001059#ifdef CONFIG_CMD_RARP
wdenk2d966952002-10-31 22:12:35 +00001060 case PROT_RARP:
Joe Hershberger8b9c5322012-05-23 07:58:03 +00001061 rarp_receive(ip, len);
wdenk2d966952002-10-31 22:12:35 +00001062 break;
Peter Tyserbf6cb242010-09-30 11:25:48 -05001063#endif
wdenk2d966952002-10-31 22:12:35 +00001064 case PROT_IP:
Joe Hershberger4ef8d532012-05-23 08:01:04 +00001065 debug_cond(DEBUG_NET_PKT, "Got IP\n");
Alessandro Rubini5cfaa4e2009-08-07 13:58:56 +02001066 /* Before we start poking the header, make sure it is there */
Joe Hershberger594c26f2012-05-23 07:58:04 +00001067 if (len < IP_UDP_HDR_SIZE) {
1068 debug("len bad %d < %lu\n", len,
1069 (ulong)IP_UDP_HDR_SIZE);
wdenk2d966952002-10-31 22:12:35 +00001070 return;
1071 }
Alessandro Rubini5cfaa4e2009-08-07 13:58:56 +02001072 /* Check the packet length */
wdenk2d966952002-10-31 22:12:35 +00001073 if (len < ntohs(ip->ip_len)) {
Joe Hershberger4ef8d532012-05-23 08:01:04 +00001074 debug("len bad %d < %d\n", len, ntohs(ip->ip_len));
wdenk2d966952002-10-31 22:12:35 +00001075 return;
1076 }
1077 len = ntohs(ip->ip_len);
Joe Hershberger4ef8d532012-05-23 08:01:04 +00001078 debug_cond(DEBUG_NET_PKT, "len=%d, v=%02x\n",
1079 len, ip->ip_hl_v & 0xff);
Robin Getz0ebf04c2009-07-23 03:01:03 -04001080
Alessandro Rubini5cfaa4e2009-08-07 13:58:56 +02001081 /* Can't deal with anything except IPv4 */
Luca Ceresolid3c65b02011-05-04 02:40:43 +00001082 if ((ip->ip_hl_v & 0xf0) != 0x40)
wdenk2d966952002-10-31 22:12:35 +00001083 return;
Alessandro Rubini5cfaa4e2009-08-07 13:58:56 +02001084 /* Can't deal with IP options (headers != 20 bytes) */
Luca Ceresolid3c65b02011-05-04 02:40:43 +00001085 if ((ip->ip_hl_v & 0x0f) > 0x05)
Remy Bohmer6b52cfe2008-06-03 15:48:17 +02001086 return;
Alessandro Rubini5cfaa4e2009-08-07 13:58:56 +02001087 /* Check the Checksum of the header */
Simon Glass0da0fcd2015-01-19 22:16:08 -07001088 if (!ip_checksum_ok((uchar *)ip, IP_HDR_SIZE)) {
Joe Hershberger4ef8d532012-05-23 08:01:04 +00001089 debug("checksum bad\n");
wdenk2d966952002-10-31 22:12:35 +00001090 return;
1091 }
Alessandro Rubini5cfaa4e2009-08-07 13:58:56 +02001092 /* If it is not for us, ignore it */
Joe Hershberger8d353eb2012-05-23 07:58:12 +00001093 dst_ip = NetReadIP(&ip->ip_dst);
1094 if (NetOurIP && dst_ip != NetOurIP && dst_ip != 0xFFFFFFFF) {
David Updegraff53a5c422007-06-11 10:41:07 -05001095#ifdef CONFIG_MCAST_TFTP
Joe Hershberger8d353eb2012-05-23 07:58:12 +00001096 if (Mcast_addr != dst_ip)
David Updegraff53a5c422007-06-11 10:41:07 -05001097#endif
Luca Ceresolic819abe2011-05-04 02:40:46 +00001098 return;
wdenk2d966952002-10-31 22:12:35 +00001099 }
Luca Ceresoli03eb1292011-04-18 06:19:50 +00001100 /* Read source IP address for later use */
1101 src_ip = NetReadIP(&ip->ip_src);
wdenk2d966952002-10-31 22:12:35 +00001102 /*
Alessandro Rubini5cfaa4e2009-08-07 13:58:56 +02001103 * 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 Ceresoliccb9ebe2011-05-04 02:40:47 +00001107 ip = NetDefragment(ip, &len);
1108 if (!ip)
Alessandro Rubini5cfaa4e2009-08-07 13:58:56 +02001109 return;
1110 /*
wdenk2d966952002-10-31 22:12:35 +00001111 * watch for ICMP host redirects
1112 *
wdenk8bde7f72003-06-27 21:31:46 +00001113 * 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 :-)
wdenk2d966952002-10-31 22:12:35 +00001118 *
wdenk8bde7f72003-06-27 21:31:46 +00001119 * 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 Glass4793ee62011-10-24 18:00:01 +00001126 *
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.
wdenk2d966952002-10-31 22:12:35 +00001130 */
1131 if (ip->ip_p == IPPROTO_ICMP) {
Simon Glass8f79bb12011-10-24 18:00:00 +00001132 receive_icmp(ip, len, src_ip, et);
1133 return;
wdenk2d966952002-10-31 22:12:35 +00001134 } else if (ip->ip_p != IPPROTO_UDP) { /* Only UDP packets */
1135 return;
1136 }
1137
Joe Hershberger4ef8d532012-05-23 08:01:04 +00001138 debug_cond(DEBUG_DEV_PKT,
1139 "received UDP (to=%pI4, from=%pI4, len=%d)\n",
1140 &dst_ip, &src_ip, len);
1141
Stefan Roese8534bf92005-08-12 20:06:52 +02001142#ifdef CONFIG_UDP_CHECKSUM
1143 if (ip->udp_xsum != 0) {
Wolfgang Denkb2f50802005-08-12 23:43:12 +02001144 ulong xsum;
Stefan Roese8534bf92005-08-12 20:06:52 +02001145 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 Denkb2f50802005-08-12 23:43:12 +02001159 ushort sumdata;
Stefan Roese8534bf92005-08-12 20:06:52 +02001160
1161 sumdata = *sumptr++;
1162 xsum += ntohs(sumdata);
1163 sumlen -= 2;
1164 }
1165 if (sumlen > 0) {
Wolfgang Denkb2f50802005-08-12 23:43:12 +02001166 ushort sumdata;
Stefan Roese8534bf92005-08-12 20:06:52 +02001167
1168 sumdata = *(unsigned char *) sumptr;
Wolfgang Denkb2f50802005-08-12 23:43:12 +02001169 sumdata = (sumdata << 8) & 0xff00;
Stefan Roese8534bf92005-08-12 20:06:52 +02001170 xsum += sumdata;
1171 }
1172 while ((xsum >> 16) != 0) {
Luca Ceresoli3e38e422011-05-11 03:59:54 +00001173 xsum = (xsum & 0x0000ffff) +
1174 ((xsum >> 16) & 0x0000ffff);
Stefan Roese8534bf92005-08-12 20:06:52 +02001175 }
1176 if ((xsum != 0x00000000) && (xsum != 0x0000ffff)) {
Wolfgang Denk9b55a252008-07-11 01:16:00 +02001177 printf(" UDP wrong checksum %08lx %08x\n",
1178 xsum, ntohs(ip->udp_xsum));
Stefan Roese8534bf92005-08-12 20:06:52 +02001179 return;
1180 }
1181 }
1182#endif
1183
David Updegraff53a5c422007-06-11 10:41:07 -05001184
Hannes Petermaier1f9ce302014-06-13 06:25:29 +02001185#if defined (CONFIG_NETCONSOLE) && !(CONFIG_SPL_BUILD)
Joe Hershberger594c26f2012-05-23 07:58:04 +00001186 nc_input_packet((uchar *)ip + IP_UDP_HDR_SIZE,
Joe Hershberger8cab08e2012-10-03 09:14:03 +00001187 src_ip,
Joe Hershberger594c26f2012-05-23 07:58:04 +00001188 ntohs(ip->udp_dst),
1189 ntohs(ip->udp_src),
1190 ntohs(ip->udp_len) - UDP_HDR_SIZE);
wdenk68ceb292004-08-02 21:11:11 +00001191#endif
wdenk2d966952002-10-31 22:12:35 +00001192 /*
1193 * IP header OK. Pass the packet to the current handler.
1194 */
Joe Hershbergerece223b2012-05-23 07:59:15 +00001195 (*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);
wdenk2d966952002-10-31 22:12:35 +00001200 break;
1201 }
1202}
1203
1204
1205/**********************************************************************/
1206
Simon Glasse4bf0c52011-10-24 18:00:02 +00001207static int net_check_prereq(enum proto_t protocol)
wdenk2d966952002-10-31 22:12:35 +00001208{
1209 switch (protocol) {
wdenk6e592382004-04-18 17:39:38 +00001210 /* Fall through */
Jon Loeliger643d1ab2007-07-09 17:45:14 -05001211#if defined(CONFIG_CMD_PING)
wdenk73a8b272003-06-05 19:27:42 +00001212 case PING:
wdenk6e592382004-04-18 17:39:38 +00001213 if (NetPingIP == 0) {
Luca Ceresoli4f63acd2011-05-11 03:59:56 +00001214 puts("*** ERROR: ping address not given\n");
Luca Ceresoli92895de2011-05-04 02:40:45 +00001215 return 1;
wdenk6e592382004-04-18 17:39:38 +00001216 }
1217 goto common;
wdenk73a8b272003-06-05 19:27:42 +00001218#endif
Jon Loeliger643d1ab2007-07-09 17:45:14 -05001219#if defined(CONFIG_CMD_SNTP)
wdenkea287de2005-04-01 00:25:43 +00001220 case SNTP:
1221 if (NetNtpServerIP == 0) {
Luca Ceresoli4f63acd2011-05-11 03:59:56 +00001222 puts("*** ERROR: NTP server address not given\n");
Luca Ceresoli92895de2011-05-04 02:40:45 +00001223 return 1;
wdenkea287de2005-04-01 00:25:43 +00001224 }
1225 goto common;
1226#endif
Robin Getz1a32bf42009-07-20 14:53:54 -04001227#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 Loeliger643d1ab2007-07-09 17:45:14 -05001235#if defined(CONFIG_CMD_NFS)
wdenkcbd8a352004-02-24 02:00:03 +00001236 case NFS:
1237#endif
Simon Glasse4bf0c52011-10-24 18:00:02 +00001238 case TFTPGET:
Simon Glass1fb7cd42011-10-24 18:00:07 +00001239 case TFTPPUT:
wdenk6e592382004-04-18 17:39:38 +00001240 if (NetServerIP == 0) {
Luca Ceresoli4f63acd2011-05-11 03:59:56 +00001241 puts("*** ERROR: `serverip' not set\n");
Luca Ceresoli92895de2011-05-04 02:40:45 +00001242 return 1;
wdenk6e592382004-04-18 17:39:38 +00001243 }
Luca Ceresoli4f63acd2011-05-11 03:59:56 +00001244#if defined(CONFIG_CMD_PING) || defined(CONFIG_CMD_SNTP) || \
1245 defined(CONFIG_CMD_DNS)
1246common:
wdenk73a8b272003-06-05 19:27:42 +00001247#endif
Simon Guinot8b6bbe12011-05-01 23:38:40 +00001248 /* Fall through */
wdenk73a8b272003-06-05 19:27:42 +00001249
Simon Guinot8b6bbe12011-05-01 23:38:40 +00001250 case NETCONS:
Luca Ceresoli7a83af02011-05-17 00:03:40 +00001251 case TFTPSRV:
wdenk6e592382004-04-18 17:39:38 +00001252 if (NetOurIP == 0) {
Luca Ceresoli4f63acd2011-05-11 03:59:56 +00001253 puts("*** ERROR: `ipaddr' not set\n");
Luca Ceresoli92895de2011-05-04 02:40:45 +00001254 return 1;
wdenk6e592382004-04-18 17:39:38 +00001255 }
1256 /* Fall through */
wdenk2d966952002-10-31 22:12:35 +00001257
Peter Tyserbf6cb242010-09-30 11:25:48 -05001258#ifdef CONFIG_CMD_RARP
wdenk2d966952002-10-31 22:12:35 +00001259 case RARP:
Peter Tyserbf6cb242010-09-30 11:25:48 -05001260#endif
wdenk2d966952002-10-31 22:12:35 +00001261 case BOOTP:
wdenka3d991b2004-04-15 21:48:45 +00001262 case CDP:
Peter Tyserbf6cb242010-09-30 11:25:48 -05001263 case DHCP:
Joe Hershbergerd22c3382012-05-23 08:00:12 +00001264 case LINKLOCAL:
Luca Ceresoli4f63acd2011-05-11 03:59:56 +00001265 if (memcmp(NetOurEther, "\0\0\0\0\0\0", 6) == 0) {
Luca Ceresoli4f63acd2011-05-11 03:59:56 +00001266 int num = eth_get_dev_index();
wdenk2d966952002-10-31 22:12:35 +00001267
wdenk6e592382004-04-18 17:39:38 +00001268 switch (num) {
1269 case -1:
Luca Ceresoli4f63acd2011-05-11 03:59:56 +00001270 puts("*** ERROR: No ethernet found.\n");
Luca Ceresoli92895de2011-05-04 02:40:45 +00001271 return 1;
wdenk6e592382004-04-18 17:39:38 +00001272 case 0:
Luca Ceresoli4f63acd2011-05-11 03:59:56 +00001273 puts("*** ERROR: `ethaddr' not set\n");
wdenk2d966952002-10-31 22:12:35 +00001274 break;
wdenk6e592382004-04-18 17:39:38 +00001275 default:
Luca Ceresoli4f63acd2011-05-11 03:59:56 +00001276 printf("*** ERROR: `eth%daddr' not set\n",
wdenk2d966952002-10-31 22:12:35 +00001277 num);
1278 break;
wdenk2d966952002-10-31 22:12:35 +00001279 }
wdenk6e592382004-04-18 17:39:38 +00001280
Luca Ceresoli4f63acd2011-05-11 03:59:56 +00001281 NetStartAgain();
Luca Ceresoli92895de2011-05-04 02:40:45 +00001282 return 2;
wdenk6e592382004-04-18 17:39:38 +00001283 }
1284 /* Fall through */
1285 default:
Luca Ceresoli92895de2011-05-04 02:40:45 +00001286 return 0;
wdenk2d966952002-10-31 22:12:35 +00001287 }
Luca Ceresoli92895de2011-05-04 02:40:45 +00001288 return 0; /* OK */
wdenk2d966952002-10-31 22:12:35 +00001289}
1290/**********************************************************************/
1291
1292int
wdenka3d991b2004-04-15 21:48:45 +00001293NetEthHdrSize(void)
1294{
1295 ushort myvlanid;
wdenk2d966952002-10-31 22:12:35 +00001296
wdenka3d991b2004-04-15 21:48:45 +00001297 myvlanid = ntohs(NetOurVLAN);
1298 if (myvlanid == (ushort)-1)
1299 myvlanid = VLAN_NONE;
1300
Luca Ceresoli3e38e422011-05-11 03:59:54 +00001301 return ((myvlanid & VLAN_IDMASK) == VLAN_NONE) ? ETHER_HDR_SIZE :
1302 VLAN_ETHER_HDR_SIZE;
wdenka3d991b2004-04-15 21:48:45 +00001303}
1304
1305int
Joe Hershbergerdb288a92012-05-15 08:59:04 +00001306NetSetEther(uchar *xet, uchar * addr, uint prot)
wdenk2d966952002-10-31 22:12:35 +00001307{
Joe Hershbergercb487f52012-05-23 07:58:06 +00001308 struct ethernet_hdr *et = (struct ethernet_hdr *)xet;
wdenka3d991b2004-04-15 21:48:45 +00001309 ushort myvlanid;
1310
1311 myvlanid = ntohs(NetOurVLAN);
1312 if (myvlanid == (ushort)-1)
1313 myvlanid = VLAN_NONE;
wdenk2d966952002-10-31 22:12:35 +00001314
Luca Ceresoli4f63acd2011-05-11 03:59:56 +00001315 memcpy(et->et_dest, addr, 6);
1316 memcpy(et->et_src, NetOurEther, 6);
wdenka3d991b2004-04-15 21:48:45 +00001317 if ((myvlanid & VLAN_IDMASK) == VLAN_NONE) {
Luca Ceresolic819abe2011-05-04 02:40:46 +00001318 et->et_protlen = htons(prot);
wdenka3d991b2004-04-15 21:48:45 +00001319 return ETHER_HDR_SIZE;
1320 } else {
Joe Hershbergerc68cca32012-05-23 07:58:07 +00001321 struct vlan_ethernet_hdr *vet =
1322 (struct vlan_ethernet_hdr *)xet;
wdenk2d966952002-10-31 22:12:35 +00001323
wdenka3d991b2004-04-15 21:48:45 +00001324 vet->vet_vlan_type = htons(PROT_VLAN);
1325 vet->vet_tag = htons((0 << 5) | (myvlanid & VLAN_IDMASK));
1326 vet->vet_type = htons(prot);
1327 return VLAN_ETHER_HDR_SIZE;
1328 }
1329}
wdenk2d966952002-10-31 22:12:35 +00001330
Joe Hershbergere7111012012-05-23 07:59:16 +00001331int net_update_ether(struct ethernet_hdr *et, uchar *addr, uint prot)
1332{
1333 ushort protlen;
1334
1335 memcpy(et->et_dest, addr, 6);
1336 memcpy(et->et_src, NetOurEther, 6);
1337 protlen = ntohs(et->et_protlen);
1338 if (protlen == PROT_VLAN) {
1339 struct vlan_ethernet_hdr *vet =
1340 (struct vlan_ethernet_hdr *)et;
1341 vet->vet_type = htons(prot);
1342 return VLAN_ETHER_HDR_SIZE;
1343 } else if (protlen > 1514) {
1344 et->et_protlen = htons(prot);
1345 return ETHER_HDR_SIZE;
1346 } else {
1347 /* 802.2 + SNAP */
1348 struct e802_hdr *et802 = (struct e802_hdr *)et;
1349 et802->et_prot = htons(prot);
1350 return E802_HDR_SIZE;
1351 }
1352}
1353
Joe Hershberger4b11c912012-05-23 07:59:07 +00001354void net_set_ip_header(uchar *pkt, IPaddr_t dest, IPaddr_t source)
wdenk2d966952002-10-31 22:12:35 +00001355{
Joe Hershberger4b11c912012-05-23 07:59:07 +00001356 struct ip_udp_hdr *ip = (struct ip_udp_hdr *)pkt;
1357
1358 /*
1359 * Construct an IP header.
1360 */
1361 /* IP_HDR_SIZE / 4 (not including UDP) */
1362 ip->ip_hl_v = 0x45;
1363 ip->ip_tos = 0;
1364 ip->ip_len = htons(IP_HDR_SIZE);
1365 ip->ip_id = htons(NetIPID++);
1366 ip->ip_off = htons(IP_FLAGS_DFRAG); /* Don't fragment */
1367 ip->ip_ttl = 255;
1368 ip->ip_sum = 0;
1369 /* already in network byte order */
1370 NetCopyIP((void *)&ip->ip_src, &source);
1371 /* already in network byte order */
1372 NetCopyIP((void *)&ip->ip_dst, &dest);
1373}
1374
1375void net_set_udp_header(uchar *pkt, IPaddr_t dest, int dport, int sport,
1376 int len)
1377{
1378 struct ip_udp_hdr *ip = (struct ip_udp_hdr *)pkt;
wdenk2d966952002-10-31 22:12:35 +00001379
1380 /*
1381 * If the data is an odd number of bytes, zero the
1382 * byte after the last byte so that the checksum
1383 * will work.
1384 */
1385 if (len & 1)
Joe Hershberger4b11c912012-05-23 07:59:07 +00001386 pkt[IP_UDP_HDR_SIZE + len] = 0;
wdenk2d966952002-10-31 22:12:35 +00001387
Joe Hershberger4b11c912012-05-23 07:59:07 +00001388 net_set_ip_header(pkt, dest, NetOurIP);
Joe Hershberger594c26f2012-05-23 07:58:04 +00001389 ip->ip_len = htons(IP_UDP_HDR_SIZE + len);
Joe Hershberger4b11c912012-05-23 07:59:07 +00001390 ip->ip_p = IPPROTO_UDP;
Simon Glass0da0fcd2015-01-19 22:16:08 -07001391 ip->ip_sum = compute_ip_checksum(ip, IP_HDR_SIZE);
Joe Hershberger4b11c912012-05-23 07:59:07 +00001392
wdenk2d966952002-10-31 22:12:35 +00001393 ip->udp_src = htons(sport);
1394 ip->udp_dst = htons(dport);
Joe Hershberger594c26f2012-05-23 07:58:04 +00001395 ip->udp_len = htons(UDP_HDR_SIZE + len);
wdenk2d966952002-10-31 22:12:35 +00001396 ip->udp_xsum = 0;
wdenk2d966952002-10-31 22:12:35 +00001397}
1398
Luca Ceresoli4f63acd2011-05-11 03:59:56 +00001399void copy_filename(char *dst, const char *src, int size)
wdenk2d966952002-10-31 22:12:35 +00001400{
1401 if (*src && (*src == '"')) {
1402 ++src;
1403 --size;
1404 }
1405
Luca Ceresolid3c65b02011-05-04 02:40:43 +00001406 while ((--size > 0) && *src && (*src != '"'))
wdenk2d966952002-10-31 22:12:35 +00001407 *dst++ = *src++;
wdenk2d966952002-10-31 22:12:35 +00001408 *dst = '\0';
1409}
1410
Luca Ceresoli3e38e422011-05-11 03:59:54 +00001411#if defined(CONFIG_CMD_NFS) || \
1412 defined(CONFIG_CMD_SNTP) || \
1413 defined(CONFIG_CMD_DNS)
Robin Getz1a32bf42009-07-20 14:53:54 -04001414/*
Robin Getz97399462010-03-08 14:07:00 -05001415 * make port a little random (1024-17407)
1416 * This keeps the math somewhat trivial to compute, and seems to work with
1417 * all supported protocols/clients/servers
Robin Getz1a32bf42009-07-20 14:53:54 -04001418 */
1419unsigned int random_port(void)
1420{
Robin Getz97399462010-03-08 14:07:00 -05001421 return 1024 + (get_timer(0) % 0x4000);
Robin Getz1a32bf42009-07-20 14:53:54 -04001422}
1423#endif
1424
Luca Ceresoli4f63acd2011-05-11 03:59:56 +00001425void ip_to_string(IPaddr_t x, char *s)
wdenk2d966952002-10-31 22:12:35 +00001426{
Luca Ceresoli4f63acd2011-05-11 03:59:56 +00001427 x = ntohl(x);
1428 sprintf(s, "%d.%d.%d.%d",
1429 (int) ((x >> 24) & 0xff),
1430 (int) ((x >> 16) & 0xff),
1431 (int) ((x >> 8) & 0xff), (int) ((x >> 0) & 0xff)
wdenk6e592382004-04-18 17:39:38 +00001432 );
wdenk2d966952002-10-31 22:12:35 +00001433}
1434
wdenka3d991b2004-04-15 21:48:45 +00001435void VLAN_to_string(ushort x, char *s)
1436{
1437 x = ntohs(x);
1438
1439 if (x == (ushort)-1)
1440 x = VLAN_NONE;
1441
1442 if (x == VLAN_NONE)
1443 strcpy(s, "none");
1444 else
1445 sprintf(s, "%d", x & VLAN_IDMASK);
1446}
1447
Mike Frysinger2e3ef6e2010-10-20 07:16:48 -04001448ushort string_to_VLAN(const char *s)
wdenka3d991b2004-04-15 21:48:45 +00001449{
1450 ushort id;
1451
1452 if (s == NULL)
wdenkb9711de2004-04-25 13:18:40 +00001453 return htons(VLAN_NONE);
wdenka3d991b2004-04-15 21:48:45 +00001454
1455 if (*s < '0' || *s > '9')
1456 id = VLAN_NONE;
1457 else
1458 id = (ushort)simple_strtoul(s, NULL, 10);
1459
wdenkb9711de2004-04-25 13:18:40 +00001460 return htons(id);
wdenka3d991b2004-04-15 21:48:45 +00001461}
1462
wdenka3d991b2004-04-15 21:48:45 +00001463ushort getenv_VLAN(char *var)
1464{
Luca Ceresoli92895de2011-05-04 02:40:45 +00001465 return string_to_VLAN(getenv(var));
wdenka3d991b2004-04-15 21:48:45 +00001466}