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