blob: 9de7d92643e04163ee504a75a63f01ec8b2e666e [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>
85#include <net.h>
Joe Hershbergerfee076e2012-05-23 07:58:15 +000086#if defined(CONFIG_STATUS_LED)
wdenk49c3f672003-10-08 22:33:00 +000087#include <miiphy.h>
Joe Hershbergerfee076e2012-05-23 07:58:15 +000088#include <status_led.h>
wdenkb4ad9622005-04-01 00:25:43 +000089#endif
Joe Hershbergerfee076e2012-05-23 07:58:15 +000090#include <watchdog.h>
91#include <linux/compiler.h>
92#include "arp.h"
93#include "bootp.h"
Joe Hershbergera4215b02012-05-23 07:57:59 +000094#include "cdp.h"
Robin Getz82f0d232009-07-20 14:53:54 -040095#if defined(CONFIG_CMD_DNS)
96#include "dns.h"
97#endif
Joe Hershbergerb35a3a62012-05-23 08:00:12 +000098#include "link_local.h"
Joe Hershbergerfee076e2012-05-23 07:58:15 +000099#include "nfs.h"
Joe Hershbergerc21bf372012-05-23 07:58:02 +0000100#include "ping.h"
Joe Hershbergerfee076e2012-05-23 07:58:15 +0000101#include "rarp.h"
102#if defined(CONFIG_CMD_SNTP)
103#include "sntp.h"
104#endif
105#include "tftp.h"
wdenk2d966952002-10-31 22:12:35 +0000106
Wolfgang Denk6405a152006-03-31 18:32:53 +0200107DECLARE_GLOBAL_DATA_PTR;
108
wdenk2d966952002-10-31 22:12:35 +0000109/** BOOTP EXTENTIONS **/
110
Luca Ceresoli5c83a962011-05-11 03:59:54 +0000111/* Our subnet mask (0=unknown) */
Luca Ceresolid01bc1c2011-05-11 03:59:55 +0000112IPaddr_t NetOurSubnetMask;
Luca Ceresoli5c83a962011-05-11 03:59:54 +0000113/* Our gateways IP address */
Luca Ceresolid01bc1c2011-05-11 03:59:55 +0000114IPaddr_t NetOurGatewayIP;
Luca Ceresoli5c83a962011-05-11 03:59:54 +0000115/* Our DNS IP address */
Luca Ceresolid01bc1c2011-05-11 03:59:55 +0000116IPaddr_t NetOurDNSIP;
Jon Loeliger5336a762007-07-09 22:08:34 -0500117#if defined(CONFIG_BOOTP_DNS2)
Luca Ceresoli5c83a962011-05-11 03:59:54 +0000118/* Our 2nd DNS IP address */
Luca Ceresolid01bc1c2011-05-11 03:59:55 +0000119IPaddr_t NetOurDNS2IP;
stroesee0aadfb2003-08-28 14:17:32 +0000120#endif
Luca Ceresoli5c83a962011-05-11 03:59:54 +0000121/* Our NIS domain */
Luca Ceresolid01bc1c2011-05-11 03:59:55 +0000122char NetOurNISDomain[32] = {0,};
Luca Ceresoli5c83a962011-05-11 03:59:54 +0000123/* Our hostname */
Luca Ceresolid01bc1c2011-05-11 03:59:55 +0000124char NetOurHostName[32] = {0,};
Luca Ceresoli5c83a962011-05-11 03:59:54 +0000125/* Our bootpath */
Luca Ceresolid01bc1c2011-05-11 03:59:55 +0000126char NetOurRootPath[64] = {0,};
Luca Ceresoli5c83a962011-05-11 03:59:54 +0000127/* Our bootfile size in blocks */
Luca Ceresolid01bc1c2011-05-11 03:59:55 +0000128ushort NetBootFileSize;
wdenk2d966952002-10-31 22:12:35 +0000129
David Updegraff7280da72007-06-11 10:41:07 -0500130#ifdef CONFIG_MCAST_TFTP /* Multicast TFTP */
131IPaddr_t Mcast_addr;
132#endif
133
wdenk2d966952002-10-31 22:12:35 +0000134/** END OF BOOTP EXTENTIONS **/
135
Luca Ceresoli5c83a962011-05-11 03:59:54 +0000136/* The actual transferred size of the bootfile (in bytes) */
137ulong NetBootFileXferSize;
138/* Our ethernet address */
139uchar NetOurEther[6];
140/* Boot server enet address */
Luca Ceresolid01bc1c2011-05-11 03:59:55 +0000141uchar NetServerEther[6];
Luca Ceresoli5c83a962011-05-11 03:59:54 +0000142/* Our IP addr (0 = unknown) */
143IPaddr_t NetOurIP;
144/* Server IP addr (0 = unknown) */
145IPaddr_t NetServerIP;
146/* Current receive packet */
Joe Hershberger4b7747e2012-05-15 08:59:04 +0000147uchar *NetRxPacket;
Luca Ceresoli5c83a962011-05-11 03:59:54 +0000148/* Current rx packet length */
149int NetRxPacketLen;
150/* IP packet ID */
151unsigned NetIPID;
152/* Ethernet bcast address */
Luca Ceresolid01bc1c2011-05-11 03:59:55 +0000153uchar NetBcastAddr[6] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
154uchar NetEtherNullAddr[6];
Rafal Jaworowski1f2c9a42007-12-27 18:19:02 +0100155#ifdef CONFIG_API
Joe Hershberger4b7747e2012-05-15 08:59:04 +0000156void (*push_packet)(void *, int len) = 0;
Rafal Jaworowski1f2c9a42007-12-27 18:19:02 +0100157#endif
Luca Ceresoli5c83a962011-05-11 03:59:54 +0000158/* Network loop state */
Joe Hershbergerd4bb76a2012-05-23 07:59:14 +0000159enum net_loop_state net_state;
Luca Ceresoli5c83a962011-05-11 03:59:54 +0000160/* Tried all network devices */
Luca Ceresolid01bc1c2011-05-11 03:59:55 +0000161int NetRestartWrap;
Luca Ceresoli5c83a962011-05-11 03:59:54 +0000162/* Network loop restarted */
Luca Ceresolid01bc1c2011-05-11 03:59:55 +0000163static int NetRestarted;
Luca Ceresoli5c83a962011-05-11 03:59:54 +0000164/* At least one device configured */
Luca Ceresolid01bc1c2011-05-11 03:59:55 +0000165static int NetDevExists;
wdenk2d966952002-10-31 22:12:35 +0000166
wdenk05939202004-04-18 17:39:38 +0000167/* XXX in both little & big endian machines 0xFFFF == ntohs(-1) */
Luca Ceresoli5c83a962011-05-11 03:59:54 +0000168/* default is without VLAN */
169ushort NetOurVLAN = 0xFFFF;
170/* ditto */
171ushort NetOurNativeVLAN = 0xFFFF;
wdenk145d2c12004-04-15 21:48:45 +0000172
Luca Ceresoli5c83a962011-05-11 03:59:54 +0000173/* Boot File name */
174char BootFile[128];
wdenk2d966952002-10-31 22:12:35 +0000175
Jon Loeliger54f35c22007-07-09 17:45:14 -0500176#if defined(CONFIG_CMD_SNTP)
Luca Ceresoli5c83a962011-05-11 03:59:54 +0000177/* NTP server IP address */
178IPaddr_t NetNtpServerIP;
179/* offset time from UTC */
Luca Ceresolid01bc1c2011-05-11 03:59:55 +0000180int NetTimeOffset;
wdenkb4ad9622005-04-01 00:25:43 +0000181#endif
182
Joe Hershberger4b7747e2012-05-15 08:59:04 +0000183uchar PktBuf[(PKTBUFSRX+1) * PKTSIZE_ALIGN + PKTALIGN];
wdenk2d966952002-10-31 22:12:35 +0000184
Luca Ceresoli5c83a962011-05-11 03:59:54 +0000185/* Receive packet */
Joe Hershberger4b7747e2012-05-15 08:59:04 +0000186uchar *NetRxPackets[PKTBUFSRX];
wdenk2d966952002-10-31 22:12:35 +0000187
Joe Hershbergerf50357b2012-05-23 07:59:15 +0000188/* Current UDP RX packet handler */
189static rxhand_f *udp_packet_handler;
190/* Current ARP RX packet handler */
191static rxhand_f *arp_packet_handler;
Simon Glass2928cd82011-10-26 14:18:38 +0000192#ifdef CONFIG_CMD_TFTPPUT
Joe Hershbergerf50357b2012-05-23 07:59:15 +0000193/* Current ICMP rx handler */
194static rxhand_icmp_f *packet_icmp_handler;
Simon Glass2928cd82011-10-26 14:18:38 +0000195#endif
Luca Ceresoli5c83a962011-05-11 03:59:54 +0000196/* Current timeout handler */
197static thand_f *timeHandler;
198/* Time base value */
199static ulong timeStart;
200/* Current timeout value */
201static ulong timeDelta;
202/* THE transmit packet */
Joe Hershberger4b7747e2012-05-15 08:59:04 +0000203uchar *NetTxPacket;
wdenk2d966952002-10-31 22:12:35 +0000204
Simon Glassd6c5f552011-10-24 18:00:02 +0000205static int net_check_prereq(enum proto_t protocol);
wdenk2d966952002-10-31 22:12:35 +0000206
Remy Bohmer0ddb8e82009-10-28 22:13:39 +0100207static int NetTryCount;
208
wdenk2d966952002-10-31 22:12:35 +0000209/**********************************************************************/
wdenke6466f62003-06-05 19:27:42 +0000210
Simon Glass5234ad12011-10-27 06:24:32 +0000211/*
212 * Check if autoload is enabled. If so, use either NFS or TFTP to download
213 * the boot file.
214 */
215void net_auto_load(void)
216{
217 const char *s = getenv("autoload");
218
219 if (s != NULL) {
220 if (*s == 'n') {
221 /*
222 * Just use BOOTP/RARP to configure system;
223 * Do not use TFTP to load the bootfile.
224 */
Joe Hershbergerd4bb76a2012-05-23 07:59:14 +0000225 net_set_state(NETLOOP_SUCCESS);
Simon Glass5234ad12011-10-27 06:24:32 +0000226 return;
227 }
228#if defined(CONFIG_CMD_NFS)
229 if (strcmp(s, "NFS") == 0) {
230 /*
231 * Use NFS to load the bootfile.
232 */
233 NfsStart();
234 return;
235 }
236#endif
237 }
238 TftpStart(TFTPGET);
239}
240
Joe Hershberger202c6002012-05-23 07:59:21 +0000241static void NetInitLoop(void)
Heiko Schocher0c303fa2009-02-10 09:38:52 +0100242{
Luca Ceresolid01bc1c2011-05-11 03:59:55 +0000243 static int env_changed_id;
Luca Ceresoli7cbd7482011-05-11 03:59:56 +0000244 int env_id = get_env_id();
Heiko Schocher0c303fa2009-02-10 09:38:52 +0100245
246 /* update only when the environment has changed */
Michael Zaidmanb97bfe42009-04-04 01:43:00 +0300247 if (env_changed_id != env_id) {
Enric Balletbo i Serra7019d252011-05-31 21:01:47 +0000248 NetOurIP = getenv_IPaddr("ipaddr");
Luca Ceresoli7cbd7482011-05-11 03:59:56 +0000249 NetOurGatewayIP = getenv_IPaddr("gatewayip");
250 NetOurSubnetMask = getenv_IPaddr("netmask");
251 NetServerIP = getenv_IPaddr("serverip");
Heiko Schocher0c303fa2009-02-10 09:38:52 +0100252 NetOurNativeVLAN = getenv_VLAN("nvlan");
Michael Zaidmanb97bfe42009-04-04 01:43:00 +0300253 NetOurVLAN = getenv_VLAN("vlan");
Robin Getz82f0d232009-07-20 14:53:54 -0400254#if defined(CONFIG_CMD_DNS)
255 NetOurDNSIP = getenv_IPaddr("dnsip");
256#endif
Michael Zaidmanb97bfe42009-04-04 01:43:00 +0300257 env_changed_id = env_id;
Heiko Schocher0c303fa2009-02-10 09:38:52 +0100258 }
Michael Zaidmanb97bfe42009-04-04 01:43:00 +0300259
Heiko Schocher3b195ff2009-04-28 08:36:11 +0200260 return;
Heiko Schocher0c303fa2009-02-10 09:38:52 +0100261}
262
Joe Hershbergerf50357b2012-05-23 07:59:15 +0000263static void net_clear_handlers(void)
264{
265 net_set_udp_handler(NULL);
266 net_set_arp_handler(NULL);
267 NetSetTimeout(0, NULL);
268}
269
270static void net_cleanup_loop(void)
271{
272 net_clear_handlers();
273}
274
Joe Hershberger017e5c42012-05-23 07:59:22 +0000275void net_init(void)
276{
277 static int first_call = 1;
278
279 if (first_call) {
280 /*
281 * Setup packet buffers, aligned correctly.
282 */
283 int i;
284
285 NetTxPacket = &PktBuf[0] + (PKTALIGN - 1);
286 NetTxPacket -= (ulong)NetTxPacket % PKTALIGN;
287 for (i = 0; i < PKTBUFSRX; i++)
288 NetRxPackets[i] = NetTxPacket + (i + 1) * PKTSIZE_ALIGN;
289
290 ArpInit();
291 net_clear_handlers();
292
293 /* Only need to setup buffer pointers once. */
294 first_call = 0;
295 }
296
297 NetInitLoop();
298}
299
wdenke6466f62003-06-05 19:27:42 +0000300/**********************************************************************/
wdenk2d966952002-10-31 22:12:35 +0000301/*
302 * Main network processing loop.
303 */
304
Simon Glassd6c5f552011-10-24 18:00:02 +0000305int NetLoop(enum proto_t protocol)
wdenk2d966952002-10-31 22:12:35 +0000306{
wdenk2d966952002-10-31 22:12:35 +0000307 bd_t *bd = gd->bd;
Simon Glass230467c2011-10-24 18:00:01 +0000308 int ret = -1;
wdenk2d966952002-10-31 22:12:35 +0000309
wdenk2d966952002-10-31 22:12:35 +0000310 NetRestarted = 0;
311 NetDevExists = 0;
Remy Bohmer0ddb8e82009-10-28 22:13:39 +0100312 NetTryCount = 1;
Joe Hershberger05a377b2012-05-23 08:01:04 +0000313 debug_cond(DEBUG_INT_STATE, "--- NetLoop Entry\n");
wdenke6466f62003-06-05 19:27:42 +0000314
Simon Glass768cbf02011-12-10 11:08:06 +0000315 bootstage_mark_name(BOOTSTAGE_ID_ETH_START, "eth_start");
Joe Hershberger017e5c42012-05-23 07:59:22 +0000316 net_init();
wdenk2d966952002-10-31 22:12:35 +0000317 eth_halt();
wdenk145d2c12004-04-15 21:48:45 +0000318 eth_set_current();
wdenkfa66e932005-04-03 14:52:59 +0000319 if (eth_init(bd) < 0) {
320 eth_halt();
Luca Ceresoli5fe6de22011-05-04 02:40:45 +0000321 return -1;
wdenkfa66e932005-04-03 14:52:59 +0000322 }
wdenk2d966952002-10-31 22:12:35 +0000323
324restart:
Luca Ceresoli7cbd7482011-05-11 03:59:56 +0000325 memcpy(NetOurEther, eth_get_dev()->enetaddr, 6);
wdenk2d966952002-10-31 22:12:35 +0000326
Joe Hershbergerd4bb76a2012-05-23 07:59:14 +0000327 net_set_state(NETLOOP_CONTINUE);
wdenk2d966952002-10-31 22:12:35 +0000328
329 /*
330 * Start the ball rolling with the given start function. From
331 * here on, this code is a state machine driven by received
332 * packets and timer events.
333 */
Joe Hershberger05a377b2012-05-23 08:01:04 +0000334 debug_cond(DEBUG_INT_STATE, "--- NetLoop Init\n");
Joe Hershberger202c6002012-05-23 07:59:21 +0000335 NetInitLoop();
wdenk2d966952002-10-31 22:12:35 +0000336
Luca Ceresoli7cbd7482011-05-11 03:59:56 +0000337 switch (net_check_prereq(protocol)) {
wdenk2d966952002-10-31 22:12:35 +0000338 case 1:
339 /* network not configured */
wdenkfa66e932005-04-03 14:52:59 +0000340 eth_halt();
Luca Ceresoli5fe6de22011-05-04 02:40:45 +0000341 return -1;
wdenk2d966952002-10-31 22:12:35 +0000342
wdenk2d966952002-10-31 22:12:35 +0000343 case 2:
344 /* network device not configured */
345 break;
wdenk2d966952002-10-31 22:12:35 +0000346
347 case 0:
wdenk2d966952002-10-31 22:12:35 +0000348 NetDevExists = 1;
Simon Glassd6c5f552011-10-24 18:00:02 +0000349 NetBootFileXferSize = 0;
wdenk2d966952002-10-31 22:12:35 +0000350 switch (protocol) {
Simon Glassd6c5f552011-10-24 18:00:02 +0000351 case TFTPGET:
Simon Glass6a398d22011-10-24 18:00:07 +0000352#ifdef CONFIG_CMD_TFTPPUT
353 case TFTPPUT:
354#endif
wdenk2d966952002-10-31 22:12:35 +0000355 /* always use ARP to get server ethernet address */
Simon Glassd6c5f552011-10-24 18:00:02 +0000356 TftpStart(protocol);
wdenk2d966952002-10-31 22:12:35 +0000357 break;
Luca Ceresoli7aa81a42011-05-17 00:03:40 +0000358#ifdef CONFIG_CMD_TFTPSRV
359 case TFTPSRV:
360 TftpStartServer();
361 break;
362#endif
Jon Loeliger54f35c22007-07-09 17:45:14 -0500363#if defined(CONFIG_CMD_DHCP)
wdenk2d966952002-10-31 22:12:35 +0000364 case DHCP:
wdenkc3919532004-10-11 22:51:13 +0000365 BootpTry = 0;
Michael Zaidman16bb21d2009-07-14 23:37:12 +0300366 NetOurIP = 0;
wdenk2d966952002-10-31 22:12:35 +0000367 DhcpRequest(); /* Basically same as BOOTP */
368 break;
Jon Loeligera9807e52007-07-10 11:05:02 -0500369#endif
wdenk2d966952002-10-31 22:12:35 +0000370
371 case BOOTP:
372 BootpTry = 0;
Michael Zaidman16bb21d2009-07-14 23:37:12 +0300373 NetOurIP = 0;
Luca Ceresoli7cbd7482011-05-11 03:59:56 +0000374 BootpRequest();
wdenk2d966952002-10-31 22:12:35 +0000375 break;
376
Peter Tyserf7a48ca2010-09-30 11:25:48 -0500377#if defined(CONFIG_CMD_RARP)
wdenk2d966952002-10-31 22:12:35 +0000378 case RARP:
379 RarpTry = 0;
Michael Zaidman16bb21d2009-07-14 23:37:12 +0300380 NetOurIP = 0;
Luca Ceresoli7cbd7482011-05-11 03:59:56 +0000381 RarpRequest();
wdenk2d966952002-10-31 22:12:35 +0000382 break;
Peter Tyserf7a48ca2010-09-30 11:25:48 -0500383#endif
Jon Loeliger54f35c22007-07-09 17:45:14 -0500384#if defined(CONFIG_CMD_PING)
wdenke6466f62003-06-05 19:27:42 +0000385 case PING:
Joe Hershbergerc21bf372012-05-23 07:58:02 +0000386 ping_start();
wdenke6466f62003-06-05 19:27:42 +0000387 break;
388#endif
Jon Loeliger54f35c22007-07-09 17:45:14 -0500389#if defined(CONFIG_CMD_NFS)
wdenkbe9c1cb2004-02-24 02:00:03 +0000390 case NFS:
391 NfsStart();
392 break;
393#endif
Jon Loeliger54f35c22007-07-09 17:45:14 -0500394#if defined(CONFIG_CMD_CDP)
wdenk145d2c12004-04-15 21:48:45 +0000395 case CDP:
396 CDPStart();
397 break;
398#endif
wdenkb8fb6192004-08-02 21:11:11 +0000399#ifdef CONFIG_NETCONSOLE
400 case NETCONS:
401 NcStart();
402 break;
403#endif
Jon Loeliger54f35c22007-07-09 17:45:14 -0500404#if defined(CONFIG_CMD_SNTP)
wdenkb4ad9622005-04-01 00:25:43 +0000405 case SNTP:
406 SntpStart();
407 break;
408#endif
Robin Getz82f0d232009-07-20 14:53:54 -0400409#if defined(CONFIG_CMD_DNS)
410 case DNS:
411 DnsStart();
412 break;
413#endif
Joe Hershbergerb35a3a62012-05-23 08:00:12 +0000414#if defined(CONFIG_CMD_LINK_LOCAL)
415 case LINKLOCAL:
416 link_local_start();
417 break;
418#endif
wdenk2d966952002-10-31 22:12:35 +0000419 default:
420 break;
421 }
422
wdenk2d966952002-10-31 22:12:35 +0000423 break;
424 }
425
Jon Loeliger54f35c22007-07-09 17:45:14 -0500426#if defined(CONFIG_MII) || defined(CONFIG_CMD_MII)
Luca Ceresoli5c83a962011-05-11 03:59:54 +0000427#if defined(CONFIG_SYS_FAULT_ECHO_LINK_DOWN) && \
428 defined(CONFIG_STATUS_LED) && \
429 defined(STATUS_LED_RED)
wdenk49c3f672003-10-08 22:33:00 +0000430 /*
wdenk9c53f402003-10-15 23:53:47 +0000431 * Echo the inverted link state to the fault LED.
wdenk49c3f672003-10-08 22:33:00 +0000432 */
Luca Ceresolie8ccbb02011-05-04 02:40:43 +0000433 if (miiphy_link(eth_get_dev()->name, CONFIG_SYS_FAULT_MII_ADDR))
Luca Ceresoli7cbd7482011-05-11 03:59:56 +0000434 status_led_set(STATUS_LED_RED, STATUS_LED_OFF);
Luca Ceresolie8ccbb02011-05-04 02:40:43 +0000435 else
Luca Ceresoli7cbd7482011-05-11 03:59:56 +0000436 status_led_set(STATUS_LED_RED, STATUS_LED_ON);
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200437#endif /* CONFIG_SYS_FAULT_ECHO_LINK_DOWN, ... */
wdenk49c3f672003-10-08 22:33:00 +0000438#endif /* CONFIG_MII, ... */
wdenk2d966952002-10-31 22:12:35 +0000439
440 /*
441 * Main packet reception loop. Loop receiving packets until
Joe Hershbergerd4bb76a2012-05-23 07:59:14 +0000442 * someone sets `net_state' to a state that terminates.
wdenk2d966952002-10-31 22:12:35 +0000443 */
444 for (;;) {
445 WATCHDOG_RESET();
446#ifdef CONFIG_SHOW_ACTIVITY
Joe Hershberger77001b432012-05-15 08:59:08 +0000447 show_activity(1);
wdenk2d966952002-10-31 22:12:35 +0000448#endif
449 /*
450 * Check the ethernet for a new packet. The ethernet
451 * receive routine will process it.
452 */
Guennadi Liakhovetskib38c2b32008-04-03 17:04:19 +0200453 eth_rx();
wdenk2d966952002-10-31 22:12:35 +0000454
455 /*
456 * Abort if ctrl-c was pressed.
457 */
458 if (ctrlc()) {
Joe Hershbergerde8205a2012-05-23 07:59:24 +0000459 /* cancel any ARP that may not have completed */
460 NetArpWaitPacketIP = 0;
461
Joe Hershbergerf50357b2012-05-23 07:59:15 +0000462 net_cleanup_loop();
wdenk57b2d802003-06-27 21:31:46 +0000463 eth_halt();
Luca Ceresoli7cbd7482011-05-11 03:59:56 +0000464 puts("\nAbort\n");
Joe Hershberger05a377b2012-05-23 08:01:04 +0000465 /* include a debug print as well incase the debug
466 messages are directed to stderr */
467 debug_cond(DEBUG_INT_STATE, "--- NetLoop Abort!\n");
Simon Glass230467c2011-10-24 18:00:01 +0000468 goto done;
wdenk2d966952002-10-31 22:12:35 +0000469 }
470
wdenke6466f62003-06-05 19:27:42 +0000471 ArpTimeoutCheck();
wdenk2d966952002-10-31 22:12:35 +0000472
473 /*
474 * Check for a timeout, and run the timeout handler
475 * if we have one.
476 */
wdenk5d841732003-08-17 18:55:18 +0000477 if (timeHandler && ((get_timer(0) - timeStart) > timeDelta)) {
wdenk2d966952002-10-31 22:12:35 +0000478 thand_f *x;
479
Jon Loeliger54f35c22007-07-09 17:45:14 -0500480#if defined(CONFIG_MII) || defined(CONFIG_CMD_MII)
Luca Ceresoli7cbd7482011-05-11 03:59:56 +0000481#if defined(CONFIG_SYS_FAULT_ECHO_LINK_DOWN) && \
482 defined(CONFIG_STATUS_LED) && \
483 defined(STATUS_LED_RED)
wdenk49c3f672003-10-08 22:33:00 +0000484 /*
wdenk9c53f402003-10-15 23:53:47 +0000485 * Echo the inverted link state to the fault LED.
wdenk49c3f672003-10-08 22:33:00 +0000486 */
Luca Ceresoli7cbd7482011-05-11 03:59:56 +0000487 if (miiphy_link(eth_get_dev()->name,
Luca Ceresoli5c83a962011-05-11 03:59:54 +0000488 CONFIG_SYS_FAULT_MII_ADDR)) {
Luca Ceresoli7cbd7482011-05-11 03:59:56 +0000489 status_led_set(STATUS_LED_RED, STATUS_LED_OFF);
wdenk49c3f672003-10-08 22:33:00 +0000490 } else {
Luca Ceresoli7cbd7482011-05-11 03:59:56 +0000491 status_led_set(STATUS_LED_RED, STATUS_LED_ON);
wdenk49c3f672003-10-08 22:33:00 +0000492 }
Luca Ceresoli7cbd7482011-05-11 03:59:56 +0000493#endif /* CONFIG_SYS_FAULT_ECHO_LINK_DOWN, ... */
wdenk49c3f672003-10-08 22:33:00 +0000494#endif /* CONFIG_MII, ... */
Joe Hershberger05a377b2012-05-23 08:01:04 +0000495 debug_cond(DEBUG_INT_STATE, "--- NetLoop timeout\n");
wdenk2d966952002-10-31 22:12:35 +0000496 x = timeHandler;
497 timeHandler = (thand_f *)0;
498 (*x)();
499 }
500
501
Joe Hershbergerd4bb76a2012-05-23 07:59:14 +0000502 switch (net_state) {
wdenk2d966952002-10-31 22:12:35 +0000503
504 case NETLOOP_RESTART:
wdenk2d966952002-10-31 22:12:35 +0000505 NetRestarted = 1;
wdenk2d966952002-10-31 22:12:35 +0000506 goto restart;
507
508 case NETLOOP_SUCCESS:
Joe Hershbergerf50357b2012-05-23 07:59:15 +0000509 net_cleanup_loop();
wdenk2d966952002-10-31 22:12:35 +0000510 if (NetBootFileXferSize > 0) {
Wolfgang Denk084d0ce2007-09-12 00:48:57 +0200511 char buf[20];
wdenk2d966952002-10-31 22:12:35 +0000512 printf("Bytes transferred = %ld (%lx hex)\n",
513 NetBootFileXferSize,
514 NetBootFileXferSize);
Wolfgang Denk084d0ce2007-09-12 00:48:57 +0200515 sprintf(buf, "%lX", NetBootFileXferSize);
wdenk2d966952002-10-31 22:12:35 +0000516 setenv("filesize", buf);
wdenk145d2c12004-04-15 21:48:45 +0000517
518 sprintf(buf, "%lX", (unsigned long)load_addr);
519 setenv("fileaddr", buf);
wdenk2d966952002-10-31 22:12:35 +0000520 }
521 eth_halt();
Simon Glass230467c2011-10-24 18:00:01 +0000522 ret = NetBootFileXferSize;
Joe Hershberger05a377b2012-05-23 08:01:04 +0000523 debug_cond(DEBUG_INT_STATE, "--- NetLoop Success!\n");
Simon Glass230467c2011-10-24 18:00:01 +0000524 goto done;
wdenk2d966952002-10-31 22:12:35 +0000525
526 case NETLOOP_FAIL:
Joe Hershbergerf50357b2012-05-23 07:59:15 +0000527 net_cleanup_loop();
Joe Hershberger05a377b2012-05-23 08:01:04 +0000528 debug_cond(DEBUG_INT_STATE, "--- NetLoop Fail!\n");
Simon Glass230467c2011-10-24 18:00:01 +0000529 goto done;
Joe Hershbergerd4bb76a2012-05-23 07:59:14 +0000530
531 case NETLOOP_CONTINUE:
532 continue;
wdenk2d966952002-10-31 22:12:35 +0000533 }
534 }
Simon Glass230467c2011-10-24 18:00:01 +0000535
536done:
Simon Glass2928cd82011-10-26 14:18:38 +0000537#ifdef CONFIG_CMD_TFTPPUT
Simon Glass230467c2011-10-24 18:00:01 +0000538 /* Clear out the handlers */
Joe Hershbergerf50357b2012-05-23 07:59:15 +0000539 net_set_udp_handler(NULL);
Simon Glass230467c2011-10-24 18:00:01 +0000540 net_set_icmp_handler(NULL);
Simon Glass2928cd82011-10-26 14:18:38 +0000541#endif
Simon Glass230467c2011-10-24 18:00:01 +0000542 return ret;
wdenk2d966952002-10-31 22:12:35 +0000543}
544
545/**********************************************************************/
546
547static void
548startAgainTimeout(void)
549{
Joe Hershbergerd4bb76a2012-05-23 07:59:14 +0000550 net_set_state(NETLOOP_RESTART);
wdenk2d966952002-10-31 22:12:35 +0000551}
552
Luca Ceresoli7cbd7482011-05-11 03:59:56 +0000553void NetStartAgain(void)
wdenk2d966952002-10-31 22:12:35 +0000554{
wdenk05939202004-04-18 17:39:38 +0000555 char *nretry;
Remy Bohmer0ddb8e82009-10-28 22:13:39 +0100556 int retry_forever = 0;
557 unsigned long retrycnt = 0;
wdenk145d2c12004-04-15 21:48:45 +0000558
Remy Bohmer0ddb8e82009-10-28 22:13:39 +0100559 nretry = getenv("netretry");
560 if (nretry) {
561 if (!strcmp(nretry, "yes"))
562 retry_forever = 1;
563 else if (!strcmp(nretry, "no"))
564 retrycnt = 0;
565 else if (!strcmp(nretry, "once"))
566 retrycnt = 1;
567 else
568 retrycnt = simple_strtoul(nretry, NULL, 0);
569 } else
570 retry_forever = 1;
571
572 if ((!retry_forever) && (NetTryCount >= retrycnt)) {
573 eth_halt();
Joe Hershbergerd4bb76a2012-05-23 07:59:14 +0000574 net_set_state(NETLOOP_FAIL);
wdenk145d2c12004-04-15 21:48:45 +0000575 return;
576 }
Remy Bohmer0ddb8e82009-10-28 22:13:39 +0100577
578 NetTryCount++;
579
Luca Ceresoli7cbd7482011-05-11 03:59:56 +0000580 eth_halt();
Matthias Fuchsa02d62b2007-12-27 16:58:41 +0100581#if !defined(CONFIG_NET_DO_NOT_TRY_ANOTHER)
Luca Ceresoli7cbd7482011-05-11 03:59:56 +0000582 eth_try_another(!NetRestarted);
Matthias Fuchsa02d62b2007-12-27 16:58:41 +0100583#endif
Luca Ceresoli7cbd7482011-05-11 03:59:56 +0000584 eth_init(gd->bd);
wdenk05939202004-04-18 17:39:38 +0000585 if (NetRestartWrap) {
wdenk2d966952002-10-31 22:12:35 +0000586 NetRestartWrap = 0;
Remy Bohmer0ddb8e82009-10-28 22:13:39 +0100587 if (NetDevExists) {
Luca Ceresoli7cbd7482011-05-11 03:59:56 +0000588 NetSetTimeout(10000UL, startAgainTimeout);
Joe Hershbergerf50357b2012-05-23 07:59:15 +0000589 net_set_udp_handler(NULL);
wdenk05939202004-04-18 17:39:38 +0000590 } else {
Joe Hershbergerd4bb76a2012-05-23 07:59:14 +0000591 net_set_state(NETLOOP_FAIL);
wdenk2d966952002-10-31 22:12:35 +0000592 }
wdenk05939202004-04-18 17:39:38 +0000593 } else {
Joe Hershbergerd4bb76a2012-05-23 07:59:14 +0000594 net_set_state(NETLOOP_RESTART);
wdenk2d966952002-10-31 22:12:35 +0000595 }
wdenk2d966952002-10-31 22:12:35 +0000596}
597
598/**********************************************************************/
599/*
600 * Miscelaneous bits.
601 */
602
Joe Hershbergerf50357b2012-05-23 07:59:15 +0000603static void dummy_handler(uchar *pkt, unsigned dport,
604 IPaddr_t sip, unsigned sport,
605 unsigned len)
Joe Hershbergeraae05082012-05-23 07:58:01 +0000606{
Joe Hershbergeraae05082012-05-23 07:58:01 +0000607}
608
Joe Hershbergerf50357b2012-05-23 07:59:15 +0000609rxhand_f *net_get_udp_handler(void)
610{
611 return udp_packet_handler;
612}
Joe Hershbergeraae05082012-05-23 07:58:01 +0000613
Joe Hershbergerf50357b2012-05-23 07:59:15 +0000614void net_set_udp_handler(rxhand_f *f)
615{
Joe Hershberger05a377b2012-05-23 08:01:04 +0000616 debug_cond(DEBUG_INT_STATE, "--- NetLoop UDP handler set (%p)\n", f);
Joe Hershbergerf50357b2012-05-23 07:59:15 +0000617 if (f == NULL)
618 udp_packet_handler = dummy_handler;
619 else
620 udp_packet_handler = f;
621}
622
623rxhand_f *net_get_arp_handler(void)
wdenk2d966952002-10-31 22:12:35 +0000624{
Joe Hershbergerf50357b2012-05-23 07:59:15 +0000625 return arp_packet_handler;
wdenk2d966952002-10-31 22:12:35 +0000626}
627
Joe Hershbergerf50357b2012-05-23 07:59:15 +0000628void net_set_arp_handler(rxhand_f *f)
629{
Joe Hershberger05a377b2012-05-23 08:01:04 +0000630 debug_cond(DEBUG_INT_STATE, "--- NetLoop ARP handler set (%p)\n", f);
Joe Hershbergerf50357b2012-05-23 07:59:15 +0000631 if (f == NULL)
632 arp_packet_handler = dummy_handler;
633 else
634 arp_packet_handler = f;
635}
636
Simon Glass2928cd82011-10-26 14:18:38 +0000637#ifdef CONFIG_CMD_TFTPPUT
Simon Glass230467c2011-10-24 18:00:01 +0000638void net_set_icmp_handler(rxhand_icmp_f *f)
639{
640 packet_icmp_handler = f;
641}
Simon Glass2928cd82011-10-26 14:18:38 +0000642#endif
wdenk2d966952002-10-31 22:12:35 +0000643
644void
Luca Ceresolif27d91f2011-05-04 02:40:44 +0000645NetSetTimeout(ulong iv, thand_f *f)
wdenk2d966952002-10-31 22:12:35 +0000646{
647 if (iv == 0) {
Joe Hershberger05a377b2012-05-23 08:01:04 +0000648 debug_cond(DEBUG_INT_STATE,
649 "--- NetLoop timeout handler cancelled\n");
wdenk2d966952002-10-31 22:12:35 +0000650 timeHandler = (thand_f *)0;
651 } else {
Joe Hershberger05a377b2012-05-23 08:01:04 +0000652 debug_cond(DEBUG_INT_STATE,
653 "--- NetLoop timeout handler set (%p)\n", f);
wdenk2d966952002-10-31 22:12:35 +0000654 timeHandler = f;
wdenk5d841732003-08-17 18:55:18 +0000655 timeStart = get_timer(0);
656 timeDelta = iv;
wdenk2d966952002-10-31 22:12:35 +0000657 }
658}
659
Joe Hershberger1a6b8d82012-05-23 07:58:10 +0000660int NetSendUDPPacket(uchar *ether, IPaddr_t dest, int dport, int sport,
661 int payload_len)
wdenke6466f62003-06-05 19:27:42 +0000662{
wdenk145d2c12004-04-15 21:48:45 +0000663 uchar *pkt;
Joe Hershberger16be9cb2012-05-23 07:59:08 +0000664 int eth_hdr_size;
665 int pkt_hdr_size;
wdenk145d2c12004-04-15 21:48:45 +0000666
Joe Hershberger017e5c42012-05-23 07:59:22 +0000667 /* make sure the NetTxPacket is initialized (NetInit() was called) */
668 assert(NetTxPacket != NULL);
669 if (NetTxPacket == NULL)
670 return -1;
671
wdenke6466f62003-06-05 19:27:42 +0000672 /* convert to new style broadcast */
673 if (dest == 0)
674 dest = 0xFFFFFFFF;
675
676 /* if broadcast, make the ether address a broadcast and don't do ARP */
677 if (dest == 0xFFFFFFFF)
678 ether = NetBcastAddr;
679
Joe Hershbergerde8205a2012-05-23 07:59:24 +0000680 pkt = (uchar *)NetTxPacket;
Joe Hershberger16be9cb2012-05-23 07:59:08 +0000681
682 eth_hdr_size = NetSetEther(pkt, ether, PROT_IP);
683 pkt += eth_hdr_size;
684 net_set_udp_header(pkt, dest, dport, sport, payload_len);
685 pkt_hdr_size = eth_hdr_size + IP_UDP_HDR_SIZE;
wdenke6466f62003-06-05 19:27:42 +0000686
Joe Hershbergerde8205a2012-05-23 07:59:24 +0000687 /* if MAC address was not discovered yet, do an ARP request */
688 if (memcmp(ether, NetEtherNullAddr, 6) == 0) {
Joe Hershberger05a377b2012-05-23 08:01:04 +0000689 debug_cond(DEBUG_DEV_PKT, "sending ARP for %pI4\n", &dest);
Robin Getz9e0a4d62009-07-23 03:01:03 -0400690
Joe Hershberger16be9cb2012-05-23 07:59:08 +0000691 /* save the ip and eth addr for the packet to send after arp */
wdenke6466f62003-06-05 19:27:42 +0000692 NetArpWaitPacketIP = dest;
693 NetArpWaitPacketMAC = ether;
wdenk145d2c12004-04-15 21:48:45 +0000694
wdenke6466f62003-06-05 19:27:42 +0000695 /* size of the waiting packet */
Joe Hershberger16be9cb2012-05-23 07:59:08 +0000696 NetArpWaitTxPacketSize = pkt_hdr_size + payload_len;
wdenke6466f62003-06-05 19:27:42 +0000697
698 /* and do the ARP request */
699 NetArpWaitTry = 1;
700 NetArpWaitTimerStart = get_timer(0);
701 ArpRequest();
702 return 1; /* waiting */
Joe Hershberger16be9cb2012-05-23 07:59:08 +0000703 } else {
Joe Hershberger05a377b2012-05-23 08:01:04 +0000704 debug_cond(DEBUG_DEV_PKT, "sending UDP to %pI4/%pM\n",
705 &dest, ether);
Joe Hershbergerebe3ca82012-05-23 07:59:13 +0000706 NetSendPacket(NetTxPacket, pkt_hdr_size + payload_len);
Joe Hershberger16be9cb2012-05-23 07:59:08 +0000707 return 0; /* transmitted */
wdenke6466f62003-06-05 19:27:42 +0000708 }
wdenke6466f62003-06-05 19:27:42 +0000709}
wdenk145d2c12004-04-15 21:48:45 +0000710
Alessandro Rubinif119e3d2009-08-07 13:58:56 +0200711#ifdef CONFIG_IP_DEFRAG
712/*
713 * This function collects fragments in a single packet, according
714 * to the algorithm in RFC815. It returns NULL or the pointer to
715 * a complete packet, in static storage
716 */
717#ifndef CONFIG_NET_MAXDEFRAG
718#define CONFIG_NET_MAXDEFRAG 16384
719#endif
720/*
721 * MAXDEFRAG, above, is chosen in the config file and is real data
722 * so we need to add the NFS overhead, which is more than TFTP.
723 * To use sizeof in the internal unnamed structures, we need a real
724 * instance (can't do "sizeof(struct rpc_t.u.reply))", unfortunately).
725 * The compiler doesn't complain nor allocates the actual structure
726 */
727static struct rpc_t rpc_specimen;
728#define IP_PKTSIZE (CONFIG_NET_MAXDEFRAG + sizeof(rpc_specimen.u.reply))
729
Joe Hershbergerc686fa12012-05-23 07:58:05 +0000730#define IP_MAXUDP (IP_PKTSIZE - IP_HDR_SIZE)
Alessandro Rubinif119e3d2009-08-07 13:58:56 +0200731
732/*
733 * this is the packet being assembled, either data or frag control.
734 * Fragments go by 8 bytes, so this union must be 8 bytes long
735 */
736struct hole {
737 /* first_byte is address of this structure */
738 u16 last_byte; /* last byte in this hole + 1 (begin of next hole) */
739 u16 next_hole; /* index of next (in 8-b blocks), 0 == none */
740 u16 prev_hole; /* index of prev, 0 == none */
741 u16 unused;
742};
743
Joe Hershberger6fe8b452012-05-23 07:58:04 +0000744static struct ip_udp_hdr *__NetDefragment(struct ip_udp_hdr *ip, int *lenp)
Alessandro Rubinif119e3d2009-08-07 13:58:56 +0200745{
Joe Hershberger77001b432012-05-15 08:59:08 +0000746 static uchar pkt_buff[IP_PKTSIZE] __aligned(PKTALIGN);
Alessandro Rubinif119e3d2009-08-07 13:58:56 +0200747 static u16 first_hole, total_len;
748 struct hole *payload, *thisfrag, *h, *newh;
Joe Hershberger6fe8b452012-05-23 07:58:04 +0000749 struct ip_udp_hdr *localip = (struct ip_udp_hdr *)pkt_buff;
Alessandro Rubinif119e3d2009-08-07 13:58:56 +0200750 uchar *indata = (uchar *)ip;
751 int offset8, start, len, done = 0;
752 u16 ip_off = ntohs(ip->ip_off);
753
754 /* payload starts after IP header, this fragment is in there */
Joe Hershbergerc686fa12012-05-23 07:58:05 +0000755 payload = (struct hole *)(pkt_buff + IP_HDR_SIZE);
Alessandro Rubinif119e3d2009-08-07 13:58:56 +0200756 offset8 = (ip_off & IP_OFFS);
757 thisfrag = payload + offset8;
758 start = offset8 * 8;
Joe Hershbergerc686fa12012-05-23 07:58:05 +0000759 len = ntohs(ip->ip_len) - IP_HDR_SIZE;
Alessandro Rubinif119e3d2009-08-07 13:58:56 +0200760
761 if (start + len > IP_MAXUDP) /* fragment extends too far */
762 return NULL;
763
764 if (!total_len || localip->ip_id != ip->ip_id) {
765 /* new (or different) packet, reset structs */
766 total_len = 0xffff;
767 payload[0].last_byte = ~0;
768 payload[0].next_hole = 0;
769 payload[0].prev_hole = 0;
770 first_hole = 0;
771 /* any IP header will work, copy the first we received */
Joe Hershbergerc686fa12012-05-23 07:58:05 +0000772 memcpy(localip, ip, IP_HDR_SIZE);
Alessandro Rubinif119e3d2009-08-07 13:58:56 +0200773 }
774
775 /*
776 * What follows is the reassembly algorithm. We use the payload
777 * array as a linked list of hole descriptors, as each hole starts
778 * at a multiple of 8 bytes. However, last byte can be whatever value,
779 * so it is represented as byte count, not as 8-byte blocks.
780 */
781
782 h = payload + first_hole;
783 while (h->last_byte < start) {
784 if (!h->next_hole) {
785 /* no hole that far away */
786 return NULL;
787 }
788 h = payload + h->next_hole;
789 }
790
Fillod Stephanee7ade8b2010-06-11 19:26:43 +0200791 /* last fragment may be 1..7 bytes, the "+7" forces acceptance */
792 if (offset8 + ((len + 7) / 8) <= h - payload) {
Alessandro Rubinif119e3d2009-08-07 13:58:56 +0200793 /* no overlap with holes (dup fragment?) */
794 return NULL;
795 }
796
797 if (!(ip_off & IP_FLAGS_MFRAG)) {
798 /* no more fragmentss: truncate this (last) hole */
799 total_len = start + len;
800 h->last_byte = start + len;
801 }
802
803 /*
804 * There is some overlap: fix the hole list. This code doesn't
805 * deal with a fragment that overlaps with two different holes
806 * (thus being a superset of a previously-received fragment).
807 */
808
Luca Ceresoli7cbd7482011-05-11 03:59:56 +0000809 if ((h >= thisfrag) && (h->last_byte <= start + len)) {
Alessandro Rubinif119e3d2009-08-07 13:58:56 +0200810 /* complete overlap with hole: remove hole */
811 if (!h->prev_hole && !h->next_hole) {
812 /* last remaining hole */
813 done = 1;
814 } else if (!h->prev_hole) {
815 /* first hole */
816 first_hole = h->next_hole;
817 payload[h->next_hole].prev_hole = 0;
818 } else if (!h->next_hole) {
819 /* last hole */
820 payload[h->prev_hole].next_hole = 0;
821 } else {
822 /* in the middle of the list */
823 payload[h->next_hole].prev_hole = h->prev_hole;
824 payload[h->prev_hole].next_hole = h->next_hole;
825 }
826
827 } else if (h->last_byte <= start + len) {
828 /* overlaps with final part of the hole: shorten this hole */
829 h->last_byte = start;
830
831 } else if (h >= thisfrag) {
832 /* overlaps with initial part of the hole: move this hole */
833 newh = thisfrag + (len / 8);
834 *newh = *h;
835 h = newh;
836 if (h->next_hole)
837 payload[h->next_hole].prev_hole = (h - payload);
838 if (h->prev_hole)
839 payload[h->prev_hole].next_hole = (h - payload);
840 else
841 first_hole = (h - payload);
842
843 } else {
844 /* fragment sits in the middle: split the hole */
845 newh = thisfrag + (len / 8);
846 *newh = *h;
847 h->last_byte = start;
848 h->next_hole = (newh - payload);
849 newh->prev_hole = (h - payload);
850 if (newh->next_hole)
851 payload[newh->next_hole].prev_hole = (newh - payload);
852 }
853
854 /* finally copy this fragment and possibly return whole packet */
Joe Hershbergerc686fa12012-05-23 07:58:05 +0000855 memcpy((uchar *)thisfrag, indata + IP_HDR_SIZE, len);
Alessandro Rubinif119e3d2009-08-07 13:58:56 +0200856 if (!done)
857 return NULL;
858
859 localip->ip_len = htons(total_len);
Joe Hershbergerc686fa12012-05-23 07:58:05 +0000860 *lenp = total_len + IP_HDR_SIZE;
Alessandro Rubinif119e3d2009-08-07 13:58:56 +0200861 return localip;
862}
863
Joe Hershberger6fe8b452012-05-23 07:58:04 +0000864static inline struct ip_udp_hdr *NetDefragment(struct ip_udp_hdr *ip, int *lenp)
Alessandro Rubinif119e3d2009-08-07 13:58:56 +0200865{
866 u16 ip_off = ntohs(ip->ip_off);
867 if (!(ip_off & (IP_OFFS | IP_FLAGS_MFRAG)))
868 return ip; /* not a fragment */
869 return __NetDefragment(ip, lenp);
870}
871
872#else /* !CONFIG_IP_DEFRAG */
873
Joe Hershberger6fe8b452012-05-23 07:58:04 +0000874static inline struct ip_udp_hdr *NetDefragment(struct ip_udp_hdr *ip, int *lenp)
Alessandro Rubinif119e3d2009-08-07 13:58:56 +0200875{
876 u16 ip_off = ntohs(ip->ip_off);
877 if (!(ip_off & (IP_OFFS | IP_FLAGS_MFRAG)))
878 return ip; /* not a fragment */
879 return NULL;
880}
881#endif
wdenk2d966952002-10-31 22:12:35 +0000882
Simon Glass43c72962011-10-24 18:00:00 +0000883/**
884 * Receive an ICMP packet. We deal with REDIRECT and PING here, and silently
885 * drop others.
886 *
887 * @parma ip IP packet containing the ICMP
888 */
Joe Hershberger6fe8b452012-05-23 07:58:04 +0000889static void receive_icmp(struct ip_udp_hdr *ip, int len,
Joe Hershberger1178f412012-05-23 07:58:06 +0000890 IPaddr_t src_ip, struct ethernet_hdr *et)
Simon Glass43c72962011-10-24 18:00:00 +0000891{
Joe Hershberger78495612012-05-23 07:58:09 +0000892 struct icmp_hdr *icmph = (struct icmp_hdr *)&ip->udp_src;
Simon Glass43c72962011-10-24 18:00:00 +0000893
894 switch (icmph->type) {
895 case ICMP_REDIRECT:
896 if (icmph->code != ICMP_REDIR_HOST)
897 return;
898 printf(" ICMP Host Redirect to %pI4 ",
899 &icmph->un.gateway);
900 break;
Joe Hershbergerc21bf372012-05-23 07:58:02 +0000901 default:
Simon Glass43c72962011-10-24 18:00:00 +0000902#if defined(CONFIG_CMD_PING)
Joe Hershbergerc21bf372012-05-23 07:58:02 +0000903 ping_receive(et, ip, len);
Simon Glass43c72962011-10-24 18:00:00 +0000904#endif
Simon Glass2928cd82011-10-26 14:18:38 +0000905#ifdef CONFIG_CMD_TFTPPUT
Simon Glass230467c2011-10-24 18:00:01 +0000906 if (packet_icmp_handler)
907 packet_icmp_handler(icmph->type, icmph->code,
908 ntohs(ip->udp_dst), src_ip, ntohs(ip->udp_src),
909 icmph->un.data, ntohs(ip->udp_len));
Simon Glass2928cd82011-10-26 14:18:38 +0000910#endif
Simon Glass43c72962011-10-24 18:00:00 +0000911 break;
912 }
913}
914
wdenk2d966952002-10-31 22:12:35 +0000915void
Joe Hershberger4b7747e2012-05-15 08:59:04 +0000916NetReceive(uchar *inpkt, int len)
wdenk2d966952002-10-31 22:12:35 +0000917{
Joe Hershberger1178f412012-05-23 07:58:06 +0000918 struct ethernet_hdr *et;
Joe Hershberger6fe8b452012-05-23 07:58:04 +0000919 struct ip_udp_hdr *ip;
Joe Hershbergerfacf5352012-05-23 07:58:12 +0000920 IPaddr_t dst_ip;
Luca Ceresoli428ab362011-04-18 06:19:50 +0000921 IPaddr_t src_ip;
Joe Hershbergerfacf5352012-05-23 07:58:12 +0000922 int eth_proto;
Jon Loeliger54f35c22007-07-09 17:45:14 -0500923#if defined(CONFIG_CMD_CDP)
wdenk145d2c12004-04-15 21:48:45 +0000924 int iscdp;
925#endif
926 ushort cti = 0, vlanid = VLAN_NONE, myvlanid, mynvlanid;
wdenk2d966952002-10-31 22:12:35 +0000927
Joe Hershberger05a377b2012-05-23 08:01:04 +0000928 debug_cond(DEBUG_NET_PKT, "packet received\n");
wdenk145d2c12004-04-15 21:48:45 +0000929
Mike Frysingerdc166032009-07-18 21:04:08 -0400930 NetRxPacket = inpkt;
931 NetRxPacketLen = len;
Joe Hershberger1178f412012-05-23 07:58:06 +0000932 et = (struct ethernet_hdr *)inpkt;
wdenk145d2c12004-04-15 21:48:45 +0000933
934 /* too small packet? */
935 if (len < ETHER_HDR_SIZE)
936 return;
Rafal Jaworowski1f2c9a42007-12-27 18:19:02 +0100937
938#ifdef CONFIG_API
939 if (push_packet) {
940 (*push_packet)(inpkt, len);
941 return;
942 }
943#endif
wdenk145d2c12004-04-15 21:48:45 +0000944
Jon Loeliger54f35c22007-07-09 17:45:14 -0500945#if defined(CONFIG_CMD_CDP)
wdenk145d2c12004-04-15 21:48:45 +0000946 /* keep track if packet is CDP */
Joe Hershberger00c62a82012-05-23 07:58:00 +0000947 iscdp = is_cdp_packet(et->et_dest);
wdenk145d2c12004-04-15 21:48:45 +0000948#endif
949
950 myvlanid = ntohs(NetOurVLAN);
951 if (myvlanid == (ushort)-1)
952 myvlanid = VLAN_NONE;
953 mynvlanid = ntohs(NetOurNativeVLAN);
954 if (mynvlanid == (ushort)-1)
955 mynvlanid = VLAN_NONE;
wdenk2d966952002-10-31 22:12:35 +0000956
Joe Hershbergerfacf5352012-05-23 07:58:12 +0000957 eth_proto = ntohs(et->et_protlen);
wdenk2d966952002-10-31 22:12:35 +0000958
Joe Hershbergerfacf5352012-05-23 07:58:12 +0000959 if (eth_proto < 1514) {
Joe Hershberger1178f412012-05-23 07:58:06 +0000960 struct e802_hdr *et802 = (struct e802_hdr *)et;
wdenk2d966952002-10-31 22:12:35 +0000961 /*
Joe Hershbergerc17fa982012-05-23 07:58:11 +0000962 * Got a 802.2 packet. Check the other protocol field.
963 * XXX VLAN over 802.2+SNAP not implemented!
wdenk2d966952002-10-31 22:12:35 +0000964 */
Joe Hershbergerfacf5352012-05-23 07:58:12 +0000965 eth_proto = ntohs(et802->et_prot);
wdenk145d2c12004-04-15 21:48:45 +0000966
Joe Hershberger6fe8b452012-05-23 07:58:04 +0000967 ip = (struct ip_udp_hdr *)(inpkt + E802_HDR_SIZE);
wdenk2d966952002-10-31 22:12:35 +0000968 len -= E802_HDR_SIZE;
wdenk145d2c12004-04-15 21:48:45 +0000969
Joe Hershbergerfacf5352012-05-23 07:58:12 +0000970 } else if (eth_proto != PROT_VLAN) { /* normal packet */
Joe Hershberger6fe8b452012-05-23 07:58:04 +0000971 ip = (struct ip_udp_hdr *)(inpkt + ETHER_HDR_SIZE);
wdenk2d966952002-10-31 22:12:35 +0000972 len -= ETHER_HDR_SIZE;
wdenk145d2c12004-04-15 21:48:45 +0000973
974 } else { /* VLAN packet */
Joe Hershbergerb43784c2012-05-23 07:58:07 +0000975 struct vlan_ethernet_hdr *vet =
976 (struct vlan_ethernet_hdr *)et;
wdenk145d2c12004-04-15 21:48:45 +0000977
Joe Hershberger05a377b2012-05-23 08:01:04 +0000978 debug_cond(DEBUG_NET_PKT, "VLAN packet received\n");
Robin Getz9e0a4d62009-07-23 03:01:03 -0400979
wdenk145d2c12004-04-15 21:48:45 +0000980 /* too small packet? */
981 if (len < VLAN_ETHER_HDR_SIZE)
982 return;
983
984 /* if no VLAN active */
985 if ((ntohs(NetOurVLAN) & VLAN_IDMASK) == VLAN_NONE
Jon Loeliger54f35c22007-07-09 17:45:14 -0500986#if defined(CONFIG_CMD_CDP)
wdenk145d2c12004-04-15 21:48:45 +0000987 && iscdp == 0
988#endif
989 )
990 return;
991
992 cti = ntohs(vet->vet_tag);
993 vlanid = cti & VLAN_IDMASK;
Joe Hershbergerfacf5352012-05-23 07:58:12 +0000994 eth_proto = ntohs(vet->vet_type);
wdenk145d2c12004-04-15 21:48:45 +0000995
Joe Hershberger6fe8b452012-05-23 07:58:04 +0000996 ip = (struct ip_udp_hdr *)(inpkt + VLAN_ETHER_HDR_SIZE);
wdenk145d2c12004-04-15 21:48:45 +0000997 len -= VLAN_ETHER_HDR_SIZE;
wdenk2d966952002-10-31 22:12:35 +0000998 }
999
Joe Hershberger05a377b2012-05-23 08:01:04 +00001000 debug_cond(DEBUG_NET_PKT, "Receive from protocol 0x%x\n", eth_proto);
wdenk2d966952002-10-31 22:12:35 +00001001
Jon Loeliger54f35c22007-07-09 17:45:14 -05001002#if defined(CONFIG_CMD_CDP)
wdenk145d2c12004-04-15 21:48:45 +00001003 if (iscdp) {
Joe Hershbergerd01a7a02012-05-23 07:58:13 +00001004 cdp_receive((uchar *)ip, len);
wdenk145d2c12004-04-15 21:48:45 +00001005 return;
1006 }
1007#endif
1008
1009 if ((myvlanid & VLAN_IDMASK) != VLAN_NONE) {
1010 if (vlanid == VLAN_NONE)
1011 vlanid = (mynvlanid & VLAN_IDMASK);
1012 /* not matched? */
1013 if (vlanid != (myvlanid & VLAN_IDMASK))
1014 return;
1015 }
1016
Joe Hershbergerfacf5352012-05-23 07:58:12 +00001017 switch (eth_proto) {
wdenk2d966952002-10-31 22:12:35 +00001018
1019 case PROT_ARP:
Joe Hershbergeraae05082012-05-23 07:58:01 +00001020 ArpReceive(et, ip, len);
wdenkcb99da52005-01-12 00:15:14 +00001021 break;
wdenk2d966952002-10-31 22:12:35 +00001022
Peter Tyserf7a48ca2010-09-30 11:25:48 -05001023#ifdef CONFIG_CMD_RARP
wdenk2d966952002-10-31 22:12:35 +00001024 case PROT_RARP:
Joe Hershberger61b4de62012-05-23 07:58:03 +00001025 rarp_receive(ip, len);
wdenk2d966952002-10-31 22:12:35 +00001026 break;
Peter Tyserf7a48ca2010-09-30 11:25:48 -05001027#endif
wdenk2d966952002-10-31 22:12:35 +00001028 case PROT_IP:
Joe Hershberger05a377b2012-05-23 08:01:04 +00001029 debug_cond(DEBUG_NET_PKT, "Got IP\n");
Alessandro Rubinif119e3d2009-08-07 13:58:56 +02001030 /* Before we start poking the header, make sure it is there */
Joe Hershberger6fe8b452012-05-23 07:58:04 +00001031 if (len < IP_UDP_HDR_SIZE) {
1032 debug("len bad %d < %lu\n", len,
1033 (ulong)IP_UDP_HDR_SIZE);
wdenk2d966952002-10-31 22:12:35 +00001034 return;
1035 }
Alessandro Rubinif119e3d2009-08-07 13:58:56 +02001036 /* Check the packet length */
wdenk2d966952002-10-31 22:12:35 +00001037 if (len < ntohs(ip->ip_len)) {
Joe Hershberger05a377b2012-05-23 08:01:04 +00001038 debug("len bad %d < %d\n", len, ntohs(ip->ip_len));
wdenk2d966952002-10-31 22:12:35 +00001039 return;
1040 }
1041 len = ntohs(ip->ip_len);
Joe Hershberger05a377b2012-05-23 08:01:04 +00001042 debug_cond(DEBUG_NET_PKT, "len=%d, v=%02x\n",
1043 len, ip->ip_hl_v & 0xff);
Robin Getz9e0a4d62009-07-23 03:01:03 -04001044
Alessandro Rubinif119e3d2009-08-07 13:58:56 +02001045 /* Can't deal with anything except IPv4 */
Luca Ceresolie8ccbb02011-05-04 02:40:43 +00001046 if ((ip->ip_hl_v & 0xf0) != 0x40)
wdenk2d966952002-10-31 22:12:35 +00001047 return;
Alessandro Rubinif119e3d2009-08-07 13:58:56 +02001048 /* Can't deal with IP options (headers != 20 bytes) */
Luca Ceresolie8ccbb02011-05-04 02:40:43 +00001049 if ((ip->ip_hl_v & 0x0f) > 0x05)
Remy Bohmerb9535782008-06-03 15:48:17 +02001050 return;
Alessandro Rubinif119e3d2009-08-07 13:58:56 +02001051 /* Check the Checksum of the header */
Joe Hershbergerc686fa12012-05-23 07:58:05 +00001052 if (!NetCksumOk((uchar *)ip, IP_HDR_SIZE / 2)) {
Joe Hershberger05a377b2012-05-23 08:01:04 +00001053 debug("checksum bad\n");
wdenk2d966952002-10-31 22:12:35 +00001054 return;
1055 }
Alessandro Rubinif119e3d2009-08-07 13:58:56 +02001056 /* If it is not for us, ignore it */
Joe Hershbergerfacf5352012-05-23 07:58:12 +00001057 dst_ip = NetReadIP(&ip->ip_dst);
1058 if (NetOurIP && dst_ip != NetOurIP && dst_ip != 0xFFFFFFFF) {
David Updegraff7280da72007-06-11 10:41:07 -05001059#ifdef CONFIG_MCAST_TFTP
Joe Hershbergerfacf5352012-05-23 07:58:12 +00001060 if (Mcast_addr != dst_ip)
David Updegraff7280da72007-06-11 10:41:07 -05001061#endif
Luca Ceresolib19d0b72011-05-04 02:40:46 +00001062 return;
wdenk2d966952002-10-31 22:12:35 +00001063 }
Luca Ceresoli428ab362011-04-18 06:19:50 +00001064 /* Read source IP address for later use */
1065 src_ip = NetReadIP(&ip->ip_src);
wdenk2d966952002-10-31 22:12:35 +00001066 /*
Alessandro Rubinif119e3d2009-08-07 13:58:56 +02001067 * The function returns the unchanged packet if it's not
1068 * a fragment, and either the complete packet or NULL if
1069 * it is a fragment (if !CONFIG_IP_DEFRAG, it returns NULL)
1070 */
Luca Ceresolidb210822011-05-04 02:40:47 +00001071 ip = NetDefragment(ip, &len);
1072 if (!ip)
Alessandro Rubinif119e3d2009-08-07 13:58:56 +02001073 return;
1074 /*
wdenk2d966952002-10-31 22:12:35 +00001075 * watch for ICMP host redirects
1076 *
wdenk57b2d802003-06-27 21:31:46 +00001077 * There is no real handler code (yet). We just watch
1078 * for ICMP host redirect messages. In case anybody
1079 * sees these messages: please contact me
1080 * (wd@denx.de), or - even better - send me the
1081 * necessary fixes :-)
wdenk2d966952002-10-31 22:12:35 +00001082 *
wdenk57b2d802003-06-27 21:31:46 +00001083 * Note: in all cases where I have seen this so far
1084 * it was a problem with the router configuration,
1085 * for instance when a router was configured in the
1086 * BOOTP reply, but the TFTP server was on the same
1087 * subnet. So this is probably a warning that your
1088 * configuration might be wrong. But I'm not really
1089 * sure if there aren't any other situations.
Simon Glass230467c2011-10-24 18:00:01 +00001090 *
1091 * Simon Glass <sjg@chromium.org>: We get an ICMP when
1092 * we send a tftp packet to a dead connection, or when
1093 * there is no server at the other end.
wdenk2d966952002-10-31 22:12:35 +00001094 */
1095 if (ip->ip_p == IPPROTO_ICMP) {
Simon Glass43c72962011-10-24 18:00:00 +00001096 receive_icmp(ip, len, src_ip, et);
1097 return;
wdenk2d966952002-10-31 22:12:35 +00001098 } else if (ip->ip_p != IPPROTO_UDP) { /* Only UDP packets */
1099 return;
1100 }
1101
Joe Hershberger05a377b2012-05-23 08:01:04 +00001102 debug_cond(DEBUG_DEV_PKT,
1103 "received UDP (to=%pI4, from=%pI4, len=%d)\n",
1104 &dst_ip, &src_ip, len);
1105
Stefan Roesedfced812005-08-12 20:06:52 +02001106#ifdef CONFIG_UDP_CHECKSUM
1107 if (ip->udp_xsum != 0) {
Wolfgang Denk30b87322005-08-12 23:43:12 +02001108 ulong xsum;
Stefan Roesedfced812005-08-12 20:06:52 +02001109 ushort *sumptr;
1110 ushort sumlen;
1111
1112 xsum = ip->ip_p;
1113 xsum += (ntohs(ip->udp_len));
1114 xsum += (ntohl(ip->ip_src) >> 16) & 0x0000ffff;
1115 xsum += (ntohl(ip->ip_src) >> 0) & 0x0000ffff;
1116 xsum += (ntohl(ip->ip_dst) >> 16) & 0x0000ffff;
1117 xsum += (ntohl(ip->ip_dst) >> 0) & 0x0000ffff;
1118
1119 sumlen = ntohs(ip->udp_len);
1120 sumptr = (ushort *) &(ip->udp_src);
1121
1122 while (sumlen > 1) {
Wolfgang Denk30b87322005-08-12 23:43:12 +02001123 ushort sumdata;
Stefan Roesedfced812005-08-12 20:06:52 +02001124
1125 sumdata = *sumptr++;
1126 xsum += ntohs(sumdata);
1127 sumlen -= 2;
1128 }
1129 if (sumlen > 0) {
Wolfgang Denk30b87322005-08-12 23:43:12 +02001130 ushort sumdata;
Stefan Roesedfced812005-08-12 20:06:52 +02001131
1132 sumdata = *(unsigned char *) sumptr;
Wolfgang Denk30b87322005-08-12 23:43:12 +02001133 sumdata = (sumdata << 8) & 0xff00;
Stefan Roesedfced812005-08-12 20:06:52 +02001134 xsum += sumdata;
1135 }
1136 while ((xsum >> 16) != 0) {
Luca Ceresoli5c83a962011-05-11 03:59:54 +00001137 xsum = (xsum & 0x0000ffff) +
1138 ((xsum >> 16) & 0x0000ffff);
Stefan Roesedfced812005-08-12 20:06:52 +02001139 }
1140 if ((xsum != 0x00000000) && (xsum != 0x0000ffff)) {
Wolfgang Denk12cec0a2008-07-11 01:16:00 +02001141 printf(" UDP wrong checksum %08lx %08x\n",
1142 xsum, ntohs(ip->udp_xsum));
Stefan Roesedfced812005-08-12 20:06:52 +02001143 return;
1144 }
1145 }
1146#endif
1147
David Updegraff7280da72007-06-11 10:41:07 -05001148
wdenkb8fb6192004-08-02 21:11:11 +00001149#ifdef CONFIG_NETCONSOLE
Joe Hershberger6fe8b452012-05-23 07:58:04 +00001150 nc_input_packet((uchar *)ip + IP_UDP_HDR_SIZE,
1151 ntohs(ip->udp_dst),
1152 ntohs(ip->udp_src),
1153 ntohs(ip->udp_len) - UDP_HDR_SIZE);
wdenkb8fb6192004-08-02 21:11:11 +00001154#endif
wdenk2d966952002-10-31 22:12:35 +00001155 /*
1156 * IP header OK. Pass the packet to the current handler.
1157 */
Joe Hershbergerf50357b2012-05-23 07:59:15 +00001158 (*udp_packet_handler)((uchar *)ip + IP_UDP_HDR_SIZE,
1159 ntohs(ip->udp_dst),
1160 src_ip,
1161 ntohs(ip->udp_src),
1162 ntohs(ip->udp_len) - UDP_HDR_SIZE);
wdenk2d966952002-10-31 22:12:35 +00001163 break;
1164 }
1165}
1166
1167
1168/**********************************************************************/
1169
Simon Glassd6c5f552011-10-24 18:00:02 +00001170static int net_check_prereq(enum proto_t protocol)
wdenk2d966952002-10-31 22:12:35 +00001171{
1172 switch (protocol) {
wdenk05939202004-04-18 17:39:38 +00001173 /* Fall through */
Jon Loeliger54f35c22007-07-09 17:45:14 -05001174#if defined(CONFIG_CMD_PING)
wdenke6466f62003-06-05 19:27:42 +00001175 case PING:
wdenk05939202004-04-18 17:39:38 +00001176 if (NetPingIP == 0) {
Luca Ceresoli7cbd7482011-05-11 03:59:56 +00001177 puts("*** ERROR: ping address not given\n");
Luca Ceresoli5fe6de22011-05-04 02:40:45 +00001178 return 1;
wdenk05939202004-04-18 17:39:38 +00001179 }
1180 goto common;
wdenke6466f62003-06-05 19:27:42 +00001181#endif
Jon Loeliger54f35c22007-07-09 17:45:14 -05001182#if defined(CONFIG_CMD_SNTP)
wdenkb4ad9622005-04-01 00:25:43 +00001183 case SNTP:
1184 if (NetNtpServerIP == 0) {
Luca Ceresoli7cbd7482011-05-11 03:59:56 +00001185 puts("*** ERROR: NTP server address not given\n");
Luca Ceresoli5fe6de22011-05-04 02:40:45 +00001186 return 1;
wdenkb4ad9622005-04-01 00:25:43 +00001187 }
1188 goto common;
1189#endif
Robin Getz82f0d232009-07-20 14:53:54 -04001190#if defined(CONFIG_CMD_DNS)
1191 case DNS:
1192 if (NetOurDNSIP == 0) {
1193 puts("*** ERROR: DNS server address not given\n");
1194 return 1;
1195 }
1196 goto common;
1197#endif
Jon Loeliger54f35c22007-07-09 17:45:14 -05001198#if defined(CONFIG_CMD_NFS)
wdenkbe9c1cb2004-02-24 02:00:03 +00001199 case NFS:
1200#endif
Simon Glassd6c5f552011-10-24 18:00:02 +00001201 case TFTPGET:
Simon Glass6a398d22011-10-24 18:00:07 +00001202 case TFTPPUT:
wdenk05939202004-04-18 17:39:38 +00001203 if (NetServerIP == 0) {
Luca Ceresoli7cbd7482011-05-11 03:59:56 +00001204 puts("*** ERROR: `serverip' not set\n");
Luca Ceresoli5fe6de22011-05-04 02:40:45 +00001205 return 1;
wdenk05939202004-04-18 17:39:38 +00001206 }
Luca Ceresoli7cbd7482011-05-11 03:59:56 +00001207#if defined(CONFIG_CMD_PING) || defined(CONFIG_CMD_SNTP) || \
1208 defined(CONFIG_CMD_DNS)
1209common:
wdenke6466f62003-06-05 19:27:42 +00001210#endif
Simon Guinot00dceba2011-05-01 23:38:40 +00001211 /* Fall through */
wdenke6466f62003-06-05 19:27:42 +00001212
Simon Guinot00dceba2011-05-01 23:38:40 +00001213 case NETCONS:
Luca Ceresoli7aa81a42011-05-17 00:03:40 +00001214 case TFTPSRV:
wdenk05939202004-04-18 17:39:38 +00001215 if (NetOurIP == 0) {
Luca Ceresoli7cbd7482011-05-11 03:59:56 +00001216 puts("*** ERROR: `ipaddr' not set\n");
Luca Ceresoli5fe6de22011-05-04 02:40:45 +00001217 return 1;
wdenk05939202004-04-18 17:39:38 +00001218 }
1219 /* Fall through */
wdenk2d966952002-10-31 22:12:35 +00001220
Peter Tyserf7a48ca2010-09-30 11:25:48 -05001221#ifdef CONFIG_CMD_RARP
wdenk2d966952002-10-31 22:12:35 +00001222 case RARP:
Peter Tyserf7a48ca2010-09-30 11:25:48 -05001223#endif
wdenk2d966952002-10-31 22:12:35 +00001224 case BOOTP:
wdenk145d2c12004-04-15 21:48:45 +00001225 case CDP:
Peter Tyserf7a48ca2010-09-30 11:25:48 -05001226 case DHCP:
Joe Hershbergerb35a3a62012-05-23 08:00:12 +00001227 case LINKLOCAL:
Luca Ceresoli7cbd7482011-05-11 03:59:56 +00001228 if (memcmp(NetOurEther, "\0\0\0\0\0\0", 6) == 0) {
Luca Ceresoli7cbd7482011-05-11 03:59:56 +00001229 int num = eth_get_dev_index();
wdenk2d966952002-10-31 22:12:35 +00001230
wdenk05939202004-04-18 17:39:38 +00001231 switch (num) {
1232 case -1:
Luca Ceresoli7cbd7482011-05-11 03:59:56 +00001233 puts("*** ERROR: No ethernet found.\n");
Luca Ceresoli5fe6de22011-05-04 02:40:45 +00001234 return 1;
wdenk05939202004-04-18 17:39:38 +00001235 case 0:
Luca Ceresoli7cbd7482011-05-11 03:59:56 +00001236 puts("*** ERROR: `ethaddr' not set\n");
wdenk2d966952002-10-31 22:12:35 +00001237 break;
wdenk05939202004-04-18 17:39:38 +00001238 default:
Luca Ceresoli7cbd7482011-05-11 03:59:56 +00001239 printf("*** ERROR: `eth%daddr' not set\n",
wdenk2d966952002-10-31 22:12:35 +00001240 num);
1241 break;
wdenk05939202004-04-18 17:39:38 +00001242 }
wdenk2d966952002-10-31 22:12:35 +00001243
Luca Ceresoli7cbd7482011-05-11 03:59:56 +00001244 NetStartAgain();
Luca Ceresoli5fe6de22011-05-04 02:40:45 +00001245 return 2;
wdenk05939202004-04-18 17:39:38 +00001246 }
1247 /* Fall through */
1248 default:
Luca Ceresoli5fe6de22011-05-04 02:40:45 +00001249 return 0;
wdenk2d966952002-10-31 22:12:35 +00001250 }
Luca Ceresoli5fe6de22011-05-04 02:40:45 +00001251 return 0; /* OK */
wdenk2d966952002-10-31 22:12:35 +00001252}
1253/**********************************************************************/
1254
1255int
Luca Ceresolif27d91f2011-05-04 02:40:44 +00001256NetCksumOk(uchar *ptr, int len)
wdenk2d966952002-10-31 22:12:35 +00001257{
1258 return !((NetCksum(ptr, len) + 1) & 0xfffe);
1259}
1260
1261
1262unsigned
Luca Ceresolif27d91f2011-05-04 02:40:44 +00001263NetCksum(uchar *ptr, int len)
wdenk2d966952002-10-31 22:12:35 +00001264{
1265 ulong xsum;
Stefan Roeseac553282005-08-31 12:55:50 +02001266 ushort *p = (ushort *)ptr;
wdenk2d966952002-10-31 22:12:35 +00001267
1268 xsum = 0;
1269 while (len-- > 0)
Wolfgang Denk3135c542005-08-26 01:36:03 +02001270 xsum += *p++;
wdenk2d966952002-10-31 22:12:35 +00001271 xsum = (xsum & 0xffff) + (xsum >> 16);
1272 xsum = (xsum & 0xffff) + (xsum >> 16);
Luca Ceresoli5fe6de22011-05-04 02:40:45 +00001273 return xsum & 0xffff;
wdenk2d966952002-10-31 22:12:35 +00001274}
1275
wdenk145d2c12004-04-15 21:48:45 +00001276int
1277NetEthHdrSize(void)
1278{
1279 ushort myvlanid;
1280
1281 myvlanid = ntohs(NetOurVLAN);
1282 if (myvlanid == (ushort)-1)
1283 myvlanid = VLAN_NONE;
wdenk2d966952002-10-31 22:12:35 +00001284
Luca Ceresoli5c83a962011-05-11 03:59:54 +00001285 return ((myvlanid & VLAN_IDMASK) == VLAN_NONE) ? ETHER_HDR_SIZE :
1286 VLAN_ETHER_HDR_SIZE;
wdenk145d2c12004-04-15 21:48:45 +00001287}
1288
1289int
Joe Hershberger4b7747e2012-05-15 08:59:04 +00001290NetSetEther(uchar *xet, uchar * addr, uint prot)
wdenk2d966952002-10-31 22:12:35 +00001291{
Joe Hershberger1178f412012-05-23 07:58:06 +00001292 struct ethernet_hdr *et = (struct ethernet_hdr *)xet;
wdenk145d2c12004-04-15 21:48:45 +00001293 ushort myvlanid;
1294
1295 myvlanid = ntohs(NetOurVLAN);
1296 if (myvlanid == (ushort)-1)
1297 myvlanid = VLAN_NONE;
wdenk2d966952002-10-31 22:12:35 +00001298
Luca Ceresoli7cbd7482011-05-11 03:59:56 +00001299 memcpy(et->et_dest, addr, 6);
1300 memcpy(et->et_src, NetOurEther, 6);
wdenk145d2c12004-04-15 21:48:45 +00001301 if ((myvlanid & VLAN_IDMASK) == VLAN_NONE) {
Luca Ceresolib19d0b72011-05-04 02:40:46 +00001302 et->et_protlen = htons(prot);
wdenk145d2c12004-04-15 21:48:45 +00001303 return ETHER_HDR_SIZE;
1304 } else {
Joe Hershbergerb43784c2012-05-23 07:58:07 +00001305 struct vlan_ethernet_hdr *vet =
1306 (struct vlan_ethernet_hdr *)xet;
wdenk2d966952002-10-31 22:12:35 +00001307
wdenk145d2c12004-04-15 21:48:45 +00001308 vet->vet_vlan_type = htons(PROT_VLAN);
1309 vet->vet_tag = htons((0 << 5) | (myvlanid & VLAN_IDMASK));
1310 vet->vet_type = htons(prot);
1311 return VLAN_ETHER_HDR_SIZE;
1312 }
1313}
wdenk2d966952002-10-31 22:12:35 +00001314
Joe Hershberger530cd6b2012-05-23 07:59:16 +00001315int net_update_ether(struct ethernet_hdr *et, uchar *addr, uint prot)
1316{
1317 ushort protlen;
1318
1319 memcpy(et->et_dest, addr, 6);
1320 memcpy(et->et_src, NetOurEther, 6);
1321 protlen = ntohs(et->et_protlen);
1322 if (protlen == PROT_VLAN) {
1323 struct vlan_ethernet_hdr *vet =
1324 (struct vlan_ethernet_hdr *)et;
1325 vet->vet_type = htons(prot);
1326 return VLAN_ETHER_HDR_SIZE;
1327 } else if (protlen > 1514) {
1328 et->et_protlen = htons(prot);
1329 return ETHER_HDR_SIZE;
1330 } else {
1331 /* 802.2 + SNAP */
1332 struct e802_hdr *et802 = (struct e802_hdr *)et;
1333 et802->et_prot = htons(prot);
1334 return E802_HDR_SIZE;
1335 }
1336}
1337
Joe Hershberger2ed5b492012-05-23 07:59:07 +00001338void net_set_ip_header(uchar *pkt, IPaddr_t dest, IPaddr_t source)
wdenk2d966952002-10-31 22:12:35 +00001339{
Joe Hershberger2ed5b492012-05-23 07:59:07 +00001340 struct ip_udp_hdr *ip = (struct ip_udp_hdr *)pkt;
wdenk2d966952002-10-31 22:12:35 +00001341
1342 /*
Joe Hershberger2ed5b492012-05-23 07:59:07 +00001343 * Construct an IP header.
wdenk2d966952002-10-31 22:12:35 +00001344 */
Luca Ceresoli5c83a962011-05-11 03:59:54 +00001345 /* IP_HDR_SIZE / 4 (not including UDP) */
1346 ip->ip_hl_v = 0x45;
wdenk2d966952002-10-31 22:12:35 +00001347 ip->ip_tos = 0;
Joe Hershberger2ed5b492012-05-23 07:59:07 +00001348 ip->ip_len = htons(IP_HDR_SIZE);
wdenk2d966952002-10-31 22:12:35 +00001349 ip->ip_id = htons(NetIPID++);
Peter Tyser0885bf02008-12-01 16:26:20 -06001350 ip->ip_off = htons(IP_FLAGS_DFRAG); /* Don't fragment */
wdenk2d966952002-10-31 22:12:35 +00001351 ip->ip_ttl = 255;
wdenk2d966952002-10-31 22:12:35 +00001352 ip->ip_sum = 0;
Luca Ceresoli5c83a962011-05-11 03:59:54 +00001353 /* already in network byte order */
Joe Hershberger2ed5b492012-05-23 07:59:07 +00001354 NetCopyIP((void *)&ip->ip_src, &source);
1355 /* already in network byte order */
Luca Ceresolif27d91f2011-05-04 02:40:44 +00001356 NetCopyIP((void *)&ip->ip_dst, &dest);
Joe Hershberger2ed5b492012-05-23 07:59:07 +00001357}
1358
1359void net_set_udp_header(uchar *pkt, IPaddr_t dest, int dport, int sport,
1360 int len)
1361{
1362 struct ip_udp_hdr *ip = (struct ip_udp_hdr *)pkt;
1363
1364 /*
1365 * If the data is an odd number of bytes, zero the
1366 * byte after the last byte so that the checksum
1367 * will work.
1368 */
1369 if (len & 1)
1370 pkt[IP_UDP_HDR_SIZE + len] = 0;
1371
1372 net_set_ip_header(pkt, dest, NetOurIP);
1373 ip->ip_len = htons(IP_UDP_HDR_SIZE + len);
1374 ip->ip_p = IPPROTO_UDP;
1375 ip->ip_sum = ~NetCksum((uchar *)ip, IP_HDR_SIZE >> 1);
1376
wdenk2d966952002-10-31 22:12:35 +00001377 ip->udp_src = htons(sport);
1378 ip->udp_dst = htons(dport);
Joe Hershberger6fe8b452012-05-23 07:58:04 +00001379 ip->udp_len = htons(UDP_HDR_SIZE + len);
wdenk2d966952002-10-31 22:12:35 +00001380 ip->udp_xsum = 0;
wdenk2d966952002-10-31 22:12:35 +00001381}
1382
Luca Ceresoli7cbd7482011-05-11 03:59:56 +00001383void copy_filename(char *dst, const char *src, int size)
wdenk2d966952002-10-31 22:12:35 +00001384{
1385 if (*src && (*src == '"')) {
1386 ++src;
1387 --size;
1388 }
1389
Luca Ceresolie8ccbb02011-05-04 02:40:43 +00001390 while ((--size > 0) && *src && (*src != '"'))
wdenk2d966952002-10-31 22:12:35 +00001391 *dst++ = *src++;
wdenk2d966952002-10-31 22:12:35 +00001392 *dst = '\0';
1393}
1394
Luca Ceresoli5c83a962011-05-11 03:59:54 +00001395#if defined(CONFIG_CMD_NFS) || \
1396 defined(CONFIG_CMD_SNTP) || \
1397 defined(CONFIG_CMD_DNS)
Robin Getz82f0d232009-07-20 14:53:54 -04001398/*
Robin Getz55b50e92010-03-08 14:07:00 -05001399 * make port a little random (1024-17407)
1400 * This keeps the math somewhat trivial to compute, and seems to work with
1401 * all supported protocols/clients/servers
Robin Getz82f0d232009-07-20 14:53:54 -04001402 */
1403unsigned int random_port(void)
1404{
Robin Getz55b50e92010-03-08 14:07:00 -05001405 return 1024 + (get_timer(0) % 0x4000);
Robin Getz82f0d232009-07-20 14:53:54 -04001406}
1407#endif
1408
Luca Ceresoli7cbd7482011-05-11 03:59:56 +00001409void ip_to_string(IPaddr_t x, char *s)
wdenk2d966952002-10-31 22:12:35 +00001410{
Luca Ceresoli7cbd7482011-05-11 03:59:56 +00001411 x = ntohl(x);
1412 sprintf(s, "%d.%d.%d.%d",
1413 (int) ((x >> 24) & 0xff),
1414 (int) ((x >> 16) & 0xff),
1415 (int) ((x >> 8) & 0xff), (int) ((x >> 0) & 0xff)
wdenk05939202004-04-18 17:39:38 +00001416 );
wdenk2d966952002-10-31 22:12:35 +00001417}
1418
wdenk145d2c12004-04-15 21:48:45 +00001419void VLAN_to_string(ushort x, char *s)
1420{
1421 x = ntohs(x);
1422
1423 if (x == (ushort)-1)
1424 x = VLAN_NONE;
1425
1426 if (x == VLAN_NONE)
1427 strcpy(s, "none");
1428 else
1429 sprintf(s, "%d", x & VLAN_IDMASK);
1430}
1431
Mike Frysinger89c73922010-10-20 07:16:48 -04001432ushort string_to_VLAN(const char *s)
wdenk145d2c12004-04-15 21:48:45 +00001433{
1434 ushort id;
1435
1436 if (s == NULL)
wdenk656140b2004-04-25 13:18:40 +00001437 return htons(VLAN_NONE);
wdenk145d2c12004-04-15 21:48:45 +00001438
1439 if (*s < '0' || *s > '9')
1440 id = VLAN_NONE;
1441 else
1442 id = (ushort)simple_strtoul(s, NULL, 10);
1443
wdenk656140b2004-04-25 13:18:40 +00001444 return htons(id);
wdenk145d2c12004-04-15 21:48:45 +00001445}
1446
wdenk145d2c12004-04-15 21:48:45 +00001447ushort getenv_VLAN(char *var)
1448{
Luca Ceresoli5fe6de22011-05-04 02:40:45 +00001449 return string_to_VLAN(getenv(var));
wdenk145d2c12004-04-15 21:48:45 +00001450}