blob: 33fcd90cd429ad066da1ca5ed50efef510b85663 [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>
80#include <net.h>
81#include "bootp.h"
82#include "tftp.h"
83#include "rarp.h"
wdenkbe9c1cb2004-02-24 02:00:03 +000084#include "nfs.h"
wdenk49c3f672003-10-08 22:33:00 +000085#ifdef CONFIG_STATUS_LED
86#include <status_led.h>
87#include <miiphy.h>
88#endif
Jon Loeliger54f35c22007-07-09 17:45:14 -050089#if defined(CONFIG_CMD_SNTP)
wdenkb4ad9622005-04-01 00:25:43 +000090#include "sntp.h"
91#endif
Peter Tyser62948502008-11-03 09:30:59 -060092#if defined(CONFIG_CDP_VERSION)
93#include <timestamp.h>
94#endif
Robin Getz82f0d232009-07-20 14:53:54 -040095#if defined(CONFIG_CMD_DNS)
96#include "dns.h"
97#endif
wdenk2d966952002-10-31 22:12:35 +000098
Wolfgang Denk6405a152006-03-31 18:32:53 +020099DECLARE_GLOBAL_DATA_PTR;
100
Guennadi Liakhovetskib38c2b32008-04-03 17:04:19 +0200101#ifndef CONFIG_ARP_TIMEOUT
Bartlomiej Sieka56668462008-10-01 15:26:28 +0200102# define ARP_TIMEOUT 5000UL /* Milliseconds before trying ARP again */
Guennadi Liakhovetskib38c2b32008-04-03 17:04:19 +0200103#else
Bartlomiej Sieka56668462008-10-01 15:26:28 +0200104# define ARP_TIMEOUT CONFIG_ARP_TIMEOUT
Guennadi Liakhovetskib38c2b32008-04-03 17:04:19 +0200105#endif
106
107
wdenke6466f62003-06-05 19:27:42 +0000108#ifndef CONFIG_NET_RETRY_COUNT
Guennadi Liakhovetskib38c2b32008-04-03 17:04:19 +0200109# define ARP_TIMEOUT_COUNT 5 /* # of timeouts before giving up */
wdenke6466f62003-06-05 19:27:42 +0000110#else
Guennadi Liakhovetskib38c2b32008-04-03 17:04:19 +0200111# define ARP_TIMEOUT_COUNT CONFIG_NET_RETRY_COUNT
wdenke6466f62003-06-05 19:27:42 +0000112#endif
113
wdenk2d966952002-10-31 22:12:35 +0000114/** BOOTP EXTENTIONS **/
115
116IPaddr_t NetOurSubnetMask=0; /* Our subnet mask (0=unknown) */
117IPaddr_t NetOurGatewayIP=0; /* Our gateways IP address */
118IPaddr_t NetOurDNSIP=0; /* Our DNS IP address */
Jon Loeliger5336a762007-07-09 22:08:34 -0500119#if defined(CONFIG_BOOTP_DNS2)
stroesee0aadfb2003-08-28 14:17:32 +0000120IPaddr_t NetOurDNS2IP=0; /* Our 2nd DNS IP address */
121#endif
wdenk2d966952002-10-31 22:12:35 +0000122char NetOurNISDomain[32]={0,}; /* Our NIS domain */
123char NetOurHostName[32]={0,}; /* Our hostname */
124char NetOurRootPath[64]={0,}; /* Our bootpath */
125ushort NetBootFileSize=0; /* Our bootfile size in blocks */
126
David Updegraff7280da72007-06-11 10:41:07 -0500127#ifdef CONFIG_MCAST_TFTP /* Multicast TFTP */
128IPaddr_t Mcast_addr;
129#endif
130
wdenk2d966952002-10-31 22:12:35 +0000131/** END OF BOOTP EXTENTIONS **/
132
133ulong NetBootFileXferSize; /* The actual transferred size of the bootfile (in bytes) */
134uchar NetOurEther[6]; /* Our ethernet address */
135uchar NetServerEther[6] = /* Boot server enet address */
wdenke6466f62003-06-05 19:27:42 +0000136 { 0, 0, 0, 0, 0, 0 };
wdenk2d966952002-10-31 22:12:35 +0000137IPaddr_t NetOurIP; /* Our IP addr (0 = unknown) */
Guennadi Liakhovetskib38c2b32008-04-03 17:04:19 +0200138IPaddr_t NetServerIP; /* Server IP addr (0 = unknown) */
Mike Frysingerdc166032009-07-18 21:04:08 -0400139volatile uchar *NetRxPacket; /* Current receive packet */
140int NetRxPacketLen; /* Current rx packet length */
wdenk2d966952002-10-31 22:12:35 +0000141unsigned NetIPID; /* IP packet ID */
142uchar NetBcastAddr[6] = /* Ethernet bcast address */
143 { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
wdenke6466f62003-06-05 19:27:42 +0000144uchar NetEtherNullAddr[6] =
145 { 0, 0, 0, 0, 0, 0 };
Rafal Jaworowski1f2c9a42007-12-27 18:19:02 +0100146#ifdef CONFIG_API
147void (*push_packet)(volatile void *, int len) = 0;
148#endif
Jon Loeliger54f35c22007-07-09 17:45:14 -0500149#if defined(CONFIG_CMD_CDP)
wdenk05939202004-04-18 17:39:38 +0000150uchar NetCDPAddr[6] = /* Ethernet bcast address */
wdenk145d2c12004-04-15 21:48:45 +0000151 { 0x01, 0x00, 0x0c, 0xcc, 0xcc, 0xcc };
152#endif
wdenk2d966952002-10-31 22:12:35 +0000153int NetState; /* Network loop state */
154#ifdef CONFIG_NET_MULTI
155int NetRestartWrap = 0; /* Tried all network devices */
156static int NetRestarted = 0; /* Network loop restarted */
157static int NetDevExists = 0; /* At least one device configured */
158#endif
159
wdenk05939202004-04-18 17:39:38 +0000160/* XXX in both little & big endian machines 0xFFFF == ntohs(-1) */
161ushort NetOurVLAN = 0xFFFF; /* default is without VLAN */
162ushort NetOurNativeVLAN = 0xFFFF; /* ditto */
wdenk145d2c12004-04-15 21:48:45 +0000163
wdenk2d966952002-10-31 22:12:35 +0000164char BootFile[128]; /* Boot File name */
165
Jon Loeliger54f35c22007-07-09 17:45:14 -0500166#if defined(CONFIG_CMD_PING)
Wolfgang Denka1be4762008-05-20 16:00:29 +0200167IPaddr_t NetPingIP; /* the ip address to ping */
wdenke6466f62003-06-05 19:27:42 +0000168
169static void PingStart(void);
170#endif
171
Jon Loeliger54f35c22007-07-09 17:45:14 -0500172#if defined(CONFIG_CMD_CDP)
wdenk145d2c12004-04-15 21:48:45 +0000173static void CDPStart(void);
174#endif
175
Jon Loeliger54f35c22007-07-09 17:45:14 -0500176#if defined(CONFIG_CMD_SNTP)
wdenkb4ad9622005-04-01 00:25:43 +0000177IPaddr_t NetNtpServerIP; /* NTP server IP address */
178int NetTimeOffset=0; /* offset time from UTC */
179#endif
180
wdenkb8fb6192004-08-02 21:11:11 +0000181#ifdef CONFIG_NETCONSOLE
182void NcStart(void);
183int nc_input_packet(uchar *pkt, unsigned dest, unsigned src, unsigned len);
184#endif
185
wdenk2d966952002-10-31 22:12:35 +0000186volatile uchar PktBuf[(PKTBUFSRX+1) * PKTSIZE_ALIGN + PKTALIGN];
187
188volatile uchar *NetRxPackets[PKTBUFSRX]; /* Receive packets */
189
190static rxhand_f *packetHandler; /* Current RX packet handler */
191static thand_f *timeHandler; /* Current timeout handler */
wdenk5d841732003-08-17 18:55:18 +0000192static ulong timeStart; /* Time base value */
193static ulong timeDelta; /* Current timeout value */
wdenk2d966952002-10-31 22:12:35 +0000194volatile uchar *NetTxPacket = 0; /* THE transmit packet */
195
196static int net_check_prereq (proto_t protocol);
197
Remy Bohmer0ddb8e82009-10-28 22:13:39 +0100198static int NetTryCount;
199
wdenk2d966952002-10-31 22:12:35 +0000200/**********************************************************************/
wdenke6466f62003-06-05 19:27:42 +0000201
202IPaddr_t NetArpWaitPacketIP;
203IPaddr_t NetArpWaitReplyIP;
204uchar *NetArpWaitPacketMAC; /* MAC address of waiting packet's destination */
Wolfgang Denk30b87322005-08-12 23:43:12 +0200205uchar *NetArpWaitTxPacket; /* THE transmit packet */
wdenke6466f62003-06-05 19:27:42 +0000206int NetArpWaitTxPacketSize;
Wolfgang Denka1be4762008-05-20 16:00:29 +0200207uchar NetArpWaitPacketBuf[PKTSIZE_ALIGN + PKTALIGN];
wdenke6466f62003-06-05 19:27:42 +0000208ulong NetArpWaitTimerStart;
209int NetArpWaitTry;
210
wdenk05939202004-04-18 17:39:38 +0000211void ArpRequest (void)
wdenke6466f62003-06-05 19:27:42 +0000212{
213 int i;
214 volatile uchar *pkt;
wdenk05939202004-04-18 17:39:38 +0000215 ARP_t *arp;
wdenke6466f62003-06-05 19:27:42 +0000216
Robin Getz9e0a4d62009-07-23 03:01:03 -0400217 debug("ARP broadcast %d\n", NetArpWaitTry);
218
wdenke6466f62003-06-05 19:27:42 +0000219 pkt = NetTxPacket;
220
wdenk05939202004-04-18 17:39:38 +0000221 pkt += NetSetEther (pkt, NetBcastAddr, PROT_ARP);
wdenke6466f62003-06-05 19:27:42 +0000222
wdenk05939202004-04-18 17:39:38 +0000223 arp = (ARP_t *) pkt;
wdenke6466f62003-06-05 19:27:42 +0000224
wdenk05939202004-04-18 17:39:38 +0000225 arp->ar_hrd = htons (ARP_ETHER);
226 arp->ar_pro = htons (PROT_IP);
wdenke6466f62003-06-05 19:27:42 +0000227 arp->ar_hln = 6;
228 arp->ar_pln = 4;
wdenk05939202004-04-18 17:39:38 +0000229 arp->ar_op = htons (ARPOP_REQUEST);
wdenke6466f62003-06-05 19:27:42 +0000230
Wolfgang Denk30b87322005-08-12 23:43:12 +0200231 memcpy (&arp->ar_data[0], NetOurEther, 6); /* source ET addr */
232 NetWriteIP ((uchar *) & arp->ar_data[6], NetOurIP); /* source IP addr */
wdenk05939202004-04-18 17:39:38 +0000233 for (i = 10; i < 16; ++i) {
234 arp->ar_data[i] = 0; /* dest ET addr = 0 */
wdenke6466f62003-06-05 19:27:42 +0000235 }
236
wdenk05939202004-04-18 17:39:38 +0000237 if ((NetArpWaitPacketIP & NetOurSubnetMask) !=
238 (NetOurIP & NetOurSubnetMask)) {
239 if (NetOurGatewayIP == 0) {
240 puts ("## Warning: gatewayip needed but not set\n");
Wolfgang Denk98ceffb2006-03-12 01:13:30 +0100241 NetArpWaitReplyIP = NetArpWaitPacketIP;
242 } else {
243 NetArpWaitReplyIP = NetOurGatewayIP;
wdenk05939202004-04-18 17:39:38 +0000244 }
wdenk05939202004-04-18 17:39:38 +0000245 } else {
246 NetArpWaitReplyIP = NetArpWaitPacketIP;
247 }
wdenke6466f62003-06-05 19:27:42 +0000248
wdenk05939202004-04-18 17:39:38 +0000249 NetWriteIP ((uchar *) & arp->ar_data[16], NetArpWaitReplyIP);
250 (void) eth_send (NetTxPacket, (pkt - NetTxPacket) + ARP_HDR_SIZE);
wdenke6466f62003-06-05 19:27:42 +0000251}
252
253void ArpTimeoutCheck(void)
254{
255 ulong t;
256
257 if (!NetArpWaitPacketIP)
258 return;
259
260 t = get_timer(0);
261
262 /* check for arp timeout */
Bartlomiej Sieka56668462008-10-01 15:26:28 +0200263 if ((t - NetArpWaitTimerStart) > ARP_TIMEOUT) {
wdenke6466f62003-06-05 19:27:42 +0000264 NetArpWaitTry++;
265
266 if (NetArpWaitTry >= ARP_TIMEOUT_COUNT) {
267 puts ("\nARP Retry count exceeded; starting again\n");
268 NetArpWaitTry = 0;
269 NetStartAgain();
270 } else {
271 NetArpWaitTimerStart = t;
272 ArpRequest();
273 }
274 }
275}
276
Heiko Schocher3b195ff2009-04-28 08:36:11 +0200277static void
Heiko Schocher0c303fa2009-02-10 09:38:52 +0100278NetInitLoop(proto_t protocol)
279{
Heiko Schocher3b195ff2009-04-28 08:36:11 +0200280 static int env_changed_id = 0;
Heiko Schocher0c303fa2009-02-10 09:38:52 +0100281 bd_t *bd = gd->bd;
282 int env_id = get_env_id ();
283
284 /* update only when the environment has changed */
Michael Zaidmanb97bfe42009-04-04 01:43:00 +0300285 if (env_changed_id != env_id) {
Heiko Schocher0c303fa2009-02-10 09:38:52 +0100286 NetCopyIP(&NetOurIP, &bd->bi_ip_addr);
287 NetOurGatewayIP = getenv_IPaddr ("gatewayip");
288 NetOurSubnetMask= getenv_IPaddr ("netmask");
Heiko Schocher0c303fa2009-02-10 09:38:52 +0100289 NetServerIP = getenv_IPaddr ("serverip");
Heiko Schocher0c303fa2009-02-10 09:38:52 +0100290 NetOurNativeVLAN = getenv_VLAN("nvlan");
Michael Zaidmanb97bfe42009-04-04 01:43:00 +0300291 NetOurVLAN = getenv_VLAN("vlan");
Robin Getz82f0d232009-07-20 14:53:54 -0400292#if defined(CONFIG_CMD_DNS)
293 NetOurDNSIP = getenv_IPaddr("dnsip");
294#endif
Michael Zaidmanb97bfe42009-04-04 01:43:00 +0300295 env_changed_id = env_id;
Heiko Schocher0c303fa2009-02-10 09:38:52 +0100296 }
Michael Zaidmanb97bfe42009-04-04 01:43:00 +0300297
Heiko Schocher3b195ff2009-04-28 08:36:11 +0200298 return;
Heiko Schocher0c303fa2009-02-10 09:38:52 +0100299}
300
wdenke6466f62003-06-05 19:27:42 +0000301/**********************************************************************/
wdenk2d966952002-10-31 22:12:35 +0000302/*
303 * Main network processing loop.
304 */
305
306int
307NetLoop(proto_t protocol)
308{
wdenk2d966952002-10-31 22:12:35 +0000309 bd_t *bd = gd->bd;
310
311#ifdef CONFIG_NET_MULTI
312 NetRestarted = 0;
313 NetDevExists = 0;
314#endif
315
wdenke6466f62003-06-05 19:27:42 +0000316 /* XXX problem with bss workaround */
317 NetArpWaitPacketMAC = NULL;
318 NetArpWaitTxPacket = NULL;
319 NetArpWaitPacketIP = 0;
320 NetArpWaitReplyIP = 0;
321 NetArpWaitTxPacket = NULL;
322 NetTxPacket = NULL;
Remy Bohmer0ddb8e82009-10-28 22:13:39 +0100323 NetTryCount = 1;
wdenke6466f62003-06-05 19:27:42 +0000324
wdenk2d966952002-10-31 22:12:35 +0000325 if (!NetTxPacket) {
326 int i;
wdenk2d966952002-10-31 22:12:35 +0000327 /*
328 * Setup packet buffers, aligned correctly.
329 */
330 NetTxPacket = &PktBuf[0] + (PKTALIGN - 1);
331 NetTxPacket -= (ulong)NetTxPacket % PKTALIGN;
332 for (i = 0; i < PKTBUFSRX; i++) {
333 NetRxPackets[i] = NetTxPacket + (i+1)*PKTSIZE_ALIGN;
334 }
wdenke6466f62003-06-05 19:27:42 +0000335 }
336
337 if (!NetArpWaitTxPacket) {
338 NetArpWaitTxPacket = &NetArpWaitPacketBuf[0] + (PKTALIGN - 1);
339 NetArpWaitTxPacket -= (ulong)NetArpWaitTxPacket % PKTALIGN;
340 NetArpWaitTxPacketSize = 0;
wdenk2d966952002-10-31 22:12:35 +0000341 }
342
343 eth_halt();
wdenk05939202004-04-18 17:39:38 +0000344#ifdef CONFIG_NET_MULTI
wdenk145d2c12004-04-15 21:48:45 +0000345 eth_set_current();
wdenk05939202004-04-18 17:39:38 +0000346#endif
wdenkfa66e932005-04-03 14:52:59 +0000347 if (eth_init(bd) < 0) {
348 eth_halt();
wdenk05939202004-04-18 17:39:38 +0000349 return(-1);
wdenkfa66e932005-04-03 14:52:59 +0000350 }
wdenk2d966952002-10-31 22:12:35 +0000351
352restart:
353#ifdef CONFIG_NET_MULTI
354 memcpy (NetOurEther, eth_get_dev()->enetaddr, 6);
355#else
Mike Frysinger79ca1b92009-02-11 18:23:48 -0500356 eth_getenv_enetaddr("ethaddr", NetOurEther);
wdenk2d966952002-10-31 22:12:35 +0000357#endif
358
359 NetState = NETLOOP_CONTINUE;
360
361 /*
362 * Start the ball rolling with the given start function. From
363 * here on, this code is a state machine driven by received
364 * packets and timer events.
365 */
Heiko Schocher0c303fa2009-02-10 09:38:52 +0100366 NetInitLoop(protocol);
wdenk2d966952002-10-31 22:12:35 +0000367
368 switch (net_check_prereq (protocol)) {
369 case 1:
370 /* network not configured */
wdenkfa66e932005-04-03 14:52:59 +0000371 eth_halt();
wdenk2582f6b2002-11-11 21:14:20 +0000372 return (-1);
wdenk2d966952002-10-31 22:12:35 +0000373
374#ifdef CONFIG_NET_MULTI
375 case 2:
376 /* network device not configured */
377 break;
378#endif /* CONFIG_NET_MULTI */
379
380 case 0:
381#ifdef CONFIG_NET_MULTI
382 NetDevExists = 1;
383#endif
384 switch (protocol) {
385 case TFTP:
386 /* always use ARP to get server ethernet address */
wdenke6466f62003-06-05 19:27:42 +0000387 TftpStart();
wdenk2d966952002-10-31 22:12:35 +0000388 break;
389
Jon Loeliger54f35c22007-07-09 17:45:14 -0500390#if defined(CONFIG_CMD_DHCP)
wdenk2d966952002-10-31 22:12:35 +0000391 case DHCP:
wdenkc3919532004-10-11 22:51:13 +0000392 BootpTry = 0;
Michael Zaidman16bb21d2009-07-14 23:37:12 +0300393 NetOurIP = 0;
wdenk2d966952002-10-31 22:12:35 +0000394 DhcpRequest(); /* Basically same as BOOTP */
395 break;
Jon Loeligera9807e52007-07-10 11:05:02 -0500396#endif
wdenk2d966952002-10-31 22:12:35 +0000397
398 case BOOTP:
399 BootpTry = 0;
Michael Zaidman16bb21d2009-07-14 23:37:12 +0300400 NetOurIP = 0;
wdenk2d966952002-10-31 22:12:35 +0000401 BootpRequest ();
402 break;
403
404 case RARP:
405 RarpTry = 0;
Michael Zaidman16bb21d2009-07-14 23:37:12 +0300406 NetOurIP = 0;
wdenk2d966952002-10-31 22:12:35 +0000407 RarpRequest ();
408 break;
Jon Loeliger54f35c22007-07-09 17:45:14 -0500409#if defined(CONFIG_CMD_PING)
wdenke6466f62003-06-05 19:27:42 +0000410 case PING:
411 PingStart();
412 break;
413#endif
Jon Loeliger54f35c22007-07-09 17:45:14 -0500414#if defined(CONFIG_CMD_NFS)
wdenkbe9c1cb2004-02-24 02:00:03 +0000415 case NFS:
416 NfsStart();
417 break;
418#endif
Jon Loeliger54f35c22007-07-09 17:45:14 -0500419#if defined(CONFIG_CMD_CDP)
wdenk145d2c12004-04-15 21:48:45 +0000420 case CDP:
421 CDPStart();
422 break;
423#endif
wdenkb8fb6192004-08-02 21:11:11 +0000424#ifdef CONFIG_NETCONSOLE
425 case NETCONS:
426 NcStart();
427 break;
428#endif
Jon Loeliger54f35c22007-07-09 17:45:14 -0500429#if defined(CONFIG_CMD_SNTP)
wdenkb4ad9622005-04-01 00:25:43 +0000430 case SNTP:
431 SntpStart();
432 break;
433#endif
Robin Getz82f0d232009-07-20 14:53:54 -0400434#if defined(CONFIG_CMD_DNS)
435 case DNS:
436 DnsStart();
437 break;
438#endif
wdenk2d966952002-10-31 22:12:35 +0000439 default:
440 break;
441 }
442
443 NetBootFileXferSize = 0;
444 break;
445 }
446
Jon Loeliger54f35c22007-07-09 17:45:14 -0500447#if defined(CONFIG_MII) || defined(CONFIG_CMD_MII)
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200448#if defined(CONFIG_SYS_FAULT_ECHO_LINK_DOWN) && defined(CONFIG_STATUS_LED) && defined(STATUS_LED_RED)
wdenk49c3f672003-10-08 22:33:00 +0000449 /*
wdenk9c53f402003-10-15 23:53:47 +0000450 * Echo the inverted link state to the fault LED.
wdenk49c3f672003-10-08 22:33:00 +0000451 */
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200452 if(miiphy_link(eth_get_dev()->name, CONFIG_SYS_FAULT_MII_ADDR)) {
wdenk49c3f672003-10-08 22:33:00 +0000453 status_led_set (STATUS_LED_RED, STATUS_LED_OFF);
454 } else {
455 status_led_set (STATUS_LED_RED, STATUS_LED_ON);
456 }
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200457#endif /* CONFIG_SYS_FAULT_ECHO_LINK_DOWN, ... */
wdenk49c3f672003-10-08 22:33:00 +0000458#endif /* CONFIG_MII, ... */
wdenk2d966952002-10-31 22:12:35 +0000459
460 /*
461 * Main packet reception loop. Loop receiving packets until
wdenk27fa5852005-04-03 14:18:51 +0000462 * someone sets `NetState' to a state that terminates.
wdenk2d966952002-10-31 22:12:35 +0000463 */
464 for (;;) {
465 WATCHDOG_RESET();
466#ifdef CONFIG_SHOW_ACTIVITY
467 {
468 extern void show_activity(int arg);
469 show_activity(1);
470 }
471#endif
472 /*
473 * Check the ethernet for a new packet. The ethernet
474 * receive routine will process it.
475 */
Guennadi Liakhovetskib38c2b32008-04-03 17:04:19 +0200476 eth_rx();
wdenk2d966952002-10-31 22:12:35 +0000477
478 /*
479 * Abort if ctrl-c was pressed.
480 */
481 if (ctrlc()) {
wdenk57b2d802003-06-27 21:31:46 +0000482 eth_halt();
wdenk42c05472004-03-23 22:14:11 +0000483 puts ("\nAbort\n");
wdenk2582f6b2002-11-11 21:14:20 +0000484 return (-1);
wdenk2d966952002-10-31 22:12:35 +0000485 }
486
wdenke6466f62003-06-05 19:27:42 +0000487 ArpTimeoutCheck();
wdenk2d966952002-10-31 22:12:35 +0000488
489 /*
490 * Check for a timeout, and run the timeout handler
491 * if we have one.
492 */
wdenk5d841732003-08-17 18:55:18 +0000493 if (timeHandler && ((get_timer(0) - timeStart) > timeDelta)) {
wdenk2d966952002-10-31 22:12:35 +0000494 thand_f *x;
495
Jon Loeliger54f35c22007-07-09 17:45:14 -0500496#if defined(CONFIG_MII) || defined(CONFIG_CMD_MII)
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200497# if defined(CONFIG_SYS_FAULT_ECHO_LINK_DOWN) && \
wdenk6148e742005-04-03 20:55:38 +0000498 defined(CONFIG_STATUS_LED) && \
wdenk27fa5852005-04-03 14:18:51 +0000499 defined(STATUS_LED_RED)
wdenk49c3f672003-10-08 22:33:00 +0000500 /*
wdenk9c53f402003-10-15 23:53:47 +0000501 * Echo the inverted link state to the fault LED.
wdenk49c3f672003-10-08 22:33:00 +0000502 */
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200503 if(miiphy_link(eth_get_dev()->name, CONFIG_SYS_FAULT_MII_ADDR)) {
wdenk49c3f672003-10-08 22:33:00 +0000504 status_led_set (STATUS_LED_RED, STATUS_LED_OFF);
505 } else {
506 status_led_set (STATUS_LED_RED, STATUS_LED_ON);
507 }
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200508# endif /* CONFIG_SYS_FAULT_ECHO_LINK_DOWN, ... */
wdenk49c3f672003-10-08 22:33:00 +0000509#endif /* CONFIG_MII, ... */
wdenk2d966952002-10-31 22:12:35 +0000510 x = timeHandler;
511 timeHandler = (thand_f *)0;
512 (*x)();
513 }
514
515
516 switch (NetState) {
517
518 case NETLOOP_RESTART:
519#ifdef CONFIG_NET_MULTI
520 NetRestarted = 1;
521#endif
522 goto restart;
523
524 case NETLOOP_SUCCESS:
525 if (NetBootFileXferSize > 0) {
Wolfgang Denk084d0ce2007-09-12 00:48:57 +0200526 char buf[20];
wdenk2d966952002-10-31 22:12:35 +0000527 printf("Bytes transferred = %ld (%lx hex)\n",
528 NetBootFileXferSize,
529 NetBootFileXferSize);
Wolfgang Denk084d0ce2007-09-12 00:48:57 +0200530 sprintf(buf, "%lX", NetBootFileXferSize);
wdenk2d966952002-10-31 22:12:35 +0000531 setenv("filesize", buf);
wdenk145d2c12004-04-15 21:48:45 +0000532
533 sprintf(buf, "%lX", (unsigned long)load_addr);
534 setenv("fileaddr", buf);
wdenk2d966952002-10-31 22:12:35 +0000535 }
536 eth_halt();
537 return NetBootFileXferSize;
538
539 case NETLOOP_FAIL:
wdenk2582f6b2002-11-11 21:14:20 +0000540 return (-1);
wdenk2d966952002-10-31 22:12:35 +0000541 }
542 }
543}
544
545/**********************************************************************/
546
547static void
548startAgainTimeout(void)
549{
550 NetState = NETLOOP_RESTART;
551}
552
553static void
554startAgainHandler(uchar * pkt, unsigned dest, unsigned src, unsigned len)
555{
556 /* Totally ignore the packet */
557}
558
wdenk05939202004-04-18 17:39:38 +0000559void NetStartAgain (void)
wdenk2d966952002-10-31 22:12:35 +0000560{
wdenk05939202004-04-18 17:39:38 +0000561 char *nretry;
Remy Bohmer0ddb8e82009-10-28 22:13:39 +0100562 int retry_forever = 0;
563 unsigned long retrycnt = 0;
wdenk145d2c12004-04-15 21:48:45 +0000564
Remy Bohmer0ddb8e82009-10-28 22:13:39 +0100565 nretry = getenv("netretry");
566 if (nretry) {
567 if (!strcmp(nretry, "yes"))
568 retry_forever = 1;
569 else if (!strcmp(nretry, "no"))
570 retrycnt = 0;
571 else if (!strcmp(nretry, "once"))
572 retrycnt = 1;
573 else
574 retrycnt = simple_strtoul(nretry, NULL, 0);
575 } else
576 retry_forever = 1;
577
578 if ((!retry_forever) && (NetTryCount >= retrycnt)) {
579 eth_halt();
wdenk145d2c12004-04-15 21:48:45 +0000580 NetState = NETLOOP_FAIL;
581 return;
582 }
Remy Bohmer0ddb8e82009-10-28 22:13:39 +0100583
584 NetTryCount++;
585
wdenk2d966952002-10-31 22:12:35 +0000586#ifndef CONFIG_NET_MULTI
Bartlomiej Sieka56668462008-10-01 15:26:28 +0200587 NetSetTimeout (10000UL, startAgainTimeout);
wdenk05939202004-04-18 17:39:38 +0000588 NetSetHandler (startAgainHandler);
589#else /* !CONFIG_NET_MULTI*/
590 eth_halt ();
Matthias Fuchsa02d62b2007-12-27 16:58:41 +0100591#if !defined(CONFIG_NET_DO_NOT_TRY_ANOTHER)
wdenk05939202004-04-18 17:39:38 +0000592 eth_try_another (!NetRestarted);
Matthias Fuchsa02d62b2007-12-27 16:58:41 +0100593#endif
wdenk05939202004-04-18 17:39:38 +0000594 eth_init (gd->bd);
595 if (NetRestartWrap) {
wdenk2d966952002-10-31 22:12:35 +0000596 NetRestartWrap = 0;
Remy Bohmer0ddb8e82009-10-28 22:13:39 +0100597 if (NetDevExists) {
Bartlomiej Sieka56668462008-10-01 15:26:28 +0200598 NetSetTimeout (10000UL, startAgainTimeout);
wdenk05939202004-04-18 17:39:38 +0000599 NetSetHandler (startAgainHandler);
600 } else {
wdenk2d966952002-10-31 22:12:35 +0000601 NetState = NETLOOP_FAIL;
602 }
wdenk05939202004-04-18 17:39:38 +0000603 } else {
wdenk2d966952002-10-31 22:12:35 +0000604 NetState = NETLOOP_RESTART;
605 }
wdenk05939202004-04-18 17:39:38 +0000606#endif /* CONFIG_NET_MULTI */
wdenk2d966952002-10-31 22:12:35 +0000607}
608
609/**********************************************************************/
610/*
611 * Miscelaneous bits.
612 */
613
614void
615NetSetHandler(rxhand_f * f)
616{
617 packetHandler = f;
618}
619
620
621void
wdenkd6a3dd42004-10-09 21:56:21 +0000622NetSetTimeout(ulong iv, thand_f * f)
wdenk2d966952002-10-31 22:12:35 +0000623{
624 if (iv == 0) {
625 timeHandler = (thand_f *)0;
626 } else {
627 timeHandler = f;
wdenk5d841732003-08-17 18:55:18 +0000628 timeStart = get_timer(0);
629 timeDelta = iv;
wdenk2d966952002-10-31 22:12:35 +0000630 }
631}
632
633
634void
635NetSendPacket(volatile uchar * pkt, int len)
636{
637 (void) eth_send(pkt, len);
638}
639
wdenke6466f62003-06-05 19:27:42 +0000640int
641NetSendUDPPacket(uchar *ether, IPaddr_t dest, int dport, int sport, int len)
642{
wdenk145d2c12004-04-15 21:48:45 +0000643 uchar *pkt;
644
wdenke6466f62003-06-05 19:27:42 +0000645 /* convert to new style broadcast */
646 if (dest == 0)
647 dest = 0xFFFFFFFF;
648
649 /* if broadcast, make the ether address a broadcast and don't do ARP */
650 if (dest == 0xFFFFFFFF)
651 ether = NetBcastAddr;
652
653 /* if MAC address was not discovered yet, save the packet and do an ARP request */
654 if (memcmp(ether, NetEtherNullAddr, 6) == 0) {
655
Robin Getz9e0a4d62009-07-23 03:01:03 -0400656 debug("sending ARP for %08lx\n", dest);
657
wdenke6466f62003-06-05 19:27:42 +0000658 NetArpWaitPacketIP = dest;
659 NetArpWaitPacketMAC = ether;
wdenk145d2c12004-04-15 21:48:45 +0000660
661 pkt = NetArpWaitTxPacket;
662 pkt += NetSetEther (pkt, NetArpWaitPacketMAC, PROT_IP);
663
664 NetSetIP (pkt, dest, dport, sport, len);
665 memcpy(pkt + IP_HDR_SIZE, (uchar *)NetTxPacket + (pkt - (uchar *)NetArpWaitTxPacket) + IP_HDR_SIZE, len);
wdenke6466f62003-06-05 19:27:42 +0000666
667 /* size of the waiting packet */
wdenk145d2c12004-04-15 21:48:45 +0000668 NetArpWaitTxPacketSize = (pkt - NetArpWaitTxPacket) + IP_HDR_SIZE + len;
wdenke6466f62003-06-05 19:27:42 +0000669
670 /* and do the ARP request */
671 NetArpWaitTry = 1;
672 NetArpWaitTimerStart = get_timer(0);
673 ArpRequest();
674 return 1; /* waiting */
675 }
676
Robin Getz9e0a4d62009-07-23 03:01:03 -0400677 debug("sending UDP to %08lx/%pM\n", dest, ether);
wdenke6466f62003-06-05 19:27:42 +0000678
wdenk145d2c12004-04-15 21:48:45 +0000679 pkt = (uchar *)NetTxPacket;
680 pkt += NetSetEther (pkt, ether, PROT_IP);
681 NetSetIP (pkt, dest, dport, sport, len);
682 (void) eth_send(NetTxPacket, (pkt - NetTxPacket) + IP_HDR_SIZE + len);
wdenke6466f62003-06-05 19:27:42 +0000683
wdenk05939202004-04-18 17:39:38 +0000684 return 0; /* transmitted */
wdenke6466f62003-06-05 19:27:42 +0000685}
686
Jon Loeliger54f35c22007-07-09 17:45:14 -0500687#if defined(CONFIG_CMD_PING)
wdenke6466f62003-06-05 19:27:42 +0000688static ushort PingSeqNo;
689
690int PingSend(void)
691{
692 static uchar mac[6];
693 volatile IP_t *ip;
694 volatile ushort *s;
wdenk145d2c12004-04-15 21:48:45 +0000695 uchar *pkt;
wdenke6466f62003-06-05 19:27:42 +0000696
697 /* XXX always send arp request */
698
699 memcpy(mac, NetEtherNullAddr, 6);
700
Robin Getz9e0a4d62009-07-23 03:01:03 -0400701 debug("sending ARP for %08lx\n", NetPingIP);
wdenke6466f62003-06-05 19:27:42 +0000702
703 NetArpWaitPacketIP = NetPingIP;
704 NetArpWaitPacketMAC = mac;
705
wdenk145d2c12004-04-15 21:48:45 +0000706 pkt = NetArpWaitTxPacket;
707 pkt += NetSetEther(pkt, mac, PROT_IP);
wdenke6466f62003-06-05 19:27:42 +0000708
wdenk145d2c12004-04-15 21:48:45 +0000709 ip = (volatile IP_t *)pkt;
wdenke6466f62003-06-05 19:27:42 +0000710
711 /*
712 * Construct an IP and ICMP header. (need to set no fragment bit - XXX)
713 */
714 ip->ip_hl_v = 0x45; /* IP_HDR_SIZE / 4 (not including UDP) */
715 ip->ip_tos = 0;
716 ip->ip_len = htons(IP_HDR_SIZE_NO_UDP + 8);
717 ip->ip_id = htons(NetIPID++);
Peter Tyser0885bf02008-12-01 16:26:20 -0600718 ip->ip_off = htons(IP_FLAGS_DFRAG); /* Don't fragment */
wdenke6466f62003-06-05 19:27:42 +0000719 ip->ip_ttl = 255;
720 ip->ip_p = 0x01; /* ICMP */
721 ip->ip_sum = 0;
722 NetCopyIP((void*)&ip->ip_src, &NetOurIP); /* already in network byte order */
723 NetCopyIP((void*)&ip->ip_dst, &NetPingIP); /* - "" - */
724 ip->ip_sum = ~NetCksum((uchar *)ip, IP_HDR_SIZE_NO_UDP / 2);
725
726 s = &ip->udp_src; /* XXX ICMP starts here */
727 s[0] = htons(0x0800); /* echo-request, code */
728 s[1] = 0; /* checksum */
Wolfgang Denka1be4762008-05-20 16:00:29 +0200729 s[2] = 0; /* identifier */
wdenke6466f62003-06-05 19:27:42 +0000730 s[3] = htons(PingSeqNo++); /* sequence number */
731 s[1] = ~NetCksum((uchar *)s, 8/2);
732
733 /* size of the waiting packet */
wdenk145d2c12004-04-15 21:48:45 +0000734 NetArpWaitTxPacketSize = (pkt - NetArpWaitTxPacket) + IP_HDR_SIZE_NO_UDP + 8;
wdenke6466f62003-06-05 19:27:42 +0000735
736 /* and do the ARP request */
737 NetArpWaitTry = 1;
738 NetArpWaitTimerStart = get_timer(0);
739 ArpRequest();
740 return 1; /* waiting */
741}
742
743static void
744PingTimeout (void)
745{
746 eth_halt();
747 NetState = NETLOOP_FAIL; /* we did not get the reply */
748}
749
750static void
751PingHandler (uchar * pkt, unsigned dest, unsigned src, unsigned len)
752{
753 IPaddr_t tmp;
754 volatile IP_t *ip = (volatile IP_t *)pkt;
755
756 tmp = NetReadIP((void *)&ip->ip_src);
757 if (tmp != NetPingIP)
758 return;
759
760 NetState = NETLOOP_SUCCESS;
761}
762
763static void PingStart(void)
764{
wdenk145d2c12004-04-15 21:48:45 +0000765#if defined(CONFIG_NET_MULTI)
766 printf ("Using %s device\n", eth_get_name());
wdenk05939202004-04-18 17:39:38 +0000767#endif /* CONFIG_NET_MULTI */
Bartlomiej Sieka56668462008-10-01 15:26:28 +0200768 NetSetTimeout (10000UL, PingTimeout);
wdenke6466f62003-06-05 19:27:42 +0000769 NetSetHandler (PingHandler);
770
771 PingSend();
772}
Jon Loeligera9807e52007-07-10 11:05:02 -0500773#endif
wdenk145d2c12004-04-15 21:48:45 +0000774
Jon Loeliger54f35c22007-07-09 17:45:14 -0500775#if defined(CONFIG_CMD_CDP)
wdenk145d2c12004-04-15 21:48:45 +0000776
777#define CDP_DEVICE_ID_TLV 0x0001
778#define CDP_ADDRESS_TLV 0x0002
779#define CDP_PORT_ID_TLV 0x0003
780#define CDP_CAPABILITIES_TLV 0x0004
781#define CDP_VERSION_TLV 0x0005
782#define CDP_PLATFORM_TLV 0x0006
783#define CDP_NATIVE_VLAN_TLV 0x000a
784#define CDP_APPLIANCE_VLAN_TLV 0x000e
785#define CDP_TRIGGER_TLV 0x000f
786#define CDP_POWER_CONSUMPTION_TLV 0x0010
787#define CDP_SYSNAME_TLV 0x0014
788#define CDP_SYSOBJECT_TLV 0x0015
789#define CDP_MANAGEMENT_ADDRESS_TLV 0x0016
790
Bartlomiej Sieka56668462008-10-01 15:26:28 +0200791#define CDP_TIMEOUT 250UL /* one packet every 250ms */
wdenk145d2c12004-04-15 21:48:45 +0000792
793static int CDPSeq;
794static int CDPOK;
795
796ushort CDPNativeVLAN;
797ushort CDPApplianceVLAN;
798
799static const uchar CDP_SNAP_hdr[8] = { 0xAA, 0xAA, 0x03, 0x00, 0x00, 0x0C, 0x20, 0x00 };
800
801static ushort CDP_compute_csum(const uchar *buff, ushort len)
802{
803 ushort csum;
804 int odd;
805 ulong result = 0;
806 ushort leftover;
Wolfgang Denk7fb52662005-10-13 16:45:02 +0200807 ushort *p;
wdenk145d2c12004-04-15 21:48:45 +0000808
809 if (len > 0) {
810 odd = 1 & (ulong)buff;
811 if (odd) {
812 result = *buff << 8;
813 len--;
814 buff++;
815 }
816 while (len > 1) {
Wolfgang Denk7fb52662005-10-13 16:45:02 +0200817 p = (ushort *)buff;
818 result += *p++;
819 buff = (uchar *)p;
wdenk145d2c12004-04-15 21:48:45 +0000820 if (result & 0x80000000)
821 result = (result & 0xFFFF) + (result >> 16);
822 len -= 2;
823 }
824 if (len) {
825 leftover = (signed short)(*(const signed char *)buff);
Wolfgang Denkf0711132005-11-10 20:59:46 +0100826 /* CISCO SUCKS big time! (and blows too):
827 * CDP uses the IP checksum algorithm with a twist;
828 * for the last byte it *sign* extends and sums.
829 */
wdenk145d2c12004-04-15 21:48:45 +0000830 result = (result & 0xffff0000) | ((result + leftover) & 0x0000ffff);
831 }
832 while (result >> 16)
833 result = (result & 0xFFFF) + (result >> 16);
834
835 if (odd)
836 result = ((result >> 8) & 0xff) | ((result & 0xff) << 8);
837 }
838
839 /* add up 16-bit and 17-bit words for 17+c bits */
840 result = (result & 0xffff) + (result >> 16);
841 /* add up 16-bit and 2-bit for 16+c bit */
842 result = (result & 0xffff) + (result >> 16);
843 /* add up carry.. */
844 result = (result & 0xffff) + (result >> 16);
845
846 /* negate */
847 csum = ~(ushort)result;
848
849 /* run time endian detection */
850 if (csum != htons(csum)) /* little endian */
851 csum = htons(csum);
852
853 return csum;
854}
855
856int CDPSendTrigger(void)
857{
858 volatile uchar *pkt;
859 volatile ushort *s;
860 volatile ushort *cp;
861 Ethernet_t *et;
wdenk145d2c12004-04-15 21:48:45 +0000862 int len;
863 ushort chksum;
wdenk05939202004-04-18 17:39:38 +0000864#if defined(CONFIG_CDP_DEVICE_ID) || defined(CONFIG_CDP_PORT_ID) || \
865 defined(CONFIG_CDP_VERSION) || defined(CONFIG_CDP_PLATFORM)
866 char buf[32];
867#endif
wdenk145d2c12004-04-15 21:48:45 +0000868
869 pkt = NetTxPacket;
870 et = (Ethernet_t *)pkt;
871
872 /* NOTE: trigger sent not on any VLAN */
873
874 /* form ethernet header */
875 memcpy(et->et_dest, NetCDPAddr, 6);
876 memcpy(et->et_src, NetOurEther, 6);
877
878 pkt += ETHER_HDR_SIZE;
879
880 /* SNAP header */
881 memcpy((uchar *)pkt, CDP_SNAP_hdr, sizeof(CDP_SNAP_hdr));
882 pkt += sizeof(CDP_SNAP_hdr);
883
884 /* CDP header */
885 *pkt++ = 0x02; /* CDP version 2 */
886 *pkt++ = 180; /* TTL */
887 s = (volatile ushort *)pkt;
888 cp = s;
889 *s++ = htons(0); /* checksum (0 for later calculation) */
890
891 /* CDP fields */
892#ifdef CONFIG_CDP_DEVICE_ID
893 *s++ = htons(CDP_DEVICE_ID_TLV);
894 *s++ = htons(CONFIG_CDP_DEVICE_ID);
Mike Frysinger79ca1b92009-02-11 18:23:48 -0500895 sprintf(buf, CONFIG_CDP_DEVICE_ID_PREFIX "%pm", NetOurEther);
wdenk145d2c12004-04-15 21:48:45 +0000896 memcpy((uchar *)s, buf, 16);
897 s += 16 / 2;
898#endif
899
900#ifdef CONFIG_CDP_PORT_ID
901 *s++ = htons(CDP_PORT_ID_TLV);
902 memset(buf, 0, sizeof(buf));
903 sprintf(buf, CONFIG_CDP_PORT_ID, eth_get_dev_index());
904 len = strlen(buf);
905 if (len & 1) /* make it even */
906 len++;
907 *s++ = htons(len + 4);
908 memcpy((uchar *)s, buf, len);
909 s += len / 2;
910#endif
911
912#ifdef CONFIG_CDP_CAPABILITIES
913 *s++ = htons(CDP_CAPABILITIES_TLV);
914 *s++ = htons(8);
915 *(ulong *)s = htonl(CONFIG_CDP_CAPABILITIES);
916 s += 2;
917#endif
918
919#ifdef CONFIG_CDP_VERSION
920 *s++ = htons(CDP_VERSION_TLV);
921 memset(buf, 0, sizeof(buf));
922 strcpy(buf, CONFIG_CDP_VERSION);
923 len = strlen(buf);
924 if (len & 1) /* make it even */
925 len++;
926 *s++ = htons(len + 4);
927 memcpy((uchar *)s, buf, len);
928 s += len / 2;
929#endif
930
931#ifdef CONFIG_CDP_PLATFORM
932 *s++ = htons(CDP_PLATFORM_TLV);
933 memset(buf, 0, sizeof(buf));
934 strcpy(buf, CONFIG_CDP_PLATFORM);
935 len = strlen(buf);
936 if (len & 1) /* make it even */
937 len++;
938 *s++ = htons(len + 4);
939 memcpy((uchar *)s, buf, len);
940 s += len / 2;
941#endif
942
943#ifdef CONFIG_CDP_TRIGGER
944 *s++ = htons(CDP_TRIGGER_TLV);
945 *s++ = htons(8);
946 *(ulong *)s = htonl(CONFIG_CDP_TRIGGER);
947 s += 2;
948#endif
949
950#ifdef CONFIG_CDP_POWER_CONSUMPTION
951 *s++ = htons(CDP_POWER_CONSUMPTION_TLV);
952 *s++ = htons(6);
953 *s++ = htons(CONFIG_CDP_POWER_CONSUMPTION);
954#endif
955
956 /* length of ethernet packet */
957 len = (uchar *)s - ((uchar *)NetTxPacket + ETHER_HDR_SIZE);
958 et->et_protlen = htons(len);
959
960 len = ETHER_HDR_SIZE + sizeof(CDP_SNAP_hdr);
961 chksum = CDP_compute_csum((uchar *)NetTxPacket + len, (uchar *)s - (NetTxPacket + len));
962 if (chksum == 0)
963 chksum = 0xFFFF;
964 *cp = htons(chksum);
965
966 (void) eth_send(NetTxPacket, (uchar *)s - NetTxPacket);
967 return 0;
968}
969
970static void
971CDPTimeout (void)
972{
973 CDPSeq++;
974
975 if (CDPSeq < 3) {
976 NetSetTimeout (CDP_TIMEOUT, CDPTimeout);
977 CDPSendTrigger();
978 return;
979 }
980
981 /* if not OK try again */
982 if (!CDPOK)
983 NetStartAgain();
984 else
985 NetState = NETLOOP_SUCCESS;
986}
987
988static void
989CDPDummyHandler (uchar * pkt, unsigned dest, unsigned src, unsigned len)
990{
991 /* nothing */
992}
993
994static void
995CDPHandler(const uchar * pkt, unsigned len)
996{
997 const uchar *t;
998 const ushort *ss;
999 ushort type, tlen;
1000 uchar applid;
1001 ushort vlan, nvlan;
1002
1003 /* minimum size? */
1004 if (len < sizeof(CDP_SNAP_hdr) + 4)
1005 goto pkt_short;
1006
1007 /* check for valid CDP SNAP header */
1008 if (memcmp(pkt, CDP_SNAP_hdr, sizeof(CDP_SNAP_hdr)) != 0)
1009 return;
1010
1011 pkt += sizeof(CDP_SNAP_hdr);
1012 len -= sizeof(CDP_SNAP_hdr);
1013
1014 /* Version of CDP protocol must be >= 2 and TTL != 0 */
1015 if (pkt[0] < 0x02 || pkt[1] == 0)
1016 return;
1017
1018 /* if version is greater than 0x02 maybe we'll have a problem; output a warning */
1019 if (pkt[0] != 0x02)
1020 printf("** WARNING: CDP packet received with a protocol version %d > 2\n",
1021 pkt[0] & 0xff);
1022
1023 if (CDP_compute_csum(pkt, len) != 0)
1024 return;
1025
1026 pkt += 4;
1027 len -= 4;
1028
1029 vlan = htons(-1);
1030 nvlan = htons(-1);
1031 while (len > 0) {
1032 if (len < 4)
1033 goto pkt_short;
1034
1035 ss = (const ushort *)pkt;
1036 type = ntohs(ss[0]);
1037 tlen = ntohs(ss[1]);
1038 if (tlen > len) {
1039 goto pkt_short;
1040 }
1041
1042 pkt += tlen;
1043 len -= tlen;
1044
1045 ss += 2; /* point ss to the data of the TLV */
1046 tlen -= 4;
1047
1048 switch (type) {
1049 case CDP_DEVICE_ID_TLV:
1050 break;
1051 case CDP_ADDRESS_TLV:
1052 break;
1053 case CDP_PORT_ID_TLV:
1054 break;
1055 case CDP_CAPABILITIES_TLV:
1056 break;
1057 case CDP_VERSION_TLV:
1058 break;
1059 case CDP_PLATFORM_TLV:
1060 break;
1061 case CDP_NATIVE_VLAN_TLV:
1062 nvlan = *ss;
1063 break;
1064 case CDP_APPLIANCE_VLAN_TLV:
1065 t = (const uchar *)ss;
1066 while (tlen > 0) {
1067 if (tlen < 3)
1068 goto pkt_short;
1069
1070 applid = t[0];
1071 ss = (const ushort *)(t + 1);
1072
1073#ifdef CONFIG_CDP_APPLIANCE_VLAN_TYPE
1074 if (applid == CONFIG_CDP_APPLIANCE_VLAN_TYPE)
1075 vlan = *ss;
1076#else
1077 vlan = ntohs(*ss); /* XXX will this work; dunno */
1078#endif
1079 t += 3; tlen -= 3;
1080 }
1081 break;
1082 case CDP_TRIGGER_TLV:
1083 break;
1084 case CDP_POWER_CONSUMPTION_TLV:
1085 break;
1086 case CDP_SYSNAME_TLV:
1087 break;
1088 case CDP_SYSOBJECT_TLV:
1089 break;
1090 case CDP_MANAGEMENT_ADDRESS_TLV:
1091 break;
1092 }
1093 }
1094
1095 CDPApplianceVLAN = vlan;
1096 CDPNativeVLAN = nvlan;
1097
1098 CDPOK = 1;
1099 return;
1100
1101 pkt_short:
1102 printf("** CDP packet is too short\n");
1103 return;
1104}
1105
1106static void CDPStart(void)
1107{
1108#if defined(CONFIG_NET_MULTI)
1109 printf ("Using %s device\n", eth_get_name());
wdenke6466f62003-06-05 19:27:42 +00001110#endif
wdenk145d2c12004-04-15 21:48:45 +00001111 CDPSeq = 0;
1112 CDPOK = 0;
1113
1114 CDPNativeVLAN = htons(-1);
1115 CDPApplianceVLAN = htons(-1);
1116
1117 NetSetTimeout (CDP_TIMEOUT, CDPTimeout);
1118 NetSetHandler (CDPDummyHandler);
1119
1120 CDPSendTrigger();
1121}
Jon Loeligera9807e52007-07-10 11:05:02 -05001122#endif
wdenk145d2c12004-04-15 21:48:45 +00001123
Alessandro Rubinif119e3d2009-08-07 13:58:56 +02001124#ifdef CONFIG_IP_DEFRAG
1125/*
1126 * This function collects fragments in a single packet, according
1127 * to the algorithm in RFC815. It returns NULL or the pointer to
1128 * a complete packet, in static storage
1129 */
1130#ifndef CONFIG_NET_MAXDEFRAG
1131#define CONFIG_NET_MAXDEFRAG 16384
1132#endif
1133/*
1134 * MAXDEFRAG, above, is chosen in the config file and is real data
1135 * so we need to add the NFS overhead, which is more than TFTP.
1136 * To use sizeof in the internal unnamed structures, we need a real
1137 * instance (can't do "sizeof(struct rpc_t.u.reply))", unfortunately).
1138 * The compiler doesn't complain nor allocates the actual structure
1139 */
1140static struct rpc_t rpc_specimen;
1141#define IP_PKTSIZE (CONFIG_NET_MAXDEFRAG + sizeof(rpc_specimen.u.reply))
1142
1143#define IP_MAXUDP (IP_PKTSIZE - IP_HDR_SIZE_NO_UDP)
1144
1145/*
1146 * this is the packet being assembled, either data or frag control.
1147 * Fragments go by 8 bytes, so this union must be 8 bytes long
1148 */
1149struct hole {
1150 /* first_byte is address of this structure */
1151 u16 last_byte; /* last byte in this hole + 1 (begin of next hole) */
1152 u16 next_hole; /* index of next (in 8-b blocks), 0 == none */
1153 u16 prev_hole; /* index of prev, 0 == none */
1154 u16 unused;
1155};
1156
1157static IP_t *__NetDefragment(IP_t *ip, int *lenp)
1158{
1159 static uchar pkt_buff[IP_PKTSIZE] __attribute__((aligned(PKTALIGN)));
1160 static u16 first_hole, total_len;
1161 struct hole *payload, *thisfrag, *h, *newh;
1162 IP_t *localip = (IP_t *)pkt_buff;
1163 uchar *indata = (uchar *)ip;
1164 int offset8, start, len, done = 0;
1165 u16 ip_off = ntohs(ip->ip_off);
1166
1167 /* payload starts after IP header, this fragment is in there */
1168 payload = (struct hole *)(pkt_buff + IP_HDR_SIZE_NO_UDP);
1169 offset8 = (ip_off & IP_OFFS);
1170 thisfrag = payload + offset8;
1171 start = offset8 * 8;
1172 len = ntohs(ip->ip_len) - IP_HDR_SIZE_NO_UDP;
1173
1174 if (start + len > IP_MAXUDP) /* fragment extends too far */
1175 return NULL;
1176
1177 if (!total_len || localip->ip_id != ip->ip_id) {
1178 /* new (or different) packet, reset structs */
1179 total_len = 0xffff;
1180 payload[0].last_byte = ~0;
1181 payload[0].next_hole = 0;
1182 payload[0].prev_hole = 0;
1183 first_hole = 0;
1184 /* any IP header will work, copy the first we received */
1185 memcpy(localip, ip, IP_HDR_SIZE_NO_UDP);
1186 }
1187
1188 /*
1189 * What follows is the reassembly algorithm. We use the payload
1190 * array as a linked list of hole descriptors, as each hole starts
1191 * at a multiple of 8 bytes. However, last byte can be whatever value,
1192 * so it is represented as byte count, not as 8-byte blocks.
1193 */
1194
1195 h = payload + first_hole;
1196 while (h->last_byte < start) {
1197 if (!h->next_hole) {
1198 /* no hole that far away */
1199 return NULL;
1200 }
1201 h = payload + h->next_hole;
1202 }
1203
Fillod Stephanee7ade8b2010-06-11 19:26:43 +02001204 /* last fragment may be 1..7 bytes, the "+7" forces acceptance */
1205 if (offset8 + ((len + 7) / 8) <= h - payload) {
Alessandro Rubinif119e3d2009-08-07 13:58:56 +02001206 /* no overlap with holes (dup fragment?) */
1207 return NULL;
1208 }
1209
1210 if (!(ip_off & IP_FLAGS_MFRAG)) {
1211 /* no more fragmentss: truncate this (last) hole */
1212 total_len = start + len;
1213 h->last_byte = start + len;
1214 }
1215
1216 /*
1217 * There is some overlap: fix the hole list. This code doesn't
1218 * deal with a fragment that overlaps with two different holes
1219 * (thus being a superset of a previously-received fragment).
1220 */
1221
1222 if ( (h >= thisfrag) && (h->last_byte <= start + len) ) {
1223 /* complete overlap with hole: remove hole */
1224 if (!h->prev_hole && !h->next_hole) {
1225 /* last remaining hole */
1226 done = 1;
1227 } else if (!h->prev_hole) {
1228 /* first hole */
1229 first_hole = h->next_hole;
1230 payload[h->next_hole].prev_hole = 0;
1231 } else if (!h->next_hole) {
1232 /* last hole */
1233 payload[h->prev_hole].next_hole = 0;
1234 } else {
1235 /* in the middle of the list */
1236 payload[h->next_hole].prev_hole = h->prev_hole;
1237 payload[h->prev_hole].next_hole = h->next_hole;
1238 }
1239
1240 } else if (h->last_byte <= start + len) {
1241 /* overlaps with final part of the hole: shorten this hole */
1242 h->last_byte = start;
1243
1244 } else if (h >= thisfrag) {
1245 /* overlaps with initial part of the hole: move this hole */
1246 newh = thisfrag + (len / 8);
1247 *newh = *h;
1248 h = newh;
1249 if (h->next_hole)
1250 payload[h->next_hole].prev_hole = (h - payload);
1251 if (h->prev_hole)
1252 payload[h->prev_hole].next_hole = (h - payload);
1253 else
1254 first_hole = (h - payload);
1255
1256 } else {
1257 /* fragment sits in the middle: split the hole */
1258 newh = thisfrag + (len / 8);
1259 *newh = *h;
1260 h->last_byte = start;
1261 h->next_hole = (newh - payload);
1262 newh->prev_hole = (h - payload);
1263 if (newh->next_hole)
1264 payload[newh->next_hole].prev_hole = (newh - payload);
1265 }
1266
1267 /* finally copy this fragment and possibly return whole packet */
1268 memcpy((uchar *)thisfrag, indata + IP_HDR_SIZE_NO_UDP, len);
1269 if (!done)
1270 return NULL;
1271
1272 localip->ip_len = htons(total_len);
1273 *lenp = total_len + IP_HDR_SIZE_NO_UDP;
1274 return localip;
1275}
1276
1277static inline IP_t *NetDefragment(IP_t *ip, int *lenp)
1278{
1279 u16 ip_off = ntohs(ip->ip_off);
1280 if (!(ip_off & (IP_OFFS | IP_FLAGS_MFRAG)))
1281 return ip; /* not a fragment */
1282 return __NetDefragment(ip, lenp);
1283}
1284
1285#else /* !CONFIG_IP_DEFRAG */
1286
1287static inline IP_t *NetDefragment(IP_t *ip, int *lenp)
1288{
1289 u16 ip_off = ntohs(ip->ip_off);
1290 if (!(ip_off & (IP_OFFS | IP_FLAGS_MFRAG)))
1291 return ip; /* not a fragment */
1292 return NULL;
1293}
1294#endif
wdenk2d966952002-10-31 22:12:35 +00001295
1296void
wdenk145d2c12004-04-15 21:48:45 +00001297NetReceive(volatile uchar * inpkt, int len)
wdenk2d966952002-10-31 22:12:35 +00001298{
1299 Ethernet_t *et;
1300 IP_t *ip;
1301 ARP_t *arp;
1302 IPaddr_t tmp;
1303 int x;
wdenk145d2c12004-04-15 21:48:45 +00001304 uchar *pkt;
Jon Loeliger54f35c22007-07-09 17:45:14 -05001305#if defined(CONFIG_CMD_CDP)
wdenk145d2c12004-04-15 21:48:45 +00001306 int iscdp;
1307#endif
1308 ushort cti = 0, vlanid = VLAN_NONE, myvlanid, mynvlanid;
wdenk2d966952002-10-31 22:12:35 +00001309
Robin Getz9e0a4d62009-07-23 03:01:03 -04001310 debug("packet received\n");
wdenk145d2c12004-04-15 21:48:45 +00001311
Mike Frysingerdc166032009-07-18 21:04:08 -04001312 NetRxPacket = inpkt;
1313 NetRxPacketLen = len;
wdenk145d2c12004-04-15 21:48:45 +00001314 et = (Ethernet_t *)inpkt;
1315
1316 /* too small packet? */
1317 if (len < ETHER_HDR_SIZE)
1318 return;
Rafal Jaworowski1f2c9a42007-12-27 18:19:02 +01001319
1320#ifdef CONFIG_API
1321 if (push_packet) {
1322 (*push_packet)(inpkt, len);
1323 return;
1324 }
1325#endif
wdenk145d2c12004-04-15 21:48:45 +00001326
Jon Loeliger54f35c22007-07-09 17:45:14 -05001327#if defined(CONFIG_CMD_CDP)
wdenk145d2c12004-04-15 21:48:45 +00001328 /* keep track if packet is CDP */
1329 iscdp = memcmp(et->et_dest, NetCDPAddr, 6) == 0;
1330#endif
1331
1332 myvlanid = ntohs(NetOurVLAN);
1333 if (myvlanid == (ushort)-1)
1334 myvlanid = VLAN_NONE;
1335 mynvlanid = ntohs(NetOurNativeVLAN);
1336 if (mynvlanid == (ushort)-1)
1337 mynvlanid = VLAN_NONE;
wdenk2d966952002-10-31 22:12:35 +00001338
1339 x = ntohs(et->et_protlen);
1340
Robin Getz9e0a4d62009-07-23 03:01:03 -04001341 debug("packet received\n");
wdenk145d2c12004-04-15 21:48:45 +00001342
wdenk2d966952002-10-31 22:12:35 +00001343 if (x < 1514) {
1344 /*
1345 * Got a 802 packet. Check the other protocol field.
1346 */
1347 x = ntohs(et->et_prot);
wdenk145d2c12004-04-15 21:48:45 +00001348
1349 ip = (IP_t *)(inpkt + E802_HDR_SIZE);
wdenk2d966952002-10-31 22:12:35 +00001350 len -= E802_HDR_SIZE;
wdenk145d2c12004-04-15 21:48:45 +00001351
1352 } else if (x != PROT_VLAN) { /* normal packet */
1353 ip = (IP_t *)(inpkt + ETHER_HDR_SIZE);
wdenk2d966952002-10-31 22:12:35 +00001354 len -= ETHER_HDR_SIZE;
wdenk145d2c12004-04-15 21:48:45 +00001355
1356 } else { /* VLAN packet */
1357 VLAN_Ethernet_t *vet = (VLAN_Ethernet_t *)et;
1358
Robin Getz9e0a4d62009-07-23 03:01:03 -04001359 debug("VLAN packet received\n");
1360
wdenk145d2c12004-04-15 21:48:45 +00001361 /* too small packet? */
1362 if (len < VLAN_ETHER_HDR_SIZE)
1363 return;
1364
1365 /* if no VLAN active */
1366 if ((ntohs(NetOurVLAN) & VLAN_IDMASK) == VLAN_NONE
Jon Loeliger54f35c22007-07-09 17:45:14 -05001367#if defined(CONFIG_CMD_CDP)
wdenk145d2c12004-04-15 21:48:45 +00001368 && iscdp == 0
1369#endif
1370 )
1371 return;
1372
1373 cti = ntohs(vet->vet_tag);
1374 vlanid = cti & VLAN_IDMASK;
1375 x = ntohs(vet->vet_type);
1376
1377 ip = (IP_t *)(inpkt + VLAN_ETHER_HDR_SIZE);
1378 len -= VLAN_ETHER_HDR_SIZE;
wdenk2d966952002-10-31 22:12:35 +00001379 }
1380
Robin Getz9e0a4d62009-07-23 03:01:03 -04001381 debug("Receive from protocol 0x%x\n", x);
wdenk2d966952002-10-31 22:12:35 +00001382
Jon Loeliger54f35c22007-07-09 17:45:14 -05001383#if defined(CONFIG_CMD_CDP)
wdenk145d2c12004-04-15 21:48:45 +00001384 if (iscdp) {
1385 CDPHandler((uchar *)ip, len);
1386 return;
1387 }
1388#endif
1389
1390 if ((myvlanid & VLAN_IDMASK) != VLAN_NONE) {
1391 if (vlanid == VLAN_NONE)
1392 vlanid = (mynvlanid & VLAN_IDMASK);
1393 /* not matched? */
1394 if (vlanid != (myvlanid & VLAN_IDMASK))
1395 return;
1396 }
1397
wdenk2d966952002-10-31 22:12:35 +00001398 switch (x) {
1399
1400 case PROT_ARP:
1401 /*
1402 * We have to deal with two types of ARP packets:
wdenk57b2d802003-06-27 21:31:46 +00001403 * - REQUEST packets will be answered by sending our
1404 * IP address - if we know it.
1405 * - REPLY packates are expected only after we asked
1406 * for the TFTP server's or the gateway's ethernet
1407 * address; so if we receive such a packet, we set
1408 * the server ethernet address
wdenk2d966952002-10-31 22:12:35 +00001409 */
Robin Getz9e0a4d62009-07-23 03:01:03 -04001410 debug("Got ARP\n");
1411
wdenk2d966952002-10-31 22:12:35 +00001412 arp = (ARP_t *)ip;
1413 if (len < ARP_HDR_SIZE) {
1414 printf("bad length %d < %d\n", len, ARP_HDR_SIZE);
1415 return;
1416 }
1417 if (ntohs(arp->ar_hrd) != ARP_ETHER) {
1418 return;
1419 }
1420 if (ntohs(arp->ar_pro) != PROT_IP) {
1421 return;
1422 }
1423 if (arp->ar_hln != 6) {
1424 return;
1425 }
1426 if (arp->ar_pln != 4) {
1427 return;
1428 }
1429
1430 if (NetOurIP == 0) {
1431 return;
1432 }
1433
1434 if (NetReadIP(&arp->ar_data[16]) != NetOurIP) {
1435 return;
1436 }
1437
1438 switch (ntohs(arp->ar_op)) {
1439 case ARPOP_REQUEST: /* reply with our IP address */
Robin Getz9e0a4d62009-07-23 03:01:03 -04001440 debug("Got ARP REQUEST, return our IP\n");
wdenk145d2c12004-04-15 21:48:45 +00001441 pkt = (uchar *)et;
1442 pkt += NetSetEther(pkt, et->et_src, PROT_ARP);
wdenk2d966952002-10-31 22:12:35 +00001443 arp->ar_op = htons(ARPOP_REPLY);
1444 memcpy (&arp->ar_data[10], &arp->ar_data[0], 6);
1445 NetCopyIP(&arp->ar_data[16], &arp->ar_data[6]);
1446 memcpy (&arp->ar_data[ 0], NetOurEther, 6);
1447 NetCopyIP(&arp->ar_data[ 6], &NetOurIP);
wdenk145d2c12004-04-15 21:48:45 +00001448 (void) eth_send((uchar *)et, (pkt - (uchar *)et) + ARP_HDR_SIZE);
wdenk2d966952002-10-31 22:12:35 +00001449 return;
wdenke6466f62003-06-05 19:27:42 +00001450
1451 case ARPOP_REPLY: /* arp reply */
1452 /* are we waiting for a reply */
1453 if (!NetArpWaitPacketIP || !NetArpWaitPacketMAC)
1454 break;
Robin Getz470a6d42009-07-21 12:15:28 -04001455
1456#ifdef CONFIG_KEEP_SERVERADDR
1457 if (NetServerIP == NetArpWaitPacketIP) {
1458 char buf[20];
1459 sprintf(buf, "%pM", arp->ar_data);
1460 setenv("serveraddr", buf);
1461 }
1462#endif
1463
Robin Getz9e0a4d62009-07-23 03:01:03 -04001464 debug("Got ARP REPLY, set server/gtwy eth addr (%pM)\n",
Mike Frysinger79ca1b92009-02-11 18:23:48 -05001465 arp->ar_data);
wdenke6466f62003-06-05 19:27:42 +00001466
1467 tmp = NetReadIP(&arp->ar_data[6]);
1468
1469 /* matched waiting packet's address */
1470 if (tmp == NetArpWaitReplyIP) {
Robin Getz9e0a4d62009-07-23 03:01:03 -04001471 debug("Got it\n");
wdenke6466f62003-06-05 19:27:42 +00001472 /* save address for later use */
1473 memcpy(NetArpWaitPacketMAC, &arp->ar_data[0], 6);
1474
wdenkb8fb6192004-08-02 21:11:11 +00001475#ifdef CONFIG_NETCONSOLE
1476 (*packetHandler)(0,0,0,0);
1477#endif
wdenke6466f62003-06-05 19:27:42 +00001478 /* modify header, and transmit it */
1479 memcpy(((Ethernet_t *)NetArpWaitTxPacket)->et_dest, NetArpWaitPacketMAC, 6);
1480 (void) eth_send(NetArpWaitTxPacket, NetArpWaitTxPacketSize);
1481
1482 /* no arp request pending now */
1483 NetArpWaitPacketIP = 0;
1484 NetArpWaitTxPacketSize = 0;
1485 NetArpWaitPacketMAC = NULL;
1486
1487 }
wdenk2d966952002-10-31 22:12:35 +00001488 return;
1489 default:
Robin Getz9e0a4d62009-07-23 03:01:03 -04001490 debug("Unexpected ARP opcode 0x%x\n", ntohs(arp->ar_op));
wdenk2d966952002-10-31 22:12:35 +00001491 return;
1492 }
wdenkcb99da52005-01-12 00:15:14 +00001493 break;
wdenk2d966952002-10-31 22:12:35 +00001494
1495 case PROT_RARP:
Robin Getz9e0a4d62009-07-23 03:01:03 -04001496 debug("Got RARP\n");
wdenk2d966952002-10-31 22:12:35 +00001497 arp = (ARP_t *)ip;
1498 if (len < ARP_HDR_SIZE) {
1499 printf("bad length %d < %d\n", len, ARP_HDR_SIZE);
1500 return;
1501 }
1502
1503 if ((ntohs(arp->ar_op) != RARPOP_REPLY) ||
1504 (ntohs(arp->ar_hrd) != ARP_ETHER) ||
1505 (ntohs(arp->ar_pro) != PROT_IP) ||
1506 (arp->ar_hln != 6) || (arp->ar_pln != 4)) {
1507
wdenk42c05472004-03-23 22:14:11 +00001508 puts ("invalid RARP header\n");
wdenk2d966952002-10-31 22:12:35 +00001509 } else {
1510 NetCopyIP(&NetOurIP, &arp->ar_data[16]);
wdenk4989f872004-03-14 15:06:13 +00001511 if (NetServerIP == 0)
1512 NetCopyIP(&NetServerIP, &arp->ar_data[ 6]);
wdenk2d966952002-10-31 22:12:35 +00001513 memcpy (NetServerEther, &arp->ar_data[ 0], 6);
1514
1515 (*packetHandler)(0,0,0,0);
1516 }
1517 break;
1518
1519 case PROT_IP:
Robin Getz9e0a4d62009-07-23 03:01:03 -04001520 debug("Got IP\n");
Alessandro Rubinif119e3d2009-08-07 13:58:56 +02001521 /* Before we start poking the header, make sure it is there */
wdenk2d966952002-10-31 22:12:35 +00001522 if (len < IP_HDR_SIZE) {
Robin Getz9e0a4d62009-07-23 03:01:03 -04001523 debug("len bad %d < %lu\n", len, (ulong)IP_HDR_SIZE);
wdenk2d966952002-10-31 22:12:35 +00001524 return;
1525 }
Alessandro Rubinif119e3d2009-08-07 13:58:56 +02001526 /* Check the packet length */
wdenk2d966952002-10-31 22:12:35 +00001527 if (len < ntohs(ip->ip_len)) {
1528 printf("len bad %d < %d\n", len, ntohs(ip->ip_len));
1529 return;
1530 }
1531 len = ntohs(ip->ip_len);
Robin Getz9e0a4d62009-07-23 03:01:03 -04001532 debug("len=%d, v=%02x\n", len, ip->ip_hl_v & 0xff);
1533
Alessandro Rubinif119e3d2009-08-07 13:58:56 +02001534 /* Can't deal with anything except IPv4 */
wdenk2d966952002-10-31 22:12:35 +00001535 if ((ip->ip_hl_v & 0xf0) != 0x40) {
1536 return;
1537 }
Alessandro Rubinif119e3d2009-08-07 13:58:56 +02001538 /* Can't deal with IP options (headers != 20 bytes) */
Remy Bohmerb9535782008-06-03 15:48:17 +02001539 if ((ip->ip_hl_v & 0x0f) > 0x05) {
1540 return;
1541 }
Alessandro Rubinif119e3d2009-08-07 13:58:56 +02001542 /* Check the Checksum of the header */
wdenk2d966952002-10-31 22:12:35 +00001543 if (!NetCksumOk((uchar *)ip, IP_HDR_SIZE_NO_UDP / 2)) {
wdenk42c05472004-03-23 22:14:11 +00001544 puts ("checksum bad\n");
wdenk2d966952002-10-31 22:12:35 +00001545 return;
1546 }
Alessandro Rubinif119e3d2009-08-07 13:58:56 +02001547 /* If it is not for us, ignore it */
wdenk2d966952002-10-31 22:12:35 +00001548 tmp = NetReadIP(&ip->ip_dst);
1549 if (NetOurIP && tmp != NetOurIP && tmp != 0xFFFFFFFF) {
David Updegraff7280da72007-06-11 10:41:07 -05001550#ifdef CONFIG_MCAST_TFTP
Wolfgang Denk627f5c32007-08-14 09:47:27 +02001551 if (Mcast_addr != tmp)
David Updegraff7280da72007-06-11 10:41:07 -05001552#endif
wdenk2d966952002-10-31 22:12:35 +00001553 return;
1554 }
1555 /*
Alessandro Rubinif119e3d2009-08-07 13:58:56 +02001556 * The function returns the unchanged packet if it's not
1557 * a fragment, and either the complete packet or NULL if
1558 * it is a fragment (if !CONFIG_IP_DEFRAG, it returns NULL)
1559 */
1560 if (!(ip = NetDefragment(ip, &len)))
1561 return;
1562 /*
wdenk2d966952002-10-31 22:12:35 +00001563 * watch for ICMP host redirects
1564 *
wdenk57b2d802003-06-27 21:31:46 +00001565 * There is no real handler code (yet). We just watch
1566 * for ICMP host redirect messages. In case anybody
1567 * sees these messages: please contact me
1568 * (wd@denx.de), or - even better - send me the
1569 * necessary fixes :-)
wdenk2d966952002-10-31 22:12:35 +00001570 *
wdenk57b2d802003-06-27 21:31:46 +00001571 * Note: in all cases where I have seen this so far
1572 * it was a problem with the router configuration,
1573 * for instance when a router was configured in the
1574 * BOOTP reply, but the TFTP server was on the same
1575 * subnet. So this is probably a warning that your
1576 * configuration might be wrong. But I'm not really
1577 * sure if there aren't any other situations.
wdenk2d966952002-10-31 22:12:35 +00001578 */
1579 if (ip->ip_p == IPPROTO_ICMP) {
1580 ICMP_t *icmph = (ICMP_t *)&(ip->udp_src);
1581
wdenke6466f62003-06-05 19:27:42 +00001582 switch (icmph->type) {
1583 case ICMP_REDIRECT:
wdenkcbc49a52005-05-03 14:12:25 +00001584 if (icmph->code != ICMP_REDIR_HOST)
1585 return;
Mike Frysingerd5695f42009-02-17 00:00:53 -05001586 printf (" ICMP Host Redirect to %pI4 ", &icmph->un.gateway);
Stefan Roesedfced812005-08-12 20:06:52 +02001587 return;
Jon Loeliger54f35c22007-07-09 17:45:14 -05001588#if defined(CONFIG_CMD_PING)
wdenke6466f62003-06-05 19:27:42 +00001589 case ICMP_ECHO_REPLY:
1590 /*
1591 * IP header OK. Pass the packet to the current handler.
1592 */
1593 /* XXX point to ip packet */
1594 (*packetHandler)((uchar *)ip, 0, 0, 0);
Stefan Roesedfced812005-08-12 20:06:52 +02001595 return;
Ed Swarthout83109192007-03-07 12:14:50 -06001596 case ICMP_ECHO_REQUEST:
Robin Getz9e0a4d62009-07-23 03:01:03 -04001597 debug("Got ICMP ECHO REQUEST, return %d bytes \n",
Ed Swarthout83109192007-03-07 12:14:50 -06001598 ETHER_HDR_SIZE + len);
Robin Getz9e0a4d62009-07-23 03:01:03 -04001599
Ed Swarthout83109192007-03-07 12:14:50 -06001600 memcpy (&et->et_dest[0], &et->et_src[0], 6);
1601 memcpy (&et->et_src[ 0], NetOurEther, 6);
1602
1603 ip->ip_sum = 0;
1604 ip->ip_off = 0;
1605 NetCopyIP((void*)&ip->ip_dst, &ip->ip_src);
1606 NetCopyIP((void*)&ip->ip_src, &NetOurIP);
1607 ip->ip_sum = ~NetCksum((uchar *)ip, IP_HDR_SIZE_NO_UDP >> 1);
1608
1609 icmph->type = ICMP_ECHO_REPLY;
1610 icmph->checksum = 0;
1611 icmph->checksum = ~NetCksum((uchar *)icmph,
1612 (len - IP_HDR_SIZE_NO_UDP) >> 1);
1613 (void) eth_send((uchar *)et, ETHER_HDR_SIZE + len);
1614 return;
wdenke6466f62003-06-05 19:27:42 +00001615#endif
1616 default:
1617 return;
1618 }
wdenk2d966952002-10-31 22:12:35 +00001619 } else if (ip->ip_p != IPPROTO_UDP) { /* Only UDP packets */
1620 return;
1621 }
1622
Stefan Roesedfced812005-08-12 20:06:52 +02001623#ifdef CONFIG_UDP_CHECKSUM
1624 if (ip->udp_xsum != 0) {
Wolfgang Denk30b87322005-08-12 23:43:12 +02001625 ulong xsum;
Stefan Roesedfced812005-08-12 20:06:52 +02001626 ushort *sumptr;
1627 ushort sumlen;
1628
1629 xsum = ip->ip_p;
1630 xsum += (ntohs(ip->udp_len));
1631 xsum += (ntohl(ip->ip_src) >> 16) & 0x0000ffff;
1632 xsum += (ntohl(ip->ip_src) >> 0) & 0x0000ffff;
1633 xsum += (ntohl(ip->ip_dst) >> 16) & 0x0000ffff;
1634 xsum += (ntohl(ip->ip_dst) >> 0) & 0x0000ffff;
1635
1636 sumlen = ntohs(ip->udp_len);
1637 sumptr = (ushort *) &(ip->udp_src);
1638
1639 while (sumlen > 1) {
Wolfgang Denk30b87322005-08-12 23:43:12 +02001640 ushort sumdata;
Stefan Roesedfced812005-08-12 20:06:52 +02001641
1642 sumdata = *sumptr++;
1643 xsum += ntohs(sumdata);
1644 sumlen -= 2;
1645 }
1646 if (sumlen > 0) {
Wolfgang Denk30b87322005-08-12 23:43:12 +02001647 ushort sumdata;
Stefan Roesedfced812005-08-12 20:06:52 +02001648
1649 sumdata = *(unsigned char *) sumptr;
Wolfgang Denk30b87322005-08-12 23:43:12 +02001650 sumdata = (sumdata << 8) & 0xff00;
Stefan Roesedfced812005-08-12 20:06:52 +02001651 xsum += sumdata;
1652 }
1653 while ((xsum >> 16) != 0) {
Wolfgang Denk30b87322005-08-12 23:43:12 +02001654 xsum = (xsum & 0x0000ffff) + ((xsum >> 16) & 0x0000ffff);
Stefan Roesedfced812005-08-12 20:06:52 +02001655 }
1656 if ((xsum != 0x00000000) && (xsum != 0x0000ffff)) {
Wolfgang Denk12cec0a2008-07-11 01:16:00 +02001657 printf(" UDP wrong checksum %08lx %08x\n",
1658 xsum, ntohs(ip->udp_xsum));
Stefan Roesedfced812005-08-12 20:06:52 +02001659 return;
1660 }
1661 }
1662#endif
1663
David Updegraff7280da72007-06-11 10:41:07 -05001664
wdenkb8fb6192004-08-02 21:11:11 +00001665#ifdef CONFIG_NETCONSOLE
1666 nc_input_packet((uchar *)ip +IP_HDR_SIZE,
1667 ntohs(ip->udp_dst),
1668 ntohs(ip->udp_src),
1669 ntohs(ip->udp_len) - 8);
1670#endif
wdenk2d966952002-10-31 22:12:35 +00001671 /*
1672 * IP header OK. Pass the packet to the current handler.
1673 */
1674 (*packetHandler)((uchar *)ip +IP_HDR_SIZE,
1675 ntohs(ip->udp_dst),
1676 ntohs(ip->udp_src),
1677 ntohs(ip->udp_len) - 8);
wdenk2d966952002-10-31 22:12:35 +00001678 break;
1679 }
1680}
1681
1682
1683/**********************************************************************/
1684
1685static int net_check_prereq (proto_t protocol)
1686{
1687 switch (protocol) {
wdenk05939202004-04-18 17:39:38 +00001688 /* Fall through */
Jon Loeliger54f35c22007-07-09 17:45:14 -05001689#if defined(CONFIG_CMD_PING)
wdenke6466f62003-06-05 19:27:42 +00001690 case PING:
wdenk05939202004-04-18 17:39:38 +00001691 if (NetPingIP == 0) {
1692 puts ("*** ERROR: ping address not given\n");
1693 return (1);
1694 }
1695 goto common;
wdenke6466f62003-06-05 19:27:42 +00001696#endif
Jon Loeliger54f35c22007-07-09 17:45:14 -05001697#if defined(CONFIG_CMD_SNTP)
wdenkb4ad9622005-04-01 00:25:43 +00001698 case SNTP:
1699 if (NetNtpServerIP == 0) {
1700 puts ("*** ERROR: NTP server address not given\n");
1701 return (1);
1702 }
1703 goto common;
1704#endif
Robin Getz82f0d232009-07-20 14:53:54 -04001705#if defined(CONFIG_CMD_DNS)
1706 case DNS:
1707 if (NetOurDNSIP == 0) {
1708 puts("*** ERROR: DNS server address not given\n");
1709 return 1;
1710 }
1711 goto common;
1712#endif
Jon Loeliger54f35c22007-07-09 17:45:14 -05001713#if defined(CONFIG_CMD_NFS)
wdenkbe9c1cb2004-02-24 02:00:03 +00001714 case NFS:
1715#endif
wdenkb8fb6192004-08-02 21:11:11 +00001716 case NETCONS:
wdenk2d966952002-10-31 22:12:35 +00001717 case TFTP:
wdenk05939202004-04-18 17:39:38 +00001718 if (NetServerIP == 0) {
1719 puts ("*** ERROR: `serverip' not set\n");
1720 return (1);
1721 }
Jon Loeliger54f35c22007-07-09 17:45:14 -05001722#if defined(CONFIG_CMD_PING) || defined(CONFIG_CMD_SNTP)
Wolfgang Denk30b87322005-08-12 23:43:12 +02001723 common:
wdenke6466f62003-06-05 19:27:42 +00001724#endif
1725
wdenk05939202004-04-18 17:39:38 +00001726 if (NetOurIP == 0) {
1727 puts ("*** ERROR: `ipaddr' not set\n");
1728 return (1);
1729 }
1730 /* Fall through */
wdenk2d966952002-10-31 22:12:35 +00001731
1732 case DHCP:
1733 case RARP:
1734 case BOOTP:
wdenk145d2c12004-04-15 21:48:45 +00001735 case CDP:
wdenk05939202004-04-18 17:39:38 +00001736 if (memcmp (NetOurEther, "\0\0\0\0\0\0", 6) == 0) {
wdenk2d966952002-10-31 22:12:35 +00001737#ifdef CONFIG_NET_MULTI
wdenk05939202004-04-18 17:39:38 +00001738 extern int eth_get_dev_index (void);
1739 int num = eth_get_dev_index ();
wdenk2d966952002-10-31 22:12:35 +00001740
wdenk05939202004-04-18 17:39:38 +00001741 switch (num) {
1742 case -1:
wdenk2d966952002-10-31 22:12:35 +00001743 puts ("*** ERROR: No ethernet found.\n");
1744 return (1);
wdenk05939202004-04-18 17:39:38 +00001745 case 0:
wdenk2d966952002-10-31 22:12:35 +00001746 puts ("*** ERROR: `ethaddr' not set\n");
1747 break;
wdenk05939202004-04-18 17:39:38 +00001748 default:
wdenk57b2d802003-06-27 21:31:46 +00001749 printf ("*** ERROR: `eth%daddr' not set\n",
wdenk2d966952002-10-31 22:12:35 +00001750 num);
1751 break;
wdenk05939202004-04-18 17:39:38 +00001752 }
wdenk2d966952002-10-31 22:12:35 +00001753
wdenk05939202004-04-18 17:39:38 +00001754 NetStartAgain ();
1755 return (2);
wdenk2d966952002-10-31 22:12:35 +00001756#else
wdenk05939202004-04-18 17:39:38 +00001757 puts ("*** ERROR: `ethaddr' not set\n");
1758 return (1);
wdenk2d966952002-10-31 22:12:35 +00001759#endif
wdenk05939202004-04-18 17:39:38 +00001760 }
1761 /* Fall through */
1762 default:
1763 return (0);
wdenk2d966952002-10-31 22:12:35 +00001764 }
wdenk05939202004-04-18 17:39:38 +00001765 return (0); /* OK */
wdenk2d966952002-10-31 22:12:35 +00001766}
1767/**********************************************************************/
1768
1769int
1770NetCksumOk(uchar * ptr, int len)
1771{
1772 return !((NetCksum(ptr, len) + 1) & 0xfffe);
1773}
1774
1775
1776unsigned
1777NetCksum(uchar * ptr, int len)
1778{
1779 ulong xsum;
Stefan Roeseac553282005-08-31 12:55:50 +02001780 ushort *p = (ushort *)ptr;
wdenk2d966952002-10-31 22:12:35 +00001781
1782 xsum = 0;
1783 while (len-- > 0)
Wolfgang Denk3135c542005-08-26 01:36:03 +02001784 xsum += *p++;
wdenk2d966952002-10-31 22:12:35 +00001785 xsum = (xsum & 0xffff) + (xsum >> 16);
1786 xsum = (xsum & 0xffff) + (xsum >> 16);
1787 return (xsum & 0xffff);
1788}
1789
wdenk145d2c12004-04-15 21:48:45 +00001790int
1791NetEthHdrSize(void)
1792{
1793 ushort myvlanid;
1794
1795 myvlanid = ntohs(NetOurVLAN);
1796 if (myvlanid == (ushort)-1)
1797 myvlanid = VLAN_NONE;
wdenk2d966952002-10-31 22:12:35 +00001798
wdenk145d2c12004-04-15 21:48:45 +00001799 return ((myvlanid & VLAN_IDMASK) == VLAN_NONE) ? ETHER_HDR_SIZE : VLAN_ETHER_HDR_SIZE;
1800}
1801
1802int
wdenk2d966952002-10-31 22:12:35 +00001803NetSetEther(volatile uchar * xet, uchar * addr, uint prot)
1804{
1805 Ethernet_t *et = (Ethernet_t *)xet;
wdenk145d2c12004-04-15 21:48:45 +00001806 ushort myvlanid;
1807
1808 myvlanid = ntohs(NetOurVLAN);
1809 if (myvlanid == (ushort)-1)
1810 myvlanid = VLAN_NONE;
wdenk2d966952002-10-31 22:12:35 +00001811
1812 memcpy (et->et_dest, addr, 6);
1813 memcpy (et->et_src, NetOurEther, 6);
wdenk145d2c12004-04-15 21:48:45 +00001814 if ((myvlanid & VLAN_IDMASK) == VLAN_NONE) {
wdenk2d966952002-10-31 22:12:35 +00001815 et->et_protlen = htons(prot);
wdenk145d2c12004-04-15 21:48:45 +00001816 return ETHER_HDR_SIZE;
1817 } else {
1818 VLAN_Ethernet_t *vet = (VLAN_Ethernet_t *)xet;
wdenk2d966952002-10-31 22:12:35 +00001819
wdenk145d2c12004-04-15 21:48:45 +00001820 vet->vet_vlan_type = htons(PROT_VLAN);
1821 vet->vet_tag = htons((0 << 5) | (myvlanid & VLAN_IDMASK));
1822 vet->vet_type = htons(prot);
1823 return VLAN_ETHER_HDR_SIZE;
1824 }
1825}
wdenk2d966952002-10-31 22:12:35 +00001826
1827void
1828NetSetIP(volatile uchar * xip, IPaddr_t dest, int dport, int sport, int len)
1829{
Olav Morkene8a02772009-01-23 12:56:26 +01001830 IP_t *ip = (IP_t *)xip;
wdenk2d966952002-10-31 22:12:35 +00001831
1832 /*
1833 * If the data is an odd number of bytes, zero the
1834 * byte after the last byte so that the checksum
1835 * will work.
1836 */
1837 if (len & 1)
1838 xip[IP_HDR_SIZE + len] = 0;
1839
1840 /*
1841 * Construct an IP and UDP header.
wdenk05939202004-04-18 17:39:38 +00001842 * (need to set no fragment bit - XXX)
wdenk2d966952002-10-31 22:12:35 +00001843 */
1844 ip->ip_hl_v = 0x45; /* IP_HDR_SIZE / 4 (not including UDP) */
1845 ip->ip_tos = 0;
1846 ip->ip_len = htons(IP_HDR_SIZE + len);
1847 ip->ip_id = htons(NetIPID++);
Peter Tyser0885bf02008-12-01 16:26:20 -06001848 ip->ip_off = htons(IP_FLAGS_DFRAG); /* Don't fragment */
wdenk2d966952002-10-31 22:12:35 +00001849 ip->ip_ttl = 255;
1850 ip->ip_p = 17; /* UDP */
1851 ip->ip_sum = 0;
1852 NetCopyIP((void*)&ip->ip_src, &NetOurIP); /* already in network byte order */
1853 NetCopyIP((void*)&ip->ip_dst, &dest); /* - "" - */
1854 ip->udp_src = htons(sport);
1855 ip->udp_dst = htons(dport);
1856 ip->udp_len = htons(8 + len);
1857 ip->udp_xsum = 0;
1858 ip->ip_sum = ~NetCksum((uchar *)ip, IP_HDR_SIZE_NO_UDP / 2);
1859}
1860
Wolfgang Denk7fb52662005-10-13 16:45:02 +02001861void copy_filename (char *dst, char *src, int size)
wdenk2d966952002-10-31 22:12:35 +00001862{
1863 if (*src && (*src == '"')) {
1864 ++src;
1865 --size;
1866 }
1867
1868 while ((--size > 0) && *src && (*src != '"')) {
1869 *dst++ = *src++;
1870 }
1871 *dst = '\0';
1872}
1873
Robin Getz82f0d232009-07-20 14:53:54 -04001874#if defined(CONFIG_CMD_NFS) || defined(CONFIG_CMD_SNTP) || defined(CONFIG_CMD_DNS)
1875/*
Robin Getz55b50e92010-03-08 14:07:00 -05001876 * make port a little random (1024-17407)
1877 * This keeps the math somewhat trivial to compute, and seems to work with
1878 * all supported protocols/clients/servers
Robin Getz82f0d232009-07-20 14:53:54 -04001879 */
1880unsigned int random_port(void)
1881{
Robin Getz55b50e92010-03-08 14:07:00 -05001882 return 1024 + (get_timer(0) % 0x4000);
Robin Getz82f0d232009-07-20 14:53:54 -04001883}
1884#endif
1885
wdenk2d966952002-10-31 22:12:35 +00001886void ip_to_string (IPaddr_t x, char *s)
1887{
wdenk145d2c12004-04-15 21:48:45 +00001888 x = ntohl (x);
1889 sprintf (s, "%d.%d.%d.%d",
1890 (int) ((x >> 24) & 0xff),
1891 (int) ((x >> 16) & 0xff),
1892 (int) ((x >> 8) & 0xff), (int) ((x >> 0) & 0xff)
wdenk05939202004-04-18 17:39:38 +00001893 );
wdenk2d966952002-10-31 22:12:35 +00001894}
1895
wdenk145d2c12004-04-15 21:48:45 +00001896void VLAN_to_string(ushort x, char *s)
1897{
1898 x = ntohs(x);
1899
1900 if (x == (ushort)-1)
1901 x = VLAN_NONE;
1902
1903 if (x == VLAN_NONE)
1904 strcpy(s, "none");
1905 else
1906 sprintf(s, "%d", x & VLAN_IDMASK);
1907}
1908
1909ushort string_to_VLAN(char *s)
1910{
1911 ushort id;
1912
1913 if (s == NULL)
wdenk656140b2004-04-25 13:18:40 +00001914 return htons(VLAN_NONE);
wdenk145d2c12004-04-15 21:48:45 +00001915
1916 if (*s < '0' || *s > '9')
1917 id = VLAN_NONE;
1918 else
1919 id = (ushort)simple_strtoul(s, NULL, 10);
1920
wdenk656140b2004-04-25 13:18:40 +00001921 return htons(id);
wdenk145d2c12004-04-15 21:48:45 +00001922}
1923
wdenk145d2c12004-04-15 21:48:45 +00001924ushort getenv_VLAN(char *var)
1925{
1926 return (string_to_VLAN(getenv(var)));
1927}