blob: df94789de95f603000d91b2e14d0fec16b544e31 [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 }
Joe Hershberger3d905d22012-07-23 13:11:16 +0000274 memcpy(NetOurEther, eth_get_dev()->enetaddr, 6);
Michael Zaidmanb97bfe42009-04-04 01:43:00 +0300275
Heiko Schocher3b195ff2009-04-28 08:36:11 +0200276 return;
Heiko Schocher0c303fa2009-02-10 09:38:52 +0100277}
278
Joe Hershbergerf50357b2012-05-23 07:59:15 +0000279static void net_clear_handlers(void)
280{
281 net_set_udp_handler(NULL);
282 net_set_arp_handler(NULL);
283 NetSetTimeout(0, NULL);
284}
285
286static void net_cleanup_loop(void)
287{
288 net_clear_handlers();
289}
290
Joe Hershberger017e5c42012-05-23 07:59:22 +0000291void net_init(void)
292{
293 static int first_call = 1;
294
295 if (first_call) {
296 /*
297 * Setup packet buffers, aligned correctly.
298 */
299 int i;
300
301 NetTxPacket = &PktBuf[0] + (PKTALIGN - 1);
302 NetTxPacket -= (ulong)NetTxPacket % PKTALIGN;
303 for (i = 0; i < PKTBUFSRX; i++)
304 NetRxPackets[i] = NetTxPacket + (i + 1) * PKTSIZE_ALIGN;
305
306 ArpInit();
307 net_clear_handlers();
308
309 /* Only need to setup buffer pointers once. */
310 first_call = 0;
311 }
312
313 NetInitLoop();
314}
315
wdenke6466f62003-06-05 19:27:42 +0000316/**********************************************************************/
wdenk2d966952002-10-31 22:12:35 +0000317/*
318 * Main network processing loop.
319 */
320
Simon Glassd6c5f552011-10-24 18:00:02 +0000321int NetLoop(enum proto_t protocol)
wdenk2d966952002-10-31 22:12:35 +0000322{
wdenk2d966952002-10-31 22:12:35 +0000323 bd_t *bd = gd->bd;
Simon Glass230467c2011-10-24 18:00:01 +0000324 int ret = -1;
wdenk2d966952002-10-31 22:12:35 +0000325
wdenk2d966952002-10-31 22:12:35 +0000326 NetRestarted = 0;
327 NetDevExists = 0;
Remy Bohmer0ddb8e82009-10-28 22:13:39 +0100328 NetTryCount = 1;
Joe Hershberger05a377b2012-05-23 08:01:04 +0000329 debug_cond(DEBUG_INT_STATE, "--- NetLoop Entry\n");
wdenke6466f62003-06-05 19:27:42 +0000330
Simon Glass768cbf02011-12-10 11:08:06 +0000331 bootstage_mark_name(BOOTSTAGE_ID_ETH_START, "eth_start");
Joe Hershberger017e5c42012-05-23 07:59:22 +0000332 net_init();
Joe Hershberger9f374062012-08-03 10:59:08 +0000333 if (eth_is_on_demand_init() || protocol != NETCONS) {
wdenkfa66e932005-04-03 14:52:59 +0000334 eth_halt();
Joe Hershberger9f374062012-08-03 10:59:08 +0000335 eth_set_current();
336 if (eth_init(bd) < 0) {
337 eth_halt();
338 return -1;
339 }
340 } else
341 eth_init_state_only(bd);
wdenk2d966952002-10-31 22:12:35 +0000342
343restart:
Joe Hershbergerd4bb76a2012-05-23 07:59:14 +0000344 net_set_state(NETLOOP_CONTINUE);
wdenk2d966952002-10-31 22:12:35 +0000345
346 /*
347 * Start the ball rolling with the given start function. From
348 * here on, this code is a state machine driven by received
349 * packets and timer events.
350 */
Joe Hershberger05a377b2012-05-23 08:01:04 +0000351 debug_cond(DEBUG_INT_STATE, "--- NetLoop Init\n");
Joe Hershberger202c6002012-05-23 07:59:21 +0000352 NetInitLoop();
wdenk2d966952002-10-31 22:12:35 +0000353
Luca Ceresoli7cbd7482011-05-11 03:59:56 +0000354 switch (net_check_prereq(protocol)) {
wdenk2d966952002-10-31 22:12:35 +0000355 case 1:
356 /* network not configured */
wdenkfa66e932005-04-03 14:52:59 +0000357 eth_halt();
Luca Ceresoli5fe6de22011-05-04 02:40:45 +0000358 return -1;
wdenk2d966952002-10-31 22:12:35 +0000359
wdenk2d966952002-10-31 22:12:35 +0000360 case 2:
361 /* network device not configured */
362 break;
wdenk2d966952002-10-31 22:12:35 +0000363
364 case 0:
wdenk2d966952002-10-31 22:12:35 +0000365 NetDevExists = 1;
Simon Glassd6c5f552011-10-24 18:00:02 +0000366 NetBootFileXferSize = 0;
wdenk2d966952002-10-31 22:12:35 +0000367 switch (protocol) {
Simon Glassd6c5f552011-10-24 18:00:02 +0000368 case TFTPGET:
Simon Glass6a398d22011-10-24 18:00:07 +0000369#ifdef CONFIG_CMD_TFTPPUT
370 case TFTPPUT:
371#endif
wdenk2d966952002-10-31 22:12:35 +0000372 /* always use ARP to get server ethernet address */
Simon Glassd6c5f552011-10-24 18:00:02 +0000373 TftpStart(protocol);
wdenk2d966952002-10-31 22:12:35 +0000374 break;
Luca Ceresoli7aa81a42011-05-17 00:03:40 +0000375#ifdef CONFIG_CMD_TFTPSRV
376 case TFTPSRV:
377 TftpStartServer();
378 break;
379#endif
Jon Loeliger54f35c22007-07-09 17:45:14 -0500380#if defined(CONFIG_CMD_DHCP)
wdenk2d966952002-10-31 22:12:35 +0000381 case DHCP:
wdenkc3919532004-10-11 22:51:13 +0000382 BootpTry = 0;
Michael Zaidman16bb21d2009-07-14 23:37:12 +0300383 NetOurIP = 0;
wdenk2d966952002-10-31 22:12:35 +0000384 DhcpRequest(); /* Basically same as BOOTP */
385 break;
Jon Loeligera9807e52007-07-10 11:05:02 -0500386#endif
wdenk2d966952002-10-31 22:12:35 +0000387
388 case BOOTP:
389 BootpTry = 0;
Michael Zaidman16bb21d2009-07-14 23:37:12 +0300390 NetOurIP = 0;
Luca Ceresoli7cbd7482011-05-11 03:59:56 +0000391 BootpRequest();
wdenk2d966952002-10-31 22:12:35 +0000392 break;
393
Peter Tyserf7a48ca2010-09-30 11:25:48 -0500394#if defined(CONFIG_CMD_RARP)
wdenk2d966952002-10-31 22:12:35 +0000395 case RARP:
396 RarpTry = 0;
Michael Zaidman16bb21d2009-07-14 23:37:12 +0300397 NetOurIP = 0;
Luca Ceresoli7cbd7482011-05-11 03:59:56 +0000398 RarpRequest();
wdenk2d966952002-10-31 22:12:35 +0000399 break;
Peter Tyserf7a48ca2010-09-30 11:25:48 -0500400#endif
Jon Loeliger54f35c22007-07-09 17:45:14 -0500401#if defined(CONFIG_CMD_PING)
wdenke6466f62003-06-05 19:27:42 +0000402 case PING:
Joe Hershbergerc21bf372012-05-23 07:58:02 +0000403 ping_start();
wdenke6466f62003-06-05 19:27:42 +0000404 break;
405#endif
Jon Loeliger54f35c22007-07-09 17:45:14 -0500406#if defined(CONFIG_CMD_NFS)
wdenkbe9c1cb2004-02-24 02:00:03 +0000407 case NFS:
408 NfsStart();
409 break;
410#endif
Jon Loeliger54f35c22007-07-09 17:45:14 -0500411#if defined(CONFIG_CMD_CDP)
wdenk145d2c12004-04-15 21:48:45 +0000412 case CDP:
413 CDPStart();
414 break;
415#endif
wdenkb8fb6192004-08-02 21:11:11 +0000416#ifdef CONFIG_NETCONSOLE
417 case NETCONS:
418 NcStart();
419 break;
420#endif
Jon Loeliger54f35c22007-07-09 17:45:14 -0500421#if defined(CONFIG_CMD_SNTP)
wdenkb4ad9622005-04-01 00:25:43 +0000422 case SNTP:
423 SntpStart();
424 break;
425#endif
Robin Getz82f0d232009-07-20 14:53:54 -0400426#if defined(CONFIG_CMD_DNS)
427 case DNS:
428 DnsStart();
429 break;
430#endif
Joe Hershbergerb35a3a62012-05-23 08:00:12 +0000431#if defined(CONFIG_CMD_LINK_LOCAL)
432 case LINKLOCAL:
433 link_local_start();
434 break;
435#endif
wdenk2d966952002-10-31 22:12:35 +0000436 default:
437 break;
438 }
439
wdenk2d966952002-10-31 22:12:35 +0000440 break;
441 }
442
Jon Loeliger54f35c22007-07-09 17:45:14 -0500443#if defined(CONFIG_MII) || defined(CONFIG_CMD_MII)
Luca Ceresoli5c83a962011-05-11 03:59:54 +0000444#if defined(CONFIG_SYS_FAULT_ECHO_LINK_DOWN) && \
445 defined(CONFIG_STATUS_LED) && \
446 defined(STATUS_LED_RED)
wdenk49c3f672003-10-08 22:33:00 +0000447 /*
wdenk9c53f402003-10-15 23:53:47 +0000448 * Echo the inverted link state to the fault LED.
wdenk49c3f672003-10-08 22:33:00 +0000449 */
Luca Ceresolie8ccbb02011-05-04 02:40:43 +0000450 if (miiphy_link(eth_get_dev()->name, CONFIG_SYS_FAULT_MII_ADDR))
Luca Ceresoli7cbd7482011-05-11 03:59:56 +0000451 status_led_set(STATUS_LED_RED, STATUS_LED_OFF);
Luca Ceresolie8ccbb02011-05-04 02:40:43 +0000452 else
Luca Ceresoli7cbd7482011-05-11 03:59:56 +0000453 status_led_set(STATUS_LED_RED, STATUS_LED_ON);
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200454#endif /* CONFIG_SYS_FAULT_ECHO_LINK_DOWN, ... */
wdenk49c3f672003-10-08 22:33:00 +0000455#endif /* CONFIG_MII, ... */
wdenk2d966952002-10-31 22:12:35 +0000456
457 /*
458 * Main packet reception loop. Loop receiving packets until
Joe Hershbergerd4bb76a2012-05-23 07:59:14 +0000459 * someone sets `net_state' to a state that terminates.
wdenk2d966952002-10-31 22:12:35 +0000460 */
461 for (;;) {
462 WATCHDOG_RESET();
463#ifdef CONFIG_SHOW_ACTIVITY
Joe Hershberger77001b432012-05-15 08:59:08 +0000464 show_activity(1);
wdenk2d966952002-10-31 22:12:35 +0000465#endif
466 /*
467 * Check the ethernet for a new packet. The ethernet
468 * receive routine will process it.
469 */
Guennadi Liakhovetskib38c2b32008-04-03 17:04:19 +0200470 eth_rx();
wdenk2d966952002-10-31 22:12:35 +0000471
472 /*
473 * Abort if ctrl-c was pressed.
474 */
475 if (ctrlc()) {
Joe Hershbergerde8205a2012-05-23 07:59:24 +0000476 /* cancel any ARP that may not have completed */
477 NetArpWaitPacketIP = 0;
478
Joe Hershbergerf50357b2012-05-23 07:59:15 +0000479 net_cleanup_loop();
wdenk57b2d802003-06-27 21:31:46 +0000480 eth_halt();
Joe Hershberger9f374062012-08-03 10:59:08 +0000481 /* Invalidate the last protocol */
482 eth_set_last_protocol(BOOTP);
483
Luca Ceresoli7cbd7482011-05-11 03:59:56 +0000484 puts("\nAbort\n");
Joe Hershberger05a377b2012-05-23 08:01:04 +0000485 /* include a debug print as well incase the debug
486 messages are directed to stderr */
487 debug_cond(DEBUG_INT_STATE, "--- NetLoop Abort!\n");
Simon Glass230467c2011-10-24 18:00:01 +0000488 goto done;
wdenk2d966952002-10-31 22:12:35 +0000489 }
490
wdenke6466f62003-06-05 19:27:42 +0000491 ArpTimeoutCheck();
wdenk2d966952002-10-31 22:12:35 +0000492
493 /*
494 * Check for a timeout, and run the timeout handler
495 * if we have one.
496 */
wdenk5d841732003-08-17 18:55:18 +0000497 if (timeHandler && ((get_timer(0) - timeStart) > timeDelta)) {
wdenk2d966952002-10-31 22:12:35 +0000498 thand_f *x;
499
Jon Loeliger54f35c22007-07-09 17:45:14 -0500500#if defined(CONFIG_MII) || defined(CONFIG_CMD_MII)
Luca Ceresoli7cbd7482011-05-11 03:59:56 +0000501#if defined(CONFIG_SYS_FAULT_ECHO_LINK_DOWN) && \
502 defined(CONFIG_STATUS_LED) && \
503 defined(STATUS_LED_RED)
wdenk49c3f672003-10-08 22:33:00 +0000504 /*
wdenk9c53f402003-10-15 23:53:47 +0000505 * Echo the inverted link state to the fault LED.
wdenk49c3f672003-10-08 22:33:00 +0000506 */
Luca Ceresoli7cbd7482011-05-11 03:59:56 +0000507 if (miiphy_link(eth_get_dev()->name,
Luca Ceresoli5c83a962011-05-11 03:59:54 +0000508 CONFIG_SYS_FAULT_MII_ADDR)) {
Luca Ceresoli7cbd7482011-05-11 03:59:56 +0000509 status_led_set(STATUS_LED_RED, STATUS_LED_OFF);
wdenk49c3f672003-10-08 22:33:00 +0000510 } else {
Luca Ceresoli7cbd7482011-05-11 03:59:56 +0000511 status_led_set(STATUS_LED_RED, STATUS_LED_ON);
wdenk49c3f672003-10-08 22:33:00 +0000512 }
Luca Ceresoli7cbd7482011-05-11 03:59:56 +0000513#endif /* CONFIG_SYS_FAULT_ECHO_LINK_DOWN, ... */
wdenk49c3f672003-10-08 22:33:00 +0000514#endif /* CONFIG_MII, ... */
Joe Hershberger05a377b2012-05-23 08:01:04 +0000515 debug_cond(DEBUG_INT_STATE, "--- NetLoop timeout\n");
wdenk2d966952002-10-31 22:12:35 +0000516 x = timeHandler;
517 timeHandler = (thand_f *)0;
518 (*x)();
519 }
520
521
Joe Hershbergerd4bb76a2012-05-23 07:59:14 +0000522 switch (net_state) {
wdenk2d966952002-10-31 22:12:35 +0000523
524 case NETLOOP_RESTART:
wdenk2d966952002-10-31 22:12:35 +0000525 NetRestarted = 1;
wdenk2d966952002-10-31 22:12:35 +0000526 goto restart;
527
528 case NETLOOP_SUCCESS:
Joe Hershbergerf50357b2012-05-23 07:59:15 +0000529 net_cleanup_loop();
wdenk2d966952002-10-31 22:12:35 +0000530 if (NetBootFileXferSize > 0) {
wdenk2d966952002-10-31 22:12:35 +0000531 printf("Bytes transferred = %ld (%lx hex)\n",
532 NetBootFileXferSize,
533 NetBootFileXferSize);
Simon Glass596295d2013-02-24 17:33:24 +0000534 setenv_hex("filesize", NetBootFileXferSize);
535 setenv_hex("fileaddr", load_addr);
wdenk2d966952002-10-31 22:12:35 +0000536 }
Joe Hershberger9f374062012-08-03 10:59:08 +0000537 if (protocol != NETCONS)
538 eth_halt();
539 else
540 eth_halt_state_only();
541
542 eth_set_last_protocol(protocol);
543
Simon Glass230467c2011-10-24 18:00:01 +0000544 ret = NetBootFileXferSize;
Joe Hershberger05a377b2012-05-23 08:01:04 +0000545 debug_cond(DEBUG_INT_STATE, "--- NetLoop Success!\n");
Simon Glass230467c2011-10-24 18:00:01 +0000546 goto done;
wdenk2d966952002-10-31 22:12:35 +0000547
548 case NETLOOP_FAIL:
Joe Hershbergerf50357b2012-05-23 07:59:15 +0000549 net_cleanup_loop();
Joe Hershberger9f374062012-08-03 10:59:08 +0000550 /* Invalidate the last protocol */
551 eth_set_last_protocol(BOOTP);
Joe Hershberger05a377b2012-05-23 08:01:04 +0000552 debug_cond(DEBUG_INT_STATE, "--- NetLoop Fail!\n");
Simon Glass230467c2011-10-24 18:00:01 +0000553 goto done;
Joe Hershbergerd4bb76a2012-05-23 07:59:14 +0000554
555 case NETLOOP_CONTINUE:
556 continue;
wdenk2d966952002-10-31 22:12:35 +0000557 }
558 }
Simon Glass230467c2011-10-24 18:00:01 +0000559
560done:
Simon Glass2928cd82011-10-26 14:18:38 +0000561#ifdef CONFIG_CMD_TFTPPUT
Simon Glass230467c2011-10-24 18:00:01 +0000562 /* Clear out the handlers */
Joe Hershbergerf50357b2012-05-23 07:59:15 +0000563 net_set_udp_handler(NULL);
Simon Glass230467c2011-10-24 18:00:01 +0000564 net_set_icmp_handler(NULL);
Simon Glass2928cd82011-10-26 14:18:38 +0000565#endif
Simon Glass230467c2011-10-24 18:00:01 +0000566 return ret;
wdenk2d966952002-10-31 22:12:35 +0000567}
568
569/**********************************************************************/
570
571static void
572startAgainTimeout(void)
573{
Joe Hershbergerd4bb76a2012-05-23 07:59:14 +0000574 net_set_state(NETLOOP_RESTART);
wdenk2d966952002-10-31 22:12:35 +0000575}
576
Luca Ceresoli7cbd7482011-05-11 03:59:56 +0000577void NetStartAgain(void)
wdenk2d966952002-10-31 22:12:35 +0000578{
wdenk05939202004-04-18 17:39:38 +0000579 char *nretry;
Remy Bohmer0ddb8e82009-10-28 22:13:39 +0100580 int retry_forever = 0;
581 unsigned long retrycnt = 0;
wdenk145d2c12004-04-15 21:48:45 +0000582
Remy Bohmer0ddb8e82009-10-28 22:13:39 +0100583 nretry = getenv("netretry");
584 if (nretry) {
585 if (!strcmp(nretry, "yes"))
586 retry_forever = 1;
587 else if (!strcmp(nretry, "no"))
588 retrycnt = 0;
589 else if (!strcmp(nretry, "once"))
590 retrycnt = 1;
591 else
592 retrycnt = simple_strtoul(nretry, NULL, 0);
593 } else
594 retry_forever = 1;
595
596 if ((!retry_forever) && (NetTryCount >= retrycnt)) {
597 eth_halt();
Joe Hershbergerd4bb76a2012-05-23 07:59:14 +0000598 net_set_state(NETLOOP_FAIL);
wdenk145d2c12004-04-15 21:48:45 +0000599 return;
600 }
Remy Bohmer0ddb8e82009-10-28 22:13:39 +0100601
602 NetTryCount++;
603
Luca Ceresoli7cbd7482011-05-11 03:59:56 +0000604 eth_halt();
Matthias Fuchsa02d62b2007-12-27 16:58:41 +0100605#if !defined(CONFIG_NET_DO_NOT_TRY_ANOTHER)
Luca Ceresoli7cbd7482011-05-11 03:59:56 +0000606 eth_try_another(!NetRestarted);
Matthias Fuchsa02d62b2007-12-27 16:58:41 +0100607#endif
Luca Ceresoli7cbd7482011-05-11 03:59:56 +0000608 eth_init(gd->bd);
wdenk05939202004-04-18 17:39:38 +0000609 if (NetRestartWrap) {
wdenk2d966952002-10-31 22:12:35 +0000610 NetRestartWrap = 0;
Remy Bohmer0ddb8e82009-10-28 22:13:39 +0100611 if (NetDevExists) {
Luca Ceresoli7cbd7482011-05-11 03:59:56 +0000612 NetSetTimeout(10000UL, startAgainTimeout);
Joe Hershbergerf50357b2012-05-23 07:59:15 +0000613 net_set_udp_handler(NULL);
wdenk05939202004-04-18 17:39:38 +0000614 } else {
Joe Hershbergerd4bb76a2012-05-23 07:59:14 +0000615 net_set_state(NETLOOP_FAIL);
wdenk2d966952002-10-31 22:12:35 +0000616 }
wdenk05939202004-04-18 17:39:38 +0000617 } else {
Joe Hershbergerd4bb76a2012-05-23 07:59:14 +0000618 net_set_state(NETLOOP_RESTART);
wdenk2d966952002-10-31 22:12:35 +0000619 }
wdenk2d966952002-10-31 22:12:35 +0000620}
621
622/**********************************************************************/
623/*
624 * Miscelaneous bits.
625 */
626
Joe Hershbergerf50357b2012-05-23 07:59:15 +0000627static void dummy_handler(uchar *pkt, unsigned dport,
628 IPaddr_t sip, unsigned sport,
629 unsigned len)
Joe Hershbergeraae05082012-05-23 07:58:01 +0000630{
Joe Hershbergeraae05082012-05-23 07:58:01 +0000631}
632
Joe Hershbergerf50357b2012-05-23 07:59:15 +0000633rxhand_f *net_get_udp_handler(void)
634{
635 return udp_packet_handler;
636}
Joe Hershbergeraae05082012-05-23 07:58:01 +0000637
Joe Hershbergerf50357b2012-05-23 07:59:15 +0000638void net_set_udp_handler(rxhand_f *f)
639{
Joe Hershberger05a377b2012-05-23 08:01:04 +0000640 debug_cond(DEBUG_INT_STATE, "--- NetLoop UDP handler set (%p)\n", f);
Joe Hershbergerf50357b2012-05-23 07:59:15 +0000641 if (f == NULL)
642 udp_packet_handler = dummy_handler;
643 else
644 udp_packet_handler = f;
645}
646
647rxhand_f *net_get_arp_handler(void)
wdenk2d966952002-10-31 22:12:35 +0000648{
Joe Hershbergerf50357b2012-05-23 07:59:15 +0000649 return arp_packet_handler;
wdenk2d966952002-10-31 22:12:35 +0000650}
651
Joe Hershbergerf50357b2012-05-23 07:59:15 +0000652void net_set_arp_handler(rxhand_f *f)
653{
Joe Hershberger05a377b2012-05-23 08:01:04 +0000654 debug_cond(DEBUG_INT_STATE, "--- NetLoop ARP handler set (%p)\n", f);
Joe Hershbergerf50357b2012-05-23 07:59:15 +0000655 if (f == NULL)
656 arp_packet_handler = dummy_handler;
657 else
658 arp_packet_handler = f;
659}
660
Simon Glass2928cd82011-10-26 14:18:38 +0000661#ifdef CONFIG_CMD_TFTPPUT
Simon Glass230467c2011-10-24 18:00:01 +0000662void net_set_icmp_handler(rxhand_icmp_f *f)
663{
664 packet_icmp_handler = f;
665}
Simon Glass2928cd82011-10-26 14:18:38 +0000666#endif
wdenk2d966952002-10-31 22:12:35 +0000667
668void
Luca Ceresolif27d91f2011-05-04 02:40:44 +0000669NetSetTimeout(ulong iv, thand_f *f)
wdenk2d966952002-10-31 22:12:35 +0000670{
671 if (iv == 0) {
Joe Hershberger05a377b2012-05-23 08:01:04 +0000672 debug_cond(DEBUG_INT_STATE,
673 "--- NetLoop timeout handler cancelled\n");
wdenk2d966952002-10-31 22:12:35 +0000674 timeHandler = (thand_f *)0;
675 } else {
Joe Hershberger05a377b2012-05-23 08:01:04 +0000676 debug_cond(DEBUG_INT_STATE,
677 "--- NetLoop timeout handler set (%p)\n", f);
wdenk2d966952002-10-31 22:12:35 +0000678 timeHandler = f;
wdenk5d841732003-08-17 18:55:18 +0000679 timeStart = get_timer(0);
Tetsuyuki Kobayashibf176182012-06-25 02:37:27 +0000680 timeDelta = iv * CONFIG_SYS_HZ / 1000;
wdenk2d966952002-10-31 22:12:35 +0000681 }
682}
683
Joe Hershberger1a6b8d82012-05-23 07:58:10 +0000684int NetSendUDPPacket(uchar *ether, IPaddr_t dest, int dport, int sport,
685 int payload_len)
wdenke6466f62003-06-05 19:27:42 +0000686{
wdenk145d2c12004-04-15 21:48:45 +0000687 uchar *pkt;
Joe Hershberger16be9cb2012-05-23 07:59:08 +0000688 int eth_hdr_size;
689 int pkt_hdr_size;
wdenk145d2c12004-04-15 21:48:45 +0000690
Joe Hershberger017e5c42012-05-23 07:59:22 +0000691 /* make sure the NetTxPacket is initialized (NetInit() was called) */
692 assert(NetTxPacket != NULL);
693 if (NetTxPacket == NULL)
694 return -1;
695
wdenke6466f62003-06-05 19:27:42 +0000696 /* convert to new style broadcast */
697 if (dest == 0)
698 dest = 0xFFFFFFFF;
699
700 /* if broadcast, make the ether address a broadcast and don't do ARP */
701 if (dest == 0xFFFFFFFF)
702 ether = NetBcastAddr;
703
Joe Hershbergerde8205a2012-05-23 07:59:24 +0000704 pkt = (uchar *)NetTxPacket;
Joe Hershberger16be9cb2012-05-23 07:59:08 +0000705
706 eth_hdr_size = NetSetEther(pkt, ether, PROT_IP);
707 pkt += eth_hdr_size;
708 net_set_udp_header(pkt, dest, dport, sport, payload_len);
709 pkt_hdr_size = eth_hdr_size + IP_UDP_HDR_SIZE;
wdenke6466f62003-06-05 19:27:42 +0000710
Joe Hershbergerde8205a2012-05-23 07:59:24 +0000711 /* if MAC address was not discovered yet, do an ARP request */
712 if (memcmp(ether, NetEtherNullAddr, 6) == 0) {
Joe Hershberger05a377b2012-05-23 08:01:04 +0000713 debug_cond(DEBUG_DEV_PKT, "sending ARP for %pI4\n", &dest);
Robin Getz9e0a4d62009-07-23 03:01:03 -0400714
Joe Hershberger16be9cb2012-05-23 07:59:08 +0000715 /* save the ip and eth addr for the packet to send after arp */
wdenke6466f62003-06-05 19:27:42 +0000716 NetArpWaitPacketIP = dest;
717 NetArpWaitPacketMAC = ether;
wdenk145d2c12004-04-15 21:48:45 +0000718
wdenke6466f62003-06-05 19:27:42 +0000719 /* size of the waiting packet */
Joe Hershberger16be9cb2012-05-23 07:59:08 +0000720 NetArpWaitTxPacketSize = pkt_hdr_size + payload_len;
wdenke6466f62003-06-05 19:27:42 +0000721
722 /* and do the ARP request */
723 NetArpWaitTry = 1;
724 NetArpWaitTimerStart = get_timer(0);
725 ArpRequest();
726 return 1; /* waiting */
Joe Hershberger16be9cb2012-05-23 07:59:08 +0000727 } else {
Joe Hershberger05a377b2012-05-23 08:01:04 +0000728 debug_cond(DEBUG_DEV_PKT, "sending UDP to %pI4/%pM\n",
729 &dest, ether);
Joe Hershbergerebe3ca82012-05-23 07:59:13 +0000730 NetSendPacket(NetTxPacket, pkt_hdr_size + payload_len);
Joe Hershberger16be9cb2012-05-23 07:59:08 +0000731 return 0; /* transmitted */
wdenke6466f62003-06-05 19:27:42 +0000732 }
wdenke6466f62003-06-05 19:27:42 +0000733}
wdenk145d2c12004-04-15 21:48:45 +0000734
Alessandro Rubinif119e3d2009-08-07 13:58:56 +0200735#ifdef CONFIG_IP_DEFRAG
736/*
737 * This function collects fragments in a single packet, according
738 * to the algorithm in RFC815. It returns NULL or the pointer to
739 * a complete packet, in static storage
740 */
741#ifndef CONFIG_NET_MAXDEFRAG
742#define CONFIG_NET_MAXDEFRAG 16384
743#endif
744/*
745 * MAXDEFRAG, above, is chosen in the config file and is real data
746 * so we need to add the NFS overhead, which is more than TFTP.
747 * To use sizeof in the internal unnamed structures, we need a real
748 * instance (can't do "sizeof(struct rpc_t.u.reply))", unfortunately).
749 * The compiler doesn't complain nor allocates the actual structure
750 */
751static struct rpc_t rpc_specimen;
752#define IP_PKTSIZE (CONFIG_NET_MAXDEFRAG + sizeof(rpc_specimen.u.reply))
753
Joe Hershbergerc686fa12012-05-23 07:58:05 +0000754#define IP_MAXUDP (IP_PKTSIZE - IP_HDR_SIZE)
Alessandro Rubinif119e3d2009-08-07 13:58:56 +0200755
756/*
757 * this is the packet being assembled, either data or frag control.
758 * Fragments go by 8 bytes, so this union must be 8 bytes long
759 */
760struct hole {
761 /* first_byte is address of this structure */
762 u16 last_byte; /* last byte in this hole + 1 (begin of next hole) */
763 u16 next_hole; /* index of next (in 8-b blocks), 0 == none */
764 u16 prev_hole; /* index of prev, 0 == none */
765 u16 unused;
766};
767
Joe Hershberger6fe8b452012-05-23 07:58:04 +0000768static struct ip_udp_hdr *__NetDefragment(struct ip_udp_hdr *ip, int *lenp)
Alessandro Rubinif119e3d2009-08-07 13:58:56 +0200769{
Joe Hershberger77001b432012-05-15 08:59:08 +0000770 static uchar pkt_buff[IP_PKTSIZE] __aligned(PKTALIGN);
Alessandro Rubinif119e3d2009-08-07 13:58:56 +0200771 static u16 first_hole, total_len;
772 struct hole *payload, *thisfrag, *h, *newh;
Joe Hershberger6fe8b452012-05-23 07:58:04 +0000773 struct ip_udp_hdr *localip = (struct ip_udp_hdr *)pkt_buff;
Alessandro Rubinif119e3d2009-08-07 13:58:56 +0200774 uchar *indata = (uchar *)ip;
775 int offset8, start, len, done = 0;
776 u16 ip_off = ntohs(ip->ip_off);
777
778 /* payload starts after IP header, this fragment is in there */
Joe Hershbergerc686fa12012-05-23 07:58:05 +0000779 payload = (struct hole *)(pkt_buff + IP_HDR_SIZE);
Alessandro Rubinif119e3d2009-08-07 13:58:56 +0200780 offset8 = (ip_off & IP_OFFS);
781 thisfrag = payload + offset8;
782 start = offset8 * 8;
Joe Hershbergerc686fa12012-05-23 07:58:05 +0000783 len = ntohs(ip->ip_len) - IP_HDR_SIZE;
Alessandro Rubinif119e3d2009-08-07 13:58:56 +0200784
785 if (start + len > IP_MAXUDP) /* fragment extends too far */
786 return NULL;
787
788 if (!total_len || localip->ip_id != ip->ip_id) {
789 /* new (or different) packet, reset structs */
790 total_len = 0xffff;
791 payload[0].last_byte = ~0;
792 payload[0].next_hole = 0;
793 payload[0].prev_hole = 0;
794 first_hole = 0;
795 /* any IP header will work, copy the first we received */
Joe Hershbergerc686fa12012-05-23 07:58:05 +0000796 memcpy(localip, ip, IP_HDR_SIZE);
Alessandro Rubinif119e3d2009-08-07 13:58:56 +0200797 }
798
799 /*
800 * What follows is the reassembly algorithm. We use the payload
801 * array as a linked list of hole descriptors, as each hole starts
802 * at a multiple of 8 bytes. However, last byte can be whatever value,
803 * so it is represented as byte count, not as 8-byte blocks.
804 */
805
806 h = payload + first_hole;
807 while (h->last_byte < start) {
808 if (!h->next_hole) {
809 /* no hole that far away */
810 return NULL;
811 }
812 h = payload + h->next_hole;
813 }
814
Fillod Stephanee7ade8b2010-06-11 19:26:43 +0200815 /* last fragment may be 1..7 bytes, the "+7" forces acceptance */
816 if (offset8 + ((len + 7) / 8) <= h - payload) {
Alessandro Rubinif119e3d2009-08-07 13:58:56 +0200817 /* no overlap with holes (dup fragment?) */
818 return NULL;
819 }
820
821 if (!(ip_off & IP_FLAGS_MFRAG)) {
822 /* no more fragmentss: truncate this (last) hole */
823 total_len = start + len;
824 h->last_byte = start + len;
825 }
826
827 /*
828 * There is some overlap: fix the hole list. This code doesn't
829 * deal with a fragment that overlaps with two different holes
830 * (thus being a superset of a previously-received fragment).
831 */
832
Luca Ceresoli7cbd7482011-05-11 03:59:56 +0000833 if ((h >= thisfrag) && (h->last_byte <= start + len)) {
Alessandro Rubinif119e3d2009-08-07 13:58:56 +0200834 /* complete overlap with hole: remove hole */
835 if (!h->prev_hole && !h->next_hole) {
836 /* last remaining hole */
837 done = 1;
838 } else if (!h->prev_hole) {
839 /* first hole */
840 first_hole = h->next_hole;
841 payload[h->next_hole].prev_hole = 0;
842 } else if (!h->next_hole) {
843 /* last hole */
844 payload[h->prev_hole].next_hole = 0;
845 } else {
846 /* in the middle of the list */
847 payload[h->next_hole].prev_hole = h->prev_hole;
848 payload[h->prev_hole].next_hole = h->next_hole;
849 }
850
851 } else if (h->last_byte <= start + len) {
852 /* overlaps with final part of the hole: shorten this hole */
853 h->last_byte = start;
854
855 } else if (h >= thisfrag) {
856 /* overlaps with initial part of the hole: move this hole */
857 newh = thisfrag + (len / 8);
858 *newh = *h;
859 h = newh;
860 if (h->next_hole)
861 payload[h->next_hole].prev_hole = (h - payload);
862 if (h->prev_hole)
863 payload[h->prev_hole].next_hole = (h - payload);
864 else
865 first_hole = (h - payload);
866
867 } else {
868 /* fragment sits in the middle: split the hole */
869 newh = thisfrag + (len / 8);
870 *newh = *h;
871 h->last_byte = start;
872 h->next_hole = (newh - payload);
873 newh->prev_hole = (h - payload);
874 if (newh->next_hole)
875 payload[newh->next_hole].prev_hole = (newh - payload);
876 }
877
878 /* finally copy this fragment and possibly return whole packet */
Joe Hershbergerc686fa12012-05-23 07:58:05 +0000879 memcpy((uchar *)thisfrag, indata + IP_HDR_SIZE, len);
Alessandro Rubinif119e3d2009-08-07 13:58:56 +0200880 if (!done)
881 return NULL;
882
883 localip->ip_len = htons(total_len);
Joe Hershbergerc686fa12012-05-23 07:58:05 +0000884 *lenp = total_len + IP_HDR_SIZE;
Alessandro Rubinif119e3d2009-08-07 13:58:56 +0200885 return localip;
886}
887
Joe Hershberger6fe8b452012-05-23 07:58:04 +0000888static inline struct ip_udp_hdr *NetDefragment(struct ip_udp_hdr *ip, int *lenp)
Alessandro Rubinif119e3d2009-08-07 13:58:56 +0200889{
890 u16 ip_off = ntohs(ip->ip_off);
891 if (!(ip_off & (IP_OFFS | IP_FLAGS_MFRAG)))
892 return ip; /* not a fragment */
893 return __NetDefragment(ip, lenp);
894}
895
896#else /* !CONFIG_IP_DEFRAG */
897
Joe Hershberger6fe8b452012-05-23 07:58:04 +0000898static inline struct ip_udp_hdr *NetDefragment(struct ip_udp_hdr *ip, int *lenp)
Alessandro Rubinif119e3d2009-08-07 13:58:56 +0200899{
900 u16 ip_off = ntohs(ip->ip_off);
901 if (!(ip_off & (IP_OFFS | IP_FLAGS_MFRAG)))
902 return ip; /* not a fragment */
903 return NULL;
904}
905#endif
wdenk2d966952002-10-31 22:12:35 +0000906
Simon Glass43c72962011-10-24 18:00:00 +0000907/**
908 * Receive an ICMP packet. We deal with REDIRECT and PING here, and silently
909 * drop others.
910 *
911 * @parma ip IP packet containing the ICMP
912 */
Joe Hershberger6fe8b452012-05-23 07:58:04 +0000913static void receive_icmp(struct ip_udp_hdr *ip, int len,
Joe Hershberger1178f412012-05-23 07:58:06 +0000914 IPaddr_t src_ip, struct ethernet_hdr *et)
Simon Glass43c72962011-10-24 18:00:00 +0000915{
Joe Hershberger78495612012-05-23 07:58:09 +0000916 struct icmp_hdr *icmph = (struct icmp_hdr *)&ip->udp_src;
Simon Glass43c72962011-10-24 18:00:00 +0000917
918 switch (icmph->type) {
919 case ICMP_REDIRECT:
920 if (icmph->code != ICMP_REDIR_HOST)
921 return;
922 printf(" ICMP Host Redirect to %pI4 ",
923 &icmph->un.gateway);
924 break;
Joe Hershbergerc21bf372012-05-23 07:58:02 +0000925 default:
Simon Glass43c72962011-10-24 18:00:00 +0000926#if defined(CONFIG_CMD_PING)
Joe Hershbergerc21bf372012-05-23 07:58:02 +0000927 ping_receive(et, ip, len);
Simon Glass43c72962011-10-24 18:00:00 +0000928#endif
Simon Glass2928cd82011-10-26 14:18:38 +0000929#ifdef CONFIG_CMD_TFTPPUT
Simon Glass230467c2011-10-24 18:00:01 +0000930 if (packet_icmp_handler)
931 packet_icmp_handler(icmph->type, icmph->code,
932 ntohs(ip->udp_dst), src_ip, ntohs(ip->udp_src),
933 icmph->un.data, ntohs(ip->udp_len));
Simon Glass2928cd82011-10-26 14:18:38 +0000934#endif
Simon Glass43c72962011-10-24 18:00:00 +0000935 break;
936 }
937}
938
wdenk2d966952002-10-31 22:12:35 +0000939void
Joe Hershberger4b7747e2012-05-15 08:59:04 +0000940NetReceive(uchar *inpkt, int len)
wdenk2d966952002-10-31 22:12:35 +0000941{
Joe Hershberger1178f412012-05-23 07:58:06 +0000942 struct ethernet_hdr *et;
Joe Hershberger6fe8b452012-05-23 07:58:04 +0000943 struct ip_udp_hdr *ip;
Joe Hershbergerfacf5352012-05-23 07:58:12 +0000944 IPaddr_t dst_ip;
Luca Ceresoli428ab362011-04-18 06:19:50 +0000945 IPaddr_t src_ip;
Joe Hershbergerfacf5352012-05-23 07:58:12 +0000946 int eth_proto;
Jon Loeliger54f35c22007-07-09 17:45:14 -0500947#if defined(CONFIG_CMD_CDP)
wdenk145d2c12004-04-15 21:48:45 +0000948 int iscdp;
949#endif
950 ushort cti = 0, vlanid = VLAN_NONE, myvlanid, mynvlanid;
wdenk2d966952002-10-31 22:12:35 +0000951
Joe Hershberger05a377b2012-05-23 08:01:04 +0000952 debug_cond(DEBUG_NET_PKT, "packet received\n");
wdenk145d2c12004-04-15 21:48:45 +0000953
Mike Frysingerdc166032009-07-18 21:04:08 -0400954 NetRxPacket = inpkt;
955 NetRxPacketLen = len;
Joe Hershberger1178f412012-05-23 07:58:06 +0000956 et = (struct ethernet_hdr *)inpkt;
wdenk145d2c12004-04-15 21:48:45 +0000957
958 /* too small packet? */
959 if (len < ETHER_HDR_SIZE)
960 return;
Rafal Jaworowski1f2c9a42007-12-27 18:19:02 +0100961
962#ifdef CONFIG_API
963 if (push_packet) {
964 (*push_packet)(inpkt, len);
965 return;
966 }
967#endif
wdenk145d2c12004-04-15 21:48:45 +0000968
Jon Loeliger54f35c22007-07-09 17:45:14 -0500969#if defined(CONFIG_CMD_CDP)
wdenk145d2c12004-04-15 21:48:45 +0000970 /* keep track if packet is CDP */
Joe Hershberger00c62a82012-05-23 07:58:00 +0000971 iscdp = is_cdp_packet(et->et_dest);
wdenk145d2c12004-04-15 21:48:45 +0000972#endif
973
974 myvlanid = ntohs(NetOurVLAN);
975 if (myvlanid == (ushort)-1)
976 myvlanid = VLAN_NONE;
977 mynvlanid = ntohs(NetOurNativeVLAN);
978 if (mynvlanid == (ushort)-1)
979 mynvlanid = VLAN_NONE;
wdenk2d966952002-10-31 22:12:35 +0000980
Joe Hershbergerfacf5352012-05-23 07:58:12 +0000981 eth_proto = ntohs(et->et_protlen);
wdenk2d966952002-10-31 22:12:35 +0000982
Joe Hershbergerfacf5352012-05-23 07:58:12 +0000983 if (eth_proto < 1514) {
Joe Hershberger1178f412012-05-23 07:58:06 +0000984 struct e802_hdr *et802 = (struct e802_hdr *)et;
wdenk2d966952002-10-31 22:12:35 +0000985 /*
Joe Hershbergerc17fa982012-05-23 07:58:11 +0000986 * Got a 802.2 packet. Check the other protocol field.
987 * XXX VLAN over 802.2+SNAP not implemented!
wdenk2d966952002-10-31 22:12:35 +0000988 */
Joe Hershbergerfacf5352012-05-23 07:58:12 +0000989 eth_proto = ntohs(et802->et_prot);
wdenk145d2c12004-04-15 21:48:45 +0000990
Joe Hershberger6fe8b452012-05-23 07:58:04 +0000991 ip = (struct ip_udp_hdr *)(inpkt + E802_HDR_SIZE);
wdenk2d966952002-10-31 22:12:35 +0000992 len -= E802_HDR_SIZE;
wdenk145d2c12004-04-15 21:48:45 +0000993
Joe Hershbergerfacf5352012-05-23 07:58:12 +0000994 } else if (eth_proto != PROT_VLAN) { /* normal packet */
Joe Hershberger6fe8b452012-05-23 07:58:04 +0000995 ip = (struct ip_udp_hdr *)(inpkt + ETHER_HDR_SIZE);
wdenk2d966952002-10-31 22:12:35 +0000996 len -= ETHER_HDR_SIZE;
wdenk145d2c12004-04-15 21:48:45 +0000997
998 } else { /* VLAN packet */
Joe Hershbergerb43784c2012-05-23 07:58:07 +0000999 struct vlan_ethernet_hdr *vet =
1000 (struct vlan_ethernet_hdr *)et;
wdenk145d2c12004-04-15 21:48:45 +00001001
Joe Hershberger05a377b2012-05-23 08:01:04 +00001002 debug_cond(DEBUG_NET_PKT, "VLAN packet received\n");
Robin Getz9e0a4d62009-07-23 03:01:03 -04001003
wdenk145d2c12004-04-15 21:48:45 +00001004 /* too small packet? */
1005 if (len < VLAN_ETHER_HDR_SIZE)
1006 return;
1007
1008 /* if no VLAN active */
1009 if ((ntohs(NetOurVLAN) & VLAN_IDMASK) == VLAN_NONE
Jon Loeliger54f35c22007-07-09 17:45:14 -05001010#if defined(CONFIG_CMD_CDP)
wdenk145d2c12004-04-15 21:48:45 +00001011 && iscdp == 0
1012#endif
1013 )
1014 return;
1015
1016 cti = ntohs(vet->vet_tag);
1017 vlanid = cti & VLAN_IDMASK;
Joe Hershbergerfacf5352012-05-23 07:58:12 +00001018 eth_proto = ntohs(vet->vet_type);
wdenk145d2c12004-04-15 21:48:45 +00001019
Joe Hershberger6fe8b452012-05-23 07:58:04 +00001020 ip = (struct ip_udp_hdr *)(inpkt + VLAN_ETHER_HDR_SIZE);
wdenk145d2c12004-04-15 21:48:45 +00001021 len -= VLAN_ETHER_HDR_SIZE;
wdenk2d966952002-10-31 22:12:35 +00001022 }
1023
Joe Hershberger05a377b2012-05-23 08:01:04 +00001024 debug_cond(DEBUG_NET_PKT, "Receive from protocol 0x%x\n", eth_proto);
wdenk2d966952002-10-31 22:12:35 +00001025
Jon Loeliger54f35c22007-07-09 17:45:14 -05001026#if defined(CONFIG_CMD_CDP)
wdenk145d2c12004-04-15 21:48:45 +00001027 if (iscdp) {
Joe Hershbergerd01a7a02012-05-23 07:58:13 +00001028 cdp_receive((uchar *)ip, len);
wdenk145d2c12004-04-15 21:48:45 +00001029 return;
1030 }
1031#endif
1032
1033 if ((myvlanid & VLAN_IDMASK) != VLAN_NONE) {
1034 if (vlanid == VLAN_NONE)
1035 vlanid = (mynvlanid & VLAN_IDMASK);
1036 /* not matched? */
1037 if (vlanid != (myvlanid & VLAN_IDMASK))
1038 return;
1039 }
1040
Joe Hershbergerfacf5352012-05-23 07:58:12 +00001041 switch (eth_proto) {
wdenk2d966952002-10-31 22:12:35 +00001042
1043 case PROT_ARP:
Joe Hershbergeraae05082012-05-23 07:58:01 +00001044 ArpReceive(et, ip, len);
wdenkcb99da52005-01-12 00:15:14 +00001045 break;
wdenk2d966952002-10-31 22:12:35 +00001046
Peter Tyserf7a48ca2010-09-30 11:25:48 -05001047#ifdef CONFIG_CMD_RARP
wdenk2d966952002-10-31 22:12:35 +00001048 case PROT_RARP:
Joe Hershberger61b4de62012-05-23 07:58:03 +00001049 rarp_receive(ip, len);
wdenk2d966952002-10-31 22:12:35 +00001050 break;
Peter Tyserf7a48ca2010-09-30 11:25:48 -05001051#endif
wdenk2d966952002-10-31 22:12:35 +00001052 case PROT_IP:
Joe Hershberger05a377b2012-05-23 08:01:04 +00001053 debug_cond(DEBUG_NET_PKT, "Got IP\n");
Alessandro Rubinif119e3d2009-08-07 13:58:56 +02001054 /* Before we start poking the header, make sure it is there */
Joe Hershberger6fe8b452012-05-23 07:58:04 +00001055 if (len < IP_UDP_HDR_SIZE) {
1056 debug("len bad %d < %lu\n", len,
1057 (ulong)IP_UDP_HDR_SIZE);
wdenk2d966952002-10-31 22:12:35 +00001058 return;
1059 }
Alessandro Rubinif119e3d2009-08-07 13:58:56 +02001060 /* Check the packet length */
wdenk2d966952002-10-31 22:12:35 +00001061 if (len < ntohs(ip->ip_len)) {
Joe Hershberger05a377b2012-05-23 08:01:04 +00001062 debug("len bad %d < %d\n", len, ntohs(ip->ip_len));
wdenk2d966952002-10-31 22:12:35 +00001063 return;
1064 }
1065 len = ntohs(ip->ip_len);
Joe Hershberger05a377b2012-05-23 08:01:04 +00001066 debug_cond(DEBUG_NET_PKT, "len=%d, v=%02x\n",
1067 len, ip->ip_hl_v & 0xff);
Robin Getz9e0a4d62009-07-23 03:01:03 -04001068
Alessandro Rubinif119e3d2009-08-07 13:58:56 +02001069 /* Can't deal with anything except IPv4 */
Luca Ceresolie8ccbb02011-05-04 02:40:43 +00001070 if ((ip->ip_hl_v & 0xf0) != 0x40)
wdenk2d966952002-10-31 22:12:35 +00001071 return;
Alessandro Rubinif119e3d2009-08-07 13:58:56 +02001072 /* Can't deal with IP options (headers != 20 bytes) */
Luca Ceresolie8ccbb02011-05-04 02:40:43 +00001073 if ((ip->ip_hl_v & 0x0f) > 0x05)
Remy Bohmerb9535782008-06-03 15:48:17 +02001074 return;
Alessandro Rubinif119e3d2009-08-07 13:58:56 +02001075 /* Check the Checksum of the header */
Joe Hershbergerc686fa12012-05-23 07:58:05 +00001076 if (!NetCksumOk((uchar *)ip, IP_HDR_SIZE / 2)) {
Joe Hershberger05a377b2012-05-23 08:01:04 +00001077 debug("checksum bad\n");
wdenk2d966952002-10-31 22:12:35 +00001078 return;
1079 }
Alessandro Rubinif119e3d2009-08-07 13:58:56 +02001080 /* If it is not for us, ignore it */
Joe Hershbergerfacf5352012-05-23 07:58:12 +00001081 dst_ip = NetReadIP(&ip->ip_dst);
1082 if (NetOurIP && dst_ip != NetOurIP && dst_ip != 0xFFFFFFFF) {
David Updegraff7280da72007-06-11 10:41:07 -05001083#ifdef CONFIG_MCAST_TFTP
Joe Hershbergerfacf5352012-05-23 07:58:12 +00001084 if (Mcast_addr != dst_ip)
David Updegraff7280da72007-06-11 10:41:07 -05001085#endif
Luca Ceresolib19d0b72011-05-04 02:40:46 +00001086 return;
wdenk2d966952002-10-31 22:12:35 +00001087 }
Luca Ceresoli428ab362011-04-18 06:19:50 +00001088 /* Read source IP address for later use */
1089 src_ip = NetReadIP(&ip->ip_src);
wdenk2d966952002-10-31 22:12:35 +00001090 /*
Alessandro Rubinif119e3d2009-08-07 13:58:56 +02001091 * The function returns the unchanged packet if it's not
1092 * a fragment, and either the complete packet or NULL if
1093 * it is a fragment (if !CONFIG_IP_DEFRAG, it returns NULL)
1094 */
Luca Ceresolidb210822011-05-04 02:40:47 +00001095 ip = NetDefragment(ip, &len);
1096 if (!ip)
Alessandro Rubinif119e3d2009-08-07 13:58:56 +02001097 return;
1098 /*
wdenk2d966952002-10-31 22:12:35 +00001099 * watch for ICMP host redirects
1100 *
wdenk57b2d802003-06-27 21:31:46 +00001101 * There is no real handler code (yet). We just watch
1102 * for ICMP host redirect messages. In case anybody
1103 * sees these messages: please contact me
1104 * (wd@denx.de), or - even better - send me the
1105 * necessary fixes :-)
wdenk2d966952002-10-31 22:12:35 +00001106 *
wdenk57b2d802003-06-27 21:31:46 +00001107 * Note: in all cases where I have seen this so far
1108 * it was a problem with the router configuration,
1109 * for instance when a router was configured in the
1110 * BOOTP reply, but the TFTP server was on the same
1111 * subnet. So this is probably a warning that your
1112 * configuration might be wrong. But I'm not really
1113 * sure if there aren't any other situations.
Simon Glass230467c2011-10-24 18:00:01 +00001114 *
1115 * Simon Glass <sjg@chromium.org>: We get an ICMP when
1116 * we send a tftp packet to a dead connection, or when
1117 * there is no server at the other end.
wdenk2d966952002-10-31 22:12:35 +00001118 */
1119 if (ip->ip_p == IPPROTO_ICMP) {
Simon Glass43c72962011-10-24 18:00:00 +00001120 receive_icmp(ip, len, src_ip, et);
1121 return;
wdenk2d966952002-10-31 22:12:35 +00001122 } else if (ip->ip_p != IPPROTO_UDP) { /* Only UDP packets */
1123 return;
1124 }
1125
Joe Hershberger05a377b2012-05-23 08:01:04 +00001126 debug_cond(DEBUG_DEV_PKT,
1127 "received UDP (to=%pI4, from=%pI4, len=%d)\n",
1128 &dst_ip, &src_ip, len);
1129
Stefan Roesedfced812005-08-12 20:06:52 +02001130#ifdef CONFIG_UDP_CHECKSUM
1131 if (ip->udp_xsum != 0) {
Wolfgang Denk30b87322005-08-12 23:43:12 +02001132 ulong xsum;
Stefan Roesedfced812005-08-12 20:06:52 +02001133 ushort *sumptr;
1134 ushort sumlen;
1135
1136 xsum = ip->ip_p;
1137 xsum += (ntohs(ip->udp_len));
1138 xsum += (ntohl(ip->ip_src) >> 16) & 0x0000ffff;
1139 xsum += (ntohl(ip->ip_src) >> 0) & 0x0000ffff;
1140 xsum += (ntohl(ip->ip_dst) >> 16) & 0x0000ffff;
1141 xsum += (ntohl(ip->ip_dst) >> 0) & 0x0000ffff;
1142
1143 sumlen = ntohs(ip->udp_len);
1144 sumptr = (ushort *) &(ip->udp_src);
1145
1146 while (sumlen > 1) {
Wolfgang Denk30b87322005-08-12 23:43:12 +02001147 ushort sumdata;
Stefan Roesedfced812005-08-12 20:06:52 +02001148
1149 sumdata = *sumptr++;
1150 xsum += ntohs(sumdata);
1151 sumlen -= 2;
1152 }
1153 if (sumlen > 0) {
Wolfgang Denk30b87322005-08-12 23:43:12 +02001154 ushort sumdata;
Stefan Roesedfced812005-08-12 20:06:52 +02001155
1156 sumdata = *(unsigned char *) sumptr;
Wolfgang Denk30b87322005-08-12 23:43:12 +02001157 sumdata = (sumdata << 8) & 0xff00;
Stefan Roesedfced812005-08-12 20:06:52 +02001158 xsum += sumdata;
1159 }
1160 while ((xsum >> 16) != 0) {
Luca Ceresoli5c83a962011-05-11 03:59:54 +00001161 xsum = (xsum & 0x0000ffff) +
1162 ((xsum >> 16) & 0x0000ffff);
Stefan Roesedfced812005-08-12 20:06:52 +02001163 }
1164 if ((xsum != 0x00000000) && (xsum != 0x0000ffff)) {
Wolfgang Denk12cec0a2008-07-11 01:16:00 +02001165 printf(" UDP wrong checksum %08lx %08x\n",
1166 xsum, ntohs(ip->udp_xsum));
Stefan Roesedfced812005-08-12 20:06:52 +02001167 return;
1168 }
1169 }
1170#endif
1171
David Updegraff7280da72007-06-11 10:41:07 -05001172
wdenkb8fb6192004-08-02 21:11:11 +00001173#ifdef CONFIG_NETCONSOLE
Joe Hershberger6fe8b452012-05-23 07:58:04 +00001174 nc_input_packet((uchar *)ip + IP_UDP_HDR_SIZE,
Joe Hershberger19d6bc02012-10-03 09:14:03 +00001175 src_ip,
Joe Hershberger6fe8b452012-05-23 07:58:04 +00001176 ntohs(ip->udp_dst),
1177 ntohs(ip->udp_src),
1178 ntohs(ip->udp_len) - UDP_HDR_SIZE);
wdenkb8fb6192004-08-02 21:11:11 +00001179#endif
wdenk2d966952002-10-31 22:12:35 +00001180 /*
1181 * IP header OK. Pass the packet to the current handler.
1182 */
Joe Hershbergerf50357b2012-05-23 07:59:15 +00001183 (*udp_packet_handler)((uchar *)ip + IP_UDP_HDR_SIZE,
1184 ntohs(ip->udp_dst),
1185 src_ip,
1186 ntohs(ip->udp_src),
1187 ntohs(ip->udp_len) - UDP_HDR_SIZE);
wdenk2d966952002-10-31 22:12:35 +00001188 break;
1189 }
1190}
1191
1192
1193/**********************************************************************/
1194
Simon Glassd6c5f552011-10-24 18:00:02 +00001195static int net_check_prereq(enum proto_t protocol)
wdenk2d966952002-10-31 22:12:35 +00001196{
1197 switch (protocol) {
wdenk05939202004-04-18 17:39:38 +00001198 /* Fall through */
Jon Loeliger54f35c22007-07-09 17:45:14 -05001199#if defined(CONFIG_CMD_PING)
wdenke6466f62003-06-05 19:27:42 +00001200 case PING:
wdenk05939202004-04-18 17:39:38 +00001201 if (NetPingIP == 0) {
Luca Ceresoli7cbd7482011-05-11 03:59:56 +00001202 puts("*** ERROR: ping address not given\n");
Luca Ceresoli5fe6de22011-05-04 02:40:45 +00001203 return 1;
wdenk05939202004-04-18 17:39:38 +00001204 }
1205 goto common;
wdenke6466f62003-06-05 19:27:42 +00001206#endif
Jon Loeliger54f35c22007-07-09 17:45:14 -05001207#if defined(CONFIG_CMD_SNTP)
wdenkb4ad9622005-04-01 00:25:43 +00001208 case SNTP:
1209 if (NetNtpServerIP == 0) {
Luca Ceresoli7cbd7482011-05-11 03:59:56 +00001210 puts("*** ERROR: NTP server address not given\n");
Luca Ceresoli5fe6de22011-05-04 02:40:45 +00001211 return 1;
wdenkb4ad9622005-04-01 00:25:43 +00001212 }
1213 goto common;
1214#endif
Robin Getz82f0d232009-07-20 14:53:54 -04001215#if defined(CONFIG_CMD_DNS)
1216 case DNS:
1217 if (NetOurDNSIP == 0) {
1218 puts("*** ERROR: DNS server address not given\n");
1219 return 1;
1220 }
1221 goto common;
1222#endif
Jon Loeliger54f35c22007-07-09 17:45:14 -05001223#if defined(CONFIG_CMD_NFS)
wdenkbe9c1cb2004-02-24 02:00:03 +00001224 case NFS:
1225#endif
Simon Glassd6c5f552011-10-24 18:00:02 +00001226 case TFTPGET:
Simon Glass6a398d22011-10-24 18:00:07 +00001227 case TFTPPUT:
wdenk05939202004-04-18 17:39:38 +00001228 if (NetServerIP == 0) {
Luca Ceresoli7cbd7482011-05-11 03:59:56 +00001229 puts("*** ERROR: `serverip' not set\n");
Luca Ceresoli5fe6de22011-05-04 02:40:45 +00001230 return 1;
wdenk05939202004-04-18 17:39:38 +00001231 }
Luca Ceresoli7cbd7482011-05-11 03:59:56 +00001232#if defined(CONFIG_CMD_PING) || defined(CONFIG_CMD_SNTP) || \
1233 defined(CONFIG_CMD_DNS)
1234common:
wdenke6466f62003-06-05 19:27:42 +00001235#endif
Simon Guinot00dceba2011-05-01 23:38:40 +00001236 /* Fall through */
wdenke6466f62003-06-05 19:27:42 +00001237
Simon Guinot00dceba2011-05-01 23:38:40 +00001238 case NETCONS:
Luca Ceresoli7aa81a42011-05-17 00:03:40 +00001239 case TFTPSRV:
wdenk05939202004-04-18 17:39:38 +00001240 if (NetOurIP == 0) {
Luca Ceresoli7cbd7482011-05-11 03:59:56 +00001241 puts("*** ERROR: `ipaddr' not set\n");
Luca Ceresoli5fe6de22011-05-04 02:40:45 +00001242 return 1;
wdenk05939202004-04-18 17:39:38 +00001243 }
1244 /* Fall through */
wdenk2d966952002-10-31 22:12:35 +00001245
Peter Tyserf7a48ca2010-09-30 11:25:48 -05001246#ifdef CONFIG_CMD_RARP
wdenk2d966952002-10-31 22:12:35 +00001247 case RARP:
Peter Tyserf7a48ca2010-09-30 11:25:48 -05001248#endif
wdenk2d966952002-10-31 22:12:35 +00001249 case BOOTP:
wdenk145d2c12004-04-15 21:48:45 +00001250 case CDP:
Peter Tyserf7a48ca2010-09-30 11:25:48 -05001251 case DHCP:
Joe Hershbergerb35a3a62012-05-23 08:00:12 +00001252 case LINKLOCAL:
Luca Ceresoli7cbd7482011-05-11 03:59:56 +00001253 if (memcmp(NetOurEther, "\0\0\0\0\0\0", 6) == 0) {
Luca Ceresoli7cbd7482011-05-11 03:59:56 +00001254 int num = eth_get_dev_index();
wdenk2d966952002-10-31 22:12:35 +00001255
wdenk05939202004-04-18 17:39:38 +00001256 switch (num) {
1257 case -1:
Luca Ceresoli7cbd7482011-05-11 03:59:56 +00001258 puts("*** ERROR: No ethernet found.\n");
Luca Ceresoli5fe6de22011-05-04 02:40:45 +00001259 return 1;
wdenk05939202004-04-18 17:39:38 +00001260 case 0:
Luca Ceresoli7cbd7482011-05-11 03:59:56 +00001261 puts("*** ERROR: `ethaddr' not set\n");
wdenk2d966952002-10-31 22:12:35 +00001262 break;
wdenk05939202004-04-18 17:39:38 +00001263 default:
Luca Ceresoli7cbd7482011-05-11 03:59:56 +00001264 printf("*** ERROR: `eth%daddr' not set\n",
wdenk2d966952002-10-31 22:12:35 +00001265 num);
1266 break;
wdenk05939202004-04-18 17:39:38 +00001267 }
wdenk2d966952002-10-31 22:12:35 +00001268
Luca Ceresoli7cbd7482011-05-11 03:59:56 +00001269 NetStartAgain();
Luca Ceresoli5fe6de22011-05-04 02:40:45 +00001270 return 2;
wdenk05939202004-04-18 17:39:38 +00001271 }
1272 /* Fall through */
1273 default:
Luca Ceresoli5fe6de22011-05-04 02:40:45 +00001274 return 0;
wdenk2d966952002-10-31 22:12:35 +00001275 }
Luca Ceresoli5fe6de22011-05-04 02:40:45 +00001276 return 0; /* OK */
wdenk2d966952002-10-31 22:12:35 +00001277}
1278/**********************************************************************/
1279
1280int
Luca Ceresolif27d91f2011-05-04 02:40:44 +00001281NetCksumOk(uchar *ptr, int len)
wdenk2d966952002-10-31 22:12:35 +00001282{
1283 return !((NetCksum(ptr, len) + 1) & 0xfffe);
1284}
1285
1286
1287unsigned
Luca Ceresolif27d91f2011-05-04 02:40:44 +00001288NetCksum(uchar *ptr, int len)
wdenk2d966952002-10-31 22:12:35 +00001289{
1290 ulong xsum;
Stefan Roeseac553282005-08-31 12:55:50 +02001291 ushort *p = (ushort *)ptr;
wdenk2d966952002-10-31 22:12:35 +00001292
1293 xsum = 0;
1294 while (len-- > 0)
Wolfgang Denk3135c542005-08-26 01:36:03 +02001295 xsum += *p++;
wdenk2d966952002-10-31 22:12:35 +00001296 xsum = (xsum & 0xffff) + (xsum >> 16);
1297 xsum = (xsum & 0xffff) + (xsum >> 16);
Luca Ceresoli5fe6de22011-05-04 02:40:45 +00001298 return xsum & 0xffff;
wdenk2d966952002-10-31 22:12:35 +00001299}
1300
wdenk145d2c12004-04-15 21:48:45 +00001301int
1302NetEthHdrSize(void)
1303{
1304 ushort myvlanid;
1305
1306 myvlanid = ntohs(NetOurVLAN);
1307 if (myvlanid == (ushort)-1)
1308 myvlanid = VLAN_NONE;
wdenk2d966952002-10-31 22:12:35 +00001309
Luca Ceresoli5c83a962011-05-11 03:59:54 +00001310 return ((myvlanid & VLAN_IDMASK) == VLAN_NONE) ? ETHER_HDR_SIZE :
1311 VLAN_ETHER_HDR_SIZE;
wdenk145d2c12004-04-15 21:48:45 +00001312}
1313
1314int
Joe Hershberger4b7747e2012-05-15 08:59:04 +00001315NetSetEther(uchar *xet, uchar * addr, uint prot)
wdenk2d966952002-10-31 22:12:35 +00001316{
Joe Hershberger1178f412012-05-23 07:58:06 +00001317 struct ethernet_hdr *et = (struct ethernet_hdr *)xet;
wdenk145d2c12004-04-15 21:48:45 +00001318 ushort myvlanid;
1319
1320 myvlanid = ntohs(NetOurVLAN);
1321 if (myvlanid == (ushort)-1)
1322 myvlanid = VLAN_NONE;
wdenk2d966952002-10-31 22:12:35 +00001323
Luca Ceresoli7cbd7482011-05-11 03:59:56 +00001324 memcpy(et->et_dest, addr, 6);
1325 memcpy(et->et_src, NetOurEther, 6);
wdenk145d2c12004-04-15 21:48:45 +00001326 if ((myvlanid & VLAN_IDMASK) == VLAN_NONE) {
Luca Ceresolib19d0b72011-05-04 02:40:46 +00001327 et->et_protlen = htons(prot);
wdenk145d2c12004-04-15 21:48:45 +00001328 return ETHER_HDR_SIZE;
1329 } else {
Joe Hershbergerb43784c2012-05-23 07:58:07 +00001330 struct vlan_ethernet_hdr *vet =
1331 (struct vlan_ethernet_hdr *)xet;
wdenk2d966952002-10-31 22:12:35 +00001332
wdenk145d2c12004-04-15 21:48:45 +00001333 vet->vet_vlan_type = htons(PROT_VLAN);
1334 vet->vet_tag = htons((0 << 5) | (myvlanid & VLAN_IDMASK));
1335 vet->vet_type = htons(prot);
1336 return VLAN_ETHER_HDR_SIZE;
1337 }
1338}
wdenk2d966952002-10-31 22:12:35 +00001339
Joe Hershberger530cd6b2012-05-23 07:59:16 +00001340int net_update_ether(struct ethernet_hdr *et, uchar *addr, uint prot)
1341{
1342 ushort protlen;
1343
1344 memcpy(et->et_dest, addr, 6);
1345 memcpy(et->et_src, NetOurEther, 6);
1346 protlen = ntohs(et->et_protlen);
1347 if (protlen == PROT_VLAN) {
1348 struct vlan_ethernet_hdr *vet =
1349 (struct vlan_ethernet_hdr *)et;
1350 vet->vet_type = htons(prot);
1351 return VLAN_ETHER_HDR_SIZE;
1352 } else if (protlen > 1514) {
1353 et->et_protlen = htons(prot);
1354 return ETHER_HDR_SIZE;
1355 } else {
1356 /* 802.2 + SNAP */
1357 struct e802_hdr *et802 = (struct e802_hdr *)et;
1358 et802->et_prot = htons(prot);
1359 return E802_HDR_SIZE;
1360 }
1361}
1362
Joe Hershberger2ed5b492012-05-23 07:59:07 +00001363void net_set_ip_header(uchar *pkt, IPaddr_t dest, IPaddr_t source)
wdenk2d966952002-10-31 22:12:35 +00001364{
Joe Hershberger2ed5b492012-05-23 07:59:07 +00001365 struct ip_udp_hdr *ip = (struct ip_udp_hdr *)pkt;
wdenk2d966952002-10-31 22:12:35 +00001366
1367 /*
Joe Hershberger2ed5b492012-05-23 07:59:07 +00001368 * Construct an IP header.
wdenk2d966952002-10-31 22:12:35 +00001369 */
Luca Ceresoli5c83a962011-05-11 03:59:54 +00001370 /* IP_HDR_SIZE / 4 (not including UDP) */
1371 ip->ip_hl_v = 0x45;
wdenk2d966952002-10-31 22:12:35 +00001372 ip->ip_tos = 0;
Joe Hershberger2ed5b492012-05-23 07:59:07 +00001373 ip->ip_len = htons(IP_HDR_SIZE);
wdenk2d966952002-10-31 22:12:35 +00001374 ip->ip_id = htons(NetIPID++);
Peter Tyser0885bf02008-12-01 16:26:20 -06001375 ip->ip_off = htons(IP_FLAGS_DFRAG); /* Don't fragment */
wdenk2d966952002-10-31 22:12:35 +00001376 ip->ip_ttl = 255;
wdenk2d966952002-10-31 22:12:35 +00001377 ip->ip_sum = 0;
Luca Ceresoli5c83a962011-05-11 03:59:54 +00001378 /* already in network byte order */
Joe Hershberger2ed5b492012-05-23 07:59:07 +00001379 NetCopyIP((void *)&ip->ip_src, &source);
1380 /* already in network byte order */
Luca Ceresolif27d91f2011-05-04 02:40:44 +00001381 NetCopyIP((void *)&ip->ip_dst, &dest);
Joe Hershberger2ed5b492012-05-23 07:59:07 +00001382}
1383
1384void net_set_udp_header(uchar *pkt, IPaddr_t dest, int dport, int sport,
1385 int len)
1386{
1387 struct ip_udp_hdr *ip = (struct ip_udp_hdr *)pkt;
1388
1389 /*
1390 * If the data is an odd number of bytes, zero the
1391 * byte after the last byte so that the checksum
1392 * will work.
1393 */
1394 if (len & 1)
1395 pkt[IP_UDP_HDR_SIZE + len] = 0;
1396
1397 net_set_ip_header(pkt, dest, NetOurIP);
1398 ip->ip_len = htons(IP_UDP_HDR_SIZE + len);
1399 ip->ip_p = IPPROTO_UDP;
1400 ip->ip_sum = ~NetCksum((uchar *)ip, IP_HDR_SIZE >> 1);
1401
wdenk2d966952002-10-31 22:12:35 +00001402 ip->udp_src = htons(sport);
1403 ip->udp_dst = htons(dport);
Joe Hershberger6fe8b452012-05-23 07:58:04 +00001404 ip->udp_len = htons(UDP_HDR_SIZE + len);
wdenk2d966952002-10-31 22:12:35 +00001405 ip->udp_xsum = 0;
wdenk2d966952002-10-31 22:12:35 +00001406}
1407
Luca Ceresoli7cbd7482011-05-11 03:59:56 +00001408void copy_filename(char *dst, const char *src, int size)
wdenk2d966952002-10-31 22:12:35 +00001409{
1410 if (*src && (*src == '"')) {
1411 ++src;
1412 --size;
1413 }
1414
Luca Ceresolie8ccbb02011-05-04 02:40:43 +00001415 while ((--size > 0) && *src && (*src != '"'))
wdenk2d966952002-10-31 22:12:35 +00001416 *dst++ = *src++;
wdenk2d966952002-10-31 22:12:35 +00001417 *dst = '\0';
1418}
1419
Luca Ceresoli5c83a962011-05-11 03:59:54 +00001420#if defined(CONFIG_CMD_NFS) || \
1421 defined(CONFIG_CMD_SNTP) || \
1422 defined(CONFIG_CMD_DNS)
Robin Getz82f0d232009-07-20 14:53:54 -04001423/*
Robin Getz55b50e92010-03-08 14:07:00 -05001424 * make port a little random (1024-17407)
1425 * This keeps the math somewhat trivial to compute, and seems to work with
1426 * all supported protocols/clients/servers
Robin Getz82f0d232009-07-20 14:53:54 -04001427 */
1428unsigned int random_port(void)
1429{
Robin Getz55b50e92010-03-08 14:07:00 -05001430 return 1024 + (get_timer(0) % 0x4000);
Robin Getz82f0d232009-07-20 14:53:54 -04001431}
1432#endif
1433
Luca Ceresoli7cbd7482011-05-11 03:59:56 +00001434void ip_to_string(IPaddr_t x, char *s)
wdenk2d966952002-10-31 22:12:35 +00001435{
Luca Ceresoli7cbd7482011-05-11 03:59:56 +00001436 x = ntohl(x);
1437 sprintf(s, "%d.%d.%d.%d",
1438 (int) ((x >> 24) & 0xff),
1439 (int) ((x >> 16) & 0xff),
1440 (int) ((x >> 8) & 0xff), (int) ((x >> 0) & 0xff)
wdenk05939202004-04-18 17:39:38 +00001441 );
wdenk2d966952002-10-31 22:12:35 +00001442}
1443
wdenk145d2c12004-04-15 21:48:45 +00001444void VLAN_to_string(ushort x, char *s)
1445{
1446 x = ntohs(x);
1447
1448 if (x == (ushort)-1)
1449 x = VLAN_NONE;
1450
1451 if (x == VLAN_NONE)
1452 strcpy(s, "none");
1453 else
1454 sprintf(s, "%d", x & VLAN_IDMASK);
1455}
1456
Mike Frysinger89c73922010-10-20 07:16:48 -04001457ushort string_to_VLAN(const char *s)
wdenk145d2c12004-04-15 21:48:45 +00001458{
1459 ushort id;
1460
1461 if (s == NULL)
wdenk656140b2004-04-25 13:18:40 +00001462 return htons(VLAN_NONE);
wdenk145d2c12004-04-15 21:48:45 +00001463
1464 if (*s < '0' || *s > '9')
1465 id = VLAN_NONE;
1466 else
1467 id = (ushort)simple_strtoul(s, NULL, 10);
1468
wdenk656140b2004-04-25 13:18:40 +00001469 return htons(id);
wdenk145d2c12004-04-15 21:48:45 +00001470}
1471
wdenk145d2c12004-04-15 21:48:45 +00001472ushort getenv_VLAN(char *var)
1473{
Luca Ceresoli5fe6de22011-05-04 02:40:45 +00001474 return string_to_VLAN(getenv(var));
wdenk145d2c12004-04-15 21:48:45 +00001475}