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