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