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.
196 arp_rtrequest(int req, struct rtentry *rt, struct rt_addrinfo *info)
198 struct sockaddr *gate = rt->rt_gateway;
199 struct llinfo_arp *la = rt->rt_llinfo;
201 struct sockaddr_dl null_sdl = { sizeof null_sdl, AF_LINK };
202 static boolean_t arpinit_done[MAXCPU];
204 if (!arpinit_done[mycpuid]) {
205 arpinit_done[mycpuid] = TRUE;
206 callout_init(&arptimer_ch[mycpuid]);
207 callout_reset(&arptimer_ch[mycpuid], hz, arptimer, NULL);
209 if (rt->rt_flags & RTF_GATEWAY)
215 * XXX: If this is a manually added route to interface
216 * such as older version of routed or gated might provide,
217 * restore cloning bit.
219 if (!(rt->rt_flags & RTF_HOST) &&
220 SIN(rt_mask(rt))->sin_addr.s_addr != 0xffffffff)
221 rt->rt_flags |= RTF_CLONING;
222 if (rt->rt_flags & RTF_CLONING) {
224 * Case 1: This route should come from a route to iface.
226 rt_setgate(rt, rt_key(rt),
227 (struct sockaddr *)&null_sdl,
229 gate = rt->rt_gateway;
230 SDL(gate)->sdl_type = rt->rt_ifp->if_type;
231 SDL(gate)->sdl_index = rt->rt_ifp->if_index;
232 rt->rt_expire = time_second;
235 /* Announce a new entry if requested. */
236 if (rt->rt_flags & RTF_ANNOUNCE) {
237 arprequest_async(rt->rt_ifp,
238 &SIN(rt_key(rt))->sin_addr,
239 &SIN(rt_key(rt))->sin_addr,
244 if (gate->sa_family != AF_LINK ||
245 gate->sa_len < sizeof(struct sockaddr_dl)) {
246 log(LOG_DEBUG, "arp_rtrequest: bad gateway value\n");
249 SDL(gate)->sdl_type = rt->rt_ifp->if_type;
250 SDL(gate)->sdl_index = rt->rt_ifp->if_index;
252 break; /* This happens on a route change */
254 * Case 2: This route may come from cloning, or a manual route
255 * add with a LL address.
257 R_Malloc(la, struct llinfo_arp *, sizeof *la);
260 log(LOG_DEBUG, "arp_rtrequest: malloc failed\n");
263 bzero(la, sizeof *la);
265 rt->rt_flags |= RTF_LLINFO;
266 LIST_INSERT_HEAD(&llinfo_arp_list[mycpuid], la, la_le);
270 * This keeps the multicast addresses from showing up
271 * in `arp -a' listings as unresolved. It's not actually
272 * functional. Then the same for broadcast.
274 if (IN_MULTICAST(ntohl(SIN(rt_key(rt))->sin_addr.s_addr))) {
275 ETHER_MAP_IP_MULTICAST(&SIN(rt_key(rt))->sin_addr,
277 SDL(gate)->sdl_alen = 6;
280 if (in_broadcast(SIN(rt_key(rt))->sin_addr, rt->rt_ifp)) {
281 memcpy(LLADDR(SDL(gate)), rt->rt_ifp->if_broadcastaddr,
282 rt->rt_ifp->if_addrlen);
283 SDL(gate)->sdl_alen = rt->rt_ifp->if_addrlen;
288 if (SIN(rt_key(rt))->sin_addr.s_addr ==
289 (IA_SIN(rt->rt_ifa))->sin_addr.s_addr) {
291 * This test used to be
292 * if (loif.if_flags & IFF_UP)
293 * It allowed local traffic to be forced
294 * through the hardware by configuring the
295 * loopback down. However, it causes problems
296 * during network configuration for boards
297 * that can't receive packets they send. It
298 * is now necessary to clear "useloopback" and
299 * remove the route to force traffic out to
303 bcopy(IF_LLADDR(rt->rt_ifp), LLADDR(SDL(gate)),
304 SDL(gate)->sdl_alen = rt->rt_ifp->if_addrlen);
313 LIST_REMOVE(la, la_le);
314 rt->rt_llinfo = NULL;
315 rt->rt_flags &= ~RTF_LLINFO;
316 if (la->la_hold != NULL)
317 m_freem(la->la_hold);
324 arpreq_alloc(struct ifnet *ifp, const struct in_addr *sip,
325 const struct in_addr *tip, const u_char *enaddr)
331 if ((m = m_gethdr(MB_DONTWAIT, MT_DATA)) == NULL)
333 m->m_pkthdr.rcvif = NULL;
335 switch (ifp->if_type) {
338 * This may not be correct for types not explicitly
339 * listed, but this is our best guess
342 ar_hrd = htons(ARPHRD_ETHER);
344 m->m_len = arphdr_len2(ifp->if_addrlen, sizeof(struct in_addr));
345 m->m_pkthdr.len = m->m_len;
346 MH_ALIGN(m, m->m_len);
348 ah = mtod(m, struct arphdr *);
353 ah->ar_pro = htons(ETHERTYPE_IP);
354 ah->ar_hln = ifp->if_addrlen; /* hardware address length */
355 ah->ar_pln = sizeof(struct in_addr); /* protocol address length */
356 ah->ar_op = htons(ARPOP_REQUEST);
357 memcpy(ar_sha(ah), enaddr, ah->ar_hln);
358 memset(ar_tha(ah), 0, ah->ar_hln);
359 memcpy(ar_spa(ah), sip, ah->ar_pln);
360 memcpy(ar_tpa(ah), tip, ah->ar_pln);
366 arpreq_send(struct ifnet *ifp, struct mbuf *m)
369 struct ether_header *eh;
371 switch (ifp->if_type) {
374 * This may not be correct for types not explicitly
375 * listed, but this is our best guess
378 eh = (struct ether_header *)sa.sa_data;
379 /* if_output() will not swap */
380 eh->ether_type = htons(ETHERTYPE_ARP);
381 memcpy(eh->ether_dhost, ifp->if_broadcastaddr, ifp->if_addrlen);
385 sa.sa_family = AF_UNSPEC;
386 sa.sa_len = sizeof(sa);
387 ifp->if_output(ifp, m, &sa, NULL);
391 arpreq_send_handler(netmsg_t msg)
393 struct mbuf *m = msg->packet.nm_packet;
394 struct ifnet *ifp = msg->lmsg.u.ms_resultp;
397 /* nmsg was embedded in the mbuf, do not reply! */
401 * Broadcast an ARP request. Caller specifies:
402 * - arp header source ip address
403 * - arp header target ip address
404 * - arp header source ethernet address
406 * NOTE: Caller MUST NOT hold ifp's serializer
409 arprequest(struct ifnet *ifp, const struct in_addr *sip,
410 const struct in_addr *tip, const u_char *enaddr)
414 if (enaddr == NULL) {
415 if (ifp->if_bridge) {
416 enaddr = IF_LLADDR(ether_bridge_interface(ifp));
418 enaddr = IF_LLADDR(ifp);
422 m = arpreq_alloc(ifp, sip, tip, enaddr);
429 * Same as arprequest(), except:
430 * - Caller is allowed to hold ifp's serializer
431 * - Network output is done in protocol thead
434 arprequest_async(struct ifnet *ifp, const struct in_addr *sip,
435 const struct in_addr *tip, const u_char *enaddr)
438 struct netmsg_packet *pmsg;
440 if (enaddr == NULL) {
441 if (ifp->if_bridge) {
442 enaddr = IF_LLADDR(ether_bridge_interface(ifp));
444 enaddr = IF_LLADDR(ifp);
447 m = arpreq_alloc(ifp, sip, tip, enaddr);
451 pmsg = &m->m_hdr.mh_netmsg;
452 netmsg_init(&pmsg->base, NULL, &netisr_apanic_rport,
453 0, arpreq_send_handler);
455 pmsg->base.lmsg.u.ms_resultp = ifp;
457 lwkt_sendmsg(cpu_portfn(mycpuid), &pmsg->base.lmsg);
461 * Resolve an IP address into an ethernet address. If success,
462 * desten is filled in. If there is no entry in arptab,
463 * set one up and broadcast a request for the IP address.
464 * Hold onto this mbuf and resend it once the address
465 * is finally resolved. A return value of 1 indicates
466 * that desten has been filled in and the packet should be sent
467 * normally; a 0 return indicates that the packet has been
468 * taken over here, either now or for later transmission.
471 arpresolve(struct ifnet *ifp, struct rtentry *rt0, struct mbuf *m,
472 struct sockaddr *dst, u_char *desten)
475 struct llinfo_arp *la = NULL;
476 struct sockaddr_dl *sdl;
478 if (m->m_flags & M_BCAST) { /* broadcast */
479 memcpy(desten, ifp->if_broadcastaddr, ifp->if_addrlen);
482 if (m->m_flags & M_MCAST) {/* multicast */
483 ETHER_MAP_IP_MULTICAST(&SIN(dst)->sin_addr, desten);
487 if (rt_llroute(dst, rt0, &rt) != 0) {
494 la = arplookup(SIN(dst)->sin_addr.s_addr,
495 TRUE, RTL_REPORTMSG, FALSE);
499 if (la == NULL || rt == NULL) {
500 log(LOG_DEBUG, "arpresolve: can't allocate llinfo for %s%s%s\n",
501 inet_ntoa(SIN(dst)->sin_addr), la ? "la" : " ",
506 sdl = SDL(rt->rt_gateway);
508 * Check the address family and length is valid, the address
509 * is resolved; otherwise, try to resolve.
511 if ((rt->rt_expire == 0 || rt->rt_expire > time_second) &&
512 sdl->sdl_family == AF_LINK && sdl->sdl_alen != 0) {
514 * If entry has an expiry time and it is approaching,
515 * see if we need to send an ARP request within this
516 * arpt_down interval.
518 if ((rt->rt_expire != 0) &&
519 (time_second + la->la_preempt > rt->rt_expire)) {
521 &SIN(rt->rt_ifa->ifa_addr)->sin_addr,
527 bcopy(LLADDR(sdl), desten, sdl->sdl_alen);
531 * If ARP is disabled or static on this interface, stop.
533 * Probably should not allocate empty llinfo struct if we are
534 * not going to be sending out an arp request.
536 if (ifp->if_flags & (IFF_NOARP | IFF_STATICARP)) {
541 * There is an arptab entry, but no ethernet address
542 * response yet. Replace the held mbuf with this
545 if (la->la_hold != NULL)
546 m_freem(la->la_hold);
548 la->la_msgport = cur_netport();
549 if (rt->rt_expire || ((rt->rt_flags & RTF_STATIC) && !sdl->sdl_alen)) {
550 rt->rt_flags &= ~RTF_REJECT;
551 if (la->la_asked == 0 || rt->rt_expire != time_second) {
552 rt->rt_expire = time_second;
553 if (la->la_asked++ < arp_maxtries) {
555 &SIN(rt->rt_ifa->ifa_addr)->sin_addr,
559 rt->rt_flags |= RTF_REJECT;
560 rt->rt_expire += arpt_down;
562 la->la_preempt = arp_maxtries;
570 * Common length and type checks are done here,
571 * then the protocol-specific routine is called.
574 arpintr(netmsg_t msg)
576 struct mbuf *m = msg->packet.nm_packet;
580 if (m->m_len < sizeof(struct arphdr) &&
581 (m = m_pullup(m, sizeof(struct arphdr))) == NULL) {
582 log(LOG_ERR, "arp: runt packet -- m_pullup failed\n");
585 ar = mtod(m, struct arphdr *);
587 ar_hrd = ntohs(ar->ar_hrd);
588 if (ar_hrd != ARPHRD_ETHER && ar_hrd != ARPHRD_IEEE802) {
589 log(LOG_ERR, "arp: unknown hardware address format (0x%2D)\n",
590 (unsigned char *)&ar->ar_hrd, "");
595 if (m->m_pkthdr.len < arphdr_len(ar)) {
596 if ((m = m_pullup(m, arphdr_len(ar))) == NULL) {
597 log(LOG_ERR, "arp: runt packet\n");
600 ar = mtod(m, struct arphdr *);
603 switch (ntohs(ar->ar_pro)) {
611 /* msg was embedded in the mbuf, do not reply! */
616 * ARP for Internet protocols on 10 Mb/s Ethernet.
617 * Algorithm is that given in RFC 826.
618 * In addition, a sanity check is performed on the sender
619 * protocol address, to catch impersonators.
620 * We no longer handle negotiations for use of trailer protocol:
621 * Formerly, ARP replied for protocol type ETHERTYPE_TRAIL sent
622 * along with IP replies if we wanted trailers sent to us,
623 * and also sent them in response to IP replies.
624 * This allowed either end to announce the desire to receive
626 * We no longer reply to requests for ETHERTYPE_TRAIL protocol either,
627 * but formerly didn't normally send requests.
630 static int log_arp_wrong_iface = 1;
631 static int log_arp_movements = 1;
632 static int log_arp_permanent_modify = 1;
634 SYSCTL_INT(_net_link_ether_inet, OID_AUTO, log_arp_wrong_iface, CTLFLAG_RW,
635 &log_arp_wrong_iface, 0,
636 "Log arp packets arriving on the wrong interface");
637 SYSCTL_INT(_net_link_ether_inet, OID_AUTO, log_arp_movements, CTLFLAG_RW,
638 &log_arp_movements, 0,
639 "Log arp replies from MACs different than the one in the cache");
640 SYSCTL_INT(_net_link_ether_inet, OID_AUTO, log_arp_permanent_modify, CTLFLAG_RW,
641 &log_arp_permanent_modify, 0,
642 "Log arp replies from MACs different than the one "
643 "in the permanent arp entry");
647 arp_hold_output(netmsg_t msg)
649 struct mbuf *m = msg->packet.nm_packet;
653 rt = msg->lmsg.u.ms_resultp;
654 ifp = m->m_pkthdr.rcvif;
655 m->m_pkthdr.rcvif = NULL;
657 ifp->if_output(ifp, m, rt_key(rt), rt);
659 /* Drop the reference count bumped by the sender */
662 /* nmsg was embedded in the mbuf, do not reply! */
666 arp_update_oncpu(struct mbuf *m, in_addr_t saddr, boolean_t create,
667 boolean_t generate_report, boolean_t dologging)
669 struct arphdr *ah = mtod(m, struct arphdr *);
670 struct ifnet *ifp = m->m_pkthdr.rcvif;
671 struct llinfo_arp *la;
672 struct sockaddr_dl *sdl;
675 la = arplookup(saddr, create, generate_report, FALSE);
676 if (la && (rt = la->la_rt) && (sdl = SDL(rt->rt_gateway))) {
677 struct in_addr isaddr = { saddr };
680 * Normally arps coming in on the wrong interface are ignored,
681 * but if we are bridging and the two interfaces belong to
682 * the same bridge, or one is a member of the bridge which
683 * is the other, then it isn't an error.
685 if (rt->rt_ifp != ifp) {
687 * (1) ifp and rt_ifp both members of same bridge
688 * (2) rt_ifp member of bridge ifp
689 * (3) ifp member of bridge rt_ifp
691 * Always replace rt_ifp with the bridge ifc.
695 if (ifp->if_bridge &&
696 rt->rt_ifp->if_bridge == ifp->if_bridge) {
697 nifp = ether_bridge_interface(ifp);
698 } else if (rt->rt_ifp->if_bridge &&
699 ether_bridge_interface(rt->rt_ifp) == ifp) {
701 } else if (ifp->if_bridge &&
702 ether_bridge_interface(ifp) == rt->rt_ifp) {
708 if ((log_arp_wrong_iface == 1 && nifp == NULL) ||
709 log_arp_wrong_iface == 2) {
712 "but got reply from %*D on %s\n",
714 rt->rt_ifp->if_xname,
715 ifp->if_addrlen, (u_char *)ar_sha(ah), ":",
722 * nifp is our man! Replace rt_ifp and adjust
725 ifp = rt->rt_ifp = nifp;
726 sdl->sdl_type = ifp->if_type;
727 sdl->sdl_index = ifp->if_index;
730 bcmp(ar_sha(ah), LLADDR(sdl), sdl->sdl_alen)) {
731 if (rt->rt_expire != 0) {
732 if (dologging && log_arp_movements) {
734 "arp: %s moved from %*D to %*D on %s\n",
736 ifp->if_addrlen, (u_char *)LLADDR(sdl),
737 ":", ifp->if_addrlen,
738 (u_char *)ar_sha(ah), ":",
742 if (dologging && log_arp_permanent_modify) {
744 "arp: %*D attempts to modify "
745 "permanent entry for %s on %s\n",
746 ifp->if_addrlen, (u_char *)ar_sha(ah),
747 ":", inet_ntoa(isaddr), ifp->if_xname);
753 * sanity check for the address length.
754 * XXX this does not work for protocols with variable address
757 if (dologging && sdl->sdl_alen && sdl->sdl_alen != ah->ar_hln) {
759 "arp from %*D: new addr len %d, was %d",
760 ifp->if_addrlen, (u_char *) ar_sha(ah), ":",
761 ah->ar_hln, sdl->sdl_alen);
763 if (ifp->if_addrlen != ah->ar_hln) {
766 "arp from %*D: addr len: new %d, i/f %d "
768 ifp->if_addrlen, (u_char *) ar_sha(ah), ":",
769 ah->ar_hln, ifp->if_addrlen);
773 memcpy(LLADDR(sdl), ar_sha(ah), sdl->sdl_alen = ah->ar_hln);
774 if (rt->rt_expire != 0) {
775 rt->rt_expire = time_second + arpt_keep;
777 rt->rt_flags &= ~RTF_REJECT;
779 la->la_preempt = arp_maxtries;
782 * This particular cpu might have been holding an mbuf
783 * pending ARP resolution. If so, transmit the mbuf now.
785 if (la->la_hold != NULL) {
786 struct mbuf *m = la->la_hold;
787 struct lwkt_port *port = la->la_msgport;
788 struct netmsg_packet *pmsg;
791 la->la_msgport = NULL;
793 m_adj(m, sizeof(struct ether_header));
796 * Make sure that this rtentry will not be freed
797 * before the packet is processed on the target
798 * msgport. The reference count will be dropped
799 * in the handler associated with this packet.
803 pmsg = &m->m_hdr.mh_netmsg;
804 netmsg_init(&pmsg->base, NULL,
805 &netisr_apanic_rport,
806 MSGF_PRIORITY, arp_hold_output);
809 /* Record necessary information */
810 m->m_pkthdr.rcvif = ifp;
811 pmsg->base.lmsg.u.ms_resultp = rt;
813 lwkt_sendmsg(port, &pmsg->base.lmsg);
820 struct netmsg_arp_update {
821 struct netmsg_base base;
827 static void arp_update_msghandler(netmsg_t msg);
832 * Called from arpintr() - this routine is run from a single cpu.
835 in_arpinput(struct mbuf *m)
838 struct ifnet *ifp = m->m_pkthdr.rcvif;
839 struct ether_header *eh;
841 struct ifaddr_container *ifac;
842 struct in_ifaddr_container *iac;
843 struct in_ifaddr *ia = NULL;
845 struct in_addr isaddr, itaddr, myaddr;
847 struct netmsg_arp_update 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 op = ntohs(ah->ar_op);
861 memcpy(&isaddr, ar_spa(ah), sizeof isaddr);
862 memcpy(&itaddr, ar_tpa(ah), sizeof itaddr);
864 myaddr.s_addr = INADDR_ANY;
866 if (ifp->if_carp != NULL) {
868 if (ifp->if_carp != NULL &&
869 carp_iamatch(ifp->if_carp, &itaddr, &isaddr, &enaddr)) {
879 * Check both target and sender IP addresses:
881 * If we receive the packet on the interface owning the address,
882 * then accept the address.
884 * For a bridge, we accept the address if the receive interface and
885 * the interface owning the address are on the same bridge, and
886 * use the bridge MAC as the is-at response. The bridge will be
887 * responsible for handling the packet.
889 * (1) Check target IP against our local IPs
891 LIST_FOREACH(iac, INADDR_HASH(itaddr.s_addr), ia_hash) {
894 /* Skip all ia's which don't match */
895 if (itaddr.s_addr != ia->ia_addr.sin_addr.s_addr)
898 if (ia->ia_ifp->if_type == IFT_CARP)
901 if (ifp->if_bridge && ia->ia_ifp &&
902 ifp->if_bridge == ia->ia_ifp->if_bridge) {
903 ifp = ether_bridge_interface(ifp);
906 if (ia->ia_ifp && ia->ia_ifp->if_bridge &&
907 ether_bridge_interface(ia->ia_ifp) == ifp) {
910 if (ifp->if_bridge && ether_bridge_interface(ifp) ==
914 if (ia->ia_ifp == ifp)
920 * (2) Check sender IP against our local IPs
922 LIST_FOREACH(iac, INADDR_HASH(isaddr.s_addr), ia_hash) {
925 /* Skip all ia's which don't match */
926 if (isaddr.s_addr != ia->ia_addr.sin_addr.s_addr)
929 if (ia->ia_ifp->if_type == IFT_CARP)
932 if (ifp->if_bridge && ia->ia_ifp &&
933 ifp->if_bridge == ia->ia_ifp->if_bridge) {
934 ifp = ether_bridge_interface(ifp);
937 if (ia->ia_ifp && ia->ia_ifp->if_bridge &&
938 ether_bridge_interface(ia->ia_ifp) == ifp) {
941 if (ifp->if_bridge && ether_bridge_interface(ifp) ==
946 if (ia->ia_ifp == ifp)
951 * No match, use the first inet address on the receive interface
952 * as a dummy address for the rest of the function.
954 TAILQ_FOREACH(ifac, &ifp->if_addrheads[mycpuid], ifa_link) {
955 struct ifaddr *ifa = ifac->ifa;
957 if (ifa->ifa_addr && ifa->ifa_addr->sa_family == AF_INET) {
964 * If we got here, we didn't find any suitable interface,
965 * so drop the packet.
972 enaddr = (uint8_t *)IF_LLADDR(ifp);
973 if (myaddr.s_addr == INADDR_ANY)
974 myaddr = ia->ia_addr.sin_addr;
975 if (!bcmp(ar_sha(ah), enaddr, ifp->if_addrlen)) {
976 m_freem(m); /* it's from me, ignore it. */
979 if (!bcmp(ar_sha(ah), ifp->if_broadcastaddr, ifp->if_addrlen)) {
981 "arp: link address is broadcast for IP address %s!\n",
986 if (isaddr.s_addr == myaddr.s_addr && myaddr.s_addr != 0) {
988 "arp: %*D is using my IP address %s!\n",
989 ifp->if_addrlen, (u_char *)ar_sha(ah), ":",
994 if (ifp->if_flags & IFF_STATICARP)
998 * When arp_restricted_match is true and the ARP response is not
999 * specifically targetted to me, ignore it. Otherwise the entry
1000 * timeout may be updated for an old MAC.
1002 if (arp_restricted_match && itaddr.s_addr != myaddr.s_addr) {
1008 netmsg_init(&msg.base, NULL, &curthread->td_msgport,
1009 0, arp_update_msghandler);
1011 msg.saddr = isaddr.s_addr;
1012 msg.create = (itaddr.s_addr == myaddr.s_addr);
1013 lwkt_domsg(rtable_portfn(0), &msg.base.lmsg, 0);
1015 arp_update_oncpu(m, isaddr.s_addr, (itaddr.s_addr == myaddr.s_addr),
1016 RTL_REPORTMSG, TRUE);
1019 if (op != ARPOP_REQUEST) {
1023 if (itaddr.s_addr == myaddr.s_addr) {
1024 /* I am the target */
1025 memcpy(ar_tha(ah), ar_sha(ah), ah->ar_hln);
1026 memcpy(ar_sha(ah), enaddr, ah->ar_hln);
1028 struct llinfo_arp *la;
1030 la = arplookup(itaddr.s_addr, FALSE, RTL_DONTREPORT, SIN_PROXY);
1032 struct sockaddr_in sin;
1034 if (!arp_proxyall) {
1039 bzero(&sin, sizeof sin);
1040 sin.sin_family = AF_INET;
1041 sin.sin_len = sizeof sin;
1042 sin.sin_addr = itaddr;
1044 rt = rtpurelookup((struct sockaddr *)&sin);
1051 * Don't send proxies for nodes on the same interface
1052 * as this one came out of, or we'll get into a fight
1053 * over who claims what Ether address.
1055 if (rt->rt_ifp == ifp) {
1059 memcpy(ar_tha(ah), ar_sha(ah), ah->ar_hln);
1060 memcpy(ar_sha(ah), enaddr, ah->ar_hln);
1062 kprintf("arp: proxying for %s\n", inet_ntoa(itaddr));
1065 struct sockaddr_dl *sdl;
1068 memcpy(ar_tha(ah), ar_sha(ah), ah->ar_hln);
1069 sdl = SDL(rt->rt_gateway);
1070 memcpy(ar_sha(ah), LLADDR(sdl), ah->ar_hln);
1074 memcpy(ar_tpa(ah), ar_spa(ah), ah->ar_pln);
1075 memcpy(ar_spa(ah), &itaddr, ah->ar_pln);
1076 ah->ar_op = htons(ARPOP_REPLY);
1077 ah->ar_pro = htons(ETHERTYPE_IP); /* let's be sure! */
1078 switch (ifp->if_type) {
1081 * May not be correct for types not explictly
1082 * listed, but it is our best guess.
1085 eh = (struct ether_header *)sa.sa_data;
1086 memcpy(eh->ether_dhost, ar_tha(ah), sizeof eh->ether_dhost);
1087 eh->ether_type = htons(ETHERTYPE_ARP);
1090 sa.sa_family = AF_UNSPEC;
1091 sa.sa_len = sizeof sa;
1092 ifp->if_output(ifp, m, &sa, NULL);
1098 arp_update_msghandler(netmsg_t msg)
1100 struct netmsg_arp_update *rmsg = (struct netmsg_arp_update *)msg;
1104 * This message handler will be called on all of the CPUs,
1105 * however, we only need to generate rtmsg on CPU0.
1107 arp_update_oncpu(rmsg->m, rmsg->saddr, rmsg->create,
1108 mycpuid == 0 ? RTL_REPORTMSG : RTL_DONTREPORT,
1111 nextcpu = mycpuid + 1;
1112 if (nextcpu < ncpus)
1113 lwkt_forwardmsg(rtable_portfn(nextcpu), &rmsg->base.lmsg);
1115 lwkt_replymsg(&rmsg->base.lmsg, 0);
1123 * Free an arp entry. If the arp entry is actively referenced or represents
1124 * a static entry we only clear it back to an unresolved state, otherwise
1125 * we destroy the entry entirely.
1127 * Note that static entries are created when route add ... -interface is used
1128 * to create an interface route to a (direct) destination.
1131 arptfree(struct llinfo_arp *la)
1133 struct rtentry *rt = la->la_rt;
1134 struct sockaddr_dl *sdl;
1138 sdl = SDL(rt->rt_gateway);
1140 ((rt->rt_refcnt > 0 && sdl->sdl_family == AF_LINK) ||
1141 (rt->rt_flags & RTF_STATIC))) {
1143 la->la_preempt = la->la_asked = 0;
1144 rt->rt_flags &= ~RTF_REJECT;
1147 rtrequest(RTM_DELETE, rt_key(rt), NULL, rt_mask(rt), 0, NULL);
1151 * Lookup or enter a new address in arptab.
1153 static struct llinfo_arp *
1154 arplookup(in_addr_t addr, boolean_t create, boolean_t generate_report,
1158 struct sockaddr_inarp sin = { sizeof sin, AF_INET };
1159 const char *why = NULL;
1161 sin.sin_addr.s_addr = addr;
1162 sin.sin_other = proxy ? SIN_PROXY : 0;
1164 rt = _rtlookup((struct sockaddr *)&sin,
1165 generate_report, RTL_DOCLONE);
1167 rt = rtpurelookup((struct sockaddr *)&sin);
1173 if (rt->rt_flags & RTF_GATEWAY)
1174 why = "host is not on local network";
1175 else if (!(rt->rt_flags & RTF_LLINFO))
1176 why = "could not allocate llinfo";
1177 else if (rt->rt_gateway->sa_family != AF_LINK)
1178 why = "gateway route is not ours";
1182 log(LOG_DEBUG, "arplookup %s failed: %s\n",
1183 inet_ntoa(sin.sin_addr), why);
1185 if (rt->rt_refcnt <= 0 && (rt->rt_flags & RTF_WASCLONED)) {
1186 /* No references to this route. Purge it. */
1187 rtrequest(RTM_DELETE, rt_key(rt), rt->rt_gateway,
1188 rt_mask(rt), rt->rt_flags, NULL);
1192 return (rt->rt_llinfo);
1196 arp_ifinit(struct ifnet *ifp, struct ifaddr *ifa)
1198 if (IA_SIN(ifa)->sin_addr.s_addr != INADDR_ANY) {
1199 arprequest_async(ifp, &IA_SIN(ifa)->sin_addr,
1200 &IA_SIN(ifa)->sin_addr, NULL);
1202 ifa->ifa_rtrequest = arp_rtrequest;
1203 ifa->ifa_flags |= RTF_CLONING;
1207 arp_iainit(struct ifnet *ifp, const struct in_addr *addr, const u_char *enaddr)
1209 if (addr->s_addr != INADDR_ANY)
1210 arprequest_async(ifp, addr, addr, enaddr);
1218 for (cpu = 0; cpu < ncpus2; cpu++)
1219 LIST_INIT(&llinfo_arp_list[cpu]);
1221 netisr_register(NETISR_ARP, arpintr, NULL);
1224 SYSINIT(arp, SI_SUB_PROTO_DOMAIN, SI_ORDER_ANY, arp_init, 0);