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