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