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