2 * Copyright (c) 2004, 2005 The DragonFly Project. All rights reserved.
4 * This code is derived from software contributed to The DragonFly Project
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. Neither the name of The DragonFly Project nor the names of its
16 * contributors may be used to endorse or promote products derived
17 * from this software without specific, prior written permission.
19 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
22 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
23 * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
24 * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
25 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
26 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
27 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
28 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
29 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 * Copyright (c) 1982, 1986, 1988, 1993
35 * The Regents of the University of California. All rights reserved.
37 * Redistribution and use in source and binary forms, with or without
38 * modification, are permitted provided that the following conditions
40 * 1. Redistributions of source code must retain the above copyright
41 * notice, this list of conditions and the following disclaimer.
42 * 2. Redistributions in binary form must reproduce the above copyright
43 * notice, this list of conditions and the following disclaimer in the
44 * documentation and/or other materials provided with the distribution.
45 * 3. Neither the name of the University nor the names of its contributors
46 * may be used to endorse or promote products derived from this software
47 * without specific prior written permission.
49 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
50 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
51 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
52 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
53 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
54 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
55 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
56 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
57 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
58 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
61 * @(#)if_ether.c 8.1 (Berkeley) 6/10/93
62 * $FreeBSD: src/sys/netinet/if_ether.c,v 1.64.2.23 2003/04/11 07:23:15 fjoe Exp $
66 * Ethernet address resolution protocol.
68 * add "inuse/lock" bit (or ref. count) along with valid bit
74 #include <sys/param.h>
75 #include <sys/kernel.h>
76 #include <sys/queue.h>
77 #include <sys/sysctl.h>
78 #include <sys/systm.h>
80 #include <sys/malloc.h>
81 #include <sys/socket.h>
82 #include <sys/syslog.h>
86 #include <net/if_dl.h>
87 #include <net/if_types.h>
88 #include <net/route.h>
89 #include <net/netisr.h>
90 #include <net/if_llc.h>
92 #include <netinet/in.h>
93 #include <netinet/in_var.h>
94 #include <netinet/if_ether.h>
96 #include <sys/thread2.h>
97 #include <sys/msgport2.h>
98 #include <net/netmsg2.h>
99 #include <net/netisr2.h>
100 #include <sys/mplock2.h>
103 #include <netinet/ip_carp.h>
106 #define SIN(s) ((struct sockaddr_in *)s)
107 #define SDL(s) ((struct sockaddr_dl *)s)
109 MALLOC_DEFINE(M_ARP, "arp", "ARP");
111 SYSCTL_DECL(_net_link_ether);
112 SYSCTL_NODE(_net_link_ether, PF_INET, inet, CTLFLAG_RW, 0, "");
115 static int arpt_prune = (5*60*1); /* walk list every 5 minutes */
116 static int arpt_keep = (20*60); /* once resolved, good for 20 more minutes */
117 static int arpt_down = 20; /* once declared down, don't send for 20 sec */
119 SYSCTL_INT(_net_link_ether_inet, OID_AUTO, prune_intvl, CTLFLAG_RW,
121 SYSCTL_INT(_net_link_ether_inet, OID_AUTO, max_age, CTLFLAG_RW,
123 SYSCTL_INT(_net_link_ether_inet, OID_AUTO, host_down_time, CTLFLAG_RW,
126 #define rt_expire rt_rmx.rmx_expire
129 LIST_ENTRY(llinfo_arp) la_le;
130 struct rtentry *la_rt;
131 struct mbuf *la_hold; /* last packet until resolved/timeout */
132 u_short la_preempt; /* countdown for pre-expiry arps */
133 u_short la_asked; /* #times we QUERIED following expiration */
136 static int arp_maxtries = 5;
137 static int useloopback = 1; /* use loopback interface for local traffic */
138 static int arp_proxyall = 0;
139 static int arp_refresh = 60; /* refresh arp cache ~60 (not impl yet) */
140 static int arp_restricted_match = 0;
141 static int arp_ignore_probes = 1;
143 SYSCTL_INT(_net_link_ether_inet, OID_AUTO, maxtries, CTLFLAG_RW,
144 &arp_maxtries, 0, "ARP resolution attempts before returning error");
145 SYSCTL_INT(_net_link_ether_inet, OID_AUTO, useloopback, CTLFLAG_RW,
146 &useloopback, 0, "Use the loopback interface for local traffic");
147 SYSCTL_INT(_net_link_ether_inet, OID_AUTO, proxyall, CTLFLAG_RW,
148 &arp_proxyall, 0, "Enable proxy ARP for all suitable requests");
149 SYSCTL_INT(_net_link_ether_inet, OID_AUTO, restricted_match, CTLFLAG_RW,
150 &arp_restricted_match, 0, "Only match against the sender");
151 SYSCTL_INT(_net_link_ether_inet, OID_AUTO, refresh, CTLFLAG_RW,
152 &arp_refresh, 0, "Preemptively refresh the ARP");
153 SYSCTL_INT(_net_link_ether_inet, OID_AUTO, ignore_probes, CTLFLAG_RW,
154 &arp_ignore_probes, 0, "Ignore ARP probes");
156 static void arp_rtrequest(int, struct rtentry *);
157 static void arprequest(struct ifnet *, const struct in_addr *,
158 const struct in_addr *, const u_char *);
159 static void arprequest_async(struct ifnet *, const struct in_addr *,
160 const struct in_addr *, const u_char *);
161 static void arpintr(netmsg_t msg);
162 static void arptfree(struct llinfo_arp *);
163 static void arptimer(void *);
164 static struct llinfo_arp *
165 arplookup(in_addr_t, boolean_t, boolean_t);
167 static void in_arpinput(struct mbuf *);
168 static void in_arpreply(struct mbuf *m, in_addr_t, in_addr_t);
169 static void arp_update_msghandler(netmsg_t);
170 static void arp_reply_msghandler(netmsg_t);
173 struct arp_pcpu_data {
174 LIST_HEAD(, llinfo_arp) llinfo_list;
175 struct callout timer_ch;
176 struct netmsg_base timer_nmsg;
179 static struct arp_pcpu_data *arp_data[MAXCPU];
182 * Timeout routine. Age arp_tab entries periodically.
185 arptimer_dispatch(netmsg_t nmsg)
187 struct arp_pcpu_data *ad = nmsg->lmsg.u.ms_resultp;
188 struct llinfo_arp *la, *nla;
190 ASSERT_NETISR_NCPUS(mycpuid);
194 netisr_replymsg(&nmsg->base, 0);
197 LIST_FOREACH_MUTABLE(la, &ad->llinfo_list, la_le, nla) {
198 if (la->la_rt->rt_expire && la->la_rt->rt_expire <= time_uptime)
201 callout_reset(&ad->timer_ch, arpt_prune * hz, arptimer, &ad->timer_nmsg);
207 struct netmsg_base *nm = xnm;
209 KKASSERT(mycpuid < netisr_ncpus);
212 if (nm->lmsg.ms_flags & MSGF_DONE)
213 netisr_sendmsg_oncpu(nm);
218 * Parallel to llc_rtrequest.
220 * Called after a route is successfully added to the tree to fix-up the
221 * route and initiate arp operations if required.
224 arp_rtrequest(int req, struct rtentry *rt)
226 struct sockaddr *gate = rt->rt_gateway;
227 struct llinfo_arp *la = rt->rt_llinfo;
229 struct sockaddr_dl null_sdl = { sizeof null_sdl, AF_LINK };
231 if (rt->rt_flags & RTF_GATEWAY)
237 * XXX: If this is a manually added route to interface
238 * such as older version of routed or gated might provide,
239 * restore cloning bit.
241 if (!(rt->rt_flags & RTF_HOST) &&
242 SIN(rt_mask(rt))->sin_addr.s_addr != 0xffffffff)
243 rt->rt_flags |= RTF_CLONING;
244 if (rt->rt_flags & RTF_CLONING) {
246 * Case 1: This route should come from a route to iface.
248 rt_setgate(rt, rt_key(rt),
249 (struct sockaddr *)&null_sdl);
250 gate = rt->rt_gateway;
251 SDL(gate)->sdl_type = rt->rt_ifp->if_type;
252 SDL(gate)->sdl_index = rt->rt_ifp->if_index;
253 rt->rt_expire = time_uptime;
257 * Announce a new entry if requested, and only announce it
260 if ((rt->rt_flags & RTF_ANNOUNCE) && mycpuid == 0) {
261 arprequest(rt->rt_ifp,
262 &SIN(rt_key(rt))->sin_addr,
263 &SIN(rt_key(rt))->sin_addr,
268 if (gate->sa_family != AF_LINK ||
269 gate->sa_len < sizeof(struct sockaddr_dl)) {
270 log(LOG_DEBUG, "arp_rtrequest: bad gateway value\n");
273 SDL(gate)->sdl_type = rt->rt_ifp->if_type;
274 SDL(gate)->sdl_index = rt->rt_ifp->if_index;
276 break; /* This happens on a route change */
278 * Case 2: This route may come from cloning, or a manual route
279 * add with a LL address.
281 R_Malloc(la, struct llinfo_arp *, sizeof *la);
284 log(LOG_DEBUG, "arp_rtrequest: malloc failed\n");
287 bzero(la, sizeof *la);
289 rt->rt_flags |= RTF_LLINFO;
290 LIST_INSERT_HEAD(&arp_data[mycpuid]->llinfo_list, la, la_le);
294 * This keeps the multicast addresses from showing up
295 * in `arp -a' listings as unresolved. It's not actually
296 * functional. Then the same for broadcast.
298 if (IN_MULTICAST(ntohl(SIN(rt_key(rt))->sin_addr.s_addr))) {
299 ETHER_MAP_IP_MULTICAST(&SIN(rt_key(rt))->sin_addr,
301 SDL(gate)->sdl_alen = 6;
304 if (in_broadcast(SIN(rt_key(rt))->sin_addr, rt->rt_ifp)) {
305 memcpy(LLADDR(SDL(gate)), rt->rt_ifp->if_broadcastaddr,
306 rt->rt_ifp->if_addrlen);
307 SDL(gate)->sdl_alen = rt->rt_ifp->if_addrlen;
313 * This fixes up the routing interface for local addresses.
314 * The route is adjusted to point at lo0 and the expiration
317 * NOTE: This prevents locally targetted traffic from going
318 * out the hardware interface, which is inefficient
319 * and might not work if the hardware cannot listen
320 * to its own transmitted packets. Setting
321 * net.link.ether.inet.useloopback to 0 will force
322 * packets for local addresses out the hardware (and
323 * it is expected to receive its own packet).
325 * XXX We should just be able to test RTF_LOCAL here instead
326 * of having to compare IPs.
328 if (SIN(rt_key(rt))->sin_addr.s_addr ==
329 (IA_SIN(rt->rt_ifa))->sin_addr.s_addr) {
331 bcopy(IF_LLADDR(rt->rt_ifp), LLADDR(SDL(gate)),
332 SDL(gate)->sdl_alen = rt->rt_ifp->if_addrlen);
341 LIST_REMOVE(la, la_le);
342 rt->rt_llinfo = NULL;
343 rt->rt_flags &= ~RTF_LLINFO;
344 if (la->la_hold != NULL)
345 m_freem(la->la_hold);
352 arpreq_alloc(struct ifnet *ifp, const struct in_addr *sip,
353 const struct in_addr *tip, const u_char *enaddr)
359 if ((m = m_gethdr(M_NOWAIT, MT_DATA)) == NULL)
361 m->m_pkthdr.rcvif = NULL;
363 switch (ifp->if_type) {
366 * This may not be correct for types not explicitly
367 * listed, but this is our best guess
370 ar_hrd = htons(ARPHRD_ETHER);
372 m->m_len = arphdr_len2(ifp->if_addrlen, sizeof(struct in_addr));
373 m->m_pkthdr.len = m->m_len;
374 MH_ALIGN(m, m->m_len);
376 ah = mtod(m, struct arphdr *);
381 ah->ar_pro = htons(ETHERTYPE_IP);
382 ah->ar_hln = ifp->if_addrlen; /* hardware address length */
383 ah->ar_pln = sizeof(struct in_addr); /* protocol address length */
384 ah->ar_op = htons(ARPOP_REQUEST);
385 memcpy(ar_sha(ah), enaddr, ah->ar_hln);
386 memset(ar_tha(ah), 0, ah->ar_hln);
387 memcpy(ar_spa(ah), sip, ah->ar_pln);
388 memcpy(ar_tpa(ah), tip, ah->ar_pln);
394 arpreq_send(struct ifnet *ifp, struct mbuf *m)
397 struct ether_header *eh;
399 ASSERT_NETISR_NCPUS(mycpuid);
401 switch (ifp->if_type) {
404 * This may not be correct for types not explicitly
405 * listed, but this is our best guess
408 eh = (struct ether_header *)sa.sa_data;
409 /* if_output() will not swap */
410 eh->ether_type = htons(ETHERTYPE_ARP);
411 memcpy(eh->ether_dhost, ifp->if_broadcastaddr, ifp->if_addrlen);
415 sa.sa_family = AF_UNSPEC;
416 sa.sa_len = sizeof(sa);
417 ifp->if_output(ifp, m, &sa, NULL);
421 arpreq_send_handler(netmsg_t msg)
423 struct mbuf *m = msg->packet.nm_packet;
424 struct ifnet *ifp = msg->lmsg.u.ms_resultp;
427 /* nmsg was embedded in the mbuf, do not reply! */
431 * Broadcast an ARP request. Caller specifies:
432 * - arp header source ip address
433 * - arp header target ip address
434 * - arp header source ethernet address
436 * NOTE: Caller MUST NOT hold ifp's serializer
439 arprequest(struct ifnet *ifp, const struct in_addr *sip,
440 const struct in_addr *tip, const u_char *enaddr)
444 ASSERT_NETISR_NCPUS(mycpuid);
446 if (enaddr == NULL) {
447 if (ifp->if_bridge) {
448 enaddr = IF_LLADDR(ether_bridge_interface(ifp));
450 enaddr = IF_LLADDR(ifp);
454 m = arpreq_alloc(ifp, sip, tip, enaddr);
461 * Same as arprequest(), except:
462 * - Caller is allowed to hold ifp's serializer
463 * - Network output is done in protocol thead
466 arprequest_async(struct ifnet *ifp, const struct in_addr *sip,
467 const struct in_addr *tip, const u_char *enaddr)
470 struct netmsg_packet *pmsg;
473 if (enaddr == NULL) {
474 if (ifp->if_bridge) {
475 enaddr = IF_LLADDR(ether_bridge_interface(ifp));
477 enaddr = IF_LLADDR(ifp);
480 m = arpreq_alloc(ifp, sip, tip, enaddr);
484 pmsg = &m->m_hdr.mh_netmsg;
485 netmsg_init(&pmsg->base, NULL, &netisr_apanic_rport,
486 0, arpreq_send_handler);
488 pmsg->base.lmsg.u.ms_resultp = ifp;
490 if (mycpuid < netisr_ncpus)
494 lwkt_sendmsg(netisr_cpuport(cpu), &pmsg->base.lmsg);
498 * Resolve an IP address into an ethernet address. If success,
499 * desten is filled in. If there is no entry in arptab,
500 * set one up and broadcast a request for the IP address.
501 * Hold onto this mbuf and resend it once the address
502 * is finally resolved. A return value of 1 indicates
503 * that desten has been filled in and the packet should be sent
504 * normally; a 0 return indicates that the packet has been
505 * taken over here, either now or for later transmission.
508 arpresolve(struct ifnet *ifp, struct rtentry *rt0, struct mbuf *m,
509 struct sockaddr *dst, u_char *desten)
511 struct rtentry *rt = NULL;
512 struct llinfo_arp *la = NULL;
513 struct sockaddr_dl *sdl;
516 if (m->m_flags & M_BCAST) { /* broadcast */
517 memcpy(desten, ifp->if_broadcastaddr, ifp->if_addrlen);
520 if (m->m_flags & M_MCAST) {/* multicast */
521 ETHER_MAP_IP_MULTICAST(&SIN(dst)->sin_addr, desten);
525 error = rt_llroute(dst, rt0, &rt);
533 la = arplookup(SIN(dst)->sin_addr.s_addr, TRUE, FALSE);
537 if (la == NULL || rt == NULL) {
538 char addr[INET_ADDRSTRLEN];
540 log(LOG_DEBUG, "arpresolve: can't allocate llinfo for %s%s%s\n",
541 kinet_ntoa(SIN(dst)->sin_addr, addr), la ? "la" : " ",
548 * Check the address family and length is valid, the address
549 * is resolved; otherwise, try to resolve.
551 sdl = SDL(rt->rt_gateway);
552 if ((rt->rt_expire == 0 || rt->rt_expire > time_uptime) &&
553 sdl->sdl_family == AF_LINK && sdl->sdl_alen != 0) {
555 * If entry has an expiry time and it is approaching,
556 * see if we need to send an ARP request within this
557 * arpt_down interval.
559 if ((rt->rt_expire != 0) &&
560 (time_uptime + la->la_preempt > rt->rt_expire)) {
562 &SIN(rt->rt_ifa->ifa_addr)->sin_addr,
568 bcopy(LLADDR(sdl), desten, sdl->sdl_alen);
573 * If ARP is disabled or static on this interface, stop.
575 * Probably should not allocate empty llinfo struct if we are
576 * not going to be sending out an arp request.
578 if (ifp->if_flags & (IFF_NOARP | IFF_STATICARP)) {
580 return ifp->if_flags & IFF_NOARP ? ENOTSUP : EINVAL;
584 * There is an arptab entry, but no ethernet address
585 * response yet. Replace the held mbuf with this
588 if (la->la_hold != NULL)
589 m_freem(la->la_hold);
593 * Return EWOULDBLOCK if we have tried less than arp_maxtries. It
594 * will be masked by ether_output(). Return EHOSTDOWN/EHOSTUNREACH
595 * if we have already sent arp_maxtries ARP requests. Retransmit the
596 * ARP request, but not faster than one request per second.
598 if (la->la_asked < arp_maxtries)
601 error = (rt != NULL && rt->rt_flags & RTF_GATEWAY) ?
602 EHOSTUNREACH : EHOSTDOWN;
604 if (rt->rt_expire || ((rt->rt_flags & RTF_STATIC) && !sdl->sdl_alen)) {
605 rt->rt_flags &= ~RTF_REJECT;
606 if (la->la_asked == 0 || rt->rt_expire != time_uptime) {
607 rt->rt_expire = time_uptime;
609 &SIN(rt->rt_ifa->ifa_addr)->sin_addr,
612 if (la->la_asked++ >= arp_maxtries) {
613 rt->rt_expire += arpt_down;
614 la->la_preempt = arp_maxtries;
615 rt_rtmsg(RTM_MISS, rt, rt->rt_ifp, 0);
623 * Common length and type checks are done here,
624 * then the protocol-specific routine is called.
627 arpintr(netmsg_t msg)
629 struct mbuf *m = msg->packet.nm_packet;
634 if (m->m_len < sizeof(struct arphdr) &&
635 (m = m_pullup(m, sizeof(struct arphdr))) == NULL) {
636 log(LOG_ERR, "arp: runt packet -- m_pullup failed\n");
639 ar = mtod(m, struct arphdr *);
641 ar_hrd = ntohs(ar->ar_hrd);
642 if (ar_hrd != ARPHRD_ETHER && ar_hrd != ARPHRD_IEEE802) {
643 hexncpy((unsigned char *)&ar->ar_hrd, 2, hexstr, 5, NULL);
644 log(LOG_ERR, "arp: unknown hardware address format (0x%s)\n",
650 if (m->m_pkthdr.len < arphdr_len(ar)) {
651 if ((m = m_pullup(m, arphdr_len(ar))) == NULL) {
652 log(LOG_ERR, "arp: runt packet\n");
655 ar = mtod(m, struct arphdr *);
658 switch (ntohs(ar->ar_pro)) {
666 /* msg was embedded in the mbuf, do not reply! */
671 * ARP for Internet protocols on 10 Mb/s Ethernet.
672 * Algorithm is that given in RFC 826.
673 * In addition, a sanity check is performed on the sender
674 * protocol address, to catch impersonators.
675 * We no longer handle negotiations for use of trailer protocol:
676 * Formerly, ARP replied for protocol type ETHERTYPE_TRAIL sent
677 * along with IP replies if we wanted trailers sent to us,
678 * and also sent them in response to IP replies.
679 * This allowed either end to announce the desire to receive
681 * We no longer reply to requests for ETHERTYPE_TRAIL protocol either,
682 * but formerly didn't normally send requests.
685 static int log_arp_wrong_iface = 1;
686 static int log_arp_movements = 1;
687 static int log_arp_permanent_modify = 1;
688 static int log_arp_creation_failure = 1;
690 SYSCTL_INT(_net_link_ether_inet, OID_AUTO, log_arp_wrong_iface, CTLFLAG_RW,
691 &log_arp_wrong_iface, 0,
692 "Log arp packets arriving on the wrong interface");
693 SYSCTL_INT(_net_link_ether_inet, OID_AUTO, log_arp_movements, CTLFLAG_RW,
694 &log_arp_movements, 0,
695 "Log arp replies from MACs different than the one in the cache");
696 SYSCTL_INT(_net_link_ether_inet, OID_AUTO, log_arp_permanent_modify, CTLFLAG_RW,
697 &log_arp_permanent_modify, 0,
698 "Log arp replies from MACs different than the one "
699 "in the permanent arp entry");
700 SYSCTL_INT(_net_link_ether_inet, OID_AUTO, log_arp_creation_failure, CTLFLAG_RW,
701 &log_arp_creation_failure, 0, "Log arp creation failure");
704 * Returns non-zero if the routine updated anything.
707 arp_update_oncpu(struct mbuf *m, in_addr_t saddr, boolean_t create,
710 struct arphdr *ah = mtod(m, struct arphdr *);
711 struct ifnet *ifp = m->m_pkthdr.rcvif;
712 struct llinfo_arp *la;
713 struct sockaddr_dl *sdl;
716 char sbuf[INET_ADDRSTRLEN];
717 int changed = create;
719 KASSERT(curthread->td_type == TD_TYPE_NETISR,
720 ("arp update not in netisr"));
722 la = arplookup(saddr, create, FALSE);
723 if (la && (rt = la->la_rt) && (sdl = SDL(rt->rt_gateway))) {
724 struct in_addr isaddr = { saddr };
725 int rt_cmd = sdl->sdl_alen == 0 ? RTM_ADD : RTM_CHANGE;
726 bool do_rtmsg = false;
729 * Normally arps coming in on the wrong interface are ignored,
730 * but if we are bridging and the two interfaces belong to
731 * the same bridge, or one is a member of the bridge which
732 * is the other, then it isn't an error.
734 if (rt->rt_ifp != ifp) {
736 * (1) ifp and rt_ifp both members of same bridge
737 * (2) rt_ifp member of bridge ifp
738 * (3) ifp member of bridge rt_ifp
740 * Always replace rt_ifp with the bridge ifc.
744 if (ifp->if_bridge &&
745 rt->rt_ifp->if_bridge == ifp->if_bridge) {
746 nifp = ether_bridge_interface(ifp);
747 } else if (rt->rt_ifp->if_bridge &&
748 ether_bridge_interface(rt->rt_ifp) == ifp) {
750 } else if (ifp->if_bridge &&
751 ether_bridge_interface(ifp) == rt->rt_ifp) {
757 if ((log_arp_wrong_iface == 1 && nifp == NULL) ||
758 log_arp_wrong_iface == 2) {
759 hexncpy((u_char *)ar_sha(ah), ifp->if_addrlen,
760 hexstr[0], HEX_NCPYLEN(ifp->if_addrlen), ":");
763 "but got reply from %s on %s\n",
764 kinet_ntoa(isaddr, sbuf),
765 rt->rt_ifp->if_xname, hexstr[0],
772 * nifp is our man! Replace rt_ifp and adjust
775 ifp = rt->rt_ifp = nifp;
776 if (sdl->sdl_type != ifp->if_type) {
777 sdl->sdl_type = ifp->if_type;
781 if (sdl->sdl_index != ifp->if_index) {
782 sdl->sdl_index = ifp->if_index;
788 bcmp(ar_sha(ah), LLADDR(sdl), sdl->sdl_alen)) {
790 if (rt->rt_expire != 0) {
791 if (dologging && log_arp_movements) {
792 hexncpy((u_char *)LLADDR(sdl), ifp->if_addrlen,
793 hexstr[0], HEX_NCPYLEN(ifp->if_addrlen), ":");
794 hexncpy((u_char *)ar_sha(ah), ifp->if_addrlen,
795 hexstr[1], HEX_NCPYLEN(ifp->if_addrlen), ":");
797 "arp: %s moved from %s to %s on %s\n",
798 kinet_ntoa(isaddr, sbuf), hexstr[0], hexstr[1],
802 if (dologging && log_arp_permanent_modify) {
803 hexncpy((u_char *)ar_sha(ah), ifp->if_addrlen,
804 hexstr[0], HEX_NCPYLEN(ifp->if_addrlen), ":");
806 "arp: %s attempts to modify "
807 "permanent entry for %s on %s\n",
808 hexstr[0], kinet_ntoa(isaddr, sbuf), ifp->if_xname);
815 * sanity check for the address length.
816 * XXX this does not work for protocols with variable address
819 if (dologging && sdl->sdl_alen && sdl->sdl_alen != ah->ar_hln) {
820 hexncpy((u_char *)ar_sha(ah), ifp->if_addrlen,
821 hexstr[0], HEX_NCPYLEN(ifp->if_addrlen), ":");
823 "arp from %s: new addr len %d, was %d",
824 hexstr[0], ah->ar_hln, sdl->sdl_alen);
826 if (ifp->if_addrlen != ah->ar_hln) {
828 hexncpy((u_char *)ar_sha(ah), ifp->if_addrlen,
829 hexstr[0], HEX_NCPYLEN(ifp->if_addrlen), ":");
831 "arp from %s: addr len: new %d, i/f %d "
832 "(ignored)", hexstr[0],
833 ah->ar_hln, ifp->if_addrlen);
837 if (sdl->sdl_alen == 0)
839 memcpy(LLADDR(sdl), ar_sha(ah), sdl->sdl_alen = ah->ar_hln);
840 if (rt->rt_expire != 0) {
841 if (rt->rt_expire != time_uptime + arpt_keep &&
842 rt->rt_expire != time_uptime + arpt_keep - 1) {
843 rt->rt_expire = time_uptime + arpt_keep;
847 if (rt->rt_flags & RTF_REJECT) {
848 rt->rt_flags &= ~RTF_REJECT;
851 if (la->la_asked != 0) {
855 if (la->la_preempt != arp_maxtries) {
856 la->la_preempt = arp_maxtries;
861 * This particular cpu might have been holding an mbuf
862 * pending ARP resolution. If so, transmit the mbuf now.
864 if (la->la_hold != NULL) {
865 struct mbuf *m = la->la_hold;
868 m_adj(m, sizeof(struct ether_header));
869 ifp->if_output(ifp, m, rt_key(rt), rt);
873 if (do_rtmsg && mycpuid == 0)
874 rt_rtmsg(rt_cmd, rt, rt->rt_ifp, 0);
880 * Called from arpintr() - this routine is run from a single cpu.
883 in_arpinput(struct mbuf *m)
886 struct ifnet *ifp = m->m_pkthdr.rcvif;
887 struct ifaddr_container *ifac;
888 struct in_ifaddr_container *iac;
889 struct in_ifaddr *ia = NULL;
890 struct in_addr isaddr, itaddr, myaddr;
891 uint8_t *enaddr = NULL;
894 char hexstr[64], sbuf[INET_ADDRSTRLEN];
896 req_len = arphdr_len2(ifp->if_addrlen, sizeof(struct in_addr));
897 if (m->m_len < req_len && (m = m_pullup(m, req_len)) == NULL) {
898 log(LOG_ERR, "in_arp: runt packet -- m_pullup failed\n");
902 ah = mtod(m, struct arphdr *);
903 memcpy(&isaddr, ar_spa(ah), sizeof isaddr);
904 memcpy(&itaddr, ar_tpa(ah), sizeof itaddr);
907 * Check both target and sender IP addresses:
909 * If we receive the packet on the interface owning the address,
910 * then accept the address.
912 * For a bridge, we accept the address if the receive interface and
913 * the interface owning the address are on the same bridge, and
914 * use the bridge MAC as the is-at response. The bridge will be
915 * responsible for handling the packet.
917 * (0) Check target IP against CARP IPs
920 LIST_FOREACH(iac, INADDR_HASH(itaddr.s_addr), ia_hash) {
921 int is_match = 0, is_parent = 0;
925 /* Skip all ia's which don't match */
926 if (itaddr.s_addr != ia->ia_addr.sin_addr.s_addr)
929 if (ia->ia_ifp->if_type != IFT_CARP)
932 if (carp_parent(ia->ia_ifp) == ifp)
934 if (is_parent || ia->ia_ifp == ifp)
935 is_match = carp_iamatch(ia);
940 * The parent interface will also receive
941 * the ethernet broadcast packets, e.g. ARP
942 * REQUEST, so if we could find a CARP
943 * interface of the parent that could match
944 * the target IP address, we then drop the
945 * packets, which is delieverd to us through
946 * the parent interface.
957 * (1) Check target IP against our local IPs
959 LIST_FOREACH(iac, INADDR_HASH(itaddr.s_addr), ia_hash) {
962 /* Skip all ia's which don't match */
963 if (itaddr.s_addr != ia->ia_addr.sin_addr.s_addr)
967 /* CARP interfaces are checked in (0) */
968 if (ia->ia_ifp->if_type == IFT_CARP)
972 if (ifp->if_bridge && ia->ia_ifp &&
973 ifp->if_bridge == ia->ia_ifp->if_bridge) {
974 ifp = ether_bridge_interface(ifp);
977 if (ia->ia_ifp && ia->ia_ifp->if_bridge &&
978 ether_bridge_interface(ia->ia_ifp) == ifp) {
981 if (ifp->if_bridge && ether_bridge_interface(ifp) ==
985 if (ia->ia_ifp == ifp) {
991 * (2) Check sender IP against our local IPs
993 LIST_FOREACH(iac, INADDR_HASH(isaddr.s_addr), ia_hash) {
996 /* Skip all ia's which don't match */
997 if (isaddr.s_addr != ia->ia_addr.sin_addr.s_addr)
1000 if (ifp->if_bridge && ia->ia_ifp &&
1001 ifp->if_bridge == ia->ia_ifp->if_bridge) {
1002 ifp = ether_bridge_interface(ifp);
1005 if (ia->ia_ifp && ia->ia_ifp->if_bridge &&
1006 ether_bridge_interface(ia->ia_ifp) == ifp) {
1009 if (ifp->if_bridge && ether_bridge_interface(ifp) ==
1014 if (ia->ia_ifp == ifp)
1019 * No match, use the first inet address on the receive interface
1020 * as a dummy address for the rest of the function.
1022 TAILQ_FOREACH(ifac, &ifp->if_addrheads[mycpuid], ifa_link) {
1023 struct ifaddr *ifa = ifac->ifa;
1025 if (ifa->ifa_addr && ifa->ifa_addr->sa_family == AF_INET) {
1032 * If we got here, we didn't find any suitable interface,
1033 * so drop the packet.
1040 enaddr = (uint8_t *)IF_LLADDR(ifp);
1041 myaddr = ia->ia_addr.sin_addr;
1042 if (!bcmp(ar_sha(ah), enaddr, ifp->if_addrlen)) {
1043 m_freem(m); /* it's from me, ignore it. */
1046 if (!bcmp(ar_sha(ah), ifp->if_broadcastaddr, ifp->if_addrlen)) {
1048 "arp: link address is broadcast for IP address %s!\n",
1049 kinet_ntoa(isaddr, sbuf));
1053 if (isaddr.s_addr == myaddr.s_addr && myaddr.s_addr != 0) {
1054 hexncpy((u_char *)ar_sha(ah), ifp->if_addrlen,
1055 hexstr, HEX_NCPYLEN(ifp->if_addrlen), ":");
1057 "arp: %s is using my IP address %s!\n",
1058 hexstr, kinet_ntoa(isaddr, sbuf));
1062 if (ifp->if_flags & IFF_STATICARP)
1066 * When arp_restricted_match is true and the ARP response is not
1067 * specifically targetted to me, ignore it. Otherwise the entry
1068 * timeout may be updated for an old MAC.
1070 if (arp_restricted_match && itaddr.s_addr != myaddr.s_addr) {
1076 * Update all CPU's routing tables with this ARP packet.
1078 * However, we only need to generate rtmsg on CPU0.
1081 changed = arp_update_oncpu(m, isaddr.s_addr,
1082 itaddr.s_addr == myaddr.s_addr,
1085 if (netisr_ncpus > 1 && changed) {
1086 struct netmsg_inarp *msg = &m->m_hdr.mh_arpmsg;
1088 netmsg_init(&msg->base, NULL, &netisr_apanic_rport,
1089 0, arp_update_msghandler);
1091 msg->saddr = isaddr.s_addr;
1092 msg->taddr = itaddr.s_addr;
1093 msg->myaddr = myaddr.s_addr;
1094 lwkt_sendmsg(netisr_cpuport(1), &msg->base.lmsg);
1100 * Just return here; after all CPUs's routing tables are
1101 * properly updated by this ARP packet, an ARP reply will
1102 * be generated if appropriate.
1106 in_arpreply(m, itaddr.s_addr, myaddr.s_addr);
1110 arp_reply_msghandler(netmsg_t msg)
1112 struct netmsg_inarp *rmsg = (struct netmsg_inarp *)msg;
1114 in_arpreply(rmsg->m, rmsg->taddr, rmsg->myaddr);
1115 /* Don't reply this netmsg; netmsg_inarp is embedded in mbuf */
1119 arp_update_msghandler(netmsg_t msg)
1121 struct netmsg_inarp *rmsg = (struct netmsg_inarp *)msg;
1124 ASSERT_NETISR_NCPUS(mycpuid);
1127 * This message handler will be called on all of the APs;
1128 * no need to generate rtmsg on them.
1130 KASSERT(mycpuid > 0, ("arp update msg on cpu%d", mycpuid));
1131 arp_update_oncpu(rmsg->m, rmsg->saddr,
1132 rmsg->taddr == rmsg->myaddr,
1135 nextcpu = mycpuid + 1;
1136 if (nextcpu < netisr_ncpus) {
1137 lwkt_forwardmsg(netisr_cpuport(nextcpu), &rmsg->base.lmsg);
1139 struct mbuf *m = rmsg->m;
1140 in_addr_t saddr = rmsg->saddr;
1141 in_addr_t taddr = rmsg->taddr;
1142 in_addr_t myaddr = rmsg->myaddr;
1145 * Dispatch this mbuf to netisr0 to perform ARP reply,
1147 * NOTE: netmsg_inarp is embedded in this mbuf.
1149 netmsg_init(&rmsg->base, NULL, &netisr_apanic_rport,
1150 0, arp_reply_msghandler);
1152 rmsg->saddr = saddr;
1153 rmsg->taddr = taddr;
1154 rmsg->myaddr = myaddr;
1155 lwkt_sendmsg(netisr_cpuport(0), &rmsg->base.lmsg);
1160 * Reply to an arp request
1163 in_arpreply(struct mbuf *m, in_addr_t taddr, in_addr_t myaddr)
1165 struct ifnet *ifp = m->m_pkthdr.rcvif;
1166 const uint8_t *enaddr;
1169 struct ether_header *eh;
1173 ah = mtod(m, struct arphdr *);
1174 if (ntohs(ah->ar_op) != ARPOP_REQUEST) {
1179 enaddr = (const uint8_t *)IF_LLADDR(ifp);
1180 if (taddr == myaddr) {
1181 /* I am the target */
1182 memcpy(ar_tha(ah), ar_sha(ah), ah->ar_hln);
1183 memcpy(ar_sha(ah), enaddr, ah->ar_hln);
1185 struct llinfo_arp *la;
1188 la = arplookup(taddr, FALSE, SIN_PROXY);
1190 struct sockaddr_in sin;
1192 char tbuf[INET_ADDRSTRLEN];
1195 if (!arp_proxyall) {
1200 bzero(&sin, sizeof sin);
1201 sin.sin_family = AF_INET;
1202 sin.sin_len = sizeof sin;
1203 sin.sin_addr.s_addr = taddr;
1205 rt = rtpurelookup((struct sockaddr *)&sin);
1213 * Don't send proxies for nodes on the same interface
1214 * as this one came out of, or we'll get into a fight
1215 * over who claims what Ether address.
1217 * If the rt entry is associated with a bridge, we
1218 * count it as the 'same' interface if ifp is
1219 * associated with the bridge.
1221 if (rt->rt_ifp == ifp || rt->rt_ifp == ifp->if_bridge) {
1225 memcpy(ar_tha(ah), ar_sha(ah), ah->ar_hln);
1226 memcpy(ar_sha(ah), enaddr, ah->ar_hln);
1228 kprintf("arp: proxying for %s\n",
1229 kinet_ntoa(itaddr, tbuf));
1232 struct sockaddr_dl *sdl;
1235 memcpy(ar_tha(ah), ar_sha(ah), ah->ar_hln);
1236 sdl = SDL(rt->rt_gateway);
1237 memcpy(ar_sha(ah), LLADDR(sdl), ah->ar_hln);
1241 memcpy(ar_tpa(ah), ar_spa(ah), ah->ar_pln);
1242 memcpy(ar_spa(ah), &taddr, ah->ar_pln);
1243 ah->ar_op = htons(ARPOP_REPLY);
1244 ah->ar_pro = htons(ETHERTYPE_IP); /* let's be sure! */
1245 switch (ifp->if_type) {
1248 * May not be correct for types not explictly
1249 * listed, but it is our best guess.
1252 eh = (struct ether_header *)sa.sa_data;
1253 memcpy(eh->ether_dhost, ar_tha(ah), sizeof eh->ether_dhost);
1254 eh->ether_type = htons(ETHERTYPE_ARP);
1257 sa.sa_family = AF_UNSPEC;
1258 sa.sa_len = sizeof sa;
1259 ifp->if_output(ifp, m, &sa, NULL);
1265 * Free an arp entry. If the arp entry is actively referenced or represents
1266 * a static entry we only clear it back to an unresolved state, otherwise
1267 * we destroy the entry entirely.
1269 * Note that static entries are created when route add ... -interface is used
1270 * to create an interface route to a (direct) destination.
1273 arptfree(struct llinfo_arp *la)
1275 struct rtentry *rt = la->la_rt, *nrt;
1276 struct sockaddr_dl *sdl;
1281 sdl = SDL(rt->rt_gateway);
1283 ((rt->rt_refcnt > 0 && sdl->sdl_family == AF_LINK) ||
1284 (rt->rt_flags & RTF_STATIC))) {
1286 la->la_preempt = la->la_asked = 0;
1287 rt->rt_flags &= ~RTF_REJECT;
1290 error = rtrequest(RTM_DELETE, rt_key(rt), NULL, rt_mask(rt), 0, &nrt);
1291 if (error == 0 && nrt != NULL) {
1292 rt_rtmsg(RTM_DELETE, nrt, nrt->rt_ifp, 0);
1298 * Lookup or enter a new address in arptab.
1300 static struct llinfo_arp *
1301 arplookup(in_addr_t addr, boolean_t create,
1305 struct sockaddr_inarp sin = { sizeof sin, AF_INET };
1306 const char *why = NULL;
1308 /* Check ARP probes, e.g. from Cisco switches. */
1309 if (addr == INADDR_ANY && arp_ignore_probes)
1312 sin.sin_addr.s_addr = addr;
1313 sin.sin_other = proxy ? SIN_PROXY : 0;
1315 rt = rtlookup((struct sockaddr *)&sin);
1317 rt = rtpurelookup((struct sockaddr *)&sin);
1323 if (rt->rt_flags & RTF_GATEWAY)
1324 why = "host is not on local network";
1325 else if (!(rt->rt_flags & RTF_LLINFO))
1326 why = "could not allocate llinfo";
1327 else if (rt->rt_gateway->sa_family != AF_LINK)
1328 why = "gateway route is not ours";
1331 if (create && log_arp_creation_failure) {
1332 char abuf[INET_ADDRSTRLEN];
1334 log(LOG_DEBUG, "arplookup %s failed: %s\n",
1335 kinet_ntoa(sin.sin_addr, abuf), why);
1337 if (rt->rt_refcnt <= 0 && (rt->rt_flags & RTF_WASCLONED)) {
1338 /* No references to this route. Purge it. */
1339 rtrequest(RTM_DELETE, rt_key(rt), rt->rt_gateway,
1340 rt_mask(rt), rt->rt_flags, NULL);
1344 return (rt->rt_llinfo);
1348 arp_ifinit(struct ifnet *ifp, struct ifaddr *ifa)
1350 ifa->ifa_rtrequest = arp_rtrequest;
1351 ifa->ifa_flags |= RTF_CLONING;
1355 arp_gratuitous(struct ifnet *ifp, struct ifaddr *ifa)
1357 if (IA_SIN(ifa)->sin_addr.s_addr != INADDR_ANY) {
1358 if (IN_NETISR_NCPUS(mycpuid)) {
1359 arprequest(ifp, &IA_SIN(ifa)->sin_addr,
1360 &IA_SIN(ifa)->sin_addr, NULL);
1362 arprequest_async(ifp, &IA_SIN(ifa)->sin_addr,
1363 &IA_SIN(ifa)->sin_addr, NULL);
1369 arp_ifaddr(void *arg __unused, struct ifnet *ifp,
1370 enum ifaddr_event event, struct ifaddr *ifa)
1372 if (ifa->ifa_rtrequest != arp_rtrequest) /* XXX need a generic way */
1374 if (ifa->ifa_addr->sa_family != AF_INET)
1376 if (event == IFADDR_EVENT_DELETE)
1380 * - CARP interfaces will take care of gratuitous ARP themselves.
1381 * - If we are the CARP interface's parent, don't send gratuitous
1382 * ARP to avoid unnecessary confusion.
1385 if (ifp->if_type != IFT_CARP && ifp->if_carp == NULL)
1388 arp_gratuitous(ifp, ifa);
1393 arp_init_dispatch(netmsg_t nm)
1395 struct arp_pcpu_data *ad;
1397 ASSERT_NETISR_NCPUS(mycpuid);
1399 ad = kmalloc(sizeof(*ad), M_ARP, M_WAITOK | M_ZERO);
1401 LIST_INIT(&ad->llinfo_list);
1402 callout_init_mp(&ad->timer_ch);
1403 netmsg_init(&ad->timer_nmsg, NULL, &netisr_adone_rport,
1404 MSGF_PRIORITY, arptimer_dispatch);
1405 ad->timer_nmsg.lmsg.u.ms_resultp = ad;
1407 arp_data[mycpuid] = ad;
1409 callout_reset(&ad->timer_ch, hz, arptimer, &ad->timer_nmsg);
1411 netisr_forwardmsg(&nm->base, mycpuid + 1);
1417 struct netmsg_base nm;
1419 netmsg_init(&nm, NULL, &curthread->td_msgport, 0, arp_init_dispatch);
1420 netisr_domsg_global(&nm);
1422 netisr_register(NETISR_ARP, arpintr, NULL);
1424 EVENTHANDLER_REGISTER(ifaddr_event, arp_ifaddr, NULL,
1425 EVENTHANDLER_PRI_LAST);
1427 SYSINIT(arp, SI_SUB_PROTO_DOMAIN, SI_ORDER_ANY, arp_init, 0);