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 <net/netisr2.h>
104 #include <sys/mplock2.h>
107 #include <netinet/ip_carp.h>
110 #define SIN(s) ((struct sockaddr_in *)s)
111 #define SDL(s) ((struct sockaddr_dl *)s)
113 SYSCTL_DECL(_net_link_ether);
114 SYSCTL_NODE(_net_link_ether, PF_INET, inet, CTLFLAG_RW, 0, "");
117 static int arpt_prune = (5*60*1); /* walk list every 5 minutes */
118 static int arpt_keep = (20*60); /* once resolved, good for 20 more minutes */
119 static int arpt_down = 20; /* once declared down, don't send for 20 sec */
121 SYSCTL_INT(_net_link_ether_inet, OID_AUTO, prune_intvl, CTLFLAG_RW,
123 SYSCTL_INT(_net_link_ether_inet, OID_AUTO, max_age, CTLFLAG_RW,
125 SYSCTL_INT(_net_link_ether_inet, OID_AUTO, host_down_time, CTLFLAG_RW,
128 #define rt_expire rt_rmx.rmx_expire
131 LIST_ENTRY(llinfo_arp) la_le;
132 struct rtentry *la_rt;
133 struct mbuf *la_hold; /* last packet until resolved/timeout */
134 struct lwkt_port *la_msgport; /* last packet's msgport */
135 u_short la_preempt; /* countdown for pre-expiry arps */
136 u_short la_asked; /* #times we QUERIED following expiration */
139 static LIST_HEAD(, llinfo_arp) llinfo_arp_list[MAXCPU];
141 static int arp_maxtries = 5;
142 static int useloopback = 1; /* use loopback interface for local traffic */
143 static int arp_proxyall = 0;
144 static int arp_refresh = 60; /* refresh arp cache ~60 (not impl yet) */
145 static int arp_restricted_match = 0;
147 SYSCTL_INT(_net_link_ether_inet, OID_AUTO, maxtries, CTLFLAG_RW,
148 &arp_maxtries, 0, "ARP resolution attempts before returning error");
149 SYSCTL_INT(_net_link_ether_inet, OID_AUTO, useloopback, CTLFLAG_RW,
150 &useloopback, 0, "Use the loopback interface for local traffic");
151 SYSCTL_INT(_net_link_ether_inet, OID_AUTO, proxyall, CTLFLAG_RW,
152 &arp_proxyall, 0, "Enable proxy ARP for all suitable requests");
153 SYSCTL_INT(_net_link_ether_inet, OID_AUTO, restricted_match, CTLFLAG_RW,
154 &arp_restricted_match, 0, "Only match against the sender");
155 SYSCTL_INT(_net_link_ether_inet, OID_AUTO, refresh, CTLFLAG_RW,
156 &arp_refresh, 0, "Preemptively refresh the ARP");
158 static void arp_rtrequest(int, struct rtentry *, struct rt_addrinfo *);
159 static void arprequest(struct ifnet *, const struct in_addr *,
160 const struct in_addr *, const u_char *);
161 static void arprequest_async(struct ifnet *, const struct in_addr *,
162 const struct in_addr *, const u_char *);
163 static void arpintr(netmsg_t msg);
164 static void arptfree(struct llinfo_arp *);
165 static void arptimer(void *);
166 static struct llinfo_arp *
167 arplookup(in_addr_t, boolean_t, boolean_t, boolean_t);
169 static void in_arpinput(struct mbuf *);
170 static void in_arpreply(struct mbuf *m, in_addr_t, in_addr_t);
171 static void arp_update_msghandler(netmsg_t);
172 static void arp_reply_msghandler(netmsg_t);
175 static struct callout arptimer_ch[MAXCPU];
178 * Timeout routine. Age arp_tab entries periodically.
182 arptimer(void *ignored_arg)
184 struct llinfo_arp *la, *nla;
187 LIST_FOREACH_MUTABLE(la, &llinfo_arp_list[mycpuid], la_le, nla) {
188 if (la->la_rt->rt_expire && la->la_rt->rt_expire <= time_second)
191 callout_reset(&arptimer_ch[mycpuid], arpt_prune * hz, arptimer, NULL);
196 * Parallel to llc_rtrequest.
198 * Called after a route is successfully added to the tree to fix-up the
199 * route and initiate arp operations if required.
202 arp_rtrequest(int req, struct rtentry *rt, struct rt_addrinfo *info)
204 struct sockaddr *gate = rt->rt_gateway;
205 struct llinfo_arp *la = rt->rt_llinfo;
207 struct sockaddr_dl null_sdl = { sizeof null_sdl, AF_LINK };
208 static boolean_t arpinit_done[MAXCPU];
210 if (!arpinit_done[mycpuid]) {
211 arpinit_done[mycpuid] = TRUE;
212 callout_init(&arptimer_ch[mycpuid]);
213 callout_reset(&arptimer_ch[mycpuid], hz, arptimer, NULL);
215 if (rt->rt_flags & RTF_GATEWAY)
221 * XXX: If this is a manually added route to interface
222 * such as older version of routed or gated might provide,
223 * restore cloning bit.
225 if (!(rt->rt_flags & RTF_HOST) &&
226 SIN(rt_mask(rt))->sin_addr.s_addr != 0xffffffff)
227 rt->rt_flags |= RTF_CLONING;
228 if (rt->rt_flags & RTF_CLONING) {
230 * Case 1: This route should come from a route to iface.
232 rt_setgate(rt, rt_key(rt),
233 (struct sockaddr *)&null_sdl,
235 gate = rt->rt_gateway;
236 SDL(gate)->sdl_type = rt->rt_ifp->if_type;
237 SDL(gate)->sdl_index = rt->rt_ifp->if_index;
238 rt->rt_expire = time_second;
241 /* Announce a new entry if requested. */
242 if (rt->rt_flags & RTF_ANNOUNCE) {
243 arprequest_async(rt->rt_ifp,
244 &SIN(rt_key(rt))->sin_addr,
245 &SIN(rt_key(rt))->sin_addr,
250 if (gate->sa_family != AF_LINK ||
251 gate->sa_len < sizeof(struct sockaddr_dl)) {
252 log(LOG_DEBUG, "arp_rtrequest: bad gateway value\n");
255 SDL(gate)->sdl_type = rt->rt_ifp->if_type;
256 SDL(gate)->sdl_index = rt->rt_ifp->if_index;
258 break; /* This happens on a route change */
260 * Case 2: This route may come from cloning, or a manual route
261 * add with a LL address.
263 R_Malloc(la, struct llinfo_arp *, sizeof *la);
266 log(LOG_DEBUG, "arp_rtrequest: malloc failed\n");
269 bzero(la, sizeof *la);
271 rt->rt_flags |= RTF_LLINFO;
272 LIST_INSERT_HEAD(&llinfo_arp_list[mycpuid], la, la_le);
276 * This keeps the multicast addresses from showing up
277 * in `arp -a' listings as unresolved. It's not actually
278 * functional. Then the same for broadcast.
280 if (IN_MULTICAST(ntohl(SIN(rt_key(rt))->sin_addr.s_addr))) {
281 ETHER_MAP_IP_MULTICAST(&SIN(rt_key(rt))->sin_addr,
283 SDL(gate)->sdl_alen = 6;
286 if (in_broadcast(SIN(rt_key(rt))->sin_addr, rt->rt_ifp)) {
287 memcpy(LLADDR(SDL(gate)), rt->rt_ifp->if_broadcastaddr,
288 rt->rt_ifp->if_addrlen);
289 SDL(gate)->sdl_alen = rt->rt_ifp->if_addrlen;
295 * This fixes up the routing interface for local addresses.
296 * The route is adjusted to point at lo0 and the expiration
299 * NOTE: This prevents locally targetted traffic from going
300 * out the hardware interface, which is inefficient
301 * and might not work if the hardware cannot listen
302 * to its own transmitted packets. Setting
303 * net.link.ether.inet.useloopback to 0 will force
304 * packets for local addresses out the hardware (and
305 * it is expected to receive its own packet).
307 * XXX We should just be able to test RTF_LOCAL here instead
308 * of having to compare IPs.
310 if (SIN(rt_key(rt))->sin_addr.s_addr ==
311 (IA_SIN(rt->rt_ifa))->sin_addr.s_addr) {
313 bcopy(IF_LLADDR(rt->rt_ifp), LLADDR(SDL(gate)),
314 SDL(gate)->sdl_alen = rt->rt_ifp->if_addrlen);
323 LIST_REMOVE(la, la_le);
324 rt->rt_llinfo = NULL;
325 rt->rt_flags &= ~RTF_LLINFO;
326 if (la->la_hold != NULL)
327 m_freem(la->la_hold);
334 arpreq_alloc(struct ifnet *ifp, const struct in_addr *sip,
335 const struct in_addr *tip, const u_char *enaddr)
341 if ((m = m_gethdr(MB_DONTWAIT, MT_DATA)) == NULL)
343 m->m_pkthdr.rcvif = NULL;
345 switch (ifp->if_type) {
348 * This may not be correct for types not explicitly
349 * listed, but this is our best guess
352 ar_hrd = htons(ARPHRD_ETHER);
354 m->m_len = arphdr_len2(ifp->if_addrlen, sizeof(struct in_addr));
355 m->m_pkthdr.len = m->m_len;
356 MH_ALIGN(m, m->m_len);
358 ah = mtod(m, struct arphdr *);
363 ah->ar_pro = htons(ETHERTYPE_IP);
364 ah->ar_hln = ifp->if_addrlen; /* hardware address length */
365 ah->ar_pln = sizeof(struct in_addr); /* protocol address length */
366 ah->ar_op = htons(ARPOP_REQUEST);
367 memcpy(ar_sha(ah), enaddr, ah->ar_hln);
368 memset(ar_tha(ah), 0, ah->ar_hln);
369 memcpy(ar_spa(ah), sip, ah->ar_pln);
370 memcpy(ar_tpa(ah), tip, ah->ar_pln);
376 arpreq_send(struct ifnet *ifp, struct mbuf *m)
379 struct ether_header *eh;
381 switch (ifp->if_type) {
384 * This may not be correct for types not explicitly
385 * listed, but this is our best guess
388 eh = (struct ether_header *)sa.sa_data;
389 /* if_output() will not swap */
390 eh->ether_type = htons(ETHERTYPE_ARP);
391 memcpy(eh->ether_dhost, ifp->if_broadcastaddr, ifp->if_addrlen);
395 sa.sa_family = AF_UNSPEC;
396 sa.sa_len = sizeof(sa);
397 ifp->if_output(ifp, m, &sa, NULL);
401 arpreq_send_handler(netmsg_t msg)
403 struct mbuf *m = msg->packet.nm_packet;
404 struct ifnet *ifp = msg->lmsg.u.ms_resultp;
407 /* nmsg was embedded in the mbuf, do not reply! */
411 * Broadcast an ARP request. Caller specifies:
412 * - arp header source ip address
413 * - arp header target ip address
414 * - arp header source ethernet address
416 * NOTE: Caller MUST NOT hold ifp's serializer
419 arprequest(struct ifnet *ifp, const struct in_addr *sip,
420 const struct in_addr *tip, const u_char *enaddr)
424 if (enaddr == NULL) {
425 if (ifp->if_bridge) {
426 enaddr = IF_LLADDR(ether_bridge_interface(ifp));
428 enaddr = IF_LLADDR(ifp);
432 m = arpreq_alloc(ifp, sip, tip, enaddr);
439 * Same as arprequest(), except:
440 * - Caller is allowed to hold ifp's serializer
441 * - Network output is done in protocol thead
444 arprequest_async(struct ifnet *ifp, const struct in_addr *sip,
445 const struct in_addr *tip, const u_char *enaddr)
448 struct netmsg_packet *pmsg;
450 if (enaddr == NULL) {
451 if (ifp->if_bridge) {
452 enaddr = IF_LLADDR(ether_bridge_interface(ifp));
454 enaddr = IF_LLADDR(ifp);
457 m = arpreq_alloc(ifp, sip, tip, enaddr);
461 pmsg = &m->m_hdr.mh_netmsg;
462 netmsg_init(&pmsg->base, NULL, &netisr_apanic_rport,
463 0, arpreq_send_handler);
465 pmsg->base.lmsg.u.ms_resultp = ifp;
467 lwkt_sendmsg(netisr_cpuport(mycpuid), &pmsg->base.lmsg);
471 * Resolve an IP address into an ethernet address. If success,
472 * desten is filled in. If there is no entry in arptab,
473 * set one up and broadcast a request for the IP address.
474 * Hold onto this mbuf and resend it once the address
475 * is finally resolved. A return value of 1 indicates
476 * that desten has been filled in and the packet should be sent
477 * normally; a 0 return indicates that the packet has been
478 * taken over here, either now or for later transmission.
481 arpresolve(struct ifnet *ifp, struct rtentry *rt0, struct mbuf *m,
482 struct sockaddr *dst, u_char *desten)
484 struct rtentry *rt = NULL;
485 struct llinfo_arp *la = NULL;
486 struct sockaddr_dl *sdl;
488 if (m->m_flags & M_BCAST) { /* broadcast */
489 memcpy(desten, ifp->if_broadcastaddr, ifp->if_addrlen);
492 if (m->m_flags & M_MCAST) {/* multicast */
493 ETHER_MAP_IP_MULTICAST(&SIN(dst)->sin_addr, desten);
497 if (rt_llroute(dst, rt0, &rt) != 0) {
504 la = arplookup(SIN(dst)->sin_addr.s_addr,
505 TRUE, RTL_REPORTMSG, FALSE);
509 if (la == NULL || rt == NULL) {
510 log(LOG_DEBUG, "arpresolve: can't allocate llinfo for %s%s%s\n",
511 inet_ntoa(SIN(dst)->sin_addr), la ? "la" : " ",
516 sdl = SDL(rt->rt_gateway);
518 * Check the address family and length is valid, the address
519 * is resolved; otherwise, try to resolve.
521 if ((rt->rt_expire == 0 || rt->rt_expire > time_second) &&
522 sdl->sdl_family == AF_LINK && sdl->sdl_alen != 0) {
524 * If entry has an expiry time and it is approaching,
525 * see if we need to send an ARP request within this
526 * arpt_down interval.
528 if ((rt->rt_expire != 0) &&
529 (time_second + la->la_preempt > rt->rt_expire)) {
531 &SIN(rt->rt_ifa->ifa_addr)->sin_addr,
537 bcopy(LLADDR(sdl), desten, sdl->sdl_alen);
541 * If ARP is disabled or static on this interface, stop.
543 * Probably should not allocate empty llinfo struct if we are
544 * not going to be sending out an arp request.
546 if (ifp->if_flags & (IFF_NOARP | IFF_STATICARP)) {
551 * There is an arptab entry, but no ethernet address
552 * response yet. Replace the held mbuf with this
555 if (la->la_hold != NULL)
556 m_freem(la->la_hold);
558 la->la_msgport = netisr_curport();
559 if (rt->rt_expire || ((rt->rt_flags & RTF_STATIC) && !sdl->sdl_alen)) {
560 rt->rt_flags &= ~RTF_REJECT;
561 if (la->la_asked == 0 || rt->rt_expire != time_second) {
562 rt->rt_expire = time_second;
563 if (la->la_asked++ < arp_maxtries) {
565 &SIN(rt->rt_ifa->ifa_addr)->sin_addr,
569 rt->rt_flags |= RTF_REJECT;
570 rt->rt_expire += arpt_down;
572 la->la_preempt = arp_maxtries;
580 * Common length and type checks are done here,
581 * then the protocol-specific routine is called.
584 arpintr(netmsg_t msg)
586 struct mbuf *m = msg->packet.nm_packet;
591 if (m->m_len < sizeof(struct arphdr) &&
592 (m = m_pullup(m, sizeof(struct arphdr))) == NULL) {
593 log(LOG_ERR, "arp: runt packet -- m_pullup failed\n");
596 ar = mtod(m, struct arphdr *);
598 ar_hrd = ntohs(ar->ar_hrd);
599 if (ar_hrd != ARPHRD_ETHER && ar_hrd != ARPHRD_IEEE802) {
600 hexncpy((unsigned char *)&ar->ar_hrd, 2, hexstr, 5, NULL);
601 log(LOG_ERR, "arp: unknown hardware address format (0x%s)\n",
607 if (m->m_pkthdr.len < arphdr_len(ar)) {
608 if ((m = m_pullup(m, arphdr_len(ar))) == NULL) {
609 log(LOG_ERR, "arp: runt packet\n");
612 ar = mtod(m, struct arphdr *);
615 switch (ntohs(ar->ar_pro)) {
623 /* msg was embedded in the mbuf, do not reply! */
628 * ARP for Internet protocols on 10 Mb/s Ethernet.
629 * Algorithm is that given in RFC 826.
630 * In addition, a sanity check is performed on the sender
631 * protocol address, to catch impersonators.
632 * We no longer handle negotiations for use of trailer protocol:
633 * Formerly, ARP replied for protocol type ETHERTYPE_TRAIL sent
634 * along with IP replies if we wanted trailers sent to us,
635 * and also sent them in response to IP replies.
636 * This allowed either end to announce the desire to receive
638 * We no longer reply to requests for ETHERTYPE_TRAIL protocol either,
639 * but formerly didn't normally send requests.
642 static int log_arp_wrong_iface = 1;
643 static int log_arp_movements = 1;
644 static int log_arp_permanent_modify = 1;
646 SYSCTL_INT(_net_link_ether_inet, OID_AUTO, log_arp_wrong_iface, CTLFLAG_RW,
647 &log_arp_wrong_iface, 0,
648 "Log arp packets arriving on the wrong interface");
649 SYSCTL_INT(_net_link_ether_inet, OID_AUTO, log_arp_movements, CTLFLAG_RW,
650 &log_arp_movements, 0,
651 "Log arp replies from MACs different than the one in the cache");
652 SYSCTL_INT(_net_link_ether_inet, OID_AUTO, log_arp_permanent_modify, CTLFLAG_RW,
653 &log_arp_permanent_modify, 0,
654 "Log arp replies from MACs different than the one "
655 "in the permanent arp entry");
659 arp_hold_output(netmsg_t msg)
661 struct mbuf *m = msg->packet.nm_packet;
665 rt = msg->lmsg.u.ms_resultp;
666 ifp = m->m_pkthdr.rcvif;
667 m->m_pkthdr.rcvif = NULL;
669 ifp->if_output(ifp, m, rt_key(rt), rt);
671 /* Drop the reference count bumped by the sender */
674 /* nmsg was embedded in the mbuf, do not reply! */
678 arp_update_oncpu(struct mbuf *m, in_addr_t saddr, boolean_t create,
679 boolean_t generate_report, boolean_t dologging)
681 struct arphdr *ah = mtod(m, struct arphdr *);
682 struct ifnet *ifp = m->m_pkthdr.rcvif;
683 struct llinfo_arp *la;
684 struct sockaddr_dl *sdl;
688 la = arplookup(saddr, create, generate_report, FALSE);
689 if (la && (rt = la->la_rt) && (sdl = SDL(rt->rt_gateway))) {
690 struct in_addr isaddr = { saddr };
693 * Normally arps coming in on the wrong interface are ignored,
694 * but if we are bridging and the two interfaces belong to
695 * the same bridge, or one is a member of the bridge which
696 * is the other, then it isn't an error.
698 if (rt->rt_ifp != ifp) {
700 * (1) ifp and rt_ifp both members of same bridge
701 * (2) rt_ifp member of bridge ifp
702 * (3) ifp member of bridge rt_ifp
704 * Always replace rt_ifp with the bridge ifc.
708 if (ifp->if_bridge &&
709 rt->rt_ifp->if_bridge == ifp->if_bridge) {
710 nifp = ether_bridge_interface(ifp);
711 } else if (rt->rt_ifp->if_bridge &&
712 ether_bridge_interface(rt->rt_ifp) == ifp) {
714 } else if (ifp->if_bridge &&
715 ether_bridge_interface(ifp) == rt->rt_ifp) {
721 if ((log_arp_wrong_iface == 1 && nifp == NULL) ||
722 log_arp_wrong_iface == 2) {
723 hexncpy((u_char *)ar_sha(ah), ifp->if_addrlen,
724 hexstr[0], HEX_NCPYLEN(ifp->if_addrlen), ":");
727 "but got reply from %s on %s\n",
729 rt->rt_ifp->if_xname, hexstr[0],
736 * nifp is our man! Replace rt_ifp and adjust
739 ifp = rt->rt_ifp = nifp;
740 sdl->sdl_type = ifp->if_type;
741 sdl->sdl_index = ifp->if_index;
744 bcmp(ar_sha(ah), LLADDR(sdl), sdl->sdl_alen)) {
745 if (rt->rt_expire != 0) {
746 if (dologging && log_arp_movements) {
747 hexncpy((u_char *)LLADDR(sdl), ifp->if_addrlen,
748 hexstr[0], HEX_NCPYLEN(ifp->if_addrlen), ":");
749 hexncpy((u_char *)ar_sha(ah), ifp->if_addrlen,
750 hexstr[1], HEX_NCPYLEN(ifp->if_addrlen), ":");
752 "arp: %s moved from %s to %s on %s\n",
753 inet_ntoa(isaddr), hexstr[0], hexstr[1],
757 if (dologging && log_arp_permanent_modify) {
758 hexncpy((u_char *)ar_sha(ah), ifp->if_addrlen,
759 hexstr[0], HEX_NCPYLEN(ifp->if_addrlen), ":");
761 "arp: %s attempts to modify "
762 "permanent entry for %s on %s\n",
763 hexstr[0], inet_ntoa(isaddr), ifp->if_xname);
769 * sanity check for the address length.
770 * XXX this does not work for protocols with variable address
773 if (dologging && sdl->sdl_alen && sdl->sdl_alen != ah->ar_hln) {
774 hexncpy((u_char *)ar_sha(ah), ifp->if_addrlen,
775 hexstr[0], HEX_NCPYLEN(ifp->if_addrlen), ":");
777 "arp from %s: new addr len %d, was %d",
778 hexstr[0], ah->ar_hln, sdl->sdl_alen);
780 if (ifp->if_addrlen != ah->ar_hln) {
782 hexncpy((u_char *)ar_sha(ah), ifp->if_addrlen,
783 hexstr[0], HEX_NCPYLEN(ifp->if_addrlen), ":");
785 "arp from %s: addr len: new %d, i/f %d "
786 "(ignored)", hexstr[0],
787 ah->ar_hln, ifp->if_addrlen);
791 memcpy(LLADDR(sdl), ar_sha(ah), sdl->sdl_alen = ah->ar_hln);
792 if (rt->rt_expire != 0) {
793 rt->rt_expire = time_second + arpt_keep;
795 rt->rt_flags &= ~RTF_REJECT;
797 la->la_preempt = arp_maxtries;
800 * This particular cpu might have been holding an mbuf
801 * pending ARP resolution. If so, transmit the mbuf now.
803 if (la->la_hold != NULL) {
804 struct mbuf *m = la->la_hold;
805 struct lwkt_port *port = la->la_msgport;
806 struct netmsg_packet *pmsg;
809 la->la_msgport = NULL;
811 m_adj(m, sizeof(struct ether_header));
814 * Make sure that this rtentry will not be freed
815 * before the packet is processed on the target
816 * msgport. The reference count will be dropped
817 * in the handler associated with this packet.
821 pmsg = &m->m_hdr.mh_netmsg;
822 netmsg_init(&pmsg->base, NULL,
823 &netisr_apanic_rport,
824 MSGF_PRIORITY, arp_hold_output);
827 /* Record necessary information */
828 m->m_pkthdr.rcvif = ifp;
829 pmsg->base.lmsg.u.ms_resultp = rt;
831 lwkt_sendmsg(port, &pmsg->base.lmsg);
837 * Called from arpintr() - this routine is run from a single cpu.
840 in_arpinput(struct mbuf *m)
843 struct ifnet *ifp = m->m_pkthdr.rcvif;
844 struct ifaddr_container *ifac;
845 struct in_ifaddr_container *iac;
846 struct in_ifaddr *ia = NULL;
847 struct in_addr isaddr, itaddr, myaddr;
848 struct netmsg_inarp *msg;
849 uint8_t *enaddr = NULL;
853 req_len = arphdr_len2(ifp->if_addrlen, sizeof(struct in_addr));
854 if (m->m_len < req_len && (m = m_pullup(m, req_len)) == NULL) {
855 log(LOG_ERR, "in_arp: runt packet -- m_pullup failed\n");
859 ah = mtod(m, struct arphdr *);
860 memcpy(&isaddr, ar_spa(ah), sizeof isaddr);
861 memcpy(&itaddr, ar_tpa(ah), sizeof itaddr);
864 * Check both target and sender IP addresses:
866 * If we receive the packet on the interface owning the address,
867 * then accept the address.
869 * For a bridge, we accept the address if the receive interface and
870 * the interface owning the address are on the same bridge, and
871 * use the bridge MAC as the is-at response. The bridge will be
872 * responsible for handling the packet.
874 * (0) Check target IP against CARP IPs
877 LIST_FOREACH(iac, INADDR_HASH(itaddr.s_addr), ia_hash) {
878 int is_match = 0, is_parent = 0;
882 /* Skip all ia's which don't match */
883 if (itaddr.s_addr != ia->ia_addr.sin_addr.s_addr)
886 if (ia->ia_ifp->if_type != IFT_CARP)
889 if (carp_parent(ia->ia_ifp) == ifp)
891 if (is_parent || ia->ia_ifp == ifp)
892 is_match = carp_iamatch(ia);
897 * The parent interface will also receive
898 * the ethernet broadcast packets, e.g. ARP
899 * REQUEST, so if we could find a CARP
900 * interface of the parent that could match
901 * the target IP address, we then drop the
902 * packets, which is delieverd to us through
903 * the parent interface.
914 * (1) Check target IP against our local IPs
916 LIST_FOREACH(iac, INADDR_HASH(itaddr.s_addr), ia_hash) {
919 /* Skip all ia's which don't match */
920 if (itaddr.s_addr != ia->ia_addr.sin_addr.s_addr)
924 /* CARP interfaces are checked in (0) */
925 if (ia->ia_ifp->if_type == IFT_CARP)
929 if (ifp->if_bridge && ia->ia_ifp &&
930 ifp->if_bridge == ia->ia_ifp->if_bridge) {
931 ifp = ether_bridge_interface(ifp);
934 if (ia->ia_ifp && ia->ia_ifp->if_bridge &&
935 ether_bridge_interface(ia->ia_ifp) == ifp) {
938 if (ifp->if_bridge && ether_bridge_interface(ifp) ==
942 if (ia->ia_ifp == ifp) {
948 * (2) Check sender IP against our local IPs
950 LIST_FOREACH(iac, INADDR_HASH(isaddr.s_addr), ia_hash) {
953 /* Skip all ia's which don't match */
954 if (isaddr.s_addr != ia->ia_addr.sin_addr.s_addr)
957 if (ifp->if_bridge && ia->ia_ifp &&
958 ifp->if_bridge == ia->ia_ifp->if_bridge) {
959 ifp = ether_bridge_interface(ifp);
962 if (ia->ia_ifp && ia->ia_ifp->if_bridge &&
963 ether_bridge_interface(ia->ia_ifp) == ifp) {
966 if (ifp->if_bridge && ether_bridge_interface(ifp) ==
971 if (ia->ia_ifp == ifp)
976 * No match, use the first inet address on the receive interface
977 * as a dummy address for the rest of the function.
979 TAILQ_FOREACH(ifac, &ifp->if_addrheads[mycpuid], ifa_link) {
980 struct ifaddr *ifa = ifac->ifa;
982 if (ifa->ifa_addr && ifa->ifa_addr->sa_family == AF_INET) {
989 * If we got here, we didn't find any suitable interface,
990 * so drop the packet.
997 enaddr = (uint8_t *)IF_LLADDR(ifp);
998 myaddr = ia->ia_addr.sin_addr;
999 if (!bcmp(ar_sha(ah), enaddr, ifp->if_addrlen)) {
1000 m_freem(m); /* it's from me, ignore it. */
1003 if (!bcmp(ar_sha(ah), ifp->if_broadcastaddr, ifp->if_addrlen)) {
1005 "arp: link address is broadcast for IP address %s!\n",
1010 if (isaddr.s_addr == myaddr.s_addr && myaddr.s_addr != 0) {
1011 hexncpy((u_char *)ar_sha(ah), ifp->if_addrlen,
1012 hexstr, HEX_NCPYLEN(ifp->if_addrlen), ":");
1014 "arp: %s is using my IP address %s!\n",
1015 hexstr, inet_ntoa(isaddr));
1019 if (ifp->if_flags & IFF_STATICARP)
1023 * When arp_restricted_match is true and the ARP response is not
1024 * specifically targetted to me, ignore it. Otherwise the entry
1025 * timeout may be updated for an old MAC.
1027 if (arp_restricted_match && itaddr.s_addr != myaddr.s_addr) {
1033 * Update all CPU's routing tables with this ARP packet
1035 msg = &m->m_hdr.mh_arpmsg;
1036 netmsg_init(&msg->base, NULL, &netisr_apanic_rport,
1037 0, arp_update_msghandler);
1039 msg->saddr = isaddr.s_addr;
1040 msg->taddr = itaddr.s_addr;
1041 msg->myaddr = myaddr.s_addr;
1042 lwkt_sendmsg(rtable_portfn(0), &msg->base.lmsg);
1045 * Just return here; after all CPUs's routing tables are
1046 * properly updated by this ARP packet, an ARP reply will
1047 * be generated if appropriate.
1051 in_arpreply(m, itaddr.s_addr, myaddr.s_addr);
1055 arp_reply_msghandler(netmsg_t msg)
1057 struct netmsg_inarp *rmsg = (struct netmsg_inarp *)msg;
1059 in_arpreply(rmsg->m, rmsg->taddr, rmsg->myaddr);
1060 /* Don't reply this netmsg; netmsg_inarp is embedded in mbuf */
1064 arp_update_msghandler(netmsg_t msg)
1066 struct netmsg_inarp *rmsg = (struct netmsg_inarp *)msg;
1070 * This message handler will be called on all of the CPUs,
1071 * however, we only need to generate rtmsg on CPU0.
1073 arp_update_oncpu(rmsg->m, rmsg->saddr, rmsg->taddr == rmsg->myaddr,
1074 mycpuid == 0 ? RTL_REPORTMSG : RTL_DONTREPORT, mycpuid == 0);
1076 nextcpu = mycpuid + 1;
1077 if (nextcpu < ncpus) {
1078 lwkt_forwardmsg(rtable_portfn(nextcpu), &rmsg->base.lmsg);
1080 struct mbuf *m = rmsg->m;
1081 in_addr_t saddr = rmsg->saddr;
1082 in_addr_t taddr = rmsg->taddr;
1083 in_addr_t myaddr = rmsg->myaddr;
1086 * Dispatch this mbuf to netisr0 to perform ARP reply,
1088 * NOTE: netmsg_inarp is embedded in this mbuf.
1090 netmsg_init(&rmsg->base, NULL, &netisr_apanic_rport,
1091 0, arp_reply_msghandler);
1093 rmsg->saddr = saddr;
1094 rmsg->taddr = taddr;
1095 rmsg->myaddr = myaddr;
1096 lwkt_sendmsg(netisr_cpuport(0), &rmsg->base.lmsg);
1101 in_arpreply(struct mbuf *m, in_addr_t taddr, in_addr_t myaddr)
1103 struct ifnet *ifp = m->m_pkthdr.rcvif;
1104 const uint8_t *enaddr;
1107 struct ether_header *eh;
1109 ah = mtod(m, struct arphdr *);
1110 if (ntohs(ah->ar_op) != ARPOP_REQUEST) {
1115 enaddr = (const uint8_t *)IF_LLADDR(ifp);
1116 if (taddr == myaddr) {
1117 /* I am the target */
1118 memcpy(ar_tha(ah), ar_sha(ah), ah->ar_hln);
1119 memcpy(ar_sha(ah), enaddr, ah->ar_hln);
1121 struct llinfo_arp *la;
1124 la = arplookup(taddr, FALSE, RTL_DONTREPORT, SIN_PROXY);
1126 struct sockaddr_in sin;
1128 if (!arp_proxyall) {
1133 bzero(&sin, sizeof sin);
1134 sin.sin_family = AF_INET;
1135 sin.sin_len = sizeof sin;
1136 sin.sin_addr.s_addr = taddr;
1138 rt = rtpurelookup((struct sockaddr *)&sin);
1145 * Don't send proxies for nodes on the same interface
1146 * as this one came out of, or we'll get into a fight
1147 * over who claims what Ether address.
1149 if (rt->rt_ifp == ifp) {
1153 memcpy(ar_tha(ah), ar_sha(ah), ah->ar_hln);
1154 memcpy(ar_sha(ah), enaddr, ah->ar_hln);
1156 kprintf("arp: proxying for %s\n", inet_ntoa(itaddr));
1159 struct sockaddr_dl *sdl;
1162 memcpy(ar_tha(ah), ar_sha(ah), ah->ar_hln);
1163 sdl = SDL(rt->rt_gateway);
1164 memcpy(ar_sha(ah), LLADDR(sdl), ah->ar_hln);
1168 memcpy(ar_tpa(ah), ar_spa(ah), ah->ar_pln);
1169 memcpy(ar_spa(ah), &taddr, ah->ar_pln);
1170 ah->ar_op = htons(ARPOP_REPLY);
1171 ah->ar_pro = htons(ETHERTYPE_IP); /* let's be sure! */
1172 switch (ifp->if_type) {
1175 * May not be correct for types not explictly
1176 * listed, but it is our best guess.
1179 eh = (struct ether_header *)sa.sa_data;
1180 memcpy(eh->ether_dhost, ar_tha(ah), sizeof eh->ether_dhost);
1181 eh->ether_type = htons(ETHERTYPE_ARP);
1184 sa.sa_family = AF_UNSPEC;
1185 sa.sa_len = sizeof sa;
1186 ifp->if_output(ifp, m, &sa, NULL);
1192 * Free an arp entry. If the arp entry is actively referenced or represents
1193 * a static entry we only clear it back to an unresolved state, otherwise
1194 * we destroy the entry entirely.
1196 * Note that static entries are created when route add ... -interface is used
1197 * to create an interface route to a (direct) destination.
1200 arptfree(struct llinfo_arp *la)
1202 struct rtentry *rt = la->la_rt;
1203 struct sockaddr_dl *sdl;
1207 sdl = SDL(rt->rt_gateway);
1209 ((rt->rt_refcnt > 0 && sdl->sdl_family == AF_LINK) ||
1210 (rt->rt_flags & RTF_STATIC))) {
1212 la->la_preempt = la->la_asked = 0;
1213 rt->rt_flags &= ~RTF_REJECT;
1216 rtrequest(RTM_DELETE, rt_key(rt), NULL, rt_mask(rt), 0, NULL);
1220 * Lookup or enter a new address in arptab.
1222 static struct llinfo_arp *
1223 arplookup(in_addr_t addr, boolean_t create, boolean_t generate_report,
1227 struct sockaddr_inarp sin = { sizeof sin, AF_INET };
1228 const char *why = NULL;
1230 sin.sin_addr.s_addr = addr;
1231 sin.sin_other = proxy ? SIN_PROXY : 0;
1233 rt = _rtlookup((struct sockaddr *)&sin,
1234 generate_report, RTL_DOCLONE);
1236 rt = rtpurelookup((struct sockaddr *)&sin);
1242 if (rt->rt_flags & RTF_GATEWAY)
1243 why = "host is not on local network";
1244 else if (!(rt->rt_flags & RTF_LLINFO))
1245 why = "could not allocate llinfo";
1246 else if (rt->rt_gateway->sa_family != AF_LINK)
1247 why = "gateway route is not ours";
1251 log(LOG_DEBUG, "arplookup %s failed: %s\n",
1252 inet_ntoa(sin.sin_addr), why);
1254 if (rt->rt_refcnt <= 0 && (rt->rt_flags & RTF_WASCLONED)) {
1255 /* No references to this route. Purge it. */
1256 rtrequest(RTM_DELETE, rt_key(rt), rt->rt_gateway,
1257 rt_mask(rt), rt->rt_flags, NULL);
1261 return (rt->rt_llinfo);
1265 arp_ifinit(struct ifnet *ifp, struct ifaddr *ifa)
1267 ifa->ifa_rtrequest = arp_rtrequest;
1268 ifa->ifa_flags |= RTF_CLONING;
1272 arp_gratuitous(struct ifnet *ifp, struct ifaddr *ifa)
1274 if (IA_SIN(ifa)->sin_addr.s_addr != INADDR_ANY) {
1275 arprequest_async(ifp, &IA_SIN(ifa)->sin_addr,
1276 &IA_SIN(ifa)->sin_addr, NULL);
1281 arp_ifaddr(void *arg __unused, struct ifnet *ifp,
1282 enum ifaddr_event event, struct ifaddr *ifa)
1284 if (ifa->ifa_rtrequest != arp_rtrequest) /* XXX need a generic way */
1286 if (ifa->ifa_addr->sa_family != AF_INET)
1288 if (event == IFADDR_EVENT_DELETE)
1292 * - CARP interfaces will take care of gratuitous ARP themselves.
1293 * - If we are the CARP interface's parent, don't send gratuitous
1294 * ARP to avoid unnecessary confusion.
1297 if (ifp->if_type != IFT_CARP && ifp->if_carp == NULL)
1300 arp_gratuitous(ifp, ifa);
1309 for (cpu = 0; cpu < ncpus2; cpu++)
1310 LIST_INIT(&llinfo_arp_list[cpu]);
1312 netisr_register(NETISR_ARP, arpintr, NULL);
1314 EVENTHANDLER_REGISTER(ifaddr_event, arp_ifaddr, NULL,
1315 EVENTHANDLER_PRI_LAST);
1318 SYSINIT(arp, SI_SUB_PROTO_DOMAIN, SI_ORDER_ANY, arp_init, 0);