2 * Copyright (c) 2004-2009 Apple Inc. All rights reserved.
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. The rights granted to you under the License
10 * may not be used to create, or enable the creation or redistribution of,
11 * unlawful or unlicensed copies of an Apple operating system, or to
12 * circumvent, violate, or enable the circumvention or violation of, any
13 * terms of an Apple operating system software license agreement.
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
18 * The Original Code and all software distributed under the License are
19 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23 * Please see the License for the specific language governing rights and
24 * limitations under the License.
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
29 * Copyright (c) 1982, 1989, 1993
30 * The Regents of the University of California. All rights reserved.
32 * Redistribution and use in source and binary forms, with or without
33 * modification, are permitted provided that the following conditions
35 * 1. Redistributions of source code must retain the above copyright
36 * notice, this list of conditions and the following disclaimer.
37 * 2. Redistributions in binary form must reproduce the above copyright
38 * notice, this list of conditions and the following disclaimer in the
39 * documentation and/or other materials provided with the distribution.
40 * 3. All advertising materials mentioning features or use of this software
41 * must display the following acknowledgement:
42 * This product includes software developed by the University of
43 * California, Berkeley and its contributors.
44 * 4. Neither the name of the University nor the names of its contributors
45 * may be used to endorse or promote products derived from this software
46 * without specific prior written permission.
48 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
49 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
50 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
51 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
52 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
53 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
54 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
55 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
56 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
57 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
62 #include <kern/debug.h>
63 #include <netinet/in_arp.h>
64 #include <sys/types.h>
65 #include <sys/param.h>
66 #include <sys/kernel_types.h>
67 #include <sys/syslog.h>
68 #include <sys/systm.h>
70 #include <sys/kernel.h>
72 #include <sys/sysctl.h>
74 #include <net/if_arp.h>
75 #include <net/if_dl.h>
77 #include <net/if_types.h>
78 #include <net/route.h>
79 #include <netinet/if_ether.h>
80 #include <netinet/in_var.h>
81 #include <kern/zalloc.h>
83 #define SA(p) ((struct sockaddr *)(p))
84 #define SIN(s) ((struct sockaddr_in *)s)
85 #define CONST_LLADDR(s) ((const u_char*)((s)->sdl_data + (s)->sdl_nlen))
86 #define rt_expire rt_rmx.rmx_expire
87 #define equal(a1, a2) (bcmp((caddr_t)(a1), (caddr_t)(a2), (a1)->sa_len) == 0)
89 static const size_t MAX_HW_LEN
= 10;
91 SYSCTL_DECL(_net_link_ether
);
92 SYSCTL_NODE(_net_link_ether
, PF_INET
, inet
, CTLFLAG_RW
|CTLFLAG_LOCKED
, 0, "");
95 static int arpt_prune
= (5*60*1); /* walk list every 5 minutes */
96 static int arpt_keep
= (20*60); /* once resolved, good for 20 more minutes */
97 static int arpt_down
= 20; /* once declared down, don't send for 20 sec */
99 /* Apple Hardware SUM16 checksuming */
100 int apple_hwcksum_tx
= 1;
101 int apple_hwcksum_rx
= 1;
103 SYSCTL_INT(_net_link_ether_inet
, OID_AUTO
, prune_intvl
, CTLFLAG_RW
,
105 SYSCTL_INT(_net_link_ether_inet
, OID_AUTO
, max_age
, CTLFLAG_RW
,
107 SYSCTL_INT(_net_link_ether_inet
, OID_AUTO
, host_down_time
, CTLFLAG_RW
,
109 SYSCTL_INT(_net_link_ether_inet
, OID_AUTO
, apple_hwcksum_tx
, CTLFLAG_RW
,
110 &apple_hwcksum_tx
, 0, "");
111 SYSCTL_INT(_net_link_ether_inet
, OID_AUTO
, apple_hwcksum_rx
, CTLFLAG_RW
,
112 &apple_hwcksum_rx
, 0, "");
116 * The following are protected by rnh_lock
118 LIST_ENTRY(llinfo_arp
) la_le
;
119 struct rtentry
*la_rt
;
121 * The following are protected by rt_lock
123 struct mbuf
*la_hold
; /* last packet until resolved/timeout */
124 int32_t la_asked
; /* last time we QUERIED for this addr */
128 * Synchronization notes:
130 * The global list of ARP entries are stored in llinfo_arp; an entry
131 * gets inserted into the list when the route is created and gets
132 * removed from the list when it is deleted; this is done as part
133 * of RTM_ADD/RTM_RESOLVE/RTM_DELETE in arp_rtrequest().
135 * Because rnh_lock and rt_lock for the entry are held during those
136 * operations, the same locks (and thus lock ordering) must be used
137 * elsewhere to access the relevant data structure fields:
139 * la_le.{le_next,le_prev}, la_rt
141 * - Routing lock (rnh_lock)
145 * - Routing entry lock (rt_lock)
147 * Due to the dependency on rt_lock, llinfo_arp has the same lifetime
148 * as the route entry itself. When a route is deleted (RTM_DELETE),
149 * it is simply removed from the global list but the memory is not
150 * freed until the route itself is freed.
152 static LIST_HEAD(, llinfo_arp
) llinfo_arp
;
154 static int arp_inuse
, arp_allocated
;
156 static int arp_maxtries
= 5;
157 static int useloopback
= 1; /* use loopback interface for local traffic */
158 static int arp_proxyall
= 0;
159 static int arp_sendllconflict
= 0;
161 SYSCTL_INT(_net_link_ether_inet
, OID_AUTO
, maxtries
, CTLFLAG_RW
,
162 &arp_maxtries
, 0, "");
163 SYSCTL_INT(_net_link_ether_inet
, OID_AUTO
, useloopback
, CTLFLAG_RW
,
164 &useloopback
, 0, "");
165 SYSCTL_INT(_net_link_ether_inet
, OID_AUTO
, proxyall
, CTLFLAG_RW
,
166 &arp_proxyall
, 0, "");
167 SYSCTL_INT(_net_link_ether_inet
, OID_AUTO
, sendllconflict
, CTLFLAG_RW
,
168 &arp_sendllconflict
, 0, "");
170 static int log_arp_warnings
= 0;
172 SYSCTL_INT(_net_link_ether_inet
, OID_AUTO
, log_arp_warnings
, CTLFLAG_RW
,
173 &log_arp_warnings
, 0,
174 "log arp warning messages");
176 static int keep_announcements
= 1;
177 SYSCTL_INT(_net_link_ether_inet
, OID_AUTO
, keep_announcements
, CTLFLAG_RW
,
178 &keep_announcements
, 0,
179 "keep arp announcements");
181 static int send_conflicting_probes
= 1;
182 SYSCTL_INT(_net_link_ether_inet
, OID_AUTO
, send_conflicting_probes
, CTLFLAG_RW
,
183 &send_conflicting_probes
, 0,
184 "send conflicting link-local arp probes");
186 static errno_t
arp_lookup_route(const struct in_addr
*, int,
187 int, route_t
*, unsigned int);
188 static void arptimer(void *);
189 static struct llinfo_arp
*arp_llinfo_alloc(void);
190 static void arp_llinfo_free(void *);
192 extern u_int32_t ipv4_ll_arp_aware
;
194 static int arpinit_done
;
196 static struct zone
*llinfo_arp_zone
;
197 #define LLINFO_ARP_ZONE_MAX 256 /* maximum elements in zone */
198 #define LLINFO_ARP_ZONE_NAME "llinfo_arp" /* name for zone */
204 log(LOG_NOTICE
, "arp_init called more than once (ignored)\n");
208 LIST_INIT(&llinfo_arp
);
210 llinfo_arp_zone
= zinit(sizeof (struct llinfo_arp
),
211 LLINFO_ARP_ZONE_MAX
* sizeof (struct llinfo_arp
), 0,
212 LLINFO_ARP_ZONE_NAME
);
213 if (llinfo_arp_zone
== NULL
)
214 panic("%s: failed allocating llinfo_arp_zone", __func__
);
216 zone_change(llinfo_arp_zone
, Z_EXPAND
, TRUE
);
221 timeout(arptimer
, (caddr_t
)0, hz
);
224 static struct llinfo_arp
*
225 arp_llinfo_alloc(void)
227 return (zalloc(llinfo_arp_zone
));
231 arp_llinfo_free(void *arg
)
233 struct llinfo_arp
*la
= arg
;
235 if (la
->la_le
.le_next
!= NULL
|| la
->la_le
.le_prev
!= NULL
) {
236 panic("%s: trying to free %p when it is in use", __func__
, la
);
240 /* Just in case there's anything there, free it */
241 if (la
->la_hold
!= NULL
) {
242 m_freem(la
->la_hold
);
246 zfree(llinfo_arp_zone
, la
);
253 arptfree(struct llinfo_arp
*la
)
255 struct rtentry
*rt
= la
->la_rt
;
256 struct sockaddr_dl
*sdl
;
258 lck_mtx_assert(rnh_lock
, LCK_MTX_ASSERT_OWNED
);
259 RT_LOCK_ASSERT_HELD(rt
);
261 if (rt
->rt_refcnt
> 0 && (sdl
= SDL(rt
->rt_gateway
)) &&
262 sdl
->sdl_family
== AF_LINK
) {
265 rt
->rt_flags
&= ~RTF_REJECT
;
269 * Safe to drop rt_lock and use rt_key, since holding
270 * rnh_lock here prevents another thread from calling
271 * rt_setgate() on this route.
274 rtrequest_locked(RTM_DELETE
, rt_key(rt
), NULL
, rt_mask(rt
),
280 * Timeout routine. Age arp_tab entries periodically.
284 arptimer(void *ignored_arg
)
286 #pragma unused (ignored_arg)
287 struct llinfo_arp
*la
, *ola
;
288 struct timeval timenow
;
290 lck_mtx_lock(rnh_lock
);
291 la
= llinfo_arp
.lh_first
;
292 getmicrotime(&timenow
);
293 while ((ola
= la
) != 0) {
294 struct rtentry
*rt
= la
->la_rt
;
295 la
= la
->la_le
.le_next
;
297 if (rt
->rt_expire
&& rt
->rt_expire
<= timenow
.tv_sec
)
298 arptfree(ola
); /* timer has expired, clear */
302 lck_mtx_unlock(rnh_lock
);
303 timeout(arptimer
, (caddr_t
)0, arpt_prune
* hz
);
307 * Parallel to llc_rtrequest.
313 __unused
struct sockaddr
*sa
)
315 struct sockaddr
*gate
= rt
->rt_gateway
;
316 struct llinfo_arp
*la
= rt
->rt_llinfo
;
317 static struct sockaddr_dl null_sdl
= {sizeof(null_sdl
), AF_LINK
, 0, 0, 0, 0, 0, {0}};
318 struct timeval timenow
;
321 panic("%s: ARP has not been initialized", __func__
);
324 lck_mtx_assert(rnh_lock
, LCK_MTX_ASSERT_OWNED
);
325 RT_LOCK_ASSERT_HELD(rt
);
327 if (rt
->rt_flags
& RTF_GATEWAY
)
329 getmicrotime(&timenow
);
334 * XXX: If this is a manually added route to interface
335 * such as older version of routed or gated might provide,
336 * restore cloning bit.
338 if ((rt
->rt_flags
& RTF_HOST
) == 0 &&
339 SIN(rt_mask(rt
))->sin_addr
.s_addr
!= 0xffffffff)
340 rt
->rt_flags
|= RTF_CLONING
;
341 if (rt
->rt_flags
& RTF_CLONING
) {
343 * Case 1: This route should come from a route to iface.
345 if (rt_setgate(rt
, rt_key(rt
),
346 (struct sockaddr
*)&null_sdl
) == 0) {
347 gate
= rt
->rt_gateway
;
348 SDL(gate
)->sdl_type
= rt
->rt_ifp
->if_type
;
349 SDL(gate
)->sdl_index
= rt
->rt_ifp
->if_index
;
351 * In case we're called before 1.0 sec.
354 rt
->rt_expire
= MAX(timenow
.tv_sec
, 1);
358 /* Announce a new entry if requested. */
359 if (rt
->rt_flags
& RTF_ANNOUNCE
) {
361 dlil_send_arp(rt
->rt_ifp
, ARPOP_REQUEST
,
362 SDL(gate
), rt_key(rt
), NULL
, rt_key(rt
));
367 if (gate
->sa_family
!= AF_LINK
||
368 gate
->sa_len
< sizeof(null_sdl
)) {
369 if (log_arp_warnings
)
370 log(LOG_DEBUG
, "arp_rtrequest: bad gateway value\n");
373 SDL(gate
)->sdl_type
= rt
->rt_ifp
->if_type
;
374 SDL(gate
)->sdl_index
= rt
->rt_ifp
->if_index
;
376 break; /* This happens on a route change */
378 * Case 2: This route may come from cloning, or a manual route
379 * add with a LL address.
381 rt
->rt_llinfo
= la
= arp_llinfo_alloc();
383 if (log_arp_warnings
)
384 log(LOG_DEBUG
, "%s: malloc failed\n", __func__
);
387 rt
->rt_llinfo_free
= arp_llinfo_free
;
389 arp_inuse
++, arp_allocated
++;
390 Bzero(la
, sizeof(*la
));
392 rt
->rt_flags
|= RTF_LLINFO
;
393 LIST_INSERT_HEAD(&llinfo_arp
, la
, la_le
);
396 * This keeps the multicast addresses from showing up
397 * in `arp -a' listings as unresolved. It's not actually
398 * functional. Then the same for broadcast.
400 if (IN_MULTICAST(ntohl(SIN(rt_key(rt
))->sin_addr
.s_addr
))) {
402 dlil_resolve_multi(rt
->rt_ifp
, rt_key(rt
), gate
,
403 sizeof(struct sockaddr_dl
));
407 else if (in_broadcast(SIN(rt_key(rt
))->sin_addr
, rt
->rt_ifp
)) {
408 struct sockaddr_dl
*gate_ll
= SDL(gate
);
409 size_t broadcast_len
;
410 ifnet_llbroadcast_copy_bytes(rt
->rt_ifp
,
411 LLADDR(gate_ll
), sizeof(gate_ll
->sdl_data
),
413 gate_ll
->sdl_alen
= broadcast_len
;
414 gate_ll
->sdl_family
= AF_LINK
;
415 gate_ll
->sdl_len
= sizeof(struct sockaddr_dl
);
416 /* In case we're called before 1.0 sec. has elapsed */
417 rt
->rt_expire
= MAX(timenow
.tv_sec
, 1);
420 if (SIN(rt_key(rt
))->sin_addr
.s_addr
==
421 (IA_SIN(rt
->rt_ifa
))->sin_addr
.s_addr
) {
423 * This test used to be
424 * if (loif.if_flags & IFF_UP)
425 * It allowed local traffic to be forced
426 * through the hardware by configuring the loopback down.
427 * However, it causes problems during network configuration
428 * for boards that can't receive packets they send.
429 * It is now necessary to clear "useloopback" and remove
430 * the route to force traffic out to the hardware.
433 ifnet_lladdr_copy_bytes(rt
->rt_ifp
, LLADDR(SDL(gate
)), SDL(gate
)->sdl_alen
= 6);
445 * Unchain it but defer the actual freeing until the route
446 * itself is to be freed. rt->rt_llinfo still points to
447 * llinfo_arp, and likewise, la->la_rt still points to this
448 * route entry, except that RTF_LLINFO is now cleared.
450 LIST_REMOVE(la
, la_le
);
451 la
->la_le
.le_next
= NULL
;
452 la
->la_le
.le_prev
= NULL
;
453 rt
->rt_flags
&= ~RTF_LLINFO
;
454 if (la
->la_hold
!= NULL
)
455 m_freem(la
->la_hold
);
461 * convert hardware address to hex string for logging errors.
464 sdl_addr_to_hex(const struct sockaddr_dl
*sdl
, char * orig_buf
, int buflen
)
466 char * buf
= orig_buf
;
468 const u_char
* lladdr
= (u_char
*)(size_t)sdl
->sdl_data
;
469 int maxbytes
= buflen
/ 3;
471 if (maxbytes
> sdl
->sdl_alen
) {
472 maxbytes
= sdl
->sdl_alen
;
475 for (i
= 0; i
< maxbytes
; i
++) {
476 snprintf(buf
, 3, "%02x", lladdr
[i
]);
478 *buf
= (i
== maxbytes
- 1) ? '\0' : ':';
485 * arp_lookup_route will lookup the route for a given address.
487 * The address must be for a host on a local network on this interface.
488 * If the returned route is non-NULL, the route is locked and the caller
489 * is responsible for unlocking it and releasing its reference.
492 arp_lookup_route(const struct in_addr
*addr
, int create
, int proxy
,
493 route_t
*route
, unsigned int ifscope
)
495 struct sockaddr_inarp sin
= {sizeof(sin
), AF_INET
, 0, {0}, {0}, 0, 0};
496 const char *why
= NULL
;
502 sin
.sin_addr
.s_addr
= addr
->s_addr
;
503 sin
.sin_other
= proxy
? SIN_PROXY
: 0;
505 rt
= rtalloc1_scoped((struct sockaddr
*)&sin
, create
, 0, ifscope
);
507 return (ENETUNREACH
);
511 if (rt
->rt_flags
& RTF_GATEWAY
) {
512 why
= "host is not on local network";
514 } else if (!(rt
->rt_flags
& RTF_LLINFO
)) {
515 why
= "could not allocate llinfo";
517 } else if (rt
->rt_gateway
->sa_family
!= AF_LINK
) {
518 why
= "gateway route is not ours";
519 error
= EPROTONOSUPPORT
;
523 if (create
&& log_arp_warnings
) {
524 char tmp
[MAX_IPv4_STR_LEN
];
525 log(LOG_DEBUG
, "arplookup link#%d %s failed: %s\n",
526 ifscope
, inet_ntop(AF_INET
, addr
, tmp
,
531 * If there are no references to this route, and it is
532 * a cloned route, and not static, and ARP had created
533 * the route, then purge it from the routing table as
534 * it is probably bogus.
536 if (rt
->rt_refcnt
== 1 &&
537 (rt
->rt_flags
& (RTF_WASCLONED
| RTF_STATIC
)) ==
540 * Prevent another thread from modiying rt_key,
541 * rt_gateway via rt_setgate() after rt_lock is
542 * dropped by marking the route as defunct.
544 rt
->rt_flags
|= RTF_CONDEMNED
;
546 rtrequest(RTM_DELETE
, rt_key(rt
), rt
->rt_gateway
,
547 rt_mask(rt
), rt
->rt_flags
, 0);
550 RT_REMREF_LOCKED(rt
);
557 * Caller releases reference and does RT_UNLOCK(rt).
564 * arp_route_to_gateway_route will find the gateway route for a given route.
566 * If the route is down, look the route up again.
567 * If the route goes through a gateway, get the route to the gateway.
568 * If the gateway route is down, look it up again.
569 * If the route is set to reject, verify it hasn't expired.
571 * If the returned route is non-NULL, the caller is responsible for
572 * releasing the reference and unlocking the route.
574 #define senderr(e) { error = (e); goto bad; }
575 __private_extern__ errno_t
576 arp_route_to_gateway_route(const struct sockaddr
*net_dest
, route_t hint0
,
579 struct timeval timenow
;
580 route_t rt
= hint0
, hint
= hint0
;
586 * Next hop determination. Because we may involve the gateway route
587 * in addition to the original route, locking is rather complicated.
588 * The general concept is that regardless of whether the route points
589 * to the original route or to the gateway route, this routine takes
590 * an extra reference on such a route. This extra reference will be
591 * released at the end.
593 * Care must be taken to ensure that the "hint0" route never gets freed
594 * via rtfree(), since the caller may have stored it inside a struct
595 * route with a reference held for that placeholder.
598 unsigned int ifindex
;
601 ifindex
= rt
->rt_ifp
->if_index
;
602 RT_ADDREF_LOCKED(rt
);
603 if (!(rt
->rt_flags
& RTF_UP
)) {
604 RT_REMREF_LOCKED(rt
);
606 /* route is down, find a new one */
607 hint
= rt
= rtalloc1_scoped((struct sockaddr
*)
608 (size_t)net_dest
, 1, 0, ifindex
);
611 ifindex
= rt
->rt_ifp
->if_index
;
613 senderr(EHOSTUNREACH
);
618 * We have a reference to "rt" by now; it will either
619 * be released or freed at the end of this routine.
621 RT_LOCK_ASSERT_HELD(rt
);
622 if (rt
->rt_flags
& RTF_GATEWAY
) {
623 struct rtentry
*gwrt
= rt
->rt_gwroute
;
624 struct sockaddr_in gw
;
626 /* If there's no gateway rt, look it up */
628 gw
= *((struct sockaddr_in
*)rt
->rt_gateway
);
632 /* Become a regular mutex */
636 * Take gwrt's lock while holding route's lock;
637 * this is okay since gwrt never points back
638 * to "rt", so no lock ordering issues.
641 if (!(gwrt
->rt_flags
& RTF_UP
)) {
642 struct rtentry
*ogwrt
;
644 rt
->rt_gwroute
= NULL
;
646 gw
= *((struct sockaddr_in
*)rt
->rt_gateway
);
650 gwrt
= rtalloc1_scoped(
651 (struct sockaddr
*)&gw
, 1, 0, ifindex
);
655 * Bail out if the route is down, no route
656 * to gateway, circular route, or if the
657 * gateway portion of "rt" has changed.
659 if (!(rt
->rt_flags
& RTF_UP
) ||
660 gwrt
== NULL
|| gwrt
== rt
||
661 !equal(SA(&gw
), rt
->rt_gateway
)) {
663 RT_REMREF_LOCKED(gwrt
);
669 senderr(EHOSTUNREACH
);
672 /* Remove any existing gwrt */
673 ogwrt
= rt
->rt_gwroute
;
674 if ((rt
->rt_gwroute
= gwrt
) != NULL
)
677 /* Clean up "rt" now while we can */
679 RT_REMREF_LOCKED(rt
);
686 /* Now free the replaced gwrt */
689 /* If still no route to gateway, bail out */
691 senderr(EHOSTUNREACH
);
693 RT_ADDREF_LOCKED(gwrt
);
695 /* Clean up "rt" now while we can */
697 RT_REMREF_LOCKED(rt
);
706 /* rt == gwrt; if it is now down, give up */
708 if (!(rt
->rt_flags
& RTF_UP
)) {
710 senderr(EHOSTUNREACH
);
714 if (rt
->rt_flags
& RTF_REJECT
) {
715 getmicrotime(&timenow
);
716 if (rt
->rt_rmx
.rmx_expire
== 0 ||
717 timenow
.tv_sec
< rt
->rt_rmx
.rmx_expire
) {
719 senderr(rt
== hint
? EHOSTDOWN
: EHOSTUNREACH
);
723 /* Become a regular mutex */
726 /* Caller is responsible for cleaning up "rt" */
732 /* Clean up route (either it is "rt" or "gwrt") */
736 RT_REMREF_LOCKED(rt
);
748 * This is the ARP pre-output routine; care must be taken to ensure that
749 * the "hint" route never gets freed via rtfree(), since the caller may
750 * have stored it inside a struct route with a reference held for that
754 arp_lookup_ip(ifnet_t ifp
, const struct sockaddr_in
*net_dest
,
755 struct sockaddr_dl
*ll_dest
, size_t ll_dest_len
, route_t hint
,
758 route_t route
= NULL
; /* output route */
760 struct sockaddr_dl
*gateway
;
761 struct llinfo_arp
*llinfo
;
762 struct timeval timenow
;
764 if (net_dest
->sin_family
!= AF_INET
)
765 return (EAFNOSUPPORT
);
767 if ((ifp
->if_flags
& (IFF_UP
|IFF_RUNNING
)) != (IFF_UP
|IFF_RUNNING
))
771 * If we were given a route, verify the route and grab the gateway
775 * Callee holds a reference on the route and returns
776 * with the route entry locked, upon success.
778 result
= arp_route_to_gateway_route((const struct sockaddr
*)
779 net_dest
, hint
, &route
);
783 RT_LOCK_ASSERT_HELD(route
);
786 if (packet
->m_flags
& M_BCAST
) {
787 size_t broadcast_len
;
788 bzero(ll_dest
, ll_dest_len
);
789 result
= ifnet_llbroadcast_copy_bytes(ifp
, LLADDR(ll_dest
),
790 ll_dest_len
- offsetof(struct sockaddr_dl
, sdl_data
),
793 ll_dest
->sdl_alen
= broadcast_len
;
794 ll_dest
->sdl_family
= AF_LINK
;
795 ll_dest
->sdl_len
= sizeof(struct sockaddr_dl
);
799 if (packet
->m_flags
& M_MCAST
) {
802 result
= dlil_resolve_multi(ifp
,
803 (const struct sockaddr
*)net_dest
,
804 (struct sockaddr
*)ll_dest
, ll_dest_len
);
811 * If we didn't find a route, or the route doesn't have
812 * link layer information, trigger the creation of the
813 * route and link layer information.
815 if (route
== NULL
|| route
->rt_llinfo
== NULL
) {
816 /* Clean up now while we can */
819 RT_REMREF_LOCKED(route
);
827 * Callee holds a reference on the route and returns
828 * with the route entry locked, upon success.
830 result
= arp_lookup_route(&net_dest
->sin_addr
, 1, 0, &route
,
833 RT_LOCK_ASSERT_HELD(route
);
836 if (result
|| route
== NULL
|| route
->rt_llinfo
== NULL
) {
837 char tmp
[MAX_IPv4_STR_LEN
];
839 /* In case result is 0 but no route, return an error */
841 result
= EHOSTUNREACH
;
843 if (log_arp_warnings
&&
844 route
!= NULL
&& route
->rt_llinfo
== NULL
)
845 log(LOG_DEBUG
, "arpresolve: can't allocate llinfo "
846 "for %s\n", inet_ntop(AF_INET
, &net_dest
->sin_addr
,
852 * Now that we have the right route, is it filled in?
854 gateway
= SDL(route
->rt_gateway
);
855 getmicrotime(&timenow
);
856 if ((route
->rt_rmx
.rmx_expire
== 0 ||
857 route
->rt_rmx
.rmx_expire
> timenow
.tv_sec
) && gateway
!= NULL
&&
858 gateway
->sdl_family
== AF_LINK
&& gateway
->sdl_alen
!= 0) {
859 bcopy(gateway
, ll_dest
, MIN(gateway
->sdl_len
, ll_dest_len
));
864 if (ifp
->if_flags
& IFF_NOARP
) {
870 * Route wasn't complete/valid. We need to arp.
872 llinfo
= route
->rt_llinfo
;
873 if (packet
!= NULL
) {
874 if (llinfo
->la_hold
!= NULL
)
875 m_freem(llinfo
->la_hold
);
876 llinfo
->la_hold
= packet
;
879 if (route
->rt_rmx
.rmx_expire
) {
880 route
->rt_flags
&= ~RTF_REJECT
;
881 if (llinfo
->la_asked
== 0 ||
882 route
->rt_rmx
.rmx_expire
!= timenow
.tv_sec
) {
883 route
->rt_rmx
.rmx_expire
= timenow
.tv_sec
;
884 if (llinfo
->la_asked
++ < arp_maxtries
) {
885 struct ifaddr
*rt_ifa
= route
->rt_ifa
;
888 dlil_send_arp(ifp
, ARPOP_REQUEST
, NULL
,
889 rt_ifa
->ifa_addr
, NULL
,
890 (const struct sockaddr
*)net_dest
);
893 result
= EJUSTRETURN
;
896 route
->rt_flags
|= RTF_REJECT
;
897 route
->rt_rmx
.rmx_expire
+= arpt_down
;
898 llinfo
->la_asked
= 0;
899 llinfo
->la_hold
= NULL
;
900 result
= EHOSTUNREACH
;
906 /* The packet is now held inside la_hold (can "packet" be NULL?) */
907 result
= EJUSTRETURN
;
912 RT_REMREF_LOCKED(route
);
926 const struct sockaddr_dl
*sender_hw
,
927 const struct sockaddr_in
*sender_ip
,
928 const struct sockaddr_in
*target_ip
)
930 char ipv4str
[MAX_IPv4_STR_LEN
];
931 struct sockaddr_dl proxied
;
932 struct sockaddr_dl
*gateway
, *target_hw
= NULL
;
934 struct in_ifaddr
*ia
;
935 struct in_ifaddr
*best_ia
= NULL
;
936 route_t route
= NULL
;
937 char buf
[3 * MAX_HW_LEN
]; // enough for MAX_HW_LEN byte hw address
938 struct llinfo_arp
*llinfo
;
940 int created_announcement
= 0;
941 int bridged
= 0, is_bridge
= 0;
943 /* Do not respond to requests for 0.0.0.0 */
944 if (target_ip
->sin_addr
.s_addr
== 0 && arpop
== ARPOP_REQUEST
)
949 if (ifp
->if_type
== IFT_BRIDGE
)
953 * Determine if this ARP is for us
954 * For a bridge, we want to check the address irrespective
955 * of the receive interface.
957 lck_rw_lock_shared(in_ifaddr_rwlock
);
958 TAILQ_FOREACH(ia
, INADDR_HASH(target_ip
->sin_addr
.s_addr
), ia_hash
) {
959 if (((bridged
&& ia
->ia_ifp
->if_bridge
!= NULL
) ||
960 (ia
->ia_ifp
== ifp
)) &&
961 ia
->ia_addr
.sin_addr
.s_addr
== target_ip
->sin_addr
.s_addr
) {
963 ifaref(&best_ia
->ia_ifa
);
964 lck_rw_done(in_ifaddr_rwlock
);
969 TAILQ_FOREACH(ia
, INADDR_HASH(sender_ip
->sin_addr
.s_addr
), ia_hash
) {
970 if (((bridged
&& ia
->ia_ifp
->if_bridge
!= NULL
) ||
971 (ia
->ia_ifp
== ifp
)) &&
972 ia
->ia_addr
.sin_addr
.s_addr
== sender_ip
->sin_addr
.s_addr
) {
974 ifaref(&best_ia
->ia_ifa
);
975 lck_rw_done(in_ifaddr_rwlock
);
980 #define BDG_MEMBER_MATCHES_ARP(addr, ifp, ia) \
981 (ia->ia_ifp->if_bridge == ifp->if_softc && \
982 !bcmp(ifnet_lladdr(ia->ia_ifp), ifnet_lladdr(ifp), ifp->if_addrlen) && \
983 addr == ia->ia_addr.sin_addr.s_addr)
985 * Check the case when bridge shares its MAC address with
986 * some of its children, so packets are claimed by bridge
987 * itself (bridge_input() does it first), but they are really
988 * meant to be destined to the bridge member.
991 TAILQ_FOREACH(ia
, INADDR_HASH(target_ip
->sin_addr
.s_addr
), ia_hash
) {
992 if (BDG_MEMBER_MATCHES_ARP(target_ip
->sin_addr
.s_addr
, ifp
, ia
)) {
995 ifaref(&best_ia
->ia_ifa
);
996 lck_rw_done(in_ifaddr_rwlock
);
1001 lck_rw_done(in_ifaddr_rwlock
);
1004 * No match, use the first inet address on the receive interface
1005 * as a dummy address for the rest of the function; we may be
1006 * proxying for another address.
1008 ifnet_lock_shared(ifp
);
1009 TAILQ_FOREACH(ifa
, &ifp
->if_addrhead
, ifa_link
) {
1010 if (ifa
->ifa_addr
->sa_family
!= AF_INET
)
1012 best_ia
= (struct in_ifaddr
*)ifa
;
1013 ifaref(&best_ia
->ia_ifa
);
1014 ifnet_lock_done(ifp
);
1017 ifnet_lock_done(ifp
);
1020 * If we're not a bridge member, or if we are but there's no
1021 * IPv4 address to use for the interface, drop the packet.
1023 if (!bridged
|| best_ia
== NULL
)
1027 /* If the packet is from this interface, ignore the packet */
1028 if (!bcmp(CONST_LLADDR(sender_hw
), ifnet_lladdr(ifp
), sender_hw
->sdl_len
)) {
1032 /* Check for a conflict */
1033 if (!bridged
&& sender_ip
->sin_addr
.s_addr
== best_ia
->ia_addr
.sin_addr
.s_addr
) {
1034 struct kev_msg ev_msg
;
1035 struct kev_in_collision
*in_collision
;
1036 u_char storage
[sizeof(struct kev_in_collision
) + MAX_HW_LEN
];
1037 in_collision
= (struct kev_in_collision
*)storage
;
1038 log(LOG_ERR
, "%s%d duplicate IP address %s sent from address %s\n",
1039 ifp
->if_name
, ifp
->if_unit
,
1040 inet_ntop(AF_INET
, &sender_ip
->sin_addr
, ipv4str
, sizeof(ipv4str
)),
1041 sdl_addr_to_hex(sender_hw
, buf
, sizeof(buf
)));
1043 /* Send a kernel event so anyone can learn of the conflict */
1044 in_collision
->link_data
.if_family
= ifp
->if_family
;
1045 in_collision
->link_data
.if_unit
= ifp
->if_unit
;
1046 strncpy(&in_collision
->link_data
.if_name
[0], ifp
->if_name
, IFNAMSIZ
);
1047 in_collision
->ia_ipaddr
= sender_ip
->sin_addr
;
1048 in_collision
->hw_len
= sender_hw
->sdl_alen
< MAX_HW_LEN
? sender_hw
->sdl_alen
: MAX_HW_LEN
;
1049 bcopy(CONST_LLADDR(sender_hw
), (caddr_t
)in_collision
->hw_addr
, in_collision
->hw_len
);
1050 ev_msg
.vendor_code
= KEV_VENDOR_APPLE
;
1051 ev_msg
.kev_class
= KEV_NETWORK_CLASS
;
1052 ev_msg
.kev_subclass
= KEV_INET_SUBCLASS
;
1053 ev_msg
.event_code
= KEV_INET_ARPCOLLISION
;
1054 ev_msg
.dv
[0].data_ptr
= in_collision
;
1055 ev_msg
.dv
[0].data_length
= sizeof(struct kev_in_collision
) + in_collision
->hw_len
;
1056 ev_msg
.dv
[1].data_length
= 0;
1057 kev_post_msg(&ev_msg
);
1063 * Look up the routing entry. If it doesn't exist and we are the
1064 * target, and the sender isn't 0.0.0.0, go ahead and create one.
1065 * Callee holds a reference on the route and returns with the route
1066 * entry locked, upon success.
1068 error
= arp_lookup_route(&sender_ip
->sin_addr
,
1069 (target_ip
->sin_addr
.s_addr
== best_ia
->ia_addr
.sin_addr
.s_addr
&&
1070 sender_ip
->sin_addr
.s_addr
!= 0), 0, &route
, ifp
->if_index
);
1073 RT_LOCK_ASSERT_HELD(route
);
1075 if (error
|| route
== 0 || route
->rt_gateway
== 0) {
1076 if (arpop
!= ARPOP_REQUEST
) {
1079 if (arp_sendllconflict
1080 && send_conflicting_probes
!= 0
1081 && (ifp
->if_eflags
& IFEF_ARPLL
) != 0
1082 && IN_LINKLOCAL(ntohl(target_ip
->sin_addr
.s_addr
))
1083 && sender_ip
->sin_addr
.s_addr
== 0) {
1085 * Verify this ARP probe doesn't conflict with an IPv4LL we know of
1086 * on another interface.
1088 if (route
!= NULL
) {
1089 RT_REMREF_LOCKED(route
);
1094 * Callee holds a reference on the route and returns
1095 * with the route entry locked, upon success.
1097 error
= arp_lookup_route(&target_ip
->sin_addr
, 0, 0,
1098 &route
, ifp
->if_index
);
1101 RT_LOCK_ASSERT_HELD(route
);
1103 if (error
== 0 && route
&& route
->rt_gateway
) {
1104 gateway
= SDL(route
->rt_gateway
);
1105 if (route
->rt_ifp
!= ifp
&& gateway
->sdl_alen
!= 0
1106 && (gateway
->sdl_alen
!= sender_hw
->sdl_alen
1107 || bcmp(CONST_LLADDR(gateway
), CONST_LLADDR(sender_hw
),
1108 gateway
->sdl_alen
) != 0)) {
1110 * A node is probing for an IPv4LL we know exists on a
1111 * different interface. We respond with a conflicting probe
1112 * to force the new device to pick a different IPv4LL
1115 if (log_arp_warnings
) {
1117 "arp: %s on %s%d sent probe for %s, already on %s%d\n",
1118 sdl_addr_to_hex(sender_hw
, buf
, sizeof(buf
)),
1119 ifp
->if_name
, ifp
->if_unit
,
1120 inet_ntop(AF_INET
, &target_ip
->sin_addr
, ipv4str
,
1122 route
->rt_ifp
->if_name
, route
->rt_ifp
->if_unit
);
1124 "arp: sending conflicting probe to %s on %s%d\n",
1125 sdl_addr_to_hex(sender_hw
, buf
, sizeof(buf
)),
1126 ifp
->if_name
, ifp
->if_unit
);
1128 /* We're done with the route */
1129 RT_REMREF_LOCKED(route
);
1133 * Send a conservative unicast "ARP probe".
1134 * This should force the other device to pick a new number.
1135 * This will not force the device to pick a new number if the device
1136 * has already assigned that number.
1137 * This will not imply to the device that we own that address.
1139 ifnet_lock_shared(ifp
);
1140 ifa
= TAILQ_FIRST(&ifp
->if_addrhead
);
1143 ifnet_lock_done(ifp
);
1144 dlil_send_arp_internal(ifp
, ARPOP_REQUEST
,
1145 ifa
!= NULL
? SDL(ifa
->ifa_addr
) : NULL
,
1146 (const struct sockaddr
*)sender_ip
, sender_hw
,
1147 (const struct sockaddr
*)target_ip
);
1155 } else if (keep_announcements
!= 0
1156 && target_ip
->sin_addr
.s_addr
== sender_ip
->sin_addr
.s_addr
) {
1157 /* don't create entry if link-local address and link-local is disabled */
1158 if (!IN_LINKLOCAL(ntohl(sender_ip
->sin_addr
.s_addr
))
1159 || (ifp
->if_eflags
& IFEF_ARPLL
) != 0) {
1160 if (route
!= NULL
) {
1161 RT_REMREF_LOCKED(route
);
1166 * Callee holds a reference on the route and
1167 * returns with the route entry locked, upon
1170 error
= arp_lookup_route(&sender_ip
->sin_addr
,
1171 1, 0, &route
, ifp
->if_index
);
1174 RT_LOCK_ASSERT_HELD(route
);
1176 if (error
== 0 && route
!= NULL
&& route
->rt_gateway
!= NULL
) {
1177 created_announcement
= 1;
1180 if (created_announcement
== 0) {
1188 RT_LOCK_ASSERT_HELD(route
);
1189 gateway
= SDL(route
->rt_gateway
);
1190 if (!bridged
&& route
->rt_ifp
!= ifp
) {
1191 if (!IN_LINKLOCAL(ntohl(sender_ip
->sin_addr
.s_addr
)) || (ifp
->if_eflags
& IFEF_ARPLL
) == 0) {
1192 if (log_arp_warnings
)
1193 log(LOG_ERR
, "arp: %s is on %s%d but got reply from %s on %s%d\n",
1194 inet_ntop(AF_INET
, &sender_ip
->sin_addr
, ipv4str
,
1196 route
->rt_ifp
->if_name
,
1197 route
->rt_ifp
->if_unit
,
1198 sdl_addr_to_hex(sender_hw
, buf
, sizeof(buf
)),
1199 ifp
->if_name
, ifp
->if_unit
);
1203 /* Don't change a permanent address */
1204 if (route
->rt_rmx
.rmx_expire
== 0) {
1209 * We're about to check and/or change the route's ifp
1210 * and ifa, so do the lock dance: drop rt_lock, hold
1211 * rnh_lock and re-hold rt_lock to avoid violating the
1212 * lock ordering. We have an extra reference on the
1213 * route, so it won't go away while we do this.
1216 lck_mtx_lock(rnh_lock
);
1219 * Don't change the cloned route away from the
1220 * parent's interface if the address did resolve
1221 * or if the route is defunct. rt_ifp on both
1222 * the parent and the clone can now be freely
1223 * accessed now that we have acquired rnh_lock.
1225 gateway
= SDL(route
->rt_gateway
);
1226 if ((gateway
->sdl_alen
!= 0 && route
->rt_parent
&&
1227 route
->rt_parent
->rt_ifp
== route
->rt_ifp
) ||
1228 (route
->rt_flags
& RTF_CONDEMNED
)) {
1229 RT_REMREF_LOCKED(route
);
1232 lck_mtx_unlock(rnh_lock
);
1235 /* Change the interface when the existing route is on */
1236 route
->rt_ifp
= ifp
;
1237 rtsetifa(route
, &best_ia
->ia_ifa
);
1238 gateway
->sdl_index
= ifp
->if_index
;
1240 lck_mtx_unlock(rnh_lock
);
1242 /* Don't bother if the route is down */
1243 if (!(route
->rt_flags
& RTF_UP
))
1245 /* Refresh gateway pointer */
1246 gateway
= SDL(route
->rt_gateway
);
1248 RT_LOCK_ASSERT_HELD(route
);
1251 if (gateway
->sdl_alen
&& bcmp(LLADDR(gateway
), CONST_LLADDR(sender_hw
), gateway
->sdl_alen
)) {
1252 if (route
->rt_rmx
.rmx_expire
&& log_arp_warnings
) {
1253 char buf2
[3 * MAX_HW_LEN
];
1254 log(LOG_INFO
, "arp: %s moved from %s to %s on %s%d\n",
1255 inet_ntop(AF_INET
, &sender_ip
->sin_addr
, ipv4str
,
1257 sdl_addr_to_hex(gateway
, buf
, sizeof(buf
)),
1258 sdl_addr_to_hex(sender_hw
, buf2
, sizeof(buf2
)),
1259 ifp
->if_name
, ifp
->if_unit
);
1261 else if (route
->rt_rmx
.rmx_expire
== 0) {
1262 if (log_arp_warnings
) {
1263 log(LOG_ERR
, "arp: %s attempts to modify "
1264 "permanent entry for %s on %s%d\n",
1265 sdl_addr_to_hex(sender_hw
, buf
,
1267 inet_ntop(AF_INET
, &sender_ip
->sin_addr
,
1268 ipv4str
, sizeof(ipv4str
)),
1269 ifp
->if_name
, ifp
->if_unit
);
1275 /* Copy the sender hardware address in to the route's gateway address */
1276 gateway
->sdl_alen
= sender_hw
->sdl_alen
;
1277 bcopy(CONST_LLADDR(sender_hw
), LLADDR(gateway
), gateway
->sdl_alen
);
1279 /* Update the expire time for the route and clear the reject flag */
1280 if (route
->rt_rmx
.rmx_expire
) {
1281 struct timeval timenow
;
1283 getmicrotime(&timenow
);
1284 route
->rt_rmx
.rmx_expire
= timenow
.tv_sec
+ arpt_keep
;
1286 route
->rt_flags
&= ~RTF_REJECT
;
1288 /* update the llinfo, send a queued packet if there is one */
1289 llinfo
= route
->rt_llinfo
;
1290 llinfo
->la_asked
= 0;
1291 if (llinfo
->la_hold
) {
1293 m0
= llinfo
->la_hold
;
1294 llinfo
->la_hold
= 0;
1297 dlil_output(ifp
, PF_INET
, m0
, (caddr_t
)route
, rt_key(route
), 0);
1303 if (route
!= NULL
) {
1304 RT_REMREF_LOCKED(route
);
1309 if (arpop
!= ARPOP_REQUEST
)
1312 /* If we are not the target, check if we should proxy */
1313 if (target_ip
->sin_addr
.s_addr
!= best_ia
->ia_addr
.sin_addr
.s_addr
) {
1315 * Find a proxy route; callee holds a reference on the
1316 * route and returns with the route entry locked, upon
1319 error
= arp_lookup_route(&target_ip
->sin_addr
, 0, SIN_PROXY
,
1320 &route
, ifp
->if_index
);
1323 RT_LOCK_ASSERT_HELD(route
);
1325 * Return proxied ARP replies only on the interface
1326 * or bridge cluster where this network resides.
1327 * Otherwise we may conflict with the host we are
1330 if (route
->rt_ifp
!= ifp
&&
1331 (route
->rt_ifp
->if_bridge
!= ifp
->if_bridge
||
1332 ifp
->if_bridge
== NULL
)) {
1333 RT_REMREF_LOCKED(route
);
1337 proxied
= *SDL(route
->rt_gateway
);
1338 target_hw
= &proxied
;
1341 * We don't have a route entry indicating we should
1342 * use proxy. If we aren't supposed to proxy all,
1349 * See if we have a route to the target ip before
1352 route
= rtalloc1_scoped((struct sockaddr
*)
1353 (size_t)target_ip
, 0, 0, ifp
->if_index
);
1358 * Don't proxy for hosts already on the same interface.
1361 if (route
->rt_ifp
== ifp
) {
1367 RT_REMREF_LOCKED(route
);
1371 dlil_send_arp(ifp
, ARPOP_REPLY
,
1372 target_hw
, (const struct sockaddr
*)target_ip
,
1373 sender_hw
, (const struct sockaddr
*)sender_ip
);
1376 if (best_ia
!= NULL
)
1377 ifafree(&best_ia
->ia_ifa
);
1386 ifa
->ifa_rtrequest
= arp_rtrequest
;
1387 ifa
->ifa_flags
|= RTF_CLONING
;
1388 dlil_send_arp(ifp
, ARPOP_REQUEST
, NULL
, ifa
->ifa_addr
, NULL
, ifa
->ifa_addr
);