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