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