2 * Copyright (c) 2004, 2005 The DragonFly Project. All rights reserved.
4 * This code is derived from software contributed to The DragonFly Project
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. Neither the name of The DragonFly Project nor the names of its
16 * contributors may be used to endorse or promote products derived
17 * from this software without specific, prior written permission.
19 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
22 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
23 * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
24 * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
25 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
26 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
27 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
28 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
29 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 * Copyright (c) 1982, 1986, 1988, 1993
35 * The Regents of the University of California. All rights reserved.
37 * Redistribution and use in source and binary forms, with or without
38 * modification, are permitted provided that the following conditions
40 * 1. Redistributions of source code must retain the above copyright
41 * notice, this list of conditions and the following disclaimer.
42 * 2. Redistributions in binary form must reproduce the above copyright
43 * notice, this list of conditions and the following disclaimer in the
44 * documentation and/or other materials provided with the distribution.
45 * 3. All advertising materials mentioning features or use of this software
46 * must display the following acknowledgement:
47 * This product includes software developed by the University of
48 * California, Berkeley and its contributors.
49 * 4. Neither the name of the University nor the names of its contributors
50 * may be used to endorse or promote products derived from this software
51 * without specific prior written permission.
53 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
54 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
55 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
56 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
57 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
58 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
59 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
60 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
61 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
62 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
65 * @(#)if_ether.c 8.1 (Berkeley) 6/10/93
66 * $FreeBSD: src/sys/netinet/if_ether.c,v 1.64.2.23 2003/04/11 07:23:15 fjoe Exp $
70 * Ethernet address resolution protocol.
72 * add "inuse/lock" bit (or ref. count) along with valid bit
78 #include <sys/param.h>
79 #include <sys/kernel.h>
80 #include <sys/queue.h>
81 #include <sys/sysctl.h>
82 #include <sys/systm.h>
84 #include <sys/malloc.h>
85 #include <sys/socket.h>
86 #include <sys/syslog.h>
90 #include <net/if_dl.h>
91 #include <net/if_types.h>
92 #include <net/route.h>
93 #include <net/netisr.h>
94 #include <net/if_llc.h>
96 #include <netinet/in.h>
97 #include <netinet/in_var.h>
98 #include <netinet/if_ether.h>
100 #include <sys/thread2.h>
101 #include <sys/msgport2.h>
102 #include <net/netmsg2.h>
103 #include <sys/mplock2.h>
106 #include <netinet/ip_carp.h>
109 #define SIN(s) ((struct sockaddr_in *)s)
110 #define SDL(s) ((struct sockaddr_dl *)s)
112 SYSCTL_DECL(_net_link_ether);
113 SYSCTL_NODE(_net_link_ether, PF_INET, inet, CTLFLAG_RW, 0, "");
116 static int arpt_prune = (5*60*1); /* walk list every 5 minutes */
117 static int arpt_keep = (20*60); /* once resolved, good for 20 more minutes */
118 static int arpt_down = 20; /* once declared down, don't send for 20 sec */
120 SYSCTL_INT(_net_link_ether_inet, OID_AUTO, prune_intvl, CTLFLAG_RW,
122 SYSCTL_INT(_net_link_ether_inet, OID_AUTO, max_age, CTLFLAG_RW,
124 SYSCTL_INT(_net_link_ether_inet, OID_AUTO, host_down_time, CTLFLAG_RW,
127 #define rt_expire rt_rmx.rmx_expire
130 LIST_ENTRY(llinfo_arp) la_le;
131 struct rtentry *la_rt;
132 struct mbuf *la_hold; /* last packet until resolved/timeout */
133 struct lwkt_port *la_msgport; /* last packet's msgport */
134 u_short la_preempt; /* countdown for pre-expiry arps */
135 u_short la_asked; /* #times we QUERIED following expiration */
138 static LIST_HEAD(, llinfo_arp) llinfo_arp_list[MAXCPU];
140 static int arp_maxtries = 5;
141 static int useloopback = 1; /* use loopback interface for local traffic */
142 static int arp_proxyall = 0;
143 static int arp_refresh = 60; /* refresh arp cache ~60 (not impl yet) */
144 static int arp_restricted_match = 0;
146 SYSCTL_INT(_net_link_ether_inet, OID_AUTO, maxtries, CTLFLAG_RW,
147 &arp_maxtries, 0, "ARP resolution attempts before returning error");
148 SYSCTL_INT(_net_link_ether_inet, OID_AUTO, useloopback, CTLFLAG_RW,
149 &useloopback, 0, "Use the loopback interface for local traffic");
150 SYSCTL_INT(_net_link_ether_inet, OID_AUTO, proxyall, CTLFLAG_RW,
151 &arp_proxyall, 0, "Enable proxy ARP for all suitable requests");
152 SYSCTL_INT(_net_link_ether_inet, OID_AUTO, restricted_match, CTLFLAG_RW,
153 &arp_restricted_match, 0, "Only match against the sender");
154 SYSCTL_INT(_net_link_ether_inet, OID_AUTO, refresh, CTLFLAG_RW,
155 &arp_refresh, 0, "Preemptively refresh the ARP");
157 static void arp_rtrequest(int, struct rtentry *, struct rt_addrinfo *);
158 static void arprequest(struct ifnet *, const struct in_addr *,
159 const struct in_addr *, const u_char *);
160 static void arprequest_async(struct ifnet *, const struct in_addr *,
161 const struct in_addr *, const u_char *);
162 static void arpintr(netmsg_t msg);
163 static void arptfree(struct llinfo_arp *);
164 static void arptimer(void *);
165 static struct llinfo_arp *
166 arplookup(in_addr_t, boolean_t, boolean_t, boolean_t);
168 static void in_arpinput(struct mbuf *);
171 static struct callout arptimer_ch[MAXCPU];
174 * Timeout routine. Age arp_tab entries periodically.
178 arptimer(void *ignored_arg)
180 struct llinfo_arp *la, *nla;
183 LIST_FOREACH_MUTABLE(la, &llinfo_arp_list[mycpuid], la_le, nla) {
184 if (la->la_rt->rt_expire && la->la_rt->rt_expire <= time_second)
187 callout_reset(&arptimer_ch[mycpuid], arpt_prune * hz, arptimer, NULL);
192 * Parallel to llc_rtrequest.
194 * Called after a route is successfully added to the tree to fix-up the
195 * route and initiate arp operations if required.
198 arp_rtrequest(int req, struct rtentry *rt, struct rt_addrinfo *info)
200 struct sockaddr *gate = rt->rt_gateway;
201 struct llinfo_arp *la = rt->rt_llinfo;
203 struct sockaddr_dl null_sdl = { sizeof null_sdl, AF_LINK };
204 static boolean_t arpinit_done[MAXCPU];
206 if (!arpinit_done[mycpuid]) {
207 arpinit_done[mycpuid] = TRUE;
208 callout_init(&arptimer_ch[mycpuid]);
209 callout_reset(&arptimer_ch[mycpuid], hz, arptimer, NULL);
211 if (rt->rt_flags & RTF_GATEWAY)
217 * XXX: If this is a manually added route to interface
218 * such as older version of routed or gated might provide,
219 * restore cloning bit.
221 if (!(rt->rt_flags & RTF_HOST) &&
222 SIN(rt_mask(rt))->sin_addr.s_addr != 0xffffffff)
223 rt->rt_flags |= RTF_CLONING;
224 if (rt->rt_flags & RTF_CLONING) {
226 * Case 1: This route should come from a route to iface.
228 rt_setgate(rt, rt_key(rt),
229 (struct sockaddr *)&null_sdl,
231 gate = rt->rt_gateway;
232 SDL(gate)->sdl_type = rt->rt_ifp->if_type;
233 SDL(gate)->sdl_index = rt->rt_ifp->if_index;
234 rt->rt_expire = time_second;
237 /* Announce a new entry if requested. */
238 if (rt->rt_flags & RTF_ANNOUNCE) {
239 arprequest_async(rt->rt_ifp,
240 &SIN(rt_key(rt))->sin_addr,
241 &SIN(rt_key(rt))->sin_addr,
246 if (gate->sa_family != AF_LINK ||
247 gate->sa_len < sizeof(struct sockaddr_dl)) {
248 log(LOG_DEBUG, "arp_rtrequest: bad gateway value\n");
251 SDL(gate)->sdl_type = rt->rt_ifp->if_type;
252 SDL(gate)->sdl_index = rt->rt_ifp->if_index;
254 break; /* This happens on a route change */
256 * Case 2: This route may come from cloning, or a manual route
257 * add with a LL address.
259 R_Malloc(la, struct llinfo_arp *, sizeof *la);
262 log(LOG_DEBUG, "arp_rtrequest: malloc failed\n");
265 bzero(la, sizeof *la);
267 rt->rt_flags |= RTF_LLINFO;
268 LIST_INSERT_HEAD(&llinfo_arp_list[mycpuid], la, la_le);
272 * This keeps the multicast addresses from showing up
273 * in `arp -a' listings as unresolved. It's not actually
274 * functional. Then the same for broadcast.
276 if (IN_MULTICAST(ntohl(SIN(rt_key(rt))->sin_addr.s_addr))) {
277 ETHER_MAP_IP_MULTICAST(&SIN(rt_key(rt))->sin_addr,
279 SDL(gate)->sdl_alen = 6;
282 if (in_broadcast(SIN(rt_key(rt))->sin_addr, rt->rt_ifp)) {
283 memcpy(LLADDR(SDL(gate)), rt->rt_ifp->if_broadcastaddr,
284 rt->rt_ifp->if_addrlen);
285 SDL(gate)->sdl_alen = rt->rt_ifp->if_addrlen;
291 * This fixes up the routing interface for local addresses.
292 * The route is adjusted to point at lo0 and the expiration
295 * NOTE: This prevents locally targetted traffic from going
296 * out the hardware interface, which is inefficient
297 * and might not work if the hardware cannot listen
298 * to its own transmitted packets. Setting
299 * net.link.ether.inet.useloopback to 0 will force
300 * packets for local addresses out the hardware (and
301 * it is expected to receive its own packet).
303 * XXX We should just be able to test RTF_LOCAL here instead
304 * of having to compare IPs.
306 if (SIN(rt_key(rt))->sin_addr.s_addr ==
307 (IA_SIN(rt->rt_ifa))->sin_addr.s_addr) {
309 bcopy(IF_LLADDR(rt->rt_ifp), LLADDR(SDL(gate)),
310 SDL(gate)->sdl_alen = rt->rt_ifp->if_addrlen);
319 LIST_REMOVE(la, la_le);
320 rt->rt_llinfo = NULL;
321 rt->rt_flags &= ~RTF_LLINFO;
322 if (la->la_hold != NULL)
323 m_freem(la->la_hold);
330 arpreq_alloc(struct ifnet *ifp, const struct in_addr *sip,
331 const struct in_addr *tip, const u_char *enaddr)
337 if ((m = m_gethdr(MB_DONTWAIT, MT_DATA)) == NULL)
339 m->m_pkthdr.rcvif = NULL;
341 switch (ifp->if_type) {
344 * This may not be correct for types not explicitly
345 * listed, but this is our best guess
348 ar_hrd = htons(ARPHRD_ETHER);
350 m->m_len = arphdr_len2(ifp->if_addrlen, sizeof(struct in_addr));
351 m->m_pkthdr.len = m->m_len;
352 MH_ALIGN(m, m->m_len);
354 ah = mtod(m, struct arphdr *);
359 ah->ar_pro = htons(ETHERTYPE_IP);
360 ah->ar_hln = ifp->if_addrlen; /* hardware address length */
361 ah->ar_pln = sizeof(struct in_addr); /* protocol address length */
362 ah->ar_op = htons(ARPOP_REQUEST);
363 memcpy(ar_sha(ah), enaddr, ah->ar_hln);
364 memset(ar_tha(ah), 0, ah->ar_hln);
365 memcpy(ar_spa(ah), sip, ah->ar_pln);
366 memcpy(ar_tpa(ah), tip, ah->ar_pln);
372 arpreq_send(struct ifnet *ifp, struct mbuf *m)
375 struct ether_header *eh;
377 switch (ifp->if_type) {
380 * This may not be correct for types not explicitly
381 * listed, but this is our best guess
384 eh = (struct ether_header *)sa.sa_data;
385 /* if_output() will not swap */
386 eh->ether_type = htons(ETHERTYPE_ARP);
387 memcpy(eh->ether_dhost, ifp->if_broadcastaddr, ifp->if_addrlen);
391 sa.sa_family = AF_UNSPEC;
392 sa.sa_len = sizeof(sa);
393 ifp->if_output(ifp, m, &sa, NULL);
397 arpreq_send_handler(netmsg_t msg)
399 struct mbuf *m = msg->packet.nm_packet;
400 struct ifnet *ifp = msg->lmsg.u.ms_resultp;
403 /* nmsg was embedded in the mbuf, do not reply! */
407 * Broadcast an ARP request. Caller specifies:
408 * - arp header source ip address
409 * - arp header target ip address
410 * - arp header source ethernet address
412 * NOTE: Caller MUST NOT hold ifp's serializer
415 arprequest(struct ifnet *ifp, const struct in_addr *sip,
416 const struct in_addr *tip, const u_char *enaddr)
420 if (enaddr == NULL) {
421 if (ifp->if_bridge) {
422 enaddr = IF_LLADDR(ether_bridge_interface(ifp));
424 enaddr = IF_LLADDR(ifp);
428 m = arpreq_alloc(ifp, sip, tip, enaddr);
435 * Same as arprequest(), except:
436 * - Caller is allowed to hold ifp's serializer
437 * - Network output is done in protocol thead
440 arprequest_async(struct ifnet *ifp, const struct in_addr *sip,
441 const struct in_addr *tip, const u_char *enaddr)
444 struct netmsg_packet *pmsg;
446 if (enaddr == NULL) {
447 if (ifp->if_bridge) {
448 enaddr = IF_LLADDR(ether_bridge_interface(ifp));
450 enaddr = IF_LLADDR(ifp);
453 m = arpreq_alloc(ifp, sip, tip, enaddr);
457 pmsg = &m->m_hdr.mh_netmsg;
458 netmsg_init(&pmsg->base, NULL, &netisr_apanic_rport,
459 0, arpreq_send_handler);
461 pmsg->base.lmsg.u.ms_resultp = ifp;
463 lwkt_sendmsg(netisr_portfn(mycpuid), &pmsg->base.lmsg);
467 * Resolve an IP address into an ethernet address. If success,
468 * desten is filled in. If there is no entry in arptab,
469 * set one up and broadcast a request for the IP address.
470 * Hold onto this mbuf and resend it once the address
471 * is finally resolved. A return value of 1 indicates
472 * that desten has been filled in and the packet should be sent
473 * normally; a 0 return indicates that the packet has been
474 * taken over here, either now or for later transmission.
477 arpresolve(struct ifnet *ifp, struct rtentry *rt0, struct mbuf *m,
478 struct sockaddr *dst, u_char *desten)
481 struct llinfo_arp *la = NULL;
482 struct sockaddr_dl *sdl;
484 if (m->m_flags & M_BCAST) { /* broadcast */
485 memcpy(desten, ifp->if_broadcastaddr, ifp->if_addrlen);
488 if (m->m_flags & M_MCAST) {/* multicast */
489 ETHER_MAP_IP_MULTICAST(&SIN(dst)->sin_addr, desten);
493 if (rt_llroute(dst, rt0, &rt) != 0) {
500 la = arplookup(SIN(dst)->sin_addr.s_addr,
501 TRUE, RTL_REPORTMSG, FALSE);
505 if (la == NULL || rt == NULL) {
506 log(LOG_DEBUG, "arpresolve: can't allocate llinfo for %s%s%s\n",
507 inet_ntoa(SIN(dst)->sin_addr), la ? "la" : " ",
512 sdl = SDL(rt->rt_gateway);
514 * Check the address family and length is valid, the address
515 * is resolved; otherwise, try to resolve.
517 if ((rt->rt_expire == 0 || rt->rt_expire > time_second) &&
518 sdl->sdl_family == AF_LINK && sdl->sdl_alen != 0) {
520 * If entry has an expiry time and it is approaching,
521 * see if we need to send an ARP request within this
522 * arpt_down interval.
524 if ((rt->rt_expire != 0) &&
525 (time_second + la->la_preempt > rt->rt_expire)) {
527 &SIN(rt->rt_ifa->ifa_addr)->sin_addr,
533 bcopy(LLADDR(sdl), desten, sdl->sdl_alen);
537 * If ARP is disabled or static on this interface, stop.
539 * Probably should not allocate empty llinfo struct if we are
540 * not going to be sending out an arp request.
542 if (ifp->if_flags & (IFF_NOARP | IFF_STATICARP)) {
547 * There is an arptab entry, but no ethernet address
548 * response yet. Replace the held mbuf with this
551 if (la->la_hold != NULL)
552 m_freem(la->la_hold);
554 la->la_msgport = cur_netport();
555 if (rt->rt_expire || ((rt->rt_flags & RTF_STATIC) && !sdl->sdl_alen)) {
556 rt->rt_flags &= ~RTF_REJECT;
557 if (la->la_asked == 0 || rt->rt_expire != time_second) {
558 rt->rt_expire = time_second;
559 if (la->la_asked++ < arp_maxtries) {
561 &SIN(rt->rt_ifa->ifa_addr)->sin_addr,
565 rt->rt_flags |= RTF_REJECT;
566 rt->rt_expire += arpt_down;
568 la->la_preempt = arp_maxtries;
576 * Common length and type checks are done here,
577 * then the protocol-specific routine is called.
580 arpintr(netmsg_t msg)
582 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 hexncpy((unsigned char *)&ar->ar_hrd, 2, hexstr, 5, NULL);
597 log(LOG_ERR, "arp: unknown hardware address format (0x%s)\n",
603 if (m->m_pkthdr.len < arphdr_len(ar)) {
604 if ((m = m_pullup(m, arphdr_len(ar))) == NULL) {
605 log(LOG_ERR, "arp: runt packet\n");
608 ar = mtod(m, struct arphdr *);
611 switch (ntohs(ar->ar_pro)) {
619 /* msg was embedded in the mbuf, do not reply! */
624 * ARP for Internet protocols on 10 Mb/s Ethernet.
625 * Algorithm is that given in RFC 826.
626 * In addition, a sanity check is performed on the sender
627 * protocol address, to catch impersonators.
628 * We no longer handle negotiations for use of trailer protocol:
629 * Formerly, ARP replied for protocol type ETHERTYPE_TRAIL sent
630 * along with IP replies if we wanted trailers sent to us,
631 * and also sent them in response to IP replies.
632 * This allowed either end to announce the desire to receive
634 * We no longer reply to requests for ETHERTYPE_TRAIL protocol either,
635 * but formerly didn't normally send requests.
638 static int log_arp_wrong_iface = 1;
639 static int log_arp_movements = 1;
640 static int log_arp_permanent_modify = 1;
642 SYSCTL_INT(_net_link_ether_inet, OID_AUTO, log_arp_wrong_iface, CTLFLAG_RW,
643 &log_arp_wrong_iface, 0,
644 "Log arp packets arriving on the wrong interface");
645 SYSCTL_INT(_net_link_ether_inet, OID_AUTO, log_arp_movements, CTLFLAG_RW,
646 &log_arp_movements, 0,
647 "Log arp replies from MACs different than the one in the cache");
648 SYSCTL_INT(_net_link_ether_inet, OID_AUTO, log_arp_permanent_modify, CTLFLAG_RW,
649 &log_arp_permanent_modify, 0,
650 "Log arp replies from MACs different than the one "
651 "in the permanent arp entry");
655 arp_hold_output(netmsg_t msg)
657 struct mbuf *m = msg->packet.nm_packet;
661 rt = msg->lmsg.u.ms_resultp;
662 ifp = m->m_pkthdr.rcvif;
663 m->m_pkthdr.rcvif = NULL;
665 ifp->if_output(ifp, m, rt_key(rt), rt);
667 /* Drop the reference count bumped by the sender */
670 /* nmsg was embedded in the mbuf, do not reply! */
674 arp_update_oncpu(struct mbuf *m, in_addr_t saddr, boolean_t create,
675 boolean_t generate_report, boolean_t dologging)
677 struct arphdr *ah = mtod(m, struct arphdr *);
678 struct ifnet *ifp = m->m_pkthdr.rcvif;
679 struct llinfo_arp *la;
680 struct sockaddr_dl *sdl;
684 la = arplookup(saddr, create, generate_report, FALSE);
685 if (la && (rt = la->la_rt) && (sdl = SDL(rt->rt_gateway))) {
686 struct in_addr isaddr = { saddr };
689 * Normally arps coming in on the wrong interface are ignored,
690 * but if we are bridging and the two interfaces belong to
691 * the same bridge, or one is a member of the bridge which
692 * is the other, then it isn't an error.
694 if (rt->rt_ifp != ifp) {
696 * (1) ifp and rt_ifp both members of same bridge
697 * (2) rt_ifp member of bridge ifp
698 * (3) ifp member of bridge rt_ifp
700 * Always replace rt_ifp with the bridge ifc.
704 if (ifp->if_bridge &&
705 rt->rt_ifp->if_bridge == ifp->if_bridge) {
706 nifp = ether_bridge_interface(ifp);
707 } else if (rt->rt_ifp->if_bridge &&
708 ether_bridge_interface(rt->rt_ifp) == ifp) {
710 } else if (ifp->if_bridge &&
711 ether_bridge_interface(ifp) == rt->rt_ifp) {
717 if ((log_arp_wrong_iface == 1 && nifp == NULL) ||
718 log_arp_wrong_iface == 2) {
719 hexncpy((u_char *)ar_sha(ah), ifp->if_addrlen,
720 hexstr[0], HEX_NCPYLEN(ifp->if_addrlen), ":");
723 "but got reply from %s on %s\n",
725 rt->rt_ifp->if_xname, hexstr[0],
732 * nifp is our man! Replace rt_ifp and adjust
735 ifp = rt->rt_ifp = nifp;
736 sdl->sdl_type = ifp->if_type;
737 sdl->sdl_index = ifp->if_index;
740 bcmp(ar_sha(ah), LLADDR(sdl), sdl->sdl_alen)) {
741 if (rt->rt_expire != 0) {
742 if (dologging && log_arp_movements) {
743 hexncpy((u_char *)LLADDR(sdl), ifp->if_addrlen,
744 hexstr[0], HEX_NCPYLEN(ifp->if_addrlen), ":");
745 hexncpy((u_char *)ar_sha(ah), ifp->if_addrlen,
746 hexstr[1], HEX_NCPYLEN(ifp->if_addrlen), ":");
748 "arp: %s moved from %s to %s on %s\n",
749 inet_ntoa(isaddr), hexstr[0], hexstr[1],
753 if (dologging && log_arp_permanent_modify) {
754 hexncpy((u_char *)ar_sha(ah), ifp->if_addrlen,
755 hexstr[0], HEX_NCPYLEN(ifp->if_addrlen), ":");
757 "arp: %s attempts to modify "
758 "permanent entry for %s on %s\n",
759 hexstr[0], inet_ntoa(isaddr), ifp->if_xname);
765 * sanity check for the address length.
766 * XXX this does not work for protocols with variable address
769 if (dologging && sdl->sdl_alen && sdl->sdl_alen != ah->ar_hln) {
770 hexncpy((u_char *)ar_sha(ah), ifp->if_addrlen,
771 hexstr[0], HEX_NCPYLEN(ifp->if_addrlen), ":");
773 "arp from %s: new addr len %d, was %d",
774 hexstr[0], ah->ar_hln, sdl->sdl_alen);
776 if (ifp->if_addrlen != ah->ar_hln) {
778 hexncpy((u_char *)ar_sha(ah), ifp->if_addrlen,
779 hexstr[0], HEX_NCPYLEN(ifp->if_addrlen), ":");
781 "arp from %s: addr len: new %d, i/f %d "
782 "(ignored)", hexstr[0],
783 ah->ar_hln, ifp->if_addrlen);
787 memcpy(LLADDR(sdl), ar_sha(ah), sdl->sdl_alen = ah->ar_hln);
788 if (rt->rt_expire != 0) {
789 rt->rt_expire = time_second + arpt_keep;
791 rt->rt_flags &= ~RTF_REJECT;
793 la->la_preempt = arp_maxtries;
796 * This particular cpu might have been holding an mbuf
797 * pending ARP resolution. If so, transmit the mbuf now.
799 if (la->la_hold != NULL) {
800 struct mbuf *m = la->la_hold;
801 struct lwkt_port *port = la->la_msgport;
802 struct netmsg_packet *pmsg;
805 la->la_msgport = NULL;
807 m_adj(m, sizeof(struct ether_header));
810 * Make sure that this rtentry will not be freed
811 * before the packet is processed on the target
812 * msgport. The reference count will be dropped
813 * in the handler associated with this packet.
817 pmsg = &m->m_hdr.mh_netmsg;
818 netmsg_init(&pmsg->base, NULL,
819 &netisr_apanic_rport,
820 MSGF_PRIORITY, arp_hold_output);
823 /* Record necessary information */
824 m->m_pkthdr.rcvif = ifp;
825 pmsg->base.lmsg.u.ms_resultp = rt;
827 lwkt_sendmsg(port, &pmsg->base.lmsg);
832 struct netmsg_arp_update {
833 struct netmsg_base base;
839 static void arp_update_msghandler(netmsg_t msg);
842 * Called from arpintr() - this routine is run from a single cpu.
845 in_arpinput(struct mbuf *m)
848 struct ifnet *ifp = m->m_pkthdr.rcvif;
849 struct ether_header *eh;
851 struct ifaddr_container *ifac;
852 struct in_ifaddr_container *iac;
853 struct in_ifaddr *ia = NULL;
855 struct in_addr isaddr, itaddr, myaddr;
856 struct netmsg_arp_update msg;
857 uint8_t *enaddr = NULL;
862 req_len = arphdr_len2(ifp->if_addrlen, sizeof(struct in_addr));
863 if (m->m_len < req_len && (m = m_pullup(m, req_len)) == NULL) {
864 log(LOG_ERR, "in_arp: runt packet -- m_pullup failed\n");
868 ah = mtod(m, struct arphdr *);
869 op = ntohs(ah->ar_op);
870 memcpy(&isaddr, ar_spa(ah), sizeof isaddr);
871 memcpy(&itaddr, ar_tpa(ah), sizeof itaddr);
874 * Check both target and sender IP addresses:
876 * If we receive the packet on the interface owning the address,
877 * then accept the address.
879 * For a bridge, we accept the address if the receive interface and
880 * the interface owning the address are on the same bridge, and
881 * use the bridge MAC as the is-at response. The bridge will be
882 * responsible for handling the packet.
884 * (0) Check target IP against CARP IPs
887 LIST_FOREACH(iac, INADDR_HASH(itaddr.s_addr), ia_hash) {
888 int is_match = 0, is_parent = 0;
892 /* Skip all ia's which don't match */
893 if (itaddr.s_addr != ia->ia_addr.sin_addr.s_addr)
896 if (ia->ia_ifp->if_type != IFT_CARP)
899 if (carp_parent(ia->ia_ifp) == ifp)
901 if (is_parent || ia->ia_ifp == ifp)
902 is_match = carp_iamatch(ia);
907 * The parent interface will also receive
908 * the ethernet broadcast packets, e.g. ARP
909 * REQUEST, so if we could find a CARP
910 * interface of the parent that could match
911 * the target IP address, we then drop the
912 * packets, which is delieverd to us through
913 * the parent interface.
924 * (1) Check target IP against our local IPs
926 LIST_FOREACH(iac, INADDR_HASH(itaddr.s_addr), ia_hash) {
929 /* Skip all ia's which don't match */
930 if (itaddr.s_addr != ia->ia_addr.sin_addr.s_addr)
934 /* CARP interfaces are checked in (0) */
935 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) ==
952 if (ia->ia_ifp == ifp) {
958 * (2) Check sender IP against our local IPs
960 LIST_FOREACH(iac, INADDR_HASH(isaddr.s_addr), ia_hash) {
963 /* Skip all ia's which don't match */
964 if (isaddr.s_addr != ia->ia_addr.sin_addr.s_addr)
967 if (ifp->if_bridge && ia->ia_ifp &&
968 ifp->if_bridge == ia->ia_ifp->if_bridge) {
969 ifp = ether_bridge_interface(ifp);
972 if (ia->ia_ifp && ia->ia_ifp->if_bridge &&
973 ether_bridge_interface(ia->ia_ifp) == ifp) {
976 if (ifp->if_bridge && ether_bridge_interface(ifp) ==
981 if (ia->ia_ifp == ifp)
986 * No match, use the first inet address on the receive interface
987 * as a dummy address for the rest of the function.
989 TAILQ_FOREACH(ifac, &ifp->if_addrheads[mycpuid], ifa_link) {
990 struct ifaddr *ifa = ifac->ifa;
992 if (ifa->ifa_addr && ifa->ifa_addr->sa_family == AF_INET) {
999 * If we got here, we didn't find any suitable interface,
1000 * so drop the packet.
1007 enaddr = (uint8_t *)IF_LLADDR(ifp);
1008 myaddr = ia->ia_addr.sin_addr;
1009 if (!bcmp(ar_sha(ah), enaddr, ifp->if_addrlen)) {
1010 m_freem(m); /* it's from me, ignore it. */
1013 if (!bcmp(ar_sha(ah), ifp->if_broadcastaddr, ifp->if_addrlen)) {
1015 "arp: link address is broadcast for IP address %s!\n",
1020 if (isaddr.s_addr == myaddr.s_addr && myaddr.s_addr != 0) {
1021 hexncpy((u_char *)ar_sha(ah), ifp->if_addrlen,
1022 hexstr, HEX_NCPYLEN(ifp->if_addrlen), ":");
1024 "arp: %s is using my IP address %s!\n",
1025 hexstr, inet_ntoa(isaddr));
1029 if (ifp->if_flags & IFF_STATICARP)
1033 * When arp_restricted_match is true and the ARP response is not
1034 * specifically targetted to me, ignore it. Otherwise the entry
1035 * timeout may be updated for an old MAC.
1037 if (arp_restricted_match && itaddr.s_addr != myaddr.s_addr) {
1042 netmsg_init(&msg.base, NULL, &curthread->td_msgport,
1043 0, arp_update_msghandler);
1045 msg.saddr = isaddr.s_addr;
1046 msg.create = (itaddr.s_addr == myaddr.s_addr);
1047 lwkt_domsg(rtable_portfn(0), &msg.base.lmsg, 0);
1049 if (op != ARPOP_REQUEST) {
1053 if (itaddr.s_addr == myaddr.s_addr) {
1054 /* I am the target */
1055 memcpy(ar_tha(ah), ar_sha(ah), ah->ar_hln);
1056 memcpy(ar_sha(ah), enaddr, ah->ar_hln);
1058 struct llinfo_arp *la;
1060 la = arplookup(itaddr.s_addr, FALSE, RTL_DONTREPORT, SIN_PROXY);
1062 struct sockaddr_in sin;
1064 if (!arp_proxyall) {
1069 bzero(&sin, sizeof sin);
1070 sin.sin_family = AF_INET;
1071 sin.sin_len = sizeof sin;
1072 sin.sin_addr = itaddr;
1074 rt = rtpurelookup((struct sockaddr *)&sin);
1081 * Don't send proxies for nodes on the same interface
1082 * as this one came out of, or we'll get into a fight
1083 * over who claims what Ether address.
1085 if (rt->rt_ifp == ifp) {
1089 memcpy(ar_tha(ah), ar_sha(ah), ah->ar_hln);
1090 memcpy(ar_sha(ah), enaddr, ah->ar_hln);
1092 kprintf("arp: proxying for %s\n", inet_ntoa(itaddr));
1095 struct sockaddr_dl *sdl;
1098 memcpy(ar_tha(ah), ar_sha(ah), ah->ar_hln);
1099 sdl = SDL(rt->rt_gateway);
1100 memcpy(ar_sha(ah), LLADDR(sdl), ah->ar_hln);
1104 memcpy(ar_tpa(ah), ar_spa(ah), ah->ar_pln);
1105 memcpy(ar_spa(ah), &itaddr, ah->ar_pln);
1106 ah->ar_op = htons(ARPOP_REPLY);
1107 ah->ar_pro = htons(ETHERTYPE_IP); /* let's be sure! */
1108 switch (ifp->if_type) {
1111 * May not be correct for types not explictly
1112 * listed, but it is our best guess.
1115 eh = (struct ether_header *)sa.sa_data;
1116 memcpy(eh->ether_dhost, ar_tha(ah), sizeof eh->ether_dhost);
1117 eh->ether_type = htons(ETHERTYPE_ARP);
1120 sa.sa_family = AF_UNSPEC;
1121 sa.sa_len = sizeof sa;
1122 ifp->if_output(ifp, m, &sa, NULL);
1126 arp_update_msghandler(netmsg_t msg)
1128 struct netmsg_arp_update *rmsg = (struct netmsg_arp_update *)msg;
1132 * This message handler will be called on all of the CPUs,
1133 * however, we only need to generate rtmsg on CPU0.
1135 arp_update_oncpu(rmsg->m, rmsg->saddr, rmsg->create,
1136 mycpuid == 0 ? RTL_REPORTMSG : RTL_DONTREPORT,
1139 nextcpu = mycpuid + 1;
1140 if (nextcpu < ncpus)
1141 lwkt_forwardmsg(rtable_portfn(nextcpu), &rmsg->base.lmsg);
1143 lwkt_replymsg(&rmsg->base.lmsg, 0);
1149 * Free an arp entry. If the arp entry is actively referenced or represents
1150 * a static entry we only clear it back to an unresolved state, otherwise
1151 * we destroy the entry entirely.
1153 * Note that static entries are created when route add ... -interface is used
1154 * to create an interface route to a (direct) destination.
1157 arptfree(struct llinfo_arp *la)
1159 struct rtentry *rt = la->la_rt;
1160 struct sockaddr_dl *sdl;
1164 sdl = SDL(rt->rt_gateway);
1166 ((rt->rt_refcnt > 0 && sdl->sdl_family == AF_LINK) ||
1167 (rt->rt_flags & RTF_STATIC))) {
1169 la->la_preempt = la->la_asked = 0;
1170 rt->rt_flags &= ~RTF_REJECT;
1173 rtrequest(RTM_DELETE, rt_key(rt), NULL, rt_mask(rt), 0, NULL);
1177 * Lookup or enter a new address in arptab.
1179 static struct llinfo_arp *
1180 arplookup(in_addr_t addr, boolean_t create, boolean_t generate_report,
1184 struct sockaddr_inarp sin = { sizeof sin, AF_INET };
1185 const char *why = NULL;
1187 sin.sin_addr.s_addr = addr;
1188 sin.sin_other = proxy ? SIN_PROXY : 0;
1190 rt = _rtlookup((struct sockaddr *)&sin,
1191 generate_report, RTL_DOCLONE);
1193 rt = rtpurelookup((struct sockaddr *)&sin);
1199 if (rt->rt_flags & RTF_GATEWAY)
1200 why = "host is not on local network";
1201 else if (!(rt->rt_flags & RTF_LLINFO))
1202 why = "could not allocate llinfo";
1203 else if (rt->rt_gateway->sa_family != AF_LINK)
1204 why = "gateway route is not ours";
1208 log(LOG_DEBUG, "arplookup %s failed: %s\n",
1209 inet_ntoa(sin.sin_addr), why);
1211 if (rt->rt_refcnt <= 0 && (rt->rt_flags & RTF_WASCLONED)) {
1212 /* No references to this route. Purge it. */
1213 rtrequest(RTM_DELETE, rt_key(rt), rt->rt_gateway,
1214 rt_mask(rt), rt->rt_flags, NULL);
1218 return (rt->rt_llinfo);
1222 arp_ifinit(struct ifnet *ifp, struct ifaddr *ifa)
1224 ifa->ifa_rtrequest = arp_rtrequest;
1225 ifa->ifa_flags |= RTF_CLONING;
1229 arp_gratuitous(struct ifnet *ifp, struct ifaddr *ifa)
1231 if (IA_SIN(ifa)->sin_addr.s_addr != INADDR_ANY) {
1232 arprequest_async(ifp, &IA_SIN(ifa)->sin_addr,
1233 &IA_SIN(ifa)->sin_addr, NULL);
1238 arp_ifaddr(void *arg __unused, struct ifnet *ifp,
1239 enum ifaddr_event event, struct ifaddr *ifa)
1241 if (ifa->ifa_rtrequest != arp_rtrequest) /* XXX need a generic way */
1243 if (ifa->ifa_addr->sa_family != AF_INET)
1245 if (event == IFADDR_EVENT_DELETE)
1249 * - CARP interfaces will take care of gratuitous ARP themselves.
1250 * - If we are the CARP interface's parent, don't send gratuitous
1251 * ARP to avoid unnecessary confusion.
1254 if (ifp->if_type != IFT_CARP && ifp->if_carp == NULL)
1257 arp_gratuitous(ifp, ifa);
1266 for (cpu = 0; cpu < ncpus2; cpu++)
1267 LIST_INIT(&llinfo_arp_list[cpu]);
1269 netisr_register(NETISR_ARP, arpintr, NULL);
1271 EVENTHANDLER_REGISTER(ifaddr_event, arp_ifaddr, NULL,
1272 EVENTHANDLER_PRI_LAST);
1275 SYSINIT(arp, SI_SUB_PROTO_DOMAIN, SI_ORDER_ANY, arp_init, 0);