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