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