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_var.h>
67 #include <net/if_llatbl.h>
68 #include <netinet/if_ether.h>
70 #include <netinet/ip_carp.h>
73 #include <security/mac/mac_framework.h>
75 #define SIN(s) ((const struct sockaddr_in *)(s))
76 #define SDL(s) ((struct sockaddr_dl *)s)
78 SYSCTL_DECL(_net_link_ether);
79 static SYSCTL_NODE(_net_link_ether, PF_INET, inet, CTLFLAG_RW, 0, "");
80 static SYSCTL_NODE(_net_link_ether, PF_ARP, arp, CTLFLAG_RW, 0, "");
83 static VNET_DEFINE(int, arpt_keep) = (20*60); /* once resolved, good for 20
85 static VNET_DEFINE(int, arp_maxtries) = 5;
86 static VNET_DEFINE(int, arp_proxyall) = 0;
87 static VNET_DEFINE(int, arpt_down) = 20; /* keep incomplete entries for
89 VNET_PCPUSTAT_DEFINE(struct arpstat, arpstat); /* ARP statistics, see if_arp.h */
90 VNET_PCPUSTAT_SYSINIT(arpstat);
93 VNET_PCPUSTAT_SYSUNINIT(arpstat);
96 static VNET_DEFINE(int, arp_maxhold) = 1;
98 #define V_arpt_keep VNET(arpt_keep)
99 #define V_arpt_down VNET(arpt_down)
100 #define V_arp_maxtries VNET(arp_maxtries)
101 #define V_arp_proxyall VNET(arp_proxyall)
102 #define V_arp_maxhold VNET(arp_maxhold)
104 SYSCTL_INT(_net_link_ether_inet, OID_AUTO, max_age, CTLFLAG_VNET | CTLFLAG_RW,
105 &VNET_NAME(arpt_keep), 0,
106 "ARP entry lifetime in seconds");
107 SYSCTL_INT(_net_link_ether_inet, OID_AUTO, maxtries, CTLFLAG_VNET | CTLFLAG_RW,
108 &VNET_NAME(arp_maxtries), 0,
109 "ARP resolution attempts before returning error");
110 SYSCTL_INT(_net_link_ether_inet, OID_AUTO, proxyall, CTLFLAG_VNET | CTLFLAG_RW,
111 &VNET_NAME(arp_proxyall), 0,
112 "Enable proxy ARP for all suitable requests");
113 SYSCTL_INT(_net_link_ether_inet, OID_AUTO, wait, CTLFLAG_VNET | CTLFLAG_RW,
114 &VNET_NAME(arpt_down), 0,
115 "Incomplete ARP entry lifetime in seconds");
116 SYSCTL_VNET_PCPUSTAT(_net_link_ether_arp, OID_AUTO, stats, struct arpstat,
117 arpstat, "ARP statistics (struct arpstat, net/if_arp.h)");
118 SYSCTL_INT(_net_link_ether_inet, OID_AUTO, maxhold, CTLFLAG_VNET | CTLFLAG_RW,
119 &VNET_NAME(arp_maxhold), 0,
120 "Number of packets to hold per ARP entry");
122 static void arp_init(void);
123 static void arpintr(struct mbuf *);
124 static void arptimer(void *);
126 static void in_arpinput(struct mbuf *);
129 static void arp_check_update_lle(struct arphdr *ah, struct in_addr isaddr,
130 struct ifnet *ifp, int bridged, struct llentry *la);
131 static void arp_update_lle(struct arphdr *ah, struct ifnet *ifp,
133 static void arp_mark_lle_reachable(struct llentry *la);
136 static const struct netisr_handler arp_nh = {
138 .nh_handler = arpintr,
139 .nh_proto = NETISR_ARP,
140 .nh_policy = NETISR_POLICY_SOURCE,
144 * Timeout routine. Age arp_tab entries periodically.
149 struct llentry *lle = (struct llentry *)arg;
152 if (lle->la_flags & LLE_STATIC) {
156 if (callout_pending(&lle->lle_timer)) {
158 * Here we are a bit odd here in the treatment of
159 * active/pending. If the pending bit is set, it got
160 * rescheduled before I ran. The active
161 * bit we ignore, since if it was stopped
162 * in ll_tablefree() and was currently running
163 * it would have return 0 so the code would
164 * not have deleted it since the callout could
165 * not be stopped so we want to go through
166 * with the delete here now. If the callout
167 * was restarted, the pending bit will be back on and
168 * we just want to bail since the callout_reset would
169 * return 1 and our reference would have been removed
170 * by arpresolve() below.
175 ifp = lle->lle_tbl->llt_ifp;
176 CURVNET_SET(ifp->if_vnet);
178 if ((lle->la_flags & LLE_DELETED) == 0) {
181 if (lle->la_flags & LLE_VALID)
182 evt = LLENTRY_EXPIRED;
184 evt = LLENTRY_TIMEDOUT;
185 EVENTHANDLER_INVOKE(lle_event, lle, evt);
188 callout_stop(&lle->lle_timer);
190 /* XXX: LOR avoidance. We still have ref on lle. */
195 /* Guard against race with other llentry_free(). */
196 if (lle->la_flags & LLE_LINKED) {
200 pkts_dropped = llentry_free(lle);
201 ARPSTAT_ADD(dropped, pkts_dropped);
203 LLE_FREE_LOCKED(lle);
205 IF_AFDATA_UNLOCK(ifp);
207 ARPSTAT_INC(timeouts);
213 * Broadcast an ARP request. Caller specifies:
214 * - arp header source ip address
215 * - arp header target ip address
216 * - arp header source ethernet address
219 arprequest(struct ifnet *ifp, const struct in_addr *sip,
220 const struct in_addr *tip, u_char *enaddr)
225 u_char *carpaddr = NULL;
229 * The caller did not supply a source address, try to find
230 * a compatible one among those assigned to this interface.
235 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
236 if (ifa->ifa_addr->sa_family != AF_INET)
240 if ((*carp_iamatch_p)(ifa, &carpaddr) == 0)
242 sip = &IA_SIN(ifa)->sin_addr;
245 sip = &IA_SIN(ifa)->sin_addr;
248 if (0 == ((sip->s_addr ^ tip->s_addr) &
249 IA_MASKSIN(ifa)->sin_addr.s_addr))
250 break; /* found it. */
252 IF_ADDR_RUNLOCK(ifp);
254 printf("%s: cannot find matching address\n", __func__);
259 enaddr = carpaddr ? carpaddr : (u_char *)IF_LLADDR(ifp);
261 if ((m = m_gethdr(M_NOWAIT, MT_DATA)) == NULL)
263 m->m_len = sizeof(*ah) + 2 * sizeof(struct in_addr) +
265 m->m_pkthdr.len = m->m_len;
266 M_ALIGN(m, m->m_len);
267 ah = mtod(m, struct arphdr *);
268 bzero((caddr_t)ah, m->m_len);
270 mac_netinet_arp_send(ifp, m);
272 ah->ar_pro = htons(ETHERTYPE_IP);
273 ah->ar_hln = ifp->if_addrlen; /* hardware address length */
274 ah->ar_pln = sizeof(struct in_addr); /* protocol address length */
275 ah->ar_op = htons(ARPOP_REQUEST);
276 bcopy(enaddr, ar_sha(ah), ah->ar_hln);
277 bcopy(sip, ar_spa(ah), ah->ar_pln);
278 bcopy(tip, ar_tpa(ah), ah->ar_pln);
279 sa.sa_family = AF_ARP;
281 m->m_flags |= M_BCAST;
282 m_clrprotoflags(m); /* Avoid confusing lower layers. */
283 (*ifp->if_output)(ifp, m, &sa, NULL);
284 ARPSTAT_INC(txrequests);
288 * Resolve an IP address into an ethernet address - heavy version.
289 * Used internally by arpresolve().
290 * We have already checked than we can't use existing lle without
291 * modification so we have to acquire LLE_EXCLUSIVE lle lock.
293 * On success, desten and flags are filled in and the function returns 0;
294 * If the packet must be held pending resolution, we return EWOULDBLOCK
295 * On other errors, we return the corresponding error code.
296 * Note that m_freem() handles NULL.
299 arpresolve_full(struct ifnet *ifp, int is_gw, int create, struct mbuf *m,
300 const struct sockaddr *dst, u_char *desten, uint32_t *pflags)
302 struct llentry *la = NULL, *la_tmp;
303 struct mbuf *curr = NULL;
304 struct mbuf *next = NULL;
311 IF_AFDATA_RLOCK(ifp);
312 la = lla_lookup(LLTABLE(ifp), LLE_EXCLUSIVE, dst);
313 IF_AFDATA_RUNLOCK(ifp);
315 if (la == NULL && (ifp->if_flags & (IFF_NOARP | IFF_STATICARP)) == 0) {
316 la = lltable_alloc_entry(LLTABLE(ifp), 0, dst);
319 "arpresolve: can't allocate llinfo for %s on %s\n",
320 inet_ntoa(SIN(dst)->sin_addr), if_name(ifp));
325 IF_AFDATA_WLOCK(ifp);
327 la_tmp = lla_lookup(LLTABLE(ifp), LLE_EXCLUSIVE, dst);
328 /* Prefer ANY existing lle over newly-created one */
330 lltable_link_entry(LLTABLE(ifp), la);
331 IF_AFDATA_WUNLOCK(ifp);
332 if (la_tmp != NULL) {
333 lltable_free_entry(LLTABLE(ifp), la);
342 if ((la->la_flags & LLE_VALID) &&
343 ((la->la_flags & LLE_STATIC) || la->la_expire > time_uptime)) {
344 bcopy(&la->ll_addr, desten, ifp->if_addrlen);
347 * If entry has an expiry time and it is approaching,
348 * see if we need to send an ARP request within this
349 * arpt_down interval.
351 if (!(la->la_flags & LLE_STATIC) &&
352 time_uptime + la->la_preempt > la->la_expire) {
358 *pflags = la->la_flags;
363 arprequest(ifp, NULL, &SIN(dst)->sin_addr, NULL);
368 renew = (la->la_asked == 0 || la->la_expire != time_uptime);
370 * There is an arptab entry, but no ethernet address
371 * response yet. Add the mbuf to the list, dropping
372 * the oldest packet if we have exceeded the system
376 if (la->la_numheld >= V_arp_maxhold) {
377 if (la->la_hold != NULL) {
378 next = la->la_hold->m_nextpkt;
379 m_freem(la->la_hold);
382 ARPSTAT_INC(dropped);
385 if (la->la_hold != NULL) {
387 while (curr->m_nextpkt != NULL)
388 curr = curr->m_nextpkt;
395 * Return EWOULDBLOCK if we have tried less than arp_maxtries. It
396 * will be masked by ether_output(). Return EHOSTDOWN/EHOSTUNREACH
397 * if we have already sent arp_maxtries ARP requests. Retransmit the
398 * ARP request, but not faster than one request per second.
400 if (la->la_asked < V_arp_maxtries)
401 error = EWOULDBLOCK; /* First request. */
403 error = is_gw != 0 ? EHOSTUNREACH : EHOSTDOWN;
409 la->la_expire = time_uptime;
410 canceled = callout_reset(&la->lle_timer, hz * V_arpt_down,
416 arprequest(ifp, NULL, &SIN(dst)->sin_addr, NULL);
425 * Resolve an IP address into an ethernet address.
427 * ifp is the interface we use
428 * is_gw != 0 if @dst represents gateway to some destination
429 * m is the mbuf. May be NULL if we don't have a packet.
430 * dst is the next hop,
431 * desten is the storage to put LL address.
432 * flags returns lle entry flags.
434 * On success, desten and flags are filled in and the function returns 0;
435 * If the packet must be held pending resolution, we return EWOULDBLOCK
436 * On other errors, we return the corresponding error code.
437 * Note that m_freem() handles NULL.
440 arpresolve(struct ifnet *ifp, int is_gw, struct mbuf *m,
441 const struct sockaddr *dst, u_char *desten, uint32_t *pflags)
443 struct llentry *la = 0;
450 if (m->m_flags & M_BCAST) {
453 ifp->if_broadcastaddr, ifp->if_addrlen);
456 if (m->m_flags & M_MCAST) {
458 ETHER_MAP_IP_MULTICAST(&SIN(dst)->sin_addr, desten);
463 IF_AFDATA_RLOCK(ifp);
464 la = lla_lookup(LLTABLE(ifp), 0, dst);
465 IF_AFDATA_RUNLOCK(ifp);
468 return (arpresolve_full(ifp, is_gw, 1, m, dst, desten, pflags));
470 if ((la->la_flags & LLE_VALID) &&
471 ((la->la_flags & LLE_STATIC) || la->la_expire > time_uptime)) {
472 bcopy(&la->ll_addr, desten, ifp->if_addrlen);
475 * If entry has an expiry time and it is approaching,
476 * see if we need to send an ARP request within this
477 * arpt_down interval.
479 if (!(la->la_flags & LLE_STATIC) &&
480 time_uptime + la->la_preempt > la->la_expire) {
486 *pflags = la->la_flags;
491 arprequest(ifp, NULL, &SIN(dst)->sin_addr, NULL);
497 return (arpresolve_full(ifp, is_gw, 0, m, dst, desten, pflags));
501 * Common length and type checks are done here,
502 * then the protocol-specific routine is called.
505 arpintr(struct mbuf *m)
511 if (m->m_len < sizeof(struct arphdr) &&
512 ((m = m_pullup(m, sizeof(struct arphdr))) == NULL)) {
513 log(LOG_NOTICE, "arp: runt packet -- m_pullup failed\n");
516 ar = mtod(m, struct arphdr *);
518 /* Check if length is sufficient */
519 if ((m = m_pullup(m, arphdr_len(ar))) == NULL) {
520 log(LOG_NOTICE, "arp: short header received\n");
523 ar = mtod(m, struct arphdr *);
527 switch (ntohs(ar->ar_hrd)) {
529 hlen = ETHER_ADDR_LEN; /* RFC 826 */
533 hlen = 6; /* RFC 1390, FDDI_ADDR_LEN */
537 hlen = 1; /* RFC 1201, ARC_ADDR_LEN */
540 case ARPHRD_INFINIBAND:
541 hlen = 20; /* RFC 4391, INFINIBAND_ALEN */
542 layer = "infiniband";
544 case ARPHRD_IEEE1394:
545 hlen = 0; /* SHALL be 16 */ /* RFC 2734 */
549 * Restrict too long harware addresses.
550 * Currently we are capable of handling 20-byte
551 * addresses ( sizeof(lle->ll_addr) )
553 if (ar->ar_hln >= 20)
557 log(LOG_NOTICE, "arp: unknown hardware address format (0x%2d)\n",
563 if (hlen != 0 && hlen != ar->ar_hln) {
564 log(LOG_NOTICE, "arp: bad %s header length: %d\n", layer,
570 ARPSTAT_INC(received);
571 switch (ntohs(ar->ar_pro)) {
583 * ARP for Internet protocols on 10 Mb/s Ethernet.
584 * Algorithm is that given in RFC 826.
585 * In addition, a sanity check is performed on the sender
586 * protocol address, to catch impersonators.
587 * We no longer handle negotiations for use of trailer protocol:
588 * Formerly, ARP replied for protocol type ETHERTYPE_TRAIL sent
589 * along with IP replies if we wanted trailers sent to us,
590 * and also sent them in response to IP replies.
591 * This allowed either end to announce the desire to receive
593 * We no longer reply to requests for ETHERTYPE_TRAIL protocol either,
594 * but formerly didn't normally send requests.
596 static int log_arp_wrong_iface = 1;
597 static int log_arp_movements = 1;
598 static int log_arp_permanent_modify = 1;
599 static int allow_multicast = 0;
600 static struct timeval arp_lastlog;
601 static int arp_curpps;
602 static int arp_maxpps = 1;
604 SYSCTL_INT(_net_link_ether_inet, OID_AUTO, log_arp_wrong_iface, CTLFLAG_RW,
605 &log_arp_wrong_iface, 0,
606 "log arp packets arriving on the wrong interface");
607 SYSCTL_INT(_net_link_ether_inet, OID_AUTO, log_arp_movements, CTLFLAG_RW,
608 &log_arp_movements, 0,
609 "log arp replies from MACs different than the one in the cache");
610 SYSCTL_INT(_net_link_ether_inet, OID_AUTO, log_arp_permanent_modify, CTLFLAG_RW,
611 &log_arp_permanent_modify, 0,
612 "log arp replies from MACs different than the one in the permanent arp entry");
613 SYSCTL_INT(_net_link_ether_inet, OID_AUTO, allow_multicast, CTLFLAG_RW,
614 &allow_multicast, 0, "accept multicast addresses");
615 SYSCTL_INT(_net_link_ether_inet, OID_AUTO, max_log_per_second,
616 CTLFLAG_RW, &arp_maxpps, 0,
617 "Maximum number of remotely triggered ARP messages that can be "
618 "logged per second");
620 #define ARP_LOG(pri, ...) do { \
621 if (ppsratecheck(&arp_lastlog, &arp_curpps, arp_maxpps)) \
622 log((pri), "arp: " __VA_ARGS__); \
626 in_arpinput(struct mbuf *m)
628 struct rm_priotracker in_ifa_tracker;
630 struct ifnet *ifp = m->m_pkthdr.rcvif;
631 struct llentry *la = NULL, *la_tmp;
634 struct in_ifaddr *ia;
636 struct in_addr isaddr, itaddr, myaddr;
637 u_int8_t *enaddr = NULL;
640 int bridged = 0, is_bridge = 0;
642 struct sockaddr_in sin;
643 struct sockaddr *dst;
644 sin.sin_len = sizeof(struct sockaddr_in);
645 sin.sin_family = AF_INET;
646 sin.sin_addr.s_addr = 0;
650 if (ifp->if_type == IFT_BRIDGE)
653 req_len = arphdr_len2(ifp->if_addrlen, sizeof(struct in_addr));
654 if (m->m_len < req_len && (m = m_pullup(m, req_len)) == NULL) {
655 ARP_LOG(LOG_NOTICE, "runt packet -- m_pullup failed\n");
659 ah = mtod(m, struct arphdr *);
661 * ARP is only for IPv4 so we can reject packets with
662 * a protocol length not equal to an IPv4 address.
664 if (ah->ar_pln != sizeof(struct in_addr)) {
665 ARP_LOG(LOG_NOTICE, "requested protocol length != %zu\n",
666 sizeof(struct in_addr));
670 if (allow_multicast == 0 && ETHER_IS_MULTICAST(ar_sha(ah))) {
671 ARP_LOG(LOG_NOTICE, "%*D is multicast\n",
672 ifp->if_addrlen, (u_char *)ar_sha(ah), ":");
676 op = ntohs(ah->ar_op);
677 (void)memcpy(&isaddr, ar_spa(ah), sizeof (isaddr));
678 (void)memcpy(&itaddr, ar_tpa(ah), sizeof (itaddr));
680 if (op == ARPOP_REPLY)
681 ARPSTAT_INC(rxreplies);
684 * For a bridge, we want to check the address irrespective
685 * of the receive interface. (This will change slightly
686 * when we have clusters of interfaces).
688 IN_IFADDR_RLOCK(&in_ifa_tracker);
689 LIST_FOREACH(ia, INADDR_HASH(itaddr.s_addr), ia_hash) {
690 if (((bridged && ia->ia_ifp->if_bridge == ifp->if_bridge) ||
691 ia->ia_ifp == ifp) &&
692 itaddr.s_addr == ia->ia_addr.sin_addr.s_addr &&
693 (ia->ia_ifa.ifa_carp == NULL ||
694 (*carp_iamatch_p)(&ia->ia_ifa, &enaddr))) {
695 ifa_ref(&ia->ia_ifa);
696 IN_IFADDR_RUNLOCK(&in_ifa_tracker);
700 LIST_FOREACH(ia, INADDR_HASH(isaddr.s_addr), ia_hash)
701 if (((bridged && ia->ia_ifp->if_bridge == ifp->if_bridge) ||
702 ia->ia_ifp == ifp) &&
703 isaddr.s_addr == ia->ia_addr.sin_addr.s_addr) {
704 ifa_ref(&ia->ia_ifa);
705 IN_IFADDR_RUNLOCK(&in_ifa_tracker);
709 #define BDG_MEMBER_MATCHES_ARP(addr, ifp, ia) \
710 (ia->ia_ifp->if_bridge == ifp->if_softc && \
711 !bcmp(IF_LLADDR(ia->ia_ifp), IF_LLADDR(ifp), ifp->if_addrlen) && \
712 addr == ia->ia_addr.sin_addr.s_addr)
714 * Check the case when bridge shares its MAC address with
715 * some of its children, so packets are claimed by bridge
716 * itself (bridge_input() does it first), but they are really
717 * meant to be destined to the bridge member.
720 LIST_FOREACH(ia, INADDR_HASH(itaddr.s_addr), ia_hash) {
721 if (BDG_MEMBER_MATCHES_ARP(itaddr.s_addr, ifp, ia)) {
722 ifa_ref(&ia->ia_ifa);
724 IN_IFADDR_RUNLOCK(&in_ifa_tracker);
729 #undef BDG_MEMBER_MATCHES_ARP
730 IN_IFADDR_RUNLOCK(&in_ifa_tracker);
733 * No match, use the first inet address on the receive interface
734 * as a dummy address for the rest of the function.
737 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link)
738 if (ifa->ifa_addr->sa_family == AF_INET &&
739 (ifa->ifa_carp == NULL ||
740 (*carp_iamatch_p)(ifa, &enaddr))) {
743 IF_ADDR_RUNLOCK(ifp);
746 IF_ADDR_RUNLOCK(ifp);
749 * If bridging, fall back to using any inet address.
751 IN_IFADDR_RLOCK(&in_ifa_tracker);
752 if (!bridged || (ia = TAILQ_FIRST(&V_in_ifaddrhead)) == NULL) {
753 IN_IFADDR_RUNLOCK(&in_ifa_tracker);
756 ifa_ref(&ia->ia_ifa);
757 IN_IFADDR_RUNLOCK(&in_ifa_tracker);
760 enaddr = (u_int8_t *)IF_LLADDR(ifp);
761 carped = (ia->ia_ifa.ifa_carp != NULL);
762 myaddr = ia->ia_addr.sin_addr;
763 ifa_free(&ia->ia_ifa);
764 if (!bcmp(ar_sha(ah), enaddr, ifp->if_addrlen))
765 goto drop; /* it's from me, ignore it. */
766 if (!bcmp(ar_sha(ah), ifp->if_broadcastaddr, ifp->if_addrlen)) {
767 ARP_LOG(LOG_NOTICE, "link address is broadcast for IP address "
768 "%s!\n", inet_ntoa(isaddr));
772 if (ifp->if_addrlen != ah->ar_hln) {
773 ARP_LOG(LOG_WARNING, "from %*D: addr len: new %d, "
774 "i/f %d (ignored)\n", ifp->if_addrlen,
775 (u_char *) ar_sha(ah), ":", ah->ar_hln,
781 * Warn if another host is using the same IP address, but only if the
782 * IP address isn't 0.0.0.0, which is used for DHCP only, in which
783 * case we suppress the warning to avoid false positive complaints of
784 * potential misconfiguration.
786 if (!bridged && !carped && isaddr.s_addr == myaddr.s_addr &&
787 myaddr.s_addr != 0) {
788 ARP_LOG(LOG_ERR, "%*D is using my IP address %s on %s!\n",
789 ifp->if_addrlen, (u_char *)ar_sha(ah), ":",
790 inet_ntoa(isaddr), ifp->if_xname);
795 if (ifp->if_flags & IFF_STATICARP)
798 bzero(&sin, sizeof(sin));
799 sin.sin_len = sizeof(struct sockaddr_in);
800 sin.sin_family = AF_INET;
801 sin.sin_addr = isaddr;
802 dst = (struct sockaddr *)&sin;
803 IF_AFDATA_RLOCK(ifp);
804 la = lla_lookup(LLTABLE(ifp), LLE_EXCLUSIVE, dst);
805 IF_AFDATA_RUNLOCK(ifp);
807 arp_check_update_lle(ah, isaddr, ifp, bridged, la);
808 else if (itaddr.s_addr == myaddr.s_addr) {
810 * Reply to our address, but no lle exists yet.
811 * do we really have to create an entry?
813 la = lltable_alloc_entry(LLTABLE(ifp), 0, dst);
816 arp_update_lle(ah, ifp, la);
818 IF_AFDATA_WLOCK(ifp);
820 la_tmp = lla_lookup(LLTABLE(ifp), LLE_EXCLUSIVE, dst);
823 * Check if lle still does not exists.
824 * If it does, that means that we either
825 * 1) have configured it explicitly, via
826 * 1a) 'arp -s' static entry or
827 * 1b) interface address static record
829 * 2) it was the result of sending first packet to-host
831 * 3) it was another arp reply packet we handled in
834 * In all cases except 3) we definitely need to prefer
835 * existing lle. For the sake of simplicity, prefer any
836 * existing lle over newly-create one.
839 lltable_link_entry(LLTABLE(ifp), la);
840 IF_AFDATA_WUNLOCK(ifp);
842 if (la_tmp == NULL) {
843 arp_mark_lle_reachable(la);
846 /* Free newly-create entry and handle packet */
847 lltable_free_entry(LLTABLE(ifp), la);
850 arp_check_update_lle(ah, isaddr, ifp, bridged, la);
851 /* arp_check_update_lle() returns @la unlocked */
856 if (op != ARPOP_REQUEST)
858 ARPSTAT_INC(rxrequests);
860 if (itaddr.s_addr == myaddr.s_addr) {
861 /* Shortcut.. the receiving interface is the target. */
862 (void)memcpy(ar_tha(ah), ar_sha(ah), ah->ar_hln);
863 (void)memcpy(ar_sha(ah), enaddr, ah->ar_hln);
865 struct llentry *lle = NULL;
867 sin.sin_addr = itaddr;
868 IF_AFDATA_RLOCK(ifp);
869 lle = lla_lookup(LLTABLE(ifp), 0, (struct sockaddr *)&sin);
870 IF_AFDATA_RUNLOCK(ifp);
872 if ((lle != NULL) && (lle->la_flags & LLE_PUB)) {
873 (void)memcpy(ar_tha(ah), ar_sha(ah), ah->ar_hln);
874 (void)memcpy(ar_sha(ah), &lle->ll_addr, ah->ar_hln);
884 sin.sin_addr = itaddr;
885 /* XXX MRT use table 0 for arp reply */
886 rt = in_rtalloc1((struct sockaddr *)&sin, 0, 0UL, 0);
891 * Don't send proxies for nodes on the same interface
892 * as this one came out of, or we'll get into a fight
893 * over who claims what Ether address.
895 if (!rt->rt_ifp || rt->rt_ifp == ifp) {
901 (void)memcpy(ar_tha(ah), ar_sha(ah), ah->ar_hln);
902 (void)memcpy(ar_sha(ah), enaddr, ah->ar_hln);
905 * Also check that the node which sent the ARP packet
906 * is on the interface we expect it to be on. This
907 * avoids ARP chaos if an interface is connected to the
910 sin.sin_addr = isaddr;
912 /* XXX MRT use table 0 for arp checks */
913 rt = in_rtalloc1((struct sockaddr *)&sin, 0, 0UL, 0);
916 if (rt->rt_ifp != ifp) {
917 ARP_LOG(LOG_INFO, "proxy: ignoring request"
918 " from %s via %s, expecting %s\n",
919 inet_ntoa(isaddr), ifp->if_xname,
920 rt->rt_ifp->if_xname);
927 printf("arp: proxying for %s\n", inet_ntoa(itaddr));
932 if (itaddr.s_addr == myaddr.s_addr &&
933 IN_LINKLOCAL(ntohl(itaddr.s_addr))) {
934 /* RFC 3927 link-local IPv4; always reply by broadcast. */
935 #ifdef DEBUG_LINKLOCAL
936 printf("arp: sending reply for link-local addr %s\n",
939 m->m_flags |= M_BCAST;
940 m->m_flags &= ~M_MCAST;
942 /* default behaviour; never reply by broadcast. */
943 m->m_flags &= ~(M_BCAST|M_MCAST);
945 (void)memcpy(ar_tpa(ah), ar_spa(ah), ah->ar_pln);
946 (void)memcpy(ar_spa(ah), &itaddr, ah->ar_pln);
947 ah->ar_op = htons(ARPOP_REPLY);
948 ah->ar_pro = htons(ETHERTYPE_IP); /* let's be sure! */
949 m->m_len = sizeof(*ah) + (2 * ah->ar_pln) + (2 * ah->ar_hln);
950 m->m_pkthdr.len = m->m_len;
951 m->m_pkthdr.rcvif = NULL;
952 sa.sa_family = AF_ARP;
954 m_clrprotoflags(m); /* Avoid confusing lower layers. */
955 (*ifp->if_output)(ifp, m, &sa, NULL);
956 ARPSTAT_INC(txreplies);
965 * Checks received arp data against existing @la.
966 * Updates lle state/performs notification if necessary.
969 arp_check_update_lle(struct arphdr *ah, struct in_addr isaddr, struct ifnet *ifp,
970 int bridged, struct llentry *la)
973 struct mbuf *m_hold, *m_hold_next;
975 LLE_WLOCK_ASSERT(la);
977 /* the following is not an error when doing bridging */
978 if (!bridged && la->lle_tbl->llt_ifp != ifp) {
979 if (log_arp_wrong_iface)
980 ARP_LOG(LOG_WARNING, "%s is on %s "
981 "but got reply from %*D on %s\n",
983 la->lle_tbl->llt_ifp->if_xname,
984 ifp->if_addrlen, (u_char *)ar_sha(ah), ":",
989 if ((la->la_flags & LLE_VALID) &&
990 bcmp(ar_sha(ah), &la->ll_addr, ifp->if_addrlen)) {
991 if (la->la_flags & LLE_STATIC) {
993 if (log_arp_permanent_modify)
995 "%*D attempts to modify "
996 "permanent entry for %s on %s\n",
998 (u_char *)ar_sha(ah), ":",
999 inet_ntoa(isaddr), ifp->if_xname);
1002 if (log_arp_movements) {
1003 ARP_LOG(LOG_INFO, "%s moved from %*D "
1007 (u_char *)&la->ll_addr, ":",
1008 ifp->if_addrlen, (u_char *)ar_sha(ah), ":",
1013 /* Check if something has changed */
1014 if (memcmp(&la->ll_addr, ar_sha(ah), ifp->if_addrlen) != 0 ||
1015 (la->la_flags & LLE_VALID) == 0) {
1016 /* Perform real LLE update */
1017 /* use afdata WLOCK to update fields */
1020 IF_AFDATA_WLOCK(ifp);
1024 * Since we droppped LLE lock, other thread might have deleted
1025 * this lle. Check and return
1027 if ((la->la_flags & LLE_DELETED) != 0) {
1028 IF_AFDATA_WUNLOCK(ifp);
1029 LLE_FREE_LOCKED(la);
1034 arp_update_lle(ah, ifp, la);
1036 IF_AFDATA_WUNLOCK(ifp);
1040 arp_mark_lle_reachable(la);
1043 * The packets are all freed within the call to the output
1046 * NB: The lock MUST be released before the call to the
1049 if (la->la_hold != NULL) {
1050 m_hold = la->la_hold;
1053 lltable_fill_sa_entry(la, &sa);
1055 for (; m_hold != NULL; m_hold = m_hold_next) {
1056 m_hold_next = m_hold->m_nextpkt;
1057 m_hold->m_nextpkt = NULL;
1058 /* Avoid confusing lower layers. */
1059 m_clrprotoflags(m_hold);
1060 (*ifp->if_output)(ifp, m_hold, &sa, NULL);
1067 * Updates @la fields used by fast path code.
1070 arp_update_lle(struct arphdr *ah, struct ifnet *ifp, struct llentry *la)
1073 memcpy(&la->ll_addr, ar_sha(ah), ifp->if_addrlen);
1074 la->la_flags |= LLE_VALID;
1078 arp_mark_lle_reachable(struct llentry *la)
1082 LLE_WLOCK_ASSERT(la);
1084 EVENTHANDLER_INVOKE(lle_event, la, LLENTRY_RESOLVED);
1086 if (!(la->la_flags & LLE_STATIC)) {
1088 la->la_expire = time_uptime + V_arpt_keep;
1089 canceled = callout_reset(&la->lle_timer,
1090 hz * V_arpt_keep, arptimer, la);
1095 la->la_preempt = V_arp_maxtries;
1099 arp_ifinit(struct ifnet *ifp, struct ifaddr *ifa)
1101 struct llentry *lle, *lle_tmp;
1102 struct sockaddr_in *dst_in;
1103 struct sockaddr *dst;
1105 if (ifa->ifa_carp != NULL)
1108 ifa->ifa_rtrequest = NULL;
1110 dst_in = IA_SIN(ifa);
1111 dst = (struct sockaddr *)dst_in;
1113 if (ntohl(IA_SIN(ifa)->sin_addr.s_addr) == INADDR_ANY)
1116 arprequest(ifp, &IA_SIN(ifa)->sin_addr,
1117 &IA_SIN(ifa)->sin_addr, IF_LLADDR(ifp));
1120 * Interface address LLE record is considered static
1121 * because kernel code relies on LLE_STATIC flag to check
1122 * if these entries can be rewriten by arp updates.
1124 lle = lltable_alloc_entry(LLTABLE(ifp), LLE_IFADDR | LLE_STATIC, dst);
1126 log(LOG_INFO, "arp_ifinit: cannot create arp "
1127 "entry for interface address\n");
1131 IF_AFDATA_WLOCK(ifp);
1133 /* Unlink any entry if exists */
1134 lle_tmp = lla_lookup(LLTABLE(ifp), LLE_EXCLUSIVE, dst);
1135 if (lle_tmp != NULL)
1136 lltable_unlink_entry(LLTABLE(ifp), lle_tmp);
1138 lltable_link_entry(LLTABLE(ifp), lle);
1139 IF_AFDATA_WUNLOCK(ifp);
1141 if (lle_tmp != NULL)
1142 EVENTHANDLER_INVOKE(lle_event, lle_tmp, LLENTRY_EXPIRED);
1144 EVENTHANDLER_INVOKE(lle_event, lle, LLENTRY_RESOLVED);
1146 if (lle_tmp != NULL)
1147 lltable_free_entry(LLTABLE(ifp), lle_tmp);
1151 arp_ifinit2(struct ifnet *ifp, struct ifaddr *ifa, u_char *enaddr)
1153 if (ntohl(IA_SIN(ifa)->sin_addr.s_addr) != INADDR_ANY)
1154 arprequest(ifp, &IA_SIN(ifa)->sin_addr,
1155 &IA_SIN(ifa)->sin_addr, enaddr);
1156 ifa->ifa_rtrequest = NULL;
1163 netisr_register(&arp_nh);
1165 SYSINIT(arp, SI_SUB_PROTO_DOMAIN, SI_ORDER_ANY, arp_init, 0);