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/if_llc.h>
62 #include <net/ethernet.h>
63 #include <net/route.h>
66 #include <netinet/in.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 <net/if_arc.h>
75 #include <net/iso88025.h>
77 #include <security/mac/mac_framework.h>
79 #define SIN(s) ((const struct sockaddr_in *)(s))
80 #define SDL(s) ((struct sockaddr_dl *)s)
82 SYSCTL_DECL(_net_link_ether);
83 static SYSCTL_NODE(_net_link_ether, PF_INET, inet, CTLFLAG_RW, 0, "");
84 static SYSCTL_NODE(_net_link_ether, PF_ARP, arp, CTLFLAG_RW, 0, "");
87 static VNET_DEFINE(int, arpt_keep) = (20*60); /* once resolved, good for 20
89 static VNET_DEFINE(int, arp_maxtries) = 5;
90 static VNET_DEFINE(int, arp_proxyall) = 0;
91 static VNET_DEFINE(int, arpt_down) = 20; /* keep incomplete entries for
93 VNET_PCPUSTAT_DEFINE(struct arpstat, arpstat); /* ARP statistics, see if_arp.h */
94 VNET_PCPUSTAT_SYSINIT(arpstat);
97 VNET_PCPUSTAT_SYSUNINIT(arpstat);
100 static VNET_DEFINE(int, arp_maxhold) = 1;
102 #define V_arpt_keep VNET(arpt_keep)
103 #define V_arpt_down VNET(arpt_down)
104 #define V_arp_maxtries VNET(arp_maxtries)
105 #define V_arp_proxyall VNET(arp_proxyall)
106 #define V_arp_maxhold VNET(arp_maxhold)
108 SYSCTL_INT(_net_link_ether_inet, OID_AUTO, max_age, CTLFLAG_VNET | CTLFLAG_RW,
109 &VNET_NAME(arpt_keep), 0,
110 "ARP entry lifetime in seconds");
111 SYSCTL_INT(_net_link_ether_inet, OID_AUTO, maxtries, CTLFLAG_VNET | CTLFLAG_RW,
112 &VNET_NAME(arp_maxtries), 0,
113 "ARP resolution attempts before returning error");
114 SYSCTL_INT(_net_link_ether_inet, OID_AUTO, proxyall, CTLFLAG_VNET | CTLFLAG_RW,
115 &VNET_NAME(arp_proxyall), 0,
116 "Enable proxy ARP for all suitable requests");
117 SYSCTL_INT(_net_link_ether_inet, OID_AUTO, wait, CTLFLAG_VNET | CTLFLAG_RW,
118 &VNET_NAME(arpt_down), 0,
119 "Incomplete ARP entry lifetime in seconds");
120 SYSCTL_VNET_PCPUSTAT(_net_link_ether_arp, OID_AUTO, stats, struct arpstat,
121 arpstat, "ARP statistics (struct arpstat, net/if_arp.h)");
122 SYSCTL_INT(_net_link_ether_inet, OID_AUTO, maxhold, CTLFLAG_VNET | CTLFLAG_RW,
123 &VNET_NAME(arp_maxhold), 0,
124 "Number of packets to hold per ARP entry");
126 static void arp_init(void);
127 static void arpintr(struct mbuf *);
128 static void arptimer(void *);
130 static void in_arpinput(struct mbuf *);
133 static void arp_check_update_lle(struct arphdr *ah, struct in_addr isaddr,
134 struct ifnet *ifp, int bridged, struct llentry *la);
135 static void arp_update_lle(struct arphdr *ah, struct ifnet *ifp,
137 static void arp_mark_lle_reachable(struct llentry *la);
140 static const struct netisr_handler arp_nh = {
142 .nh_handler = arpintr,
143 .nh_proto = NETISR_ARP,
144 .nh_policy = NETISR_POLICY_SOURCE,
149 * called by in_scrubprefix() to remove entry from the table when
150 * the interface goes away
153 arp_ifscrub(struct ifnet *ifp, uint32_t addr)
155 struct sockaddr_in addr4;
157 bzero((void *)&addr4, sizeof(addr4));
158 addr4.sin_len = sizeof(addr4);
159 addr4.sin_family = AF_INET;
160 addr4.sin_addr.s_addr = addr;
161 IF_AFDATA_WLOCK(ifp);
162 lla_delete(LLTABLE(ifp), LLE_IFADDR, (struct sockaddr *)&addr4);
163 IF_AFDATA_WUNLOCK(ifp);
168 * Timeout routine. Age arp_tab entries periodically.
173 struct llentry *lle = (struct llentry *)arg;
176 if (lle->la_flags & LLE_STATIC) {
180 if (callout_pending(&lle->lle_timer)) {
182 * Here we are a bit odd here in the treatment of
183 * active/pending. If the pending bit is set, it got
184 * rescheduled before I ran. The active
185 * bit we ignore, since if it was stopped
186 * in ll_tablefree() and was currently running
187 * it would have return 0 so the code would
188 * not have deleted it since the callout could
189 * not be stopped so we want to go through
190 * with the delete here now. If the callout
191 * was restarted, the pending bit will be back on and
192 * we just want to bail since the callout_reset would
193 * return 1 and our reference would have been removed
194 * by arpresolve() below.
199 ifp = lle->lle_tbl->llt_ifp;
200 CURVNET_SET(ifp->if_vnet);
202 if ((lle->la_flags & LLE_DELETED) == 0) {
205 if (lle->la_flags & LLE_VALID)
206 evt = LLENTRY_EXPIRED;
208 evt = LLENTRY_TIMEDOUT;
209 EVENTHANDLER_INVOKE(lle_event, lle, evt);
212 callout_stop(&lle->lle_timer);
214 /* XXX: LOR avoidance. We still have ref on lle. */
219 /* Guard against race with other llentry_free(). */
220 if (lle->la_flags & LLE_LINKED) {
224 pkts_dropped = llentry_free(lle);
225 ARPSTAT_ADD(dropped, pkts_dropped);
227 LLE_FREE_LOCKED(lle);
229 IF_AFDATA_UNLOCK(ifp);
231 ARPSTAT_INC(timeouts);
237 * Broadcast an ARP request. Caller specifies:
238 * - arp header source ip address
239 * - arp header target ip address
240 * - arp header source ethernet address
243 arprequest(struct ifnet *ifp, const struct in_addr *sip,
244 const struct in_addr *tip, u_char *enaddr)
249 u_char *carpaddr = NULL;
253 * The caller did not supply a source address, try to find
254 * a compatible one among those assigned to this interface.
259 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
260 if (ifa->ifa_addr->sa_family != AF_INET)
264 if ((*carp_iamatch_p)(ifa, &carpaddr) == 0)
266 sip = &IA_SIN(ifa)->sin_addr;
269 sip = &IA_SIN(ifa)->sin_addr;
272 if (0 == ((sip->s_addr ^ tip->s_addr) &
273 IA_MASKSIN(ifa)->sin_addr.s_addr))
274 break; /* found it. */
276 IF_ADDR_RUNLOCK(ifp);
278 printf("%s: cannot find matching address\n", __func__);
283 enaddr = carpaddr ? carpaddr : (u_char *)IF_LLADDR(ifp);
285 if ((m = m_gethdr(M_NOWAIT, MT_DATA)) == NULL)
287 m->m_len = sizeof(*ah) + 2 * sizeof(struct in_addr) +
289 m->m_pkthdr.len = m->m_len;
290 M_ALIGN(m, m->m_len);
291 ah = mtod(m, struct arphdr *);
292 bzero((caddr_t)ah, m->m_len);
294 mac_netinet_arp_send(ifp, m);
296 ah->ar_pro = htons(ETHERTYPE_IP);
297 ah->ar_hln = ifp->if_addrlen; /* hardware address length */
298 ah->ar_pln = sizeof(struct in_addr); /* protocol address length */
299 ah->ar_op = htons(ARPOP_REQUEST);
300 bcopy(enaddr, ar_sha(ah), ah->ar_hln);
301 bcopy(sip, ar_spa(ah), ah->ar_pln);
302 bcopy(tip, ar_tpa(ah), ah->ar_pln);
303 sa.sa_family = AF_ARP;
305 m->m_flags |= M_BCAST;
306 m_clrprotoflags(m); /* Avoid confusing lower layers. */
307 (*ifp->if_output)(ifp, m, &sa, NULL);
308 ARPSTAT_INC(txrequests);
312 * Resolve an IP address into an ethernet address - heavy version.
313 * Used internally by arpresolve().
314 * We have already checked than we can't use existing lle without
315 * modification so we have to acquire LLE_EXCLUSIVE lle lock.
317 * On success, desten and flags are filled in and the function returns 0;
318 * If the packet must be held pending resolution, we return EWOULDBLOCK
319 * On other errors, we return the corresponding error code.
320 * Note that m_freem() handles NULL.
323 arpresolve_full(struct ifnet *ifp, int is_gw, int create, struct mbuf *m,
324 const struct sockaddr *dst, u_char *desten, uint32_t *pflags)
326 struct llentry *la = NULL, *la_tmp;
327 struct mbuf *curr = NULL;
328 struct mbuf *next = NULL;
335 IF_AFDATA_RLOCK(ifp);
336 la = lla_lookup(LLTABLE(ifp), LLE_EXCLUSIVE, dst);
337 IF_AFDATA_RUNLOCK(ifp);
339 if (la == NULL && (ifp->if_flags & (IFF_NOARP | IFF_STATICARP)) == 0) {
340 la = lltable_alloc_entry(LLTABLE(ifp), 0, dst);
343 "arpresolve: can't allocate llinfo for %s on %s\n",
344 inet_ntoa(SIN(dst)->sin_addr), if_name(ifp));
349 IF_AFDATA_WLOCK(ifp);
351 la_tmp = lla_lookup(LLTABLE(ifp), LLE_EXCLUSIVE, dst);
352 /* Prefer ANY existing lle over newly-created one */
354 lltable_link_entry(LLTABLE(ifp), la);
355 IF_AFDATA_WUNLOCK(ifp);
356 if (la_tmp != NULL) {
357 lltable_free_entry(LLTABLE(ifp), la);
366 if ((la->la_flags & LLE_VALID) &&
367 ((la->la_flags & LLE_STATIC) || la->la_expire > time_uptime)) {
368 bcopy(&la->ll_addr, desten, ifp->if_addrlen);
371 * If entry has an expiry time and it is approaching,
372 * see if we need to send an ARP request within this
373 * arpt_down interval.
375 if (!(la->la_flags & LLE_STATIC) &&
376 time_uptime + la->la_preempt > la->la_expire) {
382 *pflags = la->la_flags;
387 arprequest(ifp, NULL, &SIN(dst)->sin_addr, NULL);
392 renew = (la->la_asked == 0 || la->la_expire != time_uptime);
394 * There is an arptab entry, but no ethernet address
395 * response yet. Add the mbuf to the list, dropping
396 * the oldest packet if we have exceeded the system
400 if (la->la_numheld >= V_arp_maxhold) {
401 if (la->la_hold != NULL) {
402 next = la->la_hold->m_nextpkt;
403 m_freem(la->la_hold);
406 ARPSTAT_INC(dropped);
409 if (la->la_hold != NULL) {
411 while (curr->m_nextpkt != NULL)
412 curr = curr->m_nextpkt;
419 * Return EWOULDBLOCK if we have tried less than arp_maxtries. It
420 * will be masked by ether_output(). Return EHOSTDOWN/EHOSTUNREACH
421 * if we have already sent arp_maxtries ARP requests. Retransmit the
422 * ARP request, but not faster than one request per second.
424 if (la->la_asked < V_arp_maxtries)
425 error = EWOULDBLOCK; /* First request. */
427 error = is_gw != 0 ? EHOSTUNREACH : EHOSTDOWN;
433 la->la_expire = time_uptime;
434 canceled = callout_reset(&la->lle_timer, hz * V_arpt_down,
440 arprequest(ifp, NULL, &SIN(dst)->sin_addr, NULL);
449 * Resolve an IP address into an ethernet address.
451 * ifp is the interface we use
452 * is_gw != 0 if @dst represents gateway to some destination
453 * m is the mbuf. May be NULL if we don't have a packet.
454 * dst is the next hop,
455 * desten is the storage to put LL address.
456 * flags returns lle entry flags.
458 * On success, desten and flags are filled in and the function returns 0;
459 * If the packet must be held pending resolution, we return EWOULDBLOCK
460 * On other errors, we return the corresponding error code.
461 * Note that m_freem() handles NULL.
464 arpresolve(struct ifnet *ifp, int is_gw, struct mbuf *m,
465 const struct sockaddr *dst, u_char *desten, uint32_t *pflags)
467 struct llentry *la = 0;
474 if (m->m_flags & M_BCAST) {
477 ifp->if_broadcastaddr, ifp->if_addrlen);
480 if (m->m_flags & M_MCAST) {
482 ETHER_MAP_IP_MULTICAST(&SIN(dst)->sin_addr, desten);
487 IF_AFDATA_RLOCK(ifp);
488 la = lla_lookup(LLTABLE(ifp), 0, dst);
489 IF_AFDATA_RUNLOCK(ifp);
492 return (arpresolve_full(ifp, is_gw, 1, m, dst, desten, pflags));
494 if ((la->la_flags & LLE_VALID) &&
495 ((la->la_flags & LLE_STATIC) || la->la_expire > time_uptime)) {
496 bcopy(&la->ll_addr, desten, ifp->if_addrlen);
499 * If entry has an expiry time and it is approaching,
500 * see if we need to send an ARP request within this
501 * arpt_down interval.
503 if (!(la->la_flags & LLE_STATIC) &&
504 time_uptime + la->la_preempt > la->la_expire) {
510 *pflags = la->la_flags;
515 arprequest(ifp, NULL, &SIN(dst)->sin_addr, NULL);
521 return (arpresolve_full(ifp, is_gw, 0, m, dst, desten, pflags));
525 * Common length and type checks are done here,
526 * then the protocol-specific routine is called.
529 arpintr(struct mbuf *m)
533 if (m->m_len < sizeof(struct arphdr) &&
534 ((m = m_pullup(m, sizeof(struct arphdr))) == NULL)) {
535 log(LOG_NOTICE, "arp: runt packet -- m_pullup failed\n");
538 ar = mtod(m, struct arphdr *);
540 if (ntohs(ar->ar_hrd) != ARPHRD_ETHER &&
541 ntohs(ar->ar_hrd) != ARPHRD_IEEE802 &&
542 ntohs(ar->ar_hrd) != ARPHRD_ARCNET &&
543 ntohs(ar->ar_hrd) != ARPHRD_IEEE1394 &&
544 ntohs(ar->ar_hrd) != ARPHRD_INFINIBAND) {
545 log(LOG_NOTICE, "arp: unknown hardware address format (0x%2D)"
546 " (from %*D to %*D)\n", (unsigned char *)&ar->ar_hrd, "",
547 ETHER_ADDR_LEN, (u_char *)ar_sha(ar), ":",
548 ETHER_ADDR_LEN, (u_char *)ar_tha(ar), ":");
553 if (m->m_len < arphdr_len(ar)) {
554 if ((m = m_pullup(m, arphdr_len(ar))) == NULL) {
555 log(LOG_NOTICE, "arp: runt packet\n");
559 ar = mtod(m, struct arphdr *);
562 ARPSTAT_INC(received);
563 switch (ntohs(ar->ar_pro)) {
575 * ARP for Internet protocols on 10 Mb/s Ethernet.
576 * Algorithm is that given in RFC 826.
577 * In addition, a sanity check is performed on the sender
578 * protocol address, to catch impersonators.
579 * We no longer handle negotiations for use of trailer protocol:
580 * Formerly, ARP replied for protocol type ETHERTYPE_TRAIL sent
581 * along with IP replies if we wanted trailers sent to us,
582 * and also sent them in response to IP replies.
583 * This allowed either end to announce the desire to receive
585 * We no longer reply to requests for ETHERTYPE_TRAIL protocol either,
586 * but formerly didn't normally send requests.
588 static int log_arp_wrong_iface = 1;
589 static int log_arp_movements = 1;
590 static int log_arp_permanent_modify = 1;
591 static int allow_multicast = 0;
592 static struct timeval arp_lastlog;
593 static int arp_curpps;
594 static int arp_maxpps = 1;
596 SYSCTL_INT(_net_link_ether_inet, OID_AUTO, log_arp_wrong_iface, CTLFLAG_RW,
597 &log_arp_wrong_iface, 0,
598 "log arp packets arriving on the wrong interface");
599 SYSCTL_INT(_net_link_ether_inet, OID_AUTO, log_arp_movements, CTLFLAG_RW,
600 &log_arp_movements, 0,
601 "log arp replies from MACs different than the one in the cache");
602 SYSCTL_INT(_net_link_ether_inet, OID_AUTO, log_arp_permanent_modify, CTLFLAG_RW,
603 &log_arp_permanent_modify, 0,
604 "log arp replies from MACs different than the one in the permanent arp entry");
605 SYSCTL_INT(_net_link_ether_inet, OID_AUTO, allow_multicast, CTLFLAG_RW,
606 &allow_multicast, 0, "accept multicast addresses");
607 SYSCTL_INT(_net_link_ether_inet, OID_AUTO, max_log_per_second,
608 CTLFLAG_RW, &arp_maxpps, 0,
609 "Maximum number of remotely triggered ARP messages that can be "
610 "logged per second");
612 #define ARP_LOG(pri, ...) do { \
613 if (ppsratecheck(&arp_lastlog, &arp_curpps, arp_maxpps)) \
614 log((pri), "arp: " __VA_ARGS__); \
618 in_arpinput(struct mbuf *m)
620 struct rm_priotracker in_ifa_tracker;
622 struct ifnet *ifp = m->m_pkthdr.rcvif;
623 struct llentry *la = NULL, *la_tmp;
626 struct in_ifaddr *ia;
628 struct in_addr isaddr, itaddr, myaddr;
629 u_int8_t *enaddr = NULL;
632 int bridged = 0, is_bridge = 0;
634 struct sockaddr_in sin;
635 struct sockaddr *dst;
636 sin.sin_len = sizeof(struct sockaddr_in);
637 sin.sin_family = AF_INET;
638 sin.sin_addr.s_addr = 0;
642 if (ifp->if_type == IFT_BRIDGE)
645 req_len = arphdr_len2(ifp->if_addrlen, sizeof(struct in_addr));
646 if (m->m_len < req_len && (m = m_pullup(m, req_len)) == NULL) {
647 ARP_LOG(LOG_NOTICE, "runt packet -- m_pullup failed\n");
651 ah = mtod(m, struct arphdr *);
653 * ARP is only for IPv4 so we can reject packets with
654 * a protocol length not equal to an IPv4 address.
656 if (ah->ar_pln != sizeof(struct in_addr)) {
657 ARP_LOG(LOG_NOTICE, "requested protocol length != %zu\n",
658 sizeof(struct in_addr));
662 if (allow_multicast == 0 && ETHER_IS_MULTICAST(ar_sha(ah))) {
663 ARP_LOG(LOG_NOTICE, "%*D is multicast\n",
664 ifp->if_addrlen, (u_char *)ar_sha(ah), ":");
668 op = ntohs(ah->ar_op);
669 (void)memcpy(&isaddr, ar_spa(ah), sizeof (isaddr));
670 (void)memcpy(&itaddr, ar_tpa(ah), sizeof (itaddr));
672 if (op == ARPOP_REPLY)
673 ARPSTAT_INC(rxreplies);
676 * For a bridge, we want to check the address irrespective
677 * of the receive interface. (This will change slightly
678 * when we have clusters of interfaces).
680 IN_IFADDR_RLOCK(&in_ifa_tracker);
681 LIST_FOREACH(ia, INADDR_HASH(itaddr.s_addr), ia_hash) {
682 if (((bridged && ia->ia_ifp->if_bridge == ifp->if_bridge) ||
683 ia->ia_ifp == ifp) &&
684 itaddr.s_addr == ia->ia_addr.sin_addr.s_addr &&
685 (ia->ia_ifa.ifa_carp == NULL ||
686 (*carp_iamatch_p)(&ia->ia_ifa, &enaddr))) {
687 ifa_ref(&ia->ia_ifa);
688 IN_IFADDR_RUNLOCK(&in_ifa_tracker);
692 LIST_FOREACH(ia, INADDR_HASH(isaddr.s_addr), ia_hash)
693 if (((bridged && ia->ia_ifp->if_bridge == ifp->if_bridge) ||
694 ia->ia_ifp == ifp) &&
695 isaddr.s_addr == ia->ia_addr.sin_addr.s_addr) {
696 ifa_ref(&ia->ia_ifa);
697 IN_IFADDR_RUNLOCK(&in_ifa_tracker);
701 #define BDG_MEMBER_MATCHES_ARP(addr, ifp, ia) \
702 (ia->ia_ifp->if_bridge == ifp->if_softc && \
703 !bcmp(IF_LLADDR(ia->ia_ifp), IF_LLADDR(ifp), ifp->if_addrlen) && \
704 addr == ia->ia_addr.sin_addr.s_addr)
706 * Check the case when bridge shares its MAC address with
707 * some of its children, so packets are claimed by bridge
708 * itself (bridge_input() does it first), but they are really
709 * meant to be destined to the bridge member.
712 LIST_FOREACH(ia, INADDR_HASH(itaddr.s_addr), ia_hash) {
713 if (BDG_MEMBER_MATCHES_ARP(itaddr.s_addr, ifp, ia)) {
714 ifa_ref(&ia->ia_ifa);
716 IN_IFADDR_RUNLOCK(&in_ifa_tracker);
721 #undef BDG_MEMBER_MATCHES_ARP
722 IN_IFADDR_RUNLOCK(&in_ifa_tracker);
725 * No match, use the first inet address on the receive interface
726 * as a dummy address for the rest of the function.
729 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link)
730 if (ifa->ifa_addr->sa_family == AF_INET &&
731 (ifa->ifa_carp == NULL ||
732 (*carp_iamatch_p)(ifa, &enaddr))) {
735 IF_ADDR_RUNLOCK(ifp);
738 IF_ADDR_RUNLOCK(ifp);
741 * If bridging, fall back to using any inet address.
743 IN_IFADDR_RLOCK(&in_ifa_tracker);
744 if (!bridged || (ia = TAILQ_FIRST(&V_in_ifaddrhead)) == NULL) {
745 IN_IFADDR_RUNLOCK(&in_ifa_tracker);
748 ifa_ref(&ia->ia_ifa);
749 IN_IFADDR_RUNLOCK(&in_ifa_tracker);
752 enaddr = (u_int8_t *)IF_LLADDR(ifp);
753 carped = (ia->ia_ifa.ifa_carp != NULL);
754 myaddr = ia->ia_addr.sin_addr;
755 ifa_free(&ia->ia_ifa);
756 if (!bcmp(ar_sha(ah), enaddr, ifp->if_addrlen))
757 goto drop; /* it's from me, ignore it. */
758 if (!bcmp(ar_sha(ah), ifp->if_broadcastaddr, ifp->if_addrlen)) {
759 ARP_LOG(LOG_NOTICE, "link address is broadcast for IP address "
760 "%s!\n", inet_ntoa(isaddr));
764 if (ifp->if_addrlen != ah->ar_hln) {
765 ARP_LOG(LOG_WARNING, "from %*D: addr len: new %d, "
766 "i/f %d (ignored)\n", ifp->if_addrlen,
767 (u_char *) ar_sha(ah), ":", ah->ar_hln,
773 * Warn if another host is using the same IP address, but only if the
774 * IP address isn't 0.0.0.0, which is used for DHCP only, in which
775 * case we suppress the warning to avoid false positive complaints of
776 * potential misconfiguration.
778 if (!bridged && !carped && isaddr.s_addr == myaddr.s_addr &&
779 myaddr.s_addr != 0) {
780 ARP_LOG(LOG_ERR, "%*D is using my IP address %s on %s!\n",
781 ifp->if_addrlen, (u_char *)ar_sha(ah), ":",
782 inet_ntoa(isaddr), ifp->if_xname);
787 if (ifp->if_flags & IFF_STATICARP)
790 bzero(&sin, sizeof(sin));
791 sin.sin_len = sizeof(struct sockaddr_in);
792 sin.sin_family = AF_INET;
793 sin.sin_addr = isaddr;
794 dst = (struct sockaddr *)&sin;
795 IF_AFDATA_RLOCK(ifp);
796 la = lla_lookup(LLTABLE(ifp), LLE_EXCLUSIVE, dst);
797 IF_AFDATA_RUNLOCK(ifp);
799 arp_check_update_lle(ah, isaddr, ifp, bridged, la);
800 else if (itaddr.s_addr == myaddr.s_addr) {
802 * Reply to our address, but no lle exists yet.
803 * do we really have to create an entry?
805 la = lltable_alloc_entry(LLTABLE(ifp), 0, dst);
808 arp_update_lle(ah, ifp, la);
810 IF_AFDATA_WLOCK(ifp);
812 la_tmp = lla_lookup(LLTABLE(ifp), LLE_EXCLUSIVE, dst);
815 * Check if lle still does not exists.
816 * If it does, that means that we either
817 * 1) have configured it explicitly, via
818 * 1a) 'arp -s' static entry or
819 * 1b) interface address static record
821 * 2) it was the result of sending first packet to-host
823 * 3) it was another arp reply packet we handled in
826 * In all cases except 3) we definitely need to prefer
827 * existing lle. For the sake of simplicity, prefer any
828 * existing lle over newly-create one.
831 lltable_link_entry(LLTABLE(ifp), la);
832 IF_AFDATA_WUNLOCK(ifp);
834 if (la_tmp == NULL) {
835 arp_mark_lle_reachable(la);
838 /* Free newly-create entry and handle packet */
839 lltable_free_entry(LLTABLE(ifp), la);
842 arp_check_update_lle(ah, isaddr, ifp, bridged, la);
843 /* arp_check_update_lle() returns @la unlocked */
848 if (op != ARPOP_REQUEST)
850 ARPSTAT_INC(rxrequests);
852 if (itaddr.s_addr == myaddr.s_addr) {
853 /* Shortcut.. the receiving interface is the target. */
854 (void)memcpy(ar_tha(ah), ar_sha(ah), ah->ar_hln);
855 (void)memcpy(ar_sha(ah), enaddr, ah->ar_hln);
857 struct llentry *lle = NULL;
859 sin.sin_addr = itaddr;
860 IF_AFDATA_RLOCK(ifp);
861 lle = lla_lookup(LLTABLE(ifp), 0, (struct sockaddr *)&sin);
862 IF_AFDATA_RUNLOCK(ifp);
864 if ((lle != NULL) && (lle->la_flags & LLE_PUB)) {
865 (void)memcpy(ar_tha(ah), ar_sha(ah), ah->ar_hln);
866 (void)memcpy(ar_sha(ah), &lle->ll_addr, ah->ar_hln);
876 sin.sin_addr = itaddr;
877 /* XXX MRT use table 0 for arp reply */
878 rt = in_rtalloc1((struct sockaddr *)&sin, 0, 0UL, 0);
883 * Don't send proxies for nodes on the same interface
884 * as this one came out of, or we'll get into a fight
885 * over who claims what Ether address.
887 if (!rt->rt_ifp || rt->rt_ifp == ifp) {
893 (void)memcpy(ar_tha(ah), ar_sha(ah), ah->ar_hln);
894 (void)memcpy(ar_sha(ah), enaddr, ah->ar_hln);
897 * Also check that the node which sent the ARP packet
898 * is on the interface we expect it to be on. This
899 * avoids ARP chaos if an interface is connected to the
902 sin.sin_addr = isaddr;
904 /* XXX MRT use table 0 for arp checks */
905 rt = in_rtalloc1((struct sockaddr *)&sin, 0, 0UL, 0);
908 if (rt->rt_ifp != ifp) {
909 ARP_LOG(LOG_INFO, "proxy: ignoring request"
910 " from %s via %s, expecting %s\n",
911 inet_ntoa(isaddr), ifp->if_xname,
912 rt->rt_ifp->if_xname);
919 printf("arp: proxying for %s\n", inet_ntoa(itaddr));
924 if (itaddr.s_addr == myaddr.s_addr &&
925 IN_LINKLOCAL(ntohl(itaddr.s_addr))) {
926 /* RFC 3927 link-local IPv4; always reply by broadcast. */
927 #ifdef DEBUG_LINKLOCAL
928 printf("arp: sending reply for link-local addr %s\n",
931 m->m_flags |= M_BCAST;
932 m->m_flags &= ~M_MCAST;
934 /* default behaviour; never reply by broadcast. */
935 m->m_flags &= ~(M_BCAST|M_MCAST);
937 (void)memcpy(ar_tpa(ah), ar_spa(ah), ah->ar_pln);
938 (void)memcpy(ar_spa(ah), &itaddr, ah->ar_pln);
939 ah->ar_op = htons(ARPOP_REPLY);
940 ah->ar_pro = htons(ETHERTYPE_IP); /* let's be sure! */
941 m->m_len = sizeof(*ah) + (2 * ah->ar_pln) + (2 * ah->ar_hln);
942 m->m_pkthdr.len = m->m_len;
943 m->m_pkthdr.rcvif = NULL;
944 sa.sa_family = AF_ARP;
946 m_clrprotoflags(m); /* Avoid confusing lower layers. */
947 (*ifp->if_output)(ifp, m, &sa, NULL);
948 ARPSTAT_INC(txreplies);
957 * Checks received arp data against existing @la.
958 * Updates lle state/performs notification if necessary.
961 arp_check_update_lle(struct arphdr *ah, struct in_addr isaddr, struct ifnet *ifp,
962 int bridged, struct llentry *la)
965 struct mbuf *m_hold, *m_hold_next;
967 LLE_WLOCK_ASSERT(la);
969 /* the following is not an error when doing bridging */
970 if (!bridged && la->lle_tbl->llt_ifp != ifp) {
971 if (log_arp_wrong_iface)
972 ARP_LOG(LOG_WARNING, "%s is on %s "
973 "but got reply from %*D on %s\n",
975 la->lle_tbl->llt_ifp->if_xname,
976 ifp->if_addrlen, (u_char *)ar_sha(ah), ":",
981 if ((la->la_flags & LLE_VALID) &&
982 bcmp(ar_sha(ah), &la->ll_addr, ifp->if_addrlen)) {
983 if (la->la_flags & LLE_STATIC) {
985 if (log_arp_permanent_modify)
987 "%*D attempts to modify "
988 "permanent entry for %s on %s\n",
990 (u_char *)ar_sha(ah), ":",
991 inet_ntoa(isaddr), ifp->if_xname);
994 if (log_arp_movements) {
995 ARP_LOG(LOG_INFO, "%s moved from %*D "
999 (u_char *)&la->ll_addr, ":",
1000 ifp->if_addrlen, (u_char *)ar_sha(ah), ":",
1005 /* Check if something has changed */
1006 if (memcmp(&la->ll_addr, ar_sha(ah), ifp->if_addrlen) != 0 ||
1007 (la->la_flags & LLE_VALID) == 0) {
1008 /* Perform real LLE update */
1009 /* use afdata WLOCK to update fields */
1012 IF_AFDATA_WLOCK(ifp);
1016 * Since we droppped LLE lock, other thread might have deleted
1017 * this lle. Check and return
1019 if ((la->la_flags & LLE_DELETED) != 0) {
1020 IF_AFDATA_WUNLOCK(ifp);
1021 LLE_FREE_LOCKED(la);
1026 arp_update_lle(ah, ifp, la);
1028 IF_AFDATA_WUNLOCK(ifp);
1032 arp_mark_lle_reachable(la);
1035 * The packets are all freed within the call to the output
1038 * NB: The lock MUST be released before the call to the
1041 if (la->la_hold != NULL) {
1042 m_hold = la->la_hold;
1045 lltable_fill_sa_entry(la, &sa);
1047 for (; m_hold != NULL; m_hold = m_hold_next) {
1048 m_hold_next = m_hold->m_nextpkt;
1049 m_hold->m_nextpkt = NULL;
1050 /* Avoid confusing lower layers. */
1051 m_clrprotoflags(m_hold);
1052 (*ifp->if_output)(ifp, m_hold, &sa, NULL);
1059 * Updates @la fields used by fast path code.
1062 arp_update_lle(struct arphdr *ah, struct ifnet *ifp, struct llentry *la)
1065 memcpy(&la->ll_addr, ar_sha(ah), ifp->if_addrlen);
1066 la->la_flags |= LLE_VALID;
1070 arp_mark_lle_reachable(struct llentry *la)
1074 LLE_WLOCK_ASSERT(la);
1076 EVENTHANDLER_INVOKE(lle_event, la, LLENTRY_RESOLVED);
1078 if (!(la->la_flags & LLE_STATIC)) {
1080 la->la_expire = time_uptime + V_arpt_keep;
1081 canceled = callout_reset(&la->lle_timer,
1082 hz * V_arpt_keep, arptimer, la);
1087 la->la_preempt = V_arp_maxtries;
1091 arp_ifinit(struct ifnet *ifp, struct ifaddr *ifa)
1093 struct llentry *lle, *lle_tmp;
1094 struct sockaddr_in *dst_in;
1095 struct sockaddr *dst;
1097 if (ifa->ifa_carp != NULL)
1100 ifa->ifa_rtrequest = NULL;
1102 dst_in = IA_SIN(ifa);
1103 dst = (struct sockaddr *)dst_in;
1105 if (ntohl(IA_SIN(ifa)->sin_addr.s_addr) == INADDR_ANY)
1108 arprequest(ifp, &IA_SIN(ifa)->sin_addr,
1109 &IA_SIN(ifa)->sin_addr, IF_LLADDR(ifp));
1112 * Interface address LLE record is considered static
1113 * because kernel code relies on LLE_STATIC flag to check
1114 * if these entries can be rewriten by arp updates.
1116 lle = lltable_alloc_entry(LLTABLE(ifp), LLE_IFADDR | LLE_STATIC, dst);
1118 log(LOG_INFO, "arp_ifinit: cannot create arp "
1119 "entry for interface address\n");
1123 IF_AFDATA_WLOCK(ifp);
1125 /* Unlink any entry if exists */
1126 lle_tmp = lla_lookup(LLTABLE(ifp), LLE_EXCLUSIVE, dst);
1127 if (lle_tmp != NULL)
1128 lltable_unlink_entry(LLTABLE(ifp), lle_tmp);
1130 lltable_link_entry(LLTABLE(ifp), lle);
1131 IF_AFDATA_WUNLOCK(ifp);
1133 if (lle_tmp != NULL)
1134 EVENTHANDLER_INVOKE(lle_event, lle_tmp, LLENTRY_EXPIRED);
1136 EVENTHANDLER_INVOKE(lle_event, lle, LLENTRY_RESOLVED);
1138 if (lle_tmp != NULL)
1139 lltable_free_entry(LLTABLE(ifp), lle_tmp);
1143 arp_ifinit2(struct ifnet *ifp, struct ifaddr *ifa, u_char *enaddr)
1145 if (ntohl(IA_SIN(ifa)->sin_addr.s_addr) != INADDR_ANY)
1146 arprequest(ifp, &IA_SIN(ifa)->sin_addr,
1147 &IA_SIN(ifa)->sin_addr, enaddr);
1148 ifa->ifa_rtrequest = NULL;
1155 netisr_register(&arp_nh);
1157 SYSINIT(arp, SI_SUB_PROTO_DOMAIN, SI_ORDER_ANY, arp_init, 0);