blob: 7663b9cd6c92541b657fb3eebcbd72ba98b9a6df [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
9 */
10
11/*
12 * General Desription:
13 *
14 * The user interface supports commands for BOOTP, RARP, and TFTP.
15 * Also, we support ARP internally. Depending on available data,
16 * these interact as follows:
17 *
18 * BOOTP:
19 *
20 * Prerequisites: - own ethernet address
21 * We want: - own IP address
22 * - TFTP server IP address
23 * - name of bootfile
24 * Next step: ARP
25 *
Joe Hershbergerb35a3a62012-05-23 08:00:12 +000026 * LINK_LOCAL:
27 *
28 * Prerequisites: - own ethernet address
29 * We want: - own IP address
30 * Next step: ARP
31 *
wdenk2d966952002-10-31 22:12:35 +000032 * RARP:
33 *
34 * Prerequisites: - own ethernet address
35 * We want: - own IP address
36 * - TFTP server IP address
37 * Next step: ARP
38 *
39 * ARP:
40 *
41 * Prerequisites: - own ethernet address
42 * - own IP address
43 * - TFTP server IP address
44 * We want: - TFTP server ethernet address
45 * Next step: TFTP
46 *
47 * DHCP:
48 *
Wolfgang Denk30b87322005-08-12 23:43:12 +020049 * Prerequisites: - own ethernet address
50 * We want: - IP, Netmask, ServerIP, Gateway IP
51 * - bootfilename, lease time
52 * Next step: - TFTP
wdenk2d966952002-10-31 22:12:35 +000053 *
54 * TFTP:
55 *
56 * Prerequisites: - own ethernet address
57 * - own IP address
58 * - TFTP server IP address
59 * - TFTP server ethernet address
60 * - name of bootfile (if unknown, we use a default name
61 * derived from our own IP address)
62 * We want: - load the boot file
63 * Next step: none
wdenkbe9c1cb2004-02-24 02:00:03 +000064 *
65 * NFS:
66 *
67 * Prerequisites: - own ethernet address
68 * - own IP address
69 * - name of bootfile (if unknown, we use a default name
70 * derived from our own IP address)
71 * We want: - load the boot file
72 * Next step: none
wdenkb4ad9622005-04-01 00:25:43 +000073 *
74 * SNTP:
75 *
Wolfgang Denk30b87322005-08-12 23:43:12 +020076 * Prerequisites: - own ethernet address
wdenkb4ad9622005-04-01 00:25:43 +000077 * - own IP address
78 * We want: - network time
79 * Next step: none
wdenk2d966952002-10-31 22:12:35 +000080 */
81
82
83#include <common.h>
wdenk2d966952002-10-31 22:12:35 +000084#include <command.h>
Joe Hershbergerdc9d1e52012-12-11 22:16:26 -060085#include <environment.h>
wdenk2d966952002-10-31 22:12:35 +000086#include <net.h>
Joe Hershbergerfee076e2012-05-23 07:58:15 +000087#if defined(CONFIG_STATUS_LED)
wdenk49c3f672003-10-08 22:33:00 +000088#include <miiphy.h>
Joe Hershbergerfee076e2012-05-23 07:58:15 +000089#include <status_led.h>
wdenkb4ad9622005-04-01 00:25:43 +000090#endif
Joe Hershbergerfee076e2012-05-23 07:58:15 +000091#include <watchdog.h>
92#include <linux/compiler.h>
93#include "arp.h"
94#include "bootp.h"
Joe Hershbergera4215b02012-05-23 07:57:59 +000095#include "cdp.h"
Robin Getz82f0d232009-07-20 14:53:54 -040096#if defined(CONFIG_CMD_DNS)
97#include "dns.h"
98#endif
Joe Hershbergerb35a3a62012-05-23 08:00:12 +000099#include "link_local.h"
Joe Hershbergerfee076e2012-05-23 07:58:15 +0000100#include "nfs.h"
Joe Hershbergerc21bf372012-05-23 07:58:02 +0000101#include "ping.h"
Joe Hershbergerfee076e2012-05-23 07:58:15 +0000102#include "rarp.h"
103#if defined(CONFIG_CMD_SNTP)
104#include "sntp.h"
105#endif
106#include "tftp.h"
wdenk2d966952002-10-31 22:12:35 +0000107
Wolfgang Denk6405a152006-03-31 18:32:53 +0200108DECLARE_GLOBAL_DATA_PTR;
109
wdenk2d966952002-10-31 22:12:35 +0000110/** BOOTP EXTENTIONS **/
111
Luca Ceresoli5c83a962011-05-11 03:59:54 +0000112/* Our subnet mask (0=unknown) */
Luca Ceresolid01bc1c2011-05-11 03:59:55 +0000113IPaddr_t NetOurSubnetMask;
Luca Ceresoli5c83a962011-05-11 03:59:54 +0000114/* Our gateways IP address */
Luca Ceresolid01bc1c2011-05-11 03:59:55 +0000115IPaddr_t NetOurGatewayIP;
Luca Ceresoli5c83a962011-05-11 03:59:54 +0000116/* Our DNS IP address */
Luca Ceresolid01bc1c2011-05-11 03:59:55 +0000117IPaddr_t NetOurDNSIP;
Jon Loeliger5336a762007-07-09 22:08:34 -0500118#if defined(CONFIG_BOOTP_DNS2)
Luca Ceresoli5c83a962011-05-11 03:59:54 +0000119/* Our 2nd DNS IP address */
Luca Ceresolid01bc1c2011-05-11 03:59:55 +0000120IPaddr_t NetOurDNS2IP;
stroesee0aadfb2003-08-28 14:17:32 +0000121#endif
Luca Ceresoli5c83a962011-05-11 03:59:54 +0000122/* Our NIS domain */
Luca Ceresolid01bc1c2011-05-11 03:59:55 +0000123char NetOurNISDomain[32] = {0,};
Luca Ceresoli5c83a962011-05-11 03:59:54 +0000124/* Our hostname */
Luca Ceresolid01bc1c2011-05-11 03:59:55 +0000125char NetOurHostName[32] = {0,};
Luca Ceresoli5c83a962011-05-11 03:59:54 +0000126/* Our bootpath */
Luca Ceresolid01bc1c2011-05-11 03:59:55 +0000127char NetOurRootPath[64] = {0,};
Luca Ceresoli5c83a962011-05-11 03:59:54 +0000128/* Our bootfile size in blocks */
Luca Ceresolid01bc1c2011-05-11 03:59:55 +0000129ushort NetBootFileSize;
wdenk2d966952002-10-31 22:12:35 +0000130
David Updegraff7280da72007-06-11 10:41:07 -0500131#ifdef CONFIG_MCAST_TFTP /* Multicast TFTP */
132IPaddr_t Mcast_addr;
133#endif
134
wdenk2d966952002-10-31 22:12:35 +0000135/** END OF BOOTP EXTENTIONS **/
136
Luca Ceresoli5c83a962011-05-11 03:59:54 +0000137/* The actual transferred size of the bootfile (in bytes) */
138ulong NetBootFileXferSize;
139/* Our ethernet address */
140uchar NetOurEther[6];
141/* Boot server enet address */
Luca Ceresolid01bc1c2011-05-11 03:59:55 +0000142uchar NetServerEther[6];
Luca Ceresoli5c83a962011-05-11 03:59:54 +0000143/* Our IP addr (0 = unknown) */
144IPaddr_t NetOurIP;
145/* Server IP addr (0 = unknown) */
146IPaddr_t NetServerIP;
147/* Current receive packet */
Joe Hershberger4b7747e2012-05-15 08:59:04 +0000148uchar *NetRxPacket;
Luca Ceresoli5c83a962011-05-11 03:59:54 +0000149/* Current rx packet length */
150int NetRxPacketLen;
151/* IP packet ID */
152unsigned NetIPID;
153/* Ethernet bcast address */
Luca Ceresolid01bc1c2011-05-11 03:59:55 +0000154uchar NetBcastAddr[6] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
155uchar NetEtherNullAddr[6];
Rafal Jaworowski1f2c9a42007-12-27 18:19:02 +0100156#ifdef CONFIG_API
Joe Hershberger4b7747e2012-05-15 08:59:04 +0000157void (*push_packet)(void *, int len) = 0;
Rafal Jaworowski1f2c9a42007-12-27 18:19:02 +0100158#endif
Luca Ceresoli5c83a962011-05-11 03:59:54 +0000159/* Network loop state */
Joe Hershbergerd4bb76a2012-05-23 07:59:14 +0000160enum net_loop_state net_state;
Luca Ceresoli5c83a962011-05-11 03:59:54 +0000161/* Tried all network devices */
Luca Ceresolid01bc1c2011-05-11 03:59:55 +0000162int NetRestartWrap;
Luca Ceresoli5c83a962011-05-11 03:59:54 +0000163/* Network loop restarted */
Luca Ceresolid01bc1c2011-05-11 03:59:55 +0000164static int NetRestarted;
Luca Ceresoli5c83a962011-05-11 03:59:54 +0000165/* At least one device configured */
Luca Ceresolid01bc1c2011-05-11 03:59:55 +0000166static int NetDevExists;
wdenk2d966952002-10-31 22:12:35 +0000167
wdenk05939202004-04-18 17:39:38 +0000168/* XXX in both little & big endian machines 0xFFFF == ntohs(-1) */
Luca Ceresoli5c83a962011-05-11 03:59:54 +0000169/* default is without VLAN */
170ushort NetOurVLAN = 0xFFFF;
171/* ditto */
172ushort NetOurNativeVLAN = 0xFFFF;
wdenk145d2c12004-04-15 21:48:45 +0000173
Luca Ceresoli5c83a962011-05-11 03:59:54 +0000174/* Boot File name */
175char BootFile[128];
wdenk2d966952002-10-31 22:12:35 +0000176
Jon Loeliger54f35c22007-07-09 17:45:14 -0500177#if defined(CONFIG_CMD_SNTP)
Luca Ceresoli5c83a962011-05-11 03:59:54 +0000178/* NTP server IP address */
179IPaddr_t NetNtpServerIP;
180/* offset time from UTC */
Luca Ceresolid01bc1c2011-05-11 03:59:55 +0000181int NetTimeOffset;
wdenkb4ad9622005-04-01 00:25:43 +0000182#endif
183
Kim Phillips40c2c032012-10-29 13:34:33 +0000184static uchar PktBuf[(PKTBUFSRX+1) * PKTSIZE_ALIGN + PKTALIGN];
wdenk2d966952002-10-31 22:12:35 +0000185
Luca Ceresoli5c83a962011-05-11 03:59:54 +0000186/* Receive packet */
Joe Hershberger4b7747e2012-05-15 08:59:04 +0000187uchar *NetRxPackets[PKTBUFSRX];
wdenk2d966952002-10-31 22:12:35 +0000188
Joe Hershbergerf50357b2012-05-23 07:59:15 +0000189/* Current UDP RX packet handler */
190static rxhand_f *udp_packet_handler;
191/* Current ARP RX packet handler */
192static rxhand_f *arp_packet_handler;
Simon Glass2928cd82011-10-26 14:18:38 +0000193#ifdef CONFIG_CMD_TFTPPUT
Joe Hershbergerf50357b2012-05-23 07:59:15 +0000194/* Current ICMP rx handler */
195static rxhand_icmp_f *packet_icmp_handler;
Simon Glass2928cd82011-10-26 14:18:38 +0000196#endif
Luca Ceresoli5c83a962011-05-11 03:59:54 +0000197/* Current timeout handler */
198static thand_f *timeHandler;
199/* Time base value */
200static ulong timeStart;
201/* Current timeout value */
202static ulong timeDelta;
203/* THE transmit packet */
Joe Hershberger4b7747e2012-05-15 08:59:04 +0000204uchar *NetTxPacket;
wdenk2d966952002-10-31 22:12:35 +0000205
Simon Glassd6c5f552011-10-24 18:00:02 +0000206static int net_check_prereq(enum proto_t protocol);
wdenk2d966952002-10-31 22:12:35 +0000207
Remy Bohmer0ddb8e82009-10-28 22:13:39 +0100208static int NetTryCount;
209
wdenk2d966952002-10-31 22:12:35 +0000210/**********************************************************************/
wdenke6466f62003-06-05 19:27:42 +0000211
Joe Hershbergerdc9d1e52012-12-11 22:16:26 -0600212static int on_bootfile(const char *name, const char *value, enum env_op op,
213 int flags)
214{
215 switch (op) {
216 case env_op_create:
217 case env_op_overwrite:
218 copy_filename(BootFile, value, sizeof(BootFile));
219 break;
220 default:
221 break;
222 }
223
224 return 0;
225}
226U_BOOT_ENV_CALLBACK(bootfile, on_bootfile);
227
Simon Glass5234ad12011-10-27 06:24:32 +0000228/*
229 * Check if autoload is enabled. If so, use either NFS or TFTP to download
230 * the boot file.
231 */
232void net_auto_load(void)
233{
Joe Hershberger864ec562012-12-11 22:16:22 -0600234#if defined(CONFIG_CMD_NFS)
Simon Glass5234ad12011-10-27 06:24:32 +0000235 const char *s = getenv("autoload");
236
Joe Hershberger864ec562012-12-11 22:16:22 -0600237 if (s != NULL && strcmp(s, "NFS") == 0) {
238 /*
239 * Use NFS to load the bootfile.
240 */
241 NfsStart();
242 return;
243 }
Simon Glass5234ad12011-10-27 06:24:32 +0000244#endif
Joe Hershberger864ec562012-12-11 22:16:22 -0600245 if (getenv_yesno("autoload") == 0) {
246 /*
247 * Just use BOOTP/RARP to configure system;
248 * Do not use TFTP to load the bootfile.
249 */
250 net_set_state(NETLOOP_SUCCESS);
251 return;
Simon Glass5234ad12011-10-27 06:24:32 +0000252 }
253 TftpStart(TFTPGET);
254}
255
Joe Hershberger202c6002012-05-23 07:59:21 +0000256static void NetInitLoop(void)
Heiko Schocher0c303fa2009-02-10 09:38:52 +0100257{
Luca Ceresolid01bc1c2011-05-11 03:59:55 +0000258 static int env_changed_id;
Luca Ceresoli7cbd7482011-05-11 03:59:56 +0000259 int env_id = get_env_id();
Heiko Schocher0c303fa2009-02-10 09:38:52 +0100260
261 /* update only when the environment has changed */
Michael Zaidmanb97bfe42009-04-04 01:43:00 +0300262 if (env_changed_id != env_id) {
Enric Balletbo i Serra7019d252011-05-31 21:01:47 +0000263 NetOurIP = getenv_IPaddr("ipaddr");
Luca Ceresoli7cbd7482011-05-11 03:59:56 +0000264 NetOurGatewayIP = getenv_IPaddr("gatewayip");
265 NetOurSubnetMask = getenv_IPaddr("netmask");
266 NetServerIP = getenv_IPaddr("serverip");
Heiko Schocher0c303fa2009-02-10 09:38:52 +0100267 NetOurNativeVLAN = getenv_VLAN("nvlan");
Michael Zaidmanb97bfe42009-04-04 01:43:00 +0300268 NetOurVLAN = getenv_VLAN("vlan");
Robin Getz82f0d232009-07-20 14:53:54 -0400269#if defined(CONFIG_CMD_DNS)
270 NetOurDNSIP = getenv_IPaddr("dnsip");
271#endif
Michael Zaidmanb97bfe42009-04-04 01:43:00 +0300272 env_changed_id = env_id;
Heiko Schocher0c303fa2009-02-10 09:38:52 +0100273 }
Jim Lin09a2bb82013-05-17 17:41:03 +0800274 if (eth_get_dev())
275 memcpy(NetOurEther, eth_get_dev()->enetaddr, 6);
Michael Zaidmanb97bfe42009-04-04 01:43:00 +0300276
Heiko Schocher3b195ff2009-04-28 08:36:11 +0200277 return;
Heiko Schocher0c303fa2009-02-10 09:38:52 +0100278}
279
Joe Hershbergerf50357b2012-05-23 07:59:15 +0000280static void net_clear_handlers(void)
281{
282 net_set_udp_handler(NULL);
283 net_set_arp_handler(NULL);
284 NetSetTimeout(0, NULL);
285}
286
287static void net_cleanup_loop(void)
288{
289 net_clear_handlers();
290}
291
Joe Hershberger017e5c42012-05-23 07:59:22 +0000292void net_init(void)
293{
294 static int first_call = 1;
295
296 if (first_call) {
297 /*
298 * Setup packet buffers, aligned correctly.
299 */
300 int i;
301
302 NetTxPacket = &PktBuf[0] + (PKTALIGN - 1);
303 NetTxPacket -= (ulong)NetTxPacket % PKTALIGN;
304 for (i = 0; i < PKTBUFSRX; i++)
305 NetRxPackets[i] = NetTxPacket + (i + 1) * PKTSIZE_ALIGN;
306
307 ArpInit();
308 net_clear_handlers();
309
310 /* Only need to setup buffer pointers once. */
311 first_call = 0;
312 }
313
314 NetInitLoop();
315}
316
wdenke6466f62003-06-05 19:27:42 +0000317/**********************************************************************/
wdenk2d966952002-10-31 22:12:35 +0000318/*
319 * Main network processing loop.
320 */
321
Simon Glassd6c5f552011-10-24 18:00:02 +0000322int NetLoop(enum proto_t protocol)
wdenk2d966952002-10-31 22:12:35 +0000323{
wdenk2d966952002-10-31 22:12:35 +0000324 bd_t *bd = gd->bd;
Simon Glass230467c2011-10-24 18:00:01 +0000325 int ret = -1;
wdenk2d966952002-10-31 22:12:35 +0000326
wdenk2d966952002-10-31 22:12:35 +0000327 NetRestarted = 0;
328 NetDevExists = 0;
Remy Bohmer0ddb8e82009-10-28 22:13:39 +0100329 NetTryCount = 1;
Joe Hershberger05a377b2012-05-23 08:01:04 +0000330 debug_cond(DEBUG_INT_STATE, "--- NetLoop Entry\n");
wdenke6466f62003-06-05 19:27:42 +0000331
Simon Glass768cbf02011-12-10 11:08:06 +0000332 bootstage_mark_name(BOOTSTAGE_ID_ETH_START, "eth_start");
Joe Hershberger017e5c42012-05-23 07:59:22 +0000333 net_init();
Joe Hershberger9f374062012-08-03 10:59:08 +0000334 if (eth_is_on_demand_init() || protocol != NETCONS) {
wdenkfa66e932005-04-03 14:52:59 +0000335 eth_halt();
Joe Hershberger9f374062012-08-03 10:59:08 +0000336 eth_set_current();
337 if (eth_init(bd) < 0) {
338 eth_halt();
339 return -1;
340 }
341 } else
342 eth_init_state_only(bd);
wdenk2d966952002-10-31 22:12:35 +0000343
344restart:
Joe Hershbergerd4bb76a2012-05-23 07:59:14 +0000345 net_set_state(NETLOOP_CONTINUE);
wdenk2d966952002-10-31 22:12:35 +0000346
347 /*
348 * Start the ball rolling with the given start function. From
349 * here on, this code is a state machine driven by received
350 * packets and timer events.
351 */
Joe Hershberger05a377b2012-05-23 08:01:04 +0000352 debug_cond(DEBUG_INT_STATE, "--- NetLoop Init\n");
Joe Hershberger202c6002012-05-23 07:59:21 +0000353 NetInitLoop();
wdenk2d966952002-10-31 22:12:35 +0000354
Luca Ceresoli7cbd7482011-05-11 03:59:56 +0000355 switch (net_check_prereq(protocol)) {
wdenk2d966952002-10-31 22:12:35 +0000356 case 1:
357 /* network not configured */
wdenkfa66e932005-04-03 14:52:59 +0000358 eth_halt();
Luca Ceresoli5fe6de22011-05-04 02:40:45 +0000359 return -1;
wdenk2d966952002-10-31 22:12:35 +0000360
wdenk2d966952002-10-31 22:12:35 +0000361 case 2:
362 /* network device not configured */
363 break;
wdenk2d966952002-10-31 22:12:35 +0000364
365 case 0:
wdenk2d966952002-10-31 22:12:35 +0000366 NetDevExists = 1;
Simon Glassd6c5f552011-10-24 18:00:02 +0000367 NetBootFileXferSize = 0;
wdenk2d966952002-10-31 22:12:35 +0000368 switch (protocol) {
Simon Glassd6c5f552011-10-24 18:00:02 +0000369 case TFTPGET:
Simon Glass6a398d22011-10-24 18:00:07 +0000370#ifdef CONFIG_CMD_TFTPPUT
371 case TFTPPUT:
372#endif
wdenk2d966952002-10-31 22:12:35 +0000373 /* always use ARP to get server ethernet address */
Simon Glassd6c5f552011-10-24 18:00:02 +0000374 TftpStart(protocol);
wdenk2d966952002-10-31 22:12:35 +0000375 break;
Luca Ceresoli7aa81a42011-05-17 00:03:40 +0000376#ifdef CONFIG_CMD_TFTPSRV
377 case TFTPSRV:
378 TftpStartServer();
379 break;
380#endif
Jon Loeliger54f35c22007-07-09 17:45:14 -0500381#if defined(CONFIG_CMD_DHCP)
wdenk2d966952002-10-31 22:12:35 +0000382 case DHCP:
wdenkc3919532004-10-11 22:51:13 +0000383 BootpTry = 0;
Michael Zaidman16bb21d2009-07-14 23:37:12 +0300384 NetOurIP = 0;
wdenk2d966952002-10-31 22:12:35 +0000385 DhcpRequest(); /* Basically same as BOOTP */
386 break;
Jon Loeligera9807e52007-07-10 11:05:02 -0500387#endif
wdenk2d966952002-10-31 22:12:35 +0000388
389 case BOOTP:
390 BootpTry = 0;
Michael Zaidman16bb21d2009-07-14 23:37:12 +0300391 NetOurIP = 0;
Luca Ceresoli7cbd7482011-05-11 03:59:56 +0000392 BootpRequest();
wdenk2d966952002-10-31 22:12:35 +0000393 break;
394
Peter Tyserf7a48ca2010-09-30 11:25:48 -0500395#if defined(CONFIG_CMD_RARP)
wdenk2d966952002-10-31 22:12:35 +0000396 case RARP:
397 RarpTry = 0;
Michael Zaidman16bb21d2009-07-14 23:37:12 +0300398 NetOurIP = 0;
Luca Ceresoli7cbd7482011-05-11 03:59:56 +0000399 RarpRequest();
wdenk2d966952002-10-31 22:12:35 +0000400 break;
Peter Tyserf7a48ca2010-09-30 11:25:48 -0500401#endif
Jon Loeliger54f35c22007-07-09 17:45:14 -0500402#if defined(CONFIG_CMD_PING)
wdenke6466f62003-06-05 19:27:42 +0000403 case PING:
Joe Hershbergerc21bf372012-05-23 07:58:02 +0000404 ping_start();
wdenke6466f62003-06-05 19:27:42 +0000405 break;
406#endif
Jon Loeliger54f35c22007-07-09 17:45:14 -0500407#if defined(CONFIG_CMD_NFS)
wdenkbe9c1cb2004-02-24 02:00:03 +0000408 case NFS:
409 NfsStart();
410 break;
411#endif
Jon Loeliger54f35c22007-07-09 17:45:14 -0500412#if defined(CONFIG_CMD_CDP)
wdenk145d2c12004-04-15 21:48:45 +0000413 case CDP:
414 CDPStart();
415 break;
416#endif
wdenkb8fb6192004-08-02 21:11:11 +0000417#ifdef CONFIG_NETCONSOLE
418 case NETCONS:
419 NcStart();
420 break;
421#endif
Jon Loeliger54f35c22007-07-09 17:45:14 -0500422#if defined(CONFIG_CMD_SNTP)
wdenkb4ad9622005-04-01 00:25:43 +0000423 case SNTP:
424 SntpStart();
425 break;
426#endif
Robin Getz82f0d232009-07-20 14:53:54 -0400427#if defined(CONFIG_CMD_DNS)
428 case DNS:
429 DnsStart();
430 break;
431#endif
Joe Hershbergerb35a3a62012-05-23 08:00:12 +0000432#if defined(CONFIG_CMD_LINK_LOCAL)
433 case LINKLOCAL:
434 link_local_start();
435 break;
436#endif
wdenk2d966952002-10-31 22:12:35 +0000437 default:
438 break;
439 }
440
wdenk2d966952002-10-31 22:12:35 +0000441 break;
442 }
443
Jon Loeliger54f35c22007-07-09 17:45:14 -0500444#if defined(CONFIG_MII) || defined(CONFIG_CMD_MII)
Luca Ceresoli5c83a962011-05-11 03:59:54 +0000445#if defined(CONFIG_SYS_FAULT_ECHO_LINK_DOWN) && \
446 defined(CONFIG_STATUS_LED) && \
447 defined(STATUS_LED_RED)
wdenk49c3f672003-10-08 22:33:00 +0000448 /*
wdenk9c53f402003-10-15 23:53:47 +0000449 * Echo the inverted link state to the fault LED.
wdenk49c3f672003-10-08 22:33:00 +0000450 */
Luca Ceresolie8ccbb02011-05-04 02:40:43 +0000451 if (miiphy_link(eth_get_dev()->name, CONFIG_SYS_FAULT_MII_ADDR))
Luca Ceresoli7cbd7482011-05-11 03:59:56 +0000452 status_led_set(STATUS_LED_RED, STATUS_LED_OFF);
Luca Ceresolie8ccbb02011-05-04 02:40:43 +0000453 else
Luca Ceresoli7cbd7482011-05-11 03:59:56 +0000454 status_led_set(STATUS_LED_RED, STATUS_LED_ON);
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200455#endif /* CONFIG_SYS_FAULT_ECHO_LINK_DOWN, ... */
wdenk49c3f672003-10-08 22:33:00 +0000456#endif /* CONFIG_MII, ... */
wdenk2d966952002-10-31 22:12:35 +0000457
458 /*
459 * Main packet reception loop. Loop receiving packets until
Joe Hershbergerd4bb76a2012-05-23 07:59:14 +0000460 * someone sets `net_state' to a state that terminates.
wdenk2d966952002-10-31 22:12:35 +0000461 */
462 for (;;) {
463 WATCHDOG_RESET();
464#ifdef CONFIG_SHOW_ACTIVITY
Joe Hershberger77001b432012-05-15 08:59:08 +0000465 show_activity(1);
wdenk2d966952002-10-31 22:12:35 +0000466#endif
467 /*
468 * Check the ethernet for a new packet. The ethernet
469 * receive routine will process it.
470 */
Guennadi Liakhovetskib38c2b32008-04-03 17:04:19 +0200471 eth_rx();
wdenk2d966952002-10-31 22:12:35 +0000472
473 /*
474 * Abort if ctrl-c was pressed.
475 */
476 if (ctrlc()) {
Joe Hershbergerde8205a2012-05-23 07:59:24 +0000477 /* cancel any ARP that may not have completed */
478 NetArpWaitPacketIP = 0;
479
Joe Hershbergerf50357b2012-05-23 07:59:15 +0000480 net_cleanup_loop();
wdenk57b2d802003-06-27 21:31:46 +0000481 eth_halt();
Joe Hershberger9f374062012-08-03 10:59:08 +0000482 /* Invalidate the last protocol */
483 eth_set_last_protocol(BOOTP);
484
Luca Ceresoli7cbd7482011-05-11 03:59:56 +0000485 puts("\nAbort\n");
Joe Hershberger05a377b2012-05-23 08:01:04 +0000486 /* include a debug print as well incase the debug
487 messages are directed to stderr */
488 debug_cond(DEBUG_INT_STATE, "--- NetLoop Abort!\n");
Simon Glass230467c2011-10-24 18:00:01 +0000489 goto done;
wdenk2d966952002-10-31 22:12:35 +0000490 }
491
wdenke6466f62003-06-05 19:27:42 +0000492 ArpTimeoutCheck();
wdenk2d966952002-10-31 22:12:35 +0000493
494 /*
495 * Check for a timeout, and run the timeout handler
496 * if we have one.
497 */
wdenk5d841732003-08-17 18:55:18 +0000498 if (timeHandler && ((get_timer(0) - timeStart) > timeDelta)) {
wdenk2d966952002-10-31 22:12:35 +0000499 thand_f *x;
500
Jon Loeliger54f35c22007-07-09 17:45:14 -0500501#if defined(CONFIG_MII) || defined(CONFIG_CMD_MII)
Luca Ceresoli7cbd7482011-05-11 03:59:56 +0000502#if defined(CONFIG_SYS_FAULT_ECHO_LINK_DOWN) && \
503 defined(CONFIG_STATUS_LED) && \
504 defined(STATUS_LED_RED)
wdenk49c3f672003-10-08 22:33:00 +0000505 /*
wdenk9c53f402003-10-15 23:53:47 +0000506 * Echo the inverted link state to the fault LED.
wdenk49c3f672003-10-08 22:33:00 +0000507 */
Luca Ceresoli7cbd7482011-05-11 03:59:56 +0000508 if (miiphy_link(eth_get_dev()->name,
Luca Ceresoli5c83a962011-05-11 03:59:54 +0000509 CONFIG_SYS_FAULT_MII_ADDR)) {
Luca Ceresoli7cbd7482011-05-11 03:59:56 +0000510 status_led_set(STATUS_LED_RED, STATUS_LED_OFF);
wdenk49c3f672003-10-08 22:33:00 +0000511 } else {
Luca Ceresoli7cbd7482011-05-11 03:59:56 +0000512 status_led_set(STATUS_LED_RED, STATUS_LED_ON);
wdenk49c3f672003-10-08 22:33:00 +0000513 }
Luca Ceresoli7cbd7482011-05-11 03:59:56 +0000514#endif /* CONFIG_SYS_FAULT_ECHO_LINK_DOWN, ... */
wdenk49c3f672003-10-08 22:33:00 +0000515#endif /* CONFIG_MII, ... */
Joe Hershberger05a377b2012-05-23 08:01:04 +0000516 debug_cond(DEBUG_INT_STATE, "--- NetLoop timeout\n");
wdenk2d966952002-10-31 22:12:35 +0000517 x = timeHandler;
518 timeHandler = (thand_f *)0;
519 (*x)();
520 }
521
522
Joe Hershbergerd4bb76a2012-05-23 07:59:14 +0000523 switch (net_state) {
wdenk2d966952002-10-31 22:12:35 +0000524
525 case NETLOOP_RESTART:
wdenk2d966952002-10-31 22:12:35 +0000526 NetRestarted = 1;
wdenk2d966952002-10-31 22:12:35 +0000527 goto restart;
528
529 case NETLOOP_SUCCESS:
Joe Hershbergerf50357b2012-05-23 07:59:15 +0000530 net_cleanup_loop();
wdenk2d966952002-10-31 22:12:35 +0000531 if (NetBootFileXferSize > 0) {
wdenk2d966952002-10-31 22:12:35 +0000532 printf("Bytes transferred = %ld (%lx hex)\n",
533 NetBootFileXferSize,
534 NetBootFileXferSize);
Simon Glass596295d2013-02-24 17:33:24 +0000535 setenv_hex("filesize", NetBootFileXferSize);
536 setenv_hex("fileaddr", load_addr);
wdenk2d966952002-10-31 22:12:35 +0000537 }
Joe Hershberger9f374062012-08-03 10:59:08 +0000538 if (protocol != NETCONS)
539 eth_halt();
540 else
541 eth_halt_state_only();
542
543 eth_set_last_protocol(protocol);
544
Simon Glass230467c2011-10-24 18:00:01 +0000545 ret = NetBootFileXferSize;
Joe Hershberger05a377b2012-05-23 08:01:04 +0000546 debug_cond(DEBUG_INT_STATE, "--- NetLoop Success!\n");
Simon Glass230467c2011-10-24 18:00:01 +0000547 goto done;
wdenk2d966952002-10-31 22:12:35 +0000548
549 case NETLOOP_FAIL:
Joe Hershbergerf50357b2012-05-23 07:59:15 +0000550 net_cleanup_loop();
Joe Hershberger9f374062012-08-03 10:59:08 +0000551 /* Invalidate the last protocol */
552 eth_set_last_protocol(BOOTP);
Joe Hershberger05a377b2012-05-23 08:01:04 +0000553 debug_cond(DEBUG_INT_STATE, "--- NetLoop Fail!\n");
Simon Glass230467c2011-10-24 18:00:01 +0000554 goto done;
Joe Hershbergerd4bb76a2012-05-23 07:59:14 +0000555
556 case NETLOOP_CONTINUE:
557 continue;
wdenk2d966952002-10-31 22:12:35 +0000558 }
559 }
Simon Glass230467c2011-10-24 18:00:01 +0000560
561done:
Simon Glass2928cd82011-10-26 14:18:38 +0000562#ifdef CONFIG_CMD_TFTPPUT
Simon Glass230467c2011-10-24 18:00:01 +0000563 /* Clear out the handlers */
Joe Hershbergerf50357b2012-05-23 07:59:15 +0000564 net_set_udp_handler(NULL);
Simon Glass230467c2011-10-24 18:00:01 +0000565 net_set_icmp_handler(NULL);
Simon Glass2928cd82011-10-26 14:18:38 +0000566#endif
Simon Glass230467c2011-10-24 18:00:01 +0000567 return ret;
wdenk2d966952002-10-31 22:12:35 +0000568}
569
570/**********************************************************************/
571
572static void
573startAgainTimeout(void)
574{
Joe Hershbergerd4bb76a2012-05-23 07:59:14 +0000575 net_set_state(NETLOOP_RESTART);
wdenk2d966952002-10-31 22:12:35 +0000576}
577
Luca Ceresoli7cbd7482011-05-11 03:59:56 +0000578void NetStartAgain(void)
wdenk2d966952002-10-31 22:12:35 +0000579{
wdenk05939202004-04-18 17:39:38 +0000580 char *nretry;
Remy Bohmer0ddb8e82009-10-28 22:13:39 +0100581 int retry_forever = 0;
582 unsigned long retrycnt = 0;
wdenk145d2c12004-04-15 21:48:45 +0000583
Remy Bohmer0ddb8e82009-10-28 22:13:39 +0100584 nretry = getenv("netretry");
585 if (nretry) {
586 if (!strcmp(nretry, "yes"))
587 retry_forever = 1;
588 else if (!strcmp(nretry, "no"))
589 retrycnt = 0;
590 else if (!strcmp(nretry, "once"))
591 retrycnt = 1;
592 else
593 retrycnt = simple_strtoul(nretry, NULL, 0);
594 } else
595 retry_forever = 1;
596
597 if ((!retry_forever) && (NetTryCount >= retrycnt)) {
598 eth_halt();
Joe Hershbergerd4bb76a2012-05-23 07:59:14 +0000599 net_set_state(NETLOOP_FAIL);
wdenk145d2c12004-04-15 21:48:45 +0000600 return;
601 }
Remy Bohmer0ddb8e82009-10-28 22:13:39 +0100602
603 NetTryCount++;
604
Luca Ceresoli7cbd7482011-05-11 03:59:56 +0000605 eth_halt();
Matthias Fuchsa02d62b2007-12-27 16:58:41 +0100606#if !defined(CONFIG_NET_DO_NOT_TRY_ANOTHER)
Luca Ceresoli7cbd7482011-05-11 03:59:56 +0000607 eth_try_another(!NetRestarted);
Matthias Fuchsa02d62b2007-12-27 16:58:41 +0100608#endif
Luca Ceresoli7cbd7482011-05-11 03:59:56 +0000609 eth_init(gd->bd);
wdenk05939202004-04-18 17:39:38 +0000610 if (NetRestartWrap) {
wdenk2d966952002-10-31 22:12:35 +0000611 NetRestartWrap = 0;
Remy Bohmer0ddb8e82009-10-28 22:13:39 +0100612 if (NetDevExists) {
Luca Ceresoli7cbd7482011-05-11 03:59:56 +0000613 NetSetTimeout(10000UL, startAgainTimeout);
Joe Hershbergerf50357b2012-05-23 07:59:15 +0000614 net_set_udp_handler(NULL);
wdenk05939202004-04-18 17:39:38 +0000615 } else {
Joe Hershbergerd4bb76a2012-05-23 07:59:14 +0000616 net_set_state(NETLOOP_FAIL);
wdenk2d966952002-10-31 22:12:35 +0000617 }
wdenk05939202004-04-18 17:39:38 +0000618 } else {
Joe Hershbergerd4bb76a2012-05-23 07:59:14 +0000619 net_set_state(NETLOOP_RESTART);
wdenk2d966952002-10-31 22:12:35 +0000620 }
wdenk2d966952002-10-31 22:12:35 +0000621}
622
623/**********************************************************************/
624/*
625 * Miscelaneous bits.
626 */
627
Joe Hershbergerf50357b2012-05-23 07:59:15 +0000628static void dummy_handler(uchar *pkt, unsigned dport,
629 IPaddr_t sip, unsigned sport,
630 unsigned len)
Joe Hershbergeraae05082012-05-23 07:58:01 +0000631{
Joe Hershbergeraae05082012-05-23 07:58:01 +0000632}
633
Joe Hershbergerf50357b2012-05-23 07:59:15 +0000634rxhand_f *net_get_udp_handler(void)
635{
636 return udp_packet_handler;
637}
Joe Hershbergeraae05082012-05-23 07:58:01 +0000638
Joe Hershbergerf50357b2012-05-23 07:59:15 +0000639void net_set_udp_handler(rxhand_f *f)
640{
Joe Hershberger05a377b2012-05-23 08:01:04 +0000641 debug_cond(DEBUG_INT_STATE, "--- NetLoop UDP handler set (%p)\n", f);
Joe Hershbergerf50357b2012-05-23 07:59:15 +0000642 if (f == NULL)
643 udp_packet_handler = dummy_handler;
644 else
645 udp_packet_handler = f;
646}
647
648rxhand_f *net_get_arp_handler(void)
wdenk2d966952002-10-31 22:12:35 +0000649{
Joe Hershbergerf50357b2012-05-23 07:59:15 +0000650 return arp_packet_handler;
wdenk2d966952002-10-31 22:12:35 +0000651}
652
Joe Hershbergerf50357b2012-05-23 07:59:15 +0000653void net_set_arp_handler(rxhand_f *f)
654{
Joe Hershberger05a377b2012-05-23 08:01:04 +0000655 debug_cond(DEBUG_INT_STATE, "--- NetLoop ARP handler set (%p)\n", f);
Joe Hershbergerf50357b2012-05-23 07:59:15 +0000656 if (f == NULL)
657 arp_packet_handler = dummy_handler;
658 else
659 arp_packet_handler = f;
660}
661
Simon Glass2928cd82011-10-26 14:18:38 +0000662#ifdef CONFIG_CMD_TFTPPUT
Simon Glass230467c2011-10-24 18:00:01 +0000663void net_set_icmp_handler(rxhand_icmp_f *f)
664{
665 packet_icmp_handler = f;
666}
Simon Glass2928cd82011-10-26 14:18:38 +0000667#endif
wdenk2d966952002-10-31 22:12:35 +0000668
669void
Luca Ceresolif27d91f2011-05-04 02:40:44 +0000670NetSetTimeout(ulong iv, thand_f *f)
wdenk2d966952002-10-31 22:12:35 +0000671{
672 if (iv == 0) {
Joe Hershberger05a377b2012-05-23 08:01:04 +0000673 debug_cond(DEBUG_INT_STATE,
674 "--- NetLoop timeout handler cancelled\n");
wdenk2d966952002-10-31 22:12:35 +0000675 timeHandler = (thand_f *)0;
676 } else {
Joe Hershberger05a377b2012-05-23 08:01:04 +0000677 debug_cond(DEBUG_INT_STATE,
678 "--- NetLoop timeout handler set (%p)\n", f);
wdenk2d966952002-10-31 22:12:35 +0000679 timeHandler = f;
wdenk5d841732003-08-17 18:55:18 +0000680 timeStart = get_timer(0);
Tetsuyuki Kobayashibf176182012-06-25 02:37:27 +0000681 timeDelta = iv * CONFIG_SYS_HZ / 1000;
wdenk2d966952002-10-31 22:12:35 +0000682 }
683}
684
Joe Hershberger1a6b8d82012-05-23 07:58:10 +0000685int NetSendUDPPacket(uchar *ether, IPaddr_t dest, int dport, int sport,
686 int payload_len)
wdenke6466f62003-06-05 19:27:42 +0000687{
wdenk145d2c12004-04-15 21:48:45 +0000688 uchar *pkt;
Joe Hershberger16be9cb2012-05-23 07:59:08 +0000689 int eth_hdr_size;
690 int pkt_hdr_size;
wdenk145d2c12004-04-15 21:48:45 +0000691
Joe Hershberger017e5c42012-05-23 07:59:22 +0000692 /* make sure the NetTxPacket is initialized (NetInit() was called) */
693 assert(NetTxPacket != NULL);
694 if (NetTxPacket == NULL)
695 return -1;
696
wdenke6466f62003-06-05 19:27:42 +0000697 /* convert to new style broadcast */
698 if (dest == 0)
699 dest = 0xFFFFFFFF;
700
701 /* if broadcast, make the ether address a broadcast and don't do ARP */
702 if (dest == 0xFFFFFFFF)
703 ether = NetBcastAddr;
704
Joe Hershbergerde8205a2012-05-23 07:59:24 +0000705 pkt = (uchar *)NetTxPacket;
Joe Hershberger16be9cb2012-05-23 07:59:08 +0000706
707 eth_hdr_size = NetSetEther(pkt, ether, PROT_IP);
708 pkt += eth_hdr_size;
709 net_set_udp_header(pkt, dest, dport, sport, payload_len);
710 pkt_hdr_size = eth_hdr_size + IP_UDP_HDR_SIZE;
wdenke6466f62003-06-05 19:27:42 +0000711
Joe Hershbergerde8205a2012-05-23 07:59:24 +0000712 /* if MAC address was not discovered yet, do an ARP request */
713 if (memcmp(ether, NetEtherNullAddr, 6) == 0) {
Joe Hershberger05a377b2012-05-23 08:01:04 +0000714 debug_cond(DEBUG_DEV_PKT, "sending ARP for %pI4\n", &dest);
Robin Getz9e0a4d62009-07-23 03:01:03 -0400715
Joe Hershberger16be9cb2012-05-23 07:59:08 +0000716 /* save the ip and eth addr for the packet to send after arp */
wdenke6466f62003-06-05 19:27:42 +0000717 NetArpWaitPacketIP = dest;
718 NetArpWaitPacketMAC = ether;
wdenk145d2c12004-04-15 21:48:45 +0000719
wdenke6466f62003-06-05 19:27:42 +0000720 /* size of the waiting packet */
Joe Hershberger16be9cb2012-05-23 07:59:08 +0000721 NetArpWaitTxPacketSize = pkt_hdr_size + payload_len;
wdenke6466f62003-06-05 19:27:42 +0000722
723 /* and do the ARP request */
724 NetArpWaitTry = 1;
725 NetArpWaitTimerStart = get_timer(0);
726 ArpRequest();
727 return 1; /* waiting */
Joe Hershberger16be9cb2012-05-23 07:59:08 +0000728 } else {
Joe Hershberger05a377b2012-05-23 08:01:04 +0000729 debug_cond(DEBUG_DEV_PKT, "sending UDP to %pI4/%pM\n",
730 &dest, ether);
Joe Hershbergerebe3ca82012-05-23 07:59:13 +0000731 NetSendPacket(NetTxPacket, pkt_hdr_size + payload_len);
Joe Hershberger16be9cb2012-05-23 07:59:08 +0000732 return 0; /* transmitted */
wdenke6466f62003-06-05 19:27:42 +0000733 }
wdenke6466f62003-06-05 19:27:42 +0000734}
wdenk145d2c12004-04-15 21:48:45 +0000735
Alessandro Rubinif119e3d2009-08-07 13:58:56 +0200736#ifdef CONFIG_IP_DEFRAG
737/*
738 * This function collects fragments in a single packet, according
739 * to the algorithm in RFC815. It returns NULL or the pointer to
740 * a complete packet, in static storage
741 */
742#ifndef CONFIG_NET_MAXDEFRAG
743#define CONFIG_NET_MAXDEFRAG 16384
744#endif
745/*
746 * MAXDEFRAG, above, is chosen in the config file and is real data
747 * so we need to add the NFS overhead, which is more than TFTP.
748 * To use sizeof in the internal unnamed structures, we need a real
749 * instance (can't do "sizeof(struct rpc_t.u.reply))", unfortunately).
750 * The compiler doesn't complain nor allocates the actual structure
751 */
752static struct rpc_t rpc_specimen;
753#define IP_PKTSIZE (CONFIG_NET_MAXDEFRAG + sizeof(rpc_specimen.u.reply))
754
Joe Hershbergerc686fa12012-05-23 07:58:05 +0000755#define IP_MAXUDP (IP_PKTSIZE - IP_HDR_SIZE)
Alessandro Rubinif119e3d2009-08-07 13:58:56 +0200756
757/*
758 * this is the packet being assembled, either data or frag control.
759 * Fragments go by 8 bytes, so this union must be 8 bytes long
760 */
761struct hole {
762 /* first_byte is address of this structure */
763 u16 last_byte; /* last byte in this hole + 1 (begin of next hole) */
764 u16 next_hole; /* index of next (in 8-b blocks), 0 == none */
765 u16 prev_hole; /* index of prev, 0 == none */
766 u16 unused;
767};
768
Joe Hershberger6fe8b452012-05-23 07:58:04 +0000769static struct ip_udp_hdr *__NetDefragment(struct ip_udp_hdr *ip, int *lenp)
Alessandro Rubinif119e3d2009-08-07 13:58:56 +0200770{
Joe Hershberger77001b432012-05-15 08:59:08 +0000771 static uchar pkt_buff[IP_PKTSIZE] __aligned(PKTALIGN);
Alessandro Rubinif119e3d2009-08-07 13:58:56 +0200772 static u16 first_hole, total_len;
773 struct hole *payload, *thisfrag, *h, *newh;
Joe Hershberger6fe8b452012-05-23 07:58:04 +0000774 struct ip_udp_hdr *localip = (struct ip_udp_hdr *)pkt_buff;
Alessandro Rubinif119e3d2009-08-07 13:58:56 +0200775 uchar *indata = (uchar *)ip;
776 int offset8, start, len, done = 0;
777 u16 ip_off = ntohs(ip->ip_off);
778
779 /* payload starts after IP header, this fragment is in there */
Joe Hershbergerc686fa12012-05-23 07:58:05 +0000780 payload = (struct hole *)(pkt_buff + IP_HDR_SIZE);
Alessandro Rubinif119e3d2009-08-07 13:58:56 +0200781 offset8 = (ip_off & IP_OFFS);
782 thisfrag = payload + offset8;
783 start = offset8 * 8;
Joe Hershbergerc686fa12012-05-23 07:58:05 +0000784 len = ntohs(ip->ip_len) - IP_HDR_SIZE;
Alessandro Rubinif119e3d2009-08-07 13:58:56 +0200785
786 if (start + len > IP_MAXUDP) /* fragment extends too far */
787 return NULL;
788
789 if (!total_len || localip->ip_id != ip->ip_id) {
790 /* new (or different) packet, reset structs */
791 total_len = 0xffff;
792 payload[0].last_byte = ~0;
793 payload[0].next_hole = 0;
794 payload[0].prev_hole = 0;
795 first_hole = 0;
796 /* any IP header will work, copy the first we received */
Joe Hershbergerc686fa12012-05-23 07:58:05 +0000797 memcpy(localip, ip, IP_HDR_SIZE);
Alessandro Rubinif119e3d2009-08-07 13:58:56 +0200798 }
799
800 /*
801 * What follows is the reassembly algorithm. We use the payload
802 * array as a linked list of hole descriptors, as each hole starts
803 * at a multiple of 8 bytes. However, last byte can be whatever value,
804 * so it is represented as byte count, not as 8-byte blocks.
805 */
806
807 h = payload + first_hole;
808 while (h->last_byte < start) {
809 if (!h->next_hole) {
810 /* no hole that far away */
811 return NULL;
812 }
813 h = payload + h->next_hole;
814 }
815
Fillod Stephanee7ade8b2010-06-11 19:26:43 +0200816 /* last fragment may be 1..7 bytes, the "+7" forces acceptance */
817 if (offset8 + ((len + 7) / 8) <= h - payload) {
Alessandro Rubinif119e3d2009-08-07 13:58:56 +0200818 /* no overlap with holes (dup fragment?) */
819 return NULL;
820 }
821
822 if (!(ip_off & IP_FLAGS_MFRAG)) {
823 /* no more fragmentss: truncate this (last) hole */
824 total_len = start + len;
825 h->last_byte = start + len;
826 }
827
828 /*
829 * There is some overlap: fix the hole list. This code doesn't
830 * deal with a fragment that overlaps with two different holes
831 * (thus being a superset of a previously-received fragment).
832 */
833
Luca Ceresoli7cbd7482011-05-11 03:59:56 +0000834 if ((h >= thisfrag) && (h->last_byte <= start + len)) {
Alessandro Rubinif119e3d2009-08-07 13:58:56 +0200835 /* complete overlap with hole: remove hole */
836 if (!h->prev_hole && !h->next_hole) {
837 /* last remaining hole */
838 done = 1;
839 } else if (!h->prev_hole) {
840 /* first hole */
841 first_hole = h->next_hole;
842 payload[h->next_hole].prev_hole = 0;
843 } else if (!h->next_hole) {
844 /* last hole */
845 payload[h->prev_hole].next_hole = 0;
846 } else {
847 /* in the middle of the list */
848 payload[h->next_hole].prev_hole = h->prev_hole;
849 payload[h->prev_hole].next_hole = h->next_hole;
850 }
851
852 } else if (h->last_byte <= start + len) {
853 /* overlaps with final part of the hole: shorten this hole */
854 h->last_byte = start;
855
856 } else if (h >= thisfrag) {
857 /* overlaps with initial part of the hole: move this hole */
858 newh = thisfrag + (len / 8);
859 *newh = *h;
860 h = newh;
861 if (h->next_hole)
862 payload[h->next_hole].prev_hole = (h - payload);
863 if (h->prev_hole)
864 payload[h->prev_hole].next_hole = (h - payload);
865 else
866 first_hole = (h - payload);
867
868 } else {
869 /* fragment sits in the middle: split the hole */
870 newh = thisfrag + (len / 8);
871 *newh = *h;
872 h->last_byte = start;
873 h->next_hole = (newh - payload);
874 newh->prev_hole = (h - payload);
875 if (newh->next_hole)
876 payload[newh->next_hole].prev_hole = (newh - payload);
877 }
878
879 /* finally copy this fragment and possibly return whole packet */
Joe Hershbergerc686fa12012-05-23 07:58:05 +0000880 memcpy((uchar *)thisfrag, indata + IP_HDR_SIZE, len);
Alessandro Rubinif119e3d2009-08-07 13:58:56 +0200881 if (!done)
882 return NULL;
883
884 localip->ip_len = htons(total_len);
Joe Hershbergerc686fa12012-05-23 07:58:05 +0000885 *lenp = total_len + IP_HDR_SIZE;
Alessandro Rubinif119e3d2009-08-07 13:58:56 +0200886 return localip;
887}
888
Joe Hershberger6fe8b452012-05-23 07:58:04 +0000889static inline struct ip_udp_hdr *NetDefragment(struct ip_udp_hdr *ip, int *lenp)
Alessandro Rubinif119e3d2009-08-07 13:58:56 +0200890{
891 u16 ip_off = ntohs(ip->ip_off);
892 if (!(ip_off & (IP_OFFS | IP_FLAGS_MFRAG)))
893 return ip; /* not a fragment */
894 return __NetDefragment(ip, lenp);
895}
896
897#else /* !CONFIG_IP_DEFRAG */
898
Joe Hershberger6fe8b452012-05-23 07:58:04 +0000899static inline struct ip_udp_hdr *NetDefragment(struct ip_udp_hdr *ip, int *lenp)
Alessandro Rubinif119e3d2009-08-07 13:58:56 +0200900{
901 u16 ip_off = ntohs(ip->ip_off);
902 if (!(ip_off & (IP_OFFS | IP_FLAGS_MFRAG)))
903 return ip; /* not a fragment */
904 return NULL;
905}
906#endif
wdenk2d966952002-10-31 22:12:35 +0000907
Simon Glass43c72962011-10-24 18:00:00 +0000908/**
909 * Receive an ICMP packet. We deal with REDIRECT and PING here, and silently
910 * drop others.
911 *
912 * @parma ip IP packet containing the ICMP
913 */
Joe Hershberger6fe8b452012-05-23 07:58:04 +0000914static void receive_icmp(struct ip_udp_hdr *ip, int len,
Joe Hershberger1178f412012-05-23 07:58:06 +0000915 IPaddr_t src_ip, struct ethernet_hdr *et)
Simon Glass43c72962011-10-24 18:00:00 +0000916{
Joe Hershberger78495612012-05-23 07:58:09 +0000917 struct icmp_hdr *icmph = (struct icmp_hdr *)&ip->udp_src;
Simon Glass43c72962011-10-24 18:00:00 +0000918
919 switch (icmph->type) {
920 case ICMP_REDIRECT:
921 if (icmph->code != ICMP_REDIR_HOST)
922 return;
923 printf(" ICMP Host Redirect to %pI4 ",
924 &icmph->un.gateway);
925 break;
Joe Hershbergerc21bf372012-05-23 07:58:02 +0000926 default:
Simon Glass43c72962011-10-24 18:00:00 +0000927#if defined(CONFIG_CMD_PING)
Joe Hershbergerc21bf372012-05-23 07:58:02 +0000928 ping_receive(et, ip, len);
Simon Glass43c72962011-10-24 18:00:00 +0000929#endif
Simon Glass2928cd82011-10-26 14:18:38 +0000930#ifdef CONFIG_CMD_TFTPPUT
Simon Glass230467c2011-10-24 18:00:01 +0000931 if (packet_icmp_handler)
932 packet_icmp_handler(icmph->type, icmph->code,
933 ntohs(ip->udp_dst), src_ip, ntohs(ip->udp_src),
934 icmph->un.data, ntohs(ip->udp_len));
Simon Glass2928cd82011-10-26 14:18:38 +0000935#endif
Simon Glass43c72962011-10-24 18:00:00 +0000936 break;
937 }
938}
939
wdenk2d966952002-10-31 22:12:35 +0000940void
Joe Hershberger4b7747e2012-05-15 08:59:04 +0000941NetReceive(uchar *inpkt, int len)
wdenk2d966952002-10-31 22:12:35 +0000942{
Joe Hershberger1178f412012-05-23 07:58:06 +0000943 struct ethernet_hdr *et;
Joe Hershberger6fe8b452012-05-23 07:58:04 +0000944 struct ip_udp_hdr *ip;
Joe Hershbergerfacf5352012-05-23 07:58:12 +0000945 IPaddr_t dst_ip;
Luca Ceresoli428ab362011-04-18 06:19:50 +0000946 IPaddr_t src_ip;
Joe Hershbergerfacf5352012-05-23 07:58:12 +0000947 int eth_proto;
Jon Loeliger54f35c22007-07-09 17:45:14 -0500948#if defined(CONFIG_CMD_CDP)
wdenk145d2c12004-04-15 21:48:45 +0000949 int iscdp;
950#endif
951 ushort cti = 0, vlanid = VLAN_NONE, myvlanid, mynvlanid;
wdenk2d966952002-10-31 22:12:35 +0000952
Joe Hershberger05a377b2012-05-23 08:01:04 +0000953 debug_cond(DEBUG_NET_PKT, "packet received\n");
wdenk145d2c12004-04-15 21:48:45 +0000954
Mike Frysingerdc166032009-07-18 21:04:08 -0400955 NetRxPacket = inpkt;
956 NetRxPacketLen = len;
Joe Hershberger1178f412012-05-23 07:58:06 +0000957 et = (struct ethernet_hdr *)inpkt;
wdenk145d2c12004-04-15 21:48:45 +0000958
959 /* too small packet? */
960 if (len < ETHER_HDR_SIZE)
961 return;
Rafal Jaworowski1f2c9a42007-12-27 18:19:02 +0100962
963#ifdef CONFIG_API
964 if (push_packet) {
965 (*push_packet)(inpkt, len);
966 return;
967 }
968#endif
wdenk145d2c12004-04-15 21:48:45 +0000969
Jon Loeliger54f35c22007-07-09 17:45:14 -0500970#if defined(CONFIG_CMD_CDP)
wdenk145d2c12004-04-15 21:48:45 +0000971 /* keep track if packet is CDP */
Joe Hershberger00c62a82012-05-23 07:58:00 +0000972 iscdp = is_cdp_packet(et->et_dest);
wdenk145d2c12004-04-15 21:48:45 +0000973#endif
974
975 myvlanid = ntohs(NetOurVLAN);
976 if (myvlanid == (ushort)-1)
977 myvlanid = VLAN_NONE;
978 mynvlanid = ntohs(NetOurNativeVLAN);
979 if (mynvlanid == (ushort)-1)
980 mynvlanid = VLAN_NONE;
wdenk2d966952002-10-31 22:12:35 +0000981
Joe Hershbergerfacf5352012-05-23 07:58:12 +0000982 eth_proto = ntohs(et->et_protlen);
wdenk2d966952002-10-31 22:12:35 +0000983
Joe Hershbergerfacf5352012-05-23 07:58:12 +0000984 if (eth_proto < 1514) {
Joe Hershberger1178f412012-05-23 07:58:06 +0000985 struct e802_hdr *et802 = (struct e802_hdr *)et;
wdenk2d966952002-10-31 22:12:35 +0000986 /*
Joe Hershbergerc17fa982012-05-23 07:58:11 +0000987 * Got a 802.2 packet. Check the other protocol field.
988 * XXX VLAN over 802.2+SNAP not implemented!
wdenk2d966952002-10-31 22:12:35 +0000989 */
Joe Hershbergerfacf5352012-05-23 07:58:12 +0000990 eth_proto = ntohs(et802->et_prot);
wdenk145d2c12004-04-15 21:48:45 +0000991
Joe Hershberger6fe8b452012-05-23 07:58:04 +0000992 ip = (struct ip_udp_hdr *)(inpkt + E802_HDR_SIZE);
wdenk2d966952002-10-31 22:12:35 +0000993 len -= E802_HDR_SIZE;
wdenk145d2c12004-04-15 21:48:45 +0000994
Joe Hershbergerfacf5352012-05-23 07:58:12 +0000995 } else if (eth_proto != PROT_VLAN) { /* normal packet */
Joe Hershberger6fe8b452012-05-23 07:58:04 +0000996 ip = (struct ip_udp_hdr *)(inpkt + ETHER_HDR_SIZE);
wdenk2d966952002-10-31 22:12:35 +0000997 len -= ETHER_HDR_SIZE;
wdenk145d2c12004-04-15 21:48:45 +0000998
999 } else { /* VLAN packet */
Joe Hershbergerb43784c2012-05-23 07:58:07 +00001000 struct vlan_ethernet_hdr *vet =
1001 (struct vlan_ethernet_hdr *)et;
wdenk145d2c12004-04-15 21:48:45 +00001002
Joe Hershberger05a377b2012-05-23 08:01:04 +00001003 debug_cond(DEBUG_NET_PKT, "VLAN packet received\n");
Robin Getz9e0a4d62009-07-23 03:01:03 -04001004
wdenk145d2c12004-04-15 21:48:45 +00001005 /* too small packet? */
1006 if (len < VLAN_ETHER_HDR_SIZE)
1007 return;
1008
1009 /* if no VLAN active */
1010 if ((ntohs(NetOurVLAN) & VLAN_IDMASK) == VLAN_NONE
Jon Loeliger54f35c22007-07-09 17:45:14 -05001011#if defined(CONFIG_CMD_CDP)
wdenk145d2c12004-04-15 21:48:45 +00001012 && iscdp == 0
1013#endif
1014 )
1015 return;
1016
1017 cti = ntohs(vet->vet_tag);
1018 vlanid = cti & VLAN_IDMASK;
Joe Hershbergerfacf5352012-05-23 07:58:12 +00001019 eth_proto = ntohs(vet->vet_type);
wdenk145d2c12004-04-15 21:48:45 +00001020
Joe Hershberger6fe8b452012-05-23 07:58:04 +00001021 ip = (struct ip_udp_hdr *)(inpkt + VLAN_ETHER_HDR_SIZE);
wdenk145d2c12004-04-15 21:48:45 +00001022 len -= VLAN_ETHER_HDR_SIZE;
wdenk2d966952002-10-31 22:12:35 +00001023 }
1024
Joe Hershberger05a377b2012-05-23 08:01:04 +00001025 debug_cond(DEBUG_NET_PKT, "Receive from protocol 0x%x\n", eth_proto);
wdenk2d966952002-10-31 22:12:35 +00001026
Jon Loeliger54f35c22007-07-09 17:45:14 -05001027#if defined(CONFIG_CMD_CDP)
wdenk145d2c12004-04-15 21:48:45 +00001028 if (iscdp) {
Joe Hershbergerd01a7a02012-05-23 07:58:13 +00001029 cdp_receive((uchar *)ip, len);
wdenk145d2c12004-04-15 21:48:45 +00001030 return;
1031 }
1032#endif
1033
1034 if ((myvlanid & VLAN_IDMASK) != VLAN_NONE) {
1035 if (vlanid == VLAN_NONE)
1036 vlanid = (mynvlanid & VLAN_IDMASK);
1037 /* not matched? */
1038 if (vlanid != (myvlanid & VLAN_IDMASK))
1039 return;
1040 }
1041
Joe Hershbergerfacf5352012-05-23 07:58:12 +00001042 switch (eth_proto) {
wdenk2d966952002-10-31 22:12:35 +00001043
1044 case PROT_ARP:
Joe Hershbergeraae05082012-05-23 07:58:01 +00001045 ArpReceive(et, ip, len);
wdenkcb99da52005-01-12 00:15:14 +00001046 break;
wdenk2d966952002-10-31 22:12:35 +00001047
Peter Tyserf7a48ca2010-09-30 11:25:48 -05001048#ifdef CONFIG_CMD_RARP
wdenk2d966952002-10-31 22:12:35 +00001049 case PROT_RARP:
Joe Hershberger61b4de62012-05-23 07:58:03 +00001050 rarp_receive(ip, len);
wdenk2d966952002-10-31 22:12:35 +00001051 break;
Peter Tyserf7a48ca2010-09-30 11:25:48 -05001052#endif
wdenk2d966952002-10-31 22:12:35 +00001053 case PROT_IP:
Joe Hershberger05a377b2012-05-23 08:01:04 +00001054 debug_cond(DEBUG_NET_PKT, "Got IP\n");
Alessandro Rubinif119e3d2009-08-07 13:58:56 +02001055 /* Before we start poking the header, make sure it is there */
Joe Hershberger6fe8b452012-05-23 07:58:04 +00001056 if (len < IP_UDP_HDR_SIZE) {
1057 debug("len bad %d < %lu\n", len,
1058 (ulong)IP_UDP_HDR_SIZE);
wdenk2d966952002-10-31 22:12:35 +00001059 return;
1060 }
Alessandro Rubinif119e3d2009-08-07 13:58:56 +02001061 /* Check the packet length */
wdenk2d966952002-10-31 22:12:35 +00001062 if (len < ntohs(ip->ip_len)) {
Joe Hershberger05a377b2012-05-23 08:01:04 +00001063 debug("len bad %d < %d\n", len, ntohs(ip->ip_len));
wdenk2d966952002-10-31 22:12:35 +00001064 return;
1065 }
1066 len = ntohs(ip->ip_len);
Joe Hershberger05a377b2012-05-23 08:01:04 +00001067 debug_cond(DEBUG_NET_PKT, "len=%d, v=%02x\n",
1068 len, ip->ip_hl_v & 0xff);
Robin Getz9e0a4d62009-07-23 03:01:03 -04001069
Alessandro Rubinif119e3d2009-08-07 13:58:56 +02001070 /* Can't deal with anything except IPv4 */
Luca Ceresolie8ccbb02011-05-04 02:40:43 +00001071 if ((ip->ip_hl_v & 0xf0) != 0x40)
wdenk2d966952002-10-31 22:12:35 +00001072 return;
Alessandro Rubinif119e3d2009-08-07 13:58:56 +02001073 /* Can't deal with IP options (headers != 20 bytes) */
Luca Ceresolie8ccbb02011-05-04 02:40:43 +00001074 if ((ip->ip_hl_v & 0x0f) > 0x05)
Remy Bohmerb9535782008-06-03 15:48:17 +02001075 return;
Alessandro Rubinif119e3d2009-08-07 13:58:56 +02001076 /* Check the Checksum of the header */
Joe Hershbergerc686fa12012-05-23 07:58:05 +00001077 if (!NetCksumOk((uchar *)ip, IP_HDR_SIZE / 2)) {
Joe Hershberger05a377b2012-05-23 08:01:04 +00001078 debug("checksum bad\n");
wdenk2d966952002-10-31 22:12:35 +00001079 return;
1080 }
Alessandro Rubinif119e3d2009-08-07 13:58:56 +02001081 /* If it is not for us, ignore it */
Joe Hershbergerfacf5352012-05-23 07:58:12 +00001082 dst_ip = NetReadIP(&ip->ip_dst);
1083 if (NetOurIP && dst_ip != NetOurIP && dst_ip != 0xFFFFFFFF) {
David Updegraff7280da72007-06-11 10:41:07 -05001084#ifdef CONFIG_MCAST_TFTP
Joe Hershbergerfacf5352012-05-23 07:58:12 +00001085 if (Mcast_addr != dst_ip)
David Updegraff7280da72007-06-11 10:41:07 -05001086#endif
Luca Ceresolib19d0b72011-05-04 02:40:46 +00001087 return;
wdenk2d966952002-10-31 22:12:35 +00001088 }
Luca Ceresoli428ab362011-04-18 06:19:50 +00001089 /* Read source IP address for later use */
1090 src_ip = NetReadIP(&ip->ip_src);
wdenk2d966952002-10-31 22:12:35 +00001091 /*
Alessandro Rubinif119e3d2009-08-07 13:58:56 +02001092 * The function returns the unchanged packet if it's not
1093 * a fragment, and either the complete packet or NULL if
1094 * it is a fragment (if !CONFIG_IP_DEFRAG, it returns NULL)
1095 */
Luca Ceresolidb210822011-05-04 02:40:47 +00001096 ip = NetDefragment(ip, &len);
1097 if (!ip)
Alessandro Rubinif119e3d2009-08-07 13:58:56 +02001098 return;
1099 /*
wdenk2d966952002-10-31 22:12:35 +00001100 * watch for ICMP host redirects
1101 *
wdenk57b2d802003-06-27 21:31:46 +00001102 * There is no real handler code (yet). We just watch
1103 * for ICMP host redirect messages. In case anybody
1104 * sees these messages: please contact me
1105 * (wd@denx.de), or - even better - send me the
1106 * necessary fixes :-)
wdenk2d966952002-10-31 22:12:35 +00001107 *
wdenk57b2d802003-06-27 21:31:46 +00001108 * Note: in all cases where I have seen this so far
1109 * it was a problem with the router configuration,
1110 * for instance when a router was configured in the
1111 * BOOTP reply, but the TFTP server was on the same
1112 * subnet. So this is probably a warning that your
1113 * configuration might be wrong. But I'm not really
1114 * sure if there aren't any other situations.
Simon Glass230467c2011-10-24 18:00:01 +00001115 *
1116 * Simon Glass <sjg@chromium.org>: We get an ICMP when
1117 * we send a tftp packet to a dead connection, or when
1118 * there is no server at the other end.
wdenk2d966952002-10-31 22:12:35 +00001119 */
1120 if (ip->ip_p == IPPROTO_ICMP) {
Simon Glass43c72962011-10-24 18:00:00 +00001121 receive_icmp(ip, len, src_ip, et);
1122 return;
wdenk2d966952002-10-31 22:12:35 +00001123 } else if (ip->ip_p != IPPROTO_UDP) { /* Only UDP packets */
1124 return;
1125 }
1126
Joe Hershberger05a377b2012-05-23 08:01:04 +00001127 debug_cond(DEBUG_DEV_PKT,
1128 "received UDP (to=%pI4, from=%pI4, len=%d)\n",
1129 &dst_ip, &src_ip, len);
1130
Stefan Roesedfced812005-08-12 20:06:52 +02001131#ifdef CONFIG_UDP_CHECKSUM
1132 if (ip->udp_xsum != 0) {
Wolfgang Denk30b87322005-08-12 23:43:12 +02001133 ulong xsum;
Stefan Roesedfced812005-08-12 20:06:52 +02001134 ushort *sumptr;
1135 ushort sumlen;
1136
1137 xsum = ip->ip_p;
1138 xsum += (ntohs(ip->udp_len));
1139 xsum += (ntohl(ip->ip_src) >> 16) & 0x0000ffff;
1140 xsum += (ntohl(ip->ip_src) >> 0) & 0x0000ffff;
1141 xsum += (ntohl(ip->ip_dst) >> 16) & 0x0000ffff;
1142 xsum += (ntohl(ip->ip_dst) >> 0) & 0x0000ffff;
1143
1144 sumlen = ntohs(ip->udp_len);
1145 sumptr = (ushort *) &(ip->udp_src);
1146
1147 while (sumlen > 1) {
Wolfgang Denk30b87322005-08-12 23:43:12 +02001148 ushort sumdata;
Stefan Roesedfced812005-08-12 20:06:52 +02001149
1150 sumdata = *sumptr++;
1151 xsum += ntohs(sumdata);
1152 sumlen -= 2;
1153 }
1154 if (sumlen > 0) {
Wolfgang Denk30b87322005-08-12 23:43:12 +02001155 ushort sumdata;
Stefan Roesedfced812005-08-12 20:06:52 +02001156
1157 sumdata = *(unsigned char *) sumptr;
Wolfgang Denk30b87322005-08-12 23:43:12 +02001158 sumdata = (sumdata << 8) & 0xff00;
Stefan Roesedfced812005-08-12 20:06:52 +02001159 xsum += sumdata;
1160 }
1161 while ((xsum >> 16) != 0) {
Luca Ceresoli5c83a962011-05-11 03:59:54 +00001162 xsum = (xsum & 0x0000ffff) +
1163 ((xsum >> 16) & 0x0000ffff);
Stefan Roesedfced812005-08-12 20:06:52 +02001164 }
1165 if ((xsum != 0x00000000) && (xsum != 0x0000ffff)) {
Wolfgang Denk12cec0a2008-07-11 01:16:00 +02001166 printf(" UDP wrong checksum %08lx %08x\n",
1167 xsum, ntohs(ip->udp_xsum));
Stefan Roesedfced812005-08-12 20:06:52 +02001168 return;
1169 }
1170 }
1171#endif
1172
David Updegraff7280da72007-06-11 10:41:07 -05001173
wdenkb8fb6192004-08-02 21:11:11 +00001174#ifdef CONFIG_NETCONSOLE
Joe Hershberger6fe8b452012-05-23 07:58:04 +00001175 nc_input_packet((uchar *)ip + IP_UDP_HDR_SIZE,
Joe Hershberger19d6bc02012-10-03 09:14:03 +00001176 src_ip,
Joe Hershberger6fe8b452012-05-23 07:58:04 +00001177 ntohs(ip->udp_dst),
1178 ntohs(ip->udp_src),
1179 ntohs(ip->udp_len) - UDP_HDR_SIZE);
wdenkb8fb6192004-08-02 21:11:11 +00001180#endif
wdenk2d966952002-10-31 22:12:35 +00001181 /*
1182 * IP header OK. Pass the packet to the current handler.
1183 */
Joe Hershbergerf50357b2012-05-23 07:59:15 +00001184 (*udp_packet_handler)((uchar *)ip + IP_UDP_HDR_SIZE,
1185 ntohs(ip->udp_dst),
1186 src_ip,
1187 ntohs(ip->udp_src),
1188 ntohs(ip->udp_len) - UDP_HDR_SIZE);
wdenk2d966952002-10-31 22:12:35 +00001189 break;
1190 }
1191}
1192
1193
1194/**********************************************************************/
1195
Simon Glassd6c5f552011-10-24 18:00:02 +00001196static int net_check_prereq(enum proto_t protocol)
wdenk2d966952002-10-31 22:12:35 +00001197{
1198 switch (protocol) {
wdenk05939202004-04-18 17:39:38 +00001199 /* Fall through */
Jon Loeliger54f35c22007-07-09 17:45:14 -05001200#if defined(CONFIG_CMD_PING)
wdenke6466f62003-06-05 19:27:42 +00001201 case PING:
wdenk05939202004-04-18 17:39:38 +00001202 if (NetPingIP == 0) {
Luca Ceresoli7cbd7482011-05-11 03:59:56 +00001203 puts("*** ERROR: ping address not given\n");
Luca Ceresoli5fe6de22011-05-04 02:40:45 +00001204 return 1;
wdenk05939202004-04-18 17:39:38 +00001205 }
1206 goto common;
wdenke6466f62003-06-05 19:27:42 +00001207#endif
Jon Loeliger54f35c22007-07-09 17:45:14 -05001208#if defined(CONFIG_CMD_SNTP)
wdenkb4ad9622005-04-01 00:25:43 +00001209 case SNTP:
1210 if (NetNtpServerIP == 0) {
Luca Ceresoli7cbd7482011-05-11 03:59:56 +00001211 puts("*** ERROR: NTP server address not given\n");
Luca Ceresoli5fe6de22011-05-04 02:40:45 +00001212 return 1;
wdenkb4ad9622005-04-01 00:25:43 +00001213 }
1214 goto common;
1215#endif
Robin Getz82f0d232009-07-20 14:53:54 -04001216#if defined(CONFIG_CMD_DNS)
1217 case DNS:
1218 if (NetOurDNSIP == 0) {
1219 puts("*** ERROR: DNS server address not given\n");
1220 return 1;
1221 }
1222 goto common;
1223#endif
Jon Loeliger54f35c22007-07-09 17:45:14 -05001224#if defined(CONFIG_CMD_NFS)
wdenkbe9c1cb2004-02-24 02:00:03 +00001225 case NFS:
1226#endif
Simon Glassd6c5f552011-10-24 18:00:02 +00001227 case TFTPGET:
Simon Glass6a398d22011-10-24 18:00:07 +00001228 case TFTPPUT:
wdenk05939202004-04-18 17:39:38 +00001229 if (NetServerIP == 0) {
Luca Ceresoli7cbd7482011-05-11 03:59:56 +00001230 puts("*** ERROR: `serverip' not set\n");
Luca Ceresoli5fe6de22011-05-04 02:40:45 +00001231 return 1;
wdenk05939202004-04-18 17:39:38 +00001232 }
Luca Ceresoli7cbd7482011-05-11 03:59:56 +00001233#if defined(CONFIG_CMD_PING) || defined(CONFIG_CMD_SNTP) || \
1234 defined(CONFIG_CMD_DNS)
1235common:
wdenke6466f62003-06-05 19:27:42 +00001236#endif
Simon Guinot00dceba2011-05-01 23:38:40 +00001237 /* Fall through */
wdenke6466f62003-06-05 19:27:42 +00001238
Simon Guinot00dceba2011-05-01 23:38:40 +00001239 case NETCONS:
Luca Ceresoli7aa81a42011-05-17 00:03:40 +00001240 case TFTPSRV:
wdenk05939202004-04-18 17:39:38 +00001241 if (NetOurIP == 0) {
Luca Ceresoli7cbd7482011-05-11 03:59:56 +00001242 puts("*** ERROR: `ipaddr' not set\n");
Luca Ceresoli5fe6de22011-05-04 02:40:45 +00001243 return 1;
wdenk05939202004-04-18 17:39:38 +00001244 }
1245 /* Fall through */
wdenk2d966952002-10-31 22:12:35 +00001246
Peter Tyserf7a48ca2010-09-30 11:25:48 -05001247#ifdef CONFIG_CMD_RARP
wdenk2d966952002-10-31 22:12:35 +00001248 case RARP:
Peter Tyserf7a48ca2010-09-30 11:25:48 -05001249#endif
wdenk2d966952002-10-31 22:12:35 +00001250 case BOOTP:
wdenk145d2c12004-04-15 21:48:45 +00001251 case CDP:
Peter Tyserf7a48ca2010-09-30 11:25:48 -05001252 case DHCP:
Joe Hershbergerb35a3a62012-05-23 08:00:12 +00001253 case LINKLOCAL:
Luca Ceresoli7cbd7482011-05-11 03:59:56 +00001254 if (memcmp(NetOurEther, "\0\0\0\0\0\0", 6) == 0) {
Luca Ceresoli7cbd7482011-05-11 03:59:56 +00001255 int num = eth_get_dev_index();
wdenk2d966952002-10-31 22:12:35 +00001256
wdenk05939202004-04-18 17:39:38 +00001257 switch (num) {
1258 case -1:
Luca Ceresoli7cbd7482011-05-11 03:59:56 +00001259 puts("*** ERROR: No ethernet found.\n");
Luca Ceresoli5fe6de22011-05-04 02:40:45 +00001260 return 1;
wdenk05939202004-04-18 17:39:38 +00001261 case 0:
Luca Ceresoli7cbd7482011-05-11 03:59:56 +00001262 puts("*** ERROR: `ethaddr' not set\n");
wdenk2d966952002-10-31 22:12:35 +00001263 break;
wdenk05939202004-04-18 17:39:38 +00001264 default:
Luca Ceresoli7cbd7482011-05-11 03:59:56 +00001265 printf("*** ERROR: `eth%daddr' not set\n",
wdenk2d966952002-10-31 22:12:35 +00001266 num);
1267 break;
wdenk05939202004-04-18 17:39:38 +00001268 }
wdenk2d966952002-10-31 22:12:35 +00001269
Luca Ceresoli7cbd7482011-05-11 03:59:56 +00001270 NetStartAgain();
Luca Ceresoli5fe6de22011-05-04 02:40:45 +00001271 return 2;
wdenk05939202004-04-18 17:39:38 +00001272 }
1273 /* Fall through */
1274 default:
Luca Ceresoli5fe6de22011-05-04 02:40:45 +00001275 return 0;
wdenk2d966952002-10-31 22:12:35 +00001276 }
Luca Ceresoli5fe6de22011-05-04 02:40:45 +00001277 return 0; /* OK */
wdenk2d966952002-10-31 22:12:35 +00001278}
1279/**********************************************************************/
1280
1281int
Luca Ceresolif27d91f2011-05-04 02:40:44 +00001282NetCksumOk(uchar *ptr, int len)
wdenk2d966952002-10-31 22:12:35 +00001283{
1284 return !((NetCksum(ptr, len) + 1) & 0xfffe);
1285}
1286
1287
1288unsigned
Luca Ceresolif27d91f2011-05-04 02:40:44 +00001289NetCksum(uchar *ptr, int len)
wdenk2d966952002-10-31 22:12:35 +00001290{
1291 ulong xsum;
Stefan Roeseac553282005-08-31 12:55:50 +02001292 ushort *p = (ushort *)ptr;
wdenk2d966952002-10-31 22:12:35 +00001293
1294 xsum = 0;
1295 while (len-- > 0)
Wolfgang Denk3135c542005-08-26 01:36:03 +02001296 xsum += *p++;
wdenk2d966952002-10-31 22:12:35 +00001297 xsum = (xsum & 0xffff) + (xsum >> 16);
1298 xsum = (xsum & 0xffff) + (xsum >> 16);
Luca Ceresoli5fe6de22011-05-04 02:40:45 +00001299 return xsum & 0xffff;
wdenk2d966952002-10-31 22:12:35 +00001300}
1301
wdenk145d2c12004-04-15 21:48:45 +00001302int
1303NetEthHdrSize(void)
1304{
1305 ushort myvlanid;
1306
1307 myvlanid = ntohs(NetOurVLAN);
1308 if (myvlanid == (ushort)-1)
1309 myvlanid = VLAN_NONE;
wdenk2d966952002-10-31 22:12:35 +00001310
Luca Ceresoli5c83a962011-05-11 03:59:54 +00001311 return ((myvlanid & VLAN_IDMASK) == VLAN_NONE) ? ETHER_HDR_SIZE :
1312 VLAN_ETHER_HDR_SIZE;
wdenk145d2c12004-04-15 21:48:45 +00001313}
1314
1315int
Joe Hershberger4b7747e2012-05-15 08:59:04 +00001316NetSetEther(uchar *xet, uchar * addr, uint prot)
wdenk2d966952002-10-31 22:12:35 +00001317{
Joe Hershberger1178f412012-05-23 07:58:06 +00001318 struct ethernet_hdr *et = (struct ethernet_hdr *)xet;
wdenk145d2c12004-04-15 21:48:45 +00001319 ushort myvlanid;
1320
1321 myvlanid = ntohs(NetOurVLAN);
1322 if (myvlanid == (ushort)-1)
1323 myvlanid = VLAN_NONE;
wdenk2d966952002-10-31 22:12:35 +00001324
Luca Ceresoli7cbd7482011-05-11 03:59:56 +00001325 memcpy(et->et_dest, addr, 6);
1326 memcpy(et->et_src, NetOurEther, 6);
wdenk145d2c12004-04-15 21:48:45 +00001327 if ((myvlanid & VLAN_IDMASK) == VLAN_NONE) {
Luca Ceresolib19d0b72011-05-04 02:40:46 +00001328 et->et_protlen = htons(prot);
wdenk145d2c12004-04-15 21:48:45 +00001329 return ETHER_HDR_SIZE;
1330 } else {
Joe Hershbergerb43784c2012-05-23 07:58:07 +00001331 struct vlan_ethernet_hdr *vet =
1332 (struct vlan_ethernet_hdr *)xet;
wdenk2d966952002-10-31 22:12:35 +00001333
wdenk145d2c12004-04-15 21:48:45 +00001334 vet->vet_vlan_type = htons(PROT_VLAN);
1335 vet->vet_tag = htons((0 << 5) | (myvlanid & VLAN_IDMASK));
1336 vet->vet_type = htons(prot);
1337 return VLAN_ETHER_HDR_SIZE;
1338 }
1339}
wdenk2d966952002-10-31 22:12:35 +00001340
Joe Hershberger530cd6b2012-05-23 07:59:16 +00001341int net_update_ether(struct ethernet_hdr *et, uchar *addr, uint prot)
1342{
1343 ushort protlen;
1344
1345 memcpy(et->et_dest, addr, 6);
1346 memcpy(et->et_src, NetOurEther, 6);
1347 protlen = ntohs(et->et_protlen);
1348 if (protlen == PROT_VLAN) {
1349 struct vlan_ethernet_hdr *vet =
1350 (struct vlan_ethernet_hdr *)et;
1351 vet->vet_type = htons(prot);
1352 return VLAN_ETHER_HDR_SIZE;
1353 } else if (protlen > 1514) {
1354 et->et_protlen = htons(prot);
1355 return ETHER_HDR_SIZE;
1356 } else {
1357 /* 802.2 + SNAP */
1358 struct e802_hdr *et802 = (struct e802_hdr *)et;
1359 et802->et_prot = htons(prot);
1360 return E802_HDR_SIZE;
1361 }
1362}
1363
Joe Hershberger2ed5b492012-05-23 07:59:07 +00001364void net_set_ip_header(uchar *pkt, IPaddr_t dest, IPaddr_t source)
wdenk2d966952002-10-31 22:12:35 +00001365{
Joe Hershberger2ed5b492012-05-23 07:59:07 +00001366 struct ip_udp_hdr *ip = (struct ip_udp_hdr *)pkt;
wdenk2d966952002-10-31 22:12:35 +00001367
1368 /*
Joe Hershberger2ed5b492012-05-23 07:59:07 +00001369 * Construct an IP header.
wdenk2d966952002-10-31 22:12:35 +00001370 */
Luca Ceresoli5c83a962011-05-11 03:59:54 +00001371 /* IP_HDR_SIZE / 4 (not including UDP) */
1372 ip->ip_hl_v = 0x45;
wdenk2d966952002-10-31 22:12:35 +00001373 ip->ip_tos = 0;
Joe Hershberger2ed5b492012-05-23 07:59:07 +00001374 ip->ip_len = htons(IP_HDR_SIZE);
wdenk2d966952002-10-31 22:12:35 +00001375 ip->ip_id = htons(NetIPID++);
Peter Tyser0885bf02008-12-01 16:26:20 -06001376 ip->ip_off = htons(IP_FLAGS_DFRAG); /* Don't fragment */
wdenk2d966952002-10-31 22:12:35 +00001377 ip->ip_ttl = 255;
wdenk2d966952002-10-31 22:12:35 +00001378 ip->ip_sum = 0;
Luca Ceresoli5c83a962011-05-11 03:59:54 +00001379 /* already in network byte order */
Joe Hershberger2ed5b492012-05-23 07:59:07 +00001380 NetCopyIP((void *)&ip->ip_src, &source);
1381 /* already in network byte order */
Luca Ceresolif27d91f2011-05-04 02:40:44 +00001382 NetCopyIP((void *)&ip->ip_dst, &dest);
Joe Hershberger2ed5b492012-05-23 07:59:07 +00001383}
1384
1385void net_set_udp_header(uchar *pkt, IPaddr_t dest, int dport, int sport,
1386 int len)
1387{
1388 struct ip_udp_hdr *ip = (struct ip_udp_hdr *)pkt;
1389
1390 /*
1391 * If the data is an odd number of bytes, zero the
1392 * byte after the last byte so that the checksum
1393 * will work.
1394 */
1395 if (len & 1)
1396 pkt[IP_UDP_HDR_SIZE + len] = 0;
1397
1398 net_set_ip_header(pkt, dest, NetOurIP);
1399 ip->ip_len = htons(IP_UDP_HDR_SIZE + len);
1400 ip->ip_p = IPPROTO_UDP;
1401 ip->ip_sum = ~NetCksum((uchar *)ip, IP_HDR_SIZE >> 1);
1402
wdenk2d966952002-10-31 22:12:35 +00001403 ip->udp_src = htons(sport);
1404 ip->udp_dst = htons(dport);
Joe Hershberger6fe8b452012-05-23 07:58:04 +00001405 ip->udp_len = htons(UDP_HDR_SIZE + len);
wdenk2d966952002-10-31 22:12:35 +00001406 ip->udp_xsum = 0;
wdenk2d966952002-10-31 22:12:35 +00001407}
1408
Luca Ceresoli7cbd7482011-05-11 03:59:56 +00001409void copy_filename(char *dst, const char *src, int size)
wdenk2d966952002-10-31 22:12:35 +00001410{
1411 if (*src && (*src == '"')) {
1412 ++src;
1413 --size;
1414 }
1415
Luca Ceresolie8ccbb02011-05-04 02:40:43 +00001416 while ((--size > 0) && *src && (*src != '"'))
wdenk2d966952002-10-31 22:12:35 +00001417 *dst++ = *src++;
wdenk2d966952002-10-31 22:12:35 +00001418 *dst = '\0';
1419}
1420
Luca Ceresoli5c83a962011-05-11 03:59:54 +00001421#if defined(CONFIG_CMD_NFS) || \
1422 defined(CONFIG_CMD_SNTP) || \
1423 defined(CONFIG_CMD_DNS)
Robin Getz82f0d232009-07-20 14:53:54 -04001424/*
Robin Getz55b50e92010-03-08 14:07:00 -05001425 * make port a little random (1024-17407)
1426 * This keeps the math somewhat trivial to compute, and seems to work with
1427 * all supported protocols/clients/servers
Robin Getz82f0d232009-07-20 14:53:54 -04001428 */
1429unsigned int random_port(void)
1430{
Robin Getz55b50e92010-03-08 14:07:00 -05001431 return 1024 + (get_timer(0) % 0x4000);
Robin Getz82f0d232009-07-20 14:53:54 -04001432}
1433#endif
1434
Luca Ceresoli7cbd7482011-05-11 03:59:56 +00001435void ip_to_string(IPaddr_t x, char *s)
wdenk2d966952002-10-31 22:12:35 +00001436{
Luca Ceresoli7cbd7482011-05-11 03:59:56 +00001437 x = ntohl(x);
1438 sprintf(s, "%d.%d.%d.%d",
1439 (int) ((x >> 24) & 0xff),
1440 (int) ((x >> 16) & 0xff),
1441 (int) ((x >> 8) & 0xff), (int) ((x >> 0) & 0xff)
wdenk05939202004-04-18 17:39:38 +00001442 );
wdenk2d966952002-10-31 22:12:35 +00001443}
1444
wdenk145d2c12004-04-15 21:48:45 +00001445void VLAN_to_string(ushort x, char *s)
1446{
1447 x = ntohs(x);
1448
1449 if (x == (ushort)-1)
1450 x = VLAN_NONE;
1451
1452 if (x == VLAN_NONE)
1453 strcpy(s, "none");
1454 else
1455 sprintf(s, "%d", x & VLAN_IDMASK);
1456}
1457
Mike Frysinger89c73922010-10-20 07:16:48 -04001458ushort string_to_VLAN(const char *s)
wdenk145d2c12004-04-15 21:48:45 +00001459{
1460 ushort id;
1461
1462 if (s == NULL)
wdenk656140b2004-04-25 13:18:40 +00001463 return htons(VLAN_NONE);
wdenk145d2c12004-04-15 21:48:45 +00001464
1465 if (*s < '0' || *s > '9')
1466 id = VLAN_NONE;
1467 else
1468 id = (ushort)simple_strtoul(s, NULL, 10);
1469
wdenk656140b2004-04-25 13:18:40 +00001470 return htons(id);
wdenk145d2c12004-04-15 21:48:45 +00001471}
1472
wdenk145d2c12004-04-15 21:48:45 +00001473ushort getenv_VLAN(char *var)
1474{
Luca Ceresoli5fe6de22011-05-04 02:40:45 +00001475 return string_to_VLAN(getenv(var));
wdenk145d2c12004-04-15 21:48:45 +00001476}