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