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. All advertising materials mentioning features or use of this software
46 * must display the following acknowledgement:
47 * This product includes software developed by the University of
48 * California, Berkeley and its contributors.
49 * 4. Neither the name of the University nor the names of its contributors
50 * may be used to endorse or promote products derived from this software
51 * without specific prior written permission.
53 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
54 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
55 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
56 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
57 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
58 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
59 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
60 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
61 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
62 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
65 * @(#)if_ether.c 8.1 (Berkeley) 6/10/93
66 * $FreeBSD: src/sys/netinet/if_ether.c,v 1.64.2.23 2003/04/11 07:23:15 fjoe Exp $
70 * Ethernet address resolution protocol.
72 * add "inuse/lock" bit (or ref. count) along with valid bit
78 #include <sys/param.h>
79 #include <sys/kernel.h>
80 #include <sys/queue.h>
81 #include <sys/sysctl.h>
82 #include <sys/systm.h>
84 #include <sys/malloc.h>
85 #include <sys/socket.h>
86 #include <sys/syslog.h>
90 #include <net/if_dl.h>
91 #include <net/if_types.h>
92 #include <net/route.h>
93 #include <net/netisr.h>
94 #include <net/if_llc.h>
96 #include <netinet/in.h>
97 #include <netinet/in_var.h>
98 #include <netinet/if_ether.h>
100 #include <sys/thread2.h>
101 #include <sys/msgport2.h>
102 #include <net/netmsg2.h>
103 #include <sys/mplock2.h>
106 #include <netinet/ip_carp.h>
109 #define SIN(s) ((struct sockaddr_in *)s)
110 #define SDL(s) ((struct sockaddr_dl *)s)
112 SYSCTL_DECL(_net_link_ether);
113 SYSCTL_NODE(_net_link_ether, PF_INET, inet, CTLFLAG_RW, 0, "");
116 static int arpt_prune = (5*60*1); /* walk list every 5 minutes */
117 static int arpt_keep = (20*60); /* once resolved, good for 20 more minutes */
118 static int arpt_down = 20; /* once declared down, don't send for 20 sec */
120 SYSCTL_INT(_net_link_ether_inet, OID_AUTO, prune_intvl, CTLFLAG_RW,
122 SYSCTL_INT(_net_link_ether_inet, OID_AUTO, max_age, CTLFLAG_RW,
124 SYSCTL_INT(_net_link_ether_inet, OID_AUTO, host_down_time, CTLFLAG_RW,
127 #define rt_expire rt_rmx.rmx_expire
130 LIST_ENTRY(llinfo_arp) la_le;
131 struct rtentry *la_rt;
132 struct mbuf *la_hold; /* last packet until resolved/timeout */
133 struct lwkt_port *la_msgport; /* last packet's msgport */
134 u_short la_preempt; /* countdown for pre-expiry arps */
135 u_short la_asked; /* #times we QUERIED following expiration */
138 static LIST_HEAD(, llinfo_arp) llinfo_arp_list[MAXCPU];
140 static int arp_maxtries = 5;
141 static int useloopback = 1; /* use loopback interface for local traffic */
142 static int arp_proxyall = 0;
143 static int arp_refresh = 60; /* refresh arp cache ~60 (not impl yet) */
144 static int arp_restricted_match = 0;
146 SYSCTL_INT(_net_link_ether_inet, OID_AUTO, maxtries, CTLFLAG_RW,
147 &arp_maxtries, 0, "ARP resolution attempts before returning error");
148 SYSCTL_INT(_net_link_ether_inet, OID_AUTO, useloopback, CTLFLAG_RW,
149 &useloopback, 0, "Use the loopback interface for local traffic");
150 SYSCTL_INT(_net_link_ether_inet, OID_AUTO, proxyall, CTLFLAG_RW,
151 &arp_proxyall, 0, "Enable proxy ARP for all suitable requests");
152 SYSCTL_INT(_net_link_ether_inet, OID_AUTO, restricted_match, CTLFLAG_RW,
153 &arp_restricted_match, 0, "Only match against the sender");
154 SYSCTL_INT(_net_link_ether_inet, OID_AUTO, refresh, CTLFLAG_RW,
155 &arp_refresh, 0, "Preemptively refresh the ARP");
157 static void arp_rtrequest(int, struct rtentry *, struct rt_addrinfo *);
158 static void arprequest(struct ifnet *, const struct in_addr *,
159 const struct in_addr *, const u_char *);
160 static void arprequest_async(struct ifnet *, const struct in_addr *,
161 const struct in_addr *, const u_char *);
162 static void arpintr(netmsg_t msg);
163 static void arptfree(struct llinfo_arp *);
164 static void arptimer(void *);
165 static struct llinfo_arp *
166 arplookup(in_addr_t, boolean_t, boolean_t, boolean_t);
168 static void in_arpinput(struct mbuf *);
169 static void in_arpreply(struct mbuf *m, in_addr_t, in_addr_t);
170 static void arp_update_msghandler(netmsg_t);
171 static void arp_reply_msghandler(netmsg_t);
174 static struct callout arptimer_ch[MAXCPU];
177 * Timeout routine. Age arp_tab entries periodically.
181 arptimer(void *ignored_arg)
183 struct llinfo_arp *la, *nla;
186 LIST_FOREACH_MUTABLE(la, &llinfo_arp_list[mycpuid], la_le, nla) {
187 if (la->la_rt->rt_expire && la->la_rt->rt_expire <= time_second)
190 callout_reset(&arptimer_ch[mycpuid], arpt_prune * hz, arptimer, NULL);
195 * Parallel to llc_rtrequest.
197 * Called after a route is successfully added to the tree to fix-up the
198 * route and initiate arp operations if required.
201 arp_rtrequest(int req, struct rtentry *rt, struct rt_addrinfo *info)
203 struct sockaddr *gate = rt->rt_gateway;
204 struct llinfo_arp *la = rt->rt_llinfo;
206 struct sockaddr_dl null_sdl = { sizeof null_sdl, AF_LINK };
207 static boolean_t arpinit_done[MAXCPU];
209 if (!arpinit_done[mycpuid]) {
210 arpinit_done[mycpuid] = TRUE;
211 callout_init(&arptimer_ch[mycpuid]);
212 callout_reset(&arptimer_ch[mycpuid], hz, arptimer, NULL);
214 if (rt->rt_flags & RTF_GATEWAY)
220 * XXX: If this is a manually added route to interface
221 * such as older version of routed or gated might provide,
222 * restore cloning bit.
224 if (!(rt->rt_flags & RTF_HOST) &&
225 SIN(rt_mask(rt))->sin_addr.s_addr != 0xffffffff)
226 rt->rt_flags |= RTF_CLONING;
227 if (rt->rt_flags & RTF_CLONING) {
229 * Case 1: This route should come from a route to iface.
231 rt_setgate(rt, rt_key(rt),
232 (struct sockaddr *)&null_sdl,
234 gate = rt->rt_gateway;
235 SDL(gate)->sdl_type = rt->rt_ifp->if_type;
236 SDL(gate)->sdl_index = rt->rt_ifp->if_index;
237 rt->rt_expire = time_second;
240 /* Announce a new entry if requested. */
241 if (rt->rt_flags & RTF_ANNOUNCE) {
242 arprequest_async(rt->rt_ifp,
243 &SIN(rt_key(rt))->sin_addr,
244 &SIN(rt_key(rt))->sin_addr,
249 if (gate->sa_family != AF_LINK ||
250 gate->sa_len < sizeof(struct sockaddr_dl)) {
251 log(LOG_DEBUG, "arp_rtrequest: bad gateway value\n");
254 SDL(gate)->sdl_type = rt->rt_ifp->if_type;
255 SDL(gate)->sdl_index = rt->rt_ifp->if_index;
257 break; /* This happens on a route change */
259 * Case 2: This route may come from cloning, or a manual route
260 * add with a LL address.
262 R_Malloc(la, struct llinfo_arp *, sizeof *la);
265 log(LOG_DEBUG, "arp_rtrequest: malloc failed\n");
268 bzero(la, sizeof *la);
270 rt->rt_flags |= RTF_LLINFO;
271 LIST_INSERT_HEAD(&llinfo_arp_list[mycpuid], la, la_le);
275 * This keeps the multicast addresses from showing up
276 * in `arp -a' listings as unresolved. It's not actually
277 * functional. Then the same for broadcast.
279 if (IN_MULTICAST(ntohl(SIN(rt_key(rt))->sin_addr.s_addr))) {
280 ETHER_MAP_IP_MULTICAST(&SIN(rt_key(rt))->sin_addr,
282 SDL(gate)->sdl_alen = 6;
285 if (in_broadcast(SIN(rt_key(rt))->sin_addr, rt->rt_ifp)) {
286 memcpy(LLADDR(SDL(gate)), rt->rt_ifp->if_broadcastaddr,
287 rt->rt_ifp->if_addrlen);
288 SDL(gate)->sdl_alen = rt->rt_ifp->if_addrlen;
294 * This fixes up the routing interface for local addresses.
295 * The route is adjusted to point at lo0 and the expiration
298 * NOTE: This prevents locally targetted traffic from going
299 * out the hardware interface, which is inefficient
300 * and might not work if the hardware cannot listen
301 * to its own transmitted packets. Setting
302 * net.link.ether.inet.useloopback to 0 will force
303 * packets for local addresses out the hardware (and
304 * it is expected to receive its own packet).
306 * XXX We should just be able to test RTF_LOCAL here instead
307 * of having to compare IPs.
309 if (SIN(rt_key(rt))->sin_addr.s_addr ==
310 (IA_SIN(rt->rt_ifa))->sin_addr.s_addr) {
312 bcopy(IF_LLADDR(rt->rt_ifp), LLADDR(SDL(gate)),
313 SDL(gate)->sdl_alen = rt->rt_ifp->if_addrlen);
322 LIST_REMOVE(la, la_le);
323 rt->rt_llinfo = NULL;
324 rt->rt_flags &= ~RTF_LLINFO;
325 if (la->la_hold != NULL)
326 m_freem(la->la_hold);
333 arpreq_alloc(struct ifnet *ifp, const struct in_addr *sip,
334 const struct in_addr *tip, const u_char *enaddr)
340 if ((m = m_gethdr(MB_DONTWAIT, MT_DATA)) == NULL)
342 m->m_pkthdr.rcvif = NULL;
344 switch (ifp->if_type) {
347 * This may not be correct for types not explicitly
348 * listed, but this is our best guess
351 ar_hrd = htons(ARPHRD_ETHER);
353 m->m_len = arphdr_len2(ifp->if_addrlen, sizeof(struct in_addr));
354 m->m_pkthdr.len = m->m_len;
355 MH_ALIGN(m, m->m_len);
357 ah = mtod(m, struct arphdr *);
362 ah->ar_pro = htons(ETHERTYPE_IP);
363 ah->ar_hln = ifp->if_addrlen; /* hardware address length */
364 ah->ar_pln = sizeof(struct in_addr); /* protocol address length */
365 ah->ar_op = htons(ARPOP_REQUEST);
366 memcpy(ar_sha(ah), enaddr, ah->ar_hln);
367 memset(ar_tha(ah), 0, ah->ar_hln);
368 memcpy(ar_spa(ah), sip, ah->ar_pln);
369 memcpy(ar_tpa(ah), tip, ah->ar_pln);
375 arpreq_send(struct ifnet *ifp, struct mbuf *m)
378 struct ether_header *eh;
380 switch (ifp->if_type) {
383 * This may not be correct for types not explicitly
384 * listed, but this is our best guess
387 eh = (struct ether_header *)sa.sa_data;
388 /* if_output() will not swap */
389 eh->ether_type = htons(ETHERTYPE_ARP);
390 memcpy(eh->ether_dhost, ifp->if_broadcastaddr, ifp->if_addrlen);
394 sa.sa_family = AF_UNSPEC;
395 sa.sa_len = sizeof(sa);
396 ifp->if_output(ifp, m, &sa, NULL);
400 arpreq_send_handler(netmsg_t msg)
402 struct mbuf *m = msg->packet.nm_packet;
403 struct ifnet *ifp = msg->lmsg.u.ms_resultp;
406 /* nmsg was embedded in the mbuf, do not reply! */
410 * Broadcast an ARP request. Caller specifies:
411 * - arp header source ip address
412 * - arp header target ip address
413 * - arp header source ethernet address
415 * NOTE: Caller MUST NOT hold ifp's serializer
418 arprequest(struct ifnet *ifp, const struct in_addr *sip,
419 const struct in_addr *tip, const u_char *enaddr)
423 if (enaddr == NULL) {
424 if (ifp->if_bridge) {
425 enaddr = IF_LLADDR(ether_bridge_interface(ifp));
427 enaddr = IF_LLADDR(ifp);
431 m = arpreq_alloc(ifp, sip, tip, enaddr);
438 * Same as arprequest(), except:
439 * - Caller is allowed to hold ifp's serializer
440 * - Network output is done in protocol thead
443 arprequest_async(struct ifnet *ifp, const struct in_addr *sip,
444 const struct in_addr *tip, const u_char *enaddr)
447 struct netmsg_packet *pmsg;
449 if (enaddr == NULL) {
450 if (ifp->if_bridge) {
451 enaddr = IF_LLADDR(ether_bridge_interface(ifp));
453 enaddr = IF_LLADDR(ifp);
456 m = arpreq_alloc(ifp, sip, tip, enaddr);
460 pmsg = &m->m_hdr.mh_netmsg;
461 netmsg_init(&pmsg->base, NULL, &netisr_apanic_rport,
462 0, arpreq_send_handler);
464 pmsg->base.lmsg.u.ms_resultp = ifp;
466 lwkt_sendmsg(netisr_portfn(mycpuid), &pmsg->base.lmsg);
470 * Resolve an IP address into an ethernet address. If success,
471 * desten is filled in. If there is no entry in arptab,
472 * set one up and broadcast a request for the IP address.
473 * Hold onto this mbuf and resend it once the address
474 * is finally resolved. A return value of 1 indicates
475 * that desten has been filled in and the packet should be sent
476 * normally; a 0 return indicates that the packet has been
477 * taken over here, either now or for later transmission.
480 arpresolve(struct ifnet *ifp, struct rtentry *rt0, struct mbuf *m,
481 struct sockaddr *dst, u_char *desten)
484 struct llinfo_arp *la = NULL;
485 struct sockaddr_dl *sdl;
487 if (m->m_flags & M_BCAST) { /* broadcast */
488 memcpy(desten, ifp->if_broadcastaddr, ifp->if_addrlen);
491 if (m->m_flags & M_MCAST) {/* multicast */
492 ETHER_MAP_IP_MULTICAST(&SIN(dst)->sin_addr, desten);
496 if (rt_llroute(dst, rt0, &rt) != 0) {
503 la = arplookup(SIN(dst)->sin_addr.s_addr,
504 TRUE, RTL_REPORTMSG, FALSE);
508 if (la == NULL || rt == NULL) {
509 log(LOG_DEBUG, "arpresolve: can't allocate llinfo for %s%s%s\n",
510 inet_ntoa(SIN(dst)->sin_addr), la ? "la" : " ",
515 sdl = SDL(rt->rt_gateway);
517 * Check the address family and length is valid, the address
518 * is resolved; otherwise, try to resolve.
520 if ((rt->rt_expire == 0 || rt->rt_expire > time_second) &&
521 sdl->sdl_family == AF_LINK && sdl->sdl_alen != 0) {
523 * If entry has an expiry time and it is approaching,
524 * see if we need to send an ARP request within this
525 * arpt_down interval.
527 if ((rt->rt_expire != 0) &&
528 (time_second + la->la_preempt > rt->rt_expire)) {
530 &SIN(rt->rt_ifa->ifa_addr)->sin_addr,
536 bcopy(LLADDR(sdl), desten, sdl->sdl_alen);
540 * If ARP is disabled or static on this interface, stop.
542 * Probably should not allocate empty llinfo struct if we are
543 * not going to be sending out an arp request.
545 if (ifp->if_flags & (IFF_NOARP | IFF_STATICARP)) {
550 * There is an arptab entry, but no ethernet address
551 * response yet. Replace the held mbuf with this
554 if (la->la_hold != NULL)
555 m_freem(la->la_hold);
557 la->la_msgport = cur_netport();
558 if (rt->rt_expire || ((rt->rt_flags & RTF_STATIC) && !sdl->sdl_alen)) {
559 rt->rt_flags &= ~RTF_REJECT;
560 if (la->la_asked == 0 || rt->rt_expire != time_second) {
561 rt->rt_expire = time_second;
562 if (la->la_asked++ < arp_maxtries) {
564 &SIN(rt->rt_ifa->ifa_addr)->sin_addr,
568 rt->rt_flags |= RTF_REJECT;
569 rt->rt_expire += arpt_down;
571 la->la_preempt = arp_maxtries;
579 * Common length and type checks are done here,
580 * then the protocol-specific routine is called.
583 arpintr(netmsg_t msg)
585 struct mbuf *m = msg->packet.nm_packet;
590 if (m->m_len < sizeof(struct arphdr) &&
591 (m = m_pullup(m, sizeof(struct arphdr))) == NULL) {
592 log(LOG_ERR, "arp: runt packet -- m_pullup failed\n");
595 ar = mtod(m, struct arphdr *);
597 ar_hrd = ntohs(ar->ar_hrd);
598 if (ar_hrd != ARPHRD_ETHER && ar_hrd != ARPHRD_IEEE802) {
599 hexncpy((unsigned char *)&ar->ar_hrd, 2, hexstr, 5, NULL);
600 log(LOG_ERR, "arp: unknown hardware address format (0x%s)\n",
606 if (m->m_pkthdr.len < arphdr_len(ar)) {
607 if ((m = m_pullup(m, arphdr_len(ar))) == NULL) {
608 log(LOG_ERR, "arp: runt packet\n");
611 ar = mtod(m, struct arphdr *);
614 switch (ntohs(ar->ar_pro)) {
622 /* msg was embedded in the mbuf, do not reply! */
627 * ARP for Internet protocols on 10 Mb/s Ethernet.
628 * Algorithm is that given in RFC 826.
629 * In addition, a sanity check is performed on the sender
630 * protocol address, to catch impersonators.
631 * We no longer handle negotiations for use of trailer protocol:
632 * Formerly, ARP replied for protocol type ETHERTYPE_TRAIL sent
633 * along with IP replies if we wanted trailers sent to us,
634 * and also sent them in response to IP replies.
635 * This allowed either end to announce the desire to receive
637 * We no longer reply to requests for ETHERTYPE_TRAIL protocol either,
638 * but formerly didn't normally send requests.
641 static int log_arp_wrong_iface = 1;
642 static int log_arp_movements = 1;
643 static int log_arp_permanent_modify = 1;
645 SYSCTL_INT(_net_link_ether_inet, OID_AUTO, log_arp_wrong_iface, CTLFLAG_RW,
646 &log_arp_wrong_iface, 0,
647 "Log arp packets arriving on the wrong interface");
648 SYSCTL_INT(_net_link_ether_inet, OID_AUTO, log_arp_movements, CTLFLAG_RW,
649 &log_arp_movements, 0,
650 "Log arp replies from MACs different than the one in the cache");
651 SYSCTL_INT(_net_link_ether_inet, OID_AUTO, log_arp_permanent_modify, CTLFLAG_RW,
652 &log_arp_permanent_modify, 0,
653 "Log arp replies from MACs different than the one "
654 "in the permanent arp entry");
658 arp_hold_output(netmsg_t msg)
660 struct mbuf *m = msg->packet.nm_packet;
664 rt = msg->lmsg.u.ms_resultp;
665 ifp = m->m_pkthdr.rcvif;
666 m->m_pkthdr.rcvif = NULL;
668 ifp->if_output(ifp, m, rt_key(rt), rt);
670 /* Drop the reference count bumped by the sender */
673 /* nmsg was embedded in the mbuf, do not reply! */
677 arp_update_oncpu(struct mbuf *m, in_addr_t saddr, boolean_t create,
678 boolean_t generate_report, boolean_t dologging)
680 struct arphdr *ah = mtod(m, struct arphdr *);
681 struct ifnet *ifp = m->m_pkthdr.rcvif;
682 struct llinfo_arp *la;
683 struct sockaddr_dl *sdl;
687 la = arplookup(saddr, create, generate_report, FALSE);
688 if (la && (rt = la->la_rt) && (sdl = SDL(rt->rt_gateway))) {
689 struct in_addr isaddr = { saddr };
692 * Normally arps coming in on the wrong interface are ignored,
693 * but if we are bridging and the two interfaces belong to
694 * the same bridge, or one is a member of the bridge which
695 * is the other, then it isn't an error.
697 if (rt->rt_ifp != ifp) {
699 * (1) ifp and rt_ifp both members of same bridge
700 * (2) rt_ifp member of bridge ifp
701 * (3) ifp member of bridge rt_ifp
703 * Always replace rt_ifp with the bridge ifc.
707 if (ifp->if_bridge &&
708 rt->rt_ifp->if_bridge == ifp->if_bridge) {
709 nifp = ether_bridge_interface(ifp);
710 } else if (rt->rt_ifp->if_bridge &&
711 ether_bridge_interface(rt->rt_ifp) == ifp) {
713 } else if (ifp->if_bridge &&
714 ether_bridge_interface(ifp) == rt->rt_ifp) {
720 if ((log_arp_wrong_iface == 1 && nifp == NULL) ||
721 log_arp_wrong_iface == 2) {
722 hexncpy((u_char *)ar_sha(ah), ifp->if_addrlen,
723 hexstr[0], HEX_NCPYLEN(ifp->if_addrlen), ":");
726 "but got reply from %s on %s\n",
728 rt->rt_ifp->if_xname, hexstr[0],
735 * nifp is our man! Replace rt_ifp and adjust
738 ifp = rt->rt_ifp = nifp;
739 sdl->sdl_type = ifp->if_type;
740 sdl->sdl_index = ifp->if_index;
743 bcmp(ar_sha(ah), LLADDR(sdl), sdl->sdl_alen)) {
744 if (rt->rt_expire != 0) {
745 if (dologging && log_arp_movements) {
746 hexncpy((u_char *)LLADDR(sdl), ifp->if_addrlen,
747 hexstr[0], HEX_NCPYLEN(ifp->if_addrlen), ":");
748 hexncpy((u_char *)ar_sha(ah), ifp->if_addrlen,
749 hexstr[1], HEX_NCPYLEN(ifp->if_addrlen), ":");
751 "arp: %s moved from %s to %s on %s\n",
752 inet_ntoa(isaddr), hexstr[0], hexstr[1],
756 if (dologging && log_arp_permanent_modify) {
757 hexncpy((u_char *)ar_sha(ah), ifp->if_addrlen,
758 hexstr[0], HEX_NCPYLEN(ifp->if_addrlen), ":");
760 "arp: %s attempts to modify "
761 "permanent entry for %s on %s\n",
762 hexstr[0], inet_ntoa(isaddr), ifp->if_xname);
768 * sanity check for the address length.
769 * XXX this does not work for protocols with variable address
772 if (dologging && sdl->sdl_alen && sdl->sdl_alen != ah->ar_hln) {
773 hexncpy((u_char *)ar_sha(ah), ifp->if_addrlen,
774 hexstr[0], HEX_NCPYLEN(ifp->if_addrlen), ":");
776 "arp from %s: new addr len %d, was %d",
777 hexstr[0], ah->ar_hln, sdl->sdl_alen);
779 if (ifp->if_addrlen != ah->ar_hln) {
781 hexncpy((u_char *)ar_sha(ah), ifp->if_addrlen,
782 hexstr[0], HEX_NCPYLEN(ifp->if_addrlen), ":");
784 "arp from %s: addr len: new %d, i/f %d "
785 "(ignored)", hexstr[0],
786 ah->ar_hln, ifp->if_addrlen);
790 memcpy(LLADDR(sdl), ar_sha(ah), sdl->sdl_alen = ah->ar_hln);
791 if (rt->rt_expire != 0) {
792 rt->rt_expire = time_second + arpt_keep;
794 rt->rt_flags &= ~RTF_REJECT;
796 la->la_preempt = arp_maxtries;
799 * This particular cpu might have been holding an mbuf
800 * pending ARP resolution. If so, transmit the mbuf now.
802 if (la->la_hold != NULL) {
803 struct mbuf *m = la->la_hold;
804 struct lwkt_port *port = la->la_msgport;
805 struct netmsg_packet *pmsg;
808 la->la_msgport = NULL;
810 m_adj(m, sizeof(struct ether_header));
813 * Make sure that this rtentry will not be freed
814 * before the packet is processed on the target
815 * msgport. The reference count will be dropped
816 * in the handler associated with this packet.
820 pmsg = &m->m_hdr.mh_netmsg;
821 netmsg_init(&pmsg->base, NULL,
822 &netisr_apanic_rport,
823 MSGF_PRIORITY, arp_hold_output);
826 /* Record necessary information */
827 m->m_pkthdr.rcvif = ifp;
828 pmsg->base.lmsg.u.ms_resultp = rt;
830 lwkt_sendmsg(port, &pmsg->base.lmsg);
836 * Called from arpintr() - this routine is run from a single cpu.
839 in_arpinput(struct mbuf *m)
842 struct ifnet *ifp = m->m_pkthdr.rcvif;
843 struct ifaddr_container *ifac;
844 struct in_ifaddr_container *iac;
845 struct in_ifaddr *ia = NULL;
846 struct in_addr isaddr, itaddr, myaddr;
847 struct netmsg_inarp *msg;
848 uint8_t *enaddr = NULL;
852 req_len = arphdr_len2(ifp->if_addrlen, sizeof(struct in_addr));
853 if (m->m_len < req_len && (m = m_pullup(m, req_len)) == NULL) {
854 log(LOG_ERR, "in_arp: runt packet -- m_pullup failed\n");
858 ah = mtod(m, struct arphdr *);
859 memcpy(&isaddr, ar_spa(ah), sizeof isaddr);
860 memcpy(&itaddr, ar_tpa(ah), sizeof itaddr);
863 * Check both target and sender IP addresses:
865 * If we receive the packet on the interface owning the address,
866 * then accept the address.
868 * For a bridge, we accept the address if the receive interface and
869 * the interface owning the address are on the same bridge, and
870 * use the bridge MAC as the is-at response. The bridge will be
871 * responsible for handling the packet.
873 * (0) Check target IP against CARP IPs
876 LIST_FOREACH(iac, INADDR_HASH(itaddr.s_addr), ia_hash) {
877 int is_match = 0, is_parent = 0;
881 /* Skip all ia's which don't match */
882 if (itaddr.s_addr != ia->ia_addr.sin_addr.s_addr)
885 if (ia->ia_ifp->if_type != IFT_CARP)
888 if (carp_parent(ia->ia_ifp) == ifp)
890 if (is_parent || ia->ia_ifp == ifp)
891 is_match = carp_iamatch(ia);
896 * The parent interface will also receive
897 * the ethernet broadcast packets, e.g. ARP
898 * REQUEST, so if we could find a CARP
899 * interface of the parent that could match
900 * the target IP address, we then drop the
901 * packets, which is delieverd to us through
902 * the parent interface.
913 * (1) Check target IP against our local IPs
915 LIST_FOREACH(iac, INADDR_HASH(itaddr.s_addr), ia_hash) {
918 /* Skip all ia's which don't match */
919 if (itaddr.s_addr != ia->ia_addr.sin_addr.s_addr)
923 /* CARP interfaces are checked in (0) */
924 if (ia->ia_ifp->if_type == IFT_CARP)
928 if (ifp->if_bridge && ia->ia_ifp &&
929 ifp->if_bridge == ia->ia_ifp->if_bridge) {
930 ifp = ether_bridge_interface(ifp);
933 if (ia->ia_ifp && ia->ia_ifp->if_bridge &&
934 ether_bridge_interface(ia->ia_ifp) == ifp) {
937 if (ifp->if_bridge && ether_bridge_interface(ifp) ==
941 if (ia->ia_ifp == ifp) {
947 * (2) Check sender IP against our local IPs
949 LIST_FOREACH(iac, INADDR_HASH(isaddr.s_addr), ia_hash) {
952 /* Skip all ia's which don't match */
953 if (isaddr.s_addr != ia->ia_addr.sin_addr.s_addr)
956 if (ifp->if_bridge && ia->ia_ifp &&
957 ifp->if_bridge == ia->ia_ifp->if_bridge) {
958 ifp = ether_bridge_interface(ifp);
961 if (ia->ia_ifp && ia->ia_ifp->if_bridge &&
962 ether_bridge_interface(ia->ia_ifp) == ifp) {
965 if (ifp->if_bridge && ether_bridge_interface(ifp) ==
970 if (ia->ia_ifp == ifp)
975 * No match, use the first inet address on the receive interface
976 * as a dummy address for the rest of the function.
978 TAILQ_FOREACH(ifac, &ifp->if_addrheads[mycpuid], ifa_link) {
979 struct ifaddr *ifa = ifac->ifa;
981 if (ifa->ifa_addr && ifa->ifa_addr->sa_family == AF_INET) {
988 * If we got here, we didn't find any suitable interface,
989 * so drop the packet.
996 enaddr = (uint8_t *)IF_LLADDR(ifp);
997 myaddr = ia->ia_addr.sin_addr;
998 if (!bcmp(ar_sha(ah), enaddr, ifp->if_addrlen)) {
999 m_freem(m); /* it's from me, ignore it. */
1002 if (!bcmp(ar_sha(ah), ifp->if_broadcastaddr, ifp->if_addrlen)) {
1004 "arp: link address is broadcast for IP address %s!\n",
1009 if (isaddr.s_addr == myaddr.s_addr && myaddr.s_addr != 0) {
1010 hexncpy((u_char *)ar_sha(ah), ifp->if_addrlen,
1011 hexstr, HEX_NCPYLEN(ifp->if_addrlen), ":");
1013 "arp: %s is using my IP address %s!\n",
1014 hexstr, inet_ntoa(isaddr));
1018 if (ifp->if_flags & IFF_STATICARP)
1022 * When arp_restricted_match is true and the ARP response is not
1023 * specifically targetted to me, ignore it. Otherwise the entry
1024 * timeout may be updated for an old MAC.
1026 if (arp_restricted_match && itaddr.s_addr != myaddr.s_addr) {
1032 * Update all CPU's routing tables with this ARP packet
1034 msg = &m->m_hdr.mh_arpmsg;
1035 netmsg_init(&msg->base, NULL, &netisr_apanic_rport,
1036 0, arp_update_msghandler);
1038 msg->saddr = isaddr.s_addr;
1039 msg->taddr = itaddr.s_addr;
1040 msg->myaddr = myaddr.s_addr;
1041 lwkt_sendmsg(rtable_portfn(0), &msg->base.lmsg);
1044 * Just return here; after all CPUs's routing tables are
1045 * properly updated by this ARP packet, an ARP reply will
1046 * be generated if appropriate.
1050 in_arpreply(m, itaddr.s_addr, myaddr.s_addr);
1054 arp_reply_msghandler(netmsg_t msg)
1056 struct netmsg_inarp *rmsg = (struct netmsg_inarp *)msg;
1058 in_arpreply(rmsg->m, rmsg->taddr, rmsg->myaddr);
1059 /* Don't reply this netmsg; netmsg_inarp is embedded in mbuf */
1063 arp_update_msghandler(netmsg_t msg)
1065 struct netmsg_inarp *rmsg = (struct netmsg_inarp *)msg;
1069 * This message handler will be called on all of the CPUs,
1070 * however, we only need to generate rtmsg on CPU0.
1072 arp_update_oncpu(rmsg->m, rmsg->saddr, rmsg->taddr == rmsg->myaddr,
1073 mycpuid == 0 ? RTL_REPORTMSG : RTL_DONTREPORT, mycpuid == 0);
1075 nextcpu = mycpuid + 1;
1076 if (nextcpu < ncpus) {
1077 lwkt_forwardmsg(rtable_portfn(nextcpu), &rmsg->base.lmsg);
1079 struct mbuf *m = rmsg->m;
1080 in_addr_t saddr = rmsg->saddr;
1081 in_addr_t taddr = rmsg->taddr;
1082 in_addr_t myaddr = rmsg->myaddr;
1085 * Dispatch this mbuf to netisr0 to perform ARP reply,
1087 * NOTE: netmsg_inarp is embedded in this mbuf.
1089 netmsg_init(&rmsg->base, NULL, &netisr_apanic_rport,
1090 0, arp_reply_msghandler);
1092 rmsg->saddr = saddr;
1093 rmsg->taddr = taddr;
1094 rmsg->myaddr = myaddr;
1095 lwkt_sendmsg(netisr_portfn(0), &rmsg->base.lmsg);
1100 in_arpreply(struct mbuf *m, in_addr_t taddr, in_addr_t myaddr)
1102 struct ifnet *ifp = m->m_pkthdr.rcvif;
1103 const uint8_t *enaddr;
1106 struct ether_header *eh;
1108 ah = mtod(m, struct arphdr *);
1109 if (ntohs(ah->ar_op) != ARPOP_REQUEST) {
1114 enaddr = (const uint8_t *)IF_LLADDR(ifp);
1115 if (taddr == myaddr) {
1116 /* I am the target */
1117 memcpy(ar_tha(ah), ar_sha(ah), ah->ar_hln);
1118 memcpy(ar_sha(ah), enaddr, ah->ar_hln);
1120 struct llinfo_arp *la;
1123 la = arplookup(taddr, FALSE, RTL_DONTREPORT, SIN_PROXY);
1125 struct sockaddr_in sin;
1127 if (!arp_proxyall) {
1132 bzero(&sin, sizeof sin);
1133 sin.sin_family = AF_INET;
1134 sin.sin_len = sizeof sin;
1135 sin.sin_addr.s_addr = taddr;
1137 rt = rtpurelookup((struct sockaddr *)&sin);
1144 * Don't send proxies for nodes on the same interface
1145 * as this one came out of, or we'll get into a fight
1146 * over who claims what Ether address.
1148 if (rt->rt_ifp == ifp) {
1152 memcpy(ar_tha(ah), ar_sha(ah), ah->ar_hln);
1153 memcpy(ar_sha(ah), enaddr, ah->ar_hln);
1155 kprintf("arp: proxying for %s\n", inet_ntoa(itaddr));
1158 struct sockaddr_dl *sdl;
1161 memcpy(ar_tha(ah), ar_sha(ah), ah->ar_hln);
1162 sdl = SDL(rt->rt_gateway);
1163 memcpy(ar_sha(ah), LLADDR(sdl), ah->ar_hln);
1167 memcpy(ar_tpa(ah), ar_spa(ah), ah->ar_pln);
1168 memcpy(ar_spa(ah), &taddr, ah->ar_pln);
1169 ah->ar_op = htons(ARPOP_REPLY);
1170 ah->ar_pro = htons(ETHERTYPE_IP); /* let's be sure! */
1171 switch (ifp->if_type) {
1174 * May not be correct for types not explictly
1175 * listed, but it is our best guess.
1178 eh = (struct ether_header *)sa.sa_data;
1179 memcpy(eh->ether_dhost, ar_tha(ah), sizeof eh->ether_dhost);
1180 eh->ether_type = htons(ETHERTYPE_ARP);
1183 sa.sa_family = AF_UNSPEC;
1184 sa.sa_len = sizeof sa;
1185 ifp->if_output(ifp, m, &sa, NULL);
1191 * Free an arp entry. If the arp entry is actively referenced or represents
1192 * a static entry we only clear it back to an unresolved state, otherwise
1193 * we destroy the entry entirely.
1195 * Note that static entries are created when route add ... -interface is used
1196 * to create an interface route to a (direct) destination.
1199 arptfree(struct llinfo_arp *la)
1201 struct rtentry *rt = la->la_rt;
1202 struct sockaddr_dl *sdl;
1206 sdl = SDL(rt->rt_gateway);
1208 ((rt->rt_refcnt > 0 && sdl->sdl_family == AF_LINK) ||
1209 (rt->rt_flags & RTF_STATIC))) {
1211 la->la_preempt = la->la_asked = 0;
1212 rt->rt_flags &= ~RTF_REJECT;
1215 rtrequest(RTM_DELETE, rt_key(rt), NULL, rt_mask(rt), 0, NULL);
1219 * Lookup or enter a new address in arptab.
1221 static struct llinfo_arp *
1222 arplookup(in_addr_t addr, boolean_t create, boolean_t generate_report,
1226 struct sockaddr_inarp sin = { sizeof sin, AF_INET };
1227 const char *why = NULL;
1229 sin.sin_addr.s_addr = addr;
1230 sin.sin_other = proxy ? SIN_PROXY : 0;
1232 rt = _rtlookup((struct sockaddr *)&sin,
1233 generate_report, RTL_DOCLONE);
1235 rt = rtpurelookup((struct sockaddr *)&sin);
1241 if (rt->rt_flags & RTF_GATEWAY)
1242 why = "host is not on local network";
1243 else if (!(rt->rt_flags & RTF_LLINFO))
1244 why = "could not allocate llinfo";
1245 else if (rt->rt_gateway->sa_family != AF_LINK)
1246 why = "gateway route is not ours";
1250 log(LOG_DEBUG, "arplookup %s failed: %s\n",
1251 inet_ntoa(sin.sin_addr), why);
1253 if (rt->rt_refcnt <= 0 && (rt->rt_flags & RTF_WASCLONED)) {
1254 /* No references to this route. Purge it. */
1255 rtrequest(RTM_DELETE, rt_key(rt), rt->rt_gateway,
1256 rt_mask(rt), rt->rt_flags, NULL);
1260 return (rt->rt_llinfo);
1264 arp_ifinit(struct ifnet *ifp, struct ifaddr *ifa)
1266 ifa->ifa_rtrequest = arp_rtrequest;
1267 ifa->ifa_flags |= RTF_CLONING;
1271 arp_gratuitous(struct ifnet *ifp, struct ifaddr *ifa)
1273 if (IA_SIN(ifa)->sin_addr.s_addr != INADDR_ANY) {
1274 arprequest_async(ifp, &IA_SIN(ifa)->sin_addr,
1275 &IA_SIN(ifa)->sin_addr, NULL);
1280 arp_ifaddr(void *arg __unused, struct ifnet *ifp,
1281 enum ifaddr_event event, struct ifaddr *ifa)
1283 if (ifa->ifa_rtrequest != arp_rtrequest) /* XXX need a generic way */
1285 if (ifa->ifa_addr->sa_family != AF_INET)
1287 if (event == IFADDR_EVENT_DELETE)
1291 * - CARP interfaces will take care of gratuitous ARP themselves.
1292 * - If we are the CARP interface's parent, don't send gratuitous
1293 * ARP to avoid unnecessary confusion.
1296 if (ifp->if_type != IFT_CARP && ifp->if_carp == NULL)
1299 arp_gratuitous(ifp, ifa);
1308 for (cpu = 0; cpu < ncpus2; cpu++)
1309 LIST_INIT(&llinfo_arp_list[cpu]);
1311 netisr_register(NETISR_ARP, arpintr, NULL);
1313 EVENTHANDLER_REGISTER(ifaddr_event, arp_ifaddr, NULL,
1314 EVENTHANDLER_PRI_LAST);
1317 SYSINIT(arp, SI_SUB_PROTO_DOMAIN, SI_ORDER_ANY, arp_init, 0);