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