blob: 4cd5a0a73c858e69d17f835b941a667a7048dae1 [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>
Joe Hershberger48522bb2012-05-15 08:59:08 +000080#include <linux/compiler.h>
wdenk2d966952002-10-31 22:12:35 +000081#include <net.h>
82#include "bootp.h"
83#include "tftp.h"
Peter Tyserbf6cb242010-09-30 11:25:48 -050084#ifdef CONFIG_CMD_RARP
wdenk2d966952002-10-31 22:12:35 +000085#include "rarp.h"
Peter Tyserbf6cb242010-09-30 11:25:48 -050086#endif
wdenkcbd8a352004-02-24 02:00:03 +000087#include "nfs.h"
wdenkfc3e2162003-10-08 22:33:00 +000088#ifdef CONFIG_STATUS_LED
89#include <status_led.h>
90#include <miiphy.h>
91#endif
Jon Loeliger643d1ab2007-07-09 17:45:14 -050092#if defined(CONFIG_CMD_SNTP)
wdenkea287de2005-04-01 00:25:43 +000093#include "sntp.h"
94#endif
Joe Hershbergerf575ae12012-05-23 07:57:59 +000095#include "cdp.h"
Robin Getz1a32bf42009-07-20 14:53:54 -040096#if defined(CONFIG_CMD_DNS)
97#include "dns.h"
98#endif
wdenk2d966952002-10-31 22:12:35 +000099
Wolfgang Denkd87080b2006-03-31 18:32:53 +0200100DECLARE_GLOBAL_DATA_PTR;
101
Guennadi Liakhovetski40cb90e2008-04-03 17:04:19 +0200102#ifndef CONFIG_ARP_TIMEOUT
Luca Ceresoli3e38e422011-05-11 03:59:54 +0000103/* Milliseconds before trying ARP again */
104# define ARP_TIMEOUT 5000UL
wdenk73a8b272003-06-05 19:27:42 +0000105#else
Bartlomiej Sieka49f3bdb2008-10-01 15:26:28 +0200106# define ARP_TIMEOUT CONFIG_ARP_TIMEOUT
Guennadi Liakhovetski40cb90e2008-04-03 17:04:19 +0200107#endif
108
109
110#ifndef CONFIG_NET_RETRY_COUNT
111# define ARP_TIMEOUT_COUNT 5 /* # of timeouts before giving up */
112#else
113# define ARP_TIMEOUT_COUNT CONFIG_NET_RETRY_COUNT
wdenk73a8b272003-06-05 19:27:42 +0000114#endif
115
wdenk2d966952002-10-31 22:12:35 +0000116/** BOOTP EXTENTIONS **/
117
Luca Ceresoli3e38e422011-05-11 03:59:54 +0000118/* Our subnet mask (0=unknown) */
Luca Ceresolic586ce62011-05-11 03:59:55 +0000119IPaddr_t NetOurSubnetMask;
Luca Ceresoli3e38e422011-05-11 03:59:54 +0000120/* Our gateways IP address */
Luca Ceresolic586ce62011-05-11 03:59:55 +0000121IPaddr_t NetOurGatewayIP;
Luca Ceresoli3e38e422011-05-11 03:59:54 +0000122/* Our DNS IP address */
Luca Ceresolic586ce62011-05-11 03:59:55 +0000123IPaddr_t NetOurDNSIP;
Jon Loeliger1fe80d72007-07-09 22:08:34 -0500124#if defined(CONFIG_BOOTP_DNS2)
Luca Ceresoli3e38e422011-05-11 03:59:54 +0000125/* Our 2nd DNS IP address */
Luca Ceresolic586ce62011-05-11 03:59:55 +0000126IPaddr_t NetOurDNS2IP;
stroesefe389a82003-08-28 14:17:32 +0000127#endif
Luca Ceresoli3e38e422011-05-11 03:59:54 +0000128/* Our NIS domain */
Luca Ceresolic586ce62011-05-11 03:59:55 +0000129char NetOurNISDomain[32] = {0,};
Luca Ceresoli3e38e422011-05-11 03:59:54 +0000130/* Our hostname */
Luca Ceresolic586ce62011-05-11 03:59:55 +0000131char NetOurHostName[32] = {0,};
Luca Ceresoli3e38e422011-05-11 03:59:54 +0000132/* Our bootpath */
Luca Ceresolic586ce62011-05-11 03:59:55 +0000133char NetOurRootPath[64] = {0,};
Luca Ceresoli3e38e422011-05-11 03:59:54 +0000134/* Our bootfile size in blocks */
Luca Ceresolic586ce62011-05-11 03:59:55 +0000135ushort NetBootFileSize;
wdenk2d966952002-10-31 22:12:35 +0000136
David Updegraff53a5c422007-06-11 10:41:07 -0500137#ifdef CONFIG_MCAST_TFTP /* Multicast TFTP */
138IPaddr_t Mcast_addr;
139#endif
140
wdenk2d966952002-10-31 22:12:35 +0000141/** END OF BOOTP EXTENTIONS **/
142
Luca Ceresoli3e38e422011-05-11 03:59:54 +0000143/* The actual transferred size of the bootfile (in bytes) */
144ulong NetBootFileXferSize;
145/* Our ethernet address */
146uchar NetOurEther[6];
147/* Boot server enet address */
Luca Ceresolic586ce62011-05-11 03:59:55 +0000148uchar NetServerEther[6];
Luca Ceresoli3e38e422011-05-11 03:59:54 +0000149/* Our IP addr (0 = unknown) */
150IPaddr_t NetOurIP;
151/* Server IP addr (0 = unknown) */
152IPaddr_t NetServerIP;
153/* Current receive packet */
Joe Hershbergerdb288a92012-05-15 08:59:04 +0000154uchar *NetRxPacket;
Luca Ceresoli3e38e422011-05-11 03:59:54 +0000155/* Current rx packet length */
156int NetRxPacketLen;
157/* IP packet ID */
158unsigned NetIPID;
159/* Ethernet bcast address */
Luca Ceresolic586ce62011-05-11 03:59:55 +0000160uchar NetBcastAddr[6] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
161uchar NetEtherNullAddr[6];
Rafal Jaworowskif85b6072007-12-27 18:19:02 +0100162#ifdef CONFIG_API
Joe Hershbergerdb288a92012-05-15 08:59:04 +0000163void (*push_packet)(void *, int len) = 0;
Rafal Jaworowskif85b6072007-12-27 18:19:02 +0100164#endif
Luca Ceresoli3e38e422011-05-11 03:59:54 +0000165/* Network loop state */
166int NetState;
Luca Ceresoli3e38e422011-05-11 03:59:54 +0000167/* Tried all network devices */
Luca Ceresolic586ce62011-05-11 03:59:55 +0000168int NetRestartWrap;
Luca Ceresoli3e38e422011-05-11 03:59:54 +0000169/* Network loop restarted */
Luca Ceresolic586ce62011-05-11 03:59:55 +0000170static int NetRestarted;
Luca Ceresoli3e38e422011-05-11 03:59:54 +0000171/* At least one device configured */
Luca Ceresolic586ce62011-05-11 03:59:55 +0000172static int NetDevExists;
wdenk2d966952002-10-31 22:12:35 +0000173
wdenk6e592382004-04-18 17:39:38 +0000174/* XXX in both little & big endian machines 0xFFFF == ntohs(-1) */
Luca Ceresoli3e38e422011-05-11 03:59:54 +0000175/* default is without VLAN */
176ushort NetOurVLAN = 0xFFFF;
177/* ditto */
178ushort NetOurNativeVLAN = 0xFFFF;
wdenka3d991b2004-04-15 21:48:45 +0000179
Luca Ceresoli3e38e422011-05-11 03:59:54 +0000180/* Boot File name */
181char BootFile[128];
wdenk2d966952002-10-31 22:12:35 +0000182
Jon Loeliger643d1ab2007-07-09 17:45:14 -0500183#if defined(CONFIG_CMD_PING)
Luca Ceresoli3e38e422011-05-11 03:59:54 +0000184/* the ip address to ping */
185IPaddr_t NetPingIP;
wdenk73a8b272003-06-05 19:27:42 +0000186
187static void PingStart(void);
188#endif
189
Jon Loeliger643d1ab2007-07-09 17:45:14 -0500190#if defined(CONFIG_CMD_SNTP)
Luca Ceresoli3e38e422011-05-11 03:59:54 +0000191/* NTP server IP address */
192IPaddr_t NetNtpServerIP;
193/* offset time from UTC */
Luca Ceresolic586ce62011-05-11 03:59:55 +0000194int NetTimeOffset;
wdenkea287de2005-04-01 00:25:43 +0000195#endif
196
Joe Hershbergerdb288a92012-05-15 08:59:04 +0000197uchar PktBuf[(PKTBUFSRX+1) * PKTSIZE_ALIGN + PKTALIGN];
wdenk2d966952002-10-31 22:12:35 +0000198
Luca Ceresoli3e38e422011-05-11 03:59:54 +0000199/* Receive packet */
Joe Hershbergerdb288a92012-05-15 08:59:04 +0000200uchar *NetRxPackets[PKTBUFSRX];
wdenk2d966952002-10-31 22:12:35 +0000201
Luca Ceresoli3e38e422011-05-11 03:59:54 +0000202/* Current RX packet handler */
203static rxhand_f *packetHandler;
Simon Glass39bccd22011-10-26 14:18:38 +0000204#ifdef CONFIG_CMD_TFTPPUT
Simon Glass4793ee62011-10-24 18:00:01 +0000205static rxhand_icmp_f *packet_icmp_handler; /* Current ICMP rx handler */
Simon Glass39bccd22011-10-26 14:18:38 +0000206#endif
Luca Ceresoli3e38e422011-05-11 03:59:54 +0000207/* Current timeout handler */
208static thand_f *timeHandler;
209/* Time base value */
210static ulong timeStart;
211/* Current timeout value */
212static ulong timeDelta;
213/* THE transmit packet */
Joe Hershbergerdb288a92012-05-15 08:59:04 +0000214uchar *NetTxPacket;
wdenk2d966952002-10-31 22:12:35 +0000215
Simon Glasse4bf0c52011-10-24 18:00:02 +0000216static int net_check_prereq(enum proto_t protocol);
wdenk2d966952002-10-31 22:12:35 +0000217
Remy Bohmer67b96e82009-10-28 22:13:39 +0100218static int NetTryCount;
219
wdenk2d966952002-10-31 22:12:35 +0000220/**********************************************************************/
wdenk73a8b272003-06-05 19:27:42 +0000221
222IPaddr_t NetArpWaitPacketIP;
223IPaddr_t NetArpWaitReplyIP;
Luca Ceresoli3e38e422011-05-11 03:59:54 +0000224/* MAC address of waiting packet's destination */
225uchar *NetArpWaitPacketMAC;
226/* THE transmit packet */
227uchar *NetArpWaitTxPacket;
wdenk73a8b272003-06-05 19:27:42 +0000228int NetArpWaitTxPacketSize;
Wolfgang Denk53677ef2008-05-20 16:00:29 +0200229uchar NetArpWaitPacketBuf[PKTSIZE_ALIGN + PKTALIGN];
wdenk73a8b272003-06-05 19:27:42 +0000230ulong NetArpWaitTimerStart;
231int NetArpWaitTry;
232
Luca Ceresoli4f63acd2011-05-11 03:59:56 +0000233void ArpRequest(void)
wdenk73a8b272003-06-05 19:27:42 +0000234{
Joe Hershbergerdb288a92012-05-15 08:59:04 +0000235 uchar *pkt;
wdenk6e592382004-04-18 17:39:38 +0000236 ARP_t *arp;
wdenk73a8b272003-06-05 19:27:42 +0000237
Robin Getz0ebf04c2009-07-23 03:01:03 -0400238 debug("ARP broadcast %d\n", NetArpWaitTry);
239
wdenk73a8b272003-06-05 19:27:42 +0000240 pkt = NetTxPacket;
241
Luca Ceresoli4f63acd2011-05-11 03:59:56 +0000242 pkt += NetSetEther(pkt, NetBcastAddr, PROT_ARP);
wdenk73a8b272003-06-05 19:27:42 +0000243
wdenk6e592382004-04-18 17:39:38 +0000244 arp = (ARP_t *) pkt;
wdenk73a8b272003-06-05 19:27:42 +0000245
Luca Ceresoli4f63acd2011-05-11 03:59:56 +0000246 arp->ar_hrd = htons(ARP_ETHER);
247 arp->ar_pro = htons(PROT_IP);
wdenk73a8b272003-06-05 19:27:42 +0000248 arp->ar_hln = 6;
249 arp->ar_pln = 4;
Luca Ceresoli4f63acd2011-05-11 03:59:56 +0000250 arp->ar_op = htons(ARPOP_REQUEST);
wdenk73a8b272003-06-05 19:27:42 +0000251
Luca Ceresoli3e38e422011-05-11 03:59:54 +0000252 /* source ET addr */
Luca Ceresoli4f63acd2011-05-11 03:59:56 +0000253 memcpy(&arp->ar_data[0], NetOurEther, 6);
Luca Ceresoli3e38e422011-05-11 03:59:54 +0000254 /* source IP addr */
Luca Ceresoli4f63acd2011-05-11 03:59:56 +0000255 NetWriteIP((uchar *) &arp->ar_data[6], NetOurIP);
Simon Glassed1ada72011-10-26 14:18:39 +0000256 /* dest ET addr = 0 */
257 memset(&arp->ar_data[10], '\0', 6);
wdenk6e592382004-04-18 17:39:38 +0000258 if ((NetArpWaitPacketIP & NetOurSubnetMask) !=
259 (NetOurIP & NetOurSubnetMask)) {
260 if (NetOurGatewayIP == 0) {
Luca Ceresoli4f63acd2011-05-11 03:59:56 +0000261 puts("## Warning: gatewayip needed but not set\n");
Wolfgang Denkd509b812006-03-12 01:13:30 +0100262 NetArpWaitReplyIP = NetArpWaitPacketIP;
263 } else {
264 NetArpWaitReplyIP = NetOurGatewayIP;
wdenk6e592382004-04-18 17:39:38 +0000265 }
wdenk6e592382004-04-18 17:39:38 +0000266 } else {
267 NetArpWaitReplyIP = NetArpWaitPacketIP;
268 }
wdenk73a8b272003-06-05 19:27:42 +0000269
Luca Ceresoli4f63acd2011-05-11 03:59:56 +0000270 NetWriteIP((uchar *) &arp->ar_data[16], NetArpWaitReplyIP);
271 (void) eth_send(NetTxPacket, (pkt - NetTxPacket) + ARP_HDR_SIZE);
wdenk73a8b272003-06-05 19:27:42 +0000272}
273
274void ArpTimeoutCheck(void)
275{
276 ulong t;
277
278 if (!NetArpWaitPacketIP)
279 return;
280
281 t = get_timer(0);
282
283 /* check for arp timeout */
Bartlomiej Sieka49f3bdb2008-10-01 15:26:28 +0200284 if ((t - NetArpWaitTimerStart) > ARP_TIMEOUT) {
wdenk73a8b272003-06-05 19:27:42 +0000285 NetArpWaitTry++;
286
287 if (NetArpWaitTry >= ARP_TIMEOUT_COUNT) {
Luca Ceresoli4f63acd2011-05-11 03:59:56 +0000288 puts("\nARP Retry count exceeded; starting again\n");
wdenk73a8b272003-06-05 19:27:42 +0000289 NetArpWaitTry = 0;
290 NetStartAgain();
291 } else {
292 NetArpWaitTimerStart = t;
293 ArpRequest();
294 }
295 }
296}
297
Simon Glasse4a3d572011-10-27 06:24:32 +0000298/*
299 * Check if autoload is enabled. If so, use either NFS or TFTP to download
300 * the boot file.
301 */
302void net_auto_load(void)
303{
304 const char *s = getenv("autoload");
305
306 if (s != NULL) {
307 if (*s == 'n') {
308 /*
309 * Just use BOOTP/RARP to configure system;
310 * Do not use TFTP to load the bootfile.
311 */
312 NetState = NETLOOP_SUCCESS;
313 return;
314 }
315#if defined(CONFIG_CMD_NFS)
316 if (strcmp(s, "NFS") == 0) {
317 /*
318 * Use NFS to load the bootfile.
319 */
320 NfsStart();
321 return;
322 }
323#endif
324 }
325 TftpStart(TFTPGET);
326}
327
Simon Glasse4bf0c52011-10-24 18:00:02 +0000328static void NetInitLoop(enum proto_t protocol)
Heiko Schocher2f70c492009-02-10 09:38:52 +0100329{
Luca Ceresolic586ce62011-05-11 03:59:55 +0000330 static int env_changed_id;
Luca Ceresoli4f63acd2011-05-11 03:59:56 +0000331 int env_id = get_env_id();
Heiko Schocher2f70c492009-02-10 09:38:52 +0100332
333 /* update only when the environment has changed */
Michael Zaidman3c172c42009-04-04 01:43:00 +0300334 if (env_changed_id != env_id) {
Enric Balletbo i Serra23a70bf2011-05-31 21:01:47 +0000335 NetOurIP = getenv_IPaddr("ipaddr");
Luca Ceresoli4f63acd2011-05-11 03:59:56 +0000336 NetOurGatewayIP = getenv_IPaddr("gatewayip");
337 NetOurSubnetMask = getenv_IPaddr("netmask");
338 NetServerIP = getenv_IPaddr("serverip");
Heiko Schocher2f70c492009-02-10 09:38:52 +0100339 NetOurNativeVLAN = getenv_VLAN("nvlan");
Michael Zaidman3c172c42009-04-04 01:43:00 +0300340 NetOurVLAN = getenv_VLAN("vlan");
Robin Getz1a32bf42009-07-20 14:53:54 -0400341#if defined(CONFIG_CMD_DNS)
342 NetOurDNSIP = getenv_IPaddr("dnsip");
343#endif
Michael Zaidman3c172c42009-04-04 01:43:00 +0300344 env_changed_id = env_id;
Heiko Schocher2f70c492009-02-10 09:38:52 +0100345 }
Michael Zaidman3c172c42009-04-04 01:43:00 +0300346
Heiko Schocherda954272009-04-28 08:36:11 +0200347 return;
Heiko Schocher2f70c492009-02-10 09:38:52 +0100348}
349
wdenk73a8b272003-06-05 19:27:42 +0000350/**********************************************************************/
wdenk2d966952002-10-31 22:12:35 +0000351/*
352 * Main network processing loop.
353 */
354
Simon Glasse4bf0c52011-10-24 18:00:02 +0000355int NetLoop(enum proto_t protocol)
wdenk2d966952002-10-31 22:12:35 +0000356{
wdenk2d966952002-10-31 22:12:35 +0000357 bd_t *bd = gd->bd;
Simon Glass4793ee62011-10-24 18:00:01 +0000358 int ret = -1;
wdenk2d966952002-10-31 22:12:35 +0000359
wdenk2d966952002-10-31 22:12:35 +0000360 NetRestarted = 0;
361 NetDevExists = 0;
wdenk2d966952002-10-31 22:12:35 +0000362
wdenk73a8b272003-06-05 19:27:42 +0000363 /* XXX problem with bss workaround */
364 NetArpWaitPacketMAC = NULL;
365 NetArpWaitTxPacket = NULL;
366 NetArpWaitPacketIP = 0;
367 NetArpWaitReplyIP = 0;
368 NetArpWaitTxPacket = NULL;
369 NetTxPacket = NULL;
Remy Bohmer67b96e82009-10-28 22:13:39 +0100370 NetTryCount = 1;
wdenk73a8b272003-06-05 19:27:42 +0000371
wdenk2d966952002-10-31 22:12:35 +0000372 if (!NetTxPacket) {
373 int i;
wdenk2d966952002-10-31 22:12:35 +0000374 /*
375 * Setup packet buffers, aligned correctly.
376 */
377 NetTxPacket = &PktBuf[0] + (PKTALIGN - 1);
378 NetTxPacket -= (ulong)NetTxPacket % PKTALIGN;
Luca Ceresolid3c65b02011-05-04 02:40:43 +0000379 for (i = 0; i < PKTBUFSRX; i++)
wdenk2d966952002-10-31 22:12:35 +0000380 NetRxPackets[i] = NetTxPacket + (i+1)*PKTSIZE_ALIGN;
wdenk73a8b272003-06-05 19:27:42 +0000381 }
382
383 if (!NetArpWaitTxPacket) {
384 NetArpWaitTxPacket = &NetArpWaitPacketBuf[0] + (PKTALIGN - 1);
385 NetArpWaitTxPacket -= (ulong)NetArpWaitTxPacket % PKTALIGN;
386 NetArpWaitTxPacketSize = 0;
wdenk2d966952002-10-31 22:12:35 +0000387 }
388
Simon Glass573f14f2011-12-10 11:08:06 +0000389 bootstage_mark_name(BOOTSTAGE_ID_ETH_START, "eth_start");
wdenk2d966952002-10-31 22:12:35 +0000390 eth_halt();
wdenka3d991b2004-04-15 21:48:45 +0000391 eth_set_current();
wdenkb1bf6f22005-04-03 14:52:59 +0000392 if (eth_init(bd) < 0) {
393 eth_halt();
Luca Ceresoli92895de2011-05-04 02:40:45 +0000394 return -1;
wdenkb1bf6f22005-04-03 14:52:59 +0000395 }
wdenk2d966952002-10-31 22:12:35 +0000396
397restart:
Luca Ceresoli4f63acd2011-05-11 03:59:56 +0000398 memcpy(NetOurEther, eth_get_dev()->enetaddr, 6);
wdenk2d966952002-10-31 22:12:35 +0000399
400 NetState = NETLOOP_CONTINUE;
401
402 /*
403 * Start the ball rolling with the given start function. From
404 * here on, this code is a state machine driven by received
405 * packets and timer events.
406 */
Heiko Schocher2f70c492009-02-10 09:38:52 +0100407 NetInitLoop(protocol);
wdenk2d966952002-10-31 22:12:35 +0000408
Luca Ceresoli4f63acd2011-05-11 03:59:56 +0000409 switch (net_check_prereq(protocol)) {
wdenk2d966952002-10-31 22:12:35 +0000410 case 1:
411 /* network not configured */
wdenkb1bf6f22005-04-03 14:52:59 +0000412 eth_halt();
Luca Ceresoli92895de2011-05-04 02:40:45 +0000413 return -1;
wdenk2d966952002-10-31 22:12:35 +0000414
wdenk2d966952002-10-31 22:12:35 +0000415 case 2:
416 /* network device not configured */
417 break;
wdenk2d966952002-10-31 22:12:35 +0000418
419 case 0:
wdenk2d966952002-10-31 22:12:35 +0000420 NetDevExists = 1;
Simon Glasse4bf0c52011-10-24 18:00:02 +0000421 NetBootFileXferSize = 0;
wdenk2d966952002-10-31 22:12:35 +0000422 switch (protocol) {
Simon Glasse4bf0c52011-10-24 18:00:02 +0000423 case TFTPGET:
Simon Glass1fb7cd42011-10-24 18:00:07 +0000424#ifdef CONFIG_CMD_TFTPPUT
425 case TFTPPUT:
426#endif
wdenk2d966952002-10-31 22:12:35 +0000427 /* always use ARP to get server ethernet address */
Simon Glasse4bf0c52011-10-24 18:00:02 +0000428 TftpStart(protocol);
wdenk2d966952002-10-31 22:12:35 +0000429 break;
Luca Ceresoli7a83af02011-05-17 00:03:40 +0000430#ifdef CONFIG_CMD_TFTPSRV
431 case TFTPSRV:
432 TftpStartServer();
433 break;
434#endif
Jon Loeliger643d1ab2007-07-09 17:45:14 -0500435#if defined(CONFIG_CMD_DHCP)
wdenk2d966952002-10-31 22:12:35 +0000436 case DHCP:
wdenkd407bf52004-10-11 22:51:13 +0000437 BootpTry = 0;
Michael Zaidman09133f82009-07-14 23:37:12 +0300438 NetOurIP = 0;
wdenk2d966952002-10-31 22:12:35 +0000439 DhcpRequest(); /* Basically same as BOOTP */
440 break;
Jon Loeliger610f2e92007-07-10 11:05:02 -0500441#endif
wdenk2d966952002-10-31 22:12:35 +0000442
443 case BOOTP:
444 BootpTry = 0;
Michael Zaidman09133f82009-07-14 23:37:12 +0300445 NetOurIP = 0;
Luca Ceresoli4f63acd2011-05-11 03:59:56 +0000446 BootpRequest();
wdenk2d966952002-10-31 22:12:35 +0000447 break;
448
Peter Tyserbf6cb242010-09-30 11:25:48 -0500449#if defined(CONFIG_CMD_RARP)
wdenk2d966952002-10-31 22:12:35 +0000450 case RARP:
451 RarpTry = 0;
Michael Zaidman09133f82009-07-14 23:37:12 +0300452 NetOurIP = 0;
Luca Ceresoli4f63acd2011-05-11 03:59:56 +0000453 RarpRequest();
wdenk2d966952002-10-31 22:12:35 +0000454 break;
Peter Tyserbf6cb242010-09-30 11:25:48 -0500455#endif
Jon Loeliger643d1ab2007-07-09 17:45:14 -0500456#if defined(CONFIG_CMD_PING)
wdenk73a8b272003-06-05 19:27:42 +0000457 case PING:
458 PingStart();
459 break;
460#endif
Jon Loeliger643d1ab2007-07-09 17:45:14 -0500461#if defined(CONFIG_CMD_NFS)
wdenkcbd8a352004-02-24 02:00:03 +0000462 case NFS:
463 NfsStart();
464 break;
465#endif
Jon Loeliger643d1ab2007-07-09 17:45:14 -0500466#if defined(CONFIG_CMD_CDP)
wdenka3d991b2004-04-15 21:48:45 +0000467 case CDP:
468 CDPStart();
469 break;
470#endif
wdenk68ceb292004-08-02 21:11:11 +0000471#ifdef CONFIG_NETCONSOLE
472 case NETCONS:
473 NcStart();
474 break;
475#endif
Jon Loeliger643d1ab2007-07-09 17:45:14 -0500476#if defined(CONFIG_CMD_SNTP)
wdenkea287de2005-04-01 00:25:43 +0000477 case SNTP:
478 SntpStart();
479 break;
480#endif
Robin Getz1a32bf42009-07-20 14:53:54 -0400481#if defined(CONFIG_CMD_DNS)
482 case DNS:
483 DnsStart();
484 break;
485#endif
wdenk2d966952002-10-31 22:12:35 +0000486 default:
487 break;
488 }
489
wdenk2d966952002-10-31 22:12:35 +0000490 break;
491 }
492
Jon Loeliger643d1ab2007-07-09 17:45:14 -0500493#if defined(CONFIG_MII) || defined(CONFIG_CMD_MII)
Luca Ceresoli3e38e422011-05-11 03:59:54 +0000494#if defined(CONFIG_SYS_FAULT_ECHO_LINK_DOWN) && \
495 defined(CONFIG_STATUS_LED) && \
496 defined(STATUS_LED_RED)
wdenkfc3e2162003-10-08 22:33:00 +0000497 /*
wdenk42d1f032003-10-15 23:53:47 +0000498 * Echo the inverted link state to the fault LED.
wdenkfc3e2162003-10-08 22:33:00 +0000499 */
Luca Ceresolid3c65b02011-05-04 02:40:43 +0000500 if (miiphy_link(eth_get_dev()->name, CONFIG_SYS_FAULT_MII_ADDR))
Luca Ceresoli4f63acd2011-05-11 03:59:56 +0000501 status_led_set(STATUS_LED_RED, STATUS_LED_OFF);
Luca Ceresolid3c65b02011-05-04 02:40:43 +0000502 else
Luca Ceresoli4f63acd2011-05-11 03:59:56 +0000503 status_led_set(STATUS_LED_RED, STATUS_LED_ON);
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200504#endif /* CONFIG_SYS_FAULT_ECHO_LINK_DOWN, ... */
wdenkfc3e2162003-10-08 22:33:00 +0000505#endif /* CONFIG_MII, ... */
wdenk2d966952002-10-31 22:12:35 +0000506
507 /*
508 * Main packet reception loop. Loop receiving packets until
wdenk59acc292005-04-03 14:18:51 +0000509 * someone sets `NetState' to a state that terminates.
wdenk2d966952002-10-31 22:12:35 +0000510 */
511 for (;;) {
512 WATCHDOG_RESET();
513#ifdef CONFIG_SHOW_ACTIVITY
Joe Hershberger48522bb2012-05-15 08:59:08 +0000514 show_activity(1);
wdenk2d966952002-10-31 22:12:35 +0000515#endif
516 /*
517 * Check the ethernet for a new packet. The ethernet
518 * receive routine will process it.
519 */
Guennadi Liakhovetski40cb90e2008-04-03 17:04:19 +0200520 eth_rx();
wdenk2d966952002-10-31 22:12:35 +0000521
522 /*
523 * Abort if ctrl-c was pressed.
524 */
525 if (ctrlc()) {
wdenk8bde7f72003-06-27 21:31:46 +0000526 eth_halt();
Luca Ceresoli4f63acd2011-05-11 03:59:56 +0000527 puts("\nAbort\n");
Simon Glass4793ee62011-10-24 18:00:01 +0000528 goto done;
wdenk2d966952002-10-31 22:12:35 +0000529 }
530
wdenk73a8b272003-06-05 19:27:42 +0000531 ArpTimeoutCheck();
wdenk2d966952002-10-31 22:12:35 +0000532
533 /*
534 * Check for a timeout, and run the timeout handler
535 * if we have one.
536 */
wdenke0ac62d2003-08-17 18:55:18 +0000537 if (timeHandler && ((get_timer(0) - timeStart) > timeDelta)) {
wdenk2d966952002-10-31 22:12:35 +0000538 thand_f *x;
539
Jon Loeliger643d1ab2007-07-09 17:45:14 -0500540#if defined(CONFIG_MII) || defined(CONFIG_CMD_MII)
Luca Ceresoli4f63acd2011-05-11 03:59:56 +0000541#if defined(CONFIG_SYS_FAULT_ECHO_LINK_DOWN) && \
542 defined(CONFIG_STATUS_LED) && \
543 defined(STATUS_LED_RED)
wdenkfc3e2162003-10-08 22:33:00 +0000544 /*
wdenk42d1f032003-10-15 23:53:47 +0000545 * Echo the inverted link state to the fault LED.
wdenkfc3e2162003-10-08 22:33:00 +0000546 */
Luca Ceresoli4f63acd2011-05-11 03:59:56 +0000547 if (miiphy_link(eth_get_dev()->name,
Luca Ceresoli3e38e422011-05-11 03:59:54 +0000548 CONFIG_SYS_FAULT_MII_ADDR)) {
Luca Ceresoli4f63acd2011-05-11 03:59:56 +0000549 status_led_set(STATUS_LED_RED, STATUS_LED_OFF);
wdenkfc3e2162003-10-08 22:33:00 +0000550 } else {
Luca Ceresoli4f63acd2011-05-11 03:59:56 +0000551 status_led_set(STATUS_LED_RED, STATUS_LED_ON);
wdenkfc3e2162003-10-08 22:33:00 +0000552 }
Luca Ceresoli4f63acd2011-05-11 03:59:56 +0000553#endif /* CONFIG_SYS_FAULT_ECHO_LINK_DOWN, ... */
wdenkfc3e2162003-10-08 22:33:00 +0000554#endif /* CONFIG_MII, ... */
wdenk2d966952002-10-31 22:12:35 +0000555 x = timeHandler;
556 timeHandler = (thand_f *)0;
557 (*x)();
558 }
559
560
561 switch (NetState) {
562
563 case NETLOOP_RESTART:
wdenk2d966952002-10-31 22:12:35 +0000564 NetRestarted = 1;
wdenk2d966952002-10-31 22:12:35 +0000565 goto restart;
566
567 case NETLOOP_SUCCESS:
568 if (NetBootFileXferSize > 0) {
Wolfgang Denkf34024d2007-09-12 00:48:57 +0200569 char buf[20];
wdenk2d966952002-10-31 22:12:35 +0000570 printf("Bytes transferred = %ld (%lx hex)\n",
571 NetBootFileXferSize,
572 NetBootFileXferSize);
Wolfgang Denkf34024d2007-09-12 00:48:57 +0200573 sprintf(buf, "%lX", NetBootFileXferSize);
wdenk2d966952002-10-31 22:12:35 +0000574 setenv("filesize", buf);
wdenka3d991b2004-04-15 21:48:45 +0000575
576 sprintf(buf, "%lX", (unsigned long)load_addr);
577 setenv("fileaddr", buf);
wdenk2d966952002-10-31 22:12:35 +0000578 }
579 eth_halt();
Simon Glass4793ee62011-10-24 18:00:01 +0000580 ret = NetBootFileXferSize;
581 goto done;
wdenk2d966952002-10-31 22:12:35 +0000582
583 case NETLOOP_FAIL:
Simon Glass4793ee62011-10-24 18:00:01 +0000584 goto done;
wdenk2d966952002-10-31 22:12:35 +0000585 }
586 }
Simon Glass4793ee62011-10-24 18:00:01 +0000587
588done:
Simon Glass39bccd22011-10-26 14:18:38 +0000589#ifdef CONFIG_CMD_TFTPPUT
Simon Glass4793ee62011-10-24 18:00:01 +0000590 /* Clear out the handlers */
591 NetSetHandler(NULL);
592 net_set_icmp_handler(NULL);
Simon Glass39bccd22011-10-26 14:18:38 +0000593#endif
Simon Glass4793ee62011-10-24 18:00:01 +0000594 return ret;
wdenk2d966952002-10-31 22:12:35 +0000595}
596
597/**********************************************************************/
598
599static void
600startAgainTimeout(void)
601{
602 NetState = NETLOOP_RESTART;
603}
604
605static void
Luca Ceresoli03eb1292011-04-18 06:19:50 +0000606startAgainHandler(uchar *pkt, unsigned dest, IPaddr_t sip,
607 unsigned src, unsigned len)
wdenk2d966952002-10-31 22:12:35 +0000608{
609 /* Totally ignore the packet */
610}
611
Luca Ceresoli4f63acd2011-05-11 03:59:56 +0000612void NetStartAgain(void)
wdenk2d966952002-10-31 22:12:35 +0000613{
wdenk6e592382004-04-18 17:39:38 +0000614 char *nretry;
Remy Bohmer67b96e82009-10-28 22:13:39 +0100615 int retry_forever = 0;
616 unsigned long retrycnt = 0;
wdenka3d991b2004-04-15 21:48:45 +0000617
Remy Bohmer67b96e82009-10-28 22:13:39 +0100618 nretry = getenv("netretry");
619 if (nretry) {
620 if (!strcmp(nretry, "yes"))
621 retry_forever = 1;
622 else if (!strcmp(nretry, "no"))
623 retrycnt = 0;
624 else if (!strcmp(nretry, "once"))
625 retrycnt = 1;
626 else
627 retrycnt = simple_strtoul(nretry, NULL, 0);
628 } else
629 retry_forever = 1;
630
631 if ((!retry_forever) && (NetTryCount >= retrycnt)) {
632 eth_halt();
wdenka3d991b2004-04-15 21:48:45 +0000633 NetState = NETLOOP_FAIL;
634 return;
635 }
Remy Bohmer67b96e82009-10-28 22:13:39 +0100636
637 NetTryCount++;
638
Luca Ceresoli4f63acd2011-05-11 03:59:56 +0000639 eth_halt();
Matthias Fuchs8b0c5c12007-12-27 16:58:41 +0100640#if !defined(CONFIG_NET_DO_NOT_TRY_ANOTHER)
Luca Ceresoli4f63acd2011-05-11 03:59:56 +0000641 eth_try_another(!NetRestarted);
Matthias Fuchs8b0c5c12007-12-27 16:58:41 +0100642#endif
Luca Ceresoli4f63acd2011-05-11 03:59:56 +0000643 eth_init(gd->bd);
wdenk6e592382004-04-18 17:39:38 +0000644 if (NetRestartWrap) {
wdenk2d966952002-10-31 22:12:35 +0000645 NetRestartWrap = 0;
Remy Bohmer67b96e82009-10-28 22:13:39 +0100646 if (NetDevExists) {
Luca Ceresoli4f63acd2011-05-11 03:59:56 +0000647 NetSetTimeout(10000UL, startAgainTimeout);
648 NetSetHandler(startAgainHandler);
wdenk6e592382004-04-18 17:39:38 +0000649 } else {
wdenk2d966952002-10-31 22:12:35 +0000650 NetState = NETLOOP_FAIL;
651 }
wdenk6e592382004-04-18 17:39:38 +0000652 } else {
wdenk2d966952002-10-31 22:12:35 +0000653 NetState = NETLOOP_RESTART;
654 }
wdenk2d966952002-10-31 22:12:35 +0000655}
656
657/**********************************************************************/
658/*
659 * Miscelaneous bits.
660 */
661
662void
Luca Ceresoli6b147d12011-05-04 02:40:44 +0000663NetSetHandler(rxhand_f *f)
wdenk2d966952002-10-31 22:12:35 +0000664{
665 packetHandler = f;
666}
667
Simon Glass39bccd22011-10-26 14:18:38 +0000668#ifdef CONFIG_CMD_TFTPPUT
Simon Glass4793ee62011-10-24 18:00:01 +0000669void net_set_icmp_handler(rxhand_icmp_f *f)
670{
671 packet_icmp_handler = f;
672}
Simon Glass39bccd22011-10-26 14:18:38 +0000673#endif
wdenk2d966952002-10-31 22:12:35 +0000674
675void
Luca Ceresoli6b147d12011-05-04 02:40:44 +0000676NetSetTimeout(ulong iv, thand_f *f)
wdenk2d966952002-10-31 22:12:35 +0000677{
678 if (iv == 0) {
679 timeHandler = (thand_f *)0;
680 } else {
681 timeHandler = f;
wdenke0ac62d2003-08-17 18:55:18 +0000682 timeStart = get_timer(0);
683 timeDelta = iv;
wdenk2d966952002-10-31 22:12:35 +0000684 }
685}
686
687
688void
Joe Hershbergerdb288a92012-05-15 08:59:04 +0000689NetSendPacket(uchar *pkt, int len)
wdenk2d966952002-10-31 22:12:35 +0000690{
691 (void) eth_send(pkt, len);
692}
693
wdenk73a8b272003-06-05 19:27:42 +0000694int
695NetSendUDPPacket(uchar *ether, IPaddr_t dest, int dport, int sport, int len)
696{
wdenka3d991b2004-04-15 21:48:45 +0000697 uchar *pkt;
698
wdenk73a8b272003-06-05 19:27:42 +0000699 /* convert to new style broadcast */
700 if (dest == 0)
701 dest = 0xFFFFFFFF;
wdenk2d966952002-10-31 22:12:35 +0000702
wdenk73a8b272003-06-05 19:27:42 +0000703 /* if broadcast, make the ether address a broadcast and don't do ARP */
704 if (dest == 0xFFFFFFFF)
705 ether = NetBcastAddr;
706
Luca Ceresoli3e38e422011-05-11 03:59:54 +0000707 /*
708 * if MAC address was not discovered yet, save the packet and do
709 * an ARP request
710 */
wdenk73a8b272003-06-05 19:27:42 +0000711 if (memcmp(ether, NetEtherNullAddr, 6) == 0) {
712
Matthias Weisserea45cb02011-12-03 03:29:44 +0000713 debug("sending ARP for %08x\n", dest);
Robin Getz0ebf04c2009-07-23 03:01:03 -0400714
wdenk73a8b272003-06-05 19:27:42 +0000715 NetArpWaitPacketIP = dest;
716 NetArpWaitPacketMAC = ether;
wdenka3d991b2004-04-15 21:48:45 +0000717
718 pkt = NetArpWaitTxPacket;
Luca Ceresoli4f63acd2011-05-11 03:59:56 +0000719 pkt += NetSetEther(pkt, NetArpWaitPacketMAC, PROT_IP);
wdenka3d991b2004-04-15 21:48:45 +0000720
Luca Ceresoli4f63acd2011-05-11 03:59:56 +0000721 NetSetIP(pkt, dest, dport, sport, len);
Luca Ceresoli3e38e422011-05-11 03:59:54 +0000722 memcpy(pkt + IP_HDR_SIZE, (uchar *)NetTxPacket +
723 (pkt - (uchar *)NetArpWaitTxPacket) + IP_HDR_SIZE, len);
wdenk73a8b272003-06-05 19:27:42 +0000724
725 /* size of the waiting packet */
Luca Ceresoli3e38e422011-05-11 03:59:54 +0000726 NetArpWaitTxPacketSize = (pkt - NetArpWaitTxPacket) +
727 IP_HDR_SIZE + len;
wdenk73a8b272003-06-05 19:27:42 +0000728
729 /* and do the ARP request */
730 NetArpWaitTry = 1;
731 NetArpWaitTimerStart = get_timer(0);
732 ArpRequest();
733 return 1; /* waiting */
734 }
735
Matthias Weisserea45cb02011-12-03 03:29:44 +0000736 debug("sending UDP to %08x/%pM\n", dest, ether);
wdenk73a8b272003-06-05 19:27:42 +0000737
wdenka3d991b2004-04-15 21:48:45 +0000738 pkt = (uchar *)NetTxPacket;
Luca Ceresoli4f63acd2011-05-11 03:59:56 +0000739 pkt += NetSetEther(pkt, ether, PROT_IP);
740 NetSetIP(pkt, dest, dport, sport, len);
wdenka3d991b2004-04-15 21:48:45 +0000741 (void) eth_send(NetTxPacket, (pkt - NetTxPacket) + IP_HDR_SIZE + len);
wdenk73a8b272003-06-05 19:27:42 +0000742
wdenk6e592382004-04-18 17:39:38 +0000743 return 0; /* transmitted */
wdenk73a8b272003-06-05 19:27:42 +0000744}
745
Jon Loeliger643d1ab2007-07-09 17:45:14 -0500746#if defined(CONFIG_CMD_PING)
wdenk73a8b272003-06-05 19:27:42 +0000747static ushort PingSeqNo;
748
749int PingSend(void)
750{
751 static uchar mac[6];
Joe Hershbergerdb288a92012-05-15 08:59:04 +0000752 IP_t *ip;
753 ushort *s;
wdenka3d991b2004-04-15 21:48:45 +0000754 uchar *pkt;
wdenk73a8b272003-06-05 19:27:42 +0000755
756 /* XXX always send arp request */
757
758 memcpy(mac, NetEtherNullAddr, 6);
759
Matthias Weisserea45cb02011-12-03 03:29:44 +0000760 debug("sending ARP for %08x\n", NetPingIP);
wdenk73a8b272003-06-05 19:27:42 +0000761
762 NetArpWaitPacketIP = NetPingIP;
763 NetArpWaitPacketMAC = mac;
764
wdenka3d991b2004-04-15 21:48:45 +0000765 pkt = NetArpWaitTxPacket;
766 pkt += NetSetEther(pkt, mac, PROT_IP);
wdenk73a8b272003-06-05 19:27:42 +0000767
Joe Hershbergerdb288a92012-05-15 08:59:04 +0000768 ip = (IP_t *)pkt;
wdenk73a8b272003-06-05 19:27:42 +0000769
770 /*
Luca Ceresoli3e38e422011-05-11 03:59:54 +0000771 * Construct an IP and ICMP header.
772 * (need to set no fragment bit - XXX)
wdenk73a8b272003-06-05 19:27:42 +0000773 */
Luca Ceresoli3e38e422011-05-11 03:59:54 +0000774 /* IP_HDR_SIZE / 4 (not including UDP) */
775 ip->ip_hl_v = 0x45;
wdenk73a8b272003-06-05 19:27:42 +0000776 ip->ip_tos = 0;
777 ip->ip_len = htons(IP_HDR_SIZE_NO_UDP + 8);
778 ip->ip_id = htons(NetIPID++);
Peter Tysere0c07b82008-12-01 16:26:20 -0600779 ip->ip_off = htons(IP_FLAGS_DFRAG); /* Don't fragment */
wdenk73a8b272003-06-05 19:27:42 +0000780 ip->ip_ttl = 255;
781 ip->ip_p = 0x01; /* ICMP */
782 ip->ip_sum = 0;
Luca Ceresoli3e38e422011-05-11 03:59:54 +0000783 /* already in network byte order */
Luca Ceresoli6b147d12011-05-04 02:40:44 +0000784 NetCopyIP((void *)&ip->ip_src, &NetOurIP);
Luca Ceresoli3e38e422011-05-11 03:59:54 +0000785 /* - "" - */
Luca Ceresoli6b147d12011-05-04 02:40:44 +0000786 NetCopyIP((void *)&ip->ip_dst, &NetPingIP);
wdenk73a8b272003-06-05 19:27:42 +0000787 ip->ip_sum = ~NetCksum((uchar *)ip, IP_HDR_SIZE_NO_UDP / 2);
788
789 s = &ip->udp_src; /* XXX ICMP starts here */
790 s[0] = htons(0x0800); /* echo-request, code */
791 s[1] = 0; /* checksum */
Wolfgang Denk53677ef2008-05-20 16:00:29 +0200792 s[2] = 0; /* identifier */
wdenk73a8b272003-06-05 19:27:42 +0000793 s[3] = htons(PingSeqNo++); /* sequence number */
794 s[1] = ~NetCksum((uchar *)s, 8/2);
795
796 /* size of the waiting packet */
Luca Ceresoli3e38e422011-05-11 03:59:54 +0000797 NetArpWaitTxPacketSize =
798 (pkt - NetArpWaitTxPacket) + IP_HDR_SIZE_NO_UDP + 8;
wdenk73a8b272003-06-05 19:27:42 +0000799
800 /* and do the ARP request */
801 NetArpWaitTry = 1;
802 NetArpWaitTimerStart = get_timer(0);
803 ArpRequest();
804 return 1; /* waiting */
805}
806
807static void
Luca Ceresoli4f63acd2011-05-11 03:59:56 +0000808PingTimeout(void)
wdenk73a8b272003-06-05 19:27:42 +0000809{
810 eth_halt();
811 NetState = NETLOOP_FAIL; /* we did not get the reply */
812}
813
814static void
Luca Ceresoli03eb1292011-04-18 06:19:50 +0000815PingHandler(uchar *pkt, unsigned dest, IPaddr_t sip, unsigned src,
816 unsigned len)
wdenk73a8b272003-06-05 19:27:42 +0000817{
Luca Ceresoli03eb1292011-04-18 06:19:50 +0000818 if (sip != NetPingIP)
wdenk73a8b272003-06-05 19:27:42 +0000819 return;
820
821 NetState = NETLOOP_SUCCESS;
822}
823
824static void PingStart(void)
825{
Luca Ceresoli4f63acd2011-05-11 03:59:56 +0000826 printf("Using %s device\n", eth_get_name());
Luca Ceresoli4f63acd2011-05-11 03:59:56 +0000827 NetSetTimeout(10000UL, PingTimeout);
828 NetSetHandler(PingHandler);
wdenk73a8b272003-06-05 19:27:42 +0000829
830 PingSend();
831}
Jon Loeliger610f2e92007-07-10 11:05:02 -0500832#endif
wdenk2d966952002-10-31 22:12:35 +0000833
Alessandro Rubini5cfaa4e2009-08-07 13:58:56 +0200834#ifdef CONFIG_IP_DEFRAG
835/*
836 * This function collects fragments in a single packet, according
837 * to the algorithm in RFC815. It returns NULL or the pointer to
838 * a complete packet, in static storage
839 */
840#ifndef CONFIG_NET_MAXDEFRAG
841#define CONFIG_NET_MAXDEFRAG 16384
842#endif
843/*
844 * MAXDEFRAG, above, is chosen in the config file and is real data
845 * so we need to add the NFS overhead, which is more than TFTP.
846 * To use sizeof in the internal unnamed structures, we need a real
847 * instance (can't do "sizeof(struct rpc_t.u.reply))", unfortunately).
848 * The compiler doesn't complain nor allocates the actual structure
849 */
850static struct rpc_t rpc_specimen;
851#define IP_PKTSIZE (CONFIG_NET_MAXDEFRAG + sizeof(rpc_specimen.u.reply))
852
853#define IP_MAXUDP (IP_PKTSIZE - IP_HDR_SIZE_NO_UDP)
854
855/*
856 * this is the packet being assembled, either data or frag control.
857 * Fragments go by 8 bytes, so this union must be 8 bytes long
858 */
859struct hole {
860 /* first_byte is address of this structure */
861 u16 last_byte; /* last byte in this hole + 1 (begin of next hole) */
862 u16 next_hole; /* index of next (in 8-b blocks), 0 == none */
863 u16 prev_hole; /* index of prev, 0 == none */
864 u16 unused;
865};
866
867static IP_t *__NetDefragment(IP_t *ip, int *lenp)
868{
Joe Hershberger48522bb2012-05-15 08:59:08 +0000869 static uchar pkt_buff[IP_PKTSIZE] __aligned(PKTALIGN);
Alessandro Rubini5cfaa4e2009-08-07 13:58:56 +0200870 static u16 first_hole, total_len;
871 struct hole *payload, *thisfrag, *h, *newh;
872 IP_t *localip = (IP_t *)pkt_buff;
873 uchar *indata = (uchar *)ip;
874 int offset8, start, len, done = 0;
875 u16 ip_off = ntohs(ip->ip_off);
876
877 /* payload starts after IP header, this fragment is in there */
878 payload = (struct hole *)(pkt_buff + IP_HDR_SIZE_NO_UDP);
879 offset8 = (ip_off & IP_OFFS);
880 thisfrag = payload + offset8;
881 start = offset8 * 8;
882 len = ntohs(ip->ip_len) - IP_HDR_SIZE_NO_UDP;
883
884 if (start + len > IP_MAXUDP) /* fragment extends too far */
885 return NULL;
886
887 if (!total_len || localip->ip_id != ip->ip_id) {
888 /* new (or different) packet, reset structs */
889 total_len = 0xffff;
890 payload[0].last_byte = ~0;
891 payload[0].next_hole = 0;
892 payload[0].prev_hole = 0;
893 first_hole = 0;
894 /* any IP header will work, copy the first we received */
895 memcpy(localip, ip, IP_HDR_SIZE_NO_UDP);
896 }
897
898 /*
899 * What follows is the reassembly algorithm. We use the payload
900 * array as a linked list of hole descriptors, as each hole starts
901 * at a multiple of 8 bytes. However, last byte can be whatever value,
902 * so it is represented as byte count, not as 8-byte blocks.
903 */
904
905 h = payload + first_hole;
906 while (h->last_byte < start) {
907 if (!h->next_hole) {
908 /* no hole that far away */
909 return NULL;
910 }
911 h = payload + h->next_hole;
912 }
913
Fillod Stephanee397e592010-06-11 19:26:43 +0200914 /* last fragment may be 1..7 bytes, the "+7" forces acceptance */
915 if (offset8 + ((len + 7) / 8) <= h - payload) {
Alessandro Rubini5cfaa4e2009-08-07 13:58:56 +0200916 /* no overlap with holes (dup fragment?) */
917 return NULL;
918 }
919
920 if (!(ip_off & IP_FLAGS_MFRAG)) {
921 /* no more fragmentss: truncate this (last) hole */
922 total_len = start + len;
923 h->last_byte = start + len;
924 }
925
926 /*
927 * There is some overlap: fix the hole list. This code doesn't
928 * deal with a fragment that overlaps with two different holes
929 * (thus being a superset of a previously-received fragment).
930 */
931
Luca Ceresoli4f63acd2011-05-11 03:59:56 +0000932 if ((h >= thisfrag) && (h->last_byte <= start + len)) {
Alessandro Rubini5cfaa4e2009-08-07 13:58:56 +0200933 /* complete overlap with hole: remove hole */
934 if (!h->prev_hole && !h->next_hole) {
935 /* last remaining hole */
936 done = 1;
937 } else if (!h->prev_hole) {
938 /* first hole */
939 first_hole = h->next_hole;
940 payload[h->next_hole].prev_hole = 0;
941 } else if (!h->next_hole) {
942 /* last hole */
943 payload[h->prev_hole].next_hole = 0;
944 } else {
945 /* in the middle of the list */
946 payload[h->next_hole].prev_hole = h->prev_hole;
947 payload[h->prev_hole].next_hole = h->next_hole;
948 }
949
950 } else if (h->last_byte <= start + len) {
951 /* overlaps with final part of the hole: shorten this hole */
952 h->last_byte = start;
953
954 } else if (h >= thisfrag) {
955 /* overlaps with initial part of the hole: move this hole */
956 newh = thisfrag + (len / 8);
957 *newh = *h;
958 h = newh;
959 if (h->next_hole)
960 payload[h->next_hole].prev_hole = (h - payload);
961 if (h->prev_hole)
962 payload[h->prev_hole].next_hole = (h - payload);
963 else
964 first_hole = (h - payload);
965
966 } else {
967 /* fragment sits in the middle: split the hole */
968 newh = thisfrag + (len / 8);
969 *newh = *h;
970 h->last_byte = start;
971 h->next_hole = (newh - payload);
972 newh->prev_hole = (h - payload);
973 if (newh->next_hole)
974 payload[newh->next_hole].prev_hole = (newh - payload);
975 }
976
977 /* finally copy this fragment and possibly return whole packet */
978 memcpy((uchar *)thisfrag, indata + IP_HDR_SIZE_NO_UDP, len);
979 if (!done)
980 return NULL;
981
982 localip->ip_len = htons(total_len);
983 *lenp = total_len + IP_HDR_SIZE_NO_UDP;
984 return localip;
985}
986
987static inline IP_t *NetDefragment(IP_t *ip, int *lenp)
988{
989 u16 ip_off = ntohs(ip->ip_off);
990 if (!(ip_off & (IP_OFFS | IP_FLAGS_MFRAG)))
991 return ip; /* not a fragment */
992 return __NetDefragment(ip, lenp);
993}
994
995#else /* !CONFIG_IP_DEFRAG */
996
997static inline IP_t *NetDefragment(IP_t *ip, int *lenp)
998{
999 u16 ip_off = ntohs(ip->ip_off);
1000 if (!(ip_off & (IP_OFFS | IP_FLAGS_MFRAG)))
1001 return ip; /* not a fragment */
1002 return NULL;
1003}
1004#endif
wdenka3d991b2004-04-15 21:48:45 +00001005
Simon Glass8f79bb12011-10-24 18:00:00 +00001006/**
1007 * Receive an ICMP packet. We deal with REDIRECT and PING here, and silently
1008 * drop others.
1009 *
1010 * @parma ip IP packet containing the ICMP
1011 */
1012static void receive_icmp(IP_t *ip, int len, IPaddr_t src_ip, Ethernet_t *et)
1013{
1014 ICMP_t *icmph = (ICMP_t *)&ip->udp_src;
1015
1016 switch (icmph->type) {
1017 case ICMP_REDIRECT:
1018 if (icmph->code != ICMP_REDIR_HOST)
1019 return;
1020 printf(" ICMP Host Redirect to %pI4 ",
1021 &icmph->un.gateway);
1022 break;
1023#if defined(CONFIG_CMD_PING)
1024 case ICMP_ECHO_REPLY:
1025 /*
1026 * IP header OK. Pass the packet to the
1027 * current handler.
1028 */
1029 /*
1030 * XXX point to ip packet - should this use
1031 * packet_icmp_handler?
1032 */
1033 (*packetHandler)((uchar *)ip, 0, src_ip, 0, 0);
1034 break;
1035 case ICMP_ECHO_REQUEST:
1036 debug("Got ICMP ECHO REQUEST, return %d bytes\n",
1037 ETHER_HDR_SIZE + len);
1038
1039 memcpy(&et->et_dest[0], &et->et_src[0], 6);
1040 memcpy(&et->et_src[0], NetOurEther, 6);
1041
1042 ip->ip_sum = 0;
1043 ip->ip_off = 0;
1044 NetCopyIP((void *)&ip->ip_dst, &ip->ip_src);
1045 NetCopyIP((void *)&ip->ip_src, &NetOurIP);
1046 ip->ip_sum = ~NetCksum((uchar *)ip,
1047 IP_HDR_SIZE_NO_UDP >> 1);
1048
1049 icmph->type = ICMP_ECHO_REPLY;
1050 icmph->checksum = 0;
1051 icmph->checksum = ~NetCksum((uchar *)icmph,
1052 (len - IP_HDR_SIZE_NO_UDP) >> 1);
1053 (void) eth_send((uchar *)et,
1054 ETHER_HDR_SIZE + len);
1055 break;
1056#endif
1057 default:
Simon Glass39bccd22011-10-26 14:18:38 +00001058#ifdef CONFIG_CMD_TFTPPUT
Simon Glass4793ee62011-10-24 18:00:01 +00001059 if (packet_icmp_handler)
1060 packet_icmp_handler(icmph->type, icmph->code,
1061 ntohs(ip->udp_dst), src_ip, ntohs(ip->udp_src),
1062 icmph->un.data, ntohs(ip->udp_len));
Simon Glass39bccd22011-10-26 14:18:38 +00001063#endif
Simon Glass8f79bb12011-10-24 18:00:00 +00001064 break;
1065 }
1066}
1067
wdenk2d966952002-10-31 22:12:35 +00001068void
Joe Hershbergerdb288a92012-05-15 08:59:04 +00001069NetReceive(uchar *inpkt, int len)
wdenk2d966952002-10-31 22:12:35 +00001070{
1071 Ethernet_t *et;
1072 IP_t *ip;
1073 ARP_t *arp;
1074 IPaddr_t tmp;
Luca Ceresoli03eb1292011-04-18 06:19:50 +00001075 IPaddr_t src_ip;
wdenk2d966952002-10-31 22:12:35 +00001076 int x;
wdenka3d991b2004-04-15 21:48:45 +00001077 uchar *pkt;
Jon Loeliger643d1ab2007-07-09 17:45:14 -05001078#if defined(CONFIG_CMD_CDP)
wdenka3d991b2004-04-15 21:48:45 +00001079 int iscdp;
1080#endif
1081 ushort cti = 0, vlanid = VLAN_NONE, myvlanid, mynvlanid;
wdenk2d966952002-10-31 22:12:35 +00001082
Robin Getz0ebf04c2009-07-23 03:01:03 -04001083 debug("packet received\n");
wdenka3d991b2004-04-15 21:48:45 +00001084
Mike Frysingerd9bec9f2009-07-18 21:04:08 -04001085 NetRxPacket = inpkt;
1086 NetRxPacketLen = len;
wdenka3d991b2004-04-15 21:48:45 +00001087 et = (Ethernet_t *)inpkt;
1088
1089 /* too small packet? */
1090 if (len < ETHER_HDR_SIZE)
1091 return;
1092
Rafal Jaworowskif85b6072007-12-27 18:19:02 +01001093#ifdef CONFIG_API
1094 if (push_packet) {
1095 (*push_packet)(inpkt, len);
1096 return;
1097 }
1098#endif
1099
Jon Loeliger643d1ab2007-07-09 17:45:14 -05001100#if defined(CONFIG_CMD_CDP)
wdenka3d991b2004-04-15 21:48:45 +00001101 /* keep track if packet is CDP */
1102 iscdp = memcmp(et->et_dest, NetCDPAddr, 6) == 0;
1103#endif
1104
1105 myvlanid = ntohs(NetOurVLAN);
1106 if (myvlanid == (ushort)-1)
1107 myvlanid = VLAN_NONE;
1108 mynvlanid = ntohs(NetOurNativeVLAN);
1109 if (mynvlanid == (ushort)-1)
1110 mynvlanid = VLAN_NONE;
wdenk2d966952002-10-31 22:12:35 +00001111
1112 x = ntohs(et->et_protlen);
1113
Robin Getz0ebf04c2009-07-23 03:01:03 -04001114 debug("packet received\n");
wdenka3d991b2004-04-15 21:48:45 +00001115
wdenk2d966952002-10-31 22:12:35 +00001116 if (x < 1514) {
1117 /*
1118 * Got a 802 packet. Check the other protocol field.
1119 */
1120 x = ntohs(et->et_prot);
wdenka3d991b2004-04-15 21:48:45 +00001121
1122 ip = (IP_t *)(inpkt + E802_HDR_SIZE);
wdenk2d966952002-10-31 22:12:35 +00001123 len -= E802_HDR_SIZE;
wdenka3d991b2004-04-15 21:48:45 +00001124
1125 } else if (x != PROT_VLAN) { /* normal packet */
1126 ip = (IP_t *)(inpkt + ETHER_HDR_SIZE);
wdenk2d966952002-10-31 22:12:35 +00001127 len -= ETHER_HDR_SIZE;
wdenka3d991b2004-04-15 21:48:45 +00001128
1129 } else { /* VLAN packet */
1130 VLAN_Ethernet_t *vet = (VLAN_Ethernet_t *)et;
1131
Robin Getz0ebf04c2009-07-23 03:01:03 -04001132 debug("VLAN packet received\n");
1133
wdenka3d991b2004-04-15 21:48:45 +00001134 /* too small packet? */
1135 if (len < VLAN_ETHER_HDR_SIZE)
1136 return;
1137
1138 /* if no VLAN active */
1139 if ((ntohs(NetOurVLAN) & VLAN_IDMASK) == VLAN_NONE
Jon Loeliger643d1ab2007-07-09 17:45:14 -05001140#if defined(CONFIG_CMD_CDP)
wdenka3d991b2004-04-15 21:48:45 +00001141 && iscdp == 0
1142#endif
1143 )
1144 return;
1145
1146 cti = ntohs(vet->vet_tag);
1147 vlanid = cti & VLAN_IDMASK;
1148 x = ntohs(vet->vet_type);
1149
1150 ip = (IP_t *)(inpkt + VLAN_ETHER_HDR_SIZE);
1151 len -= VLAN_ETHER_HDR_SIZE;
wdenk2d966952002-10-31 22:12:35 +00001152 }
1153
Robin Getz0ebf04c2009-07-23 03:01:03 -04001154 debug("Receive from protocol 0x%x\n", x);
wdenk2d966952002-10-31 22:12:35 +00001155
Jon Loeliger643d1ab2007-07-09 17:45:14 -05001156#if defined(CONFIG_CMD_CDP)
wdenka3d991b2004-04-15 21:48:45 +00001157 if (iscdp) {
1158 CDPHandler((uchar *)ip, len);
1159 return;
1160 }
1161#endif
1162
1163 if ((myvlanid & VLAN_IDMASK) != VLAN_NONE) {
1164 if (vlanid == VLAN_NONE)
1165 vlanid = (mynvlanid & VLAN_IDMASK);
1166 /* not matched? */
1167 if (vlanid != (myvlanid & VLAN_IDMASK))
1168 return;
1169 }
1170
wdenk2d966952002-10-31 22:12:35 +00001171 switch (x) {
1172
1173 case PROT_ARP:
1174 /*
1175 * We have to deal with two types of ARP packets:
wdenk8bde7f72003-06-27 21:31:46 +00001176 * - REQUEST packets will be answered by sending our
1177 * IP address - if we know it.
1178 * - REPLY packates are expected only after we asked
1179 * for the TFTP server's or the gateway's ethernet
1180 * address; so if we receive such a packet, we set
1181 * the server ethernet address
wdenk2d966952002-10-31 22:12:35 +00001182 */
Robin Getz0ebf04c2009-07-23 03:01:03 -04001183 debug("Got ARP\n");
1184
wdenk2d966952002-10-31 22:12:35 +00001185 arp = (ARP_t *)ip;
1186 if (len < ARP_HDR_SIZE) {
1187 printf("bad length %d < %d\n", len, ARP_HDR_SIZE);
1188 return;
1189 }
Luca Ceresolid3c65b02011-05-04 02:40:43 +00001190 if (ntohs(arp->ar_hrd) != ARP_ETHER)
wdenk2d966952002-10-31 22:12:35 +00001191 return;
Luca Ceresolid3c65b02011-05-04 02:40:43 +00001192 if (ntohs(arp->ar_pro) != PROT_IP)
wdenk2d966952002-10-31 22:12:35 +00001193 return;
Luca Ceresolid3c65b02011-05-04 02:40:43 +00001194 if (arp->ar_hln != 6)
wdenk2d966952002-10-31 22:12:35 +00001195 return;
Luca Ceresolid3c65b02011-05-04 02:40:43 +00001196 if (arp->ar_pln != 4)
wdenk2d966952002-10-31 22:12:35 +00001197 return;
wdenk2d966952002-10-31 22:12:35 +00001198
Luca Ceresolid3c65b02011-05-04 02:40:43 +00001199 if (NetOurIP == 0)
wdenk2d966952002-10-31 22:12:35 +00001200 return;
wdenk2d966952002-10-31 22:12:35 +00001201
Luca Ceresolid3c65b02011-05-04 02:40:43 +00001202 if (NetReadIP(&arp->ar_data[16]) != NetOurIP)
wdenk2d966952002-10-31 22:12:35 +00001203 return;
wdenk2d966952002-10-31 22:12:35 +00001204
1205 switch (ntohs(arp->ar_op)) {
Luca Ceresoli3e38e422011-05-11 03:59:54 +00001206 case ARPOP_REQUEST:
1207 /* reply with our IP address */
Robin Getz0ebf04c2009-07-23 03:01:03 -04001208 debug("Got ARP REQUEST, return our IP\n");
wdenka3d991b2004-04-15 21:48:45 +00001209 pkt = (uchar *)et;
1210 pkt += NetSetEther(pkt, et->et_src, PROT_ARP);
wdenk2d966952002-10-31 22:12:35 +00001211 arp->ar_op = htons(ARPOP_REPLY);
Luca Ceresoli4f63acd2011-05-11 03:59:56 +00001212 memcpy(&arp->ar_data[10], &arp->ar_data[0], 6);
wdenk2d966952002-10-31 22:12:35 +00001213 NetCopyIP(&arp->ar_data[16], &arp->ar_data[6]);
Luca Ceresoli4f63acd2011-05-11 03:59:56 +00001214 memcpy(&arp->ar_data[0], NetOurEther, 6);
1215 NetCopyIP(&arp->ar_data[6], &NetOurIP);
Luca Ceresoli3e38e422011-05-11 03:59:54 +00001216 (void) eth_send((uchar *)et,
1217 (pkt - (uchar *)et) + ARP_HDR_SIZE);
wdenk2d966952002-10-31 22:12:35 +00001218 return;
wdenk73a8b272003-06-05 19:27:42 +00001219
1220 case ARPOP_REPLY: /* arp reply */
1221 /* are we waiting for a reply */
1222 if (!NetArpWaitPacketIP || !NetArpWaitPacketMAC)
1223 break;
Robin Getz97cfe862009-07-21 12:15:28 -04001224
1225#ifdef CONFIG_KEEP_SERVERADDR
1226 if (NetServerIP == NetArpWaitPacketIP) {
1227 char buf[20];
1228 sprintf(buf, "%pM", arp->ar_data);
1229 setenv("serveraddr", buf);
1230 }
1231#endif
1232
Robin Getz0ebf04c2009-07-23 03:01:03 -04001233 debug("Got ARP REPLY, set server/gtwy eth addr (%pM)\n",
Mike Frysinger95823ca2009-02-11 18:23:48 -05001234 arp->ar_data);
wdenk73a8b272003-06-05 19:27:42 +00001235
1236 tmp = NetReadIP(&arp->ar_data[6]);
1237
1238 /* matched waiting packet's address */
1239 if (tmp == NetArpWaitReplyIP) {
Robin Getz0ebf04c2009-07-23 03:01:03 -04001240 debug("Got it\n");
Joe Hershberger48522bb2012-05-15 08:59:08 +00001241
wdenk73a8b272003-06-05 19:27:42 +00001242 /* save address for later use */
Luca Ceresoli3e38e422011-05-11 03:59:54 +00001243 memcpy(NetArpWaitPacketMAC,
1244 &arp->ar_data[0], 6);
wdenk73a8b272003-06-05 19:27:42 +00001245
wdenk68ceb292004-08-02 21:11:11 +00001246#ifdef CONFIG_NETCONSOLE
Luca Ceresoli03eb1292011-04-18 06:19:50 +00001247 (*packetHandler)(0, 0, 0, 0, 0);
wdenk68ceb292004-08-02 21:11:11 +00001248#endif
wdenk73a8b272003-06-05 19:27:42 +00001249 /* modify header, and transmit it */
Joe Hershberger48522bb2012-05-15 08:59:08 +00001250 memcpy(((Ethernet_t *)NetArpWaitTxPacket)->
1251 et_dest, NetArpWaitPacketMAC, 6);
Luca Ceresoli3e38e422011-05-11 03:59:54 +00001252 (void) eth_send(NetArpWaitTxPacket,
1253 NetArpWaitTxPacketSize);
wdenk73a8b272003-06-05 19:27:42 +00001254
1255 /* no arp request pending now */
1256 NetArpWaitPacketIP = 0;
1257 NetArpWaitTxPacketSize = 0;
1258 NetArpWaitPacketMAC = NULL;
1259
1260 }
wdenk2d966952002-10-31 22:12:35 +00001261 return;
1262 default:
Luca Ceresoli3e38e422011-05-11 03:59:54 +00001263 debug("Unexpected ARP opcode 0x%x\n",
1264 ntohs(arp->ar_op));
wdenk2d966952002-10-31 22:12:35 +00001265 return;
1266 }
wdenk289f9322005-01-12 00:15:14 +00001267 break;
wdenk2d966952002-10-31 22:12:35 +00001268
Peter Tyserbf6cb242010-09-30 11:25:48 -05001269#ifdef CONFIG_CMD_RARP
wdenk2d966952002-10-31 22:12:35 +00001270 case PROT_RARP:
Robin Getz0ebf04c2009-07-23 03:01:03 -04001271 debug("Got RARP\n");
wdenk2d966952002-10-31 22:12:35 +00001272 arp = (ARP_t *)ip;
1273 if (len < ARP_HDR_SIZE) {
1274 printf("bad length %d < %d\n", len, ARP_HDR_SIZE);
1275 return;
1276 }
1277
1278 if ((ntohs(arp->ar_op) != RARPOP_REPLY) ||
1279 (ntohs(arp->ar_hrd) != ARP_ETHER) ||
1280 (ntohs(arp->ar_pro) != PROT_IP) ||
1281 (arp->ar_hln != 6) || (arp->ar_pln != 4)) {
1282
Luca Ceresoli4f63acd2011-05-11 03:59:56 +00001283 puts("invalid RARP header\n");
wdenk2d966952002-10-31 22:12:35 +00001284 } else {
Luca Ceresoli4f63acd2011-05-11 03:59:56 +00001285 NetCopyIP(&NetOurIP, &arp->ar_data[16]);
wdenk3d3befa2004-03-14 15:06:13 +00001286 if (NetServerIP == 0)
Luca Ceresoli4f63acd2011-05-11 03:59:56 +00001287 NetCopyIP(&NetServerIP, &arp->ar_data[6]);
1288 memcpy(NetServerEther, &arp->ar_data[0], 6);
wdenk2d966952002-10-31 22:12:35 +00001289
Luca Ceresoli03eb1292011-04-18 06:19:50 +00001290 (*packetHandler)(0, 0, 0, 0, 0);
wdenk2d966952002-10-31 22:12:35 +00001291 }
1292 break;
Peter Tyserbf6cb242010-09-30 11:25:48 -05001293#endif
wdenk2d966952002-10-31 22:12:35 +00001294 case PROT_IP:
Robin Getz0ebf04c2009-07-23 03:01:03 -04001295 debug("Got IP\n");
Alessandro Rubini5cfaa4e2009-08-07 13:58:56 +02001296 /* Before we start poking the header, make sure it is there */
wdenk2d966952002-10-31 22:12:35 +00001297 if (len < IP_HDR_SIZE) {
Robin Getz0ebf04c2009-07-23 03:01:03 -04001298 debug("len bad %d < %lu\n", len, (ulong)IP_HDR_SIZE);
wdenk2d966952002-10-31 22:12:35 +00001299 return;
1300 }
Alessandro Rubini5cfaa4e2009-08-07 13:58:56 +02001301 /* Check the packet length */
wdenk2d966952002-10-31 22:12:35 +00001302 if (len < ntohs(ip->ip_len)) {
1303 printf("len bad %d < %d\n", len, ntohs(ip->ip_len));
1304 return;
1305 }
1306 len = ntohs(ip->ip_len);
Robin Getz0ebf04c2009-07-23 03:01:03 -04001307 debug("len=%d, v=%02x\n", len, ip->ip_hl_v & 0xff);
1308
Alessandro Rubini5cfaa4e2009-08-07 13:58:56 +02001309 /* Can't deal with anything except IPv4 */
Luca Ceresolid3c65b02011-05-04 02:40:43 +00001310 if ((ip->ip_hl_v & 0xf0) != 0x40)
wdenk2d966952002-10-31 22:12:35 +00001311 return;
Alessandro Rubini5cfaa4e2009-08-07 13:58:56 +02001312 /* Can't deal with IP options (headers != 20 bytes) */
Luca Ceresolid3c65b02011-05-04 02:40:43 +00001313 if ((ip->ip_hl_v & 0x0f) > 0x05)
Remy Bohmer6b52cfe2008-06-03 15:48:17 +02001314 return;
Alessandro Rubini5cfaa4e2009-08-07 13:58:56 +02001315 /* Check the Checksum of the header */
wdenk2d966952002-10-31 22:12:35 +00001316 if (!NetCksumOk((uchar *)ip, IP_HDR_SIZE_NO_UDP / 2)) {
Luca Ceresoli4f63acd2011-05-11 03:59:56 +00001317 puts("checksum bad\n");
wdenk2d966952002-10-31 22:12:35 +00001318 return;
1319 }
Alessandro Rubini5cfaa4e2009-08-07 13:58:56 +02001320 /* If it is not for us, ignore it */
wdenk2d966952002-10-31 22:12:35 +00001321 tmp = NetReadIP(&ip->ip_dst);
1322 if (NetOurIP && tmp != NetOurIP && tmp != 0xFFFFFFFF) {
David Updegraff53a5c422007-06-11 10:41:07 -05001323#ifdef CONFIG_MCAST_TFTP
Wolfgang Denk85eb5ca2007-08-14 09:47:27 +02001324 if (Mcast_addr != tmp)
David Updegraff53a5c422007-06-11 10:41:07 -05001325#endif
Luca Ceresolic819abe2011-05-04 02:40:46 +00001326 return;
wdenk2d966952002-10-31 22:12:35 +00001327 }
Luca Ceresoli03eb1292011-04-18 06:19:50 +00001328 /* Read source IP address for later use */
1329 src_ip = NetReadIP(&ip->ip_src);
wdenk2d966952002-10-31 22:12:35 +00001330 /*
Alessandro Rubini5cfaa4e2009-08-07 13:58:56 +02001331 * The function returns the unchanged packet if it's not
1332 * a fragment, and either the complete packet or NULL if
1333 * it is a fragment (if !CONFIG_IP_DEFRAG, it returns NULL)
1334 */
Luca Ceresoliccb9ebe2011-05-04 02:40:47 +00001335 ip = NetDefragment(ip, &len);
1336 if (!ip)
Alessandro Rubini5cfaa4e2009-08-07 13:58:56 +02001337 return;
1338 /*
wdenk2d966952002-10-31 22:12:35 +00001339 * watch for ICMP host redirects
1340 *
wdenk8bde7f72003-06-27 21:31:46 +00001341 * There is no real handler code (yet). We just watch
1342 * for ICMP host redirect messages. In case anybody
1343 * sees these messages: please contact me
1344 * (wd@denx.de), or - even better - send me the
1345 * necessary fixes :-)
wdenk2d966952002-10-31 22:12:35 +00001346 *
wdenk8bde7f72003-06-27 21:31:46 +00001347 * Note: in all cases where I have seen this so far
1348 * it was a problem with the router configuration,
1349 * for instance when a router was configured in the
1350 * BOOTP reply, but the TFTP server was on the same
1351 * subnet. So this is probably a warning that your
1352 * configuration might be wrong. But I'm not really
1353 * sure if there aren't any other situations.
Simon Glass4793ee62011-10-24 18:00:01 +00001354 *
1355 * Simon Glass <sjg@chromium.org>: We get an ICMP when
1356 * we send a tftp packet to a dead connection, or when
1357 * there is no server at the other end.
wdenk2d966952002-10-31 22:12:35 +00001358 */
1359 if (ip->ip_p == IPPROTO_ICMP) {
Simon Glass8f79bb12011-10-24 18:00:00 +00001360 receive_icmp(ip, len, src_ip, et);
1361 return;
wdenk2d966952002-10-31 22:12:35 +00001362 } else if (ip->ip_p != IPPROTO_UDP) { /* Only UDP packets */
1363 return;
1364 }
1365
Stefan Roese8534bf92005-08-12 20:06:52 +02001366#ifdef CONFIG_UDP_CHECKSUM
1367 if (ip->udp_xsum != 0) {
Wolfgang Denkb2f50802005-08-12 23:43:12 +02001368 ulong xsum;
Stefan Roese8534bf92005-08-12 20:06:52 +02001369 ushort *sumptr;
1370 ushort sumlen;
1371
1372 xsum = ip->ip_p;
1373 xsum += (ntohs(ip->udp_len));
1374 xsum += (ntohl(ip->ip_src) >> 16) & 0x0000ffff;
1375 xsum += (ntohl(ip->ip_src) >> 0) & 0x0000ffff;
1376 xsum += (ntohl(ip->ip_dst) >> 16) & 0x0000ffff;
1377 xsum += (ntohl(ip->ip_dst) >> 0) & 0x0000ffff;
1378
1379 sumlen = ntohs(ip->udp_len);
1380 sumptr = (ushort *) &(ip->udp_src);
1381
1382 while (sumlen > 1) {
Wolfgang Denkb2f50802005-08-12 23:43:12 +02001383 ushort sumdata;
Stefan Roese8534bf92005-08-12 20:06:52 +02001384
1385 sumdata = *sumptr++;
1386 xsum += ntohs(sumdata);
1387 sumlen -= 2;
1388 }
1389 if (sumlen > 0) {
Wolfgang Denkb2f50802005-08-12 23:43:12 +02001390 ushort sumdata;
Stefan Roese8534bf92005-08-12 20:06:52 +02001391
1392 sumdata = *(unsigned char *) sumptr;
Wolfgang Denkb2f50802005-08-12 23:43:12 +02001393 sumdata = (sumdata << 8) & 0xff00;
Stefan Roese8534bf92005-08-12 20:06:52 +02001394 xsum += sumdata;
1395 }
1396 while ((xsum >> 16) != 0) {
Luca Ceresoli3e38e422011-05-11 03:59:54 +00001397 xsum = (xsum & 0x0000ffff) +
1398 ((xsum >> 16) & 0x0000ffff);
Stefan Roese8534bf92005-08-12 20:06:52 +02001399 }
1400 if ((xsum != 0x00000000) && (xsum != 0x0000ffff)) {
Wolfgang Denk9b55a252008-07-11 01:16:00 +02001401 printf(" UDP wrong checksum %08lx %08x\n",
1402 xsum, ntohs(ip->udp_xsum));
Stefan Roese8534bf92005-08-12 20:06:52 +02001403 return;
1404 }
1405 }
1406#endif
1407
David Updegraff53a5c422007-06-11 10:41:07 -05001408
wdenk68ceb292004-08-02 21:11:11 +00001409#ifdef CONFIG_NETCONSOLE
Luca Ceresoli4f63acd2011-05-11 03:59:56 +00001410 nc_input_packet((uchar *)ip + IP_HDR_SIZE,
wdenk68ceb292004-08-02 21:11:11 +00001411 ntohs(ip->udp_dst),
1412 ntohs(ip->udp_src),
1413 ntohs(ip->udp_len) - 8);
1414#endif
wdenk2d966952002-10-31 22:12:35 +00001415 /*
1416 * IP header OK. Pass the packet to the current handler.
1417 */
Luca Ceresoli4f63acd2011-05-11 03:59:56 +00001418 (*packetHandler)((uchar *)ip + IP_HDR_SIZE,
wdenk2d966952002-10-31 22:12:35 +00001419 ntohs(ip->udp_dst),
Luca Ceresoli03eb1292011-04-18 06:19:50 +00001420 src_ip,
wdenk2d966952002-10-31 22:12:35 +00001421 ntohs(ip->udp_src),
1422 ntohs(ip->udp_len) - 8);
wdenk2d966952002-10-31 22:12:35 +00001423 break;
1424 }
1425}
1426
1427
1428/**********************************************************************/
1429
Simon Glasse4bf0c52011-10-24 18:00:02 +00001430static int net_check_prereq(enum proto_t protocol)
wdenk2d966952002-10-31 22:12:35 +00001431{
1432 switch (protocol) {
wdenk6e592382004-04-18 17:39:38 +00001433 /* Fall through */
Jon Loeliger643d1ab2007-07-09 17:45:14 -05001434#if defined(CONFIG_CMD_PING)
wdenk73a8b272003-06-05 19:27:42 +00001435 case PING:
wdenk6e592382004-04-18 17:39:38 +00001436 if (NetPingIP == 0) {
Luca Ceresoli4f63acd2011-05-11 03:59:56 +00001437 puts("*** ERROR: ping address not given\n");
Luca Ceresoli92895de2011-05-04 02:40:45 +00001438 return 1;
wdenk6e592382004-04-18 17:39:38 +00001439 }
1440 goto common;
wdenk73a8b272003-06-05 19:27:42 +00001441#endif
Jon Loeliger643d1ab2007-07-09 17:45:14 -05001442#if defined(CONFIG_CMD_SNTP)
wdenkea287de2005-04-01 00:25:43 +00001443 case SNTP:
1444 if (NetNtpServerIP == 0) {
Luca Ceresoli4f63acd2011-05-11 03:59:56 +00001445 puts("*** ERROR: NTP server address not given\n");
Luca Ceresoli92895de2011-05-04 02:40:45 +00001446 return 1;
wdenkea287de2005-04-01 00:25:43 +00001447 }
1448 goto common;
1449#endif
Robin Getz1a32bf42009-07-20 14:53:54 -04001450#if defined(CONFIG_CMD_DNS)
1451 case DNS:
1452 if (NetOurDNSIP == 0) {
1453 puts("*** ERROR: DNS server address not given\n");
1454 return 1;
1455 }
1456 goto common;
1457#endif
Jon Loeliger643d1ab2007-07-09 17:45:14 -05001458#if defined(CONFIG_CMD_NFS)
wdenkcbd8a352004-02-24 02:00:03 +00001459 case NFS:
1460#endif
Simon Glasse4bf0c52011-10-24 18:00:02 +00001461 case TFTPGET:
Simon Glass1fb7cd42011-10-24 18:00:07 +00001462 case TFTPPUT:
wdenk6e592382004-04-18 17:39:38 +00001463 if (NetServerIP == 0) {
Luca Ceresoli4f63acd2011-05-11 03:59:56 +00001464 puts("*** ERROR: `serverip' not set\n");
Luca Ceresoli92895de2011-05-04 02:40:45 +00001465 return 1;
wdenk6e592382004-04-18 17:39:38 +00001466 }
Luca Ceresoli4f63acd2011-05-11 03:59:56 +00001467#if defined(CONFIG_CMD_PING) || defined(CONFIG_CMD_SNTP) || \
1468 defined(CONFIG_CMD_DNS)
1469common:
wdenk73a8b272003-06-05 19:27:42 +00001470#endif
Simon Guinot8b6bbe12011-05-01 23:38:40 +00001471 /* Fall through */
wdenk73a8b272003-06-05 19:27:42 +00001472
Simon Guinot8b6bbe12011-05-01 23:38:40 +00001473 case NETCONS:
Luca Ceresoli7a83af02011-05-17 00:03:40 +00001474 case TFTPSRV:
wdenk6e592382004-04-18 17:39:38 +00001475 if (NetOurIP == 0) {
Luca Ceresoli4f63acd2011-05-11 03:59:56 +00001476 puts("*** ERROR: `ipaddr' not set\n");
Luca Ceresoli92895de2011-05-04 02:40:45 +00001477 return 1;
wdenk6e592382004-04-18 17:39:38 +00001478 }
1479 /* Fall through */
wdenk2d966952002-10-31 22:12:35 +00001480
Peter Tyserbf6cb242010-09-30 11:25:48 -05001481#ifdef CONFIG_CMD_RARP
wdenk2d966952002-10-31 22:12:35 +00001482 case RARP:
Peter Tyserbf6cb242010-09-30 11:25:48 -05001483#endif
wdenk2d966952002-10-31 22:12:35 +00001484 case BOOTP:
wdenka3d991b2004-04-15 21:48:45 +00001485 case CDP:
Peter Tyserbf6cb242010-09-30 11:25:48 -05001486 case DHCP:
Luca Ceresoli4f63acd2011-05-11 03:59:56 +00001487 if (memcmp(NetOurEther, "\0\0\0\0\0\0", 6) == 0) {
Luca Ceresoli4f63acd2011-05-11 03:59:56 +00001488 int num = eth_get_dev_index();
wdenk2d966952002-10-31 22:12:35 +00001489
wdenk6e592382004-04-18 17:39:38 +00001490 switch (num) {
1491 case -1:
Luca Ceresoli4f63acd2011-05-11 03:59:56 +00001492 puts("*** ERROR: No ethernet found.\n");
Luca Ceresoli92895de2011-05-04 02:40:45 +00001493 return 1;
wdenk6e592382004-04-18 17:39:38 +00001494 case 0:
Luca Ceresoli4f63acd2011-05-11 03:59:56 +00001495 puts("*** ERROR: `ethaddr' not set\n");
wdenk2d966952002-10-31 22:12:35 +00001496 break;
wdenk6e592382004-04-18 17:39:38 +00001497 default:
Luca Ceresoli4f63acd2011-05-11 03:59:56 +00001498 printf("*** ERROR: `eth%daddr' not set\n",
wdenk2d966952002-10-31 22:12:35 +00001499 num);
1500 break;
wdenk2d966952002-10-31 22:12:35 +00001501 }
wdenk6e592382004-04-18 17:39:38 +00001502
Luca Ceresoli4f63acd2011-05-11 03:59:56 +00001503 NetStartAgain();
Luca Ceresoli92895de2011-05-04 02:40:45 +00001504 return 2;
wdenk6e592382004-04-18 17:39:38 +00001505 }
1506 /* Fall through */
1507 default:
Luca Ceresoli92895de2011-05-04 02:40:45 +00001508 return 0;
wdenk2d966952002-10-31 22:12:35 +00001509 }
Luca Ceresoli92895de2011-05-04 02:40:45 +00001510 return 0; /* OK */
wdenk2d966952002-10-31 22:12:35 +00001511}
1512/**********************************************************************/
1513
1514int
Luca Ceresoli6b147d12011-05-04 02:40:44 +00001515NetCksumOk(uchar *ptr, int len)
wdenk2d966952002-10-31 22:12:35 +00001516{
1517 return !((NetCksum(ptr, len) + 1) & 0xfffe);
1518}
1519
1520
1521unsigned
Luca Ceresoli6b147d12011-05-04 02:40:44 +00001522NetCksum(uchar *ptr, int len)
wdenk2d966952002-10-31 22:12:35 +00001523{
1524 ulong xsum;
Stefan Roese9d2a8732005-08-31 12:55:50 +02001525 ushort *p = (ushort *)ptr;
wdenk2d966952002-10-31 22:12:35 +00001526
1527 xsum = 0;
1528 while (len-- > 0)
Wolfgang Denk7bc5ee02005-08-26 01:36:03 +02001529 xsum += *p++;
wdenk2d966952002-10-31 22:12:35 +00001530 xsum = (xsum & 0xffff) + (xsum >> 16);
1531 xsum = (xsum & 0xffff) + (xsum >> 16);
Luca Ceresoli92895de2011-05-04 02:40:45 +00001532 return xsum & 0xffff;
wdenk2d966952002-10-31 22:12:35 +00001533}
1534
wdenka3d991b2004-04-15 21:48:45 +00001535int
1536NetEthHdrSize(void)
1537{
1538 ushort myvlanid;
wdenk2d966952002-10-31 22:12:35 +00001539
wdenka3d991b2004-04-15 21:48:45 +00001540 myvlanid = ntohs(NetOurVLAN);
1541 if (myvlanid == (ushort)-1)
1542 myvlanid = VLAN_NONE;
1543
Luca Ceresoli3e38e422011-05-11 03:59:54 +00001544 return ((myvlanid & VLAN_IDMASK) == VLAN_NONE) ? ETHER_HDR_SIZE :
1545 VLAN_ETHER_HDR_SIZE;
wdenka3d991b2004-04-15 21:48:45 +00001546}
1547
1548int
Joe Hershbergerdb288a92012-05-15 08:59:04 +00001549NetSetEther(uchar *xet, uchar * addr, uint prot)
wdenk2d966952002-10-31 22:12:35 +00001550{
1551 Ethernet_t *et = (Ethernet_t *)xet;
wdenka3d991b2004-04-15 21:48:45 +00001552 ushort myvlanid;
1553
1554 myvlanid = ntohs(NetOurVLAN);
1555 if (myvlanid == (ushort)-1)
1556 myvlanid = VLAN_NONE;
wdenk2d966952002-10-31 22:12:35 +00001557
Luca Ceresoli4f63acd2011-05-11 03:59:56 +00001558 memcpy(et->et_dest, addr, 6);
1559 memcpy(et->et_src, NetOurEther, 6);
wdenka3d991b2004-04-15 21:48:45 +00001560 if ((myvlanid & VLAN_IDMASK) == VLAN_NONE) {
Luca Ceresolic819abe2011-05-04 02:40:46 +00001561 et->et_protlen = htons(prot);
wdenka3d991b2004-04-15 21:48:45 +00001562 return ETHER_HDR_SIZE;
1563 } else {
1564 VLAN_Ethernet_t *vet = (VLAN_Ethernet_t *)xet;
wdenk2d966952002-10-31 22:12:35 +00001565
wdenka3d991b2004-04-15 21:48:45 +00001566 vet->vet_vlan_type = htons(PROT_VLAN);
1567 vet->vet_tag = htons((0 << 5) | (myvlanid & VLAN_IDMASK));
1568 vet->vet_type = htons(prot);
1569 return VLAN_ETHER_HDR_SIZE;
1570 }
1571}
wdenk2d966952002-10-31 22:12:35 +00001572
1573void
Joe Hershbergerdb288a92012-05-15 08:59:04 +00001574NetSetIP(uchar *xip, IPaddr_t dest, int dport, int sport, int len)
wdenk2d966952002-10-31 22:12:35 +00001575{
Olav Morkenaf8626e2009-01-23 12:56:26 +01001576 IP_t *ip = (IP_t *)xip;
wdenk2d966952002-10-31 22:12:35 +00001577
1578 /*
1579 * If the data is an odd number of bytes, zero the
1580 * byte after the last byte so that the checksum
1581 * will work.
1582 */
1583 if (len & 1)
1584 xip[IP_HDR_SIZE + len] = 0;
1585
1586 /*
1587 * Construct an IP and UDP header.
wdenk6e592382004-04-18 17:39:38 +00001588 * (need to set no fragment bit - XXX)
wdenk2d966952002-10-31 22:12:35 +00001589 */
Luca Ceresoli3e38e422011-05-11 03:59:54 +00001590 /* IP_HDR_SIZE / 4 (not including UDP) */
1591 ip->ip_hl_v = 0x45;
wdenk2d966952002-10-31 22:12:35 +00001592 ip->ip_tos = 0;
1593 ip->ip_len = htons(IP_HDR_SIZE + len);
1594 ip->ip_id = htons(NetIPID++);
Peter Tysere0c07b82008-12-01 16:26:20 -06001595 ip->ip_off = htons(IP_FLAGS_DFRAG); /* Don't fragment */
wdenk2d966952002-10-31 22:12:35 +00001596 ip->ip_ttl = 255;
1597 ip->ip_p = 17; /* UDP */
1598 ip->ip_sum = 0;
Luca Ceresoli3e38e422011-05-11 03:59:54 +00001599 /* already in network byte order */
Luca Ceresoli6b147d12011-05-04 02:40:44 +00001600 NetCopyIP((void *)&ip->ip_src, &NetOurIP);
Luca Ceresoli3e38e422011-05-11 03:59:54 +00001601 /* - "" - */
Luca Ceresoli6b147d12011-05-04 02:40:44 +00001602 NetCopyIP((void *)&ip->ip_dst, &dest);
wdenk2d966952002-10-31 22:12:35 +00001603 ip->udp_src = htons(sport);
1604 ip->udp_dst = htons(dport);
1605 ip->udp_len = htons(8 + len);
1606 ip->udp_xsum = 0;
1607 ip->ip_sum = ~NetCksum((uchar *)ip, IP_HDR_SIZE_NO_UDP / 2);
1608}
1609
Luca Ceresoli4f63acd2011-05-11 03:59:56 +00001610void copy_filename(char *dst, const char *src, int size)
wdenk2d966952002-10-31 22:12:35 +00001611{
1612 if (*src && (*src == '"')) {
1613 ++src;
1614 --size;
1615 }
1616
Luca Ceresolid3c65b02011-05-04 02:40:43 +00001617 while ((--size > 0) && *src && (*src != '"'))
wdenk2d966952002-10-31 22:12:35 +00001618 *dst++ = *src++;
wdenk2d966952002-10-31 22:12:35 +00001619 *dst = '\0';
1620}
1621
Luca Ceresoli3e38e422011-05-11 03:59:54 +00001622#if defined(CONFIG_CMD_NFS) || \
1623 defined(CONFIG_CMD_SNTP) || \
1624 defined(CONFIG_CMD_DNS)
Robin Getz1a32bf42009-07-20 14:53:54 -04001625/*
Robin Getz97399462010-03-08 14:07:00 -05001626 * make port a little random (1024-17407)
1627 * This keeps the math somewhat trivial to compute, and seems to work with
1628 * all supported protocols/clients/servers
Robin Getz1a32bf42009-07-20 14:53:54 -04001629 */
1630unsigned int random_port(void)
1631{
Robin Getz97399462010-03-08 14:07:00 -05001632 return 1024 + (get_timer(0) % 0x4000);
Robin Getz1a32bf42009-07-20 14:53:54 -04001633}
1634#endif
1635
Luca Ceresoli4f63acd2011-05-11 03:59:56 +00001636void ip_to_string(IPaddr_t x, char *s)
wdenk2d966952002-10-31 22:12:35 +00001637{
Luca Ceresoli4f63acd2011-05-11 03:59:56 +00001638 x = ntohl(x);
1639 sprintf(s, "%d.%d.%d.%d",
1640 (int) ((x >> 24) & 0xff),
1641 (int) ((x >> 16) & 0xff),
1642 (int) ((x >> 8) & 0xff), (int) ((x >> 0) & 0xff)
wdenk6e592382004-04-18 17:39:38 +00001643 );
wdenk2d966952002-10-31 22:12:35 +00001644}
1645
wdenka3d991b2004-04-15 21:48:45 +00001646void VLAN_to_string(ushort x, char *s)
1647{
1648 x = ntohs(x);
1649
1650 if (x == (ushort)-1)
1651 x = VLAN_NONE;
1652
1653 if (x == VLAN_NONE)
1654 strcpy(s, "none");
1655 else
1656 sprintf(s, "%d", x & VLAN_IDMASK);
1657}
1658
Mike Frysinger2e3ef6e2010-10-20 07:16:48 -04001659ushort string_to_VLAN(const char *s)
wdenka3d991b2004-04-15 21:48:45 +00001660{
1661 ushort id;
1662
1663 if (s == NULL)
wdenkb9711de2004-04-25 13:18:40 +00001664 return htons(VLAN_NONE);
wdenka3d991b2004-04-15 21:48:45 +00001665
1666 if (*s < '0' || *s > '9')
1667 id = VLAN_NONE;
1668 else
1669 id = (ushort)simple_strtoul(s, NULL, 10);
1670
wdenkb9711de2004-04-25 13:18:40 +00001671 return htons(id);
wdenka3d991b2004-04-15 21:48:45 +00001672}
1673
wdenka3d991b2004-04-15 21:48:45 +00001674ushort getenv_VLAN(char *var)
1675{
Luca Ceresoli92895de2011-05-04 02:40:45 +00001676 return string_to_VLAN(getenv(var));
wdenka3d991b2004-04-15 21:48:45 +00001677}