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