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