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. Neither the name of the University nor the names of its contributors
46 * may be used to endorse or promote products derived from this software
47 * without specific prior written permission.
49 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
50 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
51 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
52 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
53 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
54 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
55 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
56 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
57 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
58 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
61 * @(#)if_ether.c 8.1 (Berkeley) 6/10/93
62 * $FreeBSD: src/sys/netinet/if_ether.c,v 1.64.2.23 2003/04/11 07:23:15 fjoe Exp $
66 * Ethernet address resolution protocol.
68 * add "inuse/lock" bit (or ref. count) along with valid bit
74 #include <sys/param.h>
75 #include <sys/kernel.h>
76 #include <sys/queue.h>
77 #include <sys/sysctl.h>
78 #include <sys/systm.h>
80 #include <sys/malloc.h>
81 #include <sys/socket.h>
82 #include <sys/syslog.h>
86 #include <net/if_dl.h>
87 #include <net/if_types.h>
88 #include <net/route.h>
89 #include <net/netisr.h>
90 #include <net/if_llc.h>
92 #include <netinet/in.h>
93 #include <netinet/in_var.h>
94 #include <netinet/if_ether.h>
96 #include <sys/thread2.h>
97 #include <sys/msgport2.h>
98 #include <net/netmsg2.h>
99 #include <net/netisr2.h>
100 #include <sys/mplock2.h>
103 #include <netinet/ip_carp.h>
106 #define SIN(s) ((struct sockaddr_in *)s)
107 #define SDL(s) ((struct sockaddr_dl *)s)
109 SYSCTL_DECL(_net_link_ether
);
110 SYSCTL_NODE(_net_link_ether
, PF_INET
, inet
, CTLFLAG_RW
, 0, "");
113 static int arpt_prune
= (5*60*1); /* walk list every 5 minutes */
114 static int arpt_keep
= (20*60); /* once resolved, good for 20 more minutes */
115 static int arpt_down
= 20; /* once declared down, don't send for 20 sec */
117 SYSCTL_INT(_net_link_ether_inet
, OID_AUTO
, prune_intvl
, CTLFLAG_RW
,
119 SYSCTL_INT(_net_link_ether_inet
, OID_AUTO
, max_age
, CTLFLAG_RW
,
121 SYSCTL_INT(_net_link_ether_inet
, OID_AUTO
, host_down_time
, CTLFLAG_RW
,
124 #define rt_expire rt_rmx.rmx_expire
127 LIST_ENTRY(llinfo_arp
) la_le
;
128 struct rtentry
*la_rt
;
129 struct mbuf
*la_hold
; /* last packet until resolved/timeout */
130 u_short la_preempt
; /* countdown for pre-expiry arps */
131 u_short la_asked
; /* #times we QUERIED following expiration */
134 static LIST_HEAD(, llinfo_arp
) llinfo_arp_list
[MAXCPU
];
136 static int arp_maxtries
= 5;
137 static int useloopback
= 1; /* use loopback interface for local traffic */
138 static int arp_proxyall
= 0;
139 static int arp_refresh
= 60; /* refresh arp cache ~60 (not impl yet) */
140 static int arp_restricted_match
= 0;
142 SYSCTL_INT(_net_link_ether_inet
, OID_AUTO
, maxtries
, CTLFLAG_RW
,
143 &arp_maxtries
, 0, "ARP resolution attempts before returning error");
144 SYSCTL_INT(_net_link_ether_inet
, OID_AUTO
, useloopback
, CTLFLAG_RW
,
145 &useloopback
, 0, "Use the loopback interface for local traffic");
146 SYSCTL_INT(_net_link_ether_inet
, OID_AUTO
, proxyall
, CTLFLAG_RW
,
147 &arp_proxyall
, 0, "Enable proxy ARP for all suitable requests");
148 SYSCTL_INT(_net_link_ether_inet
, OID_AUTO
, restricted_match
, CTLFLAG_RW
,
149 &arp_restricted_match
, 0, "Only match against the sender");
150 SYSCTL_INT(_net_link_ether_inet
, OID_AUTO
, refresh
, CTLFLAG_RW
,
151 &arp_refresh
, 0, "Preemptively refresh the ARP");
153 static void arp_rtrequest(int, struct rtentry
*);
154 static void arprequest(struct ifnet
*, const struct in_addr
*,
155 const struct in_addr
*, const u_char
*);
156 static void arprequest_async(struct ifnet
*, const struct in_addr
*,
157 const struct in_addr
*, const u_char
*);
158 static void arpintr(netmsg_t msg
);
159 static void arptfree(struct llinfo_arp
*);
160 static void arptimer(void *);
161 static struct llinfo_arp
*
162 arplookup(in_addr_t
, boolean_t
, boolean_t
, boolean_t
);
164 static void in_arpinput(struct mbuf
*);
165 static void in_arpreply(struct mbuf
*m
, in_addr_t
, in_addr_t
);
166 static void arp_update_msghandler(netmsg_t
);
167 static void arp_reply_msghandler(netmsg_t
);
170 struct arptimer_ctx
{
171 struct callout timer_ch
;
172 struct netmsg_base timer_nmsg
;
176 static struct arptimer_ctx arptimer_context
[MAXCPU
];
179 * Timeout routine. Age arp_tab entries periodically.
182 arptimer_dispatch(netmsg_t nmsg
)
184 struct llinfo_arp
*la
, *nla
;
189 lwkt_replymsg(&nmsg
->lmsg
, 0);
192 LIST_FOREACH_MUTABLE(la
, &llinfo_arp_list
[cpuid
], la_le
, nla
) {
193 if (la
->la_rt
->rt_expire
&& la
->la_rt
->rt_expire
<= time_uptime
)
196 callout_reset(&arptimer_context
[cpuid
].timer_ch
, arpt_prune
* hz
,
201 arptimer(void *arg __unused
)
204 struct lwkt_msg
*lmsg
= &arptimer_context
[cpuid
].timer_nmsg
.lmsg
;
207 if (lmsg
->ms_flags
& MSGF_DONE
)
208 lwkt_sendmsg_oncpu(netisr_cpuport(cpuid
), lmsg
);
213 * Parallel to llc_rtrequest.
215 * Called after a route is successfully added to the tree to fix-up the
216 * route and initiate arp operations if required.
219 arp_rtrequest(int req
, struct rtentry
*rt
)
221 struct sockaddr
*gate
= rt
->rt_gateway
;
222 struct llinfo_arp
*la
= rt
->rt_llinfo
;
224 struct sockaddr_dl null_sdl
= { sizeof null_sdl
, AF_LINK
};
226 if (__predict_false(!arptimer_context
[mycpuid
].timer_inited
)) {
227 struct arptimer_ctx
*ctx
= &arptimer_context
[mycpuid
];
229 ctx
->timer_inited
= TRUE
;
230 netmsg_init(&ctx
->timer_nmsg
, NULL
, &netisr_adone_rport
,
231 MSGF_PRIORITY
, arptimer_dispatch
);
232 callout_init_mp(&ctx
->timer_ch
);
233 callout_reset(&ctx
->timer_ch
, hz
, arptimer
, NULL
);
235 if (rt
->rt_flags
& RTF_GATEWAY
)
241 * XXX: If this is a manually added route to interface
242 * such as older version of routed or gated might provide,
243 * restore cloning bit.
245 if (!(rt
->rt_flags
& RTF_HOST
) &&
246 SIN(rt_mask(rt
))->sin_addr
.s_addr
!= 0xffffffff)
247 rt
->rt_flags
|= RTF_CLONING
;
248 if (rt
->rt_flags
& RTF_CLONING
) {
250 * Case 1: This route should come from a route to iface.
252 rt_setgate(rt
, rt_key(rt
),
253 (struct sockaddr
*)&null_sdl
,
255 gate
= rt
->rt_gateway
;
256 SDL(gate
)->sdl_type
= rt
->rt_ifp
->if_type
;
257 SDL(gate
)->sdl_index
= rt
->rt_ifp
->if_index
;
258 rt
->rt_expire
= time_uptime
;
262 * Announce a new entry if requested, and only announce it
265 if ((rt
->rt_flags
& RTF_ANNOUNCE
) && mycpuid
== 0) {
266 arprequest_async(rt
->rt_ifp
,
267 &SIN(rt_key(rt
))->sin_addr
,
268 &SIN(rt_key(rt
))->sin_addr
,
273 if (gate
->sa_family
!= AF_LINK
||
274 gate
->sa_len
< sizeof(struct sockaddr_dl
)) {
275 log(LOG_DEBUG
, "arp_rtrequest: bad gateway value\n");
278 SDL(gate
)->sdl_type
= rt
->rt_ifp
->if_type
;
279 SDL(gate
)->sdl_index
= rt
->rt_ifp
->if_index
;
281 break; /* This happens on a route change */
283 * Case 2: This route may come from cloning, or a manual route
284 * add with a LL address.
286 R_Malloc(la
, struct llinfo_arp
*, sizeof *la
);
289 log(LOG_DEBUG
, "arp_rtrequest: malloc failed\n");
292 bzero(la
, sizeof *la
);
294 rt
->rt_flags
|= RTF_LLINFO
;
295 LIST_INSERT_HEAD(&llinfo_arp_list
[mycpuid
], la
, la_le
);
299 * This keeps the multicast addresses from showing up
300 * in `arp -a' listings as unresolved. It's not actually
301 * functional. Then the same for broadcast.
303 if (IN_MULTICAST(ntohl(SIN(rt_key(rt
))->sin_addr
.s_addr
))) {
304 ETHER_MAP_IP_MULTICAST(&SIN(rt_key(rt
))->sin_addr
,
306 SDL(gate
)->sdl_alen
= 6;
309 if (in_broadcast(SIN(rt_key(rt
))->sin_addr
, rt
->rt_ifp
)) {
310 memcpy(LLADDR(SDL(gate
)), rt
->rt_ifp
->if_broadcastaddr
,
311 rt
->rt_ifp
->if_addrlen
);
312 SDL(gate
)->sdl_alen
= rt
->rt_ifp
->if_addrlen
;
318 * This fixes up the routing interface for local addresses.
319 * The route is adjusted to point at lo0 and the expiration
322 * NOTE: This prevents locally targetted traffic from going
323 * out the hardware interface, which is inefficient
324 * and might not work if the hardware cannot listen
325 * to its own transmitted packets. Setting
326 * net.link.ether.inet.useloopback to 0 will force
327 * packets for local addresses out the hardware (and
328 * it is expected to receive its own packet).
330 * XXX We should just be able to test RTF_LOCAL here instead
331 * of having to compare IPs.
333 if (SIN(rt_key(rt
))->sin_addr
.s_addr
==
334 (IA_SIN(rt
->rt_ifa
))->sin_addr
.s_addr
) {
336 bcopy(IF_LLADDR(rt
->rt_ifp
), LLADDR(SDL(gate
)),
337 SDL(gate
)->sdl_alen
= rt
->rt_ifp
->if_addrlen
);
346 LIST_REMOVE(la
, la_le
);
347 rt
->rt_llinfo
= NULL
;
348 rt
->rt_flags
&= ~RTF_LLINFO
;
349 if (la
->la_hold
!= NULL
)
350 m_freem(la
->la_hold
);
357 arpreq_alloc(struct ifnet
*ifp
, const struct in_addr
*sip
,
358 const struct in_addr
*tip
, const u_char
*enaddr
)
364 if ((m
= m_gethdr(M_NOWAIT
, MT_DATA
)) == NULL
)
366 m
->m_pkthdr
.rcvif
= NULL
;
368 switch (ifp
->if_type
) {
371 * This may not be correct for types not explicitly
372 * listed, but this is our best guess
375 ar_hrd
= htons(ARPHRD_ETHER
);
377 m
->m_len
= arphdr_len2(ifp
->if_addrlen
, sizeof(struct in_addr
));
378 m
->m_pkthdr
.len
= m
->m_len
;
379 MH_ALIGN(m
, m
->m_len
);
381 ah
= mtod(m
, struct arphdr
*);
386 ah
->ar_pro
= htons(ETHERTYPE_IP
);
387 ah
->ar_hln
= ifp
->if_addrlen
; /* hardware address length */
388 ah
->ar_pln
= sizeof(struct in_addr
); /* protocol address length */
389 ah
->ar_op
= htons(ARPOP_REQUEST
);
390 memcpy(ar_sha(ah
), enaddr
, ah
->ar_hln
);
391 memset(ar_tha(ah
), 0, ah
->ar_hln
);
392 memcpy(ar_spa(ah
), sip
, ah
->ar_pln
);
393 memcpy(ar_tpa(ah
), tip
, ah
->ar_pln
);
399 arpreq_send(struct ifnet
*ifp
, struct mbuf
*m
)
402 struct ether_header
*eh
;
404 switch (ifp
->if_type
) {
407 * This may not be correct for types not explicitly
408 * listed, but this is our best guess
411 eh
= (struct ether_header
*)sa
.sa_data
;
412 /* if_output() will not swap */
413 eh
->ether_type
= htons(ETHERTYPE_ARP
);
414 memcpy(eh
->ether_dhost
, ifp
->if_broadcastaddr
, ifp
->if_addrlen
);
418 sa
.sa_family
= AF_UNSPEC
;
419 sa
.sa_len
= sizeof(sa
);
420 ifp
->if_output(ifp
, m
, &sa
, NULL
);
424 arpreq_send_handler(netmsg_t msg
)
426 struct mbuf
*m
= msg
->packet
.nm_packet
;
427 struct ifnet
*ifp
= msg
->lmsg
.u
.ms_resultp
;
430 /* nmsg was embedded in the mbuf, do not reply! */
434 * Broadcast an ARP request. Caller specifies:
435 * - arp header source ip address
436 * - arp header target ip address
437 * - arp header source ethernet address
439 * NOTE: Caller MUST NOT hold ifp's serializer
442 arprequest(struct ifnet
*ifp
, const struct in_addr
*sip
,
443 const struct in_addr
*tip
, const u_char
*enaddr
)
447 if (enaddr
== NULL
) {
448 if (ifp
->if_bridge
) {
449 enaddr
= IF_LLADDR(ether_bridge_interface(ifp
));
451 enaddr
= IF_LLADDR(ifp
);
455 m
= arpreq_alloc(ifp
, sip
, tip
, enaddr
);
462 * Same as arprequest(), except:
463 * - Caller is allowed to hold ifp's serializer
464 * - Network output is done in protocol thead
467 arprequest_async(struct ifnet
*ifp
, const struct in_addr
*sip
,
468 const struct in_addr
*tip
, const u_char
*enaddr
)
471 struct netmsg_packet
*pmsg
;
473 if (enaddr
== NULL
) {
474 if (ifp
->if_bridge
) {
475 enaddr
= IF_LLADDR(ether_bridge_interface(ifp
));
477 enaddr
= IF_LLADDR(ifp
);
480 m
= arpreq_alloc(ifp
, sip
, tip
, enaddr
);
484 pmsg
= &m
->m_hdr
.mh_netmsg
;
485 netmsg_init(&pmsg
->base
, NULL
, &netisr_apanic_rport
,
486 0, arpreq_send_handler
);
488 pmsg
->base
.lmsg
.u
.ms_resultp
= ifp
;
490 lwkt_sendmsg_oncpu(netisr_cpuport(mycpuid
), &pmsg
->base
.lmsg
);
494 * Resolve an IP address into an ethernet address. If success,
495 * desten is filled in. If there is no entry in arptab,
496 * set one up and broadcast a request for the IP address.
497 * Hold onto this mbuf and resend it once the address
498 * is finally resolved. A return value of 1 indicates
499 * that desten has been filled in and the packet should be sent
500 * normally; a 0 return indicates that the packet has been
501 * taken over here, either now or for later transmission.
504 arpresolve(struct ifnet
*ifp
, struct rtentry
*rt0
, struct mbuf
*m
,
505 struct sockaddr
*dst
, u_char
*desten
)
507 struct rtentry
*rt
= NULL
;
508 struct llinfo_arp
*la
= NULL
;
509 struct sockaddr_dl
*sdl
;
511 if (m
->m_flags
& M_BCAST
) { /* broadcast */
512 memcpy(desten
, ifp
->if_broadcastaddr
, ifp
->if_addrlen
);
515 if (m
->m_flags
& M_MCAST
) {/* multicast */
516 ETHER_MAP_IP_MULTICAST(&SIN(dst
)->sin_addr
, desten
);
520 if (rt_llroute(dst
, rt0
, &rt
) != 0) {
527 la
= arplookup(SIN(dst
)->sin_addr
.s_addr
,
528 TRUE
, RTL_REPORTMSG
, FALSE
);
532 if (la
== NULL
|| rt
== NULL
) {
533 log(LOG_DEBUG
, "arpresolve: can't allocate llinfo for %s%s%s\n",
534 inet_ntoa(SIN(dst
)->sin_addr
), la
? "la" : " ",
539 sdl
= SDL(rt
->rt_gateway
);
541 * Check the address family and length is valid, the address
542 * is resolved; otherwise, try to resolve.
544 if ((rt
->rt_expire
== 0 || rt
->rt_expire
> time_uptime
) &&
545 sdl
->sdl_family
== AF_LINK
&& sdl
->sdl_alen
!= 0) {
547 * If entry has an expiry time and it is approaching,
548 * see if we need to send an ARP request within this
549 * arpt_down interval.
551 if ((rt
->rt_expire
!= 0) &&
552 (time_uptime
+ la
->la_preempt
> rt
->rt_expire
)) {
554 &SIN(rt
->rt_ifa
->ifa_addr
)->sin_addr
,
560 bcopy(LLADDR(sdl
), desten
, sdl
->sdl_alen
);
564 * If ARP is disabled or static on this interface, stop.
566 * Probably should not allocate empty llinfo struct if we are
567 * not going to be sending out an arp request.
569 if (ifp
->if_flags
& (IFF_NOARP
| IFF_STATICARP
)) {
574 * There is an arptab entry, but no ethernet address
575 * response yet. Replace the held mbuf with this
578 if (la
->la_hold
!= NULL
)
579 m_freem(la
->la_hold
);
581 if (rt
->rt_expire
|| ((rt
->rt_flags
& RTF_STATIC
) && !sdl
->sdl_alen
)) {
582 rt
->rt_flags
&= ~RTF_REJECT
;
583 if (la
->la_asked
== 0 || rt
->rt_expire
!= time_uptime
) {
584 rt
->rt_expire
= time_uptime
;
585 if (la
->la_asked
++ < arp_maxtries
) {
587 &SIN(rt
->rt_ifa
->ifa_addr
)->sin_addr
,
591 rt
->rt_flags
|= RTF_REJECT
;
592 rt
->rt_expire
+= arpt_down
;
594 la
->la_preempt
= arp_maxtries
;
602 * Common length and type checks are done here,
603 * then the protocol-specific routine is called.
606 arpintr(netmsg_t msg
)
608 struct mbuf
*m
= msg
->packet
.nm_packet
;
613 if (m
->m_len
< sizeof(struct arphdr
) &&
614 (m
= m_pullup(m
, sizeof(struct arphdr
))) == NULL
) {
615 log(LOG_ERR
, "arp: runt packet -- m_pullup failed\n");
618 ar
= mtod(m
, struct arphdr
*);
620 ar_hrd
= ntohs(ar
->ar_hrd
);
621 if (ar_hrd
!= ARPHRD_ETHER
&& ar_hrd
!= ARPHRD_IEEE802
) {
622 hexncpy((unsigned char *)&ar
->ar_hrd
, 2, hexstr
, 5, NULL
);
623 log(LOG_ERR
, "arp: unknown hardware address format (0x%s)\n",
629 if (m
->m_pkthdr
.len
< arphdr_len(ar
)) {
630 if ((m
= m_pullup(m
, arphdr_len(ar
))) == NULL
) {
631 log(LOG_ERR
, "arp: runt packet\n");
634 ar
= mtod(m
, struct arphdr
*);
637 switch (ntohs(ar
->ar_pro
)) {
645 /* msg was embedded in the mbuf, do not reply! */
650 * ARP for Internet protocols on 10 Mb/s Ethernet.
651 * Algorithm is that given in RFC 826.
652 * In addition, a sanity check is performed on the sender
653 * protocol address, to catch impersonators.
654 * We no longer handle negotiations for use of trailer protocol:
655 * Formerly, ARP replied for protocol type ETHERTYPE_TRAIL sent
656 * along with IP replies if we wanted trailers sent to us,
657 * and also sent them in response to IP replies.
658 * This allowed either end to announce the desire to receive
660 * We no longer reply to requests for ETHERTYPE_TRAIL protocol either,
661 * but formerly didn't normally send requests.
664 static int log_arp_wrong_iface
= 1;
665 static int log_arp_movements
= 1;
666 static int log_arp_permanent_modify
= 1;
667 static int log_arp_creation_failure
= 1;
669 SYSCTL_INT(_net_link_ether_inet
, OID_AUTO
, log_arp_wrong_iface
, CTLFLAG_RW
,
670 &log_arp_wrong_iface
, 0,
671 "Log arp packets arriving on the wrong interface");
672 SYSCTL_INT(_net_link_ether_inet
, OID_AUTO
, log_arp_movements
, CTLFLAG_RW
,
673 &log_arp_movements
, 0,
674 "Log arp replies from MACs different than the one in the cache");
675 SYSCTL_INT(_net_link_ether_inet
, OID_AUTO
, log_arp_permanent_modify
, CTLFLAG_RW
,
676 &log_arp_permanent_modify
, 0,
677 "Log arp replies from MACs different than the one "
678 "in the permanent arp entry");
679 SYSCTL_INT(_net_link_ether_inet
, OID_AUTO
, log_arp_creation_failure
, CTLFLAG_RW
,
680 &log_arp_creation_failure
, 0, "Log arp creation failure");
683 * Returns non-zero if the routine updated anything.
686 arp_update_oncpu(struct mbuf
*m
, in_addr_t saddr
, boolean_t create
,
687 boolean_t generate_report
, boolean_t dologging
)
689 struct arphdr
*ah
= mtod(m
, struct arphdr
*);
690 struct ifnet
*ifp
= m
->m_pkthdr
.rcvif
;
691 struct llinfo_arp
*la
;
692 struct sockaddr_dl
*sdl
;
695 int changed
= create
;
697 KASSERT(curthread
->td_type
== TD_TYPE_NETISR
,
698 ("arp update not in netisr"));
700 la
= arplookup(saddr
, create
, generate_report
, FALSE
);
701 if (la
&& (rt
= la
->la_rt
) && (sdl
= SDL(rt
->rt_gateway
))) {
702 struct in_addr isaddr
= { saddr
};
705 * Normally arps coming in on the wrong interface are ignored,
706 * but if we are bridging and the two interfaces belong to
707 * the same bridge, or one is a member of the bridge which
708 * is the other, then it isn't an error.
710 if (rt
->rt_ifp
!= ifp
) {
712 * (1) ifp and rt_ifp both members of same bridge
713 * (2) rt_ifp member of bridge ifp
714 * (3) ifp member of bridge rt_ifp
716 * Always replace rt_ifp with the bridge ifc.
720 if (ifp
->if_bridge
&&
721 rt
->rt_ifp
->if_bridge
== ifp
->if_bridge
) {
722 nifp
= ether_bridge_interface(ifp
);
723 } else if (rt
->rt_ifp
->if_bridge
&&
724 ether_bridge_interface(rt
->rt_ifp
) == ifp
) {
726 } else if (ifp
->if_bridge
&&
727 ether_bridge_interface(ifp
) == rt
->rt_ifp
) {
733 if ((log_arp_wrong_iface
== 1 && nifp
== NULL
) ||
734 log_arp_wrong_iface
== 2) {
735 hexncpy((u_char
*)ar_sha(ah
), ifp
->if_addrlen
,
736 hexstr
[0], HEX_NCPYLEN(ifp
->if_addrlen
), ":");
739 "but got reply from %s on %s\n",
741 rt
->rt_ifp
->if_xname
, hexstr
[0],
748 * nifp is our man! Replace rt_ifp and adjust
751 ifp
= rt
->rt_ifp
= nifp
;
752 if (sdl
->sdl_type
!= ifp
->if_type
) {
753 sdl
->sdl_type
= ifp
->if_type
;
756 if (sdl
->sdl_index
!= ifp
->if_index
) {
757 sdl
->sdl_index
= ifp
->if_index
;
762 bcmp(ar_sha(ah
), LLADDR(sdl
), sdl
->sdl_alen
)) {
764 if (rt
->rt_expire
!= 0) {
765 if (dologging
&& log_arp_movements
) {
766 hexncpy((u_char
*)LLADDR(sdl
), ifp
->if_addrlen
,
767 hexstr
[0], HEX_NCPYLEN(ifp
->if_addrlen
), ":");
768 hexncpy((u_char
*)ar_sha(ah
), ifp
->if_addrlen
,
769 hexstr
[1], HEX_NCPYLEN(ifp
->if_addrlen
), ":");
771 "arp: %s moved from %s to %s on %s\n",
772 inet_ntoa(isaddr
), hexstr
[0], hexstr
[1],
776 if (dologging
&& log_arp_permanent_modify
) {
777 hexncpy((u_char
*)ar_sha(ah
), ifp
->if_addrlen
,
778 hexstr
[0], HEX_NCPYLEN(ifp
->if_addrlen
), ":");
780 "arp: %s attempts to modify "
781 "permanent entry for %s on %s\n",
782 hexstr
[0], inet_ntoa(isaddr
), ifp
->if_xname
);
788 * sanity check for the address length.
789 * XXX this does not work for protocols with variable address
792 if (dologging
&& sdl
->sdl_alen
&& sdl
->sdl_alen
!= ah
->ar_hln
) {
793 hexncpy((u_char
*)ar_sha(ah
), ifp
->if_addrlen
,
794 hexstr
[0], HEX_NCPYLEN(ifp
->if_addrlen
), ":");
796 "arp from %s: new addr len %d, was %d",
797 hexstr
[0], ah
->ar_hln
, sdl
->sdl_alen
);
799 if (ifp
->if_addrlen
!= ah
->ar_hln
) {
801 hexncpy((u_char
*)ar_sha(ah
), ifp
->if_addrlen
,
802 hexstr
[0], HEX_NCPYLEN(ifp
->if_addrlen
), ":");
804 "arp from %s: addr len: new %d, i/f %d "
805 "(ignored)", hexstr
[0],
806 ah
->ar_hln
, ifp
->if_addrlen
);
810 memcpy(LLADDR(sdl
), ar_sha(ah
), sdl
->sdl_alen
= ah
->ar_hln
);
811 if (rt
->rt_expire
!= 0) {
812 if (rt
->rt_expire
!= time_uptime
+ arpt_keep
&&
813 rt
->rt_expire
!= time_uptime
+ arpt_keep
- 1) {
814 rt
->rt_expire
= time_uptime
+ arpt_keep
;
818 if (rt
->rt_flags
& RTF_REJECT
) {
819 rt
->rt_flags
&= ~RTF_REJECT
;
822 if (la
->la_asked
!= 0) {
826 if (la
->la_preempt
!= arp_maxtries
) {
827 la
->la_preempt
= arp_maxtries
;
832 * This particular cpu might have been holding an mbuf
833 * pending ARP resolution. If so, transmit the mbuf now.
835 if (la
->la_hold
!= NULL
) {
836 struct mbuf
*m
= la
->la_hold
;
839 m_adj(m
, sizeof(struct ether_header
));
840 ifp
->if_output(ifp
, m
, rt_key(rt
), rt
);
848 * Called from arpintr() - this routine is run from a single cpu.
851 in_arpinput(struct mbuf
*m
)
854 struct ifnet
*ifp
= m
->m_pkthdr
.rcvif
;
855 struct ifaddr_container
*ifac
;
856 struct in_ifaddr_container
*iac
;
857 struct in_ifaddr
*ia
= NULL
;
858 struct in_addr isaddr
, itaddr
, myaddr
;
859 uint8_t *enaddr
= NULL
;
864 req_len
= arphdr_len2(ifp
->if_addrlen
, sizeof(struct in_addr
));
865 if (m
->m_len
< req_len
&& (m
= m_pullup(m
, req_len
)) == NULL
) {
866 log(LOG_ERR
, "in_arp: runt packet -- m_pullup failed\n");
870 ah
= mtod(m
, struct arphdr
*);
871 memcpy(&isaddr
, ar_spa(ah
), sizeof isaddr
);
872 memcpy(&itaddr
, ar_tpa(ah
), sizeof itaddr
);
875 * Check both target and sender IP addresses:
877 * If we receive the packet on the interface owning the address,
878 * then accept the address.
880 * For a bridge, we accept the address if the receive interface and
881 * the interface owning the address are on the same bridge, and
882 * use the bridge MAC as the is-at response. The bridge will be
883 * responsible for handling the packet.
885 * (0) Check target IP against CARP IPs
888 LIST_FOREACH(iac
, INADDR_HASH(itaddr
.s_addr
), ia_hash
) {
889 int is_match
= 0, is_parent
= 0;
893 /* Skip all ia's which don't match */
894 if (itaddr
.s_addr
!= ia
->ia_addr
.sin_addr
.s_addr
)
897 if (ia
->ia_ifp
->if_type
!= IFT_CARP
)
900 if (carp_parent(ia
->ia_ifp
) == ifp
)
902 if (is_parent
|| ia
->ia_ifp
== ifp
)
903 is_match
= carp_iamatch(ia
);
908 * The parent interface will also receive
909 * the ethernet broadcast packets, e.g. ARP
910 * REQUEST, so if we could find a CARP
911 * interface of the parent that could match
912 * the target IP address, we then drop the
913 * packets, which is delieverd to us through
914 * the parent interface.
925 * (1) Check target IP against our local IPs
927 LIST_FOREACH(iac
, INADDR_HASH(itaddr
.s_addr
), ia_hash
) {
930 /* Skip all ia's which don't match */
931 if (itaddr
.s_addr
!= ia
->ia_addr
.sin_addr
.s_addr
)
935 /* CARP interfaces are checked in (0) */
936 if (ia
->ia_ifp
->if_type
== IFT_CARP
)
940 if (ifp
->if_bridge
&& ia
->ia_ifp
&&
941 ifp
->if_bridge
== ia
->ia_ifp
->if_bridge
) {
942 ifp
= ether_bridge_interface(ifp
);
945 if (ia
->ia_ifp
&& ia
->ia_ifp
->if_bridge
&&
946 ether_bridge_interface(ia
->ia_ifp
) == ifp
) {
949 if (ifp
->if_bridge
&& ether_bridge_interface(ifp
) ==
953 if (ia
->ia_ifp
== ifp
) {
959 * (2) Check sender IP against our local IPs
961 LIST_FOREACH(iac
, INADDR_HASH(isaddr
.s_addr
), ia_hash
) {
964 /* Skip all ia's which don't match */
965 if (isaddr
.s_addr
!= ia
->ia_addr
.sin_addr
.s_addr
)
968 if (ifp
->if_bridge
&& ia
->ia_ifp
&&
969 ifp
->if_bridge
== ia
->ia_ifp
->if_bridge
) {
970 ifp
= ether_bridge_interface(ifp
);
973 if (ia
->ia_ifp
&& ia
->ia_ifp
->if_bridge
&&
974 ether_bridge_interface(ia
->ia_ifp
) == ifp
) {
977 if (ifp
->if_bridge
&& ether_bridge_interface(ifp
) ==
982 if (ia
->ia_ifp
== ifp
)
987 * No match, use the first inet address on the receive interface
988 * as a dummy address for the rest of the function.
990 TAILQ_FOREACH(ifac
, &ifp
->if_addrheads
[mycpuid
], ifa_link
) {
991 struct ifaddr
*ifa
= ifac
->ifa
;
993 if (ifa
->ifa_addr
&& ifa
->ifa_addr
->sa_family
== AF_INET
) {
1000 * If we got here, we didn't find any suitable interface,
1001 * so drop the packet.
1008 enaddr
= (uint8_t *)IF_LLADDR(ifp
);
1009 myaddr
= ia
->ia_addr
.sin_addr
;
1010 if (!bcmp(ar_sha(ah
), enaddr
, ifp
->if_addrlen
)) {
1011 m_freem(m
); /* it's from me, ignore it. */
1014 if (!bcmp(ar_sha(ah
), ifp
->if_broadcastaddr
, ifp
->if_addrlen
)) {
1016 "arp: link address is broadcast for IP address %s!\n",
1021 if (isaddr
.s_addr
== myaddr
.s_addr
&& myaddr
.s_addr
!= 0) {
1022 hexncpy((u_char
*)ar_sha(ah
), ifp
->if_addrlen
,
1023 hexstr
, HEX_NCPYLEN(ifp
->if_addrlen
), ":");
1025 "arp: %s is using my IP address %s!\n",
1026 hexstr
, inet_ntoa(isaddr
));
1030 if (ifp
->if_flags
& IFF_STATICARP
)
1034 * When arp_restricted_match is true and the ARP response is not
1035 * specifically targetted to me, ignore it. Otherwise the entry
1036 * timeout may be updated for an old MAC.
1038 if (arp_restricted_match
&& itaddr
.s_addr
!= myaddr
.s_addr
) {
1044 * Update all CPU's routing tables with this ARP packet.
1046 * However, we only need to generate rtmsg on CPU0.
1048 ASSERT_IN_NETISR(0);
1049 changed
= arp_update_oncpu(m
, isaddr
.s_addr
,
1050 itaddr
.s_addr
== myaddr
.s_addr
,
1051 RTL_REPORTMSG
, TRUE
);
1053 if (ncpus
> 1 && changed
) {
1054 struct netmsg_inarp
*msg
= &m
->m_hdr
.mh_arpmsg
;
1056 netmsg_init(&msg
->base
, NULL
, &netisr_apanic_rport
,
1057 0, arp_update_msghandler
);
1059 msg
->saddr
= isaddr
.s_addr
;
1060 msg
->taddr
= itaddr
.s_addr
;
1061 msg
->myaddr
= myaddr
.s_addr
;
1062 lwkt_sendmsg(netisr_cpuport(1), &msg
->base
.lmsg
);
1068 * Just return here; after all CPUs's routing tables are
1069 * properly updated by this ARP packet, an ARP reply will
1070 * be generated if appropriate.
1074 in_arpreply(m
, itaddr
.s_addr
, myaddr
.s_addr
);
1078 arp_reply_msghandler(netmsg_t msg
)
1080 struct netmsg_inarp
*rmsg
= (struct netmsg_inarp
*)msg
;
1082 in_arpreply(rmsg
->m
, rmsg
->taddr
, rmsg
->myaddr
);
1083 /* Don't reply this netmsg; netmsg_inarp is embedded in mbuf */
1087 arp_update_msghandler(netmsg_t msg
)
1089 struct netmsg_inarp
*rmsg
= (struct netmsg_inarp
*)msg
;
1093 * This message handler will be called on all of the APs;
1094 * no need to generate rtmsg on them.
1096 KASSERT(mycpuid
> 0, ("arp update msg on cpu%d", mycpuid
));
1097 arp_update_oncpu(rmsg
->m
, rmsg
->saddr
,
1098 rmsg
->taddr
== rmsg
->myaddr
,
1099 RTL_DONTREPORT
, FALSE
);
1101 nextcpu
= mycpuid
+ 1;
1102 if (nextcpu
< ncpus
) {
1103 lwkt_forwardmsg(netisr_cpuport(nextcpu
), &rmsg
->base
.lmsg
);
1105 struct mbuf
*m
= rmsg
->m
;
1106 in_addr_t saddr
= rmsg
->saddr
;
1107 in_addr_t taddr
= rmsg
->taddr
;
1108 in_addr_t myaddr
= rmsg
->myaddr
;
1111 * Dispatch this mbuf to netisr0 to perform ARP reply,
1113 * NOTE: netmsg_inarp is embedded in this mbuf.
1115 netmsg_init(&rmsg
->base
, NULL
, &netisr_apanic_rport
,
1116 0, arp_reply_msghandler
);
1118 rmsg
->saddr
= saddr
;
1119 rmsg
->taddr
= taddr
;
1120 rmsg
->myaddr
= myaddr
;
1121 lwkt_sendmsg(netisr_cpuport(0), &rmsg
->base
.lmsg
);
1126 in_arpreply(struct mbuf
*m
, in_addr_t taddr
, in_addr_t myaddr
)
1128 struct ifnet
*ifp
= m
->m_pkthdr
.rcvif
;
1129 const uint8_t *enaddr
;
1132 struct ether_header
*eh
;
1134 ah
= mtod(m
, struct arphdr
*);
1135 if (ntohs(ah
->ar_op
) != ARPOP_REQUEST
) {
1140 enaddr
= (const uint8_t *)IF_LLADDR(ifp
);
1141 if (taddr
== myaddr
) {
1142 /* I am the target */
1143 memcpy(ar_tha(ah
), ar_sha(ah
), ah
->ar_hln
);
1144 memcpy(ar_sha(ah
), enaddr
, ah
->ar_hln
);
1146 struct llinfo_arp
*la
;
1149 la
= arplookup(taddr
, FALSE
, RTL_DONTREPORT
, SIN_PROXY
);
1151 struct sockaddr_in sin
;
1153 if (!arp_proxyall
) {
1158 bzero(&sin
, sizeof sin
);
1159 sin
.sin_family
= AF_INET
;
1160 sin
.sin_len
= sizeof sin
;
1161 sin
.sin_addr
.s_addr
= taddr
;
1163 rt
= rtpurelookup((struct sockaddr
*)&sin
);
1170 * Don't send proxies for nodes on the same interface
1171 * as this one came out of, or we'll get into a fight
1172 * over who claims what Ether address.
1174 if (rt
->rt_ifp
== ifp
) {
1178 memcpy(ar_tha(ah
), ar_sha(ah
), ah
->ar_hln
);
1179 memcpy(ar_sha(ah
), enaddr
, ah
->ar_hln
);
1181 kprintf("arp: proxying for %s\n", inet_ntoa(itaddr
));
1184 struct sockaddr_dl
*sdl
;
1187 memcpy(ar_tha(ah
), ar_sha(ah
), ah
->ar_hln
);
1188 sdl
= SDL(rt
->rt_gateway
);
1189 memcpy(ar_sha(ah
), LLADDR(sdl
), ah
->ar_hln
);
1193 memcpy(ar_tpa(ah
), ar_spa(ah
), ah
->ar_pln
);
1194 memcpy(ar_spa(ah
), &taddr
, ah
->ar_pln
);
1195 ah
->ar_op
= htons(ARPOP_REPLY
);
1196 ah
->ar_pro
= htons(ETHERTYPE_IP
); /* let's be sure! */
1197 switch (ifp
->if_type
) {
1200 * May not be correct for types not explictly
1201 * listed, but it is our best guess.
1204 eh
= (struct ether_header
*)sa
.sa_data
;
1205 memcpy(eh
->ether_dhost
, ar_tha(ah
), sizeof eh
->ether_dhost
);
1206 eh
->ether_type
= htons(ETHERTYPE_ARP
);
1209 sa
.sa_family
= AF_UNSPEC
;
1210 sa
.sa_len
= sizeof sa
;
1211 ifp
->if_output(ifp
, m
, &sa
, NULL
);
1217 * Free an arp entry. If the arp entry is actively referenced or represents
1218 * a static entry we only clear it back to an unresolved state, otherwise
1219 * we destroy the entry entirely.
1221 * Note that static entries are created when route add ... -interface is used
1222 * to create an interface route to a (direct) destination.
1225 arptfree(struct llinfo_arp
*la
)
1227 struct rtentry
*rt
= la
->la_rt
;
1228 struct sockaddr_dl
*sdl
;
1232 sdl
= SDL(rt
->rt_gateway
);
1234 ((rt
->rt_refcnt
> 0 && sdl
->sdl_family
== AF_LINK
) ||
1235 (rt
->rt_flags
& RTF_STATIC
))) {
1237 la
->la_preempt
= la
->la_asked
= 0;
1238 rt
->rt_flags
&= ~RTF_REJECT
;
1241 rtrequest(RTM_DELETE
, rt_key(rt
), NULL
, rt_mask(rt
), 0, NULL
);
1245 * Lookup or enter a new address in arptab.
1247 static struct llinfo_arp
*
1248 arplookup(in_addr_t addr
, boolean_t create
, boolean_t generate_report
,
1252 struct sockaddr_inarp sin
= { sizeof sin
, AF_INET
};
1253 const char *why
= NULL
;
1255 sin
.sin_addr
.s_addr
= addr
;
1256 sin
.sin_other
= proxy
? SIN_PROXY
: 0;
1258 rt
= _rtlookup((struct sockaddr
*)&sin
,
1259 generate_report
, RTL_DOCLONE
);
1261 rt
= rtpurelookup((struct sockaddr
*)&sin
);
1267 if (rt
->rt_flags
& RTF_GATEWAY
)
1268 why
= "host is not on local network";
1269 else if (!(rt
->rt_flags
& RTF_LLINFO
))
1270 why
= "could not allocate llinfo";
1271 else if (rt
->rt_gateway
->sa_family
!= AF_LINK
)
1272 why
= "gateway route is not ours";
1275 if (create
&& log_arp_creation_failure
) {
1276 log(LOG_DEBUG
, "arplookup %s failed: %s\n",
1277 inet_ntoa(sin
.sin_addr
), why
);
1279 if (rt
->rt_refcnt
<= 0 && (rt
->rt_flags
& RTF_WASCLONED
)) {
1280 /* No references to this route. Purge it. */
1281 rtrequest(RTM_DELETE
, rt_key(rt
), rt
->rt_gateway
,
1282 rt_mask(rt
), rt
->rt_flags
, NULL
);
1286 return (rt
->rt_llinfo
);
1290 arp_ifinit(struct ifnet
*ifp
, struct ifaddr
*ifa
)
1292 ifa
->ifa_rtrequest
= arp_rtrequest
;
1293 ifa
->ifa_flags
|= RTF_CLONING
;
1297 arp_gratuitous(struct ifnet
*ifp
, struct ifaddr
*ifa
)
1299 if (IA_SIN(ifa
)->sin_addr
.s_addr
!= INADDR_ANY
) {
1300 arprequest_async(ifp
, &IA_SIN(ifa
)->sin_addr
,
1301 &IA_SIN(ifa
)->sin_addr
, NULL
);
1306 arp_ifaddr(void *arg __unused
, struct ifnet
*ifp
,
1307 enum ifaddr_event event
, struct ifaddr
*ifa
)
1309 if (ifa
->ifa_rtrequest
!= arp_rtrequest
) /* XXX need a generic way */
1311 if (ifa
->ifa_addr
->sa_family
!= AF_INET
)
1313 if (event
== IFADDR_EVENT_DELETE
)
1317 * - CARP interfaces will take care of gratuitous ARP themselves.
1318 * - If we are the CARP interface's parent, don't send gratuitous
1319 * ARP to avoid unnecessary confusion.
1322 if (ifp
->if_type
!= IFT_CARP
&& ifp
->if_carp
== NULL
)
1325 arp_gratuitous(ifp
, ifa
);
1334 for (cpu
= 0; cpu
< ncpus2
; cpu
++)
1335 LIST_INIT(&llinfo_arp_list
[cpu
]);
1337 netisr_register(NETISR_ARP
, arpintr
, NULL
);
1339 EVENTHANDLER_REGISTER(ifaddr_event
, arp_ifaddr
, NULL
,
1340 EVENTHANDLER_PRI_LAST
);
1343 SYSINIT(arp
, SI_SUB_PROTO_DOMAIN
, SI_ORDER_ANY
, arp_init
, 0);