2 * Copyright (c) 2004-2011 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>
73 #include <sys/mcache.h>
74 #include <sys/protosw.h>
76 #include <net/if_arp.h>
77 #include <net/if_dl.h>
79 #include <net/if_types.h>
80 #include <net/if_llreach.h>
81 #include <net/route.h>
82 #include <netinet/if_ether.h>
83 #include <netinet/in_var.h>
84 #include <kern/zalloc.h>
86 #define SA(p) ((struct sockaddr *)(p))
87 #define SIN(s) ((struct sockaddr_in *)s)
88 #define CONST_LLADDR(s) ((const u_char*)((s)->sdl_data + (s)->sdl_nlen))
89 #define equal(a1, a2) (bcmp((caddr_t)(a1), (caddr_t)(a2), (a1)->sa_len) == 0)
91 static const size_t MAX_HW_LEN
= 10;
93 SYSCTL_DECL(_net_link_ether
);
94 SYSCTL_NODE(_net_link_ether
, PF_INET
, inet
, CTLFLAG_RW
|CTLFLAG_LOCKED
, 0, "");
97 static int arpt_prune
= (5*60*1); /* walk list every 5 minutes */
98 static int arpt_keep
= (20*60); /* once resolved, good for 20 more minutes */
99 static int arpt_down
= 20; /* once declared down, don't send for 20 sec */
101 /* Apple Hardware SUM16 checksuming */
102 int apple_hwcksum_tx
= 1;
103 int apple_hwcksum_rx
= 1;
105 static int arp_llreach_base
= (LL_BASE_REACHABLE
/ 1000); /* seconds */
107 SYSCTL_INT(_net_link_ether_inet
, OID_AUTO
, prune_intvl
,
108 CTLFLAG_RW
| CTLFLAG_LOCKED
, &arpt_prune
, 0, "");
110 SYSCTL_INT(_net_link_ether_inet
, OID_AUTO
, max_age
,
111 CTLFLAG_RW
| CTLFLAG_LOCKED
, &arpt_keep
, 0, "");
113 SYSCTL_INT(_net_link_ether_inet
, OID_AUTO
, host_down_time
,
114 CTLFLAG_RW
| CTLFLAG_LOCKED
, &arpt_down
, 0, "");
116 SYSCTL_INT(_net_link_ether_inet
, OID_AUTO
, apple_hwcksum_tx
,
117 CTLFLAG_RW
| CTLFLAG_LOCKED
, &apple_hwcksum_tx
, 0, "");
119 SYSCTL_INT(_net_link_ether_inet
, OID_AUTO
, apple_hwcksum_rx
,
120 CTLFLAG_RW
| CTLFLAG_LOCKED
, &apple_hwcksum_rx
, 0, "");
122 SYSCTL_INT(_net_link_ether_inet
, OID_AUTO
, arp_llreach_base
,
123 CTLFLAG_RW
| CTLFLAG_LOCKED
, &arp_llreach_base
, LL_BASE_REACHABLE
,
124 "default ARP link-layer reachability max lifetime (in seconds)");
128 * The following are protected by rnh_lock
130 LIST_ENTRY(llinfo_arp
) la_le
;
131 struct rtentry
*la_rt
;
133 * The following are protected by rt_lock
135 struct mbuf
*la_hold
; /* last packet until resolved/timeout */
136 struct if_llreach
*la_llreach
; /* link-layer reachability record */
137 u_int64_t la_lastused
; /* last used timestamp */
138 u_int32_t la_asked
; /* # of requests sent */
139 u_int32_t la_persist
; /* expirable, but stays around */
143 * Synchronization notes:
145 * The global list of ARP entries are stored in llinfo_arp; an entry
146 * gets inserted into the list when the route is created and gets
147 * removed from the list when it is deleted; this is done as part
148 * of RTM_ADD/RTM_RESOLVE/RTM_DELETE in arp_rtrequest().
150 * Because rnh_lock and rt_lock for the entry are held during those
151 * operations, the same locks (and thus lock ordering) must be used
152 * elsewhere to access the relevant data structure fields:
154 * la_le.{le_next,le_prev}, la_rt
156 * - Routing lock (rnh_lock)
158 * la_hold, la_asked, la_llreach, la_lastused
160 * - Routing entry lock (rt_lock)
162 * Due to the dependency on rt_lock, llinfo_arp has the same lifetime
163 * as the route entry itself. When a route is deleted (RTM_DELETE),
164 * it is simply removed from the global list but the memory is not
165 * freed until the route itself is freed.
167 static LIST_HEAD(, llinfo_arp
) llinfo_arp
;
169 static int arp_inuse
, arp_allocated
;
171 static u_int32_t arp_maxtries
= 5;
172 static int useloopback
= 1; /* use loopback interface for local traffic */
173 static int arp_proxyall
= 0;
174 static int arp_sendllconflict
= 0;
176 SYSCTL_INT(_net_link_ether_inet
, OID_AUTO
, maxtries
, CTLFLAG_RW
| CTLFLAG_LOCKED
,
177 &arp_maxtries
, 0, "");
178 SYSCTL_INT(_net_link_ether_inet
, OID_AUTO
, useloopback
, CTLFLAG_RW
| CTLFLAG_LOCKED
,
179 &useloopback
, 0, "");
180 SYSCTL_INT(_net_link_ether_inet
, OID_AUTO
, proxyall
, CTLFLAG_RW
| CTLFLAG_LOCKED
,
181 &arp_proxyall
, 0, "");
182 SYSCTL_INT(_net_link_ether_inet
, OID_AUTO
, sendllconflict
, CTLFLAG_RW
| CTLFLAG_LOCKED
,
183 &arp_sendllconflict
, 0, "");
185 static int log_arp_warnings
= 0; /* Thread safe: no accumulated state */
187 SYSCTL_INT(_net_link_ether_inet
, OID_AUTO
, log_arp_warnings
,
188 CTLFLAG_RW
| CTLFLAG_LOCKED
,
189 &log_arp_warnings
, 0,
190 "log arp warning messages");
192 static int keep_announcements
= 1; /* Thread safe: no aging of state */
193 SYSCTL_INT(_net_link_ether_inet
, OID_AUTO
, keep_announcements
,
194 CTLFLAG_RW
| CTLFLAG_LOCKED
,
195 &keep_announcements
, 0,
196 "keep arp announcements");
198 static int send_conflicting_probes
= 1; /* Thread safe: no accumulated state */
199 SYSCTL_INT(_net_link_ether_inet
, OID_AUTO
, send_conflicting_probes
,
200 CTLFLAG_RW
| CTLFLAG_LOCKED
,
201 &send_conflicting_probes
, 0,
202 "send conflicting link-local arp probes");
204 static errno_t
arp_lookup_route(const struct in_addr
*, int,
205 int, route_t
*, unsigned int);
206 static void arptimer(void *);
207 static struct llinfo_arp
*arp_llinfo_alloc(void);
208 static void arp_llinfo_free(void *);
209 static void arp_llinfo_purge(struct rtentry
*);
210 static void arp_llinfo_get_ri(struct rtentry
*, struct rt_reach_info
*);
212 static __inline
void arp_llreach_use(struct llinfo_arp
*);
213 static __inline
int arp_llreach_reachable(struct llinfo_arp
*);
214 static void arp_llreach_alloc(struct rtentry
*, struct ifnet
*, void *,
215 unsigned int, boolean_t
);
217 extern u_int32_t ipv4_ll_arp_aware
;
219 static int arpinit_done
;
221 static struct zone
*llinfo_arp_zone
;
222 #define LLINFO_ARP_ZONE_MAX 256 /* maximum elements in zone */
223 #define LLINFO_ARP_ZONE_NAME "llinfo_arp" /* name for zone */
229 log(LOG_NOTICE
, "arp_init called more than once (ignored)\n");
233 LIST_INIT(&llinfo_arp
);
235 llinfo_arp_zone
= zinit(sizeof (struct llinfo_arp
),
236 LLINFO_ARP_ZONE_MAX
* sizeof (struct llinfo_arp
), 0,
237 LLINFO_ARP_ZONE_NAME
);
238 if (llinfo_arp_zone
== NULL
)
239 panic("%s: failed allocating llinfo_arp_zone", __func__
);
241 zone_change(llinfo_arp_zone
, Z_EXPAND
, TRUE
);
242 zone_change(llinfo_arp_zone
, Z_CALLERACCT
, FALSE
);
247 timeout(arptimer
, (caddr_t
)0, hz
);
250 static struct llinfo_arp
*
251 arp_llinfo_alloc(void)
253 return (zalloc(llinfo_arp_zone
));
257 arp_llinfo_free(void *arg
)
259 struct llinfo_arp
*la
= arg
;
261 if (la
->la_le
.le_next
!= NULL
|| la
->la_le
.le_prev
!= NULL
) {
262 panic("%s: trying to free %p when it is in use", __func__
, la
);
266 /* Just in case there's anything there, free it */
267 if (la
->la_hold
!= NULL
) {
268 m_freem(la
->la_hold
);
272 /* Purge any link-layer info caching */
273 VERIFY(la
->la_rt
->rt_llinfo
== la
);
274 if (la
->la_rt
->rt_llinfo_purge
!= NULL
)
275 la
->la_rt
->rt_llinfo_purge(la
->la_rt
);
277 zfree(llinfo_arp_zone
, la
);
281 arp_llinfo_purge(struct rtentry
*rt
)
283 struct llinfo_arp
*la
= rt
->rt_llinfo
;
285 RT_LOCK_ASSERT_HELD(rt
);
286 VERIFY(rt
->rt_llinfo_purge
== arp_llinfo_purge
&& la
!= NULL
);
288 if (la
->la_llreach
!= NULL
) {
290 ifnet_llreach_free(la
->la_llreach
);
291 la
->la_llreach
= NULL
;
297 arp_llinfo_get_ri(struct rtentry
*rt
, struct rt_reach_info
*ri
)
299 struct llinfo_arp
*la
= rt
->rt_llinfo
;
300 struct if_llreach
*lr
= la
->la_llreach
;
303 bzero(ri
, sizeof (*ri
));
306 /* Export to rt_reach_info structure */
308 /* Export ARP send expiration time */
309 ri
->ri_snd_expire
= ifnet_llreach_up2cal(lr
, la
->la_lastused
);
315 arp_llreach_set_reachable(struct ifnet
*ifp
, void *addr
, unsigned int alen
)
317 /* Nothing more to do if it's disabled */
318 if (arp_llreach_base
== 0)
321 ifnet_llreach_set_reachable(ifp
, ETHERTYPE_IP
, addr
, alen
);
325 arp_llreach_use(struct llinfo_arp
*la
)
327 if (la
->la_llreach
!= NULL
)
328 la
->la_lastused
= net_uptime();
332 arp_llreach_reachable(struct llinfo_arp
*la
)
334 struct if_llreach
*lr
;
335 const char *why
= NULL
;
337 /* Nothing more to do if it's disabled; pretend it's reachable */
338 if (arp_llreach_base
== 0)
341 if ((lr
= la
->la_llreach
) == NULL
) {
343 * Link-layer reachability record isn't present for this
344 * ARP entry; pretend it's reachable and use it as is.
347 } else if (ifnet_llreach_reachable(lr
)) {
349 * Record is present, it's not shared with other ARP
350 * entries and a packet has recently been received
351 * from the remote host; consider it reachable.
353 if (lr
->lr_reqcnt
== 1)
356 /* Prime it up, if this is the first time */
357 if (la
->la_lastused
== 0) {
358 VERIFY(la
->la_llreach
!= NULL
);
363 * Record is present and shared with one or more ARP
364 * entries, and a packet has recently been received
365 * from the remote host. Since it's shared by more
366 * than one IP addresses, we can't rely on the link-
367 * layer reachability alone; consider it reachable if
368 * this ARP entry has been used "recently."
370 if (ifnet_llreach_reachable_delta(lr
, la
->la_lastused
))
373 why
= "has alias(es) and hasn't been used in a while";
375 why
= "haven't heard from it in a while";
378 if (log_arp_warnings
) {
379 char tmp
[MAX_IPv4_STR_LEN
];
380 u_int64_t now
= net_uptime();
382 log(LOG_DEBUG
, "%s%d: ARP probe(s) needed for %s; "
383 "%s [lastused %lld, lastrcvd %lld] secs ago\n",
384 lr
->lr_ifp
->if_name
, lr
->lr_ifp
->if_unit
, inet_ntop(AF_INET
,
385 &SIN(rt_key(la
->la_rt
))->sin_addr
, tmp
, sizeof (tmp
)), why
,
386 (la
->la_lastused
? (int64_t)(now
- la
->la_lastused
) : -1),
387 (lr
->lr_lastrcvd
? (int64_t)(now
- lr
->lr_lastrcvd
) : -1));
394 * Obtain a link-layer source cache entry for the sender.
396 * NOTE: This is currently only for ARP/Ethernet.
399 arp_llreach_alloc(struct rtentry
*rt
, struct ifnet
*ifp
, void *addr
,
400 unsigned int alen
, boolean_t solicited
)
402 VERIFY(rt
->rt_expire
== 0 || rt
->rt_rmx
.rmx_expire
!= 0);
403 VERIFY(rt
->rt_expire
!= 0 || rt
->rt_rmx
.rmx_expire
== 0);
404 if (arp_llreach_base
!= 0 &&
405 rt
->rt_expire
!= 0 && rt
->rt_ifp
!= lo_ifp
&&
406 ifp
->if_addrlen
== IF_LLREACH_MAXLEN
&& /* Ethernet */
407 alen
== ifp
->if_addrlen
) {
408 struct llinfo_arp
*la
= rt
->rt_llinfo
;
409 struct if_llreach
*lr
;
410 const char *why
= NULL
, *type
= "";
412 /* Become a regular mutex, just in case */
415 if ((lr
= la
->la_llreach
) != NULL
) {
416 type
= (solicited
? "ARP reply" : "ARP announcement");
418 * If target has changed, create a new record;
419 * otherwise keep existing record.
422 if (bcmp(addr
, lr
->lr_key
.addr
, alen
) != 0) {
424 /* Purge any link-layer info caching */
425 VERIFY(rt
->rt_llinfo_purge
!= NULL
);
426 rt
->rt_llinfo_purge(rt
);
428 why
= " for different target HW address; "
429 "using new llreach record";
431 lr
->lr_probes
= 0; /* reset probe count */
434 why
= " for same target HW address; "
435 "keeping existing llreach record";
441 lr
= la
->la_llreach
= ifnet_llreach_alloc(ifp
,
442 ETHERTYPE_IP
, addr
, alen
, arp_llreach_base
);
444 lr
->lr_probes
= 0; /* reset probe count */
446 why
= "creating new llreach record";
450 if (log_arp_warnings
&& lr
!= NULL
&& why
!= NULL
) {
451 char tmp
[MAX_IPv4_STR_LEN
];
453 log(LOG_DEBUG
, "%s%d: %s%s for %s\n", ifp
->if_name
,
454 ifp
->if_unit
, type
, why
, inet_ntop(AF_INET
,
455 &SIN(rt_key(rt
))->sin_addr
, tmp
, sizeof (tmp
)));
464 arptfree(struct llinfo_arp
*la
)
466 struct rtentry
*rt
= la
->la_rt
;
467 struct sockaddr_dl
*sdl
;
469 lck_mtx_assert(rnh_lock
, LCK_MTX_ASSERT_OWNED
);
470 RT_LOCK_ASSERT_HELD(rt
);
472 if (rt
->rt_refcnt
> 0 && (sdl
= SDL(rt
->rt_gateway
)) &&
473 sdl
->sdl_family
== AF_LINK
) {
476 rt
->rt_flags
&= ~RTF_REJECT
;
478 } else if (la
->la_persist
) {
480 * Instead of issuing RTM_DELETE, stop this route entry
481 * from holding an interface idle reference count; if
482 * the route is later reused, arp_validate() will revert
485 if (rt
->rt_refcnt
== 0)
486 rt_clear_idleref(rt
);
490 * Safe to drop rt_lock and use rt_key, since holding
491 * rnh_lock here prevents another thread from calling
492 * rt_setgate() on this route.
495 rtrequest_locked(RTM_DELETE
, rt_key(rt
), NULL
, rt_mask(rt
),
501 in_arpdrain(void *ignored_arg
)
503 #pragma unused (ignored_arg)
504 struct llinfo_arp
*la
, *ola
;
507 lck_mtx_lock(rnh_lock
);
508 la
= llinfo_arp
.lh_first
;
509 timenow
= net_uptime();
510 while ((ola
= la
) != 0) {
511 struct rtentry
*rt
= la
->la_rt
;
512 la
= la
->la_le
.le_next
;
514 VERIFY(rt
->rt_expire
== 0 || rt
->rt_rmx
.rmx_expire
!= 0);
515 VERIFY(rt
->rt_expire
!= 0 || rt
->rt_rmx
.rmx_expire
== 0);
516 if (rt
->rt_expire
&& rt
->rt_expire
<= timenow
)
517 arptfree(ola
); /* timer has expired, clear */
521 lck_mtx_unlock(rnh_lock
);
525 arp_validate(struct rtentry
*rt
)
527 struct llinfo_arp
*la
= rt
->rt_llinfo
;
529 RT_LOCK_ASSERT_HELD(rt
);
531 * If this is a persistent ARP entry, make it count towards the
532 * interface idleness just like before arptfree() was called.
539 * Timeout routine. Age arp_tab entries periodically.
543 arptimer(void *ignored_arg
)
545 #pragma unused (ignored_arg)
547 timeout(arptimer
, (caddr_t
)0, arpt_prune
* hz
);
551 * Parallel to llc_rtrequest.
557 __unused
struct sockaddr
*sa
)
559 struct sockaddr
*gate
= rt
->rt_gateway
;
560 struct llinfo_arp
*la
= rt
->rt_llinfo
;
561 static struct sockaddr_dl null_sdl
= {sizeof(null_sdl
), AF_LINK
, 0, 0, 0, 0, 0, {0}};
565 panic("%s: ARP has not been initialized", __func__
);
568 lck_mtx_assert(rnh_lock
, LCK_MTX_ASSERT_OWNED
);
569 RT_LOCK_ASSERT_HELD(rt
);
571 if (rt
->rt_flags
& RTF_GATEWAY
)
573 timenow
= net_uptime();
578 * XXX: If this is a manually added route to interface
579 * such as older version of routed or gated might provide,
580 * restore cloning bit.
582 if ((rt
->rt_flags
& RTF_HOST
) == 0 &&
583 SIN(rt_mask(rt
))->sin_addr
.s_addr
!= 0xffffffff)
584 rt
->rt_flags
|= RTF_CLONING
;
585 if (rt
->rt_flags
& RTF_CLONING
) {
587 * Case 1: This route should come from a route to iface.
589 if (rt_setgate(rt
, rt_key(rt
),
590 (struct sockaddr
*)&null_sdl
) == 0) {
591 gate
= rt
->rt_gateway
;
592 SDL(gate
)->sdl_type
= rt
->rt_ifp
->if_type
;
593 SDL(gate
)->sdl_index
= rt
->rt_ifp
->if_index
;
595 * In case we're called before 1.0 sec.
598 rt_setexpire(rt
, MAX(timenow
, 1));
602 /* Announce a new entry if requested. */
603 if (rt
->rt_flags
& RTF_ANNOUNCE
) {
605 arp_llreach_use(la
); /* Mark use timestamp */
607 dlil_send_arp(rt
->rt_ifp
, ARPOP_REQUEST
,
608 SDL(gate
), rt_key(rt
), NULL
, rt_key(rt
));
613 if (gate
->sa_family
!= AF_LINK
||
614 gate
->sa_len
< sizeof(null_sdl
)) {
615 if (log_arp_warnings
)
616 log(LOG_DEBUG
, "arp_rtrequest: bad gateway value\n");
619 SDL(gate
)->sdl_type
= rt
->rt_ifp
->if_type
;
620 SDL(gate
)->sdl_index
= rt
->rt_ifp
->if_index
;
622 break; /* This happens on a route change */
624 * Case 2: This route may come from cloning, or a manual route
625 * add with a LL address.
627 rt
->rt_llinfo
= la
= arp_llinfo_alloc();
629 if (log_arp_warnings
)
630 log(LOG_DEBUG
, "%s: malloc failed\n", __func__
);
633 rt
->rt_llinfo_get_ri
= arp_llinfo_get_ri
;
634 rt
->rt_llinfo_purge
= arp_llinfo_purge
;
635 rt
->rt_llinfo_free
= arp_llinfo_free
;
637 arp_inuse
++, arp_allocated
++;
638 Bzero(la
, sizeof(*la
));
640 rt
->rt_flags
|= RTF_LLINFO
;
641 LIST_INSERT_HEAD(&llinfo_arp
, la
, la_le
);
644 * This keeps the multicast addresses from showing up
645 * in `arp -a' listings as unresolved. It's not actually
646 * functional. Then the same for broadcast. For IPv4
647 * link-local address, keep the entry around even after
650 if (IN_MULTICAST(ntohl(SIN(rt_key(rt
))->sin_addr
.s_addr
))) {
652 dlil_resolve_multi(rt
->rt_ifp
, rt_key(rt
), gate
,
653 sizeof(struct sockaddr_dl
));
657 else if (in_broadcast(SIN(rt_key(rt
))->sin_addr
, rt
->rt_ifp
)) {
658 struct sockaddr_dl
*gate_ll
= SDL(gate
);
659 size_t broadcast_len
;
660 ifnet_llbroadcast_copy_bytes(rt
->rt_ifp
,
661 LLADDR(gate_ll
), sizeof(gate_ll
->sdl_data
),
663 gate_ll
->sdl_alen
= broadcast_len
;
664 gate_ll
->sdl_family
= AF_LINK
;
665 gate_ll
->sdl_len
= sizeof(struct sockaddr_dl
);
666 /* In case we're called before 1.0 sec. has elapsed */
667 rt_setexpire(rt
, MAX(timenow
, 1));
668 } else if (IN_LINKLOCAL(ntohl(SIN(rt_key(rt
))->sin_addr
.s_addr
))) {
670 * The persistent bit implies that once the ARP
671 * entry has reached it expiration time, the idle
672 * reference count to the interface will be released,
673 * but the ARP entry itself stays in the routing table
674 * until it is explicitly removed.
677 rt
->rt_flags
|= RTF_STATIC
;
680 /* Become a regular mutex, just in case */
682 IFA_LOCK_SPIN(rt
->rt_ifa
);
683 if (SIN(rt_key(rt
))->sin_addr
.s_addr
==
684 (IA_SIN(rt
->rt_ifa
))->sin_addr
.s_addr
) {
685 IFA_UNLOCK(rt
->rt_ifa
);
687 * This test used to be
688 * if (loif.if_flags & IFF_UP)
689 * It allowed local traffic to be forced through the
690 * hardware by configuring the loopback down. However,
691 * it causes problems during network configuration
692 * for boards that can't receive packets they send.
693 * It is now necessary to clear "useloopback" and
694 * remove the route to force traffic out to the
698 ifnet_lladdr_copy_bytes(rt
->rt_ifp
, LLADDR(SDL(gate
)),
699 SDL(gate
)->sdl_alen
= rt
->rt_ifp
->if_addrlen
);
701 if (rt
->rt_ifp
!= lo_ifp
) {
703 * Purge any link-layer info caching.
705 if (rt
->rt_llinfo_purge
!= NULL
)
706 rt
->rt_llinfo_purge(rt
);
709 * Adjust route ref count for the
712 if (rt
->rt_if_ref_fn
!= NULL
) {
713 rt
->rt_if_ref_fn(lo_ifp
, 1);
714 rt
->rt_if_ref_fn(rt
->rt_ifp
, -1);
720 IFA_UNLOCK(rt
->rt_ifa
);
729 * Unchain it but defer the actual freeing until the route
730 * itself is to be freed. rt->rt_llinfo still points to
731 * llinfo_arp, and likewise, la->la_rt still points to this
732 * route entry, except that RTF_LLINFO is now cleared.
734 LIST_REMOVE(la
, la_le
);
735 la
->la_le
.le_next
= NULL
;
736 la
->la_le
.le_prev
= NULL
;
739 * Purge any link-layer info caching.
741 if (rt
->rt_llinfo_purge
!= NULL
)
742 rt
->rt_llinfo_purge(rt
);
744 rt
->rt_flags
&= ~RTF_LLINFO
;
745 if (la
->la_hold
!= NULL
) {
746 m_freem(la
->la_hold
);
753 * convert hardware address to hex string for logging errors.
756 sdl_addr_to_hex(const struct sockaddr_dl
*sdl
, char * orig_buf
, int buflen
)
758 char * buf
= orig_buf
;
760 const u_char
* lladdr
= (u_char
*)(size_t)sdl
->sdl_data
;
761 int maxbytes
= buflen
/ 3;
763 if (maxbytes
> sdl
->sdl_alen
) {
764 maxbytes
= sdl
->sdl_alen
;
767 for (i
= 0; i
< maxbytes
; i
++) {
768 snprintf(buf
, 3, "%02x", lladdr
[i
]);
770 *buf
= (i
== maxbytes
- 1) ? '\0' : ':';
777 * arp_lookup_route will lookup the route for a given address.
779 * The address must be for a host on a local network on this interface.
780 * If the returned route is non-NULL, the route is locked and the caller
781 * is responsible for unlocking it and releasing its reference.
784 arp_lookup_route(const struct in_addr
*addr
, int create
, int proxy
,
785 route_t
*route
, unsigned int ifscope
)
787 struct sockaddr_inarp sin
= {sizeof(sin
), AF_INET
, 0, {0}, {0}, 0, 0};
788 const char *why
= NULL
;
794 sin
.sin_addr
.s_addr
= addr
->s_addr
;
795 sin
.sin_other
= proxy
? SIN_PROXY
: 0;
798 * If the destination is a link-local address, don't
799 * constrain the lookup (don't scope it).
801 if (IN_LINKLOCAL(ntohl(addr
->s_addr
)))
802 ifscope
= IFSCOPE_NONE
;
804 rt
= rtalloc1_scoped((struct sockaddr
*)&sin
, create
, 0, ifscope
);
806 return (ENETUNREACH
);
810 if (rt
->rt_flags
& RTF_GATEWAY
) {
811 why
= "host is not on local network";
813 } else if (!(rt
->rt_flags
& RTF_LLINFO
)) {
814 why
= "could not allocate llinfo";
816 } else if (rt
->rt_gateway
->sa_family
!= AF_LINK
) {
817 why
= "gateway route is not ours";
818 error
= EPROTONOSUPPORT
;
822 if (create
&& log_arp_warnings
) {
823 char tmp
[MAX_IPv4_STR_LEN
];
824 log(LOG_DEBUG
, "arplookup link#%d %s failed: %s\n",
825 ifscope
, inet_ntop(AF_INET
, addr
, tmp
,
830 * If there are no references to this route, and it is
831 * a cloned route, and not static, and ARP had created
832 * the route, then purge it from the routing table as
833 * it is probably bogus.
835 if (rt
->rt_refcnt
== 1 &&
836 (rt
->rt_flags
& (RTF_WASCLONED
| RTF_STATIC
)) ==
839 * Prevent another thread from modiying rt_key,
840 * rt_gateway via rt_setgate() after rt_lock is
841 * dropped by marking the route as defunct.
843 rt
->rt_flags
|= RTF_CONDEMNED
;
845 rtrequest(RTM_DELETE
, rt_key(rt
), rt
->rt_gateway
,
846 rt_mask(rt
), rt
->rt_flags
, 0);
849 RT_REMREF_LOCKED(rt
);
856 * Caller releases reference and does RT_UNLOCK(rt).
863 * arp_route_to_gateway_route will find the gateway route for a given route.
865 * If the route is down, look the route up again.
866 * If the route goes through a gateway, get the route to the gateway.
867 * If the gateway route is down, look it up again.
868 * If the route is set to reject, verify it hasn't expired.
870 * If the returned route is non-NULL, the caller is responsible for
871 * releasing the reference and unlocking the route.
873 #define senderr(e) { error = (e); goto bad; }
874 __private_extern__ errno_t
875 arp_route_to_gateway_route(const struct sockaddr
*net_dest
, route_t hint0
,
879 route_t rt
= hint0
, hint
= hint0
;
885 * Next hop determination. Because we may involve the gateway route
886 * in addition to the original route, locking is rather complicated.
887 * The general concept is that regardless of whether the route points
888 * to the original route or to the gateway route, this routine takes
889 * an extra reference on such a route. This extra reference will be
890 * released at the end.
892 * Care must be taken to ensure that the "hint0" route never gets freed
893 * via rtfree(), since the caller may have stored it inside a struct
894 * route with a reference held for that placeholder.
897 unsigned int ifindex
;
900 ifindex
= rt
->rt_ifp
->if_index
;
901 RT_ADDREF_LOCKED(rt
);
902 if (!(rt
->rt_flags
& RTF_UP
)) {
903 RT_REMREF_LOCKED(rt
);
905 /* route is down, find a new one */
906 hint
= rt
= rtalloc1_scoped((struct sockaddr
*)
907 (size_t)net_dest
, 1, 0, ifindex
);
910 ifindex
= rt
->rt_ifp
->if_index
;
912 senderr(EHOSTUNREACH
);
917 * We have a reference to "rt" by now; it will either
918 * be released or freed at the end of this routine.
920 RT_LOCK_ASSERT_HELD(rt
);
921 if (rt
->rt_flags
& RTF_GATEWAY
) {
922 struct rtentry
*gwrt
= rt
->rt_gwroute
;
923 struct sockaddr_in gw
;
925 /* If there's no gateway rt, look it up */
927 gw
= *((struct sockaddr_in
*)rt
->rt_gateway
);
931 /* Become a regular mutex */
935 * Take gwrt's lock while holding route's lock;
936 * this is okay since gwrt never points back
937 * to "rt", so no lock ordering issues.
940 if (!(gwrt
->rt_flags
& RTF_UP
)) {
941 struct rtentry
*ogwrt
;
943 rt
->rt_gwroute
= NULL
;
945 gw
= *((struct sockaddr_in
*)rt
->rt_gateway
);
949 gwrt
= rtalloc1_scoped(
950 (struct sockaddr
*)&gw
, 1, 0, ifindex
);
954 * Bail out if the route is down, no route
955 * to gateway, circular route, or if the
956 * gateway portion of "rt" has changed.
958 if (!(rt
->rt_flags
& RTF_UP
) ||
959 gwrt
== NULL
|| gwrt
== rt
||
960 !equal(SA(&gw
), rt
->rt_gateway
)) {
962 RT_REMREF_LOCKED(gwrt
);
968 senderr(EHOSTUNREACH
);
971 /* Remove any existing gwrt */
972 ogwrt
= rt
->rt_gwroute
;
973 if ((rt
->rt_gwroute
= gwrt
) != NULL
)
976 /* Clean up "rt" now while we can */
978 RT_REMREF_LOCKED(rt
);
985 /* Now free the replaced gwrt */
988 /* If still no route to gateway, bail out */
990 senderr(EHOSTUNREACH
);
992 RT_ADDREF_LOCKED(gwrt
);
994 /* Clean up "rt" now while we can */
996 RT_REMREF_LOCKED(rt
);
1005 /* rt == gwrt; if it is now down, give up */
1007 if (!(rt
->rt_flags
& RTF_UP
)) {
1009 senderr(EHOSTUNREACH
);
1013 if (rt
->rt_flags
& RTF_REJECT
) {
1014 VERIFY(rt
->rt_expire
== 0 || rt
->rt_rmx
.rmx_expire
!= 0);
1015 VERIFY(rt
->rt_expire
!= 0 || rt
->rt_rmx
.rmx_expire
== 0);
1016 timenow
= net_uptime();
1017 if (rt
->rt_expire
== 0 ||
1018 timenow
< rt
->rt_expire
) {
1020 senderr(rt
== hint
? EHOSTDOWN
: EHOSTUNREACH
);
1024 /* Become a regular mutex */
1025 RT_CONVERT_LOCK(rt
);
1027 /* Caller is responsible for cleaning up "rt" */
1033 /* Clean up route (either it is "rt" or "gwrt") */
1037 RT_REMREF_LOCKED(rt
);
1049 * This is the ARP pre-output routine; care must be taken to ensure that
1050 * the "hint" route never gets freed via rtfree(), since the caller may
1051 * have stored it inside a struct route with a reference held for that
1055 arp_lookup_ip(ifnet_t ifp
, const struct sockaddr_in
*net_dest
,
1056 struct sockaddr_dl
*ll_dest
, size_t ll_dest_len
, route_t hint
,
1059 route_t route
= NULL
; /* output route */
1061 struct sockaddr_dl
*gateway
;
1062 struct llinfo_arp
*llinfo
= NULL
;
1064 int unreachable
= 0;
1066 if (net_dest
->sin_family
!= AF_INET
)
1067 return (EAFNOSUPPORT
);
1069 if ((ifp
->if_flags
& (IFF_UP
|IFF_RUNNING
)) != (IFF_UP
|IFF_RUNNING
))
1073 * If we were given a route, verify the route and grab the gateway
1077 * Callee holds a reference on the route and returns
1078 * with the route entry locked, upon success.
1080 result
= arp_route_to_gateway_route((const struct sockaddr
*)
1081 net_dest
, hint
, &route
);
1085 RT_LOCK_ASSERT_HELD(route
);
1088 if (packet
->m_flags
& M_BCAST
) {
1089 size_t broadcast_len
;
1090 bzero(ll_dest
, ll_dest_len
);
1091 result
= ifnet_llbroadcast_copy_bytes(ifp
, LLADDR(ll_dest
),
1092 ll_dest_len
- offsetof(struct sockaddr_dl
, sdl_data
),
1095 ll_dest
->sdl_alen
= broadcast_len
;
1096 ll_dest
->sdl_family
= AF_LINK
;
1097 ll_dest
->sdl_len
= sizeof(struct sockaddr_dl
);
1101 if (packet
->m_flags
& M_MCAST
) {
1104 result
= dlil_resolve_multi(ifp
,
1105 (const struct sockaddr
*)net_dest
,
1106 (struct sockaddr
*)ll_dest
, ll_dest_len
);
1113 * If we didn't find a route, or the route doesn't have
1114 * link layer information, trigger the creation of the
1115 * route and link layer information.
1117 if (route
== NULL
|| route
->rt_llinfo
== NULL
) {
1118 /* Clean up now while we can */
1119 if (route
!= NULL
) {
1120 if (route
== hint
) {
1121 RT_REMREF_LOCKED(route
);
1129 * Callee holds a reference on the route and returns
1130 * with the route entry locked, upon success.
1132 result
= arp_lookup_route(&net_dest
->sin_addr
, 1, 0, &route
,
1135 RT_LOCK_ASSERT_HELD(route
);
1138 if (result
|| route
== NULL
|| (llinfo
= route
->rt_llinfo
) == NULL
) {
1139 char tmp
[MAX_IPv4_STR_LEN
];
1141 /* In case result is 0 but no route, return an error */
1143 result
= EHOSTUNREACH
;
1145 if (log_arp_warnings
&&
1146 route
!= NULL
&& route
->rt_llinfo
== NULL
)
1147 log(LOG_DEBUG
, "arpresolve: can't allocate llinfo "
1148 "for %s\n", inet_ntop(AF_INET
, &net_dest
->sin_addr
,
1154 * Now that we have the right route, is it filled in?
1156 gateway
= SDL(route
->rt_gateway
);
1157 timenow
= net_uptime();
1158 VERIFY(route
->rt_expire
== 0 || route
->rt_rmx
.rmx_expire
!= 0);
1159 VERIFY(route
->rt_expire
!= 0 || route
->rt_rmx
.rmx_expire
== 0);
1160 if ((route
->rt_expire
== 0 ||
1161 route
->rt_expire
> timenow
) && gateway
!= NULL
&&
1162 gateway
->sdl_family
== AF_LINK
&& gateway
->sdl_alen
!= 0 &&
1163 !(unreachable
= !arp_llreach_reachable(llinfo
))) {
1164 bcopy(gateway
, ll_dest
, MIN(gateway
->sdl_len
, ll_dest_len
));
1166 arp_llreach_use(llinfo
); /* Mark use timestamp */
1168 } else if (unreachable
) {
1170 * Discard existing answer in case we need to probe.
1172 gateway
->sdl_alen
= 0;
1175 if (ifp
->if_flags
& IFF_NOARP
) {
1181 * Route wasn't complete/valid. We need to arp.
1183 if (packet
!= NULL
) {
1184 if (llinfo
->la_hold
!= NULL
)
1185 m_freem(llinfo
->la_hold
);
1186 llinfo
->la_hold
= packet
;
1189 if (route
->rt_expire
) {
1190 route
->rt_flags
&= ~RTF_REJECT
;
1191 if (llinfo
->la_asked
== 0 ||
1192 route
->rt_expire
!= timenow
) {
1193 rt_setexpire(route
, timenow
);
1194 if (llinfo
->la_asked
++ < arp_maxtries
) {
1195 struct ifaddr
*rt_ifa
= route
->rt_ifa
;
1196 struct sockaddr
*sa
;
1198 /* Become a regular mutex, just in case */
1199 RT_CONVERT_LOCK(route
);
1200 /* Update probe count, if applicable */
1201 if (llinfo
->la_llreach
!= NULL
) {
1202 IFLR_LOCK_SPIN(llinfo
->la_llreach
);
1203 llinfo
->la_llreach
->lr_probes
++;
1204 IFLR_UNLOCK(llinfo
->la_llreach
);
1206 IFA_LOCK_SPIN(rt_ifa
);
1207 IFA_ADDREF_LOCKED(rt_ifa
);
1208 sa
= rt_ifa
->ifa_addr
;
1210 arp_llreach_use(llinfo
); /* Mark use timestamp */
1212 dlil_send_arp(ifp
, ARPOP_REQUEST
, NULL
,
1213 sa
, NULL
, (const struct sockaddr
*)net_dest
);
1216 result
= EJUSTRETURN
;
1219 route
->rt_flags
|= RTF_REJECT
;
1220 rt_setexpire(route
, rt_expiry(route
,
1221 route
->rt_expire
, arpt_down
));
1222 llinfo
->la_asked
= 0;
1224 * Clear la_hold; don't free the packet since
1225 * we're not returning EJUSTRETURN; the caller
1226 * will handle the freeing.
1228 llinfo
->la_hold
= NULL
;
1229 result
= EHOSTUNREACH
;
1235 /* The packet is now held inside la_hold (can "packet" be NULL?) */
1236 result
= EJUSTRETURN
;
1239 if (route
!= NULL
) {
1240 if (route
== hint
) {
1241 RT_REMREF_LOCKED(route
);
1252 arp_ip_handle_input(
1255 const struct sockaddr_dl
*sender_hw
,
1256 const struct sockaddr_in
*sender_ip
,
1257 const struct sockaddr_in
*target_ip
)
1259 char ipv4str
[MAX_IPv4_STR_LEN
];
1260 struct sockaddr_dl proxied
;
1261 struct sockaddr_dl
*gateway
, *target_hw
= NULL
;
1263 struct in_ifaddr
*ia
;
1264 struct in_ifaddr
*best_ia
= NULL
;
1265 struct sockaddr_in best_ia_sin
;
1266 route_t route
= NULL
;
1267 char buf
[3 * MAX_HW_LEN
]; // enough for MAX_HW_LEN byte hw address
1268 struct llinfo_arp
*llinfo
;
1270 int created_announcement
= 0;
1271 int bridged
= 0, is_bridge
= 0;
1273 /* Do not respond to requests for 0.0.0.0 */
1274 if (target_ip
->sin_addr
.s_addr
== 0 && arpop
== ARPOP_REQUEST
)
1279 if (ifp
->if_type
== IFT_BRIDGE
)
1283 * Determine if this ARP is for us
1284 * For a bridge, we want to check the address irrespective
1285 * of the receive interface.
1287 lck_rw_lock_shared(in_ifaddr_rwlock
);
1288 TAILQ_FOREACH(ia
, INADDR_HASH(target_ip
->sin_addr
.s_addr
), ia_hash
) {
1289 IFA_LOCK_SPIN(&ia
->ia_ifa
);
1290 if (((bridged
&& ia
->ia_ifp
->if_bridge
!= NULL
) ||
1291 (ia
->ia_ifp
== ifp
)) &&
1292 ia
->ia_addr
.sin_addr
.s_addr
== target_ip
->sin_addr
.s_addr
) {
1294 best_ia_sin
= best_ia
->ia_addr
;
1295 IFA_ADDREF_LOCKED(&ia
->ia_ifa
);
1296 IFA_UNLOCK(&ia
->ia_ifa
);
1297 lck_rw_done(in_ifaddr_rwlock
);
1300 IFA_UNLOCK(&ia
->ia_ifa
);
1303 TAILQ_FOREACH(ia
, INADDR_HASH(sender_ip
->sin_addr
.s_addr
), ia_hash
) {
1304 IFA_LOCK_SPIN(&ia
->ia_ifa
);
1305 if (((bridged
&& ia
->ia_ifp
->if_bridge
!= NULL
) ||
1306 (ia
->ia_ifp
== ifp
)) &&
1307 ia
->ia_addr
.sin_addr
.s_addr
== sender_ip
->sin_addr
.s_addr
) {
1309 best_ia_sin
= best_ia
->ia_addr
;
1310 IFA_ADDREF_LOCKED(&ia
->ia_ifa
);
1311 IFA_UNLOCK(&ia
->ia_ifa
);
1312 lck_rw_done(in_ifaddr_rwlock
);
1315 IFA_UNLOCK(&ia
->ia_ifa
);
1318 #define BDG_MEMBER_MATCHES_ARP(addr, ifp, ia) \
1319 (ia->ia_ifp->if_bridge == ifp->if_softc && \
1320 !bcmp(ifnet_lladdr(ia->ia_ifp), ifnet_lladdr(ifp), ifp->if_addrlen) && \
1321 addr == ia->ia_addr.sin_addr.s_addr)
1323 * Check the case when bridge shares its MAC address with
1324 * some of its children, so packets are claimed by bridge
1325 * itself (bridge_input() does it first), but they are really
1326 * meant to be destined to the bridge member.
1329 TAILQ_FOREACH(ia
, INADDR_HASH(target_ip
->sin_addr
.s_addr
),
1331 IFA_LOCK_SPIN(&ia
->ia_ifa
);
1332 if (BDG_MEMBER_MATCHES_ARP(target_ip
->sin_addr
.s_addr
,
1336 best_ia_sin
= best_ia
->ia_addr
;
1337 IFA_ADDREF_LOCKED(&ia
->ia_ifa
);
1338 IFA_UNLOCK(&ia
->ia_ifa
);
1339 lck_rw_done(in_ifaddr_rwlock
);
1342 IFA_UNLOCK(&ia
->ia_ifa
);
1345 lck_rw_done(in_ifaddr_rwlock
);
1348 * No match, use the first inet address on the receive interface
1349 * as a dummy address for the rest of the function; we may be
1350 * proxying for another address.
1352 ifnet_lock_shared(ifp
);
1353 TAILQ_FOREACH(ifa
, &ifp
->if_addrhead
, ifa_link
) {
1355 if (ifa
->ifa_addr
->sa_family
!= AF_INET
) {
1359 best_ia
= (struct in_ifaddr
*)ifa
;
1360 best_ia_sin
= best_ia
->ia_addr
;
1361 IFA_ADDREF_LOCKED(ifa
);
1363 ifnet_lock_done(ifp
);
1366 ifnet_lock_done(ifp
);
1369 * If we're not a bridge member, or if we are but there's no
1370 * IPv4 address to use for the interface, drop the packet.
1372 if (!bridged
|| best_ia
== NULL
)
1376 /* If the packet is from this interface, ignore the packet */
1377 if (!bcmp(CONST_LLADDR(sender_hw
), ifnet_lladdr(ifp
), sender_hw
->sdl_alen
)) {
1381 /* Check for a conflict */
1382 if (!bridged
&& sender_ip
->sin_addr
.s_addr
== best_ia_sin
.sin_addr
.s_addr
) {
1383 struct kev_msg ev_msg
;
1384 struct kev_in_collision
*in_collision
;
1385 u_char storage
[sizeof(struct kev_in_collision
) + MAX_HW_LEN
];
1386 bzero(&ev_msg
, sizeof(struct kev_msg
));
1387 bzero(storage
, (sizeof(struct kev_in_collision
) + MAX_HW_LEN
));
1388 in_collision
= (struct kev_in_collision
*)storage
;
1389 log(LOG_ERR
, "%s%d duplicate IP address %s sent from address %s\n",
1390 ifp
->if_name
, ifp
->if_unit
,
1391 inet_ntop(AF_INET
, &sender_ip
->sin_addr
, ipv4str
, sizeof(ipv4str
)),
1392 sdl_addr_to_hex(sender_hw
, buf
, sizeof(buf
)));
1394 /* Send a kernel event so anyone can learn of the conflict */
1395 in_collision
->link_data
.if_family
= ifp
->if_family
;
1396 in_collision
->link_data
.if_unit
= ifp
->if_unit
;
1397 strncpy(&in_collision
->link_data
.if_name
[0], ifp
->if_name
, IFNAMSIZ
);
1398 in_collision
->ia_ipaddr
= sender_ip
->sin_addr
;
1399 in_collision
->hw_len
= sender_hw
->sdl_alen
< MAX_HW_LEN
? sender_hw
->sdl_alen
: MAX_HW_LEN
;
1400 bcopy(CONST_LLADDR(sender_hw
), (caddr_t
)in_collision
->hw_addr
, in_collision
->hw_len
);
1401 ev_msg
.vendor_code
= KEV_VENDOR_APPLE
;
1402 ev_msg
.kev_class
= KEV_NETWORK_CLASS
;
1403 ev_msg
.kev_subclass
= KEV_INET_SUBCLASS
;
1404 ev_msg
.event_code
= KEV_INET_ARPCOLLISION
;
1405 ev_msg
.dv
[0].data_ptr
= in_collision
;
1406 ev_msg
.dv
[0].data_length
= sizeof(struct kev_in_collision
) + in_collision
->hw_len
;
1407 ev_msg
.dv
[1].data_length
= 0;
1408 kev_post_msg(&ev_msg
);
1414 * Look up the routing entry. If it doesn't exist and we are the
1415 * target, and the sender isn't 0.0.0.0, go ahead and create one.
1416 * Callee holds a reference on the route and returns with the route
1417 * entry locked, upon success.
1419 error
= arp_lookup_route(&sender_ip
->sin_addr
,
1420 (target_ip
->sin_addr
.s_addr
== best_ia_sin
.sin_addr
.s_addr
&&
1421 sender_ip
->sin_addr
.s_addr
!= 0), 0, &route
, ifp
->if_index
);
1424 RT_LOCK_ASSERT_HELD(route
);
1426 if (error
|| route
== 0 || route
->rt_gateway
== 0) {
1427 if (arpop
!= ARPOP_REQUEST
) {
1430 if (arp_sendllconflict
1431 && send_conflicting_probes
!= 0
1432 && (ifp
->if_eflags
& IFEF_ARPLL
) != 0
1433 && IN_LINKLOCAL(ntohl(target_ip
->sin_addr
.s_addr
))
1434 && sender_ip
->sin_addr
.s_addr
== 0) {
1436 * Verify this ARP probe doesn't conflict with an IPv4LL we know of
1437 * on another interface.
1439 if (route
!= NULL
) {
1440 RT_REMREF_LOCKED(route
);
1445 * Callee holds a reference on the route and returns
1446 * with the route entry locked, upon success.
1448 error
= arp_lookup_route(&target_ip
->sin_addr
, 0, 0,
1449 &route
, ifp
->if_index
);
1452 RT_LOCK_ASSERT_HELD(route
);
1454 if (error
== 0 && route
&& route
->rt_gateway
) {
1455 gateway
= SDL(route
->rt_gateway
);
1456 if (route
->rt_ifp
!= ifp
&& gateway
->sdl_alen
!= 0
1457 && (gateway
->sdl_alen
!= sender_hw
->sdl_alen
1458 || bcmp(CONST_LLADDR(gateway
), CONST_LLADDR(sender_hw
),
1459 gateway
->sdl_alen
) != 0)) {
1461 * A node is probing for an IPv4LL we know exists on a
1462 * different interface. We respond with a conflicting probe
1463 * to force the new device to pick a different IPv4LL
1466 if (log_arp_warnings
) {
1468 "arp: %s on %s%d sent probe for %s, already on %s%d\n",
1469 sdl_addr_to_hex(sender_hw
, buf
, sizeof(buf
)),
1470 ifp
->if_name
, ifp
->if_unit
,
1471 inet_ntop(AF_INET
, &target_ip
->sin_addr
, ipv4str
,
1473 route
->rt_ifp
->if_name
, route
->rt_ifp
->if_unit
);
1475 "arp: sending conflicting probe to %s on %s%d\n",
1476 sdl_addr_to_hex(sender_hw
, buf
, sizeof(buf
)),
1477 ifp
->if_name
, ifp
->if_unit
);
1479 /* Mark use timestamp */
1480 if (route
->rt_llinfo
!= NULL
)
1481 arp_llreach_use(route
->rt_llinfo
);
1482 /* We're done with the route */
1483 RT_REMREF_LOCKED(route
);
1487 * Send a conservative unicast "ARP probe".
1488 * This should force the other device to pick a new number.
1489 * This will not force the device to pick a new number if the device
1490 * has already assigned that number.
1491 * This will not imply to the device that we own that address.
1492 * The link address is always present; it's never freed.
1494 ifnet_lock_shared(ifp
);
1495 ifa
= ifp
->if_lladdr
;
1497 ifnet_lock_done(ifp
);
1498 dlil_send_arp_internal(ifp
, ARPOP_REQUEST
,
1500 (const struct sockaddr
*)sender_ip
, sender_hw
,
1501 (const struct sockaddr
*)target_ip
);
1507 } else if (keep_announcements
!= 0
1508 && target_ip
->sin_addr
.s_addr
== sender_ip
->sin_addr
.s_addr
) {
1509 /* don't create entry if link-local address and link-local is disabled */
1510 if (!IN_LINKLOCAL(ntohl(sender_ip
->sin_addr
.s_addr
))
1511 || (ifp
->if_eflags
& IFEF_ARPLL
) != 0) {
1512 if (route
!= NULL
) {
1513 RT_REMREF_LOCKED(route
);
1518 * Callee holds a reference on the route and
1519 * returns with the route entry locked, upon
1522 error
= arp_lookup_route(&sender_ip
->sin_addr
,
1523 1, 0, &route
, ifp
->if_index
);
1526 RT_LOCK_ASSERT_HELD(route
);
1528 if (error
== 0 && route
!= NULL
&& route
->rt_gateway
!= NULL
) {
1529 created_announcement
= 1;
1532 if (created_announcement
== 0) {
1540 RT_LOCK_ASSERT_HELD(route
);
1541 VERIFY(route
->rt_expire
== 0 || route
->rt_rmx
.rmx_expire
!= 0);
1542 VERIFY(route
->rt_expire
!= 0 || route
->rt_rmx
.rmx_expire
== 0);
1543 gateway
= SDL(route
->rt_gateway
);
1544 if (!bridged
&& route
->rt_ifp
!= ifp
) {
1545 if (!IN_LINKLOCAL(ntohl(sender_ip
->sin_addr
.s_addr
)) || (ifp
->if_eflags
& IFEF_ARPLL
) == 0) {
1546 if (log_arp_warnings
)
1547 log(LOG_ERR
, "arp: %s is on %s%d but got reply from %s on %s%d\n",
1548 inet_ntop(AF_INET
, &sender_ip
->sin_addr
, ipv4str
,
1550 route
->rt_ifp
->if_name
,
1551 route
->rt_ifp
->if_unit
,
1552 sdl_addr_to_hex(sender_hw
, buf
, sizeof(buf
)),
1553 ifp
->if_name
, ifp
->if_unit
);
1557 /* Don't change a permanent address */
1558 if (route
->rt_expire
== 0) {
1563 * We're about to check and/or change the route's ifp
1564 * and ifa, so do the lock dance: drop rt_lock, hold
1565 * rnh_lock and re-hold rt_lock to avoid violating the
1566 * lock ordering. We have an extra reference on the
1567 * route, so it won't go away while we do this.
1570 lck_mtx_lock(rnh_lock
);
1573 * Don't change the cloned route away from the
1574 * parent's interface if the address did resolve
1575 * or if the route is defunct. rt_ifp on both
1576 * the parent and the clone can now be freely
1577 * accessed now that we have acquired rnh_lock.
1579 gateway
= SDL(route
->rt_gateway
);
1580 if ((gateway
->sdl_alen
!= 0 && route
->rt_parent
&&
1581 route
->rt_parent
->rt_ifp
== route
->rt_ifp
) ||
1582 (route
->rt_flags
& RTF_CONDEMNED
)) {
1583 RT_REMREF_LOCKED(route
);
1586 lck_mtx_unlock(rnh_lock
);
1589 if (route
->rt_ifp
!= ifp
) {
1591 * Purge any link-layer info caching.
1593 if (route
->rt_llinfo_purge
!= NULL
)
1594 route
->rt_llinfo_purge(route
);
1596 /* Adjust route ref count for the interfaces */
1597 if (route
->rt_if_ref_fn
!= NULL
) {
1598 route
->rt_if_ref_fn(ifp
, 1);
1599 route
->rt_if_ref_fn(route
->rt_ifp
, -1);
1602 /* Change the interface when the existing route is on */
1603 route
->rt_ifp
= ifp
;
1604 rtsetifa(route
, &best_ia
->ia_ifa
);
1605 gateway
->sdl_index
= ifp
->if_index
;
1607 lck_mtx_unlock(rnh_lock
);
1609 /* Don't bother if the route is down */
1610 if (!(route
->rt_flags
& RTF_UP
))
1612 /* Refresh gateway pointer */
1613 gateway
= SDL(route
->rt_gateway
);
1615 RT_LOCK_ASSERT_HELD(route
);
1618 if (gateway
->sdl_alen
&& bcmp(LLADDR(gateway
), CONST_LLADDR(sender_hw
), gateway
->sdl_alen
)) {
1619 if (route
->rt_expire
&& log_arp_warnings
) {
1620 char buf2
[3 * MAX_HW_LEN
];
1621 log(LOG_INFO
, "arp: %s moved from %s to %s on %s%d\n",
1622 inet_ntop(AF_INET
, &sender_ip
->sin_addr
, ipv4str
,
1624 sdl_addr_to_hex(gateway
, buf
, sizeof(buf
)),
1625 sdl_addr_to_hex(sender_hw
, buf2
, sizeof(buf2
)),
1626 ifp
->if_name
, ifp
->if_unit
);
1628 else if (route
->rt_expire
== 0) {
1629 if (log_arp_warnings
) {
1630 log(LOG_ERR
, "arp: %s attempts to modify "
1631 "permanent entry for %s on %s%d\n",
1632 sdl_addr_to_hex(sender_hw
, buf
,
1634 inet_ntop(AF_INET
, &sender_ip
->sin_addr
,
1635 ipv4str
, sizeof(ipv4str
)),
1636 ifp
->if_name
, ifp
->if_unit
);
1642 /* Copy the sender hardware address in to the route's gateway address */
1643 gateway
->sdl_alen
= sender_hw
->sdl_alen
;
1644 bcopy(CONST_LLADDR(sender_hw
), LLADDR(gateway
), gateway
->sdl_alen
);
1646 /* Update the expire time for the route and clear the reject flag */
1647 if (route
->rt_expire
) {
1650 timenow
= net_uptime();
1652 rt_expiry(route
, timenow
, arpt_keep
));
1654 route
->rt_flags
&= ~RTF_REJECT
;
1656 /* cache the gateway (sender HW) address */
1657 arp_llreach_alloc(route
, ifp
, LLADDR(gateway
), gateway
->sdl_alen
,
1658 (arpop
== ARPOP_REPLY
));
1660 /* update the llinfo, send a queued packet if there is one */
1661 llinfo
= route
->rt_llinfo
;
1662 llinfo
->la_asked
= 0;
1663 if (llinfo
->la_hold
) {
1665 m0
= llinfo
->la_hold
;
1666 llinfo
->la_hold
= NULL
;
1669 dlil_output(ifp
, PF_INET
, m0
, (caddr_t
)route
, rt_key(route
), 0);
1675 if (route
!= NULL
) {
1676 /* Mark use timestamp if we're going to send a reply */
1677 if (arpop
== ARPOP_REQUEST
&& route
->rt_llinfo
!= NULL
)
1678 arp_llreach_use(route
->rt_llinfo
);
1679 RT_REMREF_LOCKED(route
);
1684 if (arpop
!= ARPOP_REQUEST
)
1687 /* If we are not the target, check if we should proxy */
1688 if (target_ip
->sin_addr
.s_addr
!= best_ia_sin
.sin_addr
.s_addr
) {
1690 * Find a proxy route; callee holds a reference on the
1691 * route and returns with the route entry locked, upon
1694 error
= arp_lookup_route(&target_ip
->sin_addr
, 0, SIN_PROXY
,
1695 &route
, ifp
->if_index
);
1698 RT_LOCK_ASSERT_HELD(route
);
1700 * Return proxied ARP replies only on the interface
1701 * or bridge cluster where this network resides.
1702 * Otherwise we may conflict with the host we are
1705 if (route
->rt_ifp
!= ifp
&&
1706 (route
->rt_ifp
->if_bridge
!= ifp
->if_bridge
||
1707 ifp
->if_bridge
== NULL
)) {
1708 RT_REMREF_LOCKED(route
);
1712 proxied
= *SDL(route
->rt_gateway
);
1713 target_hw
= &proxied
;
1716 * We don't have a route entry indicating we should
1717 * use proxy. If we aren't supposed to proxy all,
1724 * See if we have a route to the target ip before
1727 route
= rtalloc1_scoped((struct sockaddr
*)
1728 (size_t)target_ip
, 0, 0, ifp
->if_index
);
1733 * Don't proxy for hosts already on the same interface.
1736 if (route
->rt_ifp
== ifp
) {
1742 /* Mark use timestamp */
1743 if (route
->rt_llinfo
!= NULL
)
1744 arp_llreach_use(route
->rt_llinfo
);
1745 RT_REMREF_LOCKED(route
);
1749 dlil_send_arp(ifp
, ARPOP_REPLY
,
1750 target_hw
, (const struct sockaddr
*)target_ip
,
1751 sender_hw
, (const struct sockaddr
*)sender_ip
);
1754 if (best_ia
!= NULL
)
1755 IFA_REMREF(&best_ia
->ia_ifa
);
1760 arp_ifinit(struct ifnet
*ifp
, struct ifaddr
*ifa
)
1762 struct sockaddr
*sa
;
1765 ifa
->ifa_rtrequest
= arp_rtrequest
;
1766 ifa
->ifa_flags
|= RTF_CLONING
;
1769 dlil_send_arp(ifp
, ARPOP_REQUEST
, NULL
, sa
, NULL
, sa
);