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