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