]> git.saurik.com Git - apple/xnu.git/blob - bsd/netinet/in.c
xnu-6153.61.1.tar.gz
[apple/xnu.git] / bsd / netinet / in.c
1 /*
2 * Copyright (c) 2000-2019 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, 1986, 1991, 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 * @(#)in.c 8.4 (Berkeley) 1/9/95
61 */
62
63 #include <sys/param.h>
64 #include <sys/systm.h>
65 #include <sys/sockio.h>
66 #include <sys/socketvar.h>
67 #include <sys/malloc.h>
68 #include <sys/proc.h>
69 #include <sys/socket.h>
70 #include <sys/kernel.h>
71 #include <sys/sysctl.h>
72 #include <sys/kern_event.h>
73 #include <sys/syslog.h>
74 #include <sys/mcache.h>
75 #include <sys/protosw.h>
76 #include <sys/file.h>
77
78 #include <kern/zalloc.h>
79 #include <pexpert/pexpert.h>
80 #include <os/log.h>
81
82 #include <net/if.h>
83 #include <net/if_types.h>
84 #include <net/route.h>
85 #include <net/kpi_protocol.h>
86 #include <net/dlil.h>
87 #include <net/if_llatbl.h>
88 #include <net/if_arp.h>
89 #if PF
90 #include <net/pfvar.h>
91 #endif /* PF */
92
93 #include <netinet/in.h>
94 #include <netinet/in_var.h>
95 #include <netinet/in_pcb.h>
96 #include <netinet/igmp_var.h>
97 #include <netinet/ip_var.h>
98 #include <netinet/tcp.h>
99 #include <netinet/tcp_timer.h>
100 #include <netinet/tcp_var.h>
101 #include <netinet/if_ether.h>
102
103 static int inctl_associd(struct socket *, u_long, caddr_t);
104 static int inctl_connid(struct socket *, u_long, caddr_t);
105 static int inctl_conninfo(struct socket *, u_long, caddr_t);
106 static int inctl_autoaddr(struct ifnet *, struct ifreq *);
107 static int inctl_arpipll(struct ifnet *, struct ifreq *);
108 static int inctl_setrouter(struct ifnet *, struct ifreq *);
109 static int inctl_ifaddr(struct ifnet *, struct in_ifaddr *, u_long,
110 struct ifreq *);
111 static int inctl_ifdstaddr(struct ifnet *, struct in_ifaddr *, u_long,
112 struct ifreq *);
113 static int inctl_ifbrdaddr(struct ifnet *, struct in_ifaddr *, u_long,
114 struct ifreq *);
115 static int inctl_ifnetmask(struct ifnet *, struct in_ifaddr *, u_long,
116 struct ifreq *);
117
118 static void in_socktrim(struct sockaddr_in *);
119 static int in_ifinit(struct ifnet *, struct in_ifaddr *,
120 struct sockaddr_in *, int);
121
122 #define IA_HASH_INIT(ia) { \
123 (ia)->ia_hash.tqe_next = (void *)(uintptr_t)-1; \
124 (ia)->ia_hash.tqe_prev = (void *)(uintptr_t)-1; \
125 }
126
127 #define IA_IS_HASHED(ia) \
128 (!((ia)->ia_hash.tqe_next == (void *)(uintptr_t)-1 || \
129 (ia)->ia_hash.tqe_prev == (void *)(uintptr_t)-1))
130
131 static void in_iahash_remove(struct in_ifaddr *);
132 static void in_iahash_insert(struct in_ifaddr *);
133 static void in_iahash_insert_ptp(struct in_ifaddr *);
134 static struct in_ifaddr *in_ifaddr_alloc(int);
135 static void in_ifaddr_attached(struct ifaddr *);
136 static void in_ifaddr_detached(struct ifaddr *);
137 static void in_ifaddr_free(struct ifaddr *);
138 static void in_ifaddr_trace(struct ifaddr *, int);
139
140 static int in_getassocids(struct socket *, uint32_t *, user_addr_t);
141 static int in_getconnids(struct socket *, sae_associd_t, uint32_t *, user_addr_t);
142
143 /* IPv4 Layer 2 neighbor cache management routines */
144 static void in_lltable_destroy_lle_unlocked(struct llentry *lle);
145 static void in_lltable_destroy_lle(struct llentry *lle);
146 static struct llentry *in_lltable_new(struct in_addr addr4, u_int flags);
147 static int in_lltable_match_prefix(const struct sockaddr *saddr,
148 const struct sockaddr *smask, u_int flags, struct llentry *lle);
149 static void in_lltable_free_entry(struct lltable *llt, struct llentry *lle);
150 static int in_lltable_rtcheck(struct ifnet *ifp, u_int flags, const struct sockaddr *l3addr);
151 static inline uint32_t in_lltable_hash_dst(const struct in_addr dst, uint32_t hsize);
152 static uint32_t in_lltable_hash(const struct llentry *lle, uint32_t hsize);
153 static void in_lltable_fill_sa_entry(const struct llentry *lle, struct sockaddr *sa);
154 static inline struct llentry * in_lltable_find_dst(struct lltable *llt, struct in_addr dst);
155 static void in_lltable_delete_entry(struct lltable *llt, struct llentry *lle);
156 static struct llentry * in_lltable_alloc(struct lltable *llt, u_int flags, const struct sockaddr *l3addr);
157 static struct llentry * in_lltable_lookup(struct lltable *llt, u_int flags, const struct sockaddr *l3addr);
158 static int in_lltable_dump_entry(struct lltable *llt, struct llentry *lle, struct sysctl_req *wr);
159 static struct lltable * in_lltattach(struct ifnet *ifp);
160
161 static int subnetsarelocal = 0;
162 SYSCTL_INT(_net_inet_ip, OID_AUTO, subnets_are_local,
163 CTLFLAG_RW | CTLFLAG_LOCKED, &subnetsarelocal, 0, "");
164
165 /* Track whether or not the SIOCARPIPLL ioctl has been called */
166 u_int32_t ipv4_ll_arp_aware = 0;
167
168 #define INIFA_TRACE_HIST_SIZE 32 /* size of trace history */
169
170 /* For gdb */
171 __private_extern__ unsigned int inifa_trace_hist_size = INIFA_TRACE_HIST_SIZE;
172
173 struct in_ifaddr_dbg {
174 struct in_ifaddr inifa; /* in_ifaddr */
175 struct in_ifaddr inifa_old; /* saved in_ifaddr */
176 u_int16_t inifa_refhold_cnt; /* # of IFA_ADDREF */
177 u_int16_t inifa_refrele_cnt; /* # of IFA_REMREF */
178 /*
179 * Alloc and free callers.
180 */
181 ctrace_t inifa_alloc;
182 ctrace_t inifa_free;
183 /*
184 * Circular lists of IFA_ADDREF and IFA_REMREF callers.
185 */
186 ctrace_t inifa_refhold[INIFA_TRACE_HIST_SIZE];
187 ctrace_t inifa_refrele[INIFA_TRACE_HIST_SIZE];
188 /*
189 * Trash list linkage
190 */
191 TAILQ_ENTRY(in_ifaddr_dbg) inifa_trash_link;
192 };
193
194 /* List of trash in_ifaddr entries protected by inifa_trash_lock */
195 static TAILQ_HEAD(, in_ifaddr_dbg) inifa_trash_head;
196 static decl_lck_mtx_data(, inifa_trash_lock);
197
198 #if DEBUG
199 static unsigned int inifa_debug = 1; /* debugging (enabled) */
200 #else
201 static unsigned int inifa_debug; /* debugging (disabled) */
202 #endif /* !DEBUG */
203 static unsigned int inifa_size; /* size of zone element */
204 static struct zone *inifa_zone; /* zone for in_ifaddr */
205
206 #define INIFA_ZONE_MAX 64 /* maximum elements in zone */
207 #define INIFA_ZONE_NAME "in_ifaddr" /* zone name */
208
209 static const unsigned int in_extra_size = sizeof(struct in_ifextra);
210 static const unsigned int in_extra_bufsize = in_extra_size +
211 sizeof(void *) + sizeof(uint64_t);
212
213 /*
214 * Return 1 if the address is
215 * - loopback
216 * - unicast or multicast link local
217 * - routed via a link level gateway
218 * - belongs to a directly connected (sub)net
219 */
220 int
221 inaddr_local(struct in_addr in)
222 {
223 struct rtentry *rt;
224 struct sockaddr_in sin;
225 int local = 0;
226
227 if (ntohl(in.s_addr) == INADDR_LOOPBACK ||
228 IN_LINKLOCAL(ntohl(in.s_addr))) {
229 local = 1;
230 } else if (ntohl(in.s_addr) >= INADDR_UNSPEC_GROUP &&
231 ntohl(in.s_addr) <= INADDR_MAX_LOCAL_GROUP) {
232 local = 1;
233 } else {
234 sin.sin_family = AF_INET;
235 sin.sin_len = sizeof(sin);
236 sin.sin_addr = in;
237 rt = rtalloc1((struct sockaddr *)&sin, 0, 0);
238
239 if (rt != NULL) {
240 RT_LOCK_SPIN(rt);
241 if (rt->rt_gateway->sa_family == AF_LINK ||
242 (rt->rt_ifp->if_flags & IFF_LOOPBACK)) {
243 local = 1;
244 }
245 RT_UNLOCK(rt);
246 rtfree(rt);
247 } else {
248 local = in_localaddr(in);
249 }
250 }
251 return local;
252 }
253
254 /*
255 * Return 1 if an internet address is for a ``local'' host
256 * (one to which we have a connection). If subnetsarelocal
257 * is true, this includes other subnets of the local net,
258 * otherwise, it includes the directly-connected (sub)nets.
259 * The IPv4 link local prefix 169.254/16 is also included.
260 */
261 int
262 in_localaddr(struct in_addr in)
263 {
264 u_int32_t i = ntohl(in.s_addr);
265 struct in_ifaddr *ia;
266
267 if (IN_LINKLOCAL(i)) {
268 return 1;
269 }
270
271 if (subnetsarelocal) {
272 lck_rw_lock_shared(in_ifaddr_rwlock);
273 for (ia = in_ifaddrhead.tqh_first; ia != NULL;
274 ia = ia->ia_link.tqe_next) {
275 IFA_LOCK(&ia->ia_ifa);
276 if ((i & ia->ia_netmask) == ia->ia_net) {
277 IFA_UNLOCK(&ia->ia_ifa);
278 lck_rw_done(in_ifaddr_rwlock);
279 return 1;
280 }
281 IFA_UNLOCK(&ia->ia_ifa);
282 }
283 lck_rw_done(in_ifaddr_rwlock);
284 } else {
285 lck_rw_lock_shared(in_ifaddr_rwlock);
286 for (ia = in_ifaddrhead.tqh_first; ia != NULL;
287 ia = ia->ia_link.tqe_next) {
288 IFA_LOCK(&ia->ia_ifa);
289 if ((i & ia->ia_subnetmask) == ia->ia_subnet) {
290 IFA_UNLOCK(&ia->ia_ifa);
291 lck_rw_done(in_ifaddr_rwlock);
292 return 1;
293 }
294 IFA_UNLOCK(&ia->ia_ifa);
295 }
296 lck_rw_done(in_ifaddr_rwlock);
297 }
298 return 0;
299 }
300
301 /*
302 * Determine whether an IP address is in a reserved set of addresses
303 * that may not be forwarded, or whether datagrams to that destination
304 * may be forwarded.
305 */
306 boolean_t
307 in_canforward(struct in_addr in)
308 {
309 u_int32_t i = ntohl(in.s_addr);
310 u_int32_t net;
311
312 if (IN_EXPERIMENTAL(i) || IN_MULTICAST(i)) {
313 return FALSE;
314 }
315 if (IN_CLASSA(i)) {
316 net = i & IN_CLASSA_NET;
317 if (net == 0 || net == (IN_LOOPBACKNET << IN_CLASSA_NSHIFT)) {
318 return FALSE;
319 }
320 }
321 return TRUE;
322 }
323
324 /*
325 * Trim a mask in a sockaddr
326 */
327 static void
328 in_socktrim(struct sockaddr_in *ap)
329 {
330 char *cplim = (char *)&ap->sin_addr;
331 char *cp = (char *)(&ap->sin_addr + 1);
332
333 ap->sin_len = 0;
334 while (--cp >= cplim) {
335 if (*cp) {
336 (ap)->sin_len = cp - (char *)(ap) + 1;
337 break;
338 }
339 }
340 }
341
342 static int in_interfaces; /* number of external internet interfaces */
343
344 static int
345 in_domifattach(struct ifnet *ifp)
346 {
347 int error;
348
349 VERIFY(ifp != NULL);
350
351 if ((error = proto_plumb(PF_INET, ifp)) && error != EEXIST) {
352 log(LOG_ERR, "%s: proto_plumb returned %d if=%s\n",
353 __func__, error, if_name(ifp));
354 } else if (error == 0 && ifp->if_inetdata == NULL) {
355 void **pbuf, *base;
356 struct in_ifextra *ext;
357 int errorx;
358
359 if ((ext = (struct in_ifextra *)_MALLOC(in_extra_bufsize,
360 M_IFADDR, M_WAITOK | M_ZERO)) == NULL) {
361 error = ENOMEM;
362 errorx = proto_unplumb(PF_INET, ifp);
363 if (errorx != 0) {
364 log(LOG_ERR,
365 "%s: proto_unplumb returned %d if=%s%d\n",
366 __func__, errorx, ifp->if_name,
367 ifp->if_unit);
368 }
369 goto done;
370 }
371
372 /* Align on 64-bit boundary */
373 base = (void *)P2ROUNDUP((intptr_t)ext + sizeof(uint64_t),
374 sizeof(uint64_t));
375 VERIFY(((intptr_t)base + in_extra_size) <=
376 ((intptr_t)ext + in_extra_bufsize));
377 pbuf = (void **)((intptr_t)base - sizeof(void *));
378 *pbuf = ext;
379 ifp->if_inetdata = base;
380 IN_IFEXTRA(ifp)->ii_llt = in_lltattach(ifp);
381 VERIFY(IS_P2ALIGNED(ifp->if_inetdata, sizeof(uint64_t)));
382 }
383 done:
384 if (error == 0 && ifp->if_inetdata != NULL) {
385 /*
386 * Since the structure is never freed, we need to
387 * zero out its contents to avoid reusing stale data.
388 * A little redundant with allocation above, but it
389 * keeps the code simpler for all cases.
390 */
391 bzero(ifp->if_inetdata, in_extra_size);
392 }
393 return error;
394 }
395
396 static __attribute__((noinline)) int
397 inctl_associd(struct socket *so, u_long cmd, caddr_t data)
398 {
399 int error = 0;
400 union {
401 struct so_aidreq32 a32;
402 struct so_aidreq64 a64;
403 } u;
404
405 VERIFY(so != NULL);
406
407 switch (cmd) {
408 case SIOCGASSOCIDS32: /* struct so_aidreq32 */
409 bcopy(data, &u.a32, sizeof(u.a32));
410 error = in_getassocids(so, &u.a32.sar_cnt, u.a32.sar_aidp);
411 if (error == 0) {
412 bcopy(&u.a32, data, sizeof(u.a32));
413 }
414 break;
415
416 case SIOCGASSOCIDS64: /* struct so_aidreq64 */
417 bcopy(data, &u.a64, sizeof(u.a64));
418 error = in_getassocids(so, &u.a64.sar_cnt, u.a64.sar_aidp);
419 if (error == 0) {
420 bcopy(&u.a64, data, sizeof(u.a64));
421 }
422 break;
423
424 default:
425 VERIFY(0);
426 /* NOTREACHED */
427 }
428
429 return error;
430 }
431
432 static __attribute__((noinline)) int
433 inctl_connid(struct socket *so, u_long cmd, caddr_t data)
434 {
435 int error = 0;
436 union {
437 struct so_cidreq32 c32;
438 struct so_cidreq64 c64;
439 } u;
440
441 VERIFY(so != NULL);
442
443 switch (cmd) {
444 case SIOCGCONNIDS32: /* struct so_cidreq32 */
445 bcopy(data, &u.c32, sizeof(u.c32));
446 error = in_getconnids(so, u.c32.scr_aid, &u.c32.scr_cnt,
447 u.c32.scr_cidp);
448 if (error == 0) {
449 bcopy(&u.c32, data, sizeof(u.c32));
450 }
451 break;
452
453 case SIOCGCONNIDS64: /* struct so_cidreq64 */
454 bcopy(data, &u.c64, sizeof(u.c64));
455 error = in_getconnids(so, u.c64.scr_aid, &u.c64.scr_cnt,
456 u.c64.scr_cidp);
457 if (error == 0) {
458 bcopy(&u.c64, data, sizeof(u.c64));
459 }
460 break;
461
462 default:
463 VERIFY(0);
464 /* NOTREACHED */
465 }
466
467 return error;
468 }
469
470 static __attribute__((noinline)) int
471 inctl_conninfo(struct socket *so, u_long cmd, caddr_t data)
472 {
473 int error = 0;
474 union {
475 struct so_cinforeq32 ci32;
476 struct so_cinforeq64 ci64;
477 } u;
478
479 VERIFY(so != NULL);
480
481 switch (cmd) {
482 case SIOCGCONNINFO32: /* struct so_cinforeq32 */
483 bcopy(data, &u.ci32, sizeof(u.ci32));
484 error = in_getconninfo(so, u.ci32.scir_cid, &u.ci32.scir_flags,
485 &u.ci32.scir_ifindex, &u.ci32.scir_error, u.ci32.scir_src,
486 &u.ci32.scir_src_len, u.ci32.scir_dst, &u.ci32.scir_dst_len,
487 &u.ci32.scir_aux_type, u.ci32.scir_aux_data,
488 &u.ci32.scir_aux_len);
489 if (error == 0) {
490 bcopy(&u.ci32, data, sizeof(u.ci32));
491 }
492 break;
493
494 case SIOCGCONNINFO64: /* struct so_cinforeq64 */
495 bcopy(data, &u.ci64, sizeof(u.ci64));
496 error = in_getconninfo(so, u.ci64.scir_cid, &u.ci64.scir_flags,
497 &u.ci64.scir_ifindex, &u.ci64.scir_error, u.ci64.scir_src,
498 &u.ci64.scir_src_len, u.ci64.scir_dst, &u.ci64.scir_dst_len,
499 &u.ci64.scir_aux_type, u.ci64.scir_aux_data,
500 &u.ci64.scir_aux_len);
501 if (error == 0) {
502 bcopy(&u.ci64, data, sizeof(u.ci64));
503 }
504 break;
505
506 default:
507 VERIFY(0);
508 /* NOTREACHED */
509 }
510
511 return error;
512 }
513
514 /*
515 * Caller passes in the ioctl data pointer directly via "ifr", with the
516 * expectation that this routine always uses bcopy() or other byte-aligned
517 * memory accesses.
518 */
519 static __attribute__((noinline)) int
520 inctl_autoaddr(struct ifnet *ifp, struct ifreq *ifr)
521 {
522 int error = 0, intval;
523
524 VERIFY(ifp != NULL);
525
526 bcopy(&ifr->ifr_intval, &intval, sizeof(intval));
527
528 ifnet_lock_exclusive(ifp);
529 if (intval) {
530 /*
531 * An interface in IPv4 router mode implies that it
532 * is configured with a static IP address and should
533 * not act as a DHCP client; prevent SIOCAUTOADDR from
534 * being set in that mode.
535 */
536 if (ifp->if_eflags & IFEF_IPV4_ROUTER) {
537 intval = 0; /* be safe; clear flag if set */
538 error = EBUSY;
539 } else {
540 ifp->if_eflags |= IFEF_AUTOCONFIGURING;
541 }
542 }
543 if (!intval) {
544 ifp->if_eflags &= ~IFEF_AUTOCONFIGURING;
545 }
546 ifnet_lock_done(ifp);
547
548 return error;
549 }
550
551 /*
552 * Caller passes in the ioctl data pointer directly via "ifr", with the
553 * expectation that this routine always uses bcopy() or other byte-aligned
554 * memory accesses.
555 */
556 static __attribute__((noinline)) int
557 inctl_arpipll(struct ifnet *ifp, struct ifreq *ifr)
558 {
559 int error = 0, intval;
560
561 VERIFY(ifp != NULL);
562
563 bcopy(&ifr->ifr_intval, &intval, sizeof(intval));
564 ipv4_ll_arp_aware = 1;
565
566 ifnet_lock_exclusive(ifp);
567 if (intval) {
568 /*
569 * An interface in IPv4 router mode implies that it
570 * is configured with a static IP address and should
571 * not have to deal with IPv4 Link-Local Address;
572 * prevent SIOCARPIPLL from being set in that mode.
573 */
574 if (ifp->if_eflags & IFEF_IPV4_ROUTER) {
575 intval = 0; /* be safe; clear flag if set */
576 error = EBUSY;
577 } else {
578 ifp->if_eflags |= IFEF_ARPLL;
579 }
580 }
581 if (!intval) {
582 ifp->if_eflags &= ~IFEF_ARPLL;
583 }
584 ifnet_lock_done(ifp);
585
586 return error;
587 }
588
589 /*
590 * Handle SIOCSETROUTERMODE to set or clear the IPv4 router mode flag on
591 * the interface. When in this mode, IPv4 Link-Local Address support is
592 * disabled in ARP, and DHCP client support is disabled in IP input; turning
593 * any of them on would cause an error to be returned. Entering or exiting
594 * this mode will result in the removal of IPv4 addresses currently configured
595 * on the interface.
596 *
597 * Caller passes in the ioctl data pointer directly via "ifr", with the
598 * expectation that this routine always uses bcopy() or other byte-aligned
599 * memory accesses.
600 */
601 static __attribute__((noinline)) int
602 inctl_setrouter(struct ifnet *ifp, struct ifreq *ifr)
603 {
604 int error = 0, intval;
605
606 VERIFY(ifp != NULL);
607
608 /* Router mode isn't valid for loopback */
609 if (ifp->if_flags & IFF_LOOPBACK) {
610 return ENODEV;
611 }
612
613 bcopy(&ifr->ifr_intval, &intval, sizeof(intval));
614
615 ifnet_lock_exclusive(ifp);
616 if (intval) {
617 ifp->if_eflags |= IFEF_IPV4_ROUTER;
618 ifp->if_eflags &= ~(IFEF_ARPLL | IFEF_AUTOCONFIGURING);
619 } else {
620 ifp->if_eflags &= ~IFEF_IPV4_ROUTER;
621 }
622 ifnet_lock_done(ifp);
623
624 /* purge all IPv4 addresses configured on this interface */
625 in_purgeaddrs(ifp);
626
627 return error;
628 }
629
630 /*
631 * Caller passes in the ioctl data pointer directly via "ifr", with the
632 * expectation that this routine always uses bcopy() or other byte-aligned
633 * memory accesses.
634 */
635 static __attribute__((noinline)) int
636 inctl_ifaddr(struct ifnet *ifp, struct in_ifaddr *ia, u_long cmd,
637 struct ifreq *ifr)
638 {
639 struct kev_in_data in_event_data;
640 struct kev_msg ev_msg;
641 struct sockaddr_in addr;
642 struct ifaddr *ifa;
643 int error = 0;
644
645 VERIFY(ifp != NULL);
646
647 bzero(&in_event_data, sizeof(struct kev_in_data));
648 bzero(&ev_msg, sizeof(struct kev_msg));
649
650 switch (cmd) {
651 case SIOCGIFADDR: /* struct ifreq */
652 if (ia == NULL) {
653 error = EADDRNOTAVAIL;
654 break;
655 }
656 IFA_LOCK(&ia->ia_ifa);
657 bcopy(&ia->ia_addr, &ifr->ifr_addr, sizeof(addr));
658 IFA_UNLOCK(&ia->ia_ifa);
659 break;
660
661 case SIOCSIFADDR: /* struct ifreq */
662 VERIFY(ia != NULL);
663 bcopy(&ifr->ifr_addr, &addr, sizeof(addr));
664 /*
665 * If this is a new address, the reference count for the
666 * hash table has been taken at creation time above.
667 */
668 error = in_ifinit(ifp, ia, &addr, 1);
669 if (error == 0) {
670 (void) ifnet_notify_address(ifp, AF_INET);
671 }
672 break;
673
674 case SIOCAIFADDR: { /* struct {if,in_}aliasreq */
675 struct in_aliasreq *ifra = (struct in_aliasreq *)ifr;
676 struct sockaddr_in broadaddr, mask;
677 int hostIsNew, maskIsNew;
678
679 VERIFY(ia != NULL);
680 bcopy(&ifra->ifra_addr, &addr, sizeof(addr));
681 bcopy(&ifra->ifra_broadaddr, &broadaddr, sizeof(broadaddr));
682 bcopy(&ifra->ifra_mask, &mask, sizeof(mask));
683
684 maskIsNew = 0;
685 hostIsNew = 1;
686 error = 0;
687
688 IFA_LOCK(&ia->ia_ifa);
689 if (ia->ia_addr.sin_family == AF_INET) {
690 if (addr.sin_len == 0) {
691 addr = ia->ia_addr;
692 hostIsNew = 0;
693 } else if (addr.sin_addr.s_addr ==
694 ia->ia_addr.sin_addr.s_addr) {
695 hostIsNew = 0;
696 }
697 }
698 if (mask.sin_len) {
699 IFA_UNLOCK(&ia->ia_ifa);
700 in_ifscrub(ifp, ia, 0);
701 IFA_LOCK(&ia->ia_ifa);
702 ia->ia_sockmask = mask;
703 ia->ia_subnetmask =
704 ntohl(ia->ia_sockmask.sin_addr.s_addr);
705 maskIsNew = 1;
706 }
707 if ((ifp->if_flags & IFF_POINTOPOINT) &&
708 (broadaddr.sin_family == AF_INET)) {
709 IFA_UNLOCK(&ia->ia_ifa);
710 in_ifscrub(ifp, ia, 0);
711 IFA_LOCK(&ia->ia_ifa);
712 ia->ia_dstaddr = broadaddr;
713 ia->ia_dstaddr.sin_len = sizeof(struct sockaddr_in);
714 maskIsNew = 1; /* We lie; but the effect's the same */
715 }
716 if (addr.sin_family == AF_INET && (hostIsNew || maskIsNew)) {
717 IFA_UNLOCK(&ia->ia_ifa);
718 error = in_ifinit(ifp, ia, &addr, 0);
719 } else {
720 IFA_UNLOCK(&ia->ia_ifa);
721 }
722 if (error == 0) {
723 (void) ifnet_notify_address(ifp, AF_INET);
724 }
725 IFA_LOCK(&ia->ia_ifa);
726 if ((ifp->if_flags & IFF_BROADCAST) &&
727 (broadaddr.sin_family == AF_INET)) {
728 ia->ia_broadaddr = broadaddr;
729 }
730
731 /*
732 * Report event.
733 */
734 if ((error == 0) || (error == EEXIST)) {
735 ev_msg.vendor_code = KEV_VENDOR_APPLE;
736 ev_msg.kev_class = KEV_NETWORK_CLASS;
737 ev_msg.kev_subclass = KEV_INET_SUBCLASS;
738
739 if (hostIsNew) {
740 ev_msg.event_code = KEV_INET_NEW_ADDR;
741 } else {
742 ev_msg.event_code = KEV_INET_CHANGED_ADDR;
743 }
744
745 if (ia->ia_ifa.ifa_dstaddr) {
746 in_event_data.ia_dstaddr =
747 ((struct sockaddr_in *)(void *)ia->
748 ia_ifa.ifa_dstaddr)->sin_addr;
749 } else {
750 in_event_data.ia_dstaddr.s_addr = INADDR_ANY;
751 }
752 in_event_data.ia_addr = ia->ia_addr.sin_addr;
753 in_event_data.ia_net = ia->ia_net;
754 in_event_data.ia_netmask = ia->ia_netmask;
755 in_event_data.ia_subnet = ia->ia_subnet;
756 in_event_data.ia_subnetmask = ia->ia_subnetmask;
757 in_event_data.ia_netbroadcast = ia->ia_netbroadcast;
758 IFA_UNLOCK(&ia->ia_ifa);
759 (void) strlcpy(&in_event_data.link_data.if_name[0],
760 ifp->if_name, IFNAMSIZ);
761 in_event_data.link_data.if_family = ifp->if_family;
762 in_event_data.link_data.if_unit = ifp->if_unit;
763
764 ev_msg.dv[0].data_ptr = &in_event_data;
765 ev_msg.dv[0].data_length = sizeof(struct kev_in_data);
766 ev_msg.dv[1].data_length = 0;
767
768 dlil_post_complete_msg(ifp, &ev_msg);
769 } else {
770 IFA_UNLOCK(&ia->ia_ifa);
771 }
772 break;
773 }
774
775 case SIOCDIFADDR: /* struct ifreq */
776 VERIFY(ia != NULL);
777 error = ifnet_ioctl(ifp, PF_INET, SIOCDIFADDR, ia);
778 if (error == EOPNOTSUPP) {
779 error = 0;
780 }
781 if (error != 0) {
782 break;
783 }
784
785 /* Fill out the kernel event information */
786 ev_msg.vendor_code = KEV_VENDOR_APPLE;
787 ev_msg.kev_class = KEV_NETWORK_CLASS;
788 ev_msg.kev_subclass = KEV_INET_SUBCLASS;
789
790 ev_msg.event_code = KEV_INET_ADDR_DELETED;
791
792 IFA_LOCK(&ia->ia_ifa);
793 if (ia->ia_ifa.ifa_dstaddr) {
794 in_event_data.ia_dstaddr = ((struct sockaddr_in *)
795 (void *)ia->ia_ifa.ifa_dstaddr)->sin_addr;
796 } else {
797 in_event_data.ia_dstaddr.s_addr = INADDR_ANY;
798 }
799 in_event_data.ia_addr = ia->ia_addr.sin_addr;
800 in_event_data.ia_net = ia->ia_net;
801 in_event_data.ia_netmask = ia->ia_netmask;
802 in_event_data.ia_subnet = ia->ia_subnet;
803 in_event_data.ia_subnetmask = ia->ia_subnetmask;
804 in_event_data.ia_netbroadcast = ia->ia_netbroadcast;
805 IFA_UNLOCK(&ia->ia_ifa);
806 (void) strlcpy(&in_event_data.link_data.if_name[0],
807 ifp->if_name, IFNAMSIZ);
808 in_event_data.link_data.if_family = ifp->if_family;
809 in_event_data.link_data.if_unit = (u_int32_t)ifp->if_unit;
810
811 ev_msg.dv[0].data_ptr = &in_event_data;
812 ev_msg.dv[0].data_length = sizeof(struct kev_in_data);
813 ev_msg.dv[1].data_length = 0;
814
815 ifa = &ia->ia_ifa;
816 lck_rw_lock_exclusive(in_ifaddr_rwlock);
817 /* Release ia_link reference */
818 IFA_REMREF(ifa);
819 TAILQ_REMOVE(&in_ifaddrhead, ia, ia_link);
820 IFA_LOCK(ifa);
821 if (IA_IS_HASHED(ia)) {
822 in_iahash_remove(ia);
823 }
824 IFA_UNLOCK(ifa);
825 lck_rw_done(in_ifaddr_rwlock);
826
827 /*
828 * in_ifscrub kills the interface route.
829 */
830 in_ifscrub(ifp, ia, 0);
831 ifnet_lock_exclusive(ifp);
832 IFA_LOCK(ifa);
833 /* if_detach_ifa() releases ifa_link reference */
834 if_detach_ifa(ifp, ifa);
835 /* Our reference to this address is dropped at the bottom */
836 IFA_UNLOCK(ifa);
837
838 /* invalidate route caches */
839 routegenid_inet_update();
840
841 /*
842 * If the interface supports multicast, and no address is left,
843 * remove the "all hosts" multicast group from that interface.
844 */
845 if ((ifp->if_flags & IFF_MULTICAST) ||
846 ifp->if_allhostsinm != NULL) {
847 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
848 IFA_LOCK(ifa);
849 if (ifa->ifa_addr->sa_family == AF_INET) {
850 IFA_UNLOCK(ifa);
851 break;
852 }
853 IFA_UNLOCK(ifa);
854 }
855 ifnet_lock_done(ifp);
856
857 lck_mtx_lock(&ifp->if_addrconfig_lock);
858 if (ifa == NULL && ifp->if_allhostsinm != NULL) {
859 struct in_multi *inm = ifp->if_allhostsinm;
860 ifp->if_allhostsinm = NULL;
861
862 in_delmulti(inm);
863 /* release the reference for allhostsinm */
864 INM_REMREF(inm);
865 }
866 lck_mtx_unlock(&ifp->if_addrconfig_lock);
867 } else {
868 ifnet_lock_done(ifp);
869 }
870
871 /* Post the kernel event */
872 dlil_post_complete_msg(ifp, &ev_msg);
873
874 /*
875 * See if there is any IPV4 address left and if so,
876 * reconfigure KDP to use current primary address.
877 */
878 ifa = ifa_ifpgetprimary(ifp, AF_INET);
879 if (ifa != NULL) {
880 /*
881 * NOTE: SIOCSIFADDR is defined with struct ifreq
882 * as parameter, but here we are sending it down
883 * to the interface with a pointer to struct ifaddr,
884 * for legacy reasons.
885 */
886 error = ifnet_ioctl(ifp, PF_INET, SIOCSIFADDR, ifa);
887 if (error == EOPNOTSUPP) {
888 error = 0;
889 }
890
891 /* Release reference from ifa_ifpgetprimary() */
892 IFA_REMREF(ifa);
893 }
894 (void) ifnet_notify_address(ifp, AF_INET);
895 break;
896
897 default:
898 VERIFY(0);
899 /* NOTREACHED */
900 }
901
902 return error;
903 }
904
905 /*
906 * Caller passes in the ioctl data pointer directly via "ifr", with the
907 * expectation that this routine always uses bcopy() or other byte-aligned
908 * memory accesses.
909 */
910 static __attribute__((noinline)) int
911 inctl_ifdstaddr(struct ifnet *ifp, struct in_ifaddr *ia, u_long cmd,
912 struct ifreq *ifr)
913 {
914 struct kev_in_data in_event_data;
915 struct kev_msg ev_msg;
916 struct sockaddr_in dstaddr;
917 int error = 0;
918
919 VERIFY(ifp != NULL);
920
921 if (!(ifp->if_flags & IFF_POINTOPOINT)) {
922 return EINVAL;
923 }
924
925 bzero(&in_event_data, sizeof(struct kev_in_data));
926 bzero(&ev_msg, sizeof(struct kev_msg));
927
928 switch (cmd) {
929 case SIOCGIFDSTADDR: /* struct ifreq */
930 if (ia == NULL) {
931 error = EADDRNOTAVAIL;
932 break;
933 }
934 IFA_LOCK(&ia->ia_ifa);
935 bcopy(&ia->ia_dstaddr, &ifr->ifr_dstaddr, sizeof(dstaddr));
936 IFA_UNLOCK(&ia->ia_ifa);
937 break;
938
939 case SIOCSIFDSTADDR: /* struct ifreq */
940 VERIFY(ia != NULL);
941 IFA_LOCK(&ia->ia_ifa);
942 dstaddr = ia->ia_dstaddr;
943 bcopy(&ifr->ifr_dstaddr, &ia->ia_dstaddr, sizeof(dstaddr));
944 if (ia->ia_dstaddr.sin_family == AF_INET) {
945 ia->ia_dstaddr.sin_len = sizeof(struct sockaddr_in);
946 }
947 IFA_UNLOCK(&ia->ia_ifa);
948 /*
949 * NOTE: SIOCSIFDSTADDR is defined with struct ifreq
950 * as parameter, but here we are sending it down
951 * to the interface with a pointer to struct ifaddr,
952 * for legacy reasons.
953 */
954 error = ifnet_ioctl(ifp, PF_INET, SIOCSIFDSTADDR, ia);
955 IFA_LOCK(&ia->ia_ifa);
956 if (error == EOPNOTSUPP) {
957 error = 0;
958 }
959 if (error != 0) {
960 ia->ia_dstaddr = dstaddr;
961 IFA_UNLOCK(&ia->ia_ifa);
962 break;
963 }
964 IFA_LOCK_ASSERT_HELD(&ia->ia_ifa);
965
966 ev_msg.vendor_code = KEV_VENDOR_APPLE;
967 ev_msg.kev_class = KEV_NETWORK_CLASS;
968 ev_msg.kev_subclass = KEV_INET_SUBCLASS;
969
970 ev_msg.event_code = KEV_INET_SIFDSTADDR;
971
972 if (ia->ia_ifa.ifa_dstaddr) {
973 in_event_data.ia_dstaddr = ((struct sockaddr_in *)
974 (void *)ia->ia_ifa.ifa_dstaddr)->sin_addr;
975 } else {
976 in_event_data.ia_dstaddr.s_addr = INADDR_ANY;
977 }
978
979 in_event_data.ia_addr = ia->ia_addr.sin_addr;
980 in_event_data.ia_net = ia->ia_net;
981 in_event_data.ia_netmask = ia->ia_netmask;
982 in_event_data.ia_subnet = ia->ia_subnet;
983 in_event_data.ia_subnetmask = ia->ia_subnetmask;
984 in_event_data.ia_netbroadcast = ia->ia_netbroadcast;
985 IFA_UNLOCK(&ia->ia_ifa);
986 (void) strlcpy(&in_event_data.link_data.if_name[0],
987 ifp->if_name, IFNAMSIZ);
988 in_event_data.link_data.if_family = ifp->if_family;
989 in_event_data.link_data.if_unit = (u_int32_t)ifp->if_unit;
990
991 ev_msg.dv[0].data_ptr = &in_event_data;
992 ev_msg.dv[0].data_length = sizeof(struct kev_in_data);
993 ev_msg.dv[1].data_length = 0;
994
995 dlil_post_complete_msg(ifp, &ev_msg);
996
997 lck_mtx_lock(rnh_lock);
998 IFA_LOCK(&ia->ia_ifa);
999 if (ia->ia_flags & IFA_ROUTE) {
1000 ia->ia_ifa.ifa_dstaddr = (struct sockaddr *)&dstaddr;
1001 IFA_UNLOCK(&ia->ia_ifa);
1002 rtinit_locked(&(ia->ia_ifa), (int)RTM_DELETE, RTF_HOST);
1003 IFA_LOCK(&ia->ia_ifa);
1004 ia->ia_ifa.ifa_dstaddr =
1005 (struct sockaddr *)&ia->ia_dstaddr;
1006 IFA_UNLOCK(&ia->ia_ifa);
1007 rtinit_locked(&(ia->ia_ifa), (int)RTM_ADD,
1008 RTF_HOST | RTF_UP);
1009 } else {
1010 IFA_UNLOCK(&ia->ia_ifa);
1011 }
1012 lck_mtx_unlock(rnh_lock);
1013 break;
1014
1015
1016
1017 default:
1018 VERIFY(0);
1019 /* NOTREACHED */
1020 }
1021
1022 return error;
1023 }
1024
1025 /*
1026 * Caller passes in the ioctl data pointer directly via "ifr", with the
1027 * expectation that this routine always uses bcopy() or other byte-aligned
1028 * memory accesses.
1029 */
1030 static __attribute__((noinline)) int
1031 inctl_ifbrdaddr(struct ifnet *ifp, struct in_ifaddr *ia, u_long cmd,
1032 struct ifreq *ifr)
1033 {
1034 struct kev_in_data in_event_data;
1035 struct kev_msg ev_msg;
1036 int error = 0;
1037
1038 VERIFY(ifp != NULL);
1039
1040 if (ia == NULL) {
1041 return EADDRNOTAVAIL;
1042 }
1043
1044 if (!(ifp->if_flags & IFF_BROADCAST)) {
1045 return EINVAL;
1046 }
1047
1048 bzero(&in_event_data, sizeof(struct kev_in_data));
1049 bzero(&ev_msg, sizeof(struct kev_msg));
1050
1051 switch (cmd) {
1052 case SIOCGIFBRDADDR: /* struct ifreq */
1053 IFA_LOCK(&ia->ia_ifa);
1054 bcopy(&ia->ia_broadaddr, &ifr->ifr_broadaddr,
1055 sizeof(struct sockaddr_in));
1056 IFA_UNLOCK(&ia->ia_ifa);
1057 break;
1058
1059 case SIOCSIFBRDADDR: /* struct ifreq */
1060 IFA_LOCK(&ia->ia_ifa);
1061 bcopy(&ifr->ifr_broadaddr, &ia->ia_broadaddr,
1062 sizeof(struct sockaddr_in));
1063
1064 ev_msg.vendor_code = KEV_VENDOR_APPLE;
1065 ev_msg.kev_class = KEV_NETWORK_CLASS;
1066 ev_msg.kev_subclass = KEV_INET_SUBCLASS;
1067
1068 ev_msg.event_code = KEV_INET_SIFBRDADDR;
1069
1070 if (ia->ia_ifa.ifa_dstaddr) {
1071 in_event_data.ia_dstaddr = ((struct sockaddr_in *)
1072 (void *)ia->ia_ifa.ifa_dstaddr)->sin_addr;
1073 } else {
1074 in_event_data.ia_dstaddr.s_addr = INADDR_ANY;
1075 }
1076 in_event_data.ia_addr = ia->ia_addr.sin_addr;
1077 in_event_data.ia_net = ia->ia_net;
1078 in_event_data.ia_netmask = ia->ia_netmask;
1079 in_event_data.ia_subnet = ia->ia_subnet;
1080 in_event_data.ia_subnetmask = ia->ia_subnetmask;
1081 in_event_data.ia_netbroadcast = ia->ia_netbroadcast;
1082 IFA_UNLOCK(&ia->ia_ifa);
1083 (void) strlcpy(&in_event_data.link_data.if_name[0],
1084 ifp->if_name, IFNAMSIZ);
1085 in_event_data.link_data.if_family = ifp->if_family;
1086 in_event_data.link_data.if_unit = (u_int32_t)ifp->if_unit;
1087
1088 ev_msg.dv[0].data_ptr = &in_event_data;
1089 ev_msg.dv[0].data_length = sizeof(struct kev_in_data);
1090 ev_msg.dv[1].data_length = 0;
1091
1092 dlil_post_complete_msg(ifp, &ev_msg);
1093 break;
1094
1095 default:
1096 VERIFY(0);
1097 /* NOTREACHED */
1098 }
1099
1100 return error;
1101 }
1102
1103 /*
1104 * Caller passes in the ioctl data pointer directly via "ifr", with the
1105 * expectation that this routine always uses bcopy() or other byte-aligned
1106 * memory accesses.
1107 */
1108 static __attribute__((noinline)) int
1109 inctl_ifnetmask(struct ifnet *ifp, struct in_ifaddr *ia, u_long cmd,
1110 struct ifreq *ifr)
1111 {
1112 struct kev_in_data in_event_data;
1113 struct kev_msg ev_msg;
1114 struct sockaddr_in mask;
1115 int error = 0;
1116
1117 VERIFY(ifp != NULL);
1118
1119 bzero(&in_event_data, sizeof(struct kev_in_data));
1120 bzero(&ev_msg, sizeof(struct kev_msg));
1121
1122 switch (cmd) {
1123 case SIOCGIFNETMASK: /* struct ifreq */
1124 if (ia == NULL) {
1125 error = EADDRNOTAVAIL;
1126 break;
1127 }
1128 IFA_LOCK(&ia->ia_ifa);
1129 bcopy(&ia->ia_sockmask, &ifr->ifr_addr, sizeof(mask));
1130 IFA_UNLOCK(&ia->ia_ifa);
1131 break;
1132
1133 case SIOCSIFNETMASK: { /* struct ifreq */
1134 in_addr_t i;
1135
1136 bcopy(&ifr->ifr_addr, &mask, sizeof(mask));
1137 i = mask.sin_addr.s_addr;
1138
1139 VERIFY(ia != NULL);
1140 IFA_LOCK(&ia->ia_ifa);
1141 ia->ia_subnetmask = ntohl(ia->ia_sockmask.sin_addr.s_addr = i);
1142 ev_msg.vendor_code = KEV_VENDOR_APPLE;
1143 ev_msg.kev_class = KEV_NETWORK_CLASS;
1144 ev_msg.kev_subclass = KEV_INET_SUBCLASS;
1145
1146 ev_msg.event_code = KEV_INET_SIFNETMASK;
1147
1148 if (ia->ia_ifa.ifa_dstaddr) {
1149 in_event_data.ia_dstaddr = ((struct sockaddr_in *)
1150 (void *)ia->ia_ifa.ifa_dstaddr)->sin_addr;
1151 } else {
1152 in_event_data.ia_dstaddr.s_addr = INADDR_ANY;
1153 }
1154 in_event_data.ia_addr = ia->ia_addr.sin_addr;
1155 in_event_data.ia_net = ia->ia_net;
1156 in_event_data.ia_netmask = ia->ia_netmask;
1157 in_event_data.ia_subnet = ia->ia_subnet;
1158 in_event_data.ia_subnetmask = ia->ia_subnetmask;
1159 in_event_data.ia_netbroadcast = ia->ia_netbroadcast;
1160 IFA_UNLOCK(&ia->ia_ifa);
1161 (void) strlcpy(&in_event_data.link_data.if_name[0],
1162 ifp->if_name, IFNAMSIZ);
1163 in_event_data.link_data.if_family = ifp->if_family;
1164 in_event_data.link_data.if_unit = (u_int32_t)ifp->if_unit;
1165
1166 ev_msg.dv[0].data_ptr = &in_event_data;
1167 ev_msg.dv[0].data_length = sizeof(struct kev_in_data);
1168 ev_msg.dv[1].data_length = 0;
1169
1170 dlil_post_complete_msg(ifp, &ev_msg);
1171 break;
1172 }
1173
1174 default:
1175 VERIFY(0);
1176 /* NOTREACHED */
1177 }
1178
1179 return error;
1180 }
1181
1182 /*
1183 * Generic INET control operations (ioctl's).
1184 *
1185 * ifp is NULL if not an interface-specific ioctl.
1186 *
1187 * Most of the routines called to handle the ioctls would end up being
1188 * tail-call optimized, which unfortunately causes this routine to
1189 * consume too much stack space; this is the reason for the "noinline"
1190 * attribute used on those routines.
1191 *
1192 * If called directly from within the networking stack (as opposed to via
1193 * pru_control), the socket parameter may be NULL.
1194 */
1195 int
1196 in_control(struct socket *so, u_long cmd, caddr_t data, struct ifnet *ifp,
1197 struct proc *p)
1198 {
1199 struct ifreq *ifr = (struct ifreq *)(void *)data;
1200 struct sockaddr_in addr, dstaddr;
1201 struct sockaddr_in sin, *sa = NULL;
1202 boolean_t privileged = (proc_suser(p) == 0);
1203 boolean_t so_unlocked = FALSE;
1204 struct in_ifaddr *ia = NULL;
1205 struct ifaddr *ifa;
1206 int error = 0;
1207
1208 /* In case it's NULL, make sure it came from the kernel */
1209 VERIFY(so != NULL || p == kernproc);
1210
1211 /*
1212 * ioctls which don't require ifp, but require socket.
1213 */
1214 switch (cmd) {
1215 case SIOCGASSOCIDS32: /* struct so_aidreq32 */
1216 case SIOCGASSOCIDS64: /* struct so_aidreq64 */
1217 return inctl_associd(so, cmd, data);
1218 /* NOTREACHED */
1219
1220 case SIOCGCONNIDS32: /* struct so_cidreq32 */
1221 case SIOCGCONNIDS64: /* struct so_cidreq64 */
1222 return inctl_connid(so, cmd, data);
1223 /* NOTREACHED */
1224
1225 case SIOCGCONNINFO32: /* struct so_cinforeq32 */
1226 case SIOCGCONNINFO64: /* struct so_cinforeq64 */
1227 return inctl_conninfo(so, cmd, data);
1228 /* NOTREACHED */
1229 }
1230
1231 /*
1232 * The rest of ioctls require ifp; reject if we don't have one;
1233 * return ENXIO to be consistent with ifioctl().
1234 */
1235 if (ifp == NULL) {
1236 return ENXIO;
1237 }
1238
1239 /*
1240 * ioctls which require ifp but not interface address.
1241 */
1242 switch (cmd) {
1243 case SIOCAUTOADDR: /* struct ifreq */
1244 if (!privileged) {
1245 return EPERM;
1246 }
1247 return inctl_autoaddr(ifp, ifr);
1248 /* NOTREACHED */
1249
1250 case SIOCARPIPLL: /* struct ifreq */
1251 if (!privileged) {
1252 return EPERM;
1253 }
1254 return inctl_arpipll(ifp, ifr);
1255 /* NOTREACHED */
1256
1257 case SIOCSETROUTERMODE: /* struct ifreq */
1258 if (!privileged) {
1259 return EPERM;
1260 }
1261 return inctl_setrouter(ifp, ifr);
1262 /* NOTREACHED */
1263
1264 case SIOCPROTOATTACH: /* struct ifreq */
1265 if (!privileged) {
1266 return EPERM;
1267 }
1268 return in_domifattach(ifp);
1269 /* NOTREACHED */
1270
1271 case SIOCPROTODETACH: /* struct ifreq */
1272 if (!privileged) {
1273 return EPERM;
1274 }
1275
1276 /*
1277 * If an IPv4 address is still present, refuse to detach.
1278 */
1279 ifnet_lock_shared(ifp);
1280 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
1281 IFA_LOCK(ifa);
1282 if (ifa->ifa_addr->sa_family == AF_INET) {
1283 IFA_UNLOCK(ifa);
1284 break;
1285 }
1286 IFA_UNLOCK(ifa);
1287 }
1288 ifnet_lock_done(ifp);
1289 return (ifa == NULL) ? proto_unplumb(PF_INET, ifp) : EBUSY;
1290 /* NOTREACHED */
1291 }
1292
1293 /*
1294 * ioctls which require interface address; obtain sockaddr_in.
1295 */
1296 switch (cmd) {
1297 case SIOCAIFADDR: /* struct {if,in_}aliasreq */
1298 if (!privileged) {
1299 return EPERM;
1300 }
1301 bcopy(&((struct in_aliasreq *)(void *)data)->ifra_addr,
1302 &sin, sizeof(sin));
1303 sa = &sin;
1304 break;
1305
1306 case SIOCDIFADDR: /* struct ifreq */
1307 case SIOCSIFADDR: /* struct ifreq */
1308 case SIOCSIFDSTADDR: /* struct ifreq */
1309 case SIOCSIFNETMASK: /* struct ifreq */
1310 case SIOCSIFBRDADDR: /* struct ifreq */
1311 if (!privileged) {
1312 return EPERM;
1313 }
1314 /* FALLTHRU */
1315 case SIOCGIFADDR: /* struct ifreq */
1316 case SIOCGIFDSTADDR: /* struct ifreq */
1317 case SIOCGIFNETMASK: /* struct ifreq */
1318 case SIOCGIFBRDADDR: /* struct ifreq */
1319 bcopy(&ifr->ifr_addr, &sin, sizeof(sin));
1320 sa = &sin;
1321 break;
1322 }
1323
1324 /*
1325 * Find address for this interface, if it exists.
1326 *
1327 * If an alias address was specified, find that one instead of
1328 * the first one on the interface, if possible.
1329 */
1330 VERIFY(ia == NULL);
1331 if (sa != NULL) {
1332 struct in_ifaddr *iap;
1333
1334 /*
1335 * Any failures from this point on must take into account
1336 * a non-NULL "ia" with an outstanding reference count, and
1337 * therefore requires IFA_REMREF. Jump to "done" label
1338 * instead of calling return if "ia" is valid.
1339 */
1340 lck_rw_lock_shared(in_ifaddr_rwlock);
1341 TAILQ_FOREACH(iap, INADDR_HASH(sa->sin_addr.s_addr), ia_hash) {
1342 IFA_LOCK(&iap->ia_ifa);
1343 if (iap->ia_ifp == ifp &&
1344 iap->ia_addr.sin_addr.s_addr ==
1345 sa->sin_addr.s_addr) {
1346 ia = iap;
1347 IFA_ADDREF_LOCKED(&iap->ia_ifa);
1348 IFA_UNLOCK(&iap->ia_ifa);
1349 break;
1350 }
1351 IFA_UNLOCK(&iap->ia_ifa);
1352 }
1353 lck_rw_done(in_ifaddr_rwlock);
1354
1355 if (ia == NULL) {
1356 ifnet_lock_shared(ifp);
1357 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
1358 iap = ifatoia(ifa);
1359 IFA_LOCK(&iap->ia_ifa);
1360 if (iap->ia_addr.sin_family == AF_INET) {
1361 ia = iap;
1362 IFA_ADDREF_LOCKED(&iap->ia_ifa);
1363 IFA_UNLOCK(&iap->ia_ifa);
1364 break;
1365 }
1366 IFA_UNLOCK(&iap->ia_ifa);
1367 }
1368 ifnet_lock_done(ifp);
1369 }
1370 }
1371
1372 /*
1373 * Unlock the socket since ifnet_ioctl() may be invoked by
1374 * one of the ioctl handlers below. Socket will be re-locked
1375 * prior to returning.
1376 */
1377 if (so != NULL) {
1378 socket_unlock(so, 0);
1379 so_unlocked = TRUE;
1380 }
1381
1382 switch (cmd) {
1383 case SIOCAIFADDR: /* struct {if,in_}aliasreq */
1384 case SIOCDIFADDR: /* struct ifreq */
1385 if (cmd == SIOCAIFADDR) {
1386 bcopy(&((struct in_aliasreq *)(void *)data)->
1387 ifra_addr, &addr, sizeof(addr));
1388 bcopy(&((struct in_aliasreq *)(void *)data)->
1389 ifra_dstaddr, &dstaddr, sizeof(dstaddr));
1390 } else {
1391 VERIFY(cmd == SIOCDIFADDR);
1392 bcopy(&((struct ifreq *)(void *)data)->ifr_addr,
1393 &addr, sizeof(addr));
1394 bzero(&dstaddr, sizeof(dstaddr));
1395 }
1396
1397 if (addr.sin_family == AF_INET) {
1398 struct in_ifaddr *oia;
1399
1400 lck_rw_lock_shared(in_ifaddr_rwlock);
1401 for (oia = ia; ia; ia = ia->ia_link.tqe_next) {
1402 IFA_LOCK(&ia->ia_ifa);
1403 if (ia->ia_ifp == ifp &&
1404 ia->ia_addr.sin_addr.s_addr ==
1405 addr.sin_addr.s_addr) {
1406 IFA_ADDREF_LOCKED(&ia->ia_ifa);
1407 IFA_UNLOCK(&ia->ia_ifa);
1408 break;
1409 }
1410 IFA_UNLOCK(&ia->ia_ifa);
1411 }
1412 lck_rw_done(in_ifaddr_rwlock);
1413 if (oia != NULL) {
1414 IFA_REMREF(&oia->ia_ifa);
1415 }
1416 if ((ifp->if_flags & IFF_POINTOPOINT) &&
1417 (cmd == SIOCAIFADDR) &&
1418 (dstaddr.sin_addr.s_addr == INADDR_ANY)) {
1419 error = EDESTADDRREQ;
1420 goto done;
1421 }
1422 } else if (cmd == SIOCAIFADDR) {
1423 error = EINVAL;
1424 goto done;
1425 }
1426 if (cmd == SIOCDIFADDR) {
1427 if (ia == NULL) {
1428 error = EADDRNOTAVAIL;
1429 goto done;
1430 }
1431
1432 IFA_LOCK(&ia->ia_ifa);
1433 /*
1434 * Avoid the race condition seen when two
1435 * threads process SIOCDIFADDR command
1436 * at the same time.
1437 */
1438 while (ia->ia_ifa.ifa_debug & IFD_DETACHING) {
1439 os_log(OS_LOG_DEFAULT,
1440 "Another thread is already attempting to "
1441 "delete IPv4 address: %s on interface %s. "
1442 "Go to sleep and check again after the operation is done",
1443 inet_ntoa(sa->sin_addr), ia->ia_ifp->if_xname);
1444 ia->ia_ifa.ifa_del_waiters++;
1445 (void) msleep(ia->ia_ifa.ifa_del_wc, &ia->ia_ifa.ifa_lock, (PZERO - 1),
1446 __func__, NULL);
1447 IFA_LOCK_ASSERT_HELD(&ia->ia_ifa);
1448 }
1449
1450 if ((ia->ia_ifa.ifa_debug & IFD_ATTACHED) == 0) {
1451 error = EADDRNOTAVAIL;
1452 IFA_UNLOCK(&ia->ia_ifa);
1453 goto done;
1454 }
1455
1456 ia->ia_ifa.ifa_debug |= IFD_DETACHING;
1457 IFA_UNLOCK(&ia->ia_ifa);
1458 }
1459
1460 /* FALLTHROUGH */
1461 case SIOCSIFADDR: /* struct ifreq */
1462 case SIOCSIFDSTADDR: /* struct ifreq */
1463 case SIOCSIFNETMASK: /* struct ifreq */
1464 if (cmd == SIOCAIFADDR) {
1465 /* fell thru from above; just repeat it */
1466 bcopy(&((struct in_aliasreq *)(void *)data)->
1467 ifra_addr, &addr, sizeof(addr));
1468 } else {
1469 VERIFY(cmd == SIOCDIFADDR || cmd == SIOCSIFADDR ||
1470 cmd == SIOCSIFNETMASK || cmd == SIOCSIFDSTADDR);
1471 bcopy(&((struct ifreq *)(void *)data)->ifr_addr,
1472 &addr, sizeof(addr));
1473 }
1474
1475 if (addr.sin_family != AF_INET && cmd == SIOCSIFADDR) {
1476 error = EINVAL;
1477 goto done;
1478 }
1479 if (ia == NULL) {
1480 ia = in_ifaddr_alloc(M_WAITOK);
1481 if (ia == NULL) {
1482 error = ENOBUFS;
1483 goto done;
1484 }
1485 ifnet_lock_exclusive(ifp);
1486 ifa = &ia->ia_ifa;
1487 IFA_LOCK(ifa);
1488 /* Hold a reference for this routine */
1489 IFA_ADDREF_LOCKED(ifa);
1490 IA_HASH_INIT(ia);
1491 ifa->ifa_addr = (struct sockaddr *)&ia->ia_addr;
1492 ifa->ifa_dstaddr = (struct sockaddr *)&ia->ia_dstaddr;
1493 ifa->ifa_netmask = (struct sockaddr *)&ia->ia_sockmask;
1494 ia->ia_sockmask.sin_len = 8;
1495 if (ifp->if_flags & IFF_BROADCAST) {
1496 ia->ia_broadaddr.sin_len = sizeof(ia->ia_addr);
1497 ia->ia_broadaddr.sin_family = AF_INET;
1498 }
1499 ia->ia_ifp = ifp;
1500 if (!(ifp->if_flags & IFF_LOOPBACK)) {
1501 in_interfaces++;
1502 }
1503 /* if_attach_ifa() holds a reference for ifa_link */
1504 if_attach_ifa(ifp, ifa);
1505 /*
1506 * If we have to go through in_ifinit(), make sure
1507 * to avoid installing route(s) based on this address
1508 * via PFC_IFUP event, before the link resolver (ARP)
1509 * initializes it.
1510 */
1511 if (cmd == SIOCAIFADDR || cmd == SIOCSIFADDR) {
1512 ifa->ifa_debug |= IFD_NOTREADY;
1513 }
1514 IFA_UNLOCK(ifa);
1515 ifnet_lock_done(ifp);
1516 lck_rw_lock_exclusive(in_ifaddr_rwlock);
1517 /* Hold a reference for ia_link */
1518 IFA_ADDREF(ifa);
1519 TAILQ_INSERT_TAIL(&in_ifaddrhead, ia, ia_link);
1520 lck_rw_done(in_ifaddr_rwlock);
1521 /* discard error */
1522 (void) in_domifattach(ifp);
1523 error = 0;
1524 }
1525 break;
1526 }
1527
1528 switch (cmd) {
1529 case SIOCGIFDSTADDR: /* struct ifreq */
1530 case SIOCSIFDSTADDR: /* struct ifreq */
1531 error = inctl_ifdstaddr(ifp, ia, cmd, ifr);
1532 break;
1533
1534 case SIOCGIFBRDADDR: /* struct ifreq */
1535 case SIOCSIFBRDADDR: /* struct ifreq */
1536 error = inctl_ifbrdaddr(ifp, ia, cmd, ifr);
1537 break;
1538
1539 case SIOCGIFNETMASK: /* struct ifreq */
1540 case SIOCSIFNETMASK: /* struct ifreq */
1541 error = inctl_ifnetmask(ifp, ia, cmd, ifr);
1542 break;
1543
1544 case SIOCGIFADDR: /* struct ifreq */
1545 case SIOCSIFADDR: /* struct ifreq */
1546 case SIOCAIFADDR: /* struct {if,in_}aliasreq */
1547 case SIOCDIFADDR: /* struct ifreq */
1548 error = inctl_ifaddr(ifp, ia, cmd, ifr);
1549 break;
1550
1551 default:
1552 error = EOPNOTSUPP;
1553 break;
1554 }
1555
1556 done:
1557 if (ia != NULL) {
1558 if (cmd == SIOCDIFADDR) {
1559 IFA_LOCK(&ia->ia_ifa);
1560 ia->ia_ifa.ifa_debug &= ~IFD_DETACHING;
1561 if (ia->ia_ifa.ifa_del_waiters > 0) {
1562 ia->ia_ifa.ifa_del_waiters = 0;
1563 wakeup(ia->ia_ifa.ifa_del_wc);
1564 }
1565 IFA_UNLOCK(&ia->ia_ifa);
1566 }
1567 IFA_REMREF(&ia->ia_ifa);
1568 }
1569 if (so_unlocked) {
1570 socket_lock(so, 0);
1571 }
1572
1573 return error;
1574 }
1575
1576 /*
1577 * Delete any existing route for an interface.
1578 */
1579 void
1580 in_ifscrub(struct ifnet *ifp, struct in_ifaddr *ia, int locked)
1581 {
1582 IFA_LOCK(&ia->ia_ifa);
1583 if ((ia->ia_flags & IFA_ROUTE) == 0) {
1584 IFA_UNLOCK(&ia->ia_ifa);
1585 return;
1586 }
1587 IFA_UNLOCK(&ia->ia_ifa);
1588 if (!locked) {
1589 lck_mtx_lock(rnh_lock);
1590 }
1591 if (ifp->if_flags & (IFF_LOOPBACK | IFF_POINTOPOINT)) {
1592 rtinit_locked(&(ia->ia_ifa), (int)RTM_DELETE, RTF_HOST);
1593 } else {
1594 rtinit_locked(&(ia->ia_ifa), (int)RTM_DELETE, 0);
1595 }
1596 IFA_LOCK(&ia->ia_ifa);
1597 ia->ia_flags &= ~IFA_ROUTE;
1598 IFA_UNLOCK(&ia->ia_ifa);
1599 if (!locked) {
1600 lck_mtx_unlock(rnh_lock);
1601 }
1602 }
1603
1604 /*
1605 * Caller must hold in_ifaddr_rwlock as writer.
1606 */
1607 static void
1608 in_iahash_remove(struct in_ifaddr *ia)
1609 {
1610 LCK_RW_ASSERT(in_ifaddr_rwlock, LCK_RW_ASSERT_EXCLUSIVE);
1611 IFA_LOCK_ASSERT_HELD(&ia->ia_ifa);
1612
1613 if (!IA_IS_HASHED(ia)) {
1614 panic("attempt to remove wrong ia %p from hash table\n", ia);
1615 /* NOTREACHED */
1616 }
1617 TAILQ_REMOVE(INADDR_HASH(ia->ia_addr.sin_addr.s_addr), ia, ia_hash);
1618 IA_HASH_INIT(ia);
1619 if (IFA_REMREF_LOCKED(&ia->ia_ifa) == NULL) {
1620 panic("%s: unexpected (missing) refcnt ifa=%p", __func__,
1621 &ia->ia_ifa);
1622 /* NOTREACHED */
1623 }
1624 }
1625
1626 /*
1627 * Caller must hold in_ifaddr_rwlock as writer.
1628 */
1629 static void
1630 in_iahash_insert(struct in_ifaddr *ia)
1631 {
1632 LCK_RW_ASSERT(in_ifaddr_rwlock, LCK_RW_ASSERT_EXCLUSIVE);
1633 IFA_LOCK_ASSERT_HELD(&ia->ia_ifa);
1634
1635 if (ia->ia_addr.sin_family != AF_INET) {
1636 panic("attempt to insert wrong ia %p into hash table\n", ia);
1637 /* NOTREACHED */
1638 } else if (IA_IS_HASHED(ia)) {
1639 panic("attempt to double-insert ia %p into hash table\n", ia);
1640 /* NOTREACHED */
1641 }
1642 TAILQ_INSERT_HEAD(INADDR_HASH(ia->ia_addr.sin_addr.s_addr),
1643 ia, ia_hash);
1644 IFA_ADDREF_LOCKED(&ia->ia_ifa);
1645 }
1646
1647 /*
1648 * Some point to point interfaces that are tunnels borrow the address from
1649 * an underlying interface (e.g. VPN server). In order for source address
1650 * selection logic to find the underlying interface first, we add the address
1651 * of borrowing point to point interfaces at the end of the list.
1652 * (see rdar://6733789)
1653 *
1654 * Caller must hold in_ifaddr_rwlock as writer.
1655 */
1656 static void
1657 in_iahash_insert_ptp(struct in_ifaddr *ia)
1658 {
1659 struct in_ifaddr *tmp_ifa;
1660 struct ifnet *tmp_ifp;
1661
1662 LCK_RW_ASSERT(in_ifaddr_rwlock, LCK_RW_ASSERT_EXCLUSIVE);
1663 IFA_LOCK_ASSERT_HELD(&ia->ia_ifa);
1664
1665 if (ia->ia_addr.sin_family != AF_INET) {
1666 panic("attempt to insert wrong ia %p into hash table\n", ia);
1667 /* NOTREACHED */
1668 } else if (IA_IS_HASHED(ia)) {
1669 panic("attempt to double-insert ia %p into hash table\n", ia);
1670 /* NOTREACHED */
1671 }
1672 IFA_UNLOCK(&ia->ia_ifa);
1673 TAILQ_FOREACH(tmp_ifa, INADDR_HASH(ia->ia_addr.sin_addr.s_addr),
1674 ia_hash) {
1675 IFA_LOCK(&tmp_ifa->ia_ifa);
1676 /* ia->ia_addr won't change, so check without lock */
1677 if (IA_SIN(tmp_ifa)->sin_addr.s_addr ==
1678 ia->ia_addr.sin_addr.s_addr) {
1679 IFA_UNLOCK(&tmp_ifa->ia_ifa);
1680 break;
1681 }
1682 IFA_UNLOCK(&tmp_ifa->ia_ifa);
1683 }
1684 tmp_ifp = (tmp_ifa == NULL) ? NULL : tmp_ifa->ia_ifp;
1685
1686 IFA_LOCK(&ia->ia_ifa);
1687 if (tmp_ifp == NULL) {
1688 TAILQ_INSERT_HEAD(INADDR_HASH(ia->ia_addr.sin_addr.s_addr),
1689 ia, ia_hash);
1690 } else {
1691 TAILQ_INSERT_TAIL(INADDR_HASH(ia->ia_addr.sin_addr.s_addr),
1692 ia, ia_hash);
1693 }
1694 IFA_ADDREF_LOCKED(&ia->ia_ifa);
1695 }
1696
1697 /*
1698 * Initialize an interface's internet address
1699 * and routing table entry.
1700 */
1701 static int
1702 in_ifinit(struct ifnet *ifp, struct in_ifaddr *ia, struct sockaddr_in *sin,
1703 int scrub)
1704 {
1705 u_int32_t i = ntohl(sin->sin_addr.s_addr);
1706 struct sockaddr_in oldaddr;
1707 int flags = RTF_UP, error;
1708 struct ifaddr *ifa0;
1709 unsigned int cmd;
1710 int oldremoved = 0;
1711
1712 /* Take an extra reference for this routine */
1713 IFA_ADDREF(&ia->ia_ifa);
1714
1715 lck_rw_lock_exclusive(in_ifaddr_rwlock);
1716 IFA_LOCK(&ia->ia_ifa);
1717 oldaddr = ia->ia_addr;
1718 if (IA_IS_HASHED(ia)) {
1719 oldremoved = 1;
1720 in_iahash_remove(ia);
1721 }
1722 ia->ia_addr = *sin;
1723 /*
1724 * Interface addresses should not contain port or sin_zero information.
1725 */
1726 SIN(&ia->ia_addr)->sin_family = AF_INET;
1727 SIN(&ia->ia_addr)->sin_len = sizeof(struct sockaddr_in);
1728 SIN(&ia->ia_addr)->sin_port = 0;
1729 bzero(&SIN(&ia->ia_addr)->sin_zero, sizeof(sin->sin_zero));
1730 if ((ifp->if_flags & IFF_POINTOPOINT)) {
1731 in_iahash_insert_ptp(ia);
1732 } else {
1733 in_iahash_insert(ia);
1734 }
1735 IFA_UNLOCK(&ia->ia_ifa);
1736 lck_rw_done(in_ifaddr_rwlock);
1737
1738 /*
1739 * Give the interface a chance to initialize if this is its first
1740 * address, and to validate the address if necessary. Send down
1741 * SIOCSIFADDR for first address, and SIOCAIFADDR for alias(es).
1742 * We find the first IPV4 address assigned to it and check if this
1743 * is the same as the one passed into this routine.
1744 */
1745 ifa0 = ifa_ifpgetprimary(ifp, AF_INET);
1746 cmd = (&ia->ia_ifa == ifa0) ? SIOCSIFADDR : SIOCAIFADDR;
1747 error = ifnet_ioctl(ifp, PF_INET, cmd, ia);
1748 if (error == EOPNOTSUPP) {
1749 error = 0;
1750 }
1751 /*
1752 * If we've just sent down SIOCAIFADDR, send another ioctl down
1753 * for SIOCSIFADDR for the first IPV4 address of the interface,
1754 * because an address change on one of the addresses will result
1755 * in the removal of the previous first IPV4 address. KDP needs
1756 * be reconfigured with the current primary IPV4 address.
1757 */
1758 if (error == 0 && cmd == SIOCAIFADDR) {
1759 /*
1760 * NOTE: SIOCSIFADDR is defined with struct ifreq
1761 * as parameter, but here we are sending it down
1762 * to the interface with a pointer to struct ifaddr,
1763 * for legacy reasons.
1764 */
1765 error = ifnet_ioctl(ifp, PF_INET, SIOCSIFADDR, ifa0);
1766 if (error == EOPNOTSUPP) {
1767 error = 0;
1768 }
1769 }
1770
1771 /* Release reference from ifa_ifpgetprimary() */
1772 IFA_REMREF(ifa0);
1773
1774 if (error) {
1775 lck_rw_lock_exclusive(in_ifaddr_rwlock);
1776 IFA_LOCK(&ia->ia_ifa);
1777 if (IA_IS_HASHED(ia)) {
1778 in_iahash_remove(ia);
1779 }
1780 ia->ia_addr = oldaddr;
1781 if (oldremoved) {
1782 if ((ifp->if_flags & IFF_POINTOPOINT)) {
1783 in_iahash_insert_ptp(ia);
1784 } else {
1785 in_iahash_insert(ia);
1786 }
1787 }
1788 IFA_UNLOCK(&ia->ia_ifa);
1789 lck_rw_done(in_ifaddr_rwlock);
1790 /* Release extra reference taken above */
1791 IFA_REMREF(&ia->ia_ifa);
1792 return error;
1793 }
1794 lck_mtx_lock(rnh_lock);
1795 IFA_LOCK(&ia->ia_ifa);
1796 /*
1797 * Address has been initialized by the link resolver (ARP)
1798 * via ifnet_ioctl() above; it may now generate route(s).
1799 */
1800 ia->ia_ifa.ifa_debug &= ~IFD_NOTREADY;
1801 if (scrub) {
1802 ia->ia_ifa.ifa_addr = (struct sockaddr *)&oldaddr;
1803 IFA_UNLOCK(&ia->ia_ifa);
1804 in_ifscrub(ifp, ia, 1);
1805 IFA_LOCK(&ia->ia_ifa);
1806 ia->ia_ifa.ifa_addr = (struct sockaddr *)&ia->ia_addr;
1807 }
1808 IFA_LOCK_ASSERT_HELD(&ia->ia_ifa);
1809 if (IN_CLASSA(i)) {
1810 ia->ia_netmask = IN_CLASSA_NET;
1811 } else if (IN_CLASSB(i)) {
1812 ia->ia_netmask = IN_CLASSB_NET;
1813 } else {
1814 ia->ia_netmask = IN_CLASSC_NET;
1815 }
1816 /*
1817 * The subnet mask usually includes at least the standard network part,
1818 * but may may be smaller in the case of supernetting.
1819 * If it is set, we believe it.
1820 */
1821 if (ia->ia_subnetmask == 0) {
1822 ia->ia_subnetmask = ia->ia_netmask;
1823 ia->ia_sockmask.sin_addr.s_addr = htonl(ia->ia_subnetmask);
1824 } else {
1825 ia->ia_netmask &= ia->ia_subnetmask;
1826 }
1827 ia->ia_net = i & ia->ia_netmask;
1828 ia->ia_subnet = i & ia->ia_subnetmask;
1829 in_socktrim(&ia->ia_sockmask);
1830 /*
1831 * Add route for the network.
1832 */
1833 ia->ia_ifa.ifa_metric = ifp->if_metric;
1834 if (ifp->if_flags & IFF_BROADCAST) {
1835 ia->ia_broadaddr.sin_addr.s_addr =
1836 htonl(ia->ia_subnet | ~ia->ia_subnetmask);
1837 ia->ia_netbroadcast.s_addr =
1838 htonl(ia->ia_net | ~ia->ia_netmask);
1839 } else if (ifp->if_flags & IFF_LOOPBACK) {
1840 ia->ia_ifa.ifa_dstaddr = ia->ia_ifa.ifa_addr;
1841 flags |= RTF_HOST;
1842 } else if (ifp->if_flags & IFF_POINTOPOINT) {
1843 if (ia->ia_dstaddr.sin_family != AF_INET) {
1844 IFA_UNLOCK(&ia->ia_ifa);
1845 lck_mtx_unlock(rnh_lock);
1846 /* Release extra reference taken above */
1847 IFA_REMREF(&ia->ia_ifa);
1848 return 0;
1849 }
1850 ia->ia_dstaddr.sin_len = sizeof(struct sockaddr_in);
1851 flags |= RTF_HOST;
1852 }
1853 IFA_UNLOCK(&ia->ia_ifa);
1854
1855 if ((error = rtinit_locked(&(ia->ia_ifa), (int)RTM_ADD, flags)) == 0) {
1856 IFA_LOCK(&ia->ia_ifa);
1857 ia->ia_flags |= IFA_ROUTE;
1858 IFA_UNLOCK(&ia->ia_ifa);
1859 }
1860 lck_mtx_unlock(rnh_lock);
1861
1862 /* XXX check if the subnet route points to the same interface */
1863 if (error == EEXIST) {
1864 error = 0;
1865 }
1866
1867 /*
1868 * If the interface supports multicast, join the "all hosts"
1869 * multicast group on that interface.
1870 */
1871 if (ifp->if_flags & IFF_MULTICAST) {
1872 struct in_addr addr;
1873
1874 lck_mtx_lock(&ifp->if_addrconfig_lock);
1875 addr.s_addr = htonl(INADDR_ALLHOSTS_GROUP);
1876 if (ifp->if_allhostsinm == NULL) {
1877 struct in_multi *inm;
1878 inm = in_addmulti(&addr, ifp);
1879
1880 if (inm != NULL) {
1881 /*
1882 * Keep the reference on inm added by
1883 * in_addmulti above for storing the
1884 * pointer in allhostsinm.
1885 */
1886 ifp->if_allhostsinm = inm;
1887 } else {
1888 printf("%s: failed to add membership to "
1889 "all-hosts multicast address on %s\n",
1890 __func__, if_name(ifp));
1891 }
1892 }
1893 lck_mtx_unlock(&ifp->if_addrconfig_lock);
1894 }
1895
1896 /* Release extra reference taken above */
1897 IFA_REMREF(&ia->ia_ifa);
1898
1899 if (error == 0) {
1900 /* invalidate route caches */
1901 routegenid_inet_update();
1902 }
1903
1904 return error;
1905 }
1906
1907 /*
1908 * Return TRUE if the address might be a local broadcast address.
1909 */
1910 boolean_t
1911 in_broadcast(struct in_addr in, struct ifnet *ifp)
1912 {
1913 struct ifaddr *ifa;
1914 u_int32_t t;
1915
1916 if (in.s_addr == INADDR_BROADCAST || in.s_addr == INADDR_ANY) {
1917 return TRUE;
1918 }
1919 if (!(ifp->if_flags & IFF_BROADCAST)) {
1920 return FALSE;
1921 }
1922 t = ntohl(in.s_addr);
1923
1924 /*
1925 * Look through the list of addresses for a match
1926 * with a broadcast address.
1927 */
1928 #define ia ((struct in_ifaddr *)ifa)
1929 ifnet_lock_shared(ifp);
1930 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
1931 IFA_LOCK(ifa);
1932 if (ifa->ifa_addr->sa_family == AF_INET &&
1933 (in.s_addr == ia->ia_broadaddr.sin_addr.s_addr ||
1934 in.s_addr == ia->ia_netbroadcast.s_addr ||
1935 /*
1936 * Check for old-style (host 0) broadcast.
1937 */
1938 t == ia->ia_subnet || t == ia->ia_net) &&
1939 /*
1940 * Check for an all one subnetmask. These
1941 * only exist when an interface gets a secondary
1942 * address.
1943 */
1944 ia->ia_subnetmask != (u_int32_t)0xffffffff) {
1945 IFA_UNLOCK(ifa);
1946 ifnet_lock_done(ifp);
1947 return TRUE;
1948 }
1949 IFA_UNLOCK(ifa);
1950 }
1951 ifnet_lock_done(ifp);
1952 return FALSE;
1953 #undef ia
1954 }
1955
1956 void
1957 in_purgeaddrs(struct ifnet *ifp)
1958 {
1959 struct ifaddr **ifap;
1960 int err, i;
1961
1962 VERIFY(ifp != NULL);
1963
1964 /*
1965 * Be nice, and try the civilized way first. If we can't get
1966 * rid of them this way, then do it the rough way. We must
1967 * only get here during detach time, after the ifnet has been
1968 * removed from the global list and arrays.
1969 */
1970 err = ifnet_get_address_list_family_internal(ifp, &ifap, AF_INET, 1,
1971 M_WAITOK, 0);
1972 if (err == 0 && ifap != NULL) {
1973 struct ifreq ifr;
1974
1975 bzero(&ifr, sizeof(ifr));
1976 (void) snprintf(ifr.ifr_name, sizeof(ifr.ifr_name),
1977 "%s", if_name(ifp));
1978
1979 for (i = 0; ifap[i] != NULL; i++) {
1980 struct ifaddr *ifa;
1981
1982 ifa = ifap[i];
1983 IFA_LOCK(ifa);
1984 bcopy(ifa->ifa_addr, &ifr.ifr_addr,
1985 sizeof(struct sockaddr_in));
1986 IFA_UNLOCK(ifa);
1987 err = in_control(NULL, SIOCDIFADDR, (caddr_t)&ifr, ifp,
1988 kernproc);
1989 /* if we lost the race, ignore it */
1990 if (err == EADDRNOTAVAIL) {
1991 err = 0;
1992 }
1993 if (err != 0) {
1994 char s_addr[MAX_IPv4_STR_LEN];
1995 char s_dstaddr[MAX_IPv4_STR_LEN];
1996 struct in_addr *s, *d;
1997
1998 IFA_LOCK(ifa);
1999 s = &((struct sockaddr_in *)
2000 (void *)ifa->ifa_addr)->sin_addr;
2001 d = &((struct sockaddr_in *)
2002 (void *)ifa->ifa_dstaddr)->sin_addr;
2003 (void) inet_ntop(AF_INET, &s->s_addr, s_addr,
2004 sizeof(s_addr));
2005 (void) inet_ntop(AF_INET, &d->s_addr, s_dstaddr,
2006 sizeof(s_dstaddr));
2007 IFA_UNLOCK(ifa);
2008
2009 printf("%s: SIOCDIFADDR ifp=%s ifa_addr=%s "
2010 "ifa_dstaddr=%s (err=%d)\n", __func__,
2011 ifp->if_xname, s_addr, s_dstaddr, err);
2012 }
2013 }
2014 ifnet_free_address_list(ifap);
2015 } else if (err != 0 && err != ENXIO) {
2016 printf("%s: error retrieving list of AF_INET addresses for "
2017 "ifp=%s (err=%d)\n", __func__, ifp->if_xname, err);
2018 }
2019 }
2020
2021 /*
2022 * Called as part of ip_init
2023 */
2024 void
2025 in_ifaddr_init(void)
2026 {
2027 in_multi_init();
2028
2029 PE_parse_boot_argn("ifa_debug", &inifa_debug, sizeof(inifa_debug));
2030
2031 inifa_size = (inifa_debug == 0) ? sizeof(struct in_ifaddr) :
2032 sizeof(struct in_ifaddr_dbg);
2033
2034 inifa_zone = zinit(inifa_size, INIFA_ZONE_MAX * inifa_size,
2035 0, INIFA_ZONE_NAME);
2036 if (inifa_zone == NULL) {
2037 panic("%s: failed allocating %s", __func__, INIFA_ZONE_NAME);
2038 /* NOTREACHED */
2039 }
2040 zone_change(inifa_zone, Z_EXPAND, TRUE);
2041 zone_change(inifa_zone, Z_CALLERACCT, FALSE);
2042
2043 lck_mtx_init(&inifa_trash_lock, ifa_mtx_grp, ifa_mtx_attr);
2044 TAILQ_INIT(&inifa_trash_head);
2045 }
2046
2047 static struct in_ifaddr *
2048 in_ifaddr_alloc(int how)
2049 {
2050 struct in_ifaddr *inifa;
2051
2052 inifa = (how == M_WAITOK) ? zalloc(inifa_zone) :
2053 zalloc_noblock(inifa_zone);
2054 if (inifa != NULL) {
2055 bzero(inifa, inifa_size);
2056 inifa->ia_ifa.ifa_free = in_ifaddr_free;
2057 inifa->ia_ifa.ifa_debug |= IFD_ALLOC;
2058 inifa->ia_ifa.ifa_del_wc = &inifa->ia_ifa.ifa_debug;
2059 inifa->ia_ifa.ifa_del_waiters = 0;
2060 ifa_lock_init(&inifa->ia_ifa);
2061 if (inifa_debug != 0) {
2062 struct in_ifaddr_dbg *inifa_dbg =
2063 (struct in_ifaddr_dbg *)inifa;
2064 inifa->ia_ifa.ifa_debug |= IFD_DEBUG;
2065 inifa->ia_ifa.ifa_trace = in_ifaddr_trace;
2066 inifa->ia_ifa.ifa_attached = in_ifaddr_attached;
2067 inifa->ia_ifa.ifa_detached = in_ifaddr_detached;
2068 ctrace_record(&inifa_dbg->inifa_alloc);
2069 }
2070 }
2071 return inifa;
2072 }
2073
2074 static void
2075 in_ifaddr_free(struct ifaddr *ifa)
2076 {
2077 IFA_LOCK_ASSERT_HELD(ifa);
2078
2079 if (ifa->ifa_refcnt != 0) {
2080 panic("%s: ifa %p bad ref cnt", __func__, ifa);
2081 /* NOTREACHED */
2082 }
2083 if (!(ifa->ifa_debug & IFD_ALLOC)) {
2084 panic("%s: ifa %p cannot be freed", __func__, ifa);
2085 /* NOTREACHED */
2086 }
2087 if (ifa->ifa_debug & IFD_DEBUG) {
2088 struct in_ifaddr_dbg *inifa_dbg = (struct in_ifaddr_dbg *)ifa;
2089 ctrace_record(&inifa_dbg->inifa_free);
2090 bcopy(&inifa_dbg->inifa, &inifa_dbg->inifa_old,
2091 sizeof(struct in_ifaddr));
2092 if (ifa->ifa_debug & IFD_TRASHED) {
2093 /* Become a regular mutex, just in case */
2094 IFA_CONVERT_LOCK(ifa);
2095 lck_mtx_lock(&inifa_trash_lock);
2096 TAILQ_REMOVE(&inifa_trash_head, inifa_dbg,
2097 inifa_trash_link);
2098 lck_mtx_unlock(&inifa_trash_lock);
2099 ifa->ifa_debug &= ~IFD_TRASHED;
2100 }
2101 }
2102 IFA_UNLOCK(ifa);
2103 ifa_lock_destroy(ifa);
2104 bzero(ifa, sizeof(struct in_ifaddr));
2105 zfree(inifa_zone, ifa);
2106 }
2107
2108 static void
2109 in_ifaddr_attached(struct ifaddr *ifa)
2110 {
2111 struct in_ifaddr_dbg *inifa_dbg = (struct in_ifaddr_dbg *)ifa;
2112
2113 IFA_LOCK_ASSERT_HELD(ifa);
2114
2115 if (!(ifa->ifa_debug & IFD_DEBUG)) {
2116 panic("%s: ifa %p has no debug structure", __func__, ifa);
2117 /* NOTREACHED */
2118 }
2119 if (ifa->ifa_debug & IFD_TRASHED) {
2120 /* Become a regular mutex, just in case */
2121 IFA_CONVERT_LOCK(ifa);
2122 lck_mtx_lock(&inifa_trash_lock);
2123 TAILQ_REMOVE(&inifa_trash_head, inifa_dbg, inifa_trash_link);
2124 lck_mtx_unlock(&inifa_trash_lock);
2125 ifa->ifa_debug &= ~IFD_TRASHED;
2126 }
2127 }
2128
2129 static void
2130 in_ifaddr_detached(struct ifaddr *ifa)
2131 {
2132 struct in_ifaddr_dbg *inifa_dbg = (struct in_ifaddr_dbg *)ifa;
2133
2134 IFA_LOCK_ASSERT_HELD(ifa);
2135
2136 if (!(ifa->ifa_debug & IFD_DEBUG)) {
2137 panic("%s: ifa %p has no debug structure", __func__, ifa);
2138 /* NOTREACHED */
2139 } else if (ifa->ifa_debug & IFD_TRASHED) {
2140 panic("%s: ifa %p is already in trash list", __func__, ifa);
2141 /* NOTREACHED */
2142 }
2143 ifa->ifa_debug |= IFD_TRASHED;
2144 /* Become a regular mutex, just in case */
2145 IFA_CONVERT_LOCK(ifa);
2146 lck_mtx_lock(&inifa_trash_lock);
2147 TAILQ_INSERT_TAIL(&inifa_trash_head, inifa_dbg, inifa_trash_link);
2148 lck_mtx_unlock(&inifa_trash_lock);
2149 }
2150
2151 static void
2152 in_ifaddr_trace(struct ifaddr *ifa, int refhold)
2153 {
2154 struct in_ifaddr_dbg *inifa_dbg = (struct in_ifaddr_dbg *)ifa;
2155 ctrace_t *tr;
2156 u_int32_t idx;
2157 u_int16_t *cnt;
2158
2159 if (!(ifa->ifa_debug & IFD_DEBUG)) {
2160 panic("%s: ifa %p has no debug structure", __func__, ifa);
2161 /* NOTREACHED */
2162 }
2163 if (refhold) {
2164 cnt = &inifa_dbg->inifa_refhold_cnt;
2165 tr = inifa_dbg->inifa_refhold;
2166 } else {
2167 cnt = &inifa_dbg->inifa_refrele_cnt;
2168 tr = inifa_dbg->inifa_refrele;
2169 }
2170
2171 idx = atomic_add_16_ov(cnt, 1) % INIFA_TRACE_HIST_SIZE;
2172 ctrace_record(&tr[idx]);
2173 }
2174
2175 /*
2176 * Handle SIOCGASSOCIDS ioctl for PF_INET domain.
2177 */
2178 static int
2179 in_getassocids(struct socket *so, uint32_t *cnt, user_addr_t aidp)
2180 {
2181 struct inpcb *inp = sotoinpcb(so);
2182 sae_associd_t aid;
2183
2184 if (inp == NULL || inp->inp_state == INPCB_STATE_DEAD) {
2185 return EINVAL;
2186 }
2187
2188 /* INPCB has no concept of association */
2189 aid = SAE_ASSOCID_ANY;
2190 *cnt = 0;
2191
2192 /* just asking how many there are? */
2193 if (aidp == USER_ADDR_NULL) {
2194 return 0;
2195 }
2196
2197 return copyout(&aid, aidp, sizeof(aid));
2198 }
2199
2200 /*
2201 * Handle SIOCGCONNIDS ioctl for PF_INET domain.
2202 */
2203 static int
2204 in_getconnids(struct socket *so, sae_associd_t aid, uint32_t *cnt,
2205 user_addr_t cidp)
2206 {
2207 struct inpcb *inp = sotoinpcb(so);
2208 sae_connid_t cid;
2209
2210 if (inp == NULL || inp->inp_state == INPCB_STATE_DEAD) {
2211 return EINVAL;
2212 }
2213
2214 if (aid != SAE_ASSOCID_ANY && aid != SAE_ASSOCID_ALL) {
2215 return EINVAL;
2216 }
2217
2218 /* if connected, return 1 connection count */
2219 *cnt = ((so->so_state & SS_ISCONNECTED) ? 1 : 0);
2220
2221 /* just asking how many there are? */
2222 if (cidp == USER_ADDR_NULL) {
2223 return 0;
2224 }
2225
2226 /* if INPCB is connected, assign it connid 1 */
2227 cid = ((*cnt != 0) ? 1 : SAE_CONNID_ANY);
2228
2229 return copyout(&cid, cidp, sizeof(cid));
2230 }
2231
2232 /*
2233 * Handle SIOCGCONNINFO ioctl for PF_INET domain.
2234 */
2235 int
2236 in_getconninfo(struct socket *so, sae_connid_t cid, uint32_t *flags,
2237 uint32_t *ifindex, int32_t *soerror, user_addr_t src, socklen_t *src_len,
2238 user_addr_t dst, socklen_t *dst_len, uint32_t *aux_type,
2239 user_addr_t aux_data, uint32_t *aux_len)
2240 {
2241 struct inpcb *inp = sotoinpcb(so);
2242 struct sockaddr_in sin;
2243 struct ifnet *ifp = NULL;
2244 int error = 0;
2245 u_int32_t copy_len = 0;
2246
2247 /*
2248 * Don't test for INPCB_STATE_DEAD since this may be called
2249 * after SOF_PCBCLEARING is set, e.g. after tcp_close().
2250 */
2251 if (inp == NULL) {
2252 error = EINVAL;
2253 goto out;
2254 }
2255
2256 if (cid != SAE_CONNID_ANY && cid != SAE_CONNID_ALL && cid != 1) {
2257 error = EINVAL;
2258 goto out;
2259 }
2260
2261 ifp = inp->inp_last_outifp;
2262 *ifindex = ((ifp != NULL) ? ifp->if_index : 0);
2263 *soerror = so->so_error;
2264 *flags = 0;
2265 if (so->so_state & SS_ISCONNECTED) {
2266 *flags |= (CIF_CONNECTED | CIF_PREFERRED);
2267 }
2268 if (inp->inp_flags & INP_BOUND_IF) {
2269 *flags |= CIF_BOUND_IF;
2270 }
2271 if (!(inp->inp_flags & INP_INADDR_ANY)) {
2272 *flags |= CIF_BOUND_IP;
2273 }
2274 if (!(inp->inp_flags & INP_ANONPORT)) {
2275 *flags |= CIF_BOUND_PORT;
2276 }
2277
2278 bzero(&sin, sizeof(sin));
2279 sin.sin_len = sizeof(sin);
2280 sin.sin_family = AF_INET;
2281
2282 /* source address and port */
2283 sin.sin_port = inp->inp_lport;
2284 sin.sin_addr.s_addr = inp->inp_laddr.s_addr;
2285 if (*src_len == 0) {
2286 *src_len = sin.sin_len;
2287 } else {
2288 if (src != USER_ADDR_NULL) {
2289 copy_len = min(*src_len, sizeof(sin));
2290 error = copyout(&sin, src, copy_len);
2291 if (error != 0) {
2292 goto out;
2293 }
2294 *src_len = copy_len;
2295 }
2296 }
2297
2298 /* destination address and port */
2299 sin.sin_port = inp->inp_fport;
2300 sin.sin_addr.s_addr = inp->inp_faddr.s_addr;
2301 if (*dst_len == 0) {
2302 *dst_len = sin.sin_len;
2303 } else {
2304 if (dst != USER_ADDR_NULL) {
2305 copy_len = min(*dst_len, sizeof(sin));
2306 error = copyout(&sin, dst, copy_len);
2307 if (error != 0) {
2308 goto out;
2309 }
2310 *dst_len = copy_len;
2311 }
2312 }
2313
2314 if (SOCK_PROTO(so) == IPPROTO_TCP) {
2315 struct conninfo_tcp tcp_ci;
2316
2317 *aux_type = CIAUX_TCP;
2318 if (*aux_len == 0) {
2319 *aux_len = sizeof(tcp_ci);
2320 } else {
2321 if (aux_data != USER_ADDR_NULL) {
2322 copy_len = min(*aux_len, sizeof(tcp_ci));
2323 bzero(&tcp_ci, sizeof(tcp_ci));
2324 tcp_getconninfo(so, &tcp_ci);
2325 error = copyout(&tcp_ci, aux_data, copy_len);
2326 if (error != 0) {
2327 goto out;
2328 }
2329 *aux_len = copy_len;
2330 }
2331 }
2332 } else {
2333 *aux_type = 0;
2334 *aux_len = 0;
2335 }
2336
2337 out:
2338 return error;
2339 }
2340
2341 struct in_llentry {
2342 struct llentry base;
2343 };
2344
2345 #define IN_LLTBL_DEFAULT_HSIZE 32
2346 #define IN_LLTBL_HASH(k, h) \
2347 ((((((((k) >> 8) ^ (k)) >> 8) ^ (k)) >> 8) ^ (k)) & ((h) - 1))
2348
2349 /*
2350 * Do actual deallocation of @lle.
2351 */
2352 static void
2353 in_lltable_destroy_lle_unlocked(struct llentry *lle)
2354 {
2355 LLE_LOCK_DESTROY(lle);
2356 LLE_REQ_DESTROY(lle);
2357 FREE(lle, M_LLTABLE);
2358 }
2359
2360 /*
2361 * Called by LLE_FREE_LOCKED when number of references
2362 * drops to zero.
2363 */
2364 static void
2365 in_lltable_destroy_lle(struct llentry *lle)
2366 {
2367 LLE_WUNLOCK(lle);
2368 in_lltable_destroy_lle_unlocked(lle);
2369 }
2370
2371 static struct llentry *
2372 in_lltable_new(struct in_addr addr4, u_int flags)
2373 {
2374 #pragma unused(flags)
2375 struct in_llentry *lle;
2376
2377 MALLOC(lle, struct in_llentry *, sizeof(struct in_llentry), M_LLTABLE, M_NOWAIT | M_ZERO);
2378 if (lle == NULL) { /* NB: caller generates msg */
2379 return NULL;
2380 }
2381
2382 /*
2383 * For IPv4 this will trigger "arpresolve" to generate
2384 * an ARP request.
2385 */
2386 lle->base.la_expire = net_uptime(); /* mark expired */
2387 lle->base.r_l3addr.addr4 = addr4;
2388 lle->base.lle_refcnt = 1;
2389 lle->base.lle_free = in_lltable_destroy_lle;
2390
2391 LLE_LOCK_INIT(&lle->base);
2392 LLE_REQ_INIT(&lle->base);
2393 //callout_init(&lle->base.lle_timer, 1);
2394
2395 return &lle->base;
2396 }
2397
2398 #define IN_ARE_MASKED_ADDR_EQUAL(d, a, m) ( \
2399 ((((d).s_addr ^ (a).s_addr) & (m).s_addr)) == 0 )
2400
2401 static int
2402 in_lltable_match_prefix(const struct sockaddr *saddr,
2403 const struct sockaddr *smask, u_int flags, struct llentry *lle)
2404 {
2405 struct in_addr addr, mask, lle_addr;
2406
2407 addr = ((const struct sockaddr_in *)(const void *)saddr)->sin_addr;
2408 mask = ((const struct sockaddr_in *)(const void *)smask)->sin_addr;
2409 lle_addr.s_addr = ntohl(lle->r_l3addr.addr4.s_addr);
2410
2411 if (IN_ARE_MASKED_ADDR_EQUAL(lle_addr, addr, mask) == 0) {
2412 return 0;
2413 }
2414
2415 if (lle->la_flags & LLE_IFADDR) {
2416 /*
2417 * Delete LLE_IFADDR records IFF address & flag matches.
2418 * Note that addr is the interface address within prefix
2419 * being matched.
2420 * Note also we should handle 'ifdown' cases without removing
2421 * ifaddr macs.
2422 */
2423 if (addr.s_addr == lle_addr.s_addr && (flags & LLE_STATIC) != 0) {
2424 return 1;
2425 }
2426 return 0;
2427 }
2428
2429 /* flags & LLE_STATIC means deleting both dynamic and static entries */
2430 if ((flags & LLE_STATIC) || !(lle->la_flags & LLE_STATIC)) {
2431 return 1;
2432 }
2433
2434 return 0;
2435 }
2436
2437 static void
2438 in_lltable_free_entry(struct lltable *llt, struct llentry *lle)
2439 {
2440 struct ifnet *ifp;
2441 size_t pkts_dropped;
2442
2443 LLE_WLOCK_ASSERT(lle);
2444 KASSERT(llt != NULL, ("lltable is NULL"));
2445
2446 /* Unlink entry from table if not already */
2447 if ((lle->la_flags & LLE_LINKED) != 0) {
2448 ifp = llt->llt_ifp;
2449 IF_AFDATA_WLOCK_ASSERT(ifp, llt->llt_af);
2450 lltable_unlink_entry(llt, lle);
2451 }
2452
2453 #if 0
2454 /* cancel timer */
2455 if (callout_stop(&lle->lle_timer) > 0) {
2456 LLE_REMREF(lle);
2457 }
2458 #endif
2459 /* Drop hold queue */
2460 pkts_dropped = llentry_free(lle);
2461 arpstat.dropped += pkts_dropped;
2462 }
2463
2464
2465 static int
2466 in_lltable_rtcheck(struct ifnet *ifp, u_int flags, const struct sockaddr *l3addr)
2467 {
2468 #pragma unused(flags)
2469 struct rtentry *rt;
2470
2471 KASSERT(l3addr->sa_family == AF_INET,
2472 ("sin_family %d", l3addr->sa_family));
2473
2474 /* XXX rtalloc1 should take a const param */
2475 rt = rtalloc1(__DECONST(struct sockaddr *, l3addr), 0, 0);
2476 if (rt == NULL || (rt->rt_flags & RTF_GATEWAY) || rt->rt_ifp != ifp) {
2477 log(LOG_INFO, "IPv4 address: \"%s\" is not on the network\n",
2478 inet_ntoa(((const struct sockaddr_in *)(const void *)l3addr)->sin_addr));
2479 if (rt != NULL) {
2480 rtfree_locked(rt);
2481 }
2482 return EINVAL;
2483 }
2484 rtfree_locked(rt);
2485 return 0;
2486 }
2487
2488 static inline uint32_t
2489 in_lltable_hash_dst(const struct in_addr dst, uint32_t hsize)
2490 {
2491 return IN_LLTBL_HASH(dst.s_addr, hsize);
2492 }
2493
2494 static uint32_t
2495 in_lltable_hash(const struct llentry *lle, uint32_t hsize)
2496 {
2497 return in_lltable_hash_dst(lle->r_l3addr.addr4, hsize);
2498 }
2499
2500
2501 static void
2502 in_lltable_fill_sa_entry(const struct llentry *lle, struct sockaddr *sa)
2503 {
2504 struct sockaddr_in *sin;
2505
2506 sin = (struct sockaddr_in *)(void *)sa;
2507 bzero(sin, sizeof(*sin));
2508 sin->sin_family = AF_INET;
2509 sin->sin_len = sizeof(*sin);
2510 sin->sin_addr = lle->r_l3addr.addr4;
2511 }
2512
2513 static inline struct llentry *
2514 in_lltable_find_dst(struct lltable *llt, struct in_addr dst)
2515 {
2516 struct llentry *lle;
2517 struct llentries *lleh;
2518 u_int hashidx;
2519
2520 hashidx = in_lltable_hash_dst(dst, llt->llt_hsize);
2521 lleh = &llt->lle_head[hashidx];
2522 LIST_FOREACH(lle, lleh, lle_next) {
2523 if (lle->la_flags & LLE_DELETED) {
2524 continue;
2525 }
2526 if (lle->r_l3addr.addr4.s_addr == dst.s_addr) {
2527 break;
2528 }
2529 }
2530
2531 return lle;
2532 }
2533
2534 static void
2535 in_lltable_delete_entry(struct lltable *llt, struct llentry *lle)
2536 {
2537 #pragma unused(llt)
2538 lle->la_flags |= LLE_DELETED;
2539 //EVENTHANDLER_INVOKE(lle_event, lle, LLENTRY_DELETED);
2540 #ifdef DIAGNOSTIC
2541 log(LOG_INFO, "ifaddr cache = %p is deleted\n", lle);
2542 #endif
2543 llentry_free(lle);
2544 }
2545
2546 static struct llentry *
2547 in_lltable_alloc(struct lltable *llt, u_int flags, const struct sockaddr *l3addr)
2548 {
2549 const struct sockaddr_in *sin = (const struct sockaddr_in *) (const void *)l3addr;
2550 struct ifnet *ifp = llt->llt_ifp;
2551 struct llentry *lle;
2552
2553 KASSERT(l3addr->sa_family == AF_INET,
2554 ("sin_family %d", l3addr->sa_family));
2555
2556 /*
2557 * A route that covers the given address must have
2558 * been installed 1st because we are doing a resolution,
2559 * verify this.
2560 */
2561 if (!(flags & LLE_IFADDR) &&
2562 in_lltable_rtcheck(ifp, flags, l3addr) != 0) {
2563 return NULL;
2564 }
2565
2566 lle = in_lltable_new(sin->sin_addr, flags);
2567 if (lle == NULL) {
2568 log(LOG_INFO, "lla_lookup: new lle malloc failed\n");
2569 return NULL;
2570 }
2571 lle->la_flags = flags & ~LLE_CREATE;
2572 if (flags & LLE_STATIC) {
2573 lle->r_flags |= RLLE_VALID;
2574 }
2575 if ((flags & LLE_IFADDR) == LLE_IFADDR) {
2576 lltable_set_entry_addr(ifp, lle, LLADDR(SDL(ifp->if_lladdr->ifa_addr)));
2577 lle->la_flags |= LLE_STATIC;
2578 lle->r_flags |= (RLLE_VALID | RLLE_IFADDR);
2579 }
2580 return lle;
2581 }
2582
2583 /*
2584 * Return NULL if not found or marked for deletion.
2585 * If found return lle read locked.
2586 */
2587 static struct llentry *
2588 in_lltable_lookup(struct lltable *llt, u_int flags, const struct sockaddr *l3addr)
2589 {
2590 const struct sockaddr_in *sin = (const struct sockaddr_in *)(const void *)l3addr;
2591 struct llentry *lle;
2592
2593 IF_AFDATA_WLOCK_ASSERT(llt->llt_ifp, llt->llt_af);
2594
2595 KASSERT(l3addr->sa_family == AF_INET,
2596 ("sin_family %d", l3addr->sa_family));
2597 lle = in_lltable_find_dst(llt, sin->sin_addr);
2598
2599 if (lle == NULL) {
2600 return NULL;
2601 }
2602
2603 KASSERT((flags & (LLE_UNLOCKED | LLE_EXCLUSIVE)) !=
2604 (LLE_UNLOCKED | LLE_EXCLUSIVE), ("wrong lle request flags: 0x%X",
2605 flags));
2606
2607 if (flags & LLE_UNLOCKED) {
2608 return lle;
2609 }
2610
2611 if (flags & LLE_EXCLUSIVE) {
2612 LLE_WLOCK(lle);
2613 } else {
2614 LLE_RLOCK(lle);
2615 }
2616
2617 return lle;
2618 }
2619
2620 static int
2621 in_lltable_dump_entry(struct lltable *llt, struct llentry *lle,
2622 struct sysctl_req *wr)
2623 {
2624 struct ifnet *ifp = llt->llt_ifp;
2625 /* XXX stack use */
2626 struct {
2627 struct rt_msghdr rtm;
2628 struct sockaddr_in sin;
2629 struct sockaddr_dl sdl;
2630 } arpc;
2631 struct sockaddr_dl *sdl;
2632 int error;
2633
2634 bzero(&arpc, sizeof(arpc));
2635 /* skip deleted entries */
2636 if ((lle->la_flags & LLE_DELETED) == LLE_DELETED) {
2637 return 0;
2638 }
2639 /* Skip if jailed and not a valid IP of the prison. */
2640 lltable_fill_sa_entry(lle, (struct sockaddr *)&arpc.sin);
2641 /*
2642 * produce a msg made of:
2643 * struct rt_msghdr;
2644 * struct sockaddr_in; (IPv4)
2645 * struct sockaddr_dl;
2646 */
2647 arpc.rtm.rtm_msglen = sizeof(arpc);
2648 arpc.rtm.rtm_version = RTM_VERSION;
2649 arpc.rtm.rtm_type = RTM_GET;
2650 arpc.rtm.rtm_flags = RTF_UP;
2651 arpc.rtm.rtm_addrs = RTA_DST | RTA_GATEWAY;
2652
2653 /* publish */
2654 if (lle->la_flags & LLE_PUB) {
2655 arpc.rtm.rtm_flags |= RTF_ANNOUNCE;
2656 }
2657
2658 sdl = &arpc.sdl;
2659 sdl->sdl_family = AF_LINK;
2660 sdl->sdl_len = sizeof(*sdl);
2661 sdl->sdl_index = ifp->if_index;
2662 sdl->sdl_type = ifp->if_type;
2663 if ((lle->la_flags & LLE_VALID) == LLE_VALID) {
2664 sdl->sdl_alen = ifp->if_addrlen;
2665 bcopy(&lle->ll_addr, LLADDR(sdl), ifp->if_addrlen);
2666 } else {
2667 sdl->sdl_alen = 0;
2668 bzero(LLADDR(sdl), ifp->if_addrlen);
2669 }
2670
2671 arpc.rtm.rtm_rmx.rmx_expire =
2672 lle->la_flags & LLE_STATIC ? 0 : lle->la_expire;
2673 arpc.rtm.rtm_flags |= (RTF_HOST | RTF_LLDATA);
2674 if (lle->la_flags & LLE_STATIC) {
2675 arpc.rtm.rtm_flags |= RTF_STATIC;
2676 }
2677 if (lle->la_flags & LLE_IFADDR) {
2678 arpc.rtm.rtm_flags |= RTF_PINNED;
2679 }
2680 arpc.rtm.rtm_flags |= RTF_PINNED;
2681 arpc.rtm.rtm_index = ifp->if_index;
2682 error = SYSCTL_OUT(wr, &arpc, sizeof(arpc));
2683
2684 return error;
2685 }
2686
2687 static struct lltable *
2688 in_lltattach(struct ifnet *ifp)
2689 {
2690 struct lltable *llt;
2691
2692 llt = lltable_allocate_htbl(IN_LLTBL_DEFAULT_HSIZE);
2693 llt->llt_af = AF_INET;
2694 llt->llt_ifp = ifp;
2695
2696 llt->llt_lookup = in_lltable_lookup;
2697 llt->llt_alloc_entry = in_lltable_alloc;
2698 llt->llt_delete_entry = in_lltable_delete_entry;
2699 llt->llt_dump_entry = in_lltable_dump_entry;
2700 llt->llt_hash = in_lltable_hash;
2701 llt->llt_fill_sa_entry = in_lltable_fill_sa_entry;
2702 llt->llt_free_entry = in_lltable_free_entry;
2703 llt->llt_match_prefix = in_lltable_match_prefix;
2704 lltable_link(llt);
2705
2706 return llt;
2707 }
2708
2709 struct in_ifaddr*
2710 inifa_ifpwithflag(struct ifnet * ifp, uint32_t flag)
2711 {
2712 struct ifaddr *ifa;
2713
2714 ifnet_lock_shared(ifp);
2715 TAILQ_FOREACH(ifa, &ifp->if_addrlist, ifa_link)
2716 {
2717 IFA_LOCK_SPIN(ifa);
2718 if (ifa->ifa_addr->sa_family != AF_INET) {
2719 IFA_UNLOCK(ifa);
2720 continue;
2721 }
2722 if ((((struct in_ifaddr *)ifa)->ia_flags & flag) == flag) {
2723 IFA_ADDREF_LOCKED(ifa);
2724 IFA_UNLOCK(ifa);
2725 break;
2726 }
2727 IFA_UNLOCK(ifa);
2728 }
2729 ifnet_lock_done(ifp);
2730
2731 return (struct in_ifaddr *)ifa;
2732 }
2733
2734 struct in_ifaddr *
2735 inifa_ifpclatv4(struct ifnet * ifp)
2736 {
2737 struct ifaddr *ifa;
2738
2739 ifnet_lock_shared(ifp);
2740 TAILQ_FOREACH(ifa, &ifp->if_addrlist, ifa_link)
2741 {
2742 uint32_t addr = 0;
2743 IFA_LOCK_SPIN(ifa);
2744 if (ifa->ifa_addr->sa_family != AF_INET) {
2745 IFA_UNLOCK(ifa);
2746 continue;
2747 }
2748
2749 addr = ntohl(SIN(ifa->ifa_addr)->sin_addr.s_addr);
2750 if (!IN_LINKLOCAL(addr) &&
2751 !IN_LOOPBACK(addr)) {
2752 IFA_ADDREF_LOCKED(ifa);
2753 IFA_UNLOCK(ifa);
2754 break;
2755 }
2756 IFA_UNLOCK(ifa);
2757 }
2758 ifnet_lock_done(ifp);
2759
2760 return (struct in_ifaddr *)ifa;
2761 }