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_len < arphdr_len(ar)) { 535 m = m_pullup(m, arphdr_len(ar)); 536 if (m == NULL) { 537 ARP_LOG(LOG_NOTICE, "short packet received on %s\n", 538 if_name(ifp)); 539 return; 540 } 541 ar = mtod(m, struct arphdr *); 542 } 543 544 hlen = 0; 545 layer = ""; 546 switch (ntohs(ar->ar_hrd)) { 547 case ARPHRD_ETHER: 548 hlen = ETHER_ADDR_LEN; /* RFC 826 */ 549 layer = "ethernet"; 550 break; 551 case ARPHRD_IEEE802: 552 hlen = 6; /* RFC 1390, FDDI_ADDR_LEN */ 553 layer = "fddi"; 554 break; 555 case ARPHRD_ARCNET: 556 hlen = 1; /* RFC 1201, ARC_ADDR_LEN */ 557 layer = "arcnet"; 558 break; 559 case ARPHRD_INFINIBAND: 560 hlen = 20; /* RFC 4391, INFINIBAND_ALEN */ 561 layer = "infiniband"; 562 break; 563 case ARPHRD_IEEE1394: 564 hlen = 0; /* SHALL be 16 */ /* RFC 2734 */ 565 layer = "firewire"; 566 567 /* 568 * Restrict too long harware addresses. 569 * Currently we are capable of handling 20-byte 570 * addresses ( sizeof(lle->ll_addr) ) 571 */ 572 if (ar->ar_hln >= 20) 573 hlen = 16; 574 break; 575 default: 576 ARP_LOG(LOG_NOTICE, 577 "packet with unknown harware format 0x%02d received on %s\n", 578 ntohs(ar->ar_hrd), if_name(ifp)); 579 m_freem(m); 580 return; 581 } 582 583 if (hlen != 0 && hlen != ar->ar_hln) { 584 ARP_LOG(LOG_NOTICE, 585 "packet with invalid %s address length %d received on %s\n", 586 layer, ar->ar_hln, if_name(ifp)); 587 m_freem(m); 588 return; 589 } 590 591 ARPSTAT_INC(received); 592 switch (ntohs(ar->ar_pro)) { 593 #ifdef INET 594 case ETHERTYPE_IP: 595 in_arpinput(m); 596 return; 597 #endif 598 } 599 m_freem(m); 600 } 601 602 #ifdef INET 603 /* 604 * ARP for Internet protocols on 10 Mb/s Ethernet. 605 * Algorithm is that given in RFC 826. 606 * In addition, a sanity check is performed on the sender 607 * protocol address, to catch impersonators. 608 * We no longer handle negotiations for use of trailer protocol: 609 * Formerly, ARP replied for protocol type ETHERTYPE_TRAIL sent 610 * along with IP replies if we wanted trailers sent to us, 611 * and also sent them in response to IP replies. 612 * This allowed either end to announce the desire to receive 613 * trailer packets. 614 * We no longer reply to requests for ETHERTYPE_TRAIL protocol either, 615 * but formerly didn't normally send requests. 616 */ 617 static int log_arp_wrong_iface = 1; 618 static int log_arp_movements = 1; 619 static int log_arp_permanent_modify = 1; 620 static int allow_multicast = 0; 621 622 SYSCTL_INT(_net_link_ether_inet, OID_AUTO, log_arp_wrong_iface, CTLFLAG_RW, 623 &log_arp_wrong_iface, 0, 624 "log arp packets arriving on the wrong interface"); 625 SYSCTL_INT(_net_link_ether_inet, OID_AUTO, log_arp_movements, CTLFLAG_RW, 626 &log_arp_movements, 0, 627 "log arp replies from MACs different than the one in the cache"); 628 SYSCTL_INT(_net_link_ether_inet, OID_AUTO, log_arp_permanent_modify, CTLFLAG_RW, 629 &log_arp_permanent_modify, 0, 630 "log arp replies from MACs different than the one in the permanent arp entry"); 631 SYSCTL_INT(_net_link_ether_inet, OID_AUTO, allow_multicast, CTLFLAG_RW, 632 &allow_multicast, 0, "accept multicast addresses"); 633 634 static void 635 in_arpinput(struct mbuf *m) 636 { 637 struct rm_priotracker in_ifa_tracker; 638 struct arphdr *ah; 639 struct ifnet *ifp = m->m_pkthdr.rcvif; 640 struct llentry *la = NULL, *la_tmp; 641 struct rtentry *rt; 642 struct ifaddr *ifa; 643 struct in_ifaddr *ia; 644 struct sockaddr sa; 645 struct in_addr isaddr, itaddr, myaddr; 646 u_int8_t *enaddr = NULL; 647 int op; 648 int bridged = 0, is_bridge = 0; 649 int carped; 650 struct sockaddr_in sin; 651 struct sockaddr *dst; 652 sin.sin_len = sizeof(struct sockaddr_in); 653 sin.sin_family = AF_INET; 654 sin.sin_addr.s_addr = 0; 655 656 if (ifp->if_bridge) 657 bridged = 1; 658 if (ifp->if_type == IFT_BRIDGE) 659 is_bridge = 1; 660 661 /* 662 * We already have checked that mbuf contains enough contiguous data 663 * to hold entire arp message according to the arp header. 664 */ 665 ah = mtod(m, struct arphdr *); 666 667 /* 668 * ARP is only for IPv4 so we can reject packets with 669 * a protocol length not equal to an IPv4 address. 670 */ 671 if (ah->ar_pln != sizeof(struct in_addr)) { 672 ARP_LOG(LOG_NOTICE, "requested protocol length != %zu\n", 673 sizeof(struct in_addr)); 674 goto drop; 675 } 676 677 if (allow_multicast == 0 && ETHER_IS_MULTICAST(ar_sha(ah))) { 678 ARP_LOG(LOG_NOTICE, "%*D is multicast\n", 679 ifp->if_addrlen, (u_char *)ar_sha(ah), ":"); 680 goto drop; 681 } 682 683 op = ntohs(ah->ar_op); 684 (void)memcpy(&isaddr, ar_spa(ah), sizeof (isaddr)); 685 (void)memcpy(&itaddr, ar_tpa(ah), sizeof (itaddr)); 686 687 if (op == ARPOP_REPLY) 688 ARPSTAT_INC(rxreplies); 689 690 /* 691 * For a bridge, we want to check the address irrespective 692 * of the receive interface. (This will change slightly 693 * when we have clusters of interfaces). 694 */ 695 IN_IFADDR_RLOCK(&in_ifa_tracker); 696 LIST_FOREACH(ia, INADDR_HASH(itaddr.s_addr), ia_hash) { 697 if (((bridged && ia->ia_ifp->if_bridge == ifp->if_bridge) || 698 ia->ia_ifp == ifp) && 699 itaddr.s_addr == ia->ia_addr.sin_addr.s_addr && 700 (ia->ia_ifa.ifa_carp == NULL || 701 (*carp_iamatch_p)(&ia->ia_ifa, &enaddr))) { 702 ifa_ref(&ia->ia_ifa); 703 IN_IFADDR_RUNLOCK(&in_ifa_tracker); 704 goto match; 705 } 706 } 707 LIST_FOREACH(ia, INADDR_HASH(isaddr.s_addr), ia_hash) 708 if (((bridged && ia->ia_ifp->if_bridge == ifp->if_bridge) || 709 ia->ia_ifp == ifp) && 710 isaddr.s_addr == ia->ia_addr.sin_addr.s_addr) { 711 ifa_ref(&ia->ia_ifa); 712 IN_IFADDR_RUNLOCK(&in_ifa_tracker); 713 goto match; 714 } 715 716 #define BDG_MEMBER_MATCHES_ARP(addr, ifp, ia) \ 717 (ia->ia_ifp->if_bridge == ifp->if_softc && \ 718 !bcmp(IF_LLADDR(ia->ia_ifp), IF_LLADDR(ifp), ifp->if_addrlen) && \ 719 addr == ia->ia_addr.sin_addr.s_addr) 720 /* 721 * Check the case when bridge shares its MAC address with 722 * some of its children, so packets are claimed by bridge 723 * itself (bridge_input() does it first), but they are really 724 * meant to be destined to the bridge member. 725 */ 726 if (is_bridge) { 727 LIST_FOREACH(ia, INADDR_HASH(itaddr.s_addr), ia_hash) { 728 if (BDG_MEMBER_MATCHES_ARP(itaddr.s_addr, ifp, ia)) { 729 ifa_ref(&ia->ia_ifa); 730 ifp = ia->ia_ifp; 731 IN_IFADDR_RUNLOCK(&in_ifa_tracker); 732 goto match; 733 } 734 } 735 } 736 #undef BDG_MEMBER_MATCHES_ARP 737 IN_IFADDR_RUNLOCK(&in_ifa_tracker); 738 739 /* 740 * No match, use the first inet address on the receive interface 741 * as a dummy address for the rest of the function. 742 */ 743 IF_ADDR_RLOCK(ifp); 744 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) 745 if (ifa->ifa_addr->sa_family == AF_INET && 746 (ifa->ifa_carp == NULL || 747 (*carp_iamatch_p)(ifa, &enaddr))) { 748 ia = ifatoia(ifa); 749 ifa_ref(ifa); 750 IF_ADDR_RUNLOCK(ifp); 751 goto match; 752 } 753 IF_ADDR_RUNLOCK(ifp); 754 755 /* 756 * If bridging, fall back to using any inet address. 757 */ 758 IN_IFADDR_RLOCK(&in_ifa_tracker); 759 if (!bridged || (ia = TAILQ_FIRST(&V_in_ifaddrhead)) == NULL) { 760 IN_IFADDR_RUNLOCK(&in_ifa_tracker); 761 goto drop; 762 } 763 ifa_ref(&ia->ia_ifa); 764 IN_IFADDR_RUNLOCK(&in_ifa_tracker); 765 match: 766 if (!enaddr) 767 enaddr = (u_int8_t *)IF_LLADDR(ifp); 768 carped = (ia->ia_ifa.ifa_carp != NULL); 769 myaddr = ia->ia_addr.sin_addr; 770 ifa_free(&ia->ia_ifa); 771 if (!bcmp(ar_sha(ah), enaddr, ifp->if_addrlen)) 772 goto drop; /* it's from me, ignore it. */ 773 if (!bcmp(ar_sha(ah), ifp->if_broadcastaddr, ifp->if_addrlen)) { 774 ARP_LOG(LOG_NOTICE, "link address is broadcast for IP address " 775 "%s!\n", inet_ntoa(isaddr)); 776 goto drop; 777 } 778 779 if (ifp->if_addrlen != ah->ar_hln) { 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 787 /* 788 * Warn if another host is using the same IP address, but only if the 789 * IP address isn't 0.0.0.0, which is used for DHCP only, in which 790 * case we suppress the warning to avoid false positive complaints of 791 * potential misconfiguration. 792 */ 793 if (!bridged && !carped && isaddr.s_addr == myaddr.s_addr && 794 myaddr.s_addr != 0) { 795 ARP_LOG(LOG_ERR, "%*D is using my IP address %s on %s!\n", 796 ifp->if_addrlen, (u_char *)ar_sha(ah), ":", 797 inet_ntoa(isaddr), ifp->if_xname); 798 itaddr = myaddr; 799 ARPSTAT_INC(dupips); 800 goto reply; 801 } 802 if (ifp->if_flags & IFF_STATICARP) 803 goto reply; 804 805 bzero(&sin, sizeof(sin)); 806 sin.sin_len = sizeof(struct sockaddr_in); 807 sin.sin_family = AF_INET; 808 sin.sin_addr = isaddr; 809 dst = (struct sockaddr *)&sin; 810 IF_AFDATA_RLOCK(ifp); 811 la = lla_lookup(LLTABLE(ifp), LLE_EXCLUSIVE, dst); 812 IF_AFDATA_RUNLOCK(ifp); 813 if (la != NULL) 814 arp_check_update_lle(ah, isaddr, ifp, bridged, la); 815 else if (itaddr.s_addr == myaddr.s_addr) { 816 /* 817 * Reply to our address, but no lle exists yet. 818 * do we really have to create an entry? 819 */ 820 la = lltable_alloc_entry(LLTABLE(ifp), 0, dst); 821 if (la == NULL) 822 goto drop; 823 arp_update_lle(ah, ifp, la); 824 825 IF_AFDATA_WLOCK(ifp); 826 LLE_WLOCK(la); 827 la_tmp = lla_lookup(LLTABLE(ifp), LLE_EXCLUSIVE, dst); 828 829 /* 830 * Check if lle still does not exists. 831 * If it does, that means that we either 832 * 1) have configured it explicitly, via 833 * 1a) 'arp -s' static entry or 834 * 1b) interface address static record 835 * or 836 * 2) it was the result of sending first packet to-host 837 * or 838 * 3) it was another arp reply packet we handled in 839 * different thread. 840 * 841 * In all cases except 3) we definitely need to prefer 842 * existing lle. For the sake of simplicity, prefer any 843 * existing lle over newly-create one. 844 */ 845 if (la_tmp == NULL) 846 lltable_link_entry(LLTABLE(ifp), la); 847 IF_AFDATA_WUNLOCK(ifp); 848 849 if (la_tmp == NULL) { 850 arp_mark_lle_reachable(la); 851 LLE_WUNLOCK(la); 852 } else { 853 /* Free newly-create entry and handle packet */ 854 lltable_free_entry(LLTABLE(ifp), la); 855 la = la_tmp; 856 la_tmp = NULL; 857 arp_check_update_lle(ah, isaddr, ifp, bridged, la); 858 /* arp_check_update_lle() returns @la unlocked */ 859 } 860 la = NULL; 861 } 862 reply: 863 if (op != ARPOP_REQUEST) 864 goto drop; 865 ARPSTAT_INC(rxrequests); 866 867 if (itaddr.s_addr == myaddr.s_addr) { 868 /* Shortcut.. the receiving interface is the target. */ 869 (void)memcpy(ar_tha(ah), ar_sha(ah), ah->ar_hln); 870 (void)memcpy(ar_sha(ah), enaddr, ah->ar_hln); 871 } else { 872 struct llentry *lle = NULL; 873 874 sin.sin_addr = itaddr; 875 IF_AFDATA_RLOCK(ifp); 876 lle = lla_lookup(LLTABLE(ifp), 0, (struct sockaddr *)&sin); 877 IF_AFDATA_RUNLOCK(ifp); 878 879 if ((lle != NULL) && (lle->la_flags & LLE_PUB)) { 880 (void)memcpy(ar_tha(ah), ar_sha(ah), ah->ar_hln); 881 (void)memcpy(ar_sha(ah), &lle->ll_addr, ah->ar_hln); 882 LLE_RUNLOCK(lle); 883 } else { 884 885 if (lle != NULL) 886 LLE_RUNLOCK(lle); 887 888 if (!V_arp_proxyall) 889 goto drop; 890 891 sin.sin_addr = itaddr; 892 /* XXX MRT use table 0 for arp reply */ 893 rt = in_rtalloc1((struct sockaddr *)&sin, 0, 0UL, 0); 894 if (!rt) 895 goto drop; 896 897 /* 898 * Don't send proxies for nodes on the same interface 899 * as this one came out of, or we'll get into a fight 900 * over who claims what Ether address. 901 */ 902 if (!rt->rt_ifp || rt->rt_ifp == ifp) { 903 RTFREE_LOCKED(rt); 904 goto drop; 905 } 906 RTFREE_LOCKED(rt); 907 908 (void)memcpy(ar_tha(ah), ar_sha(ah), ah->ar_hln); 909 (void)memcpy(ar_sha(ah), enaddr, ah->ar_hln); 910 911 /* 912 * Also check that the node which sent the ARP packet 913 * is on the interface we expect it to be on. This 914 * avoids ARP chaos if an interface is connected to the 915 * wrong network. 916 */ 917 sin.sin_addr = isaddr; 918 919 /* XXX MRT use table 0 for arp checks */ 920 rt = in_rtalloc1((struct sockaddr *)&sin, 0, 0UL, 0); 921 if (!rt) 922 goto drop; 923 if (rt->rt_ifp != ifp) { 924 ARP_LOG(LOG_INFO, "proxy: ignoring request" 925 " from %s via %s, expecting %s\n", 926 inet_ntoa(isaddr), ifp->if_xname, 927 rt->rt_ifp->if_xname); 928 RTFREE_LOCKED(rt); 929 goto drop; 930 } 931 RTFREE_LOCKED(rt); 932 933 #ifdef DEBUG_PROXY 934 printf("arp: proxying for %s\n", inet_ntoa(itaddr)); 935 #endif 936 } 937 } 938 939 if (itaddr.s_addr == myaddr.s_addr && 940 IN_LINKLOCAL(ntohl(itaddr.s_addr))) { 941 /* RFC 3927 link-local IPv4; always reply by broadcast. */ 942 #ifdef DEBUG_LINKLOCAL 943 printf("arp: sending reply for link-local addr %s\n", 944 inet_ntoa(itaddr)); 945 #endif 946 m->m_flags |= M_BCAST; 947 m->m_flags &= ~M_MCAST; 948 } else { 949 /* default behaviour; never reply by broadcast. */ 950 m->m_flags &= ~(M_BCAST|M_MCAST); 951 } 952 (void)memcpy(ar_tpa(ah), ar_spa(ah), ah->ar_pln); 953 (void)memcpy(ar_spa(ah), &itaddr, ah->ar_pln); 954 ah->ar_op = htons(ARPOP_REPLY); 955 ah->ar_pro = htons(ETHERTYPE_IP); /* let's be sure! */ 956 m->m_len = sizeof(*ah) + (2 * ah->ar_pln) + (2 * ah->ar_hln); 957 m->m_pkthdr.len = m->m_len; 958 m->m_pkthdr.rcvif = NULL; 959 sa.sa_family = AF_ARP; 960 sa.sa_len = 2; 961 m_clrprotoflags(m); /* Avoid confusing lower layers. */ 962 (*ifp->if_output)(ifp, m, &sa, NULL); 963 ARPSTAT_INC(txreplies); 964 return; 965 966 drop: 967 m_freem(m); 968 } 969 #endif 970 971 /* 972 * Checks received arp data against existing @la. 973 * Updates lle state/performs notification if necessary. 974 */ 975 static void 976 arp_check_update_lle(struct arphdr *ah, struct in_addr isaddr, struct ifnet *ifp, 977 int bridged, struct llentry *la) 978 { 979 struct sockaddr sa; 980 struct mbuf *m_hold, *m_hold_next; 981 982 LLE_WLOCK_ASSERT(la); 983 984 /* the following is not an error when doing bridging */ 985 if (!bridged && la->lle_tbl->llt_ifp != ifp) { 986 if (log_arp_wrong_iface) 987 ARP_LOG(LOG_WARNING, "%s is on %s " 988 "but got reply from %*D on %s\n", 989 inet_ntoa(isaddr), 990 la->lle_tbl->llt_ifp->if_xname, 991 ifp->if_addrlen, (u_char *)ar_sha(ah), ":", 992 ifp->if_xname); 993 LLE_WUNLOCK(la); 994 return; 995 } 996 if ((la->la_flags & LLE_VALID) && 997 bcmp(ar_sha(ah), &la->ll_addr, ifp->if_addrlen)) { 998 if (la->la_flags & LLE_STATIC) { 999 LLE_WUNLOCK(la); 1000 if (log_arp_permanent_modify) 1001 ARP_LOG(LOG_ERR, 1002 "%*D attempts to modify " 1003 "permanent entry for %s on %s\n", 1004 ifp->if_addrlen, 1005 (u_char *)ar_sha(ah), ":", 1006 inet_ntoa(isaddr), ifp->if_xname); 1007 return; 1008 } 1009 if (log_arp_movements) { 1010 ARP_LOG(LOG_INFO, "%s moved from %*D " 1011 "to %*D on %s\n", 1012 inet_ntoa(isaddr), 1013 ifp->if_addrlen, 1014 (u_char *)&la->ll_addr, ":", 1015 ifp->if_addrlen, (u_char *)ar_sha(ah), ":", 1016 ifp->if_xname); 1017 } 1018 } 1019 1020 /* Check if something has changed */ 1021 if (memcmp(&la->ll_addr, ar_sha(ah), ifp->if_addrlen) != 0 || 1022 (la->la_flags & LLE_VALID) == 0) { 1023 /* Perform real LLE update */ 1024 /* use afdata WLOCK to update fields */ 1025 LLE_ADDREF(la); 1026 LLE_WUNLOCK(la); 1027 IF_AFDATA_WLOCK(ifp); 1028 LLE_WLOCK(la); 1029 1030 /* 1031 * Since we droppped LLE lock, other thread might have deleted 1032 * this lle. Check and return 1033 */ 1034 if ((la->la_flags & LLE_DELETED) != 0) { 1035 IF_AFDATA_WUNLOCK(ifp); 1036 LLE_FREE_LOCKED(la); 1037 return; 1038 } 1039 1040 /* Update data */ 1041 arp_update_lle(ah, ifp, la); 1042 1043 IF_AFDATA_WUNLOCK(ifp); 1044 LLE_REMREF(la); 1045 } 1046 1047 arp_mark_lle_reachable(la); 1048 1049 /* 1050 * The packets are all freed within the call to the output 1051 * routine. 1052 * 1053 * NB: The lock MUST be released before the call to the 1054 * output routine. 1055 */ 1056 if (la->la_hold != NULL) { 1057 m_hold = la->la_hold; 1058 la->la_hold = NULL; 1059 la->la_numheld = 0; 1060 lltable_fill_sa_entry(la, &sa); 1061 LLE_WUNLOCK(la); 1062 for (; m_hold != NULL; m_hold = m_hold_next) { 1063 m_hold_next = m_hold->m_nextpkt; 1064 m_hold->m_nextpkt = NULL; 1065 /* Avoid confusing lower layers. */ 1066 m_clrprotoflags(m_hold); 1067 (*ifp->if_output)(ifp, m_hold, &sa, NULL); 1068 } 1069 } else 1070 LLE_WUNLOCK(la); 1071 } 1072 1073 /* 1074 * Updates @la fields used by fast path code. 1075 */ 1076 static void 1077 arp_update_lle(struct arphdr *ah, struct ifnet *ifp, struct llentry *la) 1078 { 1079 1080 memcpy(&la->ll_addr, ar_sha(ah), ifp->if_addrlen); 1081 la->la_flags |= LLE_VALID; 1082 } 1083 1084 static void 1085 arp_mark_lle_reachable(struct llentry *la) 1086 { 1087 int canceled; 1088 1089 LLE_WLOCK_ASSERT(la); 1090 1091 EVENTHANDLER_INVOKE(lle_event, la, LLENTRY_RESOLVED); 1092 1093 if (!(la->la_flags & LLE_STATIC)) { 1094 LLE_ADDREF(la); 1095 la->la_expire = time_uptime + V_arpt_keep; 1096 canceled = callout_reset(&la->lle_timer, 1097 hz * V_arpt_keep, arptimer, la); 1098 if (canceled) 1099 LLE_REMREF(la); 1100 } 1101 la->la_asked = 0; 1102 la->la_preempt = V_arp_maxtries; 1103 } 1104 1105 void 1106 arp_ifinit(struct ifnet *ifp, struct ifaddr *ifa) 1107 { 1108 struct llentry *lle, *lle_tmp; 1109 struct sockaddr_in *dst_in; 1110 struct sockaddr *dst; 1111 1112 if (ifa->ifa_carp != NULL) 1113 return; 1114 1115 ifa->ifa_rtrequest = NULL; 1116 1117 dst_in = IA_SIN(ifa); 1118 dst = (struct sockaddr *)dst_in; 1119 1120 if (ntohl(IA_SIN(ifa)->sin_addr.s_addr) == INADDR_ANY) 1121 return; 1122 1123 arprequest(ifp, &IA_SIN(ifa)->sin_addr, 1124 &IA_SIN(ifa)->sin_addr, IF_LLADDR(ifp)); 1125 1126 /* 1127 * Interface address LLE record is considered static 1128 * because kernel code relies on LLE_STATIC flag to check 1129 * if these entries can be rewriten by arp updates. 1130 */ 1131 lle = lltable_alloc_entry(LLTABLE(ifp), LLE_IFADDR | LLE_STATIC, dst); 1132 if (lle == NULL) { 1133 log(LOG_INFO, "arp_ifinit: cannot create arp " 1134 "entry for interface address\n"); 1135 return; 1136 } 1137 1138 IF_AFDATA_WLOCK(ifp); 1139 LLE_WLOCK(lle); 1140 /* Unlink any entry if exists */ 1141 lle_tmp = lla_lookup(LLTABLE(ifp), LLE_EXCLUSIVE, dst); 1142 if (lle_tmp != NULL) 1143 lltable_unlink_entry(LLTABLE(ifp), lle_tmp); 1144 1145 lltable_link_entry(LLTABLE(ifp), lle); 1146 IF_AFDATA_WUNLOCK(ifp); 1147 1148 if (lle_tmp != NULL) 1149 EVENTHANDLER_INVOKE(lle_event, lle_tmp, LLENTRY_EXPIRED); 1150 1151 EVENTHANDLER_INVOKE(lle_event, lle, LLENTRY_RESOLVED); 1152 LLE_WUNLOCK(lle); 1153 if (lle_tmp != NULL) 1154 lltable_free_entry(LLTABLE(ifp), lle_tmp); 1155 } 1156 1157 void 1158 arp_ifinit2(struct ifnet *ifp, struct ifaddr *ifa, u_char *enaddr) 1159 { 1160 if (ntohl(IA_SIN(ifa)->sin_addr.s_addr) != INADDR_ANY) 1161 arprequest(ifp, &IA_SIN(ifa)->sin_addr, 1162 &IA_SIN(ifa)->sin_addr, enaddr); 1163 ifa->ifa_rtrequest = NULL; 1164 } 1165 1166 static void 1167 arp_init(void) 1168 { 1169 1170 netisr_register(&arp_nh); 1171 } 1172 SYSINIT(arp, SI_SUB_PROTO_DOMAIN, SI_ORDER_ANY, arp_init, 0); 1173