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))
77 static struct timeval arp_lastlog;
78 static int arp_curpps;
79 static int arp_maxpps = 1;
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, "");
86 static VNET_DEFINE(int, arpt_keep) = (20*60); /* once resolved, good for 20
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
92 VNET_PCPUSTAT_DEFINE(struct arpstat, arpstat); /* ARP statistics, see if_arp.h */
93 VNET_PCPUSTAT_SYSINIT(arpstat);
96 VNET_PCPUSTAT_SYSUNINIT(arpstat);
99 static VNET_DEFINE(int, arp_maxhold) = 1;
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)
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");
129 #define ARP_LOG(pri, ...) do { \
130 if (ppsratecheck(&arp_lastlog, &arp_curpps, arp_maxpps)) \
131 log((pri), "arp: " __VA_ARGS__); \
135 static void arp_init(void);
136 static void arpintr(struct mbuf *);
137 static void arptimer(void *);
139 static void in_arpinput(struct mbuf *);
142 static void arp_check_update_lle(struct arphdr *ah, struct in_addr isaddr,
143 struct ifnet *ifp, int bridged, struct llentry *la);
144 static void arp_update_lle(struct arphdr *ah, struct ifnet *ifp,
146 static void arp_mark_lle_reachable(struct llentry *la);
149 static const struct netisr_handler arp_nh = {
151 .nh_handler = arpintr,
152 .nh_proto = NETISR_ARP,
153 .nh_policy = NETISR_POLICY_SOURCE,
157 * Timeout routine. Age arp_tab entries periodically.
162 struct llentry *lle = (struct llentry *)arg;
165 if (lle->la_flags & LLE_STATIC) {
169 if (callout_pending(&lle->lle_timer)) {
171 * Here we are a bit odd here in the treatment of
172 * active/pending. If the pending bit is set, it got
173 * rescheduled before I ran. The active
174 * bit we ignore, since if it was stopped
175 * in ll_tablefree() and was currently running
176 * it would have return 0 so the code would
177 * not have deleted it since the callout could
178 * not be stopped so we want to go through
179 * with the delete here now. If the callout
180 * was restarted, the pending bit will be back on and
181 * we just want to bail since the callout_reset would
182 * return 1 and our reference would have been removed
183 * by arpresolve() below.
188 ifp = lle->lle_tbl->llt_ifp;
189 CURVNET_SET(ifp->if_vnet);
191 if ((lle->la_flags & LLE_DELETED) == 0) {
194 if (lle->la_flags & LLE_VALID)
195 evt = LLENTRY_EXPIRED;
197 evt = LLENTRY_TIMEDOUT;
198 EVENTHANDLER_INVOKE(lle_event, lle, evt);
201 callout_stop(&lle->lle_timer);
203 /* XXX: LOR avoidance. We still have ref on lle. */
208 /* Guard against race with other llentry_free(). */
209 if (lle->la_flags & LLE_LINKED) {
211 lltable_unlink_entry(lle->lle_tbl, lle);
213 IF_AFDATA_UNLOCK(ifp);
215 size_t pkts_dropped = llentry_free(lle);
217 ARPSTAT_ADD(dropped, pkts_dropped);
218 ARPSTAT_INC(timeouts);
224 * Broadcast an ARP request. Caller specifies:
225 * - arp header source ip address
226 * - arp header target ip address
227 * - arp header source ethernet address
230 arprequest(struct ifnet *ifp, const struct in_addr *sip,
231 const struct in_addr *tip, u_char *enaddr)
236 u_char *carpaddr = NULL;
240 * The caller did not supply a source address, try to find
241 * a compatible one among those assigned to this interface.
246 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
247 if (ifa->ifa_addr->sa_family != AF_INET)
251 if ((*carp_iamatch_p)(ifa, &carpaddr) == 0)
253 sip = &IA_SIN(ifa)->sin_addr;
256 sip = &IA_SIN(ifa)->sin_addr;
259 if (0 == ((sip->s_addr ^ tip->s_addr) &
260 IA_MASKSIN(ifa)->sin_addr.s_addr))
261 break; /* found it. */
263 IF_ADDR_RUNLOCK(ifp);
265 printf("%s: cannot find matching address\n", __func__);
270 enaddr = carpaddr ? carpaddr : (u_char *)IF_LLADDR(ifp);
272 if ((m = m_gethdr(M_NOWAIT, MT_DATA)) == NULL)
274 m->m_len = sizeof(*ah) + 2 * sizeof(struct in_addr) +
276 m->m_pkthdr.len = m->m_len;
277 M_ALIGN(m, m->m_len);
278 ah = mtod(m, struct arphdr *);
279 bzero((caddr_t)ah, m->m_len);
281 mac_netinet_arp_send(ifp, m);
283 ah->ar_pro = htons(ETHERTYPE_IP);
284 ah->ar_hln = ifp->if_addrlen; /* hardware address length */
285 ah->ar_pln = sizeof(struct in_addr); /* protocol address length */
286 ah->ar_op = htons(ARPOP_REQUEST);
287 bcopy(enaddr, ar_sha(ah), ah->ar_hln);
288 bcopy(sip, ar_spa(ah), ah->ar_pln);
289 bcopy(tip, ar_tpa(ah), ah->ar_pln);
290 sa.sa_family = AF_ARP;
292 m->m_flags |= M_BCAST;
293 m_clrprotoflags(m); /* Avoid confusing lower layers. */
294 (*ifp->if_output)(ifp, m, &sa, NULL);
295 ARPSTAT_INC(txrequests);
299 * Resolve an IP address into an ethernet address - heavy version.
300 * Used internally by arpresolve().
301 * We have already checked than we can't use existing lle without
302 * modification so we have to acquire LLE_EXCLUSIVE lle lock.
304 * On success, desten and flags are filled in and the function returns 0;
305 * If the packet must be held pending resolution, we return EWOULDBLOCK
306 * On other errors, we return the corresponding error code.
307 * Note that m_freem() handles NULL.
310 arpresolve_full(struct ifnet *ifp, int is_gw, int create, struct mbuf *m,
311 const struct sockaddr *dst, u_char *desten, uint32_t *pflags)
313 struct llentry *la = NULL, *la_tmp;
314 struct mbuf *curr = NULL;
315 struct mbuf *next = NULL;
322 IF_AFDATA_RLOCK(ifp);
323 la = lla_lookup(LLTABLE(ifp), LLE_EXCLUSIVE, dst);
324 IF_AFDATA_RUNLOCK(ifp);
326 if (la == NULL && (ifp->if_flags & (IFF_NOARP | IFF_STATICARP)) == 0) {
327 la = lltable_alloc_entry(LLTABLE(ifp), 0, dst);
330 "arpresolve: can't allocate llinfo for %s on %s\n",
331 inet_ntoa(SIN(dst)->sin_addr), if_name(ifp));
336 IF_AFDATA_WLOCK(ifp);
338 la_tmp = lla_lookup(LLTABLE(ifp), LLE_EXCLUSIVE, dst);
339 /* Prefer ANY existing lle over newly-created one */
341 lltable_link_entry(LLTABLE(ifp), la);
342 IF_AFDATA_WUNLOCK(ifp);
343 if (la_tmp != NULL) {
344 lltable_free_entry(LLTABLE(ifp), la);
353 if ((la->la_flags & LLE_VALID) &&
354 ((la->la_flags & LLE_STATIC) || la->la_expire > time_uptime)) {
355 bcopy(&la->ll_addr, desten, ifp->if_addrlen);
358 * If entry has an expiry time and it is approaching,
359 * see if we need to send an ARP request within this
360 * arpt_down interval.
362 if (!(la->la_flags & LLE_STATIC) &&
363 time_uptime + la->la_preempt > la->la_expire) {
369 *pflags = la->la_flags;
374 arprequest(ifp, NULL, &SIN(dst)->sin_addr, NULL);
379 renew = (la->la_asked == 0 || la->la_expire != time_uptime);
381 * There is an arptab entry, but no ethernet address
382 * response yet. Add the mbuf to the list, dropping
383 * the oldest packet if we have exceeded the system
387 if (la->la_numheld >= V_arp_maxhold) {
388 if (la->la_hold != NULL) {
389 next = la->la_hold->m_nextpkt;
390 m_freem(la->la_hold);
393 ARPSTAT_INC(dropped);
396 if (la->la_hold != NULL) {
398 while (curr->m_nextpkt != NULL)
399 curr = curr->m_nextpkt;
406 * Return EWOULDBLOCK if we have tried less than arp_maxtries. It
407 * will be masked by ether_output(). Return EHOSTDOWN/EHOSTUNREACH
408 * if we have already sent arp_maxtries ARP requests. Retransmit the
409 * ARP request, but not faster than one request per second.
411 if (la->la_asked < V_arp_maxtries)
412 error = EWOULDBLOCK; /* First request. */
414 error = is_gw != 0 ? EHOSTUNREACH : EHOSTDOWN;
420 la->la_expire = time_uptime;
421 canceled = callout_reset(&la->lle_timer, hz * V_arpt_down,
427 arprequest(ifp, NULL, &SIN(dst)->sin_addr, NULL);
436 * Resolve an IP address into an ethernet address.
438 * ifp is the interface we use
439 * is_gw != 0 if @dst represents gateway to some destination
440 * m is the mbuf. May be NULL if we don't have a packet.
441 * dst is the next hop,
442 * desten is the storage to put LL address.
443 * flags returns lle entry flags.
445 * On success, desten and flags are filled in and the function returns 0;
446 * If the packet must be held pending resolution, we return EWOULDBLOCK
447 * On other errors, we return the corresponding error code.
448 * Note that m_freem() handles NULL.
451 arpresolve(struct ifnet *ifp, int is_gw, struct mbuf *m,
452 const struct sockaddr *dst, u_char *desten, uint32_t *pflags)
454 struct llentry *la = 0;
461 if (m->m_flags & M_BCAST) {
464 ifp->if_broadcastaddr, ifp->if_addrlen);
467 if (m->m_flags & M_MCAST) {
469 ETHER_MAP_IP_MULTICAST(&SIN(dst)->sin_addr, desten);
474 IF_AFDATA_RLOCK(ifp);
475 la = lla_lookup(LLTABLE(ifp), 0, dst);
476 IF_AFDATA_RUNLOCK(ifp);
479 return (arpresolve_full(ifp, is_gw, 1, m, dst, desten, pflags));
481 if ((la->la_flags & LLE_VALID) &&
482 ((la->la_flags & LLE_STATIC) || la->la_expire > time_uptime)) {
483 bcopy(&la->ll_addr, desten, ifp->if_addrlen);
486 * If entry has an expiry time and it is approaching,
487 * see if we need to send an ARP request within this
488 * arpt_down interval.
490 if (!(la->la_flags & LLE_STATIC) &&
491 time_uptime + la->la_preempt > la->la_expire) {
497 *pflags = la->la_flags;
502 arprequest(ifp, NULL, &SIN(dst)->sin_addr, NULL);
508 return (arpresolve_full(ifp, is_gw, 0, m, dst, desten, pflags));
512 * Common length and type checks are done here,
513 * then the protocol-specific routine is called.
516 arpintr(struct mbuf *m)
523 ifp = m->m_pkthdr.rcvif;
525 if (m->m_len < sizeof(struct arphdr) &&
526 ((m = m_pullup(m, sizeof(struct arphdr))) == NULL)) {
527 ARP_LOG(LOG_NOTICE, "packet with short header received on %s\n",
531 ar = mtod(m, struct arphdr *);
533 /* Check if length is sufficient */
534 if ((m = m_pullup(m, arphdr_len(ar))) == NULL) {
535 ARP_LOG(LOG_NOTICE, "short packet received on %s\n",
539 ar = mtod(m, struct arphdr *);
543 switch (ntohs(ar->ar_hrd)) {
545 hlen = ETHER_ADDR_LEN; /* RFC 826 */
549 hlen = 6; /* RFC 1390, FDDI_ADDR_LEN */
553 hlen = 1; /* RFC 1201, ARC_ADDR_LEN */
556 case ARPHRD_INFINIBAND:
557 hlen = 20; /* RFC 4391, INFINIBAND_ALEN */
558 layer = "infiniband";
560 case ARPHRD_IEEE1394:
561 hlen = 0; /* SHALL be 16 */ /* RFC 2734 */
565 * Restrict too long harware addresses.
566 * Currently we are capable of handling 20-byte
567 * addresses ( sizeof(lle->ll_addr) )
569 if (ar->ar_hln >= 20)
574 "packet with unknown harware format 0x%02d received on %s\n",
575 ntohs(ar->ar_hrd), if_name(ifp));
580 if (hlen != 0 && hlen != ar->ar_hln) {
582 "packet with invalid %s address length %d received on %s\n",
583 layer, ar->ar_hln, if_name(ifp));
588 ARPSTAT_INC(received);
589 switch (ntohs(ar->ar_pro)) {
601 * ARP for Internet protocols on 10 Mb/s Ethernet.
602 * Algorithm is that given in RFC 826.
603 * In addition, a sanity check is performed on the sender
604 * protocol address, to catch impersonators.
605 * We no longer handle negotiations for use of trailer protocol:
606 * Formerly, ARP replied for protocol type ETHERTYPE_TRAIL sent
607 * along with IP replies if we wanted trailers sent to us,
608 * and also sent them in response to IP replies.
609 * This allowed either end to announce the desire to receive
611 * We no longer reply to requests for ETHERTYPE_TRAIL protocol either,
612 * but formerly didn't normally send requests.
614 static int log_arp_wrong_iface = 1;
615 static int log_arp_movements = 1;
616 static int log_arp_permanent_modify = 1;
617 static int allow_multicast = 0;
619 SYSCTL_INT(_net_link_ether_inet, OID_AUTO, log_arp_wrong_iface, CTLFLAG_RW,
620 &log_arp_wrong_iface, 0,
621 "log arp packets arriving on the wrong interface");
622 SYSCTL_INT(_net_link_ether_inet, OID_AUTO, log_arp_movements, CTLFLAG_RW,
623 &log_arp_movements, 0,
624 "log arp replies from MACs different than the one in the cache");
625 SYSCTL_INT(_net_link_ether_inet, OID_AUTO, log_arp_permanent_modify, CTLFLAG_RW,
626 &log_arp_permanent_modify, 0,
627 "log arp replies from MACs different than the one in the permanent arp entry");
628 SYSCTL_INT(_net_link_ether_inet, OID_AUTO, allow_multicast, CTLFLAG_RW,
629 &allow_multicast, 0, "accept multicast addresses");
632 in_arpinput(struct mbuf *m)
634 struct rm_priotracker in_ifa_tracker;
636 struct ifnet *ifp = m->m_pkthdr.rcvif;
637 struct llentry *la = NULL, *la_tmp;
640 struct in_ifaddr *ia;
642 struct in_addr isaddr, itaddr, myaddr;
643 u_int8_t *enaddr = NULL;
645 int bridged = 0, is_bridge = 0;
647 struct sockaddr_in sin;
648 struct sockaddr *dst;
649 sin.sin_len = sizeof(struct sockaddr_in);
650 sin.sin_family = AF_INET;
651 sin.sin_addr.s_addr = 0;
655 if (ifp->if_type == IFT_BRIDGE)
659 * We already have checked that mbuf contains enough contiguous data
660 * to hold entire arp message according to the arp header.
662 ah = mtod(m, struct arphdr *);
665 * ARP is only for IPv4 so we can reject packets with
666 * a protocol length not equal to an IPv4 address.
668 if (ah->ar_pln != sizeof(struct in_addr)) {
669 ARP_LOG(LOG_NOTICE, "requested protocol length != %zu\n",
670 sizeof(struct in_addr));
674 if (allow_multicast == 0 && ETHER_IS_MULTICAST(ar_sha(ah))) {
675 ARP_LOG(LOG_NOTICE, "%*D is multicast\n",
676 ifp->if_addrlen, (u_char *)ar_sha(ah), ":");
680 op = ntohs(ah->ar_op);
681 (void)memcpy(&isaddr, ar_spa(ah), sizeof (isaddr));
682 (void)memcpy(&itaddr, ar_tpa(ah), sizeof (itaddr));
684 if (op == ARPOP_REPLY)
685 ARPSTAT_INC(rxreplies);
688 * For a bridge, we want to check the address irrespective
689 * of the receive interface. (This will change slightly
690 * when we have clusters of interfaces).
692 IN_IFADDR_RLOCK(&in_ifa_tracker);
693 LIST_FOREACH(ia, INADDR_HASH(itaddr.s_addr), ia_hash) {
694 if (((bridged && ia->ia_ifp->if_bridge == ifp->if_bridge) ||
695 ia->ia_ifp == ifp) &&
696 itaddr.s_addr == ia->ia_addr.sin_addr.s_addr &&
697 (ia->ia_ifa.ifa_carp == NULL ||
698 (*carp_iamatch_p)(&ia->ia_ifa, &enaddr))) {
699 ifa_ref(&ia->ia_ifa);
700 IN_IFADDR_RUNLOCK(&in_ifa_tracker);
704 LIST_FOREACH(ia, INADDR_HASH(isaddr.s_addr), ia_hash)
705 if (((bridged && ia->ia_ifp->if_bridge == ifp->if_bridge) ||
706 ia->ia_ifp == ifp) &&
707 isaddr.s_addr == ia->ia_addr.sin_addr.s_addr) {
708 ifa_ref(&ia->ia_ifa);
709 IN_IFADDR_RUNLOCK(&in_ifa_tracker);
713 #define BDG_MEMBER_MATCHES_ARP(addr, ifp, ia) \
714 (ia->ia_ifp->if_bridge == ifp->if_softc && \
715 !bcmp(IF_LLADDR(ia->ia_ifp), IF_LLADDR(ifp), ifp->if_addrlen) && \
716 addr == ia->ia_addr.sin_addr.s_addr)
718 * Check the case when bridge shares its MAC address with
719 * some of its children, so packets are claimed by bridge
720 * itself (bridge_input() does it first), but they are really
721 * meant to be destined to the bridge member.
724 LIST_FOREACH(ia, INADDR_HASH(itaddr.s_addr), ia_hash) {
725 if (BDG_MEMBER_MATCHES_ARP(itaddr.s_addr, ifp, ia)) {
726 ifa_ref(&ia->ia_ifa);
728 IN_IFADDR_RUNLOCK(&in_ifa_tracker);
733 #undef BDG_MEMBER_MATCHES_ARP
734 IN_IFADDR_RUNLOCK(&in_ifa_tracker);
737 * No match, use the first inet address on the receive interface
738 * as a dummy address for the rest of the function.
741 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link)
742 if (ifa->ifa_addr->sa_family == AF_INET &&
743 (ifa->ifa_carp == NULL ||
744 (*carp_iamatch_p)(ifa, &enaddr))) {
747 IF_ADDR_RUNLOCK(ifp);
750 IF_ADDR_RUNLOCK(ifp);
753 * If bridging, fall back to using any inet address.
755 IN_IFADDR_RLOCK(&in_ifa_tracker);
756 if (!bridged || (ia = TAILQ_FIRST(&V_in_ifaddrhead)) == NULL) {
757 IN_IFADDR_RUNLOCK(&in_ifa_tracker);
760 ifa_ref(&ia->ia_ifa);
761 IN_IFADDR_RUNLOCK(&in_ifa_tracker);
764 enaddr = (u_int8_t *)IF_LLADDR(ifp);
765 carped = (ia->ia_ifa.ifa_carp != NULL);
766 myaddr = ia->ia_addr.sin_addr;
767 ifa_free(&ia->ia_ifa);
768 if (!bcmp(ar_sha(ah), enaddr, ifp->if_addrlen))
769 goto drop; /* it's from me, ignore it. */
770 if (!bcmp(ar_sha(ah), ifp->if_broadcastaddr, ifp->if_addrlen)) {
771 ARP_LOG(LOG_NOTICE, "link address is broadcast for IP address "
772 "%s!\n", inet_ntoa(isaddr));
776 if (ifp->if_addrlen != ah->ar_hln) {
777 ARP_LOG(LOG_WARNING, "from %*D: addr len: new %d, "
778 "i/f %d (ignored)\n", ifp->if_addrlen,
779 (u_char *) ar_sha(ah), ":", ah->ar_hln,
785 * Warn if another host is using the same IP address, but only if the
786 * IP address isn't 0.0.0.0, which is used for DHCP only, in which
787 * case we suppress the warning to avoid false positive complaints of
788 * potential misconfiguration.
790 if (!bridged && !carped && isaddr.s_addr == myaddr.s_addr &&
791 myaddr.s_addr != 0) {
792 ARP_LOG(LOG_ERR, "%*D is using my IP address %s on %s!\n",
793 ifp->if_addrlen, (u_char *)ar_sha(ah), ":",
794 inet_ntoa(isaddr), ifp->if_xname);
799 if (ifp->if_flags & IFF_STATICARP)
802 bzero(&sin, sizeof(sin));
803 sin.sin_len = sizeof(struct sockaddr_in);
804 sin.sin_family = AF_INET;
805 sin.sin_addr = isaddr;
806 dst = (struct sockaddr *)&sin;
807 IF_AFDATA_RLOCK(ifp);
808 la = lla_lookup(LLTABLE(ifp), LLE_EXCLUSIVE, dst);
809 IF_AFDATA_RUNLOCK(ifp);
811 arp_check_update_lle(ah, isaddr, ifp, bridged, la);
812 else if (itaddr.s_addr == myaddr.s_addr) {
814 * Reply to our address, but no lle exists yet.
815 * do we really have to create an entry?
817 la = lltable_alloc_entry(LLTABLE(ifp), 0, dst);
820 arp_update_lle(ah, ifp, la);
822 IF_AFDATA_WLOCK(ifp);
824 la_tmp = lla_lookup(LLTABLE(ifp), LLE_EXCLUSIVE, dst);
827 * Check if lle still does not exists.
828 * If it does, that means that we either
829 * 1) have configured it explicitly, via
830 * 1a) 'arp -s' static entry or
831 * 1b) interface address static record
833 * 2) it was the result of sending first packet to-host
835 * 3) it was another arp reply packet we handled in
838 * In all cases except 3) we definitely need to prefer
839 * existing lle. For the sake of simplicity, prefer any
840 * existing lle over newly-create one.
843 lltable_link_entry(LLTABLE(ifp), la);
844 IF_AFDATA_WUNLOCK(ifp);
846 if (la_tmp == NULL) {
847 arp_mark_lle_reachable(la);
850 /* Free newly-create entry and handle packet */
851 lltable_free_entry(LLTABLE(ifp), la);
854 arp_check_update_lle(ah, isaddr, ifp, bridged, la);
855 /* arp_check_update_lle() returns @la unlocked */
860 if (op != ARPOP_REQUEST)
862 ARPSTAT_INC(rxrequests);
864 if (itaddr.s_addr == myaddr.s_addr) {
865 /* Shortcut.. the receiving interface is the target. */
866 (void)memcpy(ar_tha(ah), ar_sha(ah), ah->ar_hln);
867 (void)memcpy(ar_sha(ah), enaddr, ah->ar_hln);
869 struct llentry *lle = NULL;
871 sin.sin_addr = itaddr;
872 IF_AFDATA_RLOCK(ifp);
873 lle = lla_lookup(LLTABLE(ifp), 0, (struct sockaddr *)&sin);
874 IF_AFDATA_RUNLOCK(ifp);
876 if ((lle != NULL) && (lle->la_flags & LLE_PUB)) {
877 (void)memcpy(ar_tha(ah), ar_sha(ah), ah->ar_hln);
878 (void)memcpy(ar_sha(ah), &lle->ll_addr, ah->ar_hln);
888 sin.sin_addr = itaddr;
889 /* XXX MRT use table 0 for arp reply */
890 rt = in_rtalloc1((struct sockaddr *)&sin, 0, 0UL, 0);
895 * Don't send proxies for nodes on the same interface
896 * as this one came out of, or we'll get into a fight
897 * over who claims what Ether address.
899 if (!rt->rt_ifp || rt->rt_ifp == ifp) {
905 (void)memcpy(ar_tha(ah), ar_sha(ah), ah->ar_hln);
906 (void)memcpy(ar_sha(ah), enaddr, ah->ar_hln);
909 * Also check that the node which sent the ARP packet
910 * is on the interface we expect it to be on. This
911 * avoids ARP chaos if an interface is connected to the
914 sin.sin_addr = isaddr;
916 /* XXX MRT use table 0 for arp checks */
917 rt = in_rtalloc1((struct sockaddr *)&sin, 0, 0UL, 0);
920 if (rt->rt_ifp != ifp) {
921 ARP_LOG(LOG_INFO, "proxy: ignoring request"
922 " from %s via %s, expecting %s\n",
923 inet_ntoa(isaddr), ifp->if_xname,
924 rt->rt_ifp->if_xname);
931 printf("arp: proxying for %s\n", inet_ntoa(itaddr));
936 if (itaddr.s_addr == myaddr.s_addr &&
937 IN_LINKLOCAL(ntohl(itaddr.s_addr))) {
938 /* RFC 3927 link-local IPv4; always reply by broadcast. */
939 #ifdef DEBUG_LINKLOCAL
940 printf("arp: sending reply for link-local addr %s\n",
943 m->m_flags |= M_BCAST;
944 m->m_flags &= ~M_MCAST;
946 /* default behaviour; never reply by broadcast. */
947 m->m_flags &= ~(M_BCAST|M_MCAST);
949 (void)memcpy(ar_tpa(ah), ar_spa(ah), ah->ar_pln);
950 (void)memcpy(ar_spa(ah), &itaddr, ah->ar_pln);
951 ah->ar_op = htons(ARPOP_REPLY);
952 ah->ar_pro = htons(ETHERTYPE_IP); /* let's be sure! */
953 m->m_len = sizeof(*ah) + (2 * ah->ar_pln) + (2 * ah->ar_hln);
954 m->m_pkthdr.len = m->m_len;
955 m->m_pkthdr.rcvif = NULL;
956 sa.sa_family = AF_ARP;
958 m_clrprotoflags(m); /* Avoid confusing lower layers. */
959 (*ifp->if_output)(ifp, m, &sa, NULL);
960 ARPSTAT_INC(txreplies);
969 * Checks received arp data against existing @la.
970 * Updates lle state/performs notification if necessary.
973 arp_check_update_lle(struct arphdr *ah, struct in_addr isaddr, struct ifnet *ifp,
974 int bridged, struct llentry *la)
977 struct mbuf *m_hold, *m_hold_next;
979 LLE_WLOCK_ASSERT(la);
981 /* the following is not an error when doing bridging */
982 if (!bridged && la->lle_tbl->llt_ifp != ifp) {
983 if (log_arp_wrong_iface)
984 ARP_LOG(LOG_WARNING, "%s is on %s "
985 "but got reply from %*D on %s\n",
987 la->lle_tbl->llt_ifp->if_xname,
988 ifp->if_addrlen, (u_char *)ar_sha(ah), ":",
993 if ((la->la_flags & LLE_VALID) &&
994 bcmp(ar_sha(ah), &la->ll_addr, ifp->if_addrlen)) {
995 if (la->la_flags & LLE_STATIC) {
997 if (log_arp_permanent_modify)
999 "%*D attempts to modify "
1000 "permanent entry for %s on %s\n",
1002 (u_char *)ar_sha(ah), ":",
1003 inet_ntoa(isaddr), ifp->if_xname);
1006 if (log_arp_movements) {
1007 ARP_LOG(LOG_INFO, "%s moved from %*D "
1011 (u_char *)&la->ll_addr, ":",
1012 ifp->if_addrlen, (u_char *)ar_sha(ah), ":",
1017 /* Check if something has changed */
1018 if (memcmp(&la->ll_addr, ar_sha(ah), ifp->if_addrlen) != 0 ||
1019 (la->la_flags & LLE_VALID) == 0) {
1020 /* Perform real LLE update */
1021 /* use afdata WLOCK to update fields */
1024 IF_AFDATA_WLOCK(ifp);
1028 * Since we droppped LLE lock, other thread might have deleted
1029 * this lle. Check and return
1031 if ((la->la_flags & LLE_DELETED) != 0) {
1032 IF_AFDATA_WUNLOCK(ifp);
1033 LLE_FREE_LOCKED(la);
1038 arp_update_lle(ah, ifp, la);
1040 IF_AFDATA_WUNLOCK(ifp);
1044 arp_mark_lle_reachable(la);
1047 * The packets are all freed within the call to the output
1050 * NB: The lock MUST be released before the call to the
1053 if (la->la_hold != NULL) {
1054 m_hold = la->la_hold;
1057 lltable_fill_sa_entry(la, &sa);
1059 for (; m_hold != NULL; m_hold = m_hold_next) {
1060 m_hold_next = m_hold->m_nextpkt;
1061 m_hold->m_nextpkt = NULL;
1062 /* Avoid confusing lower layers. */
1063 m_clrprotoflags(m_hold);
1064 (*ifp->if_output)(ifp, m_hold, &sa, NULL);
1071 * Updates @la fields used by fast path code.
1074 arp_update_lle(struct arphdr *ah, struct ifnet *ifp, struct llentry *la)
1077 memcpy(&la->ll_addr, ar_sha(ah), ifp->if_addrlen);
1078 la->la_flags |= LLE_VALID;
1082 arp_mark_lle_reachable(struct llentry *la)
1086 LLE_WLOCK_ASSERT(la);
1088 EVENTHANDLER_INVOKE(lle_event, la, LLENTRY_RESOLVED);
1090 if (!(la->la_flags & LLE_STATIC)) {
1092 la->la_expire = time_uptime + V_arpt_keep;
1093 canceled = callout_reset(&la->lle_timer,
1094 hz * V_arpt_keep, arptimer, la);
1099 la->la_preempt = V_arp_maxtries;
1103 arp_ifinit(struct ifnet *ifp, struct ifaddr *ifa)
1105 struct llentry *lle, *lle_tmp;
1106 struct sockaddr_in *dst_in;
1107 struct sockaddr *dst;
1109 if (ifa->ifa_carp != NULL)
1112 ifa->ifa_rtrequest = NULL;
1114 dst_in = IA_SIN(ifa);
1115 dst = (struct sockaddr *)dst_in;
1117 if (ntohl(IA_SIN(ifa)->sin_addr.s_addr) == INADDR_ANY)
1120 arprequest(ifp, &IA_SIN(ifa)->sin_addr,
1121 &IA_SIN(ifa)->sin_addr, IF_LLADDR(ifp));
1124 * Interface address LLE record is considered static
1125 * because kernel code relies on LLE_STATIC flag to check
1126 * if these entries can be rewriten by arp updates.
1128 lle = lltable_alloc_entry(LLTABLE(ifp), LLE_IFADDR | LLE_STATIC, dst);
1130 log(LOG_INFO, "arp_ifinit: cannot create arp "
1131 "entry for interface address\n");
1135 IF_AFDATA_WLOCK(ifp);
1137 /* Unlink any entry if exists */
1138 lle_tmp = lla_lookup(LLTABLE(ifp), LLE_EXCLUSIVE, dst);
1139 if (lle_tmp != NULL)
1140 lltable_unlink_entry(LLTABLE(ifp), lle_tmp);
1142 lltable_link_entry(LLTABLE(ifp), lle);
1143 IF_AFDATA_WUNLOCK(ifp);
1145 if (lle_tmp != NULL)
1146 EVENTHANDLER_INVOKE(lle_event, lle_tmp, LLENTRY_EXPIRED);
1148 EVENTHANDLER_INVOKE(lle_event, lle, LLENTRY_RESOLVED);
1150 if (lle_tmp != NULL)
1151 lltable_free_entry(LLTABLE(ifp), lle_tmp);
1155 arp_ifinit2(struct ifnet *ifp, struct ifaddr *ifa, u_char *enaddr)
1157 if (ntohl(IA_SIN(ifa)->sin_addr.s_addr) != INADDR_ANY)
1158 arprequest(ifp, &IA_SIN(ifa)->sin_addr,
1159 &IA_SIN(ifa)->sin_addr, enaddr);
1160 ifa->ifa_rtrequest = NULL;
1167 netisr_register(&arp_nh);
1169 SYSINIT(arp, SI_SUB_PROTO_DOMAIN, SI_ORDER_ANY, arp_init, 0);