1 /* $NetBSD: if_arp.c,v 1.66 1999/09/25 17:49:29 is Exp $ */ 2 3 /*- 4 * Copyright (c) 1998 The NetBSD Foundation, Inc. 5 * All rights reserved. 6 * 7 * This code is derived from software contributed to The NetBSD Foundation 8 * by Public Access Networks Corporation ("Panix"). It was developed under 9 * contract to Panix by Eric Haszlakiewicz and Thor Lancelot Simon. 10 * 11 * Redistribution and use in source and binary forms, with or without 12 * modification, are permitted provided that the following conditions 13 * are met: 14 * 1. Redistributions of source code must retain the above copyright 15 * notice, this list of conditions and the following disclaimer. 16 * 2. Redistributions in binary form must reproduce the above copyright 17 * notice, this list of conditions and the following disclaimer in the 18 * documentation and/or other materials provided with the distribution. 19 * 3. All advertising materials mentioning features or use of this software 20 * must display the following acknowledgement: 21 * This product includes software developed by the NetBSD 22 * Foundation, Inc. and its contributors. 23 * 4. Neither the name of The NetBSD Foundation nor the names of its 24 * contributors may be used to endorse or promote products derived 25 * from this software without specific prior written permission. 26 * 27 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 28 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 29 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 30 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 31 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 32 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 33 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 34 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 35 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 36 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 37 * POSSIBILITY OF SUCH DAMAGE. 38 */ 39 40 /* 41 * Copyright (c) 1982, 1986, 1988, 1993 42 * The Regents of the University of California. All rights reserved. 43 * 44 * Redistribution and use in source and binary forms, with or without 45 * modification, are permitted provided that the following conditions 46 * are met: 47 * 1. Redistributions of source code must retain the above copyright 48 * notice, this list of conditions and the following disclaimer. 49 * 2. Redistributions in binary form must reproduce the above copyright 50 * notice, this list of conditions and the following disclaimer in the 51 * documentation and/or other materials provided with the distribution. 52 * 3. All advertising materials mentioning features or use of this software 53 * must display the following acknowledgement: 54 * This product includes software developed by the University of 55 * California, Berkeley and its contributors. 56 * 4. Neither the name of the University nor the names of its contributors 57 * may be used to endorse or promote products derived from this software 58 * without specific prior written permission. 59 * 60 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 61 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 62 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 63 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 64 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 65 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 66 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 67 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 68 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 69 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 70 * SUCH DAMAGE. 71 * 72 * @(#)if_ether.c 8.2 (Berkeley) 9/26/94 73 */ 74 75 /* 76 * Ethernet address resolution protocol. 77 * TODO: 78 * add "inuse/lock" bit (or ref. count) along with valid bit 79 */ 80 81 #include "opt_ddb.h" 82 #include "opt_inet.h" 83 84 #ifdef INET 85 86 #include <sys/param.h> 87 #include <sys/systm.h> 88 #include <sys/malloc.h> 89 #include <sys/mbuf.h> 90 #include <sys/socket.h> 91 #include <sys/time.h> 92 #include <sys/kernel.h> 93 #include <sys/errno.h> 94 #include <sys/ioctl.h> 95 #include <sys/syslog.h> 96 #include <sys/proc.h> 97 #include <sys/protosw.h> 98 #include <sys/domain.h> 99 100 #include <net/ethertypes.h> 101 #include <net/if.h> 102 #include <net/if_dl.h> 103 #include <net/if_token.h> 104 #include <net/if_types.h> 105 #include <net/route.h> 106 107 108 #include <netinet/in.h> 109 #include <netinet/in_systm.h> 110 #include <netinet/in_var.h> 111 #include <netinet/ip.h> 112 #include <netinet/if_inarp.h> 113 114 #include "loop.h" 115 #include "arc.h" 116 #if NARC > 0 117 #include <net/if_arc.h> 118 #endif 119 #include "fddi.h" 120 #if NFDDI > 0 121 #include <net/if_fddi.h> 122 #endif 123 #include "token.h" 124 #include "token.h" 125 126 #define SIN(s) ((struct sockaddr_in *)s) 127 #define SDL(s) ((struct sockaddr_dl *)s) 128 #define SRP(s) ((struct sockaddr_inarp *)s) 129 130 /* 131 * ARP trailer negotiation. Trailer protocol is not IP specific, 132 * but ARP request/response use IP addresses. 133 */ 134 #define ETHERTYPE_IPTRAILERS ETHERTYPE_TRAIL 135 136 /* timer values */ 137 int arpt_prune = (5*60*1); /* walk list every 5 minutes */ 138 int arpt_keep = (20*60); /* once resolved, good for 20 more minutes */ 139 int arpt_down = 20; /* once declared down, don't send for 20 secs */ 140 #define rt_expire rt_rmx.rmx_expire 141 142 static void arprequest __P((struct ifnet *, 143 struct in_addr *, struct in_addr *, u_int8_t *)); 144 static void arptfree __P((struct llinfo_arp *)); 145 static void arptimer __P((void *)); 146 static struct llinfo_arp *arplookup __P((struct in_addr *, int, int)); 147 static void in_arpinput __P((struct mbuf *)); 148 149 #if NLOOP > 0 150 extern struct ifnet loif[NLOOP]; 151 #endif 152 LIST_HEAD(, llinfo_arp) llinfo_arp; 153 struct ifqueue arpintrq = {0, 0, 0, 50}; 154 int arp_inuse, arp_allocated, arp_intimer; 155 int arp_maxtries = 5; 156 int useloopback = 1; /* use loopback interface for local traffic */ 157 int arpinit_done = 0; 158 159 /* revarp state */ 160 static struct in_addr myip, srv_ip; 161 static int myip_initialized = 0; 162 static int revarp_in_progress = 0; 163 static struct ifnet *myip_ifp = NULL; 164 165 #ifdef DDB 166 static void db_print_sa __P((struct sockaddr *)); 167 static void db_print_ifa __P((struct ifaddr *)); 168 static void db_print_llinfo __P((caddr_t)); 169 static int db_show_radix_node __P((struct radix_node *, void *)); 170 #endif 171 172 /* 173 * this should be elsewhere. 174 */ 175 176 static char * 177 lla_snprintf __P((u_int8_t *, int)); 178 179 static char * 180 lla_snprintf(adrp, len) 181 u_int8_t *adrp; 182 int len; 183 { 184 static char buf[16*3]; 185 static const char hexdigits[] = { 186 '0','1','2','3','4','5','6','7', 187 '8','9','a','b','c','d','e','f' 188 }; 189 190 int i; 191 char *p; 192 193 p = buf; 194 195 *p++ = hexdigits[(*adrp)>>4]; 196 *p++ = hexdigits[(*adrp++)&0xf]; 197 198 for (i=1; i<len && i<16; i++) { 199 *p++ = ':'; 200 *p++ = hexdigits[(*adrp)>>4]; 201 *p++ = hexdigits[(*adrp++)&0xf]; 202 } 203 204 *p = 0; 205 return buf; 206 } 207 208 struct protosw arpsw[] = { 209 { 0, 0, 0, 0, 210 0, 0, 0, 0, 211 0, 212 0, 0, 0, arp_drain, 213 } 214 }; 215 216 217 struct domain arpdomain = 218 { PF_ARP, "arp", 0, 0, 0, 219 arpsw, &arpsw[sizeof(arpsw)/sizeof(arpsw[0])] 220 }; 221 222 /* 223 * ARP table locking. 224 * 225 * to prevent lossage vs. the arp_drain routine (which may be called at 226 * any time, including in a device driver context), we do two things: 227 * 228 * 1) manipulation of la->la_hold is done at splimp() (for all of 229 * about two instructions). 230 * 231 * 2) manipulation of the arp table's linked list is done under the 232 * protection of the ARP_LOCK; if arp_drain() or arptimer is called 233 * while the arp table is locked, we punt and try again later. 234 */ 235 236 int arp_locked; 237 238 static __inline int arp_lock_try __P((int)); 239 static __inline void arp_unlock __P((void)); 240 241 static __inline int 242 arp_lock_try(int recurse) 243 { 244 int s; 245 246 s = splimp(); 247 if (!recurse && arp_locked) { 248 splx(s); 249 return (0); 250 } 251 arp_locked++; 252 splx(s); 253 return (1); 254 } 255 256 static __inline void 257 arp_unlock() 258 { 259 int s; 260 261 s = splimp(); 262 arp_locked--; 263 splx(s); 264 } 265 266 #ifdef DIAGNOSTIC 267 #define ARP_LOCK(recurse) \ 268 do { \ 269 if (arp_lock_try(recurse) == 0) { \ 270 printf("%s:%d: arp already locked\n", __FILE__, __LINE__); \ 271 panic("arp_lock"); \ 272 } \ 273 } while (0) 274 #define ARP_LOCK_CHECK() \ 275 do { \ 276 if (arp_locked == 0) { \ 277 printf("%s:%d: arp lock not held\n", __FILE__, __LINE__); \ 278 panic("arp lock check"); \ 279 } \ 280 } while (0) 281 #else 282 #define ARP_LOCK(x) (void) arp_lock_try(x) 283 #define ARP_LOCK_CHECK() /* nothing */ 284 #endif 285 286 #define ARP_UNLOCK() arp_unlock() 287 288 /* 289 * ARP protocol drain routine. Called when memory is in short supply. 290 * Called at splimp(); 291 */ 292 293 void 294 arp_drain() 295 { 296 register struct llinfo_arp *la, *nla; 297 int count = 0; 298 struct mbuf *mold; 299 300 if (arp_lock_try(0) == 0) { 301 printf("arp_drain: locked; punting\n"); 302 return; 303 } 304 305 for (la = llinfo_arp.lh_first; la != 0; la = nla) { 306 nla = la->la_list.le_next; 307 308 mold = la->la_hold; 309 la->la_hold = 0; 310 311 if (mold) { 312 m_freem(mold); 313 count++; 314 } 315 } 316 ARP_UNLOCK(); 317 } 318 319 320 /* 321 * Timeout routine. Age arp_tab entries periodically. 322 */ 323 /* ARGSUSED */ 324 static void 325 arptimer(arg) 326 void *arg; 327 { 328 int s; 329 register struct llinfo_arp *la, *nla; 330 331 s = splsoftnet(); 332 333 if (arp_lock_try(0) == 0) { 334 /* get it later.. */ 335 splx(s); 336 return; 337 } 338 339 timeout(arptimer, NULL, arpt_prune * hz); 340 for (la = llinfo_arp.lh_first; la != 0; la = nla) { 341 register struct rtentry *rt = la->la_rt; 342 343 nla = la->la_list.le_next; 344 if (rt->rt_expire && rt->rt_expire <= time.tv_sec) 345 arptfree(la); /* timer has expired; clear */ 346 } 347 348 ARP_UNLOCK(); 349 350 splx(s); 351 } 352 353 /* 354 * Parallel to llc_rtrequest. 355 */ 356 void 357 arp_rtrequest(req, rt, sa) 358 int req; 359 register struct rtentry *rt; 360 struct sockaddr *sa; 361 { 362 register struct sockaddr *gate = rt->rt_gateway; 363 register struct llinfo_arp *la = (struct llinfo_arp *)rt->rt_llinfo; 364 static struct sockaddr_dl null_sdl = {sizeof(null_sdl), AF_LINK}; 365 size_t allocsize; 366 struct mbuf *mold; 367 int s; 368 369 if (!arpinit_done) { 370 arpinit_done = 1; 371 /* 372 * We generate expiration times from time.tv_sec 373 * so avoid accidently creating permanent routes. 374 */ 375 if (time.tv_sec == 0) { 376 time.tv_sec++; 377 } 378 timeout(arptimer, (caddr_t)0, hz); 379 } 380 if (rt->rt_flags & RTF_GATEWAY) 381 return; 382 383 ARP_LOCK(1); /* we may already be locked here. */ 384 385 switch (req) { 386 387 case RTM_ADD: 388 /* 389 * XXX: If this is a manually added route to interface 390 * such as older version of routed or gated might provide, 391 * restore cloning bit. 392 */ 393 if ((rt->rt_flags & RTF_HOST) == 0 && 394 SIN(rt_mask(rt))->sin_addr.s_addr != 0xffffffff) 395 rt->rt_flags |= RTF_CLONING; 396 if (rt->rt_flags & RTF_CLONING) { 397 /* 398 * Case 1: This route should come from a route to iface. 399 */ 400 rt_setgate(rt, rt_key(rt), 401 (struct sockaddr *)&null_sdl); 402 gate = rt->rt_gateway; 403 SDL(gate)->sdl_type = rt->rt_ifp->if_type; 404 SDL(gate)->sdl_index = rt->rt_ifp->if_index; 405 /* 406 * Give this route an expiration time, even though 407 * it's a "permanent" route, so that routes cloned 408 * from it do not need their expiration time set. 409 */ 410 rt->rt_expire = time.tv_sec; 411 #if NFDDI > 0 412 if (rt->rt_ifp->if_type == IFT_FDDI 413 && (rt->rt_rmx.rmx_mtu > FDDIIPMTU 414 || (rt->rt_rmx.rmx_mtu == 0 415 && rt->rt_ifp->if_mtu > FDDIIPMTU))) { 416 rt->rt_rmx.rmx_mtu = FDDIIPMTU; 417 } 418 #endif 419 #if NARC > 0 420 if (rt->rt_ifp->if_type == IFT_ARCNET) { 421 int arcipifmtu; 422 423 if (rt->rt_ifp->if_flags & IFF_LINK0) 424 arcipifmtu = arc_ipmtu; 425 else 426 arcipifmtu = ARCMTU; 427 428 if (rt->rt_rmx.rmx_mtu > arcipifmtu || 429 (rt->rt_rmx.rmx_mtu == 0 && 430 rt->rt_ifp->if_mtu > arcipifmtu)) 431 432 rt->rt_rmx.rmx_mtu = arcipifmtu; 433 } 434 #endif 435 break; 436 } 437 /* Announce a new entry if requested. */ 438 if (rt->rt_flags & RTF_ANNOUNCE) 439 arprequest(rt->rt_ifp, 440 &SIN(rt_key(rt))->sin_addr, 441 &SIN(rt_key(rt))->sin_addr, 442 (u_char *)LLADDR(SDL(gate))); 443 /*FALLTHROUGH*/ 444 case RTM_RESOLVE: 445 if (gate->sa_family != AF_LINK || 446 gate->sa_len < sizeof(null_sdl)) { 447 log(LOG_DEBUG, "arp_rtrequest: bad gateway value\n"); 448 break; 449 } 450 SDL(gate)->sdl_type = rt->rt_ifp->if_type; 451 SDL(gate)->sdl_index = rt->rt_ifp->if_index; 452 if (la != 0) 453 break; /* This happens on a route change */ 454 /* 455 * Case 2: This route may come from cloning, or a manual route 456 * add with a LL address. 457 */ 458 switch (SDL(gate)->sdl_type) { 459 #if NTOKEN > 0 460 case IFT_ISO88025: 461 allocsize = sizeof(*la) + sizeof(struct token_rif); 462 break; 463 #endif /* NTOKEN > 0 */ 464 default: 465 allocsize = sizeof(*la); 466 } 467 R_Malloc(la, struct llinfo_arp *, allocsize); 468 rt->rt_llinfo = (caddr_t)la; 469 if (la == 0) { 470 log(LOG_DEBUG, "arp_rtrequest: malloc failed\n"); 471 break; 472 } 473 arp_inuse++, arp_allocated++; 474 Bzero(la, allocsize); 475 la->la_rt = rt; 476 rt->rt_flags |= RTF_LLINFO; 477 LIST_INSERT_HEAD(&llinfo_arp, la, la_list); 478 if (in_hosteq(SIN(rt_key(rt))->sin_addr, 479 (IA_SIN(rt->rt_ifa))->sin_addr)) { 480 /* 481 * This test used to be 482 * if (loif.if_flags & IFF_UP) 483 * It allowed local traffic to be forced through 484 * the hardware by configuring the loopback down. 485 * However, it causes problems during network 486 * configuration for boards that can't receive 487 * packets they send. It is now necessary to clear 488 * "useloopback" and remove the route to force 489 * traffic out to the hardware. 490 */ 491 rt->rt_expire = 0; 492 Bcopy(LLADDR(rt->rt_ifp->if_sadl), 493 LLADDR(SDL(gate)), 494 SDL(gate)->sdl_alen = 495 rt->rt_ifp->if_data.ifi_addrlen); 496 #if NLOOP > 0 497 if (useloopback) 498 rt->rt_ifp = &loif[0]; 499 #endif 500 } 501 break; 502 503 case RTM_DELETE: 504 if (la == 0) 505 break; 506 arp_inuse--; 507 LIST_REMOVE(la, la_list); 508 rt->rt_llinfo = 0; 509 rt->rt_flags &= ~RTF_LLINFO; 510 511 s = splimp(); 512 mold = la->la_hold; 513 la->la_hold = 0; 514 splx(s); 515 516 if (mold) 517 m_freem(mold); 518 519 Free((caddr_t)la); 520 } 521 ARP_UNLOCK(); 522 } 523 524 /* 525 * Broadcast an ARP request. Caller specifies: 526 * - arp header source ip address 527 * - arp header target ip address 528 * - arp header source ethernet address 529 */ 530 static void 531 arprequest(ifp, sip, tip, enaddr) 532 register struct ifnet *ifp; 533 register struct in_addr *sip, *tip; 534 register u_int8_t *enaddr; 535 { 536 register struct mbuf *m; 537 struct arphdr *ah; 538 struct sockaddr sa; 539 540 if ((m = m_gethdr(M_DONTWAIT, MT_DATA)) == NULL) 541 return; 542 m->m_len = sizeof(*ah) + 2*sizeof(struct in_addr) + 543 2*ifp->if_data.ifi_addrlen; 544 m->m_pkthdr.len = m->m_len; 545 MH_ALIGN(m, m->m_len); 546 ah = mtod(m, struct arphdr *); 547 bzero((caddr_t)ah, m->m_len); 548 ah->ar_pro = htons(ETHERTYPE_IP); 549 ah->ar_hln = ifp->if_data.ifi_addrlen; /* hardware address length */ 550 ah->ar_pln = sizeof(struct in_addr); /* protocol address length */ 551 ah->ar_op = htons(ARPOP_REQUEST); 552 bcopy((caddr_t)enaddr, (caddr_t)ar_sha(ah), ah->ar_hln); 553 bcopy((caddr_t)sip, (caddr_t)ar_spa(ah), ah->ar_pln); 554 bcopy((caddr_t)tip, (caddr_t)ar_tpa(ah), ah->ar_pln); 555 sa.sa_family = AF_ARP; 556 sa.sa_len = 2; 557 m->m_flags |= M_BCAST; 558 (*ifp->if_output)(ifp, m, &sa, (struct rtentry *)0); 559 } 560 561 /* 562 * Resolve an IP address into an ethernet address. If success, 563 * desten is filled in. If there is no entry in arptab, 564 * set one up and broadcast a request for the IP address. 565 * Hold onto this mbuf and resend it once the address 566 * is finally resolved. A return value of 1 indicates 567 * that desten has been filled in and the packet should be sent 568 * normally; a 0 return indicates that the packet has been 569 * taken over here, either now or for later transmission. 570 */ 571 int 572 arpresolve(ifp, rt, m, dst, desten) 573 register struct ifnet *ifp; 574 register struct rtentry *rt; 575 struct mbuf *m; 576 register struct sockaddr *dst; 577 register u_char *desten; 578 { 579 register struct llinfo_arp *la; 580 struct sockaddr_dl *sdl; 581 struct mbuf *mold; 582 int s; 583 584 if (rt) 585 la = (struct llinfo_arp *)rt->rt_llinfo; 586 else { 587 if ((la = arplookup(&SIN(dst)->sin_addr, 1, 0)) != NULL) 588 rt = la->la_rt; 589 } 590 if (la == 0 || rt == 0) { 591 log(LOG_DEBUG, "arpresolve: can't allocate llinfo\n"); 592 m_freem(m); 593 return (0); 594 } 595 sdl = SDL(rt->rt_gateway); 596 /* 597 * Check the address family and length is valid, the address 598 * is resolved; otherwise, try to resolve. 599 */ 600 if ((rt->rt_expire == 0 || rt->rt_expire > time.tv_sec) && 601 sdl->sdl_family == AF_LINK && sdl->sdl_alen != 0) { 602 bcopy(LLADDR(sdl), desten, 603 min(sdl->sdl_alen, ifp->if_data.ifi_addrlen)); 604 return 1; 605 } 606 /* 607 * There is an arptab entry, but no ethernet address 608 * response yet. Replace the held mbuf with this 609 * latest one. 610 */ 611 612 s = splimp(); 613 mold = la->la_hold; 614 la->la_hold = m; 615 splx(s); 616 617 if (mold) 618 m_freem(mold); 619 620 621 /* 622 * Re-send the ARP request when appropriate. 623 */ 624 #ifdef DIAGNOSTIC 625 if (rt->rt_expire == 0) { 626 /* This should never happen. (Should it? -gwr) */ 627 printf("arpresolve: unresolved and rt_expire == 0\n"); 628 /* Set expiration time to now (expired). */ 629 rt->rt_expire = time.tv_sec; 630 } 631 #endif 632 if (rt->rt_expire) { 633 rt->rt_flags &= ~RTF_REJECT; 634 if (la->la_asked == 0 || rt->rt_expire != time.tv_sec) { 635 rt->rt_expire = time.tv_sec; 636 if (la->la_asked++ < arp_maxtries) 637 arprequest(ifp, 638 &SIN(rt->rt_ifa->ifa_addr)->sin_addr, 639 &SIN(dst)->sin_addr, 640 LLADDR(ifp->if_sadl)); 641 else { 642 rt->rt_flags |= RTF_REJECT; 643 rt->rt_expire += arpt_down; 644 la->la_asked = 0; 645 } 646 } 647 } 648 return (0); 649 } 650 651 /* 652 * Common length and type checks are done here, 653 * then the protocol-specific routine is called. 654 */ 655 void 656 arpintr() 657 { 658 register struct mbuf *m; 659 register struct arphdr *ar; 660 int s; 661 662 while (arpintrq.ifq_head) { 663 s = splimp(); 664 IF_DEQUEUE(&arpintrq, m); 665 splx(s); 666 if (m == 0 || (m->m_flags & M_PKTHDR) == 0) 667 panic("arpintr"); 668 669 if (m->m_len >= sizeof(struct arphdr) && 670 (ar = mtod(m, struct arphdr *)) && 671 /* XXX ntohs(ar->ar_hrd) == ARPHRD_ETHER && */ 672 m->m_len >= 673 sizeof(struct arphdr) + 2 * (ar->ar_hln + ar->ar_pln)) 674 switch (ntohs(ar->ar_pro)) { 675 676 case ETHERTYPE_IP: 677 case ETHERTYPE_IPTRAILERS: 678 in_arpinput(m); 679 continue; 680 } 681 m_freem(m); 682 } 683 } 684 685 /* 686 * ARP for Internet protocols on 10 Mb/s Ethernet. 687 * Algorithm is that given in RFC 826. 688 * In addition, a sanity check is performed on the sender 689 * protocol address, to catch impersonators. 690 * We no longer handle negotiations for use of trailer protocol: 691 * Formerly, ARP replied for protocol type ETHERTYPE_TRAIL sent 692 * along with IP replies if we wanted trailers sent to us, 693 * and also sent them in response to IP replies. 694 * This allowed either end to announce the desire to receive 695 * trailer packets. 696 * We no longer reply to requests for ETHERTYPE_TRAIL protocol either, 697 * but formerly didn't normally send requests. 698 */ 699 static void 700 in_arpinput(m) 701 struct mbuf *m; 702 { 703 struct arphdr *ah; 704 register struct ifnet *ifp = m->m_pkthdr.rcvif; 705 register struct llinfo_arp *la = 0; 706 register struct rtentry *rt; 707 struct in_ifaddr *ia; 708 struct sockaddr_dl *sdl; 709 struct sockaddr sa; 710 struct in_addr isaddr, itaddr, myaddr; 711 int op; 712 struct mbuf *mold; 713 int s; 714 715 716 ah = mtod(m, struct arphdr *); 717 op = ntohs(ah->ar_op); 718 bcopy((caddr_t)ar_spa(ah), (caddr_t)&isaddr, sizeof (isaddr)); 719 bcopy((caddr_t)ar_tpa(ah), (caddr_t)&itaddr, sizeof (itaddr)); 720 721 /* 722 * If the target IP address is zero, ignore the packet. 723 * This prevents the code below from tring to answer 724 * when we are using IP address zero (booting). 725 */ 726 if (in_nullhost(itaddr)) 727 goto out; 728 729 /* 730 * If the source IP address is zero, this is most likely a 731 * confused host trying to use IP address zero. (Windoze?) 732 * XXX: Should we bother trying to reply to these? 733 */ 734 if (in_nullhost(isaddr)) 735 goto out; 736 737 /* 738 * Search for a matching interface address 739 * or any address on the interface to use 740 * as a dummy address in the rest of this function 741 */ 742 INADDR_TO_IA(itaddr, ia); 743 while ((ia != NULL) && ia->ia_ifp != m->m_pkthdr.rcvif) 744 NEXT_IA_WITH_SAME_ADDR(ia); 745 746 if (ia == NULL) { 747 INADDR_TO_IA(isaddr, ia); 748 while ((ia != NULL) && ia->ia_ifp != m->m_pkthdr.rcvif) 749 NEXT_IA_WITH_SAME_ADDR(ia); 750 751 if (ia == NULL) { 752 IFP_TO_IA(ifp, ia); 753 if (ia == NULL) 754 goto out; 755 } 756 } 757 758 myaddr = ia->ia_addr.sin_addr; 759 760 if (!bcmp((caddr_t)ar_sha(ah), LLADDR(ifp->if_sadl), 761 ifp->if_data.ifi_addrlen)) 762 goto out; /* it's from me, ignore it. */ 763 764 if (!bcmp((caddr_t)ar_sha(ah), (caddr_t)ifp->if_broadcastaddr, 765 ifp->if_data.ifi_addrlen)) { 766 log(LOG_ERR, 767 "%s: arp: link address is broadcast for IP address %s!\n", 768 ifp->if_xname, in_fmtaddr(isaddr)); 769 goto out; 770 } 771 772 if (in_hosteq(isaddr, myaddr)) { 773 log(LOG_ERR, 774 "duplicate IP address %s sent from link address %s\n", 775 in_fmtaddr(isaddr), lla_snprintf(ar_sha(ah), ah->ar_hln)); 776 itaddr = myaddr; 777 goto reply; 778 } 779 la = arplookup(&isaddr, in_hosteq(itaddr, myaddr), 0); 780 if (la && (rt = la->la_rt) && (sdl = SDL(rt->rt_gateway))) { 781 if (sdl->sdl_alen && 782 bcmp((caddr_t)ar_sha(ah), LLADDR(sdl), sdl->sdl_alen)) { 783 if (rt->rt_flags & RTF_STATIC) { 784 log(LOG_INFO, 785 "%s tried to overwrite permanent arp info" 786 " for %s\n", 787 lla_snprintf(ar_sha(ah), ah->ar_hln), 788 in_fmtaddr(isaddr)); 789 goto out; 790 } else if (rt->rt_ifp != ifp) { 791 log(LOG_INFO, 792 "%s on %s tried to overwrite " 793 "arp info for %s on %s\n", 794 lla_snprintf(ar_sha(ah), ah->ar_hln), 795 ifp->if_xname, in_fmtaddr(isaddr), 796 rt->rt_ifp->if_xname); 797 goto out; 798 } else { 799 log(LOG_INFO, 800 "arp info overwritten for %s by %s\n", 801 in_fmtaddr(isaddr), 802 lla_snprintf(ar_sha(ah), ah->ar_hln)); 803 } 804 } 805 /* 806 * sanity check for the address length. 807 * XXX this does not work for protocols with variable address 808 * length. -is 809 */ 810 if (sdl->sdl_alen && 811 sdl->sdl_alen != ah->ar_hln) { 812 log(LOG_WARNING, 813 "arp from %s: new addr len %d, was %d", 814 in_fmtaddr(isaddr), ah->ar_hln, sdl->sdl_alen); 815 } 816 if (ifp->if_data.ifi_addrlen != ah->ar_hln) { 817 log(LOG_WARNING, 818 "arp from %s: addr len: new %d, i/f %d (ignored)", 819 in_fmtaddr(isaddr), ah->ar_hln, 820 ifp->if_data.ifi_addrlen); 821 goto reply; 822 } 823 #if NTOKEN > 0 824 /* 825 * XXX uses m_data and assumes the complete answer including 826 * XXX token-ring headers is in the same buf 827 */ 828 if (ifp->if_type == IFT_ISO88025) { 829 struct token_header *trh; 830 831 trh = (struct token_header *)M_TRHSTART(m); 832 if (trh->token_shost[0] & TOKEN_RI_PRESENT) { 833 struct token_rif *rif; 834 size_t riflen; 835 836 rif = TOKEN_RIF(trh); 837 riflen = (ntohs(rif->tr_rcf) & 838 TOKEN_RCF_LEN_MASK) >> 8; 839 840 if (riflen > 2 && 841 riflen < sizeof(struct token_rif) && 842 (riflen & 1) == 0) { 843 rif->tr_rcf ^= htons(TOKEN_RCF_DIRECTION); 844 rif->tr_rcf &= htons(~TOKEN_RCF_BROADCAST_MASK); 845 bcopy(rif, TOKEN_RIF(la), riflen); 846 } 847 } 848 } 849 #endif /* NTOKEN > 0 */ 850 bcopy((caddr_t)ar_sha(ah), LLADDR(sdl), 851 sdl->sdl_alen = ah->ar_hln); 852 if (rt->rt_expire) 853 rt->rt_expire = time.tv_sec + arpt_keep; 854 rt->rt_flags &= ~RTF_REJECT; 855 la->la_asked = 0; 856 857 s = splimp(); 858 mold = la->la_hold; 859 la->la_hold = 0; 860 splx(s); 861 862 if (mold) 863 (*ifp->if_output)(ifp, mold, rt_key(rt), rt); 864 } 865 reply: 866 if (op != ARPOP_REQUEST) { 867 out: 868 m_freem(m); 869 return; 870 } 871 if (in_hosteq(itaddr, myaddr)) { 872 /* I am the target */ 873 bcopy((caddr_t)ar_sha(ah), (caddr_t)ar_tha(ah), ah->ar_hln); 874 bcopy(LLADDR(ifp->if_sadl), (caddr_t)ar_sha(ah), ah->ar_hln); 875 } else { 876 la = arplookup(&itaddr, 0, SIN_PROXY); 877 if (la == 0) 878 goto out; 879 rt = la->la_rt; 880 bcopy((caddr_t)ar_sha(ah), (caddr_t)ar_tha(ah), ah->ar_hln); 881 sdl = SDL(rt->rt_gateway); 882 bcopy(LLADDR(sdl), (caddr_t)ar_sha(ah), ah->ar_hln); 883 } 884 885 bcopy((caddr_t)ar_spa(ah), (caddr_t)ar_tpa(ah), ah->ar_pln); 886 bcopy((caddr_t)&itaddr, (caddr_t)ar_spa(ah), ah->ar_pln); 887 ah->ar_op = htons(ARPOP_REPLY); 888 ah->ar_pro = htons(ETHERTYPE_IP); /* let's be sure! */ 889 m->m_flags &= ~(M_BCAST|M_MCAST); /* never reply by broadcast */ 890 m->m_len = sizeof(*ah) + (2 * ah->ar_pln) + (2 * ah->ar_hln); 891 m->m_pkthdr.len = m->m_len; 892 sa.sa_family = AF_ARP; 893 sa.sa_len = 2; 894 (*ifp->if_output)(ifp, m, &sa, (struct rtentry *)0); 895 return; 896 } 897 898 /* 899 * Free an arp entry. 900 */ 901 static void 902 arptfree(la) 903 register struct llinfo_arp *la; 904 { 905 register struct rtentry *rt = la->la_rt; 906 register struct sockaddr_dl *sdl; 907 908 ARP_LOCK_CHECK(); 909 910 if (rt == 0) 911 panic("arptfree"); 912 if (rt->rt_refcnt > 0 && (sdl = SDL(rt->rt_gateway)) && 913 sdl->sdl_family == AF_LINK) { 914 sdl->sdl_alen = 0; 915 la->la_asked = 0; 916 rt->rt_flags &= ~RTF_REJECT; 917 return; 918 } 919 rtrequest(RTM_DELETE, rt_key(rt), (struct sockaddr *)0, rt_mask(rt), 920 0, (struct rtentry **)0); 921 } 922 923 /* 924 * Lookup or enter a new address in arptab. 925 */ 926 static struct llinfo_arp * 927 arplookup(addr, create, proxy) 928 struct in_addr *addr; 929 int create, proxy; 930 { 931 register struct rtentry *rt; 932 static struct sockaddr_inarp sin; 933 const char *why = 0; 934 935 sin.sin_len = sizeof(sin); 936 sin.sin_family = AF_INET; 937 sin.sin_addr = *addr; 938 sin.sin_other = proxy ? SIN_PROXY : 0; 939 rt = rtalloc1(sintosa(&sin), create); 940 if (rt == 0) 941 return (0); 942 rt->rt_refcnt--; 943 944 if (rt->rt_flags & RTF_GATEWAY) 945 why = "host is not on local network"; 946 else if ((rt->rt_flags & RTF_LLINFO) == 0) 947 why = "could not allocate llinfo"; 948 else if (rt->rt_gateway->sa_family != AF_LINK) 949 why = "gateway route is not ours"; 950 else 951 return ((struct llinfo_arp *)rt->rt_llinfo); 952 953 if (create) 954 log(LOG_DEBUG, "arplookup: unable to enter address" 955 " for %s (%s)\n", 956 in_fmtaddr(*addr), why); 957 return (0); 958 } 959 960 int 961 arpioctl(cmd, data) 962 u_long cmd; 963 caddr_t data; 964 { 965 966 return (EOPNOTSUPP); 967 } 968 969 void 970 arp_ifinit(ifp, ifa) 971 struct ifnet *ifp; 972 struct ifaddr *ifa; 973 { 974 struct in_addr *ip; 975 976 /* 977 * Warn the user if another station has this IP address, 978 * but only if the interface IP address is not zero. 979 */ 980 ip = &IA_SIN(ifa)->sin_addr; 981 if (!in_nullhost(*ip)) 982 arprequest(ifp, ip, ip, LLADDR(ifp->if_sadl)); 983 984 ifa->ifa_rtrequest = arp_rtrequest; 985 ifa->ifa_flags |= RTF_CLONING; 986 } 987 988 /* 989 * Called from 10 Mb/s Ethernet interrupt handlers 990 * when ether packet type ETHERTYPE_REVARP 991 * is received. Common length and type checks are done here, 992 * then the protocol-specific routine is called. 993 */ 994 void 995 revarpinput(m) 996 struct mbuf *m; 997 { 998 struct arphdr *ar; 999 1000 if (m->m_len < sizeof(struct arphdr)) 1001 goto out; 1002 ar = mtod(m, struct arphdr *); 1003 #if 0 /* XXX I don't think we need this... and it will prevent other LL */ 1004 if (ntohs(ar->ar_hrd) != ARPHRD_ETHER) 1005 goto out; 1006 #endif 1007 if (m->m_len < sizeof(struct arphdr) + 2 * (ar->ar_hln + ar->ar_pln)) 1008 goto out; 1009 switch (ntohs(ar->ar_pro)) { 1010 1011 case ETHERTYPE_IP: 1012 case ETHERTYPE_IPTRAILERS: 1013 in_revarpinput(m); 1014 return; 1015 1016 default: 1017 break; 1018 } 1019 out: 1020 m_freem(m); 1021 } 1022 1023 /* 1024 * RARP for Internet protocols on 10 Mb/s Ethernet. 1025 * Algorithm is that given in RFC 903. 1026 * We are only using for bootstrap purposes to get an ip address for one of 1027 * our interfaces. Thus we support no user-interface. 1028 * 1029 * Since the contents of the RARP reply are specific to the interface that 1030 * sent the request, this code must ensure that they are properly associated. 1031 * 1032 * Note: also supports ARP via RARP packets, per the RFC. 1033 */ 1034 void 1035 in_revarpinput(m) 1036 struct mbuf *m; 1037 { 1038 struct ifnet *ifp; 1039 struct arphdr *ah; 1040 int op; 1041 1042 ah = mtod(m, struct arphdr *); 1043 op = ntohs(ah->ar_op); 1044 switch (op) { 1045 case ARPOP_REQUEST: 1046 case ARPOP_REPLY: /* per RFC */ 1047 in_arpinput(m); 1048 return; 1049 case ARPOP_REVREPLY: 1050 break; 1051 case ARPOP_REVREQUEST: /* handled by rarpd(8) */ 1052 default: 1053 goto out; 1054 } 1055 if (!revarp_in_progress) 1056 goto out; 1057 ifp = m->m_pkthdr.rcvif; 1058 if (ifp != myip_ifp) /* !same interface */ 1059 goto out; 1060 if (myip_initialized) 1061 goto wake; 1062 if (bcmp(ar_tha(ah), LLADDR(ifp->if_sadl), ifp->if_sadl->sdl_alen)) 1063 goto out; 1064 bcopy((caddr_t)ar_spa(ah), (caddr_t)&srv_ip, sizeof(srv_ip)); 1065 bcopy((caddr_t)ar_tpa(ah), (caddr_t)&myip, sizeof(myip)); 1066 myip_initialized = 1; 1067 wake: /* Do wakeup every time in case it was missed. */ 1068 wakeup((caddr_t)&myip); 1069 1070 out: 1071 m_freem(m); 1072 } 1073 1074 /* 1075 * Send a RARP request for the ip address of the specified interface. 1076 * The request should be RFC 903-compliant. 1077 */ 1078 void 1079 revarprequest(ifp) 1080 struct ifnet *ifp; 1081 { 1082 struct sockaddr sa; 1083 struct mbuf *m; 1084 struct arphdr *ah; 1085 1086 if ((m = m_gethdr(M_DONTWAIT, MT_DATA)) == NULL) 1087 return; 1088 m->m_len = sizeof(*ah) + 2*sizeof(struct in_addr) + 1089 2*ifp->if_data.ifi_addrlen; 1090 m->m_pkthdr.len = m->m_len; 1091 MH_ALIGN(m, m->m_len); 1092 ah = mtod(m, struct arphdr *); 1093 bzero((caddr_t)ah, m->m_len); 1094 ah->ar_pro = htons(ETHERTYPE_IP); 1095 ah->ar_hln = ifp->if_data.ifi_addrlen; /* hardware address length */ 1096 ah->ar_pln = sizeof(struct in_addr); /* protocol address length */ 1097 ah->ar_op = htons(ARPOP_REVREQUEST); 1098 1099 bcopy(LLADDR(ifp->if_sadl), (caddr_t)ar_sha(ah), ah->ar_hln); 1100 bcopy(LLADDR(ifp->if_sadl), (caddr_t)ar_tha(ah), ah->ar_hln); 1101 1102 sa.sa_family = AF_ARP; 1103 sa.sa_len = 2; 1104 m->m_flags |= M_BCAST; 1105 (*ifp->if_output)(ifp, m, &sa, (struct rtentry *)0); 1106 1107 } 1108 1109 /* 1110 * RARP for the ip address of the specified interface, but also 1111 * save the ip address of the server that sent the answer. 1112 * Timeout if no response is received. 1113 */ 1114 int 1115 revarpwhoarewe(ifp, serv_in, clnt_in) 1116 struct ifnet *ifp; 1117 struct in_addr *serv_in; 1118 struct in_addr *clnt_in; 1119 { 1120 int result, count = 20; 1121 1122 myip_initialized = 0; 1123 myip_ifp = ifp; 1124 1125 revarp_in_progress = 1; 1126 while (count--) { 1127 revarprequest(ifp); 1128 result = tsleep((caddr_t)&myip, PSOCK, "revarp", hz/2); 1129 if (result != EWOULDBLOCK) 1130 break; 1131 } 1132 revarp_in_progress = 0; 1133 1134 if (!myip_initialized) 1135 return ENETUNREACH; 1136 1137 bcopy((caddr_t)&srv_ip, serv_in, sizeof(*serv_in)); 1138 bcopy((caddr_t)&myip, clnt_in, sizeof(*clnt_in)); 1139 return 0; 1140 } 1141 1142 1143 1144 #ifdef DDB 1145 1146 #include <machine/db_machdep.h> 1147 #include <ddb/db_interface.h> 1148 #include <ddb/db_output.h> 1149 static void 1150 db_print_sa(sa) 1151 struct sockaddr *sa; 1152 { 1153 int len; 1154 u_char *p; 1155 1156 if (sa == 0) { 1157 db_printf("[NULL]"); 1158 return; 1159 } 1160 1161 p = (u_char*)sa; 1162 len = sa->sa_len; 1163 db_printf("["); 1164 while (len > 0) { 1165 db_printf("%d", *p); 1166 p++; len--; 1167 if (len) db_printf(","); 1168 } 1169 db_printf("]\n"); 1170 } 1171 static void 1172 db_print_ifa(ifa) 1173 struct ifaddr *ifa; 1174 { 1175 if (ifa == 0) 1176 return; 1177 db_printf(" ifa_addr="); 1178 db_print_sa(ifa->ifa_addr); 1179 db_printf(" ifa_dsta="); 1180 db_print_sa(ifa->ifa_dstaddr); 1181 db_printf(" ifa_mask="); 1182 db_print_sa(ifa->ifa_netmask); 1183 db_printf(" flags=0x%x,refcnt=%d,metric=%d\n", 1184 ifa->ifa_flags, 1185 ifa->ifa_refcnt, 1186 ifa->ifa_metric); 1187 } 1188 static void 1189 db_print_llinfo(li) 1190 caddr_t li; 1191 { 1192 struct llinfo_arp *la; 1193 1194 if (li == 0) 1195 return; 1196 la = (struct llinfo_arp *)li; 1197 db_printf(" la_rt=%p la_hold=%p, la_asked=0x%lx\n", 1198 la->la_rt, la->la_hold, la->la_asked); 1199 } 1200 /* 1201 * Function to pass to rn_walktree(). 1202 * Return non-zero error to abort walk. 1203 */ 1204 static int 1205 db_show_radix_node(rn, w) 1206 struct radix_node *rn; 1207 void *w; 1208 { 1209 struct rtentry *rt = (struct rtentry *)rn; 1210 1211 db_printf("rtentry=%p", rt); 1212 1213 db_printf(" flags=0x%x refcnt=%d use=%ld expire=%ld\n", 1214 rt->rt_flags, rt->rt_refcnt, 1215 rt->rt_use, rt->rt_expire); 1216 1217 db_printf(" key="); db_print_sa(rt_key(rt)); 1218 db_printf(" mask="); db_print_sa(rt_mask(rt)); 1219 db_printf(" gw="); db_print_sa(rt->rt_gateway); 1220 1221 db_printf(" ifp=%p ", rt->rt_ifp); 1222 if (rt->rt_ifp) 1223 db_printf("(%s)", rt->rt_ifp->if_xname); 1224 else 1225 db_printf("(NULL)"); 1226 1227 db_printf(" ifa=%p\n", rt->rt_ifa); 1228 db_print_ifa(rt->rt_ifa); 1229 1230 db_printf(" genmask="); db_print_sa(rt->rt_genmask); 1231 1232 db_printf(" gwroute=%p llinfo=%p\n", 1233 rt->rt_gwroute, rt->rt_llinfo); 1234 db_print_llinfo(rt->rt_llinfo); 1235 1236 return (0); 1237 } 1238 /* 1239 * Function to print all the route trees. 1240 * Use this from ddb: "call db_show_arptab" 1241 */ 1242 int 1243 db_show_arptab() 1244 { 1245 struct radix_node_head *rnh; 1246 rnh = rt_tables[AF_INET]; 1247 db_printf("Route tree for AF_INET\n"); 1248 if (rnh == NULL) { 1249 db_printf(" (not initialized)\n"); 1250 return (0); 1251 } 1252 rn_walktree(rnh, db_show_radix_node, NULL); 1253 return (0); 1254 } 1255 #endif 1256 #endif /* INET */ 1257 1258