1 /* $NetBSD: if_arp.c,v 1.282 2019/04/29 16:12:30 roy 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.282 2019/04/29 16:12:30 roy Exp $"); 72 73 #ifdef _KERNEL_OPT 74 #include "opt_ddb.h" 75 #include "opt_inet.h" 76 #include "opt_net_mpsafe.h" 77 #endif 78 79 #ifdef INET 80 81 #include "arp.h" 82 #include "bridge.h" 83 84 #include <sys/param.h> 85 #include <sys/systm.h> 86 #include <sys/callout.h> 87 #include <sys/kmem.h> 88 #include <sys/mbuf.h> 89 #include <sys/socket.h> 90 #include <sys/time.h> 91 #include <sys/timetc.h> 92 #include <sys/kernel.h> 93 #include <sys/errno.h> 94 #include <sys/ioctl.h> 95 #include <sys/syslog.h> 96 #include <sys/proc.h> 97 #include <sys/protosw.h> 98 #include <sys/domain.h> 99 #include <sys/sysctl.h> 100 #include <sys/socketvar.h> 101 #include <sys/percpu.h> 102 #include <sys/cprng.h> 103 #include <sys/kmem.h> 104 105 #include <net/ethertypes.h> 106 #include <net/if.h> 107 #include <net/if_dl.h> 108 #include <net/if_token.h> 109 #include <net/if_types.h> 110 #include <net/if_ether.h> 111 #include <net/if_llatbl.h> 112 #include <net/route.h> 113 #include <net/net_stats.h> 114 115 #include <netinet/in.h> 116 #include <netinet/in_systm.h> 117 #include <netinet/in_var.h> 118 #include <netinet/ip.h> 119 #include <netinet/if_inarp.h> 120 121 #include "arcnet.h" 122 #if NARCNET > 0 123 #include <net/if_arc.h> 124 #endif 125 #include "fddi.h" 126 #if NFDDI > 0 127 #include <net/if_fddi.h> 128 #endif 129 #include "token.h" 130 #include "carp.h" 131 #if NCARP > 0 132 #include <netinet/ip_carp.h> 133 #endif 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 static int arpt_keep = (20*60); /* once resolved, good for 20 more minutes */ 143 static int arpt_down = 20; /* once declared down, don't send for 20 secs */ 144 static int arp_maxhold = 1; /* number of packets to hold per ARP entry */ 145 #define rt_expire rt_rmx.rmx_expire 146 #define rt_pksent rt_rmx.rmx_pksent 147 148 int ip_dad_count = PROBE_NUM; 149 #ifdef ARP_DEBUG 150 int arp_debug = 1; 151 #else 152 int arp_debug = 0; 153 #endif 154 155 static void arp_init(void); 156 157 static void arprequest(struct ifnet *, 158 const struct in_addr *, const struct in_addr *, 159 const uint8_t *); 160 static void arpannounce1(struct ifaddr *); 161 static struct sockaddr *arp_setgate(struct rtentry *, struct sockaddr *, 162 const struct sockaddr *); 163 static void arptimer(void *); 164 static void arp_settimer(struct llentry *, int); 165 static struct llentry *arplookup(struct ifnet *, 166 const struct in_addr *, const struct sockaddr *, int); 167 static struct llentry *arpcreate(struct ifnet *, 168 const struct in_addr *, const struct sockaddr *, int); 169 static void in_arpinput(struct mbuf *); 170 static void in_revarpinput(struct mbuf *); 171 static void revarprequest(struct ifnet *); 172 173 static void arp_drainstub(void); 174 175 struct dadq; 176 static void arp_dad_timer(struct dadq *); 177 static void arp_dad_start(struct ifaddr *); 178 static void arp_dad_stop(struct ifaddr *); 179 static void arp_dad_duplicated(struct ifaddr *, const struct sockaddr_dl *); 180 181 static void arp_init_llentry(struct ifnet *, struct llentry *); 182 #if NTOKEN > 0 183 static void arp_free_llentry_tokenring(struct llentry *); 184 #endif 185 186 struct ifqueue arpintrq = { 187 .ifq_head = NULL, 188 .ifq_tail = NULL, 189 .ifq_len = 0, 190 .ifq_maxlen = 50, 191 .ifq_drops = 0, 192 }; 193 static int arp_maxtries = 5; 194 static int useloopback = 1; /* use loopback interface for local traffic */ 195 196 static percpu_t *arpstat_percpu; 197 198 #define ARP_STAT_GETREF() _NET_STAT_GETREF(arpstat_percpu) 199 #define ARP_STAT_PUTREF() _NET_STAT_PUTREF(arpstat_percpu) 200 201 #define ARP_STATINC(x) _NET_STATINC(arpstat_percpu, x) 202 #define ARP_STATADD(x, v) _NET_STATADD(arpstat_percpu, x, v) 203 204 /* revarp state */ 205 static struct in_addr myip, srv_ip; 206 static int myip_initialized = 0; 207 static int revarp_in_progress = 0; 208 static struct ifnet *myip_ifp = NULL; 209 210 static int arp_drainwanted; 211 212 static int log_movements = 1; 213 static int log_permanent_modify = 1; 214 static int log_wrong_iface = 1; 215 static int log_unknown_network = 1; 216 217 DOMAIN_DEFINE(arpdomain); /* forward declare and add to link set */ 218 219 static void 220 arp_fasttimo(void) 221 { 222 if (arp_drainwanted) { 223 arp_drain(); 224 arp_drainwanted = 0; 225 } 226 } 227 228 static const struct protosw arpsw[] = { 229 { 230 .pr_type = 0, 231 .pr_domain = &arpdomain, 232 .pr_protocol = 0, 233 .pr_flags = 0, 234 .pr_input = 0, 235 .pr_ctlinput = 0, 236 .pr_ctloutput = 0, 237 .pr_usrreqs = 0, 238 .pr_init = arp_init, 239 .pr_fasttimo = arp_fasttimo, 240 .pr_slowtimo = 0, 241 .pr_drain = arp_drainstub, 242 } 243 }; 244 245 struct domain arpdomain = { 246 .dom_family = PF_ARP, 247 .dom_name = "arp", 248 .dom_protosw = arpsw, 249 .dom_protoswNPROTOSW = &arpsw[__arraycount(arpsw)], 250 }; 251 252 static void sysctl_net_inet_arp_setup(struct sysctllog **); 253 254 void 255 arp_init(void) 256 { 257 258 sysctl_net_inet_arp_setup(NULL); 259 arpstat_percpu = percpu_alloc(sizeof(uint64_t) * ARP_NSTATS); 260 IFQ_LOCK_INIT(&arpintrq); 261 } 262 263 static void 264 arp_drainstub(void) 265 { 266 arp_drainwanted = 1; 267 } 268 269 /* 270 * ARP protocol drain routine. Called when memory is in short supply. 271 * Called at splvm(); don't acquire softnet_lock as can be called from 272 * hardware interrupt handlers. 273 */ 274 void 275 arp_drain(void) 276 { 277 278 lltable_drain(AF_INET); 279 } 280 281 static void 282 arptimer(void *arg) 283 { 284 struct llentry *lle = arg; 285 struct ifnet *ifp; 286 287 KASSERT((lle->la_flags & LLE_STATIC) == 0); 288 289 LLE_WLOCK(lle); 290 291 /* 292 * This shortcut is required to avoid trying to touch ifp that may be 293 * being destroyed. 294 */ 295 if ((lle->la_flags & LLE_LINKED) == 0) { 296 LLE_FREE_LOCKED(lle); 297 return; 298 } 299 300 ifp = lle->lle_tbl->llt_ifp; 301 302 /* XXX: LOR avoidance. We still have ref on lle. */ 303 LLE_WUNLOCK(lle); 304 305 IF_AFDATA_LOCK(ifp); 306 LLE_WLOCK(lle); 307 308 /* Guard against race with other llentry_free(). */ 309 if (lle->la_flags & LLE_LINKED) { 310 size_t pkts_dropped; 311 312 LLE_REMREF(lle); 313 pkts_dropped = llentry_free(lle); 314 ARP_STATADD(ARP_STAT_DFRDROPPED, pkts_dropped); 315 ARP_STATADD(ARP_STAT_DFRTOTAL, pkts_dropped); 316 } else { 317 LLE_FREE_LOCKED(lle); 318 } 319 320 IF_AFDATA_UNLOCK(ifp); 321 } 322 323 static void 324 arp_settimer(struct llentry *la, int sec) 325 { 326 327 LLE_WLOCK_ASSERT(la); 328 KASSERT((la->la_flags & LLE_STATIC) == 0); 329 330 /* 331 * We have to take care of a reference leak which occurs if 332 * callout_reset overwrites a pending callout schedule. Unfortunately 333 * we don't have a mean to know the overwrite, so we need to know it 334 * using callout_stop. We need to call callout_pending first to exclude 335 * the case that the callout has never been scheduled. 336 */ 337 if (callout_pending(&la->la_timer)) { 338 bool expired = callout_stop(&la->la_timer); 339 if (!expired) 340 /* A pending callout schedule is canceled. */ 341 LLE_REMREF(la); 342 } 343 LLE_ADDREF(la); 344 callout_reset(&la->la_timer, hz * sec, arptimer, la); 345 } 346 347 /* 348 * We set the gateway for RTF_CLONING routes to a "prototype" 349 * link-layer sockaddr whose interface type (if_type) and interface 350 * index (if_index) fields are prepared. 351 */ 352 static struct sockaddr * 353 arp_setgate(struct rtentry *rt, struct sockaddr *gate, 354 const struct sockaddr *netmask) 355 { 356 const struct ifnet *ifp = rt->rt_ifp; 357 uint8_t namelen = strlen(ifp->if_xname); 358 uint8_t addrlen = ifp->if_addrlen; 359 360 /* 361 * XXX: If this is a manually added route to interface 362 * such as older version of routed or gated might provide, 363 * restore cloning bit. 364 */ 365 if ((rt->rt_flags & RTF_HOST) == 0 && netmask != NULL && 366 satocsin(netmask)->sin_addr.s_addr != 0xffffffff) 367 rt->rt_flags |= RTF_CONNECTED; 368 369 if ((rt->rt_flags & (RTF_CONNECTED | RTF_LOCAL))) { 370 union { 371 struct sockaddr sa; 372 struct sockaddr_storage ss; 373 struct sockaddr_dl sdl; 374 } u; 375 /* 376 * Case 1: This route should come from a route to iface. 377 */ 378 sockaddr_dl_init(&u.sdl, sizeof(u.ss), 379 ifp->if_index, ifp->if_type, NULL, namelen, NULL, addrlen); 380 rt_setgate(rt, &u.sa); 381 gate = rt->rt_gateway; 382 } 383 return gate; 384 } 385 386 static void 387 arp_init_llentry(struct ifnet *ifp, struct llentry *lle) 388 { 389 390 switch (ifp->if_type) { 391 #if NTOKEN > 0 392 case IFT_ISO88025: 393 lle->la_opaque = kmem_intr_alloc(sizeof(struct token_rif), 394 KM_NOSLEEP); 395 lle->lle_ll_free = arp_free_llentry_tokenring; 396 break; 397 #endif 398 } 399 } 400 401 #if NTOKEN > 0 402 static void 403 arp_free_llentry_tokenring(struct llentry *lle) 404 { 405 406 kmem_intr_free(lle->la_opaque, sizeof(struct token_rif)); 407 } 408 #endif 409 410 /* 411 * Parallel to llc_rtrequest. 412 */ 413 void 414 arp_rtrequest(int req, struct rtentry *rt, const struct rt_addrinfo *info) 415 { 416 struct sockaddr *gate = rt->rt_gateway; 417 struct in_ifaddr *ia; 418 struct ifaddr *ifa; 419 struct ifnet *ifp = rt->rt_ifp; 420 int bound; 421 int s; 422 423 if (req == RTM_LLINFO_UPD) { 424 if ((ifa = info->rti_ifa) != NULL) 425 arpannounce1(ifa); 426 return; 427 } 428 429 if ((rt->rt_flags & RTF_GATEWAY) != 0) { 430 if (req != RTM_ADD) 431 return; 432 433 /* 434 * linklayers with particular link MTU limitation. 435 */ 436 switch(ifp->if_type) { 437 #if NFDDI > 0 438 case IFT_FDDI: 439 if (ifp->if_mtu > FDDIIPMTU) 440 rt->rt_rmx.rmx_mtu = FDDIIPMTU; 441 break; 442 #endif 443 #if NARCNET > 0 444 case IFT_ARCNET: 445 { 446 int arcipifmtu; 447 448 if (ifp->if_flags & IFF_LINK0) 449 arcipifmtu = arc_ipmtu; 450 else 451 arcipifmtu = ARCMTU; 452 if (ifp->if_mtu > arcipifmtu) 453 rt->rt_rmx.rmx_mtu = arcipifmtu; 454 break; 455 } 456 #endif 457 } 458 return; 459 } 460 461 switch (req) { 462 case RTM_SETGATE: 463 gate = arp_setgate(rt, gate, info->rti_info[RTAX_NETMASK]); 464 break; 465 case RTM_ADD: 466 gate = arp_setgate(rt, gate, info->rti_info[RTAX_NETMASK]); 467 if (gate == NULL) { 468 log(LOG_ERR, "%s: arp_setgate failed\n", __func__); 469 break; 470 } 471 if ((rt->rt_flags & RTF_CONNECTED) || 472 (rt->rt_flags & RTF_LOCAL)) { 473 /* 474 * Give this route an expiration time, even though 475 * it's a "permanent" route, so that routes cloned 476 * from it do not need their expiration time set. 477 */ 478 KASSERT(time_uptime != 0); 479 rt->rt_expire = time_uptime; 480 /* 481 * linklayers with particular link MTU limitation. 482 */ 483 switch (ifp->if_type) { 484 #if NFDDI > 0 485 case IFT_FDDI: 486 if ((rt->rt_rmx.rmx_locks & RTV_MTU) == 0 && 487 (rt->rt_rmx.rmx_mtu > FDDIIPMTU || 488 (rt->rt_rmx.rmx_mtu == 0 && 489 ifp->if_mtu > FDDIIPMTU))) 490 rt->rt_rmx.rmx_mtu = FDDIIPMTU; 491 break; 492 #endif 493 #if NARCNET > 0 494 case IFT_ARCNET: 495 { 496 int arcipifmtu; 497 if (ifp->if_flags & IFF_LINK0) 498 arcipifmtu = arc_ipmtu; 499 else 500 arcipifmtu = ARCMTU; 501 502 if ((rt->rt_rmx.rmx_locks & RTV_MTU) == 0 && 503 (rt->rt_rmx.rmx_mtu > arcipifmtu || 504 (rt->rt_rmx.rmx_mtu == 0 && 505 ifp->if_mtu > arcipifmtu))) 506 rt->rt_rmx.rmx_mtu = arcipifmtu; 507 break; 508 } 509 #endif 510 } 511 if (rt->rt_flags & RTF_CONNECTED) 512 break; 513 } 514 515 bound = curlwp_bind(); 516 /* Announce a new entry if requested. */ 517 if (rt->rt_flags & RTF_ANNOUNCE) { 518 struct psref psref; 519 ia = in_get_ia_on_iface_psref( 520 satocsin(rt_getkey(rt))->sin_addr, ifp, &psref); 521 if (ia != NULL) { 522 arpannounce(ifp, &ia->ia_ifa, 523 CLLADDR(satocsdl(gate))); 524 ia4_release(ia, &psref); 525 } 526 } 527 528 if (gate->sa_family != AF_LINK || 529 gate->sa_len < sockaddr_dl_measure(0, ifp->if_addrlen)) { 530 log(LOG_DEBUG, "%s: bad gateway value\n", __func__); 531 goto out; 532 } 533 534 satosdl(gate)->sdl_type = ifp->if_type; 535 satosdl(gate)->sdl_index = ifp->if_index; 536 537 /* 538 * If the route is for a broadcast address mark it as such. 539 * This way we can avoid an expensive call to in_broadcast() 540 * in ip_output() most of the time (because the route passed 541 * to ip_output() is almost always a host route). 542 */ 543 if (rt->rt_flags & RTF_HOST && 544 !(rt->rt_flags & RTF_BROADCAST) && 545 in_broadcast(satocsin(rt_getkey(rt))->sin_addr, rt->rt_ifp)) 546 rt->rt_flags |= RTF_BROADCAST; 547 /* There is little point in resolving the broadcast address */ 548 if (rt->rt_flags & RTF_BROADCAST) 549 goto out; 550 551 /* 552 * When called from rt_ifa_addlocal, we cannot depend on that 553 * the address (rt_getkey(rt)) exits in the address list of the 554 * interface. So check RTF_LOCAL instead. 555 */ 556 if (rt->rt_flags & RTF_LOCAL) { 557 rt->rt_expire = 0; 558 if (useloopback) { 559 rt->rt_ifp = lo0ifp; 560 rt->rt_rmx.rmx_mtu = 0; 561 } 562 goto out; 563 } 564 565 s = pserialize_read_enter(); 566 ia = in_get_ia_on_iface(satocsin(rt_getkey(rt))->sin_addr, ifp); 567 if (ia == NULL) { 568 pserialize_read_exit(s); 569 goto out; 570 } 571 572 rt->rt_expire = 0; 573 if (useloopback) { 574 rt->rt_ifp = lo0ifp; 575 rt->rt_rmx.rmx_mtu = 0; 576 } 577 rt->rt_flags |= RTF_LOCAL; 578 579 if (ISSET(info->rti_flags, RTF_DONTCHANGEIFA)) { 580 pserialize_read_exit(s); 581 goto out; 582 } 583 /* 584 * make sure to set rt->rt_ifa to the interface 585 * address we are using, otherwise we will have trouble 586 * with source address selection. 587 */ 588 ifa = &ia->ia_ifa; 589 if (ifa != rt->rt_ifa) 590 /* Assume it doesn't sleep */ 591 rt_replace_ifa(rt, ifa); 592 pserialize_read_exit(s); 593 out: 594 curlwp_bindx(bound); 595 break; 596 } 597 } 598 599 /* 600 * Broadcast an ARP request. Caller specifies: 601 * - arp header source ip address 602 * - arp header target ip address 603 * - arp header source ethernet address 604 */ 605 static void 606 arprequest(struct ifnet *ifp, 607 const struct in_addr *sip, const struct in_addr *tip, 608 const uint8_t *enaddr) 609 { 610 struct mbuf *m; 611 struct arphdr *ah; 612 struct sockaddr sa; 613 uint64_t *arps; 614 615 KASSERT(sip != NULL); 616 KASSERT(tip != NULL); 617 KASSERT(enaddr != NULL); 618 619 if ((m = m_gethdr(M_DONTWAIT, MT_DATA)) == NULL) 620 return; 621 MCLAIM(m, &arpdomain.dom_mowner); 622 switch (ifp->if_type) { 623 case IFT_IEEE1394: 624 m->m_len = sizeof(*ah) + 2 * sizeof(struct in_addr) + 625 ifp->if_addrlen; 626 break; 627 default: 628 m->m_len = sizeof(*ah) + 2 * sizeof(struct in_addr) + 629 2 * ifp->if_addrlen; 630 break; 631 } 632 m->m_pkthdr.len = m->m_len; 633 m_align(m, m->m_len); 634 ah = mtod(m, struct arphdr *); 635 memset(ah, 0, m->m_len); 636 switch (ifp->if_type) { 637 case IFT_IEEE1394: /* RFC2734 */ 638 /* fill it now for ar_tpa computation */ 639 ah->ar_hrd = htons(ARPHRD_IEEE1394); 640 break; 641 default: 642 /* ifp->if_output will fill ar_hrd */ 643 break; 644 } 645 ah->ar_pro = htons(ETHERTYPE_IP); 646 ah->ar_hln = ifp->if_addrlen; /* hardware address length */ 647 ah->ar_pln = sizeof(struct in_addr); /* protocol address length */ 648 ah->ar_op = htons(ARPOP_REQUEST); 649 memcpy(ar_sha(ah), enaddr, ah->ar_hln); 650 memcpy(ar_spa(ah), sip, ah->ar_pln); 651 memcpy(ar_tpa(ah), tip, ah->ar_pln); 652 sa.sa_family = AF_ARP; 653 sa.sa_len = 2; 654 m->m_flags |= M_BCAST; 655 arps = ARP_STAT_GETREF(); 656 arps[ARP_STAT_SNDTOTAL]++; 657 arps[ARP_STAT_SENDREQUEST]++; 658 ARP_STAT_PUTREF(); 659 if_output_lock(ifp, ifp, m, &sa, NULL); 660 } 661 662 void 663 arpannounce(struct ifnet *ifp, struct ifaddr *ifa, const uint8_t *enaddr) 664 { 665 struct in_ifaddr *ia = ifatoia(ifa); 666 struct in_addr *ip = &IA_SIN(ifa)->sin_addr; 667 668 if (ia->ia4_flags & (IN_IFF_NOTREADY | IN_IFF_DETACHED)) { 669 ARPLOG(LOG_DEBUG, "%s not ready\n", ARPLOGADDR(ip)); 670 return; 671 } 672 arprequest(ifp, ip, ip, enaddr); 673 } 674 675 static void 676 arpannounce1(struct ifaddr *ifa) 677 { 678 679 arpannounce(ifa->ifa_ifp, ifa, CLLADDR(ifa->ifa_ifp->if_sadl)); 680 } 681 682 /* 683 * Resolve an IP address into an ethernet address. If success, desten is 684 * filled in. If there is no entry in arptab, set one up and broadcast a 685 * request for the IP address. Hold onto this mbuf and resend it once the 686 * address is finally resolved. 687 * 688 * A return value of 0 indicates that desten has been filled in and the packet 689 * should be sent normally; a return value of EWOULDBLOCK indicates that the 690 * packet has been held pending resolution. Any other value indicates an 691 * error. 692 */ 693 int 694 arpresolve(struct ifnet *ifp, const struct rtentry *rt, struct mbuf *m, 695 const struct sockaddr *dst, void *desten, size_t destlen) 696 { 697 struct llentry *la; 698 const char *create_lookup; 699 bool renew; 700 int error; 701 struct ifnet *origifp = ifp; 702 703 #if NCARP > 0 704 if (rt != NULL && rt->rt_ifp->if_type == IFT_CARP) 705 ifp = rt->rt_ifp; 706 #endif 707 708 KASSERT(m != NULL); 709 710 la = arplookup(ifp, NULL, dst, 0); 711 if (la == NULL) 712 goto notfound; 713 714 if ((la->la_flags & LLE_VALID) && 715 ((la->la_flags & LLE_STATIC) || la->la_expire > time_uptime)) { 716 KASSERT(destlen >= ifp->if_addrlen); 717 memcpy(desten, &la->ll_addr, ifp->if_addrlen); 718 LLE_RUNLOCK(la); 719 return 0; 720 } 721 722 notfound: 723 if (ifp->if_flags & IFF_NOARP) { 724 if (la != NULL) 725 LLE_RUNLOCK(la); 726 error = ENOTSUP; 727 goto bad; 728 } 729 730 if (la == NULL) { 731 struct rtentry *_rt; 732 733 create_lookup = "create"; 734 _rt = rtalloc1(dst, 0); 735 IF_AFDATA_WLOCK(ifp); 736 la = lla_create(LLTABLE(ifp), LLE_EXCLUSIVE, dst, _rt); 737 IF_AFDATA_WUNLOCK(ifp); 738 if (_rt != NULL) 739 rt_unref(_rt); 740 if (la == NULL) 741 ARP_STATINC(ARP_STAT_ALLOCFAIL); 742 else { 743 struct sockaddr_in sin; 744 745 arp_init_llentry(ifp, la); 746 sockaddr_in_init(&sin, &la->r_l3addr.addr4, 0); 747 rt_clonedmsg(sintosa(&sin), ifp, rt); 748 } 749 } else if (LLE_TRY_UPGRADE(la) == 0) { 750 create_lookup = "lookup"; 751 LLE_RUNLOCK(la); 752 IF_AFDATA_RLOCK(ifp); 753 la = lla_lookup(LLTABLE(ifp), LLE_EXCLUSIVE, dst); 754 IF_AFDATA_RUNLOCK(ifp); 755 } 756 757 error = EINVAL; 758 if (la == NULL) { 759 log(LOG_DEBUG, 760 "%s: failed to %s llentry for %s on %s\n", 761 __func__, create_lookup, inet_ntoa(satocsin(dst)->sin_addr), 762 ifp->if_xname); 763 goto bad; 764 } 765 766 if ((la->la_flags & LLE_VALID) && 767 ((la->la_flags & LLE_STATIC) || la->la_expire > time_uptime)) 768 { 769 KASSERT(destlen >= ifp->if_addrlen); 770 memcpy(desten, &la->ll_addr, ifp->if_addrlen); 771 renew = false; 772 /* 773 * If entry has an expiry time and it is approaching, 774 * see if we need to send an ARP request within this 775 * arpt_down interval. 776 */ 777 if (!(la->la_flags & LLE_STATIC) && 778 time_uptime + la->la_preempt > la->la_expire) 779 { 780 renew = true; 781 la->la_preempt--; 782 } 783 784 LLE_WUNLOCK(la); 785 786 if (renew) { 787 const uint8_t *enaddr = CLLADDR(ifp->if_sadl); 788 arprequest(origifp, 789 &satocsin(rt->rt_ifa->ifa_addr)->sin_addr, 790 &satocsin(dst)->sin_addr, enaddr); 791 } 792 793 return 0; 794 } 795 796 if (la->la_flags & LLE_STATIC) { /* should not happen! */ 797 LLE_RUNLOCK(la); 798 log(LOG_DEBUG, "%s: ouch, empty static llinfo for %s\n", 799 __func__, inet_ntoa(satocsin(dst)->sin_addr)); 800 error = EINVAL; 801 goto bad; 802 } 803 804 renew = (la->la_asked == 0 || la->la_expire != time_uptime); 805 806 /* 807 * There is an arptab entry, but no ethernet address 808 * response yet. Add the mbuf to the list, dropping 809 * the oldest packet if we have exceeded the system 810 * setting. 811 */ 812 LLE_WLOCK_ASSERT(la); 813 if (la->la_numheld >= arp_maxhold) { 814 if (la->la_hold != NULL) { 815 struct mbuf *next = la->la_hold->m_nextpkt; 816 m_freem(la->la_hold); 817 la->la_hold = next; 818 la->la_numheld--; 819 ARP_STATINC(ARP_STAT_DFRDROPPED); 820 ARP_STATINC(ARP_STAT_DFRTOTAL); 821 } 822 } 823 if (la->la_hold != NULL) { 824 struct mbuf *curr = la->la_hold; 825 while (curr->m_nextpkt != NULL) 826 curr = curr->m_nextpkt; 827 curr->m_nextpkt = m; 828 } else 829 la->la_hold = m; 830 la->la_numheld++; 831 if (!renew) 832 LLE_DOWNGRADE(la); 833 834 /* 835 * Return EWOULDBLOCK if we have tried less than arp_maxtries. It 836 * will be masked by ether_output(). Return EHOSTDOWN/EHOSTUNREACH 837 * if we have already sent arp_maxtries ARP requests. Retransmit the 838 * ARP request, but not faster than one request per second. 839 */ 840 if (la->la_asked < arp_maxtries) 841 error = EWOULDBLOCK; /* First request. */ 842 else 843 error = (rt != NULL && rt->rt_flags & RTF_GATEWAY) ? 844 EHOSTUNREACH : EHOSTDOWN; 845 846 if (renew) { 847 const uint8_t *enaddr = CLLADDR(ifp->if_sadl); 848 la->la_expire = time_uptime; 849 arp_settimer(la, arpt_down); 850 la->la_asked++; 851 LLE_WUNLOCK(la); 852 853 if (rt != NULL) { 854 arprequest(origifp, 855 &satocsin(rt->rt_ifa->ifa_addr)->sin_addr, 856 &satocsin(dst)->sin_addr, enaddr); 857 } else { 858 struct sockaddr_in sin; 859 struct rtentry *_rt; 860 861 sockaddr_in_init(&sin, &la->r_l3addr.addr4, 0); 862 863 /* XXX */ 864 _rt = rtalloc1((struct sockaddr *)&sin, 0); 865 if (_rt == NULL) 866 goto bad; 867 arprequest(origifp, 868 &satocsin(_rt->rt_ifa->ifa_addr)->sin_addr, 869 &satocsin(dst)->sin_addr, enaddr); 870 rt_unref(_rt); 871 } 872 return error; 873 } 874 875 LLE_RUNLOCK(la); 876 return error; 877 878 bad: 879 m_freem(m); 880 return error; 881 } 882 883 /* 884 * Common length and type checks are done here, 885 * then the protocol-specific routine is called. 886 */ 887 void 888 arpintr(void) 889 { 890 struct mbuf *m; 891 struct arphdr *ar; 892 int s; 893 int arplen; 894 895 SOFTNET_KERNEL_LOCK_UNLESS_NET_MPSAFE(); 896 for (;;) { 897 struct ifnet *rcvif; 898 899 IFQ_LOCK(&arpintrq); 900 IF_DEQUEUE(&arpintrq, m); 901 IFQ_UNLOCK(&arpintrq); 902 if (m == NULL) 903 goto out; 904 if ((m->m_flags & M_PKTHDR) == 0) 905 panic("arpintr"); 906 907 MCLAIM(m, &arpdomain.dom_mowner); 908 ARP_STATINC(ARP_STAT_RCVTOTAL); 909 910 arplen = sizeof(struct arphdr); 911 if (m->m_len < arplen && (m = m_pullup(m, arplen)) == NULL) 912 goto badlen; 913 ar = mtod(m, struct arphdr *); 914 915 rcvif = m_get_rcvif(m, &s); 916 if (__predict_false(rcvif == NULL)) { 917 ARP_STATINC(ARP_STAT_RCVNOINT); 918 goto free; 919 } 920 921 /* 922 * We don't want non-IEEE1394 ARP packets on IEEE1394 923 * interfaces, and vice versa. Our life depends on that. 924 */ 925 switch (rcvif->if_type) { 926 case IFT_IEEE1394: 927 if (ntohs(ar->ar_hrd) != ARPHRD_IEEE1394) { 928 m_put_rcvif(rcvif, &s); 929 ARP_STATINC(ARP_STAT_RCVBADPROTO); 930 goto free; 931 } 932 933 arplen = sizeof(struct arphdr) + 934 ar->ar_hln + 2 * ar->ar_pln; 935 break; 936 default: 937 if (ntohs(ar->ar_hrd) == ARPHRD_IEEE1394) { 938 m_put_rcvif(rcvif, &s); 939 ARP_STATINC(ARP_STAT_RCVBADPROTO); 940 goto free; 941 } 942 943 arplen = sizeof(struct arphdr) + 944 2 * ar->ar_hln + 2 * ar->ar_pln; 945 break; 946 } 947 948 m_put_rcvif(rcvif, &s); 949 950 if (m->m_len < arplen && (m = m_pullup(m, arplen)) == NULL) 951 goto badlen; 952 ar = mtod(m, struct arphdr *); 953 954 switch (ntohs(ar->ar_pro)) { 955 case ETHERTYPE_IP: 956 case ETHERTYPE_IPTRAILERS: 957 in_arpinput(m); 958 continue; 959 default: 960 ARP_STATINC(ARP_STAT_RCVBADPROTO); 961 goto free; 962 } 963 964 badlen: 965 ARP_STATINC(ARP_STAT_RCVBADLEN); 966 free: 967 m_freem(m); 968 } 969 970 out: 971 SOFTNET_KERNEL_UNLOCK_UNLESS_NET_MPSAFE(); 972 return; /* XXX gcc */ 973 } 974 975 /* 976 * ARP for Internet protocols on 10 Mb/s Ethernet. Algorithm is that given in 977 * RFC 826. In addition, a sanity check is performed on the sender protocol 978 * address, to catch impersonators. 979 * 980 * We no longer handle negotiations for use of trailer protocol: formerly, ARP 981 * replied for protocol type ETHERTYPE_TRAIL sent along with IP replies if we 982 * wanted trailers sent to us, and also sent them in response to IP replies. 983 * This allowed either end to announce the desire to receive trailer packets. 984 * 985 * We no longer reply to requests for ETHERTYPE_TRAIL protocol either, but 986 * formerly didn't normally send requests. 987 */ 988 static void 989 in_arpinput(struct mbuf *m) 990 { 991 struct arphdr *ah; 992 struct ifnet *ifp, *rcvif = NULL; 993 struct llentry *la = NULL; 994 struct in_ifaddr *ia = NULL; 995 #if NBRIDGE > 0 996 struct in_ifaddr *bridge_ia = NULL; 997 #endif 998 #if NCARP > 0 999 uint32_t count = 0, index = 0; 1000 #endif 1001 struct sockaddr sa; 1002 struct in_addr isaddr, itaddr, myaddr; 1003 int op; 1004 void *tha; 1005 uint64_t *arps; 1006 struct psref psref, psref_ia; 1007 int s; 1008 char ipbuf[INET_ADDRSTRLEN]; 1009 bool do_dad; 1010 1011 if (__predict_false(m_makewritable(&m, 0, m->m_pkthdr.len, M_DONTWAIT))) 1012 goto out; 1013 ah = mtod(m, struct arphdr *); 1014 op = ntohs(ah->ar_op); 1015 1016 if (ah->ar_pln != sizeof(struct in_addr)) 1017 goto out; 1018 1019 ifp = if_get_bylla(ar_sha(ah), ah->ar_hln, &psref); 1020 if (ifp) { 1021 /* it's from me, ignore it. */ 1022 if_put(ifp, &psref); 1023 ARP_STATINC(ARP_STAT_RCVLOCALSHA); 1024 goto out; 1025 } 1026 1027 rcvif = ifp = m_get_rcvif_psref(m, &psref); 1028 if (__predict_false(rcvif == NULL)) 1029 goto out; 1030 if (rcvif->if_flags & IFF_NOARP) 1031 goto out; 1032 1033 memcpy(&isaddr, ar_spa(ah), sizeof(isaddr)); 1034 memcpy(&itaddr, ar_tpa(ah), sizeof(itaddr)); 1035 1036 if (m->m_flags & (M_BCAST|M_MCAST)) 1037 ARP_STATINC(ARP_STAT_RCVMCAST); 1038 1039 /* 1040 * Search for a matching interface address 1041 * or any address on the interface to use 1042 * as a dummy address in the rest of this function. 1043 * 1044 * If the target IP address is zero then try and find 1045 * the sender address for DAD. 1046 */ 1047 myaddr = in_nullhost(itaddr) ? isaddr : itaddr; 1048 s = pserialize_read_enter(); 1049 IN_ADDRHASH_READER_FOREACH(ia, myaddr.s_addr) { 1050 if (!in_hosteq(ia->ia_addr.sin_addr, myaddr)) 1051 continue; 1052 #if NCARP > 0 1053 if (ia->ia_ifp->if_type == IFT_CARP && 1054 ((ia->ia_ifp->if_flags & (IFF_UP|IFF_RUNNING)) == 1055 (IFF_UP|IFF_RUNNING))) { 1056 index++; 1057 /* XXX: ar_hln? */ 1058 if (ia->ia_ifp == rcvif && (ah->ar_hln >= 6) && 1059 carp_iamatch(ia, ar_sha(ah), 1060 &count, index)) { 1061 break; 1062 } 1063 } else 1064 #endif 1065 if (ia->ia_ifp == rcvif) 1066 break; 1067 #if NBRIDGE > 0 1068 /* 1069 * If the interface we received the packet on 1070 * is part of a bridge, check to see if we need 1071 * to "bridge" the packet to ourselves at this 1072 * layer. Note we still prefer a perfect match, 1073 * but allow this weaker match if necessary. 1074 */ 1075 if (rcvif->if_bridge != NULL && 1076 rcvif->if_bridge == ia->ia_ifp->if_bridge) 1077 bridge_ia = ia; 1078 #endif 1079 } 1080 1081 #if NBRIDGE > 0 1082 if (ia == NULL && bridge_ia != NULL) { 1083 ia = bridge_ia; 1084 m_put_rcvif_psref(rcvif, &psref); 1085 rcvif = NULL; 1086 /* FIXME */ 1087 ifp = bridge_ia->ia_ifp; 1088 } 1089 #endif 1090 if (ia != NULL) 1091 ia4_acquire(ia, &psref_ia); 1092 pserialize_read_exit(s); 1093 1094 if (ah->ar_hln != ifp->if_addrlen) { 1095 ARP_STATINC(ARP_STAT_RCVBADLEN); 1096 log(LOG_WARNING, 1097 "arp from %s: addr len: new %d, i/f %d (ignored)\n", 1098 IN_PRINT(ipbuf, &isaddr), ah->ar_hln, ifp->if_addrlen); 1099 goto out; 1100 } 1101 1102 /* Only do DaD if we have a matching address. */ 1103 do_dad = (ia != NULL); 1104 1105 if (ia == NULL) { 1106 ia = in_get_ia_on_iface_psref(isaddr, rcvif, &psref_ia); 1107 if (ia == NULL) { 1108 ia = in_get_ia_from_ifp_psref(ifp, &psref_ia); 1109 if (ia == NULL) { 1110 ARP_STATINC(ARP_STAT_RCVNOINT); 1111 goto out; 1112 } 1113 } 1114 } 1115 1116 myaddr = ia->ia_addr.sin_addr; 1117 1118 /* XXX checks for bridge case? */ 1119 if (!memcmp(ar_sha(ah), ifp->if_broadcastaddr, ifp->if_addrlen)) { 1120 ARP_STATINC(ARP_STAT_RCVBCASTSHA); 1121 log(LOG_ERR, 1122 "%s: arp: link address is broadcast for IP address %s!\n", 1123 ifp->if_xname, IN_PRINT(ipbuf, &isaddr)); 1124 goto out; 1125 } 1126 1127 /* 1128 * If the source IP address is zero, this is an RFC 5227 ARP probe 1129 */ 1130 if (in_nullhost(isaddr)) 1131 ARP_STATINC(ARP_STAT_RCVZEROSPA); 1132 else if (in_hosteq(isaddr, myaddr)) 1133 ARP_STATINC(ARP_STAT_RCVLOCALSPA); 1134 1135 if (in_nullhost(itaddr)) 1136 ARP_STATINC(ARP_STAT_RCVZEROTPA); 1137 1138 /* 1139 * DAD check, RFC 5227. 1140 * Collision on sender address is always a duplicate. 1141 * Collision on target address is only a duplicate IF 1142 * the sender address is the null host (ie a DAD probe) AND 1143 * the message was broadcast - if it's unicast then it's 1144 * a valid Unicast Poll from RFC 1122. 1145 */ 1146 if (do_dad && 1147 (in_hosteq(isaddr, myaddr) || 1148 (in_nullhost(isaddr) && in_hosteq(itaddr, myaddr) && 1149 m->m_flags & M_BCAST))) 1150 { 1151 struct sockaddr_dl sdl, *sdlp; 1152 1153 sdlp = sockaddr_dl_init(&sdl, sizeof(sdl), 1154 ifp->if_index, ifp->if_type, 1155 NULL, 0, ar_sha(ah), ah->ar_hln); 1156 arp_dad_duplicated((struct ifaddr *)ia, sdlp); 1157 goto out; 1158 } 1159 1160 /* 1161 * If the target IP address is zero, ignore the packet. 1162 * This prevents the code below from trying to answer 1163 * when we are using IP address zero (booting). 1164 */ 1165 if (in_nullhost(itaddr)) 1166 goto out; 1167 1168 if (in_nullhost(isaddr)) 1169 goto reply; 1170 1171 if (in_hosteq(itaddr, myaddr)) 1172 la = arpcreate(ifp, &isaddr, NULL, 1); 1173 else 1174 la = arplookup(ifp, &isaddr, NULL, 1); 1175 if (la == NULL) 1176 goto reply; 1177 1178 if ((la->la_flags & LLE_VALID) && 1179 memcmp(ar_sha(ah), &la->ll_addr, ifp->if_addrlen)) 1180 { 1181 char llabuf[LLA_ADDRSTRLEN], *llastr; 1182 1183 llastr = lla_snprintf(llabuf, sizeof(llabuf), 1184 ar_sha(ah), ah->ar_hln); 1185 1186 if (la->la_flags & LLE_STATIC) { 1187 ARP_STATINC(ARP_STAT_RCVOVERPERM); 1188 if (!log_permanent_modify) 1189 goto out; 1190 log(LOG_INFO, 1191 "%s tried to overwrite permanent arp info" 1192 " for %s\n", llastr, IN_PRINT(ipbuf, &isaddr)); 1193 goto out; 1194 } else if (la->lle_tbl->llt_ifp != ifp) { 1195 /* XXX should not happen? */ 1196 ARP_STATINC(ARP_STAT_RCVOVERINT); 1197 if (!log_wrong_iface) 1198 goto out; 1199 log(LOG_INFO, 1200 "%s on %s tried to overwrite " 1201 "arp info for %s on %s\n", 1202 llastr, 1203 ifp->if_xname, IN_PRINT(ipbuf, &isaddr), 1204 la->lle_tbl->llt_ifp->if_xname); 1205 goto out; 1206 } else { 1207 ARP_STATINC(ARP_STAT_RCVOVER); 1208 if (log_movements) 1209 log(LOG_INFO, "arp info overwritten " 1210 "for %s by %s\n", 1211 IN_PRINT(ipbuf, &isaddr), llastr); 1212 } 1213 } 1214 1215 KASSERT(ifp->if_sadl->sdl_alen == ifp->if_addrlen); 1216 1217 #if NTOKEN > 0 1218 /* 1219 * XXX uses m_data and assumes the complete answer including 1220 * XXX token-ring headers is in the same buf 1221 */ 1222 if (ifp->if_type == IFT_ISO88025) { 1223 struct token_header *trh; 1224 1225 trh = (struct token_header *)M_TRHSTART(m); 1226 if (trh->token_shost[0] & TOKEN_RI_PRESENT) { 1227 struct token_rif *rif; 1228 size_t riflen; 1229 1230 rif = TOKEN_RIF(trh); 1231 riflen = (ntohs(rif->tr_rcf) & 1232 TOKEN_RCF_LEN_MASK) >> 8; 1233 1234 if (riflen > 2 && 1235 riflen < sizeof(struct token_rif) && 1236 (riflen & 1) == 0) { 1237 rif->tr_rcf ^= htons(TOKEN_RCF_DIRECTION); 1238 rif->tr_rcf &= htons(~TOKEN_RCF_BROADCAST_MASK); 1239 memcpy(TOKEN_RIF_LLE(la), rif, riflen); 1240 } 1241 } 1242 } 1243 #endif 1244 1245 KASSERT(sizeof(la->ll_addr) >= ifp->if_addrlen); 1246 memcpy(&la->ll_addr, ar_sha(ah), ifp->if_addrlen); 1247 la->la_flags |= LLE_VALID; 1248 if ((la->la_flags & LLE_STATIC) == 0) { 1249 la->la_expire = time_uptime + arpt_keep; 1250 arp_settimer(la, arpt_keep); 1251 } 1252 la->la_asked = 0; 1253 /* rt->rt_flags &= ~RTF_REJECT; */ 1254 1255 if (la->la_hold != NULL) { 1256 int n = la->la_numheld; 1257 struct mbuf *m_hold, *m_hold_next; 1258 struct sockaddr_in sin; 1259 1260 sockaddr_in_init(&sin, &la->r_l3addr.addr4, 0); 1261 1262 m_hold = la->la_hold; 1263 la->la_hold = NULL; 1264 la->la_numheld = 0; 1265 /* 1266 * We have to unlock here because if_output would call 1267 * arpresolve 1268 */ 1269 LLE_WUNLOCK(la); 1270 ARP_STATADD(ARP_STAT_DFRSENT, n); 1271 ARP_STATADD(ARP_STAT_DFRTOTAL, n); 1272 for (; m_hold != NULL; m_hold = m_hold_next) { 1273 m_hold_next = m_hold->m_nextpkt; 1274 m_hold->m_nextpkt = NULL; 1275 if_output_lock(ifp, ifp, m_hold, sintosa(&sin), NULL); 1276 } 1277 } else 1278 LLE_WUNLOCK(la); 1279 la = NULL; 1280 1281 reply: 1282 if (la != NULL) { 1283 LLE_WUNLOCK(la); 1284 la = NULL; 1285 } 1286 if (op != ARPOP_REQUEST) { 1287 if (op == ARPOP_REPLY) 1288 ARP_STATINC(ARP_STAT_RCVREPLY); 1289 goto out; 1290 } 1291 ARP_STATINC(ARP_STAT_RCVREQUEST); 1292 if (in_hosteq(itaddr, myaddr)) { 1293 /* If our address is unusable, don't reply */ 1294 if (ia->ia4_flags & (IN_IFF_NOTREADY | IN_IFF_DETACHED)) 1295 goto out; 1296 /* I am the target */ 1297 tha = ar_tha(ah); 1298 if (tha) 1299 memcpy(tha, ar_sha(ah), ah->ar_hln); 1300 memcpy(ar_sha(ah), CLLADDR(ifp->if_sadl), ah->ar_hln); 1301 } else { 1302 /* Proxy ARP */ 1303 struct llentry *lle = NULL; 1304 struct sockaddr_in sin; 1305 1306 #if NCARP > 0 1307 if (ifp->if_type == IFT_CARP) { 1308 struct ifnet *_rcvif = m_get_rcvif(m, &s); 1309 int iftype = 0; 1310 if (__predict_true(_rcvif != NULL)) 1311 iftype = _rcvif->if_type; 1312 m_put_rcvif(_rcvif, &s); 1313 if (iftype != IFT_CARP) 1314 goto out; 1315 } 1316 #endif 1317 1318 tha = ar_tha(ah); 1319 1320 sockaddr_in_init(&sin, &itaddr, 0); 1321 1322 IF_AFDATA_RLOCK(ifp); 1323 lle = lla_lookup(LLTABLE(ifp), 0, (struct sockaddr *)&sin); 1324 IF_AFDATA_RUNLOCK(ifp); 1325 1326 if ((lle != NULL) && (lle->la_flags & LLE_PUB)) { 1327 if (tha) 1328 memcpy(tha, ar_sha(ah), ah->ar_hln); 1329 memcpy(ar_sha(ah), &lle->ll_addr, ah->ar_hln); 1330 LLE_RUNLOCK(lle); 1331 } else { 1332 if (lle != NULL) 1333 LLE_RUNLOCK(lle); 1334 goto out; 1335 } 1336 } 1337 ia4_release(ia, &psref_ia); 1338 1339 /* 1340 * XXX XXX: Here we're recycling the mbuf. But the mbuf could have 1341 * other mbufs in its chain, and just overwriting m->m_pkthdr.len 1342 * would be wrong in this case (the length becomes smaller than the 1343 * real chain size). 1344 * 1345 * This can theoretically cause bugs in the lower layers (drivers, 1346 * and L2encap), in some corner cases. 1347 */ 1348 memcpy(ar_tpa(ah), ar_spa(ah), ah->ar_pln); 1349 memcpy(ar_spa(ah), &itaddr, ah->ar_pln); 1350 ah->ar_op = htons(ARPOP_REPLY); 1351 ah->ar_pro = htons(ETHERTYPE_IP); /* let's be sure! */ 1352 switch (ifp->if_type) { 1353 case IFT_IEEE1394: 1354 /* ieee1394 arp reply is broadcast */ 1355 m->m_flags &= ~M_MCAST; 1356 m->m_flags |= M_BCAST; 1357 m->m_len = sizeof(*ah) + (2 * ah->ar_pln) + ah->ar_hln; 1358 break; 1359 default: 1360 m->m_flags &= ~(M_BCAST|M_MCAST); /* never reply by broadcast */ 1361 m->m_len = sizeof(*ah) + (2 * ah->ar_pln) + (2 * ah->ar_hln); 1362 break; 1363 } 1364 m->m_pkthdr.len = m->m_len; 1365 sa.sa_family = AF_ARP; 1366 sa.sa_len = 2; 1367 arps = ARP_STAT_GETREF(); 1368 arps[ARP_STAT_SNDTOTAL]++; 1369 arps[ARP_STAT_SNDREPLY]++; 1370 ARP_STAT_PUTREF(); 1371 if_output_lock(ifp, ifp, m, &sa, NULL); 1372 if (rcvif != NULL) 1373 m_put_rcvif_psref(rcvif, &psref); 1374 return; 1375 1376 out: 1377 if (la != NULL) 1378 LLE_WUNLOCK(la); 1379 if (ia != NULL) 1380 ia4_release(ia, &psref_ia); 1381 if (rcvif != NULL) 1382 m_put_rcvif_psref(rcvif, &psref); 1383 m_freem(m); 1384 } 1385 1386 /* 1387 * Lookup or a new address in arptab. 1388 */ 1389 static struct llentry * 1390 arplookup(struct ifnet *ifp, const struct in_addr *addr, 1391 const struct sockaddr *sa, int wlock) 1392 { 1393 struct sockaddr_in sin; 1394 struct llentry *la; 1395 int flags = wlock ? LLE_EXCLUSIVE : 0; 1396 1397 if (sa == NULL) { 1398 KASSERT(addr != NULL); 1399 sockaddr_in_init(&sin, addr, 0); 1400 sa = sintocsa(&sin); 1401 } 1402 1403 IF_AFDATA_RLOCK(ifp); 1404 la = lla_lookup(LLTABLE(ifp), flags, sa); 1405 IF_AFDATA_RUNLOCK(ifp); 1406 1407 return la; 1408 } 1409 1410 static struct llentry * 1411 arpcreate(struct ifnet *ifp, const struct in_addr *addr, 1412 const struct sockaddr *sa, int wlock) 1413 { 1414 struct sockaddr_in sin; 1415 struct llentry *la; 1416 int flags = wlock ? LLE_EXCLUSIVE : 0; 1417 1418 if (sa == NULL) { 1419 KASSERT(addr != NULL); 1420 sockaddr_in_init(&sin, addr, 0); 1421 sa = sintocsa(&sin); 1422 } 1423 1424 la = arplookup(ifp, addr, sa, wlock); 1425 1426 if (la == NULL) { 1427 struct rtentry *rt; 1428 1429 rt = rtalloc1(sa, 0); 1430 IF_AFDATA_WLOCK(ifp); 1431 la = lla_create(LLTABLE(ifp), flags, sa, rt); 1432 IF_AFDATA_WUNLOCK(ifp); 1433 if (rt != NULL) 1434 rt_unref(rt); 1435 1436 if (la != NULL) 1437 arp_init_llentry(ifp, la); 1438 } 1439 1440 return la; 1441 } 1442 1443 int 1444 arpioctl(u_long cmd, void *data) 1445 { 1446 1447 return EOPNOTSUPP; 1448 } 1449 1450 void 1451 arp_ifinit(struct ifnet *ifp, struct ifaddr *ifa) 1452 { 1453 struct in_ifaddr *ia = (struct in_ifaddr *)ifa; 1454 1455 ifa->ifa_rtrequest = arp_rtrequest; 1456 ifa->ifa_flags |= RTF_CONNECTED; 1457 1458 /* ARP will handle DAD for this address. */ 1459 if (in_nullhost(IA_SIN(ifa)->sin_addr)) { 1460 if (ia->ia_dad_stop != NULL) /* safety */ 1461 ia->ia_dad_stop(ifa); 1462 ia->ia_dad_start = NULL; 1463 ia->ia_dad_stop = NULL; 1464 ia->ia4_flags &= ~IN_IFF_TENTATIVE; 1465 } else { 1466 ia->ia_dad_start = arp_dad_start; 1467 ia->ia_dad_stop = arp_dad_stop; 1468 if (ia->ia4_flags & IN_IFF_TRYTENTATIVE && ip_dad_enabled()) 1469 ia->ia4_flags |= IN_IFF_TENTATIVE; 1470 else 1471 arpannounce1(ifa); 1472 } 1473 } 1474 1475 TAILQ_HEAD(dadq_head, dadq); 1476 struct dadq { 1477 TAILQ_ENTRY(dadq) dad_list; 1478 struct ifaddr *dad_ifa; 1479 int dad_count; /* max ARP to send */ 1480 int dad_arp_tcount; /* # of trials to send ARP */ 1481 int dad_arp_ocount; /* ARP sent so far */ 1482 int dad_arp_announce; /* max ARP announcements */ 1483 int dad_arp_acount; /* # of announcements */ 1484 struct callout dad_timer_ch; 1485 }; 1486 1487 static struct dadq_head dadq; 1488 static int dad_init = 0; 1489 static int dad_maxtry = 15; /* max # of *tries* to transmit DAD packet */ 1490 static kmutex_t arp_dad_lock; 1491 1492 static struct dadq * 1493 arp_dad_find(struct ifaddr *ifa) 1494 { 1495 struct dadq *dp; 1496 1497 KASSERT(mutex_owned(&arp_dad_lock)); 1498 1499 TAILQ_FOREACH(dp, &dadq, dad_list) { 1500 if (dp->dad_ifa == ifa) 1501 return dp; 1502 } 1503 return NULL; 1504 } 1505 1506 static void 1507 arp_dad_starttimer(struct dadq *dp, int ticks) 1508 { 1509 1510 callout_reset(&dp->dad_timer_ch, ticks, 1511 (void (*)(void *))arp_dad_timer, dp); 1512 } 1513 1514 static void 1515 arp_dad_stoptimer(struct dadq *dp) 1516 { 1517 1518 KASSERT(mutex_owned(&arp_dad_lock)); 1519 1520 TAILQ_REMOVE(&dadq, dp, dad_list); 1521 /* Tell the timer that dp is being destroyed. */ 1522 dp->dad_ifa = NULL; 1523 callout_halt(&dp->dad_timer_ch, &arp_dad_lock); 1524 } 1525 1526 static void 1527 arp_dad_destroytimer(struct dadq *dp) 1528 { 1529 1530 callout_destroy(&dp->dad_timer_ch); 1531 KASSERT(dp->dad_ifa == NULL); 1532 kmem_intr_free(dp, sizeof(*dp)); 1533 } 1534 1535 static void 1536 arp_dad_output(struct dadq *dp, struct ifaddr *ifa) 1537 { 1538 struct in_ifaddr *ia = (struct in_ifaddr *)ifa; 1539 struct ifnet *ifp = ifa->ifa_ifp; 1540 struct in_addr sip; 1541 1542 dp->dad_arp_tcount++; 1543 if ((ifp->if_flags & IFF_UP) == 0) 1544 return; 1545 if ((ifp->if_flags & IFF_RUNNING) == 0) 1546 return; 1547 1548 dp->dad_arp_tcount = 0; 1549 dp->dad_arp_ocount++; 1550 1551 memset(&sip, 0, sizeof(sip)); 1552 arprequest(ifa->ifa_ifp, &sip, &ia->ia_addr.sin_addr, 1553 CLLADDR(ifa->ifa_ifp->if_sadl)); 1554 } 1555 1556 /* 1557 * Start Duplicate Address Detection (DAD) for specified interface address. 1558 */ 1559 static void 1560 arp_dad_start(struct ifaddr *ifa) 1561 { 1562 struct in_ifaddr *ia = (struct in_ifaddr *)ifa; 1563 struct dadq *dp; 1564 char ipbuf[INET_ADDRSTRLEN]; 1565 1566 if (!dad_init) { 1567 TAILQ_INIT(&dadq); 1568 mutex_init(&arp_dad_lock, MUTEX_DEFAULT, IPL_NONE); 1569 dad_init++; 1570 } 1571 1572 /* 1573 * If we don't need DAD, don't do it. 1574 * - DAD is disabled 1575 */ 1576 if (!(ia->ia4_flags & IN_IFF_TENTATIVE)) { 1577 log(LOG_DEBUG, 1578 "%s: called with non-tentative address %s(%s)\n", __func__, 1579 IN_PRINT(ipbuf, &ia->ia_addr.sin_addr), 1580 ifa->ifa_ifp ? if_name(ifa->ifa_ifp) : "???"); 1581 return; 1582 } 1583 if (!ip_dad_enabled()) { 1584 ia->ia4_flags &= ~IN_IFF_TENTATIVE; 1585 rt_addrmsg(RTM_NEWADDR, ifa); 1586 arpannounce1(ifa); 1587 return; 1588 } 1589 KASSERT(ifa->ifa_ifp != NULL); 1590 if (!(ifa->ifa_ifp->if_flags & IFF_UP)) 1591 return; 1592 1593 dp = kmem_intr_alloc(sizeof(*dp), KM_NOSLEEP); 1594 1595 mutex_enter(&arp_dad_lock); 1596 if (arp_dad_find(ifa) != NULL) { 1597 mutex_exit(&arp_dad_lock); 1598 /* DAD already in progress */ 1599 if (dp != NULL) 1600 kmem_intr_free(dp, sizeof(*dp)); 1601 return; 1602 } 1603 1604 if (dp == NULL) { 1605 mutex_exit(&arp_dad_lock); 1606 log(LOG_ERR, "%s: memory allocation failed for %s(%s)\n", 1607 __func__, IN_PRINT(ipbuf, &ia->ia_addr.sin_addr), 1608 ifa->ifa_ifp ? if_name(ifa->ifa_ifp) : "???"); 1609 return; 1610 } 1611 1612 /* 1613 * Send ARP packet for DAD, ip_dad_count times. 1614 * Note that we must delay the first transmission. 1615 */ 1616 callout_init(&dp->dad_timer_ch, CALLOUT_MPSAFE); 1617 dp->dad_ifa = ifa; 1618 ifaref(ifa); /* just for safety */ 1619 dp->dad_count = ip_dad_count; 1620 dp->dad_arp_announce = 0; /* Will be set when starting to announce */ 1621 dp->dad_arp_acount = dp->dad_arp_ocount = dp->dad_arp_tcount = 0; 1622 TAILQ_INSERT_TAIL(&dadq, (struct dadq *)dp, dad_list); 1623 1624 ARPLOG(LOG_DEBUG, "%s: starting DAD for %s\n", if_name(ifa->ifa_ifp), 1625 ARPLOGADDR(&ia->ia_addr.sin_addr)); 1626 1627 arp_dad_starttimer(dp, cprng_fast32() % (PROBE_WAIT * hz)); 1628 1629 mutex_exit(&arp_dad_lock); 1630 } 1631 1632 /* 1633 * terminate DAD unconditionally. used for address removals. 1634 */ 1635 static void 1636 arp_dad_stop(struct ifaddr *ifa) 1637 { 1638 struct dadq *dp; 1639 1640 if (!dad_init) 1641 return; 1642 1643 mutex_enter(&arp_dad_lock); 1644 dp = arp_dad_find(ifa); 1645 if (dp == NULL) { 1646 mutex_exit(&arp_dad_lock); 1647 /* DAD wasn't started yet */ 1648 return; 1649 } 1650 1651 arp_dad_stoptimer(dp); 1652 1653 mutex_exit(&arp_dad_lock); 1654 1655 arp_dad_destroytimer(dp); 1656 ifafree(ifa); 1657 } 1658 1659 static void 1660 arp_dad_timer(struct dadq *dp) 1661 { 1662 struct ifaddr *ifa; 1663 struct in_ifaddr *ia; 1664 char ipbuf[INET_ADDRSTRLEN]; 1665 bool need_free = false; 1666 1667 KERNEL_LOCK_UNLESS_NET_MPSAFE(); 1668 mutex_enter(&arp_dad_lock); 1669 1670 ifa = dp->dad_ifa; 1671 if (ifa == NULL) { 1672 /* dp is being destroyed by someone. Do nothing. */ 1673 goto done; 1674 } 1675 1676 ia = (struct in_ifaddr *)ifa; 1677 if (ia->ia4_flags & IN_IFF_DUPLICATED) { 1678 log(LOG_ERR, "%s: called with duplicate address %s(%s)\n", 1679 __func__, IN_PRINT(ipbuf, &ia->ia_addr.sin_addr), 1680 ifa->ifa_ifp ? if_name(ifa->ifa_ifp) : "???"); 1681 goto done; 1682 } 1683 if ((ia->ia4_flags & IN_IFF_TENTATIVE) == 0 && dp->dad_arp_acount == 0) 1684 { 1685 log(LOG_ERR, "%s: called with non-tentative address %s(%s)\n", 1686 __func__, IN_PRINT(ipbuf, &ia->ia_addr.sin_addr), 1687 ifa->ifa_ifp ? if_name(ifa->ifa_ifp) : "???"); 1688 goto done; 1689 } 1690 1691 /* timeouted with IFF_{RUNNING,UP} check */ 1692 if (dp->dad_arp_tcount > dad_maxtry) { 1693 ARPLOG(LOG_INFO, "%s: could not run DAD, driver problem?\n", 1694 if_name(ifa->ifa_ifp)); 1695 1696 arp_dad_stoptimer(dp); 1697 need_free = true; 1698 goto done; 1699 } 1700 1701 /* Need more checks? */ 1702 if (dp->dad_arp_ocount < dp->dad_count) { 1703 int adelay; 1704 1705 /* 1706 * We have more ARP to go. Send ARP packet for DAD. 1707 */ 1708 arp_dad_output(dp, ifa); 1709 if (dp->dad_arp_ocount < dp->dad_count) 1710 adelay = (PROBE_MIN * hz) + 1711 (cprng_fast32() % 1712 ((PROBE_MAX * hz) - (PROBE_MIN * hz))); 1713 else 1714 adelay = ANNOUNCE_WAIT * hz; 1715 arp_dad_starttimer(dp, adelay); 1716 goto done; 1717 } else if (dp->dad_arp_acount == 0) { 1718 /* 1719 * We are done with DAD. 1720 * No duplicate address found. 1721 */ 1722 ia->ia4_flags &= ~IN_IFF_TENTATIVE; 1723 rt_addrmsg(RTM_NEWADDR, ifa); 1724 ARPLOG(LOG_DEBUG, 1725 "%s: DAD complete for %s - no duplicates found\n", 1726 if_name(ifa->ifa_ifp), ARPLOGADDR(&ia->ia_addr.sin_addr)); 1727 dp->dad_arp_announce = ANNOUNCE_NUM; 1728 goto announce; 1729 } else if (dp->dad_arp_acount < dp->dad_arp_announce) { 1730 announce: 1731 /* 1732 * Announce the address. 1733 */ 1734 arpannounce1(ifa); 1735 dp->dad_arp_acount++; 1736 if (dp->dad_arp_acount < dp->dad_arp_announce) { 1737 arp_dad_starttimer(dp, ANNOUNCE_INTERVAL * hz); 1738 goto done; 1739 } 1740 ARPLOG(LOG_DEBUG, 1741 "%s: ARP announcement complete for %s\n", 1742 if_name(ifa->ifa_ifp), ARPLOGADDR(&ia->ia_addr.sin_addr)); 1743 } 1744 1745 arp_dad_stoptimer(dp); 1746 need_free = true; 1747 done: 1748 mutex_exit(&arp_dad_lock); 1749 1750 if (need_free) { 1751 arp_dad_destroytimer(dp); 1752 KASSERT(ifa != NULL); 1753 ifafree(ifa); 1754 } 1755 1756 KERNEL_UNLOCK_UNLESS_NET_MPSAFE(); 1757 } 1758 1759 static void 1760 arp_dad_duplicated(struct ifaddr *ifa, const struct sockaddr_dl *from) 1761 { 1762 struct in_ifaddr *ia = (struct in_ifaddr *)ifa; 1763 struct ifnet *ifp = ifa->ifa_ifp; 1764 char ipbuf[INET_ADDRSTRLEN], llabuf[LLA_ADDRSTRLEN]; 1765 const char *iastr, *llastr; 1766 1767 iastr = IN_PRINT(ipbuf, &ia->ia_addr.sin_addr); 1768 if (__predict_false(from == NULL)) 1769 llastr = NULL; 1770 else 1771 llastr = lla_snprintf(llabuf, sizeof(llabuf), 1772 CLLADDR(from), from->sdl_alen); 1773 1774 if (ia->ia4_flags & (IN_IFF_TENTATIVE|IN_IFF_DUPLICATED)) { 1775 log(LOG_ERR, 1776 "%s: DAD duplicate address %s from %s\n", 1777 if_name(ifp), iastr, llastr); 1778 } else if (ia->ia_dad_defended == 0 || 1779 ia->ia_dad_defended < time_uptime - DEFEND_INTERVAL) { 1780 ia->ia_dad_defended = time_uptime; 1781 arpannounce1(ifa); 1782 log(LOG_ERR, 1783 "%s: DAD defended address %s from %s\n", 1784 if_name(ifp), iastr, llastr); 1785 return; 1786 } else { 1787 /* If DAD is disabled, just report the duplicate. */ 1788 if (!ip_dad_enabled()) { 1789 log(LOG_ERR, 1790 "%s: DAD ignoring duplicate address %s from %s\n", 1791 if_name(ifp), iastr, llastr); 1792 return; 1793 } 1794 log(LOG_ERR, 1795 "%s: DAD defence failed for %s from %s\n", 1796 if_name(ifp), iastr, llastr); 1797 } 1798 1799 arp_dad_stop(ifa); 1800 1801 ia->ia4_flags &= ~IN_IFF_TENTATIVE; 1802 if ((ia->ia4_flags & IN_IFF_DUPLICATED) == 0) { 1803 ia->ia4_flags |= IN_IFF_DUPLICATED; 1804 /* Inform the routing socket of the duplicate address */ 1805 rt_addrmsg_src(RTM_NEWADDR, ifa, (const struct sockaddr *)from); 1806 } 1807 } 1808 1809 /* 1810 * Called from 10 Mb/s Ethernet interrupt handlers 1811 * when ether packet type ETHERTYPE_REVARP 1812 * is received. Common length and type checks are done here, 1813 * then the protocol-specific routine is called. 1814 */ 1815 void 1816 revarpinput(struct mbuf *m) 1817 { 1818 struct arphdr *ar; 1819 int arplen; 1820 1821 arplen = sizeof(struct arphdr); 1822 if (m->m_len < arplen && (m = m_pullup(m, arplen)) == NULL) 1823 return; 1824 ar = mtod(m, struct arphdr *); 1825 1826 if (ntohs(ar->ar_hrd) == ARPHRD_IEEE1394) { 1827 goto out; 1828 } 1829 1830 arplen = sizeof(struct arphdr) + 2 * (ar->ar_hln + ar->ar_pln); 1831 if (m->m_len < arplen && (m = m_pullup(m, arplen)) == NULL) 1832 return; 1833 ar = mtod(m, struct arphdr *); 1834 1835 switch (ntohs(ar->ar_pro)) { 1836 case ETHERTYPE_IP: 1837 case ETHERTYPE_IPTRAILERS: 1838 in_revarpinput(m); 1839 return; 1840 1841 default: 1842 break; 1843 } 1844 1845 out: 1846 m_freem(m); 1847 } 1848 1849 /* 1850 * RARP for Internet protocols on 10 Mb/s Ethernet. 1851 * Algorithm is that given in RFC 903. 1852 * We are only using for bootstrap purposes to get an ip address for one of 1853 * our interfaces. Thus we support no user-interface. 1854 * 1855 * Since the contents of the RARP reply are specific to the interface that 1856 * sent the request, this code must ensure that they are properly associated. 1857 * 1858 * Note: also supports ARP via RARP packets, per the RFC. 1859 */ 1860 void 1861 in_revarpinput(struct mbuf *m) 1862 { 1863 struct arphdr *ah; 1864 void *tha; 1865 int op; 1866 struct ifnet *rcvif; 1867 int s; 1868 1869 ah = mtod(m, struct arphdr *); 1870 op = ntohs(ah->ar_op); 1871 1872 rcvif = m_get_rcvif(m, &s); 1873 if (__predict_false(rcvif == NULL)) 1874 goto out; 1875 if (rcvif->if_flags & IFF_NOARP) 1876 goto out; 1877 1878 switch (rcvif->if_type) { 1879 case IFT_IEEE1394: 1880 /* ARP without target hardware address is not supported */ 1881 goto out; 1882 default: 1883 break; 1884 } 1885 1886 switch (op) { 1887 case ARPOP_REQUEST: 1888 case ARPOP_REPLY: /* per RFC */ 1889 m_put_rcvif(rcvif, &s); 1890 in_arpinput(m); 1891 return; 1892 case ARPOP_REVREPLY: 1893 break; 1894 case ARPOP_REVREQUEST: /* handled by rarpd(8) */ 1895 default: 1896 goto out; 1897 } 1898 if (!revarp_in_progress) 1899 goto out; 1900 if (rcvif != myip_ifp) /* !same interface */ 1901 goto out; 1902 if (myip_initialized) 1903 goto wake; 1904 tha = ar_tha(ah); 1905 if (tha == NULL) 1906 goto out; 1907 if (ah->ar_pln != sizeof(struct in_addr)) 1908 goto out; 1909 if (ah->ar_hln != rcvif->if_sadl->sdl_alen) 1910 goto out; 1911 if (memcmp(tha, CLLADDR(rcvif->if_sadl), rcvif->if_sadl->sdl_alen)) 1912 goto out; 1913 memcpy(&srv_ip, ar_spa(ah), sizeof(srv_ip)); 1914 memcpy(&myip, ar_tpa(ah), sizeof(myip)); 1915 myip_initialized = 1; 1916 wake: /* Do wakeup every time in case it was missed. */ 1917 wakeup((void *)&myip); 1918 1919 out: 1920 m_put_rcvif(rcvif, &s); 1921 m_freem(m); 1922 } 1923 1924 /* 1925 * Send a RARP request for the ip address of the specified interface. 1926 * The request should be RFC 903-compliant. 1927 */ 1928 static void 1929 revarprequest(struct ifnet *ifp) 1930 { 1931 struct sockaddr sa; 1932 struct mbuf *m; 1933 struct arphdr *ah; 1934 void *tha; 1935 1936 if ((m = m_gethdr(M_DONTWAIT, MT_DATA)) == NULL) 1937 return; 1938 MCLAIM(m, &arpdomain.dom_mowner); 1939 m->m_len = sizeof(*ah) + 2*sizeof(struct in_addr) + 1940 2*ifp->if_addrlen; 1941 m->m_pkthdr.len = m->m_len; 1942 m_align(m, m->m_len); 1943 ah = mtod(m, struct arphdr *); 1944 memset(ah, 0, m->m_len); 1945 ah->ar_pro = htons(ETHERTYPE_IP); 1946 ah->ar_hln = ifp->if_addrlen; /* hardware address length */ 1947 ah->ar_pln = sizeof(struct in_addr); /* protocol address length */ 1948 ah->ar_op = htons(ARPOP_REVREQUEST); 1949 1950 memcpy(ar_sha(ah), CLLADDR(ifp->if_sadl), ah->ar_hln); 1951 tha = ar_tha(ah); 1952 if (tha == NULL) { 1953 m_free(m); 1954 return; 1955 } 1956 memcpy(tha, CLLADDR(ifp->if_sadl), ah->ar_hln); 1957 1958 sa.sa_family = AF_ARP; 1959 sa.sa_len = 2; 1960 m->m_flags |= M_BCAST; 1961 1962 if_output_lock(ifp, ifp, m, &sa, NULL); 1963 } 1964 1965 /* 1966 * RARP for the ip address of the specified interface, but also 1967 * save the ip address of the server that sent the answer. 1968 * Timeout if no response is received. 1969 */ 1970 int 1971 revarpwhoarewe(struct ifnet *ifp, struct in_addr *serv_in, 1972 struct in_addr *clnt_in) 1973 { 1974 int result, count = 20; 1975 1976 myip_initialized = 0; 1977 myip_ifp = ifp; 1978 1979 revarp_in_progress = 1; 1980 while (count--) { 1981 revarprequest(ifp); 1982 result = tsleep((void *)&myip, PSOCK, "revarp", hz/2); 1983 if (result != EWOULDBLOCK) 1984 break; 1985 } 1986 revarp_in_progress = 0; 1987 1988 if (!myip_initialized) 1989 return ENETUNREACH; 1990 1991 memcpy(serv_in, &srv_ip, sizeof(*serv_in)); 1992 memcpy(clnt_in, &myip, sizeof(*clnt_in)); 1993 return 0; 1994 } 1995 1996 void 1997 arp_stat_add(int type, uint64_t count) 1998 { 1999 ARP_STATADD(type, count); 2000 } 2001 2002 static int 2003 sysctl_net_inet_arp_stats(SYSCTLFN_ARGS) 2004 { 2005 2006 return NETSTAT_SYSCTL(arpstat_percpu, ARP_NSTATS); 2007 } 2008 2009 static void 2010 sysctl_net_inet_arp_setup(struct sysctllog **clog) 2011 { 2012 const struct sysctlnode *node; 2013 2014 sysctl_createv(clog, 0, NULL, NULL, 2015 CTLFLAG_PERMANENT, 2016 CTLTYPE_NODE, "inet", NULL, 2017 NULL, 0, NULL, 0, 2018 CTL_NET, PF_INET, CTL_EOL); 2019 sysctl_createv(clog, 0, NULL, &node, 2020 CTLFLAG_PERMANENT, 2021 CTLTYPE_NODE, "arp", 2022 SYSCTL_DESCR("Address Resolution Protocol"), 2023 NULL, 0, NULL, 0, 2024 CTL_NET, PF_INET, CTL_CREATE, CTL_EOL); 2025 2026 sysctl_createv(clog, 0, NULL, NULL, 2027 CTLFLAG_PERMANENT|CTLFLAG_READWRITE, 2028 CTLTYPE_INT, "keep", 2029 SYSCTL_DESCR("Valid ARP entry lifetime in seconds"), 2030 NULL, 0, &arpt_keep, 0, 2031 CTL_NET,PF_INET, node->sysctl_num, CTL_CREATE, CTL_EOL); 2032 2033 sysctl_createv(clog, 0, NULL, NULL, 2034 CTLFLAG_PERMANENT|CTLFLAG_READWRITE, 2035 CTLTYPE_INT, "down", 2036 SYSCTL_DESCR("Failed ARP entry lifetime in seconds"), 2037 NULL, 0, &arpt_down, 0, 2038 CTL_NET,PF_INET, node->sysctl_num, CTL_CREATE, CTL_EOL); 2039 2040 sysctl_createv(clog, 0, NULL, NULL, 2041 CTLFLAG_PERMANENT, 2042 CTLTYPE_STRUCT, "stats", 2043 SYSCTL_DESCR("ARP statistics"), 2044 sysctl_net_inet_arp_stats, 0, NULL, 0, 2045 CTL_NET,PF_INET, node->sysctl_num, CTL_CREATE, CTL_EOL); 2046 2047 sysctl_createv(clog, 0, NULL, NULL, 2048 CTLFLAG_PERMANENT|CTLFLAG_READWRITE, 2049 CTLTYPE_INT, "log_movements", 2050 SYSCTL_DESCR("log ARP replies from MACs different than" 2051 " the one in the cache"), 2052 NULL, 0, &log_movements, 0, 2053 CTL_NET,PF_INET, node->sysctl_num, CTL_CREATE, CTL_EOL); 2054 2055 sysctl_createv(clog, 0, NULL, NULL, 2056 CTLFLAG_PERMANENT|CTLFLAG_READWRITE, 2057 CTLTYPE_INT, "log_permanent_modify", 2058 SYSCTL_DESCR("log ARP replies from MACs different than" 2059 " the one in the permanent arp entry"), 2060 NULL, 0, &log_permanent_modify, 0, 2061 CTL_NET,PF_INET, node->sysctl_num, CTL_CREATE, CTL_EOL); 2062 2063 sysctl_createv(clog, 0, NULL, NULL, 2064 CTLFLAG_PERMANENT|CTLFLAG_READWRITE, 2065 CTLTYPE_INT, "log_wrong_iface", 2066 SYSCTL_DESCR("log ARP packets arriving on the wrong" 2067 " interface"), 2068 NULL, 0, &log_wrong_iface, 0, 2069 CTL_NET,PF_INET, node->sysctl_num, CTL_CREATE, CTL_EOL); 2070 2071 sysctl_createv(clog, 0, NULL, NULL, 2072 CTLFLAG_PERMANENT|CTLFLAG_READWRITE, 2073 CTLTYPE_INT, "log_unknown_network", 2074 SYSCTL_DESCR("log ARP packets from non-local network"), 2075 NULL, 0, &log_unknown_network, 0, 2076 CTL_NET,PF_INET, node->sysctl_num, CTL_CREATE, CTL_EOL); 2077 2078 sysctl_createv(clog, 0, NULL, NULL, 2079 CTLFLAG_PERMANENT|CTLFLAG_READWRITE, 2080 CTLTYPE_INT, "debug", 2081 SYSCTL_DESCR("Enable ARP DAD debug output"), 2082 NULL, 0, &arp_debug, 0, 2083 CTL_NET, PF_INET, node->sysctl_num, CTL_CREATE, CTL_EOL); 2084 } 2085 2086 #endif /* INET */ 2087