1/*	$NetBSD: if.c,v 1.260 2012/02/03 03:35:30 christos Exp $	*/
2
3/*-
4 * Copyright (c) 1999, 2000, 2001, 2008 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by William Studenmund and Jason R. Thorpe.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 *    notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 *    notice, this list of conditions and the following disclaimer in the
17 *    documentation and/or other materials provided with the distribution.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
30 */
31
32/*
33 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
34 * All rights reserved.
35 *
36 * Redistribution and use in source and binary forms, with or without
37 * modification, are permitted provided that the following conditions
38 * are met:
39 * 1. Redistributions of source code must retain the above copyright
40 *    notice, this list of conditions and the following disclaimer.
41 * 2. Redistributions in binary form must reproduce the above copyright
42 *    notice, this list of conditions and the following disclaimer in the
43 *    documentation and/or other materials provided with the distribution.
44 * 3. Neither the name of the project nor the names of its contributors
45 *    may be used to endorse or promote products derived from this software
46 *    without specific prior written permission.
47 *
48 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
49 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
50 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
51 * ARE DISCLAIMED.  IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
52 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
53 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
54 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
55 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
56 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
57 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
58 * SUCH DAMAGE.
59 */
60
61/*
62 * Copyright (c) 1980, 1986, 1993
63 *	The Regents of the University of California.  All rights reserved.
64 *
65 * Redistribution and use in source and binary forms, with or without
66 * modification, are permitted provided that the following conditions
67 * are met:
68 * 1. Redistributions of source code must retain the above copyright
69 *    notice, this list of conditions and the following disclaimer.
70 * 2. Redistributions in binary form must reproduce the above copyright
71 *    notice, this list of conditions and the following disclaimer in the
72 *    documentation and/or other materials provided with the distribution.
73 * 3. Neither the name of the University nor the names of its contributors
74 *    may be used to endorse or promote products derived from this software
75 *    without specific prior written permission.
76 *
77 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
78 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
79 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
80 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
81 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
82 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
83 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
84 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
85 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
86 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
87 * SUCH DAMAGE.
88 *
89 *	@(#)if.c	8.5 (Berkeley) 1/9/95
90 */
91
92#include <sys/cdefs.h>
93__KERNEL_RCSID(0, "$NetBSD: if.c,v 1.260 2012/02/03 03:35:30 christos Exp $");
94
95#include "opt_inet.h"
96
97#include "opt_atalk.h"
98#include "opt_natm.h"
99#include "opt_pfil_hooks.h"
100
101#include <sys/param.h>
102#include <sys/mbuf.h>
103#include <sys/systm.h>
104#include <sys/callout.h>
105#include <sys/proc.h>
106#include <sys/socket.h>
107#include <sys/socketvar.h>
108#include <sys/domain.h>
109#include <sys/protosw.h>
110#include <sys/kernel.h>
111#include <sys/ioctl.h>
112#include <sys/sysctl.h>
113#include <sys/syslog.h>
114#include <sys/kauth.h>
115#include <sys/kmem.h>
116
117#include <net/if.h>
118#include <net/if_dl.h>
119#include <net/if_ether.h>
120#include <net/if_media.h>
121#include <net80211/ieee80211.h>
122#include <net80211/ieee80211_ioctl.h>
123#include <net/if_types.h>
124#include <net/radix.h>
125#include <net/route.h>
126#include <net/netisr.h>
127#ifdef NETATALK
128#include <netatalk/at_extern.h>
129#include <netatalk/at.h>
130#endif
131#include <net/pfil.h>
132
133#ifdef INET6
134#include <netinet/in.h>
135#include <netinet6/in6_var.h>
136#include <netinet6/nd6.h>
137#endif
138
139#include "carp.h"
140#if NCARP > 0
141#include <netinet/ip_carp.h>
142#endif
143
144#include <compat/sys/sockio.h>
145#include <compat/sys/socket.h>
146
147MALLOC_DEFINE(M_IFADDR, "ifaddr", "interface address");
148MALLOC_DEFINE(M_IFMADDR, "ether_multi", "link-level multicast address");
149
150int	ifqmaxlen = IFQ_MAXLEN;
151callout_t if_slowtimo_ch;
152
153int netisr;			/* scheduling bits for network */
154
155static int	if_rt_walktree(struct rtentry *, void *);
156
157static struct if_clone *if_clone_lookup(const char *, int *);
158static int	if_clone_list(struct if_clonereq *);
159
160static LIST_HEAD(, if_clone) if_cloners = LIST_HEAD_INITIALIZER(if_cloners);
161static int if_cloners_count;
162
163static uint64_t index_gen;
164static kmutex_t index_gen_mtx;
165
166#ifdef PFIL_HOOKS
167struct pfil_head if_pfil;	/* packet filtering hook for interfaces */
168#endif
169
170static kauth_listener_t if_listener;
171
172static int ifioctl_attach(struct ifnet *);
173static void ifioctl_detach(struct ifnet *);
174static void ifnet_lock_enter(struct ifnet_lock *);
175static void ifnet_lock_exit(struct ifnet_lock *);
176static void if_detach_queues(struct ifnet *, struct ifqueue *);
177static void sysctl_sndq_setup(struct sysctllog **, const char *,
178    struct ifaltq *);
179
180#if defined(INET) || defined(INET6)
181static void sysctl_net_ifq_setup(struct sysctllog **, int, const char *,
182				 int, const char *, int, struct ifqueue *);
183#endif
184
185static int
186if_listener_cb(kauth_cred_t cred, kauth_action_t action, void *cookie,
187    void *arg0, void *arg1, void *arg2, void *arg3)
188{
189	int result;
190	enum kauth_network_req req;
191
192	result = KAUTH_RESULT_DEFER;
193	req = (enum kauth_network_req)arg1;
194
195	if (action != KAUTH_NETWORK_INTERFACE)
196		return result;
197
198	if ((req == KAUTH_REQ_NETWORK_INTERFACE_GET) ||
199	    (req == KAUTH_REQ_NETWORK_INTERFACE_SET))
200		result = KAUTH_RESULT_ALLOW;
201
202	return result;
203}
204
205/*
206 * Network interface utility routines.
207 *
208 * Routines with ifa_ifwith* names take sockaddr *'s as
209 * parameters.
210 */
211void
212ifinit(void)
213{
214#ifdef INET
215	{extern struct ifqueue ipintrq;
216	sysctl_net_ifq_setup(NULL, PF_INET, "inet", IPPROTO_IP, "ip",
217			     IPCTL_IFQ, &ipintrq);}
218#endif /* INET */
219#ifdef INET6
220	{extern struct ifqueue ip6intrq;
221	sysctl_net_ifq_setup(NULL, PF_INET6, "inet6", IPPROTO_IPV6, "ip6",
222			     IPV6CTL_IFQ, &ip6intrq);}
223#endif /* INET6 */
224
225	callout_init(&if_slowtimo_ch, 0);
226	if_slowtimo(NULL);
227
228	if_listener = kauth_listen_scope(KAUTH_SCOPE_NETWORK,
229	    if_listener_cb, NULL);
230}
231
232/*
233 * XXX Initialization before configure().
234 * XXX hack to get pfil_add_hook working in autoconf.
235 */
236void
237ifinit1(void)
238{
239
240	mutex_init(&index_gen_mtx, MUTEX_DEFAULT, IPL_NONE);
241
242#ifdef PFIL_HOOKS
243	if_pfil.ph_type = PFIL_TYPE_IFNET;
244	if_pfil.ph_ifnet = NULL;
245	if (pfil_head_register(&if_pfil) != 0)
246		printf("WARNING: unable to register pfil hook\n");
247#endif
248}
249
250struct ifnet *
251if_alloc(u_char type)
252{
253	return malloc(sizeof(struct ifnet), M_DEVBUF, M_WAITOK|M_ZERO);
254}
255
256void
257if_free(struct ifnet *ifp)
258{
259	free(ifp, M_DEVBUF);
260}
261
262void
263if_initname(struct ifnet *ifp, const char *name, int unit)
264{
265	(void)snprintf(ifp->if_xname, sizeof(ifp->if_xname),
266	    "%s%d", name, unit);
267}
268
269/*
270 * Null routines used while an interface is going away.  These routines
271 * just return an error.
272 */
273
274int
275if_nulloutput(struct ifnet *ifp, struct mbuf *m,
276    const struct sockaddr *so, struct rtentry *rt)
277{
278
279	return ENXIO;
280}
281
282void
283if_nullinput(struct ifnet *ifp, struct mbuf *m)
284{
285
286	/* Nothing. */
287}
288
289void
290if_nullstart(struct ifnet *ifp)
291{
292
293	/* Nothing. */
294}
295
296int
297if_nullioctl(struct ifnet *ifp, u_long cmd, void *data)
298{
299
300	/* Wake ifioctl_detach(), who may wait for all threads to
301	 * quit the critical section.
302	 */
303	cv_signal(&ifp->if_ioctl_lock->il_emptied);
304	return ENXIO;
305}
306
307int
308if_nullinit(struct ifnet *ifp)
309{
310
311	return ENXIO;
312}
313
314void
315if_nullstop(struct ifnet *ifp, int disable)
316{
317
318	/* Nothing. */
319}
320
321void
322if_nullwatchdog(struct ifnet *ifp)
323{
324
325	/* Nothing. */
326}
327
328void
329if_nulldrain(struct ifnet *ifp)
330{
331
332	/* Nothing. */
333}
334
335static u_int if_index = 1;
336struct ifnet_head ifnet;
337size_t if_indexlim = 0;
338struct ifaddr **ifnet_addrs = NULL;
339struct ifnet **ifindex2ifnet = NULL;
340struct ifnet *lo0ifp;
341
342void
343if_set_sadl(struct ifnet *ifp, const void *lla, u_char addrlen, bool factory)
344{
345	struct ifaddr *ifa;
346	struct sockaddr_dl *sdl;
347
348	ifp->if_addrlen = addrlen;
349	if_alloc_sadl(ifp);
350	ifa = ifp->if_dl;
351	sdl = satosdl(ifa->ifa_addr);
352
353	(void)sockaddr_dl_setaddr(sdl, sdl->sdl_len, lla, ifp->if_addrlen);
354	if (factory) {
355		ifp->if_hwdl = ifp->if_dl;
356		IFAREF(ifp->if_hwdl);
357	}
358	/* TBD routing socket */
359}
360
361struct ifaddr *
362if_dl_create(const struct ifnet *ifp, const struct sockaddr_dl **sdlp)
363{
364	unsigned socksize, ifasize;
365	int addrlen, namelen;
366	struct sockaddr_dl *mask, *sdl;
367	struct ifaddr *ifa;
368
369	namelen = strlen(ifp->if_xname);
370	addrlen = ifp->if_addrlen;
371	socksize = roundup(sockaddr_dl_measure(namelen, addrlen), sizeof(long));
372	ifasize = sizeof(*ifa) + 2 * socksize;
373	ifa = (struct ifaddr *)malloc(ifasize, M_IFADDR, M_WAITOK|M_ZERO);
374
375	sdl = (struct sockaddr_dl *)(ifa + 1);
376	mask = (struct sockaddr_dl *)(socksize + (char *)sdl);
377
378	sockaddr_dl_init(sdl, socksize, ifp->if_index, ifp->if_type,
379	    ifp->if_xname, namelen, NULL, addrlen);
380	mask->sdl_len = sockaddr_dl_measure(namelen, 0);
381	memset(&mask->sdl_data[0], 0xff, namelen);
382	ifa->ifa_rtrequest = link_rtrequest;
383	ifa->ifa_addr = (struct sockaddr *)sdl;
384	ifa->ifa_netmask = (struct sockaddr *)mask;
385
386	*sdlp = sdl;
387
388	return ifa;
389}
390
391static void
392if_sadl_setrefs(struct ifnet *ifp, struct ifaddr *ifa)
393{
394	const struct sockaddr_dl *sdl;
395	ifnet_addrs[ifp->if_index] = ifa;
396	IFAREF(ifa);
397	ifp->if_dl = ifa;
398	IFAREF(ifa);
399	sdl = satosdl(ifa->ifa_addr);
400	ifp->if_sadl = sdl;
401}
402
403/*
404 * Allocate the link level name for the specified interface.  This
405 * is an attachment helper.  It must be called after ifp->if_addrlen
406 * is initialized, which may not be the case when if_attach() is
407 * called.
408 */
409void
410if_alloc_sadl(struct ifnet *ifp)
411{
412	struct ifaddr *ifa;
413	const struct sockaddr_dl *sdl;
414
415	/*
416	 * If the interface already has a link name, release it
417	 * now.  This is useful for interfaces that can change
418	 * link types, and thus switch link names often.
419	 */
420	if (ifp->if_sadl != NULL)
421		if_free_sadl(ifp);
422
423	ifa = if_dl_create(ifp, &sdl);
424
425	ifa_insert(ifp, ifa);
426	if_sadl_setrefs(ifp, ifa);
427}
428
429static void
430if_deactivate_sadl(struct ifnet *ifp)
431{
432	struct ifaddr *ifa;
433
434	KASSERT(ifp->if_dl != NULL);
435
436	ifa = ifp->if_dl;
437
438	ifp->if_sadl = NULL;
439
440	ifnet_addrs[ifp->if_index] = NULL;
441	IFAFREE(ifa);
442	ifp->if_dl = NULL;
443	IFAFREE(ifa);
444}
445
446void
447if_activate_sadl(struct ifnet *ifp, struct ifaddr *ifa,
448    const struct sockaddr_dl *sdl)
449{
450	int s;
451
452	s = splnet();
453
454	if_deactivate_sadl(ifp);
455
456	if_sadl_setrefs(ifp, ifa);
457	IFADDR_FOREACH(ifa, ifp)
458		rtinit(ifa, RTM_LLINFO_UPD, 0);
459	splx(s);
460}
461
462/*
463 * Free the link level name for the specified interface.  This is
464 * a detach helper.  This is called from if_detach() or from
465 * link layer type specific detach functions.
466 */
467void
468if_free_sadl(struct ifnet *ifp)
469{
470	struct ifaddr *ifa;
471	int s;
472
473	ifa = ifnet_addrs[ifp->if_index];
474	if (ifa == NULL) {
475		KASSERT(ifp->if_sadl == NULL);
476		KASSERT(ifp->if_dl == NULL);
477		return;
478	}
479
480	KASSERT(ifp->if_sadl != NULL);
481	KASSERT(ifp->if_dl != NULL);
482
483	s = splnet();
484	rtinit(ifa, RTM_DELETE, 0);
485	ifa_remove(ifp, ifa);
486	if_deactivate_sadl(ifp);
487	if (ifp->if_hwdl == ifa) {
488		IFAFREE(ifa);
489		ifp->if_hwdl = NULL;
490	}
491	splx(s);
492}
493
494/*
495 * Attach an interface to the
496 * list of "active" interfaces.
497 */
498void
499if_attach(struct ifnet *ifp)
500{
501	int indexlim = 0;
502
503	if (if_indexlim == 0) {
504		TAILQ_INIT(&ifnet);
505		if_indexlim = 8;
506	}
507	TAILQ_INIT(&ifp->if_addrlist);
508	TAILQ_INSERT_TAIL(&ifnet, ifp, if_list);
509
510	if (ifioctl_attach(ifp) != 0)
511		panic("%s: ifioctl_attach() failed", __func__);
512
513	mutex_enter(&index_gen_mtx);
514	ifp->if_index_gen = index_gen++;
515	mutex_exit(&index_gen_mtx);
516
517	ifp->if_index = if_index;
518	if (ifindex2ifnet == NULL)
519		if_index++;
520	else
521		while (ifp->if_index < if_indexlim &&
522		    ifindex2ifnet[ifp->if_index] != NULL) {
523			++if_index;
524			if (if_index == 0)
525				if_index = 1;
526			/*
527			 * If we hit USHRT_MAX, we skip back to 0 since
528			 * there are a number of places where the value
529			 * of if_index or if_index itself is compared
530			 * to or stored in an unsigned short.  By
531			 * jumping back, we won't botch those assignments
532			 * or comparisons.
533			 */
534			else if (if_index == USHRT_MAX) {
535				/*
536				 * However, if we have to jump back to
537				 * zero *twice* without finding an empty
538				 * slot in ifindex2ifnet[], then there
539				 * there are too many (>65535) interfaces.
540				 */
541				if (indexlim++)
542					panic("too many interfaces");
543				else
544					if_index = 1;
545			}
546			ifp->if_index = if_index;
547		}
548
549	/*
550	 * We have some arrays that should be indexed by if_index.
551	 * since if_index will grow dynamically, they should grow too.
552	 *	struct ifadd **ifnet_addrs
553	 *	struct ifnet **ifindex2ifnet
554	 */
555	if (ifnet_addrs == NULL || ifindex2ifnet == NULL ||
556	    ifp->if_index >= if_indexlim) {
557		size_t m, n, oldlim;
558		void *q;
559
560		oldlim = if_indexlim;
561		while (ifp->if_index >= if_indexlim)
562			if_indexlim <<= 1;
563
564		/* grow ifnet_addrs */
565		m = oldlim * sizeof(struct ifaddr *);
566		n = if_indexlim * sizeof(struct ifaddr *);
567		q = malloc(n, M_IFADDR, M_WAITOK|M_ZERO);
568		if (ifnet_addrs != NULL) {
569			memcpy(q, ifnet_addrs, m);
570			free(ifnet_addrs, M_IFADDR);
571		}
572		ifnet_addrs = (struct ifaddr **)q;
573
574		/* grow ifindex2ifnet */
575		m = oldlim * sizeof(struct ifnet *);
576		n = if_indexlim * sizeof(struct ifnet *);
577		q = malloc(n, M_IFADDR, M_WAITOK|M_ZERO);
578		if (ifindex2ifnet != NULL) {
579			memcpy(q, ifindex2ifnet, m);
580			free(ifindex2ifnet, M_IFADDR);
581		}
582		ifindex2ifnet = (struct ifnet **)q;
583	}
584
585	ifindex2ifnet[ifp->if_index] = ifp;
586
587	/*
588	 * Link level name is allocated later by a separate call to
589	 * if_alloc_sadl().
590	 */
591
592	if (ifp->if_snd.ifq_maxlen == 0)
593		ifp->if_snd.ifq_maxlen = ifqmaxlen;
594
595	sysctl_sndq_setup(&ifp->if_sysctl_log, ifp->if_xname, &ifp->if_snd);
596
597	ifp->if_broadcastaddr = 0; /* reliably crash if used uninitialized */
598
599	ifp->if_link_state = LINK_STATE_UNKNOWN;
600
601	ifp->if_capenable = 0;
602	ifp->if_csum_flags_tx = 0;
603	ifp->if_csum_flags_rx = 0;
604
605#ifdef ALTQ
606	ifp->if_snd.altq_type = 0;
607	ifp->if_snd.altq_disc = NULL;
608	ifp->if_snd.altq_flags &= ALTQF_CANTCHANGE;
609	ifp->if_snd.altq_tbr  = NULL;
610	ifp->if_snd.altq_ifp  = ifp;
611#endif
612
613#ifdef PFIL_HOOKS
614	ifp->if_pfil.ph_type = PFIL_TYPE_IFNET;
615	ifp->if_pfil.ph_ifnet = ifp;
616	if (pfil_head_register(&ifp->if_pfil) != 0)
617		printf("%s: WARNING: unable to register pfil hook\n",
618		    ifp->if_xname);
619	(void)pfil_run_hooks(&if_pfil,
620	    (struct mbuf **)PFIL_IFNET_ATTACH, ifp, PFIL_IFNET);
621#endif
622
623	if (!STAILQ_EMPTY(&domains))
624		if_attachdomain1(ifp);
625
626	/* Announce the interface. */
627	rt_ifannouncemsg(ifp, IFAN_ARRIVAL);
628}
629
630void
631if_attachdomain(void)
632{
633	struct ifnet *ifp;
634	int s;
635
636	s = splnet();
637	IFNET_FOREACH(ifp)
638		if_attachdomain1(ifp);
639	splx(s);
640}
641
642void
643if_attachdomain1(struct ifnet *ifp)
644{
645	struct domain *dp;
646	int s;
647
648	s = splnet();
649
650	/* address family dependent data region */
651	memset(ifp->if_afdata, 0, sizeof(ifp->if_afdata));
652	DOMAIN_FOREACH(dp) {
653		if (dp->dom_ifattach != NULL)
654			ifp->if_afdata[dp->dom_family] =
655			    (*dp->dom_ifattach)(ifp);
656	}
657
658	splx(s);
659}
660
661/*
662 * Deactivate an interface.  This points all of the procedure
663 * handles at error stubs.  May be called from interrupt context.
664 */
665void
666if_deactivate(struct ifnet *ifp)
667{
668	int s;
669
670	s = splnet();
671
672	ifp->if_output	 = if_nulloutput;
673	ifp->if_input	 = if_nullinput;
674	ifp->if_start	 = if_nullstart;
675	ifp->if_ioctl	 = if_nullioctl;
676	ifp->if_init	 = if_nullinit;
677	ifp->if_stop	 = if_nullstop;
678	ifp->if_watchdog = if_nullwatchdog;
679	ifp->if_drain	 = if_nulldrain;
680
681	/* No more packets may be enqueued. */
682	ifp->if_snd.ifq_maxlen = 0;
683
684	splx(s);
685}
686
687void
688if_purgeaddrs(struct ifnet *ifp, int family, void (*purgeaddr)(struct ifaddr *))
689{
690	struct ifaddr *ifa, *nifa;
691
692	for (ifa = IFADDR_FIRST(ifp); ifa != NULL; ifa = nifa) {
693		nifa = IFADDR_NEXT(ifa);
694		if (ifa->ifa_addr->sa_family != family)
695			continue;
696		(*purgeaddr)(ifa);
697	}
698}
699
700/*
701 * Detach an interface from the list of "active" interfaces,
702 * freeing any resources as we go along.
703 *
704 * NOTE: This routine must be called with a valid thread context,
705 * as it may block.
706 */
707void
708if_detach(struct ifnet *ifp)
709{
710	struct socket so;
711	struct ifaddr *ifa;
712#ifdef IFAREF_DEBUG
713	struct ifaddr *last_ifa = NULL;
714#endif
715	struct domain *dp;
716	const struct protosw *pr;
717	int s, i, family, purged;
718
719	/*
720	 * XXX It's kind of lame that we have to have the
721	 * XXX socket structure...
722	 */
723	memset(&so, 0, sizeof(so));
724
725	s = splnet();
726
727	/*
728	 * Do an if_down() to give protocols a chance to do something.
729	 */
730	if_down(ifp);
731
732#ifdef ALTQ
733	if (ALTQ_IS_ENABLED(&ifp->if_snd))
734		altq_disable(&ifp->if_snd);
735	if (ALTQ_IS_ATTACHED(&ifp->if_snd))
736		altq_detach(&ifp->if_snd);
737#endif
738
739	sysctl_teardown(&ifp->if_sysctl_log);
740
741#if NCARP > 0
742	/* Remove the interface from any carp group it is a part of.  */
743	if (ifp->if_carp != NULL && ifp->if_type != IFT_CARP)
744		carp_ifdetach(ifp);
745#endif
746
747	/*
748	 * Rip all the addresses off the interface.  This should make
749	 * all of the routes go away.
750	 *
751	 * pr_usrreq calls can remove an arbitrary number of ifaddrs
752	 * from the list, including our "cursor", ifa.  For safety,
753	 * and to honor the TAILQ abstraction, I just restart the
754	 * loop after each removal.  Note that the loop will exit
755	 * when all of the remaining ifaddrs belong to the AF_LINK
756	 * family.  I am counting on the historical fact that at
757	 * least one pr_usrreq in each address domain removes at
758	 * least one ifaddr.
759	 */
760again:
761	IFADDR_FOREACH(ifa, ifp) {
762		family = ifa->ifa_addr->sa_family;
763#ifdef IFAREF_DEBUG
764		printf("if_detach: ifaddr %p, family %d, refcnt %d\n",
765		    ifa, family, ifa->ifa_refcnt);
766		if (last_ifa != NULL && ifa == last_ifa)
767			panic("if_detach: loop detected");
768		last_ifa = ifa;
769#endif
770		if (family == AF_LINK)
771			continue;
772		dp = pffinddomain(family);
773#ifdef DIAGNOSTIC
774		if (dp == NULL)
775			panic("if_detach: no domain for AF %d",
776			    family);
777#endif
778		/*
779		 * XXX These PURGEIF calls are redundant with the
780		 * purge-all-families calls below, but are left in for
781		 * now both to make a smaller change, and to avoid
782		 * unplanned interactions with clearing of
783		 * ifp->if_addrlist.
784		 */
785		purged = 0;
786		for (pr = dp->dom_protosw;
787		     pr < dp->dom_protoswNPROTOSW; pr++) {
788			so.so_proto = pr;
789			if (pr->pr_usrreq != NULL) {
790				(void) (*pr->pr_usrreq)(&so,
791				    PRU_PURGEIF, NULL, NULL,
792				    (struct mbuf *) ifp, curlwp);
793				purged = 1;
794			}
795		}
796		if (purged == 0) {
797			/*
798			 * XXX What's really the best thing to do
799			 * XXX here?  --thorpej@NetBSD.org
800			 */
801			printf("if_detach: WARNING: AF %d not purged\n",
802			    family);
803			ifa_remove(ifp, ifa);
804		}
805		goto again;
806	}
807
808	if_free_sadl(ifp);
809
810	/* Walk the routing table looking for stragglers. */
811	for (i = 0; i <= AF_MAX; i++) {
812		while (rt_walktree(i, if_rt_walktree, ifp) == ERESTART)
813			continue;
814	}
815
816	DOMAIN_FOREACH(dp) {
817		if (dp->dom_ifdetach != NULL && ifp->if_afdata[dp->dom_family])
818		{
819			void *p = ifp->if_afdata[dp->dom_family];
820			if (p) {
821				ifp->if_afdata[dp->dom_family] = NULL;
822				(*dp->dom_ifdetach)(ifp, p);
823			}
824		}
825
826		/*
827		 * One would expect multicast memberships (INET and
828		 * INET6) on UDP sockets to be purged by the PURGEIF
829		 * calls above, but if all addresses were removed from
830		 * the interface prior to destruction, the calls will
831		 * not be made (e.g. ppp, for which pppd(8) generally
832		 * removes addresses before destroying the interface).
833		 * Because there is no invariant that multicast
834		 * memberships only exist for interfaces with IPv4
835		 * addresses, we must call PURGEIF regardless of
836		 * addresses.  (Protocols which might store ifnet
837		 * pointers are marked with PR_PURGEIF.)
838		 */
839		for (pr = dp->dom_protosw; pr < dp->dom_protoswNPROTOSW; pr++) {
840			so.so_proto = pr;
841			if (pr->pr_usrreq != NULL && pr->pr_flags & PR_PURGEIF)
842				(void)(*pr->pr_usrreq)(&so, PRU_PURGEIF, NULL,
843				    NULL, (struct mbuf *)ifp, curlwp);
844		}
845	}
846
847#ifdef PFIL_HOOKS
848	(void)pfil_run_hooks(&if_pfil,
849	    (struct mbuf **)PFIL_IFNET_DETACH, ifp, PFIL_IFNET);
850	(void)pfil_head_unregister(&ifp->if_pfil);
851#endif
852
853	/* Announce that the interface is gone. */
854	rt_ifannouncemsg(ifp, IFAN_DEPARTURE);
855
856	ifindex2ifnet[ifp->if_index] = NULL;
857
858	TAILQ_REMOVE(&ifnet, ifp, if_list);
859
860	ifioctl_detach(ifp);
861
862	/*
863	 * remove packets that came from ifp, from software interrupt queues.
864	 */
865	DOMAIN_FOREACH(dp) {
866		for (i = 0; i < __arraycount(dp->dom_ifqueues); i++) {
867			struct ifqueue *iq = dp->dom_ifqueues[i];
868			if (iq == NULL)
869				break;
870			dp->dom_ifqueues[i] = NULL;
871			if_detach_queues(ifp, iq);
872		}
873	}
874
875	splx(s);
876}
877
878static void
879if_detach_queues(struct ifnet *ifp, struct ifqueue *q)
880{
881	struct mbuf *m, *prev, *next;
882
883	prev = NULL;
884	for (m = q->ifq_head; m != NULL; m = next) {
885		next = m->m_nextpkt;
886#ifdef DIAGNOSTIC
887		if ((m->m_flags & M_PKTHDR) == 0) {
888			prev = m;
889			continue;
890		}
891#endif
892		if (m->m_pkthdr.rcvif != ifp) {
893			prev = m;
894			continue;
895		}
896
897		if (prev != NULL)
898			prev->m_nextpkt = m->m_nextpkt;
899		else
900			q->ifq_head = m->m_nextpkt;
901		if (q->ifq_tail == m)
902			q->ifq_tail = prev;
903		q->ifq_len--;
904
905		m->m_nextpkt = NULL;
906		m_freem(m);
907		IF_DROP(q);
908	}
909}
910
911/*
912 * Callback for a radix tree walk to delete all references to an
913 * ifnet.
914 */
915static int
916if_rt_walktree(struct rtentry *rt, void *v)
917{
918	struct ifnet *ifp = (struct ifnet *)v;
919	int error;
920
921	if (rt->rt_ifp != ifp)
922		return 0;
923
924	/* Delete the entry. */
925	++rt->rt_refcnt;
926	error = rtrequest(RTM_DELETE, rt_getkey(rt), rt->rt_gateway,
927	    rt_mask(rt), rt->rt_flags, NULL);
928	KASSERT((rt->rt_flags & RTF_UP) == 0);
929	rt->rt_ifp = NULL;
930	RTFREE(rt);
931	if (error != 0)
932		printf("%s: warning: unable to delete rtentry @ %p, "
933		    "error = %d\n", ifp->if_xname, rt, error);
934	return ERESTART;
935}
936
937/*
938 * Create a clone network interface.
939 */
940int
941if_clone_create(const char *name)
942{
943	struct if_clone *ifc;
944	int unit;
945
946	ifc = if_clone_lookup(name, &unit);
947	if (ifc == NULL)
948		return EINVAL;
949
950	if (ifunit(name) != NULL)
951		return EEXIST;
952
953	return (*ifc->ifc_create)(ifc, unit);
954}
955
956/*
957 * Destroy a clone network interface.
958 */
959int
960if_clone_destroy(const char *name)
961{
962	struct if_clone *ifc;
963	struct ifnet *ifp;
964
965	ifc = if_clone_lookup(name, NULL);
966	if (ifc == NULL)
967		return EINVAL;
968
969	ifp = ifunit(name);
970	if (ifp == NULL)
971		return ENXIO;
972
973	if (ifc->ifc_destroy == NULL)
974		return EOPNOTSUPP;
975
976	return (*ifc->ifc_destroy)(ifp);
977}
978
979/*
980 * Look up a network interface cloner.
981 */
982static struct if_clone *
983if_clone_lookup(const char *name, int *unitp)
984{
985	struct if_clone *ifc;
986	const char *cp;
987	int unit;
988
989	/* separate interface name from unit */
990	for (cp = name;
991	    cp - name < IFNAMSIZ && *cp && (*cp < '0' || *cp > '9');
992	    cp++)
993		continue;
994
995	if (cp == name || cp - name == IFNAMSIZ || !*cp)
996		return NULL;	/* No name or unit number */
997
998	LIST_FOREACH(ifc, &if_cloners, ifc_list) {
999		if (strlen(ifc->ifc_name) == cp - name &&
1000		    strncmp(name, ifc->ifc_name, cp - name) == 0)
1001			break;
1002	}
1003
1004	if (ifc == NULL)
1005		return NULL;
1006
1007	unit = 0;
1008	while (cp - name < IFNAMSIZ && *cp) {
1009		if (*cp < '0' || *cp > '9' || unit >= INT_MAX / 10) {
1010			/* Bogus unit number. */
1011			return NULL;
1012		}
1013		unit = (unit * 10) + (*cp++ - '0');
1014	}
1015
1016	if (unitp != NULL)
1017		*unitp = unit;
1018	return ifc;
1019}
1020
1021/*
1022 * Register a network interface cloner.
1023 */
1024void
1025if_clone_attach(struct if_clone *ifc)
1026{
1027
1028	LIST_INSERT_HEAD(&if_cloners, ifc, ifc_list);
1029	if_cloners_count++;
1030}
1031
1032/*
1033 * Unregister a network interface cloner.
1034 */
1035void
1036if_clone_detach(struct if_clone *ifc)
1037{
1038
1039	LIST_REMOVE(ifc, ifc_list);
1040	if_cloners_count--;
1041}
1042
1043/*
1044 * Provide list of interface cloners to userspace.
1045 */
1046static int
1047if_clone_list(struct if_clonereq *ifcr)
1048{
1049	char outbuf[IFNAMSIZ], *dst;
1050	struct if_clone *ifc;
1051	int count, error = 0;
1052
1053	ifcr->ifcr_total = if_cloners_count;
1054	if ((dst = ifcr->ifcr_buffer) == NULL) {
1055		/* Just asking how many there are. */
1056		return 0;
1057	}
1058
1059	if (ifcr->ifcr_count < 0)
1060		return EINVAL;
1061
1062	count = (if_cloners_count < ifcr->ifcr_count) ?
1063	    if_cloners_count : ifcr->ifcr_count;
1064
1065	for (ifc = LIST_FIRST(&if_cloners); ifc != NULL && count != 0;
1066	     ifc = LIST_NEXT(ifc, ifc_list), count--, dst += IFNAMSIZ) {
1067		(void)strncpy(outbuf, ifc->ifc_name, sizeof(outbuf));
1068		if (outbuf[sizeof(outbuf) - 1] != '\0')
1069			return ENAMETOOLONG;
1070		error = copyout(outbuf, dst, sizeof(outbuf));
1071		if (error != 0)
1072			break;
1073	}
1074
1075	return error;
1076}
1077
1078void
1079ifa_insert(struct ifnet *ifp, struct ifaddr *ifa)
1080{
1081	ifa->ifa_ifp = ifp;
1082	TAILQ_INSERT_TAIL(&ifp->if_addrlist, ifa, ifa_list);
1083	IFAREF(ifa);
1084}
1085
1086void
1087ifa_remove(struct ifnet *ifp, struct ifaddr *ifa)
1088{
1089	KASSERT(ifa->ifa_ifp == ifp);
1090	TAILQ_REMOVE(&ifp->if_addrlist, ifa, ifa_list);
1091	IFAFREE(ifa);
1092}
1093
1094static inline int
1095equal(const struct sockaddr *sa1, const struct sockaddr *sa2)
1096{
1097	return sockaddr_cmp(sa1, sa2) == 0;
1098}
1099
1100/*
1101 * Locate an interface based on a complete address.
1102 */
1103/*ARGSUSED*/
1104struct ifaddr *
1105ifa_ifwithaddr(const struct sockaddr *addr)
1106{
1107	struct ifnet *ifp;
1108	struct ifaddr *ifa;
1109
1110	IFNET_FOREACH(ifp) {
1111		if (ifp->if_output == if_nulloutput)
1112			continue;
1113		IFADDR_FOREACH(ifa, ifp) {
1114			if (ifa->ifa_addr->sa_family != addr->sa_family)
1115				continue;
1116			if (equal(addr, ifa->ifa_addr))
1117				return ifa;
1118			if ((ifp->if_flags & IFF_BROADCAST) &&
1119			    ifa->ifa_broadaddr &&
1120			    /* IP6 doesn't have broadcast */
1121			    ifa->ifa_broadaddr->sa_len != 0 &&
1122			    equal(ifa->ifa_broadaddr, addr))
1123				return ifa;
1124		}
1125	}
1126	return NULL;
1127}
1128
1129/*
1130 * Locate the point to point interface with a given destination address.
1131 */
1132/*ARGSUSED*/
1133struct ifaddr *
1134ifa_ifwithdstaddr(const struct sockaddr *addr)
1135{
1136	struct ifnet *ifp;
1137	struct ifaddr *ifa;
1138
1139	IFNET_FOREACH(ifp) {
1140		if (ifp->if_output == if_nulloutput)
1141			continue;
1142		if ((ifp->if_flags & IFF_POINTOPOINT) == 0)
1143			continue;
1144		IFADDR_FOREACH(ifa, ifp) {
1145			if (ifa->ifa_addr->sa_family != addr->sa_family ||
1146			    ifa->ifa_dstaddr == NULL)
1147				continue;
1148			if (equal(addr, ifa->ifa_dstaddr))
1149				return ifa;
1150		}
1151	}
1152	return NULL;
1153}
1154
1155/*
1156 * Find an interface on a specific network.  If many, choice
1157 * is most specific found.
1158 */
1159struct ifaddr *
1160ifa_ifwithnet(const struct sockaddr *addr)
1161{
1162	struct ifnet *ifp;
1163	struct ifaddr *ifa;
1164	const struct sockaddr_dl *sdl;
1165	struct ifaddr *ifa_maybe = 0;
1166	u_int af = addr->sa_family;
1167	const char *addr_data = addr->sa_data, *cplim;
1168
1169	if (af == AF_LINK) {
1170		sdl = satocsdl(addr);
1171		if (sdl->sdl_index && sdl->sdl_index < if_indexlim &&
1172		    ifindex2ifnet[sdl->sdl_index] &&
1173		    ifindex2ifnet[sdl->sdl_index]->if_output != if_nulloutput)
1174			return ifnet_addrs[sdl->sdl_index];
1175	}
1176#ifdef NETATALK
1177	if (af == AF_APPLETALK) {
1178		const struct sockaddr_at *sat, *sat2;
1179		sat = (const struct sockaddr_at *)addr;
1180		IFNET_FOREACH(ifp) {
1181			if (ifp->if_output == if_nulloutput)
1182				continue;
1183			ifa = at_ifawithnet((const struct sockaddr_at *)addr, ifp);
1184			if (ifa == NULL)
1185				continue;
1186			sat2 = (struct sockaddr_at *)ifa->ifa_addr;
1187			if (sat2->sat_addr.s_net == sat->sat_addr.s_net)
1188				return ifa; /* exact match */
1189			if (ifa_maybe == NULL) {
1190				/* else keep the if with the right range */
1191				ifa_maybe = ifa;
1192			}
1193		}
1194		return ifa_maybe;
1195	}
1196#endif
1197	IFNET_FOREACH(ifp) {
1198		if (ifp->if_output == if_nulloutput)
1199			continue;
1200		IFADDR_FOREACH(ifa, ifp) {
1201			const char *cp, *cp2, *cp3;
1202
1203			if (ifa->ifa_addr->sa_family != af ||
1204			    ifa->ifa_netmask == NULL)
1205 next:				continue;
1206			cp = addr_data;
1207			cp2 = ifa->ifa_addr->sa_data;
1208			cp3 = ifa->ifa_netmask->sa_data;
1209			cplim = (const char *)ifa->ifa_netmask +
1210			    ifa->ifa_netmask->sa_len;
1211			while (cp3 < cplim) {
1212				if ((*cp++ ^ *cp2++) & *cp3++) {
1213					/* want to continue for() loop */
1214					goto next;
1215				}
1216			}
1217			if (ifa_maybe == NULL ||
1218			    rn_refines((void *)ifa->ifa_netmask,
1219			    (void *)ifa_maybe->ifa_netmask))
1220				ifa_maybe = ifa;
1221		}
1222	}
1223	return ifa_maybe;
1224}
1225
1226/*
1227 * Find the interface of the addresss.
1228 */
1229struct ifaddr *
1230ifa_ifwithladdr(const struct sockaddr *addr)
1231{
1232	struct ifaddr *ia;
1233
1234	if ((ia = ifa_ifwithaddr(addr)) || (ia = ifa_ifwithdstaddr(addr)) ||
1235	    (ia = ifa_ifwithnet(addr)))
1236		return ia;
1237	return NULL;
1238}
1239
1240/*
1241 * Find an interface using a specific address family
1242 */
1243struct ifaddr *
1244ifa_ifwithaf(int af)
1245{
1246	struct ifnet *ifp;
1247	struct ifaddr *ifa;
1248
1249	IFNET_FOREACH(ifp) {
1250		if (ifp->if_output == if_nulloutput)
1251			continue;
1252		IFADDR_FOREACH(ifa, ifp) {
1253			if (ifa->ifa_addr->sa_family == af)
1254				return ifa;
1255		}
1256	}
1257	return NULL;
1258}
1259
1260/*
1261 * Find an interface address specific to an interface best matching
1262 * a given address.
1263 */
1264struct ifaddr *
1265ifaof_ifpforaddr(const struct sockaddr *addr, struct ifnet *ifp)
1266{
1267	struct ifaddr *ifa;
1268	const char *cp, *cp2, *cp3;
1269	const char *cplim;
1270	struct ifaddr *ifa_maybe = 0;
1271	u_int af = addr->sa_family;
1272
1273	if (ifp->if_output == if_nulloutput)
1274		return NULL;
1275
1276	if (af >= AF_MAX)
1277		return NULL;
1278
1279	IFADDR_FOREACH(ifa, ifp) {
1280		if (ifa->ifa_addr->sa_family != af)
1281			continue;
1282		ifa_maybe = ifa;
1283		if (ifa->ifa_netmask == NULL) {
1284			if (equal(addr, ifa->ifa_addr) ||
1285			    (ifa->ifa_dstaddr &&
1286			     equal(addr, ifa->ifa_dstaddr)))
1287				return ifa;
1288			continue;
1289		}
1290		cp = addr->sa_data;
1291		cp2 = ifa->ifa_addr->sa_data;
1292		cp3 = ifa->ifa_netmask->sa_data;
1293		cplim = ifa->ifa_netmask->sa_len + (char *)ifa->ifa_netmask;
1294		for (; cp3 < cplim; cp3++) {
1295			if ((*cp++ ^ *cp2++) & *cp3)
1296				break;
1297		}
1298		if (cp3 == cplim)
1299			return ifa;
1300	}
1301	return ifa_maybe;
1302}
1303
1304/*
1305 * Default action when installing a route with a Link Level gateway.
1306 * Lookup an appropriate real ifa to point to.
1307 * This should be moved to /sys/net/link.c eventually.
1308 */
1309void
1310link_rtrequest(int cmd, struct rtentry *rt, const struct rt_addrinfo *info)
1311{
1312	struct ifaddr *ifa;
1313	const struct sockaddr *dst;
1314	struct ifnet *ifp;
1315
1316	if (cmd != RTM_ADD || (ifa = rt->rt_ifa) == NULL ||
1317	    (ifp = ifa->ifa_ifp) == NULL || (dst = rt_getkey(rt)) == NULL)
1318		return;
1319	if ((ifa = ifaof_ifpforaddr(dst, ifp)) != NULL) {
1320		rt_replace_ifa(rt, ifa);
1321		if (ifa->ifa_rtrequest && ifa->ifa_rtrequest != link_rtrequest)
1322			ifa->ifa_rtrequest(cmd, rt, info);
1323	}
1324}
1325
1326/*
1327 * Handle a change in the interface link state.
1328 */
1329void
1330if_link_state_change(struct ifnet *ifp, int link_state)
1331{
1332	if (ifp->if_link_state == link_state)
1333		return;
1334	ifp->if_link_state = link_state;
1335	/* Notify that the link state has changed. */
1336	rt_ifmsg(ifp);
1337#if NCARP > 0
1338	if (ifp->if_carp)
1339		carp_carpdev_state(ifp);
1340#endif
1341}
1342
1343/*
1344 * Mark an interface down and notify protocols of
1345 * the transition.
1346 * NOTE: must be called at splsoftnet or equivalent.
1347 */
1348void
1349if_down(struct ifnet *ifp)
1350{
1351	struct ifaddr *ifa;
1352
1353	ifp->if_flags &= ~IFF_UP;
1354	nanotime(&ifp->if_lastchange);
1355	IFADDR_FOREACH(ifa, ifp)
1356		pfctlinput(PRC_IFDOWN, ifa->ifa_addr);
1357	IFQ_PURGE(&ifp->if_snd);
1358#if NCARP > 0
1359	if (ifp->if_carp)
1360		carp_carpdev_state(ifp);
1361#endif
1362	rt_ifmsg(ifp);
1363}
1364
1365/*
1366 * Mark an interface up and notify protocols of
1367 * the transition.
1368 * NOTE: must be called at splsoftnet or equivalent.
1369 */
1370void
1371if_up(struct ifnet *ifp)
1372{
1373#ifdef notyet
1374	struct ifaddr *ifa;
1375#endif
1376
1377	ifp->if_flags |= IFF_UP;
1378	nanotime(&ifp->if_lastchange);
1379#ifdef notyet
1380	/* this has no effect on IP, and will kill all ISO connections XXX */
1381	IFADDR_FOREACH(ifa, ifp)
1382		pfctlinput(PRC_IFUP, ifa->ifa_addr);
1383#endif
1384#if NCARP > 0
1385	if (ifp->if_carp)
1386		carp_carpdev_state(ifp);
1387#endif
1388	rt_ifmsg(ifp);
1389#ifdef INET6
1390	in6_if_up(ifp);
1391#endif
1392}
1393
1394/*
1395 * Handle interface watchdog timer routines.  Called
1396 * from softclock, we decrement timers (if set) and
1397 * call the appropriate interface routine on expiration.
1398 */
1399void
1400if_slowtimo(void *arg)
1401{
1402	struct ifnet *ifp;
1403	int s = splnet();
1404
1405	IFNET_FOREACH(ifp) {
1406		if (ifp->if_timer == 0 || --ifp->if_timer)
1407			continue;
1408		if (ifp->if_watchdog != NULL)
1409			(*ifp->if_watchdog)(ifp);
1410	}
1411	splx(s);
1412	callout_reset(&if_slowtimo_ch, hz / IFNET_SLOWHZ, if_slowtimo, NULL);
1413}
1414
1415/*
1416 * Set/clear promiscuous mode on interface ifp based on the truth value
1417 * of pswitch.  The calls are reference counted so that only the first
1418 * "on" request actually has an effect, as does the final "off" request.
1419 * Results are undefined if the "off" and "on" requests are not matched.
1420 */
1421int
1422ifpromisc(struct ifnet *ifp, int pswitch)
1423{
1424	int pcount, ret;
1425	short nflags;
1426
1427	pcount = ifp->if_pcount;
1428	if (pswitch) {
1429		/*
1430		 * Allow the device to be "placed" into promiscuous
1431		 * mode even if it is not configured up.  It will
1432		 * consult IFF_PROMISC when it is brought up.
1433		 */
1434		if (ifp->if_pcount++ != 0)
1435			return 0;
1436		nflags = ifp->if_flags | IFF_PROMISC;
1437	} else {
1438		if (--ifp->if_pcount > 0)
1439			return 0;
1440		nflags = ifp->if_flags & ~IFF_PROMISC;
1441	}
1442	ret = if_flags_set(ifp, nflags);
1443	/* Restore interface state if not successful. */
1444	if (ret != 0) {
1445		ifp->if_pcount = pcount;
1446	}
1447	return ret;
1448}
1449
1450/*
1451 * Map interface name to
1452 * interface structure pointer.
1453 */
1454struct ifnet *
1455ifunit(const char *name)
1456{
1457	struct ifnet *ifp;
1458	const char *cp = name;
1459	u_int unit = 0;
1460	u_int i;
1461
1462	/*
1463	 * If the entire name is a number, treat it as an ifindex.
1464	 */
1465	for (i = 0; i < IFNAMSIZ && *cp >= '0' && *cp <= '9'; i++, cp++) {
1466		unit = unit * 10 + (*cp - '0');
1467	}
1468
1469	/*
1470	 * If the number took all of the name, then it's a valid ifindex.
1471	 */
1472	if (i == IFNAMSIZ || (cp != name && *cp == '\0')) {
1473		if (unit >= if_indexlim)
1474			return NULL;
1475		ifp = ifindex2ifnet[unit];
1476		if (ifp == NULL || ifp->if_output == if_nulloutput)
1477			return NULL;
1478		return ifp;
1479	}
1480
1481	IFNET_FOREACH(ifp) {
1482		if (ifp->if_output == if_nulloutput)
1483			continue;
1484	 	if (strcmp(ifp->if_xname, name) == 0)
1485			return ifp;
1486	}
1487	return NULL;
1488}
1489
1490ifnet_t *
1491if_byindex(u_int idx)
1492{
1493
1494	return (idx < if_indexlim) ? ifindex2ifnet[idx] : NULL;
1495}
1496
1497/* common */
1498int
1499ifioctl_common(struct ifnet *ifp, u_long cmd, void *data)
1500{
1501	int s;
1502	struct ifreq *ifr;
1503	struct ifcapreq *ifcr;
1504	struct ifdatareq *ifdr;
1505
1506	switch (cmd) {
1507	case SIOCSIFCAP:
1508		ifcr = data;
1509		if ((ifcr->ifcr_capenable & ~ifp->if_capabilities) != 0)
1510			return EINVAL;
1511
1512		if (ifcr->ifcr_capenable == ifp->if_capenable)
1513			return 0;
1514
1515		ifp->if_capenable = ifcr->ifcr_capenable;
1516
1517		/* Pre-compute the checksum flags mask. */
1518		ifp->if_csum_flags_tx = 0;
1519		ifp->if_csum_flags_rx = 0;
1520		if (ifp->if_capenable & IFCAP_CSUM_IPv4_Tx) {
1521			ifp->if_csum_flags_tx |= M_CSUM_IPv4;
1522		}
1523		if (ifp->if_capenable & IFCAP_CSUM_IPv4_Rx) {
1524			ifp->if_csum_flags_rx |= M_CSUM_IPv4;
1525		}
1526
1527		if (ifp->if_capenable & IFCAP_CSUM_TCPv4_Tx) {
1528			ifp->if_csum_flags_tx |= M_CSUM_TCPv4;
1529		}
1530		if (ifp->if_capenable & IFCAP_CSUM_TCPv4_Rx) {
1531			ifp->if_csum_flags_rx |= M_CSUM_TCPv4;
1532		}
1533
1534		if (ifp->if_capenable & IFCAP_CSUM_UDPv4_Tx) {
1535			ifp->if_csum_flags_tx |= M_CSUM_UDPv4;
1536		}
1537		if (ifp->if_capenable & IFCAP_CSUM_UDPv4_Rx) {
1538			ifp->if_csum_flags_rx |= M_CSUM_UDPv4;
1539		}
1540
1541		if (ifp->if_capenable & IFCAP_CSUM_TCPv6_Tx) {
1542			ifp->if_csum_flags_tx |= M_CSUM_TCPv6;
1543		}
1544		if (ifp->if_capenable & IFCAP_CSUM_TCPv6_Rx) {
1545			ifp->if_csum_flags_rx |= M_CSUM_TCPv6;
1546		}
1547
1548		if (ifp->if_capenable & IFCAP_CSUM_UDPv6_Tx) {
1549			ifp->if_csum_flags_tx |= M_CSUM_UDPv6;
1550		}
1551		if (ifp->if_capenable & IFCAP_CSUM_UDPv6_Rx) {
1552			ifp->if_csum_flags_rx |= M_CSUM_UDPv6;
1553		}
1554		if (ifp->if_flags & IFF_UP)
1555			return ENETRESET;
1556		return 0;
1557	case SIOCSIFFLAGS:
1558		ifr = data;
1559		if (ifp->if_flags & IFF_UP && (ifr->ifr_flags & IFF_UP) == 0) {
1560			s = splnet();
1561			if_down(ifp);
1562			splx(s);
1563		}
1564		if (ifr->ifr_flags & IFF_UP && (ifp->if_flags & IFF_UP) == 0) {
1565			s = splnet();
1566			if_up(ifp);
1567			splx(s);
1568		}
1569		ifp->if_flags = (ifp->if_flags & IFF_CANTCHANGE) |
1570			(ifr->ifr_flags &~ IFF_CANTCHANGE);
1571		break;
1572	case SIOCGIFFLAGS:
1573		ifr = data;
1574		ifr->ifr_flags = ifp->if_flags;
1575		break;
1576
1577	case SIOCGIFMETRIC:
1578		ifr = data;
1579		ifr->ifr_metric = ifp->if_metric;
1580		break;
1581
1582	case SIOCGIFMTU:
1583		ifr = data;
1584		ifr->ifr_mtu = ifp->if_mtu;
1585		break;
1586
1587	case SIOCGIFDLT:
1588		ifr = data;
1589		ifr->ifr_dlt = ifp->if_dlt;
1590		break;
1591
1592	case SIOCGIFCAP:
1593		ifcr = data;
1594		ifcr->ifcr_capabilities = ifp->if_capabilities;
1595		ifcr->ifcr_capenable = ifp->if_capenable;
1596		break;
1597
1598	case SIOCSIFMETRIC:
1599		ifr = data;
1600		ifp->if_metric = ifr->ifr_metric;
1601		break;
1602
1603	case SIOCGIFDATA:
1604		ifdr = data;
1605		ifdr->ifdr_data = ifp->if_data;
1606		break;
1607
1608	case SIOCZIFDATA:
1609		ifdr = data;
1610		ifdr->ifdr_data = ifp->if_data;
1611		/*
1612		 * Assumes that the volatile counters that can be
1613		 * zero'ed are at the end of if_data.
1614		 */
1615		memset(&ifp->if_data.ifi_ipackets, 0, sizeof(ifp->if_data) -
1616		    offsetof(struct if_data, ifi_ipackets));
1617		/*
1618		 * The memset() clears to the bottm of if_data. In the area,
1619		 * if_lastchange is included. Please be careful if new entry
1620		 * will be added into if_data or rewite this.
1621		 *
1622		 * And also, update if_lastchnage.
1623		 */
1624		getnanotime(&ifp->if_lastchange);
1625		break;
1626	case SIOCSIFMTU:
1627		ifr = data;
1628		if (ifp->if_mtu == ifr->ifr_mtu)
1629			break;
1630		ifp->if_mtu = ifr->ifr_mtu;
1631		/*
1632		 * If the link MTU changed, do network layer specific procedure.
1633		 */
1634#ifdef INET6
1635		nd6_setmtu(ifp);
1636#endif
1637		return ENETRESET;
1638	default:
1639		return ENOTTY;
1640	}
1641	return 0;
1642}
1643
1644int
1645ifaddrpref_ioctl(struct socket *so, u_long cmd, void *data, struct ifnet *ifp,
1646    lwp_t *l)
1647{
1648	struct if_addrprefreq *ifap = (struct if_addrprefreq *)data;
1649	struct ifaddr *ifa;
1650	const struct sockaddr *any, *sa;
1651	union {
1652		struct sockaddr sa;
1653		struct sockaddr_storage ss;
1654	} u, v;
1655
1656	switch (cmd) {
1657	case SIOCSIFADDRPREF:
1658		if (kauth_authorize_network(l->l_cred, KAUTH_NETWORK_INTERFACE,
1659		    KAUTH_REQ_NETWORK_INTERFACE_SETPRIV, ifp, (void *)cmd,
1660		    NULL) != 0)
1661			return EPERM;
1662	case SIOCGIFADDRPREF:
1663		break;
1664	default:
1665		return EOPNOTSUPP;
1666	}
1667
1668	/* sanity checks */
1669	if (data == NULL || ifp == NULL) {
1670		panic("invalid argument to %s", __func__);
1671		/*NOTREACHED*/
1672	}
1673
1674	/* address must be specified on ADD and DELETE */
1675	sa = sstocsa(&ifap->ifap_addr);
1676	if (sa->sa_family != sofamily(so))
1677		return EINVAL;
1678	if ((any = sockaddr_any(sa)) == NULL || sa->sa_len != any->sa_len)
1679		return EINVAL;
1680
1681	sockaddr_externalize(&v.sa, sizeof(v.ss), sa);
1682
1683	IFADDR_FOREACH(ifa, ifp) {
1684		if (ifa->ifa_addr->sa_family != sa->sa_family)
1685			continue;
1686		sockaddr_externalize(&u.sa, sizeof(u.ss), ifa->ifa_addr);
1687		if (sockaddr_cmp(&u.sa, &v.sa) == 0)
1688			break;
1689	}
1690	if (ifa == NULL)
1691		return EADDRNOTAVAIL;
1692
1693	switch (cmd) {
1694	case SIOCSIFADDRPREF:
1695		ifa->ifa_preference = ifap->ifap_preference;
1696		return 0;
1697	case SIOCGIFADDRPREF:
1698		/* fill in the if_laddrreq structure */
1699		(void)sockaddr_copy(sstosa(&ifap->ifap_addr),
1700		    sizeof(ifap->ifap_addr), ifa->ifa_addr);
1701		ifap->ifap_preference = ifa->ifa_preference;
1702		return 0;
1703	default:
1704		return EOPNOTSUPP;
1705	}
1706}
1707
1708static void
1709ifnet_lock_enter(struct ifnet_lock *il)
1710{
1711	uint64_t *nenter;
1712
1713	/* Before trying to acquire the mutex, increase the count of threads
1714	 * who have entered or who wait to enter the critical section.
1715	 * Avoid one costly locked memory transaction by keeping a count for
1716	 * each CPU.
1717	 */
1718	nenter = percpu_getref(il->il_nenter);
1719	(*nenter)++;
1720	percpu_putref(il->il_nenter);
1721	mutex_enter(&il->il_lock);
1722}
1723
1724static void
1725ifnet_lock_exit(struct ifnet_lock *il)
1726{
1727	/* Increase the count of threads who have exited the critical
1728	 * section.  Increase while we still hold the lock.
1729	 */
1730	il->il_nexit++;
1731	mutex_exit(&il->il_lock);
1732}
1733
1734/*
1735 * Interface ioctls.
1736 */
1737int
1738ifioctl(struct socket *so, u_long cmd, void *data, struct lwp *l)
1739{
1740	struct ifnet *ifp;
1741	struct ifreq *ifr;
1742	int error = 0;
1743#if defined(COMPAT_OSOCK) || defined(COMPAT_OIFREQ)
1744	u_long ocmd = cmd;
1745#endif
1746	short oif_flags;
1747#ifdef COMPAT_OIFREQ
1748	struct ifreq ifrb;
1749	struct oifreq *oifr = NULL;
1750#endif
1751
1752	switch (cmd) {
1753#ifdef COMPAT_OIFREQ
1754	case OSIOCGIFCONF:
1755	case OOSIOCGIFCONF:
1756		return compat_ifconf(cmd, data);
1757#endif
1758#ifdef COMPAT_OIFDATA
1759	case OSIOCGIFDATA:
1760	case OSIOCZIFDATA:
1761		return compat_ifdatareq(l, cmd, data);
1762#endif
1763	case SIOCGIFCONF:
1764		return ifconf(cmd, data);
1765	case SIOCINITIFADDR:
1766		return EPERM;
1767	}
1768
1769#ifdef COMPAT_OIFREQ
1770	cmd = compat_cvtcmd(cmd);
1771	if (cmd != ocmd) {
1772		oifr = data;
1773		data = ifr = &ifrb;
1774		ifreqo2n(oifr, ifr);
1775	} else
1776#endif
1777		ifr = data;
1778
1779	ifp = ifunit(ifr->ifr_name);
1780
1781	switch (cmd) {
1782	case SIOCIFCREATE:
1783	case SIOCIFDESTROY:
1784		if (l != NULL) {
1785			error = kauth_authorize_network(l->l_cred,
1786			    KAUTH_NETWORK_INTERFACE,
1787			    KAUTH_REQ_NETWORK_INTERFACE_SETPRIV, ifp,
1788			    (void *)cmd, NULL);
1789			if (error != 0)
1790				return error;
1791		}
1792		return (cmd == SIOCIFCREATE) ?
1793			if_clone_create(ifr->ifr_name) :
1794			if_clone_destroy(ifr->ifr_name);
1795
1796	case SIOCIFGCLONERS:
1797		return if_clone_list((struct if_clonereq *)data);
1798	}
1799
1800	if (ifp == NULL)
1801		return ENXIO;
1802
1803	switch (cmd) {
1804	case SIOCALIFADDR:
1805	case SIOCDLIFADDR:
1806	case SIOCSIFADDRPREF:
1807	case SIOCSIFFLAGS:
1808	case SIOCSIFCAP:
1809	case SIOCSIFMETRIC:
1810	case SIOCZIFDATA:
1811	case SIOCSIFMTU:
1812	case SIOCSIFPHYADDR:
1813	case SIOCDIFPHYADDR:
1814#ifdef INET6
1815	case SIOCSIFPHYADDR_IN6:
1816#endif
1817	case SIOCSLIFPHYADDR:
1818	case SIOCADDMULTI:
1819	case SIOCDELMULTI:
1820	case SIOCSIFMEDIA:
1821	case SIOCSDRVSPEC:
1822	case SIOCG80211:
1823	case SIOCS80211:
1824	case SIOCS80211NWID:
1825	case SIOCS80211NWKEY:
1826	case SIOCS80211POWER:
1827	case SIOCS80211BSSID:
1828	case SIOCS80211CHANNEL:
1829	case SIOCSLINKSTR:
1830		if (l != NULL) {
1831			error = kauth_authorize_network(l->l_cred,
1832			    KAUTH_NETWORK_INTERFACE,
1833			    KAUTH_REQ_NETWORK_INTERFACE_SETPRIV, ifp,
1834			    (void *)cmd, NULL);
1835			if (error != 0)
1836				return error;
1837		}
1838	}
1839
1840	oif_flags = ifp->if_flags;
1841
1842	ifnet_lock_enter(ifp->if_ioctl_lock);
1843	error = (*ifp->if_ioctl)(ifp, cmd, data);
1844	if (error != ENOTTY)
1845		;
1846	else if (so->so_proto == NULL)
1847		error = EOPNOTSUPP;
1848	else {
1849#ifdef COMPAT_OSOCK
1850		error = compat_ifioctl(so, ocmd, cmd, data, l);
1851#else
1852		error = (*so->so_proto->pr_usrreq)(so, PRU_CONTROL,
1853		    (struct mbuf *)cmd, (struct mbuf *)data,
1854		    (struct mbuf *)ifp, l);
1855#endif
1856	}
1857
1858	if (((oif_flags ^ ifp->if_flags) & IFF_UP) != 0) {
1859#ifdef INET6
1860		if ((ifp->if_flags & IFF_UP) != 0) {
1861			int s = splnet();
1862			in6_if_up(ifp);
1863			splx(s);
1864		}
1865#endif
1866	}
1867#ifdef COMPAT_OIFREQ
1868	if (cmd != ocmd)
1869		ifreqn2o(oifr, ifr);
1870#endif
1871
1872	ifnet_lock_exit(ifp->if_ioctl_lock);
1873	return error;
1874}
1875
1876/* This callback adds to the sum in `arg' the number of
1877 * threads on `ci' who have entered or who wait to enter the
1878 * critical section.
1879 */
1880static void
1881ifnet_lock_sum(void *p, void *arg, struct cpu_info *ci)
1882{
1883	uint64_t *sum = arg, *nenter = p;
1884
1885	*sum += *nenter;
1886}
1887
1888/* Return the number of threads who have entered or who wait
1889 * to enter the critical section on all CPUs.
1890 */
1891static uint64_t
1892ifnet_lock_entrances(struct ifnet_lock *il)
1893{
1894	uint64_t sum = 0;
1895
1896	percpu_foreach(il->il_nenter, ifnet_lock_sum, &sum);
1897
1898	return sum;
1899}
1900
1901static int
1902ifioctl_attach(struct ifnet *ifp)
1903{
1904	struct ifnet_lock *il;
1905
1906	/* If the driver has not supplied its own if_ioctl, then
1907	 * supply the default.
1908	 */
1909	if (ifp->if_ioctl == NULL)
1910		ifp->if_ioctl = ifioctl_common;
1911
1912	/* Create an ifnet_lock for synchronizing ifioctls. */
1913	if ((il = kmem_zalloc(sizeof(*il), KM_SLEEP)) == NULL)
1914		return ENOMEM;
1915
1916	il->il_nenter = percpu_alloc(sizeof(uint64_t));
1917	if (il->il_nenter == NULL) {
1918		kmem_free(il, sizeof(*il));
1919		return ENOMEM;
1920	}
1921
1922	mutex_init(&il->il_lock, MUTEX_DEFAULT, IPL_NONE);
1923	cv_init(&il->il_emptied, ifp->if_xname);
1924
1925	ifp->if_ioctl_lock = il;
1926
1927	return 0;
1928}
1929
1930/*
1931 * This must not be called until after `ifp' has been withdrawn from the
1932 * ifnet tables so that ifioctl() cannot get a handle on it by calling
1933 * ifunit().
1934 */
1935static void
1936ifioctl_detach(struct ifnet *ifp)
1937{
1938	struct ifnet_lock *il;
1939
1940	il = ifp->if_ioctl_lock;
1941	mutex_enter(&il->il_lock);
1942	/* Install if_nullioctl to make sure that any thread that
1943	 * subsequently enters the critical section will quit it
1944	 * immediately and signal the condition variable that we
1945	 * wait on, below.
1946	 */
1947	ifp->if_ioctl = if_nullioctl;
1948	/* Sleep while threads are still in the critical section or
1949	 * wait to enter it.
1950	 */
1951	while (ifnet_lock_entrances(il) != il->il_nexit)
1952		cv_wait(&il->il_emptied, &il->il_lock);
1953	/* At this point, we are the only thread still in the critical
1954	 * section, and no new thread can get a handle on the ifioctl
1955	 * lock, so it is safe to free its memory.
1956	 */
1957	mutex_exit(&il->il_lock);
1958	ifp->if_ioctl_lock = NULL;
1959	percpu_free(il->il_nenter, sizeof(uint64_t));
1960	il->il_nenter = NULL;
1961	cv_destroy(&il->il_emptied);
1962	mutex_destroy(&il->il_lock);
1963	kmem_free(il, sizeof(*il));
1964}
1965
1966/*
1967 * Return interface configuration
1968 * of system.  List may be used
1969 * in later ioctl's (above) to get
1970 * other information.
1971 *
1972 * Each record is a struct ifreq.  Before the addition of
1973 * sockaddr_storage, the API rule was that sockaddr flavors that did
1974 * not fit would extend beyond the struct ifreq, with the next struct
1975 * ifreq starting sa_len beyond the struct sockaddr.  Because the
1976 * union in struct ifreq includes struct sockaddr_storage, every kind
1977 * of sockaddr must fit.  Thus, there are no longer any overlength
1978 * records.
1979 *
1980 * Records are added to the user buffer if they fit, and ifc_len is
1981 * adjusted to the length that was written.  Thus, the user is only
1982 * assured of getting the complete list if ifc_len on return is at
1983 * least sizeof(struct ifreq) less than it was on entry.
1984 *
1985 * If the user buffer pointer is NULL, this routine copies no data and
1986 * returns the amount of space that would be needed.
1987 *
1988 * Invariants:
1989 * ifrp points to the next part of the user's buffer to be used.  If
1990 * ifrp != NULL, space holds the number of bytes remaining that we may
1991 * write at ifrp.  Otherwise, space holds the number of bytes that
1992 * would have been written had there been adequate space.
1993 */
1994/*ARGSUSED*/
1995int
1996ifconf(u_long cmd, void *data)
1997{
1998	struct ifconf *ifc = (struct ifconf *)data;
1999	struct ifnet *ifp;
2000	struct ifaddr *ifa;
2001	struct ifreq ifr, *ifrp;
2002	int space, error = 0;
2003	const int sz = (int)sizeof(struct ifreq);
2004
2005	if ((ifrp = ifc->ifc_req) == NULL)
2006		space = 0;
2007	else
2008		space = ifc->ifc_len;
2009	IFNET_FOREACH(ifp) {
2010		(void)strncpy(ifr.ifr_name, ifp->if_xname,
2011		    sizeof(ifr.ifr_name));
2012		if (ifr.ifr_name[sizeof(ifr.ifr_name) - 1] != '\0')
2013			return ENAMETOOLONG;
2014		if (IFADDR_EMPTY(ifp)) {
2015			/* Interface with no addresses - send zero sockaddr. */
2016			memset(&ifr.ifr_addr, 0, sizeof(ifr.ifr_addr));
2017			if (ifrp == NULL) {
2018				space += sz;
2019				continue;
2020			}
2021			if (space >= sz) {
2022				error = copyout(&ifr, ifrp, sz);
2023				if (error != 0)
2024					return error;
2025				ifrp++;
2026				space -= sz;
2027			}
2028		}
2029
2030		IFADDR_FOREACH(ifa, ifp) {
2031			struct sockaddr *sa = ifa->ifa_addr;
2032			/* all sockaddrs must fit in sockaddr_storage */
2033			KASSERT(sa->sa_len <= sizeof(ifr.ifr_ifru));
2034
2035			if (ifrp == NULL) {
2036				space += sz;
2037				continue;
2038			}
2039			memcpy(&ifr.ifr_space, sa, sa->sa_len);
2040			if (space >= sz) {
2041				error = copyout(&ifr, ifrp, sz);
2042				if (error != 0)
2043					return (error);
2044				ifrp++; space -= sz;
2045			}
2046		}
2047	}
2048	if (ifrp != NULL) {
2049		KASSERT(0 <= space && space <= ifc->ifc_len);
2050		ifc->ifc_len -= space;
2051	} else {
2052		KASSERT(space >= 0);
2053		ifc->ifc_len = space;
2054	}
2055	return (0);
2056}
2057
2058int
2059ifreq_setaddr(u_long cmd, struct ifreq *ifr, const struct sockaddr *sa)
2060{
2061	uint8_t len;
2062#ifdef COMPAT_OIFREQ
2063	struct ifreq ifrb;
2064	struct oifreq *oifr = NULL;
2065	u_long ocmd = cmd;
2066	cmd = compat_cvtcmd(cmd);
2067	if (cmd != ocmd) {
2068		oifr = (struct oifreq *)(void *)ifr;
2069		ifr = &ifrb;
2070		ifreqo2n(oifr, ifr);
2071		len = sizeof(oifr->ifr_addr);
2072	} else
2073#endif
2074		len = sizeof(ifr->ifr_ifru.ifru_space);
2075
2076	if (len < sa->sa_len)
2077		return EFBIG;
2078
2079	memset(&ifr->ifr_addr, 0, len);
2080	sockaddr_copy(&ifr->ifr_addr, len, sa);
2081
2082#ifdef COMPAT_OIFREQ
2083	if (cmd != ocmd)
2084		ifreqn2o(oifr, ifr);
2085#endif
2086	return 0;
2087}
2088
2089/*
2090 * Queue message on interface, and start output if interface
2091 * not yet active.
2092 */
2093int
2094ifq_enqueue(struct ifnet *ifp, struct mbuf *m
2095    ALTQ_COMMA ALTQ_DECL(struct altq_pktattr *pktattr))
2096{
2097	int len = m->m_pkthdr.len;
2098	int mflags = m->m_flags;
2099	int s = splnet();
2100	int error;
2101
2102	IFQ_ENQUEUE(&ifp->if_snd, m, pktattr, error);
2103	if (error != 0)
2104		goto out;
2105	ifp->if_obytes += len;
2106	if (mflags & M_MCAST)
2107		ifp->if_omcasts++;
2108	if ((ifp->if_flags & IFF_OACTIVE) == 0)
2109		(*ifp->if_start)(ifp);
2110out:
2111	splx(s);
2112	return error;
2113}
2114
2115/*
2116 * Queue message on interface, possibly using a second fast queue
2117 */
2118int
2119ifq_enqueue2(struct ifnet *ifp, struct ifqueue *ifq, struct mbuf *m
2120    ALTQ_COMMA ALTQ_DECL(struct altq_pktattr *pktattr))
2121{
2122	int error = 0;
2123
2124	if (ifq != NULL
2125#ifdef ALTQ
2126	    && ALTQ_IS_ENABLED(&ifp->if_snd) == 0
2127#endif
2128	    ) {
2129		if (IF_QFULL(ifq)) {
2130			IF_DROP(&ifp->if_snd);
2131			m_freem(m);
2132			if (error == 0)
2133				error = ENOBUFS;
2134		} else
2135			IF_ENQUEUE(ifq, m);
2136	} else
2137		IFQ_ENQUEUE(&ifp->if_snd, m, pktattr, error);
2138	if (error != 0) {
2139		++ifp->if_oerrors;
2140		return error;
2141	}
2142	return 0;
2143}
2144
2145int
2146if_addr_init(ifnet_t *ifp, struct ifaddr *ifa, const bool src)
2147{
2148	int rc;
2149
2150	if (ifp->if_initaddr != NULL)
2151		rc = (*ifp->if_initaddr)(ifp, ifa, src);
2152	else if (src ||
2153	         (rc = (*ifp->if_ioctl)(ifp, SIOCSIFDSTADDR, ifa)) == ENOTTY)
2154		rc = (*ifp->if_ioctl)(ifp, SIOCINITIFADDR, ifa);
2155
2156	return rc;
2157}
2158
2159int
2160if_flags_set(ifnet_t *ifp, const short flags)
2161{
2162	int rc;
2163
2164	if (ifp->if_setflags != NULL)
2165		rc = (*ifp->if_setflags)(ifp, flags);
2166	else {
2167		short cantflags, chgdflags;
2168		struct ifreq ifr;
2169
2170		chgdflags = ifp->if_flags ^ flags;
2171		cantflags = chgdflags & IFF_CANTCHANGE;
2172
2173		if (cantflags != 0)
2174			ifp->if_flags ^= cantflags;
2175
2176                /* Traditionally, we do not call if_ioctl after
2177                 * setting/clearing only IFF_PROMISC if the interface
2178                 * isn't IFF_UP.  Uphold that tradition.
2179		 */
2180		if (chgdflags == IFF_PROMISC && (ifp->if_flags & IFF_UP) == 0)
2181			return 0;
2182
2183		memset(&ifr, 0, sizeof(ifr));
2184
2185		ifr.ifr_flags = flags & ~IFF_CANTCHANGE;
2186		rc = (*ifp->if_ioctl)(ifp, SIOCSIFFLAGS, &ifr);
2187
2188		if (rc != 0 && cantflags != 0)
2189			ifp->if_flags ^= cantflags;
2190	}
2191
2192	return rc;
2193}
2194
2195int
2196if_mcast_op(ifnet_t *ifp, const unsigned long cmd, const struct sockaddr *sa)
2197{
2198	int rc;
2199	struct ifreq ifr;
2200
2201	if (ifp->if_mcastop != NULL)
2202		rc = (*ifp->if_mcastop)(ifp, cmd, sa);
2203	else {
2204		ifreq_setaddr(cmd, &ifr, sa);
2205		rc = (*ifp->if_ioctl)(ifp, cmd, &ifr);
2206	}
2207
2208	return rc;
2209}
2210
2211static void
2212sysctl_sndq_setup(struct sysctllog **clog, const char *ifname,
2213    struct ifaltq *ifq)
2214{
2215	const struct sysctlnode *cnode, *rnode;
2216
2217	if (sysctl_createv(clog, 0, NULL, &rnode,
2218		       CTLFLAG_PERMANENT,
2219		       CTLTYPE_NODE, "net", NULL,
2220		       NULL, 0, NULL, 0,
2221		       CTL_NET, CTL_EOL) != 0)
2222		goto bad;
2223
2224	if (sysctl_createv(clog, 0, &rnode, &rnode,
2225		       CTLFLAG_PERMANENT,
2226		       CTLTYPE_NODE, "interfaces",
2227		       SYSCTL_DESCR("Per-interface controls"),
2228		       NULL, 0, NULL, 0,
2229		       CTL_CREATE, CTL_EOL) != 0)
2230		goto bad;
2231
2232	if (sysctl_createv(clog, 0, &rnode, &rnode,
2233		       CTLFLAG_PERMANENT,
2234		       CTLTYPE_NODE, ifname,
2235		       SYSCTL_DESCR("Interface controls"),
2236		       NULL, 0, NULL, 0,
2237		       CTL_CREATE, CTL_EOL) != 0)
2238		goto bad;
2239
2240	if (sysctl_createv(clog, 0, &rnode, &rnode,
2241		       CTLFLAG_PERMANENT,
2242		       CTLTYPE_NODE, "sndq",
2243		       SYSCTL_DESCR("Interface output queue controls"),
2244		       NULL, 0, NULL, 0,
2245		       CTL_CREATE, CTL_EOL) != 0)
2246		goto bad;
2247
2248	if (sysctl_createv(clog, 0, &rnode, &cnode,
2249		       CTLFLAG_PERMANENT,
2250		       CTLTYPE_INT, "len",
2251		       SYSCTL_DESCR("Current output queue length"),
2252		       NULL, 0, &ifq->ifq_len, 0,
2253		       CTL_CREATE, CTL_EOL) != 0)
2254		goto bad;
2255
2256	if (sysctl_createv(clog, 0, &rnode, &cnode,
2257		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
2258		       CTLTYPE_INT, "maxlen",
2259		       SYSCTL_DESCR("Maximum allowed output queue length"),
2260		       NULL, 0, &ifq->ifq_maxlen, 0,
2261		       CTL_CREATE, CTL_EOL) != 0)
2262		goto bad;
2263
2264	if (sysctl_createv(clog, 0, &rnode, &cnode,
2265		       CTLFLAG_PERMANENT,
2266		       CTLTYPE_INT, "drops",
2267		       SYSCTL_DESCR("Packets dropped due to full output queue"),
2268		       NULL, 0, &ifq->ifq_drops, 0,
2269		       CTL_CREATE, CTL_EOL) != 0)
2270		goto bad;
2271
2272	return;
2273bad:
2274	printf("%s: could not attach sysctl nodes\n", ifname);
2275	return;
2276}
2277
2278#if defined(INET) || defined(INET6)
2279static void
2280sysctl_net_ifq_setup(struct sysctllog **clog,
2281		     int pf, const char *pfname,
2282		     int ipn, const char *ipname,
2283		     int qid, struct ifqueue *ifq)
2284{
2285
2286	sysctl_createv(clog, 0, NULL, NULL,
2287		       CTLFLAG_PERMANENT,
2288		       CTLTYPE_NODE, "net", NULL,
2289		       NULL, 0, NULL, 0,
2290		       CTL_NET, CTL_EOL);
2291	sysctl_createv(clog, 0, NULL, NULL,
2292		       CTLFLAG_PERMANENT,
2293		       CTLTYPE_NODE, pfname, NULL,
2294		       NULL, 0, NULL, 0,
2295		       CTL_NET, pf, CTL_EOL);
2296	sysctl_createv(clog, 0, NULL, NULL,
2297		       CTLFLAG_PERMANENT,
2298		       CTLTYPE_NODE, ipname, NULL,
2299		       NULL, 0, NULL, 0,
2300		       CTL_NET, pf, ipn, CTL_EOL);
2301	sysctl_createv(clog, 0, NULL, NULL,
2302		       CTLFLAG_PERMANENT,
2303		       CTLTYPE_NODE, "ifq",
2304		       SYSCTL_DESCR("Protocol input queue controls"),
2305		       NULL, 0, NULL, 0,
2306		       CTL_NET, pf, ipn, qid, CTL_EOL);
2307
2308	sysctl_createv(clog, 0, NULL, NULL,
2309		       CTLFLAG_PERMANENT,
2310		       CTLTYPE_INT, "len",
2311		       SYSCTL_DESCR("Current input queue length"),
2312		       NULL, 0, &ifq->ifq_len, 0,
2313		       CTL_NET, pf, ipn, qid, IFQCTL_LEN, CTL_EOL);
2314	sysctl_createv(clog, 0, NULL, NULL,
2315		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
2316		       CTLTYPE_INT, "maxlen",
2317		       SYSCTL_DESCR("Maximum allowed input queue length"),
2318		       NULL, 0, &ifq->ifq_maxlen, 0,
2319		       CTL_NET, pf, ipn, qid, IFQCTL_MAXLEN, CTL_EOL);
2320#ifdef notyet
2321	sysctl_createv(clog, 0, NULL, NULL,
2322		       CTLFLAG_PERMANENT,
2323		       CTLTYPE_INT, "peak",
2324		       SYSCTL_DESCR("Highest input queue length"),
2325		       NULL, 0, &ifq->ifq_peak, 0,
2326		       CTL_NET, pf, ipn, qid, IFQCTL_PEAK, CTL_EOL);
2327#endif
2328	sysctl_createv(clog, 0, NULL, NULL,
2329		       CTLFLAG_PERMANENT,
2330		       CTLTYPE_INT, "drops",
2331		       SYSCTL_DESCR("Packets dropped due to full input queue"),
2332		       NULL, 0, &ifq->ifq_drops, 0,
2333		       CTL_NET, pf, ipn, qid, IFQCTL_DROPS, CTL_EOL);
2334}
2335#endif /* INET || INET6 */
2336