blob: d7523fd1006208117366ed618c76e1a9690ced47 [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 *
43 * Prerequisites: - own ethernet address
44 * We want: - IP, Netmask, ServerIP, Gateway IP
45 * - bootfilename, lease time
46 * Next step: - TFTP
47 *
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
wdenk2d966952002-10-31 22:12:35 +000067 */
68
69
70#include <common.h>
71#include <watchdog.h>
72#include <command.h>
73#include <net.h>
74#include "bootp.h"
75#include "tftp.h"
76#include "rarp.h"
wdenkcbd8a352004-02-24 02:00:03 +000077#include "nfs.h"
wdenkfc3e2162003-10-08 22:33:00 +000078#ifdef CONFIG_STATUS_LED
79#include <status_led.h>
80#include <miiphy.h>
81#endif
wdenk2d966952002-10-31 22:12:35 +000082
83#if (CONFIG_COMMANDS & CFG_CMD_NET)
84
wdenk73a8b272003-06-05 19:27:42 +000085#define ARP_TIMEOUT 5 /* Seconds before trying ARP again */
86#ifndef CONFIG_NET_RETRY_COUNT
87# define ARP_TIMEOUT_COUNT 5 /* # of timeouts before giving up */
88#else
89# define ARP_TIMEOUT_COUNT (CONFIG_NET_RETRY_COUNT)
90#endif
91
wdenk2d966952002-10-31 22:12:35 +000092#if 0
93#define ET_DEBUG
94#endif
95
96/** BOOTP EXTENTIONS **/
97
98IPaddr_t NetOurSubnetMask=0; /* Our subnet mask (0=unknown) */
99IPaddr_t NetOurGatewayIP=0; /* Our gateways IP address */
100IPaddr_t NetOurDNSIP=0; /* Our DNS IP address */
stroesefe389a82003-08-28 14:17:32 +0000101#if (CONFIG_BOOTP_MASK & CONFIG_BOOTP_DNS2)
102IPaddr_t NetOurDNS2IP=0; /* Our 2nd DNS IP address */
103#endif
wdenk2d966952002-10-31 22:12:35 +0000104char NetOurNISDomain[32]={0,}; /* Our NIS domain */
105char NetOurHostName[32]={0,}; /* Our hostname */
106char NetOurRootPath[64]={0,}; /* Our bootpath */
107ushort NetBootFileSize=0; /* Our bootfile size in blocks */
108
109/** END OF BOOTP EXTENTIONS **/
110
111ulong NetBootFileXferSize; /* The actual transferred size of the bootfile (in bytes) */
112uchar NetOurEther[6]; /* Our ethernet address */
113uchar NetServerEther[6] = /* Boot server enet address */
wdenk73a8b272003-06-05 19:27:42 +0000114 { 0, 0, 0, 0, 0, 0 };
wdenk2d966952002-10-31 22:12:35 +0000115IPaddr_t NetOurIP; /* Our IP addr (0 = unknown) */
116IPaddr_t NetServerIP; /* Our IP addr (0 = unknown) */
117volatile uchar *NetRxPkt; /* Current receive packet */
118int NetRxPktLen; /* Current rx packet length */
119unsigned NetIPID; /* IP packet ID */
120uchar NetBcastAddr[6] = /* Ethernet bcast address */
121 { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
wdenk73a8b272003-06-05 19:27:42 +0000122uchar NetEtherNullAddr[6] =
123 { 0, 0, 0, 0, 0, 0 };
wdenka3d991b2004-04-15 21:48:45 +0000124#if (CONFIG_COMMANDS & CFG_CMD_CDP)
wdenk6e592382004-04-18 17:39:38 +0000125uchar NetCDPAddr[6] = /* Ethernet bcast address */
wdenka3d991b2004-04-15 21:48:45 +0000126 { 0x01, 0x00, 0x0c, 0xcc, 0xcc, 0xcc };
127#endif
wdenk2d966952002-10-31 22:12:35 +0000128int NetState; /* Network loop state */
129#ifdef CONFIG_NET_MULTI
130int NetRestartWrap = 0; /* Tried all network devices */
131static int NetRestarted = 0; /* Network loop restarted */
132static int NetDevExists = 0; /* At least one device configured */
133#endif
134
wdenk6e592382004-04-18 17:39:38 +0000135/* XXX in both little & big endian machines 0xFFFF == ntohs(-1) */
136ushort NetOurVLAN = 0xFFFF; /* default is without VLAN */
137ushort NetOurNativeVLAN = 0xFFFF; /* ditto */
wdenka3d991b2004-04-15 21:48:45 +0000138
wdenk2d966952002-10-31 22:12:35 +0000139char BootFile[128]; /* Boot File name */
140
wdenk73a8b272003-06-05 19:27:42 +0000141#if (CONFIG_COMMANDS & CFG_CMD_PING)
142IPaddr_t NetPingIP; /* the ip address to ping */
143
144static void PingStart(void);
145#endif
146
wdenka3d991b2004-04-15 21:48:45 +0000147#if (CONFIG_COMMANDS & CFG_CMD_CDP)
148static void CDPStart(void);
149#endif
150
wdenk68ceb292004-08-02 21:11:11 +0000151#ifdef CONFIG_NETCONSOLE
152void NcStart(void);
153int nc_input_packet(uchar *pkt, unsigned dest, unsigned src, unsigned len);
154#endif
155
wdenk2d966952002-10-31 22:12:35 +0000156volatile uchar PktBuf[(PKTBUFSRX+1) * PKTSIZE_ALIGN + PKTALIGN];
157
158volatile uchar *NetRxPackets[PKTBUFSRX]; /* Receive packets */
159
160static rxhand_f *packetHandler; /* Current RX packet handler */
161static thand_f *timeHandler; /* Current timeout handler */
wdenke0ac62d2003-08-17 18:55:18 +0000162static ulong timeStart; /* Time base value */
163static ulong timeDelta; /* Current timeout value */
wdenk2d966952002-10-31 22:12:35 +0000164volatile uchar *NetTxPacket = 0; /* THE transmit packet */
165
166static int net_check_prereq (proto_t protocol);
167
168/**********************************************************************/
wdenk73a8b272003-06-05 19:27:42 +0000169
170IPaddr_t NetArpWaitPacketIP;
171IPaddr_t NetArpWaitReplyIP;
172uchar *NetArpWaitPacketMAC; /* MAC address of waiting packet's destination */
173uchar *NetArpWaitTxPacket; /* THE transmit packet */
174int NetArpWaitTxPacketSize;
175uchar NetArpWaitPacketBuf[PKTSIZE_ALIGN + PKTALIGN];
176ulong NetArpWaitTimerStart;
177int NetArpWaitTry;
178
wdenk6e592382004-04-18 17:39:38 +0000179void ArpRequest (void)
wdenk73a8b272003-06-05 19:27:42 +0000180{
181 int i;
182 volatile uchar *pkt;
wdenk6e592382004-04-18 17:39:38 +0000183 ARP_t *arp;
wdenk73a8b272003-06-05 19:27:42 +0000184
185#ifdef ET_DEBUG
wdenk6e592382004-04-18 17:39:38 +0000186 printf ("ARP broadcast %d\n", NetArpWaitTry);
wdenk73a8b272003-06-05 19:27:42 +0000187#endif
188 pkt = NetTxPacket;
189
wdenk6e592382004-04-18 17:39:38 +0000190 pkt += NetSetEther (pkt, NetBcastAddr, PROT_ARP);
wdenk73a8b272003-06-05 19:27:42 +0000191
wdenk6e592382004-04-18 17:39:38 +0000192 arp = (ARP_t *) pkt;
wdenk73a8b272003-06-05 19:27:42 +0000193
wdenk6e592382004-04-18 17:39:38 +0000194 arp->ar_hrd = htons (ARP_ETHER);
195 arp->ar_pro = htons (PROT_IP);
wdenk73a8b272003-06-05 19:27:42 +0000196 arp->ar_hln = 6;
197 arp->ar_pln = 4;
wdenk6e592382004-04-18 17:39:38 +0000198 arp->ar_op = htons (ARPOP_REQUEST);
wdenk73a8b272003-06-05 19:27:42 +0000199
wdenk6e592382004-04-18 17:39:38 +0000200 memcpy (&arp->ar_data[0], NetOurEther, 6); /* source ET addr */
201 NetWriteIP ((uchar *) & arp->ar_data[6], NetOurIP); /* source IP addr */
202 for (i = 10; i < 16; ++i) {
203 arp->ar_data[i] = 0; /* dest ET addr = 0 */
wdenk73a8b272003-06-05 19:27:42 +0000204 }
205
wdenk6e592382004-04-18 17:39:38 +0000206 if ((NetArpWaitPacketIP & NetOurSubnetMask) !=
207 (NetOurIP & NetOurSubnetMask)) {
208 if (NetOurGatewayIP == 0) {
209 puts ("## Warning: gatewayip needed but not set\n");
210 }
211 NetArpWaitReplyIP = NetOurGatewayIP;
212 } else {
213 NetArpWaitReplyIP = NetArpWaitPacketIP;
214 }
wdenk73a8b272003-06-05 19:27:42 +0000215
wdenk6e592382004-04-18 17:39:38 +0000216 NetWriteIP ((uchar *) & arp->ar_data[16], NetArpWaitReplyIP);
217 (void) eth_send (NetTxPacket, (pkt - NetTxPacket) + ARP_HDR_SIZE);
wdenk73a8b272003-06-05 19:27:42 +0000218}
219
220void ArpTimeoutCheck(void)
221{
222 ulong t;
223
224 if (!NetArpWaitPacketIP)
225 return;
226
227 t = get_timer(0);
228
229 /* check for arp timeout */
230 if ((t - NetArpWaitTimerStart) > ARP_TIMEOUT * CFG_HZ) {
231 NetArpWaitTry++;
232
233 if (NetArpWaitTry >= ARP_TIMEOUT_COUNT) {
234 puts ("\nARP Retry count exceeded; starting again\n");
235 NetArpWaitTry = 0;
236 NetStartAgain();
237 } else {
238 NetArpWaitTimerStart = t;
239 ArpRequest();
240 }
241 }
242}
243
244/**********************************************************************/
wdenk2d966952002-10-31 22:12:35 +0000245/*
246 * Main network processing loop.
247 */
248
249int
250NetLoop(proto_t protocol)
251{
252 DECLARE_GLOBAL_DATA_PTR;
253
254 bd_t *bd = gd->bd;
255
256#ifdef CONFIG_NET_MULTI
257 NetRestarted = 0;
258 NetDevExists = 0;
259#endif
260
wdenk73a8b272003-06-05 19:27:42 +0000261 /* XXX problem with bss workaround */
262 NetArpWaitPacketMAC = NULL;
263 NetArpWaitTxPacket = NULL;
264 NetArpWaitPacketIP = 0;
265 NetArpWaitReplyIP = 0;
266 NetArpWaitTxPacket = NULL;
267 NetTxPacket = NULL;
268
wdenk2d966952002-10-31 22:12:35 +0000269 if (!NetTxPacket) {
270 int i;
wdenk2d966952002-10-31 22:12:35 +0000271 /*
272 * Setup packet buffers, aligned correctly.
273 */
274 NetTxPacket = &PktBuf[0] + (PKTALIGN - 1);
275 NetTxPacket -= (ulong)NetTxPacket % PKTALIGN;
276 for (i = 0; i < PKTBUFSRX; i++) {
277 NetRxPackets[i] = NetTxPacket + (i+1)*PKTSIZE_ALIGN;
278 }
wdenk73a8b272003-06-05 19:27:42 +0000279 }
280
281 if (!NetArpWaitTxPacket) {
282 NetArpWaitTxPacket = &NetArpWaitPacketBuf[0] + (PKTALIGN - 1);
283 NetArpWaitTxPacket -= (ulong)NetArpWaitTxPacket % PKTALIGN;
284 NetArpWaitTxPacketSize = 0;
wdenk2d966952002-10-31 22:12:35 +0000285 }
286
287 eth_halt();
wdenk6e592382004-04-18 17:39:38 +0000288#ifdef CONFIG_NET_MULTI
wdenka3d991b2004-04-15 21:48:45 +0000289 eth_set_current();
wdenk6e592382004-04-18 17:39:38 +0000290#endif
291 if (eth_init(bd) < 0)
292 return(-1);
wdenk2d966952002-10-31 22:12:35 +0000293
294restart:
295#ifdef CONFIG_NET_MULTI
296 memcpy (NetOurEther, eth_get_dev()->enetaddr, 6);
297#else
298 memcpy (NetOurEther, bd->bi_enetaddr, 6);
299#endif
300
301 NetState = NETLOOP_CONTINUE;
302
303 /*
304 * Start the ball rolling with the given start function. From
305 * here on, this code is a state machine driven by received
306 * packets and timer events.
307 */
308
309 switch (protocol) {
wdenkcbd8a352004-02-24 02:00:03 +0000310#if (CONFIG_COMMANDS & CFG_CMD_NFS)
311 case NFS:
312#endif
wdenk73a8b272003-06-05 19:27:42 +0000313#if (CONFIG_COMMANDS & CFG_CMD_PING)
314 case PING:
315#endif
wdenk68ceb292004-08-02 21:11:11 +0000316 case NETCONS:
wdenk2d966952002-10-31 22:12:35 +0000317 case TFTP:
318 NetCopyIP(&NetOurIP, &bd->bi_ip_addr);
wdenk2d966952002-10-31 22:12:35 +0000319 NetOurGatewayIP = getenv_IPaddr ("gatewayip");
320 NetOurSubnetMask= getenv_IPaddr ("netmask");
wdenka3d991b2004-04-15 21:48:45 +0000321 NetOurVLAN = getenv_VLAN("vlan");
322 NetOurNativeVLAN = getenv_VLAN("nvlan");
wdenk73a8b272003-06-05 19:27:42 +0000323
324 switch (protocol) {
wdenkcbd8a352004-02-24 02:00:03 +0000325#if (CONFIG_COMMANDS & CFG_CMD_NFS)
326 case NFS:
327#endif
wdenk68ceb292004-08-02 21:11:11 +0000328 case NETCONS:
wdenk73a8b272003-06-05 19:27:42 +0000329 case TFTP:
330 NetServerIP = getenv_IPaddr ("serverip");
331 break;
332#if (CONFIG_COMMANDS & CFG_CMD_PING)
333 case PING:
334 /* nothing */
335 break;
336#endif
337 default:
338 break;
339 }
340
wdenk2d966952002-10-31 22:12:35 +0000341 break;
342 case BOOTP:
343 case RARP:
344 /*
wdenk8bde7f72003-06-27 21:31:46 +0000345 * initialize our IP addr to 0 in order to accept ANY
346 * IP addr assigned to us by the BOOTP / RARP server
wdenk2d966952002-10-31 22:12:35 +0000347 */
348 NetOurIP = 0;
wdenk3d3befa2004-03-14 15:06:13 +0000349 NetServerIP = getenv_IPaddr ("serverip");
wdenka3d991b2004-04-15 21:48:45 +0000350 NetOurVLAN = getenv_VLAN("vlan"); /* VLANs must be read */
351 NetOurNativeVLAN = getenv_VLAN("nvlan");
352 case CDP:
353 NetOurVLAN = getenv_VLAN("vlan"); /* VLANs must be read */
354 NetOurNativeVLAN = getenv_VLAN("nvlan");
wdenk2d966952002-10-31 22:12:35 +0000355 break;
356 default:
357 break;
358 }
359
360 switch (net_check_prereq (protocol)) {
361 case 1:
362 /* network not configured */
wdenk1d0350e2002-11-11 21:14:20 +0000363 return (-1);
wdenk2d966952002-10-31 22:12:35 +0000364
365#ifdef CONFIG_NET_MULTI
366 case 2:
367 /* network device not configured */
368 break;
369#endif /* CONFIG_NET_MULTI */
370
371 case 0:
372#ifdef CONFIG_NET_MULTI
373 NetDevExists = 1;
374#endif
375 switch (protocol) {
376 case TFTP:
377 /* always use ARP to get server ethernet address */
wdenk73a8b272003-06-05 19:27:42 +0000378 TftpStart();
wdenk2d966952002-10-31 22:12:35 +0000379 break;
380
381#if (CONFIG_COMMANDS & CFG_CMD_DHCP)
382 case DHCP:
383 /* Start with a clean slate... */
wdenkd407bf52004-10-11 22:51:13 +0000384 BootpTry = 0;
wdenk2d966952002-10-31 22:12:35 +0000385 NetOurIP = 0;
wdenk3d3befa2004-03-14 15:06:13 +0000386 NetServerIP = getenv_IPaddr ("serverip");
wdenk2d966952002-10-31 22:12:35 +0000387 DhcpRequest(); /* Basically same as BOOTP */
388 break;
389#endif /* CFG_CMD_DHCP */
390
391 case BOOTP:
392 BootpTry = 0;
393 BootpRequest ();
394 break;
395
396 case RARP:
397 RarpTry = 0;
398 RarpRequest ();
399 break;
wdenk73a8b272003-06-05 19:27:42 +0000400#if (CONFIG_COMMANDS & CFG_CMD_PING)
401 case PING:
402 PingStart();
403 break;
404#endif
wdenkcbd8a352004-02-24 02:00:03 +0000405#if (CONFIG_COMMANDS & CFG_CMD_NFS)
406 case NFS:
407 NfsStart();
408 break;
409#endif
wdenka3d991b2004-04-15 21:48:45 +0000410#if (CONFIG_COMMANDS & CFG_CMD_CDP)
411 case CDP:
412 CDPStart();
413 break;
414#endif
wdenk68ceb292004-08-02 21:11:11 +0000415#ifdef CONFIG_NETCONSOLE
416 case NETCONS:
417 NcStart();
418 break;
419#endif
wdenk2d966952002-10-31 22:12:35 +0000420 default:
421 break;
422 }
423
424 NetBootFileXferSize = 0;
425 break;
426 }
427
wdenkfc3e2162003-10-08 22:33:00 +0000428#if defined(CONFIG_MII) || (CONFIG_COMMANDS & CFG_CMD_MII)
429#if defined(CFG_FAULT_ECHO_LINK_DOWN) && defined(CONFIG_STATUS_LED) && defined(STATUS_LED_RED)
430 /*
wdenk42d1f032003-10-15 23:53:47 +0000431 * Echo the inverted link state to the fault LED.
wdenkfc3e2162003-10-08 22:33:00 +0000432 */
433 if(miiphy_link(CFG_FAULT_MII_ADDR)) {
434 status_led_set (STATUS_LED_RED, STATUS_LED_OFF);
435 } else {
436 status_led_set (STATUS_LED_RED, STATUS_LED_ON);
437 }
438#endif /* CFG_FAULT_ECHO_LINK_DOWN, ... */
439#endif /* CONFIG_MII, ... */
wdenk2d966952002-10-31 22:12:35 +0000440
441 /*
442 * Main packet reception loop. Loop receiving packets until
443 * someone sets `NetQuit'.
444 */
445 for (;;) {
446 WATCHDOG_RESET();
447#ifdef CONFIG_SHOW_ACTIVITY
448 {
449 extern void show_activity(int arg);
450 show_activity(1);
451 }
452#endif
453 /*
454 * Check the ethernet for a new packet. The ethernet
455 * receive routine will process it.
456 */
457 eth_rx();
458
459 /*
460 * Abort if ctrl-c was pressed.
461 */
462 if (ctrlc()) {
wdenk8bde7f72003-06-27 21:31:46 +0000463 eth_halt();
wdenk4b9206e2004-03-23 22:14:11 +0000464 puts ("\nAbort\n");
wdenk1d0350e2002-11-11 21:14:20 +0000465 return (-1);
wdenk2d966952002-10-31 22:12:35 +0000466 }
467
wdenk73a8b272003-06-05 19:27:42 +0000468 ArpTimeoutCheck();
wdenk2d966952002-10-31 22:12:35 +0000469
470 /*
471 * Check for a timeout, and run the timeout handler
472 * if we have one.
473 */
wdenke0ac62d2003-08-17 18:55:18 +0000474 if (timeHandler && ((get_timer(0) - timeStart) > timeDelta)) {
wdenk2d966952002-10-31 22:12:35 +0000475 thand_f *x;
476
wdenkfc3e2162003-10-08 22:33:00 +0000477#if defined(CONFIG_MII) || (CONFIG_COMMANDS & CFG_CMD_MII)
478#if defined(CFG_FAULT_ECHO_LINK_DOWN) && defined(CONFIG_STATUS_LED) && defined(STATUS_LED_RED)
479 /*
wdenk42d1f032003-10-15 23:53:47 +0000480 * Echo the inverted link state to the fault LED.
wdenkfc3e2162003-10-08 22:33:00 +0000481 */
482 if(miiphy_link(CFG_FAULT_MII_ADDR)) {
483 status_led_set (STATUS_LED_RED, STATUS_LED_OFF);
484 } else {
485 status_led_set (STATUS_LED_RED, STATUS_LED_ON);
486 }
487#endif /* CFG_FAULT_ECHO_LINK_DOWN, ... */
488#endif /* CONFIG_MII, ... */
wdenk2d966952002-10-31 22:12:35 +0000489 x = timeHandler;
490 timeHandler = (thand_f *)0;
491 (*x)();
492 }
493
494
495 switch (NetState) {
496
497 case NETLOOP_RESTART:
498#ifdef CONFIG_NET_MULTI
499 NetRestarted = 1;
500#endif
501 goto restart;
502
503 case NETLOOP_SUCCESS:
504 if (NetBootFileXferSize > 0) {
505 char buf[10];
506 printf("Bytes transferred = %ld (%lx hex)\n",
507 NetBootFileXferSize,
508 NetBootFileXferSize);
509 sprintf(buf, "%lx", NetBootFileXferSize);
510 setenv("filesize", buf);
wdenka3d991b2004-04-15 21:48:45 +0000511
512 sprintf(buf, "%lX", (unsigned long)load_addr);
513 setenv("fileaddr", buf);
wdenk2d966952002-10-31 22:12:35 +0000514 }
515 eth_halt();
516 return NetBootFileXferSize;
517
518 case NETLOOP_FAIL:
wdenk1d0350e2002-11-11 21:14:20 +0000519 return (-1);
wdenk2d966952002-10-31 22:12:35 +0000520 }
521 }
522}
523
524/**********************************************************************/
525
526static void
527startAgainTimeout(void)
528{
529 NetState = NETLOOP_RESTART;
530}
531
532static void
533startAgainHandler(uchar * pkt, unsigned dest, unsigned src, unsigned len)
534{
535 /* Totally ignore the packet */
536}
537
wdenk6e592382004-04-18 17:39:38 +0000538void NetStartAgain (void)
wdenk2d966952002-10-31 22:12:35 +0000539{
wdenk6e592382004-04-18 17:39:38 +0000540#ifdef CONFIG_NET_MULTI
wdenka3d991b2004-04-15 21:48:45 +0000541 DECLARE_GLOBAL_DATA_PTR;
wdenk6e592382004-04-18 17:39:38 +0000542#endif
543 char *nretry;
544 int noretry = 0, once = 0;
wdenka3d991b2004-04-15 21:48:45 +0000545
wdenk6e592382004-04-18 17:39:38 +0000546 if ((nretry = getenv ("netretry")) != NULL) {
547 noretry = (strcmp (nretry, "no") == 0);
548 once = (strcmp (nretry, "once") == 0);
549 }
550 if (noretry) {
551 eth_halt ();
wdenka3d991b2004-04-15 21:48:45 +0000552 NetState = NETLOOP_FAIL;
553 return;
554 }
wdenk2d966952002-10-31 22:12:35 +0000555#ifndef CONFIG_NET_MULTI
wdenk6e592382004-04-18 17:39:38 +0000556 NetSetTimeout (10 * CFG_HZ, startAgainTimeout);
557 NetSetHandler (startAgainHandler);
558#else /* !CONFIG_NET_MULTI*/
559 eth_halt ();
560 eth_try_another (!NetRestarted);
561 eth_init (gd->bd);
562 if (NetRestartWrap) {
wdenk2d966952002-10-31 22:12:35 +0000563 NetRestartWrap = 0;
wdenk6e592382004-04-18 17:39:38 +0000564 if (NetDevExists && !once) {
565 NetSetTimeout (10 * CFG_HZ, startAgainTimeout);
566 NetSetHandler (startAgainHandler);
567 } else {
wdenk2d966952002-10-31 22:12:35 +0000568 NetState = NETLOOP_FAIL;
569 }
wdenk6e592382004-04-18 17:39:38 +0000570 } else {
wdenk2d966952002-10-31 22:12:35 +0000571 NetState = NETLOOP_RESTART;
572 }
wdenk6e592382004-04-18 17:39:38 +0000573#endif /* CONFIG_NET_MULTI */
wdenk2d966952002-10-31 22:12:35 +0000574}
575
576/**********************************************************************/
577/*
578 * Miscelaneous bits.
579 */
580
581void
582NetSetHandler(rxhand_f * f)
583{
584 packetHandler = f;
585}
586
587
588void
wdenk3e01d752004-10-09 21:56:21 +0000589NetSetTimeout(ulong iv, thand_f * f)
wdenk2d966952002-10-31 22:12:35 +0000590{
591 if (iv == 0) {
592 timeHandler = (thand_f *)0;
593 } else {
594 timeHandler = f;
wdenke0ac62d2003-08-17 18:55:18 +0000595 timeStart = get_timer(0);
596 timeDelta = iv;
wdenk2d966952002-10-31 22:12:35 +0000597 }
598}
599
600
601void
602NetSendPacket(volatile uchar * pkt, int len)
603{
604 (void) eth_send(pkt, len);
605}
606
wdenk73a8b272003-06-05 19:27:42 +0000607int
608NetSendUDPPacket(uchar *ether, IPaddr_t dest, int dport, int sport, int len)
609{
wdenka3d991b2004-04-15 21:48:45 +0000610 uchar *pkt;
611
wdenk73a8b272003-06-05 19:27:42 +0000612 /* convert to new style broadcast */
613 if (dest == 0)
614 dest = 0xFFFFFFFF;
wdenk2d966952002-10-31 22:12:35 +0000615
wdenk73a8b272003-06-05 19:27:42 +0000616 /* if broadcast, make the ether address a broadcast and don't do ARP */
617 if (dest == 0xFFFFFFFF)
618 ether = NetBcastAddr;
619
620 /* if MAC address was not discovered yet, save the packet and do an ARP request */
621 if (memcmp(ether, NetEtherNullAddr, 6) == 0) {
622
623#ifdef ET_DEBUG
624 printf("sending ARP for %08lx\n", dest);
625#endif
wdenk73a8b272003-06-05 19:27:42 +0000626 NetArpWaitPacketIP = dest;
627 NetArpWaitPacketMAC = ether;
wdenka3d991b2004-04-15 21:48:45 +0000628
629 pkt = NetArpWaitTxPacket;
630 pkt += NetSetEther (pkt, NetArpWaitPacketMAC, PROT_IP);
631
632 NetSetIP (pkt, dest, dport, sport, len);
633 memcpy(pkt + IP_HDR_SIZE, (uchar *)NetTxPacket + (pkt - (uchar *)NetArpWaitTxPacket) + IP_HDR_SIZE, len);
wdenk73a8b272003-06-05 19:27:42 +0000634
635 /* size of the waiting packet */
wdenka3d991b2004-04-15 21:48:45 +0000636 NetArpWaitTxPacketSize = (pkt - NetArpWaitTxPacket) + IP_HDR_SIZE + len;
wdenk73a8b272003-06-05 19:27:42 +0000637
638 /* and do the ARP request */
639 NetArpWaitTry = 1;
640 NetArpWaitTimerStart = get_timer(0);
641 ArpRequest();
642 return 1; /* waiting */
643 }
644
645#ifdef ET_DEBUG
646 printf("sending UDP to %08lx/%02x:%02x:%02x:%02x:%02x:%02x\n",
wdenk6e592382004-04-18 17:39:38 +0000647 dest, ether[0], ether[1], ether[2], ether[3], ether[4], ether[5]);
wdenk73a8b272003-06-05 19:27:42 +0000648#endif
649
wdenka3d991b2004-04-15 21:48:45 +0000650 pkt = (uchar *)NetTxPacket;
651 pkt += NetSetEther (pkt, ether, PROT_IP);
652 NetSetIP (pkt, dest, dport, sport, len);
653 (void) eth_send(NetTxPacket, (pkt - NetTxPacket) + IP_HDR_SIZE + len);
wdenk73a8b272003-06-05 19:27:42 +0000654
wdenk6e592382004-04-18 17:39:38 +0000655 return 0; /* transmitted */
wdenk73a8b272003-06-05 19:27:42 +0000656}
657
658#if (CONFIG_COMMANDS & CFG_CMD_PING)
659static ushort PingSeqNo;
660
661int PingSend(void)
662{
663 static uchar mac[6];
664 volatile IP_t *ip;
665 volatile ushort *s;
wdenka3d991b2004-04-15 21:48:45 +0000666 uchar *pkt;
wdenk73a8b272003-06-05 19:27:42 +0000667
668 /* XXX always send arp request */
669
670 memcpy(mac, NetEtherNullAddr, 6);
671
672#ifdef ET_DEBUG
673 printf("sending ARP for %08lx\n", NetPingIP);
674#endif
675
676 NetArpWaitPacketIP = NetPingIP;
677 NetArpWaitPacketMAC = mac;
678
wdenka3d991b2004-04-15 21:48:45 +0000679 pkt = NetArpWaitTxPacket;
680 pkt += NetSetEther(pkt, mac, PROT_IP);
wdenk73a8b272003-06-05 19:27:42 +0000681
wdenka3d991b2004-04-15 21:48:45 +0000682 ip = (volatile IP_t *)pkt;
wdenk73a8b272003-06-05 19:27:42 +0000683
684 /*
685 * Construct an IP and ICMP header. (need to set no fragment bit - XXX)
686 */
687 ip->ip_hl_v = 0x45; /* IP_HDR_SIZE / 4 (not including UDP) */
688 ip->ip_tos = 0;
689 ip->ip_len = htons(IP_HDR_SIZE_NO_UDP + 8);
690 ip->ip_id = htons(NetIPID++);
691 ip->ip_off = htons(0x4000); /* No fragmentation */
692 ip->ip_ttl = 255;
693 ip->ip_p = 0x01; /* ICMP */
694 ip->ip_sum = 0;
695 NetCopyIP((void*)&ip->ip_src, &NetOurIP); /* already in network byte order */
696 NetCopyIP((void*)&ip->ip_dst, &NetPingIP); /* - "" - */
697 ip->ip_sum = ~NetCksum((uchar *)ip, IP_HDR_SIZE_NO_UDP / 2);
698
699 s = &ip->udp_src; /* XXX ICMP starts here */
700 s[0] = htons(0x0800); /* echo-request, code */
701 s[1] = 0; /* checksum */
702 s[2] = 0; /* identifier */
703 s[3] = htons(PingSeqNo++); /* sequence number */
704 s[1] = ~NetCksum((uchar *)s, 8/2);
705
706 /* size of the waiting packet */
wdenka3d991b2004-04-15 21:48:45 +0000707 NetArpWaitTxPacketSize = (pkt - NetArpWaitTxPacket) + IP_HDR_SIZE_NO_UDP + 8;
wdenk73a8b272003-06-05 19:27:42 +0000708
709 /* and do the ARP request */
710 NetArpWaitTry = 1;
711 NetArpWaitTimerStart = get_timer(0);
712 ArpRequest();
713 return 1; /* waiting */
714}
715
716static void
717PingTimeout (void)
718{
719 eth_halt();
720 NetState = NETLOOP_FAIL; /* we did not get the reply */
721}
722
723static void
724PingHandler (uchar * pkt, unsigned dest, unsigned src, unsigned len)
725{
726 IPaddr_t tmp;
727 volatile IP_t *ip = (volatile IP_t *)pkt;
728
729 tmp = NetReadIP((void *)&ip->ip_src);
730 if (tmp != NetPingIP)
731 return;
732
733 NetState = NETLOOP_SUCCESS;
734}
735
736static void PingStart(void)
737{
wdenka3d991b2004-04-15 21:48:45 +0000738#if defined(CONFIG_NET_MULTI)
739 printf ("Using %s device\n", eth_get_name());
wdenk6e592382004-04-18 17:39:38 +0000740#endif /* CONFIG_NET_MULTI */
wdenk73a8b272003-06-05 19:27:42 +0000741 NetSetTimeout (10 * CFG_HZ, PingTimeout);
742 NetSetHandler (PingHandler);
743
744 PingSend();
745}
wdenk6e592382004-04-18 17:39:38 +0000746#endif /* CFG_CMD_PING */
wdenk2d966952002-10-31 22:12:35 +0000747
wdenka3d991b2004-04-15 21:48:45 +0000748#if (CONFIG_COMMANDS & CFG_CMD_CDP)
749
750#define CDP_DEVICE_ID_TLV 0x0001
751#define CDP_ADDRESS_TLV 0x0002
752#define CDP_PORT_ID_TLV 0x0003
753#define CDP_CAPABILITIES_TLV 0x0004
754#define CDP_VERSION_TLV 0x0005
755#define CDP_PLATFORM_TLV 0x0006
756#define CDP_NATIVE_VLAN_TLV 0x000a
757#define CDP_APPLIANCE_VLAN_TLV 0x000e
758#define CDP_TRIGGER_TLV 0x000f
759#define CDP_POWER_CONSUMPTION_TLV 0x0010
760#define CDP_SYSNAME_TLV 0x0014
761#define CDP_SYSOBJECT_TLV 0x0015
762#define CDP_MANAGEMENT_ADDRESS_TLV 0x0016
763
764#define CDP_TIMEOUT (CFG_HZ/4) /* one packet every 250ms */
765
766static int CDPSeq;
767static int CDPOK;
768
769ushort CDPNativeVLAN;
770ushort CDPApplianceVLAN;
771
772static const uchar CDP_SNAP_hdr[8] = { 0xAA, 0xAA, 0x03, 0x00, 0x00, 0x0C, 0x20, 0x00 };
773
774static ushort CDP_compute_csum(const uchar *buff, ushort len)
775{
776 ushort csum;
777 int odd;
778 ulong result = 0;
779 ushort leftover;
780
781 if (len > 0) {
782 odd = 1 & (ulong)buff;
783 if (odd) {
784 result = *buff << 8;
785 len--;
786 buff++;
787 }
788 while (len > 1) {
789 result += *((const ushort *)buff)++;
790 if (result & 0x80000000)
791 result = (result & 0xFFFF) + (result >> 16);
792 len -= 2;
793 }
794 if (len) {
795 leftover = (signed short)(*(const signed char *)buff);
796 /* * XXX CISCO SUCKS big time! (and blows too) */
797 result = (result & 0xffff0000) | ((result + leftover) & 0x0000ffff);
798 }
799 while (result >> 16)
800 result = (result & 0xFFFF) + (result >> 16);
801
802 if (odd)
803 result = ((result >> 8) & 0xff) | ((result & 0xff) << 8);
804 }
805
806 /* add up 16-bit and 17-bit words for 17+c bits */
807 result = (result & 0xffff) + (result >> 16);
808 /* add up 16-bit and 2-bit for 16+c bit */
809 result = (result & 0xffff) + (result >> 16);
810 /* add up carry.. */
811 result = (result & 0xffff) + (result >> 16);
812
813 /* negate */
814 csum = ~(ushort)result;
815
816 /* run time endian detection */
817 if (csum != htons(csum)) /* little endian */
818 csum = htons(csum);
819
820 return csum;
821}
822
823int CDPSendTrigger(void)
824{
825 volatile uchar *pkt;
826 volatile ushort *s;
827 volatile ushort *cp;
828 Ethernet_t *et;
wdenka3d991b2004-04-15 21:48:45 +0000829 int len;
830 ushort chksum;
wdenk6e592382004-04-18 17:39:38 +0000831#if defined(CONFIG_CDP_DEVICE_ID) || defined(CONFIG_CDP_PORT_ID) || \
832 defined(CONFIG_CDP_VERSION) || defined(CONFIG_CDP_PLATFORM)
833 char buf[32];
834#endif
wdenka3d991b2004-04-15 21:48:45 +0000835
836 pkt = NetTxPacket;
837 et = (Ethernet_t *)pkt;
838
839 /* NOTE: trigger sent not on any VLAN */
840
841 /* form ethernet header */
842 memcpy(et->et_dest, NetCDPAddr, 6);
843 memcpy(et->et_src, NetOurEther, 6);
844
845 pkt += ETHER_HDR_SIZE;
846
847 /* SNAP header */
848 memcpy((uchar *)pkt, CDP_SNAP_hdr, sizeof(CDP_SNAP_hdr));
849 pkt += sizeof(CDP_SNAP_hdr);
850
851 /* CDP header */
852 *pkt++ = 0x02; /* CDP version 2 */
853 *pkt++ = 180; /* TTL */
854 s = (volatile ushort *)pkt;
855 cp = s;
856 *s++ = htons(0); /* checksum (0 for later calculation) */
857
858 /* CDP fields */
859#ifdef CONFIG_CDP_DEVICE_ID
860 *s++ = htons(CDP_DEVICE_ID_TLV);
861 *s++ = htons(CONFIG_CDP_DEVICE_ID);
862 memset(buf, 0, sizeof(buf));
863 sprintf(buf, CONFIG_CDP_DEVICE_ID_PREFIX "%02X%02X%02X%02X%02X%02X",
864 NetOurEther[0] & 0xff, NetOurEther[1] & 0xff,
865 NetOurEther[2] & 0xff, NetOurEther[3] & 0xff,
866 NetOurEther[4] & 0xff, NetOurEther[5] & 0xff);
867 memcpy((uchar *)s, buf, 16);
868 s += 16 / 2;
869#endif
870
871#ifdef CONFIG_CDP_PORT_ID
872 *s++ = htons(CDP_PORT_ID_TLV);
873 memset(buf, 0, sizeof(buf));
874 sprintf(buf, CONFIG_CDP_PORT_ID, eth_get_dev_index());
875 len = strlen(buf);
876 if (len & 1) /* make it even */
877 len++;
878 *s++ = htons(len + 4);
879 memcpy((uchar *)s, buf, len);
880 s += len / 2;
881#endif
882
883#ifdef CONFIG_CDP_CAPABILITIES
884 *s++ = htons(CDP_CAPABILITIES_TLV);
885 *s++ = htons(8);
886 *(ulong *)s = htonl(CONFIG_CDP_CAPABILITIES);
887 s += 2;
888#endif
889
890#ifdef CONFIG_CDP_VERSION
891 *s++ = htons(CDP_VERSION_TLV);
892 memset(buf, 0, sizeof(buf));
893 strcpy(buf, CONFIG_CDP_VERSION);
894 len = strlen(buf);
895 if (len & 1) /* make it even */
896 len++;
897 *s++ = htons(len + 4);
898 memcpy((uchar *)s, buf, len);
899 s += len / 2;
900#endif
901
902#ifdef CONFIG_CDP_PLATFORM
903 *s++ = htons(CDP_PLATFORM_TLV);
904 memset(buf, 0, sizeof(buf));
905 strcpy(buf, CONFIG_CDP_PLATFORM);
906 len = strlen(buf);
907 if (len & 1) /* make it even */
908 len++;
909 *s++ = htons(len + 4);
910 memcpy((uchar *)s, buf, len);
911 s += len / 2;
912#endif
913
914#ifdef CONFIG_CDP_TRIGGER
915 *s++ = htons(CDP_TRIGGER_TLV);
916 *s++ = htons(8);
917 *(ulong *)s = htonl(CONFIG_CDP_TRIGGER);
918 s += 2;
919#endif
920
921#ifdef CONFIG_CDP_POWER_CONSUMPTION
922 *s++ = htons(CDP_POWER_CONSUMPTION_TLV);
923 *s++ = htons(6);
924 *s++ = htons(CONFIG_CDP_POWER_CONSUMPTION);
925#endif
926
927 /* length of ethernet packet */
928 len = (uchar *)s - ((uchar *)NetTxPacket + ETHER_HDR_SIZE);
929 et->et_protlen = htons(len);
930
931 len = ETHER_HDR_SIZE + sizeof(CDP_SNAP_hdr);
932 chksum = CDP_compute_csum((uchar *)NetTxPacket + len, (uchar *)s - (NetTxPacket + len));
933 if (chksum == 0)
934 chksum = 0xFFFF;
935 *cp = htons(chksum);
936
937 (void) eth_send(NetTxPacket, (uchar *)s - NetTxPacket);
938 return 0;
939}
940
941static void
942CDPTimeout (void)
943{
944 CDPSeq++;
945
946 if (CDPSeq < 3) {
947 NetSetTimeout (CDP_TIMEOUT, CDPTimeout);
948 CDPSendTrigger();
949 return;
950 }
951
952 /* if not OK try again */
953 if (!CDPOK)
954 NetStartAgain();
955 else
956 NetState = NETLOOP_SUCCESS;
957}
958
959static void
960CDPDummyHandler (uchar * pkt, unsigned dest, unsigned src, unsigned len)
961{
962 /* nothing */
963}
964
965static void
966CDPHandler(const uchar * pkt, unsigned len)
967{
968 const uchar *t;
969 const ushort *ss;
970 ushort type, tlen;
971 uchar applid;
972 ushort vlan, nvlan;
973
974 /* minimum size? */
975 if (len < sizeof(CDP_SNAP_hdr) + 4)
976 goto pkt_short;
977
978 /* check for valid CDP SNAP header */
979 if (memcmp(pkt, CDP_SNAP_hdr, sizeof(CDP_SNAP_hdr)) != 0)
980 return;
981
982 pkt += sizeof(CDP_SNAP_hdr);
983 len -= sizeof(CDP_SNAP_hdr);
984
985 /* Version of CDP protocol must be >= 2 and TTL != 0 */
986 if (pkt[0] < 0x02 || pkt[1] == 0)
987 return;
988
989 /* if version is greater than 0x02 maybe we'll have a problem; output a warning */
990 if (pkt[0] != 0x02)
991 printf("** WARNING: CDP packet received with a protocol version %d > 2\n",
992 pkt[0] & 0xff);
993
994 if (CDP_compute_csum(pkt, len) != 0)
995 return;
996
997 pkt += 4;
998 len -= 4;
999
1000 vlan = htons(-1);
1001 nvlan = htons(-1);
1002 while (len > 0) {
1003 if (len < 4)
1004 goto pkt_short;
1005
1006 ss = (const ushort *)pkt;
1007 type = ntohs(ss[0]);
1008 tlen = ntohs(ss[1]);
1009 if (tlen > len) {
1010 goto pkt_short;
1011 }
1012
1013 pkt += tlen;
1014 len -= tlen;
1015
1016 ss += 2; /* point ss to the data of the TLV */
1017 tlen -= 4;
1018
1019 switch (type) {
1020 case CDP_DEVICE_ID_TLV:
1021 break;
1022 case CDP_ADDRESS_TLV:
1023 break;
1024 case CDP_PORT_ID_TLV:
1025 break;
1026 case CDP_CAPABILITIES_TLV:
1027 break;
1028 case CDP_VERSION_TLV:
1029 break;
1030 case CDP_PLATFORM_TLV:
1031 break;
1032 case CDP_NATIVE_VLAN_TLV:
1033 nvlan = *ss;
1034 break;
1035 case CDP_APPLIANCE_VLAN_TLV:
1036 t = (const uchar *)ss;
1037 while (tlen > 0) {
1038 if (tlen < 3)
1039 goto pkt_short;
1040
1041 applid = t[0];
1042 ss = (const ushort *)(t + 1);
1043
1044#ifdef CONFIG_CDP_APPLIANCE_VLAN_TYPE
1045 if (applid == CONFIG_CDP_APPLIANCE_VLAN_TYPE)
1046 vlan = *ss;
1047#else
1048 vlan = ntohs(*ss); /* XXX will this work; dunno */
1049#endif
1050 t += 3; tlen -= 3;
1051 }
1052 break;
1053 case CDP_TRIGGER_TLV:
1054 break;
1055 case CDP_POWER_CONSUMPTION_TLV:
1056 break;
1057 case CDP_SYSNAME_TLV:
1058 break;
1059 case CDP_SYSOBJECT_TLV:
1060 break;
1061 case CDP_MANAGEMENT_ADDRESS_TLV:
1062 break;
1063 }
1064 }
1065
1066 CDPApplianceVLAN = vlan;
1067 CDPNativeVLAN = nvlan;
1068
1069 CDPOK = 1;
1070 return;
1071
1072 pkt_short:
1073 printf("** CDP packet is too short\n");
1074 return;
1075}
1076
1077static void CDPStart(void)
1078{
1079#if defined(CONFIG_NET_MULTI)
1080 printf ("Using %s device\n", eth_get_name());
1081#endif
1082 CDPSeq = 0;
1083 CDPOK = 0;
1084
1085 CDPNativeVLAN = htons(-1);
1086 CDPApplianceVLAN = htons(-1);
1087
1088 NetSetTimeout (CDP_TIMEOUT, CDPTimeout);
1089 NetSetHandler (CDPDummyHandler);
1090
1091 CDPSendTrigger();
1092}
wdenk6e592382004-04-18 17:39:38 +00001093#endif /* CFG_CMD_CDP */
wdenka3d991b2004-04-15 21:48:45 +00001094
1095
wdenk2d966952002-10-31 22:12:35 +00001096void
wdenka3d991b2004-04-15 21:48:45 +00001097NetReceive(volatile uchar * inpkt, int len)
wdenk2d966952002-10-31 22:12:35 +00001098{
1099 Ethernet_t *et;
1100 IP_t *ip;
1101 ARP_t *arp;
1102 IPaddr_t tmp;
1103 int x;
wdenka3d991b2004-04-15 21:48:45 +00001104 uchar *pkt;
1105#if (CONFIG_COMMANDS & CFG_CMD_CDP)
1106 int iscdp;
1107#endif
1108 ushort cti = 0, vlanid = VLAN_NONE, myvlanid, mynvlanid;
wdenk2d966952002-10-31 22:12:35 +00001109
wdenka3d991b2004-04-15 21:48:45 +00001110#ifdef ET_DEBUG
1111 printf("packet received\n");
1112#endif
1113
1114 NetRxPkt = inpkt;
wdenk2d966952002-10-31 22:12:35 +00001115 NetRxPktLen = len;
wdenka3d991b2004-04-15 21:48:45 +00001116 et = (Ethernet_t *)inpkt;
1117
1118 /* too small packet? */
1119 if (len < ETHER_HDR_SIZE)
1120 return;
1121
1122#if (CONFIG_COMMANDS & CFG_CMD_CDP)
1123 /* keep track if packet is CDP */
1124 iscdp = memcmp(et->et_dest, NetCDPAddr, 6) == 0;
1125#endif
1126
1127 myvlanid = ntohs(NetOurVLAN);
1128 if (myvlanid == (ushort)-1)
1129 myvlanid = VLAN_NONE;
1130 mynvlanid = ntohs(NetOurNativeVLAN);
1131 if (mynvlanid == (ushort)-1)
1132 mynvlanid = VLAN_NONE;
wdenk2d966952002-10-31 22:12:35 +00001133
1134 x = ntohs(et->et_protlen);
1135
wdenka3d991b2004-04-15 21:48:45 +00001136#ifdef ET_DEBUG
1137 printf("packet received\n");
1138#endif
1139
wdenk2d966952002-10-31 22:12:35 +00001140 if (x < 1514) {
1141 /*
1142 * Got a 802 packet. Check the other protocol field.
1143 */
1144 x = ntohs(et->et_prot);
wdenka3d991b2004-04-15 21:48:45 +00001145
1146 ip = (IP_t *)(inpkt + E802_HDR_SIZE);
wdenk2d966952002-10-31 22:12:35 +00001147 len -= E802_HDR_SIZE;
wdenka3d991b2004-04-15 21:48:45 +00001148
1149 } else if (x != PROT_VLAN) { /* normal packet */
1150 ip = (IP_t *)(inpkt + ETHER_HDR_SIZE);
wdenk2d966952002-10-31 22:12:35 +00001151 len -= ETHER_HDR_SIZE;
wdenka3d991b2004-04-15 21:48:45 +00001152
1153 } else { /* VLAN packet */
1154 VLAN_Ethernet_t *vet = (VLAN_Ethernet_t *)et;
1155
1156#ifdef ET_DEBUG
1157 printf("VLAN packet received\n");
1158#endif
1159 /* too small packet? */
1160 if (len < VLAN_ETHER_HDR_SIZE)
1161 return;
1162
1163 /* if no VLAN active */
1164 if ((ntohs(NetOurVLAN) & VLAN_IDMASK) == VLAN_NONE
1165#if (CONFIG_COMMANDS & CFG_CMD_CDP)
1166 && iscdp == 0
1167#endif
1168 )
1169 return;
1170
1171 cti = ntohs(vet->vet_tag);
1172 vlanid = cti & VLAN_IDMASK;
1173 x = ntohs(vet->vet_type);
1174
1175 ip = (IP_t *)(inpkt + VLAN_ETHER_HDR_SIZE);
1176 len -= VLAN_ETHER_HDR_SIZE;
wdenk2d966952002-10-31 22:12:35 +00001177 }
1178
1179#ifdef ET_DEBUG
1180 printf("Receive from protocol 0x%x\n", x);
1181#endif
1182
wdenka3d991b2004-04-15 21:48:45 +00001183#if (CONFIG_COMMANDS & CFG_CMD_CDP)
1184 if (iscdp) {
1185 CDPHandler((uchar *)ip, len);
1186 return;
1187 }
1188#endif
1189
1190 if ((myvlanid & VLAN_IDMASK) != VLAN_NONE) {
1191 if (vlanid == VLAN_NONE)
1192 vlanid = (mynvlanid & VLAN_IDMASK);
1193 /* not matched? */
1194 if (vlanid != (myvlanid & VLAN_IDMASK))
1195 return;
1196 }
1197
wdenk2d966952002-10-31 22:12:35 +00001198 switch (x) {
1199
1200 case PROT_ARP:
1201 /*
1202 * We have to deal with two types of ARP packets:
wdenk8bde7f72003-06-27 21:31:46 +00001203 * - REQUEST packets will be answered by sending our
1204 * IP address - if we know it.
1205 * - REPLY packates are expected only after we asked
1206 * for the TFTP server's or the gateway's ethernet
1207 * address; so if we receive such a packet, we set
1208 * the server ethernet address
wdenk2d966952002-10-31 22:12:35 +00001209 */
1210#ifdef ET_DEBUG
wdenk4b9206e2004-03-23 22:14:11 +00001211 puts ("Got ARP\n");
wdenk2d966952002-10-31 22:12:35 +00001212#endif
1213 arp = (ARP_t *)ip;
1214 if (len < ARP_HDR_SIZE) {
1215 printf("bad length %d < %d\n", len, ARP_HDR_SIZE);
1216 return;
1217 }
1218 if (ntohs(arp->ar_hrd) != ARP_ETHER) {
1219 return;
1220 }
1221 if (ntohs(arp->ar_pro) != PROT_IP) {
1222 return;
1223 }
1224 if (arp->ar_hln != 6) {
1225 return;
1226 }
1227 if (arp->ar_pln != 4) {
1228 return;
1229 }
1230
1231 if (NetOurIP == 0) {
1232 return;
1233 }
1234
1235 if (NetReadIP(&arp->ar_data[16]) != NetOurIP) {
1236 return;
1237 }
1238
1239 switch (ntohs(arp->ar_op)) {
1240 case ARPOP_REQUEST: /* reply with our IP address */
1241#ifdef ET_DEBUG
wdenk4b9206e2004-03-23 22:14:11 +00001242 puts ("Got ARP REQUEST, return our IP\n");
wdenk2d966952002-10-31 22:12:35 +00001243#endif
wdenka3d991b2004-04-15 21:48:45 +00001244 pkt = (uchar *)et;
1245 pkt += NetSetEther(pkt, et->et_src, PROT_ARP);
wdenk2d966952002-10-31 22:12:35 +00001246 arp->ar_op = htons(ARPOP_REPLY);
1247 memcpy (&arp->ar_data[10], &arp->ar_data[0], 6);
1248 NetCopyIP(&arp->ar_data[16], &arp->ar_data[6]);
1249 memcpy (&arp->ar_data[ 0], NetOurEther, 6);
1250 NetCopyIP(&arp->ar_data[ 6], &NetOurIP);
wdenka3d991b2004-04-15 21:48:45 +00001251 (void) eth_send((uchar *)et, (pkt - (uchar *)et) + ARP_HDR_SIZE);
wdenk2d966952002-10-31 22:12:35 +00001252 return;
wdenk73a8b272003-06-05 19:27:42 +00001253
1254 case ARPOP_REPLY: /* arp reply */
1255 /* are we waiting for a reply */
1256 if (!NetArpWaitPacketIP || !NetArpWaitPacketMAC)
1257 break;
wdenk2d966952002-10-31 22:12:35 +00001258#ifdef ET_DEBUG
wdenk73a8b272003-06-05 19:27:42 +00001259 printf("Got ARP REPLY, set server/gtwy eth addr (%02x:%02x:%02x:%02x:%02x:%02x)\n",
1260 arp->ar_data[0], arp->ar_data[1],
1261 arp->ar_data[2], arp->ar_data[3],
1262 arp->ar_data[4], arp->ar_data[5]);
wdenk2d966952002-10-31 22:12:35 +00001263#endif
wdenk73a8b272003-06-05 19:27:42 +00001264
1265 tmp = NetReadIP(&arp->ar_data[6]);
1266
1267 /* matched waiting packet's address */
1268 if (tmp == NetArpWaitReplyIP) {
1269#ifdef ET_DEBUG
wdenk4b9206e2004-03-23 22:14:11 +00001270 puts ("Got it\n");
wdenk73a8b272003-06-05 19:27:42 +00001271#endif
1272 /* save address for later use */
1273 memcpy(NetArpWaitPacketMAC, &arp->ar_data[0], 6);
1274
wdenk68ceb292004-08-02 21:11:11 +00001275#ifdef CONFIG_NETCONSOLE
1276 (*packetHandler)(0,0,0,0);
1277#endif
wdenk73a8b272003-06-05 19:27:42 +00001278 /* modify header, and transmit it */
1279 memcpy(((Ethernet_t *)NetArpWaitTxPacket)->et_dest, NetArpWaitPacketMAC, 6);
1280 (void) eth_send(NetArpWaitTxPacket, NetArpWaitTxPacketSize);
1281
1282 /* no arp request pending now */
1283 NetArpWaitPacketIP = 0;
1284 NetArpWaitTxPacketSize = 0;
1285 NetArpWaitPacketMAC = NULL;
1286
1287 }
wdenk2d966952002-10-31 22:12:35 +00001288 return;
1289 default:
1290#ifdef ET_DEBUG
1291 printf("Unexpected ARP opcode 0x%x\n", ntohs(arp->ar_op));
1292#endif
1293 return;
1294 }
wdenk289f9322005-01-12 00:15:14 +00001295 break;
wdenk2d966952002-10-31 22:12:35 +00001296
1297 case PROT_RARP:
1298#ifdef ET_DEBUG
wdenk4b9206e2004-03-23 22:14:11 +00001299 puts ("Got RARP\n");
wdenk2d966952002-10-31 22:12:35 +00001300#endif
1301 arp = (ARP_t *)ip;
1302 if (len < ARP_HDR_SIZE) {
1303 printf("bad length %d < %d\n", len, ARP_HDR_SIZE);
1304 return;
1305 }
1306
1307 if ((ntohs(arp->ar_op) != RARPOP_REPLY) ||
1308 (ntohs(arp->ar_hrd) != ARP_ETHER) ||
1309 (ntohs(arp->ar_pro) != PROT_IP) ||
1310 (arp->ar_hln != 6) || (arp->ar_pln != 4)) {
1311
wdenk4b9206e2004-03-23 22:14:11 +00001312 puts ("invalid RARP header\n");
wdenk2d966952002-10-31 22:12:35 +00001313 } else {
1314 NetCopyIP(&NetOurIP, &arp->ar_data[16]);
wdenk3d3befa2004-03-14 15:06:13 +00001315 if (NetServerIP == 0)
1316 NetCopyIP(&NetServerIP, &arp->ar_data[ 6]);
wdenk2d966952002-10-31 22:12:35 +00001317 memcpy (NetServerEther, &arp->ar_data[ 0], 6);
1318
1319 (*packetHandler)(0,0,0,0);
1320 }
1321 break;
1322
1323 case PROT_IP:
1324#ifdef ET_DEBUG
wdenk4b9206e2004-03-23 22:14:11 +00001325 puts ("Got IP\n");
wdenk2d966952002-10-31 22:12:35 +00001326#endif
1327 if (len < IP_HDR_SIZE) {
1328 debug ("len bad %d < %d\n", len, IP_HDR_SIZE);
1329 return;
1330 }
1331 if (len < ntohs(ip->ip_len)) {
1332 printf("len bad %d < %d\n", len, ntohs(ip->ip_len));
1333 return;
1334 }
1335 len = ntohs(ip->ip_len);
1336#ifdef ET_DEBUG
1337 printf("len=%d, v=%02x\n", len, ip->ip_hl_v & 0xff);
1338#endif
1339 if ((ip->ip_hl_v & 0xf0) != 0x40) {
1340 return;
1341 }
1342 if (ip->ip_off & htons(0x1fff)) { /* Can't deal w/ fragments */
1343 return;
1344 }
1345 if (!NetCksumOk((uchar *)ip, IP_HDR_SIZE_NO_UDP / 2)) {
wdenk4b9206e2004-03-23 22:14:11 +00001346 puts ("checksum bad\n");
wdenk2d966952002-10-31 22:12:35 +00001347 return;
1348 }
1349 tmp = NetReadIP(&ip->ip_dst);
1350 if (NetOurIP && tmp != NetOurIP && tmp != 0xFFFFFFFF) {
1351 return;
1352 }
1353 /*
1354 * watch for ICMP host redirects
1355 *
wdenk8bde7f72003-06-27 21:31:46 +00001356 * There is no real handler code (yet). We just watch
1357 * for ICMP host redirect messages. In case anybody
1358 * sees these messages: please contact me
1359 * (wd@denx.de), or - even better - send me the
1360 * necessary fixes :-)
wdenk2d966952002-10-31 22:12:35 +00001361 *
wdenk8bde7f72003-06-27 21:31:46 +00001362 * Note: in all cases where I have seen this so far
1363 * it was a problem with the router configuration,
1364 * for instance when a router was configured in the
1365 * BOOTP reply, but the TFTP server was on the same
1366 * subnet. So this is probably a warning that your
1367 * configuration might be wrong. But I'm not really
1368 * sure if there aren't any other situations.
wdenk2d966952002-10-31 22:12:35 +00001369 */
1370 if (ip->ip_p == IPPROTO_ICMP) {
1371 ICMP_t *icmph = (ICMP_t *)&(ip->udp_src);
1372
wdenk73a8b272003-06-05 19:27:42 +00001373 switch (icmph->type) {
1374 case ICMP_REDIRECT:
wdenk2d966952002-10-31 22:12:35 +00001375 if (icmph->code != ICMP_REDIR_HOST)
1376 return;
1377 puts (" ICMP Host Redirect to ");
1378 print_IPaddr(icmph->un.gateway);
1379 putc(' ');
wdenk73a8b272003-06-05 19:27:42 +00001380 break;
1381#if (CONFIG_COMMANDS & CFG_CMD_PING)
1382 case ICMP_ECHO_REPLY:
1383 /*
1384 * IP header OK. Pass the packet to the current handler.
1385 */
1386 /* XXX point to ip packet */
1387 (*packetHandler)((uchar *)ip, 0, 0, 0);
1388 break;
1389#endif
1390 default:
1391 return;
1392 }
wdenk2d966952002-10-31 22:12:35 +00001393 } else if (ip->ip_p != IPPROTO_UDP) { /* Only UDP packets */
1394 return;
1395 }
1396
wdenk68ceb292004-08-02 21:11:11 +00001397#ifdef CONFIG_NETCONSOLE
1398 nc_input_packet((uchar *)ip +IP_HDR_SIZE,
1399 ntohs(ip->udp_dst),
1400 ntohs(ip->udp_src),
1401 ntohs(ip->udp_len) - 8);
1402#endif
wdenk2d966952002-10-31 22:12:35 +00001403 /*
1404 * IP header OK. Pass the packet to the current handler.
1405 */
1406 (*packetHandler)((uchar *)ip +IP_HDR_SIZE,
1407 ntohs(ip->udp_dst),
1408 ntohs(ip->udp_src),
1409 ntohs(ip->udp_len) - 8);
wdenk2d966952002-10-31 22:12:35 +00001410 break;
1411 }
1412}
1413
1414
1415/**********************************************************************/
1416
1417static int net_check_prereq (proto_t protocol)
1418{
1419 switch (protocol) {
wdenk6e592382004-04-18 17:39:38 +00001420 /* Fall through */
wdenk73a8b272003-06-05 19:27:42 +00001421#if (CONFIG_COMMANDS & CFG_CMD_PING)
1422 case PING:
wdenk6e592382004-04-18 17:39:38 +00001423 if (NetPingIP == 0) {
1424 puts ("*** ERROR: ping address not given\n");
1425 return (1);
1426 }
1427 goto common;
wdenk73a8b272003-06-05 19:27:42 +00001428#endif
wdenkcbd8a352004-02-24 02:00:03 +00001429#if (CONFIG_COMMANDS & CFG_CMD_NFS)
1430 case NFS:
1431#endif
wdenk68ceb292004-08-02 21:11:11 +00001432 case NETCONS:
wdenk2d966952002-10-31 22:12:35 +00001433 case TFTP:
wdenk6e592382004-04-18 17:39:38 +00001434 if (NetServerIP == 0) {
1435 puts ("*** ERROR: `serverip' not set\n");
1436 return (1);
1437 }
wdenk73a8b272003-06-05 19:27:42 +00001438#if (CONFIG_COMMANDS & CFG_CMD_PING)
wdenk6e592382004-04-18 17:39:38 +00001439 common:
wdenk73a8b272003-06-05 19:27:42 +00001440#endif
1441
wdenk6e592382004-04-18 17:39:38 +00001442 if (NetOurIP == 0) {
1443 puts ("*** ERROR: `ipaddr' not set\n");
1444 return (1);
1445 }
1446 /* Fall through */
wdenk2d966952002-10-31 22:12:35 +00001447
1448 case DHCP:
1449 case RARP:
1450 case BOOTP:
wdenka3d991b2004-04-15 21:48:45 +00001451 case CDP:
wdenk6e592382004-04-18 17:39:38 +00001452 if (memcmp (NetOurEther, "\0\0\0\0\0\0", 6) == 0) {
wdenk2d966952002-10-31 22:12:35 +00001453#ifdef CONFIG_NET_MULTI
wdenk6e592382004-04-18 17:39:38 +00001454 extern int eth_get_dev_index (void);
1455 int num = eth_get_dev_index ();
wdenk2d966952002-10-31 22:12:35 +00001456
wdenk6e592382004-04-18 17:39:38 +00001457 switch (num) {
1458 case -1:
wdenk2d966952002-10-31 22:12:35 +00001459 puts ("*** ERROR: No ethernet found.\n");
1460 return (1);
wdenk6e592382004-04-18 17:39:38 +00001461 case 0:
wdenk2d966952002-10-31 22:12:35 +00001462 puts ("*** ERROR: `ethaddr' not set\n");
1463 break;
wdenk6e592382004-04-18 17:39:38 +00001464 default:
wdenk8bde7f72003-06-27 21:31:46 +00001465 printf ("*** ERROR: `eth%daddr' not set\n",
wdenk2d966952002-10-31 22:12:35 +00001466 num);
1467 break;
wdenk2d966952002-10-31 22:12:35 +00001468 }
wdenk6e592382004-04-18 17:39:38 +00001469
1470 NetStartAgain ();
1471 return (2);
1472#else
1473 puts ("*** ERROR: `ethaddr' not set\n");
1474 return (1);
1475#endif
1476 }
1477 /* Fall through */
1478 default:
1479 return (0);
wdenk2d966952002-10-31 22:12:35 +00001480 }
wdenk6e592382004-04-18 17:39:38 +00001481 return (0); /* OK */
wdenk2d966952002-10-31 22:12:35 +00001482}
1483/**********************************************************************/
1484
1485int
1486NetCksumOk(uchar * ptr, int len)
1487{
1488 return !((NetCksum(ptr, len) + 1) & 0xfffe);
1489}
1490
1491
1492unsigned
1493NetCksum(uchar * ptr, int len)
1494{
1495 ulong xsum;
1496
1497 xsum = 0;
1498 while (len-- > 0)
1499 xsum += *((ushort *)ptr)++;
1500 xsum = (xsum & 0xffff) + (xsum >> 16);
1501 xsum = (xsum & 0xffff) + (xsum >> 16);
1502 return (xsum & 0xffff);
1503}
1504
wdenka3d991b2004-04-15 21:48:45 +00001505int
1506NetEthHdrSize(void)
1507{
1508 ushort myvlanid;
wdenk2d966952002-10-31 22:12:35 +00001509
wdenka3d991b2004-04-15 21:48:45 +00001510 myvlanid = ntohs(NetOurVLAN);
1511 if (myvlanid == (ushort)-1)
1512 myvlanid = VLAN_NONE;
1513
1514 return ((myvlanid & VLAN_IDMASK) == VLAN_NONE) ? ETHER_HDR_SIZE : VLAN_ETHER_HDR_SIZE;
1515}
1516
1517int
wdenk2d966952002-10-31 22:12:35 +00001518NetSetEther(volatile uchar * xet, uchar * addr, uint prot)
1519{
1520 Ethernet_t *et = (Ethernet_t *)xet;
wdenka3d991b2004-04-15 21:48:45 +00001521 ushort myvlanid;
1522
1523 myvlanid = ntohs(NetOurVLAN);
1524 if (myvlanid == (ushort)-1)
1525 myvlanid = VLAN_NONE;
wdenk2d966952002-10-31 22:12:35 +00001526
1527 memcpy (et->et_dest, addr, 6);
1528 memcpy (et->et_src, NetOurEther, 6);
wdenka3d991b2004-04-15 21:48:45 +00001529 if ((myvlanid & VLAN_IDMASK) == VLAN_NONE) {
wdenk2d966952002-10-31 22:12:35 +00001530 et->et_protlen = htons(prot);
wdenka3d991b2004-04-15 21:48:45 +00001531 return ETHER_HDR_SIZE;
1532 } else {
1533 VLAN_Ethernet_t *vet = (VLAN_Ethernet_t *)xet;
wdenk2d966952002-10-31 22:12:35 +00001534
wdenka3d991b2004-04-15 21:48:45 +00001535 vet->vet_vlan_type = htons(PROT_VLAN);
1536 vet->vet_tag = htons((0 << 5) | (myvlanid & VLAN_IDMASK));
1537 vet->vet_type = htons(prot);
1538 return VLAN_ETHER_HDR_SIZE;
1539 }
1540}
wdenk2d966952002-10-31 22:12:35 +00001541
1542void
1543NetSetIP(volatile uchar * xip, IPaddr_t dest, int dport, int sport, int len)
1544{
1545 volatile IP_t *ip = (IP_t *)xip;
1546
1547 /*
1548 * If the data is an odd number of bytes, zero the
1549 * byte after the last byte so that the checksum
1550 * will work.
1551 */
1552 if (len & 1)
1553 xip[IP_HDR_SIZE + len] = 0;
1554
1555 /*
1556 * Construct an IP and UDP header.
wdenk6e592382004-04-18 17:39:38 +00001557 * (need to set no fragment bit - XXX)
wdenk2d966952002-10-31 22:12:35 +00001558 */
1559 ip->ip_hl_v = 0x45; /* IP_HDR_SIZE / 4 (not including UDP) */
1560 ip->ip_tos = 0;
1561 ip->ip_len = htons(IP_HDR_SIZE + len);
1562 ip->ip_id = htons(NetIPID++);
1563 ip->ip_off = htons(0x4000); /* No fragmentation */
1564 ip->ip_ttl = 255;
1565 ip->ip_p = 17; /* UDP */
1566 ip->ip_sum = 0;
1567 NetCopyIP((void*)&ip->ip_src, &NetOurIP); /* already in network byte order */
1568 NetCopyIP((void*)&ip->ip_dst, &dest); /* - "" - */
1569 ip->udp_src = htons(sport);
1570 ip->udp_dst = htons(dport);
1571 ip->udp_len = htons(8 + len);
1572 ip->udp_xsum = 0;
1573 ip->ip_sum = ~NetCksum((uchar *)ip, IP_HDR_SIZE_NO_UDP / 2);
1574}
1575
1576void copy_filename (uchar *dst, uchar *src, int size)
1577{
1578 if (*src && (*src == '"')) {
1579 ++src;
1580 --size;
1581 }
1582
1583 while ((--size > 0) && *src && (*src != '"')) {
1584 *dst++ = *src++;
1585 }
1586 *dst = '\0';
1587}
1588
1589#endif /* CFG_CMD_NET */
1590
1591void ip_to_string (IPaddr_t x, char *s)
1592{
wdenka3d991b2004-04-15 21:48:45 +00001593 x = ntohl (x);
1594 sprintf (s, "%d.%d.%d.%d",
1595 (int) ((x >> 24) & 0xff),
1596 (int) ((x >> 16) & 0xff),
1597 (int) ((x >> 8) & 0xff), (int) ((x >> 0) & 0xff)
wdenk6e592382004-04-18 17:39:38 +00001598 );
wdenk2d966952002-10-31 22:12:35 +00001599}
1600
wdenk73a8b272003-06-05 19:27:42 +00001601IPaddr_t string_to_ip(char *s)
wdenk2d966952002-10-31 22:12:35 +00001602{
1603 IPaddr_t addr;
wdenk73a8b272003-06-05 19:27:42 +00001604 char *e;
wdenk2d966952002-10-31 22:12:35 +00001605 int i;
1606
wdenk73a8b272003-06-05 19:27:42 +00001607 if (s == NULL)
1608 return(0);
wdenk2d966952002-10-31 22:12:35 +00001609
1610 for (addr=0, i=0; i<4; ++i) {
1611 ulong val = s ? simple_strtoul(s, &e, 10) : 0;
1612 addr <<= 8;
1613 addr |= (val & 0xFF);
1614 if (s) {
1615 s = (*e) ? e+1 : e;
1616 }
1617 }
1618
1619 return (htonl(addr));
1620}
wdenk73a8b272003-06-05 19:27:42 +00001621
wdenka3d991b2004-04-15 21:48:45 +00001622void VLAN_to_string(ushort x, char *s)
1623{
1624 x = ntohs(x);
1625
1626 if (x == (ushort)-1)
1627 x = VLAN_NONE;
1628
1629 if (x == VLAN_NONE)
1630 strcpy(s, "none");
1631 else
1632 sprintf(s, "%d", x & VLAN_IDMASK);
1633}
1634
1635ushort string_to_VLAN(char *s)
1636{
1637 ushort id;
1638
1639 if (s == NULL)
wdenkb9711de2004-04-25 13:18:40 +00001640 return htons(VLAN_NONE);
wdenka3d991b2004-04-15 21:48:45 +00001641
1642 if (*s < '0' || *s > '9')
1643 id = VLAN_NONE;
1644 else
1645 id = (ushort)simple_strtoul(s, NULL, 10);
1646
wdenkb9711de2004-04-25 13:18:40 +00001647 return htons(id);
wdenka3d991b2004-04-15 21:48:45 +00001648}
1649
wdenk73a8b272003-06-05 19:27:42 +00001650void print_IPaddr (IPaddr_t x)
1651{
wdenka3d991b2004-04-15 21:48:45 +00001652 char tmp[16];
wdenk73a8b272003-06-05 19:27:42 +00001653
wdenka3d991b2004-04-15 21:48:45 +00001654 ip_to_string (x, tmp);
wdenk73a8b272003-06-05 19:27:42 +00001655
wdenka3d991b2004-04-15 21:48:45 +00001656 puts (tmp);
wdenk73a8b272003-06-05 19:27:42 +00001657}
1658
1659IPaddr_t getenv_IPaddr (char *var)
1660{
1661 return (string_to_ip(getenv(var)));
1662}
wdenka3d991b2004-04-15 21:48:45 +00001663
1664ushort getenv_VLAN(char *var)
1665{
1666 return (string_to_VLAN(getenv(var)));
1667}