blob: aa1ff488926f829f8ced0ed174ebe5f9ae070b12 [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 *
26 * RARP:
27 *
28 * Prerequisites: - own ethernet address
29 * We want: - own IP address
30 * - TFTP server IP address
31 * Next step: ARP
32 *
33 * ARP:
34 *
35 * Prerequisites: - own ethernet address
36 * - own IP address
37 * - TFTP server IP address
38 * We want: - TFTP server ethernet address
39 * Next step: TFTP
40 *
41 * DHCP:
42 *
Wolfgang Denkb2f50802005-08-12 23:43:12 +020043 * Prerequisites: - own ethernet address
44 * We want: - IP, Netmask, ServerIP, Gateway IP
45 * - bootfilename, lease time
46 * Next step: - TFTP
wdenk2d966952002-10-31 22:12:35 +000047 *
48 * TFTP:
49 *
50 * Prerequisites: - own ethernet address
51 * - own IP address
52 * - TFTP server IP address
53 * - TFTP server ethernet address
54 * - name of bootfile (if unknown, we use a default name
55 * derived from our own IP address)
56 * We want: - load the boot file
57 * Next step: none
wdenkcbd8a352004-02-24 02:00:03 +000058 *
59 * NFS:
60 *
61 * Prerequisites: - own ethernet address
62 * - own IP address
63 * - name of bootfile (if unknown, we use a default name
64 * derived from our own IP address)
65 * We want: - load the boot file
66 * Next step: none
wdenkea287de2005-04-01 00:25:43 +000067 *
68 * SNTP:
69 *
Wolfgang Denkb2f50802005-08-12 23:43:12 +020070 * Prerequisites: - own ethernet address
wdenkea287de2005-04-01 00:25:43 +000071 * - own IP address
72 * We want: - network time
73 * Next step: none
wdenk2d966952002-10-31 22:12:35 +000074 */
75
76
77#include <common.h>
wdenk2d966952002-10-31 22:12:35 +000078#include <command.h>
79#include <net.h>
Joe Hershberger4545f4e2012-05-23 07:58:15 +000080#if defined(CONFIG_STATUS_LED)
81#include <miiphy.h>
82#include <status_led.h>
83#endif
84#include <watchdog.h>
85#include <linux/compiler.h>
Joe Hershbergerd280d3f2012-05-23 07:58:01 +000086#include "arp.h"
wdenk2d966952002-10-31 22:12:35 +000087#include "bootp.h"
Joe Hershbergerf575ae12012-05-23 07:57:59 +000088#include "cdp.h"
Robin Getz1a32bf42009-07-20 14:53:54 -040089#if defined(CONFIG_CMD_DNS)
90#include "dns.h"
91#endif
Joe Hershberger4545f4e2012-05-23 07:58:15 +000092#include "nfs.h"
Joe Hershbergera36b12f2012-05-23 07:58:02 +000093#include "ping.h"
Joe Hershberger4545f4e2012-05-23 07:58:15 +000094#include "rarp.h"
95#if defined(CONFIG_CMD_SNTP)
96#include "sntp.h"
97#endif
98#include "tftp.h"
wdenk2d966952002-10-31 22:12:35 +000099
Wolfgang Denkd87080b2006-03-31 18:32:53 +0200100DECLARE_GLOBAL_DATA_PTR;
101
wdenk2d966952002-10-31 22:12:35 +0000102/** BOOTP EXTENTIONS **/
103
Luca Ceresoli3e38e422011-05-11 03:59:54 +0000104/* Our subnet mask (0=unknown) */
Luca Ceresolic586ce62011-05-11 03:59:55 +0000105IPaddr_t NetOurSubnetMask;
Luca Ceresoli3e38e422011-05-11 03:59:54 +0000106/* Our gateways IP address */
Luca Ceresolic586ce62011-05-11 03:59:55 +0000107IPaddr_t NetOurGatewayIP;
Luca Ceresoli3e38e422011-05-11 03:59:54 +0000108/* Our DNS IP address */
Luca Ceresolic586ce62011-05-11 03:59:55 +0000109IPaddr_t NetOurDNSIP;
Jon Loeliger1fe80d72007-07-09 22:08:34 -0500110#if defined(CONFIG_BOOTP_DNS2)
Luca Ceresoli3e38e422011-05-11 03:59:54 +0000111/* Our 2nd DNS IP address */
Luca Ceresolic586ce62011-05-11 03:59:55 +0000112IPaddr_t NetOurDNS2IP;
stroesefe389a82003-08-28 14:17:32 +0000113#endif
Luca Ceresoli3e38e422011-05-11 03:59:54 +0000114/* Our NIS domain */
Luca Ceresolic586ce62011-05-11 03:59:55 +0000115char NetOurNISDomain[32] = {0,};
Luca Ceresoli3e38e422011-05-11 03:59:54 +0000116/* Our hostname */
Luca Ceresolic586ce62011-05-11 03:59:55 +0000117char NetOurHostName[32] = {0,};
Luca Ceresoli3e38e422011-05-11 03:59:54 +0000118/* Our bootpath */
Luca Ceresolic586ce62011-05-11 03:59:55 +0000119char NetOurRootPath[64] = {0,};
Luca Ceresoli3e38e422011-05-11 03:59:54 +0000120/* Our bootfile size in blocks */
Luca Ceresolic586ce62011-05-11 03:59:55 +0000121ushort NetBootFileSize;
wdenk2d966952002-10-31 22:12:35 +0000122
David Updegraff53a5c422007-06-11 10:41:07 -0500123#ifdef CONFIG_MCAST_TFTP /* Multicast TFTP */
124IPaddr_t Mcast_addr;
125#endif
126
wdenk2d966952002-10-31 22:12:35 +0000127/** END OF BOOTP EXTENTIONS **/
128
Luca Ceresoli3e38e422011-05-11 03:59:54 +0000129/* The actual transferred size of the bootfile (in bytes) */
130ulong NetBootFileXferSize;
131/* Our ethernet address */
132uchar NetOurEther[6];
133/* Boot server enet address */
Luca Ceresolic586ce62011-05-11 03:59:55 +0000134uchar NetServerEther[6];
Luca Ceresoli3e38e422011-05-11 03:59:54 +0000135/* Our IP addr (0 = unknown) */
136IPaddr_t NetOurIP;
137/* Server IP addr (0 = unknown) */
138IPaddr_t NetServerIP;
139/* Current receive packet */
Joe Hershbergerdb288a92012-05-15 08:59:04 +0000140uchar *NetRxPacket;
Luca Ceresoli3e38e422011-05-11 03:59:54 +0000141/* Current rx packet length */
142int NetRxPacketLen;
143/* IP packet ID */
144unsigned NetIPID;
145/* Ethernet bcast address */
Luca Ceresolic586ce62011-05-11 03:59:55 +0000146uchar NetBcastAddr[6] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
147uchar NetEtherNullAddr[6];
Rafal Jaworowskif85b6072007-12-27 18:19:02 +0100148#ifdef CONFIG_API
Joe Hershbergerdb288a92012-05-15 08:59:04 +0000149void (*push_packet)(void *, int len) = 0;
Rafal Jaworowskif85b6072007-12-27 18:19:02 +0100150#endif
Luca Ceresoli3e38e422011-05-11 03:59:54 +0000151/* Network loop state */
Joe Hershberger22f6e992012-05-23 07:59:14 +0000152enum net_loop_state net_state;
Luca Ceresoli3e38e422011-05-11 03:59:54 +0000153/* Tried all network devices */
Luca Ceresolic586ce62011-05-11 03:59:55 +0000154int NetRestartWrap;
Luca Ceresoli3e38e422011-05-11 03:59:54 +0000155/* Network loop restarted */
Luca Ceresolic586ce62011-05-11 03:59:55 +0000156static int NetRestarted;
Luca Ceresoli3e38e422011-05-11 03:59:54 +0000157/* At least one device configured */
Luca Ceresolic586ce62011-05-11 03:59:55 +0000158static int NetDevExists;
wdenk2d966952002-10-31 22:12:35 +0000159
wdenk6e592382004-04-18 17:39:38 +0000160/* XXX in both little & big endian machines 0xFFFF == ntohs(-1) */
Luca Ceresoli3e38e422011-05-11 03:59:54 +0000161/* default is without VLAN */
162ushort NetOurVLAN = 0xFFFF;
163/* ditto */
164ushort NetOurNativeVLAN = 0xFFFF;
wdenka3d991b2004-04-15 21:48:45 +0000165
Luca Ceresoli3e38e422011-05-11 03:59:54 +0000166/* Boot File name */
167char BootFile[128];
wdenk2d966952002-10-31 22:12:35 +0000168
Jon Loeliger643d1ab2007-07-09 17:45:14 -0500169#if defined(CONFIG_CMD_SNTP)
Luca Ceresoli3e38e422011-05-11 03:59:54 +0000170/* NTP server IP address */
171IPaddr_t NetNtpServerIP;
172/* offset time from UTC */
Luca Ceresolic586ce62011-05-11 03:59:55 +0000173int NetTimeOffset;
wdenkea287de2005-04-01 00:25:43 +0000174#endif
175
Joe Hershbergerdb288a92012-05-15 08:59:04 +0000176uchar PktBuf[(PKTBUFSRX+1) * PKTSIZE_ALIGN + PKTALIGN];
wdenk2d966952002-10-31 22:12:35 +0000177
Luca Ceresoli3e38e422011-05-11 03:59:54 +0000178/* Receive packet */
Joe Hershbergerdb288a92012-05-15 08:59:04 +0000179uchar *NetRxPackets[PKTBUFSRX];
wdenk2d966952002-10-31 22:12:35 +0000180
Joe Hershbergerece223b2012-05-23 07:59:15 +0000181/* Current UDP RX packet handler */
182static rxhand_f *udp_packet_handler;
183/* Current ARP RX packet handler */
184static rxhand_f *arp_packet_handler;
Simon Glass39bccd22011-10-26 14:18:38 +0000185#ifdef CONFIG_CMD_TFTPPUT
Joe Hershbergerece223b2012-05-23 07:59:15 +0000186/* Current ICMP rx handler */
187static rxhand_icmp_f *packet_icmp_handler;
Simon Glass39bccd22011-10-26 14:18:38 +0000188#endif
Luca Ceresoli3e38e422011-05-11 03:59:54 +0000189/* Current timeout handler */
190static thand_f *timeHandler;
191/* Time base value */
192static ulong timeStart;
193/* Current timeout value */
194static ulong timeDelta;
195/* THE transmit packet */
Joe Hershbergerdb288a92012-05-15 08:59:04 +0000196uchar *NetTxPacket;
wdenk2d966952002-10-31 22:12:35 +0000197
Simon Glasse4bf0c52011-10-24 18:00:02 +0000198static int net_check_prereq(enum proto_t protocol);
wdenk2d966952002-10-31 22:12:35 +0000199
Remy Bohmer67b96e82009-10-28 22:13:39 +0100200static int NetTryCount;
201
wdenk2d966952002-10-31 22:12:35 +0000202/**********************************************************************/
wdenk73a8b272003-06-05 19:27:42 +0000203
Simon Glasse4a3d572011-10-27 06:24:32 +0000204/*
205 * Check if autoload is enabled. If so, use either NFS or TFTP to download
206 * the boot file.
207 */
208void net_auto_load(void)
209{
210 const char *s = getenv("autoload");
211
212 if (s != NULL) {
213 if (*s == 'n') {
214 /*
215 * Just use BOOTP/RARP to configure system;
216 * Do not use TFTP to load the bootfile.
217 */
Joe Hershberger22f6e992012-05-23 07:59:14 +0000218 net_set_state(NETLOOP_SUCCESS);
Simon Glasse4a3d572011-10-27 06:24:32 +0000219 return;
220 }
221#if defined(CONFIG_CMD_NFS)
222 if (strcmp(s, "NFS") == 0) {
223 /*
224 * Use NFS to load the bootfile.
225 */
226 NfsStart();
227 return;
228 }
229#endif
230 }
231 TftpStart(TFTPGET);
232}
233
Joe Hershbergercb1c9912012-05-23 07:59:21 +0000234static void NetInitLoop(void)
Heiko Schocher2f70c492009-02-10 09:38:52 +0100235{
Luca Ceresolic586ce62011-05-11 03:59:55 +0000236 static int env_changed_id;
Luca Ceresoli4f63acd2011-05-11 03:59:56 +0000237 int env_id = get_env_id();
Heiko Schocher2f70c492009-02-10 09:38:52 +0100238
239 /* update only when the environment has changed */
Michael Zaidman3c172c42009-04-04 01:43:00 +0300240 if (env_changed_id != env_id) {
Enric Balletbo i Serra23a70bf2011-05-31 21:01:47 +0000241 NetOurIP = getenv_IPaddr("ipaddr");
Luca Ceresoli4f63acd2011-05-11 03:59:56 +0000242 NetOurGatewayIP = getenv_IPaddr("gatewayip");
243 NetOurSubnetMask = getenv_IPaddr("netmask");
244 NetServerIP = getenv_IPaddr("serverip");
Heiko Schocher2f70c492009-02-10 09:38:52 +0100245 NetOurNativeVLAN = getenv_VLAN("nvlan");
Michael Zaidman3c172c42009-04-04 01:43:00 +0300246 NetOurVLAN = getenv_VLAN("vlan");
Robin Getz1a32bf42009-07-20 14:53:54 -0400247#if defined(CONFIG_CMD_DNS)
248 NetOurDNSIP = getenv_IPaddr("dnsip");
249#endif
Michael Zaidman3c172c42009-04-04 01:43:00 +0300250 env_changed_id = env_id;
Heiko Schocher2f70c492009-02-10 09:38:52 +0100251 }
Michael Zaidman3c172c42009-04-04 01:43:00 +0300252
Heiko Schocherda954272009-04-28 08:36:11 +0200253 return;
Heiko Schocher2f70c492009-02-10 09:38:52 +0100254}
255
Joe Hershbergerece223b2012-05-23 07:59:15 +0000256static void net_clear_handlers(void)
257{
258 net_set_udp_handler(NULL);
259 net_set_arp_handler(NULL);
260 NetSetTimeout(0, NULL);
261}
262
263static void net_cleanup_loop(void)
264{
265 net_clear_handlers();
266}
267
Joe Hershberger46c495d2012-05-23 07:59:22 +0000268void net_init(void)
269{
270 static int first_call = 1;
271
272 if (first_call) {
273 /*
274 * Setup packet buffers, aligned correctly.
275 */
276 int i;
277
278 NetTxPacket = &PktBuf[0] + (PKTALIGN - 1);
279 NetTxPacket -= (ulong)NetTxPacket % PKTALIGN;
280 for (i = 0; i < PKTBUFSRX; i++)
281 NetRxPackets[i] = NetTxPacket + (i + 1) * PKTSIZE_ALIGN;
282
283 ArpInit();
284 net_clear_handlers();
285
286 /* Only need to setup buffer pointers once. */
287 first_call = 0;
288 }
289
290 NetInitLoop();
291}
292
wdenk73a8b272003-06-05 19:27:42 +0000293/**********************************************************************/
wdenk2d966952002-10-31 22:12:35 +0000294/*
295 * Main network processing loop.
296 */
297
Simon Glasse4bf0c52011-10-24 18:00:02 +0000298int NetLoop(enum proto_t protocol)
wdenk2d966952002-10-31 22:12:35 +0000299{
wdenk2d966952002-10-31 22:12:35 +0000300 bd_t *bd = gd->bd;
Simon Glass4793ee62011-10-24 18:00:01 +0000301 int ret = -1;
wdenk2d966952002-10-31 22:12:35 +0000302
wdenk2d966952002-10-31 22:12:35 +0000303 NetRestarted = 0;
304 NetDevExists = 0;
Remy Bohmer67b96e82009-10-28 22:13:39 +0100305 NetTryCount = 1;
wdenk73a8b272003-06-05 19:27:42 +0000306
Simon Glass573f14f2011-12-10 11:08:06 +0000307 bootstage_mark_name(BOOTSTAGE_ID_ETH_START, "eth_start");
Joe Hershberger46c495d2012-05-23 07:59:22 +0000308 net_init();
wdenk2d966952002-10-31 22:12:35 +0000309 eth_halt();
wdenka3d991b2004-04-15 21:48:45 +0000310 eth_set_current();
wdenkb1bf6f22005-04-03 14:52:59 +0000311 if (eth_init(bd) < 0) {
312 eth_halt();
Luca Ceresoli92895de2011-05-04 02:40:45 +0000313 return -1;
wdenkb1bf6f22005-04-03 14:52:59 +0000314 }
wdenk2d966952002-10-31 22:12:35 +0000315
316restart:
Luca Ceresoli4f63acd2011-05-11 03:59:56 +0000317 memcpy(NetOurEther, eth_get_dev()->enetaddr, 6);
wdenk2d966952002-10-31 22:12:35 +0000318
Joe Hershberger22f6e992012-05-23 07:59:14 +0000319 net_set_state(NETLOOP_CONTINUE);
wdenk2d966952002-10-31 22:12:35 +0000320
321 /*
322 * Start the ball rolling with the given start function. From
323 * here on, this code is a state machine driven by received
324 * packets and timer events.
325 */
Joe Hershbergercb1c9912012-05-23 07:59:21 +0000326 NetInitLoop();
wdenk2d966952002-10-31 22:12:35 +0000327
Luca Ceresoli4f63acd2011-05-11 03:59:56 +0000328 switch (net_check_prereq(protocol)) {
wdenk2d966952002-10-31 22:12:35 +0000329 case 1:
330 /* network not configured */
wdenkb1bf6f22005-04-03 14:52:59 +0000331 eth_halt();
Luca Ceresoli92895de2011-05-04 02:40:45 +0000332 return -1;
wdenk2d966952002-10-31 22:12:35 +0000333
wdenk2d966952002-10-31 22:12:35 +0000334 case 2:
335 /* network device not configured */
336 break;
wdenk2d966952002-10-31 22:12:35 +0000337
338 case 0:
wdenk2d966952002-10-31 22:12:35 +0000339 NetDevExists = 1;
Simon Glasse4bf0c52011-10-24 18:00:02 +0000340 NetBootFileXferSize = 0;
wdenk2d966952002-10-31 22:12:35 +0000341 switch (protocol) {
Simon Glasse4bf0c52011-10-24 18:00:02 +0000342 case TFTPGET:
Simon Glass1fb7cd42011-10-24 18:00:07 +0000343#ifdef CONFIG_CMD_TFTPPUT
344 case TFTPPUT:
345#endif
wdenk2d966952002-10-31 22:12:35 +0000346 /* always use ARP to get server ethernet address */
Simon Glasse4bf0c52011-10-24 18:00:02 +0000347 TftpStart(protocol);
wdenk2d966952002-10-31 22:12:35 +0000348 break;
Luca Ceresoli7a83af02011-05-17 00:03:40 +0000349#ifdef CONFIG_CMD_TFTPSRV
350 case TFTPSRV:
351 TftpStartServer();
352 break;
353#endif
Jon Loeliger643d1ab2007-07-09 17:45:14 -0500354#if defined(CONFIG_CMD_DHCP)
wdenk2d966952002-10-31 22:12:35 +0000355 case DHCP:
wdenkd407bf52004-10-11 22:51:13 +0000356 BootpTry = 0;
Michael Zaidman09133f82009-07-14 23:37:12 +0300357 NetOurIP = 0;
wdenk2d966952002-10-31 22:12:35 +0000358 DhcpRequest(); /* Basically same as BOOTP */
359 break;
Jon Loeliger610f2e92007-07-10 11:05:02 -0500360#endif
wdenk2d966952002-10-31 22:12:35 +0000361
362 case BOOTP:
363 BootpTry = 0;
Michael Zaidman09133f82009-07-14 23:37:12 +0300364 NetOurIP = 0;
Luca Ceresoli4f63acd2011-05-11 03:59:56 +0000365 BootpRequest();
wdenk2d966952002-10-31 22:12:35 +0000366 break;
367
Peter Tyserbf6cb242010-09-30 11:25:48 -0500368#if defined(CONFIG_CMD_RARP)
wdenk2d966952002-10-31 22:12:35 +0000369 case RARP:
370 RarpTry = 0;
Michael Zaidman09133f82009-07-14 23:37:12 +0300371 NetOurIP = 0;
Luca Ceresoli4f63acd2011-05-11 03:59:56 +0000372 RarpRequest();
wdenk2d966952002-10-31 22:12:35 +0000373 break;
Peter Tyserbf6cb242010-09-30 11:25:48 -0500374#endif
Jon Loeliger643d1ab2007-07-09 17:45:14 -0500375#if defined(CONFIG_CMD_PING)
wdenk73a8b272003-06-05 19:27:42 +0000376 case PING:
Joe Hershbergera36b12f2012-05-23 07:58:02 +0000377 ping_start();
wdenk73a8b272003-06-05 19:27:42 +0000378 break;
379#endif
Jon Loeliger643d1ab2007-07-09 17:45:14 -0500380#if defined(CONFIG_CMD_NFS)
wdenkcbd8a352004-02-24 02:00:03 +0000381 case NFS:
382 NfsStart();
383 break;
384#endif
Jon Loeliger643d1ab2007-07-09 17:45:14 -0500385#if defined(CONFIG_CMD_CDP)
wdenka3d991b2004-04-15 21:48:45 +0000386 case CDP:
387 CDPStart();
388 break;
389#endif
wdenk68ceb292004-08-02 21:11:11 +0000390#ifdef CONFIG_NETCONSOLE
391 case NETCONS:
392 NcStart();
393 break;
394#endif
Jon Loeliger643d1ab2007-07-09 17:45:14 -0500395#if defined(CONFIG_CMD_SNTP)
wdenkea287de2005-04-01 00:25:43 +0000396 case SNTP:
397 SntpStart();
398 break;
399#endif
Robin Getz1a32bf42009-07-20 14:53:54 -0400400#if defined(CONFIG_CMD_DNS)
401 case DNS:
402 DnsStart();
403 break;
404#endif
wdenk2d966952002-10-31 22:12:35 +0000405 default:
406 break;
407 }
408
wdenk2d966952002-10-31 22:12:35 +0000409 break;
410 }
411
Jon Loeliger643d1ab2007-07-09 17:45:14 -0500412#if defined(CONFIG_MII) || defined(CONFIG_CMD_MII)
Luca Ceresoli3e38e422011-05-11 03:59:54 +0000413#if defined(CONFIG_SYS_FAULT_ECHO_LINK_DOWN) && \
414 defined(CONFIG_STATUS_LED) && \
415 defined(STATUS_LED_RED)
wdenkfc3e2162003-10-08 22:33:00 +0000416 /*
wdenk42d1f032003-10-15 23:53:47 +0000417 * Echo the inverted link state to the fault LED.
wdenkfc3e2162003-10-08 22:33:00 +0000418 */
Luca Ceresolid3c65b02011-05-04 02:40:43 +0000419 if (miiphy_link(eth_get_dev()->name, CONFIG_SYS_FAULT_MII_ADDR))
Luca Ceresoli4f63acd2011-05-11 03:59:56 +0000420 status_led_set(STATUS_LED_RED, STATUS_LED_OFF);
Luca Ceresolid3c65b02011-05-04 02:40:43 +0000421 else
Luca Ceresoli4f63acd2011-05-11 03:59:56 +0000422 status_led_set(STATUS_LED_RED, STATUS_LED_ON);
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200423#endif /* CONFIG_SYS_FAULT_ECHO_LINK_DOWN, ... */
wdenkfc3e2162003-10-08 22:33:00 +0000424#endif /* CONFIG_MII, ... */
wdenk2d966952002-10-31 22:12:35 +0000425
426 /*
427 * Main packet reception loop. Loop receiving packets until
Joe Hershberger22f6e992012-05-23 07:59:14 +0000428 * someone sets `net_state' to a state that terminates.
wdenk2d966952002-10-31 22:12:35 +0000429 */
430 for (;;) {
431 WATCHDOG_RESET();
432#ifdef CONFIG_SHOW_ACTIVITY
Joe Hershberger48522bb2012-05-15 08:59:08 +0000433 show_activity(1);
wdenk2d966952002-10-31 22:12:35 +0000434#endif
435 /*
436 * Check the ethernet for a new packet. The ethernet
437 * receive routine will process it.
438 */
Guennadi Liakhovetski40cb90e2008-04-03 17:04:19 +0200439 eth_rx();
wdenk2d966952002-10-31 22:12:35 +0000440
441 /*
442 * Abort if ctrl-c was pressed.
443 */
444 if (ctrlc()) {
Joe Hershbergere94070c2012-05-23 07:59:24 +0000445 /* cancel any ARP that may not have completed */
446 NetArpWaitPacketIP = 0;
447
Joe Hershbergerece223b2012-05-23 07:59:15 +0000448 net_cleanup_loop();
wdenk8bde7f72003-06-27 21:31:46 +0000449 eth_halt();
Luca Ceresoli4f63acd2011-05-11 03:59:56 +0000450 puts("\nAbort\n");
Simon Glass4793ee62011-10-24 18:00:01 +0000451 goto done;
wdenk2d966952002-10-31 22:12:35 +0000452 }
453
wdenk73a8b272003-06-05 19:27:42 +0000454 ArpTimeoutCheck();
wdenk2d966952002-10-31 22:12:35 +0000455
456 /*
457 * Check for a timeout, and run the timeout handler
458 * if we have one.
459 */
wdenke0ac62d2003-08-17 18:55:18 +0000460 if (timeHandler && ((get_timer(0) - timeStart) > timeDelta)) {
wdenk2d966952002-10-31 22:12:35 +0000461 thand_f *x;
462
Jon Loeliger643d1ab2007-07-09 17:45:14 -0500463#if defined(CONFIG_MII) || defined(CONFIG_CMD_MII)
Luca Ceresoli4f63acd2011-05-11 03:59:56 +0000464#if defined(CONFIG_SYS_FAULT_ECHO_LINK_DOWN) && \
465 defined(CONFIG_STATUS_LED) && \
466 defined(STATUS_LED_RED)
wdenkfc3e2162003-10-08 22:33:00 +0000467 /*
wdenk42d1f032003-10-15 23:53:47 +0000468 * Echo the inverted link state to the fault LED.
wdenkfc3e2162003-10-08 22:33:00 +0000469 */
Luca Ceresoli4f63acd2011-05-11 03:59:56 +0000470 if (miiphy_link(eth_get_dev()->name,
Luca Ceresoli3e38e422011-05-11 03:59:54 +0000471 CONFIG_SYS_FAULT_MII_ADDR)) {
Luca Ceresoli4f63acd2011-05-11 03:59:56 +0000472 status_led_set(STATUS_LED_RED, STATUS_LED_OFF);
wdenkfc3e2162003-10-08 22:33:00 +0000473 } else {
Luca Ceresoli4f63acd2011-05-11 03:59:56 +0000474 status_led_set(STATUS_LED_RED, STATUS_LED_ON);
wdenkfc3e2162003-10-08 22:33:00 +0000475 }
Luca Ceresoli4f63acd2011-05-11 03:59:56 +0000476#endif /* CONFIG_SYS_FAULT_ECHO_LINK_DOWN, ... */
wdenkfc3e2162003-10-08 22:33:00 +0000477#endif /* CONFIG_MII, ... */
wdenk2d966952002-10-31 22:12:35 +0000478 x = timeHandler;
479 timeHandler = (thand_f *)0;
480 (*x)();
481 }
482
483
Joe Hershberger22f6e992012-05-23 07:59:14 +0000484 switch (net_state) {
wdenk2d966952002-10-31 22:12:35 +0000485
486 case NETLOOP_RESTART:
wdenk2d966952002-10-31 22:12:35 +0000487 NetRestarted = 1;
wdenk2d966952002-10-31 22:12:35 +0000488 goto restart;
489
490 case NETLOOP_SUCCESS:
Joe Hershbergerece223b2012-05-23 07:59:15 +0000491 net_cleanup_loop();
wdenk2d966952002-10-31 22:12:35 +0000492 if (NetBootFileXferSize > 0) {
Wolfgang Denkf34024d2007-09-12 00:48:57 +0200493 char buf[20];
wdenk2d966952002-10-31 22:12:35 +0000494 printf("Bytes transferred = %ld (%lx hex)\n",
495 NetBootFileXferSize,
496 NetBootFileXferSize);
Wolfgang Denkf34024d2007-09-12 00:48:57 +0200497 sprintf(buf, "%lX", NetBootFileXferSize);
wdenk2d966952002-10-31 22:12:35 +0000498 setenv("filesize", buf);
wdenka3d991b2004-04-15 21:48:45 +0000499
500 sprintf(buf, "%lX", (unsigned long)load_addr);
501 setenv("fileaddr", buf);
wdenk2d966952002-10-31 22:12:35 +0000502 }
503 eth_halt();
Simon Glass4793ee62011-10-24 18:00:01 +0000504 ret = NetBootFileXferSize;
505 goto done;
wdenk2d966952002-10-31 22:12:35 +0000506
507 case NETLOOP_FAIL:
Joe Hershbergerece223b2012-05-23 07:59:15 +0000508 net_cleanup_loop();
Simon Glass4793ee62011-10-24 18:00:01 +0000509 goto done;
Joe Hershberger22f6e992012-05-23 07:59:14 +0000510
511 case NETLOOP_CONTINUE:
512 continue;
wdenk2d966952002-10-31 22:12:35 +0000513 }
514 }
Simon Glass4793ee62011-10-24 18:00:01 +0000515
516done:
Simon Glass39bccd22011-10-26 14:18:38 +0000517#ifdef CONFIG_CMD_TFTPPUT
Simon Glass4793ee62011-10-24 18:00:01 +0000518 /* Clear out the handlers */
Joe Hershbergerece223b2012-05-23 07:59:15 +0000519 net_set_udp_handler(NULL);
Simon Glass4793ee62011-10-24 18:00:01 +0000520 net_set_icmp_handler(NULL);
Simon Glass39bccd22011-10-26 14:18:38 +0000521#endif
Simon Glass4793ee62011-10-24 18:00:01 +0000522 return ret;
wdenk2d966952002-10-31 22:12:35 +0000523}
524
525/**********************************************************************/
526
527static void
528startAgainTimeout(void)
529{
Joe Hershberger22f6e992012-05-23 07:59:14 +0000530 net_set_state(NETLOOP_RESTART);
wdenk2d966952002-10-31 22:12:35 +0000531}
532
Luca Ceresoli4f63acd2011-05-11 03:59:56 +0000533void NetStartAgain(void)
wdenk2d966952002-10-31 22:12:35 +0000534{
wdenk6e592382004-04-18 17:39:38 +0000535 char *nretry;
Remy Bohmer67b96e82009-10-28 22:13:39 +0100536 int retry_forever = 0;
537 unsigned long retrycnt = 0;
wdenka3d991b2004-04-15 21:48:45 +0000538
Remy Bohmer67b96e82009-10-28 22:13:39 +0100539 nretry = getenv("netretry");
540 if (nretry) {
541 if (!strcmp(nretry, "yes"))
542 retry_forever = 1;
543 else if (!strcmp(nretry, "no"))
544 retrycnt = 0;
545 else if (!strcmp(nretry, "once"))
546 retrycnt = 1;
547 else
548 retrycnt = simple_strtoul(nretry, NULL, 0);
549 } else
550 retry_forever = 1;
551
552 if ((!retry_forever) && (NetTryCount >= retrycnt)) {
553 eth_halt();
Joe Hershberger22f6e992012-05-23 07:59:14 +0000554 net_set_state(NETLOOP_FAIL);
wdenka3d991b2004-04-15 21:48:45 +0000555 return;
556 }
Remy Bohmer67b96e82009-10-28 22:13:39 +0100557
558 NetTryCount++;
559
Luca Ceresoli4f63acd2011-05-11 03:59:56 +0000560 eth_halt();
Matthias Fuchs8b0c5c12007-12-27 16:58:41 +0100561#if !defined(CONFIG_NET_DO_NOT_TRY_ANOTHER)
Luca Ceresoli4f63acd2011-05-11 03:59:56 +0000562 eth_try_another(!NetRestarted);
Matthias Fuchs8b0c5c12007-12-27 16:58:41 +0100563#endif
Luca Ceresoli4f63acd2011-05-11 03:59:56 +0000564 eth_init(gd->bd);
wdenk6e592382004-04-18 17:39:38 +0000565 if (NetRestartWrap) {
wdenk2d966952002-10-31 22:12:35 +0000566 NetRestartWrap = 0;
Remy Bohmer67b96e82009-10-28 22:13:39 +0100567 if (NetDevExists) {
Luca Ceresoli4f63acd2011-05-11 03:59:56 +0000568 NetSetTimeout(10000UL, startAgainTimeout);
Joe Hershbergerece223b2012-05-23 07:59:15 +0000569 net_set_udp_handler(NULL);
wdenk6e592382004-04-18 17:39:38 +0000570 } else {
Joe Hershberger22f6e992012-05-23 07:59:14 +0000571 net_set_state(NETLOOP_FAIL);
wdenk2d966952002-10-31 22:12:35 +0000572 }
wdenk6e592382004-04-18 17:39:38 +0000573 } else {
Joe Hershberger22f6e992012-05-23 07:59:14 +0000574 net_set_state(NETLOOP_RESTART);
wdenk2d966952002-10-31 22:12:35 +0000575 }
wdenk2d966952002-10-31 22:12:35 +0000576}
577
578/**********************************************************************/
579/*
580 * Miscelaneous bits.
581 */
582
Joe Hershbergerece223b2012-05-23 07:59:15 +0000583static void dummy_handler(uchar *pkt, unsigned dport,
584 IPaddr_t sip, unsigned sport,
585 unsigned len)
Joe Hershbergerd280d3f2012-05-23 07:58:01 +0000586{
Joe Hershbergerd280d3f2012-05-23 07:58:01 +0000587}
588
Joe Hershbergerece223b2012-05-23 07:59:15 +0000589rxhand_f *net_get_udp_handler(void)
wdenk2d966952002-10-31 22:12:35 +0000590{
Joe Hershbergerece223b2012-05-23 07:59:15 +0000591 return udp_packet_handler;
592}
593
594void net_set_udp_handler(rxhand_f *f)
595{
596 if (f == NULL)
597 udp_packet_handler = dummy_handler;
598 else
599 udp_packet_handler = f;
600}
601
602rxhand_f *net_get_arp_handler(void)
603{
604 return arp_packet_handler;
605}
606
607void net_set_arp_handler(rxhand_f *f)
608{
609 if (f == NULL)
610 arp_packet_handler = dummy_handler;
611 else
612 arp_packet_handler = f;
wdenk2d966952002-10-31 22:12:35 +0000613}
614
Simon Glass39bccd22011-10-26 14:18:38 +0000615#ifdef CONFIG_CMD_TFTPPUT
Simon Glass4793ee62011-10-24 18:00:01 +0000616void net_set_icmp_handler(rxhand_icmp_f *f)
617{
618 packet_icmp_handler = f;
619}
Simon Glass39bccd22011-10-26 14:18:38 +0000620#endif
wdenk2d966952002-10-31 22:12:35 +0000621
622void
Luca Ceresoli6b147d12011-05-04 02:40:44 +0000623NetSetTimeout(ulong iv, thand_f *f)
wdenk2d966952002-10-31 22:12:35 +0000624{
625 if (iv == 0) {
626 timeHandler = (thand_f *)0;
627 } else {
628 timeHandler = f;
wdenke0ac62d2003-08-17 18:55:18 +0000629 timeStart = get_timer(0);
630 timeDelta = iv;
wdenk2d966952002-10-31 22:12:35 +0000631 }
632}
633
Joe Hershberger206d07f2012-05-23 07:58:10 +0000634int NetSendUDPPacket(uchar *ether, IPaddr_t dest, int dport, int sport,
635 int payload_len)
wdenk73a8b272003-06-05 19:27:42 +0000636{
wdenka3d991b2004-04-15 21:48:45 +0000637 uchar *pkt;
Joe Hershberger92146372012-05-23 07:59:08 +0000638 int eth_hdr_size;
639 int pkt_hdr_size;
wdenka3d991b2004-04-15 21:48:45 +0000640
Joe Hershberger46c495d2012-05-23 07:59:22 +0000641 /* make sure the NetTxPacket is initialized (NetInit() was called) */
642 assert(NetTxPacket != NULL);
643 if (NetTxPacket == NULL)
644 return -1;
645
wdenk73a8b272003-06-05 19:27:42 +0000646 /* convert to new style broadcast */
647 if (dest == 0)
648 dest = 0xFFFFFFFF;
wdenk2d966952002-10-31 22:12:35 +0000649
wdenk73a8b272003-06-05 19:27:42 +0000650 /* if broadcast, make the ether address a broadcast and don't do ARP */
651 if (dest == 0xFFFFFFFF)
652 ether = NetBcastAddr;
653
Joe Hershbergere94070c2012-05-23 07:59:24 +0000654 pkt = (uchar *)NetTxPacket;
wdenk73a8b272003-06-05 19:27:42 +0000655
Joe Hershberger92146372012-05-23 07:59:08 +0000656 eth_hdr_size = NetSetEther(pkt, ether, PROT_IP);
657 pkt += eth_hdr_size;
658 net_set_udp_header(pkt, dest, dport, sport, payload_len);
659 pkt_hdr_size = eth_hdr_size + IP_UDP_HDR_SIZE;
Robin Getz0ebf04c2009-07-23 03:01:03 -0400660
Joe Hershbergere94070c2012-05-23 07:59:24 +0000661 /* if MAC address was not discovered yet, do an ARP request */
662 if (memcmp(ether, NetEtherNullAddr, 6) == 0) {
Joe Hershberger92146372012-05-23 07:59:08 +0000663 debug("sending ARP for %pI4\n", &dest);
664
665 /* save the ip and eth addr for the packet to send after arp */
wdenk73a8b272003-06-05 19:27:42 +0000666 NetArpWaitPacketIP = dest;
667 NetArpWaitPacketMAC = ether;
wdenka3d991b2004-04-15 21:48:45 +0000668
wdenk73a8b272003-06-05 19:27:42 +0000669 /* size of the waiting packet */
Joe Hershberger92146372012-05-23 07:59:08 +0000670 NetArpWaitTxPacketSize = pkt_hdr_size + payload_len;
wdenk73a8b272003-06-05 19:27:42 +0000671
672 /* and do the ARP request */
673 NetArpWaitTry = 1;
674 NetArpWaitTimerStart = get_timer(0);
675 ArpRequest();
676 return 1; /* waiting */
Joe Hershberger92146372012-05-23 07:59:08 +0000677 } else {
678 debug("sending UDP to %pI4/%pM\n", &dest, ether);
Joe Hershbergeradf5d932012-05-23 07:59:13 +0000679 NetSendPacket(NetTxPacket, pkt_hdr_size + payload_len);
Joe Hershberger92146372012-05-23 07:59:08 +0000680 return 0; /* transmitted */
wdenk73a8b272003-06-05 19:27:42 +0000681 }
wdenk73a8b272003-06-05 19:27:42 +0000682}
683
Alessandro Rubini5cfaa4e2009-08-07 13:58:56 +0200684#ifdef CONFIG_IP_DEFRAG
685/*
686 * This function collects fragments in a single packet, according
687 * to the algorithm in RFC815. It returns NULL or the pointer to
688 * a complete packet, in static storage
689 */
690#ifndef CONFIG_NET_MAXDEFRAG
691#define CONFIG_NET_MAXDEFRAG 16384
692#endif
693/*
694 * MAXDEFRAG, above, is chosen in the config file and is real data
695 * so we need to add the NFS overhead, which is more than TFTP.
696 * To use sizeof in the internal unnamed structures, we need a real
697 * instance (can't do "sizeof(struct rpc_t.u.reply))", unfortunately).
698 * The compiler doesn't complain nor allocates the actual structure
699 */
700static struct rpc_t rpc_specimen;
701#define IP_PKTSIZE (CONFIG_NET_MAXDEFRAG + sizeof(rpc_specimen.u.reply))
702
Joe Hershbergerc5c59df2012-05-23 07:58:05 +0000703#define IP_MAXUDP (IP_PKTSIZE - IP_HDR_SIZE)
Alessandro Rubini5cfaa4e2009-08-07 13:58:56 +0200704
705/*
706 * this is the packet being assembled, either data or frag control.
707 * Fragments go by 8 bytes, so this union must be 8 bytes long
708 */
709struct hole {
710 /* first_byte is address of this structure */
711 u16 last_byte; /* last byte in this hole + 1 (begin of next hole) */
712 u16 next_hole; /* index of next (in 8-b blocks), 0 == none */
713 u16 prev_hole; /* index of prev, 0 == none */
714 u16 unused;
715};
716
Joe Hershberger594c26f2012-05-23 07:58:04 +0000717static struct ip_udp_hdr *__NetDefragment(struct ip_udp_hdr *ip, int *lenp)
Alessandro Rubini5cfaa4e2009-08-07 13:58:56 +0200718{
Joe Hershberger48522bb2012-05-15 08:59:08 +0000719 static uchar pkt_buff[IP_PKTSIZE] __aligned(PKTALIGN);
Alessandro Rubini5cfaa4e2009-08-07 13:58:56 +0200720 static u16 first_hole, total_len;
721 struct hole *payload, *thisfrag, *h, *newh;
Joe Hershberger594c26f2012-05-23 07:58:04 +0000722 struct ip_udp_hdr *localip = (struct ip_udp_hdr *)pkt_buff;
Alessandro Rubini5cfaa4e2009-08-07 13:58:56 +0200723 uchar *indata = (uchar *)ip;
724 int offset8, start, len, done = 0;
725 u16 ip_off = ntohs(ip->ip_off);
726
727 /* payload starts after IP header, this fragment is in there */
Joe Hershbergerc5c59df2012-05-23 07:58:05 +0000728 payload = (struct hole *)(pkt_buff + IP_HDR_SIZE);
Alessandro Rubini5cfaa4e2009-08-07 13:58:56 +0200729 offset8 = (ip_off & IP_OFFS);
730 thisfrag = payload + offset8;
731 start = offset8 * 8;
Joe Hershbergerc5c59df2012-05-23 07:58:05 +0000732 len = ntohs(ip->ip_len) - IP_HDR_SIZE;
Alessandro Rubini5cfaa4e2009-08-07 13:58:56 +0200733
734 if (start + len > IP_MAXUDP) /* fragment extends too far */
735 return NULL;
736
737 if (!total_len || localip->ip_id != ip->ip_id) {
738 /* new (or different) packet, reset structs */
739 total_len = 0xffff;
740 payload[0].last_byte = ~0;
741 payload[0].next_hole = 0;
742 payload[0].prev_hole = 0;
743 first_hole = 0;
744 /* any IP header will work, copy the first we received */
Joe Hershbergerc5c59df2012-05-23 07:58:05 +0000745 memcpy(localip, ip, IP_HDR_SIZE);
Alessandro Rubini5cfaa4e2009-08-07 13:58:56 +0200746 }
747
748 /*
749 * What follows is the reassembly algorithm. We use the payload
750 * array as a linked list of hole descriptors, as each hole starts
751 * at a multiple of 8 bytes. However, last byte can be whatever value,
752 * so it is represented as byte count, not as 8-byte blocks.
753 */
754
755 h = payload + first_hole;
756 while (h->last_byte < start) {
757 if (!h->next_hole) {
758 /* no hole that far away */
759 return NULL;
760 }
761 h = payload + h->next_hole;
762 }
763
Fillod Stephanee397e592010-06-11 19:26:43 +0200764 /* last fragment may be 1..7 bytes, the "+7" forces acceptance */
765 if (offset8 + ((len + 7) / 8) <= h - payload) {
Alessandro Rubini5cfaa4e2009-08-07 13:58:56 +0200766 /* no overlap with holes (dup fragment?) */
767 return NULL;
768 }
769
770 if (!(ip_off & IP_FLAGS_MFRAG)) {
771 /* no more fragmentss: truncate this (last) hole */
772 total_len = start + len;
773 h->last_byte = start + len;
774 }
775
776 /*
777 * There is some overlap: fix the hole list. This code doesn't
778 * deal with a fragment that overlaps with two different holes
779 * (thus being a superset of a previously-received fragment).
780 */
781
Luca Ceresoli4f63acd2011-05-11 03:59:56 +0000782 if ((h >= thisfrag) && (h->last_byte <= start + len)) {
Alessandro Rubini5cfaa4e2009-08-07 13:58:56 +0200783 /* complete overlap with hole: remove hole */
784 if (!h->prev_hole && !h->next_hole) {
785 /* last remaining hole */
786 done = 1;
787 } else if (!h->prev_hole) {
788 /* first hole */
789 first_hole = h->next_hole;
790 payload[h->next_hole].prev_hole = 0;
791 } else if (!h->next_hole) {
792 /* last hole */
793 payload[h->prev_hole].next_hole = 0;
794 } else {
795 /* in the middle of the list */
796 payload[h->next_hole].prev_hole = h->prev_hole;
797 payload[h->prev_hole].next_hole = h->next_hole;
798 }
799
800 } else if (h->last_byte <= start + len) {
801 /* overlaps with final part of the hole: shorten this hole */
802 h->last_byte = start;
803
804 } else if (h >= thisfrag) {
805 /* overlaps with initial part of the hole: move this hole */
806 newh = thisfrag + (len / 8);
807 *newh = *h;
808 h = newh;
809 if (h->next_hole)
810 payload[h->next_hole].prev_hole = (h - payload);
811 if (h->prev_hole)
812 payload[h->prev_hole].next_hole = (h - payload);
813 else
814 first_hole = (h - payload);
815
816 } else {
817 /* fragment sits in the middle: split the hole */
818 newh = thisfrag + (len / 8);
819 *newh = *h;
820 h->last_byte = start;
821 h->next_hole = (newh - payload);
822 newh->prev_hole = (h - payload);
823 if (newh->next_hole)
824 payload[newh->next_hole].prev_hole = (newh - payload);
825 }
826
827 /* finally copy this fragment and possibly return whole packet */
Joe Hershbergerc5c59df2012-05-23 07:58:05 +0000828 memcpy((uchar *)thisfrag, indata + IP_HDR_SIZE, len);
Alessandro Rubini5cfaa4e2009-08-07 13:58:56 +0200829 if (!done)
830 return NULL;
831
832 localip->ip_len = htons(total_len);
Joe Hershbergerc5c59df2012-05-23 07:58:05 +0000833 *lenp = total_len + IP_HDR_SIZE;
Alessandro Rubini5cfaa4e2009-08-07 13:58:56 +0200834 return localip;
835}
836
Joe Hershberger594c26f2012-05-23 07:58:04 +0000837static inline struct ip_udp_hdr *NetDefragment(struct ip_udp_hdr *ip, int *lenp)
Alessandro Rubini5cfaa4e2009-08-07 13:58:56 +0200838{
839 u16 ip_off = ntohs(ip->ip_off);
840 if (!(ip_off & (IP_OFFS | IP_FLAGS_MFRAG)))
841 return ip; /* not a fragment */
842 return __NetDefragment(ip, lenp);
843}
844
845#else /* !CONFIG_IP_DEFRAG */
846
Joe Hershberger594c26f2012-05-23 07:58:04 +0000847static inline struct ip_udp_hdr *NetDefragment(struct ip_udp_hdr *ip, int *lenp)
Alessandro Rubini5cfaa4e2009-08-07 13:58:56 +0200848{
849 u16 ip_off = ntohs(ip->ip_off);
850 if (!(ip_off & (IP_OFFS | IP_FLAGS_MFRAG)))
851 return ip; /* not a fragment */
852 return NULL;
853}
854#endif
wdenka3d991b2004-04-15 21:48:45 +0000855
Simon Glass8f79bb12011-10-24 18:00:00 +0000856/**
857 * Receive an ICMP packet. We deal with REDIRECT and PING here, and silently
858 * drop others.
859 *
860 * @parma ip IP packet containing the ICMP
861 */
Joe Hershberger594c26f2012-05-23 07:58:04 +0000862static void receive_icmp(struct ip_udp_hdr *ip, int len,
Joe Hershbergercb487f52012-05-23 07:58:06 +0000863 IPaddr_t src_ip, struct ethernet_hdr *et)
Simon Glass8f79bb12011-10-24 18:00:00 +0000864{
Joe Hershbergere0a63072012-05-23 07:58:09 +0000865 struct icmp_hdr *icmph = (struct icmp_hdr *)&ip->udp_src;
Simon Glass8f79bb12011-10-24 18:00:00 +0000866
867 switch (icmph->type) {
868 case ICMP_REDIRECT:
869 if (icmph->code != ICMP_REDIR_HOST)
870 return;
871 printf(" ICMP Host Redirect to %pI4 ",
872 &icmph->un.gateway);
873 break;
Simon Glass8f79bb12011-10-24 18:00:00 +0000874 default:
Joe Hershbergera36b12f2012-05-23 07:58:02 +0000875#if defined(CONFIG_CMD_PING)
876 ping_receive(et, ip, len);
877#endif
Simon Glass39bccd22011-10-26 14:18:38 +0000878#ifdef CONFIG_CMD_TFTPPUT
Simon Glass4793ee62011-10-24 18:00:01 +0000879 if (packet_icmp_handler)
880 packet_icmp_handler(icmph->type, icmph->code,
881 ntohs(ip->udp_dst), src_ip, ntohs(ip->udp_src),
882 icmph->un.data, ntohs(ip->udp_len));
Simon Glass39bccd22011-10-26 14:18:38 +0000883#endif
Simon Glass8f79bb12011-10-24 18:00:00 +0000884 break;
885 }
886}
887
wdenk2d966952002-10-31 22:12:35 +0000888void
Joe Hershbergerdb288a92012-05-15 08:59:04 +0000889NetReceive(uchar *inpkt, int len)
wdenk2d966952002-10-31 22:12:35 +0000890{
Joe Hershbergercb487f52012-05-23 07:58:06 +0000891 struct ethernet_hdr *et;
Joe Hershberger594c26f2012-05-23 07:58:04 +0000892 struct ip_udp_hdr *ip;
Joe Hershberger8d353eb2012-05-23 07:58:12 +0000893 IPaddr_t dst_ip;
Luca Ceresoli03eb1292011-04-18 06:19:50 +0000894 IPaddr_t src_ip;
Joe Hershberger8d353eb2012-05-23 07:58:12 +0000895 int eth_proto;
Jon Loeliger643d1ab2007-07-09 17:45:14 -0500896#if defined(CONFIG_CMD_CDP)
wdenka3d991b2004-04-15 21:48:45 +0000897 int iscdp;
898#endif
899 ushort cti = 0, vlanid = VLAN_NONE, myvlanid, mynvlanid;
wdenk2d966952002-10-31 22:12:35 +0000900
Robin Getz0ebf04c2009-07-23 03:01:03 -0400901 debug("packet received\n");
wdenka3d991b2004-04-15 21:48:45 +0000902
Mike Frysingerd9bec9f2009-07-18 21:04:08 -0400903 NetRxPacket = inpkt;
904 NetRxPacketLen = len;
Joe Hershbergercb487f52012-05-23 07:58:06 +0000905 et = (struct ethernet_hdr *)inpkt;
wdenka3d991b2004-04-15 21:48:45 +0000906
907 /* too small packet? */
908 if (len < ETHER_HDR_SIZE)
909 return;
910
Rafal Jaworowskif85b6072007-12-27 18:19:02 +0100911#ifdef CONFIG_API
912 if (push_packet) {
913 (*push_packet)(inpkt, len);
914 return;
915 }
916#endif
917
Jon Loeliger643d1ab2007-07-09 17:45:14 -0500918#if defined(CONFIG_CMD_CDP)
wdenka3d991b2004-04-15 21:48:45 +0000919 /* keep track if packet is CDP */
Joe Hershberger17351882012-05-23 07:58:00 +0000920 iscdp = is_cdp_packet(et->et_dest);
wdenka3d991b2004-04-15 21:48:45 +0000921#endif
922
923 myvlanid = ntohs(NetOurVLAN);
924 if (myvlanid == (ushort)-1)
925 myvlanid = VLAN_NONE;
926 mynvlanid = ntohs(NetOurNativeVLAN);
927 if (mynvlanid == (ushort)-1)
928 mynvlanid = VLAN_NONE;
wdenk2d966952002-10-31 22:12:35 +0000929
Joe Hershberger8d353eb2012-05-23 07:58:12 +0000930 eth_proto = ntohs(et->et_protlen);
wdenk2d966952002-10-31 22:12:35 +0000931
Robin Getz0ebf04c2009-07-23 03:01:03 -0400932 debug("packet received\n");
wdenka3d991b2004-04-15 21:48:45 +0000933
Joe Hershberger8d353eb2012-05-23 07:58:12 +0000934 if (eth_proto < 1514) {
Joe Hershbergercb487f52012-05-23 07:58:06 +0000935 struct e802_hdr *et802 = (struct e802_hdr *)et;
wdenk2d966952002-10-31 22:12:35 +0000936 /*
Joe Hershbergerda5ebe22012-05-23 07:58:11 +0000937 * Got a 802.2 packet. Check the other protocol field.
938 * XXX VLAN over 802.2+SNAP not implemented!
wdenk2d966952002-10-31 22:12:35 +0000939 */
Joe Hershberger8d353eb2012-05-23 07:58:12 +0000940 eth_proto = ntohs(et802->et_prot);
wdenka3d991b2004-04-15 21:48:45 +0000941
Joe Hershberger594c26f2012-05-23 07:58:04 +0000942 ip = (struct ip_udp_hdr *)(inpkt + E802_HDR_SIZE);
wdenk2d966952002-10-31 22:12:35 +0000943 len -= E802_HDR_SIZE;
wdenka3d991b2004-04-15 21:48:45 +0000944
Joe Hershberger8d353eb2012-05-23 07:58:12 +0000945 } else if (eth_proto != PROT_VLAN) { /* normal packet */
Joe Hershberger594c26f2012-05-23 07:58:04 +0000946 ip = (struct ip_udp_hdr *)(inpkt + ETHER_HDR_SIZE);
wdenk2d966952002-10-31 22:12:35 +0000947 len -= ETHER_HDR_SIZE;
wdenka3d991b2004-04-15 21:48:45 +0000948
949 } else { /* VLAN packet */
Joe Hershbergerc68cca32012-05-23 07:58:07 +0000950 struct vlan_ethernet_hdr *vet =
951 (struct vlan_ethernet_hdr *)et;
wdenka3d991b2004-04-15 21:48:45 +0000952
Robin Getz0ebf04c2009-07-23 03:01:03 -0400953 debug("VLAN packet received\n");
954
wdenka3d991b2004-04-15 21:48:45 +0000955 /* too small packet? */
956 if (len < VLAN_ETHER_HDR_SIZE)
957 return;
958
959 /* if no VLAN active */
960 if ((ntohs(NetOurVLAN) & VLAN_IDMASK) == VLAN_NONE
Jon Loeliger643d1ab2007-07-09 17:45:14 -0500961#if defined(CONFIG_CMD_CDP)
wdenka3d991b2004-04-15 21:48:45 +0000962 && iscdp == 0
963#endif
964 )
965 return;
966
967 cti = ntohs(vet->vet_tag);
968 vlanid = cti & VLAN_IDMASK;
Joe Hershberger8d353eb2012-05-23 07:58:12 +0000969 eth_proto = ntohs(vet->vet_type);
wdenka3d991b2004-04-15 21:48:45 +0000970
Joe Hershberger594c26f2012-05-23 07:58:04 +0000971 ip = (struct ip_udp_hdr *)(inpkt + VLAN_ETHER_HDR_SIZE);
wdenka3d991b2004-04-15 21:48:45 +0000972 len -= VLAN_ETHER_HDR_SIZE;
wdenk2d966952002-10-31 22:12:35 +0000973 }
974
Joe Hershberger8d353eb2012-05-23 07:58:12 +0000975 debug("Receive from protocol 0x%x\n", eth_proto);
wdenk2d966952002-10-31 22:12:35 +0000976
Jon Loeliger643d1ab2007-07-09 17:45:14 -0500977#if defined(CONFIG_CMD_CDP)
wdenka3d991b2004-04-15 21:48:45 +0000978 if (iscdp) {
Joe Hershberger0b4c5ff2012-05-23 07:58:13 +0000979 cdp_receive((uchar *)ip, len);
wdenka3d991b2004-04-15 21:48:45 +0000980 return;
981 }
982#endif
983
984 if ((myvlanid & VLAN_IDMASK) != VLAN_NONE) {
985 if (vlanid == VLAN_NONE)
986 vlanid = (mynvlanid & VLAN_IDMASK);
987 /* not matched? */
988 if (vlanid != (myvlanid & VLAN_IDMASK))
989 return;
990 }
991
Joe Hershberger8d353eb2012-05-23 07:58:12 +0000992 switch (eth_proto) {
wdenk2d966952002-10-31 22:12:35 +0000993
994 case PROT_ARP:
Joe Hershbergerd280d3f2012-05-23 07:58:01 +0000995 ArpReceive(et, ip, len);
wdenk289f9322005-01-12 00:15:14 +0000996 break;
wdenk2d966952002-10-31 22:12:35 +0000997
Peter Tyserbf6cb242010-09-30 11:25:48 -0500998#ifdef CONFIG_CMD_RARP
wdenk2d966952002-10-31 22:12:35 +0000999 case PROT_RARP:
Joe Hershberger8b9c5322012-05-23 07:58:03 +00001000 rarp_receive(ip, len);
wdenk2d966952002-10-31 22:12:35 +00001001 break;
Peter Tyserbf6cb242010-09-30 11:25:48 -05001002#endif
wdenk2d966952002-10-31 22:12:35 +00001003 case PROT_IP:
Robin Getz0ebf04c2009-07-23 03:01:03 -04001004 debug("Got IP\n");
Alessandro Rubini5cfaa4e2009-08-07 13:58:56 +02001005 /* Before we start poking the header, make sure it is there */
Joe Hershberger594c26f2012-05-23 07:58:04 +00001006 if (len < IP_UDP_HDR_SIZE) {
1007 debug("len bad %d < %lu\n", len,
1008 (ulong)IP_UDP_HDR_SIZE);
wdenk2d966952002-10-31 22:12:35 +00001009 return;
1010 }
Alessandro Rubini5cfaa4e2009-08-07 13:58:56 +02001011 /* Check the packet length */
wdenk2d966952002-10-31 22:12:35 +00001012 if (len < ntohs(ip->ip_len)) {
1013 printf("len bad %d < %d\n", len, ntohs(ip->ip_len));
1014 return;
1015 }
1016 len = ntohs(ip->ip_len);
Robin Getz0ebf04c2009-07-23 03:01:03 -04001017 debug("len=%d, v=%02x\n", len, ip->ip_hl_v & 0xff);
1018
Alessandro Rubini5cfaa4e2009-08-07 13:58:56 +02001019 /* Can't deal with anything except IPv4 */
Luca Ceresolid3c65b02011-05-04 02:40:43 +00001020 if ((ip->ip_hl_v & 0xf0) != 0x40)
wdenk2d966952002-10-31 22:12:35 +00001021 return;
Alessandro Rubini5cfaa4e2009-08-07 13:58:56 +02001022 /* Can't deal with IP options (headers != 20 bytes) */
Luca Ceresolid3c65b02011-05-04 02:40:43 +00001023 if ((ip->ip_hl_v & 0x0f) > 0x05)
Remy Bohmer6b52cfe2008-06-03 15:48:17 +02001024 return;
Alessandro Rubini5cfaa4e2009-08-07 13:58:56 +02001025 /* Check the Checksum of the header */
Joe Hershbergerc5c59df2012-05-23 07:58:05 +00001026 if (!NetCksumOk((uchar *)ip, IP_HDR_SIZE / 2)) {
Luca Ceresoli4f63acd2011-05-11 03:59:56 +00001027 puts("checksum bad\n");
wdenk2d966952002-10-31 22:12:35 +00001028 return;
1029 }
Alessandro Rubini5cfaa4e2009-08-07 13:58:56 +02001030 /* If it is not for us, ignore it */
Joe Hershberger8d353eb2012-05-23 07:58:12 +00001031 dst_ip = NetReadIP(&ip->ip_dst);
1032 if (NetOurIP && dst_ip != NetOurIP && dst_ip != 0xFFFFFFFF) {
David Updegraff53a5c422007-06-11 10:41:07 -05001033#ifdef CONFIG_MCAST_TFTP
Joe Hershberger8d353eb2012-05-23 07:58:12 +00001034 if (Mcast_addr != dst_ip)
David Updegraff53a5c422007-06-11 10:41:07 -05001035#endif
Luca Ceresolic819abe2011-05-04 02:40:46 +00001036 return;
wdenk2d966952002-10-31 22:12:35 +00001037 }
Luca Ceresoli03eb1292011-04-18 06:19:50 +00001038 /* Read source IP address for later use */
1039 src_ip = NetReadIP(&ip->ip_src);
wdenk2d966952002-10-31 22:12:35 +00001040 /*
Alessandro Rubini5cfaa4e2009-08-07 13:58:56 +02001041 * The function returns the unchanged packet if it's not
1042 * a fragment, and either the complete packet or NULL if
1043 * it is a fragment (if !CONFIG_IP_DEFRAG, it returns NULL)
1044 */
Luca Ceresoliccb9ebe2011-05-04 02:40:47 +00001045 ip = NetDefragment(ip, &len);
1046 if (!ip)
Alessandro Rubini5cfaa4e2009-08-07 13:58:56 +02001047 return;
1048 /*
wdenk2d966952002-10-31 22:12:35 +00001049 * watch for ICMP host redirects
1050 *
wdenk8bde7f72003-06-27 21:31:46 +00001051 * There is no real handler code (yet). We just watch
1052 * for ICMP host redirect messages. In case anybody
1053 * sees these messages: please contact me
1054 * (wd@denx.de), or - even better - send me the
1055 * necessary fixes :-)
wdenk2d966952002-10-31 22:12:35 +00001056 *
wdenk8bde7f72003-06-27 21:31:46 +00001057 * Note: in all cases where I have seen this so far
1058 * it was a problem with the router configuration,
1059 * for instance when a router was configured in the
1060 * BOOTP reply, but the TFTP server was on the same
1061 * subnet. So this is probably a warning that your
1062 * configuration might be wrong. But I'm not really
1063 * sure if there aren't any other situations.
Simon Glass4793ee62011-10-24 18:00:01 +00001064 *
1065 * Simon Glass <sjg@chromium.org>: We get an ICMP when
1066 * we send a tftp packet to a dead connection, or when
1067 * there is no server at the other end.
wdenk2d966952002-10-31 22:12:35 +00001068 */
1069 if (ip->ip_p == IPPROTO_ICMP) {
Simon Glass8f79bb12011-10-24 18:00:00 +00001070 receive_icmp(ip, len, src_ip, et);
1071 return;
wdenk2d966952002-10-31 22:12:35 +00001072 } else if (ip->ip_p != IPPROTO_UDP) { /* Only UDP packets */
1073 return;
1074 }
1075
Stefan Roese8534bf92005-08-12 20:06:52 +02001076#ifdef CONFIG_UDP_CHECKSUM
1077 if (ip->udp_xsum != 0) {
Wolfgang Denkb2f50802005-08-12 23:43:12 +02001078 ulong xsum;
Stefan Roese8534bf92005-08-12 20:06:52 +02001079 ushort *sumptr;
1080 ushort sumlen;
1081
1082 xsum = ip->ip_p;
1083 xsum += (ntohs(ip->udp_len));
1084 xsum += (ntohl(ip->ip_src) >> 16) & 0x0000ffff;
1085 xsum += (ntohl(ip->ip_src) >> 0) & 0x0000ffff;
1086 xsum += (ntohl(ip->ip_dst) >> 16) & 0x0000ffff;
1087 xsum += (ntohl(ip->ip_dst) >> 0) & 0x0000ffff;
1088
1089 sumlen = ntohs(ip->udp_len);
1090 sumptr = (ushort *) &(ip->udp_src);
1091
1092 while (sumlen > 1) {
Wolfgang Denkb2f50802005-08-12 23:43:12 +02001093 ushort sumdata;
Stefan Roese8534bf92005-08-12 20:06:52 +02001094
1095 sumdata = *sumptr++;
1096 xsum += ntohs(sumdata);
1097 sumlen -= 2;
1098 }
1099 if (sumlen > 0) {
Wolfgang Denkb2f50802005-08-12 23:43:12 +02001100 ushort sumdata;
Stefan Roese8534bf92005-08-12 20:06:52 +02001101
1102 sumdata = *(unsigned char *) sumptr;
Wolfgang Denkb2f50802005-08-12 23:43:12 +02001103 sumdata = (sumdata << 8) & 0xff00;
Stefan Roese8534bf92005-08-12 20:06:52 +02001104 xsum += sumdata;
1105 }
1106 while ((xsum >> 16) != 0) {
Luca Ceresoli3e38e422011-05-11 03:59:54 +00001107 xsum = (xsum & 0x0000ffff) +
1108 ((xsum >> 16) & 0x0000ffff);
Stefan Roese8534bf92005-08-12 20:06:52 +02001109 }
1110 if ((xsum != 0x00000000) && (xsum != 0x0000ffff)) {
Wolfgang Denk9b55a252008-07-11 01:16:00 +02001111 printf(" UDP wrong checksum %08lx %08x\n",
1112 xsum, ntohs(ip->udp_xsum));
Stefan Roese8534bf92005-08-12 20:06:52 +02001113 return;
1114 }
1115 }
1116#endif
1117
David Updegraff53a5c422007-06-11 10:41:07 -05001118
wdenk68ceb292004-08-02 21:11:11 +00001119#ifdef CONFIG_NETCONSOLE
Joe Hershberger594c26f2012-05-23 07:58:04 +00001120 nc_input_packet((uchar *)ip + IP_UDP_HDR_SIZE,
1121 ntohs(ip->udp_dst),
1122 ntohs(ip->udp_src),
1123 ntohs(ip->udp_len) - UDP_HDR_SIZE);
wdenk68ceb292004-08-02 21:11:11 +00001124#endif
wdenk2d966952002-10-31 22:12:35 +00001125 /*
1126 * IP header OK. Pass the packet to the current handler.
1127 */
Joe Hershbergerece223b2012-05-23 07:59:15 +00001128 (*udp_packet_handler)((uchar *)ip + IP_UDP_HDR_SIZE,
1129 ntohs(ip->udp_dst),
1130 src_ip,
1131 ntohs(ip->udp_src),
1132 ntohs(ip->udp_len) - UDP_HDR_SIZE);
wdenk2d966952002-10-31 22:12:35 +00001133 break;
1134 }
1135}
1136
1137
1138/**********************************************************************/
1139
Simon Glasse4bf0c52011-10-24 18:00:02 +00001140static int net_check_prereq(enum proto_t protocol)
wdenk2d966952002-10-31 22:12:35 +00001141{
1142 switch (protocol) {
wdenk6e592382004-04-18 17:39:38 +00001143 /* Fall through */
Jon Loeliger643d1ab2007-07-09 17:45:14 -05001144#if defined(CONFIG_CMD_PING)
wdenk73a8b272003-06-05 19:27:42 +00001145 case PING:
wdenk6e592382004-04-18 17:39:38 +00001146 if (NetPingIP == 0) {
Luca Ceresoli4f63acd2011-05-11 03:59:56 +00001147 puts("*** ERROR: ping address not given\n");
Luca Ceresoli92895de2011-05-04 02:40:45 +00001148 return 1;
wdenk6e592382004-04-18 17:39:38 +00001149 }
1150 goto common;
wdenk73a8b272003-06-05 19:27:42 +00001151#endif
Jon Loeliger643d1ab2007-07-09 17:45:14 -05001152#if defined(CONFIG_CMD_SNTP)
wdenkea287de2005-04-01 00:25:43 +00001153 case SNTP:
1154 if (NetNtpServerIP == 0) {
Luca Ceresoli4f63acd2011-05-11 03:59:56 +00001155 puts("*** ERROR: NTP server address not given\n");
Luca Ceresoli92895de2011-05-04 02:40:45 +00001156 return 1;
wdenkea287de2005-04-01 00:25:43 +00001157 }
1158 goto common;
1159#endif
Robin Getz1a32bf42009-07-20 14:53:54 -04001160#if defined(CONFIG_CMD_DNS)
1161 case DNS:
1162 if (NetOurDNSIP == 0) {
1163 puts("*** ERROR: DNS server address not given\n");
1164 return 1;
1165 }
1166 goto common;
1167#endif
Jon Loeliger643d1ab2007-07-09 17:45:14 -05001168#if defined(CONFIG_CMD_NFS)
wdenkcbd8a352004-02-24 02:00:03 +00001169 case NFS:
1170#endif
Simon Glasse4bf0c52011-10-24 18:00:02 +00001171 case TFTPGET:
Simon Glass1fb7cd42011-10-24 18:00:07 +00001172 case TFTPPUT:
wdenk6e592382004-04-18 17:39:38 +00001173 if (NetServerIP == 0) {
Luca Ceresoli4f63acd2011-05-11 03:59:56 +00001174 puts("*** ERROR: `serverip' not set\n");
Luca Ceresoli92895de2011-05-04 02:40:45 +00001175 return 1;
wdenk6e592382004-04-18 17:39:38 +00001176 }
Luca Ceresoli4f63acd2011-05-11 03:59:56 +00001177#if defined(CONFIG_CMD_PING) || defined(CONFIG_CMD_SNTP) || \
1178 defined(CONFIG_CMD_DNS)
1179common:
wdenk73a8b272003-06-05 19:27:42 +00001180#endif
Simon Guinot8b6bbe12011-05-01 23:38:40 +00001181 /* Fall through */
wdenk73a8b272003-06-05 19:27:42 +00001182
Simon Guinot8b6bbe12011-05-01 23:38:40 +00001183 case NETCONS:
Luca Ceresoli7a83af02011-05-17 00:03:40 +00001184 case TFTPSRV:
wdenk6e592382004-04-18 17:39:38 +00001185 if (NetOurIP == 0) {
Luca Ceresoli4f63acd2011-05-11 03:59:56 +00001186 puts("*** ERROR: `ipaddr' not set\n");
Luca Ceresoli92895de2011-05-04 02:40:45 +00001187 return 1;
wdenk6e592382004-04-18 17:39:38 +00001188 }
1189 /* Fall through */
wdenk2d966952002-10-31 22:12:35 +00001190
Peter Tyserbf6cb242010-09-30 11:25:48 -05001191#ifdef CONFIG_CMD_RARP
wdenk2d966952002-10-31 22:12:35 +00001192 case RARP:
Peter Tyserbf6cb242010-09-30 11:25:48 -05001193#endif
wdenk2d966952002-10-31 22:12:35 +00001194 case BOOTP:
wdenka3d991b2004-04-15 21:48:45 +00001195 case CDP:
Peter Tyserbf6cb242010-09-30 11:25:48 -05001196 case DHCP:
Luca Ceresoli4f63acd2011-05-11 03:59:56 +00001197 if (memcmp(NetOurEther, "\0\0\0\0\0\0", 6) == 0) {
Luca Ceresoli4f63acd2011-05-11 03:59:56 +00001198 int num = eth_get_dev_index();
wdenk2d966952002-10-31 22:12:35 +00001199
wdenk6e592382004-04-18 17:39:38 +00001200 switch (num) {
1201 case -1:
Luca Ceresoli4f63acd2011-05-11 03:59:56 +00001202 puts("*** ERROR: No ethernet found.\n");
Luca Ceresoli92895de2011-05-04 02:40:45 +00001203 return 1;
wdenk6e592382004-04-18 17:39:38 +00001204 case 0:
Luca Ceresoli4f63acd2011-05-11 03:59:56 +00001205 puts("*** ERROR: `ethaddr' not set\n");
wdenk2d966952002-10-31 22:12:35 +00001206 break;
wdenk6e592382004-04-18 17:39:38 +00001207 default:
Luca Ceresoli4f63acd2011-05-11 03:59:56 +00001208 printf("*** ERROR: `eth%daddr' not set\n",
wdenk2d966952002-10-31 22:12:35 +00001209 num);
1210 break;
wdenk2d966952002-10-31 22:12:35 +00001211 }
wdenk6e592382004-04-18 17:39:38 +00001212
Luca Ceresoli4f63acd2011-05-11 03:59:56 +00001213 NetStartAgain();
Luca Ceresoli92895de2011-05-04 02:40:45 +00001214 return 2;
wdenk6e592382004-04-18 17:39:38 +00001215 }
1216 /* Fall through */
1217 default:
Luca Ceresoli92895de2011-05-04 02:40:45 +00001218 return 0;
wdenk2d966952002-10-31 22:12:35 +00001219 }
Luca Ceresoli92895de2011-05-04 02:40:45 +00001220 return 0; /* OK */
wdenk2d966952002-10-31 22:12:35 +00001221}
1222/**********************************************************************/
1223
1224int
Luca Ceresoli6b147d12011-05-04 02:40:44 +00001225NetCksumOk(uchar *ptr, int len)
wdenk2d966952002-10-31 22:12:35 +00001226{
1227 return !((NetCksum(ptr, len) + 1) & 0xfffe);
1228}
1229
1230
1231unsigned
Luca Ceresoli6b147d12011-05-04 02:40:44 +00001232NetCksum(uchar *ptr, int len)
wdenk2d966952002-10-31 22:12:35 +00001233{
1234 ulong xsum;
Stefan Roese9d2a8732005-08-31 12:55:50 +02001235 ushort *p = (ushort *)ptr;
wdenk2d966952002-10-31 22:12:35 +00001236
1237 xsum = 0;
1238 while (len-- > 0)
Wolfgang Denk7bc5ee02005-08-26 01:36:03 +02001239 xsum += *p++;
wdenk2d966952002-10-31 22:12:35 +00001240 xsum = (xsum & 0xffff) + (xsum >> 16);
1241 xsum = (xsum & 0xffff) + (xsum >> 16);
Luca Ceresoli92895de2011-05-04 02:40:45 +00001242 return xsum & 0xffff;
wdenk2d966952002-10-31 22:12:35 +00001243}
1244
wdenka3d991b2004-04-15 21:48:45 +00001245int
1246NetEthHdrSize(void)
1247{
1248 ushort myvlanid;
wdenk2d966952002-10-31 22:12:35 +00001249
wdenka3d991b2004-04-15 21:48:45 +00001250 myvlanid = ntohs(NetOurVLAN);
1251 if (myvlanid == (ushort)-1)
1252 myvlanid = VLAN_NONE;
1253
Luca Ceresoli3e38e422011-05-11 03:59:54 +00001254 return ((myvlanid & VLAN_IDMASK) == VLAN_NONE) ? ETHER_HDR_SIZE :
1255 VLAN_ETHER_HDR_SIZE;
wdenka3d991b2004-04-15 21:48:45 +00001256}
1257
1258int
Joe Hershbergerdb288a92012-05-15 08:59:04 +00001259NetSetEther(uchar *xet, uchar * addr, uint prot)
wdenk2d966952002-10-31 22:12:35 +00001260{
Joe Hershbergercb487f52012-05-23 07:58:06 +00001261 struct ethernet_hdr *et = (struct ethernet_hdr *)xet;
wdenka3d991b2004-04-15 21:48:45 +00001262 ushort myvlanid;
1263
1264 myvlanid = ntohs(NetOurVLAN);
1265 if (myvlanid == (ushort)-1)
1266 myvlanid = VLAN_NONE;
wdenk2d966952002-10-31 22:12:35 +00001267
Luca Ceresoli4f63acd2011-05-11 03:59:56 +00001268 memcpy(et->et_dest, addr, 6);
1269 memcpy(et->et_src, NetOurEther, 6);
wdenka3d991b2004-04-15 21:48:45 +00001270 if ((myvlanid & VLAN_IDMASK) == VLAN_NONE) {
Luca Ceresolic819abe2011-05-04 02:40:46 +00001271 et->et_protlen = htons(prot);
wdenka3d991b2004-04-15 21:48:45 +00001272 return ETHER_HDR_SIZE;
1273 } else {
Joe Hershbergerc68cca32012-05-23 07:58:07 +00001274 struct vlan_ethernet_hdr *vet =
1275 (struct vlan_ethernet_hdr *)xet;
wdenk2d966952002-10-31 22:12:35 +00001276
wdenka3d991b2004-04-15 21:48:45 +00001277 vet->vet_vlan_type = htons(PROT_VLAN);
1278 vet->vet_tag = htons((0 << 5) | (myvlanid & VLAN_IDMASK));
1279 vet->vet_type = htons(prot);
1280 return VLAN_ETHER_HDR_SIZE;
1281 }
1282}
wdenk2d966952002-10-31 22:12:35 +00001283
Joe Hershbergere7111012012-05-23 07:59:16 +00001284int net_update_ether(struct ethernet_hdr *et, uchar *addr, uint prot)
1285{
1286 ushort protlen;
1287
1288 memcpy(et->et_dest, addr, 6);
1289 memcpy(et->et_src, NetOurEther, 6);
1290 protlen = ntohs(et->et_protlen);
1291 if (protlen == PROT_VLAN) {
1292 struct vlan_ethernet_hdr *vet =
1293 (struct vlan_ethernet_hdr *)et;
1294 vet->vet_type = htons(prot);
1295 return VLAN_ETHER_HDR_SIZE;
1296 } else if (protlen > 1514) {
1297 et->et_protlen = htons(prot);
1298 return ETHER_HDR_SIZE;
1299 } else {
1300 /* 802.2 + SNAP */
1301 struct e802_hdr *et802 = (struct e802_hdr *)et;
1302 et802->et_prot = htons(prot);
1303 return E802_HDR_SIZE;
1304 }
1305}
1306
Joe Hershberger4b11c912012-05-23 07:59:07 +00001307void net_set_ip_header(uchar *pkt, IPaddr_t dest, IPaddr_t source)
wdenk2d966952002-10-31 22:12:35 +00001308{
Joe Hershberger4b11c912012-05-23 07:59:07 +00001309 struct ip_udp_hdr *ip = (struct ip_udp_hdr *)pkt;
1310
1311 /*
1312 * Construct an IP header.
1313 */
1314 /* IP_HDR_SIZE / 4 (not including UDP) */
1315 ip->ip_hl_v = 0x45;
1316 ip->ip_tos = 0;
1317 ip->ip_len = htons(IP_HDR_SIZE);
1318 ip->ip_id = htons(NetIPID++);
1319 ip->ip_off = htons(IP_FLAGS_DFRAG); /* Don't fragment */
1320 ip->ip_ttl = 255;
1321 ip->ip_sum = 0;
1322 /* already in network byte order */
1323 NetCopyIP((void *)&ip->ip_src, &source);
1324 /* already in network byte order */
1325 NetCopyIP((void *)&ip->ip_dst, &dest);
1326}
1327
1328void net_set_udp_header(uchar *pkt, IPaddr_t dest, int dport, int sport,
1329 int len)
1330{
1331 struct ip_udp_hdr *ip = (struct ip_udp_hdr *)pkt;
wdenk2d966952002-10-31 22:12:35 +00001332
1333 /*
1334 * If the data is an odd number of bytes, zero the
1335 * byte after the last byte so that the checksum
1336 * will work.
1337 */
1338 if (len & 1)
Joe Hershberger4b11c912012-05-23 07:59:07 +00001339 pkt[IP_UDP_HDR_SIZE + len] = 0;
wdenk2d966952002-10-31 22:12:35 +00001340
Joe Hershberger4b11c912012-05-23 07:59:07 +00001341 net_set_ip_header(pkt, dest, NetOurIP);
Joe Hershberger594c26f2012-05-23 07:58:04 +00001342 ip->ip_len = htons(IP_UDP_HDR_SIZE + len);
Joe Hershberger4b11c912012-05-23 07:59:07 +00001343 ip->ip_p = IPPROTO_UDP;
1344 ip->ip_sum = ~NetCksum((uchar *)ip, IP_HDR_SIZE >> 1);
1345
wdenk2d966952002-10-31 22:12:35 +00001346 ip->udp_src = htons(sport);
1347 ip->udp_dst = htons(dport);
Joe Hershberger594c26f2012-05-23 07:58:04 +00001348 ip->udp_len = htons(UDP_HDR_SIZE + len);
wdenk2d966952002-10-31 22:12:35 +00001349 ip->udp_xsum = 0;
wdenk2d966952002-10-31 22:12:35 +00001350}
1351
Luca Ceresoli4f63acd2011-05-11 03:59:56 +00001352void copy_filename(char *dst, const char *src, int size)
wdenk2d966952002-10-31 22:12:35 +00001353{
1354 if (*src && (*src == '"')) {
1355 ++src;
1356 --size;
1357 }
1358
Luca Ceresolid3c65b02011-05-04 02:40:43 +00001359 while ((--size > 0) && *src && (*src != '"'))
wdenk2d966952002-10-31 22:12:35 +00001360 *dst++ = *src++;
wdenk2d966952002-10-31 22:12:35 +00001361 *dst = '\0';
1362}
1363
Luca Ceresoli3e38e422011-05-11 03:59:54 +00001364#if defined(CONFIG_CMD_NFS) || \
1365 defined(CONFIG_CMD_SNTP) || \
1366 defined(CONFIG_CMD_DNS)
Robin Getz1a32bf42009-07-20 14:53:54 -04001367/*
Robin Getz97399462010-03-08 14:07:00 -05001368 * make port a little random (1024-17407)
1369 * This keeps the math somewhat trivial to compute, and seems to work with
1370 * all supported protocols/clients/servers
Robin Getz1a32bf42009-07-20 14:53:54 -04001371 */
1372unsigned int random_port(void)
1373{
Robin Getz97399462010-03-08 14:07:00 -05001374 return 1024 + (get_timer(0) % 0x4000);
Robin Getz1a32bf42009-07-20 14:53:54 -04001375}
1376#endif
1377
Luca Ceresoli4f63acd2011-05-11 03:59:56 +00001378void ip_to_string(IPaddr_t x, char *s)
wdenk2d966952002-10-31 22:12:35 +00001379{
Luca Ceresoli4f63acd2011-05-11 03:59:56 +00001380 x = ntohl(x);
1381 sprintf(s, "%d.%d.%d.%d",
1382 (int) ((x >> 24) & 0xff),
1383 (int) ((x >> 16) & 0xff),
1384 (int) ((x >> 8) & 0xff), (int) ((x >> 0) & 0xff)
wdenk6e592382004-04-18 17:39:38 +00001385 );
wdenk2d966952002-10-31 22:12:35 +00001386}
1387
wdenka3d991b2004-04-15 21:48:45 +00001388void VLAN_to_string(ushort x, char *s)
1389{
1390 x = ntohs(x);
1391
1392 if (x == (ushort)-1)
1393 x = VLAN_NONE;
1394
1395 if (x == VLAN_NONE)
1396 strcpy(s, "none");
1397 else
1398 sprintf(s, "%d", x & VLAN_IDMASK);
1399}
1400
Mike Frysinger2e3ef6e2010-10-20 07:16:48 -04001401ushort string_to_VLAN(const char *s)
wdenka3d991b2004-04-15 21:48:45 +00001402{
1403 ushort id;
1404
1405 if (s == NULL)
wdenkb9711de2004-04-25 13:18:40 +00001406 return htons(VLAN_NONE);
wdenka3d991b2004-04-15 21:48:45 +00001407
1408 if (*s < '0' || *s > '9')
1409 id = VLAN_NONE;
1410 else
1411 id = (ushort)simple_strtoul(s, NULL, 10);
1412
wdenkb9711de2004-04-25 13:18:40 +00001413 return htons(id);
wdenka3d991b2004-04-15 21:48:45 +00001414}
1415
wdenka3d991b2004-04-15 21:48:45 +00001416ushort getenv_VLAN(char *var)
1417{
Luca Ceresoli92895de2011-05-04 02:40:45 +00001418 return string_to_VLAN(getenv(var));
wdenka3d991b2004-04-15 21:48:45 +00001419}