blob: cac3e09d96dc2302f230fc6c1574fca834e83149 [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
wdenk2d966952002-10-31 22:12:35 +000092
Jon Loeliger54f35c22007-07-09 17:45:14 -050093#if defined(CONFIG_CMD_NET)
wdenk2d966952002-10-31 22:12:35 +000094
Wolfgang Denk6405a152006-03-31 18:32:53 +020095DECLARE_GLOBAL_DATA_PTR;
96
Jean-Christophe PLAGNIOL-VILLARDc1a61642007-11-17 11:31:10 +010097#define ARP_TIMEOUT 5UL /* Seconds before trying ARP again */
wdenke6466f62003-06-05 19:27:42 +000098#ifndef CONFIG_NET_RETRY_COUNT
99# define ARP_TIMEOUT_COUNT 5 /* # of timeouts before giving up */
100#else
101# define ARP_TIMEOUT_COUNT (CONFIG_NET_RETRY_COUNT)
102#endif
103
wdenk2d966952002-10-31 22:12:35 +0000104#if 0
105#define ET_DEBUG
106#endif
107
108/** BOOTP EXTENTIONS **/
109
110IPaddr_t NetOurSubnetMask=0; /* Our subnet mask (0=unknown) */
111IPaddr_t NetOurGatewayIP=0; /* Our gateways IP address */
112IPaddr_t NetOurDNSIP=0; /* Our DNS IP address */
Jon Loeliger5336a762007-07-09 22:08:34 -0500113#if defined(CONFIG_BOOTP_DNS2)
stroesee0aadfb2003-08-28 14:17:32 +0000114IPaddr_t NetOurDNS2IP=0; /* Our 2nd DNS IP address */
115#endif
wdenk2d966952002-10-31 22:12:35 +0000116char NetOurNISDomain[32]={0,}; /* Our NIS domain */
117char NetOurHostName[32]={0,}; /* Our hostname */
118char NetOurRootPath[64]={0,}; /* Our bootpath */
119ushort NetBootFileSize=0; /* Our bootfile size in blocks */
120
David Updegraff7280da72007-06-11 10:41:07 -0500121#ifdef CONFIG_MCAST_TFTP /* Multicast TFTP */
122IPaddr_t Mcast_addr;
123#endif
124
wdenk2d966952002-10-31 22:12:35 +0000125/** END OF BOOTP EXTENTIONS **/
126
127ulong NetBootFileXferSize; /* The actual transferred size of the bootfile (in bytes) */
128uchar NetOurEther[6]; /* Our ethernet address */
129uchar NetServerEther[6] = /* Boot server enet address */
wdenke6466f62003-06-05 19:27:42 +0000130 { 0, 0, 0, 0, 0, 0 };
wdenk2d966952002-10-31 22:12:35 +0000131IPaddr_t NetOurIP; /* Our IP addr (0 = unknown) */
132IPaddr_t NetServerIP; /* Our IP addr (0 = unknown) */
133volatile uchar *NetRxPkt; /* Current receive packet */
134int NetRxPktLen; /* Current rx packet length */
135unsigned NetIPID; /* IP packet ID */
136uchar NetBcastAddr[6] = /* Ethernet bcast address */
137 { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
wdenke6466f62003-06-05 19:27:42 +0000138uchar NetEtherNullAddr[6] =
139 { 0, 0, 0, 0, 0, 0 };
Rafal Jaworowski1f2c9a42007-12-27 18:19:02 +0100140#ifdef CONFIG_API
141void (*push_packet)(volatile void *, int len) = 0;
142#endif
Jon Loeliger54f35c22007-07-09 17:45:14 -0500143#if defined(CONFIG_CMD_CDP)
wdenk05939202004-04-18 17:39:38 +0000144uchar NetCDPAddr[6] = /* Ethernet bcast address */
wdenk145d2c12004-04-15 21:48:45 +0000145 { 0x01, 0x00, 0x0c, 0xcc, 0xcc, 0xcc };
146#endif
wdenk2d966952002-10-31 22:12:35 +0000147int NetState; /* Network loop state */
148#ifdef CONFIG_NET_MULTI
149int NetRestartWrap = 0; /* Tried all network devices */
150static int NetRestarted = 0; /* Network loop restarted */
151static int NetDevExists = 0; /* At least one device configured */
152#endif
153
wdenk05939202004-04-18 17:39:38 +0000154/* XXX in both little & big endian machines 0xFFFF == ntohs(-1) */
155ushort NetOurVLAN = 0xFFFF; /* default is without VLAN */
156ushort NetOurNativeVLAN = 0xFFFF; /* ditto */
wdenk145d2c12004-04-15 21:48:45 +0000157
wdenk2d966952002-10-31 22:12:35 +0000158char BootFile[128]; /* Boot File name */
159
Jon Loeliger54f35c22007-07-09 17:45:14 -0500160#if defined(CONFIG_CMD_PING)
wdenke6466f62003-06-05 19:27:42 +0000161IPaddr_t NetPingIP; /* the ip address to ping */
162
163static void PingStart(void);
164#endif
165
Jon Loeliger54f35c22007-07-09 17:45:14 -0500166#if defined(CONFIG_CMD_CDP)
wdenk145d2c12004-04-15 21:48:45 +0000167static void CDPStart(void);
168#endif
169
Jon Loeliger54f35c22007-07-09 17:45:14 -0500170#if defined(CONFIG_CMD_SNTP)
wdenkb4ad9622005-04-01 00:25:43 +0000171IPaddr_t NetNtpServerIP; /* NTP server IP address */
172int NetTimeOffset=0; /* offset time from UTC */
173#endif
174
wdenkb8fb6192004-08-02 21:11:11 +0000175#ifdef CONFIG_NETCONSOLE
176void NcStart(void);
177int nc_input_packet(uchar *pkt, unsigned dest, unsigned src, unsigned len);
178#endif
179
wdenk2d966952002-10-31 22:12:35 +0000180volatile uchar PktBuf[(PKTBUFSRX+1) * PKTSIZE_ALIGN + PKTALIGN];
181
182volatile uchar *NetRxPackets[PKTBUFSRX]; /* Receive packets */
183
184static rxhand_f *packetHandler; /* Current RX packet handler */
185static thand_f *timeHandler; /* Current timeout handler */
wdenk5d841732003-08-17 18:55:18 +0000186static ulong timeStart; /* Time base value */
187static ulong timeDelta; /* Current timeout value */
wdenk2d966952002-10-31 22:12:35 +0000188volatile uchar *NetTxPacket = 0; /* THE transmit packet */
189
190static int net_check_prereq (proto_t protocol);
191
192/**********************************************************************/
wdenke6466f62003-06-05 19:27:42 +0000193
194IPaddr_t NetArpWaitPacketIP;
195IPaddr_t NetArpWaitReplyIP;
196uchar *NetArpWaitPacketMAC; /* MAC address of waiting packet's destination */
Wolfgang Denk30b87322005-08-12 23:43:12 +0200197uchar *NetArpWaitTxPacket; /* THE transmit packet */
wdenke6466f62003-06-05 19:27:42 +0000198int NetArpWaitTxPacketSize;
199uchar NetArpWaitPacketBuf[PKTSIZE_ALIGN + PKTALIGN];
200ulong NetArpWaitTimerStart;
201int NetArpWaitTry;
202
wdenk05939202004-04-18 17:39:38 +0000203void ArpRequest (void)
wdenke6466f62003-06-05 19:27:42 +0000204{
205 int i;
206 volatile uchar *pkt;
wdenk05939202004-04-18 17:39:38 +0000207 ARP_t *arp;
wdenke6466f62003-06-05 19:27:42 +0000208
209#ifdef ET_DEBUG
wdenk05939202004-04-18 17:39:38 +0000210 printf ("ARP broadcast %d\n", NetArpWaitTry);
wdenke6466f62003-06-05 19:27:42 +0000211#endif
212 pkt = NetTxPacket;
213
wdenk05939202004-04-18 17:39:38 +0000214 pkt += NetSetEther (pkt, NetBcastAddr, PROT_ARP);
wdenke6466f62003-06-05 19:27:42 +0000215
wdenk05939202004-04-18 17:39:38 +0000216 arp = (ARP_t *) pkt;
wdenke6466f62003-06-05 19:27:42 +0000217
wdenk05939202004-04-18 17:39:38 +0000218 arp->ar_hrd = htons (ARP_ETHER);
219 arp->ar_pro = htons (PROT_IP);
wdenke6466f62003-06-05 19:27:42 +0000220 arp->ar_hln = 6;
221 arp->ar_pln = 4;
wdenk05939202004-04-18 17:39:38 +0000222 arp->ar_op = htons (ARPOP_REQUEST);
wdenke6466f62003-06-05 19:27:42 +0000223
Wolfgang Denk30b87322005-08-12 23:43:12 +0200224 memcpy (&arp->ar_data[0], NetOurEther, 6); /* source ET addr */
225 NetWriteIP ((uchar *) & arp->ar_data[6], NetOurIP); /* source IP addr */
wdenk05939202004-04-18 17:39:38 +0000226 for (i = 10; i < 16; ++i) {
227 arp->ar_data[i] = 0; /* dest ET addr = 0 */
wdenke6466f62003-06-05 19:27:42 +0000228 }
229
wdenk05939202004-04-18 17:39:38 +0000230 if ((NetArpWaitPacketIP & NetOurSubnetMask) !=
231 (NetOurIP & NetOurSubnetMask)) {
232 if (NetOurGatewayIP == 0) {
233 puts ("## Warning: gatewayip needed but not set\n");
Wolfgang Denk98ceffb2006-03-12 01:13:30 +0100234 NetArpWaitReplyIP = NetArpWaitPacketIP;
235 } else {
236 NetArpWaitReplyIP = NetOurGatewayIP;
wdenk05939202004-04-18 17:39:38 +0000237 }
wdenk05939202004-04-18 17:39:38 +0000238 } else {
239 NetArpWaitReplyIP = NetArpWaitPacketIP;
240 }
wdenke6466f62003-06-05 19:27:42 +0000241
wdenk05939202004-04-18 17:39:38 +0000242 NetWriteIP ((uchar *) & arp->ar_data[16], NetArpWaitReplyIP);
243 (void) eth_send (NetTxPacket, (pkt - NetTxPacket) + ARP_HDR_SIZE);
wdenke6466f62003-06-05 19:27:42 +0000244}
245
246void ArpTimeoutCheck(void)
247{
248 ulong t;
249
250 if (!NetArpWaitPacketIP)
251 return;
252
253 t = get_timer(0);
254
255 /* check for arp timeout */
256 if ((t - NetArpWaitTimerStart) > ARP_TIMEOUT * CFG_HZ) {
257 NetArpWaitTry++;
258
259 if (NetArpWaitTry >= ARP_TIMEOUT_COUNT) {
260 puts ("\nARP Retry count exceeded; starting again\n");
261 NetArpWaitTry = 0;
262 NetStartAgain();
263 } else {
264 NetArpWaitTimerStart = t;
265 ArpRequest();
266 }
267 }
268}
269
270/**********************************************************************/
wdenk2d966952002-10-31 22:12:35 +0000271/*
272 * Main network processing loop.
273 */
274
275int
276NetLoop(proto_t protocol)
277{
wdenk2d966952002-10-31 22:12:35 +0000278 bd_t *bd = gd->bd;
279
280#ifdef CONFIG_NET_MULTI
281 NetRestarted = 0;
282 NetDevExists = 0;
283#endif
284
wdenke6466f62003-06-05 19:27:42 +0000285 /* XXX problem with bss workaround */
286 NetArpWaitPacketMAC = NULL;
287 NetArpWaitTxPacket = NULL;
288 NetArpWaitPacketIP = 0;
289 NetArpWaitReplyIP = 0;
290 NetArpWaitTxPacket = NULL;
291 NetTxPacket = NULL;
292
wdenk2d966952002-10-31 22:12:35 +0000293 if (!NetTxPacket) {
294 int i;
wdenk2d966952002-10-31 22:12:35 +0000295 /*
296 * Setup packet buffers, aligned correctly.
297 */
298 NetTxPacket = &PktBuf[0] + (PKTALIGN - 1);
299 NetTxPacket -= (ulong)NetTxPacket % PKTALIGN;
300 for (i = 0; i < PKTBUFSRX; i++) {
301 NetRxPackets[i] = NetTxPacket + (i+1)*PKTSIZE_ALIGN;
302 }
wdenke6466f62003-06-05 19:27:42 +0000303 }
304
305 if (!NetArpWaitTxPacket) {
306 NetArpWaitTxPacket = &NetArpWaitPacketBuf[0] + (PKTALIGN - 1);
307 NetArpWaitTxPacket -= (ulong)NetArpWaitTxPacket % PKTALIGN;
308 NetArpWaitTxPacketSize = 0;
wdenk2d966952002-10-31 22:12:35 +0000309 }
310
311 eth_halt();
wdenk05939202004-04-18 17:39:38 +0000312#ifdef CONFIG_NET_MULTI
wdenk145d2c12004-04-15 21:48:45 +0000313 eth_set_current();
wdenk05939202004-04-18 17:39:38 +0000314#endif
wdenkfa66e932005-04-03 14:52:59 +0000315 if (eth_init(bd) < 0) {
316 eth_halt();
wdenk05939202004-04-18 17:39:38 +0000317 return(-1);
wdenkfa66e932005-04-03 14:52:59 +0000318 }
wdenk2d966952002-10-31 22:12:35 +0000319
320restart:
321#ifdef CONFIG_NET_MULTI
322 memcpy (NetOurEther, eth_get_dev()->enetaddr, 6);
323#else
324 memcpy (NetOurEther, bd->bi_enetaddr, 6);
325#endif
326
327 NetState = NETLOOP_CONTINUE;
328
329 /*
330 * Start the ball rolling with the given start function. From
331 * here on, this code is a state machine driven by received
332 * packets and timer events.
333 */
334
335 switch (protocol) {
Jon Loeliger54f35c22007-07-09 17:45:14 -0500336#if defined(CONFIG_CMD_NFS)
wdenkbe9c1cb2004-02-24 02:00:03 +0000337 case NFS:
338#endif
Jon Loeliger54f35c22007-07-09 17:45:14 -0500339#if defined(CONFIG_CMD_PING)
wdenke6466f62003-06-05 19:27:42 +0000340 case PING:
341#endif
Jon Loeliger54f35c22007-07-09 17:45:14 -0500342#if defined(CONFIG_CMD_SNTP)
wdenkb4ad9622005-04-01 00:25:43 +0000343 case SNTP:
344#endif
wdenkb8fb6192004-08-02 21:11:11 +0000345 case NETCONS:
wdenk2d966952002-10-31 22:12:35 +0000346 case TFTP:
347 NetCopyIP(&NetOurIP, &bd->bi_ip_addr);
wdenk2d966952002-10-31 22:12:35 +0000348 NetOurGatewayIP = getenv_IPaddr ("gatewayip");
349 NetOurSubnetMask= getenv_IPaddr ("netmask");
wdenk145d2c12004-04-15 21:48:45 +0000350 NetOurVLAN = getenv_VLAN("vlan");
351 NetOurNativeVLAN = getenv_VLAN("nvlan");
wdenke6466f62003-06-05 19:27:42 +0000352
353 switch (protocol) {
Jon Loeliger54f35c22007-07-09 17:45:14 -0500354#if defined(CONFIG_CMD_NFS)
wdenkbe9c1cb2004-02-24 02:00:03 +0000355 case NFS:
356#endif
wdenkb8fb6192004-08-02 21:11:11 +0000357 case NETCONS:
wdenke6466f62003-06-05 19:27:42 +0000358 case TFTP:
359 NetServerIP = getenv_IPaddr ("serverip");
360 break;
Jon Loeliger54f35c22007-07-09 17:45:14 -0500361#if defined(CONFIG_CMD_PING)
wdenke6466f62003-06-05 19:27:42 +0000362 case PING:
363 /* nothing */
364 break;
365#endif
Jon Loeliger54f35c22007-07-09 17:45:14 -0500366#if defined(CONFIG_CMD_SNTP)
wdenkb4ad9622005-04-01 00:25:43 +0000367 case SNTP:
368 /* nothing */
369 break;
370#endif
wdenke6466f62003-06-05 19:27:42 +0000371 default:
372 break;
373 }
374
wdenk2d966952002-10-31 22:12:35 +0000375 break;
376 case BOOTP:
377 case RARP:
378 /*
wdenk57b2d802003-06-27 21:31:46 +0000379 * initialize our IP addr to 0 in order to accept ANY
380 * IP addr assigned to us by the BOOTP / RARP server
wdenk2d966952002-10-31 22:12:35 +0000381 */
382 NetOurIP = 0;
wdenk4989f872004-03-14 15:06:13 +0000383 NetServerIP = getenv_IPaddr ("serverip");
Wolfgang Denk30b87322005-08-12 23:43:12 +0200384 NetOurVLAN = getenv_VLAN("vlan"); /* VLANs must be read */
385 NetOurNativeVLAN = getenv_VLAN("nvlan");
386 case CDP:
387 NetOurVLAN = getenv_VLAN("vlan"); /* VLANs must be read */
388 NetOurNativeVLAN = getenv_VLAN("nvlan");
wdenk2d966952002-10-31 22:12:35 +0000389 break;
390 default:
391 break;
392 }
393
394 switch (net_check_prereq (protocol)) {
395 case 1:
396 /* network not configured */
wdenkfa66e932005-04-03 14:52:59 +0000397 eth_halt();
wdenk2582f6b2002-11-11 21:14:20 +0000398 return (-1);
wdenk2d966952002-10-31 22:12:35 +0000399
400#ifdef CONFIG_NET_MULTI
401 case 2:
402 /* network device not configured */
403 break;
404#endif /* CONFIG_NET_MULTI */
405
406 case 0:
407#ifdef CONFIG_NET_MULTI
408 NetDevExists = 1;
409#endif
410 switch (protocol) {
411 case TFTP:
412 /* always use ARP to get server ethernet address */
wdenke6466f62003-06-05 19:27:42 +0000413 TftpStart();
wdenk2d966952002-10-31 22:12:35 +0000414 break;
415
Jon Loeliger54f35c22007-07-09 17:45:14 -0500416#if defined(CONFIG_CMD_DHCP)
wdenk2d966952002-10-31 22:12:35 +0000417 case DHCP:
418 /* Start with a clean slate... */
wdenkc3919532004-10-11 22:51:13 +0000419 BootpTry = 0;
wdenk2d966952002-10-31 22:12:35 +0000420 NetOurIP = 0;
wdenk4989f872004-03-14 15:06:13 +0000421 NetServerIP = getenv_IPaddr ("serverip");
wdenk2d966952002-10-31 22:12:35 +0000422 DhcpRequest(); /* Basically same as BOOTP */
423 break;
Jon Loeligera9807e52007-07-10 11:05:02 -0500424#endif
wdenk2d966952002-10-31 22:12:35 +0000425
426 case BOOTP:
427 BootpTry = 0;
428 BootpRequest ();
429 break;
430
431 case RARP:
432 RarpTry = 0;
433 RarpRequest ();
434 break;
Jon Loeliger54f35c22007-07-09 17:45:14 -0500435#if defined(CONFIG_CMD_PING)
wdenke6466f62003-06-05 19:27:42 +0000436 case PING:
437 PingStart();
438 break;
439#endif
Jon Loeliger54f35c22007-07-09 17:45:14 -0500440#if defined(CONFIG_CMD_NFS)
wdenkbe9c1cb2004-02-24 02:00:03 +0000441 case NFS:
442 NfsStart();
443 break;
444#endif
Jon Loeliger54f35c22007-07-09 17:45:14 -0500445#if defined(CONFIG_CMD_CDP)
wdenk145d2c12004-04-15 21:48:45 +0000446 case CDP:
447 CDPStart();
448 break;
449#endif
wdenkb8fb6192004-08-02 21:11:11 +0000450#ifdef CONFIG_NETCONSOLE
451 case NETCONS:
452 NcStart();
453 break;
454#endif
Jon Loeliger54f35c22007-07-09 17:45:14 -0500455#if defined(CONFIG_CMD_SNTP)
wdenkb4ad9622005-04-01 00:25:43 +0000456 case SNTP:
457 SntpStart();
458 break;
459#endif
wdenk2d966952002-10-31 22:12:35 +0000460 default:
461 break;
462 }
463
464 NetBootFileXferSize = 0;
465 break;
466 }
467
Jon Loeliger54f35c22007-07-09 17:45:14 -0500468#if defined(CONFIG_MII) || defined(CONFIG_CMD_MII)
wdenk49c3f672003-10-08 22:33:00 +0000469#if defined(CFG_FAULT_ECHO_LINK_DOWN) && defined(CONFIG_STATUS_LED) && defined(STATUS_LED_RED)
470 /*
wdenk9c53f402003-10-15 23:53:47 +0000471 * Echo the inverted link state to the fault LED.
wdenk49c3f672003-10-08 22:33:00 +0000472 */
Marian Balakowiczaab8c492005-10-28 22:30:33 +0200473 if(miiphy_link(eth_get_dev()->name, CFG_FAULT_MII_ADDR)) {
wdenk49c3f672003-10-08 22:33:00 +0000474 status_led_set (STATUS_LED_RED, STATUS_LED_OFF);
475 } else {
476 status_led_set (STATUS_LED_RED, STATUS_LED_ON);
477 }
478#endif /* CFG_FAULT_ECHO_LINK_DOWN, ... */
479#endif /* CONFIG_MII, ... */
wdenk2d966952002-10-31 22:12:35 +0000480
481 /*
482 * Main packet reception loop. Loop receiving packets until
wdenk27fa5852005-04-03 14:18:51 +0000483 * someone sets `NetState' to a state that terminates.
wdenk2d966952002-10-31 22:12:35 +0000484 */
485 for (;;) {
486 WATCHDOG_RESET();
487#ifdef CONFIG_SHOW_ACTIVITY
488 {
489 extern void show_activity(int arg);
490 show_activity(1);
491 }
492#endif
493 /*
494 * Check the ethernet for a new packet. The ethernet
495 * receive routine will process it.
496 */
497 eth_rx();
498
499 /*
500 * Abort if ctrl-c was pressed.
501 */
502 if (ctrlc()) {
wdenk57b2d802003-06-27 21:31:46 +0000503 eth_halt();
wdenk42c05472004-03-23 22:14:11 +0000504 puts ("\nAbort\n");
wdenk2582f6b2002-11-11 21:14:20 +0000505 return (-1);
wdenk2d966952002-10-31 22:12:35 +0000506 }
507
wdenke6466f62003-06-05 19:27:42 +0000508 ArpTimeoutCheck();
wdenk2d966952002-10-31 22:12:35 +0000509
510 /*
511 * Check for a timeout, and run the timeout handler
512 * if we have one.
513 */
wdenk5d841732003-08-17 18:55:18 +0000514 if (timeHandler && ((get_timer(0) - timeStart) > timeDelta)) {
wdenk2d966952002-10-31 22:12:35 +0000515 thand_f *x;
516
Jon Loeliger54f35c22007-07-09 17:45:14 -0500517#if defined(CONFIG_MII) || defined(CONFIG_CMD_MII)
wdenk6148e742005-04-03 20:55:38 +0000518# if defined(CFG_FAULT_ECHO_LINK_DOWN) && \
519 defined(CONFIG_STATUS_LED) && \
wdenk27fa5852005-04-03 14:18:51 +0000520 defined(STATUS_LED_RED)
wdenk49c3f672003-10-08 22:33:00 +0000521 /*
wdenk9c53f402003-10-15 23:53:47 +0000522 * Echo the inverted link state to the fault LED.
wdenk49c3f672003-10-08 22:33:00 +0000523 */
Marian Balakowiczaab8c492005-10-28 22:30:33 +0200524 if(miiphy_link(eth_get_dev()->name, CFG_FAULT_MII_ADDR)) {
wdenk49c3f672003-10-08 22:33:00 +0000525 status_led_set (STATUS_LED_RED, STATUS_LED_OFF);
526 } else {
527 status_led_set (STATUS_LED_RED, STATUS_LED_ON);
528 }
wdenk27fa5852005-04-03 14:18:51 +0000529# endif /* CFG_FAULT_ECHO_LINK_DOWN, ... */
wdenk49c3f672003-10-08 22:33:00 +0000530#endif /* CONFIG_MII, ... */
wdenk2d966952002-10-31 22:12:35 +0000531 x = timeHandler;
532 timeHandler = (thand_f *)0;
533 (*x)();
534 }
535
536
537 switch (NetState) {
538
539 case NETLOOP_RESTART:
540#ifdef CONFIG_NET_MULTI
541 NetRestarted = 1;
542#endif
543 goto restart;
544
545 case NETLOOP_SUCCESS:
546 if (NetBootFileXferSize > 0) {
Wolfgang Denk084d0ce2007-09-12 00:48:57 +0200547 char buf[20];
wdenk2d966952002-10-31 22:12:35 +0000548 printf("Bytes transferred = %ld (%lx hex)\n",
549 NetBootFileXferSize,
550 NetBootFileXferSize);
Wolfgang Denk084d0ce2007-09-12 00:48:57 +0200551 sprintf(buf, "%lX", NetBootFileXferSize);
wdenk2d966952002-10-31 22:12:35 +0000552 setenv("filesize", buf);
wdenk145d2c12004-04-15 21:48:45 +0000553
554 sprintf(buf, "%lX", (unsigned long)load_addr);
555 setenv("fileaddr", buf);
wdenk2d966952002-10-31 22:12:35 +0000556 }
557 eth_halt();
558 return NetBootFileXferSize;
559
560 case NETLOOP_FAIL:
wdenk2582f6b2002-11-11 21:14:20 +0000561 return (-1);
wdenk2d966952002-10-31 22:12:35 +0000562 }
563 }
564}
565
566/**********************************************************************/
567
568static void
569startAgainTimeout(void)
570{
571 NetState = NETLOOP_RESTART;
572}
573
574static void
575startAgainHandler(uchar * pkt, unsigned dest, unsigned src, unsigned len)
576{
577 /* Totally ignore the packet */
578}
579
wdenk05939202004-04-18 17:39:38 +0000580void NetStartAgain (void)
wdenk2d966952002-10-31 22:12:35 +0000581{
wdenk05939202004-04-18 17:39:38 +0000582 char *nretry;
583 int noretry = 0, once = 0;
wdenk145d2c12004-04-15 21:48:45 +0000584
wdenk05939202004-04-18 17:39:38 +0000585 if ((nretry = getenv ("netretry")) != NULL) {
586 noretry = (strcmp (nretry, "no") == 0);
587 once = (strcmp (nretry, "once") == 0);
588 }
589 if (noretry) {
590 eth_halt ();
wdenk145d2c12004-04-15 21:48:45 +0000591 NetState = NETLOOP_FAIL;
592 return;
593 }
wdenk2d966952002-10-31 22:12:35 +0000594#ifndef CONFIG_NET_MULTI
Jean-Christophe PLAGNIOL-VILLARDc1a61642007-11-17 11:31:10 +0100595 NetSetTimeout (10UL * CFG_HZ, startAgainTimeout);
wdenk05939202004-04-18 17:39:38 +0000596 NetSetHandler (startAgainHandler);
597#else /* !CONFIG_NET_MULTI*/
598 eth_halt ();
599 eth_try_another (!NetRestarted);
600 eth_init (gd->bd);
601 if (NetRestartWrap) {
wdenk2d966952002-10-31 22:12:35 +0000602 NetRestartWrap = 0;
wdenk05939202004-04-18 17:39:38 +0000603 if (NetDevExists && !once) {
Jean-Christophe PLAGNIOL-VILLARDc1a61642007-11-17 11:31:10 +0100604 NetSetTimeout (10UL * CFG_HZ, startAgainTimeout);
wdenk05939202004-04-18 17:39:38 +0000605 NetSetHandler (startAgainHandler);
606 } else {
wdenk2d966952002-10-31 22:12:35 +0000607 NetState = NETLOOP_FAIL;
608 }
wdenk05939202004-04-18 17:39:38 +0000609 } else {
wdenk2d966952002-10-31 22:12:35 +0000610 NetState = NETLOOP_RESTART;
611 }
wdenk05939202004-04-18 17:39:38 +0000612#endif /* CONFIG_NET_MULTI */
wdenk2d966952002-10-31 22:12:35 +0000613}
614
615/**********************************************************************/
616/*
617 * Miscelaneous bits.
618 */
619
620void
621NetSetHandler(rxhand_f * f)
622{
623 packetHandler = f;
624}
625
626
627void
wdenkd6a3dd42004-10-09 21:56:21 +0000628NetSetTimeout(ulong iv, thand_f * f)
wdenk2d966952002-10-31 22:12:35 +0000629{
630 if (iv == 0) {
631 timeHandler = (thand_f *)0;
632 } else {
633 timeHandler = f;
wdenk5d841732003-08-17 18:55:18 +0000634 timeStart = get_timer(0);
635 timeDelta = iv;
wdenk2d966952002-10-31 22:12:35 +0000636 }
637}
638
639
640void
641NetSendPacket(volatile uchar * pkt, int len)
642{
643 (void) eth_send(pkt, len);
644}
645
wdenke6466f62003-06-05 19:27:42 +0000646int
647NetSendUDPPacket(uchar *ether, IPaddr_t dest, int dport, int sport, int len)
648{
wdenk145d2c12004-04-15 21:48:45 +0000649 uchar *pkt;
650
wdenke6466f62003-06-05 19:27:42 +0000651 /* convert to new style broadcast */
652 if (dest == 0)
653 dest = 0xFFFFFFFF;
654
655 /* if broadcast, make the ether address a broadcast and don't do ARP */
656 if (dest == 0xFFFFFFFF)
657 ether = NetBcastAddr;
658
659 /* if MAC address was not discovered yet, save the packet and do an ARP request */
660 if (memcmp(ether, NetEtherNullAddr, 6) == 0) {
661
662#ifdef ET_DEBUG
663 printf("sending ARP for %08lx\n", dest);
664#endif
wdenke6466f62003-06-05 19:27:42 +0000665 NetArpWaitPacketIP = dest;
666 NetArpWaitPacketMAC = ether;
wdenk145d2c12004-04-15 21:48:45 +0000667
668 pkt = NetArpWaitTxPacket;
669 pkt += NetSetEther (pkt, NetArpWaitPacketMAC, PROT_IP);
670
671 NetSetIP (pkt, dest, dport, sport, len);
672 memcpy(pkt + IP_HDR_SIZE, (uchar *)NetTxPacket + (pkt - (uchar *)NetArpWaitTxPacket) + IP_HDR_SIZE, len);
wdenke6466f62003-06-05 19:27:42 +0000673
674 /* size of the waiting packet */
wdenk145d2c12004-04-15 21:48:45 +0000675 NetArpWaitTxPacketSize = (pkt - NetArpWaitTxPacket) + IP_HDR_SIZE + len;
wdenke6466f62003-06-05 19:27:42 +0000676
677 /* and do the ARP request */
678 NetArpWaitTry = 1;
679 NetArpWaitTimerStart = get_timer(0);
680 ArpRequest();
681 return 1; /* waiting */
682 }
683
684#ifdef ET_DEBUG
685 printf("sending UDP to %08lx/%02x:%02x:%02x:%02x:%02x:%02x\n",
wdenk05939202004-04-18 17:39:38 +0000686 dest, ether[0], ether[1], ether[2], ether[3], ether[4], ether[5]);
wdenke6466f62003-06-05 19:27:42 +0000687#endif
688
wdenk145d2c12004-04-15 21:48:45 +0000689 pkt = (uchar *)NetTxPacket;
690 pkt += NetSetEther (pkt, ether, PROT_IP);
691 NetSetIP (pkt, dest, dport, sport, len);
692 (void) eth_send(NetTxPacket, (pkt - NetTxPacket) + IP_HDR_SIZE + len);
wdenke6466f62003-06-05 19:27:42 +0000693
wdenk05939202004-04-18 17:39:38 +0000694 return 0; /* transmitted */
wdenke6466f62003-06-05 19:27:42 +0000695}
696
Jon Loeliger54f35c22007-07-09 17:45:14 -0500697#if defined(CONFIG_CMD_PING)
wdenke6466f62003-06-05 19:27:42 +0000698static ushort PingSeqNo;
699
700int PingSend(void)
701{
702 static uchar mac[6];
703 volatile IP_t *ip;
704 volatile ushort *s;
wdenk145d2c12004-04-15 21:48:45 +0000705 uchar *pkt;
wdenke6466f62003-06-05 19:27:42 +0000706
707 /* XXX always send arp request */
708
709 memcpy(mac, NetEtherNullAddr, 6);
710
711#ifdef ET_DEBUG
712 printf("sending ARP for %08lx\n", NetPingIP);
713#endif
714
715 NetArpWaitPacketIP = NetPingIP;
716 NetArpWaitPacketMAC = mac;
717
wdenk145d2c12004-04-15 21:48:45 +0000718 pkt = NetArpWaitTxPacket;
719 pkt += NetSetEther(pkt, mac, PROT_IP);
wdenke6466f62003-06-05 19:27:42 +0000720
wdenk145d2c12004-04-15 21:48:45 +0000721 ip = (volatile IP_t *)pkt;
wdenke6466f62003-06-05 19:27:42 +0000722
723 /*
724 * Construct an IP and ICMP header. (need to set no fragment bit - XXX)
725 */
726 ip->ip_hl_v = 0x45; /* IP_HDR_SIZE / 4 (not including UDP) */
727 ip->ip_tos = 0;
728 ip->ip_len = htons(IP_HDR_SIZE_NO_UDP + 8);
729 ip->ip_id = htons(NetIPID++);
730 ip->ip_off = htons(0x4000); /* No fragmentation */
731 ip->ip_ttl = 255;
732 ip->ip_p = 0x01; /* ICMP */
733 ip->ip_sum = 0;
734 NetCopyIP((void*)&ip->ip_src, &NetOurIP); /* already in network byte order */
735 NetCopyIP((void*)&ip->ip_dst, &NetPingIP); /* - "" - */
736 ip->ip_sum = ~NetCksum((uchar *)ip, IP_HDR_SIZE_NO_UDP / 2);
737
738 s = &ip->udp_src; /* XXX ICMP starts here */
739 s[0] = htons(0x0800); /* echo-request, code */
740 s[1] = 0; /* checksum */
741 s[2] = 0; /* identifier */
742 s[3] = htons(PingSeqNo++); /* sequence number */
743 s[1] = ~NetCksum((uchar *)s, 8/2);
744
745 /* size of the waiting packet */
wdenk145d2c12004-04-15 21:48:45 +0000746 NetArpWaitTxPacketSize = (pkt - NetArpWaitTxPacket) + IP_HDR_SIZE_NO_UDP + 8;
wdenke6466f62003-06-05 19:27:42 +0000747
748 /* and do the ARP request */
749 NetArpWaitTry = 1;
750 NetArpWaitTimerStart = get_timer(0);
751 ArpRequest();
752 return 1; /* waiting */
753}
754
755static void
756PingTimeout (void)
757{
758 eth_halt();
759 NetState = NETLOOP_FAIL; /* we did not get the reply */
760}
761
762static void
763PingHandler (uchar * pkt, unsigned dest, unsigned src, unsigned len)
764{
765 IPaddr_t tmp;
766 volatile IP_t *ip = (volatile IP_t *)pkt;
767
768 tmp = NetReadIP((void *)&ip->ip_src);
769 if (tmp != NetPingIP)
770 return;
771
772 NetState = NETLOOP_SUCCESS;
773}
774
775static void PingStart(void)
776{
wdenk145d2c12004-04-15 21:48:45 +0000777#if defined(CONFIG_NET_MULTI)
778 printf ("Using %s device\n", eth_get_name());
wdenk05939202004-04-18 17:39:38 +0000779#endif /* CONFIG_NET_MULTI */
Jean-Christophe PLAGNIOL-VILLARDc1a61642007-11-17 11:31:10 +0100780 NetSetTimeout (10UL * CFG_HZ, PingTimeout);
wdenke6466f62003-06-05 19:27:42 +0000781 NetSetHandler (PingHandler);
782
783 PingSend();
784}
Jon Loeligera9807e52007-07-10 11:05:02 -0500785#endif
wdenk145d2c12004-04-15 21:48:45 +0000786
Jon Loeliger54f35c22007-07-09 17:45:14 -0500787#if defined(CONFIG_CMD_CDP)
wdenk145d2c12004-04-15 21:48:45 +0000788
789#define CDP_DEVICE_ID_TLV 0x0001
790#define CDP_ADDRESS_TLV 0x0002
791#define CDP_PORT_ID_TLV 0x0003
792#define CDP_CAPABILITIES_TLV 0x0004
793#define CDP_VERSION_TLV 0x0005
794#define CDP_PLATFORM_TLV 0x0006
795#define CDP_NATIVE_VLAN_TLV 0x000a
796#define CDP_APPLIANCE_VLAN_TLV 0x000e
797#define CDP_TRIGGER_TLV 0x000f
798#define CDP_POWER_CONSUMPTION_TLV 0x0010
799#define CDP_SYSNAME_TLV 0x0014
800#define CDP_SYSOBJECT_TLV 0x0015
801#define CDP_MANAGEMENT_ADDRESS_TLV 0x0016
802
803#define CDP_TIMEOUT (CFG_HZ/4) /* one packet every 250ms */
804
805static int CDPSeq;
806static int CDPOK;
807
808ushort CDPNativeVLAN;
809ushort CDPApplianceVLAN;
810
811static const uchar CDP_SNAP_hdr[8] = { 0xAA, 0xAA, 0x03, 0x00, 0x00, 0x0C, 0x20, 0x00 };
812
813static ushort CDP_compute_csum(const uchar *buff, ushort len)
814{
815 ushort csum;
816 int odd;
817 ulong result = 0;
818 ushort leftover;
Wolfgang Denk7fb52662005-10-13 16:45:02 +0200819 ushort *p;
wdenk145d2c12004-04-15 21:48:45 +0000820
821 if (len > 0) {
822 odd = 1 & (ulong)buff;
823 if (odd) {
824 result = *buff << 8;
825 len--;
826 buff++;
827 }
828 while (len > 1) {
Wolfgang Denk7fb52662005-10-13 16:45:02 +0200829 p = (ushort *)buff;
830 result += *p++;
831 buff = (uchar *)p;
wdenk145d2c12004-04-15 21:48:45 +0000832 if (result & 0x80000000)
833 result = (result & 0xFFFF) + (result >> 16);
834 len -= 2;
835 }
836 if (len) {
837 leftover = (signed short)(*(const signed char *)buff);
Wolfgang Denkf0711132005-11-10 20:59:46 +0100838 /* CISCO SUCKS big time! (and blows too):
839 * CDP uses the IP checksum algorithm with a twist;
840 * for the last byte it *sign* extends and sums.
841 */
wdenk145d2c12004-04-15 21:48:45 +0000842 result = (result & 0xffff0000) | ((result + leftover) & 0x0000ffff);
843 }
844 while (result >> 16)
845 result = (result & 0xFFFF) + (result >> 16);
846
847 if (odd)
848 result = ((result >> 8) & 0xff) | ((result & 0xff) << 8);
849 }
850
851 /* add up 16-bit and 17-bit words for 17+c bits */
852 result = (result & 0xffff) + (result >> 16);
853 /* add up 16-bit and 2-bit for 16+c bit */
854 result = (result & 0xffff) + (result >> 16);
855 /* add up carry.. */
856 result = (result & 0xffff) + (result >> 16);
857
858 /* negate */
859 csum = ~(ushort)result;
860
861 /* run time endian detection */
862 if (csum != htons(csum)) /* little endian */
863 csum = htons(csum);
864
865 return csum;
866}
867
868int CDPSendTrigger(void)
869{
870 volatile uchar *pkt;
871 volatile ushort *s;
872 volatile ushort *cp;
873 Ethernet_t *et;
wdenk145d2c12004-04-15 21:48:45 +0000874 int len;
875 ushort chksum;
wdenk05939202004-04-18 17:39:38 +0000876#if defined(CONFIG_CDP_DEVICE_ID) || defined(CONFIG_CDP_PORT_ID) || \
877 defined(CONFIG_CDP_VERSION) || defined(CONFIG_CDP_PLATFORM)
878 char buf[32];
879#endif
wdenk145d2c12004-04-15 21:48:45 +0000880
881 pkt = NetTxPacket;
882 et = (Ethernet_t *)pkt;
883
884 /* NOTE: trigger sent not on any VLAN */
885
886 /* form ethernet header */
887 memcpy(et->et_dest, NetCDPAddr, 6);
888 memcpy(et->et_src, NetOurEther, 6);
889
890 pkt += ETHER_HDR_SIZE;
891
892 /* SNAP header */
893 memcpy((uchar *)pkt, CDP_SNAP_hdr, sizeof(CDP_SNAP_hdr));
894 pkt += sizeof(CDP_SNAP_hdr);
895
896 /* CDP header */
897 *pkt++ = 0x02; /* CDP version 2 */
898 *pkt++ = 180; /* TTL */
899 s = (volatile ushort *)pkt;
900 cp = s;
901 *s++ = htons(0); /* checksum (0 for later calculation) */
902
903 /* CDP fields */
904#ifdef CONFIG_CDP_DEVICE_ID
905 *s++ = htons(CDP_DEVICE_ID_TLV);
906 *s++ = htons(CONFIG_CDP_DEVICE_ID);
907 memset(buf, 0, sizeof(buf));
908 sprintf(buf, CONFIG_CDP_DEVICE_ID_PREFIX "%02X%02X%02X%02X%02X%02X",
909 NetOurEther[0] & 0xff, NetOurEther[1] & 0xff,
910 NetOurEther[2] & 0xff, NetOurEther[3] & 0xff,
911 NetOurEther[4] & 0xff, NetOurEther[5] & 0xff);
912 memcpy((uchar *)s, buf, 16);
913 s += 16 / 2;
914#endif
915
916#ifdef CONFIG_CDP_PORT_ID
917 *s++ = htons(CDP_PORT_ID_TLV);
918 memset(buf, 0, sizeof(buf));
919 sprintf(buf, CONFIG_CDP_PORT_ID, eth_get_dev_index());
920 len = strlen(buf);
921 if (len & 1) /* make it even */
922 len++;
923 *s++ = htons(len + 4);
924 memcpy((uchar *)s, buf, len);
925 s += len / 2;
926#endif
927
928#ifdef CONFIG_CDP_CAPABILITIES
929 *s++ = htons(CDP_CAPABILITIES_TLV);
930 *s++ = htons(8);
931 *(ulong *)s = htonl(CONFIG_CDP_CAPABILITIES);
932 s += 2;
933#endif
934
935#ifdef CONFIG_CDP_VERSION
936 *s++ = htons(CDP_VERSION_TLV);
937 memset(buf, 0, sizeof(buf));
938 strcpy(buf, CONFIG_CDP_VERSION);
939 len = strlen(buf);
940 if (len & 1) /* make it even */
941 len++;
942 *s++ = htons(len + 4);
943 memcpy((uchar *)s, buf, len);
944 s += len / 2;
945#endif
946
947#ifdef CONFIG_CDP_PLATFORM
948 *s++ = htons(CDP_PLATFORM_TLV);
949 memset(buf, 0, sizeof(buf));
950 strcpy(buf, CONFIG_CDP_PLATFORM);
951 len = strlen(buf);
952 if (len & 1) /* make it even */
953 len++;
954 *s++ = htons(len + 4);
955 memcpy((uchar *)s, buf, len);
956 s += len / 2;
957#endif
958
959#ifdef CONFIG_CDP_TRIGGER
960 *s++ = htons(CDP_TRIGGER_TLV);
961 *s++ = htons(8);
962 *(ulong *)s = htonl(CONFIG_CDP_TRIGGER);
963 s += 2;
964#endif
965
966#ifdef CONFIG_CDP_POWER_CONSUMPTION
967 *s++ = htons(CDP_POWER_CONSUMPTION_TLV);
968 *s++ = htons(6);
969 *s++ = htons(CONFIG_CDP_POWER_CONSUMPTION);
970#endif
971
972 /* length of ethernet packet */
973 len = (uchar *)s - ((uchar *)NetTxPacket + ETHER_HDR_SIZE);
974 et->et_protlen = htons(len);
975
976 len = ETHER_HDR_SIZE + sizeof(CDP_SNAP_hdr);
977 chksum = CDP_compute_csum((uchar *)NetTxPacket + len, (uchar *)s - (NetTxPacket + len));
978 if (chksum == 0)
979 chksum = 0xFFFF;
980 *cp = htons(chksum);
981
982 (void) eth_send(NetTxPacket, (uchar *)s - NetTxPacket);
983 return 0;
984}
985
986static void
987CDPTimeout (void)
988{
989 CDPSeq++;
990
991 if (CDPSeq < 3) {
992 NetSetTimeout (CDP_TIMEOUT, CDPTimeout);
993 CDPSendTrigger();
994 return;
995 }
996
997 /* if not OK try again */
998 if (!CDPOK)
999 NetStartAgain();
1000 else
1001 NetState = NETLOOP_SUCCESS;
1002}
1003
1004static void
1005CDPDummyHandler (uchar * pkt, unsigned dest, unsigned src, unsigned len)
1006{
1007 /* nothing */
1008}
1009
1010static void
1011CDPHandler(const uchar * pkt, unsigned len)
1012{
1013 const uchar *t;
1014 const ushort *ss;
1015 ushort type, tlen;
1016 uchar applid;
1017 ushort vlan, nvlan;
1018
1019 /* minimum size? */
1020 if (len < sizeof(CDP_SNAP_hdr) + 4)
1021 goto pkt_short;
1022
1023 /* check for valid CDP SNAP header */
1024 if (memcmp(pkt, CDP_SNAP_hdr, sizeof(CDP_SNAP_hdr)) != 0)
1025 return;
1026
1027 pkt += sizeof(CDP_SNAP_hdr);
1028 len -= sizeof(CDP_SNAP_hdr);
1029
1030 /* Version of CDP protocol must be >= 2 and TTL != 0 */
1031 if (pkt[0] < 0x02 || pkt[1] == 0)
1032 return;
1033
1034 /* if version is greater than 0x02 maybe we'll have a problem; output a warning */
1035 if (pkt[0] != 0x02)
1036 printf("** WARNING: CDP packet received with a protocol version %d > 2\n",
1037 pkt[0] & 0xff);
1038
1039 if (CDP_compute_csum(pkt, len) != 0)
1040 return;
1041
1042 pkt += 4;
1043 len -= 4;
1044
1045 vlan = htons(-1);
1046 nvlan = htons(-1);
1047 while (len > 0) {
1048 if (len < 4)
1049 goto pkt_short;
1050
1051 ss = (const ushort *)pkt;
1052 type = ntohs(ss[0]);
1053 tlen = ntohs(ss[1]);
1054 if (tlen > len) {
1055 goto pkt_short;
1056 }
1057
1058 pkt += tlen;
1059 len -= tlen;
1060
1061 ss += 2; /* point ss to the data of the TLV */
1062 tlen -= 4;
1063
1064 switch (type) {
1065 case CDP_DEVICE_ID_TLV:
1066 break;
1067 case CDP_ADDRESS_TLV:
1068 break;
1069 case CDP_PORT_ID_TLV:
1070 break;
1071 case CDP_CAPABILITIES_TLV:
1072 break;
1073 case CDP_VERSION_TLV:
1074 break;
1075 case CDP_PLATFORM_TLV:
1076 break;
1077 case CDP_NATIVE_VLAN_TLV:
1078 nvlan = *ss;
1079 break;
1080 case CDP_APPLIANCE_VLAN_TLV:
1081 t = (const uchar *)ss;
1082 while (tlen > 0) {
1083 if (tlen < 3)
1084 goto pkt_short;
1085
1086 applid = t[0];
1087 ss = (const ushort *)(t + 1);
1088
1089#ifdef CONFIG_CDP_APPLIANCE_VLAN_TYPE
1090 if (applid == CONFIG_CDP_APPLIANCE_VLAN_TYPE)
1091 vlan = *ss;
1092#else
1093 vlan = ntohs(*ss); /* XXX will this work; dunno */
1094#endif
1095 t += 3; tlen -= 3;
1096 }
1097 break;
1098 case CDP_TRIGGER_TLV:
1099 break;
1100 case CDP_POWER_CONSUMPTION_TLV:
1101 break;
1102 case CDP_SYSNAME_TLV:
1103 break;
1104 case CDP_SYSOBJECT_TLV:
1105 break;
1106 case CDP_MANAGEMENT_ADDRESS_TLV:
1107 break;
1108 }
1109 }
1110
1111 CDPApplianceVLAN = vlan;
1112 CDPNativeVLAN = nvlan;
1113
1114 CDPOK = 1;
1115 return;
1116
1117 pkt_short:
1118 printf("** CDP packet is too short\n");
1119 return;
1120}
1121
1122static void CDPStart(void)
1123{
1124#if defined(CONFIG_NET_MULTI)
1125 printf ("Using %s device\n", eth_get_name());
wdenke6466f62003-06-05 19:27:42 +00001126#endif
wdenk145d2c12004-04-15 21:48:45 +00001127 CDPSeq = 0;
1128 CDPOK = 0;
1129
1130 CDPNativeVLAN = htons(-1);
1131 CDPApplianceVLAN = htons(-1);
1132
1133 NetSetTimeout (CDP_TIMEOUT, CDPTimeout);
1134 NetSetHandler (CDPDummyHandler);
1135
1136 CDPSendTrigger();
1137}
Jon Loeligera9807e52007-07-10 11:05:02 -05001138#endif
wdenk145d2c12004-04-15 21:48:45 +00001139
wdenk2d966952002-10-31 22:12:35 +00001140
1141void
wdenk145d2c12004-04-15 21:48:45 +00001142NetReceive(volatile uchar * inpkt, int len)
wdenk2d966952002-10-31 22:12:35 +00001143{
1144 Ethernet_t *et;
1145 IP_t *ip;
1146 ARP_t *arp;
1147 IPaddr_t tmp;
1148 int x;
wdenk145d2c12004-04-15 21:48:45 +00001149 uchar *pkt;
Jon Loeliger54f35c22007-07-09 17:45:14 -05001150#if defined(CONFIG_CMD_CDP)
wdenk145d2c12004-04-15 21:48:45 +00001151 int iscdp;
1152#endif
1153 ushort cti = 0, vlanid = VLAN_NONE, myvlanid, mynvlanid;
wdenk2d966952002-10-31 22:12:35 +00001154
wdenk145d2c12004-04-15 21:48:45 +00001155#ifdef ET_DEBUG
1156 printf("packet received\n");
1157#endif
1158
1159 NetRxPkt = inpkt;
wdenk2d966952002-10-31 22:12:35 +00001160 NetRxPktLen = len;
wdenk145d2c12004-04-15 21:48:45 +00001161 et = (Ethernet_t *)inpkt;
1162
1163 /* too small packet? */
1164 if (len < ETHER_HDR_SIZE)
1165 return;
Rafal Jaworowski1f2c9a42007-12-27 18:19:02 +01001166
1167#ifdef CONFIG_API
1168 if (push_packet) {
1169 (*push_packet)(inpkt, len);
1170 return;
1171 }
1172#endif
wdenk145d2c12004-04-15 21:48:45 +00001173
Jon Loeliger54f35c22007-07-09 17:45:14 -05001174#if defined(CONFIG_CMD_CDP)
wdenk145d2c12004-04-15 21:48:45 +00001175 /* keep track if packet is CDP */
1176 iscdp = memcmp(et->et_dest, NetCDPAddr, 6) == 0;
1177#endif
1178
1179 myvlanid = ntohs(NetOurVLAN);
1180 if (myvlanid == (ushort)-1)
1181 myvlanid = VLAN_NONE;
1182 mynvlanid = ntohs(NetOurNativeVLAN);
1183 if (mynvlanid == (ushort)-1)
1184 mynvlanid = VLAN_NONE;
wdenk2d966952002-10-31 22:12:35 +00001185
1186 x = ntohs(et->et_protlen);
1187
wdenk145d2c12004-04-15 21:48:45 +00001188#ifdef ET_DEBUG
1189 printf("packet received\n");
1190#endif
1191
wdenk2d966952002-10-31 22:12:35 +00001192 if (x < 1514) {
1193 /*
1194 * Got a 802 packet. Check the other protocol field.
1195 */
1196 x = ntohs(et->et_prot);
wdenk145d2c12004-04-15 21:48:45 +00001197
1198 ip = (IP_t *)(inpkt + E802_HDR_SIZE);
wdenk2d966952002-10-31 22:12:35 +00001199 len -= E802_HDR_SIZE;
wdenk145d2c12004-04-15 21:48:45 +00001200
1201 } else if (x != PROT_VLAN) { /* normal packet */
1202 ip = (IP_t *)(inpkt + ETHER_HDR_SIZE);
wdenk2d966952002-10-31 22:12:35 +00001203 len -= ETHER_HDR_SIZE;
wdenk145d2c12004-04-15 21:48:45 +00001204
1205 } else { /* VLAN packet */
1206 VLAN_Ethernet_t *vet = (VLAN_Ethernet_t *)et;
1207
1208#ifdef ET_DEBUG
1209 printf("VLAN packet received\n");
1210#endif
1211 /* too small packet? */
1212 if (len < VLAN_ETHER_HDR_SIZE)
1213 return;
1214
1215 /* if no VLAN active */
1216 if ((ntohs(NetOurVLAN) & VLAN_IDMASK) == VLAN_NONE
Jon Loeliger54f35c22007-07-09 17:45:14 -05001217#if defined(CONFIG_CMD_CDP)
wdenk145d2c12004-04-15 21:48:45 +00001218 && iscdp == 0
1219#endif
1220 )
1221 return;
1222
1223 cti = ntohs(vet->vet_tag);
1224 vlanid = cti & VLAN_IDMASK;
1225 x = ntohs(vet->vet_type);
1226
1227 ip = (IP_t *)(inpkt + VLAN_ETHER_HDR_SIZE);
1228 len -= VLAN_ETHER_HDR_SIZE;
wdenk2d966952002-10-31 22:12:35 +00001229 }
1230
1231#ifdef ET_DEBUG
1232 printf("Receive from protocol 0x%x\n", x);
1233#endif
1234
Jon Loeliger54f35c22007-07-09 17:45:14 -05001235#if defined(CONFIG_CMD_CDP)
wdenk145d2c12004-04-15 21:48:45 +00001236 if (iscdp) {
1237 CDPHandler((uchar *)ip, len);
1238 return;
1239 }
1240#endif
1241
1242 if ((myvlanid & VLAN_IDMASK) != VLAN_NONE) {
1243 if (vlanid == VLAN_NONE)
1244 vlanid = (mynvlanid & VLAN_IDMASK);
1245 /* not matched? */
1246 if (vlanid != (myvlanid & VLAN_IDMASK))
1247 return;
1248 }
1249
wdenk2d966952002-10-31 22:12:35 +00001250 switch (x) {
1251
1252 case PROT_ARP:
1253 /*
1254 * We have to deal with two types of ARP packets:
wdenk57b2d802003-06-27 21:31:46 +00001255 * - REQUEST packets will be answered by sending our
1256 * IP address - if we know it.
1257 * - REPLY packates are expected only after we asked
1258 * for the TFTP server's or the gateway's ethernet
1259 * address; so if we receive such a packet, we set
1260 * the server ethernet address
wdenk2d966952002-10-31 22:12:35 +00001261 */
1262#ifdef ET_DEBUG
wdenk42c05472004-03-23 22:14:11 +00001263 puts ("Got ARP\n");
wdenk2d966952002-10-31 22:12:35 +00001264#endif
1265 arp = (ARP_t *)ip;
1266 if (len < ARP_HDR_SIZE) {
1267 printf("bad length %d < %d\n", len, ARP_HDR_SIZE);
1268 return;
1269 }
1270 if (ntohs(arp->ar_hrd) != ARP_ETHER) {
1271 return;
1272 }
1273 if (ntohs(arp->ar_pro) != PROT_IP) {
1274 return;
1275 }
1276 if (arp->ar_hln != 6) {
1277 return;
1278 }
1279 if (arp->ar_pln != 4) {
1280 return;
1281 }
1282
1283 if (NetOurIP == 0) {
1284 return;
1285 }
1286
1287 if (NetReadIP(&arp->ar_data[16]) != NetOurIP) {
1288 return;
1289 }
1290
1291 switch (ntohs(arp->ar_op)) {
1292 case ARPOP_REQUEST: /* reply with our IP address */
1293#ifdef ET_DEBUG
wdenk42c05472004-03-23 22:14:11 +00001294 puts ("Got ARP REQUEST, return our IP\n");
wdenk2d966952002-10-31 22:12:35 +00001295#endif
wdenk145d2c12004-04-15 21:48:45 +00001296 pkt = (uchar *)et;
1297 pkt += NetSetEther(pkt, et->et_src, PROT_ARP);
wdenk2d966952002-10-31 22:12:35 +00001298 arp->ar_op = htons(ARPOP_REPLY);
1299 memcpy (&arp->ar_data[10], &arp->ar_data[0], 6);
1300 NetCopyIP(&arp->ar_data[16], &arp->ar_data[6]);
1301 memcpy (&arp->ar_data[ 0], NetOurEther, 6);
1302 NetCopyIP(&arp->ar_data[ 6], &NetOurIP);
wdenk145d2c12004-04-15 21:48:45 +00001303 (void) eth_send((uchar *)et, (pkt - (uchar *)et) + ARP_HDR_SIZE);
wdenk2d966952002-10-31 22:12:35 +00001304 return;
wdenke6466f62003-06-05 19:27:42 +00001305
1306 case ARPOP_REPLY: /* arp reply */
1307 /* are we waiting for a reply */
1308 if (!NetArpWaitPacketIP || !NetArpWaitPacketMAC)
1309 break;
1310#ifdef ET_DEBUG
1311 printf("Got ARP REPLY, set server/gtwy eth addr (%02x:%02x:%02x:%02x:%02x:%02x)\n",
1312 arp->ar_data[0], arp->ar_data[1],
1313 arp->ar_data[2], arp->ar_data[3],
1314 arp->ar_data[4], arp->ar_data[5]);
1315#endif
1316
1317 tmp = NetReadIP(&arp->ar_data[6]);
1318
1319 /* matched waiting packet's address */
1320 if (tmp == NetArpWaitReplyIP) {
wdenk2d966952002-10-31 22:12:35 +00001321#ifdef ET_DEBUG
wdenk42c05472004-03-23 22:14:11 +00001322 puts ("Got it\n");
wdenk2d966952002-10-31 22:12:35 +00001323#endif
wdenke6466f62003-06-05 19:27:42 +00001324 /* save address for later use */
1325 memcpy(NetArpWaitPacketMAC, &arp->ar_data[0], 6);
1326
wdenkb8fb6192004-08-02 21:11:11 +00001327#ifdef CONFIG_NETCONSOLE
1328 (*packetHandler)(0,0,0,0);
1329#endif
wdenke6466f62003-06-05 19:27:42 +00001330 /* modify header, and transmit it */
1331 memcpy(((Ethernet_t *)NetArpWaitTxPacket)->et_dest, NetArpWaitPacketMAC, 6);
1332 (void) eth_send(NetArpWaitTxPacket, NetArpWaitTxPacketSize);
1333
1334 /* no arp request pending now */
1335 NetArpWaitPacketIP = 0;
1336 NetArpWaitTxPacketSize = 0;
1337 NetArpWaitPacketMAC = NULL;
1338
1339 }
wdenk2d966952002-10-31 22:12:35 +00001340 return;
1341 default:
1342#ifdef ET_DEBUG
1343 printf("Unexpected ARP opcode 0x%x\n", ntohs(arp->ar_op));
1344#endif
1345 return;
1346 }
wdenkcb99da52005-01-12 00:15:14 +00001347 break;
wdenk2d966952002-10-31 22:12:35 +00001348
1349 case PROT_RARP:
1350#ifdef ET_DEBUG
wdenk42c05472004-03-23 22:14:11 +00001351 puts ("Got RARP\n");
wdenk2d966952002-10-31 22:12:35 +00001352#endif
1353 arp = (ARP_t *)ip;
1354 if (len < ARP_HDR_SIZE) {
1355 printf("bad length %d < %d\n", len, ARP_HDR_SIZE);
1356 return;
1357 }
1358
1359 if ((ntohs(arp->ar_op) != RARPOP_REPLY) ||
1360 (ntohs(arp->ar_hrd) != ARP_ETHER) ||
1361 (ntohs(arp->ar_pro) != PROT_IP) ||
1362 (arp->ar_hln != 6) || (arp->ar_pln != 4)) {
1363
wdenk42c05472004-03-23 22:14:11 +00001364 puts ("invalid RARP header\n");
wdenk2d966952002-10-31 22:12:35 +00001365 } else {
1366 NetCopyIP(&NetOurIP, &arp->ar_data[16]);
wdenk4989f872004-03-14 15:06:13 +00001367 if (NetServerIP == 0)
1368 NetCopyIP(&NetServerIP, &arp->ar_data[ 6]);
wdenk2d966952002-10-31 22:12:35 +00001369 memcpy (NetServerEther, &arp->ar_data[ 0], 6);
1370
1371 (*packetHandler)(0,0,0,0);
1372 }
1373 break;
1374
1375 case PROT_IP:
1376#ifdef ET_DEBUG
wdenk42c05472004-03-23 22:14:11 +00001377 puts ("Got IP\n");
wdenk2d966952002-10-31 22:12:35 +00001378#endif
1379 if (len < IP_HDR_SIZE) {
1380 debug ("len bad %d < %d\n", len, IP_HDR_SIZE);
1381 return;
1382 }
1383 if (len < ntohs(ip->ip_len)) {
1384 printf("len bad %d < %d\n", len, ntohs(ip->ip_len));
1385 return;
1386 }
1387 len = ntohs(ip->ip_len);
1388#ifdef ET_DEBUG
1389 printf("len=%d, v=%02x\n", len, ip->ip_hl_v & 0xff);
1390#endif
1391 if ((ip->ip_hl_v & 0xf0) != 0x40) {
1392 return;
1393 }
1394 if (ip->ip_off & htons(0x1fff)) { /* Can't deal w/ fragments */
1395 return;
1396 }
1397 if (!NetCksumOk((uchar *)ip, IP_HDR_SIZE_NO_UDP / 2)) {
wdenk42c05472004-03-23 22:14:11 +00001398 puts ("checksum bad\n");
wdenk2d966952002-10-31 22:12:35 +00001399 return;
1400 }
1401 tmp = NetReadIP(&ip->ip_dst);
1402 if (NetOurIP && tmp != NetOurIP && tmp != 0xFFFFFFFF) {
David Updegraff7280da72007-06-11 10:41:07 -05001403#ifdef CONFIG_MCAST_TFTP
Wolfgang Denk627f5c32007-08-14 09:47:27 +02001404 if (Mcast_addr != tmp)
David Updegraff7280da72007-06-11 10:41:07 -05001405#endif
wdenk2d966952002-10-31 22:12:35 +00001406 return;
1407 }
1408 /*
1409 * watch for ICMP host redirects
1410 *
wdenk57b2d802003-06-27 21:31:46 +00001411 * There is no real handler code (yet). We just watch
1412 * for ICMP host redirect messages. In case anybody
1413 * sees these messages: please contact me
1414 * (wd@denx.de), or - even better - send me the
1415 * necessary fixes :-)
wdenk2d966952002-10-31 22:12:35 +00001416 *
wdenk57b2d802003-06-27 21:31:46 +00001417 * Note: in all cases where I have seen this so far
1418 * it was a problem with the router configuration,
1419 * for instance when a router was configured in the
1420 * BOOTP reply, but the TFTP server was on the same
1421 * subnet. So this is probably a warning that your
1422 * configuration might be wrong. But I'm not really
1423 * sure if there aren't any other situations.
wdenk2d966952002-10-31 22:12:35 +00001424 */
1425 if (ip->ip_p == IPPROTO_ICMP) {
1426 ICMP_t *icmph = (ICMP_t *)&(ip->udp_src);
1427
wdenke6466f62003-06-05 19:27:42 +00001428 switch (icmph->type) {
1429 case ICMP_REDIRECT:
wdenkcbc49a52005-05-03 14:12:25 +00001430 if (icmph->code != ICMP_REDIR_HOST)
1431 return;
1432 puts (" ICMP Host Redirect to ");
1433 print_IPaddr(icmph->un.gateway);
1434 putc(' ');
Stefan Roesedfced812005-08-12 20:06:52 +02001435 return;
Jon Loeliger54f35c22007-07-09 17:45:14 -05001436#if defined(CONFIG_CMD_PING)
wdenke6466f62003-06-05 19:27:42 +00001437 case ICMP_ECHO_REPLY:
1438 /*
1439 * IP header OK. Pass the packet to the current handler.
1440 */
1441 /* XXX point to ip packet */
1442 (*packetHandler)((uchar *)ip, 0, 0, 0);
Stefan Roesedfced812005-08-12 20:06:52 +02001443 return;
Ed Swarthout83109192007-03-07 12:14:50 -06001444 case ICMP_ECHO_REQUEST:
1445#ifdef ET_DEBUG
1446 printf ("Got ICMP ECHO REQUEST, return %d bytes \n",
1447 ETHER_HDR_SIZE + len);
1448#endif
1449 memcpy (&et->et_dest[0], &et->et_src[0], 6);
1450 memcpy (&et->et_src[ 0], NetOurEther, 6);
1451
1452 ip->ip_sum = 0;
1453 ip->ip_off = 0;
1454 NetCopyIP((void*)&ip->ip_dst, &ip->ip_src);
1455 NetCopyIP((void*)&ip->ip_src, &NetOurIP);
1456 ip->ip_sum = ~NetCksum((uchar *)ip, IP_HDR_SIZE_NO_UDP >> 1);
1457
1458 icmph->type = ICMP_ECHO_REPLY;
1459 icmph->checksum = 0;
1460 icmph->checksum = ~NetCksum((uchar *)icmph,
1461 (len - IP_HDR_SIZE_NO_UDP) >> 1);
1462 (void) eth_send((uchar *)et, ETHER_HDR_SIZE + len);
1463 return;
wdenke6466f62003-06-05 19:27:42 +00001464#endif
1465 default:
1466 return;
1467 }
wdenk2d966952002-10-31 22:12:35 +00001468 } else if (ip->ip_p != IPPROTO_UDP) { /* Only UDP packets */
1469 return;
1470 }
1471
Stefan Roesedfced812005-08-12 20:06:52 +02001472#ifdef CONFIG_UDP_CHECKSUM
1473 if (ip->udp_xsum != 0) {
Wolfgang Denk30b87322005-08-12 23:43:12 +02001474 ulong xsum;
Stefan Roesedfced812005-08-12 20:06:52 +02001475 ushort *sumptr;
1476 ushort sumlen;
1477
1478 xsum = ip->ip_p;
1479 xsum += (ntohs(ip->udp_len));
1480 xsum += (ntohl(ip->ip_src) >> 16) & 0x0000ffff;
1481 xsum += (ntohl(ip->ip_src) >> 0) & 0x0000ffff;
1482 xsum += (ntohl(ip->ip_dst) >> 16) & 0x0000ffff;
1483 xsum += (ntohl(ip->ip_dst) >> 0) & 0x0000ffff;
1484
1485 sumlen = ntohs(ip->udp_len);
1486 sumptr = (ushort *) &(ip->udp_src);
1487
1488 while (sumlen > 1) {
Wolfgang Denk30b87322005-08-12 23:43:12 +02001489 ushort sumdata;
Stefan Roesedfced812005-08-12 20:06:52 +02001490
1491 sumdata = *sumptr++;
1492 xsum += ntohs(sumdata);
1493 sumlen -= 2;
1494 }
1495 if (sumlen > 0) {
Wolfgang Denk30b87322005-08-12 23:43:12 +02001496 ushort sumdata;
Stefan Roesedfced812005-08-12 20:06:52 +02001497
1498 sumdata = *(unsigned char *) sumptr;
Wolfgang Denk30b87322005-08-12 23:43:12 +02001499 sumdata = (sumdata << 8) & 0xff00;
Stefan Roesedfced812005-08-12 20:06:52 +02001500 xsum += sumdata;
1501 }
1502 while ((xsum >> 16) != 0) {
Wolfgang Denk30b87322005-08-12 23:43:12 +02001503 xsum = (xsum & 0x0000ffff) + ((xsum >> 16) & 0x0000ffff);
Stefan Roesedfced812005-08-12 20:06:52 +02001504 }
1505 if ((xsum != 0x00000000) && (xsum != 0x0000ffff)) {
Wolfgang Denk30b87322005-08-12 23:43:12 +02001506 printf(" UDP wrong checksum %08x %08x\n", xsum, ntohs(ip->udp_xsum));
Stefan Roesedfced812005-08-12 20:06:52 +02001507 return;
1508 }
1509 }
1510#endif
1511
David Updegraff7280da72007-06-11 10:41:07 -05001512
wdenkb8fb6192004-08-02 21:11:11 +00001513#ifdef CONFIG_NETCONSOLE
1514 nc_input_packet((uchar *)ip +IP_HDR_SIZE,
1515 ntohs(ip->udp_dst),
1516 ntohs(ip->udp_src),
1517 ntohs(ip->udp_len) - 8);
1518#endif
wdenk2d966952002-10-31 22:12:35 +00001519 /*
1520 * IP header OK. Pass the packet to the current handler.
1521 */
1522 (*packetHandler)((uchar *)ip +IP_HDR_SIZE,
1523 ntohs(ip->udp_dst),
1524 ntohs(ip->udp_src),
1525 ntohs(ip->udp_len) - 8);
wdenk2d966952002-10-31 22:12:35 +00001526 break;
1527 }
1528}
1529
1530
1531/**********************************************************************/
1532
1533static int net_check_prereq (proto_t protocol)
1534{
1535 switch (protocol) {
wdenk05939202004-04-18 17:39:38 +00001536 /* Fall through */
Jon Loeliger54f35c22007-07-09 17:45:14 -05001537#if defined(CONFIG_CMD_PING)
wdenke6466f62003-06-05 19:27:42 +00001538 case PING:
wdenk05939202004-04-18 17:39:38 +00001539 if (NetPingIP == 0) {
1540 puts ("*** ERROR: ping address not given\n");
1541 return (1);
1542 }
1543 goto common;
wdenke6466f62003-06-05 19:27:42 +00001544#endif
Jon Loeliger54f35c22007-07-09 17:45:14 -05001545#if defined(CONFIG_CMD_SNTP)
wdenkb4ad9622005-04-01 00:25:43 +00001546 case SNTP:
1547 if (NetNtpServerIP == 0) {
1548 puts ("*** ERROR: NTP server address not given\n");
1549 return (1);
1550 }
1551 goto common;
1552#endif
Jon Loeliger54f35c22007-07-09 17:45:14 -05001553#if defined(CONFIG_CMD_NFS)
wdenkbe9c1cb2004-02-24 02:00:03 +00001554 case NFS:
1555#endif
wdenkb8fb6192004-08-02 21:11:11 +00001556 case NETCONS:
wdenk2d966952002-10-31 22:12:35 +00001557 case TFTP:
wdenk05939202004-04-18 17:39:38 +00001558 if (NetServerIP == 0) {
1559 puts ("*** ERROR: `serverip' not set\n");
1560 return (1);
1561 }
Jon Loeliger54f35c22007-07-09 17:45:14 -05001562#if defined(CONFIG_CMD_PING) || defined(CONFIG_CMD_SNTP)
Wolfgang Denk30b87322005-08-12 23:43:12 +02001563 common:
wdenke6466f62003-06-05 19:27:42 +00001564#endif
1565
wdenk05939202004-04-18 17:39:38 +00001566 if (NetOurIP == 0) {
1567 puts ("*** ERROR: `ipaddr' not set\n");
1568 return (1);
1569 }
1570 /* Fall through */
wdenk2d966952002-10-31 22:12:35 +00001571
1572 case DHCP:
1573 case RARP:
1574 case BOOTP:
wdenk145d2c12004-04-15 21:48:45 +00001575 case CDP:
wdenk05939202004-04-18 17:39:38 +00001576 if (memcmp (NetOurEther, "\0\0\0\0\0\0", 6) == 0) {
wdenk2d966952002-10-31 22:12:35 +00001577#ifdef CONFIG_NET_MULTI
wdenk05939202004-04-18 17:39:38 +00001578 extern int eth_get_dev_index (void);
1579 int num = eth_get_dev_index ();
wdenk2d966952002-10-31 22:12:35 +00001580
wdenk05939202004-04-18 17:39:38 +00001581 switch (num) {
1582 case -1:
wdenk2d966952002-10-31 22:12:35 +00001583 puts ("*** ERROR: No ethernet found.\n");
1584 return (1);
wdenk05939202004-04-18 17:39:38 +00001585 case 0:
wdenk2d966952002-10-31 22:12:35 +00001586 puts ("*** ERROR: `ethaddr' not set\n");
1587 break;
wdenk05939202004-04-18 17:39:38 +00001588 default:
wdenk57b2d802003-06-27 21:31:46 +00001589 printf ("*** ERROR: `eth%daddr' not set\n",
wdenk2d966952002-10-31 22:12:35 +00001590 num);
1591 break;
wdenk05939202004-04-18 17:39:38 +00001592 }
wdenk2d966952002-10-31 22:12:35 +00001593
wdenk05939202004-04-18 17:39:38 +00001594 NetStartAgain ();
1595 return (2);
wdenk2d966952002-10-31 22:12:35 +00001596#else
wdenk05939202004-04-18 17:39:38 +00001597 puts ("*** ERROR: `ethaddr' not set\n");
1598 return (1);
wdenk2d966952002-10-31 22:12:35 +00001599#endif
wdenk05939202004-04-18 17:39:38 +00001600 }
1601 /* Fall through */
1602 default:
1603 return (0);
wdenk2d966952002-10-31 22:12:35 +00001604 }
wdenk05939202004-04-18 17:39:38 +00001605 return (0); /* OK */
wdenk2d966952002-10-31 22:12:35 +00001606}
1607/**********************************************************************/
1608
1609int
1610NetCksumOk(uchar * ptr, int len)
1611{
1612 return !((NetCksum(ptr, len) + 1) & 0xfffe);
1613}
1614
1615
1616unsigned
1617NetCksum(uchar * ptr, int len)
1618{
1619 ulong xsum;
Stefan Roeseac553282005-08-31 12:55:50 +02001620 ushort *p = (ushort *)ptr;
wdenk2d966952002-10-31 22:12:35 +00001621
1622 xsum = 0;
1623 while (len-- > 0)
Wolfgang Denk3135c542005-08-26 01:36:03 +02001624 xsum += *p++;
wdenk2d966952002-10-31 22:12:35 +00001625 xsum = (xsum & 0xffff) + (xsum >> 16);
1626 xsum = (xsum & 0xffff) + (xsum >> 16);
1627 return (xsum & 0xffff);
1628}
1629
wdenk145d2c12004-04-15 21:48:45 +00001630int
1631NetEthHdrSize(void)
1632{
1633 ushort myvlanid;
1634
1635 myvlanid = ntohs(NetOurVLAN);
1636 if (myvlanid == (ushort)-1)
1637 myvlanid = VLAN_NONE;
wdenk2d966952002-10-31 22:12:35 +00001638
wdenk145d2c12004-04-15 21:48:45 +00001639 return ((myvlanid & VLAN_IDMASK) == VLAN_NONE) ? ETHER_HDR_SIZE : VLAN_ETHER_HDR_SIZE;
1640}
1641
1642int
wdenk2d966952002-10-31 22:12:35 +00001643NetSetEther(volatile uchar * xet, uchar * addr, uint prot)
1644{
1645 Ethernet_t *et = (Ethernet_t *)xet;
wdenk145d2c12004-04-15 21:48:45 +00001646 ushort myvlanid;
1647
1648 myvlanid = ntohs(NetOurVLAN);
1649 if (myvlanid == (ushort)-1)
1650 myvlanid = VLAN_NONE;
wdenk2d966952002-10-31 22:12:35 +00001651
1652 memcpy (et->et_dest, addr, 6);
1653 memcpy (et->et_src, NetOurEther, 6);
wdenk145d2c12004-04-15 21:48:45 +00001654 if ((myvlanid & VLAN_IDMASK) == VLAN_NONE) {
wdenk2d966952002-10-31 22:12:35 +00001655 et->et_protlen = htons(prot);
wdenk145d2c12004-04-15 21:48:45 +00001656 return ETHER_HDR_SIZE;
1657 } else {
1658 VLAN_Ethernet_t *vet = (VLAN_Ethernet_t *)xet;
wdenk2d966952002-10-31 22:12:35 +00001659
wdenk145d2c12004-04-15 21:48:45 +00001660 vet->vet_vlan_type = htons(PROT_VLAN);
1661 vet->vet_tag = htons((0 << 5) | (myvlanid & VLAN_IDMASK));
1662 vet->vet_type = htons(prot);
1663 return VLAN_ETHER_HDR_SIZE;
1664 }
1665}
wdenk2d966952002-10-31 22:12:35 +00001666
1667void
1668NetSetIP(volatile uchar * xip, IPaddr_t dest, int dport, int sport, int len)
1669{
1670 volatile IP_t *ip = (IP_t *)xip;
1671
1672 /*
1673 * If the data is an odd number of bytes, zero the
1674 * byte after the last byte so that the checksum
1675 * will work.
1676 */
1677 if (len & 1)
1678 xip[IP_HDR_SIZE + len] = 0;
1679
1680 /*
1681 * Construct an IP and UDP header.
wdenk05939202004-04-18 17:39:38 +00001682 * (need to set no fragment bit - XXX)
wdenk2d966952002-10-31 22:12:35 +00001683 */
1684 ip->ip_hl_v = 0x45; /* IP_HDR_SIZE / 4 (not including UDP) */
1685 ip->ip_tos = 0;
1686 ip->ip_len = htons(IP_HDR_SIZE + len);
1687 ip->ip_id = htons(NetIPID++);
1688 ip->ip_off = htons(0x4000); /* No fragmentation */
1689 ip->ip_ttl = 255;
1690 ip->ip_p = 17; /* UDP */
1691 ip->ip_sum = 0;
1692 NetCopyIP((void*)&ip->ip_src, &NetOurIP); /* already in network byte order */
1693 NetCopyIP((void*)&ip->ip_dst, &dest); /* - "" - */
1694 ip->udp_src = htons(sport);
1695 ip->udp_dst = htons(dport);
1696 ip->udp_len = htons(8 + len);
1697 ip->udp_xsum = 0;
1698 ip->ip_sum = ~NetCksum((uchar *)ip, IP_HDR_SIZE_NO_UDP / 2);
1699}
1700
Wolfgang Denk7fb52662005-10-13 16:45:02 +02001701void copy_filename (char *dst, char *src, int size)
wdenk2d966952002-10-31 22:12:35 +00001702{
1703 if (*src && (*src == '"')) {
1704 ++src;
1705 --size;
1706 }
1707
1708 while ((--size > 0) && *src && (*src != '"')) {
1709 *dst++ = *src++;
1710 }
1711 *dst = '\0';
1712}
1713
Jon Loeligera9807e52007-07-10 11:05:02 -05001714#endif
wdenk2d966952002-10-31 22:12:35 +00001715
1716void ip_to_string (IPaddr_t x, char *s)
1717{
wdenk145d2c12004-04-15 21:48:45 +00001718 x = ntohl (x);
1719 sprintf (s, "%d.%d.%d.%d",
1720 (int) ((x >> 24) & 0xff),
1721 (int) ((x >> 16) & 0xff),
1722 (int) ((x >> 8) & 0xff), (int) ((x >> 0) & 0xff)
wdenk05939202004-04-18 17:39:38 +00001723 );
wdenk2d966952002-10-31 22:12:35 +00001724}
1725
wdenke6466f62003-06-05 19:27:42 +00001726IPaddr_t string_to_ip(char *s)
wdenk2d966952002-10-31 22:12:35 +00001727{
1728 IPaddr_t addr;
wdenke6466f62003-06-05 19:27:42 +00001729 char *e;
wdenk2d966952002-10-31 22:12:35 +00001730 int i;
1731
wdenke6466f62003-06-05 19:27:42 +00001732 if (s == NULL)
1733 return(0);
wdenk2d966952002-10-31 22:12:35 +00001734
1735 for (addr=0, i=0; i<4; ++i) {
1736 ulong val = s ? simple_strtoul(s, &e, 10) : 0;
1737 addr <<= 8;
1738 addr |= (val & 0xFF);
1739 if (s) {
1740 s = (*e) ? e+1 : e;
1741 }
1742 }
1743
1744 return (htonl(addr));
1745}
wdenke6466f62003-06-05 19:27:42 +00001746
wdenk145d2c12004-04-15 21:48:45 +00001747void VLAN_to_string(ushort x, char *s)
1748{
1749 x = ntohs(x);
1750
1751 if (x == (ushort)-1)
1752 x = VLAN_NONE;
1753
1754 if (x == VLAN_NONE)
1755 strcpy(s, "none");
1756 else
1757 sprintf(s, "%d", x & VLAN_IDMASK);
1758}
1759
1760ushort string_to_VLAN(char *s)
1761{
1762 ushort id;
1763
1764 if (s == NULL)
wdenk656140b2004-04-25 13:18:40 +00001765 return htons(VLAN_NONE);
wdenk145d2c12004-04-15 21:48:45 +00001766
1767 if (*s < '0' || *s > '9')
1768 id = VLAN_NONE;
1769 else
1770 id = (ushort)simple_strtoul(s, NULL, 10);
1771
wdenk656140b2004-04-25 13:18:40 +00001772 return htons(id);
wdenk145d2c12004-04-15 21:48:45 +00001773}
1774
wdenke6466f62003-06-05 19:27:42 +00001775void print_IPaddr (IPaddr_t x)
1776{
wdenk145d2c12004-04-15 21:48:45 +00001777 char tmp[16];
wdenke6466f62003-06-05 19:27:42 +00001778
wdenk145d2c12004-04-15 21:48:45 +00001779 ip_to_string (x, tmp);
wdenke6466f62003-06-05 19:27:42 +00001780
wdenk145d2c12004-04-15 21:48:45 +00001781 puts (tmp);
wdenke6466f62003-06-05 19:27:42 +00001782}
1783
1784IPaddr_t getenv_IPaddr (char *var)
1785{
1786 return (string_to_ip(getenv(var)));
1787}
wdenk145d2c12004-04-15 21:48:45 +00001788
1789ushort getenv_VLAN(char *var)
1790{
1791 return (string_to_VLAN(getenv(var)));
1792}