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