]> git.saurik.com Git - apple/xnu.git/blob - bsd/netinet/in_arp.c
xnu-1504.7.4.tar.gz
[apple/xnu.git] / bsd / netinet / in_arp.c
1 /*
2 * Copyright (c) 2004-2009 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 <string.h>
74 #include <net/if_arp.h>
75 #include <net/if_dl.h>
76 #include <net/dlil.h>
77 #include <net/if_types.h>
78 #include <net/route.h>
79 #include <netinet/if_ether.h>
80 #include <netinet/in_var.h>
81 #include <kern/zalloc.h>
82
83 #define SA(p) ((struct sockaddr *)(p))
84 #define SIN(s) ((struct sockaddr_in *)s)
85 #define CONST_LLADDR(s) ((const u_char*)((s)->sdl_data + (s)->sdl_nlen))
86 #define rt_expire rt_rmx.rmx_expire
87 #define equal(a1, a2) (bcmp((caddr_t)(a1), (caddr_t)(a2), (a1)->sa_len) == 0)
88
89 static const size_t MAX_HW_LEN = 10;
90
91 SYSCTL_DECL(_net_link_ether);
92 SYSCTL_NODE(_net_link_ether, PF_INET, inet, CTLFLAG_RW|CTLFLAG_LOCKED, 0, "");
93
94 /* timer values */
95 static int arpt_prune = (5*60*1); /* walk list every 5 minutes */
96 static int arpt_keep = (20*60); /* once resolved, good for 20 more minutes */
97 static int arpt_down = 20; /* once declared down, don't send for 20 sec */
98
99 /* Apple Hardware SUM16 checksuming */
100 int apple_hwcksum_tx = 1;
101 int apple_hwcksum_rx = 1;
102
103 SYSCTL_INT(_net_link_ether_inet, OID_AUTO, prune_intvl, CTLFLAG_RW,
104 &arpt_prune, 0, "");
105 SYSCTL_INT(_net_link_ether_inet, OID_AUTO, max_age, CTLFLAG_RW,
106 &arpt_keep, 0, "");
107 SYSCTL_INT(_net_link_ether_inet, OID_AUTO, host_down_time, CTLFLAG_RW,
108 &arpt_down, 0, "");
109 SYSCTL_INT(_net_link_ether_inet, OID_AUTO, apple_hwcksum_tx, CTLFLAG_RW,
110 &apple_hwcksum_tx, 0, "");
111 SYSCTL_INT(_net_link_ether_inet, OID_AUTO, apple_hwcksum_rx, CTLFLAG_RW,
112 &apple_hwcksum_rx, 0, "");
113
114 struct llinfo_arp {
115 /*
116 * The following are protected by rnh_lock
117 */
118 LIST_ENTRY(llinfo_arp) la_le;
119 struct rtentry *la_rt;
120 /*
121 * The following are protected by rt_lock
122 */
123 struct mbuf *la_hold; /* last packet until resolved/timeout */
124 int32_t la_asked; /* last time we QUERIED for this addr */
125 };
126
127 /*
128 * Synchronization notes:
129 *
130 * The global list of ARP entries are stored in llinfo_arp; an entry
131 * gets inserted into the list when the route is created and gets
132 * removed from the list when it is deleted; this is done as part
133 * of RTM_ADD/RTM_RESOLVE/RTM_DELETE in arp_rtrequest().
134 *
135 * Because rnh_lock and rt_lock for the entry are held during those
136 * operations, the same locks (and thus lock ordering) must be used
137 * elsewhere to access the relevant data structure fields:
138 *
139 * la_le.{le_next,le_prev}, la_rt
140 *
141 * - Routing lock (rnh_lock)
142 *
143 * la_hold, la_asked
144 *
145 * - Routing entry lock (rt_lock)
146 *
147 * Due to the dependency on rt_lock, llinfo_arp has the same lifetime
148 * as the route entry itself. When a route is deleted (RTM_DELETE),
149 * it is simply removed from the global list but the memory is not
150 * freed until the route itself is freed.
151 */
152 static LIST_HEAD(, llinfo_arp) llinfo_arp;
153
154 static int arp_inuse, arp_allocated;
155
156 static int arp_maxtries = 5;
157 static int useloopback = 1; /* use loopback interface for local traffic */
158 static int arp_proxyall = 0;
159 static int arp_sendllconflict = 0;
160
161 SYSCTL_INT(_net_link_ether_inet, OID_AUTO, maxtries, CTLFLAG_RW,
162 &arp_maxtries, 0, "");
163 SYSCTL_INT(_net_link_ether_inet, OID_AUTO, useloopback, CTLFLAG_RW,
164 &useloopback, 0, "");
165 SYSCTL_INT(_net_link_ether_inet, OID_AUTO, proxyall, CTLFLAG_RW,
166 &arp_proxyall, 0, "");
167 SYSCTL_INT(_net_link_ether_inet, OID_AUTO, sendllconflict, CTLFLAG_RW,
168 &arp_sendllconflict, 0, "");
169
170 static int log_arp_warnings = 0;
171
172 SYSCTL_INT(_net_link_ether_inet, OID_AUTO, log_arp_warnings, CTLFLAG_RW,
173 &log_arp_warnings, 0,
174 "log arp warning messages");
175
176 static int keep_announcements = 1;
177 SYSCTL_INT(_net_link_ether_inet, OID_AUTO, keep_announcements, CTLFLAG_RW,
178 &keep_announcements, 0,
179 "keep arp announcements");
180
181 static int send_conflicting_probes = 1;
182 SYSCTL_INT(_net_link_ether_inet, OID_AUTO, send_conflicting_probes, CTLFLAG_RW,
183 &send_conflicting_probes, 0,
184 "send conflicting link-local arp probes");
185
186 static errno_t arp_lookup_route(const struct in_addr *, int,
187 int, route_t *, unsigned int);
188 static void arptimer(void *);
189 static struct llinfo_arp *arp_llinfo_alloc(void);
190 static void arp_llinfo_free(void *);
191
192 extern u_int32_t ipv4_ll_arp_aware;
193
194 static int arpinit_done;
195
196 static struct zone *llinfo_arp_zone;
197 #define LLINFO_ARP_ZONE_MAX 256 /* maximum elements in zone */
198 #define LLINFO_ARP_ZONE_NAME "llinfo_arp" /* name for zone */
199
200 void
201 arp_init(void)
202 {
203 if (arpinit_done) {
204 log(LOG_NOTICE, "arp_init called more than once (ignored)\n");
205 return;
206 }
207
208 LIST_INIT(&llinfo_arp);
209
210 llinfo_arp_zone = zinit(sizeof (struct llinfo_arp),
211 LLINFO_ARP_ZONE_MAX * sizeof (struct llinfo_arp), 0,
212 LLINFO_ARP_ZONE_NAME);
213 if (llinfo_arp_zone == NULL)
214 panic("%s: failed allocating llinfo_arp_zone", __func__);
215
216 zone_change(llinfo_arp_zone, Z_EXPAND, TRUE);
217
218 arpinit_done = 1;
219
220 /* start timer */
221 timeout(arptimer, (caddr_t)0, hz);
222 }
223
224 static struct llinfo_arp *
225 arp_llinfo_alloc(void)
226 {
227 return (zalloc(llinfo_arp_zone));
228 }
229
230 static void
231 arp_llinfo_free(void *arg)
232 {
233 struct llinfo_arp *la = arg;
234
235 if (la->la_le.le_next != NULL || la->la_le.le_prev != NULL) {
236 panic("%s: trying to free %p when it is in use", __func__, la);
237 /* NOTREACHED */
238 }
239
240 /* Just in case there's anything there, free it */
241 if (la->la_hold != NULL) {
242 m_freem(la->la_hold);
243 la->la_hold = NULL;
244 }
245
246 zfree(llinfo_arp_zone, la);
247 }
248
249 /*
250 * Free an arp entry.
251 */
252 static void
253 arptfree(struct llinfo_arp *la)
254 {
255 struct rtentry *rt = la->la_rt;
256 struct sockaddr_dl *sdl;
257
258 lck_mtx_assert(rnh_lock, LCK_MTX_ASSERT_OWNED);
259 RT_LOCK_ASSERT_HELD(rt);
260
261 if (rt->rt_refcnt > 0 && (sdl = SDL(rt->rt_gateway)) &&
262 sdl->sdl_family == AF_LINK) {
263 sdl->sdl_alen = 0;
264 la->la_asked = 0;
265 rt->rt_flags &= ~RTF_REJECT;
266 RT_UNLOCK(rt);
267 } else {
268 /*
269 * Safe to drop rt_lock and use rt_key, since holding
270 * rnh_lock here prevents another thread from calling
271 * rt_setgate() on this route.
272 */
273 RT_UNLOCK(rt);
274 rtrequest_locked(RTM_DELETE, rt_key(rt), NULL, rt_mask(rt),
275 0, NULL);
276 }
277 }
278
279 void
280 in_arpdrain(void *ignored_arg)
281 {
282 #pragma unused (ignored_arg)
283 struct llinfo_arp *la, *ola;
284 struct timeval timenow;
285
286 lck_mtx_lock(rnh_lock);
287 la = llinfo_arp.lh_first;
288 getmicrotime(&timenow);
289 while ((ola = la) != 0) {
290 struct rtentry *rt = la->la_rt;
291 la = la->la_le.le_next;
292 RT_LOCK(rt);
293 if (rt->rt_expire && rt->rt_expire <= timenow.tv_sec)
294 arptfree(ola); /* timer has expired, clear */
295 else
296 RT_UNLOCK(rt);
297 }
298 lck_mtx_unlock(rnh_lock);
299 }
300
301 /*
302 * Timeout routine. Age arp_tab entries periodically.
303 */
304 /* ARGSUSED */
305 static void
306 arptimer(void *ignored_arg)
307 {
308 #pragma unused (ignored_arg)
309 in_arpdrain(NULL);
310 timeout(arptimer, (caddr_t)0, arpt_prune * hz);
311 }
312
313 /*
314 * Parallel to llc_rtrequest.
315 */
316 static void
317 arp_rtrequest(
318 int req,
319 struct rtentry *rt,
320 __unused struct sockaddr *sa)
321 {
322 struct sockaddr *gate = rt->rt_gateway;
323 struct llinfo_arp *la = rt->rt_llinfo;
324 static struct sockaddr_dl null_sdl = {sizeof(null_sdl), AF_LINK, 0, 0, 0, 0, 0, {0}};
325 struct timeval timenow;
326
327 if (!arpinit_done) {
328 panic("%s: ARP has not been initialized", __func__);
329 /* NOTREACHED */
330 }
331 lck_mtx_assert(rnh_lock, LCK_MTX_ASSERT_OWNED);
332 RT_LOCK_ASSERT_HELD(rt);
333
334 if (rt->rt_flags & RTF_GATEWAY)
335 return;
336 getmicrotime(&timenow);
337 switch (req) {
338
339 case RTM_ADD:
340 /*
341 * XXX: If this is a manually added route to interface
342 * such as older version of routed or gated might provide,
343 * restore cloning bit.
344 */
345 if ((rt->rt_flags & RTF_HOST) == 0 &&
346 SIN(rt_mask(rt))->sin_addr.s_addr != 0xffffffff)
347 rt->rt_flags |= RTF_CLONING;
348 if (rt->rt_flags & RTF_CLONING) {
349 /*
350 * Case 1: This route should come from a route to iface.
351 */
352 if (rt_setgate(rt, rt_key(rt),
353 (struct sockaddr *)&null_sdl) == 0) {
354 gate = rt->rt_gateway;
355 SDL(gate)->sdl_type = rt->rt_ifp->if_type;
356 SDL(gate)->sdl_index = rt->rt_ifp->if_index;
357 /*
358 * In case we're called before 1.0 sec.
359 * has elapsed.
360 */
361 rt->rt_expire = MAX(timenow.tv_sec, 1);
362 }
363 break;
364 }
365 /* Announce a new entry if requested. */
366 if (rt->rt_flags & RTF_ANNOUNCE) {
367 RT_UNLOCK(rt);
368 dlil_send_arp(rt->rt_ifp, ARPOP_REQUEST,
369 SDL(gate), rt_key(rt), NULL, rt_key(rt));
370 RT_LOCK(rt);
371 }
372 /*FALLTHROUGH*/
373 case RTM_RESOLVE:
374 if (gate->sa_family != AF_LINK ||
375 gate->sa_len < sizeof(null_sdl)) {
376 if (log_arp_warnings)
377 log(LOG_DEBUG, "arp_rtrequest: bad gateway value\n");
378 break;
379 }
380 SDL(gate)->sdl_type = rt->rt_ifp->if_type;
381 SDL(gate)->sdl_index = rt->rt_ifp->if_index;
382 if (la != 0)
383 break; /* This happens on a route change */
384 /*
385 * Case 2: This route may come from cloning, or a manual route
386 * add with a LL address.
387 */
388 rt->rt_llinfo = la = arp_llinfo_alloc();
389 if (la == NULL) {
390 if (log_arp_warnings)
391 log(LOG_DEBUG, "%s: malloc failed\n", __func__);
392 break;
393 }
394 rt->rt_llinfo_free = arp_llinfo_free;
395
396 arp_inuse++, arp_allocated++;
397 Bzero(la, sizeof(*la));
398 la->la_rt = rt;
399 rt->rt_flags |= RTF_LLINFO;
400 LIST_INSERT_HEAD(&llinfo_arp, la, la_le);
401
402 /*
403 * This keeps the multicast addresses from showing up
404 * in `arp -a' listings as unresolved. It's not actually
405 * functional. Then the same for broadcast.
406 */
407 if (IN_MULTICAST(ntohl(SIN(rt_key(rt))->sin_addr.s_addr))) {
408 RT_UNLOCK(rt);
409 dlil_resolve_multi(rt->rt_ifp, rt_key(rt), gate,
410 sizeof(struct sockaddr_dl));
411 RT_LOCK(rt);
412 rt->rt_expire = 0;
413 }
414 else if (in_broadcast(SIN(rt_key(rt))->sin_addr, rt->rt_ifp)) {
415 struct sockaddr_dl *gate_ll = SDL(gate);
416 size_t broadcast_len;
417 ifnet_llbroadcast_copy_bytes(rt->rt_ifp,
418 LLADDR(gate_ll), sizeof(gate_ll->sdl_data),
419 &broadcast_len);
420 gate_ll->sdl_alen = broadcast_len;
421 gate_ll->sdl_family = AF_LINK;
422 gate_ll->sdl_len = sizeof(struct sockaddr_dl);
423 /* In case we're called before 1.0 sec. has elapsed */
424 rt->rt_expire = MAX(timenow.tv_sec, 1);
425 }
426
427 if (SIN(rt_key(rt))->sin_addr.s_addr ==
428 (IA_SIN(rt->rt_ifa))->sin_addr.s_addr) {
429 /*
430 * This test used to be
431 * if (loif.if_flags & IFF_UP)
432 * It allowed local traffic to be forced
433 * through the hardware by configuring the loopback down.
434 * However, it causes problems during network configuration
435 * for boards that can't receive packets they send.
436 * It is now necessary to clear "useloopback" and remove
437 * the route to force traffic out to the hardware.
438 */
439 rt->rt_expire = 0;
440 ifnet_lladdr_copy_bytes(rt->rt_ifp, LLADDR(SDL(gate)), SDL(gate)->sdl_alen = 6);
441 if (useloopback) {
442 #if IFNET_ROUTE_REFCNT
443 /* Adjust route ref count for the interfaces */
444 if (rt->rt_if_ref_fn != NULL &&
445 rt->rt_ifp != lo_ifp) {
446 rt->rt_if_ref_fn(lo_ifp, 1);
447 rt->rt_if_ref_fn(rt->rt_ifp, -1);
448 }
449 #endif /* IFNET_ROUTE_REFCNT */
450 rt->rt_ifp = lo_ifp;
451 }
452
453 }
454 break;
455
456 case RTM_DELETE:
457 if (la == 0)
458 break;
459 arp_inuse--;
460 /*
461 * Unchain it but defer the actual freeing until the route
462 * itself is to be freed. rt->rt_llinfo still points to
463 * llinfo_arp, and likewise, la->la_rt still points to this
464 * route entry, except that RTF_LLINFO is now cleared.
465 */
466 LIST_REMOVE(la, la_le);
467 la->la_le.le_next = NULL;
468 la->la_le.le_prev = NULL;
469 rt->rt_flags &= ~RTF_LLINFO;
470 if (la->la_hold != NULL)
471 m_freem(la->la_hold);
472 la->la_hold = NULL;
473 }
474 }
475
476 /*
477 * convert hardware address to hex string for logging errors.
478 */
479 static const char *
480 sdl_addr_to_hex(const struct sockaddr_dl *sdl, char * orig_buf, int buflen)
481 {
482 char * buf = orig_buf;
483 int i;
484 const u_char * lladdr = (u_char *)(size_t)sdl->sdl_data;
485 int maxbytes = buflen / 3;
486
487 if (maxbytes > sdl->sdl_alen) {
488 maxbytes = sdl->sdl_alen;
489 }
490 *buf = '\0';
491 for (i = 0; i < maxbytes; i++) {
492 snprintf(buf, 3, "%02x", lladdr[i]);
493 buf += 2;
494 *buf = (i == maxbytes - 1) ? '\0' : ':';
495 buf++;
496 }
497 return (orig_buf);
498 }
499
500 /*
501 * arp_lookup_route will lookup the route for a given address.
502 *
503 * The address must be for a host on a local network on this interface.
504 * If the returned route is non-NULL, the route is locked and the caller
505 * is responsible for unlocking it and releasing its reference.
506 */
507 static errno_t
508 arp_lookup_route(const struct in_addr *addr, int create, int proxy,
509 route_t *route, unsigned int ifscope)
510 {
511 struct sockaddr_inarp sin = {sizeof(sin), AF_INET, 0, {0}, {0}, 0, 0};
512 const char *why = NULL;
513 errno_t error = 0;
514 route_t rt;
515
516 *route = NULL;
517
518 sin.sin_addr.s_addr = addr->s_addr;
519 sin.sin_other = proxy ? SIN_PROXY : 0;
520
521 rt = rtalloc1_scoped((struct sockaddr*)&sin, create, 0, ifscope);
522 if (rt == NULL)
523 return (ENETUNREACH);
524
525 RT_LOCK(rt);
526
527 if (rt->rt_flags & RTF_GATEWAY) {
528 why = "host is not on local network";
529 error = ENETUNREACH;
530 } else if (!(rt->rt_flags & RTF_LLINFO)) {
531 why = "could not allocate llinfo";
532 error = ENOMEM;
533 } else if (rt->rt_gateway->sa_family != AF_LINK) {
534 why = "gateway route is not ours";
535 error = EPROTONOSUPPORT;
536 }
537
538 if (error != 0) {
539 if (create && log_arp_warnings) {
540 char tmp[MAX_IPv4_STR_LEN];
541 log(LOG_DEBUG, "arplookup link#%d %s failed: %s\n",
542 ifscope, inet_ntop(AF_INET, addr, tmp,
543 sizeof (tmp)), why);
544 }
545
546 /*
547 * If there are no references to this route, and it is
548 * a cloned route, and not static, and ARP had created
549 * the route, then purge it from the routing table as
550 * it is probably bogus.
551 */
552 if (rt->rt_refcnt == 1 &&
553 (rt->rt_flags & (RTF_WASCLONED | RTF_STATIC)) ==
554 RTF_WASCLONED) {
555 /*
556 * Prevent another thread from modiying rt_key,
557 * rt_gateway via rt_setgate() after rt_lock is
558 * dropped by marking the route as defunct.
559 */
560 rt->rt_flags |= RTF_CONDEMNED;
561 RT_UNLOCK(rt);
562 rtrequest(RTM_DELETE, rt_key(rt), rt->rt_gateway,
563 rt_mask(rt), rt->rt_flags, 0);
564 rtfree(rt);
565 } else {
566 RT_REMREF_LOCKED(rt);
567 RT_UNLOCK(rt);
568 }
569 return (error);
570 }
571
572 /*
573 * Caller releases reference and does RT_UNLOCK(rt).
574 */
575 *route = rt;
576 return (0);
577 }
578
579 /*
580 * arp_route_to_gateway_route will find the gateway route for a given route.
581 *
582 * If the route is down, look the route up again.
583 * If the route goes through a gateway, get the route to the gateway.
584 * If the gateway route is down, look it up again.
585 * If the route is set to reject, verify it hasn't expired.
586 *
587 * If the returned route is non-NULL, the caller is responsible for
588 * releasing the reference and unlocking the route.
589 */
590 #define senderr(e) { error = (e); goto bad; }
591 __private_extern__ errno_t
592 arp_route_to_gateway_route(const struct sockaddr *net_dest, route_t hint0,
593 route_t *out_route)
594 {
595 struct timeval timenow;
596 route_t rt = hint0, hint = hint0;
597 errno_t error = 0;
598
599 *out_route = NULL;
600
601 /*
602 * Next hop determination. Because we may involve the gateway route
603 * in addition to the original route, locking is rather complicated.
604 * The general concept is that regardless of whether the route points
605 * to the original route or to the gateway route, this routine takes
606 * an extra reference on such a route. This extra reference will be
607 * released at the end.
608 *
609 * Care must be taken to ensure that the "hint0" route never gets freed
610 * via rtfree(), since the caller may have stored it inside a struct
611 * route with a reference held for that placeholder.
612 */
613 if (rt != NULL) {
614 unsigned int ifindex;
615
616 RT_LOCK_SPIN(rt);
617 ifindex = rt->rt_ifp->if_index;
618 RT_ADDREF_LOCKED(rt);
619 if (!(rt->rt_flags & RTF_UP)) {
620 RT_REMREF_LOCKED(rt);
621 RT_UNLOCK(rt);
622 /* route is down, find a new one */
623 hint = rt = rtalloc1_scoped((struct sockaddr *)
624 (size_t)net_dest, 1, 0, ifindex);
625 if (hint != NULL) {
626 RT_LOCK_SPIN(rt);
627 ifindex = rt->rt_ifp->if_index;
628 } else {
629 senderr(EHOSTUNREACH);
630 }
631 }
632
633 /*
634 * We have a reference to "rt" by now; it will either
635 * be released or freed at the end of this routine.
636 */
637 RT_LOCK_ASSERT_HELD(rt);
638 if (rt->rt_flags & RTF_GATEWAY) {
639 struct rtentry *gwrt = rt->rt_gwroute;
640 struct sockaddr_in gw;
641
642 /* If there's no gateway rt, look it up */
643 if (gwrt == NULL) {
644 gw = *((struct sockaddr_in *)rt->rt_gateway);
645 RT_UNLOCK(rt);
646 goto lookup;
647 }
648 /* Become a regular mutex */
649 RT_CONVERT_LOCK(rt);
650
651 /*
652 * Take gwrt's lock while holding route's lock;
653 * this is okay since gwrt never points back
654 * to "rt", so no lock ordering issues.
655 */
656 RT_LOCK_SPIN(gwrt);
657 if (!(gwrt->rt_flags & RTF_UP)) {
658 struct rtentry *ogwrt;
659
660 rt->rt_gwroute = NULL;
661 RT_UNLOCK(gwrt);
662 gw = *((struct sockaddr_in *)rt->rt_gateway);
663 RT_UNLOCK(rt);
664 rtfree(gwrt);
665 lookup:
666 gwrt = rtalloc1_scoped(
667 (struct sockaddr *)&gw, 1, 0, ifindex);
668
669 RT_LOCK(rt);
670 /*
671 * Bail out if the route is down, no route
672 * to gateway, circular route, or if the
673 * gateway portion of "rt" has changed.
674 */
675 if (!(rt->rt_flags & RTF_UP) ||
676 gwrt == NULL || gwrt == rt ||
677 !equal(SA(&gw), rt->rt_gateway)) {
678 if (gwrt == rt) {
679 RT_REMREF_LOCKED(gwrt);
680 gwrt = NULL;
681 }
682 RT_UNLOCK(rt);
683 if (gwrt != NULL)
684 rtfree(gwrt);
685 senderr(EHOSTUNREACH);
686 }
687
688 /* Remove any existing gwrt */
689 ogwrt = rt->rt_gwroute;
690 if ((rt->rt_gwroute = gwrt) != NULL)
691 RT_ADDREF(gwrt);
692
693 /* Clean up "rt" now while we can */
694 if (rt == hint0) {
695 RT_REMREF_LOCKED(rt);
696 RT_UNLOCK(rt);
697 } else {
698 RT_UNLOCK(rt);
699 rtfree(rt);
700 }
701 rt = gwrt;
702 /* Now free the replaced gwrt */
703 if (ogwrt != NULL)
704 rtfree(ogwrt);
705 /* If still no route to gateway, bail out */
706 if (rt == NULL)
707 senderr(EHOSTUNREACH);
708 } else {
709 RT_ADDREF_LOCKED(gwrt);
710 RT_UNLOCK(gwrt);
711 /* Clean up "rt" now while we can */
712 if (rt == hint0) {
713 RT_REMREF_LOCKED(rt);
714 RT_UNLOCK(rt);
715 } else {
716 RT_UNLOCK(rt);
717 rtfree(rt);
718 }
719 rt = gwrt;
720 }
721
722 /* rt == gwrt; if it is now down, give up */
723 RT_LOCK_SPIN(rt);
724 if (!(rt->rt_flags & RTF_UP)) {
725 RT_UNLOCK(rt);
726 senderr(EHOSTUNREACH);
727 }
728 }
729
730 if (rt->rt_flags & RTF_REJECT) {
731 getmicrotime(&timenow);
732 if (rt->rt_rmx.rmx_expire == 0 ||
733 timenow.tv_sec < rt->rt_rmx.rmx_expire) {
734 RT_UNLOCK(rt);
735 senderr(rt == hint ? EHOSTDOWN : EHOSTUNREACH);
736 }
737 }
738
739 /* Become a regular mutex */
740 RT_CONVERT_LOCK(rt);
741
742 /* Caller is responsible for cleaning up "rt" */
743 *out_route = rt;
744 }
745 return (0);
746
747 bad:
748 /* Clean up route (either it is "rt" or "gwrt") */
749 if (rt != NULL) {
750 RT_LOCK_SPIN(rt);
751 if (rt == hint0) {
752 RT_REMREF_LOCKED(rt);
753 RT_UNLOCK(rt);
754 } else {
755 RT_UNLOCK(rt);
756 rtfree(rt);
757 }
758 }
759 return (error);
760 }
761 #undef senderr
762
763 /*
764 * This is the ARP pre-output routine; care must be taken to ensure that
765 * the "hint" route never gets freed via rtfree(), since the caller may
766 * have stored it inside a struct route with a reference held for that
767 * placeholder.
768 */
769 errno_t
770 arp_lookup_ip(ifnet_t ifp, const struct sockaddr_in *net_dest,
771 struct sockaddr_dl *ll_dest, size_t ll_dest_len, route_t hint,
772 mbuf_t packet)
773 {
774 route_t route = NULL; /* output route */
775 errno_t result = 0;
776 struct sockaddr_dl *gateway;
777 struct llinfo_arp *llinfo;
778 struct timeval timenow;
779
780 if (net_dest->sin_family != AF_INET)
781 return (EAFNOSUPPORT);
782
783 if ((ifp->if_flags & (IFF_UP|IFF_RUNNING)) != (IFF_UP|IFF_RUNNING))
784 return (ENETDOWN);
785
786 /*
787 * If we were given a route, verify the route and grab the gateway
788 */
789 if (hint != NULL) {
790 /*
791 * Callee holds a reference on the route and returns
792 * with the route entry locked, upon success.
793 */
794 result = arp_route_to_gateway_route((const struct sockaddr*)
795 net_dest, hint, &route);
796 if (result != 0)
797 return (result);
798 if (route != NULL)
799 RT_LOCK_ASSERT_HELD(route);
800 }
801
802 if (packet->m_flags & M_BCAST) {
803 size_t broadcast_len;
804 bzero(ll_dest, ll_dest_len);
805 result = ifnet_llbroadcast_copy_bytes(ifp, LLADDR(ll_dest),
806 ll_dest_len - offsetof(struct sockaddr_dl, sdl_data),
807 &broadcast_len);
808 if (result == 0) {
809 ll_dest->sdl_alen = broadcast_len;
810 ll_dest->sdl_family = AF_LINK;
811 ll_dest->sdl_len = sizeof(struct sockaddr_dl);
812 }
813 goto release;
814 }
815 if (packet->m_flags & M_MCAST) {
816 if (route != NULL)
817 RT_UNLOCK(route);
818 result = dlil_resolve_multi(ifp,
819 (const struct sockaddr*)net_dest,
820 (struct sockaddr*)ll_dest, ll_dest_len);
821 if (route != NULL)
822 RT_LOCK(route);
823 goto release;
824 }
825
826 /*
827 * If we didn't find a route, or the route doesn't have
828 * link layer information, trigger the creation of the
829 * route and link layer information.
830 */
831 if (route == NULL || route->rt_llinfo == NULL) {
832 /* Clean up now while we can */
833 if (route != NULL) {
834 if (route == hint) {
835 RT_REMREF_LOCKED(route);
836 RT_UNLOCK(route);
837 } else {
838 RT_UNLOCK(route);
839 rtfree(route);
840 }
841 }
842 /*
843 * Callee holds a reference on the route and returns
844 * with the route entry locked, upon success.
845 */
846 result = arp_lookup_route(&net_dest->sin_addr, 1, 0, &route,
847 ifp->if_index);
848 if (result == 0)
849 RT_LOCK_ASSERT_HELD(route);
850 }
851
852 if (result || route == NULL || route->rt_llinfo == NULL) {
853 char tmp[MAX_IPv4_STR_LEN];
854
855 /* In case result is 0 but no route, return an error */
856 if (result == 0)
857 result = EHOSTUNREACH;
858
859 if (log_arp_warnings &&
860 route != NULL && route->rt_llinfo == NULL)
861 log(LOG_DEBUG, "arpresolve: can't allocate llinfo "
862 "for %s\n", inet_ntop(AF_INET, &net_dest->sin_addr,
863 tmp, sizeof(tmp)));
864 goto release;
865 }
866
867 /*
868 * Now that we have the right route, is it filled in?
869 */
870 gateway = SDL(route->rt_gateway);
871 getmicrotime(&timenow);
872 if ((route->rt_rmx.rmx_expire == 0 ||
873 route->rt_rmx.rmx_expire > timenow.tv_sec) && gateway != NULL &&
874 gateway->sdl_family == AF_LINK && gateway->sdl_alen != 0) {
875 bcopy(gateway, ll_dest, MIN(gateway->sdl_len, ll_dest_len));
876 result = 0;
877 goto release;
878 }
879
880 if (ifp->if_flags & IFF_NOARP) {
881 result = ENOTSUP;
882 goto release;
883 }
884
885 /*
886 * Route wasn't complete/valid. We need to arp.
887 */
888 llinfo = route->rt_llinfo;
889 if (packet != NULL) {
890 if (llinfo->la_hold != NULL)
891 m_freem(llinfo->la_hold);
892 llinfo->la_hold = packet;
893 }
894
895 if (route->rt_rmx.rmx_expire) {
896 route->rt_flags &= ~RTF_REJECT;
897 if (llinfo->la_asked == 0 ||
898 route->rt_rmx.rmx_expire != timenow.tv_sec) {
899 route->rt_rmx.rmx_expire = timenow.tv_sec;
900 if (llinfo->la_asked++ < arp_maxtries) {
901 struct ifaddr *rt_ifa = route->rt_ifa;
902 ifaref(rt_ifa);
903 RT_UNLOCK(route);
904 dlil_send_arp(ifp, ARPOP_REQUEST, NULL,
905 rt_ifa->ifa_addr, NULL,
906 (const struct sockaddr*)net_dest);
907 ifafree(rt_ifa);
908 RT_LOCK(route);
909 result = EJUSTRETURN;
910 goto release;
911 } else {
912 route->rt_flags |= RTF_REJECT;
913 route->rt_rmx.rmx_expire = rt_expiry(route,
914 route->rt_rmx.rmx_expire, arpt_down);
915 llinfo->la_asked = 0;
916 llinfo->la_hold = NULL;
917 result = EHOSTUNREACH;
918 goto release;
919 }
920 }
921 }
922
923 /* The packet is now held inside la_hold (can "packet" be NULL?) */
924 result = EJUSTRETURN;
925
926 release:
927 if (route != NULL) {
928 if (route == hint) {
929 RT_REMREF_LOCKED(route);
930 RT_UNLOCK(route);
931 } else {
932 RT_UNLOCK(route);
933 rtfree(route);
934 }
935 }
936 return (result);
937 }
938
939 errno_t
940 arp_ip_handle_input(
941 ifnet_t ifp,
942 u_short arpop,
943 const struct sockaddr_dl *sender_hw,
944 const struct sockaddr_in *sender_ip,
945 const struct sockaddr_in *target_ip)
946 {
947 char ipv4str[MAX_IPv4_STR_LEN];
948 struct sockaddr_dl proxied;
949 struct sockaddr_dl *gateway, *target_hw = NULL;
950 struct ifaddr *ifa;
951 struct in_ifaddr *ia;
952 struct in_ifaddr *best_ia = NULL;
953 route_t route = NULL;
954 char buf[3 * MAX_HW_LEN]; // enough for MAX_HW_LEN byte hw address
955 struct llinfo_arp *llinfo;
956 errno_t error;
957 int created_announcement = 0;
958 int bridged = 0, is_bridge = 0;
959
960 /* Do not respond to requests for 0.0.0.0 */
961 if (target_ip->sin_addr.s_addr == 0 && arpop == ARPOP_REQUEST)
962 goto done;
963
964 if (ifp->if_bridge)
965 bridged = 1;
966 if (ifp->if_type == IFT_BRIDGE)
967 is_bridge = 1;
968
969 /*
970 * Determine if this ARP is for us
971 * For a bridge, we want to check the address irrespective
972 * of the receive interface.
973 */
974 lck_rw_lock_shared(in_ifaddr_rwlock);
975 TAILQ_FOREACH(ia, INADDR_HASH(target_ip->sin_addr.s_addr), ia_hash) {
976 if (((bridged && ia->ia_ifp->if_bridge != NULL) ||
977 (ia->ia_ifp == ifp)) &&
978 ia->ia_addr.sin_addr.s_addr == target_ip->sin_addr.s_addr) {
979 best_ia = ia;
980 ifaref(&best_ia->ia_ifa);
981 lck_rw_done(in_ifaddr_rwlock);
982 goto match;
983 }
984 }
985
986 TAILQ_FOREACH(ia, INADDR_HASH(sender_ip->sin_addr.s_addr), ia_hash) {
987 if (((bridged && ia->ia_ifp->if_bridge != NULL) ||
988 (ia->ia_ifp == ifp)) &&
989 ia->ia_addr.sin_addr.s_addr == sender_ip->sin_addr.s_addr) {
990 best_ia = ia;
991 ifaref(&best_ia->ia_ifa);
992 lck_rw_done(in_ifaddr_rwlock);
993 goto match;
994 }
995 }
996
997 #define BDG_MEMBER_MATCHES_ARP(addr, ifp, ia) \
998 (ia->ia_ifp->if_bridge == ifp->if_softc && \
999 !bcmp(ifnet_lladdr(ia->ia_ifp), ifnet_lladdr(ifp), ifp->if_addrlen) && \
1000 addr == ia->ia_addr.sin_addr.s_addr)
1001 /*
1002 * Check the case when bridge shares its MAC address with
1003 * some of its children, so packets are claimed by bridge
1004 * itself (bridge_input() does it first), but they are really
1005 * meant to be destined to the bridge member.
1006 */
1007 if (is_bridge) {
1008 TAILQ_FOREACH(ia, INADDR_HASH(target_ip->sin_addr.s_addr), ia_hash) {
1009 if (BDG_MEMBER_MATCHES_ARP(target_ip->sin_addr.s_addr, ifp, ia)) {
1010 ifp = ia->ia_ifp;
1011 best_ia = ia;
1012 ifaref(&best_ia->ia_ifa);
1013 lck_rw_done(in_ifaddr_rwlock);
1014 goto match;
1015 }
1016 }
1017 }
1018 lck_rw_done(in_ifaddr_rwlock);
1019
1020 /*
1021 * No match, use the first inet address on the receive interface
1022 * as a dummy address for the rest of the function; we may be
1023 * proxying for another address.
1024 */
1025 ifnet_lock_shared(ifp);
1026 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
1027 if (ifa->ifa_addr->sa_family != AF_INET)
1028 continue;
1029 best_ia = (struct in_ifaddr *)ifa;
1030 ifaref(&best_ia->ia_ifa);
1031 ifnet_lock_done(ifp);
1032 goto match;
1033 }
1034 ifnet_lock_done(ifp);
1035
1036 /*
1037 * If we're not a bridge member, or if we are but there's no
1038 * IPv4 address to use for the interface, drop the packet.
1039 */
1040 if (!bridged || best_ia == NULL)
1041 goto done;
1042
1043 match:
1044 /* If the packet is from this interface, ignore the packet */
1045 if (!bcmp(CONST_LLADDR(sender_hw), ifnet_lladdr(ifp), sender_hw->sdl_len)) {
1046 goto done;
1047 }
1048
1049 /* Check for a conflict */
1050 if (!bridged && sender_ip->sin_addr.s_addr == best_ia->ia_addr.sin_addr.s_addr) {
1051 struct kev_msg ev_msg;
1052 struct kev_in_collision *in_collision;
1053 u_char storage[sizeof(struct kev_in_collision) + MAX_HW_LEN];
1054 in_collision = (struct kev_in_collision*)storage;
1055 log(LOG_ERR, "%s%d duplicate IP address %s sent from address %s\n",
1056 ifp->if_name, ifp->if_unit,
1057 inet_ntop(AF_INET, &sender_ip->sin_addr, ipv4str, sizeof(ipv4str)),
1058 sdl_addr_to_hex(sender_hw, buf, sizeof(buf)));
1059
1060 /* Send a kernel event so anyone can learn of the conflict */
1061 in_collision->link_data.if_family = ifp->if_family;
1062 in_collision->link_data.if_unit = ifp->if_unit;
1063 strncpy(&in_collision->link_data.if_name[0], ifp->if_name, IFNAMSIZ);
1064 in_collision->ia_ipaddr = sender_ip->sin_addr;
1065 in_collision->hw_len = sender_hw->sdl_alen < MAX_HW_LEN ? sender_hw->sdl_alen : MAX_HW_LEN;
1066 bcopy(CONST_LLADDR(sender_hw), (caddr_t)in_collision->hw_addr, in_collision->hw_len);
1067 ev_msg.vendor_code = KEV_VENDOR_APPLE;
1068 ev_msg.kev_class = KEV_NETWORK_CLASS;
1069 ev_msg.kev_subclass = KEV_INET_SUBCLASS;
1070 ev_msg.event_code = KEV_INET_ARPCOLLISION;
1071 ev_msg.dv[0].data_ptr = in_collision;
1072 ev_msg.dv[0].data_length = sizeof(struct kev_in_collision) + in_collision->hw_len;
1073 ev_msg.dv[1].data_length = 0;
1074 kev_post_msg(&ev_msg);
1075
1076 goto respond;
1077 }
1078
1079 /*
1080 * Look up the routing entry. If it doesn't exist and we are the
1081 * target, and the sender isn't 0.0.0.0, go ahead and create one.
1082 * Callee holds a reference on the route and returns with the route
1083 * entry locked, upon success.
1084 */
1085 error = arp_lookup_route(&sender_ip->sin_addr,
1086 (target_ip->sin_addr.s_addr == best_ia->ia_addr.sin_addr.s_addr &&
1087 sender_ip->sin_addr.s_addr != 0), 0, &route, ifp->if_index);
1088
1089 if (error == 0)
1090 RT_LOCK_ASSERT_HELD(route);
1091
1092 if (error || route == 0 || route->rt_gateway == 0) {
1093 if (arpop != ARPOP_REQUEST) {
1094 goto respond;
1095 }
1096 if (arp_sendllconflict
1097 && send_conflicting_probes != 0
1098 && (ifp->if_eflags & IFEF_ARPLL) != 0
1099 && IN_LINKLOCAL(ntohl(target_ip->sin_addr.s_addr))
1100 && sender_ip->sin_addr.s_addr == 0) {
1101 /*
1102 * Verify this ARP probe doesn't conflict with an IPv4LL we know of
1103 * on another interface.
1104 */
1105 if (route != NULL) {
1106 RT_REMREF_LOCKED(route);
1107 RT_UNLOCK(route);
1108 route = NULL;
1109 }
1110 /*
1111 * Callee holds a reference on the route and returns
1112 * with the route entry locked, upon success.
1113 */
1114 error = arp_lookup_route(&target_ip->sin_addr, 0, 0,
1115 &route, ifp->if_index);
1116
1117 if (error == 0)
1118 RT_LOCK_ASSERT_HELD(route);
1119
1120 if (error == 0 && route && route->rt_gateway) {
1121 gateway = SDL(route->rt_gateway);
1122 if (route->rt_ifp != ifp && gateway->sdl_alen != 0
1123 && (gateway->sdl_alen != sender_hw->sdl_alen
1124 || bcmp(CONST_LLADDR(gateway), CONST_LLADDR(sender_hw),
1125 gateway->sdl_alen) != 0)) {
1126 /*
1127 * A node is probing for an IPv4LL we know exists on a
1128 * different interface. We respond with a conflicting probe
1129 * to force the new device to pick a different IPv4LL
1130 * address.
1131 */
1132 if (log_arp_warnings) {
1133 log(LOG_INFO,
1134 "arp: %s on %s%d sent probe for %s, already on %s%d\n",
1135 sdl_addr_to_hex(sender_hw, buf, sizeof(buf)),
1136 ifp->if_name, ifp->if_unit,
1137 inet_ntop(AF_INET, &target_ip->sin_addr, ipv4str,
1138 sizeof(ipv4str)),
1139 route->rt_ifp->if_name, route->rt_ifp->if_unit);
1140 log(LOG_INFO,
1141 "arp: sending conflicting probe to %s on %s%d\n",
1142 sdl_addr_to_hex(sender_hw, buf, sizeof(buf)),
1143 ifp->if_name, ifp->if_unit);
1144 }
1145 /* We're done with the route */
1146 RT_REMREF_LOCKED(route);
1147 RT_UNLOCK(route);
1148 route = NULL;
1149 /*
1150 * Send a conservative unicast "ARP probe".
1151 * This should force the other device to pick a new number.
1152 * This will not force the device to pick a new number if the device
1153 * has already assigned that number.
1154 * This will not imply to the device that we own that address.
1155 */
1156 ifnet_lock_shared(ifp);
1157 ifa = TAILQ_FIRST(&ifp->if_addrhead);
1158 if (ifa != NULL)
1159 ifaref(ifa);
1160 ifnet_lock_done(ifp);
1161 dlil_send_arp_internal(ifp, ARPOP_REQUEST,
1162 ifa != NULL ? SDL(ifa->ifa_addr) : NULL,
1163 (const struct sockaddr*)sender_ip, sender_hw,
1164 (const struct sockaddr*)target_ip);
1165 if (ifa != NULL) {
1166 ifafree(ifa);
1167 ifa = NULL;
1168 }
1169 }
1170 }
1171 goto respond;
1172 } else if (keep_announcements != 0
1173 && target_ip->sin_addr.s_addr == sender_ip->sin_addr.s_addr) {
1174 /* don't create entry if link-local address and link-local is disabled */
1175 if (!IN_LINKLOCAL(ntohl(sender_ip->sin_addr.s_addr))
1176 || (ifp->if_eflags & IFEF_ARPLL) != 0) {
1177 if (route != NULL) {
1178 RT_REMREF_LOCKED(route);
1179 RT_UNLOCK(route);
1180 route = NULL;
1181 }
1182 /*
1183 * Callee holds a reference on the route and
1184 * returns with the route entry locked, upon
1185 * success.
1186 */
1187 error = arp_lookup_route(&sender_ip->sin_addr,
1188 1, 0, &route, ifp->if_index);
1189
1190 if (error == 0)
1191 RT_LOCK_ASSERT_HELD(route);
1192
1193 if (error == 0 && route != NULL && route->rt_gateway != NULL) {
1194 created_announcement = 1;
1195 }
1196 }
1197 if (created_announcement == 0) {
1198 goto respond;
1199 }
1200 } else {
1201 goto respond;
1202 }
1203 }
1204
1205 RT_LOCK_ASSERT_HELD(route);
1206 gateway = SDL(route->rt_gateway);
1207 if (!bridged && route->rt_ifp != ifp) {
1208 if (!IN_LINKLOCAL(ntohl(sender_ip->sin_addr.s_addr)) || (ifp->if_eflags & IFEF_ARPLL) == 0) {
1209 if (log_arp_warnings)
1210 log(LOG_ERR, "arp: %s is on %s%d but got reply from %s on %s%d\n",
1211 inet_ntop(AF_INET, &sender_ip->sin_addr, ipv4str,
1212 sizeof(ipv4str)),
1213 route->rt_ifp->if_name,
1214 route->rt_ifp->if_unit,
1215 sdl_addr_to_hex(sender_hw, buf, sizeof(buf)),
1216 ifp->if_name, ifp->if_unit);
1217 goto respond;
1218 }
1219 else {
1220 /* Don't change a permanent address */
1221 if (route->rt_rmx.rmx_expire == 0) {
1222 goto respond;
1223 }
1224
1225 /*
1226 * We're about to check and/or change the route's ifp
1227 * and ifa, so do the lock dance: drop rt_lock, hold
1228 * rnh_lock and re-hold rt_lock to avoid violating the
1229 * lock ordering. We have an extra reference on the
1230 * route, so it won't go away while we do this.
1231 */
1232 RT_UNLOCK(route);
1233 lck_mtx_lock(rnh_lock);
1234 RT_LOCK(route);
1235 /*
1236 * Don't change the cloned route away from the
1237 * parent's interface if the address did resolve
1238 * or if the route is defunct. rt_ifp on both
1239 * the parent and the clone can now be freely
1240 * accessed now that we have acquired rnh_lock.
1241 */
1242 gateway = SDL(route->rt_gateway);
1243 if ((gateway->sdl_alen != 0 && route->rt_parent &&
1244 route->rt_parent->rt_ifp == route->rt_ifp) ||
1245 (route->rt_flags & RTF_CONDEMNED)) {
1246 RT_REMREF_LOCKED(route);
1247 RT_UNLOCK(route);
1248 route = NULL;
1249 lck_mtx_unlock(rnh_lock);
1250 goto respond;
1251 }
1252 #if IFNET_ROUTE_REFCNT
1253 /* Adjust route ref count for the interfaces */
1254 if (route->rt_if_ref_fn != NULL &&
1255 route->rt_ifp != ifp) {
1256 route->rt_if_ref_fn(ifp, 1);
1257 route->rt_if_ref_fn(route->rt_ifp, -1);
1258 }
1259 #endif /* IFNET_ROUTE_REFCNT */
1260 /* Change the interface when the existing route is on */
1261 route->rt_ifp = ifp;
1262 rtsetifa(route, &best_ia->ia_ifa);
1263 gateway->sdl_index = ifp->if_index;
1264 RT_UNLOCK(route);
1265 lck_mtx_unlock(rnh_lock);
1266 RT_LOCK(route);
1267 /* Don't bother if the route is down */
1268 if (!(route->rt_flags & RTF_UP))
1269 goto respond;
1270 /* Refresh gateway pointer */
1271 gateway = SDL(route->rt_gateway);
1272 }
1273 RT_LOCK_ASSERT_HELD(route);
1274 }
1275
1276 if (gateway->sdl_alen && bcmp(LLADDR(gateway), CONST_LLADDR(sender_hw), gateway->sdl_alen)) {
1277 if (route->rt_rmx.rmx_expire && log_arp_warnings) {
1278 char buf2[3 * MAX_HW_LEN];
1279 log(LOG_INFO, "arp: %s moved from %s to %s on %s%d\n",
1280 inet_ntop(AF_INET, &sender_ip->sin_addr, ipv4str,
1281 sizeof(ipv4str)),
1282 sdl_addr_to_hex(gateway, buf, sizeof(buf)),
1283 sdl_addr_to_hex(sender_hw, buf2, sizeof(buf2)),
1284 ifp->if_name, ifp->if_unit);
1285 }
1286 else if (route->rt_rmx.rmx_expire == 0) {
1287 if (log_arp_warnings) {
1288 log(LOG_ERR, "arp: %s attempts to modify "
1289 "permanent entry for %s on %s%d\n",
1290 sdl_addr_to_hex(sender_hw, buf,
1291 sizeof(buf)),
1292 inet_ntop(AF_INET, &sender_ip->sin_addr,
1293 ipv4str, sizeof(ipv4str)),
1294 ifp->if_name, ifp->if_unit);
1295 }
1296 goto respond;
1297 }
1298 }
1299
1300 /* Copy the sender hardware address in to the route's gateway address */
1301 gateway->sdl_alen = sender_hw->sdl_alen;
1302 bcopy(CONST_LLADDR(sender_hw), LLADDR(gateway), gateway->sdl_alen);
1303
1304 /* Update the expire time for the route and clear the reject flag */
1305 if (route->rt_rmx.rmx_expire) {
1306 struct timeval timenow;
1307
1308 getmicrotime(&timenow);
1309 route->rt_rmx.rmx_expire =
1310 rt_expiry(route, timenow.tv_sec, arpt_keep);
1311 }
1312 route->rt_flags &= ~RTF_REJECT;
1313
1314 /* update the llinfo, send a queued packet if there is one */
1315 llinfo = route->rt_llinfo;
1316 llinfo->la_asked = 0;
1317 if (llinfo->la_hold) {
1318 struct mbuf *m0;
1319 m0 = llinfo->la_hold;
1320 llinfo->la_hold = 0;
1321
1322 RT_UNLOCK(route);
1323 dlil_output(ifp, PF_INET, m0, (caddr_t)route, rt_key(route), 0);
1324 RT_REMREF(route);
1325 route = NULL;
1326 }
1327
1328 respond:
1329 if (route != NULL) {
1330 RT_REMREF_LOCKED(route);
1331 RT_UNLOCK(route);
1332 route = NULL;
1333 }
1334
1335 if (arpop != ARPOP_REQUEST)
1336 goto done;
1337
1338 /* If we are not the target, check if we should proxy */
1339 if (target_ip->sin_addr.s_addr != best_ia->ia_addr.sin_addr.s_addr) {
1340 /*
1341 * Find a proxy route; callee holds a reference on the
1342 * route and returns with the route entry locked, upon
1343 * success.
1344 */
1345 error = arp_lookup_route(&target_ip->sin_addr, 0, SIN_PROXY,
1346 &route, ifp->if_index);
1347
1348 if (error == 0) {
1349 RT_LOCK_ASSERT_HELD(route);
1350 /*
1351 * Return proxied ARP replies only on the interface
1352 * or bridge cluster where this network resides.
1353 * Otherwise we may conflict with the host we are
1354 * proxying for.
1355 */
1356 if (route->rt_ifp != ifp &&
1357 (route->rt_ifp->if_bridge != ifp->if_bridge ||
1358 ifp->if_bridge == NULL)) {
1359 RT_REMREF_LOCKED(route);
1360 RT_UNLOCK(route);
1361 goto done;
1362 }
1363 proxied = *SDL(route->rt_gateway);
1364 target_hw = &proxied;
1365 } else {
1366 /*
1367 * We don't have a route entry indicating we should
1368 * use proxy. If we aren't supposed to proxy all,
1369 * we are done.
1370 */
1371 if (!arp_proxyall)
1372 goto done;
1373
1374 /*
1375 * See if we have a route to the target ip before
1376 * we proxy it.
1377 */
1378 route = rtalloc1_scoped((struct sockaddr *)
1379 (size_t)target_ip, 0, 0, ifp->if_index);
1380 if (!route)
1381 goto done;
1382
1383 /*
1384 * Don't proxy for hosts already on the same interface.
1385 */
1386 RT_LOCK(route);
1387 if (route->rt_ifp == ifp) {
1388 RT_UNLOCK(route);
1389 rtfree(route);
1390 goto done;
1391 }
1392 }
1393 RT_REMREF_LOCKED(route);
1394 RT_UNLOCK(route);
1395 }
1396
1397 dlil_send_arp(ifp, ARPOP_REPLY,
1398 target_hw, (const struct sockaddr*)target_ip,
1399 sender_hw, (const struct sockaddr*)sender_ip);
1400
1401 done:
1402 if (best_ia != NULL)
1403 ifafree(&best_ia->ia_ifa);
1404 return 0;
1405 }
1406
1407 void
1408 arp_ifinit(
1409 struct ifnet *ifp,
1410 struct ifaddr *ifa)
1411 {
1412 ifa->ifa_rtrequest = arp_rtrequest;
1413 ifa->ifa_flags |= RTF_CLONING;
1414 dlil_send_arp(ifp, ARPOP_REQUEST, NULL, ifa->ifa_addr, NULL, ifa->ifa_addr);
1415 }