2 * Copyright (c) 1982, 1986, 1988, 1993
3 * The Regents of the University of California. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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
29 * @(#)if_ether.c 8.1 (Berkeley) 6/10/93
33 * Ethernet address resolution protocol.
35 * add "inuse/lock" bit (or ref. count) along with valid bit
38 #include <sys/cdefs.h>
39 __FBSDID("$FreeBSD$");
43 #include <sys/param.h>
44 #include <sys/kernel.h>
46 #include <sys/queue.h>
47 #include <sys/sysctl.h>
48 #include <sys/systm.h>
50 #include <sys/malloc.h>
52 #include <sys/rmlock.h>
53 #include <sys/socket.h>
54 #include <sys/syslog.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>
65 #include <netinet/in.h>
66 #include <netinet/in_fib.h>
67 #include <netinet/in_var.h>
68 #include <net/if_llatbl.h>
69 #include <netinet/if_ether.h>
71 #include <netinet/ip_carp.h>
74 #include <security/mac/mac_framework.h>
76 #define SIN(s) ((const struct sockaddr_in *)(s))
78 static struct timeval arp_lastlog;
79 static int arp_curpps;
80 static int arp_maxpps = 1;
82 /* Simple ARP state machine */
83 enum arp_llinfo_state {
84 ARP_LLINFO_INCOMPLETE = 0, /* No LLE data */
85 ARP_LLINFO_REACHABLE, /* LLE is valid */
86 ARP_LLINFO_VERIFY, /* LLE is valid, need refresh */
87 ARP_LLINFO_DELETED, /* LLE is deleted */
90 SYSCTL_DECL(_net_link_ether);
91 static SYSCTL_NODE(_net_link_ether, PF_INET, inet, CTLFLAG_RW, 0, "");
92 static SYSCTL_NODE(_net_link_ether, PF_ARP, arp, CTLFLAG_RW, 0, "");
95 static VNET_DEFINE(int, arpt_keep) = (20*60); /* once resolved, good for 20
97 static VNET_DEFINE(int, arp_maxtries) = 5;
98 static VNET_DEFINE(int, arp_proxyall) = 0;
99 static VNET_DEFINE(int, arpt_down) = 20; /* keep incomplete entries for
101 static VNET_DEFINE(int, arpt_rexmit) = 1; /* retransmit arp entries, sec*/
102 VNET_PCPUSTAT_DEFINE(struct arpstat, arpstat); /* ARP statistics, see if_arp.h */
103 VNET_PCPUSTAT_SYSINIT(arpstat);
106 VNET_PCPUSTAT_SYSUNINIT(arpstat);
109 static VNET_DEFINE(int, arp_maxhold) = 1;
111 #define V_arpt_keep VNET(arpt_keep)
112 #define V_arpt_down VNET(arpt_down)
113 #define V_arpt_rexmit VNET(arpt_rexmit)
114 #define V_arp_maxtries VNET(arp_maxtries)
115 #define V_arp_proxyall VNET(arp_proxyall)
116 #define V_arp_maxhold VNET(arp_maxhold)
118 SYSCTL_INT(_net_link_ether_inet, OID_AUTO, max_age, CTLFLAG_VNET | CTLFLAG_RW,
119 &VNET_NAME(arpt_keep), 0,
120 "ARP entry lifetime in seconds");
121 SYSCTL_INT(_net_link_ether_inet, OID_AUTO, maxtries, CTLFLAG_VNET | CTLFLAG_RW,
122 &VNET_NAME(arp_maxtries), 0,
123 "ARP resolution attempts before returning error");
124 SYSCTL_INT(_net_link_ether_inet, OID_AUTO, proxyall, CTLFLAG_VNET | CTLFLAG_RW,
125 &VNET_NAME(arp_proxyall), 0,
126 "Enable proxy ARP for all suitable requests");
127 SYSCTL_INT(_net_link_ether_inet, OID_AUTO, wait, CTLFLAG_VNET | CTLFLAG_RW,
128 &VNET_NAME(arpt_down), 0,
129 "Incomplete ARP entry lifetime in seconds");
130 SYSCTL_VNET_PCPUSTAT(_net_link_ether_arp, OID_AUTO, stats, struct arpstat,
131 arpstat, "ARP statistics (struct arpstat, net/if_arp.h)");
132 SYSCTL_INT(_net_link_ether_inet, OID_AUTO, maxhold, CTLFLAG_VNET | CTLFLAG_RW,
133 &VNET_NAME(arp_maxhold), 0,
134 "Number of packets to hold per ARP entry");
135 SYSCTL_INT(_net_link_ether_inet, OID_AUTO, max_log_per_second,
136 CTLFLAG_RW, &arp_maxpps, 0,
137 "Maximum number of remotely triggered ARP messages that can be "
138 "logged per second");
140 #define ARP_LOG(pri, ...) do { \
141 if (ppsratecheck(&arp_lastlog, &arp_curpps, arp_maxpps)) \
142 log((pri), "arp: " __VA_ARGS__); \
146 static void arp_init(void);
147 static void arpintr(struct mbuf *);
148 static void arptimer(void *);
150 static void in_arpinput(struct mbuf *);
153 static void arp_check_update_lle(struct arphdr *ah, struct in_addr isaddr,
154 struct ifnet *ifp, int bridged, struct llentry *la);
155 static void arp_mark_lle_reachable(struct llentry *la);
156 static void arp_iflladdr(void *arg __unused, struct ifnet *ifp);
158 static eventhandler_tag iflladdr_tag;
160 static const struct netisr_handler arp_nh = {
162 .nh_handler = arpintr,
163 .nh_proto = NETISR_ARP,
164 .nh_policy = NETISR_POLICY_SOURCE,
168 * Timeout routine. Age arp_tab entries periodically.
173 struct llentry *lle = (struct llentry *)arg;
177 if (lle->la_flags & LLE_STATIC) {
181 if (callout_pending(&lle->lle_timer)) {
183 * Here we are a bit odd here in the treatment of
184 * active/pending. If the pending bit is set, it got
185 * rescheduled before I ran. The active
186 * bit we ignore, since if it was stopped
187 * in ll_tablefree() and was currently running
188 * it would have return 0 so the code would
189 * not have deleted it since the callout could
190 * not be stopped so we want to go through
191 * with the delete here now. If the callout
192 * was restarted, the pending bit will be back on and
193 * we just want to bail since the callout_reset would
194 * return 1 and our reference would have been removed
195 * by arpresolve() below.
200 ifp = lle->lle_tbl->llt_ifp;
201 CURVNET_SET(ifp->if_vnet);
203 switch (lle->ln_state) {
204 case ARP_LLINFO_REACHABLE:
207 * Expiration time is approaching.
208 * Let's try to refresh entry if it is still
211 * Set r_skip_req to get feedback from
212 * fast path. Change state and re-schedule
218 lle->ln_state = ARP_LLINFO_VERIFY;
219 callout_schedule(&lle->lle_timer, hz * V_arpt_rexmit);
223 case ARP_LLINFO_VERIFY:
225 r_skip_req = lle->r_skip_req;
228 if (r_skip_req == 0 && lle->la_preempt > 0) {
229 /* Entry was used, issue refresh request */
231 dst = lle->r_l3addr.addr4;
233 callout_schedule(&lle->lle_timer, hz * V_arpt_rexmit);
235 arprequest(ifp, NULL, &dst, NULL);
239 /* Nothing happened. Reschedule if not too late */
240 if (lle->la_expire > time_uptime) {
241 callout_schedule(&lle->lle_timer, hz * V_arpt_rexmit);
247 case ARP_LLINFO_INCOMPLETE:
248 case ARP_LLINFO_DELETED:
252 if ((lle->la_flags & LLE_DELETED) == 0) {
255 if (lle->la_flags & LLE_VALID)
256 evt = LLENTRY_EXPIRED;
258 evt = LLENTRY_TIMEDOUT;
259 EVENTHANDLER_INVOKE(lle_event, lle, evt);
262 callout_stop(&lle->lle_timer);
264 /* XXX: LOR avoidance. We still have ref on lle. */
269 /* Guard against race with other llentry_free(). */
270 if (lle->la_flags & LLE_LINKED) {
272 lltable_unlink_entry(lle->lle_tbl, lle);
274 IF_AFDATA_UNLOCK(ifp);
276 size_t pkts_dropped = llentry_free(lle);
278 ARPSTAT_ADD(dropped, pkts_dropped);
279 ARPSTAT_INC(timeouts);
285 * Broadcast an ARP request. Caller specifies:
286 * - arp header source ip address
287 * - arp header target ip address
288 * - arp header source ethernet address
291 arprequest(struct ifnet *ifp, const struct in_addr *sip,
292 const struct in_addr *tip, u_char *enaddr)
297 u_char *carpaddr = NULL;
301 * The caller did not supply a source address, try to find
302 * a compatible one among those assigned to this interface.
307 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
308 if (ifa->ifa_addr->sa_family != AF_INET)
312 if ((*carp_iamatch_p)(ifa, &carpaddr) == 0)
314 sip = &IA_SIN(ifa)->sin_addr;
317 sip = &IA_SIN(ifa)->sin_addr;
320 if (0 == ((sip->s_addr ^ tip->s_addr) &
321 IA_MASKSIN(ifa)->sin_addr.s_addr))
322 break; /* found it. */
324 IF_ADDR_RUNLOCK(ifp);
326 printf("%s: cannot find matching address\n", __func__);
331 enaddr = carpaddr ? carpaddr : (u_char *)IF_LLADDR(ifp);
333 if ((m = m_gethdr(M_NOWAIT, MT_DATA)) == NULL)
335 m->m_len = sizeof(*ah) + 2 * sizeof(struct in_addr) +
337 m->m_pkthdr.len = m->m_len;
338 M_ALIGN(m, m->m_len);
339 ah = mtod(m, struct arphdr *);
340 bzero((caddr_t)ah, m->m_len);
342 mac_netinet_arp_send(ifp, m);
344 ah->ar_pro = htons(ETHERTYPE_IP);
345 ah->ar_hln = ifp->if_addrlen; /* hardware address length */
346 ah->ar_pln = sizeof(struct in_addr); /* protocol address length */
347 ah->ar_op = htons(ARPOP_REQUEST);
348 bcopy(enaddr, ar_sha(ah), ah->ar_hln);
349 bcopy(sip, ar_spa(ah), ah->ar_pln);
350 bcopy(tip, ar_tpa(ah), ah->ar_pln);
351 sa.sa_family = AF_ARP;
353 m->m_flags |= M_BCAST;
354 m_clrprotoflags(m); /* Avoid confusing lower layers. */
355 (*ifp->if_output)(ifp, m, &sa, NULL);
356 ARPSTAT_INC(txrequests);
360 * Resolve an IP address into an ethernet address - heavy version.
361 * Used internally by arpresolve().
362 * We have already checked than we can't use existing lle without
363 * modification so we have to acquire LLE_EXCLUSIVE lle lock.
365 * On success, desten and flags are filled in and the function returns 0;
366 * If the packet must be held pending resolution, we return EWOULDBLOCK
367 * On other errors, we return the corresponding error code.
368 * Note that m_freem() handles NULL.
371 arpresolve_full(struct ifnet *ifp, int is_gw, int create, struct mbuf *m,
372 const struct sockaddr *dst, u_char *desten, uint32_t *pflags)
374 struct llentry *la = NULL, *la_tmp;
375 struct mbuf *curr = NULL;
376 struct mbuf *next = NULL;
383 IF_AFDATA_RLOCK(ifp);
384 la = lla_lookup(LLTABLE(ifp), LLE_EXCLUSIVE, dst);
385 IF_AFDATA_RUNLOCK(ifp);
387 if (la == NULL && (ifp->if_flags & (IFF_NOARP | IFF_STATICARP)) == 0) {
388 la = lltable_alloc_entry(LLTABLE(ifp), 0, dst);
391 "arpresolve: can't allocate llinfo for %s on %s\n",
392 inet_ntoa(SIN(dst)->sin_addr), if_name(ifp));
397 IF_AFDATA_WLOCK(ifp);
399 la_tmp = lla_lookup(LLTABLE(ifp), LLE_EXCLUSIVE, dst);
400 /* Prefer ANY existing lle over newly-created one */
402 lltable_link_entry(LLTABLE(ifp), la);
403 IF_AFDATA_WUNLOCK(ifp);
404 if (la_tmp != NULL) {
405 lltable_free_entry(LLTABLE(ifp), la);
414 if ((la->la_flags & LLE_VALID) &&
415 ((la->la_flags & LLE_STATIC) || la->la_expire > time_uptime)) {
416 bcopy(&la->ll_addr, desten, ifp->if_addrlen);
418 /* Check if we have feedback request from arptimer() */
419 if (la->r_skip_req != 0) {
421 la->r_skip_req = 0; /* Notify that entry was used */
425 *pflags = la->la_flags & (LLE_VALID|LLE_IFADDR);
430 renew = (la->la_asked == 0 || la->la_expire != time_uptime);
432 * There is an arptab entry, but no ethernet address
433 * response yet. Add the mbuf to the list, dropping
434 * the oldest packet if we have exceeded the system
438 if (la->la_numheld >= V_arp_maxhold) {
439 if (la->la_hold != NULL) {
440 next = la->la_hold->m_nextpkt;
441 m_freem(la->la_hold);
444 ARPSTAT_INC(dropped);
447 if (la->la_hold != NULL) {
449 while (curr->m_nextpkt != NULL)
450 curr = curr->m_nextpkt;
457 * Return EWOULDBLOCK if we have tried less than arp_maxtries. It
458 * will be masked by ether_output(). Return EHOSTDOWN/EHOSTUNREACH
459 * if we have already sent arp_maxtries ARP requests. Retransmit the
460 * ARP request, but not faster than one request per second.
462 if (la->la_asked < V_arp_maxtries)
463 error = EWOULDBLOCK; /* First request. */
465 error = is_gw != 0 ? EHOSTUNREACH : EHOSTDOWN;
471 la->la_expire = time_uptime;
472 canceled = callout_reset(&la->lle_timer, hz * V_arpt_down,
478 arprequest(ifp, NULL, &SIN(dst)->sin_addr, NULL);
487 * Resolve an IP address into an ethernet address.
489 * ifp is the interface we use
490 * is_gw != 0 if @dst represents gateway to some destination
491 * m is the mbuf. May be NULL if we don't have a packet.
492 * dst is the next hop,
493 * desten is the storage to put LL address.
494 * flags returns subset of lle flags: LLE_VALID | LLE_IFADDR
496 * On success, desten and flags are filled in and the function returns 0;
497 * If the packet must be held pending resolution, we return EWOULDBLOCK
498 * On other errors, we return the corresponding error code.
499 * Note that m_freem() handles NULL.
502 arpresolve(struct ifnet *ifp, int is_gw, struct mbuf *m,
503 const struct sockaddr *dst, u_char *desten, uint32_t *pflags)
505 struct llentry *la = 0;
511 if (m->m_flags & M_BCAST) {
514 ifp->if_broadcastaddr, ifp->if_addrlen);
517 if (m->m_flags & M_MCAST) {
519 ETHER_MAP_IP_MULTICAST(&SIN(dst)->sin_addr, desten);
524 IF_AFDATA_RLOCK(ifp);
525 la = lla_lookup(LLTABLE(ifp), LLE_UNLOCKED, dst);
526 if (la != NULL && (la->r_flags & RLLE_VALID) != 0) {
527 /* Entry found, let's copy lle info */
528 bcopy(&la->ll_addr, desten, ifp->if_addrlen);
530 *pflags = LLE_VALID | (la->r_flags & RLLE_IFADDR);
531 /* Check if we have feedback request from arptimer() */
532 if (la->r_skip_req != 0) {
534 la->r_skip_req = 0; /* Notify that entry was used */
537 IF_AFDATA_RUNLOCK(ifp);
540 IF_AFDATA_RUNLOCK(ifp);
542 return (arpresolve_full(ifp, is_gw, 1, m, dst, desten, pflags));
546 * Common length and type checks are done here,
547 * then the protocol-specific routine is called.
550 arpintr(struct mbuf *m)
557 ifp = m->m_pkthdr.rcvif;
559 if (m->m_len < sizeof(struct arphdr) &&
560 ((m = m_pullup(m, sizeof(struct arphdr))) == NULL)) {
561 ARP_LOG(LOG_NOTICE, "packet with short header received on %s\n",
565 ar = mtod(m, struct arphdr *);
567 /* Check if length is sufficient */
568 if (m->m_len < arphdr_len(ar)) {
569 m = m_pullup(m, arphdr_len(ar));
571 ARP_LOG(LOG_NOTICE, "short packet received on %s\n",
575 ar = mtod(m, struct arphdr *);
580 switch (ntohs(ar->ar_hrd)) {
582 hlen = ETHER_ADDR_LEN; /* RFC 826 */
586 hlen = 6; /* RFC 1390, FDDI_ADDR_LEN */
590 hlen = 1; /* RFC 1201, ARC_ADDR_LEN */
593 case ARPHRD_INFINIBAND:
594 hlen = 20; /* RFC 4391, INFINIBAND_ALEN */
595 layer = "infiniband";
597 case ARPHRD_IEEE1394:
598 hlen = 0; /* SHALL be 16 */ /* RFC 2734 */
602 * Restrict too long harware addresses.
603 * Currently we are capable of handling 20-byte
604 * addresses ( sizeof(lle->ll_addr) )
606 if (ar->ar_hln >= 20)
611 "packet with unknown harware format 0x%02d received on %s\n",
612 ntohs(ar->ar_hrd), if_name(ifp));
617 if (hlen != 0 && hlen != ar->ar_hln) {
619 "packet with invalid %s address length %d received on %s\n",
620 layer, ar->ar_hln, if_name(ifp));
625 ARPSTAT_INC(received);
626 switch (ntohs(ar->ar_pro)) {
638 * ARP for Internet protocols on 10 Mb/s Ethernet.
639 * Algorithm is that given in RFC 826.
640 * In addition, a sanity check is performed on the sender
641 * protocol address, to catch impersonators.
642 * We no longer handle negotiations for use of trailer protocol:
643 * Formerly, ARP replied for protocol type ETHERTYPE_TRAIL sent
644 * along with IP replies if we wanted trailers sent to us,
645 * and also sent them in response to IP replies.
646 * This allowed either end to announce the desire to receive
648 * We no longer reply to requests for ETHERTYPE_TRAIL protocol either,
649 * but formerly didn't normally send requests.
651 static int log_arp_wrong_iface = 1;
652 static int log_arp_movements = 1;
653 static int log_arp_permanent_modify = 1;
654 static int allow_multicast = 0;
656 SYSCTL_INT(_net_link_ether_inet, OID_AUTO, log_arp_wrong_iface, CTLFLAG_RW,
657 &log_arp_wrong_iface, 0,
658 "log arp packets arriving on the wrong interface");
659 SYSCTL_INT(_net_link_ether_inet, OID_AUTO, log_arp_movements, CTLFLAG_RW,
660 &log_arp_movements, 0,
661 "log arp replies from MACs different than the one in the cache");
662 SYSCTL_INT(_net_link_ether_inet, OID_AUTO, log_arp_permanent_modify, CTLFLAG_RW,
663 &log_arp_permanent_modify, 0,
664 "log arp replies from MACs different than the one in the permanent arp entry");
665 SYSCTL_INT(_net_link_ether_inet, OID_AUTO, allow_multicast, CTLFLAG_RW,
666 &allow_multicast, 0, "accept multicast addresses");
669 in_arpinput(struct mbuf *m)
671 struct rm_priotracker in_ifa_tracker;
673 struct ifnet *ifp = m->m_pkthdr.rcvif;
674 struct llentry *la = NULL, *la_tmp;
676 struct in_ifaddr *ia;
678 struct in_addr isaddr, itaddr, myaddr;
679 u_int8_t *enaddr = NULL;
681 int bridged = 0, is_bridge = 0;
683 struct sockaddr_in sin;
684 struct sockaddr *dst;
685 struct nhop4_basic nh4;
687 sin.sin_len = sizeof(struct sockaddr_in);
688 sin.sin_family = AF_INET;
689 sin.sin_addr.s_addr = 0;
693 if (ifp->if_type == IFT_BRIDGE)
697 * We already have checked that mbuf contains enough contiguous data
698 * to hold entire arp message according to the arp header.
700 ah = mtod(m, struct arphdr *);
703 * ARP is only for IPv4 so we can reject packets with
704 * a protocol length not equal to an IPv4 address.
706 if (ah->ar_pln != sizeof(struct in_addr)) {
707 ARP_LOG(LOG_NOTICE, "requested protocol length != %zu\n",
708 sizeof(struct in_addr));
712 if (allow_multicast == 0 && ETHER_IS_MULTICAST(ar_sha(ah))) {
713 ARP_LOG(LOG_NOTICE, "%*D is multicast\n",
714 ifp->if_addrlen, (u_char *)ar_sha(ah), ":");
718 op = ntohs(ah->ar_op);
719 (void)memcpy(&isaddr, ar_spa(ah), sizeof (isaddr));
720 (void)memcpy(&itaddr, ar_tpa(ah), sizeof (itaddr));
722 if (op == ARPOP_REPLY)
723 ARPSTAT_INC(rxreplies);
726 * For a bridge, we want to check the address irrespective
727 * of the receive interface. (This will change slightly
728 * when we have clusters of interfaces).
730 IN_IFADDR_RLOCK(&in_ifa_tracker);
731 LIST_FOREACH(ia, INADDR_HASH(itaddr.s_addr), ia_hash) {
732 if (((bridged && ia->ia_ifp->if_bridge == ifp->if_bridge) ||
733 ia->ia_ifp == ifp) &&
734 itaddr.s_addr == ia->ia_addr.sin_addr.s_addr &&
735 (ia->ia_ifa.ifa_carp == NULL ||
736 (*carp_iamatch_p)(&ia->ia_ifa, &enaddr))) {
737 ifa_ref(&ia->ia_ifa);
738 IN_IFADDR_RUNLOCK(&in_ifa_tracker);
742 LIST_FOREACH(ia, INADDR_HASH(isaddr.s_addr), ia_hash)
743 if (((bridged && ia->ia_ifp->if_bridge == ifp->if_bridge) ||
744 ia->ia_ifp == ifp) &&
745 isaddr.s_addr == ia->ia_addr.sin_addr.s_addr) {
746 ifa_ref(&ia->ia_ifa);
747 IN_IFADDR_RUNLOCK(&in_ifa_tracker);
751 #define BDG_MEMBER_MATCHES_ARP(addr, ifp, ia) \
752 (ia->ia_ifp->if_bridge == ifp->if_softc && \
753 !bcmp(IF_LLADDR(ia->ia_ifp), IF_LLADDR(ifp), ifp->if_addrlen) && \
754 addr == ia->ia_addr.sin_addr.s_addr)
756 * Check the case when bridge shares its MAC address with
757 * some of its children, so packets are claimed by bridge
758 * itself (bridge_input() does it first), but they are really
759 * meant to be destined to the bridge member.
762 LIST_FOREACH(ia, INADDR_HASH(itaddr.s_addr), ia_hash) {
763 if (BDG_MEMBER_MATCHES_ARP(itaddr.s_addr, ifp, ia)) {
764 ifa_ref(&ia->ia_ifa);
766 IN_IFADDR_RUNLOCK(&in_ifa_tracker);
771 #undef BDG_MEMBER_MATCHES_ARP
772 IN_IFADDR_RUNLOCK(&in_ifa_tracker);
775 * No match, use the first inet address on the receive interface
776 * as a dummy address for the rest of the function.
779 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link)
780 if (ifa->ifa_addr->sa_family == AF_INET &&
781 (ifa->ifa_carp == NULL ||
782 (*carp_iamatch_p)(ifa, &enaddr))) {
785 IF_ADDR_RUNLOCK(ifp);
788 IF_ADDR_RUNLOCK(ifp);
791 * If bridging, fall back to using any inet address.
793 IN_IFADDR_RLOCK(&in_ifa_tracker);
794 if (!bridged || (ia = TAILQ_FIRST(&V_in_ifaddrhead)) == NULL) {
795 IN_IFADDR_RUNLOCK(&in_ifa_tracker);
798 ifa_ref(&ia->ia_ifa);
799 IN_IFADDR_RUNLOCK(&in_ifa_tracker);
802 enaddr = (u_int8_t *)IF_LLADDR(ifp);
803 carped = (ia->ia_ifa.ifa_carp != NULL);
804 myaddr = ia->ia_addr.sin_addr;
805 ifa_free(&ia->ia_ifa);
806 if (!bcmp(ar_sha(ah), enaddr, ifp->if_addrlen))
807 goto drop; /* it's from me, ignore it. */
808 if (!bcmp(ar_sha(ah), ifp->if_broadcastaddr, ifp->if_addrlen)) {
809 ARP_LOG(LOG_NOTICE, "link address is broadcast for IP address "
810 "%s!\n", inet_ntoa(isaddr));
814 if (ifp->if_addrlen != ah->ar_hln) {
815 ARP_LOG(LOG_WARNING, "from %*D: addr len: new %d, "
816 "i/f %d (ignored)\n", ifp->if_addrlen,
817 (u_char *) ar_sha(ah), ":", ah->ar_hln,
823 * Warn if another host is using the same IP address, but only if the
824 * IP address isn't 0.0.0.0, which is used for DHCP only, in which
825 * case we suppress the warning to avoid false positive complaints of
826 * potential misconfiguration.
828 if (!bridged && !carped && isaddr.s_addr == myaddr.s_addr &&
829 myaddr.s_addr != 0) {
830 ARP_LOG(LOG_ERR, "%*D is using my IP address %s on %s!\n",
831 ifp->if_addrlen, (u_char *)ar_sha(ah), ":",
832 inet_ntoa(isaddr), ifp->if_xname);
837 if (ifp->if_flags & IFF_STATICARP)
840 bzero(&sin, sizeof(sin));
841 sin.sin_len = sizeof(struct sockaddr_in);
842 sin.sin_family = AF_INET;
843 sin.sin_addr = isaddr;
844 dst = (struct sockaddr *)&sin;
845 IF_AFDATA_RLOCK(ifp);
846 la = lla_lookup(LLTABLE(ifp), LLE_EXCLUSIVE, dst);
847 IF_AFDATA_RUNLOCK(ifp);
849 arp_check_update_lle(ah, isaddr, ifp, bridged, la);
850 else if (itaddr.s_addr == myaddr.s_addr) {
852 * Request/reply to our address, but no lle exists yet.
853 * Try to create new llentry.
855 la = lltable_alloc_entry(LLTABLE(ifp), 0, dst);
859 * lle creation may fail if source address belongs
860 * to non-directly connected subnet. However, we
861 * will try to answer the request instead of dropping
866 lltable_set_entry_addr(ifp, la, ar_sha(ah));
868 IF_AFDATA_WLOCK(ifp);
870 la_tmp = lla_lookup(LLTABLE(ifp), LLE_EXCLUSIVE, dst);
873 * Check if lle still does not exists.
874 * If it does, that means that we either
875 * 1) have configured it explicitly, via
876 * 1a) 'arp -s' static entry or
877 * 1b) interface address static record
879 * 2) it was the result of sending first packet to-host
881 * 3) it was another arp reply packet we handled in
884 * In all cases except 3) we definitely need to prefer
885 * existing lle. For the sake of simplicity, prefer any
886 * existing lle over newly-create one.
889 lltable_link_entry(LLTABLE(ifp), la);
890 IF_AFDATA_WUNLOCK(ifp);
892 if (la_tmp == NULL) {
893 arp_mark_lle_reachable(la);
896 /* Free newly-create entry and handle packet */
897 lltable_free_entry(LLTABLE(ifp), la);
900 arp_check_update_lle(ah, isaddr, ifp, bridged, la);
901 /* arp_check_update_lle() returns @la unlocked */
906 if (op != ARPOP_REQUEST)
908 ARPSTAT_INC(rxrequests);
910 if (itaddr.s_addr == myaddr.s_addr) {
911 /* Shortcut.. the receiving interface is the target. */
912 (void)memcpy(ar_tha(ah), ar_sha(ah), ah->ar_hln);
913 (void)memcpy(ar_sha(ah), enaddr, ah->ar_hln);
915 struct llentry *lle = NULL;
917 sin.sin_addr = itaddr;
918 IF_AFDATA_RLOCK(ifp);
919 lle = lla_lookup(LLTABLE(ifp), 0, (struct sockaddr *)&sin);
920 IF_AFDATA_RUNLOCK(ifp);
922 if ((lle != NULL) && (lle->la_flags & LLE_PUB)) {
923 (void)memcpy(ar_tha(ah), ar_sha(ah), ah->ar_hln);
924 (void)memcpy(ar_sha(ah), &lle->ll_addr, ah->ar_hln);
934 /* XXX MRT use table 0 for arp reply */
935 if (fib4_lookup_nh_basic(0, itaddr, 0, 0, &nh4) != 0)
939 * Don't send proxies for nodes on the same interface
940 * as this one came out of, or we'll get into a fight
941 * over who claims what Ether address.
943 if (nh4.nh_ifp == ifp)
946 (void)memcpy(ar_tha(ah), ar_sha(ah), ah->ar_hln);
947 (void)memcpy(ar_sha(ah), enaddr, ah->ar_hln);
950 * Also check that the node which sent the ARP packet
951 * is on the interface we expect it to be on. This
952 * avoids ARP chaos if an interface is connected to the
956 /* XXX MRT use table 0 for arp checks */
957 if (fib4_lookup_nh_basic(0, isaddr, 0, 0, &nh4) != 0)
959 if (nh4.nh_ifp != ifp) {
960 ARP_LOG(LOG_INFO, "proxy: ignoring request"
962 inet_ntoa(isaddr), ifp->if_xname);
967 printf("arp: proxying for %s\n", inet_ntoa(itaddr));
972 if (itaddr.s_addr == myaddr.s_addr &&
973 IN_LINKLOCAL(ntohl(itaddr.s_addr))) {
974 /* RFC 3927 link-local IPv4; always reply by broadcast. */
975 #ifdef DEBUG_LINKLOCAL
976 printf("arp: sending reply for link-local addr %s\n",
979 m->m_flags |= M_BCAST;
980 m->m_flags &= ~M_MCAST;
982 /* default behaviour; never reply by broadcast. */
983 m->m_flags &= ~(M_BCAST|M_MCAST);
985 (void)memcpy(ar_tpa(ah), ar_spa(ah), ah->ar_pln);
986 (void)memcpy(ar_spa(ah), &itaddr, ah->ar_pln);
987 ah->ar_op = htons(ARPOP_REPLY);
988 ah->ar_pro = htons(ETHERTYPE_IP); /* let's be sure! */
989 m->m_len = sizeof(*ah) + (2 * ah->ar_pln) + (2 * ah->ar_hln);
990 m->m_pkthdr.len = m->m_len;
991 m->m_pkthdr.rcvif = NULL;
992 sa.sa_family = AF_ARP;
994 m_clrprotoflags(m); /* Avoid confusing lower layers. */
995 (*ifp->if_output)(ifp, m, &sa, NULL);
996 ARPSTAT_INC(txreplies);
1005 * Checks received arp data against existing @la.
1006 * Updates lle state/performs notification if necessary.
1009 arp_check_update_lle(struct arphdr *ah, struct in_addr isaddr, struct ifnet *ifp,
1010 int bridged, struct llentry *la)
1013 struct mbuf *m_hold, *m_hold_next;
1015 LLE_WLOCK_ASSERT(la);
1017 /* the following is not an error when doing bridging */
1018 if (!bridged && la->lle_tbl->llt_ifp != ifp) {
1019 if (log_arp_wrong_iface)
1020 ARP_LOG(LOG_WARNING, "%s is on %s "
1021 "but got reply from %*D on %s\n",
1023 la->lle_tbl->llt_ifp->if_xname,
1024 ifp->if_addrlen, (u_char *)ar_sha(ah), ":",
1029 if ((la->la_flags & LLE_VALID) &&
1030 bcmp(ar_sha(ah), &la->ll_addr, ifp->if_addrlen)) {
1031 if (la->la_flags & LLE_STATIC) {
1033 if (log_arp_permanent_modify)
1035 "%*D attempts to modify "
1036 "permanent entry for %s on %s\n",
1038 (u_char *)ar_sha(ah), ":",
1039 inet_ntoa(isaddr), ifp->if_xname);
1042 if (log_arp_movements) {
1043 ARP_LOG(LOG_INFO, "%s moved from %*D "
1047 (u_char *)&la->ll_addr, ":",
1048 ifp->if_addrlen, (u_char *)ar_sha(ah), ":",
1053 /* Check if something has changed */
1054 if (memcmp(&la->ll_addr, ar_sha(ah), ifp->if_addrlen) != 0 ||
1055 (la->la_flags & LLE_VALID) == 0) {
1056 /* Perform real LLE update */
1057 /* use afdata WLOCK to update fields */
1060 IF_AFDATA_WLOCK(ifp);
1064 * Since we droppped LLE lock, other thread might have deleted
1065 * this lle. Check and return
1067 if ((la->la_flags & LLE_DELETED) != 0) {
1068 IF_AFDATA_WUNLOCK(ifp);
1069 LLE_FREE_LOCKED(la);
1074 lltable_set_entry_addr(ifp, la, ar_sha(ah));
1076 IF_AFDATA_WUNLOCK(ifp);
1079 /* Clear fast path feedback request if set */
1083 arp_mark_lle_reachable(la);
1086 * The packets are all freed within the call to the output
1089 * NB: The lock MUST be released before the call to the
1092 if (la->la_hold != NULL) {
1093 m_hold = la->la_hold;
1096 lltable_fill_sa_entry(la, &sa);
1098 for (; m_hold != NULL; m_hold = m_hold_next) {
1099 m_hold_next = m_hold->m_nextpkt;
1100 m_hold->m_nextpkt = NULL;
1101 /* Avoid confusing lower layers. */
1102 m_clrprotoflags(m_hold);
1103 (*ifp->if_output)(ifp, m_hold, &sa, NULL);
1110 arp_mark_lle_reachable(struct llentry *la)
1112 int canceled, wtime;
1114 LLE_WLOCK_ASSERT(la);
1116 la->ln_state = ARP_LLINFO_REACHABLE;
1117 EVENTHANDLER_INVOKE(lle_event, la, LLENTRY_RESOLVED);
1119 if (!(la->la_flags & LLE_STATIC)) {
1121 la->la_expire = time_uptime + V_arpt_keep;
1122 wtime = V_arpt_keep - V_arp_maxtries * V_arpt_rexmit;
1124 wtime = V_arpt_keep;
1125 canceled = callout_reset(&la->lle_timer,
1126 hz * wtime, arptimer, la);
1131 la->la_preempt = V_arp_maxtries;
1135 * Add pernament link-layer record for given interface address.
1137 static __noinline void
1138 arp_add_ifa_lle(struct ifnet *ifp, const struct sockaddr *dst)
1140 struct llentry *lle, *lle_tmp;
1143 * Interface address LLE record is considered static
1144 * because kernel code relies on LLE_STATIC flag to check
1145 * if these entries can be rewriten by arp updates.
1147 lle = lltable_alloc_entry(LLTABLE(ifp), LLE_IFADDR | LLE_STATIC, dst);
1149 log(LOG_INFO, "arp_ifinit: cannot create arp "
1150 "entry for interface address\n");
1154 IF_AFDATA_WLOCK(ifp);
1156 /* Unlink any entry if exists */
1157 lle_tmp = lla_lookup(LLTABLE(ifp), LLE_EXCLUSIVE, dst);
1158 if (lle_tmp != NULL)
1159 lltable_unlink_entry(LLTABLE(ifp), lle_tmp);
1161 lltable_link_entry(LLTABLE(ifp), lle);
1162 IF_AFDATA_WUNLOCK(ifp);
1164 if (lle_tmp != NULL)
1165 EVENTHANDLER_INVOKE(lle_event, lle_tmp, LLENTRY_EXPIRED);
1167 EVENTHANDLER_INVOKE(lle_event, lle, LLENTRY_RESOLVED);
1169 if (lle_tmp != NULL)
1170 lltable_free_entry(LLTABLE(ifp), lle_tmp);
1174 arp_ifinit(struct ifnet *ifp, struct ifaddr *ifa)
1176 const struct sockaddr_in *dst_in;
1177 const struct sockaddr *dst;
1179 if (ifa->ifa_carp != NULL)
1182 dst = ifa->ifa_addr;
1183 dst_in = (const struct sockaddr_in *)dst;
1185 if (ntohl(dst_in->sin_addr.s_addr) == INADDR_ANY)
1187 arp_announce_ifaddr(ifp, dst_in->sin_addr, IF_LLADDR(ifp));
1189 arp_add_ifa_lle(ifp, dst);
1193 arp_announce_ifaddr(struct ifnet *ifp, struct in_addr addr, u_char *enaddr)
1196 if (ntohl(addr.s_addr) != INADDR_ANY)
1197 arprequest(ifp, &addr, &addr, enaddr);
1201 * Sends gratuitous ARPs for each ifaddr to notify other
1202 * nodes about the address change.
1204 static __noinline void
1205 arp_handle_ifllchange(struct ifnet *ifp)
1209 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
1210 if (ifa->ifa_addr->sa_family == AF_INET)
1211 arp_ifinit(ifp, ifa);
1216 * A handler for interface link layer address change event.
1218 static __noinline void
1219 arp_iflladdr(void *arg __unused, struct ifnet *ifp)
1222 if ((ifp->if_flags & IFF_UP) != 0)
1223 arp_handle_ifllchange(ifp);
1230 netisr_register(&arp_nh);
1231 if (IS_DEFAULT_VNET(curvnet))
1232 iflladdr_tag = EVENTHANDLER_REGISTER(iflladdr_event,
1233 arp_iflladdr, NULL, EVENTHANDLER_PRI_ANY);
1235 SYSINIT(arp, SI_SUB_PROTO_DOMAIN, SI_ORDER_ANY, arp_init, 0);