blob: e9536b36ef43929229ad739dddc90261392f8023 [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 Denk30b87322005-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
wdenkbe9c1cb2004-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
wdenkb4ad9622005-04-01 00:25:43 +000067 *
68 * SNTP:
69 *
Wolfgang Denk30b87322005-08-12 23:43:12 +020070 * Prerequisites: - own ethernet address
wdenkb4ad9622005-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>
78#include <watchdog.h>
79#include <command.h>
Joe Hershberger77001b432012-05-15 08:59:08 +000080#include <linux/compiler.h>
wdenk2d966952002-10-31 22:12:35 +000081#include <net.h>
82#include "bootp.h"
83#include "tftp.h"
Peter Tyserf7a48ca2010-09-30 11:25:48 -050084#ifdef CONFIG_CMD_RARP
wdenk2d966952002-10-31 22:12:35 +000085#include "rarp.h"
Peter Tyserf7a48ca2010-09-30 11:25:48 -050086#endif
wdenkbe9c1cb2004-02-24 02:00:03 +000087#include "nfs.h"
wdenk49c3f672003-10-08 22:33:00 +000088#ifdef CONFIG_STATUS_LED
89#include <status_led.h>
90#include <miiphy.h>
91#endif
Jon Loeliger54f35c22007-07-09 17:45:14 -050092#if defined(CONFIG_CMD_SNTP)
wdenkb4ad9622005-04-01 00:25:43 +000093#include "sntp.h"
94#endif
Peter Tyser62948502008-11-03 09:30:59 -060095#if defined(CONFIG_CDP_VERSION)
96#include <timestamp.h>
97#endif
Robin Getz82f0d232009-07-20 14:53:54 -040098#if defined(CONFIG_CMD_DNS)
99#include "dns.h"
100#endif
wdenk2d966952002-10-31 22:12:35 +0000101
Wolfgang Denk6405a152006-03-31 18:32:53 +0200102DECLARE_GLOBAL_DATA_PTR;
103
Guennadi Liakhovetskib38c2b32008-04-03 17:04:19 +0200104#ifndef CONFIG_ARP_TIMEOUT
Luca Ceresoli5c83a962011-05-11 03:59:54 +0000105/* Milliseconds before trying ARP again */
106# define ARP_TIMEOUT 5000UL
Guennadi Liakhovetskib38c2b32008-04-03 17:04:19 +0200107#else
Bartlomiej Sieka56668462008-10-01 15:26:28 +0200108# define ARP_TIMEOUT CONFIG_ARP_TIMEOUT
Guennadi Liakhovetskib38c2b32008-04-03 17:04:19 +0200109#endif
110
111
wdenke6466f62003-06-05 19:27:42 +0000112#ifndef CONFIG_NET_RETRY_COUNT
Guennadi Liakhovetskib38c2b32008-04-03 17:04:19 +0200113# define ARP_TIMEOUT_COUNT 5 /* # of timeouts before giving up */
wdenke6466f62003-06-05 19:27:42 +0000114#else
Guennadi Liakhovetskib38c2b32008-04-03 17:04:19 +0200115# define ARP_TIMEOUT_COUNT CONFIG_NET_RETRY_COUNT
wdenke6466f62003-06-05 19:27:42 +0000116#endif
117
wdenk2d966952002-10-31 22:12:35 +0000118/** BOOTP EXTENTIONS **/
119
Luca Ceresoli5c83a962011-05-11 03:59:54 +0000120/* Our subnet mask (0=unknown) */
Luca Ceresolid01bc1c2011-05-11 03:59:55 +0000121IPaddr_t NetOurSubnetMask;
Luca Ceresoli5c83a962011-05-11 03:59:54 +0000122/* Our gateways IP address */
Luca Ceresolid01bc1c2011-05-11 03:59:55 +0000123IPaddr_t NetOurGatewayIP;
Luca Ceresoli5c83a962011-05-11 03:59:54 +0000124/* Our DNS IP address */
Luca Ceresolid01bc1c2011-05-11 03:59:55 +0000125IPaddr_t NetOurDNSIP;
Jon Loeliger5336a762007-07-09 22:08:34 -0500126#if defined(CONFIG_BOOTP_DNS2)
Luca Ceresoli5c83a962011-05-11 03:59:54 +0000127/* Our 2nd DNS IP address */
Luca Ceresolid01bc1c2011-05-11 03:59:55 +0000128IPaddr_t NetOurDNS2IP;
stroesee0aadfb2003-08-28 14:17:32 +0000129#endif
Luca Ceresoli5c83a962011-05-11 03:59:54 +0000130/* Our NIS domain */
Luca Ceresolid01bc1c2011-05-11 03:59:55 +0000131char NetOurNISDomain[32] = {0,};
Luca Ceresoli5c83a962011-05-11 03:59:54 +0000132/* Our hostname */
Luca Ceresolid01bc1c2011-05-11 03:59:55 +0000133char NetOurHostName[32] = {0,};
Luca Ceresoli5c83a962011-05-11 03:59:54 +0000134/* Our bootpath */
Luca Ceresolid01bc1c2011-05-11 03:59:55 +0000135char NetOurRootPath[64] = {0,};
Luca Ceresoli5c83a962011-05-11 03:59:54 +0000136/* Our bootfile size in blocks */
Luca Ceresolid01bc1c2011-05-11 03:59:55 +0000137ushort NetBootFileSize;
wdenk2d966952002-10-31 22:12:35 +0000138
David Updegraff7280da72007-06-11 10:41:07 -0500139#ifdef CONFIG_MCAST_TFTP /* Multicast TFTP */
140IPaddr_t Mcast_addr;
141#endif
142
wdenk2d966952002-10-31 22:12:35 +0000143/** END OF BOOTP EXTENTIONS **/
144
Luca Ceresoli5c83a962011-05-11 03:59:54 +0000145/* The actual transferred size of the bootfile (in bytes) */
146ulong NetBootFileXferSize;
147/* Our ethernet address */
148uchar NetOurEther[6];
149/* Boot server enet address */
Luca Ceresolid01bc1c2011-05-11 03:59:55 +0000150uchar NetServerEther[6];
Luca Ceresoli5c83a962011-05-11 03:59:54 +0000151/* Our IP addr (0 = unknown) */
152IPaddr_t NetOurIP;
153/* Server IP addr (0 = unknown) */
154IPaddr_t NetServerIP;
155/* Current receive packet */
Joe Hershberger4b7747e2012-05-15 08:59:04 +0000156uchar *NetRxPacket;
Luca Ceresoli5c83a962011-05-11 03:59:54 +0000157/* Current rx packet length */
158int NetRxPacketLen;
159/* IP packet ID */
160unsigned NetIPID;
161/* Ethernet bcast address */
Luca Ceresolid01bc1c2011-05-11 03:59:55 +0000162uchar NetBcastAddr[6] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
163uchar NetEtherNullAddr[6];
Rafal Jaworowski1f2c9a42007-12-27 18:19:02 +0100164#ifdef CONFIG_API
Joe Hershberger4b7747e2012-05-15 08:59:04 +0000165void (*push_packet)(void *, int len) = 0;
Rafal Jaworowski1f2c9a42007-12-27 18:19:02 +0100166#endif
Jon Loeliger54f35c22007-07-09 17:45:14 -0500167#if defined(CONFIG_CMD_CDP)
Luca Ceresoli5c83a962011-05-11 03:59:54 +0000168/* Ethernet bcast address */
Luca Ceresolid01bc1c2011-05-11 03:59:55 +0000169uchar NetCDPAddr[6] = { 0x01, 0x00, 0x0c, 0xcc, 0xcc, 0xcc };
wdenk145d2c12004-04-15 21:48:45 +0000170#endif
Luca Ceresoli5c83a962011-05-11 03:59:54 +0000171/* Network loop state */
172int NetState;
Luca Ceresoli5c83a962011-05-11 03:59:54 +0000173/* Tried all network devices */
Luca Ceresolid01bc1c2011-05-11 03:59:55 +0000174int NetRestartWrap;
Luca Ceresoli5c83a962011-05-11 03:59:54 +0000175/* Network loop restarted */
Luca Ceresolid01bc1c2011-05-11 03:59:55 +0000176static int NetRestarted;
Luca Ceresoli5c83a962011-05-11 03:59:54 +0000177/* At least one device configured */
Luca Ceresolid01bc1c2011-05-11 03:59:55 +0000178static int NetDevExists;
wdenk2d966952002-10-31 22:12:35 +0000179
wdenk05939202004-04-18 17:39:38 +0000180/* XXX in both little & big endian machines 0xFFFF == ntohs(-1) */
Luca Ceresoli5c83a962011-05-11 03:59:54 +0000181/* default is without VLAN */
182ushort NetOurVLAN = 0xFFFF;
183/* ditto */
184ushort NetOurNativeVLAN = 0xFFFF;
wdenk145d2c12004-04-15 21:48:45 +0000185
Luca Ceresoli5c83a962011-05-11 03:59:54 +0000186/* Boot File name */
187char BootFile[128];
wdenk2d966952002-10-31 22:12:35 +0000188
Jon Loeliger54f35c22007-07-09 17:45:14 -0500189#if defined(CONFIG_CMD_PING)
Luca Ceresoli5c83a962011-05-11 03:59:54 +0000190/* the ip address to ping */
191IPaddr_t NetPingIP;
wdenke6466f62003-06-05 19:27:42 +0000192
193static void PingStart(void);
194#endif
195
Jon Loeliger54f35c22007-07-09 17:45:14 -0500196#if defined(CONFIG_CMD_CDP)
wdenk145d2c12004-04-15 21:48:45 +0000197static void CDPStart(void);
198#endif
199
Jon Loeliger54f35c22007-07-09 17:45:14 -0500200#if defined(CONFIG_CMD_SNTP)
Luca Ceresoli5c83a962011-05-11 03:59:54 +0000201/* NTP server IP address */
202IPaddr_t NetNtpServerIP;
203/* offset time from UTC */
Luca Ceresolid01bc1c2011-05-11 03:59:55 +0000204int NetTimeOffset;
wdenkb4ad9622005-04-01 00:25:43 +0000205#endif
206
Joe Hershberger4b7747e2012-05-15 08:59:04 +0000207uchar PktBuf[(PKTBUFSRX+1) * PKTSIZE_ALIGN + PKTALIGN];
wdenk2d966952002-10-31 22:12:35 +0000208
Luca Ceresoli5c83a962011-05-11 03:59:54 +0000209/* Receive packet */
Joe Hershberger4b7747e2012-05-15 08:59:04 +0000210uchar *NetRxPackets[PKTBUFSRX];
wdenk2d966952002-10-31 22:12:35 +0000211
Luca Ceresoli5c83a962011-05-11 03:59:54 +0000212/* Current RX packet handler */
213static rxhand_f *packetHandler;
Simon Glass2928cd82011-10-26 14:18:38 +0000214#ifdef CONFIG_CMD_TFTPPUT
Simon Glass230467c2011-10-24 18:00:01 +0000215static rxhand_icmp_f *packet_icmp_handler; /* Current ICMP rx handler */
Simon Glass2928cd82011-10-26 14:18:38 +0000216#endif
Luca Ceresoli5c83a962011-05-11 03:59:54 +0000217/* Current timeout handler */
218static thand_f *timeHandler;
219/* Time base value */
220static ulong timeStart;
221/* Current timeout value */
222static ulong timeDelta;
223/* THE transmit packet */
Joe Hershberger4b7747e2012-05-15 08:59:04 +0000224uchar *NetTxPacket;
wdenk2d966952002-10-31 22:12:35 +0000225
Simon Glassd6c5f552011-10-24 18:00:02 +0000226static int net_check_prereq(enum proto_t protocol);
wdenk2d966952002-10-31 22:12:35 +0000227
Remy Bohmer0ddb8e82009-10-28 22:13:39 +0100228static int NetTryCount;
229
wdenk2d966952002-10-31 22:12:35 +0000230/**********************************************************************/
wdenke6466f62003-06-05 19:27:42 +0000231
232IPaddr_t NetArpWaitPacketIP;
233IPaddr_t NetArpWaitReplyIP;
Luca Ceresoli5c83a962011-05-11 03:59:54 +0000234/* MAC address of waiting packet's destination */
235uchar *NetArpWaitPacketMAC;
236/* THE transmit packet */
237uchar *NetArpWaitTxPacket;
wdenke6466f62003-06-05 19:27:42 +0000238int NetArpWaitTxPacketSize;
Wolfgang Denka1be4762008-05-20 16:00:29 +0200239uchar NetArpWaitPacketBuf[PKTSIZE_ALIGN + PKTALIGN];
wdenke6466f62003-06-05 19:27:42 +0000240ulong NetArpWaitTimerStart;
241int NetArpWaitTry;
242
Luca Ceresoli7cbd7482011-05-11 03:59:56 +0000243void ArpRequest(void)
wdenke6466f62003-06-05 19:27:42 +0000244{
Joe Hershberger4b7747e2012-05-15 08:59:04 +0000245 uchar *pkt;
wdenk05939202004-04-18 17:39:38 +0000246 ARP_t *arp;
wdenke6466f62003-06-05 19:27:42 +0000247
Robin Getz9e0a4d62009-07-23 03:01:03 -0400248 debug("ARP broadcast %d\n", NetArpWaitTry);
249
wdenke6466f62003-06-05 19:27:42 +0000250 pkt = NetTxPacket;
251
Luca Ceresoli7cbd7482011-05-11 03:59:56 +0000252 pkt += NetSetEther(pkt, NetBcastAddr, PROT_ARP);
wdenke6466f62003-06-05 19:27:42 +0000253
wdenk05939202004-04-18 17:39:38 +0000254 arp = (ARP_t *) pkt;
wdenke6466f62003-06-05 19:27:42 +0000255
Luca Ceresoli7cbd7482011-05-11 03:59:56 +0000256 arp->ar_hrd = htons(ARP_ETHER);
257 arp->ar_pro = htons(PROT_IP);
wdenke6466f62003-06-05 19:27:42 +0000258 arp->ar_hln = 6;
259 arp->ar_pln = 4;
Luca Ceresoli7cbd7482011-05-11 03:59:56 +0000260 arp->ar_op = htons(ARPOP_REQUEST);
wdenke6466f62003-06-05 19:27:42 +0000261
Luca Ceresoli5c83a962011-05-11 03:59:54 +0000262 /* source ET addr */
Luca Ceresoli7cbd7482011-05-11 03:59:56 +0000263 memcpy(&arp->ar_data[0], NetOurEther, 6);
Luca Ceresoli5c83a962011-05-11 03:59:54 +0000264 /* source IP addr */
Luca Ceresoli7cbd7482011-05-11 03:59:56 +0000265 NetWriteIP((uchar *) &arp->ar_data[6], NetOurIP);
Simon Glasse4655c52011-10-26 14:18:39 +0000266 /* dest ET addr = 0 */
267 memset(&arp->ar_data[10], '\0', 6);
wdenk05939202004-04-18 17:39:38 +0000268 if ((NetArpWaitPacketIP & NetOurSubnetMask) !=
269 (NetOurIP & NetOurSubnetMask)) {
270 if (NetOurGatewayIP == 0) {
Luca Ceresoli7cbd7482011-05-11 03:59:56 +0000271 puts("## Warning: gatewayip needed but not set\n");
Wolfgang Denk98ceffb2006-03-12 01:13:30 +0100272 NetArpWaitReplyIP = NetArpWaitPacketIP;
273 } else {
274 NetArpWaitReplyIP = NetOurGatewayIP;
wdenk05939202004-04-18 17:39:38 +0000275 }
wdenk05939202004-04-18 17:39:38 +0000276 } else {
277 NetArpWaitReplyIP = NetArpWaitPacketIP;
278 }
wdenke6466f62003-06-05 19:27:42 +0000279
Luca Ceresoli7cbd7482011-05-11 03:59:56 +0000280 NetWriteIP((uchar *) &arp->ar_data[16], NetArpWaitReplyIP);
281 (void) eth_send(NetTxPacket, (pkt - NetTxPacket) + ARP_HDR_SIZE);
wdenke6466f62003-06-05 19:27:42 +0000282}
283
284void ArpTimeoutCheck(void)
285{
286 ulong t;
287
288 if (!NetArpWaitPacketIP)
289 return;
290
291 t = get_timer(0);
292
293 /* check for arp timeout */
Bartlomiej Sieka56668462008-10-01 15:26:28 +0200294 if ((t - NetArpWaitTimerStart) > ARP_TIMEOUT) {
wdenke6466f62003-06-05 19:27:42 +0000295 NetArpWaitTry++;
296
297 if (NetArpWaitTry >= ARP_TIMEOUT_COUNT) {
Luca Ceresoli7cbd7482011-05-11 03:59:56 +0000298 puts("\nARP Retry count exceeded; starting again\n");
wdenke6466f62003-06-05 19:27:42 +0000299 NetArpWaitTry = 0;
300 NetStartAgain();
301 } else {
302 NetArpWaitTimerStart = t;
303 ArpRequest();
304 }
305 }
306}
307
Simon Glass5234ad12011-10-27 06:24:32 +0000308/*
309 * Check if autoload is enabled. If so, use either NFS or TFTP to download
310 * the boot file.
311 */
312void net_auto_load(void)
313{
314 const char *s = getenv("autoload");
315
316 if (s != NULL) {
317 if (*s == 'n') {
318 /*
319 * Just use BOOTP/RARP to configure system;
320 * Do not use TFTP to load the bootfile.
321 */
322 NetState = NETLOOP_SUCCESS;
323 return;
324 }
325#if defined(CONFIG_CMD_NFS)
326 if (strcmp(s, "NFS") == 0) {
327 /*
328 * Use NFS to load the bootfile.
329 */
330 NfsStart();
331 return;
332 }
333#endif
334 }
335 TftpStart(TFTPGET);
336}
337
Simon Glassd6c5f552011-10-24 18:00:02 +0000338static void NetInitLoop(enum proto_t protocol)
Heiko Schocher0c303fa2009-02-10 09:38:52 +0100339{
Luca Ceresolid01bc1c2011-05-11 03:59:55 +0000340 static int env_changed_id;
Luca Ceresoli7cbd7482011-05-11 03:59:56 +0000341 int env_id = get_env_id();
Heiko Schocher0c303fa2009-02-10 09:38:52 +0100342
343 /* update only when the environment has changed */
Michael Zaidmanb97bfe42009-04-04 01:43:00 +0300344 if (env_changed_id != env_id) {
Enric Balletbo i Serra7019d252011-05-31 21:01:47 +0000345 NetOurIP = getenv_IPaddr("ipaddr");
Luca Ceresoli7cbd7482011-05-11 03:59:56 +0000346 NetOurGatewayIP = getenv_IPaddr("gatewayip");
347 NetOurSubnetMask = getenv_IPaddr("netmask");
348 NetServerIP = getenv_IPaddr("serverip");
Heiko Schocher0c303fa2009-02-10 09:38:52 +0100349 NetOurNativeVLAN = getenv_VLAN("nvlan");
Michael Zaidmanb97bfe42009-04-04 01:43:00 +0300350 NetOurVLAN = getenv_VLAN("vlan");
Robin Getz82f0d232009-07-20 14:53:54 -0400351#if defined(CONFIG_CMD_DNS)
352 NetOurDNSIP = getenv_IPaddr("dnsip");
353#endif
Michael Zaidmanb97bfe42009-04-04 01:43:00 +0300354 env_changed_id = env_id;
Heiko Schocher0c303fa2009-02-10 09:38:52 +0100355 }
Michael Zaidmanb97bfe42009-04-04 01:43:00 +0300356
Heiko Schocher3b195ff2009-04-28 08:36:11 +0200357 return;
Heiko Schocher0c303fa2009-02-10 09:38:52 +0100358}
359
wdenke6466f62003-06-05 19:27:42 +0000360/**********************************************************************/
wdenk2d966952002-10-31 22:12:35 +0000361/*
362 * Main network processing loop.
363 */
364
Simon Glassd6c5f552011-10-24 18:00:02 +0000365int NetLoop(enum proto_t protocol)
wdenk2d966952002-10-31 22:12:35 +0000366{
wdenk2d966952002-10-31 22:12:35 +0000367 bd_t *bd = gd->bd;
Simon Glass230467c2011-10-24 18:00:01 +0000368 int ret = -1;
wdenk2d966952002-10-31 22:12:35 +0000369
wdenk2d966952002-10-31 22:12:35 +0000370 NetRestarted = 0;
371 NetDevExists = 0;
wdenk2d966952002-10-31 22:12:35 +0000372
wdenke6466f62003-06-05 19:27:42 +0000373 /* XXX problem with bss workaround */
374 NetArpWaitPacketMAC = NULL;
375 NetArpWaitTxPacket = NULL;
376 NetArpWaitPacketIP = 0;
377 NetArpWaitReplyIP = 0;
378 NetArpWaitTxPacket = NULL;
379 NetTxPacket = NULL;
Remy Bohmer0ddb8e82009-10-28 22:13:39 +0100380 NetTryCount = 1;
wdenke6466f62003-06-05 19:27:42 +0000381
wdenk2d966952002-10-31 22:12:35 +0000382 if (!NetTxPacket) {
383 int i;
wdenk2d966952002-10-31 22:12:35 +0000384 /*
385 * Setup packet buffers, aligned correctly.
386 */
387 NetTxPacket = &PktBuf[0] + (PKTALIGN - 1);
388 NetTxPacket -= (ulong)NetTxPacket % PKTALIGN;
Luca Ceresolie8ccbb02011-05-04 02:40:43 +0000389 for (i = 0; i < PKTBUFSRX; i++)
wdenk2d966952002-10-31 22:12:35 +0000390 NetRxPackets[i] = NetTxPacket + (i+1)*PKTSIZE_ALIGN;
wdenke6466f62003-06-05 19:27:42 +0000391 }
392
393 if (!NetArpWaitTxPacket) {
394 NetArpWaitTxPacket = &NetArpWaitPacketBuf[0] + (PKTALIGN - 1);
395 NetArpWaitTxPacket -= (ulong)NetArpWaitTxPacket % PKTALIGN;
396 NetArpWaitTxPacketSize = 0;
wdenk2d966952002-10-31 22:12:35 +0000397 }
398
Simon Glass768cbf02011-12-10 11:08:06 +0000399 bootstage_mark_name(BOOTSTAGE_ID_ETH_START, "eth_start");
wdenk2d966952002-10-31 22:12:35 +0000400 eth_halt();
wdenk145d2c12004-04-15 21:48:45 +0000401 eth_set_current();
wdenkfa66e932005-04-03 14:52:59 +0000402 if (eth_init(bd) < 0) {
403 eth_halt();
Luca Ceresoli5fe6de22011-05-04 02:40:45 +0000404 return -1;
wdenkfa66e932005-04-03 14:52:59 +0000405 }
wdenk2d966952002-10-31 22:12:35 +0000406
407restart:
Luca Ceresoli7cbd7482011-05-11 03:59:56 +0000408 memcpy(NetOurEther, eth_get_dev()->enetaddr, 6);
wdenk2d966952002-10-31 22:12:35 +0000409
410 NetState = NETLOOP_CONTINUE;
411
412 /*
413 * Start the ball rolling with the given start function. From
414 * here on, this code is a state machine driven by received
415 * packets and timer events.
416 */
Heiko Schocher0c303fa2009-02-10 09:38:52 +0100417 NetInitLoop(protocol);
wdenk2d966952002-10-31 22:12:35 +0000418
Luca Ceresoli7cbd7482011-05-11 03:59:56 +0000419 switch (net_check_prereq(protocol)) {
wdenk2d966952002-10-31 22:12:35 +0000420 case 1:
421 /* network not configured */
wdenkfa66e932005-04-03 14:52:59 +0000422 eth_halt();
Luca Ceresoli5fe6de22011-05-04 02:40:45 +0000423 return -1;
wdenk2d966952002-10-31 22:12:35 +0000424
wdenk2d966952002-10-31 22:12:35 +0000425 case 2:
426 /* network device not configured */
427 break;
wdenk2d966952002-10-31 22:12:35 +0000428
429 case 0:
wdenk2d966952002-10-31 22:12:35 +0000430 NetDevExists = 1;
Simon Glassd6c5f552011-10-24 18:00:02 +0000431 NetBootFileXferSize = 0;
wdenk2d966952002-10-31 22:12:35 +0000432 switch (protocol) {
Simon Glassd6c5f552011-10-24 18:00:02 +0000433 case TFTPGET:
Simon Glass6a398d22011-10-24 18:00:07 +0000434#ifdef CONFIG_CMD_TFTPPUT
435 case TFTPPUT:
436#endif
wdenk2d966952002-10-31 22:12:35 +0000437 /* always use ARP to get server ethernet address */
Simon Glassd6c5f552011-10-24 18:00:02 +0000438 TftpStart(protocol);
wdenk2d966952002-10-31 22:12:35 +0000439 break;
Luca Ceresoli7aa81a42011-05-17 00:03:40 +0000440#ifdef CONFIG_CMD_TFTPSRV
441 case TFTPSRV:
442 TftpStartServer();
443 break;
444#endif
Jon Loeliger54f35c22007-07-09 17:45:14 -0500445#if defined(CONFIG_CMD_DHCP)
wdenk2d966952002-10-31 22:12:35 +0000446 case DHCP:
wdenkc3919532004-10-11 22:51:13 +0000447 BootpTry = 0;
Michael Zaidman16bb21d2009-07-14 23:37:12 +0300448 NetOurIP = 0;
wdenk2d966952002-10-31 22:12:35 +0000449 DhcpRequest(); /* Basically same as BOOTP */
450 break;
Jon Loeligera9807e52007-07-10 11:05:02 -0500451#endif
wdenk2d966952002-10-31 22:12:35 +0000452
453 case BOOTP:
454 BootpTry = 0;
Michael Zaidman16bb21d2009-07-14 23:37:12 +0300455 NetOurIP = 0;
Luca Ceresoli7cbd7482011-05-11 03:59:56 +0000456 BootpRequest();
wdenk2d966952002-10-31 22:12:35 +0000457 break;
458
Peter Tyserf7a48ca2010-09-30 11:25:48 -0500459#if defined(CONFIG_CMD_RARP)
wdenk2d966952002-10-31 22:12:35 +0000460 case RARP:
461 RarpTry = 0;
Michael Zaidman16bb21d2009-07-14 23:37:12 +0300462 NetOurIP = 0;
Luca Ceresoli7cbd7482011-05-11 03:59:56 +0000463 RarpRequest();
wdenk2d966952002-10-31 22:12:35 +0000464 break;
Peter Tyserf7a48ca2010-09-30 11:25:48 -0500465#endif
Jon Loeliger54f35c22007-07-09 17:45:14 -0500466#if defined(CONFIG_CMD_PING)
wdenke6466f62003-06-05 19:27:42 +0000467 case PING:
468 PingStart();
469 break;
470#endif
Jon Loeliger54f35c22007-07-09 17:45:14 -0500471#if defined(CONFIG_CMD_NFS)
wdenkbe9c1cb2004-02-24 02:00:03 +0000472 case NFS:
473 NfsStart();
474 break;
475#endif
Jon Loeliger54f35c22007-07-09 17:45:14 -0500476#if defined(CONFIG_CMD_CDP)
wdenk145d2c12004-04-15 21:48:45 +0000477 case CDP:
478 CDPStart();
479 break;
480#endif
wdenkb8fb6192004-08-02 21:11:11 +0000481#ifdef CONFIG_NETCONSOLE
482 case NETCONS:
483 NcStart();
484 break;
485#endif
Jon Loeliger54f35c22007-07-09 17:45:14 -0500486#if defined(CONFIG_CMD_SNTP)
wdenkb4ad9622005-04-01 00:25:43 +0000487 case SNTP:
488 SntpStart();
489 break;
490#endif
Robin Getz82f0d232009-07-20 14:53:54 -0400491#if defined(CONFIG_CMD_DNS)
492 case DNS:
493 DnsStart();
494 break;
495#endif
wdenk2d966952002-10-31 22:12:35 +0000496 default:
497 break;
498 }
499
wdenk2d966952002-10-31 22:12:35 +0000500 break;
501 }
502
Jon Loeliger54f35c22007-07-09 17:45:14 -0500503#if defined(CONFIG_MII) || defined(CONFIG_CMD_MII)
Luca Ceresoli5c83a962011-05-11 03:59:54 +0000504#if defined(CONFIG_SYS_FAULT_ECHO_LINK_DOWN) && \
505 defined(CONFIG_STATUS_LED) && \
506 defined(STATUS_LED_RED)
wdenk49c3f672003-10-08 22:33:00 +0000507 /*
wdenk9c53f402003-10-15 23:53:47 +0000508 * Echo the inverted link state to the fault LED.
wdenk49c3f672003-10-08 22:33:00 +0000509 */
Luca Ceresolie8ccbb02011-05-04 02:40:43 +0000510 if (miiphy_link(eth_get_dev()->name, CONFIG_SYS_FAULT_MII_ADDR))
Luca Ceresoli7cbd7482011-05-11 03:59:56 +0000511 status_led_set(STATUS_LED_RED, STATUS_LED_OFF);
Luca Ceresolie8ccbb02011-05-04 02:40:43 +0000512 else
Luca Ceresoli7cbd7482011-05-11 03:59:56 +0000513 status_led_set(STATUS_LED_RED, STATUS_LED_ON);
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200514#endif /* CONFIG_SYS_FAULT_ECHO_LINK_DOWN, ... */
wdenk49c3f672003-10-08 22:33:00 +0000515#endif /* CONFIG_MII, ... */
wdenk2d966952002-10-31 22:12:35 +0000516
517 /*
518 * Main packet reception loop. Loop receiving packets until
wdenk27fa5852005-04-03 14:18:51 +0000519 * someone sets `NetState' to a state that terminates.
wdenk2d966952002-10-31 22:12:35 +0000520 */
521 for (;;) {
522 WATCHDOG_RESET();
523#ifdef CONFIG_SHOW_ACTIVITY
Joe Hershberger77001b432012-05-15 08:59:08 +0000524 show_activity(1);
wdenk2d966952002-10-31 22:12:35 +0000525#endif
526 /*
527 * Check the ethernet for a new packet. The ethernet
528 * receive routine will process it.
529 */
Guennadi Liakhovetskib38c2b32008-04-03 17:04:19 +0200530 eth_rx();
wdenk2d966952002-10-31 22:12:35 +0000531
532 /*
533 * Abort if ctrl-c was pressed.
534 */
535 if (ctrlc()) {
wdenk57b2d802003-06-27 21:31:46 +0000536 eth_halt();
Luca Ceresoli7cbd7482011-05-11 03:59:56 +0000537 puts("\nAbort\n");
Simon Glass230467c2011-10-24 18:00:01 +0000538 goto done;
wdenk2d966952002-10-31 22:12:35 +0000539 }
540
wdenke6466f62003-06-05 19:27:42 +0000541 ArpTimeoutCheck();
wdenk2d966952002-10-31 22:12:35 +0000542
543 /*
544 * Check for a timeout, and run the timeout handler
545 * if we have one.
546 */
wdenk5d841732003-08-17 18:55:18 +0000547 if (timeHandler && ((get_timer(0) - timeStart) > timeDelta)) {
wdenk2d966952002-10-31 22:12:35 +0000548 thand_f *x;
549
Jon Loeliger54f35c22007-07-09 17:45:14 -0500550#if defined(CONFIG_MII) || defined(CONFIG_CMD_MII)
Luca Ceresoli7cbd7482011-05-11 03:59:56 +0000551#if defined(CONFIG_SYS_FAULT_ECHO_LINK_DOWN) && \
552 defined(CONFIG_STATUS_LED) && \
553 defined(STATUS_LED_RED)
wdenk49c3f672003-10-08 22:33:00 +0000554 /*
wdenk9c53f402003-10-15 23:53:47 +0000555 * Echo the inverted link state to the fault LED.
wdenk49c3f672003-10-08 22:33:00 +0000556 */
Luca Ceresoli7cbd7482011-05-11 03:59:56 +0000557 if (miiphy_link(eth_get_dev()->name,
Luca Ceresoli5c83a962011-05-11 03:59:54 +0000558 CONFIG_SYS_FAULT_MII_ADDR)) {
Luca Ceresoli7cbd7482011-05-11 03:59:56 +0000559 status_led_set(STATUS_LED_RED, STATUS_LED_OFF);
wdenk49c3f672003-10-08 22:33:00 +0000560 } else {
Luca Ceresoli7cbd7482011-05-11 03:59:56 +0000561 status_led_set(STATUS_LED_RED, STATUS_LED_ON);
wdenk49c3f672003-10-08 22:33:00 +0000562 }
Luca Ceresoli7cbd7482011-05-11 03:59:56 +0000563#endif /* CONFIG_SYS_FAULT_ECHO_LINK_DOWN, ... */
wdenk49c3f672003-10-08 22:33:00 +0000564#endif /* CONFIG_MII, ... */
wdenk2d966952002-10-31 22:12:35 +0000565 x = timeHandler;
566 timeHandler = (thand_f *)0;
567 (*x)();
568 }
569
570
571 switch (NetState) {
572
573 case NETLOOP_RESTART:
wdenk2d966952002-10-31 22:12:35 +0000574 NetRestarted = 1;
wdenk2d966952002-10-31 22:12:35 +0000575 goto restart;
576
577 case NETLOOP_SUCCESS:
578 if (NetBootFileXferSize > 0) {
Wolfgang Denk084d0ce2007-09-12 00:48:57 +0200579 char buf[20];
wdenk2d966952002-10-31 22:12:35 +0000580 printf("Bytes transferred = %ld (%lx hex)\n",
581 NetBootFileXferSize,
582 NetBootFileXferSize);
Wolfgang Denk084d0ce2007-09-12 00:48:57 +0200583 sprintf(buf, "%lX", NetBootFileXferSize);
wdenk2d966952002-10-31 22:12:35 +0000584 setenv("filesize", buf);
wdenk145d2c12004-04-15 21:48:45 +0000585
586 sprintf(buf, "%lX", (unsigned long)load_addr);
587 setenv("fileaddr", buf);
wdenk2d966952002-10-31 22:12:35 +0000588 }
589 eth_halt();
Simon Glass230467c2011-10-24 18:00:01 +0000590 ret = NetBootFileXferSize;
591 goto done;
wdenk2d966952002-10-31 22:12:35 +0000592
593 case NETLOOP_FAIL:
Simon Glass230467c2011-10-24 18:00:01 +0000594 goto done;
wdenk2d966952002-10-31 22:12:35 +0000595 }
596 }
Simon Glass230467c2011-10-24 18:00:01 +0000597
598done:
Simon Glass2928cd82011-10-26 14:18:38 +0000599#ifdef CONFIG_CMD_TFTPPUT
Simon Glass230467c2011-10-24 18:00:01 +0000600 /* Clear out the handlers */
601 NetSetHandler(NULL);
602 net_set_icmp_handler(NULL);
Simon Glass2928cd82011-10-26 14:18:38 +0000603#endif
Simon Glass230467c2011-10-24 18:00:01 +0000604 return ret;
wdenk2d966952002-10-31 22:12:35 +0000605}
606
607/**********************************************************************/
608
609static void
610startAgainTimeout(void)
611{
612 NetState = NETLOOP_RESTART;
613}
614
615static void
Luca Ceresoli428ab362011-04-18 06:19:50 +0000616startAgainHandler(uchar *pkt, unsigned dest, IPaddr_t sip,
617 unsigned src, unsigned len)
wdenk2d966952002-10-31 22:12:35 +0000618{
619 /* Totally ignore the packet */
620}
621
Luca Ceresoli7cbd7482011-05-11 03:59:56 +0000622void NetStartAgain(void)
wdenk2d966952002-10-31 22:12:35 +0000623{
wdenk05939202004-04-18 17:39:38 +0000624 char *nretry;
Remy Bohmer0ddb8e82009-10-28 22:13:39 +0100625 int retry_forever = 0;
626 unsigned long retrycnt = 0;
wdenk145d2c12004-04-15 21:48:45 +0000627
Remy Bohmer0ddb8e82009-10-28 22:13:39 +0100628 nretry = getenv("netretry");
629 if (nretry) {
630 if (!strcmp(nretry, "yes"))
631 retry_forever = 1;
632 else if (!strcmp(nretry, "no"))
633 retrycnt = 0;
634 else if (!strcmp(nretry, "once"))
635 retrycnt = 1;
636 else
637 retrycnt = simple_strtoul(nretry, NULL, 0);
638 } else
639 retry_forever = 1;
640
641 if ((!retry_forever) && (NetTryCount >= retrycnt)) {
642 eth_halt();
wdenk145d2c12004-04-15 21:48:45 +0000643 NetState = NETLOOP_FAIL;
644 return;
645 }
Remy Bohmer0ddb8e82009-10-28 22:13:39 +0100646
647 NetTryCount++;
648
Luca Ceresoli7cbd7482011-05-11 03:59:56 +0000649 eth_halt();
Matthias Fuchsa02d62b2007-12-27 16:58:41 +0100650#if !defined(CONFIG_NET_DO_NOT_TRY_ANOTHER)
Luca Ceresoli7cbd7482011-05-11 03:59:56 +0000651 eth_try_another(!NetRestarted);
Matthias Fuchsa02d62b2007-12-27 16:58:41 +0100652#endif
Luca Ceresoli7cbd7482011-05-11 03:59:56 +0000653 eth_init(gd->bd);
wdenk05939202004-04-18 17:39:38 +0000654 if (NetRestartWrap) {
wdenk2d966952002-10-31 22:12:35 +0000655 NetRestartWrap = 0;
Remy Bohmer0ddb8e82009-10-28 22:13:39 +0100656 if (NetDevExists) {
Luca Ceresoli7cbd7482011-05-11 03:59:56 +0000657 NetSetTimeout(10000UL, startAgainTimeout);
658 NetSetHandler(startAgainHandler);
wdenk05939202004-04-18 17:39:38 +0000659 } else {
wdenk2d966952002-10-31 22:12:35 +0000660 NetState = NETLOOP_FAIL;
661 }
wdenk05939202004-04-18 17:39:38 +0000662 } else {
wdenk2d966952002-10-31 22:12:35 +0000663 NetState = NETLOOP_RESTART;
664 }
wdenk2d966952002-10-31 22:12:35 +0000665}
666
667/**********************************************************************/
668/*
669 * Miscelaneous bits.
670 */
671
672void
Luca Ceresolif27d91f2011-05-04 02:40:44 +0000673NetSetHandler(rxhand_f *f)
wdenk2d966952002-10-31 22:12:35 +0000674{
675 packetHandler = f;
676}
677
Simon Glass2928cd82011-10-26 14:18:38 +0000678#ifdef CONFIG_CMD_TFTPPUT
Simon Glass230467c2011-10-24 18:00:01 +0000679void net_set_icmp_handler(rxhand_icmp_f *f)
680{
681 packet_icmp_handler = f;
682}
Simon Glass2928cd82011-10-26 14:18:38 +0000683#endif
wdenk2d966952002-10-31 22:12:35 +0000684
685void
Luca Ceresolif27d91f2011-05-04 02:40:44 +0000686NetSetTimeout(ulong iv, thand_f *f)
wdenk2d966952002-10-31 22:12:35 +0000687{
688 if (iv == 0) {
689 timeHandler = (thand_f *)0;
690 } else {
691 timeHandler = f;
wdenk5d841732003-08-17 18:55:18 +0000692 timeStart = get_timer(0);
693 timeDelta = iv;
wdenk2d966952002-10-31 22:12:35 +0000694 }
695}
696
697
698void
Joe Hershberger4b7747e2012-05-15 08:59:04 +0000699NetSendPacket(uchar *pkt, int len)
wdenk2d966952002-10-31 22:12:35 +0000700{
701 (void) eth_send(pkt, len);
702}
703
wdenke6466f62003-06-05 19:27:42 +0000704int
705NetSendUDPPacket(uchar *ether, IPaddr_t dest, int dport, int sport, int len)
706{
wdenk145d2c12004-04-15 21:48:45 +0000707 uchar *pkt;
708
wdenke6466f62003-06-05 19:27:42 +0000709 /* convert to new style broadcast */
710 if (dest == 0)
711 dest = 0xFFFFFFFF;
712
713 /* if broadcast, make the ether address a broadcast and don't do ARP */
714 if (dest == 0xFFFFFFFF)
715 ether = NetBcastAddr;
716
Luca Ceresoli5c83a962011-05-11 03:59:54 +0000717 /*
718 * if MAC address was not discovered yet, save the packet and do
719 * an ARP request
720 */
wdenke6466f62003-06-05 19:27:42 +0000721 if (memcmp(ether, NetEtherNullAddr, 6) == 0) {
722
Matthias Weisser6cc63262011-12-03 03:29:44 +0000723 debug("sending ARP for %08x\n", dest);
Robin Getz9e0a4d62009-07-23 03:01:03 -0400724
wdenke6466f62003-06-05 19:27:42 +0000725 NetArpWaitPacketIP = dest;
726 NetArpWaitPacketMAC = ether;
wdenk145d2c12004-04-15 21:48:45 +0000727
728 pkt = NetArpWaitTxPacket;
Luca Ceresoli7cbd7482011-05-11 03:59:56 +0000729 pkt += NetSetEther(pkt, NetArpWaitPacketMAC, PROT_IP);
wdenk145d2c12004-04-15 21:48:45 +0000730
Luca Ceresoli7cbd7482011-05-11 03:59:56 +0000731 NetSetIP(pkt, dest, dport, sport, len);
Luca Ceresoli5c83a962011-05-11 03:59:54 +0000732 memcpy(pkt + IP_HDR_SIZE, (uchar *)NetTxPacket +
733 (pkt - (uchar *)NetArpWaitTxPacket) + IP_HDR_SIZE, len);
wdenke6466f62003-06-05 19:27:42 +0000734
735 /* size of the waiting packet */
Luca Ceresoli5c83a962011-05-11 03:59:54 +0000736 NetArpWaitTxPacketSize = (pkt - NetArpWaitTxPacket) +
737 IP_HDR_SIZE + len;
wdenke6466f62003-06-05 19:27:42 +0000738
739 /* and do the ARP request */
740 NetArpWaitTry = 1;
741 NetArpWaitTimerStart = get_timer(0);
742 ArpRequest();
743 return 1; /* waiting */
744 }
745
Matthias Weisser6cc63262011-12-03 03:29:44 +0000746 debug("sending UDP to %08x/%pM\n", dest, ether);
wdenke6466f62003-06-05 19:27:42 +0000747
wdenk145d2c12004-04-15 21:48:45 +0000748 pkt = (uchar *)NetTxPacket;
Luca Ceresoli7cbd7482011-05-11 03:59:56 +0000749 pkt += NetSetEther(pkt, ether, PROT_IP);
750 NetSetIP(pkt, dest, dport, sport, len);
wdenk145d2c12004-04-15 21:48:45 +0000751 (void) eth_send(NetTxPacket, (pkt - NetTxPacket) + IP_HDR_SIZE + len);
wdenke6466f62003-06-05 19:27:42 +0000752
wdenk05939202004-04-18 17:39:38 +0000753 return 0; /* transmitted */
wdenke6466f62003-06-05 19:27:42 +0000754}
755
Jon Loeliger54f35c22007-07-09 17:45:14 -0500756#if defined(CONFIG_CMD_PING)
wdenke6466f62003-06-05 19:27:42 +0000757static ushort PingSeqNo;
758
759int PingSend(void)
760{
761 static uchar mac[6];
Joe Hershberger4b7747e2012-05-15 08:59:04 +0000762 IP_t *ip;
763 ushort *s;
wdenk145d2c12004-04-15 21:48:45 +0000764 uchar *pkt;
wdenke6466f62003-06-05 19:27:42 +0000765
766 /* XXX always send arp request */
767
768 memcpy(mac, NetEtherNullAddr, 6);
769
Matthias Weisser6cc63262011-12-03 03:29:44 +0000770 debug("sending ARP for %08x\n", NetPingIP);
wdenke6466f62003-06-05 19:27:42 +0000771
772 NetArpWaitPacketIP = NetPingIP;
773 NetArpWaitPacketMAC = mac;
774
wdenk145d2c12004-04-15 21:48:45 +0000775 pkt = NetArpWaitTxPacket;
776 pkt += NetSetEther(pkt, mac, PROT_IP);
wdenke6466f62003-06-05 19:27:42 +0000777
Joe Hershberger4b7747e2012-05-15 08:59:04 +0000778 ip = (IP_t *)pkt;
wdenke6466f62003-06-05 19:27:42 +0000779
780 /*
Luca Ceresoli5c83a962011-05-11 03:59:54 +0000781 * Construct an IP and ICMP header.
782 * (need to set no fragment bit - XXX)
wdenke6466f62003-06-05 19:27:42 +0000783 */
Luca Ceresoli5c83a962011-05-11 03:59:54 +0000784 /* IP_HDR_SIZE / 4 (not including UDP) */
785 ip->ip_hl_v = 0x45;
wdenke6466f62003-06-05 19:27:42 +0000786 ip->ip_tos = 0;
787 ip->ip_len = htons(IP_HDR_SIZE_NO_UDP + 8);
788 ip->ip_id = htons(NetIPID++);
Peter Tyser0885bf02008-12-01 16:26:20 -0600789 ip->ip_off = htons(IP_FLAGS_DFRAG); /* Don't fragment */
wdenke6466f62003-06-05 19:27:42 +0000790 ip->ip_ttl = 255;
791 ip->ip_p = 0x01; /* ICMP */
792 ip->ip_sum = 0;
Luca Ceresoli5c83a962011-05-11 03:59:54 +0000793 /* already in network byte order */
Luca Ceresolif27d91f2011-05-04 02:40:44 +0000794 NetCopyIP((void *)&ip->ip_src, &NetOurIP);
Luca Ceresoli5c83a962011-05-11 03:59:54 +0000795 /* - "" - */
Luca Ceresolif27d91f2011-05-04 02:40:44 +0000796 NetCopyIP((void *)&ip->ip_dst, &NetPingIP);
wdenke6466f62003-06-05 19:27:42 +0000797 ip->ip_sum = ~NetCksum((uchar *)ip, IP_HDR_SIZE_NO_UDP / 2);
798
799 s = &ip->udp_src; /* XXX ICMP starts here */
800 s[0] = htons(0x0800); /* echo-request, code */
801 s[1] = 0; /* checksum */
Wolfgang Denka1be4762008-05-20 16:00:29 +0200802 s[2] = 0; /* identifier */
wdenke6466f62003-06-05 19:27:42 +0000803 s[3] = htons(PingSeqNo++); /* sequence number */
804 s[1] = ~NetCksum((uchar *)s, 8/2);
805
806 /* size of the waiting packet */
Luca Ceresoli5c83a962011-05-11 03:59:54 +0000807 NetArpWaitTxPacketSize =
808 (pkt - NetArpWaitTxPacket) + IP_HDR_SIZE_NO_UDP + 8;
wdenke6466f62003-06-05 19:27:42 +0000809
810 /* and do the ARP request */
811 NetArpWaitTry = 1;
812 NetArpWaitTimerStart = get_timer(0);
813 ArpRequest();
814 return 1; /* waiting */
815}
816
817static void
Luca Ceresoli7cbd7482011-05-11 03:59:56 +0000818PingTimeout(void)
wdenke6466f62003-06-05 19:27:42 +0000819{
820 eth_halt();
821 NetState = NETLOOP_FAIL; /* we did not get the reply */
822}
823
824static void
Luca Ceresoli428ab362011-04-18 06:19:50 +0000825PingHandler(uchar *pkt, unsigned dest, IPaddr_t sip, unsigned src,
826 unsigned len)
wdenke6466f62003-06-05 19:27:42 +0000827{
Luca Ceresoli428ab362011-04-18 06:19:50 +0000828 if (sip != NetPingIP)
wdenke6466f62003-06-05 19:27:42 +0000829 return;
830
831 NetState = NETLOOP_SUCCESS;
832}
833
834static void PingStart(void)
835{
Luca Ceresoli7cbd7482011-05-11 03:59:56 +0000836 printf("Using %s device\n", eth_get_name());
Luca Ceresoli7cbd7482011-05-11 03:59:56 +0000837 NetSetTimeout(10000UL, PingTimeout);
838 NetSetHandler(PingHandler);
wdenke6466f62003-06-05 19:27:42 +0000839
840 PingSend();
841}
Jon Loeligera9807e52007-07-10 11:05:02 -0500842#endif
wdenk145d2c12004-04-15 21:48:45 +0000843
Jon Loeliger54f35c22007-07-09 17:45:14 -0500844#if defined(CONFIG_CMD_CDP)
wdenk145d2c12004-04-15 21:48:45 +0000845
846#define CDP_DEVICE_ID_TLV 0x0001
847#define CDP_ADDRESS_TLV 0x0002
848#define CDP_PORT_ID_TLV 0x0003
849#define CDP_CAPABILITIES_TLV 0x0004
850#define CDP_VERSION_TLV 0x0005
851#define CDP_PLATFORM_TLV 0x0006
852#define CDP_NATIVE_VLAN_TLV 0x000a
853#define CDP_APPLIANCE_VLAN_TLV 0x000e
854#define CDP_TRIGGER_TLV 0x000f
855#define CDP_POWER_CONSUMPTION_TLV 0x0010
856#define CDP_SYSNAME_TLV 0x0014
857#define CDP_SYSOBJECT_TLV 0x0015
858#define CDP_MANAGEMENT_ADDRESS_TLV 0x0016
859
Bartlomiej Sieka56668462008-10-01 15:26:28 +0200860#define CDP_TIMEOUT 250UL /* one packet every 250ms */
wdenk145d2c12004-04-15 21:48:45 +0000861
862static int CDPSeq;
863static int CDPOK;
864
865ushort CDPNativeVLAN;
866ushort CDPApplianceVLAN;
867
Luca Ceresoli5c83a962011-05-11 03:59:54 +0000868static const uchar CDP_SNAP_hdr[8] = { 0xAA, 0xAA, 0x03, 0x00, 0x00, 0x0C, 0x20,
869 0x00 };
wdenk145d2c12004-04-15 21:48:45 +0000870
871static ushort CDP_compute_csum(const uchar *buff, ushort len)
872{
873 ushort csum;
874 int odd;
875 ulong result = 0;
876 ushort leftover;
Wolfgang Denk7fb52662005-10-13 16:45:02 +0200877 ushort *p;
wdenk145d2c12004-04-15 21:48:45 +0000878
879 if (len > 0) {
880 odd = 1 & (ulong)buff;
881 if (odd) {
882 result = *buff << 8;
883 len--;
884 buff++;
885 }
886 while (len > 1) {
Wolfgang Denk7fb52662005-10-13 16:45:02 +0200887 p = (ushort *)buff;
888 result += *p++;
889 buff = (uchar *)p;
wdenk145d2c12004-04-15 21:48:45 +0000890 if (result & 0x80000000)
891 result = (result & 0xFFFF) + (result >> 16);
892 len -= 2;
893 }
894 if (len) {
895 leftover = (signed short)(*(const signed char *)buff);
Wolfgang Denkf0711132005-11-10 20:59:46 +0100896 /* CISCO SUCKS big time! (and blows too):
897 * CDP uses the IP checksum algorithm with a twist;
898 * for the last byte it *sign* extends and sums.
899 */
Luca Ceresoli5c83a962011-05-11 03:59:54 +0000900 result = (result & 0xffff0000) |
901 ((result + leftover) & 0x0000ffff);
wdenk145d2c12004-04-15 21:48:45 +0000902 }
903 while (result >> 16)
904 result = (result & 0xFFFF) + (result >> 16);
905
906 if (odd)
Luca Ceresoli5c83a962011-05-11 03:59:54 +0000907 result = ((result >> 8) & 0xff) |
908 ((result & 0xff) << 8);
wdenk145d2c12004-04-15 21:48:45 +0000909 }
910
911 /* add up 16-bit and 17-bit words for 17+c bits */
912 result = (result & 0xffff) + (result >> 16);
913 /* add up 16-bit and 2-bit for 16+c bit */
914 result = (result & 0xffff) + (result >> 16);
915 /* add up carry.. */
916 result = (result & 0xffff) + (result >> 16);
917
918 /* negate */
919 csum = ~(ushort)result;
920
921 /* run time endian detection */
922 if (csum != htons(csum)) /* little endian */
923 csum = htons(csum);
924
925 return csum;
926}
927
928int CDPSendTrigger(void)
929{
Joe Hershberger4b7747e2012-05-15 08:59:04 +0000930 uchar *pkt;
931 ushort *s;
932 ushort *cp;
wdenk145d2c12004-04-15 21:48:45 +0000933 Ethernet_t *et;
wdenk145d2c12004-04-15 21:48:45 +0000934 int len;
935 ushort chksum;
Luca Ceresoli7cbd7482011-05-11 03:59:56 +0000936#if defined(CONFIG_CDP_DEVICE_ID) || defined(CONFIG_CDP_PORT_ID) || \
937 defined(CONFIG_CDP_VERSION) || defined(CONFIG_CDP_PLATFORM)
wdenk05939202004-04-18 17:39:38 +0000938 char buf[32];
939#endif
wdenk145d2c12004-04-15 21:48:45 +0000940
941 pkt = NetTxPacket;
942 et = (Ethernet_t *)pkt;
943
944 /* NOTE: trigger sent not on any VLAN */
945
946 /* form ethernet header */
947 memcpy(et->et_dest, NetCDPAddr, 6);
948 memcpy(et->et_src, NetOurEther, 6);
949
950 pkt += ETHER_HDR_SIZE;
951
952 /* SNAP header */
953 memcpy((uchar *)pkt, CDP_SNAP_hdr, sizeof(CDP_SNAP_hdr));
954 pkt += sizeof(CDP_SNAP_hdr);
955
956 /* CDP header */
957 *pkt++ = 0x02; /* CDP version 2 */
958 *pkt++ = 180; /* TTL */
Joe Hershberger4b7747e2012-05-15 08:59:04 +0000959 s = (ushort *)pkt;
wdenk145d2c12004-04-15 21:48:45 +0000960 cp = s;
Luca Ceresoli5c83a962011-05-11 03:59:54 +0000961 /* checksum (0 for later calculation) */
962 *s++ = htons(0);
wdenk145d2c12004-04-15 21:48:45 +0000963
964 /* CDP fields */
965#ifdef CONFIG_CDP_DEVICE_ID
966 *s++ = htons(CDP_DEVICE_ID_TLV);
967 *s++ = htons(CONFIG_CDP_DEVICE_ID);
Mike Frysinger79ca1b92009-02-11 18:23:48 -0500968 sprintf(buf, CONFIG_CDP_DEVICE_ID_PREFIX "%pm", NetOurEther);
wdenk145d2c12004-04-15 21:48:45 +0000969 memcpy((uchar *)s, buf, 16);
970 s += 16 / 2;
971#endif
972
973#ifdef CONFIG_CDP_PORT_ID
974 *s++ = htons(CDP_PORT_ID_TLV);
975 memset(buf, 0, sizeof(buf));
976 sprintf(buf, CONFIG_CDP_PORT_ID, eth_get_dev_index());
977 len = strlen(buf);
978 if (len & 1) /* make it even */
979 len++;
980 *s++ = htons(len + 4);
981 memcpy((uchar *)s, buf, len);
982 s += len / 2;
983#endif
984
985#ifdef CONFIG_CDP_CAPABILITIES
986 *s++ = htons(CDP_CAPABILITIES_TLV);
987 *s++ = htons(8);
988 *(ulong *)s = htonl(CONFIG_CDP_CAPABILITIES);
989 s += 2;
990#endif
991
992#ifdef CONFIG_CDP_VERSION
993 *s++ = htons(CDP_VERSION_TLV);
994 memset(buf, 0, sizeof(buf));
995 strcpy(buf, CONFIG_CDP_VERSION);
996 len = strlen(buf);
997 if (len & 1) /* make it even */
998 len++;
999 *s++ = htons(len + 4);
1000 memcpy((uchar *)s, buf, len);
1001 s += len / 2;
1002#endif
1003
1004#ifdef CONFIG_CDP_PLATFORM
1005 *s++ = htons(CDP_PLATFORM_TLV);
1006 memset(buf, 0, sizeof(buf));
1007 strcpy(buf, CONFIG_CDP_PLATFORM);
1008 len = strlen(buf);
1009 if (len & 1) /* make it even */
1010 len++;
1011 *s++ = htons(len + 4);
1012 memcpy((uchar *)s, buf, len);
1013 s += len / 2;
1014#endif
1015
1016#ifdef CONFIG_CDP_TRIGGER
1017 *s++ = htons(CDP_TRIGGER_TLV);
1018 *s++ = htons(8);
1019 *(ulong *)s = htonl(CONFIG_CDP_TRIGGER);
1020 s += 2;
1021#endif
1022
1023#ifdef CONFIG_CDP_POWER_CONSUMPTION
1024 *s++ = htons(CDP_POWER_CONSUMPTION_TLV);
1025 *s++ = htons(6);
1026 *s++ = htons(CONFIG_CDP_POWER_CONSUMPTION);
1027#endif
1028
1029 /* length of ethernet packet */
1030 len = (uchar *)s - ((uchar *)NetTxPacket + ETHER_HDR_SIZE);
1031 et->et_protlen = htons(len);
1032
1033 len = ETHER_HDR_SIZE + sizeof(CDP_SNAP_hdr);
Luca Ceresoli5c83a962011-05-11 03:59:54 +00001034 chksum = CDP_compute_csum((uchar *)NetTxPacket + len,
1035 (uchar *)s - (NetTxPacket + len));
wdenk145d2c12004-04-15 21:48:45 +00001036 if (chksum == 0)
1037 chksum = 0xFFFF;
1038 *cp = htons(chksum);
1039
1040 (void) eth_send(NetTxPacket, (uchar *)s - NetTxPacket);
1041 return 0;
1042}
1043
1044static void
Luca Ceresoli7cbd7482011-05-11 03:59:56 +00001045CDPTimeout(void)
wdenk145d2c12004-04-15 21:48:45 +00001046{
1047 CDPSeq++;
1048
1049 if (CDPSeq < 3) {
Luca Ceresoli7cbd7482011-05-11 03:59:56 +00001050 NetSetTimeout(CDP_TIMEOUT, CDPTimeout);
wdenk145d2c12004-04-15 21:48:45 +00001051 CDPSendTrigger();
1052 return;
1053 }
1054
1055 /* if not OK try again */
1056 if (!CDPOK)
1057 NetStartAgain();
1058 else
1059 NetState = NETLOOP_SUCCESS;
1060}
1061
1062static void
Luca Ceresoli428ab362011-04-18 06:19:50 +00001063CDPDummyHandler(uchar *pkt, unsigned dest, IPaddr_t sip, unsigned src,
1064 unsigned len)
wdenk145d2c12004-04-15 21:48:45 +00001065{
1066 /* nothing */
1067}
1068
1069static void
Luca Ceresolif27d91f2011-05-04 02:40:44 +00001070CDPHandler(const uchar *pkt, unsigned len)
wdenk145d2c12004-04-15 21:48:45 +00001071{
1072 const uchar *t;
1073 const ushort *ss;
1074 ushort type, tlen;
wdenk145d2c12004-04-15 21:48:45 +00001075 ushort vlan, nvlan;
1076
1077 /* minimum size? */
1078 if (len < sizeof(CDP_SNAP_hdr) + 4)
1079 goto pkt_short;
1080
1081 /* check for valid CDP SNAP header */
1082 if (memcmp(pkt, CDP_SNAP_hdr, sizeof(CDP_SNAP_hdr)) != 0)
1083 return;
1084
1085 pkt += sizeof(CDP_SNAP_hdr);
1086 len -= sizeof(CDP_SNAP_hdr);
1087
1088 /* Version of CDP protocol must be >= 2 and TTL != 0 */
1089 if (pkt[0] < 0x02 || pkt[1] == 0)
1090 return;
1091
Luca Ceresoli5c83a962011-05-11 03:59:54 +00001092 /*
1093 * if version is greater than 0x02 maybe we'll have a problem;
1094 * output a warning
1095 */
wdenk145d2c12004-04-15 21:48:45 +00001096 if (pkt[0] != 0x02)
Joe Hershberger77001b432012-05-15 08:59:08 +00001097 printf("**WARNING: CDP packet received with a protocol version "
1098 "%d > 2\n", pkt[0] & 0xff);
wdenk145d2c12004-04-15 21:48:45 +00001099
1100 if (CDP_compute_csum(pkt, len) != 0)
1101 return;
1102
1103 pkt += 4;
1104 len -= 4;
1105
1106 vlan = htons(-1);
1107 nvlan = htons(-1);
1108 while (len > 0) {
1109 if (len < 4)
1110 goto pkt_short;
1111
1112 ss = (const ushort *)pkt;
1113 type = ntohs(ss[0]);
1114 tlen = ntohs(ss[1]);
Luca Ceresolie8ccbb02011-05-04 02:40:43 +00001115 if (tlen > len)
wdenk145d2c12004-04-15 21:48:45 +00001116 goto pkt_short;
wdenk145d2c12004-04-15 21:48:45 +00001117
1118 pkt += tlen;
1119 len -= tlen;
1120
1121 ss += 2; /* point ss to the data of the TLV */
1122 tlen -= 4;
1123
1124 switch (type) {
Luca Ceresolib19d0b72011-05-04 02:40:46 +00001125 case CDP_DEVICE_ID_TLV:
1126 break;
1127 case CDP_ADDRESS_TLV:
1128 break;
1129 case CDP_PORT_ID_TLV:
1130 break;
1131 case CDP_CAPABILITIES_TLV:
1132 break;
1133 case CDP_VERSION_TLV:
1134 break;
1135 case CDP_PLATFORM_TLV:
1136 break;
1137 case CDP_NATIVE_VLAN_TLV:
1138 nvlan = *ss;
1139 break;
1140 case CDP_APPLIANCE_VLAN_TLV:
1141 t = (const uchar *)ss;
1142 while (tlen > 0) {
1143 if (tlen < 3)
1144 goto pkt_short;
wdenk145d2c12004-04-15 21:48:45 +00001145
Luca Ceresolib19d0b72011-05-04 02:40:46 +00001146 ss = (const ushort *)(t + 1);
wdenk145d2c12004-04-15 21:48:45 +00001147
1148#ifdef CONFIG_CDP_APPLIANCE_VLAN_TYPE
Wolfgang Denk3de85dd2011-11-04 15:55:42 +00001149 if (t[0] == CONFIG_CDP_APPLIANCE_VLAN_TYPE)
Luca Ceresolib19d0b72011-05-04 02:40:46 +00001150 vlan = *ss;
wdenk145d2c12004-04-15 21:48:45 +00001151#else
Luca Ceresolib19d0b72011-05-04 02:40:46 +00001152 /* XXX will this work; dunno */
1153 vlan = ntohs(*ss);
wdenk145d2c12004-04-15 21:48:45 +00001154#endif
Luca Ceresolib19d0b72011-05-04 02:40:46 +00001155 t += 3; tlen -= 3;
1156 }
1157 break;
1158 case CDP_TRIGGER_TLV:
1159 break;
1160 case CDP_POWER_CONSUMPTION_TLV:
1161 break;
1162 case CDP_SYSNAME_TLV:
1163 break;
1164 case CDP_SYSOBJECT_TLV:
1165 break;
1166 case CDP_MANAGEMENT_ADDRESS_TLV:
1167 break;
wdenk145d2c12004-04-15 21:48:45 +00001168 }
1169 }
1170
1171 CDPApplianceVLAN = vlan;
1172 CDPNativeVLAN = nvlan;
1173
1174 CDPOK = 1;
1175 return;
1176
1177 pkt_short:
1178 printf("** CDP packet is too short\n");
1179 return;
1180}
1181
1182static void CDPStart(void)
1183{
Luca Ceresoli7cbd7482011-05-11 03:59:56 +00001184 printf("Using %s device\n", eth_get_name());
wdenk145d2c12004-04-15 21:48:45 +00001185 CDPSeq = 0;
1186 CDPOK = 0;
1187
1188 CDPNativeVLAN = htons(-1);
1189 CDPApplianceVLAN = htons(-1);
1190
Luca Ceresoli7cbd7482011-05-11 03:59:56 +00001191 NetSetTimeout(CDP_TIMEOUT, CDPTimeout);
1192 NetSetHandler(CDPDummyHandler);
wdenk145d2c12004-04-15 21:48:45 +00001193
1194 CDPSendTrigger();
1195}
Jon Loeligera9807e52007-07-10 11:05:02 -05001196#endif
wdenk145d2c12004-04-15 21:48:45 +00001197
Alessandro Rubinif119e3d2009-08-07 13:58:56 +02001198#ifdef CONFIG_IP_DEFRAG
1199/*
1200 * This function collects fragments in a single packet, according
1201 * to the algorithm in RFC815. It returns NULL or the pointer to
1202 * a complete packet, in static storage
1203 */
1204#ifndef CONFIG_NET_MAXDEFRAG
1205#define CONFIG_NET_MAXDEFRAG 16384
1206#endif
1207/*
1208 * MAXDEFRAG, above, is chosen in the config file and is real data
1209 * so we need to add the NFS overhead, which is more than TFTP.
1210 * To use sizeof in the internal unnamed structures, we need a real
1211 * instance (can't do "sizeof(struct rpc_t.u.reply))", unfortunately).
1212 * The compiler doesn't complain nor allocates the actual structure
1213 */
1214static struct rpc_t rpc_specimen;
1215#define IP_PKTSIZE (CONFIG_NET_MAXDEFRAG + sizeof(rpc_specimen.u.reply))
1216
1217#define IP_MAXUDP (IP_PKTSIZE - IP_HDR_SIZE_NO_UDP)
1218
1219/*
1220 * this is the packet being assembled, either data or frag control.
1221 * Fragments go by 8 bytes, so this union must be 8 bytes long
1222 */
1223struct hole {
1224 /* first_byte is address of this structure */
1225 u16 last_byte; /* last byte in this hole + 1 (begin of next hole) */
1226 u16 next_hole; /* index of next (in 8-b blocks), 0 == none */
1227 u16 prev_hole; /* index of prev, 0 == none */
1228 u16 unused;
1229};
1230
1231static IP_t *__NetDefragment(IP_t *ip, int *lenp)
1232{
Joe Hershberger77001b432012-05-15 08:59:08 +00001233 static uchar pkt_buff[IP_PKTSIZE] __aligned(PKTALIGN);
Alessandro Rubinif119e3d2009-08-07 13:58:56 +02001234 static u16 first_hole, total_len;
1235 struct hole *payload, *thisfrag, *h, *newh;
1236 IP_t *localip = (IP_t *)pkt_buff;
1237 uchar *indata = (uchar *)ip;
1238 int offset8, start, len, done = 0;
1239 u16 ip_off = ntohs(ip->ip_off);
1240
1241 /* payload starts after IP header, this fragment is in there */
1242 payload = (struct hole *)(pkt_buff + IP_HDR_SIZE_NO_UDP);
1243 offset8 = (ip_off & IP_OFFS);
1244 thisfrag = payload + offset8;
1245 start = offset8 * 8;
1246 len = ntohs(ip->ip_len) - IP_HDR_SIZE_NO_UDP;
1247
1248 if (start + len > IP_MAXUDP) /* fragment extends too far */
1249 return NULL;
1250
1251 if (!total_len || localip->ip_id != ip->ip_id) {
1252 /* new (or different) packet, reset structs */
1253 total_len = 0xffff;
1254 payload[0].last_byte = ~0;
1255 payload[0].next_hole = 0;
1256 payload[0].prev_hole = 0;
1257 first_hole = 0;
1258 /* any IP header will work, copy the first we received */
1259 memcpy(localip, ip, IP_HDR_SIZE_NO_UDP);
1260 }
1261
1262 /*
1263 * What follows is the reassembly algorithm. We use the payload
1264 * array as a linked list of hole descriptors, as each hole starts
1265 * at a multiple of 8 bytes. However, last byte can be whatever value,
1266 * so it is represented as byte count, not as 8-byte blocks.
1267 */
1268
1269 h = payload + first_hole;
1270 while (h->last_byte < start) {
1271 if (!h->next_hole) {
1272 /* no hole that far away */
1273 return NULL;
1274 }
1275 h = payload + h->next_hole;
1276 }
1277
Fillod Stephanee7ade8b2010-06-11 19:26:43 +02001278 /* last fragment may be 1..7 bytes, the "+7" forces acceptance */
1279 if (offset8 + ((len + 7) / 8) <= h - payload) {
Alessandro Rubinif119e3d2009-08-07 13:58:56 +02001280 /* no overlap with holes (dup fragment?) */
1281 return NULL;
1282 }
1283
1284 if (!(ip_off & IP_FLAGS_MFRAG)) {
1285 /* no more fragmentss: truncate this (last) hole */
1286 total_len = start + len;
1287 h->last_byte = start + len;
1288 }
1289
1290 /*
1291 * There is some overlap: fix the hole list. This code doesn't
1292 * deal with a fragment that overlaps with two different holes
1293 * (thus being a superset of a previously-received fragment).
1294 */
1295
Luca Ceresoli7cbd7482011-05-11 03:59:56 +00001296 if ((h >= thisfrag) && (h->last_byte <= start + len)) {
Alessandro Rubinif119e3d2009-08-07 13:58:56 +02001297 /* complete overlap with hole: remove hole */
1298 if (!h->prev_hole && !h->next_hole) {
1299 /* last remaining hole */
1300 done = 1;
1301 } else if (!h->prev_hole) {
1302 /* first hole */
1303 first_hole = h->next_hole;
1304 payload[h->next_hole].prev_hole = 0;
1305 } else if (!h->next_hole) {
1306 /* last hole */
1307 payload[h->prev_hole].next_hole = 0;
1308 } else {
1309 /* in the middle of the list */
1310 payload[h->next_hole].prev_hole = h->prev_hole;
1311 payload[h->prev_hole].next_hole = h->next_hole;
1312 }
1313
1314 } else if (h->last_byte <= start + len) {
1315 /* overlaps with final part of the hole: shorten this hole */
1316 h->last_byte = start;
1317
1318 } else if (h >= thisfrag) {
1319 /* overlaps with initial part of the hole: move this hole */
1320 newh = thisfrag + (len / 8);
1321 *newh = *h;
1322 h = newh;
1323 if (h->next_hole)
1324 payload[h->next_hole].prev_hole = (h - payload);
1325 if (h->prev_hole)
1326 payload[h->prev_hole].next_hole = (h - payload);
1327 else
1328 first_hole = (h - payload);
1329
1330 } else {
1331 /* fragment sits in the middle: split the hole */
1332 newh = thisfrag + (len / 8);
1333 *newh = *h;
1334 h->last_byte = start;
1335 h->next_hole = (newh - payload);
1336 newh->prev_hole = (h - payload);
1337 if (newh->next_hole)
1338 payload[newh->next_hole].prev_hole = (newh - payload);
1339 }
1340
1341 /* finally copy this fragment and possibly return whole packet */
1342 memcpy((uchar *)thisfrag, indata + IP_HDR_SIZE_NO_UDP, len);
1343 if (!done)
1344 return NULL;
1345
1346 localip->ip_len = htons(total_len);
1347 *lenp = total_len + IP_HDR_SIZE_NO_UDP;
1348 return localip;
1349}
1350
1351static inline IP_t *NetDefragment(IP_t *ip, int *lenp)
1352{
1353 u16 ip_off = ntohs(ip->ip_off);
1354 if (!(ip_off & (IP_OFFS | IP_FLAGS_MFRAG)))
1355 return ip; /* not a fragment */
1356 return __NetDefragment(ip, lenp);
1357}
1358
1359#else /* !CONFIG_IP_DEFRAG */
1360
1361static inline IP_t *NetDefragment(IP_t *ip, int *lenp)
1362{
1363 u16 ip_off = ntohs(ip->ip_off);
1364 if (!(ip_off & (IP_OFFS | IP_FLAGS_MFRAG)))
1365 return ip; /* not a fragment */
1366 return NULL;
1367}
1368#endif
wdenk2d966952002-10-31 22:12:35 +00001369
Simon Glass43c72962011-10-24 18:00:00 +00001370/**
1371 * Receive an ICMP packet. We deal with REDIRECT and PING here, and silently
1372 * drop others.
1373 *
1374 * @parma ip IP packet containing the ICMP
1375 */
1376static void receive_icmp(IP_t *ip, int len, IPaddr_t src_ip, Ethernet_t *et)
1377{
1378 ICMP_t *icmph = (ICMP_t *)&ip->udp_src;
1379
1380 switch (icmph->type) {
1381 case ICMP_REDIRECT:
1382 if (icmph->code != ICMP_REDIR_HOST)
1383 return;
1384 printf(" ICMP Host Redirect to %pI4 ",
1385 &icmph->un.gateway);
1386 break;
1387#if defined(CONFIG_CMD_PING)
1388 case ICMP_ECHO_REPLY:
1389 /*
1390 * IP header OK. Pass the packet to the
1391 * current handler.
1392 */
1393 /*
1394 * XXX point to ip packet - should this use
1395 * packet_icmp_handler?
1396 */
1397 (*packetHandler)((uchar *)ip, 0, src_ip, 0, 0);
1398 break;
1399 case ICMP_ECHO_REQUEST:
1400 debug("Got ICMP ECHO REQUEST, return %d bytes\n",
1401 ETHER_HDR_SIZE + len);
1402
1403 memcpy(&et->et_dest[0], &et->et_src[0], 6);
1404 memcpy(&et->et_src[0], NetOurEther, 6);
1405
1406 ip->ip_sum = 0;
1407 ip->ip_off = 0;
1408 NetCopyIP((void *)&ip->ip_dst, &ip->ip_src);
1409 NetCopyIP((void *)&ip->ip_src, &NetOurIP);
1410 ip->ip_sum = ~NetCksum((uchar *)ip,
1411 IP_HDR_SIZE_NO_UDP >> 1);
1412
1413 icmph->type = ICMP_ECHO_REPLY;
1414 icmph->checksum = 0;
1415 icmph->checksum = ~NetCksum((uchar *)icmph,
1416 (len - IP_HDR_SIZE_NO_UDP) >> 1);
1417 (void) eth_send((uchar *)et,
1418 ETHER_HDR_SIZE + len);
1419 break;
1420#endif
1421 default:
Simon Glass2928cd82011-10-26 14:18:38 +00001422#ifdef CONFIG_CMD_TFTPPUT
Simon Glass230467c2011-10-24 18:00:01 +00001423 if (packet_icmp_handler)
1424 packet_icmp_handler(icmph->type, icmph->code,
1425 ntohs(ip->udp_dst), src_ip, ntohs(ip->udp_src),
1426 icmph->un.data, ntohs(ip->udp_len));
Simon Glass2928cd82011-10-26 14:18:38 +00001427#endif
Simon Glass43c72962011-10-24 18:00:00 +00001428 break;
1429 }
1430}
1431
wdenk2d966952002-10-31 22:12:35 +00001432void
Joe Hershberger4b7747e2012-05-15 08:59:04 +00001433NetReceive(uchar *inpkt, int len)
wdenk2d966952002-10-31 22:12:35 +00001434{
1435 Ethernet_t *et;
1436 IP_t *ip;
1437 ARP_t *arp;
1438 IPaddr_t tmp;
Luca Ceresoli428ab362011-04-18 06:19:50 +00001439 IPaddr_t src_ip;
wdenk2d966952002-10-31 22:12:35 +00001440 int x;
wdenk145d2c12004-04-15 21:48:45 +00001441 uchar *pkt;
Jon Loeliger54f35c22007-07-09 17:45:14 -05001442#if defined(CONFIG_CMD_CDP)
wdenk145d2c12004-04-15 21:48:45 +00001443 int iscdp;
1444#endif
1445 ushort cti = 0, vlanid = VLAN_NONE, myvlanid, mynvlanid;
wdenk2d966952002-10-31 22:12:35 +00001446
Robin Getz9e0a4d62009-07-23 03:01:03 -04001447 debug("packet received\n");
wdenk145d2c12004-04-15 21:48:45 +00001448
Mike Frysingerdc166032009-07-18 21:04:08 -04001449 NetRxPacket = inpkt;
1450 NetRxPacketLen = len;
wdenk145d2c12004-04-15 21:48:45 +00001451 et = (Ethernet_t *)inpkt;
1452
1453 /* too small packet? */
1454 if (len < ETHER_HDR_SIZE)
1455 return;
Rafal Jaworowski1f2c9a42007-12-27 18:19:02 +01001456
1457#ifdef CONFIG_API
1458 if (push_packet) {
1459 (*push_packet)(inpkt, len);
1460 return;
1461 }
1462#endif
wdenk145d2c12004-04-15 21:48:45 +00001463
Jon Loeliger54f35c22007-07-09 17:45:14 -05001464#if defined(CONFIG_CMD_CDP)
wdenk145d2c12004-04-15 21:48:45 +00001465 /* keep track if packet is CDP */
1466 iscdp = memcmp(et->et_dest, NetCDPAddr, 6) == 0;
1467#endif
1468
1469 myvlanid = ntohs(NetOurVLAN);
1470 if (myvlanid == (ushort)-1)
1471 myvlanid = VLAN_NONE;
1472 mynvlanid = ntohs(NetOurNativeVLAN);
1473 if (mynvlanid == (ushort)-1)
1474 mynvlanid = VLAN_NONE;
wdenk2d966952002-10-31 22:12:35 +00001475
1476 x = ntohs(et->et_protlen);
1477
Robin Getz9e0a4d62009-07-23 03:01:03 -04001478 debug("packet received\n");
wdenk145d2c12004-04-15 21:48:45 +00001479
wdenk2d966952002-10-31 22:12:35 +00001480 if (x < 1514) {
1481 /*
1482 * Got a 802 packet. Check the other protocol field.
1483 */
1484 x = ntohs(et->et_prot);
wdenk145d2c12004-04-15 21:48:45 +00001485
1486 ip = (IP_t *)(inpkt + E802_HDR_SIZE);
wdenk2d966952002-10-31 22:12:35 +00001487 len -= E802_HDR_SIZE;
wdenk145d2c12004-04-15 21:48:45 +00001488
1489 } else if (x != PROT_VLAN) { /* normal packet */
1490 ip = (IP_t *)(inpkt + ETHER_HDR_SIZE);
wdenk2d966952002-10-31 22:12:35 +00001491 len -= ETHER_HDR_SIZE;
wdenk145d2c12004-04-15 21:48:45 +00001492
1493 } else { /* VLAN packet */
1494 VLAN_Ethernet_t *vet = (VLAN_Ethernet_t *)et;
1495
Robin Getz9e0a4d62009-07-23 03:01:03 -04001496 debug("VLAN packet received\n");
1497
wdenk145d2c12004-04-15 21:48:45 +00001498 /* too small packet? */
1499 if (len < VLAN_ETHER_HDR_SIZE)
1500 return;
1501
1502 /* if no VLAN active */
1503 if ((ntohs(NetOurVLAN) & VLAN_IDMASK) == VLAN_NONE
Jon Loeliger54f35c22007-07-09 17:45:14 -05001504#if defined(CONFIG_CMD_CDP)
wdenk145d2c12004-04-15 21:48:45 +00001505 && iscdp == 0
1506#endif
1507 )
1508 return;
1509
1510 cti = ntohs(vet->vet_tag);
1511 vlanid = cti & VLAN_IDMASK;
1512 x = ntohs(vet->vet_type);
1513
1514 ip = (IP_t *)(inpkt + VLAN_ETHER_HDR_SIZE);
1515 len -= VLAN_ETHER_HDR_SIZE;
wdenk2d966952002-10-31 22:12:35 +00001516 }
1517
Robin Getz9e0a4d62009-07-23 03:01:03 -04001518 debug("Receive from protocol 0x%x\n", x);
wdenk2d966952002-10-31 22:12:35 +00001519
Jon Loeliger54f35c22007-07-09 17:45:14 -05001520#if defined(CONFIG_CMD_CDP)
wdenk145d2c12004-04-15 21:48:45 +00001521 if (iscdp) {
1522 CDPHandler((uchar *)ip, len);
1523 return;
1524 }
1525#endif
1526
1527 if ((myvlanid & VLAN_IDMASK) != VLAN_NONE) {
1528 if (vlanid == VLAN_NONE)
1529 vlanid = (mynvlanid & VLAN_IDMASK);
1530 /* not matched? */
1531 if (vlanid != (myvlanid & VLAN_IDMASK))
1532 return;
1533 }
1534
wdenk2d966952002-10-31 22:12:35 +00001535 switch (x) {
1536
1537 case PROT_ARP:
1538 /*
1539 * We have to deal with two types of ARP packets:
wdenk57b2d802003-06-27 21:31:46 +00001540 * - REQUEST packets will be answered by sending our
1541 * IP address - if we know it.
1542 * - REPLY packates are expected only after we asked
1543 * for the TFTP server's or the gateway's ethernet
1544 * address; so if we receive such a packet, we set
1545 * the server ethernet address
wdenk2d966952002-10-31 22:12:35 +00001546 */
Robin Getz9e0a4d62009-07-23 03:01:03 -04001547 debug("Got ARP\n");
1548
wdenk2d966952002-10-31 22:12:35 +00001549 arp = (ARP_t *)ip;
1550 if (len < ARP_HDR_SIZE) {
1551 printf("bad length %d < %d\n", len, ARP_HDR_SIZE);
1552 return;
1553 }
Luca Ceresolie8ccbb02011-05-04 02:40:43 +00001554 if (ntohs(arp->ar_hrd) != ARP_ETHER)
wdenk2d966952002-10-31 22:12:35 +00001555 return;
Luca Ceresolie8ccbb02011-05-04 02:40:43 +00001556 if (ntohs(arp->ar_pro) != PROT_IP)
wdenk2d966952002-10-31 22:12:35 +00001557 return;
Luca Ceresolie8ccbb02011-05-04 02:40:43 +00001558 if (arp->ar_hln != 6)
wdenk2d966952002-10-31 22:12:35 +00001559 return;
Luca Ceresolie8ccbb02011-05-04 02:40:43 +00001560 if (arp->ar_pln != 4)
wdenk2d966952002-10-31 22:12:35 +00001561 return;
wdenk2d966952002-10-31 22:12:35 +00001562
Luca Ceresolie8ccbb02011-05-04 02:40:43 +00001563 if (NetOurIP == 0)
wdenk2d966952002-10-31 22:12:35 +00001564 return;
wdenk2d966952002-10-31 22:12:35 +00001565
Luca Ceresolie8ccbb02011-05-04 02:40:43 +00001566 if (NetReadIP(&arp->ar_data[16]) != NetOurIP)
wdenk2d966952002-10-31 22:12:35 +00001567 return;
wdenk2d966952002-10-31 22:12:35 +00001568
1569 switch (ntohs(arp->ar_op)) {
Luca Ceresoli5c83a962011-05-11 03:59:54 +00001570 case ARPOP_REQUEST:
1571 /* reply with our IP address */
Robin Getz9e0a4d62009-07-23 03:01:03 -04001572 debug("Got ARP REQUEST, return our IP\n");
wdenk145d2c12004-04-15 21:48:45 +00001573 pkt = (uchar *)et;
1574 pkt += NetSetEther(pkt, et->et_src, PROT_ARP);
wdenk2d966952002-10-31 22:12:35 +00001575 arp->ar_op = htons(ARPOP_REPLY);
Luca Ceresoli7cbd7482011-05-11 03:59:56 +00001576 memcpy(&arp->ar_data[10], &arp->ar_data[0], 6);
wdenk2d966952002-10-31 22:12:35 +00001577 NetCopyIP(&arp->ar_data[16], &arp->ar_data[6]);
Luca Ceresoli7cbd7482011-05-11 03:59:56 +00001578 memcpy(&arp->ar_data[0], NetOurEther, 6);
1579 NetCopyIP(&arp->ar_data[6], &NetOurIP);
Luca Ceresoli5c83a962011-05-11 03:59:54 +00001580 (void) eth_send((uchar *)et,
1581 (pkt - (uchar *)et) + ARP_HDR_SIZE);
wdenk2d966952002-10-31 22:12:35 +00001582 return;
wdenke6466f62003-06-05 19:27:42 +00001583
1584 case ARPOP_REPLY: /* arp reply */
1585 /* are we waiting for a reply */
1586 if (!NetArpWaitPacketIP || !NetArpWaitPacketMAC)
1587 break;
Robin Getz470a6d42009-07-21 12:15:28 -04001588
1589#ifdef CONFIG_KEEP_SERVERADDR
1590 if (NetServerIP == NetArpWaitPacketIP) {
1591 char buf[20];
1592 sprintf(buf, "%pM", arp->ar_data);
1593 setenv("serveraddr", buf);
1594 }
1595#endif
1596
Robin Getz9e0a4d62009-07-23 03:01:03 -04001597 debug("Got ARP REPLY, set server/gtwy eth addr (%pM)\n",
Mike Frysinger79ca1b92009-02-11 18:23:48 -05001598 arp->ar_data);
wdenke6466f62003-06-05 19:27:42 +00001599
1600 tmp = NetReadIP(&arp->ar_data[6]);
1601
1602 /* matched waiting packet's address */
1603 if (tmp == NetArpWaitReplyIP) {
Robin Getz9e0a4d62009-07-23 03:01:03 -04001604 debug("Got it\n");
Joe Hershberger77001b432012-05-15 08:59:08 +00001605
wdenke6466f62003-06-05 19:27:42 +00001606 /* save address for later use */
Luca Ceresoli5c83a962011-05-11 03:59:54 +00001607 memcpy(NetArpWaitPacketMAC,
1608 &arp->ar_data[0], 6);
wdenke6466f62003-06-05 19:27:42 +00001609
wdenkb8fb6192004-08-02 21:11:11 +00001610#ifdef CONFIG_NETCONSOLE
Luca Ceresoli428ab362011-04-18 06:19:50 +00001611 (*packetHandler)(0, 0, 0, 0, 0);
wdenkb8fb6192004-08-02 21:11:11 +00001612#endif
wdenke6466f62003-06-05 19:27:42 +00001613 /* modify header, and transmit it */
Joe Hershberger77001b432012-05-15 08:59:08 +00001614 memcpy(((Ethernet_t *)NetArpWaitTxPacket)->
1615 et_dest, NetArpWaitPacketMAC, 6);
Luca Ceresoli5c83a962011-05-11 03:59:54 +00001616 (void) eth_send(NetArpWaitTxPacket,
1617 NetArpWaitTxPacketSize);
wdenke6466f62003-06-05 19:27:42 +00001618
1619 /* no arp request pending now */
1620 NetArpWaitPacketIP = 0;
1621 NetArpWaitTxPacketSize = 0;
1622 NetArpWaitPacketMAC = NULL;
1623
1624 }
wdenk2d966952002-10-31 22:12:35 +00001625 return;
1626 default:
Luca Ceresoli5c83a962011-05-11 03:59:54 +00001627 debug("Unexpected ARP opcode 0x%x\n",
1628 ntohs(arp->ar_op));
wdenk2d966952002-10-31 22:12:35 +00001629 return;
1630 }
wdenkcb99da52005-01-12 00:15:14 +00001631 break;
wdenk2d966952002-10-31 22:12:35 +00001632
Peter Tyserf7a48ca2010-09-30 11:25:48 -05001633#ifdef CONFIG_CMD_RARP
wdenk2d966952002-10-31 22:12:35 +00001634 case PROT_RARP:
Robin Getz9e0a4d62009-07-23 03:01:03 -04001635 debug("Got RARP\n");
wdenk2d966952002-10-31 22:12:35 +00001636 arp = (ARP_t *)ip;
1637 if (len < ARP_HDR_SIZE) {
1638 printf("bad length %d < %d\n", len, ARP_HDR_SIZE);
1639 return;
1640 }
1641
1642 if ((ntohs(arp->ar_op) != RARPOP_REPLY) ||
1643 (ntohs(arp->ar_hrd) != ARP_ETHER) ||
1644 (ntohs(arp->ar_pro) != PROT_IP) ||
1645 (arp->ar_hln != 6) || (arp->ar_pln != 4)) {
1646
Luca Ceresoli7cbd7482011-05-11 03:59:56 +00001647 puts("invalid RARP header\n");
wdenk2d966952002-10-31 22:12:35 +00001648 } else {
Luca Ceresoli7cbd7482011-05-11 03:59:56 +00001649 NetCopyIP(&NetOurIP, &arp->ar_data[16]);
wdenk4989f872004-03-14 15:06:13 +00001650 if (NetServerIP == 0)
Luca Ceresoli7cbd7482011-05-11 03:59:56 +00001651 NetCopyIP(&NetServerIP, &arp->ar_data[6]);
1652 memcpy(NetServerEther, &arp->ar_data[0], 6);
wdenk2d966952002-10-31 22:12:35 +00001653
Luca Ceresoli428ab362011-04-18 06:19:50 +00001654 (*packetHandler)(0, 0, 0, 0, 0);
wdenk2d966952002-10-31 22:12:35 +00001655 }
1656 break;
Peter Tyserf7a48ca2010-09-30 11:25:48 -05001657#endif
wdenk2d966952002-10-31 22:12:35 +00001658 case PROT_IP:
Robin Getz9e0a4d62009-07-23 03:01:03 -04001659 debug("Got IP\n");
Alessandro Rubinif119e3d2009-08-07 13:58:56 +02001660 /* Before we start poking the header, make sure it is there */
wdenk2d966952002-10-31 22:12:35 +00001661 if (len < IP_HDR_SIZE) {
Robin Getz9e0a4d62009-07-23 03:01:03 -04001662 debug("len bad %d < %lu\n", len, (ulong)IP_HDR_SIZE);
wdenk2d966952002-10-31 22:12:35 +00001663 return;
1664 }
Alessandro Rubinif119e3d2009-08-07 13:58:56 +02001665 /* Check the packet length */
wdenk2d966952002-10-31 22:12:35 +00001666 if (len < ntohs(ip->ip_len)) {
1667 printf("len bad %d < %d\n", len, ntohs(ip->ip_len));
1668 return;
1669 }
1670 len = ntohs(ip->ip_len);
Robin Getz9e0a4d62009-07-23 03:01:03 -04001671 debug("len=%d, v=%02x\n", len, ip->ip_hl_v & 0xff);
1672
Alessandro Rubinif119e3d2009-08-07 13:58:56 +02001673 /* Can't deal with anything except IPv4 */
Luca Ceresolie8ccbb02011-05-04 02:40:43 +00001674 if ((ip->ip_hl_v & 0xf0) != 0x40)
wdenk2d966952002-10-31 22:12:35 +00001675 return;
Alessandro Rubinif119e3d2009-08-07 13:58:56 +02001676 /* Can't deal with IP options (headers != 20 bytes) */
Luca Ceresolie8ccbb02011-05-04 02:40:43 +00001677 if ((ip->ip_hl_v & 0x0f) > 0x05)
Remy Bohmerb9535782008-06-03 15:48:17 +02001678 return;
Alessandro Rubinif119e3d2009-08-07 13:58:56 +02001679 /* Check the Checksum of the header */
wdenk2d966952002-10-31 22:12:35 +00001680 if (!NetCksumOk((uchar *)ip, IP_HDR_SIZE_NO_UDP / 2)) {
Luca Ceresoli7cbd7482011-05-11 03:59:56 +00001681 puts("checksum bad\n");
wdenk2d966952002-10-31 22:12:35 +00001682 return;
1683 }
Alessandro Rubinif119e3d2009-08-07 13:58:56 +02001684 /* If it is not for us, ignore it */
wdenk2d966952002-10-31 22:12:35 +00001685 tmp = NetReadIP(&ip->ip_dst);
1686 if (NetOurIP && tmp != NetOurIP && tmp != 0xFFFFFFFF) {
David Updegraff7280da72007-06-11 10:41:07 -05001687#ifdef CONFIG_MCAST_TFTP
Wolfgang Denk627f5c32007-08-14 09:47:27 +02001688 if (Mcast_addr != tmp)
David Updegraff7280da72007-06-11 10:41:07 -05001689#endif
Luca Ceresolib19d0b72011-05-04 02:40:46 +00001690 return;
wdenk2d966952002-10-31 22:12:35 +00001691 }
Luca Ceresoli428ab362011-04-18 06:19:50 +00001692 /* Read source IP address for later use */
1693 src_ip = NetReadIP(&ip->ip_src);
wdenk2d966952002-10-31 22:12:35 +00001694 /*
Alessandro Rubinif119e3d2009-08-07 13:58:56 +02001695 * The function returns the unchanged packet if it's not
1696 * a fragment, and either the complete packet or NULL if
1697 * it is a fragment (if !CONFIG_IP_DEFRAG, it returns NULL)
1698 */
Luca Ceresolidb210822011-05-04 02:40:47 +00001699 ip = NetDefragment(ip, &len);
1700 if (!ip)
Alessandro Rubinif119e3d2009-08-07 13:58:56 +02001701 return;
1702 /*
wdenk2d966952002-10-31 22:12:35 +00001703 * watch for ICMP host redirects
1704 *
wdenk57b2d802003-06-27 21:31:46 +00001705 * There is no real handler code (yet). We just watch
1706 * for ICMP host redirect messages. In case anybody
1707 * sees these messages: please contact me
1708 * (wd@denx.de), or - even better - send me the
1709 * necessary fixes :-)
wdenk2d966952002-10-31 22:12:35 +00001710 *
wdenk57b2d802003-06-27 21:31:46 +00001711 * Note: in all cases where I have seen this so far
1712 * it was a problem with the router configuration,
1713 * for instance when a router was configured in the
1714 * BOOTP reply, but the TFTP server was on the same
1715 * subnet. So this is probably a warning that your
1716 * configuration might be wrong. But I'm not really
1717 * sure if there aren't any other situations.
Simon Glass230467c2011-10-24 18:00:01 +00001718 *
1719 * Simon Glass <sjg@chromium.org>: We get an ICMP when
1720 * we send a tftp packet to a dead connection, or when
1721 * there is no server at the other end.
wdenk2d966952002-10-31 22:12:35 +00001722 */
1723 if (ip->ip_p == IPPROTO_ICMP) {
Simon Glass43c72962011-10-24 18:00:00 +00001724 receive_icmp(ip, len, src_ip, et);
1725 return;
wdenk2d966952002-10-31 22:12:35 +00001726 } else if (ip->ip_p != IPPROTO_UDP) { /* Only UDP packets */
1727 return;
1728 }
1729
Stefan Roesedfced812005-08-12 20:06:52 +02001730#ifdef CONFIG_UDP_CHECKSUM
1731 if (ip->udp_xsum != 0) {
Wolfgang Denk30b87322005-08-12 23:43:12 +02001732 ulong xsum;
Stefan Roesedfced812005-08-12 20:06:52 +02001733 ushort *sumptr;
1734 ushort sumlen;
1735
1736 xsum = ip->ip_p;
1737 xsum += (ntohs(ip->udp_len));
1738 xsum += (ntohl(ip->ip_src) >> 16) & 0x0000ffff;
1739 xsum += (ntohl(ip->ip_src) >> 0) & 0x0000ffff;
1740 xsum += (ntohl(ip->ip_dst) >> 16) & 0x0000ffff;
1741 xsum += (ntohl(ip->ip_dst) >> 0) & 0x0000ffff;
1742
1743 sumlen = ntohs(ip->udp_len);
1744 sumptr = (ushort *) &(ip->udp_src);
1745
1746 while (sumlen > 1) {
Wolfgang Denk30b87322005-08-12 23:43:12 +02001747 ushort sumdata;
Stefan Roesedfced812005-08-12 20:06:52 +02001748
1749 sumdata = *sumptr++;
1750 xsum += ntohs(sumdata);
1751 sumlen -= 2;
1752 }
1753 if (sumlen > 0) {
Wolfgang Denk30b87322005-08-12 23:43:12 +02001754 ushort sumdata;
Stefan Roesedfced812005-08-12 20:06:52 +02001755
1756 sumdata = *(unsigned char *) sumptr;
Wolfgang Denk30b87322005-08-12 23:43:12 +02001757 sumdata = (sumdata << 8) & 0xff00;
Stefan Roesedfced812005-08-12 20:06:52 +02001758 xsum += sumdata;
1759 }
1760 while ((xsum >> 16) != 0) {
Luca Ceresoli5c83a962011-05-11 03:59:54 +00001761 xsum = (xsum & 0x0000ffff) +
1762 ((xsum >> 16) & 0x0000ffff);
Stefan Roesedfced812005-08-12 20:06:52 +02001763 }
1764 if ((xsum != 0x00000000) && (xsum != 0x0000ffff)) {
Wolfgang Denk12cec0a2008-07-11 01:16:00 +02001765 printf(" UDP wrong checksum %08lx %08x\n",
1766 xsum, ntohs(ip->udp_xsum));
Stefan Roesedfced812005-08-12 20:06:52 +02001767 return;
1768 }
1769 }
1770#endif
1771
David Updegraff7280da72007-06-11 10:41:07 -05001772
wdenkb8fb6192004-08-02 21:11:11 +00001773#ifdef CONFIG_NETCONSOLE
Luca Ceresoli7cbd7482011-05-11 03:59:56 +00001774 nc_input_packet((uchar *)ip + IP_HDR_SIZE,
wdenkb8fb6192004-08-02 21:11:11 +00001775 ntohs(ip->udp_dst),
1776 ntohs(ip->udp_src),
1777 ntohs(ip->udp_len) - 8);
1778#endif
wdenk2d966952002-10-31 22:12:35 +00001779 /*
1780 * IP header OK. Pass the packet to the current handler.
1781 */
Luca Ceresoli7cbd7482011-05-11 03:59:56 +00001782 (*packetHandler)((uchar *)ip + IP_HDR_SIZE,
wdenk2d966952002-10-31 22:12:35 +00001783 ntohs(ip->udp_dst),
Luca Ceresoli428ab362011-04-18 06:19:50 +00001784 src_ip,
wdenk2d966952002-10-31 22:12:35 +00001785 ntohs(ip->udp_src),
1786 ntohs(ip->udp_len) - 8);
wdenk2d966952002-10-31 22:12:35 +00001787 break;
1788 }
1789}
1790
1791
1792/**********************************************************************/
1793
Simon Glassd6c5f552011-10-24 18:00:02 +00001794static int net_check_prereq(enum proto_t protocol)
wdenk2d966952002-10-31 22:12:35 +00001795{
1796 switch (protocol) {
wdenk05939202004-04-18 17:39:38 +00001797 /* Fall through */
Jon Loeliger54f35c22007-07-09 17:45:14 -05001798#if defined(CONFIG_CMD_PING)
wdenke6466f62003-06-05 19:27:42 +00001799 case PING:
wdenk05939202004-04-18 17:39:38 +00001800 if (NetPingIP == 0) {
Luca Ceresoli7cbd7482011-05-11 03:59:56 +00001801 puts("*** ERROR: ping address not given\n");
Luca Ceresoli5fe6de22011-05-04 02:40:45 +00001802 return 1;
wdenk05939202004-04-18 17:39:38 +00001803 }
1804 goto common;
wdenke6466f62003-06-05 19:27:42 +00001805#endif
Jon Loeliger54f35c22007-07-09 17:45:14 -05001806#if defined(CONFIG_CMD_SNTP)
wdenkb4ad9622005-04-01 00:25:43 +00001807 case SNTP:
1808 if (NetNtpServerIP == 0) {
Luca Ceresoli7cbd7482011-05-11 03:59:56 +00001809 puts("*** ERROR: NTP server address not given\n");
Luca Ceresoli5fe6de22011-05-04 02:40:45 +00001810 return 1;
wdenkb4ad9622005-04-01 00:25:43 +00001811 }
1812 goto common;
1813#endif
Robin Getz82f0d232009-07-20 14:53:54 -04001814#if defined(CONFIG_CMD_DNS)
1815 case DNS:
1816 if (NetOurDNSIP == 0) {
1817 puts("*** ERROR: DNS server address not given\n");
1818 return 1;
1819 }
1820 goto common;
1821#endif
Jon Loeliger54f35c22007-07-09 17:45:14 -05001822#if defined(CONFIG_CMD_NFS)
wdenkbe9c1cb2004-02-24 02:00:03 +00001823 case NFS:
1824#endif
Simon Glassd6c5f552011-10-24 18:00:02 +00001825 case TFTPGET:
Simon Glass6a398d22011-10-24 18:00:07 +00001826 case TFTPPUT:
wdenk05939202004-04-18 17:39:38 +00001827 if (NetServerIP == 0) {
Luca Ceresoli7cbd7482011-05-11 03:59:56 +00001828 puts("*** ERROR: `serverip' not set\n");
Luca Ceresoli5fe6de22011-05-04 02:40:45 +00001829 return 1;
wdenk05939202004-04-18 17:39:38 +00001830 }
Luca Ceresoli7cbd7482011-05-11 03:59:56 +00001831#if defined(CONFIG_CMD_PING) || defined(CONFIG_CMD_SNTP) || \
1832 defined(CONFIG_CMD_DNS)
1833common:
wdenke6466f62003-06-05 19:27:42 +00001834#endif
Simon Guinot00dceba2011-05-01 23:38:40 +00001835 /* Fall through */
wdenke6466f62003-06-05 19:27:42 +00001836
Simon Guinot00dceba2011-05-01 23:38:40 +00001837 case NETCONS:
Luca Ceresoli7aa81a42011-05-17 00:03:40 +00001838 case TFTPSRV:
wdenk05939202004-04-18 17:39:38 +00001839 if (NetOurIP == 0) {
Luca Ceresoli7cbd7482011-05-11 03:59:56 +00001840 puts("*** ERROR: `ipaddr' not set\n");
Luca Ceresoli5fe6de22011-05-04 02:40:45 +00001841 return 1;
wdenk05939202004-04-18 17:39:38 +00001842 }
1843 /* Fall through */
wdenk2d966952002-10-31 22:12:35 +00001844
Peter Tyserf7a48ca2010-09-30 11:25:48 -05001845#ifdef CONFIG_CMD_RARP
wdenk2d966952002-10-31 22:12:35 +00001846 case RARP:
Peter Tyserf7a48ca2010-09-30 11:25:48 -05001847#endif
wdenk2d966952002-10-31 22:12:35 +00001848 case BOOTP:
wdenk145d2c12004-04-15 21:48:45 +00001849 case CDP:
Peter Tyserf7a48ca2010-09-30 11:25:48 -05001850 case DHCP:
Luca Ceresoli7cbd7482011-05-11 03:59:56 +00001851 if (memcmp(NetOurEther, "\0\0\0\0\0\0", 6) == 0) {
Luca Ceresoli7cbd7482011-05-11 03:59:56 +00001852 int num = eth_get_dev_index();
wdenk2d966952002-10-31 22:12:35 +00001853
wdenk05939202004-04-18 17:39:38 +00001854 switch (num) {
1855 case -1:
Luca Ceresoli7cbd7482011-05-11 03:59:56 +00001856 puts("*** ERROR: No ethernet found.\n");
Luca Ceresoli5fe6de22011-05-04 02:40:45 +00001857 return 1;
wdenk05939202004-04-18 17:39:38 +00001858 case 0:
Luca Ceresoli7cbd7482011-05-11 03:59:56 +00001859 puts("*** ERROR: `ethaddr' not set\n");
wdenk2d966952002-10-31 22:12:35 +00001860 break;
wdenk05939202004-04-18 17:39:38 +00001861 default:
Luca Ceresoli7cbd7482011-05-11 03:59:56 +00001862 printf("*** ERROR: `eth%daddr' not set\n",
wdenk2d966952002-10-31 22:12:35 +00001863 num);
1864 break;
wdenk05939202004-04-18 17:39:38 +00001865 }
wdenk2d966952002-10-31 22:12:35 +00001866
Luca Ceresoli7cbd7482011-05-11 03:59:56 +00001867 NetStartAgain();
Luca Ceresoli5fe6de22011-05-04 02:40:45 +00001868 return 2;
wdenk05939202004-04-18 17:39:38 +00001869 }
1870 /* Fall through */
1871 default:
Luca Ceresoli5fe6de22011-05-04 02:40:45 +00001872 return 0;
wdenk2d966952002-10-31 22:12:35 +00001873 }
Luca Ceresoli5fe6de22011-05-04 02:40:45 +00001874 return 0; /* OK */
wdenk2d966952002-10-31 22:12:35 +00001875}
1876/**********************************************************************/
1877
1878int
Luca Ceresolif27d91f2011-05-04 02:40:44 +00001879NetCksumOk(uchar *ptr, int len)
wdenk2d966952002-10-31 22:12:35 +00001880{
1881 return !((NetCksum(ptr, len) + 1) & 0xfffe);
1882}
1883
1884
1885unsigned
Luca Ceresolif27d91f2011-05-04 02:40:44 +00001886NetCksum(uchar *ptr, int len)
wdenk2d966952002-10-31 22:12:35 +00001887{
1888 ulong xsum;
Stefan Roeseac553282005-08-31 12:55:50 +02001889 ushort *p = (ushort *)ptr;
wdenk2d966952002-10-31 22:12:35 +00001890
1891 xsum = 0;
1892 while (len-- > 0)
Wolfgang Denk3135c542005-08-26 01:36:03 +02001893 xsum += *p++;
wdenk2d966952002-10-31 22:12:35 +00001894 xsum = (xsum & 0xffff) + (xsum >> 16);
1895 xsum = (xsum & 0xffff) + (xsum >> 16);
Luca Ceresoli5fe6de22011-05-04 02:40:45 +00001896 return xsum & 0xffff;
wdenk2d966952002-10-31 22:12:35 +00001897}
1898
wdenk145d2c12004-04-15 21:48:45 +00001899int
1900NetEthHdrSize(void)
1901{
1902 ushort myvlanid;
1903
1904 myvlanid = ntohs(NetOurVLAN);
1905 if (myvlanid == (ushort)-1)
1906 myvlanid = VLAN_NONE;
wdenk2d966952002-10-31 22:12:35 +00001907
Luca Ceresoli5c83a962011-05-11 03:59:54 +00001908 return ((myvlanid & VLAN_IDMASK) == VLAN_NONE) ? ETHER_HDR_SIZE :
1909 VLAN_ETHER_HDR_SIZE;
wdenk145d2c12004-04-15 21:48:45 +00001910}
1911
1912int
Joe Hershberger4b7747e2012-05-15 08:59:04 +00001913NetSetEther(uchar *xet, uchar * addr, uint prot)
wdenk2d966952002-10-31 22:12:35 +00001914{
1915 Ethernet_t *et = (Ethernet_t *)xet;
wdenk145d2c12004-04-15 21:48:45 +00001916 ushort myvlanid;
1917
1918 myvlanid = ntohs(NetOurVLAN);
1919 if (myvlanid == (ushort)-1)
1920 myvlanid = VLAN_NONE;
wdenk2d966952002-10-31 22:12:35 +00001921
Luca Ceresoli7cbd7482011-05-11 03:59:56 +00001922 memcpy(et->et_dest, addr, 6);
1923 memcpy(et->et_src, NetOurEther, 6);
wdenk145d2c12004-04-15 21:48:45 +00001924 if ((myvlanid & VLAN_IDMASK) == VLAN_NONE) {
Luca Ceresolib19d0b72011-05-04 02:40:46 +00001925 et->et_protlen = htons(prot);
wdenk145d2c12004-04-15 21:48:45 +00001926 return ETHER_HDR_SIZE;
1927 } else {
1928 VLAN_Ethernet_t *vet = (VLAN_Ethernet_t *)xet;
wdenk2d966952002-10-31 22:12:35 +00001929
wdenk145d2c12004-04-15 21:48:45 +00001930 vet->vet_vlan_type = htons(PROT_VLAN);
1931 vet->vet_tag = htons((0 << 5) | (myvlanid & VLAN_IDMASK));
1932 vet->vet_type = htons(prot);
1933 return VLAN_ETHER_HDR_SIZE;
1934 }
1935}
wdenk2d966952002-10-31 22:12:35 +00001936
1937void
Joe Hershberger4b7747e2012-05-15 08:59:04 +00001938NetSetIP(uchar *xip, IPaddr_t dest, int dport, int sport, int len)
wdenk2d966952002-10-31 22:12:35 +00001939{
Olav Morkene8a02772009-01-23 12:56:26 +01001940 IP_t *ip = (IP_t *)xip;
wdenk2d966952002-10-31 22:12:35 +00001941
1942 /*
1943 * If the data is an odd number of bytes, zero the
1944 * byte after the last byte so that the checksum
1945 * will work.
1946 */
1947 if (len & 1)
1948 xip[IP_HDR_SIZE + len] = 0;
1949
1950 /*
1951 * Construct an IP and UDP header.
wdenk05939202004-04-18 17:39:38 +00001952 * (need to set no fragment bit - XXX)
wdenk2d966952002-10-31 22:12:35 +00001953 */
Luca Ceresoli5c83a962011-05-11 03:59:54 +00001954 /* IP_HDR_SIZE / 4 (not including UDP) */
1955 ip->ip_hl_v = 0x45;
wdenk2d966952002-10-31 22:12:35 +00001956 ip->ip_tos = 0;
1957 ip->ip_len = htons(IP_HDR_SIZE + len);
1958 ip->ip_id = htons(NetIPID++);
Peter Tyser0885bf02008-12-01 16:26:20 -06001959 ip->ip_off = htons(IP_FLAGS_DFRAG); /* Don't fragment */
wdenk2d966952002-10-31 22:12:35 +00001960 ip->ip_ttl = 255;
1961 ip->ip_p = 17; /* UDP */
1962 ip->ip_sum = 0;
Luca Ceresoli5c83a962011-05-11 03:59:54 +00001963 /* already in network byte order */
Luca Ceresolif27d91f2011-05-04 02:40:44 +00001964 NetCopyIP((void *)&ip->ip_src, &NetOurIP);
Luca Ceresoli5c83a962011-05-11 03:59:54 +00001965 /* - "" - */
Luca Ceresolif27d91f2011-05-04 02:40:44 +00001966 NetCopyIP((void *)&ip->ip_dst, &dest);
wdenk2d966952002-10-31 22:12:35 +00001967 ip->udp_src = htons(sport);
1968 ip->udp_dst = htons(dport);
1969 ip->udp_len = htons(8 + len);
1970 ip->udp_xsum = 0;
1971 ip->ip_sum = ~NetCksum((uchar *)ip, IP_HDR_SIZE_NO_UDP / 2);
1972}
1973
Luca Ceresoli7cbd7482011-05-11 03:59:56 +00001974void copy_filename(char *dst, const char *src, int size)
wdenk2d966952002-10-31 22:12:35 +00001975{
1976 if (*src && (*src == '"')) {
1977 ++src;
1978 --size;
1979 }
1980
Luca Ceresolie8ccbb02011-05-04 02:40:43 +00001981 while ((--size > 0) && *src && (*src != '"'))
wdenk2d966952002-10-31 22:12:35 +00001982 *dst++ = *src++;
wdenk2d966952002-10-31 22:12:35 +00001983 *dst = '\0';
1984}
1985
Luca Ceresoli5c83a962011-05-11 03:59:54 +00001986#if defined(CONFIG_CMD_NFS) || \
1987 defined(CONFIG_CMD_SNTP) || \
1988 defined(CONFIG_CMD_DNS)
Robin Getz82f0d232009-07-20 14:53:54 -04001989/*
Robin Getz55b50e92010-03-08 14:07:00 -05001990 * make port a little random (1024-17407)
1991 * This keeps the math somewhat trivial to compute, and seems to work with
1992 * all supported protocols/clients/servers
Robin Getz82f0d232009-07-20 14:53:54 -04001993 */
1994unsigned int random_port(void)
1995{
Robin Getz55b50e92010-03-08 14:07:00 -05001996 return 1024 + (get_timer(0) % 0x4000);
Robin Getz82f0d232009-07-20 14:53:54 -04001997}
1998#endif
1999
Luca Ceresoli7cbd7482011-05-11 03:59:56 +00002000void ip_to_string(IPaddr_t x, char *s)
wdenk2d966952002-10-31 22:12:35 +00002001{
Luca Ceresoli7cbd7482011-05-11 03:59:56 +00002002 x = ntohl(x);
2003 sprintf(s, "%d.%d.%d.%d",
2004 (int) ((x >> 24) & 0xff),
2005 (int) ((x >> 16) & 0xff),
2006 (int) ((x >> 8) & 0xff), (int) ((x >> 0) & 0xff)
wdenk05939202004-04-18 17:39:38 +00002007 );
wdenk2d966952002-10-31 22:12:35 +00002008}
2009
wdenk145d2c12004-04-15 21:48:45 +00002010void VLAN_to_string(ushort x, char *s)
2011{
2012 x = ntohs(x);
2013
2014 if (x == (ushort)-1)
2015 x = VLAN_NONE;
2016
2017 if (x == VLAN_NONE)
2018 strcpy(s, "none");
2019 else
2020 sprintf(s, "%d", x & VLAN_IDMASK);
2021}
2022
Mike Frysinger89c73922010-10-20 07:16:48 -04002023ushort string_to_VLAN(const char *s)
wdenk145d2c12004-04-15 21:48:45 +00002024{
2025 ushort id;
2026
2027 if (s == NULL)
wdenk656140b2004-04-25 13:18:40 +00002028 return htons(VLAN_NONE);
wdenk145d2c12004-04-15 21:48:45 +00002029
2030 if (*s < '0' || *s > '9')
2031 id = VLAN_NONE;
2032 else
2033 id = (ushort)simple_strtoul(s, NULL, 10);
2034
wdenk656140b2004-04-25 13:18:40 +00002035 return htons(id);
wdenk145d2c12004-04-15 21:48:45 +00002036}
2037
wdenk145d2c12004-04-15 21:48:45 +00002038ushort getenv_VLAN(char *var)
2039{
Luca Ceresoli5fe6de22011-05-04 02:40:45 +00002040 return string_to_VLAN(getenv(var));
wdenk145d2c12004-04-15 21:48:45 +00002041}