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