blob: 83e20f9d3ef53aefbd084172963f7b78afd6c2fe [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) */
120IPaddr_t NetOurSubnetMask=0;
121/* Our gateways IP address */
122IPaddr_t NetOurGatewayIP=0;
123/* Our DNS IP address */
124IPaddr_t NetOurDNSIP=0;
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 */
127IPaddr_t NetOurDNS2IP=0;
stroesefe389a82003-08-28 14:17:32 +0000128#endif
Luca Ceresoli3e38e422011-05-11 03:59:54 +0000129/* Our NIS domain */
130char NetOurNISDomain[32]={0,};
131/* Our hostname */
132char NetOurHostName[32]={0,};
133/* Our bootpath */
134char NetOurRootPath[64]={0,};
135/* Our bootfile size in blocks */
136ushort NetBootFileSize=0;
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 */
149uchar NetServerEther[6] =
wdenk73a8b272003-06-05 19:27:42 +0000150 { 0, 0, 0, 0, 0, 0 };
Luca Ceresoli3e38e422011-05-11 03:59:54 +0000151/* Our IP addr (0 = unknown) */
152IPaddr_t NetOurIP;
153/* Server IP addr (0 = unknown) */
154IPaddr_t NetServerIP;
155/* Current receive packet */
156volatile uchar *NetRxPacket;
157/* Current rx packet length */
158int NetRxPacketLen;
159/* IP packet ID */
160unsigned NetIPID;
161/* Ethernet bcast address */
162uchar NetBcastAddr[6] =
wdenk2d966952002-10-31 22:12:35 +0000163 { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
wdenk73a8b272003-06-05 19:27:42 +0000164uchar NetEtherNullAddr[6] =
165 { 0, 0, 0, 0, 0, 0 };
Rafal Jaworowskif85b6072007-12-27 18:19:02 +0100166#ifdef CONFIG_API
167void (*push_packet)(volatile void *, int len) = 0;
168#endif
Jon Loeliger643d1ab2007-07-09 17:45:14 -0500169#if defined(CONFIG_CMD_CDP)
Luca Ceresoli3e38e422011-05-11 03:59:54 +0000170/* Ethernet bcast address */
171uchar NetCDPAddr[6] =
wdenka3d991b2004-04-15 21:48:45 +0000172 { 0x01, 0x00, 0x0c, 0xcc, 0xcc, 0xcc };
173#endif
Luca Ceresoli3e38e422011-05-11 03:59:54 +0000174/* Network loop state */
175int NetState;
wdenk2d966952002-10-31 22:12:35 +0000176#ifdef CONFIG_NET_MULTI
Luca Ceresoli3e38e422011-05-11 03:59:54 +0000177/* Tried all network devices */
178int NetRestartWrap = 0;
179/* Network loop restarted */
180static int NetRestarted = 0;
181/* At least one device configured */
182static int NetDevExists = 0;
wdenk2d966952002-10-31 22:12:35 +0000183#endif
184
wdenk6e592382004-04-18 17:39:38 +0000185/* XXX in both little & big endian machines 0xFFFF == ntohs(-1) */
Luca Ceresoli3e38e422011-05-11 03:59:54 +0000186/* default is without VLAN */
187ushort NetOurVLAN = 0xFFFF;
188/* ditto */
189ushort NetOurNativeVLAN = 0xFFFF;
wdenka3d991b2004-04-15 21:48:45 +0000190
Luca Ceresoli3e38e422011-05-11 03:59:54 +0000191/* Boot File name */
192char BootFile[128];
wdenk2d966952002-10-31 22:12:35 +0000193
Jon Loeliger643d1ab2007-07-09 17:45:14 -0500194#if defined(CONFIG_CMD_PING)
Luca Ceresoli3e38e422011-05-11 03:59:54 +0000195/* the ip address to ping */
196IPaddr_t NetPingIP;
wdenk73a8b272003-06-05 19:27:42 +0000197
198static void PingStart(void);
199#endif
200
Jon Loeliger643d1ab2007-07-09 17:45:14 -0500201#if defined(CONFIG_CMD_CDP)
wdenka3d991b2004-04-15 21:48:45 +0000202static void CDPStart(void);
203#endif
204
Jon Loeliger643d1ab2007-07-09 17:45:14 -0500205#if defined(CONFIG_CMD_SNTP)
Luca Ceresoli3e38e422011-05-11 03:59:54 +0000206/* NTP server IP address */
207IPaddr_t NetNtpServerIP;
208/* offset time from UTC */
209int NetTimeOffset=0;
wdenkea287de2005-04-01 00:25:43 +0000210#endif
211
wdenk68ceb292004-08-02 21:11:11 +0000212#ifdef CONFIG_NETCONSOLE
213void NcStart(void);
214int nc_input_packet(uchar *pkt, unsigned dest, unsigned src, unsigned len);
215#endif
216
wdenk2d966952002-10-31 22:12:35 +0000217volatile uchar PktBuf[(PKTBUFSRX+1) * PKTSIZE_ALIGN + PKTALIGN];
218
Luca Ceresoli3e38e422011-05-11 03:59:54 +0000219/* Receive packet */
220volatile uchar *NetRxPackets[PKTBUFSRX];
wdenk2d966952002-10-31 22:12:35 +0000221
Luca Ceresoli3e38e422011-05-11 03:59:54 +0000222/* Current RX packet handler */
223static rxhand_f *packetHandler;
224/* Current timeout handler */
225static thand_f *timeHandler;
226/* Time base value */
227static ulong timeStart;
228/* Current timeout value */
229static ulong timeDelta;
230/* THE transmit packet */
231volatile uchar *NetTxPacket = 0;
wdenk2d966952002-10-31 22:12:35 +0000232
233static int net_check_prereq (proto_t protocol);
234
Remy Bohmer67b96e82009-10-28 22:13:39 +0100235static int NetTryCount;
236
wdenk2d966952002-10-31 22:12:35 +0000237/**********************************************************************/
wdenk73a8b272003-06-05 19:27:42 +0000238
239IPaddr_t NetArpWaitPacketIP;
240IPaddr_t NetArpWaitReplyIP;
Luca Ceresoli3e38e422011-05-11 03:59:54 +0000241/* MAC address of waiting packet's destination */
242uchar *NetArpWaitPacketMAC;
243/* THE transmit packet */
244uchar *NetArpWaitTxPacket;
wdenk73a8b272003-06-05 19:27:42 +0000245int NetArpWaitTxPacketSize;
Wolfgang Denk53677ef2008-05-20 16:00:29 +0200246uchar NetArpWaitPacketBuf[PKTSIZE_ALIGN + PKTALIGN];
wdenk73a8b272003-06-05 19:27:42 +0000247ulong NetArpWaitTimerStart;
248int NetArpWaitTry;
249
wdenk6e592382004-04-18 17:39:38 +0000250void ArpRequest (void)
wdenk73a8b272003-06-05 19:27:42 +0000251{
252 int i;
253 volatile uchar *pkt;
wdenk6e592382004-04-18 17:39:38 +0000254 ARP_t *arp;
wdenk73a8b272003-06-05 19:27:42 +0000255
Robin Getz0ebf04c2009-07-23 03:01:03 -0400256 debug("ARP broadcast %d\n", NetArpWaitTry);
257
wdenk73a8b272003-06-05 19:27:42 +0000258 pkt = NetTxPacket;
259
wdenk6e592382004-04-18 17:39:38 +0000260 pkt += NetSetEther (pkt, NetBcastAddr, PROT_ARP);
wdenk73a8b272003-06-05 19:27:42 +0000261
wdenk6e592382004-04-18 17:39:38 +0000262 arp = (ARP_t *) pkt;
wdenk73a8b272003-06-05 19:27:42 +0000263
wdenk6e592382004-04-18 17:39:38 +0000264 arp->ar_hrd = htons (ARP_ETHER);
265 arp->ar_pro = htons (PROT_IP);
wdenk73a8b272003-06-05 19:27:42 +0000266 arp->ar_hln = 6;
267 arp->ar_pln = 4;
wdenk6e592382004-04-18 17:39:38 +0000268 arp->ar_op = htons (ARPOP_REQUEST);
wdenk73a8b272003-06-05 19:27:42 +0000269
Luca Ceresoli3e38e422011-05-11 03:59:54 +0000270 /* source ET addr */
271 memcpy (&arp->ar_data[0], NetOurEther, 6);
272 /* source IP addr */
273 NetWriteIP ((uchar *) & arp->ar_data[6], NetOurIP);
wdenk6e592382004-04-18 17:39:38 +0000274 for (i = 10; i < 16; ++i) {
Luca Ceresoli3e38e422011-05-11 03:59:54 +0000275 /* dest ET addr = 0 */
276 arp->ar_data[i] = 0;
wdenk73a8b272003-06-05 19:27:42 +0000277 }
278
wdenk6e592382004-04-18 17:39:38 +0000279 if ((NetArpWaitPacketIP & NetOurSubnetMask) !=
280 (NetOurIP & NetOurSubnetMask)) {
281 if (NetOurGatewayIP == 0) {
282 puts ("## Warning: gatewayip needed but not set\n");
Wolfgang Denkd509b812006-03-12 01:13:30 +0100283 NetArpWaitReplyIP = NetArpWaitPacketIP;
284 } else {
285 NetArpWaitReplyIP = NetOurGatewayIP;
wdenk6e592382004-04-18 17:39:38 +0000286 }
wdenk6e592382004-04-18 17:39:38 +0000287 } else {
288 NetArpWaitReplyIP = NetArpWaitPacketIP;
289 }
wdenk73a8b272003-06-05 19:27:42 +0000290
wdenk6e592382004-04-18 17:39:38 +0000291 NetWriteIP ((uchar *) & arp->ar_data[16], NetArpWaitReplyIP);
292 (void) eth_send (NetTxPacket, (pkt - NetTxPacket) + ARP_HDR_SIZE);
wdenk73a8b272003-06-05 19:27:42 +0000293}
294
295void ArpTimeoutCheck(void)
296{
297 ulong t;
298
299 if (!NetArpWaitPacketIP)
300 return;
301
302 t = get_timer(0);
303
304 /* check for arp timeout */
Bartlomiej Sieka49f3bdb2008-10-01 15:26:28 +0200305 if ((t - NetArpWaitTimerStart) > ARP_TIMEOUT) {
wdenk73a8b272003-06-05 19:27:42 +0000306 NetArpWaitTry++;
307
308 if (NetArpWaitTry >= ARP_TIMEOUT_COUNT) {
309 puts ("\nARP Retry count exceeded; starting again\n");
310 NetArpWaitTry = 0;
311 NetStartAgain();
312 } else {
313 NetArpWaitTimerStart = t;
314 ArpRequest();
315 }
316 }
317}
318
Heiko Schocherda954272009-04-28 08:36:11 +0200319static void
Heiko Schocher2f70c492009-02-10 09:38:52 +0100320NetInitLoop(proto_t protocol)
321{
Heiko Schocherda954272009-04-28 08:36:11 +0200322 static int env_changed_id = 0;
Heiko Schocher2f70c492009-02-10 09:38:52 +0100323 bd_t *bd = gd->bd;
324 int env_id = get_env_id ();
325
326 /* update only when the environment has changed */
Michael Zaidman3c172c42009-04-04 01:43:00 +0300327 if (env_changed_id != env_id) {
Heiko Schocher2f70c492009-02-10 09:38:52 +0100328 NetCopyIP(&NetOurIP, &bd->bi_ip_addr);
329 NetOurGatewayIP = getenv_IPaddr ("gatewayip");
330 NetOurSubnetMask= getenv_IPaddr ("netmask");
Heiko Schocher2f70c492009-02-10 09:38:52 +0100331 NetServerIP = getenv_IPaddr ("serverip");
Heiko Schocher2f70c492009-02-10 09:38:52 +0100332 NetOurNativeVLAN = getenv_VLAN("nvlan");
Michael Zaidman3c172c42009-04-04 01:43:00 +0300333 NetOurVLAN = getenv_VLAN("vlan");
Robin Getz1a32bf42009-07-20 14:53:54 -0400334#if defined(CONFIG_CMD_DNS)
335 NetOurDNSIP = getenv_IPaddr("dnsip");
336#endif
Michael Zaidman3c172c42009-04-04 01:43:00 +0300337 env_changed_id = env_id;
Heiko Schocher2f70c492009-02-10 09:38:52 +0100338 }
Michael Zaidman3c172c42009-04-04 01:43:00 +0300339
Heiko Schocherda954272009-04-28 08:36:11 +0200340 return;
Heiko Schocher2f70c492009-02-10 09:38:52 +0100341}
342
wdenk73a8b272003-06-05 19:27:42 +0000343/**********************************************************************/
wdenk2d966952002-10-31 22:12:35 +0000344/*
345 * Main network processing loop.
346 */
347
348int
349NetLoop(proto_t protocol)
350{
wdenk2d966952002-10-31 22:12:35 +0000351 bd_t *bd = gd->bd;
352
353#ifdef CONFIG_NET_MULTI
354 NetRestarted = 0;
355 NetDevExists = 0;
356#endif
357
wdenk73a8b272003-06-05 19:27:42 +0000358 /* XXX problem with bss workaround */
359 NetArpWaitPacketMAC = NULL;
360 NetArpWaitTxPacket = NULL;
361 NetArpWaitPacketIP = 0;
362 NetArpWaitReplyIP = 0;
363 NetArpWaitTxPacket = NULL;
364 NetTxPacket = NULL;
Remy Bohmer67b96e82009-10-28 22:13:39 +0100365 NetTryCount = 1;
wdenk73a8b272003-06-05 19:27:42 +0000366
wdenk2d966952002-10-31 22:12:35 +0000367 if (!NetTxPacket) {
368 int i;
wdenk2d966952002-10-31 22:12:35 +0000369 /*
370 * Setup packet buffers, aligned correctly.
371 */
372 NetTxPacket = &PktBuf[0] + (PKTALIGN - 1);
373 NetTxPacket -= (ulong)NetTxPacket % PKTALIGN;
374 for (i = 0; i < PKTBUFSRX; i++) {
375 NetRxPackets[i] = NetTxPacket + (i+1)*PKTSIZE_ALIGN;
376 }
wdenk73a8b272003-06-05 19:27:42 +0000377 }
378
379 if (!NetArpWaitTxPacket) {
380 NetArpWaitTxPacket = &NetArpWaitPacketBuf[0] + (PKTALIGN - 1);
381 NetArpWaitTxPacket -= (ulong)NetArpWaitTxPacket % PKTALIGN;
382 NetArpWaitTxPacketSize = 0;
wdenk2d966952002-10-31 22:12:35 +0000383 }
384
385 eth_halt();
wdenk6e592382004-04-18 17:39:38 +0000386#ifdef CONFIG_NET_MULTI
wdenka3d991b2004-04-15 21:48:45 +0000387 eth_set_current();
wdenk6e592382004-04-18 17:39:38 +0000388#endif
wdenkb1bf6f22005-04-03 14:52:59 +0000389 if (eth_init(bd) < 0) {
390 eth_halt();
wdenk6e592382004-04-18 17:39:38 +0000391 return(-1);
wdenkb1bf6f22005-04-03 14:52:59 +0000392 }
wdenk2d966952002-10-31 22:12:35 +0000393
394restart:
395#ifdef CONFIG_NET_MULTI
396 memcpy (NetOurEther, eth_get_dev()->enetaddr, 6);
397#else
Mike Frysinger95823ca2009-02-11 18:23:48 -0500398 eth_getenv_enetaddr("ethaddr", NetOurEther);
wdenk2d966952002-10-31 22:12:35 +0000399#endif
400
401 NetState = NETLOOP_CONTINUE;
402
403 /*
404 * Start the ball rolling with the given start function. From
405 * here on, this code is a state machine driven by received
406 * packets and timer events.
407 */
Heiko Schocher2f70c492009-02-10 09:38:52 +0100408 NetInitLoop(protocol);
wdenk2d966952002-10-31 22:12:35 +0000409
410 switch (net_check_prereq (protocol)) {
411 case 1:
412 /* network not configured */
wdenkb1bf6f22005-04-03 14:52:59 +0000413 eth_halt();
wdenk1d0350e2002-11-11 21:14:20 +0000414 return (-1);
wdenk2d966952002-10-31 22:12:35 +0000415
416#ifdef CONFIG_NET_MULTI
417 case 2:
418 /* network device not configured */
419 break;
420#endif /* CONFIG_NET_MULTI */
421
422 case 0:
423#ifdef CONFIG_NET_MULTI
424 NetDevExists = 1;
425#endif
426 switch (protocol) {
427 case TFTP:
428 /* always use ARP to get server ethernet address */
wdenk73a8b272003-06-05 19:27:42 +0000429 TftpStart();
wdenk2d966952002-10-31 22:12:35 +0000430 break;
431
Jon Loeliger643d1ab2007-07-09 17:45:14 -0500432#if defined(CONFIG_CMD_DHCP)
wdenk2d966952002-10-31 22:12:35 +0000433 case DHCP:
wdenkd407bf52004-10-11 22:51:13 +0000434 BootpTry = 0;
Michael Zaidman09133f82009-07-14 23:37:12 +0300435 NetOurIP = 0;
wdenk2d966952002-10-31 22:12:35 +0000436 DhcpRequest(); /* Basically same as BOOTP */
437 break;
Jon Loeliger610f2e92007-07-10 11:05:02 -0500438#endif
wdenk2d966952002-10-31 22:12:35 +0000439
440 case BOOTP:
441 BootpTry = 0;
Michael Zaidman09133f82009-07-14 23:37:12 +0300442 NetOurIP = 0;
wdenk2d966952002-10-31 22:12:35 +0000443 BootpRequest ();
444 break;
445
Peter Tyserbf6cb242010-09-30 11:25:48 -0500446#if defined(CONFIG_CMD_RARP)
wdenk2d966952002-10-31 22:12:35 +0000447 case RARP:
448 RarpTry = 0;
Michael Zaidman09133f82009-07-14 23:37:12 +0300449 NetOurIP = 0;
wdenk2d966952002-10-31 22:12:35 +0000450 RarpRequest ();
451 break;
Peter Tyserbf6cb242010-09-30 11:25:48 -0500452#endif
Jon Loeliger643d1ab2007-07-09 17:45:14 -0500453#if defined(CONFIG_CMD_PING)
wdenk73a8b272003-06-05 19:27:42 +0000454 case PING:
455 PingStart();
456 break;
457#endif
Jon Loeliger643d1ab2007-07-09 17:45:14 -0500458#if defined(CONFIG_CMD_NFS)
wdenkcbd8a352004-02-24 02:00:03 +0000459 case NFS:
460 NfsStart();
461 break;
462#endif
Jon Loeliger643d1ab2007-07-09 17:45:14 -0500463#if defined(CONFIG_CMD_CDP)
wdenka3d991b2004-04-15 21:48:45 +0000464 case CDP:
465 CDPStart();
466 break;
467#endif
wdenk68ceb292004-08-02 21:11:11 +0000468#ifdef CONFIG_NETCONSOLE
469 case NETCONS:
470 NcStart();
471 break;
472#endif
Jon Loeliger643d1ab2007-07-09 17:45:14 -0500473#if defined(CONFIG_CMD_SNTP)
wdenkea287de2005-04-01 00:25:43 +0000474 case SNTP:
475 SntpStart();
476 break;
477#endif
Robin Getz1a32bf42009-07-20 14:53:54 -0400478#if defined(CONFIG_CMD_DNS)
479 case DNS:
480 DnsStart();
481 break;
482#endif
wdenk2d966952002-10-31 22:12:35 +0000483 default:
484 break;
485 }
486
487 NetBootFileXferSize = 0;
488 break;
489 }
490
Jon Loeliger643d1ab2007-07-09 17:45:14 -0500491#if defined(CONFIG_MII) || defined(CONFIG_CMD_MII)
Luca Ceresoli3e38e422011-05-11 03:59:54 +0000492#if defined(CONFIG_SYS_FAULT_ECHO_LINK_DOWN) && \
493 defined(CONFIG_STATUS_LED) && \
494 defined(STATUS_LED_RED)
wdenkfc3e2162003-10-08 22:33:00 +0000495 /*
wdenk42d1f032003-10-15 23:53:47 +0000496 * Echo the inverted link state to the fault LED.
wdenkfc3e2162003-10-08 22:33:00 +0000497 */
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200498 if(miiphy_link(eth_get_dev()->name, CONFIG_SYS_FAULT_MII_ADDR)) {
wdenkfc3e2162003-10-08 22:33:00 +0000499 status_led_set (STATUS_LED_RED, STATUS_LED_OFF);
500 } else {
501 status_led_set (STATUS_LED_RED, STATUS_LED_ON);
502 }
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200503#endif /* CONFIG_SYS_FAULT_ECHO_LINK_DOWN, ... */
wdenkfc3e2162003-10-08 22:33:00 +0000504#endif /* CONFIG_MII, ... */
wdenk2d966952002-10-31 22:12:35 +0000505
506 /*
507 * Main packet reception loop. Loop receiving packets until
wdenk59acc292005-04-03 14:18:51 +0000508 * someone sets `NetState' to a state that terminates.
wdenk2d966952002-10-31 22:12:35 +0000509 */
510 for (;;) {
511 WATCHDOG_RESET();
512#ifdef CONFIG_SHOW_ACTIVITY
513 {
514 extern void show_activity(int arg);
515 show_activity(1);
516 }
517#endif
518 /*
519 * Check the ethernet for a new packet. The ethernet
520 * receive routine will process it.
521 */
Guennadi Liakhovetski40cb90e2008-04-03 17:04:19 +0200522 eth_rx();
wdenk2d966952002-10-31 22:12:35 +0000523
524 /*
525 * Abort if ctrl-c was pressed.
526 */
527 if (ctrlc()) {
wdenk8bde7f72003-06-27 21:31:46 +0000528 eth_halt();
wdenk4b9206e2004-03-23 22:14:11 +0000529 puts ("\nAbort\n");
wdenk1d0350e2002-11-11 21:14:20 +0000530 return (-1);
wdenk2d966952002-10-31 22:12:35 +0000531 }
532
wdenk73a8b272003-06-05 19:27:42 +0000533 ArpTimeoutCheck();
wdenk2d966952002-10-31 22:12:35 +0000534
535 /*
536 * Check for a timeout, and run the timeout handler
537 * if we have one.
538 */
wdenke0ac62d2003-08-17 18:55:18 +0000539 if (timeHandler && ((get_timer(0) - timeStart) > timeDelta)) {
wdenk2d966952002-10-31 22:12:35 +0000540 thand_f *x;
541
Jon Loeliger643d1ab2007-07-09 17:45:14 -0500542#if defined(CONFIG_MII) || defined(CONFIG_CMD_MII)
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200543# if defined(CONFIG_SYS_FAULT_ECHO_LINK_DOWN) && \
wdenk63153492005-04-03 20:55:38 +0000544 defined(CONFIG_STATUS_LED) && \
wdenk59acc292005-04-03 14:18:51 +0000545 defined(STATUS_LED_RED)
wdenkfc3e2162003-10-08 22:33:00 +0000546 /*
wdenk42d1f032003-10-15 23:53:47 +0000547 * Echo the inverted link state to the fault LED.
wdenkfc3e2162003-10-08 22:33:00 +0000548 */
Luca Ceresoli3e38e422011-05-11 03:59:54 +0000549 if(miiphy_link(eth_get_dev()->name,
550 CONFIG_SYS_FAULT_MII_ADDR)) {
wdenkfc3e2162003-10-08 22:33:00 +0000551 status_led_set (STATUS_LED_RED, STATUS_LED_OFF);
552 } else {
553 status_led_set (STATUS_LED_RED, STATUS_LED_ON);
554 }
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200555# endif /* CONFIG_SYS_FAULT_ECHO_LINK_DOWN, ... */
wdenkfc3e2162003-10-08 22:33:00 +0000556#endif /* CONFIG_MII, ... */
wdenk2d966952002-10-31 22:12:35 +0000557 x = timeHandler;
558 timeHandler = (thand_f *)0;
559 (*x)();
560 }
561
562
563 switch (NetState) {
564
565 case NETLOOP_RESTART:
566#ifdef CONFIG_NET_MULTI
567 NetRestarted = 1;
568#endif
569 goto restart;
570
571 case NETLOOP_SUCCESS:
572 if (NetBootFileXferSize > 0) {
Wolfgang Denkf34024d2007-09-12 00:48:57 +0200573 char buf[20];
wdenk2d966952002-10-31 22:12:35 +0000574 printf("Bytes transferred = %ld (%lx hex)\n",
575 NetBootFileXferSize,
576 NetBootFileXferSize);
Wolfgang Denkf34024d2007-09-12 00:48:57 +0200577 sprintf(buf, "%lX", NetBootFileXferSize);
wdenk2d966952002-10-31 22:12:35 +0000578 setenv("filesize", buf);
wdenka3d991b2004-04-15 21:48:45 +0000579
580 sprintf(buf, "%lX", (unsigned long)load_addr);
581 setenv("fileaddr", buf);
wdenk2d966952002-10-31 22:12:35 +0000582 }
583 eth_halt();
584 return NetBootFileXferSize;
585
586 case NETLOOP_FAIL:
wdenk1d0350e2002-11-11 21:14:20 +0000587 return (-1);
wdenk2d966952002-10-31 22:12:35 +0000588 }
589 }
590}
591
592/**********************************************************************/
593
594static void
595startAgainTimeout(void)
596{
597 NetState = NETLOOP_RESTART;
598}
599
600static void
Luca Ceresoli03eb1292011-04-18 06:19:50 +0000601startAgainHandler(uchar *pkt, unsigned dest, IPaddr_t sip,
602 unsigned src, unsigned len)
wdenk2d966952002-10-31 22:12:35 +0000603{
604 /* Totally ignore the packet */
605}
606
wdenk6e592382004-04-18 17:39:38 +0000607void NetStartAgain (void)
wdenk2d966952002-10-31 22:12:35 +0000608{
wdenk6e592382004-04-18 17:39:38 +0000609 char *nretry;
Remy Bohmer67b96e82009-10-28 22:13:39 +0100610 int retry_forever = 0;
611 unsigned long retrycnt = 0;
wdenka3d991b2004-04-15 21:48:45 +0000612
Remy Bohmer67b96e82009-10-28 22:13:39 +0100613 nretry = getenv("netretry");
614 if (nretry) {
615 if (!strcmp(nretry, "yes"))
616 retry_forever = 1;
617 else if (!strcmp(nretry, "no"))
618 retrycnt = 0;
619 else if (!strcmp(nretry, "once"))
620 retrycnt = 1;
621 else
622 retrycnt = simple_strtoul(nretry, NULL, 0);
623 } else
624 retry_forever = 1;
625
626 if ((!retry_forever) && (NetTryCount >= retrycnt)) {
627 eth_halt();
wdenka3d991b2004-04-15 21:48:45 +0000628 NetState = NETLOOP_FAIL;
629 return;
630 }
Remy Bohmer67b96e82009-10-28 22:13:39 +0100631
632 NetTryCount++;
633
wdenk2d966952002-10-31 22:12:35 +0000634#ifndef CONFIG_NET_MULTI
Bartlomiej Sieka49f3bdb2008-10-01 15:26:28 +0200635 NetSetTimeout (10000UL, startAgainTimeout);
wdenk6e592382004-04-18 17:39:38 +0000636 NetSetHandler (startAgainHandler);
637#else /* !CONFIG_NET_MULTI*/
638 eth_halt ();
Matthias Fuchs8b0c5c12007-12-27 16:58:41 +0100639#if !defined(CONFIG_NET_DO_NOT_TRY_ANOTHER)
wdenk6e592382004-04-18 17:39:38 +0000640 eth_try_another (!NetRestarted);
Matthias Fuchs8b0c5c12007-12-27 16:58:41 +0100641#endif
wdenk6e592382004-04-18 17:39:38 +0000642 eth_init (gd->bd);
643 if (NetRestartWrap) {
wdenk2d966952002-10-31 22:12:35 +0000644 NetRestartWrap = 0;
Remy Bohmer67b96e82009-10-28 22:13:39 +0100645 if (NetDevExists) {
Bartlomiej Sieka49f3bdb2008-10-01 15:26:28 +0200646 NetSetTimeout (10000UL, startAgainTimeout);
wdenk6e592382004-04-18 17:39:38 +0000647 NetSetHandler (startAgainHandler);
648 } else {
wdenk2d966952002-10-31 22:12:35 +0000649 NetState = NETLOOP_FAIL;
650 }
wdenk6e592382004-04-18 17:39:38 +0000651 } else {
wdenk2d966952002-10-31 22:12:35 +0000652 NetState = NETLOOP_RESTART;
653 }
wdenk6e592382004-04-18 17:39:38 +0000654#endif /* CONFIG_NET_MULTI */
wdenk2d966952002-10-31 22:12:35 +0000655}
656
657/**********************************************************************/
658/*
659 * Miscelaneous bits.
660 */
661
662void
663NetSetHandler(rxhand_f * f)
664{
665 packetHandler = f;
666}
667
668
669void
wdenk3e01d752004-10-09 21:56:21 +0000670NetSetTimeout(ulong iv, thand_f * f)
wdenk2d966952002-10-31 22:12:35 +0000671{
672 if (iv == 0) {
673 timeHandler = (thand_f *)0;
674 } else {
675 timeHandler = f;
wdenke0ac62d2003-08-17 18:55:18 +0000676 timeStart = get_timer(0);
677 timeDelta = iv;
wdenk2d966952002-10-31 22:12:35 +0000678 }
679}
680
681
682void
683NetSendPacket(volatile uchar * pkt, int len)
684{
685 (void) eth_send(pkt, len);
686}
687
wdenk73a8b272003-06-05 19:27:42 +0000688int
689NetSendUDPPacket(uchar *ether, IPaddr_t dest, int dport, int sport, int len)
690{
wdenka3d991b2004-04-15 21:48:45 +0000691 uchar *pkt;
692
wdenk73a8b272003-06-05 19:27:42 +0000693 /* convert to new style broadcast */
694 if (dest == 0)
695 dest = 0xFFFFFFFF;
wdenk2d966952002-10-31 22:12:35 +0000696
wdenk73a8b272003-06-05 19:27:42 +0000697 /* if broadcast, make the ether address a broadcast and don't do ARP */
698 if (dest == 0xFFFFFFFF)
699 ether = NetBcastAddr;
700
Luca Ceresoli3e38e422011-05-11 03:59:54 +0000701 /*
702 * if MAC address was not discovered yet, save the packet and do
703 * an ARP request
704 */
wdenk73a8b272003-06-05 19:27:42 +0000705 if (memcmp(ether, NetEtherNullAddr, 6) == 0) {
706
Robin Getz0ebf04c2009-07-23 03:01:03 -0400707 debug("sending ARP for %08lx\n", dest);
708
wdenk73a8b272003-06-05 19:27:42 +0000709 NetArpWaitPacketIP = dest;
710 NetArpWaitPacketMAC = ether;
wdenka3d991b2004-04-15 21:48:45 +0000711
712 pkt = NetArpWaitTxPacket;
713 pkt += NetSetEther (pkt, NetArpWaitPacketMAC, PROT_IP);
714
715 NetSetIP (pkt, dest, dport, sport, len);
Luca Ceresoli3e38e422011-05-11 03:59:54 +0000716 memcpy(pkt + IP_HDR_SIZE, (uchar *)NetTxPacket +
717 (pkt - (uchar *)NetArpWaitTxPacket) + IP_HDR_SIZE, len);
wdenk73a8b272003-06-05 19:27:42 +0000718
719 /* size of the waiting packet */
Luca Ceresoli3e38e422011-05-11 03:59:54 +0000720 NetArpWaitTxPacketSize = (pkt - NetArpWaitTxPacket) +
721 IP_HDR_SIZE + len;
wdenk73a8b272003-06-05 19:27:42 +0000722
723 /* and do the ARP request */
724 NetArpWaitTry = 1;
725 NetArpWaitTimerStart = get_timer(0);
726 ArpRequest();
727 return 1; /* waiting */
728 }
729
Robin Getz0ebf04c2009-07-23 03:01:03 -0400730 debug("sending UDP to %08lx/%pM\n", dest, ether);
wdenk73a8b272003-06-05 19:27:42 +0000731
wdenka3d991b2004-04-15 21:48:45 +0000732 pkt = (uchar *)NetTxPacket;
733 pkt += NetSetEther (pkt, ether, PROT_IP);
734 NetSetIP (pkt, dest, dport, sport, len);
735 (void) eth_send(NetTxPacket, (pkt - NetTxPacket) + IP_HDR_SIZE + len);
wdenk73a8b272003-06-05 19:27:42 +0000736
wdenk6e592382004-04-18 17:39:38 +0000737 return 0; /* transmitted */
wdenk73a8b272003-06-05 19:27:42 +0000738}
739
Jon Loeliger643d1ab2007-07-09 17:45:14 -0500740#if defined(CONFIG_CMD_PING)
wdenk73a8b272003-06-05 19:27:42 +0000741static ushort PingSeqNo;
742
743int PingSend(void)
744{
745 static uchar mac[6];
746 volatile IP_t *ip;
747 volatile ushort *s;
wdenka3d991b2004-04-15 21:48:45 +0000748 uchar *pkt;
wdenk73a8b272003-06-05 19:27:42 +0000749
750 /* XXX always send arp request */
751
752 memcpy(mac, NetEtherNullAddr, 6);
753
Robin Getz0ebf04c2009-07-23 03:01:03 -0400754 debug("sending ARP for %08lx\n", NetPingIP);
wdenk73a8b272003-06-05 19:27:42 +0000755
756 NetArpWaitPacketIP = NetPingIP;
757 NetArpWaitPacketMAC = mac;
758
wdenka3d991b2004-04-15 21:48:45 +0000759 pkt = NetArpWaitTxPacket;
760 pkt += NetSetEther(pkt, mac, PROT_IP);
wdenk73a8b272003-06-05 19:27:42 +0000761
wdenka3d991b2004-04-15 21:48:45 +0000762 ip = (volatile IP_t *)pkt;
wdenk73a8b272003-06-05 19:27:42 +0000763
764 /*
Luca Ceresoli3e38e422011-05-11 03:59:54 +0000765 * Construct an IP and ICMP header.
766 * (need to set no fragment bit - XXX)
wdenk73a8b272003-06-05 19:27:42 +0000767 */
Luca Ceresoli3e38e422011-05-11 03:59:54 +0000768 /* IP_HDR_SIZE / 4 (not including UDP) */
769 ip->ip_hl_v = 0x45;
wdenk73a8b272003-06-05 19:27:42 +0000770 ip->ip_tos = 0;
771 ip->ip_len = htons(IP_HDR_SIZE_NO_UDP + 8);
772 ip->ip_id = htons(NetIPID++);
Peter Tysere0c07b82008-12-01 16:26:20 -0600773 ip->ip_off = htons(IP_FLAGS_DFRAG); /* Don't fragment */
wdenk73a8b272003-06-05 19:27:42 +0000774 ip->ip_ttl = 255;
775 ip->ip_p = 0x01; /* ICMP */
776 ip->ip_sum = 0;
Luca Ceresoli3e38e422011-05-11 03:59:54 +0000777 /* already in network byte order */
778 NetCopyIP((void*)&ip->ip_src, &NetOurIP);
779 /* - "" - */
780 NetCopyIP((void*)&ip->ip_dst, &NetPingIP);
wdenk73a8b272003-06-05 19:27:42 +0000781 ip->ip_sum = ~NetCksum((uchar *)ip, IP_HDR_SIZE_NO_UDP / 2);
782
783 s = &ip->udp_src; /* XXX ICMP starts here */
784 s[0] = htons(0x0800); /* echo-request, code */
785 s[1] = 0; /* checksum */
Wolfgang Denk53677ef2008-05-20 16:00:29 +0200786 s[2] = 0; /* identifier */
wdenk73a8b272003-06-05 19:27:42 +0000787 s[3] = htons(PingSeqNo++); /* sequence number */
788 s[1] = ~NetCksum((uchar *)s, 8/2);
789
790 /* size of the waiting packet */
Luca Ceresoli3e38e422011-05-11 03:59:54 +0000791 NetArpWaitTxPacketSize =
792 (pkt - NetArpWaitTxPacket) + IP_HDR_SIZE_NO_UDP + 8;
wdenk73a8b272003-06-05 19:27:42 +0000793
794 /* and do the ARP request */
795 NetArpWaitTry = 1;
796 NetArpWaitTimerStart = get_timer(0);
797 ArpRequest();
798 return 1; /* waiting */
799}
800
801static void
802PingTimeout (void)
803{
804 eth_halt();
805 NetState = NETLOOP_FAIL; /* we did not get the reply */
806}
807
808static void
Luca Ceresoli03eb1292011-04-18 06:19:50 +0000809PingHandler(uchar *pkt, unsigned dest, IPaddr_t sip, unsigned src,
810 unsigned len)
wdenk73a8b272003-06-05 19:27:42 +0000811{
Luca Ceresoli03eb1292011-04-18 06:19:50 +0000812 if (sip != NetPingIP)
wdenk73a8b272003-06-05 19:27:42 +0000813 return;
814
815 NetState = NETLOOP_SUCCESS;
816}
817
818static void PingStart(void)
819{
wdenka3d991b2004-04-15 21:48:45 +0000820#if defined(CONFIG_NET_MULTI)
821 printf ("Using %s device\n", eth_get_name());
wdenk6e592382004-04-18 17:39:38 +0000822#endif /* CONFIG_NET_MULTI */
Bartlomiej Sieka49f3bdb2008-10-01 15:26:28 +0200823 NetSetTimeout (10000UL, PingTimeout);
wdenk73a8b272003-06-05 19:27:42 +0000824 NetSetHandler (PingHandler);
825
826 PingSend();
827}
Jon Loeliger610f2e92007-07-10 11:05:02 -0500828#endif
wdenk2d966952002-10-31 22:12:35 +0000829
Jon Loeliger643d1ab2007-07-09 17:45:14 -0500830#if defined(CONFIG_CMD_CDP)
wdenka3d991b2004-04-15 21:48:45 +0000831
832#define CDP_DEVICE_ID_TLV 0x0001
833#define CDP_ADDRESS_TLV 0x0002
834#define CDP_PORT_ID_TLV 0x0003
835#define CDP_CAPABILITIES_TLV 0x0004
836#define CDP_VERSION_TLV 0x0005
837#define CDP_PLATFORM_TLV 0x0006
838#define CDP_NATIVE_VLAN_TLV 0x000a
839#define CDP_APPLIANCE_VLAN_TLV 0x000e
840#define CDP_TRIGGER_TLV 0x000f
841#define CDP_POWER_CONSUMPTION_TLV 0x0010
842#define CDP_SYSNAME_TLV 0x0014
843#define CDP_SYSOBJECT_TLV 0x0015
844#define CDP_MANAGEMENT_ADDRESS_TLV 0x0016
845
Bartlomiej Sieka49f3bdb2008-10-01 15:26:28 +0200846#define CDP_TIMEOUT 250UL /* one packet every 250ms */
wdenka3d991b2004-04-15 21:48:45 +0000847
848static int CDPSeq;
849static int CDPOK;
850
851ushort CDPNativeVLAN;
852ushort CDPApplianceVLAN;
853
Luca Ceresoli3e38e422011-05-11 03:59:54 +0000854static const uchar CDP_SNAP_hdr[8] = { 0xAA, 0xAA, 0x03, 0x00, 0x00, 0x0C, 0x20,
855 0x00 };
wdenka3d991b2004-04-15 21:48:45 +0000856
857static ushort CDP_compute_csum(const uchar *buff, ushort len)
858{
859 ushort csum;
860 int odd;
861 ulong result = 0;
862 ushort leftover;
Wolfgang Denk77ddac92005-10-13 16:45:02 +0200863 ushort *p;
wdenka3d991b2004-04-15 21:48:45 +0000864
865 if (len > 0) {
866 odd = 1 & (ulong)buff;
867 if (odd) {
868 result = *buff << 8;
869 len--;
870 buff++;
871 }
872 while (len > 1) {
Wolfgang Denk77ddac92005-10-13 16:45:02 +0200873 p = (ushort *)buff;
874 result += *p++;
875 buff = (uchar *)p;
wdenka3d991b2004-04-15 21:48:45 +0000876 if (result & 0x80000000)
877 result = (result & 0xFFFF) + (result >> 16);
878 len -= 2;
879 }
880 if (len) {
881 leftover = (signed short)(*(const signed char *)buff);
Wolfgang Denk3ada8342005-11-10 20:59:46 +0100882 /* CISCO SUCKS big time! (and blows too):
883 * CDP uses the IP checksum algorithm with a twist;
884 * for the last byte it *sign* extends and sums.
885 */
Luca Ceresoli3e38e422011-05-11 03:59:54 +0000886 result = (result & 0xffff0000) |
887 ((result + leftover) & 0x0000ffff);
wdenka3d991b2004-04-15 21:48:45 +0000888 }
889 while (result >> 16)
890 result = (result & 0xFFFF) + (result >> 16);
891
892 if (odd)
Luca Ceresoli3e38e422011-05-11 03:59:54 +0000893 result = ((result >> 8) & 0xff) |
894 ((result & 0xff) << 8);
wdenka3d991b2004-04-15 21:48:45 +0000895 }
896
897 /* add up 16-bit and 17-bit words for 17+c bits */
898 result = (result & 0xffff) + (result >> 16);
899 /* add up 16-bit and 2-bit for 16+c bit */
900 result = (result & 0xffff) + (result >> 16);
901 /* add up carry.. */
902 result = (result & 0xffff) + (result >> 16);
903
904 /* negate */
905 csum = ~(ushort)result;
906
907 /* run time endian detection */
908 if (csum != htons(csum)) /* little endian */
909 csum = htons(csum);
910
911 return csum;
912}
913
914int CDPSendTrigger(void)
915{
916 volatile uchar *pkt;
917 volatile ushort *s;
918 volatile ushort *cp;
919 Ethernet_t *et;
wdenka3d991b2004-04-15 21:48:45 +0000920 int len;
921 ushort chksum;
wdenk6e592382004-04-18 17:39:38 +0000922#if defined(CONFIG_CDP_DEVICE_ID) || defined(CONFIG_CDP_PORT_ID) || \
923 defined(CONFIG_CDP_VERSION) || defined(CONFIG_CDP_PLATFORM)
924 char buf[32];
925#endif
wdenka3d991b2004-04-15 21:48:45 +0000926
927 pkt = NetTxPacket;
928 et = (Ethernet_t *)pkt;
929
930 /* NOTE: trigger sent not on any VLAN */
931
932 /* form ethernet header */
933 memcpy(et->et_dest, NetCDPAddr, 6);
934 memcpy(et->et_src, NetOurEther, 6);
935
936 pkt += ETHER_HDR_SIZE;
937
938 /* SNAP header */
939 memcpy((uchar *)pkt, CDP_SNAP_hdr, sizeof(CDP_SNAP_hdr));
940 pkt += sizeof(CDP_SNAP_hdr);
941
942 /* CDP header */
943 *pkt++ = 0x02; /* CDP version 2 */
944 *pkt++ = 180; /* TTL */
945 s = (volatile ushort *)pkt;
946 cp = s;
Luca Ceresoli3e38e422011-05-11 03:59:54 +0000947 /* checksum (0 for later calculation) */
948 *s++ = htons(0);
wdenka3d991b2004-04-15 21:48:45 +0000949
950 /* CDP fields */
951#ifdef CONFIG_CDP_DEVICE_ID
952 *s++ = htons(CDP_DEVICE_ID_TLV);
953 *s++ = htons(CONFIG_CDP_DEVICE_ID);
Mike Frysinger95823ca2009-02-11 18:23:48 -0500954 sprintf(buf, CONFIG_CDP_DEVICE_ID_PREFIX "%pm", NetOurEther);
wdenka3d991b2004-04-15 21:48:45 +0000955 memcpy((uchar *)s, buf, 16);
956 s += 16 / 2;
957#endif
958
959#ifdef CONFIG_CDP_PORT_ID
960 *s++ = htons(CDP_PORT_ID_TLV);
961 memset(buf, 0, sizeof(buf));
962 sprintf(buf, CONFIG_CDP_PORT_ID, eth_get_dev_index());
963 len = strlen(buf);
964 if (len & 1) /* make it even */
965 len++;
966 *s++ = htons(len + 4);
967 memcpy((uchar *)s, buf, len);
968 s += len / 2;
969#endif
970
971#ifdef CONFIG_CDP_CAPABILITIES
972 *s++ = htons(CDP_CAPABILITIES_TLV);
973 *s++ = htons(8);
974 *(ulong *)s = htonl(CONFIG_CDP_CAPABILITIES);
975 s += 2;
976#endif
977
978#ifdef CONFIG_CDP_VERSION
979 *s++ = htons(CDP_VERSION_TLV);
980 memset(buf, 0, sizeof(buf));
981 strcpy(buf, CONFIG_CDP_VERSION);
982 len = strlen(buf);
983 if (len & 1) /* make it even */
984 len++;
985 *s++ = htons(len + 4);
986 memcpy((uchar *)s, buf, len);
987 s += len / 2;
988#endif
989
990#ifdef CONFIG_CDP_PLATFORM
991 *s++ = htons(CDP_PLATFORM_TLV);
992 memset(buf, 0, sizeof(buf));
993 strcpy(buf, CONFIG_CDP_PLATFORM);
994 len = strlen(buf);
995 if (len & 1) /* make it even */
996 len++;
997 *s++ = htons(len + 4);
998 memcpy((uchar *)s, buf, len);
999 s += len / 2;
1000#endif
1001
1002#ifdef CONFIG_CDP_TRIGGER
1003 *s++ = htons(CDP_TRIGGER_TLV);
1004 *s++ = htons(8);
1005 *(ulong *)s = htonl(CONFIG_CDP_TRIGGER);
1006 s += 2;
1007#endif
1008
1009#ifdef CONFIG_CDP_POWER_CONSUMPTION
1010 *s++ = htons(CDP_POWER_CONSUMPTION_TLV);
1011 *s++ = htons(6);
1012 *s++ = htons(CONFIG_CDP_POWER_CONSUMPTION);
1013#endif
1014
1015 /* length of ethernet packet */
1016 len = (uchar *)s - ((uchar *)NetTxPacket + ETHER_HDR_SIZE);
1017 et->et_protlen = htons(len);
1018
1019 len = ETHER_HDR_SIZE + sizeof(CDP_SNAP_hdr);
Luca Ceresoli3e38e422011-05-11 03:59:54 +00001020 chksum = CDP_compute_csum((uchar *)NetTxPacket + len,
1021 (uchar *)s - (NetTxPacket + len));
wdenka3d991b2004-04-15 21:48:45 +00001022 if (chksum == 0)
1023 chksum = 0xFFFF;
1024 *cp = htons(chksum);
1025
1026 (void) eth_send(NetTxPacket, (uchar *)s - NetTxPacket);
1027 return 0;
1028}
1029
1030static void
1031CDPTimeout (void)
1032{
1033 CDPSeq++;
1034
1035 if (CDPSeq < 3) {
1036 NetSetTimeout (CDP_TIMEOUT, CDPTimeout);
1037 CDPSendTrigger();
1038 return;
1039 }
1040
1041 /* if not OK try again */
1042 if (!CDPOK)
1043 NetStartAgain();
1044 else
1045 NetState = NETLOOP_SUCCESS;
1046}
1047
1048static void
Luca Ceresoli03eb1292011-04-18 06:19:50 +00001049CDPDummyHandler(uchar *pkt, unsigned dest, IPaddr_t sip, unsigned src,
1050 unsigned len)
wdenka3d991b2004-04-15 21:48:45 +00001051{
1052 /* nothing */
1053}
1054
1055static void
1056CDPHandler(const uchar * pkt, unsigned len)
1057{
1058 const uchar *t;
1059 const ushort *ss;
1060 ushort type, tlen;
1061 uchar applid;
1062 ushort vlan, nvlan;
1063
1064 /* minimum size? */
1065 if (len < sizeof(CDP_SNAP_hdr) + 4)
1066 goto pkt_short;
1067
1068 /* check for valid CDP SNAP header */
1069 if (memcmp(pkt, CDP_SNAP_hdr, sizeof(CDP_SNAP_hdr)) != 0)
1070 return;
1071
1072 pkt += sizeof(CDP_SNAP_hdr);
1073 len -= sizeof(CDP_SNAP_hdr);
1074
1075 /* Version of CDP protocol must be >= 2 and TTL != 0 */
1076 if (pkt[0] < 0x02 || pkt[1] == 0)
1077 return;
1078
Luca Ceresoli3e38e422011-05-11 03:59:54 +00001079 /*
1080 * if version is greater than 0x02 maybe we'll have a problem;
1081 * output a warning
1082 */
wdenka3d991b2004-04-15 21:48:45 +00001083 if (pkt[0] != 0x02)
1084 printf("** WARNING: CDP packet received with a protocol version %d > 2\n",
1085 pkt[0] & 0xff);
1086
1087 if (CDP_compute_csum(pkt, len) != 0)
1088 return;
1089
1090 pkt += 4;
1091 len -= 4;
1092
1093 vlan = htons(-1);
1094 nvlan = htons(-1);
1095 while (len > 0) {
1096 if (len < 4)
1097 goto pkt_short;
1098
1099 ss = (const ushort *)pkt;
1100 type = ntohs(ss[0]);
1101 tlen = ntohs(ss[1]);
1102 if (tlen > len) {
1103 goto pkt_short;
1104 }
1105
1106 pkt += tlen;
1107 len -= tlen;
1108
1109 ss += 2; /* point ss to the data of the TLV */
1110 tlen -= 4;
1111
1112 switch (type) {
1113 case CDP_DEVICE_ID_TLV:
1114 break;
1115 case CDP_ADDRESS_TLV:
1116 break;
1117 case CDP_PORT_ID_TLV:
1118 break;
1119 case CDP_CAPABILITIES_TLV:
1120 break;
1121 case CDP_VERSION_TLV:
1122 break;
1123 case CDP_PLATFORM_TLV:
1124 break;
1125 case CDP_NATIVE_VLAN_TLV:
1126 nvlan = *ss;
1127 break;
1128 case CDP_APPLIANCE_VLAN_TLV:
1129 t = (const uchar *)ss;
1130 while (tlen > 0) {
1131 if (tlen < 3)
1132 goto pkt_short;
1133
1134 applid = t[0];
1135 ss = (const ushort *)(t + 1);
1136
1137#ifdef CONFIG_CDP_APPLIANCE_VLAN_TYPE
Luca Ceresoli3e38e422011-05-11 03:59:54 +00001138 if (applid ==
1139 CONFIG_CDP_APPLIANCE_VLAN_TYPE)
wdenka3d991b2004-04-15 21:48:45 +00001140 vlan = *ss;
1141#else
Luca Ceresoli3e38e422011-05-11 03:59:54 +00001142 /* XXX will this work; dunno */
1143 vlan = ntohs(*ss);
wdenka3d991b2004-04-15 21:48:45 +00001144#endif
1145 t += 3; tlen -= 3;
1146 }
1147 break;
1148 case CDP_TRIGGER_TLV:
1149 break;
1150 case CDP_POWER_CONSUMPTION_TLV:
1151 break;
1152 case CDP_SYSNAME_TLV:
1153 break;
1154 case CDP_SYSOBJECT_TLV:
1155 break;
1156 case CDP_MANAGEMENT_ADDRESS_TLV:
1157 break;
1158 }
1159 }
1160
1161 CDPApplianceVLAN = vlan;
1162 CDPNativeVLAN = nvlan;
1163
1164 CDPOK = 1;
1165 return;
1166
1167 pkt_short:
1168 printf("** CDP packet is too short\n");
1169 return;
1170}
1171
1172static void CDPStart(void)
1173{
1174#if defined(CONFIG_NET_MULTI)
1175 printf ("Using %s device\n", eth_get_name());
1176#endif
1177 CDPSeq = 0;
1178 CDPOK = 0;
1179
1180 CDPNativeVLAN = htons(-1);
1181 CDPApplianceVLAN = htons(-1);
1182
1183 NetSetTimeout (CDP_TIMEOUT, CDPTimeout);
1184 NetSetHandler (CDPDummyHandler);
1185
1186 CDPSendTrigger();
1187}
Jon Loeliger610f2e92007-07-10 11:05:02 -05001188#endif
wdenka3d991b2004-04-15 21:48:45 +00001189
Alessandro Rubini5cfaa4e2009-08-07 13:58:56 +02001190#ifdef CONFIG_IP_DEFRAG
1191/*
1192 * This function collects fragments in a single packet, according
1193 * to the algorithm in RFC815. It returns NULL or the pointer to
1194 * a complete packet, in static storage
1195 */
1196#ifndef CONFIG_NET_MAXDEFRAG
1197#define CONFIG_NET_MAXDEFRAG 16384
1198#endif
1199/*
1200 * MAXDEFRAG, above, is chosen in the config file and is real data
1201 * so we need to add the NFS overhead, which is more than TFTP.
1202 * To use sizeof in the internal unnamed structures, we need a real
1203 * instance (can't do "sizeof(struct rpc_t.u.reply))", unfortunately).
1204 * The compiler doesn't complain nor allocates the actual structure
1205 */
1206static struct rpc_t rpc_specimen;
1207#define IP_PKTSIZE (CONFIG_NET_MAXDEFRAG + sizeof(rpc_specimen.u.reply))
1208
1209#define IP_MAXUDP (IP_PKTSIZE - IP_HDR_SIZE_NO_UDP)
1210
1211/*
1212 * this is the packet being assembled, either data or frag control.
1213 * Fragments go by 8 bytes, so this union must be 8 bytes long
1214 */
1215struct hole {
1216 /* first_byte is address of this structure */
1217 u16 last_byte; /* last byte in this hole + 1 (begin of next hole) */
1218 u16 next_hole; /* index of next (in 8-b blocks), 0 == none */
1219 u16 prev_hole; /* index of prev, 0 == none */
1220 u16 unused;
1221};
1222
1223static IP_t *__NetDefragment(IP_t *ip, int *lenp)
1224{
1225 static uchar pkt_buff[IP_PKTSIZE] __attribute__((aligned(PKTALIGN)));
1226 static u16 first_hole, total_len;
1227 struct hole *payload, *thisfrag, *h, *newh;
1228 IP_t *localip = (IP_t *)pkt_buff;
1229 uchar *indata = (uchar *)ip;
1230 int offset8, start, len, done = 0;
1231 u16 ip_off = ntohs(ip->ip_off);
1232
1233 /* payload starts after IP header, this fragment is in there */
1234 payload = (struct hole *)(pkt_buff + IP_HDR_SIZE_NO_UDP);
1235 offset8 = (ip_off & IP_OFFS);
1236 thisfrag = payload + offset8;
1237 start = offset8 * 8;
1238 len = ntohs(ip->ip_len) - IP_HDR_SIZE_NO_UDP;
1239
1240 if (start + len > IP_MAXUDP) /* fragment extends too far */
1241 return NULL;
1242
1243 if (!total_len || localip->ip_id != ip->ip_id) {
1244 /* new (or different) packet, reset structs */
1245 total_len = 0xffff;
1246 payload[0].last_byte = ~0;
1247 payload[0].next_hole = 0;
1248 payload[0].prev_hole = 0;
1249 first_hole = 0;
1250 /* any IP header will work, copy the first we received */
1251 memcpy(localip, ip, IP_HDR_SIZE_NO_UDP);
1252 }
1253
1254 /*
1255 * What follows is the reassembly algorithm. We use the payload
1256 * array as a linked list of hole descriptors, as each hole starts
1257 * at a multiple of 8 bytes. However, last byte can be whatever value,
1258 * so it is represented as byte count, not as 8-byte blocks.
1259 */
1260
1261 h = payload + first_hole;
1262 while (h->last_byte < start) {
1263 if (!h->next_hole) {
1264 /* no hole that far away */
1265 return NULL;
1266 }
1267 h = payload + h->next_hole;
1268 }
1269
Fillod Stephanee397e592010-06-11 19:26:43 +02001270 /* last fragment may be 1..7 bytes, the "+7" forces acceptance */
1271 if (offset8 + ((len + 7) / 8) <= h - payload) {
Alessandro Rubini5cfaa4e2009-08-07 13:58:56 +02001272 /* no overlap with holes (dup fragment?) */
1273 return NULL;
1274 }
1275
1276 if (!(ip_off & IP_FLAGS_MFRAG)) {
1277 /* no more fragmentss: truncate this (last) hole */
1278 total_len = start + len;
1279 h->last_byte = start + len;
1280 }
1281
1282 /*
1283 * There is some overlap: fix the hole list. This code doesn't
1284 * deal with a fragment that overlaps with two different holes
1285 * (thus being a superset of a previously-received fragment).
1286 */
1287
1288 if ( (h >= thisfrag) && (h->last_byte <= start + len) ) {
1289 /* complete overlap with hole: remove hole */
1290 if (!h->prev_hole && !h->next_hole) {
1291 /* last remaining hole */
1292 done = 1;
1293 } else if (!h->prev_hole) {
1294 /* first hole */
1295 first_hole = h->next_hole;
1296 payload[h->next_hole].prev_hole = 0;
1297 } else if (!h->next_hole) {
1298 /* last hole */
1299 payload[h->prev_hole].next_hole = 0;
1300 } else {
1301 /* in the middle of the list */
1302 payload[h->next_hole].prev_hole = h->prev_hole;
1303 payload[h->prev_hole].next_hole = h->next_hole;
1304 }
1305
1306 } else if (h->last_byte <= start + len) {
1307 /* overlaps with final part of the hole: shorten this hole */
1308 h->last_byte = start;
1309
1310 } else if (h >= thisfrag) {
1311 /* overlaps with initial part of the hole: move this hole */
1312 newh = thisfrag + (len / 8);
1313 *newh = *h;
1314 h = newh;
1315 if (h->next_hole)
1316 payload[h->next_hole].prev_hole = (h - payload);
1317 if (h->prev_hole)
1318 payload[h->prev_hole].next_hole = (h - payload);
1319 else
1320 first_hole = (h - payload);
1321
1322 } else {
1323 /* fragment sits in the middle: split the hole */
1324 newh = thisfrag + (len / 8);
1325 *newh = *h;
1326 h->last_byte = start;
1327 h->next_hole = (newh - payload);
1328 newh->prev_hole = (h - payload);
1329 if (newh->next_hole)
1330 payload[newh->next_hole].prev_hole = (newh - payload);
1331 }
1332
1333 /* finally copy this fragment and possibly return whole packet */
1334 memcpy((uchar *)thisfrag, indata + IP_HDR_SIZE_NO_UDP, len);
1335 if (!done)
1336 return NULL;
1337
1338 localip->ip_len = htons(total_len);
1339 *lenp = total_len + IP_HDR_SIZE_NO_UDP;
1340 return localip;
1341}
1342
1343static inline IP_t *NetDefragment(IP_t *ip, int *lenp)
1344{
1345 u16 ip_off = ntohs(ip->ip_off);
1346 if (!(ip_off & (IP_OFFS | IP_FLAGS_MFRAG)))
1347 return ip; /* not a fragment */
1348 return __NetDefragment(ip, lenp);
1349}
1350
1351#else /* !CONFIG_IP_DEFRAG */
1352
1353static inline IP_t *NetDefragment(IP_t *ip, int *lenp)
1354{
1355 u16 ip_off = ntohs(ip->ip_off);
1356 if (!(ip_off & (IP_OFFS | IP_FLAGS_MFRAG)))
1357 return ip; /* not a fragment */
1358 return NULL;
1359}
1360#endif
wdenka3d991b2004-04-15 21:48:45 +00001361
wdenk2d966952002-10-31 22:12:35 +00001362void
wdenka3d991b2004-04-15 21:48:45 +00001363NetReceive(volatile uchar * inpkt, int len)
wdenk2d966952002-10-31 22:12:35 +00001364{
1365 Ethernet_t *et;
1366 IP_t *ip;
1367 ARP_t *arp;
1368 IPaddr_t tmp;
Luca Ceresoli03eb1292011-04-18 06:19:50 +00001369 IPaddr_t src_ip;
wdenk2d966952002-10-31 22:12:35 +00001370 int x;
wdenka3d991b2004-04-15 21:48:45 +00001371 uchar *pkt;
Jon Loeliger643d1ab2007-07-09 17:45:14 -05001372#if defined(CONFIG_CMD_CDP)
wdenka3d991b2004-04-15 21:48:45 +00001373 int iscdp;
1374#endif
1375 ushort cti = 0, vlanid = VLAN_NONE, myvlanid, mynvlanid;
wdenk2d966952002-10-31 22:12:35 +00001376
Robin Getz0ebf04c2009-07-23 03:01:03 -04001377 debug("packet received\n");
wdenka3d991b2004-04-15 21:48:45 +00001378
Mike Frysingerd9bec9f2009-07-18 21:04:08 -04001379 NetRxPacket = inpkt;
1380 NetRxPacketLen = len;
wdenka3d991b2004-04-15 21:48:45 +00001381 et = (Ethernet_t *)inpkt;
1382
1383 /* too small packet? */
1384 if (len < ETHER_HDR_SIZE)
1385 return;
1386
Rafal Jaworowskif85b6072007-12-27 18:19:02 +01001387#ifdef CONFIG_API
1388 if (push_packet) {
1389 (*push_packet)(inpkt, len);
1390 return;
1391 }
1392#endif
1393
Jon Loeliger643d1ab2007-07-09 17:45:14 -05001394#if defined(CONFIG_CMD_CDP)
wdenka3d991b2004-04-15 21:48:45 +00001395 /* keep track if packet is CDP */
1396 iscdp = memcmp(et->et_dest, NetCDPAddr, 6) == 0;
1397#endif
1398
1399 myvlanid = ntohs(NetOurVLAN);
1400 if (myvlanid == (ushort)-1)
1401 myvlanid = VLAN_NONE;
1402 mynvlanid = ntohs(NetOurNativeVLAN);
1403 if (mynvlanid == (ushort)-1)
1404 mynvlanid = VLAN_NONE;
wdenk2d966952002-10-31 22:12:35 +00001405
1406 x = ntohs(et->et_protlen);
1407
Robin Getz0ebf04c2009-07-23 03:01:03 -04001408 debug("packet received\n");
wdenka3d991b2004-04-15 21:48:45 +00001409
wdenk2d966952002-10-31 22:12:35 +00001410 if (x < 1514) {
1411 /*
1412 * Got a 802 packet. Check the other protocol field.
1413 */
1414 x = ntohs(et->et_prot);
wdenka3d991b2004-04-15 21:48:45 +00001415
1416 ip = (IP_t *)(inpkt + E802_HDR_SIZE);
wdenk2d966952002-10-31 22:12:35 +00001417 len -= E802_HDR_SIZE;
wdenka3d991b2004-04-15 21:48:45 +00001418
1419 } else if (x != PROT_VLAN) { /* normal packet */
1420 ip = (IP_t *)(inpkt + ETHER_HDR_SIZE);
wdenk2d966952002-10-31 22:12:35 +00001421 len -= ETHER_HDR_SIZE;
wdenka3d991b2004-04-15 21:48:45 +00001422
1423 } else { /* VLAN packet */
1424 VLAN_Ethernet_t *vet = (VLAN_Ethernet_t *)et;
1425
Robin Getz0ebf04c2009-07-23 03:01:03 -04001426 debug("VLAN packet received\n");
1427
wdenka3d991b2004-04-15 21:48:45 +00001428 /* too small packet? */
1429 if (len < VLAN_ETHER_HDR_SIZE)
1430 return;
1431
1432 /* if no VLAN active */
1433 if ((ntohs(NetOurVLAN) & VLAN_IDMASK) == VLAN_NONE
Jon Loeliger643d1ab2007-07-09 17:45:14 -05001434#if defined(CONFIG_CMD_CDP)
wdenka3d991b2004-04-15 21:48:45 +00001435 && iscdp == 0
1436#endif
1437 )
1438 return;
1439
1440 cti = ntohs(vet->vet_tag);
1441 vlanid = cti & VLAN_IDMASK;
1442 x = ntohs(vet->vet_type);
1443
1444 ip = (IP_t *)(inpkt + VLAN_ETHER_HDR_SIZE);
1445 len -= VLAN_ETHER_HDR_SIZE;
wdenk2d966952002-10-31 22:12:35 +00001446 }
1447
Robin Getz0ebf04c2009-07-23 03:01:03 -04001448 debug("Receive from protocol 0x%x\n", x);
wdenk2d966952002-10-31 22:12:35 +00001449
Jon Loeliger643d1ab2007-07-09 17:45:14 -05001450#if defined(CONFIG_CMD_CDP)
wdenka3d991b2004-04-15 21:48:45 +00001451 if (iscdp) {
1452 CDPHandler((uchar *)ip, len);
1453 return;
1454 }
1455#endif
1456
1457 if ((myvlanid & VLAN_IDMASK) != VLAN_NONE) {
1458 if (vlanid == VLAN_NONE)
1459 vlanid = (mynvlanid & VLAN_IDMASK);
1460 /* not matched? */
1461 if (vlanid != (myvlanid & VLAN_IDMASK))
1462 return;
1463 }
1464
wdenk2d966952002-10-31 22:12:35 +00001465 switch (x) {
1466
1467 case PROT_ARP:
1468 /*
1469 * We have to deal with two types of ARP packets:
wdenk8bde7f72003-06-27 21:31:46 +00001470 * - REQUEST packets will be answered by sending our
1471 * IP address - if we know it.
1472 * - REPLY packates are expected only after we asked
1473 * for the TFTP server's or the gateway's ethernet
1474 * address; so if we receive such a packet, we set
1475 * the server ethernet address
wdenk2d966952002-10-31 22:12:35 +00001476 */
Robin Getz0ebf04c2009-07-23 03:01:03 -04001477 debug("Got ARP\n");
1478
wdenk2d966952002-10-31 22:12:35 +00001479 arp = (ARP_t *)ip;
1480 if (len < ARP_HDR_SIZE) {
1481 printf("bad length %d < %d\n", len, ARP_HDR_SIZE);
1482 return;
1483 }
1484 if (ntohs(arp->ar_hrd) != ARP_ETHER) {
1485 return;
1486 }
1487 if (ntohs(arp->ar_pro) != PROT_IP) {
1488 return;
1489 }
1490 if (arp->ar_hln != 6) {
1491 return;
1492 }
1493 if (arp->ar_pln != 4) {
1494 return;
1495 }
1496
1497 if (NetOurIP == 0) {
1498 return;
1499 }
1500
1501 if (NetReadIP(&arp->ar_data[16]) != NetOurIP) {
1502 return;
1503 }
1504
1505 switch (ntohs(arp->ar_op)) {
Luca Ceresoli3e38e422011-05-11 03:59:54 +00001506 case ARPOP_REQUEST:
1507 /* reply with our IP address */
Robin Getz0ebf04c2009-07-23 03:01:03 -04001508 debug("Got ARP REQUEST, return our IP\n");
wdenka3d991b2004-04-15 21:48:45 +00001509 pkt = (uchar *)et;
1510 pkt += NetSetEther(pkt, et->et_src, PROT_ARP);
wdenk2d966952002-10-31 22:12:35 +00001511 arp->ar_op = htons(ARPOP_REPLY);
1512 memcpy (&arp->ar_data[10], &arp->ar_data[0], 6);
1513 NetCopyIP(&arp->ar_data[16], &arp->ar_data[6]);
1514 memcpy (&arp->ar_data[ 0], NetOurEther, 6);
1515 NetCopyIP(&arp->ar_data[ 6], &NetOurIP);
Luca Ceresoli3e38e422011-05-11 03:59:54 +00001516 (void) eth_send((uchar *)et,
1517 (pkt - (uchar *)et) + ARP_HDR_SIZE);
wdenk2d966952002-10-31 22:12:35 +00001518 return;
wdenk73a8b272003-06-05 19:27:42 +00001519
1520 case ARPOP_REPLY: /* arp reply */
1521 /* are we waiting for a reply */
1522 if (!NetArpWaitPacketIP || !NetArpWaitPacketMAC)
1523 break;
Robin Getz97cfe862009-07-21 12:15:28 -04001524
1525#ifdef CONFIG_KEEP_SERVERADDR
1526 if (NetServerIP == NetArpWaitPacketIP) {
1527 char buf[20];
1528 sprintf(buf, "%pM", arp->ar_data);
1529 setenv("serveraddr", buf);
1530 }
1531#endif
1532
Robin Getz0ebf04c2009-07-23 03:01:03 -04001533 debug("Got ARP REPLY, set server/gtwy eth addr (%pM)\n",
Mike Frysinger95823ca2009-02-11 18:23:48 -05001534 arp->ar_data);
wdenk73a8b272003-06-05 19:27:42 +00001535
1536 tmp = NetReadIP(&arp->ar_data[6]);
1537
1538 /* matched waiting packet's address */
1539 if (tmp == NetArpWaitReplyIP) {
Robin Getz0ebf04c2009-07-23 03:01:03 -04001540 debug("Got it\n");
wdenk73a8b272003-06-05 19:27:42 +00001541 /* save address for later use */
Luca Ceresoli3e38e422011-05-11 03:59:54 +00001542 memcpy(NetArpWaitPacketMAC,
1543 &arp->ar_data[0], 6);
wdenk73a8b272003-06-05 19:27:42 +00001544
wdenk68ceb292004-08-02 21:11:11 +00001545#ifdef CONFIG_NETCONSOLE
Luca Ceresoli03eb1292011-04-18 06:19:50 +00001546 (*packetHandler)(0, 0, 0, 0, 0);
wdenk68ceb292004-08-02 21:11:11 +00001547#endif
wdenk73a8b272003-06-05 19:27:42 +00001548 /* modify header, and transmit it */
1549 memcpy(((Ethernet_t *)NetArpWaitTxPacket)->et_dest, NetArpWaitPacketMAC, 6);
Luca Ceresoli3e38e422011-05-11 03:59:54 +00001550 (void) eth_send(NetArpWaitTxPacket,
1551 NetArpWaitTxPacketSize);
wdenk73a8b272003-06-05 19:27:42 +00001552
1553 /* no arp request pending now */
1554 NetArpWaitPacketIP = 0;
1555 NetArpWaitTxPacketSize = 0;
1556 NetArpWaitPacketMAC = NULL;
1557
1558 }
wdenk2d966952002-10-31 22:12:35 +00001559 return;
1560 default:
Luca Ceresoli3e38e422011-05-11 03:59:54 +00001561 debug("Unexpected ARP opcode 0x%x\n",
1562 ntohs(arp->ar_op));
wdenk2d966952002-10-31 22:12:35 +00001563 return;
1564 }
wdenk289f9322005-01-12 00:15:14 +00001565 break;
wdenk2d966952002-10-31 22:12:35 +00001566
Peter Tyserbf6cb242010-09-30 11:25:48 -05001567#ifdef CONFIG_CMD_RARP
wdenk2d966952002-10-31 22:12:35 +00001568 case PROT_RARP:
Robin Getz0ebf04c2009-07-23 03:01:03 -04001569 debug("Got RARP\n");
wdenk2d966952002-10-31 22:12:35 +00001570 arp = (ARP_t *)ip;
1571 if (len < ARP_HDR_SIZE) {
1572 printf("bad length %d < %d\n", len, ARP_HDR_SIZE);
1573 return;
1574 }
1575
1576 if ((ntohs(arp->ar_op) != RARPOP_REPLY) ||
1577 (ntohs(arp->ar_hrd) != ARP_ETHER) ||
1578 (ntohs(arp->ar_pro) != PROT_IP) ||
1579 (arp->ar_hln != 6) || (arp->ar_pln != 4)) {
1580
wdenk4b9206e2004-03-23 22:14:11 +00001581 puts ("invalid RARP header\n");
wdenk2d966952002-10-31 22:12:35 +00001582 } else {
1583 NetCopyIP(&NetOurIP, &arp->ar_data[16]);
wdenk3d3befa2004-03-14 15:06:13 +00001584 if (NetServerIP == 0)
1585 NetCopyIP(&NetServerIP, &arp->ar_data[ 6]);
wdenk2d966952002-10-31 22:12:35 +00001586 memcpy (NetServerEther, &arp->ar_data[ 0], 6);
1587
Luca Ceresoli03eb1292011-04-18 06:19:50 +00001588 (*packetHandler)(0, 0, 0, 0, 0);
wdenk2d966952002-10-31 22:12:35 +00001589 }
1590 break;
Peter Tyserbf6cb242010-09-30 11:25:48 -05001591#endif
wdenk2d966952002-10-31 22:12:35 +00001592 case PROT_IP:
Robin Getz0ebf04c2009-07-23 03:01:03 -04001593 debug("Got IP\n");
Alessandro Rubini5cfaa4e2009-08-07 13:58:56 +02001594 /* Before we start poking the header, make sure it is there */
wdenk2d966952002-10-31 22:12:35 +00001595 if (len < IP_HDR_SIZE) {
Robin Getz0ebf04c2009-07-23 03:01:03 -04001596 debug("len bad %d < %lu\n", len, (ulong)IP_HDR_SIZE);
wdenk2d966952002-10-31 22:12:35 +00001597 return;
1598 }
Alessandro Rubini5cfaa4e2009-08-07 13:58:56 +02001599 /* Check the packet length */
wdenk2d966952002-10-31 22:12:35 +00001600 if (len < ntohs(ip->ip_len)) {
1601 printf("len bad %d < %d\n", len, ntohs(ip->ip_len));
1602 return;
1603 }
1604 len = ntohs(ip->ip_len);
Robin Getz0ebf04c2009-07-23 03:01:03 -04001605 debug("len=%d, v=%02x\n", len, ip->ip_hl_v & 0xff);
1606
Alessandro Rubini5cfaa4e2009-08-07 13:58:56 +02001607 /* Can't deal with anything except IPv4 */
wdenk2d966952002-10-31 22:12:35 +00001608 if ((ip->ip_hl_v & 0xf0) != 0x40) {
1609 return;
1610 }
Alessandro Rubini5cfaa4e2009-08-07 13:58:56 +02001611 /* Can't deal with IP options (headers != 20 bytes) */
Remy Bohmer6b52cfe2008-06-03 15:48:17 +02001612 if ((ip->ip_hl_v & 0x0f) > 0x05) {
1613 return;
1614 }
Alessandro Rubini5cfaa4e2009-08-07 13:58:56 +02001615 /* Check the Checksum of the header */
wdenk2d966952002-10-31 22:12:35 +00001616 if (!NetCksumOk((uchar *)ip, IP_HDR_SIZE_NO_UDP / 2)) {
wdenk4b9206e2004-03-23 22:14:11 +00001617 puts ("checksum bad\n");
wdenk2d966952002-10-31 22:12:35 +00001618 return;
1619 }
Alessandro Rubini5cfaa4e2009-08-07 13:58:56 +02001620 /* If it is not for us, ignore it */
wdenk2d966952002-10-31 22:12:35 +00001621 tmp = NetReadIP(&ip->ip_dst);
1622 if (NetOurIP && tmp != NetOurIP && tmp != 0xFFFFFFFF) {
David Updegraff53a5c422007-06-11 10:41:07 -05001623#ifdef CONFIG_MCAST_TFTP
Wolfgang Denk85eb5ca2007-08-14 09:47:27 +02001624 if (Mcast_addr != tmp)
David Updegraff53a5c422007-06-11 10:41:07 -05001625#endif
wdenk2d966952002-10-31 22:12:35 +00001626 return;
1627 }
Luca Ceresoli03eb1292011-04-18 06:19:50 +00001628 /* Read source IP address for later use */
1629 src_ip = NetReadIP(&ip->ip_src);
wdenk2d966952002-10-31 22:12:35 +00001630 /*
Alessandro Rubini5cfaa4e2009-08-07 13:58:56 +02001631 * The function returns the unchanged packet if it's not
1632 * a fragment, and either the complete packet or NULL if
1633 * it is a fragment (if !CONFIG_IP_DEFRAG, it returns NULL)
1634 */
1635 if (!(ip = NetDefragment(ip, &len)))
1636 return;
1637 /*
wdenk2d966952002-10-31 22:12:35 +00001638 * watch for ICMP host redirects
1639 *
wdenk8bde7f72003-06-27 21:31:46 +00001640 * There is no real handler code (yet). We just watch
1641 * for ICMP host redirect messages. In case anybody
1642 * sees these messages: please contact me
1643 * (wd@denx.de), or - even better - send me the
1644 * necessary fixes :-)
wdenk2d966952002-10-31 22:12:35 +00001645 *
wdenk8bde7f72003-06-27 21:31:46 +00001646 * Note: in all cases where I have seen this so far
1647 * it was a problem with the router configuration,
1648 * for instance when a router was configured in the
1649 * BOOTP reply, but the TFTP server was on the same
1650 * subnet. So this is probably a warning that your
1651 * configuration might be wrong. But I'm not really
1652 * sure if there aren't any other situations.
wdenk2d966952002-10-31 22:12:35 +00001653 */
1654 if (ip->ip_p == IPPROTO_ICMP) {
1655 ICMP_t *icmph = (ICMP_t *)&(ip->udp_src);
1656
wdenk73a8b272003-06-05 19:27:42 +00001657 switch (icmph->type) {
1658 case ICMP_REDIRECT:
wdenk90dc6702005-05-03 14:12:25 +00001659 if (icmph->code != ICMP_REDIR_HOST)
1660 return;
Luca Ceresoli3e38e422011-05-11 03:59:54 +00001661 printf (" ICMP Host Redirect to %pI4 ",
1662 &icmph->un.gateway);
Stefan Roese8534bf92005-08-12 20:06:52 +02001663 return;
Jon Loeliger643d1ab2007-07-09 17:45:14 -05001664#if defined(CONFIG_CMD_PING)
wdenk73a8b272003-06-05 19:27:42 +00001665 case ICMP_ECHO_REPLY:
1666 /*
Luca Ceresoli3e38e422011-05-11 03:59:54 +00001667 * IP header OK. Pass the packet to the
1668 * current handler.
wdenk73a8b272003-06-05 19:27:42 +00001669 */
1670 /* XXX point to ip packet */
Luca Ceresoli03eb1292011-04-18 06:19:50 +00001671 (*packetHandler)((uchar *)ip, 0, src_ip, 0, 0);
Stefan Roese8534bf92005-08-12 20:06:52 +02001672 return;
Ed Swarthout83853172007-03-07 12:14:50 -06001673 case ICMP_ECHO_REQUEST:
Luca Ceresoli3e38e422011-05-11 03:59:54 +00001674 debug("Got ICMP ECHO REQUEST, return %d bytes\n",
Luca Ceresoli03eb1292011-04-18 06:19:50 +00001675 ETHER_HDR_SIZE + len);
Robin Getz0ebf04c2009-07-23 03:01:03 -04001676
Ed Swarthout83853172007-03-07 12:14:50 -06001677 memcpy (&et->et_dest[0], &et->et_src[0], 6);
1678 memcpy (&et->et_src[ 0], NetOurEther, 6);
1679
1680 ip->ip_sum = 0;
1681 ip->ip_off = 0;
1682 NetCopyIP((void*)&ip->ip_dst, &ip->ip_src);
1683 NetCopyIP((void*)&ip->ip_src, &NetOurIP);
Luca Ceresoli3e38e422011-05-11 03:59:54 +00001684 ip->ip_sum = ~NetCksum((uchar *)ip,
1685 IP_HDR_SIZE_NO_UDP >> 1);
Ed Swarthout83853172007-03-07 12:14:50 -06001686
1687 icmph->type = ICMP_ECHO_REPLY;
1688 icmph->checksum = 0;
1689 icmph->checksum = ~NetCksum((uchar *)icmph,
Luca Ceresoli3e38e422011-05-11 03:59:54 +00001690 (len - IP_HDR_SIZE_NO_UDP) >> 1);
1691 (void) eth_send((uchar *)et,
1692 ETHER_HDR_SIZE + len);
Ed Swarthout83853172007-03-07 12:14:50 -06001693 return;
wdenk73a8b272003-06-05 19:27:42 +00001694#endif
1695 default:
1696 return;
1697 }
wdenk2d966952002-10-31 22:12:35 +00001698 } else if (ip->ip_p != IPPROTO_UDP) { /* Only UDP packets */
1699 return;
1700 }
1701
Stefan Roese8534bf92005-08-12 20:06:52 +02001702#ifdef CONFIG_UDP_CHECKSUM
1703 if (ip->udp_xsum != 0) {
Wolfgang Denkb2f50802005-08-12 23:43:12 +02001704 ulong xsum;
Stefan Roese8534bf92005-08-12 20:06:52 +02001705 ushort *sumptr;
1706 ushort sumlen;
1707
1708 xsum = ip->ip_p;
1709 xsum += (ntohs(ip->udp_len));
1710 xsum += (ntohl(ip->ip_src) >> 16) & 0x0000ffff;
1711 xsum += (ntohl(ip->ip_src) >> 0) & 0x0000ffff;
1712 xsum += (ntohl(ip->ip_dst) >> 16) & 0x0000ffff;
1713 xsum += (ntohl(ip->ip_dst) >> 0) & 0x0000ffff;
1714
1715 sumlen = ntohs(ip->udp_len);
1716 sumptr = (ushort *) &(ip->udp_src);
1717
1718 while (sumlen > 1) {
Wolfgang Denkb2f50802005-08-12 23:43:12 +02001719 ushort sumdata;
Stefan Roese8534bf92005-08-12 20:06:52 +02001720
1721 sumdata = *sumptr++;
1722 xsum += ntohs(sumdata);
1723 sumlen -= 2;
1724 }
1725 if (sumlen > 0) {
Wolfgang Denkb2f50802005-08-12 23:43:12 +02001726 ushort sumdata;
Stefan Roese8534bf92005-08-12 20:06:52 +02001727
1728 sumdata = *(unsigned char *) sumptr;
Wolfgang Denkb2f50802005-08-12 23:43:12 +02001729 sumdata = (sumdata << 8) & 0xff00;
Stefan Roese8534bf92005-08-12 20:06:52 +02001730 xsum += sumdata;
1731 }
1732 while ((xsum >> 16) != 0) {
Luca Ceresoli3e38e422011-05-11 03:59:54 +00001733 xsum = (xsum & 0x0000ffff) +
1734 ((xsum >> 16) & 0x0000ffff);
Stefan Roese8534bf92005-08-12 20:06:52 +02001735 }
1736 if ((xsum != 0x00000000) && (xsum != 0x0000ffff)) {
Wolfgang Denk9b55a252008-07-11 01:16:00 +02001737 printf(" UDP wrong checksum %08lx %08x\n",
1738 xsum, ntohs(ip->udp_xsum));
Stefan Roese8534bf92005-08-12 20:06:52 +02001739 return;
1740 }
1741 }
1742#endif
1743
David Updegraff53a5c422007-06-11 10:41:07 -05001744
wdenk68ceb292004-08-02 21:11:11 +00001745#ifdef CONFIG_NETCONSOLE
1746 nc_input_packet((uchar *)ip +IP_HDR_SIZE,
1747 ntohs(ip->udp_dst),
1748 ntohs(ip->udp_src),
1749 ntohs(ip->udp_len) - 8);
1750#endif
wdenk2d966952002-10-31 22:12:35 +00001751 /*
1752 * IP header OK. Pass the packet to the current handler.
1753 */
1754 (*packetHandler)((uchar *)ip +IP_HDR_SIZE,
1755 ntohs(ip->udp_dst),
Luca Ceresoli03eb1292011-04-18 06:19:50 +00001756 src_ip,
wdenk2d966952002-10-31 22:12:35 +00001757 ntohs(ip->udp_src),
1758 ntohs(ip->udp_len) - 8);
wdenk2d966952002-10-31 22:12:35 +00001759 break;
1760 }
1761}
1762
1763
1764/**********************************************************************/
1765
1766static int net_check_prereq (proto_t protocol)
1767{
1768 switch (protocol) {
wdenk6e592382004-04-18 17:39:38 +00001769 /* Fall through */
Jon Loeliger643d1ab2007-07-09 17:45:14 -05001770#if defined(CONFIG_CMD_PING)
wdenk73a8b272003-06-05 19:27:42 +00001771 case PING:
wdenk6e592382004-04-18 17:39:38 +00001772 if (NetPingIP == 0) {
1773 puts ("*** ERROR: ping address not given\n");
1774 return (1);
1775 }
1776 goto common;
wdenk73a8b272003-06-05 19:27:42 +00001777#endif
Jon Loeliger643d1ab2007-07-09 17:45:14 -05001778#if defined(CONFIG_CMD_SNTP)
wdenkea287de2005-04-01 00:25:43 +00001779 case SNTP:
1780 if (NetNtpServerIP == 0) {
1781 puts ("*** ERROR: NTP server address not given\n");
1782 return (1);
1783 }
1784 goto common;
1785#endif
Robin Getz1a32bf42009-07-20 14:53:54 -04001786#if defined(CONFIG_CMD_DNS)
1787 case DNS:
1788 if (NetOurDNSIP == 0) {
1789 puts("*** ERROR: DNS server address not given\n");
1790 return 1;
1791 }
1792 goto common;
1793#endif
Jon Loeliger643d1ab2007-07-09 17:45:14 -05001794#if defined(CONFIG_CMD_NFS)
wdenkcbd8a352004-02-24 02:00:03 +00001795 case NFS:
1796#endif
wdenk2d966952002-10-31 22:12:35 +00001797 case TFTP:
wdenk6e592382004-04-18 17:39:38 +00001798 if (NetServerIP == 0) {
1799 puts ("*** ERROR: `serverip' not set\n");
1800 return (1);
1801 }
Gray Remlin9030a552011-03-29 10:21:32 +00001802#if defined(CONFIG_CMD_PING) || defined(CONFIG_CMD_SNTP) || \
1803 defined(CONFIG_CMD_DNS)
Wolfgang Denkb2f50802005-08-12 23:43:12 +02001804 common:
wdenk73a8b272003-06-05 19:27:42 +00001805#endif
Simon Guinot8b6bbe12011-05-01 23:38:40 +00001806 /* Fall through */
wdenk73a8b272003-06-05 19:27:42 +00001807
Simon Guinot8b6bbe12011-05-01 23:38:40 +00001808 case NETCONS:
wdenk6e592382004-04-18 17:39:38 +00001809 if (NetOurIP == 0) {
1810 puts ("*** ERROR: `ipaddr' not set\n");
1811 return (1);
1812 }
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:
wdenk6e592382004-04-18 17:39:38 +00001821 if (memcmp (NetOurEther, "\0\0\0\0\0\0", 6) == 0) {
wdenk2d966952002-10-31 22:12:35 +00001822#ifdef CONFIG_NET_MULTI
wdenk6e592382004-04-18 17:39:38 +00001823 extern int eth_get_dev_index (void);
1824 int num = eth_get_dev_index ();
wdenk2d966952002-10-31 22:12:35 +00001825
wdenk6e592382004-04-18 17:39:38 +00001826 switch (num) {
1827 case -1:
wdenk2d966952002-10-31 22:12:35 +00001828 puts ("*** ERROR: No ethernet found.\n");
1829 return (1);
wdenk6e592382004-04-18 17:39:38 +00001830 case 0:
wdenk2d966952002-10-31 22:12:35 +00001831 puts ("*** ERROR: `ethaddr' not set\n");
1832 break;
wdenk6e592382004-04-18 17:39:38 +00001833 default:
wdenk8bde7f72003-06-27 21:31:46 +00001834 printf ("*** ERROR: `eth%daddr' not set\n",
wdenk2d966952002-10-31 22:12:35 +00001835 num);
1836 break;
wdenk2d966952002-10-31 22:12:35 +00001837 }
wdenk6e592382004-04-18 17:39:38 +00001838
1839 NetStartAgain ();
1840 return (2);
1841#else
1842 puts ("*** ERROR: `ethaddr' not set\n");
1843 return (1);
1844#endif
1845 }
1846 /* Fall through */
1847 default:
1848 return (0);
wdenk2d966952002-10-31 22:12:35 +00001849 }
wdenk6e592382004-04-18 17:39:38 +00001850 return (0); /* OK */
wdenk2d966952002-10-31 22:12:35 +00001851}
1852/**********************************************************************/
1853
1854int
1855NetCksumOk(uchar * ptr, int len)
1856{
1857 return !((NetCksum(ptr, len) + 1) & 0xfffe);
1858}
1859
1860
1861unsigned
1862NetCksum(uchar * ptr, int len)
1863{
1864 ulong xsum;
Stefan Roese9d2a8732005-08-31 12:55:50 +02001865 ushort *p = (ushort *)ptr;
wdenk2d966952002-10-31 22:12:35 +00001866
1867 xsum = 0;
1868 while (len-- > 0)
Wolfgang Denk7bc5ee02005-08-26 01:36:03 +02001869 xsum += *p++;
wdenk2d966952002-10-31 22:12:35 +00001870 xsum = (xsum & 0xffff) + (xsum >> 16);
1871 xsum = (xsum & 0xffff) + (xsum >> 16);
1872 return (xsum & 0xffff);
1873}
1874
wdenka3d991b2004-04-15 21:48:45 +00001875int
1876NetEthHdrSize(void)
1877{
1878 ushort myvlanid;
wdenk2d966952002-10-31 22:12:35 +00001879
wdenka3d991b2004-04-15 21:48:45 +00001880 myvlanid = ntohs(NetOurVLAN);
1881 if (myvlanid == (ushort)-1)
1882 myvlanid = VLAN_NONE;
1883
Luca Ceresoli3e38e422011-05-11 03:59:54 +00001884 return ((myvlanid & VLAN_IDMASK) == VLAN_NONE) ? ETHER_HDR_SIZE :
1885 VLAN_ETHER_HDR_SIZE;
wdenka3d991b2004-04-15 21:48:45 +00001886}
1887
1888int
wdenk2d966952002-10-31 22:12:35 +00001889NetSetEther(volatile uchar * xet, uchar * addr, uint prot)
1890{
1891 Ethernet_t *et = (Ethernet_t *)xet;
wdenka3d991b2004-04-15 21:48:45 +00001892 ushort myvlanid;
1893
1894 myvlanid = ntohs(NetOurVLAN);
1895 if (myvlanid == (ushort)-1)
1896 myvlanid = VLAN_NONE;
wdenk2d966952002-10-31 22:12:35 +00001897
1898 memcpy (et->et_dest, addr, 6);
1899 memcpy (et->et_src, NetOurEther, 6);
wdenka3d991b2004-04-15 21:48:45 +00001900 if ((myvlanid & VLAN_IDMASK) == VLAN_NONE) {
wdenk2d966952002-10-31 22:12:35 +00001901 et->et_protlen = htons(prot);
wdenka3d991b2004-04-15 21:48:45 +00001902 return ETHER_HDR_SIZE;
1903 } else {
1904 VLAN_Ethernet_t *vet = (VLAN_Ethernet_t *)xet;
wdenk2d966952002-10-31 22:12:35 +00001905
wdenka3d991b2004-04-15 21:48:45 +00001906 vet->vet_vlan_type = htons(PROT_VLAN);
1907 vet->vet_tag = htons((0 << 5) | (myvlanid & VLAN_IDMASK));
1908 vet->vet_type = htons(prot);
1909 return VLAN_ETHER_HDR_SIZE;
1910 }
1911}
wdenk2d966952002-10-31 22:12:35 +00001912
1913void
1914NetSetIP(volatile uchar * xip, IPaddr_t dest, int dport, int sport, int len)
1915{
Olav Morkenaf8626e2009-01-23 12:56:26 +01001916 IP_t *ip = (IP_t *)xip;
wdenk2d966952002-10-31 22:12:35 +00001917
1918 /*
1919 * If the data is an odd number of bytes, zero the
1920 * byte after the last byte so that the checksum
1921 * will work.
1922 */
1923 if (len & 1)
1924 xip[IP_HDR_SIZE + len] = 0;
1925
1926 /*
1927 * Construct an IP and UDP header.
wdenk6e592382004-04-18 17:39:38 +00001928 * (need to set no fragment bit - XXX)
wdenk2d966952002-10-31 22:12:35 +00001929 */
Luca Ceresoli3e38e422011-05-11 03:59:54 +00001930 /* IP_HDR_SIZE / 4 (not including UDP) */
1931 ip->ip_hl_v = 0x45;
wdenk2d966952002-10-31 22:12:35 +00001932 ip->ip_tos = 0;
1933 ip->ip_len = htons(IP_HDR_SIZE + len);
1934 ip->ip_id = htons(NetIPID++);
Peter Tysere0c07b82008-12-01 16:26:20 -06001935 ip->ip_off = htons(IP_FLAGS_DFRAG); /* Don't fragment */
wdenk2d966952002-10-31 22:12:35 +00001936 ip->ip_ttl = 255;
1937 ip->ip_p = 17; /* UDP */
1938 ip->ip_sum = 0;
Luca Ceresoli3e38e422011-05-11 03:59:54 +00001939 /* already in network byte order */
1940 NetCopyIP((void*)&ip->ip_src, &NetOurIP);
1941 /* - "" - */
1942 NetCopyIP((void*)&ip->ip_dst, &dest);
wdenk2d966952002-10-31 22:12:35 +00001943 ip->udp_src = htons(sport);
1944 ip->udp_dst = htons(dport);
1945 ip->udp_len = htons(8 + len);
1946 ip->udp_xsum = 0;
1947 ip->ip_sum = ~NetCksum((uchar *)ip, IP_HDR_SIZE_NO_UDP / 2);
1948}
1949
Mike Frysingerb920ee92010-10-20 07:16:40 -04001950void copy_filename (char *dst, const char *src, int size)
wdenk2d966952002-10-31 22:12:35 +00001951{
1952 if (*src && (*src == '"')) {
1953 ++src;
1954 --size;
1955 }
1956
1957 while ((--size > 0) && *src && (*src != '"')) {
1958 *dst++ = *src++;
1959 }
1960 *dst = '\0';
1961}
1962
Luca Ceresoli3e38e422011-05-11 03:59:54 +00001963#if defined(CONFIG_CMD_NFS) || \
1964 defined(CONFIG_CMD_SNTP) || \
1965 defined(CONFIG_CMD_DNS)
Robin Getz1a32bf42009-07-20 14:53:54 -04001966/*
Robin Getz97399462010-03-08 14:07:00 -05001967 * make port a little random (1024-17407)
1968 * This keeps the math somewhat trivial to compute, and seems to work with
1969 * all supported protocols/clients/servers
Robin Getz1a32bf42009-07-20 14:53:54 -04001970 */
1971unsigned int random_port(void)
1972{
Robin Getz97399462010-03-08 14:07:00 -05001973 return 1024 + (get_timer(0) % 0x4000);
Robin Getz1a32bf42009-07-20 14:53:54 -04001974}
1975#endif
1976
wdenk2d966952002-10-31 22:12:35 +00001977void ip_to_string (IPaddr_t x, char *s)
1978{
wdenka3d991b2004-04-15 21:48:45 +00001979 x = ntohl (x);
1980 sprintf (s, "%d.%d.%d.%d",
1981 (int) ((x >> 24) & 0xff),
1982 (int) ((x >> 16) & 0xff),
1983 (int) ((x >> 8) & 0xff), (int) ((x >> 0) & 0xff)
wdenk6e592382004-04-18 17:39:38 +00001984 );
wdenk2d966952002-10-31 22:12:35 +00001985}
1986
wdenka3d991b2004-04-15 21:48:45 +00001987void VLAN_to_string(ushort x, char *s)
1988{
1989 x = ntohs(x);
1990
1991 if (x == (ushort)-1)
1992 x = VLAN_NONE;
1993
1994 if (x == VLAN_NONE)
1995 strcpy(s, "none");
1996 else
1997 sprintf(s, "%d", x & VLAN_IDMASK);
1998}
1999
Mike Frysinger2e3ef6e2010-10-20 07:16:48 -04002000ushort string_to_VLAN(const char *s)
wdenka3d991b2004-04-15 21:48:45 +00002001{
2002 ushort id;
2003
2004 if (s == NULL)
wdenkb9711de2004-04-25 13:18:40 +00002005 return htons(VLAN_NONE);
wdenka3d991b2004-04-15 21:48:45 +00002006
2007 if (*s < '0' || *s > '9')
2008 id = VLAN_NONE;
2009 else
2010 id = (ushort)simple_strtoul(s, NULL, 10);
2011
wdenkb9711de2004-04-25 13:18:40 +00002012 return htons(id);
wdenka3d991b2004-04-15 21:48:45 +00002013}
2014
wdenka3d991b2004-04-15 21:48:45 +00002015ushort getenv_VLAN(char *var)
2016{
2017 return (string_to_VLAN(getenv(var)));
2018}