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