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