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