]> git.saurik.com Git - apple/xnu.git/blob - bsd/netinet/in_arp.c
xnu-1699.26.8.tar.gz
[apple/xnu.git] / bsd / netinet / in_arp.c
1 /*
2 * Copyright (c) 2004-2011 Apple Inc. All rights reserved.
3 *
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
5 *
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.
14 *
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
17 *
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.
25 *
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
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>
73 #include <sys/mcache.h>
74 #include <sys/protosw.h>
75 #include <string.h>
76 #include <net/if_arp.h>
77 #include <net/if_dl.h>
78 #include <net/dlil.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>
85
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)
90
91 static const size_t MAX_HW_LEN = 10;
92
93 SYSCTL_DECL(_net_link_ether);
94 SYSCTL_NODE(_net_link_ether, PF_INET, inet, CTLFLAG_RW|CTLFLAG_LOCKED, 0, "");
95
96 /* timer values */
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 */
100
101 /* Apple Hardware SUM16 checksuming */
102 int apple_hwcksum_tx = 1;
103 int apple_hwcksum_rx = 1;
104
105 static int arp_llreach_base = (LL_BASE_REACHABLE / 1000); /* seconds */
106
107 SYSCTL_INT(_net_link_ether_inet, OID_AUTO, prune_intvl,
108 CTLFLAG_RW | CTLFLAG_LOCKED, &arpt_prune, 0, "");
109
110 SYSCTL_INT(_net_link_ether_inet, OID_AUTO, max_age,
111 CTLFLAG_RW | CTLFLAG_LOCKED, &arpt_keep, 0, "");
112
113 SYSCTL_INT(_net_link_ether_inet, OID_AUTO, host_down_time,
114 CTLFLAG_RW | CTLFLAG_LOCKED, &arpt_down, 0, "");
115
116 SYSCTL_INT(_net_link_ether_inet, OID_AUTO, apple_hwcksum_tx,
117 CTLFLAG_RW | CTLFLAG_LOCKED, &apple_hwcksum_tx, 0, "");
118
119 SYSCTL_INT(_net_link_ether_inet, OID_AUTO, apple_hwcksum_rx,
120 CTLFLAG_RW | CTLFLAG_LOCKED, &apple_hwcksum_rx, 0, "");
121
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)");
125
126 struct llinfo_arp {
127 /*
128 * The following are protected by rnh_lock
129 */
130 LIST_ENTRY(llinfo_arp) la_le;
131 struct rtentry *la_rt;
132 /*
133 * The following are protected by rt_lock
134 */
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 */
140 };
141
142 /*
143 * Synchronization notes:
144 *
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().
149 *
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:
153 *
154 * la_le.{le_next,le_prev}, la_rt
155 *
156 * - Routing lock (rnh_lock)
157 *
158 * la_hold, la_asked, la_llreach, la_lastused
159 *
160 * - Routing entry lock (rt_lock)
161 *
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.
166 */
167 static LIST_HEAD(, llinfo_arp) llinfo_arp;
168
169 static int arp_inuse, arp_allocated;
170
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;
175
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, "");
184
185 static int log_arp_warnings = 0; /* Thread safe: no accumulated state */
186
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");
191
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");
197
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");
203
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 *);
211
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);
216
217 extern u_int32_t ipv4_ll_arp_aware;
218
219 static int arpinit_done;
220
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 */
224
225 void
226 arp_init(void)
227 {
228 if (arpinit_done) {
229 log(LOG_NOTICE, "arp_init called more than once (ignored)\n");
230 return;
231 }
232
233 LIST_INIT(&llinfo_arp);
234
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__);
240
241 zone_change(llinfo_arp_zone, Z_EXPAND, TRUE);
242 zone_change(llinfo_arp_zone, Z_CALLERACCT, FALSE);
243
244 arpinit_done = 1;
245
246 /* start timer */
247 timeout(arptimer, (caddr_t)0, hz);
248 }
249
250 static struct llinfo_arp *
251 arp_llinfo_alloc(void)
252 {
253 return (zalloc(llinfo_arp_zone));
254 }
255
256 static void
257 arp_llinfo_free(void *arg)
258 {
259 struct llinfo_arp *la = arg;
260
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);
263 /* NOTREACHED */
264 }
265
266 /* Just in case there's anything there, free it */
267 if (la->la_hold != NULL) {
268 m_freem(la->la_hold);
269 la->la_hold = NULL;
270 }
271
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);
276
277 zfree(llinfo_arp_zone, la);
278 }
279
280 static void
281 arp_llinfo_purge(struct rtentry *rt)
282 {
283 struct llinfo_arp *la = rt->rt_llinfo;
284
285 RT_LOCK_ASSERT_HELD(rt);
286 VERIFY(rt->rt_llinfo_purge == arp_llinfo_purge && la != NULL);
287
288 if (la->la_llreach != NULL) {
289 RT_CONVERT_LOCK(rt);
290 ifnet_llreach_free(la->la_llreach);
291 la->la_llreach = NULL;
292 }
293 la->la_lastused = 0;
294 }
295
296 static void
297 arp_llinfo_get_ri(struct rtentry *rt, struct rt_reach_info *ri)
298 {
299 struct llinfo_arp *la = rt->rt_llinfo;
300 struct if_llreach *lr = la->la_llreach;
301
302 if (lr == NULL) {
303 bzero(ri, sizeof (*ri));
304 } else {
305 IFLR_LOCK(lr);
306 /* Export to rt_reach_info structure */
307 ifnet_lr2ri(lr, ri);
308 /* Export ARP send expiration time */
309 ri->ri_snd_expire = ifnet_llreach_up2cal(lr, la->la_lastused);
310 IFLR_UNLOCK(lr);
311 }
312 }
313
314 void
315 arp_llreach_set_reachable(struct ifnet *ifp, void *addr, unsigned int alen)
316 {
317 /* Nothing more to do if it's disabled */
318 if (arp_llreach_base == 0)
319 return;
320
321 ifnet_llreach_set_reachable(ifp, ETHERTYPE_IP, addr, alen);
322 }
323
324 static __inline void
325 arp_llreach_use(struct llinfo_arp *la)
326 {
327 if (la->la_llreach != NULL)
328 la->la_lastused = net_uptime();
329 }
330
331 static __inline int
332 arp_llreach_reachable(struct llinfo_arp *la)
333 {
334 struct if_llreach *lr;
335 const char *why = NULL;
336
337 /* Nothing more to do if it's disabled; pretend it's reachable */
338 if (arp_llreach_base == 0)
339 return (1);
340
341 if ((lr = la->la_llreach) == NULL) {
342 /*
343 * Link-layer reachability record isn't present for this
344 * ARP entry; pretend it's reachable and use it as is.
345 */
346 return (1);
347 } else if (ifnet_llreach_reachable(lr)) {
348 /*
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.
352 */
353 if (lr->lr_reqcnt == 1)
354 return (1);
355
356 /* Prime it up, if this is the first time */
357 if (la->la_lastused == 0) {
358 VERIFY(la->la_llreach != NULL);
359 arp_llreach_use(la);
360 }
361
362 /*
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."
369 */
370 if (ifnet_llreach_reachable_delta(lr, la->la_lastused))
371 return (1);
372
373 why = "has alias(es) and hasn't been used in a while";
374 } else {
375 why = "haven't heard from it in a while";
376 }
377
378 if (log_arp_warnings) {
379 char tmp[MAX_IPv4_STR_LEN];
380 u_int64_t now = net_uptime();
381
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));
388
389 }
390 return (0);
391 }
392
393 /*
394 * Obtain a link-layer source cache entry for the sender.
395 *
396 * NOTE: This is currently only for ARP/Ethernet.
397 */
398 static void
399 arp_llreach_alloc(struct rtentry *rt, struct ifnet *ifp, void *addr,
400 unsigned int alen, boolean_t solicited)
401 {
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 = "";
411
412 /* Become a regular mutex, just in case */
413 RT_CONVERT_LOCK(rt);
414
415 if ((lr = la->la_llreach) != NULL) {
416 type = (solicited ? "ARP reply" : "ARP announcement");
417 /*
418 * If target has changed, create a new record;
419 * otherwise keep existing record.
420 */
421 IFLR_LOCK(lr);
422 if (bcmp(addr, lr->lr_key.addr, alen) != 0) {
423 IFLR_UNLOCK(lr);
424 /* Purge any link-layer info caching */
425 VERIFY(rt->rt_llinfo_purge != NULL);
426 rt->rt_llinfo_purge(rt);
427 lr = NULL;
428 why = " for different target HW address; "
429 "using new llreach record";
430 } else {
431 lr->lr_probes = 0; /* reset probe count */
432 IFLR_UNLOCK(lr);
433 if (solicited) {
434 why = " for same target HW address; "
435 "keeping existing llreach record";
436 }
437 }
438 }
439
440 if (lr == NULL) {
441 lr = la->la_llreach = ifnet_llreach_alloc(ifp,
442 ETHERTYPE_IP, addr, alen, arp_llreach_base);
443 if (lr != NULL) {
444 lr->lr_probes = 0; /* reset probe count */
445 if (why == NULL)
446 why = "creating new llreach record";
447 }
448 }
449
450 if (log_arp_warnings && lr != NULL && why != NULL) {
451 char tmp[MAX_IPv4_STR_LEN];
452
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)));
456 }
457 }
458 }
459
460 /*
461 * Free an arp entry.
462 */
463 static void
464 arptfree(struct llinfo_arp *la)
465 {
466 struct rtentry *rt = la->la_rt;
467 struct sockaddr_dl *sdl;
468
469 lck_mtx_assert(rnh_lock, LCK_MTX_ASSERT_OWNED);
470 RT_LOCK_ASSERT_HELD(rt);
471
472 if (rt->rt_refcnt > 0 && (sdl = SDL(rt->rt_gateway)) &&
473 sdl->sdl_family == AF_LINK) {
474 sdl->sdl_alen = 0;
475 la->la_asked = 0;
476 rt->rt_flags &= ~RTF_REJECT;
477 RT_UNLOCK(rt);
478 } else if (la->la_persist) {
479 /*
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
483 * this action.
484 */
485 if (rt->rt_refcnt == 0)
486 rt_clear_idleref(rt);
487 RT_UNLOCK(rt);
488 } else {
489 /*
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.
493 */
494 RT_UNLOCK(rt);
495 rtrequest_locked(RTM_DELETE, rt_key(rt), NULL, rt_mask(rt),
496 0, NULL);
497 }
498 }
499
500 void
501 in_arpdrain(void *ignored_arg)
502 {
503 #pragma unused (ignored_arg)
504 struct llinfo_arp *la, *ola;
505 uint64_t timenow;
506
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;
513 RT_LOCK(rt);
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 */
518 else
519 RT_UNLOCK(rt);
520 }
521 lck_mtx_unlock(rnh_lock);
522 }
523
524 void
525 arp_validate(struct rtentry *rt)
526 {
527 struct llinfo_arp *la = rt->rt_llinfo;
528
529 RT_LOCK_ASSERT_HELD(rt);
530 /*
531 * If this is a persistent ARP entry, make it count towards the
532 * interface idleness just like before arptfree() was called.
533 */
534 if (la->la_persist)
535 rt_set_idleref(rt);
536 }
537
538 /*
539 * Timeout routine. Age arp_tab entries periodically.
540 */
541 /* ARGSUSED */
542 static void
543 arptimer(void *ignored_arg)
544 {
545 #pragma unused (ignored_arg)
546 in_arpdrain(NULL);
547 timeout(arptimer, (caddr_t)0, arpt_prune * hz);
548 }
549
550 /*
551 * Parallel to llc_rtrequest.
552 */
553 static void
554 arp_rtrequest(
555 int req,
556 struct rtentry *rt,
557 __unused struct sockaddr *sa)
558 {
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}};
562 uint64_t timenow;
563
564 if (!arpinit_done) {
565 panic("%s: ARP has not been initialized", __func__);
566 /* NOTREACHED */
567 }
568 lck_mtx_assert(rnh_lock, LCK_MTX_ASSERT_OWNED);
569 RT_LOCK_ASSERT_HELD(rt);
570
571 if (rt->rt_flags & RTF_GATEWAY)
572 return;
573 timenow = net_uptime();
574 switch (req) {
575
576 case RTM_ADD:
577 /*
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.
581 */
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) {
586 /*
587 * Case 1: This route should come from a route to iface.
588 */
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;
594 /*
595 * In case we're called before 1.0 sec.
596 * has elapsed.
597 */
598 rt_setexpire(rt, MAX(timenow, 1));
599 }
600 break;
601 }
602 /* Announce a new entry if requested. */
603 if (rt->rt_flags & RTF_ANNOUNCE) {
604 if (la != NULL)
605 arp_llreach_use(la); /* Mark use timestamp */
606 RT_UNLOCK(rt);
607 dlil_send_arp(rt->rt_ifp, ARPOP_REQUEST,
608 SDL(gate), rt_key(rt), NULL, rt_key(rt));
609 RT_LOCK(rt);
610 }
611 /*FALLTHROUGH*/
612 case RTM_RESOLVE:
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");
617 break;
618 }
619 SDL(gate)->sdl_type = rt->rt_ifp->if_type;
620 SDL(gate)->sdl_index = rt->rt_ifp->if_index;
621 if (la != 0)
622 break; /* This happens on a route change */
623 /*
624 * Case 2: This route may come from cloning, or a manual route
625 * add with a LL address.
626 */
627 rt->rt_llinfo = la = arp_llinfo_alloc();
628 if (la == NULL) {
629 if (log_arp_warnings)
630 log(LOG_DEBUG, "%s: malloc failed\n", __func__);
631 break;
632 }
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;
636
637 arp_inuse++, arp_allocated++;
638 Bzero(la, sizeof(*la));
639 la->la_rt = rt;
640 rt->rt_flags |= RTF_LLINFO;
641 LIST_INSERT_HEAD(&llinfo_arp, la, la_le);
642
643 /*
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
648 * it has expired.
649 */
650 if (IN_MULTICAST(ntohl(SIN(rt_key(rt))->sin_addr.s_addr))) {
651 RT_UNLOCK(rt);
652 dlil_resolve_multi(rt->rt_ifp, rt_key(rt), gate,
653 sizeof(struct sockaddr_dl));
654 RT_LOCK(rt);
655 rt_setexpire(rt, 0);
656 }
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),
662 &broadcast_len);
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))) {
669 /*
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.
675 */
676 la->la_persist = 1;
677 rt->rt_flags |= RTF_STATIC;
678 }
679
680 /* Become a regular mutex, just in case */
681 RT_CONVERT_LOCK(rt);
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);
686 /*
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
695 * hardware.
696 */
697 rt_setexpire(rt, 0);
698 ifnet_lladdr_copy_bytes(rt->rt_ifp, LLADDR(SDL(gate)),
699 SDL(gate)->sdl_alen = rt->rt_ifp->if_addrlen);
700 if (useloopback) {
701 if (rt->rt_ifp != lo_ifp) {
702 /*
703 * Purge any link-layer info caching.
704 */
705 if (rt->rt_llinfo_purge != NULL)
706 rt->rt_llinfo_purge(rt);
707
708 /*
709 * Adjust route ref count for the
710 * interfaces.
711 */
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);
715 }
716 }
717 rt->rt_ifp = lo_ifp;
718 }
719 } else {
720 IFA_UNLOCK(rt->rt_ifa);
721 }
722 break;
723
724 case RTM_DELETE:
725 if (la == 0)
726 break;
727 arp_inuse--;
728 /*
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.
733 */
734 LIST_REMOVE(la, la_le);
735 la->la_le.le_next = NULL;
736 la->la_le.le_prev = NULL;
737
738 /*
739 * Purge any link-layer info caching.
740 */
741 if (rt->rt_llinfo_purge != NULL)
742 rt->rt_llinfo_purge(rt);
743
744 rt->rt_flags &= ~RTF_LLINFO;
745 if (la->la_hold != NULL) {
746 m_freem(la->la_hold);
747 la->la_hold = NULL;
748 }
749 }
750 }
751
752 /*
753 * convert hardware address to hex string for logging errors.
754 */
755 static const char *
756 sdl_addr_to_hex(const struct sockaddr_dl *sdl, char * orig_buf, int buflen)
757 {
758 char * buf = orig_buf;
759 int i;
760 const u_char * lladdr = (u_char *)(size_t)sdl->sdl_data;
761 int maxbytes = buflen / 3;
762
763 if (maxbytes > sdl->sdl_alen) {
764 maxbytes = sdl->sdl_alen;
765 }
766 *buf = '\0';
767 for (i = 0; i < maxbytes; i++) {
768 snprintf(buf, 3, "%02x", lladdr[i]);
769 buf += 2;
770 *buf = (i == maxbytes - 1) ? '\0' : ':';
771 buf++;
772 }
773 return (orig_buf);
774 }
775
776 /*
777 * arp_lookup_route will lookup the route for a given address.
778 *
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.
782 */
783 static errno_t
784 arp_lookup_route(const struct in_addr *addr, int create, int proxy,
785 route_t *route, unsigned int ifscope)
786 {
787 struct sockaddr_inarp sin = {sizeof(sin), AF_INET, 0, {0}, {0}, 0, 0};
788 const char *why = NULL;
789 errno_t error = 0;
790 route_t rt;
791
792 *route = NULL;
793
794 sin.sin_addr.s_addr = addr->s_addr;
795 sin.sin_other = proxy ? SIN_PROXY : 0;
796
797 /*
798 * If the destination is a link-local address, don't
799 * constrain the lookup (don't scope it).
800 */
801 if (IN_LINKLOCAL(ntohl(addr->s_addr)))
802 ifscope = IFSCOPE_NONE;
803
804 rt = rtalloc1_scoped((struct sockaddr*)&sin, create, 0, ifscope);
805 if (rt == NULL)
806 return (ENETUNREACH);
807
808 RT_LOCK(rt);
809
810 if (rt->rt_flags & RTF_GATEWAY) {
811 why = "host is not on local network";
812 error = ENETUNREACH;
813 } else if (!(rt->rt_flags & RTF_LLINFO)) {
814 why = "could not allocate llinfo";
815 error = ENOMEM;
816 } else if (rt->rt_gateway->sa_family != AF_LINK) {
817 why = "gateway route is not ours";
818 error = EPROTONOSUPPORT;
819 }
820
821 if (error != 0) {
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,
826 sizeof (tmp)), why);
827 }
828
829 /*
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.
834 */
835 if (rt->rt_refcnt == 1 &&
836 (rt->rt_flags & (RTF_WASCLONED | RTF_STATIC)) ==
837 RTF_WASCLONED) {
838 /*
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.
842 */
843 rt->rt_flags |= RTF_CONDEMNED;
844 RT_UNLOCK(rt);
845 rtrequest(RTM_DELETE, rt_key(rt), rt->rt_gateway,
846 rt_mask(rt), rt->rt_flags, 0);
847 rtfree(rt);
848 } else {
849 RT_REMREF_LOCKED(rt);
850 RT_UNLOCK(rt);
851 }
852 return (error);
853 }
854
855 /*
856 * Caller releases reference and does RT_UNLOCK(rt).
857 */
858 *route = rt;
859 return (0);
860 }
861
862 /*
863 * arp_route_to_gateway_route will find the gateway route for a given route.
864 *
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.
869 *
870 * If the returned route is non-NULL, the caller is responsible for
871 * releasing the reference and unlocking the route.
872 */
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,
876 route_t *out_route)
877 {
878 uint64_t timenow;
879 route_t rt = hint0, hint = hint0;
880 errno_t error = 0;
881
882 *out_route = NULL;
883
884 /*
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.
891 *
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.
895 */
896 if (rt != NULL) {
897 unsigned int ifindex;
898
899 RT_LOCK_SPIN(rt);
900 ifindex = rt->rt_ifp->if_index;
901 RT_ADDREF_LOCKED(rt);
902 if (!(rt->rt_flags & RTF_UP)) {
903 RT_REMREF_LOCKED(rt);
904 RT_UNLOCK(rt);
905 /* route is down, find a new one */
906 hint = rt = rtalloc1_scoped((struct sockaddr *)
907 (size_t)net_dest, 1, 0, ifindex);
908 if (hint != NULL) {
909 RT_LOCK_SPIN(rt);
910 ifindex = rt->rt_ifp->if_index;
911 } else {
912 senderr(EHOSTUNREACH);
913 }
914 }
915
916 /*
917 * We have a reference to "rt" by now; it will either
918 * be released or freed at the end of this routine.
919 */
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;
924
925 /* If there's no gateway rt, look it up */
926 if (gwrt == NULL) {
927 gw = *((struct sockaddr_in *)rt->rt_gateway);
928 RT_UNLOCK(rt);
929 goto lookup;
930 }
931 /* Become a regular mutex */
932 RT_CONVERT_LOCK(rt);
933
934 /*
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.
938 */
939 RT_LOCK_SPIN(gwrt);
940 if (!(gwrt->rt_flags & RTF_UP)) {
941 struct rtentry *ogwrt;
942
943 rt->rt_gwroute = NULL;
944 RT_UNLOCK(gwrt);
945 gw = *((struct sockaddr_in *)rt->rt_gateway);
946 RT_UNLOCK(rt);
947 rtfree(gwrt);
948 lookup:
949 gwrt = rtalloc1_scoped(
950 (struct sockaddr *)&gw, 1, 0, ifindex);
951
952 RT_LOCK(rt);
953 /*
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.
957 */
958 if (!(rt->rt_flags & RTF_UP) ||
959 gwrt == NULL || gwrt == rt ||
960 !equal(SA(&gw), rt->rt_gateway)) {
961 if (gwrt == rt) {
962 RT_REMREF_LOCKED(gwrt);
963 gwrt = NULL;
964 }
965 RT_UNLOCK(rt);
966 if (gwrt != NULL)
967 rtfree(gwrt);
968 senderr(EHOSTUNREACH);
969 }
970
971 /* Remove any existing gwrt */
972 ogwrt = rt->rt_gwroute;
973 if ((rt->rt_gwroute = gwrt) != NULL)
974 RT_ADDREF(gwrt);
975
976 /* Clean up "rt" now while we can */
977 if (rt == hint0) {
978 RT_REMREF_LOCKED(rt);
979 RT_UNLOCK(rt);
980 } else {
981 RT_UNLOCK(rt);
982 rtfree(rt);
983 }
984 rt = gwrt;
985 /* Now free the replaced gwrt */
986 if (ogwrt != NULL)
987 rtfree(ogwrt);
988 /* If still no route to gateway, bail out */
989 if (rt == NULL)
990 senderr(EHOSTUNREACH);
991 } else {
992 RT_ADDREF_LOCKED(gwrt);
993 RT_UNLOCK(gwrt);
994 /* Clean up "rt" now while we can */
995 if (rt == hint0) {
996 RT_REMREF_LOCKED(rt);
997 RT_UNLOCK(rt);
998 } else {
999 RT_UNLOCK(rt);
1000 rtfree(rt);
1001 }
1002 rt = gwrt;
1003 }
1004
1005 /* rt == gwrt; if it is now down, give up */
1006 RT_LOCK_SPIN(rt);
1007 if (!(rt->rt_flags & RTF_UP)) {
1008 RT_UNLOCK(rt);
1009 senderr(EHOSTUNREACH);
1010 }
1011 }
1012
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) {
1019 RT_UNLOCK(rt);
1020 senderr(rt == hint ? EHOSTDOWN : EHOSTUNREACH);
1021 }
1022 }
1023
1024 /* Become a regular mutex */
1025 RT_CONVERT_LOCK(rt);
1026
1027 /* Caller is responsible for cleaning up "rt" */
1028 *out_route = rt;
1029 }
1030 return (0);
1031
1032 bad:
1033 /* Clean up route (either it is "rt" or "gwrt") */
1034 if (rt != NULL) {
1035 RT_LOCK_SPIN(rt);
1036 if (rt == hint0) {
1037 RT_REMREF_LOCKED(rt);
1038 RT_UNLOCK(rt);
1039 } else {
1040 RT_UNLOCK(rt);
1041 rtfree(rt);
1042 }
1043 }
1044 return (error);
1045 }
1046 #undef senderr
1047
1048 /*
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
1052 * placeholder.
1053 */
1054 errno_t
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,
1057 mbuf_t packet)
1058 {
1059 route_t route = NULL; /* output route */
1060 errno_t result = 0;
1061 struct sockaddr_dl *gateway;
1062 struct llinfo_arp *llinfo = NULL;
1063 uint64_t timenow;
1064 int unreachable = 0;
1065
1066 if (net_dest->sin_family != AF_INET)
1067 return (EAFNOSUPPORT);
1068
1069 if ((ifp->if_flags & (IFF_UP|IFF_RUNNING)) != (IFF_UP|IFF_RUNNING))
1070 return (ENETDOWN);
1071
1072 /*
1073 * If we were given a route, verify the route and grab the gateway
1074 */
1075 if (hint != NULL) {
1076 /*
1077 * Callee holds a reference on the route and returns
1078 * with the route entry locked, upon success.
1079 */
1080 result = arp_route_to_gateway_route((const struct sockaddr*)
1081 net_dest, hint, &route);
1082 if (result != 0)
1083 return (result);
1084 if (route != NULL)
1085 RT_LOCK_ASSERT_HELD(route);
1086 }
1087
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),
1093 &broadcast_len);
1094 if (result == 0) {
1095 ll_dest->sdl_alen = broadcast_len;
1096 ll_dest->sdl_family = AF_LINK;
1097 ll_dest->sdl_len = sizeof(struct sockaddr_dl);
1098 }
1099 goto release;
1100 }
1101 if (packet->m_flags & M_MCAST) {
1102 if (route != NULL)
1103 RT_UNLOCK(route);
1104 result = dlil_resolve_multi(ifp,
1105 (const struct sockaddr*)net_dest,
1106 (struct sockaddr*)ll_dest, ll_dest_len);
1107 if (route != NULL)
1108 RT_LOCK(route);
1109 goto release;
1110 }
1111
1112 /*
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.
1116 */
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);
1122 RT_UNLOCK(route);
1123 } else {
1124 RT_UNLOCK(route);
1125 rtfree(route);
1126 }
1127 }
1128 /*
1129 * Callee holds a reference on the route and returns
1130 * with the route entry locked, upon success.
1131 */
1132 result = arp_lookup_route(&net_dest->sin_addr, 1, 0, &route,
1133 ifp->if_index);
1134 if (result == 0)
1135 RT_LOCK_ASSERT_HELD(route);
1136 }
1137
1138 if (result || route == NULL || (llinfo = route->rt_llinfo) == NULL) {
1139 char tmp[MAX_IPv4_STR_LEN];
1140
1141 /* In case result is 0 but no route, return an error */
1142 if (result == 0)
1143 result = EHOSTUNREACH;
1144
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,
1149 tmp, sizeof(tmp)));
1150 goto release;
1151 }
1152
1153 /*
1154 * Now that we have the right route, is it filled in?
1155 */
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));
1165 result = 0;
1166 arp_llreach_use(llinfo); /* Mark use timestamp */
1167 goto release;
1168 } else if (unreachable) {
1169 /*
1170 * Discard existing answer in case we need to probe.
1171 */
1172 gateway->sdl_alen = 0;
1173 }
1174
1175 if (ifp->if_flags & IFF_NOARP) {
1176 result = ENOTSUP;
1177 goto release;
1178 }
1179
1180 /*
1181 * Route wasn't complete/valid. We need to arp.
1182 */
1183 if (packet != NULL) {
1184 if (llinfo->la_hold != NULL)
1185 m_freem(llinfo->la_hold);
1186 llinfo->la_hold = packet;
1187 }
1188
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;
1197
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);
1205 }
1206 IFA_LOCK_SPIN(rt_ifa);
1207 IFA_ADDREF_LOCKED(rt_ifa);
1208 sa = rt_ifa->ifa_addr;
1209 IFA_UNLOCK(rt_ifa);
1210 arp_llreach_use(llinfo); /* Mark use timestamp */
1211 RT_UNLOCK(route);
1212 dlil_send_arp(ifp, ARPOP_REQUEST, NULL,
1213 sa, NULL, (const struct sockaddr*)net_dest);
1214 IFA_REMREF(rt_ifa);
1215 RT_LOCK(route);
1216 result = EJUSTRETURN;
1217 goto release;
1218 } else {
1219 route->rt_flags |= RTF_REJECT;
1220 rt_setexpire(route, rt_expiry(route,
1221 route->rt_expire, arpt_down));
1222 llinfo->la_asked = 0;
1223 /*
1224 * Clear la_hold; don't free the packet since
1225 * we're not returning EJUSTRETURN; the caller
1226 * will handle the freeing.
1227 */
1228 llinfo->la_hold = NULL;
1229 result = EHOSTUNREACH;
1230 goto release;
1231 }
1232 }
1233 }
1234
1235 /* The packet is now held inside la_hold (can "packet" be NULL?) */
1236 result = EJUSTRETURN;
1237
1238 release:
1239 if (route != NULL) {
1240 if (route == hint) {
1241 RT_REMREF_LOCKED(route);
1242 RT_UNLOCK(route);
1243 } else {
1244 RT_UNLOCK(route);
1245 rtfree(route);
1246 }
1247 }
1248 return (result);
1249 }
1250
1251 errno_t
1252 arp_ip_handle_input(
1253 ifnet_t ifp,
1254 u_short arpop,
1255 const struct sockaddr_dl *sender_hw,
1256 const struct sockaddr_in *sender_ip,
1257 const struct sockaddr_in *target_ip)
1258 {
1259 char ipv4str[MAX_IPv4_STR_LEN];
1260 struct sockaddr_dl proxied;
1261 struct sockaddr_dl *gateway, *target_hw = NULL;
1262 struct ifaddr *ifa;
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;
1269 errno_t error;
1270 int created_announcement = 0;
1271 int bridged = 0, is_bridge = 0;
1272
1273 /* Do not respond to requests for 0.0.0.0 */
1274 if (target_ip->sin_addr.s_addr == 0 && arpop == ARPOP_REQUEST)
1275 goto done;
1276
1277 if (ifp->if_bridge)
1278 bridged = 1;
1279 if (ifp->if_type == IFT_BRIDGE)
1280 is_bridge = 1;
1281
1282 /*
1283 * Determine if this ARP is for us
1284 * For a bridge, we want to check the address irrespective
1285 * of the receive interface.
1286 */
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) {
1293 best_ia = ia;
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);
1298 goto match;
1299 }
1300 IFA_UNLOCK(&ia->ia_ifa);
1301 }
1302
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) {
1308 best_ia = ia;
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);
1313 goto match;
1314 }
1315 IFA_UNLOCK(&ia->ia_ifa);
1316 }
1317
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)
1322 /*
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.
1327 */
1328 if (is_bridge) {
1329 TAILQ_FOREACH(ia, INADDR_HASH(target_ip->sin_addr.s_addr),
1330 ia_hash) {
1331 IFA_LOCK_SPIN(&ia->ia_ifa);
1332 if (BDG_MEMBER_MATCHES_ARP(target_ip->sin_addr.s_addr,
1333 ifp, ia)) {
1334 ifp = ia->ia_ifp;
1335 best_ia = ia;
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);
1340 goto match;
1341 }
1342 IFA_UNLOCK(&ia->ia_ifa);
1343 }
1344 }
1345 lck_rw_done(in_ifaddr_rwlock);
1346
1347 /*
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.
1351 */
1352 ifnet_lock_shared(ifp);
1353 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
1354 IFA_LOCK_SPIN(ifa);
1355 if (ifa->ifa_addr->sa_family != AF_INET) {
1356 IFA_UNLOCK(ifa);
1357 continue;
1358 }
1359 best_ia = (struct in_ifaddr *)ifa;
1360 best_ia_sin = best_ia->ia_addr;
1361 IFA_ADDREF_LOCKED(ifa);
1362 IFA_UNLOCK(ifa);
1363 ifnet_lock_done(ifp);
1364 goto match;
1365 }
1366 ifnet_lock_done(ifp);
1367
1368 /*
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.
1371 */
1372 if (!bridged || best_ia == NULL)
1373 goto done;
1374
1375 match:
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)) {
1378 goto done;
1379 }
1380
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)));
1393
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);
1409
1410 goto respond;
1411 }
1412
1413 /*
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.
1418 */
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);
1422
1423 if (error == 0)
1424 RT_LOCK_ASSERT_HELD(route);
1425
1426 if (error || route == 0 || route->rt_gateway == 0) {
1427 if (arpop != ARPOP_REQUEST) {
1428 goto respond;
1429 }
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) {
1435 /*
1436 * Verify this ARP probe doesn't conflict with an IPv4LL we know of
1437 * on another interface.
1438 */
1439 if (route != NULL) {
1440 RT_REMREF_LOCKED(route);
1441 RT_UNLOCK(route);
1442 route = NULL;
1443 }
1444 /*
1445 * Callee holds a reference on the route and returns
1446 * with the route entry locked, upon success.
1447 */
1448 error = arp_lookup_route(&target_ip->sin_addr, 0, 0,
1449 &route, ifp->if_index);
1450
1451 if (error == 0)
1452 RT_LOCK_ASSERT_HELD(route);
1453
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)) {
1460 /*
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
1464 * address.
1465 */
1466 if (log_arp_warnings) {
1467 log(LOG_INFO,
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,
1472 sizeof(ipv4str)),
1473 route->rt_ifp->if_name, route->rt_ifp->if_unit);
1474 log(LOG_INFO,
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);
1478 }
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);
1484 RT_UNLOCK(route);
1485 route = NULL;
1486 /*
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.
1493 */
1494 ifnet_lock_shared(ifp);
1495 ifa = ifp->if_lladdr;
1496 IFA_ADDREF(ifa);
1497 ifnet_lock_done(ifp);
1498 dlil_send_arp_internal(ifp, ARPOP_REQUEST,
1499 SDL(ifa->ifa_addr),
1500 (const struct sockaddr*)sender_ip, sender_hw,
1501 (const struct sockaddr*)target_ip);
1502 IFA_REMREF(ifa);
1503 ifa = NULL;
1504 }
1505 }
1506 goto respond;
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);
1514 RT_UNLOCK(route);
1515 route = NULL;
1516 }
1517 /*
1518 * Callee holds a reference on the route and
1519 * returns with the route entry locked, upon
1520 * success.
1521 */
1522 error = arp_lookup_route(&sender_ip->sin_addr,
1523 1, 0, &route, ifp->if_index);
1524
1525 if (error == 0)
1526 RT_LOCK_ASSERT_HELD(route);
1527
1528 if (error == 0 && route != NULL && route->rt_gateway != NULL) {
1529 created_announcement = 1;
1530 }
1531 }
1532 if (created_announcement == 0) {
1533 goto respond;
1534 }
1535 } else {
1536 goto respond;
1537 }
1538 }
1539
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,
1549 sizeof(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);
1554 goto respond;
1555 }
1556 else {
1557 /* Don't change a permanent address */
1558 if (route->rt_expire == 0) {
1559 goto respond;
1560 }
1561
1562 /*
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.
1568 */
1569 RT_UNLOCK(route);
1570 lck_mtx_lock(rnh_lock);
1571 RT_LOCK(route);
1572 /*
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.
1578 */
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);
1584 RT_UNLOCK(route);
1585 route = NULL;
1586 lck_mtx_unlock(rnh_lock);
1587 goto respond;
1588 }
1589 if (route->rt_ifp != ifp) {
1590 /*
1591 * Purge any link-layer info caching.
1592 */
1593 if (route->rt_llinfo_purge != NULL)
1594 route->rt_llinfo_purge(route);
1595
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);
1600 }
1601 }
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;
1606 RT_UNLOCK(route);
1607 lck_mtx_unlock(rnh_lock);
1608 RT_LOCK(route);
1609 /* Don't bother if the route is down */
1610 if (!(route->rt_flags & RTF_UP))
1611 goto respond;
1612 /* Refresh gateway pointer */
1613 gateway = SDL(route->rt_gateway);
1614 }
1615 RT_LOCK_ASSERT_HELD(route);
1616 }
1617
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,
1623 sizeof(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);
1627 }
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,
1633 sizeof(buf)),
1634 inet_ntop(AF_INET, &sender_ip->sin_addr,
1635 ipv4str, sizeof(ipv4str)),
1636 ifp->if_name, ifp->if_unit);
1637 }
1638 goto respond;
1639 }
1640 }
1641
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);
1645
1646 /* Update the expire time for the route and clear the reject flag */
1647 if (route->rt_expire) {
1648 uint64_t timenow;
1649
1650 timenow = net_uptime();
1651 rt_setexpire(route,
1652 rt_expiry(route, timenow, arpt_keep));
1653 }
1654 route->rt_flags &= ~RTF_REJECT;
1655
1656 /* cache the gateway (sender HW) address */
1657 arp_llreach_alloc(route, ifp, LLADDR(gateway), gateway->sdl_alen,
1658 (arpop == ARPOP_REPLY));
1659
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) {
1664 struct mbuf *m0;
1665 m0 = llinfo->la_hold;
1666 llinfo->la_hold = NULL;
1667
1668 RT_UNLOCK(route);
1669 dlil_output(ifp, PF_INET, m0, (caddr_t)route, rt_key(route), 0);
1670 RT_REMREF(route);
1671 route = NULL;
1672 }
1673
1674 respond:
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);
1680 RT_UNLOCK(route);
1681 route = NULL;
1682 }
1683
1684 if (arpop != ARPOP_REQUEST)
1685 goto done;
1686
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) {
1689 /*
1690 * Find a proxy route; callee holds a reference on the
1691 * route and returns with the route entry locked, upon
1692 * success.
1693 */
1694 error = arp_lookup_route(&target_ip->sin_addr, 0, SIN_PROXY,
1695 &route, ifp->if_index);
1696
1697 if (error == 0) {
1698 RT_LOCK_ASSERT_HELD(route);
1699 /*
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
1703 * proxying for.
1704 */
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);
1709 RT_UNLOCK(route);
1710 goto done;
1711 }
1712 proxied = *SDL(route->rt_gateway);
1713 target_hw = &proxied;
1714 } else {
1715 /*
1716 * We don't have a route entry indicating we should
1717 * use proxy. If we aren't supposed to proxy all,
1718 * we are done.
1719 */
1720 if (!arp_proxyall)
1721 goto done;
1722
1723 /*
1724 * See if we have a route to the target ip before
1725 * we proxy it.
1726 */
1727 route = rtalloc1_scoped((struct sockaddr *)
1728 (size_t)target_ip, 0, 0, ifp->if_index);
1729 if (!route)
1730 goto done;
1731
1732 /*
1733 * Don't proxy for hosts already on the same interface.
1734 */
1735 RT_LOCK(route);
1736 if (route->rt_ifp == ifp) {
1737 RT_UNLOCK(route);
1738 rtfree(route);
1739 goto done;
1740 }
1741 }
1742 /* Mark use timestamp */
1743 if (route->rt_llinfo != NULL)
1744 arp_llreach_use(route->rt_llinfo);
1745 RT_REMREF_LOCKED(route);
1746 RT_UNLOCK(route);
1747 }
1748
1749 dlil_send_arp(ifp, ARPOP_REPLY,
1750 target_hw, (const struct sockaddr*)target_ip,
1751 sender_hw, (const struct sockaddr*)sender_ip);
1752
1753 done:
1754 if (best_ia != NULL)
1755 IFA_REMREF(&best_ia->ia_ifa);
1756 return 0;
1757 }
1758
1759 void
1760 arp_ifinit(struct ifnet *ifp, struct ifaddr *ifa)
1761 {
1762 struct sockaddr *sa;
1763
1764 IFA_LOCK(ifa);
1765 ifa->ifa_rtrequest = arp_rtrequest;
1766 ifa->ifa_flags |= RTF_CLONING;
1767 sa = ifa->ifa_addr;
1768 IFA_UNLOCK(ifa);
1769 dlil_send_arp(ifp, ARPOP_REQUEST, NULL, sa, NULL, sa);
1770 }