blob: afec4433f07c8ea24a074c98bab7a1fb4aba7c92 [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];
Joe Hershberger2a504df2015-03-22 17:09:11 -0500186#ifdef CONFIG_DM_ETH
187/* Receive packets */
188uchar *net_rx_packets[PKTBUFSRX];
189#else
Luca Ceresoli3e38e422011-05-11 03:59:54 +0000190/* Receive packet */
Joe Hershbergerdb288a92012-05-15 08:59:04 +0000191uchar *NetRxPackets[PKTBUFSRX];
Joe Hershberger2a504df2015-03-22 17:09:11 -0500192#endif
Joe Hershbergerece223b2012-05-23 07:59:15 +0000193/* Current UDP RX packet handler */
194static rxhand_f *udp_packet_handler;
195/* Current ARP RX packet handler */
196static rxhand_f *arp_packet_handler;
Simon Glass39bccd22011-10-26 14:18:38 +0000197#ifdef CONFIG_CMD_TFTPPUT
Joe Hershbergerece223b2012-05-23 07:59:15 +0000198/* Current ICMP rx handler */
199static rxhand_icmp_f *packet_icmp_handler;
Simon Glass39bccd22011-10-26 14:18:38 +0000200#endif
Luca Ceresoli3e38e422011-05-11 03:59:54 +0000201/* Current timeout handler */
202static thand_f *timeHandler;
203/* Time base value */
204static ulong timeStart;
205/* Current timeout value */
206static ulong timeDelta;
207/* THE transmit packet */
Joe Hershbergerdb288a92012-05-15 08:59:04 +0000208uchar *NetTxPacket;
wdenk2d966952002-10-31 22:12:35 +0000209
Simon Glasse4bf0c52011-10-24 18:00:02 +0000210static int net_check_prereq(enum proto_t protocol);
wdenk2d966952002-10-31 22:12:35 +0000211
Remy Bohmer67b96e82009-10-28 22:13:39 +0100212static int NetTryCount;
213
Jim Linb63056d2013-08-13 19:03:05 +0800214int __maybe_unused net_busy_flag;
215
wdenk2d966952002-10-31 22:12:35 +0000216/**********************************************************************/
wdenk73a8b272003-06-05 19:27:42 +0000217
Joe Hershbergera9f51c92012-12-11 22:16:26 -0600218static int on_bootfile(const char *name, const char *value, enum env_op op,
219 int flags)
220{
221 switch (op) {
222 case env_op_create:
223 case env_op_overwrite:
224 copy_filename(BootFile, value, sizeof(BootFile));
225 break;
226 default:
227 break;
228 }
229
230 return 0;
231}
232U_BOOT_ENV_CALLBACK(bootfile, on_bootfile);
233
Simon Glasse4a3d572011-10-27 06:24:32 +0000234/*
235 * Check if autoload is enabled. If so, use either NFS or TFTP to download
236 * the boot file.
237 */
238void net_auto_load(void)
239{
Joe Hershbergerec8a2522012-12-11 22:16:22 -0600240#if defined(CONFIG_CMD_NFS)
Simon Glasse4a3d572011-10-27 06:24:32 +0000241 const char *s = getenv("autoload");
242
Joe Hershbergerec8a2522012-12-11 22:16:22 -0600243 if (s != NULL && strcmp(s, "NFS") == 0) {
244 /*
245 * Use NFS to load the bootfile.
246 */
247 NfsStart();
248 return;
249 }
Simon Glasse4a3d572011-10-27 06:24:32 +0000250#endif
Joe Hershbergerec8a2522012-12-11 22:16:22 -0600251 if (getenv_yesno("autoload") == 0) {
252 /*
253 * Just use BOOTP/RARP to configure system;
254 * Do not use TFTP to load the bootfile.
255 */
256 net_set_state(NETLOOP_SUCCESS);
257 return;
Simon Glasse4a3d572011-10-27 06:24:32 +0000258 }
259 TftpStart(TFTPGET);
260}
261
Joe Hershbergercb1c9912012-05-23 07:59:21 +0000262static void NetInitLoop(void)
Heiko Schocher2f70c492009-02-10 09:38:52 +0100263{
Luca Ceresolic586ce62011-05-11 03:59:55 +0000264 static int env_changed_id;
Luca Ceresoli4f63acd2011-05-11 03:59:56 +0000265 int env_id = get_env_id();
Heiko Schocher2f70c492009-02-10 09:38:52 +0100266
267 /* update only when the environment has changed */
Michael Zaidman3c172c42009-04-04 01:43:00 +0300268 if (env_changed_id != env_id) {
Enric Balletbo i Serra23a70bf2011-05-31 21:01:47 +0000269 NetOurIP = getenv_IPaddr("ipaddr");
Luca Ceresoli4f63acd2011-05-11 03:59:56 +0000270 NetOurGatewayIP = getenv_IPaddr("gatewayip");
271 NetOurSubnetMask = getenv_IPaddr("netmask");
272 NetServerIP = getenv_IPaddr("serverip");
Heiko Schocher2f70c492009-02-10 09:38:52 +0100273 NetOurNativeVLAN = getenv_VLAN("nvlan");
Michael Zaidman3c172c42009-04-04 01:43:00 +0300274 NetOurVLAN = getenv_VLAN("vlan");
Robin Getz1a32bf42009-07-20 14:53:54 -0400275#if defined(CONFIG_CMD_DNS)
276 NetOurDNSIP = getenv_IPaddr("dnsip");
277#endif
Michael Zaidman3c172c42009-04-04 01:43:00 +0300278 env_changed_id = env_id;
Heiko Schocher2f70c492009-02-10 09:38:52 +0100279 }
Jim Lin7315cfd2013-05-17 17:41:03 +0800280 if (eth_get_dev())
Joe Hershberger8b2c9a72015-03-22 17:09:00 -0500281 memcpy(NetOurEther, eth_get_ethaddr(), 6);
Michael Zaidman3c172c42009-04-04 01:43:00 +0300282
Heiko Schocherda954272009-04-28 08:36:11 +0200283 return;
Heiko Schocher2f70c492009-02-10 09:38:52 +0100284}
285
Joe Hershbergerece223b2012-05-23 07:59:15 +0000286static void net_clear_handlers(void)
287{
288 net_set_udp_handler(NULL);
289 net_set_arp_handler(NULL);
290 NetSetTimeout(0, NULL);
291}
292
293static void net_cleanup_loop(void)
294{
295 net_clear_handlers();
296}
297
Joe Hershberger46c495d2012-05-23 07:59:22 +0000298void net_init(void)
299{
300 static int first_call = 1;
301
302 if (first_call) {
303 /*
304 * Setup packet buffers, aligned correctly.
305 */
306 int i;
307
308 NetTxPacket = &PktBuf[0] + (PKTALIGN - 1);
309 NetTxPacket -= (ulong)NetTxPacket % PKTALIGN;
Joe Hershberger2a504df2015-03-22 17:09:11 -0500310#ifdef CONFIG_DM_ETH
311 for (i = 0; i < PKTBUFSRX; i++) {
312 net_rx_packets[i] = NetTxPacket + (i + 1) *
313 PKTSIZE_ALIGN;
314 }
315#else
Joe Hershberger46c495d2012-05-23 07:59:22 +0000316 for (i = 0; i < PKTBUFSRX; i++)
317 NetRxPackets[i] = NetTxPacket + (i + 1) * PKTSIZE_ALIGN;
Joe Hershberger2a504df2015-03-22 17:09:11 -0500318#endif
Joe Hershberger46c495d2012-05-23 07:59:22 +0000319 ArpInit();
320 net_clear_handlers();
321
322 /* Only need to setup buffer pointers once. */
323 first_call = 0;
324 }
325
326 NetInitLoop();
327}
328
wdenk73a8b272003-06-05 19:27:42 +0000329/**********************************************************************/
wdenk2d966952002-10-31 22:12:35 +0000330/*
331 * Main network processing loop.
332 */
333
Simon Glasse4bf0c52011-10-24 18:00:02 +0000334int NetLoop(enum proto_t protocol)
wdenk2d966952002-10-31 22:12:35 +0000335{
Simon Glass4793ee62011-10-24 18:00:01 +0000336 int ret = -1;
wdenk2d966952002-10-31 22:12:35 +0000337
wdenk2d966952002-10-31 22:12:35 +0000338 NetRestarted = 0;
339 NetDevExists = 0;
Remy Bohmer67b96e82009-10-28 22:13:39 +0100340 NetTryCount = 1;
Joe Hershberger4ef8d532012-05-23 08:01:04 +0000341 debug_cond(DEBUG_INT_STATE, "--- NetLoop Entry\n");
wdenk73a8b272003-06-05 19:27:42 +0000342
Simon Glass573f14f2011-12-10 11:08:06 +0000343 bootstage_mark_name(BOOTSTAGE_ID_ETH_START, "eth_start");
Joe Hershberger46c495d2012-05-23 07:59:22 +0000344 net_init();
Joe Hershbergerf8be7d62012-08-03 10:59:08 +0000345 if (eth_is_on_demand_init() || protocol != NETCONS) {
wdenkb1bf6f22005-04-03 14:52:59 +0000346 eth_halt();
Joe Hershbergerf8be7d62012-08-03 10:59:08 +0000347 eth_set_current();
Joe Hershbergerd2eaec62015-03-22 17:09:06 -0500348 if (eth_init() < 0) {
Joe Hershbergerf8be7d62012-08-03 10:59:08 +0000349 eth_halt();
350 return -1;
351 }
352 } else
Joe Hershbergerd2eaec62015-03-22 17:09:06 -0500353 eth_init_state_only();
wdenk2d966952002-10-31 22:12:35 +0000354
355restart:
Jim Linb63056d2013-08-13 19:03:05 +0800356#ifdef CONFIG_USB_KEYBOARD
357 net_busy_flag = 0;
358#endif
Joe Hershberger22f6e992012-05-23 07:59:14 +0000359 net_set_state(NETLOOP_CONTINUE);
wdenk2d966952002-10-31 22:12:35 +0000360
361 /*
362 * Start the ball rolling with the given start function. From
363 * here on, this code is a state machine driven by received
364 * packets and timer events.
365 */
Joe Hershberger4ef8d532012-05-23 08:01:04 +0000366 debug_cond(DEBUG_INT_STATE, "--- NetLoop Init\n");
Joe Hershbergercb1c9912012-05-23 07:59:21 +0000367 NetInitLoop();
wdenk2d966952002-10-31 22:12:35 +0000368
Luca Ceresoli4f63acd2011-05-11 03:59:56 +0000369 switch (net_check_prereq(protocol)) {
wdenk2d966952002-10-31 22:12:35 +0000370 case 1:
371 /* network not configured */
wdenkb1bf6f22005-04-03 14:52:59 +0000372 eth_halt();
Luca Ceresoli92895de2011-05-04 02:40:45 +0000373 return -1;
wdenk2d966952002-10-31 22:12:35 +0000374
wdenk2d966952002-10-31 22:12:35 +0000375 case 2:
376 /* network device not configured */
377 break;
wdenk2d966952002-10-31 22:12:35 +0000378
379 case 0:
wdenk2d966952002-10-31 22:12:35 +0000380 NetDevExists = 1;
Simon Glasse4bf0c52011-10-24 18:00:02 +0000381 NetBootFileXferSize = 0;
wdenk2d966952002-10-31 22:12:35 +0000382 switch (protocol) {
Simon Glasse4bf0c52011-10-24 18:00:02 +0000383 case TFTPGET:
Simon Glass1fb7cd42011-10-24 18:00:07 +0000384#ifdef CONFIG_CMD_TFTPPUT
385 case TFTPPUT:
386#endif
wdenk2d966952002-10-31 22:12:35 +0000387 /* always use ARP to get server ethernet address */
Simon Glasse4bf0c52011-10-24 18:00:02 +0000388 TftpStart(protocol);
wdenk2d966952002-10-31 22:12:35 +0000389 break;
Luca Ceresoli7a83af02011-05-17 00:03:40 +0000390#ifdef CONFIG_CMD_TFTPSRV
391 case TFTPSRV:
392 TftpStartServer();
393 break;
394#endif
Jon Loeliger643d1ab2007-07-09 17:45:14 -0500395#if defined(CONFIG_CMD_DHCP)
wdenk2d966952002-10-31 22:12:35 +0000396 case DHCP:
Stephen Warrenf59be6e2014-07-25 17:30:48 -0600397 BootpReset();
Michael Zaidman09133f82009-07-14 23:37:12 +0300398 NetOurIP = 0;
wdenk2d966952002-10-31 22:12:35 +0000399 DhcpRequest(); /* Basically same as BOOTP */
400 break;
Jon Loeliger610f2e92007-07-10 11:05:02 -0500401#endif
wdenk2d966952002-10-31 22:12:35 +0000402
403 case BOOTP:
Stephen Warrenf59be6e2014-07-25 17:30:48 -0600404 BootpReset();
Michael Zaidman09133f82009-07-14 23:37:12 +0300405 NetOurIP = 0;
Luca Ceresoli4f63acd2011-05-11 03:59:56 +0000406 BootpRequest();
wdenk2d966952002-10-31 22:12:35 +0000407 break;
408
Peter Tyserbf6cb242010-09-30 11:25:48 -0500409#if defined(CONFIG_CMD_RARP)
wdenk2d966952002-10-31 22:12:35 +0000410 case RARP:
411 RarpTry = 0;
Michael Zaidman09133f82009-07-14 23:37:12 +0300412 NetOurIP = 0;
Luca Ceresoli4f63acd2011-05-11 03:59:56 +0000413 RarpRequest();
wdenk2d966952002-10-31 22:12:35 +0000414 break;
Peter Tyserbf6cb242010-09-30 11:25:48 -0500415#endif
Jon Loeliger643d1ab2007-07-09 17:45:14 -0500416#if defined(CONFIG_CMD_PING)
wdenk73a8b272003-06-05 19:27:42 +0000417 case PING:
Joe Hershbergera36b12f2012-05-23 07:58:02 +0000418 ping_start();
wdenk73a8b272003-06-05 19:27:42 +0000419 break;
420#endif
Jon Loeliger643d1ab2007-07-09 17:45:14 -0500421#if defined(CONFIG_CMD_NFS)
wdenkcbd8a352004-02-24 02:00:03 +0000422 case NFS:
423 NfsStart();
424 break;
425#endif
Jon Loeliger643d1ab2007-07-09 17:45:14 -0500426#if defined(CONFIG_CMD_CDP)
wdenka3d991b2004-04-15 21:48:45 +0000427 case CDP:
428 CDPStart();
429 break;
430#endif
Hannes Petermaier1f9ce302014-06-13 06:25:29 +0200431#if defined (CONFIG_NETCONSOLE) && !(CONFIG_SPL_BUILD)
wdenk68ceb292004-08-02 21:11:11 +0000432 case NETCONS:
433 NcStart();
434 break;
435#endif
Jon Loeliger643d1ab2007-07-09 17:45:14 -0500436#if defined(CONFIG_CMD_SNTP)
wdenkea287de2005-04-01 00:25:43 +0000437 case SNTP:
438 SntpStart();
439 break;
440#endif
Robin Getz1a32bf42009-07-20 14:53:54 -0400441#if defined(CONFIG_CMD_DNS)
442 case DNS:
443 DnsStart();
444 break;
445#endif
Joe Hershbergerd22c3382012-05-23 08:00:12 +0000446#if defined(CONFIG_CMD_LINK_LOCAL)
447 case LINKLOCAL:
448 link_local_start();
449 break;
450#endif
wdenk2d966952002-10-31 22:12:35 +0000451 default:
452 break;
453 }
454
wdenk2d966952002-10-31 22:12:35 +0000455 break;
456 }
457
Jon Loeliger643d1ab2007-07-09 17:45:14 -0500458#if defined(CONFIG_MII) || defined(CONFIG_CMD_MII)
Luca Ceresoli3e38e422011-05-11 03:59:54 +0000459#if defined(CONFIG_SYS_FAULT_ECHO_LINK_DOWN) && \
460 defined(CONFIG_STATUS_LED) && \
461 defined(STATUS_LED_RED)
wdenkfc3e2162003-10-08 22:33:00 +0000462 /*
wdenk42d1f032003-10-15 23:53:47 +0000463 * Echo the inverted link state to the fault LED.
wdenkfc3e2162003-10-08 22:33:00 +0000464 */
Luca Ceresolid3c65b02011-05-04 02:40:43 +0000465 if (miiphy_link(eth_get_dev()->name, CONFIG_SYS_FAULT_MII_ADDR))
Luca Ceresoli4f63acd2011-05-11 03:59:56 +0000466 status_led_set(STATUS_LED_RED, STATUS_LED_OFF);
Luca Ceresolid3c65b02011-05-04 02:40:43 +0000467 else
Luca Ceresoli4f63acd2011-05-11 03:59:56 +0000468 status_led_set(STATUS_LED_RED, STATUS_LED_ON);
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200469#endif /* CONFIG_SYS_FAULT_ECHO_LINK_DOWN, ... */
wdenkfc3e2162003-10-08 22:33:00 +0000470#endif /* CONFIG_MII, ... */
Jim Linb63056d2013-08-13 19:03:05 +0800471#ifdef CONFIG_USB_KEYBOARD
472 net_busy_flag = 1;
473#endif
wdenk2d966952002-10-31 22:12:35 +0000474
475 /*
476 * Main packet reception loop. Loop receiving packets until
Joe Hershberger22f6e992012-05-23 07:59:14 +0000477 * someone sets `net_state' to a state that terminates.
wdenk2d966952002-10-31 22:12:35 +0000478 */
479 for (;;) {
480 WATCHDOG_RESET();
481#ifdef CONFIG_SHOW_ACTIVITY
Joe Hershberger48522bb2012-05-15 08:59:08 +0000482 show_activity(1);
wdenk2d966952002-10-31 22:12:35 +0000483#endif
484 /*
485 * Check the ethernet for a new packet. The ethernet
486 * receive routine will process it.
487 */
Guennadi Liakhovetski40cb90e2008-04-03 17:04:19 +0200488 eth_rx();
wdenk2d966952002-10-31 22:12:35 +0000489
490 /*
491 * Abort if ctrl-c was pressed.
492 */
493 if (ctrlc()) {
Joe Hershbergere94070c2012-05-23 07:59:24 +0000494 /* cancel any ARP that may not have completed */
495 NetArpWaitPacketIP = 0;
496
Joe Hershbergerece223b2012-05-23 07:59:15 +0000497 net_cleanup_loop();
wdenk8bde7f72003-06-27 21:31:46 +0000498 eth_halt();
Joe Hershbergerf8be7d62012-08-03 10:59:08 +0000499 /* Invalidate the last protocol */
500 eth_set_last_protocol(BOOTP);
501
Luca Ceresoli4f63acd2011-05-11 03:59:56 +0000502 puts("\nAbort\n");
Joe Hershberger4ef8d532012-05-23 08:01:04 +0000503 /* include a debug print as well incase the debug
504 messages are directed to stderr */
505 debug_cond(DEBUG_INT_STATE, "--- NetLoop Abort!\n");
Simon Glass4793ee62011-10-24 18:00:01 +0000506 goto done;
wdenk2d966952002-10-31 22:12:35 +0000507 }
508
wdenk73a8b272003-06-05 19:27:42 +0000509 ArpTimeoutCheck();
wdenk2d966952002-10-31 22:12:35 +0000510
511 /*
512 * Check for a timeout, and run the timeout handler
513 * if we have one.
514 */
wdenke0ac62d2003-08-17 18:55:18 +0000515 if (timeHandler && ((get_timer(0) - timeStart) > timeDelta)) {
wdenk2d966952002-10-31 22:12:35 +0000516 thand_f *x;
517
Jon Loeliger643d1ab2007-07-09 17:45:14 -0500518#if defined(CONFIG_MII) || defined(CONFIG_CMD_MII)
Luca Ceresoli4f63acd2011-05-11 03:59:56 +0000519#if defined(CONFIG_SYS_FAULT_ECHO_LINK_DOWN) && \
520 defined(CONFIG_STATUS_LED) && \
521 defined(STATUS_LED_RED)
wdenkfc3e2162003-10-08 22:33:00 +0000522 /*
wdenk42d1f032003-10-15 23:53:47 +0000523 * Echo the inverted link state to the fault LED.
wdenkfc3e2162003-10-08 22:33:00 +0000524 */
Luca Ceresoli4f63acd2011-05-11 03:59:56 +0000525 if (miiphy_link(eth_get_dev()->name,
Luca Ceresoli3e38e422011-05-11 03:59:54 +0000526 CONFIG_SYS_FAULT_MII_ADDR)) {
Luca Ceresoli4f63acd2011-05-11 03:59:56 +0000527 status_led_set(STATUS_LED_RED, STATUS_LED_OFF);
wdenkfc3e2162003-10-08 22:33:00 +0000528 } else {
Luca Ceresoli4f63acd2011-05-11 03:59:56 +0000529 status_led_set(STATUS_LED_RED, STATUS_LED_ON);
wdenkfc3e2162003-10-08 22:33:00 +0000530 }
Luca Ceresoli4f63acd2011-05-11 03:59:56 +0000531#endif /* CONFIG_SYS_FAULT_ECHO_LINK_DOWN, ... */
wdenkfc3e2162003-10-08 22:33:00 +0000532#endif /* CONFIG_MII, ... */
Joe Hershberger4ef8d532012-05-23 08:01:04 +0000533 debug_cond(DEBUG_INT_STATE, "--- NetLoop timeout\n");
wdenk2d966952002-10-31 22:12:35 +0000534 x = timeHandler;
535 timeHandler = (thand_f *)0;
536 (*x)();
537 }
538
Joe Hershberger5c421332015-03-22 17:09:07 -0500539 if (net_state == NETLOOP_FAIL)
540 NetStartAgain();
wdenk2d966952002-10-31 22:12:35 +0000541
Joe Hershberger22f6e992012-05-23 07:59:14 +0000542 switch (net_state) {
wdenk2d966952002-10-31 22:12:35 +0000543
544 case NETLOOP_RESTART:
wdenk2d966952002-10-31 22:12:35 +0000545 NetRestarted = 1;
wdenk2d966952002-10-31 22:12:35 +0000546 goto restart;
547
548 case NETLOOP_SUCCESS:
Joe Hershbergerece223b2012-05-23 07:59:15 +0000549 net_cleanup_loop();
wdenk2d966952002-10-31 22:12:35 +0000550 if (NetBootFileXferSize > 0) {
wdenk2d966952002-10-31 22:12:35 +0000551 printf("Bytes transferred = %ld (%lx hex)\n",
552 NetBootFileXferSize,
553 NetBootFileXferSize);
Simon Glass978226d2013-02-24 17:33:24 +0000554 setenv_hex("filesize", NetBootFileXferSize);
555 setenv_hex("fileaddr", load_addr);
wdenk2d966952002-10-31 22:12:35 +0000556 }
Joe Hershbergerf8be7d62012-08-03 10:59:08 +0000557 if (protocol != NETCONS)
558 eth_halt();
559 else
560 eth_halt_state_only();
561
562 eth_set_last_protocol(protocol);
563
Simon Glass4793ee62011-10-24 18:00:01 +0000564 ret = NetBootFileXferSize;
Joe Hershberger4ef8d532012-05-23 08:01:04 +0000565 debug_cond(DEBUG_INT_STATE, "--- NetLoop Success!\n");
Simon Glass4793ee62011-10-24 18:00:01 +0000566 goto done;
wdenk2d966952002-10-31 22:12:35 +0000567
568 case NETLOOP_FAIL:
Joe Hershbergerece223b2012-05-23 07:59:15 +0000569 net_cleanup_loop();
Joe Hershbergerf8be7d62012-08-03 10:59:08 +0000570 /* Invalidate the last protocol */
571 eth_set_last_protocol(BOOTP);
Joe Hershberger4ef8d532012-05-23 08:01:04 +0000572 debug_cond(DEBUG_INT_STATE, "--- NetLoop Fail!\n");
Simon Glass4793ee62011-10-24 18:00:01 +0000573 goto done;
Joe Hershberger22f6e992012-05-23 07:59:14 +0000574
575 case NETLOOP_CONTINUE:
576 continue;
wdenk2d966952002-10-31 22:12:35 +0000577 }
578 }
Simon Glass4793ee62011-10-24 18:00:01 +0000579
580done:
Jim Linb63056d2013-08-13 19:03:05 +0800581#ifdef CONFIG_USB_KEYBOARD
582 net_busy_flag = 0;
583#endif
Simon Glass39bccd22011-10-26 14:18:38 +0000584#ifdef CONFIG_CMD_TFTPPUT
Simon Glass4793ee62011-10-24 18:00:01 +0000585 /* Clear out the handlers */
Joe Hershbergerece223b2012-05-23 07:59:15 +0000586 net_set_udp_handler(NULL);
Simon Glass4793ee62011-10-24 18:00:01 +0000587 net_set_icmp_handler(NULL);
Simon Glass39bccd22011-10-26 14:18:38 +0000588#endif
Simon Glass4793ee62011-10-24 18:00:01 +0000589 return ret;
wdenk2d966952002-10-31 22:12:35 +0000590}
591
592/**********************************************************************/
593
594static void
595startAgainTimeout(void)
596{
Joe Hershberger22f6e992012-05-23 07:59:14 +0000597 net_set_state(NETLOOP_RESTART);
wdenk2d966952002-10-31 22:12:35 +0000598}
599
Luca Ceresoli4f63acd2011-05-11 03:59:56 +0000600void NetStartAgain(void)
wdenk2d966952002-10-31 22:12:35 +0000601{
wdenk6e592382004-04-18 17:39:38 +0000602 char *nretry;
Remy Bohmer67b96e82009-10-28 22:13:39 +0100603 int retry_forever = 0;
604 unsigned long retrycnt = 0;
wdenka3d991b2004-04-15 21:48:45 +0000605
Remy Bohmer67b96e82009-10-28 22:13:39 +0100606 nretry = getenv("netretry");
607 if (nretry) {
608 if (!strcmp(nretry, "yes"))
609 retry_forever = 1;
610 else if (!strcmp(nretry, "no"))
611 retrycnt = 0;
612 else if (!strcmp(nretry, "once"))
613 retrycnt = 1;
614 else
615 retrycnt = simple_strtoul(nretry, NULL, 0);
Joe Hershberger5c421332015-03-22 17:09:07 -0500616 } else {
617 retrycnt = 0;
618 retry_forever = 0;
619 }
Remy Bohmer67b96e82009-10-28 22:13:39 +0100620
621 if ((!retry_forever) && (NetTryCount >= retrycnt)) {
622 eth_halt();
Joe Hershberger22f6e992012-05-23 07:59:14 +0000623 net_set_state(NETLOOP_FAIL);
wdenka3d991b2004-04-15 21:48:45 +0000624 return;
625 }
Remy Bohmer67b96e82009-10-28 22:13:39 +0100626
627 NetTryCount++;
628
Luca Ceresoli4f63acd2011-05-11 03:59:56 +0000629 eth_halt();
Matthias Fuchs8b0c5c12007-12-27 16:58:41 +0100630#if !defined(CONFIG_NET_DO_NOT_TRY_ANOTHER)
Luca Ceresoli4f63acd2011-05-11 03:59:56 +0000631 eth_try_another(!NetRestarted);
Matthias Fuchs8b0c5c12007-12-27 16:58:41 +0100632#endif
Joe Hershbergerd2eaec62015-03-22 17:09:06 -0500633 eth_init();
wdenk6e592382004-04-18 17:39:38 +0000634 if (NetRestartWrap) {
wdenk2d966952002-10-31 22:12:35 +0000635 NetRestartWrap = 0;
Remy Bohmer67b96e82009-10-28 22:13:39 +0100636 if (NetDevExists) {
Luca Ceresoli4f63acd2011-05-11 03:59:56 +0000637 NetSetTimeout(10000UL, startAgainTimeout);
Joe Hershbergerece223b2012-05-23 07:59:15 +0000638 net_set_udp_handler(NULL);
wdenk6e592382004-04-18 17:39:38 +0000639 } else {
Joe Hershberger22f6e992012-05-23 07:59:14 +0000640 net_set_state(NETLOOP_FAIL);
wdenk2d966952002-10-31 22:12:35 +0000641 }
wdenk6e592382004-04-18 17:39:38 +0000642 } else {
Joe Hershberger22f6e992012-05-23 07:59:14 +0000643 net_set_state(NETLOOP_RESTART);
wdenk2d966952002-10-31 22:12:35 +0000644 }
wdenk2d966952002-10-31 22:12:35 +0000645}
646
647/**********************************************************************/
648/*
649 * Miscelaneous bits.
650 */
651
Joe Hershbergerece223b2012-05-23 07:59:15 +0000652static void dummy_handler(uchar *pkt, unsigned dport,
653 IPaddr_t sip, unsigned sport,
654 unsigned len)
Joe Hershbergerd280d3f2012-05-23 07:58:01 +0000655{
Joe Hershbergerd280d3f2012-05-23 07:58:01 +0000656}
657
Joe Hershbergerece223b2012-05-23 07:59:15 +0000658rxhand_f *net_get_udp_handler(void)
wdenk2d966952002-10-31 22:12:35 +0000659{
Joe Hershbergerece223b2012-05-23 07:59:15 +0000660 return udp_packet_handler;
661}
662
663void net_set_udp_handler(rxhand_f *f)
664{
Joe Hershberger4ef8d532012-05-23 08:01:04 +0000665 debug_cond(DEBUG_INT_STATE, "--- NetLoop UDP handler set (%p)\n", f);
Joe Hershbergerece223b2012-05-23 07:59:15 +0000666 if (f == NULL)
667 udp_packet_handler = dummy_handler;
668 else
669 udp_packet_handler = f;
670}
671
672rxhand_f *net_get_arp_handler(void)
673{
674 return arp_packet_handler;
675}
676
677void net_set_arp_handler(rxhand_f *f)
678{
Joe Hershberger4ef8d532012-05-23 08:01:04 +0000679 debug_cond(DEBUG_INT_STATE, "--- NetLoop ARP handler set (%p)\n", f);
Joe Hershbergerece223b2012-05-23 07:59:15 +0000680 if (f == NULL)
681 arp_packet_handler = dummy_handler;
682 else
683 arp_packet_handler = f;
wdenk2d966952002-10-31 22:12:35 +0000684}
685
Simon Glass39bccd22011-10-26 14:18:38 +0000686#ifdef CONFIG_CMD_TFTPPUT
Simon Glass4793ee62011-10-24 18:00:01 +0000687void net_set_icmp_handler(rxhand_icmp_f *f)
688{
689 packet_icmp_handler = f;
690}
Simon Glass39bccd22011-10-26 14:18:38 +0000691#endif
wdenk2d966952002-10-31 22:12:35 +0000692
693void
Luca Ceresoli6b147d12011-05-04 02:40:44 +0000694NetSetTimeout(ulong iv, thand_f *f)
wdenk2d966952002-10-31 22:12:35 +0000695{
696 if (iv == 0) {
Joe Hershberger4ef8d532012-05-23 08:01:04 +0000697 debug_cond(DEBUG_INT_STATE,
698 "--- NetLoop timeout handler cancelled\n");
wdenk2d966952002-10-31 22:12:35 +0000699 timeHandler = (thand_f *)0;
700 } else {
Joe Hershberger4ef8d532012-05-23 08:01:04 +0000701 debug_cond(DEBUG_INT_STATE,
702 "--- NetLoop timeout handler set (%p)\n", f);
wdenk2d966952002-10-31 22:12:35 +0000703 timeHandler = f;
wdenke0ac62d2003-08-17 18:55:18 +0000704 timeStart = get_timer(0);
Tetsuyuki Kobayashi1389f982012-06-25 02:37:27 +0000705 timeDelta = iv * CONFIG_SYS_HZ / 1000;
wdenk2d966952002-10-31 22:12:35 +0000706 }
707}
708
Joe Hershberger206d07f2012-05-23 07:58:10 +0000709int NetSendUDPPacket(uchar *ether, IPaddr_t dest, int dport, int sport,
710 int payload_len)
wdenk73a8b272003-06-05 19:27:42 +0000711{
wdenka3d991b2004-04-15 21:48:45 +0000712 uchar *pkt;
Joe Hershberger92146372012-05-23 07:59:08 +0000713 int eth_hdr_size;
714 int pkt_hdr_size;
wdenka3d991b2004-04-15 21:48:45 +0000715
Joe Hershberger46c495d2012-05-23 07:59:22 +0000716 /* make sure the NetTxPacket is initialized (NetInit() was called) */
717 assert(NetTxPacket != NULL);
718 if (NetTxPacket == NULL)
719 return -1;
720
wdenk73a8b272003-06-05 19:27:42 +0000721 /* convert to new style broadcast */
722 if (dest == 0)
723 dest = 0xFFFFFFFF;
wdenk2d966952002-10-31 22:12:35 +0000724
wdenk73a8b272003-06-05 19:27:42 +0000725 /* if broadcast, make the ether address a broadcast and don't do ARP */
726 if (dest == 0xFFFFFFFF)
727 ether = NetBcastAddr;
728
Joe Hershbergere94070c2012-05-23 07:59:24 +0000729 pkt = (uchar *)NetTxPacket;
wdenk73a8b272003-06-05 19:27:42 +0000730
Joe Hershberger92146372012-05-23 07:59:08 +0000731 eth_hdr_size = NetSetEther(pkt, ether, PROT_IP);
732 pkt += eth_hdr_size;
733 net_set_udp_header(pkt, dest, dport, sport, payload_len);
734 pkt_hdr_size = eth_hdr_size + IP_UDP_HDR_SIZE;
Robin Getz0ebf04c2009-07-23 03:01:03 -0400735
Joe Hershbergere94070c2012-05-23 07:59:24 +0000736 /* if MAC address was not discovered yet, do an ARP request */
737 if (memcmp(ether, NetEtherNullAddr, 6) == 0) {
Joe Hershberger4ef8d532012-05-23 08:01:04 +0000738 debug_cond(DEBUG_DEV_PKT, "sending ARP for %pI4\n", &dest);
Joe Hershberger92146372012-05-23 07:59:08 +0000739
740 /* save the ip and eth addr for the packet to send after arp */
wdenk73a8b272003-06-05 19:27:42 +0000741 NetArpWaitPacketIP = dest;
742 NetArpWaitPacketMAC = ether;
wdenka3d991b2004-04-15 21:48:45 +0000743
wdenk73a8b272003-06-05 19:27:42 +0000744 /* size of the waiting packet */
Joe Hershberger92146372012-05-23 07:59:08 +0000745 NetArpWaitTxPacketSize = pkt_hdr_size + payload_len;
wdenk73a8b272003-06-05 19:27:42 +0000746
747 /* and do the ARP request */
748 NetArpWaitTry = 1;
749 NetArpWaitTimerStart = get_timer(0);
750 ArpRequest();
751 return 1; /* waiting */
Joe Hershberger92146372012-05-23 07:59:08 +0000752 } else {
Joe Hershberger4ef8d532012-05-23 08:01:04 +0000753 debug_cond(DEBUG_DEV_PKT, "sending UDP to %pI4/%pM\n",
754 &dest, ether);
Joe Hershbergeradf5d932012-05-23 07:59:13 +0000755 NetSendPacket(NetTxPacket, pkt_hdr_size + payload_len);
Joe Hershberger92146372012-05-23 07:59:08 +0000756 return 0; /* transmitted */
wdenk73a8b272003-06-05 19:27:42 +0000757 }
wdenk73a8b272003-06-05 19:27:42 +0000758}
759
Alessandro Rubini5cfaa4e2009-08-07 13:58:56 +0200760#ifdef CONFIG_IP_DEFRAG
761/*
762 * This function collects fragments in a single packet, according
763 * to the algorithm in RFC815. It returns NULL or the pointer to
764 * a complete packet, in static storage
765 */
766#ifndef CONFIG_NET_MAXDEFRAG
767#define CONFIG_NET_MAXDEFRAG 16384
768#endif
769/*
770 * MAXDEFRAG, above, is chosen in the config file and is real data
771 * so we need to add the NFS overhead, which is more than TFTP.
772 * To use sizeof in the internal unnamed structures, we need a real
773 * instance (can't do "sizeof(struct rpc_t.u.reply))", unfortunately).
774 * The compiler doesn't complain nor allocates the actual structure
775 */
776static struct rpc_t rpc_specimen;
777#define IP_PKTSIZE (CONFIG_NET_MAXDEFRAG + sizeof(rpc_specimen.u.reply))
778
Joe Hershbergerc5c59df2012-05-23 07:58:05 +0000779#define IP_MAXUDP (IP_PKTSIZE - IP_HDR_SIZE)
Alessandro Rubini5cfaa4e2009-08-07 13:58:56 +0200780
781/*
782 * this is the packet being assembled, either data or frag control.
783 * Fragments go by 8 bytes, so this union must be 8 bytes long
784 */
785struct hole {
786 /* first_byte is address of this structure */
787 u16 last_byte; /* last byte in this hole + 1 (begin of next hole) */
788 u16 next_hole; /* index of next (in 8-b blocks), 0 == none */
789 u16 prev_hole; /* index of prev, 0 == none */
790 u16 unused;
791};
792
Joe Hershberger594c26f2012-05-23 07:58:04 +0000793static struct ip_udp_hdr *__NetDefragment(struct ip_udp_hdr *ip, int *lenp)
Alessandro Rubini5cfaa4e2009-08-07 13:58:56 +0200794{
Joe Hershberger48522bb2012-05-15 08:59:08 +0000795 static uchar pkt_buff[IP_PKTSIZE] __aligned(PKTALIGN);
Alessandro Rubini5cfaa4e2009-08-07 13:58:56 +0200796 static u16 first_hole, total_len;
797 struct hole *payload, *thisfrag, *h, *newh;
Joe Hershberger594c26f2012-05-23 07:58:04 +0000798 struct ip_udp_hdr *localip = (struct ip_udp_hdr *)pkt_buff;
Alessandro Rubini5cfaa4e2009-08-07 13:58:56 +0200799 uchar *indata = (uchar *)ip;
800 int offset8, start, len, done = 0;
801 u16 ip_off = ntohs(ip->ip_off);
802
803 /* payload starts after IP header, this fragment is in there */
Joe Hershbergerc5c59df2012-05-23 07:58:05 +0000804 payload = (struct hole *)(pkt_buff + IP_HDR_SIZE);
Alessandro Rubini5cfaa4e2009-08-07 13:58:56 +0200805 offset8 = (ip_off & IP_OFFS);
806 thisfrag = payload + offset8;
807 start = offset8 * 8;
Joe Hershbergerc5c59df2012-05-23 07:58:05 +0000808 len = ntohs(ip->ip_len) - IP_HDR_SIZE;
Alessandro Rubini5cfaa4e2009-08-07 13:58:56 +0200809
810 if (start + len > IP_MAXUDP) /* fragment extends too far */
811 return NULL;
812
813 if (!total_len || localip->ip_id != ip->ip_id) {
814 /* new (or different) packet, reset structs */
815 total_len = 0xffff;
816 payload[0].last_byte = ~0;
817 payload[0].next_hole = 0;
818 payload[0].prev_hole = 0;
819 first_hole = 0;
820 /* any IP header will work, copy the first we received */
Joe Hershbergerc5c59df2012-05-23 07:58:05 +0000821 memcpy(localip, ip, IP_HDR_SIZE);
Alessandro Rubini5cfaa4e2009-08-07 13:58:56 +0200822 }
823
824 /*
825 * What follows is the reassembly algorithm. We use the payload
826 * array as a linked list of hole descriptors, as each hole starts
827 * at a multiple of 8 bytes. However, last byte can be whatever value,
828 * so it is represented as byte count, not as 8-byte blocks.
829 */
830
831 h = payload + first_hole;
832 while (h->last_byte < start) {
833 if (!h->next_hole) {
834 /* no hole that far away */
835 return NULL;
836 }
837 h = payload + h->next_hole;
838 }
839
Fillod Stephanee397e592010-06-11 19:26:43 +0200840 /* last fragment may be 1..7 bytes, the "+7" forces acceptance */
841 if (offset8 + ((len + 7) / 8) <= h - payload) {
Alessandro Rubini5cfaa4e2009-08-07 13:58:56 +0200842 /* no overlap with holes (dup fragment?) */
843 return NULL;
844 }
845
846 if (!(ip_off & IP_FLAGS_MFRAG)) {
847 /* no more fragmentss: truncate this (last) hole */
848 total_len = start + len;
849 h->last_byte = start + len;
850 }
851
852 /*
853 * There is some overlap: fix the hole list. This code doesn't
854 * deal with a fragment that overlaps with two different holes
855 * (thus being a superset of a previously-received fragment).
856 */
857
Luca Ceresoli4f63acd2011-05-11 03:59:56 +0000858 if ((h >= thisfrag) && (h->last_byte <= start + len)) {
Alessandro Rubini5cfaa4e2009-08-07 13:58:56 +0200859 /* complete overlap with hole: remove hole */
860 if (!h->prev_hole && !h->next_hole) {
861 /* last remaining hole */
862 done = 1;
863 } else if (!h->prev_hole) {
864 /* first hole */
865 first_hole = h->next_hole;
866 payload[h->next_hole].prev_hole = 0;
867 } else if (!h->next_hole) {
868 /* last hole */
869 payload[h->prev_hole].next_hole = 0;
870 } else {
871 /* in the middle of the list */
872 payload[h->next_hole].prev_hole = h->prev_hole;
873 payload[h->prev_hole].next_hole = h->next_hole;
874 }
875
876 } else if (h->last_byte <= start + len) {
877 /* overlaps with final part of the hole: shorten this hole */
878 h->last_byte = start;
879
880 } else if (h >= thisfrag) {
881 /* overlaps with initial part of the hole: move this hole */
882 newh = thisfrag + (len / 8);
883 *newh = *h;
884 h = newh;
885 if (h->next_hole)
886 payload[h->next_hole].prev_hole = (h - payload);
887 if (h->prev_hole)
888 payload[h->prev_hole].next_hole = (h - payload);
889 else
890 first_hole = (h - payload);
891
892 } else {
893 /* fragment sits in the middle: split the hole */
894 newh = thisfrag + (len / 8);
895 *newh = *h;
896 h->last_byte = start;
897 h->next_hole = (newh - payload);
898 newh->prev_hole = (h - payload);
899 if (newh->next_hole)
900 payload[newh->next_hole].prev_hole = (newh - payload);
901 }
902
903 /* finally copy this fragment and possibly return whole packet */
Joe Hershbergerc5c59df2012-05-23 07:58:05 +0000904 memcpy((uchar *)thisfrag, indata + IP_HDR_SIZE, len);
Alessandro Rubini5cfaa4e2009-08-07 13:58:56 +0200905 if (!done)
906 return NULL;
907
908 localip->ip_len = htons(total_len);
Joe Hershbergerc5c59df2012-05-23 07:58:05 +0000909 *lenp = total_len + IP_HDR_SIZE;
Alessandro Rubini5cfaa4e2009-08-07 13:58:56 +0200910 return localip;
911}
912
Joe Hershberger594c26f2012-05-23 07:58:04 +0000913static inline struct ip_udp_hdr *NetDefragment(struct ip_udp_hdr *ip, int *lenp)
Alessandro Rubini5cfaa4e2009-08-07 13:58:56 +0200914{
915 u16 ip_off = ntohs(ip->ip_off);
916 if (!(ip_off & (IP_OFFS | IP_FLAGS_MFRAG)))
917 return ip; /* not a fragment */
918 return __NetDefragment(ip, lenp);
919}
920
921#else /* !CONFIG_IP_DEFRAG */
922
Joe Hershberger594c26f2012-05-23 07:58:04 +0000923static inline struct ip_udp_hdr *NetDefragment(struct ip_udp_hdr *ip, int *lenp)
Alessandro Rubini5cfaa4e2009-08-07 13:58:56 +0200924{
925 u16 ip_off = ntohs(ip->ip_off);
926 if (!(ip_off & (IP_OFFS | IP_FLAGS_MFRAG)))
927 return ip; /* not a fragment */
928 return NULL;
929}
930#endif
wdenka3d991b2004-04-15 21:48:45 +0000931
Simon Glass8f79bb12011-10-24 18:00:00 +0000932/**
933 * Receive an ICMP packet. We deal with REDIRECT and PING here, and silently
934 * drop others.
935 *
936 * @parma ip IP packet containing the ICMP
937 */
Joe Hershberger594c26f2012-05-23 07:58:04 +0000938static void receive_icmp(struct ip_udp_hdr *ip, int len,
Joe Hershbergercb487f52012-05-23 07:58:06 +0000939 IPaddr_t src_ip, struct ethernet_hdr *et)
Simon Glass8f79bb12011-10-24 18:00:00 +0000940{
Joe Hershbergere0a63072012-05-23 07:58:09 +0000941 struct icmp_hdr *icmph = (struct icmp_hdr *)&ip->udp_src;
Simon Glass8f79bb12011-10-24 18:00:00 +0000942
943 switch (icmph->type) {
944 case ICMP_REDIRECT:
945 if (icmph->code != ICMP_REDIR_HOST)
946 return;
947 printf(" ICMP Host Redirect to %pI4 ",
948 &icmph->un.gateway);
949 break;
Simon Glass8f79bb12011-10-24 18:00:00 +0000950 default:
Joe Hershbergera36b12f2012-05-23 07:58:02 +0000951#if defined(CONFIG_CMD_PING)
952 ping_receive(et, ip, len);
953#endif
Simon Glass39bccd22011-10-26 14:18:38 +0000954#ifdef CONFIG_CMD_TFTPPUT
Simon Glass4793ee62011-10-24 18:00:01 +0000955 if (packet_icmp_handler)
956 packet_icmp_handler(icmph->type, icmph->code,
957 ntohs(ip->udp_dst), src_ip, ntohs(ip->udp_src),
958 icmph->un.data, ntohs(ip->udp_len));
Simon Glass39bccd22011-10-26 14:18:38 +0000959#endif
Simon Glass8f79bb12011-10-24 18:00:00 +0000960 break;
961 }
962}
963
Joe Hershberger2a504df2015-03-22 17:09:11 -0500964void net_process_received_packet(uchar *in_packet, int len)
wdenk2d966952002-10-31 22:12:35 +0000965{
Joe Hershbergercb487f52012-05-23 07:58:06 +0000966 struct ethernet_hdr *et;
Joe Hershberger594c26f2012-05-23 07:58:04 +0000967 struct ip_udp_hdr *ip;
Joe Hershberger8d353eb2012-05-23 07:58:12 +0000968 IPaddr_t dst_ip;
Luca Ceresoli03eb1292011-04-18 06:19:50 +0000969 IPaddr_t src_ip;
Joe Hershberger8d353eb2012-05-23 07:58:12 +0000970 int eth_proto;
Jon Loeliger643d1ab2007-07-09 17:45:14 -0500971#if defined(CONFIG_CMD_CDP)
wdenka3d991b2004-04-15 21:48:45 +0000972 int iscdp;
973#endif
974 ushort cti = 0, vlanid = VLAN_NONE, myvlanid, mynvlanid;
wdenk2d966952002-10-31 22:12:35 +0000975
Joe Hershberger4ef8d532012-05-23 08:01:04 +0000976 debug_cond(DEBUG_NET_PKT, "packet received\n");
wdenka3d991b2004-04-15 21:48:45 +0000977
Joe Hershberger2a504df2015-03-22 17:09:11 -0500978 NetRxPacket = in_packet;
Mike Frysingerd9bec9f2009-07-18 21:04:08 -0400979 NetRxPacketLen = len;
Joe Hershberger2a504df2015-03-22 17:09:11 -0500980 et = (struct ethernet_hdr *)in_packet;
wdenka3d991b2004-04-15 21:48:45 +0000981
982 /* too small packet? */
983 if (len < ETHER_HDR_SIZE)
984 return;
985
Rafal Jaworowskif85b6072007-12-27 18:19:02 +0100986#ifdef CONFIG_API
987 if (push_packet) {
Joe Hershberger2a504df2015-03-22 17:09:11 -0500988 (*push_packet)(in_packet, len);
Rafal Jaworowskif85b6072007-12-27 18:19:02 +0100989 return;
990 }
991#endif
992
Jon Loeliger643d1ab2007-07-09 17:45:14 -0500993#if defined(CONFIG_CMD_CDP)
wdenka3d991b2004-04-15 21:48:45 +0000994 /* keep track if packet is CDP */
Joe Hershberger17351882012-05-23 07:58:00 +0000995 iscdp = is_cdp_packet(et->et_dest);
wdenka3d991b2004-04-15 21:48:45 +0000996#endif
997
998 myvlanid = ntohs(NetOurVLAN);
999 if (myvlanid == (ushort)-1)
1000 myvlanid = VLAN_NONE;
1001 mynvlanid = ntohs(NetOurNativeVLAN);
1002 if (mynvlanid == (ushort)-1)
1003 mynvlanid = VLAN_NONE;
wdenk2d966952002-10-31 22:12:35 +00001004
Joe Hershberger8d353eb2012-05-23 07:58:12 +00001005 eth_proto = ntohs(et->et_protlen);
wdenk2d966952002-10-31 22:12:35 +00001006
Joe Hershberger8d353eb2012-05-23 07:58:12 +00001007 if (eth_proto < 1514) {
Joe Hershbergercb487f52012-05-23 07:58:06 +00001008 struct e802_hdr *et802 = (struct e802_hdr *)et;
wdenk2d966952002-10-31 22:12:35 +00001009 /*
Joe Hershbergerda5ebe22012-05-23 07:58:11 +00001010 * Got a 802.2 packet. Check the other protocol field.
1011 * XXX VLAN over 802.2+SNAP not implemented!
wdenk2d966952002-10-31 22:12:35 +00001012 */
Joe Hershberger8d353eb2012-05-23 07:58:12 +00001013 eth_proto = ntohs(et802->et_prot);
wdenka3d991b2004-04-15 21:48:45 +00001014
Joe Hershberger2a504df2015-03-22 17:09:11 -05001015 ip = (struct ip_udp_hdr *)(in_packet + E802_HDR_SIZE);
wdenk2d966952002-10-31 22:12:35 +00001016 len -= E802_HDR_SIZE;
wdenka3d991b2004-04-15 21:48:45 +00001017
Joe Hershberger8d353eb2012-05-23 07:58:12 +00001018 } else if (eth_proto != PROT_VLAN) { /* normal packet */
Joe Hershberger2a504df2015-03-22 17:09:11 -05001019 ip = (struct ip_udp_hdr *)(in_packet + ETHER_HDR_SIZE);
wdenk2d966952002-10-31 22:12:35 +00001020 len -= ETHER_HDR_SIZE;
wdenka3d991b2004-04-15 21:48:45 +00001021
1022 } else { /* VLAN packet */
Joe Hershbergerc68cca32012-05-23 07:58:07 +00001023 struct vlan_ethernet_hdr *vet =
1024 (struct vlan_ethernet_hdr *)et;
wdenka3d991b2004-04-15 21:48:45 +00001025
Joe Hershberger4ef8d532012-05-23 08:01:04 +00001026 debug_cond(DEBUG_NET_PKT, "VLAN packet received\n");
Robin Getz0ebf04c2009-07-23 03:01:03 -04001027
wdenka3d991b2004-04-15 21:48:45 +00001028 /* too small packet? */
1029 if (len < VLAN_ETHER_HDR_SIZE)
1030 return;
1031
1032 /* if no VLAN active */
1033 if ((ntohs(NetOurVLAN) & VLAN_IDMASK) == VLAN_NONE
Jon Loeliger643d1ab2007-07-09 17:45:14 -05001034#if defined(CONFIG_CMD_CDP)
wdenka3d991b2004-04-15 21:48:45 +00001035 && iscdp == 0
1036#endif
1037 )
1038 return;
1039
1040 cti = ntohs(vet->vet_tag);
1041 vlanid = cti & VLAN_IDMASK;
Joe Hershberger8d353eb2012-05-23 07:58:12 +00001042 eth_proto = ntohs(vet->vet_type);
wdenka3d991b2004-04-15 21:48:45 +00001043
Joe Hershberger2a504df2015-03-22 17:09:11 -05001044 ip = (struct ip_udp_hdr *)(in_packet + VLAN_ETHER_HDR_SIZE);
wdenka3d991b2004-04-15 21:48:45 +00001045 len -= VLAN_ETHER_HDR_SIZE;
wdenk2d966952002-10-31 22:12:35 +00001046 }
1047
Joe Hershberger4ef8d532012-05-23 08:01:04 +00001048 debug_cond(DEBUG_NET_PKT, "Receive from protocol 0x%x\n", eth_proto);
wdenk2d966952002-10-31 22:12:35 +00001049
Jon Loeliger643d1ab2007-07-09 17:45:14 -05001050#if defined(CONFIG_CMD_CDP)
wdenka3d991b2004-04-15 21:48:45 +00001051 if (iscdp) {
Joe Hershberger0b4c5ff2012-05-23 07:58:13 +00001052 cdp_receive((uchar *)ip, len);
wdenka3d991b2004-04-15 21:48:45 +00001053 return;
1054 }
1055#endif
1056
1057 if ((myvlanid & VLAN_IDMASK) != VLAN_NONE) {
1058 if (vlanid == VLAN_NONE)
1059 vlanid = (mynvlanid & VLAN_IDMASK);
1060 /* not matched? */
1061 if (vlanid != (myvlanid & VLAN_IDMASK))
1062 return;
1063 }
1064
Joe Hershberger8d353eb2012-05-23 07:58:12 +00001065 switch (eth_proto) {
wdenk2d966952002-10-31 22:12:35 +00001066
1067 case PROT_ARP:
Joe Hershbergerd280d3f2012-05-23 07:58:01 +00001068 ArpReceive(et, ip, len);
wdenk289f9322005-01-12 00:15:14 +00001069 break;
wdenk2d966952002-10-31 22:12:35 +00001070
Peter Tyserbf6cb242010-09-30 11:25:48 -05001071#ifdef CONFIG_CMD_RARP
wdenk2d966952002-10-31 22:12:35 +00001072 case PROT_RARP:
Joe Hershberger8b9c5322012-05-23 07:58:03 +00001073 rarp_receive(ip, len);
wdenk2d966952002-10-31 22:12:35 +00001074 break;
Peter Tyserbf6cb242010-09-30 11:25:48 -05001075#endif
wdenk2d966952002-10-31 22:12:35 +00001076 case PROT_IP:
Joe Hershberger4ef8d532012-05-23 08:01:04 +00001077 debug_cond(DEBUG_NET_PKT, "Got IP\n");
Alessandro Rubini5cfaa4e2009-08-07 13:58:56 +02001078 /* Before we start poking the header, make sure it is there */
Joe Hershberger594c26f2012-05-23 07:58:04 +00001079 if (len < IP_UDP_HDR_SIZE) {
1080 debug("len bad %d < %lu\n", len,
1081 (ulong)IP_UDP_HDR_SIZE);
wdenk2d966952002-10-31 22:12:35 +00001082 return;
1083 }
Alessandro Rubini5cfaa4e2009-08-07 13:58:56 +02001084 /* Check the packet length */
wdenk2d966952002-10-31 22:12:35 +00001085 if (len < ntohs(ip->ip_len)) {
Joe Hershberger4ef8d532012-05-23 08:01:04 +00001086 debug("len bad %d < %d\n", len, ntohs(ip->ip_len));
wdenk2d966952002-10-31 22:12:35 +00001087 return;
1088 }
1089 len = ntohs(ip->ip_len);
Joe Hershberger4ef8d532012-05-23 08:01:04 +00001090 debug_cond(DEBUG_NET_PKT, "len=%d, v=%02x\n",
1091 len, ip->ip_hl_v & 0xff);
Robin Getz0ebf04c2009-07-23 03:01:03 -04001092
Alessandro Rubini5cfaa4e2009-08-07 13:58:56 +02001093 /* Can't deal with anything except IPv4 */
Luca Ceresolid3c65b02011-05-04 02:40:43 +00001094 if ((ip->ip_hl_v & 0xf0) != 0x40)
wdenk2d966952002-10-31 22:12:35 +00001095 return;
Alessandro Rubini5cfaa4e2009-08-07 13:58:56 +02001096 /* Can't deal with IP options (headers != 20 bytes) */
Luca Ceresolid3c65b02011-05-04 02:40:43 +00001097 if ((ip->ip_hl_v & 0x0f) > 0x05)
Remy Bohmer6b52cfe2008-06-03 15:48:17 +02001098 return;
Alessandro Rubini5cfaa4e2009-08-07 13:58:56 +02001099 /* Check the Checksum of the header */
Simon Glass0da0fcd2015-01-19 22:16:08 -07001100 if (!ip_checksum_ok((uchar *)ip, IP_HDR_SIZE)) {
Joe Hershberger4ef8d532012-05-23 08:01:04 +00001101 debug("checksum bad\n");
wdenk2d966952002-10-31 22:12:35 +00001102 return;
1103 }
Alessandro Rubini5cfaa4e2009-08-07 13:58:56 +02001104 /* If it is not for us, ignore it */
Joe Hershberger8d353eb2012-05-23 07:58:12 +00001105 dst_ip = NetReadIP(&ip->ip_dst);
1106 if (NetOurIP && dst_ip != NetOurIP && dst_ip != 0xFFFFFFFF) {
David Updegraff53a5c422007-06-11 10:41:07 -05001107#ifdef CONFIG_MCAST_TFTP
Joe Hershberger8d353eb2012-05-23 07:58:12 +00001108 if (Mcast_addr != dst_ip)
David Updegraff53a5c422007-06-11 10:41:07 -05001109#endif
Luca Ceresolic819abe2011-05-04 02:40:46 +00001110 return;
wdenk2d966952002-10-31 22:12:35 +00001111 }
Luca Ceresoli03eb1292011-04-18 06:19:50 +00001112 /* Read source IP address for later use */
1113 src_ip = NetReadIP(&ip->ip_src);
wdenk2d966952002-10-31 22:12:35 +00001114 /*
Alessandro Rubini5cfaa4e2009-08-07 13:58:56 +02001115 * The function returns the unchanged packet if it's not
1116 * a fragment, and either the complete packet or NULL if
1117 * it is a fragment (if !CONFIG_IP_DEFRAG, it returns NULL)
1118 */
Luca Ceresoliccb9ebe2011-05-04 02:40:47 +00001119 ip = NetDefragment(ip, &len);
1120 if (!ip)
Alessandro Rubini5cfaa4e2009-08-07 13:58:56 +02001121 return;
1122 /*
wdenk2d966952002-10-31 22:12:35 +00001123 * watch for ICMP host redirects
1124 *
wdenk8bde7f72003-06-27 21:31:46 +00001125 * There is no real handler code (yet). We just watch
1126 * for ICMP host redirect messages. In case anybody
1127 * sees these messages: please contact me
1128 * (wd@denx.de), or - even better - send me the
1129 * necessary fixes :-)
wdenk2d966952002-10-31 22:12:35 +00001130 *
wdenk8bde7f72003-06-27 21:31:46 +00001131 * Note: in all cases where I have seen this so far
1132 * it was a problem with the router configuration,
1133 * for instance when a router was configured in the
1134 * BOOTP reply, but the TFTP server was on the same
1135 * subnet. So this is probably a warning that your
1136 * configuration might be wrong. But I'm not really
1137 * sure if there aren't any other situations.
Simon Glass4793ee62011-10-24 18:00:01 +00001138 *
1139 * Simon Glass <sjg@chromium.org>: We get an ICMP when
1140 * we send a tftp packet to a dead connection, or when
1141 * there is no server at the other end.
wdenk2d966952002-10-31 22:12:35 +00001142 */
1143 if (ip->ip_p == IPPROTO_ICMP) {
Simon Glass8f79bb12011-10-24 18:00:00 +00001144 receive_icmp(ip, len, src_ip, et);
1145 return;
wdenk2d966952002-10-31 22:12:35 +00001146 } else if (ip->ip_p != IPPROTO_UDP) { /* Only UDP packets */
1147 return;
1148 }
1149
Joe Hershberger4ef8d532012-05-23 08:01:04 +00001150 debug_cond(DEBUG_DEV_PKT,
1151 "received UDP (to=%pI4, from=%pI4, len=%d)\n",
1152 &dst_ip, &src_ip, len);
1153
Stefan Roese8534bf92005-08-12 20:06:52 +02001154#ifdef CONFIG_UDP_CHECKSUM
1155 if (ip->udp_xsum != 0) {
Wolfgang Denkb2f50802005-08-12 23:43:12 +02001156 ulong xsum;
Stefan Roese8534bf92005-08-12 20:06:52 +02001157 ushort *sumptr;
1158 ushort sumlen;
1159
1160 xsum = ip->ip_p;
1161 xsum += (ntohs(ip->udp_len));
1162 xsum += (ntohl(ip->ip_src) >> 16) & 0x0000ffff;
1163 xsum += (ntohl(ip->ip_src) >> 0) & 0x0000ffff;
1164 xsum += (ntohl(ip->ip_dst) >> 16) & 0x0000ffff;
1165 xsum += (ntohl(ip->ip_dst) >> 0) & 0x0000ffff;
1166
1167 sumlen = ntohs(ip->udp_len);
1168 sumptr = (ushort *) &(ip->udp_src);
1169
1170 while (sumlen > 1) {
Wolfgang Denkb2f50802005-08-12 23:43:12 +02001171 ushort sumdata;
Stefan Roese8534bf92005-08-12 20:06:52 +02001172
1173 sumdata = *sumptr++;
1174 xsum += ntohs(sumdata);
1175 sumlen -= 2;
1176 }
1177 if (sumlen > 0) {
Wolfgang Denkb2f50802005-08-12 23:43:12 +02001178 ushort sumdata;
Stefan Roese8534bf92005-08-12 20:06:52 +02001179
1180 sumdata = *(unsigned char *) sumptr;
Wolfgang Denkb2f50802005-08-12 23:43:12 +02001181 sumdata = (sumdata << 8) & 0xff00;
Stefan Roese8534bf92005-08-12 20:06:52 +02001182 xsum += sumdata;
1183 }
1184 while ((xsum >> 16) != 0) {
Luca Ceresoli3e38e422011-05-11 03:59:54 +00001185 xsum = (xsum & 0x0000ffff) +
1186 ((xsum >> 16) & 0x0000ffff);
Stefan Roese8534bf92005-08-12 20:06:52 +02001187 }
1188 if ((xsum != 0x00000000) && (xsum != 0x0000ffff)) {
Wolfgang Denk9b55a252008-07-11 01:16:00 +02001189 printf(" UDP wrong checksum %08lx %08x\n",
1190 xsum, ntohs(ip->udp_xsum));
Stefan Roese8534bf92005-08-12 20:06:52 +02001191 return;
1192 }
1193 }
1194#endif
1195
David Updegraff53a5c422007-06-11 10:41:07 -05001196
Hannes Petermaier1f9ce302014-06-13 06:25:29 +02001197#if defined (CONFIG_NETCONSOLE) && !(CONFIG_SPL_BUILD)
Joe Hershberger594c26f2012-05-23 07:58:04 +00001198 nc_input_packet((uchar *)ip + IP_UDP_HDR_SIZE,
Joe Hershberger8cab08e2012-10-03 09:14:03 +00001199 src_ip,
Joe Hershberger594c26f2012-05-23 07:58:04 +00001200 ntohs(ip->udp_dst),
1201 ntohs(ip->udp_src),
1202 ntohs(ip->udp_len) - UDP_HDR_SIZE);
wdenk68ceb292004-08-02 21:11:11 +00001203#endif
wdenk2d966952002-10-31 22:12:35 +00001204 /*
1205 * IP header OK. Pass the packet to the current handler.
1206 */
Joe Hershbergerece223b2012-05-23 07:59:15 +00001207 (*udp_packet_handler)((uchar *)ip + IP_UDP_HDR_SIZE,
1208 ntohs(ip->udp_dst),
1209 src_ip,
1210 ntohs(ip->udp_src),
1211 ntohs(ip->udp_len) - UDP_HDR_SIZE);
wdenk2d966952002-10-31 22:12:35 +00001212 break;
1213 }
1214}
1215
1216
1217/**********************************************************************/
1218
Simon Glasse4bf0c52011-10-24 18:00:02 +00001219static int net_check_prereq(enum proto_t protocol)
wdenk2d966952002-10-31 22:12:35 +00001220{
1221 switch (protocol) {
wdenk6e592382004-04-18 17:39:38 +00001222 /* Fall through */
Jon Loeliger643d1ab2007-07-09 17:45:14 -05001223#if defined(CONFIG_CMD_PING)
wdenk73a8b272003-06-05 19:27:42 +00001224 case PING:
wdenk6e592382004-04-18 17:39:38 +00001225 if (NetPingIP == 0) {
Luca Ceresoli4f63acd2011-05-11 03:59:56 +00001226 puts("*** ERROR: ping address not given\n");
Luca Ceresoli92895de2011-05-04 02:40:45 +00001227 return 1;
wdenk6e592382004-04-18 17:39:38 +00001228 }
1229 goto common;
wdenk73a8b272003-06-05 19:27:42 +00001230#endif
Jon Loeliger643d1ab2007-07-09 17:45:14 -05001231#if defined(CONFIG_CMD_SNTP)
wdenkea287de2005-04-01 00:25:43 +00001232 case SNTP:
1233 if (NetNtpServerIP == 0) {
Luca Ceresoli4f63acd2011-05-11 03:59:56 +00001234 puts("*** ERROR: NTP server address not given\n");
Luca Ceresoli92895de2011-05-04 02:40:45 +00001235 return 1;
wdenkea287de2005-04-01 00:25:43 +00001236 }
1237 goto common;
1238#endif
Robin Getz1a32bf42009-07-20 14:53:54 -04001239#if defined(CONFIG_CMD_DNS)
1240 case DNS:
1241 if (NetOurDNSIP == 0) {
1242 puts("*** ERROR: DNS server address not given\n");
1243 return 1;
1244 }
1245 goto common;
1246#endif
Jon Loeliger643d1ab2007-07-09 17:45:14 -05001247#if defined(CONFIG_CMD_NFS)
wdenkcbd8a352004-02-24 02:00:03 +00001248 case NFS:
1249#endif
Simon Glasse4bf0c52011-10-24 18:00:02 +00001250 case TFTPGET:
Simon Glass1fb7cd42011-10-24 18:00:07 +00001251 case TFTPPUT:
wdenk6e592382004-04-18 17:39:38 +00001252 if (NetServerIP == 0) {
Luca Ceresoli4f63acd2011-05-11 03:59:56 +00001253 puts("*** ERROR: `serverip' not set\n");
Luca Ceresoli92895de2011-05-04 02:40:45 +00001254 return 1;
wdenk6e592382004-04-18 17:39:38 +00001255 }
Luca Ceresoli4f63acd2011-05-11 03:59:56 +00001256#if defined(CONFIG_CMD_PING) || defined(CONFIG_CMD_SNTP) || \
1257 defined(CONFIG_CMD_DNS)
1258common:
wdenk73a8b272003-06-05 19:27:42 +00001259#endif
Simon Guinot8b6bbe12011-05-01 23:38:40 +00001260 /* Fall through */
wdenk73a8b272003-06-05 19:27:42 +00001261
Simon Guinot8b6bbe12011-05-01 23:38:40 +00001262 case NETCONS:
Luca Ceresoli7a83af02011-05-17 00:03:40 +00001263 case TFTPSRV:
wdenk6e592382004-04-18 17:39:38 +00001264 if (NetOurIP == 0) {
Luca Ceresoli4f63acd2011-05-11 03:59:56 +00001265 puts("*** ERROR: `ipaddr' not set\n");
Luca Ceresoli92895de2011-05-04 02:40:45 +00001266 return 1;
wdenk6e592382004-04-18 17:39:38 +00001267 }
1268 /* Fall through */
wdenk2d966952002-10-31 22:12:35 +00001269
Peter Tyserbf6cb242010-09-30 11:25:48 -05001270#ifdef CONFIG_CMD_RARP
wdenk2d966952002-10-31 22:12:35 +00001271 case RARP:
Peter Tyserbf6cb242010-09-30 11:25:48 -05001272#endif
wdenk2d966952002-10-31 22:12:35 +00001273 case BOOTP:
wdenka3d991b2004-04-15 21:48:45 +00001274 case CDP:
Peter Tyserbf6cb242010-09-30 11:25:48 -05001275 case DHCP:
Joe Hershbergerd22c3382012-05-23 08:00:12 +00001276 case LINKLOCAL:
Luca Ceresoli4f63acd2011-05-11 03:59:56 +00001277 if (memcmp(NetOurEther, "\0\0\0\0\0\0", 6) == 0) {
Luca Ceresoli4f63acd2011-05-11 03:59:56 +00001278 int num = eth_get_dev_index();
wdenk2d966952002-10-31 22:12:35 +00001279
wdenk6e592382004-04-18 17:39:38 +00001280 switch (num) {
1281 case -1:
Luca Ceresoli4f63acd2011-05-11 03:59:56 +00001282 puts("*** ERROR: No ethernet found.\n");
Luca Ceresoli92895de2011-05-04 02:40:45 +00001283 return 1;
wdenk6e592382004-04-18 17:39:38 +00001284 case 0:
Luca Ceresoli4f63acd2011-05-11 03:59:56 +00001285 puts("*** ERROR: `ethaddr' not set\n");
wdenk2d966952002-10-31 22:12:35 +00001286 break;
wdenk6e592382004-04-18 17:39:38 +00001287 default:
Luca Ceresoli4f63acd2011-05-11 03:59:56 +00001288 printf("*** ERROR: `eth%daddr' not set\n",
wdenk2d966952002-10-31 22:12:35 +00001289 num);
1290 break;
wdenk2d966952002-10-31 22:12:35 +00001291 }
wdenk6e592382004-04-18 17:39:38 +00001292
Luca Ceresoli4f63acd2011-05-11 03:59:56 +00001293 NetStartAgain();
Luca Ceresoli92895de2011-05-04 02:40:45 +00001294 return 2;
wdenk6e592382004-04-18 17:39:38 +00001295 }
1296 /* Fall through */
1297 default:
Luca Ceresoli92895de2011-05-04 02:40:45 +00001298 return 0;
wdenk2d966952002-10-31 22:12:35 +00001299 }
Luca Ceresoli92895de2011-05-04 02:40:45 +00001300 return 0; /* OK */
wdenk2d966952002-10-31 22:12:35 +00001301}
1302/**********************************************************************/
1303
1304int
wdenka3d991b2004-04-15 21:48:45 +00001305NetEthHdrSize(void)
1306{
1307 ushort myvlanid;
wdenk2d966952002-10-31 22:12:35 +00001308
wdenka3d991b2004-04-15 21:48:45 +00001309 myvlanid = ntohs(NetOurVLAN);
1310 if (myvlanid == (ushort)-1)
1311 myvlanid = VLAN_NONE;
1312
Luca Ceresoli3e38e422011-05-11 03:59:54 +00001313 return ((myvlanid & VLAN_IDMASK) == VLAN_NONE) ? ETHER_HDR_SIZE :
1314 VLAN_ETHER_HDR_SIZE;
wdenka3d991b2004-04-15 21:48:45 +00001315}
1316
1317int
Joe Hershbergerdb288a92012-05-15 08:59:04 +00001318NetSetEther(uchar *xet, uchar * addr, uint prot)
wdenk2d966952002-10-31 22:12:35 +00001319{
Joe Hershbergercb487f52012-05-23 07:58:06 +00001320 struct ethernet_hdr *et = (struct ethernet_hdr *)xet;
wdenka3d991b2004-04-15 21:48:45 +00001321 ushort myvlanid;
1322
1323 myvlanid = ntohs(NetOurVLAN);
1324 if (myvlanid == (ushort)-1)
1325 myvlanid = VLAN_NONE;
wdenk2d966952002-10-31 22:12:35 +00001326
Luca Ceresoli4f63acd2011-05-11 03:59:56 +00001327 memcpy(et->et_dest, addr, 6);
1328 memcpy(et->et_src, NetOurEther, 6);
wdenka3d991b2004-04-15 21:48:45 +00001329 if ((myvlanid & VLAN_IDMASK) == VLAN_NONE) {
Luca Ceresolic819abe2011-05-04 02:40:46 +00001330 et->et_protlen = htons(prot);
wdenka3d991b2004-04-15 21:48:45 +00001331 return ETHER_HDR_SIZE;
1332 } else {
Joe Hershbergerc68cca32012-05-23 07:58:07 +00001333 struct vlan_ethernet_hdr *vet =
1334 (struct vlan_ethernet_hdr *)xet;
wdenk2d966952002-10-31 22:12:35 +00001335
wdenka3d991b2004-04-15 21:48:45 +00001336 vet->vet_vlan_type = htons(PROT_VLAN);
1337 vet->vet_tag = htons((0 << 5) | (myvlanid & VLAN_IDMASK));
1338 vet->vet_type = htons(prot);
1339 return VLAN_ETHER_HDR_SIZE;
1340 }
1341}
wdenk2d966952002-10-31 22:12:35 +00001342
Joe Hershbergere7111012012-05-23 07:59:16 +00001343int net_update_ether(struct ethernet_hdr *et, uchar *addr, uint prot)
1344{
1345 ushort protlen;
1346
1347 memcpy(et->et_dest, addr, 6);
1348 memcpy(et->et_src, NetOurEther, 6);
1349 protlen = ntohs(et->et_protlen);
1350 if (protlen == PROT_VLAN) {
1351 struct vlan_ethernet_hdr *vet =
1352 (struct vlan_ethernet_hdr *)et;
1353 vet->vet_type = htons(prot);
1354 return VLAN_ETHER_HDR_SIZE;
1355 } else if (protlen > 1514) {
1356 et->et_protlen = htons(prot);
1357 return ETHER_HDR_SIZE;
1358 } else {
1359 /* 802.2 + SNAP */
1360 struct e802_hdr *et802 = (struct e802_hdr *)et;
1361 et802->et_prot = htons(prot);
1362 return E802_HDR_SIZE;
1363 }
1364}
1365
Joe Hershberger4b11c912012-05-23 07:59:07 +00001366void net_set_ip_header(uchar *pkt, IPaddr_t dest, IPaddr_t source)
wdenk2d966952002-10-31 22:12:35 +00001367{
Joe Hershberger4b11c912012-05-23 07:59:07 +00001368 struct ip_udp_hdr *ip = (struct ip_udp_hdr *)pkt;
1369
1370 /*
1371 * Construct an IP header.
1372 */
1373 /* IP_HDR_SIZE / 4 (not including UDP) */
1374 ip->ip_hl_v = 0x45;
1375 ip->ip_tos = 0;
1376 ip->ip_len = htons(IP_HDR_SIZE);
1377 ip->ip_id = htons(NetIPID++);
1378 ip->ip_off = htons(IP_FLAGS_DFRAG); /* Don't fragment */
1379 ip->ip_ttl = 255;
1380 ip->ip_sum = 0;
1381 /* already in network byte order */
1382 NetCopyIP((void *)&ip->ip_src, &source);
1383 /* already in network byte order */
1384 NetCopyIP((void *)&ip->ip_dst, &dest);
1385}
1386
1387void net_set_udp_header(uchar *pkt, IPaddr_t dest, int dport, int sport,
1388 int len)
1389{
1390 struct ip_udp_hdr *ip = (struct ip_udp_hdr *)pkt;
wdenk2d966952002-10-31 22:12:35 +00001391
1392 /*
1393 * If the data is an odd number of bytes, zero the
1394 * byte after the last byte so that the checksum
1395 * will work.
1396 */
1397 if (len & 1)
Joe Hershberger4b11c912012-05-23 07:59:07 +00001398 pkt[IP_UDP_HDR_SIZE + len] = 0;
wdenk2d966952002-10-31 22:12:35 +00001399
Joe Hershberger4b11c912012-05-23 07:59:07 +00001400 net_set_ip_header(pkt, dest, NetOurIP);
Joe Hershberger594c26f2012-05-23 07:58:04 +00001401 ip->ip_len = htons(IP_UDP_HDR_SIZE + len);
Joe Hershberger4b11c912012-05-23 07:59:07 +00001402 ip->ip_p = IPPROTO_UDP;
Simon Glass0da0fcd2015-01-19 22:16:08 -07001403 ip->ip_sum = compute_ip_checksum(ip, IP_HDR_SIZE);
Joe Hershberger4b11c912012-05-23 07:59:07 +00001404
wdenk2d966952002-10-31 22:12:35 +00001405 ip->udp_src = htons(sport);
1406 ip->udp_dst = htons(dport);
Joe Hershberger594c26f2012-05-23 07:58:04 +00001407 ip->udp_len = htons(UDP_HDR_SIZE + len);
wdenk2d966952002-10-31 22:12:35 +00001408 ip->udp_xsum = 0;
wdenk2d966952002-10-31 22:12:35 +00001409}
1410
Luca Ceresoli4f63acd2011-05-11 03:59:56 +00001411void copy_filename(char *dst, const char *src, int size)
wdenk2d966952002-10-31 22:12:35 +00001412{
1413 if (*src && (*src == '"')) {
1414 ++src;
1415 --size;
1416 }
1417
Luca Ceresolid3c65b02011-05-04 02:40:43 +00001418 while ((--size > 0) && *src && (*src != '"'))
wdenk2d966952002-10-31 22:12:35 +00001419 *dst++ = *src++;
wdenk2d966952002-10-31 22:12:35 +00001420 *dst = '\0';
1421}
1422
Luca Ceresoli3e38e422011-05-11 03:59:54 +00001423#if defined(CONFIG_CMD_NFS) || \
1424 defined(CONFIG_CMD_SNTP) || \
1425 defined(CONFIG_CMD_DNS)
Robin Getz1a32bf42009-07-20 14:53:54 -04001426/*
Robin Getz97399462010-03-08 14:07:00 -05001427 * make port a little random (1024-17407)
1428 * This keeps the math somewhat trivial to compute, and seems to work with
1429 * all supported protocols/clients/servers
Robin Getz1a32bf42009-07-20 14:53:54 -04001430 */
1431unsigned int random_port(void)
1432{
Robin Getz97399462010-03-08 14:07:00 -05001433 return 1024 + (get_timer(0) % 0x4000);
Robin Getz1a32bf42009-07-20 14:53:54 -04001434}
1435#endif
1436
Luca Ceresoli4f63acd2011-05-11 03:59:56 +00001437void ip_to_string(IPaddr_t x, char *s)
wdenk2d966952002-10-31 22:12:35 +00001438{
Luca Ceresoli4f63acd2011-05-11 03:59:56 +00001439 x = ntohl(x);
1440 sprintf(s, "%d.%d.%d.%d",
1441 (int) ((x >> 24) & 0xff),
1442 (int) ((x >> 16) & 0xff),
1443 (int) ((x >> 8) & 0xff), (int) ((x >> 0) & 0xff)
wdenk6e592382004-04-18 17:39:38 +00001444 );
wdenk2d966952002-10-31 22:12:35 +00001445}
1446
wdenka3d991b2004-04-15 21:48:45 +00001447void VLAN_to_string(ushort x, char *s)
1448{
1449 x = ntohs(x);
1450
1451 if (x == (ushort)-1)
1452 x = VLAN_NONE;
1453
1454 if (x == VLAN_NONE)
1455 strcpy(s, "none");
1456 else
1457 sprintf(s, "%d", x & VLAN_IDMASK);
1458}
1459
Mike Frysinger2e3ef6e2010-10-20 07:16:48 -04001460ushort string_to_VLAN(const char *s)
wdenka3d991b2004-04-15 21:48:45 +00001461{
1462 ushort id;
1463
1464 if (s == NULL)
wdenkb9711de2004-04-25 13:18:40 +00001465 return htons(VLAN_NONE);
wdenka3d991b2004-04-15 21:48:45 +00001466
1467 if (*s < '0' || *s > '9')
1468 id = VLAN_NONE;
1469 else
1470 id = (ushort)simple_strtoul(s, NULL, 10);
1471
wdenkb9711de2004-04-25 13:18:40 +00001472 return htons(id);
wdenka3d991b2004-04-15 21:48:45 +00001473}
1474
wdenka3d991b2004-04-15 21:48:45 +00001475ushort getenv_VLAN(char *var)
1476{
Luca Ceresoli92895de2011-05-04 02:40:45 +00001477 return string_to_VLAN(getenv(var));
wdenka3d991b2004-04-15 21:48:45 +00001478}