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