]> git.ipfire.org Git - people/ms/u-boot.git/blame - net/net.c
net: Make the netconsole buffer size configurable
[people/ms/u-boot.git] / net / net.c
CommitLineData
2d966958
WD
1/*
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 *
d22c338e
JH
26 * LINK_LOCAL:
27 *
28 * Prerequisites: - own ethernet address
29 * We want: - own IP address
30 * Next step: ARP
31 *
2d966958
WD
32 * RARP:
33 *
34 * Prerequisites: - own ethernet address
35 * We want: - own IP address
36 * - TFTP server IP address
37 * Next step: ARP
38 *
39 * ARP:
40 *
41 * Prerequisites: - own ethernet address
42 * - own IP address
43 * - TFTP server IP address
44 * We want: - TFTP server ethernet address
45 * Next step: TFTP
46 *
47 * DHCP:
48 *
b2f50807
WD
49 * Prerequisites: - own ethernet address
50 * We want: - IP, Netmask, ServerIP, Gateway IP
51 * - bootfilename, lease time
52 * Next step: - TFTP
2d966958
WD
53 *
54 * TFTP:
55 *
56 * Prerequisites: - own ethernet address
57 * - own IP address
58 * - TFTP server IP address
59 * - TFTP server ethernet address
60 * - name of bootfile (if unknown, we use a default name
61 * derived from our own IP address)
62 * We want: - load the boot file
63 * Next step: none
cbd8a35c
WD
64 *
65 * NFS:
66 *
67 * Prerequisites: - own ethernet address
68 * - own IP address
69 * - name of bootfile (if unknown, we use a default name
70 * derived from our own IP address)
71 * We want: - load the boot file
72 * Next step: none
ea287deb
WD
73 *
74 * SNTP:
75 *
b2f50807 76 * Prerequisites: - own ethernet address
ea287deb
WD
77 * - own IP address
78 * We want: - network time
79 * Next step: none
2d966958
WD
80 */
81
82
83#include <common.h>
2d966958
WD
84#include <command.h>
85#include <net.h>
4545f4e6 86#if defined(CONFIG_STATUS_LED)
fc3e2165 87#include <miiphy.h>
4545f4e6 88#include <status_led.h>
fc3e2165 89#endif
4545f4e6
JH
90#include <watchdog.h>
91#include <linux/compiler.h>
92#include "arp.h"
93#include "bootp.h"
f575ae1f 94#include "cdp.h"
1a32bf41
RG
95#if defined(CONFIG_CMD_DNS)
96#include "dns.h"
97#endif
d22c338e 98#include "link_local.h"
4545f4e6 99#include "nfs.h"
a36b12f9 100#include "ping.h"
4545f4e6
JH
101#include "rarp.h"
102#if defined(CONFIG_CMD_SNTP)
103#include "sntp.h"
104#endif
105#include "tftp.h"
2d966958 106
d87080b7
WD
107DECLARE_GLOBAL_DATA_PTR;
108
2d966958
WD
109/** BOOTP EXTENTIONS **/
110
3e38e429 111/* Our subnet mask (0=unknown) */
c586ce6e 112IPaddr_t NetOurSubnetMask;
3e38e429 113/* Our gateways IP address */
c586ce6e 114IPaddr_t NetOurGatewayIP;
3e38e429 115/* Our DNS IP address */
c586ce6e 116IPaddr_t NetOurDNSIP;
1fe80d79 117#if defined(CONFIG_BOOTP_DNS2)
3e38e429 118/* Our 2nd DNS IP address */
c586ce6e 119IPaddr_t NetOurDNS2IP;
3e38e429
LC
120#endif
121/* Our NIS domain */
c586ce6e 122char NetOurNISDomain[32] = {0,};
3e38e429 123/* Our hostname */
c586ce6e 124char NetOurHostName[32] = {0,};
3e38e429 125/* Our bootpath */
c586ce6e 126char NetOurRootPath[64] = {0,};
3e38e429 127/* Our bootfile size in blocks */
c586ce6e 128ushort NetBootFileSize;
2d966958 129
53a5c424
DU
130#ifdef CONFIG_MCAST_TFTP /* Multicast TFTP */
131IPaddr_t Mcast_addr;
132#endif
133
2d966958
WD
134/** END OF BOOTP EXTENTIONS **/
135
3e38e429
LC
136/* The actual transferred size of the bootfile (in bytes) */
137ulong NetBootFileXferSize;
138/* Our ethernet address */
139uchar NetOurEther[6];
140/* Boot server enet address */
c586ce6e 141uchar NetServerEther[6];
3e38e429
LC
142/* Our IP addr (0 = unknown) */
143IPaddr_t NetOurIP;
144/* Server IP addr (0 = unknown) */
145IPaddr_t NetServerIP;
146/* Current receive packet */
db288a96 147uchar *NetRxPacket;
3e38e429
LC
148/* Current rx packet length */
149int NetRxPacketLen;
150/* IP packet ID */
151unsigned NetIPID;
152/* Ethernet bcast address */
c586ce6e
LC
153uchar NetBcastAddr[6] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
154uchar NetEtherNullAddr[6];
f85b6071 155#ifdef CONFIG_API
db288a96 156void (*push_packet)(void *, int len) = 0;
f85b6071 157#endif
3e38e429 158/* Network loop state */
22f6e99d 159enum net_loop_state net_state;
3e38e429 160/* Tried all network devices */
c586ce6e 161int NetRestartWrap;
3e38e429 162/* Network loop restarted */
c586ce6e 163static int NetRestarted;
3e38e429 164/* At least one device configured */
c586ce6e 165static int NetDevExists;
2d966958 166
6e592385 167/* XXX in both little & big endian machines 0xFFFF == ntohs(-1) */
3e38e429
LC
168/* default is without VLAN */
169ushort NetOurVLAN = 0xFFFF;
170/* ditto */
171ushort NetOurNativeVLAN = 0xFFFF;
a3d991bd 172
3e38e429
LC
173/* Boot File name */
174char BootFile[128];
2d966958 175
643d1ab2 176#if defined(CONFIG_CMD_SNTP)
3e38e429
LC
177/* NTP server IP address */
178IPaddr_t NetNtpServerIP;
179/* offset time from UTC */
c586ce6e 180int NetTimeOffset;
ea287deb
WD
181#endif
182
db288a96 183uchar PktBuf[(PKTBUFSRX+1) * PKTSIZE_ALIGN + PKTALIGN];
2d966958 184
3e38e429 185/* Receive packet */
db288a96 186uchar *NetRxPackets[PKTBUFSRX];
2d966958 187
ece223b5
JH
188/* Current UDP RX packet handler */
189static rxhand_f *udp_packet_handler;
190/* Current ARP RX packet handler */
191static rxhand_f *arp_packet_handler;
39bccd21 192#ifdef CONFIG_CMD_TFTPPUT
ece223b5
JH
193/* Current ICMP rx handler */
194static rxhand_icmp_f *packet_icmp_handler;
39bccd21 195#endif
3e38e429
LC
196/* Current timeout handler */
197static thand_f *timeHandler;
198/* Time base value */
199static ulong timeStart;
200/* Current timeout value */
201static ulong timeDelta;
202/* THE transmit packet */
db288a96 203uchar *NetTxPacket;
2d966958 204
e4bf0c5c 205static int net_check_prereq(enum proto_t protocol);
2d966958 206
67b96e87
RB
207static int NetTryCount;
208
73a8b27c
WD
209/**********************************************************************/
210
e4a3d57d
SG
211/*
212 * Check if autoload is enabled. If so, use either NFS or TFTP to download
213 * the boot file.
214 */
215void net_auto_load(void)
216{
217 const char *s = getenv("autoload");
218
219 if (s != NULL) {
220 if (*s == 'n') {
221 /*
222 * Just use BOOTP/RARP to configure system;
223 * Do not use TFTP to load the bootfile.
224 */
22f6e99d 225 net_set_state(NETLOOP_SUCCESS);
e4a3d57d
SG
226 return;
227 }
228#if defined(CONFIG_CMD_NFS)
229 if (strcmp(s, "NFS") == 0) {
230 /*
231 * Use NFS to load the bootfile.
232 */
233 NfsStart();
234 return;
235 }
236#endif
237 }
238 TftpStart(TFTPGET);
239}
240
cb1c9911 241static void NetInitLoop(void)
2f70c49e 242{
c586ce6e 243 static int env_changed_id;
4f63acd0 244 int env_id = get_env_id();
2f70c49e
HS
245
246 /* update only when the environment has changed */
3c172c4f 247 if (env_changed_id != env_id) {
23a70bf9 248 NetOurIP = getenv_IPaddr("ipaddr");
4f63acd0
LC
249 NetOurGatewayIP = getenv_IPaddr("gatewayip");
250 NetOurSubnetMask = getenv_IPaddr("netmask");
251 NetServerIP = getenv_IPaddr("serverip");
2f70c49e 252 NetOurNativeVLAN = getenv_VLAN("nvlan");
3c172c4f 253 NetOurVLAN = getenv_VLAN("vlan");
1a32bf41
RG
254#if defined(CONFIG_CMD_DNS)
255 NetOurDNSIP = getenv_IPaddr("dnsip");
256#endif
3c172c4f 257 env_changed_id = env_id;
2f70c49e 258 }
a03d6388 259 memcpy(NetOurEther, eth_get_dev()->enetaddr, 6);
3c172c4f 260
da95427c 261 return;
2f70c49e
HS
262}
263
ece223b5
JH
264static void net_clear_handlers(void)
265{
266 net_set_udp_handler(NULL);
267 net_set_arp_handler(NULL);
268 NetSetTimeout(0, NULL);
269}
270
271static void net_cleanup_loop(void)
272{
273 net_clear_handlers();
274}
275
46c495d5
JH
276void net_init(void)
277{
278 static int first_call = 1;
279
280 if (first_call) {
281 /*
282 * Setup packet buffers, aligned correctly.
283 */
284 int i;
285
286 NetTxPacket = &PktBuf[0] + (PKTALIGN - 1);
287 NetTxPacket -= (ulong)NetTxPacket % PKTALIGN;
288 for (i = 0; i < PKTBUFSRX; i++)
289 NetRxPackets[i] = NetTxPacket + (i + 1) * PKTSIZE_ALIGN;
290
291 ArpInit();
292 net_clear_handlers();
293
294 /* Only need to setup buffer pointers once. */
295 first_call = 0;
296 }
297
298 NetInitLoop();
299}
300
2d966958
WD
301/**********************************************************************/
302/*
303 * Main network processing loop.
304 */
305
e4bf0c5c 306int NetLoop(enum proto_t protocol)
2d966958 307{
2d966958 308 bd_t *bd = gd->bd;
4793ee65 309 int ret = -1;
2d966958 310
2d966958
WD
311 NetRestarted = 0;
312 NetDevExists = 0;
67b96e87 313 NetTryCount = 1;
4ef8d53c 314 debug_cond(DEBUG_INT_STATE, "--- NetLoop Entry\n");
73a8b27c 315
573f14fe 316 bootstage_mark_name(BOOTSTAGE_ID_ETH_START, "eth_start");
46c495d5 317 net_init();
2d966958 318 eth_halt();
a3d991bd 319 eth_set_current();
b1bf6f2c
WD
320 if (eth_init(bd) < 0) {
321 eth_halt();
92895de9 322 return -1;
b1bf6f2c 323 }
2d966958
WD
324
325restart:
22f6e99d 326 net_set_state(NETLOOP_CONTINUE);
2d966958
WD
327
328 /*
329 * Start the ball rolling with the given start function. From
330 * here on, this code is a state machine driven by received
331 * packets and timer events.
332 */
4ef8d53c 333 debug_cond(DEBUG_INT_STATE, "--- NetLoop Init\n");
cb1c9911 334 NetInitLoop();
2d966958 335
4f63acd0 336 switch (net_check_prereq(protocol)) {
2d966958
WD
337 case 1:
338 /* network not configured */
b1bf6f2c 339 eth_halt();
92895de9 340 return -1;
2d966958 341
2d966958
WD
342 case 2:
343 /* network device not configured */
344 break;
2d966958
WD
345
346 case 0:
2d966958 347 NetDevExists = 1;
e4bf0c5c 348 NetBootFileXferSize = 0;
2d966958 349 switch (protocol) {
e4bf0c5c 350 case TFTPGET:
1fb7cd49
SG
351#ifdef CONFIG_CMD_TFTPPUT
352 case TFTPPUT:
353#endif
2d966958 354 /* always use ARP to get server ethernet address */
e4bf0c5c 355 TftpStart(protocol);
2d966958 356 break;
7a83af07
LC
357#ifdef CONFIG_CMD_TFTPSRV
358 case TFTPSRV:
359 TftpStartServer();
360 break;
361#endif
643d1ab2 362#if defined(CONFIG_CMD_DHCP)
2d966958 363 case DHCP:
d407bf52 364 BootpTry = 0;
09133f85 365 NetOurIP = 0;
2d966958
WD
366 DhcpRequest(); /* Basically same as BOOTP */
367 break;
610f2e9c 368#endif
2d966958
WD
369
370 case BOOTP:
371 BootpTry = 0;
09133f85 372 NetOurIP = 0;
4f63acd0 373 BootpRequest();
2d966958
WD
374 break;
375
bf6cb247 376#if defined(CONFIG_CMD_RARP)
2d966958
WD
377 case RARP:
378 RarpTry = 0;
09133f85 379 NetOurIP = 0;
4f63acd0 380 RarpRequest();
2d966958 381 break;
bf6cb247 382#endif
643d1ab2 383#if defined(CONFIG_CMD_PING)
73a8b27c 384 case PING:
a36b12f9 385 ping_start();
73a8b27c 386 break;
cbd8a35c 387#endif
643d1ab2 388#if defined(CONFIG_CMD_NFS)
cbd8a35c
WD
389 case NFS:
390 NfsStart();
391 break;
a3d991bd 392#endif
643d1ab2 393#if defined(CONFIG_CMD_CDP)
a3d991bd
WD
394 case CDP:
395 CDPStart();
396 break;
68ceb29e
WD
397#endif
398#ifdef CONFIG_NETCONSOLE
399 case NETCONS:
400 NcStart();
401 break;
ea287deb 402#endif
643d1ab2 403#if defined(CONFIG_CMD_SNTP)
ea287deb
WD
404 case SNTP:
405 SntpStart();
406 break;
1a32bf41
RG
407#endif
408#if defined(CONFIG_CMD_DNS)
409 case DNS:
410 DnsStart();
411 break;
d22c338e
JH
412#endif
413#if defined(CONFIG_CMD_LINK_LOCAL)
414 case LINKLOCAL:
415 link_local_start();
416 break;
73a8b27c 417#endif
2d966958
WD
418 default:
419 break;
420 }
421
2d966958
WD
422 break;
423 }
424
643d1ab2 425#if defined(CONFIG_MII) || defined(CONFIG_CMD_MII)
3e38e429
LC
426#if defined(CONFIG_SYS_FAULT_ECHO_LINK_DOWN) && \
427 defined(CONFIG_STATUS_LED) && \
428 defined(STATUS_LED_RED)
fc3e2165 429 /*
42d1f039 430 * Echo the inverted link state to the fault LED.
fc3e2165 431 */
d3c65b01 432 if (miiphy_link(eth_get_dev()->name, CONFIG_SYS_FAULT_MII_ADDR))
4f63acd0 433 status_led_set(STATUS_LED_RED, STATUS_LED_OFF);
d3c65b01 434 else
4f63acd0 435 status_led_set(STATUS_LED_RED, STATUS_LED_ON);
6d0f6bcf 436#endif /* CONFIG_SYS_FAULT_ECHO_LINK_DOWN, ... */
fc3e2165 437#endif /* CONFIG_MII, ... */
2d966958
WD
438
439 /*
440 * Main packet reception loop. Loop receiving packets until
22f6e99d 441 * someone sets `net_state' to a state that terminates.
2d966958
WD
442 */
443 for (;;) {
444 WATCHDOG_RESET();
445#ifdef CONFIG_SHOW_ACTIVITY
48522bb5 446 show_activity(1);
2d966958
WD
447#endif
448 /*
449 * Check the ethernet for a new packet. The ethernet
450 * receive routine will process it.
451 */
40cb90ee 452 eth_rx();
2d966958
WD
453
454 /*
455 * Abort if ctrl-c was pressed.
456 */
457 if (ctrlc()) {
e94070c4
JH
458 /* cancel any ARP that may not have completed */
459 NetArpWaitPacketIP = 0;
460
ece223b5 461 net_cleanup_loop();
8bde7f77 462 eth_halt();
4f63acd0 463 puts("\nAbort\n");
4ef8d53c
JH
464 /* include a debug print as well incase the debug
465 messages are directed to stderr */
466 debug_cond(DEBUG_INT_STATE, "--- NetLoop Abort!\n");
4793ee65 467 goto done;
2d966958
WD
468 }
469
73a8b27c 470 ArpTimeoutCheck();
2d966958
WD
471
472 /*
473 * Check for a timeout, and run the timeout handler
474 * if we have one.
475 */
e0ac62d7 476 if (timeHandler && ((get_timer(0) - timeStart) > timeDelta)) {
2d966958
WD
477 thand_f *x;
478
643d1ab2 479#if defined(CONFIG_MII) || defined(CONFIG_CMD_MII)
4f63acd0
LC
480#if defined(CONFIG_SYS_FAULT_ECHO_LINK_DOWN) && \
481 defined(CONFIG_STATUS_LED) && \
482 defined(STATUS_LED_RED)
fc3e2165 483 /*
42d1f039 484 * Echo the inverted link state to the fault LED.
fc3e2165 485 */
4f63acd0 486 if (miiphy_link(eth_get_dev()->name,
3e38e429 487 CONFIG_SYS_FAULT_MII_ADDR)) {
4f63acd0 488 status_led_set(STATUS_LED_RED, STATUS_LED_OFF);
fc3e2165 489 } else {
4f63acd0 490 status_led_set(STATUS_LED_RED, STATUS_LED_ON);
fc3e2165 491 }
4f63acd0 492#endif /* CONFIG_SYS_FAULT_ECHO_LINK_DOWN, ... */
fc3e2165 493#endif /* CONFIG_MII, ... */
4ef8d53c 494 debug_cond(DEBUG_INT_STATE, "--- NetLoop timeout\n");
2d966958
WD
495 x = timeHandler;
496 timeHandler = (thand_f *)0;
497 (*x)();
498 }
499
500
22f6e99d 501 switch (net_state) {
2d966958
WD
502
503 case NETLOOP_RESTART:
2d966958 504 NetRestarted = 1;
2d966958
WD
505 goto restart;
506
507 case NETLOOP_SUCCESS:
ece223b5 508 net_cleanup_loop();
2d966958 509 if (NetBootFileXferSize > 0) {
f34024d4 510 char buf[20];
2d966958
WD
511 printf("Bytes transferred = %ld (%lx hex)\n",
512 NetBootFileXferSize,
513 NetBootFileXferSize);
f34024d4 514 sprintf(buf, "%lX", NetBootFileXferSize);
2d966958 515 setenv("filesize", buf);
a3d991bd
WD
516
517 sprintf(buf, "%lX", (unsigned long)load_addr);
518 setenv("fileaddr", buf);
2d966958
WD
519 }
520 eth_halt();
4793ee65 521 ret = NetBootFileXferSize;
4ef8d53c 522 debug_cond(DEBUG_INT_STATE, "--- NetLoop Success!\n");
4793ee65 523 goto done;
2d966958
WD
524
525 case NETLOOP_FAIL:
ece223b5 526 net_cleanup_loop();
4ef8d53c 527 debug_cond(DEBUG_INT_STATE, "--- NetLoop Fail!\n");
4793ee65 528 goto done;
22f6e99d
JH
529
530 case NETLOOP_CONTINUE:
531 continue;
2d966958
WD
532 }
533 }
4793ee65
SG
534
535done:
39bccd21 536#ifdef CONFIG_CMD_TFTPPUT
4793ee65 537 /* Clear out the handlers */
ece223b5 538 net_set_udp_handler(NULL);
4793ee65 539 net_set_icmp_handler(NULL);
39bccd21 540#endif
4793ee65 541 return ret;
2d966958
WD
542}
543
544/**********************************************************************/
545
546static void
547startAgainTimeout(void)
548{
22f6e99d 549 net_set_state(NETLOOP_RESTART);
2d966958
WD
550}
551
4f63acd0 552void NetStartAgain(void)
2d966958 553{
6e592385 554 char *nretry;
67b96e87
RB
555 int retry_forever = 0;
556 unsigned long retrycnt = 0;
557
558 nretry = getenv("netretry");
559 if (nretry) {
560 if (!strcmp(nretry, "yes"))
561 retry_forever = 1;
562 else if (!strcmp(nretry, "no"))
563 retrycnt = 0;
564 else if (!strcmp(nretry, "once"))
565 retrycnt = 1;
566 else
567 retrycnt = simple_strtoul(nretry, NULL, 0);
568 } else
569 retry_forever = 1;
570
571 if ((!retry_forever) && (NetTryCount >= retrycnt)) {
572 eth_halt();
22f6e99d 573 net_set_state(NETLOOP_FAIL);
a3d991bd
WD
574 return;
575 }
67b96e87
RB
576
577 NetTryCount++;
578
4f63acd0 579 eth_halt();
8b0c5c12 580#if !defined(CONFIG_NET_DO_NOT_TRY_ANOTHER)
4f63acd0 581 eth_try_another(!NetRestarted);
8b0c5c12 582#endif
4f63acd0 583 eth_init(gd->bd);
6e592385 584 if (NetRestartWrap) {
2d966958 585 NetRestartWrap = 0;
67b96e87 586 if (NetDevExists) {
4f63acd0 587 NetSetTimeout(10000UL, startAgainTimeout);
ece223b5 588 net_set_udp_handler(NULL);
6e592385 589 } else {
22f6e99d 590 net_set_state(NETLOOP_FAIL);
2d966958 591 }
6e592385 592 } else {
22f6e99d 593 net_set_state(NETLOOP_RESTART);
2d966958 594 }
2d966958
WD
595}
596
597/**********************************************************************/
598/*
599 * Miscelaneous bits.
600 */
601
ece223b5
JH
602static void dummy_handler(uchar *pkt, unsigned dport,
603 IPaddr_t sip, unsigned sport,
604 unsigned len)
d280d3f4 605{
d280d3f4
JH
606}
607
ece223b5
JH
608rxhand_f *net_get_udp_handler(void)
609{
610 return udp_packet_handler;
611}
d280d3f4 612
ece223b5
JH
613void net_set_udp_handler(rxhand_f *f)
614{
4ef8d53c 615 debug_cond(DEBUG_INT_STATE, "--- NetLoop UDP handler set (%p)\n", f);
ece223b5
JH
616 if (f == NULL)
617 udp_packet_handler = dummy_handler;
618 else
619 udp_packet_handler = f;
620}
621
622rxhand_f *net_get_arp_handler(void)
2d966958 623{
ece223b5
JH
624 return arp_packet_handler;
625}
626
627void net_set_arp_handler(rxhand_f *f)
628{
4ef8d53c 629 debug_cond(DEBUG_INT_STATE, "--- NetLoop ARP handler set (%p)\n", f);
ece223b5
JH
630 if (f == NULL)
631 arp_packet_handler = dummy_handler;
632 else
633 arp_packet_handler = f;
2d966958
WD
634}
635
39bccd21 636#ifdef CONFIG_CMD_TFTPPUT
4793ee65
SG
637void net_set_icmp_handler(rxhand_icmp_f *f)
638{
639 packet_icmp_handler = f;
640}
39bccd21 641#endif
2d966958
WD
642
643void
6b147d11 644NetSetTimeout(ulong iv, thand_f *f)
2d966958
WD
645{
646 if (iv == 0) {
4ef8d53c
JH
647 debug_cond(DEBUG_INT_STATE,
648 "--- NetLoop timeout handler cancelled\n");
2d966958
WD
649 timeHandler = (thand_f *)0;
650 } else {
4ef8d53c
JH
651 debug_cond(DEBUG_INT_STATE,
652 "--- NetLoop timeout handler set (%p)\n", f);
2d966958 653 timeHandler = f;
e0ac62d7 654 timeStart = get_timer(0);
1389f98f 655 timeDelta = iv * CONFIG_SYS_HZ / 1000;
2d966958
WD
656 }
657}
658
206d07fd
JH
659int NetSendUDPPacket(uchar *ether, IPaddr_t dest, int dport, int sport,
660 int payload_len)
73a8b27c 661{
a3d991bd 662 uchar *pkt;
9214637a
JH
663 int eth_hdr_size;
664 int pkt_hdr_size;
a3d991bd 665
46c495d5
JH
666 /* make sure the NetTxPacket is initialized (NetInit() was called) */
667 assert(NetTxPacket != NULL);
668 if (NetTxPacket == NULL)
669 return -1;
670
73a8b27c
WD
671 /* convert to new style broadcast */
672 if (dest == 0)
673 dest = 0xFFFFFFFF;
674
675 /* if broadcast, make the ether address a broadcast and don't do ARP */
676 if (dest == 0xFFFFFFFF)
677 ether = NetBcastAddr;
678
e94070c4 679 pkt = (uchar *)NetTxPacket;
9214637a
JH
680
681 eth_hdr_size = NetSetEther(pkt, ether, PROT_IP);
682 pkt += eth_hdr_size;
683 net_set_udp_header(pkt, dest, dport, sport, payload_len);
684 pkt_hdr_size = eth_hdr_size + IP_UDP_HDR_SIZE;
73a8b27c 685
e94070c4
JH
686 /* if MAC address was not discovered yet, do an ARP request */
687 if (memcmp(ether, NetEtherNullAddr, 6) == 0) {
4ef8d53c 688 debug_cond(DEBUG_DEV_PKT, "sending ARP for %pI4\n", &dest);
0ebf04c6 689
9214637a 690 /* save the ip and eth addr for the packet to send after arp */
73a8b27c
WD
691 NetArpWaitPacketIP = dest;
692 NetArpWaitPacketMAC = ether;
a3d991bd 693
73a8b27c 694 /* size of the waiting packet */
9214637a 695 NetArpWaitTxPacketSize = pkt_hdr_size + payload_len;
73a8b27c
WD
696
697 /* and do the ARP request */
698 NetArpWaitTry = 1;
699 NetArpWaitTimerStart = get_timer(0);
700 ArpRequest();
701 return 1; /* waiting */
9214637a 702 } else {
4ef8d53c
JH
703 debug_cond(DEBUG_DEV_PKT, "sending UDP to %pI4/%pM\n",
704 &dest, ether);
adf5d93e 705 NetSendPacket(NetTxPacket, pkt_hdr_size + payload_len);
9214637a 706 return 0; /* transmitted */
73a8b27c 707 }
73a8b27c
WD
708}
709
5cfaa4e5
AR
710#ifdef CONFIG_IP_DEFRAG
711/*
712 * This function collects fragments in a single packet, according
713 * to the algorithm in RFC815. It returns NULL or the pointer to
714 * a complete packet, in static storage
715 */
716#ifndef CONFIG_NET_MAXDEFRAG
717#define CONFIG_NET_MAXDEFRAG 16384
718#endif
719/*
720 * MAXDEFRAG, above, is chosen in the config file and is real data
721 * so we need to add the NFS overhead, which is more than TFTP.
722 * To use sizeof in the internal unnamed structures, we need a real
723 * instance (can't do "sizeof(struct rpc_t.u.reply))", unfortunately).
724 * The compiler doesn't complain nor allocates the actual structure
725 */
726static struct rpc_t rpc_specimen;
727#define IP_PKTSIZE (CONFIG_NET_MAXDEFRAG + sizeof(rpc_specimen.u.reply))
728
c5c59df0 729#define IP_MAXUDP (IP_PKTSIZE - IP_HDR_SIZE)
5cfaa4e5
AR
730
731/*
732 * this is the packet being assembled, either data or frag control.
733 * Fragments go by 8 bytes, so this union must be 8 bytes long
734 */
735struct hole {
736 /* first_byte is address of this structure */
737 u16 last_byte; /* last byte in this hole + 1 (begin of next hole) */
738 u16 next_hole; /* index of next (in 8-b blocks), 0 == none */
739 u16 prev_hole; /* index of prev, 0 == none */
740 u16 unused;
741};
742
594c26f8 743static struct ip_udp_hdr *__NetDefragment(struct ip_udp_hdr *ip, int *lenp)
5cfaa4e5 744{
48522bb5 745 static uchar pkt_buff[IP_PKTSIZE] __aligned(PKTALIGN);
5cfaa4e5
AR
746 static u16 first_hole, total_len;
747 struct hole *payload, *thisfrag, *h, *newh;
594c26f8 748 struct ip_udp_hdr *localip = (struct ip_udp_hdr *)pkt_buff;
5cfaa4e5
AR
749 uchar *indata = (uchar *)ip;
750 int offset8, start, len, done = 0;
751 u16 ip_off = ntohs(ip->ip_off);
752
753 /* payload starts after IP header, this fragment is in there */
c5c59df0 754 payload = (struct hole *)(pkt_buff + IP_HDR_SIZE);
5cfaa4e5
AR
755 offset8 = (ip_off & IP_OFFS);
756 thisfrag = payload + offset8;
757 start = offset8 * 8;
c5c59df0 758 len = ntohs(ip->ip_len) - IP_HDR_SIZE;
5cfaa4e5
AR
759
760 if (start + len > IP_MAXUDP) /* fragment extends too far */
761 return NULL;
762
763 if (!total_len || localip->ip_id != ip->ip_id) {
764 /* new (or different) packet, reset structs */
765 total_len = 0xffff;
766 payload[0].last_byte = ~0;
767 payload[0].next_hole = 0;
768 payload[0].prev_hole = 0;
769 first_hole = 0;
770 /* any IP header will work, copy the first we received */
c5c59df0 771 memcpy(localip, ip, IP_HDR_SIZE);
5cfaa4e5
AR
772 }
773
774 /*
775 * What follows is the reassembly algorithm. We use the payload
776 * array as a linked list of hole descriptors, as each hole starts
777 * at a multiple of 8 bytes. However, last byte can be whatever value,
778 * so it is represented as byte count, not as 8-byte blocks.
779 */
780
781 h = payload + first_hole;
782 while (h->last_byte < start) {
783 if (!h->next_hole) {
784 /* no hole that far away */
785 return NULL;
786 }
787 h = payload + h->next_hole;
788 }
789
e397e59e
FS
790 /* last fragment may be 1..7 bytes, the "+7" forces acceptance */
791 if (offset8 + ((len + 7) / 8) <= h - payload) {
5cfaa4e5
AR
792 /* no overlap with holes (dup fragment?) */
793 return NULL;
794 }
795
796 if (!(ip_off & IP_FLAGS_MFRAG)) {
797 /* no more fragmentss: truncate this (last) hole */
798 total_len = start + len;
799 h->last_byte = start + len;
800 }
801
802 /*
803 * There is some overlap: fix the hole list. This code doesn't
804 * deal with a fragment that overlaps with two different holes
805 * (thus being a superset of a previously-received fragment).
806 */
807
4f63acd0 808 if ((h >= thisfrag) && (h->last_byte <= start + len)) {
5cfaa4e5
AR
809 /* complete overlap with hole: remove hole */
810 if (!h->prev_hole && !h->next_hole) {
811 /* last remaining hole */
812 done = 1;
813 } else if (!h->prev_hole) {
814 /* first hole */
815 first_hole = h->next_hole;
816 payload[h->next_hole].prev_hole = 0;
817 } else if (!h->next_hole) {
818 /* last hole */
819 payload[h->prev_hole].next_hole = 0;
820 } else {
821 /* in the middle of the list */
822 payload[h->next_hole].prev_hole = h->prev_hole;
823 payload[h->prev_hole].next_hole = h->next_hole;
824 }
825
826 } else if (h->last_byte <= start + len) {
827 /* overlaps with final part of the hole: shorten this hole */
828 h->last_byte = start;
829
830 } else if (h >= thisfrag) {
831 /* overlaps with initial part of the hole: move this hole */
832 newh = thisfrag + (len / 8);
833 *newh = *h;
834 h = newh;
835 if (h->next_hole)
836 payload[h->next_hole].prev_hole = (h - payload);
837 if (h->prev_hole)
838 payload[h->prev_hole].next_hole = (h - payload);
839 else
840 first_hole = (h - payload);
841
842 } else {
843 /* fragment sits in the middle: split the hole */
844 newh = thisfrag + (len / 8);
845 *newh = *h;
846 h->last_byte = start;
847 h->next_hole = (newh - payload);
848 newh->prev_hole = (h - payload);
849 if (newh->next_hole)
850 payload[newh->next_hole].prev_hole = (newh - payload);
851 }
852
853 /* finally copy this fragment and possibly return whole packet */
c5c59df0 854 memcpy((uchar *)thisfrag, indata + IP_HDR_SIZE, len);
5cfaa4e5
AR
855 if (!done)
856 return NULL;
857
858 localip->ip_len = htons(total_len);
c5c59df0 859 *lenp = total_len + IP_HDR_SIZE;
5cfaa4e5
AR
860 return localip;
861}
862
594c26f8 863static inline struct ip_udp_hdr *NetDefragment(struct ip_udp_hdr *ip, int *lenp)
5cfaa4e5
AR
864{
865 u16 ip_off = ntohs(ip->ip_off);
866 if (!(ip_off & (IP_OFFS | IP_FLAGS_MFRAG)))
867 return ip; /* not a fragment */
868 return __NetDefragment(ip, lenp);
869}
870
871#else /* !CONFIG_IP_DEFRAG */
872
594c26f8 873static inline struct ip_udp_hdr *NetDefragment(struct ip_udp_hdr *ip, int *lenp)
5cfaa4e5
AR
874{
875 u16 ip_off = ntohs(ip->ip_off);
876 if (!(ip_off & (IP_OFFS | IP_FLAGS_MFRAG)))
877 return ip; /* not a fragment */
878 return NULL;
879}
880#endif
a3d991bd 881
8f79bb17
SG
882/**
883 * Receive an ICMP packet. We deal with REDIRECT and PING here, and silently
884 * drop others.
885 *
886 * @parma ip IP packet containing the ICMP
887 */
594c26f8 888static void receive_icmp(struct ip_udp_hdr *ip, int len,
cb487f56 889 IPaddr_t src_ip, struct ethernet_hdr *et)
8f79bb17 890{
e0a63079 891 struct icmp_hdr *icmph = (struct icmp_hdr *)&ip->udp_src;
8f79bb17
SG
892
893 switch (icmph->type) {
894 case ICMP_REDIRECT:
895 if (icmph->code != ICMP_REDIR_HOST)
896 return;
897 printf(" ICMP Host Redirect to %pI4 ",
898 &icmph->un.gateway);
899 break;
a36b12f9 900 default:
8f79bb17 901#if defined(CONFIG_CMD_PING)
a36b12f9 902 ping_receive(et, ip, len);
8f79bb17 903#endif
39bccd21 904#ifdef CONFIG_CMD_TFTPPUT
4793ee65
SG
905 if (packet_icmp_handler)
906 packet_icmp_handler(icmph->type, icmph->code,
907 ntohs(ip->udp_dst), src_ip, ntohs(ip->udp_src),
908 icmph->un.data, ntohs(ip->udp_len));
39bccd21 909#endif
8f79bb17
SG
910 break;
911 }
912}
913
2d966958 914void
db288a96 915NetReceive(uchar *inpkt, int len)
2d966958 916{
cb487f56 917 struct ethernet_hdr *et;
594c26f8 918 struct ip_udp_hdr *ip;
8d353eb8 919 IPaddr_t dst_ip;
03eb129f 920 IPaddr_t src_ip;
8d353eb8 921 int eth_proto;
643d1ab2 922#if defined(CONFIG_CMD_CDP)
a3d991bd
WD
923 int iscdp;
924#endif
925 ushort cti = 0, vlanid = VLAN_NONE, myvlanid, mynvlanid;
926
4ef8d53c 927 debug_cond(DEBUG_NET_PKT, "packet received\n");
2d966958 928
d9bec9f4
MF
929 NetRxPacket = inpkt;
930 NetRxPacketLen = len;
cb487f56 931 et = (struct ethernet_hdr *)inpkt;
a3d991bd
WD
932
933 /* too small packet? */
934 if (len < ETHER_HDR_SIZE)
935 return;
936
f85b6071
RJ
937#ifdef CONFIG_API
938 if (push_packet) {
939 (*push_packet)(inpkt, len);
940 return;
941 }
942#endif
943
643d1ab2 944#if defined(CONFIG_CMD_CDP)
a3d991bd 945 /* keep track if packet is CDP */
17351883 946 iscdp = is_cdp_packet(et->et_dest);
a3d991bd
WD
947#endif
948
949 myvlanid = ntohs(NetOurVLAN);
950 if (myvlanid == (ushort)-1)
951 myvlanid = VLAN_NONE;
952 mynvlanid = ntohs(NetOurNativeVLAN);
953 if (mynvlanid == (ushort)-1)
954 mynvlanid = VLAN_NONE;
2d966958 955
8d353eb8 956 eth_proto = ntohs(et->et_protlen);
2d966958 957
8d353eb8 958 if (eth_proto < 1514) {
cb487f56 959 struct e802_hdr *et802 = (struct e802_hdr *)et;
2d966958 960 /*
da5ebe2c
JH
961 * Got a 802.2 packet. Check the other protocol field.
962 * XXX VLAN over 802.2+SNAP not implemented!
2d966958 963 */
8d353eb8 964 eth_proto = ntohs(et802->et_prot);
a3d991bd 965
594c26f8 966 ip = (struct ip_udp_hdr *)(inpkt + E802_HDR_SIZE);
2d966958 967 len -= E802_HDR_SIZE;
a3d991bd 968
8d353eb8 969 } else if (eth_proto != PROT_VLAN) { /* normal packet */
594c26f8 970 ip = (struct ip_udp_hdr *)(inpkt + ETHER_HDR_SIZE);
2d966958 971 len -= ETHER_HDR_SIZE;
a3d991bd
WD
972
973 } else { /* VLAN packet */
c68cca35
JH
974 struct vlan_ethernet_hdr *vet =
975 (struct vlan_ethernet_hdr *)et;
a3d991bd 976
4ef8d53c 977 debug_cond(DEBUG_NET_PKT, "VLAN packet received\n");
0ebf04c6 978
a3d991bd
WD
979 /* too small packet? */
980 if (len < VLAN_ETHER_HDR_SIZE)
981 return;
982
983 /* if no VLAN active */
984 if ((ntohs(NetOurVLAN) & VLAN_IDMASK) == VLAN_NONE
643d1ab2 985#if defined(CONFIG_CMD_CDP)
a3d991bd
WD
986 && iscdp == 0
987#endif
988 )
989 return;
990
991 cti = ntohs(vet->vet_tag);
992 vlanid = cti & VLAN_IDMASK;
8d353eb8 993 eth_proto = ntohs(vet->vet_type);
a3d991bd 994
594c26f8 995 ip = (struct ip_udp_hdr *)(inpkt + VLAN_ETHER_HDR_SIZE);
a3d991bd 996 len -= VLAN_ETHER_HDR_SIZE;
2d966958
WD
997 }
998
4ef8d53c 999 debug_cond(DEBUG_NET_PKT, "Receive from protocol 0x%x\n", eth_proto);
2d966958 1000
643d1ab2 1001#if defined(CONFIG_CMD_CDP)
a3d991bd 1002 if (iscdp) {
0b4c5ff4 1003 cdp_receive((uchar *)ip, len);
a3d991bd
WD
1004 return;
1005 }
1006#endif
1007
1008 if ((myvlanid & VLAN_IDMASK) != VLAN_NONE) {
1009 if (vlanid == VLAN_NONE)
1010 vlanid = (mynvlanid & VLAN_IDMASK);
1011 /* not matched? */
1012 if (vlanid != (myvlanid & VLAN_IDMASK))
1013 return;
1014 }
1015
8d353eb8 1016 switch (eth_proto) {
2d966958
WD
1017
1018 case PROT_ARP:
d280d3f4 1019 ArpReceive(et, ip, len);
289f932c 1020 break;
2d966958 1021
bf6cb247 1022#ifdef CONFIG_CMD_RARP
2d966958 1023 case PROT_RARP:
8b9c5322 1024 rarp_receive(ip, len);
2d966958 1025 break;
bf6cb247 1026#endif
2d966958 1027 case PROT_IP:
4ef8d53c 1028 debug_cond(DEBUG_NET_PKT, "Got IP\n");
5cfaa4e5 1029 /* Before we start poking the header, make sure it is there */
594c26f8
JH
1030 if (len < IP_UDP_HDR_SIZE) {
1031 debug("len bad %d < %lu\n", len,
1032 (ulong)IP_UDP_HDR_SIZE);
2d966958
WD
1033 return;
1034 }
5cfaa4e5 1035 /* Check the packet length */
2d966958 1036 if (len < ntohs(ip->ip_len)) {
4ef8d53c 1037 debug("len bad %d < %d\n", len, ntohs(ip->ip_len));
2d966958
WD
1038 return;
1039 }
1040 len = ntohs(ip->ip_len);
4ef8d53c
JH
1041 debug_cond(DEBUG_NET_PKT, "len=%d, v=%02x\n",
1042 len, ip->ip_hl_v & 0xff);
0ebf04c6 1043
5cfaa4e5 1044 /* Can't deal with anything except IPv4 */
d3c65b01 1045 if ((ip->ip_hl_v & 0xf0) != 0x40)
2d966958 1046 return;
5cfaa4e5 1047 /* Can't deal with IP options (headers != 20 bytes) */
d3c65b01 1048 if ((ip->ip_hl_v & 0x0f) > 0x05)
6b52cfe1 1049 return;
5cfaa4e5 1050 /* Check the Checksum of the header */
c5c59df0 1051 if (!NetCksumOk((uchar *)ip, IP_HDR_SIZE / 2)) {
4ef8d53c 1052 debug("checksum bad\n");
2d966958
WD
1053 return;
1054 }
5cfaa4e5 1055 /* If it is not for us, ignore it */
8d353eb8
JH
1056 dst_ip = NetReadIP(&ip->ip_dst);
1057 if (NetOurIP && dst_ip != NetOurIP && dst_ip != 0xFFFFFFFF) {
53a5c424 1058#ifdef CONFIG_MCAST_TFTP
8d353eb8 1059 if (Mcast_addr != dst_ip)
53a5c424 1060#endif
c819abee 1061 return;
2d966958 1062 }
03eb129f
LC
1063 /* Read source IP address for later use */
1064 src_ip = NetReadIP(&ip->ip_src);
5cfaa4e5
AR
1065 /*
1066 * The function returns the unchanged packet if it's not
1067 * a fragment, and either the complete packet or NULL if
1068 * it is a fragment (if !CONFIG_IP_DEFRAG, it returns NULL)
1069 */
ccb9ebef
LC
1070 ip = NetDefragment(ip, &len);
1071 if (!ip)
5cfaa4e5 1072 return;
2d966958
WD
1073 /*
1074 * watch for ICMP host redirects
1075 *
8bde7f77
WD
1076 * There is no real handler code (yet). We just watch
1077 * for ICMP host redirect messages. In case anybody
1078 * sees these messages: please contact me
1079 * (wd@denx.de), or - even better - send me the
1080 * necessary fixes :-)
2d966958 1081 *
8bde7f77
WD
1082 * Note: in all cases where I have seen this so far
1083 * it was a problem with the router configuration,
1084 * for instance when a router was configured in the
1085 * BOOTP reply, but the TFTP server was on the same
1086 * subnet. So this is probably a warning that your
1087 * configuration might be wrong. But I'm not really
1088 * sure if there aren't any other situations.
4793ee65
SG
1089 *
1090 * Simon Glass <sjg@chromium.org>: We get an ICMP when
1091 * we send a tftp packet to a dead connection, or when
1092 * there is no server at the other end.
2d966958
WD
1093 */
1094 if (ip->ip_p == IPPROTO_ICMP) {
8f79bb17
SG
1095 receive_icmp(ip, len, src_ip, et);
1096 return;
2d966958
WD
1097 } else if (ip->ip_p != IPPROTO_UDP) { /* Only UDP packets */
1098 return;
1099 }
1100
4ef8d53c
JH
1101 debug_cond(DEBUG_DEV_PKT,
1102 "received UDP (to=%pI4, from=%pI4, len=%d)\n",
1103 &dst_ip, &src_ip, len);
1104
8534bf9a
SR
1105#ifdef CONFIG_UDP_CHECKSUM
1106 if (ip->udp_xsum != 0) {
b2f50807 1107 ulong xsum;
8534bf9a
SR
1108 ushort *sumptr;
1109 ushort sumlen;
1110
1111 xsum = ip->ip_p;
1112 xsum += (ntohs(ip->udp_len));
1113 xsum += (ntohl(ip->ip_src) >> 16) & 0x0000ffff;
1114 xsum += (ntohl(ip->ip_src) >> 0) & 0x0000ffff;
1115 xsum += (ntohl(ip->ip_dst) >> 16) & 0x0000ffff;
1116 xsum += (ntohl(ip->ip_dst) >> 0) & 0x0000ffff;
1117
1118 sumlen = ntohs(ip->udp_len);
1119 sumptr = (ushort *) &(ip->udp_src);
1120
1121 while (sumlen > 1) {
b2f50807 1122 ushort sumdata;
8534bf9a
SR
1123
1124 sumdata = *sumptr++;
1125 xsum += ntohs(sumdata);
1126 sumlen -= 2;
1127 }
1128 if (sumlen > 0) {
b2f50807 1129 ushort sumdata;
8534bf9a
SR
1130
1131 sumdata = *(unsigned char *) sumptr;
b2f50807 1132 sumdata = (sumdata << 8) & 0xff00;
8534bf9a
SR
1133 xsum += sumdata;
1134 }
1135 while ((xsum >> 16) != 0) {
3e38e429
LC
1136 xsum = (xsum & 0x0000ffff) +
1137 ((xsum >> 16) & 0x0000ffff);
8534bf9a
SR
1138 }
1139 if ((xsum != 0x00000000) && (xsum != 0x0000ffff)) {
9b55a253
WD
1140 printf(" UDP wrong checksum %08lx %08x\n",
1141 xsum, ntohs(ip->udp_xsum));
8534bf9a
SR
1142 return;
1143 }
1144 }
1145#endif
1146
53a5c424 1147
68ceb29e 1148#ifdef CONFIG_NETCONSOLE
594c26f8
JH
1149 nc_input_packet((uchar *)ip + IP_UDP_HDR_SIZE,
1150 ntohs(ip->udp_dst),
1151 ntohs(ip->udp_src),
1152 ntohs(ip->udp_len) - UDP_HDR_SIZE);
68ceb29e 1153#endif
2d966958
WD
1154 /*
1155 * IP header OK. Pass the packet to the current handler.
1156 */
ece223b5
JH
1157 (*udp_packet_handler)((uchar *)ip + IP_UDP_HDR_SIZE,
1158 ntohs(ip->udp_dst),
1159 src_ip,
1160 ntohs(ip->udp_src),
1161 ntohs(ip->udp_len) - UDP_HDR_SIZE);
2d966958
WD
1162 break;
1163 }
1164}
1165
1166
1167/**********************************************************************/
1168
e4bf0c5c 1169static int net_check_prereq(enum proto_t protocol)
2d966958
WD
1170{
1171 switch (protocol) {
6e592385 1172 /* Fall through */
643d1ab2 1173#if defined(CONFIG_CMD_PING)
73a8b27c 1174 case PING:
6e592385 1175 if (NetPingIP == 0) {
4f63acd0 1176 puts("*** ERROR: ping address not given\n");
92895de9 1177 return 1;
6e592385
WD
1178 }
1179 goto common;
cbd8a35c 1180#endif
643d1ab2 1181#if defined(CONFIG_CMD_SNTP)
ea287deb
WD
1182 case SNTP:
1183 if (NetNtpServerIP == 0) {
4f63acd0 1184 puts("*** ERROR: NTP server address not given\n");
92895de9 1185 return 1;
ea287deb
WD
1186 }
1187 goto common;
1188#endif
1a32bf41
RG
1189#if defined(CONFIG_CMD_DNS)
1190 case DNS:
1191 if (NetOurDNSIP == 0) {
1192 puts("*** ERROR: DNS server address not given\n");
1193 return 1;
1194 }
1195 goto common;
1196#endif
643d1ab2 1197#if defined(CONFIG_CMD_NFS)
cbd8a35c 1198 case NFS:
73a8b27c 1199#endif
e4bf0c5c 1200 case TFTPGET:
1fb7cd49 1201 case TFTPPUT:
6e592385 1202 if (NetServerIP == 0) {
4f63acd0 1203 puts("*** ERROR: `serverip' not set\n");
92895de9 1204 return 1;
6e592385 1205 }
4f63acd0
LC
1206#if defined(CONFIG_CMD_PING) || defined(CONFIG_CMD_SNTP) || \
1207 defined(CONFIG_CMD_DNS)
1208common:
73a8b27c 1209#endif
8b6bbe10 1210 /* Fall through */
73a8b27c 1211
8b6bbe10 1212 case NETCONS:
7a83af07 1213 case TFTPSRV:
6e592385 1214 if (NetOurIP == 0) {
4f63acd0 1215 puts("*** ERROR: `ipaddr' not set\n");
92895de9 1216 return 1;
6e592385
WD
1217 }
1218 /* Fall through */
2d966958 1219
bf6cb247 1220#ifdef CONFIG_CMD_RARP
2d966958 1221 case RARP:
bf6cb247 1222#endif
2d966958 1223 case BOOTP:
a3d991bd 1224 case CDP:
bf6cb247 1225 case DHCP:
d22c338e 1226 case LINKLOCAL:
4f63acd0 1227 if (memcmp(NetOurEther, "\0\0\0\0\0\0", 6) == 0) {
4f63acd0 1228 int num = eth_get_dev_index();
2d966958 1229
6e592385
WD
1230 switch (num) {
1231 case -1:
4f63acd0 1232 puts("*** ERROR: No ethernet found.\n");
92895de9 1233 return 1;
6e592385 1234 case 0:
4f63acd0 1235 puts("*** ERROR: `ethaddr' not set\n");
2d966958 1236 break;
6e592385 1237 default:
4f63acd0 1238 printf("*** ERROR: `eth%daddr' not set\n",
2d966958
WD
1239 num);
1240 break;
6e592385 1241 }
2d966958 1242
4f63acd0 1243 NetStartAgain();
92895de9 1244 return 2;
6e592385
WD
1245 }
1246 /* Fall through */
1247 default:
92895de9 1248 return 0;
2d966958 1249 }
92895de9 1250 return 0; /* OK */
2d966958
WD
1251}
1252/**********************************************************************/
1253
1254int
6b147d11 1255NetCksumOk(uchar *ptr, int len)
2d966958
WD
1256{
1257 return !((NetCksum(ptr, len) + 1) & 0xfffe);
1258}
1259
1260
1261unsigned
6b147d11 1262NetCksum(uchar *ptr, int len)
2d966958
WD
1263{
1264 ulong xsum;
9d2a873b 1265 ushort *p = (ushort *)ptr;
2d966958
WD
1266
1267 xsum = 0;
1268 while (len-- > 0)
7bc5ee07 1269 xsum += *p++;
2d966958
WD
1270 xsum = (xsum & 0xffff) + (xsum >> 16);
1271 xsum = (xsum & 0xffff) + (xsum >> 16);
92895de9 1272 return xsum & 0xffff;
2d966958
WD
1273}
1274
a3d991bd
WD
1275int
1276NetEthHdrSize(void)
1277{
1278 ushort myvlanid;
2d966958 1279
a3d991bd
WD
1280 myvlanid = ntohs(NetOurVLAN);
1281 if (myvlanid == (ushort)-1)
1282 myvlanid = VLAN_NONE;
1283
3e38e429
LC
1284 return ((myvlanid & VLAN_IDMASK) == VLAN_NONE) ? ETHER_HDR_SIZE :
1285 VLAN_ETHER_HDR_SIZE;
a3d991bd
WD
1286}
1287
1288int
db288a96 1289NetSetEther(uchar *xet, uchar * addr, uint prot)
2d966958 1290{
cb487f56 1291 struct ethernet_hdr *et = (struct ethernet_hdr *)xet;
a3d991bd
WD
1292 ushort myvlanid;
1293
1294 myvlanid = ntohs(NetOurVLAN);
1295 if (myvlanid == (ushort)-1)
1296 myvlanid = VLAN_NONE;
2d966958 1297
4f63acd0
LC
1298 memcpy(et->et_dest, addr, 6);
1299 memcpy(et->et_src, NetOurEther, 6);
a3d991bd 1300 if ((myvlanid & VLAN_IDMASK) == VLAN_NONE) {
c819abee 1301 et->et_protlen = htons(prot);
a3d991bd
WD
1302 return ETHER_HDR_SIZE;
1303 } else {
c68cca35
JH
1304 struct vlan_ethernet_hdr *vet =
1305 (struct vlan_ethernet_hdr *)xet;
2d966958 1306
a3d991bd
WD
1307 vet->vet_vlan_type = htons(PROT_VLAN);
1308 vet->vet_tag = htons((0 << 5) | (myvlanid & VLAN_IDMASK));
1309 vet->vet_type = htons(prot);
1310 return VLAN_ETHER_HDR_SIZE;
1311 }
1312}
2d966958 1313
e7111015
JH
1314int net_update_ether(struct ethernet_hdr *et, uchar *addr, uint prot)
1315{
1316 ushort protlen;
1317
1318 memcpy(et->et_dest, addr, 6);
1319 memcpy(et->et_src, NetOurEther, 6);
1320 protlen = ntohs(et->et_protlen);
1321 if (protlen == PROT_VLAN) {
1322 struct vlan_ethernet_hdr *vet =
1323 (struct vlan_ethernet_hdr *)et;
1324 vet->vet_type = htons(prot);
1325 return VLAN_ETHER_HDR_SIZE;
1326 } else if (protlen > 1514) {
1327 et->et_protlen = htons(prot);
1328 return ETHER_HDR_SIZE;
1329 } else {
1330 /* 802.2 + SNAP */
1331 struct e802_hdr *et802 = (struct e802_hdr *)et;
1332 et802->et_prot = htons(prot);
1333 return E802_HDR_SIZE;
1334 }
1335}
1336
4b11c916 1337void net_set_ip_header(uchar *pkt, IPaddr_t dest, IPaddr_t source)
2d966958 1338{
4b11c916 1339 struct ip_udp_hdr *ip = (struct ip_udp_hdr *)pkt;
2d966958
WD
1340
1341 /*
4b11c916 1342 * Construct an IP header.
2d966958 1343 */
3e38e429
LC
1344 /* IP_HDR_SIZE / 4 (not including UDP) */
1345 ip->ip_hl_v = 0x45;
2d966958 1346 ip->ip_tos = 0;
4b11c916 1347 ip->ip_len = htons(IP_HDR_SIZE);
2d966958 1348 ip->ip_id = htons(NetIPID++);
e0c07b86 1349 ip->ip_off = htons(IP_FLAGS_DFRAG); /* Don't fragment */
2d966958 1350 ip->ip_ttl = 255;
2d966958 1351 ip->ip_sum = 0;
3e38e429 1352 /* already in network byte order */
4b11c916
JH
1353 NetCopyIP((void *)&ip->ip_src, &source);
1354 /* already in network byte order */
6b147d11 1355 NetCopyIP((void *)&ip->ip_dst, &dest);
4b11c916
JH
1356}
1357
1358void net_set_udp_header(uchar *pkt, IPaddr_t dest, int dport, int sport,
1359 int len)
1360{
1361 struct ip_udp_hdr *ip = (struct ip_udp_hdr *)pkt;
1362
1363 /*
1364 * If the data is an odd number of bytes, zero the
1365 * byte after the last byte so that the checksum
1366 * will work.
1367 */
1368 if (len & 1)
1369 pkt[IP_UDP_HDR_SIZE + len] = 0;
1370
1371 net_set_ip_header(pkt, dest, NetOurIP);
1372 ip->ip_len = htons(IP_UDP_HDR_SIZE + len);
1373 ip->ip_p = IPPROTO_UDP;
1374 ip->ip_sum = ~NetCksum((uchar *)ip, IP_HDR_SIZE >> 1);
1375
2d966958
WD
1376 ip->udp_src = htons(sport);
1377 ip->udp_dst = htons(dport);
594c26f8 1378 ip->udp_len = htons(UDP_HDR_SIZE + len);
2d966958 1379 ip->udp_xsum = 0;
2d966958
WD
1380}
1381
4f63acd0 1382void copy_filename(char *dst, const char *src, int size)
2d966958
WD
1383{
1384 if (*src && (*src == '"')) {
1385 ++src;
1386 --size;
1387 }
1388
d3c65b01 1389 while ((--size > 0) && *src && (*src != '"'))
2d966958 1390 *dst++ = *src++;
2d966958
WD
1391 *dst = '\0';
1392}
1393
3e38e429
LC
1394#if defined(CONFIG_CMD_NFS) || \
1395 defined(CONFIG_CMD_SNTP) || \
1396 defined(CONFIG_CMD_DNS)
1a32bf41 1397/*
9739946c
RG
1398 * make port a little random (1024-17407)
1399 * This keeps the math somewhat trivial to compute, and seems to work with
1400 * all supported protocols/clients/servers
1a32bf41
RG
1401 */
1402unsigned int random_port(void)
1403{
9739946c 1404 return 1024 + (get_timer(0) % 0x4000);
1a32bf41
RG
1405}
1406#endif
1407
4f63acd0 1408void ip_to_string(IPaddr_t x, char *s)
2d966958 1409{
4f63acd0
LC
1410 x = ntohl(x);
1411 sprintf(s, "%d.%d.%d.%d",
1412 (int) ((x >> 24) & 0xff),
1413 (int) ((x >> 16) & 0xff),
1414 (int) ((x >> 8) & 0xff), (int) ((x >> 0) & 0xff)
6e592385 1415 );
2d966958
WD
1416}
1417
a3d991bd
WD
1418void VLAN_to_string(ushort x, char *s)
1419{
1420 x = ntohs(x);
1421
1422 if (x == (ushort)-1)
1423 x = VLAN_NONE;
1424
1425 if (x == VLAN_NONE)
1426 strcpy(s, "none");
1427 else
1428 sprintf(s, "%d", x & VLAN_IDMASK);
1429}
1430
2e3ef6e4 1431ushort string_to_VLAN(const char *s)
a3d991bd
WD
1432{
1433 ushort id;
1434
1435 if (s == NULL)
b9711de1 1436 return htons(VLAN_NONE);
a3d991bd
WD
1437
1438 if (*s < '0' || *s > '9')
1439 id = VLAN_NONE;
1440 else
1441 id = (ushort)simple_strtoul(s, NULL, 10);
1442
b9711de1 1443 return htons(id);
a3d991bd
WD
1444}
1445
a3d991bd
WD
1446ushort getenv_VLAN(char *var)
1447{
92895de9 1448 return string_to_VLAN(getenv(var));
a3d991bd 1449}