]> git.ipfire.org Git - thirdparty/u-boot.git/blame - net/net.c
net: Remove <common.h> and add needed includes
[thirdparty/u-boot.git] / net / net.c
CommitLineData
f739fcd8 1// SPDX-License-Identifier: GPL-2.0
2d966958
WD
2/*
3 * Copied from Linux Monitor (LiMon) - Networking.
4 *
5 * Copyright 1994 - 2000 Neil Russell.
6 * (See License)
7 * Copyright 2000 Roland Borde
8 * Copyright 2000 Paolo Scaffardi
9 * Copyright 2000-2002 Wolfgang Denk, wd@denx.de
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 *
6de98b60 27 * LINKLOCAL:
d22c338e
JH
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 74 *
d8970dae
LF
75 *
76 * WOL:
77 *
78 * Prerequisites: - own ethernet address
79 * We want: - magic packet or timeout
80 * Next step: none
2d966958
WD
81 */
82
83
52f24238 84#include <bootstage.h>
2d966958 85#include <command.h>
24b852a7 86#include <console.h>
c7694dd4 87#include <env.h>
f3998fdc 88#include <env_internal.h>
60304592 89#include <errno.h>
8e8ccfe1 90#include <image.h>
f7ae49fc 91#include <log.h>
2d966958 92#include <net.h>
ffdbf3ba
VM
93#include <net6.h>
94#include <ndisc.h>
443d3191
DM
95#include <net/fastboot_udp.h>
96#include <net/fastboot_tcp.h>
34696958 97#include <net/tftp.h>
09bd3d0b 98#include <net/ncsi.h>
3eaac630
RF
99#if defined(CONFIG_CMD_PCAP)
100#include <net/pcap.h>
101#endif
b43ea1bf 102#include <net/udp.h>
2d8d190c 103#if defined(CONFIG_LED_STATUS)
fc3e2165 104#include <miiphy.h>
4545f4e6 105#include <status_led.h>
fc3e2165 106#endif
4545f4e6
JH
107#include <watchdog.h>
108#include <linux/compiler.h>
f43b2df3 109#include <test/test.h>
a0245818
SE
110#include <net/tcp.h>
111#include <net/wget.h>
4545f4e6
JH
112#include "arp.h"
113#include "bootp.h"
f575ae1f 114#include "cdp.h"
1a32bf41
RG
115#if defined(CONFIG_CMD_DNS)
116#include "dns.h"
117#endif
d22c338e 118#include "link_local.h"
4545f4e6 119#include "nfs.h"
a36b12f9 120#include "ping.h"
4545f4e6 121#include "rarp.h"
d8970dae
LF
122#if defined(CONFIG_CMD_WOL)
123#include "wol.h"
124#endif
a0245818 125#include "dhcpv6.h"
6de98b60 126#include "net_rand.h"
2d966958 127
2d966958
WD
128/** BOOTP EXTENTIONS **/
129
3e38e429 130/* Our subnet mask (0=unknown) */
049a95a7 131struct in_addr net_netmask;
3e38e429 132/* Our gateways IP address */
049a95a7 133struct in_addr net_gateway;
3e38e429 134/* Our DNS IP address */
049a95a7 135struct in_addr net_dns_server;
1fe80d79 136#if defined(CONFIG_BOOTP_DNS2)
3e38e429 137/* Our 2nd DNS IP address */
049a95a7 138struct in_addr net_dns_server2;
3e38e429 139#endif
a0245818
SE
140/* Indicates whether the pxe path prefix / config file was specified in dhcp option */
141char *pxelinux_configfile;
2d966958
WD
142
143/** END OF BOOTP EXTENTIONS **/
144
3e38e429 145/* Our ethernet address */
0adb5b76 146u8 net_ethaddr[6];
3e38e429 147/* Boot server enet address */
0adb5b76 148u8 net_server_ethaddr[6];
3e38e429 149/* Our IP addr (0 = unknown) */
049a95a7 150struct in_addr net_ip;
3e38e429 151/* Server IP addr (0 = unknown) */
049a95a7 152struct in_addr net_server_ip;
3e38e429 153/* Current receive packet */
1203fcce 154uchar *net_rx_packet;
3e38e429 155/* Current rx packet length */
1203fcce 156int net_rx_packet_len;
3e38e429 157/* IP packet ID */
bc0571fc 158static unsigned net_ip_id;
3e38e429 159/* Ethernet bcast address */
0adb5b76
JH
160const u8 net_bcast_ethaddr[6] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
161const u8 net_null_ethaddr[6];
0efe1bcf 162#if defined(CONFIG_API) || defined(CONFIG_EFI_LOADER)
bc0571fc 163void (*push_packet)(void *, int len) = 0;
f85b6071 164#endif
3e38e429 165/* Network loop state */
22f6e99d 166enum net_loop_state net_state;
3e38e429 167/* Tried all network devices */
bc0571fc 168int net_restart_wrap;
3e38e429 169/* Network loop restarted */
bc0571fc 170static int net_restarted;
3e38e429 171/* At least one device configured */
bc0571fc 172static int net_dev_exists;
2d966958 173
6e592385 174/* XXX in both little & big endian machines 0xFFFF == ntohs(-1) */
3e38e429 175/* default is without VLAN */
4fd5055f 176ushort net_our_vlan = 0xFFFF;
3e38e429 177/* ditto */
4fd5055f 178ushort net_native_vlan = 0xFFFF;
a3d991bd 179
3e38e429 180/* Boot File name */
11a69ff8 181char net_boot_file_name[1024];
449312c1
AG
182/* Indicates whether the file name was specified on the command line */
183bool net_boot_file_name_explicit;
1411157d
JH
184/* The actual transferred size of the bootfile (in bytes) */
185u32 net_boot_file_size;
186/* Boot file size in blocks as reported by the DHCP server */
187u32 net_boot_file_expected_size_in_blocks;
2d966958 188
1203fcce 189static uchar net_pkt_buf[(PKTBUFSRX+1) * PKTSIZE_ALIGN + PKTALIGN];
2a504df0
JH
190/* Receive packets */
191uchar *net_rx_packets[PKTBUFSRX];
ece223b5
JH
192/* Current UDP RX packet handler */
193static rxhand_f *udp_packet_handler;
194/* Current ARP RX packet handler */
195static rxhand_f *arp_packet_handler;
39bccd21 196#ifdef CONFIG_CMD_TFTPPUT
ece223b5
JH
197/* Current ICMP rx handler */
198static rxhand_icmp_f *packet_icmp_handler;
39bccd21 199#endif
3e38e429 200/* Current timeout handler */
bc0571fc 201static thand_f *time_handler;
3e38e429 202/* Time base value */
bc0571fc 203static ulong time_start;
3e38e429 204/* Current timeout value */
bc0571fc 205static ulong time_delta;
3e38e429 206/* THE transmit packet */
1203fcce 207uchar *net_tx_packet;
2d966958 208
e4bf0c5c 209static int net_check_prereq(enum proto_t protocol);
2d966958 210
bc0571fc 211static int net_try_count;
67b96e87 212
b63056d6
JL
213int __maybe_unused net_busy_flag;
214
73a8b27c
WD
215/**********************************************************************/
216
fd305633
JH
217static int on_ipaddr(const char *name, const char *value, enum env_op op,
218 int flags)
219{
220 if (flags & H_PROGRAMMATIC)
221 return 0;
222
223 net_ip = string_to_ip(value);
224
225 return 0;
226}
227U_BOOT_ENV_CALLBACK(ipaddr, on_ipaddr);
228
229static int on_gatewayip(const char *name, const char *value, enum env_op op,
230 int flags)
231{
232 if (flags & H_PROGRAMMATIC)
233 return 0;
234
235 net_gateway = string_to_ip(value);
236
237 return 0;
238}
239U_BOOT_ENV_CALLBACK(gatewayip, on_gatewayip);
240
241static int on_netmask(const char *name, const char *value, enum env_op op,
242 int flags)
243{
244 if (flags & H_PROGRAMMATIC)
245 return 0;
246
247 net_netmask = string_to_ip(value);
248
249 return 0;
250}
251U_BOOT_ENV_CALLBACK(netmask, on_netmask);
252
253static int on_serverip(const char *name, const char *value, enum env_op op,
254 int flags)
255{
256 if (flags & H_PROGRAMMATIC)
257 return 0;
258
259 net_server_ip = string_to_ip(value);
260
261 return 0;
262}
263U_BOOT_ENV_CALLBACK(serverip, on_serverip);
264
265static int on_nvlan(const char *name, const char *value, enum env_op op,
266 int flags)
267{
268 if (flags & H_PROGRAMMATIC)
269 return 0;
270
271 net_native_vlan = string_to_vlan(value);
272
273 return 0;
274}
275U_BOOT_ENV_CALLBACK(nvlan, on_nvlan);
276
277static int on_vlan(const char *name, const char *value, enum env_op op,
278 int flags)
279{
280 if (flags & H_PROGRAMMATIC)
281 return 0;
282
283 net_our_vlan = string_to_vlan(value);
284
285 return 0;
286}
287U_BOOT_ENV_CALLBACK(vlan, on_vlan);
288
289#if defined(CONFIG_CMD_DNS)
290static int on_dnsip(const char *name, const char *value, enum env_op op,
291 int flags)
292{
293 if (flags & H_PROGRAMMATIC)
294 return 0;
295
296 net_dns_server = string_to_ip(value);
297
298 return 0;
299}
300U_BOOT_ENV_CALLBACK(dnsip, on_dnsip);
301#endif
302
e4a3d57d
SG
303/*
304 * Check if autoload is enabled. If so, use either NFS or TFTP to download
305 * the boot file.
306 */
307void net_auto_load(void)
308{
a6ab4b54 309#if defined(CONFIG_CMD_NFS) && !defined(CONFIG_SPL_BUILD)
00caae6d 310 const char *s = env_get("autoload");
e4a3d57d 311
ec8a252c 312 if (s != NULL && strcmp(s, "NFS") == 0) {
3855cad6
JH
313 if (net_check_prereq(NFS)) {
314/* We aren't expecting to get a serverip, so just accept the assigned IP */
43407539
SG
315 if (IS_ENABLED(CONFIG_BOOTP_SERVERIP)) {
316 net_set_state(NETLOOP_SUCCESS);
317 } else {
318 printf("Cannot autoload with NFS\n");
319 net_set_state(NETLOOP_FAIL);
320 }
3855cad6
JH
321 return;
322 }
ec8a252c
JH
323 /*
324 * Use NFS to load the bootfile.
325 */
68c76a3a 326 nfs_start();
ec8a252c
JH
327 return;
328 }
e4a3d57d 329#endif
bfebc8c9 330 if (env_get_yesno("autoload") == 0) {
ec8a252c
JH
331 /*
332 * Just use BOOTP/RARP to configure system;
333 * Do not use TFTP to load the bootfile.
334 */
335 net_set_state(NETLOOP_SUCCESS);
336 return;
e4a3d57d 337 }
3855cad6
JH
338 if (net_check_prereq(TFTPGET)) {
339/* We aren't expecting to get a serverip, so just accept the assigned IP */
43407539
SG
340 if (IS_ENABLED(CONFIG_BOOTP_SERVERIP)) {
341 net_set_state(NETLOOP_SUCCESS);
342 } else {
343 printf("Cannot autoload with TFTPGET\n");
344 net_set_state(NETLOOP_FAIL);
345 }
3855cad6
JH
346 return;
347 }
8885c5fe 348 tftp_start(TFTPGET);
e4a3d57d
SG
349}
350
c3f0278e 351static int net_init_loop(void)
2f70c49e 352{
6de98b60
EM
353 static bool first_call = true;
354
ffdbf3ba 355 if (eth_get_dev()) {
0adb5b76 356 memcpy(net_ethaddr, eth_get_ethaddr(), 6);
ffdbf3ba
VM
357
358 if (IS_ENABLED(CONFIG_IPV6)) {
359 ip6_make_lladdr(&net_link_local_ip6, net_ethaddr);
360 if (!memcmp(&net_ip6, &net_null_addr_ip6,
361 sizeof(struct in6_addr)))
362 memcpy(&net_ip6, &net_link_local_ip6,
363 sizeof(struct in6_addr));
364 }
365 }
c3f0278e
SA
366 else
367 /*
368 * Not ideal, but there's no way to get the actual error, and I
369 * don't feel like fixing all the users of eth_get_dev to deal
370 * with errors.
371 */
372 return -ENONET;
3c172c4f 373
6de98b60
EM
374 if (IS_ENABLED(CONFIG_IPV6_ROUTER_DISCOVERY))
375 if (first_call && use_ip6) {
376 first_call = false;
377 srand_mac(); /* This is for rand used in ip6_send_rs. */
378 net_loop(RS);
379 }
c3f0278e 380 return 0;
2f70c49e
HS
381}
382
ece223b5
JH
383static void net_clear_handlers(void)
384{
385 net_set_udp_handler(NULL);
386 net_set_arp_handler(NULL);
bc0571fc 387 net_set_timeout_handler(0, NULL);
ece223b5
JH
388}
389
390static void net_cleanup_loop(void)
391{
392 net_clear_handlers();
393}
394
c3f0278e 395int net_init(void)
46c495d5
JH
396{
397 static int first_call = 1;
398
399 if (first_call) {
400 /*
401 * Setup packet buffers, aligned correctly.
402 */
403 int i;
404
1203fcce
JH
405 net_tx_packet = &net_pkt_buf[0] + (PKTALIGN - 1);
406 net_tx_packet -= (ulong)net_tx_packet % PKTALIGN;
2a504df0 407 for (i = 0; i < PKTBUFSRX; i++) {
1203fcce
JH
408 net_rx_packets[i] = net_tx_packet +
409 (i + 1) * PKTSIZE_ALIGN;
2a504df0 410 }
85d25e0e 411 arp_init();
ffdbf3ba 412 ndisc_init();
46c495d5
JH
413 net_clear_handlers();
414
415 /* Only need to setup buffer pointers once. */
416 first_call = 0;
a3bf193b
YCLP
417 if (IS_ENABLED(CONFIG_PROT_TCP))
418 tcp_set_tcp_state(TCP_CLOSED);
46c495d5
JH
419 }
420
c3f0278e 421 return net_init_loop();
46c495d5
JH
422}
423
2d966958
WD
424/**********************************************************************/
425/*
426 * Main network processing loop.
427 */
428
bc0571fc 429int net_loop(enum proto_t protocol)
2d966958 430{
60304592 431 int ret = -EINVAL;
60177b26 432 enum net_loop_state prev_net_state = net_state;
2d966958 433
def7a5c0
MS
434#if defined(CONFIG_CMD_PING)
435 if (protocol != PING)
436 net_ping_ip.s_addr = 0;
437#endif
bc0571fc
JH
438 net_restarted = 0;
439 net_dev_exists = 0;
440 net_try_count = 1;
441 debug_cond(DEBUG_INT_STATE, "--- net_loop Entry\n");
73a8b27c 442
09bd3d0b
SMJ
443#ifdef CONFIG_PHY_NCSI
444 if (phy_interface_is_ncsi() && protocol != NCSI && !ncsi_active()) {
445 printf("%s: configuring NCSI first\n", __func__);
446 if (net_loop(NCSI) < 0)
447 return ret;
448 eth_init_state_only();
449 goto restart;
450 }
451#endif
452
573f14fe 453 bootstage_mark_name(BOOTSTAGE_ID_ETH_START, "eth_start");
46c495d5 454 net_init();
d9506cd4 455 if (eth_is_on_demand_init()) {
b1bf6f2c 456 eth_halt();
f8be7d65 457 eth_set_current();
60304592
JH
458 ret = eth_init();
459 if (ret < 0) {
f8be7d65 460 eth_halt();
60304592 461 return ret;
f8be7d65 462 }
bc0571fc 463 } else {
d2eaec60 464 eth_init_state_only();
bc0571fc 465 }
4b290d4a 466
2d966958 467restart:
b63056d6
JL
468#ifdef CONFIG_USB_KEYBOARD
469 net_busy_flag = 0;
470#endif
22f6e99d 471 net_set_state(NETLOOP_CONTINUE);
2d966958
WD
472
473 /*
474 * Start the ball rolling with the given start function. From
475 * here on, this code is a state machine driven by received
476 * packets and timer events.
477 */
bc0571fc
JH
478 debug_cond(DEBUG_INT_STATE, "--- net_loop Init\n");
479 net_init_loop();
2d966958 480
f43b2df3
SG
481 if (!test_eth_enabled())
482 return 0;
483
4f63acd0 484 switch (net_check_prereq(protocol)) {
2d966958
WD
485 case 1:
486 /* network not configured */
b1bf6f2c 487 eth_halt();
60177b26 488 net_set_state(prev_net_state);
60304592 489 return -ENODEV;
2d966958 490
2d966958
WD
491 case 2:
492 /* network device not configured */
493 break;
2d966958
WD
494
495 case 0:
bc0571fc 496 net_dev_exists = 1;
1411157d 497 net_boot_file_size = 0;
2d966958 498 switch (protocol) {
808f13d8 499#ifdef CONFIG_CMD_TFTPBOOT
e4bf0c5c 500 case TFTPGET:
1fb7cd49
SG
501#ifdef CONFIG_CMD_TFTPPUT
502 case TFTPPUT:
503#endif
2d966958 504 /* always use ARP to get server ethernet address */
8885c5fe 505 tftp_start(protocol);
2d966958 506 break;
808f13d8 507#endif
7a83af07
LC
508#ifdef CONFIG_CMD_TFTPSRV
509 case TFTPSRV:
8885c5fe 510 tftp_start_server();
7a83af07
LC
511 break;
512#endif
7d04986a 513#if CONFIG_IS_ENABLED(UDP_FUNCTION_FASTBOOT)
443d3191
DM
514 case FASTBOOT_UDP:
515 fastboot_udp_start_server();
516 break;
517#endif
7d04986a 518#if CONFIG_IS_ENABLED(TCP_FUNCTION_FASTBOOT)
443d3191
DM
519 case FASTBOOT_TCP:
520 fastboot_tcp_start_server();
f73a7df9
AK
521 break;
522#endif
643d1ab2 523#if defined(CONFIG_CMD_DHCP)
2d966958 524 case DHCP:
7044c6bb 525 bootp_reset();
049a95a7 526 net_ip.s_addr = 0;
7044c6bb 527 dhcp_request(); /* Basically same as BOOTP */
2d966958 528 break;
610f2e9c 529#endif
a0245818
SE
530 case DHCP6:
531 if (IS_ENABLED(CONFIG_CMD_DHCP6))
532 dhcp6_start();
533 break;
808f13d8 534#if defined(CONFIG_CMD_BOOTP)
2d966958 535 case BOOTP:
7044c6bb 536 bootp_reset();
049a95a7 537 net_ip.s_addr = 0;
7044c6bb 538 bootp_request();
2d966958 539 break;
808f13d8 540#endif
bf6cb247 541#if defined(CONFIG_CMD_RARP)
2d966958 542 case RARP:
698d78e5 543 rarp_try = 0;
049a95a7 544 net_ip.s_addr = 0;
698d78e5 545 rarp_request();
2d966958 546 break;
bf6cb247 547#endif
643d1ab2 548#if defined(CONFIG_CMD_PING)
73a8b27c 549 case PING:
a36b12f9 550 ping_start();
73a8b27c 551 break;
cbd8a35c 552#endif
eeb0a2c6
VM
553#if defined(CONFIG_CMD_PING6)
554 case PING6:
555 ping6_start();
556 break;
557#endif
a6ab4b54 558#if defined(CONFIG_CMD_NFS) && !defined(CONFIG_SPL_BUILD)
cbd8a35c 559 case NFS:
68c76a3a 560 nfs_start();
cbd8a35c 561 break;
a3d991bd 562#endif
cfbae482
YCLP
563#if defined(CONFIG_CMD_WGET)
564 case WGET:
565 wget_start();
566 break;
567#endif
643d1ab2 568#if defined(CONFIG_CMD_CDP)
a3d991bd 569 case CDP:
6aede5b7 570 cdp_start();
a3d991bd 571 break;
68ceb29e 572#endif
66c89ee3 573#if defined(CONFIG_NETCONSOLE) && !defined(CONFIG_SPL_BUILD)
68ceb29e 574 case NETCONS:
6a38a5f3 575 nc_start();
68ceb29e 576 break;
ea287deb 577#endif
1a32bf41
RG
578#if defined(CONFIG_CMD_DNS)
579 case DNS:
786eac5f 580 dns_start();
1a32bf41 581 break;
d22c338e
JH
582#endif
583#if defined(CONFIG_CMD_LINK_LOCAL)
584 case LINKLOCAL:
585 link_local_start();
586 break;
d8970dae
LF
587#endif
588#if defined(CONFIG_CMD_WOL)
589 case WOL:
590 wol_start();
591 break;
09bd3d0b
SMJ
592#endif
593#if defined(CONFIG_PHY_NCSI)
594 case NCSI:
595 ncsi_probe_packages();
596 break;
73a8b27c 597#endif
6de98b60
EM
598 case RS:
599 if (IS_ENABLED(CONFIG_IPV6_ROUTER_DISCOVERY))
600 ip6_send_rs();
601 break;
2d966958
WD
602 default:
603 break;
604 }
605
b43ea1bf
PR
606 if (IS_ENABLED(CONFIG_PROT_UDP) && protocol == UDP)
607 udp_start();
608
2d966958
WD
609 break;
610 }
611
643d1ab2 612#if defined(CONFIG_MII) || defined(CONFIG_CMD_MII)
3e38e429 613#if defined(CONFIG_SYS_FAULT_ECHO_LINK_DOWN) && \
2d8d190c
UM
614 defined(CONFIG_LED_STATUS) && \
615 defined(CONFIG_LED_STATUS_RED)
fc3e2165 616 /*
42d1f039 617 * Echo the inverted link state to the fault LED.
fc3e2165 618 */
d3c65b01 619 if (miiphy_link(eth_get_dev()->name, CONFIG_SYS_FAULT_MII_ADDR))
2d8d190c 620 status_led_set(CONFIG_LED_STATUS_RED, CONFIG_LED_STATUS_OFF);
d3c65b01 621 else
2d8d190c 622 status_led_set(CONFIG_LED_STATUS_RED, CONFIG_LED_STATUS_ON);
6d0f6bcf 623#endif /* CONFIG_SYS_FAULT_ECHO_LINK_DOWN, ... */
fc3e2165 624#endif /* CONFIG_MII, ... */
b63056d6
JL
625#ifdef CONFIG_USB_KEYBOARD
626 net_busy_flag = 1;
627#endif
2d966958
WD
628
629 /*
630 * Main packet reception loop. Loop receiving packets until
22f6e99d 631 * someone sets `net_state' to a state that terminates.
2d966958
WD
632 */
633 for (;;) {
29caf930 634 schedule();
c5a75339
JH
635 if (arp_timeout_check() > 0)
636 time_start = get_timer(0);
637
ffdbf3ba
VM
638 if (IS_ENABLED(CONFIG_IPV6)) {
639 if (use_ip6 && (ndisc_timeout_check() > 0))
640 time_start = get_timer(0);
641 }
642
2d966958
WD
643 /*
644 * Check the ethernet for a new packet. The ethernet
645 * receive routine will process it.
60304592
JH
646 * Most drivers return the most recent packet size, but not
647 * errors that may have happened.
2d966958 648 */
40cb90ee 649 eth_rx();
2d966958
WD
650
651 /*
652 * Abort if ctrl-c was pressed.
653 */
654 if (ctrlc()) {
e94070c4 655 /* cancel any ARP that may not have completed */
049a95a7 656 net_arp_wait_packet_ip.s_addr = 0;
e94070c4 657
ece223b5 658 net_cleanup_loop();
8bde7f77 659 eth_halt();
f8be7d65
JH
660 /* Invalidate the last protocol */
661 eth_set_last_protocol(BOOTP);
662
4f63acd0 663 puts("\nAbort\n");
4ef8d53c
JH
664 /* include a debug print as well incase the debug
665 messages are directed to stderr */
bc0571fc 666 debug_cond(DEBUG_INT_STATE, "--- net_loop Abort!\n");
19a4fbaa 667 ret = -EINTR;
4793ee65 668 goto done;
2d966958
WD
669 }
670
2d966958
WD
671 /*
672 * Check for a timeout, and run the timeout handler
673 * if we have one.
674 */
bc0571fc
JH
675 if (time_handler &&
676 ((get_timer(0) - time_start) > time_delta)) {
2d966958
WD
677 thand_f *x;
678
643d1ab2 679#if defined(CONFIG_MII) || defined(CONFIG_CMD_MII)
4f63acd0 680#if defined(CONFIG_SYS_FAULT_ECHO_LINK_DOWN) && \
2d8d190c
UM
681 defined(CONFIG_LED_STATUS) && \
682 defined(CONFIG_LED_STATUS_RED)
fc3e2165 683 /*
42d1f039 684 * Echo the inverted link state to the fault LED.
fc3e2165 685 */
4f63acd0 686 if (miiphy_link(eth_get_dev()->name,
bc0571fc 687 CONFIG_SYS_FAULT_MII_ADDR))
2d8d190c
UM
688 status_led_set(CONFIG_LED_STATUS_RED,
689 CONFIG_LED_STATUS_OFF);
bc0571fc 690 else
2d8d190c
UM
691 status_led_set(CONFIG_LED_STATUS_RED,
692 CONFIG_LED_STATUS_ON);
4f63acd0 693#endif /* CONFIG_SYS_FAULT_ECHO_LINK_DOWN, ... */
fc3e2165 694#endif /* CONFIG_MII, ... */
bc0571fc
JH
695 debug_cond(DEBUG_INT_STATE, "--- net_loop timeout\n");
696 x = time_handler;
697 time_handler = (thand_f *)0;
2d966958 698 (*x)();
6de98b60
EM
699 } else if (IS_ENABLED(CONFIG_IPV6_ROUTER_DISCOVERY))
700 if (time_handler && protocol == RS)
701 if (!ip6_is_unspecified_addr(&net_gateway6) &&
702 net_prefix_length != 0) {
703 net_set_state(NETLOOP_SUCCESS);
704 net_set_timeout_handler(0, NULL);
705 }
2d966958 706
5c421331 707 if (net_state == NETLOOP_FAIL)
bc0571fc 708 ret = net_start_again();
2d966958 709
22f6e99d 710 switch (net_state) {
2d966958 711 case NETLOOP_RESTART:
bc0571fc 712 net_restarted = 1;
2d966958
WD
713 goto restart;
714
715 case NETLOOP_SUCCESS:
ece223b5 716 net_cleanup_loop();
1411157d 717 if (net_boot_file_size > 0) {
8f911a7b 718 printf("Bytes transferred = %u (%x hex)\n",
1411157d 719 net_boot_file_size, net_boot_file_size);
018f5303 720 env_set_hex("filesize", net_boot_file_size);
bb872dd9 721 env_set_hex("fileaddr", image_load_addr);
2d966958 722 }
09bd3d0b 723 if (protocol != NETCONS && protocol != NCSI)
f8be7d65
JH
724 eth_halt();
725 else
726 eth_halt_state_only();
727
728 eth_set_last_protocol(protocol);
729
1411157d 730 ret = net_boot_file_size;
bc0571fc 731 debug_cond(DEBUG_INT_STATE, "--- net_loop Success!\n");
4793ee65 732 goto done;
2d966958
WD
733
734 case NETLOOP_FAIL:
ece223b5 735 net_cleanup_loop();
f8be7d65
JH
736 /* Invalidate the last protocol */
737 eth_set_last_protocol(BOOTP);
bc0571fc 738 debug_cond(DEBUG_INT_STATE, "--- net_loop Fail!\n");
a735e6e9 739 ret = -ENONET;
4793ee65 740 goto done;
22f6e99d
JH
741
742 case NETLOOP_CONTINUE:
743 continue;
2d966958
WD
744 }
745 }
4793ee65
SG
746
747done:
b63056d6
JL
748#ifdef CONFIG_USB_KEYBOARD
749 net_busy_flag = 0;
750#endif
39bccd21 751#ifdef CONFIG_CMD_TFTPPUT
4793ee65 752 /* Clear out the handlers */
ece223b5 753 net_set_udp_handler(NULL);
4793ee65 754 net_set_icmp_handler(NULL);
39bccd21 755#endif
60177b26 756 net_set_state(prev_net_state);
3eaac630
RF
757
758#if defined(CONFIG_CMD_PCAP)
759 if (pcap_active())
760 pcap_print_status();
761#endif
4793ee65 762 return ret;
2d966958
WD
763}
764
765/**********************************************************************/
766
bc0571fc 767static void start_again_timeout_handler(void)
2d966958 768{
22f6e99d 769 net_set_state(NETLOOP_RESTART);
2d966958
WD
770}
771
bc0571fc 772int net_start_again(void)
2d966958 773{
6e592385 774 char *nretry;
67b96e87
RB
775 int retry_forever = 0;
776 unsigned long retrycnt = 0;
60304592 777 int ret;
67b96e87 778
00caae6d 779 nretry = env_get("netretry");
67b96e87
RB
780 if (nretry) {
781 if (!strcmp(nretry, "yes"))
782 retry_forever = 1;
783 else if (!strcmp(nretry, "no"))
784 retrycnt = 0;
785 else if (!strcmp(nretry, "once"))
786 retrycnt = 1;
787 else
788 retrycnt = simple_strtoul(nretry, NULL, 0);
5c421331
JH
789 } else {
790 retrycnt = 0;
791 retry_forever = 0;
792 }
67b96e87 793
17d413b2 794 if ((!retry_forever) && (net_try_count > retrycnt)) {
67b96e87 795 eth_halt();
22f6e99d 796 net_set_state(NETLOOP_FAIL);
60304592
JH
797 /*
798 * We don't provide a way for the protocol to return an error,
799 * but this is almost always the reason.
800 */
801 return -ETIMEDOUT;
a3d991bd 802 }
67b96e87 803
bc0571fc 804 net_try_count++;
67b96e87 805
4f63acd0 806 eth_halt();
8b0c5c12 807#if !defined(CONFIG_NET_DO_NOT_TRY_ANOTHER)
bc0571fc 808 eth_try_another(!net_restarted);
8b0c5c12 809#endif
60304592 810 ret = eth_init();
bc0571fc
JH
811 if (net_restart_wrap) {
812 net_restart_wrap = 0;
813 if (net_dev_exists) {
814 net_set_timeout_handler(10000UL,
815 start_again_timeout_handler);
ece223b5 816 net_set_udp_handler(NULL);
6e592385 817 } else {
22f6e99d 818 net_set_state(NETLOOP_FAIL);
2d966958 819 }
6e592385 820 } else {
22f6e99d 821 net_set_state(NETLOOP_RESTART);
2d966958 822 }
60304592 823 return ret;
2d966958
WD
824}
825
826/**********************************************************************/
827/*
828 * Miscelaneous bits.
829 */
830
ece223b5 831static void dummy_handler(uchar *pkt, unsigned dport,
049a95a7 832 struct in_addr sip, unsigned sport,
ece223b5 833 unsigned len)
d280d3f4 834{
d280d3f4
JH
835}
836
ece223b5
JH
837rxhand_f *net_get_udp_handler(void)
838{
839 return udp_packet_handler;
840}
d280d3f4 841
ece223b5
JH
842void net_set_udp_handler(rxhand_f *f)
843{
bc0571fc 844 debug_cond(DEBUG_INT_STATE, "--- net_loop UDP handler set (%p)\n", f);
ece223b5
JH
845 if (f == NULL)
846 udp_packet_handler = dummy_handler;
847 else
848 udp_packet_handler = f;
849}
850
851rxhand_f *net_get_arp_handler(void)
2d966958 852{
ece223b5
JH
853 return arp_packet_handler;
854}
855
856void net_set_arp_handler(rxhand_f *f)
857{
bc0571fc 858 debug_cond(DEBUG_INT_STATE, "--- net_loop ARP handler set (%p)\n", f);
ece223b5
JH
859 if (f == NULL)
860 arp_packet_handler = dummy_handler;
861 else
862 arp_packet_handler = f;
2d966958
WD
863}
864
39bccd21 865#ifdef CONFIG_CMD_TFTPPUT
4793ee65
SG
866void net_set_icmp_handler(rxhand_icmp_f *f)
867{
868 packet_icmp_handler = f;
869}
39bccd21 870#endif
2d966958 871
bc0571fc 872void net_set_timeout_handler(ulong iv, thand_f *f)
2d966958
WD
873{
874 if (iv == 0) {
4ef8d53c 875 debug_cond(DEBUG_INT_STATE,
bc0571fc
JH
876 "--- net_loop timeout handler cancelled\n");
877 time_handler = (thand_f *)0;
2d966958 878 } else {
4ef8d53c 879 debug_cond(DEBUG_INT_STATE,
bc0571fc
JH
880 "--- net_loop timeout handler set (%p)\n", f);
881 time_handler = f;
882 time_start = get_timer(0);
883 time_delta = iv * CONFIG_SYS_HZ / 1000;
2d966958
WD
884 }
885}
886
ac3f26cc
JH
887uchar *net_get_async_tx_pkt_buf(void)
888{
889 if (arp_is_waiting())
890 return arp_tx_packet; /* If we are waiting, we already sent */
891 else
892 return net_tx_packet;
893}
894
1203fcce 895int net_send_udp_packet(uchar *ether, struct in_addr dest, int dport, int sport,
206d07fd 896 int payload_len)
5d457ecb
DH
897{
898 return net_send_ip_packet(ether, dest, dport, sport, payload_len,
899 IPPROTO_UDP, 0, 0, 0);
900}
901
a3bf193b
YCLP
902#if defined(CONFIG_PROT_TCP)
903int net_send_tcp_packet(int payload_len, int dport, int sport, u8 action,
904 u32 tcp_seq_num, u32 tcp_ack_num)
905{
906 return net_send_ip_packet(net_server_ethaddr, net_server_ip, dport,
907 sport, payload_len, IPPROTO_TCP, action,
908 tcp_seq_num, tcp_ack_num);
909}
910#endif
911
5d457ecb
DH
912int net_send_ip_packet(uchar *ether, struct in_addr dest, int dport, int sport,
913 int payload_len, int proto, u8 action, u32 tcp_seq_num,
914 u32 tcp_ack_num)
73a8b27c 915{
a3d991bd 916 uchar *pkt;
9214637a
JH
917 int eth_hdr_size;
918 int pkt_hdr_size;
a3d991bd 919
bc0571fc 920 /* make sure the net_tx_packet is initialized (net_init() was called) */
1203fcce
JH
921 assert(net_tx_packet != NULL);
922 if (net_tx_packet == NULL)
46c495d5
JH
923 return -1;
924
73a8b27c 925 /* convert to new style broadcast */
049a95a7
JH
926 if (dest.s_addr == 0)
927 dest.s_addr = 0xFFFFFFFF;
73a8b27c
WD
928
929 /* if broadcast, make the ether address a broadcast and don't do ARP */
049a95a7 930 if (dest.s_addr == 0xFFFFFFFF)
0adb5b76 931 ether = (uchar *)net_bcast_ethaddr;
73a8b27c 932
1203fcce 933 pkt = (uchar *)net_tx_packet;
9214637a 934
1203fcce 935 eth_hdr_size = net_set_ether(pkt, ether, PROT_IP);
5d457ecb
DH
936
937 switch (proto) {
938 case IPPROTO_UDP:
939 net_set_udp_header(pkt + eth_hdr_size, dest, dport, sport,
940 payload_len);
941 pkt_hdr_size = eth_hdr_size + IP_UDP_HDR_SIZE;
942 break;
a3bf193b
YCLP
943#if defined(CONFIG_PROT_TCP)
944 case IPPROTO_TCP:
945 pkt_hdr_size = eth_hdr_size
946 + tcp_set_tcp_header(pkt + eth_hdr_size, dport, sport,
947 payload_len, action, tcp_seq_num,
948 tcp_ack_num);
949 break;
950#endif
5d457ecb
DH
951 default:
952 return -EINVAL;
953 }
73a8b27c 954
e94070c4 955 /* if MAC address was not discovered yet, do an ARP request */
0adb5b76 956 if (memcmp(ether, net_null_ethaddr, 6) == 0) {
4ef8d53c 957 debug_cond(DEBUG_DEV_PKT, "sending ARP for %pI4\n", &dest);
0ebf04c6 958
9214637a 959 /* save the ip and eth addr for the packet to send after arp */
049a95a7 960 net_arp_wait_packet_ip = dest;
85d25e0e 961 arp_wait_packet_ethaddr = ether;
a3d991bd 962
73a8b27c 963 /* size of the waiting packet */
85d25e0e 964 arp_wait_tx_packet_size = pkt_hdr_size + payload_len;
73a8b27c
WD
965
966 /* and do the ARP request */
85d25e0e
JH
967 arp_wait_try = 1;
968 arp_wait_timer_start = get_timer(0);
969 arp_request();
73a8b27c 970 return 1; /* waiting */
9214637a 971 } else {
4ef8d53c 972 debug_cond(DEBUG_DEV_PKT, "sending UDP to %pI4/%pM\n",
bc0571fc 973 &dest, ether);
1203fcce 974 net_send_packet(net_tx_packet, pkt_hdr_size + payload_len);
9214637a 975 return 0; /* transmitted */
73a8b27c 976 }
73a8b27c
WD
977}
978
5cfaa4e5
AR
979#ifdef CONFIG_IP_DEFRAG
980/*
981 * This function collects fragments in a single packet, according
982 * to the algorithm in RFC815. It returns NULL or the pointer to
983 * a complete packet, in static storage
984 */
aa7a6487 985#define IP_PKTSIZE (CONFIG_NET_MAXDEFRAG)
5cfaa4e5 986
c5c59df0 987#define IP_MAXUDP (IP_PKTSIZE - IP_HDR_SIZE)
5cfaa4e5
AR
988
989/*
990 * this is the packet being assembled, either data or frag control.
991 * Fragments go by 8 bytes, so this union must be 8 bytes long
992 */
993struct hole {
994 /* first_byte is address of this structure */
995 u16 last_byte; /* last byte in this hole + 1 (begin of next hole) */
996 u16 next_hole; /* index of next (in 8-b blocks), 0 == none */
997 u16 prev_hole; /* index of prev, 0 == none */
998 u16 unused;
999};
1000
bc0571fc 1001static struct ip_udp_hdr *__net_defragment(struct ip_udp_hdr *ip, int *lenp)
5cfaa4e5 1002{
48522bb5 1003 static uchar pkt_buff[IP_PKTSIZE] __aligned(PKTALIGN);
5cfaa4e5
AR
1004 static u16 first_hole, total_len;
1005 struct hole *payload, *thisfrag, *h, *newh;
594c26f8 1006 struct ip_udp_hdr *localip = (struct ip_udp_hdr *)pkt_buff;
5cfaa4e5
AR
1007 uchar *indata = (uchar *)ip;
1008 int offset8, start, len, done = 0;
1009 u16 ip_off = ntohs(ip->ip_off);
1010
1817c382
RV
1011 /*
1012 * Calling code already rejected <, but we don't have to deal
1013 * with an IP fragment with no payload.
1014 */
1015 if (ntohs(ip->ip_len) <= IP_HDR_SIZE)
b85d130e
FE
1016 return NULL;
1017
5cfaa4e5 1018 /* payload starts after IP header, this fragment is in there */
c5c59df0 1019 payload = (struct hole *)(pkt_buff + IP_HDR_SIZE);
5cfaa4e5
AR
1020 offset8 = (ip_off & IP_OFFS);
1021 thisfrag = payload + offset8;
1022 start = offset8 * 8;
c5c59df0 1023 len = ntohs(ip->ip_len) - IP_HDR_SIZE;
5cfaa4e5 1024
1817c382
RV
1025 /* All but last fragment must have a multiple-of-8 payload. */
1026 if ((len & 7) && (ip_off & IP_FLAGS_MFRAG))
1027 return NULL;
1028
5cfaa4e5
AR
1029 if (start + len > IP_MAXUDP) /* fragment extends too far */
1030 return NULL;
1031
1032 if (!total_len || localip->ip_id != ip->ip_id) {
1033 /* new (or different) packet, reset structs */
1034 total_len = 0xffff;
1035 payload[0].last_byte = ~0;
1036 payload[0].next_hole = 0;
1037 payload[0].prev_hole = 0;
1038 first_hole = 0;
1039 /* any IP header will work, copy the first we received */
c5c59df0 1040 memcpy(localip, ip, IP_HDR_SIZE);
5cfaa4e5
AR
1041 }
1042
1043 /*
1044 * What follows is the reassembly algorithm. We use the payload
1045 * array as a linked list of hole descriptors, as each hole starts
1046 * at a multiple of 8 bytes. However, last byte can be whatever value,
1047 * so it is represented as byte count, not as 8-byte blocks.
1048 */
1049
1050 h = payload + first_hole;
1051 while (h->last_byte < start) {
1052 if (!h->next_hole) {
1053 /* no hole that far away */
1054 return NULL;
1055 }
1056 h = payload + h->next_hole;
1057 }
1058
e397e59e
FS
1059 /* last fragment may be 1..7 bytes, the "+7" forces acceptance */
1060 if (offset8 + ((len + 7) / 8) <= h - payload) {
5cfaa4e5
AR
1061 /* no overlap with holes (dup fragment?) */
1062 return NULL;
1063 }
1064
1065 if (!(ip_off & IP_FLAGS_MFRAG)) {
1066 /* no more fragmentss: truncate this (last) hole */
1067 total_len = start + len;
1068 h->last_byte = start + len;
1069 }
1070
1071 /*
06869686
RV
1072 * There is some overlap: fix the hole list. This code deals
1073 * with a fragment that overlaps with two different holes
1074 * (thus being a superset of a previously-received fragment)
1075 * by only using the part of the fragment that fits in the
1076 * first hole.
5cfaa4e5 1077 */
06869686
RV
1078 if (h->last_byte < start + len)
1079 len = h->last_byte - start;
5cfaa4e5 1080
4f63acd0 1081 if ((h >= thisfrag) && (h->last_byte <= start + len)) {
5cfaa4e5
AR
1082 /* complete overlap with hole: remove hole */
1083 if (!h->prev_hole && !h->next_hole) {
1084 /* last remaining hole */
1085 done = 1;
1086 } else if (!h->prev_hole) {
1087 /* first hole */
1088 first_hole = h->next_hole;
1089 payload[h->next_hole].prev_hole = 0;
1090 } else if (!h->next_hole) {
1091 /* last hole */
1092 payload[h->prev_hole].next_hole = 0;
1093 } else {
1094 /* in the middle of the list */
1095 payload[h->next_hole].prev_hole = h->prev_hole;
1096 payload[h->prev_hole].next_hole = h->next_hole;
1097 }
1098
1099 } else if (h->last_byte <= start + len) {
1100 /* overlaps with final part of the hole: shorten this hole */
1101 h->last_byte = start;
1102
1103 } else if (h >= thisfrag) {
1104 /* overlaps with initial part of the hole: move this hole */
1105 newh = thisfrag + (len / 8);
1106 *newh = *h;
1107 h = newh;
1108 if (h->next_hole)
1109 payload[h->next_hole].prev_hole = (h - payload);
1110 if (h->prev_hole)
1111 payload[h->prev_hole].next_hole = (h - payload);
1112 else
1113 first_hole = (h - payload);
1114
1115 } else {
1116 /* fragment sits in the middle: split the hole */
1117 newh = thisfrag + (len / 8);
1118 *newh = *h;
1119 h->last_byte = start;
1120 h->next_hole = (newh - payload);
1121 newh->prev_hole = (h - payload);
1122 if (newh->next_hole)
1123 payload[newh->next_hole].prev_hole = (newh - payload);
1124 }
1125
1126 /* finally copy this fragment and possibly return whole packet */
c5c59df0 1127 memcpy((uchar *)thisfrag, indata + IP_HDR_SIZE, len);
5cfaa4e5
AR
1128 if (!done)
1129 return NULL;
1130
c5c59df0 1131 *lenp = total_len + IP_HDR_SIZE;
06653c70 1132 localip->ip_len = htons(*lenp);
5cfaa4e5
AR
1133 return localip;
1134}
1135
bc0571fc
JH
1136static inline struct ip_udp_hdr *net_defragment(struct ip_udp_hdr *ip,
1137 int *lenp)
5cfaa4e5
AR
1138{
1139 u16 ip_off = ntohs(ip->ip_off);
1140 if (!(ip_off & (IP_OFFS | IP_FLAGS_MFRAG)))
1141 return ip; /* not a fragment */
bc0571fc 1142 return __net_defragment(ip, lenp);
5cfaa4e5
AR
1143}
1144
1145#else /* !CONFIG_IP_DEFRAG */
1146
bc0571fc
JH
1147static inline struct ip_udp_hdr *net_defragment(struct ip_udp_hdr *ip,
1148 int *lenp)
5cfaa4e5
AR
1149{
1150 u16 ip_off = ntohs(ip->ip_off);
1151 if (!(ip_off & (IP_OFFS | IP_FLAGS_MFRAG)))
1152 return ip; /* not a fragment */
1153 return NULL;
1154}
1155#endif
a3d991bd 1156
8f79bb17
SG
1157/**
1158 * Receive an ICMP packet. We deal with REDIRECT and PING here, and silently
1159 * drop others.
1160 *
1161 * @parma ip IP packet containing the ICMP
1162 */
594c26f8 1163static void receive_icmp(struct ip_udp_hdr *ip, int len,
049a95a7 1164 struct in_addr src_ip, struct ethernet_hdr *et)
8f79bb17 1165{
e0a63079 1166 struct icmp_hdr *icmph = (struct icmp_hdr *)&ip->udp_src;
8f79bb17
SG
1167
1168 switch (icmph->type) {
1169 case ICMP_REDIRECT:
1170 if (icmph->code != ICMP_REDIR_HOST)
1171 return;
1172 printf(" ICMP Host Redirect to %pI4 ",
bc0571fc 1173 &icmph->un.gateway);
8f79bb17 1174 break;
a36b12f9 1175 default:
8f79bb17 1176#if defined(CONFIG_CMD_PING)
a36b12f9 1177 ping_receive(et, ip, len);
8f79bb17 1178#endif
39bccd21 1179#ifdef CONFIG_CMD_TFTPPUT
4793ee65
SG
1180 if (packet_icmp_handler)
1181 packet_icmp_handler(icmph->type, icmph->code,
bc0571fc
JH
1182 ntohs(ip->udp_dst), src_ip,
1183 ntohs(ip->udp_src), icmph->un.data,
1184 ntohs(ip->udp_len));
39bccd21 1185#endif
8f79bb17
SG
1186 break;
1187 }
1188}
1189
2a504df0 1190void net_process_received_packet(uchar *in_packet, int len)
2d966958 1191{
cb487f56 1192 struct ethernet_hdr *et;
594c26f8 1193 struct ip_udp_hdr *ip;
049a95a7
JH
1194 struct in_addr dst_ip;
1195 struct in_addr src_ip;
8d353eb8 1196 int eth_proto;
643d1ab2 1197#if defined(CONFIG_CMD_CDP)
a3d991bd
WD
1198 int iscdp;
1199#endif
1200 ushort cti = 0, vlanid = VLAN_NONE, myvlanid, mynvlanid;
1201
4ef8d53c 1202 debug_cond(DEBUG_NET_PKT, "packet received\n");
798962ce
SA
1203 if (DEBUG_NET_PKT_TRACE)
1204 print_hex_dump_bytes("rx: ", DUMP_PREFIX_OFFSET, in_packet,
1205 len);
2d966958 1206
3eaac630
RF
1207#if defined(CONFIG_CMD_PCAP)
1208 pcap_post(in_packet, len, false);
1209#endif
1203fcce
JH
1210 net_rx_packet = in_packet;
1211 net_rx_packet_len = len;
2a504df0 1212 et = (struct ethernet_hdr *)in_packet;
a3d991bd
WD
1213
1214 /* too small packet? */
1215 if (len < ETHER_HDR_SIZE)
1216 return;
1217
0efe1bcf 1218#if defined(CONFIG_API) || defined(CONFIG_EFI_LOADER)
f85b6071 1219 if (push_packet) {
2a504df0 1220 (*push_packet)(in_packet, len);
f85b6071
RJ
1221 return;
1222 }
1223#endif
1224
643d1ab2 1225#if defined(CONFIG_CMD_CDP)
a3d991bd 1226 /* keep track if packet is CDP */
17351883 1227 iscdp = is_cdp_packet(et->et_dest);
a3d991bd
WD
1228#endif
1229
4fd5055f 1230 myvlanid = ntohs(net_our_vlan);
a3d991bd
WD
1231 if (myvlanid == (ushort)-1)
1232 myvlanid = VLAN_NONE;
4fd5055f 1233 mynvlanid = ntohs(net_native_vlan);
a3d991bd
WD
1234 if (mynvlanid == (ushort)-1)
1235 mynvlanid = VLAN_NONE;
2d966958 1236
8d353eb8 1237 eth_proto = ntohs(et->et_protlen);
2d966958 1238
8d353eb8 1239 if (eth_proto < 1514) {
cb487f56 1240 struct e802_hdr *et802 = (struct e802_hdr *)et;
2d966958 1241 /*
da5ebe2c
JH
1242 * Got a 802.2 packet. Check the other protocol field.
1243 * XXX VLAN over 802.2+SNAP not implemented!
2d966958 1244 */
8d353eb8 1245 eth_proto = ntohs(et802->et_prot);
a3d991bd 1246
2a504df0 1247 ip = (struct ip_udp_hdr *)(in_packet + E802_HDR_SIZE);
2d966958 1248 len -= E802_HDR_SIZE;
a3d991bd 1249
8d353eb8 1250 } else if (eth_proto != PROT_VLAN) { /* normal packet */
2a504df0 1251 ip = (struct ip_udp_hdr *)(in_packet + ETHER_HDR_SIZE);
2d966958 1252 len -= ETHER_HDR_SIZE;
a3d991bd
WD
1253
1254 } else { /* VLAN packet */
c68cca35
JH
1255 struct vlan_ethernet_hdr *vet =
1256 (struct vlan_ethernet_hdr *)et;
a3d991bd 1257
4ef8d53c 1258 debug_cond(DEBUG_NET_PKT, "VLAN packet received\n");
0ebf04c6 1259
a3d991bd
WD
1260 /* too small packet? */
1261 if (len < VLAN_ETHER_HDR_SIZE)
1262 return;
1263
1264 /* if no VLAN active */
4fd5055f 1265 if ((ntohs(net_our_vlan) & VLAN_IDMASK) == VLAN_NONE
643d1ab2 1266#if defined(CONFIG_CMD_CDP)
a3d991bd
WD
1267 && iscdp == 0
1268#endif
1269 )
1270 return;
1271
1272 cti = ntohs(vet->vet_tag);
1273 vlanid = cti & VLAN_IDMASK;
8d353eb8 1274 eth_proto = ntohs(vet->vet_type);
a3d991bd 1275
2a504df0 1276 ip = (struct ip_udp_hdr *)(in_packet + VLAN_ETHER_HDR_SIZE);
a3d991bd 1277 len -= VLAN_ETHER_HDR_SIZE;
2d966958
WD
1278 }
1279
4ef8d53c 1280 debug_cond(DEBUG_NET_PKT, "Receive from protocol 0x%x\n", eth_proto);
2d966958 1281
643d1ab2 1282#if defined(CONFIG_CMD_CDP)
a3d991bd 1283 if (iscdp) {
0b4c5ff4 1284 cdp_receive((uchar *)ip, len);
a3d991bd
WD
1285 return;
1286 }
1287#endif
1288
1289 if ((myvlanid & VLAN_IDMASK) != VLAN_NONE) {
1290 if (vlanid == VLAN_NONE)
1291 vlanid = (mynvlanid & VLAN_IDMASK);
1292 /* not matched? */
1293 if (vlanid != (myvlanid & VLAN_IDMASK))
1294 return;
1295 }
1296
8d353eb8 1297 switch (eth_proto) {
2d966958 1298 case PROT_ARP:
85d25e0e 1299 arp_receive(et, ip, len);
289f932c 1300 break;
2d966958 1301
bf6cb247 1302#ifdef CONFIG_CMD_RARP
2d966958 1303 case PROT_RARP:
8b9c5322 1304 rarp_receive(ip, len);
2d966958 1305 break;
ffdbf3ba
VM
1306#endif
1307#if IS_ENABLED(CONFIG_IPV6)
1308 case PROT_IP6:
1309 net_ip6_handler(et, (struct ip6_hdr *)ip, len);
0d6d5a4a 1310 break;
bf6cb247 1311#endif
2d966958 1312 case PROT_IP:
4ef8d53c 1313 debug_cond(DEBUG_NET_PKT, "Got IP\n");
5cfaa4e5 1314 /* Before we start poking the header, make sure it is there */
ad359d89 1315 if (len < IP_HDR_SIZE) {
594c26f8 1316 debug("len bad %d < %lu\n", len,
ad359d89 1317 (ulong)IP_HDR_SIZE);
2d966958
WD
1318 return;
1319 }
5cfaa4e5 1320 /* Check the packet length */
2d966958 1321 if (len < ntohs(ip->ip_len)) {
4ef8d53c 1322 debug("len bad %d < %d\n", len, ntohs(ip->ip_len));
2d966958
WD
1323 return;
1324 }
1325 len = ntohs(ip->ip_len);
ad359d89
RV
1326 if (len < IP_HDR_SIZE) {
1327 debug("bad ip->ip_len %d < %d\n", len, (int)IP_HDR_SIZE);
1328 return;
1329 }
4ef8d53c 1330 debug_cond(DEBUG_NET_PKT, "len=%d, v=%02x\n",
bc0571fc 1331 len, ip->ip_hl_v & 0xff);
0ebf04c6 1332
5cfaa4e5 1333 /* Can't deal with anything except IPv4 */
d3c65b01 1334 if ((ip->ip_hl_v & 0xf0) != 0x40)
2d966958 1335 return;
5cfaa4e5 1336 /* Can't deal with IP options (headers != 20 bytes) */
b0fcc48c 1337 if ((ip->ip_hl_v & 0x0f) != 0x05)
6b52cfe1 1338 return;
5cfaa4e5 1339 /* Check the Checksum of the header */
0da0fcd5 1340 if (!ip_checksum_ok((uchar *)ip, IP_HDR_SIZE)) {
4ef8d53c 1341 debug("checksum bad\n");
2d966958
WD
1342 return;
1343 }
5cfaa4e5 1344 /* If it is not for us, ignore it */
049a95a7
JH
1345 dst_ip = net_read_ip(&ip->ip_dst);
1346 if (net_ip.s_addr && dst_ip.s_addr != net_ip.s_addr &&
1347 dst_ip.s_addr != 0xFFFFFFFF) {
c819abee 1348 return;
2d966958 1349 }
03eb129f 1350 /* Read source IP address for later use */
049a95a7 1351 src_ip = net_read_ip(&ip->ip_src);
5cfaa4e5
AR
1352 /*
1353 * The function returns the unchanged packet if it's not
1354 * a fragment, and either the complete packet or NULL if
1355 * it is a fragment (if !CONFIG_IP_DEFRAG, it returns NULL)
1356 */
bc0571fc 1357 ip = net_defragment(ip, &len);
ccb9ebef 1358 if (!ip)
5cfaa4e5 1359 return;
2d966958
WD
1360 /*
1361 * watch for ICMP host redirects
1362 *
8bde7f77
WD
1363 * There is no real handler code (yet). We just watch
1364 * for ICMP host redirect messages. In case anybody
1365 * sees these messages: please contact me
1366 * (wd@denx.de), or - even better - send me the
1367 * necessary fixes :-)
2d966958 1368 *
8bde7f77
WD
1369 * Note: in all cases where I have seen this so far
1370 * it was a problem with the router configuration,
1371 * for instance when a router was configured in the
1372 * BOOTP reply, but the TFTP server was on the same
1373 * subnet. So this is probably a warning that your
1374 * configuration might be wrong. But I'm not really
1375 * sure if there aren't any other situations.
4793ee65
SG
1376 *
1377 * Simon Glass <sjg@chromium.org>: We get an ICMP when
1378 * we send a tftp packet to a dead connection, or when
1379 * there is no server at the other end.
2d966958
WD
1380 */
1381 if (ip->ip_p == IPPROTO_ICMP) {
8f79bb17
SG
1382 receive_icmp(ip, len, src_ip, et);
1383 return;
a3bf193b
YCLP
1384#if defined(CONFIG_PROT_TCP)
1385 } else if (ip->ip_p == IPPROTO_TCP) {
1386 debug_cond(DEBUG_DEV_PKT,
1387 "TCP PH (to=%pI4, from=%pI4, len=%d)\n",
1388 &dst_ip, &src_ip, len);
1389
1390 rxhand_tcp_f((union tcp_build_pkt *)ip, len);
1391 return;
1392#endif
2d966958
WD
1393 } else if (ip->ip_p != IPPROTO_UDP) { /* Only UDP packets */
1394 return;
1395 }
1396
06653c70 1397 if (ntohs(ip->udp_len) < UDP_HDR_SIZE || ntohs(ip->udp_len) > len - IP_HDR_SIZE)
fe728806 1398 return;
1399
4ef8d53c 1400 debug_cond(DEBUG_DEV_PKT,
bc0571fc
JH
1401 "received UDP (to=%pI4, from=%pI4, len=%d)\n",
1402 &dst_ip, &src_ip, len);
4ef8d53c 1403
4b37fd14 1404 if (IS_ENABLED(CONFIG_UDP_CHECKSUM) && ip->udp_xsum != 0) {
b2f50807 1405 ulong xsum;
8524423d 1406 u8 *sumptr;
8534bf9a
SR
1407 ushort sumlen;
1408
1409 xsum = ip->ip_p;
1410 xsum += (ntohs(ip->udp_len));
049a95a7
JH
1411 xsum += (ntohl(ip->ip_src.s_addr) >> 16) & 0x0000ffff;
1412 xsum += (ntohl(ip->ip_src.s_addr) >> 0) & 0x0000ffff;
1413 xsum += (ntohl(ip->ip_dst.s_addr) >> 16) & 0x0000ffff;
1414 xsum += (ntohl(ip->ip_dst.s_addr) >> 0) & 0x0000ffff;
8534bf9a
SR
1415
1416 sumlen = ntohs(ip->udp_len);
8524423d 1417 sumptr = (u8 *)&ip->udp_src;
8534bf9a
SR
1418
1419 while (sumlen > 1) {
8524423d
HS
1420 /* inlined ntohs() to avoid alignment errors */
1421 xsum += (sumptr[0] << 8) + sumptr[1];
1422 sumptr += 2;
8534bf9a
SR
1423 sumlen -= 2;
1424 }
8524423d
HS
1425 if (sumlen > 0)
1426 xsum += (sumptr[0] << 8) + sumptr[0];
8534bf9a 1427 while ((xsum >> 16) != 0) {
3e38e429
LC
1428 xsum = (xsum & 0x0000ffff) +
1429 ((xsum >> 16) & 0x0000ffff);
8534bf9a
SR
1430 }
1431 if ((xsum != 0x00000000) && (xsum != 0x0000ffff)) {
9b55a253 1432 printf(" UDP wrong checksum %08lx %08x\n",
bc0571fc 1433 xsum, ntohs(ip->udp_xsum));
8534bf9a
SR
1434 return;
1435 }
1436 }
8534bf9a 1437
66c89ee3 1438#if defined(CONFIG_NETCONSOLE) && !defined(CONFIG_SPL_BUILD)
594c26f8 1439 nc_input_packet((uchar *)ip + IP_UDP_HDR_SIZE,
bc0571fc
JH
1440 src_ip,
1441 ntohs(ip->udp_dst),
1442 ntohs(ip->udp_src),
1443 ntohs(ip->udp_len) - UDP_HDR_SIZE);
68ceb29e 1444#endif
2d966958 1445 /*
bc0571fc 1446 * IP header OK. Pass the packet to the current handler.
2d966958 1447 */
ece223b5 1448 (*udp_packet_handler)((uchar *)ip + IP_UDP_HDR_SIZE,
bc0571fc
JH
1449 ntohs(ip->udp_dst),
1450 src_ip,
1451 ntohs(ip->udp_src),
1452 ntohs(ip->udp_len) - UDP_HDR_SIZE);
2d966958 1453 break;
d8970dae
LF
1454#ifdef CONFIG_CMD_WOL
1455 case PROT_WOL:
1456 wol_receive(ip, len);
1457 break;
09bd3d0b
SMJ
1458#endif
1459#ifdef CONFIG_PHY_NCSI
1460 case PROT_NCSI:
1461 ncsi_receive(et, ip, len);
1462 break;
d8970dae 1463#endif
2d966958
WD
1464 }
1465}
1466
2d966958
WD
1467/**********************************************************************/
1468
e4bf0c5c 1469static int net_check_prereq(enum proto_t protocol)
2d966958
WD
1470{
1471 switch (protocol) {
6e592385 1472 /* Fall through */
643d1ab2 1473#if defined(CONFIG_CMD_PING)
73a8b27c 1474 case PING:
049a95a7 1475 if (net_ping_ip.s_addr == 0) {
4f63acd0 1476 puts("*** ERROR: ping address not given\n");
92895de9 1477 return 1;
6e592385
WD
1478 }
1479 goto common;
cbd8a35c 1480#endif
eeb0a2c6
VM
1481#if defined(CONFIG_CMD_PING6)
1482 case PING6:
1483 if (ip6_is_unspecified_addr(&net_ping_ip6)) {
1484 puts("*** ERROR: ping address not given\n");
1485 return 1;
1486 }
1487 goto common;
1488#endif
1a32bf41
RG
1489#if defined(CONFIG_CMD_DNS)
1490 case DNS:
049a95a7 1491 if (net_dns_server.s_addr == 0) {
1a32bf41
RG
1492 puts("*** ERROR: DNS server address not given\n");
1493 return 1;
1494 }
1495 goto common;
1496#endif
b43ea1bf
PR
1497#if defined(CONFIG_PROT_UDP)
1498 case UDP:
1499 if (udp_prereq())
1500 return 1;
1501 goto common;
1502#endif
1503
643d1ab2 1504#if defined(CONFIG_CMD_NFS)
cbd8a35c 1505 case NFS:
73a8b27c 1506#endif
bc0571fc 1507 /* Fall through */
e4bf0c5c 1508 case TFTPGET:
1fb7cd49 1509 case TFTPPUT:
7fbf230d
VM
1510 if (IS_ENABLED(CONFIG_IPV6) && use_ip6) {
1511 if (!memcmp(&net_server_ip6, &net_null_addr_ip6,
1512 sizeof(struct in6_addr)) &&
1513 !strchr(net_boot_file_name, '[')) {
1514 puts("*** ERROR: `serverip6' not set\n");
1515 return 1;
1516 }
1517 } else if (net_server_ip.s_addr == 0 && !is_serverip_in_cmd()) {
4f63acd0 1518 puts("*** ERROR: `serverip' not set\n");
92895de9 1519 return 1;
6e592385 1520 }
912ece4c 1521#if defined(CONFIG_CMD_PING) || \
b43ea1bf 1522 defined(CONFIG_CMD_DNS) || defined(CONFIG_PROT_UDP)
4f63acd0 1523common:
73a8b27c 1524#endif
8b6bbe10 1525 /* Fall through */
73a8b27c 1526
8b6bbe10 1527 case NETCONS:
443d3191
DM
1528 case FASTBOOT_UDP:
1529 case FASTBOOT_TCP:
7a83af07 1530 case TFTPSRV:
7fbf230d
VM
1531 if (IS_ENABLED(CONFIG_IPV6) && use_ip6) {
1532 if (!memcmp(&net_link_local_ip6, &net_null_addr_ip6,
1533 sizeof(struct in6_addr))) {
1534 puts("*** ERROR: `ip6addr` not set\n");
1535 return 1;
1536 }
1537 } else if (net_ip.s_addr == 0) {
4f63acd0 1538 puts("*** ERROR: `ipaddr' not set\n");
92895de9 1539 return 1;
6e592385
WD
1540 }
1541 /* Fall through */
2d966958 1542
bf6cb247 1543#ifdef CONFIG_CMD_RARP
2d966958 1544 case RARP:
09bd3d0b
SMJ
1545#endif
1546#ifdef CONFIG_PHY_NCSI
1547 case NCSI:
bf6cb247 1548#endif
2d966958 1549 case BOOTP:
a3d991bd 1550 case CDP:
bf6cb247 1551 case DHCP:
d22c338e 1552 case LINKLOCAL:
0adb5b76 1553 if (memcmp(net_ethaddr, "\0\0\0\0\0\0", 6) == 0) {
4f63acd0 1554 int num = eth_get_dev_index();
2d966958 1555
6e592385
WD
1556 switch (num) {
1557 case -1:
4f63acd0 1558 puts("*** ERROR: No ethernet found.\n");
92895de9 1559 return 1;
6e592385 1560 case 0:
4f63acd0 1561 puts("*** ERROR: `ethaddr' not set\n");
2d966958 1562 break;
6e592385 1563 default:
4f63acd0 1564 printf("*** ERROR: `eth%daddr' not set\n",
bc0571fc 1565 num);
2d966958 1566 break;
6e592385 1567 }
2d966958 1568
bc0571fc 1569 net_start_again();
92895de9 1570 return 2;
6e592385
WD
1571 }
1572 /* Fall through */
1573 default:
92895de9 1574 return 0;
2d966958 1575 }
92895de9 1576 return 0; /* OK */
2d966958
WD
1577}
1578/**********************************************************************/
1579
a3d991bd 1580int
1203fcce 1581net_eth_hdr_size(void)
a3d991bd
WD
1582{
1583 ushort myvlanid;
2d966958 1584
4fd5055f 1585 myvlanid = ntohs(net_our_vlan);
a3d991bd
WD
1586 if (myvlanid == (ushort)-1)
1587 myvlanid = VLAN_NONE;
1588
3e38e429
LC
1589 return ((myvlanid & VLAN_IDMASK) == VLAN_NONE) ? ETHER_HDR_SIZE :
1590 VLAN_ETHER_HDR_SIZE;
a3d991bd
WD
1591}
1592
1203fcce 1593int net_set_ether(uchar *xet, const uchar *dest_ethaddr, uint prot)
2d966958 1594{
cb487f56 1595 struct ethernet_hdr *et = (struct ethernet_hdr *)xet;
a3d991bd
WD
1596 ushort myvlanid;
1597
4fd5055f 1598 myvlanid = ntohs(net_our_vlan);
a3d991bd
WD
1599 if (myvlanid == (ushort)-1)
1600 myvlanid = VLAN_NONE;
2d966958 1601
0adb5b76
JH
1602 memcpy(et->et_dest, dest_ethaddr, 6);
1603 memcpy(et->et_src, net_ethaddr, 6);
a3d991bd 1604 if ((myvlanid & VLAN_IDMASK) == VLAN_NONE) {
c819abee 1605 et->et_protlen = htons(prot);
a3d991bd
WD
1606 return ETHER_HDR_SIZE;
1607 } else {
c68cca35
JH
1608 struct vlan_ethernet_hdr *vet =
1609 (struct vlan_ethernet_hdr *)xet;
2d966958 1610
a3d991bd
WD
1611 vet->vet_vlan_type = htons(PROT_VLAN);
1612 vet->vet_tag = htons((0 << 5) | (myvlanid & VLAN_IDMASK));
1613 vet->vet_type = htons(prot);
1614 return VLAN_ETHER_HDR_SIZE;
1615 }
1616}
2d966958 1617
e7111015
JH
1618int net_update_ether(struct ethernet_hdr *et, uchar *addr, uint prot)
1619{
1620 ushort protlen;
1621
1622 memcpy(et->et_dest, addr, 6);
0adb5b76 1623 memcpy(et->et_src, net_ethaddr, 6);
e7111015
JH
1624 protlen = ntohs(et->et_protlen);
1625 if (protlen == PROT_VLAN) {
1626 struct vlan_ethernet_hdr *vet =
1627 (struct vlan_ethernet_hdr *)et;
1628 vet->vet_type = htons(prot);
1629 return VLAN_ETHER_HDR_SIZE;
1630 } else if (protlen > 1514) {
1631 et->et_protlen = htons(prot);
1632 return ETHER_HDR_SIZE;
1633 } else {
1634 /* 802.2 + SNAP */
1635 struct e802_hdr *et802 = (struct e802_hdr *)et;
1636 et802->et_prot = htons(prot);
1637 return E802_HDR_SIZE;
1638 }
1639}
1640
5d457ecb
DH
1641void net_set_ip_header(uchar *pkt, struct in_addr dest, struct in_addr source,
1642 u16 pkt_len, u8 proto)
2d966958 1643{
4b11c916 1644 struct ip_udp_hdr *ip = (struct ip_udp_hdr *)pkt;
2d966958
WD
1645
1646 /*
4b11c916 1647 * Construct an IP header.
2d966958 1648 */
3e38e429
LC
1649 /* IP_HDR_SIZE / 4 (not including UDP) */
1650 ip->ip_hl_v = 0x45;
2d966958 1651 ip->ip_tos = 0;
5d457ecb
DH
1652 ip->ip_len = htons(pkt_len);
1653 ip->ip_p = proto;
bc0571fc 1654 ip->ip_id = htons(net_ip_id++);
e0c07b86 1655 ip->ip_off = htons(IP_FLAGS_DFRAG); /* Don't fragment */
2d966958 1656 ip->ip_ttl = 255;
2d966958 1657 ip->ip_sum = 0;
3e38e429 1658 /* already in network byte order */
049a95a7 1659 net_copy_ip((void *)&ip->ip_src, &source);
4b11c916 1660 /* already in network byte order */
049a95a7 1661 net_copy_ip((void *)&ip->ip_dst, &dest);
5d457ecb
DH
1662
1663 ip->ip_sum = compute_ip_checksum(ip, IP_HDR_SIZE);
4b11c916
JH
1664}
1665
049a95a7 1666void net_set_udp_header(uchar *pkt, struct in_addr dest, int dport, int sport,
4b11c916
JH
1667 int len)
1668{
1669 struct ip_udp_hdr *ip = (struct ip_udp_hdr *)pkt;
1670
1671 /*
1672 * If the data is an odd number of bytes, zero the
1673 * byte after the last byte so that the checksum
1674 * will work.
1675 */
1676 if (len & 1)
1677 pkt[IP_UDP_HDR_SIZE + len] = 0;
1678
5d457ecb
DH
1679 net_set_ip_header(pkt, dest, net_ip, IP_UDP_HDR_SIZE + len,
1680 IPPROTO_UDP);
4b11c916 1681
2d966958
WD
1682 ip->udp_src = htons(sport);
1683 ip->udp_dst = htons(dport);
594c26f8 1684 ip->udp_len = htons(UDP_HDR_SIZE + len);
2d966958 1685 ip->udp_xsum = 0;
2d966958
WD
1686}
1687
4f63acd0 1688void copy_filename(char *dst, const char *src, int size)
2d966958 1689{
16cf145f 1690 if (src && *src && (*src == '"')) {
2d966958
WD
1691 ++src;
1692 --size;
1693 }
1694
16cf145f 1695 while ((--size > 0) && src && *src && (*src != '"'))
2d966958 1696 *dst++ = *src++;
2d966958
WD
1697 *dst = '\0';
1698}
1699
3a66fcb7
JH
1700int is_serverip_in_cmd(void)
1701{
1702 return !!strchr(net_boot_file_name, ':');
1703}
1704
6ab12830
JH
1705int net_parse_bootfile(struct in_addr *ipaddr, char *filename, int max_len)
1706{
1707 char *colon;
85f8e03b
LF
1708 struct in_addr ip;
1709 ip.s_addr = 0;
6ab12830
JH
1710
1711 if (net_boot_file_name[0] == '\0')
1712 return 0;
1713
1714 colon = strchr(net_boot_file_name, ':');
1715 if (colon) {
85f8e03b
LF
1716 ip = string_to_ip(net_boot_file_name);
1717 if (ipaddr && ip.s_addr)
1718 *ipaddr = ip;
1719 }
1720 if (ip.s_addr) {
6ab12830
JH
1721 strncpy(filename, colon + 1, max_len);
1722 } else {
1723 strncpy(filename, net_boot_file_name, max_len);
1724 }
1725 filename[max_len - 1] = '\0';
1726
1727 return 1;
1728}
1729
049a95a7 1730void ip_to_string(struct in_addr x, char *s)
2d966958 1731{
049a95a7 1732 x.s_addr = ntohl(x.s_addr);
4f63acd0 1733 sprintf(s, "%d.%d.%d.%d",
049a95a7
JH
1734 (int) ((x.s_addr >> 24) & 0xff),
1735 (int) ((x.s_addr >> 16) & 0xff),
1736 (int) ((x.s_addr >> 8) & 0xff),
1737 (int) ((x.s_addr >> 0) & 0xff)
6e592385 1738 );
2d966958
WD
1739}
1740
4fd5055f 1741void vlan_to_string(ushort x, char *s)
a3d991bd
WD
1742{
1743 x = ntohs(x);
1744
1745 if (x == (ushort)-1)
1746 x = VLAN_NONE;
1747
1748 if (x == VLAN_NONE)
1749 strcpy(s, "none");
1750 else
1751 sprintf(s, "%d", x & VLAN_IDMASK);
1752}
1753
4fd5055f 1754ushort string_to_vlan(const char *s)
a3d991bd
WD
1755{
1756 ushort id;
1757
1758 if (s == NULL)
b9711de1 1759 return htons(VLAN_NONE);
a3d991bd
WD
1760
1761 if (*s < '0' || *s > '9')
1762 id = VLAN_NONE;
1763 else
0b1284eb 1764 id = (ushort)dectoul(s, NULL);
a3d991bd 1765
b9711de1 1766 return htons(id);
a3d991bd
WD
1767}
1768
723806cc 1769ushort env_get_vlan(char *var)
a3d991bd 1770{
00caae6d 1771 return string_to_vlan(env_get(var));
a3d991bd 1772}