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