Coverage Report

Created: 2026-06-25 06:11

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