blob: da68a61d63887f32b11e52274b4ab96aecc9a0ae [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 Hershbergerd22c3382012-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 Denkb2f50802005-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
wdenkcbd8a352004-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
wdenkea287de2005-04-01 00:25:43 +000073 *
74 * SNTP:
75 *
Wolfgang Denkb2f50802005-08-12 23:43:12 +020076 * Prerequisites: - own ethernet address
wdenkea287de2005-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 Hershberger4545f4e2012-05-23 07:58:15 +000086#if defined(CONFIG_STATUS_LED)
87#include <miiphy.h>
88#include <status_led.h>
89#endif
90#include <watchdog.h>
91#include <linux/compiler.h>
Joe Hershbergerd280d3f2012-05-23 07:58:01 +000092#include "arp.h"
wdenk2d966952002-10-31 22:12:35 +000093#include "bootp.h"
Joe Hershbergerf575ae12012-05-23 07:57:59 +000094#include "cdp.h"
Robin Getz1a32bf42009-07-20 14:53:54 -040095#if defined(CONFIG_CMD_DNS)
96#include "dns.h"
97#endif
Joe Hershbergerd22c3382012-05-23 08:00:12 +000098#include "link_local.h"
Joe Hershberger4545f4e2012-05-23 07:58:15 +000099#include "nfs.h"
Joe Hershbergera36b12f2012-05-23 07:58:02 +0000100#include "ping.h"
Joe Hershberger4545f4e2012-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 Denkd87080b2006-03-31 18:32:53 +0200107DECLARE_GLOBAL_DATA_PTR;
108
wdenk2d966952002-10-31 22:12:35 +0000109/** BOOTP EXTENTIONS **/
110
Luca Ceresoli3e38e422011-05-11 03:59:54 +0000111/* Our subnet mask (0=unknown) */
Luca Ceresolic586ce62011-05-11 03:59:55 +0000112IPaddr_t NetOurSubnetMask;
Luca Ceresoli3e38e422011-05-11 03:59:54 +0000113/* Our gateways IP address */
Luca Ceresolic586ce62011-05-11 03:59:55 +0000114IPaddr_t NetOurGatewayIP;
Luca Ceresoli3e38e422011-05-11 03:59:54 +0000115/* Our DNS IP address */
Luca Ceresolic586ce62011-05-11 03:59:55 +0000116IPaddr_t NetOurDNSIP;
Jon Loeliger1fe80d72007-07-09 22:08:34 -0500117#if defined(CONFIG_BOOTP_DNS2)
Luca Ceresoli3e38e422011-05-11 03:59:54 +0000118/* Our 2nd DNS IP address */
Luca Ceresolic586ce62011-05-11 03:59:55 +0000119IPaddr_t NetOurDNS2IP;
stroesefe389a82003-08-28 14:17:32 +0000120#endif
Luca Ceresoli3e38e422011-05-11 03:59:54 +0000121/* Our NIS domain */
Luca Ceresolic586ce62011-05-11 03:59:55 +0000122char NetOurNISDomain[32] = {0,};
Luca Ceresoli3e38e422011-05-11 03:59:54 +0000123/* Our hostname */
Luca Ceresolic586ce62011-05-11 03:59:55 +0000124char NetOurHostName[32] = {0,};
Luca Ceresoli3e38e422011-05-11 03:59:54 +0000125/* Our bootpath */
Luca Ceresolic586ce62011-05-11 03:59:55 +0000126char NetOurRootPath[64] = {0,};
Luca Ceresoli3e38e422011-05-11 03:59:54 +0000127/* Our bootfile size in blocks */
Luca Ceresolic586ce62011-05-11 03:59:55 +0000128ushort NetBootFileSize;
wdenk2d966952002-10-31 22:12:35 +0000129
David Updegraff53a5c422007-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 Ceresoli3e38e422011-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 Ceresolic586ce62011-05-11 03:59:55 +0000141uchar NetServerEther[6];
Luca Ceresoli3e38e422011-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 Hershbergerdb288a92012-05-15 08:59:04 +0000147uchar *NetRxPacket;
Luca Ceresoli3e38e422011-05-11 03:59:54 +0000148/* Current rx packet length */
149int NetRxPacketLen;
150/* IP packet ID */
151unsigned NetIPID;
152/* Ethernet bcast address */
Luca Ceresolic586ce62011-05-11 03:59:55 +0000153uchar NetBcastAddr[6] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
154uchar NetEtherNullAddr[6];
Rafal Jaworowskif85b6072007-12-27 18:19:02 +0100155#ifdef CONFIG_API
Joe Hershbergerdb288a92012-05-15 08:59:04 +0000156void (*push_packet)(void *, int len) = 0;
Rafal Jaworowskif85b6072007-12-27 18:19:02 +0100157#endif
Luca Ceresoli3e38e422011-05-11 03:59:54 +0000158/* Network loop state */
Joe Hershberger22f6e992012-05-23 07:59:14 +0000159enum net_loop_state net_state;
Luca Ceresoli3e38e422011-05-11 03:59:54 +0000160/* Tried all network devices */
Luca Ceresolic586ce62011-05-11 03:59:55 +0000161int NetRestartWrap;
Luca Ceresoli3e38e422011-05-11 03:59:54 +0000162/* Network loop restarted */
Luca Ceresolic586ce62011-05-11 03:59:55 +0000163static int NetRestarted;
Luca Ceresoli3e38e422011-05-11 03:59:54 +0000164/* At least one device configured */
Luca Ceresolic586ce62011-05-11 03:59:55 +0000165static int NetDevExists;
wdenk2d966952002-10-31 22:12:35 +0000166
wdenk6e592382004-04-18 17:39:38 +0000167/* XXX in both little & big endian machines 0xFFFF == ntohs(-1) */
Luca Ceresoli3e38e422011-05-11 03:59:54 +0000168/* default is without VLAN */
169ushort NetOurVLAN = 0xFFFF;
170/* ditto */
171ushort NetOurNativeVLAN = 0xFFFF;
wdenka3d991b2004-04-15 21:48:45 +0000172
Luca Ceresoli3e38e422011-05-11 03:59:54 +0000173/* Boot File name */
174char BootFile[128];
wdenk2d966952002-10-31 22:12:35 +0000175
Jon Loeliger643d1ab2007-07-09 17:45:14 -0500176#if defined(CONFIG_CMD_SNTP)
Luca Ceresoli3e38e422011-05-11 03:59:54 +0000177/* NTP server IP address */
178IPaddr_t NetNtpServerIP;
179/* offset time from UTC */
Luca Ceresolic586ce62011-05-11 03:59:55 +0000180int NetTimeOffset;
wdenkea287de2005-04-01 00:25:43 +0000181#endif
182
Kim Phillips06370592012-10-29 13:34:33 +0000183static uchar PktBuf[(PKTBUFSRX+1) * PKTSIZE_ALIGN + PKTALIGN];
wdenk2d966952002-10-31 22:12:35 +0000184
Luca Ceresoli3e38e422011-05-11 03:59:54 +0000185/* Receive packet */
Joe Hershbergerdb288a92012-05-15 08:59:04 +0000186uchar *NetRxPackets[PKTBUFSRX];
wdenk2d966952002-10-31 22:12:35 +0000187
Joe Hershbergerece223b2012-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 Glass39bccd22011-10-26 14:18:38 +0000192#ifdef CONFIG_CMD_TFTPPUT
Joe Hershbergerece223b2012-05-23 07:59:15 +0000193/* Current ICMP rx handler */
194static rxhand_icmp_f *packet_icmp_handler;
Simon Glass39bccd22011-10-26 14:18:38 +0000195#endif
Luca Ceresoli3e38e422011-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 Hershbergerdb288a92012-05-15 08:59:04 +0000203uchar *NetTxPacket;
wdenk2d966952002-10-31 22:12:35 +0000204
Simon Glasse4bf0c52011-10-24 18:00:02 +0000205static int net_check_prereq(enum proto_t protocol);
wdenk2d966952002-10-31 22:12:35 +0000206
Remy Bohmer67b96e82009-10-28 22:13:39 +0100207static int NetTryCount;
208
wdenk2d966952002-10-31 22:12:35 +0000209/**********************************************************************/
wdenk73a8b272003-06-05 19:27:42 +0000210
Simon Glasse4a3d572011-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{
Joe Hershbergerec8a2522012-12-11 22:16:22 -0600217#if defined(CONFIG_CMD_NFS)
Simon Glasse4a3d572011-10-27 06:24:32 +0000218 const char *s = getenv("autoload");
219
Joe Hershbergerec8a2522012-12-11 22:16:22 -0600220 if (s != NULL && strcmp(s, "NFS") == 0) {
221 /*
222 * Use NFS to load the bootfile.
223 */
224 NfsStart();
225 return;
226 }
Simon Glasse4a3d572011-10-27 06:24:32 +0000227#endif
Joe Hershbergerec8a2522012-12-11 22:16:22 -0600228 if (getenv_yesno("autoload") == 0) {
229 /*
230 * Just use BOOTP/RARP to configure system;
231 * Do not use TFTP to load the bootfile.
232 */
233 net_set_state(NETLOOP_SUCCESS);
234 return;
Simon Glasse4a3d572011-10-27 06:24:32 +0000235 }
236 TftpStart(TFTPGET);
237}
238
Joe Hershbergercb1c9912012-05-23 07:59:21 +0000239static void NetInitLoop(void)
Heiko Schocher2f70c492009-02-10 09:38:52 +0100240{
Luca Ceresolic586ce62011-05-11 03:59:55 +0000241 static int env_changed_id;
Luca Ceresoli4f63acd2011-05-11 03:59:56 +0000242 int env_id = get_env_id();
Heiko Schocher2f70c492009-02-10 09:38:52 +0100243
244 /* update only when the environment has changed */
Michael Zaidman3c172c42009-04-04 01:43:00 +0300245 if (env_changed_id != env_id) {
Enric Balletbo i Serra23a70bf2011-05-31 21:01:47 +0000246 NetOurIP = getenv_IPaddr("ipaddr");
Luca Ceresoli4f63acd2011-05-11 03:59:56 +0000247 NetOurGatewayIP = getenv_IPaddr("gatewayip");
248 NetOurSubnetMask = getenv_IPaddr("netmask");
249 NetServerIP = getenv_IPaddr("serverip");
Heiko Schocher2f70c492009-02-10 09:38:52 +0100250 NetOurNativeVLAN = getenv_VLAN("nvlan");
Michael Zaidman3c172c42009-04-04 01:43:00 +0300251 NetOurVLAN = getenv_VLAN("vlan");
Robin Getz1a32bf42009-07-20 14:53:54 -0400252#if defined(CONFIG_CMD_DNS)
253 NetOurDNSIP = getenv_IPaddr("dnsip");
254#endif
Michael Zaidman3c172c42009-04-04 01:43:00 +0300255 env_changed_id = env_id;
Heiko Schocher2f70c492009-02-10 09:38:52 +0100256 }
Joe Hershbergera03d6382012-07-23 13:11:16 +0000257 memcpy(NetOurEther, eth_get_dev()->enetaddr, 6);
Michael Zaidman3c172c42009-04-04 01:43:00 +0300258
Heiko Schocherda954272009-04-28 08:36:11 +0200259 return;
Heiko Schocher2f70c492009-02-10 09:38:52 +0100260}
261
Joe Hershbergerece223b2012-05-23 07:59:15 +0000262static void net_clear_handlers(void)
263{
264 net_set_udp_handler(NULL);
265 net_set_arp_handler(NULL);
266 NetSetTimeout(0, NULL);
267}
268
269static void net_cleanup_loop(void)
270{
271 net_clear_handlers();
272}
273
Joe Hershberger46c495d2012-05-23 07:59:22 +0000274void net_init(void)
275{
276 static int first_call = 1;
277
278 if (first_call) {
279 /*
280 * Setup packet buffers, aligned correctly.
281 */
282 int i;
283
284 NetTxPacket = &PktBuf[0] + (PKTALIGN - 1);
285 NetTxPacket -= (ulong)NetTxPacket % PKTALIGN;
286 for (i = 0; i < PKTBUFSRX; i++)
287 NetRxPackets[i] = NetTxPacket + (i + 1) * PKTSIZE_ALIGN;
288
289 ArpInit();
290 net_clear_handlers();
291
292 /* Only need to setup buffer pointers once. */
293 first_call = 0;
294 }
295
296 NetInitLoop();
297}
298
wdenk73a8b272003-06-05 19:27:42 +0000299/**********************************************************************/
wdenk2d966952002-10-31 22:12:35 +0000300/*
301 * Main network processing loop.
302 */
303
Simon Glasse4bf0c52011-10-24 18:00:02 +0000304int NetLoop(enum proto_t protocol)
wdenk2d966952002-10-31 22:12:35 +0000305{
wdenk2d966952002-10-31 22:12:35 +0000306 bd_t *bd = gd->bd;
Simon Glass4793ee62011-10-24 18:00:01 +0000307 int ret = -1;
wdenk2d966952002-10-31 22:12:35 +0000308
wdenk2d966952002-10-31 22:12:35 +0000309 NetRestarted = 0;
310 NetDevExists = 0;
Remy Bohmer67b96e82009-10-28 22:13:39 +0100311 NetTryCount = 1;
Joe Hershberger4ef8d532012-05-23 08:01:04 +0000312 debug_cond(DEBUG_INT_STATE, "--- NetLoop Entry\n");
wdenk73a8b272003-06-05 19:27:42 +0000313
Simon Glass573f14f2011-12-10 11:08:06 +0000314 bootstage_mark_name(BOOTSTAGE_ID_ETH_START, "eth_start");
Joe Hershberger46c495d2012-05-23 07:59:22 +0000315 net_init();
Joe Hershbergerf8be7d62012-08-03 10:59:08 +0000316 if (eth_is_on_demand_init() || protocol != NETCONS) {
wdenkb1bf6f22005-04-03 14:52:59 +0000317 eth_halt();
Joe Hershbergerf8be7d62012-08-03 10:59:08 +0000318 eth_set_current();
319 if (eth_init(bd) < 0) {
320 eth_halt();
321 return -1;
322 }
323 } else
324 eth_init_state_only(bd);
wdenk2d966952002-10-31 22:12:35 +0000325
326restart:
Joe Hershberger22f6e992012-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 Hershberger4ef8d532012-05-23 08:01:04 +0000334 debug_cond(DEBUG_INT_STATE, "--- NetLoop Init\n");
Joe Hershbergercb1c9912012-05-23 07:59:21 +0000335 NetInitLoop();
wdenk2d966952002-10-31 22:12:35 +0000336
Luca Ceresoli4f63acd2011-05-11 03:59:56 +0000337 switch (net_check_prereq(protocol)) {
wdenk2d966952002-10-31 22:12:35 +0000338 case 1:
339 /* network not configured */
wdenkb1bf6f22005-04-03 14:52:59 +0000340 eth_halt();
Luca Ceresoli92895de2011-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 Glasse4bf0c52011-10-24 18:00:02 +0000349 NetBootFileXferSize = 0;
wdenk2d966952002-10-31 22:12:35 +0000350 switch (protocol) {
Simon Glasse4bf0c52011-10-24 18:00:02 +0000351 case TFTPGET:
Simon Glass1fb7cd42011-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 Glasse4bf0c52011-10-24 18:00:02 +0000356 TftpStart(protocol);
wdenk2d966952002-10-31 22:12:35 +0000357 break;
Luca Ceresoli7a83af02011-05-17 00:03:40 +0000358#ifdef CONFIG_CMD_TFTPSRV
359 case TFTPSRV:
360 TftpStartServer();
361 break;
362#endif
Jon Loeliger643d1ab2007-07-09 17:45:14 -0500363#if defined(CONFIG_CMD_DHCP)
wdenk2d966952002-10-31 22:12:35 +0000364 case DHCP:
wdenkd407bf52004-10-11 22:51:13 +0000365 BootpTry = 0;
Michael Zaidman09133f82009-07-14 23:37:12 +0300366 NetOurIP = 0;
wdenk2d966952002-10-31 22:12:35 +0000367 DhcpRequest(); /* Basically same as BOOTP */
368 break;
Jon Loeliger610f2e92007-07-10 11:05:02 -0500369#endif
wdenk2d966952002-10-31 22:12:35 +0000370
371 case BOOTP:
372 BootpTry = 0;
Michael Zaidman09133f82009-07-14 23:37:12 +0300373 NetOurIP = 0;
Luca Ceresoli4f63acd2011-05-11 03:59:56 +0000374 BootpRequest();
wdenk2d966952002-10-31 22:12:35 +0000375 break;
376
Peter Tyserbf6cb242010-09-30 11:25:48 -0500377#if defined(CONFIG_CMD_RARP)
wdenk2d966952002-10-31 22:12:35 +0000378 case RARP:
379 RarpTry = 0;
Michael Zaidman09133f82009-07-14 23:37:12 +0300380 NetOurIP = 0;
Luca Ceresoli4f63acd2011-05-11 03:59:56 +0000381 RarpRequest();
wdenk2d966952002-10-31 22:12:35 +0000382 break;
Peter Tyserbf6cb242010-09-30 11:25:48 -0500383#endif
Jon Loeliger643d1ab2007-07-09 17:45:14 -0500384#if defined(CONFIG_CMD_PING)
wdenk73a8b272003-06-05 19:27:42 +0000385 case PING:
Joe Hershbergera36b12f2012-05-23 07:58:02 +0000386 ping_start();
wdenk73a8b272003-06-05 19:27:42 +0000387 break;
388#endif
Jon Loeliger643d1ab2007-07-09 17:45:14 -0500389#if defined(CONFIG_CMD_NFS)
wdenkcbd8a352004-02-24 02:00:03 +0000390 case NFS:
391 NfsStart();
392 break;
393#endif
Jon Loeliger643d1ab2007-07-09 17:45:14 -0500394#if defined(CONFIG_CMD_CDP)
wdenka3d991b2004-04-15 21:48:45 +0000395 case CDP:
396 CDPStart();
397 break;
398#endif
wdenk68ceb292004-08-02 21:11:11 +0000399#ifdef CONFIG_NETCONSOLE
400 case NETCONS:
401 NcStart();
402 break;
403#endif
Jon Loeliger643d1ab2007-07-09 17:45:14 -0500404#if defined(CONFIG_CMD_SNTP)
wdenkea287de2005-04-01 00:25:43 +0000405 case SNTP:
406 SntpStart();
407 break;
408#endif
Robin Getz1a32bf42009-07-20 14:53:54 -0400409#if defined(CONFIG_CMD_DNS)
410 case DNS:
411 DnsStart();
412 break;
413#endif
Joe Hershbergerd22c3382012-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 Loeliger643d1ab2007-07-09 17:45:14 -0500426#if defined(CONFIG_MII) || defined(CONFIG_CMD_MII)
Luca Ceresoli3e38e422011-05-11 03:59:54 +0000427#if defined(CONFIG_SYS_FAULT_ECHO_LINK_DOWN) && \
428 defined(CONFIG_STATUS_LED) && \
429 defined(STATUS_LED_RED)
wdenkfc3e2162003-10-08 22:33:00 +0000430 /*
wdenk42d1f032003-10-15 23:53:47 +0000431 * Echo the inverted link state to the fault LED.
wdenkfc3e2162003-10-08 22:33:00 +0000432 */
Luca Ceresolid3c65b02011-05-04 02:40:43 +0000433 if (miiphy_link(eth_get_dev()->name, CONFIG_SYS_FAULT_MII_ADDR))
Luca Ceresoli4f63acd2011-05-11 03:59:56 +0000434 status_led_set(STATUS_LED_RED, STATUS_LED_OFF);
Luca Ceresolid3c65b02011-05-04 02:40:43 +0000435 else
Luca Ceresoli4f63acd2011-05-11 03:59:56 +0000436 status_led_set(STATUS_LED_RED, STATUS_LED_ON);
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200437#endif /* CONFIG_SYS_FAULT_ECHO_LINK_DOWN, ... */
wdenkfc3e2162003-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 Hershberger22f6e992012-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 Hershberger48522bb2012-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 Liakhovetski40cb90e2008-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 Hershbergere94070c2012-05-23 07:59:24 +0000459 /* cancel any ARP that may not have completed */
460 NetArpWaitPacketIP = 0;
461
Joe Hershbergerece223b2012-05-23 07:59:15 +0000462 net_cleanup_loop();
wdenk8bde7f72003-06-27 21:31:46 +0000463 eth_halt();
Joe Hershbergerf8be7d62012-08-03 10:59:08 +0000464 /* Invalidate the last protocol */
465 eth_set_last_protocol(BOOTP);
466
Luca Ceresoli4f63acd2011-05-11 03:59:56 +0000467 puts("\nAbort\n");
Joe Hershberger4ef8d532012-05-23 08:01:04 +0000468 /* include a debug print as well incase the debug
469 messages are directed to stderr */
470 debug_cond(DEBUG_INT_STATE, "--- NetLoop Abort!\n");
Simon Glass4793ee62011-10-24 18:00:01 +0000471 goto done;
wdenk2d966952002-10-31 22:12:35 +0000472 }
473
wdenk73a8b272003-06-05 19:27:42 +0000474 ArpTimeoutCheck();
wdenk2d966952002-10-31 22:12:35 +0000475
476 /*
477 * Check for a timeout, and run the timeout handler
478 * if we have one.
479 */
wdenke0ac62d2003-08-17 18:55:18 +0000480 if (timeHandler && ((get_timer(0) - timeStart) > timeDelta)) {
wdenk2d966952002-10-31 22:12:35 +0000481 thand_f *x;
482
Jon Loeliger643d1ab2007-07-09 17:45:14 -0500483#if defined(CONFIG_MII) || defined(CONFIG_CMD_MII)
Luca Ceresoli4f63acd2011-05-11 03:59:56 +0000484#if defined(CONFIG_SYS_FAULT_ECHO_LINK_DOWN) && \
485 defined(CONFIG_STATUS_LED) && \
486 defined(STATUS_LED_RED)
wdenkfc3e2162003-10-08 22:33:00 +0000487 /*
wdenk42d1f032003-10-15 23:53:47 +0000488 * Echo the inverted link state to the fault LED.
wdenkfc3e2162003-10-08 22:33:00 +0000489 */
Luca Ceresoli4f63acd2011-05-11 03:59:56 +0000490 if (miiphy_link(eth_get_dev()->name,
Luca Ceresoli3e38e422011-05-11 03:59:54 +0000491 CONFIG_SYS_FAULT_MII_ADDR)) {
Luca Ceresoli4f63acd2011-05-11 03:59:56 +0000492 status_led_set(STATUS_LED_RED, STATUS_LED_OFF);
wdenkfc3e2162003-10-08 22:33:00 +0000493 } else {
Luca Ceresoli4f63acd2011-05-11 03:59:56 +0000494 status_led_set(STATUS_LED_RED, STATUS_LED_ON);
wdenkfc3e2162003-10-08 22:33:00 +0000495 }
Luca Ceresoli4f63acd2011-05-11 03:59:56 +0000496#endif /* CONFIG_SYS_FAULT_ECHO_LINK_DOWN, ... */
wdenkfc3e2162003-10-08 22:33:00 +0000497#endif /* CONFIG_MII, ... */
Joe Hershberger4ef8d532012-05-23 08:01:04 +0000498 debug_cond(DEBUG_INT_STATE, "--- NetLoop timeout\n");
wdenk2d966952002-10-31 22:12:35 +0000499 x = timeHandler;
500 timeHandler = (thand_f *)0;
501 (*x)();
502 }
503
504
Joe Hershberger22f6e992012-05-23 07:59:14 +0000505 switch (net_state) {
wdenk2d966952002-10-31 22:12:35 +0000506
507 case NETLOOP_RESTART:
wdenk2d966952002-10-31 22:12:35 +0000508 NetRestarted = 1;
wdenk2d966952002-10-31 22:12:35 +0000509 goto restart;
510
511 case NETLOOP_SUCCESS:
Joe Hershbergerece223b2012-05-23 07:59:15 +0000512 net_cleanup_loop();
wdenk2d966952002-10-31 22:12:35 +0000513 if (NetBootFileXferSize > 0) {
Wolfgang Denkf34024d2007-09-12 00:48:57 +0200514 char buf[20];
wdenk2d966952002-10-31 22:12:35 +0000515 printf("Bytes transferred = %ld (%lx hex)\n",
516 NetBootFileXferSize,
517 NetBootFileXferSize);
Wolfgang Denkf34024d2007-09-12 00:48:57 +0200518 sprintf(buf, "%lX", NetBootFileXferSize);
wdenk2d966952002-10-31 22:12:35 +0000519 setenv("filesize", buf);
wdenka3d991b2004-04-15 21:48:45 +0000520
521 sprintf(buf, "%lX", (unsigned long)load_addr);
522 setenv("fileaddr", buf);
wdenk2d966952002-10-31 22:12:35 +0000523 }
Joe Hershbergerf8be7d62012-08-03 10:59:08 +0000524 if (protocol != NETCONS)
525 eth_halt();
526 else
527 eth_halt_state_only();
528
529 eth_set_last_protocol(protocol);
530
Simon Glass4793ee62011-10-24 18:00:01 +0000531 ret = NetBootFileXferSize;
Joe Hershberger4ef8d532012-05-23 08:01:04 +0000532 debug_cond(DEBUG_INT_STATE, "--- NetLoop Success!\n");
Simon Glass4793ee62011-10-24 18:00:01 +0000533 goto done;
wdenk2d966952002-10-31 22:12:35 +0000534
535 case NETLOOP_FAIL:
Joe Hershbergerece223b2012-05-23 07:59:15 +0000536 net_cleanup_loop();
Joe Hershbergerf8be7d62012-08-03 10:59:08 +0000537 /* Invalidate the last protocol */
538 eth_set_last_protocol(BOOTP);
Joe Hershberger4ef8d532012-05-23 08:01:04 +0000539 debug_cond(DEBUG_INT_STATE, "--- NetLoop Fail!\n");
Simon Glass4793ee62011-10-24 18:00:01 +0000540 goto done;
Joe Hershberger22f6e992012-05-23 07:59:14 +0000541
542 case NETLOOP_CONTINUE:
543 continue;
wdenk2d966952002-10-31 22:12:35 +0000544 }
545 }
Simon Glass4793ee62011-10-24 18:00:01 +0000546
547done:
Simon Glass39bccd22011-10-26 14:18:38 +0000548#ifdef CONFIG_CMD_TFTPPUT
Simon Glass4793ee62011-10-24 18:00:01 +0000549 /* Clear out the handlers */
Joe Hershbergerece223b2012-05-23 07:59:15 +0000550 net_set_udp_handler(NULL);
Simon Glass4793ee62011-10-24 18:00:01 +0000551 net_set_icmp_handler(NULL);
Simon Glass39bccd22011-10-26 14:18:38 +0000552#endif
Simon Glass4793ee62011-10-24 18:00:01 +0000553 return ret;
wdenk2d966952002-10-31 22:12:35 +0000554}
555
556/**********************************************************************/
557
558static void
559startAgainTimeout(void)
560{
Joe Hershberger22f6e992012-05-23 07:59:14 +0000561 net_set_state(NETLOOP_RESTART);
wdenk2d966952002-10-31 22:12:35 +0000562}
563
Luca Ceresoli4f63acd2011-05-11 03:59:56 +0000564void NetStartAgain(void)
wdenk2d966952002-10-31 22:12:35 +0000565{
wdenk6e592382004-04-18 17:39:38 +0000566 char *nretry;
Remy Bohmer67b96e82009-10-28 22:13:39 +0100567 int retry_forever = 0;
568 unsigned long retrycnt = 0;
wdenka3d991b2004-04-15 21:48:45 +0000569
Remy Bohmer67b96e82009-10-28 22:13:39 +0100570 nretry = getenv("netretry");
571 if (nretry) {
572 if (!strcmp(nretry, "yes"))
573 retry_forever = 1;
574 else if (!strcmp(nretry, "no"))
575 retrycnt = 0;
576 else if (!strcmp(nretry, "once"))
577 retrycnt = 1;
578 else
579 retrycnt = simple_strtoul(nretry, NULL, 0);
580 } else
581 retry_forever = 1;
582
583 if ((!retry_forever) && (NetTryCount >= retrycnt)) {
584 eth_halt();
Joe Hershberger22f6e992012-05-23 07:59:14 +0000585 net_set_state(NETLOOP_FAIL);
wdenka3d991b2004-04-15 21:48:45 +0000586 return;
587 }
Remy Bohmer67b96e82009-10-28 22:13:39 +0100588
589 NetTryCount++;
590
Luca Ceresoli4f63acd2011-05-11 03:59:56 +0000591 eth_halt();
Matthias Fuchs8b0c5c12007-12-27 16:58:41 +0100592#if !defined(CONFIG_NET_DO_NOT_TRY_ANOTHER)
Luca Ceresoli4f63acd2011-05-11 03:59:56 +0000593 eth_try_another(!NetRestarted);
Matthias Fuchs8b0c5c12007-12-27 16:58:41 +0100594#endif
Luca Ceresoli4f63acd2011-05-11 03:59:56 +0000595 eth_init(gd->bd);
wdenk6e592382004-04-18 17:39:38 +0000596 if (NetRestartWrap) {
wdenk2d966952002-10-31 22:12:35 +0000597 NetRestartWrap = 0;
Remy Bohmer67b96e82009-10-28 22:13:39 +0100598 if (NetDevExists) {
Luca Ceresoli4f63acd2011-05-11 03:59:56 +0000599 NetSetTimeout(10000UL, startAgainTimeout);
Joe Hershbergerece223b2012-05-23 07:59:15 +0000600 net_set_udp_handler(NULL);
wdenk6e592382004-04-18 17:39:38 +0000601 } else {
Joe Hershberger22f6e992012-05-23 07:59:14 +0000602 net_set_state(NETLOOP_FAIL);
wdenk2d966952002-10-31 22:12:35 +0000603 }
wdenk6e592382004-04-18 17:39:38 +0000604 } else {
Joe Hershberger22f6e992012-05-23 07:59:14 +0000605 net_set_state(NETLOOP_RESTART);
wdenk2d966952002-10-31 22:12:35 +0000606 }
wdenk2d966952002-10-31 22:12:35 +0000607}
608
609/**********************************************************************/
610/*
611 * Miscelaneous bits.
612 */
613
Joe Hershbergerece223b2012-05-23 07:59:15 +0000614static void dummy_handler(uchar *pkt, unsigned dport,
615 IPaddr_t sip, unsigned sport,
616 unsigned len)
Joe Hershbergerd280d3f2012-05-23 07:58:01 +0000617{
Joe Hershbergerd280d3f2012-05-23 07:58:01 +0000618}
619
Joe Hershbergerece223b2012-05-23 07:59:15 +0000620rxhand_f *net_get_udp_handler(void)
wdenk2d966952002-10-31 22:12:35 +0000621{
Joe Hershbergerece223b2012-05-23 07:59:15 +0000622 return udp_packet_handler;
623}
624
625void net_set_udp_handler(rxhand_f *f)
626{
Joe Hershberger4ef8d532012-05-23 08:01:04 +0000627 debug_cond(DEBUG_INT_STATE, "--- NetLoop UDP handler set (%p)\n", f);
Joe Hershbergerece223b2012-05-23 07:59:15 +0000628 if (f == NULL)
629 udp_packet_handler = dummy_handler;
630 else
631 udp_packet_handler = f;
632}
633
634rxhand_f *net_get_arp_handler(void)
635{
636 return arp_packet_handler;
637}
638
639void net_set_arp_handler(rxhand_f *f)
640{
Joe Hershberger4ef8d532012-05-23 08:01:04 +0000641 debug_cond(DEBUG_INT_STATE, "--- NetLoop ARP handler set (%p)\n", f);
Joe Hershbergerece223b2012-05-23 07:59:15 +0000642 if (f == NULL)
643 arp_packet_handler = dummy_handler;
644 else
645 arp_packet_handler = f;
wdenk2d966952002-10-31 22:12:35 +0000646}
647
Simon Glass39bccd22011-10-26 14:18:38 +0000648#ifdef CONFIG_CMD_TFTPPUT
Simon Glass4793ee62011-10-24 18:00:01 +0000649void net_set_icmp_handler(rxhand_icmp_f *f)
650{
651 packet_icmp_handler = f;
652}
Simon Glass39bccd22011-10-26 14:18:38 +0000653#endif
wdenk2d966952002-10-31 22:12:35 +0000654
655void
Luca Ceresoli6b147d12011-05-04 02:40:44 +0000656NetSetTimeout(ulong iv, thand_f *f)
wdenk2d966952002-10-31 22:12:35 +0000657{
658 if (iv == 0) {
Joe Hershberger4ef8d532012-05-23 08:01:04 +0000659 debug_cond(DEBUG_INT_STATE,
660 "--- NetLoop timeout handler cancelled\n");
wdenk2d966952002-10-31 22:12:35 +0000661 timeHandler = (thand_f *)0;
662 } else {
Joe Hershberger4ef8d532012-05-23 08:01:04 +0000663 debug_cond(DEBUG_INT_STATE,
664 "--- NetLoop timeout handler set (%p)\n", f);
wdenk2d966952002-10-31 22:12:35 +0000665 timeHandler = f;
wdenke0ac62d2003-08-17 18:55:18 +0000666 timeStart = get_timer(0);
Tetsuyuki Kobayashi1389f982012-06-25 02:37:27 +0000667 timeDelta = iv * CONFIG_SYS_HZ / 1000;
wdenk2d966952002-10-31 22:12:35 +0000668 }
669}
670
Joe Hershberger206d07f2012-05-23 07:58:10 +0000671int NetSendUDPPacket(uchar *ether, IPaddr_t dest, int dport, int sport,
672 int payload_len)
wdenk73a8b272003-06-05 19:27:42 +0000673{
wdenka3d991b2004-04-15 21:48:45 +0000674 uchar *pkt;
Joe Hershberger92146372012-05-23 07:59:08 +0000675 int eth_hdr_size;
676 int pkt_hdr_size;
wdenka3d991b2004-04-15 21:48:45 +0000677
Joe Hershberger46c495d2012-05-23 07:59:22 +0000678 /* make sure the NetTxPacket is initialized (NetInit() was called) */
679 assert(NetTxPacket != NULL);
680 if (NetTxPacket == NULL)
681 return -1;
682
wdenk73a8b272003-06-05 19:27:42 +0000683 /* convert to new style broadcast */
684 if (dest == 0)
685 dest = 0xFFFFFFFF;
wdenk2d966952002-10-31 22:12:35 +0000686
wdenk73a8b272003-06-05 19:27:42 +0000687 /* if broadcast, make the ether address a broadcast and don't do ARP */
688 if (dest == 0xFFFFFFFF)
689 ether = NetBcastAddr;
690
Joe Hershbergere94070c2012-05-23 07:59:24 +0000691 pkt = (uchar *)NetTxPacket;
wdenk73a8b272003-06-05 19:27:42 +0000692
Joe Hershberger92146372012-05-23 07:59:08 +0000693 eth_hdr_size = NetSetEther(pkt, ether, PROT_IP);
694 pkt += eth_hdr_size;
695 net_set_udp_header(pkt, dest, dport, sport, payload_len);
696 pkt_hdr_size = eth_hdr_size + IP_UDP_HDR_SIZE;
Robin Getz0ebf04c2009-07-23 03:01:03 -0400697
Joe Hershbergere94070c2012-05-23 07:59:24 +0000698 /* if MAC address was not discovered yet, do an ARP request */
699 if (memcmp(ether, NetEtherNullAddr, 6) == 0) {
Joe Hershberger4ef8d532012-05-23 08:01:04 +0000700 debug_cond(DEBUG_DEV_PKT, "sending ARP for %pI4\n", &dest);
Joe Hershberger92146372012-05-23 07:59:08 +0000701
702 /* save the ip and eth addr for the packet to send after arp */
wdenk73a8b272003-06-05 19:27:42 +0000703 NetArpWaitPacketIP = dest;
704 NetArpWaitPacketMAC = ether;
wdenka3d991b2004-04-15 21:48:45 +0000705
wdenk73a8b272003-06-05 19:27:42 +0000706 /* size of the waiting packet */
Joe Hershberger92146372012-05-23 07:59:08 +0000707 NetArpWaitTxPacketSize = pkt_hdr_size + payload_len;
wdenk73a8b272003-06-05 19:27:42 +0000708
709 /* and do the ARP request */
710 NetArpWaitTry = 1;
711 NetArpWaitTimerStart = get_timer(0);
712 ArpRequest();
713 return 1; /* waiting */
Joe Hershberger92146372012-05-23 07:59:08 +0000714 } else {
Joe Hershberger4ef8d532012-05-23 08:01:04 +0000715 debug_cond(DEBUG_DEV_PKT, "sending UDP to %pI4/%pM\n",
716 &dest, ether);
Joe Hershbergeradf5d932012-05-23 07:59:13 +0000717 NetSendPacket(NetTxPacket, pkt_hdr_size + payload_len);
Joe Hershberger92146372012-05-23 07:59:08 +0000718 return 0; /* transmitted */
wdenk73a8b272003-06-05 19:27:42 +0000719 }
wdenk73a8b272003-06-05 19:27:42 +0000720}
721
Alessandro Rubini5cfaa4e2009-08-07 13:58:56 +0200722#ifdef CONFIG_IP_DEFRAG
723/*
724 * This function collects fragments in a single packet, according
725 * to the algorithm in RFC815. It returns NULL or the pointer to
726 * a complete packet, in static storage
727 */
728#ifndef CONFIG_NET_MAXDEFRAG
729#define CONFIG_NET_MAXDEFRAG 16384
730#endif
731/*
732 * MAXDEFRAG, above, is chosen in the config file and is real data
733 * so we need to add the NFS overhead, which is more than TFTP.
734 * To use sizeof in the internal unnamed structures, we need a real
735 * instance (can't do "sizeof(struct rpc_t.u.reply))", unfortunately).
736 * The compiler doesn't complain nor allocates the actual structure
737 */
738static struct rpc_t rpc_specimen;
739#define IP_PKTSIZE (CONFIG_NET_MAXDEFRAG + sizeof(rpc_specimen.u.reply))
740
Joe Hershbergerc5c59df2012-05-23 07:58:05 +0000741#define IP_MAXUDP (IP_PKTSIZE - IP_HDR_SIZE)
Alessandro Rubini5cfaa4e2009-08-07 13:58:56 +0200742
743/*
744 * this is the packet being assembled, either data or frag control.
745 * Fragments go by 8 bytes, so this union must be 8 bytes long
746 */
747struct hole {
748 /* first_byte is address of this structure */
749 u16 last_byte; /* last byte in this hole + 1 (begin of next hole) */
750 u16 next_hole; /* index of next (in 8-b blocks), 0 == none */
751 u16 prev_hole; /* index of prev, 0 == none */
752 u16 unused;
753};
754
Joe Hershberger594c26f2012-05-23 07:58:04 +0000755static struct ip_udp_hdr *__NetDefragment(struct ip_udp_hdr *ip, int *lenp)
Alessandro Rubini5cfaa4e2009-08-07 13:58:56 +0200756{
Joe Hershberger48522bb2012-05-15 08:59:08 +0000757 static uchar pkt_buff[IP_PKTSIZE] __aligned(PKTALIGN);
Alessandro Rubini5cfaa4e2009-08-07 13:58:56 +0200758 static u16 first_hole, total_len;
759 struct hole *payload, *thisfrag, *h, *newh;
Joe Hershberger594c26f2012-05-23 07:58:04 +0000760 struct ip_udp_hdr *localip = (struct ip_udp_hdr *)pkt_buff;
Alessandro Rubini5cfaa4e2009-08-07 13:58:56 +0200761 uchar *indata = (uchar *)ip;
762 int offset8, start, len, done = 0;
763 u16 ip_off = ntohs(ip->ip_off);
764
765 /* payload starts after IP header, this fragment is in there */
Joe Hershbergerc5c59df2012-05-23 07:58:05 +0000766 payload = (struct hole *)(pkt_buff + IP_HDR_SIZE);
Alessandro Rubini5cfaa4e2009-08-07 13:58:56 +0200767 offset8 = (ip_off & IP_OFFS);
768 thisfrag = payload + offset8;
769 start = offset8 * 8;
Joe Hershbergerc5c59df2012-05-23 07:58:05 +0000770 len = ntohs(ip->ip_len) - IP_HDR_SIZE;
Alessandro Rubini5cfaa4e2009-08-07 13:58:56 +0200771
772 if (start + len > IP_MAXUDP) /* fragment extends too far */
773 return NULL;
774
775 if (!total_len || localip->ip_id != ip->ip_id) {
776 /* new (or different) packet, reset structs */
777 total_len = 0xffff;
778 payload[0].last_byte = ~0;
779 payload[0].next_hole = 0;
780 payload[0].prev_hole = 0;
781 first_hole = 0;
782 /* any IP header will work, copy the first we received */
Joe Hershbergerc5c59df2012-05-23 07:58:05 +0000783 memcpy(localip, ip, IP_HDR_SIZE);
Alessandro Rubini5cfaa4e2009-08-07 13:58:56 +0200784 }
785
786 /*
787 * What follows is the reassembly algorithm. We use the payload
788 * array as a linked list of hole descriptors, as each hole starts
789 * at a multiple of 8 bytes. However, last byte can be whatever value,
790 * so it is represented as byte count, not as 8-byte blocks.
791 */
792
793 h = payload + first_hole;
794 while (h->last_byte < start) {
795 if (!h->next_hole) {
796 /* no hole that far away */
797 return NULL;
798 }
799 h = payload + h->next_hole;
800 }
801
Fillod Stephanee397e592010-06-11 19:26:43 +0200802 /* last fragment may be 1..7 bytes, the "+7" forces acceptance */
803 if (offset8 + ((len + 7) / 8) <= h - payload) {
Alessandro Rubini5cfaa4e2009-08-07 13:58:56 +0200804 /* no overlap with holes (dup fragment?) */
805 return NULL;
806 }
807
808 if (!(ip_off & IP_FLAGS_MFRAG)) {
809 /* no more fragmentss: truncate this (last) hole */
810 total_len = start + len;
811 h->last_byte = start + len;
812 }
813
814 /*
815 * There is some overlap: fix the hole list. This code doesn't
816 * deal with a fragment that overlaps with two different holes
817 * (thus being a superset of a previously-received fragment).
818 */
819
Luca Ceresoli4f63acd2011-05-11 03:59:56 +0000820 if ((h >= thisfrag) && (h->last_byte <= start + len)) {
Alessandro Rubini5cfaa4e2009-08-07 13:58:56 +0200821 /* complete overlap with hole: remove hole */
822 if (!h->prev_hole && !h->next_hole) {
823 /* last remaining hole */
824 done = 1;
825 } else if (!h->prev_hole) {
826 /* first hole */
827 first_hole = h->next_hole;
828 payload[h->next_hole].prev_hole = 0;
829 } else if (!h->next_hole) {
830 /* last hole */
831 payload[h->prev_hole].next_hole = 0;
832 } else {
833 /* in the middle of the list */
834 payload[h->next_hole].prev_hole = h->prev_hole;
835 payload[h->prev_hole].next_hole = h->next_hole;
836 }
837
838 } else if (h->last_byte <= start + len) {
839 /* overlaps with final part of the hole: shorten this hole */
840 h->last_byte = start;
841
842 } else if (h >= thisfrag) {
843 /* overlaps with initial part of the hole: move this hole */
844 newh = thisfrag + (len / 8);
845 *newh = *h;
846 h = newh;
847 if (h->next_hole)
848 payload[h->next_hole].prev_hole = (h - payload);
849 if (h->prev_hole)
850 payload[h->prev_hole].next_hole = (h - payload);
851 else
852 first_hole = (h - payload);
853
854 } else {
855 /* fragment sits in the middle: split the hole */
856 newh = thisfrag + (len / 8);
857 *newh = *h;
858 h->last_byte = start;
859 h->next_hole = (newh - payload);
860 newh->prev_hole = (h - payload);
861 if (newh->next_hole)
862 payload[newh->next_hole].prev_hole = (newh - payload);
863 }
864
865 /* finally copy this fragment and possibly return whole packet */
Joe Hershbergerc5c59df2012-05-23 07:58:05 +0000866 memcpy((uchar *)thisfrag, indata + IP_HDR_SIZE, len);
Alessandro Rubini5cfaa4e2009-08-07 13:58:56 +0200867 if (!done)
868 return NULL;
869
870 localip->ip_len = htons(total_len);
Joe Hershbergerc5c59df2012-05-23 07:58:05 +0000871 *lenp = total_len + IP_HDR_SIZE;
Alessandro Rubini5cfaa4e2009-08-07 13:58:56 +0200872 return localip;
873}
874
Joe Hershberger594c26f2012-05-23 07:58:04 +0000875static inline struct ip_udp_hdr *NetDefragment(struct ip_udp_hdr *ip, int *lenp)
Alessandro Rubini5cfaa4e2009-08-07 13:58:56 +0200876{
877 u16 ip_off = ntohs(ip->ip_off);
878 if (!(ip_off & (IP_OFFS | IP_FLAGS_MFRAG)))
879 return ip; /* not a fragment */
880 return __NetDefragment(ip, lenp);
881}
882
883#else /* !CONFIG_IP_DEFRAG */
884
Joe Hershberger594c26f2012-05-23 07:58:04 +0000885static inline struct ip_udp_hdr *NetDefragment(struct ip_udp_hdr *ip, int *lenp)
Alessandro Rubini5cfaa4e2009-08-07 13:58:56 +0200886{
887 u16 ip_off = ntohs(ip->ip_off);
888 if (!(ip_off & (IP_OFFS | IP_FLAGS_MFRAG)))
889 return ip; /* not a fragment */
890 return NULL;
891}
892#endif
wdenka3d991b2004-04-15 21:48:45 +0000893
Simon Glass8f79bb12011-10-24 18:00:00 +0000894/**
895 * Receive an ICMP packet. We deal with REDIRECT and PING here, and silently
896 * drop others.
897 *
898 * @parma ip IP packet containing the ICMP
899 */
Joe Hershberger594c26f2012-05-23 07:58:04 +0000900static void receive_icmp(struct ip_udp_hdr *ip, int len,
Joe Hershbergercb487f52012-05-23 07:58:06 +0000901 IPaddr_t src_ip, struct ethernet_hdr *et)
Simon Glass8f79bb12011-10-24 18:00:00 +0000902{
Joe Hershbergere0a63072012-05-23 07:58:09 +0000903 struct icmp_hdr *icmph = (struct icmp_hdr *)&ip->udp_src;
Simon Glass8f79bb12011-10-24 18:00:00 +0000904
905 switch (icmph->type) {
906 case ICMP_REDIRECT:
907 if (icmph->code != ICMP_REDIR_HOST)
908 return;
909 printf(" ICMP Host Redirect to %pI4 ",
910 &icmph->un.gateway);
911 break;
Simon Glass8f79bb12011-10-24 18:00:00 +0000912 default:
Joe Hershbergera36b12f2012-05-23 07:58:02 +0000913#if defined(CONFIG_CMD_PING)
914 ping_receive(et, ip, len);
915#endif
Simon Glass39bccd22011-10-26 14:18:38 +0000916#ifdef CONFIG_CMD_TFTPPUT
Simon Glass4793ee62011-10-24 18:00:01 +0000917 if (packet_icmp_handler)
918 packet_icmp_handler(icmph->type, icmph->code,
919 ntohs(ip->udp_dst), src_ip, ntohs(ip->udp_src),
920 icmph->un.data, ntohs(ip->udp_len));
Simon Glass39bccd22011-10-26 14:18:38 +0000921#endif
Simon Glass8f79bb12011-10-24 18:00:00 +0000922 break;
923 }
924}
925
wdenk2d966952002-10-31 22:12:35 +0000926void
Joe Hershbergerdb288a92012-05-15 08:59:04 +0000927NetReceive(uchar *inpkt, int len)
wdenk2d966952002-10-31 22:12:35 +0000928{
Joe Hershbergercb487f52012-05-23 07:58:06 +0000929 struct ethernet_hdr *et;
Joe Hershberger594c26f2012-05-23 07:58:04 +0000930 struct ip_udp_hdr *ip;
Joe Hershberger8d353eb2012-05-23 07:58:12 +0000931 IPaddr_t dst_ip;
Luca Ceresoli03eb1292011-04-18 06:19:50 +0000932 IPaddr_t src_ip;
Joe Hershberger8d353eb2012-05-23 07:58:12 +0000933 int eth_proto;
Jon Loeliger643d1ab2007-07-09 17:45:14 -0500934#if defined(CONFIG_CMD_CDP)
wdenka3d991b2004-04-15 21:48:45 +0000935 int iscdp;
936#endif
937 ushort cti = 0, vlanid = VLAN_NONE, myvlanid, mynvlanid;
wdenk2d966952002-10-31 22:12:35 +0000938
Joe Hershberger4ef8d532012-05-23 08:01:04 +0000939 debug_cond(DEBUG_NET_PKT, "packet received\n");
wdenka3d991b2004-04-15 21:48:45 +0000940
Mike Frysingerd9bec9f2009-07-18 21:04:08 -0400941 NetRxPacket = inpkt;
942 NetRxPacketLen = len;
Joe Hershbergercb487f52012-05-23 07:58:06 +0000943 et = (struct ethernet_hdr *)inpkt;
wdenka3d991b2004-04-15 21:48:45 +0000944
945 /* too small packet? */
946 if (len < ETHER_HDR_SIZE)
947 return;
948
Rafal Jaworowskif85b6072007-12-27 18:19:02 +0100949#ifdef CONFIG_API
950 if (push_packet) {
951 (*push_packet)(inpkt, len);
952 return;
953 }
954#endif
955
Jon Loeliger643d1ab2007-07-09 17:45:14 -0500956#if defined(CONFIG_CMD_CDP)
wdenka3d991b2004-04-15 21:48:45 +0000957 /* keep track if packet is CDP */
Joe Hershberger17351882012-05-23 07:58:00 +0000958 iscdp = is_cdp_packet(et->et_dest);
wdenka3d991b2004-04-15 21:48:45 +0000959#endif
960
961 myvlanid = ntohs(NetOurVLAN);
962 if (myvlanid == (ushort)-1)
963 myvlanid = VLAN_NONE;
964 mynvlanid = ntohs(NetOurNativeVLAN);
965 if (mynvlanid == (ushort)-1)
966 mynvlanid = VLAN_NONE;
wdenk2d966952002-10-31 22:12:35 +0000967
Joe Hershberger8d353eb2012-05-23 07:58:12 +0000968 eth_proto = ntohs(et->et_protlen);
wdenk2d966952002-10-31 22:12:35 +0000969
Joe Hershberger8d353eb2012-05-23 07:58:12 +0000970 if (eth_proto < 1514) {
Joe Hershbergercb487f52012-05-23 07:58:06 +0000971 struct e802_hdr *et802 = (struct e802_hdr *)et;
wdenk2d966952002-10-31 22:12:35 +0000972 /*
Joe Hershbergerda5ebe22012-05-23 07:58:11 +0000973 * Got a 802.2 packet. Check the other protocol field.
974 * XXX VLAN over 802.2+SNAP not implemented!
wdenk2d966952002-10-31 22:12:35 +0000975 */
Joe Hershberger8d353eb2012-05-23 07:58:12 +0000976 eth_proto = ntohs(et802->et_prot);
wdenka3d991b2004-04-15 21:48:45 +0000977
Joe Hershberger594c26f2012-05-23 07:58:04 +0000978 ip = (struct ip_udp_hdr *)(inpkt + E802_HDR_SIZE);
wdenk2d966952002-10-31 22:12:35 +0000979 len -= E802_HDR_SIZE;
wdenka3d991b2004-04-15 21:48:45 +0000980
Joe Hershberger8d353eb2012-05-23 07:58:12 +0000981 } else if (eth_proto != PROT_VLAN) { /* normal packet */
Joe Hershberger594c26f2012-05-23 07:58:04 +0000982 ip = (struct ip_udp_hdr *)(inpkt + ETHER_HDR_SIZE);
wdenk2d966952002-10-31 22:12:35 +0000983 len -= ETHER_HDR_SIZE;
wdenka3d991b2004-04-15 21:48:45 +0000984
985 } else { /* VLAN packet */
Joe Hershbergerc68cca32012-05-23 07:58:07 +0000986 struct vlan_ethernet_hdr *vet =
987 (struct vlan_ethernet_hdr *)et;
wdenka3d991b2004-04-15 21:48:45 +0000988
Joe Hershberger4ef8d532012-05-23 08:01:04 +0000989 debug_cond(DEBUG_NET_PKT, "VLAN packet received\n");
Robin Getz0ebf04c2009-07-23 03:01:03 -0400990
wdenka3d991b2004-04-15 21:48:45 +0000991 /* too small packet? */
992 if (len < VLAN_ETHER_HDR_SIZE)
993 return;
994
995 /* if no VLAN active */
996 if ((ntohs(NetOurVLAN) & VLAN_IDMASK) == VLAN_NONE
Jon Loeliger643d1ab2007-07-09 17:45:14 -0500997#if defined(CONFIG_CMD_CDP)
wdenka3d991b2004-04-15 21:48:45 +0000998 && iscdp == 0
999#endif
1000 )
1001 return;
1002
1003 cti = ntohs(vet->vet_tag);
1004 vlanid = cti & VLAN_IDMASK;
Joe Hershberger8d353eb2012-05-23 07:58:12 +00001005 eth_proto = ntohs(vet->vet_type);
wdenka3d991b2004-04-15 21:48:45 +00001006
Joe Hershberger594c26f2012-05-23 07:58:04 +00001007 ip = (struct ip_udp_hdr *)(inpkt + VLAN_ETHER_HDR_SIZE);
wdenka3d991b2004-04-15 21:48:45 +00001008 len -= VLAN_ETHER_HDR_SIZE;
wdenk2d966952002-10-31 22:12:35 +00001009 }
1010
Joe Hershberger4ef8d532012-05-23 08:01:04 +00001011 debug_cond(DEBUG_NET_PKT, "Receive from protocol 0x%x\n", eth_proto);
wdenk2d966952002-10-31 22:12:35 +00001012
Jon Loeliger643d1ab2007-07-09 17:45:14 -05001013#if defined(CONFIG_CMD_CDP)
wdenka3d991b2004-04-15 21:48:45 +00001014 if (iscdp) {
Joe Hershberger0b4c5ff2012-05-23 07:58:13 +00001015 cdp_receive((uchar *)ip, len);
wdenka3d991b2004-04-15 21:48:45 +00001016 return;
1017 }
1018#endif
1019
1020 if ((myvlanid & VLAN_IDMASK) != VLAN_NONE) {
1021 if (vlanid == VLAN_NONE)
1022 vlanid = (mynvlanid & VLAN_IDMASK);
1023 /* not matched? */
1024 if (vlanid != (myvlanid & VLAN_IDMASK))
1025 return;
1026 }
1027
Joe Hershberger8d353eb2012-05-23 07:58:12 +00001028 switch (eth_proto) {
wdenk2d966952002-10-31 22:12:35 +00001029
1030 case PROT_ARP:
Joe Hershbergerd280d3f2012-05-23 07:58:01 +00001031 ArpReceive(et, ip, len);
wdenk289f9322005-01-12 00:15:14 +00001032 break;
wdenk2d966952002-10-31 22:12:35 +00001033
Peter Tyserbf6cb242010-09-30 11:25:48 -05001034#ifdef CONFIG_CMD_RARP
wdenk2d966952002-10-31 22:12:35 +00001035 case PROT_RARP:
Joe Hershberger8b9c5322012-05-23 07:58:03 +00001036 rarp_receive(ip, len);
wdenk2d966952002-10-31 22:12:35 +00001037 break;
Peter Tyserbf6cb242010-09-30 11:25:48 -05001038#endif
wdenk2d966952002-10-31 22:12:35 +00001039 case PROT_IP:
Joe Hershberger4ef8d532012-05-23 08:01:04 +00001040 debug_cond(DEBUG_NET_PKT, "Got IP\n");
Alessandro Rubini5cfaa4e2009-08-07 13:58:56 +02001041 /* Before we start poking the header, make sure it is there */
Joe Hershberger594c26f2012-05-23 07:58:04 +00001042 if (len < IP_UDP_HDR_SIZE) {
1043 debug("len bad %d < %lu\n", len,
1044 (ulong)IP_UDP_HDR_SIZE);
wdenk2d966952002-10-31 22:12:35 +00001045 return;
1046 }
Alessandro Rubini5cfaa4e2009-08-07 13:58:56 +02001047 /* Check the packet length */
wdenk2d966952002-10-31 22:12:35 +00001048 if (len < ntohs(ip->ip_len)) {
Joe Hershberger4ef8d532012-05-23 08:01:04 +00001049 debug("len bad %d < %d\n", len, ntohs(ip->ip_len));
wdenk2d966952002-10-31 22:12:35 +00001050 return;
1051 }
1052 len = ntohs(ip->ip_len);
Joe Hershberger4ef8d532012-05-23 08:01:04 +00001053 debug_cond(DEBUG_NET_PKT, "len=%d, v=%02x\n",
1054 len, ip->ip_hl_v & 0xff);
Robin Getz0ebf04c2009-07-23 03:01:03 -04001055
Alessandro Rubini5cfaa4e2009-08-07 13:58:56 +02001056 /* Can't deal with anything except IPv4 */
Luca Ceresolid3c65b02011-05-04 02:40:43 +00001057 if ((ip->ip_hl_v & 0xf0) != 0x40)
wdenk2d966952002-10-31 22:12:35 +00001058 return;
Alessandro Rubini5cfaa4e2009-08-07 13:58:56 +02001059 /* Can't deal with IP options (headers != 20 bytes) */
Luca Ceresolid3c65b02011-05-04 02:40:43 +00001060 if ((ip->ip_hl_v & 0x0f) > 0x05)
Remy Bohmer6b52cfe2008-06-03 15:48:17 +02001061 return;
Alessandro Rubini5cfaa4e2009-08-07 13:58:56 +02001062 /* Check the Checksum of the header */
Joe Hershbergerc5c59df2012-05-23 07:58:05 +00001063 if (!NetCksumOk((uchar *)ip, IP_HDR_SIZE / 2)) {
Joe Hershberger4ef8d532012-05-23 08:01:04 +00001064 debug("checksum bad\n");
wdenk2d966952002-10-31 22:12:35 +00001065 return;
1066 }
Alessandro Rubini5cfaa4e2009-08-07 13:58:56 +02001067 /* If it is not for us, ignore it */
Joe Hershberger8d353eb2012-05-23 07:58:12 +00001068 dst_ip = NetReadIP(&ip->ip_dst);
1069 if (NetOurIP && dst_ip != NetOurIP && dst_ip != 0xFFFFFFFF) {
David Updegraff53a5c422007-06-11 10:41:07 -05001070#ifdef CONFIG_MCAST_TFTP
Joe Hershberger8d353eb2012-05-23 07:58:12 +00001071 if (Mcast_addr != dst_ip)
David Updegraff53a5c422007-06-11 10:41:07 -05001072#endif
Luca Ceresolic819abe2011-05-04 02:40:46 +00001073 return;
wdenk2d966952002-10-31 22:12:35 +00001074 }
Luca Ceresoli03eb1292011-04-18 06:19:50 +00001075 /* Read source IP address for later use */
1076 src_ip = NetReadIP(&ip->ip_src);
wdenk2d966952002-10-31 22:12:35 +00001077 /*
Alessandro Rubini5cfaa4e2009-08-07 13:58:56 +02001078 * The function returns the unchanged packet if it's not
1079 * a fragment, and either the complete packet or NULL if
1080 * it is a fragment (if !CONFIG_IP_DEFRAG, it returns NULL)
1081 */
Luca Ceresoliccb9ebe2011-05-04 02:40:47 +00001082 ip = NetDefragment(ip, &len);
1083 if (!ip)
Alessandro Rubini5cfaa4e2009-08-07 13:58:56 +02001084 return;
1085 /*
wdenk2d966952002-10-31 22:12:35 +00001086 * watch for ICMP host redirects
1087 *
wdenk8bde7f72003-06-27 21:31:46 +00001088 * There is no real handler code (yet). We just watch
1089 * for ICMP host redirect messages. In case anybody
1090 * sees these messages: please contact me
1091 * (wd@denx.de), or - even better - send me the
1092 * necessary fixes :-)
wdenk2d966952002-10-31 22:12:35 +00001093 *
wdenk8bde7f72003-06-27 21:31:46 +00001094 * Note: in all cases where I have seen this so far
1095 * it was a problem with the router configuration,
1096 * for instance when a router was configured in the
1097 * BOOTP reply, but the TFTP server was on the same
1098 * subnet. So this is probably a warning that your
1099 * configuration might be wrong. But I'm not really
1100 * sure if there aren't any other situations.
Simon Glass4793ee62011-10-24 18:00:01 +00001101 *
1102 * Simon Glass <sjg@chromium.org>: We get an ICMP when
1103 * we send a tftp packet to a dead connection, or when
1104 * there is no server at the other end.
wdenk2d966952002-10-31 22:12:35 +00001105 */
1106 if (ip->ip_p == IPPROTO_ICMP) {
Simon Glass8f79bb12011-10-24 18:00:00 +00001107 receive_icmp(ip, len, src_ip, et);
1108 return;
wdenk2d966952002-10-31 22:12:35 +00001109 } else if (ip->ip_p != IPPROTO_UDP) { /* Only UDP packets */
1110 return;
1111 }
1112
Joe Hershberger4ef8d532012-05-23 08:01:04 +00001113 debug_cond(DEBUG_DEV_PKT,
1114 "received UDP (to=%pI4, from=%pI4, len=%d)\n",
1115 &dst_ip, &src_ip, len);
1116
Stefan Roese8534bf92005-08-12 20:06:52 +02001117#ifdef CONFIG_UDP_CHECKSUM
1118 if (ip->udp_xsum != 0) {
Wolfgang Denkb2f50802005-08-12 23:43:12 +02001119 ulong xsum;
Stefan Roese8534bf92005-08-12 20:06:52 +02001120 ushort *sumptr;
1121 ushort sumlen;
1122
1123 xsum = ip->ip_p;
1124 xsum += (ntohs(ip->udp_len));
1125 xsum += (ntohl(ip->ip_src) >> 16) & 0x0000ffff;
1126 xsum += (ntohl(ip->ip_src) >> 0) & 0x0000ffff;
1127 xsum += (ntohl(ip->ip_dst) >> 16) & 0x0000ffff;
1128 xsum += (ntohl(ip->ip_dst) >> 0) & 0x0000ffff;
1129
1130 sumlen = ntohs(ip->udp_len);
1131 sumptr = (ushort *) &(ip->udp_src);
1132
1133 while (sumlen > 1) {
Wolfgang Denkb2f50802005-08-12 23:43:12 +02001134 ushort sumdata;
Stefan Roese8534bf92005-08-12 20:06:52 +02001135
1136 sumdata = *sumptr++;
1137 xsum += ntohs(sumdata);
1138 sumlen -= 2;
1139 }
1140 if (sumlen > 0) {
Wolfgang Denkb2f50802005-08-12 23:43:12 +02001141 ushort sumdata;
Stefan Roese8534bf92005-08-12 20:06:52 +02001142
1143 sumdata = *(unsigned char *) sumptr;
Wolfgang Denkb2f50802005-08-12 23:43:12 +02001144 sumdata = (sumdata << 8) & 0xff00;
Stefan Roese8534bf92005-08-12 20:06:52 +02001145 xsum += sumdata;
1146 }
1147 while ((xsum >> 16) != 0) {
Luca Ceresoli3e38e422011-05-11 03:59:54 +00001148 xsum = (xsum & 0x0000ffff) +
1149 ((xsum >> 16) & 0x0000ffff);
Stefan Roese8534bf92005-08-12 20:06:52 +02001150 }
1151 if ((xsum != 0x00000000) && (xsum != 0x0000ffff)) {
Wolfgang Denk9b55a252008-07-11 01:16:00 +02001152 printf(" UDP wrong checksum %08lx %08x\n",
1153 xsum, ntohs(ip->udp_xsum));
Stefan Roese8534bf92005-08-12 20:06:52 +02001154 return;
1155 }
1156 }
1157#endif
1158
David Updegraff53a5c422007-06-11 10:41:07 -05001159
wdenk68ceb292004-08-02 21:11:11 +00001160#ifdef CONFIG_NETCONSOLE
Joe Hershberger594c26f2012-05-23 07:58:04 +00001161 nc_input_packet((uchar *)ip + IP_UDP_HDR_SIZE,
Joe Hershberger8cab08e2012-10-03 09:14:03 +00001162 src_ip,
Joe Hershberger594c26f2012-05-23 07:58:04 +00001163 ntohs(ip->udp_dst),
1164 ntohs(ip->udp_src),
1165 ntohs(ip->udp_len) - UDP_HDR_SIZE);
wdenk68ceb292004-08-02 21:11:11 +00001166#endif
wdenk2d966952002-10-31 22:12:35 +00001167 /*
1168 * IP header OK. Pass the packet to the current handler.
1169 */
Joe Hershbergerece223b2012-05-23 07:59:15 +00001170 (*udp_packet_handler)((uchar *)ip + IP_UDP_HDR_SIZE,
1171 ntohs(ip->udp_dst),
1172 src_ip,
1173 ntohs(ip->udp_src),
1174 ntohs(ip->udp_len) - UDP_HDR_SIZE);
wdenk2d966952002-10-31 22:12:35 +00001175 break;
1176 }
1177}
1178
1179
1180/**********************************************************************/
1181
Simon Glasse4bf0c52011-10-24 18:00:02 +00001182static int net_check_prereq(enum proto_t protocol)
wdenk2d966952002-10-31 22:12:35 +00001183{
1184 switch (protocol) {
wdenk6e592382004-04-18 17:39:38 +00001185 /* Fall through */
Jon Loeliger643d1ab2007-07-09 17:45:14 -05001186#if defined(CONFIG_CMD_PING)
wdenk73a8b272003-06-05 19:27:42 +00001187 case PING:
wdenk6e592382004-04-18 17:39:38 +00001188 if (NetPingIP == 0) {
Luca Ceresoli4f63acd2011-05-11 03:59:56 +00001189 puts("*** ERROR: ping address not given\n");
Luca Ceresoli92895de2011-05-04 02:40:45 +00001190 return 1;
wdenk6e592382004-04-18 17:39:38 +00001191 }
1192 goto common;
wdenk73a8b272003-06-05 19:27:42 +00001193#endif
Jon Loeliger643d1ab2007-07-09 17:45:14 -05001194#if defined(CONFIG_CMD_SNTP)
wdenkea287de2005-04-01 00:25:43 +00001195 case SNTP:
1196 if (NetNtpServerIP == 0) {
Luca Ceresoli4f63acd2011-05-11 03:59:56 +00001197 puts("*** ERROR: NTP server address not given\n");
Luca Ceresoli92895de2011-05-04 02:40:45 +00001198 return 1;
wdenkea287de2005-04-01 00:25:43 +00001199 }
1200 goto common;
1201#endif
Robin Getz1a32bf42009-07-20 14:53:54 -04001202#if defined(CONFIG_CMD_DNS)
1203 case DNS:
1204 if (NetOurDNSIP == 0) {
1205 puts("*** ERROR: DNS server address not given\n");
1206 return 1;
1207 }
1208 goto common;
1209#endif
Jon Loeliger643d1ab2007-07-09 17:45:14 -05001210#if defined(CONFIG_CMD_NFS)
wdenkcbd8a352004-02-24 02:00:03 +00001211 case NFS:
1212#endif
Simon Glasse4bf0c52011-10-24 18:00:02 +00001213 case TFTPGET:
Simon Glass1fb7cd42011-10-24 18:00:07 +00001214 case TFTPPUT:
wdenk6e592382004-04-18 17:39:38 +00001215 if (NetServerIP == 0) {
Luca Ceresoli4f63acd2011-05-11 03:59:56 +00001216 puts("*** ERROR: `serverip' not set\n");
Luca Ceresoli92895de2011-05-04 02:40:45 +00001217 return 1;
wdenk6e592382004-04-18 17:39:38 +00001218 }
Luca Ceresoli4f63acd2011-05-11 03:59:56 +00001219#if defined(CONFIG_CMD_PING) || defined(CONFIG_CMD_SNTP) || \
1220 defined(CONFIG_CMD_DNS)
1221common:
wdenk73a8b272003-06-05 19:27:42 +00001222#endif
Simon Guinot8b6bbe12011-05-01 23:38:40 +00001223 /* Fall through */
wdenk73a8b272003-06-05 19:27:42 +00001224
Simon Guinot8b6bbe12011-05-01 23:38:40 +00001225 case NETCONS:
Luca Ceresoli7a83af02011-05-17 00:03:40 +00001226 case TFTPSRV:
wdenk6e592382004-04-18 17:39:38 +00001227 if (NetOurIP == 0) {
Luca Ceresoli4f63acd2011-05-11 03:59:56 +00001228 puts("*** ERROR: `ipaddr' not set\n");
Luca Ceresoli92895de2011-05-04 02:40:45 +00001229 return 1;
wdenk6e592382004-04-18 17:39:38 +00001230 }
1231 /* Fall through */
wdenk2d966952002-10-31 22:12:35 +00001232
Peter Tyserbf6cb242010-09-30 11:25:48 -05001233#ifdef CONFIG_CMD_RARP
wdenk2d966952002-10-31 22:12:35 +00001234 case RARP:
Peter Tyserbf6cb242010-09-30 11:25:48 -05001235#endif
wdenk2d966952002-10-31 22:12:35 +00001236 case BOOTP:
wdenka3d991b2004-04-15 21:48:45 +00001237 case CDP:
Peter Tyserbf6cb242010-09-30 11:25:48 -05001238 case DHCP:
Joe Hershbergerd22c3382012-05-23 08:00:12 +00001239 case LINKLOCAL:
Luca Ceresoli4f63acd2011-05-11 03:59:56 +00001240 if (memcmp(NetOurEther, "\0\0\0\0\0\0", 6) == 0) {
Luca Ceresoli4f63acd2011-05-11 03:59:56 +00001241 int num = eth_get_dev_index();
wdenk2d966952002-10-31 22:12:35 +00001242
wdenk6e592382004-04-18 17:39:38 +00001243 switch (num) {
1244 case -1:
Luca Ceresoli4f63acd2011-05-11 03:59:56 +00001245 puts("*** ERROR: No ethernet found.\n");
Luca Ceresoli92895de2011-05-04 02:40:45 +00001246 return 1;
wdenk6e592382004-04-18 17:39:38 +00001247 case 0:
Luca Ceresoli4f63acd2011-05-11 03:59:56 +00001248 puts("*** ERROR: `ethaddr' not set\n");
wdenk2d966952002-10-31 22:12:35 +00001249 break;
wdenk6e592382004-04-18 17:39:38 +00001250 default:
Luca Ceresoli4f63acd2011-05-11 03:59:56 +00001251 printf("*** ERROR: `eth%daddr' not set\n",
wdenk2d966952002-10-31 22:12:35 +00001252 num);
1253 break;
wdenk2d966952002-10-31 22:12:35 +00001254 }
wdenk6e592382004-04-18 17:39:38 +00001255
Luca Ceresoli4f63acd2011-05-11 03:59:56 +00001256 NetStartAgain();
Luca Ceresoli92895de2011-05-04 02:40:45 +00001257 return 2;
wdenk6e592382004-04-18 17:39:38 +00001258 }
1259 /* Fall through */
1260 default:
Luca Ceresoli92895de2011-05-04 02:40:45 +00001261 return 0;
wdenk2d966952002-10-31 22:12:35 +00001262 }
Luca Ceresoli92895de2011-05-04 02:40:45 +00001263 return 0; /* OK */
wdenk2d966952002-10-31 22:12:35 +00001264}
1265/**********************************************************************/
1266
1267int
Luca Ceresoli6b147d12011-05-04 02:40:44 +00001268NetCksumOk(uchar *ptr, int len)
wdenk2d966952002-10-31 22:12:35 +00001269{
1270 return !((NetCksum(ptr, len) + 1) & 0xfffe);
1271}
1272
1273
1274unsigned
Luca Ceresoli6b147d12011-05-04 02:40:44 +00001275NetCksum(uchar *ptr, int len)
wdenk2d966952002-10-31 22:12:35 +00001276{
1277 ulong xsum;
Stefan Roese9d2a8732005-08-31 12:55:50 +02001278 ushort *p = (ushort *)ptr;
wdenk2d966952002-10-31 22:12:35 +00001279
1280 xsum = 0;
1281 while (len-- > 0)
Wolfgang Denk7bc5ee02005-08-26 01:36:03 +02001282 xsum += *p++;
wdenk2d966952002-10-31 22:12:35 +00001283 xsum = (xsum & 0xffff) + (xsum >> 16);
1284 xsum = (xsum & 0xffff) + (xsum >> 16);
Luca Ceresoli92895de2011-05-04 02:40:45 +00001285 return xsum & 0xffff;
wdenk2d966952002-10-31 22:12:35 +00001286}
1287
wdenka3d991b2004-04-15 21:48:45 +00001288int
1289NetEthHdrSize(void)
1290{
1291 ushort myvlanid;
wdenk2d966952002-10-31 22:12:35 +00001292
wdenka3d991b2004-04-15 21:48:45 +00001293 myvlanid = ntohs(NetOurVLAN);
1294 if (myvlanid == (ushort)-1)
1295 myvlanid = VLAN_NONE;
1296
Luca Ceresoli3e38e422011-05-11 03:59:54 +00001297 return ((myvlanid & VLAN_IDMASK) == VLAN_NONE) ? ETHER_HDR_SIZE :
1298 VLAN_ETHER_HDR_SIZE;
wdenka3d991b2004-04-15 21:48:45 +00001299}
1300
1301int
Joe Hershbergerdb288a92012-05-15 08:59:04 +00001302NetSetEther(uchar *xet, uchar * addr, uint prot)
wdenk2d966952002-10-31 22:12:35 +00001303{
Joe Hershbergercb487f52012-05-23 07:58:06 +00001304 struct ethernet_hdr *et = (struct ethernet_hdr *)xet;
wdenka3d991b2004-04-15 21:48:45 +00001305 ushort myvlanid;
1306
1307 myvlanid = ntohs(NetOurVLAN);
1308 if (myvlanid == (ushort)-1)
1309 myvlanid = VLAN_NONE;
wdenk2d966952002-10-31 22:12:35 +00001310
Luca Ceresoli4f63acd2011-05-11 03:59:56 +00001311 memcpy(et->et_dest, addr, 6);
1312 memcpy(et->et_src, NetOurEther, 6);
wdenka3d991b2004-04-15 21:48:45 +00001313 if ((myvlanid & VLAN_IDMASK) == VLAN_NONE) {
Luca Ceresolic819abe2011-05-04 02:40:46 +00001314 et->et_protlen = htons(prot);
wdenka3d991b2004-04-15 21:48:45 +00001315 return ETHER_HDR_SIZE;
1316 } else {
Joe Hershbergerc68cca32012-05-23 07:58:07 +00001317 struct vlan_ethernet_hdr *vet =
1318 (struct vlan_ethernet_hdr *)xet;
wdenk2d966952002-10-31 22:12:35 +00001319
wdenka3d991b2004-04-15 21:48:45 +00001320 vet->vet_vlan_type = htons(PROT_VLAN);
1321 vet->vet_tag = htons((0 << 5) | (myvlanid & VLAN_IDMASK));
1322 vet->vet_type = htons(prot);
1323 return VLAN_ETHER_HDR_SIZE;
1324 }
1325}
wdenk2d966952002-10-31 22:12:35 +00001326
Joe Hershbergere7111012012-05-23 07:59:16 +00001327int net_update_ether(struct ethernet_hdr *et, uchar *addr, uint prot)
1328{
1329 ushort protlen;
1330
1331 memcpy(et->et_dest, addr, 6);
1332 memcpy(et->et_src, NetOurEther, 6);
1333 protlen = ntohs(et->et_protlen);
1334 if (protlen == PROT_VLAN) {
1335 struct vlan_ethernet_hdr *vet =
1336 (struct vlan_ethernet_hdr *)et;
1337 vet->vet_type = htons(prot);
1338 return VLAN_ETHER_HDR_SIZE;
1339 } else if (protlen > 1514) {
1340 et->et_protlen = htons(prot);
1341 return ETHER_HDR_SIZE;
1342 } else {
1343 /* 802.2 + SNAP */
1344 struct e802_hdr *et802 = (struct e802_hdr *)et;
1345 et802->et_prot = htons(prot);
1346 return E802_HDR_SIZE;
1347 }
1348}
1349
Joe Hershberger4b11c912012-05-23 07:59:07 +00001350void net_set_ip_header(uchar *pkt, IPaddr_t dest, IPaddr_t source)
wdenk2d966952002-10-31 22:12:35 +00001351{
Joe Hershberger4b11c912012-05-23 07:59:07 +00001352 struct ip_udp_hdr *ip = (struct ip_udp_hdr *)pkt;
1353
1354 /*
1355 * Construct an IP header.
1356 */
1357 /* IP_HDR_SIZE / 4 (not including UDP) */
1358 ip->ip_hl_v = 0x45;
1359 ip->ip_tos = 0;
1360 ip->ip_len = htons(IP_HDR_SIZE);
1361 ip->ip_id = htons(NetIPID++);
1362 ip->ip_off = htons(IP_FLAGS_DFRAG); /* Don't fragment */
1363 ip->ip_ttl = 255;
1364 ip->ip_sum = 0;
1365 /* already in network byte order */
1366 NetCopyIP((void *)&ip->ip_src, &source);
1367 /* already in network byte order */
1368 NetCopyIP((void *)&ip->ip_dst, &dest);
1369}
1370
1371void net_set_udp_header(uchar *pkt, IPaddr_t dest, int dport, int sport,
1372 int len)
1373{
1374 struct ip_udp_hdr *ip = (struct ip_udp_hdr *)pkt;
wdenk2d966952002-10-31 22:12:35 +00001375
1376 /*
1377 * If the data is an odd number of bytes, zero the
1378 * byte after the last byte so that the checksum
1379 * will work.
1380 */
1381 if (len & 1)
Joe Hershberger4b11c912012-05-23 07:59:07 +00001382 pkt[IP_UDP_HDR_SIZE + len] = 0;
wdenk2d966952002-10-31 22:12:35 +00001383
Joe Hershberger4b11c912012-05-23 07:59:07 +00001384 net_set_ip_header(pkt, dest, NetOurIP);
Joe Hershberger594c26f2012-05-23 07:58:04 +00001385 ip->ip_len = htons(IP_UDP_HDR_SIZE + len);
Joe Hershberger4b11c912012-05-23 07:59:07 +00001386 ip->ip_p = IPPROTO_UDP;
1387 ip->ip_sum = ~NetCksum((uchar *)ip, IP_HDR_SIZE >> 1);
1388
wdenk2d966952002-10-31 22:12:35 +00001389 ip->udp_src = htons(sport);
1390 ip->udp_dst = htons(dport);
Joe Hershberger594c26f2012-05-23 07:58:04 +00001391 ip->udp_len = htons(UDP_HDR_SIZE + len);
wdenk2d966952002-10-31 22:12:35 +00001392 ip->udp_xsum = 0;
wdenk2d966952002-10-31 22:12:35 +00001393}
1394
Luca Ceresoli4f63acd2011-05-11 03:59:56 +00001395void copy_filename(char *dst, const char *src, int size)
wdenk2d966952002-10-31 22:12:35 +00001396{
1397 if (*src && (*src == '"')) {
1398 ++src;
1399 --size;
1400 }
1401
Luca Ceresolid3c65b02011-05-04 02:40:43 +00001402 while ((--size > 0) && *src && (*src != '"'))
wdenk2d966952002-10-31 22:12:35 +00001403 *dst++ = *src++;
wdenk2d966952002-10-31 22:12:35 +00001404 *dst = '\0';
1405}
1406
Luca Ceresoli3e38e422011-05-11 03:59:54 +00001407#if defined(CONFIG_CMD_NFS) || \
1408 defined(CONFIG_CMD_SNTP) || \
1409 defined(CONFIG_CMD_DNS)
Robin Getz1a32bf42009-07-20 14:53:54 -04001410/*
Robin Getz97399462010-03-08 14:07:00 -05001411 * make port a little random (1024-17407)
1412 * This keeps the math somewhat trivial to compute, and seems to work with
1413 * all supported protocols/clients/servers
Robin Getz1a32bf42009-07-20 14:53:54 -04001414 */
1415unsigned int random_port(void)
1416{
Robin Getz97399462010-03-08 14:07:00 -05001417 return 1024 + (get_timer(0) % 0x4000);
Robin Getz1a32bf42009-07-20 14:53:54 -04001418}
1419#endif
1420
Luca Ceresoli4f63acd2011-05-11 03:59:56 +00001421void ip_to_string(IPaddr_t x, char *s)
wdenk2d966952002-10-31 22:12:35 +00001422{
Luca Ceresoli4f63acd2011-05-11 03:59:56 +00001423 x = ntohl(x);
1424 sprintf(s, "%d.%d.%d.%d",
1425 (int) ((x >> 24) & 0xff),
1426 (int) ((x >> 16) & 0xff),
1427 (int) ((x >> 8) & 0xff), (int) ((x >> 0) & 0xff)
wdenk6e592382004-04-18 17:39:38 +00001428 );
wdenk2d966952002-10-31 22:12:35 +00001429}
1430
wdenka3d991b2004-04-15 21:48:45 +00001431void VLAN_to_string(ushort x, char *s)
1432{
1433 x = ntohs(x);
1434
1435 if (x == (ushort)-1)
1436 x = VLAN_NONE;
1437
1438 if (x == VLAN_NONE)
1439 strcpy(s, "none");
1440 else
1441 sprintf(s, "%d", x & VLAN_IDMASK);
1442}
1443
Mike Frysinger2e3ef6e2010-10-20 07:16:48 -04001444ushort string_to_VLAN(const char *s)
wdenka3d991b2004-04-15 21:48:45 +00001445{
1446 ushort id;
1447
1448 if (s == NULL)
wdenkb9711de2004-04-25 13:18:40 +00001449 return htons(VLAN_NONE);
wdenka3d991b2004-04-15 21:48:45 +00001450
1451 if (*s < '0' || *s > '9')
1452 id = VLAN_NONE;
1453 else
1454 id = (ushort)simple_strtoul(s, NULL, 10);
1455
wdenkb9711de2004-04-25 13:18:40 +00001456 return htons(id);
wdenka3d991b2004-04-15 21:48:45 +00001457}
1458
wdenka3d991b2004-04-15 21:48:45 +00001459ushort getenv_VLAN(char *var)
1460{
Luca Ceresoli92895de2011-05-04 02:40:45 +00001461 return string_to_VLAN(getenv(var));
wdenka3d991b2004-04-15 21:48:45 +00001462}