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 * RARP: 27 * 28 * Prerequisites: - own ethernet address 29 * We want: - own IP address 30 * - TFTP server IP address 31 * Next step: ARP 32 * 33 * ARP: 34 * 35 * Prerequisites: - own ethernet address 36 * - own IP address 37 * - TFTP server IP address 38 * We want: - TFTP server ethernet address 39 * Next step: TFTP 40 * 41 * DHCP: 42 * 43 * Prerequisites: - own ethernet address 44 * We want: - IP, Netmask, ServerIP, Gateway IP 45 * - bootfilename, lease time 46 * Next step: - TFTP 47 * 48 * TFTP: 49 * 50 * Prerequisites: - own ethernet address 51 * - own IP address 52 * - TFTP server IP address 53 * - TFTP server ethernet address 54 * - name of bootfile (if unknown, we use a default name 55 * derived from our own IP address) 56 * We want: - load the boot file 57 * Next step: none 58 * 59 * NFS: 60 * 61 * Prerequisites: - own ethernet address 62 * - own IP address 63 * - name of bootfile (if unknown, we use a default name 64 * derived from our own IP address) 65 * We want: - load the boot file 66 * Next step: none 67 * 68 * SNTP: 69 * 70 * Prerequisites: - own ethernet address 71 * - own IP address 72 * We want: - network time 73 * Next step: none 74 */ 75 76 77 #include <common.h> 78 #include <watchdog.h> 79 #include <command.h> 80 #include <net.h> 81 #include "bootp.h" 82 #include "tftp.h" 83 #ifdef CONFIG_CMD_RARP 84 #include "rarp.h" 85 #endif 86 #include "nfs.h" 87 #ifdef CONFIG_STATUS_LED 88 #include <status_led.h> 89 #include <miiphy.h> 90 #endif 91 #if defined(CONFIG_CMD_SNTP) 92 #include "sntp.h" 93 #endif 94 #if defined(CONFIG_CDP_VERSION) 95 #include <timestamp.h> 96 #endif 97 #if defined(CONFIG_CMD_DNS) 98 #include "dns.h" 99 #endif 100 101 DECLARE_GLOBAL_DATA_PTR; 102 103 #ifndef CONFIG_ARP_TIMEOUT 104 /* Milliseconds before trying ARP again */ 105 # define ARP_TIMEOUT 5000UL 106 #else 107 # define ARP_TIMEOUT CONFIG_ARP_TIMEOUT 108 #endif 109 110 111 #ifndef CONFIG_NET_RETRY_COUNT 112 # define ARP_TIMEOUT_COUNT 5 /* # of timeouts before giving up */ 113 #else 114 # define ARP_TIMEOUT_COUNT CONFIG_NET_RETRY_COUNT 115 #endif 116 117 /** BOOTP EXTENTIONS **/ 118 119 /* Our subnet mask (0=unknown) */ 120 IPaddr_t NetOurSubnetMask=0; 121 /* Our gateways IP address */ 122 IPaddr_t NetOurGatewayIP=0; 123 /* Our DNS IP address */ 124 IPaddr_t NetOurDNSIP=0; 125 #if defined(CONFIG_BOOTP_DNS2) 126 /* Our 2nd DNS IP address */ 127 IPaddr_t NetOurDNS2IP=0; 128 #endif 129 /* Our NIS domain */ 130 char NetOurNISDomain[32]={0,}; 131 /* Our hostname */ 132 char NetOurHostName[32]={0,}; 133 /* Our bootpath */ 134 char NetOurRootPath[64]={0,}; 135 /* Our bootfile size in blocks */ 136 ushort NetBootFileSize=0; 137 138 #ifdef CONFIG_MCAST_TFTP /* Multicast TFTP */ 139 IPaddr_t Mcast_addr; 140 #endif 141 142 /** END OF BOOTP EXTENTIONS **/ 143 144 /* The actual transferred size of the bootfile (in bytes) */ 145 ulong NetBootFileXferSize; 146 /* Our ethernet address */ 147 uchar NetOurEther[6]; 148 /* Boot server enet address */ 149 uchar NetServerEther[6] = 150 { 0, 0, 0, 0, 0, 0 }; 151 /* Our IP addr (0 = unknown) */ 152 IPaddr_t NetOurIP; 153 /* Server IP addr (0 = unknown) */ 154 IPaddr_t NetServerIP; 155 /* Current receive packet */ 156 volatile uchar *NetRxPacket; 157 /* Current rx packet length */ 158 int NetRxPacketLen; 159 /* IP packet ID */ 160 unsigned NetIPID; 161 /* Ethernet bcast address */ 162 uchar NetBcastAddr[6] = 163 { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff }; 164 uchar NetEtherNullAddr[6] = 165 { 0, 0, 0, 0, 0, 0 }; 166 #ifdef CONFIG_API 167 void (*push_packet)(volatile void *, int len) = 0; 168 #endif 169 #if defined(CONFIG_CMD_CDP) 170 /* Ethernet bcast address */ 171 uchar NetCDPAddr[6] = 172 { 0x01, 0x00, 0x0c, 0xcc, 0xcc, 0xcc }; 173 #endif 174 /* Network loop state */ 175 int NetState; 176 #ifdef CONFIG_NET_MULTI 177 /* Tried all network devices */ 178 int NetRestartWrap = 0; 179 /* Network loop restarted */ 180 static int NetRestarted = 0; 181 /* At least one device configured */ 182 static int NetDevExists = 0; 183 #endif 184 185 /* XXX in both little & big endian machines 0xFFFF == ntohs(-1) */ 186 /* default is without VLAN */ 187 ushort NetOurVLAN = 0xFFFF; 188 /* ditto */ 189 ushort NetOurNativeVLAN = 0xFFFF; 190 191 /* Boot File name */ 192 char BootFile[128]; 193 194 #if defined(CONFIG_CMD_PING) 195 /* the ip address to ping */ 196 IPaddr_t NetPingIP; 197 198 static void PingStart(void); 199 #endif 200 201 #if defined(CONFIG_CMD_CDP) 202 static void CDPStart(void); 203 #endif 204 205 #if defined(CONFIG_CMD_SNTP) 206 /* NTP server IP address */ 207 IPaddr_t NetNtpServerIP; 208 /* offset time from UTC */ 209 int NetTimeOffset=0; 210 #endif 211 212 #ifdef CONFIG_NETCONSOLE 213 void NcStart(void); 214 int nc_input_packet(uchar *pkt, unsigned dest, unsigned src, unsigned len); 215 #endif 216 217 volatile uchar PktBuf[(PKTBUFSRX+1) * PKTSIZE_ALIGN + PKTALIGN]; 218 219 /* Receive packet */ 220 volatile uchar *NetRxPackets[PKTBUFSRX]; 221 222 /* Current RX packet handler */ 223 static rxhand_f *packetHandler; 224 /* Current timeout handler */ 225 static thand_f *timeHandler; 226 /* Time base value */ 227 static ulong timeStart; 228 /* Current timeout value */ 229 static ulong timeDelta; 230 /* THE transmit packet */ 231 volatile uchar *NetTxPacket = 0; 232 233 static int net_check_prereq (proto_t protocol); 234 235 static int NetTryCount; 236 237 /**********************************************************************/ 238 239 IPaddr_t NetArpWaitPacketIP; 240 IPaddr_t NetArpWaitReplyIP; 241 /* MAC address of waiting packet's destination */ 242 uchar *NetArpWaitPacketMAC; 243 /* THE transmit packet */ 244 uchar *NetArpWaitTxPacket; 245 int NetArpWaitTxPacketSize; 246 uchar NetArpWaitPacketBuf[PKTSIZE_ALIGN + PKTALIGN]; 247 ulong NetArpWaitTimerStart; 248 int NetArpWaitTry; 249 250 void ArpRequest (void) 251 { 252 int i; 253 volatile uchar *pkt; 254 ARP_t *arp; 255 256 debug("ARP broadcast %d\n", NetArpWaitTry); 257 258 pkt = NetTxPacket; 259 260 pkt += NetSetEther (pkt, NetBcastAddr, PROT_ARP); 261 262 arp = (ARP_t *) pkt; 263 264 arp->ar_hrd = htons (ARP_ETHER); 265 arp->ar_pro = htons (PROT_IP); 266 arp->ar_hln = 6; 267 arp->ar_pln = 4; 268 arp->ar_op = htons (ARPOP_REQUEST); 269 270 /* source ET addr */ 271 memcpy (&arp->ar_data[0], NetOurEther, 6); 272 /* source IP addr */ 273 NetWriteIP ((uchar *) & arp->ar_data[6], NetOurIP); 274 for (i = 10; i < 16; ++i) { 275 /* dest ET addr = 0 */ 276 arp->ar_data[i] = 0; 277 } 278 279 if ((NetArpWaitPacketIP & NetOurSubnetMask) != 280 (NetOurIP & NetOurSubnetMask)) { 281 if (NetOurGatewayIP == 0) { 282 puts ("## Warning: gatewayip needed but not set\n"); 283 NetArpWaitReplyIP = NetArpWaitPacketIP; 284 } else { 285 NetArpWaitReplyIP = NetOurGatewayIP; 286 } 287 } else { 288 NetArpWaitReplyIP = NetArpWaitPacketIP; 289 } 290 291 NetWriteIP ((uchar *) & arp->ar_data[16], NetArpWaitReplyIP); 292 (void) eth_send (NetTxPacket, (pkt - NetTxPacket) + ARP_HDR_SIZE); 293 } 294 295 void ArpTimeoutCheck(void) 296 { 297 ulong t; 298 299 if (!NetArpWaitPacketIP) 300 return; 301 302 t = get_timer(0); 303 304 /* check for arp timeout */ 305 if ((t - NetArpWaitTimerStart) > ARP_TIMEOUT) { 306 NetArpWaitTry++; 307 308 if (NetArpWaitTry >= ARP_TIMEOUT_COUNT) { 309 puts ("\nARP Retry count exceeded; starting again\n"); 310 NetArpWaitTry = 0; 311 NetStartAgain(); 312 } else { 313 NetArpWaitTimerStart = t; 314 ArpRequest(); 315 } 316 } 317 } 318 319 static void 320 NetInitLoop(proto_t protocol) 321 { 322 static int env_changed_id = 0; 323 bd_t *bd = gd->bd; 324 int env_id = get_env_id (); 325 326 /* update only when the environment has changed */ 327 if (env_changed_id != env_id) { 328 NetCopyIP(&NetOurIP, &bd->bi_ip_addr); 329 NetOurGatewayIP = getenv_IPaddr ("gatewayip"); 330 NetOurSubnetMask= getenv_IPaddr ("netmask"); 331 NetServerIP = getenv_IPaddr ("serverip"); 332 NetOurNativeVLAN = getenv_VLAN("nvlan"); 333 NetOurVLAN = getenv_VLAN("vlan"); 334 #if defined(CONFIG_CMD_DNS) 335 NetOurDNSIP = getenv_IPaddr("dnsip"); 336 #endif 337 env_changed_id = env_id; 338 } 339 340 return; 341 } 342 343 /**********************************************************************/ 344 /* 345 * Main network processing loop. 346 */ 347 348 int 349 NetLoop(proto_t protocol) 350 { 351 bd_t *bd = gd->bd; 352 353 #ifdef CONFIG_NET_MULTI 354 NetRestarted = 0; 355 NetDevExists = 0; 356 #endif 357 358 /* XXX problem with bss workaround */ 359 NetArpWaitPacketMAC = NULL; 360 NetArpWaitTxPacket = NULL; 361 NetArpWaitPacketIP = 0; 362 NetArpWaitReplyIP = 0; 363 NetArpWaitTxPacket = NULL; 364 NetTxPacket = NULL; 365 NetTryCount = 1; 366 367 if (!NetTxPacket) { 368 int i; 369 /* 370 * Setup packet buffers, aligned correctly. 371 */ 372 NetTxPacket = &PktBuf[0] + (PKTALIGN - 1); 373 NetTxPacket -= (ulong)NetTxPacket % PKTALIGN; 374 for (i = 0; i < PKTBUFSRX; i++) { 375 NetRxPackets[i] = NetTxPacket + (i+1)*PKTSIZE_ALIGN; 376 } 377 } 378 379 if (!NetArpWaitTxPacket) { 380 NetArpWaitTxPacket = &NetArpWaitPacketBuf[0] + (PKTALIGN - 1); 381 NetArpWaitTxPacket -= (ulong)NetArpWaitTxPacket % PKTALIGN; 382 NetArpWaitTxPacketSize = 0; 383 } 384 385 eth_halt(); 386 #ifdef CONFIG_NET_MULTI 387 eth_set_current(); 388 #endif 389 if (eth_init(bd) < 0) { 390 eth_halt(); 391 return(-1); 392 } 393 394 restart: 395 #ifdef CONFIG_NET_MULTI 396 memcpy (NetOurEther, eth_get_dev()->enetaddr, 6); 397 #else 398 eth_getenv_enetaddr("ethaddr", NetOurEther); 399 #endif 400 401 NetState = NETLOOP_CONTINUE; 402 403 /* 404 * Start the ball rolling with the given start function. From 405 * here on, this code is a state machine driven by received 406 * packets and timer events. 407 */ 408 NetInitLoop(protocol); 409 410 switch (net_check_prereq (protocol)) { 411 case 1: 412 /* network not configured */ 413 eth_halt(); 414 return (-1); 415 416 #ifdef CONFIG_NET_MULTI 417 case 2: 418 /* network device not configured */ 419 break; 420 #endif /* CONFIG_NET_MULTI */ 421 422 case 0: 423 #ifdef CONFIG_NET_MULTI 424 NetDevExists = 1; 425 #endif 426 switch (protocol) { 427 case TFTP: 428 /* always use ARP to get server ethernet address */ 429 TftpStart(); 430 break; 431 432 #if defined(CONFIG_CMD_DHCP) 433 case DHCP: 434 BootpTry = 0; 435 NetOurIP = 0; 436 DhcpRequest(); /* Basically same as BOOTP */ 437 break; 438 #endif 439 440 case BOOTP: 441 BootpTry = 0; 442 NetOurIP = 0; 443 BootpRequest (); 444 break; 445 446 #if defined(CONFIG_CMD_RARP) 447 case RARP: 448 RarpTry = 0; 449 NetOurIP = 0; 450 RarpRequest (); 451 break; 452 #endif 453 #if defined(CONFIG_CMD_PING) 454 case PING: 455 PingStart(); 456 break; 457 #endif 458 #if defined(CONFIG_CMD_NFS) 459 case NFS: 460 NfsStart(); 461 break; 462 #endif 463 #if defined(CONFIG_CMD_CDP) 464 case CDP: 465 CDPStart(); 466 break; 467 #endif 468 #ifdef CONFIG_NETCONSOLE 469 case NETCONS: 470 NcStart(); 471 break; 472 #endif 473 #if defined(CONFIG_CMD_SNTP) 474 case SNTP: 475 SntpStart(); 476 break; 477 #endif 478 #if defined(CONFIG_CMD_DNS) 479 case DNS: 480 DnsStart(); 481 break; 482 #endif 483 default: 484 break; 485 } 486 487 NetBootFileXferSize = 0; 488 break; 489 } 490 491 #if defined(CONFIG_MII) || defined(CONFIG_CMD_MII) 492 #if defined(CONFIG_SYS_FAULT_ECHO_LINK_DOWN) && \ 493 defined(CONFIG_STATUS_LED) && \ 494 defined(STATUS_LED_RED) 495 /* 496 * Echo the inverted link state to the fault LED. 497 */ 498 if(miiphy_link(eth_get_dev()->name, CONFIG_SYS_FAULT_MII_ADDR)) { 499 status_led_set (STATUS_LED_RED, STATUS_LED_OFF); 500 } else { 501 status_led_set (STATUS_LED_RED, STATUS_LED_ON); 502 } 503 #endif /* CONFIG_SYS_FAULT_ECHO_LINK_DOWN, ... */ 504 #endif /* CONFIG_MII, ... */ 505 506 /* 507 * Main packet reception loop. Loop receiving packets until 508 * someone sets `NetState' to a state that terminates. 509 */ 510 for (;;) { 511 WATCHDOG_RESET(); 512 #ifdef CONFIG_SHOW_ACTIVITY 513 { 514 extern void show_activity(int arg); 515 show_activity(1); 516 } 517 #endif 518 /* 519 * Check the ethernet for a new packet. The ethernet 520 * receive routine will process it. 521 */ 522 eth_rx(); 523 524 /* 525 * Abort if ctrl-c was pressed. 526 */ 527 if (ctrlc()) { 528 eth_halt(); 529 puts ("\nAbort\n"); 530 return (-1); 531 } 532 533 ArpTimeoutCheck(); 534 535 /* 536 * Check for a timeout, and run the timeout handler 537 * if we have one. 538 */ 539 if (timeHandler && ((get_timer(0) - timeStart) > timeDelta)) { 540 thand_f *x; 541 542 #if defined(CONFIG_MII) || defined(CONFIG_CMD_MII) 543 # if defined(CONFIG_SYS_FAULT_ECHO_LINK_DOWN) && \ 544 defined(CONFIG_STATUS_LED) && \ 545 defined(STATUS_LED_RED) 546 /* 547 * Echo the inverted link state to the fault LED. 548 */ 549 if(miiphy_link(eth_get_dev()->name, 550 CONFIG_SYS_FAULT_MII_ADDR)) { 551 status_led_set (STATUS_LED_RED, STATUS_LED_OFF); 552 } else { 553 status_led_set (STATUS_LED_RED, STATUS_LED_ON); 554 } 555 # endif /* CONFIG_SYS_FAULT_ECHO_LINK_DOWN, ... */ 556 #endif /* CONFIG_MII, ... */ 557 x = timeHandler; 558 timeHandler = (thand_f *)0; 559 (*x)(); 560 } 561 562 563 switch (NetState) { 564 565 case NETLOOP_RESTART: 566 #ifdef CONFIG_NET_MULTI 567 NetRestarted = 1; 568 #endif 569 goto restart; 570 571 case NETLOOP_SUCCESS: 572 if (NetBootFileXferSize > 0) { 573 char buf[20]; 574 printf("Bytes transferred = %ld (%lx hex)\n", 575 NetBootFileXferSize, 576 NetBootFileXferSize); 577 sprintf(buf, "%lX", NetBootFileXferSize); 578 setenv("filesize", buf); 579 580 sprintf(buf, "%lX", (unsigned long)load_addr); 581 setenv("fileaddr", buf); 582 } 583 eth_halt(); 584 return NetBootFileXferSize; 585 586 case NETLOOP_FAIL: 587 return (-1); 588 } 589 } 590 } 591 592 /**********************************************************************/ 593 594 static void 595 startAgainTimeout(void) 596 { 597 NetState = NETLOOP_RESTART; 598 } 599 600 static void 601 startAgainHandler(uchar *pkt, unsigned dest, IPaddr_t sip, 602 unsigned src, unsigned len) 603 { 604 /* Totally ignore the packet */ 605 } 606 607 void NetStartAgain (void) 608 { 609 char *nretry; 610 int retry_forever = 0; 611 unsigned long retrycnt = 0; 612 613 nretry = getenv("netretry"); 614 if (nretry) { 615 if (!strcmp(nretry, "yes")) 616 retry_forever = 1; 617 else if (!strcmp(nretry, "no")) 618 retrycnt = 0; 619 else if (!strcmp(nretry, "once")) 620 retrycnt = 1; 621 else 622 retrycnt = simple_strtoul(nretry, NULL, 0); 623 } else 624 retry_forever = 1; 625 626 if ((!retry_forever) && (NetTryCount >= retrycnt)) { 627 eth_halt(); 628 NetState = NETLOOP_FAIL; 629 return; 630 } 631 632 NetTryCount++; 633 634 #ifndef CONFIG_NET_MULTI 635 NetSetTimeout (10000UL, startAgainTimeout); 636 NetSetHandler (startAgainHandler); 637 #else /* !CONFIG_NET_MULTI*/ 638 eth_halt (); 639 #if !defined(CONFIG_NET_DO_NOT_TRY_ANOTHER) 640 eth_try_another (!NetRestarted); 641 #endif 642 eth_init (gd->bd); 643 if (NetRestartWrap) { 644 NetRestartWrap = 0; 645 if (NetDevExists) { 646 NetSetTimeout (10000UL, startAgainTimeout); 647 NetSetHandler (startAgainHandler); 648 } else { 649 NetState = NETLOOP_FAIL; 650 } 651 } else { 652 NetState = NETLOOP_RESTART; 653 } 654 #endif /* CONFIG_NET_MULTI */ 655 } 656 657 /**********************************************************************/ 658 /* 659 * Miscelaneous bits. 660 */ 661 662 void 663 NetSetHandler(rxhand_f * f) 664 { 665 packetHandler = f; 666 } 667 668 669 void 670 NetSetTimeout(ulong iv, thand_f * f) 671 { 672 if (iv == 0) { 673 timeHandler = (thand_f *)0; 674 } else { 675 timeHandler = f; 676 timeStart = get_timer(0); 677 timeDelta = iv; 678 } 679 } 680 681 682 void 683 NetSendPacket(volatile uchar * pkt, int len) 684 { 685 (void) eth_send(pkt, len); 686 } 687 688 int 689 NetSendUDPPacket(uchar *ether, IPaddr_t dest, int dport, int sport, int len) 690 { 691 uchar *pkt; 692 693 /* convert to new style broadcast */ 694 if (dest == 0) 695 dest = 0xFFFFFFFF; 696 697 /* if broadcast, make the ether address a broadcast and don't do ARP */ 698 if (dest == 0xFFFFFFFF) 699 ether = NetBcastAddr; 700 701 /* 702 * if MAC address was not discovered yet, save the packet and do 703 * an ARP request 704 */ 705 if (memcmp(ether, NetEtherNullAddr, 6) == 0) { 706 707 debug("sending ARP for %08lx\n", dest); 708 709 NetArpWaitPacketIP = dest; 710 NetArpWaitPacketMAC = ether; 711 712 pkt = NetArpWaitTxPacket; 713 pkt += NetSetEther (pkt, NetArpWaitPacketMAC, PROT_IP); 714 715 NetSetIP (pkt, dest, dport, sport, len); 716 memcpy(pkt + IP_HDR_SIZE, (uchar *)NetTxPacket + 717 (pkt - (uchar *)NetArpWaitTxPacket) + IP_HDR_SIZE, len); 718 719 /* size of the waiting packet */ 720 NetArpWaitTxPacketSize = (pkt - NetArpWaitTxPacket) + 721 IP_HDR_SIZE + len; 722 723 /* and do the ARP request */ 724 NetArpWaitTry = 1; 725 NetArpWaitTimerStart = get_timer(0); 726 ArpRequest(); 727 return 1; /* waiting */ 728 } 729 730 debug("sending UDP to %08lx/%pM\n", dest, ether); 731 732 pkt = (uchar *)NetTxPacket; 733 pkt += NetSetEther (pkt, ether, PROT_IP); 734 NetSetIP (pkt, dest, dport, sport, len); 735 (void) eth_send(NetTxPacket, (pkt - NetTxPacket) + IP_HDR_SIZE + len); 736 737 return 0; /* transmitted */ 738 } 739 740 #if defined(CONFIG_CMD_PING) 741 static ushort PingSeqNo; 742 743 int PingSend(void) 744 { 745 static uchar mac[6]; 746 volatile IP_t *ip; 747 volatile ushort *s; 748 uchar *pkt; 749 750 /* XXX always send arp request */ 751 752 memcpy(mac, NetEtherNullAddr, 6); 753 754 debug("sending ARP for %08lx\n", NetPingIP); 755 756 NetArpWaitPacketIP = NetPingIP; 757 NetArpWaitPacketMAC = mac; 758 759 pkt = NetArpWaitTxPacket; 760 pkt += NetSetEther(pkt, mac, PROT_IP); 761 762 ip = (volatile IP_t *)pkt; 763 764 /* 765 * Construct an IP and ICMP header. 766 * (need to set no fragment bit - XXX) 767 */ 768 /* IP_HDR_SIZE / 4 (not including UDP) */ 769 ip->ip_hl_v = 0x45; 770 ip->ip_tos = 0; 771 ip->ip_len = htons(IP_HDR_SIZE_NO_UDP + 8); 772 ip->ip_id = htons(NetIPID++); 773 ip->ip_off = htons(IP_FLAGS_DFRAG); /* Don't fragment */ 774 ip->ip_ttl = 255; 775 ip->ip_p = 0x01; /* ICMP */ 776 ip->ip_sum = 0; 777 /* already in network byte order */ 778 NetCopyIP((void*)&ip->ip_src, &NetOurIP); 779 /* - "" - */ 780 NetCopyIP((void*)&ip->ip_dst, &NetPingIP); 781 ip->ip_sum = ~NetCksum((uchar *)ip, IP_HDR_SIZE_NO_UDP / 2); 782 783 s = &ip->udp_src; /* XXX ICMP starts here */ 784 s[0] = htons(0x0800); /* echo-request, code */ 785 s[1] = 0; /* checksum */ 786 s[2] = 0; /* identifier */ 787 s[3] = htons(PingSeqNo++); /* sequence number */ 788 s[1] = ~NetCksum((uchar *)s, 8/2); 789 790 /* size of the waiting packet */ 791 NetArpWaitTxPacketSize = 792 (pkt - NetArpWaitTxPacket) + IP_HDR_SIZE_NO_UDP + 8; 793 794 /* and do the ARP request */ 795 NetArpWaitTry = 1; 796 NetArpWaitTimerStart = get_timer(0); 797 ArpRequest(); 798 return 1; /* waiting */ 799 } 800 801 static void 802 PingTimeout (void) 803 { 804 eth_halt(); 805 NetState = NETLOOP_FAIL; /* we did not get the reply */ 806 } 807 808 static void 809 PingHandler(uchar *pkt, unsigned dest, IPaddr_t sip, unsigned src, 810 unsigned len) 811 { 812 if (sip != NetPingIP) 813 return; 814 815 NetState = NETLOOP_SUCCESS; 816 } 817 818 static void PingStart(void) 819 { 820 #if defined(CONFIG_NET_MULTI) 821 printf ("Using %s device\n", eth_get_name()); 822 #endif /* CONFIG_NET_MULTI */ 823 NetSetTimeout (10000UL, PingTimeout); 824 NetSetHandler (PingHandler); 825 826 PingSend(); 827 } 828 #endif 829 830 #if defined(CONFIG_CMD_CDP) 831 832 #define CDP_DEVICE_ID_TLV 0x0001 833 #define CDP_ADDRESS_TLV 0x0002 834 #define CDP_PORT_ID_TLV 0x0003 835 #define CDP_CAPABILITIES_TLV 0x0004 836 #define CDP_VERSION_TLV 0x0005 837 #define CDP_PLATFORM_TLV 0x0006 838 #define CDP_NATIVE_VLAN_TLV 0x000a 839 #define CDP_APPLIANCE_VLAN_TLV 0x000e 840 #define CDP_TRIGGER_TLV 0x000f 841 #define CDP_POWER_CONSUMPTION_TLV 0x0010 842 #define CDP_SYSNAME_TLV 0x0014 843 #define CDP_SYSOBJECT_TLV 0x0015 844 #define CDP_MANAGEMENT_ADDRESS_TLV 0x0016 845 846 #define CDP_TIMEOUT 250UL /* one packet every 250ms */ 847 848 static int CDPSeq; 849 static int CDPOK; 850 851 ushort CDPNativeVLAN; 852 ushort CDPApplianceVLAN; 853 854 static const uchar CDP_SNAP_hdr[8] = { 0xAA, 0xAA, 0x03, 0x00, 0x00, 0x0C, 0x20, 855 0x00 }; 856 857 static ushort CDP_compute_csum(const uchar *buff, ushort len) 858 { 859 ushort csum; 860 int odd; 861 ulong result = 0; 862 ushort leftover; 863 ushort *p; 864 865 if (len > 0) { 866 odd = 1 & (ulong)buff; 867 if (odd) { 868 result = *buff << 8; 869 len--; 870 buff++; 871 } 872 while (len > 1) { 873 p = (ushort *)buff; 874 result += *p++; 875 buff = (uchar *)p; 876 if (result & 0x80000000) 877 result = (result & 0xFFFF) + (result >> 16); 878 len -= 2; 879 } 880 if (len) { 881 leftover = (signed short)(*(const signed char *)buff); 882 /* CISCO SUCKS big time! (and blows too): 883 * CDP uses the IP checksum algorithm with a twist; 884 * for the last byte it *sign* extends and sums. 885 */ 886 result = (result & 0xffff0000) | 887 ((result + leftover) & 0x0000ffff); 888 } 889 while (result >> 16) 890 result = (result & 0xFFFF) + (result >> 16); 891 892 if (odd) 893 result = ((result >> 8) & 0xff) | 894 ((result & 0xff) << 8); 895 } 896 897 /* add up 16-bit and 17-bit words for 17+c bits */ 898 result = (result & 0xffff) + (result >> 16); 899 /* add up 16-bit and 2-bit for 16+c bit */ 900 result = (result & 0xffff) + (result >> 16); 901 /* add up carry.. */ 902 result = (result & 0xffff) + (result >> 16); 903 904 /* negate */ 905 csum = ~(ushort)result; 906 907 /* run time endian detection */ 908 if (csum != htons(csum)) /* little endian */ 909 csum = htons(csum); 910 911 return csum; 912 } 913 914 int CDPSendTrigger(void) 915 { 916 volatile uchar *pkt; 917 volatile ushort *s; 918 volatile ushort *cp; 919 Ethernet_t *et; 920 int len; 921 ushort chksum; 922 #if defined(CONFIG_CDP_DEVICE_ID) || defined(CONFIG_CDP_PORT_ID) || \ 923 defined(CONFIG_CDP_VERSION) || defined(CONFIG_CDP_PLATFORM) 924 char buf[32]; 925 #endif 926 927 pkt = NetTxPacket; 928 et = (Ethernet_t *)pkt; 929 930 /* NOTE: trigger sent not on any VLAN */ 931 932 /* form ethernet header */ 933 memcpy(et->et_dest, NetCDPAddr, 6); 934 memcpy(et->et_src, NetOurEther, 6); 935 936 pkt += ETHER_HDR_SIZE; 937 938 /* SNAP header */ 939 memcpy((uchar *)pkt, CDP_SNAP_hdr, sizeof(CDP_SNAP_hdr)); 940 pkt += sizeof(CDP_SNAP_hdr); 941 942 /* CDP header */ 943 *pkt++ = 0x02; /* CDP version 2 */ 944 *pkt++ = 180; /* TTL */ 945 s = (volatile ushort *)pkt; 946 cp = s; 947 /* checksum (0 for later calculation) */ 948 *s++ = htons(0); 949 950 /* CDP fields */ 951 #ifdef CONFIG_CDP_DEVICE_ID 952 *s++ = htons(CDP_DEVICE_ID_TLV); 953 *s++ = htons(CONFIG_CDP_DEVICE_ID); 954 sprintf(buf, CONFIG_CDP_DEVICE_ID_PREFIX "%pm", NetOurEther); 955 memcpy((uchar *)s, buf, 16); 956 s += 16 / 2; 957 #endif 958 959 #ifdef CONFIG_CDP_PORT_ID 960 *s++ = htons(CDP_PORT_ID_TLV); 961 memset(buf, 0, sizeof(buf)); 962 sprintf(buf, CONFIG_CDP_PORT_ID, eth_get_dev_index()); 963 len = strlen(buf); 964 if (len & 1) /* make it even */ 965 len++; 966 *s++ = htons(len + 4); 967 memcpy((uchar *)s, buf, len); 968 s += len / 2; 969 #endif 970 971 #ifdef CONFIG_CDP_CAPABILITIES 972 *s++ = htons(CDP_CAPABILITIES_TLV); 973 *s++ = htons(8); 974 *(ulong *)s = htonl(CONFIG_CDP_CAPABILITIES); 975 s += 2; 976 #endif 977 978 #ifdef CONFIG_CDP_VERSION 979 *s++ = htons(CDP_VERSION_TLV); 980 memset(buf, 0, sizeof(buf)); 981 strcpy(buf, CONFIG_CDP_VERSION); 982 len = strlen(buf); 983 if (len & 1) /* make it even */ 984 len++; 985 *s++ = htons(len + 4); 986 memcpy((uchar *)s, buf, len); 987 s += len / 2; 988 #endif 989 990 #ifdef CONFIG_CDP_PLATFORM 991 *s++ = htons(CDP_PLATFORM_TLV); 992 memset(buf, 0, sizeof(buf)); 993 strcpy(buf, CONFIG_CDP_PLATFORM); 994 len = strlen(buf); 995 if (len & 1) /* make it even */ 996 len++; 997 *s++ = htons(len + 4); 998 memcpy((uchar *)s, buf, len); 999 s += len / 2; 1000 #endif 1001 1002 #ifdef CONFIG_CDP_TRIGGER 1003 *s++ = htons(CDP_TRIGGER_TLV); 1004 *s++ = htons(8); 1005 *(ulong *)s = htonl(CONFIG_CDP_TRIGGER); 1006 s += 2; 1007 #endif 1008 1009 #ifdef CONFIG_CDP_POWER_CONSUMPTION 1010 *s++ = htons(CDP_POWER_CONSUMPTION_TLV); 1011 *s++ = htons(6); 1012 *s++ = htons(CONFIG_CDP_POWER_CONSUMPTION); 1013 #endif 1014 1015 /* length of ethernet packet */ 1016 len = (uchar *)s - ((uchar *)NetTxPacket + ETHER_HDR_SIZE); 1017 et->et_protlen = htons(len); 1018 1019 len = ETHER_HDR_SIZE + sizeof(CDP_SNAP_hdr); 1020 chksum = CDP_compute_csum((uchar *)NetTxPacket + len, 1021 (uchar *)s - (NetTxPacket + len)); 1022 if (chksum == 0) 1023 chksum = 0xFFFF; 1024 *cp = htons(chksum); 1025 1026 (void) eth_send(NetTxPacket, (uchar *)s - NetTxPacket); 1027 return 0; 1028 } 1029 1030 static void 1031 CDPTimeout (void) 1032 { 1033 CDPSeq++; 1034 1035 if (CDPSeq < 3) { 1036 NetSetTimeout (CDP_TIMEOUT, CDPTimeout); 1037 CDPSendTrigger(); 1038 return; 1039 } 1040 1041 /* if not OK try again */ 1042 if (!CDPOK) 1043 NetStartAgain(); 1044 else 1045 NetState = NETLOOP_SUCCESS; 1046 } 1047 1048 static void 1049 CDPDummyHandler(uchar *pkt, unsigned dest, IPaddr_t sip, unsigned src, 1050 unsigned len) 1051 { 1052 /* nothing */ 1053 } 1054 1055 static void 1056 CDPHandler(const uchar * pkt, unsigned len) 1057 { 1058 const uchar *t; 1059 const ushort *ss; 1060 ushort type, tlen; 1061 uchar applid; 1062 ushort vlan, nvlan; 1063 1064 /* minimum size? */ 1065 if (len < sizeof(CDP_SNAP_hdr) + 4) 1066 goto pkt_short; 1067 1068 /* check for valid CDP SNAP header */ 1069 if (memcmp(pkt, CDP_SNAP_hdr, sizeof(CDP_SNAP_hdr)) != 0) 1070 return; 1071 1072 pkt += sizeof(CDP_SNAP_hdr); 1073 len -= sizeof(CDP_SNAP_hdr); 1074 1075 /* Version of CDP protocol must be >= 2 and TTL != 0 */ 1076 if (pkt[0] < 0x02 || pkt[1] == 0) 1077 return; 1078 1079 /* 1080 * if version is greater than 0x02 maybe we'll have a problem; 1081 * output a warning 1082 */ 1083 if (pkt[0] != 0x02) 1084 printf("** WARNING: CDP packet received with a protocol version %d > 2\n", 1085 pkt[0] & 0xff); 1086 1087 if (CDP_compute_csum(pkt, len) != 0) 1088 return; 1089 1090 pkt += 4; 1091 len -= 4; 1092 1093 vlan = htons(-1); 1094 nvlan = htons(-1); 1095 while (len > 0) { 1096 if (len < 4) 1097 goto pkt_short; 1098 1099 ss = (const ushort *)pkt; 1100 type = ntohs(ss[0]); 1101 tlen = ntohs(ss[1]); 1102 if (tlen > len) { 1103 goto pkt_short; 1104 } 1105 1106 pkt += tlen; 1107 len -= tlen; 1108 1109 ss += 2; /* point ss to the data of the TLV */ 1110 tlen -= 4; 1111 1112 switch (type) { 1113 case CDP_DEVICE_ID_TLV: 1114 break; 1115 case CDP_ADDRESS_TLV: 1116 break; 1117 case CDP_PORT_ID_TLV: 1118 break; 1119 case CDP_CAPABILITIES_TLV: 1120 break; 1121 case CDP_VERSION_TLV: 1122 break; 1123 case CDP_PLATFORM_TLV: 1124 break; 1125 case CDP_NATIVE_VLAN_TLV: 1126 nvlan = *ss; 1127 break; 1128 case CDP_APPLIANCE_VLAN_TLV: 1129 t = (const uchar *)ss; 1130 while (tlen > 0) { 1131 if (tlen < 3) 1132 goto pkt_short; 1133 1134 applid = t[0]; 1135 ss = (const ushort *)(t + 1); 1136 1137 #ifdef CONFIG_CDP_APPLIANCE_VLAN_TYPE 1138 if (applid == 1139 CONFIG_CDP_APPLIANCE_VLAN_TYPE) 1140 vlan = *ss; 1141 #else 1142 /* XXX will this work; dunno */ 1143 vlan = ntohs(*ss); 1144 #endif 1145 t += 3; tlen -= 3; 1146 } 1147 break; 1148 case CDP_TRIGGER_TLV: 1149 break; 1150 case CDP_POWER_CONSUMPTION_TLV: 1151 break; 1152 case CDP_SYSNAME_TLV: 1153 break; 1154 case CDP_SYSOBJECT_TLV: 1155 break; 1156 case CDP_MANAGEMENT_ADDRESS_TLV: 1157 break; 1158 } 1159 } 1160 1161 CDPApplianceVLAN = vlan; 1162 CDPNativeVLAN = nvlan; 1163 1164 CDPOK = 1; 1165 return; 1166 1167 pkt_short: 1168 printf("** CDP packet is too short\n"); 1169 return; 1170 } 1171 1172 static void CDPStart(void) 1173 { 1174 #if defined(CONFIG_NET_MULTI) 1175 printf ("Using %s device\n", eth_get_name()); 1176 #endif 1177 CDPSeq = 0; 1178 CDPOK = 0; 1179 1180 CDPNativeVLAN = htons(-1); 1181 CDPApplianceVLAN = htons(-1); 1182 1183 NetSetTimeout (CDP_TIMEOUT, CDPTimeout); 1184 NetSetHandler (CDPDummyHandler); 1185 1186 CDPSendTrigger(); 1187 } 1188 #endif 1189 1190 #ifdef CONFIG_IP_DEFRAG 1191 /* 1192 * This function collects fragments in a single packet, according 1193 * to the algorithm in RFC815. It returns NULL or the pointer to 1194 * a complete packet, in static storage 1195 */ 1196 #ifndef CONFIG_NET_MAXDEFRAG 1197 #define CONFIG_NET_MAXDEFRAG 16384 1198 #endif 1199 /* 1200 * MAXDEFRAG, above, is chosen in the config file and is real data 1201 * so we need to add the NFS overhead, which is more than TFTP. 1202 * To use sizeof in the internal unnamed structures, we need a real 1203 * instance (can't do "sizeof(struct rpc_t.u.reply))", unfortunately). 1204 * The compiler doesn't complain nor allocates the actual structure 1205 */ 1206 static struct rpc_t rpc_specimen; 1207 #define IP_PKTSIZE (CONFIG_NET_MAXDEFRAG + sizeof(rpc_specimen.u.reply)) 1208 1209 #define IP_MAXUDP (IP_PKTSIZE - IP_HDR_SIZE_NO_UDP) 1210 1211 /* 1212 * this is the packet being assembled, either data or frag control. 1213 * Fragments go by 8 bytes, so this union must be 8 bytes long 1214 */ 1215 struct hole { 1216 /* first_byte is address of this structure */ 1217 u16 last_byte; /* last byte in this hole + 1 (begin of next hole) */ 1218 u16 next_hole; /* index of next (in 8-b blocks), 0 == none */ 1219 u16 prev_hole; /* index of prev, 0 == none */ 1220 u16 unused; 1221 }; 1222 1223 static IP_t *__NetDefragment(IP_t *ip, int *lenp) 1224 { 1225 static uchar pkt_buff[IP_PKTSIZE] __attribute__((aligned(PKTALIGN))); 1226 static u16 first_hole, total_len; 1227 struct hole *payload, *thisfrag, *h, *newh; 1228 IP_t *localip = (IP_t *)pkt_buff; 1229 uchar *indata = (uchar *)ip; 1230 int offset8, start, len, done = 0; 1231 u16 ip_off = ntohs(ip->ip_off); 1232 1233 /* payload starts after IP header, this fragment is in there */ 1234 payload = (struct hole *)(pkt_buff + IP_HDR_SIZE_NO_UDP); 1235 offset8 = (ip_off & IP_OFFS); 1236 thisfrag = payload + offset8; 1237 start = offset8 * 8; 1238 len = ntohs(ip->ip_len) - IP_HDR_SIZE_NO_UDP; 1239 1240 if (start + len > IP_MAXUDP) /* fragment extends too far */ 1241 return NULL; 1242 1243 if (!total_len || localip->ip_id != ip->ip_id) { 1244 /* new (or different) packet, reset structs */ 1245 total_len = 0xffff; 1246 payload[0].last_byte = ~0; 1247 payload[0].next_hole = 0; 1248 payload[0].prev_hole = 0; 1249 first_hole = 0; 1250 /* any IP header will work, copy the first we received */ 1251 memcpy(localip, ip, IP_HDR_SIZE_NO_UDP); 1252 } 1253 1254 /* 1255 * What follows is the reassembly algorithm. We use the payload 1256 * array as a linked list of hole descriptors, as each hole starts 1257 * at a multiple of 8 bytes. However, last byte can be whatever value, 1258 * so it is represented as byte count, not as 8-byte blocks. 1259 */ 1260 1261 h = payload + first_hole; 1262 while (h->last_byte < start) { 1263 if (!h->next_hole) { 1264 /* no hole that far away */ 1265 return NULL; 1266 } 1267 h = payload + h->next_hole; 1268 } 1269 1270 /* last fragment may be 1..7 bytes, the "+7" forces acceptance */ 1271 if (offset8 + ((len + 7) / 8) <= h - payload) { 1272 /* no overlap with holes (dup fragment?) */ 1273 return NULL; 1274 } 1275 1276 if (!(ip_off & IP_FLAGS_MFRAG)) { 1277 /* no more fragmentss: truncate this (last) hole */ 1278 total_len = start + len; 1279 h->last_byte = start + len; 1280 } 1281 1282 /* 1283 * There is some overlap: fix the hole list. This code doesn't 1284 * deal with a fragment that overlaps with two different holes 1285 * (thus being a superset of a previously-received fragment). 1286 */ 1287 1288 if ( (h >= thisfrag) && (h->last_byte <= start + len) ) { 1289 /* complete overlap with hole: remove hole */ 1290 if (!h->prev_hole && !h->next_hole) { 1291 /* last remaining hole */ 1292 done = 1; 1293 } else if (!h->prev_hole) { 1294 /* first hole */ 1295 first_hole = h->next_hole; 1296 payload[h->next_hole].prev_hole = 0; 1297 } else if (!h->next_hole) { 1298 /* last hole */ 1299 payload[h->prev_hole].next_hole = 0; 1300 } else { 1301 /* in the middle of the list */ 1302 payload[h->next_hole].prev_hole = h->prev_hole; 1303 payload[h->prev_hole].next_hole = h->next_hole; 1304 } 1305 1306 } else if (h->last_byte <= start + len) { 1307 /* overlaps with final part of the hole: shorten this hole */ 1308 h->last_byte = start; 1309 1310 } else if (h >= thisfrag) { 1311 /* overlaps with initial part of the hole: move this hole */ 1312 newh = thisfrag + (len / 8); 1313 *newh = *h; 1314 h = newh; 1315 if (h->next_hole) 1316 payload[h->next_hole].prev_hole = (h - payload); 1317 if (h->prev_hole) 1318 payload[h->prev_hole].next_hole = (h - payload); 1319 else 1320 first_hole = (h - payload); 1321 1322 } else { 1323 /* fragment sits in the middle: split the hole */ 1324 newh = thisfrag + (len / 8); 1325 *newh = *h; 1326 h->last_byte = start; 1327 h->next_hole = (newh - payload); 1328 newh->prev_hole = (h - payload); 1329 if (newh->next_hole) 1330 payload[newh->next_hole].prev_hole = (newh - payload); 1331 } 1332 1333 /* finally copy this fragment and possibly return whole packet */ 1334 memcpy((uchar *)thisfrag, indata + IP_HDR_SIZE_NO_UDP, len); 1335 if (!done) 1336 return NULL; 1337 1338 localip->ip_len = htons(total_len); 1339 *lenp = total_len + IP_HDR_SIZE_NO_UDP; 1340 return localip; 1341 } 1342 1343 static inline IP_t *NetDefragment(IP_t *ip, int *lenp) 1344 { 1345 u16 ip_off = ntohs(ip->ip_off); 1346 if (!(ip_off & (IP_OFFS | IP_FLAGS_MFRAG))) 1347 return ip; /* not a fragment */ 1348 return __NetDefragment(ip, lenp); 1349 } 1350 1351 #else /* !CONFIG_IP_DEFRAG */ 1352 1353 static inline IP_t *NetDefragment(IP_t *ip, int *lenp) 1354 { 1355 u16 ip_off = ntohs(ip->ip_off); 1356 if (!(ip_off & (IP_OFFS | IP_FLAGS_MFRAG))) 1357 return ip; /* not a fragment */ 1358 return NULL; 1359 } 1360 #endif 1361 1362 void 1363 NetReceive(volatile uchar * inpkt, int len) 1364 { 1365 Ethernet_t *et; 1366 IP_t *ip; 1367 ARP_t *arp; 1368 IPaddr_t tmp; 1369 IPaddr_t src_ip; 1370 int x; 1371 uchar *pkt; 1372 #if defined(CONFIG_CMD_CDP) 1373 int iscdp; 1374 #endif 1375 ushort cti = 0, vlanid = VLAN_NONE, myvlanid, mynvlanid; 1376 1377 debug("packet received\n"); 1378 1379 NetRxPacket = inpkt; 1380 NetRxPacketLen = len; 1381 et = (Ethernet_t *)inpkt; 1382 1383 /* too small packet? */ 1384 if (len < ETHER_HDR_SIZE) 1385 return; 1386 1387 #ifdef CONFIG_API 1388 if (push_packet) { 1389 (*push_packet)(inpkt, len); 1390 return; 1391 } 1392 #endif 1393 1394 #if defined(CONFIG_CMD_CDP) 1395 /* keep track if packet is CDP */ 1396 iscdp = memcmp(et->et_dest, NetCDPAddr, 6) == 0; 1397 #endif 1398 1399 myvlanid = ntohs(NetOurVLAN); 1400 if (myvlanid == (ushort)-1) 1401 myvlanid = VLAN_NONE; 1402 mynvlanid = ntohs(NetOurNativeVLAN); 1403 if (mynvlanid == (ushort)-1) 1404 mynvlanid = VLAN_NONE; 1405 1406 x = ntohs(et->et_protlen); 1407 1408 debug("packet received\n"); 1409 1410 if (x < 1514) { 1411 /* 1412 * Got a 802 packet. Check the other protocol field. 1413 */ 1414 x = ntohs(et->et_prot); 1415 1416 ip = (IP_t *)(inpkt + E802_HDR_SIZE); 1417 len -= E802_HDR_SIZE; 1418 1419 } else if (x != PROT_VLAN) { /* normal packet */ 1420 ip = (IP_t *)(inpkt + ETHER_HDR_SIZE); 1421 len -= ETHER_HDR_SIZE; 1422 1423 } else { /* VLAN packet */ 1424 VLAN_Ethernet_t *vet = (VLAN_Ethernet_t *)et; 1425 1426 debug("VLAN packet received\n"); 1427 1428 /* too small packet? */ 1429 if (len < VLAN_ETHER_HDR_SIZE) 1430 return; 1431 1432 /* if no VLAN active */ 1433 if ((ntohs(NetOurVLAN) & VLAN_IDMASK) == VLAN_NONE 1434 #if defined(CONFIG_CMD_CDP) 1435 && iscdp == 0 1436 #endif 1437 ) 1438 return; 1439 1440 cti = ntohs(vet->vet_tag); 1441 vlanid = cti & VLAN_IDMASK; 1442 x = ntohs(vet->vet_type); 1443 1444 ip = (IP_t *)(inpkt + VLAN_ETHER_HDR_SIZE); 1445 len -= VLAN_ETHER_HDR_SIZE; 1446 } 1447 1448 debug("Receive from protocol 0x%x\n", x); 1449 1450 #if defined(CONFIG_CMD_CDP) 1451 if (iscdp) { 1452 CDPHandler((uchar *)ip, len); 1453 return; 1454 } 1455 #endif 1456 1457 if ((myvlanid & VLAN_IDMASK) != VLAN_NONE) { 1458 if (vlanid == VLAN_NONE) 1459 vlanid = (mynvlanid & VLAN_IDMASK); 1460 /* not matched? */ 1461 if (vlanid != (myvlanid & VLAN_IDMASK)) 1462 return; 1463 } 1464 1465 switch (x) { 1466 1467 case PROT_ARP: 1468 /* 1469 * We have to deal with two types of ARP packets: 1470 * - REQUEST packets will be answered by sending our 1471 * IP address - if we know it. 1472 * - REPLY packates are expected only after we asked 1473 * for the TFTP server's or the gateway's ethernet 1474 * address; so if we receive such a packet, we set 1475 * the server ethernet address 1476 */ 1477 debug("Got ARP\n"); 1478 1479 arp = (ARP_t *)ip; 1480 if (len < ARP_HDR_SIZE) { 1481 printf("bad length %d < %d\n", len, ARP_HDR_SIZE); 1482 return; 1483 } 1484 if (ntohs(arp->ar_hrd) != ARP_ETHER) { 1485 return; 1486 } 1487 if (ntohs(arp->ar_pro) != PROT_IP) { 1488 return; 1489 } 1490 if (arp->ar_hln != 6) { 1491 return; 1492 } 1493 if (arp->ar_pln != 4) { 1494 return; 1495 } 1496 1497 if (NetOurIP == 0) { 1498 return; 1499 } 1500 1501 if (NetReadIP(&arp->ar_data[16]) != NetOurIP) { 1502 return; 1503 } 1504 1505 switch (ntohs(arp->ar_op)) { 1506 case ARPOP_REQUEST: 1507 /* reply with our IP address */ 1508 debug("Got ARP REQUEST, return our IP\n"); 1509 pkt = (uchar *)et; 1510 pkt += NetSetEther(pkt, et->et_src, PROT_ARP); 1511 arp->ar_op = htons(ARPOP_REPLY); 1512 memcpy (&arp->ar_data[10], &arp->ar_data[0], 6); 1513 NetCopyIP(&arp->ar_data[16], &arp->ar_data[6]); 1514 memcpy (&arp->ar_data[ 0], NetOurEther, 6); 1515 NetCopyIP(&arp->ar_data[ 6], &NetOurIP); 1516 (void) eth_send((uchar *)et, 1517 (pkt - (uchar *)et) + ARP_HDR_SIZE); 1518 return; 1519 1520 case ARPOP_REPLY: /* arp reply */ 1521 /* are we waiting for a reply */ 1522 if (!NetArpWaitPacketIP || !NetArpWaitPacketMAC) 1523 break; 1524 1525 #ifdef CONFIG_KEEP_SERVERADDR 1526 if (NetServerIP == NetArpWaitPacketIP) { 1527 char buf[20]; 1528 sprintf(buf, "%pM", arp->ar_data); 1529 setenv("serveraddr", buf); 1530 } 1531 #endif 1532 1533 debug("Got ARP REPLY, set server/gtwy eth addr (%pM)\n", 1534 arp->ar_data); 1535 1536 tmp = NetReadIP(&arp->ar_data[6]); 1537 1538 /* matched waiting packet's address */ 1539 if (tmp == NetArpWaitReplyIP) { 1540 debug("Got it\n"); 1541 /* save address for later use */ 1542 memcpy(NetArpWaitPacketMAC, 1543 &arp->ar_data[0], 6); 1544 1545 #ifdef CONFIG_NETCONSOLE 1546 (*packetHandler)(0, 0, 0, 0, 0); 1547 #endif 1548 /* modify header, and transmit it */ 1549 memcpy(((Ethernet_t *)NetArpWaitTxPacket)->et_dest, NetArpWaitPacketMAC, 6); 1550 (void) eth_send(NetArpWaitTxPacket, 1551 NetArpWaitTxPacketSize); 1552 1553 /* no arp request pending now */ 1554 NetArpWaitPacketIP = 0; 1555 NetArpWaitTxPacketSize = 0; 1556 NetArpWaitPacketMAC = NULL; 1557 1558 } 1559 return; 1560 default: 1561 debug("Unexpected ARP opcode 0x%x\n", 1562 ntohs(arp->ar_op)); 1563 return; 1564 } 1565 break; 1566 1567 #ifdef CONFIG_CMD_RARP 1568 case PROT_RARP: 1569 debug("Got RARP\n"); 1570 arp = (ARP_t *)ip; 1571 if (len < ARP_HDR_SIZE) { 1572 printf("bad length %d < %d\n", len, ARP_HDR_SIZE); 1573 return; 1574 } 1575 1576 if ((ntohs(arp->ar_op) != RARPOP_REPLY) || 1577 (ntohs(arp->ar_hrd) != ARP_ETHER) || 1578 (ntohs(arp->ar_pro) != PROT_IP) || 1579 (arp->ar_hln != 6) || (arp->ar_pln != 4)) { 1580 1581 puts ("invalid RARP header\n"); 1582 } else { 1583 NetCopyIP(&NetOurIP, &arp->ar_data[16]); 1584 if (NetServerIP == 0) 1585 NetCopyIP(&NetServerIP, &arp->ar_data[ 6]); 1586 memcpy (NetServerEther, &arp->ar_data[ 0], 6); 1587 1588 (*packetHandler)(0, 0, 0, 0, 0); 1589 } 1590 break; 1591 #endif 1592 case PROT_IP: 1593 debug("Got IP\n"); 1594 /* Before we start poking the header, make sure it is there */ 1595 if (len < IP_HDR_SIZE) { 1596 debug("len bad %d < %lu\n", len, (ulong)IP_HDR_SIZE); 1597 return; 1598 } 1599 /* Check the packet length */ 1600 if (len < ntohs(ip->ip_len)) { 1601 printf("len bad %d < %d\n", len, ntohs(ip->ip_len)); 1602 return; 1603 } 1604 len = ntohs(ip->ip_len); 1605 debug("len=%d, v=%02x\n", len, ip->ip_hl_v & 0xff); 1606 1607 /* Can't deal with anything except IPv4 */ 1608 if ((ip->ip_hl_v & 0xf0) != 0x40) { 1609 return; 1610 } 1611 /* Can't deal with IP options (headers != 20 bytes) */ 1612 if ((ip->ip_hl_v & 0x0f) > 0x05) { 1613 return; 1614 } 1615 /* Check the Checksum of the header */ 1616 if (!NetCksumOk((uchar *)ip, IP_HDR_SIZE_NO_UDP / 2)) { 1617 puts ("checksum bad\n"); 1618 return; 1619 } 1620 /* If it is not for us, ignore it */ 1621 tmp = NetReadIP(&ip->ip_dst); 1622 if (NetOurIP && tmp != NetOurIP && tmp != 0xFFFFFFFF) { 1623 #ifdef CONFIG_MCAST_TFTP 1624 if (Mcast_addr != tmp) 1625 #endif 1626 return; 1627 } 1628 /* Read source IP address for later use */ 1629 src_ip = NetReadIP(&ip->ip_src); 1630 /* 1631 * The function returns the unchanged packet if it's not 1632 * a fragment, and either the complete packet or NULL if 1633 * it is a fragment (if !CONFIG_IP_DEFRAG, it returns NULL) 1634 */ 1635 if (!(ip = NetDefragment(ip, &len))) 1636 return; 1637 /* 1638 * watch for ICMP host redirects 1639 * 1640 * There is no real handler code (yet). We just watch 1641 * for ICMP host redirect messages. In case anybody 1642 * sees these messages: please contact me 1643 * (wd@denx.de), or - even better - send me the 1644 * necessary fixes :-) 1645 * 1646 * Note: in all cases where I have seen this so far 1647 * it was a problem with the router configuration, 1648 * for instance when a router was configured in the 1649 * BOOTP reply, but the TFTP server was on the same 1650 * subnet. So this is probably a warning that your 1651 * configuration might be wrong. But I'm not really 1652 * sure if there aren't any other situations. 1653 */ 1654 if (ip->ip_p == IPPROTO_ICMP) { 1655 ICMP_t *icmph = (ICMP_t *)&(ip->udp_src); 1656 1657 switch (icmph->type) { 1658 case ICMP_REDIRECT: 1659 if (icmph->code != ICMP_REDIR_HOST) 1660 return; 1661 printf (" ICMP Host Redirect to %pI4 ", 1662 &icmph->un.gateway); 1663 return; 1664 #if defined(CONFIG_CMD_PING) 1665 case ICMP_ECHO_REPLY: 1666 /* 1667 * IP header OK. Pass the packet to the 1668 * current handler. 1669 */ 1670 /* XXX point to ip packet */ 1671 (*packetHandler)((uchar *)ip, 0, src_ip, 0, 0); 1672 return; 1673 case ICMP_ECHO_REQUEST: 1674 debug("Got ICMP ECHO REQUEST, return %d bytes\n", 1675 ETHER_HDR_SIZE + len); 1676 1677 memcpy (&et->et_dest[0], &et->et_src[0], 6); 1678 memcpy (&et->et_src[ 0], NetOurEther, 6); 1679 1680 ip->ip_sum = 0; 1681 ip->ip_off = 0; 1682 NetCopyIP((void*)&ip->ip_dst, &ip->ip_src); 1683 NetCopyIP((void*)&ip->ip_src, &NetOurIP); 1684 ip->ip_sum = ~NetCksum((uchar *)ip, 1685 IP_HDR_SIZE_NO_UDP >> 1); 1686 1687 icmph->type = ICMP_ECHO_REPLY; 1688 icmph->checksum = 0; 1689 icmph->checksum = ~NetCksum((uchar *)icmph, 1690 (len - IP_HDR_SIZE_NO_UDP) >> 1); 1691 (void) eth_send((uchar *)et, 1692 ETHER_HDR_SIZE + len); 1693 return; 1694 #endif 1695 default: 1696 return; 1697 } 1698 } else if (ip->ip_p != IPPROTO_UDP) { /* Only UDP packets */ 1699 return; 1700 } 1701 1702 #ifdef CONFIG_UDP_CHECKSUM 1703 if (ip->udp_xsum != 0) { 1704 ulong xsum; 1705 ushort *sumptr; 1706 ushort sumlen; 1707 1708 xsum = ip->ip_p; 1709 xsum += (ntohs(ip->udp_len)); 1710 xsum += (ntohl(ip->ip_src) >> 16) & 0x0000ffff; 1711 xsum += (ntohl(ip->ip_src) >> 0) & 0x0000ffff; 1712 xsum += (ntohl(ip->ip_dst) >> 16) & 0x0000ffff; 1713 xsum += (ntohl(ip->ip_dst) >> 0) & 0x0000ffff; 1714 1715 sumlen = ntohs(ip->udp_len); 1716 sumptr = (ushort *) &(ip->udp_src); 1717 1718 while (sumlen > 1) { 1719 ushort sumdata; 1720 1721 sumdata = *sumptr++; 1722 xsum += ntohs(sumdata); 1723 sumlen -= 2; 1724 } 1725 if (sumlen > 0) { 1726 ushort sumdata; 1727 1728 sumdata = *(unsigned char *) sumptr; 1729 sumdata = (sumdata << 8) & 0xff00; 1730 xsum += sumdata; 1731 } 1732 while ((xsum >> 16) != 0) { 1733 xsum = (xsum & 0x0000ffff) + 1734 ((xsum >> 16) & 0x0000ffff); 1735 } 1736 if ((xsum != 0x00000000) && (xsum != 0x0000ffff)) { 1737 printf(" UDP wrong checksum %08lx %08x\n", 1738 xsum, ntohs(ip->udp_xsum)); 1739 return; 1740 } 1741 } 1742 #endif 1743 1744 1745 #ifdef CONFIG_NETCONSOLE 1746 nc_input_packet((uchar *)ip +IP_HDR_SIZE, 1747 ntohs(ip->udp_dst), 1748 ntohs(ip->udp_src), 1749 ntohs(ip->udp_len) - 8); 1750 #endif 1751 /* 1752 * IP header OK. Pass the packet to the current handler. 1753 */ 1754 (*packetHandler)((uchar *)ip +IP_HDR_SIZE, 1755 ntohs(ip->udp_dst), 1756 src_ip, 1757 ntohs(ip->udp_src), 1758 ntohs(ip->udp_len) - 8); 1759 break; 1760 } 1761 } 1762 1763 1764 /**********************************************************************/ 1765 1766 static int net_check_prereq (proto_t protocol) 1767 { 1768 switch (protocol) { 1769 /* Fall through */ 1770 #if defined(CONFIG_CMD_PING) 1771 case PING: 1772 if (NetPingIP == 0) { 1773 puts ("*** ERROR: ping address not given\n"); 1774 return (1); 1775 } 1776 goto common; 1777 #endif 1778 #if defined(CONFIG_CMD_SNTP) 1779 case SNTP: 1780 if (NetNtpServerIP == 0) { 1781 puts ("*** ERROR: NTP server address not given\n"); 1782 return (1); 1783 } 1784 goto common; 1785 #endif 1786 #if defined(CONFIG_CMD_DNS) 1787 case DNS: 1788 if (NetOurDNSIP == 0) { 1789 puts("*** ERROR: DNS server address not given\n"); 1790 return 1; 1791 } 1792 goto common; 1793 #endif 1794 #if defined(CONFIG_CMD_NFS) 1795 case NFS: 1796 #endif 1797 case TFTP: 1798 if (NetServerIP == 0) { 1799 puts ("*** ERROR: `serverip' not set\n"); 1800 return (1); 1801 } 1802 #if defined(CONFIG_CMD_PING) || defined(CONFIG_CMD_SNTP) || \ 1803 defined(CONFIG_CMD_DNS) 1804 common: 1805 #endif 1806 /* Fall through */ 1807 1808 case NETCONS: 1809 if (NetOurIP == 0) { 1810 puts ("*** ERROR: `ipaddr' not set\n"); 1811 return (1); 1812 } 1813 /* Fall through */ 1814 1815 #ifdef CONFIG_CMD_RARP 1816 case RARP: 1817 #endif 1818 case BOOTP: 1819 case CDP: 1820 case DHCP: 1821 if (memcmp (NetOurEther, "\0\0\0\0\0\0", 6) == 0) { 1822 #ifdef CONFIG_NET_MULTI 1823 extern int eth_get_dev_index (void); 1824 int num = eth_get_dev_index (); 1825 1826 switch (num) { 1827 case -1: 1828 puts ("*** ERROR: No ethernet found.\n"); 1829 return (1); 1830 case 0: 1831 puts ("*** ERROR: `ethaddr' not set\n"); 1832 break; 1833 default: 1834 printf ("*** ERROR: `eth%daddr' not set\n", 1835 num); 1836 break; 1837 } 1838 1839 NetStartAgain (); 1840 return (2); 1841 #else 1842 puts ("*** ERROR: `ethaddr' not set\n"); 1843 return (1); 1844 #endif 1845 } 1846 /* Fall through */ 1847 default: 1848 return (0); 1849 } 1850 return (0); /* OK */ 1851 } 1852 /**********************************************************************/ 1853 1854 int 1855 NetCksumOk(uchar * ptr, int len) 1856 { 1857 return !((NetCksum(ptr, len) + 1) & 0xfffe); 1858 } 1859 1860 1861 unsigned 1862 NetCksum(uchar * ptr, int len) 1863 { 1864 ulong xsum; 1865 ushort *p = (ushort *)ptr; 1866 1867 xsum = 0; 1868 while (len-- > 0) 1869 xsum += *p++; 1870 xsum = (xsum & 0xffff) + (xsum >> 16); 1871 xsum = (xsum & 0xffff) + (xsum >> 16); 1872 return (xsum & 0xffff); 1873 } 1874 1875 int 1876 NetEthHdrSize(void) 1877 { 1878 ushort myvlanid; 1879 1880 myvlanid = ntohs(NetOurVLAN); 1881 if (myvlanid == (ushort)-1) 1882 myvlanid = VLAN_NONE; 1883 1884 return ((myvlanid & VLAN_IDMASK) == VLAN_NONE) ? ETHER_HDR_SIZE : 1885 VLAN_ETHER_HDR_SIZE; 1886 } 1887 1888 int 1889 NetSetEther(volatile uchar * xet, uchar * addr, uint prot) 1890 { 1891 Ethernet_t *et = (Ethernet_t *)xet; 1892 ushort myvlanid; 1893 1894 myvlanid = ntohs(NetOurVLAN); 1895 if (myvlanid == (ushort)-1) 1896 myvlanid = VLAN_NONE; 1897 1898 memcpy (et->et_dest, addr, 6); 1899 memcpy (et->et_src, NetOurEther, 6); 1900 if ((myvlanid & VLAN_IDMASK) == VLAN_NONE) { 1901 et->et_protlen = htons(prot); 1902 return ETHER_HDR_SIZE; 1903 } else { 1904 VLAN_Ethernet_t *vet = (VLAN_Ethernet_t *)xet; 1905 1906 vet->vet_vlan_type = htons(PROT_VLAN); 1907 vet->vet_tag = htons((0 << 5) | (myvlanid & VLAN_IDMASK)); 1908 vet->vet_type = htons(prot); 1909 return VLAN_ETHER_HDR_SIZE; 1910 } 1911 } 1912 1913 void 1914 NetSetIP(volatile uchar * xip, IPaddr_t dest, int dport, int sport, int len) 1915 { 1916 IP_t *ip = (IP_t *)xip; 1917 1918 /* 1919 * If the data is an odd number of bytes, zero the 1920 * byte after the last byte so that the checksum 1921 * will work. 1922 */ 1923 if (len & 1) 1924 xip[IP_HDR_SIZE + len] = 0; 1925 1926 /* 1927 * Construct an IP and UDP header. 1928 * (need to set no fragment bit - XXX) 1929 */ 1930 /* IP_HDR_SIZE / 4 (not including UDP) */ 1931 ip->ip_hl_v = 0x45; 1932 ip->ip_tos = 0; 1933 ip->ip_len = htons(IP_HDR_SIZE + len); 1934 ip->ip_id = htons(NetIPID++); 1935 ip->ip_off = htons(IP_FLAGS_DFRAG); /* Don't fragment */ 1936 ip->ip_ttl = 255; 1937 ip->ip_p = 17; /* UDP */ 1938 ip->ip_sum = 0; 1939 /* already in network byte order */ 1940 NetCopyIP((void*)&ip->ip_src, &NetOurIP); 1941 /* - "" - */ 1942 NetCopyIP((void*)&ip->ip_dst, &dest); 1943 ip->udp_src = htons(sport); 1944 ip->udp_dst = htons(dport); 1945 ip->udp_len = htons(8 + len); 1946 ip->udp_xsum = 0; 1947 ip->ip_sum = ~NetCksum((uchar *)ip, IP_HDR_SIZE_NO_UDP / 2); 1948 } 1949 1950 void copy_filename (char *dst, const char *src, int size) 1951 { 1952 if (*src && (*src == '"')) { 1953 ++src; 1954 --size; 1955 } 1956 1957 while ((--size > 0) && *src && (*src != '"')) { 1958 *dst++ = *src++; 1959 } 1960 *dst = '\0'; 1961 } 1962 1963 #if defined(CONFIG_CMD_NFS) || \ 1964 defined(CONFIG_CMD_SNTP) || \ 1965 defined(CONFIG_CMD_DNS) 1966 /* 1967 * make port a little random (1024-17407) 1968 * This keeps the math somewhat trivial to compute, and seems to work with 1969 * all supported protocols/clients/servers 1970 */ 1971 unsigned int random_port(void) 1972 { 1973 return 1024 + (get_timer(0) % 0x4000); 1974 } 1975 #endif 1976 1977 void ip_to_string (IPaddr_t x, char *s) 1978 { 1979 x = ntohl (x); 1980 sprintf (s, "%d.%d.%d.%d", 1981 (int) ((x >> 24) & 0xff), 1982 (int) ((x >> 16) & 0xff), 1983 (int) ((x >> 8) & 0xff), (int) ((x >> 0) & 0xff) 1984 ); 1985 } 1986 1987 void VLAN_to_string(ushort x, char *s) 1988 { 1989 x = ntohs(x); 1990 1991 if (x == (ushort)-1) 1992 x = VLAN_NONE; 1993 1994 if (x == VLAN_NONE) 1995 strcpy(s, "none"); 1996 else 1997 sprintf(s, "%d", x & VLAN_IDMASK); 1998 } 1999 2000 ushort string_to_VLAN(const char *s) 2001 { 2002 ushort id; 2003 2004 if (s == NULL) 2005 return htons(VLAN_NONE); 2006 2007 if (*s < '0' || *s > '9') 2008 id = VLAN_NONE; 2009 else 2010 id = (ushort)simple_strtoul(s, NULL, 10); 2011 2012 return htons(id); 2013 } 2014 2015 ushort getenv_VLAN(char *var) 2016 { 2017 return (string_to_VLAN(getenv(var))); 2018 } 2019