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