]> git.saurik.com Git - apple/xnu.git/blame - bsd/netinet/in_arp.c
xnu-4903.270.47.tar.gz
[apple/xnu.git] / bsd / netinet / in_arp.c
CommitLineData
91447636 1/*
5ba3f43e 2 * Copyright (c) 2004-2017 Apple Inc. All rights reserved.
5d5c5d0d 3 *
2d21ac55 4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
39236c6e 5 *
2d21ac55
A
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.
39236c6e 14 *
2d21ac55
A
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
39236c6e 17 *
2d21ac55
A
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
8f6c56a5
A
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
2d21ac55
A
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.
39236c6e 25 *
2d21ac55 26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
91447636
A
27 */
28/*
29 * Copyright (c) 1982, 1989, 1993
30 * The Regents of the University of California. All rights reserved.
31 *
32 * Redistribution and use in source and binary forms, with or without
33 * modification, are permitted provided that the following conditions
34 * are met:
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.
47 *
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
58 * SUCH DAMAGE.
59 *
60 */
61
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>
69#include <sys/time.h>
70#include <sys/kernel.h>
71#include <sys/mbuf.h>
72#include <sys/sysctl.h>
6d2010ae
A
73#include <sys/mcache.h>
74#include <sys/protosw.h>
91447636
A
75#include <string.h>
76#include <net/if_arp.h>
77#include <net/if_dl.h>
78#include <net/dlil.h>
b7266188 79#include <net/if_types.h>
6d2010ae 80#include <net/if_llreach.h>
91447636 81#include <net/route.h>
5ba3f43e 82#include <net/nwk_wq.h>
39236c6e 83
91447636
A
84#include <netinet/if_ether.h>
85#include <netinet/in_var.h>
d9a64523
A
86#include <netinet/ip.h>
87#include <netinet/ip6.h>
b0d623f7 88#include <kern/zalloc.h>
91447636 89
39037602
A
90#include <kern/thread.h>
91#include <kern/sched_prim.h>
92
0a7de745 93#define CONST_LLADDR(s) ((const u_char*)((s)->sdl_data + (s)->sdl_nlen))
91447636
A
94
95static const size_t MAX_HW_LEN = 10;
96
b0d623f7
A
97/*
98 * Synchronization notes:
99 *
100 * The global list of ARP entries are stored in llinfo_arp; an entry
101 * gets inserted into the list when the route is created and gets
102 * removed from the list when it is deleted; this is done as part
103 * of RTM_ADD/RTM_RESOLVE/RTM_DELETE in arp_rtrequest().
104 *
105 * Because rnh_lock and rt_lock for the entry are held during those
106 * operations, the same locks (and thus lock ordering) must be used
107 * elsewhere to access the relevant data structure fields:
108 *
109 * la_le.{le_next,le_prev}, la_rt
110 *
111 * - Routing lock (rnh_lock)
112 *
39037602 113 * la_holdq, la_asked, la_llreach, la_lastused, la_flags
b0d623f7
A
114 *
115 * - Routing entry lock (rt_lock)
116 *
117 * Due to the dependency on rt_lock, llinfo_arp has the same lifetime
118 * as the route entry itself. When a route is deleted (RTM_DELETE),
119 * it is simply removed from the global list but the memory is not
120 * freed until the route itself is freed.
121 */
39236c6e
A
122struct llinfo_arp {
123 /*
124 * The following are protected by rnh_lock
125 */
126 LIST_ENTRY(llinfo_arp) la_le;
5ba3f43e 127 struct rtentry *la_rt;
39236c6e
A
128 /*
129 * The following are protected by rt_lock
130 */
5ba3f43e
A
131 class_queue_t la_holdq; /* packets awaiting resolution */
132 struct if_llreach *la_llreach; /* link-layer reachability record */
133 u_int64_t la_lastused; /* last used timestamp */
134 u_int32_t la_asked; /* # of requests sent */
135 u_int32_t la_maxtries; /* retry limit */
136 u_int64_t la_probeexp; /* probe deadline timestamp */
137 u_int32_t la_prbreq_cnt; /* probe request count */
39037602 138 u_int32_t la_flags;
5ba3f43e
A
139#define LLINFO_RTRFAIL_EVTSENT 0x1 /* sent an ARP event */
140#define LLINFO_PROBING 0x2 /* waiting for an ARP reply */
39236c6e 141};
5ba3f43e 142
91447636
A
143static LIST_HEAD(, llinfo_arp) llinfo_arp;
144
39037602 145static thread_call_t arp_timeout_tcall;
0a7de745 146static int arp_timeout_run; /* arp_timeout is scheduled to run */
39037602 147static void arp_timeout(thread_call_param_t arg0, thread_call_param_t arg1);
39236c6e
A
148static void arp_sched_timeout(struct timeval *);
149
39037602 150static thread_call_t arp_probe_tcall;
0a7de745 151static int arp_probe_run; /* arp_probe is scheduled to run */
39037602
A
152static void arp_probe(thread_call_param_t arg0, thread_call_param_t arg1);
153static void arp_sched_probe(struct timeval *);
154
39236c6e
A
155static void arptfree(struct llinfo_arp *, void *);
156static errno_t arp_lookup_route(const struct in_addr *, int,
157 int, route_t *, unsigned int);
158static int arp_getstat SYSCTL_HANDLER_ARGS;
159
160static struct llinfo_arp *arp_llinfo_alloc(int);
161static void arp_llinfo_free(void *);
39037602 162static uint32_t arp_llinfo_flushq(struct llinfo_arp *);
39236c6e
A
163static void arp_llinfo_purge(struct rtentry *);
164static void arp_llinfo_get_ri(struct rtentry *, struct rt_reach_info *);
165static void arp_llinfo_get_iflri(struct rtentry *, struct ifnet_llreach_info *);
3e170ce0 166static void arp_llinfo_refresh(struct rtentry *);
39236c6e
A
167
168static __inline void arp_llreach_use(struct llinfo_arp *);
169static __inline int arp_llreach_reachable(struct llinfo_arp *);
170static void arp_llreach_alloc(struct rtentry *, struct ifnet *, void *,
5ba3f43e 171 unsigned int, boolean_t, uint32_t *);
39236c6e
A
172
173extern int tvtohz(struct timeval *);
174
175static int arpinit_done;
176
177SYSCTL_DECL(_net_link_ether);
0a7de745 178SYSCTL_NODE(_net_link_ether, PF_INET, inet, CTLFLAG_RW | CTLFLAG_LOCKED, 0, "");
39236c6e 179
0a7de745 180static int arpt_prune = (5 * 60 * 1); /* walk list every 5 minutes */
39236c6e 181SYSCTL_INT(_net_link_ether_inet, OID_AUTO, prune_intvl,
0a7de745 182 CTLFLAG_RW | CTLFLAG_LOCKED, &arpt_prune, 0, "");
39236c6e 183
0a7de745 184#define ARP_PROBE_TIME 7 /* seconds */
39037602
A
185static u_int32_t arpt_probe = ARP_PROBE_TIME;
186SYSCTL_UINT(_net_link_ether_inet, OID_AUTO, probe_intvl,
0a7de745 187 CTLFLAG_RW | CTLFLAG_LOCKED, &arpt_probe, 0, "");
39037602 188
0a7de745 189static int arpt_keep = (20 * 60); /* once resolved, good for 20 more minutes */
39236c6e 190SYSCTL_INT(_net_link_ether_inet, OID_AUTO, max_age,
0a7de745 191 CTLFLAG_RW | CTLFLAG_LOCKED, &arpt_keep, 0, "");
39236c6e 192
0a7de745 193static int arpt_down = 20; /* once declared down, don't send for 20 sec */
39236c6e 194SYSCTL_INT(_net_link_ether_inet, OID_AUTO, host_down_time,
0a7de745 195 CTLFLAG_RW | CTLFLAG_LOCKED, &arpt_down, 0, "");
39236c6e 196
0a7de745 197static int arp_llreach_base = 120; /* seconds */
39236c6e 198SYSCTL_INT(_net_link_ether_inet, OID_AUTO, arp_llreach_base,
0a7de745
A
199 CTLFLAG_RW | CTLFLAG_LOCKED, &arp_llreach_base, 0,
200 "default ARP link-layer reachability max lifetime (in seconds)");
39236c6e 201
0a7de745 202#define ARP_UNICAST_LIMIT 3 /* # of probes until ARP refresh broadcast */
39236c6e
A
203static u_int32_t arp_unicast_lim = ARP_UNICAST_LIMIT;
204SYSCTL_INT(_net_link_ether_inet, OID_AUTO, arp_unicast_lim,
0a7de745
A
205 CTLFLAG_RW | CTLFLAG_LOCKED, &arp_unicast_lim, ARP_UNICAST_LIMIT,
206 "number of unicast ARP refresh probes before using broadcast");
91447636 207
6d2010ae 208static u_int32_t arp_maxtries = 5;
39236c6e 209SYSCTL_INT(_net_link_ether_inet, OID_AUTO, maxtries,
0a7de745 210 CTLFLAG_RW | CTLFLAG_LOCKED, &arp_maxtries, 0, "");
91447636 211
39037602
A
212static u_int32_t arp_maxhold = 16;
213SYSCTL_UINT(_net_link_ether_inet, OID_AUTO, maxhold,
0a7de745 214 CTLFLAG_RW | CTLFLAG_LOCKED, &arp_maxhold, 0, "");
39037602 215
0a7de745 216static int useloopback = 1; /* use loopback interface for local traffic */
39236c6e 217SYSCTL_INT(_net_link_ether_inet, OID_AUTO, useloopback,
0a7de745 218 CTLFLAG_RW | CTLFLAG_LOCKED, &useloopback, 0, "");
39236c6e
A
219
220static int arp_proxyall = 0;
221SYSCTL_INT(_net_link_ether_inet, OID_AUTO, proxyall,
0a7de745 222 CTLFLAG_RW | CTLFLAG_LOCKED, &arp_proxyall, 0, "");
39236c6e
A
223
224static int arp_sendllconflict = 0;
225SYSCTL_INT(_net_link_ether_inet, OID_AUTO, sendllconflict,
0a7de745 226 CTLFLAG_RW | CTLFLAG_LOCKED, &arp_sendllconflict, 0, "");
91447636 227
0a7de745 228static int log_arp_warnings = 0; /* Thread safe: no accumulated state */
6d2010ae 229SYSCTL_INT(_net_link_ether_inet, OID_AUTO, log_arp_warnings,
0a7de745
A
230 CTLFLAG_RW | CTLFLAG_LOCKED,
231 &log_arp_warnings, 0,
232 "log arp warning messages");
91447636 233
0a7de745 234static int keep_announcements = 1; /* Thread safe: no aging of state */
6d2010ae 235SYSCTL_INT(_net_link_ether_inet, OID_AUTO, keep_announcements,
0a7de745
A
236 CTLFLAG_RW | CTLFLAG_LOCKED,
237 &keep_announcements, 0,
238 "keep arp announcements");
2d21ac55 239
0a7de745 240static int send_conflicting_probes = 1; /* Thread safe: no accumulated state */
6d2010ae 241SYSCTL_INT(_net_link_ether_inet, OID_AUTO, send_conflicting_probes,
0a7de745
A
242 CTLFLAG_RW | CTLFLAG_LOCKED,
243 &send_conflicting_probes, 0,
244 "send conflicting link-local arp probes");
2d21ac55 245
39236c6e
A
246static int arp_verbose;
247SYSCTL_INT(_net_link_ether_inet, OID_AUTO, verbose,
0a7de745 248 CTLFLAG_RW | CTLFLAG_LOCKED, &arp_verbose, 0, "");
6d2010ae 249
39037602
A
250/*
251 * Generally protected by rnh_lock; use atomic operations on fields
252 * that are also modified outside of that lock (if needed).
253 */
0a7de745 254struct arpstat arpstat __attribute__((aligned(sizeof(uint64_t))));
fe8ab488 255SYSCTL_PROC(_net_link_ether_inet, OID_AUTO, stats,
0a7de745
A
256 CTLTYPE_STRUCT | CTLFLAG_RD | CTLFLAG_LOCKED,
257 0, 0, arp_getstat, "S,arpstat",
258 "ARP statistics (struct arpstat, net/if_arp.h)");
b0d623f7 259
b0d623f7 260static struct zone *llinfo_arp_zone;
0a7de745
A
261#define LLINFO_ARP_ZONE_MAX 256 /* maximum elements in zone */
262#define LLINFO_ARP_ZONE_NAME "llinfo_arp" /* name for zone */
b0d623f7
A
263
264void
265arp_init(void)
266{
39236c6e 267 VERIFY(!arpinit_done);
b0d623f7
A
268
269 LIST_INIT(&llinfo_arp);
270
0a7de745
A
271 llinfo_arp_zone = zinit(sizeof(struct llinfo_arp),
272 LLINFO_ARP_ZONE_MAX * sizeof(struct llinfo_arp), 0,
b0d623f7 273 LLINFO_ARP_ZONE_NAME);
0a7de745 274 if (llinfo_arp_zone == NULL) {
b0d623f7 275 panic("%s: failed allocating llinfo_arp_zone", __func__);
0a7de745 276 }
b0d623f7
A
277
278 zone_change(llinfo_arp_zone, Z_EXPAND, TRUE);
6d2010ae 279 zone_change(llinfo_arp_zone, Z_CALLERACCT, FALSE);
b0d623f7
A
280
281 arpinit_done = 1;
b0d623f7
A
282}
283
284static struct llinfo_arp *
39236c6e 285arp_llinfo_alloc(int how)
b0d623f7 286{
39236c6e
A
287 struct llinfo_arp *la;
288
289 la = (how == M_WAITOK) ? zalloc(llinfo_arp_zone) :
290 zalloc_noblock(llinfo_arp_zone);
39037602 291 if (la != NULL) {
0a7de745 292 bzero(la, sizeof(*la));
39037602
A
293 /*
294 * The type of queue (Q_DROPHEAD) here is just a hint;
295 * the actual logic that works on this queue performs
296 * a head drop, details in arp_llinfo_addq().
297 */
298 _qinit(&la->la_holdq, Q_DROPHEAD, (arp_maxhold == 0) ?
5ba3f43e 299 (uint32_t)-1 : arp_maxhold, QP_MBUF);
39037602 300 }
39236c6e 301
0a7de745 302 return la;
b0d623f7
A
303}
304
305static void
306arp_llinfo_free(void *arg)
307{
308 struct llinfo_arp *la = arg;
309
310 if (la->la_le.le_next != NULL || la->la_le.le_prev != NULL) {
311 panic("%s: trying to free %p when it is in use", __func__, la);
312 /* NOTREACHED */
313 }
314
39037602
A
315 /* Free any held packets */
316 (void) arp_llinfo_flushq(la);
b0d623f7 317
6d2010ae
A
318 /* Purge any link-layer info caching */
319 VERIFY(la->la_rt->rt_llinfo == la);
0a7de745 320 if (la->la_rt->rt_llinfo_purge != NULL) {
6d2010ae 321 la->la_rt->rt_llinfo_purge(la->la_rt);
0a7de745 322 }
6d2010ae 323
b0d623f7
A
324 zfree(llinfo_arp_zone, la);
325}
326
39037602
A
327static void
328arp_llinfo_addq(struct llinfo_arp *la, struct mbuf *m)
329{
330 if (qlen(&la->la_holdq) >= qlimit(&la->la_holdq)) {
331 struct mbuf *_m;
332 /* prune less than CTL, else take what's at the head */
333 _m = _getq_scidx_lt(&la->la_holdq, SCIDX_CTL);
0a7de745 334 if (_m == NULL) {
39037602 335 _m = _getq(&la->la_holdq);
0a7de745 336 }
39037602
A
337 VERIFY(_m != NULL);
338 if (arp_verbose) {
339 log(LOG_DEBUG, "%s: dropping packet (scidx %u)\n",
340 __func__, MBUF_SCIDX(mbuf_get_service_class(_m)));
341 }
342 m_freem(_m);
343 atomic_add_32(&arpstat.dropped, 1);
344 atomic_add_32(&arpstat.held, -1);
345 }
346 _addq(&la->la_holdq, m);
347 atomic_add_32(&arpstat.held, 1);
348 if (arp_verbose) {
349 log(LOG_DEBUG, "%s: enqueued packet (scidx %u), qlen now %u\n",
350 __func__, MBUF_SCIDX(mbuf_get_service_class(m)),
351 qlen(&la->la_holdq));
352 }
353}
354
355static uint32_t
356arp_llinfo_flushq(struct llinfo_arp *la)
357{
358 uint32_t held = qlen(&la->la_holdq);
359
5ba3f43e
A
360 if (held != 0) {
361 atomic_add_32(&arpstat.purged, held);
362 atomic_add_32(&arpstat.held, -held);
363 _flushq(&la->la_holdq);
364 }
365 la->la_prbreq_cnt = 0;
39037602 366 VERIFY(qempty(&la->la_holdq));
0a7de745 367 return held;
39037602
A
368}
369
6d2010ae
A
370static void
371arp_llinfo_purge(struct rtentry *rt)
372{
373 struct llinfo_arp *la = rt->rt_llinfo;
374
375 RT_LOCK_ASSERT_HELD(rt);
376 VERIFY(rt->rt_llinfo_purge == arp_llinfo_purge && la != NULL);
377
378 if (la->la_llreach != NULL) {
379 RT_CONVERT_LOCK(rt);
380 ifnet_llreach_free(la->la_llreach);
381 la->la_llreach = NULL;
382 }
383 la->la_lastused = 0;
384}
385
386static void
387arp_llinfo_get_ri(struct rtentry *rt, struct rt_reach_info *ri)
388{
389 struct llinfo_arp *la = rt->rt_llinfo;
390 struct if_llreach *lr = la->la_llreach;
391
392 if (lr == NULL) {
0a7de745 393 bzero(ri, sizeof(*ri));
316670eb
A
394 ri->ri_rssi = IFNET_RSSI_UNKNOWN;
395 ri->ri_lqm = IFNET_LQM_THRESH_OFF;
396 ri->ri_npm = IFNET_NPM_THRESH_UNKNOWN;
6d2010ae
A
397 } else {
398 IFLR_LOCK(lr);
399 /* Export to rt_reach_info structure */
400 ifnet_lr2ri(lr, ri);
316670eb
A
401 /* Export ARP send expiration (calendar) time */
402 ri->ri_snd_expire =
403 ifnet_llreach_up2calexp(lr, la->la_lastused);
404 IFLR_UNLOCK(lr);
405 }
406}
407
408static void
409arp_llinfo_get_iflri(struct rtentry *rt, struct ifnet_llreach_info *iflri)
410{
411 struct llinfo_arp *la = rt->rt_llinfo;
412 struct if_llreach *lr = la->la_llreach;
413
414 if (lr == NULL) {
0a7de745 415 bzero(iflri, sizeof(*iflri));
316670eb
A
416 iflri->iflri_rssi = IFNET_RSSI_UNKNOWN;
417 iflri->iflri_lqm = IFNET_LQM_THRESH_OFF;
418 iflri->iflri_npm = IFNET_NPM_THRESH_UNKNOWN;
419 } else {
420 IFLR_LOCK(lr);
421 /* Export to ifnet_llreach_info structure */
422 ifnet_lr2iflri(lr, iflri);
423 /* Export ARP send expiration (uptime) time */
424 iflri->iflri_snd_expire =
425 ifnet_llreach_up2upexp(lr, la->la_lastused);
6d2010ae
A
426 IFLR_UNLOCK(lr);
427 }
428}
429
3e170ce0
A
430static void
431arp_llinfo_refresh(struct rtentry *rt)
432{
433 uint64_t timenow = net_uptime();
434 /*
435 * If route entry is permanent or if expiry is less
436 * than timenow and extra time taken for unicast probe
437 * we can't expedite the refresh
438 */
439 if ((rt->rt_expire == 0) ||
440 (rt->rt_flags & RTF_STATIC) ||
441 !(rt->rt_flags & RTF_LLINFO)) {
442 return;
443 }
444
0a7de745 445 if (rt->rt_expire > timenow) {
39037602 446 rt->rt_expire = timenow;
0a7de745 447 }
3e170ce0
A
448 return;
449}
450
6d2010ae
A
451void
452arp_llreach_set_reachable(struct ifnet *ifp, void *addr, unsigned int alen)
453{
454 /* Nothing more to do if it's disabled */
0a7de745 455 if (arp_llreach_base == 0) {
6d2010ae 456 return;
0a7de745 457 }
6d2010ae
A
458
459 ifnet_llreach_set_reachable(ifp, ETHERTYPE_IP, addr, alen);
460}
461
462static __inline void
463arp_llreach_use(struct llinfo_arp *la)
464{
0a7de745 465 if (la->la_llreach != NULL) {
6d2010ae 466 la->la_lastused = net_uptime();
0a7de745 467 }
6d2010ae
A
468}
469
470static __inline int
471arp_llreach_reachable(struct llinfo_arp *la)
472{
473 struct if_llreach *lr;
474 const char *why = NULL;
475
476 /* Nothing more to do if it's disabled; pretend it's reachable */
0a7de745
A
477 if (arp_llreach_base == 0) {
478 return 1;
479 }
6d2010ae
A
480
481 if ((lr = la->la_llreach) == NULL) {
482 /*
483 * Link-layer reachability record isn't present for this
484 * ARP entry; pretend it's reachable and use it as is.
485 */
0a7de745 486 return 1;
6d2010ae
A
487 } else if (ifnet_llreach_reachable(lr)) {
488 /*
489 * Record is present, it's not shared with other ARP
490 * entries and a packet has recently been received
491 * from the remote host; consider it reachable.
492 */
0a7de745
A
493 if (lr->lr_reqcnt == 1) {
494 return 1;
495 }
6d2010ae
A
496
497 /* Prime it up, if this is the first time */
498 if (la->la_lastused == 0) {
499 VERIFY(la->la_llreach != NULL);
500 arp_llreach_use(la);
501 }
502
503 /*
504 * Record is present and shared with one or more ARP
505 * entries, and a packet has recently been received
506 * from the remote host. Since it's shared by more
507 * than one IP addresses, we can't rely on the link-
508 * layer reachability alone; consider it reachable if
509 * this ARP entry has been used "recently."
510 */
0a7de745
A
511 if (ifnet_llreach_reachable_delta(lr, la->la_lastused)) {
512 return 1;
513 }
6d2010ae
A
514
515 why = "has alias(es) and hasn't been used in a while";
516 } else {
517 why = "haven't heard from it in a while";
518 }
519
39236c6e 520 if (arp_verbose > 1) {
6d2010ae
A
521 char tmp[MAX_IPv4_STR_LEN];
522 u_int64_t now = net_uptime();
523
39236c6e 524 log(LOG_DEBUG, "%s: ARP probe(s) needed for %s; "
6d2010ae 525 "%s [lastused %lld, lastrcvd %lld] secs ago\n",
39236c6e 526 if_name(lr->lr_ifp), inet_ntop(AF_INET,
0a7de745 527 &SIN(rt_key(la->la_rt))->sin_addr, tmp, sizeof(tmp)), why,
39236c6e
A
528 (la->la_lastused ? (int64_t)(now - la->la_lastused) : -1),
529 (lr->lr_lastrcvd ? (int64_t)(now - lr->lr_lastrcvd) : -1));
6d2010ae 530 }
0a7de745 531 return 0;
6d2010ae
A
532}
533
534/*
535 * Obtain a link-layer source cache entry for the sender.
536 *
537 * NOTE: This is currently only for ARP/Ethernet.
538 */
539static void
540arp_llreach_alloc(struct rtentry *rt, struct ifnet *ifp, void *addr,
5ba3f43e 541 unsigned int alen, boolean_t solicited, uint32_t *p_rt_event_code)
6d2010ae
A
542{
543 VERIFY(rt->rt_expire == 0 || rt->rt_rmx.rmx_expire != 0);
544 VERIFY(rt->rt_expire != 0 || rt->rt_rmx.rmx_expire == 0);
39236c6e
A
545
546 if (arp_llreach_base != 0 && rt->rt_expire != 0 &&
547 !(rt->rt_ifp->if_flags & IFF_LOOPBACK) &&
0a7de745 548 ifp->if_addrlen == IF_LLREACH_MAXLEN && /* Ethernet */
6d2010ae
A
549 alen == ifp->if_addrlen) {
550 struct llinfo_arp *la = rt->rt_llinfo;
551 struct if_llreach *lr;
552 const char *why = NULL, *type = "";
553
554 /* Become a regular mutex, just in case */
555 RT_CONVERT_LOCK(rt);
556
557 if ((lr = la->la_llreach) != NULL) {
558 type = (solicited ? "ARP reply" : "ARP announcement");
559 /*
560 * If target has changed, create a new record;
561 * otherwise keep existing record.
562 */
563 IFLR_LOCK(lr);
564 if (bcmp(addr, lr->lr_key.addr, alen) != 0) {
565 IFLR_UNLOCK(lr);
566 /* Purge any link-layer info caching */
567 VERIFY(rt->rt_llinfo_purge != NULL);
568 rt->rt_llinfo_purge(rt);
569 lr = NULL;
570 why = " for different target HW address; "
571 "using new llreach record";
5ba3f43e 572 *p_rt_event_code = ROUTE_LLENTRY_CHANGED;
6d2010ae 573 } else {
5ba3f43e
A
574 /*
575 * If we were doing unicast probing, we need to
576 * deliver an event for neighbor cache resolution
577 */
0a7de745 578 if (lr->lr_probes != 0) {
5ba3f43e 579 *p_rt_event_code = ROUTE_LLENTRY_RESOLVED;
0a7de745 580 }
5ba3f43e 581
0a7de745 582 lr->lr_probes = 0; /* reset probe count */
6d2010ae
A
583 IFLR_UNLOCK(lr);
584 if (solicited) {
585 why = " for same target HW address; "
586 "keeping existing llreach record";
587 }
588 }
589 }
590
591 if (lr == NULL) {
592 lr = la->la_llreach = ifnet_llreach_alloc(ifp,
593 ETHERTYPE_IP, addr, alen, arp_llreach_base);
594 if (lr != NULL) {
0a7de745
A
595 lr->lr_probes = 0; /* reset probe count */
596 if (why == NULL) {
6d2010ae 597 why = "creating new llreach record";
0a7de745 598 }
6d2010ae 599 }
5ba3f43e 600 *p_rt_event_code = ROUTE_LLENTRY_RESOLVED;
6d2010ae
A
601 }
602
39236c6e 603 if (arp_verbose > 1 && lr != NULL && why != NULL) {
6d2010ae
A
604 char tmp[MAX_IPv4_STR_LEN];
605
39236c6e
A
606 log(LOG_DEBUG, "%s: %s%s for %s\n", if_name(ifp),
607 type, why, inet_ntop(AF_INET,
0a7de745 608 &SIN(rt_key(rt))->sin_addr, tmp, sizeof(tmp)));
6d2010ae
A
609 }
610 }
611}
612
39236c6e 613struct arptf_arg {
39037602
A
614 boolean_t draining;
615 boolean_t probing;
39236c6e
A
616 uint32_t killed;
617 uint32_t aging;
618 uint32_t sticky;
619 uint32_t found;
39037602
A
620 uint32_t qlen;
621 uint32_t qsize;
39236c6e
A
622};
623
91447636
A
624/*
625 * Free an arp entry.
626 */
627static void
39236c6e 628arptfree(struct llinfo_arp *la, void *arg)
91447636 629{
39236c6e 630 struct arptf_arg *ap = arg;
91447636 631 struct rtentry *rt = la->la_rt;
39037602 632 uint64_t timenow;
b0d623f7 633
5ba3f43e 634 LCK_MTX_ASSERT(rnh_lock, LCK_MTX_ASSERT_OWNED);
b0d623f7 635
39236c6e
A
636 /* rnh_lock acquired by caller protects rt from going away */
637 RT_LOCK(rt);
638
639 VERIFY(rt->rt_expire == 0 || rt->rt_rmx.rmx_expire != 0);
640 VERIFY(rt->rt_expire != 0 || rt->rt_rmx.rmx_expire == 0);
641
642 ap->found++;
39037602
A
643 timenow = net_uptime();
644
645 /* If we're probing, flush out held packets upon probe expiration */
646 if (ap->probing && (la->la_flags & LLINFO_PROBING) &&
647 la->la_probeexp <= timenow) {
648 struct sockaddr_dl *sdl = SDL(rt->rt_gateway);
0a7de745 649 if (sdl != NULL) {
39037602 650 sdl->sdl_alen = 0;
0a7de745 651 }
39037602 652 (void) arp_llinfo_flushq(la);
5ba3f43e
A
653 /*
654 * Enqueue work item to invoke callback for this route entry
655 */
656 route_event_enqueue_nwk_wq_entry(rt, NULL,
657 ROUTE_LLENTRY_UNREACH, NULL, TRUE);
39037602
A
658 }
659
5ba3f43e
A
660 /*
661 * The following is mostly being used to arm the timer
662 * again and for logging.
663 * qlen is used to re-arm the timer. Therefore, pure probe
664 * requests can be considered as 0 length packets
665 * contributing only to length but not to the size.
666 */
39037602 667 ap->qlen += qlen(&la->la_holdq);
5ba3f43e 668 ap->qlen += la->la_prbreq_cnt;
39037602
A
669 ap->qsize += qsize(&la->la_holdq);
670
39236c6e
A
671 if (rt->rt_expire == 0 || (rt->rt_flags & RTF_STATIC)) {
672 ap->sticky++;
673 /* ARP entry is permanent? */
15129b1c 674 if (rt->rt_expire == 0) {
39236c6e
A
675 RT_UNLOCK(rt);
676 return;
677 }
678 }
679
680 /* ARP entry hasn't expired and we're not draining? */
39037602 681 if (!ap->draining && rt->rt_expire > timenow) {
b0d623f7 682 RT_UNLOCK(rt);
39236c6e
A
683 ap->aging++;
684 return;
685 }
686
687 if (rt->rt_refcnt > 0) {
6d2010ae 688 /*
39236c6e
A
689 * ARP entry has expired, with outstanding refcnt.
690 * If we're not draining, force ARP query to be
691 * generated next time this entry is used.
6d2010ae 692 */
39037602 693 if (!ap->draining && !ap->probing) {
39236c6e 694 struct sockaddr_dl *sdl = SDL(rt->rt_gateway);
0a7de745 695 if (sdl != NULL) {
39236c6e 696 sdl->sdl_alen = 0;
0a7de745 697 }
39236c6e
A
698 la->la_asked = 0;
699 rt->rt_flags &= ~RTF_REJECT;
700 }
6d2010ae 701 RT_UNLOCK(rt);
39037602 702 } else if (!(rt->rt_flags & RTF_STATIC) && !ap->probing) {
b0d623f7 703 /*
39236c6e
A
704 * ARP entry has no outstanding refcnt, and we're either
705 * draining or it has expired; delete it from the routing
706 * table. Safe to drop rt_lock and use rt_key, since holding
b0d623f7
A
707 * rnh_lock here prevents another thread from calling
708 * rt_setgate() on this route.
709 */
710 RT_UNLOCK(rt);
39236c6e
A
711 rtrequest_locked(RTM_DELETE, rt_key(rt), NULL,
712 rt_mask(rt), 0, NULL);
713 arpstat.timeouts++;
714 ap->killed++;
715 } else {
716 /* ARP entry is static; let it linger */
717 RT_UNLOCK(rt);
91447636 718 }
91447636
A
719}
720
d1ecb069 721void
39236c6e 722in_arpdrain(void *arg)
91447636 723{
39236c6e 724#pragma unused(arg)
0c530ab8 725 struct llinfo_arp *la, *ola;
39236c6e
A
726 struct arptf_arg farg;
727
0a7de745 728 if (arp_verbose) {
39236c6e 729 log(LOG_DEBUG, "%s: draining ARP entries\n", __func__);
0a7de745 730 }
91447636 731
b0d623f7 732 lck_mtx_lock(rnh_lock);
0c530ab8 733 la = llinfo_arp.lh_first;
0a7de745 734 bzero(&farg, sizeof(farg));
39037602 735 farg.draining = TRUE;
39236c6e 736 while ((ola = la) != NULL) {
91447636 737 la = la->la_le.le_next;
39236c6e
A
738 arptfree(ola, &farg);
739 }
740 if (arp_verbose) {
39037602
A
741 log(LOG_DEBUG, "%s: found %u, aging %u, sticky %u, killed %u; "
742 "%u pkts held (%u bytes)\n", __func__, farg.found,
743 farg.aging, farg.sticky, farg.killed, farg.qlen,
744 farg.qsize);
91447636 745 }
b0d623f7 746 lck_mtx_unlock(rnh_lock);
d1ecb069
A
747}
748
39236c6e
A
749/*
750 * Timeout routine. Age arp_tab entries periodically.
751 */
752static void
39037602 753arp_timeout(thread_call_param_t arg0, thread_call_param_t arg1)
6d2010ae 754{
39037602 755#pragma unused(arg0, arg1)
39236c6e
A
756 struct llinfo_arp *la, *ola;
757 struct timeval atv;
758 struct arptf_arg farg;
6d2010ae 759
39236c6e
A
760 lck_mtx_lock(rnh_lock);
761 la = llinfo_arp.lh_first;
0a7de745 762 bzero(&farg, sizeof(farg));
39236c6e
A
763 while ((ola = la) != NULL) {
764 la = la->la_le.le_next;
765 arptfree(ola, &farg);
766 }
767 if (arp_verbose) {
39037602
A
768 log(LOG_DEBUG, "%s: found %u, aging %u, sticky %u, killed %u; "
769 "%u pkts held (%u bytes)\n", __func__, farg.found,
770 farg.aging, farg.sticky, farg.killed, farg.qlen,
771 farg.qsize);
39236c6e
A
772 }
773 atv.tv_usec = 0;
39037602 774 atv.tv_sec = MAX(arpt_prune, 5);
39236c6e
A
775 /* re-arm the timer if there's work to do */
776 arp_timeout_run = 0;
0a7de745 777 if (farg.aging > 0) {
39236c6e 778 arp_sched_timeout(&atv);
0a7de745 779 } else if (arp_verbose) {
39236c6e 780 log(LOG_DEBUG, "%s: not rescheduling timer\n", __func__);
0a7de745 781 }
39236c6e 782 lck_mtx_unlock(rnh_lock);
6d2010ae
A
783}
784
d1ecb069 785static void
39236c6e 786arp_sched_timeout(struct timeval *atv)
d1ecb069 787{
5ba3f43e 788 LCK_MTX_ASSERT(rnh_lock, LCK_MTX_ASSERT_OWNED);
39236c6e
A
789
790 if (!arp_timeout_run) {
791 struct timeval tv;
39037602
A
792 uint64_t deadline = 0;
793
794 if (arp_timeout_tcall == NULL) {
795 arp_timeout_tcall =
796 thread_call_allocate(arp_timeout, NULL);
797 VERIFY(arp_timeout_tcall != NULL);
798 }
39236c6e
A
799
800 if (atv == NULL) {
801 tv.tv_usec = 0;
802 tv.tv_sec = MAX(arpt_prune / 5, 1);
803 atv = &tv;
804 }
805 if (arp_verbose) {
806 log(LOG_DEBUG, "%s: timer scheduled in "
807 "T+%llus.%lluu\n", __func__,
808 (uint64_t)atv->tv_sec, (uint64_t)atv->tv_usec);
809 }
810 arp_timeout_run = 1;
39037602
A
811
812 clock_deadline_for_periodic_event(atv->tv_sec * NSEC_PER_SEC,
813 mach_absolute_time(), &deadline);
814 (void) thread_call_enter_delayed(arp_timeout_tcall, deadline);
815 }
816}
817
818/*
819 * Probe routine.
820 */
821static void
822arp_probe(thread_call_param_t arg0, thread_call_param_t arg1)
823{
824#pragma unused(arg0, arg1)
825 struct llinfo_arp *la, *ola;
826 struct timeval atv;
827 struct arptf_arg farg;
828
829 lck_mtx_lock(rnh_lock);
830 la = llinfo_arp.lh_first;
0a7de745 831 bzero(&farg, sizeof(farg));
39037602
A
832 farg.probing = TRUE;
833 while ((ola = la) != NULL) {
834 la = la->la_le.le_next;
835 arptfree(ola, &farg);
836 }
837 if (arp_verbose) {
838 log(LOG_DEBUG, "%s: found %u, aging %u, sticky %u, killed %u; "
839 "%u pkts held (%u bytes)\n", __func__, farg.found,
840 farg.aging, farg.sticky, farg.killed, farg.qlen,
841 farg.qsize);
842 }
843 atv.tv_usec = 0;
844 atv.tv_sec = MAX(arpt_probe, ARP_PROBE_TIME);
845 /* re-arm the probe if there's work to do */
846 arp_probe_run = 0;
0a7de745 847 if (farg.qlen > 0) {
39037602 848 arp_sched_probe(&atv);
0a7de745 849 } else if (arp_verbose) {
39037602 850 log(LOG_DEBUG, "%s: not rescheduling probe\n", __func__);
0a7de745 851 }
39037602
A
852 lck_mtx_unlock(rnh_lock);
853}
854
855static void
856arp_sched_probe(struct timeval *atv)
857{
5ba3f43e 858 LCK_MTX_ASSERT(rnh_lock, LCK_MTX_ASSERT_OWNED);
39037602
A
859
860 if (!arp_probe_run) {
861 struct timeval tv;
862 uint64_t deadline = 0;
863
864 if (arp_probe_tcall == NULL) {
865 arp_probe_tcall =
866 thread_call_allocate(arp_probe, NULL);
867 VERIFY(arp_probe_tcall != NULL);
868 }
869
870 if (atv == NULL) {
871 tv.tv_usec = 0;
872 tv.tv_sec = MAX(arpt_probe, ARP_PROBE_TIME);
873 atv = &tv;
874 }
875 if (arp_verbose) {
876 log(LOG_DEBUG, "%s: probe scheduled in "
877 "T+%llus.%lluu\n", __func__,
878 (uint64_t)atv->tv_sec, (uint64_t)atv->tv_usec);
879 }
880 arp_probe_run = 1;
881
882 clock_deadline_for_periodic_event(atv->tv_sec * NSEC_PER_SEC,
883 mach_absolute_time(), &deadline);
884 (void) thread_call_enter_delayed(arp_probe_tcall, deadline);
39236c6e 885 }
91447636
A
886}
887
888/*
39236c6e 889 * ifa_rtrequest() callback
91447636
A
890 */
891static void
39236c6e 892arp_rtrequest(int req, struct rtentry *rt, struct sockaddr *sa)
91447636 893{
39236c6e 894#pragma unused(sa)
91447636 895 struct sockaddr *gate = rt->rt_gateway;
b0d623f7 896 struct llinfo_arp *la = rt->rt_llinfo;
39236c6e 897 static struct sockaddr_dl null_sdl =
0a7de745 898 { .sdl_len = sizeof(null_sdl), .sdl_family = AF_LINK };
6d2010ae 899 uint64_t timenow;
39236c6e 900 char buf[MAX_IPv4_STR_LEN];
91447636 901
39236c6e 902 VERIFY(arpinit_done);
5ba3f43e 903 LCK_MTX_ASSERT(rnh_lock, LCK_MTX_ASSERT_OWNED);
b0d623f7 904 RT_LOCK_ASSERT_HELD(rt);
91447636 905
0a7de745 906 if (rt->rt_flags & RTF_GATEWAY) {
91447636 907 return;
0a7de745 908 }
39236c6e 909
6d2010ae 910 timenow = net_uptime();
91447636 911 switch (req) {
91447636
A
912 case RTM_ADD:
913 /*
914 * XXX: If this is a manually added route to interface
915 * such as older version of routed or gated might provide,
916 * restore cloning bit.
917 */
39236c6e 918 if (!(rt->rt_flags & RTF_HOST) && rt_mask(rt) != NULL &&
0a7de745 919 SIN(rt_mask(rt))->sin_addr.s_addr != INADDR_BROADCAST) {
91447636 920 rt->rt_flags |= RTF_CLONING;
0a7de745 921 }
39236c6e 922
91447636
A
923 if (rt->rt_flags & RTF_CLONING) {
924 /*
925 * Case 1: This route should come from a route to iface.
926 */
39236c6e 927 if (rt_setgate(rt, rt_key(rt), SA(&null_sdl)) == 0) {
b0d623f7
A
928 gate = rt->rt_gateway;
929 SDL(gate)->sdl_type = rt->rt_ifp->if_type;
930 SDL(gate)->sdl_index = rt->rt_ifp->if_index;
931 /*
932 * In case we're called before 1.0 sec.
933 * has elapsed.
934 */
6d2010ae 935 rt_setexpire(rt, MAX(timenow, 1));
b0d623f7 936 }
91447636
A
937 break;
938 }
939 /* Announce a new entry if requested. */
b0d623f7 940 if (rt->rt_flags & RTF_ANNOUNCE) {
0a7de745 941 if (la != NULL) {
6d2010ae 942 arp_llreach_use(la); /* Mark use timestamp */
0a7de745 943 }
b0d623f7
A
944 RT_UNLOCK(rt);
945 dlil_send_arp(rt->rt_ifp, ARPOP_REQUEST,
316670eb 946 SDL(gate), rt_key(rt), NULL, rt_key(rt), 0);
b0d623f7 947 RT_LOCK(rt);
39236c6e 948 arpstat.txannounces++;
b0d623f7 949 }
0a7de745 950 /* FALLTHRU */
91447636
A
951 case RTM_RESOLVE:
952 if (gate->sa_family != AF_LINK ||
0a7de745 953 gate->sa_len < sizeof(null_sdl)) {
39236c6e
A
954 arpstat.invalidreqs++;
955 log(LOG_ERR, "%s: route to %s has bad gateway address "
956 "(sa_family %u sa_len %u) on %s\n",
957 __func__, inet_ntop(AF_INET,
958 &SIN(rt_key(rt))->sin_addr.s_addr, buf,
0a7de745 959 sizeof(buf)), gate->sa_family, gate->sa_len,
39236c6e 960 if_name(rt->rt_ifp));
91447636
A
961 break;
962 }
963 SDL(gate)->sdl_type = rt->rt_ifp->if_type;
964 SDL(gate)->sdl_index = rt->rt_ifp->if_index;
39236c6e 965
0a7de745 966 if (la != NULL) {
91447636 967 break; /* This happens on a route change */
0a7de745 968 }
91447636
A
969 /*
970 * Case 2: This route may come from cloning, or a manual route
971 * add with a LL address.
972 */
39236c6e 973 rt->rt_llinfo = la = arp_llinfo_alloc(M_WAITOK);
b0d623f7 974 if (la == NULL) {
39236c6e 975 arpstat.reqnobufs++;
91447636
A
976 break;
977 }
0a7de745
A
978 rt->rt_llinfo_get_ri = arp_llinfo_get_ri;
979 rt->rt_llinfo_get_iflri = arp_llinfo_get_iflri;
980 rt->rt_llinfo_purge = arp_llinfo_purge;
981 rt->rt_llinfo_free = arp_llinfo_free;
3e170ce0 982 rt->rt_llinfo_refresh = arp_llinfo_refresh;
91447636 983 rt->rt_flags |= RTF_LLINFO;
39236c6e 984 la->la_rt = rt;
91447636 985 LIST_INSERT_HEAD(&llinfo_arp, la, la_le);
39236c6e
A
986 arpstat.inuse++;
987
988 /* We have at least one entry; arm the timer if not already */
989 arp_sched_timeout(NULL);
91447636 990
91447636
A
991 /*
992 * This keeps the multicast addresses from showing up
993 * in `arp -a' listings as unresolved. It's not actually
6d2010ae
A
994 * functional. Then the same for broadcast. For IPv4
995 * link-local address, keep the entry around even after
996 * it has expired.
91447636
A
997 */
998 if (IN_MULTICAST(ntohl(SIN(rt_key(rt))->sin_addr.s_addr))) {
b0d623f7
A
999 RT_UNLOCK(rt);
1000 dlil_resolve_multi(rt->rt_ifp, rt_key(rt), gate,
0a7de745 1001 sizeof(struct sockaddr_dl));
b0d623f7 1002 RT_LOCK(rt);
6d2010ae 1003 rt_setexpire(rt, 0);
39236c6e
A
1004 } else if (in_broadcast(SIN(rt_key(rt))->sin_addr,
1005 rt->rt_ifp)) {
1006 struct sockaddr_dl *gate_ll = SDL(gate);
1007 size_t broadcast_len;
b0d623f7 1008 ifnet_llbroadcast_copy_bytes(rt->rt_ifp,
0a7de745 1009 LLADDR(gate_ll), sizeof(gate_ll->sdl_data),
b0d623f7 1010 &broadcast_len);
91447636
A
1011 gate_ll->sdl_alen = broadcast_len;
1012 gate_ll->sdl_family = AF_LINK;
0a7de745 1013 gate_ll->sdl_len = sizeof(struct sockaddr_dl);
593a1d5f 1014 /* In case we're called before 1.0 sec. has elapsed */
6d2010ae 1015 rt_setexpire(rt, MAX(timenow, 1));
39236c6e
A
1016 } else if (IN_LINKLOCAL(ntohl(SIN(rt_key(rt))->
1017 sin_addr.s_addr))) {
6d2010ae 1018 rt->rt_flags |= RTF_STATIC;
91447636 1019 }
91447636 1020
39236c6e
A
1021 /* Set default maximum number of retries */
1022 la->la_maxtries = arp_maxtries;
1023
6d2010ae
A
1024 /* Become a regular mutex, just in case */
1025 RT_CONVERT_LOCK(rt);
1026 IFA_LOCK_SPIN(rt->rt_ifa);
91447636
A
1027 if (SIN(rt_key(rt))->sin_addr.s_addr ==
1028 (IA_SIN(rt->rt_ifa))->sin_addr.s_addr) {
6d2010ae
A
1029 IFA_UNLOCK(rt->rt_ifa);
1030 /*
1031 * This test used to be
1032 * if (loif.if_flags & IFF_UP)
1033 * It allowed local traffic to be forced through the
1034 * hardware by configuring the loopback down. However,
1035 * it causes problems during network configuration
1036 * for boards that can't receive packets they send.
1037 * It is now necessary to clear "useloopback" and
1038 * remove the route to force traffic out to the
1039 * hardware.
1040 */
1041 rt_setexpire(rt, 0);
1042 ifnet_lladdr_copy_bytes(rt->rt_ifp, LLADDR(SDL(gate)),
1043 SDL(gate)->sdl_alen = rt->rt_ifp->if_addrlen);
d1ecb069 1044 if (useloopback) {
6d2010ae
A
1045 if (rt->rt_ifp != lo_ifp) {
1046 /*
1047 * Purge any link-layer info caching.
1048 */
0a7de745 1049 if (rt->rt_llinfo_purge != NULL) {
6d2010ae 1050 rt->rt_llinfo_purge(rt);
0a7de745 1051 }
6d2010ae
A
1052
1053 /*
1054 * Adjust route ref count for the
1055 * interfaces.
1056 */
1057 if (rt->rt_if_ref_fn != NULL) {
1058 rt->rt_if_ref_fn(lo_ifp, 1);
1059 rt->rt_if_ref_fn(rt->rt_ifp, -1);
1060 }
d1ecb069 1061 }
2d21ac55 1062 rt->rt_ifp = lo_ifp;
39236c6e
A
1063 /*
1064 * If rmx_mtu is not locked, update it
1065 * to the MTU used by the new interface.
1066 */
0a7de745 1067 if (!(rt->rt_rmx.rmx_locks & RTV_MTU)) {
39236c6e 1068 rt->rt_rmx.rmx_mtu = rt->rt_ifp->if_mtu;
0a7de745 1069 }
d1ecb069 1070 }
6d2010ae
A
1071 } else {
1072 IFA_UNLOCK(rt->rt_ifa);
91447636
A
1073 }
1074 break;
1075
1076 case RTM_DELETE:
0a7de745 1077 if (la == NULL) {
91447636 1078 break;
0a7de745 1079 }
b0d623f7
A
1080 /*
1081 * Unchain it but defer the actual freeing until the route
1082 * itself is to be freed. rt->rt_llinfo still points to
1083 * llinfo_arp, and likewise, la->la_rt still points to this
1084 * route entry, except that RTF_LLINFO is now cleared.
1085 */
91447636 1086 LIST_REMOVE(la, la_le);
b0d623f7
A
1087 la->la_le.le_next = NULL;
1088 la->la_le.le_prev = NULL;
39236c6e 1089 arpstat.inuse--;
6d2010ae
A
1090
1091 /*
1092 * Purge any link-layer info caching.
1093 */
0a7de745 1094 if (rt->rt_llinfo_purge != NULL) {
6d2010ae 1095 rt->rt_llinfo_purge(rt);
0a7de745 1096 }
6d2010ae 1097
91447636 1098 rt->rt_flags &= ~RTF_LLINFO;
39037602 1099 (void) arp_llinfo_flushq(la);
91447636
A
1100 }
1101}
1102
1103/*
1104 * convert hardware address to hex string for logging errors.
1105 */
1106static const char *
39236c6e 1107sdl_addr_to_hex(const struct sockaddr_dl *sdl, char *orig_buf, int buflen)
91447636 1108{
39236c6e
A
1109 char *buf = orig_buf;
1110 int i;
1111 const u_char *lladdr = (u_char *)(size_t)sdl->sdl_data;
1112 int maxbytes = buflen / 3;
1113
91447636
A
1114 if (maxbytes > sdl->sdl_alen) {
1115 maxbytes = sdl->sdl_alen;
39236c6e 1116 }
91447636
A
1117 *buf = '\0';
1118 for (i = 0; i < maxbytes; i++) {
1119 snprintf(buf, 3, "%02x", lladdr[i]);
1120 buf += 2;
1121 *buf = (i == maxbytes - 1) ? '\0' : ':';
1122 buf++;
1123 }
0a7de745 1124 return orig_buf;
91447636
A
1125}
1126
1127/*
1128 * arp_lookup_route will lookup the route for a given address.
1129 *
b0d623f7
A
1130 * The address must be for a host on a local network on this interface.
1131 * If the returned route is non-NULL, the route is locked and the caller
1132 * is responsible for unlocking it and releasing its reference.
91447636
A
1133 */
1134static errno_t
b0d623f7
A
1135arp_lookup_route(const struct in_addr *addr, int create, int proxy,
1136 route_t *route, unsigned int ifscope)
91447636 1137{
39236c6e 1138 struct sockaddr_inarp sin =
0a7de745 1139 { sizeof(sin), AF_INET, 0, { 0 }, { 0 }, 0, 0 };
2d21ac55 1140 const char *why = NULL;
0a7de745 1141 errno_t error = 0;
b0d623f7
A
1142 route_t rt;
1143
1144 *route = NULL;
91447636
A
1145
1146 sin.sin_addr.s_addr = addr->s_addr;
1147 sin.sin_other = proxy ? SIN_PROXY : 0;
c910b4d9 1148
6d2010ae
A
1149 /*
1150 * If the destination is a link-local address, don't
1151 * constrain the lookup (don't scope it).
1152 */
0a7de745 1153 if (IN_LINKLOCAL(ntohl(addr->s_addr))) {
6d2010ae 1154 ifscope = IFSCOPE_NONE;
0a7de745 1155 }
6d2010ae 1156
39236c6e 1157 rt = rtalloc1_scoped((struct sockaddr *)&sin, create, 0, ifscope);
0a7de745
A
1158 if (rt == NULL) {
1159 return ENETUNREACH;
1160 }
b0d623f7
A
1161
1162 RT_LOCK(rt);
1163
1164 if (rt->rt_flags & RTF_GATEWAY) {
91447636 1165 why = "host is not on local network";
91447636 1166 error = ENETUNREACH;
b0d623f7 1167 } else if (!(rt->rt_flags & RTF_LLINFO)) {
91447636 1168 why = "could not allocate llinfo";
91447636 1169 error = ENOMEM;
b0d623f7 1170 } else if (rt->rt_gateway->sa_family != AF_LINK) {
91447636 1171 why = "gateway route is not ours";
91447636
A
1172 error = EPROTONOSUPPORT;
1173 }
b0d623f7
A
1174
1175 if (error != 0) {
39236c6e 1176 if (create && (arp_verbose || log_arp_warnings)) {
b0d623f7 1177 char tmp[MAX_IPv4_STR_LEN];
39236c6e
A
1178 log(LOG_DEBUG, "%s: link#%d %s failed: %s\n",
1179 __func__, ifscope, inet_ntop(AF_INET, addr, tmp,
0a7de745 1180 sizeof(tmp)), why);
b0d623f7
A
1181 }
1182
1183 /*
1184 * If there are no references to this route, and it is
1185 * a cloned route, and not static, and ARP had created
1186 * the route, then purge it from the routing table as
1187 * it is probably bogus.
1188 */
1189 if (rt->rt_refcnt == 1 &&
1190 (rt->rt_flags & (RTF_WASCLONED | RTF_STATIC)) ==
1191 RTF_WASCLONED) {
1192 /*
1193 * Prevent another thread from modiying rt_key,
1194 * rt_gateway via rt_setgate() after rt_lock is
1195 * dropped by marking the route as defunct.
1196 */
1197 rt->rt_flags |= RTF_CONDEMNED;
1198 RT_UNLOCK(rt);
1199 rtrequest(RTM_DELETE, rt_key(rt), rt->rt_gateway,
39236c6e 1200 rt_mask(rt), rt->rt_flags, NULL);
b0d623f7
A
1201 rtfree(rt);
1202 } else {
1203 RT_REMREF_LOCKED(rt);
1204 RT_UNLOCK(rt);
1205 }
0a7de745 1206 return error;
91447636 1207 }
91447636 1208
b0d623f7
A
1209 /*
1210 * Caller releases reference and does RT_UNLOCK(rt).
1211 */
1212 *route = rt;
0a7de745 1213 return 0;
b0d623f7 1214}
91447636 1215
5ba3f43e 1216boolean_t
0a7de745 1217arp_is_entry_probing(route_t p_route)
5ba3f43e
A
1218{
1219 struct llinfo_arp *llinfo = p_route->rt_llinfo;
1220
1221 if (llinfo != NULL &&
1222 llinfo->la_llreach != NULL &&
0a7de745
A
1223 llinfo->la_llreach->lr_probes != 0) {
1224 return TRUE;
1225 }
5ba3f43e 1226
0a7de745 1227 return FALSE;
5ba3f43e
A
1228}
1229
b0d623f7
A
1230/*
1231 * This is the ARP pre-output routine; care must be taken to ensure that
1232 * the "hint" route never gets freed via rtfree(), since the caller may
1233 * have stored it inside a struct route with a reference held for that
1234 * placeholder.
1235 */
91447636 1236errno_t
b0d623f7 1237arp_lookup_ip(ifnet_t ifp, const struct sockaddr_in *net_dest,
0a7de745 1238 struct sockaddr_dl *ll_dest, size_t ll_dest_len, route_t hint,
b0d623f7 1239 mbuf_t packet)
91447636 1240{
0a7de745
A
1241 route_t route = NULL; /* output route */
1242 errno_t result = 0;
39236c6e
A
1243 struct sockaddr_dl *gateway;
1244 struct llinfo_arp *llinfo = NULL;
39037602 1245 boolean_t usable, probing = FALSE;
6d2010ae 1246 uint64_t timenow;
fe8ab488
A
1247 struct if_llreach *lr;
1248 struct ifaddr *rt_ifa;
1249 struct sockaddr *sa;
1250 uint32_t rtflags;
1251 struct sockaddr_dl sdl;
5ba3f43e 1252 boolean_t send_probe_notif = FALSE;
b0d623f7 1253
0a7de745
A
1254 if (ifp == NULL || net_dest == NULL) {
1255 return EINVAL;
1256 }
39037602 1257
0a7de745
A
1258 if (net_dest->sin_family != AF_INET) {
1259 return EAFNOSUPPORT;
1260 }
b0d623f7 1261
0a7de745
A
1262 if ((ifp->if_flags & (IFF_UP | IFF_RUNNING)) != (IFF_UP | IFF_RUNNING)) {
1263 return ENETDOWN;
1264 }
b0d623f7 1265
91447636
A
1266 /*
1267 * If we were given a route, verify the route and grab the gateway
1268 */
b0d623f7
A
1269 if (hint != NULL) {
1270 /*
1271 * Callee holds a reference on the route and returns
1272 * with the route entry locked, upon success.
1273 */
316670eb 1274 result = route_to_gwroute((const struct sockaddr *)
b0d623f7 1275 net_dest, hint, &route);
0a7de745
A
1276 if (result != 0) {
1277 return result;
1278 }
1279 if (route != NULL) {
b0d623f7 1280 RT_LOCK_ASSERT_HELD(route);
0a7de745 1281 }
91447636 1282 }
b0d623f7 1283
39037602
A
1284 if ((packet != NULL && (packet->m_flags & M_BCAST)) ||
1285 in_broadcast(net_dest->sin_addr, ifp)) {
39236c6e 1286 size_t broadcast_len;
91447636 1287 bzero(ll_dest, ll_dest_len);
b0d623f7
A
1288 result = ifnet_llbroadcast_copy_bytes(ifp, LLADDR(ll_dest),
1289 ll_dest_len - offsetof(struct sockaddr_dl, sdl_data),
1290 &broadcast_len);
1291 if (result == 0) {
1292 ll_dest->sdl_alen = broadcast_len;
1293 ll_dest->sdl_family = AF_LINK;
0a7de745 1294 ll_dest->sdl_len = sizeof(struct sockaddr_dl);
91447636 1295 }
b0d623f7 1296 goto release;
91447636 1297 }
39037602
A
1298 if ((packet != NULL && (packet->m_flags & M_MCAST)) ||
1299 ((ifp->if_flags & IFF_MULTICAST) &&
1300 IN_MULTICAST(ntohl(net_dest->sin_addr.s_addr)))) {
0a7de745 1301 if (route != NULL) {
b0d623f7 1302 RT_UNLOCK(route);
0a7de745 1303 }
b0d623f7 1304 result = dlil_resolve_multi(ifp,
39236c6e
A
1305 (const struct sockaddr *)net_dest,
1306 (struct sockaddr *)ll_dest, ll_dest_len);
0a7de745 1307 if (route != NULL) {
b0d623f7 1308 RT_LOCK(route);
0a7de745 1309 }
b0d623f7 1310 goto release;
91447636 1311 }
b0d623f7 1312
91447636
A
1313 /*
1314 * If we didn't find a route, or the route doesn't have
1315 * link layer information, trigger the creation of the
1316 * route and link layer information.
1317 */
b0d623f7
A
1318 if (route == NULL || route->rt_llinfo == NULL) {
1319 /* Clean up now while we can */
1320 if (route != NULL) {
1321 if (route == hint) {
1322 RT_REMREF_LOCKED(route);
1323 RT_UNLOCK(route);
1324 } else {
1325 RT_UNLOCK(route);
1326 rtfree(route);
1327 }
1328 }
1329 /*
1330 * Callee holds a reference on the route and returns
1331 * with the route entry locked, upon success.
1332 */
c910b4d9
A
1333 result = arp_lookup_route(&net_dest->sin_addr, 1, 0, &route,
1334 ifp->if_index);
0a7de745 1335 if (result == 0) {
b0d623f7 1336 RT_LOCK_ASSERT_HELD(route);
0a7de745 1337 }
b0d623f7
A
1338 }
1339
6d2010ae 1340 if (result || route == NULL || (llinfo = route->rt_llinfo) == NULL) {
b0d623f7 1341 /* In case result is 0 but no route, return an error */
0a7de745 1342 if (result == 0) {
b0d623f7 1343 result = EHOSTUNREACH;
0a7de745 1344 }
b0d623f7 1345
39236c6e
A
1346 if (route != NULL && route->rt_llinfo == NULL) {
1347 char tmp[MAX_IPv4_STR_LEN];
1348 log(LOG_ERR, "%s: can't allocate llinfo for %s\n",
1349 __func__, inet_ntop(AF_INET, &net_dest->sin_addr,
0a7de745 1350 tmp, sizeof(tmp)));
39236c6e 1351 }
b0d623f7 1352 goto release;
91447636 1353 }
b0d623f7 1354
91447636
A
1355 /*
1356 * Now that we have the right route, is it filled in?
1357 */
1358 gateway = SDL(route->rt_gateway);
6d2010ae
A
1359 timenow = net_uptime();
1360 VERIFY(route->rt_expire == 0 || route->rt_rmx.rmx_expire != 0);
1361 VERIFY(route->rt_expire != 0 || route->rt_rmx.rmx_expire == 0);
39037602
A
1362
1363 usable = ((route->rt_expire == 0 || route->rt_expire > timenow) &&
1364 gateway != NULL && gateway->sdl_family == AF_LINK &&
1365 gateway->sdl_alen != 0);
1366
1367 if (usable) {
1368 boolean_t unreachable = !arp_llreach_reachable(llinfo);
1369
1370 /* Entry is usable, so fill in info for caller */
91447636 1371 bcopy(gateway, ll_dest, MIN(gateway->sdl_len, ll_dest_len));
b0d623f7 1372 result = 0;
0a7de745 1373 arp_llreach_use(llinfo); /* Mark use timestamp */
39037602 1374
fe8ab488 1375 lr = llinfo->la_llreach;
0a7de745 1376 if (lr == NULL) {
fe8ab488 1377 goto release;
0a7de745 1378 }
fe8ab488 1379 rt_ifa = route->rt_ifa;
39037602 1380
fe8ab488
A
1381 /* Become a regular mutex, just in case */
1382 RT_CONVERT_LOCK(route);
1383 IFLR_LOCK_SPIN(lr);
39037602
A
1384
1385 if ((unreachable || (llinfo->la_flags & LLINFO_PROBING)) &&
1386 lr->lr_probes < arp_unicast_lim) {
1387 /*
1388 * Thus mark the entry with la_probeexp deadline to
1389 * trigger the probe timer to be scheduled (if not
1390 * already). This gets cleared the moment we get
1391 * an ARP reply.
1392 */
1393 probing = TRUE;
1394 if (lr->lr_probes == 0) {
1395 llinfo->la_probeexp = (timenow + arpt_probe);
1396 llinfo->la_flags |= LLINFO_PROBING;
5ba3f43e
A
1397 /*
1398 * Provide notification that ARP unicast
1399 * probing has started.
1400 * We only do it for the first unicast probe
1401 * attempt.
1402 */
1403 send_probe_notif = TRUE;
39037602
A
1404 }
1405
1406 /*
1407 * Start the unicast probe and anticipate a reply;
1408 * afterwards, return existing entry to caller and
1409 * let it be used anyway. If peer is non-existent
1410 * we'll broadcast ARP next time around.
1411 */
fe8ab488 1412 lr->lr_probes++;
0a7de745 1413 bzero(&sdl, sizeof(sdl));
fe8ab488
A
1414 sdl.sdl_alen = ifp->if_addrlen;
1415 bcopy(&lr->lr_key.addr, LLADDR(&sdl),
1416 ifp->if_addrlen);
1417 IFLR_UNLOCK(lr);
1418 IFA_LOCK_SPIN(rt_ifa);
1419 IFA_ADDREF_LOCKED(rt_ifa);
1420 sa = rt_ifa->ifa_addr;
1421 IFA_UNLOCK(rt_ifa);
1422 rtflags = route->rt_flags;
1423 RT_UNLOCK(route);
1424 dlil_send_arp(ifp, ARPOP_REQUEST, NULL, sa,
1425 (const struct sockaddr_dl *)&sdl,
1426 (const struct sockaddr *)net_dest, rtflags);
1427 IFA_REMREF(rt_ifa);
1428 RT_LOCK(route);
39037602
A
1429 goto release;
1430 } else {
fe8ab488 1431 IFLR_UNLOCK(lr);
39037602
A
1432 if (!unreachable &&
1433 !(llinfo->la_flags & LLINFO_PROBING)) {
1434 /*
1435 * Normal case where peer is still reachable,
1436 * we're not probing and if_addrlen is anything
1437 * but IF_LLREACH_MAXLEN.
1438 */
1439 goto release;
1440 }
1441 }
91447636 1442 }
b0d623f7
A
1443
1444 if (ifp->if_flags & IFF_NOARP) {
1445 result = ENOTSUP;
1446 goto release;
1447 }
1448
91447636 1449 /*
39037602
A
1450 * Route wasn't complete/valid; we need to send out ARP request.
1451 * If we've exceeded the limit of la_holdq, drop from the head
1452 * of queue and add this packet to the tail. If we end up with
1453 * RTF_REJECT below, we'll dequeue this from tail and have the
1454 * caller free the packet instead. It's safe to do that since
1455 * we still hold the route's rt_lock.
91447636 1456 */
0a7de745 1457 if (packet != NULL) {
39037602 1458 arp_llinfo_addq(llinfo, packet);
0a7de745 1459 } else {
5ba3f43e 1460 llinfo->la_prbreq_cnt++;
0a7de745 1461 }
39037602
A
1462 /*
1463 * Regardless of permanent vs. expirable entry, we need to
1464 * avoid having packets sit in la_holdq forever; thus mark the
1465 * entry with la_probeexp deadline to trigger the probe timer
1466 * to be scheduled (if not already). This gets cleared the
1467 * moment we get an ARP reply.
1468 */
1469 probing = TRUE;
5ba3f43e 1470 if ((qlen(&llinfo->la_holdq) + llinfo->la_prbreq_cnt) == 1) {
39037602
A
1471 llinfo->la_probeexp = (timenow + arpt_probe);
1472 llinfo->la_flags |= LLINFO_PROBING;
1473 }
5ba3f43e 1474
6d2010ae 1475 if (route->rt_expire) {
91447636 1476 route->rt_flags &= ~RTF_REJECT;
39236c6e 1477 if (llinfo->la_asked == 0 || route->rt_expire != timenow) {
6d2010ae 1478 rt_setexpire(route, timenow);
39236c6e 1479 if (llinfo->la_asked++ < llinfo->la_maxtries) {
fe8ab488
A
1480 struct kev_msg ev_msg;
1481 struct kev_in_arpfailure in_arpfailure;
1482 boolean_t sendkev = FALSE;
6d2010ae 1483
fe8ab488
A
1484 rt_ifa = route->rt_ifa;
1485 lr = llinfo->la_llreach;
6d2010ae
A
1486 /* Become a regular mutex, just in case */
1487 RT_CONVERT_LOCK(route);
1488 /* Update probe count, if applicable */
39236c6e
A
1489 if (lr != NULL) {
1490 IFLR_LOCK_SPIN(lr);
1491 lr->lr_probes++;
39236c6e 1492 IFLR_UNLOCK(lr);
6d2010ae 1493 }
fe8ab488
A
1494 if (ifp->if_addrlen == IF_LLREACH_MAXLEN &&
1495 route->rt_flags & RTF_ROUTER &&
1496 llinfo->la_asked > 1) {
1497 sendkev = TRUE;
1498 llinfo->la_flags |= LLINFO_RTRFAIL_EVTSENT;
1499 }
6d2010ae
A
1500 IFA_LOCK_SPIN(rt_ifa);
1501 IFA_ADDREF_LOCKED(rt_ifa);
1502 sa = rt_ifa->ifa_addr;
1503 IFA_UNLOCK(rt_ifa);
39236c6e 1504 arp_llreach_use(llinfo); /* Mark use tstamp */
316670eb 1505 rtflags = route->rt_flags;
b0d623f7 1506 RT_UNLOCK(route);
39236c6e 1507 dlil_send_arp(ifp, ARPOP_REQUEST, NULL, sa,
fe8ab488
A
1508 NULL, (const struct sockaddr *)net_dest,
1509 rtflags);
6d2010ae 1510 IFA_REMREF(rt_ifa);
fe8ab488
A
1511 if (sendkev) {
1512 bzero(&ev_msg, sizeof(ev_msg));
39037602 1513 bzero(&in_arpfailure,
fe8ab488
A
1514 sizeof(in_arpfailure));
1515 in_arpfailure.link_data.if_family =
1516 ifp->if_family;
1517 in_arpfailure.link_data.if_unit =
1518 ifp->if_unit;
1519 strlcpy(in_arpfailure.link_data.if_name,
1520 ifp->if_name, IFNAMSIZ);
1521 ev_msg.vendor_code = KEV_VENDOR_APPLE;
1522 ev_msg.kev_class = KEV_NETWORK_CLASS;
1523 ev_msg.kev_subclass = KEV_INET_SUBCLASS;
1524 ev_msg.event_code =
1525 KEV_INET_ARPRTRFAILURE;
1526 ev_msg.dv[0].data_ptr = &in_arpfailure;
1527 ev_msg.dv[0].data_length =
1528 sizeof(struct
0a7de745 1529 kev_in_arpfailure);
39037602 1530 dlil_post_complete_msg(NULL, &ev_msg);
fe8ab488 1531 }
b0d623f7 1532 result = EJUSTRETURN;
fe8ab488 1533 RT_LOCK(route);
b0d623f7
A
1534 goto release;
1535 } else {
91447636 1536 route->rt_flags |= RTF_REJECT;
39236c6e
A
1537 rt_setexpire(route,
1538 route->rt_expire + arpt_down);
91447636 1539 llinfo->la_asked = 0;
6d2010ae 1540 /*
39037602
A
1541 * Remove the packet that was just added above;
1542 * don't free it since we're not returning
1543 * EJUSTRETURN. The caller will handle the
1544 * freeing. Since we haven't dropped rt_lock
1545 * from the time of _addq() above, this packet
1546 * must be at the tail.
6d2010ae 1547 */
39037602
A
1548 if (packet != NULL) {
1549 struct mbuf *_m =
1550 _getq_tail(&llinfo->la_holdq);
1551 atomic_add_32(&arpstat.held, -1);
1552 VERIFY(_m == packet);
1553 }
b0d623f7 1554 result = EHOSTUNREACH;
5ba3f43e
A
1555
1556 /*
1557 * Enqueue work item to invoke callback for this route entry
1558 */
1559 route_event_enqueue_nwk_wq_entry(route, NULL,
1560 ROUTE_LLENTRY_UNREACH, NULL, TRUE);
b0d623f7 1561 goto release;
91447636
A
1562 }
1563 }
1564 }
b0d623f7 1565
39037602 1566 /* The packet is now held inside la_holdq */
b0d623f7
A
1567 result = EJUSTRETURN;
1568
1569release:
0a7de745 1570 if (result == EHOSTUNREACH) {
39037602 1571 atomic_add_32(&arpstat.dropped, 1);
0a7de745 1572 }
39236c6e 1573
b0d623f7 1574 if (route != NULL) {
5ba3f43e
A
1575 if (send_probe_notif) {
1576 route_event_enqueue_nwk_wq_entry(route, NULL,
1577 ROUTE_LLENTRY_PROBED, NULL, TRUE);
1578
1579 if (route->rt_flags & RTF_ROUTER) {
1580 struct radix_node_head *rnh = NULL;
1581 struct route_event rt_ev;
1582 route_event_init(&rt_ev, route, NULL, ROUTE_LLENTRY_PROBED);
1583 /*
1584 * We already have a reference on rt. The function
1585 * frees it before returning.
1586 */
1587 RT_UNLOCK(route);
1588 lck_mtx_lock(rnh_lock);
1589 rnh = rt_tables[AF_INET];
1590
0a7de745 1591 if (rnh != NULL) {
5ba3f43e
A
1592 (void) rnh->rnh_walktree(rnh,
1593 route_event_walktree, (void *)&rt_ev);
0a7de745 1594 }
5ba3f43e
A
1595 lck_mtx_unlock(rnh_lock);
1596 RT_LOCK(route);
1597 }
1598 }
1599
b0d623f7
A
1600 if (route == hint) {
1601 RT_REMREF_LOCKED(route);
1602 RT_UNLOCK(route);
1603 } else {
1604 RT_UNLOCK(route);
1605 rtfree(route);
1606 }
1607 }
39037602
A
1608 if (probing) {
1609 /* Do this after we drop rt_lock to preserve ordering */
1610 lck_mtx_lock(rnh_lock);
1611 arp_sched_probe(NULL);
1612 lck_mtx_unlock(rnh_lock);
1613 }
0a7de745 1614 return result;
91447636
A
1615}
1616
1617errno_t
39236c6e
A
1618arp_ip_handle_input(ifnet_t ifp, u_short arpop,
1619 const struct sockaddr_dl *sender_hw, const struct sockaddr_in *sender_ip,
1620 const struct sockaddr_in *target_ip)
91447636 1621{
39236c6e 1622 char ipv4str[MAX_IPv4_STR_LEN];
b0d623f7
A
1623 struct sockaddr_dl proxied;
1624 struct sockaddr_dl *gateway, *target_hw = NULL;
1625 struct ifaddr *ifa;
91447636
A
1626 struct in_ifaddr *ia;
1627 struct in_ifaddr *best_ia = NULL;
6d2010ae 1628 struct sockaddr_in best_ia_sin;
0a7de745 1629 route_t route = NULL;
39236c6e 1630 char buf[3 * MAX_HW_LEN]; /* enough for MAX_HW_LEN byte hw address */
91447636 1631 struct llinfo_arp *llinfo;
0a7de745 1632 errno_t error;
2d21ac55 1633 int created_announcement = 0;
b7266188 1634 int bridged = 0, is_bridge = 0;
5ba3f43e 1635 uint32_t rt_evcode = 0;
6d2010ae 1636
39037602
A
1637 /*
1638 * Here and other places within this routine where we don't hold
1639 * rnh_lock, trade accuracy for speed for the common scenarios
1640 * and avoid the use of atomic updates.
1641 */
39236c6e
A
1642 arpstat.received++;
1643
91447636 1644 /* Do not respond to requests for 0.0.0.0 */
0a7de745 1645 if (target_ip->sin_addr.s_addr == INADDR_ANY && arpop == ARPOP_REQUEST) {
b0d623f7 1646 goto done;
0a7de745 1647 }
6d2010ae 1648
0a7de745 1649 if (ifp->if_bridge) {
b7266188 1650 bridged = 1;
0a7de745
A
1651 }
1652 if (ifp->if_type == IFT_BRIDGE) {
b7266188 1653 is_bridge = 1;
0a7de745 1654 }
b0d623f7 1655
0a7de745 1656 if (arpop == ARPOP_REPLY) {
39236c6e 1657 arpstat.rxreplies++;
0a7de745 1658 }
39236c6e 1659
91447636
A
1660 /*
1661 * Determine if this ARP is for us
6d2010ae 1662 * For a bridge, we want to check the address irrespective
b7266188 1663 * of the receive interface.
91447636 1664 */
b0d623f7
A
1665 lck_rw_lock_shared(in_ifaddr_rwlock);
1666 TAILQ_FOREACH(ia, INADDR_HASH(target_ip->sin_addr.s_addr), ia_hash) {
6d2010ae 1667 IFA_LOCK_SPIN(&ia->ia_ifa);
b7266188 1668 if (((bridged && ia->ia_ifp->if_bridge != NULL) ||
6d2010ae 1669 (ia->ia_ifp == ifp)) &&
b0d623f7 1670 ia->ia_addr.sin_addr.s_addr == target_ip->sin_addr.s_addr) {
6d2010ae
A
1671 best_ia = ia;
1672 best_ia_sin = best_ia->ia_addr;
1673 IFA_ADDREF_LOCKED(&ia->ia_ifa);
1674 IFA_UNLOCK(&ia->ia_ifa);
1675 lck_rw_done(in_ifaddr_rwlock);
1676 goto match;
91447636 1677 }
6d2010ae 1678 IFA_UNLOCK(&ia->ia_ifa);
91447636 1679 }
b0d623f7
A
1680
1681 TAILQ_FOREACH(ia, INADDR_HASH(sender_ip->sin_addr.s_addr), ia_hash) {
6d2010ae 1682 IFA_LOCK_SPIN(&ia->ia_ifa);
b7266188 1683 if (((bridged && ia->ia_ifp->if_bridge != NULL) ||
6d2010ae 1684 (ia->ia_ifp == ifp)) &&
b0d623f7 1685 ia->ia_addr.sin_addr.s_addr == sender_ip->sin_addr.s_addr) {
6d2010ae
A
1686 best_ia = ia;
1687 best_ia_sin = best_ia->ia_addr;
1688 IFA_ADDREF_LOCKED(&ia->ia_ifa);
1689 IFA_UNLOCK(&ia->ia_ifa);
1690 lck_rw_done(in_ifaddr_rwlock);
1691 goto match;
b7266188 1692 }
6d2010ae 1693 IFA_UNLOCK(&ia->ia_ifa);
b7266188
A
1694 }
1695
0a7de745
A
1696#define BDG_MEMBER_MATCHES_ARP(addr, ifp, ia) \
1697 (ia->ia_ifp->if_bridge == ifp->if_softc && \
39236c6e 1698 bcmp(IF_LLADDR(ia->ia_ifp), IF_LLADDR(ifp), ifp->if_addrlen) == 0 && \
b7266188
A
1699 addr == ia->ia_addr.sin_addr.s_addr)
1700 /*
1701 * Check the case when bridge shares its MAC address with
1702 * some of its children, so packets are claimed by bridge
1703 * itself (bridge_input() does it first), but they are really
1704 * meant to be destined to the bridge member.
1705 */
1706 if (is_bridge) {
6d2010ae
A
1707 TAILQ_FOREACH(ia, INADDR_HASH(target_ip->sin_addr.s_addr),
1708 ia_hash) {
1709 IFA_LOCK_SPIN(&ia->ia_ifa);
1710 if (BDG_MEMBER_MATCHES_ARP(target_ip->sin_addr.s_addr,
1711 ifp, ia)) {
b7266188
A
1712 ifp = ia->ia_ifp;
1713 best_ia = ia;
6d2010ae
A
1714 best_ia_sin = best_ia->ia_addr;
1715 IFA_ADDREF_LOCKED(&ia->ia_ifa);
1716 IFA_UNLOCK(&ia->ia_ifa);
b7266188
A
1717 lck_rw_done(in_ifaddr_rwlock);
1718 goto match;
1719 }
6d2010ae 1720 IFA_UNLOCK(&ia->ia_ifa);
b0d623f7 1721 }
91447636 1722 }
39236c6e 1723#undef BDG_MEMBER_MATCHES_ARP
b0d623f7
A
1724 lck_rw_done(in_ifaddr_rwlock);
1725
1726 /*
1727 * No match, use the first inet address on the receive interface
1728 * as a dummy address for the rest of the function; we may be
1729 * proxying for another address.
1730 */
1731 ifnet_lock_shared(ifp);
1732 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
6d2010ae
A
1733 IFA_LOCK_SPIN(ifa);
1734 if (ifa->ifa_addr->sa_family != AF_INET) {
1735 IFA_UNLOCK(ifa);
b0d623f7 1736 continue;
6d2010ae 1737 }
b0d623f7 1738 best_ia = (struct in_ifaddr *)ifa;
6d2010ae
A
1739 best_ia_sin = best_ia->ia_addr;
1740 IFA_ADDREF_LOCKED(ifa);
1741 IFA_UNLOCK(ifa);
b7266188
A
1742 ifnet_lock_done(ifp);
1743 goto match;
b0d623f7
A
1744 }
1745 ifnet_lock_done(ifp);
1746
b7266188
A
1747 /*
1748 * If we're not a bridge member, or if we are but there's no
1749 * IPv4 address to use for the interface, drop the packet.
1750 */
0a7de745 1751 if (!bridged || best_ia == NULL) {
b0d623f7 1752 goto done;
0a7de745 1753 }
b0d623f7
A
1754
1755match:
91447636 1756 /* If the packet is from this interface, ignore the packet */
39236c6e 1757 if (bcmp(CONST_LLADDR(sender_hw), IF_LLADDR(ifp),
0a7de745 1758 sender_hw->sdl_alen) == 0) {
b0d623f7 1759 goto done;
0a7de745 1760 }
b0d623f7 1761
91447636 1762 /* Check for a conflict */
39236c6e
A
1763 if (!bridged &&
1764 sender_ip->sin_addr.s_addr == best_ia_sin.sin_addr.s_addr) {
1765 struct kev_msg ev_msg;
0a7de745
A
1766 struct kev_in_collision *in_collision;
1767 u_char storage[sizeof(struct kev_in_collision) + MAX_HW_LEN];
39236c6e 1768
0a7de745
A
1769 bzero(&ev_msg, sizeof(struct kev_msg));
1770 bzero(storage, (sizeof(struct kev_in_collision) + MAX_HW_LEN));
39236c6e
A
1771 in_collision = (struct kev_in_collision *)(void *)storage;
1772 log(LOG_ERR, "%s duplicate IP address %s sent from "
1773 "address %s\n", if_name(ifp),
1774 inet_ntop(AF_INET, &sender_ip->sin_addr, ipv4str,
0a7de745
A
1775 sizeof(ipv4str)), sdl_addr_to_hex(sender_hw, buf,
1776 sizeof(buf)));
b0d623f7 1777
91447636
A
1778 /* Send a kernel event so anyone can learn of the conflict */
1779 in_collision->link_data.if_family = ifp->if_family;
1780 in_collision->link_data.if_unit = ifp->if_unit;
fe8ab488 1781 strlcpy(&in_collision->link_data.if_name[0],
39236c6e 1782 ifp->if_name, IFNAMSIZ);
91447636 1783 in_collision->ia_ipaddr = sender_ip->sin_addr;
39236c6e
A
1784 in_collision->hw_len = (sender_hw->sdl_alen < MAX_HW_LEN) ?
1785 sender_hw->sdl_alen : MAX_HW_LEN;
1786 bcopy(CONST_LLADDR(sender_hw), (caddr_t)in_collision->hw_addr,
1787 in_collision->hw_len);
91447636
A
1788 ev_msg.vendor_code = KEV_VENDOR_APPLE;
1789 ev_msg.kev_class = KEV_NETWORK_CLASS;
1790 ev_msg.kev_subclass = KEV_INET_SUBCLASS;
1791 ev_msg.event_code = KEV_INET_ARPCOLLISION;
1792 ev_msg.dv[0].data_ptr = in_collision;
39236c6e 1793 ev_msg.dv[0].data_length =
0a7de745 1794 sizeof(struct kev_in_collision) + in_collision->hw_len;
91447636 1795 ev_msg.dv[1].data_length = 0;
39037602
A
1796 dlil_post_complete_msg(NULL, &ev_msg);
1797 atomic_add_32(&arpstat.dupips, 1);
91447636
A
1798 goto respond;
1799 }
b0d623f7 1800
91447636
A
1801 /*
1802 * Look up the routing entry. If it doesn't exist and we are the
c910b4d9 1803 * target, and the sender isn't 0.0.0.0, go ahead and create one.
b0d623f7
A
1804 * Callee holds a reference on the route and returns with the route
1805 * entry locked, upon success.
91447636 1806 */
c910b4d9 1807 error = arp_lookup_route(&sender_ip->sin_addr,
6d2010ae 1808 (target_ip->sin_addr.s_addr == best_ia_sin.sin_addr.s_addr &&
c910b4d9 1809 sender_ip->sin_addr.s_addr != 0), 0, &route, ifp->if_index);
b0d623f7 1810
0a7de745 1811 if (error == 0) {
b0d623f7 1812 RT_LOCK_ASSERT_HELD(route);
0a7de745 1813 }
b0d623f7 1814
39236c6e 1815 if (error || route == NULL || route->rt_gateway == NULL) {
0a7de745 1816 if (arpop != ARPOP_REQUEST) {
2d21ac55 1817 goto respond;
0a7de745 1818 }
39236c6e
A
1819
1820 if (arp_sendllconflict && send_conflicting_probes != 0 &&
1821 (ifp->if_eflags & IFEF_ARPLL) &&
1822 IN_LINKLOCAL(ntohl(target_ip->sin_addr.s_addr)) &&
1823 sender_ip->sin_addr.s_addr == INADDR_ANY) {
91447636 1824 /*
39236c6e
A
1825 * Verify this ARP probe doesn't conflict with
1826 * an IPv4LL we know of on another interface.
91447636 1827 */
b0d623f7
A
1828 if (route != NULL) {
1829 RT_REMREF_LOCKED(route);
1830 RT_UNLOCK(route);
1831 route = NULL;
1832 }
1833 /*
1834 * Callee holds a reference on the route and returns
1835 * with the route entry locked, upon success.
1836 */
c910b4d9
A
1837 error = arp_lookup_route(&target_ip->sin_addr, 0, 0,
1838 &route, ifp->if_index);
b0d623f7 1839
39236c6e 1840 if (error != 0 || route == NULL ||
0a7de745 1841 route->rt_gateway == NULL) {
39236c6e 1842 goto respond;
0a7de745 1843 }
b0d623f7 1844
39236c6e
A
1845 RT_LOCK_ASSERT_HELD(route);
1846
1847 gateway = SDL(route->rt_gateway);
1848 if (route->rt_ifp != ifp && gateway->sdl_alen != 0 &&
1849 (gateway->sdl_alen != sender_hw->sdl_alen ||
1850 bcmp(CONST_LLADDR(gateway), CONST_LLADDR(sender_hw),
1851 gateway->sdl_alen) != 0)) {
1852 /*
1853 * A node is probing for an IPv4LL we know
1854 * exists on a different interface. We respond
1855 * with a conflicting probe to force the new
1856 * device to pick a different IPv4LL address.
1857 */
1858 if (arp_verbose || log_arp_warnings) {
1859 log(LOG_INFO, "arp: %s on %s sent "
1860 "probe for %s, already on %s\n",
1861 sdl_addr_to_hex(sender_hw, buf,
0a7de745 1862 sizeof(buf)), if_name(ifp),
39236c6e
A
1863 inet_ntop(AF_INET,
1864 &target_ip->sin_addr, ipv4str,
0a7de745 1865 sizeof(ipv4str)),
39236c6e
A
1866 if_name(route->rt_ifp));
1867 log(LOG_INFO, "arp: sending "
1868 "conflicting probe to %s on %s\n",
1869 sdl_addr_to_hex(sender_hw, buf,
0a7de745 1870 sizeof(buf)), if_name(ifp));
6d2010ae 1871 }
39236c6e 1872 /* Mark use timestamp */
0a7de745 1873 if (route->rt_llinfo != NULL) {
39236c6e 1874 arp_llreach_use(route->rt_llinfo);
0a7de745 1875 }
39236c6e
A
1876 /* We're done with the route */
1877 RT_REMREF_LOCKED(route);
1878 RT_UNLOCK(route);
1879 route = NULL;
1880 /*
1881 * Send a conservative unicast "ARP probe".
1882 * This should force the other device to pick
1883 * a new number. This will not force the
1884 * device to pick a new number if the device
1885 * has already assigned that number. This will
1886 * not imply to the device that we own that
1887 * address. The link address is always
1888 * present; it's never freed.
1889 */
1890 ifnet_lock_shared(ifp);
1891 ifa = ifp->if_lladdr;
1892 IFA_ADDREF(ifa);
1893 ifnet_lock_done(ifp);
1894 dlil_send_arp_internal(ifp, ARPOP_REQUEST,
1895 SDL(ifa->ifa_addr),
1896 (const struct sockaddr *)sender_ip,
1897 sender_hw,
1898 (const struct sockaddr *)target_ip);
1899 IFA_REMREF(ifa);
1900 ifa = NULL;
39037602 1901 atomic_add_32(&arpstat.txconflicts, 1);
91447636 1902 }
2d21ac55 1903 goto respond;
39236c6e
A
1904 } else if (keep_announcements != 0 &&
1905 target_ip->sin_addr.s_addr == sender_ip->sin_addr.s_addr) {
1906 /*
1907 * Don't create entry if link-local address and
1908 * link-local is disabled
1909 */
1910 if (!IN_LINKLOCAL(ntohl(sender_ip->sin_addr.s_addr)) ||
1911 (ifp->if_eflags & IFEF_ARPLL)) {
b0d623f7
A
1912 if (route != NULL) {
1913 RT_REMREF_LOCKED(route);
1914 RT_UNLOCK(route);
1915 route = NULL;
1916 }
1917 /*
1918 * Callee holds a reference on the route and
1919 * returns with the route entry locked, upon
1920 * success.
1921 */
c910b4d9
A
1922 error = arp_lookup_route(&sender_ip->sin_addr,
1923 1, 0, &route, ifp->if_index);
b0d623f7 1924
0a7de745 1925 if (error == 0) {
b0d623f7 1926 RT_LOCK_ASSERT_HELD(route);
0a7de745 1927 }
b0d623f7 1928
39236c6e 1929 if (error == 0 && route != NULL &&
0a7de745 1930 route->rt_gateway != NULL) {
2d21ac55 1931 created_announcement = 1;
0a7de745 1932 }
2d21ac55 1933 }
0a7de745 1934 if (created_announcement == 0) {
2d21ac55 1935 goto respond;
0a7de745 1936 }
2d21ac55
A
1937 } else {
1938 goto respond;
91447636 1939 }
91447636 1940 }
b0d623f7
A
1941
1942 RT_LOCK_ASSERT_HELD(route);
6d2010ae
A
1943 VERIFY(route->rt_expire == 0 || route->rt_rmx.rmx_expire != 0);
1944 VERIFY(route->rt_expire != 0 || route->rt_rmx.rmx_expire == 0);
39236c6e 1945
91447636 1946 gateway = SDL(route->rt_gateway);
b7266188 1947 if (!bridged && route->rt_ifp != ifp) {
39236c6e
A
1948 if (!IN_LINKLOCAL(ntohl(sender_ip->sin_addr.s_addr)) ||
1949 !(ifp->if_eflags & IFEF_ARPLL)) {
0a7de745 1950 if (arp_verbose || log_arp_warnings) {
39236c6e
A
1951 log(LOG_ERR, "arp: %s is on %s but got "
1952 "reply from %s on %s\n",
1953 inet_ntop(AF_INET, &sender_ip->sin_addr,
0a7de745 1954 ipv4str, sizeof(ipv4str)),
39236c6e
A
1955 if_name(route->rt_ifp),
1956 sdl_addr_to_hex(sender_hw, buf,
0a7de745
A
1957 sizeof(buf)), if_name(ifp));
1958 }
91447636 1959 goto respond;
39236c6e 1960 } else {
91447636 1961 /* Don't change a permanent address */
0a7de745 1962 if (route->rt_expire == 0) {
91447636 1963 goto respond;
0a7de745 1964 }
b0d623f7
A
1965
1966 /*
1967 * We're about to check and/or change the route's ifp
1968 * and ifa, so do the lock dance: drop rt_lock, hold
1969 * rnh_lock and re-hold rt_lock to avoid violating the
1970 * lock ordering. We have an extra reference on the
1971 * route, so it won't go away while we do this.
1972 */
1973 RT_UNLOCK(route);
1974 lck_mtx_lock(rnh_lock);
1975 RT_LOCK(route);
91447636 1976 /*
b0d623f7
A
1977 * Don't change the cloned route away from the
1978 * parent's interface if the address did resolve
1979 * or if the route is defunct. rt_ifp on both
1980 * the parent and the clone can now be freely
1981 * accessed now that we have acquired rnh_lock.
91447636 1982 */
b0d623f7 1983 gateway = SDL(route->rt_gateway);
39236c6e
A
1984 if ((gateway->sdl_alen != 0 &&
1985 route->rt_parent != NULL &&
b0d623f7
A
1986 route->rt_parent->rt_ifp == route->rt_ifp) ||
1987 (route->rt_flags & RTF_CONDEMNED)) {
1988 RT_REMREF_LOCKED(route);
1989 RT_UNLOCK(route);
1990 route = NULL;
1991 lck_mtx_unlock(rnh_lock);
91447636
A
1992 goto respond;
1993 }
6d2010ae
A
1994 if (route->rt_ifp != ifp) {
1995 /*
1996 * Purge any link-layer info caching.
1997 */
0a7de745 1998 if (route->rt_llinfo_purge != NULL) {
6d2010ae 1999 route->rt_llinfo_purge(route);
0a7de745 2000 }
6d2010ae
A
2001
2002 /* Adjust route ref count for the interfaces */
2003 if (route->rt_if_ref_fn != NULL) {
2004 route->rt_if_ref_fn(ifp, 1);
2005 route->rt_if_ref_fn(route->rt_ifp, -1);
2006 }
d1ecb069 2007 }
91447636
A
2008 /* Change the interface when the existing route is on */
2009 route->rt_ifp = ifp;
39236c6e
A
2010 /*
2011 * If rmx_mtu is not locked, update it
2012 * to the MTU used by the new interface.
2013 */
d9a64523 2014 if (!(route->rt_rmx.rmx_locks & RTV_MTU)) {
39236c6e 2015 route->rt_rmx.rmx_mtu = route->rt_ifp->if_mtu;
d9a64523
A
2016 if (INTF_ADJUST_MTU_FOR_CLAT46(ifp)) {
2017 route->rt_rmx.rmx_mtu = IN6_LINKMTU(route->rt_ifp);
2018 /* Further adjust the size for CLAT46 expansion */
2019 route->rt_rmx.rmx_mtu -= CLAT46_HDR_EXPANSION_OVERHD;
2020 }
2021 }
39236c6e 2022
91447636
A
2023 rtsetifa(route, &best_ia->ia_ifa);
2024 gateway->sdl_index = ifp->if_index;
b0d623f7
A
2025 RT_UNLOCK(route);
2026 lck_mtx_unlock(rnh_lock);
2027 RT_LOCK(route);
2028 /* Don't bother if the route is down */
0a7de745 2029 if (!(route->rt_flags & RTF_UP)) {
b0d623f7 2030 goto respond;
0a7de745 2031 }
b0d623f7
A
2032 /* Refresh gateway pointer */
2033 gateway = SDL(route->rt_gateway);
91447636 2034 }
b0d623f7 2035 RT_LOCK_ASSERT_HELD(route);
91447636 2036 }
b0d623f7 2037
39236c6e
A
2038 if (gateway->sdl_alen != 0 && bcmp(LLADDR(gateway),
2039 CONST_LLADDR(sender_hw), gateway->sdl_alen) != 0) {
2040 if (route->rt_expire != 0 &&
2041 (arp_verbose || log_arp_warnings)) {
91447636 2042 char buf2[3 * MAX_HW_LEN];
39236c6e 2043 log(LOG_INFO, "arp: %s moved from %s to %s on %s\n",
2d21ac55 2044 inet_ntop(AF_INET, &sender_ip->sin_addr, ipv4str,
0a7de745
A
2045 sizeof(ipv4str)),
2046 sdl_addr_to_hex(gateway, buf, sizeof(buf)),
2047 sdl_addr_to_hex(sender_hw, buf2, sizeof(buf2)),
39236c6e
A
2048 if_name(ifp));
2049 } else if (route->rt_expire == 0) {
2050 if (arp_verbose || log_arp_warnings) {
2d21ac55 2051 log(LOG_ERR, "arp: %s attempts to modify "
39236c6e 2052 "permanent entry for %s on %s\n",
2d21ac55 2053 sdl_addr_to_hex(sender_hw, buf,
0a7de745 2054 sizeof(buf)),
2d21ac55 2055 inet_ntop(AF_INET, &sender_ip->sin_addr,
0a7de745 2056 ipv4str, sizeof(ipv4str)),
39236c6e 2057 if_name(ifp));
2d21ac55 2058 }
91447636
A
2059 goto respond;
2060 }
2061 }
b0d623f7 2062
91447636
A
2063 /* Copy the sender hardware address in to the route's gateway address */
2064 gateway->sdl_alen = sender_hw->sdl_alen;
2065 bcopy(CONST_LLADDR(sender_hw), LLADDR(gateway), gateway->sdl_alen);
b0d623f7 2066
91447636 2067 /* Update the expire time for the route and clear the reject flag */
0a7de745 2068 if (route->rt_expire != 0) {
39236c6e 2069 rt_setexpire(route, net_uptime() + arpt_keep);
0a7de745 2070 }
91447636 2071 route->rt_flags &= ~RTF_REJECT;
b0d623f7 2072
6d2010ae
A
2073 /* cache the gateway (sender HW) address */
2074 arp_llreach_alloc(route, ifp, LLADDR(gateway), gateway->sdl_alen,
5ba3f43e 2075 (arpop == ARPOP_REPLY), &rt_evcode);
6d2010ae 2076
b0d623f7 2077 llinfo = route->rt_llinfo;
fe8ab488
A
2078 /* send a notification that the route is back up */
2079 if (ifp->if_addrlen == IF_LLREACH_MAXLEN &&
39037602 2080 route->rt_flags & RTF_ROUTER &&
fe8ab488
A
2081 llinfo->la_flags & LLINFO_RTRFAIL_EVTSENT) {
2082 struct kev_msg ev_msg;
3e170ce0 2083 struct kev_in_arpalive in_arpalive;
fe8ab488
A
2084
2085 llinfo->la_flags &= ~LLINFO_RTRFAIL_EVTSENT;
2086 RT_UNLOCK(route);
2087 bzero(&ev_msg, sizeof(ev_msg));
2088 bzero(&in_arpalive, sizeof(in_arpalive));
2089 in_arpalive.link_data.if_family = ifp->if_family;
2090 in_arpalive.link_data.if_unit = ifp->if_unit;
2091 strlcpy(in_arpalive.link_data.if_name, ifp->if_name, IFNAMSIZ);
2092 ev_msg.vendor_code = KEV_VENDOR_APPLE;
2093 ev_msg.kev_class = KEV_NETWORK_CLASS;
2094 ev_msg.kev_subclass = KEV_INET_SUBCLASS;
2095 ev_msg.event_code = KEV_INET_ARPRTRALIVE;
2096 ev_msg.dv[0].data_ptr = &in_arpalive;
39037602
A
2097 ev_msg.dv[0].data_length = sizeof(struct kev_in_arpalive);
2098 dlil_post_complete_msg(NULL, &ev_msg);
fe8ab488
A
2099 RT_LOCK(route);
2100 }
39037602 2101 /* Update the llinfo, send out all queued packets at once */
91447636 2102 llinfo->la_asked = 0;
39037602 2103 llinfo->la_flags &= ~LLINFO_PROBING;
5ba3f43e
A
2104 llinfo->la_prbreq_cnt = 0;
2105
2106 if (rt_evcode) {
2107 /*
2108 * Enqueue work item to invoke callback for this route entry
2109 */
2110 route_event_enqueue_nwk_wq_entry(route, NULL, rt_evcode, NULL, TRUE);
2111
2112 if (route->rt_flags & RTF_ROUTER) {
2113 struct radix_node_head *rnh = NULL;
2114 struct route_event rt_ev;
2115 route_event_init(&rt_ev, route, NULL, rt_evcode);
2116 /*
2117 * We already have a reference on rt. The function
2118 * frees it before returning.
2119 */
2120 RT_UNLOCK(route);
2121 lck_mtx_lock(rnh_lock);
2122 rnh = rt_tables[AF_INET];
2123
0a7de745 2124 if (rnh != NULL) {
5ba3f43e
A
2125 (void) rnh->rnh_walktree(rnh, route_event_walktree,
2126 (void *)&rt_ev);
0a7de745 2127 }
5ba3f43e
A
2128 lck_mtx_unlock(rnh_lock);
2129 RT_LOCK(route);
2130 }
2131 }
2132
39037602
A
2133 if (!qempty(&llinfo->la_holdq)) {
2134 uint32_t held;
2135 struct mbuf *m0 =
2136 _getq_all(&llinfo->la_holdq, NULL, &held, NULL);
2137 if (arp_verbose) {
2138 log(LOG_DEBUG, "%s: sending %u held packets\n",
2139 __func__, held);
2140 }
2141 atomic_add_32(&arpstat.held, -held);
2142 VERIFY(qempty(&llinfo->la_holdq));
b0d623f7 2143 RT_UNLOCK(route);
39236c6e
A
2144 dlil_output(ifp, PF_INET, m0, (caddr_t)route,
2145 rt_key(route), 0, NULL);
b0d623f7
A
2146 RT_REMREF(route);
2147 route = NULL;
91447636 2148 }
b0d623f7 2149
91447636 2150respond:
b0d623f7 2151 if (route != NULL) {
6d2010ae 2152 /* Mark use timestamp if we're going to send a reply */
0a7de745 2153 if (arpop == ARPOP_REQUEST && route->rt_llinfo != NULL) {
6d2010ae 2154 arp_llreach_use(route->rt_llinfo);
0a7de745 2155 }
b0d623f7
A
2156 RT_REMREF_LOCKED(route);
2157 RT_UNLOCK(route);
2158 route = NULL;
91447636 2159 }
b0d623f7 2160
0a7de745 2161 if (arpop != ARPOP_REQUEST) {
b0d623f7 2162 goto done;
0a7de745 2163 }
b0d623f7 2164
39037602 2165 /* See comments at the beginning of this routine */
39236c6e
A
2166 arpstat.rxrequests++;
2167
91447636 2168 /* If we are not the target, check if we should proxy */
6d2010ae 2169 if (target_ip->sin_addr.s_addr != best_ia_sin.sin_addr.s_addr) {
b0d623f7
A
2170 /*
2171 * Find a proxy route; callee holds a reference on the
2172 * route and returns with the route entry locked, upon
2173 * success.
2174 */
c910b4d9
A
2175 error = arp_lookup_route(&target_ip->sin_addr, 0, SIN_PROXY,
2176 &route, ifp->if_index);
b0d623f7
A
2177
2178 if (error == 0) {
2179 RT_LOCK_ASSERT_HELD(route);
b7266188
A
2180 /*
2181 * Return proxied ARP replies only on the interface
2182 * or bridge cluster where this network resides.
2183 * Otherwise we may conflict with the host we are
2184 * proxying for.
2185 */
2186 if (route->rt_ifp != ifp &&
39236c6e
A
2187 (route->rt_ifp->if_bridge != ifp->if_bridge ||
2188 ifp->if_bridge == NULL)) {
2189 RT_REMREF_LOCKED(route);
2190 RT_UNLOCK(route);
2191 goto done;
2192 }
b0d623f7
A
2193 proxied = *SDL(route->rt_gateway);
2194 target_hw = &proxied;
2195 } else {
2196 /*
2197 * We don't have a route entry indicating we should
2198 * use proxy. If we aren't supposed to proxy all,
2199 * we are done.
2200 */
0a7de745 2201 if (!arp_proxyall) {
b0d623f7 2202 goto done;
0a7de745 2203 }
b0d623f7
A
2204
2205 /*
2206 * See if we have a route to the target ip before
2207 * we proxy it.
2208 */
2209 route = rtalloc1_scoped((struct sockaddr *)
2210 (size_t)target_ip, 0, 0, ifp->if_index);
0a7de745 2211 if (!route) {
b0d623f7 2212 goto done;
0a7de745 2213 }
b0d623f7 2214
91447636
A
2215 /*
2216 * Don't proxy for hosts already on the same interface.
2217 */
b0d623f7 2218 RT_LOCK(route);
91447636 2219 if (route->rt_ifp == ifp) {
b0d623f7
A
2220 RT_UNLOCK(route);
2221 rtfree(route);
2222 goto done;
91447636
A
2223 }
2224 }
6d2010ae 2225 /* Mark use timestamp */
0a7de745 2226 if (route->rt_llinfo != NULL) {
6d2010ae 2227 arp_llreach_use(route->rt_llinfo);
0a7de745 2228 }
b0d623f7
A
2229 RT_REMREF_LOCKED(route);
2230 RT_UNLOCK(route);
91447636 2231 }
b0d623f7
A
2232
2233 dlil_send_arp(ifp, ARPOP_REPLY,
39236c6e
A
2234 target_hw, (const struct sockaddr *)target_ip,
2235 sender_hw, (const struct sockaddr *)sender_ip, 0);
b0d623f7
A
2236
2237done:
0a7de745 2238 if (best_ia != NULL) {
6d2010ae 2239 IFA_REMREF(&best_ia->ia_ifa);
0a7de745
A
2240 }
2241 return 0;
91447636
A
2242}
2243
2244void
6d2010ae 2245arp_ifinit(struct ifnet *ifp, struct ifaddr *ifa)
91447636 2246{
6d2010ae
A
2247 struct sockaddr *sa;
2248
2249 IFA_LOCK(ifa);
91447636
A
2250 ifa->ifa_rtrequest = arp_rtrequest;
2251 ifa->ifa_flags |= RTF_CLONING;
6d2010ae
A
2252 sa = ifa->ifa_addr;
2253 IFA_UNLOCK(ifa);
316670eb 2254 dlil_send_arp(ifp, ARPOP_REQUEST, NULL, sa, NULL, sa, 0);
91447636 2255}
39236c6e
A
2256
2257static int
2258arp_getstat SYSCTL_HANDLER_ARGS
2259{
2260#pragma unused(oidp, arg1, arg2)
0a7de745
A
2261 if (req->oldptr == USER_ADDR_NULL) {
2262 req->oldlen = (size_t)sizeof(struct arpstat);
2263 }
39236c6e 2264
0a7de745 2265 return SYSCTL_OUT(req, &arpstat, MIN(sizeof(arpstat), req->oldlen));
39236c6e 2266}