blob: 4bbe5313c3e2124938649919636b0025bf98b01c [file] [log] [blame]
wdenk2d966952002-10-31 22:12:35 +00001/*
2 * Copied from Linux Monitor (LiMon) - Networking.
3 *
4 * Copyright 1994 - 2000 Neil Russell.
5 * (See License)
6 * Copyright 2000 Roland Borde
7 * Copyright 2000 Paolo Scaffardi
8 * Copyright 2000-2002 Wolfgang Denk, wd@denx.de
9 */
10
11/*
12 * General Desription:
13 *
14 * The user interface supports commands for BOOTP, RARP, and TFTP.
15 * Also, we support ARP internally. Depending on available data,
16 * these interact as follows:
17 *
18 * BOOTP:
19 *
20 * Prerequisites: - own ethernet address
21 * We want: - own IP address
22 * - TFTP server IP address
23 * - name of bootfile
24 * Next step: ARP
25 *
26 * RARP:
27 *
28 * Prerequisites: - own ethernet address
29 * We want: - own IP address
30 * - TFTP server IP address
31 * Next step: ARP
32 *
33 * ARP:
34 *
35 * Prerequisites: - own ethernet address
36 * - own IP address
37 * - TFTP server IP address
38 * We want: - TFTP server ethernet address
39 * Next step: TFTP
40 *
41 * DHCP:
42 *
Wolfgang Denk30b87322005-08-12 23:43:12 +020043 * Prerequisites: - own ethernet address
44 * We want: - IP, Netmask, ServerIP, Gateway IP
45 * - bootfilename, lease time
46 * Next step: - TFTP
wdenk2d966952002-10-31 22:12:35 +000047 *
48 * TFTP:
49 *
50 * Prerequisites: - own ethernet address
51 * - own IP address
52 * - TFTP server IP address
53 * - TFTP server ethernet address
54 * - name of bootfile (if unknown, we use a default name
55 * derived from our own IP address)
56 * We want: - load the boot file
57 * Next step: none
wdenkbe9c1cb2004-02-24 02:00:03 +000058 *
59 * NFS:
60 *
61 * Prerequisites: - own ethernet address
62 * - own IP address
63 * - name of bootfile (if unknown, we use a default name
64 * derived from our own IP address)
65 * We want: - load the boot file
66 * Next step: none
wdenkb4ad9622005-04-01 00:25:43 +000067 *
68 * SNTP:
69 *
Wolfgang Denk30b87322005-08-12 23:43:12 +020070 * Prerequisites: - own ethernet address
wdenkb4ad9622005-04-01 00:25:43 +000071 * - own IP address
72 * We want: - network time
73 * Next step: none
wdenk2d966952002-10-31 22:12:35 +000074 */
75
76
77#include <common.h>
78#include <watchdog.h>
79#include <command.h>
80#include <net.h>
81#include "bootp.h"
82#include "tftp.h"
83#include "rarp.h"
wdenkbe9c1cb2004-02-24 02:00:03 +000084#include "nfs.h"
wdenk49c3f672003-10-08 22:33:00 +000085#ifdef CONFIG_STATUS_LED
86#include <status_led.h>
87#include <miiphy.h>
88#endif
Jon Loeliger54f35c22007-07-09 17:45:14 -050089#if defined(CONFIG_CMD_SNTP)
wdenkb4ad9622005-04-01 00:25:43 +000090#include "sntp.h"
91#endif
Peter Tyser62948502008-11-03 09:30:59 -060092#if defined(CONFIG_CDP_VERSION)
93#include <timestamp.h>
94#endif
Robin Getz82f0d232009-07-20 14:53:54 -040095#if defined(CONFIG_CMD_DNS)
96#include "dns.h"
97#endif
wdenk2d966952002-10-31 22:12:35 +000098
Jon Loeliger54f35c22007-07-09 17:45:14 -050099#if defined(CONFIG_CMD_NET)
wdenk2d966952002-10-31 22:12:35 +0000100
Wolfgang Denk6405a152006-03-31 18:32:53 +0200101DECLARE_GLOBAL_DATA_PTR;
102
Guennadi Liakhovetskib38c2b32008-04-03 17:04:19 +0200103#ifndef CONFIG_ARP_TIMEOUT
Bartlomiej Sieka56668462008-10-01 15:26:28 +0200104# define ARP_TIMEOUT 5000UL /* Milliseconds before trying ARP again */
Guennadi Liakhovetskib38c2b32008-04-03 17:04:19 +0200105#else
Bartlomiej Sieka56668462008-10-01 15:26:28 +0200106# define ARP_TIMEOUT CONFIG_ARP_TIMEOUT
Guennadi Liakhovetskib38c2b32008-04-03 17:04:19 +0200107#endif
108
109
wdenke6466f62003-06-05 19:27:42 +0000110#ifndef CONFIG_NET_RETRY_COUNT
Guennadi Liakhovetskib38c2b32008-04-03 17:04:19 +0200111# define ARP_TIMEOUT_COUNT 5 /* # of timeouts before giving up */
wdenke6466f62003-06-05 19:27:42 +0000112#else
Guennadi Liakhovetskib38c2b32008-04-03 17:04:19 +0200113# define ARP_TIMEOUT_COUNT CONFIG_NET_RETRY_COUNT
wdenke6466f62003-06-05 19:27:42 +0000114#endif
115
wdenk2d966952002-10-31 22:12:35 +0000116#if 0
117#define ET_DEBUG
118#endif
119
120/** BOOTP EXTENTIONS **/
121
122IPaddr_t NetOurSubnetMask=0; /* Our subnet mask (0=unknown) */
123IPaddr_t NetOurGatewayIP=0; /* Our gateways IP address */
124IPaddr_t NetOurDNSIP=0; /* Our DNS IP address */
Jon Loeliger5336a762007-07-09 22:08:34 -0500125#if defined(CONFIG_BOOTP_DNS2)
stroesee0aadfb2003-08-28 14:17:32 +0000126IPaddr_t NetOurDNS2IP=0; /* Our 2nd DNS IP address */
127#endif
wdenk2d966952002-10-31 22:12:35 +0000128char NetOurNISDomain[32]={0,}; /* Our NIS domain */
129char NetOurHostName[32]={0,}; /* Our hostname */
130char NetOurRootPath[64]={0,}; /* Our bootpath */
131ushort NetBootFileSize=0; /* Our bootfile size in blocks */
132
David Updegraff7280da72007-06-11 10:41:07 -0500133#ifdef CONFIG_MCAST_TFTP /* Multicast TFTP */
134IPaddr_t Mcast_addr;
135#endif
136
wdenk2d966952002-10-31 22:12:35 +0000137/** END OF BOOTP EXTENTIONS **/
138
139ulong NetBootFileXferSize; /* The actual transferred size of the bootfile (in bytes) */
140uchar NetOurEther[6]; /* Our ethernet address */
141uchar NetServerEther[6] = /* Boot server enet address */
wdenke6466f62003-06-05 19:27:42 +0000142 { 0, 0, 0, 0, 0, 0 };
wdenk2d966952002-10-31 22:12:35 +0000143IPaddr_t NetOurIP; /* Our IP addr (0 = unknown) */
Guennadi Liakhovetskib38c2b32008-04-03 17:04:19 +0200144IPaddr_t NetServerIP; /* Server IP addr (0 = unknown) */
Mike Frysingerdc166032009-07-18 21:04:08 -0400145volatile uchar *NetRxPacket; /* Current receive packet */
146int NetRxPacketLen; /* Current rx packet length */
wdenk2d966952002-10-31 22:12:35 +0000147unsigned NetIPID; /* IP packet ID */
148uchar NetBcastAddr[6] = /* Ethernet bcast address */
149 { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
wdenke6466f62003-06-05 19:27:42 +0000150uchar NetEtherNullAddr[6] =
151 { 0, 0, 0, 0, 0, 0 };
Rafal Jaworowski1f2c9a42007-12-27 18:19:02 +0100152#ifdef CONFIG_API
153void (*push_packet)(volatile void *, int len) = 0;
154#endif
Jon Loeliger54f35c22007-07-09 17:45:14 -0500155#if defined(CONFIG_CMD_CDP)
wdenk05939202004-04-18 17:39:38 +0000156uchar NetCDPAddr[6] = /* Ethernet bcast address */
wdenk145d2c12004-04-15 21:48:45 +0000157 { 0x01, 0x00, 0x0c, 0xcc, 0xcc, 0xcc };
158#endif
wdenk2d966952002-10-31 22:12:35 +0000159int NetState; /* Network loop state */
160#ifdef CONFIG_NET_MULTI
161int NetRestartWrap = 0; /* Tried all network devices */
162static int NetRestarted = 0; /* Network loop restarted */
163static int NetDevExists = 0; /* At least one device configured */
164#endif
165
wdenk05939202004-04-18 17:39:38 +0000166/* XXX in both little & big endian machines 0xFFFF == ntohs(-1) */
167ushort NetOurVLAN = 0xFFFF; /* default is without VLAN */
168ushort NetOurNativeVLAN = 0xFFFF; /* ditto */
wdenk145d2c12004-04-15 21:48:45 +0000169
wdenk2d966952002-10-31 22:12:35 +0000170char BootFile[128]; /* Boot File name */
171
Jon Loeliger54f35c22007-07-09 17:45:14 -0500172#if defined(CONFIG_CMD_PING)
Wolfgang Denka1be4762008-05-20 16:00:29 +0200173IPaddr_t NetPingIP; /* the ip address to ping */
wdenke6466f62003-06-05 19:27:42 +0000174
175static void PingStart(void);
176#endif
177
Jon Loeliger54f35c22007-07-09 17:45:14 -0500178#if defined(CONFIG_CMD_CDP)
wdenk145d2c12004-04-15 21:48:45 +0000179static void CDPStart(void);
180#endif
181
Jon Loeliger54f35c22007-07-09 17:45:14 -0500182#if defined(CONFIG_CMD_SNTP)
wdenkb4ad9622005-04-01 00:25:43 +0000183IPaddr_t NetNtpServerIP; /* NTP server IP address */
184int NetTimeOffset=0; /* offset time from UTC */
185#endif
186
wdenkb8fb6192004-08-02 21:11:11 +0000187#ifdef CONFIG_NETCONSOLE
188void NcStart(void);
189int nc_input_packet(uchar *pkt, unsigned dest, unsigned src, unsigned len);
190#endif
191
wdenk2d966952002-10-31 22:12:35 +0000192volatile uchar PktBuf[(PKTBUFSRX+1) * PKTSIZE_ALIGN + PKTALIGN];
193
194volatile uchar *NetRxPackets[PKTBUFSRX]; /* Receive packets */
195
196static rxhand_f *packetHandler; /* Current RX packet handler */
197static thand_f *timeHandler; /* Current timeout handler */
wdenk5d841732003-08-17 18:55:18 +0000198static ulong timeStart; /* Time base value */
199static ulong timeDelta; /* Current timeout value */
wdenk2d966952002-10-31 22:12:35 +0000200volatile uchar *NetTxPacket = 0; /* THE transmit packet */
201
202static int net_check_prereq (proto_t protocol);
203
204/**********************************************************************/
wdenke6466f62003-06-05 19:27:42 +0000205
206IPaddr_t NetArpWaitPacketIP;
207IPaddr_t NetArpWaitReplyIP;
208uchar *NetArpWaitPacketMAC; /* MAC address of waiting packet's destination */
Wolfgang Denk30b87322005-08-12 23:43:12 +0200209uchar *NetArpWaitTxPacket; /* THE transmit packet */
wdenke6466f62003-06-05 19:27:42 +0000210int NetArpWaitTxPacketSize;
Wolfgang Denka1be4762008-05-20 16:00:29 +0200211uchar NetArpWaitPacketBuf[PKTSIZE_ALIGN + PKTALIGN];
wdenke6466f62003-06-05 19:27:42 +0000212ulong NetArpWaitTimerStart;
213int NetArpWaitTry;
214
wdenk05939202004-04-18 17:39:38 +0000215void ArpRequest (void)
wdenke6466f62003-06-05 19:27:42 +0000216{
217 int i;
218 volatile uchar *pkt;
wdenk05939202004-04-18 17:39:38 +0000219 ARP_t *arp;
wdenke6466f62003-06-05 19:27:42 +0000220
221#ifdef ET_DEBUG
wdenk05939202004-04-18 17:39:38 +0000222 printf ("ARP broadcast %d\n", NetArpWaitTry);
wdenke6466f62003-06-05 19:27:42 +0000223#endif
224 pkt = NetTxPacket;
225
wdenk05939202004-04-18 17:39:38 +0000226 pkt += NetSetEther (pkt, NetBcastAddr, PROT_ARP);
wdenke6466f62003-06-05 19:27:42 +0000227
wdenk05939202004-04-18 17:39:38 +0000228 arp = (ARP_t *) pkt;
wdenke6466f62003-06-05 19:27:42 +0000229
wdenk05939202004-04-18 17:39:38 +0000230 arp->ar_hrd = htons (ARP_ETHER);
231 arp->ar_pro = htons (PROT_IP);
wdenke6466f62003-06-05 19:27:42 +0000232 arp->ar_hln = 6;
233 arp->ar_pln = 4;
wdenk05939202004-04-18 17:39:38 +0000234 arp->ar_op = htons (ARPOP_REQUEST);
wdenke6466f62003-06-05 19:27:42 +0000235
Wolfgang Denk30b87322005-08-12 23:43:12 +0200236 memcpy (&arp->ar_data[0], NetOurEther, 6); /* source ET addr */
237 NetWriteIP ((uchar *) & arp->ar_data[6], NetOurIP); /* source IP addr */
wdenk05939202004-04-18 17:39:38 +0000238 for (i = 10; i < 16; ++i) {
239 arp->ar_data[i] = 0; /* dest ET addr = 0 */
wdenke6466f62003-06-05 19:27:42 +0000240 }
241
wdenk05939202004-04-18 17:39:38 +0000242 if ((NetArpWaitPacketIP & NetOurSubnetMask) !=
243 (NetOurIP & NetOurSubnetMask)) {
244 if (NetOurGatewayIP == 0) {
245 puts ("## Warning: gatewayip needed but not set\n");
Wolfgang Denk98ceffb2006-03-12 01:13:30 +0100246 NetArpWaitReplyIP = NetArpWaitPacketIP;
247 } else {
248 NetArpWaitReplyIP = NetOurGatewayIP;
wdenk05939202004-04-18 17:39:38 +0000249 }
wdenk05939202004-04-18 17:39:38 +0000250 } else {
251 NetArpWaitReplyIP = NetArpWaitPacketIP;
252 }
wdenke6466f62003-06-05 19:27:42 +0000253
wdenk05939202004-04-18 17:39:38 +0000254 NetWriteIP ((uchar *) & arp->ar_data[16], NetArpWaitReplyIP);
255 (void) eth_send (NetTxPacket, (pkt - NetTxPacket) + ARP_HDR_SIZE);
wdenke6466f62003-06-05 19:27:42 +0000256}
257
258void ArpTimeoutCheck(void)
259{
260 ulong t;
261
262 if (!NetArpWaitPacketIP)
263 return;
264
265 t = get_timer(0);
266
267 /* check for arp timeout */
Bartlomiej Sieka56668462008-10-01 15:26:28 +0200268 if ((t - NetArpWaitTimerStart) > ARP_TIMEOUT) {
wdenke6466f62003-06-05 19:27:42 +0000269 NetArpWaitTry++;
270
271 if (NetArpWaitTry >= ARP_TIMEOUT_COUNT) {
272 puts ("\nARP Retry count exceeded; starting again\n");
273 NetArpWaitTry = 0;
274 NetStartAgain();
275 } else {
276 NetArpWaitTimerStart = t;
277 ArpRequest();
278 }
279 }
280}
281
Heiko Schocher3b195ff2009-04-28 08:36:11 +0200282static void
Heiko Schocher0c303fa2009-02-10 09:38:52 +0100283NetInitLoop(proto_t protocol)
284{
Heiko Schocher3b195ff2009-04-28 08:36:11 +0200285 static int env_changed_id = 0;
Heiko Schocher0c303fa2009-02-10 09:38:52 +0100286 bd_t *bd = gd->bd;
287 int env_id = get_env_id ();
288
289 /* update only when the environment has changed */
Michael Zaidmanb97bfe42009-04-04 01:43:00 +0300290 if (env_changed_id != env_id) {
Heiko Schocher0c303fa2009-02-10 09:38:52 +0100291 NetCopyIP(&NetOurIP, &bd->bi_ip_addr);
292 NetOurGatewayIP = getenv_IPaddr ("gatewayip");
293 NetOurSubnetMask= getenv_IPaddr ("netmask");
Heiko Schocher0c303fa2009-02-10 09:38:52 +0100294 NetServerIP = getenv_IPaddr ("serverip");
Heiko Schocher0c303fa2009-02-10 09:38:52 +0100295 NetOurNativeVLAN = getenv_VLAN("nvlan");
Michael Zaidmanb97bfe42009-04-04 01:43:00 +0300296 NetOurVLAN = getenv_VLAN("vlan");
Robin Getz82f0d232009-07-20 14:53:54 -0400297#if defined(CONFIG_CMD_DNS)
298 NetOurDNSIP = getenv_IPaddr("dnsip");
299#endif
Michael Zaidmanb97bfe42009-04-04 01:43:00 +0300300 env_changed_id = env_id;
Heiko Schocher0c303fa2009-02-10 09:38:52 +0100301 }
Michael Zaidmanb97bfe42009-04-04 01:43:00 +0300302
Heiko Schocher3b195ff2009-04-28 08:36:11 +0200303 return;
Heiko Schocher0c303fa2009-02-10 09:38:52 +0100304}
305
wdenke6466f62003-06-05 19:27:42 +0000306/**********************************************************************/
wdenk2d966952002-10-31 22:12:35 +0000307/*
308 * Main network processing loop.
309 */
310
311int
312NetLoop(proto_t protocol)
313{
wdenk2d966952002-10-31 22:12:35 +0000314 bd_t *bd = gd->bd;
315
316#ifdef CONFIG_NET_MULTI
317 NetRestarted = 0;
318 NetDevExists = 0;
319#endif
320
wdenke6466f62003-06-05 19:27:42 +0000321 /* XXX problem with bss workaround */
322 NetArpWaitPacketMAC = NULL;
323 NetArpWaitTxPacket = NULL;
324 NetArpWaitPacketIP = 0;
325 NetArpWaitReplyIP = 0;
326 NetArpWaitTxPacket = NULL;
327 NetTxPacket = NULL;
328
wdenk2d966952002-10-31 22:12:35 +0000329 if (!NetTxPacket) {
330 int i;
wdenk2d966952002-10-31 22:12:35 +0000331 /*
332 * Setup packet buffers, aligned correctly.
333 */
334 NetTxPacket = &PktBuf[0] + (PKTALIGN - 1);
335 NetTxPacket -= (ulong)NetTxPacket % PKTALIGN;
336 for (i = 0; i < PKTBUFSRX; i++) {
337 NetRxPackets[i] = NetTxPacket + (i+1)*PKTSIZE_ALIGN;
338 }
wdenke6466f62003-06-05 19:27:42 +0000339 }
340
341 if (!NetArpWaitTxPacket) {
342 NetArpWaitTxPacket = &NetArpWaitPacketBuf[0] + (PKTALIGN - 1);
343 NetArpWaitTxPacket -= (ulong)NetArpWaitTxPacket % PKTALIGN;
344 NetArpWaitTxPacketSize = 0;
wdenk2d966952002-10-31 22:12:35 +0000345 }
346
347 eth_halt();
wdenk05939202004-04-18 17:39:38 +0000348#ifdef CONFIG_NET_MULTI
wdenk145d2c12004-04-15 21:48:45 +0000349 eth_set_current();
wdenk05939202004-04-18 17:39:38 +0000350#endif
wdenkfa66e932005-04-03 14:52:59 +0000351 if (eth_init(bd) < 0) {
352 eth_halt();
wdenk05939202004-04-18 17:39:38 +0000353 return(-1);
wdenkfa66e932005-04-03 14:52:59 +0000354 }
wdenk2d966952002-10-31 22:12:35 +0000355
356restart:
357#ifdef CONFIG_NET_MULTI
358 memcpy (NetOurEther, eth_get_dev()->enetaddr, 6);
359#else
Mike Frysinger79ca1b92009-02-11 18:23:48 -0500360 eth_getenv_enetaddr("ethaddr", NetOurEther);
wdenk2d966952002-10-31 22:12:35 +0000361#endif
362
363 NetState = NETLOOP_CONTINUE;
364
365 /*
366 * Start the ball rolling with the given start function. From
367 * here on, this code is a state machine driven by received
368 * packets and timer events.
369 */
Heiko Schocher0c303fa2009-02-10 09:38:52 +0100370 NetInitLoop(protocol);
wdenk2d966952002-10-31 22:12:35 +0000371
372 switch (net_check_prereq (protocol)) {
373 case 1:
374 /* network not configured */
wdenkfa66e932005-04-03 14:52:59 +0000375 eth_halt();
wdenk2582f6b2002-11-11 21:14:20 +0000376 return (-1);
wdenk2d966952002-10-31 22:12:35 +0000377
378#ifdef CONFIG_NET_MULTI
379 case 2:
380 /* network device not configured */
381 break;
382#endif /* CONFIG_NET_MULTI */
383
384 case 0:
385#ifdef CONFIG_NET_MULTI
386 NetDevExists = 1;
387#endif
388 switch (protocol) {
389 case TFTP:
390 /* always use ARP to get server ethernet address */
wdenke6466f62003-06-05 19:27:42 +0000391 TftpStart();
wdenk2d966952002-10-31 22:12:35 +0000392 break;
393
Jon Loeliger54f35c22007-07-09 17:45:14 -0500394#if defined(CONFIG_CMD_DHCP)
wdenk2d966952002-10-31 22:12:35 +0000395 case DHCP:
wdenkc3919532004-10-11 22:51:13 +0000396 BootpTry = 0;
wdenk2d966952002-10-31 22:12:35 +0000397 DhcpRequest(); /* Basically same as BOOTP */
398 break;
Jon Loeligera9807e52007-07-10 11:05:02 -0500399#endif
wdenk2d966952002-10-31 22:12:35 +0000400
401 case BOOTP:
402 BootpTry = 0;
403 BootpRequest ();
404 break;
405
406 case RARP:
407 RarpTry = 0;
408 RarpRequest ();
409 break;
Jon Loeliger54f35c22007-07-09 17:45:14 -0500410#if defined(CONFIG_CMD_PING)
wdenke6466f62003-06-05 19:27:42 +0000411 case PING:
412 PingStart();
413 break;
414#endif
Jon Loeliger54f35c22007-07-09 17:45:14 -0500415#if defined(CONFIG_CMD_NFS)
wdenkbe9c1cb2004-02-24 02:00:03 +0000416 case NFS:
417 NfsStart();
418 break;
419#endif
Jon Loeliger54f35c22007-07-09 17:45:14 -0500420#if defined(CONFIG_CMD_CDP)
wdenk145d2c12004-04-15 21:48:45 +0000421 case CDP:
422 CDPStart();
423 break;
424#endif
wdenkb8fb6192004-08-02 21:11:11 +0000425#ifdef CONFIG_NETCONSOLE
426 case NETCONS:
427 NcStart();
428 break;
429#endif
Jon Loeliger54f35c22007-07-09 17:45:14 -0500430#if defined(CONFIG_CMD_SNTP)
wdenkb4ad9622005-04-01 00:25:43 +0000431 case SNTP:
432 SntpStart();
433 break;
434#endif
Robin Getz82f0d232009-07-20 14:53:54 -0400435#if defined(CONFIG_CMD_DNS)
436 case DNS:
437 DnsStart();
438 break;
439#endif
wdenk2d966952002-10-31 22:12:35 +0000440 default:
441 break;
442 }
443
444 NetBootFileXferSize = 0;
445 break;
446 }
447
Jon Loeliger54f35c22007-07-09 17:45:14 -0500448#if defined(CONFIG_MII) || defined(CONFIG_CMD_MII)
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200449#if defined(CONFIG_SYS_FAULT_ECHO_LINK_DOWN) && defined(CONFIG_STATUS_LED) && defined(STATUS_LED_RED)
wdenk49c3f672003-10-08 22:33:00 +0000450 /*
wdenk9c53f402003-10-15 23:53:47 +0000451 * Echo the inverted link state to the fault LED.
wdenk49c3f672003-10-08 22:33:00 +0000452 */
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200453 if(miiphy_link(eth_get_dev()->name, CONFIG_SYS_FAULT_MII_ADDR)) {
wdenk49c3f672003-10-08 22:33:00 +0000454 status_led_set (STATUS_LED_RED, STATUS_LED_OFF);
455 } else {
456 status_led_set (STATUS_LED_RED, STATUS_LED_ON);
457 }
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200458#endif /* CONFIG_SYS_FAULT_ECHO_LINK_DOWN, ... */
wdenk49c3f672003-10-08 22:33:00 +0000459#endif /* CONFIG_MII, ... */
wdenk2d966952002-10-31 22:12:35 +0000460
461 /*
462 * Main packet reception loop. Loop receiving packets until
wdenk27fa5852005-04-03 14:18:51 +0000463 * someone sets `NetState' to a state that terminates.
wdenk2d966952002-10-31 22:12:35 +0000464 */
465 for (;;) {
466 WATCHDOG_RESET();
467#ifdef CONFIG_SHOW_ACTIVITY
468 {
469 extern void show_activity(int arg);
470 show_activity(1);
471 }
472#endif
473 /*
474 * Check the ethernet for a new packet. The ethernet
475 * receive routine will process it.
476 */
Guennadi Liakhovetskib38c2b32008-04-03 17:04:19 +0200477 eth_rx();
wdenk2d966952002-10-31 22:12:35 +0000478
479 /*
480 * Abort if ctrl-c was pressed.
481 */
482 if (ctrlc()) {
wdenk57b2d802003-06-27 21:31:46 +0000483 eth_halt();
wdenk42c05472004-03-23 22:14:11 +0000484 puts ("\nAbort\n");
wdenk2582f6b2002-11-11 21:14:20 +0000485 return (-1);
wdenk2d966952002-10-31 22:12:35 +0000486 }
487
wdenke6466f62003-06-05 19:27:42 +0000488 ArpTimeoutCheck();
wdenk2d966952002-10-31 22:12:35 +0000489
490 /*
491 * Check for a timeout, and run the timeout handler
492 * if we have one.
493 */
wdenk5d841732003-08-17 18:55:18 +0000494 if (timeHandler && ((get_timer(0) - timeStart) > timeDelta)) {
wdenk2d966952002-10-31 22:12:35 +0000495 thand_f *x;
496
Jon Loeliger54f35c22007-07-09 17:45:14 -0500497#if defined(CONFIG_MII) || defined(CONFIG_CMD_MII)
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200498# if defined(CONFIG_SYS_FAULT_ECHO_LINK_DOWN) && \
wdenk6148e742005-04-03 20:55:38 +0000499 defined(CONFIG_STATUS_LED) && \
wdenk27fa5852005-04-03 14:18:51 +0000500 defined(STATUS_LED_RED)
wdenk49c3f672003-10-08 22:33:00 +0000501 /*
wdenk9c53f402003-10-15 23:53:47 +0000502 * Echo the inverted link state to the fault LED.
wdenk49c3f672003-10-08 22:33:00 +0000503 */
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200504 if(miiphy_link(eth_get_dev()->name, CONFIG_SYS_FAULT_MII_ADDR)) {
wdenk49c3f672003-10-08 22:33:00 +0000505 status_led_set (STATUS_LED_RED, STATUS_LED_OFF);
506 } else {
507 status_led_set (STATUS_LED_RED, STATUS_LED_ON);
508 }
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200509# endif /* CONFIG_SYS_FAULT_ECHO_LINK_DOWN, ... */
wdenk49c3f672003-10-08 22:33:00 +0000510#endif /* CONFIG_MII, ... */
wdenk2d966952002-10-31 22:12:35 +0000511 x = timeHandler;
512 timeHandler = (thand_f *)0;
513 (*x)();
514 }
515
516
517 switch (NetState) {
518
519 case NETLOOP_RESTART:
520#ifdef CONFIG_NET_MULTI
521 NetRestarted = 1;
522#endif
523 goto restart;
524
525 case NETLOOP_SUCCESS:
526 if (NetBootFileXferSize > 0) {
Wolfgang Denk084d0ce2007-09-12 00:48:57 +0200527 char buf[20];
wdenk2d966952002-10-31 22:12:35 +0000528 printf("Bytes transferred = %ld (%lx hex)\n",
529 NetBootFileXferSize,
530 NetBootFileXferSize);
Wolfgang Denk084d0ce2007-09-12 00:48:57 +0200531 sprintf(buf, "%lX", NetBootFileXferSize);
wdenk2d966952002-10-31 22:12:35 +0000532 setenv("filesize", buf);
wdenk145d2c12004-04-15 21:48:45 +0000533
534 sprintf(buf, "%lX", (unsigned long)load_addr);
535 setenv("fileaddr", buf);
wdenk2d966952002-10-31 22:12:35 +0000536 }
537 eth_halt();
538 return NetBootFileXferSize;
539
540 case NETLOOP_FAIL:
wdenk2582f6b2002-11-11 21:14:20 +0000541 return (-1);
wdenk2d966952002-10-31 22:12:35 +0000542 }
543 }
544}
545
546/**********************************************************************/
547
548static void
549startAgainTimeout(void)
550{
551 NetState = NETLOOP_RESTART;
552}
553
554static void
555startAgainHandler(uchar * pkt, unsigned dest, unsigned src, unsigned len)
556{
557 /* Totally ignore the packet */
558}
559
wdenk05939202004-04-18 17:39:38 +0000560void NetStartAgain (void)
wdenk2d966952002-10-31 22:12:35 +0000561{
wdenk05939202004-04-18 17:39:38 +0000562 char *nretry;
563 int noretry = 0, once = 0;
wdenk145d2c12004-04-15 21:48:45 +0000564
wdenk05939202004-04-18 17:39:38 +0000565 if ((nretry = getenv ("netretry")) != NULL) {
566 noretry = (strcmp (nretry, "no") == 0);
567 once = (strcmp (nretry, "once") == 0);
568 }
569 if (noretry) {
570 eth_halt ();
wdenk145d2c12004-04-15 21:48:45 +0000571 NetState = NETLOOP_FAIL;
572 return;
573 }
wdenk2d966952002-10-31 22:12:35 +0000574#ifndef CONFIG_NET_MULTI
Bartlomiej Sieka56668462008-10-01 15:26:28 +0200575 NetSetTimeout (10000UL, startAgainTimeout);
wdenk05939202004-04-18 17:39:38 +0000576 NetSetHandler (startAgainHandler);
577#else /* !CONFIG_NET_MULTI*/
578 eth_halt ();
Matthias Fuchsa02d62b2007-12-27 16:58:41 +0100579#if !defined(CONFIG_NET_DO_NOT_TRY_ANOTHER)
wdenk05939202004-04-18 17:39:38 +0000580 eth_try_another (!NetRestarted);
Matthias Fuchsa02d62b2007-12-27 16:58:41 +0100581#endif
wdenk05939202004-04-18 17:39:38 +0000582 eth_init (gd->bd);
583 if (NetRestartWrap) {
wdenk2d966952002-10-31 22:12:35 +0000584 NetRestartWrap = 0;
wdenk05939202004-04-18 17:39:38 +0000585 if (NetDevExists && !once) {
Bartlomiej Sieka56668462008-10-01 15:26:28 +0200586 NetSetTimeout (10000UL, startAgainTimeout);
wdenk05939202004-04-18 17:39:38 +0000587 NetSetHandler (startAgainHandler);
588 } else {
wdenk2d966952002-10-31 22:12:35 +0000589 NetState = NETLOOP_FAIL;
590 }
wdenk05939202004-04-18 17:39:38 +0000591 } else {
wdenk2d966952002-10-31 22:12:35 +0000592 NetState = NETLOOP_RESTART;
593 }
wdenk05939202004-04-18 17:39:38 +0000594#endif /* CONFIG_NET_MULTI */
wdenk2d966952002-10-31 22:12:35 +0000595}
596
597/**********************************************************************/
598/*
599 * Miscelaneous bits.
600 */
601
602void
603NetSetHandler(rxhand_f * f)
604{
605 packetHandler = f;
606}
607
608
609void
wdenkd6a3dd42004-10-09 21:56:21 +0000610NetSetTimeout(ulong iv, thand_f * f)
wdenk2d966952002-10-31 22:12:35 +0000611{
612 if (iv == 0) {
613 timeHandler = (thand_f *)0;
614 } else {
615 timeHandler = f;
wdenk5d841732003-08-17 18:55:18 +0000616 timeStart = get_timer(0);
617 timeDelta = iv;
wdenk2d966952002-10-31 22:12:35 +0000618 }
619}
620
621
622void
623NetSendPacket(volatile uchar * pkt, int len)
624{
625 (void) eth_send(pkt, len);
626}
627
wdenke6466f62003-06-05 19:27:42 +0000628int
629NetSendUDPPacket(uchar *ether, IPaddr_t dest, int dport, int sport, int len)
630{
wdenk145d2c12004-04-15 21:48:45 +0000631 uchar *pkt;
632
wdenke6466f62003-06-05 19:27:42 +0000633 /* convert to new style broadcast */
634 if (dest == 0)
635 dest = 0xFFFFFFFF;
636
637 /* if broadcast, make the ether address a broadcast and don't do ARP */
638 if (dest == 0xFFFFFFFF)
639 ether = NetBcastAddr;
640
641 /* if MAC address was not discovered yet, save the packet and do an ARP request */
642 if (memcmp(ether, NetEtherNullAddr, 6) == 0) {
643
644#ifdef ET_DEBUG
645 printf("sending ARP for %08lx\n", dest);
646#endif
wdenke6466f62003-06-05 19:27:42 +0000647 NetArpWaitPacketIP = dest;
648 NetArpWaitPacketMAC = ether;
wdenk145d2c12004-04-15 21:48:45 +0000649
650 pkt = NetArpWaitTxPacket;
651 pkt += NetSetEther (pkt, NetArpWaitPacketMAC, PROT_IP);
652
653 NetSetIP (pkt, dest, dport, sport, len);
654 memcpy(pkt + IP_HDR_SIZE, (uchar *)NetTxPacket + (pkt - (uchar *)NetArpWaitTxPacket) + IP_HDR_SIZE, len);
wdenke6466f62003-06-05 19:27:42 +0000655
656 /* size of the waiting packet */
wdenk145d2c12004-04-15 21:48:45 +0000657 NetArpWaitTxPacketSize = (pkt - NetArpWaitTxPacket) + IP_HDR_SIZE + len;
wdenke6466f62003-06-05 19:27:42 +0000658
659 /* and do the ARP request */
660 NetArpWaitTry = 1;
661 NetArpWaitTimerStart = get_timer(0);
662 ArpRequest();
663 return 1; /* waiting */
664 }
665
666#ifdef ET_DEBUG
Mike Frysinger79ca1b92009-02-11 18:23:48 -0500667 printf("sending UDP to %08lx/%pM\n", dest, ether);
wdenke6466f62003-06-05 19:27:42 +0000668#endif
669
wdenk145d2c12004-04-15 21:48:45 +0000670 pkt = (uchar *)NetTxPacket;
671 pkt += NetSetEther (pkt, ether, PROT_IP);
672 NetSetIP (pkt, dest, dport, sport, len);
673 (void) eth_send(NetTxPacket, (pkt - NetTxPacket) + IP_HDR_SIZE + len);
wdenke6466f62003-06-05 19:27:42 +0000674
wdenk05939202004-04-18 17:39:38 +0000675 return 0; /* transmitted */
wdenke6466f62003-06-05 19:27:42 +0000676}
677
Jon Loeliger54f35c22007-07-09 17:45:14 -0500678#if defined(CONFIG_CMD_PING)
wdenke6466f62003-06-05 19:27:42 +0000679static ushort PingSeqNo;
680
681int PingSend(void)
682{
683 static uchar mac[6];
684 volatile IP_t *ip;
685 volatile ushort *s;
wdenk145d2c12004-04-15 21:48:45 +0000686 uchar *pkt;
wdenke6466f62003-06-05 19:27:42 +0000687
688 /* XXX always send arp request */
689
690 memcpy(mac, NetEtherNullAddr, 6);
691
692#ifdef ET_DEBUG
693 printf("sending ARP for %08lx\n", NetPingIP);
694#endif
695
696 NetArpWaitPacketIP = NetPingIP;
697 NetArpWaitPacketMAC = mac;
698
wdenk145d2c12004-04-15 21:48:45 +0000699 pkt = NetArpWaitTxPacket;
700 pkt += NetSetEther(pkt, mac, PROT_IP);
wdenke6466f62003-06-05 19:27:42 +0000701
wdenk145d2c12004-04-15 21:48:45 +0000702 ip = (volatile IP_t *)pkt;
wdenke6466f62003-06-05 19:27:42 +0000703
704 /*
705 * Construct an IP and ICMP header. (need to set no fragment bit - XXX)
706 */
707 ip->ip_hl_v = 0x45; /* IP_HDR_SIZE / 4 (not including UDP) */
708 ip->ip_tos = 0;
709 ip->ip_len = htons(IP_HDR_SIZE_NO_UDP + 8);
710 ip->ip_id = htons(NetIPID++);
Peter Tyser0885bf02008-12-01 16:26:20 -0600711 ip->ip_off = htons(IP_FLAGS_DFRAG); /* Don't fragment */
wdenke6466f62003-06-05 19:27:42 +0000712 ip->ip_ttl = 255;
713 ip->ip_p = 0x01; /* ICMP */
714 ip->ip_sum = 0;
715 NetCopyIP((void*)&ip->ip_src, &NetOurIP); /* already in network byte order */
716 NetCopyIP((void*)&ip->ip_dst, &NetPingIP); /* - "" - */
717 ip->ip_sum = ~NetCksum((uchar *)ip, IP_HDR_SIZE_NO_UDP / 2);
718
719 s = &ip->udp_src; /* XXX ICMP starts here */
720 s[0] = htons(0x0800); /* echo-request, code */
721 s[1] = 0; /* checksum */
Wolfgang Denka1be4762008-05-20 16:00:29 +0200722 s[2] = 0; /* identifier */
wdenke6466f62003-06-05 19:27:42 +0000723 s[3] = htons(PingSeqNo++); /* sequence number */
724 s[1] = ~NetCksum((uchar *)s, 8/2);
725
726 /* size of the waiting packet */
wdenk145d2c12004-04-15 21:48:45 +0000727 NetArpWaitTxPacketSize = (pkt - NetArpWaitTxPacket) + IP_HDR_SIZE_NO_UDP + 8;
wdenke6466f62003-06-05 19:27:42 +0000728
729 /* and do the ARP request */
730 NetArpWaitTry = 1;
731 NetArpWaitTimerStart = get_timer(0);
732 ArpRequest();
733 return 1; /* waiting */
734}
735
736static void
737PingTimeout (void)
738{
739 eth_halt();
740 NetState = NETLOOP_FAIL; /* we did not get the reply */
741}
742
743static void
744PingHandler (uchar * pkt, unsigned dest, unsigned src, unsigned len)
745{
746 IPaddr_t tmp;
747 volatile IP_t *ip = (volatile IP_t *)pkt;
748
749 tmp = NetReadIP((void *)&ip->ip_src);
750 if (tmp != NetPingIP)
751 return;
752
753 NetState = NETLOOP_SUCCESS;
754}
755
756static void PingStart(void)
757{
wdenk145d2c12004-04-15 21:48:45 +0000758#if defined(CONFIG_NET_MULTI)
759 printf ("Using %s device\n", eth_get_name());
wdenk05939202004-04-18 17:39:38 +0000760#endif /* CONFIG_NET_MULTI */
Bartlomiej Sieka56668462008-10-01 15:26:28 +0200761 NetSetTimeout (10000UL, PingTimeout);
wdenke6466f62003-06-05 19:27:42 +0000762 NetSetHandler (PingHandler);
763
764 PingSend();
765}
Jon Loeligera9807e52007-07-10 11:05:02 -0500766#endif
wdenk145d2c12004-04-15 21:48:45 +0000767
Jon Loeliger54f35c22007-07-09 17:45:14 -0500768#if defined(CONFIG_CMD_CDP)
wdenk145d2c12004-04-15 21:48:45 +0000769
770#define CDP_DEVICE_ID_TLV 0x0001
771#define CDP_ADDRESS_TLV 0x0002
772#define CDP_PORT_ID_TLV 0x0003
773#define CDP_CAPABILITIES_TLV 0x0004
774#define CDP_VERSION_TLV 0x0005
775#define CDP_PLATFORM_TLV 0x0006
776#define CDP_NATIVE_VLAN_TLV 0x000a
777#define CDP_APPLIANCE_VLAN_TLV 0x000e
778#define CDP_TRIGGER_TLV 0x000f
779#define CDP_POWER_CONSUMPTION_TLV 0x0010
780#define CDP_SYSNAME_TLV 0x0014
781#define CDP_SYSOBJECT_TLV 0x0015
782#define CDP_MANAGEMENT_ADDRESS_TLV 0x0016
783
Bartlomiej Sieka56668462008-10-01 15:26:28 +0200784#define CDP_TIMEOUT 250UL /* one packet every 250ms */
wdenk145d2c12004-04-15 21:48:45 +0000785
786static int CDPSeq;
787static int CDPOK;
788
789ushort CDPNativeVLAN;
790ushort CDPApplianceVLAN;
791
792static const uchar CDP_SNAP_hdr[8] = { 0xAA, 0xAA, 0x03, 0x00, 0x00, 0x0C, 0x20, 0x00 };
793
794static ushort CDP_compute_csum(const uchar *buff, ushort len)
795{
796 ushort csum;
797 int odd;
798 ulong result = 0;
799 ushort leftover;
Wolfgang Denk7fb52662005-10-13 16:45:02 +0200800 ushort *p;
wdenk145d2c12004-04-15 21:48:45 +0000801
802 if (len > 0) {
803 odd = 1 & (ulong)buff;
804 if (odd) {
805 result = *buff << 8;
806 len--;
807 buff++;
808 }
809 while (len > 1) {
Wolfgang Denk7fb52662005-10-13 16:45:02 +0200810 p = (ushort *)buff;
811 result += *p++;
812 buff = (uchar *)p;
wdenk145d2c12004-04-15 21:48:45 +0000813 if (result & 0x80000000)
814 result = (result & 0xFFFF) + (result >> 16);
815 len -= 2;
816 }
817 if (len) {
818 leftover = (signed short)(*(const signed char *)buff);
Wolfgang Denkf0711132005-11-10 20:59:46 +0100819 /* CISCO SUCKS big time! (and blows too):
820 * CDP uses the IP checksum algorithm with a twist;
821 * for the last byte it *sign* extends and sums.
822 */
wdenk145d2c12004-04-15 21:48:45 +0000823 result = (result & 0xffff0000) | ((result + leftover) & 0x0000ffff);
824 }
825 while (result >> 16)
826 result = (result & 0xFFFF) + (result >> 16);
827
828 if (odd)
829 result = ((result >> 8) & 0xff) | ((result & 0xff) << 8);
830 }
831
832 /* add up 16-bit and 17-bit words for 17+c bits */
833 result = (result & 0xffff) + (result >> 16);
834 /* add up 16-bit and 2-bit for 16+c bit */
835 result = (result & 0xffff) + (result >> 16);
836 /* add up carry.. */
837 result = (result & 0xffff) + (result >> 16);
838
839 /* negate */
840 csum = ~(ushort)result;
841
842 /* run time endian detection */
843 if (csum != htons(csum)) /* little endian */
844 csum = htons(csum);
845
846 return csum;
847}
848
849int CDPSendTrigger(void)
850{
851 volatile uchar *pkt;
852 volatile ushort *s;
853 volatile ushort *cp;
854 Ethernet_t *et;
wdenk145d2c12004-04-15 21:48:45 +0000855 int len;
856 ushort chksum;
wdenk05939202004-04-18 17:39:38 +0000857#if defined(CONFIG_CDP_DEVICE_ID) || defined(CONFIG_CDP_PORT_ID) || \
858 defined(CONFIG_CDP_VERSION) || defined(CONFIG_CDP_PLATFORM)
859 char buf[32];
860#endif
wdenk145d2c12004-04-15 21:48:45 +0000861
862 pkt = NetTxPacket;
863 et = (Ethernet_t *)pkt;
864
865 /* NOTE: trigger sent not on any VLAN */
866
867 /* form ethernet header */
868 memcpy(et->et_dest, NetCDPAddr, 6);
869 memcpy(et->et_src, NetOurEther, 6);
870
871 pkt += ETHER_HDR_SIZE;
872
873 /* SNAP header */
874 memcpy((uchar *)pkt, CDP_SNAP_hdr, sizeof(CDP_SNAP_hdr));
875 pkt += sizeof(CDP_SNAP_hdr);
876
877 /* CDP header */
878 *pkt++ = 0x02; /* CDP version 2 */
879 *pkt++ = 180; /* TTL */
880 s = (volatile ushort *)pkt;
881 cp = s;
882 *s++ = htons(0); /* checksum (0 for later calculation) */
883
884 /* CDP fields */
885#ifdef CONFIG_CDP_DEVICE_ID
886 *s++ = htons(CDP_DEVICE_ID_TLV);
887 *s++ = htons(CONFIG_CDP_DEVICE_ID);
Mike Frysinger79ca1b92009-02-11 18:23:48 -0500888 sprintf(buf, CONFIG_CDP_DEVICE_ID_PREFIX "%pm", NetOurEther);
wdenk145d2c12004-04-15 21:48:45 +0000889 memcpy((uchar *)s, buf, 16);
890 s += 16 / 2;
891#endif
892
893#ifdef CONFIG_CDP_PORT_ID
894 *s++ = htons(CDP_PORT_ID_TLV);
895 memset(buf, 0, sizeof(buf));
896 sprintf(buf, CONFIG_CDP_PORT_ID, eth_get_dev_index());
897 len = strlen(buf);
898 if (len & 1) /* make it even */
899 len++;
900 *s++ = htons(len + 4);
901 memcpy((uchar *)s, buf, len);
902 s += len / 2;
903#endif
904
905#ifdef CONFIG_CDP_CAPABILITIES
906 *s++ = htons(CDP_CAPABILITIES_TLV);
907 *s++ = htons(8);
908 *(ulong *)s = htonl(CONFIG_CDP_CAPABILITIES);
909 s += 2;
910#endif
911
912#ifdef CONFIG_CDP_VERSION
913 *s++ = htons(CDP_VERSION_TLV);
914 memset(buf, 0, sizeof(buf));
915 strcpy(buf, CONFIG_CDP_VERSION);
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_PLATFORM
925 *s++ = htons(CDP_PLATFORM_TLV);
926 memset(buf, 0, sizeof(buf));
927 strcpy(buf, CONFIG_CDP_PLATFORM);
928 len = strlen(buf);
929 if (len & 1) /* make it even */
930 len++;
931 *s++ = htons(len + 4);
932 memcpy((uchar *)s, buf, len);
933 s += len / 2;
934#endif
935
936#ifdef CONFIG_CDP_TRIGGER
937 *s++ = htons(CDP_TRIGGER_TLV);
938 *s++ = htons(8);
939 *(ulong *)s = htonl(CONFIG_CDP_TRIGGER);
940 s += 2;
941#endif
942
943#ifdef CONFIG_CDP_POWER_CONSUMPTION
944 *s++ = htons(CDP_POWER_CONSUMPTION_TLV);
945 *s++ = htons(6);
946 *s++ = htons(CONFIG_CDP_POWER_CONSUMPTION);
947#endif
948
949 /* length of ethernet packet */
950 len = (uchar *)s - ((uchar *)NetTxPacket + ETHER_HDR_SIZE);
951 et->et_protlen = htons(len);
952
953 len = ETHER_HDR_SIZE + sizeof(CDP_SNAP_hdr);
954 chksum = CDP_compute_csum((uchar *)NetTxPacket + len, (uchar *)s - (NetTxPacket + len));
955 if (chksum == 0)
956 chksum = 0xFFFF;
957 *cp = htons(chksum);
958
959 (void) eth_send(NetTxPacket, (uchar *)s - NetTxPacket);
960 return 0;
961}
962
963static void
964CDPTimeout (void)
965{
966 CDPSeq++;
967
968 if (CDPSeq < 3) {
969 NetSetTimeout (CDP_TIMEOUT, CDPTimeout);
970 CDPSendTrigger();
971 return;
972 }
973
974 /* if not OK try again */
975 if (!CDPOK)
976 NetStartAgain();
977 else
978 NetState = NETLOOP_SUCCESS;
979}
980
981static void
982CDPDummyHandler (uchar * pkt, unsigned dest, unsigned src, unsigned len)
983{
984 /* nothing */
985}
986
987static void
988CDPHandler(const uchar * pkt, unsigned len)
989{
990 const uchar *t;
991 const ushort *ss;
992 ushort type, tlen;
993 uchar applid;
994 ushort vlan, nvlan;
995
996 /* minimum size? */
997 if (len < sizeof(CDP_SNAP_hdr) + 4)
998 goto pkt_short;
999
1000 /* check for valid CDP SNAP header */
1001 if (memcmp(pkt, CDP_SNAP_hdr, sizeof(CDP_SNAP_hdr)) != 0)
1002 return;
1003
1004 pkt += sizeof(CDP_SNAP_hdr);
1005 len -= sizeof(CDP_SNAP_hdr);
1006
1007 /* Version of CDP protocol must be >= 2 and TTL != 0 */
1008 if (pkt[0] < 0x02 || pkt[1] == 0)
1009 return;
1010
1011 /* if version is greater than 0x02 maybe we'll have a problem; output a warning */
1012 if (pkt[0] != 0x02)
1013 printf("** WARNING: CDP packet received with a protocol version %d > 2\n",
1014 pkt[0] & 0xff);
1015
1016 if (CDP_compute_csum(pkt, len) != 0)
1017 return;
1018
1019 pkt += 4;
1020 len -= 4;
1021
1022 vlan = htons(-1);
1023 nvlan = htons(-1);
1024 while (len > 0) {
1025 if (len < 4)
1026 goto pkt_short;
1027
1028 ss = (const ushort *)pkt;
1029 type = ntohs(ss[0]);
1030 tlen = ntohs(ss[1]);
1031 if (tlen > len) {
1032 goto pkt_short;
1033 }
1034
1035 pkt += tlen;
1036 len -= tlen;
1037
1038 ss += 2; /* point ss to the data of the TLV */
1039 tlen -= 4;
1040
1041 switch (type) {
1042 case CDP_DEVICE_ID_TLV:
1043 break;
1044 case CDP_ADDRESS_TLV:
1045 break;
1046 case CDP_PORT_ID_TLV:
1047 break;
1048 case CDP_CAPABILITIES_TLV:
1049 break;
1050 case CDP_VERSION_TLV:
1051 break;
1052 case CDP_PLATFORM_TLV:
1053 break;
1054 case CDP_NATIVE_VLAN_TLV:
1055 nvlan = *ss;
1056 break;
1057 case CDP_APPLIANCE_VLAN_TLV:
1058 t = (const uchar *)ss;
1059 while (tlen > 0) {
1060 if (tlen < 3)
1061 goto pkt_short;
1062
1063 applid = t[0];
1064 ss = (const ushort *)(t + 1);
1065
1066#ifdef CONFIG_CDP_APPLIANCE_VLAN_TYPE
1067 if (applid == CONFIG_CDP_APPLIANCE_VLAN_TYPE)
1068 vlan = *ss;
1069#else
1070 vlan = ntohs(*ss); /* XXX will this work; dunno */
1071#endif
1072 t += 3; tlen -= 3;
1073 }
1074 break;
1075 case CDP_TRIGGER_TLV:
1076 break;
1077 case CDP_POWER_CONSUMPTION_TLV:
1078 break;
1079 case CDP_SYSNAME_TLV:
1080 break;
1081 case CDP_SYSOBJECT_TLV:
1082 break;
1083 case CDP_MANAGEMENT_ADDRESS_TLV:
1084 break;
1085 }
1086 }
1087
1088 CDPApplianceVLAN = vlan;
1089 CDPNativeVLAN = nvlan;
1090
1091 CDPOK = 1;
1092 return;
1093
1094 pkt_short:
1095 printf("** CDP packet is too short\n");
1096 return;
1097}
1098
1099static void CDPStart(void)
1100{
1101#if defined(CONFIG_NET_MULTI)
1102 printf ("Using %s device\n", eth_get_name());
wdenke6466f62003-06-05 19:27:42 +00001103#endif
wdenk145d2c12004-04-15 21:48:45 +00001104 CDPSeq = 0;
1105 CDPOK = 0;
1106
1107 CDPNativeVLAN = htons(-1);
1108 CDPApplianceVLAN = htons(-1);
1109
1110 NetSetTimeout (CDP_TIMEOUT, CDPTimeout);
1111 NetSetHandler (CDPDummyHandler);
1112
1113 CDPSendTrigger();
1114}
Jon Loeligera9807e52007-07-10 11:05:02 -05001115#endif
wdenk145d2c12004-04-15 21:48:45 +00001116
wdenk2d966952002-10-31 22:12:35 +00001117
1118void
wdenk145d2c12004-04-15 21:48:45 +00001119NetReceive(volatile uchar * inpkt, int len)
wdenk2d966952002-10-31 22:12:35 +00001120{
1121 Ethernet_t *et;
1122 IP_t *ip;
1123 ARP_t *arp;
1124 IPaddr_t tmp;
1125 int x;
wdenk145d2c12004-04-15 21:48:45 +00001126 uchar *pkt;
Jon Loeliger54f35c22007-07-09 17:45:14 -05001127#if defined(CONFIG_CMD_CDP)
wdenk145d2c12004-04-15 21:48:45 +00001128 int iscdp;
1129#endif
1130 ushort cti = 0, vlanid = VLAN_NONE, myvlanid, mynvlanid;
wdenk2d966952002-10-31 22:12:35 +00001131
wdenk145d2c12004-04-15 21:48:45 +00001132#ifdef ET_DEBUG
1133 printf("packet received\n");
1134#endif
1135
Mike Frysingerdc166032009-07-18 21:04:08 -04001136 NetRxPacket = inpkt;
1137 NetRxPacketLen = len;
wdenk145d2c12004-04-15 21:48:45 +00001138 et = (Ethernet_t *)inpkt;
1139
1140 /* too small packet? */
1141 if (len < ETHER_HDR_SIZE)
1142 return;
Rafal Jaworowski1f2c9a42007-12-27 18:19:02 +01001143
1144#ifdef CONFIG_API
1145 if (push_packet) {
1146 (*push_packet)(inpkt, len);
1147 return;
1148 }
1149#endif
wdenk145d2c12004-04-15 21:48:45 +00001150
Jon Loeliger54f35c22007-07-09 17:45:14 -05001151#if defined(CONFIG_CMD_CDP)
wdenk145d2c12004-04-15 21:48:45 +00001152 /* keep track if packet is CDP */
1153 iscdp = memcmp(et->et_dest, NetCDPAddr, 6) == 0;
1154#endif
1155
1156 myvlanid = ntohs(NetOurVLAN);
1157 if (myvlanid == (ushort)-1)
1158 myvlanid = VLAN_NONE;
1159 mynvlanid = ntohs(NetOurNativeVLAN);
1160 if (mynvlanid == (ushort)-1)
1161 mynvlanid = VLAN_NONE;
wdenk2d966952002-10-31 22:12:35 +00001162
1163 x = ntohs(et->et_protlen);
1164
wdenk145d2c12004-04-15 21:48:45 +00001165#ifdef ET_DEBUG
1166 printf("packet received\n");
1167#endif
1168
wdenk2d966952002-10-31 22:12:35 +00001169 if (x < 1514) {
1170 /*
1171 * Got a 802 packet. Check the other protocol field.
1172 */
1173 x = ntohs(et->et_prot);
wdenk145d2c12004-04-15 21:48:45 +00001174
1175 ip = (IP_t *)(inpkt + E802_HDR_SIZE);
wdenk2d966952002-10-31 22:12:35 +00001176 len -= E802_HDR_SIZE;
wdenk145d2c12004-04-15 21:48:45 +00001177
1178 } else if (x != PROT_VLAN) { /* normal packet */
1179 ip = (IP_t *)(inpkt + ETHER_HDR_SIZE);
wdenk2d966952002-10-31 22:12:35 +00001180 len -= ETHER_HDR_SIZE;
wdenk145d2c12004-04-15 21:48:45 +00001181
1182 } else { /* VLAN packet */
1183 VLAN_Ethernet_t *vet = (VLAN_Ethernet_t *)et;
1184
1185#ifdef ET_DEBUG
1186 printf("VLAN packet received\n");
1187#endif
1188 /* too small packet? */
1189 if (len < VLAN_ETHER_HDR_SIZE)
1190 return;
1191
1192 /* if no VLAN active */
1193 if ((ntohs(NetOurVLAN) & VLAN_IDMASK) == VLAN_NONE
Jon Loeliger54f35c22007-07-09 17:45:14 -05001194#if defined(CONFIG_CMD_CDP)
wdenk145d2c12004-04-15 21:48:45 +00001195 && iscdp == 0
1196#endif
1197 )
1198 return;
1199
1200 cti = ntohs(vet->vet_tag);
1201 vlanid = cti & VLAN_IDMASK;
1202 x = ntohs(vet->vet_type);
1203
1204 ip = (IP_t *)(inpkt + VLAN_ETHER_HDR_SIZE);
1205 len -= VLAN_ETHER_HDR_SIZE;
wdenk2d966952002-10-31 22:12:35 +00001206 }
1207
1208#ifdef ET_DEBUG
1209 printf("Receive from protocol 0x%x\n", x);
1210#endif
1211
Jon Loeliger54f35c22007-07-09 17:45:14 -05001212#if defined(CONFIG_CMD_CDP)
wdenk145d2c12004-04-15 21:48:45 +00001213 if (iscdp) {
1214 CDPHandler((uchar *)ip, len);
1215 return;
1216 }
1217#endif
1218
1219 if ((myvlanid & VLAN_IDMASK) != VLAN_NONE) {
1220 if (vlanid == VLAN_NONE)
1221 vlanid = (mynvlanid & VLAN_IDMASK);
1222 /* not matched? */
1223 if (vlanid != (myvlanid & VLAN_IDMASK))
1224 return;
1225 }
1226
wdenk2d966952002-10-31 22:12:35 +00001227 switch (x) {
1228
1229 case PROT_ARP:
1230 /*
1231 * We have to deal with two types of ARP packets:
wdenk57b2d802003-06-27 21:31:46 +00001232 * - REQUEST packets will be answered by sending our
1233 * IP address - if we know it.
1234 * - REPLY packates are expected only after we asked
1235 * for the TFTP server's or the gateway's ethernet
1236 * address; so if we receive such a packet, we set
1237 * the server ethernet address
wdenk2d966952002-10-31 22:12:35 +00001238 */
1239#ifdef ET_DEBUG
wdenk42c05472004-03-23 22:14:11 +00001240 puts ("Got ARP\n");
wdenk2d966952002-10-31 22:12:35 +00001241#endif
1242 arp = (ARP_t *)ip;
1243 if (len < ARP_HDR_SIZE) {
1244 printf("bad length %d < %d\n", len, ARP_HDR_SIZE);
1245 return;
1246 }
1247 if (ntohs(arp->ar_hrd) != ARP_ETHER) {
1248 return;
1249 }
1250 if (ntohs(arp->ar_pro) != PROT_IP) {
1251 return;
1252 }
1253 if (arp->ar_hln != 6) {
1254 return;
1255 }
1256 if (arp->ar_pln != 4) {
1257 return;
1258 }
1259
1260 if (NetOurIP == 0) {
1261 return;
1262 }
1263
1264 if (NetReadIP(&arp->ar_data[16]) != NetOurIP) {
1265 return;
1266 }
1267
1268 switch (ntohs(arp->ar_op)) {
1269 case ARPOP_REQUEST: /* reply with our IP address */
1270#ifdef ET_DEBUG
wdenk42c05472004-03-23 22:14:11 +00001271 puts ("Got ARP REQUEST, return our IP\n");
wdenk2d966952002-10-31 22:12:35 +00001272#endif
wdenk145d2c12004-04-15 21:48:45 +00001273 pkt = (uchar *)et;
1274 pkt += NetSetEther(pkt, et->et_src, PROT_ARP);
wdenk2d966952002-10-31 22:12:35 +00001275 arp->ar_op = htons(ARPOP_REPLY);
1276 memcpy (&arp->ar_data[10], &arp->ar_data[0], 6);
1277 NetCopyIP(&arp->ar_data[16], &arp->ar_data[6]);
1278 memcpy (&arp->ar_data[ 0], NetOurEther, 6);
1279 NetCopyIP(&arp->ar_data[ 6], &NetOurIP);
wdenk145d2c12004-04-15 21:48:45 +00001280 (void) eth_send((uchar *)et, (pkt - (uchar *)et) + ARP_HDR_SIZE);
wdenk2d966952002-10-31 22:12:35 +00001281 return;
wdenke6466f62003-06-05 19:27:42 +00001282
1283 case ARPOP_REPLY: /* arp reply */
1284 /* are we waiting for a reply */
1285 if (!NetArpWaitPacketIP || !NetArpWaitPacketMAC)
1286 break;
1287#ifdef ET_DEBUG
Mike Frysinger79ca1b92009-02-11 18:23:48 -05001288 printf("Got ARP REPLY, set server/gtwy eth addr (%pM)\n",
1289 arp->ar_data);
wdenke6466f62003-06-05 19:27:42 +00001290#endif
1291
1292 tmp = NetReadIP(&arp->ar_data[6]);
1293
1294 /* matched waiting packet's address */
1295 if (tmp == NetArpWaitReplyIP) {
wdenk2d966952002-10-31 22:12:35 +00001296#ifdef ET_DEBUG
wdenk42c05472004-03-23 22:14:11 +00001297 puts ("Got it\n");
wdenk2d966952002-10-31 22:12:35 +00001298#endif
wdenke6466f62003-06-05 19:27:42 +00001299 /* save address for later use */
1300 memcpy(NetArpWaitPacketMAC, &arp->ar_data[0], 6);
1301
wdenkb8fb6192004-08-02 21:11:11 +00001302#ifdef CONFIG_NETCONSOLE
1303 (*packetHandler)(0,0,0,0);
1304#endif
wdenke6466f62003-06-05 19:27:42 +00001305 /* modify header, and transmit it */
1306 memcpy(((Ethernet_t *)NetArpWaitTxPacket)->et_dest, NetArpWaitPacketMAC, 6);
1307 (void) eth_send(NetArpWaitTxPacket, NetArpWaitTxPacketSize);
1308
1309 /* no arp request pending now */
1310 NetArpWaitPacketIP = 0;
1311 NetArpWaitTxPacketSize = 0;
1312 NetArpWaitPacketMAC = NULL;
1313
1314 }
wdenk2d966952002-10-31 22:12:35 +00001315 return;
1316 default:
1317#ifdef ET_DEBUG
1318 printf("Unexpected ARP opcode 0x%x\n", ntohs(arp->ar_op));
1319#endif
1320 return;
1321 }
wdenkcb99da52005-01-12 00:15:14 +00001322 break;
wdenk2d966952002-10-31 22:12:35 +00001323
1324 case PROT_RARP:
1325#ifdef ET_DEBUG
wdenk42c05472004-03-23 22:14:11 +00001326 puts ("Got RARP\n");
wdenk2d966952002-10-31 22:12:35 +00001327#endif
1328 arp = (ARP_t *)ip;
1329 if (len < ARP_HDR_SIZE) {
1330 printf("bad length %d < %d\n", len, ARP_HDR_SIZE);
1331 return;
1332 }
1333
1334 if ((ntohs(arp->ar_op) != RARPOP_REPLY) ||
1335 (ntohs(arp->ar_hrd) != ARP_ETHER) ||
1336 (ntohs(arp->ar_pro) != PROT_IP) ||
1337 (arp->ar_hln != 6) || (arp->ar_pln != 4)) {
1338
wdenk42c05472004-03-23 22:14:11 +00001339 puts ("invalid RARP header\n");
wdenk2d966952002-10-31 22:12:35 +00001340 } else {
1341 NetCopyIP(&NetOurIP, &arp->ar_data[16]);
wdenk4989f872004-03-14 15:06:13 +00001342 if (NetServerIP == 0)
1343 NetCopyIP(&NetServerIP, &arp->ar_data[ 6]);
wdenk2d966952002-10-31 22:12:35 +00001344 memcpy (NetServerEther, &arp->ar_data[ 0], 6);
1345
1346 (*packetHandler)(0,0,0,0);
1347 }
1348 break;
1349
1350 case PROT_IP:
1351#ifdef ET_DEBUG
wdenk42c05472004-03-23 22:14:11 +00001352 puts ("Got IP\n");
wdenk2d966952002-10-31 22:12:35 +00001353#endif
1354 if (len < IP_HDR_SIZE) {
Wolfgang Denk81490f42008-07-13 23:07:35 +02001355 debug ("len bad %d < %lu\n", len, (ulong)IP_HDR_SIZE);
wdenk2d966952002-10-31 22:12:35 +00001356 return;
1357 }
1358 if (len < ntohs(ip->ip_len)) {
1359 printf("len bad %d < %d\n", len, ntohs(ip->ip_len));
1360 return;
1361 }
1362 len = ntohs(ip->ip_len);
1363#ifdef ET_DEBUG
1364 printf("len=%d, v=%02x\n", len, ip->ip_hl_v & 0xff);
1365#endif
1366 if ((ip->ip_hl_v & 0xf0) != 0x40) {
1367 return;
1368 }
Peter Tyser49570cd2008-12-01 16:26:21 -06001369 /* Can't deal with fragments */
1370 if (ip->ip_off & htons(IP_OFFS | IP_FLAGS_MFRAG)) {
wdenk2d966952002-10-31 22:12:35 +00001371 return;
1372 }
Remy Bohmerb9535782008-06-03 15:48:17 +02001373 /* can't deal with headers > 20 bytes */
1374 if ((ip->ip_hl_v & 0x0f) > 0x05) {
1375 return;
1376 }
wdenk2d966952002-10-31 22:12:35 +00001377 if (!NetCksumOk((uchar *)ip, IP_HDR_SIZE_NO_UDP / 2)) {
wdenk42c05472004-03-23 22:14:11 +00001378 puts ("checksum bad\n");
wdenk2d966952002-10-31 22:12:35 +00001379 return;
1380 }
1381 tmp = NetReadIP(&ip->ip_dst);
1382 if (NetOurIP && tmp != NetOurIP && tmp != 0xFFFFFFFF) {
David Updegraff7280da72007-06-11 10:41:07 -05001383#ifdef CONFIG_MCAST_TFTP
Wolfgang Denk627f5c32007-08-14 09:47:27 +02001384 if (Mcast_addr != tmp)
David Updegraff7280da72007-06-11 10:41:07 -05001385#endif
wdenk2d966952002-10-31 22:12:35 +00001386 return;
1387 }
1388 /*
1389 * watch for ICMP host redirects
1390 *
wdenk57b2d802003-06-27 21:31:46 +00001391 * There is no real handler code (yet). We just watch
1392 * for ICMP host redirect messages. In case anybody
1393 * sees these messages: please contact me
1394 * (wd@denx.de), or - even better - send me the
1395 * necessary fixes :-)
wdenk2d966952002-10-31 22:12:35 +00001396 *
wdenk57b2d802003-06-27 21:31:46 +00001397 * Note: in all cases where I have seen this so far
1398 * it was a problem with the router configuration,
1399 * for instance when a router was configured in the
1400 * BOOTP reply, but the TFTP server was on the same
1401 * subnet. So this is probably a warning that your
1402 * configuration might be wrong. But I'm not really
1403 * sure if there aren't any other situations.
wdenk2d966952002-10-31 22:12:35 +00001404 */
1405 if (ip->ip_p == IPPROTO_ICMP) {
1406 ICMP_t *icmph = (ICMP_t *)&(ip->udp_src);
1407
wdenke6466f62003-06-05 19:27:42 +00001408 switch (icmph->type) {
1409 case ICMP_REDIRECT:
wdenkcbc49a52005-05-03 14:12:25 +00001410 if (icmph->code != ICMP_REDIR_HOST)
1411 return;
Mike Frysingerd5695f42009-02-17 00:00:53 -05001412 printf (" ICMP Host Redirect to %pI4 ", &icmph->un.gateway);
Stefan Roesedfced812005-08-12 20:06:52 +02001413 return;
Jon Loeliger54f35c22007-07-09 17:45:14 -05001414#if defined(CONFIG_CMD_PING)
wdenke6466f62003-06-05 19:27:42 +00001415 case ICMP_ECHO_REPLY:
1416 /*
1417 * IP header OK. Pass the packet to the current handler.
1418 */
1419 /* XXX point to ip packet */
1420 (*packetHandler)((uchar *)ip, 0, 0, 0);
Stefan Roesedfced812005-08-12 20:06:52 +02001421 return;
Ed Swarthout83109192007-03-07 12:14:50 -06001422 case ICMP_ECHO_REQUEST:
1423#ifdef ET_DEBUG
1424 printf ("Got ICMP ECHO REQUEST, return %d bytes \n",
1425 ETHER_HDR_SIZE + len);
1426#endif
1427 memcpy (&et->et_dest[0], &et->et_src[0], 6);
1428 memcpy (&et->et_src[ 0], NetOurEther, 6);
1429
1430 ip->ip_sum = 0;
1431 ip->ip_off = 0;
1432 NetCopyIP((void*)&ip->ip_dst, &ip->ip_src);
1433 NetCopyIP((void*)&ip->ip_src, &NetOurIP);
1434 ip->ip_sum = ~NetCksum((uchar *)ip, IP_HDR_SIZE_NO_UDP >> 1);
1435
1436 icmph->type = ICMP_ECHO_REPLY;
1437 icmph->checksum = 0;
1438 icmph->checksum = ~NetCksum((uchar *)icmph,
1439 (len - IP_HDR_SIZE_NO_UDP) >> 1);
1440 (void) eth_send((uchar *)et, ETHER_HDR_SIZE + len);
1441 return;
wdenke6466f62003-06-05 19:27:42 +00001442#endif
1443 default:
1444 return;
1445 }
wdenk2d966952002-10-31 22:12:35 +00001446 } else if (ip->ip_p != IPPROTO_UDP) { /* Only UDP packets */
1447 return;
1448 }
1449
Stefan Roesedfced812005-08-12 20:06:52 +02001450#ifdef CONFIG_UDP_CHECKSUM
1451 if (ip->udp_xsum != 0) {
Wolfgang Denk30b87322005-08-12 23:43:12 +02001452 ulong xsum;
Stefan Roesedfced812005-08-12 20:06:52 +02001453 ushort *sumptr;
1454 ushort sumlen;
1455
1456 xsum = ip->ip_p;
1457 xsum += (ntohs(ip->udp_len));
1458 xsum += (ntohl(ip->ip_src) >> 16) & 0x0000ffff;
1459 xsum += (ntohl(ip->ip_src) >> 0) & 0x0000ffff;
1460 xsum += (ntohl(ip->ip_dst) >> 16) & 0x0000ffff;
1461 xsum += (ntohl(ip->ip_dst) >> 0) & 0x0000ffff;
1462
1463 sumlen = ntohs(ip->udp_len);
1464 sumptr = (ushort *) &(ip->udp_src);
1465
1466 while (sumlen > 1) {
Wolfgang Denk30b87322005-08-12 23:43:12 +02001467 ushort sumdata;
Stefan Roesedfced812005-08-12 20:06:52 +02001468
1469 sumdata = *sumptr++;
1470 xsum += ntohs(sumdata);
1471 sumlen -= 2;
1472 }
1473 if (sumlen > 0) {
Wolfgang Denk30b87322005-08-12 23:43:12 +02001474 ushort sumdata;
Stefan Roesedfced812005-08-12 20:06:52 +02001475
1476 sumdata = *(unsigned char *) sumptr;
Wolfgang Denk30b87322005-08-12 23:43:12 +02001477 sumdata = (sumdata << 8) & 0xff00;
Stefan Roesedfced812005-08-12 20:06:52 +02001478 xsum += sumdata;
1479 }
1480 while ((xsum >> 16) != 0) {
Wolfgang Denk30b87322005-08-12 23:43:12 +02001481 xsum = (xsum & 0x0000ffff) + ((xsum >> 16) & 0x0000ffff);
Stefan Roesedfced812005-08-12 20:06:52 +02001482 }
1483 if ((xsum != 0x00000000) && (xsum != 0x0000ffff)) {
Wolfgang Denk12cec0a2008-07-11 01:16:00 +02001484 printf(" UDP wrong checksum %08lx %08x\n",
1485 xsum, ntohs(ip->udp_xsum));
Stefan Roesedfced812005-08-12 20:06:52 +02001486 return;
1487 }
1488 }
1489#endif
1490
David Updegraff7280da72007-06-11 10:41:07 -05001491
wdenkb8fb6192004-08-02 21:11:11 +00001492#ifdef CONFIG_NETCONSOLE
1493 nc_input_packet((uchar *)ip +IP_HDR_SIZE,
1494 ntohs(ip->udp_dst),
1495 ntohs(ip->udp_src),
1496 ntohs(ip->udp_len) - 8);
1497#endif
wdenk2d966952002-10-31 22:12:35 +00001498 /*
1499 * IP header OK. Pass the packet to the current handler.
1500 */
1501 (*packetHandler)((uchar *)ip +IP_HDR_SIZE,
1502 ntohs(ip->udp_dst),
1503 ntohs(ip->udp_src),
1504 ntohs(ip->udp_len) - 8);
wdenk2d966952002-10-31 22:12:35 +00001505 break;
1506 }
1507}
1508
1509
1510/**********************************************************************/
1511
1512static int net_check_prereq (proto_t protocol)
1513{
1514 switch (protocol) {
wdenk05939202004-04-18 17:39:38 +00001515 /* Fall through */
Jon Loeliger54f35c22007-07-09 17:45:14 -05001516#if defined(CONFIG_CMD_PING)
wdenke6466f62003-06-05 19:27:42 +00001517 case PING:
wdenk05939202004-04-18 17:39:38 +00001518 if (NetPingIP == 0) {
1519 puts ("*** ERROR: ping address not given\n");
1520 return (1);
1521 }
1522 goto common;
wdenke6466f62003-06-05 19:27:42 +00001523#endif
Jon Loeliger54f35c22007-07-09 17:45:14 -05001524#if defined(CONFIG_CMD_SNTP)
wdenkb4ad9622005-04-01 00:25:43 +00001525 case SNTP:
1526 if (NetNtpServerIP == 0) {
1527 puts ("*** ERROR: NTP server address not given\n");
1528 return (1);
1529 }
1530 goto common;
1531#endif
Robin Getz82f0d232009-07-20 14:53:54 -04001532#if defined(CONFIG_CMD_DNS)
1533 case DNS:
1534 if (NetOurDNSIP == 0) {
1535 puts("*** ERROR: DNS server address not given\n");
1536 return 1;
1537 }
1538 goto common;
1539#endif
Jon Loeliger54f35c22007-07-09 17:45:14 -05001540#if defined(CONFIG_CMD_NFS)
wdenkbe9c1cb2004-02-24 02:00:03 +00001541 case NFS:
1542#endif
wdenkb8fb6192004-08-02 21:11:11 +00001543 case NETCONS:
wdenk2d966952002-10-31 22:12:35 +00001544 case TFTP:
wdenk05939202004-04-18 17:39:38 +00001545 if (NetServerIP == 0) {
1546 puts ("*** ERROR: `serverip' not set\n");
1547 return (1);
1548 }
Jon Loeliger54f35c22007-07-09 17:45:14 -05001549#if defined(CONFIG_CMD_PING) || defined(CONFIG_CMD_SNTP)
Wolfgang Denk30b87322005-08-12 23:43:12 +02001550 common:
wdenke6466f62003-06-05 19:27:42 +00001551#endif
1552
wdenk05939202004-04-18 17:39:38 +00001553 if (NetOurIP == 0) {
1554 puts ("*** ERROR: `ipaddr' not set\n");
1555 return (1);
1556 }
1557 /* Fall through */
wdenk2d966952002-10-31 22:12:35 +00001558
1559 case DHCP:
1560 case RARP:
1561 case BOOTP:
wdenk145d2c12004-04-15 21:48:45 +00001562 case CDP:
wdenk05939202004-04-18 17:39:38 +00001563 if (memcmp (NetOurEther, "\0\0\0\0\0\0", 6) == 0) {
wdenk2d966952002-10-31 22:12:35 +00001564#ifdef CONFIG_NET_MULTI
wdenk05939202004-04-18 17:39:38 +00001565 extern int eth_get_dev_index (void);
1566 int num = eth_get_dev_index ();
wdenk2d966952002-10-31 22:12:35 +00001567
wdenk05939202004-04-18 17:39:38 +00001568 switch (num) {
1569 case -1:
wdenk2d966952002-10-31 22:12:35 +00001570 puts ("*** ERROR: No ethernet found.\n");
1571 return (1);
wdenk05939202004-04-18 17:39:38 +00001572 case 0:
wdenk2d966952002-10-31 22:12:35 +00001573 puts ("*** ERROR: `ethaddr' not set\n");
1574 break;
wdenk05939202004-04-18 17:39:38 +00001575 default:
wdenk57b2d802003-06-27 21:31:46 +00001576 printf ("*** ERROR: `eth%daddr' not set\n",
wdenk2d966952002-10-31 22:12:35 +00001577 num);
1578 break;
wdenk05939202004-04-18 17:39:38 +00001579 }
wdenk2d966952002-10-31 22:12:35 +00001580
wdenk05939202004-04-18 17:39:38 +00001581 NetStartAgain ();
1582 return (2);
wdenk2d966952002-10-31 22:12:35 +00001583#else
wdenk05939202004-04-18 17:39:38 +00001584 puts ("*** ERROR: `ethaddr' not set\n");
1585 return (1);
wdenk2d966952002-10-31 22:12:35 +00001586#endif
wdenk05939202004-04-18 17:39:38 +00001587 }
1588 /* Fall through */
1589 default:
1590 return (0);
wdenk2d966952002-10-31 22:12:35 +00001591 }
wdenk05939202004-04-18 17:39:38 +00001592 return (0); /* OK */
wdenk2d966952002-10-31 22:12:35 +00001593}
1594/**********************************************************************/
1595
1596int
1597NetCksumOk(uchar * ptr, int len)
1598{
1599 return !((NetCksum(ptr, len) + 1) & 0xfffe);
1600}
1601
1602
1603unsigned
1604NetCksum(uchar * ptr, int len)
1605{
1606 ulong xsum;
Stefan Roeseac553282005-08-31 12:55:50 +02001607 ushort *p = (ushort *)ptr;
wdenk2d966952002-10-31 22:12:35 +00001608
1609 xsum = 0;
1610 while (len-- > 0)
Wolfgang Denk3135c542005-08-26 01:36:03 +02001611 xsum += *p++;
wdenk2d966952002-10-31 22:12:35 +00001612 xsum = (xsum & 0xffff) + (xsum >> 16);
1613 xsum = (xsum & 0xffff) + (xsum >> 16);
1614 return (xsum & 0xffff);
1615}
1616
wdenk145d2c12004-04-15 21:48:45 +00001617int
1618NetEthHdrSize(void)
1619{
1620 ushort myvlanid;
1621
1622 myvlanid = ntohs(NetOurVLAN);
1623 if (myvlanid == (ushort)-1)
1624 myvlanid = VLAN_NONE;
wdenk2d966952002-10-31 22:12:35 +00001625
wdenk145d2c12004-04-15 21:48:45 +00001626 return ((myvlanid & VLAN_IDMASK) == VLAN_NONE) ? ETHER_HDR_SIZE : VLAN_ETHER_HDR_SIZE;
1627}
1628
1629int
wdenk2d966952002-10-31 22:12:35 +00001630NetSetEther(volatile uchar * xet, uchar * addr, uint prot)
1631{
1632 Ethernet_t *et = (Ethernet_t *)xet;
wdenk145d2c12004-04-15 21:48:45 +00001633 ushort myvlanid;
1634
1635 myvlanid = ntohs(NetOurVLAN);
1636 if (myvlanid == (ushort)-1)
1637 myvlanid = VLAN_NONE;
wdenk2d966952002-10-31 22:12:35 +00001638
1639 memcpy (et->et_dest, addr, 6);
1640 memcpy (et->et_src, NetOurEther, 6);
wdenk145d2c12004-04-15 21:48:45 +00001641 if ((myvlanid & VLAN_IDMASK) == VLAN_NONE) {
wdenk2d966952002-10-31 22:12:35 +00001642 et->et_protlen = htons(prot);
wdenk145d2c12004-04-15 21:48:45 +00001643 return ETHER_HDR_SIZE;
1644 } else {
1645 VLAN_Ethernet_t *vet = (VLAN_Ethernet_t *)xet;
wdenk2d966952002-10-31 22:12:35 +00001646
wdenk145d2c12004-04-15 21:48:45 +00001647 vet->vet_vlan_type = htons(PROT_VLAN);
1648 vet->vet_tag = htons((0 << 5) | (myvlanid & VLAN_IDMASK));
1649 vet->vet_type = htons(prot);
1650 return VLAN_ETHER_HDR_SIZE;
1651 }
1652}
wdenk2d966952002-10-31 22:12:35 +00001653
1654void
1655NetSetIP(volatile uchar * xip, IPaddr_t dest, int dport, int sport, int len)
1656{
Olav Morkene8a02772009-01-23 12:56:26 +01001657 IP_t *ip = (IP_t *)xip;
wdenk2d966952002-10-31 22:12:35 +00001658
1659 /*
1660 * If the data is an odd number of bytes, zero the
1661 * byte after the last byte so that the checksum
1662 * will work.
1663 */
1664 if (len & 1)
1665 xip[IP_HDR_SIZE + len] = 0;
1666
1667 /*
1668 * Construct an IP and UDP header.
wdenk05939202004-04-18 17:39:38 +00001669 * (need to set no fragment bit - XXX)
wdenk2d966952002-10-31 22:12:35 +00001670 */
1671 ip->ip_hl_v = 0x45; /* IP_HDR_SIZE / 4 (not including UDP) */
1672 ip->ip_tos = 0;
1673 ip->ip_len = htons(IP_HDR_SIZE + len);
1674 ip->ip_id = htons(NetIPID++);
Peter Tyser0885bf02008-12-01 16:26:20 -06001675 ip->ip_off = htons(IP_FLAGS_DFRAG); /* Don't fragment */
wdenk2d966952002-10-31 22:12:35 +00001676 ip->ip_ttl = 255;
1677 ip->ip_p = 17; /* UDP */
1678 ip->ip_sum = 0;
1679 NetCopyIP((void*)&ip->ip_src, &NetOurIP); /* already in network byte order */
1680 NetCopyIP((void*)&ip->ip_dst, &dest); /* - "" - */
1681 ip->udp_src = htons(sport);
1682 ip->udp_dst = htons(dport);
1683 ip->udp_len = htons(8 + len);
1684 ip->udp_xsum = 0;
1685 ip->ip_sum = ~NetCksum((uchar *)ip, IP_HDR_SIZE_NO_UDP / 2);
1686}
1687
Wolfgang Denk7fb52662005-10-13 16:45:02 +02001688void copy_filename (char *dst, char *src, int size)
wdenk2d966952002-10-31 22:12:35 +00001689{
1690 if (*src && (*src == '"')) {
1691 ++src;
1692 --size;
1693 }
1694
1695 while ((--size > 0) && *src && (*src != '"')) {
1696 *dst++ = *src++;
1697 }
1698 *dst = '\0';
1699}
1700
Jon Loeligera9807e52007-07-10 11:05:02 -05001701#endif
wdenk2d966952002-10-31 22:12:35 +00001702
Robin Getz82f0d232009-07-20 14:53:54 -04001703#if defined(CONFIG_CMD_NFS) || defined(CONFIG_CMD_SNTP) || defined(CONFIG_CMD_DNS)
1704/*
1705 * make port a little random, but use something trivial to compute
1706 */
1707unsigned int random_port(void)
1708{
1709 return 1024 + (get_timer(0) % 0x8000);;
1710}
1711#endif
1712
wdenk2d966952002-10-31 22:12:35 +00001713void ip_to_string (IPaddr_t x, char *s)
1714{
wdenk145d2c12004-04-15 21:48:45 +00001715 x = ntohl (x);
1716 sprintf (s, "%d.%d.%d.%d",
1717 (int) ((x >> 24) & 0xff),
1718 (int) ((x >> 16) & 0xff),
1719 (int) ((x >> 8) & 0xff), (int) ((x >> 0) & 0xff)
wdenk05939202004-04-18 17:39:38 +00001720 );
wdenk2d966952002-10-31 22:12:35 +00001721}
1722
wdenke6466f62003-06-05 19:27:42 +00001723IPaddr_t string_to_ip(char *s)
wdenk2d966952002-10-31 22:12:35 +00001724{
1725 IPaddr_t addr;
wdenke6466f62003-06-05 19:27:42 +00001726 char *e;
wdenk2d966952002-10-31 22:12:35 +00001727 int i;
1728
wdenke6466f62003-06-05 19:27:42 +00001729 if (s == NULL)
1730 return(0);
wdenk2d966952002-10-31 22:12:35 +00001731
1732 for (addr=0, i=0; i<4; ++i) {
1733 ulong val = s ? simple_strtoul(s, &e, 10) : 0;
1734 addr <<= 8;
1735 addr |= (val & 0xFF);
1736 if (s) {
1737 s = (*e) ? e+1 : e;
1738 }
1739 }
1740
1741 return (htonl(addr));
1742}
wdenke6466f62003-06-05 19:27:42 +00001743
wdenk145d2c12004-04-15 21:48:45 +00001744void VLAN_to_string(ushort x, char *s)
1745{
1746 x = ntohs(x);
1747
1748 if (x == (ushort)-1)
1749 x = VLAN_NONE;
1750
1751 if (x == VLAN_NONE)
1752 strcpy(s, "none");
1753 else
1754 sprintf(s, "%d", x & VLAN_IDMASK);
1755}
1756
1757ushort string_to_VLAN(char *s)
1758{
1759 ushort id;
1760
1761 if (s == NULL)
wdenk656140b2004-04-25 13:18:40 +00001762 return htons(VLAN_NONE);
wdenk145d2c12004-04-15 21:48:45 +00001763
1764 if (*s < '0' || *s > '9')
1765 id = VLAN_NONE;
1766 else
1767 id = (ushort)simple_strtoul(s, NULL, 10);
1768
wdenk656140b2004-04-25 13:18:40 +00001769 return htons(id);
wdenk145d2c12004-04-15 21:48:45 +00001770}
1771
wdenke6466f62003-06-05 19:27:42 +00001772IPaddr_t getenv_IPaddr (char *var)
1773{
1774 return (string_to_ip(getenv(var)));
1775}
wdenk145d2c12004-04-15 21:48:45 +00001776
1777ushort getenv_VLAN(char *var)
1778{
1779 return (string_to_VLAN(getenv(var)));
1780}