blob: a6096324126ada55e2f30afe023d376f6720ff16 [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"
Peter Tyserf7a48ca2010-09-30 11:25:48 -050083#ifdef CONFIG_CMD_RARP
wdenk2d966952002-10-31 22:12:35 +000084#include "rarp.h"
Peter Tyserf7a48ca2010-09-30 11:25:48 -050085#endif
wdenkbe9c1cb2004-02-24 02:00:03 +000086#include "nfs.h"
wdenk49c3f672003-10-08 22:33:00 +000087#ifdef CONFIG_STATUS_LED
88#include <status_led.h>
89#include <miiphy.h>
90#endif
Jon Loeliger54f35c22007-07-09 17:45:14 -050091#if defined(CONFIG_CMD_SNTP)
wdenkb4ad9622005-04-01 00:25:43 +000092#include "sntp.h"
93#endif
Peter Tyser62948502008-11-03 09:30:59 -060094#if defined(CONFIG_CDP_VERSION)
95#include <timestamp.h>
96#endif
Robin Getz82f0d232009-07-20 14:53:54 -040097#if defined(CONFIG_CMD_DNS)
98#include "dns.h"
99#endif
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/** BOOTP EXTENTIONS **/
117
118IPaddr_t NetOurSubnetMask=0; /* Our subnet mask (0=unknown) */
119IPaddr_t NetOurGatewayIP=0; /* Our gateways IP address */
120IPaddr_t NetOurDNSIP=0; /* Our DNS IP address */
Jon Loeliger5336a762007-07-09 22:08:34 -0500121#if defined(CONFIG_BOOTP_DNS2)
stroesee0aadfb2003-08-28 14:17:32 +0000122IPaddr_t NetOurDNS2IP=0; /* Our 2nd DNS IP address */
123#endif
wdenk2d966952002-10-31 22:12:35 +0000124char NetOurNISDomain[32]={0,}; /* Our NIS domain */
125char NetOurHostName[32]={0,}; /* Our hostname */
126char NetOurRootPath[64]={0,}; /* Our bootpath */
127ushort NetBootFileSize=0; /* Our bootfile size in blocks */
128
David Updegraff7280da72007-06-11 10:41:07 -0500129#ifdef CONFIG_MCAST_TFTP /* Multicast TFTP */
130IPaddr_t Mcast_addr;
131#endif
132
wdenk2d966952002-10-31 22:12:35 +0000133/** END OF BOOTP EXTENTIONS **/
134
135ulong NetBootFileXferSize; /* The actual transferred size of the bootfile (in bytes) */
136uchar NetOurEther[6]; /* Our ethernet address */
137uchar NetServerEther[6] = /* Boot server enet address */
wdenke6466f62003-06-05 19:27:42 +0000138 { 0, 0, 0, 0, 0, 0 };
wdenk2d966952002-10-31 22:12:35 +0000139IPaddr_t NetOurIP; /* Our IP addr (0 = unknown) */
Guennadi Liakhovetskib38c2b32008-04-03 17:04:19 +0200140IPaddr_t NetServerIP; /* Server IP addr (0 = unknown) */
Mike Frysingerdc166032009-07-18 21:04:08 -0400141volatile uchar *NetRxPacket; /* Current receive packet */
142int NetRxPacketLen; /* Current rx packet length */
wdenk2d966952002-10-31 22:12:35 +0000143unsigned NetIPID; /* IP packet ID */
144uchar NetBcastAddr[6] = /* Ethernet bcast address */
145 { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
wdenke6466f62003-06-05 19:27:42 +0000146uchar NetEtherNullAddr[6] =
147 { 0, 0, 0, 0, 0, 0 };
Rafal Jaworowski1f2c9a42007-12-27 18:19:02 +0100148#ifdef CONFIG_API
149void (*push_packet)(volatile void *, int len) = 0;
150#endif
Jon Loeliger54f35c22007-07-09 17:45:14 -0500151#if defined(CONFIG_CMD_CDP)
wdenk05939202004-04-18 17:39:38 +0000152uchar NetCDPAddr[6] = /* Ethernet bcast address */
wdenk145d2c12004-04-15 21:48:45 +0000153 { 0x01, 0x00, 0x0c, 0xcc, 0xcc, 0xcc };
154#endif
wdenk2d966952002-10-31 22:12:35 +0000155int NetState; /* Network loop state */
156#ifdef CONFIG_NET_MULTI
157int NetRestartWrap = 0; /* Tried all network devices */
158static int NetRestarted = 0; /* Network loop restarted */
159static int NetDevExists = 0; /* At least one device configured */
160#endif
161
wdenk05939202004-04-18 17:39:38 +0000162/* XXX in both little & big endian machines 0xFFFF == ntohs(-1) */
163ushort NetOurVLAN = 0xFFFF; /* default is without VLAN */
164ushort NetOurNativeVLAN = 0xFFFF; /* ditto */
wdenk145d2c12004-04-15 21:48:45 +0000165
wdenk2d966952002-10-31 22:12:35 +0000166char BootFile[128]; /* Boot File name */
167
Jon Loeliger54f35c22007-07-09 17:45:14 -0500168#if defined(CONFIG_CMD_PING)
Wolfgang Denka1be4762008-05-20 16:00:29 +0200169IPaddr_t NetPingIP; /* the ip address to ping */
wdenke6466f62003-06-05 19:27:42 +0000170
171static void PingStart(void);
172#endif
173
Jon Loeliger54f35c22007-07-09 17:45:14 -0500174#if defined(CONFIG_CMD_CDP)
wdenk145d2c12004-04-15 21:48:45 +0000175static void CDPStart(void);
176#endif
177
Jon Loeliger54f35c22007-07-09 17:45:14 -0500178#if defined(CONFIG_CMD_SNTP)
wdenkb4ad9622005-04-01 00:25:43 +0000179IPaddr_t NetNtpServerIP; /* NTP server IP address */
180int NetTimeOffset=0; /* offset time from UTC */
181#endif
182
wdenkb8fb6192004-08-02 21:11:11 +0000183#ifdef CONFIG_NETCONSOLE
184void NcStart(void);
185int nc_input_packet(uchar *pkt, unsigned dest, unsigned src, unsigned len);
186#endif
187
wdenk2d966952002-10-31 22:12:35 +0000188volatile uchar PktBuf[(PKTBUFSRX+1) * PKTSIZE_ALIGN + PKTALIGN];
189
190volatile uchar *NetRxPackets[PKTBUFSRX]; /* Receive packets */
191
192static rxhand_f *packetHandler; /* Current RX packet handler */
193static thand_f *timeHandler; /* Current timeout handler */
wdenk5d841732003-08-17 18:55:18 +0000194static ulong timeStart; /* Time base value */
195static ulong timeDelta; /* Current timeout value */
wdenk2d966952002-10-31 22:12:35 +0000196volatile uchar *NetTxPacket = 0; /* THE transmit packet */
197
198static int net_check_prereq (proto_t protocol);
199
Remy Bohmer0ddb8e82009-10-28 22:13:39 +0100200static int NetTryCount;
201
wdenk2d966952002-10-31 22:12:35 +0000202/**********************************************************************/
wdenke6466f62003-06-05 19:27:42 +0000203
204IPaddr_t NetArpWaitPacketIP;
205IPaddr_t NetArpWaitReplyIP;
206uchar *NetArpWaitPacketMAC; /* MAC address of waiting packet's destination */
Wolfgang Denk30b87322005-08-12 23:43:12 +0200207uchar *NetArpWaitTxPacket; /* THE transmit packet */
wdenke6466f62003-06-05 19:27:42 +0000208int NetArpWaitTxPacketSize;
Wolfgang Denka1be4762008-05-20 16:00:29 +0200209uchar NetArpWaitPacketBuf[PKTSIZE_ALIGN + PKTALIGN];
wdenke6466f62003-06-05 19:27:42 +0000210ulong NetArpWaitTimerStart;
211int NetArpWaitTry;
212
wdenk05939202004-04-18 17:39:38 +0000213void ArpRequest (void)
wdenke6466f62003-06-05 19:27:42 +0000214{
215 int i;
216 volatile uchar *pkt;
wdenk05939202004-04-18 17:39:38 +0000217 ARP_t *arp;
wdenke6466f62003-06-05 19:27:42 +0000218
Robin Getz9e0a4d62009-07-23 03:01:03 -0400219 debug("ARP broadcast %d\n", NetArpWaitTry);
220
wdenke6466f62003-06-05 19:27:42 +0000221 pkt = NetTxPacket;
222
wdenk05939202004-04-18 17:39:38 +0000223 pkt += NetSetEther (pkt, NetBcastAddr, PROT_ARP);
wdenke6466f62003-06-05 19:27:42 +0000224
wdenk05939202004-04-18 17:39:38 +0000225 arp = (ARP_t *) pkt;
wdenke6466f62003-06-05 19:27:42 +0000226
wdenk05939202004-04-18 17:39:38 +0000227 arp->ar_hrd = htons (ARP_ETHER);
228 arp->ar_pro = htons (PROT_IP);
wdenke6466f62003-06-05 19:27:42 +0000229 arp->ar_hln = 6;
230 arp->ar_pln = 4;
wdenk05939202004-04-18 17:39:38 +0000231 arp->ar_op = htons (ARPOP_REQUEST);
wdenke6466f62003-06-05 19:27:42 +0000232
Wolfgang Denk30b87322005-08-12 23:43:12 +0200233 memcpy (&arp->ar_data[0], NetOurEther, 6); /* source ET addr */
234 NetWriteIP ((uchar *) & arp->ar_data[6], NetOurIP); /* source IP addr */
wdenk05939202004-04-18 17:39:38 +0000235 for (i = 10; i < 16; ++i) {
236 arp->ar_data[i] = 0; /* dest ET addr = 0 */
wdenke6466f62003-06-05 19:27:42 +0000237 }
238
wdenk05939202004-04-18 17:39:38 +0000239 if ((NetArpWaitPacketIP & NetOurSubnetMask) !=
240 (NetOurIP & NetOurSubnetMask)) {
241 if (NetOurGatewayIP == 0) {
242 puts ("## Warning: gatewayip needed but not set\n");
Wolfgang Denk98ceffb2006-03-12 01:13:30 +0100243 NetArpWaitReplyIP = NetArpWaitPacketIP;
244 } else {
245 NetArpWaitReplyIP = NetOurGatewayIP;
wdenk05939202004-04-18 17:39:38 +0000246 }
wdenk05939202004-04-18 17:39:38 +0000247 } else {
248 NetArpWaitReplyIP = NetArpWaitPacketIP;
249 }
wdenke6466f62003-06-05 19:27:42 +0000250
wdenk05939202004-04-18 17:39:38 +0000251 NetWriteIP ((uchar *) & arp->ar_data[16], NetArpWaitReplyIP);
252 (void) eth_send (NetTxPacket, (pkt - NetTxPacket) + ARP_HDR_SIZE);
wdenke6466f62003-06-05 19:27:42 +0000253}
254
255void ArpTimeoutCheck(void)
256{
257 ulong t;
258
259 if (!NetArpWaitPacketIP)
260 return;
261
262 t = get_timer(0);
263
264 /* check for arp timeout */
Bartlomiej Sieka56668462008-10-01 15:26:28 +0200265 if ((t - NetArpWaitTimerStart) > ARP_TIMEOUT) {
wdenke6466f62003-06-05 19:27:42 +0000266 NetArpWaitTry++;
267
268 if (NetArpWaitTry >= ARP_TIMEOUT_COUNT) {
269 puts ("\nARP Retry count exceeded; starting again\n");
270 NetArpWaitTry = 0;
271 NetStartAgain();
272 } else {
273 NetArpWaitTimerStart = t;
274 ArpRequest();
275 }
276 }
277}
278
Heiko Schocher3b195ff2009-04-28 08:36:11 +0200279static void
Heiko Schocher0c303fa2009-02-10 09:38:52 +0100280NetInitLoop(proto_t protocol)
281{
Heiko Schocher3b195ff2009-04-28 08:36:11 +0200282 static int env_changed_id = 0;
Heiko Schocher0c303fa2009-02-10 09:38:52 +0100283 bd_t *bd = gd->bd;
284 int env_id = get_env_id ();
285
286 /* update only when the environment has changed */
Michael Zaidmanb97bfe42009-04-04 01:43:00 +0300287 if (env_changed_id != env_id) {
Heiko Schocher0c303fa2009-02-10 09:38:52 +0100288 NetCopyIP(&NetOurIP, &bd->bi_ip_addr);
289 NetOurGatewayIP = getenv_IPaddr ("gatewayip");
290 NetOurSubnetMask= getenv_IPaddr ("netmask");
Heiko Schocher0c303fa2009-02-10 09:38:52 +0100291 NetServerIP = getenv_IPaddr ("serverip");
Heiko Schocher0c303fa2009-02-10 09:38:52 +0100292 NetOurNativeVLAN = getenv_VLAN("nvlan");
Michael Zaidmanb97bfe42009-04-04 01:43:00 +0300293 NetOurVLAN = getenv_VLAN("vlan");
Robin Getz82f0d232009-07-20 14:53:54 -0400294#if defined(CONFIG_CMD_DNS)
295 NetOurDNSIP = getenv_IPaddr("dnsip");
296#endif
Michael Zaidmanb97bfe42009-04-04 01:43:00 +0300297 env_changed_id = env_id;
Heiko Schocher0c303fa2009-02-10 09:38:52 +0100298 }
Michael Zaidmanb97bfe42009-04-04 01:43:00 +0300299
Heiko Schocher3b195ff2009-04-28 08:36:11 +0200300 return;
Heiko Schocher0c303fa2009-02-10 09:38:52 +0100301}
302
wdenke6466f62003-06-05 19:27:42 +0000303/**********************************************************************/
wdenk2d966952002-10-31 22:12:35 +0000304/*
305 * Main network processing loop.
306 */
307
308int
309NetLoop(proto_t protocol)
310{
wdenk2d966952002-10-31 22:12:35 +0000311 bd_t *bd = gd->bd;
312
313#ifdef CONFIG_NET_MULTI
314 NetRestarted = 0;
315 NetDevExists = 0;
316#endif
317
wdenke6466f62003-06-05 19:27:42 +0000318 /* XXX problem with bss workaround */
319 NetArpWaitPacketMAC = NULL;
320 NetArpWaitTxPacket = NULL;
321 NetArpWaitPacketIP = 0;
322 NetArpWaitReplyIP = 0;
323 NetArpWaitTxPacket = NULL;
324 NetTxPacket = NULL;
Remy Bohmer0ddb8e82009-10-28 22:13:39 +0100325 NetTryCount = 1;
wdenke6466f62003-06-05 19:27:42 +0000326
wdenk2d966952002-10-31 22:12:35 +0000327 if (!NetTxPacket) {
328 int i;
wdenk2d966952002-10-31 22:12:35 +0000329 /*
330 * Setup packet buffers, aligned correctly.
331 */
332 NetTxPacket = &PktBuf[0] + (PKTALIGN - 1);
333 NetTxPacket -= (ulong)NetTxPacket % PKTALIGN;
334 for (i = 0; i < PKTBUFSRX; i++) {
335 NetRxPackets[i] = NetTxPacket + (i+1)*PKTSIZE_ALIGN;
336 }
wdenke6466f62003-06-05 19:27:42 +0000337 }
338
339 if (!NetArpWaitTxPacket) {
340 NetArpWaitTxPacket = &NetArpWaitPacketBuf[0] + (PKTALIGN - 1);
341 NetArpWaitTxPacket -= (ulong)NetArpWaitTxPacket % PKTALIGN;
342 NetArpWaitTxPacketSize = 0;
wdenk2d966952002-10-31 22:12:35 +0000343 }
344
345 eth_halt();
wdenk05939202004-04-18 17:39:38 +0000346#ifdef CONFIG_NET_MULTI
wdenk145d2c12004-04-15 21:48:45 +0000347 eth_set_current();
wdenk05939202004-04-18 17:39:38 +0000348#endif
wdenkfa66e932005-04-03 14:52:59 +0000349 if (eth_init(bd) < 0) {
350 eth_halt();
wdenk05939202004-04-18 17:39:38 +0000351 return(-1);
wdenkfa66e932005-04-03 14:52:59 +0000352 }
wdenk2d966952002-10-31 22:12:35 +0000353
354restart:
355#ifdef CONFIG_NET_MULTI
356 memcpy (NetOurEther, eth_get_dev()->enetaddr, 6);
357#else
Mike Frysinger79ca1b92009-02-11 18:23:48 -0500358 eth_getenv_enetaddr("ethaddr", NetOurEther);
wdenk2d966952002-10-31 22:12:35 +0000359#endif
360
361 NetState = NETLOOP_CONTINUE;
362
363 /*
364 * Start the ball rolling with the given start function. From
365 * here on, this code is a state machine driven by received
366 * packets and timer events.
367 */
Heiko Schocher0c303fa2009-02-10 09:38:52 +0100368 NetInitLoop(protocol);
wdenk2d966952002-10-31 22:12:35 +0000369
370 switch (net_check_prereq (protocol)) {
371 case 1:
372 /* network not configured */
wdenkfa66e932005-04-03 14:52:59 +0000373 eth_halt();
wdenk2582f6b2002-11-11 21:14:20 +0000374 return (-1);
wdenk2d966952002-10-31 22:12:35 +0000375
376#ifdef CONFIG_NET_MULTI
377 case 2:
378 /* network device not configured */
379 break;
380#endif /* CONFIG_NET_MULTI */
381
382 case 0:
383#ifdef CONFIG_NET_MULTI
384 NetDevExists = 1;
385#endif
386 switch (protocol) {
387 case TFTP:
388 /* always use ARP to get server ethernet address */
wdenke6466f62003-06-05 19:27:42 +0000389 TftpStart();
wdenk2d966952002-10-31 22:12:35 +0000390 break;
391
Jon Loeliger54f35c22007-07-09 17:45:14 -0500392#if defined(CONFIG_CMD_DHCP)
wdenk2d966952002-10-31 22:12:35 +0000393 case DHCP:
wdenkc3919532004-10-11 22:51:13 +0000394 BootpTry = 0;
Michael Zaidman16bb21d2009-07-14 23:37:12 +0300395 NetOurIP = 0;
wdenk2d966952002-10-31 22:12:35 +0000396 DhcpRequest(); /* Basically same as BOOTP */
397 break;
Jon Loeligera9807e52007-07-10 11:05:02 -0500398#endif
wdenk2d966952002-10-31 22:12:35 +0000399
400 case BOOTP:
401 BootpTry = 0;
Michael Zaidman16bb21d2009-07-14 23:37:12 +0300402 NetOurIP = 0;
wdenk2d966952002-10-31 22:12:35 +0000403 BootpRequest ();
404 break;
405
Peter Tyserf7a48ca2010-09-30 11:25:48 -0500406#if defined(CONFIG_CMD_RARP)
wdenk2d966952002-10-31 22:12:35 +0000407 case RARP:
408 RarpTry = 0;
Michael Zaidman16bb21d2009-07-14 23:37:12 +0300409 NetOurIP = 0;
wdenk2d966952002-10-31 22:12:35 +0000410 RarpRequest ();
411 break;
Peter Tyserf7a48ca2010-09-30 11:25:48 -0500412#endif
Jon Loeliger54f35c22007-07-09 17:45:14 -0500413#if defined(CONFIG_CMD_PING)
wdenke6466f62003-06-05 19:27:42 +0000414 case PING:
415 PingStart();
416 break;
417#endif
Jon Loeliger54f35c22007-07-09 17:45:14 -0500418#if defined(CONFIG_CMD_NFS)
wdenkbe9c1cb2004-02-24 02:00:03 +0000419 case NFS:
420 NfsStart();
421 break;
422#endif
Jon Loeliger54f35c22007-07-09 17:45:14 -0500423#if defined(CONFIG_CMD_CDP)
wdenk145d2c12004-04-15 21:48:45 +0000424 case CDP:
425 CDPStart();
426 break;
427#endif
wdenkb8fb6192004-08-02 21:11:11 +0000428#ifdef CONFIG_NETCONSOLE
429 case NETCONS:
430 NcStart();
431 break;
432#endif
Jon Loeliger54f35c22007-07-09 17:45:14 -0500433#if defined(CONFIG_CMD_SNTP)
wdenkb4ad9622005-04-01 00:25:43 +0000434 case SNTP:
435 SntpStart();
436 break;
437#endif
Robin Getz82f0d232009-07-20 14:53:54 -0400438#if defined(CONFIG_CMD_DNS)
439 case DNS:
440 DnsStart();
441 break;
442#endif
wdenk2d966952002-10-31 22:12:35 +0000443 default:
444 break;
445 }
446
447 NetBootFileXferSize = 0;
448 break;
449 }
450
Jon Loeliger54f35c22007-07-09 17:45:14 -0500451#if defined(CONFIG_MII) || defined(CONFIG_CMD_MII)
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200452#if defined(CONFIG_SYS_FAULT_ECHO_LINK_DOWN) && defined(CONFIG_STATUS_LED) && defined(STATUS_LED_RED)
wdenk49c3f672003-10-08 22:33:00 +0000453 /*
wdenk9c53f402003-10-15 23:53:47 +0000454 * Echo the inverted link state to the fault LED.
wdenk49c3f672003-10-08 22:33:00 +0000455 */
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200456 if(miiphy_link(eth_get_dev()->name, CONFIG_SYS_FAULT_MII_ADDR)) {
wdenk49c3f672003-10-08 22:33:00 +0000457 status_led_set (STATUS_LED_RED, STATUS_LED_OFF);
458 } else {
459 status_led_set (STATUS_LED_RED, STATUS_LED_ON);
460 }
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200461#endif /* CONFIG_SYS_FAULT_ECHO_LINK_DOWN, ... */
wdenk49c3f672003-10-08 22:33:00 +0000462#endif /* CONFIG_MII, ... */
wdenk2d966952002-10-31 22:12:35 +0000463
464 /*
465 * Main packet reception loop. Loop receiving packets until
wdenk27fa5852005-04-03 14:18:51 +0000466 * someone sets `NetState' to a state that terminates.
wdenk2d966952002-10-31 22:12:35 +0000467 */
468 for (;;) {
469 WATCHDOG_RESET();
470#ifdef CONFIG_SHOW_ACTIVITY
471 {
472 extern void show_activity(int arg);
473 show_activity(1);
474 }
475#endif
476 /*
477 * Check the ethernet for a new packet. The ethernet
478 * receive routine will process it.
479 */
Guennadi Liakhovetskib38c2b32008-04-03 17:04:19 +0200480 eth_rx();
wdenk2d966952002-10-31 22:12:35 +0000481
482 /*
483 * Abort if ctrl-c was pressed.
484 */
485 if (ctrlc()) {
wdenk57b2d802003-06-27 21:31:46 +0000486 eth_halt();
wdenk42c05472004-03-23 22:14:11 +0000487 puts ("\nAbort\n");
wdenk2582f6b2002-11-11 21:14:20 +0000488 return (-1);
wdenk2d966952002-10-31 22:12:35 +0000489 }
490
wdenke6466f62003-06-05 19:27:42 +0000491 ArpTimeoutCheck();
wdenk2d966952002-10-31 22:12:35 +0000492
493 /*
494 * Check for a timeout, and run the timeout handler
495 * if we have one.
496 */
wdenk5d841732003-08-17 18:55:18 +0000497 if (timeHandler && ((get_timer(0) - timeStart) > timeDelta)) {
wdenk2d966952002-10-31 22:12:35 +0000498 thand_f *x;
499
Jon Loeliger54f35c22007-07-09 17:45:14 -0500500#if defined(CONFIG_MII) || defined(CONFIG_CMD_MII)
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200501# if defined(CONFIG_SYS_FAULT_ECHO_LINK_DOWN) && \
wdenk6148e742005-04-03 20:55:38 +0000502 defined(CONFIG_STATUS_LED) && \
wdenk27fa5852005-04-03 14:18:51 +0000503 defined(STATUS_LED_RED)
wdenk49c3f672003-10-08 22:33:00 +0000504 /*
wdenk9c53f402003-10-15 23:53:47 +0000505 * Echo the inverted link state to the fault LED.
wdenk49c3f672003-10-08 22:33:00 +0000506 */
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200507 if(miiphy_link(eth_get_dev()->name, CONFIG_SYS_FAULT_MII_ADDR)) {
wdenk49c3f672003-10-08 22:33:00 +0000508 status_led_set (STATUS_LED_RED, STATUS_LED_OFF);
509 } else {
510 status_led_set (STATUS_LED_RED, STATUS_LED_ON);
511 }
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200512# endif /* CONFIG_SYS_FAULT_ECHO_LINK_DOWN, ... */
wdenk49c3f672003-10-08 22:33:00 +0000513#endif /* CONFIG_MII, ... */
wdenk2d966952002-10-31 22:12:35 +0000514 x = timeHandler;
515 timeHandler = (thand_f *)0;
516 (*x)();
517 }
518
519
520 switch (NetState) {
521
522 case NETLOOP_RESTART:
523#ifdef CONFIG_NET_MULTI
524 NetRestarted = 1;
525#endif
526 goto restart;
527
528 case NETLOOP_SUCCESS:
529 if (NetBootFileXferSize > 0) {
Wolfgang Denk084d0ce2007-09-12 00:48:57 +0200530 char buf[20];
wdenk2d966952002-10-31 22:12:35 +0000531 printf("Bytes transferred = %ld (%lx hex)\n",
532 NetBootFileXferSize,
533 NetBootFileXferSize);
Wolfgang Denk084d0ce2007-09-12 00:48:57 +0200534 sprintf(buf, "%lX", NetBootFileXferSize);
wdenk2d966952002-10-31 22:12:35 +0000535 setenv("filesize", buf);
wdenk145d2c12004-04-15 21:48:45 +0000536
537 sprintf(buf, "%lX", (unsigned long)load_addr);
538 setenv("fileaddr", buf);
wdenk2d966952002-10-31 22:12:35 +0000539 }
540 eth_halt();
541 return NetBootFileXferSize;
542
543 case NETLOOP_FAIL:
wdenk2582f6b2002-11-11 21:14:20 +0000544 return (-1);
wdenk2d966952002-10-31 22:12:35 +0000545 }
546 }
547}
548
549/**********************************************************************/
550
551static void
552startAgainTimeout(void)
553{
554 NetState = NETLOOP_RESTART;
555}
556
557static void
558startAgainHandler(uchar * pkt, unsigned dest, unsigned src, unsigned len)
559{
560 /* Totally ignore the packet */
561}
562
wdenk05939202004-04-18 17:39:38 +0000563void NetStartAgain (void)
wdenk2d966952002-10-31 22:12:35 +0000564{
wdenk05939202004-04-18 17:39:38 +0000565 char *nretry;
Remy Bohmer0ddb8e82009-10-28 22:13:39 +0100566 int retry_forever = 0;
567 unsigned long retrycnt = 0;
wdenk145d2c12004-04-15 21:48:45 +0000568
Remy Bohmer0ddb8e82009-10-28 22:13:39 +0100569 nretry = getenv("netretry");
570 if (nretry) {
571 if (!strcmp(nretry, "yes"))
572 retry_forever = 1;
573 else if (!strcmp(nretry, "no"))
574 retrycnt = 0;
575 else if (!strcmp(nretry, "once"))
576 retrycnt = 1;
577 else
578 retrycnt = simple_strtoul(nretry, NULL, 0);
579 } else
580 retry_forever = 1;
581
582 if ((!retry_forever) && (NetTryCount >= retrycnt)) {
583 eth_halt();
wdenk145d2c12004-04-15 21:48:45 +0000584 NetState = NETLOOP_FAIL;
585 return;
586 }
Remy Bohmer0ddb8e82009-10-28 22:13:39 +0100587
588 NetTryCount++;
589
wdenk2d966952002-10-31 22:12:35 +0000590#ifndef CONFIG_NET_MULTI
Bartlomiej Sieka56668462008-10-01 15:26:28 +0200591 NetSetTimeout (10000UL, startAgainTimeout);
wdenk05939202004-04-18 17:39:38 +0000592 NetSetHandler (startAgainHandler);
593#else /* !CONFIG_NET_MULTI*/
594 eth_halt ();
Matthias Fuchsa02d62b2007-12-27 16:58:41 +0100595#if !defined(CONFIG_NET_DO_NOT_TRY_ANOTHER)
wdenk05939202004-04-18 17:39:38 +0000596 eth_try_another (!NetRestarted);
Matthias Fuchsa02d62b2007-12-27 16:58:41 +0100597#endif
wdenk05939202004-04-18 17:39:38 +0000598 eth_init (gd->bd);
599 if (NetRestartWrap) {
wdenk2d966952002-10-31 22:12:35 +0000600 NetRestartWrap = 0;
Remy Bohmer0ddb8e82009-10-28 22:13:39 +0100601 if (NetDevExists) {
Bartlomiej Sieka56668462008-10-01 15:26:28 +0200602 NetSetTimeout (10000UL, startAgainTimeout);
wdenk05939202004-04-18 17:39:38 +0000603 NetSetHandler (startAgainHandler);
604 } else {
wdenk2d966952002-10-31 22:12:35 +0000605 NetState = NETLOOP_FAIL;
606 }
wdenk05939202004-04-18 17:39:38 +0000607 } else {
wdenk2d966952002-10-31 22:12:35 +0000608 NetState = NETLOOP_RESTART;
609 }
wdenk05939202004-04-18 17:39:38 +0000610#endif /* CONFIG_NET_MULTI */
wdenk2d966952002-10-31 22:12:35 +0000611}
612
613/**********************************************************************/
614/*
615 * Miscelaneous bits.
616 */
617
618void
619NetSetHandler(rxhand_f * f)
620{
621 packetHandler = f;
622}
623
624
625void
wdenkd6a3dd42004-10-09 21:56:21 +0000626NetSetTimeout(ulong iv, thand_f * f)
wdenk2d966952002-10-31 22:12:35 +0000627{
628 if (iv == 0) {
629 timeHandler = (thand_f *)0;
630 } else {
631 timeHandler = f;
wdenk5d841732003-08-17 18:55:18 +0000632 timeStart = get_timer(0);
633 timeDelta = iv;
wdenk2d966952002-10-31 22:12:35 +0000634 }
635}
636
637
638void
639NetSendPacket(volatile uchar * pkt, int len)
640{
641 (void) eth_send(pkt, len);
642}
643
wdenke6466f62003-06-05 19:27:42 +0000644int
645NetSendUDPPacket(uchar *ether, IPaddr_t dest, int dport, int sport, int len)
646{
wdenk145d2c12004-04-15 21:48:45 +0000647 uchar *pkt;
648
wdenke6466f62003-06-05 19:27:42 +0000649 /* convert to new style broadcast */
650 if (dest == 0)
651 dest = 0xFFFFFFFF;
652
653 /* if broadcast, make the ether address a broadcast and don't do ARP */
654 if (dest == 0xFFFFFFFF)
655 ether = NetBcastAddr;
656
657 /* if MAC address was not discovered yet, save the packet and do an ARP request */
658 if (memcmp(ether, NetEtherNullAddr, 6) == 0) {
659
Robin Getz9e0a4d62009-07-23 03:01:03 -0400660 debug("sending ARP for %08lx\n", dest);
661
wdenke6466f62003-06-05 19:27:42 +0000662 NetArpWaitPacketIP = dest;
663 NetArpWaitPacketMAC = ether;
wdenk145d2c12004-04-15 21:48:45 +0000664
665 pkt = NetArpWaitTxPacket;
666 pkt += NetSetEther (pkt, NetArpWaitPacketMAC, PROT_IP);
667
668 NetSetIP (pkt, dest, dport, sport, len);
669 memcpy(pkt + IP_HDR_SIZE, (uchar *)NetTxPacket + (pkt - (uchar *)NetArpWaitTxPacket) + IP_HDR_SIZE, len);
wdenke6466f62003-06-05 19:27:42 +0000670
671 /* size of the waiting packet */
wdenk145d2c12004-04-15 21:48:45 +0000672 NetArpWaitTxPacketSize = (pkt - NetArpWaitTxPacket) + IP_HDR_SIZE + len;
wdenke6466f62003-06-05 19:27:42 +0000673
674 /* and do the ARP request */
675 NetArpWaitTry = 1;
676 NetArpWaitTimerStart = get_timer(0);
677 ArpRequest();
678 return 1; /* waiting */
679 }
680
Robin Getz9e0a4d62009-07-23 03:01:03 -0400681 debug("sending UDP to %08lx/%pM\n", dest, ether);
wdenke6466f62003-06-05 19:27:42 +0000682
wdenk145d2c12004-04-15 21:48:45 +0000683 pkt = (uchar *)NetTxPacket;
684 pkt += NetSetEther (pkt, ether, PROT_IP);
685 NetSetIP (pkt, dest, dport, sport, len);
686 (void) eth_send(NetTxPacket, (pkt - NetTxPacket) + IP_HDR_SIZE + len);
wdenke6466f62003-06-05 19:27:42 +0000687
wdenk05939202004-04-18 17:39:38 +0000688 return 0; /* transmitted */
wdenke6466f62003-06-05 19:27:42 +0000689}
690
Jon Loeliger54f35c22007-07-09 17:45:14 -0500691#if defined(CONFIG_CMD_PING)
wdenke6466f62003-06-05 19:27:42 +0000692static ushort PingSeqNo;
693
694int PingSend(void)
695{
696 static uchar mac[6];
697 volatile IP_t *ip;
698 volatile ushort *s;
wdenk145d2c12004-04-15 21:48:45 +0000699 uchar *pkt;
wdenke6466f62003-06-05 19:27:42 +0000700
701 /* XXX always send arp request */
702
703 memcpy(mac, NetEtherNullAddr, 6);
704
Robin Getz9e0a4d62009-07-23 03:01:03 -0400705 debug("sending ARP for %08lx\n", NetPingIP);
wdenke6466f62003-06-05 19:27:42 +0000706
707 NetArpWaitPacketIP = NetPingIP;
708 NetArpWaitPacketMAC = mac;
709
wdenk145d2c12004-04-15 21:48:45 +0000710 pkt = NetArpWaitTxPacket;
711 pkt += NetSetEther(pkt, mac, PROT_IP);
wdenke6466f62003-06-05 19:27:42 +0000712
wdenk145d2c12004-04-15 21:48:45 +0000713 ip = (volatile IP_t *)pkt;
wdenke6466f62003-06-05 19:27:42 +0000714
715 /*
716 * Construct an IP and ICMP header. (need to set no fragment bit - XXX)
717 */
718 ip->ip_hl_v = 0x45; /* IP_HDR_SIZE / 4 (not including UDP) */
719 ip->ip_tos = 0;
720 ip->ip_len = htons(IP_HDR_SIZE_NO_UDP + 8);
721 ip->ip_id = htons(NetIPID++);
Peter Tyser0885bf02008-12-01 16:26:20 -0600722 ip->ip_off = htons(IP_FLAGS_DFRAG); /* Don't fragment */
wdenke6466f62003-06-05 19:27:42 +0000723 ip->ip_ttl = 255;
724 ip->ip_p = 0x01; /* ICMP */
725 ip->ip_sum = 0;
726 NetCopyIP((void*)&ip->ip_src, &NetOurIP); /* already in network byte order */
727 NetCopyIP((void*)&ip->ip_dst, &NetPingIP); /* - "" - */
728 ip->ip_sum = ~NetCksum((uchar *)ip, IP_HDR_SIZE_NO_UDP / 2);
729
730 s = &ip->udp_src; /* XXX ICMP starts here */
731 s[0] = htons(0x0800); /* echo-request, code */
732 s[1] = 0; /* checksum */
Wolfgang Denka1be4762008-05-20 16:00:29 +0200733 s[2] = 0; /* identifier */
wdenke6466f62003-06-05 19:27:42 +0000734 s[3] = htons(PingSeqNo++); /* sequence number */
735 s[1] = ~NetCksum((uchar *)s, 8/2);
736
737 /* size of the waiting packet */
wdenk145d2c12004-04-15 21:48:45 +0000738 NetArpWaitTxPacketSize = (pkt - NetArpWaitTxPacket) + IP_HDR_SIZE_NO_UDP + 8;
wdenke6466f62003-06-05 19:27:42 +0000739
740 /* and do the ARP request */
741 NetArpWaitTry = 1;
742 NetArpWaitTimerStart = get_timer(0);
743 ArpRequest();
744 return 1; /* waiting */
745}
746
747static void
748PingTimeout (void)
749{
750 eth_halt();
751 NetState = NETLOOP_FAIL; /* we did not get the reply */
752}
753
754static void
755PingHandler (uchar * pkt, unsigned dest, unsigned src, unsigned len)
756{
757 IPaddr_t tmp;
758 volatile IP_t *ip = (volatile IP_t *)pkt;
759
760 tmp = NetReadIP((void *)&ip->ip_src);
761 if (tmp != NetPingIP)
762 return;
763
764 NetState = NETLOOP_SUCCESS;
765}
766
767static void PingStart(void)
768{
wdenk145d2c12004-04-15 21:48:45 +0000769#if defined(CONFIG_NET_MULTI)
770 printf ("Using %s device\n", eth_get_name());
wdenk05939202004-04-18 17:39:38 +0000771#endif /* CONFIG_NET_MULTI */
Bartlomiej Sieka56668462008-10-01 15:26:28 +0200772 NetSetTimeout (10000UL, PingTimeout);
wdenke6466f62003-06-05 19:27:42 +0000773 NetSetHandler (PingHandler);
774
775 PingSend();
776}
Jon Loeligera9807e52007-07-10 11:05:02 -0500777#endif
wdenk145d2c12004-04-15 21:48:45 +0000778
Jon Loeliger54f35c22007-07-09 17:45:14 -0500779#if defined(CONFIG_CMD_CDP)
wdenk145d2c12004-04-15 21:48:45 +0000780
781#define CDP_DEVICE_ID_TLV 0x0001
782#define CDP_ADDRESS_TLV 0x0002
783#define CDP_PORT_ID_TLV 0x0003
784#define CDP_CAPABILITIES_TLV 0x0004
785#define CDP_VERSION_TLV 0x0005
786#define CDP_PLATFORM_TLV 0x0006
787#define CDP_NATIVE_VLAN_TLV 0x000a
788#define CDP_APPLIANCE_VLAN_TLV 0x000e
789#define CDP_TRIGGER_TLV 0x000f
790#define CDP_POWER_CONSUMPTION_TLV 0x0010
791#define CDP_SYSNAME_TLV 0x0014
792#define CDP_SYSOBJECT_TLV 0x0015
793#define CDP_MANAGEMENT_ADDRESS_TLV 0x0016
794
Bartlomiej Sieka56668462008-10-01 15:26:28 +0200795#define CDP_TIMEOUT 250UL /* one packet every 250ms */
wdenk145d2c12004-04-15 21:48:45 +0000796
797static int CDPSeq;
798static int CDPOK;
799
800ushort CDPNativeVLAN;
801ushort CDPApplianceVLAN;
802
803static const uchar CDP_SNAP_hdr[8] = { 0xAA, 0xAA, 0x03, 0x00, 0x00, 0x0C, 0x20, 0x00 };
804
805static ushort CDP_compute_csum(const uchar *buff, ushort len)
806{
807 ushort csum;
808 int odd;
809 ulong result = 0;
810 ushort leftover;
Wolfgang Denk7fb52662005-10-13 16:45:02 +0200811 ushort *p;
wdenk145d2c12004-04-15 21:48:45 +0000812
813 if (len > 0) {
814 odd = 1 & (ulong)buff;
815 if (odd) {
816 result = *buff << 8;
817 len--;
818 buff++;
819 }
820 while (len > 1) {
Wolfgang Denk7fb52662005-10-13 16:45:02 +0200821 p = (ushort *)buff;
822 result += *p++;
823 buff = (uchar *)p;
wdenk145d2c12004-04-15 21:48:45 +0000824 if (result & 0x80000000)
825 result = (result & 0xFFFF) + (result >> 16);
826 len -= 2;
827 }
828 if (len) {
829 leftover = (signed short)(*(const signed char *)buff);
Wolfgang Denkf0711132005-11-10 20:59:46 +0100830 /* CISCO SUCKS big time! (and blows too):
831 * CDP uses the IP checksum algorithm with a twist;
832 * for the last byte it *sign* extends and sums.
833 */
wdenk145d2c12004-04-15 21:48:45 +0000834 result = (result & 0xffff0000) | ((result + leftover) & 0x0000ffff);
835 }
836 while (result >> 16)
837 result = (result & 0xFFFF) + (result >> 16);
838
839 if (odd)
840 result = ((result >> 8) & 0xff) | ((result & 0xff) << 8);
841 }
842
843 /* add up 16-bit and 17-bit words for 17+c bits */
844 result = (result & 0xffff) + (result >> 16);
845 /* add up 16-bit and 2-bit for 16+c bit */
846 result = (result & 0xffff) + (result >> 16);
847 /* add up carry.. */
848 result = (result & 0xffff) + (result >> 16);
849
850 /* negate */
851 csum = ~(ushort)result;
852
853 /* run time endian detection */
854 if (csum != htons(csum)) /* little endian */
855 csum = htons(csum);
856
857 return csum;
858}
859
860int CDPSendTrigger(void)
861{
862 volatile uchar *pkt;
863 volatile ushort *s;
864 volatile ushort *cp;
865 Ethernet_t *et;
wdenk145d2c12004-04-15 21:48:45 +0000866 int len;
867 ushort chksum;
wdenk05939202004-04-18 17:39:38 +0000868#if defined(CONFIG_CDP_DEVICE_ID) || defined(CONFIG_CDP_PORT_ID) || \
869 defined(CONFIG_CDP_VERSION) || defined(CONFIG_CDP_PLATFORM)
870 char buf[32];
871#endif
wdenk145d2c12004-04-15 21:48:45 +0000872
873 pkt = NetTxPacket;
874 et = (Ethernet_t *)pkt;
875
876 /* NOTE: trigger sent not on any VLAN */
877
878 /* form ethernet header */
879 memcpy(et->et_dest, NetCDPAddr, 6);
880 memcpy(et->et_src, NetOurEther, 6);
881
882 pkt += ETHER_HDR_SIZE;
883
884 /* SNAP header */
885 memcpy((uchar *)pkt, CDP_SNAP_hdr, sizeof(CDP_SNAP_hdr));
886 pkt += sizeof(CDP_SNAP_hdr);
887
888 /* CDP header */
889 *pkt++ = 0x02; /* CDP version 2 */
890 *pkt++ = 180; /* TTL */
891 s = (volatile ushort *)pkt;
892 cp = s;
893 *s++ = htons(0); /* checksum (0 for later calculation) */
894
895 /* CDP fields */
896#ifdef CONFIG_CDP_DEVICE_ID
897 *s++ = htons(CDP_DEVICE_ID_TLV);
898 *s++ = htons(CONFIG_CDP_DEVICE_ID);
Mike Frysinger79ca1b92009-02-11 18:23:48 -0500899 sprintf(buf, CONFIG_CDP_DEVICE_ID_PREFIX "%pm", NetOurEther);
wdenk145d2c12004-04-15 21:48:45 +0000900 memcpy((uchar *)s, buf, 16);
901 s += 16 / 2;
902#endif
903
904#ifdef CONFIG_CDP_PORT_ID
905 *s++ = htons(CDP_PORT_ID_TLV);
906 memset(buf, 0, sizeof(buf));
907 sprintf(buf, CONFIG_CDP_PORT_ID, eth_get_dev_index());
908 len = strlen(buf);
909 if (len & 1) /* make it even */
910 len++;
911 *s++ = htons(len + 4);
912 memcpy((uchar *)s, buf, len);
913 s += len / 2;
914#endif
915
916#ifdef CONFIG_CDP_CAPABILITIES
917 *s++ = htons(CDP_CAPABILITIES_TLV);
918 *s++ = htons(8);
919 *(ulong *)s = htonl(CONFIG_CDP_CAPABILITIES);
920 s += 2;
921#endif
922
923#ifdef CONFIG_CDP_VERSION
924 *s++ = htons(CDP_VERSION_TLV);
925 memset(buf, 0, sizeof(buf));
926 strcpy(buf, CONFIG_CDP_VERSION);
927 len = strlen(buf);
928 if (len & 1) /* make it even */
929 len++;
930 *s++ = htons(len + 4);
931 memcpy((uchar *)s, buf, len);
932 s += len / 2;
933#endif
934
935#ifdef CONFIG_CDP_PLATFORM
936 *s++ = htons(CDP_PLATFORM_TLV);
937 memset(buf, 0, sizeof(buf));
938 strcpy(buf, CONFIG_CDP_PLATFORM);
939 len = strlen(buf);
940 if (len & 1) /* make it even */
941 len++;
942 *s++ = htons(len + 4);
943 memcpy((uchar *)s, buf, len);
944 s += len / 2;
945#endif
946
947#ifdef CONFIG_CDP_TRIGGER
948 *s++ = htons(CDP_TRIGGER_TLV);
949 *s++ = htons(8);
950 *(ulong *)s = htonl(CONFIG_CDP_TRIGGER);
951 s += 2;
952#endif
953
954#ifdef CONFIG_CDP_POWER_CONSUMPTION
955 *s++ = htons(CDP_POWER_CONSUMPTION_TLV);
956 *s++ = htons(6);
957 *s++ = htons(CONFIG_CDP_POWER_CONSUMPTION);
958#endif
959
960 /* length of ethernet packet */
961 len = (uchar *)s - ((uchar *)NetTxPacket + ETHER_HDR_SIZE);
962 et->et_protlen = htons(len);
963
964 len = ETHER_HDR_SIZE + sizeof(CDP_SNAP_hdr);
965 chksum = CDP_compute_csum((uchar *)NetTxPacket + len, (uchar *)s - (NetTxPacket + len));
966 if (chksum == 0)
967 chksum = 0xFFFF;
968 *cp = htons(chksum);
969
970 (void) eth_send(NetTxPacket, (uchar *)s - NetTxPacket);
971 return 0;
972}
973
974static void
975CDPTimeout (void)
976{
977 CDPSeq++;
978
979 if (CDPSeq < 3) {
980 NetSetTimeout (CDP_TIMEOUT, CDPTimeout);
981 CDPSendTrigger();
982 return;
983 }
984
985 /* if not OK try again */
986 if (!CDPOK)
987 NetStartAgain();
988 else
989 NetState = NETLOOP_SUCCESS;
990}
991
992static void
993CDPDummyHandler (uchar * pkt, unsigned dest, unsigned src, unsigned len)
994{
995 /* nothing */
996}
997
998static void
999CDPHandler(const uchar * pkt, unsigned len)
1000{
1001 const uchar *t;
1002 const ushort *ss;
1003 ushort type, tlen;
1004 uchar applid;
1005 ushort vlan, nvlan;
1006
1007 /* minimum size? */
1008 if (len < sizeof(CDP_SNAP_hdr) + 4)
1009 goto pkt_short;
1010
1011 /* check for valid CDP SNAP header */
1012 if (memcmp(pkt, CDP_SNAP_hdr, sizeof(CDP_SNAP_hdr)) != 0)
1013 return;
1014
1015 pkt += sizeof(CDP_SNAP_hdr);
1016 len -= sizeof(CDP_SNAP_hdr);
1017
1018 /* Version of CDP protocol must be >= 2 and TTL != 0 */
1019 if (pkt[0] < 0x02 || pkt[1] == 0)
1020 return;
1021
1022 /* if version is greater than 0x02 maybe we'll have a problem; output a warning */
1023 if (pkt[0] != 0x02)
1024 printf("** WARNING: CDP packet received with a protocol version %d > 2\n",
1025 pkt[0] & 0xff);
1026
1027 if (CDP_compute_csum(pkt, len) != 0)
1028 return;
1029
1030 pkt += 4;
1031 len -= 4;
1032
1033 vlan = htons(-1);
1034 nvlan = htons(-1);
1035 while (len > 0) {
1036 if (len < 4)
1037 goto pkt_short;
1038
1039 ss = (const ushort *)pkt;
1040 type = ntohs(ss[0]);
1041 tlen = ntohs(ss[1]);
1042 if (tlen > len) {
1043 goto pkt_short;
1044 }
1045
1046 pkt += tlen;
1047 len -= tlen;
1048
1049 ss += 2; /* point ss to the data of the TLV */
1050 tlen -= 4;
1051
1052 switch (type) {
1053 case CDP_DEVICE_ID_TLV:
1054 break;
1055 case CDP_ADDRESS_TLV:
1056 break;
1057 case CDP_PORT_ID_TLV:
1058 break;
1059 case CDP_CAPABILITIES_TLV:
1060 break;
1061 case CDP_VERSION_TLV:
1062 break;
1063 case CDP_PLATFORM_TLV:
1064 break;
1065 case CDP_NATIVE_VLAN_TLV:
1066 nvlan = *ss;
1067 break;
1068 case CDP_APPLIANCE_VLAN_TLV:
1069 t = (const uchar *)ss;
1070 while (tlen > 0) {
1071 if (tlen < 3)
1072 goto pkt_short;
1073
1074 applid = t[0];
1075 ss = (const ushort *)(t + 1);
1076
1077#ifdef CONFIG_CDP_APPLIANCE_VLAN_TYPE
1078 if (applid == CONFIG_CDP_APPLIANCE_VLAN_TYPE)
1079 vlan = *ss;
1080#else
1081 vlan = ntohs(*ss); /* XXX will this work; dunno */
1082#endif
1083 t += 3; tlen -= 3;
1084 }
1085 break;
1086 case CDP_TRIGGER_TLV:
1087 break;
1088 case CDP_POWER_CONSUMPTION_TLV:
1089 break;
1090 case CDP_SYSNAME_TLV:
1091 break;
1092 case CDP_SYSOBJECT_TLV:
1093 break;
1094 case CDP_MANAGEMENT_ADDRESS_TLV:
1095 break;
1096 }
1097 }
1098
1099 CDPApplianceVLAN = vlan;
1100 CDPNativeVLAN = nvlan;
1101
1102 CDPOK = 1;
1103 return;
1104
1105 pkt_short:
1106 printf("** CDP packet is too short\n");
1107 return;
1108}
1109
1110static void CDPStart(void)
1111{
1112#if defined(CONFIG_NET_MULTI)
1113 printf ("Using %s device\n", eth_get_name());
wdenke6466f62003-06-05 19:27:42 +00001114#endif
wdenk145d2c12004-04-15 21:48:45 +00001115 CDPSeq = 0;
1116 CDPOK = 0;
1117
1118 CDPNativeVLAN = htons(-1);
1119 CDPApplianceVLAN = htons(-1);
1120
1121 NetSetTimeout (CDP_TIMEOUT, CDPTimeout);
1122 NetSetHandler (CDPDummyHandler);
1123
1124 CDPSendTrigger();
1125}
Jon Loeligera9807e52007-07-10 11:05:02 -05001126#endif
wdenk145d2c12004-04-15 21:48:45 +00001127
Alessandro Rubinif119e3d2009-08-07 13:58:56 +02001128#ifdef CONFIG_IP_DEFRAG
1129/*
1130 * This function collects fragments in a single packet, according
1131 * to the algorithm in RFC815. It returns NULL or the pointer to
1132 * a complete packet, in static storage
1133 */
1134#ifndef CONFIG_NET_MAXDEFRAG
1135#define CONFIG_NET_MAXDEFRAG 16384
1136#endif
1137/*
1138 * MAXDEFRAG, above, is chosen in the config file and is real data
1139 * so we need to add the NFS overhead, which is more than TFTP.
1140 * To use sizeof in the internal unnamed structures, we need a real
1141 * instance (can't do "sizeof(struct rpc_t.u.reply))", unfortunately).
1142 * The compiler doesn't complain nor allocates the actual structure
1143 */
1144static struct rpc_t rpc_specimen;
1145#define IP_PKTSIZE (CONFIG_NET_MAXDEFRAG + sizeof(rpc_specimen.u.reply))
1146
1147#define IP_MAXUDP (IP_PKTSIZE - IP_HDR_SIZE_NO_UDP)
1148
1149/*
1150 * this is the packet being assembled, either data or frag control.
1151 * Fragments go by 8 bytes, so this union must be 8 bytes long
1152 */
1153struct hole {
1154 /* first_byte is address of this structure */
1155 u16 last_byte; /* last byte in this hole + 1 (begin of next hole) */
1156 u16 next_hole; /* index of next (in 8-b blocks), 0 == none */
1157 u16 prev_hole; /* index of prev, 0 == none */
1158 u16 unused;
1159};
1160
1161static IP_t *__NetDefragment(IP_t *ip, int *lenp)
1162{
1163 static uchar pkt_buff[IP_PKTSIZE] __attribute__((aligned(PKTALIGN)));
1164 static u16 first_hole, total_len;
1165 struct hole *payload, *thisfrag, *h, *newh;
1166 IP_t *localip = (IP_t *)pkt_buff;
1167 uchar *indata = (uchar *)ip;
1168 int offset8, start, len, done = 0;
1169 u16 ip_off = ntohs(ip->ip_off);
1170
1171 /* payload starts after IP header, this fragment is in there */
1172 payload = (struct hole *)(pkt_buff + IP_HDR_SIZE_NO_UDP);
1173 offset8 = (ip_off & IP_OFFS);
1174 thisfrag = payload + offset8;
1175 start = offset8 * 8;
1176 len = ntohs(ip->ip_len) - IP_HDR_SIZE_NO_UDP;
1177
1178 if (start + len > IP_MAXUDP) /* fragment extends too far */
1179 return NULL;
1180
1181 if (!total_len || localip->ip_id != ip->ip_id) {
1182 /* new (or different) packet, reset structs */
1183 total_len = 0xffff;
1184 payload[0].last_byte = ~0;
1185 payload[0].next_hole = 0;
1186 payload[0].prev_hole = 0;
1187 first_hole = 0;
1188 /* any IP header will work, copy the first we received */
1189 memcpy(localip, ip, IP_HDR_SIZE_NO_UDP);
1190 }
1191
1192 /*
1193 * What follows is the reassembly algorithm. We use the payload
1194 * array as a linked list of hole descriptors, as each hole starts
1195 * at a multiple of 8 bytes. However, last byte can be whatever value,
1196 * so it is represented as byte count, not as 8-byte blocks.
1197 */
1198
1199 h = payload + first_hole;
1200 while (h->last_byte < start) {
1201 if (!h->next_hole) {
1202 /* no hole that far away */
1203 return NULL;
1204 }
1205 h = payload + h->next_hole;
1206 }
1207
Fillod Stephanee7ade8b2010-06-11 19:26:43 +02001208 /* last fragment may be 1..7 bytes, the "+7" forces acceptance */
1209 if (offset8 + ((len + 7) / 8) <= h - payload) {
Alessandro Rubinif119e3d2009-08-07 13:58:56 +02001210 /* no overlap with holes (dup fragment?) */
1211 return NULL;
1212 }
1213
1214 if (!(ip_off & IP_FLAGS_MFRAG)) {
1215 /* no more fragmentss: truncate this (last) hole */
1216 total_len = start + len;
1217 h->last_byte = start + len;
1218 }
1219
1220 /*
1221 * There is some overlap: fix the hole list. This code doesn't
1222 * deal with a fragment that overlaps with two different holes
1223 * (thus being a superset of a previously-received fragment).
1224 */
1225
1226 if ( (h >= thisfrag) && (h->last_byte <= start + len) ) {
1227 /* complete overlap with hole: remove hole */
1228 if (!h->prev_hole && !h->next_hole) {
1229 /* last remaining hole */
1230 done = 1;
1231 } else if (!h->prev_hole) {
1232 /* first hole */
1233 first_hole = h->next_hole;
1234 payload[h->next_hole].prev_hole = 0;
1235 } else if (!h->next_hole) {
1236 /* last hole */
1237 payload[h->prev_hole].next_hole = 0;
1238 } else {
1239 /* in the middle of the list */
1240 payload[h->next_hole].prev_hole = h->prev_hole;
1241 payload[h->prev_hole].next_hole = h->next_hole;
1242 }
1243
1244 } else if (h->last_byte <= start + len) {
1245 /* overlaps with final part of the hole: shorten this hole */
1246 h->last_byte = start;
1247
1248 } else if (h >= thisfrag) {
1249 /* overlaps with initial part of the hole: move this hole */
1250 newh = thisfrag + (len / 8);
1251 *newh = *h;
1252 h = newh;
1253 if (h->next_hole)
1254 payload[h->next_hole].prev_hole = (h - payload);
1255 if (h->prev_hole)
1256 payload[h->prev_hole].next_hole = (h - payload);
1257 else
1258 first_hole = (h - payload);
1259
1260 } else {
1261 /* fragment sits in the middle: split the hole */
1262 newh = thisfrag + (len / 8);
1263 *newh = *h;
1264 h->last_byte = start;
1265 h->next_hole = (newh - payload);
1266 newh->prev_hole = (h - payload);
1267 if (newh->next_hole)
1268 payload[newh->next_hole].prev_hole = (newh - payload);
1269 }
1270
1271 /* finally copy this fragment and possibly return whole packet */
1272 memcpy((uchar *)thisfrag, indata + IP_HDR_SIZE_NO_UDP, len);
1273 if (!done)
1274 return NULL;
1275
1276 localip->ip_len = htons(total_len);
1277 *lenp = total_len + IP_HDR_SIZE_NO_UDP;
1278 return localip;
1279}
1280
1281static inline IP_t *NetDefragment(IP_t *ip, int *lenp)
1282{
1283 u16 ip_off = ntohs(ip->ip_off);
1284 if (!(ip_off & (IP_OFFS | IP_FLAGS_MFRAG)))
1285 return ip; /* not a fragment */
1286 return __NetDefragment(ip, lenp);
1287}
1288
1289#else /* !CONFIG_IP_DEFRAG */
1290
1291static inline IP_t *NetDefragment(IP_t *ip, int *lenp)
1292{
1293 u16 ip_off = ntohs(ip->ip_off);
1294 if (!(ip_off & (IP_OFFS | IP_FLAGS_MFRAG)))
1295 return ip; /* not a fragment */
1296 return NULL;
1297}
1298#endif
wdenk2d966952002-10-31 22:12:35 +00001299
1300void
wdenk145d2c12004-04-15 21:48:45 +00001301NetReceive(volatile uchar * inpkt, int len)
wdenk2d966952002-10-31 22:12:35 +00001302{
1303 Ethernet_t *et;
1304 IP_t *ip;
1305 ARP_t *arp;
1306 IPaddr_t tmp;
1307 int x;
wdenk145d2c12004-04-15 21:48:45 +00001308 uchar *pkt;
Jon Loeliger54f35c22007-07-09 17:45:14 -05001309#if defined(CONFIG_CMD_CDP)
wdenk145d2c12004-04-15 21:48:45 +00001310 int iscdp;
1311#endif
1312 ushort cti = 0, vlanid = VLAN_NONE, myvlanid, mynvlanid;
wdenk2d966952002-10-31 22:12:35 +00001313
Robin Getz9e0a4d62009-07-23 03:01:03 -04001314 debug("packet received\n");
wdenk145d2c12004-04-15 21:48:45 +00001315
Mike Frysingerdc166032009-07-18 21:04:08 -04001316 NetRxPacket = inpkt;
1317 NetRxPacketLen = len;
wdenk145d2c12004-04-15 21:48:45 +00001318 et = (Ethernet_t *)inpkt;
1319
1320 /* too small packet? */
1321 if (len < ETHER_HDR_SIZE)
1322 return;
Rafal Jaworowski1f2c9a42007-12-27 18:19:02 +01001323
1324#ifdef CONFIG_API
1325 if (push_packet) {
1326 (*push_packet)(inpkt, len);
1327 return;
1328 }
1329#endif
wdenk145d2c12004-04-15 21:48:45 +00001330
Jon Loeliger54f35c22007-07-09 17:45:14 -05001331#if defined(CONFIG_CMD_CDP)
wdenk145d2c12004-04-15 21:48:45 +00001332 /* keep track if packet is CDP */
1333 iscdp = memcmp(et->et_dest, NetCDPAddr, 6) == 0;
1334#endif
1335
1336 myvlanid = ntohs(NetOurVLAN);
1337 if (myvlanid == (ushort)-1)
1338 myvlanid = VLAN_NONE;
1339 mynvlanid = ntohs(NetOurNativeVLAN);
1340 if (mynvlanid == (ushort)-1)
1341 mynvlanid = VLAN_NONE;
wdenk2d966952002-10-31 22:12:35 +00001342
1343 x = ntohs(et->et_protlen);
1344
Robin Getz9e0a4d62009-07-23 03:01:03 -04001345 debug("packet received\n");
wdenk145d2c12004-04-15 21:48:45 +00001346
wdenk2d966952002-10-31 22:12:35 +00001347 if (x < 1514) {
1348 /*
1349 * Got a 802 packet. Check the other protocol field.
1350 */
1351 x = ntohs(et->et_prot);
wdenk145d2c12004-04-15 21:48:45 +00001352
1353 ip = (IP_t *)(inpkt + E802_HDR_SIZE);
wdenk2d966952002-10-31 22:12:35 +00001354 len -= E802_HDR_SIZE;
wdenk145d2c12004-04-15 21:48:45 +00001355
1356 } else if (x != PROT_VLAN) { /* normal packet */
1357 ip = (IP_t *)(inpkt + ETHER_HDR_SIZE);
wdenk2d966952002-10-31 22:12:35 +00001358 len -= ETHER_HDR_SIZE;
wdenk145d2c12004-04-15 21:48:45 +00001359
1360 } else { /* VLAN packet */
1361 VLAN_Ethernet_t *vet = (VLAN_Ethernet_t *)et;
1362
Robin Getz9e0a4d62009-07-23 03:01:03 -04001363 debug("VLAN packet received\n");
1364
wdenk145d2c12004-04-15 21:48:45 +00001365 /* too small packet? */
1366 if (len < VLAN_ETHER_HDR_SIZE)
1367 return;
1368
1369 /* if no VLAN active */
1370 if ((ntohs(NetOurVLAN) & VLAN_IDMASK) == VLAN_NONE
Jon Loeliger54f35c22007-07-09 17:45:14 -05001371#if defined(CONFIG_CMD_CDP)
wdenk145d2c12004-04-15 21:48:45 +00001372 && iscdp == 0
1373#endif
1374 )
1375 return;
1376
1377 cti = ntohs(vet->vet_tag);
1378 vlanid = cti & VLAN_IDMASK;
1379 x = ntohs(vet->vet_type);
1380
1381 ip = (IP_t *)(inpkt + VLAN_ETHER_HDR_SIZE);
1382 len -= VLAN_ETHER_HDR_SIZE;
wdenk2d966952002-10-31 22:12:35 +00001383 }
1384
Robin Getz9e0a4d62009-07-23 03:01:03 -04001385 debug("Receive from protocol 0x%x\n", x);
wdenk2d966952002-10-31 22:12:35 +00001386
Jon Loeliger54f35c22007-07-09 17:45:14 -05001387#if defined(CONFIG_CMD_CDP)
wdenk145d2c12004-04-15 21:48:45 +00001388 if (iscdp) {
1389 CDPHandler((uchar *)ip, len);
1390 return;
1391 }
1392#endif
1393
1394 if ((myvlanid & VLAN_IDMASK) != VLAN_NONE) {
1395 if (vlanid == VLAN_NONE)
1396 vlanid = (mynvlanid & VLAN_IDMASK);
1397 /* not matched? */
1398 if (vlanid != (myvlanid & VLAN_IDMASK))
1399 return;
1400 }
1401
wdenk2d966952002-10-31 22:12:35 +00001402 switch (x) {
1403
1404 case PROT_ARP:
1405 /*
1406 * We have to deal with two types of ARP packets:
wdenk57b2d802003-06-27 21:31:46 +00001407 * - REQUEST packets will be answered by sending our
1408 * IP address - if we know it.
1409 * - REPLY packates are expected only after we asked
1410 * for the TFTP server's or the gateway's ethernet
1411 * address; so if we receive such a packet, we set
1412 * the server ethernet address
wdenk2d966952002-10-31 22:12:35 +00001413 */
Robin Getz9e0a4d62009-07-23 03:01:03 -04001414 debug("Got ARP\n");
1415
wdenk2d966952002-10-31 22:12:35 +00001416 arp = (ARP_t *)ip;
1417 if (len < ARP_HDR_SIZE) {
1418 printf("bad length %d < %d\n", len, ARP_HDR_SIZE);
1419 return;
1420 }
1421 if (ntohs(arp->ar_hrd) != ARP_ETHER) {
1422 return;
1423 }
1424 if (ntohs(arp->ar_pro) != PROT_IP) {
1425 return;
1426 }
1427 if (arp->ar_hln != 6) {
1428 return;
1429 }
1430 if (arp->ar_pln != 4) {
1431 return;
1432 }
1433
1434 if (NetOurIP == 0) {
1435 return;
1436 }
1437
1438 if (NetReadIP(&arp->ar_data[16]) != NetOurIP) {
1439 return;
1440 }
1441
1442 switch (ntohs(arp->ar_op)) {
1443 case ARPOP_REQUEST: /* reply with our IP address */
Robin Getz9e0a4d62009-07-23 03:01:03 -04001444 debug("Got ARP REQUEST, return our IP\n");
wdenk145d2c12004-04-15 21:48:45 +00001445 pkt = (uchar *)et;
1446 pkt += NetSetEther(pkt, et->et_src, PROT_ARP);
wdenk2d966952002-10-31 22:12:35 +00001447 arp->ar_op = htons(ARPOP_REPLY);
1448 memcpy (&arp->ar_data[10], &arp->ar_data[0], 6);
1449 NetCopyIP(&arp->ar_data[16], &arp->ar_data[6]);
1450 memcpy (&arp->ar_data[ 0], NetOurEther, 6);
1451 NetCopyIP(&arp->ar_data[ 6], &NetOurIP);
wdenk145d2c12004-04-15 21:48:45 +00001452 (void) eth_send((uchar *)et, (pkt - (uchar *)et) + ARP_HDR_SIZE);
wdenk2d966952002-10-31 22:12:35 +00001453 return;
wdenke6466f62003-06-05 19:27:42 +00001454
1455 case ARPOP_REPLY: /* arp reply */
1456 /* are we waiting for a reply */
1457 if (!NetArpWaitPacketIP || !NetArpWaitPacketMAC)
1458 break;
Robin Getz470a6d42009-07-21 12:15:28 -04001459
1460#ifdef CONFIG_KEEP_SERVERADDR
1461 if (NetServerIP == NetArpWaitPacketIP) {
1462 char buf[20];
1463 sprintf(buf, "%pM", arp->ar_data);
1464 setenv("serveraddr", buf);
1465 }
1466#endif
1467
Robin Getz9e0a4d62009-07-23 03:01:03 -04001468 debug("Got ARP REPLY, set server/gtwy eth addr (%pM)\n",
Mike Frysinger79ca1b92009-02-11 18:23:48 -05001469 arp->ar_data);
wdenke6466f62003-06-05 19:27:42 +00001470
1471 tmp = NetReadIP(&arp->ar_data[6]);
1472
1473 /* matched waiting packet's address */
1474 if (tmp == NetArpWaitReplyIP) {
Robin Getz9e0a4d62009-07-23 03:01:03 -04001475 debug("Got it\n");
wdenke6466f62003-06-05 19:27:42 +00001476 /* save address for later use */
1477 memcpy(NetArpWaitPacketMAC, &arp->ar_data[0], 6);
1478
wdenkb8fb6192004-08-02 21:11:11 +00001479#ifdef CONFIG_NETCONSOLE
1480 (*packetHandler)(0,0,0,0);
1481#endif
wdenke6466f62003-06-05 19:27:42 +00001482 /* modify header, and transmit it */
1483 memcpy(((Ethernet_t *)NetArpWaitTxPacket)->et_dest, NetArpWaitPacketMAC, 6);
1484 (void) eth_send(NetArpWaitTxPacket, NetArpWaitTxPacketSize);
1485
1486 /* no arp request pending now */
1487 NetArpWaitPacketIP = 0;
1488 NetArpWaitTxPacketSize = 0;
1489 NetArpWaitPacketMAC = NULL;
1490
1491 }
wdenk2d966952002-10-31 22:12:35 +00001492 return;
1493 default:
Robin Getz9e0a4d62009-07-23 03:01:03 -04001494 debug("Unexpected ARP opcode 0x%x\n", ntohs(arp->ar_op));
wdenk2d966952002-10-31 22:12:35 +00001495 return;
1496 }
wdenkcb99da52005-01-12 00:15:14 +00001497 break;
wdenk2d966952002-10-31 22:12:35 +00001498
Peter Tyserf7a48ca2010-09-30 11:25:48 -05001499#ifdef CONFIG_CMD_RARP
wdenk2d966952002-10-31 22:12:35 +00001500 case PROT_RARP:
Robin Getz9e0a4d62009-07-23 03:01:03 -04001501 debug("Got RARP\n");
wdenk2d966952002-10-31 22:12:35 +00001502 arp = (ARP_t *)ip;
1503 if (len < ARP_HDR_SIZE) {
1504 printf("bad length %d < %d\n", len, ARP_HDR_SIZE);
1505 return;
1506 }
1507
1508 if ((ntohs(arp->ar_op) != RARPOP_REPLY) ||
1509 (ntohs(arp->ar_hrd) != ARP_ETHER) ||
1510 (ntohs(arp->ar_pro) != PROT_IP) ||
1511 (arp->ar_hln != 6) || (arp->ar_pln != 4)) {
1512
wdenk42c05472004-03-23 22:14:11 +00001513 puts ("invalid RARP header\n");
wdenk2d966952002-10-31 22:12:35 +00001514 } else {
1515 NetCopyIP(&NetOurIP, &arp->ar_data[16]);
wdenk4989f872004-03-14 15:06:13 +00001516 if (NetServerIP == 0)
1517 NetCopyIP(&NetServerIP, &arp->ar_data[ 6]);
wdenk2d966952002-10-31 22:12:35 +00001518 memcpy (NetServerEther, &arp->ar_data[ 0], 6);
1519
1520 (*packetHandler)(0,0,0,0);
1521 }
1522 break;
Peter Tyserf7a48ca2010-09-30 11:25:48 -05001523#endif
wdenk2d966952002-10-31 22:12:35 +00001524 case PROT_IP:
Robin Getz9e0a4d62009-07-23 03:01:03 -04001525 debug("Got IP\n");
Alessandro Rubinif119e3d2009-08-07 13:58:56 +02001526 /* Before we start poking the header, make sure it is there */
wdenk2d966952002-10-31 22:12:35 +00001527 if (len < IP_HDR_SIZE) {
Robin Getz9e0a4d62009-07-23 03:01:03 -04001528 debug("len bad %d < %lu\n", len, (ulong)IP_HDR_SIZE);
wdenk2d966952002-10-31 22:12:35 +00001529 return;
1530 }
Alessandro Rubinif119e3d2009-08-07 13:58:56 +02001531 /* Check the packet length */
wdenk2d966952002-10-31 22:12:35 +00001532 if (len < ntohs(ip->ip_len)) {
1533 printf("len bad %d < %d\n", len, ntohs(ip->ip_len));
1534 return;
1535 }
1536 len = ntohs(ip->ip_len);
Robin Getz9e0a4d62009-07-23 03:01:03 -04001537 debug("len=%d, v=%02x\n", len, ip->ip_hl_v & 0xff);
1538
Alessandro Rubinif119e3d2009-08-07 13:58:56 +02001539 /* Can't deal with anything except IPv4 */
wdenk2d966952002-10-31 22:12:35 +00001540 if ((ip->ip_hl_v & 0xf0) != 0x40) {
1541 return;
1542 }
Alessandro Rubinif119e3d2009-08-07 13:58:56 +02001543 /* Can't deal with IP options (headers != 20 bytes) */
Remy Bohmerb9535782008-06-03 15:48:17 +02001544 if ((ip->ip_hl_v & 0x0f) > 0x05) {
1545 return;
1546 }
Alessandro Rubinif119e3d2009-08-07 13:58:56 +02001547 /* Check the Checksum of the header */
wdenk2d966952002-10-31 22:12:35 +00001548 if (!NetCksumOk((uchar *)ip, IP_HDR_SIZE_NO_UDP / 2)) {
wdenk42c05472004-03-23 22:14:11 +00001549 puts ("checksum bad\n");
wdenk2d966952002-10-31 22:12:35 +00001550 return;
1551 }
Alessandro Rubinif119e3d2009-08-07 13:58:56 +02001552 /* If it is not for us, ignore it */
wdenk2d966952002-10-31 22:12:35 +00001553 tmp = NetReadIP(&ip->ip_dst);
1554 if (NetOurIP && tmp != NetOurIP && tmp != 0xFFFFFFFF) {
David Updegraff7280da72007-06-11 10:41:07 -05001555#ifdef CONFIG_MCAST_TFTP
Wolfgang Denk627f5c32007-08-14 09:47:27 +02001556 if (Mcast_addr != tmp)
David Updegraff7280da72007-06-11 10:41:07 -05001557#endif
wdenk2d966952002-10-31 22:12:35 +00001558 return;
1559 }
1560 /*
Alessandro Rubinif119e3d2009-08-07 13:58:56 +02001561 * The function returns the unchanged packet if it's not
1562 * a fragment, and either the complete packet or NULL if
1563 * it is a fragment (if !CONFIG_IP_DEFRAG, it returns NULL)
1564 */
1565 if (!(ip = NetDefragment(ip, &len)))
1566 return;
1567 /*
wdenk2d966952002-10-31 22:12:35 +00001568 * watch for ICMP host redirects
1569 *
wdenk57b2d802003-06-27 21:31:46 +00001570 * There is no real handler code (yet). We just watch
1571 * for ICMP host redirect messages. In case anybody
1572 * sees these messages: please contact me
1573 * (wd@denx.de), or - even better - send me the
1574 * necessary fixes :-)
wdenk2d966952002-10-31 22:12:35 +00001575 *
wdenk57b2d802003-06-27 21:31:46 +00001576 * Note: in all cases where I have seen this so far
1577 * it was a problem with the router configuration,
1578 * for instance when a router was configured in the
1579 * BOOTP reply, but the TFTP server was on the same
1580 * subnet. So this is probably a warning that your
1581 * configuration might be wrong. But I'm not really
1582 * sure if there aren't any other situations.
wdenk2d966952002-10-31 22:12:35 +00001583 */
1584 if (ip->ip_p == IPPROTO_ICMP) {
1585 ICMP_t *icmph = (ICMP_t *)&(ip->udp_src);
1586
wdenke6466f62003-06-05 19:27:42 +00001587 switch (icmph->type) {
1588 case ICMP_REDIRECT:
wdenkcbc49a52005-05-03 14:12:25 +00001589 if (icmph->code != ICMP_REDIR_HOST)
1590 return;
Mike Frysingerd5695f42009-02-17 00:00:53 -05001591 printf (" ICMP Host Redirect to %pI4 ", &icmph->un.gateway);
Stefan Roesedfced812005-08-12 20:06:52 +02001592 return;
Jon Loeliger54f35c22007-07-09 17:45:14 -05001593#if defined(CONFIG_CMD_PING)
wdenke6466f62003-06-05 19:27:42 +00001594 case ICMP_ECHO_REPLY:
1595 /*
1596 * IP header OK. Pass the packet to the current handler.
1597 */
1598 /* XXX point to ip packet */
1599 (*packetHandler)((uchar *)ip, 0, 0, 0);
Stefan Roesedfced812005-08-12 20:06:52 +02001600 return;
Ed Swarthout83109192007-03-07 12:14:50 -06001601 case ICMP_ECHO_REQUEST:
Robin Getz9e0a4d62009-07-23 03:01:03 -04001602 debug("Got ICMP ECHO REQUEST, return %d bytes \n",
Ed Swarthout83109192007-03-07 12:14:50 -06001603 ETHER_HDR_SIZE + len);
Robin Getz9e0a4d62009-07-23 03:01:03 -04001604
Ed Swarthout83109192007-03-07 12:14:50 -06001605 memcpy (&et->et_dest[0], &et->et_src[0], 6);
1606 memcpy (&et->et_src[ 0], NetOurEther, 6);
1607
1608 ip->ip_sum = 0;
1609 ip->ip_off = 0;
1610 NetCopyIP((void*)&ip->ip_dst, &ip->ip_src);
1611 NetCopyIP((void*)&ip->ip_src, &NetOurIP);
1612 ip->ip_sum = ~NetCksum((uchar *)ip, IP_HDR_SIZE_NO_UDP >> 1);
1613
1614 icmph->type = ICMP_ECHO_REPLY;
1615 icmph->checksum = 0;
1616 icmph->checksum = ~NetCksum((uchar *)icmph,
1617 (len - IP_HDR_SIZE_NO_UDP) >> 1);
1618 (void) eth_send((uchar *)et, ETHER_HDR_SIZE + len);
1619 return;
wdenke6466f62003-06-05 19:27:42 +00001620#endif
1621 default:
1622 return;
1623 }
wdenk2d966952002-10-31 22:12:35 +00001624 } else if (ip->ip_p != IPPROTO_UDP) { /* Only UDP packets */
1625 return;
1626 }
1627
Stefan Roesedfced812005-08-12 20:06:52 +02001628#ifdef CONFIG_UDP_CHECKSUM
1629 if (ip->udp_xsum != 0) {
Wolfgang Denk30b87322005-08-12 23:43:12 +02001630 ulong xsum;
Stefan Roesedfced812005-08-12 20:06:52 +02001631 ushort *sumptr;
1632 ushort sumlen;
1633
1634 xsum = ip->ip_p;
1635 xsum += (ntohs(ip->udp_len));
1636 xsum += (ntohl(ip->ip_src) >> 16) & 0x0000ffff;
1637 xsum += (ntohl(ip->ip_src) >> 0) & 0x0000ffff;
1638 xsum += (ntohl(ip->ip_dst) >> 16) & 0x0000ffff;
1639 xsum += (ntohl(ip->ip_dst) >> 0) & 0x0000ffff;
1640
1641 sumlen = ntohs(ip->udp_len);
1642 sumptr = (ushort *) &(ip->udp_src);
1643
1644 while (sumlen > 1) {
Wolfgang Denk30b87322005-08-12 23:43:12 +02001645 ushort sumdata;
Stefan Roesedfced812005-08-12 20:06:52 +02001646
1647 sumdata = *sumptr++;
1648 xsum += ntohs(sumdata);
1649 sumlen -= 2;
1650 }
1651 if (sumlen > 0) {
Wolfgang Denk30b87322005-08-12 23:43:12 +02001652 ushort sumdata;
Stefan Roesedfced812005-08-12 20:06:52 +02001653
1654 sumdata = *(unsigned char *) sumptr;
Wolfgang Denk30b87322005-08-12 23:43:12 +02001655 sumdata = (sumdata << 8) & 0xff00;
Stefan Roesedfced812005-08-12 20:06:52 +02001656 xsum += sumdata;
1657 }
1658 while ((xsum >> 16) != 0) {
Wolfgang Denk30b87322005-08-12 23:43:12 +02001659 xsum = (xsum & 0x0000ffff) + ((xsum >> 16) & 0x0000ffff);
Stefan Roesedfced812005-08-12 20:06:52 +02001660 }
1661 if ((xsum != 0x00000000) && (xsum != 0x0000ffff)) {
Wolfgang Denk12cec0a2008-07-11 01:16:00 +02001662 printf(" UDP wrong checksum %08lx %08x\n",
1663 xsum, ntohs(ip->udp_xsum));
Stefan Roesedfced812005-08-12 20:06:52 +02001664 return;
1665 }
1666 }
1667#endif
1668
David Updegraff7280da72007-06-11 10:41:07 -05001669
wdenkb8fb6192004-08-02 21:11:11 +00001670#ifdef CONFIG_NETCONSOLE
1671 nc_input_packet((uchar *)ip +IP_HDR_SIZE,
1672 ntohs(ip->udp_dst),
1673 ntohs(ip->udp_src),
1674 ntohs(ip->udp_len) - 8);
1675#endif
wdenk2d966952002-10-31 22:12:35 +00001676 /*
1677 * IP header OK. Pass the packet to the current handler.
1678 */
1679 (*packetHandler)((uchar *)ip +IP_HDR_SIZE,
1680 ntohs(ip->udp_dst),
1681 ntohs(ip->udp_src),
1682 ntohs(ip->udp_len) - 8);
wdenk2d966952002-10-31 22:12:35 +00001683 break;
1684 }
1685}
1686
1687
1688/**********************************************************************/
1689
1690static int net_check_prereq (proto_t protocol)
1691{
1692 switch (protocol) {
wdenk05939202004-04-18 17:39:38 +00001693 /* Fall through */
Jon Loeliger54f35c22007-07-09 17:45:14 -05001694#if defined(CONFIG_CMD_PING)
wdenke6466f62003-06-05 19:27:42 +00001695 case PING:
wdenk05939202004-04-18 17:39:38 +00001696 if (NetPingIP == 0) {
1697 puts ("*** ERROR: ping address not given\n");
1698 return (1);
1699 }
1700 goto common;
wdenke6466f62003-06-05 19:27:42 +00001701#endif
Jon Loeliger54f35c22007-07-09 17:45:14 -05001702#if defined(CONFIG_CMD_SNTP)
wdenkb4ad9622005-04-01 00:25:43 +00001703 case SNTP:
1704 if (NetNtpServerIP == 0) {
1705 puts ("*** ERROR: NTP server address not given\n");
1706 return (1);
1707 }
1708 goto common;
1709#endif
Robin Getz82f0d232009-07-20 14:53:54 -04001710#if defined(CONFIG_CMD_DNS)
1711 case DNS:
1712 if (NetOurDNSIP == 0) {
1713 puts("*** ERROR: DNS server address not given\n");
1714 return 1;
1715 }
1716 goto common;
1717#endif
Jon Loeliger54f35c22007-07-09 17:45:14 -05001718#if defined(CONFIG_CMD_NFS)
wdenkbe9c1cb2004-02-24 02:00:03 +00001719 case NFS:
1720#endif
wdenkb8fb6192004-08-02 21:11:11 +00001721 case NETCONS:
wdenk2d966952002-10-31 22:12:35 +00001722 case TFTP:
wdenk05939202004-04-18 17:39:38 +00001723 if (NetServerIP == 0) {
1724 puts ("*** ERROR: `serverip' not set\n");
1725 return (1);
1726 }
Jon Loeliger54f35c22007-07-09 17:45:14 -05001727#if defined(CONFIG_CMD_PING) || defined(CONFIG_CMD_SNTP)
Wolfgang Denk30b87322005-08-12 23:43:12 +02001728 common:
wdenke6466f62003-06-05 19:27:42 +00001729#endif
1730
wdenk05939202004-04-18 17:39:38 +00001731 if (NetOurIP == 0) {
1732 puts ("*** ERROR: `ipaddr' not set\n");
1733 return (1);
1734 }
1735 /* Fall through */
wdenk2d966952002-10-31 22:12:35 +00001736
Peter Tyserf7a48ca2010-09-30 11:25:48 -05001737#ifdef CONFIG_CMD_RARP
wdenk2d966952002-10-31 22:12:35 +00001738 case RARP:
Peter Tyserf7a48ca2010-09-30 11:25:48 -05001739#endif
wdenk2d966952002-10-31 22:12:35 +00001740 case BOOTP:
wdenk145d2c12004-04-15 21:48:45 +00001741 case CDP:
Peter Tyserf7a48ca2010-09-30 11:25:48 -05001742 case DHCP:
wdenk05939202004-04-18 17:39:38 +00001743 if (memcmp (NetOurEther, "\0\0\0\0\0\0", 6) == 0) {
wdenk2d966952002-10-31 22:12:35 +00001744#ifdef CONFIG_NET_MULTI
wdenk05939202004-04-18 17:39:38 +00001745 extern int eth_get_dev_index (void);
1746 int num = eth_get_dev_index ();
wdenk2d966952002-10-31 22:12:35 +00001747
wdenk05939202004-04-18 17:39:38 +00001748 switch (num) {
1749 case -1:
wdenk2d966952002-10-31 22:12:35 +00001750 puts ("*** ERROR: No ethernet found.\n");
1751 return (1);
wdenk05939202004-04-18 17:39:38 +00001752 case 0:
wdenk2d966952002-10-31 22:12:35 +00001753 puts ("*** ERROR: `ethaddr' not set\n");
1754 break;
wdenk05939202004-04-18 17:39:38 +00001755 default:
wdenk57b2d802003-06-27 21:31:46 +00001756 printf ("*** ERROR: `eth%daddr' not set\n",
wdenk2d966952002-10-31 22:12:35 +00001757 num);
1758 break;
wdenk05939202004-04-18 17:39:38 +00001759 }
wdenk2d966952002-10-31 22:12:35 +00001760
wdenk05939202004-04-18 17:39:38 +00001761 NetStartAgain ();
1762 return (2);
wdenk2d966952002-10-31 22:12:35 +00001763#else
wdenk05939202004-04-18 17:39:38 +00001764 puts ("*** ERROR: `ethaddr' not set\n");
1765 return (1);
wdenk2d966952002-10-31 22:12:35 +00001766#endif
wdenk05939202004-04-18 17:39:38 +00001767 }
1768 /* Fall through */
1769 default:
1770 return (0);
wdenk2d966952002-10-31 22:12:35 +00001771 }
wdenk05939202004-04-18 17:39:38 +00001772 return (0); /* OK */
wdenk2d966952002-10-31 22:12:35 +00001773}
1774/**********************************************************************/
1775
1776int
1777NetCksumOk(uchar * ptr, int len)
1778{
1779 return !((NetCksum(ptr, len) + 1) & 0xfffe);
1780}
1781
1782
1783unsigned
1784NetCksum(uchar * ptr, int len)
1785{
1786 ulong xsum;
Stefan Roeseac553282005-08-31 12:55:50 +02001787 ushort *p = (ushort *)ptr;
wdenk2d966952002-10-31 22:12:35 +00001788
1789 xsum = 0;
1790 while (len-- > 0)
Wolfgang Denk3135c542005-08-26 01:36:03 +02001791 xsum += *p++;
wdenk2d966952002-10-31 22:12:35 +00001792 xsum = (xsum & 0xffff) + (xsum >> 16);
1793 xsum = (xsum & 0xffff) + (xsum >> 16);
1794 return (xsum & 0xffff);
1795}
1796
wdenk145d2c12004-04-15 21:48:45 +00001797int
1798NetEthHdrSize(void)
1799{
1800 ushort myvlanid;
1801
1802 myvlanid = ntohs(NetOurVLAN);
1803 if (myvlanid == (ushort)-1)
1804 myvlanid = VLAN_NONE;
wdenk2d966952002-10-31 22:12:35 +00001805
wdenk145d2c12004-04-15 21:48:45 +00001806 return ((myvlanid & VLAN_IDMASK) == VLAN_NONE) ? ETHER_HDR_SIZE : VLAN_ETHER_HDR_SIZE;
1807}
1808
1809int
wdenk2d966952002-10-31 22:12:35 +00001810NetSetEther(volatile uchar * xet, uchar * addr, uint prot)
1811{
1812 Ethernet_t *et = (Ethernet_t *)xet;
wdenk145d2c12004-04-15 21:48:45 +00001813 ushort myvlanid;
1814
1815 myvlanid = ntohs(NetOurVLAN);
1816 if (myvlanid == (ushort)-1)
1817 myvlanid = VLAN_NONE;
wdenk2d966952002-10-31 22:12:35 +00001818
1819 memcpy (et->et_dest, addr, 6);
1820 memcpy (et->et_src, NetOurEther, 6);
wdenk145d2c12004-04-15 21:48:45 +00001821 if ((myvlanid & VLAN_IDMASK) == VLAN_NONE) {
wdenk2d966952002-10-31 22:12:35 +00001822 et->et_protlen = htons(prot);
wdenk145d2c12004-04-15 21:48:45 +00001823 return ETHER_HDR_SIZE;
1824 } else {
1825 VLAN_Ethernet_t *vet = (VLAN_Ethernet_t *)xet;
wdenk2d966952002-10-31 22:12:35 +00001826
wdenk145d2c12004-04-15 21:48:45 +00001827 vet->vet_vlan_type = htons(PROT_VLAN);
1828 vet->vet_tag = htons((0 << 5) | (myvlanid & VLAN_IDMASK));
1829 vet->vet_type = htons(prot);
1830 return VLAN_ETHER_HDR_SIZE;
1831 }
1832}
wdenk2d966952002-10-31 22:12:35 +00001833
1834void
1835NetSetIP(volatile uchar * xip, IPaddr_t dest, int dport, int sport, int len)
1836{
Olav Morkene8a02772009-01-23 12:56:26 +01001837 IP_t *ip = (IP_t *)xip;
wdenk2d966952002-10-31 22:12:35 +00001838
1839 /*
1840 * If the data is an odd number of bytes, zero the
1841 * byte after the last byte so that the checksum
1842 * will work.
1843 */
1844 if (len & 1)
1845 xip[IP_HDR_SIZE + len] = 0;
1846
1847 /*
1848 * Construct an IP and UDP header.
wdenk05939202004-04-18 17:39:38 +00001849 * (need to set no fragment bit - XXX)
wdenk2d966952002-10-31 22:12:35 +00001850 */
1851 ip->ip_hl_v = 0x45; /* IP_HDR_SIZE / 4 (not including UDP) */
1852 ip->ip_tos = 0;
1853 ip->ip_len = htons(IP_HDR_SIZE + len);
1854 ip->ip_id = htons(NetIPID++);
Peter Tyser0885bf02008-12-01 16:26:20 -06001855 ip->ip_off = htons(IP_FLAGS_DFRAG); /* Don't fragment */
wdenk2d966952002-10-31 22:12:35 +00001856 ip->ip_ttl = 255;
1857 ip->ip_p = 17; /* UDP */
1858 ip->ip_sum = 0;
1859 NetCopyIP((void*)&ip->ip_src, &NetOurIP); /* already in network byte order */
1860 NetCopyIP((void*)&ip->ip_dst, &dest); /* - "" - */
1861 ip->udp_src = htons(sport);
1862 ip->udp_dst = htons(dport);
1863 ip->udp_len = htons(8 + len);
1864 ip->udp_xsum = 0;
1865 ip->ip_sum = ~NetCksum((uchar *)ip, IP_HDR_SIZE_NO_UDP / 2);
1866}
1867
Mike Frysingerc8ecaa32010-10-20 07:16:40 -04001868void copy_filename (char *dst, const char *src, int size)
wdenk2d966952002-10-31 22:12:35 +00001869{
1870 if (*src && (*src == '"')) {
1871 ++src;
1872 --size;
1873 }
1874
1875 while ((--size > 0) && *src && (*src != '"')) {
1876 *dst++ = *src++;
1877 }
1878 *dst = '\0';
1879}
1880
Robin Getz82f0d232009-07-20 14:53:54 -04001881#if defined(CONFIG_CMD_NFS) || defined(CONFIG_CMD_SNTP) || defined(CONFIG_CMD_DNS)
1882/*
Robin Getz55b50e92010-03-08 14:07:00 -05001883 * make port a little random (1024-17407)
1884 * This keeps the math somewhat trivial to compute, and seems to work with
1885 * all supported protocols/clients/servers
Robin Getz82f0d232009-07-20 14:53:54 -04001886 */
1887unsigned int random_port(void)
1888{
Robin Getz55b50e92010-03-08 14:07:00 -05001889 return 1024 + (get_timer(0) % 0x4000);
Robin Getz82f0d232009-07-20 14:53:54 -04001890}
1891#endif
1892
wdenk2d966952002-10-31 22:12:35 +00001893void ip_to_string (IPaddr_t x, char *s)
1894{
wdenk145d2c12004-04-15 21:48:45 +00001895 x = ntohl (x);
1896 sprintf (s, "%d.%d.%d.%d",
1897 (int) ((x >> 24) & 0xff),
1898 (int) ((x >> 16) & 0xff),
1899 (int) ((x >> 8) & 0xff), (int) ((x >> 0) & 0xff)
wdenk05939202004-04-18 17:39:38 +00001900 );
wdenk2d966952002-10-31 22:12:35 +00001901}
1902
wdenk145d2c12004-04-15 21:48:45 +00001903void VLAN_to_string(ushort x, char *s)
1904{
1905 x = ntohs(x);
1906
1907 if (x == (ushort)-1)
1908 x = VLAN_NONE;
1909
1910 if (x == VLAN_NONE)
1911 strcpy(s, "none");
1912 else
1913 sprintf(s, "%d", x & VLAN_IDMASK);
1914}
1915
Mike Frysinger89c73922010-10-20 07:16:48 -04001916ushort string_to_VLAN(const char *s)
wdenk145d2c12004-04-15 21:48:45 +00001917{
1918 ushort id;
1919
1920 if (s == NULL)
wdenk656140b2004-04-25 13:18:40 +00001921 return htons(VLAN_NONE);
wdenk145d2c12004-04-15 21:48:45 +00001922
1923 if (*s < '0' || *s > '9')
1924 id = VLAN_NONE;
1925 else
1926 id = (ushort)simple_strtoul(s, NULL, 10);
1927
wdenk656140b2004-04-25 13:18:40 +00001928 return htons(id);
wdenk145d2c12004-04-15 21:48:45 +00001929}
1930
wdenk145d2c12004-04-15 21:48:45 +00001931ushort getenv_VLAN(char *var)
1932{
1933 return (string_to_VLAN(getenv(var)));
1934}