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 in_arpdrain(void *ignored_arg
)
282 #pragma unused (ignored_arg)
283 struct llinfo_arp
*la
, *ola
;
284 struct timeval timenow
;
286 lck_mtx_lock(rnh_lock
);
287 la
= llinfo_arp
.lh_first
;
288 getmicrotime(&timenow
);
289 while ((ola
= la
) != 0) {
290 struct rtentry
*rt
= la
->la_rt
;
291 la
= la
->la_le
.le_next
;
293 if (rt
->rt_expire
&& rt
->rt_expire
<= timenow
.tv_sec
)
294 arptfree(ola
); /* timer has expired, clear */
298 lck_mtx_unlock(rnh_lock
);
302 * Timeout routine. Age arp_tab entries periodically.
306 arptimer(void *ignored_arg
)
308 #pragma unused (ignored_arg)
310 timeout(arptimer
, (caddr_t
)0, arpt_prune
* hz
);
314 * Parallel to llc_rtrequest.
320 __unused
struct sockaddr
*sa
)
322 struct sockaddr
*gate
= rt
->rt_gateway
;
323 struct llinfo_arp
*la
= rt
->rt_llinfo
;
324 static struct sockaddr_dl null_sdl
= {sizeof(null_sdl
), AF_LINK
, 0, 0, 0, 0, 0, {0}};
325 struct timeval timenow
;
328 panic("%s: ARP has not been initialized", __func__
);
331 lck_mtx_assert(rnh_lock
, LCK_MTX_ASSERT_OWNED
);
332 RT_LOCK_ASSERT_HELD(rt
);
334 if (rt
->rt_flags
& RTF_GATEWAY
)
336 getmicrotime(&timenow
);
341 * XXX: If this is a manually added route to interface
342 * such as older version of routed or gated might provide,
343 * restore cloning bit.
345 if ((rt
->rt_flags
& RTF_HOST
) == 0 &&
346 SIN(rt_mask(rt
))->sin_addr
.s_addr
!= 0xffffffff)
347 rt
->rt_flags
|= RTF_CLONING
;
348 if (rt
->rt_flags
& RTF_CLONING
) {
350 * Case 1: This route should come from a route to iface.
352 if (rt_setgate(rt
, rt_key(rt
),
353 (struct sockaddr
*)&null_sdl
) == 0) {
354 gate
= rt
->rt_gateway
;
355 SDL(gate
)->sdl_type
= rt
->rt_ifp
->if_type
;
356 SDL(gate
)->sdl_index
= rt
->rt_ifp
->if_index
;
358 * In case we're called before 1.0 sec.
361 rt
->rt_expire
= MAX(timenow
.tv_sec
, 1);
365 /* Announce a new entry if requested. */
366 if (rt
->rt_flags
& RTF_ANNOUNCE
) {
368 dlil_send_arp(rt
->rt_ifp
, ARPOP_REQUEST
,
369 SDL(gate
), rt_key(rt
), NULL
, rt_key(rt
));
374 if (gate
->sa_family
!= AF_LINK
||
375 gate
->sa_len
< sizeof(null_sdl
)) {
376 if (log_arp_warnings
)
377 log(LOG_DEBUG
, "arp_rtrequest: bad gateway value\n");
380 SDL(gate
)->sdl_type
= rt
->rt_ifp
->if_type
;
381 SDL(gate
)->sdl_index
= rt
->rt_ifp
->if_index
;
383 break; /* This happens on a route change */
385 * Case 2: This route may come from cloning, or a manual route
386 * add with a LL address.
388 rt
->rt_llinfo
= la
= arp_llinfo_alloc();
390 if (log_arp_warnings
)
391 log(LOG_DEBUG
, "%s: malloc failed\n", __func__
);
394 rt
->rt_llinfo_free
= arp_llinfo_free
;
396 arp_inuse
++, arp_allocated
++;
397 Bzero(la
, sizeof(*la
));
399 rt
->rt_flags
|= RTF_LLINFO
;
400 LIST_INSERT_HEAD(&llinfo_arp
, la
, la_le
);
403 * This keeps the multicast addresses from showing up
404 * in `arp -a' listings as unresolved. It's not actually
405 * functional. Then the same for broadcast.
407 if (IN_MULTICAST(ntohl(SIN(rt_key(rt
))->sin_addr
.s_addr
))) {
409 dlil_resolve_multi(rt
->rt_ifp
, rt_key(rt
), gate
,
410 sizeof(struct sockaddr_dl
));
414 else if (in_broadcast(SIN(rt_key(rt
))->sin_addr
, rt
->rt_ifp
)) {
415 struct sockaddr_dl
*gate_ll
= SDL(gate
);
416 size_t broadcast_len
;
417 ifnet_llbroadcast_copy_bytes(rt
->rt_ifp
,
418 LLADDR(gate_ll
), sizeof(gate_ll
->sdl_data
),
420 gate_ll
->sdl_alen
= broadcast_len
;
421 gate_ll
->sdl_family
= AF_LINK
;
422 gate_ll
->sdl_len
= sizeof(struct sockaddr_dl
);
423 /* In case we're called before 1.0 sec. has elapsed */
424 rt
->rt_expire
= MAX(timenow
.tv_sec
, 1);
427 if (SIN(rt_key(rt
))->sin_addr
.s_addr
==
428 (IA_SIN(rt
->rt_ifa
))->sin_addr
.s_addr
) {
430 * This test used to be
431 * if (loif.if_flags & IFF_UP)
432 * It allowed local traffic to be forced
433 * through the hardware by configuring the loopback down.
434 * However, it causes problems during network configuration
435 * for boards that can't receive packets they send.
436 * It is now necessary to clear "useloopback" and remove
437 * the route to force traffic out to the hardware.
440 ifnet_lladdr_copy_bytes(rt
->rt_ifp
, LLADDR(SDL(gate
)), SDL(gate
)->sdl_alen
= 6);
442 #if IFNET_ROUTE_REFCNT
443 /* Adjust route ref count for the interfaces */
444 if (rt
->rt_if_ref_fn
!= NULL
&&
445 rt
->rt_ifp
!= lo_ifp
) {
446 rt
->rt_if_ref_fn(lo_ifp
, 1);
447 rt
->rt_if_ref_fn(rt
->rt_ifp
, -1);
449 #endif /* IFNET_ROUTE_REFCNT */
461 * Unchain it but defer the actual freeing until the route
462 * itself is to be freed. rt->rt_llinfo still points to
463 * llinfo_arp, and likewise, la->la_rt still points to this
464 * route entry, except that RTF_LLINFO is now cleared.
466 LIST_REMOVE(la
, la_le
);
467 la
->la_le
.le_next
= NULL
;
468 la
->la_le
.le_prev
= NULL
;
469 rt
->rt_flags
&= ~RTF_LLINFO
;
470 if (la
->la_hold
!= NULL
)
471 m_freem(la
->la_hold
);
477 * convert hardware address to hex string for logging errors.
480 sdl_addr_to_hex(const struct sockaddr_dl
*sdl
, char * orig_buf
, int buflen
)
482 char * buf
= orig_buf
;
484 const u_char
* lladdr
= (u_char
*)(size_t)sdl
->sdl_data
;
485 int maxbytes
= buflen
/ 3;
487 if (maxbytes
> sdl
->sdl_alen
) {
488 maxbytes
= sdl
->sdl_alen
;
491 for (i
= 0; i
< maxbytes
; i
++) {
492 snprintf(buf
, 3, "%02x", lladdr
[i
]);
494 *buf
= (i
== maxbytes
- 1) ? '\0' : ':';
501 * arp_lookup_route will lookup the route for a given address.
503 * The address must be for a host on a local network on this interface.
504 * If the returned route is non-NULL, the route is locked and the caller
505 * is responsible for unlocking it and releasing its reference.
508 arp_lookup_route(const struct in_addr
*addr
, int create
, int proxy
,
509 route_t
*route
, unsigned int ifscope
)
511 struct sockaddr_inarp sin
= {sizeof(sin
), AF_INET
, 0, {0}, {0}, 0, 0};
512 const char *why
= NULL
;
518 sin
.sin_addr
.s_addr
= addr
->s_addr
;
519 sin
.sin_other
= proxy
? SIN_PROXY
: 0;
521 rt
= rtalloc1_scoped((struct sockaddr
*)&sin
, create
, 0, ifscope
);
523 return (ENETUNREACH
);
527 if (rt
->rt_flags
& RTF_GATEWAY
) {
528 why
= "host is not on local network";
530 } else if (!(rt
->rt_flags
& RTF_LLINFO
)) {
531 why
= "could not allocate llinfo";
533 } else if (rt
->rt_gateway
->sa_family
!= AF_LINK
) {
534 why
= "gateway route is not ours";
535 error
= EPROTONOSUPPORT
;
539 if (create
&& log_arp_warnings
) {
540 char tmp
[MAX_IPv4_STR_LEN
];
541 log(LOG_DEBUG
, "arplookup link#%d %s failed: %s\n",
542 ifscope
, inet_ntop(AF_INET
, addr
, tmp
,
547 * If there are no references to this route, and it is
548 * a cloned route, and not static, and ARP had created
549 * the route, then purge it from the routing table as
550 * it is probably bogus.
552 if (rt
->rt_refcnt
== 1 &&
553 (rt
->rt_flags
& (RTF_WASCLONED
| RTF_STATIC
)) ==
556 * Prevent another thread from modiying rt_key,
557 * rt_gateway via rt_setgate() after rt_lock is
558 * dropped by marking the route as defunct.
560 rt
->rt_flags
|= RTF_CONDEMNED
;
562 rtrequest(RTM_DELETE
, rt_key(rt
), rt
->rt_gateway
,
563 rt_mask(rt
), rt
->rt_flags
, 0);
566 RT_REMREF_LOCKED(rt
);
573 * Caller releases reference and does RT_UNLOCK(rt).
580 * arp_route_to_gateway_route will find the gateway route for a given route.
582 * If the route is down, look the route up again.
583 * If the route goes through a gateway, get the route to the gateway.
584 * If the gateway route is down, look it up again.
585 * If the route is set to reject, verify it hasn't expired.
587 * If the returned route is non-NULL, the caller is responsible for
588 * releasing the reference and unlocking the route.
590 #define senderr(e) { error = (e); goto bad; }
591 __private_extern__ errno_t
592 arp_route_to_gateway_route(const struct sockaddr
*net_dest
, route_t hint0
,
595 struct timeval timenow
;
596 route_t rt
= hint0
, hint
= hint0
;
602 * Next hop determination. Because we may involve the gateway route
603 * in addition to the original route, locking is rather complicated.
604 * The general concept is that regardless of whether the route points
605 * to the original route or to the gateway route, this routine takes
606 * an extra reference on such a route. This extra reference will be
607 * released at the end.
609 * Care must be taken to ensure that the "hint0" route never gets freed
610 * via rtfree(), since the caller may have stored it inside a struct
611 * route with a reference held for that placeholder.
614 unsigned int ifindex
;
617 ifindex
= rt
->rt_ifp
->if_index
;
618 RT_ADDREF_LOCKED(rt
);
619 if (!(rt
->rt_flags
& RTF_UP
)) {
620 RT_REMREF_LOCKED(rt
);
622 /* route is down, find a new one */
623 hint
= rt
= rtalloc1_scoped((struct sockaddr
*)
624 (size_t)net_dest
, 1, 0, ifindex
);
627 ifindex
= rt
->rt_ifp
->if_index
;
629 senderr(EHOSTUNREACH
);
634 * We have a reference to "rt" by now; it will either
635 * be released or freed at the end of this routine.
637 RT_LOCK_ASSERT_HELD(rt
);
638 if (rt
->rt_flags
& RTF_GATEWAY
) {
639 struct rtentry
*gwrt
= rt
->rt_gwroute
;
640 struct sockaddr_in gw
;
642 /* If there's no gateway rt, look it up */
644 gw
= *((struct sockaddr_in
*)rt
->rt_gateway
);
648 /* Become a regular mutex */
652 * Take gwrt's lock while holding route's lock;
653 * this is okay since gwrt never points back
654 * to "rt", so no lock ordering issues.
657 if (!(gwrt
->rt_flags
& RTF_UP
)) {
658 struct rtentry
*ogwrt
;
660 rt
->rt_gwroute
= NULL
;
662 gw
= *((struct sockaddr_in
*)rt
->rt_gateway
);
666 gwrt
= rtalloc1_scoped(
667 (struct sockaddr
*)&gw
, 1, 0, ifindex
);
671 * Bail out if the route is down, no route
672 * to gateway, circular route, or if the
673 * gateway portion of "rt" has changed.
675 if (!(rt
->rt_flags
& RTF_UP
) ||
676 gwrt
== NULL
|| gwrt
== rt
||
677 !equal(SA(&gw
), rt
->rt_gateway
)) {
679 RT_REMREF_LOCKED(gwrt
);
685 senderr(EHOSTUNREACH
);
688 /* Remove any existing gwrt */
689 ogwrt
= rt
->rt_gwroute
;
690 if ((rt
->rt_gwroute
= gwrt
) != NULL
)
693 /* Clean up "rt" now while we can */
695 RT_REMREF_LOCKED(rt
);
702 /* Now free the replaced gwrt */
705 /* If still no route to gateway, bail out */
707 senderr(EHOSTUNREACH
);
709 RT_ADDREF_LOCKED(gwrt
);
711 /* Clean up "rt" now while we can */
713 RT_REMREF_LOCKED(rt
);
722 /* rt == gwrt; if it is now down, give up */
724 if (!(rt
->rt_flags
& RTF_UP
)) {
726 senderr(EHOSTUNREACH
);
730 if (rt
->rt_flags
& RTF_REJECT
) {
731 getmicrotime(&timenow
);
732 if (rt
->rt_rmx
.rmx_expire
== 0 ||
733 timenow
.tv_sec
< rt
->rt_rmx
.rmx_expire
) {
735 senderr(rt
== hint
? EHOSTDOWN
: EHOSTUNREACH
);
739 /* Become a regular mutex */
742 /* Caller is responsible for cleaning up "rt" */
748 /* Clean up route (either it is "rt" or "gwrt") */
752 RT_REMREF_LOCKED(rt
);
764 * This is the ARP pre-output routine; care must be taken to ensure that
765 * the "hint" route never gets freed via rtfree(), since the caller may
766 * have stored it inside a struct route with a reference held for that
770 arp_lookup_ip(ifnet_t ifp
, const struct sockaddr_in
*net_dest
,
771 struct sockaddr_dl
*ll_dest
, size_t ll_dest_len
, route_t hint
,
774 route_t route
= NULL
; /* output route */
776 struct sockaddr_dl
*gateway
;
777 struct llinfo_arp
*llinfo
;
778 struct timeval timenow
;
780 if (net_dest
->sin_family
!= AF_INET
)
781 return (EAFNOSUPPORT
);
783 if ((ifp
->if_flags
& (IFF_UP
|IFF_RUNNING
)) != (IFF_UP
|IFF_RUNNING
))
787 * If we were given a route, verify the route and grab the gateway
791 * Callee holds a reference on the route and returns
792 * with the route entry locked, upon success.
794 result
= arp_route_to_gateway_route((const struct sockaddr
*)
795 net_dest
, hint
, &route
);
799 RT_LOCK_ASSERT_HELD(route
);
802 if (packet
->m_flags
& M_BCAST
) {
803 size_t broadcast_len
;
804 bzero(ll_dest
, ll_dest_len
);
805 result
= ifnet_llbroadcast_copy_bytes(ifp
, LLADDR(ll_dest
),
806 ll_dest_len
- offsetof(struct sockaddr_dl
, sdl_data
),
809 ll_dest
->sdl_alen
= broadcast_len
;
810 ll_dest
->sdl_family
= AF_LINK
;
811 ll_dest
->sdl_len
= sizeof(struct sockaddr_dl
);
815 if (packet
->m_flags
& M_MCAST
) {
818 result
= dlil_resolve_multi(ifp
,
819 (const struct sockaddr
*)net_dest
,
820 (struct sockaddr
*)ll_dest
, ll_dest_len
);
827 * If we didn't find a route, or the route doesn't have
828 * link layer information, trigger the creation of the
829 * route and link layer information.
831 if (route
== NULL
|| route
->rt_llinfo
== NULL
) {
832 /* Clean up now while we can */
835 RT_REMREF_LOCKED(route
);
843 * Callee holds a reference on the route and returns
844 * with the route entry locked, upon success.
846 result
= arp_lookup_route(&net_dest
->sin_addr
, 1, 0, &route
,
849 RT_LOCK_ASSERT_HELD(route
);
852 if (result
|| route
== NULL
|| route
->rt_llinfo
== NULL
) {
853 char tmp
[MAX_IPv4_STR_LEN
];
855 /* In case result is 0 but no route, return an error */
857 result
= EHOSTUNREACH
;
859 if (log_arp_warnings
&&
860 route
!= NULL
&& route
->rt_llinfo
== NULL
)
861 log(LOG_DEBUG
, "arpresolve: can't allocate llinfo "
862 "for %s\n", inet_ntop(AF_INET
, &net_dest
->sin_addr
,
868 * Now that we have the right route, is it filled in?
870 gateway
= SDL(route
->rt_gateway
);
871 getmicrotime(&timenow
);
872 if ((route
->rt_rmx
.rmx_expire
== 0 ||
873 route
->rt_rmx
.rmx_expire
> timenow
.tv_sec
) && gateway
!= NULL
&&
874 gateway
->sdl_family
== AF_LINK
&& gateway
->sdl_alen
!= 0) {
875 bcopy(gateway
, ll_dest
, MIN(gateway
->sdl_len
, ll_dest_len
));
880 if (ifp
->if_flags
& IFF_NOARP
) {
886 * Route wasn't complete/valid. We need to arp.
888 llinfo
= route
->rt_llinfo
;
889 if (packet
!= NULL
) {
890 if (llinfo
->la_hold
!= NULL
)
891 m_freem(llinfo
->la_hold
);
892 llinfo
->la_hold
= packet
;
895 if (route
->rt_rmx
.rmx_expire
) {
896 route
->rt_flags
&= ~RTF_REJECT
;
897 if (llinfo
->la_asked
== 0 ||
898 route
->rt_rmx
.rmx_expire
!= timenow
.tv_sec
) {
899 route
->rt_rmx
.rmx_expire
= timenow
.tv_sec
;
900 if (llinfo
->la_asked
++ < arp_maxtries
) {
901 struct ifaddr
*rt_ifa
= route
->rt_ifa
;
904 dlil_send_arp(ifp
, ARPOP_REQUEST
, NULL
,
905 rt_ifa
->ifa_addr
, NULL
,
906 (const struct sockaddr
*)net_dest
);
909 result
= EJUSTRETURN
;
912 route
->rt_flags
|= RTF_REJECT
;
913 route
->rt_rmx
.rmx_expire
= rt_expiry(route
,
914 route
->rt_rmx
.rmx_expire
, arpt_down
);
915 llinfo
->la_asked
= 0;
916 llinfo
->la_hold
= NULL
;
917 result
= EHOSTUNREACH
;
923 /* The packet is now held inside la_hold (can "packet" be NULL?) */
924 result
= EJUSTRETURN
;
929 RT_REMREF_LOCKED(route
);
943 const struct sockaddr_dl
*sender_hw
,
944 const struct sockaddr_in
*sender_ip
,
945 const struct sockaddr_in
*target_ip
)
947 char ipv4str
[MAX_IPv4_STR_LEN
];
948 struct sockaddr_dl proxied
;
949 struct sockaddr_dl
*gateway
, *target_hw
= NULL
;
951 struct in_ifaddr
*ia
;
952 struct in_ifaddr
*best_ia
= NULL
;
953 route_t route
= NULL
;
954 char buf
[3 * MAX_HW_LEN
]; // enough for MAX_HW_LEN byte hw address
955 struct llinfo_arp
*llinfo
;
957 int created_announcement
= 0;
958 int bridged
= 0, is_bridge
= 0;
960 /* Do not respond to requests for 0.0.0.0 */
961 if (target_ip
->sin_addr
.s_addr
== 0 && arpop
== ARPOP_REQUEST
)
966 if (ifp
->if_type
== IFT_BRIDGE
)
970 * Determine if this ARP is for us
971 * For a bridge, we want to check the address irrespective
972 * of the receive interface.
974 lck_rw_lock_shared(in_ifaddr_rwlock
);
975 TAILQ_FOREACH(ia
, INADDR_HASH(target_ip
->sin_addr
.s_addr
), ia_hash
) {
976 if (((bridged
&& ia
->ia_ifp
->if_bridge
!= NULL
) ||
977 (ia
->ia_ifp
== ifp
)) &&
978 ia
->ia_addr
.sin_addr
.s_addr
== target_ip
->sin_addr
.s_addr
) {
980 ifaref(&best_ia
->ia_ifa
);
981 lck_rw_done(in_ifaddr_rwlock
);
986 TAILQ_FOREACH(ia
, INADDR_HASH(sender_ip
->sin_addr
.s_addr
), ia_hash
) {
987 if (((bridged
&& ia
->ia_ifp
->if_bridge
!= NULL
) ||
988 (ia
->ia_ifp
== ifp
)) &&
989 ia
->ia_addr
.sin_addr
.s_addr
== sender_ip
->sin_addr
.s_addr
) {
991 ifaref(&best_ia
->ia_ifa
);
992 lck_rw_done(in_ifaddr_rwlock
);
997 #define BDG_MEMBER_MATCHES_ARP(addr, ifp, ia) \
998 (ia->ia_ifp->if_bridge == ifp->if_softc && \
999 !bcmp(ifnet_lladdr(ia->ia_ifp), ifnet_lladdr(ifp), ifp->if_addrlen) && \
1000 addr == ia->ia_addr.sin_addr.s_addr)
1002 * Check the case when bridge shares its MAC address with
1003 * some of its children, so packets are claimed by bridge
1004 * itself (bridge_input() does it first), but they are really
1005 * meant to be destined to the bridge member.
1008 TAILQ_FOREACH(ia
, INADDR_HASH(target_ip
->sin_addr
.s_addr
), ia_hash
) {
1009 if (BDG_MEMBER_MATCHES_ARP(target_ip
->sin_addr
.s_addr
, ifp
, ia
)) {
1012 ifaref(&best_ia
->ia_ifa
);
1013 lck_rw_done(in_ifaddr_rwlock
);
1018 lck_rw_done(in_ifaddr_rwlock
);
1021 * No match, use the first inet address on the receive interface
1022 * as a dummy address for the rest of the function; we may be
1023 * proxying for another address.
1025 ifnet_lock_shared(ifp
);
1026 TAILQ_FOREACH(ifa
, &ifp
->if_addrhead
, ifa_link
) {
1027 if (ifa
->ifa_addr
->sa_family
!= AF_INET
)
1029 best_ia
= (struct in_ifaddr
*)ifa
;
1030 ifaref(&best_ia
->ia_ifa
);
1031 ifnet_lock_done(ifp
);
1034 ifnet_lock_done(ifp
);
1037 * If we're not a bridge member, or if we are but there's no
1038 * IPv4 address to use for the interface, drop the packet.
1040 if (!bridged
|| best_ia
== NULL
)
1044 /* If the packet is from this interface, ignore the packet */
1045 if (!bcmp(CONST_LLADDR(sender_hw
), ifnet_lladdr(ifp
), sender_hw
->sdl_len
)) {
1049 /* Check for a conflict */
1050 if (!bridged
&& sender_ip
->sin_addr
.s_addr
== best_ia
->ia_addr
.sin_addr
.s_addr
) {
1051 struct kev_msg ev_msg
;
1052 struct kev_in_collision
*in_collision
;
1053 u_char storage
[sizeof(struct kev_in_collision
) + MAX_HW_LEN
];
1054 in_collision
= (struct kev_in_collision
*)storage
;
1055 log(LOG_ERR
, "%s%d duplicate IP address %s sent from address %s\n",
1056 ifp
->if_name
, ifp
->if_unit
,
1057 inet_ntop(AF_INET
, &sender_ip
->sin_addr
, ipv4str
, sizeof(ipv4str
)),
1058 sdl_addr_to_hex(sender_hw
, buf
, sizeof(buf
)));
1060 /* Send a kernel event so anyone can learn of the conflict */
1061 in_collision
->link_data
.if_family
= ifp
->if_family
;
1062 in_collision
->link_data
.if_unit
= ifp
->if_unit
;
1063 strncpy(&in_collision
->link_data
.if_name
[0], ifp
->if_name
, IFNAMSIZ
);
1064 in_collision
->ia_ipaddr
= sender_ip
->sin_addr
;
1065 in_collision
->hw_len
= sender_hw
->sdl_alen
< MAX_HW_LEN
? sender_hw
->sdl_alen
: MAX_HW_LEN
;
1066 bcopy(CONST_LLADDR(sender_hw
), (caddr_t
)in_collision
->hw_addr
, in_collision
->hw_len
);
1067 ev_msg
.vendor_code
= KEV_VENDOR_APPLE
;
1068 ev_msg
.kev_class
= KEV_NETWORK_CLASS
;
1069 ev_msg
.kev_subclass
= KEV_INET_SUBCLASS
;
1070 ev_msg
.event_code
= KEV_INET_ARPCOLLISION
;
1071 ev_msg
.dv
[0].data_ptr
= in_collision
;
1072 ev_msg
.dv
[0].data_length
= sizeof(struct kev_in_collision
) + in_collision
->hw_len
;
1073 ev_msg
.dv
[1].data_length
= 0;
1074 kev_post_msg(&ev_msg
);
1080 * Look up the routing entry. If it doesn't exist and we are the
1081 * target, and the sender isn't 0.0.0.0, go ahead and create one.
1082 * Callee holds a reference on the route and returns with the route
1083 * entry locked, upon success.
1085 error
= arp_lookup_route(&sender_ip
->sin_addr
,
1086 (target_ip
->sin_addr
.s_addr
== best_ia
->ia_addr
.sin_addr
.s_addr
&&
1087 sender_ip
->sin_addr
.s_addr
!= 0), 0, &route
, ifp
->if_index
);
1090 RT_LOCK_ASSERT_HELD(route
);
1092 if (error
|| route
== 0 || route
->rt_gateway
== 0) {
1093 if (arpop
!= ARPOP_REQUEST
) {
1096 if (arp_sendllconflict
1097 && send_conflicting_probes
!= 0
1098 && (ifp
->if_eflags
& IFEF_ARPLL
) != 0
1099 && IN_LINKLOCAL(ntohl(target_ip
->sin_addr
.s_addr
))
1100 && sender_ip
->sin_addr
.s_addr
== 0) {
1102 * Verify this ARP probe doesn't conflict with an IPv4LL we know of
1103 * on another interface.
1105 if (route
!= NULL
) {
1106 RT_REMREF_LOCKED(route
);
1111 * Callee holds a reference on the route and returns
1112 * with the route entry locked, upon success.
1114 error
= arp_lookup_route(&target_ip
->sin_addr
, 0, 0,
1115 &route
, ifp
->if_index
);
1118 RT_LOCK_ASSERT_HELD(route
);
1120 if (error
== 0 && route
&& route
->rt_gateway
) {
1121 gateway
= SDL(route
->rt_gateway
);
1122 if (route
->rt_ifp
!= ifp
&& gateway
->sdl_alen
!= 0
1123 && (gateway
->sdl_alen
!= sender_hw
->sdl_alen
1124 || bcmp(CONST_LLADDR(gateway
), CONST_LLADDR(sender_hw
),
1125 gateway
->sdl_alen
) != 0)) {
1127 * A node is probing for an IPv4LL we know exists on a
1128 * different interface. We respond with a conflicting probe
1129 * to force the new device to pick a different IPv4LL
1132 if (log_arp_warnings
) {
1134 "arp: %s on %s%d sent probe for %s, already on %s%d\n",
1135 sdl_addr_to_hex(sender_hw
, buf
, sizeof(buf
)),
1136 ifp
->if_name
, ifp
->if_unit
,
1137 inet_ntop(AF_INET
, &target_ip
->sin_addr
, ipv4str
,
1139 route
->rt_ifp
->if_name
, route
->rt_ifp
->if_unit
);
1141 "arp: sending conflicting probe to %s on %s%d\n",
1142 sdl_addr_to_hex(sender_hw
, buf
, sizeof(buf
)),
1143 ifp
->if_name
, ifp
->if_unit
);
1145 /* We're done with the route */
1146 RT_REMREF_LOCKED(route
);
1150 * Send a conservative unicast "ARP probe".
1151 * This should force the other device to pick a new number.
1152 * This will not force the device to pick a new number if the device
1153 * has already assigned that number.
1154 * This will not imply to the device that we own that address.
1156 ifnet_lock_shared(ifp
);
1157 ifa
= TAILQ_FIRST(&ifp
->if_addrhead
);
1160 ifnet_lock_done(ifp
);
1161 dlil_send_arp_internal(ifp
, ARPOP_REQUEST
,
1162 ifa
!= NULL
? SDL(ifa
->ifa_addr
) : NULL
,
1163 (const struct sockaddr
*)sender_ip
, sender_hw
,
1164 (const struct sockaddr
*)target_ip
);
1172 } else if (keep_announcements
!= 0
1173 && target_ip
->sin_addr
.s_addr
== sender_ip
->sin_addr
.s_addr
) {
1174 /* don't create entry if link-local address and link-local is disabled */
1175 if (!IN_LINKLOCAL(ntohl(sender_ip
->sin_addr
.s_addr
))
1176 || (ifp
->if_eflags
& IFEF_ARPLL
) != 0) {
1177 if (route
!= NULL
) {
1178 RT_REMREF_LOCKED(route
);
1183 * Callee holds a reference on the route and
1184 * returns with the route entry locked, upon
1187 error
= arp_lookup_route(&sender_ip
->sin_addr
,
1188 1, 0, &route
, ifp
->if_index
);
1191 RT_LOCK_ASSERT_HELD(route
);
1193 if (error
== 0 && route
!= NULL
&& route
->rt_gateway
!= NULL
) {
1194 created_announcement
= 1;
1197 if (created_announcement
== 0) {
1205 RT_LOCK_ASSERT_HELD(route
);
1206 gateway
= SDL(route
->rt_gateway
);
1207 if (!bridged
&& route
->rt_ifp
!= ifp
) {
1208 if (!IN_LINKLOCAL(ntohl(sender_ip
->sin_addr
.s_addr
)) || (ifp
->if_eflags
& IFEF_ARPLL
) == 0) {
1209 if (log_arp_warnings
)
1210 log(LOG_ERR
, "arp: %s is on %s%d but got reply from %s on %s%d\n",
1211 inet_ntop(AF_INET
, &sender_ip
->sin_addr
, ipv4str
,
1213 route
->rt_ifp
->if_name
,
1214 route
->rt_ifp
->if_unit
,
1215 sdl_addr_to_hex(sender_hw
, buf
, sizeof(buf
)),
1216 ifp
->if_name
, ifp
->if_unit
);
1220 /* Don't change a permanent address */
1221 if (route
->rt_rmx
.rmx_expire
== 0) {
1226 * We're about to check and/or change the route's ifp
1227 * and ifa, so do the lock dance: drop rt_lock, hold
1228 * rnh_lock and re-hold rt_lock to avoid violating the
1229 * lock ordering. We have an extra reference on the
1230 * route, so it won't go away while we do this.
1233 lck_mtx_lock(rnh_lock
);
1236 * Don't change the cloned route away from the
1237 * parent's interface if the address did resolve
1238 * or if the route is defunct. rt_ifp on both
1239 * the parent and the clone can now be freely
1240 * accessed now that we have acquired rnh_lock.
1242 gateway
= SDL(route
->rt_gateway
);
1243 if ((gateway
->sdl_alen
!= 0 && route
->rt_parent
&&
1244 route
->rt_parent
->rt_ifp
== route
->rt_ifp
) ||
1245 (route
->rt_flags
& RTF_CONDEMNED
)) {
1246 RT_REMREF_LOCKED(route
);
1249 lck_mtx_unlock(rnh_lock
);
1252 #if IFNET_ROUTE_REFCNT
1253 /* Adjust route ref count for the interfaces */
1254 if (route
->rt_if_ref_fn
!= NULL
&&
1255 route
->rt_ifp
!= ifp
) {
1256 route
->rt_if_ref_fn(ifp
, 1);
1257 route
->rt_if_ref_fn(route
->rt_ifp
, -1);
1259 #endif /* IFNET_ROUTE_REFCNT */
1260 /* Change the interface when the existing route is on */
1261 route
->rt_ifp
= ifp
;
1262 rtsetifa(route
, &best_ia
->ia_ifa
);
1263 gateway
->sdl_index
= ifp
->if_index
;
1265 lck_mtx_unlock(rnh_lock
);
1267 /* Don't bother if the route is down */
1268 if (!(route
->rt_flags
& RTF_UP
))
1270 /* Refresh gateway pointer */
1271 gateway
= SDL(route
->rt_gateway
);
1273 RT_LOCK_ASSERT_HELD(route
);
1276 if (gateway
->sdl_alen
&& bcmp(LLADDR(gateway
), CONST_LLADDR(sender_hw
), gateway
->sdl_alen
)) {
1277 if (route
->rt_rmx
.rmx_expire
&& log_arp_warnings
) {
1278 char buf2
[3 * MAX_HW_LEN
];
1279 log(LOG_INFO
, "arp: %s moved from %s to %s on %s%d\n",
1280 inet_ntop(AF_INET
, &sender_ip
->sin_addr
, ipv4str
,
1282 sdl_addr_to_hex(gateway
, buf
, sizeof(buf
)),
1283 sdl_addr_to_hex(sender_hw
, buf2
, sizeof(buf2
)),
1284 ifp
->if_name
, ifp
->if_unit
);
1286 else if (route
->rt_rmx
.rmx_expire
== 0) {
1287 if (log_arp_warnings
) {
1288 log(LOG_ERR
, "arp: %s attempts to modify "
1289 "permanent entry for %s on %s%d\n",
1290 sdl_addr_to_hex(sender_hw
, buf
,
1292 inet_ntop(AF_INET
, &sender_ip
->sin_addr
,
1293 ipv4str
, sizeof(ipv4str
)),
1294 ifp
->if_name
, ifp
->if_unit
);
1300 /* Copy the sender hardware address in to the route's gateway address */
1301 gateway
->sdl_alen
= sender_hw
->sdl_alen
;
1302 bcopy(CONST_LLADDR(sender_hw
), LLADDR(gateway
), gateway
->sdl_alen
);
1304 /* Update the expire time for the route and clear the reject flag */
1305 if (route
->rt_rmx
.rmx_expire
) {
1306 struct timeval timenow
;
1308 getmicrotime(&timenow
);
1309 route
->rt_rmx
.rmx_expire
=
1310 rt_expiry(route
, timenow
.tv_sec
, arpt_keep
);
1312 route
->rt_flags
&= ~RTF_REJECT
;
1314 /* update the llinfo, send a queued packet if there is one */
1315 llinfo
= route
->rt_llinfo
;
1316 llinfo
->la_asked
= 0;
1317 if (llinfo
->la_hold
) {
1319 m0
= llinfo
->la_hold
;
1320 llinfo
->la_hold
= 0;
1323 dlil_output(ifp
, PF_INET
, m0
, (caddr_t
)route
, rt_key(route
), 0);
1329 if (route
!= NULL
) {
1330 RT_REMREF_LOCKED(route
);
1335 if (arpop
!= ARPOP_REQUEST
)
1338 /* If we are not the target, check if we should proxy */
1339 if (target_ip
->sin_addr
.s_addr
!= best_ia
->ia_addr
.sin_addr
.s_addr
) {
1341 * Find a proxy route; callee holds a reference on the
1342 * route and returns with the route entry locked, upon
1345 error
= arp_lookup_route(&target_ip
->sin_addr
, 0, SIN_PROXY
,
1346 &route
, ifp
->if_index
);
1349 RT_LOCK_ASSERT_HELD(route
);
1351 * Return proxied ARP replies only on the interface
1352 * or bridge cluster where this network resides.
1353 * Otherwise we may conflict with the host we are
1356 if (route
->rt_ifp
!= ifp
&&
1357 (route
->rt_ifp
->if_bridge
!= ifp
->if_bridge
||
1358 ifp
->if_bridge
== NULL
)) {
1359 RT_REMREF_LOCKED(route
);
1363 proxied
= *SDL(route
->rt_gateway
);
1364 target_hw
= &proxied
;
1367 * We don't have a route entry indicating we should
1368 * use proxy. If we aren't supposed to proxy all,
1375 * See if we have a route to the target ip before
1378 route
= rtalloc1_scoped((struct sockaddr
*)
1379 (size_t)target_ip
, 0, 0, ifp
->if_index
);
1384 * Don't proxy for hosts already on the same interface.
1387 if (route
->rt_ifp
== ifp
) {
1393 RT_REMREF_LOCKED(route
);
1397 dlil_send_arp(ifp
, ARPOP_REPLY
,
1398 target_hw
, (const struct sockaddr
*)target_ip
,
1399 sender_hw
, (const struct sockaddr
*)sender_ip
);
1402 if (best_ia
!= NULL
)
1403 ifafree(&best_ia
->ia_ifa
);
1412 ifa
->ifa_rtrequest
= arp_rtrequest
;
1413 ifa
->ifa_flags
|= RTF_CLONING
;
1414 dlil_send_arp(ifp
, ARPOP_REQUEST
, NULL
, ifa
->ifa_addr
, NULL
, ifa
->ifa_addr
);