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