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