blob: a22b350efea9b6a19bcd2e7990866e6b72ab5a35 [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
Luca Ceresoli5c83a962011-05-11 03:59:54 +0000104/* Milliseconds before trying ARP again */
105# define ARP_TIMEOUT 5000UL
Guennadi Liakhovetskib38c2b32008-04-03 17:04:19 +0200106#else
Bartlomiej Sieka56668462008-10-01 15:26:28 +0200107# define ARP_TIMEOUT CONFIG_ARP_TIMEOUT
Guennadi Liakhovetskib38c2b32008-04-03 17:04:19 +0200108#endif
109
110
wdenke6466f62003-06-05 19:27:42 +0000111#ifndef CONFIG_NET_RETRY_COUNT
Guennadi Liakhovetskib38c2b32008-04-03 17:04:19 +0200112# define ARP_TIMEOUT_COUNT 5 /* # of timeouts before giving up */
wdenke6466f62003-06-05 19:27:42 +0000113#else
Guennadi Liakhovetskib38c2b32008-04-03 17:04:19 +0200114# define ARP_TIMEOUT_COUNT CONFIG_NET_RETRY_COUNT
wdenke6466f62003-06-05 19:27:42 +0000115#endif
116
wdenk2d966952002-10-31 22:12:35 +0000117/** BOOTP EXTENTIONS **/
118
Luca Ceresoli5c83a962011-05-11 03:59:54 +0000119/* Our subnet mask (0=unknown) */
Luca Ceresolid01bc1c2011-05-11 03:59:55 +0000120IPaddr_t NetOurSubnetMask;
Luca Ceresoli5c83a962011-05-11 03:59:54 +0000121/* Our gateways IP address */
Luca Ceresolid01bc1c2011-05-11 03:59:55 +0000122IPaddr_t NetOurGatewayIP;
Luca Ceresoli5c83a962011-05-11 03:59:54 +0000123/* Our DNS IP address */
Luca Ceresolid01bc1c2011-05-11 03:59:55 +0000124IPaddr_t NetOurDNSIP;
Jon Loeliger5336a762007-07-09 22:08:34 -0500125#if defined(CONFIG_BOOTP_DNS2)
Luca Ceresoli5c83a962011-05-11 03:59:54 +0000126/* Our 2nd DNS IP address */
Luca Ceresolid01bc1c2011-05-11 03:59:55 +0000127IPaddr_t NetOurDNS2IP;
stroesee0aadfb2003-08-28 14:17:32 +0000128#endif
Luca Ceresoli5c83a962011-05-11 03:59:54 +0000129/* Our NIS domain */
Luca Ceresolid01bc1c2011-05-11 03:59:55 +0000130char NetOurNISDomain[32] = {0,};
Luca Ceresoli5c83a962011-05-11 03:59:54 +0000131/* Our hostname */
Luca Ceresolid01bc1c2011-05-11 03:59:55 +0000132char NetOurHostName[32] = {0,};
Luca Ceresoli5c83a962011-05-11 03:59:54 +0000133/* Our bootpath */
Luca Ceresolid01bc1c2011-05-11 03:59:55 +0000134char NetOurRootPath[64] = {0,};
Luca Ceresoli5c83a962011-05-11 03:59:54 +0000135/* Our bootfile size in blocks */
Luca Ceresolid01bc1c2011-05-11 03:59:55 +0000136ushort NetBootFileSize;
wdenk2d966952002-10-31 22:12:35 +0000137
David Updegraff7280da72007-06-11 10:41:07 -0500138#ifdef CONFIG_MCAST_TFTP /* Multicast TFTP */
139IPaddr_t Mcast_addr;
140#endif
141
wdenk2d966952002-10-31 22:12:35 +0000142/** END OF BOOTP EXTENTIONS **/
143
Luca Ceresoli5c83a962011-05-11 03:59:54 +0000144/* The actual transferred size of the bootfile (in bytes) */
145ulong NetBootFileXferSize;
146/* Our ethernet address */
147uchar NetOurEther[6];
148/* Boot server enet address */
Luca Ceresolid01bc1c2011-05-11 03:59:55 +0000149uchar NetServerEther[6];
Luca Ceresoli5c83a962011-05-11 03:59:54 +0000150/* Our IP addr (0 = unknown) */
151IPaddr_t NetOurIP;
152/* Server IP addr (0 = unknown) */
153IPaddr_t NetServerIP;
154/* Current receive packet */
155volatile uchar *NetRxPacket;
156/* Current rx packet length */
157int NetRxPacketLen;
158/* IP packet ID */
159unsigned NetIPID;
160/* Ethernet bcast address */
Luca Ceresolid01bc1c2011-05-11 03:59:55 +0000161uchar NetBcastAddr[6] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
162uchar NetEtherNullAddr[6];
Rafal Jaworowski1f2c9a42007-12-27 18:19:02 +0100163#ifdef CONFIG_API
164void (*push_packet)(volatile void *, int len) = 0;
165#endif
Jon Loeliger54f35c22007-07-09 17:45:14 -0500166#if defined(CONFIG_CMD_CDP)
Luca Ceresoli5c83a962011-05-11 03:59:54 +0000167/* Ethernet bcast address */
Luca Ceresolid01bc1c2011-05-11 03:59:55 +0000168uchar NetCDPAddr[6] = { 0x01, 0x00, 0x0c, 0xcc, 0xcc, 0xcc };
wdenk145d2c12004-04-15 21:48:45 +0000169#endif
Luca Ceresoli5c83a962011-05-11 03:59:54 +0000170/* Network loop state */
171int NetState;
wdenk2d966952002-10-31 22:12:35 +0000172#ifdef CONFIG_NET_MULTI
Luca Ceresoli5c83a962011-05-11 03:59:54 +0000173/* Tried all network devices */
Luca Ceresolid01bc1c2011-05-11 03:59:55 +0000174int NetRestartWrap;
Luca Ceresoli5c83a962011-05-11 03:59:54 +0000175/* Network loop restarted */
Luca Ceresolid01bc1c2011-05-11 03:59:55 +0000176static int NetRestarted;
Luca Ceresoli5c83a962011-05-11 03:59:54 +0000177/* At least one device configured */
Luca Ceresolid01bc1c2011-05-11 03:59:55 +0000178static int NetDevExists;
wdenk2d966952002-10-31 22:12:35 +0000179#endif
180
wdenk05939202004-04-18 17:39:38 +0000181/* XXX in both little & big endian machines 0xFFFF == ntohs(-1) */
Luca Ceresoli5c83a962011-05-11 03:59:54 +0000182/* default is without VLAN */
183ushort NetOurVLAN = 0xFFFF;
184/* ditto */
185ushort NetOurNativeVLAN = 0xFFFF;
wdenk145d2c12004-04-15 21:48:45 +0000186
Luca Ceresoli5c83a962011-05-11 03:59:54 +0000187/* Boot File name */
188char BootFile[128];
wdenk2d966952002-10-31 22:12:35 +0000189
Jon Loeliger54f35c22007-07-09 17:45:14 -0500190#if defined(CONFIG_CMD_PING)
Luca Ceresoli5c83a962011-05-11 03:59:54 +0000191/* the ip address to ping */
192IPaddr_t NetPingIP;
wdenke6466f62003-06-05 19:27:42 +0000193
194static void PingStart(void);
195#endif
196
Jon Loeliger54f35c22007-07-09 17:45:14 -0500197#if defined(CONFIG_CMD_CDP)
wdenk145d2c12004-04-15 21:48:45 +0000198static void CDPStart(void);
199#endif
200
Jon Loeliger54f35c22007-07-09 17:45:14 -0500201#if defined(CONFIG_CMD_SNTP)
Luca Ceresoli5c83a962011-05-11 03:59:54 +0000202/* NTP server IP address */
203IPaddr_t NetNtpServerIP;
204/* offset time from UTC */
Luca Ceresolid01bc1c2011-05-11 03:59:55 +0000205int NetTimeOffset;
wdenkb4ad9622005-04-01 00:25:43 +0000206#endif
207
wdenkb8fb6192004-08-02 21:11:11 +0000208#ifdef CONFIG_NETCONSOLE
209void NcStart(void);
210int nc_input_packet(uchar *pkt, unsigned dest, unsigned src, unsigned len);
211#endif
212
wdenk2d966952002-10-31 22:12:35 +0000213volatile uchar PktBuf[(PKTBUFSRX+1) * PKTSIZE_ALIGN + PKTALIGN];
214
Luca Ceresoli5c83a962011-05-11 03:59:54 +0000215/* Receive packet */
216volatile uchar *NetRxPackets[PKTBUFSRX];
wdenk2d966952002-10-31 22:12:35 +0000217
Luca Ceresoli5c83a962011-05-11 03:59:54 +0000218/* Current RX packet handler */
219static rxhand_f *packetHandler;
220/* Current timeout handler */
221static thand_f *timeHandler;
222/* Time base value */
223static ulong timeStart;
224/* Current timeout value */
225static ulong timeDelta;
226/* THE transmit packet */
Luca Ceresolid01bc1c2011-05-11 03:59:55 +0000227volatile uchar *NetTxPacket;
wdenk2d966952002-10-31 22:12:35 +0000228
Luca Ceresoli7cbd7482011-05-11 03:59:56 +0000229static int net_check_prereq(proto_t protocol);
wdenk2d966952002-10-31 22:12:35 +0000230
Remy Bohmer0ddb8e82009-10-28 22:13:39 +0100231static int NetTryCount;
232
wdenk2d966952002-10-31 22:12:35 +0000233/**********************************************************************/
wdenke6466f62003-06-05 19:27:42 +0000234
235IPaddr_t NetArpWaitPacketIP;
236IPaddr_t NetArpWaitReplyIP;
Luca Ceresoli5c83a962011-05-11 03:59:54 +0000237/* MAC address of waiting packet's destination */
238uchar *NetArpWaitPacketMAC;
239/* THE transmit packet */
240uchar *NetArpWaitTxPacket;
wdenke6466f62003-06-05 19:27:42 +0000241int NetArpWaitTxPacketSize;
Wolfgang Denka1be4762008-05-20 16:00:29 +0200242uchar NetArpWaitPacketBuf[PKTSIZE_ALIGN + PKTALIGN];
wdenke6466f62003-06-05 19:27:42 +0000243ulong NetArpWaitTimerStart;
244int NetArpWaitTry;
245
Luca Ceresoli7cbd7482011-05-11 03:59:56 +0000246void ArpRequest(void)
wdenke6466f62003-06-05 19:27:42 +0000247{
248 int i;
249 volatile uchar *pkt;
wdenk05939202004-04-18 17:39:38 +0000250 ARP_t *arp;
wdenke6466f62003-06-05 19:27:42 +0000251
Robin Getz9e0a4d62009-07-23 03:01:03 -0400252 debug("ARP broadcast %d\n", NetArpWaitTry);
253
wdenke6466f62003-06-05 19:27:42 +0000254 pkt = NetTxPacket;
255
Luca Ceresoli7cbd7482011-05-11 03:59:56 +0000256 pkt += NetSetEther(pkt, NetBcastAddr, PROT_ARP);
wdenke6466f62003-06-05 19:27:42 +0000257
wdenk05939202004-04-18 17:39:38 +0000258 arp = (ARP_t *) pkt;
wdenke6466f62003-06-05 19:27:42 +0000259
Luca Ceresoli7cbd7482011-05-11 03:59:56 +0000260 arp->ar_hrd = htons(ARP_ETHER);
261 arp->ar_pro = htons(PROT_IP);
wdenke6466f62003-06-05 19:27:42 +0000262 arp->ar_hln = 6;
263 arp->ar_pln = 4;
Luca Ceresoli7cbd7482011-05-11 03:59:56 +0000264 arp->ar_op = htons(ARPOP_REQUEST);
wdenke6466f62003-06-05 19:27:42 +0000265
Luca Ceresoli5c83a962011-05-11 03:59:54 +0000266 /* source ET addr */
Luca Ceresoli7cbd7482011-05-11 03:59:56 +0000267 memcpy(&arp->ar_data[0], NetOurEther, 6);
Luca Ceresoli5c83a962011-05-11 03:59:54 +0000268 /* source IP addr */
Luca Ceresoli7cbd7482011-05-11 03:59:56 +0000269 NetWriteIP((uchar *) &arp->ar_data[6], NetOurIP);
wdenk05939202004-04-18 17:39:38 +0000270 for (i = 10; i < 16; ++i) {
Luca Ceresoli5c83a962011-05-11 03:59:54 +0000271 /* dest ET addr = 0 */
272 arp->ar_data[i] = 0;
wdenke6466f62003-06-05 19:27:42 +0000273 }
274
wdenk05939202004-04-18 17:39:38 +0000275 if ((NetArpWaitPacketIP & NetOurSubnetMask) !=
276 (NetOurIP & NetOurSubnetMask)) {
277 if (NetOurGatewayIP == 0) {
Luca Ceresoli7cbd7482011-05-11 03:59:56 +0000278 puts("## Warning: gatewayip needed but not set\n");
Wolfgang Denk98ceffb2006-03-12 01:13:30 +0100279 NetArpWaitReplyIP = NetArpWaitPacketIP;
280 } else {
281 NetArpWaitReplyIP = NetOurGatewayIP;
wdenk05939202004-04-18 17:39:38 +0000282 }
wdenk05939202004-04-18 17:39:38 +0000283 } else {
284 NetArpWaitReplyIP = NetArpWaitPacketIP;
285 }
wdenke6466f62003-06-05 19:27:42 +0000286
Luca Ceresoli7cbd7482011-05-11 03:59:56 +0000287 NetWriteIP((uchar *) &arp->ar_data[16], NetArpWaitReplyIP);
288 (void) eth_send(NetTxPacket, (pkt - NetTxPacket) + ARP_HDR_SIZE);
wdenke6466f62003-06-05 19:27:42 +0000289}
290
291void ArpTimeoutCheck(void)
292{
293 ulong t;
294
295 if (!NetArpWaitPacketIP)
296 return;
297
298 t = get_timer(0);
299
300 /* check for arp timeout */
Bartlomiej Sieka56668462008-10-01 15:26:28 +0200301 if ((t - NetArpWaitTimerStart) > ARP_TIMEOUT) {
wdenke6466f62003-06-05 19:27:42 +0000302 NetArpWaitTry++;
303
304 if (NetArpWaitTry >= ARP_TIMEOUT_COUNT) {
Luca Ceresoli7cbd7482011-05-11 03:59:56 +0000305 puts("\nARP Retry count exceeded; starting again\n");
wdenke6466f62003-06-05 19:27:42 +0000306 NetArpWaitTry = 0;
307 NetStartAgain();
308 } else {
309 NetArpWaitTimerStart = t;
310 ArpRequest();
311 }
312 }
313}
314
Heiko Schocher3b195ff2009-04-28 08:36:11 +0200315static void
Heiko Schocher0c303fa2009-02-10 09:38:52 +0100316NetInitLoop(proto_t protocol)
317{
Luca Ceresolid01bc1c2011-05-11 03:59:55 +0000318 static int env_changed_id;
Heiko Schocher0c303fa2009-02-10 09:38:52 +0100319 bd_t *bd = gd->bd;
Luca Ceresoli7cbd7482011-05-11 03:59:56 +0000320 int env_id = get_env_id();
Heiko Schocher0c303fa2009-02-10 09:38:52 +0100321
322 /* update only when the environment has changed */
Michael Zaidmanb97bfe42009-04-04 01:43:00 +0300323 if (env_changed_id != env_id) {
Heiko Schocher0c303fa2009-02-10 09:38:52 +0100324 NetCopyIP(&NetOurIP, &bd->bi_ip_addr);
Luca Ceresoli7cbd7482011-05-11 03:59:56 +0000325 NetOurGatewayIP = getenv_IPaddr("gatewayip");
326 NetOurSubnetMask = getenv_IPaddr("netmask");
327 NetServerIP = getenv_IPaddr("serverip");
Heiko Schocher0c303fa2009-02-10 09:38:52 +0100328 NetOurNativeVLAN = getenv_VLAN("nvlan");
Michael Zaidmanb97bfe42009-04-04 01:43:00 +0300329 NetOurVLAN = getenv_VLAN("vlan");
Robin Getz82f0d232009-07-20 14:53:54 -0400330#if defined(CONFIG_CMD_DNS)
331 NetOurDNSIP = getenv_IPaddr("dnsip");
332#endif
Michael Zaidmanb97bfe42009-04-04 01:43:00 +0300333 env_changed_id = env_id;
Heiko Schocher0c303fa2009-02-10 09:38:52 +0100334 }
Michael Zaidmanb97bfe42009-04-04 01:43:00 +0300335
Heiko Schocher3b195ff2009-04-28 08:36:11 +0200336 return;
Heiko Schocher0c303fa2009-02-10 09:38:52 +0100337}
338
wdenke6466f62003-06-05 19:27:42 +0000339/**********************************************************************/
wdenk2d966952002-10-31 22:12:35 +0000340/*
341 * Main network processing loop.
342 */
343
344int
345NetLoop(proto_t protocol)
346{
wdenk2d966952002-10-31 22:12:35 +0000347 bd_t *bd = gd->bd;
348
349#ifdef CONFIG_NET_MULTI
350 NetRestarted = 0;
351 NetDevExists = 0;
352#endif
353
wdenke6466f62003-06-05 19:27:42 +0000354 /* XXX problem with bss workaround */
355 NetArpWaitPacketMAC = NULL;
356 NetArpWaitTxPacket = NULL;
357 NetArpWaitPacketIP = 0;
358 NetArpWaitReplyIP = 0;
359 NetArpWaitTxPacket = NULL;
360 NetTxPacket = NULL;
Remy Bohmer0ddb8e82009-10-28 22:13:39 +0100361 NetTryCount = 1;
wdenke6466f62003-06-05 19:27:42 +0000362
wdenk2d966952002-10-31 22:12:35 +0000363 if (!NetTxPacket) {
364 int i;
wdenk2d966952002-10-31 22:12:35 +0000365 /*
366 * Setup packet buffers, aligned correctly.
367 */
368 NetTxPacket = &PktBuf[0] + (PKTALIGN - 1);
369 NetTxPacket -= (ulong)NetTxPacket % PKTALIGN;
Luca Ceresolie8ccbb02011-05-04 02:40:43 +0000370 for (i = 0; i < PKTBUFSRX; i++)
wdenk2d966952002-10-31 22:12:35 +0000371 NetRxPackets[i] = NetTxPacket + (i+1)*PKTSIZE_ALIGN;
wdenke6466f62003-06-05 19:27:42 +0000372 }
373
374 if (!NetArpWaitTxPacket) {
375 NetArpWaitTxPacket = &NetArpWaitPacketBuf[0] + (PKTALIGN - 1);
376 NetArpWaitTxPacket -= (ulong)NetArpWaitTxPacket % PKTALIGN;
377 NetArpWaitTxPacketSize = 0;
wdenk2d966952002-10-31 22:12:35 +0000378 }
379
380 eth_halt();
wdenk05939202004-04-18 17:39:38 +0000381#ifdef CONFIG_NET_MULTI
wdenk145d2c12004-04-15 21:48:45 +0000382 eth_set_current();
wdenk05939202004-04-18 17:39:38 +0000383#endif
wdenkfa66e932005-04-03 14:52:59 +0000384 if (eth_init(bd) < 0) {
385 eth_halt();
wdenk05939202004-04-18 17:39:38 +0000386 return(-1);
wdenkfa66e932005-04-03 14:52:59 +0000387 }
wdenk2d966952002-10-31 22:12:35 +0000388
389restart:
390#ifdef CONFIG_NET_MULTI
Luca Ceresoli7cbd7482011-05-11 03:59:56 +0000391 memcpy(NetOurEther, eth_get_dev()->enetaddr, 6);
wdenk2d966952002-10-31 22:12:35 +0000392#else
Mike Frysinger79ca1b92009-02-11 18:23:48 -0500393 eth_getenv_enetaddr("ethaddr", NetOurEther);
wdenk2d966952002-10-31 22:12:35 +0000394#endif
395
396 NetState = NETLOOP_CONTINUE;
397
398 /*
399 * Start the ball rolling with the given start function. From
400 * here on, this code is a state machine driven by received
401 * packets and timer events.
402 */
Heiko Schocher0c303fa2009-02-10 09:38:52 +0100403 NetInitLoop(protocol);
wdenk2d966952002-10-31 22:12:35 +0000404
Luca Ceresoli7cbd7482011-05-11 03:59:56 +0000405 switch (net_check_prereq(protocol)) {
wdenk2d966952002-10-31 22:12:35 +0000406 case 1:
407 /* network not configured */
wdenkfa66e932005-04-03 14:52:59 +0000408 eth_halt();
wdenk2582f6b2002-11-11 21:14:20 +0000409 return (-1);
wdenk2d966952002-10-31 22:12:35 +0000410
411#ifdef CONFIG_NET_MULTI
412 case 2:
413 /* network device not configured */
414 break;
415#endif /* CONFIG_NET_MULTI */
416
417 case 0:
418#ifdef CONFIG_NET_MULTI
419 NetDevExists = 1;
420#endif
421 switch (protocol) {
422 case TFTP:
423 /* always use ARP to get server ethernet address */
wdenke6466f62003-06-05 19:27:42 +0000424 TftpStart();
wdenk2d966952002-10-31 22:12:35 +0000425 break;
426
Jon Loeliger54f35c22007-07-09 17:45:14 -0500427#if defined(CONFIG_CMD_DHCP)
wdenk2d966952002-10-31 22:12:35 +0000428 case DHCP:
wdenkc3919532004-10-11 22:51:13 +0000429 BootpTry = 0;
Michael Zaidman16bb21d2009-07-14 23:37:12 +0300430 NetOurIP = 0;
wdenk2d966952002-10-31 22:12:35 +0000431 DhcpRequest(); /* Basically same as BOOTP */
432 break;
Jon Loeligera9807e52007-07-10 11:05:02 -0500433#endif
wdenk2d966952002-10-31 22:12:35 +0000434
435 case BOOTP:
436 BootpTry = 0;
Michael Zaidman16bb21d2009-07-14 23:37:12 +0300437 NetOurIP = 0;
Luca Ceresoli7cbd7482011-05-11 03:59:56 +0000438 BootpRequest();
wdenk2d966952002-10-31 22:12:35 +0000439 break;
440
Peter Tyserf7a48ca2010-09-30 11:25:48 -0500441#if defined(CONFIG_CMD_RARP)
wdenk2d966952002-10-31 22:12:35 +0000442 case RARP:
443 RarpTry = 0;
Michael Zaidman16bb21d2009-07-14 23:37:12 +0300444 NetOurIP = 0;
Luca Ceresoli7cbd7482011-05-11 03:59:56 +0000445 RarpRequest();
wdenk2d966952002-10-31 22:12:35 +0000446 break;
Peter Tyserf7a48ca2010-09-30 11:25:48 -0500447#endif
Jon Loeliger54f35c22007-07-09 17:45:14 -0500448#if defined(CONFIG_CMD_PING)
wdenke6466f62003-06-05 19:27:42 +0000449 case PING:
450 PingStart();
451 break;
452#endif
Jon Loeliger54f35c22007-07-09 17:45:14 -0500453#if defined(CONFIG_CMD_NFS)
wdenkbe9c1cb2004-02-24 02:00:03 +0000454 case NFS:
455 NfsStart();
456 break;
457#endif
Jon Loeliger54f35c22007-07-09 17:45:14 -0500458#if defined(CONFIG_CMD_CDP)
wdenk145d2c12004-04-15 21:48:45 +0000459 case CDP:
460 CDPStart();
461 break;
462#endif
wdenkb8fb6192004-08-02 21:11:11 +0000463#ifdef CONFIG_NETCONSOLE
464 case NETCONS:
465 NcStart();
466 break;
467#endif
Jon Loeliger54f35c22007-07-09 17:45:14 -0500468#if defined(CONFIG_CMD_SNTP)
wdenkb4ad9622005-04-01 00:25:43 +0000469 case SNTP:
470 SntpStart();
471 break;
472#endif
Robin Getz82f0d232009-07-20 14:53:54 -0400473#if defined(CONFIG_CMD_DNS)
474 case DNS:
475 DnsStart();
476 break;
477#endif
wdenk2d966952002-10-31 22:12:35 +0000478 default:
479 break;
480 }
481
482 NetBootFileXferSize = 0;
483 break;
484 }
485
Jon Loeliger54f35c22007-07-09 17:45:14 -0500486#if defined(CONFIG_MII) || defined(CONFIG_CMD_MII)
Luca Ceresoli5c83a962011-05-11 03:59:54 +0000487#if defined(CONFIG_SYS_FAULT_ECHO_LINK_DOWN) && \
488 defined(CONFIG_STATUS_LED) && \
489 defined(STATUS_LED_RED)
wdenk49c3f672003-10-08 22:33:00 +0000490 /*
wdenk9c53f402003-10-15 23:53:47 +0000491 * Echo the inverted link state to the fault LED.
wdenk49c3f672003-10-08 22:33:00 +0000492 */
Luca Ceresolie8ccbb02011-05-04 02:40:43 +0000493 if (miiphy_link(eth_get_dev()->name, CONFIG_SYS_FAULT_MII_ADDR))
Luca Ceresoli7cbd7482011-05-11 03:59:56 +0000494 status_led_set(STATUS_LED_RED, STATUS_LED_OFF);
Luca Ceresolie8ccbb02011-05-04 02:40:43 +0000495 else
Luca Ceresoli7cbd7482011-05-11 03:59:56 +0000496 status_led_set(STATUS_LED_RED, STATUS_LED_ON);
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200497#endif /* CONFIG_SYS_FAULT_ECHO_LINK_DOWN, ... */
wdenk49c3f672003-10-08 22:33:00 +0000498#endif /* CONFIG_MII, ... */
wdenk2d966952002-10-31 22:12:35 +0000499
500 /*
501 * Main packet reception loop. Loop receiving packets until
wdenk27fa5852005-04-03 14:18:51 +0000502 * someone sets `NetState' to a state that terminates.
wdenk2d966952002-10-31 22:12:35 +0000503 */
504 for (;;) {
505 WATCHDOG_RESET();
506#ifdef CONFIG_SHOW_ACTIVITY
507 {
508 extern void show_activity(int arg);
509 show_activity(1);
510 }
511#endif
512 /*
513 * Check the ethernet for a new packet. The ethernet
514 * receive routine will process it.
515 */
Guennadi Liakhovetskib38c2b32008-04-03 17:04:19 +0200516 eth_rx();
wdenk2d966952002-10-31 22:12:35 +0000517
518 /*
519 * Abort if ctrl-c was pressed.
520 */
521 if (ctrlc()) {
wdenk57b2d802003-06-27 21:31:46 +0000522 eth_halt();
Luca Ceresoli7cbd7482011-05-11 03:59:56 +0000523 puts("\nAbort\n");
wdenk2582f6b2002-11-11 21:14:20 +0000524 return (-1);
wdenk2d966952002-10-31 22:12:35 +0000525 }
526
wdenke6466f62003-06-05 19:27:42 +0000527 ArpTimeoutCheck();
wdenk2d966952002-10-31 22:12:35 +0000528
529 /*
530 * Check for a timeout, and run the timeout handler
531 * if we have one.
532 */
wdenk5d841732003-08-17 18:55:18 +0000533 if (timeHandler && ((get_timer(0) - timeStart) > timeDelta)) {
wdenk2d966952002-10-31 22:12:35 +0000534 thand_f *x;
535
Jon Loeliger54f35c22007-07-09 17:45:14 -0500536#if defined(CONFIG_MII) || defined(CONFIG_CMD_MII)
Luca Ceresoli7cbd7482011-05-11 03:59:56 +0000537#if defined(CONFIG_SYS_FAULT_ECHO_LINK_DOWN) && \
538 defined(CONFIG_STATUS_LED) && \
539 defined(STATUS_LED_RED)
wdenk49c3f672003-10-08 22:33:00 +0000540 /*
wdenk9c53f402003-10-15 23:53:47 +0000541 * Echo the inverted link state to the fault LED.
wdenk49c3f672003-10-08 22:33:00 +0000542 */
Luca Ceresoli7cbd7482011-05-11 03:59:56 +0000543 if (miiphy_link(eth_get_dev()->name,
Luca Ceresoli5c83a962011-05-11 03:59:54 +0000544 CONFIG_SYS_FAULT_MII_ADDR)) {
Luca Ceresoli7cbd7482011-05-11 03:59:56 +0000545 status_led_set(STATUS_LED_RED, STATUS_LED_OFF);
wdenk49c3f672003-10-08 22:33:00 +0000546 } else {
Luca Ceresoli7cbd7482011-05-11 03:59:56 +0000547 status_led_set(STATUS_LED_RED, STATUS_LED_ON);
wdenk49c3f672003-10-08 22:33:00 +0000548 }
Luca Ceresoli7cbd7482011-05-11 03:59:56 +0000549#endif /* CONFIG_SYS_FAULT_ECHO_LINK_DOWN, ... */
wdenk49c3f672003-10-08 22:33:00 +0000550#endif /* CONFIG_MII, ... */
wdenk2d966952002-10-31 22:12:35 +0000551 x = timeHandler;
552 timeHandler = (thand_f *)0;
553 (*x)();
554 }
555
556
557 switch (NetState) {
558
559 case NETLOOP_RESTART:
560#ifdef CONFIG_NET_MULTI
561 NetRestarted = 1;
562#endif
563 goto restart;
564
565 case NETLOOP_SUCCESS:
566 if (NetBootFileXferSize > 0) {
Wolfgang Denk084d0ce2007-09-12 00:48:57 +0200567 char buf[20];
wdenk2d966952002-10-31 22:12:35 +0000568 printf("Bytes transferred = %ld (%lx hex)\n",
569 NetBootFileXferSize,
570 NetBootFileXferSize);
Wolfgang Denk084d0ce2007-09-12 00:48:57 +0200571 sprintf(buf, "%lX", NetBootFileXferSize);
wdenk2d966952002-10-31 22:12:35 +0000572 setenv("filesize", buf);
wdenk145d2c12004-04-15 21:48:45 +0000573
574 sprintf(buf, "%lX", (unsigned long)load_addr);
575 setenv("fileaddr", buf);
wdenk2d966952002-10-31 22:12:35 +0000576 }
577 eth_halt();
578 return NetBootFileXferSize;
579
580 case NETLOOP_FAIL:
wdenk2582f6b2002-11-11 21:14:20 +0000581 return (-1);
wdenk2d966952002-10-31 22:12:35 +0000582 }
583 }
584}
585
586/**********************************************************************/
587
588static void
589startAgainTimeout(void)
590{
591 NetState = NETLOOP_RESTART;
592}
593
594static void
Luca Ceresoli428ab362011-04-18 06:19:50 +0000595startAgainHandler(uchar *pkt, unsigned dest, IPaddr_t sip,
596 unsigned src, unsigned len)
wdenk2d966952002-10-31 22:12:35 +0000597{
598 /* Totally ignore the packet */
599}
600
Luca Ceresoli7cbd7482011-05-11 03:59:56 +0000601void NetStartAgain(void)
wdenk2d966952002-10-31 22:12:35 +0000602{
wdenk05939202004-04-18 17:39:38 +0000603 char *nretry;
Remy Bohmer0ddb8e82009-10-28 22:13:39 +0100604 int retry_forever = 0;
605 unsigned long retrycnt = 0;
wdenk145d2c12004-04-15 21:48:45 +0000606
Remy Bohmer0ddb8e82009-10-28 22:13:39 +0100607 nretry = getenv("netretry");
608 if (nretry) {
609 if (!strcmp(nretry, "yes"))
610 retry_forever = 1;
611 else if (!strcmp(nretry, "no"))
612 retrycnt = 0;
613 else if (!strcmp(nretry, "once"))
614 retrycnt = 1;
615 else
616 retrycnt = simple_strtoul(nretry, NULL, 0);
617 } else
618 retry_forever = 1;
619
620 if ((!retry_forever) && (NetTryCount >= retrycnt)) {
621 eth_halt();
wdenk145d2c12004-04-15 21:48:45 +0000622 NetState = NETLOOP_FAIL;
623 return;
624 }
Remy Bohmer0ddb8e82009-10-28 22:13:39 +0100625
626 NetTryCount++;
627
wdenk2d966952002-10-31 22:12:35 +0000628#ifndef CONFIG_NET_MULTI
Luca Ceresoli7cbd7482011-05-11 03:59:56 +0000629 NetSetTimeout(10000UL, startAgainTimeout);
630 NetSetHandler(startAgainHandler);
wdenk05939202004-04-18 17:39:38 +0000631#else /* !CONFIG_NET_MULTI*/
Luca Ceresoli7cbd7482011-05-11 03:59:56 +0000632 eth_halt();
Matthias Fuchsa02d62b2007-12-27 16:58:41 +0100633#if !defined(CONFIG_NET_DO_NOT_TRY_ANOTHER)
Luca Ceresoli7cbd7482011-05-11 03:59:56 +0000634 eth_try_another(!NetRestarted);
Matthias Fuchsa02d62b2007-12-27 16:58:41 +0100635#endif
Luca Ceresoli7cbd7482011-05-11 03:59:56 +0000636 eth_init(gd->bd);
wdenk05939202004-04-18 17:39:38 +0000637 if (NetRestartWrap) {
wdenk2d966952002-10-31 22:12:35 +0000638 NetRestartWrap = 0;
Remy Bohmer0ddb8e82009-10-28 22:13:39 +0100639 if (NetDevExists) {
Luca Ceresoli7cbd7482011-05-11 03:59:56 +0000640 NetSetTimeout(10000UL, startAgainTimeout);
641 NetSetHandler(startAgainHandler);
wdenk05939202004-04-18 17:39:38 +0000642 } else {
wdenk2d966952002-10-31 22:12:35 +0000643 NetState = NETLOOP_FAIL;
644 }
wdenk05939202004-04-18 17:39:38 +0000645 } else {
wdenk2d966952002-10-31 22:12:35 +0000646 NetState = NETLOOP_RESTART;
647 }
wdenk05939202004-04-18 17:39:38 +0000648#endif /* CONFIG_NET_MULTI */
wdenk2d966952002-10-31 22:12:35 +0000649}
650
651/**********************************************************************/
652/*
653 * Miscelaneous bits.
654 */
655
656void
657NetSetHandler(rxhand_f * f)
658{
659 packetHandler = f;
660}
661
662
663void
wdenkd6a3dd42004-10-09 21:56:21 +0000664NetSetTimeout(ulong iv, thand_f * f)
wdenk2d966952002-10-31 22:12:35 +0000665{
666 if (iv == 0) {
667 timeHandler = (thand_f *)0;
668 } else {
669 timeHandler = f;
wdenk5d841732003-08-17 18:55:18 +0000670 timeStart = get_timer(0);
671 timeDelta = iv;
wdenk2d966952002-10-31 22:12:35 +0000672 }
673}
674
675
676void
677NetSendPacket(volatile uchar * pkt, int len)
678{
679 (void) eth_send(pkt, len);
680}
681
wdenke6466f62003-06-05 19:27:42 +0000682int
683NetSendUDPPacket(uchar *ether, IPaddr_t dest, int dport, int sport, int len)
684{
wdenk145d2c12004-04-15 21:48:45 +0000685 uchar *pkt;
686
wdenke6466f62003-06-05 19:27:42 +0000687 /* convert to new style broadcast */
688 if (dest == 0)
689 dest = 0xFFFFFFFF;
690
691 /* if broadcast, make the ether address a broadcast and don't do ARP */
692 if (dest == 0xFFFFFFFF)
693 ether = NetBcastAddr;
694
Luca Ceresoli5c83a962011-05-11 03:59:54 +0000695 /*
696 * if MAC address was not discovered yet, save the packet and do
697 * an ARP request
698 */
wdenke6466f62003-06-05 19:27:42 +0000699 if (memcmp(ether, NetEtherNullAddr, 6) == 0) {
700
Robin Getz9e0a4d62009-07-23 03:01:03 -0400701 debug("sending ARP for %08lx\n", dest);
702
wdenke6466f62003-06-05 19:27:42 +0000703 NetArpWaitPacketIP = dest;
704 NetArpWaitPacketMAC = ether;
wdenk145d2c12004-04-15 21:48:45 +0000705
706 pkt = NetArpWaitTxPacket;
Luca Ceresoli7cbd7482011-05-11 03:59:56 +0000707 pkt += NetSetEther(pkt, NetArpWaitPacketMAC, PROT_IP);
wdenk145d2c12004-04-15 21:48:45 +0000708
Luca Ceresoli7cbd7482011-05-11 03:59:56 +0000709 NetSetIP(pkt, dest, dport, sport, len);
Luca Ceresoli5c83a962011-05-11 03:59:54 +0000710 memcpy(pkt + IP_HDR_SIZE, (uchar *)NetTxPacket +
711 (pkt - (uchar *)NetArpWaitTxPacket) + IP_HDR_SIZE, len);
wdenke6466f62003-06-05 19:27:42 +0000712
713 /* size of the waiting packet */
Luca Ceresoli5c83a962011-05-11 03:59:54 +0000714 NetArpWaitTxPacketSize = (pkt - NetArpWaitTxPacket) +
715 IP_HDR_SIZE + len;
wdenke6466f62003-06-05 19:27:42 +0000716
717 /* and do the ARP request */
718 NetArpWaitTry = 1;
719 NetArpWaitTimerStart = get_timer(0);
720 ArpRequest();
721 return 1; /* waiting */
722 }
723
Robin Getz9e0a4d62009-07-23 03:01:03 -0400724 debug("sending UDP to %08lx/%pM\n", dest, ether);
wdenke6466f62003-06-05 19:27:42 +0000725
wdenk145d2c12004-04-15 21:48:45 +0000726 pkt = (uchar *)NetTxPacket;
Luca Ceresoli7cbd7482011-05-11 03:59:56 +0000727 pkt += NetSetEther(pkt, ether, PROT_IP);
728 NetSetIP(pkt, dest, dport, sport, len);
wdenk145d2c12004-04-15 21:48:45 +0000729 (void) eth_send(NetTxPacket, (pkt - NetTxPacket) + IP_HDR_SIZE + len);
wdenke6466f62003-06-05 19:27:42 +0000730
wdenk05939202004-04-18 17:39:38 +0000731 return 0; /* transmitted */
wdenke6466f62003-06-05 19:27:42 +0000732}
733
Jon Loeliger54f35c22007-07-09 17:45:14 -0500734#if defined(CONFIG_CMD_PING)
wdenke6466f62003-06-05 19:27:42 +0000735static ushort PingSeqNo;
736
737int PingSend(void)
738{
739 static uchar mac[6];
740 volatile IP_t *ip;
741 volatile ushort *s;
wdenk145d2c12004-04-15 21:48:45 +0000742 uchar *pkt;
wdenke6466f62003-06-05 19:27:42 +0000743
744 /* XXX always send arp request */
745
746 memcpy(mac, NetEtherNullAddr, 6);
747
Robin Getz9e0a4d62009-07-23 03:01:03 -0400748 debug("sending ARP for %08lx\n", NetPingIP);
wdenke6466f62003-06-05 19:27:42 +0000749
750 NetArpWaitPacketIP = NetPingIP;
751 NetArpWaitPacketMAC = mac;
752
wdenk145d2c12004-04-15 21:48:45 +0000753 pkt = NetArpWaitTxPacket;
754 pkt += NetSetEther(pkt, mac, PROT_IP);
wdenke6466f62003-06-05 19:27:42 +0000755
wdenk145d2c12004-04-15 21:48:45 +0000756 ip = (volatile IP_t *)pkt;
wdenke6466f62003-06-05 19:27:42 +0000757
758 /*
Luca Ceresoli5c83a962011-05-11 03:59:54 +0000759 * Construct an IP and ICMP header.
760 * (need to set no fragment bit - XXX)
wdenke6466f62003-06-05 19:27:42 +0000761 */
Luca Ceresoli5c83a962011-05-11 03:59:54 +0000762 /* IP_HDR_SIZE / 4 (not including UDP) */
763 ip->ip_hl_v = 0x45;
wdenke6466f62003-06-05 19:27:42 +0000764 ip->ip_tos = 0;
765 ip->ip_len = htons(IP_HDR_SIZE_NO_UDP + 8);
766 ip->ip_id = htons(NetIPID++);
Peter Tyser0885bf02008-12-01 16:26:20 -0600767 ip->ip_off = htons(IP_FLAGS_DFRAG); /* Don't fragment */
wdenke6466f62003-06-05 19:27:42 +0000768 ip->ip_ttl = 255;
769 ip->ip_p = 0x01; /* ICMP */
770 ip->ip_sum = 0;
Luca Ceresoli5c83a962011-05-11 03:59:54 +0000771 /* already in network byte order */
772 NetCopyIP((void*)&ip->ip_src, &NetOurIP);
773 /* - "" - */
774 NetCopyIP((void*)&ip->ip_dst, &NetPingIP);
wdenke6466f62003-06-05 19:27:42 +0000775 ip->ip_sum = ~NetCksum((uchar *)ip, IP_HDR_SIZE_NO_UDP / 2);
776
777 s = &ip->udp_src; /* XXX ICMP starts here */
778 s[0] = htons(0x0800); /* echo-request, code */
779 s[1] = 0; /* checksum */
Wolfgang Denka1be4762008-05-20 16:00:29 +0200780 s[2] = 0; /* identifier */
wdenke6466f62003-06-05 19:27:42 +0000781 s[3] = htons(PingSeqNo++); /* sequence number */
782 s[1] = ~NetCksum((uchar *)s, 8/2);
783
784 /* size of the waiting packet */
Luca Ceresoli5c83a962011-05-11 03:59:54 +0000785 NetArpWaitTxPacketSize =
786 (pkt - NetArpWaitTxPacket) + IP_HDR_SIZE_NO_UDP + 8;
wdenke6466f62003-06-05 19:27:42 +0000787
788 /* and do the ARP request */
789 NetArpWaitTry = 1;
790 NetArpWaitTimerStart = get_timer(0);
791 ArpRequest();
792 return 1; /* waiting */
793}
794
795static void
Luca Ceresoli7cbd7482011-05-11 03:59:56 +0000796PingTimeout(void)
wdenke6466f62003-06-05 19:27:42 +0000797{
798 eth_halt();
799 NetState = NETLOOP_FAIL; /* we did not get the reply */
800}
801
802static void
Luca Ceresoli428ab362011-04-18 06:19:50 +0000803PingHandler(uchar *pkt, unsigned dest, IPaddr_t sip, unsigned src,
804 unsigned len)
wdenke6466f62003-06-05 19:27:42 +0000805{
Luca Ceresoli428ab362011-04-18 06:19:50 +0000806 if (sip != NetPingIP)
wdenke6466f62003-06-05 19:27:42 +0000807 return;
808
809 NetState = NETLOOP_SUCCESS;
810}
811
812static void PingStart(void)
813{
wdenk145d2c12004-04-15 21:48:45 +0000814#if defined(CONFIG_NET_MULTI)
Luca Ceresoli7cbd7482011-05-11 03:59:56 +0000815 printf("Using %s device\n", eth_get_name());
wdenk05939202004-04-18 17:39:38 +0000816#endif /* CONFIG_NET_MULTI */
Luca Ceresoli7cbd7482011-05-11 03:59:56 +0000817 NetSetTimeout(10000UL, PingTimeout);
818 NetSetHandler(PingHandler);
wdenke6466f62003-06-05 19:27:42 +0000819
820 PingSend();
821}
Jon Loeligera9807e52007-07-10 11:05:02 -0500822#endif
wdenk145d2c12004-04-15 21:48:45 +0000823
Jon Loeliger54f35c22007-07-09 17:45:14 -0500824#if defined(CONFIG_CMD_CDP)
wdenk145d2c12004-04-15 21:48:45 +0000825
826#define CDP_DEVICE_ID_TLV 0x0001
827#define CDP_ADDRESS_TLV 0x0002
828#define CDP_PORT_ID_TLV 0x0003
829#define CDP_CAPABILITIES_TLV 0x0004
830#define CDP_VERSION_TLV 0x0005
831#define CDP_PLATFORM_TLV 0x0006
832#define CDP_NATIVE_VLAN_TLV 0x000a
833#define CDP_APPLIANCE_VLAN_TLV 0x000e
834#define CDP_TRIGGER_TLV 0x000f
835#define CDP_POWER_CONSUMPTION_TLV 0x0010
836#define CDP_SYSNAME_TLV 0x0014
837#define CDP_SYSOBJECT_TLV 0x0015
838#define CDP_MANAGEMENT_ADDRESS_TLV 0x0016
839
Bartlomiej Sieka56668462008-10-01 15:26:28 +0200840#define CDP_TIMEOUT 250UL /* one packet every 250ms */
wdenk145d2c12004-04-15 21:48:45 +0000841
842static int CDPSeq;
843static int CDPOK;
844
845ushort CDPNativeVLAN;
846ushort CDPApplianceVLAN;
847
Luca Ceresoli5c83a962011-05-11 03:59:54 +0000848static const uchar CDP_SNAP_hdr[8] = { 0xAA, 0xAA, 0x03, 0x00, 0x00, 0x0C, 0x20,
849 0x00 };
wdenk145d2c12004-04-15 21:48:45 +0000850
851static ushort CDP_compute_csum(const uchar *buff, ushort len)
852{
853 ushort csum;
854 int odd;
855 ulong result = 0;
856 ushort leftover;
Wolfgang Denk7fb52662005-10-13 16:45:02 +0200857 ushort *p;
wdenk145d2c12004-04-15 21:48:45 +0000858
859 if (len > 0) {
860 odd = 1 & (ulong)buff;
861 if (odd) {
862 result = *buff << 8;
863 len--;
864 buff++;
865 }
866 while (len > 1) {
Wolfgang Denk7fb52662005-10-13 16:45:02 +0200867 p = (ushort *)buff;
868 result += *p++;
869 buff = (uchar *)p;
wdenk145d2c12004-04-15 21:48:45 +0000870 if (result & 0x80000000)
871 result = (result & 0xFFFF) + (result >> 16);
872 len -= 2;
873 }
874 if (len) {
875 leftover = (signed short)(*(const signed char *)buff);
Wolfgang Denkf0711132005-11-10 20:59:46 +0100876 /* CISCO SUCKS big time! (and blows too):
877 * CDP uses the IP checksum algorithm with a twist;
878 * for the last byte it *sign* extends and sums.
879 */
Luca Ceresoli5c83a962011-05-11 03:59:54 +0000880 result = (result & 0xffff0000) |
881 ((result + leftover) & 0x0000ffff);
wdenk145d2c12004-04-15 21:48:45 +0000882 }
883 while (result >> 16)
884 result = (result & 0xFFFF) + (result >> 16);
885
886 if (odd)
Luca Ceresoli5c83a962011-05-11 03:59:54 +0000887 result = ((result >> 8) & 0xff) |
888 ((result & 0xff) << 8);
wdenk145d2c12004-04-15 21:48:45 +0000889 }
890
891 /* add up 16-bit and 17-bit words for 17+c bits */
892 result = (result & 0xffff) + (result >> 16);
893 /* add up 16-bit and 2-bit for 16+c bit */
894 result = (result & 0xffff) + (result >> 16);
895 /* add up carry.. */
896 result = (result & 0xffff) + (result >> 16);
897
898 /* negate */
899 csum = ~(ushort)result;
900
901 /* run time endian detection */
902 if (csum != htons(csum)) /* little endian */
903 csum = htons(csum);
904
905 return csum;
906}
907
908int CDPSendTrigger(void)
909{
910 volatile uchar *pkt;
911 volatile ushort *s;
912 volatile ushort *cp;
913 Ethernet_t *et;
wdenk145d2c12004-04-15 21:48:45 +0000914 int len;
915 ushort chksum;
Luca Ceresoli7cbd7482011-05-11 03:59:56 +0000916#if defined(CONFIG_CDP_DEVICE_ID) || defined(CONFIG_CDP_PORT_ID) || \
917 defined(CONFIG_CDP_VERSION) || defined(CONFIG_CDP_PLATFORM)
wdenk05939202004-04-18 17:39:38 +0000918 char buf[32];
919#endif
wdenk145d2c12004-04-15 21:48:45 +0000920
921 pkt = NetTxPacket;
922 et = (Ethernet_t *)pkt;
923
924 /* NOTE: trigger sent not on any VLAN */
925
926 /* form ethernet header */
927 memcpy(et->et_dest, NetCDPAddr, 6);
928 memcpy(et->et_src, NetOurEther, 6);
929
930 pkt += ETHER_HDR_SIZE;
931
932 /* SNAP header */
933 memcpy((uchar *)pkt, CDP_SNAP_hdr, sizeof(CDP_SNAP_hdr));
934 pkt += sizeof(CDP_SNAP_hdr);
935
936 /* CDP header */
937 *pkt++ = 0x02; /* CDP version 2 */
938 *pkt++ = 180; /* TTL */
939 s = (volatile ushort *)pkt;
940 cp = s;
Luca Ceresoli5c83a962011-05-11 03:59:54 +0000941 /* checksum (0 for later calculation) */
942 *s++ = htons(0);
wdenk145d2c12004-04-15 21:48:45 +0000943
944 /* CDP fields */
945#ifdef CONFIG_CDP_DEVICE_ID
946 *s++ = htons(CDP_DEVICE_ID_TLV);
947 *s++ = htons(CONFIG_CDP_DEVICE_ID);
Mike Frysinger79ca1b92009-02-11 18:23:48 -0500948 sprintf(buf, CONFIG_CDP_DEVICE_ID_PREFIX "%pm", NetOurEther);
wdenk145d2c12004-04-15 21:48:45 +0000949 memcpy((uchar *)s, buf, 16);
950 s += 16 / 2;
951#endif
952
953#ifdef CONFIG_CDP_PORT_ID
954 *s++ = htons(CDP_PORT_ID_TLV);
955 memset(buf, 0, sizeof(buf));
956 sprintf(buf, CONFIG_CDP_PORT_ID, eth_get_dev_index());
957 len = strlen(buf);
958 if (len & 1) /* make it even */
959 len++;
960 *s++ = htons(len + 4);
961 memcpy((uchar *)s, buf, len);
962 s += len / 2;
963#endif
964
965#ifdef CONFIG_CDP_CAPABILITIES
966 *s++ = htons(CDP_CAPABILITIES_TLV);
967 *s++ = htons(8);
968 *(ulong *)s = htonl(CONFIG_CDP_CAPABILITIES);
969 s += 2;
970#endif
971
972#ifdef CONFIG_CDP_VERSION
973 *s++ = htons(CDP_VERSION_TLV);
974 memset(buf, 0, sizeof(buf));
975 strcpy(buf, CONFIG_CDP_VERSION);
976 len = strlen(buf);
977 if (len & 1) /* make it even */
978 len++;
979 *s++ = htons(len + 4);
980 memcpy((uchar *)s, buf, len);
981 s += len / 2;
982#endif
983
984#ifdef CONFIG_CDP_PLATFORM
985 *s++ = htons(CDP_PLATFORM_TLV);
986 memset(buf, 0, sizeof(buf));
987 strcpy(buf, CONFIG_CDP_PLATFORM);
988 len = strlen(buf);
989 if (len & 1) /* make it even */
990 len++;
991 *s++ = htons(len + 4);
992 memcpy((uchar *)s, buf, len);
993 s += len / 2;
994#endif
995
996#ifdef CONFIG_CDP_TRIGGER
997 *s++ = htons(CDP_TRIGGER_TLV);
998 *s++ = htons(8);
999 *(ulong *)s = htonl(CONFIG_CDP_TRIGGER);
1000 s += 2;
1001#endif
1002
1003#ifdef CONFIG_CDP_POWER_CONSUMPTION
1004 *s++ = htons(CDP_POWER_CONSUMPTION_TLV);
1005 *s++ = htons(6);
1006 *s++ = htons(CONFIG_CDP_POWER_CONSUMPTION);
1007#endif
1008
1009 /* length of ethernet packet */
1010 len = (uchar *)s - ((uchar *)NetTxPacket + ETHER_HDR_SIZE);
1011 et->et_protlen = htons(len);
1012
1013 len = ETHER_HDR_SIZE + sizeof(CDP_SNAP_hdr);
Luca Ceresoli5c83a962011-05-11 03:59:54 +00001014 chksum = CDP_compute_csum((uchar *)NetTxPacket + len,
1015 (uchar *)s - (NetTxPacket + len));
wdenk145d2c12004-04-15 21:48:45 +00001016 if (chksum == 0)
1017 chksum = 0xFFFF;
1018 *cp = htons(chksum);
1019
1020 (void) eth_send(NetTxPacket, (uchar *)s - NetTxPacket);
1021 return 0;
1022}
1023
1024static void
Luca Ceresoli7cbd7482011-05-11 03:59:56 +00001025CDPTimeout(void)
wdenk145d2c12004-04-15 21:48:45 +00001026{
1027 CDPSeq++;
1028
1029 if (CDPSeq < 3) {
Luca Ceresoli7cbd7482011-05-11 03:59:56 +00001030 NetSetTimeout(CDP_TIMEOUT, CDPTimeout);
wdenk145d2c12004-04-15 21:48:45 +00001031 CDPSendTrigger();
1032 return;
1033 }
1034
1035 /* if not OK try again */
1036 if (!CDPOK)
1037 NetStartAgain();
1038 else
1039 NetState = NETLOOP_SUCCESS;
1040}
1041
1042static void
Luca Ceresoli428ab362011-04-18 06:19:50 +00001043CDPDummyHandler(uchar *pkt, unsigned dest, IPaddr_t sip, unsigned src,
1044 unsigned len)
wdenk145d2c12004-04-15 21:48:45 +00001045{
1046 /* nothing */
1047}
1048
1049static void
1050CDPHandler(const uchar * pkt, unsigned len)
1051{
1052 const uchar *t;
1053 const ushort *ss;
1054 ushort type, tlen;
1055 uchar applid;
1056 ushort vlan, nvlan;
1057
1058 /* minimum size? */
1059 if (len < sizeof(CDP_SNAP_hdr) + 4)
1060 goto pkt_short;
1061
1062 /* check for valid CDP SNAP header */
1063 if (memcmp(pkt, CDP_SNAP_hdr, sizeof(CDP_SNAP_hdr)) != 0)
1064 return;
1065
1066 pkt += sizeof(CDP_SNAP_hdr);
1067 len -= sizeof(CDP_SNAP_hdr);
1068
1069 /* Version of CDP protocol must be >= 2 and TTL != 0 */
1070 if (pkt[0] < 0x02 || pkt[1] == 0)
1071 return;
1072
Luca Ceresoli5c83a962011-05-11 03:59:54 +00001073 /*
1074 * if version is greater than 0x02 maybe we'll have a problem;
1075 * output a warning
1076 */
wdenk145d2c12004-04-15 21:48:45 +00001077 if (pkt[0] != 0x02)
1078 printf("** WARNING: CDP packet received with a protocol version %d > 2\n",
1079 pkt[0] & 0xff);
1080
1081 if (CDP_compute_csum(pkt, len) != 0)
1082 return;
1083
1084 pkt += 4;
1085 len -= 4;
1086
1087 vlan = htons(-1);
1088 nvlan = htons(-1);
1089 while (len > 0) {
1090 if (len < 4)
1091 goto pkt_short;
1092
1093 ss = (const ushort *)pkt;
1094 type = ntohs(ss[0]);
1095 tlen = ntohs(ss[1]);
Luca Ceresolie8ccbb02011-05-04 02:40:43 +00001096 if (tlen > len)
wdenk145d2c12004-04-15 21:48:45 +00001097 goto pkt_short;
wdenk145d2c12004-04-15 21:48:45 +00001098
1099 pkt += tlen;
1100 len -= tlen;
1101
1102 ss += 2; /* point ss to the data of the TLV */
1103 tlen -= 4;
1104
1105 switch (type) {
1106 case CDP_DEVICE_ID_TLV:
1107 break;
1108 case CDP_ADDRESS_TLV:
1109 break;
1110 case CDP_PORT_ID_TLV:
1111 break;
1112 case CDP_CAPABILITIES_TLV:
1113 break;
1114 case CDP_VERSION_TLV:
1115 break;
1116 case CDP_PLATFORM_TLV:
1117 break;
1118 case CDP_NATIVE_VLAN_TLV:
1119 nvlan = *ss;
1120 break;
1121 case CDP_APPLIANCE_VLAN_TLV:
1122 t = (const uchar *)ss;
1123 while (tlen > 0) {
1124 if (tlen < 3)
1125 goto pkt_short;
1126
1127 applid = t[0];
1128 ss = (const ushort *)(t + 1);
1129
1130#ifdef CONFIG_CDP_APPLIANCE_VLAN_TYPE
Luca Ceresoli5c83a962011-05-11 03:59:54 +00001131 if (applid ==
1132 CONFIG_CDP_APPLIANCE_VLAN_TYPE)
wdenk145d2c12004-04-15 21:48:45 +00001133 vlan = *ss;
1134#else
Luca Ceresoli5c83a962011-05-11 03:59:54 +00001135 /* XXX will this work; dunno */
1136 vlan = ntohs(*ss);
wdenk145d2c12004-04-15 21:48:45 +00001137#endif
1138 t += 3; tlen -= 3;
1139 }
1140 break;
1141 case CDP_TRIGGER_TLV:
1142 break;
1143 case CDP_POWER_CONSUMPTION_TLV:
1144 break;
1145 case CDP_SYSNAME_TLV:
1146 break;
1147 case CDP_SYSOBJECT_TLV:
1148 break;
1149 case CDP_MANAGEMENT_ADDRESS_TLV:
1150 break;
1151 }
1152 }
1153
1154 CDPApplianceVLAN = vlan;
1155 CDPNativeVLAN = nvlan;
1156
1157 CDPOK = 1;
1158 return;
1159
1160 pkt_short:
1161 printf("** CDP packet is too short\n");
1162 return;
1163}
1164
1165static void CDPStart(void)
1166{
1167#if defined(CONFIG_NET_MULTI)
Luca Ceresoli7cbd7482011-05-11 03:59:56 +00001168 printf("Using %s device\n", eth_get_name());
wdenke6466f62003-06-05 19:27:42 +00001169#endif
wdenk145d2c12004-04-15 21:48:45 +00001170 CDPSeq = 0;
1171 CDPOK = 0;
1172
1173 CDPNativeVLAN = htons(-1);
1174 CDPApplianceVLAN = htons(-1);
1175
Luca Ceresoli7cbd7482011-05-11 03:59:56 +00001176 NetSetTimeout(CDP_TIMEOUT, CDPTimeout);
1177 NetSetHandler(CDPDummyHandler);
wdenk145d2c12004-04-15 21:48:45 +00001178
1179 CDPSendTrigger();
1180}
Jon Loeligera9807e52007-07-10 11:05:02 -05001181#endif
wdenk145d2c12004-04-15 21:48:45 +00001182
Alessandro Rubinif119e3d2009-08-07 13:58:56 +02001183#ifdef CONFIG_IP_DEFRAG
1184/*
1185 * This function collects fragments in a single packet, according
1186 * to the algorithm in RFC815. It returns NULL or the pointer to
1187 * a complete packet, in static storage
1188 */
1189#ifndef CONFIG_NET_MAXDEFRAG
1190#define CONFIG_NET_MAXDEFRAG 16384
1191#endif
1192/*
1193 * MAXDEFRAG, above, is chosen in the config file and is real data
1194 * so we need to add the NFS overhead, which is more than TFTP.
1195 * To use sizeof in the internal unnamed structures, we need a real
1196 * instance (can't do "sizeof(struct rpc_t.u.reply))", unfortunately).
1197 * The compiler doesn't complain nor allocates the actual structure
1198 */
1199static struct rpc_t rpc_specimen;
1200#define IP_PKTSIZE (CONFIG_NET_MAXDEFRAG + sizeof(rpc_specimen.u.reply))
1201
1202#define IP_MAXUDP (IP_PKTSIZE - IP_HDR_SIZE_NO_UDP)
1203
1204/*
1205 * this is the packet being assembled, either data or frag control.
1206 * Fragments go by 8 bytes, so this union must be 8 bytes long
1207 */
1208struct hole {
1209 /* first_byte is address of this structure */
1210 u16 last_byte; /* last byte in this hole + 1 (begin of next hole) */
1211 u16 next_hole; /* index of next (in 8-b blocks), 0 == none */
1212 u16 prev_hole; /* index of prev, 0 == none */
1213 u16 unused;
1214};
1215
1216static IP_t *__NetDefragment(IP_t *ip, int *lenp)
1217{
1218 static uchar pkt_buff[IP_PKTSIZE] __attribute__((aligned(PKTALIGN)));
1219 static u16 first_hole, total_len;
1220 struct hole *payload, *thisfrag, *h, *newh;
1221 IP_t *localip = (IP_t *)pkt_buff;
1222 uchar *indata = (uchar *)ip;
1223 int offset8, start, len, done = 0;
1224 u16 ip_off = ntohs(ip->ip_off);
1225
1226 /* payload starts after IP header, this fragment is in there */
1227 payload = (struct hole *)(pkt_buff + IP_HDR_SIZE_NO_UDP);
1228 offset8 = (ip_off & IP_OFFS);
1229 thisfrag = payload + offset8;
1230 start = offset8 * 8;
1231 len = ntohs(ip->ip_len) - IP_HDR_SIZE_NO_UDP;
1232
1233 if (start + len > IP_MAXUDP) /* fragment extends too far */
1234 return NULL;
1235
1236 if (!total_len || localip->ip_id != ip->ip_id) {
1237 /* new (or different) packet, reset structs */
1238 total_len = 0xffff;
1239 payload[0].last_byte = ~0;
1240 payload[0].next_hole = 0;
1241 payload[0].prev_hole = 0;
1242 first_hole = 0;
1243 /* any IP header will work, copy the first we received */
1244 memcpy(localip, ip, IP_HDR_SIZE_NO_UDP);
1245 }
1246
1247 /*
1248 * What follows is the reassembly algorithm. We use the payload
1249 * array as a linked list of hole descriptors, as each hole starts
1250 * at a multiple of 8 bytes. However, last byte can be whatever value,
1251 * so it is represented as byte count, not as 8-byte blocks.
1252 */
1253
1254 h = payload + first_hole;
1255 while (h->last_byte < start) {
1256 if (!h->next_hole) {
1257 /* no hole that far away */
1258 return NULL;
1259 }
1260 h = payload + h->next_hole;
1261 }
1262
Fillod Stephanee7ade8b2010-06-11 19:26:43 +02001263 /* last fragment may be 1..7 bytes, the "+7" forces acceptance */
1264 if (offset8 + ((len + 7) / 8) <= h - payload) {
Alessandro Rubinif119e3d2009-08-07 13:58:56 +02001265 /* no overlap with holes (dup fragment?) */
1266 return NULL;
1267 }
1268
1269 if (!(ip_off & IP_FLAGS_MFRAG)) {
1270 /* no more fragmentss: truncate this (last) hole */
1271 total_len = start + len;
1272 h->last_byte = start + len;
1273 }
1274
1275 /*
1276 * There is some overlap: fix the hole list. This code doesn't
1277 * deal with a fragment that overlaps with two different holes
1278 * (thus being a superset of a previously-received fragment).
1279 */
1280
Luca Ceresoli7cbd7482011-05-11 03:59:56 +00001281 if ((h >= thisfrag) && (h->last_byte <= start + len)) {
Alessandro Rubinif119e3d2009-08-07 13:58:56 +02001282 /* complete overlap with hole: remove hole */
1283 if (!h->prev_hole && !h->next_hole) {
1284 /* last remaining hole */
1285 done = 1;
1286 } else if (!h->prev_hole) {
1287 /* first hole */
1288 first_hole = h->next_hole;
1289 payload[h->next_hole].prev_hole = 0;
1290 } else if (!h->next_hole) {
1291 /* last hole */
1292 payload[h->prev_hole].next_hole = 0;
1293 } else {
1294 /* in the middle of the list */
1295 payload[h->next_hole].prev_hole = h->prev_hole;
1296 payload[h->prev_hole].next_hole = h->next_hole;
1297 }
1298
1299 } else if (h->last_byte <= start + len) {
1300 /* overlaps with final part of the hole: shorten this hole */
1301 h->last_byte = start;
1302
1303 } else if (h >= thisfrag) {
1304 /* overlaps with initial part of the hole: move this hole */
1305 newh = thisfrag + (len / 8);
1306 *newh = *h;
1307 h = newh;
1308 if (h->next_hole)
1309 payload[h->next_hole].prev_hole = (h - payload);
1310 if (h->prev_hole)
1311 payload[h->prev_hole].next_hole = (h - payload);
1312 else
1313 first_hole = (h - payload);
1314
1315 } else {
1316 /* fragment sits in the middle: split the hole */
1317 newh = thisfrag + (len / 8);
1318 *newh = *h;
1319 h->last_byte = start;
1320 h->next_hole = (newh - payload);
1321 newh->prev_hole = (h - payload);
1322 if (newh->next_hole)
1323 payload[newh->next_hole].prev_hole = (newh - payload);
1324 }
1325
1326 /* finally copy this fragment and possibly return whole packet */
1327 memcpy((uchar *)thisfrag, indata + IP_HDR_SIZE_NO_UDP, len);
1328 if (!done)
1329 return NULL;
1330
1331 localip->ip_len = htons(total_len);
1332 *lenp = total_len + IP_HDR_SIZE_NO_UDP;
1333 return localip;
1334}
1335
1336static inline IP_t *NetDefragment(IP_t *ip, int *lenp)
1337{
1338 u16 ip_off = ntohs(ip->ip_off);
1339 if (!(ip_off & (IP_OFFS | IP_FLAGS_MFRAG)))
1340 return ip; /* not a fragment */
1341 return __NetDefragment(ip, lenp);
1342}
1343
1344#else /* !CONFIG_IP_DEFRAG */
1345
1346static inline IP_t *NetDefragment(IP_t *ip, int *lenp)
1347{
1348 u16 ip_off = ntohs(ip->ip_off);
1349 if (!(ip_off & (IP_OFFS | IP_FLAGS_MFRAG)))
1350 return ip; /* not a fragment */
1351 return NULL;
1352}
1353#endif
wdenk2d966952002-10-31 22:12:35 +00001354
1355void
wdenk145d2c12004-04-15 21:48:45 +00001356NetReceive(volatile uchar * inpkt, int len)
wdenk2d966952002-10-31 22:12:35 +00001357{
1358 Ethernet_t *et;
1359 IP_t *ip;
1360 ARP_t *arp;
1361 IPaddr_t tmp;
Luca Ceresoli428ab362011-04-18 06:19:50 +00001362 IPaddr_t src_ip;
wdenk2d966952002-10-31 22:12:35 +00001363 int x;
wdenk145d2c12004-04-15 21:48:45 +00001364 uchar *pkt;
Jon Loeliger54f35c22007-07-09 17:45:14 -05001365#if defined(CONFIG_CMD_CDP)
wdenk145d2c12004-04-15 21:48:45 +00001366 int iscdp;
1367#endif
1368 ushort cti = 0, vlanid = VLAN_NONE, myvlanid, mynvlanid;
wdenk2d966952002-10-31 22:12:35 +00001369
Robin Getz9e0a4d62009-07-23 03:01:03 -04001370 debug("packet received\n");
wdenk145d2c12004-04-15 21:48:45 +00001371
Mike Frysingerdc166032009-07-18 21:04:08 -04001372 NetRxPacket = inpkt;
1373 NetRxPacketLen = len;
wdenk145d2c12004-04-15 21:48:45 +00001374 et = (Ethernet_t *)inpkt;
1375
1376 /* too small packet? */
1377 if (len < ETHER_HDR_SIZE)
1378 return;
Rafal Jaworowski1f2c9a42007-12-27 18:19:02 +01001379
1380#ifdef CONFIG_API
1381 if (push_packet) {
1382 (*push_packet)(inpkt, len);
1383 return;
1384 }
1385#endif
wdenk145d2c12004-04-15 21:48:45 +00001386
Jon Loeliger54f35c22007-07-09 17:45:14 -05001387#if defined(CONFIG_CMD_CDP)
wdenk145d2c12004-04-15 21:48:45 +00001388 /* keep track if packet is CDP */
1389 iscdp = memcmp(et->et_dest, NetCDPAddr, 6) == 0;
1390#endif
1391
1392 myvlanid = ntohs(NetOurVLAN);
1393 if (myvlanid == (ushort)-1)
1394 myvlanid = VLAN_NONE;
1395 mynvlanid = ntohs(NetOurNativeVLAN);
1396 if (mynvlanid == (ushort)-1)
1397 mynvlanid = VLAN_NONE;
wdenk2d966952002-10-31 22:12:35 +00001398
1399 x = ntohs(et->et_protlen);
1400
Robin Getz9e0a4d62009-07-23 03:01:03 -04001401 debug("packet received\n");
wdenk145d2c12004-04-15 21:48:45 +00001402
wdenk2d966952002-10-31 22:12:35 +00001403 if (x < 1514) {
1404 /*
1405 * Got a 802 packet. Check the other protocol field.
1406 */
1407 x = ntohs(et->et_prot);
wdenk145d2c12004-04-15 21:48:45 +00001408
1409 ip = (IP_t *)(inpkt + E802_HDR_SIZE);
wdenk2d966952002-10-31 22:12:35 +00001410 len -= E802_HDR_SIZE;
wdenk145d2c12004-04-15 21:48:45 +00001411
1412 } else if (x != PROT_VLAN) { /* normal packet */
1413 ip = (IP_t *)(inpkt + ETHER_HDR_SIZE);
wdenk2d966952002-10-31 22:12:35 +00001414 len -= ETHER_HDR_SIZE;
wdenk145d2c12004-04-15 21:48:45 +00001415
1416 } else { /* VLAN packet */
1417 VLAN_Ethernet_t *vet = (VLAN_Ethernet_t *)et;
1418
Robin Getz9e0a4d62009-07-23 03:01:03 -04001419 debug("VLAN packet received\n");
1420
wdenk145d2c12004-04-15 21:48:45 +00001421 /* too small packet? */
1422 if (len < VLAN_ETHER_HDR_SIZE)
1423 return;
1424
1425 /* if no VLAN active */
1426 if ((ntohs(NetOurVLAN) & VLAN_IDMASK) == VLAN_NONE
Jon Loeliger54f35c22007-07-09 17:45:14 -05001427#if defined(CONFIG_CMD_CDP)
wdenk145d2c12004-04-15 21:48:45 +00001428 && iscdp == 0
1429#endif
1430 )
1431 return;
1432
1433 cti = ntohs(vet->vet_tag);
1434 vlanid = cti & VLAN_IDMASK;
1435 x = ntohs(vet->vet_type);
1436
1437 ip = (IP_t *)(inpkt + VLAN_ETHER_HDR_SIZE);
1438 len -= VLAN_ETHER_HDR_SIZE;
wdenk2d966952002-10-31 22:12:35 +00001439 }
1440
Robin Getz9e0a4d62009-07-23 03:01:03 -04001441 debug("Receive from protocol 0x%x\n", x);
wdenk2d966952002-10-31 22:12:35 +00001442
Jon Loeliger54f35c22007-07-09 17:45:14 -05001443#if defined(CONFIG_CMD_CDP)
wdenk145d2c12004-04-15 21:48:45 +00001444 if (iscdp) {
1445 CDPHandler((uchar *)ip, len);
1446 return;
1447 }
1448#endif
1449
1450 if ((myvlanid & VLAN_IDMASK) != VLAN_NONE) {
1451 if (vlanid == VLAN_NONE)
1452 vlanid = (mynvlanid & VLAN_IDMASK);
1453 /* not matched? */
1454 if (vlanid != (myvlanid & VLAN_IDMASK))
1455 return;
1456 }
1457
wdenk2d966952002-10-31 22:12:35 +00001458 switch (x) {
1459
1460 case PROT_ARP:
1461 /*
1462 * We have to deal with two types of ARP packets:
wdenk57b2d802003-06-27 21:31:46 +00001463 * - REQUEST packets will be answered by sending our
1464 * IP address - if we know it.
1465 * - REPLY packates are expected only after we asked
1466 * for the TFTP server's or the gateway's ethernet
1467 * address; so if we receive such a packet, we set
1468 * the server ethernet address
wdenk2d966952002-10-31 22:12:35 +00001469 */
Robin Getz9e0a4d62009-07-23 03:01:03 -04001470 debug("Got ARP\n");
1471
wdenk2d966952002-10-31 22:12:35 +00001472 arp = (ARP_t *)ip;
1473 if (len < ARP_HDR_SIZE) {
1474 printf("bad length %d < %d\n", len, ARP_HDR_SIZE);
1475 return;
1476 }
Luca Ceresolie8ccbb02011-05-04 02:40:43 +00001477 if (ntohs(arp->ar_hrd) != ARP_ETHER)
wdenk2d966952002-10-31 22:12:35 +00001478 return;
Luca Ceresolie8ccbb02011-05-04 02:40:43 +00001479 if (ntohs(arp->ar_pro) != PROT_IP)
wdenk2d966952002-10-31 22:12:35 +00001480 return;
Luca Ceresolie8ccbb02011-05-04 02:40:43 +00001481 if (arp->ar_hln != 6)
wdenk2d966952002-10-31 22:12:35 +00001482 return;
Luca Ceresolie8ccbb02011-05-04 02:40:43 +00001483 if (arp->ar_pln != 4)
wdenk2d966952002-10-31 22:12:35 +00001484 return;
wdenk2d966952002-10-31 22:12:35 +00001485
Luca Ceresolie8ccbb02011-05-04 02:40:43 +00001486 if (NetOurIP == 0)
wdenk2d966952002-10-31 22:12:35 +00001487 return;
wdenk2d966952002-10-31 22:12:35 +00001488
Luca Ceresolie8ccbb02011-05-04 02:40:43 +00001489 if (NetReadIP(&arp->ar_data[16]) != NetOurIP)
wdenk2d966952002-10-31 22:12:35 +00001490 return;
wdenk2d966952002-10-31 22:12:35 +00001491
1492 switch (ntohs(arp->ar_op)) {
Luca Ceresoli5c83a962011-05-11 03:59:54 +00001493 case ARPOP_REQUEST:
1494 /* reply with our IP address */
Robin Getz9e0a4d62009-07-23 03:01:03 -04001495 debug("Got ARP REQUEST, return our IP\n");
wdenk145d2c12004-04-15 21:48:45 +00001496 pkt = (uchar *)et;
1497 pkt += NetSetEther(pkt, et->et_src, PROT_ARP);
wdenk2d966952002-10-31 22:12:35 +00001498 arp->ar_op = htons(ARPOP_REPLY);
Luca Ceresoli7cbd7482011-05-11 03:59:56 +00001499 memcpy(&arp->ar_data[10], &arp->ar_data[0], 6);
wdenk2d966952002-10-31 22:12:35 +00001500 NetCopyIP(&arp->ar_data[16], &arp->ar_data[6]);
Luca Ceresoli7cbd7482011-05-11 03:59:56 +00001501 memcpy(&arp->ar_data[0], NetOurEther, 6);
1502 NetCopyIP(&arp->ar_data[6], &NetOurIP);
Luca Ceresoli5c83a962011-05-11 03:59:54 +00001503 (void) eth_send((uchar *)et,
1504 (pkt - (uchar *)et) + ARP_HDR_SIZE);
wdenk2d966952002-10-31 22:12:35 +00001505 return;
wdenke6466f62003-06-05 19:27:42 +00001506
1507 case ARPOP_REPLY: /* arp reply */
1508 /* are we waiting for a reply */
1509 if (!NetArpWaitPacketIP || !NetArpWaitPacketMAC)
1510 break;
Robin Getz470a6d42009-07-21 12:15:28 -04001511
1512#ifdef CONFIG_KEEP_SERVERADDR
1513 if (NetServerIP == NetArpWaitPacketIP) {
1514 char buf[20];
1515 sprintf(buf, "%pM", arp->ar_data);
1516 setenv("serveraddr", buf);
1517 }
1518#endif
1519
Robin Getz9e0a4d62009-07-23 03:01:03 -04001520 debug("Got ARP REPLY, set server/gtwy eth addr (%pM)\n",
Mike Frysinger79ca1b92009-02-11 18:23:48 -05001521 arp->ar_data);
wdenke6466f62003-06-05 19:27:42 +00001522
1523 tmp = NetReadIP(&arp->ar_data[6]);
1524
1525 /* matched waiting packet's address */
1526 if (tmp == NetArpWaitReplyIP) {
Robin Getz9e0a4d62009-07-23 03:01:03 -04001527 debug("Got it\n");
wdenke6466f62003-06-05 19:27:42 +00001528 /* save address for later use */
Luca Ceresoli5c83a962011-05-11 03:59:54 +00001529 memcpy(NetArpWaitPacketMAC,
1530 &arp->ar_data[0], 6);
wdenke6466f62003-06-05 19:27:42 +00001531
wdenkb8fb6192004-08-02 21:11:11 +00001532#ifdef CONFIG_NETCONSOLE
Luca Ceresoli428ab362011-04-18 06:19:50 +00001533 (*packetHandler)(0, 0, 0, 0, 0);
wdenkb8fb6192004-08-02 21:11:11 +00001534#endif
wdenke6466f62003-06-05 19:27:42 +00001535 /* modify header, and transmit it */
1536 memcpy(((Ethernet_t *)NetArpWaitTxPacket)->et_dest, NetArpWaitPacketMAC, 6);
Luca Ceresoli5c83a962011-05-11 03:59:54 +00001537 (void) eth_send(NetArpWaitTxPacket,
1538 NetArpWaitTxPacketSize);
wdenke6466f62003-06-05 19:27:42 +00001539
1540 /* no arp request pending now */
1541 NetArpWaitPacketIP = 0;
1542 NetArpWaitTxPacketSize = 0;
1543 NetArpWaitPacketMAC = NULL;
1544
1545 }
wdenk2d966952002-10-31 22:12:35 +00001546 return;
1547 default:
Luca Ceresoli5c83a962011-05-11 03:59:54 +00001548 debug("Unexpected ARP opcode 0x%x\n",
1549 ntohs(arp->ar_op));
wdenk2d966952002-10-31 22:12:35 +00001550 return;
1551 }
wdenkcb99da52005-01-12 00:15:14 +00001552 break;
wdenk2d966952002-10-31 22:12:35 +00001553
Peter Tyserf7a48ca2010-09-30 11:25:48 -05001554#ifdef CONFIG_CMD_RARP
wdenk2d966952002-10-31 22:12:35 +00001555 case PROT_RARP:
Robin Getz9e0a4d62009-07-23 03:01:03 -04001556 debug("Got RARP\n");
wdenk2d966952002-10-31 22:12:35 +00001557 arp = (ARP_t *)ip;
1558 if (len < ARP_HDR_SIZE) {
1559 printf("bad length %d < %d\n", len, ARP_HDR_SIZE);
1560 return;
1561 }
1562
1563 if ((ntohs(arp->ar_op) != RARPOP_REPLY) ||
1564 (ntohs(arp->ar_hrd) != ARP_ETHER) ||
1565 (ntohs(arp->ar_pro) != PROT_IP) ||
1566 (arp->ar_hln != 6) || (arp->ar_pln != 4)) {
1567
Luca Ceresoli7cbd7482011-05-11 03:59:56 +00001568 puts("invalid RARP header\n");
wdenk2d966952002-10-31 22:12:35 +00001569 } else {
Luca Ceresoli7cbd7482011-05-11 03:59:56 +00001570 NetCopyIP(&NetOurIP, &arp->ar_data[16]);
wdenk4989f872004-03-14 15:06:13 +00001571 if (NetServerIP == 0)
Luca Ceresoli7cbd7482011-05-11 03:59:56 +00001572 NetCopyIP(&NetServerIP, &arp->ar_data[6]);
1573 memcpy(NetServerEther, &arp->ar_data[0], 6);
wdenk2d966952002-10-31 22:12:35 +00001574
Luca Ceresoli428ab362011-04-18 06:19:50 +00001575 (*packetHandler)(0, 0, 0, 0, 0);
wdenk2d966952002-10-31 22:12:35 +00001576 }
1577 break;
Peter Tyserf7a48ca2010-09-30 11:25:48 -05001578#endif
wdenk2d966952002-10-31 22:12:35 +00001579 case PROT_IP:
Robin Getz9e0a4d62009-07-23 03:01:03 -04001580 debug("Got IP\n");
Alessandro Rubinif119e3d2009-08-07 13:58:56 +02001581 /* Before we start poking the header, make sure it is there */
wdenk2d966952002-10-31 22:12:35 +00001582 if (len < IP_HDR_SIZE) {
Robin Getz9e0a4d62009-07-23 03:01:03 -04001583 debug("len bad %d < %lu\n", len, (ulong)IP_HDR_SIZE);
wdenk2d966952002-10-31 22:12:35 +00001584 return;
1585 }
Alessandro Rubinif119e3d2009-08-07 13:58:56 +02001586 /* Check the packet length */
wdenk2d966952002-10-31 22:12:35 +00001587 if (len < ntohs(ip->ip_len)) {
1588 printf("len bad %d < %d\n", len, ntohs(ip->ip_len));
1589 return;
1590 }
1591 len = ntohs(ip->ip_len);
Robin Getz9e0a4d62009-07-23 03:01:03 -04001592 debug("len=%d, v=%02x\n", len, ip->ip_hl_v & 0xff);
1593
Alessandro Rubinif119e3d2009-08-07 13:58:56 +02001594 /* Can't deal with anything except IPv4 */
Luca Ceresolie8ccbb02011-05-04 02:40:43 +00001595 if ((ip->ip_hl_v & 0xf0) != 0x40)
wdenk2d966952002-10-31 22:12:35 +00001596 return;
Alessandro Rubinif119e3d2009-08-07 13:58:56 +02001597 /* Can't deal with IP options (headers != 20 bytes) */
Luca Ceresolie8ccbb02011-05-04 02:40:43 +00001598 if ((ip->ip_hl_v & 0x0f) > 0x05)
Remy Bohmerb9535782008-06-03 15:48:17 +02001599 return;
Alessandro Rubinif119e3d2009-08-07 13:58:56 +02001600 /* Check the Checksum of the header */
wdenk2d966952002-10-31 22:12:35 +00001601 if (!NetCksumOk((uchar *)ip, IP_HDR_SIZE_NO_UDP / 2)) {
Luca Ceresoli7cbd7482011-05-11 03:59:56 +00001602 puts("checksum bad\n");
wdenk2d966952002-10-31 22:12:35 +00001603 return;
1604 }
Alessandro Rubinif119e3d2009-08-07 13:58:56 +02001605 /* If it is not for us, ignore it */
wdenk2d966952002-10-31 22:12:35 +00001606 tmp = NetReadIP(&ip->ip_dst);
1607 if (NetOurIP && tmp != NetOurIP && tmp != 0xFFFFFFFF) {
David Updegraff7280da72007-06-11 10:41:07 -05001608#ifdef CONFIG_MCAST_TFTP
Wolfgang Denk627f5c32007-08-14 09:47:27 +02001609 if (Mcast_addr != tmp)
David Updegraff7280da72007-06-11 10:41:07 -05001610#endif
wdenk2d966952002-10-31 22:12:35 +00001611 return;
1612 }
Luca Ceresoli428ab362011-04-18 06:19:50 +00001613 /* Read source IP address for later use */
1614 src_ip = NetReadIP(&ip->ip_src);
wdenk2d966952002-10-31 22:12:35 +00001615 /*
Alessandro Rubinif119e3d2009-08-07 13:58:56 +02001616 * The function returns the unchanged packet if it's not
1617 * a fragment, and either the complete packet or NULL if
1618 * it is a fragment (if !CONFIG_IP_DEFRAG, it returns NULL)
1619 */
1620 if (!(ip = NetDefragment(ip, &len)))
1621 return;
1622 /*
wdenk2d966952002-10-31 22:12:35 +00001623 * watch for ICMP host redirects
1624 *
wdenk57b2d802003-06-27 21:31:46 +00001625 * There is no real handler code (yet). We just watch
1626 * for ICMP host redirect messages. In case anybody
1627 * sees these messages: please contact me
1628 * (wd@denx.de), or - even better - send me the
1629 * necessary fixes :-)
wdenk2d966952002-10-31 22:12:35 +00001630 *
wdenk57b2d802003-06-27 21:31:46 +00001631 * Note: in all cases where I have seen this so far
1632 * it was a problem with the router configuration,
1633 * for instance when a router was configured in the
1634 * BOOTP reply, but the TFTP server was on the same
1635 * subnet. So this is probably a warning that your
1636 * configuration might be wrong. But I'm not really
1637 * sure if there aren't any other situations.
wdenk2d966952002-10-31 22:12:35 +00001638 */
1639 if (ip->ip_p == IPPROTO_ICMP) {
1640 ICMP_t *icmph = (ICMP_t *)&(ip->udp_src);
1641
wdenke6466f62003-06-05 19:27:42 +00001642 switch (icmph->type) {
1643 case ICMP_REDIRECT:
wdenkcbc49a52005-05-03 14:12:25 +00001644 if (icmph->code != ICMP_REDIR_HOST)
1645 return;
Luca Ceresoli7cbd7482011-05-11 03:59:56 +00001646 printf(" ICMP Host Redirect to %pI4 ",
Luca Ceresoli5c83a962011-05-11 03:59:54 +00001647 &icmph->un.gateway);
Stefan Roesedfced812005-08-12 20:06:52 +02001648 return;
Jon Loeliger54f35c22007-07-09 17:45:14 -05001649#if defined(CONFIG_CMD_PING)
wdenke6466f62003-06-05 19:27:42 +00001650 case ICMP_ECHO_REPLY:
1651 /*
Luca Ceresoli5c83a962011-05-11 03:59:54 +00001652 * IP header OK. Pass the packet to the
1653 * current handler.
wdenke6466f62003-06-05 19:27:42 +00001654 */
1655 /* XXX point to ip packet */
Luca Ceresoli428ab362011-04-18 06:19:50 +00001656 (*packetHandler)((uchar *)ip, 0, src_ip, 0, 0);
Stefan Roesedfced812005-08-12 20:06:52 +02001657 return;
Ed Swarthout83109192007-03-07 12:14:50 -06001658 case ICMP_ECHO_REQUEST:
Luca Ceresoli5c83a962011-05-11 03:59:54 +00001659 debug("Got ICMP ECHO REQUEST, return %d bytes\n",
Luca Ceresoli428ab362011-04-18 06:19:50 +00001660 ETHER_HDR_SIZE + len);
Robin Getz9e0a4d62009-07-23 03:01:03 -04001661
Luca Ceresoli7cbd7482011-05-11 03:59:56 +00001662 memcpy(&et->et_dest[0], &et->et_src[0], 6);
1663 memcpy(&et->et_src[0], NetOurEther, 6);
Ed Swarthout83109192007-03-07 12:14:50 -06001664
1665 ip->ip_sum = 0;
1666 ip->ip_off = 0;
1667 NetCopyIP((void*)&ip->ip_dst, &ip->ip_src);
1668 NetCopyIP((void*)&ip->ip_src, &NetOurIP);
Luca Ceresoli5c83a962011-05-11 03:59:54 +00001669 ip->ip_sum = ~NetCksum((uchar *)ip,
1670 IP_HDR_SIZE_NO_UDP >> 1);
Ed Swarthout83109192007-03-07 12:14:50 -06001671
1672 icmph->type = ICMP_ECHO_REPLY;
1673 icmph->checksum = 0;
1674 icmph->checksum = ~NetCksum((uchar *)icmph,
Luca Ceresoli5c83a962011-05-11 03:59:54 +00001675 (len - IP_HDR_SIZE_NO_UDP) >> 1);
1676 (void) eth_send((uchar *)et,
1677 ETHER_HDR_SIZE + len);
Ed Swarthout83109192007-03-07 12:14:50 -06001678 return;
wdenke6466f62003-06-05 19:27:42 +00001679#endif
1680 default:
1681 return;
1682 }
wdenk2d966952002-10-31 22:12:35 +00001683 } else if (ip->ip_p != IPPROTO_UDP) { /* Only UDP packets */
1684 return;
1685 }
1686
Stefan Roesedfced812005-08-12 20:06:52 +02001687#ifdef CONFIG_UDP_CHECKSUM
1688 if (ip->udp_xsum != 0) {
Wolfgang Denk30b87322005-08-12 23:43:12 +02001689 ulong xsum;
Stefan Roesedfced812005-08-12 20:06:52 +02001690 ushort *sumptr;
1691 ushort sumlen;
1692
1693 xsum = ip->ip_p;
1694 xsum += (ntohs(ip->udp_len));
1695 xsum += (ntohl(ip->ip_src) >> 16) & 0x0000ffff;
1696 xsum += (ntohl(ip->ip_src) >> 0) & 0x0000ffff;
1697 xsum += (ntohl(ip->ip_dst) >> 16) & 0x0000ffff;
1698 xsum += (ntohl(ip->ip_dst) >> 0) & 0x0000ffff;
1699
1700 sumlen = ntohs(ip->udp_len);
1701 sumptr = (ushort *) &(ip->udp_src);
1702
1703 while (sumlen > 1) {
Wolfgang Denk30b87322005-08-12 23:43:12 +02001704 ushort sumdata;
Stefan Roesedfced812005-08-12 20:06:52 +02001705
1706 sumdata = *sumptr++;
1707 xsum += ntohs(sumdata);
1708 sumlen -= 2;
1709 }
1710 if (sumlen > 0) {
Wolfgang Denk30b87322005-08-12 23:43:12 +02001711 ushort sumdata;
Stefan Roesedfced812005-08-12 20:06:52 +02001712
1713 sumdata = *(unsigned char *) sumptr;
Wolfgang Denk30b87322005-08-12 23:43:12 +02001714 sumdata = (sumdata << 8) & 0xff00;
Stefan Roesedfced812005-08-12 20:06:52 +02001715 xsum += sumdata;
1716 }
1717 while ((xsum >> 16) != 0) {
Luca Ceresoli5c83a962011-05-11 03:59:54 +00001718 xsum = (xsum & 0x0000ffff) +
1719 ((xsum >> 16) & 0x0000ffff);
Stefan Roesedfced812005-08-12 20:06:52 +02001720 }
1721 if ((xsum != 0x00000000) && (xsum != 0x0000ffff)) {
Wolfgang Denk12cec0a2008-07-11 01:16:00 +02001722 printf(" UDP wrong checksum %08lx %08x\n",
1723 xsum, ntohs(ip->udp_xsum));
Stefan Roesedfced812005-08-12 20:06:52 +02001724 return;
1725 }
1726 }
1727#endif
1728
David Updegraff7280da72007-06-11 10:41:07 -05001729
wdenkb8fb6192004-08-02 21:11:11 +00001730#ifdef CONFIG_NETCONSOLE
Luca Ceresoli7cbd7482011-05-11 03:59:56 +00001731 nc_input_packet((uchar *)ip + IP_HDR_SIZE,
wdenkb8fb6192004-08-02 21:11:11 +00001732 ntohs(ip->udp_dst),
1733 ntohs(ip->udp_src),
1734 ntohs(ip->udp_len) - 8);
1735#endif
wdenk2d966952002-10-31 22:12:35 +00001736 /*
1737 * IP header OK. Pass the packet to the current handler.
1738 */
Luca Ceresoli7cbd7482011-05-11 03:59:56 +00001739 (*packetHandler)((uchar *)ip + IP_HDR_SIZE,
wdenk2d966952002-10-31 22:12:35 +00001740 ntohs(ip->udp_dst),
Luca Ceresoli428ab362011-04-18 06:19:50 +00001741 src_ip,
wdenk2d966952002-10-31 22:12:35 +00001742 ntohs(ip->udp_src),
1743 ntohs(ip->udp_len) - 8);
wdenk2d966952002-10-31 22:12:35 +00001744 break;
1745 }
1746}
1747
1748
1749/**********************************************************************/
1750
Luca Ceresoli7cbd7482011-05-11 03:59:56 +00001751static int net_check_prereq(proto_t protocol)
wdenk2d966952002-10-31 22:12:35 +00001752{
1753 switch (protocol) {
wdenk05939202004-04-18 17:39:38 +00001754 /* Fall through */
Jon Loeliger54f35c22007-07-09 17:45:14 -05001755#if defined(CONFIG_CMD_PING)
wdenke6466f62003-06-05 19:27:42 +00001756 case PING:
wdenk05939202004-04-18 17:39:38 +00001757 if (NetPingIP == 0) {
Luca Ceresoli7cbd7482011-05-11 03:59:56 +00001758 puts("*** ERROR: ping address not given\n");
wdenk05939202004-04-18 17:39:38 +00001759 return (1);
1760 }
1761 goto common;
wdenke6466f62003-06-05 19:27:42 +00001762#endif
Jon Loeliger54f35c22007-07-09 17:45:14 -05001763#if defined(CONFIG_CMD_SNTP)
wdenkb4ad9622005-04-01 00:25:43 +00001764 case SNTP:
1765 if (NetNtpServerIP == 0) {
Luca Ceresoli7cbd7482011-05-11 03:59:56 +00001766 puts("*** ERROR: NTP server address not given\n");
wdenkb4ad9622005-04-01 00:25:43 +00001767 return (1);
1768 }
1769 goto common;
1770#endif
Robin Getz82f0d232009-07-20 14:53:54 -04001771#if defined(CONFIG_CMD_DNS)
1772 case DNS:
1773 if (NetOurDNSIP == 0) {
1774 puts("*** ERROR: DNS server address not given\n");
1775 return 1;
1776 }
1777 goto common;
1778#endif
Jon Loeliger54f35c22007-07-09 17:45:14 -05001779#if defined(CONFIG_CMD_NFS)
wdenkbe9c1cb2004-02-24 02:00:03 +00001780 case NFS:
1781#endif
wdenk2d966952002-10-31 22:12:35 +00001782 case TFTP:
wdenk05939202004-04-18 17:39:38 +00001783 if (NetServerIP == 0) {
Luca Ceresoli7cbd7482011-05-11 03:59:56 +00001784 puts("*** ERROR: `serverip' not set\n");
wdenk05939202004-04-18 17:39:38 +00001785 return (1);
1786 }
Luca Ceresoli7cbd7482011-05-11 03:59:56 +00001787#if defined(CONFIG_CMD_PING) || defined(CONFIG_CMD_SNTP) || \
1788 defined(CONFIG_CMD_DNS)
1789common:
wdenke6466f62003-06-05 19:27:42 +00001790#endif
Simon Guinot00dceba2011-05-01 23:38:40 +00001791 /* Fall through */
wdenke6466f62003-06-05 19:27:42 +00001792
Simon Guinot00dceba2011-05-01 23:38:40 +00001793 case NETCONS:
wdenk05939202004-04-18 17:39:38 +00001794 if (NetOurIP == 0) {
Luca Ceresoli7cbd7482011-05-11 03:59:56 +00001795 puts("*** ERROR: `ipaddr' not set\n");
wdenk05939202004-04-18 17:39:38 +00001796 return (1);
1797 }
1798 /* Fall through */
wdenk2d966952002-10-31 22:12:35 +00001799
Peter Tyserf7a48ca2010-09-30 11:25:48 -05001800#ifdef CONFIG_CMD_RARP
wdenk2d966952002-10-31 22:12:35 +00001801 case RARP:
Peter Tyserf7a48ca2010-09-30 11:25:48 -05001802#endif
wdenk2d966952002-10-31 22:12:35 +00001803 case BOOTP:
wdenk145d2c12004-04-15 21:48:45 +00001804 case CDP:
Peter Tyserf7a48ca2010-09-30 11:25:48 -05001805 case DHCP:
Luca Ceresoli7cbd7482011-05-11 03:59:56 +00001806 if (memcmp(NetOurEther, "\0\0\0\0\0\0", 6) == 0) {
wdenk2d966952002-10-31 22:12:35 +00001807#ifdef CONFIG_NET_MULTI
Luca Ceresoli7cbd7482011-05-11 03:59:56 +00001808 extern int eth_get_dev_index(void);
1809 int num = eth_get_dev_index();
wdenk2d966952002-10-31 22:12:35 +00001810
wdenk05939202004-04-18 17:39:38 +00001811 switch (num) {
1812 case -1:
Luca Ceresoli7cbd7482011-05-11 03:59:56 +00001813 puts("*** ERROR: No ethernet found.\n");
wdenk2d966952002-10-31 22:12:35 +00001814 return (1);
wdenk05939202004-04-18 17:39:38 +00001815 case 0:
Luca Ceresoli7cbd7482011-05-11 03:59:56 +00001816 puts("*** ERROR: `ethaddr' not set\n");
wdenk2d966952002-10-31 22:12:35 +00001817 break;
wdenk05939202004-04-18 17:39:38 +00001818 default:
Luca Ceresoli7cbd7482011-05-11 03:59:56 +00001819 printf("*** ERROR: `eth%daddr' not set\n",
wdenk2d966952002-10-31 22:12:35 +00001820 num);
1821 break;
wdenk05939202004-04-18 17:39:38 +00001822 }
wdenk2d966952002-10-31 22:12:35 +00001823
Luca Ceresoli7cbd7482011-05-11 03:59:56 +00001824 NetStartAgain();
wdenk05939202004-04-18 17:39:38 +00001825 return (2);
wdenk2d966952002-10-31 22:12:35 +00001826#else
Luca Ceresoli7cbd7482011-05-11 03:59:56 +00001827 puts("*** ERROR: `ethaddr' not set\n");
wdenk05939202004-04-18 17:39:38 +00001828 return (1);
wdenk2d966952002-10-31 22:12:35 +00001829#endif
wdenk05939202004-04-18 17:39:38 +00001830 }
1831 /* Fall through */
1832 default:
1833 return (0);
wdenk2d966952002-10-31 22:12:35 +00001834 }
wdenk05939202004-04-18 17:39:38 +00001835 return (0); /* OK */
wdenk2d966952002-10-31 22:12:35 +00001836}
1837/**********************************************************************/
1838
1839int
1840NetCksumOk(uchar * ptr, int len)
1841{
1842 return !((NetCksum(ptr, len) + 1) & 0xfffe);
1843}
1844
1845
1846unsigned
1847NetCksum(uchar * ptr, int len)
1848{
1849 ulong xsum;
Stefan Roeseac553282005-08-31 12:55:50 +02001850 ushort *p = (ushort *)ptr;
wdenk2d966952002-10-31 22:12:35 +00001851
1852 xsum = 0;
1853 while (len-- > 0)
Wolfgang Denk3135c542005-08-26 01:36:03 +02001854 xsum += *p++;
wdenk2d966952002-10-31 22:12:35 +00001855 xsum = (xsum & 0xffff) + (xsum >> 16);
1856 xsum = (xsum & 0xffff) + (xsum >> 16);
1857 return (xsum & 0xffff);
1858}
1859
wdenk145d2c12004-04-15 21:48:45 +00001860int
1861NetEthHdrSize(void)
1862{
1863 ushort myvlanid;
1864
1865 myvlanid = ntohs(NetOurVLAN);
1866 if (myvlanid == (ushort)-1)
1867 myvlanid = VLAN_NONE;
wdenk2d966952002-10-31 22:12:35 +00001868
Luca Ceresoli5c83a962011-05-11 03:59:54 +00001869 return ((myvlanid & VLAN_IDMASK) == VLAN_NONE) ? ETHER_HDR_SIZE :
1870 VLAN_ETHER_HDR_SIZE;
wdenk145d2c12004-04-15 21:48:45 +00001871}
1872
1873int
wdenk2d966952002-10-31 22:12:35 +00001874NetSetEther(volatile uchar * xet, uchar * addr, uint prot)
1875{
1876 Ethernet_t *et = (Ethernet_t *)xet;
wdenk145d2c12004-04-15 21:48:45 +00001877 ushort myvlanid;
1878
1879 myvlanid = ntohs(NetOurVLAN);
1880 if (myvlanid == (ushort)-1)
1881 myvlanid = VLAN_NONE;
wdenk2d966952002-10-31 22:12:35 +00001882
Luca Ceresoli7cbd7482011-05-11 03:59:56 +00001883 memcpy(et->et_dest, addr, 6);
1884 memcpy(et->et_src, NetOurEther, 6);
wdenk145d2c12004-04-15 21:48:45 +00001885 if ((myvlanid & VLAN_IDMASK) == VLAN_NONE) {
wdenk2d966952002-10-31 22:12:35 +00001886 et->et_protlen = htons(prot);
wdenk145d2c12004-04-15 21:48:45 +00001887 return ETHER_HDR_SIZE;
1888 } else {
1889 VLAN_Ethernet_t *vet = (VLAN_Ethernet_t *)xet;
wdenk2d966952002-10-31 22:12:35 +00001890
wdenk145d2c12004-04-15 21:48:45 +00001891 vet->vet_vlan_type = htons(PROT_VLAN);
1892 vet->vet_tag = htons((0 << 5) | (myvlanid & VLAN_IDMASK));
1893 vet->vet_type = htons(prot);
1894 return VLAN_ETHER_HDR_SIZE;
1895 }
1896}
wdenk2d966952002-10-31 22:12:35 +00001897
1898void
1899NetSetIP(volatile uchar * xip, IPaddr_t dest, int dport, int sport, int len)
1900{
Olav Morkene8a02772009-01-23 12:56:26 +01001901 IP_t *ip = (IP_t *)xip;
wdenk2d966952002-10-31 22:12:35 +00001902
1903 /*
1904 * If the data is an odd number of bytes, zero the
1905 * byte after the last byte so that the checksum
1906 * will work.
1907 */
1908 if (len & 1)
1909 xip[IP_HDR_SIZE + len] = 0;
1910
1911 /*
1912 * Construct an IP and UDP header.
wdenk05939202004-04-18 17:39:38 +00001913 * (need to set no fragment bit - XXX)
wdenk2d966952002-10-31 22:12:35 +00001914 */
Luca Ceresoli5c83a962011-05-11 03:59:54 +00001915 /* IP_HDR_SIZE / 4 (not including UDP) */
1916 ip->ip_hl_v = 0x45;
wdenk2d966952002-10-31 22:12:35 +00001917 ip->ip_tos = 0;
1918 ip->ip_len = htons(IP_HDR_SIZE + len);
1919 ip->ip_id = htons(NetIPID++);
Peter Tyser0885bf02008-12-01 16:26:20 -06001920 ip->ip_off = htons(IP_FLAGS_DFRAG); /* Don't fragment */
wdenk2d966952002-10-31 22:12:35 +00001921 ip->ip_ttl = 255;
1922 ip->ip_p = 17; /* UDP */
1923 ip->ip_sum = 0;
Luca Ceresoli5c83a962011-05-11 03:59:54 +00001924 /* already in network byte order */
1925 NetCopyIP((void*)&ip->ip_src, &NetOurIP);
1926 /* - "" - */
1927 NetCopyIP((void*)&ip->ip_dst, &dest);
wdenk2d966952002-10-31 22:12:35 +00001928 ip->udp_src = htons(sport);
1929 ip->udp_dst = htons(dport);
1930 ip->udp_len = htons(8 + len);
1931 ip->udp_xsum = 0;
1932 ip->ip_sum = ~NetCksum((uchar *)ip, IP_HDR_SIZE_NO_UDP / 2);
1933}
1934
Luca Ceresoli7cbd7482011-05-11 03:59:56 +00001935void copy_filename(char *dst, const char *src, int size)
wdenk2d966952002-10-31 22:12:35 +00001936{
1937 if (*src && (*src == '"')) {
1938 ++src;
1939 --size;
1940 }
1941
Luca Ceresolie8ccbb02011-05-04 02:40:43 +00001942 while ((--size > 0) && *src && (*src != '"'))
wdenk2d966952002-10-31 22:12:35 +00001943 *dst++ = *src++;
wdenk2d966952002-10-31 22:12:35 +00001944 *dst = '\0';
1945}
1946
Luca Ceresoli5c83a962011-05-11 03:59:54 +00001947#if defined(CONFIG_CMD_NFS) || \
1948 defined(CONFIG_CMD_SNTP) || \
1949 defined(CONFIG_CMD_DNS)
Robin Getz82f0d232009-07-20 14:53:54 -04001950/*
Robin Getz55b50e92010-03-08 14:07:00 -05001951 * make port a little random (1024-17407)
1952 * This keeps the math somewhat trivial to compute, and seems to work with
1953 * all supported protocols/clients/servers
Robin Getz82f0d232009-07-20 14:53:54 -04001954 */
1955unsigned int random_port(void)
1956{
Robin Getz55b50e92010-03-08 14:07:00 -05001957 return 1024 + (get_timer(0) % 0x4000);
Robin Getz82f0d232009-07-20 14:53:54 -04001958}
1959#endif
1960
Luca Ceresoli7cbd7482011-05-11 03:59:56 +00001961void ip_to_string(IPaddr_t x, char *s)
wdenk2d966952002-10-31 22:12:35 +00001962{
Luca Ceresoli7cbd7482011-05-11 03:59:56 +00001963 x = ntohl(x);
1964 sprintf(s, "%d.%d.%d.%d",
1965 (int) ((x >> 24) & 0xff),
1966 (int) ((x >> 16) & 0xff),
1967 (int) ((x >> 8) & 0xff), (int) ((x >> 0) & 0xff)
wdenk05939202004-04-18 17:39:38 +00001968 );
wdenk2d966952002-10-31 22:12:35 +00001969}
1970
wdenk145d2c12004-04-15 21:48:45 +00001971void VLAN_to_string(ushort x, char *s)
1972{
1973 x = ntohs(x);
1974
1975 if (x == (ushort)-1)
1976 x = VLAN_NONE;
1977
1978 if (x == VLAN_NONE)
1979 strcpy(s, "none");
1980 else
1981 sprintf(s, "%d", x & VLAN_IDMASK);
1982}
1983
Mike Frysinger89c73922010-10-20 07:16:48 -04001984ushort string_to_VLAN(const char *s)
wdenk145d2c12004-04-15 21:48:45 +00001985{
1986 ushort id;
1987
1988 if (s == NULL)
wdenk656140b2004-04-25 13:18:40 +00001989 return htons(VLAN_NONE);
wdenk145d2c12004-04-15 21:48:45 +00001990
1991 if (*s < '0' || *s > '9')
1992 id = VLAN_NONE;
1993 else
1994 id = (ushort)simple_strtoul(s, NULL, 10);
1995
wdenk656140b2004-04-25 13:18:40 +00001996 return htons(id);
wdenk145d2c12004-04-15 21:48:45 +00001997}
1998
wdenk145d2c12004-04-15 21:48:45 +00001999ushort getenv_VLAN(char *var)
2000{
2001 return (string_to_VLAN(getenv(var)));
2002}