Coverage Report

Created: 2026-08-14 06:46

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/SockFuzzer/third_party/xnu/bsd/net/if.c
Line
Count
Source
1
/*
2
 * Copyright (c) 2000-2020 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) 1980, 1986, 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
 *  @(#)if.c  8.3 (Berkeley) 1/4/94
61
 * $FreeBSD: src/sys/net/if.c,v 1.85.2.9 2001/07/24 19:10:17 brooks Exp $
62
 */
63
/*
64
 * NOTICE: This file was modified by SPARTA, Inc. in 2006 to introduce
65
 * support for mandatory and extensible security protections.  This notice
66
 * is included in support of clause 2.2 (b) of the Apple Public License,
67
 * Version 2.0.
68
 */
69
70
#include <kern/locks.h>
71
72
#include <sys/param.h>
73
#include <sys/malloc.h>
74
#include <sys/mbuf.h>
75
#include <sys/systm.h>
76
#include <sys/proc.h>
77
#include <sys/socket.h>
78
#include <sys/socketvar.h>
79
#include <sys/protosw.h>
80
#include <sys/kernel.h>
81
#include <sys/sockio.h>
82
#include <sys/syslog.h>
83
#include <sys/sysctl.h>
84
#include <sys/mcache.h>
85
#include <sys/kauth.h>
86
#include <sys/priv.h>
87
#include <kern/zalloc.h>
88
#include <mach/boolean.h>
89
90
#include <machine/endian.h>
91
92
#include <pexpert/pexpert.h>
93
94
#include <net/if.h>
95
#include <net/if_arp.h>
96
#include <net/if_dl.h>
97
#include <net/if_types.h>
98
#include <net/if_var.h>
99
#include <net/if_media.h>
100
#include <net/if_ppp.h>
101
#include <net/ethernet.h>
102
#include <net/network_agent.h>
103
#include <net/pktsched/pktsched_netem.h>
104
#include <net/radix.h>
105
#include <net/route.h>
106
#include <net/dlil.h>
107
#include <net/nwk_wq.h>
108
109
#include <sys/domain.h>
110
#include <libkern/OSAtomic.h>
111
112
#if INET
113
#include <netinet/in.h>
114
#include <netinet/in_var.h>
115
#include <netinet/in_tclass.h>
116
#include <netinet/ip_var.h>
117
#include <netinet/ip.h>
118
#include <netinet/ip6.h>
119
#include <netinet/ip_var.h>
120
#include <netinet/tcp.h>
121
#include <netinet/tcp_var.h>
122
#include <netinet/udp.h>
123
#include <netinet/udp_var.h>
124
#include <netinet6/in6_var.h>
125
#include <netinet6/in6_ifattach.h>
126
#include <netinet6/ip6_var.h>
127
#include <netinet6/nd6.h>
128
#endif /* INET */
129
130
131
#include <os/log.h>
132
133
/*
134
 * System initialization
135
 */
136
137
extern char *proc_name_address(void *);
138
139
/* Lock group and attribute for ifaddr lock */
140
lck_attr_t      *ifa_mtx_attr;
141
lck_grp_t       *ifa_mtx_grp;
142
static lck_grp_attr_t   *ifa_mtx_grp_attr;
143
144
static int ifioctl_ifreq(struct socket *, u_long, struct ifreq *,
145
    struct proc *);
146
static int ifioctl_ifconf(u_long, caddr_t);
147
static int ifioctl_ifclone(u_long, caddr_t);
148
static int ifioctl_iforder(u_long, caddr_t);
149
static int ifioctl_ifdesc(struct ifnet *, u_long, caddr_t, struct proc *);
150
static int ifioctl_linkparams(struct ifnet *, u_long, caddr_t, struct proc *);
151
static int ifioctl_qstats(struct ifnet *, u_long, caddr_t);
152
static int ifioctl_throttle(struct ifnet *, u_long, caddr_t, struct proc *);
153
static int ifioctl_netsignature(struct ifnet *, u_long, caddr_t);
154
static int ifconf(u_long cmd, user_addr_t ifrp, int * ret_space);
155
__private_extern__ void link_rtrequest(int, struct rtentry *, struct sockaddr *);
156
void if_rtproto_del(struct ifnet *ifp, int protocol);
157
158
static int if_addmulti_common(struct ifnet *, const struct sockaddr *,
159
    struct ifmultiaddr **, int);
160
static int if_delmulti_common(struct ifmultiaddr *, struct ifnet *,
161
    const struct sockaddr *, int);
162
static struct ifnet *ifunit_common(const char *, boolean_t);
163
164
static int if_rtmtu(struct radix_node *, void *);
165
static void if_rtmtu_update(struct ifnet *);
166
167
static int if_clone_list(int, int *, user_addr_t);
168
169
MALLOC_DEFINE(M_IFADDR, "ifaddr", "interface address");
170
171
struct  ifnethead ifnet_head = TAILQ_HEAD_INITIALIZER(ifnet_head);
172
173
/* ifnet_ordered_head and if_ordered_count are protected by the ifnet_head lock */
174
struct  ifnethead ifnet_ordered_head = TAILQ_HEAD_INITIALIZER(ifnet_ordered_head);
175
static  u_int32_t if_ordered_count = 0;
176
177
static int      if_cloners_count;
178
LIST_HEAD(, if_clone) if_cloners = LIST_HEAD_INITIALIZER(if_cloners);
179
180
static struct ifaddr *ifa_ifwithnet_common(const struct sockaddr *,
181
    unsigned int);
182
static void if_attach_ifa_common(struct ifnet *, struct ifaddr *, int);
183
static void if_detach_ifa_common(struct ifnet *, struct ifaddr *, int);
184
185
static void if_attach_ifma(struct ifnet *, struct ifmultiaddr *, int);
186
static int if_detach_ifma(struct ifnet *, struct ifmultiaddr *, int);
187
188
static struct ifmultiaddr *ifma_alloc(int);
189
static void ifma_free(struct ifmultiaddr *);
190
static void ifma_trace(struct ifmultiaddr *, int);
191
192
#if DEBUG
193
static unsigned int ifma_debug = 1;     /* debugging (enabled) */
194
#else
195
static unsigned int ifma_debug;         /* debugging (disabled) */
196
#endif /* !DEBUG */
197
static unsigned int ifma_size;          /* size of zone element */
198
static struct zone *ifma_zone;          /* zone for ifmultiaddr */
199
200
0
#define IFMA_TRACE_HIST_SIZE    32      /* size of trace history */
201
202
/* For gdb */
203
__private_extern__ unsigned int ifma_trace_hist_size = IFMA_TRACE_HIST_SIZE;
204
205
struct ifmultiaddr_dbg {
206
  struct ifmultiaddr      ifma;                   /* ifmultiaddr */
207
  u_int16_t               ifma_refhold_cnt;       /* # of ref */
208
  u_int16_t               ifma_refrele_cnt;       /* # of rele */
209
  /*
210
   * Circular lists of IFA_ADDREF and IFA_REMREF callers.
211
   */
212
  ctrace_t                ifma_refhold[IFMA_TRACE_HIST_SIZE];
213
  ctrace_t                ifma_refrele[IFMA_TRACE_HIST_SIZE];
214
  /*
215
   * Trash list linkage
216
   */
217
  TAILQ_ENTRY(ifmultiaddr_dbg) ifma_trash_link;
218
};
219
220
/* List of trash ifmultiaddr entries protected by ifma_trash_lock */
221
static TAILQ_HEAD(, ifmultiaddr_dbg) ifma_trash_head;
222
static decl_lck_mtx_data(, ifma_trash_lock);
223
224
#define IFMA_ZONE_MAX           64              /* maximum elements in zone */
225
1
#define IFMA_ZONE_NAME          "ifmultiaddr"   /* zone name */
226
227
/*
228
 * XXX: declare here to avoid to include many inet6 related files..
229
 * should be more generalized?
230
 */
231
extern void     nd6_setmtu(struct ifnet *);
232
extern lck_mtx_t *nd6_mutex;
233
234
SYSCTL_NODE(_net, PF_LINK, link, CTLFLAG_RW | CTLFLAG_LOCKED, 0, "Link layers");
235
SYSCTL_NODE(_net_link, 0, generic, CTLFLAG_RW | CTLFLAG_LOCKED, 0,
236
    "Generic link-management");
237
238
SYSCTL_DECL(_net_link_generic_system);
239
240
static uint32_t if_verbose = 0;
241
SYSCTL_INT(_net_link_generic_system, OID_AUTO, if_verbose,
242
    CTLFLAG_RW | CTLFLAG_LOCKED, &if_verbose, 0, "");
243
244
#if (DEBUG || DEVELOPMENT)
245
static uint32_t default_tcp_kao_max = 0;
246
SYSCTL_INT(_net_link_generic_system, OID_AUTO, default_tcp_kao_max,
247
    CTLFLAG_RW | CTLFLAG_LOCKED, &default_tcp_kao_max, 0, "");
248
#else
249
static const uint32_t default_tcp_kao_max = 0;
250
#endif /* (DEBUG || DEVELOPMENT) */
251
252
u_int32_t companion_link_sock_buffer_limit = 0;
253
254
static int
255
sysctl_set_companion_link_sock_buf_limit SYSCTL_HANDLER_ARGS
256
0
{
257
0
#pragma unused(arg1, arg2)
258
0
  int error, tmp = companion_link_sock_buffer_limit;
259
0
  error = sysctl_handle_int(oidp, &tmp, 0, req);
260
0
  if (tmp < 0) {
261
0
    return EINVAL;
262
0
  }
263
0
  if ((error = priv_check_cred(kauth_cred_get(),
264
0
      PRIV_NET_INTERFACE_CONTROL, 0)) != 0) {
265
0
    return error;
266
0
  }
267
268
0
  u_int32_t new_limit = tmp;
269
0
  if (new_limit == companion_link_sock_buffer_limit) {
270
0
    return 0;
271
0
  }
272
273
0
  bool recover = new_limit == 0 ? true : false;
274
0
  if (recover) {
275
0
    error = inp_recover_companion_link(&tcbinfo);
276
0
  } else {
277
0
    error = inp_limit_companion_link(&tcbinfo, new_limit);
278
0
  }
279
0
  if (!error) {
280
0
    companion_link_sock_buffer_limit = new_limit;
281
0
  }
282
0
  return error;
283
0
}
284
285
SYSCTL_PROC(_net_link_generic_system, OID_AUTO, companion_sndbuf_limit,
286
    CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_LOCKED | CTLFLAG_ANYBODY,
287
    &companion_link_sock_buffer_limit, 0, sysctl_set_companion_link_sock_buf_limit,
288
    "I", "set sock send buffer limit of connections using companion links");
289
290
boolean_t intcoproc_unrestricted;
291
292
/* Eventhandler context for interface events */
293
struct eventhandler_lists_ctxt ifnet_evhdlr_ctxt;
294
295
void
296
ifa_init(void)
297
1
{
298
  /* Setup lock group and attribute for ifaddr */
299
1
  ifa_mtx_grp_attr = lck_grp_attr_alloc_init();
300
1
  ifa_mtx_grp = lck_grp_alloc_init("ifaddr", ifa_mtx_grp_attr);
301
1
  ifa_mtx_attr = lck_attr_alloc_init();
302
303
1
  PE_parse_boot_argn("ifa_debug", &ifma_debug, sizeof(ifma_debug));
304
305
1
  ifma_size = (ifma_debug == 0) ? sizeof(struct ifmultiaddr) :
306
1
      sizeof(struct ifmultiaddr_dbg);
307
308
1
  ifma_zone = zone_create(IFMA_ZONE_NAME, ifma_size, ZC_NONE);
309
1
  lck_mtx_init(&ifma_trash_lock, ifa_mtx_grp, ifa_mtx_attr);
310
1
  TAILQ_INIT(&ifma_trash_head);
311
312
1
  PE_parse_boot_argn("intcoproc_unrestricted", &intcoproc_unrestricted,
313
1
      sizeof(intcoproc_unrestricted));
314
1
}
315
316
/*
317
 * Network interface utility routines.
318
 *
319
 * Routines with ifa_ifwith* names take sockaddr *'s as
320
 * parameters.
321
 */
322
323
int if_index;
324
struct ifaddr **ifnet_addrs;
325
struct ifnet **ifindex2ifnet;
326
327
__private_extern__ void
328
if_attach_ifa(struct ifnet *ifp, struct ifaddr *ifa)
329
2
{
330
2
  if_attach_ifa_common(ifp, ifa, 0);
331
2
}
332
333
__private_extern__ void
334
if_attach_link_ifa(struct ifnet *ifp, struct ifaddr *ifa)
335
47
{
336
47
  if_attach_ifa_common(ifp, ifa, 1);
337
47
}
338
339
static void
340
if_attach_ifa_common(struct ifnet *ifp, struct ifaddr *ifa, int link)
341
49
{
342
49
  ifnet_lock_assert(ifp, IFNET_LCK_ASSERT_EXCLUSIVE);
343
49
  IFA_LOCK_ASSERT_HELD(ifa);
344
345
49
  if (ifa->ifa_ifp != ifp) {
346
0
    panic("%s: Mismatch ifa_ifp=%p != ifp=%p", __func__,
347
0
        ifa->ifa_ifp, ifp);
348
    /* NOTREACHED */
349
49
  } else if (ifa->ifa_debug & IFD_ATTACHED) {
350
0
    panic("%s: Attempt to attach an already attached ifa=%p",
351
0
        __func__, ifa);
352
    /* NOTREACHED */
353
49
  } else if (link && !(ifa->ifa_debug & IFD_LINK)) {
354
0
    panic("%s: Unexpected non-link address ifa=%p", __func__, ifa);
355
    /* NOTREACHED */
356
49
  } else if (!link && (ifa->ifa_debug & IFD_LINK)) {
357
0
    panic("%s: Unexpected link address ifa=%p", __func__, ifa);
358
    /* NOTREACHED */
359
0
  }
360
49
  IFA_ADDREF_LOCKED(ifa);
361
49
  ifa->ifa_debug |= IFD_ATTACHED;
362
49
  if (link) {
363
47
    TAILQ_INSERT_HEAD(&ifp->if_addrhead, ifa, ifa_link);
364
47
  } else {
365
2
    TAILQ_INSERT_TAIL(&ifp->if_addrhead, ifa, ifa_link);
366
2
  }
367
368
49
  if (ifa->ifa_attached != NULL) {
369
0
    (*ifa->ifa_attached)(ifa);
370
0
  }
371
372
49
}
373
374
__private_extern__ void
375
if_detach_ifa(struct ifnet *ifp, struct ifaddr *ifa)
376
0
{
377
0
  if_detach_ifa_common(ifp, ifa, 0);
378
0
}
379
380
__private_extern__ void
381
if_detach_link_ifa(struct ifnet *ifp, struct ifaddr *ifa)
382
0
{
383
0
  if_detach_ifa_common(ifp, ifa, 1);
384
0
}
385
386
static void
387
if_detach_ifa_common(struct ifnet *ifp, struct ifaddr *ifa, int link)
388
0
{
389
0
  ifnet_lock_assert(ifp, IFNET_LCK_ASSERT_EXCLUSIVE);
390
0
  IFA_LOCK_ASSERT_HELD(ifa);
391
392
0
  if (link && !(ifa->ifa_debug & IFD_LINK)) {
393
0
    panic("%s: Unexpected non-link address ifa=%p", __func__, ifa);
394
    /* NOTREACHED */
395
0
  } else if (link && ifa != TAILQ_FIRST(&ifp->if_addrhead)) {
396
0
    panic("%s: Link address ifa=%p not first", __func__, ifa);
397
    /* NOTREACHED */
398
0
  } else if (!link && (ifa->ifa_debug & IFD_LINK)) {
399
0
    panic("%s: Unexpected link address ifa=%p", __func__, ifa);
400
    /* NOTREACHED */
401
0
  } else if (!(ifa->ifa_debug & IFD_ATTACHED)) {
402
0
    panic("%s: Attempt to detach an unattached address ifa=%p",
403
0
        __func__, ifa);
404
    /* NOTREACHED */
405
0
  } else if (ifa->ifa_ifp != ifp) {
406
0
    panic("%s: Mismatch ifa_ifp=%p, ifp=%p", __func__,
407
0
        ifa->ifa_ifp, ifp);
408
    /* NOTREACHED */
409
0
  } else if (ifa->ifa_debug & IFD_DEBUG) {
410
0
    struct ifaddr *ifa2;
411
0
    TAILQ_FOREACH(ifa2, &ifp->if_addrhead, ifa_link) {
412
0
      if (ifa2 == ifa) {
413
0
        break;
414
0
      }
415
0
    }
416
0
    if (ifa2 != ifa) {
417
0
      panic("%s: Attempt to detach a stray address ifa=%p",
418
0
          __func__, ifa);
419
      /* NOTREACHED */
420
0
    }
421
0
  }
422
0
  TAILQ_REMOVE(&ifp->if_addrhead, ifa, ifa_link);
423
  /* This must not be the last reference to the ifaddr */
424
0
  if (IFA_REMREF_LOCKED(ifa) == NULL) {
425
0
    panic("%s: unexpected (missing) refcnt ifa=%p", __func__, ifa);
426
    /* NOTREACHED */
427
0
  }
428
0
  ifa->ifa_debug &= ~IFD_ATTACHED;
429
430
0
  if (ifa->ifa_detached != NULL) {
431
0
    (*ifa->ifa_detached)(ifa);
432
0
  }
433
434
0
}
435
436
1
#define INITIAL_IF_INDEXLIM     8
437
438
/*
439
 * Function: if_next_index
440
 * Purpose:
441
 *   Return the next available interface index.
442
 *   Grow the ifnet_addrs[] and ifindex2ifnet[] arrays to accomodate the
443
 *   added entry when necessary.
444
 *
445
 * Note:
446
 *   ifnet_addrs[] is indexed by (if_index - 1), whereas
447
 *   ifindex2ifnet[] is indexed by ifp->if_index.  That requires us to
448
 *   always allocate one extra element to hold ifindex2ifnet[0], which
449
 *   is unused.
450
 */
451
int if_next_index(void);
452
453
__private_extern__ int
454
if_next_index(void)
455
47
{
456
47
  static int      if_indexlim = 0;
457
47
  int             new_index;
458
459
47
  new_index = ++if_index;
460
47
  if (if_index > if_indexlim) {
461
4
    unsigned        n;
462
4
    int             new_if_indexlim;
463
4
    caddr_t         new_ifnet_addrs;
464
4
    caddr_t         new_ifindex2ifnet;
465
4
    caddr_t         old_ifnet_addrs;
466
467
4
    old_ifnet_addrs = (caddr_t)ifnet_addrs;
468
4
    if (ifnet_addrs == NULL) {
469
1
      new_if_indexlim = INITIAL_IF_INDEXLIM;
470
3
    } else {
471
3
      new_if_indexlim = if_indexlim << 1;
472
3
    }
473
474
    /* allocate space for the larger arrays */
475
4
    n = (2 * new_if_indexlim + 1) * sizeof(caddr_t);
476
4
    new_ifnet_addrs = _MALLOC(n, M_IFADDR, M_WAITOK | M_ZERO);
477
4
    if (new_ifnet_addrs == NULL) {
478
0
      --if_index;
479
0
      return -1;
480
0
    }
481
482
4
    new_ifindex2ifnet = new_ifnet_addrs
483
4
        + new_if_indexlim * sizeof(caddr_t);
484
4
    if (ifnet_addrs != NULL) {
485
      /* copy the existing data */
486
3
      bcopy((caddr_t)ifnet_addrs, new_ifnet_addrs,
487
3
          if_indexlim * sizeof(caddr_t));
488
3
      bcopy((caddr_t)ifindex2ifnet,
489
3
          new_ifindex2ifnet,
490
3
          (if_indexlim + 1) * sizeof(caddr_t));
491
3
    }
492
493
    /* switch to the new tables and size */
494
4
    ifnet_addrs = (struct ifaddr **)(void *)new_ifnet_addrs;
495
4
    ifindex2ifnet = (struct ifnet **)(void *)new_ifindex2ifnet;
496
4
    if_indexlim = new_if_indexlim;
497
498
    /* release the old data */
499
4
    if (old_ifnet_addrs != NULL) {
500
3
      _FREE((caddr_t)old_ifnet_addrs, M_IFADDR);
501
3
    }
502
4
  }
503
47
  return new_index;
504
47
}
505
506
/*
507
 * Create a clone network interface.
508
 */
509
static int
510
if_clone_create(char *name, int len, void *params)
511
259
{
512
259
  struct if_clone *ifc;
513
259
  char *dp;
514
259
  int wildcard;
515
259
  u_int32_t bytoff, bitoff;
516
259
  u_int32_t unit;
517
259
  int err;
518
519
259
  ifc = if_clone_lookup(name, &unit);
520
259
  if (ifc == NULL) {
521
214
    return EINVAL;
522
214
  }
523
524
45
  if (ifunit(name) != NULL) {
525
0
    return EEXIST;
526
0
  }
527
528
45
  bytoff = bitoff = 0;
529
45
  wildcard = (unit == UINT32_MAX);
530
  /*
531
   * Find a free unit if none was given.
532
   */
533
45
  if (wildcard) {
534
150
    while ((bytoff < ifc->ifc_bmlen) &&
535
150
        (ifc->ifc_units[bytoff] == 0xff)) {
536
105
      bytoff++;
537
105
    }
538
45
    if (bytoff >= ifc->ifc_bmlen) {
539
0
      return ENOSPC;
540
0
    }
541
195
    while ((ifc->ifc_units[bytoff] & (1 << bitoff)) != 0) {
542
150
      bitoff++;
543
150
    }
544
45
    unit = (bytoff << 3) + bitoff;
545
45
  }
546
547
45
  if (unit > ifc->ifc_maxunit) {
548
0
    return ENXIO;
549
0
  }
550
551
45
  lck_mtx_lock(&ifc->ifc_mutex);
552
45
  err = (*ifc->ifc_create)(ifc, unit, params);
553
45
  if (err != 0) {
554
0
    lck_mtx_unlock(&ifc->ifc_mutex);
555
0
    return err;
556
0
  }
557
558
45
  if (!wildcard) {
559
0
    bytoff = unit >> 3;
560
0
    bitoff = unit - (bytoff << 3);
561
0
  }
562
563
  /*
564
   * Allocate the unit in the bitmap.
565
   */
566
45
  KASSERT((ifc->ifc_units[bytoff] & (1 << bitoff)) == 0,
567
45
      ("%s: bit is already set", __func__));
568
45
  ifc->ifc_units[bytoff] |= (1 << bitoff);
569
570
  /* In the wildcard case, we need to update the name. */
571
45
  if (wildcard) {
572
225
    for (dp = name; *dp != '\0'; dp++) {
573
180
      ;
574
180
    }
575
45
    if (snprintf(dp, len - (dp - name), "%d", unit) >
576
45
        len - (dp - name) - 1) {
577
      /*
578
       * This can only be a programmer error and
579
       * there's no straightforward way to recover if
580
       * it happens.
581
       */
582
0
      panic("%s: interface name too long", __func__);
583
      /* NOTREACHED */
584
0
    }
585
45
  }
586
45
  lck_mtx_unlock(&ifc->ifc_mutex);
587
588
45
  return 0;
589
45
}
590
591
/*
592
 * Destroy a clone network interface.
593
 */
594
static int
595
if_clone_destroy(const char *name)
596
205
{
597
205
  struct if_clone *ifc = NULL;
598
205
  struct ifnet *ifp = NULL;
599
205
  int bytoff, bitoff;
600
205
  u_int32_t unit;
601
205
  int error = 0;
602
603
205
  ifc = if_clone_lookup(name, &unit);
604
605
205
  if (ifc == NULL) {
606
205
    error = EINVAL;
607
205
    goto done;
608
205
  }
609
610
0
  if (unit < ifc->ifc_minifs) {
611
0
    error = EINVAL;
612
0
    goto done;
613
0
  }
614
615
0
  ifp = ifunit_ref(name);
616
0
  if (ifp == NULL) {
617
0
    error = ENXIO;
618
0
    goto done;
619
0
  }
620
621
0
  if (ifc->ifc_destroy == NULL) {
622
0
    error = EOPNOTSUPP;
623
0
    goto done;
624
0
  }
625
626
0
  lck_mtx_lock(&ifc->ifc_mutex);
627
0
  error = (*ifc->ifc_destroy)(ifp);
628
629
0
  if (error) {
630
0
    lck_mtx_unlock(&ifc->ifc_mutex);
631
0
    goto done;
632
0
  }
633
634
  /* Compute offset in the bitmap and deallocate the unit. */
635
0
  bytoff = unit >> 3;
636
0
  bitoff = unit - (bytoff << 3);
637
0
  KASSERT((ifc->ifc_units[bytoff] & (1 << bitoff)) != 0,
638
0
      ("%s: bit is already cleared", __func__));
639
0
  ifc->ifc_units[bytoff] &= ~(1 << bitoff);
640
0
  lck_mtx_unlock(&ifc->ifc_mutex);
641
642
205
done:
643
205
  if (ifp != NULL) {
644
0
    ifnet_decr_iorefcnt(ifp);
645
0
  }
646
205
  return error;
647
0
}
648
649
/*
650
 * Look up a network interface cloner.
651
 */
652
653
__private_extern__ struct if_clone *
654
if_clone_lookup(const char *name, u_int32_t *unitp)
655
464
{
656
464
  struct if_clone *ifc;
657
464
  const char *cp;
658
464
  u_int32_t i;
659
660
3.15k
  for (ifc = LIST_FIRST(&if_cloners); ifc != NULL;) {
661
2.95k
    for (cp = name, i = 0; i < ifc->ifc_namelen; i++, cp++) {
662
2.91k
      if (ifc->ifc_name[i] != *cp) {
663
2.69k
        goto next_ifc;
664
2.69k
      }
665
2.91k
    }
666
46
    goto found_name;
667
2.69k
next_ifc:
668
2.69k
    ifc = LIST_NEXT(ifc, ifc_list);
669
2.69k
  }
670
671
  /* No match. */
672
418
  return (struct if_clone *)NULL;
673
674
46
found_name:
675
46
  if (*cp == '\0') {
676
45
    i = UINT32_MAX;
677
45
  } else {
678
1
    for (i = 0; *cp != '\0'; cp++) {
679
1
      if (*cp < '0' || *cp > '9') {
680
        /* Bogus unit number. */
681
1
        return NULL;
682
1
      }
683
0
      i = (i * 10) + (*cp - '0');
684
0
    }
685
1
  }
686
687
45
  if (unitp != NULL) {
688
45
    *unitp = i;
689
45
  }
690
45
  return ifc;
691
46
}
692
693
void *
694
if_clone_softc_allocate(const struct if_clone *ifc)
695
45
{
696
45
  void *p_clone = NULL;
697
698
45
  VERIFY(ifc != NULL);
699
700
0
  p_clone = zalloc(ifc->ifc_zone);
701
45
  if (p_clone != NULL) {
702
45
    bzero(p_clone, ifc->ifc_softc_size);
703
45
  }
704
705
45
  return p_clone;
706
45
}
707
708
void
709
if_clone_softc_deallocate(const struct if_clone *ifc, void *p_softc)
710
0
{
711
0
  VERIFY(ifc != NULL && p_softc != NULL);
712
0
  bzero(p_softc, ifc->ifc_softc_size);
713
0
  zfree(ifc->ifc_zone, p_softc);
714
0
}
715
716
/*
717
 * Register a network interface cloner.
718
 */
719
int
720
if_clone_attach(struct if_clone *ifc)
721
6
{
722
6
  int bytoff, bitoff;
723
6
  int err;
724
6
  int len, maxclone;
725
6
  u_int32_t unit;
726
727
6
  KASSERT(ifc->ifc_minifs - 1 <= ifc->ifc_maxunit,
728
6
      ("%s: %s requested more units then allowed (%d > %d)",
729
6
      __func__, ifc->ifc_name, ifc->ifc_minifs,
730
6
      ifc->ifc_maxunit + 1));
731
  /*
732
   * Compute bitmap size and allocate it.
733
   */
734
6
  maxclone = ifc->ifc_maxunit + 1;
735
6
  len = maxclone >> 3;
736
6
  if ((len << 3) < maxclone) {
737
1
    len++;
738
1
  }
739
6
  ifc->ifc_units = _MALLOC(len, M_CLONE, M_WAITOK | M_ZERO);
740
6
  if (ifc->ifc_units == NULL) {
741
0
    return ENOBUFS;
742
0
  }
743
6
  ifc->ifc_bmlen = len;
744
6
  lck_mtx_init(&ifc->ifc_mutex, ifnet_lock_group, ifnet_lock_attr);
745
746
6
  if (ifc->ifc_softc_size != 0) {
747
6
    ifc->ifc_zone = zone_create(ifc->ifc_name, ifc->ifc_softc_size,
748
6
        ZC_DESTRUCTIBLE);
749
6
  }
750
751
6
  LIST_INSERT_HEAD(&if_cloners, ifc, ifc_list);
752
6
  if_cloners_count++;
753
754
6
  for (unit = 0; unit < ifc->ifc_minifs; unit++) {
755
0
    err = (*ifc->ifc_create)(ifc, unit, NULL);
756
0
    KASSERT(err == 0,
757
0
        ("%s: failed to create required interface %s%d",
758
0
        __func__, ifc->ifc_name, unit));
759
760
    /* Allocate the unit in the bitmap. */
761
0
    bytoff = unit >> 3;
762
0
    bitoff = unit - (bytoff << 3);
763
0
    ifc->ifc_units[bytoff] |= (1 << bitoff);
764
0
  }
765
766
6
  return 0;
767
6
}
768
769
/*
770
 * Unregister a network interface cloner.
771
 */
772
void
773
if_clone_detach(struct if_clone *ifc)
774
0
{
775
0
  LIST_REMOVE(ifc, ifc_list);
776
0
  FREE(ifc->ifc_units, M_CLONE);
777
0
  if (ifc->ifc_softc_size != 0) {
778
0
    zdestroy(ifc->ifc_zone);
779
0
  }
780
781
0
  lck_mtx_destroy(&ifc->ifc_mutex, ifnet_lock_group);
782
0
  if_cloners_count--;
783
0
}
784
785
/*
786
 * Provide list of interface cloners to userspace.
787
 */
788
static int
789
if_clone_list(int count, int *ret_total, user_addr_t dst)
790
1.44k
{
791
1.44k
  char outbuf[IFNAMSIZ];
792
1.44k
  struct if_clone *ifc;
793
1.44k
  int error = 0;
794
795
1.44k
  *ret_total = if_cloners_count;
796
1.44k
  if (dst == USER_ADDR_NULL) {
797
    /* Just asking how many there are. */
798
1.01k
    return 0;
799
1.01k
  }
800
801
434
  if (count < 0) {
802
100
    return EINVAL;
803
100
  }
804
805
334
  count = (if_cloners_count < count) ? if_cloners_count : count;
806
807
1.66k
  for (ifc = LIST_FIRST(&if_cloners); ifc != NULL && count != 0;
808
1.47k
      ifc = LIST_NEXT(ifc, ifc_list), count--, dst += IFNAMSIZ) {
809
1.47k
    bzero(outbuf, sizeof(outbuf));
810
1.47k
    strlcpy(outbuf, ifc->ifc_name, IFNAMSIZ);
811
1.47k
    error = copyout(outbuf, dst, IFNAMSIZ);
812
1.47k
    if (error) {
813
141
      break;
814
141
    }
815
1.47k
  }
816
817
334
  return error;
818
434
}
819
820
u_int32_t
821
if_functional_type(struct ifnet *ifp, bool exclude_delegate)
822
1
{
823
1
  u_int32_t ret = IFRTYPE_FUNCTIONAL_UNKNOWN;
824
825
1
  if (ifp != NULL) {
826
1
    if (ifp->if_flags & IFF_LOOPBACK) {
827
0
      ret = IFRTYPE_FUNCTIONAL_LOOPBACK;
828
1
    } else if (IFNET_IS_COMPANION_LINK(ifp)) {
829
0
      ret = IFRTYPE_FUNCTIONAL_COMPANIONLINK;
830
1
    } else if ((exclude_delegate &&
831
0
        (ifp->if_family == IFNET_FAMILY_ETHERNET &&
832
0
        ifp->if_subfamily == IFNET_SUBFAMILY_WIFI)) ||
833
1
        (!exclude_delegate && IFNET_IS_WIFI(ifp))) {
834
0
      if (ifp->if_eflags & IFEF_AWDL) {
835
0
        ret = IFRTYPE_FUNCTIONAL_WIFI_AWDL;
836
0
      } else {
837
0
        ret = IFRTYPE_FUNCTIONAL_WIFI_INFRA;
838
0
      }
839
1
    } else if ((exclude_delegate &&
840
0
        (ifp->if_type == IFT_CELLULAR)) ||
841
1
        (!exclude_delegate && IFNET_IS_CELLULAR(ifp))) {
842
0
      ret = IFRTYPE_FUNCTIONAL_CELLULAR;
843
1
    } else if (IFNET_IS_INTCOPROC(ifp)) {
844
0
      ret = IFRTYPE_FUNCTIONAL_INTCOPROC;
845
1
    } else if ((exclude_delegate &&
846
0
        (ifp->if_family == IFNET_FAMILY_ETHERNET ||
847
0
        ifp->if_family == IFNET_FAMILY_BOND ||
848
0
        ifp->if_family == IFNET_FAMILY_VLAN ||
849
0
        ifp->if_family == IFNET_FAMILY_FIREWIRE)) ||
850
1
        (!exclude_delegate && IFNET_IS_WIRED(ifp))) {
851
0
      ret = IFRTYPE_FUNCTIONAL_WIRED;
852
0
    }
853
1
  }
854
855
1
  return ret;
856
1
}
857
858
/*
859
 * Similar to ifa_ifwithaddr, except that this is IPv4 specific
860
 * and that it matches only the local (not broadcast) address.
861
 */
862
__private_extern__ struct in_ifaddr *
863
ifa_foraddr(unsigned int addr)
864
3.02k
{
865
3.02k
  return ifa_foraddr_scoped(addr, IFSCOPE_NONE);
866
3.02k
}
867
868
/*
869
 * Similar to ifa_foraddr, except with the added interface scope
870
 * constraint (unless the caller passes in IFSCOPE_NONE in which
871
 * case there is no scope restriction).
872
 */
873
__private_extern__ struct in_ifaddr *
874
ifa_foraddr_scoped(unsigned int addr, unsigned int scope)
875
22.2k
{
876
22.2k
  struct in_ifaddr *ia = NULL;
877
878
22.2k
  lck_rw_lock_shared(in_ifaddr_rwlock);
879
22.2k
  TAILQ_FOREACH(ia, INADDR_HASH(addr), ia_hash) {
880
22.2k
    IFA_LOCK_SPIN(&ia->ia_ifa);
881
22.2k
    if (ia->ia_addr.sin_addr.s_addr == addr &&
882
22.2k
        (scope == IFSCOPE_NONE || ia->ia_ifp->if_index == scope)) {
883
22.2k
      IFA_ADDREF_LOCKED(&ia->ia_ifa); /* for caller */
884
22.2k
      IFA_UNLOCK(&ia->ia_ifa);
885
22.2k
      break;
886
22.2k
    }
887
0
    IFA_UNLOCK(&ia->ia_ifa);
888
0
  }
889
22.2k
  lck_rw_done(in_ifaddr_rwlock);
890
22.2k
  return ia;
891
22.2k
}
892
893
/*
894
 * Similar to ifa_foraddr, except that this for IPv6.
895
 */
896
__private_extern__ struct in6_ifaddr *
897
ifa_foraddr6(struct in6_addr *addr6)
898
47.3k
{
899
47.3k
  return ifa_foraddr6_scoped(addr6, IFSCOPE_NONE);
900
47.3k
}
901
902
__private_extern__ struct in6_ifaddr *
903
ifa_foraddr6_scoped(struct in6_addr *addr6, unsigned int scope)
904
50.6k
{
905
50.6k
  struct in6_ifaddr *ia = NULL;
906
907
50.6k
  lck_rw_lock_shared(&in6_ifaddr_rwlock);
908
50.6k
  TAILQ_FOREACH(ia, IN6ADDR_HASH(addr6), ia6_hash) {
909
49.3k
    IFA_LOCK(&ia->ia_ifa);
910
49.3k
    if (IN6_ARE_ADDR_EQUAL(&ia->ia_addr.sin6_addr, addr6) &&
911
49.2k
        (scope == IFSCOPE_NONE || ia->ia_ifp->if_index == scope)) {
912
49.2k
      IFA_ADDREF_LOCKED(&ia->ia_ifa); /* for caller */
913
49.2k
      IFA_UNLOCK(&ia->ia_ifa);
914
49.2k
      break;
915
49.2k
    }
916
6
    IFA_UNLOCK(&ia->ia_ifa);
917
6
  }
918
50.6k
  lck_rw_done(&in6_ifaddr_rwlock);
919
920
50.6k
  return ia;
921
50.6k
}
922
923
/*
924
 * Return the first (primary) address of a given family on an interface.
925
 */
926
__private_extern__ struct ifaddr *
927
ifa_ifpgetprimary(struct ifnet *ifp, int family)
928
1
{
929
1
  struct ifaddr *ifa;
930
931
1
  ifnet_lock_shared(ifp);
932
2
  TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
933
2
    IFA_LOCK_SPIN(ifa);
934
2
    if (ifa->ifa_addr->sa_family == family) {
935
1
      IFA_ADDREF_LOCKED(ifa); /* for caller */
936
1
      IFA_UNLOCK(ifa);
937
1
      break;
938
1
    }
939
1
    IFA_UNLOCK(ifa);
940
1
  }
941
1
  ifnet_lock_done(ifp);
942
943
1
  return ifa;
944
1
}
945
946
static inline int
947
ifa_equal(const struct sockaddr *sa1, const struct sockaddr *sa2)
948
25.9k
{
949
25.9k
  if (!sa1 || !sa2) {
950
0
    return 0;
951
0
  }
952
25.9k
  if (sa1->sa_len != sa2->sa_len) {
953
18
    return 0;
954
18
  }
955
956
25.9k
  return bcmp(sa1, sa2, sa1->sa_len) == 0;
957
25.9k
}
958
959
/*
960
 * Locate an interface based on a complete address.
961
 */
962
struct ifaddr *
963
ifa_ifwithaddr_locked(const struct sockaddr *addr)
964
6.55k
{
965
6.55k
  struct ifnet *ifp;
966
6.55k
  struct ifaddr *ifa;
967
6.55k
  struct ifaddr *result = NULL;
968
969
34.4k
  for (ifp = ifnet_head.tqh_first; ifp && !result;
970
27.9k
      ifp = ifp->if_link.tqe_next) {
971
27.9k
    ifnet_lock_shared(ifp);
972
56.8k
    for (ifa = ifp->if_addrhead.tqh_first; ifa;
973
34.9k
        ifa = ifa->ifa_link.tqe_next) {
974
34.9k
      IFA_LOCK_SPIN(ifa);
975
34.9k
      if (ifa->ifa_addr->sa_family != addr->sa_family) {
976
28.4k
        IFA_UNLOCK(ifa);
977
28.4k
        continue;
978
28.4k
      }
979
6.55k
      if (ifa_equal(addr, ifa->ifa_addr)) {
980
6.03k
        result = ifa;
981
6.03k
        IFA_ADDREF_LOCKED(ifa); /* for caller */
982
6.03k
        IFA_UNLOCK(ifa);
983
6.03k
        break;
984
6.03k
      }
985
521
      if ((ifp->if_flags & IFF_BROADCAST) &&
986
0
          ifa->ifa_broadaddr != NULL &&
987
          /* IP6 doesn't have broadcast */
988
0
          ifa->ifa_broadaddr->sa_len != 0 &&
989
0
          ifa_equal(ifa->ifa_broadaddr, addr)) {
990
0
        result = ifa;
991
0
        IFA_ADDREF_LOCKED(ifa); /* for caller */
992
0
        IFA_UNLOCK(ifa);
993
0
        break;
994
0
      }
995
521
      IFA_UNLOCK(ifa);
996
521
    }
997
27.9k
    ifnet_lock_done(ifp);
998
27.9k
  }
999
1000
6.55k
  return result;
1001
6.55k
}
1002
1003
struct ifaddr *
1004
ifa_ifwithaddr(const struct sockaddr *addr)
1005
964
{
1006
964
  struct ifaddr *result = NULL;
1007
1008
964
  ifnet_head_lock_shared();
1009
1010
964
  result = ifa_ifwithaddr_locked(addr);
1011
1012
964
  ifnet_head_done();
1013
1014
964
  return result;
1015
964
}
1016
/*
1017
 * Locate the point to point interface with a given destination address.
1018
 */
1019
/*ARGSUSED*/
1020
struct ifaddr *
1021
ifa_ifwithdstaddr(const struct sockaddr *addr)
1022
2.49k
{
1023
2.49k
  struct ifnet *ifp;
1024
2.49k
  struct ifaddr *ifa;
1025
2.49k
  struct ifaddr *result = NULL;
1026
1027
2.49k
  ifnet_head_lock_shared();
1028
119k
  for (ifp = ifnet_head.tqh_first; ifp && !result;
1029
116k
      ifp = ifp->if_link.tqe_next) {
1030
116k
    if ((ifp->if_flags & IFF_POINTOPOINT)) {
1031
0
      ifnet_lock_shared(ifp);
1032
0
      for (ifa = ifp->if_addrhead.tqh_first; ifa;
1033
0
          ifa = ifa->ifa_link.tqe_next) {
1034
0
        IFA_LOCK_SPIN(ifa);
1035
0
        if (ifa->ifa_addr->sa_family !=
1036
0
            addr->sa_family) {
1037
0
          IFA_UNLOCK(ifa);
1038
0
          continue;
1039
0
        }
1040
0
        if (ifa_equal(addr, ifa->ifa_dstaddr)) {
1041
0
          result = ifa;
1042
0
          IFA_ADDREF_LOCKED(ifa); /* for caller */
1043
0
          IFA_UNLOCK(ifa);
1044
0
          break;
1045
0
        }
1046
0
        IFA_UNLOCK(ifa);
1047
0
      }
1048
0
      ifnet_lock_done(ifp);
1049
0
    }
1050
116k
  }
1051
2.49k
  ifnet_head_done();
1052
2.49k
  return result;
1053
2.49k
}
1054
1055
/*
1056
 * Locate the source address of an interface based on a complete address.
1057
 */
1058
struct ifaddr *
1059
ifa_ifwithaddr_scoped_locked(const struct sockaddr *addr, unsigned int ifscope)
1060
5.61k
{
1061
5.61k
  struct ifaddr *result = NULL;
1062
5.61k
  struct ifnet *ifp;
1063
1064
5.61k
  if (ifscope == IFSCOPE_NONE) {
1065
5.59k
    return ifa_ifwithaddr_locked(addr);
1066
5.59k
  }
1067
1068
20
  if (ifscope > (unsigned int)if_index) {
1069
0
    return NULL;
1070
0
  }
1071
1072
20
  ifp = ifindex2ifnet[ifscope];
1073
20
  if (ifp != NULL) {
1074
20
    struct ifaddr *ifa = NULL;
1075
1076
    /*
1077
     * This is suboptimal; there should be a better way
1078
     * to search for a given address of an interface
1079
     * for any given address family.
1080
     */
1081
20
    ifnet_lock_shared(ifp);
1082
60
    for (ifa = ifp->if_addrhead.tqh_first; ifa != NULL;
1083
60
        ifa = ifa->ifa_link.tqe_next) {
1084
60
      IFA_LOCK_SPIN(ifa);
1085
60
      if (ifa->ifa_addr->sa_family != addr->sa_family) {
1086
40
        IFA_UNLOCK(ifa);
1087
40
        continue;
1088
40
      }
1089
20
      if (ifa_equal(addr, ifa->ifa_addr)) {
1090
20
        result = ifa;
1091
20
        IFA_ADDREF_LOCKED(ifa); /* for caller */
1092
20
        IFA_UNLOCK(ifa);
1093
20
        break;
1094
20
      }
1095
0
      if ((ifp->if_flags & IFF_BROADCAST) &&
1096
0
          ifa->ifa_broadaddr != NULL &&
1097
          /* IP6 doesn't have broadcast */
1098
0
          ifa->ifa_broadaddr->sa_len != 0 &&
1099
0
          ifa_equal(ifa->ifa_broadaddr, addr)) {
1100
0
        result = ifa;
1101
0
        IFA_ADDREF_LOCKED(ifa); /* for caller */
1102
0
        IFA_UNLOCK(ifa);
1103
0
        break;
1104
0
      }
1105
0
      IFA_UNLOCK(ifa);
1106
0
    }
1107
20
    ifnet_lock_done(ifp);
1108
20
  }
1109
1110
20
  return result;
1111
20
}
1112
1113
struct ifaddr *
1114
ifa_ifwithaddr_scoped(const struct sockaddr *addr, unsigned int ifscope)
1115
5.61k
{
1116
5.61k
  struct ifaddr *result = NULL;
1117
1118
5.61k
  ifnet_head_lock_shared();
1119
1120
5.61k
  result = ifa_ifwithaddr_scoped_locked(addr, ifscope);
1121
1122
5.61k
  ifnet_head_done();
1123
1124
5.61k
  return result;
1125
5.61k
}
1126
1127
struct ifaddr *
1128
ifa_ifwithnet(const struct sockaddr *addr)
1129
0
{
1130
0
  return ifa_ifwithnet_common(addr, IFSCOPE_NONE);
1131
0
}
1132
1133
struct ifaddr *
1134
ifa_ifwithnet_scoped(const struct sockaddr *addr, unsigned int ifscope)
1135
0
{
1136
0
  return ifa_ifwithnet_common(addr, ifscope);
1137
0
}
1138
1139
/*
1140
 * Find an interface on a specific network.  If many, choice
1141
 * is most specific found.
1142
 */
1143
static struct ifaddr *
1144
ifa_ifwithnet_common(const struct sockaddr *addr, unsigned int ifscope)
1145
0
{
1146
0
  struct ifnet *ifp;
1147
0
  struct ifaddr *ifa = NULL;
1148
0
  struct ifaddr *ifa_maybe = NULL;
1149
0
  u_int af = addr->sa_family;
1150
0
  const char *addr_data = addr->sa_data, *cplim;
1151
1152
0
  if (af != AF_INET && af != AF_INET6) {
1153
0
    ifscope = IFSCOPE_NONE;
1154
0
  }
1155
1156
0
  ifnet_head_lock_shared();
1157
  /*
1158
   * AF_LINK addresses can be looked up directly by their index number,
1159
   * so do that if we can.
1160
   */
1161
0
  if (af == AF_LINK) {
1162
0
    const struct sockaddr_dl *sdl =
1163
0
        (const struct sockaddr_dl *)(uintptr_t)(size_t)addr;
1164
0
    if (sdl->sdl_index && sdl->sdl_index <= if_index) {
1165
0
      ifa = ifnet_addrs[sdl->sdl_index - 1];
1166
0
      if (ifa != NULL) {
1167
0
        IFA_ADDREF(ifa);
1168
0
      }
1169
1170
0
      ifnet_head_done();
1171
0
      return ifa;
1172
0
    }
1173
0
  }
1174
1175
  /*
1176
   * Scan though each interface, looking for ones that have
1177
   * addresses in this address family.
1178
   */
1179
0
  for (ifp = ifnet_head.tqh_first; ifp; ifp = ifp->if_link.tqe_next) {
1180
0
    ifnet_lock_shared(ifp);
1181
0
    for (ifa = ifp->if_addrhead.tqh_first; ifa;
1182
0
        ifa = ifa->ifa_link.tqe_next) {
1183
0
      const char *cp, *cp2, *cp3;
1184
1185
0
      IFA_LOCK(ifa);
1186
0
      if (ifa->ifa_addr == NULL ||
1187
0
          ifa->ifa_addr->sa_family != af) {
1188
0
next:
1189
0
        IFA_UNLOCK(ifa);
1190
0
        continue;
1191
0
      }
1192
      /*
1193
       * If we're looking up with a scope,
1194
       * find using a matching interface.
1195
       */
1196
0
      if (ifscope != IFSCOPE_NONE &&
1197
0
          ifp->if_index != ifscope) {
1198
0
        IFA_UNLOCK(ifa);
1199
0
        continue;
1200
0
      }
1201
1202
      /*
1203
       * Scan all the bits in the ifa's address.
1204
       * If a bit dissagrees with what we are
1205
       * looking for, mask it with the netmask
1206
       * to see if it really matters.
1207
       * (A byte at a time)
1208
       */
1209
0
      if (ifa->ifa_netmask == 0) {
1210
0
        IFA_UNLOCK(ifa);
1211
0
        continue;
1212
0
      }
1213
0
      cp = addr_data;
1214
0
      cp2 = ifa->ifa_addr->sa_data;
1215
0
      cp3 = ifa->ifa_netmask->sa_data;
1216
0
      cplim = ifa->ifa_netmask->sa_len +
1217
0
          (char *)ifa->ifa_netmask;
1218
0
      while (cp3 < cplim) {
1219
0
        if ((*cp++ ^ *cp2++) & *cp3++) {
1220
0
          goto next; /* next address! */
1221
0
        }
1222
0
      }
1223
      /*
1224
       * If the netmask of what we just found
1225
       * is more specific than what we had before
1226
       * (if we had one) then remember the new one
1227
       * before continuing to search
1228
       * for an even better one.
1229
       */
1230
0
      if (ifa_maybe == NULL ||
1231
0
          rn_refines((caddr_t)ifa->ifa_netmask,
1232
0
          (caddr_t)ifa_maybe->ifa_netmask)) {
1233
0
        IFA_ADDREF_LOCKED(ifa); /* ifa_maybe */
1234
0
        IFA_UNLOCK(ifa);
1235
0
        if (ifa_maybe != NULL) {
1236
0
          IFA_REMREF(ifa_maybe);
1237
0
        }
1238
0
        ifa_maybe = ifa;
1239
0
      } else {
1240
0
        IFA_UNLOCK(ifa);
1241
0
      }
1242
0
      IFA_LOCK_ASSERT_NOTHELD(ifa);
1243
0
    }
1244
0
    ifnet_lock_done(ifp);
1245
1246
0
    if (ifa != NULL) {
1247
0
      break;
1248
0
    }
1249
0
  }
1250
0
  ifnet_head_done();
1251
1252
0
  if (ifa == NULL) {
1253
0
    ifa = ifa_maybe;
1254
0
  } else if (ifa_maybe != NULL) {
1255
0
    IFA_REMREF(ifa_maybe);
1256
0
  }
1257
1258
0
  return ifa;
1259
0
}
1260
1261
/*
1262
 * Find an interface address specific to an interface best matching
1263
 * a given address applying same source address selection rules
1264
 * as done in the kernel for implicit source address binding
1265
 */
1266
struct ifaddr *
1267
ifaof_ifpforaddr_select(const struct sockaddr *addr, struct ifnet *ifp)
1268
0
{
1269
0
  u_int af = addr->sa_family;
1270
1271
0
  if (af == AF_INET6) {
1272
0
    return in6_selectsrc_core_ifa(__DECONST(struct sockaddr_in6 *, addr), ifp, 0);
1273
0
  }
1274
1275
0
  return ifaof_ifpforaddr(addr, ifp);
1276
0
}
1277
1278
/*
1279
 * Find an interface address specific to an interface best matching
1280
 * a given address without regards to source address selection.
1281
 *
1282
 * This is appropriate for use-cases where we just want to update/init
1283
 * some data structure like routing table entries.
1284
 */
1285
struct ifaddr *
1286
ifaof_ifpforaddr(const struct sockaddr *addr, struct ifnet *ifp)
1287
19.3k
{
1288
19.3k
  struct ifaddr *ifa = NULL;
1289
19.3k
  const char *cp, *cp2, *cp3;
1290
19.3k
  char *cplim;
1291
19.3k
  struct ifaddr *ifa_maybe = NULL;
1292
19.3k
  struct ifaddr *better_ifa_maybe = NULL;
1293
19.3k
  u_int af = addr->sa_family;
1294
1295
19.3k
  if (af >= AF_MAX) {
1296
0
    return NULL;
1297
0
  }
1298
1299
19.3k
  ifnet_lock_shared(ifp);
1300
76.7k
  for (ifa = ifp->if_addrhead.tqh_first; ifa;
1301
57.6k
      ifa = ifa->ifa_link.tqe_next) {
1302
57.6k
    IFA_LOCK(ifa);
1303
57.6k
    if (ifa->ifa_addr->sa_family != af) {
1304
38.3k
      IFA_UNLOCK(ifa);
1305
38.3k
      continue;
1306
38.3k
    }
1307
19.3k
    if (ifa_maybe == NULL) {
1308
19.3k
      IFA_ADDREF_LOCKED(ifa); /* for ifa_maybe */
1309
19.3k
      ifa_maybe = ifa;
1310
19.3k
    }
1311
19.3k
    if (ifa->ifa_netmask == 0) {
1312
0
      if (ifa_equal(addr, ifa->ifa_addr) ||
1313
0
          ifa_equal(addr, ifa->ifa_dstaddr)) {
1314
0
        IFA_ADDREF_LOCKED(ifa); /* for caller */
1315
0
        IFA_UNLOCK(ifa);
1316
0
        break;
1317
0
      }
1318
0
      IFA_UNLOCK(ifa);
1319
0
      continue;
1320
0
    }
1321
19.3k
    if (ifp->if_flags & IFF_POINTOPOINT) {
1322
0
      if (ifa_equal(addr, ifa->ifa_dstaddr)) {
1323
0
        IFA_ADDREF_LOCKED(ifa); /* for caller */
1324
0
        IFA_UNLOCK(ifa);
1325
0
        break;
1326
0
      }
1327
19.3k
    } else {
1328
19.3k
      if (ifa_equal(addr, ifa->ifa_addr)) {
1329
        /* exact match */
1330
264
        IFA_ADDREF_LOCKED(ifa); /* for caller */
1331
264
        IFA_UNLOCK(ifa);
1332
264
        break;
1333
264
      }
1334
19.0k
      cp = addr->sa_data;
1335
19.0k
      cp2 = ifa->ifa_addr->sa_data;
1336
19.0k
      cp3 = ifa->ifa_netmask->sa_data;
1337
19.0k
      cplim = ifa->ifa_netmask->sa_len +
1338
19.0k
          (char *)ifa->ifa_netmask;
1339
57.3k
      for (; cp3 < cplim; cp3++) {
1340
57.2k
        if ((*cp++ ^ *cp2++) & *cp3) {
1341
18.9k
          break;
1342
18.9k
        }
1343
57.2k
      }
1344
19.0k
      if (cp3 == cplim) {
1345
        /* subnet match */
1346
96
        if (better_ifa_maybe == NULL) {
1347
          /* for better_ifa_maybe */
1348
96
          IFA_ADDREF_LOCKED(ifa);
1349
96
          better_ifa_maybe = ifa;
1350
96
        }
1351
96
      }
1352
19.0k
    }
1353
19.0k
    IFA_UNLOCK(ifa);
1354
19.0k
  }
1355
1356
19.3k
  if (ifa == NULL) {
1357
19.0k
    if (better_ifa_maybe != NULL) {
1358
96
      ifa = better_ifa_maybe;
1359
96
      better_ifa_maybe = NULL;
1360
18.9k
    } else {
1361
18.9k
      ifa = ifa_maybe;
1362
18.9k
      ifa_maybe = NULL;
1363
18.9k
    }
1364
19.0k
  }
1365
1366
19.3k
  ifnet_lock_done(ifp);
1367
1368
19.3k
  if (better_ifa_maybe != NULL) {
1369
0
    IFA_REMREF(better_ifa_maybe);
1370
0
  }
1371
19.3k
  if (ifa_maybe != NULL) {
1372
360
    IFA_REMREF(ifa_maybe);
1373
360
  }
1374
1375
19.3k
  return ifa;
1376
19.3k
}
1377
1378
#include <net/route.h>
1379
1380
/*
1381
 * Default action when installing a route with a Link Level gateway.
1382
 * Lookup an appropriate real ifa to point to.
1383
 * This should be moved to /sys/net/link.c eventually.
1384
 */
1385
void
1386
link_rtrequest(int cmd, struct rtentry *rt, struct sockaddr *sa)
1387
0
{
1388
0
  struct ifaddr *ifa;
1389
0
  struct sockaddr *dst;
1390
0
  struct ifnet *ifp;
1391
0
  void (*ifa_rtrequest)(int, struct rtentry *, struct sockaddr *);
1392
1393
0
  LCK_MTX_ASSERT(rnh_lock, LCK_MTX_ASSERT_OWNED);
1394
0
  RT_LOCK_ASSERT_HELD(rt);
1395
1396
0
  if (cmd != RTM_ADD || ((ifa = rt->rt_ifa) == 0) ||
1397
0
      ((ifp = ifa->ifa_ifp) == 0) || ((dst = rt_key(rt)) == 0)) {
1398
0
    return;
1399
0
  }
1400
1401
  /* Become a regular mutex, just in case */
1402
0
  RT_CONVERT_LOCK(rt);
1403
1404
0
  ifa = ifaof_ifpforaddr(dst, ifp);
1405
0
  if (ifa) {
1406
0
    rtsetifa(rt, ifa);
1407
0
    IFA_LOCK_SPIN(ifa);
1408
0
    ifa_rtrequest = ifa->ifa_rtrequest;
1409
0
    IFA_UNLOCK(ifa);
1410
0
    if (ifa_rtrequest != NULL && ifa_rtrequest != link_rtrequest) {
1411
0
      ifa_rtrequest(cmd, rt, sa);
1412
0
    }
1413
0
    IFA_REMREF(ifa);
1414
0
  }
1415
0
}
1416
1417
/*
1418
 * if_updown will set the interface up or down. It will
1419
 * prevent other up/down events from occurring until this
1420
 * up/down event has completed.
1421
 *
1422
 * Caller must lock ifnet. This function will drop the
1423
 * lock. This allows ifnet_set_flags to set the rest of
1424
 * the flags after we change the up/down state without
1425
 * dropping the interface lock between setting the
1426
 * up/down state and updating the rest of the flags.
1427
 */
1428
__private_extern__ void
1429
if_updown( struct ifnet *ifp, int up)
1430
28.1k
{
1431
28.1k
  u_int32_t eflags;
1432
28.1k
  int i;
1433
28.1k
  struct ifaddr **ifa;
1434
28.1k
  struct timespec tv;
1435
28.1k
  struct ifclassq *ifq = &ifp->if_snd;
1436
1437
  /* Wait until no one else is changing the up/down state */
1438
28.1k
  while ((ifp->if_eflags & IFEF_UPDOWNCHANGE) != 0) {
1439
0
    tv.tv_sec = 0;
1440
0
    tv.tv_nsec = NSEC_PER_SEC / 10;
1441
0
    ifnet_lock_done(ifp);
1442
0
    msleep(&ifp->if_eflags, NULL, 0, "if_updown", &tv);
1443
0
    ifnet_lock_exclusive(ifp);
1444
0
  }
1445
1446
  /* Verify that the interface isn't already in the right state */
1447
28.1k
  if ((!up && (ifp->if_flags & IFF_UP) == 0) ||
1448
16.9k
      (up && (ifp->if_flags & IFF_UP) == IFF_UP)) {
1449
16.9k
    return;
1450
16.9k
  }
1451
1452
  /* Indicate that the up/down state is changing */
1453
11.1k
  eflags = if_set_eflags(ifp, IFEF_UPDOWNCHANGE);
1454
11.1k
  ASSERT((eflags & IFEF_UPDOWNCHANGE) == 0);
1455
1456
  /* Mark interface up or down */
1457
11.1k
  if (up) {
1458
5.59k
    ifp->if_flags |= IFF_UP;
1459
5.59k
  } else {
1460
5.58k
    ifp->if_flags &= ~IFF_UP;
1461
5.58k
  }
1462
1463
11.1k
  ifnet_touch_lastchange(ifp);
1464
11.1k
  ifnet_touch_lastupdown(ifp);
1465
1466
  /* Drop the lock to notify addresses and route */
1467
11.1k
  ifnet_lock_done(ifp);
1468
1469
11.1k
  IFCQ_LOCK(ifq);
1470
11.1k
  if_qflush(ifp, 1);
1471
1472
  /* Inform all transmit queues about the new link state */
1473
11.1k
  ifnet_update_sndq(ifq, up ? CLASSQ_EV_LINK_UP : CLASSQ_EV_LINK_DOWN);
1474
11.1k
  IFCQ_UNLOCK(ifq);
1475
1476
11.1k
  if (ifnet_get_address_list(ifp, &ifa) == 0) {
1477
44.7k
    for (i = 0; ifa[i] != 0; i++) {
1478
33.5k
      pfctlinput(up ? PRC_IFUP : PRC_IFDOWN, ifa[i]->ifa_addr);
1479
33.5k
    }
1480
11.1k
    ifnet_free_address_list(ifa);
1481
11.1k
  }
1482
11.1k
  rt_ifmsg(ifp);
1483
1484
  /* Aquire the lock to clear the changing flag */
1485
11.1k
  ifnet_lock_exclusive(ifp);
1486
11.1k
  if_clear_eflags(ifp, IFEF_UPDOWNCHANGE);
1487
11.1k
  wakeup(&ifp->if_eflags);
1488
11.1k
}
1489
1490
/*
1491
 * Mark an interface down and notify protocols of
1492
 * the transition.
1493
 */
1494
void
1495
if_down(
1496
  struct ifnet *ifp)
1497
0
{
1498
0
  ifnet_lock_exclusive(ifp);
1499
0
  if_updown(ifp, 0);
1500
0
  ifnet_lock_done(ifp);
1501
0
}
1502
1503
/*
1504
 * Mark an interface up and notify protocols of
1505
 * the transition.
1506
 */
1507
void
1508
if_up(
1509
  struct ifnet *ifp)
1510
0
{
1511
0
  ifnet_lock_exclusive(ifp);
1512
0
  if_updown(ifp, 1);
1513
0
  ifnet_lock_done(ifp);
1514
0
}
1515
1516
/*
1517
 * Flush an interface queue.
1518
 */
1519
void
1520
if_qflush(struct ifnet *ifp, int ifq_locked)
1521
11.1k
{
1522
11.1k
  struct ifclassq *ifq = &ifp->if_snd;
1523
1524
11.1k
  if (!ifq_locked) {
1525
0
    IFCQ_LOCK(ifq);
1526
0
  }
1527
1528
11.1k
  if (IFCQ_IS_ENABLED(ifq)) {
1529
0
    fq_if_request_classq(ifq, CLASSQRQ_PURGE, NULL);
1530
0
  }
1531
1532
11.1k
  VERIFY(IFCQ_IS_EMPTY(ifq));
1533
1534
11.1k
  if (!ifq_locked) {
1535
0
    IFCQ_UNLOCK(ifq);
1536
0
  }
1537
11.1k
}
1538
1539
void
1540
if_qflush_sc(struct ifnet *ifp, mbuf_svc_class_t sc, u_int32_t flow,
1541
    u_int32_t *packets, u_int32_t *bytes, int ifq_locked)
1542
0
{
1543
0
  struct ifclassq *ifq = &ifp->if_snd;
1544
0
  u_int32_t cnt = 0, len = 0;
1545
1546
0
  VERIFY(sc == MBUF_SC_UNSPEC || MBUF_VALID_SC(sc));
1547
0
  VERIFY(flow != 0);
1548
1549
0
  if (!ifq_locked) {
1550
0
    IFCQ_LOCK(ifq);
1551
0
  }
1552
1553
0
  if (IFCQ_IS_ENABLED(ifq)) {
1554
0
    cqrq_purge_sc_t req = { sc, flow, 0, 0 };
1555
1556
0
    fq_if_request_classq(ifq, CLASSQRQ_PURGE_SC, &req);
1557
0
    cnt = req.packets;
1558
0
    len = req.bytes;
1559
0
  }
1560
1561
0
  if (!ifq_locked) {
1562
0
    IFCQ_UNLOCK(ifq);
1563
0
  }
1564
1565
0
  if (packets != NULL) {
1566
0
    *packets = cnt;
1567
0
  }
1568
0
  if (bytes != NULL) {
1569
0
    *bytes = len;
1570
0
  }
1571
0
}
1572
1573
/*
1574
 * Extracts interface unit number and name from string, returns -1 upon failure.
1575
 * Upon success, returns extracted unit number, and interface name in dst.
1576
 */
1577
int
1578
ifunit_extract(const char *src, char *dst, size_t dstlen, int *unit)
1579
86.2k
{
1580
86.2k
  const char *cp;
1581
86.2k
  size_t len, m;
1582
86.2k
  char c;
1583
86.2k
  int u;
1584
1585
86.2k
  if (src == NULL || dst == NULL || dstlen == 0 || unit == NULL) {
1586
0
    return -1;
1587
0
  }
1588
1589
86.2k
  len = strlen(src);
1590
86.2k
  if (len < 2 || len > dstlen) {
1591
44.4k
    return -1;
1592
44.4k
  }
1593
41.7k
  cp = src + len - 1;
1594
41.7k
  c = *cp;
1595
41.7k
  if (c < '0' || c > '9') {
1596
7.72k
    return -1;            /* trailing garbage */
1597
7.72k
  }
1598
34.0k
  u = 0;
1599
34.0k
  m = 1;
1600
40.6k
  do {
1601
40.6k
    if (cp == src) {
1602
119
      return -1;    /* no interface name */
1603
119
    }
1604
40.5k
    u += (c - '0') * m;
1605
40.5k
    if (u > 1000000) {
1606
825
      return -1;    /* number is unreasonable */
1607
825
    }
1608
39.6k
    m *= 10;
1609
39.6k
    c = *--cp;
1610
39.6k
  } while (c >= '0' && c <= '9');
1611
33.0k
  len = cp - src + 1;
1612
33.0k
  bcopy(src, dst, len);
1613
33.0k
  dst[len] = '\0';
1614
33.0k
  *unit = u;
1615
1616
33.0k
  return 0;
1617
34.0k
}
1618
1619
/*
1620
 * Map interface name to
1621
 * interface structure pointer.
1622
 */
1623
static struct ifnet *
1624
ifunit_common(const char *name, boolean_t hold)
1625
86.2k
{
1626
86.2k
  char namebuf[IFNAMSIZ + 1];
1627
86.2k
  struct ifnet *ifp;
1628
86.2k
  int unit;
1629
1630
86.2k
  if (ifunit_extract(name, namebuf, sizeof(namebuf), &unit) < 0) {
1631
53.1k
    return NULL;
1632
53.1k
  }
1633
1634
  /* for safety, since we use strcmp() below */
1635
33.0k
  namebuf[sizeof(namebuf) - 1] = '\0';
1636
1637
  /*
1638
   * Now search all the interfaces for this name/number
1639
   */
1640
33.0k
  ifnet_head_lock_shared();
1641
65.7k
  TAILQ_FOREACH(ifp, &ifnet_head, if_link) {
1642
    /*
1643
     * Use strcmp() rather than strncmp() here,
1644
     * since we want to match the entire string.
1645
     */
1646
65.7k
    if (strcmp(ifp->if_name, namebuf)) {
1647
31.8k
      continue;
1648
31.8k
    }
1649
33.9k
    if (unit == ifp->if_unit) {
1650
32.3k
      break;
1651
32.3k
    }
1652
33.9k
  }
1653
1654
  /* if called from ifunit_ref() and ifnet is not attached, bail */
1655
33.0k
  if (hold && ifp != NULL && !ifnet_is_attached(ifp, 1)) {
1656
0
    ifp = NULL;
1657
0
  }
1658
1659
33.0k
  ifnet_head_done();
1660
33.0k
  return ifp;
1661
86.2k
}
1662
1663
struct ifnet *
1664
ifunit(const char *name)
1665
59.8k
{
1666
59.8k
  return ifunit_common(name, FALSE);
1667
59.8k
}
1668
1669
/*
1670
 * Similar to ifunit(), except that we hold an I/O reference count on an
1671
 * attached interface, which must later be released via ifnet_decr_iorefcnt().
1672
 * Will return NULL unless interface exists and is fully attached.
1673
 */
1674
struct ifnet *
1675
ifunit_ref(const char *name)
1676
26.4k
{
1677
26.4k
  return ifunit_common(name, TRUE);
1678
26.4k
}
1679
1680
/*
1681
 * Map interface name in a sockaddr_dl to
1682
 * interface structure pointer.
1683
 */
1684
struct ifnet *
1685
if_withname(struct sockaddr *sa)
1686
0
{
1687
0
  char ifname[IFNAMSIZ + 1];
1688
0
  struct sockaddr_dl *sdl = (struct sockaddr_dl *)(void *)sa;
1689
1690
0
  if ((sa->sa_family != AF_LINK) || (sdl->sdl_nlen == 0) ||
1691
0
      (sdl->sdl_nlen > IFNAMSIZ)) {
1692
0
    return NULL;
1693
0
  }
1694
1695
  /*
1696
   * ifunit wants a null-terminated name.  It may not be null-terminated
1697
   * in the sockaddr.  We don't want to change the caller's sockaddr,
1698
   * and there might not be room to put the trailing null anyway, so we
1699
   * make a local copy that we know we can null terminate safely.
1700
   */
1701
1702
0
  bcopy(sdl->sdl_data, ifname, sdl->sdl_nlen);
1703
0
  ifname[sdl->sdl_nlen] = '\0';
1704
0
  return ifunit(ifname);
1705
0
}
1706
1707
static __attribute__((noinline)) int
1708
ifioctl_ifconf(u_long cmd, caddr_t data)
1709
2.83k
{
1710
2.83k
  int error = 0;
1711
1712
2.83k
  switch (cmd) {
1713
695
  case OSIOCGIFCONF32:                    /* struct ifconf32 */
1714
1.68k
  case SIOCGIFCONF32: {                   /* struct ifconf32 */
1715
1.68k
    struct ifconf32 ifc;
1716
1.68k
    bcopy(data, &ifc, sizeof(ifc));
1717
1.68k
    error = ifconf(cmd, CAST_USER_ADDR_T(ifc.ifc_req),
1718
1.68k
        &ifc.ifc_len);
1719
1.68k
    bcopy(&ifc, data, sizeof(ifc));
1720
1.68k
    break;
1721
695
  }
1722
1723
973
  case SIOCGIFCONF64:                     /* struct ifconf64 */
1724
1.14k
  case OSIOCGIFCONF64: {                  /* struct ifconf64 */
1725
1.14k
    struct ifconf64 ifc;
1726
1.14k
    bcopy(data, &ifc, sizeof(ifc));
1727
1.14k
    error = ifconf(cmd, ifc.ifc_req, &ifc.ifc_len);
1728
1.14k
    bcopy(&ifc, data, sizeof(ifc));
1729
1.14k
    break;
1730
973
  }
1731
1732
0
  default:
1733
0
    VERIFY(0);
1734
    /* NOTREACHED */
1735
2.83k
  }
1736
1737
2.83k
  return error;
1738
2.83k
}
1739
1740
static __attribute__((noinline)) int
1741
ifioctl_ifclone(u_long cmd, caddr_t data)
1742
1.44k
{
1743
1.44k
  int error = 0;
1744
1745
1.44k
  switch (cmd) {
1746
480
  case SIOCIFGCLONERS32: {                /* struct if_clonereq32 */
1747
480
    struct if_clonereq32 ifcr;
1748
480
    bcopy(data, &ifcr, sizeof(ifcr));
1749
480
    error = if_clone_list(ifcr.ifcr_count, &ifcr.ifcr_total,
1750
480
        CAST_USER_ADDR_T(ifcr.ifcru_buffer));
1751
480
    bcopy(&ifcr, data, sizeof(ifcr));
1752
480
    break;
1753
0
  }
1754
1755
968
  case SIOCIFGCLONERS64: {                /* struct if_clonereq64 */
1756
968
    struct if_clonereq64 ifcr;
1757
968
    bcopy(data, &ifcr, sizeof(ifcr));
1758
968
    error = if_clone_list(ifcr.ifcr_count, &ifcr.ifcr_total,
1759
968
        ifcr.ifcru_buffer);
1760
968
    bcopy(&ifcr, data, sizeof(ifcr));
1761
968
    break;
1762
0
  }
1763
1764
0
  default:
1765
0
    VERIFY(0);
1766
    /* NOTREACHED */
1767
1.44k
  }
1768
1769
1.44k
  return error;
1770
1.44k
}
1771
1772
static __attribute__((noinline)) int
1773
ifioctl_ifdesc(struct ifnet *ifp, u_long cmd, caddr_t data, struct proc *p)
1774
0
{
1775
0
  struct if_descreq *ifdr = (struct if_descreq *)(void *)data;
1776
0
  u_int32_t ifdr_len;
1777
0
  int error = 0;
1778
1779
0
  VERIFY(ifp != NULL);
1780
1781
0
  switch (cmd) {
1782
0
  case SIOCSIFDESC: {                     /* struct if_descreq */
1783
0
    if ((error = proc_suser(p)) != 0) {
1784
0
      break;
1785
0
    }
1786
1787
0
    ifnet_lock_exclusive(ifp);
1788
0
    bcopy(&ifdr->ifdr_len, &ifdr_len, sizeof(ifdr_len));
1789
0
    if (ifdr_len > sizeof(ifdr->ifdr_desc) ||
1790
0
        ifdr_len > ifp->if_desc.ifd_maxlen) {
1791
0
      error = EINVAL;
1792
0
      ifnet_lock_done(ifp);
1793
0
      break;
1794
0
    }
1795
1796
0
    bzero(ifp->if_desc.ifd_desc, ifp->if_desc.ifd_maxlen);
1797
0
    if ((ifp->if_desc.ifd_len = ifdr_len) > 0) {
1798
0
      bcopy(ifdr->ifdr_desc, ifp->if_desc.ifd_desc,
1799
0
          MIN(ifdr_len, ifp->if_desc.ifd_maxlen));
1800
0
    }
1801
0
    ifnet_lock_done(ifp);
1802
0
    break;
1803
0
  }
1804
1805
0
  case SIOCGIFDESC: {                     /* struct if_descreq */
1806
0
    ifnet_lock_shared(ifp);
1807
0
    ifdr_len = MIN(ifp->if_desc.ifd_len, sizeof(ifdr->ifdr_desc));
1808
0
    bcopy(&ifdr_len, &ifdr->ifdr_len, sizeof(ifdr_len));
1809
0
    bzero(&ifdr->ifdr_desc, sizeof(ifdr->ifdr_desc));
1810
0
    if (ifdr_len > 0) {
1811
0
      bcopy(ifp->if_desc.ifd_desc, ifdr->ifdr_desc, ifdr_len);
1812
0
    }
1813
0
    ifnet_lock_done(ifp);
1814
0
    break;
1815
0
  }
1816
1817
0
  default:
1818
0
    VERIFY(0);
1819
    /* NOTREACHED */
1820
0
  }
1821
1822
0
  return error;
1823
0
}
1824
1825
static __attribute__((noinline)) int
1826
ifioctl_linkparams(struct ifnet *ifp, u_long cmd, caddr_t data, struct proc *p)
1827
0
{
1828
0
  struct if_linkparamsreq *iflpr =
1829
0
      (struct if_linkparamsreq *)(void *)data;
1830
0
  struct ifclassq *ifq;
1831
0
  int error = 0;
1832
1833
0
  VERIFY(ifp != NULL);
1834
0
  ifq = &ifp->if_snd;
1835
1836
0
  switch (cmd) {
1837
0
  case SIOCSIFLINKPARAMS: {               /* struct if_linkparamsreq */
1838
0
    struct tb_profile tb = { .rate = 0, .percent = 0, .depth = 0 };
1839
1840
0
    if ((error = proc_suser(p)) != 0) {
1841
0
      break;
1842
0
    }
1843
1844
1845
0
    char netem_name[32];
1846
0
    (void) snprintf(netem_name, sizeof(netem_name),
1847
0
        "if_output_netem_%s", if_name(ifp));
1848
0
    error = netem_config(&ifp->if_output_netem, netem_name,
1849
0
        &iflpr->iflpr_output_netem, (void *)ifp,
1850
0
        ifnet_enqueue_netem, NETEM_MAX_BATCH_SIZE);
1851
0
    if (error != 0) {
1852
0
      break;
1853
0
    }
1854
1855
0
    IFCQ_LOCK(ifq);
1856
0
    if (!IFCQ_IS_READY(ifq)) {
1857
0
      error = ENXIO;
1858
0
      IFCQ_UNLOCK(ifq);
1859
0
      break;
1860
0
    }
1861
0
    bcopy(&iflpr->iflpr_output_tbr_rate, &tb.rate,
1862
0
        sizeof(tb.rate));
1863
0
    bcopy(&iflpr->iflpr_output_tbr_percent, &tb.percent,
1864
0
        sizeof(tb.percent));
1865
0
    error = ifclassq_tbr_set(ifq, &tb, TRUE);
1866
0
    IFCQ_UNLOCK(ifq);
1867
0
    break;
1868
0
  }
1869
1870
0
  case SIOCGIFLINKPARAMS: {               /* struct if_linkparamsreq */
1871
0
    u_int32_t sched_type = PKTSCHEDT_NONE, flags = 0;
1872
0
    u_int64_t tbr_bw = 0, tbr_pct = 0;
1873
1874
0
    IFCQ_LOCK(ifq);
1875
1876
0
    if (IFCQ_IS_ENABLED(ifq)) {
1877
0
      sched_type = ifq->ifcq_type;
1878
0
    }
1879
1880
0
    bcopy(&sched_type, &iflpr->iflpr_output_sched,
1881
0
        sizeof(iflpr->iflpr_output_sched));
1882
1883
0
    if (IFCQ_TBR_IS_ENABLED(ifq)) {
1884
0
      tbr_bw = ifq->ifcq_tbr.tbr_rate_raw;
1885
0
      tbr_pct = ifq->ifcq_tbr.tbr_percent;
1886
0
    }
1887
0
    bcopy(&tbr_bw, &iflpr->iflpr_output_tbr_rate,
1888
0
        sizeof(iflpr->iflpr_output_tbr_rate));
1889
0
    bcopy(&tbr_pct, &iflpr->iflpr_output_tbr_percent,
1890
0
        sizeof(iflpr->iflpr_output_tbr_percent));
1891
0
    IFCQ_UNLOCK(ifq);
1892
1893
0
    if (ifp->if_output_sched_model ==
1894
0
        IFNET_SCHED_MODEL_DRIVER_MANAGED) {
1895
0
      flags |= IFLPRF_DRVMANAGED;
1896
0
    }
1897
0
    bcopy(&flags, &iflpr->iflpr_flags, sizeof(iflpr->iflpr_flags));
1898
0
    bcopy(&ifp->if_output_bw, &iflpr->iflpr_output_bw,
1899
0
        sizeof(iflpr->iflpr_output_bw));
1900
0
    bcopy(&ifp->if_input_bw, &iflpr->iflpr_input_bw,
1901
0
        sizeof(iflpr->iflpr_input_bw));
1902
0
    bcopy(&ifp->if_output_lt, &iflpr->iflpr_output_lt,
1903
0
        sizeof(iflpr->iflpr_output_lt));
1904
0
    bcopy(&ifp->if_input_lt, &iflpr->iflpr_input_lt,
1905
0
        sizeof(iflpr->iflpr_input_lt));
1906
1907
0
    if (ifp->if_output_netem != NULL) {
1908
0
      netem_get_params(ifp->if_output_netem,
1909
0
          &iflpr->iflpr_output_netem);
1910
0
    }
1911
1912
0
    break;
1913
0
  }
1914
1915
0
  default:
1916
0
    VERIFY(0);
1917
    /* NOTREACHED */
1918
0
  }
1919
1920
0
  return error;
1921
0
}
1922
1923
static __attribute__((noinline)) int
1924
ifioctl_qstats(struct ifnet *ifp, u_long cmd, caddr_t data)
1925
0
{
1926
0
  struct if_qstatsreq *ifqr = (struct if_qstatsreq *)(void *)data;
1927
0
  u_int32_t ifqr_len, ifqr_slot;
1928
0
  int error = 0;
1929
1930
0
  VERIFY(ifp != NULL);
1931
1932
0
  switch (cmd) {
1933
0
  case SIOCGIFQUEUESTATS: {               /* struct if_qstatsreq */
1934
0
    bcopy(&ifqr->ifqr_slot, &ifqr_slot, sizeof(ifqr_slot));
1935
0
    bcopy(&ifqr->ifqr_len, &ifqr_len, sizeof(ifqr_len));
1936
0
    error = ifclassq_getqstats(&ifp->if_snd, ifqr_slot,
1937
0
        ifqr->ifqr_buf, &ifqr_len);
1938
0
    if (error != 0) {
1939
0
      ifqr_len = 0;
1940
0
    }
1941
0
    bcopy(&ifqr_len, &ifqr->ifqr_len, sizeof(ifqr_len));
1942
0
    break;
1943
0
  }
1944
1945
0
  default:
1946
0
    VERIFY(0);
1947
    /* NOTREACHED */
1948
0
  }
1949
1950
0
  return error;
1951
0
}
1952
1953
static __attribute__((noinline)) int
1954
ifioctl_throttle(struct ifnet *ifp, u_long cmd, caddr_t data, struct proc *p)
1955
0
{
1956
0
  struct if_throttlereq *ifthr = (struct if_throttlereq *)(void *)data;
1957
0
  u_int32_t ifthr_level;
1958
0
  int error = 0;
1959
1960
0
  VERIFY(ifp != NULL);
1961
1962
0
  switch (cmd) {
1963
0
  case SIOCSIFTHROTTLE: {                 /* struct if_throttlereq */
1964
    /*
1965
     * XXX: Use priv_check_cred() instead of root check?
1966
     */
1967
0
    if ((error = proc_suser(p)) != 0) {
1968
0
      break;
1969
0
    }
1970
1971
0
    bcopy(&ifthr->ifthr_level, &ifthr_level, sizeof(ifthr_level));
1972
0
    error = ifnet_set_throttle(ifp, ifthr_level);
1973
0
    if (error == EALREADY) {
1974
0
      error = 0;
1975
0
    }
1976
0
    break;
1977
0
  }
1978
1979
0
  case SIOCGIFTHROTTLE: {                 /* struct if_throttlereq */
1980
0
    if ((error = ifnet_get_throttle(ifp, &ifthr_level)) == 0) {
1981
0
      bcopy(&ifthr_level, &ifthr->ifthr_level,
1982
0
          sizeof(ifthr_level));
1983
0
    }
1984
0
    break;
1985
0
  }
1986
1987
0
  default:
1988
0
    VERIFY(0);
1989
    /* NOTREACHED */
1990
0
  }
1991
1992
0
  return error;
1993
0
}
1994
1995
static int
1996
ifioctl_getnetagents(struct ifnet *ifp, u_int32_t *count, user_addr_t uuid_p)
1997
2
{
1998
2
  int error = 0;
1999
2
  u_int32_t index = 0;
2000
2
  u_int32_t valid_netagent_count = 0;
2001
2
  *count = 0;
2002
2003
2
  ifnet_lock_assert(ifp, IFNET_LCK_ASSERT_SHARED);
2004
2005
2
  if (ifp->if_agentids != NULL) {
2006
0
    for (index = 0; index < ifp->if_agentcount; index++) {
2007
0
      uuid_t *netagent_uuid = &(ifp->if_agentids[index]);
2008
0
      if (!uuid_is_null(*netagent_uuid)) {
2009
0
        if (uuid_p != USER_ADDR_NULL) {
2010
0
          error = copyout(netagent_uuid,
2011
0
              uuid_p + sizeof(uuid_t) * valid_netagent_count,
2012
0
              sizeof(uuid_t));
2013
0
          if (error != 0) {
2014
0
            return error;
2015
0
          }
2016
0
        }
2017
0
        valid_netagent_count++;
2018
0
      }
2019
0
    }
2020
0
  }
2021
2
  *count = valid_netagent_count;
2022
2023
2
  return 0;
2024
2
}
2025
2026
1
#define IF_MAXAGENTS            64
2027
#define IF_AGENT_INCREMENT      8
2028
int
2029
if_add_netagent_locked(struct ifnet *ifp, uuid_t new_agent_uuid)
2030
3
{
2031
3
  VERIFY(ifp != NULL);
2032
2033
3
  uuid_t *first_empty_slot = NULL;
2034
3
  u_int32_t index = 0;
2035
3
  bool already_added = FALSE;
2036
2037
3
  if (ifp->if_agentids != NULL) {
2038
2
    for (index = 0; index < ifp->if_agentcount; index++) {
2039
2
      uuid_t *netagent_uuid = &(ifp->if_agentids[index]);
2040
2
      if (uuid_compare(*netagent_uuid, new_agent_uuid) == 0) {
2041
        /* Already present, ignore */
2042
2
        already_added = TRUE;
2043
2
        break;
2044
2
      }
2045
0
      if (first_empty_slot == NULL &&
2046
0
          uuid_is_null(*netagent_uuid)) {
2047
0
        first_empty_slot = netagent_uuid;
2048
0
      }
2049
0
    }
2050
2
  }
2051
3
  if (already_added) {
2052
    /* Already added agent, don't return an error */
2053
2
    return 0;
2054
2
  }
2055
1
  if (first_empty_slot == NULL) {
2056
1
    if (ifp->if_agentcount >= IF_MAXAGENTS) {
2057
      /* No room for another netagent UUID, bail */
2058
0
      return ENOMEM;
2059
1
    } else {
2060
      /* Calculate new array size */
2061
1
      u_int32_t new_agent_count =
2062
1
          MIN(ifp->if_agentcount + IF_AGENT_INCREMENT,
2063
1
          IF_MAXAGENTS);
2064
2065
      /* Reallocate array */
2066
1
      uuid_t *new_agent_array = _REALLOC(ifp->if_agentids,
2067
1
          sizeof(uuid_t) * new_agent_count, M_NETAGENT,
2068
1
          M_WAITOK | M_ZERO);
2069
1
      if (new_agent_array == NULL) {
2070
0
        return ENOMEM;
2071
0
      }
2072
2073
      /* Save new array */
2074
1
      ifp->if_agentids = new_agent_array;
2075
2076
      /* Set first empty slot */
2077
1
      first_empty_slot =
2078
1
          &(ifp->if_agentids[ifp->if_agentcount]);
2079
2080
      /* Save new array length */
2081
1
      ifp->if_agentcount = new_agent_count;
2082
1
    }
2083
1
  }
2084
1
  uuid_copy(*first_empty_slot, new_agent_uuid);
2085
1
  netagent_post_updated_interfaces(new_agent_uuid);
2086
1
  return 0;
2087
1
}
2088
2089
int
2090
if_add_netagent(struct ifnet *ifp, uuid_t new_agent_uuid)
2091
0
{
2092
0
  VERIFY(ifp != NULL);
2093
2094
0
  ifnet_lock_exclusive(ifp);
2095
2096
0
  int error = if_add_netagent_locked(ifp, new_agent_uuid);
2097
2098
0
  ifnet_lock_done(ifp);
2099
2100
0
  return error;
2101
0
}
2102
2103
static int
2104
if_delete_netagent_locked(struct ifnet *ifp, uuid_t remove_agent_uuid)
2105
0
{
2106
0
  u_int32_t index = 0;
2107
0
  bool removed_agent_id = FALSE;
2108
2109
0
  if (ifp->if_agentids != NULL) {
2110
0
    for (index = 0; index < ifp->if_agentcount; index++) {
2111
0
      uuid_t *netagent_uuid = &(ifp->if_agentids[index]);
2112
0
      if (uuid_compare(*netagent_uuid,
2113
0
          remove_agent_uuid) == 0) {
2114
0
        uuid_clear(*netagent_uuid);
2115
0
        removed_agent_id = TRUE;
2116
0
        break;
2117
0
      }
2118
0
    }
2119
0
  }
2120
0
  if (removed_agent_id) {
2121
0
    netagent_post_updated_interfaces(remove_agent_uuid);
2122
0
  }
2123
2124
0
  return 0;
2125
0
}
2126
2127
int
2128
if_delete_netagent(struct ifnet *ifp, uuid_t remove_agent_uuid)
2129
0
{
2130
0
  VERIFY(ifp != NULL);
2131
2132
0
  ifnet_lock_exclusive(ifp);
2133
2134
0
  int error = if_delete_netagent_locked(ifp, remove_agent_uuid);
2135
2136
0
  ifnet_lock_done(ifp);
2137
2138
0
  return error;
2139
0
}
2140
2141
boolean_t
2142
if_check_netagent(struct ifnet *ifp, uuid_t find_agent_uuid)
2143
0
{
2144
0
  boolean_t found = FALSE;
2145
2146
0
  if (!ifp || uuid_is_null(find_agent_uuid)) {
2147
0
    return FALSE;
2148
0
  }
2149
2150
0
  ifnet_lock_shared(ifp);
2151
2152
0
  if (ifp->if_agentids != NULL) {
2153
0
    for (uint32_t index = 0; index < ifp->if_agentcount; index++) {
2154
0
      if (uuid_compare(ifp->if_agentids[index], find_agent_uuid) == 0) {
2155
0
        found = TRUE;
2156
0
        break;
2157
0
      }
2158
0
    }
2159
0
  }
2160
2161
0
  ifnet_lock_done(ifp);
2162
2163
0
  return found;
2164
0
}
2165
2166
static __attribute__((noinline)) int
2167
ifioctl_netagent(struct ifnet *ifp, u_long cmd, caddr_t data, struct proc *p)
2168
5
{
2169
5
  struct if_agentidreq *ifar = (struct if_agentidreq *)(void *)data;
2170
5
  union {
2171
5
    struct if_agentidsreq32 s32;
2172
5
    struct if_agentidsreq64 s64;
2173
5
  } u;
2174
5
  int error = 0;
2175
2176
5
  VERIFY(ifp != NULL);
2177
2178
  /* Get an io ref count if the interface is attached */
2179
5
  if (!ifnet_is_attached(ifp, 1)) {
2180
0
    return EOPNOTSUPP;
2181
0
  }
2182
2183
5
  if (cmd == SIOCAIFAGENTID ||
2184
3
      cmd == SIOCDIFAGENTID) {
2185
3
    ifnet_lock_exclusive(ifp);
2186
3
  } else {
2187
2
    ifnet_lock_shared(ifp);
2188
2
  }
2189
2190
5
  switch (cmd) {
2191
3
  case SIOCAIFAGENTID: {                  /* struct if_agentidreq */
2192
    // TODO: Use priv_check_cred() instead of root check
2193
3
    if ((error = proc_suser(p)) != 0) {
2194
0
      break;
2195
0
    }
2196
3
    error = if_add_netagent_locked(ifp, ifar->ifar_uuid);
2197
3
    break;
2198
3
  }
2199
0
  case SIOCDIFAGENTID: {                          /* struct if_agentidreq */
2200
    // TODO: Use priv_check_cred() instead of root check
2201
0
    if ((error = proc_suser(p)) != 0) {
2202
0
      break;
2203
0
    }
2204
0
    error = if_delete_netagent_locked(ifp, ifar->ifar_uuid);
2205
0
    break;
2206
0
  }
2207
0
  case SIOCGIFAGENTIDS32: {               /* struct if_agentidsreq32 */
2208
0
    bcopy(data, &u.s32, sizeof(u.s32));
2209
0
    error = ifioctl_getnetagents(ifp, &u.s32.ifar_count,
2210
0
        u.s32.ifar_uuids);
2211
0
    if (error == 0) {
2212
0
      bcopy(&u.s32, data, sizeof(u.s32));
2213
0
    }
2214
0
    break;
2215
0
  }
2216
2
  case SIOCGIFAGENTIDS64: {               /* struct if_agentidsreq64 */
2217
2
    bcopy(data, &u.s64, sizeof(u.s64));
2218
2
    error = ifioctl_getnetagents(ifp, &u.s64.ifar_count,
2219
2
        u.s64.ifar_uuids);
2220
2
    if (error == 0) {
2221
2
      bcopy(&u.s64, data, sizeof(u.s64));
2222
2
    }
2223
2
    break;
2224
0
  }
2225
0
  default:
2226
0
    VERIFY(0);
2227
    /* NOTREACHED */
2228
5
  }
2229
2230
5
  ifnet_lock_done(ifp);
2231
5
  ifnet_decr_iorefcnt(ifp);
2232
2233
5
  return error;
2234
5
}
2235
2236
void
2237
ifnet_clear_netagent(uuid_t netagent_uuid)
2238
0
{
2239
0
  struct ifnet *ifp = NULL;
2240
0
  u_int32_t index = 0;
2241
2242
0
  ifnet_head_lock_shared();
2243
2244
0
  TAILQ_FOREACH(ifp, &ifnet_head, if_link) {
2245
0
    ifnet_lock_shared(ifp);
2246
0
    if (ifp->if_agentids != NULL) {
2247
0
      for (index = 0; index < ifp->if_agentcount; index++) {
2248
0
        uuid_t *ifp_netagent_uuid = &(ifp->if_agentids[index]);
2249
0
        if (uuid_compare(*ifp_netagent_uuid, netagent_uuid) == 0) {
2250
0
          uuid_clear(*ifp_netagent_uuid);
2251
0
        }
2252
0
      }
2253
0
    }
2254
0
    ifnet_lock_done(ifp);
2255
0
  }
2256
2257
0
  ifnet_head_done();
2258
0
}
2259
2260
void
2261
ifnet_increment_generation(ifnet_t interface)
2262
28.1k
{
2263
28.1k
  OSIncrementAtomic(&interface->if_generation);
2264
28.1k
}
2265
2266
u_int32_t
2267
ifnet_get_generation(ifnet_t interface)
2268
0
{
2269
0
  return interface->if_generation;
2270
0
}
2271
2272
void
2273
ifnet_remove_from_ordered_list(struct ifnet *ifp)
2274
0
{
2275
0
  ifnet_head_assert_exclusive();
2276
2277
  // Remove from list
2278
0
  TAILQ_REMOVE(&ifnet_ordered_head, ifp, if_ordered_link);
2279
0
  ifp->if_ordered_link.tqe_next = NULL;
2280
0
  ifp->if_ordered_link.tqe_prev = NULL;
2281
2282
  // Update ordered count
2283
0
  VERIFY(if_ordered_count > 0);
2284
0
  if_ordered_count--;
2285
0
}
2286
2287
static int
2288
ifnet_reset_order(u_int32_t *ordered_indices, u_int32_t count)
2289
397
{
2290
397
  struct ifnet *ifp = NULL;
2291
397
  int error = 0;
2292
2293
397
  ifnet_head_lock_exclusive();
2294
397
  for (u_int32_t order_index = 0; order_index < count; order_index++) {
2295
9
    if (ordered_indices[order_index] == IFSCOPE_NONE ||
2296
9
        ordered_indices[order_index] > (uint32_t)if_index) {
2297
9
      error = EINVAL;
2298
9
      ifnet_head_done();
2299
9
      return error;
2300
9
    }
2301
9
  }
2302
  // Flush current ordered list
2303
388
  for (ifp = TAILQ_FIRST(&ifnet_ordered_head); ifp != NULL;
2304
388
      ifp = TAILQ_FIRST(&ifnet_ordered_head)) {
2305
0
    ifnet_lock_exclusive(ifp);
2306
0
    ifnet_remove_from_ordered_list(ifp);
2307
0
    ifnet_lock_done(ifp);
2308
0
  }
2309
2310
388
  VERIFY(if_ordered_count == 0);
2311
2312
388
  for (u_int32_t order_index = 0; order_index < count; order_index++) {
2313
0
    u_int32_t interface_index = ordered_indices[order_index];
2314
0
    ifp = ifindex2ifnet[interface_index];
2315
0
    if (ifp == NULL) {
2316
0
      continue;
2317
0
    }
2318
0
    ifnet_lock_exclusive(ifp);
2319
0
    TAILQ_INSERT_TAIL(&ifnet_ordered_head, ifp, if_ordered_link);
2320
0
    ifnet_lock_done(ifp);
2321
0
    if_ordered_count++;
2322
0
  }
2323
2324
388
  ifnet_head_done();
2325
2326
388
  necp_update_all_clients();
2327
2328
388
  return error;
2329
397
}
2330
2331
int
2332
if_set_qosmarking_mode(struct ifnet *ifp, u_int32_t mode)
2333
1
{
2334
1
  int error = 0;
2335
1
  u_int32_t old_mode = ifp->if_qosmarking_mode;
2336
2337
1
  switch (mode) {
2338
0
  case IFRTYPE_QOSMARKING_MODE_NONE:
2339
0
    ifp->if_qosmarking_mode = IFRTYPE_QOSMARKING_MODE_NONE;
2340
0
    break;
2341
0
  case IFRTYPE_QOSMARKING_FASTLANE:
2342
0
  case IFRTYPE_QOSMARKING_RFC4594:
2343
0
    ifp->if_qosmarking_mode = mode;
2344
0
    break;
2345
1
  default:
2346
1
    error = EINVAL;
2347
1
    break;
2348
1
  }
2349
1
  if (error == 0 && old_mode != ifp->if_qosmarking_mode) {
2350
0
    dlil_post_msg(ifp, KEV_DL_SUBCLASS, KEV_DL_QOS_MODE_CHANGED,
2351
0
        NULL, 0);
2352
0
  }
2353
1
  return error;
2354
1
}
2355
2356
static __attribute__((noinline)) int
2357
ifioctl_iforder(u_long cmd, caddr_t data)
2358
924
{
2359
924
  int error = 0;
2360
924
  u_int32_t *ordered_indices = NULL;
2361
924
  if (data == NULL) {
2362
0
    return EINVAL;
2363
0
  }
2364
2365
924
  switch (cmd) {
2366
924
  case SIOCSIFORDER: {            /* struct if_order */
2367
924
    struct if_order *ifo = (struct if_order *)(void *)data;
2368
2369
924
    if (ifo->ifo_count > (u_int32_t)if_index) {
2370
527
      error = EINVAL;
2371
527
      break;
2372
527
    }
2373
2374
397
    size_t length = (ifo->ifo_count * sizeof(u_int32_t));
2375
397
    if (length > 0) {
2376
9
      if (ifo->ifo_ordered_indices == USER_ADDR_NULL) {
2377
0
        error = EINVAL;
2378
0
        break;
2379
0
      }
2380
9
      ordered_indices = _MALLOC(length, M_NECP, M_WAITOK);
2381
9
      if (ordered_indices == NULL) {
2382
0
        error = ENOMEM;
2383
0
        break;
2384
0
      }
2385
2386
      // nedwill: Copy in random fuzzed data.
2387
9
      error = copyin((user_addr_t)1,
2388
9
          ordered_indices, length);
2389
9
      if (error != 0) {
2390
0
        break;
2391
0
      }
2392
2393
      /* ordered_indices should not contain duplicates */
2394
9
      bool found_duplicate = FALSE;
2395
25
      for (uint32_t i = 0; i < (ifo->ifo_count - 1) && !found_duplicate; i++) {
2396
68
        for (uint32_t j = i + 1; j < ifo->ifo_count && !found_duplicate; j++) {
2397
52
          if (ordered_indices[j] == ordered_indices[i]) {
2398
0
            error = EINVAL;
2399
0
            found_duplicate = TRUE;
2400
0
            break;
2401
0
          }
2402
52
        }
2403
16
      }
2404
9
      if (found_duplicate) {
2405
0
        break;
2406
0
      }
2407
2408
9
      error = ifnet_reset_order(ordered_indices, ifo->ifo_count);
2409
388
    } else {
2410
      // Clear the list
2411
388
      error = ifnet_reset_order(NULL, 0);
2412
388
    }
2413
397
    break;
2414
397
  }
2415
2416
397
  default: {
2417
0
    VERIFY(0);
2418
    /* NOTREACHED */
2419
0
  }
2420
924
  }
2421
2422
924
  if (ordered_indices != NULL) {
2423
9
    _FREE(ordered_indices, M_NECP);
2424
9
  }
2425
2426
924
  return error;
2427
924
}
2428
2429
static __attribute__((noinline)) int
2430
ifioctl_networkid(struct ifnet *ifp, caddr_t data)
2431
0
{
2432
0
  struct if_netidreq *ifnetidr = (struct if_netidreq *)(void *)data;
2433
0
  int error = 0;
2434
0
  int len = ifnetidr->ifnetid_len;
2435
2436
0
  VERIFY(ifp != NULL);
2437
2438
0
  if (len > sizeof(ifnetidr->ifnetid)) {
2439
0
    error = EINVAL;
2440
0
    goto end;
2441
0
  }
2442
2443
0
  if (len == 0) {
2444
0
    bzero(&ifp->network_id, sizeof(ifp->network_id));
2445
0
  } else if (len > sizeof(ifp->network_id)) {
2446
0
    error = EINVAL;
2447
0
    goto end;
2448
0
  }
2449
2450
0
  ifp->network_id_len = len;
2451
0
  bcopy(data, ifp->network_id, len);
2452
0
end:
2453
0
  return error;
2454
0
}
2455
2456
static __attribute__((noinline)) int
2457
ifioctl_netsignature(struct ifnet *ifp, u_long cmd, caddr_t data)
2458
0
{
2459
0
  struct if_nsreq *ifnsr = (struct if_nsreq *)(void *)data;
2460
0
  u_int16_t flags;
2461
0
  int error = 0;
2462
2463
0
  VERIFY(ifp != NULL);
2464
2465
0
  switch (cmd) {
2466
0
  case SIOCSIFNETSIGNATURE:               /* struct if_nsreq */
2467
0
    if (ifnsr->ifnsr_len > sizeof(ifnsr->ifnsr_data)) {
2468
0
      error = EINVAL;
2469
0
      break;
2470
0
    }
2471
0
    bcopy(&ifnsr->ifnsr_flags, &flags, sizeof(flags));
2472
0
    error = ifnet_set_netsignature(ifp, ifnsr->ifnsr_family,
2473
0
        ifnsr->ifnsr_len, flags, ifnsr->ifnsr_data);
2474
0
    break;
2475
2476
0
  case SIOCGIFNETSIGNATURE:               /* struct if_nsreq */
2477
0
    ifnsr->ifnsr_len = sizeof(ifnsr->ifnsr_data);
2478
0
    error = ifnet_get_netsignature(ifp, ifnsr->ifnsr_family,
2479
0
        &ifnsr->ifnsr_len, &flags, ifnsr->ifnsr_data);
2480
0
    if (error == 0) {
2481
0
      bcopy(&flags, &ifnsr->ifnsr_flags, sizeof(flags));
2482
0
    } else {
2483
0
      ifnsr->ifnsr_len = 0;
2484
0
    }
2485
0
    break;
2486
2487
0
  default:
2488
0
    VERIFY(0);
2489
    /* NOTREACHED */
2490
0
  }
2491
2492
0
  return error;
2493
0
}
2494
2495
static __attribute__((noinline)) int
2496
ifioctl_nat64prefix(struct ifnet *ifp, u_long cmd, caddr_t data)
2497
2
{
2498
2
  struct if_nat64req *ifnat64 = (struct if_nat64req *)(void *)data;
2499
2
  int error = 0;
2500
2501
2
  VERIFY(ifp != NULL);
2502
2503
0
  switch (cmd) {
2504
1
  case SIOCSIFNAT64PREFIX:                /* struct if_nat64req */
2505
1
    error = ifnet_set_nat64prefix(ifp, ifnat64->ifnat64_prefixes);
2506
1
    if (error != 0) {
2507
1
      ip6stat.ip6s_clat464_plat64_pfx_setfail++;
2508
1
    }
2509
1
    break;
2510
2511
1
  case SIOCGIFNAT64PREFIX:                /* struct if_nat64req */
2512
1
    error = ifnet_get_nat64prefix(ifp, ifnat64->ifnat64_prefixes);
2513
1
    if (error != 0) {
2514
1
      ip6stat.ip6s_clat464_plat64_pfx_getfail++;
2515
1
    }
2516
1
    break;
2517
2518
0
  default:
2519
0
    VERIFY(0);
2520
    /* NOTREACHED */
2521
2
  }
2522
2523
2
  return error;
2524
2
}
2525
2526
static __attribute__((noinline)) int
2527
ifioctl_clat46addr(struct ifnet *ifp, u_long cmd, caddr_t data)
2528
0
{
2529
0
  struct if_clat46req *ifclat46 = (struct if_clat46req *)(void *)data;
2530
0
  struct in6_ifaddr *ia6_clat = NULL;
2531
0
  int error = 0;
2532
2533
0
  VERIFY(ifp != NULL);
2534
2535
0
  switch (cmd) {
2536
0
  case SIOCGIFCLAT46ADDR:
2537
0
    ia6_clat = in6ifa_ifpwithflag(ifp, IN6_IFF_CLAT46);
2538
0
    if (ia6_clat == NULL) {
2539
0
      error = ENOENT;
2540
0
      break;
2541
0
    }
2542
2543
0
    bcopy(&ia6_clat->ia_addr.sin6_addr, &ifclat46->ifclat46_addr.v6_address,
2544
0
        sizeof(ifclat46->ifclat46_addr.v6_address));
2545
0
    ifclat46->ifclat46_addr.v6_prefixlen = ia6_clat->ia_plen;
2546
0
    IFA_REMREF(&ia6_clat->ia_ifa);
2547
0
    break;
2548
0
  default:
2549
0
    VERIFY(0);
2550
    /* NOTREACHED */
2551
0
  }
2552
2553
0
  return error;
2554
0
}
2555
2556
2557
static int
2558
ifioctl_get_protolist(struct ifnet *ifp, u_int32_t * ret_count,
2559
    user_addr_t ifpl)
2560
0
{
2561
0
  u_int32_t       actual_count;
2562
0
  u_int32_t       count;
2563
0
  int             error = 0;
2564
0
  u_int32_t       *list = NULL;
2565
2566
  /* find out how many */
2567
0
  count = if_get_protolist(ifp, NULL, 0);
2568
0
  if (ifpl == USER_ADDR_NULL) {
2569
0
    goto done;
2570
0
  }
2571
2572
  /* copy out how many there's space for */
2573
0
  if (*ret_count < count) {
2574
0
    count = *ret_count;
2575
0
  }
2576
0
  if (count == 0) {
2577
0
    goto done;
2578
0
  }
2579
0
  list = _MALLOC(count * sizeof(*list), M_TEMP, M_WAITOK | M_ZERO);
2580
0
  if (list == NULL) {
2581
0
    error = ENOMEM;
2582
0
    goto done;
2583
0
  }
2584
0
  actual_count = if_get_protolist(ifp, list, count);
2585
0
  if (actual_count < count) {
2586
0
    count = actual_count;
2587
0
  }
2588
0
  if (count != 0) {
2589
0
    error = copyout((caddr_t)list, ifpl, count * sizeof(*list));
2590
0
  }
2591
2592
0
done:
2593
0
  if (list != NULL) {
2594
0
    if_free_protolist(list);
2595
0
  }
2596
0
  *ret_count = count;
2597
0
  return error;
2598
0
}
2599
2600
static __attribute__((noinline)) int
2601
ifioctl_protolist(struct ifnet *ifp, u_long cmd, caddr_t data)
2602
0
{
2603
0
  int error = 0;
2604
2605
0
  switch (cmd) {
2606
0
  case SIOCGIFPROTOLIST32: {              /* struct if_protolistreq32 */
2607
0
    struct if_protolistreq32        ifpl;
2608
2609
0
    bcopy(data, &ifpl, sizeof(ifpl));
2610
0
    if (ifpl.ifpl_reserved != 0) {
2611
0
      error = EINVAL;
2612
0
      break;
2613
0
    }
2614
0
    error = ifioctl_get_protolist(ifp, &ifpl.ifpl_count,
2615
0
        CAST_USER_ADDR_T(ifpl.ifpl_list));
2616
0
    bcopy(&ifpl, data, sizeof(ifpl));
2617
0
    break;
2618
0
  }
2619
0
  case SIOCGIFPROTOLIST64: {              /* struct if_protolistreq64 */
2620
0
    struct if_protolistreq64        ifpl;
2621
2622
0
    bcopy(data, &ifpl, sizeof(ifpl));
2623
0
    if (ifpl.ifpl_reserved != 0) {
2624
0
      error = EINVAL;
2625
0
      break;
2626
0
    }
2627
0
    error = ifioctl_get_protolist(ifp, &ifpl.ifpl_count,
2628
0
        ifpl.ifpl_list);
2629
0
    bcopy(&ifpl, data, sizeof(ifpl));
2630
0
    break;
2631
0
  }
2632
0
  default:
2633
0
    VERIFY(0);
2634
    /* NOTREACHED */
2635
0
  }
2636
2637
0
  return error;
2638
0
}
2639
2640
/*
2641
 * List the ioctl()s we can perform on restricted INTCOPROC interfaces.
2642
 */
2643
static bool
2644
ifioctl_restrict_intcoproc(unsigned long cmd, const char *ifname,
2645
    struct ifnet *ifp, struct proc *p)
2646
64.4k
{
2647
64.4k
  if (intcoproc_unrestricted == TRUE) {
2648
0
    return false;
2649
0
  }
2650
64.4k
  if (proc_pid(p) == 0) {
2651
64.4k
    return false;
2652
64.4k
  }
2653
0
  if (ifname) {
2654
0
    ifp = ifunit(ifname);
2655
0
  }
2656
0
  if (ifp == NULL) {
2657
0
    return false;
2658
0
  }
2659
0
  if (!IFNET_IS_INTCOPROC(ifp)) {
2660
0
    return false;
2661
0
  }
2662
0
  switch (cmd) {
2663
0
  case SIOCGIFBRDADDR:
2664
0
  case SIOCGIFCONF32:
2665
0
  case SIOCGIFCONF64:
2666
0
  case SIOCGIFFLAGS:
2667
0
  case SIOCGIFEFLAGS:
2668
0
  case SIOCGIFCAP:
2669
0
  case SIOCGIFMETRIC:
2670
0
  case SIOCGIFMTU:
2671
0
  case SIOCGIFPHYS:
2672
0
  case SIOCGIFTYPE:
2673
0
  case SIOCGIFFUNCTIONALTYPE:
2674
0
  case SIOCGIFPSRCADDR:
2675
0
  case SIOCGIFPDSTADDR:
2676
0
  case SIOCGIFGENERIC:
2677
0
  case SIOCGIFDEVMTU:
2678
0
  case SIOCGIFVLAN:
2679
0
  case SIOCGIFBOND:
2680
0
  case SIOCGIFWAKEFLAGS:
2681
0
  case SIOCGIFGETRTREFCNT:
2682
0
  case SIOCGIFOPPORTUNISTIC:
2683
0
  case SIOCGIFLINKQUALITYMETRIC:
2684
0
  case SIOCGIFLOG:
2685
0
  case SIOCGIFDELEGATE:
2686
0
  case SIOCGIFEXPENSIVE:
2687
0
  case SIOCGIFINTERFACESTATE:
2688
0
  case SIOCGIFPROBECONNECTIVITY:
2689
0
  case SIOCGIFTIMESTAMPENABLED:
2690
0
  case SIOCGECNMODE:
2691
0
  case SIOCGQOSMARKINGMODE:
2692
0
  case SIOCGQOSMARKINGENABLED:
2693
0
  case SIOCGIFLOWINTERNET:
2694
0
  case SIOCGIFSTATUS:
2695
0
  case SIOCGIFMEDIA32:
2696
0
  case SIOCGIFMEDIA64:
2697
0
  case SIOCGIFXMEDIA32:
2698
0
  case SIOCGIFXMEDIA64:
2699
0
  case SIOCGIFDESC:
2700
0
  case SIOCGIFLINKPARAMS:
2701
0
  case SIOCGIFQUEUESTATS:
2702
0
  case SIOCGIFTHROTTLE:
2703
0
  case SIOCGIFAGENTIDS32:
2704
0
  case SIOCGIFAGENTIDS64:
2705
0
  case SIOCGIFNETSIGNATURE:
2706
0
  case SIOCGIFINFO_IN6:
2707
0
  case SIOCGIFAFLAG_IN6:
2708
0
  case SIOCGNBRINFO_IN6:
2709
0
  case SIOCGIFALIFETIME_IN6:
2710
0
  case SIOCGIFNETMASK_IN6:
2711
0
  case SIOCGIFPROTOLIST32:
2712
0
  case SIOCGIFPROTOLIST64:
2713
0
  case SIOCGIFXFLAGS:
2714
0
    return false;
2715
0
  default:
2716
0
#if (DEBUG || DEVELOPMENT)
2717
0
    printf("%s: cmd 0x%lx not allowed (pid %u)\n",
2718
0
        __func__, cmd, proc_pid(p));
2719
0
#endif
2720
0
    return true;
2721
0
  }
2722
0
  return false;
2723
0
}
2724
2725
/*
2726
 * Given a media word, return one suitable for an application
2727
 * using the original encoding.
2728
 */
2729
static int
2730
compat_media(int media)
2731
0
{
2732
0
  if (IFM_TYPE(media) == IFM_ETHER && IFM_SUBTYPE(media) > IFM_OTHER) {
2733
0
    media &= ~IFM_TMASK;
2734
0
    media |= IFM_OTHER;
2735
0
  }
2736
0
  return media;
2737
0
}
2738
2739
static int
2740
compat_ifmu_ulist(struct ifnet *ifp, u_long cmd, void *data)
2741
0
{
2742
0
  struct ifmediareq *ifmr = (struct ifmediareq *)data;
2743
0
  user_addr_t user_addr;
2744
0
  int i;
2745
0
  int *media_list = NULL;
2746
0
  int error = 0;
2747
0
  bool list_modified = false;
2748
2749
0
  user_addr = (cmd == SIOCGIFMEDIA64) ?
2750
0
      ((struct ifmediareq64 *)ifmr)->ifmu_ulist :
2751
0
      CAST_USER_ADDR_T(((struct ifmediareq32 *)ifmr)->ifmu_ulist);
2752
0
  if (user_addr == USER_ADDR_NULL || ifmr->ifm_count == 0) {
2753
0
    return 0;
2754
0
  }
2755
0
  MALLOC(media_list, int *, ifmr->ifm_count * sizeof(int),
2756
0
      M_TEMP, M_WAITOK | M_ZERO);
2757
0
  if (media_list == NULL) {
2758
0
    os_log_error(OS_LOG_DEFAULT,
2759
0
        "%s: %s MALLOC() failed",
2760
0
        __func__, ifp->if_xname);
2761
0
    error = ENOMEM;
2762
0
    goto done;
2763
0
  }
2764
0
  error = copyin(user_addr, media_list, ifmr->ifm_count * sizeof(int));
2765
0
  if (error != 0) {
2766
0
    os_log_error(OS_LOG_DEFAULT,
2767
0
        "%s: %s copyin() error %d",
2768
0
        __func__, ifp->if_xname, error);
2769
0
    goto done;
2770
0
  }
2771
0
  for (i = 0; i < ifmr->ifm_count; i++) {
2772
0
    int old_media, new_media;
2773
2774
0
    old_media = media_list[i];
2775
2776
0
    new_media = compat_media(old_media);
2777
0
    if (new_media == old_media) {
2778
0
      continue;
2779
0
    }
2780
0
    if (if_verbose != 0) {
2781
0
      os_log_info(OS_LOG_DEFAULT,
2782
0
          "%s: %s converted extended media %08x to compat media %08x",
2783
0
          __func__, ifp->if_xname, old_media, new_media);
2784
0
    }
2785
0
    media_list[i] = new_media;
2786
0
    list_modified = true;
2787
0
  }
2788
0
  if (list_modified) {
2789
0
    error = copyout(media_list, user_addr, ifmr->ifm_count * sizeof(int));
2790
0
    if (error != 0) {
2791
0
      os_log_error(OS_LOG_DEFAULT,
2792
0
          "%s: %s copyout() error %d",
2793
0
          __func__, ifp->if_xname, error);
2794
0
      goto done;
2795
0
    }
2796
0
  }
2797
0
done:
2798
0
  if (media_list != NULL) {
2799
0
    FREE(media_list, M_TEMP);
2800
0
  }
2801
0
  return error;
2802
0
}
2803
2804
static int
2805
compat_ifmediareq(struct ifnet *ifp, u_long cmd, void *data)
2806
0
{
2807
0
  struct ifmediareq *ifmr = (struct ifmediareq *)data;
2808
0
  int error;
2809
2810
0
  ifmr->ifm_active = compat_media(ifmr->ifm_active);
2811
0
  ifmr->ifm_current = compat_media(ifmr->ifm_current);
2812
2813
0
  error = compat_ifmu_ulist(ifp, cmd, data);
2814
2815
0
  return error;
2816
0
}
2817
2818
static int
2819
ifioctl_get_media(struct ifnet *ifp, struct socket *so, u_long cmd, caddr_t data)
2820
1
{
2821
1
  int error = 0;
2822
2823
  /*
2824
   * An ifnet must not implement SIOCGIFXMEDIA as it gets the extended
2825
   *  media subtypes macros from <net/if_media.h>
2826
   */
2827
1
  switch (cmd) {
2828
1
  case SIOCGIFMEDIA32:
2829
1
  case SIOCGIFXMEDIA32:
2830
1
    error = ifnet_ioctl(ifp, SOCK_DOM(so), SIOCGIFMEDIA32, data);
2831
1
    break;
2832
0
  case SIOCGIFMEDIA64:
2833
0
  case SIOCGIFXMEDIA64:
2834
0
    error = ifnet_ioctl(ifp, SOCK_DOM(so), SIOCGIFMEDIA64, data);
2835
0
    break;
2836
1
  }
2837
1
  if (if_verbose != 0 && error != 0) {
2838
0
    os_log(OS_LOG_DEFAULT, "%s: first ifnet_ioctl(%s, %08lx) error %d",
2839
0
        __func__, ifp->if_xname, cmd, error);
2840
0
  }
2841
1
  if (error == 0 && (cmd == SIOCGIFMEDIA32 || cmd == SIOCGIFMEDIA64)) {
2842
0
    error = compat_ifmediareq(ifp, cmd, data);
2843
0
  }
2844
1
  return error;
2845
1
}
2846
/*
2847
 * Interface ioctls.
2848
 *
2849
 * Most of the routines called to handle the ioctls would end up being
2850
 * tail-call optimized, which unfortunately causes this routine to
2851
 * consume too much stack space; this is the reason for the "noinline"
2852
 * attribute used on those routines.
2853
 */
2854
int
2855
ifioctl(struct socket *so, u_long cmd, caddr_t data, struct proc *p)
2856
92.9k
{
2857
92.9k
  char ifname[IFNAMSIZ + 1];
2858
92.9k
  struct ifnet *ifp = NULL;
2859
92.9k
  struct ifstat *ifs = NULL;
2860
92.9k
  int error = 0;
2861
2862
92.9k
  bzero(ifname, sizeof(ifname));
2863
2864
  /*
2865
   * ioctls which don't require ifp, or ifreq ioctls
2866
   */
2867
92.9k
  switch (cmd) {
2868
695
  case OSIOCGIFCONF32:                    /* struct ifconf32 */
2869
1.68k
  case SIOCGIFCONF32:                     /* struct ifconf32 */
2870
2.65k
  case SIOCGIFCONF64:                     /* struct ifconf64 */
2871
2.83k
  case OSIOCGIFCONF64:                    /* struct ifconf64 */
2872
2.83k
    error = ifioctl_ifconf(cmd, data);
2873
2.83k
    goto done;
2874
2875
480
  case SIOCIFGCLONERS32:                  /* struct if_clonereq32 */
2876
1.44k
  case SIOCIFGCLONERS64:                  /* struct if_clonereq64 */
2877
1.44k
    error = ifioctl_ifclone(cmd, data);
2878
1.44k
    goto done;
2879
2880
198
  case SIOCGIFAGENTDATA32:                /* struct netagent_req32 */
2881
899
  case SIOCGIFAGENTDATA64:                /* struct netagent_req64 */
2882
1.02k
  case SIOCGIFAGENTLIST32:                /* struct netagentlist_req32 */
2883
1.14k
  case SIOCGIFAGENTLIST64:                /* struct netagentlist_req64 */
2884
1.14k
    error = netagent_ioctl(cmd, data);
2885
1.14k
    goto done;
2886
2887
924
  case SIOCSIFORDER:                      /* struct if_order */
2888
924
    error = ifioctl_iforder(cmd, data);
2889
924
    goto done;
2890
2891
530
  case SIOCSIFDSTADDR:                    /* struct ifreq */
2892
795
  case SIOCSIFADDR:                       /* struct ifreq */
2893
812
  case SIOCSIFBRDADDR:                    /* struct ifreq */
2894
821
  case SIOCSIFNETMASK:                    /* struct ifreq */
2895
961
  case OSIOCGIFADDR:                      /* struct ifreq */
2896
3.35k
  case OSIOCGIFDSTADDR:                   /* struct ifreq */
2897
3.38k
  case OSIOCGIFBRDADDR:                   /* struct ifreq */
2898
3.78k
  case OSIOCGIFNETMASK:                   /* struct ifreq */
2899
3.78k
  case SIOCSIFKPI:                        /* struct ifreq */
2900
3.78k
    if (so->so_proto == NULL) {
2901
0
      error = EOPNOTSUPP;
2902
0
      goto done;
2903
0
    }
2904
3.78k
    OS_FALLTHROUGH;
2905
4.04k
  case SIOCIFCREATE:                      /* struct ifreq */
2906
4.04k
  case SIOCIFCREATE2:                     /* struct ifreq */
2907
4.24k
  case SIOCIFDESTROY:                     /* struct ifreq */
2908
5.48k
  case SIOCGIFFLAGS:                      /* struct ifreq */
2909
5.50k
  case SIOCGIFEFLAGS:                     /* struct ifreq */
2910
5.50k
  case SIOCGIFCAP:                        /* struct ifreq */
2911
5.52k
  case SIOCGIFMETRIC:                     /* struct ifreq */
2912
5.57k
  case SIOCGIFMTU:                        /* struct ifreq */
2913
5.62k
  case SIOCGIFPHYS:                       /* struct ifreq */
2914
33.7k
  case SIOCSIFFLAGS:                      /* struct ifreq */
2915
33.7k
  case SIOCSIFCAP:                        /* struct ifreq */
2916
34.9k
  case SIOCSIFMETRIC:                     /* struct ifreq */
2917
35.6k
  case SIOCSIFPHYS:                       /* struct ifreq */
2918
37.5k
  case SIOCSIFMTU:                        /* struct ifreq */
2919
40.1k
  case SIOCADDMULTI:                      /* struct ifreq */
2920
42.3k
  case SIOCDELMULTI:                      /* struct ifreq */
2921
42.5k
  case SIOCDIFPHYADDR:                    /* struct ifreq */
2922
42.5k
  case SIOCSIFMEDIA:                      /* struct ifreq */
2923
42.8k
  case SIOCSIFGENERIC:                    /* struct ifreq */
2924
43.4k
  case SIOCSIFLLADDR:                     /* struct ifreq */
2925
44.1k
  case SIOCSIFALTMTU:                     /* struct ifreq */
2926
46.6k
  case SIOCSIFVLAN:                       /* struct ifreq */
2927
46.6k
  case SIOCSIFBOND:                       /* struct ifreq */
2928
46.7k
  case SIOCGIFLLADDR:                     /* struct ifreq */
2929
46.9k
  case SIOCGIFTYPE:                       /* struct ifreq */
2930
46.9k
  case SIOCGIFFUNCTIONALTYPE:             /* struct ifreq */
2931
47.0k
  case SIOCGIFPSRCADDR:                   /* struct ifreq */
2932
47.2k
  case SIOCGIFPDSTADDR:                   /* struct ifreq */
2933
47.3k
  case SIOCGIFGENERIC:                    /* struct ifreq */
2934
47.5k
  case SIOCGIFDEVMTU:                     /* struct ifreq */
2935
48.0k
  case SIOCGIFVLAN:                       /* struct ifreq */
2936
48.0k
  case SIOCGIFBOND:                       /* struct ifreq */
2937
48.0k
  case SIOCGIFWAKEFLAGS:                  /* struct ifreq */
2938
48.4k
  case SIOCGIFGETRTREFCNT:                /* struct ifreq */
2939
48.4k
  case SIOCSIFOPPORTUNISTIC:              /* struct ifreq */
2940
48.4k
  case SIOCGIFOPPORTUNISTIC:              /* struct ifreq */
2941
48.4k
  case SIOCGIFLINKQUALITYMETRIC:          /* struct ifreq */
2942
48.9k
  case SIOCSIFLOG:                        /* struct ifreq */
2943
49.0k
  case SIOCGIFLOG:                        /* struct ifreq */
2944
49.0k
  case SIOCGIFDELEGATE:                   /* struct ifreq */
2945
49.2k
  case SIOCGIFEXPENSIVE:                  /* struct ifreq */
2946
49.2k
  case SIOCSIFEXPENSIVE:                  /* struct ifreq */
2947
50.5k
  case SIOCSIF2KCL:                       /* struct ifreq */
2948
50.5k
  case SIOCGIF2KCL:                       /* struct ifreq */
2949
51.3k
  case SIOCSIFINTERFACESTATE:             /* struct ifreq */
2950
51.3k
  case SIOCGIFINTERFACESTATE:             /* struct ifreq */
2951
52.0k
  case SIOCSIFPROBECONNECTIVITY:          /* struct ifreq */
2952
52.2k
  case SIOCGIFPROBECONNECTIVITY:          /* struct ifreq */
2953
52.9k
  case SIOCGSTARTDELAY:                   /* struct ifreq */
2954
53.6k
  case SIOCSIFTIMESTAMPENABLE:            /* struct ifreq */
2955
54.6k
  case SIOCSIFTIMESTAMPDISABLE:           /* struct ifreq */
2956
54.6k
  case SIOCGIFTIMESTAMPENABLED:           /* struct ifreq */
2957
54.6k
#if (DEBUG || DEVELOPMENT)
2958
54.7k
  case SIOCSIFDISABLEOUTPUT:              /* struct ifreq */
2959
54.7k
#endif /* (DEBUG || DEVELOPMENT) */
2960
56.1k
  case SIOCGECNMODE:                      /* struct ifreq */
2961
58.0k
  case SIOCSECNMODE:
2962
58.1k
  case SIOCSQOSMARKINGMODE:               /* struct ifreq */
2963
58.2k
  case SIOCSQOSMARKINGENABLED:            /* struct ifreq */
2964
60.1k
  case SIOCGQOSMARKINGMODE:               /* struct ifreq */
2965
60.1k
  case SIOCGQOSMARKINGENABLED:            /* struct ifreq */
2966
60.1k
  case SIOCSIFLOWINTERNET:                /* struct ifreq */
2967
60.2k
  case SIOCGIFLOWINTERNET:                /* struct ifreq */
2968
60.2k
  case SIOCGIFLOWPOWER:                   /* struct ifreq */
2969
60.2k
  case SIOCSIFLOWPOWER:                   /* struct ifreq */
2970
60.2k
  case SIOCSIF6LOWPAN:                    /* struct ifreq */
2971
60.2k
  case SIOCGIF6LOWPAN:                    /* struct ifreq */
2972
60.2k
  case SIOCGIFMPKLOG:                     /* struct ifreq */
2973
60.2k
  case SIOCSIFMPKLOG:                     /* struct ifreq */
2974
60.2k
  case SIOCGIFCONSTRAINED:                /* struct ifreq */
2975
60.2k
  case SIOCSIFCONSTRAINED:                /* struct ifreq */
2976
60.2k
  case SIOCGIFXFLAGS:                     /* struct ifreq */
2977
60.2k
  case SIOCGIFNOACKPRIO:                  /* struct ifreq */
2978
60.2k
  case SIOCSIFNOACKPRIO:                  /* struct ifreq */
2979
60.2k
  {                       /* struct ifreq */
2980
60.2k
    struct ifreq ifr;
2981
60.2k
    bcopy(data, &ifr, sizeof(ifr));
2982
60.2k
    ifr.ifr_name[IFNAMSIZ - 1] = '\0';
2983
60.2k
    bcopy(&ifr.ifr_name, ifname, IFNAMSIZ);
2984
60.2k
    if (ifioctl_restrict_intcoproc(cmd, ifname, NULL, p) == true) {
2985
0
      error = EPERM;
2986
0
      goto done;
2987
0
    }
2988
60.2k
    error = ifioctl_ifreq(so, cmd, &ifr, p);
2989
60.2k
    bcopy(&ifr, data, sizeof(ifr));
2990
60.2k
    goto done;
2991
60.2k
  }
2992
92.9k
  }
2993
2994
  /*
2995
   * ioctls which require ifp.  Note that we acquire dlil_ifnet_lock
2996
   * here to ensure that the ifnet, if found, has been fully attached.
2997
   */
2998
26.4k
  dlil_if_lock();
2999
26.4k
  switch (cmd) {
3000
172
  case SIOCSIFPHYADDR:                    /* struct {if,in_}aliasreq */
3001
172
    bcopy(((struct in_aliasreq *)(void *)data)->ifra_name,
3002
172
        ifname, IFNAMSIZ);
3003
172
    ifp = ifunit_ref(ifname);
3004
172
    break;
3005
3006
17
  case SIOCSIFPHYADDR_IN6_32:             /* struct in6_aliasreq_32 */
3007
17
    bcopy(((struct in6_aliasreq_32 *)(void *)data)->ifra_name,
3008
17
        ifname, IFNAMSIZ);
3009
17
    ifp = ifunit_ref(ifname);
3010
17
    break;
3011
3012
69
  case SIOCSIFPHYADDR_IN6_64:             /* struct in6_aliasreq_64 */
3013
69
    bcopy(((struct in6_aliasreq_64 *)(void *)data)->ifra_name,
3014
69
        ifname, IFNAMSIZ);
3015
69
    ifp = ifunit_ref(ifname);
3016
69
    break;
3017
3018
892
  case SIOCGIFSTATUS:                     /* struct ifstat */
3019
892
    ifs = _MALLOC(sizeof(*ifs), M_DEVBUF, M_WAITOK);
3020
892
    if (ifs == NULL) {
3021
0
      error = ENOMEM;
3022
0
      dlil_if_unlock();
3023
0
      goto done;
3024
0
    }
3025
892
    bcopy(data, ifs, sizeof(*ifs));
3026
892
    ifs->ifs_name[IFNAMSIZ - 1] = '\0';
3027
892
    bcopy(ifs->ifs_name, ifname, IFNAMSIZ);
3028
892
    ifp = ifunit_ref(ifname);
3029
892
    break;
3030
3031
2.37k
  case SIOCGIFMEDIA32:                    /* struct ifmediareq32 */
3032
2.37k
  case SIOCGIFXMEDIA32:                    /* struct ifmediareq32 */
3033
2.37k
    bcopy(((struct ifmediareq32 *)(void *)data)->ifm_name,
3034
2.37k
        ifname, IFNAMSIZ);
3035
2.37k
    ifp = ifunit_ref(ifname);
3036
2.37k
    break;
3037
3038
20
  case SIOCGIFMEDIA64:                    /* struct ifmediareq64 */
3039
20
  case SIOCGIFXMEDIA64:                    /* struct ifmediareq64 */
3040
20
    bcopy(((struct ifmediareq64 *)(void *)data)->ifm_name,
3041
20
        ifname, IFNAMSIZ);
3042
20
    ifp = ifunit_ref(ifname);
3043
20
    break;
3044
3045
579
  case SIOCSIFDESC:                       /* struct if_descreq */
3046
585
  case SIOCGIFDESC:                       /* struct if_descreq */
3047
585
    bcopy(((struct if_descreq *)(void *)data)->ifdr_name,
3048
585
        ifname, IFNAMSIZ);
3049
585
    ifp = ifunit_ref(ifname);
3050
585
    break;
3051
3052
8.14k
  case SIOCSIFLINKPARAMS:                 /* struct if_linkparamsreq */
3053
8.21k
  case SIOCGIFLINKPARAMS:                 /* struct if_linkparamsreq */
3054
8.21k
    bcopy(((struct if_linkparamsreq *)(void *)data)->iflpr_name,
3055
8.21k
        ifname, IFNAMSIZ);
3056
8.21k
    ifp = ifunit_ref(ifname);
3057
8.21k
    break;
3058
3059
13
  case SIOCGIFQUEUESTATS:                 /* struct if_qstatsreq */
3060
13
    bcopy(((struct if_qstatsreq *)(void *)data)->ifqr_name,
3061
13
        ifname, IFNAMSIZ);
3062
13
    ifp = ifunit_ref(ifname);
3063
13
    break;
3064
3065
704
  case SIOCSIFTHROTTLE:                   /* struct if_throttlereq */
3066
709
  case SIOCGIFTHROTTLE:                   /* struct if_throttlereq */
3067
709
    bcopy(((struct if_throttlereq *)(void *)data)->ifthr_name,
3068
709
        ifname, IFNAMSIZ);
3069
709
    ifp = ifunit_ref(ifname);
3070
709
    break;
3071
3072
848
  case SIOCAIFAGENTID:                    /* struct if_agentidreq */
3073
1.46k
  case SIOCDIFAGENTID:                    /* struct if_agentidreq */
3074
1.46k
  case SIOCGIFAGENTIDS32:         /* struct if_agentidsreq32 */
3075
1.62k
  case SIOCGIFAGENTIDS64:         /* struct if_agentidsreq64 */
3076
1.62k
    bcopy(((struct if_agentidreq *)(void *)data)->ifar_name,
3077
1.62k
        ifname, IFNAMSIZ);
3078
1.62k
    ifp = ifunit_ref(ifname);
3079
1.62k
    break;
3080
3081
869
  case SIOCSIFNETSIGNATURE:               /* struct if_nsreq */
3082
887
  case SIOCGIFNETSIGNATURE:               /* struct if_nsreq */
3083
887
    bcopy(((struct if_nsreq *)(void *)data)->ifnsr_name,
3084
887
        ifname, IFNAMSIZ);
3085
887
    ifp = ifunit_ref(ifname);
3086
887
    break;
3087
3088
0
  case SIOCSIFNETWORKID:                  /* struct if_netidreq */
3089
0
    bcopy(((struct if_netidreq *)(void *)data)->ifnetid_name,
3090
0
        ifname, IFNAMSIZ);
3091
0
    ifp = ifunit_ref(ifname);
3092
0
    break;
3093
0
  case SIOCGIFPROTOLIST32:                /* struct if_protolistreq32 */
3094
0
  case SIOCGIFPROTOLIST64:                /* struct if_protolistreq64 */
3095
0
    bcopy(((struct if_protolistreq *)(void *)data)->ifpl_name,
3096
0
        ifname, IFNAMSIZ);
3097
0
    ifp = ifunit_ref(ifname);
3098
0
    break;
3099
10.8k
  default:
3100
    /*
3101
     * This is a bad assumption, but the code seems to
3102
     * have been doing this in the past; caveat emptor.
3103
     */
3104
10.8k
    bcopy(((struct ifreq *)(void *)data)->ifr_name,
3105
10.8k
        ifname, IFNAMSIZ);
3106
10.8k
    ifp = ifunit_ref(ifname);
3107
10.8k
    break;
3108
26.4k
  }
3109
26.4k
  dlil_if_unlock();
3110
3111
26.4k
  if (ifp == NULL) {
3112
22.1k
    error = ENXIO;
3113
22.1k
    goto done;
3114
22.1k
  }
3115
3116
4.23k
  if (ifioctl_restrict_intcoproc(cmd, NULL, ifp, p) == true) {
3117
0
    error = EPERM;
3118
0
    goto done;
3119
0
  }
3120
4.23k
  switch (cmd) {
3121
0
  case SIOCSIFPHYADDR:                    /* struct {if,in_}aliasreq */
3122
2
  case SIOCSIFPHYADDR_IN6_32:             /* struct in6_aliasreq_32 */
3123
2
  case SIOCSIFPHYADDR_IN6_64:             /* struct in6_aliasreq_64 */
3124
2
    error = proc_suser(p);
3125
2
    if (error != 0) {
3126
0
      break;
3127
0
    }
3128
3129
2
    error = ifnet_ioctl(ifp, SOCK_DOM(so), cmd, data);
3130
2
    if (error != 0) {
3131
2
      break;
3132
2
    }
3133
3134
0
    ifnet_touch_lastchange(ifp);
3135
0
    break;
3136
3137
0
  case SIOCGIFSTATUS:                     /* struct ifstat */
3138
0
    VERIFY(ifs != NULL);
3139
0
    ifs->ascii[0] = '\0';
3140
3141
0
    error = ifnet_ioctl(ifp, SOCK_DOM(so), cmd, (caddr_t)ifs);
3142
3143
0
    bcopy(ifs, data, sizeof(*ifs));
3144
0
    break;
3145
3146
1
  case SIOCGIFMEDIA32:                    /* struct ifmediareq32 */
3147
1
  case SIOCGIFMEDIA64:                    /* struct ifmediareq64 */
3148
1
  case SIOCGIFXMEDIA32:                    /* struct ifmediareq32 */
3149
1
  case SIOCGIFXMEDIA64:                    /* struct ifmediareq64 */
3150
1
    error = ifioctl_get_media(ifp, so, cmd, data);
3151
1
    break;
3152
3153
0
  case SIOCSIFDESC:                       /* struct if_descreq */
3154
0
  case SIOCGIFDESC:                       /* struct if_descreq */
3155
0
    error = ifioctl_ifdesc(ifp, cmd, data, p);
3156
0
    break;
3157
3158
0
  case SIOCSIFLINKPARAMS:                 /* struct if_linkparamsreq */
3159
0
  case SIOCGIFLINKPARAMS:                 /* struct if_linkparamsreq */
3160
0
    error = ifioctl_linkparams(ifp, cmd, data, p);
3161
0
    break;
3162
3163
0
  case SIOCGIFQUEUESTATS:                 /* struct if_qstatsreq */
3164
0
    error = ifioctl_qstats(ifp, cmd, data);
3165
0
    break;
3166
3167
0
  case SIOCSIFTHROTTLE:                   /* struct if_throttlereq */
3168
0
  case SIOCGIFTHROTTLE:                   /* struct if_throttlereq */
3169
0
    error = ifioctl_throttle(ifp, cmd, data, p);
3170
0
    break;
3171
3172
3
  case SIOCAIFAGENTID:                    /* struct if_agentidreq */
3173
3
  case SIOCDIFAGENTID:                    /* struct if_agentidreq */
3174
3
  case SIOCGIFAGENTIDS32:         /* struct if_agentidsreq32 */
3175
5
  case SIOCGIFAGENTIDS64:         /* struct if_agentidsreq64 */
3176
5
    error = ifioctl_netagent(ifp, cmd, data, p);
3177
5
    break;
3178
3179
0
  case SIOCSIFNETSIGNATURE:               /* struct if_nsreq */
3180
0
  case SIOCGIFNETSIGNATURE:               /* struct if_nsreq */
3181
0
    error = ifioctl_netsignature(ifp, cmd, data);
3182
0
    break;
3183
3184
0
  case SIOCSIFNETWORKID:                  /* struct if_netidreq */
3185
0
    error = ifioctl_networkid(ifp, data);
3186
0
    break;
3187
1
  case SIOCSIFNAT64PREFIX:                /* struct if_nat64req */
3188
2
  case SIOCGIFNAT64PREFIX:                /* struct if_nat64req */
3189
2
    error = ifioctl_nat64prefix(ifp, cmd, data);
3190
2
    break;
3191
3192
0
  case SIOCGIFCLAT46ADDR:                 /* struct if_clat46req */
3193
0
    error = ifioctl_clat46addr(ifp, cmd, data);
3194
0
    break;
3195
3196
0
  case SIOCGIFPROTOLIST32:                /* struct if_protolistreq32 */
3197
0
  case SIOCGIFPROTOLIST64:                /* struct if_protolistreq64 */
3198
0
    error = ifioctl_protolist(ifp, cmd, data);
3199
0
    break;
3200
3201
4.22k
  default:
3202
4.22k
    if (so->so_proto == NULL) {
3203
0
      error = EOPNOTSUPP;
3204
0
      break;
3205
0
    }
3206
3207
4.22k
    socket_lock(so, 1);
3208
4.22k
    error = ((*so->so_proto->pr_usrreqs->pru_control)(so, cmd,
3209
4.22k
        data, ifp, p));
3210
4.22k
    socket_unlock(so, 1);
3211
3212
    // Don't allow to call SIOCAIFADDR and SIOCDIFADDR with
3213
    // ifreq as the code expects ifaddr
3214
4.22k
    if ((error == EOPNOTSUPP || error == ENOTSUP) &&
3215
4.22k
        !(cmd == SIOCAIFADDR || cmd == SIOCDIFADDR)) {
3216
4.22k
      error = ifnet_ioctl(ifp, SOCK_DOM(so), cmd, data);
3217
4.22k
    }
3218
4.22k
    break;
3219
4.23k
  }
3220
3221
92.9k
done:
3222
92.9k
  if (ifs != NULL) {
3223
892
    _FREE(ifs, M_DEVBUF);
3224
892
  }
3225
3226
92.9k
  if (if_verbose) {
3227
0
    if (ifname[0] == '\0') {
3228
0
      (void) snprintf(ifname, sizeof(ifname), "%s",
3229
0
          "NULL");
3230
0
    } else if (ifp != NULL) {
3231
0
      (void) snprintf(ifname, sizeof(ifname), "%s",
3232
0
          if_name(ifp));
3233
0
    }
3234
3235
0
    if (error != 0) {
3236
0
      printf("%s[%s,%d]: ifp %s cmd 0x%08lx (%c%c [%lu] "
3237
0
          "%c %lu) error %d\n", __func__,
3238
0
          proc_name_address(p), proc_pid(p),
3239
0
          ifname, cmd, (cmd & IOC_IN) ? 'I' : ' ',
3240
0
          (cmd & IOC_OUT) ? 'O' : ' ', IOCPARM_LEN(cmd),
3241
0
          (char)IOCGROUP(cmd), cmd & 0xff, error);
3242
0
    } else if (if_verbose > 1) {
3243
0
      printf("%s[%s,%d]: ifp %s cmd 0x%08lx (%c%c [%lu] "
3244
0
          "%c %lu) OK\n", __func__,
3245
0
          proc_name_address(p), proc_pid(p),
3246
0
          ifname, cmd, (cmd & IOC_IN) ? 'I' : ' ',
3247
0
          (cmd & IOC_OUT) ? 'O' : ' ', IOCPARM_LEN(cmd),
3248
0
          (char)IOCGROUP(cmd), cmd & 0xff);
3249
0
    }
3250
0
  }
3251
3252
92.9k
  if (ifp != NULL) {
3253
4.23k
    ifnet_decr_iorefcnt(ifp);
3254
4.23k
  }
3255
92.9k
  return error;
3256
4.23k
}
3257
3258
static __attribute__((noinline)) int
3259
ifioctl_ifreq(struct socket *so, u_long cmd, struct ifreq *ifr, struct proc *p)
3260
60.2k
{
3261
60.2k
  struct ifnet *ifp;
3262
60.2k
  u_long ocmd = cmd;
3263
60.2k
  int error = 0;
3264
60.2k
  struct kev_msg ev_msg;
3265
60.2k
  struct net_event_data ev_data;
3266
3267
60.2k
  bzero(&ev_data, sizeof(struct net_event_data));
3268
60.2k
  bzero(&ev_msg, sizeof(struct kev_msg));
3269
3270
60.2k
  switch (cmd) {
3271
259
  case SIOCIFCREATE:
3272
259
  case SIOCIFCREATE2:
3273
259
    error = proc_suser(p);
3274
259
    if (error) {
3275
0
      return error;
3276
0
    }
3277
259
    return if_clone_create(ifr->ifr_name, sizeof(ifr->ifr_name),
3278
259
               cmd == SIOCIFCREATE2 ? ifr->ifr_data : NULL);
3279
205
  case SIOCIFDESTROY:
3280
205
    error = proc_suser(p);
3281
205
    if (error) {
3282
0
      return error;
3283
0
    }
3284
205
    return if_clone_destroy(ifr->ifr_name);
3285
60.2k
  }
3286
3287
  /*
3288
   * ioctls which require ifp.  Note that we acquire dlil_ifnet_lock
3289
   * here to ensure that the ifnet, if found, has been fully attached.
3290
   */
3291
59.7k
  dlil_if_lock();
3292
59.7k
  ifp = ifunit(ifr->ifr_name);
3293
59.7k
  dlil_if_unlock();
3294
3295
59.7k
  if (ifp == NULL) {
3296
31.6k
    return ENXIO;
3297
31.6k
  }
3298
3299
28.1k
  switch (cmd) {
3300
0
  case SIOCGIFFLAGS:
3301
0
    ifnet_lock_shared(ifp);
3302
0
    ifr->ifr_flags = ifp->if_flags;
3303
0
    ifnet_lock_done(ifp);
3304
0
    break;
3305
3306
0
  case SIOCGIFEFLAGS:
3307
0
    ifnet_lock_shared(ifp);
3308
0
    ifr->ifr_eflags = ifp->if_eflags;
3309
0
    ifnet_lock_done(ifp);
3310
0
    break;
3311
3312
0
  case SIOCGIFXFLAGS:
3313
0
    ifnet_lock_shared(ifp);
3314
0
    ifr->ifr_xflags = ifp->if_xflags;
3315
0
    ifnet_lock_done(ifp);
3316
0
    break;
3317
3318
0
  case SIOCGIFCAP:
3319
0
    ifnet_lock_shared(ifp);
3320
0
    ifr->ifr_reqcap = ifp->if_capabilities;
3321
0
    ifr->ifr_curcap = ifp->if_capenable;
3322
0
    ifnet_lock_done(ifp);
3323
0
    break;
3324
3325
0
  case SIOCGIFMETRIC:
3326
0
    ifnet_lock_shared(ifp);
3327
0
    ifr->ifr_metric = ifp->if_metric;
3328
0
    ifnet_lock_done(ifp);
3329
0
    break;
3330
3331
0
  case SIOCGIFMTU:
3332
0
    ifnet_lock_shared(ifp);
3333
0
    ifr->ifr_mtu = ifp->if_mtu;
3334
0
    ifnet_lock_done(ifp);
3335
0
    break;
3336
3337
0
  case SIOCGIFPHYS:
3338
0
    ifnet_lock_shared(ifp);
3339
0
    ifr->ifr_phys = ifp->if_physical;
3340
0
    ifnet_lock_done(ifp);
3341
0
    break;
3342
3343
28.1k
  case SIOCSIFFLAGS:
3344
28.1k
    error = proc_suser(p);
3345
28.1k
    if (error != 0) {
3346
0
      break;
3347
0
    }
3348
3349
28.1k
    (void) ifnet_set_flags(ifp, ifr->ifr_flags,
3350
28.1k
        (u_int16_t)~IFF_CANTCHANGE);
3351
3352
    /*
3353
     * Note that we intentionally ignore any error from below
3354
     * for the SIOCSIFFLAGS case.
3355
     */
3356
28.1k
    (void) ifnet_ioctl(ifp, SOCK_DOM(so), cmd, (caddr_t)ifr);
3357
3358
    /*
3359
     * Send the event even upon error from the driver because
3360
     * we changed the flags.
3361
     */
3362
28.1k
    dlil_post_sifflags_msg(ifp);
3363
3364
28.1k
    ifnet_touch_lastchange(ifp);
3365
28.1k
    break;
3366
3367
0
  case SIOCSIFCAP:
3368
0
    error = proc_suser(p);
3369
0
    if (error != 0) {
3370
0
      break;
3371
0
    }
3372
3373
0
    if ((ifr->ifr_reqcap & ~ifp->if_capabilities)) {
3374
0
      error = EINVAL;
3375
0
      break;
3376
0
    }
3377
0
    error = ifnet_ioctl(ifp, SOCK_DOM(so), cmd, (caddr_t)ifr);
3378
3379
0
    ifnet_touch_lastchange(ifp);
3380
0
    break;
3381
3382
1
  case SIOCSIFMETRIC:
3383
1
    error = proc_suser(p);
3384
1
    if (error != 0) {
3385
0
      break;
3386
0
    }
3387
3388
1
    ifp->if_metric = ifr->ifr_metric;
3389
3390
1
    ev_msg.vendor_code    = KEV_VENDOR_APPLE;
3391
1
    ev_msg.kev_class      = KEV_NETWORK_CLASS;
3392
1
    ev_msg.kev_subclass   = KEV_DL_SUBCLASS;
3393
3394
1
    ev_msg.event_code = KEV_DL_SIFMETRICS;
3395
1
    strlcpy(&ev_data.if_name[0], ifp->if_name, IFNAMSIZ);
3396
1
    ev_data.if_family = ifp->if_family;
3397
1
    ev_data.if_unit   = (u_int32_t) ifp->if_unit;
3398
1
    ev_msg.dv[0].data_length = sizeof(struct net_event_data);
3399
1
    ev_msg.dv[0].data_ptr    = &ev_data;
3400
3401
1
    ev_msg.dv[1].data_length = 0;
3402
1
    dlil_post_complete_msg(ifp, &ev_msg);
3403
3404
1
    ifnet_touch_lastchange(ifp);
3405
1
    break;
3406
3407
2
  case SIOCSIFPHYS:
3408
2
    error = proc_suser(p);
3409
2
    if (error != 0) {
3410
0
      break;
3411
0
    }
3412
3413
2
    error = ifnet_ioctl(ifp, SOCK_DOM(so), cmd, (caddr_t)ifr);
3414
2
    if (error != 0) {
3415
2
      break;
3416
2
    }
3417
3418
0
    ev_msg.vendor_code    = KEV_VENDOR_APPLE;
3419
0
    ev_msg.kev_class      = KEV_NETWORK_CLASS;
3420
0
    ev_msg.kev_subclass   = KEV_DL_SUBCLASS;
3421
3422
0
    ev_msg.event_code = KEV_DL_SIFPHYS;
3423
0
    strlcpy(&ev_data.if_name[0], ifp->if_name, IFNAMSIZ);
3424
0
    ev_data.if_family = ifp->if_family;
3425
0
    ev_data.if_unit   = (u_int32_t) ifp->if_unit;
3426
0
    ev_msg.dv[0].data_length = sizeof(struct net_event_data);
3427
0
    ev_msg.dv[0].data_ptr    = &ev_data;
3428
0
    ev_msg.dv[1].data_length = 0;
3429
0
    dlil_post_complete_msg(ifp, &ev_msg);
3430
3431
0
    ifnet_touch_lastchange(ifp);
3432
0
    break;
3433
3434
1
  case SIOCSIFMTU: {
3435
1
    u_int32_t oldmtu = ifp->if_mtu;
3436
1
    struct ifclassq *ifq = &ifp->if_snd;
3437
3438
1
    error = proc_suser(p);
3439
1
    if (error != 0) {
3440
0
      break;
3441
0
    }
3442
3443
1
    if (ifp->if_ioctl == NULL) {
3444
0
      error = EOPNOTSUPP;
3445
0
      break;
3446
0
    }
3447
1
    if (ifr->ifr_mtu < IF_MINMTU || ifr->ifr_mtu > IF_MAXMTU) {
3448
0
      error = EINVAL;
3449
0
      break;
3450
0
    }
3451
1
    error = ifnet_ioctl(ifp, SOCK_DOM(so), cmd, (caddr_t)ifr);
3452
1
    if (error != 0) {
3453
0
      break;
3454
0
    }
3455
3456
1
    ev_msg.vendor_code    = KEV_VENDOR_APPLE;
3457
1
    ev_msg.kev_class      = KEV_NETWORK_CLASS;
3458
1
    ev_msg.kev_subclass   = KEV_DL_SUBCLASS;
3459
3460
1
    ev_msg.event_code = KEV_DL_SIFMTU;
3461
1
    strlcpy(&ev_data.if_name[0], ifp->if_name, IFNAMSIZ);
3462
1
    ev_data.if_family = ifp->if_family;
3463
1
    ev_data.if_unit   = (u_int32_t) ifp->if_unit;
3464
1
    ev_msg.dv[0].data_length = sizeof(struct net_event_data);
3465
1
    ev_msg.dv[0].data_ptr    = &ev_data;
3466
1
    ev_msg.dv[1].data_length = 0;
3467
1
    dlil_post_complete_msg(ifp, &ev_msg);
3468
3469
1
    ifnet_touch_lastchange(ifp);
3470
1
    rt_ifmsg(ifp);
3471
3472
    /*
3473
     * If the link MTU changed, do network layer specific procedure
3474
     * and update all route entries associated with the interface,
3475
     * so that their MTU metric gets updated.
3476
     */
3477
1
    if (ifp->if_mtu != oldmtu) {
3478
1
      if_rtmtu_update(ifp);
3479
1
      nd6_setmtu(ifp);
3480
      /* Inform all transmit queues about the new MTU */
3481
1
      IFCQ_LOCK(ifq);
3482
1
      ifnet_update_sndq(ifq, CLASSQ_EV_LINK_MTU);
3483
1
      IFCQ_UNLOCK(ifq);
3484
1
    }
3485
1
    break;
3486
1
  }
3487
3488
1
  case SIOCADDMULTI:
3489
3
  case SIOCDELMULTI:
3490
3
    error = proc_suser(p);
3491
3
    if (error != 0) {
3492
0
      break;
3493
0
    }
3494
3495
    /* Don't allow group membership on non-multicast interfaces. */
3496
3
    if ((ifp->if_flags & IFF_MULTICAST) == 0) {
3497
0
      error = EOPNOTSUPP;
3498
0
      break;
3499
0
    }
3500
3501
    /* Don't let users screw up protocols' entries. */
3502
3
    if (ifr->ifr_addr.sa_family != AF_UNSPEC &&
3503
1
        ifr->ifr_addr.sa_family != AF_LINK) {
3504
1
      error = EINVAL;
3505
1
      break;
3506
1
    }
3507
2
    if (ifr->ifr_addr.sa_len > sizeof(struct sockaddr)) {
3508
2
      ifr->ifr_addr.sa_len = sizeof(struct sockaddr);
3509
2
    }
3510
3511
    /*
3512
     * User is permitted to anonymously join a particular link
3513
     * multicast group via SIOCADDMULTI.  Subsequent join requested
3514
     * for the same record which has an outstanding refcnt from a
3515
     * past if_addmulti_anon() will not result in EADDRINUSE error
3516
     * (unlike other BSDs.)  Anonymously leaving a group is also
3517
     * allowed only as long as there is an outstanding refcnt held
3518
     * by a previous anonymous request, or else ENOENT (even if the
3519
     * link-layer multicast membership exists for a network-layer
3520
     * membership.)
3521
     */
3522
2
    if (cmd == SIOCADDMULTI) {
3523
0
      error = if_addmulti_anon(ifp, &ifr->ifr_addr, NULL);
3524
0
      ev_msg.event_code = KEV_DL_ADDMULTI;
3525
2
    } else {
3526
2
      error = if_delmulti_anon(ifp, &ifr->ifr_addr);
3527
2
      ev_msg.event_code = KEV_DL_DELMULTI;
3528
2
    }
3529
2
    if (error != 0) {
3530
2
      break;
3531
2
    }
3532
3533
0
    ev_msg.vendor_code    = KEV_VENDOR_APPLE;
3534
0
    ev_msg.kev_class      = KEV_NETWORK_CLASS;
3535
0
    ev_msg.kev_subclass   = KEV_DL_SUBCLASS;
3536
0
    strlcpy(&ev_data.if_name[0], ifp->if_name, IFNAMSIZ);
3537
3538
0
    ev_data.if_family = ifp->if_family;
3539
0
    ev_data.if_unit   = (u_int32_t) ifp->if_unit;
3540
0
    ev_msg.dv[0].data_length = sizeof(struct net_event_data);
3541
0
    ev_msg.dv[0].data_ptr    = &ev_data;
3542
0
    ev_msg.dv[1].data_length = 0;
3543
0
    dlil_post_complete_msg(ifp, &ev_msg);
3544
3545
0
    ifnet_touch_lastchange(ifp);
3546
0
    break;
3547
3548
0
  case SIOCSIFMEDIA:
3549
0
    error = proc_suser(p);
3550
0
    if (error != 0) {
3551
0
      break;
3552
0
    }
3553
    /*
3554
     * Silently ignore setting IFM_OTHER
3555
     */
3556
0
    if (ifr->ifr_media == IFM_OTHER) {
3557
0
      os_log_info(OS_LOG_DEFAULT,
3558
0
          "%s: %s SIOCSIFMEDIA ignore IFM_OTHER",
3559
0
          __func__, ifp->if_xname);
3560
0
      error = 0;
3561
0
      break;
3562
0
    }
3563
0
    error = ifnet_ioctl(ifp, SOCK_DOM(so), cmd, (caddr_t)ifr);
3564
0
    if (error != 0) {
3565
0
      break;
3566
0
    }
3567
0
    ifnet_touch_lastchange(ifp);
3568
0
    break;
3569
3570
0
  case SIOCDIFPHYADDR:
3571
0
  case SIOCSIFGENERIC:
3572
0
  case SIOCSIFLLADDR:
3573
0
  case SIOCSIFALTMTU:
3574
1
  case SIOCSIFVLAN:
3575
1
  case SIOCSIFBOND:
3576
1
  case SIOCSIF6LOWPAN:
3577
1
    error = proc_suser(p);
3578
1
    if (error != 0) {
3579
0
      break;
3580
0
    }
3581
3582
1
    error = ifnet_ioctl(ifp, SOCK_DOM(so), cmd, (caddr_t)ifr);
3583
1
    if (error != 0) {
3584
1
      break;
3585
1
    }
3586
3587
0
    ifnet_touch_lastchange(ifp);
3588
0
    break;
3589
3590
0
  case SIOCGIFLLADDR: {
3591
0
    struct sockaddr_dl *sdl = SDL(ifp->if_lladdr->ifa_addr);
3592
3593
0
    if (sdl->sdl_alen == 0) {
3594
0
      error = EADDRNOTAVAIL;
3595
0
      break;
3596
0
    }
3597
    /* If larger than 14-bytes we'll need another mechanism */
3598
0
    if (sdl->sdl_alen > sizeof(ifr->ifr_addr.sa_data)) {
3599
0
      error = EMSGSIZE;
3600
0
      break;
3601
0
    }
3602
    /* Follow the same convention used by SIOCSIFLLADDR */
3603
0
    bzero(&ifr->ifr_addr, sizeof(ifr->ifr_addr));
3604
0
    ifr->ifr_addr.sa_family = AF_LINK;
3605
0
    ifr->ifr_addr.sa_len = sdl->sdl_alen;
3606
0
    error = ifnet_guarded_lladdr_copy_bytes(ifp,
3607
0
        &ifr->ifr_addr.sa_data, sdl->sdl_alen);
3608
0
    break;
3609
0
  }
3610
3611
0
  case SIOCGIFTYPE:
3612
0
    ifr->ifr_type.ift_type = ifp->if_type;
3613
0
    ifr->ifr_type.ift_family = ifp->if_family;
3614
0
    ifr->ifr_type.ift_subfamily = ifp->if_subfamily;
3615
0
    break;
3616
3617
1
  case SIOCGIFFUNCTIONALTYPE:
3618
1
    ifr->ifr_functional_type = if_functional_type(ifp, FALSE);
3619
1
    break;
3620
3621
0
  case SIOCGIFPSRCADDR:
3622
1
  case SIOCGIFPDSTADDR:
3623
1
  case SIOCGIFGENERIC:
3624
1
  case SIOCGIFDEVMTU:
3625
1
  case SIOCGIFVLAN:
3626
1
  case SIOCGIFBOND:
3627
1
  case SIOCGIF6LOWPAN:
3628
1
    error = ifnet_ioctl(ifp, SOCK_DOM(so), cmd, (caddr_t)ifr);
3629
1
    break;
3630
3631
0
  case SIOCGIFWAKEFLAGS:
3632
0
    ifnet_lock_shared(ifp);
3633
0
    ifr->ifr_wake_flags = ifnet_get_wake_flags(ifp);
3634
0
    ifnet_lock_done(ifp);
3635
0
    break;
3636
3637
0
  case SIOCGIFGETRTREFCNT:
3638
0
    ifnet_lock_shared(ifp);
3639
0
    ifr->ifr_route_refcnt = ifp->if_route_refcnt;
3640
0
    ifnet_lock_done(ifp);
3641
0
    break;
3642
3643
0
  case SIOCSIFOPPORTUNISTIC:
3644
0
  case SIOCGIFOPPORTUNISTIC:
3645
0
    error = ifnet_getset_opportunistic(ifp, cmd, ifr, p);
3646
0
    break;
3647
3648
1
  case SIOCGIFLINKQUALITYMETRIC:
3649
1
    ifnet_lock_shared(ifp);
3650
1
    if ((ifp->if_interface_state.valid_bitmask &
3651
1
        IF_INTERFACE_STATE_LQM_STATE_VALID)) {
3652
1
      ifr->ifr_link_quality_metric =
3653
1
          ifp->if_interface_state.lqm_state;
3654
1
    } else if (IF_FULLY_ATTACHED(ifp)) {
3655
0
      ifr->ifr_link_quality_metric =
3656
0
          IFNET_LQM_THRESH_UNKNOWN;
3657
0
    } else {
3658
0
      ifr->ifr_link_quality_metric =
3659
0
          IFNET_LQM_THRESH_OFF;
3660
0
    }
3661
1
    ifnet_lock_done(ifp);
3662
1
    break;
3663
3664
0
  case SIOCSIFLOG:
3665
2
  case SIOCGIFLOG:
3666
2
    error = ifnet_getset_log(ifp, cmd, ifr, p);
3667
2
    break;
3668
3669
0
  case SIOCGIFDELEGATE:
3670
0
    ifnet_lock_shared(ifp);
3671
0
    ifr->ifr_delegated = ((ifp->if_delegated.ifp != NULL) ?
3672
0
        ifp->if_delegated.ifp->if_index : 0);
3673
0
    ifnet_lock_done(ifp);
3674
0
    break;
3675
3676
0
  case SIOCGIFEXPENSIVE:
3677
0
    ifnet_lock_shared(ifp);
3678
0
    if (ifp->if_eflags & IFEF_EXPENSIVE) {
3679
0
      ifr->ifr_expensive = 1;
3680
0
    } else {
3681
0
      ifr->ifr_expensive = 0;
3682
0
    }
3683
0
    ifnet_lock_done(ifp);
3684
0
    break;
3685
3686
0
  case SIOCSIFEXPENSIVE:
3687
0
  {
3688
0
    struct ifnet *difp;
3689
3690
0
    if ((error = priv_check_cred(kauth_cred_get(),
3691
0
        PRIV_NET_INTERFACE_CONTROL, 0)) != 0) {
3692
0
      return error;
3693
0
    }
3694
0
    if (ifr->ifr_expensive) {
3695
0
      if_set_eflags(ifp, IFEF_EXPENSIVE);
3696
0
    } else {
3697
0
      if_clear_eflags(ifp, IFEF_EXPENSIVE);
3698
0
    }
3699
0
    ifnet_increment_generation(ifp);
3700
3701
    /*
3702
     * Update the expensive bit in the delegated interface
3703
     * structure.
3704
     */
3705
0
    ifnet_head_lock_shared();
3706
0
    TAILQ_FOREACH(difp, &ifnet_head, if_link) {
3707
0
      ifnet_lock_exclusive(difp);
3708
0
      if (difp->if_delegated.ifp == ifp) {
3709
0
        difp->if_delegated.expensive =
3710
0
            ifp->if_eflags & IFEF_EXPENSIVE ? 1 : 0;
3711
0
        ifnet_increment_generation(difp);
3712
0
      }
3713
0
      ifnet_lock_done(difp);
3714
0
    }
3715
0
    ifnet_head_done();
3716
0
    necp_update_all_clients();
3717
0
    break;
3718
0
  }
3719
3720
0
  case SIOCGIFCONSTRAINED:
3721
0
    if ((ifp->if_xflags & IFXF_CONSTRAINED) != 0) {
3722
0
      ifr->ifr_constrained = 1;
3723
0
    } else {
3724
0
      ifr->ifr_constrained = 0;
3725
0
    }
3726
0
    break;
3727
3728
0
  case SIOCSIFCONSTRAINED:
3729
0
  {
3730
0
    struct ifnet *difp;
3731
3732
0
    if ((error = priv_check_cred(kauth_cred_get(),
3733
0
        PRIV_NET_INTERFACE_CONTROL, 0)) != 0) {
3734
0
      return error;
3735
0
    }
3736
0
    if (ifr->ifr_constrained) {
3737
0
      if_set_xflags(ifp, IFXF_CONSTRAINED);
3738
0
    } else {
3739
0
      if_clear_xflags(ifp, IFXF_CONSTRAINED);
3740
0
    }
3741
0
    ifnet_increment_generation(ifp);
3742
    /*
3743
     * Update the constrained bit in the delegated interface
3744
     * structure.
3745
     */
3746
0
    ifnet_head_lock_shared();
3747
0
    TAILQ_FOREACH(difp, &ifnet_head, if_link) {
3748
0
      ifnet_lock_exclusive(difp);
3749
0
      if (difp->if_delegated.ifp == ifp) {
3750
0
        difp->if_delegated.constrained =
3751
0
            ((ifp->if_xflags & IFXF_CONSTRAINED) != 0) ? 1 : 0;
3752
0
        ifnet_increment_generation(difp);
3753
0
      }
3754
0
      ifnet_lock_done(difp);
3755
0
    }
3756
0
    ifnet_head_done();
3757
0
    necp_update_all_clients();
3758
0
    break;
3759
0
  }
3760
3761
0
  case SIOCGIF2KCL:
3762
0
    ifnet_lock_shared(ifp);
3763
0
    if (ifp->if_eflags & IFEF_2KCL) {
3764
0
      ifr->ifr_2kcl = 1;
3765
0
    } else {
3766
0
      ifr->ifr_2kcl = 0;
3767
0
    }
3768
0
    ifnet_lock_done(ifp);
3769
0
    break;
3770
3771
0
  case SIOCSIF2KCL:
3772
0
    if ((error = priv_check_cred(kauth_cred_get(),
3773
0
        PRIV_NET_INTERFACE_CONTROL, 0)) != 0) {
3774
0
      return error;
3775
0
    }
3776
0
    if (ifr->ifr_2kcl) {
3777
0
      if_set_eflags(ifp, IFEF_2KCL);
3778
0
    } else {
3779
0
      if_clear_eflags(ifp, IFEF_2KCL);
3780
0
    }
3781
0
    break;
3782
0
  case SIOCGSTARTDELAY:
3783
0
    ifnet_lock_shared(ifp);
3784
0
    if (ifp->if_eflags & IFEF_ENQUEUE_MULTI) {
3785
0
      ifr->ifr_start_delay_qlen =
3786
0
          ifp->if_start_delay_qlen;
3787
0
      ifr->ifr_start_delay_timeout =
3788
0
          ifp->if_start_delay_timeout;
3789
0
    } else {
3790
0
      ifr->ifr_start_delay_qlen = 0;
3791
0
      ifr->ifr_start_delay_timeout = 0;
3792
0
    }
3793
0
    ifnet_lock_done(ifp);
3794
0
    break;
3795
3
  case SIOCSIFDSTADDR:
3796
4
  case SIOCSIFADDR:
3797
4
  case SIOCSIFBRDADDR:
3798
4
  case SIOCSIFNETMASK:
3799
4
  case OSIOCGIFADDR:
3800
5
  case OSIOCGIFDSTADDR:
3801
5
  case OSIOCGIFBRDADDR:
3802
5
  case OSIOCGIFNETMASK:
3803
6
  case SIOCSIFKPI:
3804
6
    VERIFY(so->so_proto != NULL);
3805
3806
6
    if (cmd == SIOCSIFDSTADDR || cmd == SIOCSIFADDR ||
3807
6
        cmd == SIOCSIFBRDADDR || cmd == SIOCSIFNETMASK) {
3808
4
#if BYTE_ORDER != BIG_ENDIAN
3809
4
      if (ifr->ifr_addr.sa_family == 0 &&
3810
3
          ifr->ifr_addr.sa_len < 16) {
3811
3
        ifr->ifr_addr.sa_family = ifr->ifr_addr.sa_len;
3812
3
        ifr->ifr_addr.sa_len = 16;
3813
3
      }
3814
#else
3815
      if (ifr->ifr_addr.sa_len == 0) {
3816
        ifr->ifr_addr.sa_len = 16;
3817
      }
3818
#endif
3819
4
    } else if (cmd == OSIOCGIFADDR) {
3820
0
      cmd = SIOCGIFADDR;      /* struct ifreq */
3821
2
    } else if (cmd == OSIOCGIFDSTADDR) {
3822
1
      cmd = SIOCGIFDSTADDR;   /* struct ifreq */
3823
1
    } else if (cmd == OSIOCGIFBRDADDR) {
3824
0
      cmd = SIOCGIFBRDADDR;   /* struct ifreq */
3825
1
    } else if (cmd == OSIOCGIFNETMASK) {
3826
0
      cmd = SIOCGIFNETMASK;   /* struct ifreq */
3827
0
    }
3828
3829
6
    socket_lock(so, 1);
3830
6
    error = ((*so->so_proto->pr_usrreqs->pru_control)(so, cmd,
3831
6
        (caddr_t)ifr, ifp, p));
3832
6
    socket_unlock(so, 1);
3833
3834
6
    switch (ocmd) {
3835
0
    case OSIOCGIFADDR:
3836
1
    case OSIOCGIFDSTADDR:
3837
1
    case OSIOCGIFBRDADDR:
3838
1
    case OSIOCGIFNETMASK:
3839
1
      bcopy(&ifr->ifr_addr.sa_family, &ifr->ifr_addr,
3840
1
          sizeof(u_short));
3841
6
    }
3842
3843
6
    if (cmd == SIOCSIFKPI) {
3844
1
      int temperr = proc_suser(p);
3845
1
      if (temperr != 0) {
3846
0
        error = temperr;
3847
0
      }
3848
1
    }
3849
    // Don't allow to call SIOCSIFADDR and SIOCSIFDSTADDR
3850
    // with ifreq as the code expects ifaddr
3851
6
    if ((error == EOPNOTSUPP || error == ENOTSUP) &&
3852
6
        !(cmd == SIOCSIFADDR || cmd == SIOCSIFDSTADDR)) {
3853
2
      error = ifnet_ioctl(ifp, SOCK_DOM(so), cmd,
3854
2
          (caddr_t)ifr);
3855
2
    }
3856
6
    break;
3857
3858
0
  case SIOCGIFINTERFACESTATE:
3859
0
    if_get_state(ifp, &ifr->ifr_interface_state);
3860
0
    break;
3861
3862
1
  case SIOCSIFINTERFACESTATE:
3863
1
    if ((error = priv_check_cred(kauth_cred_get(),
3864
1
        PRIV_NET_INTERFACE_CONTROL, 0)) != 0) {
3865
0
      return error;
3866
0
    }
3867
3868
1
    error = if_state_update(ifp, &ifr->ifr_interface_state);
3869
3870
1
    break;
3871
0
  case SIOCSIFPROBECONNECTIVITY:
3872
0
    if ((error = priv_check_cred(kauth_cred_get(),
3873
0
        PRIV_NET_INTERFACE_CONTROL, 0)) != 0) {
3874
0
      return error;
3875
0
    }
3876
0
    error = if_probe_connectivity(ifp,
3877
0
        ifr->ifr_probe_connectivity);
3878
0
    break;
3879
1
  case SIOCGIFPROBECONNECTIVITY:
3880
1
    if ((error = priv_check_cred(kauth_cred_get(),
3881
1
        PRIV_NET_INTERFACE_CONTROL, 0)) != 0) {
3882
0
      return error;
3883
0
    }
3884
1
    if (ifp->if_eflags & IFEF_PROBE_CONNECTIVITY) {
3885
0
      ifr->ifr_probe_connectivity = 1;
3886
1
    } else {
3887
1
      ifr->ifr_probe_connectivity = 0;
3888
1
    }
3889
1
    break;
3890
0
  case SIOCGECNMODE:
3891
0
    if ((ifp->if_eflags & (IFEF_ECN_ENABLE | IFEF_ECN_DISABLE)) ==
3892
0
        IFEF_ECN_ENABLE) {
3893
0
      ifr->ifr_ecn_mode = IFRTYPE_ECN_ENABLE;
3894
0
    } else if ((ifp->if_eflags & (IFEF_ECN_ENABLE | IFEF_ECN_DISABLE)) ==
3895
0
        IFEF_ECN_DISABLE) {
3896
0
      ifr->ifr_ecn_mode = IFRTYPE_ECN_DISABLE;
3897
0
    } else {
3898
0
      ifr->ifr_ecn_mode = IFRTYPE_ECN_DEFAULT;
3899
0
    }
3900
0
    break;
3901
0
  case SIOCSECNMODE:
3902
0
    if ((error = priv_check_cred(kauth_cred_get(),
3903
0
        PRIV_NET_INTERFACE_CONTROL, 0)) != 0) {
3904
0
      return error;
3905
0
    }
3906
0
    if (ifr->ifr_ecn_mode == IFRTYPE_ECN_DEFAULT) {
3907
0
      if_clear_eflags(ifp, IFEF_ECN_ENABLE | IFEF_ECN_DISABLE);
3908
0
    } else if (ifr->ifr_ecn_mode == IFRTYPE_ECN_ENABLE) {
3909
0
      if_set_eflags(ifp, IFEF_ECN_ENABLE);
3910
0
      if_clear_eflags(ifp, IFEF_ECN_DISABLE);
3911
0
    } else if (ifr->ifr_ecn_mode == IFRTYPE_ECN_DISABLE) {
3912
0
      if_set_eflags(ifp, IFEF_ECN_DISABLE);
3913
0
      if_clear_eflags(ifp, IFEF_ECN_ENABLE);
3914
0
    } else {
3915
0
      error = EINVAL;
3916
0
    }
3917
0
    break;
3918
3919
0
  case SIOCSIFTIMESTAMPENABLE:
3920
1
  case SIOCSIFTIMESTAMPDISABLE:
3921
1
    error = proc_suser(p);
3922
1
    if (error != 0) {
3923
0
      break;
3924
0
    }
3925
3926
1
    if ((cmd == SIOCSIFTIMESTAMPENABLE &&
3927
0
        (ifp->if_xflags & IFXF_TIMESTAMP_ENABLED) != 0) ||
3928
1
        (cmd == SIOCSIFTIMESTAMPDISABLE &&
3929
1
        (ifp->if_xflags & IFXF_TIMESTAMP_ENABLED) == 0)) {
3930
1
      break;
3931
1
    }
3932
0
    if (cmd == SIOCSIFTIMESTAMPENABLE) {
3933
0
      if_set_xflags(ifp, IFXF_TIMESTAMP_ENABLED);
3934
0
    } else {
3935
0
      if_clear_xflags(ifp, IFXF_TIMESTAMP_ENABLED);
3936
0
    }
3937
    /*
3938
     * Pass the setting to the interface if it supports either
3939
     * software or hardware time stamping
3940
     */
3941
0
    if (ifp->if_capabilities & (IFCAP_HW_TIMESTAMP |
3942
0
        IFCAP_SW_TIMESTAMP)) {
3943
0
      error = ifnet_ioctl(ifp, SOCK_DOM(so), cmd,
3944
0
          (caddr_t)ifr);
3945
0
    }
3946
0
    break;
3947
0
  case SIOCGIFTIMESTAMPENABLED: {
3948
0
    if ((ifp->if_xflags & IFXF_TIMESTAMP_ENABLED) != 0) {
3949
0
      ifr->ifr_intval = 1;
3950
0
    } else {
3951
0
      ifr->ifr_intval = 0;
3952
0
    }
3953
0
    break;
3954
1
  }
3955
1
  case SIOCSQOSMARKINGMODE:
3956
1
    if ((error = priv_check_cred(kauth_cred_get(),
3957
1
        PRIV_NET_INTERFACE_CONTROL, 0)) != 0) {
3958
0
      return error;
3959
0
    }
3960
1
    error = if_set_qosmarking_mode(ifp, ifr->ifr_qosmarking_mode);
3961
1
    break;
3962
3963
1
  case SIOCGQOSMARKINGMODE:
3964
1
    ifr->ifr_qosmarking_mode = ifp->if_qosmarking_mode;
3965
1
    break;
3966
3967
0
  case SIOCSQOSMARKINGENABLED:
3968
0
    if ((error = priv_check_cred(kauth_cred_get(),
3969
0
        PRIV_NET_INTERFACE_CONTROL, 0)) != 0) {
3970
0
      return error;
3971
0
    }
3972
0
    if (ifr->ifr_qosmarking_enabled != 0) {
3973
0
      if_set_eflags(ifp, IFEF_QOSMARKING_ENABLED);
3974
0
    } else {
3975
0
      if_clear_eflags(ifp, IFEF_QOSMARKING_ENABLED);
3976
0
    }
3977
0
    break;
3978
3979
0
  case SIOCGQOSMARKINGENABLED:
3980
0
    ifr->ifr_qosmarking_enabled =
3981
0
        ((ifp->if_eflags & IFEF_QOSMARKING_ENABLED) != 0) ? 1 : 0;
3982
0
    break;
3983
3984
0
  case SIOCSIFDISABLEOUTPUT:
3985
0
#if (DEBUG || DEVELOPMENT)
3986
0
    if (ifr->ifr_disable_output == 1) {
3987
0
      error = ifnet_disable_output(ifp);
3988
0
    } else if (ifr->ifr_disable_output == 0) {
3989
0
      error = ifnet_enable_output(ifp);
3990
0
    } else {
3991
0
      error = EINVAL;
3992
0
    }
3993
#else
3994
    error = EINVAL;
3995
#endif /* (DEBUG || DEVELOPMENT) */
3996
0
    break;
3997
3998
0
  case SIOCSIFSUBFAMILY:
3999
0
    if ((error = priv_check_cred(kauth_cred_get(),
4000
0
        PRIV_NET_INTERFACE_CONTROL, 0)) != 0) {
4001
0
      return error;
4002
0
    }
4003
0
    error = ifnet_ioctl(ifp, SOCK_DOM(so), cmd, (caddr_t)ifr);
4004
0
    break;
4005
4006
2
  case SIOCSIFLOWINTERNET:
4007
2
    if ((error = priv_check_cred(kauth_cred_get(),
4008
2
        PRIV_NET_INTERFACE_CONTROL, 0)) != 0) {
4009
0
      return error;
4010
0
    }
4011
4012
2
    if (ifr->ifr_low_internet & IFRTYPE_LOW_INTERNET_ENABLE_UL) {
4013
0
      if_set_xflags(ifp, IFXF_LOW_INTERNET_UL);
4014
2
    } else {
4015
2
      if_clear_xflags(ifp, IFXF_LOW_INTERNET_UL);
4016
2
    }
4017
2
    if (ifr->ifr_low_internet & IFRTYPE_LOW_INTERNET_ENABLE_DL) {
4018
0
      if_set_xflags(ifp, IFXF_LOW_INTERNET_DL);
4019
2
    } else {
4020
2
      if_clear_xflags(ifp, IFXF_LOW_INTERNET_DL);
4021
2
    }
4022
2
    break;
4023
2
  case SIOCGIFLOWINTERNET:
4024
2
    ifnet_lock_shared(ifp);
4025
2
    ifr->ifr_low_internet = 0;
4026
2
    if ((ifp->if_xflags & IFXF_LOW_INTERNET_UL) != 0) {
4027
0
      ifr->ifr_low_internet |=
4028
0
          IFRTYPE_LOW_INTERNET_ENABLE_UL;
4029
0
    }
4030
2
    if ((ifp->if_xflags & IFXF_LOW_INTERNET_DL) != 0) {
4031
0
      ifr->ifr_low_internet |=
4032
0
          IFRTYPE_LOW_INTERNET_ENABLE_DL;
4033
0
    }
4034
2
    ifnet_lock_done(ifp);
4035
2
    break;
4036
0
  case SIOCGIFLOWPOWER:
4037
0
    ifr->ifr_low_power_mode =
4038
0
        ((ifp->if_xflags & IFXF_LOW_POWER) != 0);
4039
0
    break;
4040
0
  case SIOCSIFLOWPOWER:
4041
0
#if (DEVELOPMENT || DEBUG)
4042
0
    error = if_set_low_power(ifp, (ifr->ifr_low_power_mode != 0));
4043
#else /* DEVELOPMENT || DEBUG */
4044
    error = EOPNOTSUPP;
4045
#endif /* DEVELOPMENT || DEBUG */
4046
0
    break;
4047
4048
0
  case SIOCGIFMPKLOG:
4049
0
    ifr->ifr_mpk_log = ((ifp->if_xflags & IFXF_MPK_LOG) != 0);
4050
0
    break;
4051
0
  case SIOCSIFMPKLOG:
4052
0
    if (ifr->ifr_mpk_log) {
4053
0
      if_set_xflags(ifp, IFXF_MPK_LOG);
4054
0
    } else {
4055
0
      if_clear_xflags(ifp, IFXF_MPK_LOG);
4056
0
    }
4057
0
    break;
4058
0
  case SIOCGIFNOACKPRIO:
4059
0
    if ((ifp->if_eflags & IFEF_NOACKPRI) != 0) {
4060
0
      ifr->ifr_noack_prio = 1;
4061
0
    } else {
4062
0
      ifr->ifr_noack_prio = 0;
4063
0
    }
4064
0
    break;
4065
4066
0
  case SIOCSIFNOACKPRIO:
4067
0
    if ((error = priv_check_cred(kauth_cred_get(),
4068
0
        PRIV_NET_INTERFACE_CONTROL, 0)) != 0) {
4069
0
      return error;
4070
0
    }
4071
0
    if (ifr->ifr_noack_prio) {
4072
0
      if_set_eflags(ifp, IFEF_NOACKPRI);
4073
0
    } else {
4074
0
      if_clear_eflags(ifp, IFEF_NOACKPRI);
4075
0
    }
4076
0
    break;
4077
4078
0
  default:
4079
0
    VERIFY(0);
4080
    /* NOTREACHED */
4081
28.1k
  }
4082
4083
28.1k
  return error;
4084
28.1k
}
4085
4086
int
4087
ifioctllocked(struct socket *so, u_long cmd, caddr_t data, struct proc *p)
4088
92.9k
{
4089
92.9k
  int error;
4090
4091
92.9k
  socket_unlock(so, 0);
4092
92.9k
  error = ifioctl(so, cmd, data, p);
4093
92.9k
  socket_lock(so, 0);
4094
92.9k
  return error;
4095
92.9k
}
4096
4097
/*
4098
 * Set/clear promiscuous mode on interface ifp based on the truth value
4099
 * of pswitch.  The calls are reference counted so that only the first
4100
 * "on" request actually has an effect, as does the final "off" request.
4101
 * Results are undefined if the "off" and "on" requests are not matched.
4102
 */
4103
errno_t
4104
ifnet_set_promiscuous(
4105
  ifnet_t ifp,
4106
  int pswitch)
4107
0
{
4108
0
  int error = 0;
4109
0
  int oldflags = 0;
4110
0
  int newflags = 0;
4111
4112
0
  ifnet_lock_exclusive(ifp);
4113
0
  oldflags = ifp->if_flags;
4114
0
  ifp->if_pcount += pswitch ? 1 : -1;
4115
4116
0
  if (ifp->if_pcount > 0) {
4117
0
    ifp->if_flags |= IFF_PROMISC;
4118
0
  } else {
4119
0
    ifp->if_flags &= ~IFF_PROMISC;
4120
0
  }
4121
4122
0
  newflags = ifp->if_flags;
4123
0
  ifnet_lock_done(ifp);
4124
4125
0
  if (newflags != oldflags && (newflags & IFF_UP) != 0) {
4126
0
    error = ifnet_ioctl(ifp, 0, SIOCSIFFLAGS, NULL);
4127
0
    if (error == 0) {
4128
0
      rt_ifmsg(ifp);
4129
0
    } else {
4130
0
      ifnet_lock_exclusive(ifp);
4131
      // revert the flags
4132
0
      ifp->if_pcount -= pswitch ? 1 : -1;
4133
0
      if (ifp->if_pcount > 0) {
4134
0
        ifp->if_flags |= IFF_PROMISC;
4135
0
      } else {
4136
0
        ifp->if_flags &= ~IFF_PROMISC;
4137
0
      }
4138
0
      ifnet_lock_done(ifp);
4139
0
    }
4140
0
  }
4141
4142
0
  if (newflags != oldflags) {
4143
0
    log(LOG_INFO, "%s: promiscuous mode %s%s\n",
4144
0
        if_name(ifp),
4145
0
        (newflags & IFF_PROMISC) != 0 ? "enable" : "disable",
4146
0
        error != 0 ? " failed" : " succeeded");
4147
0
  }
4148
0
  return error;
4149
0
}
4150
4151
/*
4152
 * Return interface configuration
4153
 * of system.  List may be used
4154
 * in later ioctl's (above) to get
4155
 * other information.
4156
 */
4157
/*ARGSUSED*/
4158
static int
4159
ifconf(u_long cmd, user_addr_t ifrp, int *ret_space)
4160
2.83k
{
4161
2.83k
  struct ifnet *ifp = NULL;
4162
2.83k
  struct ifaddr *ifa;
4163
2.83k
  struct ifreq ifr;
4164
2.83k
  int error = 0;
4165
2.83k
  size_t space;
4166
2.83k
  net_thread_marks_t marks;
4167
4168
2.83k
  marks = net_thread_marks_push(NET_THREAD_CKREQ_LLADDR);
4169
4170
  /*
4171
   * Zero the ifr buffer to make sure we don't
4172
   * disclose the contents of the stack.
4173
   */
4174
2.83k
  bzero(&ifr, sizeof(struct ifreq));
4175
4176
2.83k
  space = *ret_space;
4177
2.83k
  ifnet_head_lock_shared();
4178
15.4k
  for (ifp = ifnet_head.tqh_first; space > sizeof(ifr) &&
4179
13.5k
      ifp; ifp = ifp->if_link.tqe_next) {
4180
13.3k
    char workbuf[64];
4181
13.3k
    size_t ifnlen, addrs;
4182
4183
13.3k
    ifnlen = snprintf(workbuf, sizeof(workbuf),
4184
13.3k
        "%s", if_name(ifp));
4185
13.3k
    if (ifnlen + 1 > sizeof(ifr.ifr_name)) {
4186
0
      error = ENAMETOOLONG;
4187
0
      break;
4188
13.3k
    } else {
4189
13.3k
      strlcpy(ifr.ifr_name, workbuf, IFNAMSIZ);
4190
13.3k
    }
4191
4192
13.3k
    ifnet_lock_shared(ifp);
4193
4194
13.3k
    addrs = 0;
4195
13.3k
    ifa = ifp->if_addrhead.tqh_first;
4196
27.5k
    for (; space > sizeof(ifr) && ifa;
4197
14.9k
        ifa = ifa->ifa_link.tqe_next) {
4198
14.9k
      struct sockaddr *sa;
4199
14.9k
      union {
4200
14.9k
        struct sockaddr sa;
4201
14.9k
        struct sockaddr_dl sdl;
4202
14.9k
        uint8_t buf[SOCK_MAXADDRLEN + 1];
4203
14.9k
      } u;
4204
4205
      /*
4206
       * Make sure to accomodate the largest possible
4207
       * size of SA(if_lladdr)->sa_len.
4208
       */
4209
14.9k
      _CASSERT(sizeof(u) == (SOCK_MAXADDRLEN + 1));
4210
4211
14.9k
      IFA_LOCK(ifa);
4212
14.9k
      sa = ifa->ifa_addr;
4213
14.9k
      addrs++;
4214
4215
14.9k
      if (ifa == ifp->if_lladdr) {
4216
13.3k
        VERIFY(sa->sa_family == AF_LINK);
4217
0
        bcopy(sa, &u, sa->sa_len);
4218
13.3k
        IFA_UNLOCK(ifa);
4219
13.3k
        ifnet_guarded_lladdr_copy_bytes(ifp,
4220
13.3k
            LLADDR(&u.sdl), u.sdl.sdl_alen);
4221
13.3k
        IFA_LOCK(ifa);
4222
13.3k
        sa = &u.sa;
4223
13.3k
      }
4224
4225
14.9k
      if (cmd == OSIOCGIFCONF32 || cmd == OSIOCGIFCONF64) {
4226
5.82k
        struct osockaddr *osa =
4227
5.82k
            (struct osockaddr *)(void *)&ifr.ifr_addr;
4228
5.82k
        ifr.ifr_addr = *sa;
4229
5.82k
        osa->sa_family = sa->sa_family;
4230
5.82k
        error = copyout((caddr_t)&ifr, ifrp,
4231
5.82k
            sizeof(ifr));
4232
5.82k
        ifrp += sizeof(struct ifreq);
4233
9.08k
      } else if (sa->sa_len <= sizeof(*sa)) {
4234
523
        ifr.ifr_addr = *sa;
4235
523
        error = copyout((caddr_t)&ifr, ifrp,
4236
523
            sizeof(ifr));
4237
523
        ifrp += sizeof(struct ifreq);
4238
8.56k
      } else {
4239
8.56k
        if (space <
4240
8.56k
            sizeof(ifr) + sa->sa_len - sizeof(*sa)) {
4241
0
          IFA_UNLOCK(ifa);
4242
0
          break;
4243
0
        }
4244
8.56k
        space -= sa->sa_len - sizeof(*sa);
4245
8.56k
        error = copyout((caddr_t)&ifr, ifrp,
4246
8.56k
            sizeof(ifr.ifr_name));
4247
8.56k
        if (error == 0) {
4248
8.31k
          error = copyout((caddr_t)sa, (ifrp +
4249
8.31k
              offsetof(struct ifreq, ifr_addr)),
4250
8.31k
              sa->sa_len);
4251
8.31k
        }
4252
8.56k
        ifrp += (sa->sa_len + offsetof(struct ifreq,
4253
8.56k
            ifr_addr));
4254
8.56k
      }
4255
14.9k
      IFA_UNLOCK(ifa);
4256
14.9k
      if (error) {
4257
755
        break;
4258
755
      }
4259
14.1k
      space -= sizeof(ifr);
4260
14.1k
    }
4261
13.3k
    ifnet_lock_done(ifp);
4262
4263
13.3k
    if (error) {
4264
755
      break;
4265
755
    }
4266
12.6k
    if (!addrs) {
4267
0
      bzero((caddr_t)&ifr.ifr_addr, sizeof(ifr.ifr_addr));
4268
0
      error = copyout((caddr_t)&ifr, ifrp, sizeof(ifr));
4269
0
      if (error) {
4270
0
        break;
4271
0
      }
4272
0
      space -= sizeof(ifr);
4273
0
      ifrp += sizeof(struct ifreq);
4274
0
    }
4275
12.6k
  }
4276
2.83k
  ifnet_head_done();
4277
2.83k
  *ret_space -= space;
4278
2.83k
  net_thread_marks_pop(marks);
4279
2.83k
  return error;
4280
2.83k
}
4281
4282
/*
4283
 * Just like if_promisc(), but for all-multicast-reception mode.
4284
 */
4285
int
4286
if_allmulti(struct ifnet *ifp, int onswitch)
4287
0
{
4288
0
  int error = 0;
4289
0
  int     modified = 0;
4290
4291
0
  ifnet_lock_exclusive(ifp);
4292
4293
0
  if (onswitch) {
4294
0
    if (ifp->if_amcount++ == 0) {
4295
0
      ifp->if_flags |= IFF_ALLMULTI;
4296
0
      modified = 1;
4297
0
    }
4298
0
  } else {
4299
0
    if (ifp->if_amcount > 1) {
4300
0
      ifp->if_amcount--;
4301
0
    } else {
4302
0
      ifp->if_amcount = 0;
4303
0
      ifp->if_flags &= ~IFF_ALLMULTI;
4304
0
      modified = 1;
4305
0
    }
4306
0
  }
4307
0
  ifnet_lock_done(ifp);
4308
4309
0
  if (modified) {
4310
0
    error = ifnet_ioctl(ifp, 0, SIOCSIFFLAGS, NULL);
4311
0
  }
4312
4313
0
  if (error == 0) {
4314
0
    rt_ifmsg(ifp);
4315
0
  }
4316
0
  return error;
4317
0
}
4318
4319
static struct ifmultiaddr *
4320
ifma_alloc(int how)
4321
5
{
4322
5
  struct ifmultiaddr *ifma;
4323
4324
5
  ifma = (how == M_WAITOK) ? zalloc(ifma_zone) :
4325
5
      zalloc_noblock(ifma_zone);
4326
4327
5
  if (ifma != NULL) {
4328
5
    bzero(ifma, ifma_size);
4329
5
    lck_mtx_init(&ifma->ifma_lock, ifa_mtx_grp, ifa_mtx_attr);
4330
5
    ifma->ifma_debug |= IFD_ALLOC;
4331
5
    if (ifma_debug != 0) {
4332
0
      ifma->ifma_debug |= IFD_DEBUG;
4333
0
      ifma->ifma_trace = ifma_trace;
4334
0
    }
4335
5
  }
4336
5
  return ifma;
4337
5
}
4338
4339
static void
4340
ifma_free(struct ifmultiaddr *ifma)
4341
0
{
4342
0
  IFMA_LOCK(ifma);
4343
4344
0
  if (ifma->ifma_protospec != NULL) {
4345
0
    panic("%s: Protospec not NULL for ifma=%p", __func__, ifma);
4346
    /* NOTREACHED */
4347
0
  } else if ((ifma->ifma_flags & IFMAF_ANONYMOUS) ||
4348
0
      ifma->ifma_anoncnt != 0) {
4349
0
    panic("%s: Freeing ifma=%p with outstanding anon req",
4350
0
        __func__, ifma);
4351
    /* NOTREACHED */
4352
0
  } else if (ifma->ifma_debug & IFD_ATTACHED) {
4353
0
    panic("%s: ifma=%p attached to ifma_ifp=%p is being freed",
4354
0
        __func__, ifma, ifma->ifma_ifp);
4355
    /* NOTREACHED */
4356
0
  } else if (!(ifma->ifma_debug & IFD_ALLOC)) {
4357
0
    panic("%s: ifma %p cannot be freed", __func__, ifma);
4358
    /* NOTREACHED */
4359
0
  } else if (ifma->ifma_refcount != 0) {
4360
0
    panic("%s: non-zero refcount ifma=%p", __func__, ifma);
4361
    /* NOTREACHED */
4362
0
  } else if (ifma->ifma_reqcnt != 0) {
4363
0
    panic("%s: non-zero reqcnt ifma=%p", __func__, ifma);
4364
    /* NOTREACHED */
4365
0
  } else if (ifma->ifma_ifp != NULL) {
4366
0
    panic("%s: non-NULL ifma_ifp=%p for ifma=%p", __func__,
4367
0
        ifma->ifma_ifp, ifma);
4368
    /* NOTREACHED */
4369
0
  } else if (ifma->ifma_ll != NULL) {
4370
0
    panic("%s: non-NULL ifma_ll=%p for ifma=%p", __func__,
4371
0
        ifma->ifma_ll, ifma);
4372
    /* NOTREACHED */
4373
0
  }
4374
0
  ifma->ifma_debug &= ~IFD_ALLOC;
4375
0
  if ((ifma->ifma_debug & (IFD_DEBUG | IFD_TRASHED)) ==
4376
0
      (IFD_DEBUG | IFD_TRASHED)) {
4377
0
    lck_mtx_lock(&ifma_trash_lock);
4378
0
    TAILQ_REMOVE(&ifma_trash_head, (struct ifmultiaddr_dbg *)ifma,
4379
0
        ifma_trash_link);
4380
0
    lck_mtx_unlock(&ifma_trash_lock);
4381
0
    ifma->ifma_debug &= ~IFD_TRASHED;
4382
0
  }
4383
0
  IFMA_UNLOCK(ifma);
4384
4385
0
  if (ifma->ifma_addr != NULL) {
4386
0
    FREE(ifma->ifma_addr, M_IFADDR);
4387
0
    ifma->ifma_addr = NULL;
4388
0
  }
4389
0
  lck_mtx_destroy(&ifma->ifma_lock, ifa_mtx_grp);
4390
0
  zfree(ifma_zone, ifma);
4391
0
}
4392
4393
static void
4394
ifma_trace(struct ifmultiaddr *ifma, int refhold)
4395
0
{
4396
0
  struct ifmultiaddr_dbg *ifma_dbg = (struct ifmultiaddr_dbg *)ifma;
4397
0
  ctrace_t *tr;
4398
0
  u_int32_t idx;
4399
0
  u_int16_t *cnt;
4400
4401
0
  if (!(ifma->ifma_debug & IFD_DEBUG)) {
4402
0
    panic("%s: ifma %p has no debug structure", __func__, ifma);
4403
    /* NOTREACHED */
4404
0
  }
4405
0
  if (refhold) {
4406
0
    cnt = &ifma_dbg->ifma_refhold_cnt;
4407
0
    tr = ifma_dbg->ifma_refhold;
4408
0
  } else {
4409
0
    cnt = &ifma_dbg->ifma_refrele_cnt;
4410
0
    tr = ifma_dbg->ifma_refrele;
4411
0
  }
4412
4413
0
  idx = atomic_add_16_ov(cnt, 1) % IFMA_TRACE_HIST_SIZE;
4414
0
  ctrace_record(&tr[idx]);
4415
0
}
4416
4417
void
4418
ifma_addref(struct ifmultiaddr *ifma, int locked)
4419
15
{
4420
15
  if (!locked) {
4421
0
    IFMA_LOCK(ifma);
4422
15
  } else {
4423
15
    IFMA_LOCK_ASSERT_HELD(ifma);
4424
15
  }
4425
4426
15
  if (++ifma->ifma_refcount == 0) {
4427
0
    panic("%s: ifma=%p wraparound refcnt", __func__, ifma);
4428
    /* NOTREACHED */
4429
15
  } else if (ifma->ifma_trace != NULL) {
4430
0
    (*ifma->ifma_trace)(ifma, TRUE);
4431
0
  }
4432
15
  if (!locked) {
4433
0
    IFMA_UNLOCK(ifma);
4434
0
  }
4435
15
}
4436
4437
void
4438
ifma_remref(struct ifmultiaddr *ifma)
4439
5
{
4440
5
  struct ifmultiaddr *ll;
4441
4442
5
  IFMA_LOCK(ifma);
4443
4444
5
  if (ifma->ifma_refcount == 0) {
4445
0
    panic("%s: ifma=%p negative refcnt", __func__, ifma);
4446
    /* NOTREACHED */
4447
5
  } else if (ifma->ifma_trace != NULL) {
4448
0
    (*ifma->ifma_trace)(ifma, FALSE);
4449
0
  }
4450
4451
5
  --ifma->ifma_refcount;
4452
5
  if (ifma->ifma_refcount > 0) {
4453
5
    IFMA_UNLOCK(ifma);
4454
5
    return;
4455
5
  }
4456
4457
0
  ll = ifma->ifma_ll;
4458
0
  ifma->ifma_ifp = NULL;
4459
0
  ifma->ifma_ll = NULL;
4460
0
  IFMA_UNLOCK(ifma);
4461
0
  ifma_free(ifma);        /* deallocate it */
4462
4463
0
  if (ll != NULL) {
4464
0
    IFMA_REMREF(ll);
4465
0
  }
4466
0
}
4467
4468
static void
4469
if_attach_ifma(struct ifnet *ifp, struct ifmultiaddr *ifma, int anon)
4470
5
{
4471
5
  ifnet_lock_assert(ifp, IFNET_LCK_ASSERT_EXCLUSIVE);
4472
5
  IFMA_LOCK_ASSERT_HELD(ifma);
4473
4474
5
  if (ifma->ifma_ifp != ifp) {
4475
0
    panic("%s: Mismatch ifma_ifp=%p != ifp=%p", __func__,
4476
0
        ifma->ifma_ifp, ifp);
4477
    /* NOTREACHED */
4478
5
  } else if (ifma->ifma_debug & IFD_ATTACHED) {
4479
0
    panic("%s: Attempt to attach an already attached ifma=%p",
4480
0
        __func__, ifma);
4481
    /* NOTREACHED */
4482
5
  } else if (anon && (ifma->ifma_flags & IFMAF_ANONYMOUS)) {
4483
0
    panic("%s: ifma=%p unexpected IFMAF_ANONYMOUS", __func__, ifma);
4484
    /* NOTREACHED */
4485
5
  } else if (ifma->ifma_debug & IFD_TRASHED) {
4486
0
    panic("%s: Attempt to reattach a detached ifma=%p",
4487
0
        __func__, ifma);
4488
    /* NOTREACHED */
4489
0
  }
4490
4491
5
  ifma->ifma_reqcnt++;
4492
5
  VERIFY(ifma->ifma_reqcnt == 1);
4493
5
  IFMA_ADDREF_LOCKED(ifma);
4494
5
  ifma->ifma_debug |= IFD_ATTACHED;
4495
5
  if (anon) {
4496
0
    ifma->ifma_anoncnt++;
4497
0
    VERIFY(ifma->ifma_anoncnt == 1);
4498
0
    ifma->ifma_flags |= IFMAF_ANONYMOUS;
4499
0
  }
4500
4501
5
  LIST_INSERT_HEAD(&ifp->if_multiaddrs, ifma, ifma_link);
4502
5
}
4503
4504
static int
4505
if_detach_ifma(struct ifnet *ifp, struct ifmultiaddr *ifma, int anon)
4506
0
{
4507
0
  ifnet_lock_assert(ifp, IFNET_LCK_ASSERT_EXCLUSIVE);
4508
0
  IFMA_LOCK_ASSERT_HELD(ifma);
4509
4510
0
  if (ifma->ifma_reqcnt == 0) {
4511
0
    panic("%s: ifma=%p negative reqcnt", __func__, ifma);
4512
    /* NOTREACHED */
4513
0
  } else if (anon && !(ifma->ifma_flags & IFMAF_ANONYMOUS)) {
4514
0
    panic("%s: ifma=%p missing IFMAF_ANONYMOUS", __func__, ifma);
4515
    /* NOTREACHED */
4516
0
  } else if (anon && ifma->ifma_anoncnt == 0) {
4517
0
    panic("%s: ifma=%p negative anonreqcnt", __func__, ifma);
4518
    /* NOTREACHED */
4519
0
  } else if (ifma->ifma_ifp != ifp) {
4520
0
    panic("%s: Mismatch ifma_ifp=%p, ifp=%p", __func__,
4521
0
        ifma->ifma_ifp, ifp);
4522
    /* NOTREACHED */
4523
0
  }
4524
4525
0
  if (anon) {
4526
0
    --ifma->ifma_anoncnt;
4527
0
    if (ifma->ifma_anoncnt > 0) {
4528
0
      return 0;
4529
0
    }
4530
0
    ifma->ifma_flags &= ~IFMAF_ANONYMOUS;
4531
0
  }
4532
4533
0
  --ifma->ifma_reqcnt;
4534
0
  if (ifma->ifma_reqcnt > 0) {
4535
0
    return 0;
4536
0
  }
4537
4538
0
  if (ifma->ifma_protospec != NULL) {
4539
0
    panic("%s: Protospec not NULL for ifma=%p", __func__, ifma);
4540
    /* NOTREACHED */
4541
0
  } else if ((ifma->ifma_flags & IFMAF_ANONYMOUS) ||
4542
0
      ifma->ifma_anoncnt != 0) {
4543
0
    panic("%s: Detaching ifma=%p with outstanding anon req",
4544
0
        __func__, ifma);
4545
    /* NOTREACHED */
4546
0
  } else if (!(ifma->ifma_debug & IFD_ATTACHED)) {
4547
0
    panic("%s: Attempt to detach an unattached address ifma=%p",
4548
0
        __func__, ifma);
4549
    /* NOTREACHED */
4550
0
  } else if (ifma->ifma_debug & IFD_TRASHED) {
4551
0
    panic("%s: ifma %p is already in trash list", __func__, ifma);
4552
    /* NOTREACHED */
4553
0
  }
4554
4555
  /*
4556
   * NOTE: Caller calls IFMA_REMREF
4557
   */
4558
0
  ifma->ifma_debug &= ~IFD_ATTACHED;
4559
0
  LIST_REMOVE(ifma, ifma_link);
4560
0
  if (LIST_EMPTY(&ifp->if_multiaddrs)) {
4561
0
    ifp->if_updatemcasts = 0;
4562
0
  }
4563
4564
0
  if (ifma->ifma_debug & IFD_DEBUG) {
4565
    /* Become a regular mutex, just in case */
4566
0
    IFMA_CONVERT_LOCK(ifma);
4567
0
    lck_mtx_lock(&ifma_trash_lock);
4568
0
    TAILQ_INSERT_TAIL(&ifma_trash_head,
4569
0
        (struct ifmultiaddr_dbg *)ifma, ifma_trash_link);
4570
0
    lck_mtx_unlock(&ifma_trash_lock);
4571
0
    ifma->ifma_debug |= IFD_TRASHED;
4572
0
  }
4573
4574
0
  return 1;
4575
0
}
4576
4577
/*
4578
 * Find an ifmultiaddr that matches a socket address on an interface.
4579
 *
4580
 * Caller is responsible for holding the ifnet_lock while calling
4581
 * this function.
4582
 */
4583
static int
4584
if_addmulti_doesexist(struct ifnet *ifp, const struct sockaddr *sa,
4585
    struct ifmultiaddr **retifma, int anon)
4586
10
{
4587
10
  struct ifmultiaddr *ifma;
4588
4589
30
  for (ifma = LIST_FIRST(&ifp->if_multiaddrs); ifma != NULL;
4590
20
      ifma = LIST_NEXT(ifma, ifma_link)) {
4591
20
    IFMA_LOCK_SPIN(ifma);
4592
20
    if (!ifa_equal(sa, ifma->ifma_addr)) {
4593
20
      IFMA_UNLOCK(ifma);
4594
20
      continue;
4595
20
    }
4596
0
    if (anon) {
4597
0
      VERIFY(!(ifma->ifma_flags & IFMAF_ANONYMOUS) ||
4598
0
          ifma->ifma_anoncnt != 0);
4599
0
      VERIFY((ifma->ifma_flags & IFMAF_ANONYMOUS) ||
4600
0
          ifma->ifma_anoncnt == 0);
4601
0
      ifma->ifma_anoncnt++;
4602
0
      if (!(ifma->ifma_flags & IFMAF_ANONYMOUS)) {
4603
0
        VERIFY(ifma->ifma_anoncnt == 1);
4604
0
        ifma->ifma_flags |= IFMAF_ANONYMOUS;
4605
0
      }
4606
0
    }
4607
0
    if (!anon || ifma->ifma_anoncnt == 1) {
4608
0
      ifma->ifma_reqcnt++;
4609
0
      VERIFY(ifma->ifma_reqcnt > 1);
4610
0
    }
4611
0
    if (retifma != NULL) {
4612
0
      *retifma = ifma;
4613
0
      IFMA_ADDREF_LOCKED(ifma);
4614
0
    }
4615
0
    IFMA_UNLOCK(ifma);
4616
0
    return 0;
4617
20
  }
4618
10
  return ENOENT;
4619
10
}
4620
4621
/*
4622
 * Radar 3642395, make sure all multicasts are in a standard format.
4623
 */
4624
static struct sockaddr *
4625
copy_and_normalize(const struct sockaddr *original)
4626
7
{
4627
7
  int                     alen = 0;
4628
7
  const u_char            *aptr = NULL;
4629
7
  struct sockaddr         *copy = NULL;
4630
7
  struct sockaddr_dl      *sdl_new = NULL;
4631
7
  int                     len = 0;
4632
4633
7
  if (original->sa_family != AF_LINK &&
4634
7
      original->sa_family != AF_UNSPEC) {
4635
    /* Just make a copy */
4636
5
    MALLOC(copy, struct sockaddr *, original->sa_len,
4637
5
        M_IFADDR, M_WAITOK);
4638
5
    if (copy != NULL) {
4639
5
      bcopy(original, copy, original->sa_len);
4640
5
    }
4641
5
    return copy;
4642
5
  }
4643
4644
2
  switch (original->sa_family) {
4645
0
  case AF_LINK: {
4646
0
    const struct sockaddr_dl *sdl_original =
4647
0
        (struct sockaddr_dl *)(uintptr_t)(size_t)original;
4648
4649
0
    if (sdl_original->sdl_nlen + sdl_original->sdl_alen +
4650
0
        sdl_original->sdl_slen +
4651
0
        offsetof(struct sockaddr_dl, sdl_data) >
4652
0
        sdl_original->sdl_len) {
4653
0
      return NULL;
4654
0
    }
4655
4656
0
    alen = sdl_original->sdl_alen;
4657
0
    aptr = CONST_LLADDR(sdl_original);
4658
0
  }
4659
0
  break;
4660
4661
2
  case AF_UNSPEC: {
4662
2
    if (original->sa_len < ETHER_ADDR_LEN +
4663
2
        offsetof(struct sockaddr, sa_data)) {
4664
0
      return NULL;
4665
0
    }
4666
4667
2
    alen = ETHER_ADDR_LEN;
4668
2
    aptr = (const u_char *)original->sa_data;
4669
2
  }
4670
0
  break;
4671
2
  }
4672
4673
2
  if (alen == 0 || aptr == NULL) {
4674
0
    return NULL;
4675
0
  }
4676
4677
2
  len = alen + offsetof(struct sockaddr_dl, sdl_data);
4678
2
  MALLOC(sdl_new, struct sockaddr_dl *, len, M_IFADDR, M_WAITOK);
4679
4680
2
  if (sdl_new != NULL) {
4681
2
    bzero(sdl_new, len);
4682
2
    sdl_new->sdl_len = len;
4683
2
    sdl_new->sdl_family = AF_LINK;
4684
2
    sdl_new->sdl_alen = alen;
4685
2
    bcopy(aptr, LLADDR(sdl_new), alen);
4686
2
  }
4687
4688
2
  return (struct sockaddr *)sdl_new;
4689
2
}
4690
4691
/*
4692
 * Network-layer protocol domains which hold references to the underlying
4693
 * link-layer record must use this routine.
4694
 */
4695
int
4696
if_addmulti(struct ifnet *ifp, const struct sockaddr *sa,
4697
    struct ifmultiaddr **retifma)
4698
5
{
4699
5
  return if_addmulti_common(ifp, sa, retifma, 0);
4700
5
}
4701
4702
/*
4703
 * Anything other than network-layer protocol domains which hold references
4704
 * to the underlying link-layer record must use this routine: SIOCADDMULTI
4705
 * ioctl, ifnet_add_multicast(), if_bond.
4706
 */
4707
int
4708
if_addmulti_anon(struct ifnet *ifp, const struct sockaddr *sa,
4709
    struct ifmultiaddr **retifma)
4710
0
{
4711
0
  return if_addmulti_common(ifp, sa, retifma, 1);
4712
0
}
4713
4714
/*
4715
 * Register an additional multicast address with a network interface.
4716
 *
4717
 * - If the address is already present, bump the reference count on the
4718
 *   address and return.
4719
 * - If the address is not link-layer, look up a link layer address.
4720
 * - Allocate address structures for one or both addresses, and attach to the
4721
 *   multicast address list on the interface.  If automatically adding a link
4722
 *   layer address, the protocol address will own a reference to the link
4723
 *   layer address, to be freed when it is freed.
4724
 * - Notify the network device driver of an addition to the multicast address
4725
 *   list.
4726
 *
4727
 * 'sa' points to caller-owned memory with the desired multicast address.
4728
 *
4729
 * 'retifma' will be used to return a pointer to the resulting multicast
4730
 * address reference, if desired.
4731
 *
4732
 * 'anon' indicates a link-layer address with no protocol address reference
4733
 * made to it.  Anything other than network-layer protocol domain requests
4734
 * are considered as anonymous.
4735
 */
4736
static int
4737
if_addmulti_common(struct ifnet *ifp, const struct sockaddr *sa,
4738
    struct ifmultiaddr **retifma, int anon)
4739
5
{
4740
5
  struct sockaddr_storage storage;
4741
5
  struct sockaddr *llsa = NULL;
4742
5
  struct sockaddr *dupsa = NULL;
4743
5
  int error = 0, ll_firstref = 0, lladdr;
4744
5
  struct ifmultiaddr *ifma = NULL;
4745
5
  struct ifmultiaddr *llifma = NULL;
4746
4747
  /* Only AF_UNSPEC/AF_LINK is allowed for an "anonymous" address */
4748
5
  VERIFY(!anon || sa->sa_family == AF_UNSPEC ||
4749
5
      sa->sa_family == AF_LINK);
4750
4751
  /* If sa is a AF_LINK or AF_UNSPEC, duplicate and normalize it */
4752
5
  if (sa->sa_family == AF_LINK || sa->sa_family == AF_UNSPEC) {
4753
0
    dupsa = copy_and_normalize(sa);
4754
0
    if (dupsa == NULL) {
4755
0
      error = ENOMEM;
4756
0
      goto cleanup;
4757
0
    }
4758
0
    sa = dupsa;
4759
0
  }
4760
4761
5
  ifnet_lock_exclusive(ifp);
4762
5
  if (!(ifp->if_flags & IFF_MULTICAST)) {
4763
0
    error = EADDRNOTAVAIL;
4764
0
    ifnet_lock_done(ifp);
4765
0
    goto cleanup;
4766
0
  }
4767
4768
  /* If the address is already present, return a new reference to it */
4769
5
  error = if_addmulti_doesexist(ifp, sa, retifma, anon);
4770
5
  ifnet_lock_done(ifp);
4771
5
  if (error == 0) {
4772
0
    goto cleanup;
4773
0
  }
4774
4775
  /*
4776
   * The address isn't already present; give the link layer a chance
4777
   * to accept/reject it, and also find out which AF_LINK address this
4778
   * maps to, if it isn't one already.
4779
   */
4780
5
  error = dlil_resolve_multi(ifp, sa, (struct sockaddr *)&storage,
4781
5
      sizeof(storage));
4782
5
  if (error == 0 && storage.ss_len != 0) {
4783
0
    llsa = copy_and_normalize((struct sockaddr *)&storage);
4784
0
    if (llsa == NULL) {
4785
0
      error = ENOMEM;
4786
0
      goto cleanup;
4787
0
    }
4788
4789
0
    llifma = ifma_alloc(M_WAITOK);
4790
0
    if (llifma == NULL) {
4791
0
      error = ENOMEM;
4792
0
      goto cleanup;
4793
0
    }
4794
0
  }
4795
4796
  /* to be similar to FreeBSD */
4797
5
  if (error == EOPNOTSUPP) {
4798
5
    error = 0;
4799
5
  } else if (error != 0) {
4800
0
    goto cleanup;
4801
0
  }
4802
4803
  /* Allocate while we aren't holding any locks */
4804
5
  if (dupsa == NULL) {
4805
5
    dupsa = copy_and_normalize(sa);
4806
5
    if (dupsa == NULL) {
4807
0
      error = ENOMEM;
4808
0
      goto cleanup;
4809
0
    }
4810
5
  }
4811
5
  ifma = ifma_alloc(M_WAITOK);
4812
5
  if (ifma == NULL) {
4813
0
    error = ENOMEM;
4814
0
    goto cleanup;
4815
0
  }
4816
4817
5
  ifnet_lock_exclusive(ifp);
4818
  /*
4819
   * Check again for the matching multicast.
4820
   */
4821
5
  error = if_addmulti_doesexist(ifp, sa, retifma, anon);
4822
5
  if (error == 0) {
4823
0
    ifnet_lock_done(ifp);
4824
0
    goto cleanup;
4825
0
  }
4826
4827
5
  if (llifma != NULL) {
4828
0
    VERIFY(!anon);  /* must not get here if "anonymous" */
4829
0
    if (if_addmulti_doesexist(ifp, llsa, &ifma->ifma_ll, 0) == 0) {
4830
0
      FREE(llsa, M_IFADDR);
4831
0
      llsa = NULL;
4832
0
      ifma_free(llifma);
4833
0
      llifma = NULL;
4834
0
      VERIFY(ifma->ifma_ll->ifma_ifp == ifp);
4835
0
    } else {
4836
0
      ll_firstref = 1;
4837
0
      llifma->ifma_addr = llsa;
4838
0
      llifma->ifma_ifp = ifp;
4839
0
      IFMA_LOCK(llifma);
4840
0
      if_attach_ifma(ifp, llifma, 0);
4841
      /* add extra refcnt for ifma */
4842
0
      IFMA_ADDREF_LOCKED(llifma);
4843
0
      IFMA_UNLOCK(llifma);
4844
0
      ifma->ifma_ll = llifma;
4845
0
    }
4846
0
  }
4847
4848
  /* "anonymous" request should not result in network address */
4849
5
  VERIFY(!anon || ifma->ifma_ll == NULL);
4850
4851
0
  ifma->ifma_addr = dupsa;
4852
5
  ifma->ifma_ifp = ifp;
4853
5
  IFMA_LOCK(ifma);
4854
5
  if_attach_ifma(ifp, ifma, anon);
4855
5
  IFMA_ADDREF_LOCKED(ifma);               /* for this routine */
4856
5
  if (retifma != NULL) {
4857
5
    *retifma = ifma;
4858
5
    IFMA_ADDREF_LOCKED(*retifma);   /* for caller */
4859
5
  }
4860
5
  lladdr = (ifma->ifma_addr->sa_family == AF_UNSPEC ||
4861
5
      ifma->ifma_addr->sa_family == AF_LINK);
4862
5
  IFMA_UNLOCK(ifma);
4863
5
  ifnet_lock_done(ifp);
4864
4865
5
  rt_newmaddrmsg(RTM_NEWMADDR, ifma);
4866
5
  IFMA_REMREF(ifma);                      /* for this routine */
4867
4868
  /*
4869
   * We are certain we have added something, so call down to the
4870
   * interface to let them know about it.  Do this only for newly-
4871
   * added AF_LINK/AF_UNSPEC address in the if_multiaddrs set.
4872
   */
4873
5
  if (lladdr || ll_firstref) {
4874
0
    (void) ifnet_ioctl(ifp, 0, SIOCADDMULTI, NULL);
4875
0
  }
4876
4877
5
  if (ifp->if_updatemcasts > 0) {
4878
0
    ifp->if_updatemcasts = 0;
4879
0
  }
4880
4881
5
  return 0;
4882
4883
0
cleanup:
4884
0
  if (ifma != NULL) {
4885
0
    ifma_free(ifma);
4886
0
  }
4887
0
  if (dupsa != NULL) {
4888
0
    FREE(dupsa, M_IFADDR);
4889
0
  }
4890
0
  if (llifma != NULL) {
4891
0
    ifma_free(llifma);
4892
0
  }
4893
0
  if (llsa != NULL) {
4894
0
    FREE(llsa, M_IFADDR);
4895
0
  }
4896
4897
0
  return error;
4898
5
}
4899
4900
/*
4901
 * Delete a multicast group membership by network-layer group address.
4902
 * This routine is deprecated.
4903
 */
4904
int
4905
if_delmulti(struct ifnet *ifp, const struct sockaddr *sa)
4906
0
{
4907
0
  return if_delmulti_common(NULL, ifp, sa, 0);
4908
0
}
4909
4910
/*
4911
 * Delete a multicast group membership by group membership pointer.
4912
 * Network-layer protocol domains must use this routine.
4913
 */
4914
int
4915
if_delmulti_ifma(struct ifmultiaddr *ifma)
4916
0
{
4917
0
  return if_delmulti_common(ifma, NULL, NULL, 0);
4918
0
}
4919
4920
/*
4921
 * Anything other than network-layer protocol domains which hold references
4922
 * to the underlying link-layer record must use this routine: SIOCDELMULTI
4923
 * ioctl, ifnet_remove_multicast(), if_bond.
4924
 */
4925
int
4926
if_delmulti_anon(struct ifnet *ifp, const struct sockaddr *sa)
4927
2
{
4928
2
  return if_delmulti_common(NULL, ifp, sa, 1);
4929
2
}
4930
4931
/*
4932
 * Delete a multicast group membership by network-layer group address.
4933
 *
4934
 * Returns ENOENT if the entry could not be found.
4935
 */
4936
static int
4937
if_delmulti_common(struct ifmultiaddr *ifma, struct ifnet *ifp,
4938
    const struct sockaddr *sa, int anon)
4939
2
{
4940
2
  struct sockaddr         *dupsa = NULL;
4941
2
  int                     lastref, ll_lastref = 0, lladdr;
4942
2
  struct ifmultiaddr      *ll = NULL;
4943
4944
  /* sanity check for callers */
4945
2
  VERIFY(ifma != NULL || (ifp != NULL && sa != NULL));
4946
4947
2
  if (ifma != NULL) {
4948
0
    ifp = ifma->ifma_ifp;
4949
0
  }
4950
4951
2
  if (sa != NULL &&
4952
2
      (sa->sa_family == AF_LINK || sa->sa_family == AF_UNSPEC)) {
4953
2
    dupsa = copy_and_normalize(sa);
4954
2
    if (dupsa == NULL) {
4955
0
      return ENOMEM;
4956
0
    }
4957
2
    sa = dupsa;
4958
2
  }
4959
4960
2
  ifnet_lock_exclusive(ifp);
4961
2
  if (ifma == NULL) {
4962
12
    for (ifma = LIST_FIRST(&ifp->if_multiaddrs); ifma != NULL;
4963
10
        ifma = LIST_NEXT(ifma, ifma_link)) {
4964
10
      IFMA_LOCK(ifma);
4965
10
      if (!ifa_equal(sa, ifma->ifma_addr) ||
4966
10
          (anon && !(ifma->ifma_flags & IFMAF_ANONYMOUS))) {
4967
10
        VERIFY(!(ifma->ifma_flags & IFMAF_ANONYMOUS) ||
4968
10
            ifma->ifma_anoncnt != 0);
4969
10
        IFMA_UNLOCK(ifma);
4970
10
        continue;
4971
10
      }
4972
      /* found; keep it locked */
4973
0
      break;
4974
10
    }
4975
2
    if (ifma == NULL) {
4976
2
      if (dupsa != NULL) {
4977
2
        FREE(dupsa, M_IFADDR);
4978
2
      }
4979
2
      ifnet_lock_done(ifp);
4980
2
      return ENOENT;
4981
2
    }
4982
2
  } else {
4983
0
    IFMA_LOCK(ifma);
4984
0
  }
4985
0
  IFMA_LOCK_ASSERT_HELD(ifma);
4986
0
  IFMA_ADDREF_LOCKED(ifma);       /* for this routine */
4987
0
  lastref = if_detach_ifma(ifp, ifma, anon);
4988
0
  VERIFY(!lastref || (!(ifma->ifma_debug & IFD_ATTACHED) &&
4989
0
      ifma->ifma_reqcnt == 0));
4990
0
  VERIFY(!anon || ifma->ifma_ll == NULL);
4991
0
  ll = ifma->ifma_ll;
4992
0
  lladdr = (ifma->ifma_addr->sa_family == AF_UNSPEC ||
4993
0
      ifma->ifma_addr->sa_family == AF_LINK);
4994
0
  IFMA_UNLOCK(ifma);
4995
0
  if (lastref && ll != NULL) {
4996
0
    IFMA_LOCK(ll);
4997
0
    ll_lastref = if_detach_ifma(ifp, ll, 0);
4998
0
    IFMA_UNLOCK(ll);
4999
0
  }
5000
0
  ifnet_lock_done(ifp);
5001
5002
0
  if (lastref) {
5003
0
    rt_newmaddrmsg(RTM_DELMADDR, ifma);
5004
0
  }
5005
5006
0
  if ((ll == NULL && lastref && lladdr) || ll_lastref) {
5007
    /*
5008
     * Make sure the interface driver is notified in the
5009
     * case of a link layer mcast group being left.  Do
5010
     * this only for a AF_LINK/AF_UNSPEC address that has
5011
     * been removed from the if_multiaddrs set.
5012
     */
5013
0
    ifnet_ioctl(ifp, 0, SIOCDELMULTI, NULL);
5014
0
  }
5015
5016
0
  if (lastref) {
5017
0
    IFMA_REMREF(ifma);      /* for if_multiaddrs list */
5018
0
  }
5019
0
  if (ll_lastref) {
5020
0
    IFMA_REMREF(ll);        /* for if_multiaddrs list */
5021
0
  }
5022
0
  IFMA_REMREF(ifma);              /* for this routine */
5023
0
  if (dupsa != NULL) {
5024
0
    FREE(dupsa, M_IFADDR);
5025
0
  }
5026
5027
0
  return 0;
5028
2
}
5029
5030
/*
5031
 * Shutdown all network activity.  Used boot() when halting
5032
 * system.
5033
 */
5034
int
5035
if_down_all(void)
5036
0
{
5037
0
  struct ifnet **ifp;
5038
0
  u_int32_t       count;
5039
0
  u_int32_t       i;
5040
5041
0
  if (ifnet_list_get_all(IFNET_FAMILY_ANY, &ifp, &count) == 0) {
5042
0
    for (i = 0; i < count; i++) {
5043
0
      if_down(ifp[i]);
5044
0
      dlil_proto_unplumb_all(ifp[i]);
5045
0
    }
5046
0
    ifnet_list_free(ifp);
5047
0
  }
5048
5049
0
  return 0;
5050
0
}
5051
5052
/*
5053
 * Delete Routes for a Network Interface
5054
 *
5055
 * Called for each routing entry via the rnh->rnh_walktree() call above
5056
 * to delete all route entries referencing a detaching network interface.
5057
 *
5058
 * Arguments:
5059
 *  rn  pointer to node in the routing table
5060
 *  arg argument passed to rnh->rnh_walktree() - detaching interface
5061
 *
5062
 * Returns:
5063
 *  0 successful
5064
 *  errno failed - reason indicated
5065
 *
5066
 */
5067
static int
5068
if_rtdel(struct radix_node *rn, void *arg)
5069
0
{
5070
0
  struct rtentry  *rt = (struct rtentry *)rn;
5071
0
  struct ifnet    *ifp = arg;
5072
0
  int             err;
5073
5074
0
  if (rt == NULL) {
5075
0
    return 0;
5076
0
  }
5077
  /*
5078
   * Checking against RTF_UP protects against walktree
5079
   * recursion problems with cloned routes.
5080
   */
5081
0
  RT_LOCK(rt);
5082
0
  if (rt->rt_ifp == ifp && (rt->rt_flags & RTF_UP)) {
5083
    /*
5084
     * Safe to drop rt_lock and use rt_key, rt_gateway,
5085
     * since holding rnh_lock here prevents another thread
5086
     * from calling rt_setgate() on this route.
5087
     */
5088
0
    RT_UNLOCK(rt);
5089
0
    err = rtrequest_locked(RTM_DELETE, rt_key(rt), rt->rt_gateway,
5090
0
        rt_mask(rt), rt->rt_flags, NULL);
5091
0
    if (err) {
5092
0
      log(LOG_WARNING, "if_rtdel: error %d\n", err);
5093
0
    }
5094
0
  } else {
5095
0
    RT_UNLOCK(rt);
5096
0
  }
5097
0
  return 0;
5098
0
}
5099
5100
/*
5101
 * Removes routing table reference to a given interface
5102
 * for a given protocol family
5103
 */
5104
void
5105
if_rtproto_del(struct ifnet *ifp, int protocol)
5106
0
{
5107
0
  struct radix_node_head  *rnh;
5108
5109
0
  if ((protocol <= AF_MAX) && (protocol >= 0) &&
5110
0
      ((rnh = rt_tables[protocol]) != NULL) && (ifp != NULL)) {
5111
0
    lck_mtx_lock(rnh_lock);
5112
0
    (void) rnh->rnh_walktree(rnh, if_rtdel, ifp);
5113
0
    lck_mtx_unlock(rnh_lock);
5114
0
  }
5115
0
}
5116
5117
static int
5118
if_rtmtu(struct radix_node *rn, void *arg)
5119
5
{
5120
5
  struct rtentry *rt = (struct rtentry *)rn;
5121
5
  struct ifnet *ifp = arg;
5122
5123
5
  RT_LOCK(rt);
5124
5
  if (rt->rt_ifp == ifp) {
5125
    /*
5126
     * Update the MTU of this entry only if the MTU
5127
     * has not been locked (RTV_MTU is not set) and
5128
     * if it was non-zero to begin with.
5129
     */
5130
5
    if (!(rt->rt_rmx.rmx_locks & RTV_MTU) && rt->rt_rmx.rmx_mtu) {
5131
5
      rt->rt_rmx.rmx_mtu = ifp->if_mtu;
5132
5
      if (rt_key(rt)->sa_family == AF_INET &&
5133
1
          INTF_ADJUST_MTU_FOR_CLAT46(ifp)) {
5134
0
        rt->rt_rmx.rmx_mtu = IN6_LINKMTU(ifp);
5135
        /* Further adjust the size for CLAT46 expansion */
5136
0
        rt->rt_rmx.rmx_mtu -= CLAT46_HDR_EXPANSION_OVERHD;
5137
0
      }
5138
5
    }
5139
5
  }
5140
5
  RT_UNLOCK(rt);
5141
5142
5
  return 0;
5143
5
}
5144
5145
/*
5146
 * Update the MTU metric of all route entries in all protocol tables
5147
 * associated with a particular interface; this is called when the
5148
 * MTU of that interface has changed.
5149
 */
5150
static void
5151
if_rtmtu_update(struct ifnet *ifp)
5152
1
{
5153
1
  struct radix_node_head *rnh;
5154
1
  int p;
5155
5156
43
  for (p = 0; p < AF_MAX + 1; p++) {
5157
42
    if ((rnh = rt_tables[p]) == NULL) {
5158
40
      continue;
5159
40
    }
5160
5161
2
    lck_mtx_lock(rnh_lock);
5162
2
    (void) rnh->rnh_walktree(rnh, if_rtmtu, ifp);
5163
2
    lck_mtx_unlock(rnh_lock);
5164
2
  }
5165
1
  routegenid_update();
5166
1
}
5167
5168
__private_extern__ void
5169
if_data_internal_to_if_data(struct ifnet *ifp,
5170
    const struct if_data_internal *if_data_int, struct if_data *if_data)
5171
0
{
5172
0
#pragma unused(ifp)
5173
0
#define COPYFIELD(fld)          if_data->fld = if_data_int->fld
5174
0
#define COPYFIELD32(fld)        if_data->fld = (u_int32_t)(if_data_int->fld)
5175
/* compiler will cast down to 32-bit */
5176
0
#define COPYFIELD32_ATOMIC(fld) do {                                    \
5177
0
  atomic_get_64(if_data->fld,                                     \
5178
0
      (u_int64_t *)(void *)(uintptr_t)&if_data_int->fld);         \
5179
0
} while (0)
5180
5181
0
  COPYFIELD(ifi_type);
5182
0
  COPYFIELD(ifi_typelen);
5183
0
  COPYFIELD(ifi_physical);
5184
0
  COPYFIELD(ifi_addrlen);
5185
0
  COPYFIELD(ifi_hdrlen);
5186
0
  COPYFIELD(ifi_recvquota);
5187
0
  COPYFIELD(ifi_xmitquota);
5188
0
  if_data->ifi_unused1 = 0;
5189
0
  COPYFIELD(ifi_mtu);
5190
0
  COPYFIELD(ifi_metric);
5191
0
  if (if_data_int->ifi_baudrate & 0xFFFFFFFF00000000LL) {
5192
0
    if_data->ifi_baudrate = 0xFFFFFFFF;
5193
0
  } else {
5194
0
    COPYFIELD32(ifi_baudrate);
5195
0
  }
5196
5197
0
  COPYFIELD32_ATOMIC(ifi_ipackets);
5198
0
  COPYFIELD32_ATOMIC(ifi_ierrors);
5199
0
  COPYFIELD32_ATOMIC(ifi_opackets);
5200
0
  COPYFIELD32_ATOMIC(ifi_oerrors);
5201
0
  COPYFIELD32_ATOMIC(ifi_collisions);
5202
0
  COPYFIELD32_ATOMIC(ifi_ibytes);
5203
0
  COPYFIELD32_ATOMIC(ifi_obytes);
5204
0
  COPYFIELD32_ATOMIC(ifi_imcasts);
5205
0
  COPYFIELD32_ATOMIC(ifi_omcasts);
5206
0
  COPYFIELD32_ATOMIC(ifi_iqdrops);
5207
0
  COPYFIELD32_ATOMIC(ifi_noproto);
5208
5209
0
  COPYFIELD(ifi_recvtiming);
5210
0
  COPYFIELD(ifi_xmittiming);
5211
5212
0
  if_data->ifi_lastchange.tv_sec = if_data_int->ifi_lastchange.tv_sec;
5213
0
  if_data->ifi_lastchange.tv_usec = if_data_int->ifi_lastchange.tv_usec;
5214
5215
0
  if_data->ifi_lastchange.tv_sec += boottime_sec();
5216
5217
0
  if_data->ifi_unused2 = 0;
5218
0
  COPYFIELD(ifi_hwassist);
5219
0
  if_data->ifi_reserved1 = 0;
5220
0
  if_data->ifi_reserved2 = 0;
5221
0
#undef COPYFIELD32_ATOMIC
5222
0
#undef COPYFIELD32
5223
0
#undef COPYFIELD
5224
0
}
5225
5226
__private_extern__ void
5227
if_data_internal_to_if_data64(struct ifnet *ifp,
5228
    const struct if_data_internal *if_data_int,
5229
    struct if_data64 *if_data64)
5230
0
{
5231
0
#pragma unused(ifp)
5232
0
#define COPYFIELD64(fld)        if_data64->fld = if_data_int->fld
5233
0
#define COPYFIELD64_ATOMIC(fld) do {                                    \
5234
0
  atomic_get_64(if_data64->fld,                                   \
5235
0
      (u_int64_t *)(void *)(uintptr_t)&if_data_int->fld);         \
5236
0
} while (0)
5237
5238
0
  COPYFIELD64(ifi_type);
5239
0
  COPYFIELD64(ifi_typelen);
5240
0
  COPYFIELD64(ifi_physical);
5241
0
  COPYFIELD64(ifi_addrlen);
5242
0
  COPYFIELD64(ifi_hdrlen);
5243
0
  COPYFIELD64(ifi_recvquota);
5244
0
  COPYFIELD64(ifi_xmitquota);
5245
0
  if_data64->ifi_unused1 = 0;
5246
0
  COPYFIELD64(ifi_mtu);
5247
0
  COPYFIELD64(ifi_metric);
5248
0
  COPYFIELD64(ifi_baudrate);
5249
5250
0
  COPYFIELD64_ATOMIC(ifi_ipackets);
5251
0
  COPYFIELD64_ATOMIC(ifi_ierrors);
5252
0
  COPYFIELD64_ATOMIC(ifi_opackets);
5253
0
  COPYFIELD64_ATOMIC(ifi_oerrors);
5254
0
  COPYFIELD64_ATOMIC(ifi_collisions);
5255
0
  COPYFIELD64_ATOMIC(ifi_ibytes);
5256
0
  COPYFIELD64_ATOMIC(ifi_obytes);
5257
0
  COPYFIELD64_ATOMIC(ifi_imcasts);
5258
0
  COPYFIELD64_ATOMIC(ifi_omcasts);
5259
0
  COPYFIELD64_ATOMIC(ifi_iqdrops);
5260
0
  COPYFIELD64_ATOMIC(ifi_noproto);
5261
5262
  /*
5263
   * Note these two fields are actually 32 bit, so doing
5264
   * COPYFIELD64_ATOMIC will cause them to be misaligned
5265
   */
5266
0
  COPYFIELD64(ifi_recvtiming);
5267
0
  COPYFIELD64(ifi_xmittiming);
5268
5269
0
  if_data64->ifi_lastchange.tv_sec = if_data_int->ifi_lastchange.tv_sec;
5270
0
  if_data64->ifi_lastchange.tv_usec = if_data_int->ifi_lastchange.tv_usec;
5271
5272
0
  if_data64->ifi_lastchange.tv_sec += boottime_sec();
5273
5274
0
#undef COPYFIELD64
5275
0
}
5276
5277
__private_extern__ void
5278
if_copy_traffic_class(struct ifnet *ifp,
5279
    struct if_traffic_class *if_tc)
5280
0
{
5281
0
#define COPY_IF_TC_FIELD64_ATOMIC(fld) do {                     \
5282
0
  atomic_get_64(if_tc->fld,                               \
5283
0
      (u_int64_t *)(void *)(uintptr_t)&ifp->if_tc.fld);   \
5284
0
} while (0)
5285
5286
0
  bzero(if_tc, sizeof(*if_tc));
5287
0
  COPY_IF_TC_FIELD64_ATOMIC(ifi_ibepackets);
5288
0
  COPY_IF_TC_FIELD64_ATOMIC(ifi_ibebytes);
5289
0
  COPY_IF_TC_FIELD64_ATOMIC(ifi_obepackets);
5290
0
  COPY_IF_TC_FIELD64_ATOMIC(ifi_obebytes);
5291
0
  COPY_IF_TC_FIELD64_ATOMIC(ifi_ibkpackets);
5292
0
  COPY_IF_TC_FIELD64_ATOMIC(ifi_ibkbytes);
5293
0
  COPY_IF_TC_FIELD64_ATOMIC(ifi_obkpackets);
5294
0
  COPY_IF_TC_FIELD64_ATOMIC(ifi_obkbytes);
5295
0
  COPY_IF_TC_FIELD64_ATOMIC(ifi_ivipackets);
5296
0
  COPY_IF_TC_FIELD64_ATOMIC(ifi_ivibytes);
5297
0
  COPY_IF_TC_FIELD64_ATOMIC(ifi_ovipackets);
5298
0
  COPY_IF_TC_FIELD64_ATOMIC(ifi_ovibytes);
5299
0
  COPY_IF_TC_FIELD64_ATOMIC(ifi_ivopackets);
5300
0
  COPY_IF_TC_FIELD64_ATOMIC(ifi_ivobytes);
5301
0
  COPY_IF_TC_FIELD64_ATOMIC(ifi_ovopackets);
5302
0
  COPY_IF_TC_FIELD64_ATOMIC(ifi_ovobytes);
5303
0
  COPY_IF_TC_FIELD64_ATOMIC(ifi_ipvpackets);
5304
0
  COPY_IF_TC_FIELD64_ATOMIC(ifi_ipvbytes);
5305
0
  COPY_IF_TC_FIELD64_ATOMIC(ifi_opvpackets);
5306
0
  COPY_IF_TC_FIELD64_ATOMIC(ifi_opvbytes);
5307
5308
0
#undef COPY_IF_TC_FIELD64_ATOMIC
5309
0
}
5310
5311
void
5312
if_copy_data_extended(struct ifnet *ifp, struct if_data_extended *if_de)
5313
0
{
5314
0
#define COPY_IF_DE_FIELD64_ATOMIC(fld) do {                     \
5315
0
  atomic_get_64(if_de->fld,                               \
5316
0
      (u_int64_t *)(void *)(uintptr_t)&ifp->if_data.fld); \
5317
0
} while (0)
5318
5319
0
  bzero(if_de, sizeof(*if_de));
5320
0
  COPY_IF_DE_FIELD64_ATOMIC(ifi_alignerrs);
5321
0
  COPY_IF_DE_FIELD64_ATOMIC(ifi_dt_bytes);
5322
0
  COPY_IF_DE_FIELD64_ATOMIC(ifi_fpackets);
5323
0
  COPY_IF_DE_FIELD64_ATOMIC(ifi_fbytes);
5324
5325
0
#undef COPY_IF_DE_FIELD64_ATOMIC
5326
0
}
5327
5328
void
5329
if_copy_packet_stats(struct ifnet *ifp, struct if_packet_stats *if_ps)
5330
0
{
5331
0
#define COPY_IF_PS_TCP_FIELD64_ATOMIC(fld) do {                         \
5332
0
  atomic_get_64(if_ps->ifi_tcp_##fld,                             \
5333
0
      (u_int64_t *)(void *)(uintptr_t)&ifp->if_tcp_stat->fld);    \
5334
0
} while (0)
5335
5336
0
#define COPY_IF_PS_UDP_FIELD64_ATOMIC(fld) do {                         \
5337
0
  atomic_get_64(if_ps->ifi_udp_##fld,                             \
5338
0
      (u_int64_t *)(void *)(uintptr_t)&ifp->if_udp_stat->fld);    \
5339
0
} while (0)
5340
5341
0
  COPY_IF_PS_TCP_FIELD64_ATOMIC(badformat);
5342
0
  COPY_IF_PS_TCP_FIELD64_ATOMIC(unspecv6);
5343
0
  COPY_IF_PS_TCP_FIELD64_ATOMIC(synfin);
5344
0
  COPY_IF_PS_TCP_FIELD64_ATOMIC(badformatipsec);
5345
0
  COPY_IF_PS_TCP_FIELD64_ATOMIC(noconnnolist);
5346
0
  COPY_IF_PS_TCP_FIELD64_ATOMIC(noconnlist);
5347
0
  COPY_IF_PS_TCP_FIELD64_ATOMIC(listbadsyn);
5348
0
  COPY_IF_PS_TCP_FIELD64_ATOMIC(icmp6unreach);
5349
0
  COPY_IF_PS_TCP_FIELD64_ATOMIC(deprecate6);
5350
0
  COPY_IF_PS_TCP_FIELD64_ATOMIC(ooopacket);
5351
0
  COPY_IF_PS_TCP_FIELD64_ATOMIC(rstinsynrcv);
5352
0
  COPY_IF_PS_TCP_FIELD64_ATOMIC(dospacket);
5353
0
  COPY_IF_PS_TCP_FIELD64_ATOMIC(cleanup);
5354
0
  COPY_IF_PS_TCP_FIELD64_ATOMIC(synwindow);
5355
5356
0
  COPY_IF_PS_UDP_FIELD64_ATOMIC(port_unreach);
5357
0
  COPY_IF_PS_UDP_FIELD64_ATOMIC(faithprefix);
5358
0
  COPY_IF_PS_UDP_FIELD64_ATOMIC(port0);
5359
0
  COPY_IF_PS_UDP_FIELD64_ATOMIC(badlength);
5360
0
  COPY_IF_PS_UDP_FIELD64_ATOMIC(badchksum);
5361
0
  COPY_IF_PS_UDP_FIELD64_ATOMIC(badmcast);
5362
0
  COPY_IF_PS_UDP_FIELD64_ATOMIC(cleanup);
5363
0
  COPY_IF_PS_UDP_FIELD64_ATOMIC(badipsec);
5364
5365
0
#undef COPY_IF_PS_TCP_FIELD64_ATOMIC
5366
0
#undef COPY_IF_PS_UDP_FIELD64_ATOMIC
5367
0
}
5368
5369
void
5370
if_copy_rxpoll_stats(struct ifnet *ifp, struct if_rxpoll_stats *if_rs)
5371
0
{
5372
0
  bzero(if_rs, sizeof(*if_rs));
5373
0
  if (!(ifp->if_eflags & IFEF_RXPOLL) || !ifnet_is_attached(ifp, 1)) {
5374
0
    return;
5375
0
  }
5376
0
  bcopy(&ifp->if_poll_pstats, if_rs, sizeof(*if_rs));
5377
  /* Release the IO refcnt */
5378
0
  ifnet_decr_iorefcnt(ifp);
5379
0
}
5380
5381
void
5382
if_copy_netif_stats(struct ifnet *ifp, struct if_netif_stats *if_ns)
5383
0
{
5384
0
  bzero(if_ns, sizeof(*if_ns));
5385
0
#pragma unused(ifp)
5386
0
}
5387
5388
struct ifaddr *
5389
ifa_remref(struct ifaddr *ifa, int locked)
5390
544k
{
5391
544k
  if (!locked) {
5392
544k
    IFA_LOCK_SPIN(ifa);
5393
544k
  } else {
5394
0
    IFA_LOCK_ASSERT_HELD(ifa);
5395
0
  }
5396
5397
544k
  if (ifa->ifa_refcnt == 0) {
5398
0
    panic("%s: ifa %p negative refcnt\n", __func__, ifa);
5399
544k
  } else if (ifa->ifa_trace != NULL) {
5400
0
    (*ifa->ifa_trace)(ifa, FALSE);
5401
0
  }
5402
544k
  if (--ifa->ifa_refcnt == 0) {
5403
0
    if (ifa->ifa_debug & IFD_ATTACHED) {
5404
0
      panic("ifa %p attached to ifp is being freed\n", ifa);
5405
0
    }
5406
    /*
5407
     * Some interface addresses are allocated either statically
5408
     * or carved out of a larger block.  Only free it if it was
5409
     * allocated via MALLOC or via the corresponding per-address
5410
     * family allocator.  Otherwise, leave it alone.
5411
     */
5412
0
    if (ifa->ifa_debug & IFD_ALLOC) {
5413
0
      if (ifa->ifa_free == NULL) {
5414
0
        IFA_UNLOCK(ifa);
5415
0
        FREE(ifa, M_IFADDR);
5416
0
      } else {
5417
        /* Become a regular mutex */
5418
0
        IFA_CONVERT_LOCK(ifa);
5419
        /* callee will unlock */
5420
0
        (*ifa->ifa_free)(ifa);
5421
0
      }
5422
0
    } else {
5423
0
      IFA_UNLOCK(ifa);
5424
0
    }
5425
0
    ifa = NULL;
5426
0
  }
5427
5428
544k
  if (!locked && ifa != NULL) {
5429
544k
    IFA_UNLOCK(ifa);
5430
544k
  }
5431
5432
544k
  return ifa;
5433
544k
}
5434
5435
void
5436
ifa_addref(struct ifaddr *ifa, int locked)
5437
544k
{
5438
544k
  if (!locked) {
5439
95.9k
    IFA_LOCK_SPIN(ifa);
5440
448k
  } else {
5441
448k
    IFA_LOCK_ASSERT_HELD(ifa);
5442
448k
  }
5443
5444
544k
  if (++ifa->ifa_refcnt == 0) {
5445
0
    panic("%s: ifa %p wraparound refcnt\n", __func__, ifa);
5446
    /* NOTREACHED */
5447
544k
  } else if (ifa->ifa_trace != NULL) {
5448
0
    (*ifa->ifa_trace)(ifa, TRUE);
5449
0
  }
5450
544k
  if (!locked) {
5451
95.9k
    IFA_UNLOCK(ifa);
5452
95.9k
  }
5453
544k
}
5454
5455
void
5456
ifa_lock_init(struct ifaddr *ifa)
5457
49
{
5458
49
  lck_mtx_init(&ifa->ifa_lock, ifa_mtx_grp, ifa_mtx_attr);
5459
49
}
5460
5461
void
5462
ifa_lock_destroy(struct ifaddr *ifa)
5463
0
{
5464
0
  IFA_LOCK_ASSERT_NOTHELD(ifa);
5465
0
  lck_mtx_destroy(&ifa->ifa_lock, ifa_mtx_grp);
5466
0
}
5467
5468
/*
5469
 * 'i' group ioctls.
5470
 *
5471
 * The switch statement below does nothing at runtime, as it serves as a
5472
 * compile time check to ensure that all of the socket 'i' ioctls (those
5473
 * in the 'i' group going thru soo_ioctl) that are made available by the
5474
 * networking stack is unique.  This works as long as this routine gets
5475
 * updated each time a new interface ioctl gets added.
5476
 *
5477
 * Any failures at compile time indicates duplicated ioctl values.
5478
 */
5479
static __attribute__((unused)) void
5480
ifioctl_cassert(void)
5481
0
{
5482
0
  /*
5483
0
   * This is equivalent to _CASSERT() and the compiler wouldn't
5484
0
   * generate any instructions, thus for compile time only.
5485
0
   */
5486
0
  switch ((u_long)0) {
5487
0
  case 0:
5488
0
5489
0
  /* bsd/net/if_ppp.h */
5490
0
  case SIOCGPPPSTATS:
5491
0
  case SIOCGPPPCSTATS:
5492
0
5493
0
  /* bsd/netinet6/in6_var.h */
5494
0
  case SIOCSIFADDR_IN6:
5495
0
  case SIOCGIFADDR_IN6:
5496
0
  case SIOCSIFDSTADDR_IN6:
5497
0
  case SIOCSIFNETMASK_IN6:
5498
0
  case SIOCGIFDSTADDR_IN6:
5499
0
  case SIOCGIFNETMASK_IN6:
5500
0
  case SIOCDIFADDR_IN6:
5501
0
  case SIOCAIFADDR_IN6_32:
5502
0
  case SIOCAIFADDR_IN6_64:
5503
0
  case SIOCSIFPHYADDR_IN6_32:
5504
0
  case SIOCSIFPHYADDR_IN6_64:
5505
0
  case SIOCGIFPSRCADDR_IN6:
5506
0
  case SIOCGIFPDSTADDR_IN6:
5507
0
  case SIOCGIFAFLAG_IN6:
5508
0
  case SIOCGDRLST_IN6_32:
5509
0
  case SIOCGDRLST_IN6_64:
5510
0
  case SIOCGPRLST_IN6_32:
5511
0
  case SIOCGPRLST_IN6_64:
5512
0
  case OSIOCGIFINFO_IN6:
5513
0
  case SIOCGIFINFO_IN6:
5514
0
  case SIOCSNDFLUSH_IN6:
5515
0
  case SIOCGNBRINFO_IN6_32:
5516
0
  case SIOCGNBRINFO_IN6_64:
5517
0
  case SIOCSPFXFLUSH_IN6:
5518
0
  case SIOCSRTRFLUSH_IN6:
5519
0
  case SIOCGIFALIFETIME_IN6:
5520
0
  case SIOCSIFALIFETIME_IN6:
5521
0
  case SIOCGIFSTAT_IN6:
5522
0
  case SIOCGIFSTAT_ICMP6:
5523
0
  case SIOCSDEFIFACE_IN6_32:
5524
0
  case SIOCSDEFIFACE_IN6_64:
5525
0
  case SIOCGDEFIFACE_IN6_32:
5526
0
  case SIOCGDEFIFACE_IN6_64:
5527
0
  case SIOCSIFINFO_FLAGS:
5528
0
  case SIOCSSCOPE6:
5529
0
  case SIOCGSCOPE6:
5530
0
  case SIOCGSCOPE6DEF:
5531
0
  case SIOCSIFPREFIX_IN6:
5532
0
  case SIOCGIFPREFIX_IN6:
5533
0
  case SIOCDIFPREFIX_IN6:
5534
0
  case SIOCAIFPREFIX_IN6:
5535
0
  case SIOCCIFPREFIX_IN6:
5536
0
  case SIOCSGIFPREFIX_IN6:
5537
0
  case SIOCPROTOATTACH_IN6_32:
5538
0
  case SIOCPROTOATTACH_IN6_64:
5539
0
  case SIOCPROTODETACH_IN6:
5540
0
  case SIOCLL_START_32:
5541
0
  case SIOCLL_START_64:
5542
0
  case SIOCLL_STOP:
5543
0
  case SIOCAUTOCONF_START:
5544
0
  case SIOCAUTOCONF_STOP:
5545
0
  case SIOCSETROUTERMODE_IN6:
5546
0
  case SIOCGETROUTERMODE_IN6:
5547
0
  case SIOCLL_CGASTART_32:
5548
0
  case SIOCLL_CGASTART_64:
5549
0
  case SIOCGIFCGAPREP_IN6:
5550
0
  case SIOCSIFCGAPREP_IN6:
5551
0
5552
0
  /* bsd/sys/sockio.h */
5553
0
  case SIOCSIFADDR:
5554
0
  case OSIOCGIFADDR:
5555
0
  case SIOCSIFDSTADDR:
5556
0
  case OSIOCGIFDSTADDR:
5557
0
  case SIOCSIFFLAGS:
5558
0
  case SIOCGIFFLAGS:
5559
0
  case OSIOCGIFBRDADDR:
5560
0
  case SIOCSIFBRDADDR:
5561
0
  case OSIOCGIFCONF32:
5562
0
  case OSIOCGIFCONF64:
5563
0
  case OSIOCGIFNETMASK:
5564
0
  case SIOCSIFNETMASK:
5565
0
  case SIOCGIFMETRIC:
5566
0
  case SIOCSIFMETRIC:
5567
0
  case SIOCDIFADDR:
5568
0
  case SIOCAIFADDR:
5569
0
5570
0
  case SIOCGIFADDR:
5571
0
  case SIOCGIFDSTADDR:
5572
0
  case SIOCGIFBRDADDR:
5573
0
  case SIOCGIFCONF32:
5574
0
  case SIOCGIFCONF64:
5575
0
  case SIOCGIFNETMASK:
5576
0
  case SIOCAUTOADDR:
5577
0
  case SIOCAUTONETMASK:
5578
0
  case SIOCARPIPLL:
5579
0
5580
0
  case SIOCADDMULTI:
5581
0
  case SIOCDELMULTI:
5582
0
  case SIOCGIFMTU:
5583
0
  case SIOCSIFMTU:
5584
0
  case SIOCGIFPHYS:
5585
0
  case SIOCSIFPHYS:
5586
0
  case SIOCSIFMEDIA:
5587
0
  case SIOCGIFMEDIA32:
5588
0
  case SIOCGIFMEDIA64:
5589
0
  case SIOCGIFXMEDIA32:
5590
0
  case SIOCGIFXMEDIA64:
5591
0
  case SIOCSIFGENERIC:
5592
0
  case SIOCGIFGENERIC:
5593
0
  case SIOCRSLVMULTI:
5594
0
5595
0
  case SIOCSIFLLADDR:
5596
0
  case SIOCGIFSTATUS:
5597
0
  case SIOCSIFPHYADDR:
5598
0
  case SIOCGIFPSRCADDR:
5599
0
  case SIOCGIFPDSTADDR:
5600
0
  case SIOCDIFPHYADDR:
5601
0
5602
0
  case SIOCGIFDEVMTU:
5603
0
  case SIOCSIFALTMTU:
5604
0
  case SIOCGIFALTMTU:
5605
0
  case SIOCSIFBOND:
5606
0
  case SIOCGIFBOND:
5607
0
5608
0
  case SIOCPROTOATTACH:
5609
0
  case SIOCPROTODETACH:
5610
0
5611
0
  case SIOCSIFCAP:
5612
0
  case SIOCGIFCAP:
5613
0
5614
0
  case SIOCIFCREATE:
5615
0
  case SIOCIFDESTROY:
5616
0
  case SIOCIFCREATE2:
5617
0
5618
0
  case SIOCSDRVSPEC32:
5619
0
  case SIOCGDRVSPEC32:
5620
0
  case SIOCSDRVSPEC64:
5621
0
  case SIOCGDRVSPEC64:
5622
0
5623
0
  case SIOCSIFVLAN:
5624
0
  case SIOCGIFVLAN:
5625
0
5626
0
  case SIOCIFGCLONERS32:
5627
0
  case SIOCIFGCLONERS64:
5628
0
5629
0
  case SIOCGIFASYNCMAP:
5630
0
  case SIOCSIFASYNCMAP:
5631
0
  case SIOCSIFKPI:
5632
0
  case SIOCGIFKPI:
5633
0
5634
0
  case SIOCGIFWAKEFLAGS:
5635
0
5636
0
  case SIOCGIFGETRTREFCNT:
5637
0
  case SIOCGIFLINKQUALITYMETRIC:
5638
0
  case SIOCSIFOPPORTUNISTIC:
5639
0
  case SIOCGIFOPPORTUNISTIC:
5640
0
  case SIOCGETROUTERMODE:
5641
0
  case SIOCSETROUTERMODE:
5642
0
  case SIOCGIFEFLAGS:
5643
0
  case SIOCSIFDESC:
5644
0
  case SIOCGIFDESC:
5645
0
  case SIOCSIFLINKPARAMS:
5646
0
  case SIOCGIFLINKPARAMS:
5647
0
  case SIOCGIFQUEUESTATS:
5648
0
  case SIOCSIFTHROTTLE:
5649
0
  case SIOCGIFTHROTTLE:
5650
0
5651
0
  case SIOCGASSOCIDS32:
5652
0
  case SIOCGASSOCIDS64:
5653
0
  case SIOCGCONNIDS32:
5654
0
  case SIOCGCONNIDS64:
5655
0
  case SIOCGCONNINFO32:
5656
0
  case SIOCGCONNINFO64:
5657
0
  case SIOCSCONNORDER:
5658
0
  case SIOCGCONNORDER:
5659
0
5660
0
  case SIOCSIFLOG:
5661
0
  case SIOCGIFLOG:
5662
0
  case SIOCGIFDELEGATE:
5663
0
  case SIOCGIFLLADDR:
5664
0
  case SIOCGIFTYPE:
5665
0
  case SIOCGIFEXPENSIVE:
5666
0
  case SIOCSIFEXPENSIVE:
5667
0
  case SIOCGIF2KCL:
5668
0
  case SIOCSIF2KCL:
5669
0
  case SIOCGSTARTDELAY:
5670
0
5671
0
  case SIOCAIFAGENTID:
5672
0
  case SIOCDIFAGENTID:
5673
0
  case SIOCGIFAGENTIDS32:
5674
0
  case SIOCGIFAGENTIDS64:
5675
0
  case SIOCGIFAGENTDATA32:
5676
0
  case SIOCGIFAGENTDATA64:
5677
0
5678
0
  case SIOCSIFINTERFACESTATE:
5679
0
  case SIOCGIFINTERFACESTATE:
5680
0
  case SIOCSIFPROBECONNECTIVITY:
5681
0
  case SIOCGIFPROBECONNECTIVITY:
5682
0
5683
0
  case SIOCGIFFUNCTIONALTYPE:
5684
0
  case SIOCSIFNETSIGNATURE:
5685
0
  case SIOCGIFNETSIGNATURE:
5686
0
5687
0
  case SIOCSIFNETWORKID:
5688
0
  case SIOCGECNMODE:
5689
0
  case SIOCSECNMODE:
5690
0
5691
0
  case SIOCSIFORDER:
5692
0
5693
0
  case SIOCSQOSMARKINGMODE:
5694
0
  case SIOCSQOSMARKINGENABLED:
5695
0
  case SIOCGQOSMARKINGMODE:
5696
0
  case SIOCGQOSMARKINGENABLED:
5697
0
5698
0
  case SIOCSIFTIMESTAMPENABLE:
5699
0
  case SIOCSIFTIMESTAMPDISABLE:
5700
0
  case SIOCGIFTIMESTAMPENABLED:
5701
0
5702
0
  case SIOCSIFDISABLEOUTPUT:
5703
0
5704
0
  case SIOCSIFSUBFAMILY:
5705
0
5706
0
  case SIOCGIFAGENTLIST32:
5707
0
  case SIOCGIFAGENTLIST64:
5708
0
5709
0
  case SIOCSIFLOWINTERNET:
5710
0
  case SIOCGIFLOWINTERNET:
5711
0
5712
0
  case SIOCGIFNAT64PREFIX:
5713
0
  case SIOCSIFNAT64PREFIX:
5714
0
5715
0
  case SIOCGIFCLAT46ADDR:
5716
0
5717
0
  case SIOCGIFPROTOLIST32:
5718
0
  case SIOCGIFPROTOLIST64:
5719
0
5720
0
  case SIOCSIF6LOWPAN:
5721
0
  case SIOCGIF6LOWPAN:
5722
0
5723
0
  case SIOCGIFLOWPOWER:
5724
0
  case SIOCSIFLOWPOWER:
5725
0
5726
0
  case SIOCGIFMPKLOG:
5727
0
  case SIOCSIFMPKLOG:
5728
0
5729
0
  case SIOCGIFCONSTRAINED:
5730
0
  case SIOCSIFCONSTRAINED:
5731
0
5732
0
  case SIOCGIFXFLAGS:
5733
0
5734
0
  case SIOCGIFNOACKPRIO:
5735
0
  case SIOCSIFNOACKPRIO:
5736
0
    ;
5737
0
  }
5738
0
}
5739
5740
uint32_t
5741
ifnet_mbuf_packetpreamblelen(struct ifnet *ifp)
5742
0
{
5743
0
#pragma unused(ifp)
5744
0
  return 0;
5745
0
}
5746
5747
/* The following is used to enqueue work items for interface events */
5748
struct intf_event {
5749
  struct ifnet *ifp;
5750
  union sockaddr_in_4_6 addr;
5751
  uint32_t intf_event_code;
5752
};
5753
5754
static void
5755
intf_event_callback(void *arg)
5756
0
{
5757
0
  struct intf_event *p_intf_ev = (struct intf_event *)arg;
5758
5759
  /* Call this before we walk the tree */
5760
0
  EVENTHANDLER_INVOKE(&ifnet_evhdlr_ctxt, ifnet_event, p_intf_ev->ifp,
5761
0
      (struct sockaddr *)&(p_intf_ev->addr), p_intf_ev->intf_event_code);
5762
0
}
5763
5764
struct intf_event_nwk_wq_entry {
5765
  struct nwk_wq_entry nwk_wqe;
5766
  struct intf_event intf_ev_arg;
5767
};
5768
5769
void
5770
intf_event_enqueue_nwk_wq_entry(struct ifnet *ifp, struct sockaddr *addrp,
5771
    uint32_t intf_event_code)
5772
0
{
5773
0
#pragma unused(addrp)
5774
0
  struct intf_event_nwk_wq_entry *p_intf_ev = NULL;
5775
5776
0
  MALLOC(p_intf_ev, struct intf_event_nwk_wq_entry *,
5777
0
      sizeof(struct intf_event_nwk_wq_entry),
5778
0
      M_NWKWQ, M_WAITOK | M_ZERO);
5779
5780
0
  p_intf_ev->intf_ev_arg.ifp = ifp;
5781
  /*
5782
   * XXX Not using addr in the arg. This will be used
5783
   * once we need IP address add/delete events
5784
   */
5785
0
  p_intf_ev->intf_ev_arg.intf_event_code = intf_event_code;
5786
0
  p_intf_ev->nwk_wqe.func = intf_event_callback;
5787
0
  p_intf_ev->nwk_wqe.is_arg_managed = TRUE;
5788
0
  p_intf_ev->nwk_wqe.arg = &p_intf_ev->intf_ev_arg;
5789
0
  nwk_wq_enqueue((struct nwk_wq_entry*)p_intf_ev);
5790
0
}
5791
5792
int
5793
if_get_tcp_kao_max(struct ifnet *ifp)
5794
0
{
5795
0
  int error = 0;
5796
5797
0
  if (ifp->if_tcp_kao_max == 0) {
5798
0
    struct ifreq ifr;
5799
5800
0
    memset(&ifr, 0, sizeof(struct ifreq));
5801
0
    error = ifnet_ioctl(ifp, 0, SIOCGIFTCPKAOMAX, &ifr);
5802
5803
0
    ifnet_lock_exclusive(ifp);
5804
0
    if (error == 0) {
5805
0
      ifp->if_tcp_kao_max = ifr.ifr_tcp_kao_max;
5806
0
    } else if (error == EOPNOTSUPP) {
5807
0
      ifp->if_tcp_kao_max = default_tcp_kao_max;
5808
0
    }
5809
0
    ifnet_lock_done(ifp);
5810
0
  }
5811
0
  return error;
5812
0
}