1 /*- 2 * Copyright (c) 1982, 1986, 1988, 1993 3 * The Regents of the University of California. All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 4. Neither the name of the University nor the names of its contributors 14 * may be used to endorse or promote products derived from this software 15 * without specific prior written permission. 16 * 17 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 20 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 27 * SUCH DAMAGE. 28 * 29 * @(#)if_ether.c 8.1 (Berkeley) 6/10/93 30 */ 31 32 /* 33 * Ethernet address resolution protocol. 34 * TODO: 35 * add "inuse/lock" bit (or ref. count) along with valid bit 36 */ 37 38 #include <sys/cdefs.h> 39 __FBSDID("$FreeBSD$"); 40 41 #include "opt_inet.h" 42 #include "opt_route.h" 43 #include "opt_mac.h" 44 #include "opt_carp.h" 45 46 #include <sys/param.h> 47 #include <sys/kernel.h> 48 #include <sys/queue.h> 49 #include <sys/sysctl.h> 50 #include <sys/systm.h> 51 #include <sys/mbuf.h> 52 #include <sys/malloc.h> 53 #include <sys/proc.h> 54 #include <sys/socket.h> 55 #include <sys/syslog.h> 56 #include <sys/vimage.h> 57 58 #include <net/if.h> 59 #include <net/if_dl.h> 60 #include <net/if_types.h> 61 #include <net/route.h> 62 #include <net/netisr.h> 63 #include <net/if_llc.h> 64 #include <net/ethernet.h> 65 #include <net/vnet.h> 66 67 #include <netinet/in.h> 68 #include <netinet/in_var.h> 69 #include <net/if_llatbl.h> 70 #include <netinet/if_ether.h> 71 #include <netinet/vinet.h> 72 73 #include <net/if_arc.h> 74 #include <net/iso88025.h> 75 76 #ifdef DEV_CARP 77 #include <netinet/ip_carp.h> 78 #endif 79 80 #include <security/mac/mac_framework.h> 81 82 #define SIN(s) ((struct sockaddr_in *)s) 83 #define SDL(s) ((struct sockaddr_dl *)s) 84 85 SYSCTL_DECL(_net_link_ether); 86 SYSCTL_NODE(_net_link_ether, PF_INET, inet, CTLFLAG_RW, 0, ""); 87 88 /* timer values */ 89 #ifdef VIMAGE_GLOBALS 90 static int arpt_keep; /* once resolved, good for 20 more minutes */ 91 static int arp_maxtries; 92 int useloopback; /* use loopback interface for local traffic */ 93 static int arp_proxyall; 94 #endif 95 96 SYSCTL_V_INT(V_NET, vnet_inet, _net_link_ether_inet, OID_AUTO, max_age, 97 CTLFLAG_RW, arpt_keep, 0, "ARP entry lifetime in seconds"); 98 99 SYSCTL_V_INT(V_NET, vnet_inet, _net_link_ether_inet, OID_AUTO, maxtries, 100 CTLFLAG_RW, arp_maxtries, 0, 101 "ARP resolution attempts before returning error"); 102 SYSCTL_V_INT(V_NET, vnet_inet, _net_link_ether_inet, OID_AUTO, useloopback, 103 CTLFLAG_RW, useloopback, 0, 104 "Use the loopback interface for local traffic"); 105 SYSCTL_V_INT(V_NET, vnet_inet, _net_link_ether_inet, OID_AUTO, proxyall, 106 CTLFLAG_RW, arp_proxyall, 0, 107 "Enable proxy ARP for all suitable requests"); 108 109 static void arp_init(void); 110 static int arp_iattach(const void *); 111 void arprequest(struct ifnet *, 112 struct in_addr *, struct in_addr *, u_char *); 113 static void arpintr(struct mbuf *); 114 static void arptimer(void *); 115 #ifdef INET 116 static void in_arpinput(struct mbuf *); 117 #endif 118 119 static const struct netisr_handler arp_nh = { 120 .nh_name = "arp", 121 .nh_handler = arpintr, 122 .nh_proto = NETISR_ARP, 123 .nh_policy = NETISR_POLICY_SOURCE, 124 }; 125 126 #ifndef VIMAGE_GLOBALS 127 static const vnet_modinfo_t vnet_arp_modinfo = { 128 .vmi_id = VNET_MOD_ARP, 129 .vmi_name = "arp", 130 .vmi_dependson = VNET_MOD_INET, 131 .vmi_iattach = arp_iattach 132 }; 133 #endif /* !VIMAGE_GLOBALS */ 134 135 #ifdef AF_INET 136 void arp_ifscrub(struct ifnet *ifp, uint32_t addr); 137 138 /* 139 * called by in_ifscrub to remove entry from the table when 140 * the interface goes away 141 */ 142 void 143 arp_ifscrub(struct ifnet *ifp, uint32_t addr) 144 { 145 struct sockaddr_in addr4; 146 147 bzero((void *)&addr4, sizeof(addr4)); 148 addr4.sin_len = sizeof(addr4); 149 addr4.sin_family = AF_INET; 150 addr4.sin_addr.s_addr = addr; 151 CURVNET_SET(ifp->if_vnet); 152 IF_AFDATA_LOCK(ifp); 153 lla_lookup(LLTABLE(ifp), (LLE_DELETE | LLE_IFADDR), 154 (struct sockaddr *)&addr4); 155 IF_AFDATA_UNLOCK(ifp); 156 CURVNET_RESTORE(); 157 } 158 #endif 159 160 /* 161 * Timeout routine. Age arp_tab entries periodically. 162 */ 163 static void 164 arptimer(void *arg) 165 { 166 struct ifnet *ifp; 167 struct llentry *lle = (struct llentry *)arg; 168 169 if (lle == NULL) { 170 panic("%s: NULL entry!\n", __func__); 171 return; 172 } 173 ifp = lle->lle_tbl->llt_ifp; 174 IF_AFDATA_LOCK(ifp); 175 LLE_WLOCK(lle); 176 if (((lle->la_flags & LLE_DELETED) 177 || (time_second >= lle->la_expire)) 178 && (!callout_pending(&lle->la_timer) && 179 callout_active(&lle->la_timer))) 180 (void) llentry_free(lle); 181 else { 182 /* 183 * Still valid, just drop our reference 184 */ 185 LLE_FREE_LOCKED(lle); 186 } 187 IF_AFDATA_UNLOCK(ifp); 188 } 189 190 /* 191 * Broadcast an ARP request. Caller specifies: 192 * - arp header source ip address 193 * - arp header target ip address 194 * - arp header source ethernet address 195 */ 196 void 197 arprequest(struct ifnet *ifp, struct in_addr *sip, struct in_addr *tip, 198 u_char *enaddr) 199 { 200 struct mbuf *m; 201 struct arphdr *ah; 202 struct sockaddr sa; 203 204 if (sip == NULL) { 205 /* XXX don't believe this can happen (or explain why) */ 206 /* 207 * The caller did not supply a source address, try to find 208 * a compatible one among those assigned to this interface. 209 */ 210 struct ifaddr *ifa; 211 212 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) { 213 if (!ifa->ifa_addr || 214 ifa->ifa_addr->sa_family != AF_INET) 215 continue; 216 sip = &SIN(ifa->ifa_addr)->sin_addr; 217 if (0 == ((sip->s_addr ^ tip->s_addr) & 218 SIN(ifa->ifa_netmask)->sin_addr.s_addr) ) 219 break; /* found it. */ 220 } 221 if (sip == NULL) { 222 printf("%s: cannot find matching address\n", __func__); 223 return; 224 } 225 } 226 227 if ((m = m_gethdr(M_DONTWAIT, MT_DATA)) == NULL) 228 return; 229 m->m_len = sizeof(*ah) + 2*sizeof(struct in_addr) + 230 2*ifp->if_data.ifi_addrlen; 231 m->m_pkthdr.len = m->m_len; 232 MH_ALIGN(m, m->m_len); 233 ah = mtod(m, struct arphdr *); 234 bzero((caddr_t)ah, m->m_len); 235 #ifdef MAC 236 mac_netinet_arp_send(ifp, m); 237 #endif 238 ah->ar_pro = htons(ETHERTYPE_IP); 239 ah->ar_hln = ifp->if_addrlen; /* hardware address length */ 240 ah->ar_pln = sizeof(struct in_addr); /* protocol address length */ 241 ah->ar_op = htons(ARPOP_REQUEST); 242 bcopy((caddr_t)enaddr, (caddr_t)ar_sha(ah), ah->ar_hln); 243 bcopy((caddr_t)sip, (caddr_t)ar_spa(ah), ah->ar_pln); 244 bcopy((caddr_t)tip, (caddr_t)ar_tpa(ah), ah->ar_pln); 245 sa.sa_family = AF_ARP; 246 sa.sa_len = 2; 247 m->m_flags |= M_BCAST; 248 (*ifp->if_output)(ifp, m, &sa, NULL); 249 } 250 251 /* 252 * Resolve an IP address into an ethernet address. 253 * On input: 254 * ifp is the interface we use 255 * rt0 is the route to the final destination (possibly useless) 256 * m is the mbuf. May be NULL if we don't have a packet. 257 * dst is the next hop, 258 * desten is where we want the address. 259 * 260 * On success, desten is filled in and the function returns 0; 261 * If the packet must be held pending resolution, we return EWOULDBLOCK 262 * On other errors, we return the corresponding error code. 263 * Note that m_freem() handles NULL. 264 */ 265 int 266 arpresolve(struct ifnet *ifp, struct rtentry *rt0, struct mbuf *m, 267 struct sockaddr *dst, u_char *desten, struct llentry **lle) 268 { 269 INIT_VNET_INET(ifp->if_vnet); 270 struct llentry *la = 0; 271 u_int flags = 0; 272 int error, renew; 273 274 *lle = NULL; 275 if (m != NULL) { 276 if (m->m_flags & M_BCAST) { 277 /* broadcast */ 278 (void)memcpy(desten, 279 ifp->if_broadcastaddr, ifp->if_addrlen); 280 return (0); 281 } 282 if (m->m_flags & M_MCAST && ifp->if_type != IFT_ARCNET) { 283 /* multicast */ 284 ETHER_MAP_IP_MULTICAST(&SIN(dst)->sin_addr, desten); 285 return (0); 286 } 287 } 288 /* XXXXX 289 */ 290 retry: 291 IF_AFDATA_RLOCK(ifp); 292 la = lla_lookup(LLTABLE(ifp), flags, dst); 293 IF_AFDATA_RUNLOCK(ifp); 294 if ((la == NULL) && ((flags & LLE_EXCLUSIVE) == 0) 295 && ((ifp->if_flags & (IFF_NOARP | IFF_STATICARP)) == 0)) { 296 flags |= (LLE_CREATE | LLE_EXCLUSIVE); 297 IF_AFDATA_WLOCK(ifp); 298 la = lla_lookup(LLTABLE(ifp), flags, dst); 299 IF_AFDATA_WUNLOCK(ifp); 300 } 301 if (la == NULL) { 302 if (flags & LLE_CREATE) 303 log(LOG_DEBUG, 304 "arpresolve: can't allocate llinfo for %s\n", 305 inet_ntoa(SIN(dst)->sin_addr)); 306 m_freem(m); 307 return (EINVAL); 308 } 309 310 if ((la->la_flags & LLE_VALID) && 311 ((la->la_flags & LLE_STATIC) || la->la_expire > time_uptime)) { 312 bcopy(&la->ll_addr, desten, ifp->if_addrlen); 313 /* 314 * If entry has an expiry time and it is approaching, 315 * see if we need to send an ARP request within this 316 * arpt_down interval. 317 */ 318 if (!(la->la_flags & LLE_STATIC) && 319 time_uptime + la->la_preempt > la->la_expire) { 320 arprequest(ifp, NULL, 321 &SIN(dst)->sin_addr, IF_LLADDR(ifp)); 322 323 la->la_preempt--; 324 } 325 326 *lle = la; 327 error = 0; 328 goto done; 329 } 330 331 if (la->la_flags & LLE_STATIC) { /* should not happen! */ 332 log(LOG_DEBUG, "arpresolve: ouch, empty static llinfo for %s\n", 333 inet_ntoa(SIN(dst)->sin_addr)); 334 m_freem(m); 335 error = EINVAL; 336 goto done; 337 } 338 339 renew = (la->la_asked == 0 || la->la_expire != time_uptime); 340 if ((renew || m != NULL) && (flags & LLE_EXCLUSIVE) == 0) { 341 flags |= LLE_EXCLUSIVE; 342 LLE_RUNLOCK(la); 343 goto retry; 344 } 345 /* 346 * There is an arptab entry, but no ethernet address 347 * response yet. Replace the held mbuf with this 348 * latest one. 349 */ 350 if (m != NULL) { 351 if (la->la_hold != NULL) 352 m_freem(la->la_hold); 353 la->la_hold = m; 354 if (renew == 0 && (flags & LLE_EXCLUSIVE)) { 355 flags &= ~LLE_EXCLUSIVE; 356 LLE_DOWNGRADE(la); 357 } 358 359 } 360 /* 361 * Return EWOULDBLOCK if we have tried less than arp_maxtries. It 362 * will be masked by ether_output(). Return EHOSTDOWN/EHOSTUNREACH 363 * if we have already sent arp_maxtries ARP requests. Retransmit the 364 * ARP request, but not faster than one request per second. 365 */ 366 if (la->la_asked < V_arp_maxtries) 367 error = EWOULDBLOCK; /* First request. */ 368 else 369 error = 370 (rt0->rt_flags & RTF_GATEWAY) ? EHOSTDOWN : EHOSTUNREACH; 371 372 if (renew) { 373 LLE_ADDREF(la); 374 la->la_expire = time_uptime; 375 callout_reset(&la->la_timer, hz, arptimer, la); 376 la->la_asked++; 377 LLE_WUNLOCK(la); 378 arprequest(ifp, NULL, &SIN(dst)->sin_addr, 379 IF_LLADDR(ifp)); 380 return (error); 381 } 382 done: 383 if (flags & LLE_EXCLUSIVE) 384 LLE_WUNLOCK(la); 385 else 386 LLE_RUNLOCK(la); 387 return (error); 388 } 389 390 /* 391 * Common length and type checks are done here, 392 * then the protocol-specific routine is called. 393 */ 394 static void 395 arpintr(struct mbuf *m) 396 { 397 struct arphdr *ar; 398 399 if (m->m_len < sizeof(struct arphdr) && 400 ((m = m_pullup(m, sizeof(struct arphdr))) == NULL)) { 401 log(LOG_ERR, "arp: runt packet -- m_pullup failed\n"); 402 return; 403 } 404 ar = mtod(m, struct arphdr *); 405 406 if (ntohs(ar->ar_hrd) != ARPHRD_ETHER && 407 ntohs(ar->ar_hrd) != ARPHRD_IEEE802 && 408 ntohs(ar->ar_hrd) != ARPHRD_ARCNET && 409 ntohs(ar->ar_hrd) != ARPHRD_IEEE1394) { 410 log(LOG_ERR, "arp: unknown hardware address format (0x%2D)\n", 411 (unsigned char *)&ar->ar_hrd, ""); 412 m_freem(m); 413 return; 414 } 415 416 if (m->m_len < arphdr_len(ar)) { 417 if ((m = m_pullup(m, arphdr_len(ar))) == NULL) { 418 log(LOG_ERR, "arp: runt packet\n"); 419 m_freem(m); 420 return; 421 } 422 ar = mtod(m, struct arphdr *); 423 } 424 425 switch (ntohs(ar->ar_pro)) { 426 #ifdef INET 427 case ETHERTYPE_IP: 428 in_arpinput(m); 429 return; 430 #endif 431 } 432 m_freem(m); 433 } 434 435 #ifdef INET 436 /* 437 * ARP for Internet protocols on 10 Mb/s Ethernet. 438 * Algorithm is that given in RFC 826. 439 * In addition, a sanity check is performed on the sender 440 * protocol address, to catch impersonators. 441 * We no longer handle negotiations for use of trailer protocol: 442 * Formerly, ARP replied for protocol type ETHERTYPE_TRAIL sent 443 * along with IP replies if we wanted trailers sent to us, 444 * and also sent them in response to IP replies. 445 * This allowed either end to announce the desire to receive 446 * trailer packets. 447 * We no longer reply to requests for ETHERTYPE_TRAIL protocol either, 448 * but formerly didn't normally send requests. 449 */ 450 static int log_arp_wrong_iface = 1; 451 static int log_arp_movements = 1; 452 static int log_arp_permanent_modify = 1; 453 454 SYSCTL_INT(_net_link_ether_inet, OID_AUTO, log_arp_wrong_iface, CTLFLAG_RW, 455 &log_arp_wrong_iface, 0, 456 "log arp packets arriving on the wrong interface"); 457 SYSCTL_INT(_net_link_ether_inet, OID_AUTO, log_arp_movements, CTLFLAG_RW, 458 &log_arp_movements, 0, 459 "log arp replies from MACs different than the one in the cache"); 460 SYSCTL_INT(_net_link_ether_inet, OID_AUTO, log_arp_permanent_modify, CTLFLAG_RW, 461 &log_arp_permanent_modify, 0, 462 "log arp replies from MACs different than the one in the permanent arp entry"); 463 464 465 static void 466 in_arpinput(struct mbuf *m) 467 { 468 struct arphdr *ah; 469 struct ifnet *ifp = m->m_pkthdr.rcvif; 470 struct llentry *la = NULL; 471 struct rtentry *rt; 472 struct ifaddr *ifa; 473 struct in_ifaddr *ia; 474 struct sockaddr sa; 475 struct in_addr isaddr, itaddr, myaddr; 476 u_int8_t *enaddr = NULL; 477 int op, flags; 478 struct mbuf *m0; 479 int req_len; 480 int bridged = 0, is_bridge = 0; 481 #ifdef DEV_CARP 482 int carp_match = 0; 483 #endif 484 struct sockaddr_in sin; 485 sin.sin_len = sizeof(struct sockaddr_in); 486 sin.sin_family = AF_INET; 487 sin.sin_addr.s_addr = 0; 488 INIT_VNET_INET(ifp->if_vnet); 489 490 if (ifp->if_bridge) 491 bridged = 1; 492 if (ifp->if_type == IFT_BRIDGE) 493 is_bridge = 1; 494 495 req_len = arphdr_len2(ifp->if_addrlen, sizeof(struct in_addr)); 496 if (m->m_len < req_len && (m = m_pullup(m, req_len)) == NULL) { 497 log(LOG_ERR, "in_arp: runt packet -- m_pullup failed\n"); 498 return; 499 } 500 501 ah = mtod(m, struct arphdr *); 502 op = ntohs(ah->ar_op); 503 (void)memcpy(&isaddr, ar_spa(ah), sizeof (isaddr)); 504 (void)memcpy(&itaddr, ar_tpa(ah), sizeof (itaddr)); 505 506 /* 507 * For a bridge, we want to check the address irrespective 508 * of the receive interface. (This will change slightly 509 * when we have clusters of interfaces). 510 * If the interface does not match, but the recieving interface 511 * is part of carp, we call carp_iamatch to see if this is a 512 * request for the virtual host ip. 513 * XXX: This is really ugly! 514 */ 515 LIST_FOREACH(ia, INADDR_HASH(itaddr.s_addr), ia_hash) { 516 if (((bridged && ia->ia_ifp->if_bridge != NULL) || 517 ia->ia_ifp == ifp) && 518 itaddr.s_addr == ia->ia_addr.sin_addr.s_addr) 519 goto match; 520 #ifdef DEV_CARP 521 if (ifp->if_carp != NULL && 522 carp_iamatch(ifp->if_carp, ia, &isaddr, &enaddr) && 523 itaddr.s_addr == ia->ia_addr.sin_addr.s_addr) { 524 carp_match = 1; 525 goto match; 526 } 527 #endif 528 } 529 LIST_FOREACH(ia, INADDR_HASH(isaddr.s_addr), ia_hash) 530 if (((bridged && ia->ia_ifp->if_bridge != NULL) || 531 ia->ia_ifp == ifp) && 532 isaddr.s_addr == ia->ia_addr.sin_addr.s_addr) 533 goto match; 534 535 #define BDG_MEMBER_MATCHES_ARP(addr, ifp, ia) \ 536 (ia->ia_ifp->if_bridge == ifp->if_softc && \ 537 !bcmp(IF_LLADDR(ia->ia_ifp), IF_LLADDR(ifp), ifp->if_addrlen) && \ 538 addr == ia->ia_addr.sin_addr.s_addr) 539 /* 540 * Check the case when bridge shares its MAC address with 541 * some of its children, so packets are claimed by bridge 542 * itself (bridge_input() does it first), but they are really 543 * meant to be destined to the bridge member. 544 */ 545 if (is_bridge) { 546 LIST_FOREACH(ia, INADDR_HASH(itaddr.s_addr), ia_hash) { 547 if (BDG_MEMBER_MATCHES_ARP(itaddr.s_addr, ifp, ia)) { 548 ifp = ia->ia_ifp; 549 goto match; 550 } 551 } 552 } 553 #undef BDG_MEMBER_MATCHES_ARP 554 555 /* 556 * No match, use the first inet address on the receive interface 557 * as a dummy address for the rest of the function. 558 */ 559 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) 560 if (ifa->ifa_addr->sa_family == AF_INET) { 561 ia = ifatoia(ifa); 562 goto match; 563 } 564 /* 565 * If bridging, fall back to using any inet address. 566 */ 567 if (!bridged || (ia = TAILQ_FIRST(&V_in_ifaddrhead)) == NULL) 568 goto drop; 569 match: 570 if (!enaddr) 571 enaddr = (u_int8_t *)IF_LLADDR(ifp); 572 myaddr = ia->ia_addr.sin_addr; 573 if (!bcmp(ar_sha(ah), enaddr, ifp->if_addrlen)) 574 goto drop; /* it's from me, ignore it. */ 575 if (!bcmp(ar_sha(ah), ifp->if_broadcastaddr, ifp->if_addrlen)) { 576 log(LOG_ERR, 577 "arp: link address is broadcast for IP address %s!\n", 578 inet_ntoa(isaddr)); 579 goto drop; 580 } 581 /* 582 * Warn if another host is using the same IP address, but only if the 583 * IP address isn't 0.0.0.0, which is used for DHCP only, in which 584 * case we suppress the warning to avoid false positive complaints of 585 * potential misconfiguration. 586 */ 587 if (!bridged && isaddr.s_addr == myaddr.s_addr && myaddr.s_addr != 0) { 588 log(LOG_ERR, 589 "arp: %*D is using my IP address %s on %s!\n", 590 ifp->if_addrlen, (u_char *)ar_sha(ah), ":", 591 inet_ntoa(isaddr), ifp->if_xname); 592 itaddr = myaddr; 593 goto reply; 594 } 595 if (ifp->if_flags & IFF_STATICARP) 596 goto reply; 597 598 bzero(&sin, sizeof(sin)); 599 sin.sin_len = sizeof(struct sockaddr_in); 600 sin.sin_family = AF_INET; 601 sin.sin_addr = isaddr; 602 flags = (itaddr.s_addr == myaddr.s_addr) ? LLE_CREATE : 0; 603 flags |= LLE_EXCLUSIVE; 604 IF_AFDATA_LOCK(ifp); 605 la = lla_lookup(LLTABLE(ifp), flags, (struct sockaddr *)&sin); 606 IF_AFDATA_UNLOCK(ifp); 607 if (la != NULL) { 608 /* the following is not an error when doing bridging */ 609 if (!bridged && la->lle_tbl->llt_ifp != ifp 610 #ifdef DEV_CARP 611 && (ifp->if_type != IFT_CARP || !carp_match) 612 #endif 613 ) { 614 if (log_arp_wrong_iface) 615 log(LOG_ERR, "arp: %s is on %s " 616 "but got reply from %*D on %s\n", 617 inet_ntoa(isaddr), 618 la->lle_tbl->llt_ifp->if_xname, 619 ifp->if_addrlen, (u_char *)ar_sha(ah), ":", 620 ifp->if_xname); 621 goto reply; 622 } 623 if ((la->la_flags & LLE_VALID) && 624 bcmp(ar_sha(ah), &la->ll_addr, ifp->if_addrlen)) { 625 if (la->la_flags & LLE_STATIC) { 626 log(LOG_ERR, 627 "arp: %*D attempts to modify permanent " 628 "entry for %s on %s\n", 629 ifp->if_addrlen, (u_char *)ar_sha(ah), ":", 630 inet_ntoa(isaddr), ifp->if_xname); 631 goto reply; 632 } 633 if (log_arp_movements) { 634 log(LOG_INFO, "arp: %s moved from %*D " 635 "to %*D on %s\n", 636 inet_ntoa(isaddr), 637 ifp->if_addrlen, 638 (u_char *)&la->ll_addr, ":", 639 ifp->if_addrlen, (u_char *)ar_sha(ah), ":", 640 ifp->if_xname); 641 } 642 } 643 644 if (ifp->if_addrlen != ah->ar_hln) { 645 log(LOG_WARNING, 646 "arp from %*D: addr len: new %d, i/f %d (ignored)", 647 ifp->if_addrlen, (u_char *) ar_sha(ah), ":", 648 ah->ar_hln, ifp->if_addrlen); 649 goto reply; 650 } 651 (void)memcpy(&la->ll_addr, ar_sha(ah), ifp->if_addrlen); 652 la->la_flags |= LLE_VALID; 653 654 if (!(la->la_flags & LLE_STATIC)) { 655 la->la_expire = time_uptime + V_arpt_keep; 656 callout_reset(&la->la_timer, hz * V_arpt_keep, 657 arptimer, la); 658 } 659 la->la_asked = 0; 660 la->la_preempt = V_arp_maxtries; 661 if (la->la_hold != NULL) { 662 m0 = la->la_hold; 663 la->la_hold = 0; 664 memcpy(&sa, L3_ADDR(la), sizeof(sa)); 665 LLE_WUNLOCK(la); 666 667 (*ifp->if_output)(ifp, m0, &sa, NULL); 668 return; 669 } 670 } 671 reply: 672 if (op != ARPOP_REQUEST) 673 goto drop; 674 675 if (itaddr.s_addr == myaddr.s_addr) { 676 /* Shortcut.. the receiving interface is the target. */ 677 (void)memcpy(ar_tha(ah), ar_sha(ah), ah->ar_hln); 678 (void)memcpy(ar_sha(ah), enaddr, ah->ar_hln); 679 } else { 680 struct llentry *lle = NULL; 681 682 if (!V_arp_proxyall) 683 goto drop; 684 685 sin.sin_addr = itaddr; 686 /* XXX MRT use table 0 for arp reply */ 687 rt = in_rtalloc1((struct sockaddr *)&sin, 0, 0UL, 0); 688 if (!rt) 689 goto drop; 690 691 /* 692 * Don't send proxies for nodes on the same interface 693 * as this one came out of, or we'll get into a fight 694 * over who claims what Ether address. 695 */ 696 if (!rt->rt_ifp || rt->rt_ifp == ifp) { 697 RTFREE_LOCKED(rt); 698 goto drop; 699 } 700 IF_AFDATA_LOCK(rt->rt_ifp); 701 lle = lla_lookup(LLTABLE(rt->rt_ifp), 0, (struct sockaddr *)&sin); 702 IF_AFDATA_UNLOCK(rt->rt_ifp); 703 RTFREE_LOCKED(rt); 704 705 if (lle != NULL) { 706 (void)memcpy(ar_tha(ah), ar_sha(ah), ah->ar_hln); 707 (void)memcpy(ar_sha(ah), &lle->ll_addr, ah->ar_hln); 708 LLE_RUNLOCK(lle); 709 } else 710 goto drop; 711 712 /* 713 * Also check that the node which sent the ARP packet 714 * is on the the interface we expect it to be on. This 715 * avoids ARP chaos if an interface is connected to the 716 * wrong network. 717 */ 718 sin.sin_addr = isaddr; 719 720 /* XXX MRT use table 0 for arp checks */ 721 rt = in_rtalloc1((struct sockaddr *)&sin, 0, 0UL, 0); 722 if (!rt) 723 goto drop; 724 if (rt->rt_ifp != ifp) { 725 log(LOG_INFO, "arp_proxy: ignoring request" 726 " from %s via %s, expecting %s\n", 727 inet_ntoa(isaddr), ifp->if_xname, 728 rt->rt_ifp->if_xname); 729 RTFREE_LOCKED(rt); 730 goto drop; 731 } 732 RTFREE_LOCKED(rt); 733 734 #ifdef DEBUG_PROXY 735 printf("arp: proxying for %s\n", 736 inet_ntoa(itaddr)); 737 #endif 738 } 739 740 if (la != NULL) 741 LLE_WUNLOCK(la); 742 if (itaddr.s_addr == myaddr.s_addr && 743 IN_LINKLOCAL(ntohl(itaddr.s_addr))) { 744 /* RFC 3927 link-local IPv4; always reply by broadcast. */ 745 #ifdef DEBUG_LINKLOCAL 746 printf("arp: sending reply for link-local addr %s\n", 747 inet_ntoa(itaddr)); 748 #endif 749 m->m_flags |= M_BCAST; 750 m->m_flags &= ~M_MCAST; 751 } else { 752 /* default behaviour; never reply by broadcast. */ 753 m->m_flags &= ~(M_BCAST|M_MCAST); 754 } 755 (void)memcpy(ar_tpa(ah), ar_spa(ah), ah->ar_pln); 756 (void)memcpy(ar_spa(ah), &itaddr, ah->ar_pln); 757 ah->ar_op = htons(ARPOP_REPLY); 758 ah->ar_pro = htons(ETHERTYPE_IP); /* let's be sure! */ 759 m->m_len = sizeof(*ah) + (2 * ah->ar_pln) + (2 * ah->ar_hln); 760 m->m_pkthdr.len = m->m_len; 761 sa.sa_family = AF_ARP; 762 sa.sa_len = 2; 763 (*ifp->if_output)(ifp, m, &sa, NULL); 764 return; 765 766 drop: 767 if (la != NULL) 768 LLE_WUNLOCK(la); 769 m_freem(m); 770 } 771 #endif 772 773 void 774 arp_ifinit(struct ifnet *ifp, struct ifaddr *ifa) 775 { 776 struct llentry *lle; 777 778 if (ntohl(IA_SIN(ifa)->sin_addr.s_addr) != INADDR_ANY) { 779 arprequest(ifp, &IA_SIN(ifa)->sin_addr, 780 &IA_SIN(ifa)->sin_addr, IF_LLADDR(ifp)); 781 /* 782 * interface address is considered static entry 783 * because the output of the arp utility shows 784 * that L2 entry as permanent 785 */ 786 IF_AFDATA_LOCK(ifp); 787 lle = lla_lookup(LLTABLE(ifp), (LLE_CREATE | LLE_IFADDR | LLE_STATIC), 788 (struct sockaddr *)IA_SIN(ifa)); 789 IF_AFDATA_UNLOCK(ifp); 790 if (lle == NULL) 791 log(LOG_INFO, "arp_ifinit: cannot create arp " 792 "entry for interface address\n"); 793 else 794 LLE_RUNLOCK(lle); 795 } 796 ifa->ifa_rtrequest = NULL; 797 } 798 799 void 800 arp_ifinit2(struct ifnet *ifp, struct ifaddr *ifa, u_char *enaddr) 801 { 802 if (ntohl(IA_SIN(ifa)->sin_addr.s_addr) != INADDR_ANY) 803 arprequest(ifp, &IA_SIN(ifa)->sin_addr, 804 &IA_SIN(ifa)->sin_addr, enaddr); 805 ifa->ifa_rtrequest = NULL; 806 } 807 808 static int 809 arp_iattach(const void *unused __unused) 810 { 811 INIT_VNET_INET(curvnet); 812 813 V_arpt_keep = (20*60); /* once resolved, good for 20 more minutes */ 814 V_arp_maxtries = 5; 815 V_useloopback = 1; /* use loopback interface for local traffic */ 816 V_arp_proxyall = 0; 817 818 return (0); 819 } 820 821 static void 822 arp_init(void) 823 { 824 825 #ifndef VIMAGE_GLOBALS 826 vnet_mod_register(&vnet_arp_modinfo); 827 #else 828 arp_iattach(NULL); 829 #endif 830 831 netisr_register(&arp_nh); 832 } 833 SYSINIT(arp, SI_SUB_PROTO_DOMAIN, SI_ORDER_ANY, arp_init, 0); 834