blob: c778a8065619ac2a2e68c75b617102c1869d9a98 [file] [log] [blame]
wdenk2d966952002-10-31 22:12:35 +00001/*
2 * Copied from Linux Monitor (LiMon) - Networking.
3 *
4 * Copyright 1994 - 2000 Neil Russell.
5 * (See License)
6 * Copyright 2000 Roland Borde
7 * Copyright 2000 Paolo Scaffardi
8 * Copyright 2000-2002 Wolfgang Denk, wd@denx.de
9 */
10
11/*
12 * General Desription:
13 *
14 * The user interface supports commands for BOOTP, RARP, and TFTP.
15 * Also, we support ARP internally. Depending on available data,
16 * these interact as follows:
17 *
18 * BOOTP:
19 *
20 * Prerequisites: - own ethernet address
21 * We want: - own IP address
22 * - TFTP server IP address
23 * - name of bootfile
24 * Next step: ARP
25 *
26 * RARP:
27 *
28 * Prerequisites: - own ethernet address
29 * We want: - own IP address
30 * - TFTP server IP address
31 * Next step: ARP
32 *
33 * ARP:
34 *
35 * Prerequisites: - own ethernet address
36 * - own IP address
37 * - TFTP server IP address
38 * We want: - TFTP server ethernet address
39 * Next step: TFTP
40 *
41 * DHCP:
42 *
Wolfgang Denkb2f50802005-08-12 23:43:12 +020043 * Prerequisites: - own ethernet address
44 * We want: - IP, Netmask, ServerIP, Gateway IP
45 * - bootfilename, lease time
46 * Next step: - TFTP
wdenk2d966952002-10-31 22:12:35 +000047 *
48 * TFTP:
49 *
50 * Prerequisites: - own ethernet address
51 * - own IP address
52 * - TFTP server IP address
53 * - TFTP server ethernet address
54 * - name of bootfile (if unknown, we use a default name
55 * derived from our own IP address)
56 * We want: - load the boot file
57 * Next step: none
wdenkcbd8a352004-02-24 02:00:03 +000058 *
59 * NFS:
60 *
61 * Prerequisites: - own ethernet address
62 * - own IP address
63 * - name of bootfile (if unknown, we use a default name
64 * derived from our own IP address)
65 * We want: - load the boot file
66 * Next step: none
wdenkea287de2005-04-01 00:25:43 +000067 *
68 * SNTP:
69 *
Wolfgang Denkb2f50802005-08-12 23:43:12 +020070 * Prerequisites: - own ethernet address
wdenkea287de2005-04-01 00:25:43 +000071 * - own IP address
72 * We want: - network time
73 * Next step: none
wdenk2d966952002-10-31 22:12:35 +000074 */
75
76
77#include <common.h>
78#include <watchdog.h>
79#include <command.h>
80#include <net.h>
81#include "bootp.h"
82#include "tftp.h"
Peter Tyserbf6cb242010-09-30 11:25:48 -050083#ifdef CONFIG_CMD_RARP
wdenk2d966952002-10-31 22:12:35 +000084#include "rarp.h"
Peter Tyserbf6cb242010-09-30 11:25:48 -050085#endif
wdenkcbd8a352004-02-24 02:00:03 +000086#include "nfs.h"
wdenkfc3e2162003-10-08 22:33:00 +000087#ifdef CONFIG_STATUS_LED
88#include <status_led.h>
89#include <miiphy.h>
90#endif
Jon Loeliger643d1ab2007-07-09 17:45:14 -050091#if defined(CONFIG_CMD_SNTP)
wdenkea287de2005-04-01 00:25:43 +000092#include "sntp.h"
93#endif
Peter Tyser561858e2008-11-03 09:30:59 -060094#if defined(CONFIG_CDP_VERSION)
95#include <timestamp.h>
96#endif
Robin Getz1a32bf42009-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 Denkd87080b2006-03-31 18:32:53 +0200101DECLARE_GLOBAL_DATA_PTR;
102
Guennadi Liakhovetski40cb90e2008-04-03 17:04:19 +0200103#ifndef CONFIG_ARP_TIMEOUT
Luca Ceresoli3e38e422011-05-11 03:59:54 +0000104/* Milliseconds before trying ARP again */
105# define ARP_TIMEOUT 5000UL
wdenk73a8b272003-06-05 19:27:42 +0000106#else
Bartlomiej Sieka49f3bdb2008-10-01 15:26:28 +0200107# define ARP_TIMEOUT CONFIG_ARP_TIMEOUT
Guennadi Liakhovetski40cb90e2008-04-03 17:04:19 +0200108#endif
109
110
111#ifndef CONFIG_NET_RETRY_COUNT
112# define ARP_TIMEOUT_COUNT 5 /* # of timeouts before giving up */
113#else
114# define ARP_TIMEOUT_COUNT CONFIG_NET_RETRY_COUNT
wdenk73a8b272003-06-05 19:27:42 +0000115#endif
116
wdenk2d966952002-10-31 22:12:35 +0000117/** BOOTP EXTENTIONS **/
118
Luca Ceresoli3e38e422011-05-11 03:59:54 +0000119/* Our subnet mask (0=unknown) */
Luca Ceresolic586ce62011-05-11 03:59:55 +0000120IPaddr_t NetOurSubnetMask;
Luca Ceresoli3e38e422011-05-11 03:59:54 +0000121/* Our gateways IP address */
Luca Ceresolic586ce62011-05-11 03:59:55 +0000122IPaddr_t NetOurGatewayIP;
Luca Ceresoli3e38e422011-05-11 03:59:54 +0000123/* Our DNS IP address */
Luca Ceresolic586ce62011-05-11 03:59:55 +0000124IPaddr_t NetOurDNSIP;
Jon Loeliger1fe80d72007-07-09 22:08:34 -0500125#if defined(CONFIG_BOOTP_DNS2)
Luca Ceresoli3e38e422011-05-11 03:59:54 +0000126/* Our 2nd DNS IP address */
Luca Ceresolic586ce62011-05-11 03:59:55 +0000127IPaddr_t NetOurDNS2IP;
stroesefe389a82003-08-28 14:17:32 +0000128#endif
Luca Ceresoli3e38e422011-05-11 03:59:54 +0000129/* Our NIS domain */
Luca Ceresolic586ce62011-05-11 03:59:55 +0000130char NetOurNISDomain[32] = {0,};
Luca Ceresoli3e38e422011-05-11 03:59:54 +0000131/* Our hostname */
Luca Ceresolic586ce62011-05-11 03:59:55 +0000132char NetOurHostName[32] = {0,};
Luca Ceresoli3e38e422011-05-11 03:59:54 +0000133/* Our bootpath */
Luca Ceresolic586ce62011-05-11 03:59:55 +0000134char NetOurRootPath[64] = {0,};
Luca Ceresoli3e38e422011-05-11 03:59:54 +0000135/* Our bootfile size in blocks */
Luca Ceresolic586ce62011-05-11 03:59:55 +0000136ushort NetBootFileSize;
wdenk2d966952002-10-31 22:12:35 +0000137
David Updegraff53a5c422007-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 Ceresoli3e38e422011-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 Ceresolic586ce62011-05-11 03:59:55 +0000149uchar NetServerEther[6];
Luca Ceresoli3e38e422011-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 Ceresolic586ce62011-05-11 03:59:55 +0000161uchar NetBcastAddr[6] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
162uchar NetEtherNullAddr[6];
Rafal Jaworowskif85b6072007-12-27 18:19:02 +0100163#ifdef CONFIG_API
164void (*push_packet)(volatile void *, int len) = 0;
165#endif
Jon Loeliger643d1ab2007-07-09 17:45:14 -0500166#if defined(CONFIG_CMD_CDP)
Luca Ceresoli3e38e422011-05-11 03:59:54 +0000167/* Ethernet bcast address */
Luca Ceresolic586ce62011-05-11 03:59:55 +0000168uchar NetCDPAddr[6] = { 0x01, 0x00, 0x0c, 0xcc, 0xcc, 0xcc };
wdenka3d991b2004-04-15 21:48:45 +0000169#endif
Luca Ceresoli3e38e422011-05-11 03:59:54 +0000170/* Network loop state */
171int NetState;
Luca Ceresoli3e38e422011-05-11 03:59:54 +0000172/* Tried all network devices */
Luca Ceresolic586ce62011-05-11 03:59:55 +0000173int NetRestartWrap;
Luca Ceresoli3e38e422011-05-11 03:59:54 +0000174/* Network loop restarted */
Luca Ceresolic586ce62011-05-11 03:59:55 +0000175static int NetRestarted;
Luca Ceresoli3e38e422011-05-11 03:59:54 +0000176/* At least one device configured */
Luca Ceresolic586ce62011-05-11 03:59:55 +0000177static int NetDevExists;
wdenk2d966952002-10-31 22:12:35 +0000178
wdenk6e592382004-04-18 17:39:38 +0000179/* XXX in both little & big endian machines 0xFFFF == ntohs(-1) */
Luca Ceresoli3e38e422011-05-11 03:59:54 +0000180/* default is without VLAN */
181ushort NetOurVLAN = 0xFFFF;
182/* ditto */
183ushort NetOurNativeVLAN = 0xFFFF;
wdenka3d991b2004-04-15 21:48:45 +0000184
Luca Ceresoli3e38e422011-05-11 03:59:54 +0000185/* Boot File name */
186char BootFile[128];
wdenk2d966952002-10-31 22:12:35 +0000187
Jon Loeliger643d1ab2007-07-09 17:45:14 -0500188#if defined(CONFIG_CMD_PING)
Luca Ceresoli3e38e422011-05-11 03:59:54 +0000189/* the ip address to ping */
190IPaddr_t NetPingIP;
wdenk73a8b272003-06-05 19:27:42 +0000191
192static void PingStart(void);
193#endif
194
Jon Loeliger643d1ab2007-07-09 17:45:14 -0500195#if defined(CONFIG_CMD_CDP)
wdenka3d991b2004-04-15 21:48:45 +0000196static void CDPStart(void);
197#endif
198
Jon Loeliger643d1ab2007-07-09 17:45:14 -0500199#if defined(CONFIG_CMD_SNTP)
Luca Ceresoli3e38e422011-05-11 03:59:54 +0000200/* NTP server IP address */
201IPaddr_t NetNtpServerIP;
202/* offset time from UTC */
Luca Ceresolic586ce62011-05-11 03:59:55 +0000203int NetTimeOffset;
wdenkea287de2005-04-01 00:25:43 +0000204#endif
205
wdenk68ceb292004-08-02 21:11:11 +0000206#ifdef CONFIG_NETCONSOLE
207void NcStart(void);
208int nc_input_packet(uchar *pkt, unsigned dest, unsigned src, unsigned len);
209#endif
210
wdenk2d966952002-10-31 22:12:35 +0000211volatile uchar PktBuf[(PKTBUFSRX+1) * PKTSIZE_ALIGN + PKTALIGN];
212
Luca Ceresoli3e38e422011-05-11 03:59:54 +0000213/* Receive packet */
214volatile uchar *NetRxPackets[PKTBUFSRX];
wdenk2d966952002-10-31 22:12:35 +0000215
Luca Ceresoli3e38e422011-05-11 03:59:54 +0000216/* Current RX packet handler */
217static rxhand_f *packetHandler;
Simon Glass4793ee62011-10-24 18:00:01 +0000218static rxhand_icmp_f *packet_icmp_handler; /* Current ICMP rx handler */
Luca Ceresoli3e38e422011-05-11 03:59:54 +0000219/* Current timeout handler */
220static thand_f *timeHandler;
221/* Time base value */
222static ulong timeStart;
223/* Current timeout value */
224static ulong timeDelta;
225/* THE transmit packet */
Luca Ceresolic586ce62011-05-11 03:59:55 +0000226volatile uchar *NetTxPacket;
wdenk2d966952002-10-31 22:12:35 +0000227
Simon Glasse4bf0c52011-10-24 18:00:02 +0000228static int net_check_prereq(enum proto_t protocol);
wdenk2d966952002-10-31 22:12:35 +0000229
Remy Bohmer67b96e82009-10-28 22:13:39 +0100230static int NetTryCount;
231
wdenk2d966952002-10-31 22:12:35 +0000232/**********************************************************************/
wdenk73a8b272003-06-05 19:27:42 +0000233
234IPaddr_t NetArpWaitPacketIP;
235IPaddr_t NetArpWaitReplyIP;
Luca Ceresoli3e38e422011-05-11 03:59:54 +0000236/* MAC address of waiting packet's destination */
237uchar *NetArpWaitPacketMAC;
238/* THE transmit packet */
239uchar *NetArpWaitTxPacket;
wdenk73a8b272003-06-05 19:27:42 +0000240int NetArpWaitTxPacketSize;
Wolfgang Denk53677ef2008-05-20 16:00:29 +0200241uchar NetArpWaitPacketBuf[PKTSIZE_ALIGN + PKTALIGN];
wdenk73a8b272003-06-05 19:27:42 +0000242ulong NetArpWaitTimerStart;
243int NetArpWaitTry;
244
Luca Ceresoli4f63acd2011-05-11 03:59:56 +0000245void ArpRequest(void)
wdenk73a8b272003-06-05 19:27:42 +0000246{
247 int i;
248 volatile uchar *pkt;
wdenk6e592382004-04-18 17:39:38 +0000249 ARP_t *arp;
wdenk73a8b272003-06-05 19:27:42 +0000250
Robin Getz0ebf04c2009-07-23 03:01:03 -0400251 debug("ARP broadcast %d\n", NetArpWaitTry);
252
wdenk73a8b272003-06-05 19:27:42 +0000253 pkt = NetTxPacket;
254
Luca Ceresoli4f63acd2011-05-11 03:59:56 +0000255 pkt += NetSetEther(pkt, NetBcastAddr, PROT_ARP);
wdenk73a8b272003-06-05 19:27:42 +0000256
wdenk6e592382004-04-18 17:39:38 +0000257 arp = (ARP_t *) pkt;
wdenk73a8b272003-06-05 19:27:42 +0000258
Luca Ceresoli4f63acd2011-05-11 03:59:56 +0000259 arp->ar_hrd = htons(ARP_ETHER);
260 arp->ar_pro = htons(PROT_IP);
wdenk73a8b272003-06-05 19:27:42 +0000261 arp->ar_hln = 6;
262 arp->ar_pln = 4;
Luca Ceresoli4f63acd2011-05-11 03:59:56 +0000263 arp->ar_op = htons(ARPOP_REQUEST);
wdenk73a8b272003-06-05 19:27:42 +0000264
Luca Ceresoli3e38e422011-05-11 03:59:54 +0000265 /* source ET addr */
Luca Ceresoli4f63acd2011-05-11 03:59:56 +0000266 memcpy(&arp->ar_data[0], NetOurEther, 6);
Luca Ceresoli3e38e422011-05-11 03:59:54 +0000267 /* source IP addr */
Luca Ceresoli4f63acd2011-05-11 03:59:56 +0000268 NetWriteIP((uchar *) &arp->ar_data[6], NetOurIP);
wdenk6e592382004-04-18 17:39:38 +0000269 for (i = 10; i < 16; ++i) {
Luca Ceresoli3e38e422011-05-11 03:59:54 +0000270 /* dest ET addr = 0 */
271 arp->ar_data[i] = 0;
wdenk73a8b272003-06-05 19:27:42 +0000272 }
273
wdenk6e592382004-04-18 17:39:38 +0000274 if ((NetArpWaitPacketIP & NetOurSubnetMask) !=
275 (NetOurIP & NetOurSubnetMask)) {
276 if (NetOurGatewayIP == 0) {
Luca Ceresoli4f63acd2011-05-11 03:59:56 +0000277 puts("## Warning: gatewayip needed but not set\n");
Wolfgang Denkd509b812006-03-12 01:13:30 +0100278 NetArpWaitReplyIP = NetArpWaitPacketIP;
279 } else {
280 NetArpWaitReplyIP = NetOurGatewayIP;
wdenk6e592382004-04-18 17:39:38 +0000281 }
wdenk6e592382004-04-18 17:39:38 +0000282 } else {
283 NetArpWaitReplyIP = NetArpWaitPacketIP;
284 }
wdenk73a8b272003-06-05 19:27:42 +0000285
Luca Ceresoli4f63acd2011-05-11 03:59:56 +0000286 NetWriteIP((uchar *) &arp->ar_data[16], NetArpWaitReplyIP);
287 (void) eth_send(NetTxPacket, (pkt - NetTxPacket) + ARP_HDR_SIZE);
wdenk73a8b272003-06-05 19:27:42 +0000288}
289
290void ArpTimeoutCheck(void)
291{
292 ulong t;
293
294 if (!NetArpWaitPacketIP)
295 return;
296
297 t = get_timer(0);
298
299 /* check for arp timeout */
Bartlomiej Sieka49f3bdb2008-10-01 15:26:28 +0200300 if ((t - NetArpWaitTimerStart) > ARP_TIMEOUT) {
wdenk73a8b272003-06-05 19:27:42 +0000301 NetArpWaitTry++;
302
303 if (NetArpWaitTry >= ARP_TIMEOUT_COUNT) {
Luca Ceresoli4f63acd2011-05-11 03:59:56 +0000304 puts("\nARP Retry count exceeded; starting again\n");
wdenk73a8b272003-06-05 19:27:42 +0000305 NetArpWaitTry = 0;
306 NetStartAgain();
307 } else {
308 NetArpWaitTimerStart = t;
309 ArpRequest();
310 }
311 }
312}
313
Simon Glasse4bf0c52011-10-24 18:00:02 +0000314static void NetInitLoop(enum proto_t protocol)
Heiko Schocher2f70c492009-02-10 09:38:52 +0100315{
Luca Ceresolic586ce62011-05-11 03:59:55 +0000316 static int env_changed_id;
Heiko Schocher2f70c492009-02-10 09:38:52 +0100317 bd_t *bd = gd->bd;
Luca Ceresoli4f63acd2011-05-11 03:59:56 +0000318 int env_id = get_env_id();
Heiko Schocher2f70c492009-02-10 09:38:52 +0100319
320 /* update only when the environment has changed */
Michael Zaidman3c172c42009-04-04 01:43:00 +0300321 if (env_changed_id != env_id) {
Enric Balletbo i Serra23a70bf2011-05-31 21:01:47 +0000322 NetOurIP = getenv_IPaddr("ipaddr");
323 NetCopyIP(&bd->bi_ip_addr, &NetOurIP);
Luca Ceresoli4f63acd2011-05-11 03:59:56 +0000324 NetOurGatewayIP = getenv_IPaddr("gatewayip");
325 NetOurSubnetMask = getenv_IPaddr("netmask");
326 NetServerIP = getenv_IPaddr("serverip");
Heiko Schocher2f70c492009-02-10 09:38:52 +0100327 NetOurNativeVLAN = getenv_VLAN("nvlan");
Michael Zaidman3c172c42009-04-04 01:43:00 +0300328 NetOurVLAN = getenv_VLAN("vlan");
Robin Getz1a32bf42009-07-20 14:53:54 -0400329#if defined(CONFIG_CMD_DNS)
330 NetOurDNSIP = getenv_IPaddr("dnsip");
331#endif
Michael Zaidman3c172c42009-04-04 01:43:00 +0300332 env_changed_id = env_id;
Heiko Schocher2f70c492009-02-10 09:38:52 +0100333 }
Michael Zaidman3c172c42009-04-04 01:43:00 +0300334
Heiko Schocherda954272009-04-28 08:36:11 +0200335 return;
Heiko Schocher2f70c492009-02-10 09:38:52 +0100336}
337
wdenk73a8b272003-06-05 19:27:42 +0000338/**********************************************************************/
wdenk2d966952002-10-31 22:12:35 +0000339/*
340 * Main network processing loop.
341 */
342
Simon Glasse4bf0c52011-10-24 18:00:02 +0000343int NetLoop(enum proto_t protocol)
wdenk2d966952002-10-31 22:12:35 +0000344{
wdenk2d966952002-10-31 22:12:35 +0000345 bd_t *bd = gd->bd;
Simon Glass4793ee62011-10-24 18:00:01 +0000346 int ret = -1;
wdenk2d966952002-10-31 22:12:35 +0000347
wdenk2d966952002-10-31 22:12:35 +0000348 NetRestarted = 0;
349 NetDevExists = 0;
wdenk2d966952002-10-31 22:12:35 +0000350
wdenk73a8b272003-06-05 19:27:42 +0000351 /* XXX problem with bss workaround */
352 NetArpWaitPacketMAC = NULL;
353 NetArpWaitTxPacket = NULL;
354 NetArpWaitPacketIP = 0;
355 NetArpWaitReplyIP = 0;
356 NetArpWaitTxPacket = NULL;
357 NetTxPacket = NULL;
Remy Bohmer67b96e82009-10-28 22:13:39 +0100358 NetTryCount = 1;
wdenk73a8b272003-06-05 19:27:42 +0000359
wdenk2d966952002-10-31 22:12:35 +0000360 if (!NetTxPacket) {
361 int i;
wdenk2d966952002-10-31 22:12:35 +0000362 /*
363 * Setup packet buffers, aligned correctly.
364 */
365 NetTxPacket = &PktBuf[0] + (PKTALIGN - 1);
366 NetTxPacket -= (ulong)NetTxPacket % PKTALIGN;
Luca Ceresolid3c65b02011-05-04 02:40:43 +0000367 for (i = 0; i < PKTBUFSRX; i++)
wdenk2d966952002-10-31 22:12:35 +0000368 NetRxPackets[i] = NetTxPacket + (i+1)*PKTSIZE_ALIGN;
wdenk73a8b272003-06-05 19:27:42 +0000369 }
370
371 if (!NetArpWaitTxPacket) {
372 NetArpWaitTxPacket = &NetArpWaitPacketBuf[0] + (PKTALIGN - 1);
373 NetArpWaitTxPacket -= (ulong)NetArpWaitTxPacket % PKTALIGN;
374 NetArpWaitTxPacketSize = 0;
wdenk2d966952002-10-31 22:12:35 +0000375 }
376
377 eth_halt();
wdenka3d991b2004-04-15 21:48:45 +0000378 eth_set_current();
wdenkb1bf6f22005-04-03 14:52:59 +0000379 if (eth_init(bd) < 0) {
380 eth_halt();
Luca Ceresoli92895de2011-05-04 02:40:45 +0000381 return -1;
wdenkb1bf6f22005-04-03 14:52:59 +0000382 }
wdenk2d966952002-10-31 22:12:35 +0000383
384restart:
Luca Ceresoli4f63acd2011-05-11 03:59:56 +0000385 memcpy(NetOurEther, eth_get_dev()->enetaddr, 6);
wdenk2d966952002-10-31 22:12:35 +0000386
387 NetState = NETLOOP_CONTINUE;
388
389 /*
390 * Start the ball rolling with the given start function. From
391 * here on, this code is a state machine driven by received
392 * packets and timer events.
393 */
Heiko Schocher2f70c492009-02-10 09:38:52 +0100394 NetInitLoop(protocol);
wdenk2d966952002-10-31 22:12:35 +0000395
Luca Ceresoli4f63acd2011-05-11 03:59:56 +0000396 switch (net_check_prereq(protocol)) {
wdenk2d966952002-10-31 22:12:35 +0000397 case 1:
398 /* network not configured */
wdenkb1bf6f22005-04-03 14:52:59 +0000399 eth_halt();
Luca Ceresoli92895de2011-05-04 02:40:45 +0000400 return -1;
wdenk2d966952002-10-31 22:12:35 +0000401
wdenk2d966952002-10-31 22:12:35 +0000402 case 2:
403 /* network device not configured */
404 break;
wdenk2d966952002-10-31 22:12:35 +0000405
406 case 0:
wdenk2d966952002-10-31 22:12:35 +0000407 NetDevExists = 1;
Simon Glasse4bf0c52011-10-24 18:00:02 +0000408 NetBootFileXferSize = 0;
wdenk2d966952002-10-31 22:12:35 +0000409 switch (protocol) {
Simon Glasse4bf0c52011-10-24 18:00:02 +0000410 case TFTPGET:
wdenk2d966952002-10-31 22:12:35 +0000411 /* always use ARP to get server ethernet address */
Simon Glasse4bf0c52011-10-24 18:00:02 +0000412 TftpStart(protocol);
wdenk2d966952002-10-31 22:12:35 +0000413 break;
Luca Ceresoli7a83af02011-05-17 00:03:40 +0000414#ifdef CONFIG_CMD_TFTPSRV
415 case TFTPSRV:
416 TftpStartServer();
417 break;
418#endif
Jon Loeliger643d1ab2007-07-09 17:45:14 -0500419#if defined(CONFIG_CMD_DHCP)
wdenk2d966952002-10-31 22:12:35 +0000420 case DHCP:
wdenkd407bf52004-10-11 22:51:13 +0000421 BootpTry = 0;
Michael Zaidman09133f82009-07-14 23:37:12 +0300422 NetOurIP = 0;
wdenk2d966952002-10-31 22:12:35 +0000423 DhcpRequest(); /* Basically same as BOOTP */
424 break;
Jon Loeliger610f2e92007-07-10 11:05:02 -0500425#endif
wdenk2d966952002-10-31 22:12:35 +0000426
427 case BOOTP:
428 BootpTry = 0;
Michael Zaidman09133f82009-07-14 23:37:12 +0300429 NetOurIP = 0;
Luca Ceresoli4f63acd2011-05-11 03:59:56 +0000430 BootpRequest();
wdenk2d966952002-10-31 22:12:35 +0000431 break;
432
Peter Tyserbf6cb242010-09-30 11:25:48 -0500433#if defined(CONFIG_CMD_RARP)
wdenk2d966952002-10-31 22:12:35 +0000434 case RARP:
435 RarpTry = 0;
Michael Zaidman09133f82009-07-14 23:37:12 +0300436 NetOurIP = 0;
Luca Ceresoli4f63acd2011-05-11 03:59:56 +0000437 RarpRequest();
wdenk2d966952002-10-31 22:12:35 +0000438 break;
Peter Tyserbf6cb242010-09-30 11:25:48 -0500439#endif
Jon Loeliger643d1ab2007-07-09 17:45:14 -0500440#if defined(CONFIG_CMD_PING)
wdenk73a8b272003-06-05 19:27:42 +0000441 case PING:
442 PingStart();
443 break;
444#endif
Jon Loeliger643d1ab2007-07-09 17:45:14 -0500445#if defined(CONFIG_CMD_NFS)
wdenkcbd8a352004-02-24 02:00:03 +0000446 case NFS:
447 NfsStart();
448 break;
449#endif
Jon Loeliger643d1ab2007-07-09 17:45:14 -0500450#if defined(CONFIG_CMD_CDP)
wdenka3d991b2004-04-15 21:48:45 +0000451 case CDP:
452 CDPStart();
453 break;
454#endif
wdenk68ceb292004-08-02 21:11:11 +0000455#ifdef CONFIG_NETCONSOLE
456 case NETCONS:
457 NcStart();
458 break;
459#endif
Jon Loeliger643d1ab2007-07-09 17:45:14 -0500460#if defined(CONFIG_CMD_SNTP)
wdenkea287de2005-04-01 00:25:43 +0000461 case SNTP:
462 SntpStart();
463 break;
464#endif
Robin Getz1a32bf42009-07-20 14:53:54 -0400465#if defined(CONFIG_CMD_DNS)
466 case DNS:
467 DnsStart();
468 break;
469#endif
wdenk2d966952002-10-31 22:12:35 +0000470 default:
471 break;
472 }
473
wdenk2d966952002-10-31 22:12:35 +0000474 break;
475 }
476
Jon Loeliger643d1ab2007-07-09 17:45:14 -0500477#if defined(CONFIG_MII) || defined(CONFIG_CMD_MII)
Luca Ceresoli3e38e422011-05-11 03:59:54 +0000478#if defined(CONFIG_SYS_FAULT_ECHO_LINK_DOWN) && \
479 defined(CONFIG_STATUS_LED) && \
480 defined(STATUS_LED_RED)
wdenkfc3e2162003-10-08 22:33:00 +0000481 /*
wdenk42d1f032003-10-15 23:53:47 +0000482 * Echo the inverted link state to the fault LED.
wdenkfc3e2162003-10-08 22:33:00 +0000483 */
Luca Ceresolid3c65b02011-05-04 02:40:43 +0000484 if (miiphy_link(eth_get_dev()->name, CONFIG_SYS_FAULT_MII_ADDR))
Luca Ceresoli4f63acd2011-05-11 03:59:56 +0000485 status_led_set(STATUS_LED_RED, STATUS_LED_OFF);
Luca Ceresolid3c65b02011-05-04 02:40:43 +0000486 else
Luca Ceresoli4f63acd2011-05-11 03:59:56 +0000487 status_led_set(STATUS_LED_RED, STATUS_LED_ON);
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200488#endif /* CONFIG_SYS_FAULT_ECHO_LINK_DOWN, ... */
wdenkfc3e2162003-10-08 22:33:00 +0000489#endif /* CONFIG_MII, ... */
wdenk2d966952002-10-31 22:12:35 +0000490
491 /*
492 * Main packet reception loop. Loop receiving packets until
wdenk59acc292005-04-03 14:18:51 +0000493 * someone sets `NetState' to a state that terminates.
wdenk2d966952002-10-31 22:12:35 +0000494 */
495 for (;;) {
496 WATCHDOG_RESET();
497#ifdef CONFIG_SHOW_ACTIVITY
498 {
499 extern void show_activity(int arg);
500 show_activity(1);
501 }
502#endif
503 /*
504 * Check the ethernet for a new packet. The ethernet
505 * receive routine will process it.
506 */
Guennadi Liakhovetski40cb90e2008-04-03 17:04:19 +0200507 eth_rx();
wdenk2d966952002-10-31 22:12:35 +0000508
509 /*
510 * Abort if ctrl-c was pressed.
511 */
512 if (ctrlc()) {
wdenk8bde7f72003-06-27 21:31:46 +0000513 eth_halt();
Luca Ceresoli4f63acd2011-05-11 03:59:56 +0000514 puts("\nAbort\n");
Simon Glass4793ee62011-10-24 18:00:01 +0000515 goto done;
wdenk2d966952002-10-31 22:12:35 +0000516 }
517
wdenk73a8b272003-06-05 19:27:42 +0000518 ArpTimeoutCheck();
wdenk2d966952002-10-31 22:12:35 +0000519
520 /*
521 * Check for a timeout, and run the timeout handler
522 * if we have one.
523 */
wdenke0ac62d2003-08-17 18:55:18 +0000524 if (timeHandler && ((get_timer(0) - timeStart) > timeDelta)) {
wdenk2d966952002-10-31 22:12:35 +0000525 thand_f *x;
526
Jon Loeliger643d1ab2007-07-09 17:45:14 -0500527#if defined(CONFIG_MII) || defined(CONFIG_CMD_MII)
Luca Ceresoli4f63acd2011-05-11 03:59:56 +0000528#if defined(CONFIG_SYS_FAULT_ECHO_LINK_DOWN) && \
529 defined(CONFIG_STATUS_LED) && \
530 defined(STATUS_LED_RED)
wdenkfc3e2162003-10-08 22:33:00 +0000531 /*
wdenk42d1f032003-10-15 23:53:47 +0000532 * Echo the inverted link state to the fault LED.
wdenkfc3e2162003-10-08 22:33:00 +0000533 */
Luca Ceresoli4f63acd2011-05-11 03:59:56 +0000534 if (miiphy_link(eth_get_dev()->name,
Luca Ceresoli3e38e422011-05-11 03:59:54 +0000535 CONFIG_SYS_FAULT_MII_ADDR)) {
Luca Ceresoli4f63acd2011-05-11 03:59:56 +0000536 status_led_set(STATUS_LED_RED, STATUS_LED_OFF);
wdenkfc3e2162003-10-08 22:33:00 +0000537 } else {
Luca Ceresoli4f63acd2011-05-11 03:59:56 +0000538 status_led_set(STATUS_LED_RED, STATUS_LED_ON);
wdenkfc3e2162003-10-08 22:33:00 +0000539 }
Luca Ceresoli4f63acd2011-05-11 03:59:56 +0000540#endif /* CONFIG_SYS_FAULT_ECHO_LINK_DOWN, ... */
wdenkfc3e2162003-10-08 22:33:00 +0000541#endif /* CONFIG_MII, ... */
wdenk2d966952002-10-31 22:12:35 +0000542 x = timeHandler;
543 timeHandler = (thand_f *)0;
544 (*x)();
545 }
546
547
548 switch (NetState) {
549
550 case NETLOOP_RESTART:
wdenk2d966952002-10-31 22:12:35 +0000551 NetRestarted = 1;
wdenk2d966952002-10-31 22:12:35 +0000552 goto restart;
553
554 case NETLOOP_SUCCESS:
555 if (NetBootFileXferSize > 0) {
Wolfgang Denkf34024d2007-09-12 00:48:57 +0200556 char buf[20];
wdenk2d966952002-10-31 22:12:35 +0000557 printf("Bytes transferred = %ld (%lx hex)\n",
558 NetBootFileXferSize,
559 NetBootFileXferSize);
Wolfgang Denkf34024d2007-09-12 00:48:57 +0200560 sprintf(buf, "%lX", NetBootFileXferSize);
wdenk2d966952002-10-31 22:12:35 +0000561 setenv("filesize", buf);
wdenka3d991b2004-04-15 21:48:45 +0000562
563 sprintf(buf, "%lX", (unsigned long)load_addr);
564 setenv("fileaddr", buf);
wdenk2d966952002-10-31 22:12:35 +0000565 }
566 eth_halt();
Simon Glass4793ee62011-10-24 18:00:01 +0000567 ret = NetBootFileXferSize;
568 goto done;
wdenk2d966952002-10-31 22:12:35 +0000569
570 case NETLOOP_FAIL:
Simon Glass4793ee62011-10-24 18:00:01 +0000571 goto done;
wdenk2d966952002-10-31 22:12:35 +0000572 }
573 }
Simon Glass4793ee62011-10-24 18:00:01 +0000574
575done:
576 /* Clear out the handlers */
577 NetSetHandler(NULL);
578 net_set_icmp_handler(NULL);
579 return ret;
wdenk2d966952002-10-31 22:12:35 +0000580}
581
582/**********************************************************************/
583
584static void
585startAgainTimeout(void)
586{
587 NetState = NETLOOP_RESTART;
588}
589
590static void
Luca Ceresoli03eb1292011-04-18 06:19:50 +0000591startAgainHandler(uchar *pkt, unsigned dest, IPaddr_t sip,
592 unsigned src, unsigned len)
wdenk2d966952002-10-31 22:12:35 +0000593{
594 /* Totally ignore the packet */
595}
596
Luca Ceresoli4f63acd2011-05-11 03:59:56 +0000597void NetStartAgain(void)
wdenk2d966952002-10-31 22:12:35 +0000598{
wdenk6e592382004-04-18 17:39:38 +0000599 char *nretry;
Remy Bohmer67b96e82009-10-28 22:13:39 +0100600 int retry_forever = 0;
601 unsigned long retrycnt = 0;
wdenka3d991b2004-04-15 21:48:45 +0000602
Remy Bohmer67b96e82009-10-28 22:13:39 +0100603 nretry = getenv("netretry");
604 if (nretry) {
605 if (!strcmp(nretry, "yes"))
606 retry_forever = 1;
607 else if (!strcmp(nretry, "no"))
608 retrycnt = 0;
609 else if (!strcmp(nretry, "once"))
610 retrycnt = 1;
611 else
612 retrycnt = simple_strtoul(nretry, NULL, 0);
613 } else
614 retry_forever = 1;
615
616 if ((!retry_forever) && (NetTryCount >= retrycnt)) {
617 eth_halt();
wdenka3d991b2004-04-15 21:48:45 +0000618 NetState = NETLOOP_FAIL;
619 return;
620 }
Remy Bohmer67b96e82009-10-28 22:13:39 +0100621
622 NetTryCount++;
623
Luca Ceresoli4f63acd2011-05-11 03:59:56 +0000624 eth_halt();
Matthias Fuchs8b0c5c12007-12-27 16:58:41 +0100625#if !defined(CONFIG_NET_DO_NOT_TRY_ANOTHER)
Luca Ceresoli4f63acd2011-05-11 03:59:56 +0000626 eth_try_another(!NetRestarted);
Matthias Fuchs8b0c5c12007-12-27 16:58:41 +0100627#endif
Luca Ceresoli4f63acd2011-05-11 03:59:56 +0000628 eth_init(gd->bd);
wdenk6e592382004-04-18 17:39:38 +0000629 if (NetRestartWrap) {
wdenk2d966952002-10-31 22:12:35 +0000630 NetRestartWrap = 0;
Remy Bohmer67b96e82009-10-28 22:13:39 +0100631 if (NetDevExists) {
Luca Ceresoli4f63acd2011-05-11 03:59:56 +0000632 NetSetTimeout(10000UL, startAgainTimeout);
633 NetSetHandler(startAgainHandler);
wdenk6e592382004-04-18 17:39:38 +0000634 } else {
wdenk2d966952002-10-31 22:12:35 +0000635 NetState = NETLOOP_FAIL;
636 }
wdenk6e592382004-04-18 17:39:38 +0000637 } else {
wdenk2d966952002-10-31 22:12:35 +0000638 NetState = NETLOOP_RESTART;
639 }
wdenk2d966952002-10-31 22:12:35 +0000640}
641
642/**********************************************************************/
643/*
644 * Miscelaneous bits.
645 */
646
647void
Luca Ceresoli6b147d12011-05-04 02:40:44 +0000648NetSetHandler(rxhand_f *f)
wdenk2d966952002-10-31 22:12:35 +0000649{
650 packetHandler = f;
651}
652
Simon Glass4793ee62011-10-24 18:00:01 +0000653void net_set_icmp_handler(rxhand_icmp_f *f)
654{
655 packet_icmp_handler = f;
656}
wdenk2d966952002-10-31 22:12:35 +0000657
658void
Luca Ceresoli6b147d12011-05-04 02:40:44 +0000659NetSetTimeout(ulong iv, thand_f *f)
wdenk2d966952002-10-31 22:12:35 +0000660{
661 if (iv == 0) {
662 timeHandler = (thand_f *)0;
663 } else {
664 timeHandler = f;
wdenke0ac62d2003-08-17 18:55:18 +0000665 timeStart = get_timer(0);
666 timeDelta = iv;
wdenk2d966952002-10-31 22:12:35 +0000667 }
668}
669
670
671void
Luca Ceresoli6b147d12011-05-04 02:40:44 +0000672NetSendPacket(volatile uchar *pkt, int len)
wdenk2d966952002-10-31 22:12:35 +0000673{
674 (void) eth_send(pkt, len);
675}
676
wdenk73a8b272003-06-05 19:27:42 +0000677int
678NetSendUDPPacket(uchar *ether, IPaddr_t dest, int dport, int sport, int len)
679{
wdenka3d991b2004-04-15 21:48:45 +0000680 uchar *pkt;
681
wdenk73a8b272003-06-05 19:27:42 +0000682 /* convert to new style broadcast */
683 if (dest == 0)
684 dest = 0xFFFFFFFF;
wdenk2d966952002-10-31 22:12:35 +0000685
wdenk73a8b272003-06-05 19:27:42 +0000686 /* if broadcast, make the ether address a broadcast and don't do ARP */
687 if (dest == 0xFFFFFFFF)
688 ether = NetBcastAddr;
689
Luca Ceresoli3e38e422011-05-11 03:59:54 +0000690 /*
691 * if MAC address was not discovered yet, save the packet and do
692 * an ARP request
693 */
wdenk73a8b272003-06-05 19:27:42 +0000694 if (memcmp(ether, NetEtherNullAddr, 6) == 0) {
695
Robin Getz0ebf04c2009-07-23 03:01:03 -0400696 debug("sending ARP for %08lx\n", dest);
697
wdenk73a8b272003-06-05 19:27:42 +0000698 NetArpWaitPacketIP = dest;
699 NetArpWaitPacketMAC = ether;
wdenka3d991b2004-04-15 21:48:45 +0000700
701 pkt = NetArpWaitTxPacket;
Luca Ceresoli4f63acd2011-05-11 03:59:56 +0000702 pkt += NetSetEther(pkt, NetArpWaitPacketMAC, PROT_IP);
wdenka3d991b2004-04-15 21:48:45 +0000703
Luca Ceresoli4f63acd2011-05-11 03:59:56 +0000704 NetSetIP(pkt, dest, dport, sport, len);
Luca Ceresoli3e38e422011-05-11 03:59:54 +0000705 memcpy(pkt + IP_HDR_SIZE, (uchar *)NetTxPacket +
706 (pkt - (uchar *)NetArpWaitTxPacket) + IP_HDR_SIZE, len);
wdenk73a8b272003-06-05 19:27:42 +0000707
708 /* size of the waiting packet */
Luca Ceresoli3e38e422011-05-11 03:59:54 +0000709 NetArpWaitTxPacketSize = (pkt - NetArpWaitTxPacket) +
710 IP_HDR_SIZE + len;
wdenk73a8b272003-06-05 19:27:42 +0000711
712 /* and do the ARP request */
713 NetArpWaitTry = 1;
714 NetArpWaitTimerStart = get_timer(0);
715 ArpRequest();
716 return 1; /* waiting */
717 }
718
Robin Getz0ebf04c2009-07-23 03:01:03 -0400719 debug("sending UDP to %08lx/%pM\n", dest, ether);
wdenk73a8b272003-06-05 19:27:42 +0000720
wdenka3d991b2004-04-15 21:48:45 +0000721 pkt = (uchar *)NetTxPacket;
Luca Ceresoli4f63acd2011-05-11 03:59:56 +0000722 pkt += NetSetEther(pkt, ether, PROT_IP);
723 NetSetIP(pkt, dest, dport, sport, len);
wdenka3d991b2004-04-15 21:48:45 +0000724 (void) eth_send(NetTxPacket, (pkt - NetTxPacket) + IP_HDR_SIZE + len);
wdenk73a8b272003-06-05 19:27:42 +0000725
wdenk6e592382004-04-18 17:39:38 +0000726 return 0; /* transmitted */
wdenk73a8b272003-06-05 19:27:42 +0000727}
728
Jon Loeliger643d1ab2007-07-09 17:45:14 -0500729#if defined(CONFIG_CMD_PING)
wdenk73a8b272003-06-05 19:27:42 +0000730static ushort PingSeqNo;
731
732int PingSend(void)
733{
734 static uchar mac[6];
735 volatile IP_t *ip;
736 volatile ushort *s;
wdenka3d991b2004-04-15 21:48:45 +0000737 uchar *pkt;
wdenk73a8b272003-06-05 19:27:42 +0000738
739 /* XXX always send arp request */
740
741 memcpy(mac, NetEtherNullAddr, 6);
742
Robin Getz0ebf04c2009-07-23 03:01:03 -0400743 debug("sending ARP for %08lx\n", NetPingIP);
wdenk73a8b272003-06-05 19:27:42 +0000744
745 NetArpWaitPacketIP = NetPingIP;
746 NetArpWaitPacketMAC = mac;
747
wdenka3d991b2004-04-15 21:48:45 +0000748 pkt = NetArpWaitTxPacket;
749 pkt += NetSetEther(pkt, mac, PROT_IP);
wdenk73a8b272003-06-05 19:27:42 +0000750
wdenka3d991b2004-04-15 21:48:45 +0000751 ip = (volatile IP_t *)pkt;
wdenk73a8b272003-06-05 19:27:42 +0000752
753 /*
Luca Ceresoli3e38e422011-05-11 03:59:54 +0000754 * Construct an IP and ICMP header.
755 * (need to set no fragment bit - XXX)
wdenk73a8b272003-06-05 19:27:42 +0000756 */
Luca Ceresoli3e38e422011-05-11 03:59:54 +0000757 /* IP_HDR_SIZE / 4 (not including UDP) */
758 ip->ip_hl_v = 0x45;
wdenk73a8b272003-06-05 19:27:42 +0000759 ip->ip_tos = 0;
760 ip->ip_len = htons(IP_HDR_SIZE_NO_UDP + 8);
761 ip->ip_id = htons(NetIPID++);
Peter Tysere0c07b82008-12-01 16:26:20 -0600762 ip->ip_off = htons(IP_FLAGS_DFRAG); /* Don't fragment */
wdenk73a8b272003-06-05 19:27:42 +0000763 ip->ip_ttl = 255;
764 ip->ip_p = 0x01; /* ICMP */
765 ip->ip_sum = 0;
Luca Ceresoli3e38e422011-05-11 03:59:54 +0000766 /* already in network byte order */
Luca Ceresoli6b147d12011-05-04 02:40:44 +0000767 NetCopyIP((void *)&ip->ip_src, &NetOurIP);
Luca Ceresoli3e38e422011-05-11 03:59:54 +0000768 /* - "" - */
Luca Ceresoli6b147d12011-05-04 02:40:44 +0000769 NetCopyIP((void *)&ip->ip_dst, &NetPingIP);
wdenk73a8b272003-06-05 19:27:42 +0000770 ip->ip_sum = ~NetCksum((uchar *)ip, IP_HDR_SIZE_NO_UDP / 2);
771
772 s = &ip->udp_src; /* XXX ICMP starts here */
773 s[0] = htons(0x0800); /* echo-request, code */
774 s[1] = 0; /* checksum */
Wolfgang Denk53677ef2008-05-20 16:00:29 +0200775 s[2] = 0; /* identifier */
wdenk73a8b272003-06-05 19:27:42 +0000776 s[3] = htons(PingSeqNo++); /* sequence number */
777 s[1] = ~NetCksum((uchar *)s, 8/2);
778
779 /* size of the waiting packet */
Luca Ceresoli3e38e422011-05-11 03:59:54 +0000780 NetArpWaitTxPacketSize =
781 (pkt - NetArpWaitTxPacket) + IP_HDR_SIZE_NO_UDP + 8;
wdenk73a8b272003-06-05 19:27:42 +0000782
783 /* and do the ARP request */
784 NetArpWaitTry = 1;
785 NetArpWaitTimerStart = get_timer(0);
786 ArpRequest();
787 return 1; /* waiting */
788}
789
790static void
Luca Ceresoli4f63acd2011-05-11 03:59:56 +0000791PingTimeout(void)
wdenk73a8b272003-06-05 19:27:42 +0000792{
793 eth_halt();
794 NetState = NETLOOP_FAIL; /* we did not get the reply */
795}
796
797static void
Luca Ceresoli03eb1292011-04-18 06:19:50 +0000798PingHandler(uchar *pkt, unsigned dest, IPaddr_t sip, unsigned src,
799 unsigned len)
wdenk73a8b272003-06-05 19:27:42 +0000800{
Luca Ceresoli03eb1292011-04-18 06:19:50 +0000801 if (sip != NetPingIP)
wdenk73a8b272003-06-05 19:27:42 +0000802 return;
803
804 NetState = NETLOOP_SUCCESS;
805}
806
807static void PingStart(void)
808{
Luca Ceresoli4f63acd2011-05-11 03:59:56 +0000809 printf("Using %s device\n", eth_get_name());
Luca Ceresoli4f63acd2011-05-11 03:59:56 +0000810 NetSetTimeout(10000UL, PingTimeout);
811 NetSetHandler(PingHandler);
wdenk73a8b272003-06-05 19:27:42 +0000812
813 PingSend();
814}
Jon Loeliger610f2e92007-07-10 11:05:02 -0500815#endif
wdenk2d966952002-10-31 22:12:35 +0000816
Jon Loeliger643d1ab2007-07-09 17:45:14 -0500817#if defined(CONFIG_CMD_CDP)
wdenka3d991b2004-04-15 21:48:45 +0000818
819#define CDP_DEVICE_ID_TLV 0x0001
820#define CDP_ADDRESS_TLV 0x0002
821#define CDP_PORT_ID_TLV 0x0003
822#define CDP_CAPABILITIES_TLV 0x0004
823#define CDP_VERSION_TLV 0x0005
824#define CDP_PLATFORM_TLV 0x0006
825#define CDP_NATIVE_VLAN_TLV 0x000a
826#define CDP_APPLIANCE_VLAN_TLV 0x000e
827#define CDP_TRIGGER_TLV 0x000f
828#define CDP_POWER_CONSUMPTION_TLV 0x0010
829#define CDP_SYSNAME_TLV 0x0014
830#define CDP_SYSOBJECT_TLV 0x0015
831#define CDP_MANAGEMENT_ADDRESS_TLV 0x0016
832
Bartlomiej Sieka49f3bdb2008-10-01 15:26:28 +0200833#define CDP_TIMEOUT 250UL /* one packet every 250ms */
wdenka3d991b2004-04-15 21:48:45 +0000834
835static int CDPSeq;
836static int CDPOK;
837
838ushort CDPNativeVLAN;
839ushort CDPApplianceVLAN;
840
Luca Ceresoli3e38e422011-05-11 03:59:54 +0000841static const uchar CDP_SNAP_hdr[8] = { 0xAA, 0xAA, 0x03, 0x00, 0x00, 0x0C, 0x20,
842 0x00 };
wdenka3d991b2004-04-15 21:48:45 +0000843
844static ushort CDP_compute_csum(const uchar *buff, ushort len)
845{
846 ushort csum;
847 int odd;
848 ulong result = 0;
849 ushort leftover;
Wolfgang Denk77ddac92005-10-13 16:45:02 +0200850 ushort *p;
wdenka3d991b2004-04-15 21:48:45 +0000851
852 if (len > 0) {
853 odd = 1 & (ulong)buff;
854 if (odd) {
855 result = *buff << 8;
856 len--;
857 buff++;
858 }
859 while (len > 1) {
Wolfgang Denk77ddac92005-10-13 16:45:02 +0200860 p = (ushort *)buff;
861 result += *p++;
862 buff = (uchar *)p;
wdenka3d991b2004-04-15 21:48:45 +0000863 if (result & 0x80000000)
864 result = (result & 0xFFFF) + (result >> 16);
865 len -= 2;
866 }
867 if (len) {
868 leftover = (signed short)(*(const signed char *)buff);
Wolfgang Denk3ada8342005-11-10 20:59:46 +0100869 /* CISCO SUCKS big time! (and blows too):
870 * CDP uses the IP checksum algorithm with a twist;
871 * for the last byte it *sign* extends and sums.
872 */
Luca Ceresoli3e38e422011-05-11 03:59:54 +0000873 result = (result & 0xffff0000) |
874 ((result + leftover) & 0x0000ffff);
wdenka3d991b2004-04-15 21:48:45 +0000875 }
876 while (result >> 16)
877 result = (result & 0xFFFF) + (result >> 16);
878
879 if (odd)
Luca Ceresoli3e38e422011-05-11 03:59:54 +0000880 result = ((result >> 8) & 0xff) |
881 ((result & 0xff) << 8);
wdenka3d991b2004-04-15 21:48:45 +0000882 }
883
884 /* add up 16-bit and 17-bit words for 17+c bits */
885 result = (result & 0xffff) + (result >> 16);
886 /* add up 16-bit and 2-bit for 16+c bit */
887 result = (result & 0xffff) + (result >> 16);
888 /* add up carry.. */
889 result = (result & 0xffff) + (result >> 16);
890
891 /* negate */
892 csum = ~(ushort)result;
893
894 /* run time endian detection */
895 if (csum != htons(csum)) /* little endian */
896 csum = htons(csum);
897
898 return csum;
899}
900
901int CDPSendTrigger(void)
902{
903 volatile uchar *pkt;
904 volatile ushort *s;
905 volatile ushort *cp;
906 Ethernet_t *et;
wdenka3d991b2004-04-15 21:48:45 +0000907 int len;
908 ushort chksum;
Luca Ceresoli4f63acd2011-05-11 03:59:56 +0000909#if defined(CONFIG_CDP_DEVICE_ID) || defined(CONFIG_CDP_PORT_ID) || \
910 defined(CONFIG_CDP_VERSION) || defined(CONFIG_CDP_PLATFORM)
wdenk6e592382004-04-18 17:39:38 +0000911 char buf[32];
912#endif
wdenka3d991b2004-04-15 21:48:45 +0000913
914 pkt = NetTxPacket;
915 et = (Ethernet_t *)pkt;
916
917 /* NOTE: trigger sent not on any VLAN */
918
919 /* form ethernet header */
920 memcpy(et->et_dest, NetCDPAddr, 6);
921 memcpy(et->et_src, NetOurEther, 6);
922
923 pkt += ETHER_HDR_SIZE;
924
925 /* SNAP header */
926 memcpy((uchar *)pkt, CDP_SNAP_hdr, sizeof(CDP_SNAP_hdr));
927 pkt += sizeof(CDP_SNAP_hdr);
928
929 /* CDP header */
930 *pkt++ = 0x02; /* CDP version 2 */
931 *pkt++ = 180; /* TTL */
932 s = (volatile ushort *)pkt;
933 cp = s;
Luca Ceresoli3e38e422011-05-11 03:59:54 +0000934 /* checksum (0 for later calculation) */
935 *s++ = htons(0);
wdenka3d991b2004-04-15 21:48:45 +0000936
937 /* CDP fields */
938#ifdef CONFIG_CDP_DEVICE_ID
939 *s++ = htons(CDP_DEVICE_ID_TLV);
940 *s++ = htons(CONFIG_CDP_DEVICE_ID);
Mike Frysinger95823ca2009-02-11 18:23:48 -0500941 sprintf(buf, CONFIG_CDP_DEVICE_ID_PREFIX "%pm", NetOurEther);
wdenka3d991b2004-04-15 21:48:45 +0000942 memcpy((uchar *)s, buf, 16);
943 s += 16 / 2;
944#endif
945
946#ifdef CONFIG_CDP_PORT_ID
947 *s++ = htons(CDP_PORT_ID_TLV);
948 memset(buf, 0, sizeof(buf));
949 sprintf(buf, CONFIG_CDP_PORT_ID, eth_get_dev_index());
950 len = strlen(buf);
951 if (len & 1) /* make it even */
952 len++;
953 *s++ = htons(len + 4);
954 memcpy((uchar *)s, buf, len);
955 s += len / 2;
956#endif
957
958#ifdef CONFIG_CDP_CAPABILITIES
959 *s++ = htons(CDP_CAPABILITIES_TLV);
960 *s++ = htons(8);
961 *(ulong *)s = htonl(CONFIG_CDP_CAPABILITIES);
962 s += 2;
963#endif
964
965#ifdef CONFIG_CDP_VERSION
966 *s++ = htons(CDP_VERSION_TLV);
967 memset(buf, 0, sizeof(buf));
968 strcpy(buf, CONFIG_CDP_VERSION);
969 len = strlen(buf);
970 if (len & 1) /* make it even */
971 len++;
972 *s++ = htons(len + 4);
973 memcpy((uchar *)s, buf, len);
974 s += len / 2;
975#endif
976
977#ifdef CONFIG_CDP_PLATFORM
978 *s++ = htons(CDP_PLATFORM_TLV);
979 memset(buf, 0, sizeof(buf));
980 strcpy(buf, CONFIG_CDP_PLATFORM);
981 len = strlen(buf);
982 if (len & 1) /* make it even */
983 len++;
984 *s++ = htons(len + 4);
985 memcpy((uchar *)s, buf, len);
986 s += len / 2;
987#endif
988
989#ifdef CONFIG_CDP_TRIGGER
990 *s++ = htons(CDP_TRIGGER_TLV);
991 *s++ = htons(8);
992 *(ulong *)s = htonl(CONFIG_CDP_TRIGGER);
993 s += 2;
994#endif
995
996#ifdef CONFIG_CDP_POWER_CONSUMPTION
997 *s++ = htons(CDP_POWER_CONSUMPTION_TLV);
998 *s++ = htons(6);
999 *s++ = htons(CONFIG_CDP_POWER_CONSUMPTION);
1000#endif
1001
1002 /* length of ethernet packet */
1003 len = (uchar *)s - ((uchar *)NetTxPacket + ETHER_HDR_SIZE);
1004 et->et_protlen = htons(len);
1005
1006 len = ETHER_HDR_SIZE + sizeof(CDP_SNAP_hdr);
Luca Ceresoli3e38e422011-05-11 03:59:54 +00001007 chksum = CDP_compute_csum((uchar *)NetTxPacket + len,
1008 (uchar *)s - (NetTxPacket + len));
wdenka3d991b2004-04-15 21:48:45 +00001009 if (chksum == 0)
1010 chksum = 0xFFFF;
1011 *cp = htons(chksum);
1012
1013 (void) eth_send(NetTxPacket, (uchar *)s - NetTxPacket);
1014 return 0;
1015}
1016
1017static void
Luca Ceresoli4f63acd2011-05-11 03:59:56 +00001018CDPTimeout(void)
wdenka3d991b2004-04-15 21:48:45 +00001019{
1020 CDPSeq++;
1021
1022 if (CDPSeq < 3) {
Luca Ceresoli4f63acd2011-05-11 03:59:56 +00001023 NetSetTimeout(CDP_TIMEOUT, CDPTimeout);
wdenka3d991b2004-04-15 21:48:45 +00001024 CDPSendTrigger();
1025 return;
1026 }
1027
1028 /* if not OK try again */
1029 if (!CDPOK)
1030 NetStartAgain();
1031 else
1032 NetState = NETLOOP_SUCCESS;
1033}
1034
1035static void
Luca Ceresoli03eb1292011-04-18 06:19:50 +00001036CDPDummyHandler(uchar *pkt, unsigned dest, IPaddr_t sip, unsigned src,
1037 unsigned len)
wdenka3d991b2004-04-15 21:48:45 +00001038{
1039 /* nothing */
1040}
1041
1042static void
Luca Ceresoli6b147d12011-05-04 02:40:44 +00001043CDPHandler(const uchar *pkt, unsigned len)
wdenka3d991b2004-04-15 21:48:45 +00001044{
1045 const uchar *t;
1046 const ushort *ss;
1047 ushort type, tlen;
1048 uchar applid;
1049 ushort vlan, nvlan;
1050
1051 /* minimum size? */
1052 if (len < sizeof(CDP_SNAP_hdr) + 4)
1053 goto pkt_short;
1054
1055 /* check for valid CDP SNAP header */
1056 if (memcmp(pkt, CDP_SNAP_hdr, sizeof(CDP_SNAP_hdr)) != 0)
1057 return;
1058
1059 pkt += sizeof(CDP_SNAP_hdr);
1060 len -= sizeof(CDP_SNAP_hdr);
1061
1062 /* Version of CDP protocol must be >= 2 and TTL != 0 */
1063 if (pkt[0] < 0x02 || pkt[1] == 0)
1064 return;
1065
Luca Ceresoli3e38e422011-05-11 03:59:54 +00001066 /*
1067 * if version is greater than 0x02 maybe we'll have a problem;
1068 * output a warning
1069 */
wdenka3d991b2004-04-15 21:48:45 +00001070 if (pkt[0] != 0x02)
1071 printf("** WARNING: CDP packet received with a protocol version %d > 2\n",
1072 pkt[0] & 0xff);
1073
1074 if (CDP_compute_csum(pkt, len) != 0)
1075 return;
1076
1077 pkt += 4;
1078 len -= 4;
1079
1080 vlan = htons(-1);
1081 nvlan = htons(-1);
1082 while (len > 0) {
1083 if (len < 4)
1084 goto pkt_short;
1085
1086 ss = (const ushort *)pkt;
1087 type = ntohs(ss[0]);
1088 tlen = ntohs(ss[1]);
Luca Ceresolid3c65b02011-05-04 02:40:43 +00001089 if (tlen > len)
wdenka3d991b2004-04-15 21:48:45 +00001090 goto pkt_short;
wdenka3d991b2004-04-15 21:48:45 +00001091
1092 pkt += tlen;
1093 len -= tlen;
1094
1095 ss += 2; /* point ss to the data of the TLV */
1096 tlen -= 4;
1097
1098 switch (type) {
Luca Ceresolic819abe2011-05-04 02:40:46 +00001099 case CDP_DEVICE_ID_TLV:
1100 break;
1101 case CDP_ADDRESS_TLV:
1102 break;
1103 case CDP_PORT_ID_TLV:
1104 break;
1105 case CDP_CAPABILITIES_TLV:
1106 break;
1107 case CDP_VERSION_TLV:
1108 break;
1109 case CDP_PLATFORM_TLV:
1110 break;
1111 case CDP_NATIVE_VLAN_TLV:
1112 nvlan = *ss;
1113 break;
1114 case CDP_APPLIANCE_VLAN_TLV:
1115 t = (const uchar *)ss;
1116 while (tlen > 0) {
1117 if (tlen < 3)
1118 goto pkt_short;
wdenka3d991b2004-04-15 21:48:45 +00001119
Luca Ceresolic819abe2011-05-04 02:40:46 +00001120 applid = t[0];
1121 ss = (const ushort *)(t + 1);
wdenka3d991b2004-04-15 21:48:45 +00001122
1123#ifdef CONFIG_CDP_APPLIANCE_VLAN_TYPE
Luca Ceresolic819abe2011-05-04 02:40:46 +00001124 if (applid == CONFIG_CDP_APPLIANCE_VLAN_TYPE)
1125 vlan = *ss;
wdenka3d991b2004-04-15 21:48:45 +00001126#else
Luca Ceresolic819abe2011-05-04 02:40:46 +00001127 /* XXX will this work; dunno */
1128 vlan = ntohs(*ss);
wdenka3d991b2004-04-15 21:48:45 +00001129#endif
Luca Ceresolic819abe2011-05-04 02:40:46 +00001130 t += 3; tlen -= 3;
1131 }
1132 break;
1133 case CDP_TRIGGER_TLV:
1134 break;
1135 case CDP_POWER_CONSUMPTION_TLV:
1136 break;
1137 case CDP_SYSNAME_TLV:
1138 break;
1139 case CDP_SYSOBJECT_TLV:
1140 break;
1141 case CDP_MANAGEMENT_ADDRESS_TLV:
1142 break;
wdenka3d991b2004-04-15 21:48:45 +00001143 }
1144 }
1145
1146 CDPApplianceVLAN = vlan;
1147 CDPNativeVLAN = nvlan;
1148
1149 CDPOK = 1;
1150 return;
1151
1152 pkt_short:
1153 printf("** CDP packet is too short\n");
1154 return;
1155}
1156
1157static void CDPStart(void)
1158{
Luca Ceresoli4f63acd2011-05-11 03:59:56 +00001159 printf("Using %s device\n", eth_get_name());
wdenka3d991b2004-04-15 21:48:45 +00001160 CDPSeq = 0;
1161 CDPOK = 0;
1162
1163 CDPNativeVLAN = htons(-1);
1164 CDPApplianceVLAN = htons(-1);
1165
Luca Ceresoli4f63acd2011-05-11 03:59:56 +00001166 NetSetTimeout(CDP_TIMEOUT, CDPTimeout);
1167 NetSetHandler(CDPDummyHandler);
wdenka3d991b2004-04-15 21:48:45 +00001168
1169 CDPSendTrigger();
1170}
Jon Loeliger610f2e92007-07-10 11:05:02 -05001171#endif
wdenka3d991b2004-04-15 21:48:45 +00001172
Alessandro Rubini5cfaa4e2009-08-07 13:58:56 +02001173#ifdef CONFIG_IP_DEFRAG
1174/*
1175 * This function collects fragments in a single packet, according
1176 * to the algorithm in RFC815. It returns NULL or the pointer to
1177 * a complete packet, in static storage
1178 */
1179#ifndef CONFIG_NET_MAXDEFRAG
1180#define CONFIG_NET_MAXDEFRAG 16384
1181#endif
1182/*
1183 * MAXDEFRAG, above, is chosen in the config file and is real data
1184 * so we need to add the NFS overhead, which is more than TFTP.
1185 * To use sizeof in the internal unnamed structures, we need a real
1186 * instance (can't do "sizeof(struct rpc_t.u.reply))", unfortunately).
1187 * The compiler doesn't complain nor allocates the actual structure
1188 */
1189static struct rpc_t rpc_specimen;
1190#define IP_PKTSIZE (CONFIG_NET_MAXDEFRAG + sizeof(rpc_specimen.u.reply))
1191
1192#define IP_MAXUDP (IP_PKTSIZE - IP_HDR_SIZE_NO_UDP)
1193
1194/*
1195 * this is the packet being assembled, either data or frag control.
1196 * Fragments go by 8 bytes, so this union must be 8 bytes long
1197 */
1198struct hole {
1199 /* first_byte is address of this structure */
1200 u16 last_byte; /* last byte in this hole + 1 (begin of next hole) */
1201 u16 next_hole; /* index of next (in 8-b blocks), 0 == none */
1202 u16 prev_hole; /* index of prev, 0 == none */
1203 u16 unused;
1204};
1205
1206static IP_t *__NetDefragment(IP_t *ip, int *lenp)
1207{
1208 static uchar pkt_buff[IP_PKTSIZE] __attribute__((aligned(PKTALIGN)));
1209 static u16 first_hole, total_len;
1210 struct hole *payload, *thisfrag, *h, *newh;
1211 IP_t *localip = (IP_t *)pkt_buff;
1212 uchar *indata = (uchar *)ip;
1213 int offset8, start, len, done = 0;
1214 u16 ip_off = ntohs(ip->ip_off);
1215
1216 /* payload starts after IP header, this fragment is in there */
1217 payload = (struct hole *)(pkt_buff + IP_HDR_SIZE_NO_UDP);
1218 offset8 = (ip_off & IP_OFFS);
1219 thisfrag = payload + offset8;
1220 start = offset8 * 8;
1221 len = ntohs(ip->ip_len) - IP_HDR_SIZE_NO_UDP;
1222
1223 if (start + len > IP_MAXUDP) /* fragment extends too far */
1224 return NULL;
1225
1226 if (!total_len || localip->ip_id != ip->ip_id) {
1227 /* new (or different) packet, reset structs */
1228 total_len = 0xffff;
1229 payload[0].last_byte = ~0;
1230 payload[0].next_hole = 0;
1231 payload[0].prev_hole = 0;
1232 first_hole = 0;
1233 /* any IP header will work, copy the first we received */
1234 memcpy(localip, ip, IP_HDR_SIZE_NO_UDP);
1235 }
1236
1237 /*
1238 * What follows is the reassembly algorithm. We use the payload
1239 * array as a linked list of hole descriptors, as each hole starts
1240 * at a multiple of 8 bytes. However, last byte can be whatever value,
1241 * so it is represented as byte count, not as 8-byte blocks.
1242 */
1243
1244 h = payload + first_hole;
1245 while (h->last_byte < start) {
1246 if (!h->next_hole) {
1247 /* no hole that far away */
1248 return NULL;
1249 }
1250 h = payload + h->next_hole;
1251 }
1252
Fillod Stephanee397e592010-06-11 19:26:43 +02001253 /* last fragment may be 1..7 bytes, the "+7" forces acceptance */
1254 if (offset8 + ((len + 7) / 8) <= h - payload) {
Alessandro Rubini5cfaa4e2009-08-07 13:58:56 +02001255 /* no overlap with holes (dup fragment?) */
1256 return NULL;
1257 }
1258
1259 if (!(ip_off & IP_FLAGS_MFRAG)) {
1260 /* no more fragmentss: truncate this (last) hole */
1261 total_len = start + len;
1262 h->last_byte = start + len;
1263 }
1264
1265 /*
1266 * There is some overlap: fix the hole list. This code doesn't
1267 * deal with a fragment that overlaps with two different holes
1268 * (thus being a superset of a previously-received fragment).
1269 */
1270
Luca Ceresoli4f63acd2011-05-11 03:59:56 +00001271 if ((h >= thisfrag) && (h->last_byte <= start + len)) {
Alessandro Rubini5cfaa4e2009-08-07 13:58:56 +02001272 /* complete overlap with hole: remove hole */
1273 if (!h->prev_hole && !h->next_hole) {
1274 /* last remaining hole */
1275 done = 1;
1276 } else if (!h->prev_hole) {
1277 /* first hole */
1278 first_hole = h->next_hole;
1279 payload[h->next_hole].prev_hole = 0;
1280 } else if (!h->next_hole) {
1281 /* last hole */
1282 payload[h->prev_hole].next_hole = 0;
1283 } else {
1284 /* in the middle of the list */
1285 payload[h->next_hole].prev_hole = h->prev_hole;
1286 payload[h->prev_hole].next_hole = h->next_hole;
1287 }
1288
1289 } else if (h->last_byte <= start + len) {
1290 /* overlaps with final part of the hole: shorten this hole */
1291 h->last_byte = start;
1292
1293 } else if (h >= thisfrag) {
1294 /* overlaps with initial part of the hole: move this hole */
1295 newh = thisfrag + (len / 8);
1296 *newh = *h;
1297 h = newh;
1298 if (h->next_hole)
1299 payload[h->next_hole].prev_hole = (h - payload);
1300 if (h->prev_hole)
1301 payload[h->prev_hole].next_hole = (h - payload);
1302 else
1303 first_hole = (h - payload);
1304
1305 } else {
1306 /* fragment sits in the middle: split the hole */
1307 newh = thisfrag + (len / 8);
1308 *newh = *h;
1309 h->last_byte = start;
1310 h->next_hole = (newh - payload);
1311 newh->prev_hole = (h - payload);
1312 if (newh->next_hole)
1313 payload[newh->next_hole].prev_hole = (newh - payload);
1314 }
1315
1316 /* finally copy this fragment and possibly return whole packet */
1317 memcpy((uchar *)thisfrag, indata + IP_HDR_SIZE_NO_UDP, len);
1318 if (!done)
1319 return NULL;
1320
1321 localip->ip_len = htons(total_len);
1322 *lenp = total_len + IP_HDR_SIZE_NO_UDP;
1323 return localip;
1324}
1325
1326static inline IP_t *NetDefragment(IP_t *ip, int *lenp)
1327{
1328 u16 ip_off = ntohs(ip->ip_off);
1329 if (!(ip_off & (IP_OFFS | IP_FLAGS_MFRAG)))
1330 return ip; /* not a fragment */
1331 return __NetDefragment(ip, lenp);
1332}
1333
1334#else /* !CONFIG_IP_DEFRAG */
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 NULL;
1342}
1343#endif
wdenka3d991b2004-04-15 21:48:45 +00001344
Simon Glass8f79bb12011-10-24 18:00:00 +00001345/**
1346 * Receive an ICMP packet. We deal with REDIRECT and PING here, and silently
1347 * drop others.
1348 *
1349 * @parma ip IP packet containing the ICMP
1350 */
1351static void receive_icmp(IP_t *ip, int len, IPaddr_t src_ip, Ethernet_t *et)
1352{
1353 ICMP_t *icmph = (ICMP_t *)&ip->udp_src;
1354
1355 switch (icmph->type) {
1356 case ICMP_REDIRECT:
1357 if (icmph->code != ICMP_REDIR_HOST)
1358 return;
1359 printf(" ICMP Host Redirect to %pI4 ",
1360 &icmph->un.gateway);
1361 break;
1362#if defined(CONFIG_CMD_PING)
1363 case ICMP_ECHO_REPLY:
1364 /*
1365 * IP header OK. Pass the packet to the
1366 * current handler.
1367 */
1368 /*
1369 * XXX point to ip packet - should this use
1370 * packet_icmp_handler?
1371 */
1372 (*packetHandler)((uchar *)ip, 0, src_ip, 0, 0);
1373 break;
1374 case ICMP_ECHO_REQUEST:
1375 debug("Got ICMP ECHO REQUEST, return %d bytes\n",
1376 ETHER_HDR_SIZE + len);
1377
1378 memcpy(&et->et_dest[0], &et->et_src[0], 6);
1379 memcpy(&et->et_src[0], NetOurEther, 6);
1380
1381 ip->ip_sum = 0;
1382 ip->ip_off = 0;
1383 NetCopyIP((void *)&ip->ip_dst, &ip->ip_src);
1384 NetCopyIP((void *)&ip->ip_src, &NetOurIP);
1385 ip->ip_sum = ~NetCksum((uchar *)ip,
1386 IP_HDR_SIZE_NO_UDP >> 1);
1387
1388 icmph->type = ICMP_ECHO_REPLY;
1389 icmph->checksum = 0;
1390 icmph->checksum = ~NetCksum((uchar *)icmph,
1391 (len - IP_HDR_SIZE_NO_UDP) >> 1);
1392 (void) eth_send((uchar *)et,
1393 ETHER_HDR_SIZE + len);
1394 break;
1395#endif
1396 default:
Simon Glass4793ee62011-10-24 18:00:01 +00001397 if (packet_icmp_handler)
1398 packet_icmp_handler(icmph->type, icmph->code,
1399 ntohs(ip->udp_dst), src_ip, ntohs(ip->udp_src),
1400 icmph->un.data, ntohs(ip->udp_len));
Simon Glass8f79bb12011-10-24 18:00:00 +00001401 break;
1402 }
1403}
1404
wdenk2d966952002-10-31 22:12:35 +00001405void
Luca Ceresoli6b147d12011-05-04 02:40:44 +00001406NetReceive(volatile uchar *inpkt, int len)
wdenk2d966952002-10-31 22:12:35 +00001407{
1408 Ethernet_t *et;
1409 IP_t *ip;
1410 ARP_t *arp;
1411 IPaddr_t tmp;
Luca Ceresoli03eb1292011-04-18 06:19:50 +00001412 IPaddr_t src_ip;
wdenk2d966952002-10-31 22:12:35 +00001413 int x;
wdenka3d991b2004-04-15 21:48:45 +00001414 uchar *pkt;
Jon Loeliger643d1ab2007-07-09 17:45:14 -05001415#if defined(CONFIG_CMD_CDP)
wdenka3d991b2004-04-15 21:48:45 +00001416 int iscdp;
1417#endif
1418 ushort cti = 0, vlanid = VLAN_NONE, myvlanid, mynvlanid;
wdenk2d966952002-10-31 22:12:35 +00001419
Robin Getz0ebf04c2009-07-23 03:01:03 -04001420 debug("packet received\n");
wdenka3d991b2004-04-15 21:48:45 +00001421
Mike Frysingerd9bec9f2009-07-18 21:04:08 -04001422 NetRxPacket = inpkt;
1423 NetRxPacketLen = len;
wdenka3d991b2004-04-15 21:48:45 +00001424 et = (Ethernet_t *)inpkt;
1425
1426 /* too small packet? */
1427 if (len < ETHER_HDR_SIZE)
1428 return;
1429
Rafal Jaworowskif85b6072007-12-27 18:19:02 +01001430#ifdef CONFIG_API
1431 if (push_packet) {
1432 (*push_packet)(inpkt, len);
1433 return;
1434 }
1435#endif
1436
Jon Loeliger643d1ab2007-07-09 17:45:14 -05001437#if defined(CONFIG_CMD_CDP)
wdenka3d991b2004-04-15 21:48:45 +00001438 /* keep track if packet is CDP */
1439 iscdp = memcmp(et->et_dest, NetCDPAddr, 6) == 0;
1440#endif
1441
1442 myvlanid = ntohs(NetOurVLAN);
1443 if (myvlanid == (ushort)-1)
1444 myvlanid = VLAN_NONE;
1445 mynvlanid = ntohs(NetOurNativeVLAN);
1446 if (mynvlanid == (ushort)-1)
1447 mynvlanid = VLAN_NONE;
wdenk2d966952002-10-31 22:12:35 +00001448
1449 x = ntohs(et->et_protlen);
1450
Robin Getz0ebf04c2009-07-23 03:01:03 -04001451 debug("packet received\n");
wdenka3d991b2004-04-15 21:48:45 +00001452
wdenk2d966952002-10-31 22:12:35 +00001453 if (x < 1514) {
1454 /*
1455 * Got a 802 packet. Check the other protocol field.
1456 */
1457 x = ntohs(et->et_prot);
wdenka3d991b2004-04-15 21:48:45 +00001458
1459 ip = (IP_t *)(inpkt + E802_HDR_SIZE);
wdenk2d966952002-10-31 22:12:35 +00001460 len -= E802_HDR_SIZE;
wdenka3d991b2004-04-15 21:48:45 +00001461
1462 } else if (x != PROT_VLAN) { /* normal packet */
1463 ip = (IP_t *)(inpkt + ETHER_HDR_SIZE);
wdenk2d966952002-10-31 22:12:35 +00001464 len -= ETHER_HDR_SIZE;
wdenka3d991b2004-04-15 21:48:45 +00001465
1466 } else { /* VLAN packet */
1467 VLAN_Ethernet_t *vet = (VLAN_Ethernet_t *)et;
1468
Robin Getz0ebf04c2009-07-23 03:01:03 -04001469 debug("VLAN packet received\n");
1470
wdenka3d991b2004-04-15 21:48:45 +00001471 /* too small packet? */
1472 if (len < VLAN_ETHER_HDR_SIZE)
1473 return;
1474
1475 /* if no VLAN active */
1476 if ((ntohs(NetOurVLAN) & VLAN_IDMASK) == VLAN_NONE
Jon Loeliger643d1ab2007-07-09 17:45:14 -05001477#if defined(CONFIG_CMD_CDP)
wdenka3d991b2004-04-15 21:48:45 +00001478 && iscdp == 0
1479#endif
1480 )
1481 return;
1482
1483 cti = ntohs(vet->vet_tag);
1484 vlanid = cti & VLAN_IDMASK;
1485 x = ntohs(vet->vet_type);
1486
1487 ip = (IP_t *)(inpkt + VLAN_ETHER_HDR_SIZE);
1488 len -= VLAN_ETHER_HDR_SIZE;
wdenk2d966952002-10-31 22:12:35 +00001489 }
1490
Robin Getz0ebf04c2009-07-23 03:01:03 -04001491 debug("Receive from protocol 0x%x\n", x);
wdenk2d966952002-10-31 22:12:35 +00001492
Jon Loeliger643d1ab2007-07-09 17:45:14 -05001493#if defined(CONFIG_CMD_CDP)
wdenka3d991b2004-04-15 21:48:45 +00001494 if (iscdp) {
1495 CDPHandler((uchar *)ip, len);
1496 return;
1497 }
1498#endif
1499
1500 if ((myvlanid & VLAN_IDMASK) != VLAN_NONE) {
1501 if (vlanid == VLAN_NONE)
1502 vlanid = (mynvlanid & VLAN_IDMASK);
1503 /* not matched? */
1504 if (vlanid != (myvlanid & VLAN_IDMASK))
1505 return;
1506 }
1507
wdenk2d966952002-10-31 22:12:35 +00001508 switch (x) {
1509
1510 case PROT_ARP:
1511 /*
1512 * We have to deal with two types of ARP packets:
wdenk8bde7f72003-06-27 21:31:46 +00001513 * - REQUEST packets will be answered by sending our
1514 * IP address - if we know it.
1515 * - REPLY packates are expected only after we asked
1516 * for the TFTP server's or the gateway's ethernet
1517 * address; so if we receive such a packet, we set
1518 * the server ethernet address
wdenk2d966952002-10-31 22:12:35 +00001519 */
Robin Getz0ebf04c2009-07-23 03:01:03 -04001520 debug("Got ARP\n");
1521
wdenk2d966952002-10-31 22:12:35 +00001522 arp = (ARP_t *)ip;
1523 if (len < ARP_HDR_SIZE) {
1524 printf("bad length %d < %d\n", len, ARP_HDR_SIZE);
1525 return;
1526 }
Luca Ceresolid3c65b02011-05-04 02:40:43 +00001527 if (ntohs(arp->ar_hrd) != ARP_ETHER)
wdenk2d966952002-10-31 22:12:35 +00001528 return;
Luca Ceresolid3c65b02011-05-04 02:40:43 +00001529 if (ntohs(arp->ar_pro) != PROT_IP)
wdenk2d966952002-10-31 22:12:35 +00001530 return;
Luca Ceresolid3c65b02011-05-04 02:40:43 +00001531 if (arp->ar_hln != 6)
wdenk2d966952002-10-31 22:12:35 +00001532 return;
Luca Ceresolid3c65b02011-05-04 02:40:43 +00001533 if (arp->ar_pln != 4)
wdenk2d966952002-10-31 22:12:35 +00001534 return;
wdenk2d966952002-10-31 22:12:35 +00001535
Luca Ceresolid3c65b02011-05-04 02:40:43 +00001536 if (NetOurIP == 0)
wdenk2d966952002-10-31 22:12:35 +00001537 return;
wdenk2d966952002-10-31 22:12:35 +00001538
Luca Ceresolid3c65b02011-05-04 02:40:43 +00001539 if (NetReadIP(&arp->ar_data[16]) != NetOurIP)
wdenk2d966952002-10-31 22:12:35 +00001540 return;
wdenk2d966952002-10-31 22:12:35 +00001541
1542 switch (ntohs(arp->ar_op)) {
Luca Ceresoli3e38e422011-05-11 03:59:54 +00001543 case ARPOP_REQUEST:
1544 /* reply with our IP address */
Robin Getz0ebf04c2009-07-23 03:01:03 -04001545 debug("Got ARP REQUEST, return our IP\n");
wdenka3d991b2004-04-15 21:48:45 +00001546 pkt = (uchar *)et;
1547 pkt += NetSetEther(pkt, et->et_src, PROT_ARP);
wdenk2d966952002-10-31 22:12:35 +00001548 arp->ar_op = htons(ARPOP_REPLY);
Luca Ceresoli4f63acd2011-05-11 03:59:56 +00001549 memcpy(&arp->ar_data[10], &arp->ar_data[0], 6);
wdenk2d966952002-10-31 22:12:35 +00001550 NetCopyIP(&arp->ar_data[16], &arp->ar_data[6]);
Luca Ceresoli4f63acd2011-05-11 03:59:56 +00001551 memcpy(&arp->ar_data[0], NetOurEther, 6);
1552 NetCopyIP(&arp->ar_data[6], &NetOurIP);
Luca Ceresoli3e38e422011-05-11 03:59:54 +00001553 (void) eth_send((uchar *)et,
1554 (pkt - (uchar *)et) + ARP_HDR_SIZE);
wdenk2d966952002-10-31 22:12:35 +00001555 return;
wdenk73a8b272003-06-05 19:27:42 +00001556
1557 case ARPOP_REPLY: /* arp reply */
1558 /* are we waiting for a reply */
1559 if (!NetArpWaitPacketIP || !NetArpWaitPacketMAC)
1560 break;
Robin Getz97cfe862009-07-21 12:15:28 -04001561
1562#ifdef CONFIG_KEEP_SERVERADDR
1563 if (NetServerIP == NetArpWaitPacketIP) {
1564 char buf[20];
1565 sprintf(buf, "%pM", arp->ar_data);
1566 setenv("serveraddr", buf);
1567 }
1568#endif
1569
Robin Getz0ebf04c2009-07-23 03:01:03 -04001570 debug("Got ARP REPLY, set server/gtwy eth addr (%pM)\n",
Mike Frysinger95823ca2009-02-11 18:23:48 -05001571 arp->ar_data);
wdenk73a8b272003-06-05 19:27:42 +00001572
1573 tmp = NetReadIP(&arp->ar_data[6]);
1574
1575 /* matched waiting packet's address */
1576 if (tmp == NetArpWaitReplyIP) {
Robin Getz0ebf04c2009-07-23 03:01:03 -04001577 debug("Got it\n");
wdenk73a8b272003-06-05 19:27:42 +00001578 /* save address for later use */
Luca Ceresoli3e38e422011-05-11 03:59:54 +00001579 memcpy(NetArpWaitPacketMAC,
1580 &arp->ar_data[0], 6);
wdenk73a8b272003-06-05 19:27:42 +00001581
wdenk68ceb292004-08-02 21:11:11 +00001582#ifdef CONFIG_NETCONSOLE
Luca Ceresoli03eb1292011-04-18 06:19:50 +00001583 (*packetHandler)(0, 0, 0, 0, 0);
wdenk68ceb292004-08-02 21:11:11 +00001584#endif
wdenk73a8b272003-06-05 19:27:42 +00001585 /* modify header, and transmit it */
1586 memcpy(((Ethernet_t *)NetArpWaitTxPacket)->et_dest, NetArpWaitPacketMAC, 6);
Luca Ceresoli3e38e422011-05-11 03:59:54 +00001587 (void) eth_send(NetArpWaitTxPacket,
1588 NetArpWaitTxPacketSize);
wdenk73a8b272003-06-05 19:27:42 +00001589
1590 /* no arp request pending now */
1591 NetArpWaitPacketIP = 0;
1592 NetArpWaitTxPacketSize = 0;
1593 NetArpWaitPacketMAC = NULL;
1594
1595 }
wdenk2d966952002-10-31 22:12:35 +00001596 return;
1597 default:
Luca Ceresoli3e38e422011-05-11 03:59:54 +00001598 debug("Unexpected ARP opcode 0x%x\n",
1599 ntohs(arp->ar_op));
wdenk2d966952002-10-31 22:12:35 +00001600 return;
1601 }
wdenk289f9322005-01-12 00:15:14 +00001602 break;
wdenk2d966952002-10-31 22:12:35 +00001603
Peter Tyserbf6cb242010-09-30 11:25:48 -05001604#ifdef CONFIG_CMD_RARP
wdenk2d966952002-10-31 22:12:35 +00001605 case PROT_RARP:
Robin Getz0ebf04c2009-07-23 03:01:03 -04001606 debug("Got RARP\n");
wdenk2d966952002-10-31 22:12:35 +00001607 arp = (ARP_t *)ip;
1608 if (len < ARP_HDR_SIZE) {
1609 printf("bad length %d < %d\n", len, ARP_HDR_SIZE);
1610 return;
1611 }
1612
1613 if ((ntohs(arp->ar_op) != RARPOP_REPLY) ||
1614 (ntohs(arp->ar_hrd) != ARP_ETHER) ||
1615 (ntohs(arp->ar_pro) != PROT_IP) ||
1616 (arp->ar_hln != 6) || (arp->ar_pln != 4)) {
1617
Luca Ceresoli4f63acd2011-05-11 03:59:56 +00001618 puts("invalid RARP header\n");
wdenk2d966952002-10-31 22:12:35 +00001619 } else {
Luca Ceresoli4f63acd2011-05-11 03:59:56 +00001620 NetCopyIP(&NetOurIP, &arp->ar_data[16]);
wdenk3d3befa2004-03-14 15:06:13 +00001621 if (NetServerIP == 0)
Luca Ceresoli4f63acd2011-05-11 03:59:56 +00001622 NetCopyIP(&NetServerIP, &arp->ar_data[6]);
1623 memcpy(NetServerEther, &arp->ar_data[0], 6);
wdenk2d966952002-10-31 22:12:35 +00001624
Luca Ceresoli03eb1292011-04-18 06:19:50 +00001625 (*packetHandler)(0, 0, 0, 0, 0);
wdenk2d966952002-10-31 22:12:35 +00001626 }
1627 break;
Peter Tyserbf6cb242010-09-30 11:25:48 -05001628#endif
wdenk2d966952002-10-31 22:12:35 +00001629 case PROT_IP:
Robin Getz0ebf04c2009-07-23 03:01:03 -04001630 debug("Got IP\n");
Alessandro Rubini5cfaa4e2009-08-07 13:58:56 +02001631 /* Before we start poking the header, make sure it is there */
wdenk2d966952002-10-31 22:12:35 +00001632 if (len < IP_HDR_SIZE) {
Robin Getz0ebf04c2009-07-23 03:01:03 -04001633 debug("len bad %d < %lu\n", len, (ulong)IP_HDR_SIZE);
wdenk2d966952002-10-31 22:12:35 +00001634 return;
1635 }
Alessandro Rubini5cfaa4e2009-08-07 13:58:56 +02001636 /* Check the packet length */
wdenk2d966952002-10-31 22:12:35 +00001637 if (len < ntohs(ip->ip_len)) {
1638 printf("len bad %d < %d\n", len, ntohs(ip->ip_len));
1639 return;
1640 }
1641 len = ntohs(ip->ip_len);
Robin Getz0ebf04c2009-07-23 03:01:03 -04001642 debug("len=%d, v=%02x\n", len, ip->ip_hl_v & 0xff);
1643
Alessandro Rubini5cfaa4e2009-08-07 13:58:56 +02001644 /* Can't deal with anything except IPv4 */
Luca Ceresolid3c65b02011-05-04 02:40:43 +00001645 if ((ip->ip_hl_v & 0xf0) != 0x40)
wdenk2d966952002-10-31 22:12:35 +00001646 return;
Alessandro Rubini5cfaa4e2009-08-07 13:58:56 +02001647 /* Can't deal with IP options (headers != 20 bytes) */
Luca Ceresolid3c65b02011-05-04 02:40:43 +00001648 if ((ip->ip_hl_v & 0x0f) > 0x05)
Remy Bohmer6b52cfe2008-06-03 15:48:17 +02001649 return;
Alessandro Rubini5cfaa4e2009-08-07 13:58:56 +02001650 /* Check the Checksum of the header */
wdenk2d966952002-10-31 22:12:35 +00001651 if (!NetCksumOk((uchar *)ip, IP_HDR_SIZE_NO_UDP / 2)) {
Luca Ceresoli4f63acd2011-05-11 03:59:56 +00001652 puts("checksum bad\n");
wdenk2d966952002-10-31 22:12:35 +00001653 return;
1654 }
Alessandro Rubini5cfaa4e2009-08-07 13:58:56 +02001655 /* If it is not for us, ignore it */
wdenk2d966952002-10-31 22:12:35 +00001656 tmp = NetReadIP(&ip->ip_dst);
1657 if (NetOurIP && tmp != NetOurIP && tmp != 0xFFFFFFFF) {
David Updegraff53a5c422007-06-11 10:41:07 -05001658#ifdef CONFIG_MCAST_TFTP
Wolfgang Denk85eb5ca2007-08-14 09:47:27 +02001659 if (Mcast_addr != tmp)
David Updegraff53a5c422007-06-11 10:41:07 -05001660#endif
Luca Ceresolic819abe2011-05-04 02:40:46 +00001661 return;
wdenk2d966952002-10-31 22:12:35 +00001662 }
Luca Ceresoli03eb1292011-04-18 06:19:50 +00001663 /* Read source IP address for later use */
1664 src_ip = NetReadIP(&ip->ip_src);
wdenk2d966952002-10-31 22:12:35 +00001665 /*
Alessandro Rubini5cfaa4e2009-08-07 13:58:56 +02001666 * The function returns the unchanged packet if it's not
1667 * a fragment, and either the complete packet or NULL if
1668 * it is a fragment (if !CONFIG_IP_DEFRAG, it returns NULL)
1669 */
Luca Ceresoliccb9ebe2011-05-04 02:40:47 +00001670 ip = NetDefragment(ip, &len);
1671 if (!ip)
Alessandro Rubini5cfaa4e2009-08-07 13:58:56 +02001672 return;
1673 /*
wdenk2d966952002-10-31 22:12:35 +00001674 * watch for ICMP host redirects
1675 *
wdenk8bde7f72003-06-27 21:31:46 +00001676 * There is no real handler code (yet). We just watch
1677 * for ICMP host redirect messages. In case anybody
1678 * sees these messages: please contact me
1679 * (wd@denx.de), or - even better - send me the
1680 * necessary fixes :-)
wdenk2d966952002-10-31 22:12:35 +00001681 *
wdenk8bde7f72003-06-27 21:31:46 +00001682 * Note: in all cases where I have seen this so far
1683 * it was a problem with the router configuration,
1684 * for instance when a router was configured in the
1685 * BOOTP reply, but the TFTP server was on the same
1686 * subnet. So this is probably a warning that your
1687 * configuration might be wrong. But I'm not really
1688 * sure if there aren't any other situations.
Simon Glass4793ee62011-10-24 18:00:01 +00001689 *
1690 * Simon Glass <sjg@chromium.org>: We get an ICMP when
1691 * we send a tftp packet to a dead connection, or when
1692 * there is no server at the other end.
wdenk2d966952002-10-31 22:12:35 +00001693 */
1694 if (ip->ip_p == IPPROTO_ICMP) {
Simon Glass8f79bb12011-10-24 18:00:00 +00001695 receive_icmp(ip, len, src_ip, et);
1696 return;
wdenk2d966952002-10-31 22:12:35 +00001697 } else if (ip->ip_p != IPPROTO_UDP) { /* Only UDP packets */
1698 return;
1699 }
1700
Stefan Roese8534bf92005-08-12 20:06:52 +02001701#ifdef CONFIG_UDP_CHECKSUM
1702 if (ip->udp_xsum != 0) {
Wolfgang Denkb2f50802005-08-12 23:43:12 +02001703 ulong xsum;
Stefan Roese8534bf92005-08-12 20:06:52 +02001704 ushort *sumptr;
1705 ushort sumlen;
1706
1707 xsum = ip->ip_p;
1708 xsum += (ntohs(ip->udp_len));
1709 xsum += (ntohl(ip->ip_src) >> 16) & 0x0000ffff;
1710 xsum += (ntohl(ip->ip_src) >> 0) & 0x0000ffff;
1711 xsum += (ntohl(ip->ip_dst) >> 16) & 0x0000ffff;
1712 xsum += (ntohl(ip->ip_dst) >> 0) & 0x0000ffff;
1713
1714 sumlen = ntohs(ip->udp_len);
1715 sumptr = (ushort *) &(ip->udp_src);
1716
1717 while (sumlen > 1) {
Wolfgang Denkb2f50802005-08-12 23:43:12 +02001718 ushort sumdata;
Stefan Roese8534bf92005-08-12 20:06:52 +02001719
1720 sumdata = *sumptr++;
1721 xsum += ntohs(sumdata);
1722 sumlen -= 2;
1723 }
1724 if (sumlen > 0) {
Wolfgang Denkb2f50802005-08-12 23:43:12 +02001725 ushort sumdata;
Stefan Roese8534bf92005-08-12 20:06:52 +02001726
1727 sumdata = *(unsigned char *) sumptr;
Wolfgang Denkb2f50802005-08-12 23:43:12 +02001728 sumdata = (sumdata << 8) & 0xff00;
Stefan Roese8534bf92005-08-12 20:06:52 +02001729 xsum += sumdata;
1730 }
1731 while ((xsum >> 16) != 0) {
Luca Ceresoli3e38e422011-05-11 03:59:54 +00001732 xsum = (xsum & 0x0000ffff) +
1733 ((xsum >> 16) & 0x0000ffff);
Stefan Roese8534bf92005-08-12 20:06:52 +02001734 }
1735 if ((xsum != 0x00000000) && (xsum != 0x0000ffff)) {
Wolfgang Denk9b55a252008-07-11 01:16:00 +02001736 printf(" UDP wrong checksum %08lx %08x\n",
1737 xsum, ntohs(ip->udp_xsum));
Stefan Roese8534bf92005-08-12 20:06:52 +02001738 return;
1739 }
1740 }
1741#endif
1742
David Updegraff53a5c422007-06-11 10:41:07 -05001743
wdenk68ceb292004-08-02 21:11:11 +00001744#ifdef CONFIG_NETCONSOLE
Luca Ceresoli4f63acd2011-05-11 03:59:56 +00001745 nc_input_packet((uchar *)ip + IP_HDR_SIZE,
wdenk68ceb292004-08-02 21:11:11 +00001746 ntohs(ip->udp_dst),
1747 ntohs(ip->udp_src),
1748 ntohs(ip->udp_len) - 8);
1749#endif
wdenk2d966952002-10-31 22:12:35 +00001750 /*
1751 * IP header OK. Pass the packet to the current handler.
1752 */
Luca Ceresoli4f63acd2011-05-11 03:59:56 +00001753 (*packetHandler)((uchar *)ip + IP_HDR_SIZE,
wdenk2d966952002-10-31 22:12:35 +00001754 ntohs(ip->udp_dst),
Luca Ceresoli03eb1292011-04-18 06:19:50 +00001755 src_ip,
wdenk2d966952002-10-31 22:12:35 +00001756 ntohs(ip->udp_src),
1757 ntohs(ip->udp_len) - 8);
wdenk2d966952002-10-31 22:12:35 +00001758 break;
1759 }
1760}
1761
1762
1763/**********************************************************************/
1764
Simon Glasse4bf0c52011-10-24 18:00:02 +00001765static int net_check_prereq(enum proto_t protocol)
wdenk2d966952002-10-31 22:12:35 +00001766{
1767 switch (protocol) {
wdenk6e592382004-04-18 17:39:38 +00001768 /* Fall through */
Jon Loeliger643d1ab2007-07-09 17:45:14 -05001769#if defined(CONFIG_CMD_PING)
wdenk73a8b272003-06-05 19:27:42 +00001770 case PING:
wdenk6e592382004-04-18 17:39:38 +00001771 if (NetPingIP == 0) {
Luca Ceresoli4f63acd2011-05-11 03:59:56 +00001772 puts("*** ERROR: ping address not given\n");
Luca Ceresoli92895de2011-05-04 02:40:45 +00001773 return 1;
wdenk6e592382004-04-18 17:39:38 +00001774 }
1775 goto common;
wdenk73a8b272003-06-05 19:27:42 +00001776#endif
Jon Loeliger643d1ab2007-07-09 17:45:14 -05001777#if defined(CONFIG_CMD_SNTP)
wdenkea287de2005-04-01 00:25:43 +00001778 case SNTP:
1779 if (NetNtpServerIP == 0) {
Luca Ceresoli4f63acd2011-05-11 03:59:56 +00001780 puts("*** ERROR: NTP server address not given\n");
Luca Ceresoli92895de2011-05-04 02:40:45 +00001781 return 1;
wdenkea287de2005-04-01 00:25:43 +00001782 }
1783 goto common;
1784#endif
Robin Getz1a32bf42009-07-20 14:53:54 -04001785#if defined(CONFIG_CMD_DNS)
1786 case DNS:
1787 if (NetOurDNSIP == 0) {
1788 puts("*** ERROR: DNS server address not given\n");
1789 return 1;
1790 }
1791 goto common;
1792#endif
Jon Loeliger643d1ab2007-07-09 17:45:14 -05001793#if defined(CONFIG_CMD_NFS)
wdenkcbd8a352004-02-24 02:00:03 +00001794 case NFS:
1795#endif
Simon Glasse4bf0c52011-10-24 18:00:02 +00001796 case TFTPGET:
wdenk6e592382004-04-18 17:39:38 +00001797 if (NetServerIP == 0) {
Luca Ceresoli4f63acd2011-05-11 03:59:56 +00001798 puts("*** ERROR: `serverip' not set\n");
Luca Ceresoli92895de2011-05-04 02:40:45 +00001799 return 1;
wdenk6e592382004-04-18 17:39:38 +00001800 }
Luca Ceresoli4f63acd2011-05-11 03:59:56 +00001801#if defined(CONFIG_CMD_PING) || defined(CONFIG_CMD_SNTP) || \
1802 defined(CONFIG_CMD_DNS)
1803common:
wdenk73a8b272003-06-05 19:27:42 +00001804#endif
Simon Guinot8b6bbe12011-05-01 23:38:40 +00001805 /* Fall through */
wdenk73a8b272003-06-05 19:27:42 +00001806
Simon Guinot8b6bbe12011-05-01 23:38:40 +00001807 case NETCONS:
Luca Ceresoli7a83af02011-05-17 00:03:40 +00001808 case TFTPSRV:
wdenk6e592382004-04-18 17:39:38 +00001809 if (NetOurIP == 0) {
Luca Ceresoli4f63acd2011-05-11 03:59:56 +00001810 puts("*** ERROR: `ipaddr' not set\n");
Luca Ceresoli92895de2011-05-04 02:40:45 +00001811 return 1;
wdenk6e592382004-04-18 17:39:38 +00001812 }
1813 /* Fall through */
wdenk2d966952002-10-31 22:12:35 +00001814
Peter Tyserbf6cb242010-09-30 11:25:48 -05001815#ifdef CONFIG_CMD_RARP
wdenk2d966952002-10-31 22:12:35 +00001816 case RARP:
Peter Tyserbf6cb242010-09-30 11:25:48 -05001817#endif
wdenk2d966952002-10-31 22:12:35 +00001818 case BOOTP:
wdenka3d991b2004-04-15 21:48:45 +00001819 case CDP:
Peter Tyserbf6cb242010-09-30 11:25:48 -05001820 case DHCP:
Luca Ceresoli4f63acd2011-05-11 03:59:56 +00001821 if (memcmp(NetOurEther, "\0\0\0\0\0\0", 6) == 0) {
Luca Ceresoli4f63acd2011-05-11 03:59:56 +00001822 extern int eth_get_dev_index(void);
1823 int num = eth_get_dev_index();
wdenk2d966952002-10-31 22:12:35 +00001824
wdenk6e592382004-04-18 17:39:38 +00001825 switch (num) {
1826 case -1:
Luca Ceresoli4f63acd2011-05-11 03:59:56 +00001827 puts("*** ERROR: No ethernet found.\n");
Luca Ceresoli92895de2011-05-04 02:40:45 +00001828 return 1;
wdenk6e592382004-04-18 17:39:38 +00001829 case 0:
Luca Ceresoli4f63acd2011-05-11 03:59:56 +00001830 puts("*** ERROR: `ethaddr' not set\n");
wdenk2d966952002-10-31 22:12:35 +00001831 break;
wdenk6e592382004-04-18 17:39:38 +00001832 default:
Luca Ceresoli4f63acd2011-05-11 03:59:56 +00001833 printf("*** ERROR: `eth%daddr' not set\n",
wdenk2d966952002-10-31 22:12:35 +00001834 num);
1835 break;
wdenk2d966952002-10-31 22:12:35 +00001836 }
wdenk6e592382004-04-18 17:39:38 +00001837
Luca Ceresoli4f63acd2011-05-11 03:59:56 +00001838 NetStartAgain();
Luca Ceresoli92895de2011-05-04 02:40:45 +00001839 return 2;
wdenk6e592382004-04-18 17:39:38 +00001840 }
1841 /* Fall through */
1842 default:
Luca Ceresoli92895de2011-05-04 02:40:45 +00001843 return 0;
wdenk2d966952002-10-31 22:12:35 +00001844 }
Luca Ceresoli92895de2011-05-04 02:40:45 +00001845 return 0; /* OK */
wdenk2d966952002-10-31 22:12:35 +00001846}
1847/**********************************************************************/
1848
1849int
Luca Ceresoli6b147d12011-05-04 02:40:44 +00001850NetCksumOk(uchar *ptr, int len)
wdenk2d966952002-10-31 22:12:35 +00001851{
1852 return !((NetCksum(ptr, len) + 1) & 0xfffe);
1853}
1854
1855
1856unsigned
Luca Ceresoli6b147d12011-05-04 02:40:44 +00001857NetCksum(uchar *ptr, int len)
wdenk2d966952002-10-31 22:12:35 +00001858{
1859 ulong xsum;
Stefan Roese9d2a8732005-08-31 12:55:50 +02001860 ushort *p = (ushort *)ptr;
wdenk2d966952002-10-31 22:12:35 +00001861
1862 xsum = 0;
1863 while (len-- > 0)
Wolfgang Denk7bc5ee02005-08-26 01:36:03 +02001864 xsum += *p++;
wdenk2d966952002-10-31 22:12:35 +00001865 xsum = (xsum & 0xffff) + (xsum >> 16);
1866 xsum = (xsum & 0xffff) + (xsum >> 16);
Luca Ceresoli92895de2011-05-04 02:40:45 +00001867 return xsum & 0xffff;
wdenk2d966952002-10-31 22:12:35 +00001868}
1869
wdenka3d991b2004-04-15 21:48:45 +00001870int
1871NetEthHdrSize(void)
1872{
1873 ushort myvlanid;
wdenk2d966952002-10-31 22:12:35 +00001874
wdenka3d991b2004-04-15 21:48:45 +00001875 myvlanid = ntohs(NetOurVLAN);
1876 if (myvlanid == (ushort)-1)
1877 myvlanid = VLAN_NONE;
1878
Luca Ceresoli3e38e422011-05-11 03:59:54 +00001879 return ((myvlanid & VLAN_IDMASK) == VLAN_NONE) ? ETHER_HDR_SIZE :
1880 VLAN_ETHER_HDR_SIZE;
wdenka3d991b2004-04-15 21:48:45 +00001881}
1882
1883int
Luca Ceresoli6b147d12011-05-04 02:40:44 +00001884NetSetEther(volatile uchar *xet, uchar * addr, uint prot)
wdenk2d966952002-10-31 22:12:35 +00001885{
1886 Ethernet_t *et = (Ethernet_t *)xet;
wdenka3d991b2004-04-15 21:48:45 +00001887 ushort myvlanid;
1888
1889 myvlanid = ntohs(NetOurVLAN);
1890 if (myvlanid == (ushort)-1)
1891 myvlanid = VLAN_NONE;
wdenk2d966952002-10-31 22:12:35 +00001892
Luca Ceresoli4f63acd2011-05-11 03:59:56 +00001893 memcpy(et->et_dest, addr, 6);
1894 memcpy(et->et_src, NetOurEther, 6);
wdenka3d991b2004-04-15 21:48:45 +00001895 if ((myvlanid & VLAN_IDMASK) == VLAN_NONE) {
Luca Ceresolic819abe2011-05-04 02:40:46 +00001896 et->et_protlen = htons(prot);
wdenka3d991b2004-04-15 21:48:45 +00001897 return ETHER_HDR_SIZE;
1898 } else {
1899 VLAN_Ethernet_t *vet = (VLAN_Ethernet_t *)xet;
wdenk2d966952002-10-31 22:12:35 +00001900
wdenka3d991b2004-04-15 21:48:45 +00001901 vet->vet_vlan_type = htons(PROT_VLAN);
1902 vet->vet_tag = htons((0 << 5) | (myvlanid & VLAN_IDMASK));
1903 vet->vet_type = htons(prot);
1904 return VLAN_ETHER_HDR_SIZE;
1905 }
1906}
wdenk2d966952002-10-31 22:12:35 +00001907
1908void
Luca Ceresoli6b147d12011-05-04 02:40:44 +00001909NetSetIP(volatile uchar *xip, IPaddr_t dest, int dport, int sport, int len)
wdenk2d966952002-10-31 22:12:35 +00001910{
Olav Morkenaf8626e2009-01-23 12:56:26 +01001911 IP_t *ip = (IP_t *)xip;
wdenk2d966952002-10-31 22:12:35 +00001912
1913 /*
1914 * If the data is an odd number of bytes, zero the
1915 * byte after the last byte so that the checksum
1916 * will work.
1917 */
1918 if (len & 1)
1919 xip[IP_HDR_SIZE + len] = 0;
1920
1921 /*
1922 * Construct an IP and UDP header.
wdenk6e592382004-04-18 17:39:38 +00001923 * (need to set no fragment bit - XXX)
wdenk2d966952002-10-31 22:12:35 +00001924 */
Luca Ceresoli3e38e422011-05-11 03:59:54 +00001925 /* IP_HDR_SIZE / 4 (not including UDP) */
1926 ip->ip_hl_v = 0x45;
wdenk2d966952002-10-31 22:12:35 +00001927 ip->ip_tos = 0;
1928 ip->ip_len = htons(IP_HDR_SIZE + len);
1929 ip->ip_id = htons(NetIPID++);
Peter Tysere0c07b82008-12-01 16:26:20 -06001930 ip->ip_off = htons(IP_FLAGS_DFRAG); /* Don't fragment */
wdenk2d966952002-10-31 22:12:35 +00001931 ip->ip_ttl = 255;
1932 ip->ip_p = 17; /* UDP */
1933 ip->ip_sum = 0;
Luca Ceresoli3e38e422011-05-11 03:59:54 +00001934 /* already in network byte order */
Luca Ceresoli6b147d12011-05-04 02:40:44 +00001935 NetCopyIP((void *)&ip->ip_src, &NetOurIP);
Luca Ceresoli3e38e422011-05-11 03:59:54 +00001936 /* - "" - */
Luca Ceresoli6b147d12011-05-04 02:40:44 +00001937 NetCopyIP((void *)&ip->ip_dst, &dest);
wdenk2d966952002-10-31 22:12:35 +00001938 ip->udp_src = htons(sport);
1939 ip->udp_dst = htons(dport);
1940 ip->udp_len = htons(8 + len);
1941 ip->udp_xsum = 0;
1942 ip->ip_sum = ~NetCksum((uchar *)ip, IP_HDR_SIZE_NO_UDP / 2);
1943}
1944
Luca Ceresoli4f63acd2011-05-11 03:59:56 +00001945void copy_filename(char *dst, const char *src, int size)
wdenk2d966952002-10-31 22:12:35 +00001946{
1947 if (*src && (*src == '"')) {
1948 ++src;
1949 --size;
1950 }
1951
Luca Ceresolid3c65b02011-05-04 02:40:43 +00001952 while ((--size > 0) && *src && (*src != '"'))
wdenk2d966952002-10-31 22:12:35 +00001953 *dst++ = *src++;
wdenk2d966952002-10-31 22:12:35 +00001954 *dst = '\0';
1955}
1956
Luca Ceresoli3e38e422011-05-11 03:59:54 +00001957#if defined(CONFIG_CMD_NFS) || \
1958 defined(CONFIG_CMD_SNTP) || \
1959 defined(CONFIG_CMD_DNS)
Robin Getz1a32bf42009-07-20 14:53:54 -04001960/*
Robin Getz97399462010-03-08 14:07:00 -05001961 * make port a little random (1024-17407)
1962 * This keeps the math somewhat trivial to compute, and seems to work with
1963 * all supported protocols/clients/servers
Robin Getz1a32bf42009-07-20 14:53:54 -04001964 */
1965unsigned int random_port(void)
1966{
Robin Getz97399462010-03-08 14:07:00 -05001967 return 1024 + (get_timer(0) % 0x4000);
Robin Getz1a32bf42009-07-20 14:53:54 -04001968}
1969#endif
1970
Luca Ceresoli4f63acd2011-05-11 03:59:56 +00001971void ip_to_string(IPaddr_t x, char *s)
wdenk2d966952002-10-31 22:12:35 +00001972{
Luca Ceresoli4f63acd2011-05-11 03:59:56 +00001973 x = ntohl(x);
1974 sprintf(s, "%d.%d.%d.%d",
1975 (int) ((x >> 24) & 0xff),
1976 (int) ((x >> 16) & 0xff),
1977 (int) ((x >> 8) & 0xff), (int) ((x >> 0) & 0xff)
wdenk6e592382004-04-18 17:39:38 +00001978 );
wdenk2d966952002-10-31 22:12:35 +00001979}
1980
wdenka3d991b2004-04-15 21:48:45 +00001981void VLAN_to_string(ushort x, char *s)
1982{
1983 x = ntohs(x);
1984
1985 if (x == (ushort)-1)
1986 x = VLAN_NONE;
1987
1988 if (x == VLAN_NONE)
1989 strcpy(s, "none");
1990 else
1991 sprintf(s, "%d", x & VLAN_IDMASK);
1992}
1993
Mike Frysinger2e3ef6e2010-10-20 07:16:48 -04001994ushort string_to_VLAN(const char *s)
wdenka3d991b2004-04-15 21:48:45 +00001995{
1996 ushort id;
1997
1998 if (s == NULL)
wdenkb9711de2004-04-25 13:18:40 +00001999 return htons(VLAN_NONE);
wdenka3d991b2004-04-15 21:48:45 +00002000
2001 if (*s < '0' || *s > '9')
2002 id = VLAN_NONE;
2003 else
2004 id = (ushort)simple_strtoul(s, NULL, 10);
2005
wdenkb9711de2004-04-25 13:18:40 +00002006 return htons(id);
wdenka3d991b2004-04-15 21:48:45 +00002007}
2008
wdenka3d991b2004-04-15 21:48:45 +00002009ushort getenv_VLAN(char *var)
2010{
Luca Ceresoli92895de2011-05-04 02:40:45 +00002011 return string_to_VLAN(getenv(var));
wdenka3d991b2004-04-15 21:48:45 +00002012}