]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/net/if.c
Widen ifnet_detach_sxlock coverage
[FreeBSD/FreeBSD.git] / sys / net / if.c
1 /*-
2  * SPDX-License-Identifier: BSD-3-Clause
3  *
4  * Copyright (c) 1980, 1986, 1993
5  *      The Regents of the University of California.  All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  * 3. Neither the name of the University nor the names of its contributors
16  *    may be used to endorse or promote products derived from this software
17  *    without specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29  * SUCH DAMAGE.
30  *
31  *      @(#)if.c        8.5 (Berkeley) 1/9/95
32  * $FreeBSD$
33  */
34
35 #include "opt_bpf.h"
36 #include "opt_inet6.h"
37 #include "opt_inet.h"
38
39 #include <sys/param.h>
40 #include <sys/conf.h>
41 #include <sys/eventhandler.h>
42 #include <sys/malloc.h>
43 #include <sys/domainset.h>
44 #include <sys/sbuf.h>
45 #include <sys/bus.h>
46 #include <sys/epoch.h>
47 #include <sys/mbuf.h>
48 #include <sys/systm.h>
49 #include <sys/priv.h>
50 #include <sys/proc.h>
51 #include <sys/socket.h>
52 #include <sys/socketvar.h>
53 #include <sys/protosw.h>
54 #include <sys/kernel.h>
55 #include <sys/lock.h>
56 #include <sys/refcount.h>
57 #include <sys/module.h>
58 #include <sys/rwlock.h>
59 #include <sys/sockio.h>
60 #include <sys/syslog.h>
61 #include <sys/sysctl.h>
62 #include <sys/sysent.h>
63 #include <sys/taskqueue.h>
64 #include <sys/domain.h>
65 #include <sys/jail.h>
66 #include <sys/priv.h>
67
68 #include <machine/stdarg.h>
69 #include <vm/uma.h>
70
71 #include <net/bpf.h>
72 #include <net/ethernet.h>
73 #include <net/if.h>
74 #include <net/if_arp.h>
75 #include <net/if_clone.h>
76 #include <net/if_dl.h>
77 #include <net/if_types.h>
78 #include <net/if_var.h>
79 #include <net/if_media.h>
80 #include <net/if_vlan_var.h>
81 #include <net/radix.h>
82 #include <net/route.h>
83 #include <net/route/route_ctl.h>
84 #include <net/vnet.h>
85
86 #if defined(INET) || defined(INET6)
87 #include <net/ethernet.h>
88 #include <netinet/in.h>
89 #include <netinet/in_var.h>
90 #include <netinet/ip.h>
91 #include <netinet/ip_carp.h>
92 #ifdef INET
93 #include <net/debugnet.h>
94 #include <netinet/if_ether.h>
95 #endif /* INET */
96 #ifdef INET6
97 #include <netinet6/in6_var.h>
98 #include <netinet6/in6_ifattach.h>
99 #endif /* INET6 */
100 #endif /* INET || INET6 */
101
102 #include <security/mac/mac_framework.h>
103
104 /*
105  * Consumers of struct ifreq such as tcpdump assume no pad between ifr_name
106  * and ifr_ifru when it is used in SIOCGIFCONF.
107  */
108 _Static_assert(sizeof(((struct ifreq *)0)->ifr_name) ==
109     offsetof(struct ifreq, ifr_ifru), "gap between ifr_name and ifr_ifru");
110
111 __read_mostly epoch_t net_epoch_preempt;
112 #ifdef COMPAT_FREEBSD32
113 #include <sys/mount.h>
114 #include <compat/freebsd32/freebsd32.h>
115
116 struct ifreq_buffer32 {
117         uint32_t        length;         /* (size_t) */
118         uint32_t        buffer;         /* (void *) */
119 };
120
121 /*
122  * Interface request structure used for socket
123  * ioctl's.  All interface ioctl's must have parameter
124  * definitions which begin with ifr_name.  The
125  * remainder may be interface specific.
126  */
127 struct ifreq32 {
128         char    ifr_name[IFNAMSIZ];             /* if name, e.g. "en0" */
129         union {
130                 struct sockaddr ifru_addr;
131                 struct sockaddr ifru_dstaddr;
132                 struct sockaddr ifru_broadaddr;
133                 struct ifreq_buffer32 ifru_buffer;
134                 short           ifru_flags[2];
135                 short           ifru_index;
136                 int             ifru_jid;
137                 int             ifru_metric;
138                 int             ifru_mtu;
139                 int             ifru_phys;
140                 int             ifru_media;
141                 uint32_t        ifru_data;
142                 int             ifru_cap[2];
143                 u_int           ifru_fib;
144                 u_char          ifru_vlan_pcp;
145         } ifr_ifru;
146 };
147 CTASSERT(sizeof(struct ifreq) == sizeof(struct ifreq32));
148 CTASSERT(__offsetof(struct ifreq, ifr_ifru) ==
149     __offsetof(struct ifreq32, ifr_ifru));
150
151 struct ifgroupreq32 {
152         char    ifgr_name[IFNAMSIZ];
153         u_int   ifgr_len;
154         union {
155                 char            ifgru_group[IFNAMSIZ];
156                 uint32_t        ifgru_groups;
157         } ifgr_ifgru;
158 };
159
160 struct ifmediareq32 {
161         char            ifm_name[IFNAMSIZ];
162         int             ifm_current;
163         int             ifm_mask;
164         int             ifm_status;
165         int             ifm_active;
166         int             ifm_count;
167         uint32_t        ifm_ulist;      /* (int *) */
168 };
169 #define SIOCGIFMEDIA32  _IOC_NEWTYPE(SIOCGIFMEDIA, struct ifmediareq32)
170 #define SIOCGIFXMEDIA32 _IOC_NEWTYPE(SIOCGIFXMEDIA, struct ifmediareq32)
171
172 #define _CASE_IOC_IFGROUPREQ_32(cmd)                            \
173     _IOC_NEWTYPE((cmd), struct ifgroupreq32): case
174 #else /* !COMPAT_FREEBSD32 */
175 #define _CASE_IOC_IFGROUPREQ_32(cmd)
176 #endif /* !COMPAT_FREEBSD32 */
177
178 #define CASE_IOC_IFGROUPREQ(cmd)        \
179     _CASE_IOC_IFGROUPREQ_32(cmd)        \
180     (cmd)
181
182 union ifreq_union {
183         struct ifreq    ifr;
184 #ifdef COMPAT_FREEBSD32
185         struct ifreq32  ifr32;
186 #endif
187 };
188
189 union ifgroupreq_union {
190         struct ifgroupreq ifgr;
191 #ifdef COMPAT_FREEBSD32
192         struct ifgroupreq32 ifgr32;
193 #endif
194 };
195
196 SYSCTL_NODE(_net, PF_LINK, link, CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
197     "Link layers");
198 SYSCTL_NODE(_net_link, 0, generic, CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
199     "Generic link-management");
200
201 SYSCTL_INT(_net_link, OID_AUTO, ifqmaxlen, CTLFLAG_RDTUN,
202     &ifqmaxlen, 0, "max send queue size");
203
204 /* Log link state change events */
205 static int log_link_state_change = 1;
206
207 SYSCTL_INT(_net_link, OID_AUTO, log_link_state_change, CTLFLAG_RW,
208         &log_link_state_change, 0,
209         "log interface link state change events");
210
211 /* Log promiscuous mode change events */
212 static int log_promisc_mode_change = 1;
213
214 SYSCTL_INT(_net_link, OID_AUTO, log_promisc_mode_change, CTLFLAG_RDTUN,
215         &log_promisc_mode_change, 1,
216         "log promiscuous mode change events");
217
218 /* Interface description */
219 static unsigned int ifdescr_maxlen = 1024;
220 SYSCTL_UINT(_net, OID_AUTO, ifdescr_maxlen, CTLFLAG_RW,
221         &ifdescr_maxlen, 0,
222         "administrative maximum length for interface description");
223
224 static MALLOC_DEFINE(M_IFDESCR, "ifdescr", "ifnet descriptions");
225
226 /* global sx for non-critical path ifdescr */
227 static struct sx ifdescr_sx;
228 SX_SYSINIT(ifdescr_sx, &ifdescr_sx, "ifnet descr");
229
230 void    (*ng_ether_link_state_p)(struct ifnet *ifp, int state);
231 void    (*lagg_linkstate_p)(struct ifnet *ifp, int state);
232 /* These are external hooks for CARP. */
233 void    (*carp_linkstate_p)(struct ifnet *ifp);
234 void    (*carp_demote_adj_p)(int, char *);
235 int     (*carp_master_p)(struct ifaddr *);
236 #if defined(INET) || defined(INET6)
237 int     (*carp_forus_p)(struct ifnet *ifp, u_char *dhost);
238 int     (*carp_output_p)(struct ifnet *ifp, struct mbuf *m,
239     const struct sockaddr *sa);
240 int     (*carp_ioctl_p)(struct ifreq *, u_long, struct thread *);   
241 int     (*carp_attach_p)(struct ifaddr *, int);
242 void    (*carp_detach_p)(struct ifaddr *, bool);
243 #endif
244 #ifdef INET
245 int     (*carp_iamatch_p)(struct ifaddr *, uint8_t **);
246 #endif
247 #ifdef INET6
248 struct ifaddr *(*carp_iamatch6_p)(struct ifnet *ifp, struct in6_addr *taddr6);
249 caddr_t (*carp_macmatch6_p)(struct ifnet *ifp, struct mbuf *m,
250     const struct in6_addr *taddr);
251 #endif
252
253 struct mbuf *(*tbr_dequeue_ptr)(struct ifaltq *, int) = NULL;
254
255 /*
256  * XXX: Style; these should be sorted alphabetically, and unprototyped
257  * static functions should be prototyped. Currently they are sorted by
258  * declaration order.
259  */
260 static void     if_attachdomain(void *);
261 static void     if_attachdomain1(struct ifnet *);
262 static int      ifconf(u_long, caddr_t);
263 static void     *if_grow(void);
264 static void     if_input_default(struct ifnet *, struct mbuf *);
265 static int      if_requestencap_default(struct ifnet *, struct if_encap_req *);
266 static void     if_route(struct ifnet *, int flag, int fam);
267 static int      if_setflag(struct ifnet *, int, int, int *, int);
268 static int      if_transmit(struct ifnet *ifp, struct mbuf *m);
269 static void     if_unroute(struct ifnet *, int flag, int fam);
270 static int      if_delmulti_locked(struct ifnet *, struct ifmultiaddr *, int);
271 static void     do_link_state_change(void *, int);
272 static int      if_getgroup(struct ifgroupreq *, struct ifnet *);
273 static int      if_getgroupmembers(struct ifgroupreq *);
274 static void     if_delgroups(struct ifnet *);
275 static void     if_attach_internal(struct ifnet *, int, struct if_clone *);
276 static int      if_detach_internal(struct ifnet *, int, struct if_clone **);
277 static void     if_siocaddmulti(void *, int);
278 static void     if_link_ifnet(struct ifnet *);
279 static bool     if_unlink_ifnet(struct ifnet *, bool);
280 #ifdef VIMAGE
281 static int      if_vmove(struct ifnet *, struct vnet *);
282 #endif
283
284 #ifdef INET6
285 /*
286  * XXX: declare here to avoid to include many inet6 related files..
287  * should be more generalized?
288  */
289 extern void     nd6_setmtu(struct ifnet *);
290 #endif
291
292 /* ipsec helper hooks */
293 VNET_DEFINE(struct hhook_head *, ipsec_hhh_in[HHOOK_IPSEC_COUNT]);
294 VNET_DEFINE(struct hhook_head *, ipsec_hhh_out[HHOOK_IPSEC_COUNT]);
295
296 VNET_DEFINE(int, if_index);
297 int     ifqmaxlen = IFQ_MAXLEN;
298 VNET_DEFINE(struct ifnethead, ifnet);   /* depend on static init XXX */
299 VNET_DEFINE(struct ifgrouphead, ifg_head);
300
301 VNET_DEFINE_STATIC(int, if_indexlim) = 8;
302
303 /* Table of ifnet by index. */
304 VNET_DEFINE(struct ifnet **, ifindex_table);
305
306 #define V_if_indexlim           VNET(if_indexlim)
307 #define V_ifindex_table         VNET(ifindex_table)
308
309 /*
310  * The global network interface list (V_ifnet) and related state (such as
311  * if_index, if_indexlim, and ifindex_table) are protected by an sxlock.
312  * This may be acquired to stabilise the list, or we may rely on NET_EPOCH.
313  */
314 struct sx ifnet_sxlock;
315 SX_SYSINIT_FLAGS(ifnet_sx, &ifnet_sxlock, "ifnet_sx", SX_RECURSE);
316
317 struct sx ifnet_detach_sxlock;
318 SX_SYSINIT_FLAGS(ifnet_detach, &ifnet_detach_sxlock, "ifnet_detach_sx",
319     SX_RECURSE);
320
321 /*
322  * The allocation of network interfaces is a rather non-atomic affair; we
323  * need to select an index before we are ready to expose the interface for
324  * use, so will use this pointer value to indicate reservation.
325  */
326 #define IFNET_HOLD      (void *)(uintptr_t)(-1)
327
328 #ifdef VIMAGE
329 #define VNET_IS_SHUTTING_DOWN(_vnet)                                    \
330     ((_vnet)->vnet_shutdown && (_vnet)->vnet_state < SI_SUB_VNET_DONE)
331 #endif
332
333 static  if_com_alloc_t *if_com_alloc[256];
334 static  if_com_free_t *if_com_free[256];
335
336 static MALLOC_DEFINE(M_IFNET, "ifnet", "interface internals");
337 MALLOC_DEFINE(M_IFADDR, "ifaddr", "interface address");
338 MALLOC_DEFINE(M_IFMADDR, "ether_multi", "link-level multicast address");
339
340 struct ifnet *
341 ifnet_byindex(u_short idx)
342 {
343         struct ifnet *ifp;
344
345         if (__predict_false(idx > V_if_index))
346                 return (NULL);
347
348         ifp = *(struct ifnet * const volatile *)(V_ifindex_table + idx);
349         return (__predict_false(ifp == IFNET_HOLD) ? NULL : ifp);
350 }
351
352 struct ifnet *
353 ifnet_byindex_ref(u_short idx)
354 {
355         struct ifnet *ifp;
356
357         NET_EPOCH_ASSERT();
358
359         ifp = ifnet_byindex(idx);
360         if (ifp == NULL || (ifp->if_flags & IFF_DYING))
361                 return (NULL);
362         if_ref(ifp);
363         return (ifp);
364 }
365
366 /*
367  * Allocate an ifindex array entry; return 0 on success or an error on
368  * failure.
369  */
370 static u_short
371 ifindex_alloc(void **old)
372 {
373         u_short idx;
374
375         IFNET_WLOCK_ASSERT();
376         /*
377          * Try to find an empty slot below V_if_index.  If we fail, take the
378          * next slot.
379          */
380         for (idx = 1; idx <= V_if_index; idx++) {
381                 if (V_ifindex_table[idx] == NULL)
382                         break;
383         }
384
385         /* Catch if_index overflow. */
386         if (idx >= V_if_indexlim) {
387                 *old = if_grow();
388                 return (USHRT_MAX);
389         }
390         if (idx > V_if_index)
391                 V_if_index = idx;
392         return (idx);
393 }
394
395 static void
396 ifindex_free_locked(u_short idx)
397 {
398
399         IFNET_WLOCK_ASSERT();
400
401         V_ifindex_table[idx] = NULL;
402         while (V_if_index > 0 &&
403             V_ifindex_table[V_if_index] == NULL)
404                 V_if_index--;
405 }
406
407 static void
408 ifindex_free(u_short idx)
409 {
410
411         IFNET_WLOCK();
412         ifindex_free_locked(idx);
413         IFNET_WUNLOCK();
414 }
415
416 static void
417 ifnet_setbyindex(u_short idx, struct ifnet *ifp)
418 {
419
420         V_ifindex_table[idx] = ifp;
421 }
422
423 struct ifaddr *
424 ifaddr_byindex(u_short idx)
425 {
426         struct ifnet *ifp;
427         struct ifaddr *ifa = NULL;
428
429         NET_EPOCH_ASSERT();
430
431         ifp = ifnet_byindex(idx);
432         if (ifp != NULL && (ifa = ifp->if_addr) != NULL)
433                 ifa_ref(ifa);
434         return (ifa);
435 }
436
437 /*
438  * Network interface utility routines.
439  *
440  * Routines with ifa_ifwith* names take sockaddr *'s as
441  * parameters.
442  */
443
444 static void
445 vnet_if_init(const void *unused __unused)
446 {
447         void *old;
448
449         CK_STAILQ_INIT(&V_ifnet);
450         CK_STAILQ_INIT(&V_ifg_head);
451         IFNET_WLOCK();
452         old = if_grow();                                /* create initial table */
453         IFNET_WUNLOCK();
454         epoch_wait_preempt(net_epoch_preempt);
455         free(old, M_IFNET);
456         vnet_if_clone_init();
457 }
458 VNET_SYSINIT(vnet_if_init, SI_SUB_INIT_IF, SI_ORDER_SECOND, vnet_if_init,
459     NULL);
460
461 #ifdef VIMAGE
462 static void
463 vnet_if_uninit(const void *unused __unused)
464 {
465
466         VNET_ASSERT(CK_STAILQ_EMPTY(&V_ifnet), ("%s:%d tailq &V_ifnet=%p "
467             "not empty", __func__, __LINE__, &V_ifnet));
468         VNET_ASSERT(CK_STAILQ_EMPTY(&V_ifg_head), ("%s:%d tailq &V_ifg_head=%p "
469             "not empty", __func__, __LINE__, &V_ifg_head));
470
471         free((caddr_t)V_ifindex_table, M_IFNET);
472 }
473 VNET_SYSUNINIT(vnet_if_uninit, SI_SUB_INIT_IF, SI_ORDER_FIRST,
474     vnet_if_uninit, NULL);
475 #endif
476
477 static void
478 if_link_ifnet(struct ifnet *ifp)
479 {
480
481         IFNET_WLOCK();
482         CK_STAILQ_INSERT_TAIL(&V_ifnet, ifp, if_link);
483 #ifdef VIMAGE
484         curvnet->vnet_ifcnt++;
485 #endif
486         IFNET_WUNLOCK();
487 }
488
489 static bool
490 if_unlink_ifnet(struct ifnet *ifp, bool vmove)
491 {
492         struct ifnet *iter;
493         int found = 0;
494
495         IFNET_WLOCK();
496         CK_STAILQ_FOREACH(iter, &V_ifnet, if_link)
497                 if (iter == ifp) {
498                         CK_STAILQ_REMOVE(&V_ifnet, ifp, ifnet, if_link);
499                         if (!vmove)
500                                 ifp->if_flags |= IFF_DYING;
501                         found = 1;
502                         break;
503                 }
504 #ifdef VIMAGE
505         curvnet->vnet_ifcnt--;
506 #endif
507         IFNET_WUNLOCK();
508
509         return (found);
510 }
511
512 #ifdef VIMAGE
513 static void
514 vnet_if_return(const void *unused __unused)
515 {
516         struct ifnet *ifp, *nifp;
517         struct ifnet **pending;
518         int found, i;
519
520         i = 0;
521
522         /*
523          * We need to protect our access to the V_ifnet tailq. Ordinarily we'd
524          * enter NET_EPOCH, but that's not possible, because if_vmove() calls
525          * if_detach_internal(), which waits for NET_EPOCH callbacks to
526          * complete. We can't do that from within NET_EPOCH.
527          *
528          * However, we can also use the IFNET_xLOCK, which is the V_ifnet
529          * read/write lock. We cannot hold the lock as we call if_vmove()
530          * though, as that presents LOR w.r.t ifnet_sx, in_multi_sx and iflib
531          * ctx lock.
532          */
533         IFNET_WLOCK();
534
535         pending = malloc(sizeof(struct ifnet *) * curvnet->vnet_ifcnt,
536             M_IFNET, M_WAITOK | M_ZERO);
537
538         /* Return all inherited interfaces to their parent vnets. */
539         CK_STAILQ_FOREACH_SAFE(ifp, &V_ifnet, if_link, nifp) {
540                 if (ifp->if_home_vnet != ifp->if_vnet) {
541                         found = if_unlink_ifnet(ifp, true);
542                         MPASS(found);
543
544                         pending[i++] = ifp;
545                 }
546         }
547         IFNET_WUNLOCK();
548
549         for (int j = 0; j < i; j++) {
550                 if_vmove(pending[j], pending[j]->if_home_vnet);
551         }
552
553         free(pending, M_IFNET);
554 }
555 VNET_SYSUNINIT(vnet_if_return, SI_SUB_VNET_DONE, SI_ORDER_ANY,
556     vnet_if_return, NULL);
557 #endif
558
559 static void *
560 if_grow(void)
561 {
562         int oldlim;
563         u_int n;
564         struct ifnet **e;
565         void *old;
566
567         old = NULL;
568         IFNET_WLOCK_ASSERT();
569         oldlim = V_if_indexlim;
570         IFNET_WUNLOCK();
571         n = (oldlim << 1) * sizeof(*e);
572         e = malloc(n, M_IFNET, M_WAITOK | M_ZERO);
573         IFNET_WLOCK();
574         if (V_if_indexlim != oldlim) {
575                 free(e, M_IFNET);
576                 return (NULL);
577         }
578         if (V_ifindex_table != NULL) {
579                 memcpy((caddr_t)e, (caddr_t)V_ifindex_table, n/2);
580                 old = V_ifindex_table;
581         }
582         V_if_indexlim <<= 1;
583         V_ifindex_table = e;
584         return (old);
585 }
586
587 /*
588  * Allocate a struct ifnet and an index for an interface.  A layer 2
589  * common structure will also be allocated if an allocation routine is
590  * registered for the passed type.
591  */
592 struct ifnet *
593 if_alloc_domain(u_char type, int numa_domain)
594 {
595         struct ifnet *ifp;
596         u_short idx;
597         void *old;
598
599         KASSERT(numa_domain <= IF_NODOM, ("numa_domain too large"));
600         if (numa_domain == IF_NODOM)
601                 ifp = malloc(sizeof(struct ifnet), M_IFNET,
602                     M_WAITOK | M_ZERO);
603         else
604                 ifp = malloc_domainset(sizeof(struct ifnet), M_IFNET,
605                     DOMAINSET_PREF(numa_domain), M_WAITOK | M_ZERO);
606  restart:
607         IFNET_WLOCK();
608         idx = ifindex_alloc(&old);
609         if (__predict_false(idx == USHRT_MAX)) {
610                 IFNET_WUNLOCK();
611                 epoch_wait_preempt(net_epoch_preempt);
612                 free(old, M_IFNET);
613                 goto restart;
614         }
615         ifnet_setbyindex(idx, IFNET_HOLD);
616         IFNET_WUNLOCK();
617         ifp->if_index = idx;
618         ifp->if_type = type;
619         ifp->if_alloctype = type;
620         ifp->if_numa_domain = numa_domain;
621 #ifdef VIMAGE
622         ifp->if_vnet = curvnet;
623 #endif
624         if (if_com_alloc[type] != NULL) {
625                 ifp->if_l2com = if_com_alloc[type](type, ifp);
626                 if (ifp->if_l2com == NULL) {
627                         free(ifp, M_IFNET);
628                         ifindex_free(idx);
629                         return (NULL);
630                 }
631         }
632
633         IF_ADDR_LOCK_INIT(ifp);
634         TASK_INIT(&ifp->if_linktask, 0, do_link_state_change, ifp);
635         TASK_INIT(&ifp->if_addmultitask, 0, if_siocaddmulti, ifp);
636         ifp->if_afdata_initialized = 0;
637         IF_AFDATA_LOCK_INIT(ifp);
638         CK_STAILQ_INIT(&ifp->if_addrhead);
639         CK_STAILQ_INIT(&ifp->if_multiaddrs);
640         CK_STAILQ_INIT(&ifp->if_groups);
641 #ifdef MAC
642         mac_ifnet_init(ifp);
643 #endif
644         ifq_init(&ifp->if_snd, ifp);
645
646         refcount_init(&ifp->if_refcount, 1);    /* Index reference. */
647         for (int i = 0; i < IFCOUNTERS; i++)
648                 ifp->if_counters[i] = counter_u64_alloc(M_WAITOK);
649         ifp->if_get_counter = if_get_counter_default;
650         ifp->if_pcp = IFNET_PCP_NONE;
651         ifnet_setbyindex(ifp->if_index, ifp);
652         return (ifp);
653 }
654
655 struct ifnet *
656 if_alloc_dev(u_char type, device_t dev)
657 {
658         int numa_domain;
659
660         if (dev == NULL || bus_get_domain(dev, &numa_domain) != 0)
661                 return (if_alloc_domain(type, IF_NODOM));
662         return (if_alloc_domain(type, numa_domain));
663 }
664
665 struct ifnet *
666 if_alloc(u_char type)
667 {
668
669         return (if_alloc_domain(type, IF_NODOM));
670 }
671 /*
672  * Do the actual work of freeing a struct ifnet, and layer 2 common
673  * structure.  This call is made when the last reference to an
674  * interface is released.
675  */
676 static void
677 if_free_internal(struct ifnet *ifp)
678 {
679
680         KASSERT((ifp->if_flags & IFF_DYING),
681             ("if_free_internal: interface not dying"));
682
683         if (if_com_free[ifp->if_alloctype] != NULL)
684                 if_com_free[ifp->if_alloctype](ifp->if_l2com,
685                     ifp->if_alloctype);
686
687 #ifdef MAC
688         mac_ifnet_destroy(ifp);
689 #endif /* MAC */
690         IF_AFDATA_DESTROY(ifp);
691         IF_ADDR_LOCK_DESTROY(ifp);
692         ifq_delete(&ifp->if_snd);
693
694         for (int i = 0; i < IFCOUNTERS; i++)
695                 counter_u64_free(ifp->if_counters[i]);
696
697         free(ifp->if_description, M_IFDESCR);
698         free(ifp->if_hw_addr, M_IFADDR);
699         free(ifp, M_IFNET);
700 }
701
702 static void
703 if_destroy(epoch_context_t ctx)
704 {
705         struct ifnet *ifp;
706
707         ifp = __containerof(ctx, struct ifnet, if_epoch_ctx);
708         if_free_internal(ifp);
709 }
710
711 /*
712  * Deregister an interface and free the associated storage.
713  */
714 void
715 if_free(struct ifnet *ifp)
716 {
717
718         ifp->if_flags |= IFF_DYING;                     /* XXX: Locking */
719
720         CURVNET_SET_QUIET(ifp->if_vnet);
721         IFNET_WLOCK();
722         KASSERT(ifp == ifnet_byindex(ifp->if_index),
723             ("%s: freeing unallocated ifnet", ifp->if_xname));
724
725         ifindex_free_locked(ifp->if_index);
726         IFNET_WUNLOCK();
727
728         if (refcount_release(&ifp->if_refcount))
729                 NET_EPOCH_CALL(if_destroy, &ifp->if_epoch_ctx);
730         CURVNET_RESTORE();
731 }
732
733 /*
734  * Interfaces to keep an ifnet type-stable despite the possibility of the
735  * driver calling if_free().  If there are additional references, we defer
736  * freeing the underlying data structure.
737  */
738 void
739 if_ref(struct ifnet *ifp)
740 {
741
742         /* We don't assert the ifnet list lock here, but arguably should. */
743         refcount_acquire(&ifp->if_refcount);
744 }
745
746 void
747 if_rele(struct ifnet *ifp)
748 {
749
750         if (!refcount_release(&ifp->if_refcount))
751                 return;
752         NET_EPOCH_CALL(if_destroy, &ifp->if_epoch_ctx);
753 }
754
755 void
756 ifq_init(struct ifaltq *ifq, struct ifnet *ifp)
757 {
758
759         mtx_init(&ifq->ifq_mtx, ifp->if_xname, "if send queue", MTX_DEF);
760
761         if (ifq->ifq_maxlen == 0) 
762                 ifq->ifq_maxlen = ifqmaxlen;
763
764         ifq->altq_type = 0;
765         ifq->altq_disc = NULL;
766         ifq->altq_flags &= ALTQF_CANTCHANGE;
767         ifq->altq_tbr  = NULL;
768         ifq->altq_ifp  = ifp;
769 }
770
771 void
772 ifq_delete(struct ifaltq *ifq)
773 {
774         mtx_destroy(&ifq->ifq_mtx);
775 }
776
777 /*
778  * Perform generic interface initialization tasks and attach the interface
779  * to the list of "active" interfaces.  If vmove flag is set on entry
780  * to if_attach_internal(), perform only a limited subset of initialization
781  * tasks, given that we are moving from one vnet to another an ifnet which
782  * has already been fully initialized.
783  *
784  * Note that if_detach_internal() removes group membership unconditionally
785  * even when vmove flag is set, and if_attach_internal() adds only IFG_ALL.
786  * Thus, when if_vmove() is applied to a cloned interface, group membership
787  * is lost while a cloned one always joins a group whose name is
788  * ifc->ifc_name.  To recover this after if_detach_internal() and
789  * if_attach_internal(), the cloner should be specified to
790  * if_attach_internal() via ifc.  If it is non-NULL, if_attach_internal()
791  * attempts to join a group whose name is ifc->ifc_name.
792  *
793  * XXX:
794  *  - The decision to return void and thus require this function to
795  *    succeed is questionable.
796  *  - We should probably do more sanity checking.  For instance we don't
797  *    do anything to insure if_xname is unique or non-empty.
798  */
799 void
800 if_attach(struct ifnet *ifp)
801 {
802
803         if_attach_internal(ifp, 0, NULL);
804 }
805
806 /*
807  * Compute the least common TSO limit.
808  */
809 void
810 if_hw_tsomax_common(if_t ifp, struct ifnet_hw_tsomax *pmax)
811 {
812         /*
813          * 1) If there is no limit currently, take the limit from
814          * the network adapter.
815          *
816          * 2) If the network adapter has a limit below the current
817          * limit, apply it.
818          */
819         if (pmax->tsomaxbytes == 0 || (ifp->if_hw_tsomax != 0 &&
820             ifp->if_hw_tsomax < pmax->tsomaxbytes)) {
821                 pmax->tsomaxbytes = ifp->if_hw_tsomax;
822         }
823         if (pmax->tsomaxsegcount == 0 || (ifp->if_hw_tsomaxsegcount != 0 &&
824             ifp->if_hw_tsomaxsegcount < pmax->tsomaxsegcount)) {
825                 pmax->tsomaxsegcount = ifp->if_hw_tsomaxsegcount;
826         }
827         if (pmax->tsomaxsegsize == 0 || (ifp->if_hw_tsomaxsegsize != 0 &&
828             ifp->if_hw_tsomaxsegsize < pmax->tsomaxsegsize)) {
829                 pmax->tsomaxsegsize = ifp->if_hw_tsomaxsegsize;
830         }
831 }
832
833 /*
834  * Update TSO limit of a network adapter.
835  *
836  * Returns zero if no change. Else non-zero.
837  */
838 int
839 if_hw_tsomax_update(if_t ifp, struct ifnet_hw_tsomax *pmax)
840 {
841         int retval = 0;
842         if (ifp->if_hw_tsomax != pmax->tsomaxbytes) {
843                 ifp->if_hw_tsomax = pmax->tsomaxbytes;
844                 retval++;
845         }
846         if (ifp->if_hw_tsomaxsegsize != pmax->tsomaxsegsize) {
847                 ifp->if_hw_tsomaxsegsize = pmax->tsomaxsegsize;
848                 retval++;
849         }
850         if (ifp->if_hw_tsomaxsegcount != pmax->tsomaxsegcount) {
851                 ifp->if_hw_tsomaxsegcount = pmax->tsomaxsegcount;
852                 retval++;
853         }
854         return (retval);
855 }
856
857 static void
858 if_attach_internal(struct ifnet *ifp, int vmove, struct if_clone *ifc)
859 {
860         unsigned socksize, ifasize;
861         int namelen, masklen;
862         struct sockaddr_dl *sdl;
863         struct ifaddr *ifa;
864
865         if (ifp->if_index == 0 || ifp != ifnet_byindex(ifp->if_index))
866                 panic ("%s: BUG: if_attach called without if_alloc'd input()\n",
867                     ifp->if_xname);
868
869 #ifdef VIMAGE
870         ifp->if_vnet = curvnet;
871         if (ifp->if_home_vnet == NULL)
872                 ifp->if_home_vnet = curvnet;
873 #endif
874
875         if_addgroup(ifp, IFG_ALL);
876
877         /* Restore group membership for cloned interfaces. */
878         if (vmove && ifc != NULL)
879                 if_clone_addgroup(ifp, ifc);
880
881         getmicrotime(&ifp->if_lastchange);
882         ifp->if_epoch = time_uptime;
883
884         KASSERT((ifp->if_transmit == NULL && ifp->if_qflush == NULL) ||
885             (ifp->if_transmit != NULL && ifp->if_qflush != NULL),
886             ("transmit and qflush must both either be set or both be NULL"));
887         if (ifp->if_transmit == NULL) {
888                 ifp->if_transmit = if_transmit;
889                 ifp->if_qflush = if_qflush;
890         }
891         if (ifp->if_input == NULL)
892                 ifp->if_input = if_input_default;
893
894         if (ifp->if_requestencap == NULL)
895                 ifp->if_requestencap = if_requestencap_default;
896
897         if (!vmove) {
898 #ifdef MAC
899                 mac_ifnet_create(ifp);
900 #endif
901
902                 /*
903                  * Create a Link Level name for this device.
904                  */
905                 namelen = strlen(ifp->if_xname);
906                 /*
907                  * Always save enough space for any possiable name so we
908                  * can do a rename in place later.
909                  */
910                 masklen = offsetof(struct sockaddr_dl, sdl_data[0]) + IFNAMSIZ;
911                 socksize = masklen + ifp->if_addrlen;
912                 if (socksize < sizeof(*sdl))
913                         socksize = sizeof(*sdl);
914                 socksize = roundup2(socksize, sizeof(long));
915                 ifasize = sizeof(*ifa) + 2 * socksize;
916                 ifa = ifa_alloc(ifasize, M_WAITOK);
917                 sdl = (struct sockaddr_dl *)(ifa + 1);
918                 sdl->sdl_len = socksize;
919                 sdl->sdl_family = AF_LINK;
920                 bcopy(ifp->if_xname, sdl->sdl_data, namelen);
921                 sdl->sdl_nlen = namelen;
922                 sdl->sdl_index = ifp->if_index;
923                 sdl->sdl_type = ifp->if_type;
924                 ifp->if_addr = ifa;
925                 ifa->ifa_ifp = ifp;
926                 ifa->ifa_addr = (struct sockaddr *)sdl;
927                 sdl = (struct sockaddr_dl *)(socksize + (caddr_t)sdl);
928                 ifa->ifa_netmask = (struct sockaddr *)sdl;
929                 sdl->sdl_len = masklen;
930                 while (namelen != 0)
931                         sdl->sdl_data[--namelen] = 0xff;
932                 CK_STAILQ_INSERT_HEAD(&ifp->if_addrhead, ifa, ifa_link);
933                 /* Reliably crash if used uninitialized. */
934                 ifp->if_broadcastaddr = NULL;
935
936                 if (ifp->if_type == IFT_ETHER) {
937                         ifp->if_hw_addr = malloc(ifp->if_addrlen, M_IFADDR,
938                             M_WAITOK | M_ZERO);
939                 }
940
941 #if defined(INET) || defined(INET6)
942                 /* Use defaults for TSO, if nothing is set */
943                 if (ifp->if_hw_tsomax == 0 &&
944                     ifp->if_hw_tsomaxsegcount == 0 &&
945                     ifp->if_hw_tsomaxsegsize == 0) {
946                         /*
947                          * The TSO defaults needs to be such that an
948                          * NFS mbuf list of 35 mbufs totalling just
949                          * below 64K works and that a chain of mbufs
950                          * can be defragged into at most 32 segments:
951                          */
952                         ifp->if_hw_tsomax = min(IP_MAXPACKET, (32 * MCLBYTES) -
953                             (ETHER_HDR_LEN + ETHER_VLAN_ENCAP_LEN));
954                         ifp->if_hw_tsomaxsegcount = 35;
955                         ifp->if_hw_tsomaxsegsize = 2048;        /* 2K */
956
957                         /* XXX some drivers set IFCAP_TSO after ethernet attach */
958                         if (ifp->if_capabilities & IFCAP_TSO) {
959                                 if_printf(ifp, "Using defaults for TSO: %u/%u/%u\n",
960                                     ifp->if_hw_tsomax,
961                                     ifp->if_hw_tsomaxsegcount,
962                                     ifp->if_hw_tsomaxsegsize);
963                         }
964                 }
965 #endif
966         }
967 #ifdef VIMAGE
968         else {
969                 /*
970                  * Update the interface index in the link layer address
971                  * of the interface.
972                  */
973                 for (ifa = ifp->if_addr; ifa != NULL;
974                     ifa = CK_STAILQ_NEXT(ifa, ifa_link)) {
975                         if (ifa->ifa_addr->sa_family == AF_LINK) {
976                                 sdl = (struct sockaddr_dl *)ifa->ifa_addr;
977                                 sdl->sdl_index = ifp->if_index;
978                         }
979                 }
980         }
981 #endif
982
983         if_link_ifnet(ifp);
984
985         if (domain_init_status >= 2)
986                 if_attachdomain1(ifp);
987
988         EVENTHANDLER_INVOKE(ifnet_arrival_event, ifp);
989         if (IS_DEFAULT_VNET(curvnet))
990                 devctl_notify("IFNET", ifp->if_xname, "ATTACH", NULL);
991
992         /* Announce the interface. */
993         rt_ifannouncemsg(ifp, IFAN_ARRIVAL);
994 }
995
996 static void
997 if_epochalloc(void *dummy __unused)
998 {
999
1000         net_epoch_preempt = epoch_alloc("Net preemptible", EPOCH_PREEMPT);
1001 }
1002 SYSINIT(ifepochalloc, SI_SUB_EPOCH, SI_ORDER_ANY, if_epochalloc, NULL);
1003
1004 static void
1005 if_attachdomain(void *dummy)
1006 {
1007         struct ifnet *ifp;
1008
1009         CK_STAILQ_FOREACH(ifp, &V_ifnet, if_link)
1010                 if_attachdomain1(ifp);
1011 }
1012 SYSINIT(domainifattach, SI_SUB_PROTO_IFATTACHDOMAIN, SI_ORDER_SECOND,
1013     if_attachdomain, NULL);
1014
1015 static void
1016 if_attachdomain1(struct ifnet *ifp)
1017 {
1018         struct domain *dp;
1019
1020         /*
1021          * Since dp->dom_ifattach calls malloc() with M_WAITOK, we
1022          * cannot lock ifp->if_afdata initialization, entirely.
1023          */
1024         IF_AFDATA_LOCK(ifp);
1025         if (ifp->if_afdata_initialized >= domain_init_status) {
1026                 IF_AFDATA_UNLOCK(ifp);
1027                 log(LOG_WARNING, "%s called more than once on %s\n",
1028                     __func__, ifp->if_xname);
1029                 return;
1030         }
1031         ifp->if_afdata_initialized = domain_init_status;
1032         IF_AFDATA_UNLOCK(ifp);
1033
1034         /* address family dependent data region */
1035         bzero(ifp->if_afdata, sizeof(ifp->if_afdata));
1036         for (dp = domains; dp; dp = dp->dom_next) {
1037                 if (dp->dom_ifattach)
1038                         ifp->if_afdata[dp->dom_family] =
1039                             (*dp->dom_ifattach)(ifp);
1040         }
1041 }
1042
1043 /*
1044  * Remove any unicast or broadcast network addresses from an interface.
1045  */
1046 void
1047 if_purgeaddrs(struct ifnet *ifp)
1048 {
1049         struct ifaddr *ifa;
1050
1051         while (1) {
1052                 struct epoch_tracker et;
1053
1054                 NET_EPOCH_ENTER(et);
1055                 CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
1056                         if (ifa->ifa_addr->sa_family != AF_LINK)
1057                                 break;
1058                 }
1059                 NET_EPOCH_EXIT(et);
1060
1061                 if (ifa == NULL)
1062                         break;
1063 #ifdef INET
1064                 /* XXX: Ugly!! ad hoc just for INET */
1065                 if (ifa->ifa_addr->sa_family == AF_INET) {
1066                         struct ifaliasreq ifr;
1067
1068                         bzero(&ifr, sizeof(ifr));
1069                         ifr.ifra_addr = *ifa->ifa_addr;
1070                         if (ifa->ifa_dstaddr)
1071                                 ifr.ifra_broadaddr = *ifa->ifa_dstaddr;
1072                         if (in_control(NULL, SIOCDIFADDR, (caddr_t)&ifr, ifp,
1073                             NULL) == 0)
1074                                 continue;
1075                 }
1076 #endif /* INET */
1077 #ifdef INET6
1078                 if (ifa->ifa_addr->sa_family == AF_INET6) {
1079                         in6_purgeifaddr((struct in6_ifaddr *)ifa);
1080                         /* ifp_addrhead is already updated */
1081                         continue;
1082                 }
1083 #endif /* INET6 */
1084                 IF_ADDR_WLOCK(ifp);
1085                 CK_STAILQ_REMOVE(&ifp->if_addrhead, ifa, ifaddr, ifa_link);
1086                 IF_ADDR_WUNLOCK(ifp);
1087                 ifa_free(ifa);
1088         }
1089 }
1090
1091 /*
1092  * Remove any multicast network addresses from an interface when an ifnet
1093  * is going away.
1094  */
1095 static void
1096 if_purgemaddrs(struct ifnet *ifp)
1097 {
1098         struct ifmultiaddr *ifma;
1099
1100         IF_ADDR_WLOCK(ifp);
1101         while (!CK_STAILQ_EMPTY(&ifp->if_multiaddrs)) {
1102                 ifma = CK_STAILQ_FIRST(&ifp->if_multiaddrs);
1103                 CK_STAILQ_REMOVE(&ifp->if_multiaddrs, ifma, ifmultiaddr, ifma_link);
1104                 if_delmulti_locked(ifp, ifma, 1);
1105         }
1106         IF_ADDR_WUNLOCK(ifp);
1107 }
1108
1109 /*
1110  * Detach an interface, removing it from the list of "active" interfaces.
1111  * If vmove flag is set on entry to if_detach_internal(), perform only a
1112  * limited subset of cleanup tasks, given that we are moving an ifnet from
1113  * one vnet to another, where it must be fully operational.
1114  *
1115  * XXXRW: There are some significant questions about event ordering, and
1116  * how to prevent things from starting to use the interface during detach.
1117  */
1118 void
1119 if_detach(struct ifnet *ifp)
1120 {
1121         bool found;
1122
1123         CURVNET_SET_QUIET(ifp->if_vnet);
1124         found = if_unlink_ifnet(ifp, false);
1125         if (found) {
1126                 sx_xlock(&ifnet_detach_sxlock);
1127                 if_detach_internal(ifp, 0, NULL);
1128                 sx_xunlock(&ifnet_detach_sxlock);
1129         }
1130         CURVNET_RESTORE();
1131 }
1132
1133 /*
1134  * The vmove flag, if set, indicates that we are called from a callpath
1135  * that is moving an interface to a different vnet instance.
1136  *
1137  * The shutdown flag, if set, indicates that we are called in the
1138  * process of shutting down a vnet instance.  Currently only the
1139  * vnet_if_return SYSUNINIT function sets it.  Note: we can be called
1140  * on a vnet instance shutdown without this flag being set, e.g., when
1141  * the cloned interfaces are destoyed as first thing of teardown.
1142  */
1143 static int
1144 if_detach_internal(struct ifnet *ifp, int vmove, struct if_clone **ifcp)
1145 {
1146         struct ifaddr *ifa;
1147         int i;
1148         struct domain *dp;
1149 #ifdef VIMAGE
1150         bool shutdown;
1151
1152         shutdown = VNET_IS_SHUTTING_DOWN(ifp->if_vnet);
1153 #endif
1154
1155         /*
1156          * At this point we know the interface still was on the ifnet list
1157          * and we removed it so we are in a stable state.
1158          */
1159         epoch_wait_preempt(net_epoch_preempt);
1160
1161         /*
1162          * Ensure all pending EPOCH(9) callbacks have been executed. This
1163          * fixes issues about late destruction of multicast options
1164          * which lead to leave group calls, which in turn access the
1165          * belonging ifnet structure:
1166          */
1167         epoch_drain_callbacks(net_epoch_preempt);
1168
1169         /*
1170          * In any case (destroy or vmove) detach us from the groups
1171          * and remove/wait for pending events on the taskq.
1172          * XXX-BZ in theory an interface could still enqueue a taskq change?
1173          */
1174         if_delgroups(ifp);
1175
1176         taskqueue_drain(taskqueue_swi, &ifp->if_linktask);
1177         taskqueue_drain(taskqueue_swi, &ifp->if_addmultitask);
1178
1179         /*
1180          * Check if this is a cloned interface or not. Must do even if
1181          * shutting down as a if_vmove_reclaim() would move the ifp and
1182          * the if_clone_addgroup() will have a corrupted string overwise
1183          * from a gibberish pointer.
1184          */
1185         if (vmove && ifcp != NULL)
1186                 *ifcp = if_clone_findifc(ifp);
1187
1188         if_down(ifp);
1189
1190 #ifdef VIMAGE
1191         /*
1192          * On VNET shutdown abort here as the stack teardown will do all
1193          * the work top-down for us.
1194          */
1195         if (shutdown) {
1196                 /* Give interface users the chance to clean up. */
1197                 EVENTHANDLER_INVOKE(ifnet_departure_event, ifp);
1198
1199                 /*
1200                  * In case of a vmove we are done here without error.
1201                  * If we would signal an error it would lead to the same
1202                  * abort as if we did not find the ifnet anymore.
1203                  * if_detach() calls us in void context and does not care
1204                  * about an early abort notification, so life is splendid :)
1205                  */
1206                 goto finish_vnet_shutdown;
1207         }
1208 #endif
1209
1210         /*
1211          * At this point we are not tearing down a VNET and are either
1212          * going to destroy or vmove the interface and have to cleanup
1213          * accordingly.
1214          */
1215
1216         /*
1217          * Remove routes and flush queues.
1218          */
1219 #ifdef ALTQ
1220         if (ALTQ_IS_ENABLED(&ifp->if_snd))
1221                 altq_disable(&ifp->if_snd);
1222         if (ALTQ_IS_ATTACHED(&ifp->if_snd))
1223                 altq_detach(&ifp->if_snd);
1224 #endif
1225
1226         if_purgeaddrs(ifp);
1227
1228 #ifdef INET
1229         in_ifdetach(ifp);
1230 #endif
1231
1232 #ifdef INET6
1233         /*
1234          * Remove all IPv6 kernel structs related to ifp.  This should be done
1235          * before removing routing entries below, since IPv6 interface direct
1236          * routes are expected to be removed by the IPv6-specific kernel API.
1237          * Otherwise, the kernel will detect some inconsistency and bark it.
1238          */
1239         in6_ifdetach(ifp);
1240 #endif
1241         if_purgemaddrs(ifp);
1242
1243         /* Announce that the interface is gone. */
1244         rt_ifannouncemsg(ifp, IFAN_DEPARTURE);
1245         EVENTHANDLER_INVOKE(ifnet_departure_event, ifp);
1246         if (IS_DEFAULT_VNET(curvnet))
1247                 devctl_notify("IFNET", ifp->if_xname, "DETACH", NULL);
1248
1249         if (!vmove) {
1250                 /*
1251                  * Prevent further calls into the device driver via ifnet.
1252                  */
1253                 if_dead(ifp);
1254
1255                 /*
1256                  * Clean up all addresses.
1257                  */
1258                 IF_ADDR_WLOCK(ifp);
1259                 if (!CK_STAILQ_EMPTY(&ifp->if_addrhead)) {
1260                         ifa = CK_STAILQ_FIRST(&ifp->if_addrhead);
1261                         CK_STAILQ_REMOVE(&ifp->if_addrhead, ifa, ifaddr, ifa_link);
1262                         IF_ADDR_WUNLOCK(ifp);
1263                         ifa_free(ifa);
1264                 } else
1265                         IF_ADDR_WUNLOCK(ifp);
1266         }
1267
1268         rt_flushifroutes(ifp);
1269
1270 #ifdef VIMAGE
1271 finish_vnet_shutdown:
1272 #endif
1273         /*
1274          * We cannot hold the lock over dom_ifdetach calls as they might
1275          * sleep, for example trying to drain a callout, thus open up the
1276          * theoretical race with re-attaching.
1277          */
1278         IF_AFDATA_LOCK(ifp);
1279         i = ifp->if_afdata_initialized;
1280         ifp->if_afdata_initialized = 0;
1281         IF_AFDATA_UNLOCK(ifp);
1282         for (dp = domains; i > 0 && dp; dp = dp->dom_next) {
1283                 if (dp->dom_ifdetach && ifp->if_afdata[dp->dom_family]) {
1284                         (*dp->dom_ifdetach)(ifp,
1285                             ifp->if_afdata[dp->dom_family]);
1286                         ifp->if_afdata[dp->dom_family] = NULL;
1287                 }
1288         }
1289
1290         return (0);
1291 }
1292
1293 #ifdef VIMAGE
1294 /*
1295  * if_vmove() performs a limited version of if_detach() in current
1296  * vnet and if_attach()es the ifnet to the vnet specified as 2nd arg.
1297  * An attempt is made to shrink if_index in current vnet, find an
1298  * unused if_index in target vnet and calls if_grow() if necessary,
1299  * and finally find an unused if_xname for the target vnet.
1300  */
1301 static int
1302 if_vmove(struct ifnet *ifp, struct vnet *new_vnet)
1303 {
1304         struct if_clone *ifc;
1305 #ifdef DEV_BPF
1306         u_int bif_dlt, bif_hdrlen;
1307 #endif
1308         void *old;
1309         int rc;
1310
1311 #ifdef DEV_BPF
1312         /*
1313          * if_detach_internal() will call the eventhandler to notify
1314          * interface departure.  That will detach if_bpf.  We need to
1315          * safe the dlt and hdrlen so we can re-attach it later.
1316          */
1317         bpf_get_bp_params(ifp->if_bpf, &bif_dlt, &bif_hdrlen);
1318 #endif
1319
1320         /*
1321          * Detach from current vnet, but preserve LLADDR info, do not
1322          * mark as dead etc. so that the ifnet can be reattached later.
1323          * If we cannot find it, we lost the race to someone else.
1324          */
1325         rc = if_detach_internal(ifp, 1, &ifc);
1326         if (rc != 0)
1327                 return (rc);
1328
1329         /*
1330          * Unlink the ifnet from ifindex_table[] in current vnet, and shrink
1331          * the if_index for that vnet if possible.
1332          *
1333          * NOTE: IFNET_WLOCK/IFNET_WUNLOCK() are assumed to be unvirtualized,
1334          * or we'd lock on one vnet and unlock on another.
1335          */
1336         IFNET_WLOCK();
1337         ifindex_free_locked(ifp->if_index);
1338         IFNET_WUNLOCK();
1339
1340         /*
1341          * Perform interface-specific reassignment tasks, if provided by
1342          * the driver.
1343          */
1344         if (ifp->if_reassign != NULL)
1345                 ifp->if_reassign(ifp, new_vnet, NULL);
1346
1347         /*
1348          * Switch to the context of the target vnet.
1349          */
1350         CURVNET_SET_QUIET(new_vnet);
1351  restart:
1352         IFNET_WLOCK();
1353         ifp->if_index = ifindex_alloc(&old);
1354         if (__predict_false(ifp->if_index == USHRT_MAX)) {
1355                 IFNET_WUNLOCK();
1356                 epoch_wait_preempt(net_epoch_preempt);
1357                 free(old, M_IFNET);
1358                 goto restart;
1359         }
1360         ifnet_setbyindex(ifp->if_index, ifp);
1361         IFNET_WUNLOCK();
1362
1363         if_attach_internal(ifp, 1, ifc);
1364
1365 #ifdef DEV_BPF
1366         if (ifp->if_bpf == NULL)
1367                 bpfattach(ifp, bif_dlt, bif_hdrlen);
1368 #endif
1369
1370         CURVNET_RESTORE();
1371         return (0);
1372 }
1373
1374 /*
1375  * Move an ifnet to or from another child prison/vnet, specified by the jail id.
1376  */
1377 static int
1378 if_vmove_loan(struct thread *td, struct ifnet *ifp, char *ifname, int jid)
1379 {
1380         struct prison *pr;
1381         struct ifnet *difp;
1382         int error;
1383         bool found;
1384         bool shutdown;
1385
1386         /* Try to find the prison within our visibility. */
1387         sx_slock(&allprison_lock);
1388         pr = prison_find_child(td->td_ucred->cr_prison, jid);
1389         sx_sunlock(&allprison_lock);
1390         if (pr == NULL)
1391                 return (ENXIO);
1392         prison_hold_locked(pr);
1393         mtx_unlock(&pr->pr_mtx);
1394
1395         /* Do not try to move the iface from and to the same prison. */
1396         if (pr->pr_vnet == ifp->if_vnet) {
1397                 prison_free(pr);
1398                 return (EEXIST);
1399         }
1400
1401         /* Make sure the named iface does not exists in the dst. prison/vnet. */
1402         /* XXX Lock interfaces to avoid races. */
1403         CURVNET_SET_QUIET(pr->pr_vnet);
1404         difp = ifunit(ifname);
1405         if (difp != NULL) {
1406                 CURVNET_RESTORE();
1407                 prison_free(pr);
1408                 return (EEXIST);
1409         }
1410
1411         /* Make sure the VNET is stable. */
1412         shutdown = VNET_IS_SHUTTING_DOWN(ifp->if_vnet);
1413         if (shutdown) {
1414                 CURVNET_RESTORE();
1415                 prison_free(pr);
1416                 return (EBUSY);
1417         }
1418         CURVNET_RESTORE();
1419
1420         found = if_unlink_ifnet(ifp, true);
1421         MPASS(found);
1422
1423         /* Move the interface into the child jail/vnet. */
1424         error = if_vmove(ifp, pr->pr_vnet);
1425
1426         /* Report the new if_xname back to the userland on success. */
1427         if (error == 0)
1428                 sprintf(ifname, "%s", ifp->if_xname);
1429
1430         prison_free(pr);
1431         return (error);
1432 }
1433
1434 static int
1435 if_vmove_reclaim(struct thread *td, char *ifname, int jid)
1436 {
1437         struct prison *pr;
1438         struct vnet *vnet_dst;
1439         struct ifnet *ifp;
1440         int error, found;
1441         bool shutdown;
1442
1443         /* Try to find the prison within our visibility. */
1444         sx_slock(&allprison_lock);
1445         pr = prison_find_child(td->td_ucred->cr_prison, jid);
1446         sx_sunlock(&allprison_lock);
1447         if (pr == NULL)
1448                 return (ENXIO);
1449         prison_hold_locked(pr);
1450         mtx_unlock(&pr->pr_mtx);
1451
1452         /* Make sure the named iface exists in the source prison/vnet. */
1453         CURVNET_SET(pr->pr_vnet);
1454         ifp = ifunit(ifname);           /* XXX Lock to avoid races. */
1455         if (ifp == NULL) {
1456                 CURVNET_RESTORE();
1457                 prison_free(pr);
1458                 return (ENXIO);
1459         }
1460
1461         /* Do not try to move the iface from and to the same prison. */
1462         vnet_dst = TD_TO_VNET(td);
1463         if (vnet_dst == ifp->if_vnet) {
1464                 CURVNET_RESTORE();
1465                 prison_free(pr);
1466                 return (EEXIST);
1467         }
1468
1469         /* Make sure the VNET is stable. */
1470         shutdown = VNET_IS_SHUTTING_DOWN(ifp->if_vnet);
1471         if (shutdown) {
1472                 CURVNET_RESTORE();
1473                 prison_free(pr);
1474                 return (EBUSY);
1475         }
1476
1477         /* Get interface back from child jail/vnet. */
1478         found = if_unlink_ifnet(ifp, true);
1479         MPASS(found);
1480         error = if_vmove(ifp, vnet_dst);
1481         CURVNET_RESTORE();
1482
1483         /* Report the new if_xname back to the userland on success. */
1484         if (error == 0)
1485                 sprintf(ifname, "%s", ifp->if_xname);
1486
1487         prison_free(pr);
1488         return (error);
1489 }
1490 #endif /* VIMAGE */
1491
1492 /*
1493  * Add a group to an interface
1494  */
1495 int
1496 if_addgroup(struct ifnet *ifp, const char *groupname)
1497 {
1498         struct ifg_list         *ifgl;
1499         struct ifg_group        *ifg = NULL;
1500         struct ifg_member       *ifgm;
1501         int                      new = 0;
1502
1503         if (groupname[0] && groupname[strlen(groupname) - 1] >= '0' &&
1504             groupname[strlen(groupname) - 1] <= '9')
1505                 return (EINVAL);
1506
1507         IFNET_WLOCK();
1508         CK_STAILQ_FOREACH(ifgl, &ifp->if_groups, ifgl_next)
1509                 if (!strcmp(ifgl->ifgl_group->ifg_group, groupname)) {
1510                         IFNET_WUNLOCK();
1511                         return (EEXIST);
1512                 }
1513
1514         if ((ifgl = malloc(sizeof(*ifgl), M_TEMP, M_NOWAIT)) == NULL) {
1515                 IFNET_WUNLOCK();
1516                 return (ENOMEM);
1517         }
1518
1519         if ((ifgm = malloc(sizeof(*ifgm), M_TEMP, M_NOWAIT)) == NULL) {
1520                 free(ifgl, M_TEMP);
1521                 IFNET_WUNLOCK();
1522                 return (ENOMEM);
1523         }
1524
1525         CK_STAILQ_FOREACH(ifg, &V_ifg_head, ifg_next)
1526                 if (!strcmp(ifg->ifg_group, groupname))
1527                         break;
1528
1529         if (ifg == NULL) {
1530                 if ((ifg = malloc(sizeof(*ifg), M_TEMP, M_NOWAIT)) == NULL) {
1531                         free(ifgl, M_TEMP);
1532                         free(ifgm, M_TEMP);
1533                         IFNET_WUNLOCK();
1534                         return (ENOMEM);
1535                 }
1536                 strlcpy(ifg->ifg_group, groupname, sizeof(ifg->ifg_group));
1537                 ifg->ifg_refcnt = 0;
1538                 CK_STAILQ_INIT(&ifg->ifg_members);
1539                 CK_STAILQ_INSERT_TAIL(&V_ifg_head, ifg, ifg_next);
1540                 new = 1;
1541         }
1542
1543         ifg->ifg_refcnt++;
1544         ifgl->ifgl_group = ifg;
1545         ifgm->ifgm_ifp = ifp;
1546
1547         IF_ADDR_WLOCK(ifp);
1548         CK_STAILQ_INSERT_TAIL(&ifg->ifg_members, ifgm, ifgm_next);
1549         CK_STAILQ_INSERT_TAIL(&ifp->if_groups, ifgl, ifgl_next);
1550         IF_ADDR_WUNLOCK(ifp);
1551
1552         IFNET_WUNLOCK();
1553
1554         if (new)
1555                 EVENTHANDLER_INVOKE(group_attach_event, ifg);
1556         EVENTHANDLER_INVOKE(group_change_event, groupname);
1557
1558         return (0);
1559 }
1560
1561 /*
1562  * Helper function to remove a group out of an interface.  Expects the global
1563  * ifnet lock to be write-locked, and drops it before returning.
1564  */
1565 static void
1566 _if_delgroup_locked(struct ifnet *ifp, struct ifg_list *ifgl,
1567     const char *groupname)
1568 {
1569         struct ifg_member *ifgm;
1570         bool freeifgl;
1571
1572         IFNET_WLOCK_ASSERT();
1573
1574         IF_ADDR_WLOCK(ifp);
1575         CK_STAILQ_REMOVE(&ifp->if_groups, ifgl, ifg_list, ifgl_next);
1576         IF_ADDR_WUNLOCK(ifp);
1577
1578         CK_STAILQ_FOREACH(ifgm, &ifgl->ifgl_group->ifg_members, ifgm_next) {
1579                 if (ifgm->ifgm_ifp == ifp) {
1580                         CK_STAILQ_REMOVE(&ifgl->ifgl_group->ifg_members, ifgm,
1581                             ifg_member, ifgm_next);
1582                         break;
1583                 }
1584         }
1585
1586         if (--ifgl->ifgl_group->ifg_refcnt == 0) {
1587                 CK_STAILQ_REMOVE(&V_ifg_head, ifgl->ifgl_group, ifg_group,
1588                     ifg_next);
1589                 freeifgl = true;
1590         } else {
1591                 freeifgl = false;
1592         }
1593         IFNET_WUNLOCK();
1594
1595         epoch_wait_preempt(net_epoch_preempt);
1596         if (freeifgl) {
1597                 EVENTHANDLER_INVOKE(group_detach_event, ifgl->ifgl_group);
1598                 free(ifgl->ifgl_group, M_TEMP);
1599         }
1600         free(ifgm, M_TEMP);
1601         free(ifgl, M_TEMP);
1602
1603         EVENTHANDLER_INVOKE(group_change_event, groupname);
1604 }
1605
1606 /*
1607  * Remove a group from an interface
1608  */
1609 int
1610 if_delgroup(struct ifnet *ifp, const char *groupname)
1611 {
1612         struct ifg_list *ifgl;
1613
1614         IFNET_WLOCK();
1615         CK_STAILQ_FOREACH(ifgl, &ifp->if_groups, ifgl_next)
1616                 if (strcmp(ifgl->ifgl_group->ifg_group, groupname) == 0)
1617                         break;
1618         if (ifgl == NULL) {
1619                 IFNET_WUNLOCK();
1620                 return (ENOENT);
1621         }
1622
1623         _if_delgroup_locked(ifp, ifgl, groupname);
1624
1625         return (0);
1626 }
1627
1628 /*
1629  * Remove an interface from all groups
1630  */
1631 static void
1632 if_delgroups(struct ifnet *ifp)
1633 {
1634         struct ifg_list *ifgl;
1635         char groupname[IFNAMSIZ];
1636
1637         IFNET_WLOCK();
1638         while ((ifgl = CK_STAILQ_FIRST(&ifp->if_groups)) != NULL) {
1639                 strlcpy(groupname, ifgl->ifgl_group->ifg_group, IFNAMSIZ);
1640                 _if_delgroup_locked(ifp, ifgl, groupname);
1641                 IFNET_WLOCK();
1642         }
1643         IFNET_WUNLOCK();
1644 }
1645
1646 static char *
1647 ifgr_group_get(void *ifgrp)
1648 {
1649         union ifgroupreq_union *ifgrup;
1650
1651         ifgrup = ifgrp;
1652 #ifdef COMPAT_FREEBSD32
1653         if (SV_CURPROC_FLAG(SV_ILP32))
1654                 return (&ifgrup->ifgr32.ifgr_ifgru.ifgru_group[0]);
1655 #endif
1656         return (&ifgrup->ifgr.ifgr_ifgru.ifgru_group[0]);
1657 }
1658
1659 static struct ifg_req *
1660 ifgr_groups_get(void *ifgrp)
1661 {
1662         union ifgroupreq_union *ifgrup;
1663
1664         ifgrup = ifgrp;
1665 #ifdef COMPAT_FREEBSD32
1666         if (SV_CURPROC_FLAG(SV_ILP32))
1667                 return ((struct ifg_req *)(uintptr_t)
1668                     ifgrup->ifgr32.ifgr_ifgru.ifgru_groups);
1669 #endif
1670         return (ifgrup->ifgr.ifgr_ifgru.ifgru_groups);
1671 }
1672
1673 /*
1674  * Stores all groups from an interface in memory pointed to by ifgr.
1675  */
1676 static int
1677 if_getgroup(struct ifgroupreq *ifgr, struct ifnet *ifp)
1678 {
1679         int                      len, error;
1680         struct ifg_list         *ifgl;
1681         struct ifg_req           ifgrq, *ifgp;
1682
1683         NET_EPOCH_ASSERT();
1684
1685         if (ifgr->ifgr_len == 0) {
1686                 CK_STAILQ_FOREACH(ifgl, &ifp->if_groups, ifgl_next)
1687                         ifgr->ifgr_len += sizeof(struct ifg_req);
1688                 return (0);
1689         }
1690
1691         len = ifgr->ifgr_len;
1692         ifgp = ifgr_groups_get(ifgr);
1693         /* XXX: wire */
1694         CK_STAILQ_FOREACH(ifgl, &ifp->if_groups, ifgl_next) {
1695                 if (len < sizeof(ifgrq))
1696                         return (EINVAL);
1697                 bzero(&ifgrq, sizeof ifgrq);
1698                 strlcpy(ifgrq.ifgrq_group, ifgl->ifgl_group->ifg_group,
1699                     sizeof(ifgrq.ifgrq_group));
1700                 if ((error = copyout(&ifgrq, ifgp, sizeof(struct ifg_req))))
1701                         return (error);
1702                 len -= sizeof(ifgrq);
1703                 ifgp++;
1704         }
1705
1706         return (0);
1707 }
1708
1709 /*
1710  * Stores all members of a group in memory pointed to by igfr
1711  */
1712 static int
1713 if_getgroupmembers(struct ifgroupreq *ifgr)
1714 {
1715         struct ifg_group        *ifg;
1716         struct ifg_member       *ifgm;
1717         struct ifg_req           ifgrq, *ifgp;
1718         int                      len, error;
1719
1720         IFNET_RLOCK();
1721         CK_STAILQ_FOREACH(ifg, &V_ifg_head, ifg_next)
1722                 if (strcmp(ifg->ifg_group, ifgr->ifgr_name) == 0)
1723                         break;
1724         if (ifg == NULL) {
1725                 IFNET_RUNLOCK();
1726                 return (ENOENT);
1727         }
1728
1729         if (ifgr->ifgr_len == 0) {
1730                 CK_STAILQ_FOREACH(ifgm, &ifg->ifg_members, ifgm_next)
1731                         ifgr->ifgr_len += sizeof(ifgrq);
1732                 IFNET_RUNLOCK();
1733                 return (0);
1734         }
1735
1736         len = ifgr->ifgr_len;
1737         ifgp = ifgr_groups_get(ifgr);
1738         CK_STAILQ_FOREACH(ifgm, &ifg->ifg_members, ifgm_next) {
1739                 if (len < sizeof(ifgrq)) {
1740                         IFNET_RUNLOCK();
1741                         return (EINVAL);
1742                 }
1743                 bzero(&ifgrq, sizeof ifgrq);
1744                 strlcpy(ifgrq.ifgrq_member, ifgm->ifgm_ifp->if_xname,
1745                     sizeof(ifgrq.ifgrq_member));
1746                 if ((error = copyout(&ifgrq, ifgp, sizeof(struct ifg_req)))) {
1747                         IFNET_RUNLOCK();
1748                         return (error);
1749                 }
1750                 len -= sizeof(ifgrq);
1751                 ifgp++;
1752         }
1753         IFNET_RUNLOCK();
1754
1755         return (0);
1756 }
1757
1758 /*
1759  * Return counter values from counter(9)s stored in ifnet.
1760  */
1761 uint64_t
1762 if_get_counter_default(struct ifnet *ifp, ift_counter cnt)
1763 {
1764
1765         KASSERT(cnt < IFCOUNTERS, ("%s: invalid cnt %d", __func__, cnt));
1766
1767         return (counter_u64_fetch(ifp->if_counters[cnt]));
1768 }
1769
1770 /*
1771  * Increase an ifnet counter. Usually used for counters shared
1772  * between the stack and a driver, but function supports them all.
1773  */
1774 void
1775 if_inc_counter(struct ifnet *ifp, ift_counter cnt, int64_t inc)
1776 {
1777
1778         KASSERT(cnt < IFCOUNTERS, ("%s: invalid cnt %d", __func__, cnt));
1779
1780         counter_u64_add(ifp->if_counters[cnt], inc);
1781 }
1782
1783 /*
1784  * Copy data from ifnet to userland API structure if_data.
1785  */
1786 void
1787 if_data_copy(struct ifnet *ifp, struct if_data *ifd)
1788 {
1789
1790         ifd->ifi_type = ifp->if_type;
1791         ifd->ifi_physical = 0;
1792         ifd->ifi_addrlen = ifp->if_addrlen;
1793         ifd->ifi_hdrlen = ifp->if_hdrlen;
1794         ifd->ifi_link_state = ifp->if_link_state;
1795         ifd->ifi_vhid = 0;
1796         ifd->ifi_datalen = sizeof(struct if_data);
1797         ifd->ifi_mtu = ifp->if_mtu;
1798         ifd->ifi_metric = ifp->if_metric;
1799         ifd->ifi_baudrate = ifp->if_baudrate;
1800         ifd->ifi_hwassist = ifp->if_hwassist;
1801         ifd->ifi_epoch = ifp->if_epoch;
1802         ifd->ifi_lastchange = ifp->if_lastchange;
1803
1804         ifd->ifi_ipackets = ifp->if_get_counter(ifp, IFCOUNTER_IPACKETS);
1805         ifd->ifi_ierrors = ifp->if_get_counter(ifp, IFCOUNTER_IERRORS);
1806         ifd->ifi_opackets = ifp->if_get_counter(ifp, IFCOUNTER_OPACKETS);
1807         ifd->ifi_oerrors = ifp->if_get_counter(ifp, IFCOUNTER_OERRORS);
1808         ifd->ifi_collisions = ifp->if_get_counter(ifp, IFCOUNTER_COLLISIONS);
1809         ifd->ifi_ibytes = ifp->if_get_counter(ifp, IFCOUNTER_IBYTES);
1810         ifd->ifi_obytes = ifp->if_get_counter(ifp, IFCOUNTER_OBYTES);
1811         ifd->ifi_imcasts = ifp->if_get_counter(ifp, IFCOUNTER_IMCASTS);
1812         ifd->ifi_omcasts = ifp->if_get_counter(ifp, IFCOUNTER_OMCASTS);
1813         ifd->ifi_iqdrops = ifp->if_get_counter(ifp, IFCOUNTER_IQDROPS);
1814         ifd->ifi_oqdrops = ifp->if_get_counter(ifp, IFCOUNTER_OQDROPS);
1815         ifd->ifi_noproto = ifp->if_get_counter(ifp, IFCOUNTER_NOPROTO);
1816 }
1817
1818 /*
1819  * Initialization, destruction and refcounting functions for ifaddrs.
1820  */
1821 struct ifaddr *
1822 ifa_alloc(size_t size, int flags)
1823 {
1824         struct ifaddr *ifa;
1825
1826         KASSERT(size >= sizeof(struct ifaddr),
1827             ("%s: invalid size %zu", __func__, size));
1828
1829         ifa = malloc(size, M_IFADDR, M_ZERO | flags);
1830         if (ifa == NULL)
1831                 return (NULL);
1832
1833         if ((ifa->ifa_opackets = counter_u64_alloc(flags)) == NULL)
1834                 goto fail;
1835         if ((ifa->ifa_ipackets = counter_u64_alloc(flags)) == NULL)
1836                 goto fail;
1837         if ((ifa->ifa_obytes = counter_u64_alloc(flags)) == NULL)
1838                 goto fail;
1839         if ((ifa->ifa_ibytes = counter_u64_alloc(flags)) == NULL)
1840                 goto fail;
1841
1842         refcount_init(&ifa->ifa_refcnt, 1);
1843
1844         return (ifa);
1845
1846 fail:
1847         /* free(NULL) is okay */
1848         counter_u64_free(ifa->ifa_opackets);
1849         counter_u64_free(ifa->ifa_ipackets);
1850         counter_u64_free(ifa->ifa_obytes);
1851         counter_u64_free(ifa->ifa_ibytes);
1852         free(ifa, M_IFADDR);
1853
1854         return (NULL);
1855 }
1856
1857 void
1858 ifa_ref(struct ifaddr *ifa)
1859 {
1860
1861         refcount_acquire(&ifa->ifa_refcnt);
1862 }
1863
1864 static void
1865 ifa_destroy(epoch_context_t ctx)
1866 {
1867         struct ifaddr *ifa;
1868
1869         ifa = __containerof(ctx, struct ifaddr, ifa_epoch_ctx);
1870         counter_u64_free(ifa->ifa_opackets);
1871         counter_u64_free(ifa->ifa_ipackets);
1872         counter_u64_free(ifa->ifa_obytes);
1873         counter_u64_free(ifa->ifa_ibytes);
1874         free(ifa, M_IFADDR);
1875 }
1876
1877 void
1878 ifa_free(struct ifaddr *ifa)
1879 {
1880
1881         if (refcount_release(&ifa->ifa_refcnt))
1882                 NET_EPOCH_CALL(ifa_destroy, &ifa->ifa_epoch_ctx);
1883 }
1884
1885 /*
1886  * XXX: Because sockaddr_dl has deeper structure than the sockaddr
1887  * structs used to represent other address families, it is necessary
1888  * to perform a different comparison.
1889  */
1890
1891 #define sa_dl_equal(a1, a2)     \
1892         ((((const struct sockaddr_dl *)(a1))->sdl_len ==                \
1893          ((const struct sockaddr_dl *)(a2))->sdl_len) &&                \
1894          (bcmp(CLLADDR((const struct sockaddr_dl *)(a1)),               \
1895                CLLADDR((const struct sockaddr_dl *)(a2)),               \
1896                ((const struct sockaddr_dl *)(a1))->sdl_alen) == 0))
1897
1898 /*
1899  * Locate an interface based on a complete address.
1900  */
1901 /*ARGSUSED*/
1902 struct ifaddr *
1903 ifa_ifwithaddr(const struct sockaddr *addr)
1904 {
1905         struct ifnet *ifp;
1906         struct ifaddr *ifa;
1907
1908         NET_EPOCH_ASSERT();
1909
1910         CK_STAILQ_FOREACH(ifp, &V_ifnet, if_link) {
1911                 CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
1912                         if (ifa->ifa_addr->sa_family != addr->sa_family)
1913                                 continue;
1914                         if (sa_equal(addr, ifa->ifa_addr)) {
1915                                 goto done;
1916                         }
1917                         /* IP6 doesn't have broadcast */
1918                         if ((ifp->if_flags & IFF_BROADCAST) &&
1919                             ifa->ifa_broadaddr &&
1920                             ifa->ifa_broadaddr->sa_len != 0 &&
1921                             sa_equal(ifa->ifa_broadaddr, addr)) {
1922                                 goto done;
1923                         }
1924                 }
1925         }
1926         ifa = NULL;
1927 done:
1928         return (ifa);
1929 }
1930
1931 int
1932 ifa_ifwithaddr_check(const struct sockaddr *addr)
1933 {
1934         struct epoch_tracker et;
1935         int rc;
1936
1937         NET_EPOCH_ENTER(et);
1938         rc = (ifa_ifwithaddr(addr) != NULL);
1939         NET_EPOCH_EXIT(et);
1940         return (rc);
1941 }
1942
1943 /*
1944  * Locate an interface based on the broadcast address.
1945  */
1946 /* ARGSUSED */
1947 struct ifaddr *
1948 ifa_ifwithbroadaddr(const struct sockaddr *addr, int fibnum)
1949 {
1950         struct ifnet *ifp;
1951         struct ifaddr *ifa;
1952
1953         NET_EPOCH_ASSERT();
1954         CK_STAILQ_FOREACH(ifp, &V_ifnet, if_link) {
1955                 if ((fibnum != RT_ALL_FIBS) && (ifp->if_fib != fibnum))
1956                         continue;
1957                 CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
1958                         if (ifa->ifa_addr->sa_family != addr->sa_family)
1959                                 continue;
1960                         if ((ifp->if_flags & IFF_BROADCAST) &&
1961                             ifa->ifa_broadaddr &&
1962                             ifa->ifa_broadaddr->sa_len != 0 &&
1963                             sa_equal(ifa->ifa_broadaddr, addr)) {
1964                                 goto done;
1965                         }
1966                 }
1967         }
1968         ifa = NULL;
1969 done:
1970         return (ifa);
1971 }
1972
1973 /*
1974  * Locate the point to point interface with a given destination address.
1975  */
1976 /*ARGSUSED*/
1977 struct ifaddr *
1978 ifa_ifwithdstaddr(const struct sockaddr *addr, int fibnum)
1979 {
1980         struct ifnet *ifp;
1981         struct ifaddr *ifa;
1982
1983         NET_EPOCH_ASSERT();
1984         CK_STAILQ_FOREACH(ifp, &V_ifnet, if_link) {
1985                 if ((ifp->if_flags & IFF_POINTOPOINT) == 0)
1986                         continue;
1987                 if ((fibnum != RT_ALL_FIBS) && (ifp->if_fib != fibnum))
1988                         continue;
1989                 CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
1990                         if (ifa->ifa_addr->sa_family != addr->sa_family)
1991                                 continue;
1992                         if (ifa->ifa_dstaddr != NULL &&
1993                             sa_equal(addr, ifa->ifa_dstaddr)) {
1994                                 goto done;
1995                         }
1996                 }
1997         }
1998         ifa = NULL;
1999 done:
2000         return (ifa);
2001 }
2002
2003 /*
2004  * Find an interface on a specific network.  If many, choice
2005  * is most specific found.
2006  */
2007 struct ifaddr *
2008 ifa_ifwithnet(const struct sockaddr *addr, int ignore_ptp, int fibnum)
2009 {
2010         struct ifnet *ifp;
2011         struct ifaddr *ifa;
2012         struct ifaddr *ifa_maybe = NULL;
2013         u_int af = addr->sa_family;
2014         const char *addr_data = addr->sa_data, *cplim;
2015
2016         NET_EPOCH_ASSERT();
2017         /*
2018          * AF_LINK addresses can be looked up directly by their index number,
2019          * so do that if we can.
2020          */
2021         if (af == AF_LINK) {
2022             const struct sockaddr_dl *sdl = (const struct sockaddr_dl *)addr;
2023             if (sdl->sdl_index && sdl->sdl_index <= V_if_index)
2024                 return (ifaddr_byindex(sdl->sdl_index));
2025         }
2026
2027         /*
2028          * Scan though each interface, looking for ones that have addresses
2029          * in this address family and the requested fib.
2030          */
2031         CK_STAILQ_FOREACH(ifp, &V_ifnet, if_link) {
2032                 if ((fibnum != RT_ALL_FIBS) && (ifp->if_fib != fibnum))
2033                         continue;
2034                 CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
2035                         const char *cp, *cp2, *cp3;
2036
2037                         if (ifa->ifa_addr->sa_family != af)
2038 next:                           continue;
2039                         if (af == AF_INET && 
2040                             ifp->if_flags & IFF_POINTOPOINT && !ignore_ptp) {
2041                                 /*
2042                                  * This is a bit broken as it doesn't
2043                                  * take into account that the remote end may
2044                                  * be a single node in the network we are
2045                                  * looking for.
2046                                  * The trouble is that we don't know the
2047                                  * netmask for the remote end.
2048                                  */
2049                                 if (ifa->ifa_dstaddr != NULL &&
2050                                     sa_equal(addr, ifa->ifa_dstaddr)) {
2051                                         goto done;
2052                                 }
2053                         } else {
2054                                 /*
2055                                  * Scan all the bits in the ifa's address.
2056                                  * If a bit dissagrees with what we are
2057                                  * looking for, mask it with the netmask
2058                                  * to see if it really matters.
2059                                  * (A byte at a time)
2060                                  */
2061                                 if (ifa->ifa_netmask == 0)
2062                                         continue;
2063                                 cp = addr_data;
2064                                 cp2 = ifa->ifa_addr->sa_data;
2065                                 cp3 = ifa->ifa_netmask->sa_data;
2066                                 cplim = ifa->ifa_netmask->sa_len
2067                                         + (char *)ifa->ifa_netmask;
2068                                 while (cp3 < cplim)
2069                                         if ((*cp++ ^ *cp2++) & *cp3++)
2070                                                 goto next; /* next address! */
2071                                 /*
2072                                  * If the netmask of what we just found
2073                                  * is more specific than what we had before
2074                                  * (if we had one), or if the virtual status
2075                                  * of new prefix is better than of the old one,
2076                                  * then remember the new one before continuing
2077                                  * to search for an even better one.
2078                                  */
2079                                 if (ifa_maybe == NULL ||
2080                                     ifa_preferred(ifa_maybe, ifa) ||
2081                                     rn_refines((caddr_t)ifa->ifa_netmask,
2082                                     (caddr_t)ifa_maybe->ifa_netmask)) {
2083                                         ifa_maybe = ifa;
2084                                 }
2085                         }
2086                 }
2087         }
2088         ifa = ifa_maybe;
2089         ifa_maybe = NULL;
2090 done:
2091         return (ifa);
2092 }
2093
2094 /*
2095  * Find an interface address specific to an interface best matching
2096  * a given address.
2097  */
2098 struct ifaddr *
2099 ifaof_ifpforaddr(const struct sockaddr *addr, struct ifnet *ifp)
2100 {
2101         struct ifaddr *ifa;
2102         const char *cp, *cp2, *cp3;
2103         char *cplim;
2104         struct ifaddr *ifa_maybe = NULL;
2105         u_int af = addr->sa_family;
2106
2107         if (af >= AF_MAX)
2108                 return (NULL);
2109
2110         NET_EPOCH_ASSERT();
2111         CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
2112                 if (ifa->ifa_addr->sa_family != af)
2113                         continue;
2114                 if (ifa_maybe == NULL)
2115                         ifa_maybe = ifa;
2116                 if (ifa->ifa_netmask == 0) {
2117                         if (sa_equal(addr, ifa->ifa_addr) ||
2118                             (ifa->ifa_dstaddr &&
2119                             sa_equal(addr, ifa->ifa_dstaddr)))
2120                                 goto done;
2121                         continue;
2122                 }
2123                 if (ifp->if_flags & IFF_POINTOPOINT) {
2124                         if (sa_equal(addr, ifa->ifa_dstaddr))
2125                                 goto done;
2126                 } else {
2127                         cp = addr->sa_data;
2128                         cp2 = ifa->ifa_addr->sa_data;
2129                         cp3 = ifa->ifa_netmask->sa_data;
2130                         cplim = ifa->ifa_netmask->sa_len + (char *)ifa->ifa_netmask;
2131                         for (; cp3 < cplim; cp3++)
2132                                 if ((*cp++ ^ *cp2++) & *cp3)
2133                                         break;
2134                         if (cp3 == cplim)
2135                                 goto done;
2136                 }
2137         }
2138         ifa = ifa_maybe;
2139 done:
2140         return (ifa);
2141 }
2142
2143 /*
2144  * See whether new ifa is better than current one:
2145  * 1) A non-virtual one is preferred over virtual.
2146  * 2) A virtual in master state preferred over any other state.
2147  *
2148  * Used in several address selecting functions.
2149  */
2150 int
2151 ifa_preferred(struct ifaddr *cur, struct ifaddr *next)
2152 {
2153
2154         return (cur->ifa_carp && (!next->ifa_carp ||
2155             ((*carp_master_p)(next) && !(*carp_master_p)(cur))));
2156 }
2157
2158 struct sockaddr_dl *
2159 link_alloc_sdl(size_t size, int flags)
2160 {
2161
2162         return (malloc(size, M_TEMP, flags));
2163 }
2164
2165 void
2166 link_free_sdl(struct sockaddr *sa)
2167 {
2168         free(sa, M_TEMP);
2169 }
2170
2171 /*
2172  * Fills in given sdl with interface basic info.
2173  * Returns pointer to filled sdl.
2174  */
2175 struct sockaddr_dl *
2176 link_init_sdl(struct ifnet *ifp, struct sockaddr *paddr, u_char iftype)
2177 {
2178         struct sockaddr_dl *sdl;
2179
2180         sdl = (struct sockaddr_dl *)paddr;
2181         memset(sdl, 0, sizeof(struct sockaddr_dl));
2182         sdl->sdl_len = sizeof(struct sockaddr_dl);
2183         sdl->sdl_family = AF_LINK;
2184         sdl->sdl_index = ifp->if_index;
2185         sdl->sdl_type = iftype;
2186
2187         return (sdl);
2188 }
2189
2190 /*
2191  * Mark an interface down and notify protocols of
2192  * the transition.
2193  */
2194 static void
2195 if_unroute(struct ifnet *ifp, int flag, int fam)
2196 {
2197         struct ifaddr *ifa;
2198
2199         KASSERT(flag == IFF_UP, ("if_unroute: flag != IFF_UP"));
2200
2201         ifp->if_flags &= ~flag;
2202         getmicrotime(&ifp->if_lastchange);
2203         CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link)
2204                 if (fam == PF_UNSPEC || (fam == ifa->ifa_addr->sa_family))
2205                         pfctlinput(PRC_IFDOWN, ifa->ifa_addr);
2206         ifp->if_qflush(ifp);
2207
2208         if (ifp->if_carp)
2209                 (*carp_linkstate_p)(ifp);
2210         rt_ifmsg(ifp);
2211 }
2212
2213 /*
2214  * Mark an interface up and notify protocols of
2215  * the transition.
2216  */
2217 static void
2218 if_route(struct ifnet *ifp, int flag, int fam)
2219 {
2220         struct ifaddr *ifa;
2221
2222         KASSERT(flag == IFF_UP, ("if_route: flag != IFF_UP"));
2223
2224         ifp->if_flags |= flag;
2225         getmicrotime(&ifp->if_lastchange);
2226         CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link)
2227                 if (fam == PF_UNSPEC || (fam == ifa->ifa_addr->sa_family))
2228                         pfctlinput(PRC_IFUP, ifa->ifa_addr);
2229         if (ifp->if_carp)
2230                 (*carp_linkstate_p)(ifp);
2231         rt_ifmsg(ifp);
2232 #ifdef INET6
2233         in6_if_up(ifp);
2234 #endif
2235 }
2236
2237 void    (*vlan_link_state_p)(struct ifnet *);   /* XXX: private from if_vlan */
2238 void    (*vlan_trunk_cap_p)(struct ifnet *);            /* XXX: private from if_vlan */
2239 struct ifnet *(*vlan_trunkdev_p)(struct ifnet *);
2240 struct  ifnet *(*vlan_devat_p)(struct ifnet *, uint16_t);
2241 int     (*vlan_tag_p)(struct ifnet *, uint16_t *);
2242 int     (*vlan_pcp_p)(struct ifnet *, uint16_t *);
2243 int     (*vlan_setcookie_p)(struct ifnet *, void *);
2244 void    *(*vlan_cookie_p)(struct ifnet *);
2245
2246 /*
2247  * Handle a change in the interface link state. To avoid LORs
2248  * between driver lock and upper layer locks, as well as possible
2249  * recursions, we post event to taskqueue, and all job
2250  * is done in static do_link_state_change().
2251  */
2252 void
2253 if_link_state_change(struct ifnet *ifp, int link_state)
2254 {
2255         /* Return if state hasn't changed. */
2256         if (ifp->if_link_state == link_state)
2257                 return;
2258
2259         ifp->if_link_state = link_state;
2260
2261         /* XXXGL: reference ifp? */
2262         taskqueue_enqueue(taskqueue_swi, &ifp->if_linktask);
2263 }
2264
2265 static void
2266 do_link_state_change(void *arg, int pending)
2267 {
2268         struct ifnet *ifp;
2269         int link_state;
2270
2271         ifp = arg;
2272         link_state = ifp->if_link_state;
2273
2274         CURVNET_SET(ifp->if_vnet);
2275         rt_ifmsg(ifp);
2276         if (ifp->if_vlantrunk != NULL)
2277                 (*vlan_link_state_p)(ifp);
2278
2279         if ((ifp->if_type == IFT_ETHER || ifp->if_type == IFT_L2VLAN) &&
2280             ifp->if_l2com != NULL)
2281                 (*ng_ether_link_state_p)(ifp, link_state);
2282         if (ifp->if_carp)
2283                 (*carp_linkstate_p)(ifp);
2284         if (ifp->if_bridge)
2285                 ifp->if_bridge_linkstate(ifp);
2286         if (ifp->if_lagg)
2287                 (*lagg_linkstate_p)(ifp, link_state);
2288
2289         if (IS_DEFAULT_VNET(curvnet))
2290                 devctl_notify("IFNET", ifp->if_xname,
2291                     (link_state == LINK_STATE_UP) ? "LINK_UP" : "LINK_DOWN",
2292                     NULL);
2293         if (pending > 1)
2294                 if_printf(ifp, "%d link states coalesced\n", pending);
2295         if (log_link_state_change)
2296                 if_printf(ifp, "link state changed to %s\n",
2297                     (link_state == LINK_STATE_UP) ? "UP" : "DOWN" );
2298         EVENTHANDLER_INVOKE(ifnet_link_event, ifp, link_state);
2299         CURVNET_RESTORE();
2300 }
2301
2302 /*
2303  * Mark an interface down and notify protocols of
2304  * the transition.
2305  */
2306 void
2307 if_down(struct ifnet *ifp)
2308 {
2309
2310         EVENTHANDLER_INVOKE(ifnet_event, ifp, IFNET_EVENT_DOWN);
2311         if_unroute(ifp, IFF_UP, AF_UNSPEC);
2312 }
2313
2314 /*
2315  * Mark an interface up and notify protocols of
2316  * the transition.
2317  */
2318 void
2319 if_up(struct ifnet *ifp)
2320 {
2321
2322         if_route(ifp, IFF_UP, AF_UNSPEC);
2323         EVENTHANDLER_INVOKE(ifnet_event, ifp, IFNET_EVENT_UP);
2324 }
2325
2326 /*
2327  * Flush an interface queue.
2328  */
2329 void
2330 if_qflush(struct ifnet *ifp)
2331 {
2332         struct mbuf *m, *n;
2333         struct ifaltq *ifq;
2334
2335         ifq = &ifp->if_snd;
2336         IFQ_LOCK(ifq);
2337 #ifdef ALTQ
2338         if (ALTQ_IS_ENABLED(ifq))
2339                 ALTQ_PURGE(ifq);
2340 #endif
2341         n = ifq->ifq_head;
2342         while ((m = n) != NULL) {
2343                 n = m->m_nextpkt;
2344                 m_freem(m);
2345         }
2346         ifq->ifq_head = 0;
2347         ifq->ifq_tail = 0;
2348         ifq->ifq_len = 0;
2349         IFQ_UNLOCK(ifq);
2350 }
2351
2352 /*
2353  * Map interface name to interface structure pointer, with or without
2354  * returning a reference.
2355  */
2356 struct ifnet *
2357 ifunit_ref(const char *name)
2358 {
2359         struct epoch_tracker et;
2360         struct ifnet *ifp;
2361
2362         NET_EPOCH_ENTER(et);
2363         CK_STAILQ_FOREACH(ifp, &V_ifnet, if_link) {
2364                 if (strncmp(name, ifp->if_xname, IFNAMSIZ) == 0 &&
2365                     !(ifp->if_flags & IFF_DYING))
2366                         break;
2367         }
2368         if (ifp != NULL)
2369                 if_ref(ifp);
2370         NET_EPOCH_EXIT(et);
2371         return (ifp);
2372 }
2373
2374 struct ifnet *
2375 ifunit(const char *name)
2376 {
2377         struct epoch_tracker et;
2378         struct ifnet *ifp;
2379
2380         NET_EPOCH_ENTER(et);
2381         CK_STAILQ_FOREACH(ifp, &V_ifnet, if_link) {
2382                 if (strncmp(name, ifp->if_xname, IFNAMSIZ) == 0)
2383                         break;
2384         }
2385         NET_EPOCH_EXIT(et);
2386         return (ifp);
2387 }
2388
2389 void *
2390 ifr_buffer_get_buffer(void *data)
2391 {
2392         union ifreq_union *ifrup;
2393
2394         ifrup = data;
2395 #ifdef COMPAT_FREEBSD32
2396         if (SV_CURPROC_FLAG(SV_ILP32))
2397                 return ((void *)(uintptr_t)
2398                     ifrup->ifr32.ifr_ifru.ifru_buffer.buffer);
2399 #endif
2400         return (ifrup->ifr.ifr_ifru.ifru_buffer.buffer);
2401 }
2402
2403 static void
2404 ifr_buffer_set_buffer_null(void *data)
2405 {
2406         union ifreq_union *ifrup;
2407
2408         ifrup = data;
2409 #ifdef COMPAT_FREEBSD32
2410         if (SV_CURPROC_FLAG(SV_ILP32))
2411                 ifrup->ifr32.ifr_ifru.ifru_buffer.buffer = 0;
2412         else
2413 #endif
2414                 ifrup->ifr.ifr_ifru.ifru_buffer.buffer = NULL;
2415 }
2416
2417 size_t
2418 ifr_buffer_get_length(void *data)
2419 {
2420         union ifreq_union *ifrup;
2421
2422         ifrup = data;
2423 #ifdef COMPAT_FREEBSD32
2424         if (SV_CURPROC_FLAG(SV_ILP32))
2425                 return (ifrup->ifr32.ifr_ifru.ifru_buffer.length);
2426 #endif
2427         return (ifrup->ifr.ifr_ifru.ifru_buffer.length);
2428 }
2429
2430 static void
2431 ifr_buffer_set_length(void *data, size_t len)
2432 {
2433         union ifreq_union *ifrup;
2434
2435         ifrup = data;
2436 #ifdef COMPAT_FREEBSD32
2437         if (SV_CURPROC_FLAG(SV_ILP32))
2438                 ifrup->ifr32.ifr_ifru.ifru_buffer.length = len;
2439         else
2440 #endif
2441                 ifrup->ifr.ifr_ifru.ifru_buffer.length = len;
2442 }
2443
2444 void *
2445 ifr_data_get_ptr(void *ifrp)
2446 {
2447         union ifreq_union *ifrup;
2448
2449         ifrup = ifrp;
2450 #ifdef COMPAT_FREEBSD32
2451         if (SV_CURPROC_FLAG(SV_ILP32))
2452                 return ((void *)(uintptr_t)
2453                     ifrup->ifr32.ifr_ifru.ifru_data);
2454 #endif
2455                 return (ifrup->ifr.ifr_ifru.ifru_data);
2456 }
2457
2458 /*
2459  * Hardware specific interface ioctls.
2460  */
2461 int
2462 ifhwioctl(u_long cmd, struct ifnet *ifp, caddr_t data, struct thread *td)
2463 {
2464         struct ifreq *ifr;
2465         int error = 0, do_ifup = 0;
2466         int new_flags, temp_flags;
2467         size_t namelen, onamelen;
2468         size_t descrlen;
2469         char *descrbuf, *odescrbuf;
2470         char new_name[IFNAMSIZ];
2471         struct ifaddr *ifa;
2472         struct sockaddr_dl *sdl;
2473
2474         ifr = (struct ifreq *)data;
2475         switch (cmd) {
2476         case SIOCGIFINDEX:
2477                 ifr->ifr_index = ifp->if_index;
2478                 break;
2479
2480         case SIOCGIFFLAGS:
2481                 temp_flags = ifp->if_flags | ifp->if_drv_flags;
2482                 ifr->ifr_flags = temp_flags & 0xffff;
2483                 ifr->ifr_flagshigh = temp_flags >> 16;
2484                 break;
2485
2486         case SIOCGIFCAP:
2487                 ifr->ifr_reqcap = ifp->if_capabilities;
2488                 ifr->ifr_curcap = ifp->if_capenable;
2489                 break;
2490
2491         case SIOCGIFDATA:
2492         {
2493                 struct if_data ifd;
2494
2495                 /* Ensure uninitialised padding is not leaked. */
2496                 memset(&ifd, 0, sizeof(ifd));
2497
2498                 if_data_copy(ifp, &ifd);
2499                 error = copyout(&ifd, ifr_data_get_ptr(ifr), sizeof(ifd));
2500                 break;
2501         }
2502
2503 #ifdef MAC
2504         case SIOCGIFMAC:
2505                 error = mac_ifnet_ioctl_get(td->td_ucred, ifr, ifp);
2506                 break;
2507 #endif
2508
2509         case SIOCGIFMETRIC:
2510                 ifr->ifr_metric = ifp->if_metric;
2511                 break;
2512
2513         case SIOCGIFMTU:
2514                 ifr->ifr_mtu = ifp->if_mtu;
2515                 break;
2516
2517         case SIOCGIFPHYS:
2518                 /* XXXGL: did this ever worked? */
2519                 ifr->ifr_phys = 0;
2520                 break;
2521
2522         case SIOCGIFDESCR:
2523                 error = 0;
2524                 sx_slock(&ifdescr_sx);
2525                 if (ifp->if_description == NULL)
2526                         error = ENOMSG;
2527                 else {
2528                         /* space for terminating nul */
2529                         descrlen = strlen(ifp->if_description) + 1;
2530                         if (ifr_buffer_get_length(ifr) < descrlen)
2531                                 ifr_buffer_set_buffer_null(ifr);
2532                         else
2533                                 error = copyout(ifp->if_description,
2534                                     ifr_buffer_get_buffer(ifr), descrlen);
2535                         ifr_buffer_set_length(ifr, descrlen);
2536                 }
2537                 sx_sunlock(&ifdescr_sx);
2538                 break;
2539
2540         case SIOCSIFDESCR:
2541                 error = priv_check(td, PRIV_NET_SETIFDESCR);
2542                 if (error)
2543                         return (error);
2544
2545                 /*
2546                  * Copy only (length-1) bytes to make sure that
2547                  * if_description is always nul terminated.  The
2548                  * length parameter is supposed to count the
2549                  * terminating nul in.
2550                  */
2551                 if (ifr_buffer_get_length(ifr) > ifdescr_maxlen)
2552                         return (ENAMETOOLONG);
2553                 else if (ifr_buffer_get_length(ifr) == 0)
2554                         descrbuf = NULL;
2555                 else {
2556                         descrbuf = malloc(ifr_buffer_get_length(ifr),
2557                             M_IFDESCR, M_WAITOK | M_ZERO);
2558                         error = copyin(ifr_buffer_get_buffer(ifr), descrbuf,
2559                             ifr_buffer_get_length(ifr) - 1);
2560                         if (error) {
2561                                 free(descrbuf, M_IFDESCR);
2562                                 break;
2563                         }
2564                 }
2565
2566                 sx_xlock(&ifdescr_sx);
2567                 odescrbuf = ifp->if_description;
2568                 ifp->if_description = descrbuf;
2569                 sx_xunlock(&ifdescr_sx);
2570
2571                 getmicrotime(&ifp->if_lastchange);
2572                 free(odescrbuf, M_IFDESCR);
2573                 break;
2574
2575         case SIOCGIFFIB:
2576                 ifr->ifr_fib = ifp->if_fib;
2577                 break;
2578
2579         case SIOCSIFFIB:
2580                 error = priv_check(td, PRIV_NET_SETIFFIB);
2581                 if (error)
2582                         return (error);
2583                 if (ifr->ifr_fib >= rt_numfibs)
2584                         return (EINVAL);
2585
2586                 ifp->if_fib = ifr->ifr_fib;
2587                 break;
2588
2589         case SIOCSIFFLAGS:
2590                 error = priv_check(td, PRIV_NET_SETIFFLAGS);
2591                 if (error)
2592                         return (error);
2593                 /*
2594                  * Currently, no driver owned flags pass the IFF_CANTCHANGE
2595                  * check, so we don't need special handling here yet.
2596                  */
2597                 new_flags = (ifr->ifr_flags & 0xffff) |
2598                     (ifr->ifr_flagshigh << 16);
2599                 if (ifp->if_flags & IFF_UP &&
2600                     (new_flags & IFF_UP) == 0) {
2601                         if_down(ifp);
2602                 } else if (new_flags & IFF_UP &&
2603                     (ifp->if_flags & IFF_UP) == 0) {
2604                         do_ifup = 1;
2605                 }
2606                 /* See if permanently promiscuous mode bit is about to flip */
2607                 if ((ifp->if_flags ^ new_flags) & IFF_PPROMISC) {
2608                         if (new_flags & IFF_PPROMISC)
2609                                 ifp->if_flags |= IFF_PROMISC;
2610                         else if (ifp->if_pcount == 0)
2611                                 ifp->if_flags &= ~IFF_PROMISC;
2612                         if (log_promisc_mode_change)
2613                                 if_printf(ifp, "permanently promiscuous mode %s\n",
2614                                     ((new_flags & IFF_PPROMISC) ?
2615                                      "enabled" : "disabled"));
2616                 }
2617                 ifp->if_flags = (ifp->if_flags & IFF_CANTCHANGE) |
2618                         (new_flags &~ IFF_CANTCHANGE);
2619                 if (ifp->if_ioctl) {
2620                         (void) (*ifp->if_ioctl)(ifp, cmd, data);
2621                 }
2622                 if (do_ifup)
2623                         if_up(ifp);
2624                 getmicrotime(&ifp->if_lastchange);
2625                 break;
2626
2627         case SIOCSIFCAP:
2628                 error = priv_check(td, PRIV_NET_SETIFCAP);
2629                 if (error)
2630                         return (error);
2631                 if (ifp->if_ioctl == NULL)
2632                         return (EOPNOTSUPP);
2633                 if (ifr->ifr_reqcap & ~ifp->if_capabilities)
2634                         return (EINVAL);
2635                 error = (*ifp->if_ioctl)(ifp, cmd, data);
2636                 if (error == 0)
2637                         getmicrotime(&ifp->if_lastchange);
2638                 break;
2639
2640 #ifdef MAC
2641         case SIOCSIFMAC:
2642                 error = mac_ifnet_ioctl_set(td->td_ucred, ifr, ifp);
2643                 break;
2644 #endif
2645
2646         case SIOCSIFNAME:
2647                 error = priv_check(td, PRIV_NET_SETIFNAME);
2648                 if (error)
2649                         return (error);
2650                 error = copyinstr(ifr_data_get_ptr(ifr), new_name, IFNAMSIZ,
2651                     NULL);
2652                 if (error != 0)
2653                         return (error);
2654                 if (new_name[0] == '\0')
2655                         return (EINVAL);
2656                 if (new_name[IFNAMSIZ-1] != '\0') {
2657                         new_name[IFNAMSIZ-1] = '\0';
2658                         if (strlen(new_name) == IFNAMSIZ-1)
2659                                 return (EINVAL);
2660                 }
2661                 if (strcmp(new_name, ifp->if_xname) == 0)
2662                         break;
2663                 if (ifunit(new_name) != NULL)
2664                         return (EEXIST);
2665
2666                 /*
2667                  * XXX: Locking.  Nothing else seems to lock if_flags,
2668                  * and there are numerous other races with the
2669                  * ifunit() checks not being atomic with namespace
2670                  * changes (renames, vmoves, if_attach, etc).
2671                  */
2672                 ifp->if_flags |= IFF_RENAMING;
2673                 
2674                 /* Announce the departure of the interface. */
2675                 rt_ifannouncemsg(ifp, IFAN_DEPARTURE);
2676                 EVENTHANDLER_INVOKE(ifnet_departure_event, ifp);
2677
2678                 if_printf(ifp, "changing name to '%s'\n", new_name);
2679
2680                 IF_ADDR_WLOCK(ifp);
2681                 strlcpy(ifp->if_xname, new_name, sizeof(ifp->if_xname));
2682                 ifa = ifp->if_addr;
2683                 sdl = (struct sockaddr_dl *)ifa->ifa_addr;
2684                 namelen = strlen(new_name);
2685                 onamelen = sdl->sdl_nlen;
2686                 /*
2687                  * Move the address if needed.  This is safe because we
2688                  * allocate space for a name of length IFNAMSIZ when we
2689                  * create this in if_attach().
2690                  */
2691                 if (namelen != onamelen) {
2692                         bcopy(sdl->sdl_data + onamelen,
2693                             sdl->sdl_data + namelen, sdl->sdl_alen);
2694                 }
2695                 bcopy(new_name, sdl->sdl_data, namelen);
2696                 sdl->sdl_nlen = namelen;
2697                 sdl = (struct sockaddr_dl *)ifa->ifa_netmask;
2698                 bzero(sdl->sdl_data, onamelen);
2699                 while (namelen != 0)
2700                         sdl->sdl_data[--namelen] = 0xff;
2701                 IF_ADDR_WUNLOCK(ifp);
2702
2703                 EVENTHANDLER_INVOKE(ifnet_arrival_event, ifp);
2704                 /* Announce the return of the interface. */
2705                 rt_ifannouncemsg(ifp, IFAN_ARRIVAL);
2706
2707                 ifp->if_flags &= ~IFF_RENAMING;
2708                 break;
2709
2710 #ifdef VIMAGE
2711         case SIOCSIFVNET:
2712                 error = priv_check(td, PRIV_NET_SETIFVNET);
2713                 if (error)
2714                         return (error);
2715                 error = if_vmove_loan(td, ifp, ifr->ifr_name, ifr->ifr_jid);
2716                 break;
2717 #endif
2718
2719         case SIOCSIFMETRIC:
2720                 error = priv_check(td, PRIV_NET_SETIFMETRIC);
2721                 if (error)
2722                         return (error);
2723                 ifp->if_metric = ifr->ifr_metric;
2724                 getmicrotime(&ifp->if_lastchange);
2725                 break;
2726
2727         case SIOCSIFPHYS:
2728                 error = priv_check(td, PRIV_NET_SETIFPHYS);
2729                 if (error)
2730                         return (error);
2731                 if (ifp->if_ioctl == NULL)
2732                         return (EOPNOTSUPP);
2733                 error = (*ifp->if_ioctl)(ifp, cmd, data);
2734                 if (error == 0)
2735                         getmicrotime(&ifp->if_lastchange);
2736                 break;
2737
2738         case SIOCSIFMTU:
2739         {
2740                 u_long oldmtu = ifp->if_mtu;
2741
2742                 error = priv_check(td, PRIV_NET_SETIFMTU);
2743                 if (error)
2744                         return (error);
2745                 if (ifr->ifr_mtu < IF_MINMTU || ifr->ifr_mtu > IF_MAXMTU)
2746                         return (EINVAL);
2747                 if (ifp->if_ioctl == NULL)
2748                         return (EOPNOTSUPP);
2749                 error = (*ifp->if_ioctl)(ifp, cmd, data);
2750                 if (error == 0) {
2751                         getmicrotime(&ifp->if_lastchange);
2752                         rt_ifmsg(ifp);
2753 #ifdef INET
2754                         DEBUGNET_NOTIFY_MTU(ifp);
2755 #endif
2756                 }
2757                 /*
2758                  * If the link MTU changed, do network layer specific procedure.
2759                  */
2760                 if (ifp->if_mtu != oldmtu) {
2761 #ifdef INET6
2762                         nd6_setmtu(ifp);
2763 #endif
2764                         rt_updatemtu(ifp);
2765                 }
2766                 break;
2767         }
2768
2769         case SIOCADDMULTI:
2770         case SIOCDELMULTI:
2771                 if (cmd == SIOCADDMULTI)
2772                         error = priv_check(td, PRIV_NET_ADDMULTI);
2773                 else
2774                         error = priv_check(td, PRIV_NET_DELMULTI);
2775                 if (error)
2776                         return (error);
2777
2778                 /* Don't allow group membership on non-multicast interfaces. */
2779                 if ((ifp->if_flags & IFF_MULTICAST) == 0)
2780                         return (EOPNOTSUPP);
2781
2782                 /* Don't let users screw up protocols' entries. */
2783                 if (ifr->ifr_addr.sa_family != AF_LINK)
2784                         return (EINVAL);
2785
2786                 if (cmd == SIOCADDMULTI) {
2787                         struct epoch_tracker et;
2788                         struct ifmultiaddr *ifma;
2789
2790                         /*
2791                          * Userland is only permitted to join groups once
2792                          * via the if_addmulti() KPI, because it cannot hold
2793                          * struct ifmultiaddr * between calls. It may also
2794                          * lose a race while we check if the membership
2795                          * already exists.
2796                          */
2797                         NET_EPOCH_ENTER(et);
2798                         ifma = if_findmulti(ifp, &ifr->ifr_addr);
2799                         NET_EPOCH_EXIT(et);
2800                         if (ifma != NULL)
2801                                 error = EADDRINUSE;
2802                         else
2803                                 error = if_addmulti(ifp, &ifr->ifr_addr, &ifma);
2804                 } else {
2805                         error = if_delmulti(ifp, &ifr->ifr_addr);
2806                 }
2807                 if (error == 0)
2808                         getmicrotime(&ifp->if_lastchange);
2809                 break;
2810
2811         case SIOCSIFPHYADDR:
2812         case SIOCDIFPHYADDR:
2813 #ifdef INET6
2814         case SIOCSIFPHYADDR_IN6:
2815 #endif
2816         case SIOCSIFMEDIA:
2817         case SIOCSIFGENERIC:
2818                 error = priv_check(td, PRIV_NET_HWIOCTL);
2819                 if (error)
2820                         return (error);
2821                 if (ifp->if_ioctl == NULL)
2822                         return (EOPNOTSUPP);
2823                 error = (*ifp->if_ioctl)(ifp, cmd, data);
2824                 if (error == 0)
2825                         getmicrotime(&ifp->if_lastchange);
2826                 break;
2827
2828         case SIOCGIFSTATUS:
2829         case SIOCGIFPSRCADDR:
2830         case SIOCGIFPDSTADDR:
2831         case SIOCGIFMEDIA:
2832         case SIOCGIFXMEDIA:
2833         case SIOCGIFGENERIC:
2834         case SIOCGIFRSSKEY:
2835         case SIOCGIFRSSHASH:
2836         case SIOCGIFDOWNREASON:
2837                 if (ifp->if_ioctl == NULL)
2838                         return (EOPNOTSUPP);
2839                 error = (*ifp->if_ioctl)(ifp, cmd, data);
2840                 break;
2841
2842         case SIOCSIFLLADDR:
2843                 error = priv_check(td, PRIV_NET_SETLLADDR);
2844                 if (error)
2845                         return (error);
2846                 error = if_setlladdr(ifp,
2847                     ifr->ifr_addr.sa_data, ifr->ifr_addr.sa_len);
2848                 break;
2849
2850         case SIOCGHWADDR:
2851                 error = if_gethwaddr(ifp, ifr);
2852                 break;
2853
2854         case CASE_IOC_IFGROUPREQ(SIOCAIFGROUP):
2855                 error = priv_check(td, PRIV_NET_ADDIFGROUP);
2856                 if (error)
2857                         return (error);
2858                 if ((error = if_addgroup(ifp,
2859                     ifgr_group_get((struct ifgroupreq *)data))))
2860                         return (error);
2861                 break;
2862
2863         case CASE_IOC_IFGROUPREQ(SIOCGIFGROUP):
2864         {
2865                 struct epoch_tracker et;
2866
2867                 NET_EPOCH_ENTER(et);
2868                 error = if_getgroup((struct ifgroupreq *)data, ifp);
2869                 NET_EPOCH_EXIT(et);
2870                 break;
2871         }
2872
2873         case CASE_IOC_IFGROUPREQ(SIOCDIFGROUP):
2874                 error = priv_check(td, PRIV_NET_DELIFGROUP);
2875                 if (error)
2876                         return (error);
2877                 if ((error = if_delgroup(ifp,
2878                     ifgr_group_get((struct ifgroupreq *)data))))
2879                         return (error);
2880                 break;
2881
2882         default:
2883                 error = ENOIOCTL;
2884                 break;
2885         }
2886         return (error);
2887 }
2888
2889 #ifdef COMPAT_FREEBSD32
2890 struct ifconf32 {
2891         int32_t ifc_len;
2892         union {
2893                 uint32_t        ifcu_buf;
2894                 uint32_t        ifcu_req;
2895         } ifc_ifcu;
2896 };
2897 #define SIOCGIFCONF32   _IOWR('i', 36, struct ifconf32)
2898 #endif
2899
2900 #ifdef COMPAT_FREEBSD32
2901 static void
2902 ifmr_init(struct ifmediareq *ifmr, caddr_t data)
2903 {
2904         struct ifmediareq32 *ifmr32;
2905
2906         ifmr32 = (struct ifmediareq32 *)data;
2907         memcpy(ifmr->ifm_name, ifmr32->ifm_name,
2908             sizeof(ifmr->ifm_name));
2909         ifmr->ifm_current = ifmr32->ifm_current;
2910         ifmr->ifm_mask = ifmr32->ifm_mask;
2911         ifmr->ifm_status = ifmr32->ifm_status;
2912         ifmr->ifm_active = ifmr32->ifm_active;
2913         ifmr->ifm_count = ifmr32->ifm_count;
2914         ifmr->ifm_ulist = (int *)(uintptr_t)ifmr32->ifm_ulist;
2915 }
2916
2917 static void
2918 ifmr_update(const struct ifmediareq *ifmr, caddr_t data)
2919 {
2920         struct ifmediareq32 *ifmr32;
2921
2922         ifmr32 = (struct ifmediareq32 *)data;
2923         ifmr32->ifm_current = ifmr->ifm_current;
2924         ifmr32->ifm_mask = ifmr->ifm_mask;
2925         ifmr32->ifm_status = ifmr->ifm_status;
2926         ifmr32->ifm_active = ifmr->ifm_active;
2927         ifmr32->ifm_count = ifmr->ifm_count;
2928 }
2929 #endif
2930
2931 /*
2932  * Interface ioctls.
2933  */
2934 int
2935 ifioctl(struct socket *so, u_long cmd, caddr_t data, struct thread *td)
2936 {
2937 #ifdef COMPAT_FREEBSD32
2938         caddr_t saved_data = NULL;
2939         struct ifmediareq ifmr;
2940         struct ifmediareq *ifmrp = NULL;
2941 #endif
2942         struct ifnet *ifp;
2943         struct ifreq *ifr;
2944         int error;
2945         int oif_flags;
2946 #ifdef VIMAGE
2947         bool shutdown;
2948 #endif
2949
2950         CURVNET_SET(so->so_vnet);
2951 #ifdef VIMAGE
2952         /* Make sure the VNET is stable. */
2953         shutdown = VNET_IS_SHUTTING_DOWN(so->so_vnet);
2954         if (shutdown) {
2955                 CURVNET_RESTORE();
2956                 return (EBUSY);
2957         }
2958 #endif
2959
2960         switch (cmd) {
2961         case SIOCGIFCONF:
2962                 error = ifconf(cmd, data);
2963                 goto out_noref;
2964
2965 #ifdef COMPAT_FREEBSD32
2966         case SIOCGIFCONF32:
2967                 {
2968                         struct ifconf32 *ifc32;
2969                         struct ifconf ifc;
2970
2971                         ifc32 = (struct ifconf32 *)data;
2972                         ifc.ifc_len = ifc32->ifc_len;
2973                         ifc.ifc_buf = PTRIN(ifc32->ifc_buf);
2974
2975                         error = ifconf(SIOCGIFCONF, (void *)&ifc);
2976                         if (error == 0)
2977                                 ifc32->ifc_len = ifc.ifc_len;
2978                         goto out_noref;
2979                 }
2980 #endif
2981         }
2982
2983 #ifdef COMPAT_FREEBSD32
2984         switch (cmd) {
2985         case SIOCGIFMEDIA32:
2986         case SIOCGIFXMEDIA32:
2987                 ifmrp = &ifmr;
2988                 ifmr_init(ifmrp, data);
2989                 cmd = _IOC_NEWTYPE(cmd, struct ifmediareq);
2990                 saved_data = data;
2991                 data = (caddr_t)ifmrp;
2992         }
2993 #endif
2994
2995         ifr = (struct ifreq *)data;
2996         switch (cmd) {
2997 #ifdef VIMAGE
2998         case SIOCSIFRVNET:
2999                 error = priv_check(td, PRIV_NET_SETIFVNET);
3000                 if (error == 0)
3001                         error = if_vmove_reclaim(td, ifr->ifr_name,
3002                             ifr->ifr_jid);
3003                 goto out_noref;
3004 #endif
3005         case SIOCIFCREATE:
3006         case SIOCIFCREATE2:
3007                 error = priv_check(td, PRIV_NET_IFCREATE);
3008                 if (error == 0)
3009                         error = if_clone_create(ifr->ifr_name,
3010                             sizeof(ifr->ifr_name), cmd == SIOCIFCREATE2 ?
3011                             ifr_data_get_ptr(ifr) : NULL);
3012                 goto out_noref;
3013         case SIOCIFDESTROY:
3014                 error = priv_check(td, PRIV_NET_IFDESTROY);
3015
3016                 if (error == 0) {
3017                         sx_xlock(&ifnet_detach_sxlock);
3018                         error = if_clone_destroy(ifr->ifr_name);
3019                         sx_xunlock(&ifnet_detach_sxlock);
3020                 }
3021                 goto out_noref;
3022
3023         case SIOCIFGCLONERS:
3024                 error = if_clone_list((struct if_clonereq *)data);
3025                 goto out_noref;
3026
3027         case CASE_IOC_IFGROUPREQ(SIOCGIFGMEMB):
3028                 error = if_getgroupmembers((struct ifgroupreq *)data);
3029                 goto out_noref;
3030
3031 #if defined(INET) || defined(INET6)
3032         case SIOCSVH:
3033         case SIOCGVH:
3034                 if (carp_ioctl_p == NULL)
3035                         error = EPROTONOSUPPORT;
3036                 else
3037                         error = (*carp_ioctl_p)(ifr, cmd, td);
3038                 goto out_noref;
3039 #endif
3040         }
3041
3042         ifp = ifunit_ref(ifr->ifr_name);
3043         if (ifp == NULL) {
3044                 error = ENXIO;
3045                 goto out_noref;
3046         }
3047
3048         error = ifhwioctl(cmd, ifp, data, td);
3049         if (error != ENOIOCTL)
3050                 goto out_ref;
3051
3052         oif_flags = ifp->if_flags;
3053         if (so->so_proto == NULL) {
3054                 error = EOPNOTSUPP;
3055                 goto out_ref;
3056         }
3057
3058         /*
3059          * Pass the request on to the socket control method, and if the
3060          * latter returns EOPNOTSUPP, directly to the interface.
3061          *
3062          * Make an exception for the legacy SIOCSIF* requests.  Drivers
3063          * trust SIOCSIFADDR et al to come from an already privileged
3064          * layer, and do not perform any credentials checks or input
3065          * validation.
3066          */
3067         error = ((*so->so_proto->pr_usrreqs->pru_control)(so, cmd, data,
3068             ifp, td));
3069         if (error == EOPNOTSUPP && ifp != NULL && ifp->if_ioctl != NULL &&
3070             cmd != SIOCSIFADDR && cmd != SIOCSIFBRDADDR &&
3071             cmd != SIOCSIFDSTADDR && cmd != SIOCSIFNETMASK)
3072                 error = (*ifp->if_ioctl)(ifp, cmd, data);
3073
3074         if ((oif_flags ^ ifp->if_flags) & IFF_UP) {
3075 #ifdef INET6
3076                 if (ifp->if_flags & IFF_UP)
3077                         in6_if_up(ifp);
3078 #endif
3079         }
3080
3081 out_ref:
3082         if_rele(ifp);
3083 out_noref:
3084 #ifdef COMPAT_FREEBSD32
3085         if (ifmrp != NULL) {
3086                 KASSERT((cmd == SIOCGIFMEDIA || cmd == SIOCGIFXMEDIA),
3087                     ("ifmrp non-NULL, but cmd is not an ifmedia req 0x%lx",
3088                      cmd));
3089                 data = saved_data;
3090                 ifmr_update(ifmrp, data);
3091         }
3092 #endif
3093         CURVNET_RESTORE();
3094         return (error);
3095 }
3096
3097 /*
3098  * The code common to handling reference counted flags,
3099  * e.g., in ifpromisc() and if_allmulti().
3100  * The "pflag" argument can specify a permanent mode flag to check,
3101  * such as IFF_PPROMISC for promiscuous mode; should be 0 if none.
3102  *
3103  * Only to be used on stack-owned flags, not driver-owned flags.
3104  */
3105 static int
3106 if_setflag(struct ifnet *ifp, int flag, int pflag, int *refcount, int onswitch)
3107 {
3108         struct ifreq ifr;
3109         int error;
3110         int oldflags, oldcount;
3111
3112         /* Sanity checks to catch programming errors */
3113         KASSERT((flag & (IFF_DRV_OACTIVE|IFF_DRV_RUNNING)) == 0,
3114             ("%s: setting driver-owned flag %d", __func__, flag));
3115
3116         if (onswitch)
3117                 KASSERT(*refcount >= 0,
3118                     ("%s: increment negative refcount %d for flag %d",
3119                     __func__, *refcount, flag));
3120         else
3121                 KASSERT(*refcount > 0,
3122                     ("%s: decrement non-positive refcount %d for flag %d",
3123                     __func__, *refcount, flag));
3124
3125         /* In case this mode is permanent, just touch refcount */
3126         if (ifp->if_flags & pflag) {
3127                 *refcount += onswitch ? 1 : -1;
3128                 return (0);
3129         }
3130
3131         /* Save ifnet parameters for if_ioctl() may fail */
3132         oldcount = *refcount;
3133         oldflags = ifp->if_flags;
3134
3135         /*
3136          * See if we aren't the only and touching refcount is enough.
3137          * Actually toggle interface flag if we are the first or last.
3138          */
3139         if (onswitch) {
3140                 if ((*refcount)++)
3141                         return (0);
3142                 ifp->if_flags |= flag;
3143         } else {
3144                 if (--(*refcount))
3145                         return (0);
3146                 ifp->if_flags &= ~flag;
3147         }
3148
3149         /* Call down the driver since we've changed interface flags */
3150         if (ifp->if_ioctl == NULL) {
3151                 error = EOPNOTSUPP;
3152                 goto recover;
3153         }
3154         ifr.ifr_flags = ifp->if_flags & 0xffff;
3155         ifr.ifr_flagshigh = ifp->if_flags >> 16;
3156         error = (*ifp->if_ioctl)(ifp, SIOCSIFFLAGS, (caddr_t)&ifr);
3157         if (error)
3158                 goto recover;
3159         /* Notify userland that interface flags have changed */
3160         rt_ifmsg(ifp);
3161         return (0);
3162
3163 recover:
3164         /* Recover after driver error */
3165         *refcount = oldcount;
3166         ifp->if_flags = oldflags;
3167         return (error);
3168 }
3169
3170 /*
3171  * Set/clear promiscuous mode on interface ifp based on the truth value
3172  * of pswitch.  The calls are reference counted so that only the first
3173  * "on" request actually has an effect, as does the final "off" request.
3174  * Results are undefined if the "off" and "on" requests are not matched.
3175  */
3176 int
3177 ifpromisc(struct ifnet *ifp, int pswitch)
3178 {
3179         int error;
3180         int oldflags = ifp->if_flags;
3181
3182         error = if_setflag(ifp, IFF_PROMISC, IFF_PPROMISC,
3183                            &ifp->if_pcount, pswitch);
3184         /* If promiscuous mode status has changed, log a message */
3185         if (error == 0 && ((ifp->if_flags ^ oldflags) & IFF_PROMISC) &&
3186             log_promisc_mode_change)
3187                 if_printf(ifp, "promiscuous mode %s\n",
3188                     (ifp->if_flags & IFF_PROMISC) ? "enabled" : "disabled");
3189         return (error);
3190 }
3191
3192 /*
3193  * Return interface configuration
3194  * of system.  List may be used
3195  * in later ioctl's (above) to get
3196  * other information.
3197  */
3198 /*ARGSUSED*/
3199 static int
3200 ifconf(u_long cmd, caddr_t data)
3201 {
3202         struct ifconf *ifc = (struct ifconf *)data;
3203         struct ifnet *ifp;
3204         struct ifaddr *ifa;
3205         struct ifreq ifr;
3206         struct sbuf *sb;
3207         int error, full = 0, valid_len, max_len;
3208
3209         /* Limit initial buffer size to maxphys to avoid DoS from userspace. */
3210         max_len = maxphys - 1;
3211
3212         /* Prevent hostile input from being able to crash the system */
3213         if (ifc->ifc_len <= 0)
3214                 return (EINVAL);
3215
3216 again:
3217         if (ifc->ifc_len <= max_len) {
3218                 max_len = ifc->ifc_len;
3219                 full = 1;
3220         }
3221         sb = sbuf_new(NULL, NULL, max_len + 1, SBUF_FIXEDLEN);
3222         max_len = 0;
3223         valid_len = 0;
3224
3225         IFNET_RLOCK();
3226         CK_STAILQ_FOREACH(ifp, &V_ifnet, if_link) {
3227                 struct epoch_tracker et;
3228                 int addrs;
3229
3230                 /*
3231                  * Zero the ifr to make sure we don't disclose the contents
3232                  * of the stack.
3233                  */
3234                 memset(&ifr, 0, sizeof(ifr));
3235
3236                 if (strlcpy(ifr.ifr_name, ifp->if_xname, sizeof(ifr.ifr_name))
3237                     >= sizeof(ifr.ifr_name)) {
3238                         sbuf_delete(sb);
3239                         IFNET_RUNLOCK();
3240                         return (ENAMETOOLONG);
3241                 }
3242
3243                 addrs = 0;
3244                 NET_EPOCH_ENTER(et);
3245                 CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
3246                         struct sockaddr *sa = ifa->ifa_addr;
3247
3248                         if (prison_if(curthread->td_ucred, sa) != 0)
3249                                 continue;
3250                         addrs++;
3251                         if (sa->sa_len <= sizeof(*sa)) {
3252                                 if (sa->sa_len < sizeof(*sa)) {
3253                                         memset(&ifr.ifr_ifru.ifru_addr, 0,
3254                                             sizeof(ifr.ifr_ifru.ifru_addr));
3255                                         memcpy(&ifr.ifr_ifru.ifru_addr, sa,
3256                                             sa->sa_len);
3257                                 } else
3258                                         ifr.ifr_ifru.ifru_addr = *sa;
3259                                 sbuf_bcat(sb, &ifr, sizeof(ifr));
3260                                 max_len += sizeof(ifr);
3261                         } else {
3262                                 sbuf_bcat(sb, &ifr,
3263                                     offsetof(struct ifreq, ifr_addr));
3264                                 max_len += offsetof(struct ifreq, ifr_addr);
3265                                 sbuf_bcat(sb, sa, sa->sa_len);
3266                                 max_len += sa->sa_len;
3267                         }
3268
3269                         if (sbuf_error(sb) == 0)
3270                                 valid_len = sbuf_len(sb);
3271                 }
3272                 NET_EPOCH_EXIT(et);
3273                 if (addrs == 0) {
3274                         sbuf_bcat(sb, &ifr, sizeof(ifr));
3275                         max_len += sizeof(ifr);
3276
3277                         if (sbuf_error(sb) == 0)
3278                                 valid_len = sbuf_len(sb);
3279                 }
3280         }
3281         IFNET_RUNLOCK();
3282
3283         /*
3284          * If we didn't allocate enough space (uncommon), try again.  If
3285          * we have already allocated as much space as we are allowed,
3286          * return what we've got.
3287          */
3288         if (valid_len != max_len && !full) {
3289                 sbuf_delete(sb);
3290                 goto again;
3291         }
3292
3293         ifc->ifc_len = valid_len;
3294         sbuf_finish(sb);
3295         error = copyout(sbuf_data(sb), ifc->ifc_req, ifc->ifc_len);
3296         sbuf_delete(sb);
3297         return (error);
3298 }
3299
3300 /*
3301  * Just like ifpromisc(), but for all-multicast-reception mode.
3302  */
3303 int
3304 if_allmulti(struct ifnet *ifp, int onswitch)
3305 {
3306
3307         return (if_setflag(ifp, IFF_ALLMULTI, 0, &ifp->if_amcount, onswitch));
3308 }
3309
3310 struct ifmultiaddr *
3311 if_findmulti(struct ifnet *ifp, const struct sockaddr *sa)
3312 {
3313         struct ifmultiaddr *ifma;
3314
3315         IF_ADDR_LOCK_ASSERT(ifp);
3316
3317         CK_STAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
3318                 if (sa->sa_family == AF_LINK) {
3319                         if (sa_dl_equal(ifma->ifma_addr, sa))
3320                                 break;
3321                 } else {
3322                         if (sa_equal(ifma->ifma_addr, sa))
3323                                 break;
3324                 }
3325         }
3326
3327         return ifma;
3328 }
3329
3330 /*
3331  * Allocate a new ifmultiaddr and initialize based on passed arguments.  We
3332  * make copies of passed sockaddrs.  The ifmultiaddr will not be added to
3333  * the ifnet multicast address list here, so the caller must do that and
3334  * other setup work (such as notifying the device driver).  The reference
3335  * count is initialized to 1.
3336  */
3337 static struct ifmultiaddr *
3338 if_allocmulti(struct ifnet *ifp, struct sockaddr *sa, struct sockaddr *llsa,
3339     int mflags)
3340 {
3341         struct ifmultiaddr *ifma;
3342         struct sockaddr *dupsa;
3343
3344         ifma = malloc(sizeof *ifma, M_IFMADDR, mflags |
3345             M_ZERO);
3346         if (ifma == NULL)
3347                 return (NULL);
3348
3349         dupsa = malloc(sa->sa_len, M_IFMADDR, mflags);
3350         if (dupsa == NULL) {
3351                 free(ifma, M_IFMADDR);
3352                 return (NULL);
3353         }
3354         bcopy(sa, dupsa, sa->sa_len);
3355         ifma->ifma_addr = dupsa;
3356
3357         ifma->ifma_ifp = ifp;
3358         ifma->ifma_refcount = 1;
3359         ifma->ifma_protospec = NULL;
3360
3361         if (llsa == NULL) {
3362                 ifma->ifma_lladdr = NULL;
3363                 return (ifma);
3364         }
3365
3366         dupsa = malloc(llsa->sa_len, M_IFMADDR, mflags);
3367         if (dupsa == NULL) {
3368                 free(ifma->ifma_addr, M_IFMADDR);
3369                 free(ifma, M_IFMADDR);
3370                 return (NULL);
3371         }
3372         bcopy(llsa, dupsa, llsa->sa_len);
3373         ifma->ifma_lladdr = dupsa;
3374
3375         return (ifma);
3376 }
3377
3378 /*
3379  * if_freemulti: free ifmultiaddr structure and possibly attached related
3380  * addresses.  The caller is responsible for implementing reference
3381  * counting, notifying the driver, handling routing messages, and releasing
3382  * any dependent link layer state.
3383  */
3384 #ifdef MCAST_VERBOSE
3385 extern void kdb_backtrace(void);
3386 #endif
3387 static void
3388 if_freemulti_internal(struct ifmultiaddr *ifma)
3389 {
3390
3391         KASSERT(ifma->ifma_refcount == 0, ("if_freemulti: refcount %d",
3392             ifma->ifma_refcount));
3393
3394         if (ifma->ifma_lladdr != NULL)
3395                 free(ifma->ifma_lladdr, M_IFMADDR);
3396 #ifdef MCAST_VERBOSE
3397         kdb_backtrace();
3398         printf("%s freeing ifma: %p\n", __func__, ifma);
3399 #endif
3400         free(ifma->ifma_addr, M_IFMADDR);
3401         free(ifma, M_IFMADDR);
3402 }
3403
3404 static void
3405 if_destroymulti(epoch_context_t ctx)
3406 {
3407         struct ifmultiaddr *ifma;
3408
3409         ifma = __containerof(ctx, struct ifmultiaddr, ifma_epoch_ctx);
3410         if_freemulti_internal(ifma);
3411 }
3412
3413 void
3414 if_freemulti(struct ifmultiaddr *ifma)
3415 {
3416         KASSERT(ifma->ifma_refcount == 0, ("if_freemulti_epoch: refcount %d",
3417             ifma->ifma_refcount));
3418
3419         NET_EPOCH_CALL(if_destroymulti, &ifma->ifma_epoch_ctx);
3420 }
3421
3422 /*
3423  * Register an additional multicast address with a network interface.
3424  *
3425  * - If the address is already present, bump the reference count on the
3426  *   address and return.
3427  * - If the address is not link-layer, look up a link layer address.
3428  * - Allocate address structures for one or both addresses, and attach to the
3429  *   multicast address list on the interface.  If automatically adding a link
3430  *   layer address, the protocol address will own a reference to the link
3431  *   layer address, to be freed when it is freed.
3432  * - Notify the network device driver of an addition to the multicast address
3433  *   list.
3434  *
3435  * 'sa' points to caller-owned memory with the desired multicast address.
3436  *
3437  * 'retifma' will be used to return a pointer to the resulting multicast
3438  * address reference, if desired.
3439  */
3440 int
3441 if_addmulti(struct ifnet *ifp, struct sockaddr *sa,
3442     struct ifmultiaddr **retifma)
3443 {
3444         struct ifmultiaddr *ifma, *ll_ifma;
3445         struct sockaddr *llsa;
3446         struct sockaddr_dl sdl;
3447         int error;
3448
3449 #ifdef INET
3450         IN_MULTI_LIST_UNLOCK_ASSERT();
3451 #endif
3452 #ifdef INET6
3453         IN6_MULTI_LIST_UNLOCK_ASSERT();
3454 #endif
3455         /*
3456          * If the address is already present, return a new reference to it;
3457          * otherwise, allocate storage and set up a new address.
3458          */
3459         IF_ADDR_WLOCK(ifp);
3460         ifma = if_findmulti(ifp, sa);
3461         if (ifma != NULL) {
3462                 ifma->ifma_refcount++;
3463                 if (retifma != NULL)
3464                         *retifma = ifma;
3465                 IF_ADDR_WUNLOCK(ifp);
3466                 return (0);
3467         }
3468
3469         /*
3470          * The address isn't already present; resolve the protocol address
3471          * into a link layer address, and then look that up, bump its
3472          * refcount or allocate an ifma for that also.
3473          * Most link layer resolving functions returns address data which
3474          * fits inside default sockaddr_dl structure. However callback
3475          * can allocate another sockaddr structure, in that case we need to
3476          * free it later.
3477          */
3478         llsa = NULL;
3479         ll_ifma = NULL;
3480         if (ifp->if_resolvemulti != NULL) {
3481                 /* Provide called function with buffer size information */
3482                 sdl.sdl_len = sizeof(sdl);
3483                 llsa = (struct sockaddr *)&sdl;
3484                 error = ifp->if_resolvemulti(ifp, &llsa, sa);
3485                 if (error)
3486                         goto unlock_out;
3487         }
3488
3489         /*
3490          * Allocate the new address.  Don't hook it up yet, as we may also
3491          * need to allocate a link layer multicast address.
3492          */
3493         ifma = if_allocmulti(ifp, sa, llsa, M_NOWAIT);
3494         if (ifma == NULL) {
3495                 error = ENOMEM;
3496                 goto free_llsa_out;
3497         }
3498
3499         /*
3500          * If a link layer address is found, we'll need to see if it's
3501          * already present in the address list, or allocate is as well.
3502          * When this block finishes, the link layer address will be on the
3503          * list.
3504          */
3505         if (llsa != NULL) {
3506                 ll_ifma = if_findmulti(ifp, llsa);
3507                 if (ll_ifma == NULL) {
3508                         ll_ifma = if_allocmulti(ifp, llsa, NULL, M_NOWAIT);
3509                         if (ll_ifma == NULL) {
3510                                 --ifma->ifma_refcount;
3511                                 if_freemulti(ifma);
3512                                 error = ENOMEM;
3513                                 goto free_llsa_out;
3514                         }
3515                         ll_ifma->ifma_flags |= IFMA_F_ENQUEUED;
3516                         CK_STAILQ_INSERT_HEAD(&ifp->if_multiaddrs, ll_ifma,
3517                             ifma_link);
3518                 } else
3519                         ll_ifma->ifma_refcount++;
3520                 ifma->ifma_llifma = ll_ifma;
3521         }
3522
3523         /*
3524          * We now have a new multicast address, ifma, and possibly a new or
3525          * referenced link layer address.  Add the primary address to the
3526          * ifnet address list.
3527          */
3528         ifma->ifma_flags |= IFMA_F_ENQUEUED;
3529         CK_STAILQ_INSERT_HEAD(&ifp->if_multiaddrs, ifma, ifma_link);
3530
3531         if (retifma != NULL)
3532                 *retifma = ifma;
3533
3534         /*
3535          * Must generate the message while holding the lock so that 'ifma'
3536          * pointer is still valid.
3537          */
3538         rt_newmaddrmsg(RTM_NEWMADDR, ifma);
3539         IF_ADDR_WUNLOCK(ifp);
3540
3541         /*
3542          * We are certain we have added something, so call down to the
3543          * interface to let them know about it.
3544          */
3545         if (ifp->if_ioctl != NULL) {
3546                 if (THREAD_CAN_SLEEP())
3547                         (void )(*ifp->if_ioctl)(ifp, SIOCADDMULTI, 0);
3548                 else
3549                         taskqueue_enqueue(taskqueue_swi, &ifp->if_addmultitask);
3550         }
3551
3552         if ((llsa != NULL) && (llsa != (struct sockaddr *)&sdl))
3553                 link_free_sdl(llsa);
3554
3555         return (0);
3556
3557 free_llsa_out:
3558         if ((llsa != NULL) && (llsa != (struct sockaddr *)&sdl))
3559                 link_free_sdl(llsa);
3560
3561 unlock_out:
3562         IF_ADDR_WUNLOCK(ifp);
3563         return (error);
3564 }
3565
3566 static void
3567 if_siocaddmulti(void *arg, int pending)
3568 {
3569         struct ifnet *ifp;
3570
3571         ifp = arg;
3572 #ifdef DIAGNOSTIC
3573         if (pending > 1)
3574                 if_printf(ifp, "%d SIOCADDMULTI coalesced\n", pending);
3575 #endif
3576         CURVNET_SET(ifp->if_vnet);
3577         (void )(*ifp->if_ioctl)(ifp, SIOCADDMULTI, 0);
3578         CURVNET_RESTORE();
3579 }
3580
3581 /*
3582  * Delete a multicast group membership by network-layer group address.
3583  *
3584  * Returns ENOENT if the entry could not be found. If ifp no longer
3585  * exists, results are undefined. This entry point should only be used
3586  * from subsystems which do appropriate locking to hold ifp for the
3587  * duration of the call.
3588  * Network-layer protocol domains must use if_delmulti_ifma().
3589  */
3590 int
3591 if_delmulti(struct ifnet *ifp, struct sockaddr *sa)
3592 {
3593         struct ifmultiaddr *ifma;
3594         int lastref;
3595
3596         KASSERT(ifp, ("%s: NULL ifp", __func__));
3597
3598         IF_ADDR_WLOCK(ifp);
3599         lastref = 0;
3600         ifma = if_findmulti(ifp, sa);
3601         if (ifma != NULL)
3602                 lastref = if_delmulti_locked(ifp, ifma, 0);
3603         IF_ADDR_WUNLOCK(ifp);
3604
3605         if (ifma == NULL)
3606                 return (ENOENT);
3607
3608         if (lastref && ifp->if_ioctl != NULL) {
3609                 (void)(*ifp->if_ioctl)(ifp, SIOCDELMULTI, 0);
3610         }
3611
3612         return (0);
3613 }
3614
3615 /*
3616  * Delete all multicast group membership for an interface.
3617  * Should be used to quickly flush all multicast filters.
3618  */
3619 void
3620 if_delallmulti(struct ifnet *ifp)
3621 {
3622         struct ifmultiaddr *ifma;
3623         struct ifmultiaddr *next;
3624
3625         IF_ADDR_WLOCK(ifp);
3626         CK_STAILQ_FOREACH_SAFE(ifma, &ifp->if_multiaddrs, ifma_link, next)
3627                 if_delmulti_locked(ifp, ifma, 0);
3628         IF_ADDR_WUNLOCK(ifp);
3629 }
3630
3631 void
3632 if_delmulti_ifma(struct ifmultiaddr *ifma)
3633 {
3634         if_delmulti_ifma_flags(ifma, 0);
3635 }
3636
3637 /*
3638  * Delete a multicast group membership by group membership pointer.
3639  * Network-layer protocol domains must use this routine.
3640  *
3641  * It is safe to call this routine if the ifp disappeared.
3642  */
3643 void
3644 if_delmulti_ifma_flags(struct ifmultiaddr *ifma, int flags)
3645 {
3646         struct ifnet *ifp;
3647         int lastref;
3648         MCDPRINTF("%s freeing ifma: %p\n", __func__, ifma);
3649 #ifdef INET
3650         IN_MULTI_LIST_UNLOCK_ASSERT();
3651 #endif
3652         ifp = ifma->ifma_ifp;
3653 #ifdef DIAGNOSTIC
3654         if (ifp == NULL) {
3655                 printf("%s: ifma_ifp seems to be detached\n", __func__);
3656         } else {
3657                 struct epoch_tracker et;
3658                 struct ifnet *oifp;
3659
3660                 NET_EPOCH_ENTER(et);
3661                 CK_STAILQ_FOREACH(oifp, &V_ifnet, if_link)
3662                         if (ifp == oifp)
3663                                 break;
3664                 NET_EPOCH_EXIT(et);
3665                 if (ifp != oifp)
3666                         ifp = NULL;
3667         }
3668 #endif
3669         /*
3670          * If and only if the ifnet instance exists: Acquire the address lock.
3671          */
3672         if (ifp != NULL)
3673                 IF_ADDR_WLOCK(ifp);
3674
3675         lastref = if_delmulti_locked(ifp, ifma, flags);
3676
3677         if (ifp != NULL) {
3678                 /*
3679                  * If and only if the ifnet instance exists:
3680                  *  Release the address lock.
3681                  *  If the group was left: update the hardware hash filter.
3682                  */
3683                 IF_ADDR_WUNLOCK(ifp);
3684                 if (lastref && ifp->if_ioctl != NULL) {
3685                         (void)(*ifp->if_ioctl)(ifp, SIOCDELMULTI, 0);
3686                 }
3687         }
3688 }
3689
3690 /*
3691  * Perform deletion of network-layer and/or link-layer multicast address.
3692  *
3693  * Return 0 if the reference count was decremented.
3694  * Return 1 if the final reference was released, indicating that the
3695  * hardware hash filter should be reprogrammed.
3696  */
3697 static int
3698 if_delmulti_locked(struct ifnet *ifp, struct ifmultiaddr *ifma, int detaching)
3699 {
3700         struct ifmultiaddr *ll_ifma;
3701
3702         if (ifp != NULL && ifma->ifma_ifp != NULL) {
3703                 KASSERT(ifma->ifma_ifp == ifp,
3704                     ("%s: inconsistent ifp %p", __func__, ifp));
3705                 IF_ADDR_WLOCK_ASSERT(ifp);
3706         }
3707
3708         ifp = ifma->ifma_ifp;
3709         MCDPRINTF("%s freeing %p from %s \n", __func__, ifma, ifp ? ifp->if_xname : "");
3710
3711         /*
3712          * If the ifnet is detaching, null out references to ifnet,
3713          * so that upper protocol layers will notice, and not attempt
3714          * to obtain locks for an ifnet which no longer exists. The
3715          * routing socket announcement must happen before the ifnet
3716          * instance is detached from the system.
3717          */
3718         if (detaching) {
3719 #ifdef DIAGNOSTIC
3720                 printf("%s: detaching ifnet instance %p\n", __func__, ifp);
3721 #endif
3722                 /*
3723                  * ifp may already be nulled out if we are being reentered
3724                  * to delete the ll_ifma.
3725                  */
3726                 if (ifp != NULL) {
3727                         rt_newmaddrmsg(RTM_DELMADDR, ifma);
3728                         ifma->ifma_ifp = NULL;
3729                 }
3730         }
3731
3732         if (--ifma->ifma_refcount > 0)
3733                 return 0;
3734
3735         if (ifp != NULL && detaching == 0 && (ifma->ifma_flags & IFMA_F_ENQUEUED)) {
3736                 CK_STAILQ_REMOVE(&ifp->if_multiaddrs, ifma, ifmultiaddr, ifma_link);
3737                 ifma->ifma_flags &= ~IFMA_F_ENQUEUED;
3738         }
3739         /*
3740          * If this ifma is a network-layer ifma, a link-layer ifma may
3741          * have been associated with it. Release it first if so.
3742          */
3743         ll_ifma = ifma->ifma_llifma;
3744         if (ll_ifma != NULL) {
3745                 KASSERT(ifma->ifma_lladdr != NULL,
3746                     ("%s: llifma w/o lladdr", __func__));
3747                 if (detaching)
3748                         ll_ifma->ifma_ifp = NULL;       /* XXX */
3749                 if (--ll_ifma->ifma_refcount == 0) {
3750                         if (ifp != NULL) {
3751                                 if (ll_ifma->ifma_flags & IFMA_F_ENQUEUED) {
3752                                         CK_STAILQ_REMOVE(&ifp->if_multiaddrs, ll_ifma, ifmultiaddr,
3753                                                 ifma_link);
3754                                         ll_ifma->ifma_flags &= ~IFMA_F_ENQUEUED;
3755                                 }
3756                         }
3757                         if_freemulti(ll_ifma);
3758                 }
3759         }
3760 #ifdef INVARIANTS
3761         if (ifp) {
3762                 struct ifmultiaddr *ifmatmp;
3763
3764                 CK_STAILQ_FOREACH(ifmatmp, &ifp->if_multiaddrs, ifma_link)
3765                         MPASS(ifma != ifmatmp);
3766         }
3767 #endif
3768         if_freemulti(ifma);
3769         /*
3770          * The last reference to this instance of struct ifmultiaddr
3771          * was released; the hardware should be notified of this change.
3772          */
3773         return 1;
3774 }
3775
3776 /*
3777  * Set the link layer address on an interface.
3778  *
3779  * At this time we only support certain types of interfaces,
3780  * and we don't allow the length of the address to change.
3781  *
3782  * Set noinline to be dtrace-friendly
3783  */
3784 __noinline int
3785 if_setlladdr(struct ifnet *ifp, const u_char *lladdr, int len)
3786 {
3787         struct sockaddr_dl *sdl;
3788         struct ifaddr *ifa;
3789         struct ifreq ifr;
3790
3791         ifa = ifp->if_addr;
3792         if (ifa == NULL)
3793                 return (EINVAL);
3794
3795         sdl = (struct sockaddr_dl *)ifa->ifa_addr;
3796         if (sdl == NULL)
3797                 return (EINVAL);
3798
3799         if (len != sdl->sdl_alen)       /* don't allow length to change */
3800                 return (EINVAL);
3801
3802         switch (ifp->if_type) {
3803         case IFT_ETHER:
3804         case IFT_XETHER:
3805         case IFT_L2VLAN:
3806         case IFT_BRIDGE:
3807         case IFT_IEEE8023ADLAG:
3808                 bcopy(lladdr, LLADDR(sdl), len);
3809                 break;
3810         default:
3811                 return (ENODEV);
3812         }
3813
3814         /*
3815          * If the interface is already up, we need
3816          * to re-init it in order to reprogram its
3817          * address filter.
3818          */
3819         if ((ifp->if_flags & IFF_UP) != 0) {
3820                 if (ifp->if_ioctl) {
3821                         ifp->if_flags &= ~IFF_UP;
3822                         ifr.ifr_flags = ifp->if_flags & 0xffff;
3823                         ifr.ifr_flagshigh = ifp->if_flags >> 16;
3824                         (*ifp->if_ioctl)(ifp, SIOCSIFFLAGS, (caddr_t)&ifr);
3825                         ifp->if_flags |= IFF_UP;
3826                         ifr.ifr_flags = ifp->if_flags & 0xffff;
3827                         ifr.ifr_flagshigh = ifp->if_flags >> 16;
3828                         (*ifp->if_ioctl)(ifp, SIOCSIFFLAGS, (caddr_t)&ifr);
3829                 }
3830         }
3831         EVENTHANDLER_INVOKE(iflladdr_event, ifp);
3832
3833         return (0);
3834 }
3835
3836 /*
3837  * Compat function for handling basic encapsulation requests.
3838  * Not converted stacks (FDDI, IB, ..) supports traditional
3839  * output model: ARP (and other similar L2 protocols) are handled
3840  * inside output routine, arpresolve/nd6_resolve() returns MAC
3841  * address instead of full prepend.
3842  *
3843  * This function creates calculated header==MAC for IPv4/IPv6 and
3844  * returns EAFNOSUPPORT (which is then handled in ARP code) for other
3845  * address families.
3846  */
3847 static int
3848 if_requestencap_default(struct ifnet *ifp, struct if_encap_req *req)
3849 {
3850
3851         if (req->rtype != IFENCAP_LL)
3852                 return (EOPNOTSUPP);
3853
3854         if (req->bufsize < req->lladdr_len)
3855                 return (ENOMEM);
3856
3857         switch (req->family) {
3858         case AF_INET:
3859         case AF_INET6:
3860                 break;
3861         default:
3862                 return (EAFNOSUPPORT);
3863         }
3864
3865         /* Copy lladdr to storage as is */
3866         memmove(req->buf, req->lladdr, req->lladdr_len);
3867         req->bufsize = req->lladdr_len;
3868         req->lladdr_off = 0;
3869
3870         return (0);
3871 }
3872
3873 /*
3874  * Tunnel interfaces can nest, also they may cause infinite recursion
3875  * calls when misconfigured. We'll prevent this by detecting loops.
3876  * High nesting level may cause stack exhaustion. We'll prevent this
3877  * by introducing upper limit.
3878  *
3879  * Return 0, if tunnel nesting count is equal or less than limit.
3880  */
3881 int
3882 if_tunnel_check_nesting(struct ifnet *ifp, struct mbuf *m, uint32_t cookie,
3883     int limit)
3884 {
3885         struct m_tag *mtag;
3886         int count;
3887
3888         count = 1;
3889         mtag = NULL;
3890         while ((mtag = m_tag_locate(m, cookie, 0, mtag)) != NULL) {
3891                 if (*(struct ifnet **)(mtag + 1) == ifp) {
3892                         log(LOG_NOTICE, "%s: loop detected\n", if_name(ifp));
3893                         return (EIO);
3894                 }
3895                 count++;
3896         }
3897         if (count > limit) {
3898                 log(LOG_NOTICE,
3899                     "%s: if_output recursively called too many times(%d)\n",
3900                     if_name(ifp), count);
3901                 return (EIO);
3902         }
3903         mtag = m_tag_alloc(cookie, 0, sizeof(struct ifnet *), M_NOWAIT);
3904         if (mtag == NULL)
3905                 return (ENOMEM);
3906         *(struct ifnet **)(mtag + 1) = ifp;
3907         m_tag_prepend(m, mtag);
3908         return (0);
3909 }
3910
3911 /*
3912  * Get the link layer address that was read from the hardware at attach.
3913  *
3914  * This is only set by Ethernet NICs (IFT_ETHER), but laggX interfaces re-type
3915  * their component interfaces as IFT_IEEE8023ADLAG.
3916  */
3917 int
3918 if_gethwaddr(struct ifnet *ifp, struct ifreq *ifr)
3919 {
3920
3921         if (ifp->if_hw_addr == NULL)
3922                 return (ENODEV);
3923
3924         switch (ifp->if_type) {
3925         case IFT_ETHER:
3926         case IFT_IEEE8023ADLAG:
3927                 bcopy(ifp->if_hw_addr, ifr->ifr_addr.sa_data, ifp->if_addrlen);
3928                 return (0);
3929         default:
3930                 return (ENODEV);
3931         }
3932 }
3933
3934 /*
3935  * The name argument must be a pointer to storage which will last as
3936  * long as the interface does.  For physical devices, the result of
3937  * device_get_name(dev) is a good choice and for pseudo-devices a
3938  * static string works well.
3939  */
3940 void
3941 if_initname(struct ifnet *ifp, const char *name, int unit)
3942 {
3943         ifp->if_dname = name;
3944         ifp->if_dunit = unit;
3945         if (unit != IF_DUNIT_NONE)
3946                 snprintf(ifp->if_xname, IFNAMSIZ, "%s%d", name, unit);
3947         else
3948                 strlcpy(ifp->if_xname, name, IFNAMSIZ);
3949 }
3950
3951 int
3952 if_printf(struct ifnet *ifp, const char *fmt, ...)
3953 {
3954         char if_fmt[256];
3955         va_list ap;
3956
3957         snprintf(if_fmt, sizeof(if_fmt), "%s: %s", ifp->if_xname, fmt);
3958         va_start(ap, fmt);
3959         vlog(LOG_INFO, if_fmt, ap);
3960         va_end(ap);
3961         return (0);
3962 }
3963
3964 void
3965 if_start(struct ifnet *ifp)
3966 {
3967
3968         (*(ifp)->if_start)(ifp);
3969 }
3970
3971 /*
3972  * Backwards compatibility interface for drivers 
3973  * that have not implemented it
3974  */
3975 static int
3976 if_transmit(struct ifnet *ifp, struct mbuf *m)
3977 {
3978         int error;
3979
3980         IFQ_HANDOFF(ifp, m, error);
3981         return (error);
3982 }
3983
3984 static void
3985 if_input_default(struct ifnet *ifp __unused, struct mbuf *m)
3986 {
3987
3988         m_freem(m);
3989 }
3990
3991 int
3992 if_handoff(struct ifqueue *ifq, struct mbuf *m, struct ifnet *ifp, int adjust)
3993 {
3994         int active = 0;
3995
3996         IF_LOCK(ifq);
3997         if (_IF_QFULL(ifq)) {
3998                 IF_UNLOCK(ifq);
3999                 if_inc_counter(ifp, IFCOUNTER_OQDROPS, 1);
4000                 m_freem(m);
4001                 return (0);
4002         }
4003         if (ifp != NULL) {
4004                 if_inc_counter(ifp, IFCOUNTER_OBYTES, m->m_pkthdr.len + adjust);
4005                 if (m->m_flags & (M_BCAST|M_MCAST))
4006                         if_inc_counter(ifp, IFCOUNTER_OMCASTS, 1);
4007                 active = ifp->if_drv_flags & IFF_DRV_OACTIVE;
4008         }
4009         _IF_ENQUEUE(ifq, m);
4010         IF_UNLOCK(ifq);
4011         if (ifp != NULL && !active)
4012                 (*(ifp)->if_start)(ifp);
4013         return (1);
4014 }
4015
4016 void
4017 if_register_com_alloc(u_char type,
4018     if_com_alloc_t *a, if_com_free_t *f)
4019 {
4020
4021         KASSERT(if_com_alloc[type] == NULL,
4022             ("if_register_com_alloc: %d already registered", type));
4023         KASSERT(if_com_free[type] == NULL,
4024             ("if_register_com_alloc: %d free already registered", type));
4025
4026         if_com_alloc[type] = a;
4027         if_com_free[type] = f;
4028 }
4029
4030 void
4031 if_deregister_com_alloc(u_char type)
4032 {
4033
4034         KASSERT(if_com_alloc[type] != NULL,
4035             ("if_deregister_com_alloc: %d not registered", type));
4036         KASSERT(if_com_free[type] != NULL,
4037             ("if_deregister_com_alloc: %d free not registered", type));
4038         if_com_alloc[type] = NULL;
4039         if_com_free[type] = NULL;
4040 }
4041
4042 /* API for driver access to network stack owned ifnet.*/
4043 uint64_t
4044 if_setbaudrate(struct ifnet *ifp, uint64_t baudrate)
4045 {
4046         uint64_t oldbrate;
4047
4048         oldbrate = ifp->if_baudrate;
4049         ifp->if_baudrate = baudrate;
4050         return (oldbrate);
4051 }
4052
4053 uint64_t
4054 if_getbaudrate(if_t ifp)
4055 {
4056
4057         return (((struct ifnet *)ifp)->if_baudrate);
4058 }
4059
4060 int
4061 if_setcapabilities(if_t ifp, int capabilities)
4062 {
4063         ((struct ifnet *)ifp)->if_capabilities = capabilities;
4064         return (0);
4065 }
4066
4067 int
4068 if_setcapabilitiesbit(if_t ifp, int setbit, int clearbit)
4069 {
4070         ((struct ifnet *)ifp)->if_capabilities |= setbit;
4071         ((struct ifnet *)ifp)->if_capabilities &= ~clearbit;
4072
4073         return (0);
4074 }
4075
4076 int
4077 if_getcapabilities(if_t ifp)
4078 {
4079         return ((struct ifnet *)ifp)->if_capabilities;
4080 }
4081
4082 int 
4083 if_setcapenable(if_t ifp, int capabilities)
4084 {
4085         ((struct ifnet *)ifp)->if_capenable = capabilities;
4086         return (0);
4087 }
4088
4089 int 
4090 if_setcapenablebit(if_t ifp, int setcap, int clearcap)
4091 {
4092         if(setcap) 
4093                 ((struct ifnet *)ifp)->if_capenable |= setcap;
4094         if(clearcap)
4095                 ((struct ifnet *)ifp)->if_capenable &= ~clearcap;
4096
4097         return (0);
4098 }
4099
4100 const char *
4101 if_getdname(if_t ifp)
4102 {
4103         return ((struct ifnet *)ifp)->if_dname;
4104 }
4105
4106 int 
4107 if_togglecapenable(if_t ifp, int togglecap)
4108 {
4109         ((struct ifnet *)ifp)->if_capenable ^= togglecap;
4110         return (0);
4111 }
4112
4113 int
4114 if_getcapenable(if_t ifp)
4115 {
4116         return ((struct ifnet *)ifp)->if_capenable;
4117 }
4118
4119 /*
4120  * This is largely undesirable because it ties ifnet to a device, but does
4121  * provide flexiblity for an embedded product vendor. Should be used with
4122  * the understanding that it violates the interface boundaries, and should be
4123  * a last resort only.
4124  */
4125 int
4126 if_setdev(if_t ifp, void *dev)
4127 {
4128         return (0);
4129 }
4130
4131 int
4132 if_setdrvflagbits(if_t ifp, int set_flags, int clear_flags)
4133 {
4134         ((struct ifnet *)ifp)->if_drv_flags |= set_flags;
4135         ((struct ifnet *)ifp)->if_drv_flags &= ~clear_flags;
4136
4137         return (0);
4138 }
4139
4140 int
4141 if_getdrvflags(if_t ifp)
4142 {
4143         return ((struct ifnet *)ifp)->if_drv_flags;
4144 }
4145
4146 int
4147 if_setdrvflags(if_t ifp, int flags)
4148 {
4149         ((struct ifnet *)ifp)->if_drv_flags = flags;
4150         return (0);
4151 }
4152
4153 int
4154 if_setflags(if_t ifp, int flags)
4155 {
4156
4157         ifp->if_flags = flags;
4158         return (0);
4159 }
4160
4161 int
4162 if_setflagbits(if_t ifp, int set, int clear)
4163 {
4164         ((struct ifnet *)ifp)->if_flags |= set;
4165         ((struct ifnet *)ifp)->if_flags &= ~clear;
4166
4167         return (0);
4168 }
4169
4170 int
4171 if_getflags(if_t ifp)
4172 {
4173         return ((struct ifnet *)ifp)->if_flags;
4174 }
4175
4176 int
4177 if_clearhwassist(if_t ifp)
4178 {
4179         ((struct ifnet *)ifp)->if_hwassist = 0;
4180         return (0);
4181 }
4182
4183 int
4184 if_sethwassistbits(if_t ifp, int toset, int toclear)
4185 {
4186         ((struct ifnet *)ifp)->if_hwassist |= toset;
4187         ((struct ifnet *)ifp)->if_hwassist &= ~toclear;
4188
4189         return (0);
4190 }
4191
4192 int
4193 if_sethwassist(if_t ifp, int hwassist_bit)
4194 {
4195         ((struct ifnet *)ifp)->if_hwassist = hwassist_bit;
4196         return (0);
4197 }
4198
4199 int
4200 if_gethwassist(if_t ifp)
4201 {
4202         return ((struct ifnet *)ifp)->if_hwassist;
4203 }
4204
4205 int
4206 if_setmtu(if_t ifp, int mtu)
4207 {
4208         ((struct ifnet *)ifp)->if_mtu = mtu;
4209         return (0);
4210 }
4211
4212 int
4213 if_getmtu(if_t ifp)
4214 {
4215         return ((struct ifnet *)ifp)->if_mtu;
4216 }
4217
4218 int
4219 if_getmtu_family(if_t ifp, int family)
4220 {
4221         struct domain *dp;
4222
4223         for (dp = domains; dp; dp = dp->dom_next) {
4224                 if (dp->dom_family == family && dp->dom_ifmtu != NULL)
4225                         return (dp->dom_ifmtu((struct ifnet *)ifp));
4226         }
4227
4228         return (((struct ifnet *)ifp)->if_mtu);
4229 }
4230
4231 /*
4232  * Methods for drivers to access interface unicast and multicast
4233  * link level addresses.  Driver shall not know 'struct ifaddr' neither
4234  * 'struct ifmultiaddr'.
4235  */
4236 u_int
4237 if_lladdr_count(if_t ifp)
4238 {
4239         struct epoch_tracker et;
4240         struct ifaddr *ifa;
4241         u_int count;
4242
4243         count = 0;
4244         NET_EPOCH_ENTER(et);
4245         CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link)
4246                 if (ifa->ifa_addr->sa_family == AF_LINK)
4247                         count++;
4248         NET_EPOCH_EXIT(et);
4249
4250         return (count);
4251 }
4252
4253 u_int
4254 if_foreach_lladdr(if_t ifp, iflladdr_cb_t cb, void *cb_arg)
4255 {
4256         struct epoch_tracker et;
4257         struct ifaddr *ifa;
4258         u_int count;
4259
4260         MPASS(cb);
4261
4262         count = 0;
4263         NET_EPOCH_ENTER(et);
4264         CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
4265                 if (ifa->ifa_addr->sa_family != AF_LINK)
4266                         continue;
4267                 count += (*cb)(cb_arg, (struct sockaddr_dl *)ifa->ifa_addr,
4268                     count);
4269         }
4270         NET_EPOCH_EXIT(et);
4271
4272         return (count);
4273 }
4274
4275 u_int
4276 if_llmaddr_count(if_t ifp)
4277 {
4278         struct epoch_tracker et;
4279         struct ifmultiaddr *ifma;
4280         int count;
4281
4282         count = 0;
4283         NET_EPOCH_ENTER(et);
4284         CK_STAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link)
4285                 if (ifma->ifma_addr->sa_family == AF_LINK)
4286                         count++;
4287         NET_EPOCH_EXIT(et);
4288
4289         return (count);
4290 }
4291
4292 u_int
4293 if_foreach_llmaddr(if_t ifp, iflladdr_cb_t cb, void *cb_arg)
4294 {
4295         struct epoch_tracker et;
4296         struct ifmultiaddr *ifma;
4297         u_int count;
4298
4299         MPASS(cb);
4300
4301         count = 0;
4302         NET_EPOCH_ENTER(et);
4303         CK_STAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
4304                 if (ifma->ifma_addr->sa_family != AF_LINK)
4305                         continue;
4306                 count += (*cb)(cb_arg, (struct sockaddr_dl *)ifma->ifma_addr,
4307                     count);
4308         }
4309         NET_EPOCH_EXIT(et);
4310
4311         return (count);
4312 }
4313
4314 int
4315 if_setsoftc(if_t ifp, void *softc)
4316 {
4317         ((struct ifnet *)ifp)->if_softc = softc;
4318         return (0);
4319 }
4320
4321 void *
4322 if_getsoftc(if_t ifp)
4323 {
4324         return ((struct ifnet *)ifp)->if_softc;
4325 }
4326
4327 void 
4328 if_setrcvif(struct mbuf *m, if_t ifp)
4329 {
4330
4331         MPASS((m->m_pkthdr.csum_flags & CSUM_SND_TAG) == 0);
4332         m->m_pkthdr.rcvif = (struct ifnet *)ifp;
4333 }
4334
4335 void 
4336 if_setvtag(struct mbuf *m, uint16_t tag)
4337 {
4338         m->m_pkthdr.ether_vtag = tag;   
4339 }
4340
4341 uint16_t
4342 if_getvtag(struct mbuf *m)
4343 {
4344
4345         return (m->m_pkthdr.ether_vtag);
4346 }
4347
4348 int
4349 if_sendq_empty(if_t ifp)
4350 {
4351         return IFQ_DRV_IS_EMPTY(&((struct ifnet *)ifp)->if_snd);
4352 }
4353
4354 struct ifaddr *
4355 if_getifaddr(if_t ifp)
4356 {
4357         return ((struct ifnet *)ifp)->if_addr;
4358 }
4359
4360 int
4361 if_getamcount(if_t ifp)
4362 {
4363         return ((struct ifnet *)ifp)->if_amcount;
4364 }
4365
4366 int
4367 if_setsendqready(if_t ifp)
4368 {
4369         IFQ_SET_READY(&((struct ifnet *)ifp)->if_snd);
4370         return (0);
4371 }
4372
4373 int
4374 if_setsendqlen(if_t ifp, int tx_desc_count)
4375 {
4376         IFQ_SET_MAXLEN(&((struct ifnet *)ifp)->if_snd, tx_desc_count);
4377         ((struct ifnet *)ifp)->if_snd.ifq_drv_maxlen = tx_desc_count;
4378
4379         return (0);
4380 }
4381
4382 int
4383 if_vlantrunkinuse(if_t ifp)
4384 {
4385         return ((struct ifnet *)ifp)->if_vlantrunk != NULL?1:0;
4386 }
4387
4388 int
4389 if_input(if_t ifp, struct mbuf* sendmp)
4390 {
4391         (*((struct ifnet *)ifp)->if_input)((struct ifnet *)ifp, sendmp);
4392         return (0);
4393
4394 }
4395
4396 struct mbuf *
4397 if_dequeue(if_t ifp)
4398 {
4399         struct mbuf *m;
4400         IFQ_DRV_DEQUEUE(&((struct ifnet *)ifp)->if_snd, m);
4401
4402         return (m);
4403 }
4404
4405 int
4406 if_sendq_prepend(if_t ifp, struct mbuf *m)
4407 {
4408         IFQ_DRV_PREPEND(&((struct ifnet *)ifp)->if_snd, m);
4409         return (0);
4410 }
4411
4412 int
4413 if_setifheaderlen(if_t ifp, int len)
4414 {
4415         ((struct ifnet *)ifp)->if_hdrlen = len;
4416         return (0);
4417 }
4418
4419 caddr_t
4420 if_getlladdr(if_t ifp)
4421 {
4422         return (IF_LLADDR((struct ifnet *)ifp));
4423 }
4424
4425 void *
4426 if_gethandle(u_char type)
4427 {
4428         return (if_alloc(type));
4429 }
4430
4431 void
4432 if_bpfmtap(if_t ifh, struct mbuf *m)
4433 {
4434         struct ifnet *ifp = (struct ifnet *)ifh;
4435
4436         BPF_MTAP(ifp, m);
4437 }
4438
4439 void
4440 if_etherbpfmtap(if_t ifh, struct mbuf *m)
4441 {
4442         struct ifnet *ifp = (struct ifnet *)ifh;
4443
4444         ETHER_BPF_MTAP(ifp, m);
4445 }
4446
4447 void
4448 if_vlancap(if_t ifh)
4449 {
4450         struct ifnet *ifp = (struct ifnet *)ifh;
4451         VLAN_CAPABILITIES(ifp);
4452 }
4453
4454 int
4455 if_sethwtsomax(if_t ifp, u_int if_hw_tsomax)
4456 {
4457
4458         ((struct ifnet *)ifp)->if_hw_tsomax = if_hw_tsomax;
4459         return (0);
4460 }
4461
4462 int
4463 if_sethwtsomaxsegcount(if_t ifp, u_int if_hw_tsomaxsegcount)
4464 {
4465
4466         ((struct ifnet *)ifp)->if_hw_tsomaxsegcount = if_hw_tsomaxsegcount;
4467         return (0);
4468 }
4469
4470 int
4471 if_sethwtsomaxsegsize(if_t ifp, u_int if_hw_tsomaxsegsize)
4472 {
4473
4474         ((struct ifnet *)ifp)->if_hw_tsomaxsegsize = if_hw_tsomaxsegsize;
4475         return (0);
4476 }
4477
4478 u_int
4479 if_gethwtsomax(if_t ifp)
4480 {
4481
4482         return (((struct ifnet *)ifp)->if_hw_tsomax);
4483 }
4484
4485 u_int
4486 if_gethwtsomaxsegcount(if_t ifp)
4487 {
4488
4489         return (((struct ifnet *)ifp)->if_hw_tsomaxsegcount);
4490 }
4491
4492 u_int
4493 if_gethwtsomaxsegsize(if_t ifp)
4494 {
4495
4496         return (((struct ifnet *)ifp)->if_hw_tsomaxsegsize);
4497 }
4498
4499 void
4500 if_setinitfn(if_t ifp, void (*init_fn)(void *))
4501 {
4502         ((struct ifnet *)ifp)->if_init = init_fn;
4503 }
4504
4505 void
4506 if_setioctlfn(if_t ifp, int (*ioctl_fn)(if_t, u_long, caddr_t))
4507 {
4508         ((struct ifnet *)ifp)->if_ioctl = (void *)ioctl_fn;
4509 }
4510
4511 void
4512 if_setstartfn(if_t ifp, void (*start_fn)(if_t))
4513 {
4514         ((struct ifnet *)ifp)->if_start = (void *)start_fn;
4515 }
4516
4517 void
4518 if_settransmitfn(if_t ifp, if_transmit_fn_t start_fn)
4519 {
4520         ((struct ifnet *)ifp)->if_transmit = start_fn;
4521 }
4522
4523 void if_setqflushfn(if_t ifp, if_qflush_fn_t flush_fn)
4524 {
4525         ((struct ifnet *)ifp)->if_qflush = flush_fn;
4526
4527 }
4528
4529 void
4530 if_setgetcounterfn(if_t ifp, if_get_counter_t fn)
4531 {
4532
4533         ifp->if_get_counter = fn;
4534 }
4535
4536 /* Revisit these - These are inline functions originally. */
4537 int
4538 drbr_inuse_drv(if_t ifh, struct buf_ring *br)
4539 {
4540         return drbr_inuse(ifh, br);
4541 }
4542
4543 struct mbuf*
4544 drbr_dequeue_drv(if_t ifh, struct buf_ring *br)
4545 {
4546         return drbr_dequeue(ifh, br);
4547 }
4548
4549 int
4550 drbr_needs_enqueue_drv(if_t ifh, struct buf_ring *br)
4551 {
4552         return drbr_needs_enqueue(ifh, br);
4553 }
4554
4555 int
4556 drbr_enqueue_drv(if_t ifh, struct buf_ring *br, struct mbuf *m)
4557 {
4558         return drbr_enqueue(ifh, br, m);
4559
4560 }