blob: 77e83b5bd0a986a1f8853a52caf4b6e0e3284dd0 [file] [log] [blame]
wdenk2d966952002-10-31 22:12:35 +00001/*
2 * Copied from Linux Monitor (LiMon) - Networking.
3 *
4 * Copyright 1994 - 2000 Neil Russell.
5 * (See License)
6 * Copyright 2000 Roland Borde
7 * Copyright 2000 Paolo Scaffardi
8 * Copyright 2000-2002 Wolfgang Denk, wd@denx.de
9 */
10
11/*
12 * General Desription:
13 *
14 * The user interface supports commands for BOOTP, RARP, and TFTP.
15 * Also, we support ARP internally. Depending on available data,
16 * these interact as follows:
17 *
18 * BOOTP:
19 *
20 * Prerequisites: - own ethernet address
21 * We want: - own IP address
22 * - TFTP server IP address
23 * - name of bootfile
24 * Next step: ARP
25 *
26 * RARP:
27 *
28 * Prerequisites: - own ethernet address
29 * We want: - own IP address
30 * - TFTP server IP address
31 * Next step: ARP
32 *
33 * ARP:
34 *
35 * Prerequisites: - own ethernet address
36 * - own IP address
37 * - TFTP server IP address
38 * We want: - TFTP server ethernet address
39 * Next step: TFTP
40 *
41 * DHCP:
42 *
Wolfgang Denkb2f50802005-08-12 23:43:12 +020043 * Prerequisites: - own ethernet address
44 * We want: - IP, Netmask, ServerIP, Gateway IP
45 * - bootfilename, lease time
46 * Next step: - TFTP
wdenk2d966952002-10-31 22:12:35 +000047 *
48 * TFTP:
49 *
50 * Prerequisites: - own ethernet address
51 * - own IP address
52 * - TFTP server IP address
53 * - TFTP server ethernet address
54 * - name of bootfile (if unknown, we use a default name
55 * derived from our own IP address)
56 * We want: - load the boot file
57 * Next step: none
wdenkcbd8a352004-02-24 02:00:03 +000058 *
59 * NFS:
60 *
61 * Prerequisites: - own ethernet address
62 * - own IP address
63 * - name of bootfile (if unknown, we use a default name
64 * derived from our own IP address)
65 * We want: - load the boot file
66 * Next step: none
wdenkea287de2005-04-01 00:25:43 +000067 *
68 * SNTP:
69 *
Wolfgang Denkb2f50802005-08-12 23:43:12 +020070 * Prerequisites: - own ethernet address
wdenkea287de2005-04-01 00:25:43 +000071 * - own IP address
72 * We want: - network time
73 * Next step: none
wdenk2d966952002-10-31 22:12:35 +000074 */
75
76
77#include <common.h>
78#include <watchdog.h>
79#include <command.h>
80#include <net.h>
81#include "bootp.h"
82#include "tftp.h"
83#include "rarp.h"
wdenkcbd8a352004-02-24 02:00:03 +000084#include "nfs.h"
wdenkfc3e2162003-10-08 22:33:00 +000085#ifdef CONFIG_STATUS_LED
86#include <status_led.h>
87#include <miiphy.h>
88#endif
Jon Loeliger643d1ab2007-07-09 17:45:14 -050089#if defined(CONFIG_CMD_SNTP)
wdenkea287de2005-04-01 00:25:43 +000090#include "sntp.h"
91#endif
wdenk2d966952002-10-31 22:12:35 +000092
Jon Loeliger643d1ab2007-07-09 17:45:14 -050093#if defined(CONFIG_CMD_NET)
wdenk2d966952002-10-31 22:12:35 +000094
Wolfgang Denkd87080b2006-03-31 18:32:53 +020095DECLARE_GLOBAL_DATA_PTR;
96
Guennadi Liakhovetski40cb90e2008-04-03 17:04:19 +020097#ifndef CONFIG_ARP_TIMEOUT
Bartlomiej Sieka49f3bdb2008-10-01 15:26:28 +020098# define ARP_TIMEOUT 5000UL /* Milliseconds before trying ARP again */
wdenk73a8b272003-06-05 19:27:42 +000099#else
Bartlomiej Sieka49f3bdb2008-10-01 15:26:28 +0200100# define ARP_TIMEOUT CONFIG_ARP_TIMEOUT
Guennadi Liakhovetski40cb90e2008-04-03 17:04:19 +0200101#endif
102
103
104#ifndef CONFIG_NET_RETRY_COUNT
105# define ARP_TIMEOUT_COUNT 5 /* # of timeouts before giving up */
106#else
107# define ARP_TIMEOUT_COUNT CONFIG_NET_RETRY_COUNT
wdenk73a8b272003-06-05 19:27:42 +0000108#endif
109
wdenk2d966952002-10-31 22:12:35 +0000110#if 0
111#define ET_DEBUG
112#endif
113
114/** 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 Loeliger1fe80d72007-07-09 22:08:34 -0500119#if defined(CONFIG_BOOTP_DNS2)
stroesefe389a82003-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 Updegraff53a5c422007-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 */
wdenk73a8b272003-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 Liakhovetski40cb90e2008-04-03 17:04:19 +0200138IPaddr_t NetServerIP; /* Server IP addr (0 = unknown) */
wdenk2d966952002-10-31 22:12:35 +0000139volatile uchar *NetRxPkt; /* Current receive packet */
140int NetRxPktLen; /* Current rx packet length */
141unsigned NetIPID; /* IP packet ID */
142uchar NetBcastAddr[6] = /* Ethernet bcast address */
143 { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
wdenk73a8b272003-06-05 19:27:42 +0000144uchar NetEtherNullAddr[6] =
145 { 0, 0, 0, 0, 0, 0 };
Rafal Jaworowskif85b6072007-12-27 18:19:02 +0100146#ifdef CONFIG_API
147void (*push_packet)(volatile void *, int len) = 0;
148#endif
Jon Loeliger643d1ab2007-07-09 17:45:14 -0500149#if defined(CONFIG_CMD_CDP)
wdenk6e592382004-04-18 17:39:38 +0000150uchar NetCDPAddr[6] = /* Ethernet bcast address */
wdenka3d991b2004-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
wdenk6e592382004-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 */
wdenka3d991b2004-04-15 21:48:45 +0000163
wdenk2d966952002-10-31 22:12:35 +0000164char BootFile[128]; /* Boot File name */
165
Jon Loeliger643d1ab2007-07-09 17:45:14 -0500166#if defined(CONFIG_CMD_PING)
Wolfgang Denk53677ef2008-05-20 16:00:29 +0200167IPaddr_t NetPingIP; /* the ip address to ping */
wdenk73a8b272003-06-05 19:27:42 +0000168
169static void PingStart(void);
170#endif
171
Jon Loeliger643d1ab2007-07-09 17:45:14 -0500172#if defined(CONFIG_CMD_CDP)
wdenka3d991b2004-04-15 21:48:45 +0000173static void CDPStart(void);
174#endif
175
Jon Loeliger643d1ab2007-07-09 17:45:14 -0500176#if defined(CONFIG_CMD_SNTP)
wdenkea287de2005-04-01 00:25:43 +0000177IPaddr_t NetNtpServerIP; /* NTP server IP address */
178int NetTimeOffset=0; /* offset time from UTC */
179#endif
180
wdenk68ceb292004-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 */
wdenke0ac62d2003-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
198/**********************************************************************/
wdenk73a8b272003-06-05 19:27:42 +0000199
200IPaddr_t NetArpWaitPacketIP;
201IPaddr_t NetArpWaitReplyIP;
202uchar *NetArpWaitPacketMAC; /* MAC address of waiting packet's destination */
Wolfgang Denkb2f50802005-08-12 23:43:12 +0200203uchar *NetArpWaitTxPacket; /* THE transmit packet */
wdenk73a8b272003-06-05 19:27:42 +0000204int NetArpWaitTxPacketSize;
Wolfgang Denk53677ef2008-05-20 16:00:29 +0200205uchar NetArpWaitPacketBuf[PKTSIZE_ALIGN + PKTALIGN];
wdenk73a8b272003-06-05 19:27:42 +0000206ulong NetArpWaitTimerStart;
207int NetArpWaitTry;
208
wdenk6e592382004-04-18 17:39:38 +0000209void ArpRequest (void)
wdenk73a8b272003-06-05 19:27:42 +0000210{
211 int i;
212 volatile uchar *pkt;
wdenk6e592382004-04-18 17:39:38 +0000213 ARP_t *arp;
wdenk73a8b272003-06-05 19:27:42 +0000214
215#ifdef ET_DEBUG
wdenk6e592382004-04-18 17:39:38 +0000216 printf ("ARP broadcast %d\n", NetArpWaitTry);
wdenk73a8b272003-06-05 19:27:42 +0000217#endif
218 pkt = NetTxPacket;
219
wdenk6e592382004-04-18 17:39:38 +0000220 pkt += NetSetEther (pkt, NetBcastAddr, PROT_ARP);
wdenk73a8b272003-06-05 19:27:42 +0000221
wdenk6e592382004-04-18 17:39:38 +0000222 arp = (ARP_t *) pkt;
wdenk73a8b272003-06-05 19:27:42 +0000223
wdenk6e592382004-04-18 17:39:38 +0000224 arp->ar_hrd = htons (ARP_ETHER);
225 arp->ar_pro = htons (PROT_IP);
wdenk73a8b272003-06-05 19:27:42 +0000226 arp->ar_hln = 6;
227 arp->ar_pln = 4;
wdenk6e592382004-04-18 17:39:38 +0000228 arp->ar_op = htons (ARPOP_REQUEST);
wdenk73a8b272003-06-05 19:27:42 +0000229
Wolfgang Denkb2f50802005-08-12 23:43:12 +0200230 memcpy (&arp->ar_data[0], NetOurEther, 6); /* source ET addr */
231 NetWriteIP ((uchar *) & arp->ar_data[6], NetOurIP); /* source IP addr */
wdenk6e592382004-04-18 17:39:38 +0000232 for (i = 10; i < 16; ++i) {
233 arp->ar_data[i] = 0; /* dest ET addr = 0 */
wdenk73a8b272003-06-05 19:27:42 +0000234 }
235
wdenk6e592382004-04-18 17:39:38 +0000236 if ((NetArpWaitPacketIP & NetOurSubnetMask) !=
237 (NetOurIP & NetOurSubnetMask)) {
238 if (NetOurGatewayIP == 0) {
239 puts ("## Warning: gatewayip needed but not set\n");
Wolfgang Denkd509b812006-03-12 01:13:30 +0100240 NetArpWaitReplyIP = NetArpWaitPacketIP;
241 } else {
242 NetArpWaitReplyIP = NetOurGatewayIP;
wdenk6e592382004-04-18 17:39:38 +0000243 }
wdenk6e592382004-04-18 17:39:38 +0000244 } else {
245 NetArpWaitReplyIP = NetArpWaitPacketIP;
246 }
wdenk73a8b272003-06-05 19:27:42 +0000247
wdenk6e592382004-04-18 17:39:38 +0000248 NetWriteIP ((uchar *) & arp->ar_data[16], NetArpWaitReplyIP);
249 (void) eth_send (NetTxPacket, (pkt - NetTxPacket) + ARP_HDR_SIZE);
wdenk73a8b272003-06-05 19:27:42 +0000250}
251
252void ArpTimeoutCheck(void)
253{
254 ulong t;
255
256 if (!NetArpWaitPacketIP)
257 return;
258
259 t = get_timer(0);
260
261 /* check for arp timeout */
Bartlomiej Sieka49f3bdb2008-10-01 15:26:28 +0200262 if ((t - NetArpWaitTimerStart) > ARP_TIMEOUT) {
wdenk73a8b272003-06-05 19:27:42 +0000263 NetArpWaitTry++;
264
265 if (NetArpWaitTry >= ARP_TIMEOUT_COUNT) {
266 puts ("\nARP Retry count exceeded; starting again\n");
267 NetArpWaitTry = 0;
268 NetStartAgain();
269 } else {
270 NetArpWaitTimerStart = t;
271 ArpRequest();
272 }
273 }
274}
275
276/**********************************************************************/
wdenk2d966952002-10-31 22:12:35 +0000277/*
278 * Main network processing loop.
279 */
280
281int
282NetLoop(proto_t protocol)
283{
wdenk2d966952002-10-31 22:12:35 +0000284 bd_t *bd = gd->bd;
285
286#ifdef CONFIG_NET_MULTI
287 NetRestarted = 0;
288 NetDevExists = 0;
289#endif
290
wdenk73a8b272003-06-05 19:27:42 +0000291 /* XXX problem with bss workaround */
292 NetArpWaitPacketMAC = NULL;
293 NetArpWaitTxPacket = NULL;
294 NetArpWaitPacketIP = 0;
295 NetArpWaitReplyIP = 0;
296 NetArpWaitTxPacket = NULL;
297 NetTxPacket = NULL;
298
wdenk2d966952002-10-31 22:12:35 +0000299 if (!NetTxPacket) {
300 int i;
wdenk2d966952002-10-31 22:12:35 +0000301 /*
302 * Setup packet buffers, aligned correctly.
303 */
304 NetTxPacket = &PktBuf[0] + (PKTALIGN - 1);
305 NetTxPacket -= (ulong)NetTxPacket % PKTALIGN;
306 for (i = 0; i < PKTBUFSRX; i++) {
307 NetRxPackets[i] = NetTxPacket + (i+1)*PKTSIZE_ALIGN;
308 }
wdenk73a8b272003-06-05 19:27:42 +0000309 }
310
311 if (!NetArpWaitTxPacket) {
312 NetArpWaitTxPacket = &NetArpWaitPacketBuf[0] + (PKTALIGN - 1);
313 NetArpWaitTxPacket -= (ulong)NetArpWaitTxPacket % PKTALIGN;
314 NetArpWaitTxPacketSize = 0;
wdenk2d966952002-10-31 22:12:35 +0000315 }
316
317 eth_halt();
wdenk6e592382004-04-18 17:39:38 +0000318#ifdef CONFIG_NET_MULTI
wdenka3d991b2004-04-15 21:48:45 +0000319 eth_set_current();
wdenk6e592382004-04-18 17:39:38 +0000320#endif
wdenkb1bf6f22005-04-03 14:52:59 +0000321 if (eth_init(bd) < 0) {
322 eth_halt();
wdenk6e592382004-04-18 17:39:38 +0000323 return(-1);
wdenkb1bf6f22005-04-03 14:52:59 +0000324 }
wdenk2d966952002-10-31 22:12:35 +0000325
326restart:
327#ifdef CONFIG_NET_MULTI
328 memcpy (NetOurEther, eth_get_dev()->enetaddr, 6);
329#else
330 memcpy (NetOurEther, bd->bi_enetaddr, 6);
331#endif
332
333 NetState = NETLOOP_CONTINUE;
334
335 /*
336 * Start the ball rolling with the given start function. From
337 * here on, this code is a state machine driven by received
338 * packets and timer events.
339 */
340
341 switch (protocol) {
Jon Loeliger643d1ab2007-07-09 17:45:14 -0500342#if defined(CONFIG_CMD_NFS)
wdenkcbd8a352004-02-24 02:00:03 +0000343 case NFS:
344#endif
Jon Loeliger643d1ab2007-07-09 17:45:14 -0500345#if defined(CONFIG_CMD_PING)
wdenk73a8b272003-06-05 19:27:42 +0000346 case PING:
347#endif
Jon Loeliger643d1ab2007-07-09 17:45:14 -0500348#if defined(CONFIG_CMD_SNTP)
wdenkea287de2005-04-01 00:25:43 +0000349 case SNTP:
350#endif
wdenk68ceb292004-08-02 21:11:11 +0000351 case NETCONS:
wdenk2d966952002-10-31 22:12:35 +0000352 case TFTP:
353 NetCopyIP(&NetOurIP, &bd->bi_ip_addr);
wdenk2d966952002-10-31 22:12:35 +0000354 NetOurGatewayIP = getenv_IPaddr ("gatewayip");
355 NetOurSubnetMask= getenv_IPaddr ("netmask");
wdenka3d991b2004-04-15 21:48:45 +0000356 NetOurVLAN = getenv_VLAN("vlan");
357 NetOurNativeVLAN = getenv_VLAN("nvlan");
wdenk73a8b272003-06-05 19:27:42 +0000358
359 switch (protocol) {
Jon Loeliger643d1ab2007-07-09 17:45:14 -0500360#if defined(CONFIG_CMD_NFS)
wdenkcbd8a352004-02-24 02:00:03 +0000361 case NFS:
362#endif
wdenk68ceb292004-08-02 21:11:11 +0000363 case NETCONS:
wdenk73a8b272003-06-05 19:27:42 +0000364 case TFTP:
365 NetServerIP = getenv_IPaddr ("serverip");
366 break;
Jon Loeliger643d1ab2007-07-09 17:45:14 -0500367#if defined(CONFIG_CMD_PING)
wdenk73a8b272003-06-05 19:27:42 +0000368 case PING:
369 /* nothing */
370 break;
371#endif
Jon Loeliger643d1ab2007-07-09 17:45:14 -0500372#if defined(CONFIG_CMD_SNTP)
wdenkea287de2005-04-01 00:25:43 +0000373 case SNTP:
374 /* nothing */
375 break;
376#endif
wdenk73a8b272003-06-05 19:27:42 +0000377 default:
378 break;
379 }
380
wdenk2d966952002-10-31 22:12:35 +0000381 break;
382 case BOOTP:
383 case RARP:
384 /*
wdenk8bde7f72003-06-27 21:31:46 +0000385 * initialize our IP addr to 0 in order to accept ANY
386 * IP addr assigned to us by the BOOTP / RARP server
wdenk2d966952002-10-31 22:12:35 +0000387 */
388 NetOurIP = 0;
wdenk3d3befa2004-03-14 15:06:13 +0000389 NetServerIP = getenv_IPaddr ("serverip");
Wolfgang Denkb2f50802005-08-12 23:43:12 +0200390 NetOurVLAN = getenv_VLAN("vlan"); /* VLANs must be read */
391 NetOurNativeVLAN = getenv_VLAN("nvlan");
392 case CDP:
393 NetOurVLAN = getenv_VLAN("vlan"); /* VLANs must be read */
394 NetOurNativeVLAN = getenv_VLAN("nvlan");
wdenk2d966952002-10-31 22:12:35 +0000395 break;
396 default:
397 break;
398 }
399
400 switch (net_check_prereq (protocol)) {
401 case 1:
402 /* network not configured */
wdenkb1bf6f22005-04-03 14:52:59 +0000403 eth_halt();
wdenk1d0350e2002-11-11 21:14:20 +0000404 return (-1);
wdenk2d966952002-10-31 22:12:35 +0000405
406#ifdef CONFIG_NET_MULTI
407 case 2:
408 /* network device not configured */
409 break;
410#endif /* CONFIG_NET_MULTI */
411
412 case 0:
413#ifdef CONFIG_NET_MULTI
414 NetDevExists = 1;
415#endif
416 switch (protocol) {
417 case TFTP:
418 /* always use ARP to get server ethernet address */
wdenk73a8b272003-06-05 19:27:42 +0000419 TftpStart();
wdenk2d966952002-10-31 22:12:35 +0000420 break;
421
Jon Loeliger643d1ab2007-07-09 17:45:14 -0500422#if defined(CONFIG_CMD_DHCP)
wdenk2d966952002-10-31 22:12:35 +0000423 case DHCP:
424 /* Start with a clean slate... */
wdenkd407bf52004-10-11 22:51:13 +0000425 BootpTry = 0;
wdenk2d966952002-10-31 22:12:35 +0000426 NetOurIP = 0;
wdenk3d3befa2004-03-14 15:06:13 +0000427 NetServerIP = getenv_IPaddr ("serverip");
wdenk2d966952002-10-31 22:12:35 +0000428 DhcpRequest(); /* Basically same as BOOTP */
429 break;
Jon Loeliger610f2e92007-07-10 11:05:02 -0500430#endif
wdenk2d966952002-10-31 22:12:35 +0000431
432 case BOOTP:
433 BootpTry = 0;
434 BootpRequest ();
435 break;
436
437 case RARP:
438 RarpTry = 0;
439 RarpRequest ();
440 break;
Jon Loeliger643d1ab2007-07-09 17:45:14 -0500441#if defined(CONFIG_CMD_PING)
wdenk73a8b272003-06-05 19:27:42 +0000442 case PING:
443 PingStart();
444 break;
445#endif
Jon Loeliger643d1ab2007-07-09 17:45:14 -0500446#if defined(CONFIG_CMD_NFS)
wdenkcbd8a352004-02-24 02:00:03 +0000447 case NFS:
448 NfsStart();
449 break;
450#endif
Jon Loeliger643d1ab2007-07-09 17:45:14 -0500451#if defined(CONFIG_CMD_CDP)
wdenka3d991b2004-04-15 21:48:45 +0000452 case CDP:
453 CDPStart();
454 break;
455#endif
wdenk68ceb292004-08-02 21:11:11 +0000456#ifdef CONFIG_NETCONSOLE
457 case NETCONS:
458 NcStart();
459 break;
460#endif
Jon Loeliger643d1ab2007-07-09 17:45:14 -0500461#if defined(CONFIG_CMD_SNTP)
wdenkea287de2005-04-01 00:25:43 +0000462 case SNTP:
463 SntpStart();
464 break;
465#endif
wdenk2d966952002-10-31 22:12:35 +0000466 default:
467 break;
468 }
469
470 NetBootFileXferSize = 0;
471 break;
472 }
473
Jon Loeliger643d1ab2007-07-09 17:45:14 -0500474#if defined(CONFIG_MII) || defined(CONFIG_CMD_MII)
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200475#if defined(CONFIG_SYS_FAULT_ECHO_LINK_DOWN) && defined(CONFIG_STATUS_LED) && defined(STATUS_LED_RED)
wdenkfc3e2162003-10-08 22:33:00 +0000476 /*
wdenk42d1f032003-10-15 23:53:47 +0000477 * Echo the inverted link state to the fault LED.
wdenkfc3e2162003-10-08 22:33:00 +0000478 */
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200479 if(miiphy_link(eth_get_dev()->name, CONFIG_SYS_FAULT_MII_ADDR)) {
wdenkfc3e2162003-10-08 22:33:00 +0000480 status_led_set (STATUS_LED_RED, STATUS_LED_OFF);
481 } else {
482 status_led_set (STATUS_LED_RED, STATUS_LED_ON);
483 }
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200484#endif /* CONFIG_SYS_FAULT_ECHO_LINK_DOWN, ... */
wdenkfc3e2162003-10-08 22:33:00 +0000485#endif /* CONFIG_MII, ... */
wdenk2d966952002-10-31 22:12:35 +0000486
487 /*
488 * Main packet reception loop. Loop receiving packets until
wdenk59acc292005-04-03 14:18:51 +0000489 * someone sets `NetState' to a state that terminates.
wdenk2d966952002-10-31 22:12:35 +0000490 */
491 for (;;) {
492 WATCHDOG_RESET();
493#ifdef CONFIG_SHOW_ACTIVITY
494 {
495 extern void show_activity(int arg);
496 show_activity(1);
497 }
498#endif
499 /*
500 * Check the ethernet for a new packet. The ethernet
501 * receive routine will process it.
502 */
Guennadi Liakhovetski40cb90e2008-04-03 17:04:19 +0200503 eth_rx();
wdenk2d966952002-10-31 22:12:35 +0000504
505 /*
506 * Abort if ctrl-c was pressed.
507 */
508 if (ctrlc()) {
wdenk8bde7f72003-06-27 21:31:46 +0000509 eth_halt();
wdenk4b9206e2004-03-23 22:14:11 +0000510 puts ("\nAbort\n");
wdenk1d0350e2002-11-11 21:14:20 +0000511 return (-1);
wdenk2d966952002-10-31 22:12:35 +0000512 }
513
wdenk73a8b272003-06-05 19:27:42 +0000514 ArpTimeoutCheck();
wdenk2d966952002-10-31 22:12:35 +0000515
516 /*
517 * Check for a timeout, and run the timeout handler
518 * if we have one.
519 */
wdenke0ac62d2003-08-17 18:55:18 +0000520 if (timeHandler && ((get_timer(0) - timeStart) > timeDelta)) {
wdenk2d966952002-10-31 22:12:35 +0000521 thand_f *x;
522
Jon Loeliger643d1ab2007-07-09 17:45:14 -0500523#if defined(CONFIG_MII) || defined(CONFIG_CMD_MII)
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200524# if defined(CONFIG_SYS_FAULT_ECHO_LINK_DOWN) && \
wdenk63153492005-04-03 20:55:38 +0000525 defined(CONFIG_STATUS_LED) && \
wdenk59acc292005-04-03 14:18:51 +0000526 defined(STATUS_LED_RED)
wdenkfc3e2162003-10-08 22:33:00 +0000527 /*
wdenk42d1f032003-10-15 23:53:47 +0000528 * Echo the inverted link state to the fault LED.
wdenkfc3e2162003-10-08 22:33:00 +0000529 */
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200530 if(miiphy_link(eth_get_dev()->name, CONFIG_SYS_FAULT_MII_ADDR)) {
wdenkfc3e2162003-10-08 22:33:00 +0000531 status_led_set (STATUS_LED_RED, STATUS_LED_OFF);
532 } else {
533 status_led_set (STATUS_LED_RED, STATUS_LED_ON);
534 }
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200535# endif /* CONFIG_SYS_FAULT_ECHO_LINK_DOWN, ... */
wdenkfc3e2162003-10-08 22:33:00 +0000536#endif /* CONFIG_MII, ... */
wdenk2d966952002-10-31 22:12:35 +0000537 x = timeHandler;
538 timeHandler = (thand_f *)0;
539 (*x)();
540 }
541
542
543 switch (NetState) {
544
545 case NETLOOP_RESTART:
546#ifdef CONFIG_NET_MULTI
547 NetRestarted = 1;
548#endif
549 goto restart;
550
551 case NETLOOP_SUCCESS:
552 if (NetBootFileXferSize > 0) {
Wolfgang Denkf34024d2007-09-12 00:48:57 +0200553 char buf[20];
wdenk2d966952002-10-31 22:12:35 +0000554 printf("Bytes transferred = %ld (%lx hex)\n",
555 NetBootFileXferSize,
556 NetBootFileXferSize);
Wolfgang Denkf34024d2007-09-12 00:48:57 +0200557 sprintf(buf, "%lX", NetBootFileXferSize);
wdenk2d966952002-10-31 22:12:35 +0000558 setenv("filesize", buf);
wdenka3d991b2004-04-15 21:48:45 +0000559
560 sprintf(buf, "%lX", (unsigned long)load_addr);
561 setenv("fileaddr", buf);
wdenk2d966952002-10-31 22:12:35 +0000562 }
563 eth_halt();
564 return NetBootFileXferSize;
565
566 case NETLOOP_FAIL:
wdenk1d0350e2002-11-11 21:14:20 +0000567 return (-1);
wdenk2d966952002-10-31 22:12:35 +0000568 }
569 }
570}
571
572/**********************************************************************/
573
574static void
575startAgainTimeout(void)
576{
577 NetState = NETLOOP_RESTART;
578}
579
580static void
581startAgainHandler(uchar * pkt, unsigned dest, unsigned src, unsigned len)
582{
583 /* Totally ignore the packet */
584}
585
wdenk6e592382004-04-18 17:39:38 +0000586void NetStartAgain (void)
wdenk2d966952002-10-31 22:12:35 +0000587{
wdenk6e592382004-04-18 17:39:38 +0000588 char *nretry;
589 int noretry = 0, once = 0;
wdenka3d991b2004-04-15 21:48:45 +0000590
wdenk6e592382004-04-18 17:39:38 +0000591 if ((nretry = getenv ("netretry")) != NULL) {
592 noretry = (strcmp (nretry, "no") == 0);
593 once = (strcmp (nretry, "once") == 0);
594 }
595 if (noretry) {
596 eth_halt ();
wdenka3d991b2004-04-15 21:48:45 +0000597 NetState = NETLOOP_FAIL;
598 return;
599 }
wdenk2d966952002-10-31 22:12:35 +0000600#ifndef CONFIG_NET_MULTI
Bartlomiej Sieka49f3bdb2008-10-01 15:26:28 +0200601 NetSetTimeout (10000UL, startAgainTimeout);
wdenk6e592382004-04-18 17:39:38 +0000602 NetSetHandler (startAgainHandler);
603#else /* !CONFIG_NET_MULTI*/
604 eth_halt ();
Matthias Fuchs8b0c5c12007-12-27 16:58:41 +0100605#if !defined(CONFIG_NET_DO_NOT_TRY_ANOTHER)
wdenk6e592382004-04-18 17:39:38 +0000606 eth_try_another (!NetRestarted);
Matthias Fuchs8b0c5c12007-12-27 16:58:41 +0100607#endif
wdenk6e592382004-04-18 17:39:38 +0000608 eth_init (gd->bd);
609 if (NetRestartWrap) {
wdenk2d966952002-10-31 22:12:35 +0000610 NetRestartWrap = 0;
wdenk6e592382004-04-18 17:39:38 +0000611 if (NetDevExists && !once) {
Bartlomiej Sieka49f3bdb2008-10-01 15:26:28 +0200612 NetSetTimeout (10000UL, startAgainTimeout);
wdenk6e592382004-04-18 17:39:38 +0000613 NetSetHandler (startAgainHandler);
614 } else {
wdenk2d966952002-10-31 22:12:35 +0000615 NetState = NETLOOP_FAIL;
616 }
wdenk6e592382004-04-18 17:39:38 +0000617 } else {
wdenk2d966952002-10-31 22:12:35 +0000618 NetState = NETLOOP_RESTART;
619 }
wdenk6e592382004-04-18 17:39:38 +0000620#endif /* CONFIG_NET_MULTI */
wdenk2d966952002-10-31 22:12:35 +0000621}
622
623/**********************************************************************/
624/*
625 * Miscelaneous bits.
626 */
627
628void
629NetSetHandler(rxhand_f * f)
630{
631 packetHandler = f;
632}
633
634
635void
wdenk3e01d752004-10-09 21:56:21 +0000636NetSetTimeout(ulong iv, thand_f * f)
wdenk2d966952002-10-31 22:12:35 +0000637{
638 if (iv == 0) {
639 timeHandler = (thand_f *)0;
640 } else {
641 timeHandler = f;
wdenke0ac62d2003-08-17 18:55:18 +0000642 timeStart = get_timer(0);
643 timeDelta = iv;
wdenk2d966952002-10-31 22:12:35 +0000644 }
645}
646
647
648void
649NetSendPacket(volatile uchar * pkt, int len)
650{
651 (void) eth_send(pkt, len);
652}
653
wdenk73a8b272003-06-05 19:27:42 +0000654int
655NetSendUDPPacket(uchar *ether, IPaddr_t dest, int dport, int sport, int len)
656{
wdenka3d991b2004-04-15 21:48:45 +0000657 uchar *pkt;
658
wdenk73a8b272003-06-05 19:27:42 +0000659 /* convert to new style broadcast */
660 if (dest == 0)
661 dest = 0xFFFFFFFF;
wdenk2d966952002-10-31 22:12:35 +0000662
wdenk73a8b272003-06-05 19:27:42 +0000663 /* if broadcast, make the ether address a broadcast and don't do ARP */
664 if (dest == 0xFFFFFFFF)
665 ether = NetBcastAddr;
666
667 /* if MAC address was not discovered yet, save the packet and do an ARP request */
668 if (memcmp(ether, NetEtherNullAddr, 6) == 0) {
669
670#ifdef ET_DEBUG
671 printf("sending ARP for %08lx\n", dest);
672#endif
wdenk73a8b272003-06-05 19:27:42 +0000673 NetArpWaitPacketIP = dest;
674 NetArpWaitPacketMAC = ether;
wdenka3d991b2004-04-15 21:48:45 +0000675
676 pkt = NetArpWaitTxPacket;
677 pkt += NetSetEther (pkt, NetArpWaitPacketMAC, PROT_IP);
678
679 NetSetIP (pkt, dest, dport, sport, len);
680 memcpy(pkt + IP_HDR_SIZE, (uchar *)NetTxPacket + (pkt - (uchar *)NetArpWaitTxPacket) + IP_HDR_SIZE, len);
wdenk73a8b272003-06-05 19:27:42 +0000681
682 /* size of the waiting packet */
wdenka3d991b2004-04-15 21:48:45 +0000683 NetArpWaitTxPacketSize = (pkt - NetArpWaitTxPacket) + IP_HDR_SIZE + len;
wdenk73a8b272003-06-05 19:27:42 +0000684
685 /* and do the ARP request */
686 NetArpWaitTry = 1;
687 NetArpWaitTimerStart = get_timer(0);
688 ArpRequest();
689 return 1; /* waiting */
690 }
691
692#ifdef ET_DEBUG
693 printf("sending UDP to %08lx/%02x:%02x:%02x:%02x:%02x:%02x\n",
wdenk6e592382004-04-18 17:39:38 +0000694 dest, ether[0], ether[1], ether[2], ether[3], ether[4], ether[5]);
wdenk73a8b272003-06-05 19:27:42 +0000695#endif
696
wdenka3d991b2004-04-15 21:48:45 +0000697 pkt = (uchar *)NetTxPacket;
698 pkt += NetSetEther (pkt, ether, PROT_IP);
699 NetSetIP (pkt, dest, dport, sport, len);
700 (void) eth_send(NetTxPacket, (pkt - NetTxPacket) + IP_HDR_SIZE + len);
wdenk73a8b272003-06-05 19:27:42 +0000701
wdenk6e592382004-04-18 17:39:38 +0000702 return 0; /* transmitted */
wdenk73a8b272003-06-05 19:27:42 +0000703}
704
Jon Loeliger643d1ab2007-07-09 17:45:14 -0500705#if defined(CONFIG_CMD_PING)
wdenk73a8b272003-06-05 19:27:42 +0000706static ushort PingSeqNo;
707
708int PingSend(void)
709{
710 static uchar mac[6];
711 volatile IP_t *ip;
712 volatile ushort *s;
wdenka3d991b2004-04-15 21:48:45 +0000713 uchar *pkt;
wdenk73a8b272003-06-05 19:27:42 +0000714
715 /* XXX always send arp request */
716
717 memcpy(mac, NetEtherNullAddr, 6);
718
719#ifdef ET_DEBUG
720 printf("sending ARP for %08lx\n", NetPingIP);
721#endif
722
723 NetArpWaitPacketIP = NetPingIP;
724 NetArpWaitPacketMAC = mac;
725
wdenka3d991b2004-04-15 21:48:45 +0000726 pkt = NetArpWaitTxPacket;
727 pkt += NetSetEther(pkt, mac, PROT_IP);
wdenk73a8b272003-06-05 19:27:42 +0000728
wdenka3d991b2004-04-15 21:48:45 +0000729 ip = (volatile IP_t *)pkt;
wdenk73a8b272003-06-05 19:27:42 +0000730
731 /*
732 * Construct an IP and ICMP header. (need to set no fragment bit - XXX)
733 */
734 ip->ip_hl_v = 0x45; /* IP_HDR_SIZE / 4 (not including UDP) */
735 ip->ip_tos = 0;
736 ip->ip_len = htons(IP_HDR_SIZE_NO_UDP + 8);
737 ip->ip_id = htons(NetIPID++);
738 ip->ip_off = htons(0x4000); /* No fragmentation */
739 ip->ip_ttl = 255;
740 ip->ip_p = 0x01; /* ICMP */
741 ip->ip_sum = 0;
742 NetCopyIP((void*)&ip->ip_src, &NetOurIP); /* already in network byte order */
743 NetCopyIP((void*)&ip->ip_dst, &NetPingIP); /* - "" - */
744 ip->ip_sum = ~NetCksum((uchar *)ip, IP_HDR_SIZE_NO_UDP / 2);
745
746 s = &ip->udp_src; /* XXX ICMP starts here */
747 s[0] = htons(0x0800); /* echo-request, code */
748 s[1] = 0; /* checksum */
Wolfgang Denk53677ef2008-05-20 16:00:29 +0200749 s[2] = 0; /* identifier */
wdenk73a8b272003-06-05 19:27:42 +0000750 s[3] = htons(PingSeqNo++); /* sequence number */
751 s[1] = ~NetCksum((uchar *)s, 8/2);
752
753 /* size of the waiting packet */
wdenka3d991b2004-04-15 21:48:45 +0000754 NetArpWaitTxPacketSize = (pkt - NetArpWaitTxPacket) + IP_HDR_SIZE_NO_UDP + 8;
wdenk73a8b272003-06-05 19:27:42 +0000755
756 /* and do the ARP request */
757 NetArpWaitTry = 1;
758 NetArpWaitTimerStart = get_timer(0);
759 ArpRequest();
760 return 1; /* waiting */
761}
762
763static void
764PingTimeout (void)
765{
766 eth_halt();
767 NetState = NETLOOP_FAIL; /* we did not get the reply */
768}
769
770static void
771PingHandler (uchar * pkt, unsigned dest, unsigned src, unsigned len)
772{
773 IPaddr_t tmp;
774 volatile IP_t *ip = (volatile IP_t *)pkt;
775
776 tmp = NetReadIP((void *)&ip->ip_src);
777 if (tmp != NetPingIP)
778 return;
779
780 NetState = NETLOOP_SUCCESS;
781}
782
783static void PingStart(void)
784{
wdenka3d991b2004-04-15 21:48:45 +0000785#if defined(CONFIG_NET_MULTI)
786 printf ("Using %s device\n", eth_get_name());
wdenk6e592382004-04-18 17:39:38 +0000787#endif /* CONFIG_NET_MULTI */
Bartlomiej Sieka49f3bdb2008-10-01 15:26:28 +0200788 NetSetTimeout (10000UL, PingTimeout);
wdenk73a8b272003-06-05 19:27:42 +0000789 NetSetHandler (PingHandler);
790
791 PingSend();
792}
Jon Loeliger610f2e92007-07-10 11:05:02 -0500793#endif
wdenk2d966952002-10-31 22:12:35 +0000794
Jon Loeliger643d1ab2007-07-09 17:45:14 -0500795#if defined(CONFIG_CMD_CDP)
wdenka3d991b2004-04-15 21:48:45 +0000796
797#define CDP_DEVICE_ID_TLV 0x0001
798#define CDP_ADDRESS_TLV 0x0002
799#define CDP_PORT_ID_TLV 0x0003
800#define CDP_CAPABILITIES_TLV 0x0004
801#define CDP_VERSION_TLV 0x0005
802#define CDP_PLATFORM_TLV 0x0006
803#define CDP_NATIVE_VLAN_TLV 0x000a
804#define CDP_APPLIANCE_VLAN_TLV 0x000e
805#define CDP_TRIGGER_TLV 0x000f
806#define CDP_POWER_CONSUMPTION_TLV 0x0010
807#define CDP_SYSNAME_TLV 0x0014
808#define CDP_SYSOBJECT_TLV 0x0015
809#define CDP_MANAGEMENT_ADDRESS_TLV 0x0016
810
Bartlomiej Sieka49f3bdb2008-10-01 15:26:28 +0200811#define CDP_TIMEOUT 250UL /* one packet every 250ms */
wdenka3d991b2004-04-15 21:48:45 +0000812
813static int CDPSeq;
814static int CDPOK;
815
816ushort CDPNativeVLAN;
817ushort CDPApplianceVLAN;
818
819static const uchar CDP_SNAP_hdr[8] = { 0xAA, 0xAA, 0x03, 0x00, 0x00, 0x0C, 0x20, 0x00 };
820
821static ushort CDP_compute_csum(const uchar *buff, ushort len)
822{
823 ushort csum;
824 int odd;
825 ulong result = 0;
826 ushort leftover;
Wolfgang Denk77ddac92005-10-13 16:45:02 +0200827 ushort *p;
wdenka3d991b2004-04-15 21:48:45 +0000828
829 if (len > 0) {
830 odd = 1 & (ulong)buff;
831 if (odd) {
832 result = *buff << 8;
833 len--;
834 buff++;
835 }
836 while (len > 1) {
Wolfgang Denk77ddac92005-10-13 16:45:02 +0200837 p = (ushort *)buff;
838 result += *p++;
839 buff = (uchar *)p;
wdenka3d991b2004-04-15 21:48:45 +0000840 if (result & 0x80000000)
841 result = (result & 0xFFFF) + (result >> 16);
842 len -= 2;
843 }
844 if (len) {
845 leftover = (signed short)(*(const signed char *)buff);
Wolfgang Denk3ada8342005-11-10 20:59:46 +0100846 /* CISCO SUCKS big time! (and blows too):
847 * CDP uses the IP checksum algorithm with a twist;
848 * for the last byte it *sign* extends and sums.
849 */
wdenka3d991b2004-04-15 21:48:45 +0000850 result = (result & 0xffff0000) | ((result + leftover) & 0x0000ffff);
851 }
852 while (result >> 16)
853 result = (result & 0xFFFF) + (result >> 16);
854
855 if (odd)
856 result = ((result >> 8) & 0xff) | ((result & 0xff) << 8);
857 }
858
859 /* add up 16-bit and 17-bit words for 17+c bits */
860 result = (result & 0xffff) + (result >> 16);
861 /* add up 16-bit and 2-bit for 16+c bit */
862 result = (result & 0xffff) + (result >> 16);
863 /* add up carry.. */
864 result = (result & 0xffff) + (result >> 16);
865
866 /* negate */
867 csum = ~(ushort)result;
868
869 /* run time endian detection */
870 if (csum != htons(csum)) /* little endian */
871 csum = htons(csum);
872
873 return csum;
874}
875
876int CDPSendTrigger(void)
877{
878 volatile uchar *pkt;
879 volatile ushort *s;
880 volatile ushort *cp;
881 Ethernet_t *et;
wdenka3d991b2004-04-15 21:48:45 +0000882 int len;
883 ushort chksum;
wdenk6e592382004-04-18 17:39:38 +0000884#if defined(CONFIG_CDP_DEVICE_ID) || defined(CONFIG_CDP_PORT_ID) || \
885 defined(CONFIG_CDP_VERSION) || defined(CONFIG_CDP_PLATFORM)
886 char buf[32];
887#endif
wdenka3d991b2004-04-15 21:48:45 +0000888
889 pkt = NetTxPacket;
890 et = (Ethernet_t *)pkt;
891
892 /* NOTE: trigger sent not on any VLAN */
893
894 /* form ethernet header */
895 memcpy(et->et_dest, NetCDPAddr, 6);
896 memcpy(et->et_src, NetOurEther, 6);
897
898 pkt += ETHER_HDR_SIZE;
899
900 /* SNAP header */
901 memcpy((uchar *)pkt, CDP_SNAP_hdr, sizeof(CDP_SNAP_hdr));
902 pkt += sizeof(CDP_SNAP_hdr);
903
904 /* CDP header */
905 *pkt++ = 0x02; /* CDP version 2 */
906 *pkt++ = 180; /* TTL */
907 s = (volatile ushort *)pkt;
908 cp = s;
909 *s++ = htons(0); /* checksum (0 for later calculation) */
910
911 /* CDP fields */
912#ifdef CONFIG_CDP_DEVICE_ID
913 *s++ = htons(CDP_DEVICE_ID_TLV);
914 *s++ = htons(CONFIG_CDP_DEVICE_ID);
915 memset(buf, 0, sizeof(buf));
916 sprintf(buf, CONFIG_CDP_DEVICE_ID_PREFIX "%02X%02X%02X%02X%02X%02X",
917 NetOurEther[0] & 0xff, NetOurEther[1] & 0xff,
918 NetOurEther[2] & 0xff, NetOurEther[3] & 0xff,
919 NetOurEther[4] & 0xff, NetOurEther[5] & 0xff);
920 memcpy((uchar *)s, buf, 16);
921 s += 16 / 2;
922#endif
923
924#ifdef CONFIG_CDP_PORT_ID
925 *s++ = htons(CDP_PORT_ID_TLV);
926 memset(buf, 0, sizeof(buf));
927 sprintf(buf, CONFIG_CDP_PORT_ID, eth_get_dev_index());
928 len = strlen(buf);
929 if (len & 1) /* make it even */
930 len++;
931 *s++ = htons(len + 4);
932 memcpy((uchar *)s, buf, len);
933 s += len / 2;
934#endif
935
936#ifdef CONFIG_CDP_CAPABILITIES
937 *s++ = htons(CDP_CAPABILITIES_TLV);
938 *s++ = htons(8);
939 *(ulong *)s = htonl(CONFIG_CDP_CAPABILITIES);
940 s += 2;
941#endif
942
943#ifdef CONFIG_CDP_VERSION
944 *s++ = htons(CDP_VERSION_TLV);
945 memset(buf, 0, sizeof(buf));
946 strcpy(buf, CONFIG_CDP_VERSION);
947 len = strlen(buf);
948 if (len & 1) /* make it even */
949 len++;
950 *s++ = htons(len + 4);
951 memcpy((uchar *)s, buf, len);
952 s += len / 2;
953#endif
954
955#ifdef CONFIG_CDP_PLATFORM
956 *s++ = htons(CDP_PLATFORM_TLV);
957 memset(buf, 0, sizeof(buf));
958 strcpy(buf, CONFIG_CDP_PLATFORM);
959 len = strlen(buf);
960 if (len & 1) /* make it even */
961 len++;
962 *s++ = htons(len + 4);
963 memcpy((uchar *)s, buf, len);
964 s += len / 2;
965#endif
966
967#ifdef CONFIG_CDP_TRIGGER
968 *s++ = htons(CDP_TRIGGER_TLV);
969 *s++ = htons(8);
970 *(ulong *)s = htonl(CONFIG_CDP_TRIGGER);
971 s += 2;
972#endif
973
974#ifdef CONFIG_CDP_POWER_CONSUMPTION
975 *s++ = htons(CDP_POWER_CONSUMPTION_TLV);
976 *s++ = htons(6);
977 *s++ = htons(CONFIG_CDP_POWER_CONSUMPTION);
978#endif
979
980 /* length of ethernet packet */
981 len = (uchar *)s - ((uchar *)NetTxPacket + ETHER_HDR_SIZE);
982 et->et_protlen = htons(len);
983
984 len = ETHER_HDR_SIZE + sizeof(CDP_SNAP_hdr);
985 chksum = CDP_compute_csum((uchar *)NetTxPacket + len, (uchar *)s - (NetTxPacket + len));
986 if (chksum == 0)
987 chksum = 0xFFFF;
988 *cp = htons(chksum);
989
990 (void) eth_send(NetTxPacket, (uchar *)s - NetTxPacket);
991 return 0;
992}
993
994static void
995CDPTimeout (void)
996{
997 CDPSeq++;
998
999 if (CDPSeq < 3) {
1000 NetSetTimeout (CDP_TIMEOUT, CDPTimeout);
1001 CDPSendTrigger();
1002 return;
1003 }
1004
1005 /* if not OK try again */
1006 if (!CDPOK)
1007 NetStartAgain();
1008 else
1009 NetState = NETLOOP_SUCCESS;
1010}
1011
1012static void
1013CDPDummyHandler (uchar * pkt, unsigned dest, unsigned src, unsigned len)
1014{
1015 /* nothing */
1016}
1017
1018static void
1019CDPHandler(const uchar * pkt, unsigned len)
1020{
1021 const uchar *t;
1022 const ushort *ss;
1023 ushort type, tlen;
1024 uchar applid;
1025 ushort vlan, nvlan;
1026
1027 /* minimum size? */
1028 if (len < sizeof(CDP_SNAP_hdr) + 4)
1029 goto pkt_short;
1030
1031 /* check for valid CDP SNAP header */
1032 if (memcmp(pkt, CDP_SNAP_hdr, sizeof(CDP_SNAP_hdr)) != 0)
1033 return;
1034
1035 pkt += sizeof(CDP_SNAP_hdr);
1036 len -= sizeof(CDP_SNAP_hdr);
1037
1038 /* Version of CDP protocol must be >= 2 and TTL != 0 */
1039 if (pkt[0] < 0x02 || pkt[1] == 0)
1040 return;
1041
1042 /* if version is greater than 0x02 maybe we'll have a problem; output a warning */
1043 if (pkt[0] != 0x02)
1044 printf("** WARNING: CDP packet received with a protocol version %d > 2\n",
1045 pkt[0] & 0xff);
1046
1047 if (CDP_compute_csum(pkt, len) != 0)
1048 return;
1049
1050 pkt += 4;
1051 len -= 4;
1052
1053 vlan = htons(-1);
1054 nvlan = htons(-1);
1055 while (len > 0) {
1056 if (len < 4)
1057 goto pkt_short;
1058
1059 ss = (const ushort *)pkt;
1060 type = ntohs(ss[0]);
1061 tlen = ntohs(ss[1]);
1062 if (tlen > len) {
1063 goto pkt_short;
1064 }
1065
1066 pkt += tlen;
1067 len -= tlen;
1068
1069 ss += 2; /* point ss to the data of the TLV */
1070 tlen -= 4;
1071
1072 switch (type) {
1073 case CDP_DEVICE_ID_TLV:
1074 break;
1075 case CDP_ADDRESS_TLV:
1076 break;
1077 case CDP_PORT_ID_TLV:
1078 break;
1079 case CDP_CAPABILITIES_TLV:
1080 break;
1081 case CDP_VERSION_TLV:
1082 break;
1083 case CDP_PLATFORM_TLV:
1084 break;
1085 case CDP_NATIVE_VLAN_TLV:
1086 nvlan = *ss;
1087 break;
1088 case CDP_APPLIANCE_VLAN_TLV:
1089 t = (const uchar *)ss;
1090 while (tlen > 0) {
1091 if (tlen < 3)
1092 goto pkt_short;
1093
1094 applid = t[0];
1095 ss = (const ushort *)(t + 1);
1096
1097#ifdef CONFIG_CDP_APPLIANCE_VLAN_TYPE
1098 if (applid == CONFIG_CDP_APPLIANCE_VLAN_TYPE)
1099 vlan = *ss;
1100#else
1101 vlan = ntohs(*ss); /* XXX will this work; dunno */
1102#endif
1103 t += 3; tlen -= 3;
1104 }
1105 break;
1106 case CDP_TRIGGER_TLV:
1107 break;
1108 case CDP_POWER_CONSUMPTION_TLV:
1109 break;
1110 case CDP_SYSNAME_TLV:
1111 break;
1112 case CDP_SYSOBJECT_TLV:
1113 break;
1114 case CDP_MANAGEMENT_ADDRESS_TLV:
1115 break;
1116 }
1117 }
1118
1119 CDPApplianceVLAN = vlan;
1120 CDPNativeVLAN = nvlan;
1121
1122 CDPOK = 1;
1123 return;
1124
1125 pkt_short:
1126 printf("** CDP packet is too short\n");
1127 return;
1128}
1129
1130static void CDPStart(void)
1131{
1132#if defined(CONFIG_NET_MULTI)
1133 printf ("Using %s device\n", eth_get_name());
1134#endif
1135 CDPSeq = 0;
1136 CDPOK = 0;
1137
1138 CDPNativeVLAN = htons(-1);
1139 CDPApplianceVLAN = htons(-1);
1140
1141 NetSetTimeout (CDP_TIMEOUT, CDPTimeout);
1142 NetSetHandler (CDPDummyHandler);
1143
1144 CDPSendTrigger();
1145}
Jon Loeliger610f2e92007-07-10 11:05:02 -05001146#endif
wdenka3d991b2004-04-15 21:48:45 +00001147
1148
wdenk2d966952002-10-31 22:12:35 +00001149void
wdenka3d991b2004-04-15 21:48:45 +00001150NetReceive(volatile uchar * inpkt, int len)
wdenk2d966952002-10-31 22:12:35 +00001151{
1152 Ethernet_t *et;
1153 IP_t *ip;
1154 ARP_t *arp;
1155 IPaddr_t tmp;
1156 int x;
wdenka3d991b2004-04-15 21:48:45 +00001157 uchar *pkt;
Jon Loeliger643d1ab2007-07-09 17:45:14 -05001158#if defined(CONFIG_CMD_CDP)
wdenka3d991b2004-04-15 21:48:45 +00001159 int iscdp;
1160#endif
1161 ushort cti = 0, vlanid = VLAN_NONE, myvlanid, mynvlanid;
wdenk2d966952002-10-31 22:12:35 +00001162
wdenka3d991b2004-04-15 21:48:45 +00001163#ifdef ET_DEBUG
1164 printf("packet received\n");
1165#endif
1166
1167 NetRxPkt = inpkt;
wdenk2d966952002-10-31 22:12:35 +00001168 NetRxPktLen = len;
wdenka3d991b2004-04-15 21:48:45 +00001169 et = (Ethernet_t *)inpkt;
1170
1171 /* too small packet? */
1172 if (len < ETHER_HDR_SIZE)
1173 return;
1174
Rafal Jaworowskif85b6072007-12-27 18:19:02 +01001175#ifdef CONFIG_API
1176 if (push_packet) {
1177 (*push_packet)(inpkt, len);
1178 return;
1179 }
1180#endif
1181
Jon Loeliger643d1ab2007-07-09 17:45:14 -05001182#if defined(CONFIG_CMD_CDP)
wdenka3d991b2004-04-15 21:48:45 +00001183 /* keep track if packet is CDP */
1184 iscdp = memcmp(et->et_dest, NetCDPAddr, 6) == 0;
1185#endif
1186
1187 myvlanid = ntohs(NetOurVLAN);
1188 if (myvlanid == (ushort)-1)
1189 myvlanid = VLAN_NONE;
1190 mynvlanid = ntohs(NetOurNativeVLAN);
1191 if (mynvlanid == (ushort)-1)
1192 mynvlanid = VLAN_NONE;
wdenk2d966952002-10-31 22:12:35 +00001193
1194 x = ntohs(et->et_protlen);
1195
wdenka3d991b2004-04-15 21:48:45 +00001196#ifdef ET_DEBUG
1197 printf("packet received\n");
1198#endif
1199
wdenk2d966952002-10-31 22:12:35 +00001200 if (x < 1514) {
1201 /*
1202 * Got a 802 packet. Check the other protocol field.
1203 */
1204 x = ntohs(et->et_prot);
wdenka3d991b2004-04-15 21:48:45 +00001205
1206 ip = (IP_t *)(inpkt + E802_HDR_SIZE);
wdenk2d966952002-10-31 22:12:35 +00001207 len -= E802_HDR_SIZE;
wdenka3d991b2004-04-15 21:48:45 +00001208
1209 } else if (x != PROT_VLAN) { /* normal packet */
1210 ip = (IP_t *)(inpkt + ETHER_HDR_SIZE);
wdenk2d966952002-10-31 22:12:35 +00001211 len -= ETHER_HDR_SIZE;
wdenka3d991b2004-04-15 21:48:45 +00001212
1213 } else { /* VLAN packet */
1214 VLAN_Ethernet_t *vet = (VLAN_Ethernet_t *)et;
1215
1216#ifdef ET_DEBUG
1217 printf("VLAN packet received\n");
1218#endif
1219 /* too small packet? */
1220 if (len < VLAN_ETHER_HDR_SIZE)
1221 return;
1222
1223 /* if no VLAN active */
1224 if ((ntohs(NetOurVLAN) & VLAN_IDMASK) == VLAN_NONE
Jon Loeliger643d1ab2007-07-09 17:45:14 -05001225#if defined(CONFIG_CMD_CDP)
wdenka3d991b2004-04-15 21:48:45 +00001226 && iscdp == 0
1227#endif
1228 )
1229 return;
1230
1231 cti = ntohs(vet->vet_tag);
1232 vlanid = cti & VLAN_IDMASK;
1233 x = ntohs(vet->vet_type);
1234
1235 ip = (IP_t *)(inpkt + VLAN_ETHER_HDR_SIZE);
1236 len -= VLAN_ETHER_HDR_SIZE;
wdenk2d966952002-10-31 22:12:35 +00001237 }
1238
1239#ifdef ET_DEBUG
1240 printf("Receive from protocol 0x%x\n", x);
1241#endif
1242
Jon Loeliger643d1ab2007-07-09 17:45:14 -05001243#if defined(CONFIG_CMD_CDP)
wdenka3d991b2004-04-15 21:48:45 +00001244 if (iscdp) {
1245 CDPHandler((uchar *)ip, len);
1246 return;
1247 }
1248#endif
1249
1250 if ((myvlanid & VLAN_IDMASK) != VLAN_NONE) {
1251 if (vlanid == VLAN_NONE)
1252 vlanid = (mynvlanid & VLAN_IDMASK);
1253 /* not matched? */
1254 if (vlanid != (myvlanid & VLAN_IDMASK))
1255 return;
1256 }
1257
wdenk2d966952002-10-31 22:12:35 +00001258 switch (x) {
1259
1260 case PROT_ARP:
1261 /*
1262 * We have to deal with two types of ARP packets:
wdenk8bde7f72003-06-27 21:31:46 +00001263 * - REQUEST packets will be answered by sending our
1264 * IP address - if we know it.
1265 * - REPLY packates are expected only after we asked
1266 * for the TFTP server's or the gateway's ethernet
1267 * address; so if we receive such a packet, we set
1268 * the server ethernet address
wdenk2d966952002-10-31 22:12:35 +00001269 */
1270#ifdef ET_DEBUG
wdenk4b9206e2004-03-23 22:14:11 +00001271 puts ("Got ARP\n");
wdenk2d966952002-10-31 22:12:35 +00001272#endif
1273 arp = (ARP_t *)ip;
1274 if (len < ARP_HDR_SIZE) {
1275 printf("bad length %d < %d\n", len, ARP_HDR_SIZE);
1276 return;
1277 }
1278 if (ntohs(arp->ar_hrd) != ARP_ETHER) {
1279 return;
1280 }
1281 if (ntohs(arp->ar_pro) != PROT_IP) {
1282 return;
1283 }
1284 if (arp->ar_hln != 6) {
1285 return;
1286 }
1287 if (arp->ar_pln != 4) {
1288 return;
1289 }
1290
1291 if (NetOurIP == 0) {
1292 return;
1293 }
1294
1295 if (NetReadIP(&arp->ar_data[16]) != NetOurIP) {
1296 return;
1297 }
1298
1299 switch (ntohs(arp->ar_op)) {
1300 case ARPOP_REQUEST: /* reply with our IP address */
1301#ifdef ET_DEBUG
wdenk4b9206e2004-03-23 22:14:11 +00001302 puts ("Got ARP REQUEST, return our IP\n");
wdenk2d966952002-10-31 22:12:35 +00001303#endif
wdenka3d991b2004-04-15 21:48:45 +00001304 pkt = (uchar *)et;
1305 pkt += NetSetEther(pkt, et->et_src, PROT_ARP);
wdenk2d966952002-10-31 22:12:35 +00001306 arp->ar_op = htons(ARPOP_REPLY);
1307 memcpy (&arp->ar_data[10], &arp->ar_data[0], 6);
1308 NetCopyIP(&arp->ar_data[16], &arp->ar_data[6]);
1309 memcpy (&arp->ar_data[ 0], NetOurEther, 6);
1310 NetCopyIP(&arp->ar_data[ 6], &NetOurIP);
wdenka3d991b2004-04-15 21:48:45 +00001311 (void) eth_send((uchar *)et, (pkt - (uchar *)et) + ARP_HDR_SIZE);
wdenk2d966952002-10-31 22:12:35 +00001312 return;
wdenk73a8b272003-06-05 19:27:42 +00001313
1314 case ARPOP_REPLY: /* arp reply */
1315 /* are we waiting for a reply */
1316 if (!NetArpWaitPacketIP || !NetArpWaitPacketMAC)
1317 break;
wdenk2d966952002-10-31 22:12:35 +00001318#ifdef ET_DEBUG
wdenk73a8b272003-06-05 19:27:42 +00001319 printf("Got ARP REPLY, set server/gtwy eth addr (%02x:%02x:%02x:%02x:%02x:%02x)\n",
1320 arp->ar_data[0], arp->ar_data[1],
1321 arp->ar_data[2], arp->ar_data[3],
1322 arp->ar_data[4], arp->ar_data[5]);
wdenk2d966952002-10-31 22:12:35 +00001323#endif
wdenk73a8b272003-06-05 19:27:42 +00001324
1325 tmp = NetReadIP(&arp->ar_data[6]);
1326
1327 /* matched waiting packet's address */
1328 if (tmp == NetArpWaitReplyIP) {
1329#ifdef ET_DEBUG
wdenk4b9206e2004-03-23 22:14:11 +00001330 puts ("Got it\n");
wdenk73a8b272003-06-05 19:27:42 +00001331#endif
1332 /* save address for later use */
1333 memcpy(NetArpWaitPacketMAC, &arp->ar_data[0], 6);
1334
wdenk68ceb292004-08-02 21:11:11 +00001335#ifdef CONFIG_NETCONSOLE
1336 (*packetHandler)(0,0,0,0);
1337#endif
wdenk73a8b272003-06-05 19:27:42 +00001338 /* modify header, and transmit it */
1339 memcpy(((Ethernet_t *)NetArpWaitTxPacket)->et_dest, NetArpWaitPacketMAC, 6);
1340 (void) eth_send(NetArpWaitTxPacket, NetArpWaitTxPacketSize);
1341
1342 /* no arp request pending now */
1343 NetArpWaitPacketIP = 0;
1344 NetArpWaitTxPacketSize = 0;
1345 NetArpWaitPacketMAC = NULL;
1346
1347 }
wdenk2d966952002-10-31 22:12:35 +00001348 return;
1349 default:
1350#ifdef ET_DEBUG
1351 printf("Unexpected ARP opcode 0x%x\n", ntohs(arp->ar_op));
1352#endif
1353 return;
1354 }
wdenk289f9322005-01-12 00:15:14 +00001355 break;
wdenk2d966952002-10-31 22:12:35 +00001356
1357 case PROT_RARP:
1358#ifdef ET_DEBUG
wdenk4b9206e2004-03-23 22:14:11 +00001359 puts ("Got RARP\n");
wdenk2d966952002-10-31 22:12:35 +00001360#endif
1361 arp = (ARP_t *)ip;
1362 if (len < ARP_HDR_SIZE) {
1363 printf("bad length %d < %d\n", len, ARP_HDR_SIZE);
1364 return;
1365 }
1366
1367 if ((ntohs(arp->ar_op) != RARPOP_REPLY) ||
1368 (ntohs(arp->ar_hrd) != ARP_ETHER) ||
1369 (ntohs(arp->ar_pro) != PROT_IP) ||
1370 (arp->ar_hln != 6) || (arp->ar_pln != 4)) {
1371
wdenk4b9206e2004-03-23 22:14:11 +00001372 puts ("invalid RARP header\n");
wdenk2d966952002-10-31 22:12:35 +00001373 } else {
1374 NetCopyIP(&NetOurIP, &arp->ar_data[16]);
wdenk3d3befa2004-03-14 15:06:13 +00001375 if (NetServerIP == 0)
1376 NetCopyIP(&NetServerIP, &arp->ar_data[ 6]);
wdenk2d966952002-10-31 22:12:35 +00001377 memcpy (NetServerEther, &arp->ar_data[ 0], 6);
1378
1379 (*packetHandler)(0,0,0,0);
1380 }
1381 break;
1382
1383 case PROT_IP:
1384#ifdef ET_DEBUG
wdenk4b9206e2004-03-23 22:14:11 +00001385 puts ("Got IP\n");
wdenk2d966952002-10-31 22:12:35 +00001386#endif
1387 if (len < IP_HDR_SIZE) {
Wolfgang Denk25dbe982008-07-13 23:07:35 +02001388 debug ("len bad %d < %lu\n", len, (ulong)IP_HDR_SIZE);
wdenk2d966952002-10-31 22:12:35 +00001389 return;
1390 }
1391 if (len < ntohs(ip->ip_len)) {
1392 printf("len bad %d < %d\n", len, ntohs(ip->ip_len));
1393 return;
1394 }
1395 len = ntohs(ip->ip_len);
1396#ifdef ET_DEBUG
1397 printf("len=%d, v=%02x\n", len, ip->ip_hl_v & 0xff);
1398#endif
1399 if ((ip->ip_hl_v & 0xf0) != 0x40) {
1400 return;
1401 }
1402 if (ip->ip_off & htons(0x1fff)) { /* Can't deal w/ fragments */
1403 return;
1404 }
Remy Bohmer6b52cfe2008-06-03 15:48:17 +02001405 /* can't deal with headers > 20 bytes */
1406 if ((ip->ip_hl_v & 0x0f) > 0x05) {
1407 return;
1408 }
wdenk2d966952002-10-31 22:12:35 +00001409 if (!NetCksumOk((uchar *)ip, IP_HDR_SIZE_NO_UDP / 2)) {
wdenk4b9206e2004-03-23 22:14:11 +00001410 puts ("checksum bad\n");
wdenk2d966952002-10-31 22:12:35 +00001411 return;
1412 }
1413 tmp = NetReadIP(&ip->ip_dst);
1414 if (NetOurIP && tmp != NetOurIP && tmp != 0xFFFFFFFF) {
David Updegraff53a5c422007-06-11 10:41:07 -05001415#ifdef CONFIG_MCAST_TFTP
Wolfgang Denk85eb5ca2007-08-14 09:47:27 +02001416 if (Mcast_addr != tmp)
David Updegraff53a5c422007-06-11 10:41:07 -05001417#endif
wdenk2d966952002-10-31 22:12:35 +00001418 return;
1419 }
1420 /*
1421 * watch for ICMP host redirects
1422 *
wdenk8bde7f72003-06-27 21:31:46 +00001423 * There is no real handler code (yet). We just watch
1424 * for ICMP host redirect messages. In case anybody
1425 * sees these messages: please contact me
1426 * (wd@denx.de), or - even better - send me the
1427 * necessary fixes :-)
wdenk2d966952002-10-31 22:12:35 +00001428 *
wdenk8bde7f72003-06-27 21:31:46 +00001429 * Note: in all cases where I have seen this so far
1430 * it was a problem with the router configuration,
1431 * for instance when a router was configured in the
1432 * BOOTP reply, but the TFTP server was on the same
1433 * subnet. So this is probably a warning that your
1434 * configuration might be wrong. But I'm not really
1435 * sure if there aren't any other situations.
wdenk2d966952002-10-31 22:12:35 +00001436 */
1437 if (ip->ip_p == IPPROTO_ICMP) {
1438 ICMP_t *icmph = (ICMP_t *)&(ip->udp_src);
1439
wdenk73a8b272003-06-05 19:27:42 +00001440 switch (icmph->type) {
1441 case ICMP_REDIRECT:
wdenk90dc6702005-05-03 14:12:25 +00001442 if (icmph->code != ICMP_REDIR_HOST)
1443 return;
1444 puts (" ICMP Host Redirect to ");
1445 print_IPaddr(icmph->un.gateway);
1446 putc(' ');
Stefan Roese8534bf92005-08-12 20:06:52 +02001447 return;
Jon Loeliger643d1ab2007-07-09 17:45:14 -05001448#if defined(CONFIG_CMD_PING)
wdenk73a8b272003-06-05 19:27:42 +00001449 case ICMP_ECHO_REPLY:
1450 /*
1451 * IP header OK. Pass the packet to the current handler.
1452 */
1453 /* XXX point to ip packet */
1454 (*packetHandler)((uchar *)ip, 0, 0, 0);
Stefan Roese8534bf92005-08-12 20:06:52 +02001455 return;
Ed Swarthout83853172007-03-07 12:14:50 -06001456 case ICMP_ECHO_REQUEST:
1457#ifdef ET_DEBUG
1458 printf ("Got ICMP ECHO REQUEST, return %d bytes \n",
1459 ETHER_HDR_SIZE + len);
1460#endif
1461 memcpy (&et->et_dest[0], &et->et_src[0], 6);
1462 memcpy (&et->et_src[ 0], NetOurEther, 6);
1463
1464 ip->ip_sum = 0;
1465 ip->ip_off = 0;
1466 NetCopyIP((void*)&ip->ip_dst, &ip->ip_src);
1467 NetCopyIP((void*)&ip->ip_src, &NetOurIP);
1468 ip->ip_sum = ~NetCksum((uchar *)ip, IP_HDR_SIZE_NO_UDP >> 1);
1469
1470 icmph->type = ICMP_ECHO_REPLY;
1471 icmph->checksum = 0;
1472 icmph->checksum = ~NetCksum((uchar *)icmph,
1473 (len - IP_HDR_SIZE_NO_UDP) >> 1);
1474 (void) eth_send((uchar *)et, ETHER_HDR_SIZE + len);
1475 return;
wdenk73a8b272003-06-05 19:27:42 +00001476#endif
1477 default:
1478 return;
1479 }
wdenk2d966952002-10-31 22:12:35 +00001480 } else if (ip->ip_p != IPPROTO_UDP) { /* Only UDP packets */
1481 return;
1482 }
1483
Stefan Roese8534bf92005-08-12 20:06:52 +02001484#ifdef CONFIG_UDP_CHECKSUM
1485 if (ip->udp_xsum != 0) {
Wolfgang Denkb2f50802005-08-12 23:43:12 +02001486 ulong xsum;
Stefan Roese8534bf92005-08-12 20:06:52 +02001487 ushort *sumptr;
1488 ushort sumlen;
1489
1490 xsum = ip->ip_p;
1491 xsum += (ntohs(ip->udp_len));
1492 xsum += (ntohl(ip->ip_src) >> 16) & 0x0000ffff;
1493 xsum += (ntohl(ip->ip_src) >> 0) & 0x0000ffff;
1494 xsum += (ntohl(ip->ip_dst) >> 16) & 0x0000ffff;
1495 xsum += (ntohl(ip->ip_dst) >> 0) & 0x0000ffff;
1496
1497 sumlen = ntohs(ip->udp_len);
1498 sumptr = (ushort *) &(ip->udp_src);
1499
1500 while (sumlen > 1) {
Wolfgang Denkb2f50802005-08-12 23:43:12 +02001501 ushort sumdata;
Stefan Roese8534bf92005-08-12 20:06:52 +02001502
1503 sumdata = *sumptr++;
1504 xsum += ntohs(sumdata);
1505 sumlen -= 2;
1506 }
1507 if (sumlen > 0) {
Wolfgang Denkb2f50802005-08-12 23:43:12 +02001508 ushort sumdata;
Stefan Roese8534bf92005-08-12 20:06:52 +02001509
1510 sumdata = *(unsigned char *) sumptr;
Wolfgang Denkb2f50802005-08-12 23:43:12 +02001511 sumdata = (sumdata << 8) & 0xff00;
Stefan Roese8534bf92005-08-12 20:06:52 +02001512 xsum += sumdata;
1513 }
1514 while ((xsum >> 16) != 0) {
Wolfgang Denkb2f50802005-08-12 23:43:12 +02001515 xsum = (xsum & 0x0000ffff) + ((xsum >> 16) & 0x0000ffff);
Stefan Roese8534bf92005-08-12 20:06:52 +02001516 }
1517 if ((xsum != 0x00000000) && (xsum != 0x0000ffff)) {
Wolfgang Denk9b55a252008-07-11 01:16:00 +02001518 printf(" UDP wrong checksum %08lx %08x\n",
1519 xsum, ntohs(ip->udp_xsum));
Stefan Roese8534bf92005-08-12 20:06:52 +02001520 return;
1521 }
1522 }
1523#endif
1524
David Updegraff53a5c422007-06-11 10:41:07 -05001525
wdenk68ceb292004-08-02 21:11:11 +00001526#ifdef CONFIG_NETCONSOLE
1527 nc_input_packet((uchar *)ip +IP_HDR_SIZE,
1528 ntohs(ip->udp_dst),
1529 ntohs(ip->udp_src),
1530 ntohs(ip->udp_len) - 8);
1531#endif
wdenk2d966952002-10-31 22:12:35 +00001532 /*
1533 * IP header OK. Pass the packet to the current handler.
1534 */
1535 (*packetHandler)((uchar *)ip +IP_HDR_SIZE,
1536 ntohs(ip->udp_dst),
1537 ntohs(ip->udp_src),
1538 ntohs(ip->udp_len) - 8);
wdenk2d966952002-10-31 22:12:35 +00001539 break;
1540 }
1541}
1542
1543
1544/**********************************************************************/
1545
1546static int net_check_prereq (proto_t protocol)
1547{
1548 switch (protocol) {
wdenk6e592382004-04-18 17:39:38 +00001549 /* Fall through */
Jon Loeliger643d1ab2007-07-09 17:45:14 -05001550#if defined(CONFIG_CMD_PING)
wdenk73a8b272003-06-05 19:27:42 +00001551 case PING:
wdenk6e592382004-04-18 17:39:38 +00001552 if (NetPingIP == 0) {
1553 puts ("*** ERROR: ping address not given\n");
1554 return (1);
1555 }
1556 goto common;
wdenk73a8b272003-06-05 19:27:42 +00001557#endif
Jon Loeliger643d1ab2007-07-09 17:45:14 -05001558#if defined(CONFIG_CMD_SNTP)
wdenkea287de2005-04-01 00:25:43 +00001559 case SNTP:
1560 if (NetNtpServerIP == 0) {
1561 puts ("*** ERROR: NTP server address not given\n");
1562 return (1);
1563 }
1564 goto common;
1565#endif
Jon Loeliger643d1ab2007-07-09 17:45:14 -05001566#if defined(CONFIG_CMD_NFS)
wdenkcbd8a352004-02-24 02:00:03 +00001567 case NFS:
1568#endif
wdenk68ceb292004-08-02 21:11:11 +00001569 case NETCONS:
wdenk2d966952002-10-31 22:12:35 +00001570 case TFTP:
wdenk6e592382004-04-18 17:39:38 +00001571 if (NetServerIP == 0) {
1572 puts ("*** ERROR: `serverip' not set\n");
1573 return (1);
1574 }
Jon Loeliger643d1ab2007-07-09 17:45:14 -05001575#if defined(CONFIG_CMD_PING) || defined(CONFIG_CMD_SNTP)
Wolfgang Denkb2f50802005-08-12 23:43:12 +02001576 common:
wdenk73a8b272003-06-05 19:27:42 +00001577#endif
1578
wdenk6e592382004-04-18 17:39:38 +00001579 if (NetOurIP == 0) {
1580 puts ("*** ERROR: `ipaddr' not set\n");
1581 return (1);
1582 }
1583 /* Fall through */
wdenk2d966952002-10-31 22:12:35 +00001584
1585 case DHCP:
1586 case RARP:
1587 case BOOTP:
wdenka3d991b2004-04-15 21:48:45 +00001588 case CDP:
wdenk6e592382004-04-18 17:39:38 +00001589 if (memcmp (NetOurEther, "\0\0\0\0\0\0", 6) == 0) {
wdenk2d966952002-10-31 22:12:35 +00001590#ifdef CONFIG_NET_MULTI
wdenk6e592382004-04-18 17:39:38 +00001591 extern int eth_get_dev_index (void);
1592 int num = eth_get_dev_index ();
wdenk2d966952002-10-31 22:12:35 +00001593
wdenk6e592382004-04-18 17:39:38 +00001594 switch (num) {
1595 case -1:
wdenk2d966952002-10-31 22:12:35 +00001596 puts ("*** ERROR: No ethernet found.\n");
1597 return (1);
wdenk6e592382004-04-18 17:39:38 +00001598 case 0:
wdenk2d966952002-10-31 22:12:35 +00001599 puts ("*** ERROR: `ethaddr' not set\n");
1600 break;
wdenk6e592382004-04-18 17:39:38 +00001601 default:
wdenk8bde7f72003-06-27 21:31:46 +00001602 printf ("*** ERROR: `eth%daddr' not set\n",
wdenk2d966952002-10-31 22:12:35 +00001603 num);
1604 break;
wdenk2d966952002-10-31 22:12:35 +00001605 }
wdenk6e592382004-04-18 17:39:38 +00001606
1607 NetStartAgain ();
1608 return (2);
1609#else
1610 puts ("*** ERROR: `ethaddr' not set\n");
1611 return (1);
1612#endif
1613 }
1614 /* Fall through */
1615 default:
1616 return (0);
wdenk2d966952002-10-31 22:12:35 +00001617 }
wdenk6e592382004-04-18 17:39:38 +00001618 return (0); /* OK */
wdenk2d966952002-10-31 22:12:35 +00001619}
1620/**********************************************************************/
1621
1622int
1623NetCksumOk(uchar * ptr, int len)
1624{
1625 return !((NetCksum(ptr, len) + 1) & 0xfffe);
1626}
1627
1628
1629unsigned
1630NetCksum(uchar * ptr, int len)
1631{
1632 ulong xsum;
Stefan Roese9d2a8732005-08-31 12:55:50 +02001633 ushort *p = (ushort *)ptr;
wdenk2d966952002-10-31 22:12:35 +00001634
1635 xsum = 0;
1636 while (len-- > 0)
Wolfgang Denk7bc5ee02005-08-26 01:36:03 +02001637 xsum += *p++;
wdenk2d966952002-10-31 22:12:35 +00001638 xsum = (xsum & 0xffff) + (xsum >> 16);
1639 xsum = (xsum & 0xffff) + (xsum >> 16);
1640 return (xsum & 0xffff);
1641}
1642
wdenka3d991b2004-04-15 21:48:45 +00001643int
1644NetEthHdrSize(void)
1645{
1646 ushort myvlanid;
wdenk2d966952002-10-31 22:12:35 +00001647
wdenka3d991b2004-04-15 21:48:45 +00001648 myvlanid = ntohs(NetOurVLAN);
1649 if (myvlanid == (ushort)-1)
1650 myvlanid = VLAN_NONE;
1651
1652 return ((myvlanid & VLAN_IDMASK) == VLAN_NONE) ? ETHER_HDR_SIZE : VLAN_ETHER_HDR_SIZE;
1653}
1654
1655int
wdenk2d966952002-10-31 22:12:35 +00001656NetSetEther(volatile uchar * xet, uchar * addr, uint prot)
1657{
1658 Ethernet_t *et = (Ethernet_t *)xet;
wdenka3d991b2004-04-15 21:48:45 +00001659 ushort myvlanid;
1660
1661 myvlanid = ntohs(NetOurVLAN);
1662 if (myvlanid == (ushort)-1)
1663 myvlanid = VLAN_NONE;
wdenk2d966952002-10-31 22:12:35 +00001664
1665 memcpy (et->et_dest, addr, 6);
1666 memcpy (et->et_src, NetOurEther, 6);
wdenka3d991b2004-04-15 21:48:45 +00001667 if ((myvlanid & VLAN_IDMASK) == VLAN_NONE) {
wdenk2d966952002-10-31 22:12:35 +00001668 et->et_protlen = htons(prot);
wdenka3d991b2004-04-15 21:48:45 +00001669 return ETHER_HDR_SIZE;
1670 } else {
1671 VLAN_Ethernet_t *vet = (VLAN_Ethernet_t *)xet;
wdenk2d966952002-10-31 22:12:35 +00001672
wdenka3d991b2004-04-15 21:48:45 +00001673 vet->vet_vlan_type = htons(PROT_VLAN);
1674 vet->vet_tag = htons((0 << 5) | (myvlanid & VLAN_IDMASK));
1675 vet->vet_type = htons(prot);
1676 return VLAN_ETHER_HDR_SIZE;
1677 }
1678}
wdenk2d966952002-10-31 22:12:35 +00001679
1680void
1681NetSetIP(volatile uchar * xip, IPaddr_t dest, int dport, int sport, int len)
1682{
1683 volatile IP_t *ip = (IP_t *)xip;
1684
1685 /*
1686 * If the data is an odd number of bytes, zero the
1687 * byte after the last byte so that the checksum
1688 * will work.
1689 */
1690 if (len & 1)
1691 xip[IP_HDR_SIZE + len] = 0;
1692
1693 /*
1694 * Construct an IP and UDP header.
wdenk6e592382004-04-18 17:39:38 +00001695 * (need to set no fragment bit - XXX)
wdenk2d966952002-10-31 22:12:35 +00001696 */
1697 ip->ip_hl_v = 0x45; /* IP_HDR_SIZE / 4 (not including UDP) */
1698 ip->ip_tos = 0;
1699 ip->ip_len = htons(IP_HDR_SIZE + len);
1700 ip->ip_id = htons(NetIPID++);
1701 ip->ip_off = htons(0x4000); /* No fragmentation */
1702 ip->ip_ttl = 255;
1703 ip->ip_p = 17; /* UDP */
1704 ip->ip_sum = 0;
1705 NetCopyIP((void*)&ip->ip_src, &NetOurIP); /* already in network byte order */
1706 NetCopyIP((void*)&ip->ip_dst, &dest); /* - "" - */
1707 ip->udp_src = htons(sport);
1708 ip->udp_dst = htons(dport);
1709 ip->udp_len = htons(8 + len);
1710 ip->udp_xsum = 0;
1711 ip->ip_sum = ~NetCksum((uchar *)ip, IP_HDR_SIZE_NO_UDP / 2);
1712}
1713
Wolfgang Denk77ddac92005-10-13 16:45:02 +02001714void copy_filename (char *dst, char *src, int size)
wdenk2d966952002-10-31 22:12:35 +00001715{
1716 if (*src && (*src == '"')) {
1717 ++src;
1718 --size;
1719 }
1720
1721 while ((--size > 0) && *src && (*src != '"')) {
1722 *dst++ = *src++;
1723 }
1724 *dst = '\0';
1725}
1726
Jon Loeliger610f2e92007-07-10 11:05:02 -05001727#endif
wdenk2d966952002-10-31 22:12:35 +00001728
1729void ip_to_string (IPaddr_t x, char *s)
1730{
wdenka3d991b2004-04-15 21:48:45 +00001731 x = ntohl (x);
1732 sprintf (s, "%d.%d.%d.%d",
1733 (int) ((x >> 24) & 0xff),
1734 (int) ((x >> 16) & 0xff),
1735 (int) ((x >> 8) & 0xff), (int) ((x >> 0) & 0xff)
wdenk6e592382004-04-18 17:39:38 +00001736 );
wdenk2d966952002-10-31 22:12:35 +00001737}
1738
wdenk73a8b272003-06-05 19:27:42 +00001739IPaddr_t string_to_ip(char *s)
wdenk2d966952002-10-31 22:12:35 +00001740{
1741 IPaddr_t addr;
wdenk73a8b272003-06-05 19:27:42 +00001742 char *e;
wdenk2d966952002-10-31 22:12:35 +00001743 int i;
1744
wdenk73a8b272003-06-05 19:27:42 +00001745 if (s == NULL)
1746 return(0);
wdenk2d966952002-10-31 22:12:35 +00001747
1748 for (addr=0, i=0; i<4; ++i) {
1749 ulong val = s ? simple_strtoul(s, &e, 10) : 0;
1750 addr <<= 8;
1751 addr |= (val & 0xFF);
1752 if (s) {
1753 s = (*e) ? e+1 : e;
1754 }
1755 }
1756
1757 return (htonl(addr));
1758}
wdenk73a8b272003-06-05 19:27:42 +00001759
wdenka3d991b2004-04-15 21:48:45 +00001760void VLAN_to_string(ushort x, char *s)
1761{
1762 x = ntohs(x);
1763
1764 if (x == (ushort)-1)
1765 x = VLAN_NONE;
1766
1767 if (x == VLAN_NONE)
1768 strcpy(s, "none");
1769 else
1770 sprintf(s, "%d", x & VLAN_IDMASK);
1771}
1772
1773ushort string_to_VLAN(char *s)
1774{
1775 ushort id;
1776
1777 if (s == NULL)
wdenkb9711de2004-04-25 13:18:40 +00001778 return htons(VLAN_NONE);
wdenka3d991b2004-04-15 21:48:45 +00001779
1780 if (*s < '0' || *s > '9')
1781 id = VLAN_NONE;
1782 else
1783 id = (ushort)simple_strtoul(s, NULL, 10);
1784
wdenkb9711de2004-04-25 13:18:40 +00001785 return htons(id);
wdenka3d991b2004-04-15 21:48:45 +00001786}
1787
wdenk73a8b272003-06-05 19:27:42 +00001788void print_IPaddr (IPaddr_t x)
1789{
wdenka3d991b2004-04-15 21:48:45 +00001790 char tmp[16];
wdenk73a8b272003-06-05 19:27:42 +00001791
wdenka3d991b2004-04-15 21:48:45 +00001792 ip_to_string (x, tmp);
wdenk73a8b272003-06-05 19:27:42 +00001793
wdenka3d991b2004-04-15 21:48:45 +00001794 puts (tmp);
wdenk73a8b272003-06-05 19:27:42 +00001795}
1796
1797IPaddr_t getenv_IPaddr (char *var)
1798{
1799 return (string_to_ip(getenv(var)));
1800}
wdenka3d991b2004-04-15 21:48:45 +00001801
1802ushort getenv_VLAN(char *var)
1803{
1804 return (string_to_VLAN(getenv(var)));
1805}