blob: 506203761f43bb54d4115f3715b50b807a35b423 [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
wdenkea287de2005-04-01 00:25:43 +000089#if (CONFIG_COMMANDS & CFG_CMD_SNTP)
90#include "sntp.h"
91#endif
wdenk2d966952002-10-31 22:12:35 +000092
93#if (CONFIG_COMMANDS & CFG_CMD_NET)
94
wdenk73a8b272003-06-05 19:27:42 +000095#define ARP_TIMEOUT 5 /* Seconds before trying ARP again */
96#ifndef CONFIG_NET_RETRY_COUNT
97# define ARP_TIMEOUT_COUNT 5 /* # of timeouts before giving up */
98#else
99# define ARP_TIMEOUT_COUNT (CONFIG_NET_RETRY_COUNT)
100#endif
101
wdenk2d966952002-10-31 22:12:35 +0000102#if 0
103#define ET_DEBUG
104#endif
105
106/** BOOTP EXTENTIONS **/
107
108IPaddr_t NetOurSubnetMask=0; /* Our subnet mask (0=unknown) */
109IPaddr_t NetOurGatewayIP=0; /* Our gateways IP address */
110IPaddr_t NetOurDNSIP=0; /* Our DNS IP address */
stroesefe389a82003-08-28 14:17:32 +0000111#if (CONFIG_BOOTP_MASK & CONFIG_BOOTP_DNS2)
112IPaddr_t NetOurDNS2IP=0; /* Our 2nd DNS IP address */
113#endif
wdenk2d966952002-10-31 22:12:35 +0000114char NetOurNISDomain[32]={0,}; /* Our NIS domain */
115char NetOurHostName[32]={0,}; /* Our hostname */
116char NetOurRootPath[64]={0,}; /* Our bootpath */
117ushort NetBootFileSize=0; /* Our bootfile size in blocks */
118
119/** END OF BOOTP EXTENTIONS **/
120
121ulong NetBootFileXferSize; /* The actual transferred size of the bootfile (in bytes) */
122uchar NetOurEther[6]; /* Our ethernet address */
123uchar NetServerEther[6] = /* Boot server enet address */
wdenk73a8b272003-06-05 19:27:42 +0000124 { 0, 0, 0, 0, 0, 0 };
wdenk2d966952002-10-31 22:12:35 +0000125IPaddr_t NetOurIP; /* Our IP addr (0 = unknown) */
126IPaddr_t NetServerIP; /* Our IP addr (0 = unknown) */
127volatile uchar *NetRxPkt; /* Current receive packet */
128int NetRxPktLen; /* Current rx packet length */
129unsigned NetIPID; /* IP packet ID */
130uchar NetBcastAddr[6] = /* Ethernet bcast address */
131 { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
wdenk73a8b272003-06-05 19:27:42 +0000132uchar NetEtherNullAddr[6] =
133 { 0, 0, 0, 0, 0, 0 };
wdenka3d991b2004-04-15 21:48:45 +0000134#if (CONFIG_COMMANDS & CFG_CMD_CDP)
wdenk6e592382004-04-18 17:39:38 +0000135uchar NetCDPAddr[6] = /* Ethernet bcast address */
wdenka3d991b2004-04-15 21:48:45 +0000136 { 0x01, 0x00, 0x0c, 0xcc, 0xcc, 0xcc };
137#endif
wdenk2d966952002-10-31 22:12:35 +0000138int NetState; /* Network loop state */
139#ifdef CONFIG_NET_MULTI
140int NetRestartWrap = 0; /* Tried all network devices */
141static int NetRestarted = 0; /* Network loop restarted */
142static int NetDevExists = 0; /* At least one device configured */
143#endif
144
wdenk6e592382004-04-18 17:39:38 +0000145/* XXX in both little & big endian machines 0xFFFF == ntohs(-1) */
146ushort NetOurVLAN = 0xFFFF; /* default is without VLAN */
147ushort NetOurNativeVLAN = 0xFFFF; /* ditto */
wdenka3d991b2004-04-15 21:48:45 +0000148
wdenk2d966952002-10-31 22:12:35 +0000149char BootFile[128]; /* Boot File name */
150
wdenk73a8b272003-06-05 19:27:42 +0000151#if (CONFIG_COMMANDS & CFG_CMD_PING)
152IPaddr_t NetPingIP; /* the ip address to ping */
153
154static void PingStart(void);
155#endif
156
wdenka3d991b2004-04-15 21:48:45 +0000157#if (CONFIG_COMMANDS & CFG_CMD_CDP)
158static void CDPStart(void);
159#endif
160
wdenkea287de2005-04-01 00:25:43 +0000161#if (CONFIG_COMMANDS & CFG_CMD_SNTP)
162IPaddr_t NetNtpServerIP; /* NTP server IP address */
163int NetTimeOffset=0; /* offset time from UTC */
164#endif
165
wdenk68ceb292004-08-02 21:11:11 +0000166#ifdef CONFIG_NETCONSOLE
167void NcStart(void);
168int nc_input_packet(uchar *pkt, unsigned dest, unsigned src, unsigned len);
169#endif
170
wdenk2d966952002-10-31 22:12:35 +0000171volatile uchar PktBuf[(PKTBUFSRX+1) * PKTSIZE_ALIGN + PKTALIGN];
172
173volatile uchar *NetRxPackets[PKTBUFSRX]; /* Receive packets */
174
175static rxhand_f *packetHandler; /* Current RX packet handler */
176static thand_f *timeHandler; /* Current timeout handler */
wdenke0ac62d2003-08-17 18:55:18 +0000177static ulong timeStart; /* Time base value */
178static ulong timeDelta; /* Current timeout value */
wdenk2d966952002-10-31 22:12:35 +0000179volatile uchar *NetTxPacket = 0; /* THE transmit packet */
180
181static int net_check_prereq (proto_t protocol);
182
183/**********************************************************************/
wdenk73a8b272003-06-05 19:27:42 +0000184
185IPaddr_t NetArpWaitPacketIP;
186IPaddr_t NetArpWaitReplyIP;
187uchar *NetArpWaitPacketMAC; /* MAC address of waiting packet's destination */
Wolfgang Denkb2f50802005-08-12 23:43:12 +0200188uchar *NetArpWaitTxPacket; /* THE transmit packet */
wdenk73a8b272003-06-05 19:27:42 +0000189int NetArpWaitTxPacketSize;
190uchar NetArpWaitPacketBuf[PKTSIZE_ALIGN + PKTALIGN];
191ulong NetArpWaitTimerStart;
192int NetArpWaitTry;
193
wdenk6e592382004-04-18 17:39:38 +0000194void ArpRequest (void)
wdenk73a8b272003-06-05 19:27:42 +0000195{
196 int i;
197 volatile uchar *pkt;
wdenk6e592382004-04-18 17:39:38 +0000198 ARP_t *arp;
wdenk73a8b272003-06-05 19:27:42 +0000199
200#ifdef ET_DEBUG
wdenk6e592382004-04-18 17:39:38 +0000201 printf ("ARP broadcast %d\n", NetArpWaitTry);
wdenk73a8b272003-06-05 19:27:42 +0000202#endif
203 pkt = NetTxPacket;
204
wdenk6e592382004-04-18 17:39:38 +0000205 pkt += NetSetEther (pkt, NetBcastAddr, PROT_ARP);
wdenk73a8b272003-06-05 19:27:42 +0000206
wdenk6e592382004-04-18 17:39:38 +0000207 arp = (ARP_t *) pkt;
wdenk73a8b272003-06-05 19:27:42 +0000208
wdenk6e592382004-04-18 17:39:38 +0000209 arp->ar_hrd = htons (ARP_ETHER);
210 arp->ar_pro = htons (PROT_IP);
wdenk73a8b272003-06-05 19:27:42 +0000211 arp->ar_hln = 6;
212 arp->ar_pln = 4;
wdenk6e592382004-04-18 17:39:38 +0000213 arp->ar_op = htons (ARPOP_REQUEST);
wdenk73a8b272003-06-05 19:27:42 +0000214
Wolfgang Denkb2f50802005-08-12 23:43:12 +0200215 memcpy (&arp->ar_data[0], NetOurEther, 6); /* source ET addr */
216 NetWriteIP ((uchar *) & arp->ar_data[6], NetOurIP); /* source IP addr */
wdenk6e592382004-04-18 17:39:38 +0000217 for (i = 10; i < 16; ++i) {
218 arp->ar_data[i] = 0; /* dest ET addr = 0 */
wdenk73a8b272003-06-05 19:27:42 +0000219 }
220
wdenk6e592382004-04-18 17:39:38 +0000221 if ((NetArpWaitPacketIP & NetOurSubnetMask) !=
222 (NetOurIP & NetOurSubnetMask)) {
223 if (NetOurGatewayIP == 0) {
224 puts ("## Warning: gatewayip needed but not set\n");
Wolfgang Denkd509b812006-03-12 01:13:30 +0100225 NetArpWaitReplyIP = NetArpWaitPacketIP;
226 } else {
227 NetArpWaitReplyIP = NetOurGatewayIP;
wdenk6e592382004-04-18 17:39:38 +0000228 }
wdenk6e592382004-04-18 17:39:38 +0000229 } else {
230 NetArpWaitReplyIP = NetArpWaitPacketIP;
231 }
wdenk73a8b272003-06-05 19:27:42 +0000232
wdenk6e592382004-04-18 17:39:38 +0000233 NetWriteIP ((uchar *) & arp->ar_data[16], NetArpWaitReplyIP);
234 (void) eth_send (NetTxPacket, (pkt - NetTxPacket) + ARP_HDR_SIZE);
wdenk73a8b272003-06-05 19:27:42 +0000235}
236
237void ArpTimeoutCheck(void)
238{
239 ulong t;
240
241 if (!NetArpWaitPacketIP)
242 return;
243
244 t = get_timer(0);
245
246 /* check for arp timeout */
247 if ((t - NetArpWaitTimerStart) > ARP_TIMEOUT * CFG_HZ) {
248 NetArpWaitTry++;
249
250 if (NetArpWaitTry >= ARP_TIMEOUT_COUNT) {
251 puts ("\nARP Retry count exceeded; starting again\n");
252 NetArpWaitTry = 0;
253 NetStartAgain();
254 } else {
255 NetArpWaitTimerStart = t;
256 ArpRequest();
257 }
258 }
259}
260
261/**********************************************************************/
wdenk2d966952002-10-31 22:12:35 +0000262/*
263 * Main network processing loop.
264 */
265
266int
267NetLoop(proto_t protocol)
268{
269 DECLARE_GLOBAL_DATA_PTR;
270
271 bd_t *bd = gd->bd;
272
273#ifdef CONFIG_NET_MULTI
274 NetRestarted = 0;
275 NetDevExists = 0;
276#endif
277
wdenk73a8b272003-06-05 19:27:42 +0000278 /* XXX problem with bss workaround */
279 NetArpWaitPacketMAC = NULL;
280 NetArpWaitTxPacket = NULL;
281 NetArpWaitPacketIP = 0;
282 NetArpWaitReplyIP = 0;
283 NetArpWaitTxPacket = NULL;
284 NetTxPacket = NULL;
285
wdenk2d966952002-10-31 22:12:35 +0000286 if (!NetTxPacket) {
287 int i;
wdenk2d966952002-10-31 22:12:35 +0000288 /*
289 * Setup packet buffers, aligned correctly.
290 */
291 NetTxPacket = &PktBuf[0] + (PKTALIGN - 1);
292 NetTxPacket -= (ulong)NetTxPacket % PKTALIGN;
293 for (i = 0; i < PKTBUFSRX; i++) {
294 NetRxPackets[i] = NetTxPacket + (i+1)*PKTSIZE_ALIGN;
295 }
wdenk73a8b272003-06-05 19:27:42 +0000296 }
297
298 if (!NetArpWaitTxPacket) {
299 NetArpWaitTxPacket = &NetArpWaitPacketBuf[0] + (PKTALIGN - 1);
300 NetArpWaitTxPacket -= (ulong)NetArpWaitTxPacket % PKTALIGN;
301 NetArpWaitTxPacketSize = 0;
wdenk2d966952002-10-31 22:12:35 +0000302 }
303
304 eth_halt();
wdenk6e592382004-04-18 17:39:38 +0000305#ifdef CONFIG_NET_MULTI
wdenka3d991b2004-04-15 21:48:45 +0000306 eth_set_current();
wdenk6e592382004-04-18 17:39:38 +0000307#endif
wdenkb1bf6f22005-04-03 14:52:59 +0000308 if (eth_init(bd) < 0) {
309 eth_halt();
wdenk6e592382004-04-18 17:39:38 +0000310 return(-1);
wdenkb1bf6f22005-04-03 14:52:59 +0000311 }
wdenk2d966952002-10-31 22:12:35 +0000312
313restart:
314#ifdef CONFIG_NET_MULTI
315 memcpy (NetOurEther, eth_get_dev()->enetaddr, 6);
316#else
317 memcpy (NetOurEther, bd->bi_enetaddr, 6);
318#endif
319
320 NetState = NETLOOP_CONTINUE;
321
322 /*
323 * Start the ball rolling with the given start function. From
324 * here on, this code is a state machine driven by received
325 * packets and timer events.
326 */
327
328 switch (protocol) {
wdenkcbd8a352004-02-24 02:00:03 +0000329#if (CONFIG_COMMANDS & CFG_CMD_NFS)
330 case NFS:
331#endif
wdenk73a8b272003-06-05 19:27:42 +0000332#if (CONFIG_COMMANDS & CFG_CMD_PING)
333 case PING:
334#endif
wdenkea287de2005-04-01 00:25:43 +0000335#if (CONFIG_COMMANDS & CFG_CMD_SNTP)
336 case SNTP:
337#endif
wdenk68ceb292004-08-02 21:11:11 +0000338 case NETCONS:
wdenk2d966952002-10-31 22:12:35 +0000339 case TFTP:
340 NetCopyIP(&NetOurIP, &bd->bi_ip_addr);
wdenk2d966952002-10-31 22:12:35 +0000341 NetOurGatewayIP = getenv_IPaddr ("gatewayip");
342 NetOurSubnetMask= getenv_IPaddr ("netmask");
wdenka3d991b2004-04-15 21:48:45 +0000343 NetOurVLAN = getenv_VLAN("vlan");
344 NetOurNativeVLAN = getenv_VLAN("nvlan");
wdenk73a8b272003-06-05 19:27:42 +0000345
346 switch (protocol) {
wdenkcbd8a352004-02-24 02:00:03 +0000347#if (CONFIG_COMMANDS & CFG_CMD_NFS)
348 case NFS:
349#endif
wdenk68ceb292004-08-02 21:11:11 +0000350 case NETCONS:
wdenk73a8b272003-06-05 19:27:42 +0000351 case TFTP:
352 NetServerIP = getenv_IPaddr ("serverip");
353 break;
354#if (CONFIG_COMMANDS & CFG_CMD_PING)
355 case PING:
356 /* nothing */
357 break;
358#endif
wdenkea287de2005-04-01 00:25:43 +0000359#if (CONFIG_COMMANDS & CFG_CMD_SNTP)
360 case SNTP:
361 /* nothing */
362 break;
363#endif
wdenk73a8b272003-06-05 19:27:42 +0000364 default:
365 break;
366 }
367
wdenk2d966952002-10-31 22:12:35 +0000368 break;
369 case BOOTP:
370 case RARP:
371 /*
wdenk8bde7f72003-06-27 21:31:46 +0000372 * initialize our IP addr to 0 in order to accept ANY
373 * IP addr assigned to us by the BOOTP / RARP server
wdenk2d966952002-10-31 22:12:35 +0000374 */
375 NetOurIP = 0;
wdenk3d3befa2004-03-14 15:06:13 +0000376 NetServerIP = getenv_IPaddr ("serverip");
Wolfgang Denkb2f50802005-08-12 23:43:12 +0200377 NetOurVLAN = getenv_VLAN("vlan"); /* VLANs must be read */
378 NetOurNativeVLAN = getenv_VLAN("nvlan");
379 case CDP:
380 NetOurVLAN = getenv_VLAN("vlan"); /* VLANs must be read */
381 NetOurNativeVLAN = getenv_VLAN("nvlan");
wdenk2d966952002-10-31 22:12:35 +0000382 break;
383 default:
384 break;
385 }
386
387 switch (net_check_prereq (protocol)) {
388 case 1:
389 /* network not configured */
wdenkb1bf6f22005-04-03 14:52:59 +0000390 eth_halt();
wdenk1d0350e2002-11-11 21:14:20 +0000391 return (-1);
wdenk2d966952002-10-31 22:12:35 +0000392
393#ifdef CONFIG_NET_MULTI
394 case 2:
395 /* network device not configured */
396 break;
397#endif /* CONFIG_NET_MULTI */
398
399 case 0:
400#ifdef CONFIG_NET_MULTI
401 NetDevExists = 1;
402#endif
403 switch (protocol) {
404 case TFTP:
405 /* always use ARP to get server ethernet address */
wdenk73a8b272003-06-05 19:27:42 +0000406 TftpStart();
wdenk2d966952002-10-31 22:12:35 +0000407 break;
408
409#if (CONFIG_COMMANDS & CFG_CMD_DHCP)
410 case DHCP:
411 /* Start with a clean slate... */
wdenkd407bf52004-10-11 22:51:13 +0000412 BootpTry = 0;
wdenk2d966952002-10-31 22:12:35 +0000413 NetOurIP = 0;
wdenk3d3befa2004-03-14 15:06:13 +0000414 NetServerIP = getenv_IPaddr ("serverip");
wdenk2d966952002-10-31 22:12:35 +0000415 DhcpRequest(); /* Basically same as BOOTP */
416 break;
417#endif /* CFG_CMD_DHCP */
418
419 case BOOTP:
420 BootpTry = 0;
421 BootpRequest ();
422 break;
423
424 case RARP:
425 RarpTry = 0;
426 RarpRequest ();
427 break;
wdenk73a8b272003-06-05 19:27:42 +0000428#if (CONFIG_COMMANDS & CFG_CMD_PING)
429 case PING:
430 PingStart();
431 break;
432#endif
wdenkcbd8a352004-02-24 02:00:03 +0000433#if (CONFIG_COMMANDS & CFG_CMD_NFS)
434 case NFS:
435 NfsStart();
436 break;
437#endif
wdenka3d991b2004-04-15 21:48:45 +0000438#if (CONFIG_COMMANDS & CFG_CMD_CDP)
439 case CDP:
440 CDPStart();
441 break;
442#endif
wdenk68ceb292004-08-02 21:11:11 +0000443#ifdef CONFIG_NETCONSOLE
444 case NETCONS:
445 NcStart();
446 break;
447#endif
wdenkea287de2005-04-01 00:25:43 +0000448#if (CONFIG_COMMANDS & CFG_CMD_SNTP)
449 case SNTP:
450 SntpStart();
451 break;
452#endif
wdenk2d966952002-10-31 22:12:35 +0000453 default:
454 break;
455 }
456
457 NetBootFileXferSize = 0;
458 break;
459 }
460
wdenkfc3e2162003-10-08 22:33:00 +0000461#if defined(CONFIG_MII) || (CONFIG_COMMANDS & CFG_CMD_MII)
462#if defined(CFG_FAULT_ECHO_LINK_DOWN) && defined(CONFIG_STATUS_LED) && defined(STATUS_LED_RED)
463 /*
wdenk42d1f032003-10-15 23:53:47 +0000464 * Echo the inverted link state to the fault LED.
wdenkfc3e2162003-10-08 22:33:00 +0000465 */
Marian Balakowicz63ff0042005-10-28 22:30:33 +0200466 if(miiphy_link(eth_get_dev()->name, CFG_FAULT_MII_ADDR)) {
wdenkfc3e2162003-10-08 22:33:00 +0000467 status_led_set (STATUS_LED_RED, STATUS_LED_OFF);
468 } else {
469 status_led_set (STATUS_LED_RED, STATUS_LED_ON);
470 }
471#endif /* CFG_FAULT_ECHO_LINK_DOWN, ... */
472#endif /* CONFIG_MII, ... */
wdenk2d966952002-10-31 22:12:35 +0000473
474 /*
475 * Main packet reception loop. Loop receiving packets until
wdenk59acc292005-04-03 14:18:51 +0000476 * someone sets `NetState' to a state that terminates.
wdenk2d966952002-10-31 22:12:35 +0000477 */
478 for (;;) {
479 WATCHDOG_RESET();
480#ifdef CONFIG_SHOW_ACTIVITY
481 {
482 extern void show_activity(int arg);
483 show_activity(1);
484 }
485#endif
486 /*
487 * Check the ethernet for a new packet. The ethernet
488 * receive routine will process it.
489 */
490 eth_rx();
491
492 /*
493 * Abort if ctrl-c was pressed.
494 */
495 if (ctrlc()) {
wdenk8bde7f72003-06-27 21:31:46 +0000496 eth_halt();
wdenk4b9206e2004-03-23 22:14:11 +0000497 puts ("\nAbort\n");
wdenk1d0350e2002-11-11 21:14:20 +0000498 return (-1);
wdenk2d966952002-10-31 22:12:35 +0000499 }
500
wdenk73a8b272003-06-05 19:27:42 +0000501 ArpTimeoutCheck();
wdenk2d966952002-10-31 22:12:35 +0000502
503 /*
504 * Check for a timeout, and run the timeout handler
505 * if we have one.
506 */
wdenke0ac62d2003-08-17 18:55:18 +0000507 if (timeHandler && ((get_timer(0) - timeStart) > timeDelta)) {
wdenk2d966952002-10-31 22:12:35 +0000508 thand_f *x;
509
wdenkfc3e2162003-10-08 22:33:00 +0000510#if defined(CONFIG_MII) || (CONFIG_COMMANDS & CFG_CMD_MII)
wdenk63153492005-04-03 20:55:38 +0000511# if defined(CFG_FAULT_ECHO_LINK_DOWN) && \
512 defined(CONFIG_STATUS_LED) && \
wdenk59acc292005-04-03 14:18:51 +0000513 defined(STATUS_LED_RED)
wdenkfc3e2162003-10-08 22:33:00 +0000514 /*
wdenk42d1f032003-10-15 23:53:47 +0000515 * Echo the inverted link state to the fault LED.
wdenkfc3e2162003-10-08 22:33:00 +0000516 */
Marian Balakowicz63ff0042005-10-28 22:30:33 +0200517 if(miiphy_link(eth_get_dev()->name, CFG_FAULT_MII_ADDR)) {
wdenkfc3e2162003-10-08 22:33:00 +0000518 status_led_set (STATUS_LED_RED, STATUS_LED_OFF);
519 } else {
520 status_led_set (STATUS_LED_RED, STATUS_LED_ON);
521 }
wdenk59acc292005-04-03 14:18:51 +0000522# endif /* CFG_FAULT_ECHO_LINK_DOWN, ... */
wdenkfc3e2162003-10-08 22:33:00 +0000523#endif /* CONFIG_MII, ... */
wdenk2d966952002-10-31 22:12:35 +0000524 x = timeHandler;
525 timeHandler = (thand_f *)0;
526 (*x)();
527 }
528
529
530 switch (NetState) {
531
532 case NETLOOP_RESTART:
533#ifdef CONFIG_NET_MULTI
534 NetRestarted = 1;
535#endif
536 goto restart;
537
538 case NETLOOP_SUCCESS:
539 if (NetBootFileXferSize > 0) {
540 char buf[10];
541 printf("Bytes transferred = %ld (%lx hex)\n",
542 NetBootFileXferSize,
543 NetBootFileXferSize);
544 sprintf(buf, "%lx", NetBootFileXferSize);
545 setenv("filesize", buf);
wdenka3d991b2004-04-15 21:48:45 +0000546
547 sprintf(buf, "%lX", (unsigned long)load_addr);
548 setenv("fileaddr", buf);
wdenk2d966952002-10-31 22:12:35 +0000549 }
550 eth_halt();
551 return NetBootFileXferSize;
552
553 case NETLOOP_FAIL:
wdenk1d0350e2002-11-11 21:14:20 +0000554 return (-1);
wdenk2d966952002-10-31 22:12:35 +0000555 }
556 }
557}
558
559/**********************************************************************/
560
561static void
562startAgainTimeout(void)
563{
564 NetState = NETLOOP_RESTART;
565}
566
567static void
568startAgainHandler(uchar * pkt, unsigned dest, unsigned src, unsigned len)
569{
570 /* Totally ignore the packet */
571}
572
wdenk6e592382004-04-18 17:39:38 +0000573void NetStartAgain (void)
wdenk2d966952002-10-31 22:12:35 +0000574{
wdenk6e592382004-04-18 17:39:38 +0000575#ifdef CONFIG_NET_MULTI
wdenka3d991b2004-04-15 21:48:45 +0000576 DECLARE_GLOBAL_DATA_PTR;
wdenk6e592382004-04-18 17:39:38 +0000577#endif
578 char *nretry;
579 int noretry = 0, once = 0;
wdenka3d991b2004-04-15 21:48:45 +0000580
wdenk6e592382004-04-18 17:39:38 +0000581 if ((nretry = getenv ("netretry")) != NULL) {
582 noretry = (strcmp (nretry, "no") == 0);
583 once = (strcmp (nretry, "once") == 0);
584 }
585 if (noretry) {
586 eth_halt ();
wdenka3d991b2004-04-15 21:48:45 +0000587 NetState = NETLOOP_FAIL;
588 return;
589 }
wdenk2d966952002-10-31 22:12:35 +0000590#ifndef CONFIG_NET_MULTI
wdenk6e592382004-04-18 17:39:38 +0000591 NetSetTimeout (10 * CFG_HZ, startAgainTimeout);
592 NetSetHandler (startAgainHandler);
593#else /* !CONFIG_NET_MULTI*/
594 eth_halt ();
595 eth_try_another (!NetRestarted);
596 eth_init (gd->bd);
597 if (NetRestartWrap) {
wdenk2d966952002-10-31 22:12:35 +0000598 NetRestartWrap = 0;
wdenk6e592382004-04-18 17:39:38 +0000599 if (NetDevExists && !once) {
600 NetSetTimeout (10 * CFG_HZ, startAgainTimeout);
601 NetSetHandler (startAgainHandler);
602 } else {
wdenk2d966952002-10-31 22:12:35 +0000603 NetState = NETLOOP_FAIL;
604 }
wdenk6e592382004-04-18 17:39:38 +0000605 } else {
wdenk2d966952002-10-31 22:12:35 +0000606 NetState = NETLOOP_RESTART;
607 }
wdenk6e592382004-04-18 17:39:38 +0000608#endif /* CONFIG_NET_MULTI */
wdenk2d966952002-10-31 22:12:35 +0000609}
610
611/**********************************************************************/
612/*
613 * Miscelaneous bits.
614 */
615
616void
617NetSetHandler(rxhand_f * f)
618{
619 packetHandler = f;
620}
621
622
623void
wdenk3e01d752004-10-09 21:56:21 +0000624NetSetTimeout(ulong iv, thand_f * f)
wdenk2d966952002-10-31 22:12:35 +0000625{
626 if (iv == 0) {
627 timeHandler = (thand_f *)0;
628 } else {
629 timeHandler = f;
wdenke0ac62d2003-08-17 18:55:18 +0000630 timeStart = get_timer(0);
631 timeDelta = iv;
wdenk2d966952002-10-31 22:12:35 +0000632 }
633}
634
635
636void
637NetSendPacket(volatile uchar * pkt, int len)
638{
639 (void) eth_send(pkt, len);
640}
641
wdenk73a8b272003-06-05 19:27:42 +0000642int
643NetSendUDPPacket(uchar *ether, IPaddr_t dest, int dport, int sport, int len)
644{
wdenka3d991b2004-04-15 21:48:45 +0000645 uchar *pkt;
646
wdenk73a8b272003-06-05 19:27:42 +0000647 /* convert to new style broadcast */
648 if (dest == 0)
649 dest = 0xFFFFFFFF;
wdenk2d966952002-10-31 22:12:35 +0000650
wdenk73a8b272003-06-05 19:27:42 +0000651 /* if broadcast, make the ether address a broadcast and don't do ARP */
652 if (dest == 0xFFFFFFFF)
653 ether = NetBcastAddr;
654
655 /* if MAC address was not discovered yet, save the packet and do an ARP request */
656 if (memcmp(ether, NetEtherNullAddr, 6) == 0) {
657
658#ifdef ET_DEBUG
659 printf("sending ARP for %08lx\n", dest);
660#endif
wdenk73a8b272003-06-05 19:27:42 +0000661 NetArpWaitPacketIP = dest;
662 NetArpWaitPacketMAC = ether;
wdenka3d991b2004-04-15 21:48:45 +0000663
664 pkt = NetArpWaitTxPacket;
665 pkt += NetSetEther (pkt, NetArpWaitPacketMAC, PROT_IP);
666
667 NetSetIP (pkt, dest, dport, sport, len);
668 memcpy(pkt + IP_HDR_SIZE, (uchar *)NetTxPacket + (pkt - (uchar *)NetArpWaitTxPacket) + IP_HDR_SIZE, len);
wdenk73a8b272003-06-05 19:27:42 +0000669
670 /* size of the waiting packet */
wdenka3d991b2004-04-15 21:48:45 +0000671 NetArpWaitTxPacketSize = (pkt - NetArpWaitTxPacket) + IP_HDR_SIZE + len;
wdenk73a8b272003-06-05 19:27:42 +0000672
673 /* and do the ARP request */
674 NetArpWaitTry = 1;
675 NetArpWaitTimerStart = get_timer(0);
676 ArpRequest();
677 return 1; /* waiting */
678 }
679
680#ifdef ET_DEBUG
681 printf("sending UDP to %08lx/%02x:%02x:%02x:%02x:%02x:%02x\n",
wdenk6e592382004-04-18 17:39:38 +0000682 dest, ether[0], ether[1], ether[2], ether[3], ether[4], ether[5]);
wdenk73a8b272003-06-05 19:27:42 +0000683#endif
684
wdenka3d991b2004-04-15 21:48:45 +0000685 pkt = (uchar *)NetTxPacket;
686 pkt += NetSetEther (pkt, ether, PROT_IP);
687 NetSetIP (pkt, dest, dport, sport, len);
688 (void) eth_send(NetTxPacket, (pkt - NetTxPacket) + IP_HDR_SIZE + len);
wdenk73a8b272003-06-05 19:27:42 +0000689
wdenk6e592382004-04-18 17:39:38 +0000690 return 0; /* transmitted */
wdenk73a8b272003-06-05 19:27:42 +0000691}
692
693#if (CONFIG_COMMANDS & CFG_CMD_PING)
694static ushort PingSeqNo;
695
696int PingSend(void)
697{
698 static uchar mac[6];
699 volatile IP_t *ip;
700 volatile ushort *s;
wdenka3d991b2004-04-15 21:48:45 +0000701 uchar *pkt;
wdenk73a8b272003-06-05 19:27:42 +0000702
703 /* XXX always send arp request */
704
705 memcpy(mac, NetEtherNullAddr, 6);
706
707#ifdef ET_DEBUG
708 printf("sending ARP for %08lx\n", NetPingIP);
709#endif
710
711 NetArpWaitPacketIP = NetPingIP;
712 NetArpWaitPacketMAC = mac;
713
wdenka3d991b2004-04-15 21:48:45 +0000714 pkt = NetArpWaitTxPacket;
715 pkt += NetSetEther(pkt, mac, PROT_IP);
wdenk73a8b272003-06-05 19:27:42 +0000716
wdenka3d991b2004-04-15 21:48:45 +0000717 ip = (volatile IP_t *)pkt;
wdenk73a8b272003-06-05 19:27:42 +0000718
719 /*
720 * Construct an IP and ICMP header. (need to set no fragment bit - XXX)
721 */
722 ip->ip_hl_v = 0x45; /* IP_HDR_SIZE / 4 (not including UDP) */
723 ip->ip_tos = 0;
724 ip->ip_len = htons(IP_HDR_SIZE_NO_UDP + 8);
725 ip->ip_id = htons(NetIPID++);
726 ip->ip_off = htons(0x4000); /* No fragmentation */
727 ip->ip_ttl = 255;
728 ip->ip_p = 0x01; /* ICMP */
729 ip->ip_sum = 0;
730 NetCopyIP((void*)&ip->ip_src, &NetOurIP); /* already in network byte order */
731 NetCopyIP((void*)&ip->ip_dst, &NetPingIP); /* - "" - */
732 ip->ip_sum = ~NetCksum((uchar *)ip, IP_HDR_SIZE_NO_UDP / 2);
733
734 s = &ip->udp_src; /* XXX ICMP starts here */
735 s[0] = htons(0x0800); /* echo-request, code */
736 s[1] = 0; /* checksum */
737 s[2] = 0; /* identifier */
738 s[3] = htons(PingSeqNo++); /* sequence number */
739 s[1] = ~NetCksum((uchar *)s, 8/2);
740
741 /* size of the waiting packet */
wdenka3d991b2004-04-15 21:48:45 +0000742 NetArpWaitTxPacketSize = (pkt - NetArpWaitTxPacket) + IP_HDR_SIZE_NO_UDP + 8;
wdenk73a8b272003-06-05 19:27:42 +0000743
744 /* and do the ARP request */
745 NetArpWaitTry = 1;
746 NetArpWaitTimerStart = get_timer(0);
747 ArpRequest();
748 return 1; /* waiting */
749}
750
751static void
752PingTimeout (void)
753{
754 eth_halt();
755 NetState = NETLOOP_FAIL; /* we did not get the reply */
756}
757
758static void
759PingHandler (uchar * pkt, unsigned dest, unsigned src, unsigned len)
760{
761 IPaddr_t tmp;
762 volatile IP_t *ip = (volatile IP_t *)pkt;
763
764 tmp = NetReadIP((void *)&ip->ip_src);
765 if (tmp != NetPingIP)
766 return;
767
768 NetState = NETLOOP_SUCCESS;
769}
770
771static void PingStart(void)
772{
wdenka3d991b2004-04-15 21:48:45 +0000773#if defined(CONFIG_NET_MULTI)
774 printf ("Using %s device\n", eth_get_name());
wdenk6e592382004-04-18 17:39:38 +0000775#endif /* CONFIG_NET_MULTI */
wdenk73a8b272003-06-05 19:27:42 +0000776 NetSetTimeout (10 * CFG_HZ, PingTimeout);
777 NetSetHandler (PingHandler);
778
779 PingSend();
780}
wdenk6e592382004-04-18 17:39:38 +0000781#endif /* CFG_CMD_PING */
wdenk2d966952002-10-31 22:12:35 +0000782
wdenka3d991b2004-04-15 21:48:45 +0000783#if (CONFIG_COMMANDS & CFG_CMD_CDP)
784
785#define CDP_DEVICE_ID_TLV 0x0001
786#define CDP_ADDRESS_TLV 0x0002
787#define CDP_PORT_ID_TLV 0x0003
788#define CDP_CAPABILITIES_TLV 0x0004
789#define CDP_VERSION_TLV 0x0005
790#define CDP_PLATFORM_TLV 0x0006
791#define CDP_NATIVE_VLAN_TLV 0x000a
792#define CDP_APPLIANCE_VLAN_TLV 0x000e
793#define CDP_TRIGGER_TLV 0x000f
794#define CDP_POWER_CONSUMPTION_TLV 0x0010
795#define CDP_SYSNAME_TLV 0x0014
796#define CDP_SYSOBJECT_TLV 0x0015
797#define CDP_MANAGEMENT_ADDRESS_TLV 0x0016
798
799#define CDP_TIMEOUT (CFG_HZ/4) /* one packet every 250ms */
800
801static int CDPSeq;
802static int CDPOK;
803
804ushort CDPNativeVLAN;
805ushort CDPApplianceVLAN;
806
807static const uchar CDP_SNAP_hdr[8] = { 0xAA, 0xAA, 0x03, 0x00, 0x00, 0x0C, 0x20, 0x00 };
808
809static ushort CDP_compute_csum(const uchar *buff, ushort len)
810{
811 ushort csum;
812 int odd;
813 ulong result = 0;
814 ushort leftover;
Wolfgang Denk77ddac92005-10-13 16:45:02 +0200815 ushort *p;
wdenka3d991b2004-04-15 21:48:45 +0000816
817 if (len > 0) {
818 odd = 1 & (ulong)buff;
819 if (odd) {
820 result = *buff << 8;
821 len--;
822 buff++;
823 }
824 while (len > 1) {
Wolfgang Denk77ddac92005-10-13 16:45:02 +0200825 p = (ushort *)buff;
826 result += *p++;
827 buff = (uchar *)p;
wdenka3d991b2004-04-15 21:48:45 +0000828 if (result & 0x80000000)
829 result = (result & 0xFFFF) + (result >> 16);
830 len -= 2;
831 }
832 if (len) {
833 leftover = (signed short)(*(const signed char *)buff);
Wolfgang Denk3ada8342005-11-10 20:59:46 +0100834 /* CISCO SUCKS big time! (and blows too):
835 * CDP uses the IP checksum algorithm with a twist;
836 * for the last byte it *sign* extends and sums.
837 */
wdenka3d991b2004-04-15 21:48:45 +0000838 result = (result & 0xffff0000) | ((result + leftover) & 0x0000ffff);
839 }
840 while (result >> 16)
841 result = (result & 0xFFFF) + (result >> 16);
842
843 if (odd)
844 result = ((result >> 8) & 0xff) | ((result & 0xff) << 8);
845 }
846
847 /* add up 16-bit and 17-bit words for 17+c bits */
848 result = (result & 0xffff) + (result >> 16);
849 /* add up 16-bit and 2-bit for 16+c bit */
850 result = (result & 0xffff) + (result >> 16);
851 /* add up carry.. */
852 result = (result & 0xffff) + (result >> 16);
853
854 /* negate */
855 csum = ~(ushort)result;
856
857 /* run time endian detection */
858 if (csum != htons(csum)) /* little endian */
859 csum = htons(csum);
860
861 return csum;
862}
863
864int CDPSendTrigger(void)
865{
866 volatile uchar *pkt;
867 volatile ushort *s;
868 volatile ushort *cp;
869 Ethernet_t *et;
wdenka3d991b2004-04-15 21:48:45 +0000870 int len;
871 ushort chksum;
wdenk6e592382004-04-18 17:39:38 +0000872#if defined(CONFIG_CDP_DEVICE_ID) || defined(CONFIG_CDP_PORT_ID) || \
873 defined(CONFIG_CDP_VERSION) || defined(CONFIG_CDP_PLATFORM)
874 char buf[32];
875#endif
wdenka3d991b2004-04-15 21:48:45 +0000876
877 pkt = NetTxPacket;
878 et = (Ethernet_t *)pkt;
879
880 /* NOTE: trigger sent not on any VLAN */
881
882 /* form ethernet header */
883 memcpy(et->et_dest, NetCDPAddr, 6);
884 memcpy(et->et_src, NetOurEther, 6);
885
886 pkt += ETHER_HDR_SIZE;
887
888 /* SNAP header */
889 memcpy((uchar *)pkt, CDP_SNAP_hdr, sizeof(CDP_SNAP_hdr));
890 pkt += sizeof(CDP_SNAP_hdr);
891
892 /* CDP header */
893 *pkt++ = 0x02; /* CDP version 2 */
894 *pkt++ = 180; /* TTL */
895 s = (volatile ushort *)pkt;
896 cp = s;
897 *s++ = htons(0); /* checksum (0 for later calculation) */
898
899 /* CDP fields */
900#ifdef CONFIG_CDP_DEVICE_ID
901 *s++ = htons(CDP_DEVICE_ID_TLV);
902 *s++ = htons(CONFIG_CDP_DEVICE_ID);
903 memset(buf, 0, sizeof(buf));
904 sprintf(buf, CONFIG_CDP_DEVICE_ID_PREFIX "%02X%02X%02X%02X%02X%02X",
905 NetOurEther[0] & 0xff, NetOurEther[1] & 0xff,
906 NetOurEther[2] & 0xff, NetOurEther[3] & 0xff,
907 NetOurEther[4] & 0xff, NetOurEther[5] & 0xff);
908 memcpy((uchar *)s, buf, 16);
909 s += 16 / 2;
910#endif
911
912#ifdef CONFIG_CDP_PORT_ID
913 *s++ = htons(CDP_PORT_ID_TLV);
914 memset(buf, 0, sizeof(buf));
915 sprintf(buf, CONFIG_CDP_PORT_ID, eth_get_dev_index());
916 len = strlen(buf);
917 if (len & 1) /* make it even */
918 len++;
919 *s++ = htons(len + 4);
920 memcpy((uchar *)s, buf, len);
921 s += len / 2;
922#endif
923
924#ifdef CONFIG_CDP_CAPABILITIES
925 *s++ = htons(CDP_CAPABILITIES_TLV);
926 *s++ = htons(8);
927 *(ulong *)s = htonl(CONFIG_CDP_CAPABILITIES);
928 s += 2;
929#endif
930
931#ifdef CONFIG_CDP_VERSION
932 *s++ = htons(CDP_VERSION_TLV);
933 memset(buf, 0, sizeof(buf));
934 strcpy(buf, CONFIG_CDP_VERSION);
935 len = strlen(buf);
936 if (len & 1) /* make it even */
937 len++;
938 *s++ = htons(len + 4);
939 memcpy((uchar *)s, buf, len);
940 s += len / 2;
941#endif
942
943#ifdef CONFIG_CDP_PLATFORM
944 *s++ = htons(CDP_PLATFORM_TLV);
945 memset(buf, 0, sizeof(buf));
946 strcpy(buf, CONFIG_CDP_PLATFORM);
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_TRIGGER
956 *s++ = htons(CDP_TRIGGER_TLV);
957 *s++ = htons(8);
958 *(ulong *)s = htonl(CONFIG_CDP_TRIGGER);
959 s += 2;
960#endif
961
962#ifdef CONFIG_CDP_POWER_CONSUMPTION
963 *s++ = htons(CDP_POWER_CONSUMPTION_TLV);
964 *s++ = htons(6);
965 *s++ = htons(CONFIG_CDP_POWER_CONSUMPTION);
966#endif
967
968 /* length of ethernet packet */
969 len = (uchar *)s - ((uchar *)NetTxPacket + ETHER_HDR_SIZE);
970 et->et_protlen = htons(len);
971
972 len = ETHER_HDR_SIZE + sizeof(CDP_SNAP_hdr);
973 chksum = CDP_compute_csum((uchar *)NetTxPacket + len, (uchar *)s - (NetTxPacket + len));
974 if (chksum == 0)
975 chksum = 0xFFFF;
976 *cp = htons(chksum);
977
978 (void) eth_send(NetTxPacket, (uchar *)s - NetTxPacket);
979 return 0;
980}
981
982static void
983CDPTimeout (void)
984{
985 CDPSeq++;
986
987 if (CDPSeq < 3) {
988 NetSetTimeout (CDP_TIMEOUT, CDPTimeout);
989 CDPSendTrigger();
990 return;
991 }
992
993 /* if not OK try again */
994 if (!CDPOK)
995 NetStartAgain();
996 else
997 NetState = NETLOOP_SUCCESS;
998}
999
1000static void
1001CDPDummyHandler (uchar * pkt, unsigned dest, unsigned src, unsigned len)
1002{
1003 /* nothing */
1004}
1005
1006static void
1007CDPHandler(const uchar * pkt, unsigned len)
1008{
1009 const uchar *t;
1010 const ushort *ss;
1011 ushort type, tlen;
1012 uchar applid;
1013 ushort vlan, nvlan;
1014
1015 /* minimum size? */
1016 if (len < sizeof(CDP_SNAP_hdr) + 4)
1017 goto pkt_short;
1018
1019 /* check for valid CDP SNAP header */
1020 if (memcmp(pkt, CDP_SNAP_hdr, sizeof(CDP_SNAP_hdr)) != 0)
1021 return;
1022
1023 pkt += sizeof(CDP_SNAP_hdr);
1024 len -= sizeof(CDP_SNAP_hdr);
1025
1026 /* Version of CDP protocol must be >= 2 and TTL != 0 */
1027 if (pkt[0] < 0x02 || pkt[1] == 0)
1028 return;
1029
1030 /* if version is greater than 0x02 maybe we'll have a problem; output a warning */
1031 if (pkt[0] != 0x02)
1032 printf("** WARNING: CDP packet received with a protocol version %d > 2\n",
1033 pkt[0] & 0xff);
1034
1035 if (CDP_compute_csum(pkt, len) != 0)
1036 return;
1037
1038 pkt += 4;
1039 len -= 4;
1040
1041 vlan = htons(-1);
1042 nvlan = htons(-1);
1043 while (len > 0) {
1044 if (len < 4)
1045 goto pkt_short;
1046
1047 ss = (const ushort *)pkt;
1048 type = ntohs(ss[0]);
1049 tlen = ntohs(ss[1]);
1050 if (tlen > len) {
1051 goto pkt_short;
1052 }
1053
1054 pkt += tlen;
1055 len -= tlen;
1056
1057 ss += 2; /* point ss to the data of the TLV */
1058 tlen -= 4;
1059
1060 switch (type) {
1061 case CDP_DEVICE_ID_TLV:
1062 break;
1063 case CDP_ADDRESS_TLV:
1064 break;
1065 case CDP_PORT_ID_TLV:
1066 break;
1067 case CDP_CAPABILITIES_TLV:
1068 break;
1069 case CDP_VERSION_TLV:
1070 break;
1071 case CDP_PLATFORM_TLV:
1072 break;
1073 case CDP_NATIVE_VLAN_TLV:
1074 nvlan = *ss;
1075 break;
1076 case CDP_APPLIANCE_VLAN_TLV:
1077 t = (const uchar *)ss;
1078 while (tlen > 0) {
1079 if (tlen < 3)
1080 goto pkt_short;
1081
1082 applid = t[0];
1083 ss = (const ushort *)(t + 1);
1084
1085#ifdef CONFIG_CDP_APPLIANCE_VLAN_TYPE
1086 if (applid == CONFIG_CDP_APPLIANCE_VLAN_TYPE)
1087 vlan = *ss;
1088#else
1089 vlan = ntohs(*ss); /* XXX will this work; dunno */
1090#endif
1091 t += 3; tlen -= 3;
1092 }
1093 break;
1094 case CDP_TRIGGER_TLV:
1095 break;
1096 case CDP_POWER_CONSUMPTION_TLV:
1097 break;
1098 case CDP_SYSNAME_TLV:
1099 break;
1100 case CDP_SYSOBJECT_TLV:
1101 break;
1102 case CDP_MANAGEMENT_ADDRESS_TLV:
1103 break;
1104 }
1105 }
1106
1107 CDPApplianceVLAN = vlan;
1108 CDPNativeVLAN = nvlan;
1109
1110 CDPOK = 1;
1111 return;
1112
1113 pkt_short:
1114 printf("** CDP packet is too short\n");
1115 return;
1116}
1117
1118static void CDPStart(void)
1119{
1120#if defined(CONFIG_NET_MULTI)
1121 printf ("Using %s device\n", eth_get_name());
1122#endif
1123 CDPSeq = 0;
1124 CDPOK = 0;
1125
1126 CDPNativeVLAN = htons(-1);
1127 CDPApplianceVLAN = htons(-1);
1128
1129 NetSetTimeout (CDP_TIMEOUT, CDPTimeout);
1130 NetSetHandler (CDPDummyHandler);
1131
1132 CDPSendTrigger();
1133}
wdenk6e592382004-04-18 17:39:38 +00001134#endif /* CFG_CMD_CDP */
wdenka3d991b2004-04-15 21:48:45 +00001135
1136
wdenk2d966952002-10-31 22:12:35 +00001137void
wdenka3d991b2004-04-15 21:48:45 +00001138NetReceive(volatile uchar * inpkt, int len)
wdenk2d966952002-10-31 22:12:35 +00001139{
1140 Ethernet_t *et;
1141 IP_t *ip;
1142 ARP_t *arp;
1143 IPaddr_t tmp;
1144 int x;
wdenka3d991b2004-04-15 21:48:45 +00001145 uchar *pkt;
1146#if (CONFIG_COMMANDS & CFG_CMD_CDP)
1147 int iscdp;
1148#endif
1149 ushort cti = 0, vlanid = VLAN_NONE, myvlanid, mynvlanid;
wdenk2d966952002-10-31 22:12:35 +00001150
wdenka3d991b2004-04-15 21:48:45 +00001151#ifdef ET_DEBUG
1152 printf("packet received\n");
1153#endif
1154
1155 NetRxPkt = inpkt;
wdenk2d966952002-10-31 22:12:35 +00001156 NetRxPktLen = len;
wdenka3d991b2004-04-15 21:48:45 +00001157 et = (Ethernet_t *)inpkt;
1158
1159 /* too small packet? */
1160 if (len < ETHER_HDR_SIZE)
1161 return;
1162
1163#if (CONFIG_COMMANDS & CFG_CMD_CDP)
1164 /* keep track if packet is CDP */
1165 iscdp = memcmp(et->et_dest, NetCDPAddr, 6) == 0;
1166#endif
1167
1168 myvlanid = ntohs(NetOurVLAN);
1169 if (myvlanid == (ushort)-1)
1170 myvlanid = VLAN_NONE;
1171 mynvlanid = ntohs(NetOurNativeVLAN);
1172 if (mynvlanid == (ushort)-1)
1173 mynvlanid = VLAN_NONE;
wdenk2d966952002-10-31 22:12:35 +00001174
1175 x = ntohs(et->et_protlen);
1176
wdenka3d991b2004-04-15 21:48:45 +00001177#ifdef ET_DEBUG
1178 printf("packet received\n");
1179#endif
1180
wdenk2d966952002-10-31 22:12:35 +00001181 if (x < 1514) {
1182 /*
1183 * Got a 802 packet. Check the other protocol field.
1184 */
1185 x = ntohs(et->et_prot);
wdenka3d991b2004-04-15 21:48:45 +00001186
1187 ip = (IP_t *)(inpkt + E802_HDR_SIZE);
wdenk2d966952002-10-31 22:12:35 +00001188 len -= E802_HDR_SIZE;
wdenka3d991b2004-04-15 21:48:45 +00001189
1190 } else if (x != PROT_VLAN) { /* normal packet */
1191 ip = (IP_t *)(inpkt + ETHER_HDR_SIZE);
wdenk2d966952002-10-31 22:12:35 +00001192 len -= ETHER_HDR_SIZE;
wdenka3d991b2004-04-15 21:48:45 +00001193
1194 } else { /* VLAN packet */
1195 VLAN_Ethernet_t *vet = (VLAN_Ethernet_t *)et;
1196
1197#ifdef ET_DEBUG
1198 printf("VLAN packet received\n");
1199#endif
1200 /* too small packet? */
1201 if (len < VLAN_ETHER_HDR_SIZE)
1202 return;
1203
1204 /* if no VLAN active */
1205 if ((ntohs(NetOurVLAN) & VLAN_IDMASK) == VLAN_NONE
1206#if (CONFIG_COMMANDS & CFG_CMD_CDP)
1207 && iscdp == 0
1208#endif
1209 )
1210 return;
1211
1212 cti = ntohs(vet->vet_tag);
1213 vlanid = cti & VLAN_IDMASK;
1214 x = ntohs(vet->vet_type);
1215
1216 ip = (IP_t *)(inpkt + VLAN_ETHER_HDR_SIZE);
1217 len -= VLAN_ETHER_HDR_SIZE;
wdenk2d966952002-10-31 22:12:35 +00001218 }
1219
1220#ifdef ET_DEBUG
1221 printf("Receive from protocol 0x%x\n", x);
1222#endif
1223
wdenka3d991b2004-04-15 21:48:45 +00001224#if (CONFIG_COMMANDS & CFG_CMD_CDP)
1225 if (iscdp) {
1226 CDPHandler((uchar *)ip, len);
1227 return;
1228 }
1229#endif
1230
1231 if ((myvlanid & VLAN_IDMASK) != VLAN_NONE) {
1232 if (vlanid == VLAN_NONE)
1233 vlanid = (mynvlanid & VLAN_IDMASK);
1234 /* not matched? */
1235 if (vlanid != (myvlanid & VLAN_IDMASK))
1236 return;
1237 }
1238
wdenk2d966952002-10-31 22:12:35 +00001239 switch (x) {
1240
1241 case PROT_ARP:
1242 /*
1243 * We have to deal with two types of ARP packets:
wdenk8bde7f72003-06-27 21:31:46 +00001244 * - REQUEST packets will be answered by sending our
1245 * IP address - if we know it.
1246 * - REPLY packates are expected only after we asked
1247 * for the TFTP server's or the gateway's ethernet
1248 * address; so if we receive such a packet, we set
1249 * the server ethernet address
wdenk2d966952002-10-31 22:12:35 +00001250 */
1251#ifdef ET_DEBUG
wdenk4b9206e2004-03-23 22:14:11 +00001252 puts ("Got ARP\n");
wdenk2d966952002-10-31 22:12:35 +00001253#endif
1254 arp = (ARP_t *)ip;
1255 if (len < ARP_HDR_SIZE) {
1256 printf("bad length %d < %d\n", len, ARP_HDR_SIZE);
1257 return;
1258 }
1259 if (ntohs(arp->ar_hrd) != ARP_ETHER) {
1260 return;
1261 }
1262 if (ntohs(arp->ar_pro) != PROT_IP) {
1263 return;
1264 }
1265 if (arp->ar_hln != 6) {
1266 return;
1267 }
1268 if (arp->ar_pln != 4) {
1269 return;
1270 }
1271
1272 if (NetOurIP == 0) {
1273 return;
1274 }
1275
1276 if (NetReadIP(&arp->ar_data[16]) != NetOurIP) {
1277 return;
1278 }
1279
1280 switch (ntohs(arp->ar_op)) {
1281 case ARPOP_REQUEST: /* reply with our IP address */
1282#ifdef ET_DEBUG
wdenk4b9206e2004-03-23 22:14:11 +00001283 puts ("Got ARP REQUEST, return our IP\n");
wdenk2d966952002-10-31 22:12:35 +00001284#endif
wdenka3d991b2004-04-15 21:48:45 +00001285 pkt = (uchar *)et;
1286 pkt += NetSetEther(pkt, et->et_src, PROT_ARP);
wdenk2d966952002-10-31 22:12:35 +00001287 arp->ar_op = htons(ARPOP_REPLY);
1288 memcpy (&arp->ar_data[10], &arp->ar_data[0], 6);
1289 NetCopyIP(&arp->ar_data[16], &arp->ar_data[6]);
1290 memcpy (&arp->ar_data[ 0], NetOurEther, 6);
1291 NetCopyIP(&arp->ar_data[ 6], &NetOurIP);
wdenka3d991b2004-04-15 21:48:45 +00001292 (void) eth_send((uchar *)et, (pkt - (uchar *)et) + ARP_HDR_SIZE);
wdenk2d966952002-10-31 22:12:35 +00001293 return;
wdenk73a8b272003-06-05 19:27:42 +00001294
1295 case ARPOP_REPLY: /* arp reply */
1296 /* are we waiting for a reply */
1297 if (!NetArpWaitPacketIP || !NetArpWaitPacketMAC)
1298 break;
wdenk2d966952002-10-31 22:12:35 +00001299#ifdef ET_DEBUG
wdenk73a8b272003-06-05 19:27:42 +00001300 printf("Got ARP REPLY, set server/gtwy eth addr (%02x:%02x:%02x:%02x:%02x:%02x)\n",
1301 arp->ar_data[0], arp->ar_data[1],
1302 arp->ar_data[2], arp->ar_data[3],
1303 arp->ar_data[4], arp->ar_data[5]);
wdenk2d966952002-10-31 22:12:35 +00001304#endif
wdenk73a8b272003-06-05 19:27:42 +00001305
1306 tmp = NetReadIP(&arp->ar_data[6]);
1307
1308 /* matched waiting packet's address */
1309 if (tmp == NetArpWaitReplyIP) {
1310#ifdef ET_DEBUG
wdenk4b9206e2004-03-23 22:14:11 +00001311 puts ("Got it\n");
wdenk73a8b272003-06-05 19:27:42 +00001312#endif
1313 /* save address for later use */
1314 memcpy(NetArpWaitPacketMAC, &arp->ar_data[0], 6);
1315
wdenk68ceb292004-08-02 21:11:11 +00001316#ifdef CONFIG_NETCONSOLE
1317 (*packetHandler)(0,0,0,0);
1318#endif
wdenk73a8b272003-06-05 19:27:42 +00001319 /* modify header, and transmit it */
1320 memcpy(((Ethernet_t *)NetArpWaitTxPacket)->et_dest, NetArpWaitPacketMAC, 6);
1321 (void) eth_send(NetArpWaitTxPacket, NetArpWaitTxPacketSize);
1322
1323 /* no arp request pending now */
1324 NetArpWaitPacketIP = 0;
1325 NetArpWaitTxPacketSize = 0;
1326 NetArpWaitPacketMAC = NULL;
1327
1328 }
wdenk2d966952002-10-31 22:12:35 +00001329 return;
1330 default:
1331#ifdef ET_DEBUG
1332 printf("Unexpected ARP opcode 0x%x\n", ntohs(arp->ar_op));
1333#endif
1334 return;
1335 }
wdenk289f9322005-01-12 00:15:14 +00001336 break;
wdenk2d966952002-10-31 22:12:35 +00001337
1338 case PROT_RARP:
1339#ifdef ET_DEBUG
wdenk4b9206e2004-03-23 22:14:11 +00001340 puts ("Got RARP\n");
wdenk2d966952002-10-31 22:12:35 +00001341#endif
1342 arp = (ARP_t *)ip;
1343 if (len < ARP_HDR_SIZE) {
1344 printf("bad length %d < %d\n", len, ARP_HDR_SIZE);
1345 return;
1346 }
1347
1348 if ((ntohs(arp->ar_op) != RARPOP_REPLY) ||
1349 (ntohs(arp->ar_hrd) != ARP_ETHER) ||
1350 (ntohs(arp->ar_pro) != PROT_IP) ||
1351 (arp->ar_hln != 6) || (arp->ar_pln != 4)) {
1352
wdenk4b9206e2004-03-23 22:14:11 +00001353 puts ("invalid RARP header\n");
wdenk2d966952002-10-31 22:12:35 +00001354 } else {
1355 NetCopyIP(&NetOurIP, &arp->ar_data[16]);
wdenk3d3befa2004-03-14 15:06:13 +00001356 if (NetServerIP == 0)
1357 NetCopyIP(&NetServerIP, &arp->ar_data[ 6]);
wdenk2d966952002-10-31 22:12:35 +00001358 memcpy (NetServerEther, &arp->ar_data[ 0], 6);
1359
1360 (*packetHandler)(0,0,0,0);
1361 }
1362 break;
1363
1364 case PROT_IP:
1365#ifdef ET_DEBUG
wdenk4b9206e2004-03-23 22:14:11 +00001366 puts ("Got IP\n");
wdenk2d966952002-10-31 22:12:35 +00001367#endif
1368 if (len < IP_HDR_SIZE) {
1369 debug ("len bad %d < %d\n", len, IP_HDR_SIZE);
1370 return;
1371 }
1372 if (len < ntohs(ip->ip_len)) {
1373 printf("len bad %d < %d\n", len, ntohs(ip->ip_len));
1374 return;
1375 }
1376 len = ntohs(ip->ip_len);
1377#ifdef ET_DEBUG
1378 printf("len=%d, v=%02x\n", len, ip->ip_hl_v & 0xff);
1379#endif
1380 if ((ip->ip_hl_v & 0xf0) != 0x40) {
1381 return;
1382 }
1383 if (ip->ip_off & htons(0x1fff)) { /* Can't deal w/ fragments */
1384 return;
1385 }
1386 if (!NetCksumOk((uchar *)ip, IP_HDR_SIZE_NO_UDP / 2)) {
wdenk4b9206e2004-03-23 22:14:11 +00001387 puts ("checksum bad\n");
wdenk2d966952002-10-31 22:12:35 +00001388 return;
1389 }
1390 tmp = NetReadIP(&ip->ip_dst);
1391 if (NetOurIP && tmp != NetOurIP && tmp != 0xFFFFFFFF) {
1392 return;
1393 }
1394 /*
1395 * watch for ICMP host redirects
1396 *
wdenk8bde7f72003-06-27 21:31:46 +00001397 * There is no real handler code (yet). We just watch
1398 * for ICMP host redirect messages. In case anybody
1399 * sees these messages: please contact me
1400 * (wd@denx.de), or - even better - send me the
1401 * necessary fixes :-)
wdenk2d966952002-10-31 22:12:35 +00001402 *
wdenk8bde7f72003-06-27 21:31:46 +00001403 * Note: in all cases where I have seen this so far
1404 * it was a problem with the router configuration,
1405 * for instance when a router was configured in the
1406 * BOOTP reply, but the TFTP server was on the same
1407 * subnet. So this is probably a warning that your
1408 * configuration might be wrong. But I'm not really
1409 * sure if there aren't any other situations.
wdenk2d966952002-10-31 22:12:35 +00001410 */
1411 if (ip->ip_p == IPPROTO_ICMP) {
1412 ICMP_t *icmph = (ICMP_t *)&(ip->udp_src);
1413
wdenk73a8b272003-06-05 19:27:42 +00001414 switch (icmph->type) {
1415 case ICMP_REDIRECT:
wdenk90dc6702005-05-03 14:12:25 +00001416 if (icmph->code != ICMP_REDIR_HOST)
1417 return;
1418 puts (" ICMP Host Redirect to ");
1419 print_IPaddr(icmph->un.gateway);
1420 putc(' ');
Stefan Roese8534bf92005-08-12 20:06:52 +02001421 return;
wdenk73a8b272003-06-05 19:27:42 +00001422#if (CONFIG_COMMANDS & CFG_CMD_PING)
1423 case ICMP_ECHO_REPLY:
1424 /*
1425 * IP header OK. Pass the packet to the current handler.
1426 */
1427 /* XXX point to ip packet */
1428 (*packetHandler)((uchar *)ip, 0, 0, 0);
Stefan Roese8534bf92005-08-12 20:06:52 +02001429 return;
wdenk73a8b272003-06-05 19:27:42 +00001430#endif
1431 default:
1432 return;
1433 }
wdenk2d966952002-10-31 22:12:35 +00001434 } else if (ip->ip_p != IPPROTO_UDP) { /* Only UDP packets */
1435 return;
1436 }
1437
Stefan Roese8534bf92005-08-12 20:06:52 +02001438#ifdef CONFIG_UDP_CHECKSUM
1439 if (ip->udp_xsum != 0) {
Wolfgang Denkb2f50802005-08-12 23:43:12 +02001440 ulong xsum;
Stefan Roese8534bf92005-08-12 20:06:52 +02001441 ushort *sumptr;
1442 ushort sumlen;
1443
1444 xsum = ip->ip_p;
1445 xsum += (ntohs(ip->udp_len));
1446 xsum += (ntohl(ip->ip_src) >> 16) & 0x0000ffff;
1447 xsum += (ntohl(ip->ip_src) >> 0) & 0x0000ffff;
1448 xsum += (ntohl(ip->ip_dst) >> 16) & 0x0000ffff;
1449 xsum += (ntohl(ip->ip_dst) >> 0) & 0x0000ffff;
1450
1451 sumlen = ntohs(ip->udp_len);
1452 sumptr = (ushort *) &(ip->udp_src);
1453
1454 while (sumlen > 1) {
Wolfgang Denkb2f50802005-08-12 23:43:12 +02001455 ushort sumdata;
Stefan Roese8534bf92005-08-12 20:06:52 +02001456
1457 sumdata = *sumptr++;
1458 xsum += ntohs(sumdata);
1459 sumlen -= 2;
1460 }
1461 if (sumlen > 0) {
Wolfgang Denkb2f50802005-08-12 23:43:12 +02001462 ushort sumdata;
Stefan Roese8534bf92005-08-12 20:06:52 +02001463
1464 sumdata = *(unsigned char *) sumptr;
Wolfgang Denkb2f50802005-08-12 23:43:12 +02001465 sumdata = (sumdata << 8) & 0xff00;
Stefan Roese8534bf92005-08-12 20:06:52 +02001466 xsum += sumdata;
1467 }
1468 while ((xsum >> 16) != 0) {
Wolfgang Denkb2f50802005-08-12 23:43:12 +02001469 xsum = (xsum & 0x0000ffff) + ((xsum >> 16) & 0x0000ffff);
Stefan Roese8534bf92005-08-12 20:06:52 +02001470 }
1471 if ((xsum != 0x00000000) && (xsum != 0x0000ffff)) {
Wolfgang Denkb2f50802005-08-12 23:43:12 +02001472 printf(" UDP wrong checksum %08x %08x\n", xsum, ntohs(ip->udp_xsum));
Stefan Roese8534bf92005-08-12 20:06:52 +02001473 return;
1474 }
1475 }
1476#endif
1477
wdenk68ceb292004-08-02 21:11:11 +00001478#ifdef CONFIG_NETCONSOLE
1479 nc_input_packet((uchar *)ip +IP_HDR_SIZE,
1480 ntohs(ip->udp_dst),
1481 ntohs(ip->udp_src),
1482 ntohs(ip->udp_len) - 8);
1483#endif
wdenk2d966952002-10-31 22:12:35 +00001484 /*
1485 * IP header OK. Pass the packet to the current handler.
1486 */
1487 (*packetHandler)((uchar *)ip +IP_HDR_SIZE,
1488 ntohs(ip->udp_dst),
1489 ntohs(ip->udp_src),
1490 ntohs(ip->udp_len) - 8);
wdenk2d966952002-10-31 22:12:35 +00001491 break;
1492 }
1493}
1494
1495
1496/**********************************************************************/
1497
1498static int net_check_prereq (proto_t protocol)
1499{
1500 switch (protocol) {
wdenk6e592382004-04-18 17:39:38 +00001501 /* Fall through */
wdenk73a8b272003-06-05 19:27:42 +00001502#if (CONFIG_COMMANDS & CFG_CMD_PING)
1503 case PING:
wdenk6e592382004-04-18 17:39:38 +00001504 if (NetPingIP == 0) {
1505 puts ("*** ERROR: ping address not given\n");
1506 return (1);
1507 }
1508 goto common;
wdenk73a8b272003-06-05 19:27:42 +00001509#endif
wdenkea287de2005-04-01 00:25:43 +00001510#if (CONFIG_COMMANDS & CFG_CMD_SNTP)
1511 case SNTP:
1512 if (NetNtpServerIP == 0) {
1513 puts ("*** ERROR: NTP server address not given\n");
1514 return (1);
1515 }
1516 goto common;
1517#endif
wdenkcbd8a352004-02-24 02:00:03 +00001518#if (CONFIG_COMMANDS & CFG_CMD_NFS)
1519 case NFS:
1520#endif
wdenk68ceb292004-08-02 21:11:11 +00001521 case NETCONS:
wdenk2d966952002-10-31 22:12:35 +00001522 case TFTP:
wdenk6e592382004-04-18 17:39:38 +00001523 if (NetServerIP == 0) {
1524 puts ("*** ERROR: `serverip' not set\n");
1525 return (1);
1526 }
wdenkea287de2005-04-01 00:25:43 +00001527#if (CONFIG_COMMANDS & (CFG_CMD_PING | CFG_CMD_SNTP))
Wolfgang Denkb2f50802005-08-12 23:43:12 +02001528 common:
wdenk73a8b272003-06-05 19:27:42 +00001529#endif
1530
wdenk6e592382004-04-18 17:39:38 +00001531 if (NetOurIP == 0) {
1532 puts ("*** ERROR: `ipaddr' not set\n");
1533 return (1);
1534 }
1535 /* Fall through */
wdenk2d966952002-10-31 22:12:35 +00001536
1537 case DHCP:
1538 case RARP:
1539 case BOOTP:
wdenka3d991b2004-04-15 21:48:45 +00001540 case CDP:
wdenk6e592382004-04-18 17:39:38 +00001541 if (memcmp (NetOurEther, "\0\0\0\0\0\0", 6) == 0) {
wdenk2d966952002-10-31 22:12:35 +00001542#ifdef CONFIG_NET_MULTI
wdenk6e592382004-04-18 17:39:38 +00001543 extern int eth_get_dev_index (void);
1544 int num = eth_get_dev_index ();
wdenk2d966952002-10-31 22:12:35 +00001545
wdenk6e592382004-04-18 17:39:38 +00001546 switch (num) {
1547 case -1:
wdenk2d966952002-10-31 22:12:35 +00001548 puts ("*** ERROR: No ethernet found.\n");
1549 return (1);
wdenk6e592382004-04-18 17:39:38 +00001550 case 0:
wdenk2d966952002-10-31 22:12:35 +00001551 puts ("*** ERROR: `ethaddr' not set\n");
1552 break;
wdenk6e592382004-04-18 17:39:38 +00001553 default:
wdenk8bde7f72003-06-27 21:31:46 +00001554 printf ("*** ERROR: `eth%daddr' not set\n",
wdenk2d966952002-10-31 22:12:35 +00001555 num);
1556 break;
wdenk2d966952002-10-31 22:12:35 +00001557 }
wdenk6e592382004-04-18 17:39:38 +00001558
1559 NetStartAgain ();
1560 return (2);
1561#else
1562 puts ("*** ERROR: `ethaddr' not set\n");
1563 return (1);
1564#endif
1565 }
1566 /* Fall through */
1567 default:
1568 return (0);
wdenk2d966952002-10-31 22:12:35 +00001569 }
wdenk6e592382004-04-18 17:39:38 +00001570 return (0); /* OK */
wdenk2d966952002-10-31 22:12:35 +00001571}
1572/**********************************************************************/
1573
1574int
1575NetCksumOk(uchar * ptr, int len)
1576{
1577 return !((NetCksum(ptr, len) + 1) & 0xfffe);
1578}
1579
1580
1581unsigned
1582NetCksum(uchar * ptr, int len)
1583{
1584 ulong xsum;
Stefan Roese9d2a8732005-08-31 12:55:50 +02001585 ushort *p = (ushort *)ptr;
wdenk2d966952002-10-31 22:12:35 +00001586
1587 xsum = 0;
1588 while (len-- > 0)
Wolfgang Denk7bc5ee02005-08-26 01:36:03 +02001589 xsum += *p++;
wdenk2d966952002-10-31 22:12:35 +00001590 xsum = (xsum & 0xffff) + (xsum >> 16);
1591 xsum = (xsum & 0xffff) + (xsum >> 16);
1592 return (xsum & 0xffff);
1593}
1594
wdenka3d991b2004-04-15 21:48:45 +00001595int
1596NetEthHdrSize(void)
1597{
1598 ushort myvlanid;
wdenk2d966952002-10-31 22:12:35 +00001599
wdenka3d991b2004-04-15 21:48:45 +00001600 myvlanid = ntohs(NetOurVLAN);
1601 if (myvlanid == (ushort)-1)
1602 myvlanid = VLAN_NONE;
1603
1604 return ((myvlanid & VLAN_IDMASK) == VLAN_NONE) ? ETHER_HDR_SIZE : VLAN_ETHER_HDR_SIZE;
1605}
1606
1607int
wdenk2d966952002-10-31 22:12:35 +00001608NetSetEther(volatile uchar * xet, uchar * addr, uint prot)
1609{
1610 Ethernet_t *et = (Ethernet_t *)xet;
wdenka3d991b2004-04-15 21:48:45 +00001611 ushort myvlanid;
1612
1613 myvlanid = ntohs(NetOurVLAN);
1614 if (myvlanid == (ushort)-1)
1615 myvlanid = VLAN_NONE;
wdenk2d966952002-10-31 22:12:35 +00001616
1617 memcpy (et->et_dest, addr, 6);
1618 memcpy (et->et_src, NetOurEther, 6);
wdenka3d991b2004-04-15 21:48:45 +00001619 if ((myvlanid & VLAN_IDMASK) == VLAN_NONE) {
wdenk2d966952002-10-31 22:12:35 +00001620 et->et_protlen = htons(prot);
wdenka3d991b2004-04-15 21:48:45 +00001621 return ETHER_HDR_SIZE;
1622 } else {
1623 VLAN_Ethernet_t *vet = (VLAN_Ethernet_t *)xet;
wdenk2d966952002-10-31 22:12:35 +00001624
wdenka3d991b2004-04-15 21:48:45 +00001625 vet->vet_vlan_type = htons(PROT_VLAN);
1626 vet->vet_tag = htons((0 << 5) | (myvlanid & VLAN_IDMASK));
1627 vet->vet_type = htons(prot);
1628 return VLAN_ETHER_HDR_SIZE;
1629 }
1630}
wdenk2d966952002-10-31 22:12:35 +00001631
1632void
1633NetSetIP(volatile uchar * xip, IPaddr_t dest, int dport, int sport, int len)
1634{
1635 volatile IP_t *ip = (IP_t *)xip;
1636
1637 /*
1638 * If the data is an odd number of bytes, zero the
1639 * byte after the last byte so that the checksum
1640 * will work.
1641 */
1642 if (len & 1)
1643 xip[IP_HDR_SIZE + len] = 0;
1644
1645 /*
1646 * Construct an IP and UDP header.
wdenk6e592382004-04-18 17:39:38 +00001647 * (need to set no fragment bit - XXX)
wdenk2d966952002-10-31 22:12:35 +00001648 */
1649 ip->ip_hl_v = 0x45; /* IP_HDR_SIZE / 4 (not including UDP) */
1650 ip->ip_tos = 0;
1651 ip->ip_len = htons(IP_HDR_SIZE + len);
1652 ip->ip_id = htons(NetIPID++);
1653 ip->ip_off = htons(0x4000); /* No fragmentation */
1654 ip->ip_ttl = 255;
1655 ip->ip_p = 17; /* UDP */
1656 ip->ip_sum = 0;
1657 NetCopyIP((void*)&ip->ip_src, &NetOurIP); /* already in network byte order */
1658 NetCopyIP((void*)&ip->ip_dst, &dest); /* - "" - */
1659 ip->udp_src = htons(sport);
1660 ip->udp_dst = htons(dport);
1661 ip->udp_len = htons(8 + len);
1662 ip->udp_xsum = 0;
1663 ip->ip_sum = ~NetCksum((uchar *)ip, IP_HDR_SIZE_NO_UDP / 2);
1664}
1665
Wolfgang Denk77ddac92005-10-13 16:45:02 +02001666void copy_filename (char *dst, char *src, int size)
wdenk2d966952002-10-31 22:12:35 +00001667{
1668 if (*src && (*src == '"')) {
1669 ++src;
1670 --size;
1671 }
1672
1673 while ((--size > 0) && *src && (*src != '"')) {
1674 *dst++ = *src++;
1675 }
1676 *dst = '\0';
1677}
1678
1679#endif /* CFG_CMD_NET */
1680
1681void ip_to_string (IPaddr_t x, char *s)
1682{
wdenka3d991b2004-04-15 21:48:45 +00001683 x = ntohl (x);
1684 sprintf (s, "%d.%d.%d.%d",
1685 (int) ((x >> 24) & 0xff),
1686 (int) ((x >> 16) & 0xff),
1687 (int) ((x >> 8) & 0xff), (int) ((x >> 0) & 0xff)
wdenk6e592382004-04-18 17:39:38 +00001688 );
wdenk2d966952002-10-31 22:12:35 +00001689}
1690
wdenk73a8b272003-06-05 19:27:42 +00001691IPaddr_t string_to_ip(char *s)
wdenk2d966952002-10-31 22:12:35 +00001692{
1693 IPaddr_t addr;
wdenk73a8b272003-06-05 19:27:42 +00001694 char *e;
wdenk2d966952002-10-31 22:12:35 +00001695 int i;
1696
wdenk73a8b272003-06-05 19:27:42 +00001697 if (s == NULL)
1698 return(0);
wdenk2d966952002-10-31 22:12:35 +00001699
1700 for (addr=0, i=0; i<4; ++i) {
1701 ulong val = s ? simple_strtoul(s, &e, 10) : 0;
1702 addr <<= 8;
1703 addr |= (val & 0xFF);
1704 if (s) {
1705 s = (*e) ? e+1 : e;
1706 }
1707 }
1708
1709 return (htonl(addr));
1710}
wdenk73a8b272003-06-05 19:27:42 +00001711
wdenka3d991b2004-04-15 21:48:45 +00001712void VLAN_to_string(ushort x, char *s)
1713{
1714 x = ntohs(x);
1715
1716 if (x == (ushort)-1)
1717 x = VLAN_NONE;
1718
1719 if (x == VLAN_NONE)
1720 strcpy(s, "none");
1721 else
1722 sprintf(s, "%d", x & VLAN_IDMASK);
1723}
1724
1725ushort string_to_VLAN(char *s)
1726{
1727 ushort id;
1728
1729 if (s == NULL)
wdenkb9711de2004-04-25 13:18:40 +00001730 return htons(VLAN_NONE);
wdenka3d991b2004-04-15 21:48:45 +00001731
1732 if (*s < '0' || *s > '9')
1733 id = VLAN_NONE;
1734 else
1735 id = (ushort)simple_strtoul(s, NULL, 10);
1736
wdenkb9711de2004-04-25 13:18:40 +00001737 return htons(id);
wdenka3d991b2004-04-15 21:48:45 +00001738}
1739
wdenk73a8b272003-06-05 19:27:42 +00001740void print_IPaddr (IPaddr_t x)
1741{
wdenka3d991b2004-04-15 21:48:45 +00001742 char tmp[16];
wdenk73a8b272003-06-05 19:27:42 +00001743
wdenka3d991b2004-04-15 21:48:45 +00001744 ip_to_string (x, tmp);
wdenk73a8b272003-06-05 19:27:42 +00001745
wdenka3d991b2004-04-15 21:48:45 +00001746 puts (tmp);
wdenk73a8b272003-06-05 19:27:42 +00001747}
1748
1749IPaddr_t getenv_IPaddr (char *var)
1750{
1751 return (string_to_ip(getenv(var)));
1752}
wdenka3d991b2004-04-15 21:48:45 +00001753
1754ushort getenv_VLAN(char *var)
1755{
1756 return (string_to_VLAN(getenv(var)));
1757}