blob: 84f28bf32e11a1627e8708896346ba0a6fd800be [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;
Luca Ceresoli5c83a962011-05-11 03:59:54 +0000172/* Tried all network devices */
Luca Ceresolid01bc1c2011-05-11 03:59:55 +0000173int NetRestartWrap;
Luca Ceresoli5c83a962011-05-11 03:59:54 +0000174/* Network loop restarted */
Luca Ceresolid01bc1c2011-05-11 03:59:55 +0000175static int NetRestarted;
Luca Ceresoli5c83a962011-05-11 03:59:54 +0000176/* At least one device configured */
Luca Ceresolid01bc1c2011-05-11 03:59:55 +0000177static int NetDevExists;
wdenk2d966952002-10-31 22:12:35 +0000178
wdenk05939202004-04-18 17:39:38 +0000179/* XXX in both little & big endian machines 0xFFFF == ntohs(-1) */
Luca Ceresoli5c83a962011-05-11 03:59:54 +0000180/* default is without VLAN */
181ushort NetOurVLAN = 0xFFFF;
182/* ditto */
183ushort NetOurNativeVLAN = 0xFFFF;
wdenk145d2c12004-04-15 21:48:45 +0000184
Luca Ceresoli5c83a962011-05-11 03:59:54 +0000185/* Boot File name */
186char BootFile[128];
wdenk2d966952002-10-31 22:12:35 +0000187
Jon Loeliger54f35c22007-07-09 17:45:14 -0500188#if defined(CONFIG_CMD_PING)
Luca Ceresoli5c83a962011-05-11 03:59:54 +0000189/* the ip address to ping */
190IPaddr_t NetPingIP;
wdenke6466f62003-06-05 19:27:42 +0000191
192static void PingStart(void);
193#endif
194
Jon Loeliger54f35c22007-07-09 17:45:14 -0500195#if defined(CONFIG_CMD_CDP)
wdenk145d2c12004-04-15 21:48:45 +0000196static void CDPStart(void);
197#endif
198
Jon Loeliger54f35c22007-07-09 17:45:14 -0500199#if defined(CONFIG_CMD_SNTP)
Luca Ceresoli5c83a962011-05-11 03:59:54 +0000200/* NTP server IP address */
201IPaddr_t NetNtpServerIP;
202/* offset time from UTC */
Luca Ceresolid01bc1c2011-05-11 03:59:55 +0000203int NetTimeOffset;
wdenkb4ad9622005-04-01 00:25:43 +0000204#endif
205
wdenkb8fb6192004-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 Ceresoli5c83a962011-05-11 03:59:54 +0000213/* Receive packet */
214volatile uchar *NetRxPackets[PKTBUFSRX];
wdenk2d966952002-10-31 22:12:35 +0000215
Luca Ceresoli5c83a962011-05-11 03:59:54 +0000216/* Current RX packet handler */
217static rxhand_f *packetHandler;
Simon Glass230467c2011-10-24 18:00:01 +0000218static rxhand_icmp_f *packet_icmp_handler; /* Current ICMP rx handler */
Luca Ceresoli5c83a962011-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 Ceresolid01bc1c2011-05-11 03:59:55 +0000226volatile uchar *NetTxPacket;
wdenk2d966952002-10-31 22:12:35 +0000227
Simon Glassd6c5f552011-10-24 18:00:02 +0000228static int net_check_prereq(enum proto_t protocol);
wdenk2d966952002-10-31 22:12:35 +0000229
Remy Bohmer0ddb8e82009-10-28 22:13:39 +0100230static int NetTryCount;
231
wdenk2d966952002-10-31 22:12:35 +0000232/**********************************************************************/
wdenke6466f62003-06-05 19:27:42 +0000233
234IPaddr_t NetArpWaitPacketIP;
235IPaddr_t NetArpWaitReplyIP;
Luca Ceresoli5c83a962011-05-11 03:59:54 +0000236/* MAC address of waiting packet's destination */
237uchar *NetArpWaitPacketMAC;
238/* THE transmit packet */
239uchar *NetArpWaitTxPacket;
wdenke6466f62003-06-05 19:27:42 +0000240int NetArpWaitTxPacketSize;
Wolfgang Denka1be4762008-05-20 16:00:29 +0200241uchar NetArpWaitPacketBuf[PKTSIZE_ALIGN + PKTALIGN];
wdenke6466f62003-06-05 19:27:42 +0000242ulong NetArpWaitTimerStart;
243int NetArpWaitTry;
244
Luca Ceresoli7cbd7482011-05-11 03:59:56 +0000245void ArpRequest(void)
wdenke6466f62003-06-05 19:27:42 +0000246{
247 int i;
248 volatile uchar *pkt;
wdenk05939202004-04-18 17:39:38 +0000249 ARP_t *arp;
wdenke6466f62003-06-05 19:27:42 +0000250
Robin Getz9e0a4d62009-07-23 03:01:03 -0400251 debug("ARP broadcast %d\n", NetArpWaitTry);
252
wdenke6466f62003-06-05 19:27:42 +0000253 pkt = NetTxPacket;
254
Luca Ceresoli7cbd7482011-05-11 03:59:56 +0000255 pkt += NetSetEther(pkt, NetBcastAddr, PROT_ARP);
wdenke6466f62003-06-05 19:27:42 +0000256
wdenk05939202004-04-18 17:39:38 +0000257 arp = (ARP_t *) pkt;
wdenke6466f62003-06-05 19:27:42 +0000258
Luca Ceresoli7cbd7482011-05-11 03:59:56 +0000259 arp->ar_hrd = htons(ARP_ETHER);
260 arp->ar_pro = htons(PROT_IP);
wdenke6466f62003-06-05 19:27:42 +0000261 arp->ar_hln = 6;
262 arp->ar_pln = 4;
Luca Ceresoli7cbd7482011-05-11 03:59:56 +0000263 arp->ar_op = htons(ARPOP_REQUEST);
wdenke6466f62003-06-05 19:27:42 +0000264
Luca Ceresoli5c83a962011-05-11 03:59:54 +0000265 /* source ET addr */
Luca Ceresoli7cbd7482011-05-11 03:59:56 +0000266 memcpy(&arp->ar_data[0], NetOurEther, 6);
Luca Ceresoli5c83a962011-05-11 03:59:54 +0000267 /* source IP addr */
Luca Ceresoli7cbd7482011-05-11 03:59:56 +0000268 NetWriteIP((uchar *) &arp->ar_data[6], NetOurIP);
wdenk05939202004-04-18 17:39:38 +0000269 for (i = 10; i < 16; ++i) {
Luca Ceresoli5c83a962011-05-11 03:59:54 +0000270 /* dest ET addr = 0 */
271 arp->ar_data[i] = 0;
wdenke6466f62003-06-05 19:27:42 +0000272 }
273
wdenk05939202004-04-18 17:39:38 +0000274 if ((NetArpWaitPacketIP & NetOurSubnetMask) !=
275 (NetOurIP & NetOurSubnetMask)) {
276 if (NetOurGatewayIP == 0) {
Luca Ceresoli7cbd7482011-05-11 03:59:56 +0000277 puts("## Warning: gatewayip needed but not set\n");
Wolfgang Denk98ceffb2006-03-12 01:13:30 +0100278 NetArpWaitReplyIP = NetArpWaitPacketIP;
279 } else {
280 NetArpWaitReplyIP = NetOurGatewayIP;
wdenk05939202004-04-18 17:39:38 +0000281 }
wdenk05939202004-04-18 17:39:38 +0000282 } else {
283 NetArpWaitReplyIP = NetArpWaitPacketIP;
284 }
wdenke6466f62003-06-05 19:27:42 +0000285
Luca Ceresoli7cbd7482011-05-11 03:59:56 +0000286 NetWriteIP((uchar *) &arp->ar_data[16], NetArpWaitReplyIP);
287 (void) eth_send(NetTxPacket, (pkt - NetTxPacket) + ARP_HDR_SIZE);
wdenke6466f62003-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 Sieka56668462008-10-01 15:26:28 +0200300 if ((t - NetArpWaitTimerStart) > ARP_TIMEOUT) {
wdenke6466f62003-06-05 19:27:42 +0000301 NetArpWaitTry++;
302
303 if (NetArpWaitTry >= ARP_TIMEOUT_COUNT) {
Luca Ceresoli7cbd7482011-05-11 03:59:56 +0000304 puts("\nARP Retry count exceeded; starting again\n");
wdenke6466f62003-06-05 19:27:42 +0000305 NetArpWaitTry = 0;
306 NetStartAgain();
307 } else {
308 NetArpWaitTimerStart = t;
309 ArpRequest();
310 }
311 }
312}
313
Simon Glassd6c5f552011-10-24 18:00:02 +0000314static void NetInitLoop(enum proto_t protocol)
Heiko Schocher0c303fa2009-02-10 09:38:52 +0100315{
Luca Ceresolid01bc1c2011-05-11 03:59:55 +0000316 static int env_changed_id;
Heiko Schocher0c303fa2009-02-10 09:38:52 +0100317 bd_t *bd = gd->bd;
Luca Ceresoli7cbd7482011-05-11 03:59:56 +0000318 int env_id = get_env_id();
Heiko Schocher0c303fa2009-02-10 09:38:52 +0100319
320 /* update only when the environment has changed */
Michael Zaidmanb97bfe42009-04-04 01:43:00 +0300321 if (env_changed_id != env_id) {
Enric Balletbo i Serra7019d252011-05-31 21:01:47 +0000322 NetOurIP = getenv_IPaddr("ipaddr");
323 NetCopyIP(&bd->bi_ip_addr, &NetOurIP);
Luca Ceresoli7cbd7482011-05-11 03:59:56 +0000324 NetOurGatewayIP = getenv_IPaddr("gatewayip");
325 NetOurSubnetMask = getenv_IPaddr("netmask");
326 NetServerIP = getenv_IPaddr("serverip");
Heiko Schocher0c303fa2009-02-10 09:38:52 +0100327 NetOurNativeVLAN = getenv_VLAN("nvlan");
Michael Zaidmanb97bfe42009-04-04 01:43:00 +0300328 NetOurVLAN = getenv_VLAN("vlan");
Robin Getz82f0d232009-07-20 14:53:54 -0400329#if defined(CONFIG_CMD_DNS)
330 NetOurDNSIP = getenv_IPaddr("dnsip");
331#endif
Michael Zaidmanb97bfe42009-04-04 01:43:00 +0300332 env_changed_id = env_id;
Heiko Schocher0c303fa2009-02-10 09:38:52 +0100333 }
Michael Zaidmanb97bfe42009-04-04 01:43:00 +0300334
Heiko Schocher3b195ff2009-04-28 08:36:11 +0200335 return;
Heiko Schocher0c303fa2009-02-10 09:38:52 +0100336}
337
wdenke6466f62003-06-05 19:27:42 +0000338/**********************************************************************/
wdenk2d966952002-10-31 22:12:35 +0000339/*
340 * Main network processing loop.
341 */
342
Simon Glassd6c5f552011-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 Glass230467c2011-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
wdenke6466f62003-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 Bohmer0ddb8e82009-10-28 22:13:39 +0100358 NetTryCount = 1;
wdenke6466f62003-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 Ceresolie8ccbb02011-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;
wdenke6466f62003-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();
wdenk145d2c12004-04-15 21:48:45 +0000378 eth_set_current();
wdenkfa66e932005-04-03 14:52:59 +0000379 if (eth_init(bd) < 0) {
380 eth_halt();
Luca Ceresoli5fe6de22011-05-04 02:40:45 +0000381 return -1;
wdenkfa66e932005-04-03 14:52:59 +0000382 }
wdenk2d966952002-10-31 22:12:35 +0000383
384restart:
Luca Ceresoli7cbd7482011-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 Schocher0c303fa2009-02-10 09:38:52 +0100394 NetInitLoop(protocol);
wdenk2d966952002-10-31 22:12:35 +0000395
Luca Ceresoli7cbd7482011-05-11 03:59:56 +0000396 switch (net_check_prereq(protocol)) {
wdenk2d966952002-10-31 22:12:35 +0000397 case 1:
398 /* network not configured */
wdenkfa66e932005-04-03 14:52:59 +0000399 eth_halt();
Luca Ceresoli5fe6de22011-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 Glassd6c5f552011-10-24 18:00:02 +0000408 NetBootFileXferSize = 0;
wdenk2d966952002-10-31 22:12:35 +0000409 switch (protocol) {
Simon Glassd6c5f552011-10-24 18:00:02 +0000410 case TFTPGET:
Simon Glass6a398d22011-10-24 18:00:07 +0000411#ifdef CONFIG_CMD_TFTPPUT
412 case TFTPPUT:
413#endif
wdenk2d966952002-10-31 22:12:35 +0000414 /* always use ARP to get server ethernet address */
Simon Glassd6c5f552011-10-24 18:00:02 +0000415 TftpStart(protocol);
wdenk2d966952002-10-31 22:12:35 +0000416 break;
Luca Ceresoli7aa81a42011-05-17 00:03:40 +0000417#ifdef CONFIG_CMD_TFTPSRV
418 case TFTPSRV:
419 TftpStartServer();
420 break;
421#endif
Jon Loeliger54f35c22007-07-09 17:45:14 -0500422#if defined(CONFIG_CMD_DHCP)
wdenk2d966952002-10-31 22:12:35 +0000423 case DHCP:
wdenkc3919532004-10-11 22:51:13 +0000424 BootpTry = 0;
Michael Zaidman16bb21d2009-07-14 23:37:12 +0300425 NetOurIP = 0;
wdenk2d966952002-10-31 22:12:35 +0000426 DhcpRequest(); /* Basically same as BOOTP */
427 break;
Jon Loeligera9807e52007-07-10 11:05:02 -0500428#endif
wdenk2d966952002-10-31 22:12:35 +0000429
430 case BOOTP:
431 BootpTry = 0;
Michael Zaidman16bb21d2009-07-14 23:37:12 +0300432 NetOurIP = 0;
Luca Ceresoli7cbd7482011-05-11 03:59:56 +0000433 BootpRequest();
wdenk2d966952002-10-31 22:12:35 +0000434 break;
435
Peter Tyserf7a48ca2010-09-30 11:25:48 -0500436#if defined(CONFIG_CMD_RARP)
wdenk2d966952002-10-31 22:12:35 +0000437 case RARP:
438 RarpTry = 0;
Michael Zaidman16bb21d2009-07-14 23:37:12 +0300439 NetOurIP = 0;
Luca Ceresoli7cbd7482011-05-11 03:59:56 +0000440 RarpRequest();
wdenk2d966952002-10-31 22:12:35 +0000441 break;
Peter Tyserf7a48ca2010-09-30 11:25:48 -0500442#endif
Jon Loeliger54f35c22007-07-09 17:45:14 -0500443#if defined(CONFIG_CMD_PING)
wdenke6466f62003-06-05 19:27:42 +0000444 case PING:
445 PingStart();
446 break;
447#endif
Jon Loeliger54f35c22007-07-09 17:45:14 -0500448#if defined(CONFIG_CMD_NFS)
wdenkbe9c1cb2004-02-24 02:00:03 +0000449 case NFS:
450 NfsStart();
451 break;
452#endif
Jon Loeliger54f35c22007-07-09 17:45:14 -0500453#if defined(CONFIG_CMD_CDP)
wdenk145d2c12004-04-15 21:48:45 +0000454 case CDP:
455 CDPStart();
456 break;
457#endif
wdenkb8fb6192004-08-02 21:11:11 +0000458#ifdef CONFIG_NETCONSOLE
459 case NETCONS:
460 NcStart();
461 break;
462#endif
Jon Loeliger54f35c22007-07-09 17:45:14 -0500463#if defined(CONFIG_CMD_SNTP)
wdenkb4ad9622005-04-01 00:25:43 +0000464 case SNTP:
465 SntpStart();
466 break;
467#endif
Robin Getz82f0d232009-07-20 14:53:54 -0400468#if defined(CONFIG_CMD_DNS)
469 case DNS:
470 DnsStart();
471 break;
472#endif
wdenk2d966952002-10-31 22:12:35 +0000473 default:
474 break;
475 }
476
wdenk2d966952002-10-31 22:12:35 +0000477 break;
478 }
479
Jon Loeliger54f35c22007-07-09 17:45:14 -0500480#if defined(CONFIG_MII) || defined(CONFIG_CMD_MII)
Luca Ceresoli5c83a962011-05-11 03:59:54 +0000481#if defined(CONFIG_SYS_FAULT_ECHO_LINK_DOWN) && \
482 defined(CONFIG_STATUS_LED) && \
483 defined(STATUS_LED_RED)
wdenk49c3f672003-10-08 22:33:00 +0000484 /*
wdenk9c53f402003-10-15 23:53:47 +0000485 * Echo the inverted link state to the fault LED.
wdenk49c3f672003-10-08 22:33:00 +0000486 */
Luca Ceresolie8ccbb02011-05-04 02:40:43 +0000487 if (miiphy_link(eth_get_dev()->name, CONFIG_SYS_FAULT_MII_ADDR))
Luca Ceresoli7cbd7482011-05-11 03:59:56 +0000488 status_led_set(STATUS_LED_RED, STATUS_LED_OFF);
Luca Ceresolie8ccbb02011-05-04 02:40:43 +0000489 else
Luca Ceresoli7cbd7482011-05-11 03:59:56 +0000490 status_led_set(STATUS_LED_RED, STATUS_LED_ON);
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200491#endif /* CONFIG_SYS_FAULT_ECHO_LINK_DOWN, ... */
wdenk49c3f672003-10-08 22:33:00 +0000492#endif /* CONFIG_MII, ... */
wdenk2d966952002-10-31 22:12:35 +0000493
494 /*
495 * Main packet reception loop. Loop receiving packets until
wdenk27fa5852005-04-03 14:18:51 +0000496 * someone sets `NetState' to a state that terminates.
wdenk2d966952002-10-31 22:12:35 +0000497 */
498 for (;;) {
499 WATCHDOG_RESET();
500#ifdef CONFIG_SHOW_ACTIVITY
501 {
502 extern void show_activity(int arg);
503 show_activity(1);
504 }
505#endif
506 /*
507 * Check the ethernet for a new packet. The ethernet
508 * receive routine will process it.
509 */
Guennadi Liakhovetskib38c2b32008-04-03 17:04:19 +0200510 eth_rx();
wdenk2d966952002-10-31 22:12:35 +0000511
512 /*
513 * Abort if ctrl-c was pressed.
514 */
515 if (ctrlc()) {
wdenk57b2d802003-06-27 21:31:46 +0000516 eth_halt();
Luca Ceresoli7cbd7482011-05-11 03:59:56 +0000517 puts("\nAbort\n");
Simon Glass230467c2011-10-24 18:00:01 +0000518 goto done;
wdenk2d966952002-10-31 22:12:35 +0000519 }
520
wdenke6466f62003-06-05 19:27:42 +0000521 ArpTimeoutCheck();
wdenk2d966952002-10-31 22:12:35 +0000522
523 /*
524 * Check for a timeout, and run the timeout handler
525 * if we have one.
526 */
wdenk5d841732003-08-17 18:55:18 +0000527 if (timeHandler && ((get_timer(0) - timeStart) > timeDelta)) {
wdenk2d966952002-10-31 22:12:35 +0000528 thand_f *x;
529
Jon Loeliger54f35c22007-07-09 17:45:14 -0500530#if defined(CONFIG_MII) || defined(CONFIG_CMD_MII)
Luca Ceresoli7cbd7482011-05-11 03:59:56 +0000531#if defined(CONFIG_SYS_FAULT_ECHO_LINK_DOWN) && \
532 defined(CONFIG_STATUS_LED) && \
533 defined(STATUS_LED_RED)
wdenk49c3f672003-10-08 22:33:00 +0000534 /*
wdenk9c53f402003-10-15 23:53:47 +0000535 * Echo the inverted link state to the fault LED.
wdenk49c3f672003-10-08 22:33:00 +0000536 */
Luca Ceresoli7cbd7482011-05-11 03:59:56 +0000537 if (miiphy_link(eth_get_dev()->name,
Luca Ceresoli5c83a962011-05-11 03:59:54 +0000538 CONFIG_SYS_FAULT_MII_ADDR)) {
Luca Ceresoli7cbd7482011-05-11 03:59:56 +0000539 status_led_set(STATUS_LED_RED, STATUS_LED_OFF);
wdenk49c3f672003-10-08 22:33:00 +0000540 } else {
Luca Ceresoli7cbd7482011-05-11 03:59:56 +0000541 status_led_set(STATUS_LED_RED, STATUS_LED_ON);
wdenk49c3f672003-10-08 22:33:00 +0000542 }
Luca Ceresoli7cbd7482011-05-11 03:59:56 +0000543#endif /* CONFIG_SYS_FAULT_ECHO_LINK_DOWN, ... */
wdenk49c3f672003-10-08 22:33:00 +0000544#endif /* CONFIG_MII, ... */
wdenk2d966952002-10-31 22:12:35 +0000545 x = timeHandler;
546 timeHandler = (thand_f *)0;
547 (*x)();
548 }
549
550
551 switch (NetState) {
552
553 case NETLOOP_RESTART:
wdenk2d966952002-10-31 22:12:35 +0000554 NetRestarted = 1;
wdenk2d966952002-10-31 22:12:35 +0000555 goto restart;
556
557 case NETLOOP_SUCCESS:
558 if (NetBootFileXferSize > 0) {
Wolfgang Denk084d0ce2007-09-12 00:48:57 +0200559 char buf[20];
wdenk2d966952002-10-31 22:12:35 +0000560 printf("Bytes transferred = %ld (%lx hex)\n",
561 NetBootFileXferSize,
562 NetBootFileXferSize);
Wolfgang Denk084d0ce2007-09-12 00:48:57 +0200563 sprintf(buf, "%lX", NetBootFileXferSize);
wdenk2d966952002-10-31 22:12:35 +0000564 setenv("filesize", buf);
wdenk145d2c12004-04-15 21:48:45 +0000565
566 sprintf(buf, "%lX", (unsigned long)load_addr);
567 setenv("fileaddr", buf);
wdenk2d966952002-10-31 22:12:35 +0000568 }
569 eth_halt();
Simon Glass230467c2011-10-24 18:00:01 +0000570 ret = NetBootFileXferSize;
571 goto done;
wdenk2d966952002-10-31 22:12:35 +0000572
573 case NETLOOP_FAIL:
Simon Glass230467c2011-10-24 18:00:01 +0000574 goto done;
wdenk2d966952002-10-31 22:12:35 +0000575 }
576 }
Simon Glass230467c2011-10-24 18:00:01 +0000577
578done:
579 /* Clear out the handlers */
580 NetSetHandler(NULL);
581 net_set_icmp_handler(NULL);
582 return ret;
wdenk2d966952002-10-31 22:12:35 +0000583}
584
585/**********************************************************************/
586
587static void
588startAgainTimeout(void)
589{
590 NetState = NETLOOP_RESTART;
591}
592
593static void
Luca Ceresoli428ab362011-04-18 06:19:50 +0000594startAgainHandler(uchar *pkt, unsigned dest, IPaddr_t sip,
595 unsigned src, unsigned len)
wdenk2d966952002-10-31 22:12:35 +0000596{
597 /* Totally ignore the packet */
598}
599
Luca Ceresoli7cbd7482011-05-11 03:59:56 +0000600void NetStartAgain(void)
wdenk2d966952002-10-31 22:12:35 +0000601{
wdenk05939202004-04-18 17:39:38 +0000602 char *nretry;
Remy Bohmer0ddb8e82009-10-28 22:13:39 +0100603 int retry_forever = 0;
604 unsigned long retrycnt = 0;
wdenk145d2c12004-04-15 21:48:45 +0000605
Remy Bohmer0ddb8e82009-10-28 22:13:39 +0100606 nretry = getenv("netretry");
607 if (nretry) {
608 if (!strcmp(nretry, "yes"))
609 retry_forever = 1;
610 else if (!strcmp(nretry, "no"))
611 retrycnt = 0;
612 else if (!strcmp(nretry, "once"))
613 retrycnt = 1;
614 else
615 retrycnt = simple_strtoul(nretry, NULL, 0);
616 } else
617 retry_forever = 1;
618
619 if ((!retry_forever) && (NetTryCount >= retrycnt)) {
620 eth_halt();
wdenk145d2c12004-04-15 21:48:45 +0000621 NetState = NETLOOP_FAIL;
622 return;
623 }
Remy Bohmer0ddb8e82009-10-28 22:13:39 +0100624
625 NetTryCount++;
626
Luca Ceresoli7cbd7482011-05-11 03:59:56 +0000627 eth_halt();
Matthias Fuchsa02d62b2007-12-27 16:58:41 +0100628#if !defined(CONFIG_NET_DO_NOT_TRY_ANOTHER)
Luca Ceresoli7cbd7482011-05-11 03:59:56 +0000629 eth_try_another(!NetRestarted);
Matthias Fuchsa02d62b2007-12-27 16:58:41 +0100630#endif
Luca Ceresoli7cbd7482011-05-11 03:59:56 +0000631 eth_init(gd->bd);
wdenk05939202004-04-18 17:39:38 +0000632 if (NetRestartWrap) {
wdenk2d966952002-10-31 22:12:35 +0000633 NetRestartWrap = 0;
Remy Bohmer0ddb8e82009-10-28 22:13:39 +0100634 if (NetDevExists) {
Luca Ceresoli7cbd7482011-05-11 03:59:56 +0000635 NetSetTimeout(10000UL, startAgainTimeout);
636 NetSetHandler(startAgainHandler);
wdenk05939202004-04-18 17:39:38 +0000637 } else {
wdenk2d966952002-10-31 22:12:35 +0000638 NetState = NETLOOP_FAIL;
639 }
wdenk05939202004-04-18 17:39:38 +0000640 } else {
wdenk2d966952002-10-31 22:12:35 +0000641 NetState = NETLOOP_RESTART;
642 }
wdenk2d966952002-10-31 22:12:35 +0000643}
644
645/**********************************************************************/
646/*
647 * Miscelaneous bits.
648 */
649
650void
Luca Ceresolif27d91f2011-05-04 02:40:44 +0000651NetSetHandler(rxhand_f *f)
wdenk2d966952002-10-31 22:12:35 +0000652{
653 packetHandler = f;
654}
655
Simon Glass230467c2011-10-24 18:00:01 +0000656void net_set_icmp_handler(rxhand_icmp_f *f)
657{
658 packet_icmp_handler = f;
659}
wdenk2d966952002-10-31 22:12:35 +0000660
661void
Luca Ceresolif27d91f2011-05-04 02:40:44 +0000662NetSetTimeout(ulong iv, thand_f *f)
wdenk2d966952002-10-31 22:12:35 +0000663{
664 if (iv == 0) {
665 timeHandler = (thand_f *)0;
666 } else {
667 timeHandler = f;
wdenk5d841732003-08-17 18:55:18 +0000668 timeStart = get_timer(0);
669 timeDelta = iv;
wdenk2d966952002-10-31 22:12:35 +0000670 }
671}
672
673
674void
Luca Ceresolif27d91f2011-05-04 02:40:44 +0000675NetSendPacket(volatile uchar *pkt, int len)
wdenk2d966952002-10-31 22:12:35 +0000676{
677 (void) eth_send(pkt, len);
678}
679
wdenke6466f62003-06-05 19:27:42 +0000680int
681NetSendUDPPacket(uchar *ether, IPaddr_t dest, int dport, int sport, int len)
682{
wdenk145d2c12004-04-15 21:48:45 +0000683 uchar *pkt;
684
wdenke6466f62003-06-05 19:27:42 +0000685 /* convert to new style broadcast */
686 if (dest == 0)
687 dest = 0xFFFFFFFF;
688
689 /* if broadcast, make the ether address a broadcast and don't do ARP */
690 if (dest == 0xFFFFFFFF)
691 ether = NetBcastAddr;
692
Luca Ceresoli5c83a962011-05-11 03:59:54 +0000693 /*
694 * if MAC address was not discovered yet, save the packet and do
695 * an ARP request
696 */
wdenke6466f62003-06-05 19:27:42 +0000697 if (memcmp(ether, NetEtherNullAddr, 6) == 0) {
698
Robin Getz9e0a4d62009-07-23 03:01:03 -0400699 debug("sending ARP for %08lx\n", dest);
700
wdenke6466f62003-06-05 19:27:42 +0000701 NetArpWaitPacketIP = dest;
702 NetArpWaitPacketMAC = ether;
wdenk145d2c12004-04-15 21:48:45 +0000703
704 pkt = NetArpWaitTxPacket;
Luca Ceresoli7cbd7482011-05-11 03:59:56 +0000705 pkt += NetSetEther(pkt, NetArpWaitPacketMAC, PROT_IP);
wdenk145d2c12004-04-15 21:48:45 +0000706
Luca Ceresoli7cbd7482011-05-11 03:59:56 +0000707 NetSetIP(pkt, dest, dport, sport, len);
Luca Ceresoli5c83a962011-05-11 03:59:54 +0000708 memcpy(pkt + IP_HDR_SIZE, (uchar *)NetTxPacket +
709 (pkt - (uchar *)NetArpWaitTxPacket) + IP_HDR_SIZE, len);
wdenke6466f62003-06-05 19:27:42 +0000710
711 /* size of the waiting packet */
Luca Ceresoli5c83a962011-05-11 03:59:54 +0000712 NetArpWaitTxPacketSize = (pkt - NetArpWaitTxPacket) +
713 IP_HDR_SIZE + len;
wdenke6466f62003-06-05 19:27:42 +0000714
715 /* and do the ARP request */
716 NetArpWaitTry = 1;
717 NetArpWaitTimerStart = get_timer(0);
718 ArpRequest();
719 return 1; /* waiting */
720 }
721
Robin Getz9e0a4d62009-07-23 03:01:03 -0400722 debug("sending UDP to %08lx/%pM\n", dest, ether);
wdenke6466f62003-06-05 19:27:42 +0000723
wdenk145d2c12004-04-15 21:48:45 +0000724 pkt = (uchar *)NetTxPacket;
Luca Ceresoli7cbd7482011-05-11 03:59:56 +0000725 pkt += NetSetEther(pkt, ether, PROT_IP);
726 NetSetIP(pkt, dest, dport, sport, len);
wdenk145d2c12004-04-15 21:48:45 +0000727 (void) eth_send(NetTxPacket, (pkt - NetTxPacket) + IP_HDR_SIZE + len);
wdenke6466f62003-06-05 19:27:42 +0000728
wdenk05939202004-04-18 17:39:38 +0000729 return 0; /* transmitted */
wdenke6466f62003-06-05 19:27:42 +0000730}
731
Jon Loeliger54f35c22007-07-09 17:45:14 -0500732#if defined(CONFIG_CMD_PING)
wdenke6466f62003-06-05 19:27:42 +0000733static ushort PingSeqNo;
734
735int PingSend(void)
736{
737 static uchar mac[6];
738 volatile IP_t *ip;
739 volatile ushort *s;
wdenk145d2c12004-04-15 21:48:45 +0000740 uchar *pkt;
wdenke6466f62003-06-05 19:27:42 +0000741
742 /* XXX always send arp request */
743
744 memcpy(mac, NetEtherNullAddr, 6);
745
Robin Getz9e0a4d62009-07-23 03:01:03 -0400746 debug("sending ARP for %08lx\n", NetPingIP);
wdenke6466f62003-06-05 19:27:42 +0000747
748 NetArpWaitPacketIP = NetPingIP;
749 NetArpWaitPacketMAC = mac;
750
wdenk145d2c12004-04-15 21:48:45 +0000751 pkt = NetArpWaitTxPacket;
752 pkt += NetSetEther(pkt, mac, PROT_IP);
wdenke6466f62003-06-05 19:27:42 +0000753
wdenk145d2c12004-04-15 21:48:45 +0000754 ip = (volatile IP_t *)pkt;
wdenke6466f62003-06-05 19:27:42 +0000755
756 /*
Luca Ceresoli5c83a962011-05-11 03:59:54 +0000757 * Construct an IP and ICMP header.
758 * (need to set no fragment bit - XXX)
wdenke6466f62003-06-05 19:27:42 +0000759 */
Luca Ceresoli5c83a962011-05-11 03:59:54 +0000760 /* IP_HDR_SIZE / 4 (not including UDP) */
761 ip->ip_hl_v = 0x45;
wdenke6466f62003-06-05 19:27:42 +0000762 ip->ip_tos = 0;
763 ip->ip_len = htons(IP_HDR_SIZE_NO_UDP + 8);
764 ip->ip_id = htons(NetIPID++);
Peter Tyser0885bf02008-12-01 16:26:20 -0600765 ip->ip_off = htons(IP_FLAGS_DFRAG); /* Don't fragment */
wdenke6466f62003-06-05 19:27:42 +0000766 ip->ip_ttl = 255;
767 ip->ip_p = 0x01; /* ICMP */
768 ip->ip_sum = 0;
Luca Ceresoli5c83a962011-05-11 03:59:54 +0000769 /* already in network byte order */
Luca Ceresolif27d91f2011-05-04 02:40:44 +0000770 NetCopyIP((void *)&ip->ip_src, &NetOurIP);
Luca Ceresoli5c83a962011-05-11 03:59:54 +0000771 /* - "" - */
Luca Ceresolif27d91f2011-05-04 02:40:44 +0000772 NetCopyIP((void *)&ip->ip_dst, &NetPingIP);
wdenke6466f62003-06-05 19:27:42 +0000773 ip->ip_sum = ~NetCksum((uchar *)ip, IP_HDR_SIZE_NO_UDP / 2);
774
775 s = &ip->udp_src; /* XXX ICMP starts here */
776 s[0] = htons(0x0800); /* echo-request, code */
777 s[1] = 0; /* checksum */
Wolfgang Denka1be4762008-05-20 16:00:29 +0200778 s[2] = 0; /* identifier */
wdenke6466f62003-06-05 19:27:42 +0000779 s[3] = htons(PingSeqNo++); /* sequence number */
780 s[1] = ~NetCksum((uchar *)s, 8/2);
781
782 /* size of the waiting packet */
Luca Ceresoli5c83a962011-05-11 03:59:54 +0000783 NetArpWaitTxPacketSize =
784 (pkt - NetArpWaitTxPacket) + IP_HDR_SIZE_NO_UDP + 8;
wdenke6466f62003-06-05 19:27:42 +0000785
786 /* and do the ARP request */
787 NetArpWaitTry = 1;
788 NetArpWaitTimerStart = get_timer(0);
789 ArpRequest();
790 return 1; /* waiting */
791}
792
793static void
Luca Ceresoli7cbd7482011-05-11 03:59:56 +0000794PingTimeout(void)
wdenke6466f62003-06-05 19:27:42 +0000795{
796 eth_halt();
797 NetState = NETLOOP_FAIL; /* we did not get the reply */
798}
799
800static void
Luca Ceresoli428ab362011-04-18 06:19:50 +0000801PingHandler(uchar *pkt, unsigned dest, IPaddr_t sip, unsigned src,
802 unsigned len)
wdenke6466f62003-06-05 19:27:42 +0000803{
Luca Ceresoli428ab362011-04-18 06:19:50 +0000804 if (sip != NetPingIP)
wdenke6466f62003-06-05 19:27:42 +0000805 return;
806
807 NetState = NETLOOP_SUCCESS;
808}
809
810static void PingStart(void)
811{
Luca Ceresoli7cbd7482011-05-11 03:59:56 +0000812 printf("Using %s device\n", eth_get_name());
Luca Ceresoli7cbd7482011-05-11 03:59:56 +0000813 NetSetTimeout(10000UL, PingTimeout);
814 NetSetHandler(PingHandler);
wdenke6466f62003-06-05 19:27:42 +0000815
816 PingSend();
817}
Jon Loeligera9807e52007-07-10 11:05:02 -0500818#endif
wdenk145d2c12004-04-15 21:48:45 +0000819
Jon Loeliger54f35c22007-07-09 17:45:14 -0500820#if defined(CONFIG_CMD_CDP)
wdenk145d2c12004-04-15 21:48:45 +0000821
822#define CDP_DEVICE_ID_TLV 0x0001
823#define CDP_ADDRESS_TLV 0x0002
824#define CDP_PORT_ID_TLV 0x0003
825#define CDP_CAPABILITIES_TLV 0x0004
826#define CDP_VERSION_TLV 0x0005
827#define CDP_PLATFORM_TLV 0x0006
828#define CDP_NATIVE_VLAN_TLV 0x000a
829#define CDP_APPLIANCE_VLAN_TLV 0x000e
830#define CDP_TRIGGER_TLV 0x000f
831#define CDP_POWER_CONSUMPTION_TLV 0x0010
832#define CDP_SYSNAME_TLV 0x0014
833#define CDP_SYSOBJECT_TLV 0x0015
834#define CDP_MANAGEMENT_ADDRESS_TLV 0x0016
835
Bartlomiej Sieka56668462008-10-01 15:26:28 +0200836#define CDP_TIMEOUT 250UL /* one packet every 250ms */
wdenk145d2c12004-04-15 21:48:45 +0000837
838static int CDPSeq;
839static int CDPOK;
840
841ushort CDPNativeVLAN;
842ushort CDPApplianceVLAN;
843
Luca Ceresoli5c83a962011-05-11 03:59:54 +0000844static const uchar CDP_SNAP_hdr[8] = { 0xAA, 0xAA, 0x03, 0x00, 0x00, 0x0C, 0x20,
845 0x00 };
wdenk145d2c12004-04-15 21:48:45 +0000846
847static ushort CDP_compute_csum(const uchar *buff, ushort len)
848{
849 ushort csum;
850 int odd;
851 ulong result = 0;
852 ushort leftover;
Wolfgang Denk7fb52662005-10-13 16:45:02 +0200853 ushort *p;
wdenk145d2c12004-04-15 21:48:45 +0000854
855 if (len > 0) {
856 odd = 1 & (ulong)buff;
857 if (odd) {
858 result = *buff << 8;
859 len--;
860 buff++;
861 }
862 while (len > 1) {
Wolfgang Denk7fb52662005-10-13 16:45:02 +0200863 p = (ushort *)buff;
864 result += *p++;
865 buff = (uchar *)p;
wdenk145d2c12004-04-15 21:48:45 +0000866 if (result & 0x80000000)
867 result = (result & 0xFFFF) + (result >> 16);
868 len -= 2;
869 }
870 if (len) {
871 leftover = (signed short)(*(const signed char *)buff);
Wolfgang Denkf0711132005-11-10 20:59:46 +0100872 /* CISCO SUCKS big time! (and blows too):
873 * CDP uses the IP checksum algorithm with a twist;
874 * for the last byte it *sign* extends and sums.
875 */
Luca Ceresoli5c83a962011-05-11 03:59:54 +0000876 result = (result & 0xffff0000) |
877 ((result + leftover) & 0x0000ffff);
wdenk145d2c12004-04-15 21:48:45 +0000878 }
879 while (result >> 16)
880 result = (result & 0xFFFF) + (result >> 16);
881
882 if (odd)
Luca Ceresoli5c83a962011-05-11 03:59:54 +0000883 result = ((result >> 8) & 0xff) |
884 ((result & 0xff) << 8);
wdenk145d2c12004-04-15 21:48:45 +0000885 }
886
887 /* add up 16-bit and 17-bit words for 17+c bits */
888 result = (result & 0xffff) + (result >> 16);
889 /* add up 16-bit and 2-bit for 16+c bit */
890 result = (result & 0xffff) + (result >> 16);
891 /* add up carry.. */
892 result = (result & 0xffff) + (result >> 16);
893
894 /* negate */
895 csum = ~(ushort)result;
896
897 /* run time endian detection */
898 if (csum != htons(csum)) /* little endian */
899 csum = htons(csum);
900
901 return csum;
902}
903
904int CDPSendTrigger(void)
905{
906 volatile uchar *pkt;
907 volatile ushort *s;
908 volatile ushort *cp;
909 Ethernet_t *et;
wdenk145d2c12004-04-15 21:48:45 +0000910 int len;
911 ushort chksum;
Luca Ceresoli7cbd7482011-05-11 03:59:56 +0000912#if defined(CONFIG_CDP_DEVICE_ID) || defined(CONFIG_CDP_PORT_ID) || \
913 defined(CONFIG_CDP_VERSION) || defined(CONFIG_CDP_PLATFORM)
wdenk05939202004-04-18 17:39:38 +0000914 char buf[32];
915#endif
wdenk145d2c12004-04-15 21:48:45 +0000916
917 pkt = NetTxPacket;
918 et = (Ethernet_t *)pkt;
919
920 /* NOTE: trigger sent not on any VLAN */
921
922 /* form ethernet header */
923 memcpy(et->et_dest, NetCDPAddr, 6);
924 memcpy(et->et_src, NetOurEther, 6);
925
926 pkt += ETHER_HDR_SIZE;
927
928 /* SNAP header */
929 memcpy((uchar *)pkt, CDP_SNAP_hdr, sizeof(CDP_SNAP_hdr));
930 pkt += sizeof(CDP_SNAP_hdr);
931
932 /* CDP header */
933 *pkt++ = 0x02; /* CDP version 2 */
934 *pkt++ = 180; /* TTL */
935 s = (volatile ushort *)pkt;
936 cp = s;
Luca Ceresoli5c83a962011-05-11 03:59:54 +0000937 /* checksum (0 for later calculation) */
938 *s++ = htons(0);
wdenk145d2c12004-04-15 21:48:45 +0000939
940 /* CDP fields */
941#ifdef CONFIG_CDP_DEVICE_ID
942 *s++ = htons(CDP_DEVICE_ID_TLV);
943 *s++ = htons(CONFIG_CDP_DEVICE_ID);
Mike Frysinger79ca1b92009-02-11 18:23:48 -0500944 sprintf(buf, CONFIG_CDP_DEVICE_ID_PREFIX "%pm", NetOurEther);
wdenk145d2c12004-04-15 21:48:45 +0000945 memcpy((uchar *)s, buf, 16);
946 s += 16 / 2;
947#endif
948
949#ifdef CONFIG_CDP_PORT_ID
950 *s++ = htons(CDP_PORT_ID_TLV);
951 memset(buf, 0, sizeof(buf));
952 sprintf(buf, CONFIG_CDP_PORT_ID, eth_get_dev_index());
953 len = strlen(buf);
954 if (len & 1) /* make it even */
955 len++;
956 *s++ = htons(len + 4);
957 memcpy((uchar *)s, buf, len);
958 s += len / 2;
959#endif
960
961#ifdef CONFIG_CDP_CAPABILITIES
962 *s++ = htons(CDP_CAPABILITIES_TLV);
963 *s++ = htons(8);
964 *(ulong *)s = htonl(CONFIG_CDP_CAPABILITIES);
965 s += 2;
966#endif
967
968#ifdef CONFIG_CDP_VERSION
969 *s++ = htons(CDP_VERSION_TLV);
970 memset(buf, 0, sizeof(buf));
971 strcpy(buf, CONFIG_CDP_VERSION);
972 len = strlen(buf);
973 if (len & 1) /* make it even */
974 len++;
975 *s++ = htons(len + 4);
976 memcpy((uchar *)s, buf, len);
977 s += len / 2;
978#endif
979
980#ifdef CONFIG_CDP_PLATFORM
981 *s++ = htons(CDP_PLATFORM_TLV);
982 memset(buf, 0, sizeof(buf));
983 strcpy(buf, CONFIG_CDP_PLATFORM);
984 len = strlen(buf);
985 if (len & 1) /* make it even */
986 len++;
987 *s++ = htons(len + 4);
988 memcpy((uchar *)s, buf, len);
989 s += len / 2;
990#endif
991
992#ifdef CONFIG_CDP_TRIGGER
993 *s++ = htons(CDP_TRIGGER_TLV);
994 *s++ = htons(8);
995 *(ulong *)s = htonl(CONFIG_CDP_TRIGGER);
996 s += 2;
997#endif
998
999#ifdef CONFIG_CDP_POWER_CONSUMPTION
1000 *s++ = htons(CDP_POWER_CONSUMPTION_TLV);
1001 *s++ = htons(6);
1002 *s++ = htons(CONFIG_CDP_POWER_CONSUMPTION);
1003#endif
1004
1005 /* length of ethernet packet */
1006 len = (uchar *)s - ((uchar *)NetTxPacket + ETHER_HDR_SIZE);
1007 et->et_protlen = htons(len);
1008
1009 len = ETHER_HDR_SIZE + sizeof(CDP_SNAP_hdr);
Luca Ceresoli5c83a962011-05-11 03:59:54 +00001010 chksum = CDP_compute_csum((uchar *)NetTxPacket + len,
1011 (uchar *)s - (NetTxPacket + len));
wdenk145d2c12004-04-15 21:48:45 +00001012 if (chksum == 0)
1013 chksum = 0xFFFF;
1014 *cp = htons(chksum);
1015
1016 (void) eth_send(NetTxPacket, (uchar *)s - NetTxPacket);
1017 return 0;
1018}
1019
1020static void
Luca Ceresoli7cbd7482011-05-11 03:59:56 +00001021CDPTimeout(void)
wdenk145d2c12004-04-15 21:48:45 +00001022{
1023 CDPSeq++;
1024
1025 if (CDPSeq < 3) {
Luca Ceresoli7cbd7482011-05-11 03:59:56 +00001026 NetSetTimeout(CDP_TIMEOUT, CDPTimeout);
wdenk145d2c12004-04-15 21:48:45 +00001027 CDPSendTrigger();
1028 return;
1029 }
1030
1031 /* if not OK try again */
1032 if (!CDPOK)
1033 NetStartAgain();
1034 else
1035 NetState = NETLOOP_SUCCESS;
1036}
1037
1038static void
Luca Ceresoli428ab362011-04-18 06:19:50 +00001039CDPDummyHandler(uchar *pkt, unsigned dest, IPaddr_t sip, unsigned src,
1040 unsigned len)
wdenk145d2c12004-04-15 21:48:45 +00001041{
1042 /* nothing */
1043}
1044
1045static void
Luca Ceresolif27d91f2011-05-04 02:40:44 +00001046CDPHandler(const uchar *pkt, unsigned len)
wdenk145d2c12004-04-15 21:48:45 +00001047{
1048 const uchar *t;
1049 const ushort *ss;
1050 ushort type, tlen;
1051 uchar applid;
1052 ushort vlan, nvlan;
1053
1054 /* minimum size? */
1055 if (len < sizeof(CDP_SNAP_hdr) + 4)
1056 goto pkt_short;
1057
1058 /* check for valid CDP SNAP header */
1059 if (memcmp(pkt, CDP_SNAP_hdr, sizeof(CDP_SNAP_hdr)) != 0)
1060 return;
1061
1062 pkt += sizeof(CDP_SNAP_hdr);
1063 len -= sizeof(CDP_SNAP_hdr);
1064
1065 /* Version of CDP protocol must be >= 2 and TTL != 0 */
1066 if (pkt[0] < 0x02 || pkt[1] == 0)
1067 return;
1068
Luca Ceresoli5c83a962011-05-11 03:59:54 +00001069 /*
1070 * if version is greater than 0x02 maybe we'll have a problem;
1071 * output a warning
1072 */
wdenk145d2c12004-04-15 21:48:45 +00001073 if (pkt[0] != 0x02)
1074 printf("** WARNING: CDP packet received with a protocol version %d > 2\n",
1075 pkt[0] & 0xff);
1076
1077 if (CDP_compute_csum(pkt, len) != 0)
1078 return;
1079
1080 pkt += 4;
1081 len -= 4;
1082
1083 vlan = htons(-1);
1084 nvlan = htons(-1);
1085 while (len > 0) {
1086 if (len < 4)
1087 goto pkt_short;
1088
1089 ss = (const ushort *)pkt;
1090 type = ntohs(ss[0]);
1091 tlen = ntohs(ss[1]);
Luca Ceresolie8ccbb02011-05-04 02:40:43 +00001092 if (tlen > len)
wdenk145d2c12004-04-15 21:48:45 +00001093 goto pkt_short;
wdenk145d2c12004-04-15 21:48:45 +00001094
1095 pkt += tlen;
1096 len -= tlen;
1097
1098 ss += 2; /* point ss to the data of the TLV */
1099 tlen -= 4;
1100
1101 switch (type) {
Luca Ceresolib19d0b72011-05-04 02:40:46 +00001102 case CDP_DEVICE_ID_TLV:
1103 break;
1104 case CDP_ADDRESS_TLV:
1105 break;
1106 case CDP_PORT_ID_TLV:
1107 break;
1108 case CDP_CAPABILITIES_TLV:
1109 break;
1110 case CDP_VERSION_TLV:
1111 break;
1112 case CDP_PLATFORM_TLV:
1113 break;
1114 case CDP_NATIVE_VLAN_TLV:
1115 nvlan = *ss;
1116 break;
1117 case CDP_APPLIANCE_VLAN_TLV:
1118 t = (const uchar *)ss;
1119 while (tlen > 0) {
1120 if (tlen < 3)
1121 goto pkt_short;
wdenk145d2c12004-04-15 21:48:45 +00001122
Luca Ceresolib19d0b72011-05-04 02:40:46 +00001123 applid = t[0];
1124 ss = (const ushort *)(t + 1);
wdenk145d2c12004-04-15 21:48:45 +00001125
1126#ifdef CONFIG_CDP_APPLIANCE_VLAN_TYPE
Luca Ceresolib19d0b72011-05-04 02:40:46 +00001127 if (applid == CONFIG_CDP_APPLIANCE_VLAN_TYPE)
1128 vlan = *ss;
wdenk145d2c12004-04-15 21:48:45 +00001129#else
Luca Ceresolib19d0b72011-05-04 02:40:46 +00001130 /* XXX will this work; dunno */
1131 vlan = ntohs(*ss);
wdenk145d2c12004-04-15 21:48:45 +00001132#endif
Luca Ceresolib19d0b72011-05-04 02:40:46 +00001133 t += 3; tlen -= 3;
1134 }
1135 break;
1136 case CDP_TRIGGER_TLV:
1137 break;
1138 case CDP_POWER_CONSUMPTION_TLV:
1139 break;
1140 case CDP_SYSNAME_TLV:
1141 break;
1142 case CDP_SYSOBJECT_TLV:
1143 break;
1144 case CDP_MANAGEMENT_ADDRESS_TLV:
1145 break;
wdenk145d2c12004-04-15 21:48:45 +00001146 }
1147 }
1148
1149 CDPApplianceVLAN = vlan;
1150 CDPNativeVLAN = nvlan;
1151
1152 CDPOK = 1;
1153 return;
1154
1155 pkt_short:
1156 printf("** CDP packet is too short\n");
1157 return;
1158}
1159
1160static void CDPStart(void)
1161{
Luca Ceresoli7cbd7482011-05-11 03:59:56 +00001162 printf("Using %s device\n", eth_get_name());
wdenk145d2c12004-04-15 21:48:45 +00001163 CDPSeq = 0;
1164 CDPOK = 0;
1165
1166 CDPNativeVLAN = htons(-1);
1167 CDPApplianceVLAN = htons(-1);
1168
Luca Ceresoli7cbd7482011-05-11 03:59:56 +00001169 NetSetTimeout(CDP_TIMEOUT, CDPTimeout);
1170 NetSetHandler(CDPDummyHandler);
wdenk145d2c12004-04-15 21:48:45 +00001171
1172 CDPSendTrigger();
1173}
Jon Loeligera9807e52007-07-10 11:05:02 -05001174#endif
wdenk145d2c12004-04-15 21:48:45 +00001175
Alessandro Rubinif119e3d2009-08-07 13:58:56 +02001176#ifdef CONFIG_IP_DEFRAG
1177/*
1178 * This function collects fragments in a single packet, according
1179 * to the algorithm in RFC815. It returns NULL or the pointer to
1180 * a complete packet, in static storage
1181 */
1182#ifndef CONFIG_NET_MAXDEFRAG
1183#define CONFIG_NET_MAXDEFRAG 16384
1184#endif
1185/*
1186 * MAXDEFRAG, above, is chosen in the config file and is real data
1187 * so we need to add the NFS overhead, which is more than TFTP.
1188 * To use sizeof in the internal unnamed structures, we need a real
1189 * instance (can't do "sizeof(struct rpc_t.u.reply))", unfortunately).
1190 * The compiler doesn't complain nor allocates the actual structure
1191 */
1192static struct rpc_t rpc_specimen;
1193#define IP_PKTSIZE (CONFIG_NET_MAXDEFRAG + sizeof(rpc_specimen.u.reply))
1194
1195#define IP_MAXUDP (IP_PKTSIZE - IP_HDR_SIZE_NO_UDP)
1196
1197/*
1198 * this is the packet being assembled, either data or frag control.
1199 * Fragments go by 8 bytes, so this union must be 8 bytes long
1200 */
1201struct hole {
1202 /* first_byte is address of this structure */
1203 u16 last_byte; /* last byte in this hole + 1 (begin of next hole) */
1204 u16 next_hole; /* index of next (in 8-b blocks), 0 == none */
1205 u16 prev_hole; /* index of prev, 0 == none */
1206 u16 unused;
1207};
1208
1209static IP_t *__NetDefragment(IP_t *ip, int *lenp)
1210{
1211 static uchar pkt_buff[IP_PKTSIZE] __attribute__((aligned(PKTALIGN)));
1212 static u16 first_hole, total_len;
1213 struct hole *payload, *thisfrag, *h, *newh;
1214 IP_t *localip = (IP_t *)pkt_buff;
1215 uchar *indata = (uchar *)ip;
1216 int offset8, start, len, done = 0;
1217 u16 ip_off = ntohs(ip->ip_off);
1218
1219 /* payload starts after IP header, this fragment is in there */
1220 payload = (struct hole *)(pkt_buff + IP_HDR_SIZE_NO_UDP);
1221 offset8 = (ip_off & IP_OFFS);
1222 thisfrag = payload + offset8;
1223 start = offset8 * 8;
1224 len = ntohs(ip->ip_len) - IP_HDR_SIZE_NO_UDP;
1225
1226 if (start + len > IP_MAXUDP) /* fragment extends too far */
1227 return NULL;
1228
1229 if (!total_len || localip->ip_id != ip->ip_id) {
1230 /* new (or different) packet, reset structs */
1231 total_len = 0xffff;
1232 payload[0].last_byte = ~0;
1233 payload[0].next_hole = 0;
1234 payload[0].prev_hole = 0;
1235 first_hole = 0;
1236 /* any IP header will work, copy the first we received */
1237 memcpy(localip, ip, IP_HDR_SIZE_NO_UDP);
1238 }
1239
1240 /*
1241 * What follows is the reassembly algorithm. We use the payload
1242 * array as a linked list of hole descriptors, as each hole starts
1243 * at a multiple of 8 bytes. However, last byte can be whatever value,
1244 * so it is represented as byte count, not as 8-byte blocks.
1245 */
1246
1247 h = payload + first_hole;
1248 while (h->last_byte < start) {
1249 if (!h->next_hole) {
1250 /* no hole that far away */
1251 return NULL;
1252 }
1253 h = payload + h->next_hole;
1254 }
1255
Fillod Stephanee7ade8b2010-06-11 19:26:43 +02001256 /* last fragment may be 1..7 bytes, the "+7" forces acceptance */
1257 if (offset8 + ((len + 7) / 8) <= h - payload) {
Alessandro Rubinif119e3d2009-08-07 13:58:56 +02001258 /* no overlap with holes (dup fragment?) */
1259 return NULL;
1260 }
1261
1262 if (!(ip_off & IP_FLAGS_MFRAG)) {
1263 /* no more fragmentss: truncate this (last) hole */
1264 total_len = start + len;
1265 h->last_byte = start + len;
1266 }
1267
1268 /*
1269 * There is some overlap: fix the hole list. This code doesn't
1270 * deal with a fragment that overlaps with two different holes
1271 * (thus being a superset of a previously-received fragment).
1272 */
1273
Luca Ceresoli7cbd7482011-05-11 03:59:56 +00001274 if ((h >= thisfrag) && (h->last_byte <= start + len)) {
Alessandro Rubinif119e3d2009-08-07 13:58:56 +02001275 /* complete overlap with hole: remove hole */
1276 if (!h->prev_hole && !h->next_hole) {
1277 /* last remaining hole */
1278 done = 1;
1279 } else if (!h->prev_hole) {
1280 /* first hole */
1281 first_hole = h->next_hole;
1282 payload[h->next_hole].prev_hole = 0;
1283 } else if (!h->next_hole) {
1284 /* last hole */
1285 payload[h->prev_hole].next_hole = 0;
1286 } else {
1287 /* in the middle of the list */
1288 payload[h->next_hole].prev_hole = h->prev_hole;
1289 payload[h->prev_hole].next_hole = h->next_hole;
1290 }
1291
1292 } else if (h->last_byte <= start + len) {
1293 /* overlaps with final part of the hole: shorten this hole */
1294 h->last_byte = start;
1295
1296 } else if (h >= thisfrag) {
1297 /* overlaps with initial part of the hole: move this hole */
1298 newh = thisfrag + (len / 8);
1299 *newh = *h;
1300 h = newh;
1301 if (h->next_hole)
1302 payload[h->next_hole].prev_hole = (h - payload);
1303 if (h->prev_hole)
1304 payload[h->prev_hole].next_hole = (h - payload);
1305 else
1306 first_hole = (h - payload);
1307
1308 } else {
1309 /* fragment sits in the middle: split the hole */
1310 newh = thisfrag + (len / 8);
1311 *newh = *h;
1312 h->last_byte = start;
1313 h->next_hole = (newh - payload);
1314 newh->prev_hole = (h - payload);
1315 if (newh->next_hole)
1316 payload[newh->next_hole].prev_hole = (newh - payload);
1317 }
1318
1319 /* finally copy this fragment and possibly return whole packet */
1320 memcpy((uchar *)thisfrag, indata + IP_HDR_SIZE_NO_UDP, len);
1321 if (!done)
1322 return NULL;
1323
1324 localip->ip_len = htons(total_len);
1325 *lenp = total_len + IP_HDR_SIZE_NO_UDP;
1326 return localip;
1327}
1328
1329static inline IP_t *NetDefragment(IP_t *ip, int *lenp)
1330{
1331 u16 ip_off = ntohs(ip->ip_off);
1332 if (!(ip_off & (IP_OFFS | IP_FLAGS_MFRAG)))
1333 return ip; /* not a fragment */
1334 return __NetDefragment(ip, lenp);
1335}
1336
1337#else /* !CONFIG_IP_DEFRAG */
1338
1339static inline IP_t *NetDefragment(IP_t *ip, int *lenp)
1340{
1341 u16 ip_off = ntohs(ip->ip_off);
1342 if (!(ip_off & (IP_OFFS | IP_FLAGS_MFRAG)))
1343 return ip; /* not a fragment */
1344 return NULL;
1345}
1346#endif
wdenk2d966952002-10-31 22:12:35 +00001347
Simon Glass43c72962011-10-24 18:00:00 +00001348/**
1349 * Receive an ICMP packet. We deal with REDIRECT and PING here, and silently
1350 * drop others.
1351 *
1352 * @parma ip IP packet containing the ICMP
1353 */
1354static void receive_icmp(IP_t *ip, int len, IPaddr_t src_ip, Ethernet_t *et)
1355{
1356 ICMP_t *icmph = (ICMP_t *)&ip->udp_src;
1357
1358 switch (icmph->type) {
1359 case ICMP_REDIRECT:
1360 if (icmph->code != ICMP_REDIR_HOST)
1361 return;
1362 printf(" ICMP Host Redirect to %pI4 ",
1363 &icmph->un.gateway);
1364 break;
1365#if defined(CONFIG_CMD_PING)
1366 case ICMP_ECHO_REPLY:
1367 /*
1368 * IP header OK. Pass the packet to the
1369 * current handler.
1370 */
1371 /*
1372 * XXX point to ip packet - should this use
1373 * packet_icmp_handler?
1374 */
1375 (*packetHandler)((uchar *)ip, 0, src_ip, 0, 0);
1376 break;
1377 case ICMP_ECHO_REQUEST:
1378 debug("Got ICMP ECHO REQUEST, return %d bytes\n",
1379 ETHER_HDR_SIZE + len);
1380
1381 memcpy(&et->et_dest[0], &et->et_src[0], 6);
1382 memcpy(&et->et_src[0], NetOurEther, 6);
1383
1384 ip->ip_sum = 0;
1385 ip->ip_off = 0;
1386 NetCopyIP((void *)&ip->ip_dst, &ip->ip_src);
1387 NetCopyIP((void *)&ip->ip_src, &NetOurIP);
1388 ip->ip_sum = ~NetCksum((uchar *)ip,
1389 IP_HDR_SIZE_NO_UDP >> 1);
1390
1391 icmph->type = ICMP_ECHO_REPLY;
1392 icmph->checksum = 0;
1393 icmph->checksum = ~NetCksum((uchar *)icmph,
1394 (len - IP_HDR_SIZE_NO_UDP) >> 1);
1395 (void) eth_send((uchar *)et,
1396 ETHER_HDR_SIZE + len);
1397 break;
1398#endif
1399 default:
Simon Glass230467c2011-10-24 18:00:01 +00001400 if (packet_icmp_handler)
1401 packet_icmp_handler(icmph->type, icmph->code,
1402 ntohs(ip->udp_dst), src_ip, ntohs(ip->udp_src),
1403 icmph->un.data, ntohs(ip->udp_len));
Simon Glass43c72962011-10-24 18:00:00 +00001404 break;
1405 }
1406}
1407
wdenk2d966952002-10-31 22:12:35 +00001408void
Luca Ceresolif27d91f2011-05-04 02:40:44 +00001409NetReceive(volatile uchar *inpkt, int len)
wdenk2d966952002-10-31 22:12:35 +00001410{
1411 Ethernet_t *et;
1412 IP_t *ip;
1413 ARP_t *arp;
1414 IPaddr_t tmp;
Luca Ceresoli428ab362011-04-18 06:19:50 +00001415 IPaddr_t src_ip;
wdenk2d966952002-10-31 22:12:35 +00001416 int x;
wdenk145d2c12004-04-15 21:48:45 +00001417 uchar *pkt;
Jon Loeliger54f35c22007-07-09 17:45:14 -05001418#if defined(CONFIG_CMD_CDP)
wdenk145d2c12004-04-15 21:48:45 +00001419 int iscdp;
1420#endif
1421 ushort cti = 0, vlanid = VLAN_NONE, myvlanid, mynvlanid;
wdenk2d966952002-10-31 22:12:35 +00001422
Robin Getz9e0a4d62009-07-23 03:01:03 -04001423 debug("packet received\n");
wdenk145d2c12004-04-15 21:48:45 +00001424
Mike Frysingerdc166032009-07-18 21:04:08 -04001425 NetRxPacket = inpkt;
1426 NetRxPacketLen = len;
wdenk145d2c12004-04-15 21:48:45 +00001427 et = (Ethernet_t *)inpkt;
1428
1429 /* too small packet? */
1430 if (len < ETHER_HDR_SIZE)
1431 return;
Rafal Jaworowski1f2c9a42007-12-27 18:19:02 +01001432
1433#ifdef CONFIG_API
1434 if (push_packet) {
1435 (*push_packet)(inpkt, len);
1436 return;
1437 }
1438#endif
wdenk145d2c12004-04-15 21:48:45 +00001439
Jon Loeliger54f35c22007-07-09 17:45:14 -05001440#if defined(CONFIG_CMD_CDP)
wdenk145d2c12004-04-15 21:48:45 +00001441 /* keep track if packet is CDP */
1442 iscdp = memcmp(et->et_dest, NetCDPAddr, 6) == 0;
1443#endif
1444
1445 myvlanid = ntohs(NetOurVLAN);
1446 if (myvlanid == (ushort)-1)
1447 myvlanid = VLAN_NONE;
1448 mynvlanid = ntohs(NetOurNativeVLAN);
1449 if (mynvlanid == (ushort)-1)
1450 mynvlanid = VLAN_NONE;
wdenk2d966952002-10-31 22:12:35 +00001451
1452 x = ntohs(et->et_protlen);
1453
Robin Getz9e0a4d62009-07-23 03:01:03 -04001454 debug("packet received\n");
wdenk145d2c12004-04-15 21:48:45 +00001455
wdenk2d966952002-10-31 22:12:35 +00001456 if (x < 1514) {
1457 /*
1458 * Got a 802 packet. Check the other protocol field.
1459 */
1460 x = ntohs(et->et_prot);
wdenk145d2c12004-04-15 21:48:45 +00001461
1462 ip = (IP_t *)(inpkt + E802_HDR_SIZE);
wdenk2d966952002-10-31 22:12:35 +00001463 len -= E802_HDR_SIZE;
wdenk145d2c12004-04-15 21:48:45 +00001464
1465 } else if (x != PROT_VLAN) { /* normal packet */
1466 ip = (IP_t *)(inpkt + ETHER_HDR_SIZE);
wdenk2d966952002-10-31 22:12:35 +00001467 len -= ETHER_HDR_SIZE;
wdenk145d2c12004-04-15 21:48:45 +00001468
1469 } else { /* VLAN packet */
1470 VLAN_Ethernet_t *vet = (VLAN_Ethernet_t *)et;
1471
Robin Getz9e0a4d62009-07-23 03:01:03 -04001472 debug("VLAN packet received\n");
1473
wdenk145d2c12004-04-15 21:48:45 +00001474 /* too small packet? */
1475 if (len < VLAN_ETHER_HDR_SIZE)
1476 return;
1477
1478 /* if no VLAN active */
1479 if ((ntohs(NetOurVLAN) & VLAN_IDMASK) == VLAN_NONE
Jon Loeliger54f35c22007-07-09 17:45:14 -05001480#if defined(CONFIG_CMD_CDP)
wdenk145d2c12004-04-15 21:48:45 +00001481 && iscdp == 0
1482#endif
1483 )
1484 return;
1485
1486 cti = ntohs(vet->vet_tag);
1487 vlanid = cti & VLAN_IDMASK;
1488 x = ntohs(vet->vet_type);
1489
1490 ip = (IP_t *)(inpkt + VLAN_ETHER_HDR_SIZE);
1491 len -= VLAN_ETHER_HDR_SIZE;
wdenk2d966952002-10-31 22:12:35 +00001492 }
1493
Robin Getz9e0a4d62009-07-23 03:01:03 -04001494 debug("Receive from protocol 0x%x\n", x);
wdenk2d966952002-10-31 22:12:35 +00001495
Jon Loeliger54f35c22007-07-09 17:45:14 -05001496#if defined(CONFIG_CMD_CDP)
wdenk145d2c12004-04-15 21:48:45 +00001497 if (iscdp) {
1498 CDPHandler((uchar *)ip, len);
1499 return;
1500 }
1501#endif
1502
1503 if ((myvlanid & VLAN_IDMASK) != VLAN_NONE) {
1504 if (vlanid == VLAN_NONE)
1505 vlanid = (mynvlanid & VLAN_IDMASK);
1506 /* not matched? */
1507 if (vlanid != (myvlanid & VLAN_IDMASK))
1508 return;
1509 }
1510
wdenk2d966952002-10-31 22:12:35 +00001511 switch (x) {
1512
1513 case PROT_ARP:
1514 /*
1515 * We have to deal with two types of ARP packets:
wdenk57b2d802003-06-27 21:31:46 +00001516 * - REQUEST packets will be answered by sending our
1517 * IP address - if we know it.
1518 * - REPLY packates are expected only after we asked
1519 * for the TFTP server's or the gateway's ethernet
1520 * address; so if we receive such a packet, we set
1521 * the server ethernet address
wdenk2d966952002-10-31 22:12:35 +00001522 */
Robin Getz9e0a4d62009-07-23 03:01:03 -04001523 debug("Got ARP\n");
1524
wdenk2d966952002-10-31 22:12:35 +00001525 arp = (ARP_t *)ip;
1526 if (len < ARP_HDR_SIZE) {
1527 printf("bad length %d < %d\n", len, ARP_HDR_SIZE);
1528 return;
1529 }
Luca Ceresolie8ccbb02011-05-04 02:40:43 +00001530 if (ntohs(arp->ar_hrd) != ARP_ETHER)
wdenk2d966952002-10-31 22:12:35 +00001531 return;
Luca Ceresolie8ccbb02011-05-04 02:40:43 +00001532 if (ntohs(arp->ar_pro) != PROT_IP)
wdenk2d966952002-10-31 22:12:35 +00001533 return;
Luca Ceresolie8ccbb02011-05-04 02:40:43 +00001534 if (arp->ar_hln != 6)
wdenk2d966952002-10-31 22:12:35 +00001535 return;
Luca Ceresolie8ccbb02011-05-04 02:40:43 +00001536 if (arp->ar_pln != 4)
wdenk2d966952002-10-31 22:12:35 +00001537 return;
wdenk2d966952002-10-31 22:12:35 +00001538
Luca Ceresolie8ccbb02011-05-04 02:40:43 +00001539 if (NetOurIP == 0)
wdenk2d966952002-10-31 22:12:35 +00001540 return;
wdenk2d966952002-10-31 22:12:35 +00001541
Luca Ceresolie8ccbb02011-05-04 02:40:43 +00001542 if (NetReadIP(&arp->ar_data[16]) != NetOurIP)
wdenk2d966952002-10-31 22:12:35 +00001543 return;
wdenk2d966952002-10-31 22:12:35 +00001544
1545 switch (ntohs(arp->ar_op)) {
Luca Ceresoli5c83a962011-05-11 03:59:54 +00001546 case ARPOP_REQUEST:
1547 /* reply with our IP address */
Robin Getz9e0a4d62009-07-23 03:01:03 -04001548 debug("Got ARP REQUEST, return our IP\n");
wdenk145d2c12004-04-15 21:48:45 +00001549 pkt = (uchar *)et;
1550 pkt += NetSetEther(pkt, et->et_src, PROT_ARP);
wdenk2d966952002-10-31 22:12:35 +00001551 arp->ar_op = htons(ARPOP_REPLY);
Luca Ceresoli7cbd7482011-05-11 03:59:56 +00001552 memcpy(&arp->ar_data[10], &arp->ar_data[0], 6);
wdenk2d966952002-10-31 22:12:35 +00001553 NetCopyIP(&arp->ar_data[16], &arp->ar_data[6]);
Luca Ceresoli7cbd7482011-05-11 03:59:56 +00001554 memcpy(&arp->ar_data[0], NetOurEther, 6);
1555 NetCopyIP(&arp->ar_data[6], &NetOurIP);
Luca Ceresoli5c83a962011-05-11 03:59:54 +00001556 (void) eth_send((uchar *)et,
1557 (pkt - (uchar *)et) + ARP_HDR_SIZE);
wdenk2d966952002-10-31 22:12:35 +00001558 return;
wdenke6466f62003-06-05 19:27:42 +00001559
1560 case ARPOP_REPLY: /* arp reply */
1561 /* are we waiting for a reply */
1562 if (!NetArpWaitPacketIP || !NetArpWaitPacketMAC)
1563 break;
Robin Getz470a6d42009-07-21 12:15:28 -04001564
1565#ifdef CONFIG_KEEP_SERVERADDR
1566 if (NetServerIP == NetArpWaitPacketIP) {
1567 char buf[20];
1568 sprintf(buf, "%pM", arp->ar_data);
1569 setenv("serveraddr", buf);
1570 }
1571#endif
1572
Robin Getz9e0a4d62009-07-23 03:01:03 -04001573 debug("Got ARP REPLY, set server/gtwy eth addr (%pM)\n",
Mike Frysinger79ca1b92009-02-11 18:23:48 -05001574 arp->ar_data);
wdenke6466f62003-06-05 19:27:42 +00001575
1576 tmp = NetReadIP(&arp->ar_data[6]);
1577
1578 /* matched waiting packet's address */
1579 if (tmp == NetArpWaitReplyIP) {
Robin Getz9e0a4d62009-07-23 03:01:03 -04001580 debug("Got it\n");
wdenke6466f62003-06-05 19:27:42 +00001581 /* save address for later use */
Luca Ceresoli5c83a962011-05-11 03:59:54 +00001582 memcpy(NetArpWaitPacketMAC,
1583 &arp->ar_data[0], 6);
wdenke6466f62003-06-05 19:27:42 +00001584
wdenkb8fb6192004-08-02 21:11:11 +00001585#ifdef CONFIG_NETCONSOLE
Luca Ceresoli428ab362011-04-18 06:19:50 +00001586 (*packetHandler)(0, 0, 0, 0, 0);
wdenkb8fb6192004-08-02 21:11:11 +00001587#endif
wdenke6466f62003-06-05 19:27:42 +00001588 /* modify header, and transmit it */
1589 memcpy(((Ethernet_t *)NetArpWaitTxPacket)->et_dest, NetArpWaitPacketMAC, 6);
Luca Ceresoli5c83a962011-05-11 03:59:54 +00001590 (void) eth_send(NetArpWaitTxPacket,
1591 NetArpWaitTxPacketSize);
wdenke6466f62003-06-05 19:27:42 +00001592
1593 /* no arp request pending now */
1594 NetArpWaitPacketIP = 0;
1595 NetArpWaitTxPacketSize = 0;
1596 NetArpWaitPacketMAC = NULL;
1597
1598 }
wdenk2d966952002-10-31 22:12:35 +00001599 return;
1600 default:
Luca Ceresoli5c83a962011-05-11 03:59:54 +00001601 debug("Unexpected ARP opcode 0x%x\n",
1602 ntohs(arp->ar_op));
wdenk2d966952002-10-31 22:12:35 +00001603 return;
1604 }
wdenkcb99da52005-01-12 00:15:14 +00001605 break;
wdenk2d966952002-10-31 22:12:35 +00001606
Peter Tyserf7a48ca2010-09-30 11:25:48 -05001607#ifdef CONFIG_CMD_RARP
wdenk2d966952002-10-31 22:12:35 +00001608 case PROT_RARP:
Robin Getz9e0a4d62009-07-23 03:01:03 -04001609 debug("Got RARP\n");
wdenk2d966952002-10-31 22:12:35 +00001610 arp = (ARP_t *)ip;
1611 if (len < ARP_HDR_SIZE) {
1612 printf("bad length %d < %d\n", len, ARP_HDR_SIZE);
1613 return;
1614 }
1615
1616 if ((ntohs(arp->ar_op) != RARPOP_REPLY) ||
1617 (ntohs(arp->ar_hrd) != ARP_ETHER) ||
1618 (ntohs(arp->ar_pro) != PROT_IP) ||
1619 (arp->ar_hln != 6) || (arp->ar_pln != 4)) {
1620
Luca Ceresoli7cbd7482011-05-11 03:59:56 +00001621 puts("invalid RARP header\n");
wdenk2d966952002-10-31 22:12:35 +00001622 } else {
Luca Ceresoli7cbd7482011-05-11 03:59:56 +00001623 NetCopyIP(&NetOurIP, &arp->ar_data[16]);
wdenk4989f872004-03-14 15:06:13 +00001624 if (NetServerIP == 0)
Luca Ceresoli7cbd7482011-05-11 03:59:56 +00001625 NetCopyIP(&NetServerIP, &arp->ar_data[6]);
1626 memcpy(NetServerEther, &arp->ar_data[0], 6);
wdenk2d966952002-10-31 22:12:35 +00001627
Luca Ceresoli428ab362011-04-18 06:19:50 +00001628 (*packetHandler)(0, 0, 0, 0, 0);
wdenk2d966952002-10-31 22:12:35 +00001629 }
1630 break;
Peter Tyserf7a48ca2010-09-30 11:25:48 -05001631#endif
wdenk2d966952002-10-31 22:12:35 +00001632 case PROT_IP:
Robin Getz9e0a4d62009-07-23 03:01:03 -04001633 debug("Got IP\n");
Alessandro Rubinif119e3d2009-08-07 13:58:56 +02001634 /* Before we start poking the header, make sure it is there */
wdenk2d966952002-10-31 22:12:35 +00001635 if (len < IP_HDR_SIZE) {
Robin Getz9e0a4d62009-07-23 03:01:03 -04001636 debug("len bad %d < %lu\n", len, (ulong)IP_HDR_SIZE);
wdenk2d966952002-10-31 22:12:35 +00001637 return;
1638 }
Alessandro Rubinif119e3d2009-08-07 13:58:56 +02001639 /* Check the packet length */
wdenk2d966952002-10-31 22:12:35 +00001640 if (len < ntohs(ip->ip_len)) {
1641 printf("len bad %d < %d\n", len, ntohs(ip->ip_len));
1642 return;
1643 }
1644 len = ntohs(ip->ip_len);
Robin Getz9e0a4d62009-07-23 03:01:03 -04001645 debug("len=%d, v=%02x\n", len, ip->ip_hl_v & 0xff);
1646
Alessandro Rubinif119e3d2009-08-07 13:58:56 +02001647 /* Can't deal with anything except IPv4 */
Luca Ceresolie8ccbb02011-05-04 02:40:43 +00001648 if ((ip->ip_hl_v & 0xf0) != 0x40)
wdenk2d966952002-10-31 22:12:35 +00001649 return;
Alessandro Rubinif119e3d2009-08-07 13:58:56 +02001650 /* Can't deal with IP options (headers != 20 bytes) */
Luca Ceresolie8ccbb02011-05-04 02:40:43 +00001651 if ((ip->ip_hl_v & 0x0f) > 0x05)
Remy Bohmerb9535782008-06-03 15:48:17 +02001652 return;
Alessandro Rubinif119e3d2009-08-07 13:58:56 +02001653 /* Check the Checksum of the header */
wdenk2d966952002-10-31 22:12:35 +00001654 if (!NetCksumOk((uchar *)ip, IP_HDR_SIZE_NO_UDP / 2)) {
Luca Ceresoli7cbd7482011-05-11 03:59:56 +00001655 puts("checksum bad\n");
wdenk2d966952002-10-31 22:12:35 +00001656 return;
1657 }
Alessandro Rubinif119e3d2009-08-07 13:58:56 +02001658 /* If it is not for us, ignore it */
wdenk2d966952002-10-31 22:12:35 +00001659 tmp = NetReadIP(&ip->ip_dst);
1660 if (NetOurIP && tmp != NetOurIP && tmp != 0xFFFFFFFF) {
David Updegraff7280da72007-06-11 10:41:07 -05001661#ifdef CONFIG_MCAST_TFTP
Wolfgang Denk627f5c32007-08-14 09:47:27 +02001662 if (Mcast_addr != tmp)
David Updegraff7280da72007-06-11 10:41:07 -05001663#endif
Luca Ceresolib19d0b72011-05-04 02:40:46 +00001664 return;
wdenk2d966952002-10-31 22:12:35 +00001665 }
Luca Ceresoli428ab362011-04-18 06:19:50 +00001666 /* Read source IP address for later use */
1667 src_ip = NetReadIP(&ip->ip_src);
wdenk2d966952002-10-31 22:12:35 +00001668 /*
Alessandro Rubinif119e3d2009-08-07 13:58:56 +02001669 * The function returns the unchanged packet if it's not
1670 * a fragment, and either the complete packet or NULL if
1671 * it is a fragment (if !CONFIG_IP_DEFRAG, it returns NULL)
1672 */
Luca Ceresolidb210822011-05-04 02:40:47 +00001673 ip = NetDefragment(ip, &len);
1674 if (!ip)
Alessandro Rubinif119e3d2009-08-07 13:58:56 +02001675 return;
1676 /*
wdenk2d966952002-10-31 22:12:35 +00001677 * watch for ICMP host redirects
1678 *
wdenk57b2d802003-06-27 21:31:46 +00001679 * There is no real handler code (yet). We just watch
1680 * for ICMP host redirect messages. In case anybody
1681 * sees these messages: please contact me
1682 * (wd@denx.de), or - even better - send me the
1683 * necessary fixes :-)
wdenk2d966952002-10-31 22:12:35 +00001684 *
wdenk57b2d802003-06-27 21:31:46 +00001685 * Note: in all cases where I have seen this so far
1686 * it was a problem with the router configuration,
1687 * for instance when a router was configured in the
1688 * BOOTP reply, but the TFTP server was on the same
1689 * subnet. So this is probably a warning that your
1690 * configuration might be wrong. But I'm not really
1691 * sure if there aren't any other situations.
Simon Glass230467c2011-10-24 18:00:01 +00001692 *
1693 * Simon Glass <sjg@chromium.org>: We get an ICMP when
1694 * we send a tftp packet to a dead connection, or when
1695 * there is no server at the other end.
wdenk2d966952002-10-31 22:12:35 +00001696 */
1697 if (ip->ip_p == IPPROTO_ICMP) {
Simon Glass43c72962011-10-24 18:00:00 +00001698 receive_icmp(ip, len, src_ip, et);
1699 return;
wdenk2d966952002-10-31 22:12:35 +00001700 } else if (ip->ip_p != IPPROTO_UDP) { /* Only UDP packets */
1701 return;
1702 }
1703
Stefan Roesedfced812005-08-12 20:06:52 +02001704#ifdef CONFIG_UDP_CHECKSUM
1705 if (ip->udp_xsum != 0) {
Wolfgang Denk30b87322005-08-12 23:43:12 +02001706 ulong xsum;
Stefan Roesedfced812005-08-12 20:06:52 +02001707 ushort *sumptr;
1708 ushort sumlen;
1709
1710 xsum = ip->ip_p;
1711 xsum += (ntohs(ip->udp_len));
1712 xsum += (ntohl(ip->ip_src) >> 16) & 0x0000ffff;
1713 xsum += (ntohl(ip->ip_src) >> 0) & 0x0000ffff;
1714 xsum += (ntohl(ip->ip_dst) >> 16) & 0x0000ffff;
1715 xsum += (ntohl(ip->ip_dst) >> 0) & 0x0000ffff;
1716
1717 sumlen = ntohs(ip->udp_len);
1718 sumptr = (ushort *) &(ip->udp_src);
1719
1720 while (sumlen > 1) {
Wolfgang Denk30b87322005-08-12 23:43:12 +02001721 ushort sumdata;
Stefan Roesedfced812005-08-12 20:06:52 +02001722
1723 sumdata = *sumptr++;
1724 xsum += ntohs(sumdata);
1725 sumlen -= 2;
1726 }
1727 if (sumlen > 0) {
Wolfgang Denk30b87322005-08-12 23:43:12 +02001728 ushort sumdata;
Stefan Roesedfced812005-08-12 20:06:52 +02001729
1730 sumdata = *(unsigned char *) sumptr;
Wolfgang Denk30b87322005-08-12 23:43:12 +02001731 sumdata = (sumdata << 8) & 0xff00;
Stefan Roesedfced812005-08-12 20:06:52 +02001732 xsum += sumdata;
1733 }
1734 while ((xsum >> 16) != 0) {
Luca Ceresoli5c83a962011-05-11 03:59:54 +00001735 xsum = (xsum & 0x0000ffff) +
1736 ((xsum >> 16) & 0x0000ffff);
Stefan Roesedfced812005-08-12 20:06:52 +02001737 }
1738 if ((xsum != 0x00000000) && (xsum != 0x0000ffff)) {
Wolfgang Denk12cec0a2008-07-11 01:16:00 +02001739 printf(" UDP wrong checksum %08lx %08x\n",
1740 xsum, ntohs(ip->udp_xsum));
Stefan Roesedfced812005-08-12 20:06:52 +02001741 return;
1742 }
1743 }
1744#endif
1745
David Updegraff7280da72007-06-11 10:41:07 -05001746
wdenkb8fb6192004-08-02 21:11:11 +00001747#ifdef CONFIG_NETCONSOLE
Luca Ceresoli7cbd7482011-05-11 03:59:56 +00001748 nc_input_packet((uchar *)ip + IP_HDR_SIZE,
wdenkb8fb6192004-08-02 21:11:11 +00001749 ntohs(ip->udp_dst),
1750 ntohs(ip->udp_src),
1751 ntohs(ip->udp_len) - 8);
1752#endif
wdenk2d966952002-10-31 22:12:35 +00001753 /*
1754 * IP header OK. Pass the packet to the current handler.
1755 */
Luca Ceresoli7cbd7482011-05-11 03:59:56 +00001756 (*packetHandler)((uchar *)ip + IP_HDR_SIZE,
wdenk2d966952002-10-31 22:12:35 +00001757 ntohs(ip->udp_dst),
Luca Ceresoli428ab362011-04-18 06:19:50 +00001758 src_ip,
wdenk2d966952002-10-31 22:12:35 +00001759 ntohs(ip->udp_src),
1760 ntohs(ip->udp_len) - 8);
wdenk2d966952002-10-31 22:12:35 +00001761 break;
1762 }
1763}
1764
1765
1766/**********************************************************************/
1767
Simon Glassd6c5f552011-10-24 18:00:02 +00001768static int net_check_prereq(enum proto_t protocol)
wdenk2d966952002-10-31 22:12:35 +00001769{
1770 switch (protocol) {
wdenk05939202004-04-18 17:39:38 +00001771 /* Fall through */
Jon Loeliger54f35c22007-07-09 17:45:14 -05001772#if defined(CONFIG_CMD_PING)
wdenke6466f62003-06-05 19:27:42 +00001773 case PING:
wdenk05939202004-04-18 17:39:38 +00001774 if (NetPingIP == 0) {
Luca Ceresoli7cbd7482011-05-11 03:59:56 +00001775 puts("*** ERROR: ping address not given\n");
Luca Ceresoli5fe6de22011-05-04 02:40:45 +00001776 return 1;
wdenk05939202004-04-18 17:39:38 +00001777 }
1778 goto common;
wdenke6466f62003-06-05 19:27:42 +00001779#endif
Jon Loeliger54f35c22007-07-09 17:45:14 -05001780#if defined(CONFIG_CMD_SNTP)
wdenkb4ad9622005-04-01 00:25:43 +00001781 case SNTP:
1782 if (NetNtpServerIP == 0) {
Luca Ceresoli7cbd7482011-05-11 03:59:56 +00001783 puts("*** ERROR: NTP server address not given\n");
Luca Ceresoli5fe6de22011-05-04 02:40:45 +00001784 return 1;
wdenkb4ad9622005-04-01 00:25:43 +00001785 }
1786 goto common;
1787#endif
Robin Getz82f0d232009-07-20 14:53:54 -04001788#if defined(CONFIG_CMD_DNS)
1789 case DNS:
1790 if (NetOurDNSIP == 0) {
1791 puts("*** ERROR: DNS server address not given\n");
1792 return 1;
1793 }
1794 goto common;
1795#endif
Jon Loeliger54f35c22007-07-09 17:45:14 -05001796#if defined(CONFIG_CMD_NFS)
wdenkbe9c1cb2004-02-24 02:00:03 +00001797 case NFS:
1798#endif
Simon Glassd6c5f552011-10-24 18:00:02 +00001799 case TFTPGET:
Simon Glass6a398d22011-10-24 18:00:07 +00001800 case TFTPPUT:
wdenk05939202004-04-18 17:39:38 +00001801 if (NetServerIP == 0) {
Luca Ceresoli7cbd7482011-05-11 03:59:56 +00001802 puts("*** ERROR: `serverip' not set\n");
Luca Ceresoli5fe6de22011-05-04 02:40:45 +00001803 return 1;
wdenk05939202004-04-18 17:39:38 +00001804 }
Luca Ceresoli7cbd7482011-05-11 03:59:56 +00001805#if defined(CONFIG_CMD_PING) || defined(CONFIG_CMD_SNTP) || \
1806 defined(CONFIG_CMD_DNS)
1807common:
wdenke6466f62003-06-05 19:27:42 +00001808#endif
Simon Guinot00dceba2011-05-01 23:38:40 +00001809 /* Fall through */
wdenke6466f62003-06-05 19:27:42 +00001810
Simon Guinot00dceba2011-05-01 23:38:40 +00001811 case NETCONS:
Luca Ceresoli7aa81a42011-05-17 00:03:40 +00001812 case TFTPSRV:
wdenk05939202004-04-18 17:39:38 +00001813 if (NetOurIP == 0) {
Luca Ceresoli7cbd7482011-05-11 03:59:56 +00001814 puts("*** ERROR: `ipaddr' not set\n");
Luca Ceresoli5fe6de22011-05-04 02:40:45 +00001815 return 1;
wdenk05939202004-04-18 17:39:38 +00001816 }
1817 /* Fall through */
wdenk2d966952002-10-31 22:12:35 +00001818
Peter Tyserf7a48ca2010-09-30 11:25:48 -05001819#ifdef CONFIG_CMD_RARP
wdenk2d966952002-10-31 22:12:35 +00001820 case RARP:
Peter Tyserf7a48ca2010-09-30 11:25:48 -05001821#endif
wdenk2d966952002-10-31 22:12:35 +00001822 case BOOTP:
wdenk145d2c12004-04-15 21:48:45 +00001823 case CDP:
Peter Tyserf7a48ca2010-09-30 11:25:48 -05001824 case DHCP:
Luca Ceresoli7cbd7482011-05-11 03:59:56 +00001825 if (memcmp(NetOurEther, "\0\0\0\0\0\0", 6) == 0) {
Luca Ceresoli7cbd7482011-05-11 03:59:56 +00001826 extern int eth_get_dev_index(void);
1827 int num = eth_get_dev_index();
wdenk2d966952002-10-31 22:12:35 +00001828
wdenk05939202004-04-18 17:39:38 +00001829 switch (num) {
1830 case -1:
Luca Ceresoli7cbd7482011-05-11 03:59:56 +00001831 puts("*** ERROR: No ethernet found.\n");
Luca Ceresoli5fe6de22011-05-04 02:40:45 +00001832 return 1;
wdenk05939202004-04-18 17:39:38 +00001833 case 0:
Luca Ceresoli7cbd7482011-05-11 03:59:56 +00001834 puts("*** ERROR: `ethaddr' not set\n");
wdenk2d966952002-10-31 22:12:35 +00001835 break;
wdenk05939202004-04-18 17:39:38 +00001836 default:
Luca Ceresoli7cbd7482011-05-11 03:59:56 +00001837 printf("*** ERROR: `eth%daddr' not set\n",
wdenk2d966952002-10-31 22:12:35 +00001838 num);
1839 break;
wdenk05939202004-04-18 17:39:38 +00001840 }
wdenk2d966952002-10-31 22:12:35 +00001841
Luca Ceresoli7cbd7482011-05-11 03:59:56 +00001842 NetStartAgain();
Luca Ceresoli5fe6de22011-05-04 02:40:45 +00001843 return 2;
wdenk05939202004-04-18 17:39:38 +00001844 }
1845 /* Fall through */
1846 default:
Luca Ceresoli5fe6de22011-05-04 02:40:45 +00001847 return 0;
wdenk2d966952002-10-31 22:12:35 +00001848 }
Luca Ceresoli5fe6de22011-05-04 02:40:45 +00001849 return 0; /* OK */
wdenk2d966952002-10-31 22:12:35 +00001850}
1851/**********************************************************************/
1852
1853int
Luca Ceresolif27d91f2011-05-04 02:40:44 +00001854NetCksumOk(uchar *ptr, int len)
wdenk2d966952002-10-31 22:12:35 +00001855{
1856 return !((NetCksum(ptr, len) + 1) & 0xfffe);
1857}
1858
1859
1860unsigned
Luca Ceresolif27d91f2011-05-04 02:40:44 +00001861NetCksum(uchar *ptr, int len)
wdenk2d966952002-10-31 22:12:35 +00001862{
1863 ulong xsum;
Stefan Roeseac553282005-08-31 12:55:50 +02001864 ushort *p = (ushort *)ptr;
wdenk2d966952002-10-31 22:12:35 +00001865
1866 xsum = 0;
1867 while (len-- > 0)
Wolfgang Denk3135c542005-08-26 01:36:03 +02001868 xsum += *p++;
wdenk2d966952002-10-31 22:12:35 +00001869 xsum = (xsum & 0xffff) + (xsum >> 16);
1870 xsum = (xsum & 0xffff) + (xsum >> 16);
Luca Ceresoli5fe6de22011-05-04 02:40:45 +00001871 return xsum & 0xffff;
wdenk2d966952002-10-31 22:12:35 +00001872}
1873
wdenk145d2c12004-04-15 21:48:45 +00001874int
1875NetEthHdrSize(void)
1876{
1877 ushort myvlanid;
1878
1879 myvlanid = ntohs(NetOurVLAN);
1880 if (myvlanid == (ushort)-1)
1881 myvlanid = VLAN_NONE;
wdenk2d966952002-10-31 22:12:35 +00001882
Luca Ceresoli5c83a962011-05-11 03:59:54 +00001883 return ((myvlanid & VLAN_IDMASK) == VLAN_NONE) ? ETHER_HDR_SIZE :
1884 VLAN_ETHER_HDR_SIZE;
wdenk145d2c12004-04-15 21:48:45 +00001885}
1886
1887int
Luca Ceresolif27d91f2011-05-04 02:40:44 +00001888NetSetEther(volatile uchar *xet, uchar * addr, uint prot)
wdenk2d966952002-10-31 22:12:35 +00001889{
1890 Ethernet_t *et = (Ethernet_t *)xet;
wdenk145d2c12004-04-15 21:48:45 +00001891 ushort myvlanid;
1892
1893 myvlanid = ntohs(NetOurVLAN);
1894 if (myvlanid == (ushort)-1)
1895 myvlanid = VLAN_NONE;
wdenk2d966952002-10-31 22:12:35 +00001896
Luca Ceresoli7cbd7482011-05-11 03:59:56 +00001897 memcpy(et->et_dest, addr, 6);
1898 memcpy(et->et_src, NetOurEther, 6);
wdenk145d2c12004-04-15 21:48:45 +00001899 if ((myvlanid & VLAN_IDMASK) == VLAN_NONE) {
Luca Ceresolib19d0b72011-05-04 02:40:46 +00001900 et->et_protlen = htons(prot);
wdenk145d2c12004-04-15 21:48:45 +00001901 return ETHER_HDR_SIZE;
1902 } else {
1903 VLAN_Ethernet_t *vet = (VLAN_Ethernet_t *)xet;
wdenk2d966952002-10-31 22:12:35 +00001904
wdenk145d2c12004-04-15 21:48:45 +00001905 vet->vet_vlan_type = htons(PROT_VLAN);
1906 vet->vet_tag = htons((0 << 5) | (myvlanid & VLAN_IDMASK));
1907 vet->vet_type = htons(prot);
1908 return VLAN_ETHER_HDR_SIZE;
1909 }
1910}
wdenk2d966952002-10-31 22:12:35 +00001911
1912void
Luca Ceresolif27d91f2011-05-04 02:40:44 +00001913NetSetIP(volatile uchar *xip, IPaddr_t dest, int dport, int sport, int len)
wdenk2d966952002-10-31 22:12:35 +00001914{
Olav Morkene8a02772009-01-23 12:56:26 +01001915 IP_t *ip = (IP_t *)xip;
wdenk2d966952002-10-31 22:12:35 +00001916
1917 /*
1918 * If the data is an odd number of bytes, zero the
1919 * byte after the last byte so that the checksum
1920 * will work.
1921 */
1922 if (len & 1)
1923 xip[IP_HDR_SIZE + len] = 0;
1924
1925 /*
1926 * Construct an IP and UDP header.
wdenk05939202004-04-18 17:39:38 +00001927 * (need to set no fragment bit - XXX)
wdenk2d966952002-10-31 22:12:35 +00001928 */
Luca Ceresoli5c83a962011-05-11 03:59:54 +00001929 /* IP_HDR_SIZE / 4 (not including UDP) */
1930 ip->ip_hl_v = 0x45;
wdenk2d966952002-10-31 22:12:35 +00001931 ip->ip_tos = 0;
1932 ip->ip_len = htons(IP_HDR_SIZE + len);
1933 ip->ip_id = htons(NetIPID++);
Peter Tyser0885bf02008-12-01 16:26:20 -06001934 ip->ip_off = htons(IP_FLAGS_DFRAG); /* Don't fragment */
wdenk2d966952002-10-31 22:12:35 +00001935 ip->ip_ttl = 255;
1936 ip->ip_p = 17; /* UDP */
1937 ip->ip_sum = 0;
Luca Ceresoli5c83a962011-05-11 03:59:54 +00001938 /* already in network byte order */
Luca Ceresolif27d91f2011-05-04 02:40:44 +00001939 NetCopyIP((void *)&ip->ip_src, &NetOurIP);
Luca Ceresoli5c83a962011-05-11 03:59:54 +00001940 /* - "" - */
Luca Ceresolif27d91f2011-05-04 02:40:44 +00001941 NetCopyIP((void *)&ip->ip_dst, &dest);
wdenk2d966952002-10-31 22:12:35 +00001942 ip->udp_src = htons(sport);
1943 ip->udp_dst = htons(dport);
1944 ip->udp_len = htons(8 + len);
1945 ip->udp_xsum = 0;
1946 ip->ip_sum = ~NetCksum((uchar *)ip, IP_HDR_SIZE_NO_UDP / 2);
1947}
1948
Luca Ceresoli7cbd7482011-05-11 03:59:56 +00001949void copy_filename(char *dst, const char *src, int size)
wdenk2d966952002-10-31 22:12:35 +00001950{
1951 if (*src && (*src == '"')) {
1952 ++src;
1953 --size;
1954 }
1955
Luca Ceresolie8ccbb02011-05-04 02:40:43 +00001956 while ((--size > 0) && *src && (*src != '"'))
wdenk2d966952002-10-31 22:12:35 +00001957 *dst++ = *src++;
wdenk2d966952002-10-31 22:12:35 +00001958 *dst = '\0';
1959}
1960
Luca Ceresoli5c83a962011-05-11 03:59:54 +00001961#if defined(CONFIG_CMD_NFS) || \
1962 defined(CONFIG_CMD_SNTP) || \
1963 defined(CONFIG_CMD_DNS)
Robin Getz82f0d232009-07-20 14:53:54 -04001964/*
Robin Getz55b50e92010-03-08 14:07:00 -05001965 * make port a little random (1024-17407)
1966 * This keeps the math somewhat trivial to compute, and seems to work with
1967 * all supported protocols/clients/servers
Robin Getz82f0d232009-07-20 14:53:54 -04001968 */
1969unsigned int random_port(void)
1970{
Robin Getz55b50e92010-03-08 14:07:00 -05001971 return 1024 + (get_timer(0) % 0x4000);
Robin Getz82f0d232009-07-20 14:53:54 -04001972}
1973#endif
1974
Luca Ceresoli7cbd7482011-05-11 03:59:56 +00001975void ip_to_string(IPaddr_t x, char *s)
wdenk2d966952002-10-31 22:12:35 +00001976{
Luca Ceresoli7cbd7482011-05-11 03:59:56 +00001977 x = ntohl(x);
1978 sprintf(s, "%d.%d.%d.%d",
1979 (int) ((x >> 24) & 0xff),
1980 (int) ((x >> 16) & 0xff),
1981 (int) ((x >> 8) & 0xff), (int) ((x >> 0) & 0xff)
wdenk05939202004-04-18 17:39:38 +00001982 );
wdenk2d966952002-10-31 22:12:35 +00001983}
1984
wdenk145d2c12004-04-15 21:48:45 +00001985void VLAN_to_string(ushort x, char *s)
1986{
1987 x = ntohs(x);
1988
1989 if (x == (ushort)-1)
1990 x = VLAN_NONE;
1991
1992 if (x == VLAN_NONE)
1993 strcpy(s, "none");
1994 else
1995 sprintf(s, "%d", x & VLAN_IDMASK);
1996}
1997
Mike Frysinger89c73922010-10-20 07:16:48 -04001998ushort string_to_VLAN(const char *s)
wdenk145d2c12004-04-15 21:48:45 +00001999{
2000 ushort id;
2001
2002 if (s == NULL)
wdenk656140b2004-04-25 13:18:40 +00002003 return htons(VLAN_NONE);
wdenk145d2c12004-04-15 21:48:45 +00002004
2005 if (*s < '0' || *s > '9')
2006 id = VLAN_NONE;
2007 else
2008 id = (ushort)simple_strtoul(s, NULL, 10);
2009
wdenk656140b2004-04-25 13:18:40 +00002010 return htons(id);
wdenk145d2c12004-04-15 21:48:45 +00002011}
2012
wdenk145d2c12004-04-15 21:48:45 +00002013ushort getenv_VLAN(char *var)
2014{
Luca Ceresoli5fe6de22011-05-04 02:40:45 +00002015 return string_to_VLAN(getenv(var));
wdenk145d2c12004-04-15 21:48:45 +00002016}