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