xref: /openbsd-src/sys/net/if.c (revision 25c4e8bd056e974b28f4a0ffd39d76c190a56013)
1 /*	$OpenBSD: if.c,v 1.659 2022/07/14 11:03:15 mvs Exp $	*/
2 /*	$NetBSD: if.c,v 1.35 1996/05/07 05:26:04 thorpej Exp $	*/
3 
4 /*
5  * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  * 3. Neither the name of the project nor the names of its contributors
17  *    may be used to endorse or promote products derived from this software
18  *    without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
21  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23  * ARE DISCLAIMED.  IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
24  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30  * SUCH DAMAGE.
31  */
32 
33 /*
34  * Copyright (c) 1980, 1986, 1993
35  *	The Regents of the University of California.  All rights reserved.
36  *
37  * Redistribution and use in source and binary forms, with or without
38  * modification, are permitted provided that the following conditions
39  * are met:
40  * 1. Redistributions of source code must retain the above copyright
41  *    notice, this list of conditions and the following disclaimer.
42  * 2. Redistributions in binary form must reproduce the above copyright
43  *    notice, this list of conditions and the following disclaimer in the
44  *    documentation and/or other materials provided with the distribution.
45  * 3. Neither the name of the University nor the names of its contributors
46  *    may be used to endorse or promote products derived from this software
47  *    without specific prior written permission.
48  *
49  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
50  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
51  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
52  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
53  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
54  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
55  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
56  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
57  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
58  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
59  * SUCH DAMAGE.
60  *
61  *	@(#)if.c	8.3 (Berkeley) 1/4/94
62  */
63 
64 #include "bpfilter.h"
65 #include "bridge.h"
66 #include "carp.h"
67 #include "ether.h"
68 #include "pf.h"
69 #include "pfsync.h"
70 #include "ppp.h"
71 #include "pppoe.h"
72 #include "if_wg.h"
73 
74 #include <sys/param.h>
75 #include <sys/systm.h>
76 #include <sys/mbuf.h>
77 #include <sys/socket.h>
78 #include <sys/socketvar.h>
79 #include <sys/timeout.h>
80 #include <sys/protosw.h>
81 #include <sys/kernel.h>
82 #include <sys/ioctl.h>
83 #include <sys/domain.h>
84 #include <sys/task.h>
85 #include <sys/atomic.h>
86 #include <sys/percpu.h>
87 #include <sys/proc.h>
88 #include <sys/stdint.h>	/* uintptr_t */
89 #include <sys/rwlock.h>
90 
91 #include <net/if.h>
92 #include <net/if_dl.h>
93 #include <net/if_types.h>
94 #include <net/route.h>
95 #include <net/netisr.h>
96 
97 #include <netinet/in.h>
98 #include <netinet/if_ether.h>
99 #include <netinet/igmp.h>
100 #ifdef MROUTING
101 #include <netinet/ip_mroute.h>
102 #endif
103 
104 #ifdef INET6
105 #include <netinet6/in6_var.h>
106 #include <netinet6/in6_ifattach.h>
107 #include <netinet6/nd6.h>
108 #include <netinet/ip6.h>
109 #include <netinet6/ip6_var.h>
110 #endif
111 
112 #ifdef MPLS
113 #include <netmpls/mpls.h>
114 #endif
115 
116 #if NBPFILTER > 0
117 #include <net/bpf.h>
118 #endif
119 
120 #if NBRIDGE > 0
121 #include <net/if_bridge.h>
122 #endif
123 
124 #if NCARP > 0
125 #include <netinet/ip_carp.h>
126 #endif
127 
128 #if NPF > 0
129 #include <net/pfvar.h>
130 #endif
131 
132 #include <sys/device.h>
133 
134 void	if_attachsetup(struct ifnet *);
135 void	if_attachdomain(struct ifnet *);
136 void	if_attach_common(struct ifnet *);
137 void	if_remove(struct ifnet *);
138 int	if_createrdomain(int, struct ifnet *);
139 int	if_setrdomain(struct ifnet *, int);
140 void	if_slowtimo(void *);
141 
142 void	if_detached_qstart(struct ifqueue *);
143 int	if_detached_ioctl(struct ifnet *, u_long, caddr_t);
144 
145 int	ifioctl_get(u_long, caddr_t);
146 int	ifconf(caddr_t);
147 static int
148 	if_sffpage_check(const caddr_t);
149 
150 int	if_getgroup(caddr_t, struct ifnet *);
151 int	if_getgroupmembers(caddr_t);
152 int	if_getgroupattribs(caddr_t);
153 int	if_setgroupattribs(caddr_t);
154 int	if_getgrouplist(caddr_t);
155 
156 void	if_linkstate(struct ifnet *);
157 void	if_linkstate_task(void *);
158 
159 int	if_clone_list(struct if_clonereq *);
160 struct if_clone	*if_clone_lookup(const char *, int *);
161 
162 int	if_group_egress_build(void);
163 
164 void	if_watchdog_task(void *);
165 
166 void	if_netisr(void *);
167 
168 #ifdef DDB
169 void	ifa_print_all(void);
170 #endif
171 
172 void	if_qstart_compat(struct ifqueue *);
173 
174 /*
175  * interface index map
176  *
177  * the kernel maintains a mapping of interface indexes to struct ifnet
178  * pointers.
179  *
180  * the map is an array of struct ifnet pointers prefixed by an if_map
181  * structure. the if_map structure stores the length of its array.
182  *
183  * as interfaces are attached to the system, the map is grown on demand
184  * up to USHRT_MAX entries.
185  *
186  * interface index 0 is reserved and represents no interface. this
187  * supports the use of the interface index as the scope for IPv6 link
188  * local addresses, where scope 0 means no scope has been specified.
189  * it also supports the use of interface index as the unique identifier
190  * for network interfaces in SNMP applications as per RFC2863. therefore
191  * if_get(0) returns NULL.
192  */
193 
194 void if_ifp_dtor(void *, void *);
195 void if_map_dtor(void *, void *);
196 struct ifnet *if_ref(struct ifnet *);
197 
198 /*
199  * struct if_map
200  *
201  * bounded array of ifnet srp pointers used to fetch references of live
202  * interfaces with if_get().
203  */
204 
205 struct if_map {
206 	unsigned long		 limit;
207 	/* followed by limit ifnet srp pointers */
208 };
209 
210 /*
211  * struct if_idxmap
212  *
213  * infrastructure to manage updates and accesses to the current if_map.
214  */
215 
216 struct if_idxmap {
217 	unsigned int		 serial;
218 	unsigned int		 count;
219 	struct srp		 map;
220 };
221 
222 void	if_idxmap_init(unsigned int);
223 void	if_idxmap_insert(struct ifnet *);
224 void	if_idxmap_remove(struct ifnet *);
225 
226 TAILQ_HEAD(, ifg_group) ifg_head = TAILQ_HEAD_INITIALIZER(ifg_head);
227 
228 LIST_HEAD(, if_clone) if_cloners = LIST_HEAD_INITIALIZER(if_cloners);
229 int if_cloners_count;
230 
231 struct rwlock if_cloners_lock = RWLOCK_INITIALIZER("clonelk");
232 
233 /* hooks should only be added, deleted, and run from a process context */
234 struct mutex if_hooks_mtx = MUTEX_INITIALIZER(IPL_NONE);
235 void	if_hooks_run(struct task_list *);
236 
237 int	ifq_congestion;
238 
239 int		 netisr;
240 
241 #define	NET_TASKQ	4
242 struct taskq	*nettqmp[NET_TASKQ];
243 
244 struct task if_input_task_locked = TASK_INITIALIZER(if_netisr, NULL);
245 
246 /*
247  * Serialize socket operations to ensure no new sleeping points
248  * are introduced in IP output paths.
249  */
250 struct rwlock netlock = RWLOCK_INITIALIZER("netlock");
251 
252 /*
253  * Network interface utility routines.
254  */
255 void
256 ifinit(void)
257 {
258 	unsigned int	i;
259 
260 	/*
261 	 * most machines boot with 4 or 5 interfaces, so size the initial map
262 	 * to accommodate this
263 	 */
264 	if_idxmap_init(8);
265 
266 	for (i = 0; i < NET_TASKQ; i++) {
267 		nettqmp[i] = taskq_create("softnet", 1, IPL_NET, TASKQ_MPSAFE);
268 		if (nettqmp[i] == NULL)
269 			panic("unable to create network taskq %d", i);
270 	}
271 }
272 
273 static struct if_idxmap if_idxmap = {
274 	0,
275 	0,
276 	SRP_INITIALIZER()
277 };
278 
279 struct srp_gc if_ifp_gc = SRP_GC_INITIALIZER(if_ifp_dtor, NULL);
280 struct srp_gc if_map_gc = SRP_GC_INITIALIZER(if_map_dtor, NULL);
281 
282 struct ifnet_head ifnet = TAILQ_HEAD_INITIALIZER(ifnet);
283 
284 void
285 if_idxmap_init(unsigned int limit)
286 {
287 	struct if_map *if_map;
288 	struct srp *map;
289 	unsigned int i;
290 
291 	if_idxmap.serial = 1; /* skip ifidx 0 so it can return NULL */
292 
293 	if_map = malloc(sizeof(*if_map) + limit * sizeof(*map),
294 	    M_IFADDR, M_WAITOK);
295 
296 	if_map->limit = limit;
297 	map = (struct srp *)(if_map + 1);
298 	for (i = 0; i < limit; i++)
299 		srp_init(&map[i]);
300 
301 	/* this is called early so there's nothing to race with */
302 	srp_update_locked(&if_map_gc, &if_idxmap.map, if_map);
303 }
304 
305 void
306 if_idxmap_insert(struct ifnet *ifp)
307 {
308 	struct if_map *if_map;
309 	struct srp *map;
310 	unsigned int index, i;
311 
312 	refcnt_init(&ifp->if_refcnt);
313 
314 	/* the kernel lock guarantees serialised modifications to if_idxmap */
315 	KERNEL_ASSERT_LOCKED();
316 
317 	if (++if_idxmap.count > USHRT_MAX)
318 		panic("too many interfaces");
319 
320 	if_map = srp_get_locked(&if_idxmap.map);
321 	map = (struct srp *)(if_map + 1);
322 
323 	index = if_idxmap.serial++ & USHRT_MAX;
324 
325 	if (index >= if_map->limit) {
326 		struct if_map *nif_map;
327 		struct srp *nmap;
328 		unsigned int nlimit;
329 		struct ifnet *nifp;
330 
331 		nlimit = if_map->limit * 2;
332 		nif_map = malloc(sizeof(*nif_map) + nlimit * sizeof(*nmap),
333 		    M_IFADDR, M_WAITOK);
334 		nmap = (struct srp *)(nif_map + 1);
335 
336 		nif_map->limit = nlimit;
337 		for (i = 0; i < if_map->limit; i++) {
338 			srp_init(&nmap[i]);
339 			nifp = srp_get_locked(&map[i]);
340 			if (nifp != NULL) {
341 				srp_update_locked(&if_ifp_gc, &nmap[i],
342 				    if_ref(nifp));
343 			}
344 		}
345 
346 		while (i < nlimit) {
347 			srp_init(&nmap[i]);
348 			i++;
349 		}
350 
351 		srp_update_locked(&if_map_gc, &if_idxmap.map, nif_map);
352 		if_map = nif_map;
353 		map = nmap;
354 	}
355 
356 	/* pick the next free index */
357 	for (i = 0; i < USHRT_MAX; i++) {
358 		if (index != 0 && srp_get_locked(&map[index]) == NULL)
359 			break;
360 
361 		index = if_idxmap.serial++ & USHRT_MAX;
362 	}
363 
364 	/* commit */
365 	ifp->if_index = index;
366 	srp_update_locked(&if_ifp_gc, &map[index], if_ref(ifp));
367 }
368 
369 void
370 if_idxmap_remove(struct ifnet *ifp)
371 {
372 	struct if_map *if_map;
373 	struct srp *map;
374 	unsigned int index;
375 
376 	index = ifp->if_index;
377 
378 	/* the kernel lock guarantees serialised modifications to if_idxmap */
379 	KERNEL_ASSERT_LOCKED();
380 
381 	if_map = srp_get_locked(&if_idxmap.map);
382 	KASSERT(index < if_map->limit);
383 
384 	map = (struct srp *)(if_map + 1);
385 	KASSERT(ifp == (struct ifnet *)srp_get_locked(&map[index]));
386 
387 	srp_update_locked(&if_ifp_gc, &map[index], NULL);
388 	if_idxmap.count--;
389 	/* end of if_idxmap modifications */
390 }
391 
392 void
393 if_ifp_dtor(void *null, void *ifp)
394 {
395 	if_put(ifp);
396 }
397 
398 void
399 if_map_dtor(void *null, void *m)
400 {
401 	struct if_map *if_map = m;
402 	struct srp *map = (struct srp *)(if_map + 1);
403 	unsigned int i;
404 
405 	/*
406 	 * dont need to serialize the use of update_locked since this is
407 	 * the last reference to this map. there's nothing to race against.
408 	 */
409 	for (i = 0; i < if_map->limit; i++)
410 		srp_update_locked(&if_ifp_gc, &map[i], NULL);
411 
412 	free(if_map, M_IFADDR, sizeof(*if_map) + if_map->limit * sizeof(*map));
413 }
414 
415 /*
416  * Attach an interface to the
417  * list of "active" interfaces.
418  */
419 void
420 if_attachsetup(struct ifnet *ifp)
421 {
422 	unsigned long ifidx;
423 
424 	NET_ASSERT_LOCKED();
425 
426 	if_addgroup(ifp, IFG_ALL);
427 
428 	if_attachdomain(ifp);
429 #if NPF > 0
430 	pfi_attach_ifnet(ifp);
431 #endif
432 
433 	timeout_set(&ifp->if_slowtimo, if_slowtimo, ifp);
434 	if_slowtimo(ifp);
435 
436 	if_idxmap_insert(ifp);
437 	KASSERT(if_get(0) == NULL);
438 
439 	ifidx = ifp->if_index;
440 
441 	task_set(&ifp->if_watchdogtask, if_watchdog_task, (void *)ifidx);
442 	task_set(&ifp->if_linkstatetask, if_linkstate_task, (void *)ifidx);
443 
444 	/* Announce the interface. */
445 	rtm_ifannounce(ifp, IFAN_ARRIVAL);
446 }
447 
448 /*
449  * Allocate the link level name for the specified interface.  This
450  * is an attachment helper.  It must be called after ifp->if_addrlen
451  * is initialized, which may not be the case when if_attach() is
452  * called.
453  */
454 void
455 if_alloc_sadl(struct ifnet *ifp)
456 {
457 	unsigned int socksize;
458 	int namelen, masklen;
459 	struct sockaddr_dl *sdl;
460 
461 	/*
462 	 * If the interface already has a link name, release it
463 	 * now.  This is useful for interfaces that can change
464 	 * link types, and thus switch link names often.
465 	 */
466 	if_free_sadl(ifp);
467 
468 	namelen = strlen(ifp->if_xname);
469 	masklen = offsetof(struct sockaddr_dl, sdl_data[0]) + namelen;
470 	socksize = masklen + ifp->if_addrlen;
471 #define ROUNDUP(a) (1 + (((a) - 1) | (sizeof(long) - 1)))
472 	if (socksize < sizeof(*sdl))
473 		socksize = sizeof(*sdl);
474 	socksize = ROUNDUP(socksize);
475 	sdl = malloc(socksize, M_IFADDR, M_WAITOK|M_ZERO);
476 	sdl->sdl_len = socksize;
477 	sdl->sdl_family = AF_LINK;
478 	bcopy(ifp->if_xname, sdl->sdl_data, namelen);
479 	sdl->sdl_nlen = namelen;
480 	sdl->sdl_alen = ifp->if_addrlen;
481 	sdl->sdl_index = ifp->if_index;
482 	sdl->sdl_type = ifp->if_type;
483 	ifp->if_sadl = sdl;
484 }
485 
486 /*
487  * Free the link level name for the specified interface.  This is
488  * a detach helper.  This is called from if_detach() or from
489  * link layer type specific detach functions.
490  */
491 void
492 if_free_sadl(struct ifnet *ifp)
493 {
494 	if (ifp->if_sadl == NULL)
495 		return;
496 
497 	free(ifp->if_sadl, M_IFADDR, ifp->if_sadl->sdl_len);
498 	ifp->if_sadl = NULL;
499 }
500 
501 void
502 if_attachdomain(struct ifnet *ifp)
503 {
504 	const struct domain *dp;
505 	int i, s;
506 
507 	s = splnet();
508 
509 	/* address family dependent data region */
510 	bzero(ifp->if_afdata, sizeof(ifp->if_afdata));
511 	for (i = 0; (dp = domains[i]) != NULL; i++) {
512 		if (dp->dom_ifattach)
513 			ifp->if_afdata[dp->dom_family] =
514 			    (*dp->dom_ifattach)(ifp);
515 	}
516 
517 	splx(s);
518 }
519 
520 void
521 if_attachhead(struct ifnet *ifp)
522 {
523 	if_attach_common(ifp);
524 	NET_LOCK();
525 	TAILQ_INSERT_HEAD(&ifnet, ifp, if_list);
526 	if_attachsetup(ifp);
527 	NET_UNLOCK();
528 }
529 
530 void
531 if_attach(struct ifnet *ifp)
532 {
533 	if_attach_common(ifp);
534 	NET_LOCK();
535 	TAILQ_INSERT_TAIL(&ifnet, ifp, if_list);
536 	if_attachsetup(ifp);
537 	NET_UNLOCK();
538 }
539 
540 void
541 if_attach_queues(struct ifnet *ifp, unsigned int nqs)
542 {
543 	struct ifqueue **map;
544 	struct ifqueue *ifq;
545 	int i;
546 
547 	KASSERT(ifp->if_ifqs == ifp->if_snd.ifq_ifqs);
548 	KASSERT(nqs != 0);
549 
550 	map = mallocarray(sizeof(*map), nqs, M_DEVBUF, M_WAITOK);
551 
552 	ifp->if_snd.ifq_softc = NULL;
553 	map[0] = &ifp->if_snd;
554 
555 	for (i = 1; i < nqs; i++) {
556 		ifq = malloc(sizeof(*ifq), M_DEVBUF, M_WAITOK|M_ZERO);
557 		ifq_set_maxlen(ifq, ifp->if_snd.ifq_maxlen);
558 		ifq_init(ifq, ifp, i);
559 		map[i] = ifq;
560 	}
561 
562 	ifp->if_ifqs = map;
563 	ifp->if_nifqs = nqs;
564 }
565 
566 void
567 if_attach_iqueues(struct ifnet *ifp, unsigned int niqs)
568 {
569 	struct ifiqueue **map;
570 	struct ifiqueue *ifiq;
571 	unsigned int i;
572 
573 	KASSERT(niqs != 0);
574 
575 	map = mallocarray(niqs, sizeof(*map), M_DEVBUF, M_WAITOK);
576 
577 	ifp->if_rcv.ifiq_softc = NULL;
578 	map[0] = &ifp->if_rcv;
579 
580 	for (i = 1; i < niqs; i++) {
581 		ifiq = malloc(sizeof(*ifiq), M_DEVBUF, M_WAITOK|M_ZERO);
582 		ifiq_init(ifiq, ifp, i);
583 		map[i] = ifiq;
584 	}
585 
586 	ifp->if_iqs = map;
587 	ifp->if_niqs = niqs;
588 }
589 
590 void
591 if_attach_common(struct ifnet *ifp)
592 {
593 	KASSERT(ifp->if_ioctl != NULL);
594 
595 	TAILQ_INIT(&ifp->if_addrlist);
596 	TAILQ_INIT(&ifp->if_maddrlist);
597 	TAILQ_INIT(&ifp->if_groups);
598 
599 	if (!ISSET(ifp->if_xflags, IFXF_MPSAFE)) {
600 		KASSERTMSG(ifp->if_qstart == NULL,
601 		    "%s: if_qstart set without MPSAFE set", ifp->if_xname);
602 		ifp->if_qstart = if_qstart_compat;
603 	} else {
604 		KASSERTMSG(ifp->if_start == NULL,
605 		    "%s: if_start set with MPSAFE set", ifp->if_xname);
606 		KASSERTMSG(ifp->if_qstart != NULL,
607 		    "%s: if_qstart not set with MPSAFE set", ifp->if_xname);
608 	}
609 
610 	ifq_init(&ifp->if_snd, ifp, 0);
611 
612 	ifp->if_snd.ifq_ifqs[0] = &ifp->if_snd;
613 	ifp->if_ifqs = ifp->if_snd.ifq_ifqs;
614 	ifp->if_nifqs = 1;
615 	if (ifp->if_txmit == 0)
616 		ifp->if_txmit = IF_TXMIT_DEFAULT;
617 
618 	ifiq_init(&ifp->if_rcv, ifp, 0);
619 
620 	ifp->if_rcv.ifiq_ifiqs[0] = &ifp->if_rcv;
621 	ifp->if_iqs = ifp->if_rcv.ifiq_ifiqs;
622 	ifp->if_niqs = 1;
623 
624 	TAILQ_INIT(&ifp->if_addrhooks);
625 	TAILQ_INIT(&ifp->if_linkstatehooks);
626 	TAILQ_INIT(&ifp->if_detachhooks);
627 
628 	if (ifp->if_rtrequest == NULL)
629 		ifp->if_rtrequest = if_rtrequest_dummy;
630 	if (ifp->if_enqueue == NULL)
631 		ifp->if_enqueue = if_enqueue_ifq;
632 #if NBPFILTER > 0
633 	if (ifp->if_bpf_mtap == NULL)
634 		ifp->if_bpf_mtap = bpf_mtap_ether;
635 #endif
636 	ifp->if_llprio = IFQ_DEFPRIO;
637 }
638 
639 void
640 if_attach_ifq(struct ifnet *ifp, const struct ifq_ops *newops, void *args)
641 {
642 	/*
643 	 * only switch the ifq_ops on the first ifq on an interface.
644 	 *
645 	 * the only ifq_ops we provide priq and hfsc, and hfsc only
646 	 * works on a single ifq. because the code uses the ifq_ops
647 	 * on the first ifq (if_snd) to select a queue for an mbuf,
648 	 * by switching only the first one we change both the algorithm
649 	 * and force the routing of all new packets to it.
650 	 */
651 	ifq_attach(&ifp->if_snd, newops, args);
652 }
653 
654 void
655 if_start(struct ifnet *ifp)
656 {
657 	KASSERT(ifp->if_qstart == if_qstart_compat);
658 	if_qstart_compat(&ifp->if_snd);
659 }
660 void
661 if_qstart_compat(struct ifqueue *ifq)
662 {
663 	struct ifnet *ifp = ifq->ifq_if;
664 	int s;
665 
666 	/*
667 	 * the stack assumes that an interface can have multiple
668 	 * transmit rings, but a lot of drivers are still written
669 	 * so that interfaces and send rings have a 1:1 mapping.
670 	 * this provides compatibility between the stack and the older
671 	 * drivers by translating from the only queue they have
672 	 * (ifp->if_snd) back to the interface and calling if_start.
673 	 */
674 
675 	KERNEL_LOCK();
676 	s = splnet();
677 	(*ifp->if_start)(ifp);
678 	splx(s);
679 	KERNEL_UNLOCK();
680 }
681 
682 int
683 if_enqueue(struct ifnet *ifp, struct mbuf *m)
684 {
685 	CLR(m->m_pkthdr.csum_flags, M_TIMESTAMP);
686 
687 #if NPF > 0
688 	if (m->m_pkthdr.pf.delay > 0)
689 		return (pf_delay_pkt(m, ifp->if_index));
690 #endif
691 
692 #if NBRIDGE > 0
693 	if (ifp->if_bridgeidx && (m->m_flags & M_PROTO1) == 0) {
694 		int error;
695 
696 		error = bridge_enqueue(ifp, m);
697 		return (error);
698 	}
699 #endif
700 
701 #if NPF > 0
702 	pf_pkt_addr_changed(m);
703 #endif	/* NPF > 0 */
704 
705 	return ((*ifp->if_enqueue)(ifp, m));
706 }
707 
708 int
709 if_enqueue_ifq(struct ifnet *ifp, struct mbuf *m)
710 {
711 	struct ifqueue *ifq = &ifp->if_snd;
712 	int error;
713 
714 	if (ifp->if_nifqs > 1) {
715 		unsigned int idx;
716 
717 		/*
718 		 * use the operations on the first ifq to pick which of
719 		 * the array gets this mbuf.
720 		 */
721 
722 		idx = ifq_idx(&ifp->if_snd, ifp->if_nifqs, m);
723 		ifq = ifp->if_ifqs[idx];
724 	}
725 
726 	error = ifq_enqueue(ifq, m);
727 	if (error)
728 		return (error);
729 
730 	ifq_start(ifq);
731 
732 	return (0);
733 }
734 
735 void
736 if_input(struct ifnet *ifp, struct mbuf_list *ml)
737 {
738 	ifiq_input(&ifp->if_rcv, ml);
739 }
740 
741 int
742 if_input_local(struct ifnet *ifp, struct mbuf *m, sa_family_t af)
743 {
744 	int keepflags;
745 
746 #if NBPFILTER > 0
747 	/*
748 	 * Only send packets to bpf if they are destined to local
749 	 * addresses.
750 	 *
751 	 * if_input_local() is also called for SIMPLEX interfaces to
752 	 * duplicate packets for local use.  But don't dup them to bpf.
753 	 */
754 	if (ifp->if_flags & IFF_LOOPBACK) {
755 		caddr_t if_bpf = ifp->if_bpf;
756 
757 		if (if_bpf)
758 			bpf_mtap_af(if_bpf, af, m, BPF_DIRECTION_OUT);
759 	}
760 #endif
761 	keepflags = m->m_flags & (M_BCAST|M_MCAST);
762 	m_resethdr(m);
763 	m->m_flags |= M_LOOP | keepflags;
764 	m->m_pkthdr.ph_ifidx = ifp->if_index;
765 	m->m_pkthdr.ph_rtableid = ifp->if_rdomain;
766 
767 	ifp->if_opackets++;
768 	ifp->if_obytes += m->m_pkthdr.len;
769 
770 	ifp->if_ipackets++;
771 	ifp->if_ibytes += m->m_pkthdr.len;
772 
773 	switch (af) {
774 	case AF_INET:
775 		ipv4_input(ifp, m);
776 		break;
777 #ifdef INET6
778 	case AF_INET6:
779 		ipv6_input(ifp, m);
780 		break;
781 #endif /* INET6 */
782 #ifdef MPLS
783 	case AF_MPLS:
784 		mpls_input(ifp, m);
785 		break;
786 #endif /* MPLS */
787 	default:
788 		printf("%s: can't handle af%d\n", ifp->if_xname, af);
789 		m_freem(m);
790 		return (EAFNOSUPPORT);
791 	}
792 
793 	return (0);
794 }
795 
796 int
797 if_output_local(struct ifnet *ifp, struct mbuf *m, sa_family_t af)
798 {
799 	struct ifiqueue *ifiq;
800 	unsigned int flow = 0;
801 
802 	m->m_pkthdr.ph_family = af;
803 	m->m_pkthdr.ph_ifidx = ifp->if_index;
804 	m->m_pkthdr.ph_rtableid = ifp->if_rdomain;
805 
806 	if (ISSET(m->m_pkthdr.csum_flags, M_FLOWID))
807 		flow = m->m_pkthdr.ph_flowid;
808 
809 	ifiq = ifp->if_iqs[flow % ifp->if_niqs];
810 
811 	return (ifiq_enqueue(ifiq, m) == 0 ? 0 : ENOBUFS);
812 }
813 
814 void
815 if_input_process(struct ifnet *ifp, struct mbuf_list *ml)
816 {
817 	struct mbuf *m;
818 
819 	if (ml_empty(ml))
820 		return;
821 
822 	if (!ISSET(ifp->if_xflags, IFXF_CLONED))
823 		enqueue_randomness(ml_len(ml) ^ (uintptr_t)MBUF_LIST_FIRST(ml));
824 
825 	/*
826 	 * We grab the NET_LOCK() before processing any packet to
827 	 * ensure there's no contention on the routing table lock.
828 	 *
829 	 * Without it we could race with a userland thread to insert
830 	 * a L2 entry in ip{6,}_output().  Such race would result in
831 	 * one of the threads sleeping *inside* the IP output path.
832 	 *
833 	 * Since we have a NET_LOCK() we also use it to serialize access
834 	 * to PF globals, pipex globals, unicast and multicast addresses
835 	 * lists and the socket layer.
836 	 */
837 
838 	NET_RLOCK_IN_SOFTNET();
839 	while ((m = ml_dequeue(ml)) != NULL)
840 		(*ifp->if_input)(ifp, m);
841 	NET_RUNLOCK_IN_SOFTNET();
842 }
843 
844 void
845 if_vinput(struct ifnet *ifp, struct mbuf *m)
846 {
847 #if NBPFILTER > 0
848 	caddr_t if_bpf;
849 #endif
850 
851 	m->m_pkthdr.ph_ifidx = ifp->if_index;
852 	m->m_pkthdr.ph_rtableid = ifp->if_rdomain;
853 
854 	counters_pkt(ifp->if_counters,
855 	    ifc_ipackets, ifc_ibytes, m->m_pkthdr.len);
856 
857 #if NPF > 0
858 	pf_pkt_addr_changed(m);
859 #endif
860 
861 #if NBPFILTER > 0
862 	if_bpf = ifp->if_bpf;
863 	if (if_bpf) {
864 		if ((*ifp->if_bpf_mtap)(if_bpf, m, BPF_DIRECTION_IN)) {
865 			m_freem(m);
866 			return;
867 		}
868 	}
869 #endif
870 
871 	if (__predict_true(!ISSET(ifp->if_xflags, IFXF_MONITOR)))
872 		(*ifp->if_input)(ifp, m);
873 	else
874 		m_freem(m);
875 }
876 
877 void
878 if_netisr(void *unused)
879 {
880 	int n, t = 0;
881 
882 	NET_LOCK();
883 
884 	while ((n = netisr) != 0) {
885 		/* Like sched_pause() but with a rwlock dance. */
886 		if (curcpu()->ci_schedstate.spc_schedflags & SPCF_SHOULDYIELD) {
887 			NET_UNLOCK();
888 			yield();
889 			NET_LOCK();
890 		}
891 
892 		atomic_clearbits_int(&netisr, n);
893 
894 #if NETHER > 0
895 		if (n & (1 << NETISR_ARP)) {
896 			KERNEL_LOCK();
897 			arpintr();
898 			KERNEL_UNLOCK();
899 		}
900 #endif
901 		if (n & (1 << NETISR_IP))
902 			ipintr();
903 #ifdef INET6
904 		if (n & (1 << NETISR_IPV6))
905 			ip6intr();
906 #endif
907 #if NPPP > 0
908 		if (n & (1 << NETISR_PPP)) {
909 			KERNEL_LOCK();
910 			pppintr();
911 			KERNEL_UNLOCK();
912 		}
913 #endif
914 #if NBRIDGE > 0
915 		if (n & (1 << NETISR_BRIDGE))
916 			bridgeintr();
917 #endif
918 #ifdef PIPEX
919 		if (n & (1 << NETISR_PIPEX))
920 			pipexintr();
921 #endif
922 #if NPPPOE > 0
923 		if (n & (1 << NETISR_PPPOE)) {
924 			KERNEL_LOCK();
925 			pppoeintr();
926 			KERNEL_UNLOCK();
927 		}
928 #endif
929 		t |= n;
930 	}
931 
932 #if NPFSYNC > 0
933 	if (t & (1 << NETISR_PFSYNC)) {
934 		KERNEL_LOCK();
935 		pfsyncintr();
936 		KERNEL_UNLOCK();
937 	}
938 #endif
939 
940 	NET_UNLOCK();
941 }
942 
943 void
944 if_hooks_run(struct task_list *hooks)
945 {
946 	struct task *t, *nt;
947 	struct task cursor = { .t_func = NULL };
948 	void (*func)(void *);
949 	void *arg;
950 
951 	mtx_enter(&if_hooks_mtx);
952 	for (t = TAILQ_FIRST(hooks); t != NULL; t = nt) {
953 		if (t->t_func == NULL) { /* skip cursors */
954 			nt = TAILQ_NEXT(t, t_entry);
955 			continue;
956 		}
957 		func = t->t_func;
958 		arg = t->t_arg;
959 
960 		TAILQ_INSERT_AFTER(hooks, t, &cursor, t_entry);
961 		mtx_leave(&if_hooks_mtx);
962 
963 		(*func)(arg);
964 
965 		mtx_enter(&if_hooks_mtx);
966 		nt = TAILQ_NEXT(&cursor, t_entry); /* avoid _Q_INVALIDATE */
967 		TAILQ_REMOVE(hooks, &cursor, t_entry);
968 	}
969 	mtx_leave(&if_hooks_mtx);
970 }
971 
972 void
973 if_remove(struct ifnet *ifp)
974 {
975 	/* Remove the interface from the list of all interfaces. */
976 	NET_LOCK();
977 	TAILQ_REMOVE(&ifnet, ifp, if_list);
978 	NET_UNLOCK();
979 
980 	/* Remove the interface from the interface index map. */
981 	if_idxmap_remove(ifp);
982 
983 	/* Sleep until the last reference is released. */
984 	refcnt_finalize(&ifp->if_refcnt, "ifrm");
985 }
986 
987 void
988 if_deactivate(struct ifnet *ifp)
989 {
990 	/*
991 	 * Call detach hooks from head to tail.  To make sure detach
992 	 * hooks are executed in the reverse order they were added, all
993 	 * the hooks have to be added to the head!
994 	 */
995 
996 	NET_LOCK();
997 	if_hooks_run(&ifp->if_detachhooks);
998 	NET_UNLOCK();
999 }
1000 
1001 void
1002 if_detachhook_add(struct ifnet *ifp, struct task *t)
1003 {
1004 	mtx_enter(&if_hooks_mtx);
1005 	TAILQ_INSERT_HEAD(&ifp->if_detachhooks, t, t_entry);
1006 	mtx_leave(&if_hooks_mtx);
1007 }
1008 
1009 void
1010 if_detachhook_del(struct ifnet *ifp, struct task *t)
1011 {
1012 	mtx_enter(&if_hooks_mtx);
1013 	TAILQ_REMOVE(&ifp->if_detachhooks, t, t_entry);
1014 	mtx_leave(&if_hooks_mtx);
1015 }
1016 
1017 /*
1018  * Detach an interface from everything in the kernel.  Also deallocate
1019  * private resources.
1020  */
1021 void
1022 if_detach(struct ifnet *ifp)
1023 {
1024 	struct ifaddr *ifa;
1025 	struct ifg_list *ifg;
1026 	const struct domain *dp;
1027 	int i, s;
1028 
1029 	/* Undo pseudo-driver changes. */
1030 	if_deactivate(ifp);
1031 
1032 	/* Other CPUs must not have a reference before we start destroying. */
1033 	if_remove(ifp);
1034 
1035 	ifp->if_qstart = if_detached_qstart;
1036 
1037 	/* Wait until the start routines finished. */
1038 	ifq_barrier(&ifp->if_snd);
1039 	ifq_clr_oactive(&ifp->if_snd);
1040 
1041 #if NBPFILTER > 0
1042 	bpfdetach(ifp);
1043 #endif
1044 
1045 	NET_LOCK();
1046 	s = splnet();
1047 	ifp->if_ioctl = if_detached_ioctl;
1048 	ifp->if_watchdog = NULL;
1049 
1050 	/* Remove the watchdog timeout & task */
1051 	timeout_del(&ifp->if_slowtimo);
1052 	task_del(net_tq(ifp->if_index), &ifp->if_watchdogtask);
1053 
1054 	/* Remove the link state task */
1055 	task_del(net_tq(ifp->if_index), &ifp->if_linkstatetask);
1056 
1057 	rti_delete(ifp);
1058 #if NETHER > 0 && defined(NFSCLIENT)
1059 	if (ifp->if_index == revarp_ifidx)
1060 		revarp_ifidx = 0;
1061 #endif
1062 #ifdef MROUTING
1063 	vif_delete(ifp);
1064 #endif
1065 	in_ifdetach(ifp);
1066 #ifdef INET6
1067 	in6_ifdetach(ifp);
1068 #endif
1069 #if NPF > 0
1070 	pfi_detach_ifnet(ifp);
1071 #endif
1072 
1073 	while ((ifg = TAILQ_FIRST(&ifp->if_groups)) != NULL)
1074 		if_delgroup(ifp, ifg->ifgl_group->ifg_group);
1075 
1076 	if_free_sadl(ifp);
1077 
1078 	/* We should not have any address left at this point. */
1079 	if (!TAILQ_EMPTY(&ifp->if_addrlist)) {
1080 #ifdef DIAGNOSTIC
1081 		printf("%s: address list non empty\n", ifp->if_xname);
1082 #endif
1083 		while ((ifa = TAILQ_FIRST(&ifp->if_addrlist)) != NULL) {
1084 			ifa_del(ifp, ifa);
1085 			ifa->ifa_ifp = NULL;
1086 			ifafree(ifa);
1087 		}
1088 	}
1089 
1090 	KASSERT(TAILQ_EMPTY(&ifp->if_addrhooks));
1091 	KASSERT(TAILQ_EMPTY(&ifp->if_linkstatehooks));
1092 	KASSERT(TAILQ_EMPTY(&ifp->if_detachhooks));
1093 
1094 	for (i = 0; (dp = domains[i]) != NULL; i++) {
1095 		if (dp->dom_ifdetach && ifp->if_afdata[dp->dom_family])
1096 			(*dp->dom_ifdetach)(ifp,
1097 			    ifp->if_afdata[dp->dom_family]);
1098 	}
1099 
1100 	/* Announce that the interface is gone. */
1101 	rtm_ifannounce(ifp, IFAN_DEPARTURE);
1102 	splx(s);
1103 	NET_UNLOCK();
1104 
1105 	if (ifp->if_counters != NULL)
1106 		if_counters_free(ifp);
1107 
1108 	for (i = 0; i < ifp->if_nifqs; i++)
1109 		ifq_destroy(ifp->if_ifqs[i]);
1110 	if (ifp->if_ifqs != ifp->if_snd.ifq_ifqs) {
1111 		for (i = 1; i < ifp->if_nifqs; i++) {
1112 			free(ifp->if_ifqs[i], M_DEVBUF,
1113 			    sizeof(struct ifqueue));
1114 		}
1115 		free(ifp->if_ifqs, M_DEVBUF,
1116 		    sizeof(struct ifqueue *) * ifp->if_nifqs);
1117 	}
1118 
1119 	for (i = 0; i < ifp->if_niqs; i++)
1120 		ifiq_destroy(ifp->if_iqs[i]);
1121 	if (ifp->if_iqs != ifp->if_rcv.ifiq_ifiqs) {
1122 		for (i = 1; i < ifp->if_niqs; i++) {
1123 			free(ifp->if_iqs[i], M_DEVBUF,
1124 			    sizeof(struct ifiqueue));
1125 		}
1126 		free(ifp->if_iqs, M_DEVBUF,
1127 		    sizeof(struct ifiqueue *) * ifp->if_niqs);
1128 	}
1129 }
1130 
1131 /*
1132  * Returns true if ``ifp0'' is connected to the interface with index ``ifidx''.
1133  */
1134 int
1135 if_isconnected(const struct ifnet *ifp0, unsigned int ifidx)
1136 {
1137 	struct ifnet *ifp;
1138 	int connected = 0;
1139 
1140 	ifp = if_get(ifidx);
1141 	if (ifp == NULL)
1142 		return (0);
1143 
1144 	if (ifp0->if_index == ifp->if_index)
1145 		connected = 1;
1146 
1147 #if NBRIDGE > 0
1148 	if (ifp0->if_bridgeidx != 0 && ifp0->if_bridgeidx == ifp->if_bridgeidx)
1149 		connected = 1;
1150 #endif
1151 #if NCARP > 0
1152 	if ((ifp0->if_type == IFT_CARP &&
1153 	    ifp0->if_carpdevidx == ifp->if_index) ||
1154 	    (ifp->if_type == IFT_CARP && ifp->if_carpdevidx == ifp0->if_index))
1155 		connected = 1;
1156 #endif
1157 
1158 	if_put(ifp);
1159 	return (connected);
1160 }
1161 
1162 /*
1163  * Create a clone network interface.
1164  */
1165 int
1166 if_clone_create(const char *name, int rdomain)
1167 {
1168 	struct if_clone *ifc;
1169 	struct ifnet *ifp;
1170 	int unit, ret;
1171 
1172 	ifc = if_clone_lookup(name, &unit);
1173 	if (ifc == NULL)
1174 		return (EINVAL);
1175 
1176 	rw_enter_write(&if_cloners_lock);
1177 
1178 	if ((ifp = if_unit(name)) != NULL) {
1179 		ret = EEXIST;
1180 		goto unlock;
1181 	}
1182 
1183 	ret = (*ifc->ifc_create)(ifc, unit);
1184 
1185 	if (ret != 0 || (ifp = if_unit(name)) == NULL)
1186 		goto unlock;
1187 
1188 	NET_LOCK();
1189 	if_addgroup(ifp, ifc->ifc_name);
1190 	if (rdomain != 0)
1191 		if_setrdomain(ifp, rdomain);
1192 	NET_UNLOCK();
1193 unlock:
1194 	rw_exit_write(&if_cloners_lock);
1195 	if_put(ifp);
1196 
1197 	return (ret);
1198 }
1199 
1200 /*
1201  * Destroy a clone network interface.
1202  */
1203 int
1204 if_clone_destroy(const char *name)
1205 {
1206 	struct if_clone *ifc;
1207 	struct ifnet *ifp;
1208 	int ret;
1209 
1210 	ifc = if_clone_lookup(name, NULL);
1211 	if (ifc == NULL)
1212 		return (EINVAL);
1213 
1214 	if (ifc->ifc_destroy == NULL)
1215 		return (EOPNOTSUPP);
1216 
1217 	rw_enter_write(&if_cloners_lock);
1218 
1219 	TAILQ_FOREACH(ifp, &ifnet, if_list) {
1220 		if (strcmp(ifp->if_xname, name) == 0)
1221 			break;
1222 	}
1223 	if (ifp == NULL) {
1224 		rw_exit_write(&if_cloners_lock);
1225 		return (ENXIO);
1226 	}
1227 
1228 	NET_LOCK();
1229 	if (ifp->if_flags & IFF_UP) {
1230 		int s;
1231 		s = splnet();
1232 		if_down(ifp);
1233 		splx(s);
1234 	}
1235 	NET_UNLOCK();
1236 	ret = (*ifc->ifc_destroy)(ifp);
1237 
1238 	rw_exit_write(&if_cloners_lock);
1239 
1240 	return (ret);
1241 }
1242 
1243 /*
1244  * Look up a network interface cloner.
1245  */
1246 struct if_clone *
1247 if_clone_lookup(const char *name, int *unitp)
1248 {
1249 	struct if_clone *ifc;
1250 	const char *cp;
1251 	int unit;
1252 
1253 	/* separate interface name from unit */
1254 	for (cp = name;
1255 	    cp - name < IFNAMSIZ && *cp && (*cp < '0' || *cp > '9');
1256 	    cp++)
1257 		continue;
1258 
1259 	if (cp == name || cp - name == IFNAMSIZ || !*cp)
1260 		return (NULL);	/* No name or unit number */
1261 
1262 	if (cp - name < IFNAMSIZ-1 && *cp == '0' && cp[1] != '\0')
1263 		return (NULL);	/* unit number 0 padded */
1264 
1265 	LIST_FOREACH(ifc, &if_cloners, ifc_list) {
1266 		if (strlen(ifc->ifc_name) == cp - name &&
1267 		    !strncmp(name, ifc->ifc_name, cp - name))
1268 			break;
1269 	}
1270 
1271 	if (ifc == NULL)
1272 		return (NULL);
1273 
1274 	unit = 0;
1275 	while (cp - name < IFNAMSIZ && *cp) {
1276 		if (*cp < '0' || *cp > '9' ||
1277 		    unit > (INT_MAX - (*cp - '0')) / 10) {
1278 			/* Bogus unit number. */
1279 			return (NULL);
1280 		}
1281 		unit = (unit * 10) + (*cp++ - '0');
1282 	}
1283 
1284 	if (unitp != NULL)
1285 		*unitp = unit;
1286 	return (ifc);
1287 }
1288 
1289 /*
1290  * Register a network interface cloner.
1291  */
1292 void
1293 if_clone_attach(struct if_clone *ifc)
1294 {
1295 	/*
1296 	 * we are called at kernel boot by main(), when pseudo devices are
1297 	 * being attached. The main() is the only guy which may alter the
1298 	 * if_cloners. While system is running and main() is done with
1299 	 * initialization, the if_cloners becomes immutable.
1300 	 */
1301 	KASSERT(pdevinit_done == 0);
1302 	LIST_INSERT_HEAD(&if_cloners, ifc, ifc_list);
1303 	if_cloners_count++;
1304 }
1305 
1306 /*
1307  * Provide list of interface cloners to userspace.
1308  */
1309 int
1310 if_clone_list(struct if_clonereq *ifcr)
1311 {
1312 	char outbuf[IFNAMSIZ], *dst;
1313 	struct if_clone *ifc;
1314 	int count, error = 0;
1315 
1316 	if ((dst = ifcr->ifcr_buffer) == NULL) {
1317 		/* Just asking how many there are. */
1318 		ifcr->ifcr_total = if_cloners_count;
1319 		return (0);
1320 	}
1321 
1322 	if (ifcr->ifcr_count < 0)
1323 		return (EINVAL);
1324 
1325 	ifcr->ifcr_total = if_cloners_count;
1326 	count = MIN(if_cloners_count, ifcr->ifcr_count);
1327 
1328 	LIST_FOREACH(ifc, &if_cloners, ifc_list) {
1329 		if (count == 0)
1330 			break;
1331 		bzero(outbuf, sizeof outbuf);
1332 		strlcpy(outbuf, ifc->ifc_name, IFNAMSIZ);
1333 		error = copyout(outbuf, dst, IFNAMSIZ);
1334 		if (error)
1335 			break;
1336 		count--;
1337 		dst += IFNAMSIZ;
1338 	}
1339 
1340 	return (error);
1341 }
1342 
1343 /*
1344  * set queue congestion marker
1345  */
1346 void
1347 if_congestion(void)
1348 {
1349 	extern int ticks;
1350 
1351 	ifq_congestion = ticks;
1352 }
1353 
1354 int
1355 if_congested(void)
1356 {
1357 	extern int ticks;
1358 	int diff;
1359 
1360 	diff = ticks - ifq_congestion;
1361 	if (diff < 0) {
1362 		ifq_congestion = ticks - hz;
1363 		return (0);
1364 	}
1365 
1366 	return (diff <= (hz / 100));
1367 }
1368 
1369 #define	equal(a1, a2)	\
1370 	(bcmp((caddr_t)(a1), (caddr_t)(a2),	\
1371 	(a1)->sa_len) == 0)
1372 
1373 /*
1374  * Locate an interface based on a complete address.
1375  */
1376 struct ifaddr *
1377 ifa_ifwithaddr(struct sockaddr *addr, u_int rtableid)
1378 {
1379 	struct ifnet *ifp;
1380 	struct ifaddr *ifa;
1381 	u_int rdomain;
1382 
1383 	rdomain = rtable_l2(rtableid);
1384 	KERNEL_LOCK();
1385 	TAILQ_FOREACH(ifp, &ifnet, if_list) {
1386 		if (ifp->if_rdomain != rdomain)
1387 			continue;
1388 
1389 		TAILQ_FOREACH(ifa, &ifp->if_addrlist, ifa_list) {
1390 			if (ifa->ifa_addr->sa_family != addr->sa_family)
1391 				continue;
1392 
1393 			if (equal(addr, ifa->ifa_addr)) {
1394 				KERNEL_UNLOCK();
1395 				return (ifa);
1396 			}
1397 		}
1398 	}
1399 	KERNEL_UNLOCK();
1400 	return (NULL);
1401 }
1402 
1403 /*
1404  * Locate the point to point interface with a given destination address.
1405  */
1406 struct ifaddr *
1407 ifa_ifwithdstaddr(struct sockaddr *addr, u_int rdomain)
1408 {
1409 	struct ifnet *ifp;
1410 	struct ifaddr *ifa;
1411 
1412 	rdomain = rtable_l2(rdomain);
1413 	KERNEL_LOCK();
1414 	TAILQ_FOREACH(ifp, &ifnet, if_list) {
1415 		if (ifp->if_rdomain != rdomain)
1416 			continue;
1417 		if (ifp->if_flags & IFF_POINTOPOINT) {
1418 			TAILQ_FOREACH(ifa, &ifp->if_addrlist, ifa_list) {
1419 				if (ifa->ifa_addr->sa_family !=
1420 				    addr->sa_family || ifa->ifa_dstaddr == NULL)
1421 					continue;
1422 				if (equal(addr, ifa->ifa_dstaddr)) {
1423 					KERNEL_UNLOCK();
1424 					return (ifa);
1425 				}
1426 			}
1427 		}
1428 	}
1429 	KERNEL_UNLOCK();
1430 	return (NULL);
1431 }
1432 
1433 /*
1434  * Find an interface address specific to an interface best matching
1435  * a given address.
1436  */
1437 struct ifaddr *
1438 ifaof_ifpforaddr(struct sockaddr *addr, struct ifnet *ifp)
1439 {
1440 	struct ifaddr *ifa;
1441 	char *cp, *cp2, *cp3;
1442 	char *cplim;
1443 	struct ifaddr *ifa_maybe = NULL;
1444 	u_int af = addr->sa_family;
1445 
1446 	if (af >= AF_MAX)
1447 		return (NULL);
1448 	TAILQ_FOREACH(ifa, &ifp->if_addrlist, ifa_list) {
1449 		if (ifa->ifa_addr->sa_family != af)
1450 			continue;
1451 		if (ifa_maybe == NULL)
1452 			ifa_maybe = ifa;
1453 		if (ifa->ifa_netmask == 0 || ifp->if_flags & IFF_POINTOPOINT) {
1454 			if (equal(addr, ifa->ifa_addr) ||
1455 			    (ifa->ifa_dstaddr && equal(addr, ifa->ifa_dstaddr)))
1456 				return (ifa);
1457 			continue;
1458 		}
1459 		cp = addr->sa_data;
1460 		cp2 = ifa->ifa_addr->sa_data;
1461 		cp3 = ifa->ifa_netmask->sa_data;
1462 		cplim = ifa->ifa_netmask->sa_len + (char *)ifa->ifa_netmask;
1463 		for (; cp3 < cplim; cp3++)
1464 			if ((*cp++ ^ *cp2++) & *cp3)
1465 				break;
1466 		if (cp3 == cplim)
1467 			return (ifa);
1468 	}
1469 	return (ifa_maybe);
1470 }
1471 
1472 void
1473 if_rtrequest_dummy(struct ifnet *ifp, int req, struct rtentry *rt)
1474 {
1475 }
1476 
1477 /*
1478  * Default action when installing a local route on a point-to-point
1479  * interface.
1480  */
1481 void
1482 p2p_rtrequest(struct ifnet *ifp, int req, struct rtentry *rt)
1483 {
1484 	struct ifnet *lo0ifp;
1485 	struct ifaddr *ifa, *lo0ifa;
1486 
1487 	switch (req) {
1488 	case RTM_ADD:
1489 		if (!ISSET(rt->rt_flags, RTF_LOCAL))
1490 			break;
1491 
1492 		TAILQ_FOREACH(ifa, &ifp->if_addrlist, ifa_list) {
1493 			if (memcmp(rt_key(rt), ifa->ifa_addr,
1494 			    rt_key(rt)->sa_len) == 0)
1495 				break;
1496 		}
1497 
1498 		if (ifa == NULL)
1499 			break;
1500 
1501 		KASSERT(ifa == rt->rt_ifa);
1502 
1503 		lo0ifp = if_get(rtable_loindex(ifp->if_rdomain));
1504 		KASSERT(lo0ifp != NULL);
1505 		TAILQ_FOREACH(lo0ifa, &lo0ifp->if_addrlist, ifa_list) {
1506 			if (lo0ifa->ifa_addr->sa_family ==
1507 			    ifa->ifa_addr->sa_family)
1508 				break;
1509 		}
1510 		if_put(lo0ifp);
1511 
1512 		if (lo0ifa == NULL)
1513 			break;
1514 
1515 		rt->rt_flags &= ~RTF_LLINFO;
1516 		break;
1517 	case RTM_DELETE:
1518 	case RTM_RESOLVE:
1519 	default:
1520 		break;
1521 	}
1522 }
1523 
1524 int
1525 p2p_bpf_mtap(caddr_t if_bpf, const struct mbuf *m, u_int dir)
1526 {
1527 #if NBPFILTER > 0
1528 	return (bpf_mtap_af(if_bpf, m->m_pkthdr.ph_family, m, dir));
1529 #else
1530 	return (0);
1531 #endif
1532 }
1533 
1534 void
1535 p2p_input(struct ifnet *ifp, struct mbuf *m)
1536 {
1537 	void (*input)(struct ifnet *, struct mbuf *);
1538 
1539 	switch (m->m_pkthdr.ph_family) {
1540 	case AF_INET:
1541 		input = ipv4_input;
1542 		break;
1543 #ifdef INET6
1544 	case AF_INET6:
1545 		input = ipv6_input;
1546 		break;
1547 #endif
1548 #ifdef MPLS
1549 	case AF_MPLS:
1550 		input = mpls_input;
1551 		break;
1552 #endif
1553 	default:
1554 		m_freem(m);
1555 		return;
1556 	}
1557 
1558 	(*input)(ifp, m);
1559 }
1560 
1561 /*
1562  * Bring down all interfaces
1563  */
1564 void
1565 if_downall(void)
1566 {
1567 	struct ifreq ifrq;	/* XXX only partly built */
1568 	struct ifnet *ifp;
1569 
1570 	NET_LOCK();
1571 	TAILQ_FOREACH(ifp, &ifnet, if_list) {
1572 		if ((ifp->if_flags & IFF_UP) == 0)
1573 			continue;
1574 		if_down(ifp);
1575 		ifrq.ifr_flags = ifp->if_flags;
1576 		(*ifp->if_ioctl)(ifp, SIOCSIFFLAGS, (caddr_t)&ifrq);
1577 	}
1578 	NET_UNLOCK();
1579 }
1580 
1581 /*
1582  * Mark an interface down and notify protocols of
1583  * the transition.
1584  */
1585 void
1586 if_down(struct ifnet *ifp)
1587 {
1588 	NET_ASSERT_LOCKED();
1589 
1590 	ifp->if_flags &= ~IFF_UP;
1591 	getmicrotime(&ifp->if_lastchange);
1592 	ifq_purge(&ifp->if_snd);
1593 
1594 	if_linkstate(ifp);
1595 }
1596 
1597 /*
1598  * Mark an interface up and notify protocols of
1599  * the transition.
1600  */
1601 void
1602 if_up(struct ifnet *ifp)
1603 {
1604 	NET_ASSERT_LOCKED();
1605 
1606 	ifp->if_flags |= IFF_UP;
1607 	getmicrotime(&ifp->if_lastchange);
1608 
1609 #ifdef INET6
1610 	/* Userland expects the kernel to set ::1 on default lo(4). */
1611 	if (ifp->if_index == rtable_loindex(ifp->if_rdomain))
1612 		in6_ifattach(ifp);
1613 #endif
1614 
1615 	if_linkstate(ifp);
1616 }
1617 
1618 /*
1619  * Notify userland, the routing table and hooks owner of
1620  * a link-state transition.
1621  */
1622 void
1623 if_linkstate_task(void *xifidx)
1624 {
1625 	unsigned int ifidx = (unsigned long)xifidx;
1626 	struct ifnet *ifp;
1627 
1628 	KERNEL_LOCK();
1629 	NET_LOCK();
1630 
1631 	ifp = if_get(ifidx);
1632 	if (ifp != NULL)
1633 		if_linkstate(ifp);
1634 	if_put(ifp);
1635 
1636 	NET_UNLOCK();
1637 	KERNEL_UNLOCK();
1638 }
1639 
1640 void
1641 if_linkstate(struct ifnet *ifp)
1642 {
1643 	NET_ASSERT_LOCKED();
1644 
1645 	rtm_ifchg(ifp);
1646 	rt_if_track(ifp);
1647 
1648 	if_hooks_run(&ifp->if_linkstatehooks);
1649 }
1650 
1651 void
1652 if_linkstatehook_add(struct ifnet *ifp, struct task *t)
1653 {
1654 	mtx_enter(&if_hooks_mtx);
1655 	TAILQ_INSERT_HEAD(&ifp->if_linkstatehooks, t, t_entry);
1656 	mtx_leave(&if_hooks_mtx);
1657 }
1658 
1659 void
1660 if_linkstatehook_del(struct ifnet *ifp, struct task *t)
1661 {
1662 	mtx_enter(&if_hooks_mtx);
1663 	TAILQ_REMOVE(&ifp->if_linkstatehooks, t, t_entry);
1664 	mtx_leave(&if_hooks_mtx);
1665 }
1666 
1667 /*
1668  * Schedule a link state change task.
1669  */
1670 void
1671 if_link_state_change(struct ifnet *ifp)
1672 {
1673 	task_add(net_tq(ifp->if_index), &ifp->if_linkstatetask);
1674 }
1675 
1676 /*
1677  * Handle interface watchdog timer routine.  Called
1678  * from softclock, we decrement timer (if set) and
1679  * call the appropriate interface routine on expiration.
1680  */
1681 void
1682 if_slowtimo(void *arg)
1683 {
1684 	struct ifnet *ifp = arg;
1685 	int s = splnet();
1686 
1687 	if (ifp->if_watchdog) {
1688 		if (ifp->if_timer > 0 && --ifp->if_timer == 0)
1689 			task_add(net_tq(ifp->if_index), &ifp->if_watchdogtask);
1690 		timeout_add_sec(&ifp->if_slowtimo, IFNET_SLOWTIMO);
1691 	}
1692 	splx(s);
1693 }
1694 
1695 void
1696 if_watchdog_task(void *xifidx)
1697 {
1698 	unsigned int ifidx = (unsigned long)xifidx;
1699 	struct ifnet *ifp;
1700 	int s;
1701 
1702 	ifp = if_get(ifidx);
1703 	if (ifp == NULL)
1704 		return;
1705 
1706 	KERNEL_LOCK();
1707 	s = splnet();
1708 	if (ifp->if_watchdog)
1709 		(*ifp->if_watchdog)(ifp);
1710 	splx(s);
1711 	KERNEL_UNLOCK();
1712 
1713 	if_put(ifp);
1714 }
1715 
1716 /*
1717  * Map interface name to interface structure pointer.
1718  */
1719 struct ifnet *
1720 if_unit(const char *name)
1721 {
1722 	struct ifnet *ifp;
1723 
1724 	KERNEL_ASSERT_LOCKED();
1725 
1726 	TAILQ_FOREACH(ifp, &ifnet, if_list) {
1727 		if (strcmp(ifp->if_xname, name) == 0) {
1728 			if_ref(ifp);
1729 			return (ifp);
1730 		}
1731 	}
1732 
1733 	return (NULL);
1734 }
1735 
1736 /*
1737  * Map interface index to interface structure pointer.
1738  */
1739 struct ifnet *
1740 if_get(unsigned int index)
1741 {
1742 	struct srp_ref sr;
1743 	struct if_map *if_map;
1744 	struct srp *map;
1745 	struct ifnet *ifp = NULL;
1746 
1747 	if_map = srp_enter(&sr, &if_idxmap.map);
1748 	if (index < if_map->limit) {
1749 		map = (struct srp *)(if_map + 1);
1750 
1751 		ifp = srp_follow(&sr, &map[index]);
1752 		if (ifp != NULL) {
1753 			KASSERT(ifp->if_index == index);
1754 			if_ref(ifp);
1755 		}
1756 	}
1757 	srp_leave(&sr);
1758 
1759 	return (ifp);
1760 }
1761 
1762 struct ifnet *
1763 if_ref(struct ifnet *ifp)
1764 {
1765 	refcnt_take(&ifp->if_refcnt);
1766 
1767 	return (ifp);
1768 }
1769 
1770 void
1771 if_put(struct ifnet *ifp)
1772 {
1773 	if (ifp == NULL)
1774 		return;
1775 
1776 	refcnt_rele_wake(&ifp->if_refcnt);
1777 }
1778 
1779 int
1780 if_setlladdr(struct ifnet *ifp, const uint8_t *lladdr)
1781 {
1782 	if (ifp->if_sadl == NULL)
1783 		return (EINVAL);
1784 
1785 	memcpy(((struct arpcom *)ifp)->ac_enaddr, lladdr, ETHER_ADDR_LEN);
1786 	memcpy(LLADDR(ifp->if_sadl), lladdr, ETHER_ADDR_LEN);
1787 
1788 	return (0);
1789 }
1790 
1791 int
1792 if_createrdomain(int rdomain, struct ifnet *ifp)
1793 {
1794 	int error;
1795 	struct ifnet *loifp;
1796 	char loifname[IFNAMSIZ];
1797 	unsigned int unit = rdomain;
1798 
1799 	if ((error = rtable_add(rdomain)) != 0)
1800 		return (error);
1801 	if (!rtable_empty(rdomain))
1802 		return (EEXIST);
1803 
1804 	/* Create rdomain including its loopback if with unit == rdomain */
1805 	snprintf(loifname, sizeof(loifname), "lo%u", unit);
1806 	error = if_clone_create(loifname, 0);
1807 	if ((loifp = if_unit(loifname)) == NULL)
1808 		return (ENXIO);
1809 	if (error && (ifp != loifp || error != EEXIST)) {
1810 		if_put(loifp);
1811 		return (error);
1812 	}
1813 
1814 	rtable_l2set(rdomain, rdomain, loifp->if_index);
1815 	loifp->if_rdomain = rdomain;
1816 	if_put(loifp);
1817 
1818 	return (0);
1819 }
1820 
1821 int
1822 if_setrdomain(struct ifnet *ifp, int rdomain)
1823 {
1824 	struct ifreq ifr;
1825 	int error, up = 0, s;
1826 
1827 	if (rdomain < 0 || rdomain > RT_TABLEID_MAX)
1828 		return (EINVAL);
1829 
1830 	if (rdomain != ifp->if_rdomain &&
1831 	    (ifp->if_flags & IFF_LOOPBACK) &&
1832 	    (ifp->if_index == rtable_loindex(ifp->if_rdomain)))
1833 		return (EPERM);
1834 
1835 	if (!rtable_exists(rdomain))
1836 		return (ESRCH);
1837 
1838 	/* make sure that the routing table is a real rdomain */
1839 	if (rdomain != rtable_l2(rdomain))
1840 		return (EINVAL);
1841 
1842 	if (rdomain != ifp->if_rdomain) {
1843 		s = splnet();
1844 		/*
1845 		 * We are tearing down the world.
1846 		 * Take down the IF so:
1847 		 * 1. everything that cares gets a message
1848 		 * 2. the automagic IPv6 bits are recreated
1849 		 */
1850 		if (ifp->if_flags & IFF_UP) {
1851 			up = 1;
1852 			if_down(ifp);
1853 		}
1854 		rti_delete(ifp);
1855 #ifdef MROUTING
1856 		vif_delete(ifp);
1857 #endif
1858 		in_ifdetach(ifp);
1859 #ifdef INET6
1860 		in6_ifdetach(ifp);
1861 #endif
1862 		splx(s);
1863 	}
1864 
1865 	/* Let devices like enc(4) or mpe(4) know about the change */
1866 	ifr.ifr_rdomainid = rdomain;
1867 	if ((error = (*ifp->if_ioctl)(ifp, SIOCSIFRDOMAIN,
1868 	    (caddr_t)&ifr)) != ENOTTY)
1869 		return (error);
1870 	error = 0;
1871 
1872 	/* Add interface to the specified rdomain */
1873 	ifp->if_rdomain = rdomain;
1874 
1875 	/* If we took down the IF, bring it back */
1876 	if (up) {
1877 		s = splnet();
1878 		if_up(ifp);
1879 		splx(s);
1880 	}
1881 
1882 	return (0);
1883 }
1884 
1885 /*
1886  * Interface ioctls.
1887  */
1888 int
1889 ifioctl(struct socket *so, u_long cmd, caddr_t data, struct proc *p)
1890 {
1891 	struct ifnet *ifp;
1892 	struct ifreq *ifr = (struct ifreq *)data;
1893 	struct ifgroupreq *ifgr = (struct ifgroupreq *)data;
1894 	struct if_afreq *ifar = (struct if_afreq *)data;
1895 	char ifdescrbuf[IFDESCRSIZE];
1896 	char ifrtlabelbuf[RTLABEL_LEN];
1897 	int s, error = 0, oif_xflags;
1898 	size_t bytesdone;
1899 	unsigned short oif_flags;
1900 
1901 	switch (cmd) {
1902 	case SIOCIFCREATE:
1903 		if ((error = suser(p)) != 0)
1904 			return (error);
1905 		error = if_clone_create(ifr->ifr_name, 0);
1906 		return (error);
1907 	case SIOCIFDESTROY:
1908 		if ((error = suser(p)) != 0)
1909 			return (error);
1910 		error = if_clone_destroy(ifr->ifr_name);
1911 		return (error);
1912 	case SIOCSIFGATTR:
1913 		if ((error = suser(p)) != 0)
1914 			return (error);
1915 		NET_LOCK();
1916 		error = if_setgroupattribs(data);
1917 		NET_UNLOCK();
1918 		return (error);
1919 	case SIOCGIFCONF:
1920 	case SIOCIFGCLONERS:
1921 	case SIOCGIFGMEMB:
1922 	case SIOCGIFGATTR:
1923 	case SIOCGIFGLIST:
1924 	case SIOCGIFFLAGS:
1925 	case SIOCGIFXFLAGS:
1926 	case SIOCGIFMETRIC:
1927 	case SIOCGIFMTU:
1928 	case SIOCGIFHARDMTU:
1929 	case SIOCGIFDATA:
1930 	case SIOCGIFDESCR:
1931 	case SIOCGIFRTLABEL:
1932 	case SIOCGIFPRIORITY:
1933 	case SIOCGIFRDOMAIN:
1934 	case SIOCGIFGROUP:
1935 	case SIOCGIFLLPRIO:
1936 		return (ifioctl_get(cmd, data));
1937 	}
1938 
1939 	ifp = if_unit(ifr->ifr_name);
1940 	if (ifp == NULL)
1941 		return (ENXIO);
1942 	oif_flags = ifp->if_flags;
1943 	oif_xflags = ifp->if_xflags;
1944 
1945 	switch (cmd) {
1946 	case SIOCIFAFATTACH:
1947 	case SIOCIFAFDETACH:
1948 		if ((error = suser(p)) != 0)
1949 			break;
1950 		NET_LOCK();
1951 		switch (ifar->ifar_af) {
1952 		case AF_INET:
1953 			/* attach is a noop for AF_INET */
1954 			if (cmd == SIOCIFAFDETACH)
1955 				in_ifdetach(ifp);
1956 			break;
1957 #ifdef INET6
1958 		case AF_INET6:
1959 			if (cmd == SIOCIFAFATTACH)
1960 				error = in6_ifattach(ifp);
1961 			else
1962 				in6_ifdetach(ifp);
1963 			break;
1964 #endif /* INET6 */
1965 		default:
1966 			error = EAFNOSUPPORT;
1967 		}
1968 		NET_UNLOCK();
1969 		break;
1970 
1971 	case SIOCSIFXFLAGS:
1972 		if ((error = suser(p)) != 0)
1973 			break;
1974 
1975 		NET_LOCK();
1976 #ifdef INET6
1977 		if ((ISSET(ifr->ifr_flags, IFXF_AUTOCONF6) ||
1978 		    ISSET(ifr->ifr_flags, IFXF_AUTOCONF6TEMP)) &&
1979 		    !ISSET(ifp->if_xflags, IFXF_AUTOCONF6) &&
1980 		    !ISSET(ifp->if_xflags, IFXF_AUTOCONF6TEMP)) {
1981 			error = in6_ifattach(ifp);
1982 			if (error != 0) {
1983 				NET_UNLOCK();
1984 				break;
1985 			}
1986 		}
1987 
1988 		if (ISSET(ifr->ifr_flags, IFXF_INET6_NOSOII) &&
1989 		    !ISSET(ifp->if_xflags, IFXF_INET6_NOSOII))
1990 			ifp->if_xflags |= IFXF_INET6_NOSOII;
1991 
1992 		if (!ISSET(ifr->ifr_flags, IFXF_INET6_NOSOII) &&
1993 		    ISSET(ifp->if_xflags, IFXF_INET6_NOSOII))
1994 			ifp->if_xflags &= ~IFXF_INET6_NOSOII;
1995 
1996 #endif	/* INET6 */
1997 
1998 #ifdef MPLS
1999 		if (ISSET(ifr->ifr_flags, IFXF_MPLS) &&
2000 		    !ISSET(ifp->if_xflags, IFXF_MPLS)) {
2001 			s = splnet();
2002 			ifp->if_xflags |= IFXF_MPLS;
2003 			ifp->if_ll_output = ifp->if_output;
2004 			ifp->if_output = mpls_output;
2005 			splx(s);
2006 		}
2007 		if (ISSET(ifp->if_xflags, IFXF_MPLS) &&
2008 		    !ISSET(ifr->ifr_flags, IFXF_MPLS)) {
2009 			s = splnet();
2010 			ifp->if_xflags &= ~IFXF_MPLS;
2011 			ifp->if_output = ifp->if_ll_output;
2012 			ifp->if_ll_output = NULL;
2013 			splx(s);
2014 		}
2015 #endif	/* MPLS */
2016 
2017 #ifndef SMALL_KERNEL
2018 		if (ifp->if_capabilities & IFCAP_WOL) {
2019 			if (ISSET(ifr->ifr_flags, IFXF_WOL) &&
2020 			    !ISSET(ifp->if_xflags, IFXF_WOL)) {
2021 				s = splnet();
2022 				ifp->if_xflags |= IFXF_WOL;
2023 				error = ifp->if_wol(ifp, 1);
2024 				splx(s);
2025 			}
2026 			if (ISSET(ifp->if_xflags, IFXF_WOL) &&
2027 			    !ISSET(ifr->ifr_flags, IFXF_WOL)) {
2028 				s = splnet();
2029 				ifp->if_xflags &= ~IFXF_WOL;
2030 				error = ifp->if_wol(ifp, 0);
2031 				splx(s);
2032 			}
2033 		} else if (ISSET(ifr->ifr_flags, IFXF_WOL)) {
2034 			ifr->ifr_flags &= ~IFXF_WOL;
2035 			error = ENOTSUP;
2036 		}
2037 
2038 		if (ISSET(ifp->if_capabilities, IFCAP_TSO) &&
2039 		    ISSET(ifr->ifr_flags, IFXF_TSO) !=
2040 		    ISSET(ifp->if_xflags, IFXF_TSO)) {
2041 			struct ifreq ifrq;
2042 
2043 			s = splnet();
2044 
2045 			if (ISSET(ifr->ifr_flags, IFXF_TSO))
2046 				ifp->if_xflags |= IFXF_TSO;
2047 			else
2048 				ifp->if_xflags &= ~IFXF_TSO;
2049 
2050 			NET_ASSERT_LOCKED();	/* for ioctl */
2051 			KERNEL_ASSERT_LOCKED();	/* for if_flags */
2052 
2053 			if (ISSET(ifp->if_flags, IFF_UP)) {
2054 				/* go down for a moment... */
2055 				ifp->if_flags &= ~IFF_UP;
2056 				ifrq.ifr_flags = ifp->if_flags;
2057 				(*ifp->if_ioctl)(ifp, SIOCSIFFLAGS,
2058 				    (caddr_t)&ifrq);
2059 
2060 				/* ... and up again */
2061 				ifp->if_flags |= IFF_UP;
2062 				ifrq.ifr_flags = ifp->if_flags;
2063 				(*ifp->if_ioctl)(ifp, SIOCSIFFLAGS,
2064 				    (caddr_t)&ifrq);
2065 			}
2066 
2067 			splx(s);
2068 		} else if (!ISSET(ifp->if_capabilities, IFCAP_TSO) &&
2069 		    ISSET(ifr->ifr_flags, IFXF_TSO)) {
2070 			ifr->ifr_flags &= ~IFXF_TSO;
2071 			error = ENOTSUP;
2072 		}
2073 #endif
2074 
2075 		if (error == 0)
2076 			ifp->if_xflags = (ifp->if_xflags & IFXF_CANTCHANGE) |
2077 				(ifr->ifr_flags & ~IFXF_CANTCHANGE);
2078 
2079 		if (!ISSET(ifp->if_flags, IFF_UP) &&
2080 		    ((!ISSET(oif_xflags, IFXF_AUTOCONF4) &&
2081 		    ISSET(ifp->if_xflags, IFXF_AUTOCONF4)) ||
2082 		    (!ISSET(oif_xflags, IFXF_AUTOCONF6) &&
2083 		    ISSET(ifp->if_xflags, IFXF_AUTOCONF6)) ||
2084 		    (!ISSET(oif_xflags, IFXF_AUTOCONF6TEMP) &&
2085 		    ISSET(ifp->if_xflags, IFXF_AUTOCONF6TEMP)))) {
2086 			ifr->ifr_flags = ifp->if_flags | IFF_UP;
2087 			goto forceup;
2088 		}
2089 
2090 		NET_UNLOCK();
2091 		break;
2092 
2093 	case SIOCSIFFLAGS:
2094 		if ((error = suser(p)) != 0)
2095 			break;
2096 
2097 		NET_LOCK();
2098 forceup:
2099 		ifp->if_flags = (ifp->if_flags & IFF_CANTCHANGE) |
2100 			(ifr->ifr_flags & ~IFF_CANTCHANGE);
2101 		error = (*ifp->if_ioctl)(ifp, SIOCSIFFLAGS, data);
2102 		if (error != 0) {
2103 			ifp->if_flags = oif_flags;
2104 			if (cmd == SIOCSIFXFLAGS)
2105 				ifp->if_xflags = oif_xflags;
2106 		} else if (ISSET(oif_flags ^ ifp->if_flags, IFF_UP)) {
2107 			s = splnet();
2108 			if (ISSET(ifp->if_flags, IFF_UP))
2109 				if_up(ifp);
2110 			else
2111 				if_down(ifp);
2112 			splx(s);
2113 		}
2114 		NET_UNLOCK();
2115 		break;
2116 
2117 	case SIOCSIFMETRIC:
2118 		if ((error = suser(p)) != 0)
2119 			break;
2120 		NET_LOCK();
2121 		ifp->if_metric = ifr->ifr_metric;
2122 		NET_UNLOCK();
2123 		break;
2124 
2125 	case SIOCSIFMTU:
2126 		if ((error = suser(p)) != 0)
2127 			break;
2128 		NET_LOCK();
2129 		error = (*ifp->if_ioctl)(ifp, cmd, data);
2130 		NET_UNLOCK();
2131 		if (error == 0)
2132 			rtm_ifchg(ifp);
2133 		break;
2134 
2135 	case SIOCSIFDESCR:
2136 		if ((error = suser(p)) != 0)
2137 			break;
2138 		error = copyinstr(ifr->ifr_data, ifdescrbuf,
2139 		    IFDESCRSIZE, &bytesdone);
2140 		if (error == 0) {
2141 			(void)memset(ifp->if_description, 0, IFDESCRSIZE);
2142 			strlcpy(ifp->if_description, ifdescrbuf, IFDESCRSIZE);
2143 		}
2144 		break;
2145 
2146 	case SIOCSIFRTLABEL:
2147 		if ((error = suser(p)) != 0)
2148 			break;
2149 		error = copyinstr(ifr->ifr_data, ifrtlabelbuf,
2150 		    RTLABEL_LEN, &bytesdone);
2151 		if (error == 0) {
2152 			rtlabel_unref(ifp->if_rtlabelid);
2153 			ifp->if_rtlabelid = rtlabel_name2id(ifrtlabelbuf);
2154 		}
2155 		break;
2156 
2157 	case SIOCSIFPRIORITY:
2158 		if ((error = suser(p)) != 0)
2159 			break;
2160 		if (ifr->ifr_metric < 0 || ifr->ifr_metric > 15) {
2161 			error = EINVAL;
2162 			break;
2163 		}
2164 		ifp->if_priority = ifr->ifr_metric;
2165 		break;
2166 
2167 	case SIOCSIFRDOMAIN:
2168 		if ((error = suser(p)) != 0)
2169 			break;
2170 		error = if_createrdomain(ifr->ifr_rdomainid, ifp);
2171 		if (!error || error == EEXIST) {
2172 			NET_LOCK();
2173 			error = if_setrdomain(ifp, ifr->ifr_rdomainid);
2174 			NET_UNLOCK();
2175 		}
2176 		break;
2177 
2178 	case SIOCAIFGROUP:
2179 		if ((error = suser(p)))
2180 			break;
2181 		NET_LOCK();
2182 		error = if_addgroup(ifp, ifgr->ifgr_group);
2183 		if (error == 0) {
2184 			error = (*ifp->if_ioctl)(ifp, cmd, data);
2185 			if (error == ENOTTY)
2186 				error = 0;
2187 		}
2188 		NET_UNLOCK();
2189 		break;
2190 
2191 	case SIOCDIFGROUP:
2192 		if ((error = suser(p)))
2193 			break;
2194 		NET_LOCK();
2195 		error = (*ifp->if_ioctl)(ifp, cmd, data);
2196 		if (error == ENOTTY)
2197 			error = 0;
2198 		if (error == 0)
2199 			error = if_delgroup(ifp, ifgr->ifgr_group);
2200 		NET_UNLOCK();
2201 		break;
2202 
2203 	case SIOCSIFLLADDR:
2204 		if ((error = suser(p)))
2205 			break;
2206 		if ((ifp->if_sadl == NULL) ||
2207 		    (ifr->ifr_addr.sa_len != ETHER_ADDR_LEN) ||
2208 		    (ETHER_IS_MULTICAST(ifr->ifr_addr.sa_data))) {
2209 			error = EINVAL;
2210 			break;
2211 		}
2212 		NET_LOCK();
2213 		switch (ifp->if_type) {
2214 		case IFT_ETHER:
2215 		case IFT_CARP:
2216 		case IFT_XETHER:
2217 		case IFT_ISO88025:
2218 			error = (*ifp->if_ioctl)(ifp, cmd, data);
2219 			if (error == ENOTTY)
2220 				error = 0;
2221 			if (error == 0)
2222 				error = if_setlladdr(ifp,
2223 				    ifr->ifr_addr.sa_data);
2224 			break;
2225 		default:
2226 			error = ENODEV;
2227 		}
2228 
2229 		if (error == 0)
2230 			ifnewlladdr(ifp);
2231 		NET_UNLOCK();
2232 		if (error == 0)
2233 			rtm_ifchg(ifp);
2234 		break;
2235 
2236 	case SIOCSIFLLPRIO:
2237 		if ((error = suser(p)))
2238 			break;
2239 		if (ifr->ifr_llprio < IFQ_MINPRIO ||
2240 		    ifr->ifr_llprio > IFQ_MAXPRIO) {
2241 			error = EINVAL;
2242 			break;
2243 		}
2244 		NET_LOCK();
2245 		ifp->if_llprio = ifr->ifr_llprio;
2246 		NET_UNLOCK();
2247 		break;
2248 
2249 	case SIOCGIFSFFPAGE:
2250 		error = suser(p);
2251 		if (error != 0)
2252 			break;
2253 
2254 		error = if_sffpage_check(data);
2255 		if (error != 0)
2256 			break;
2257 
2258 		/* don't take NET_LOCK because i2c reads take a long time */
2259 		error = ((*ifp->if_ioctl)(ifp, cmd, data));
2260 		break;
2261 
2262 	case SIOCSETKALIVE:
2263 	case SIOCDIFPHYADDR:
2264 	case SIOCSLIFPHYADDR:
2265 	case SIOCSLIFPHYRTABLE:
2266 	case SIOCSLIFPHYTTL:
2267 	case SIOCSLIFPHYDF:
2268 	case SIOCSLIFPHYECN:
2269 	case SIOCADDMULTI:
2270 	case SIOCDELMULTI:
2271 	case SIOCSIFMEDIA:
2272 	case SIOCSVNETID:
2273 	case SIOCDVNETID:
2274 	case SIOCSVNETFLOWID:
2275 	case SIOCSTXHPRIO:
2276 	case SIOCSRXHPRIO:
2277 	case SIOCSIFPAIR:
2278 	case SIOCSIFPARENT:
2279 	case SIOCDIFPARENT:
2280 	case SIOCSETMPWCFG:
2281 	case SIOCSETLABEL:
2282 	case SIOCDELLABEL:
2283 	case SIOCSPWE3CTRLWORD:
2284 	case SIOCSPWE3FAT:
2285 	case SIOCSPWE3NEIGHBOR:
2286 	case SIOCDPWE3NEIGHBOR:
2287 #if NBRIDGE > 0
2288 	case SIOCBRDGADD:
2289 	case SIOCBRDGDEL:
2290 	case SIOCBRDGSIFFLGS:
2291 	case SIOCBRDGSCACHE:
2292 	case SIOCBRDGADDS:
2293 	case SIOCBRDGDELS:
2294 	case SIOCBRDGSADDR:
2295 	case SIOCBRDGSTO:
2296 	case SIOCBRDGDADDR:
2297 	case SIOCBRDGFLUSH:
2298 	case SIOCBRDGADDL:
2299 	case SIOCBRDGSIFPROT:
2300 	case SIOCBRDGARL:
2301 	case SIOCBRDGFRL:
2302 	case SIOCBRDGSPRI:
2303 	case SIOCBRDGSHT:
2304 	case SIOCBRDGSFD:
2305 	case SIOCBRDGSMA:
2306 	case SIOCBRDGSIFPRIO:
2307 	case SIOCBRDGSIFCOST:
2308 	case SIOCBRDGSTXHC:
2309 	case SIOCBRDGSPROTO:
2310 #endif
2311 		if ((error = suser(p)) != 0)
2312 			break;
2313 		/* FALLTHROUGH */
2314 	default:
2315 		error = ((*so->so_proto->pr_usrreq)(so, PRU_CONTROL,
2316 			(struct mbuf *) cmd, (struct mbuf *) data,
2317 			(struct mbuf *) ifp, p));
2318 		if (error != EOPNOTSUPP)
2319 			break;
2320 		switch (cmd) {
2321 		case SIOCAIFADDR:
2322 		case SIOCDIFADDR:
2323 		case SIOCSIFADDR:
2324 		case SIOCSIFNETMASK:
2325 		case SIOCSIFDSTADDR:
2326 		case SIOCSIFBRDADDR:
2327 #ifdef INET6
2328 		case SIOCAIFADDR_IN6:
2329 		case SIOCDIFADDR_IN6:
2330 #endif
2331 			error = suser(p);
2332 			break;
2333 		default:
2334 			error = 0;
2335 			break;
2336 		}
2337 		if (error)
2338 			break;
2339 		NET_LOCK();
2340 		error = ((*ifp->if_ioctl)(ifp, cmd, data));
2341 		NET_UNLOCK();
2342 		break;
2343 	}
2344 
2345 	if (oif_flags != ifp->if_flags || oif_xflags != ifp->if_xflags) {
2346 		/* if_up() and if_down() already sent an update, skip here */
2347 		if (((oif_flags ^ ifp->if_flags) & IFF_UP) == 0)
2348 			rtm_ifchg(ifp);
2349 	}
2350 
2351 	if (((oif_flags ^ ifp->if_flags) & IFF_UP) != 0)
2352 		getmicrotime(&ifp->if_lastchange);
2353 
2354 	if_put(ifp);
2355 
2356 	return (error);
2357 }
2358 
2359 int
2360 ifioctl_get(u_long cmd, caddr_t data)
2361 {
2362 	struct ifnet *ifp;
2363 	struct ifreq *ifr = (struct ifreq *)data;
2364 	char ifdescrbuf[IFDESCRSIZE];
2365 	char ifrtlabelbuf[RTLABEL_LEN];
2366 	int error = 0;
2367 	size_t bytesdone;
2368 	const char *label;
2369 
2370 	switch(cmd) {
2371 	case SIOCGIFCONF:
2372 		NET_RLOCK_IN_IOCTL();
2373 		error = ifconf(data);
2374 		NET_RUNLOCK_IN_IOCTL();
2375 		return (error);
2376 	case SIOCIFGCLONERS:
2377 		error = if_clone_list((struct if_clonereq *)data);
2378 		return (error);
2379 	case SIOCGIFGMEMB:
2380 		NET_RLOCK_IN_IOCTL();
2381 		error = if_getgroupmembers(data);
2382 		NET_RUNLOCK_IN_IOCTL();
2383 		return (error);
2384 	case SIOCGIFGATTR:
2385 		NET_RLOCK_IN_IOCTL();
2386 		error = if_getgroupattribs(data);
2387 		NET_RUNLOCK_IN_IOCTL();
2388 		return (error);
2389 	case SIOCGIFGLIST:
2390 		NET_RLOCK_IN_IOCTL();
2391 		error = if_getgrouplist(data);
2392 		NET_RUNLOCK_IN_IOCTL();
2393 		return (error);
2394 	}
2395 
2396 	ifp = if_unit(ifr->ifr_name);
2397 	if (ifp == NULL)
2398 		return (ENXIO);
2399 
2400 	NET_RLOCK_IN_IOCTL();
2401 
2402 	switch(cmd) {
2403 	case SIOCGIFFLAGS:
2404 		ifr->ifr_flags = ifp->if_flags;
2405 		if (ifq_is_oactive(&ifp->if_snd))
2406 			ifr->ifr_flags |= IFF_OACTIVE;
2407 		break;
2408 
2409 	case SIOCGIFXFLAGS:
2410 		ifr->ifr_flags = ifp->if_xflags & ~(IFXF_MPSAFE|IFXF_CLONED);
2411 		break;
2412 
2413 	case SIOCGIFMETRIC:
2414 		ifr->ifr_metric = ifp->if_metric;
2415 		break;
2416 
2417 	case SIOCGIFMTU:
2418 		ifr->ifr_mtu = ifp->if_mtu;
2419 		break;
2420 
2421 	case SIOCGIFHARDMTU:
2422 		ifr->ifr_hardmtu = ifp->if_hardmtu;
2423 		break;
2424 
2425 	case SIOCGIFDATA: {
2426 		struct if_data ifdata;
2427 		if_getdata(ifp, &ifdata);
2428 		error = copyout(&ifdata, ifr->ifr_data, sizeof(ifdata));
2429 		break;
2430 	}
2431 
2432 	case SIOCGIFDESCR:
2433 		strlcpy(ifdescrbuf, ifp->if_description, IFDESCRSIZE);
2434 		error = copyoutstr(ifdescrbuf, ifr->ifr_data, IFDESCRSIZE,
2435 		    &bytesdone);
2436 		break;
2437 
2438 	case SIOCGIFRTLABEL:
2439 		if (ifp->if_rtlabelid &&
2440 		    (label = rtlabel_id2name(ifp->if_rtlabelid)) != NULL) {
2441 			strlcpy(ifrtlabelbuf, label, RTLABEL_LEN);
2442 			error = copyoutstr(ifrtlabelbuf, ifr->ifr_data,
2443 			    RTLABEL_LEN, &bytesdone);
2444 		} else
2445 			error = ENOENT;
2446 		break;
2447 
2448 	case SIOCGIFPRIORITY:
2449 		ifr->ifr_metric = ifp->if_priority;
2450 		break;
2451 
2452 	case SIOCGIFRDOMAIN:
2453 		ifr->ifr_rdomainid = ifp->if_rdomain;
2454 		break;
2455 
2456 	case SIOCGIFGROUP:
2457 		error = if_getgroup(data, ifp);
2458 		break;
2459 
2460 	case SIOCGIFLLPRIO:
2461 		ifr->ifr_llprio = ifp->if_llprio;
2462 		break;
2463 
2464 	default:
2465 		panic("invalid ioctl %lu", cmd);
2466 	}
2467 
2468 	NET_RUNLOCK_IN_IOCTL();
2469 
2470 	if_put(ifp);
2471 
2472 	return (error);
2473 }
2474 
2475 static int
2476 if_sffpage_check(const caddr_t data)
2477 {
2478 	const struct if_sffpage *sff = (const struct if_sffpage *)data;
2479 
2480 	switch (sff->sff_addr) {
2481 	case IFSFF_ADDR_EEPROM:
2482 	case IFSFF_ADDR_DDM:
2483 		break;
2484 	default:
2485 		return (EINVAL);
2486 	}
2487 
2488 	return (0);
2489 }
2490 
2491 int
2492 if_txhprio_l2_check(int hdrprio)
2493 {
2494 	switch (hdrprio) {
2495 	case IF_HDRPRIO_PACKET:
2496 		return (0);
2497 	default:
2498 		if (hdrprio >= IF_HDRPRIO_MIN && hdrprio <= IF_HDRPRIO_MAX)
2499 			return (0);
2500 		break;
2501 	}
2502 
2503 	return (EINVAL);
2504 }
2505 
2506 int
2507 if_txhprio_l3_check(int hdrprio)
2508 {
2509 	switch (hdrprio) {
2510 	case IF_HDRPRIO_PACKET:
2511 	case IF_HDRPRIO_PAYLOAD:
2512 		return (0);
2513 	default:
2514 		if (hdrprio >= IF_HDRPRIO_MIN && hdrprio <= IF_HDRPRIO_MAX)
2515 			return (0);
2516 		break;
2517 	}
2518 
2519 	return (EINVAL);
2520 }
2521 
2522 int
2523 if_rxhprio_l2_check(int hdrprio)
2524 {
2525 	switch (hdrprio) {
2526 	case IF_HDRPRIO_PACKET:
2527 	case IF_HDRPRIO_OUTER:
2528 		return (0);
2529 	default:
2530 		if (hdrprio >= IF_HDRPRIO_MIN && hdrprio <= IF_HDRPRIO_MAX)
2531 			return (0);
2532 		break;
2533 	}
2534 
2535 	return (EINVAL);
2536 }
2537 
2538 int
2539 if_rxhprio_l3_check(int hdrprio)
2540 {
2541 	switch (hdrprio) {
2542 	case IF_HDRPRIO_PACKET:
2543 	case IF_HDRPRIO_PAYLOAD:
2544 	case IF_HDRPRIO_OUTER:
2545 		return (0);
2546 	default:
2547 		if (hdrprio >= IF_HDRPRIO_MIN && hdrprio <= IF_HDRPRIO_MAX)
2548 			return (0);
2549 		break;
2550 	}
2551 
2552 	return (EINVAL);
2553 }
2554 
2555 /*
2556  * Return interface configuration
2557  * of system.  List may be used
2558  * in later ioctl's (above) to get
2559  * other information.
2560  */
2561 int
2562 ifconf(caddr_t data)
2563 {
2564 	struct ifconf *ifc = (struct ifconf *)data;
2565 	struct ifnet *ifp;
2566 	struct ifaddr *ifa;
2567 	struct ifreq ifr, *ifrp;
2568 	int space = ifc->ifc_len, error = 0;
2569 
2570 	/* If ifc->ifc_len is 0, fill it in with the needed size and return. */
2571 	if (space == 0) {
2572 		TAILQ_FOREACH(ifp, &ifnet, if_list) {
2573 			struct sockaddr *sa;
2574 
2575 			if (TAILQ_EMPTY(&ifp->if_addrlist))
2576 				space += sizeof (ifr);
2577 			else
2578 				TAILQ_FOREACH(ifa,
2579 				    &ifp->if_addrlist, ifa_list) {
2580 					sa = ifa->ifa_addr;
2581 					if (sa->sa_len > sizeof(*sa))
2582 						space += sa->sa_len -
2583 						    sizeof(*sa);
2584 					space += sizeof(ifr);
2585 				}
2586 		}
2587 		ifc->ifc_len = space;
2588 		return (0);
2589 	}
2590 
2591 	ifrp = ifc->ifc_req;
2592 	TAILQ_FOREACH(ifp, &ifnet, if_list) {
2593 		if (space < sizeof(ifr))
2594 			break;
2595 		bcopy(ifp->if_xname, ifr.ifr_name, IFNAMSIZ);
2596 		if (TAILQ_EMPTY(&ifp->if_addrlist)) {
2597 			bzero((caddr_t)&ifr.ifr_addr, sizeof(ifr.ifr_addr));
2598 			error = copyout((caddr_t)&ifr, (caddr_t)ifrp,
2599 			    sizeof(ifr));
2600 			if (error)
2601 				break;
2602 			space -= sizeof (ifr), ifrp++;
2603 		} else
2604 			TAILQ_FOREACH(ifa, &ifp->if_addrlist, ifa_list) {
2605 				struct sockaddr *sa = ifa->ifa_addr;
2606 
2607 				if (space < sizeof(ifr))
2608 					break;
2609 				if (sa->sa_len <= sizeof(*sa)) {
2610 					ifr.ifr_addr = *sa;
2611 					error = copyout((caddr_t)&ifr,
2612 					    (caddr_t)ifrp, sizeof (ifr));
2613 					ifrp++;
2614 				} else {
2615 					space -= sa->sa_len - sizeof(*sa);
2616 					if (space < sizeof (ifr))
2617 						break;
2618 					error = copyout((caddr_t)&ifr,
2619 					    (caddr_t)ifrp,
2620 					    sizeof(ifr.ifr_name));
2621 					if (error == 0)
2622 						error = copyout((caddr_t)sa,
2623 						    (caddr_t)&ifrp->ifr_addr,
2624 						    sa->sa_len);
2625 					ifrp = (struct ifreq *)(sa->sa_len +
2626 					    (caddr_t)&ifrp->ifr_addr);
2627 				}
2628 				if (error)
2629 					break;
2630 				space -= sizeof (ifr);
2631 			}
2632 	}
2633 	ifc->ifc_len -= space;
2634 	return (error);
2635 }
2636 
2637 void
2638 if_counters_alloc(struct ifnet *ifp)
2639 {
2640 	KASSERT(ifp->if_counters == NULL);
2641 
2642 	ifp->if_counters = counters_alloc(ifc_ncounters);
2643 }
2644 
2645 void
2646 if_counters_free(struct ifnet *ifp)
2647 {
2648 	KASSERT(ifp->if_counters != NULL);
2649 
2650 	counters_free(ifp->if_counters, ifc_ncounters);
2651 	ifp->if_counters = NULL;
2652 }
2653 
2654 void
2655 if_getdata(struct ifnet *ifp, struct if_data *data)
2656 {
2657 	unsigned int i;
2658 
2659 	*data = ifp->if_data;
2660 
2661 	if (ifp->if_counters != NULL) {
2662 		uint64_t counters[ifc_ncounters];
2663 
2664 		counters_read(ifp->if_counters, counters, nitems(counters));
2665 
2666 		data->ifi_ipackets += counters[ifc_ipackets];
2667 		data->ifi_ierrors += counters[ifc_ierrors];
2668 		data->ifi_opackets += counters[ifc_opackets];
2669 		data->ifi_oerrors += counters[ifc_oerrors];
2670 		data->ifi_collisions += counters[ifc_collisions];
2671 		data->ifi_ibytes += counters[ifc_ibytes];
2672 		data->ifi_obytes += counters[ifc_obytes];
2673 		data->ifi_imcasts += counters[ifc_imcasts];
2674 		data->ifi_omcasts += counters[ifc_omcasts];
2675 		data->ifi_iqdrops += counters[ifc_iqdrops];
2676 		data->ifi_oqdrops += counters[ifc_oqdrops];
2677 		data->ifi_noproto += counters[ifc_noproto];
2678 	}
2679 
2680 	for (i = 0; i < ifp->if_nifqs; i++) {
2681 		struct ifqueue *ifq = ifp->if_ifqs[i];
2682 
2683 		ifq_add_data(ifq, data);
2684 	}
2685 
2686 	for (i = 0; i < ifp->if_niqs; i++) {
2687 		struct ifiqueue *ifiq = ifp->if_iqs[i];
2688 
2689 		ifiq_add_data(ifiq, data);
2690 	}
2691 }
2692 
2693 /*
2694  * Dummy functions replaced in ifnet during detach (if protocols decide to
2695  * fiddle with the if during detach.
2696  */
2697 void
2698 if_detached_qstart(struct ifqueue *ifq)
2699 {
2700 	ifq_purge(ifq);
2701 }
2702 
2703 int
2704 if_detached_ioctl(struct ifnet *ifp, u_long a, caddr_t b)
2705 {
2706 	return ENODEV;
2707 }
2708 
2709 /*
2710  * Create interface group without members
2711  */
2712 struct ifg_group *
2713 if_creategroup(const char *groupname)
2714 {
2715 	struct ifg_group	*ifg;
2716 
2717 	if ((ifg = malloc(sizeof(*ifg), M_TEMP, M_NOWAIT)) == NULL)
2718 		return (NULL);
2719 
2720 	strlcpy(ifg->ifg_group, groupname, sizeof(ifg->ifg_group));
2721 	ifg->ifg_refcnt = 1;
2722 	ifg->ifg_carp_demoted = 0;
2723 	TAILQ_INIT(&ifg->ifg_members);
2724 #if NPF > 0
2725 	pfi_attach_ifgroup(ifg);
2726 #endif
2727 	TAILQ_INSERT_TAIL(&ifg_head, ifg, ifg_next);
2728 
2729 	return (ifg);
2730 }
2731 
2732 /*
2733  * Add a group to an interface
2734  */
2735 int
2736 if_addgroup(struct ifnet *ifp, const char *groupname)
2737 {
2738 	struct ifg_list		*ifgl;
2739 	struct ifg_group	*ifg = NULL;
2740 	struct ifg_member	*ifgm;
2741 	size_t			 namelen;
2742 
2743 	namelen = strlen(groupname);
2744 	if (namelen == 0 || namelen >= IFNAMSIZ ||
2745 	    (groupname[namelen - 1] >= '0' && groupname[namelen - 1] <= '9'))
2746 		return (EINVAL);
2747 
2748 	TAILQ_FOREACH(ifgl, &ifp->if_groups, ifgl_next)
2749 		if (!strcmp(ifgl->ifgl_group->ifg_group, groupname))
2750 			return (EEXIST);
2751 
2752 	if ((ifgl = malloc(sizeof(*ifgl), M_TEMP, M_NOWAIT)) == NULL)
2753 		return (ENOMEM);
2754 
2755 	if ((ifgm = malloc(sizeof(*ifgm), M_TEMP, M_NOWAIT)) == NULL) {
2756 		free(ifgl, M_TEMP, sizeof(*ifgl));
2757 		return (ENOMEM);
2758 	}
2759 
2760 	TAILQ_FOREACH(ifg, &ifg_head, ifg_next)
2761 		if (!strcmp(ifg->ifg_group, groupname))
2762 			break;
2763 
2764 	if (ifg == NULL) {
2765 		ifg = if_creategroup(groupname);
2766 		if (ifg == NULL) {
2767 			free(ifgl, M_TEMP, sizeof(*ifgl));
2768 			free(ifgm, M_TEMP, sizeof(*ifgm));
2769 			return (ENOMEM);
2770 		}
2771 	} else
2772 		ifg->ifg_refcnt++;
2773 	KASSERT(ifg->ifg_refcnt != 0);
2774 
2775 	ifgl->ifgl_group = ifg;
2776 	ifgm->ifgm_ifp = ifp;
2777 
2778 	TAILQ_INSERT_TAIL(&ifg->ifg_members, ifgm, ifgm_next);
2779 	TAILQ_INSERT_TAIL(&ifp->if_groups, ifgl, ifgl_next);
2780 
2781 #if NPF > 0
2782 	pfi_group_addmember(groupname);
2783 #endif
2784 
2785 	return (0);
2786 }
2787 
2788 /*
2789  * Remove a group from an interface
2790  */
2791 int
2792 if_delgroup(struct ifnet *ifp, const char *groupname)
2793 {
2794 	struct ifg_list		*ifgl;
2795 	struct ifg_member	*ifgm;
2796 
2797 	TAILQ_FOREACH(ifgl, &ifp->if_groups, ifgl_next)
2798 		if (!strcmp(ifgl->ifgl_group->ifg_group, groupname))
2799 			break;
2800 	if (ifgl == NULL)
2801 		return (ENOENT);
2802 
2803 	TAILQ_REMOVE(&ifp->if_groups, ifgl, ifgl_next);
2804 
2805 	TAILQ_FOREACH(ifgm, &ifgl->ifgl_group->ifg_members, ifgm_next)
2806 		if (ifgm->ifgm_ifp == ifp)
2807 			break;
2808 
2809 	if (ifgm != NULL) {
2810 		TAILQ_REMOVE(&ifgl->ifgl_group->ifg_members, ifgm, ifgm_next);
2811 		free(ifgm, M_TEMP, sizeof(*ifgm));
2812 	}
2813 
2814 #if NPF > 0
2815 	pfi_group_delmember(groupname);
2816 #endif
2817 
2818 	KASSERT(ifgl->ifgl_group->ifg_refcnt != 0);
2819 	if (--ifgl->ifgl_group->ifg_refcnt == 0) {
2820 		TAILQ_REMOVE(&ifg_head, ifgl->ifgl_group, ifg_next);
2821 #if NPF > 0
2822 		pfi_detach_ifgroup(ifgl->ifgl_group);
2823 #endif
2824 		free(ifgl->ifgl_group, M_TEMP, sizeof(*ifgl->ifgl_group));
2825 	}
2826 
2827 	free(ifgl, M_TEMP, sizeof(*ifgl));
2828 
2829 	return (0);
2830 }
2831 
2832 /*
2833  * Stores all groups from an interface in memory pointed
2834  * to by data
2835  */
2836 int
2837 if_getgroup(caddr_t data, struct ifnet *ifp)
2838 {
2839 	int			 len, error;
2840 	struct ifg_list		*ifgl;
2841 	struct ifg_req		 ifgrq, *ifgp;
2842 	struct ifgroupreq	*ifgr = (struct ifgroupreq *)data;
2843 
2844 	if (ifgr->ifgr_len == 0) {
2845 		TAILQ_FOREACH(ifgl, &ifp->if_groups, ifgl_next)
2846 			ifgr->ifgr_len += sizeof(struct ifg_req);
2847 		return (0);
2848 	}
2849 
2850 	len = ifgr->ifgr_len;
2851 	ifgp = ifgr->ifgr_groups;
2852 	TAILQ_FOREACH(ifgl, &ifp->if_groups, ifgl_next) {
2853 		if (len < sizeof(ifgrq))
2854 			return (EINVAL);
2855 		bzero(&ifgrq, sizeof ifgrq);
2856 		strlcpy(ifgrq.ifgrq_group, ifgl->ifgl_group->ifg_group,
2857 		    sizeof(ifgrq.ifgrq_group));
2858 		if ((error = copyout((caddr_t)&ifgrq, (caddr_t)ifgp,
2859 		    sizeof(struct ifg_req))))
2860 			return (error);
2861 		len -= sizeof(ifgrq);
2862 		ifgp++;
2863 	}
2864 
2865 	return (0);
2866 }
2867 
2868 /*
2869  * Stores all members of a group in memory pointed to by data
2870  */
2871 int
2872 if_getgroupmembers(caddr_t data)
2873 {
2874 	struct ifgroupreq	*ifgr = (struct ifgroupreq *)data;
2875 	struct ifg_group	*ifg;
2876 	struct ifg_member	*ifgm;
2877 	struct ifg_req		 ifgrq, *ifgp;
2878 	int			 len, error;
2879 
2880 	TAILQ_FOREACH(ifg, &ifg_head, ifg_next)
2881 		if (!strcmp(ifg->ifg_group, ifgr->ifgr_name))
2882 			break;
2883 	if (ifg == NULL)
2884 		return (ENOENT);
2885 
2886 	if (ifgr->ifgr_len == 0) {
2887 		TAILQ_FOREACH(ifgm, &ifg->ifg_members, ifgm_next)
2888 			ifgr->ifgr_len += sizeof(ifgrq);
2889 		return (0);
2890 	}
2891 
2892 	len = ifgr->ifgr_len;
2893 	ifgp = ifgr->ifgr_groups;
2894 	TAILQ_FOREACH(ifgm, &ifg->ifg_members, ifgm_next) {
2895 		if (len < sizeof(ifgrq))
2896 			return (EINVAL);
2897 		bzero(&ifgrq, sizeof ifgrq);
2898 		strlcpy(ifgrq.ifgrq_member, ifgm->ifgm_ifp->if_xname,
2899 		    sizeof(ifgrq.ifgrq_member));
2900 		if ((error = copyout((caddr_t)&ifgrq, (caddr_t)ifgp,
2901 		    sizeof(struct ifg_req))))
2902 			return (error);
2903 		len -= sizeof(ifgrq);
2904 		ifgp++;
2905 	}
2906 
2907 	return (0);
2908 }
2909 
2910 int
2911 if_getgroupattribs(caddr_t data)
2912 {
2913 	struct ifgroupreq	*ifgr = (struct ifgroupreq *)data;
2914 	struct ifg_group	*ifg;
2915 
2916 	TAILQ_FOREACH(ifg, &ifg_head, ifg_next)
2917 		if (!strcmp(ifg->ifg_group, ifgr->ifgr_name))
2918 			break;
2919 	if (ifg == NULL)
2920 		return (ENOENT);
2921 
2922 	ifgr->ifgr_attrib.ifg_carp_demoted = ifg->ifg_carp_demoted;
2923 
2924 	return (0);
2925 }
2926 
2927 int
2928 if_setgroupattribs(caddr_t data)
2929 {
2930 	struct ifgroupreq	*ifgr = (struct ifgroupreq *)data;
2931 	struct ifg_group	*ifg;
2932 	struct ifg_member	*ifgm;
2933 	int			 demote;
2934 
2935 	TAILQ_FOREACH(ifg, &ifg_head, ifg_next)
2936 		if (!strcmp(ifg->ifg_group, ifgr->ifgr_name))
2937 			break;
2938 	if (ifg == NULL)
2939 		return (ENOENT);
2940 
2941 	demote = ifgr->ifgr_attrib.ifg_carp_demoted;
2942 	if (demote + ifg->ifg_carp_demoted > 0xff ||
2943 	    demote + ifg->ifg_carp_demoted < 0)
2944 		return (EINVAL);
2945 
2946 	ifg->ifg_carp_demoted += demote;
2947 
2948 	TAILQ_FOREACH(ifgm, &ifg->ifg_members, ifgm_next)
2949 		ifgm->ifgm_ifp->if_ioctl(ifgm->ifgm_ifp, SIOCSIFGATTR, data);
2950 
2951 	return (0);
2952 }
2953 
2954 /*
2955  * Stores all groups in memory pointed to by data
2956  */
2957 int
2958 if_getgrouplist(caddr_t data)
2959 {
2960 	struct ifgroupreq	*ifgr = (struct ifgroupreq *)data;
2961 	struct ifg_group	*ifg;
2962 	struct ifg_req		 ifgrq, *ifgp;
2963 	int			 len, error;
2964 
2965 	if (ifgr->ifgr_len == 0) {
2966 		TAILQ_FOREACH(ifg, &ifg_head, ifg_next)
2967 			ifgr->ifgr_len += sizeof(ifgrq);
2968 		return (0);
2969 	}
2970 
2971 	len = ifgr->ifgr_len;
2972 	ifgp = ifgr->ifgr_groups;
2973 	TAILQ_FOREACH(ifg, &ifg_head, ifg_next) {
2974 		if (len < sizeof(ifgrq))
2975 			return (EINVAL);
2976 		bzero(&ifgrq, sizeof ifgrq);
2977 		strlcpy(ifgrq.ifgrq_group, ifg->ifg_group,
2978 		    sizeof(ifgrq.ifgrq_group));
2979 		if ((error = copyout((caddr_t)&ifgrq, (caddr_t)ifgp,
2980 		    sizeof(struct ifg_req))))
2981 			return (error);
2982 		len -= sizeof(ifgrq);
2983 		ifgp++;
2984 	}
2985 
2986 	return (0);
2987 }
2988 
2989 void
2990 if_group_routechange(struct sockaddr *dst, struct sockaddr *mask)
2991 {
2992 	switch (dst->sa_family) {
2993 	case AF_INET:
2994 		if (satosin(dst)->sin_addr.s_addr == INADDR_ANY &&
2995 		    mask && (mask->sa_len == 0 ||
2996 		    satosin(mask)->sin_addr.s_addr == INADDR_ANY))
2997 			if_group_egress_build();
2998 		break;
2999 #ifdef INET6
3000 	case AF_INET6:
3001 		if (IN6_ARE_ADDR_EQUAL(&(satosin6(dst))->sin6_addr,
3002 		    &in6addr_any) && mask && (mask->sa_len == 0 ||
3003 		    IN6_ARE_ADDR_EQUAL(&(satosin6(mask))->sin6_addr,
3004 		    &in6addr_any)))
3005 			if_group_egress_build();
3006 		break;
3007 #endif
3008 	}
3009 }
3010 
3011 int
3012 if_group_egress_build(void)
3013 {
3014 	struct ifnet		*ifp;
3015 	struct ifg_group	*ifg;
3016 	struct ifg_member	*ifgm, *next;
3017 	struct sockaddr_in	 sa_in;
3018 #ifdef INET6
3019 	struct sockaddr_in6	 sa_in6;
3020 #endif
3021 	struct rtentry		*rt;
3022 
3023 	TAILQ_FOREACH(ifg, &ifg_head, ifg_next)
3024 		if (!strcmp(ifg->ifg_group, IFG_EGRESS))
3025 			break;
3026 
3027 	if (ifg != NULL)
3028 		TAILQ_FOREACH_SAFE(ifgm, &ifg->ifg_members, ifgm_next, next)
3029 			if_delgroup(ifgm->ifgm_ifp, IFG_EGRESS);
3030 
3031 	bzero(&sa_in, sizeof(sa_in));
3032 	sa_in.sin_len = sizeof(sa_in);
3033 	sa_in.sin_family = AF_INET;
3034 	rt = rtable_lookup(0, sintosa(&sa_in), sintosa(&sa_in), NULL, RTP_ANY);
3035 	while (rt != NULL) {
3036 		ifp = if_get(rt->rt_ifidx);
3037 		if (ifp != NULL) {
3038 			if_addgroup(ifp, IFG_EGRESS);
3039 			if_put(ifp);
3040 		}
3041 		rt = rtable_iterate(rt);
3042 	}
3043 
3044 #ifdef INET6
3045 	bcopy(&sa6_any, &sa_in6, sizeof(sa_in6));
3046 	rt = rtable_lookup(0, sin6tosa(&sa_in6), sin6tosa(&sa_in6), NULL,
3047 	    RTP_ANY);
3048 	while (rt != NULL) {
3049 		ifp = if_get(rt->rt_ifidx);
3050 		if (ifp != NULL) {
3051 			if_addgroup(ifp, IFG_EGRESS);
3052 			if_put(ifp);
3053 		}
3054 		rt = rtable_iterate(rt);
3055 	}
3056 #endif /* INET6 */
3057 
3058 	return (0);
3059 }
3060 
3061 /*
3062  * Set/clear promiscuous mode on interface ifp based on the truth value
3063  * of pswitch.  The calls are reference counted so that only the first
3064  * "on" request actually has an effect, as does the final "off" request.
3065  * Results are undefined if the "off" and "on" requests are not matched.
3066  */
3067 int
3068 ifpromisc(struct ifnet *ifp, int pswitch)
3069 {
3070 	struct ifreq ifr;
3071 	unsigned short oif_flags;
3072 	int oif_pcount, error;
3073 
3074 	NET_ASSERT_LOCKED(); /* modifying if_flags and if_pcount */
3075 
3076 	oif_flags = ifp->if_flags;
3077 	oif_pcount = ifp->if_pcount;
3078 	if (pswitch) {
3079 		if (ifp->if_pcount++ != 0)
3080 			return (0);
3081 		ifp->if_flags |= IFF_PROMISC;
3082 	} else {
3083 		if (--ifp->if_pcount > 0)
3084 			return (0);
3085 		ifp->if_flags &= ~IFF_PROMISC;
3086 	}
3087 
3088 	if ((ifp->if_flags & IFF_UP) == 0)
3089 		return (0);
3090 
3091 	memset(&ifr, 0, sizeof(ifr));
3092 	ifr.ifr_flags = ifp->if_flags;
3093 	error = ((*ifp->if_ioctl)(ifp, SIOCSIFFLAGS, (caddr_t)&ifr));
3094 	if (error) {
3095 		ifp->if_flags = oif_flags;
3096 		ifp->if_pcount = oif_pcount;
3097 	}
3098 
3099 	return (error);
3100 }
3101 
3102 void
3103 ifa_add(struct ifnet *ifp, struct ifaddr *ifa)
3104 {
3105 	TAILQ_INSERT_TAIL(&ifp->if_addrlist, ifa, ifa_list);
3106 }
3107 
3108 void
3109 ifa_del(struct ifnet *ifp, struct ifaddr *ifa)
3110 {
3111 	TAILQ_REMOVE(&ifp->if_addrlist, ifa, ifa_list);
3112 }
3113 
3114 void
3115 ifa_update_broadaddr(struct ifnet *ifp, struct ifaddr *ifa, struct sockaddr *sa)
3116 {
3117 	if (ifa->ifa_broadaddr->sa_len != sa->sa_len)
3118 		panic("ifa_update_broadaddr does not support dynamic length");
3119 	bcopy(sa, ifa->ifa_broadaddr, sa->sa_len);
3120 }
3121 
3122 #ifdef DDB
3123 /* debug function, can be called from ddb> */
3124 void
3125 ifa_print_all(void)
3126 {
3127 	struct ifnet *ifp;
3128 	struct ifaddr *ifa;
3129 
3130 	TAILQ_FOREACH(ifp, &ifnet, if_list) {
3131 		TAILQ_FOREACH(ifa, &ifp->if_addrlist, ifa_list) {
3132 			char addr[INET6_ADDRSTRLEN];
3133 
3134 			switch (ifa->ifa_addr->sa_family) {
3135 			case AF_INET:
3136 				printf("%s", inet_ntop(AF_INET,
3137 				    &satosin(ifa->ifa_addr)->sin_addr,
3138 				    addr, sizeof(addr)));
3139 				break;
3140 #ifdef INET6
3141 			case AF_INET6:
3142 				printf("%s", inet_ntop(AF_INET6,
3143 				    &(satosin6(ifa->ifa_addr))->sin6_addr,
3144 				    addr, sizeof(addr)));
3145 				break;
3146 #endif
3147 			}
3148 			printf(" on %s\n", ifp->if_xname);
3149 		}
3150 	}
3151 }
3152 #endif /* DDB */
3153 
3154 void
3155 ifnewlladdr(struct ifnet *ifp)
3156 {
3157 #ifdef INET6
3158 	struct ifaddr *ifa;
3159 #endif
3160 	struct ifreq ifrq;
3161 	short up;
3162 
3163 	NET_ASSERT_LOCKED();	/* for ioctl and in6 */
3164 	KERNEL_ASSERT_LOCKED();	/* for if_flags */
3165 
3166 	up = ifp->if_flags & IFF_UP;
3167 
3168 	if (up) {
3169 		/* go down for a moment... */
3170 		ifp->if_flags &= ~IFF_UP;
3171 		ifrq.ifr_flags = ifp->if_flags;
3172 		(*ifp->if_ioctl)(ifp, SIOCSIFFLAGS, (caddr_t)&ifrq);
3173 	}
3174 
3175 	ifp->if_flags |= IFF_UP;
3176 	ifrq.ifr_flags = ifp->if_flags;
3177 	(*ifp->if_ioctl)(ifp, SIOCSIFFLAGS, (caddr_t)&ifrq);
3178 
3179 #ifdef INET6
3180 	/*
3181 	 * Update the link-local address.  Don't do it if we're
3182 	 * a router to avoid confusing hosts on the network.
3183 	 */
3184 	if (!ip6_forwarding) {
3185 		ifa = &in6ifa_ifpforlinklocal(ifp, 0)->ia_ifa;
3186 		if (ifa) {
3187 			in6_purgeaddr(ifa);
3188 			if_hooks_run(&ifp->if_addrhooks);
3189 			in6_ifattach(ifp);
3190 		}
3191 	}
3192 #endif
3193 	if (!up) {
3194 		/* go back down */
3195 		ifp->if_flags &= ~IFF_UP;
3196 		ifrq.ifr_flags = ifp->if_flags;
3197 		(*ifp->if_ioctl)(ifp, SIOCSIFFLAGS, (caddr_t)&ifrq);
3198 	}
3199 }
3200 
3201 void
3202 if_addrhook_add(struct ifnet *ifp, struct task *t)
3203 {
3204 	mtx_enter(&if_hooks_mtx);
3205 	TAILQ_INSERT_TAIL(&ifp->if_addrhooks, t, t_entry);
3206 	mtx_leave(&if_hooks_mtx);
3207 }
3208 
3209 void
3210 if_addrhook_del(struct ifnet *ifp, struct task *t)
3211 {
3212 	mtx_enter(&if_hooks_mtx);
3213 	TAILQ_REMOVE(&ifp->if_addrhooks, t, t_entry);
3214 	mtx_leave(&if_hooks_mtx);
3215 }
3216 
3217 void
3218 if_addrhooks_run(struct ifnet *ifp)
3219 {
3220 	if_hooks_run(&ifp->if_addrhooks);
3221 }
3222 
3223 void
3224 if_rxr_init(struct if_rxring *rxr, u_int lwm, u_int hwm)
3225 {
3226 	extern int ticks;
3227 
3228 	memset(rxr, 0, sizeof(*rxr));
3229 
3230 	rxr->rxr_adjusted = ticks;
3231 	rxr->rxr_cwm = rxr->rxr_lwm = lwm;
3232 	rxr->rxr_hwm = hwm;
3233 }
3234 
3235 static inline void
3236 if_rxr_adjust_cwm(struct if_rxring *rxr)
3237 {
3238 	extern int ticks;
3239 
3240 	if (rxr->rxr_alive >= rxr->rxr_lwm)
3241 		return;
3242 	else if (rxr->rxr_cwm < rxr->rxr_hwm)
3243 		rxr->rxr_cwm++;
3244 
3245 	rxr->rxr_adjusted = ticks;
3246 }
3247 
3248 void
3249 if_rxr_livelocked(struct if_rxring *rxr)
3250 {
3251 	extern int ticks;
3252 
3253 	if (ticks - rxr->rxr_adjusted >= 1) {
3254 		if (rxr->rxr_cwm > rxr->rxr_lwm)
3255 			rxr->rxr_cwm--;
3256 
3257 		rxr->rxr_adjusted = ticks;
3258 	}
3259 }
3260 
3261 u_int
3262 if_rxr_get(struct if_rxring *rxr, u_int max)
3263 {
3264 	extern int ticks;
3265 	u_int diff;
3266 
3267 	if (ticks - rxr->rxr_adjusted >= 1) {
3268 		/* we're free to try for an adjustment */
3269 		if_rxr_adjust_cwm(rxr);
3270 	}
3271 
3272 	if (rxr->rxr_alive >= rxr->rxr_cwm)
3273 		return (0);
3274 
3275 	diff = min(rxr->rxr_cwm - rxr->rxr_alive, max);
3276 	rxr->rxr_alive += diff;
3277 
3278 	return (diff);
3279 }
3280 
3281 int
3282 if_rxr_info_ioctl(struct if_rxrinfo *uifri, u_int t, struct if_rxring_info *e)
3283 {
3284 	struct if_rxrinfo kifri;
3285 	int error;
3286 	u_int n;
3287 
3288 	error = copyin(uifri, &kifri, sizeof(kifri));
3289 	if (error)
3290 		return (error);
3291 
3292 	n = min(t, kifri.ifri_total);
3293 	kifri.ifri_total = t;
3294 
3295 	if (n > 0) {
3296 		error = copyout(e, kifri.ifri_entries, sizeof(*e) * n);
3297 		if (error)
3298 			return (error);
3299 	}
3300 
3301 	return (copyout(&kifri, uifri, sizeof(kifri)));
3302 }
3303 
3304 int
3305 if_rxr_ioctl(struct if_rxrinfo *ifri, const char *name, u_int size,
3306     struct if_rxring *rxr)
3307 {
3308 	struct if_rxring_info ifr;
3309 
3310 	memset(&ifr, 0, sizeof(ifr));
3311 
3312 	if (name != NULL)
3313 		strlcpy(ifr.ifr_name, name, sizeof(ifr.ifr_name));
3314 
3315 	ifr.ifr_size = size;
3316 	ifr.ifr_info = *rxr;
3317 
3318 	return (if_rxr_info_ioctl(ifri, 1, &ifr));
3319 }
3320 
3321 /*
3322  * Network stack input queues.
3323  */
3324 
3325 void
3326 niq_init(struct niqueue *niq, u_int maxlen, u_int isr)
3327 {
3328 	mq_init(&niq->ni_q, maxlen, IPL_NET);
3329 	niq->ni_isr = isr;
3330 }
3331 
3332 int
3333 niq_enqueue(struct niqueue *niq, struct mbuf *m)
3334 {
3335 	int rv;
3336 
3337 	rv = mq_enqueue(&niq->ni_q, m);
3338 	if (rv == 0)
3339 		schednetisr(niq->ni_isr);
3340 	else
3341 		if_congestion();
3342 
3343 	return (rv);
3344 }
3345 
3346 int
3347 niq_enlist(struct niqueue *niq, struct mbuf_list *ml)
3348 {
3349 	int rv;
3350 
3351 	rv = mq_enlist(&niq->ni_q, ml);
3352 	if (rv == 0)
3353 		schednetisr(niq->ni_isr);
3354 	else
3355 		if_congestion();
3356 
3357 	return (rv);
3358 }
3359 
3360 __dead void
3361 unhandled_af(int af)
3362 {
3363 	panic("unhandled af %d", af);
3364 }
3365 
3366 struct taskq *
3367 net_tq(unsigned int ifindex)
3368 {
3369 	struct taskq *t = NULL;
3370 	static int nettaskqs;
3371 
3372 	if (nettaskqs == 0)
3373 		nettaskqs = min(NET_TASKQ, ncpus);
3374 
3375 	t = nettqmp[ifindex % nettaskqs];
3376 
3377 	return (t);
3378 }
3379