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