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