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 $
67 * $DragonFly: src/sys/netinet/if_ether.c,v 1.59 2008/11/22 11:03:35 sephe Exp $
71 * Ethernet address resolution protocol.
73 * add "inuse/lock" bit (or ref. count) along with valid bit
79 #include <sys/param.h>
80 #include <sys/kernel.h>
81 #include <sys/queue.h>
82 #include <sys/sysctl.h>
83 #include <sys/systm.h>
85 #include <sys/malloc.h>
86 #include <sys/socket.h>
87 #include <sys/syslog.h>
91 #include <net/if_dl.h>
92 #include <net/if_types.h>
93 #include <net/route.h>
94 #include <net/netisr.h>
95 #include <net/if_llc.h>
97 #include <netinet/in.h>
98 #include <netinet/in_var.h>
99 #include <netinet/if_ether.h>
101 #include <sys/thread2.h>
102 #include <sys/msgport2.h>
103 #include <net/netmsg2.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 *);
172 static struct callout arptimer_ch[MAXCPU];
175 * Timeout routine. Age arp_tab entries periodically.
179 arptimer(void *ignored_arg)
181 struct llinfo_arp *la, *nla;
184 LIST_FOREACH_MUTABLE(la, &llinfo_arp_list[mycpuid], la_le, nla) {
185 if (la->la_rt->rt_expire && la->la_rt->rt_expire <= time_second)
188 callout_reset(&arptimer_ch[mycpuid], arpt_prune * hz, arptimer, NULL);
193 * Parallel to llc_rtrequest.
195 * Called after a route is successfully added to the tree to fix-up the
196 * route and initiate arp operations if required.
199 arp_rtrequest(int req, struct rtentry *rt, struct rt_addrinfo *info)
201 struct sockaddr *gate = rt->rt_gateway;
202 struct llinfo_arp *la = rt->rt_llinfo;
204 struct sockaddr_dl null_sdl = { sizeof null_sdl, AF_LINK };
205 static boolean_t arpinit_done[MAXCPU];
207 if (!arpinit_done[mycpuid]) {
208 arpinit_done[mycpuid] = TRUE;
209 callout_init(&arptimer_ch[mycpuid]);
210 callout_reset(&arptimer_ch[mycpuid], hz, arptimer, NULL);
212 if (rt->rt_flags & RTF_GATEWAY)
218 * XXX: If this is a manually added route to interface
219 * such as older version of routed or gated might provide,
220 * restore cloning bit.
222 if (!(rt->rt_flags & RTF_HOST) &&
223 SIN(rt_mask(rt))->sin_addr.s_addr != 0xffffffff)
224 rt->rt_flags |= RTF_CLONING;
225 if (rt->rt_flags & RTF_CLONING) {
227 * Case 1: This route should come from a route to iface.
229 rt_setgate(rt, rt_key(rt),
230 (struct sockaddr *)&null_sdl,
232 gate = rt->rt_gateway;
233 SDL(gate)->sdl_type = rt->rt_ifp->if_type;
234 SDL(gate)->sdl_index = rt->rt_ifp->if_index;
235 rt->rt_expire = time_second;
238 /* Announce a new entry if requested. */
239 if (rt->rt_flags & RTF_ANNOUNCE) {
240 arprequest_async(rt->rt_ifp,
241 &SIN(rt_key(rt))->sin_addr,
242 &SIN(rt_key(rt))->sin_addr,
247 if (gate->sa_family != AF_LINK ||
248 gate->sa_len < sizeof(struct sockaddr_dl)) {
249 log(LOG_DEBUG, "arp_rtrequest: bad gateway value\n");
252 SDL(gate)->sdl_type = rt->rt_ifp->if_type;
253 SDL(gate)->sdl_index = rt->rt_ifp->if_index;
255 break; /* This happens on a route change */
257 * Case 2: This route may come from cloning, or a manual route
258 * add with a LL address.
260 R_Malloc(la, struct llinfo_arp *, sizeof *la);
263 log(LOG_DEBUG, "arp_rtrequest: malloc failed\n");
266 bzero(la, sizeof *la);
268 rt->rt_flags |= RTF_LLINFO;
269 LIST_INSERT_HEAD(&llinfo_arp_list[mycpuid], la, la_le);
273 * This keeps the multicast addresses from showing up
274 * in `arp -a' listings as unresolved. It's not actually
275 * functional. Then the same for broadcast.
277 if (IN_MULTICAST(ntohl(SIN(rt_key(rt))->sin_addr.s_addr))) {
278 ETHER_MAP_IP_MULTICAST(&SIN(rt_key(rt))->sin_addr,
280 SDL(gate)->sdl_alen = 6;
283 if (in_broadcast(SIN(rt_key(rt))->sin_addr, rt->rt_ifp)) {
284 memcpy(LLADDR(SDL(gate)), rt->rt_ifp->if_broadcastaddr,
285 rt->rt_ifp->if_addrlen);
286 SDL(gate)->sdl_alen = rt->rt_ifp->if_addrlen;
292 * This fixes up the routing interface for local addresses.
293 * The route is adjusted to point at lo0 and the expiration
296 * NOTE: This prevents locally targetted traffic from going
297 * out the hardware interface, which is inefficient
298 * and might not work if the hardware cannot listen
299 * to its own transmitted packets. Setting
300 * net.link.ether.inet.useloopback to 0 will force
301 * packets for local addresses out the hardware (and
302 * it is expected to receive its own packet).
304 * XXX We should just be able to test RTF_LOCAL here instead
305 * of having to compare IPs.
307 if (SIN(rt_key(rt))->sin_addr.s_addr ==
308 (IA_SIN(rt->rt_ifa))->sin_addr.s_addr) {
310 bcopy(IF_LLADDR(rt->rt_ifp), LLADDR(SDL(gate)),
311 SDL(gate)->sdl_alen = rt->rt_ifp->if_addrlen);
320 LIST_REMOVE(la, la_le);
321 rt->rt_llinfo = NULL;
322 rt->rt_flags &= ~RTF_LLINFO;
323 if (la->la_hold != NULL)
324 m_freem(la->la_hold);
331 arpreq_alloc(struct ifnet *ifp, const struct in_addr *sip,
332 const struct in_addr *tip, const u_char *enaddr)
338 if ((m = m_gethdr(MB_DONTWAIT, MT_DATA)) == NULL)
340 m->m_pkthdr.rcvif = NULL;
342 switch (ifp->if_type) {
345 * This may not be correct for types not explicitly
346 * listed, but this is our best guess
349 ar_hrd = htons(ARPHRD_ETHER);
351 m->m_len = arphdr_len2(ifp->if_addrlen, sizeof(struct in_addr));
352 m->m_pkthdr.len = m->m_len;
353 MH_ALIGN(m, m->m_len);
355 ah = mtod(m, struct arphdr *);
360 ah->ar_pro = htons(ETHERTYPE_IP);
361 ah->ar_hln = ifp->if_addrlen; /* hardware address length */
362 ah->ar_pln = sizeof(struct in_addr); /* protocol address length */
363 ah->ar_op = htons(ARPOP_REQUEST);
364 memcpy(ar_sha(ah), enaddr, ah->ar_hln);
365 memset(ar_tha(ah), 0, ah->ar_hln);
366 memcpy(ar_spa(ah), sip, ah->ar_pln);
367 memcpy(ar_tpa(ah), tip, ah->ar_pln);
373 arpreq_send(struct ifnet *ifp, struct mbuf *m)
376 struct ether_header *eh;
378 switch (ifp->if_type) {
381 * This may not be correct for types not explicitly
382 * listed, but this is our best guess
385 eh = (struct ether_header *)sa.sa_data;
386 /* if_output() will not swap */
387 eh->ether_type = htons(ETHERTYPE_ARP);
388 memcpy(eh->ether_dhost, ifp->if_broadcastaddr, ifp->if_addrlen);
392 sa.sa_family = AF_UNSPEC;
393 sa.sa_len = sizeof(sa);
394 ifp->if_output(ifp, m, &sa, NULL);
398 arpreq_send_handler(netmsg_t msg)
400 struct mbuf *m = msg->packet.nm_packet;
401 struct ifnet *ifp = msg->lmsg.u.ms_resultp;
404 /* nmsg was embedded in the mbuf, do not reply! */
408 * Broadcast an ARP request. Caller specifies:
409 * - arp header source ip address
410 * - arp header target ip address
411 * - arp header source ethernet address
413 * NOTE: Caller MUST NOT hold ifp's serializer
416 arprequest(struct ifnet *ifp, const struct in_addr *sip,
417 const struct in_addr *tip, const u_char *enaddr)
421 if (enaddr == NULL) {
422 if (ifp->if_bridge) {
423 enaddr = IF_LLADDR(ether_bridge_interface(ifp));
425 enaddr = IF_LLADDR(ifp);
429 m = arpreq_alloc(ifp, sip, tip, enaddr);
436 * Same as arprequest(), except:
437 * - Caller is allowed to hold ifp's serializer
438 * - Network output is done in protocol thead
441 arprequest_async(struct ifnet *ifp, const struct in_addr *sip,
442 const struct in_addr *tip, const u_char *enaddr)
445 struct netmsg_packet *pmsg;
447 if (enaddr == NULL) {
448 if (ifp->if_bridge) {
449 enaddr = IF_LLADDR(ether_bridge_interface(ifp));
451 enaddr = IF_LLADDR(ifp);
454 m = arpreq_alloc(ifp, sip, tip, enaddr);
458 pmsg = &m->m_hdr.mh_netmsg;
459 netmsg_init(&pmsg->base, NULL, &netisr_apanic_rport,
460 0, arpreq_send_handler);
462 pmsg->base.lmsg.u.ms_resultp = ifp;
464 lwkt_sendmsg(cpu_portfn(mycpuid), &pmsg->base.lmsg);
468 * Resolve an IP address into an ethernet address. If success,
469 * desten is filled in. If there is no entry in arptab,
470 * set one up and broadcast a request for the IP address.
471 * Hold onto this mbuf and resend it once the address
472 * is finally resolved. A return value of 1 indicates
473 * that desten has been filled in and the packet should be sent
474 * normally; a 0 return indicates that the packet has been
475 * taken over here, either now or for later transmission.
478 arpresolve(struct ifnet *ifp, struct rtentry *rt0, struct mbuf *m,
479 struct sockaddr *dst, u_char *desten)
482 struct llinfo_arp *la = NULL;
483 struct sockaddr_dl *sdl;
485 if (m->m_flags & M_BCAST) { /* broadcast */
486 memcpy(desten, ifp->if_broadcastaddr, ifp->if_addrlen);
489 if (m->m_flags & M_MCAST) {/* multicast */
490 ETHER_MAP_IP_MULTICAST(&SIN(dst)->sin_addr, desten);
494 if (rt_llroute(dst, rt0, &rt) != 0) {
501 la = arplookup(SIN(dst)->sin_addr.s_addr,
502 TRUE, RTL_REPORTMSG, FALSE);
506 if (la == NULL || rt == NULL) {
507 log(LOG_DEBUG, "arpresolve: can't allocate llinfo for %s%s%s\n",
508 inet_ntoa(SIN(dst)->sin_addr), la ? "la" : " ",
513 sdl = SDL(rt->rt_gateway);
515 * Check the address family and length is valid, the address
516 * is resolved; otherwise, try to resolve.
518 if ((rt->rt_expire == 0 || rt->rt_expire > time_second) &&
519 sdl->sdl_family == AF_LINK && sdl->sdl_alen != 0) {
521 * If entry has an expiry time and it is approaching,
522 * see if we need to send an ARP request within this
523 * arpt_down interval.
525 if ((rt->rt_expire != 0) &&
526 (time_second + la->la_preempt > rt->rt_expire)) {
528 &SIN(rt->rt_ifa->ifa_addr)->sin_addr,
534 bcopy(LLADDR(sdl), desten, sdl->sdl_alen);
538 * If ARP is disabled or static on this interface, stop.
540 * Probably should not allocate empty llinfo struct if we are
541 * not going to be sending out an arp request.
543 if (ifp->if_flags & (IFF_NOARP | IFF_STATICARP)) {
548 * There is an arptab entry, but no ethernet address
549 * response yet. Replace the held mbuf with this
552 if (la->la_hold != NULL)
553 m_freem(la->la_hold);
555 la->la_msgport = cur_netport();
556 if (rt->rt_expire || ((rt->rt_flags & RTF_STATIC) && !sdl->sdl_alen)) {
557 rt->rt_flags &= ~RTF_REJECT;
558 if (la->la_asked == 0 || rt->rt_expire != time_second) {
559 rt->rt_expire = time_second;
560 if (la->la_asked++ < arp_maxtries) {
562 &SIN(rt->rt_ifa->ifa_addr)->sin_addr,
566 rt->rt_flags |= RTF_REJECT;
567 rt->rt_expire += arpt_down;
569 la->la_preempt = arp_maxtries;
577 * Common length and type checks are done here,
578 * then the protocol-specific routine is called.
581 arpintr(netmsg_t msg)
583 struct mbuf *m = msg->packet.nm_packet;
587 if (m->m_len < sizeof(struct arphdr) &&
588 (m = m_pullup(m, sizeof(struct arphdr))) == NULL) {
589 log(LOG_ERR, "arp: runt packet -- m_pullup failed\n");
592 ar = mtod(m, struct arphdr *);
594 ar_hrd = ntohs(ar->ar_hrd);
595 if (ar_hrd != ARPHRD_ETHER && ar_hrd != ARPHRD_IEEE802) {
596 log(LOG_ERR, "arp: unknown hardware address format (0x%2D)\n",
597 (unsigned char *)&ar->ar_hrd, "");
602 if (m->m_pkthdr.len < arphdr_len(ar)) {
603 if ((m = m_pullup(m, arphdr_len(ar))) == NULL) {
604 log(LOG_ERR, "arp: runt packet\n");
607 ar = mtod(m, struct arphdr *);
610 switch (ntohs(ar->ar_pro)) {
618 /* msg was embedded in the mbuf, do not reply! */
623 * ARP for Internet protocols on 10 Mb/s Ethernet.
624 * Algorithm is that given in RFC 826.
625 * In addition, a sanity check is performed on the sender
626 * protocol address, to catch impersonators.
627 * We no longer handle negotiations for use of trailer protocol:
628 * Formerly, ARP replied for protocol type ETHERTYPE_TRAIL sent
629 * along with IP replies if we wanted trailers sent to us,
630 * and also sent them in response to IP replies.
631 * This allowed either end to announce the desire to receive
633 * We no longer reply to requests for ETHERTYPE_TRAIL protocol either,
634 * but formerly didn't normally send requests.
637 static int log_arp_wrong_iface = 1;
638 static int log_arp_movements = 1;
639 static int log_arp_permanent_modify = 1;
641 SYSCTL_INT(_net_link_ether_inet, OID_AUTO, log_arp_wrong_iface, CTLFLAG_RW,
642 &log_arp_wrong_iface, 0,
643 "Log arp packets arriving on the wrong interface");
644 SYSCTL_INT(_net_link_ether_inet, OID_AUTO, log_arp_movements, CTLFLAG_RW,
645 &log_arp_movements, 0,
646 "Log arp replies from MACs different than the one in the cache");
647 SYSCTL_INT(_net_link_ether_inet, OID_AUTO, log_arp_permanent_modify, CTLFLAG_RW,
648 &log_arp_permanent_modify, 0,
649 "Log arp replies from MACs different than the one "
650 "in the permanent arp entry");
654 arp_hold_output(netmsg_t msg)
656 struct mbuf *m = msg->packet.nm_packet;
660 rt = msg->lmsg.u.ms_resultp;
661 ifp = m->m_pkthdr.rcvif;
662 m->m_pkthdr.rcvif = NULL;
664 ifp->if_output(ifp, m, rt_key(rt), rt);
666 /* Drop the reference count bumped by the sender */
669 /* nmsg was embedded in the mbuf, do not reply! */
673 arp_update_oncpu(struct mbuf *m, in_addr_t saddr, boolean_t create,
674 boolean_t generate_report, boolean_t dologging)
676 struct arphdr *ah = mtod(m, struct arphdr *);
677 struct ifnet *ifp = m->m_pkthdr.rcvif;
678 struct llinfo_arp *la;
679 struct sockaddr_dl *sdl;
682 la = arplookup(saddr, create, generate_report, FALSE);
683 if (la && (rt = la->la_rt) && (sdl = SDL(rt->rt_gateway))) {
684 struct in_addr isaddr = { saddr };
687 * Normally arps coming in on the wrong interface are ignored,
688 * but if we are bridging and the two interfaces belong to
689 * the same bridge, or one is a member of the bridge which
690 * is the other, then it isn't an error.
692 if (rt->rt_ifp != ifp) {
694 * (1) ifp and rt_ifp both members of same bridge
695 * (2) rt_ifp member of bridge ifp
696 * (3) ifp member of bridge rt_ifp
698 * Always replace rt_ifp with the bridge ifc.
702 if (ifp->if_bridge &&
703 rt->rt_ifp->if_bridge == ifp->if_bridge) {
704 nifp = ether_bridge_interface(ifp);
705 } else if (rt->rt_ifp->if_bridge &&
706 ether_bridge_interface(rt->rt_ifp) == ifp) {
708 } else if (ifp->if_bridge &&
709 ether_bridge_interface(ifp) == rt->rt_ifp) {
715 if ((log_arp_wrong_iface == 1 && nifp == NULL) ||
716 log_arp_wrong_iface == 2) {
719 "but got reply from %*D on %s\n",
721 rt->rt_ifp->if_xname,
722 ifp->if_addrlen, (u_char *)ar_sha(ah), ":",
729 * nifp is our man! Replace rt_ifp and adjust
732 ifp = rt->rt_ifp = nifp;
733 sdl->sdl_type = ifp->if_type;
734 sdl->sdl_index = ifp->if_index;
737 bcmp(ar_sha(ah), LLADDR(sdl), sdl->sdl_alen)) {
738 if (rt->rt_expire != 0) {
739 if (dologging && log_arp_movements) {
741 "arp: %s moved from %*D to %*D on %s\n",
743 ifp->if_addrlen, (u_char *)LLADDR(sdl),
744 ":", ifp->if_addrlen,
745 (u_char *)ar_sha(ah), ":",
749 if (dologging && log_arp_permanent_modify) {
751 "arp: %*D attempts to modify "
752 "permanent entry for %s on %s\n",
753 ifp->if_addrlen, (u_char *)ar_sha(ah),
754 ":", inet_ntoa(isaddr), ifp->if_xname);
760 * sanity check for the address length.
761 * XXX this does not work for protocols with variable address
764 if (dologging && sdl->sdl_alen && sdl->sdl_alen != ah->ar_hln) {
766 "arp from %*D: new addr len %d, was %d",
767 ifp->if_addrlen, (u_char *) ar_sha(ah), ":",
768 ah->ar_hln, sdl->sdl_alen);
770 if (ifp->if_addrlen != ah->ar_hln) {
773 "arp from %*D: addr len: new %d, i/f %d "
775 ifp->if_addrlen, (u_char *) ar_sha(ah), ":",
776 ah->ar_hln, ifp->if_addrlen);
780 memcpy(LLADDR(sdl), ar_sha(ah), sdl->sdl_alen = ah->ar_hln);
781 if (rt->rt_expire != 0) {
782 rt->rt_expire = time_second + arpt_keep;
784 rt->rt_flags &= ~RTF_REJECT;
786 la->la_preempt = arp_maxtries;
789 * This particular cpu might have been holding an mbuf
790 * pending ARP resolution. If so, transmit the mbuf now.
792 if (la->la_hold != NULL) {
793 struct mbuf *m = la->la_hold;
794 struct lwkt_port *port = la->la_msgport;
795 struct netmsg_packet *pmsg;
798 la->la_msgport = NULL;
800 m_adj(m, sizeof(struct ether_header));
803 * Make sure that this rtentry will not be freed
804 * before the packet is processed on the target
805 * msgport. The reference count will be dropped
806 * in the handler associated with this packet.
810 pmsg = &m->m_hdr.mh_netmsg;
811 netmsg_init(&pmsg->base, NULL,
812 &netisr_apanic_rport,
813 MSGF_PRIORITY, arp_hold_output);
816 /* Record necessary information */
817 m->m_pkthdr.rcvif = ifp;
818 pmsg->base.lmsg.u.ms_resultp = rt;
820 lwkt_sendmsg(port, &pmsg->base.lmsg);
827 struct netmsg_arp_update {
828 struct netmsg_base base;
834 static void arp_update_msghandler(netmsg_t msg);
839 * Called from arpintr() - this routine is run from a single cpu.
842 in_arpinput(struct mbuf *m)
845 struct ifnet *ifp = m->m_pkthdr.rcvif;
846 struct ether_header *eh;
848 struct ifaddr_container *ifac;
849 struct in_ifaddr_container *iac;
850 struct in_ifaddr *ia = NULL;
852 struct in_addr isaddr, itaddr, myaddr;
854 struct netmsg_arp_update msg;
856 uint8_t *enaddr = NULL;
860 req_len = arphdr_len2(ifp->if_addrlen, sizeof(struct in_addr));
861 if (m->m_len < req_len && (m = m_pullup(m, req_len)) == NULL) {
862 log(LOG_ERR, "in_arp: runt packet -- m_pullup failed\n");
866 ah = mtod(m, struct arphdr *);
867 op = ntohs(ah->ar_op);
868 memcpy(&isaddr, ar_spa(ah), sizeof isaddr);
869 memcpy(&itaddr, ar_tpa(ah), sizeof itaddr);
871 myaddr.s_addr = INADDR_ANY;
873 if (ifp->if_carp != NULL) {
875 if (ifp->if_carp != NULL &&
876 carp_iamatch(ifp->if_carp, &itaddr, &isaddr, &enaddr)) {
886 * Check both target and sender IP addresses:
888 * If we receive the packet on the interface owning the address,
889 * then accept the address.
891 * For a bridge, we accept the address if the receive interface and
892 * the interface owning the address are on the same bridge, and
893 * use the bridge MAC as the is-at response. The bridge will be
894 * responsible for handling the packet.
896 * (1) Check target IP against our local IPs
898 LIST_FOREACH(iac, INADDR_HASH(itaddr.s_addr), ia_hash) {
901 /* Skip all ia's which don't match */
902 if (itaddr.s_addr != ia->ia_addr.sin_addr.s_addr)
905 if (ia->ia_ifp->if_type == IFT_CARP)
908 if (ifp->if_bridge && ia->ia_ifp &&
909 ifp->if_bridge == ia->ia_ifp->if_bridge) {
910 ifp = ether_bridge_interface(ifp);
913 if (ia->ia_ifp && ia->ia_ifp->if_bridge &&
914 ether_bridge_interface(ia->ia_ifp) == ifp) {
917 if (ifp->if_bridge && ether_bridge_interface(ifp) ==
921 if (ia->ia_ifp == ifp)
927 * (2) Check sender IP against our local IPs
929 LIST_FOREACH(iac, INADDR_HASH(isaddr.s_addr), ia_hash) {
932 /* Skip all ia's which don't match */
933 if (isaddr.s_addr != ia->ia_addr.sin_addr.s_addr)
936 if (ia->ia_ifp->if_type == IFT_CARP)
939 if (ifp->if_bridge && ia->ia_ifp &&
940 ifp->if_bridge == ia->ia_ifp->if_bridge) {
941 ifp = ether_bridge_interface(ifp);
944 if (ia->ia_ifp && ia->ia_ifp->if_bridge &&
945 ether_bridge_interface(ia->ia_ifp) == ifp) {
948 if (ifp->if_bridge && ether_bridge_interface(ifp) ==
953 if (ia->ia_ifp == ifp)
958 * No match, use the first inet address on the receive interface
959 * as a dummy address for the rest of the function.
961 TAILQ_FOREACH(ifac, &ifp->if_addrheads[mycpuid], ifa_link) {
962 struct ifaddr *ifa = ifac->ifa;
964 if (ifa->ifa_addr && ifa->ifa_addr->sa_family == AF_INET) {
971 * If we got here, we didn't find any suitable interface,
972 * so drop the packet.
979 enaddr = (uint8_t *)IF_LLADDR(ifp);
980 if (myaddr.s_addr == INADDR_ANY)
981 myaddr = ia->ia_addr.sin_addr;
982 if (!bcmp(ar_sha(ah), enaddr, ifp->if_addrlen)) {
983 m_freem(m); /* it's from me, ignore it. */
986 if (!bcmp(ar_sha(ah), ifp->if_broadcastaddr, ifp->if_addrlen)) {
988 "arp: link address is broadcast for IP address %s!\n",
993 if (isaddr.s_addr == myaddr.s_addr && myaddr.s_addr != 0) {
995 "arp: %*D is using my IP address %s!\n",
996 ifp->if_addrlen, (u_char *)ar_sha(ah), ":",
1001 if (ifp->if_flags & IFF_STATICARP)
1005 * When arp_restricted_match is true and the ARP response is not
1006 * specifically targetted to me, ignore it. Otherwise the entry
1007 * timeout may be updated for an old MAC.
1009 if (arp_restricted_match && itaddr.s_addr != myaddr.s_addr) {
1015 netmsg_init(&msg.base, NULL, &curthread->td_msgport,
1016 0, arp_update_msghandler);
1018 msg.saddr = isaddr.s_addr;
1019 msg.create = (itaddr.s_addr == myaddr.s_addr);
1020 lwkt_domsg(rtable_portfn(0), &msg.base.lmsg, 0);
1022 arp_update_oncpu(m, isaddr.s_addr, (itaddr.s_addr == myaddr.s_addr),
1023 RTL_REPORTMSG, TRUE);
1026 if (op != ARPOP_REQUEST) {
1030 if (itaddr.s_addr == myaddr.s_addr) {
1031 /* I am the target */
1032 memcpy(ar_tha(ah), ar_sha(ah), ah->ar_hln);
1033 memcpy(ar_sha(ah), enaddr, ah->ar_hln);
1035 struct llinfo_arp *la;
1037 la = arplookup(itaddr.s_addr, FALSE, RTL_DONTREPORT, SIN_PROXY);
1039 struct sockaddr_in sin;
1041 if (!arp_proxyall) {
1046 bzero(&sin, sizeof sin);
1047 sin.sin_family = AF_INET;
1048 sin.sin_len = sizeof sin;
1049 sin.sin_addr = itaddr;
1051 rt = rtpurelookup((struct sockaddr *)&sin);
1058 * Don't send proxies for nodes on the same interface
1059 * as this one came out of, or we'll get into a fight
1060 * over who claims what Ether address.
1062 if (rt->rt_ifp == ifp) {
1066 memcpy(ar_tha(ah), ar_sha(ah), ah->ar_hln);
1067 memcpy(ar_sha(ah), enaddr, ah->ar_hln);
1069 kprintf("arp: proxying for %s\n", inet_ntoa(itaddr));
1072 struct sockaddr_dl *sdl;
1075 memcpy(ar_tha(ah), ar_sha(ah), ah->ar_hln);
1076 sdl = SDL(rt->rt_gateway);
1077 memcpy(ar_sha(ah), LLADDR(sdl), ah->ar_hln);
1081 memcpy(ar_tpa(ah), ar_spa(ah), ah->ar_pln);
1082 memcpy(ar_spa(ah), &itaddr, ah->ar_pln);
1083 ah->ar_op = htons(ARPOP_REPLY);
1084 ah->ar_pro = htons(ETHERTYPE_IP); /* let's be sure! */
1085 switch (ifp->if_type) {
1088 * May not be correct for types not explictly
1089 * listed, but it is our best guess.
1092 eh = (struct ether_header *)sa.sa_data;
1093 memcpy(eh->ether_dhost, ar_tha(ah), sizeof eh->ether_dhost);
1094 eh->ether_type = htons(ETHERTYPE_ARP);
1097 sa.sa_family = AF_UNSPEC;
1098 sa.sa_len = sizeof sa;
1099 ifp->if_output(ifp, m, &sa, NULL);
1105 arp_update_msghandler(netmsg_t msg)
1107 struct netmsg_arp_update *rmsg = (struct netmsg_arp_update *)msg;
1111 * This message handler will be called on all of the CPUs,
1112 * however, we only need to generate rtmsg on CPU0.
1114 arp_update_oncpu(rmsg->m, rmsg->saddr, rmsg->create,
1115 mycpuid == 0 ? RTL_REPORTMSG : RTL_DONTREPORT,
1118 nextcpu = mycpuid + 1;
1119 if (nextcpu < ncpus)
1120 lwkt_forwardmsg(rtable_portfn(nextcpu), &rmsg->base.lmsg);
1122 lwkt_replymsg(&rmsg->base.lmsg, 0);
1130 * Free an arp entry. If the arp entry is actively referenced or represents
1131 * a static entry we only clear it back to an unresolved state, otherwise
1132 * we destroy the entry entirely.
1134 * Note that static entries are created when route add ... -interface is used
1135 * to create an interface route to a (direct) destination.
1138 arptfree(struct llinfo_arp *la)
1140 struct rtentry *rt = la->la_rt;
1141 struct sockaddr_dl *sdl;
1145 sdl = SDL(rt->rt_gateway);
1147 ((rt->rt_refcnt > 0 && sdl->sdl_family == AF_LINK) ||
1148 (rt->rt_flags & RTF_STATIC))) {
1150 la->la_preempt = la->la_asked = 0;
1151 rt->rt_flags &= ~RTF_REJECT;
1154 rtrequest(RTM_DELETE, rt_key(rt), NULL, rt_mask(rt), 0, NULL);
1158 * Lookup or enter a new address in arptab.
1160 static struct llinfo_arp *
1161 arplookup(in_addr_t addr, boolean_t create, boolean_t generate_report,
1165 struct sockaddr_inarp sin = { sizeof sin, AF_INET };
1166 const char *why = NULL;
1168 sin.sin_addr.s_addr = addr;
1169 sin.sin_other = proxy ? SIN_PROXY : 0;
1171 rt = _rtlookup((struct sockaddr *)&sin,
1172 generate_report, RTL_DOCLONE);
1174 rt = rtpurelookup((struct sockaddr *)&sin);
1180 if (rt->rt_flags & RTF_GATEWAY)
1181 why = "host is not on local network";
1182 else if (!(rt->rt_flags & RTF_LLINFO))
1183 why = "could not allocate llinfo";
1184 else if (rt->rt_gateway->sa_family != AF_LINK)
1185 why = "gateway route is not ours";
1189 log(LOG_DEBUG, "arplookup %s failed: %s\n",
1190 inet_ntoa(sin.sin_addr), why);
1192 if (rt->rt_refcnt <= 0 && (rt->rt_flags & RTF_WASCLONED)) {
1193 /* No references to this route. Purge it. */
1194 rtrequest(RTM_DELETE, rt_key(rt), rt->rt_gateway,
1195 rt_mask(rt), rt->rt_flags, NULL);
1199 return (rt->rt_llinfo);
1203 arp_ifinit(struct ifnet *ifp, struct ifaddr *ifa)
1205 if (IA_SIN(ifa)->sin_addr.s_addr != INADDR_ANY) {
1206 arprequest_async(ifp, &IA_SIN(ifa)->sin_addr,
1207 &IA_SIN(ifa)->sin_addr, NULL);
1209 ifa->ifa_rtrequest = arp_rtrequest;
1210 ifa->ifa_flags |= RTF_CLONING;
1214 arp_iainit(struct ifnet *ifp, const struct in_addr *addr, const u_char *enaddr)
1216 if (addr->s_addr != INADDR_ANY)
1217 arprequest_async(ifp, addr, addr, enaddr);
1225 for (cpu = 0; cpu < ncpus2; cpu++)
1226 LIST_INIT(&llinfo_arp_list[cpu]);
1228 netisr_register(NETISR_ARP, arpintr, NULL);
1231 SYSINIT(arp, SI_SUB_PROTO_DOMAIN, SI_ORDER_ANY, arp_init, 0);