1 /* 2 * Copyright (c) 2004, 2005 The DragonFly Project. All rights reserved. 3 * 4 * This code is derived from software contributed to The DragonFly Project 5 * by Jeffrey M. Hsu. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 3. Neither the name of The DragonFly Project nor the names of its 16 * contributors may be used to endorse or promote products derived 17 * from this software without specific, prior written permission. 18 * 19 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 21 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 22 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE 23 * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 24 * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING, 25 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 26 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED 27 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 28 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT 29 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 30 * 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.1 (Berkeley) 6/10/93 62 * $FreeBSD: src/sys/netinet/if_ether.c,v 1.64.2.23 2003/04/11 07:23:15 fjoe Exp $ 63 */ 64 65 /* 66 * Ethernet address resolution protocol. 67 * TODO: 68 * add "inuse/lock" bit (or ref. count) along with valid bit 69 */ 70 71 #include "opt_inet.h" 72 #include "opt_carp.h" 73 74 #include <sys/param.h> 75 #include <sys/kernel.h> 76 #include <sys/queue.h> 77 #include <sys/sysctl.h> 78 #include <sys/systm.h> 79 #include <sys/mbuf.h> 80 #include <sys/malloc.h> 81 #include <sys/socket.h> 82 #include <sys/syslog.h> 83 #include <sys/lock.h> 84 85 #include <net/if.h> 86 #include <net/if_dl.h> 87 #include <net/if_types.h> 88 #include <net/route.h> 89 #include <net/netisr.h> 90 #include <net/if_llc.h> 91 92 #include <netinet/in.h> 93 #include <netinet/in_var.h> 94 #include <netinet/if_ether.h> 95 96 #include <sys/thread2.h> 97 #include <sys/msgport2.h> 98 #include <net/netmsg2.h> 99 #include <net/netisr2.h> 100 #include <sys/mplock2.h> 101 102 #ifdef CARP 103 #include <netinet/ip_carp.h> 104 #endif 105 106 #define SIN(s) ((struct sockaddr_in *)s) 107 #define SDL(s) ((struct sockaddr_dl *)s) 108 109 SYSCTL_DECL(_net_link_ether); 110 SYSCTL_NODE(_net_link_ether, PF_INET, inet, CTLFLAG_RW, 0, ""); 111 112 /* timer values */ 113 static int arpt_prune = (5*60*1); /* walk list every 5 minutes */ 114 static int arpt_keep = (20*60); /* once resolved, good for 20 more minutes */ 115 static int arpt_down = 20; /* once declared down, don't send for 20 sec */ 116 117 SYSCTL_INT(_net_link_ether_inet, OID_AUTO, prune_intvl, CTLFLAG_RW, 118 &arpt_prune, 0, ""); 119 SYSCTL_INT(_net_link_ether_inet, OID_AUTO, max_age, CTLFLAG_RW, 120 &arpt_keep, 0, ""); 121 SYSCTL_INT(_net_link_ether_inet, OID_AUTO, host_down_time, CTLFLAG_RW, 122 &arpt_down, 0, ""); 123 124 #define rt_expire rt_rmx.rmx_expire 125 126 struct llinfo_arp { 127 LIST_ENTRY(llinfo_arp) la_le; 128 struct rtentry *la_rt; 129 struct mbuf *la_hold; /* last packet until resolved/timeout */ 130 u_short la_preempt; /* countdown for pre-expiry arps */ 131 u_short la_asked; /* #times we QUERIED following expiration */ 132 }; 133 134 static LIST_HEAD(, llinfo_arp) llinfo_arp_list[MAXCPU]; 135 136 static int arp_maxtries = 5; 137 static int useloopback = 1; /* use loopback interface for local traffic */ 138 static int arp_proxyall = 0; 139 static int arp_refresh = 60; /* refresh arp cache ~60 (not impl yet) */ 140 static int arp_restricted_match = 0; 141 142 SYSCTL_INT(_net_link_ether_inet, OID_AUTO, maxtries, CTLFLAG_RW, 143 &arp_maxtries, 0, "ARP resolution attempts before returning error"); 144 SYSCTL_INT(_net_link_ether_inet, OID_AUTO, useloopback, CTLFLAG_RW, 145 &useloopback, 0, "Use the loopback interface for local traffic"); 146 SYSCTL_INT(_net_link_ether_inet, OID_AUTO, proxyall, CTLFLAG_RW, 147 &arp_proxyall, 0, "Enable proxy ARP for all suitable requests"); 148 SYSCTL_INT(_net_link_ether_inet, OID_AUTO, restricted_match, CTLFLAG_RW, 149 &arp_restricted_match, 0, "Only match against the sender"); 150 SYSCTL_INT(_net_link_ether_inet, OID_AUTO, refresh, CTLFLAG_RW, 151 &arp_refresh, 0, "Preemptively refresh the ARP"); 152 153 static void arp_rtrequest(int, struct rtentry *); 154 static void arprequest(struct ifnet *, const struct in_addr *, 155 const struct in_addr *, const u_char *); 156 static void arprequest_async(struct ifnet *, const struct in_addr *, 157 const struct in_addr *, const u_char *); 158 static void arpintr(netmsg_t msg); 159 static void arptfree(struct llinfo_arp *); 160 static void arptimer(void *); 161 static struct llinfo_arp * 162 arplookup(in_addr_t, boolean_t, boolean_t, boolean_t); 163 #ifdef INET 164 static void in_arpinput(struct mbuf *); 165 static void in_arpreply(struct mbuf *m, in_addr_t, in_addr_t); 166 static void arp_update_msghandler(netmsg_t); 167 static void arp_reply_msghandler(netmsg_t); 168 #endif 169 170 struct arptimer_ctx { 171 struct callout timer_ch; 172 struct netmsg_base timer_nmsg; 173 int timer_inited; 174 } __cachealign; 175 176 static struct arptimer_ctx arptimer_context[MAXCPU]; 177 178 /* 179 * Timeout routine. Age arp_tab entries periodically. 180 */ 181 static void 182 arptimer_dispatch(netmsg_t nmsg) 183 { 184 struct llinfo_arp *la, *nla; 185 int cpuid = mycpuid; 186 187 /* Reply ASAP */ 188 crit_enter(); 189 lwkt_replymsg(&nmsg->lmsg, 0); 190 crit_exit(); 191 192 LIST_FOREACH_MUTABLE(la, &llinfo_arp_list[cpuid], la_le, nla) { 193 if (la->la_rt->rt_expire && la->la_rt->rt_expire <= time_uptime) 194 arptfree(la); 195 } 196 callout_reset(&arptimer_context[cpuid].timer_ch, arpt_prune * hz, 197 arptimer, NULL); 198 } 199 200 static void 201 arptimer(void *arg __unused) 202 { 203 int cpuid = mycpuid; 204 struct lwkt_msg *lmsg = &arptimer_context[cpuid].timer_nmsg.lmsg; 205 206 crit_enter(); 207 if (lmsg->ms_flags & MSGF_DONE) 208 lwkt_sendmsg_oncpu(netisr_cpuport(cpuid), lmsg); 209 crit_exit(); 210 } 211 212 /* 213 * Parallel to llc_rtrequest. 214 * 215 * Called after a route is successfully added to the tree to fix-up the 216 * route and initiate arp operations if required. 217 */ 218 static void 219 arp_rtrequest(int req, struct rtentry *rt) 220 { 221 struct sockaddr *gate = rt->rt_gateway; 222 struct llinfo_arp *la = rt->rt_llinfo; 223 224 struct sockaddr_dl null_sdl = { sizeof null_sdl, AF_LINK }; 225 226 if (__predict_false(!arptimer_context[mycpuid].timer_inited)) { 227 struct arptimer_ctx *ctx = &arptimer_context[mycpuid]; 228 229 ctx->timer_inited = TRUE; 230 netmsg_init(&ctx->timer_nmsg, NULL, &netisr_adone_rport, 231 MSGF_PRIORITY, arptimer_dispatch); 232 callout_init_mp(&ctx->timer_ch); 233 callout_reset(&ctx->timer_ch, hz, arptimer, NULL); 234 } 235 if (rt->rt_flags & RTF_GATEWAY) 236 return; 237 238 switch (req) { 239 case RTM_ADD: 240 /* 241 * XXX: If this is a manually added route to interface 242 * such as older version of routed or gated might provide, 243 * restore cloning bit. 244 */ 245 if (!(rt->rt_flags & RTF_HOST) && 246 SIN(rt_mask(rt))->sin_addr.s_addr != 0xffffffff) 247 rt->rt_flags |= RTF_CLONING; 248 if (rt->rt_flags & RTF_CLONING) { 249 /* 250 * Case 1: This route should come from a route to iface. 251 */ 252 rt_setgate(rt, rt_key(rt), 253 (struct sockaddr *)&null_sdl, 254 RTL_DONTREPORT); 255 gate = rt->rt_gateway; 256 SDL(gate)->sdl_type = rt->rt_ifp->if_type; 257 SDL(gate)->sdl_index = rt->rt_ifp->if_index; 258 rt->rt_expire = time_uptime; 259 break; 260 } 261 /* 262 * Announce a new entry if requested, and only announce it 263 * once on cpu0. 264 */ 265 if ((rt->rt_flags & RTF_ANNOUNCE) && mycpuid == 0) { 266 arprequest_async(rt->rt_ifp, 267 &SIN(rt_key(rt))->sin_addr, 268 &SIN(rt_key(rt))->sin_addr, 269 LLADDR(SDL(gate))); 270 } 271 /*FALLTHROUGH*/ 272 case RTM_RESOLVE: 273 if (gate->sa_family != AF_LINK || 274 gate->sa_len < sizeof(struct sockaddr_dl)) { 275 log(LOG_DEBUG, "arp_rtrequest: bad gateway value\n"); 276 break; 277 } 278 SDL(gate)->sdl_type = rt->rt_ifp->if_type; 279 SDL(gate)->sdl_index = rt->rt_ifp->if_index; 280 if (la != NULL) 281 break; /* This happens on a route change */ 282 /* 283 * Case 2: This route may come from cloning, or a manual route 284 * add with a LL address. 285 */ 286 R_Malloc(la, struct llinfo_arp *, sizeof *la); 287 rt->rt_llinfo = la; 288 if (la == NULL) { 289 log(LOG_DEBUG, "arp_rtrequest: malloc failed\n"); 290 break; 291 } 292 bzero(la, sizeof *la); 293 la->la_rt = rt; 294 rt->rt_flags |= RTF_LLINFO; 295 LIST_INSERT_HEAD(&llinfo_arp_list[mycpuid], la, la_le); 296 297 #ifdef INET 298 /* 299 * This keeps the multicast addresses from showing up 300 * in `arp -a' listings as unresolved. It's not actually 301 * functional. Then the same for broadcast. 302 */ 303 if (IN_MULTICAST(ntohl(SIN(rt_key(rt))->sin_addr.s_addr))) { 304 ETHER_MAP_IP_MULTICAST(&SIN(rt_key(rt))->sin_addr, 305 LLADDR(SDL(gate))); 306 SDL(gate)->sdl_alen = 6; 307 rt->rt_expire = 0; 308 } 309 if (in_broadcast(SIN(rt_key(rt))->sin_addr, rt->rt_ifp)) { 310 memcpy(LLADDR(SDL(gate)), rt->rt_ifp->if_broadcastaddr, 311 rt->rt_ifp->if_addrlen); 312 SDL(gate)->sdl_alen = rt->rt_ifp->if_addrlen; 313 rt->rt_expire = 0; 314 } 315 #endif 316 317 /* 318 * This fixes up the routing interface for local addresses. 319 * The route is adjusted to point at lo0 and the expiration 320 * timer is disabled. 321 * 322 * NOTE: This prevents locally targetted traffic from going 323 * out the hardware interface, which is inefficient 324 * and might not work if the hardware cannot listen 325 * to its own transmitted packets. Setting 326 * net.link.ether.inet.useloopback to 0 will force 327 * packets for local addresses out the hardware (and 328 * it is expected to receive its own packet). 329 * 330 * XXX We should just be able to test RTF_LOCAL here instead 331 * of having to compare IPs. 332 */ 333 if (SIN(rt_key(rt))->sin_addr.s_addr == 334 (IA_SIN(rt->rt_ifa))->sin_addr.s_addr) { 335 rt->rt_expire = 0; 336 bcopy(IF_LLADDR(rt->rt_ifp), LLADDR(SDL(gate)), 337 SDL(gate)->sdl_alen = rt->rt_ifp->if_addrlen); 338 if (useloopback) 339 rt->rt_ifp = loif; 340 } 341 break; 342 343 case RTM_DELETE: 344 if (la == NULL) 345 break; 346 LIST_REMOVE(la, la_le); 347 rt->rt_llinfo = NULL; 348 rt->rt_flags &= ~RTF_LLINFO; 349 if (la->la_hold != NULL) 350 m_freem(la->la_hold); 351 Free(la); 352 break; 353 } 354 } 355 356 static struct mbuf * 357 arpreq_alloc(struct ifnet *ifp, const struct in_addr *sip, 358 const struct in_addr *tip, const u_char *enaddr) 359 { 360 struct mbuf *m; 361 struct arphdr *ah; 362 u_short ar_hrd; 363 364 if ((m = m_gethdr(M_NOWAIT, MT_DATA)) == NULL) 365 return NULL; 366 m->m_pkthdr.rcvif = NULL; 367 368 switch (ifp->if_type) { 369 case IFT_ETHER: 370 /* 371 * This may not be correct for types not explicitly 372 * listed, but this is our best guess 373 */ 374 default: 375 ar_hrd = htons(ARPHRD_ETHER); 376 377 m->m_len = arphdr_len2(ifp->if_addrlen, sizeof(struct in_addr)); 378 m->m_pkthdr.len = m->m_len; 379 MH_ALIGN(m, m->m_len); 380 381 ah = mtod(m, struct arphdr *); 382 break; 383 } 384 385 ah->ar_hrd = ar_hrd; 386 ah->ar_pro = htons(ETHERTYPE_IP); 387 ah->ar_hln = ifp->if_addrlen; /* hardware address length */ 388 ah->ar_pln = sizeof(struct in_addr); /* protocol address length */ 389 ah->ar_op = htons(ARPOP_REQUEST); 390 memcpy(ar_sha(ah), enaddr, ah->ar_hln); 391 memset(ar_tha(ah), 0, ah->ar_hln); 392 memcpy(ar_spa(ah), sip, ah->ar_pln); 393 memcpy(ar_tpa(ah), tip, ah->ar_pln); 394 395 return m; 396 } 397 398 static void 399 arpreq_send(struct ifnet *ifp, struct mbuf *m) 400 { 401 struct sockaddr sa; 402 struct ether_header *eh; 403 404 switch (ifp->if_type) { 405 case IFT_ETHER: 406 /* 407 * This may not be correct for types not explicitly 408 * listed, but this is our best guess 409 */ 410 default: 411 eh = (struct ether_header *)sa.sa_data; 412 /* if_output() will not swap */ 413 eh->ether_type = htons(ETHERTYPE_ARP); 414 memcpy(eh->ether_dhost, ifp->if_broadcastaddr, ifp->if_addrlen); 415 break; 416 } 417 418 sa.sa_family = AF_UNSPEC; 419 sa.sa_len = sizeof(sa); 420 ifp->if_output(ifp, m, &sa, NULL); 421 } 422 423 static void 424 arpreq_send_handler(netmsg_t msg) 425 { 426 struct mbuf *m = msg->packet.nm_packet; 427 struct ifnet *ifp = msg->lmsg.u.ms_resultp; 428 429 arpreq_send(ifp, m); 430 /* nmsg was embedded in the mbuf, do not reply! */ 431 } 432 433 /* 434 * Broadcast an ARP request. Caller specifies: 435 * - arp header source ip address 436 * - arp header target ip address 437 * - arp header source ethernet address 438 * 439 * NOTE: Caller MUST NOT hold ifp's serializer 440 */ 441 static void 442 arprequest(struct ifnet *ifp, const struct in_addr *sip, 443 const struct in_addr *tip, const u_char *enaddr) 444 { 445 struct mbuf *m; 446 447 if (enaddr == NULL) { 448 if (ifp->if_bridge) { 449 enaddr = IF_LLADDR(ether_bridge_interface(ifp)); 450 } else { 451 enaddr = IF_LLADDR(ifp); 452 } 453 } 454 455 m = arpreq_alloc(ifp, sip, tip, enaddr); 456 if (m == NULL) 457 return; 458 arpreq_send(ifp, m); 459 } 460 461 /* 462 * Same as arprequest(), except: 463 * - Caller is allowed to hold ifp's serializer 464 * - Network output is done in protocol thead 465 */ 466 static void 467 arprequest_async(struct ifnet *ifp, const struct in_addr *sip, 468 const struct in_addr *tip, const u_char *enaddr) 469 { 470 struct mbuf *m; 471 struct netmsg_packet *pmsg; 472 473 if (enaddr == NULL) { 474 if (ifp->if_bridge) { 475 enaddr = IF_LLADDR(ether_bridge_interface(ifp)); 476 } else { 477 enaddr = IF_LLADDR(ifp); 478 } 479 } 480 m = arpreq_alloc(ifp, sip, tip, enaddr); 481 if (m == NULL) 482 return; 483 484 pmsg = &m->m_hdr.mh_netmsg; 485 netmsg_init(&pmsg->base, NULL, &netisr_apanic_rport, 486 0, arpreq_send_handler); 487 pmsg->nm_packet = m; 488 pmsg->base.lmsg.u.ms_resultp = ifp; 489 490 lwkt_sendmsg_oncpu(netisr_cpuport(mycpuid), &pmsg->base.lmsg); 491 } 492 493 /* 494 * Resolve an IP address into an ethernet address. If success, 495 * desten is filled in. If there is no entry in arptab, 496 * set one up and broadcast a request for the IP address. 497 * Hold onto this mbuf and resend it once the address 498 * is finally resolved. A return value of 1 indicates 499 * that desten has been filled in and the packet should be sent 500 * normally; a 0 return indicates that the packet has been 501 * taken over here, either now or for later transmission. 502 */ 503 int 504 arpresolve(struct ifnet *ifp, struct rtentry *rt0, struct mbuf *m, 505 struct sockaddr *dst, u_char *desten) 506 { 507 struct rtentry *rt = NULL; 508 struct llinfo_arp *la = NULL; 509 struct sockaddr_dl *sdl; 510 511 if (m->m_flags & M_BCAST) { /* broadcast */ 512 memcpy(desten, ifp->if_broadcastaddr, ifp->if_addrlen); 513 return (1); 514 } 515 if (m->m_flags & M_MCAST) {/* multicast */ 516 ETHER_MAP_IP_MULTICAST(&SIN(dst)->sin_addr, desten); 517 return (1); 518 } 519 if (rt0 != NULL) { 520 if (rt_llroute(dst, rt0, &rt) != 0) { 521 m_freem(m); 522 return 0; 523 } 524 la = rt->rt_llinfo; 525 } 526 if (la == NULL) { 527 la = arplookup(SIN(dst)->sin_addr.s_addr, 528 TRUE, RTL_REPORTMSG, FALSE); 529 if (la != NULL) 530 rt = la->la_rt; 531 } 532 if (la == NULL || rt == NULL) { 533 char addr[INET_ADDRSTRLEN]; 534 535 log(LOG_DEBUG, "arpresolve: can't allocate llinfo for %s%s%s\n", 536 kinet_ntoa(SIN(dst)->sin_addr, addr), la ? "la" : " ", 537 rt ? "rt" : ""); 538 m_freem(m); 539 return (0); 540 } 541 sdl = SDL(rt->rt_gateway); 542 /* 543 * Check the address family and length is valid, the address 544 * is resolved; otherwise, try to resolve. 545 */ 546 if ((rt->rt_expire == 0 || rt->rt_expire > time_uptime) && 547 sdl->sdl_family == AF_LINK && sdl->sdl_alen != 0) { 548 /* 549 * If entry has an expiry time and it is approaching, 550 * see if we need to send an ARP request within this 551 * arpt_down interval. 552 */ 553 if ((rt->rt_expire != 0) && 554 (time_uptime + la->la_preempt > rt->rt_expire)) { 555 arprequest(ifp, 556 &SIN(rt->rt_ifa->ifa_addr)->sin_addr, 557 &SIN(dst)->sin_addr, 558 NULL); 559 la->la_preempt--; 560 } 561 562 bcopy(LLADDR(sdl), desten, sdl->sdl_alen); 563 return 1; 564 } 565 /* 566 * If ARP is disabled or static on this interface, stop. 567 * XXX 568 * Probably should not allocate empty llinfo struct if we are 569 * not going to be sending out an arp request. 570 */ 571 if (ifp->if_flags & (IFF_NOARP | IFF_STATICARP)) { 572 m_freem(m); 573 return (0); 574 } 575 /* 576 * There is an arptab entry, but no ethernet address 577 * response yet. Replace the held mbuf with this 578 * latest one. 579 */ 580 if (la->la_hold != NULL) 581 m_freem(la->la_hold); 582 la->la_hold = m; 583 if (rt->rt_expire || ((rt->rt_flags & RTF_STATIC) && !sdl->sdl_alen)) { 584 rt->rt_flags &= ~RTF_REJECT; 585 if (la->la_asked == 0 || rt->rt_expire != time_uptime) { 586 rt->rt_expire = time_uptime; 587 if (la->la_asked++ < arp_maxtries) { 588 arprequest(ifp, 589 &SIN(rt->rt_ifa->ifa_addr)->sin_addr, 590 &SIN(dst)->sin_addr, 591 NULL); 592 } else { 593 rt->rt_flags |= RTF_REJECT; 594 rt->rt_expire += arpt_down; 595 la->la_asked = 0; 596 la->la_preempt = arp_maxtries; 597 } 598 } 599 } 600 return (0); 601 } 602 603 /* 604 * Common length and type checks are done here, 605 * then the protocol-specific routine is called. 606 */ 607 static void 608 arpintr(netmsg_t msg) 609 { 610 struct mbuf *m = msg->packet.nm_packet; 611 struct arphdr *ar; 612 u_short ar_hrd; 613 char hexstr[6]; 614 615 if (m->m_len < sizeof(struct arphdr) && 616 (m = m_pullup(m, sizeof(struct arphdr))) == NULL) { 617 log(LOG_ERR, "arp: runt packet -- m_pullup failed\n"); 618 return; 619 } 620 ar = mtod(m, struct arphdr *); 621 622 ar_hrd = ntohs(ar->ar_hrd); 623 if (ar_hrd != ARPHRD_ETHER && ar_hrd != ARPHRD_IEEE802) { 624 hexncpy((unsigned char *)&ar->ar_hrd, 2, hexstr, 5, NULL); 625 log(LOG_ERR, "arp: unknown hardware address format (0x%s)\n", 626 hexstr); 627 m_freem(m); 628 return; 629 } 630 631 if (m->m_pkthdr.len < arphdr_len(ar)) { 632 if ((m = m_pullup(m, arphdr_len(ar))) == NULL) { 633 log(LOG_ERR, "arp: runt packet\n"); 634 return; 635 } 636 ar = mtod(m, struct arphdr *); 637 } 638 639 switch (ntohs(ar->ar_pro)) { 640 #ifdef INET 641 case ETHERTYPE_IP: 642 in_arpinput(m); 643 return; 644 #endif 645 } 646 m_freem(m); 647 /* msg was embedded in the mbuf, do not reply! */ 648 } 649 650 #ifdef INET 651 /* 652 * ARP for Internet protocols on 10 Mb/s Ethernet. 653 * Algorithm is that given in RFC 826. 654 * In addition, a sanity check is performed on the sender 655 * protocol address, to catch impersonators. 656 * We no longer handle negotiations for use of trailer protocol: 657 * Formerly, ARP replied for protocol type ETHERTYPE_TRAIL sent 658 * along with IP replies if we wanted trailers sent to us, 659 * and also sent them in response to IP replies. 660 * This allowed either end to announce the desire to receive 661 * trailer packets. 662 * We no longer reply to requests for ETHERTYPE_TRAIL protocol either, 663 * but formerly didn't normally send requests. 664 */ 665 666 static int log_arp_wrong_iface = 1; 667 static int log_arp_movements = 1; 668 static int log_arp_permanent_modify = 1; 669 static int log_arp_creation_failure = 1; 670 671 SYSCTL_INT(_net_link_ether_inet, OID_AUTO, log_arp_wrong_iface, CTLFLAG_RW, 672 &log_arp_wrong_iface, 0, 673 "Log arp packets arriving on the wrong interface"); 674 SYSCTL_INT(_net_link_ether_inet, OID_AUTO, log_arp_movements, CTLFLAG_RW, 675 &log_arp_movements, 0, 676 "Log arp replies from MACs different than the one in the cache"); 677 SYSCTL_INT(_net_link_ether_inet, OID_AUTO, log_arp_permanent_modify, CTLFLAG_RW, 678 &log_arp_permanent_modify, 0, 679 "Log arp replies from MACs different than the one " 680 "in the permanent arp entry"); 681 SYSCTL_INT(_net_link_ether_inet, OID_AUTO, log_arp_creation_failure, CTLFLAG_RW, 682 &log_arp_creation_failure, 0, "Log arp creation failure"); 683 684 /* 685 * Returns non-zero if the routine updated anything. 686 */ 687 static int 688 arp_update_oncpu(struct mbuf *m, in_addr_t saddr, boolean_t create, 689 boolean_t generate_report, boolean_t dologging) 690 { 691 struct arphdr *ah = mtod(m, struct arphdr *); 692 struct ifnet *ifp = m->m_pkthdr.rcvif; 693 struct llinfo_arp *la; 694 struct sockaddr_dl *sdl; 695 struct rtentry *rt; 696 char hexstr[2][64]; 697 char sbuf[INET_ADDRSTRLEN]; 698 int changed = create; 699 700 KASSERT(curthread->td_type == TD_TYPE_NETISR, 701 ("arp update not in netisr")); 702 703 la = arplookup(saddr, create, generate_report, FALSE); 704 if (la && (rt = la->la_rt) && (sdl = SDL(rt->rt_gateway))) { 705 struct in_addr isaddr = { saddr }; 706 707 /* 708 * Normally arps coming in on the wrong interface are ignored, 709 * but if we are bridging and the two interfaces belong to 710 * the same bridge, or one is a member of the bridge which 711 * is the other, then it isn't an error. 712 */ 713 if (rt->rt_ifp != ifp) { 714 /* 715 * (1) ifp and rt_ifp both members of same bridge 716 * (2) rt_ifp member of bridge ifp 717 * (3) ifp member of bridge rt_ifp 718 * 719 * Always replace rt_ifp with the bridge ifc. 720 */ 721 struct ifnet *nifp; 722 723 if (ifp->if_bridge && 724 rt->rt_ifp->if_bridge == ifp->if_bridge) { 725 nifp = ether_bridge_interface(ifp); 726 } else if (rt->rt_ifp->if_bridge && 727 ether_bridge_interface(rt->rt_ifp) == ifp) { 728 nifp = ifp; 729 } else if (ifp->if_bridge && 730 ether_bridge_interface(ifp) == rt->rt_ifp) { 731 nifp = rt->rt_ifp; 732 } else { 733 nifp = NULL; 734 } 735 736 if ((log_arp_wrong_iface == 1 && nifp == NULL) || 737 log_arp_wrong_iface == 2) { 738 hexncpy((u_char *)ar_sha(ah), ifp->if_addrlen, 739 hexstr[0], HEX_NCPYLEN(ifp->if_addrlen), ":"); 740 log(LOG_ERR, 741 "arp: %s is on %s " 742 "but got reply from %s on %s\n", 743 kinet_ntoa(isaddr, sbuf), 744 rt->rt_ifp->if_xname, hexstr[0], 745 ifp->if_xname); 746 } 747 if (nifp == NULL) 748 return 0; 749 750 /* 751 * nifp is our man! Replace rt_ifp and adjust 752 * the sdl. 753 */ 754 ifp = rt->rt_ifp = nifp; 755 if (sdl->sdl_type != ifp->if_type) { 756 sdl->sdl_type = ifp->if_type; 757 changed = 1; 758 } 759 if (sdl->sdl_index != ifp->if_index) { 760 sdl->sdl_index = ifp->if_index; 761 changed = 1; 762 } 763 } 764 if (sdl->sdl_alen && 765 bcmp(ar_sha(ah), LLADDR(sdl), sdl->sdl_alen)) { 766 changed = 1; 767 if (rt->rt_expire != 0) { 768 if (dologging && log_arp_movements) { 769 hexncpy((u_char *)LLADDR(sdl), ifp->if_addrlen, 770 hexstr[0], HEX_NCPYLEN(ifp->if_addrlen), ":"); 771 hexncpy((u_char *)ar_sha(ah), ifp->if_addrlen, 772 hexstr[1], HEX_NCPYLEN(ifp->if_addrlen), ":"); 773 log(LOG_INFO, 774 "arp: %s moved from %s to %s on %s\n", 775 kinet_ntoa(isaddr, sbuf), hexstr[0], hexstr[1], 776 ifp->if_xname); 777 } 778 } else { 779 if (dologging && log_arp_permanent_modify) { 780 hexncpy((u_char *)ar_sha(ah), ifp->if_addrlen, 781 hexstr[0], HEX_NCPYLEN(ifp->if_addrlen), ":"); 782 log(LOG_ERR, 783 "arp: %s attempts to modify " 784 "permanent entry for %s on %s\n", 785 hexstr[0], kinet_ntoa(isaddr, sbuf), ifp->if_xname); 786 } 787 return changed; 788 } 789 } 790 /* 791 * sanity check for the address length. 792 * XXX this does not work for protocols with variable address 793 * length. -is 794 */ 795 if (dologging && sdl->sdl_alen && sdl->sdl_alen != ah->ar_hln) { 796 hexncpy((u_char *)ar_sha(ah), ifp->if_addrlen, 797 hexstr[0], HEX_NCPYLEN(ifp->if_addrlen), ":"); 798 log(LOG_WARNING, 799 "arp from %s: new addr len %d, was %d", 800 hexstr[0], ah->ar_hln, sdl->sdl_alen); 801 } 802 if (ifp->if_addrlen != ah->ar_hln) { 803 if (dologging) { 804 hexncpy((u_char *)ar_sha(ah), ifp->if_addrlen, 805 hexstr[0], HEX_NCPYLEN(ifp->if_addrlen), ":"); 806 log(LOG_WARNING, 807 "arp from %s: addr len: new %d, i/f %d " 808 "(ignored)", hexstr[0], 809 ah->ar_hln, ifp->if_addrlen); 810 } 811 return changed; 812 } 813 memcpy(LLADDR(sdl), ar_sha(ah), sdl->sdl_alen = ah->ar_hln); 814 if (rt->rt_expire != 0) { 815 if (rt->rt_expire != time_uptime + arpt_keep && 816 rt->rt_expire != time_uptime + arpt_keep - 1) { 817 rt->rt_expire = time_uptime + arpt_keep; 818 changed = 1; 819 } 820 } 821 if (rt->rt_flags & RTF_REJECT) { 822 rt->rt_flags &= ~RTF_REJECT; 823 changed = 1; 824 } 825 if (la->la_asked != 0) { 826 la->la_asked = 0; 827 changed = 1; 828 } 829 if (la->la_preempt != arp_maxtries) { 830 la->la_preempt = arp_maxtries; 831 changed = 1; 832 } 833 834 /* 835 * This particular cpu might have been holding an mbuf 836 * pending ARP resolution. If so, transmit the mbuf now. 837 */ 838 if (la->la_hold != NULL) { 839 struct mbuf *m = la->la_hold; 840 841 la->la_hold = NULL; 842 m_adj(m, sizeof(struct ether_header)); 843 ifp->if_output(ifp, m, rt_key(rt), rt); 844 changed = 1; 845 } 846 } 847 return changed; 848 } 849 850 /* 851 * Called from arpintr() - this routine is run from a single cpu. 852 */ 853 static void 854 in_arpinput(struct mbuf *m) 855 { 856 struct arphdr *ah; 857 struct ifnet *ifp = m->m_pkthdr.rcvif; 858 struct ifaddr_container *ifac; 859 struct in_ifaddr_container *iac; 860 struct in_ifaddr *ia = NULL; 861 struct in_addr isaddr, itaddr, myaddr; 862 uint8_t *enaddr = NULL; 863 int req_len; 864 int changed; 865 char hexstr[64], sbuf[INET_ADDRSTRLEN]; 866 867 req_len = arphdr_len2(ifp->if_addrlen, sizeof(struct in_addr)); 868 if (m->m_len < req_len && (m = m_pullup(m, req_len)) == NULL) { 869 log(LOG_ERR, "in_arp: runt packet -- m_pullup failed\n"); 870 return; 871 } 872 873 ah = mtod(m, struct arphdr *); 874 memcpy(&isaddr, ar_spa(ah), sizeof isaddr); 875 memcpy(&itaddr, ar_tpa(ah), sizeof itaddr); 876 877 /* 878 * Check both target and sender IP addresses: 879 * 880 * If we receive the packet on the interface owning the address, 881 * then accept the address. 882 * 883 * For a bridge, we accept the address if the receive interface and 884 * the interface owning the address are on the same bridge, and 885 * use the bridge MAC as the is-at response. The bridge will be 886 * responsible for handling the packet. 887 * 888 * (0) Check target IP against CARP IPs 889 */ 890 #ifdef CARP 891 LIST_FOREACH(iac, INADDR_HASH(itaddr.s_addr), ia_hash) { 892 int is_match = 0, is_parent = 0; 893 894 ia = iac->ia; 895 896 /* Skip all ia's which don't match */ 897 if (itaddr.s_addr != ia->ia_addr.sin_addr.s_addr) 898 continue; 899 900 if (ia->ia_ifp->if_type != IFT_CARP) 901 continue; 902 903 if (carp_parent(ia->ia_ifp) == ifp) 904 is_parent = 1; 905 if (is_parent || ia->ia_ifp == ifp) 906 is_match = carp_iamatch(ia); 907 908 if (is_match) { 909 if (is_parent) { 910 /* 911 * The parent interface will also receive 912 * the ethernet broadcast packets, e.g. ARP 913 * REQUEST, so if we could find a CARP 914 * interface of the parent that could match 915 * the target IP address, we then drop the 916 * packets, which is delieverd to us through 917 * the parent interface. 918 */ 919 m_freem(m); 920 return; 921 } 922 goto match; 923 } 924 } 925 #endif /* CARP */ 926 927 /* 928 * (1) Check target IP against our local IPs 929 */ 930 LIST_FOREACH(iac, INADDR_HASH(itaddr.s_addr), ia_hash) { 931 ia = iac->ia; 932 933 /* Skip all ia's which don't match */ 934 if (itaddr.s_addr != ia->ia_addr.sin_addr.s_addr) 935 continue; 936 937 #ifdef CARP 938 /* CARP interfaces are checked in (0) */ 939 if (ia->ia_ifp->if_type == IFT_CARP) 940 continue; 941 #endif 942 943 if (ifp->if_bridge && ia->ia_ifp && 944 ifp->if_bridge == ia->ia_ifp->if_bridge) { 945 ifp = ether_bridge_interface(ifp); 946 goto match; 947 } 948 if (ia->ia_ifp && ia->ia_ifp->if_bridge && 949 ether_bridge_interface(ia->ia_ifp) == ifp) { 950 goto match; 951 } 952 if (ifp->if_bridge && ether_bridge_interface(ifp) == 953 ia->ia_ifp) { 954 goto match; 955 } 956 if (ia->ia_ifp == ifp) { 957 goto match; 958 } 959 } 960 961 /* 962 * (2) Check sender IP against our local IPs 963 */ 964 LIST_FOREACH(iac, INADDR_HASH(isaddr.s_addr), ia_hash) { 965 ia = iac->ia; 966 967 /* Skip all ia's which don't match */ 968 if (isaddr.s_addr != ia->ia_addr.sin_addr.s_addr) 969 continue; 970 971 if (ifp->if_bridge && ia->ia_ifp && 972 ifp->if_bridge == ia->ia_ifp->if_bridge) { 973 ifp = ether_bridge_interface(ifp); 974 goto match; 975 } 976 if (ia->ia_ifp && ia->ia_ifp->if_bridge && 977 ether_bridge_interface(ia->ia_ifp) == ifp) { 978 goto match; 979 } 980 if (ifp->if_bridge && ether_bridge_interface(ifp) == 981 ia->ia_ifp) { 982 goto match; 983 } 984 985 if (ia->ia_ifp == ifp) 986 goto match; 987 } 988 989 /* 990 * No match, use the first inet address on the receive interface 991 * as a dummy address for the rest of the function. 992 */ 993 TAILQ_FOREACH(ifac, &ifp->if_addrheads[mycpuid], ifa_link) { 994 struct ifaddr *ifa = ifac->ifa; 995 996 if (ifa->ifa_addr && ifa->ifa_addr->sa_family == AF_INET) { 997 ia = ifatoia(ifa); 998 goto match; 999 } 1000 } 1001 1002 /* 1003 * If we got here, we didn't find any suitable interface, 1004 * so drop the packet. 1005 */ 1006 m_freem(m); 1007 return; 1008 1009 match: 1010 if (!enaddr) 1011 enaddr = (uint8_t *)IF_LLADDR(ifp); 1012 myaddr = ia->ia_addr.sin_addr; 1013 if (!bcmp(ar_sha(ah), enaddr, ifp->if_addrlen)) { 1014 m_freem(m); /* it's from me, ignore it. */ 1015 return; 1016 } 1017 if (!bcmp(ar_sha(ah), ifp->if_broadcastaddr, ifp->if_addrlen)) { 1018 log(LOG_ERR, 1019 "arp: link address is broadcast for IP address %s!\n", 1020 kinet_ntoa(isaddr, sbuf)); 1021 m_freem(m); 1022 return; 1023 } 1024 if (isaddr.s_addr == myaddr.s_addr && myaddr.s_addr != 0) { 1025 hexncpy((u_char *)ar_sha(ah), ifp->if_addrlen, 1026 hexstr, HEX_NCPYLEN(ifp->if_addrlen), ":"); 1027 log(LOG_ERR, 1028 "arp: %s is using my IP address %s!\n", 1029 hexstr, kinet_ntoa(isaddr, sbuf)); 1030 itaddr = myaddr; 1031 goto reply; 1032 } 1033 if (ifp->if_flags & IFF_STATICARP) 1034 goto reply; 1035 1036 /* 1037 * When arp_restricted_match is true and the ARP response is not 1038 * specifically targetted to me, ignore it. Otherwise the entry 1039 * timeout may be updated for an old MAC. 1040 */ 1041 if (arp_restricted_match && itaddr.s_addr != myaddr.s_addr) { 1042 m_freem(m); 1043 return; 1044 } 1045 1046 /* 1047 * Update all CPU's routing tables with this ARP packet. 1048 * 1049 * However, we only need to generate rtmsg on CPU0. 1050 */ 1051 ASSERT_IN_NETISR(0); 1052 changed = arp_update_oncpu(m, isaddr.s_addr, 1053 itaddr.s_addr == myaddr.s_addr, 1054 RTL_REPORTMSG, TRUE); 1055 1056 if (ncpus > 1 && changed) { 1057 struct netmsg_inarp *msg = &m->m_hdr.mh_arpmsg; 1058 1059 netmsg_init(&msg->base, NULL, &netisr_apanic_rport, 1060 0, arp_update_msghandler); 1061 msg->m = m; 1062 msg->saddr = isaddr.s_addr; 1063 msg->taddr = itaddr.s_addr; 1064 msg->myaddr = myaddr.s_addr; 1065 lwkt_sendmsg(netisr_cpuport(1), &msg->base.lmsg); 1066 } else { 1067 goto reply; 1068 } 1069 1070 /* 1071 * Just return here; after all CPUs's routing tables are 1072 * properly updated by this ARP packet, an ARP reply will 1073 * be generated if appropriate. 1074 */ 1075 return; 1076 reply: 1077 in_arpreply(m, itaddr.s_addr, myaddr.s_addr); 1078 } 1079 1080 static void 1081 arp_reply_msghandler(netmsg_t msg) 1082 { 1083 struct netmsg_inarp *rmsg = (struct netmsg_inarp *)msg; 1084 1085 in_arpreply(rmsg->m, rmsg->taddr, rmsg->myaddr); 1086 /* Don't reply this netmsg; netmsg_inarp is embedded in mbuf */ 1087 } 1088 1089 static void 1090 arp_update_msghandler(netmsg_t msg) 1091 { 1092 struct netmsg_inarp *rmsg = (struct netmsg_inarp *)msg; 1093 int nextcpu; 1094 1095 /* 1096 * This message handler will be called on all of the APs; 1097 * no need to generate rtmsg on them. 1098 */ 1099 KASSERT(mycpuid > 0, ("arp update msg on cpu%d", mycpuid)); 1100 arp_update_oncpu(rmsg->m, rmsg->saddr, 1101 rmsg->taddr == rmsg->myaddr, 1102 RTL_DONTREPORT, FALSE); 1103 1104 nextcpu = mycpuid + 1; 1105 if (nextcpu < ncpus) { 1106 lwkt_forwardmsg(netisr_cpuport(nextcpu), &rmsg->base.lmsg); 1107 } else { 1108 struct mbuf *m = rmsg->m; 1109 in_addr_t saddr = rmsg->saddr; 1110 in_addr_t taddr = rmsg->taddr; 1111 in_addr_t myaddr = rmsg->myaddr; 1112 1113 /* 1114 * Dispatch this mbuf to netisr0 to perform ARP reply, 1115 * if appropriate. 1116 * NOTE: netmsg_inarp is embedded in this mbuf. 1117 */ 1118 netmsg_init(&rmsg->base, NULL, &netisr_apanic_rport, 1119 0, arp_reply_msghandler); 1120 rmsg->m = m; 1121 rmsg->saddr = saddr; 1122 rmsg->taddr = taddr; 1123 rmsg->myaddr = myaddr; 1124 lwkt_sendmsg(netisr_cpuport(0), &rmsg->base.lmsg); 1125 } 1126 } 1127 1128 static void 1129 in_arpreply(struct mbuf *m, in_addr_t taddr, in_addr_t myaddr) 1130 { 1131 struct ifnet *ifp = m->m_pkthdr.rcvif; 1132 const uint8_t *enaddr; 1133 struct arphdr *ah; 1134 struct sockaddr sa; 1135 struct ether_header *eh; 1136 1137 ah = mtod(m, struct arphdr *); 1138 if (ntohs(ah->ar_op) != ARPOP_REQUEST) { 1139 m_freem(m); 1140 return; 1141 } 1142 1143 enaddr = (const uint8_t *)IF_LLADDR(ifp); 1144 if (taddr == myaddr) { 1145 /* I am the target */ 1146 memcpy(ar_tha(ah), ar_sha(ah), ah->ar_hln); 1147 memcpy(ar_sha(ah), enaddr, ah->ar_hln); 1148 } else { 1149 struct llinfo_arp *la; 1150 struct rtentry *rt; 1151 1152 la = arplookup(taddr, FALSE, RTL_DONTREPORT, SIN_PROXY); 1153 if (la == NULL) { 1154 struct sockaddr_in sin; 1155 #ifdef DEBUG_PROXY 1156 char tbuf[INET_ADDRSTRLEN]; 1157 #endif 1158 1159 if (!arp_proxyall) { 1160 m_freem(m); 1161 return; 1162 } 1163 1164 bzero(&sin, sizeof sin); 1165 sin.sin_family = AF_INET; 1166 sin.sin_len = sizeof sin; 1167 sin.sin_addr.s_addr = taddr; 1168 1169 rt = rtpurelookup((struct sockaddr *)&sin); 1170 if (rt == NULL) { 1171 m_freem(m); 1172 return; 1173 } 1174 --rt->rt_refcnt; 1175 /* 1176 * Don't send proxies for nodes on the same interface 1177 * as this one came out of, or we'll get into a fight 1178 * over who claims what Ether address. 1179 */ 1180 if (rt->rt_ifp == ifp) { 1181 m_freem(m); 1182 return; 1183 } 1184 memcpy(ar_tha(ah), ar_sha(ah), ah->ar_hln); 1185 memcpy(ar_sha(ah), enaddr, ah->ar_hln); 1186 #ifdef DEBUG_PROXY 1187 kprintf("arp: proxying for %s\n", 1188 kinet_ntoa(itaddr, tbuf)); 1189 #endif 1190 } else { 1191 struct sockaddr_dl *sdl; 1192 1193 rt = la->la_rt; 1194 memcpy(ar_tha(ah), ar_sha(ah), ah->ar_hln); 1195 sdl = SDL(rt->rt_gateway); 1196 memcpy(ar_sha(ah), LLADDR(sdl), ah->ar_hln); 1197 } 1198 } 1199 1200 memcpy(ar_tpa(ah), ar_spa(ah), ah->ar_pln); 1201 memcpy(ar_spa(ah), &taddr, ah->ar_pln); 1202 ah->ar_op = htons(ARPOP_REPLY); 1203 ah->ar_pro = htons(ETHERTYPE_IP); /* let's be sure! */ 1204 switch (ifp->if_type) { 1205 case IFT_ETHER: 1206 /* 1207 * May not be correct for types not explictly 1208 * listed, but it is our best guess. 1209 */ 1210 default: 1211 eh = (struct ether_header *)sa.sa_data; 1212 memcpy(eh->ether_dhost, ar_tha(ah), sizeof eh->ether_dhost); 1213 eh->ether_type = htons(ETHERTYPE_ARP); 1214 break; 1215 } 1216 sa.sa_family = AF_UNSPEC; 1217 sa.sa_len = sizeof sa; 1218 ifp->if_output(ifp, m, &sa, NULL); 1219 } 1220 1221 #endif /* INET */ 1222 1223 /* 1224 * Free an arp entry. If the arp entry is actively referenced or represents 1225 * a static entry we only clear it back to an unresolved state, otherwise 1226 * we destroy the entry entirely. 1227 * 1228 * Note that static entries are created when route add ... -interface is used 1229 * to create an interface route to a (direct) destination. 1230 */ 1231 static void 1232 arptfree(struct llinfo_arp *la) 1233 { 1234 struct rtentry *rt = la->la_rt; 1235 struct sockaddr_dl *sdl; 1236 1237 if (rt == NULL) 1238 panic("arptfree"); 1239 sdl = SDL(rt->rt_gateway); 1240 if (sdl != NULL && 1241 ((rt->rt_refcnt > 0 && sdl->sdl_family == AF_LINK) || 1242 (rt->rt_flags & RTF_STATIC))) { 1243 sdl->sdl_alen = 0; 1244 la->la_preempt = la->la_asked = 0; 1245 rt->rt_flags &= ~RTF_REJECT; 1246 return; 1247 } 1248 rtrequest(RTM_DELETE, rt_key(rt), NULL, rt_mask(rt), 0, NULL); 1249 } 1250 1251 /* 1252 * Lookup or enter a new address in arptab. 1253 */ 1254 static struct llinfo_arp * 1255 arplookup(in_addr_t addr, boolean_t create, boolean_t generate_report, 1256 boolean_t proxy) 1257 { 1258 struct rtentry *rt; 1259 struct sockaddr_inarp sin = { sizeof sin, AF_INET }; 1260 const char *why = NULL; 1261 1262 sin.sin_addr.s_addr = addr; 1263 sin.sin_other = proxy ? SIN_PROXY : 0; 1264 if (create) { 1265 rt = _rtlookup((struct sockaddr *)&sin, 1266 generate_report, RTL_DOCLONE); 1267 } else { 1268 rt = rtpurelookup((struct sockaddr *)&sin); 1269 } 1270 if (rt == NULL) 1271 return (NULL); 1272 rt->rt_refcnt--; 1273 1274 if (rt->rt_flags & RTF_GATEWAY) 1275 why = "host is not on local network"; 1276 else if (!(rt->rt_flags & RTF_LLINFO)) 1277 why = "could not allocate llinfo"; 1278 else if (rt->rt_gateway->sa_family != AF_LINK) 1279 why = "gateway route is not ours"; 1280 1281 if (why) { 1282 if (create && log_arp_creation_failure) { 1283 char abuf[INET_ADDRSTRLEN]; 1284 1285 log(LOG_DEBUG, "arplookup %s failed: %s\n", 1286 kinet_ntoa(sin.sin_addr, abuf), why); 1287 } 1288 if (rt->rt_refcnt <= 0 && (rt->rt_flags & RTF_WASCLONED)) { 1289 /* No references to this route. Purge it. */ 1290 rtrequest(RTM_DELETE, rt_key(rt), rt->rt_gateway, 1291 rt_mask(rt), rt->rt_flags, NULL); 1292 } 1293 return (NULL); 1294 } 1295 return (rt->rt_llinfo); 1296 } 1297 1298 void 1299 arp_ifinit(struct ifnet *ifp, struct ifaddr *ifa) 1300 { 1301 ifa->ifa_rtrequest = arp_rtrequest; 1302 ifa->ifa_flags |= RTF_CLONING; 1303 } 1304 1305 void 1306 arp_gratuitous(struct ifnet *ifp, struct ifaddr *ifa) 1307 { 1308 if (IA_SIN(ifa)->sin_addr.s_addr != INADDR_ANY) { 1309 arprequest_async(ifp, &IA_SIN(ifa)->sin_addr, 1310 &IA_SIN(ifa)->sin_addr, NULL); 1311 } 1312 } 1313 1314 static void 1315 arp_ifaddr(void *arg __unused, struct ifnet *ifp, 1316 enum ifaddr_event event, struct ifaddr *ifa) 1317 { 1318 if (ifa->ifa_rtrequest != arp_rtrequest) /* XXX need a generic way */ 1319 return; 1320 if (ifa->ifa_addr->sa_family != AF_INET) 1321 return; 1322 if (event == IFADDR_EVENT_DELETE) 1323 return; 1324 1325 /* 1326 * - CARP interfaces will take care of gratuitous ARP themselves. 1327 * - If we are the CARP interface's parent, don't send gratuitous 1328 * ARP to avoid unnecessary confusion. 1329 */ 1330 #ifdef CARP 1331 if (ifp->if_type != IFT_CARP && ifp->if_carp == NULL) 1332 #endif 1333 { 1334 arp_gratuitous(ifp, ifa); 1335 } 1336 } 1337 1338 static void 1339 arp_init(void) 1340 { 1341 int cpu; 1342 1343 for (cpu = 0; cpu < ncpus2; cpu++) 1344 LIST_INIT(&llinfo_arp_list[cpu]); 1345 1346 netisr_register(NETISR_ARP, arpintr, NULL); 1347 1348 EVENTHANDLER_REGISTER(ifaddr_event, arp_ifaddr, NULL, 1349 EVENTHANDLER_PRI_LAST); 1350 } 1351 1352 SYSINIT(arp, SI_SUB_PROTO_DOMAIN, SI_ORDER_ANY, arp_init, 0); 1353