xref: /openbsd-src/sys/net/if.c (revision 58fbf5d6aa35e3d66f2c32c61d2f38824a990e85)
1 /*	$OpenBSD: if.c,v 1.632 2021/02/20 04:55:52 dlg 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 "switch.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("clonerlock");
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	1
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 accomodate 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 	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 compatability 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 destinated 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 	NET_LOCK();
838 	while ((m = ml_dequeue(ml)) != NULL)
839 		(*ifp->if_input)(ifp, m);
840 	NET_UNLOCK();
841 }
842 
843 void
844 if_vinput(struct ifnet *ifp, struct mbuf *m)
845 {
846 #if NBPFILTER > 0
847 	caddr_t if_bpf;
848 #endif
849 
850 	m->m_pkthdr.ph_ifidx = ifp->if_index;
851 	m->m_pkthdr.ph_rtableid = ifp->if_rdomain;
852 
853 	counters_pkt(ifp->if_counters,
854 	    ifc_ipackets, ifc_ibytes, m->m_pkthdr.len);
855 
856 #if NPF > 0
857 	pf_pkt_addr_changed(m);
858 #endif
859 
860 #if NBPFILTER > 0
861 	if_bpf = ifp->if_bpf;
862 	if (if_bpf) {
863 		if ((*ifp->if_bpf_mtap)(if_bpf, m, BPF_DIRECTION_IN)) {
864 			m_freem(m);
865 			return;
866 		}
867 	}
868 #endif
869 
870 	if (__predict_true(!ISSET(ifp->if_xflags, IFXF_MONITOR)))
871 		(*ifp->if_input)(ifp, m);
872 }
873 
874 void
875 if_netisr(void *unused)
876 {
877 	int n, t = 0;
878 
879 	NET_LOCK();
880 
881 	while ((n = netisr) != 0) {
882 		/* Like sched_pause() but with a rwlock dance. */
883 		if (curcpu()->ci_schedstate.spc_schedflags & SPCF_SHOULDYIELD) {
884 			NET_UNLOCK();
885 			yield();
886 			NET_LOCK();
887 		}
888 
889 		atomic_clearbits_int(&netisr, n);
890 
891 #if NETHER > 0
892 		if (n & (1 << NETISR_ARP)) {
893 			KERNEL_LOCK();
894 			arpintr();
895 			KERNEL_UNLOCK();
896 		}
897 #endif
898 #if NPPP > 0
899 		if (n & (1 << NETISR_PPP)) {
900 			KERNEL_LOCK();
901 			pppintr();
902 			KERNEL_UNLOCK();
903 		}
904 #endif
905 #if NBRIDGE > 0
906 		if (n & (1 << NETISR_BRIDGE))
907 			bridgeintr();
908 #endif
909 #if NSWITCH > 0
910 		if (n & (1 << NETISR_SWITCH)) {
911 			KERNEL_LOCK();
912 			switchintr();
913 			KERNEL_UNLOCK();
914 		}
915 #endif
916 		t |= n;
917 	}
918 
919 #if NPFSYNC > 0
920 	if (t & (1 << NETISR_PFSYNC)) {
921 		KERNEL_LOCK();
922 		pfsyncintr();
923 		KERNEL_UNLOCK();
924 	}
925 #endif
926 
927 	NET_UNLOCK();
928 }
929 
930 void
931 if_hooks_run(struct task_list *hooks)
932 {
933 	struct task *t, *nt;
934 	struct task cursor = { .t_func = NULL };
935 	void (*func)(void *);
936 	void *arg;
937 
938 	mtx_enter(&if_hooks_mtx);
939 	for (t = TAILQ_FIRST(hooks); t != NULL; t = nt) {
940 		if (t->t_func == NULL) { /* skip cursors */
941 			nt = TAILQ_NEXT(t, t_entry);
942 			continue;
943 		}
944 		func = t->t_func;
945 		arg = t->t_arg;
946 
947 		TAILQ_INSERT_AFTER(hooks, t, &cursor, t_entry);
948 		mtx_leave(&if_hooks_mtx);
949 
950 		(*func)(arg);
951 
952 		mtx_enter(&if_hooks_mtx);
953 		nt = TAILQ_NEXT(&cursor, t_entry); /* avoid _Q_INVALIDATE */
954 		TAILQ_REMOVE(hooks, &cursor, t_entry);
955 	}
956 	mtx_leave(&if_hooks_mtx);
957 }
958 
959 void
960 if_remove(struct ifnet *ifp)
961 {
962 	/* Remove the interface from the list of all interfaces. */
963 	NET_LOCK();
964 	TAILQ_REMOVE(&ifnet, ifp, if_list);
965 	NET_UNLOCK();
966 
967 	/* Remove the interface from the interface index map. */
968 	if_idxmap_remove(ifp);
969 
970 	/* Sleep until the last reference is released. */
971 	refcnt_finalize(&ifp->if_refcnt, "ifrm");
972 }
973 
974 void
975 if_deactivate(struct ifnet *ifp)
976 {
977 	/*
978 	 * Call detach hooks from head to tail.  To make sure detach
979 	 * hooks are executed in the reverse order they were added, all
980 	 * the hooks have to be added to the head!
981 	 */
982 
983 	NET_LOCK();
984 	if_hooks_run(&ifp->if_detachhooks);
985 	NET_UNLOCK();
986 }
987 
988 void
989 if_detachhook_add(struct ifnet *ifp, struct task *t)
990 {
991 	mtx_enter(&if_hooks_mtx);
992 	TAILQ_INSERT_HEAD(&ifp->if_detachhooks, t, t_entry);
993 	mtx_leave(&if_hooks_mtx);
994 }
995 
996 void
997 if_detachhook_del(struct ifnet *ifp, struct task *t)
998 {
999 	mtx_enter(&if_hooks_mtx);
1000 	TAILQ_REMOVE(&ifp->if_detachhooks, t, t_entry);
1001 	mtx_leave(&if_hooks_mtx);
1002 }
1003 
1004 /*
1005  * Detach an interface from everything in the kernel.  Also deallocate
1006  * private resources.
1007  */
1008 void
1009 if_detach(struct ifnet *ifp)
1010 {
1011 	struct ifaddr *ifa;
1012 	struct ifg_list *ifg;
1013 	struct domain *dp;
1014 	int i, s;
1015 
1016 	/* Undo pseudo-driver changes. */
1017 	if_deactivate(ifp);
1018 
1019 	/* Other CPUs must not have a reference before we start destroying. */
1020 	if_remove(ifp);
1021 
1022 	ifq_clr_oactive(&ifp->if_snd);
1023 
1024 #if NBPFILTER > 0
1025 	bpfdetach(ifp);
1026 #endif
1027 
1028 	NET_LOCK();
1029 	s = splnet();
1030 	ifp->if_qstart = if_detached_qstart;
1031 	ifp->if_ioctl = if_detached_ioctl;
1032 	ifp->if_watchdog = NULL;
1033 
1034 	/* Remove the watchdog timeout & task */
1035 	timeout_del(&ifp->if_slowtimo);
1036 	task_del(net_tq(ifp->if_index), &ifp->if_watchdogtask);
1037 
1038 	/* Remove the link state task */
1039 	task_del(net_tq(ifp->if_index), &ifp->if_linkstatetask);
1040 
1041 	rti_delete(ifp);
1042 #if NETHER > 0 && defined(NFSCLIENT)
1043 	if (ifp->if_index == revarp_ifidx)
1044 		revarp_ifidx = 0;
1045 #endif
1046 #ifdef MROUTING
1047 	vif_delete(ifp);
1048 #endif
1049 	in_ifdetach(ifp);
1050 #ifdef INET6
1051 	in6_ifdetach(ifp);
1052 #endif
1053 #if NPF > 0
1054 	pfi_detach_ifnet(ifp);
1055 #endif
1056 
1057 	while ((ifg = TAILQ_FIRST(&ifp->if_groups)) != NULL)
1058 		if_delgroup(ifp, ifg->ifgl_group->ifg_group);
1059 
1060 	if_free_sadl(ifp);
1061 
1062 	/* We should not have any address left at this point. */
1063 	if (!TAILQ_EMPTY(&ifp->if_addrlist)) {
1064 #ifdef DIAGNOSTIC
1065 		printf("%s: address list non empty\n", ifp->if_xname);
1066 #endif
1067 		while ((ifa = TAILQ_FIRST(&ifp->if_addrlist)) != NULL) {
1068 			ifa_del(ifp, ifa);
1069 			ifa->ifa_ifp = NULL;
1070 			ifafree(ifa);
1071 		}
1072 	}
1073 
1074 	KASSERT(TAILQ_EMPTY(&ifp->if_addrhooks));
1075 	KASSERT(TAILQ_EMPTY(&ifp->if_linkstatehooks));
1076 	KASSERT(TAILQ_EMPTY(&ifp->if_detachhooks));
1077 
1078 	for (i = 0; (dp = domains[i]) != NULL; i++) {
1079 		if (dp->dom_ifdetach && ifp->if_afdata[dp->dom_family])
1080 			(*dp->dom_ifdetach)(ifp,
1081 			    ifp->if_afdata[dp->dom_family]);
1082 	}
1083 
1084 	/* Announce that the interface is gone. */
1085 	rtm_ifannounce(ifp, IFAN_DEPARTURE);
1086 	splx(s);
1087 	NET_UNLOCK();
1088 
1089 	if (ifp->if_counters != NULL)
1090 		if_counters_free(ifp);
1091 
1092 	for (i = 0; i < ifp->if_nifqs; i++)
1093 		ifq_destroy(ifp->if_ifqs[i]);
1094 	if (ifp->if_ifqs != ifp->if_snd.ifq_ifqs) {
1095 		for (i = 1; i < ifp->if_nifqs; i++) {
1096 			free(ifp->if_ifqs[i], M_DEVBUF,
1097 			    sizeof(struct ifqueue));
1098 		}
1099 		free(ifp->if_ifqs, M_DEVBUF,
1100 		    sizeof(struct ifqueue *) * ifp->if_nifqs);
1101 	}
1102 
1103 	for (i = 0; i < ifp->if_niqs; i++)
1104 		ifiq_destroy(ifp->if_iqs[i]);
1105 	if (ifp->if_iqs != ifp->if_rcv.ifiq_ifiqs) {
1106 		for (i = 1; i < ifp->if_niqs; i++) {
1107 			free(ifp->if_iqs[i], M_DEVBUF,
1108 			    sizeof(struct ifiqueue));
1109 		}
1110 		free(ifp->if_iqs, M_DEVBUF,
1111 		    sizeof(struct ifiqueue *) * ifp->if_niqs);
1112 	}
1113 }
1114 
1115 /*
1116  * Returns true if ``ifp0'' is connected to the interface with index ``ifidx''.
1117  */
1118 int
1119 if_isconnected(const struct ifnet *ifp0, unsigned int ifidx)
1120 {
1121 	struct ifnet *ifp;
1122 	int connected = 0;
1123 
1124 	ifp = if_get(ifidx);
1125 	if (ifp == NULL)
1126 		return (0);
1127 
1128 	if (ifp0->if_index == ifp->if_index)
1129 		connected = 1;
1130 
1131 #if NBRIDGE > 0
1132 	if (ifp0->if_bridgeidx != 0 && ifp0->if_bridgeidx == ifp->if_bridgeidx)
1133 		connected = 1;
1134 #endif
1135 #if NCARP > 0
1136 	if ((ifp0->if_type == IFT_CARP &&
1137 	    ifp0->if_carpdevidx == ifp->if_index) ||
1138 	    (ifp->if_type == IFT_CARP && ifp->if_carpdevidx == ifp0->if_index))
1139 		connected = 1;
1140 #endif
1141 
1142 	if_put(ifp);
1143 	return (connected);
1144 }
1145 
1146 /*
1147  * Create a clone network interface.
1148  */
1149 int
1150 if_clone_create(const char *name, int rdomain)
1151 {
1152 	struct if_clone *ifc;
1153 	struct ifnet *ifp;
1154 	int unit, ret;
1155 
1156 	ifc = if_clone_lookup(name, &unit);
1157 	if (ifc == NULL)
1158 		return (EINVAL);
1159 
1160 	rw_enter_write(&if_cloners_lock);
1161 
1162 	if ((ifp = if_unit(name)) != NULL) {
1163 		ret = EEXIST;
1164 		goto unlock;
1165 	}
1166 
1167 	ret = (*ifc->ifc_create)(ifc, unit);
1168 
1169 	if (ret != 0 || (ifp = if_unit(name)) == NULL)
1170 		goto unlock;
1171 
1172 	NET_LOCK();
1173 	if_addgroup(ifp, ifc->ifc_name);
1174 	if (rdomain != 0)
1175 		if_setrdomain(ifp, rdomain);
1176 	NET_UNLOCK();
1177 unlock:
1178 	rw_exit_write(&if_cloners_lock);
1179 	if_put(ifp);
1180 
1181 	return (ret);
1182 }
1183 
1184 /*
1185  * Destroy a clone network interface.
1186  */
1187 int
1188 if_clone_destroy(const char *name)
1189 {
1190 	struct if_clone *ifc;
1191 	struct ifnet *ifp;
1192 	int ret;
1193 
1194 	ifc = if_clone_lookup(name, NULL);
1195 	if (ifc == NULL)
1196 		return (EINVAL);
1197 
1198 	if (ifc->ifc_destroy == NULL)
1199 		return (EOPNOTSUPP);
1200 
1201 	rw_enter_write(&if_cloners_lock);
1202 
1203 	TAILQ_FOREACH(ifp, &ifnet, if_list) {
1204 		if (strcmp(ifp->if_xname, name) == 0)
1205 			break;
1206 	}
1207 	if (ifp == NULL) {
1208 		rw_exit_write(&if_cloners_lock);
1209 		return (ENXIO);
1210 	}
1211 
1212 	NET_LOCK();
1213 	if (ifp->if_flags & IFF_UP) {
1214 		int s;
1215 		s = splnet();
1216 		if_down(ifp);
1217 		splx(s);
1218 	}
1219 	NET_UNLOCK();
1220 	ret = (*ifc->ifc_destroy)(ifp);
1221 
1222 	rw_exit_write(&if_cloners_lock);
1223 
1224 	return (ret);
1225 }
1226 
1227 /*
1228  * Look up a network interface cloner.
1229  */
1230 struct if_clone *
1231 if_clone_lookup(const char *name, int *unitp)
1232 {
1233 	struct if_clone *ifc;
1234 	const char *cp;
1235 	int unit;
1236 
1237 	/* separate interface name from unit */
1238 	for (cp = name;
1239 	    cp - name < IFNAMSIZ && *cp && (*cp < '0' || *cp > '9');
1240 	    cp++)
1241 		continue;
1242 
1243 	if (cp == name || cp - name == IFNAMSIZ || !*cp)
1244 		return (NULL);	/* No name or unit number */
1245 
1246 	if (cp - name < IFNAMSIZ-1 && *cp == '0' && cp[1] != '\0')
1247 		return (NULL);	/* unit number 0 padded */
1248 
1249 	LIST_FOREACH(ifc, &if_cloners, ifc_list) {
1250 		if (strlen(ifc->ifc_name) == cp - name &&
1251 		    !strncmp(name, ifc->ifc_name, cp - name))
1252 			break;
1253 	}
1254 
1255 	if (ifc == NULL)
1256 		return (NULL);
1257 
1258 	unit = 0;
1259 	while (cp - name < IFNAMSIZ && *cp) {
1260 		if (*cp < '0' || *cp > '9' ||
1261 		    unit > (INT_MAX - (*cp - '0')) / 10) {
1262 			/* Bogus unit number. */
1263 			return (NULL);
1264 		}
1265 		unit = (unit * 10) + (*cp++ - '0');
1266 	}
1267 
1268 	if (unitp != NULL)
1269 		*unitp = unit;
1270 	return (ifc);
1271 }
1272 
1273 /*
1274  * Register a network interface cloner.
1275  */
1276 void
1277 if_clone_attach(struct if_clone *ifc)
1278 {
1279 	/*
1280 	 * we are called at kernel boot by main(), when pseudo devices are
1281 	 * being attached. The main() is the only guy which may alter the
1282 	 * if_cloners. While system is running and main() is done with
1283 	 * initialization, the if_cloners becomes immutable.
1284 	 */
1285 	KASSERT(pdevinit_done == 0);
1286 	LIST_INSERT_HEAD(&if_cloners, ifc, ifc_list);
1287 	if_cloners_count++;
1288 }
1289 
1290 /*
1291  * Provide list of interface cloners to userspace.
1292  */
1293 int
1294 if_clone_list(struct if_clonereq *ifcr)
1295 {
1296 	char outbuf[IFNAMSIZ], *dst;
1297 	struct if_clone *ifc;
1298 	int count, error = 0;
1299 
1300 	if ((dst = ifcr->ifcr_buffer) == NULL) {
1301 		/* Just asking how many there are. */
1302 		ifcr->ifcr_total = if_cloners_count;
1303 		return (0);
1304 	}
1305 
1306 	if (ifcr->ifcr_count < 0)
1307 		return (EINVAL);
1308 
1309 	ifcr->ifcr_total = if_cloners_count;
1310 	count = MIN(if_cloners_count, ifcr->ifcr_count);
1311 
1312 	LIST_FOREACH(ifc, &if_cloners, ifc_list) {
1313 		if (count == 0)
1314 			break;
1315 		bzero(outbuf, sizeof outbuf);
1316 		strlcpy(outbuf, ifc->ifc_name, IFNAMSIZ);
1317 		error = copyout(outbuf, dst, IFNAMSIZ);
1318 		if (error)
1319 			break;
1320 		count--;
1321 		dst += IFNAMSIZ;
1322 	}
1323 
1324 	return (error);
1325 }
1326 
1327 /*
1328  * set queue congestion marker
1329  */
1330 void
1331 if_congestion(void)
1332 {
1333 	extern int ticks;
1334 
1335 	ifq_congestion = ticks;
1336 }
1337 
1338 int
1339 if_congested(void)
1340 {
1341 	extern int ticks;
1342 	int diff;
1343 
1344 	diff = ticks - ifq_congestion;
1345 	if (diff < 0) {
1346 		ifq_congestion = ticks - hz;
1347 		return (0);
1348 	}
1349 
1350 	return (diff <= (hz / 100));
1351 }
1352 
1353 #define	equal(a1, a2)	\
1354 	(bcmp((caddr_t)(a1), (caddr_t)(a2),	\
1355 	(a1)->sa_len) == 0)
1356 
1357 /*
1358  * Locate an interface based on a complete address.
1359  */
1360 struct ifaddr *
1361 ifa_ifwithaddr(struct sockaddr *addr, u_int rtableid)
1362 {
1363 	struct ifnet *ifp;
1364 	struct ifaddr *ifa;
1365 	u_int rdomain;
1366 
1367 	rdomain = rtable_l2(rtableid);
1368 	KERNEL_LOCK();
1369 	TAILQ_FOREACH(ifp, &ifnet, if_list) {
1370 		if (ifp->if_rdomain != rdomain)
1371 			continue;
1372 
1373 		TAILQ_FOREACH(ifa, &ifp->if_addrlist, ifa_list) {
1374 			if (ifa->ifa_addr->sa_family != addr->sa_family)
1375 				continue;
1376 
1377 			if (equal(addr, ifa->ifa_addr)) {
1378 				KERNEL_UNLOCK();
1379 				return (ifa);
1380 			}
1381 		}
1382 	}
1383 	KERNEL_UNLOCK();
1384 	return (NULL);
1385 }
1386 
1387 /*
1388  * Locate the point to point interface with a given destination address.
1389  */
1390 struct ifaddr *
1391 ifa_ifwithdstaddr(struct sockaddr *addr, u_int rdomain)
1392 {
1393 	struct ifnet *ifp;
1394 	struct ifaddr *ifa;
1395 
1396 	rdomain = rtable_l2(rdomain);
1397 	KERNEL_LOCK();
1398 	TAILQ_FOREACH(ifp, &ifnet, if_list) {
1399 		if (ifp->if_rdomain != rdomain)
1400 			continue;
1401 		if (ifp->if_flags & IFF_POINTOPOINT) {
1402 			TAILQ_FOREACH(ifa, &ifp->if_addrlist, ifa_list) {
1403 				if (ifa->ifa_addr->sa_family !=
1404 				    addr->sa_family || ifa->ifa_dstaddr == NULL)
1405 					continue;
1406 				if (equal(addr, ifa->ifa_dstaddr)) {
1407 					KERNEL_UNLOCK();
1408 					return (ifa);
1409 				}
1410 			}
1411 		}
1412 	}
1413 	KERNEL_UNLOCK();
1414 	return (NULL);
1415 }
1416 
1417 /*
1418  * Find an interface address specific to an interface best matching
1419  * a given address.
1420  */
1421 struct ifaddr *
1422 ifaof_ifpforaddr(struct sockaddr *addr, struct ifnet *ifp)
1423 {
1424 	struct ifaddr *ifa;
1425 	char *cp, *cp2, *cp3;
1426 	char *cplim;
1427 	struct ifaddr *ifa_maybe = NULL;
1428 	u_int af = addr->sa_family;
1429 
1430 	if (af >= AF_MAX)
1431 		return (NULL);
1432 	TAILQ_FOREACH(ifa, &ifp->if_addrlist, ifa_list) {
1433 		if (ifa->ifa_addr->sa_family != af)
1434 			continue;
1435 		if (ifa_maybe == NULL)
1436 			ifa_maybe = ifa;
1437 		if (ifa->ifa_netmask == 0 || ifp->if_flags & IFF_POINTOPOINT) {
1438 			if (equal(addr, ifa->ifa_addr) ||
1439 			    (ifa->ifa_dstaddr && equal(addr, ifa->ifa_dstaddr)))
1440 				return (ifa);
1441 			continue;
1442 		}
1443 		cp = addr->sa_data;
1444 		cp2 = ifa->ifa_addr->sa_data;
1445 		cp3 = ifa->ifa_netmask->sa_data;
1446 		cplim = ifa->ifa_netmask->sa_len + (char *)ifa->ifa_netmask;
1447 		for (; cp3 < cplim; cp3++)
1448 			if ((*cp++ ^ *cp2++) & *cp3)
1449 				break;
1450 		if (cp3 == cplim)
1451 			return (ifa);
1452 	}
1453 	return (ifa_maybe);
1454 }
1455 
1456 void
1457 if_rtrequest_dummy(struct ifnet *ifp, int req, struct rtentry *rt)
1458 {
1459 }
1460 
1461 /*
1462  * Default action when installing a local route on a point-to-point
1463  * interface.
1464  */
1465 void
1466 p2p_rtrequest(struct ifnet *ifp, int req, struct rtentry *rt)
1467 {
1468 	struct ifnet *lo0ifp;
1469 	struct ifaddr *ifa, *lo0ifa;
1470 
1471 	switch (req) {
1472 	case RTM_ADD:
1473 		if (!ISSET(rt->rt_flags, RTF_LOCAL))
1474 			break;
1475 
1476 		TAILQ_FOREACH(ifa, &ifp->if_addrlist, ifa_list) {
1477 			if (memcmp(rt_key(rt), ifa->ifa_addr,
1478 			    rt_key(rt)->sa_len) == 0)
1479 				break;
1480 		}
1481 
1482 		if (ifa == NULL)
1483 			break;
1484 
1485 		KASSERT(ifa == rt->rt_ifa);
1486 
1487 		lo0ifp = if_get(rtable_loindex(ifp->if_rdomain));
1488 		KASSERT(lo0ifp != NULL);
1489 		TAILQ_FOREACH(lo0ifa, &lo0ifp->if_addrlist, ifa_list) {
1490 			if (lo0ifa->ifa_addr->sa_family ==
1491 			    ifa->ifa_addr->sa_family)
1492 				break;
1493 		}
1494 		if_put(lo0ifp);
1495 
1496 		if (lo0ifa == NULL)
1497 			break;
1498 
1499 		rt->rt_flags &= ~RTF_LLINFO;
1500 		break;
1501 	case RTM_DELETE:
1502 	case RTM_RESOLVE:
1503 	default:
1504 		break;
1505 	}
1506 }
1507 
1508 int
1509 p2p_bpf_mtap(caddr_t if_bpf, const struct mbuf *m, u_int dir)
1510 {
1511 #if NBPFILTER > 0
1512 	return (bpf_mtap_af(if_bpf, m->m_pkthdr.ph_family, m, dir));
1513 #else
1514 	return (0);
1515 #endif
1516 }
1517 
1518 void
1519 p2p_input(struct ifnet *ifp, struct mbuf *m)
1520 {
1521 	void (*input)(struct ifnet *, struct mbuf *);
1522 
1523 	switch (m->m_pkthdr.ph_family) {
1524 	case AF_INET:
1525 		input = ipv4_input;
1526 		break;
1527 #ifdef INET6
1528 	case AF_INET6:
1529 		input = ipv6_input;
1530 		break;
1531 #endif
1532 #ifdef MPLS
1533 	case AF_MPLS:
1534 		input = mpls_input;
1535 		break;
1536 #endif
1537 	default:
1538 		m_freem(m);
1539 		return;
1540 	}
1541 
1542 	(*input)(ifp, m);
1543 }
1544 
1545 /*
1546  * Bring down all interfaces
1547  */
1548 void
1549 if_downall(void)
1550 {
1551 	struct ifreq ifrq;	/* XXX only partly built */
1552 	struct ifnet *ifp;
1553 
1554 	NET_LOCK();
1555 	TAILQ_FOREACH(ifp, &ifnet, if_list) {
1556 		if ((ifp->if_flags & IFF_UP) == 0)
1557 			continue;
1558 		if_down(ifp);
1559 		ifrq.ifr_flags = ifp->if_flags;
1560 		(*ifp->if_ioctl)(ifp, SIOCSIFFLAGS, (caddr_t)&ifrq);
1561 	}
1562 	NET_UNLOCK();
1563 }
1564 
1565 /*
1566  * Mark an interface down and notify protocols of
1567  * the transition.
1568  */
1569 void
1570 if_down(struct ifnet *ifp)
1571 {
1572 	NET_ASSERT_LOCKED();
1573 
1574 	ifp->if_flags &= ~IFF_UP;
1575 	getmicrotime(&ifp->if_lastchange);
1576 	ifq_purge(&ifp->if_snd);
1577 
1578 	if_linkstate(ifp);
1579 }
1580 
1581 /*
1582  * Mark an interface up and notify protocols of
1583  * the transition.
1584  */
1585 void
1586 if_up(struct ifnet *ifp)
1587 {
1588 	NET_ASSERT_LOCKED();
1589 
1590 	ifp->if_flags |= IFF_UP;
1591 	getmicrotime(&ifp->if_lastchange);
1592 
1593 #ifdef INET6
1594 	/* Userland expects the kernel to set ::1 on default lo(4). */
1595 	if (ifp->if_index == rtable_loindex(ifp->if_rdomain))
1596 		in6_ifattach(ifp);
1597 #endif
1598 
1599 	if_linkstate(ifp);
1600 }
1601 
1602 /*
1603  * Notify userland, the routing table and hooks owner of
1604  * a link-state transition.
1605  */
1606 void
1607 if_linkstate_task(void *xifidx)
1608 {
1609 	unsigned int ifidx = (unsigned long)xifidx;
1610 	struct ifnet *ifp;
1611 
1612 	KERNEL_LOCK();
1613 	NET_LOCK();
1614 
1615 	ifp = if_get(ifidx);
1616 	if (ifp != NULL)
1617 		if_linkstate(ifp);
1618 	if_put(ifp);
1619 
1620 	NET_UNLOCK();
1621 	KERNEL_UNLOCK();
1622 }
1623 
1624 void
1625 if_linkstate(struct ifnet *ifp)
1626 {
1627 	NET_ASSERT_LOCKED();
1628 
1629 	rtm_ifchg(ifp);
1630 	rt_if_track(ifp);
1631 
1632 	if_hooks_run(&ifp->if_linkstatehooks);
1633 }
1634 
1635 void
1636 if_linkstatehook_add(struct ifnet *ifp, struct task *t)
1637 {
1638 	mtx_enter(&if_hooks_mtx);
1639 	TAILQ_INSERT_HEAD(&ifp->if_linkstatehooks, t, t_entry);
1640 	mtx_leave(&if_hooks_mtx);
1641 }
1642 
1643 void
1644 if_linkstatehook_del(struct ifnet *ifp, struct task *t)
1645 {
1646 	mtx_enter(&if_hooks_mtx);
1647 	TAILQ_REMOVE(&ifp->if_linkstatehooks, t, t_entry);
1648 	mtx_leave(&if_hooks_mtx);
1649 }
1650 
1651 /*
1652  * Schedule a link state change task.
1653  */
1654 void
1655 if_link_state_change(struct ifnet *ifp)
1656 {
1657 	task_add(net_tq(ifp->if_index), &ifp->if_linkstatetask);
1658 }
1659 
1660 /*
1661  * Handle interface watchdog timer routine.  Called
1662  * from softclock, we decrement timer (if set) and
1663  * call the appropriate interface routine on expiration.
1664  */
1665 void
1666 if_slowtimo(void *arg)
1667 {
1668 	struct ifnet *ifp = arg;
1669 	int s = splnet();
1670 
1671 	if (ifp->if_watchdog) {
1672 		if (ifp->if_timer > 0 && --ifp->if_timer == 0)
1673 			task_add(net_tq(ifp->if_index), &ifp->if_watchdogtask);
1674 		timeout_add_sec(&ifp->if_slowtimo, IFNET_SLOWTIMO);
1675 	}
1676 	splx(s);
1677 }
1678 
1679 void
1680 if_watchdog_task(void *xifidx)
1681 {
1682 	unsigned int ifidx = (unsigned long)xifidx;
1683 	struct ifnet *ifp;
1684 	int s;
1685 
1686 	ifp = if_get(ifidx);
1687 	if (ifp == NULL)
1688 		return;
1689 
1690 	KERNEL_LOCK();
1691 	s = splnet();
1692 	if (ifp->if_watchdog)
1693 		(*ifp->if_watchdog)(ifp);
1694 	splx(s);
1695 	KERNEL_UNLOCK();
1696 
1697 	if_put(ifp);
1698 }
1699 
1700 /*
1701  * Map interface name to interface structure pointer.
1702  */
1703 struct ifnet *
1704 if_unit(const char *name)
1705 {
1706 	struct ifnet *ifp;
1707 
1708 	KERNEL_ASSERT_LOCKED();
1709 
1710 	TAILQ_FOREACH(ifp, &ifnet, if_list) {
1711 		if (strcmp(ifp->if_xname, name) == 0) {
1712 			if_ref(ifp);
1713 			return (ifp);
1714 		}
1715 	}
1716 
1717 	return (NULL);
1718 }
1719 
1720 /*
1721  * Map interface index to interface structure pointer.
1722  */
1723 struct ifnet *
1724 if_get(unsigned int index)
1725 {
1726 	struct srp_ref sr;
1727 	struct if_map *if_map;
1728 	struct srp *map;
1729 	struct ifnet *ifp = NULL;
1730 
1731 	if_map = srp_enter(&sr, &if_idxmap.map);
1732 	if (index < if_map->limit) {
1733 		map = (struct srp *)(if_map + 1);
1734 
1735 		ifp = srp_follow(&sr, &map[index]);
1736 		if (ifp != NULL) {
1737 			KASSERT(ifp->if_index == index);
1738 			if_ref(ifp);
1739 		}
1740 	}
1741 	srp_leave(&sr);
1742 
1743 	return (ifp);
1744 }
1745 
1746 struct ifnet *
1747 if_ref(struct ifnet *ifp)
1748 {
1749 	refcnt_take(&ifp->if_refcnt);
1750 
1751 	return (ifp);
1752 }
1753 
1754 void
1755 if_put(struct ifnet *ifp)
1756 {
1757 	if (ifp == NULL)
1758 		return;
1759 
1760 	refcnt_rele_wake(&ifp->if_refcnt);
1761 }
1762 
1763 int
1764 if_setlladdr(struct ifnet *ifp, const uint8_t *lladdr)
1765 {
1766 	if (ifp->if_sadl == NULL)
1767 		return (EINVAL);
1768 
1769 	memcpy(((struct arpcom *)ifp)->ac_enaddr, lladdr, ETHER_ADDR_LEN);
1770 	memcpy(LLADDR(ifp->if_sadl), lladdr, ETHER_ADDR_LEN);
1771 
1772 	return (0);
1773 }
1774 
1775 int
1776 if_createrdomain(int rdomain, struct ifnet *ifp)
1777 {
1778 	int error;
1779 	struct ifnet *loifp;
1780 	char loifname[IFNAMSIZ];
1781 	unsigned int unit = rdomain;
1782 
1783 	if (!rtable_exists(rdomain) && (error = rtable_add(rdomain)) != 0)
1784 		return (error);
1785 	if (!rtable_empty(rdomain))
1786 		return (EEXIST);
1787 
1788 	/* Create rdomain including its loopback if with unit == rdomain */
1789 	snprintf(loifname, sizeof(loifname), "lo%u", unit);
1790 	error = if_clone_create(loifname, 0);
1791 	if ((loifp = if_unit(loifname)) == NULL)
1792 		return (ENXIO);
1793 	if (error && (ifp != loifp || error != EEXIST)) {
1794 		if_put(loifp);
1795 		return (error);
1796 	}
1797 
1798 	rtable_l2set(rdomain, rdomain, loifp->if_index);
1799 	loifp->if_rdomain = rdomain;
1800 	if_put(loifp);
1801 
1802 	return (0);
1803 }
1804 
1805 int
1806 if_setrdomain(struct ifnet *ifp, int rdomain)
1807 {
1808 	struct ifreq ifr;
1809 	int error, up = 0, s;
1810 
1811 	if (rdomain < 0 || rdomain > RT_TABLEID_MAX)
1812 		return (EINVAL);
1813 
1814 	if (rdomain != ifp->if_rdomain &&
1815 	    (ifp->if_flags & IFF_LOOPBACK) &&
1816 	    (ifp->if_index == rtable_loindex(ifp->if_rdomain)))
1817 		return (EPERM);
1818 
1819 	if (!rtable_exists(rdomain))
1820 		return (ESRCH);
1821 
1822 	/* make sure that the routing table is a real rdomain */
1823 	if (rdomain != rtable_l2(rdomain))
1824 		return (EINVAL);
1825 
1826 	if (rdomain != ifp->if_rdomain) {
1827 		s = splnet();
1828 		/*
1829 		 * We are tearing down the world.
1830 		 * Take down the IF so:
1831 		 * 1. everything that cares gets a message
1832 		 * 2. the automagic IPv6 bits are recreated
1833 		 */
1834 		if (ifp->if_flags & IFF_UP) {
1835 			up = 1;
1836 			if_down(ifp);
1837 		}
1838 		rti_delete(ifp);
1839 #ifdef MROUTING
1840 		vif_delete(ifp);
1841 #endif
1842 		in_ifdetach(ifp);
1843 #ifdef INET6
1844 		in6_ifdetach(ifp);
1845 #endif
1846 		splx(s);
1847 	}
1848 
1849 	/* Let devices like enc(4) or mpe(4) know about the change */
1850 	ifr.ifr_rdomainid = rdomain;
1851 	if ((error = (*ifp->if_ioctl)(ifp, SIOCSIFRDOMAIN,
1852 	    (caddr_t)&ifr)) != ENOTTY)
1853 		return (error);
1854 	error = 0;
1855 
1856 	/* Add interface to the specified rdomain */
1857 	ifp->if_rdomain = rdomain;
1858 
1859 	/* If we took down the IF, bring it back */
1860 	if (up) {
1861 		s = splnet();
1862 		if_up(ifp);
1863 		splx(s);
1864 	}
1865 
1866 	return (0);
1867 }
1868 
1869 /*
1870  * Interface ioctls.
1871  */
1872 int
1873 ifioctl(struct socket *so, u_long cmd, caddr_t data, struct proc *p)
1874 {
1875 	struct ifnet *ifp;
1876 	struct ifreq *ifr = (struct ifreq *)data;
1877 	struct ifgroupreq *ifgr = (struct ifgroupreq *)data;
1878 	struct if_afreq *ifar = (struct if_afreq *)data;
1879 	char ifdescrbuf[IFDESCRSIZE];
1880 	char ifrtlabelbuf[RTLABEL_LEN];
1881 	int s, error = 0, oif_xflags;
1882 	size_t bytesdone;
1883 	unsigned short oif_flags;
1884 
1885 	switch (cmd) {
1886 	case SIOCIFCREATE:
1887 		if ((error = suser(p)) != 0)
1888 			return (error);
1889 		error = if_clone_create(ifr->ifr_name, 0);
1890 		return (error);
1891 	case SIOCIFDESTROY:
1892 		if ((error = suser(p)) != 0)
1893 			return (error);
1894 		error = if_clone_destroy(ifr->ifr_name);
1895 		return (error);
1896 	case SIOCSIFGATTR:
1897 		if ((error = suser(p)) != 0)
1898 			return (error);
1899 		NET_LOCK();
1900 		error = if_setgroupattribs(data);
1901 		NET_UNLOCK();
1902 		return (error);
1903 	case SIOCGIFCONF:
1904 	case SIOCIFGCLONERS:
1905 	case SIOCGIFGMEMB:
1906 	case SIOCGIFGATTR:
1907 	case SIOCGIFGLIST:
1908 	case SIOCGIFFLAGS:
1909 	case SIOCGIFXFLAGS:
1910 	case SIOCGIFMETRIC:
1911 	case SIOCGIFMTU:
1912 	case SIOCGIFHARDMTU:
1913 	case SIOCGIFDATA:
1914 	case SIOCGIFDESCR:
1915 	case SIOCGIFRTLABEL:
1916 	case SIOCGIFPRIORITY:
1917 	case SIOCGIFRDOMAIN:
1918 	case SIOCGIFGROUP:
1919 	case SIOCGIFLLPRIO:
1920 		return (ifioctl_get(cmd, data));
1921 	}
1922 
1923 	ifp = if_unit(ifr->ifr_name);
1924 	if (ifp == NULL)
1925 		return (ENXIO);
1926 	oif_flags = ifp->if_flags;
1927 	oif_xflags = ifp->if_xflags;
1928 
1929 	switch (cmd) {
1930 	case SIOCIFAFATTACH:
1931 	case SIOCIFAFDETACH:
1932 		if ((error = suser(p)) != 0)
1933 			break;
1934 		NET_LOCK();
1935 		switch (ifar->ifar_af) {
1936 		case AF_INET:
1937 			/* attach is a noop for AF_INET */
1938 			if (cmd == SIOCIFAFDETACH)
1939 				in_ifdetach(ifp);
1940 			break;
1941 #ifdef INET6
1942 		case AF_INET6:
1943 			if (cmd == SIOCIFAFATTACH)
1944 				error = in6_ifattach(ifp);
1945 			else
1946 				in6_ifdetach(ifp);
1947 			break;
1948 #endif /* INET6 */
1949 		default:
1950 			error = EAFNOSUPPORT;
1951 		}
1952 		NET_UNLOCK();
1953 		break;
1954 
1955 	case SIOCSIFFLAGS:
1956 		if ((error = suser(p)) != 0)
1957 			break;
1958 
1959 		NET_LOCK();
1960 		ifp->if_flags = (ifp->if_flags & IFF_CANTCHANGE) |
1961 			(ifr->ifr_flags & ~IFF_CANTCHANGE);
1962 
1963 		error = (*ifp->if_ioctl)(ifp, cmd, data);
1964 		if (error != 0) {
1965 			ifp->if_flags = oif_flags;
1966 		} else if (ISSET(oif_flags ^ ifp->if_flags, IFF_UP)) {
1967 			s = splnet();
1968 			if (ISSET(ifp->if_flags, IFF_UP))
1969 				if_up(ifp);
1970 			else
1971 				if_down(ifp);
1972 			splx(s);
1973 		}
1974 		NET_UNLOCK();
1975 		break;
1976 
1977 	case SIOCSIFXFLAGS:
1978 		if ((error = suser(p)) != 0)
1979 			break;
1980 
1981 		NET_LOCK();
1982 #ifdef INET6
1983 		if (ISSET(ifr->ifr_flags, IFXF_AUTOCONF6)) {
1984 			error = in6_ifattach(ifp);
1985 			if (error != 0) {
1986 				NET_UNLOCK();
1987 				break;
1988 			}
1989 		}
1990 
1991 		if (ISSET(ifr->ifr_flags, IFXF_INET6_NOSOII) &&
1992 		    !ISSET(ifp->if_xflags, IFXF_INET6_NOSOII))
1993 			ifp->if_xflags |= IFXF_INET6_NOSOII;
1994 
1995 		if (!ISSET(ifr->ifr_flags, IFXF_INET6_NOSOII) &&
1996 		    ISSET(ifp->if_xflags, IFXF_INET6_NOSOII))
1997 			ifp->if_xflags &= ~IFXF_INET6_NOSOII;
1998 
1999 #endif	/* INET6 */
2000 
2001 #ifdef MPLS
2002 		if (ISSET(ifr->ifr_flags, IFXF_MPLS) &&
2003 		    !ISSET(ifp->if_xflags, IFXF_MPLS)) {
2004 			s = splnet();
2005 			ifp->if_xflags |= IFXF_MPLS;
2006 			ifp->if_ll_output = ifp->if_output;
2007 			ifp->if_output = mpls_output;
2008 			splx(s);
2009 		}
2010 		if (ISSET(ifp->if_xflags, IFXF_MPLS) &&
2011 		    !ISSET(ifr->ifr_flags, IFXF_MPLS)) {
2012 			s = splnet();
2013 			ifp->if_xflags &= ~IFXF_MPLS;
2014 			ifp->if_output = ifp->if_ll_output;
2015 			ifp->if_ll_output = NULL;
2016 			splx(s);
2017 		}
2018 #endif	/* MPLS */
2019 
2020 #ifndef SMALL_KERNEL
2021 		if (ifp->if_capabilities & IFCAP_WOL) {
2022 			if (ISSET(ifr->ifr_flags, IFXF_WOL) &&
2023 			    !ISSET(ifp->if_xflags, IFXF_WOL)) {
2024 				s = splnet();
2025 				ifp->if_xflags |= IFXF_WOL;
2026 				error = ifp->if_wol(ifp, 1);
2027 				splx(s);
2028 			}
2029 			if (ISSET(ifp->if_xflags, IFXF_WOL) &&
2030 			    !ISSET(ifr->ifr_flags, IFXF_WOL)) {
2031 				s = splnet();
2032 				ifp->if_xflags &= ~IFXF_WOL;
2033 				error = ifp->if_wol(ifp, 0);
2034 				splx(s);
2035 			}
2036 		} else if (ISSET(ifr->ifr_flags, IFXF_WOL)) {
2037 			ifr->ifr_flags &= ~IFXF_WOL;
2038 			error = ENOTSUP;
2039 		}
2040 #endif
2041 
2042 		if (error == 0)
2043 			ifp->if_xflags = (ifp->if_xflags & IFXF_CANTCHANGE) |
2044 				(ifr->ifr_flags & ~IFXF_CANTCHANGE);
2045 		NET_UNLOCK();
2046 		break;
2047 
2048 	case SIOCSIFMETRIC:
2049 		if ((error = suser(p)) != 0)
2050 			break;
2051 		NET_LOCK();
2052 		ifp->if_metric = ifr->ifr_metric;
2053 		NET_UNLOCK();
2054 		break;
2055 
2056 	case SIOCSIFMTU:
2057 		if ((error = suser(p)) != 0)
2058 			break;
2059 		NET_LOCK();
2060 		error = (*ifp->if_ioctl)(ifp, cmd, data);
2061 		NET_UNLOCK();
2062 		if (!error)
2063 			rtm_ifchg(ifp);
2064 		break;
2065 
2066 	case SIOCSIFDESCR:
2067 		if ((error = suser(p)) != 0)
2068 			break;
2069 		error = copyinstr(ifr->ifr_data, ifdescrbuf,
2070 		    IFDESCRSIZE, &bytesdone);
2071 		if (error == 0) {
2072 			(void)memset(ifp->if_description, 0, IFDESCRSIZE);
2073 			strlcpy(ifp->if_description, ifdescrbuf, IFDESCRSIZE);
2074 		}
2075 		break;
2076 
2077 	case SIOCSIFRTLABEL:
2078 		if ((error = suser(p)) != 0)
2079 			break;
2080 		error = copyinstr(ifr->ifr_data, ifrtlabelbuf,
2081 		    RTLABEL_LEN, &bytesdone);
2082 		if (error == 0) {
2083 			rtlabel_unref(ifp->if_rtlabelid);
2084 			ifp->if_rtlabelid = rtlabel_name2id(ifrtlabelbuf);
2085 		}
2086 		break;
2087 
2088 	case SIOCSIFPRIORITY:
2089 		if ((error = suser(p)) != 0)
2090 			break;
2091 		if (ifr->ifr_metric < 0 || ifr->ifr_metric > 15) {
2092 			error = EINVAL;
2093 			break;
2094 		}
2095 		ifp->if_priority = ifr->ifr_metric;
2096 		break;
2097 
2098 	case SIOCSIFRDOMAIN:
2099 		if ((error = suser(p)) != 0)
2100 			break;
2101 		error = if_createrdomain(ifr->ifr_rdomainid, ifp);
2102 		if (!error || error == EEXIST) {
2103 			NET_LOCK();
2104 			error = if_setrdomain(ifp, ifr->ifr_rdomainid);
2105 			NET_UNLOCK();
2106 		}
2107 		break;
2108 
2109 	case SIOCAIFGROUP:
2110 		if ((error = suser(p)))
2111 			break;
2112 		NET_LOCK();
2113 		error = if_addgroup(ifp, ifgr->ifgr_group);
2114 		if (error == 0) {
2115 			error = (*ifp->if_ioctl)(ifp, cmd, data);
2116 			if (error == ENOTTY)
2117 				error = 0;
2118 		}
2119 		NET_UNLOCK();
2120 		break;
2121 
2122 	case SIOCDIFGROUP:
2123 		if ((error = suser(p)))
2124 			break;
2125 		NET_LOCK();
2126 		error = (*ifp->if_ioctl)(ifp, cmd, data);
2127 		if (error == ENOTTY)
2128 			error = 0;
2129 		if (error == 0)
2130 			error = if_delgroup(ifp, ifgr->ifgr_group);
2131 		NET_UNLOCK();
2132 		break;
2133 
2134 	case SIOCSIFLLADDR:
2135 		if ((error = suser(p)))
2136 			break;
2137 		if ((ifp->if_sadl == NULL) ||
2138 		    (ifr->ifr_addr.sa_len != ETHER_ADDR_LEN) ||
2139 		    (ETHER_IS_MULTICAST(ifr->ifr_addr.sa_data))) {
2140 			error = EINVAL;
2141 			break;
2142 		}
2143 		NET_LOCK();
2144 		switch (ifp->if_type) {
2145 		case IFT_ETHER:
2146 		case IFT_CARP:
2147 		case IFT_XETHER:
2148 		case IFT_ISO88025:
2149 			error = (*ifp->if_ioctl)(ifp, cmd, data);
2150 			if (error == ENOTTY)
2151 				error = 0;
2152 			if (error == 0)
2153 				error = if_setlladdr(ifp,
2154 				    ifr->ifr_addr.sa_data);
2155 			break;
2156 		default:
2157 			error = ENODEV;
2158 		}
2159 
2160 		if (error == 0)
2161 			ifnewlladdr(ifp);
2162 		NET_UNLOCK();
2163 		break;
2164 
2165 	case SIOCSIFLLPRIO:
2166 		if ((error = suser(p)))
2167 			break;
2168 		if (ifr->ifr_llprio < IFQ_MINPRIO ||
2169 		    ifr->ifr_llprio > IFQ_MAXPRIO) {
2170 			error = EINVAL;
2171 			break;
2172 		}
2173 		NET_LOCK();
2174 		ifp->if_llprio = ifr->ifr_llprio;
2175 		NET_UNLOCK();
2176 		break;
2177 
2178 	case SIOCGIFSFFPAGE:
2179 		error = suser(p);
2180 		if (error != 0)
2181 			break;
2182 
2183 		error = if_sffpage_check(data);
2184 		if (error != 0)
2185 			break;
2186 
2187 		/* don't take NET_LOCK because i2c reads take a long time */
2188 		error = ((*ifp->if_ioctl)(ifp, cmd, data));
2189 		break;
2190 
2191 	case SIOCSETKALIVE:
2192 	case SIOCDIFPHYADDR:
2193 	case SIOCSLIFPHYADDR:
2194 	case SIOCSLIFPHYRTABLE:
2195 	case SIOCSLIFPHYTTL:
2196 	case SIOCSLIFPHYDF:
2197 	case SIOCSLIFPHYECN:
2198 	case SIOCADDMULTI:
2199 	case SIOCDELMULTI:
2200 	case SIOCSIFMEDIA:
2201 	case SIOCSVNETID:
2202 	case SIOCDVNETID:
2203 	case SIOCSVNETFLOWID:
2204 	case SIOCSTXHPRIO:
2205 	case SIOCSRXHPRIO:
2206 	case SIOCSIFPAIR:
2207 	case SIOCSIFPARENT:
2208 	case SIOCDIFPARENT:
2209 	case SIOCSETMPWCFG:
2210 	case SIOCSETLABEL:
2211 	case SIOCDELLABEL:
2212 	case SIOCSPWE3CTRLWORD:
2213 	case SIOCSPWE3FAT:
2214 	case SIOCSPWE3NEIGHBOR:
2215 	case SIOCDPWE3NEIGHBOR:
2216 #if NBRIDGE > 0
2217 	case SIOCBRDGADD:
2218 	case SIOCBRDGDEL:
2219 	case SIOCBRDGSIFFLGS:
2220 	case SIOCBRDGSCACHE:
2221 	case SIOCBRDGADDS:
2222 	case SIOCBRDGDELS:
2223 	case SIOCBRDGSADDR:
2224 	case SIOCBRDGSTO:
2225 	case SIOCBRDGDADDR:
2226 	case SIOCBRDGFLUSH:
2227 	case SIOCBRDGADDL:
2228 	case SIOCBRDGSIFPROT:
2229 	case SIOCBRDGARL:
2230 	case SIOCBRDGFRL:
2231 	case SIOCBRDGSPRI:
2232 	case SIOCBRDGSHT:
2233 	case SIOCBRDGSFD:
2234 	case SIOCBRDGSMA:
2235 	case SIOCBRDGSIFPRIO:
2236 	case SIOCBRDGSIFCOST:
2237 	case SIOCBRDGSTXHC:
2238 	case SIOCBRDGSPROTO:
2239 	case SIOCSWSPORTNO:
2240 #endif
2241 		if ((error = suser(p)) != 0)
2242 			break;
2243 		/* FALLTHROUGH */
2244 	default:
2245 		error = ((*so->so_proto->pr_usrreq)(so, PRU_CONTROL,
2246 			(struct mbuf *) cmd, (struct mbuf *) data,
2247 			(struct mbuf *) ifp, p));
2248 		if (error != EOPNOTSUPP)
2249 			break;
2250 		switch (cmd) {
2251 		case SIOCAIFADDR:
2252 		case SIOCDIFADDR:
2253 		case SIOCSIFADDR:
2254 		case SIOCSIFNETMASK:
2255 		case SIOCSIFDSTADDR:
2256 		case SIOCSIFBRDADDR:
2257 #ifdef INET6
2258 		case SIOCAIFADDR_IN6:
2259 		case SIOCDIFADDR_IN6:
2260 #endif
2261 			error = suser(p);
2262 			break;
2263 		default:
2264 			error = 0;
2265 			break;
2266 		}
2267 		if (error)
2268 			break;
2269 		NET_LOCK();
2270 		error = ((*ifp->if_ioctl)(ifp, cmd, data));
2271 		NET_UNLOCK();
2272 		break;
2273 	}
2274 
2275 	if (oif_flags != ifp->if_flags || oif_xflags != ifp->if_xflags)
2276 		rtm_ifchg(ifp);
2277 
2278 	if (((oif_flags ^ ifp->if_flags) & IFF_UP) != 0)
2279 		getmicrotime(&ifp->if_lastchange);
2280 
2281 	if_put(ifp);
2282 
2283 	return (error);
2284 }
2285 
2286 int
2287 ifioctl_get(u_long cmd, caddr_t data)
2288 {
2289 	struct ifnet *ifp;
2290 	struct ifreq *ifr = (struct ifreq *)data;
2291 	char ifdescrbuf[IFDESCRSIZE];
2292 	char ifrtlabelbuf[RTLABEL_LEN];
2293 	int error = 0;
2294 	size_t bytesdone;
2295 	const char *label;
2296 
2297 	switch(cmd) {
2298 	case SIOCGIFCONF:
2299 		NET_RLOCK_IN_IOCTL();
2300 		error = ifconf(data);
2301 		NET_RUNLOCK_IN_IOCTL();
2302 		return (error);
2303 	case SIOCIFGCLONERS:
2304 		error = if_clone_list((struct if_clonereq *)data);
2305 		return (error);
2306 	case SIOCGIFGMEMB:
2307 		NET_RLOCK_IN_IOCTL();
2308 		error = if_getgroupmembers(data);
2309 		NET_RUNLOCK_IN_IOCTL();
2310 		return (error);
2311 	case SIOCGIFGATTR:
2312 		NET_RLOCK_IN_IOCTL();
2313 		error = if_getgroupattribs(data);
2314 		NET_RUNLOCK_IN_IOCTL();
2315 		return (error);
2316 	case SIOCGIFGLIST:
2317 		NET_RLOCK_IN_IOCTL();
2318 		error = if_getgrouplist(data);
2319 		NET_RUNLOCK_IN_IOCTL();
2320 		return (error);
2321 	}
2322 
2323 	ifp = if_unit(ifr->ifr_name);
2324 	if (ifp == NULL)
2325 		return (ENXIO);
2326 
2327 	NET_RLOCK_IN_IOCTL();
2328 
2329 	switch(cmd) {
2330 	case SIOCGIFFLAGS:
2331 		ifr->ifr_flags = ifp->if_flags;
2332 		if (ifq_is_oactive(&ifp->if_snd))
2333 			ifr->ifr_flags |= IFF_OACTIVE;
2334 		break;
2335 
2336 	case SIOCGIFXFLAGS:
2337 		ifr->ifr_flags = ifp->if_xflags & ~(IFXF_MPSAFE|IFXF_CLONED);
2338 		break;
2339 
2340 	case SIOCGIFMETRIC:
2341 		ifr->ifr_metric = ifp->if_metric;
2342 		break;
2343 
2344 	case SIOCGIFMTU:
2345 		ifr->ifr_mtu = ifp->if_mtu;
2346 		break;
2347 
2348 	case SIOCGIFHARDMTU:
2349 		ifr->ifr_hardmtu = ifp->if_hardmtu;
2350 		break;
2351 
2352 	case SIOCGIFDATA: {
2353 		struct if_data ifdata;
2354 		if_getdata(ifp, &ifdata);
2355 		error = copyout(&ifdata, ifr->ifr_data, sizeof(ifdata));
2356 		break;
2357 	}
2358 
2359 	case SIOCGIFDESCR:
2360 		strlcpy(ifdescrbuf, ifp->if_description, IFDESCRSIZE);
2361 		error = copyoutstr(ifdescrbuf, ifr->ifr_data, IFDESCRSIZE,
2362 		    &bytesdone);
2363 		break;
2364 
2365 	case SIOCGIFRTLABEL:
2366 		if (ifp->if_rtlabelid &&
2367 		    (label = rtlabel_id2name(ifp->if_rtlabelid)) != NULL) {
2368 			strlcpy(ifrtlabelbuf, label, RTLABEL_LEN);
2369 			error = copyoutstr(ifrtlabelbuf, ifr->ifr_data,
2370 			    RTLABEL_LEN, &bytesdone);
2371 		} else
2372 			error = ENOENT;
2373 		break;
2374 
2375 	case SIOCGIFPRIORITY:
2376 		ifr->ifr_metric = ifp->if_priority;
2377 		break;
2378 
2379 	case SIOCGIFRDOMAIN:
2380 		ifr->ifr_rdomainid = ifp->if_rdomain;
2381 		break;
2382 
2383 	case SIOCGIFGROUP:
2384 		error = if_getgroup(data, ifp);
2385 		break;
2386 
2387 	case SIOCGIFLLPRIO:
2388 		ifr->ifr_llprio = ifp->if_llprio;
2389 		break;
2390 
2391 	default:
2392 		panic("invalid ioctl %lu", cmd);
2393 	}
2394 
2395 	NET_RUNLOCK_IN_IOCTL();
2396 
2397 	if_put(ifp);
2398 
2399 	return (error);
2400 }
2401 
2402 static int
2403 if_sffpage_check(const caddr_t data)
2404 {
2405 	const struct if_sffpage *sff = (const struct if_sffpage *)data;
2406 
2407 	switch (sff->sff_addr) {
2408 	case IFSFF_ADDR_EEPROM:
2409 	case IFSFF_ADDR_DDM:
2410 		break;
2411 	default:
2412 		return (EINVAL);
2413 	}
2414 
2415 	return (0);
2416 }
2417 
2418 int
2419 if_txhprio_l2_check(int hdrprio)
2420 {
2421 	switch (hdrprio) {
2422 	case IF_HDRPRIO_PACKET:
2423 		return (0);
2424 	default:
2425 		if (hdrprio >= IF_HDRPRIO_MIN && hdrprio <= IF_HDRPRIO_MAX)
2426 			return (0);
2427 		break;
2428 	}
2429 
2430 	return (EINVAL);
2431 }
2432 
2433 int
2434 if_txhprio_l3_check(int hdrprio)
2435 {
2436 	switch (hdrprio) {
2437 	case IF_HDRPRIO_PACKET:
2438 	case IF_HDRPRIO_PAYLOAD:
2439 		return (0);
2440 	default:
2441 		if (hdrprio >= IF_HDRPRIO_MIN && hdrprio <= IF_HDRPRIO_MAX)
2442 			return (0);
2443 		break;
2444 	}
2445 
2446 	return (EINVAL);
2447 }
2448 
2449 int
2450 if_rxhprio_l2_check(int hdrprio)
2451 {
2452 	switch (hdrprio) {
2453 	case IF_HDRPRIO_PACKET:
2454 	case IF_HDRPRIO_OUTER:
2455 		return (0);
2456 	default:
2457 		if (hdrprio >= IF_HDRPRIO_MIN && hdrprio <= IF_HDRPRIO_MAX)
2458 			return (0);
2459 		break;
2460 	}
2461 
2462 	return (EINVAL);
2463 }
2464 
2465 int
2466 if_rxhprio_l3_check(int hdrprio)
2467 {
2468 	switch (hdrprio) {
2469 	case IF_HDRPRIO_PACKET:
2470 	case IF_HDRPRIO_PAYLOAD:
2471 	case IF_HDRPRIO_OUTER:
2472 		return (0);
2473 	default:
2474 		if (hdrprio >= IF_HDRPRIO_MIN && hdrprio <= IF_HDRPRIO_MAX)
2475 			return (0);
2476 		break;
2477 	}
2478 
2479 	return (EINVAL);
2480 }
2481 
2482 /*
2483  * Return interface configuration
2484  * of system.  List may be used
2485  * in later ioctl's (above) to get
2486  * other information.
2487  */
2488 int
2489 ifconf(caddr_t data)
2490 {
2491 	struct ifconf *ifc = (struct ifconf *)data;
2492 	struct ifnet *ifp;
2493 	struct ifaddr *ifa;
2494 	struct ifreq ifr, *ifrp;
2495 	int space = ifc->ifc_len, error = 0;
2496 
2497 	/* If ifc->ifc_len is 0, fill it in with the needed size and return. */
2498 	if (space == 0) {
2499 		TAILQ_FOREACH(ifp, &ifnet, if_list) {
2500 			struct sockaddr *sa;
2501 
2502 			if (TAILQ_EMPTY(&ifp->if_addrlist))
2503 				space += sizeof (ifr);
2504 			else
2505 				TAILQ_FOREACH(ifa,
2506 				    &ifp->if_addrlist, ifa_list) {
2507 					sa = ifa->ifa_addr;
2508 					if (sa->sa_len > sizeof(*sa))
2509 						space += sa->sa_len -
2510 						    sizeof(*sa);
2511 					space += sizeof(ifr);
2512 				}
2513 		}
2514 		ifc->ifc_len = space;
2515 		return (0);
2516 	}
2517 
2518 	ifrp = ifc->ifc_req;
2519 	TAILQ_FOREACH(ifp, &ifnet, if_list) {
2520 		if (space < sizeof(ifr))
2521 			break;
2522 		bcopy(ifp->if_xname, ifr.ifr_name, IFNAMSIZ);
2523 		if (TAILQ_EMPTY(&ifp->if_addrlist)) {
2524 			bzero((caddr_t)&ifr.ifr_addr, sizeof(ifr.ifr_addr));
2525 			error = copyout((caddr_t)&ifr, (caddr_t)ifrp,
2526 			    sizeof(ifr));
2527 			if (error)
2528 				break;
2529 			space -= sizeof (ifr), ifrp++;
2530 		} else
2531 			TAILQ_FOREACH(ifa, &ifp->if_addrlist, ifa_list) {
2532 				struct sockaddr *sa = ifa->ifa_addr;
2533 
2534 				if (space < sizeof(ifr))
2535 					break;
2536 				if (sa->sa_len <= sizeof(*sa)) {
2537 					ifr.ifr_addr = *sa;
2538 					error = copyout((caddr_t)&ifr,
2539 					    (caddr_t)ifrp, sizeof (ifr));
2540 					ifrp++;
2541 				} else {
2542 					space -= sa->sa_len - sizeof(*sa);
2543 					if (space < sizeof (ifr))
2544 						break;
2545 					error = copyout((caddr_t)&ifr,
2546 					    (caddr_t)ifrp,
2547 					    sizeof(ifr.ifr_name));
2548 					if (error == 0)
2549 						error = copyout((caddr_t)sa,
2550 						    (caddr_t)&ifrp->ifr_addr,
2551 						    sa->sa_len);
2552 					ifrp = (struct ifreq *)(sa->sa_len +
2553 					    (caddr_t)&ifrp->ifr_addr);
2554 				}
2555 				if (error)
2556 					break;
2557 				space -= sizeof (ifr);
2558 			}
2559 	}
2560 	ifc->ifc_len -= space;
2561 	return (error);
2562 }
2563 
2564 void
2565 if_counters_alloc(struct ifnet *ifp)
2566 {
2567 	KASSERT(ifp->if_counters == NULL);
2568 
2569 	ifp->if_counters = counters_alloc(ifc_ncounters);
2570 }
2571 
2572 void
2573 if_counters_free(struct ifnet *ifp)
2574 {
2575 	KASSERT(ifp->if_counters != NULL);
2576 
2577 	counters_free(ifp->if_counters, ifc_ncounters);
2578 	ifp->if_counters = NULL;
2579 }
2580 
2581 void
2582 if_getdata(struct ifnet *ifp, struct if_data *data)
2583 {
2584 	unsigned int i;
2585 
2586 	*data = ifp->if_data;
2587 
2588 	if (ifp->if_counters != NULL) {
2589 		uint64_t counters[ifc_ncounters];
2590 
2591 		counters_read(ifp->if_counters, counters, nitems(counters));
2592 
2593 		data->ifi_ipackets += counters[ifc_ipackets];
2594 		data->ifi_ierrors += counters[ifc_ierrors];
2595 		data->ifi_opackets += counters[ifc_opackets];
2596 		data->ifi_oerrors += counters[ifc_oerrors];
2597 		data->ifi_collisions += counters[ifc_collisions];
2598 		data->ifi_ibytes += counters[ifc_ibytes];
2599 		data->ifi_obytes += counters[ifc_obytes];
2600 		data->ifi_imcasts += counters[ifc_imcasts];
2601 		data->ifi_omcasts += counters[ifc_omcasts];
2602 		data->ifi_iqdrops += counters[ifc_iqdrops];
2603 		data->ifi_oqdrops += counters[ifc_oqdrops];
2604 		data->ifi_noproto += counters[ifc_noproto];
2605 	}
2606 
2607 	for (i = 0; i < ifp->if_nifqs; i++) {
2608 		struct ifqueue *ifq = ifp->if_ifqs[i];
2609 
2610 		ifq_add_data(ifq, data);
2611 	}
2612 
2613 	for (i = 0; i < ifp->if_niqs; i++) {
2614 		struct ifiqueue *ifiq = ifp->if_iqs[i];
2615 
2616 		ifiq_add_data(ifiq, data);
2617 	}
2618 }
2619 
2620 /*
2621  * Dummy functions replaced in ifnet during detach (if protocols decide to
2622  * fiddle with the if during detach.
2623  */
2624 void
2625 if_detached_qstart(struct ifqueue *ifq)
2626 {
2627 	ifq_purge(ifq);
2628 }
2629 
2630 int
2631 if_detached_ioctl(struct ifnet *ifp, u_long a, caddr_t b)
2632 {
2633 	return ENODEV;
2634 }
2635 
2636 /*
2637  * Create interface group without members
2638  */
2639 struct ifg_group *
2640 if_creategroup(const char *groupname)
2641 {
2642 	struct ifg_group	*ifg;
2643 
2644 	if ((ifg = malloc(sizeof(*ifg), M_TEMP, M_NOWAIT)) == NULL)
2645 		return (NULL);
2646 
2647 	strlcpy(ifg->ifg_group, groupname, sizeof(ifg->ifg_group));
2648 	ifg->ifg_refcnt = 1;
2649 	ifg->ifg_carp_demoted = 0;
2650 	TAILQ_INIT(&ifg->ifg_members);
2651 #if NPF > 0
2652 	pfi_attach_ifgroup(ifg);
2653 #endif
2654 	TAILQ_INSERT_TAIL(&ifg_head, ifg, ifg_next);
2655 
2656 	return (ifg);
2657 }
2658 
2659 /*
2660  * Add a group to an interface
2661  */
2662 int
2663 if_addgroup(struct ifnet *ifp, const char *groupname)
2664 {
2665 	struct ifg_list		*ifgl;
2666 	struct ifg_group	*ifg = NULL;
2667 	struct ifg_member	*ifgm;
2668 	size_t			 namelen;
2669 
2670 	namelen = strlen(groupname);
2671 	if (namelen == 0 || namelen >= IFNAMSIZ ||
2672 	    (groupname[namelen - 1] >= '0' && groupname[namelen - 1] <= '9'))
2673 		return (EINVAL);
2674 
2675 	TAILQ_FOREACH(ifgl, &ifp->if_groups, ifgl_next)
2676 		if (!strcmp(ifgl->ifgl_group->ifg_group, groupname))
2677 			return (EEXIST);
2678 
2679 	if ((ifgl = malloc(sizeof(*ifgl), M_TEMP, M_NOWAIT)) == NULL)
2680 		return (ENOMEM);
2681 
2682 	if ((ifgm = malloc(sizeof(*ifgm), M_TEMP, M_NOWAIT)) == NULL) {
2683 		free(ifgl, M_TEMP, sizeof(*ifgl));
2684 		return (ENOMEM);
2685 	}
2686 
2687 	TAILQ_FOREACH(ifg, &ifg_head, ifg_next)
2688 		if (!strcmp(ifg->ifg_group, groupname))
2689 			break;
2690 
2691 	if (ifg == NULL) {
2692 		ifg = if_creategroup(groupname);
2693 		if (ifg == NULL) {
2694 			free(ifgl, M_TEMP, sizeof(*ifgl));
2695 			free(ifgm, M_TEMP, sizeof(*ifgm));
2696 			return (ENOMEM);
2697 		}
2698 	} else
2699 		ifg->ifg_refcnt++;
2700 	KASSERT(ifg->ifg_refcnt != 0);
2701 
2702 	ifgl->ifgl_group = ifg;
2703 	ifgm->ifgm_ifp = ifp;
2704 
2705 	TAILQ_INSERT_TAIL(&ifg->ifg_members, ifgm, ifgm_next);
2706 	TAILQ_INSERT_TAIL(&ifp->if_groups, ifgl, ifgl_next);
2707 
2708 #if NPF > 0
2709 	pfi_group_addmember(groupname, ifp);
2710 #endif
2711 
2712 	return (0);
2713 }
2714 
2715 /*
2716  * Remove a group from an interface
2717  */
2718 int
2719 if_delgroup(struct ifnet *ifp, const char *groupname)
2720 {
2721 	struct ifg_list		*ifgl;
2722 	struct ifg_member	*ifgm;
2723 
2724 	TAILQ_FOREACH(ifgl, &ifp->if_groups, ifgl_next)
2725 		if (!strcmp(ifgl->ifgl_group->ifg_group, groupname))
2726 			break;
2727 	if (ifgl == NULL)
2728 		return (ENOENT);
2729 
2730 	TAILQ_REMOVE(&ifp->if_groups, ifgl, ifgl_next);
2731 
2732 	TAILQ_FOREACH(ifgm, &ifgl->ifgl_group->ifg_members, ifgm_next)
2733 		if (ifgm->ifgm_ifp == ifp)
2734 			break;
2735 
2736 	if (ifgm != NULL) {
2737 		TAILQ_REMOVE(&ifgl->ifgl_group->ifg_members, ifgm, ifgm_next);
2738 		free(ifgm, M_TEMP, sizeof(*ifgm));
2739 	}
2740 
2741 #if NPF > 0
2742 	pfi_group_change(groupname);
2743 #endif
2744 
2745 	KASSERT(ifgl->ifgl_group->ifg_refcnt != 0);
2746 	if (--ifgl->ifgl_group->ifg_refcnt == 0) {
2747 		TAILQ_REMOVE(&ifg_head, ifgl->ifgl_group, ifg_next);
2748 #if NPF > 0
2749 		pfi_detach_ifgroup(ifgl->ifgl_group);
2750 #endif
2751 		free(ifgl->ifgl_group, M_TEMP, sizeof(*ifgl->ifgl_group));
2752 	}
2753 
2754 	free(ifgl, M_TEMP, sizeof(*ifgl));
2755 
2756 	return (0);
2757 }
2758 
2759 /*
2760  * Stores all groups from an interface in memory pointed
2761  * to by data
2762  */
2763 int
2764 if_getgroup(caddr_t data, struct ifnet *ifp)
2765 {
2766 	int			 len, error;
2767 	struct ifg_list		*ifgl;
2768 	struct ifg_req		 ifgrq, *ifgp;
2769 	struct ifgroupreq	*ifgr = (struct ifgroupreq *)data;
2770 
2771 	if (ifgr->ifgr_len == 0) {
2772 		TAILQ_FOREACH(ifgl, &ifp->if_groups, ifgl_next)
2773 			ifgr->ifgr_len += sizeof(struct ifg_req);
2774 		return (0);
2775 	}
2776 
2777 	len = ifgr->ifgr_len;
2778 	ifgp = ifgr->ifgr_groups;
2779 	TAILQ_FOREACH(ifgl, &ifp->if_groups, ifgl_next) {
2780 		if (len < sizeof(ifgrq))
2781 			return (EINVAL);
2782 		bzero(&ifgrq, sizeof ifgrq);
2783 		strlcpy(ifgrq.ifgrq_group, ifgl->ifgl_group->ifg_group,
2784 		    sizeof(ifgrq.ifgrq_group));
2785 		if ((error = copyout((caddr_t)&ifgrq, (caddr_t)ifgp,
2786 		    sizeof(struct ifg_req))))
2787 			return (error);
2788 		len -= sizeof(ifgrq);
2789 		ifgp++;
2790 	}
2791 
2792 	return (0);
2793 }
2794 
2795 /*
2796  * Stores all members of a group in memory pointed to by data
2797  */
2798 int
2799 if_getgroupmembers(caddr_t data)
2800 {
2801 	struct ifgroupreq	*ifgr = (struct ifgroupreq *)data;
2802 	struct ifg_group	*ifg;
2803 	struct ifg_member	*ifgm;
2804 	struct ifg_req		 ifgrq, *ifgp;
2805 	int			 len, error;
2806 
2807 	TAILQ_FOREACH(ifg, &ifg_head, ifg_next)
2808 		if (!strcmp(ifg->ifg_group, ifgr->ifgr_name))
2809 			break;
2810 	if (ifg == NULL)
2811 		return (ENOENT);
2812 
2813 	if (ifgr->ifgr_len == 0) {
2814 		TAILQ_FOREACH(ifgm, &ifg->ifg_members, ifgm_next)
2815 			ifgr->ifgr_len += sizeof(ifgrq);
2816 		return (0);
2817 	}
2818 
2819 	len = ifgr->ifgr_len;
2820 	ifgp = ifgr->ifgr_groups;
2821 	TAILQ_FOREACH(ifgm, &ifg->ifg_members, ifgm_next) {
2822 		if (len < sizeof(ifgrq))
2823 			return (EINVAL);
2824 		bzero(&ifgrq, sizeof ifgrq);
2825 		strlcpy(ifgrq.ifgrq_member, ifgm->ifgm_ifp->if_xname,
2826 		    sizeof(ifgrq.ifgrq_member));
2827 		if ((error = copyout((caddr_t)&ifgrq, (caddr_t)ifgp,
2828 		    sizeof(struct ifg_req))))
2829 			return (error);
2830 		len -= sizeof(ifgrq);
2831 		ifgp++;
2832 	}
2833 
2834 	return (0);
2835 }
2836 
2837 int
2838 if_getgroupattribs(caddr_t data)
2839 {
2840 	struct ifgroupreq	*ifgr = (struct ifgroupreq *)data;
2841 	struct ifg_group	*ifg;
2842 
2843 	TAILQ_FOREACH(ifg, &ifg_head, ifg_next)
2844 		if (!strcmp(ifg->ifg_group, ifgr->ifgr_name))
2845 			break;
2846 	if (ifg == NULL)
2847 		return (ENOENT);
2848 
2849 	ifgr->ifgr_attrib.ifg_carp_demoted = ifg->ifg_carp_demoted;
2850 
2851 	return (0);
2852 }
2853 
2854 int
2855 if_setgroupattribs(caddr_t data)
2856 {
2857 	struct ifgroupreq	*ifgr = (struct ifgroupreq *)data;
2858 	struct ifg_group	*ifg;
2859 	struct ifg_member	*ifgm;
2860 	int			 demote;
2861 
2862 	TAILQ_FOREACH(ifg, &ifg_head, ifg_next)
2863 		if (!strcmp(ifg->ifg_group, ifgr->ifgr_name))
2864 			break;
2865 	if (ifg == NULL)
2866 		return (ENOENT);
2867 
2868 	demote = ifgr->ifgr_attrib.ifg_carp_demoted;
2869 	if (demote + ifg->ifg_carp_demoted > 0xff ||
2870 	    demote + ifg->ifg_carp_demoted < 0)
2871 		return (EINVAL);
2872 
2873 	ifg->ifg_carp_demoted += demote;
2874 
2875 	TAILQ_FOREACH(ifgm, &ifg->ifg_members, ifgm_next)
2876 		ifgm->ifgm_ifp->if_ioctl(ifgm->ifgm_ifp, SIOCSIFGATTR, data);
2877 
2878 	return (0);
2879 }
2880 
2881 /*
2882  * Stores all groups in memory pointed to by data
2883  */
2884 int
2885 if_getgrouplist(caddr_t data)
2886 {
2887 	struct ifgroupreq	*ifgr = (struct ifgroupreq *)data;
2888 	struct ifg_group	*ifg;
2889 	struct ifg_req		 ifgrq, *ifgp;
2890 	int			 len, error;
2891 
2892 	if (ifgr->ifgr_len == 0) {
2893 		TAILQ_FOREACH(ifg, &ifg_head, ifg_next)
2894 			ifgr->ifgr_len += sizeof(ifgrq);
2895 		return (0);
2896 	}
2897 
2898 	len = ifgr->ifgr_len;
2899 	ifgp = ifgr->ifgr_groups;
2900 	TAILQ_FOREACH(ifg, &ifg_head, ifg_next) {
2901 		if (len < sizeof(ifgrq))
2902 			return (EINVAL);
2903 		bzero(&ifgrq, sizeof ifgrq);
2904 		strlcpy(ifgrq.ifgrq_group, ifg->ifg_group,
2905 		    sizeof(ifgrq.ifgrq_group));
2906 		if ((error = copyout((caddr_t)&ifgrq, (caddr_t)ifgp,
2907 		    sizeof(struct ifg_req))))
2908 			return (error);
2909 		len -= sizeof(ifgrq);
2910 		ifgp++;
2911 	}
2912 
2913 	return (0);
2914 }
2915 
2916 void
2917 if_group_routechange(struct sockaddr *dst, struct sockaddr *mask)
2918 {
2919 	switch (dst->sa_family) {
2920 	case AF_INET:
2921 		if (satosin(dst)->sin_addr.s_addr == INADDR_ANY &&
2922 		    mask && (mask->sa_len == 0 ||
2923 		    satosin(mask)->sin_addr.s_addr == INADDR_ANY))
2924 			if_group_egress_build();
2925 		break;
2926 #ifdef INET6
2927 	case AF_INET6:
2928 		if (IN6_ARE_ADDR_EQUAL(&(satosin6(dst))->sin6_addr,
2929 		    &in6addr_any) && mask && (mask->sa_len == 0 ||
2930 		    IN6_ARE_ADDR_EQUAL(&(satosin6(mask))->sin6_addr,
2931 		    &in6addr_any)))
2932 			if_group_egress_build();
2933 		break;
2934 #endif
2935 	}
2936 }
2937 
2938 int
2939 if_group_egress_build(void)
2940 {
2941 	struct ifnet		*ifp;
2942 	struct ifg_group	*ifg;
2943 	struct ifg_member	*ifgm, *next;
2944 	struct sockaddr_in	 sa_in;
2945 #ifdef INET6
2946 	struct sockaddr_in6	 sa_in6;
2947 #endif
2948 	struct rtentry		*rt;
2949 
2950 	TAILQ_FOREACH(ifg, &ifg_head, ifg_next)
2951 		if (!strcmp(ifg->ifg_group, IFG_EGRESS))
2952 			break;
2953 
2954 	if (ifg != NULL)
2955 		TAILQ_FOREACH_SAFE(ifgm, &ifg->ifg_members, ifgm_next, next)
2956 			if_delgroup(ifgm->ifgm_ifp, IFG_EGRESS);
2957 
2958 	bzero(&sa_in, sizeof(sa_in));
2959 	sa_in.sin_len = sizeof(sa_in);
2960 	sa_in.sin_family = AF_INET;
2961 	rt = rtable_lookup(0, sintosa(&sa_in), sintosa(&sa_in), NULL, RTP_ANY);
2962 	while (rt != NULL) {
2963 		ifp = if_get(rt->rt_ifidx);
2964 		if (ifp != NULL) {
2965 			if_addgroup(ifp, IFG_EGRESS);
2966 			if_put(ifp);
2967 		}
2968 		rt = rtable_iterate(rt);
2969 	}
2970 
2971 #ifdef INET6
2972 	bcopy(&sa6_any, &sa_in6, sizeof(sa_in6));
2973 	rt = rtable_lookup(0, sin6tosa(&sa_in6), sin6tosa(&sa_in6), NULL,
2974 	    RTP_ANY);
2975 	while (rt != NULL) {
2976 		ifp = if_get(rt->rt_ifidx);
2977 		if (ifp != NULL) {
2978 			if_addgroup(ifp, IFG_EGRESS);
2979 			if_put(ifp);
2980 		}
2981 		rt = rtable_iterate(rt);
2982 	}
2983 #endif /* INET6 */
2984 
2985 	return (0);
2986 }
2987 
2988 /*
2989  * Set/clear promiscuous mode on interface ifp based on the truth value
2990  * of pswitch.  The calls are reference counted so that only the first
2991  * "on" request actually has an effect, as does the final "off" request.
2992  * Results are undefined if the "off" and "on" requests are not matched.
2993  */
2994 int
2995 ifpromisc(struct ifnet *ifp, int pswitch)
2996 {
2997 	struct ifreq ifr;
2998 	unsigned short oif_flags;
2999 	int oif_pcount, error;
3000 
3001 	NET_ASSERT_LOCKED(); /* modifying if_flags and if_pcount */
3002 
3003 	oif_flags = ifp->if_flags;
3004 	oif_pcount = ifp->if_pcount;
3005 	if (pswitch) {
3006 		if (ifp->if_pcount++ != 0)
3007 			return (0);
3008 		ifp->if_flags |= IFF_PROMISC;
3009 	} else {
3010 		if (--ifp->if_pcount > 0)
3011 			return (0);
3012 		ifp->if_flags &= ~IFF_PROMISC;
3013 	}
3014 
3015 	if ((ifp->if_flags & IFF_UP) == 0)
3016 		return (0);
3017 
3018 	memset(&ifr, 0, sizeof(ifr));
3019 	ifr.ifr_flags = ifp->if_flags;
3020 	error = ((*ifp->if_ioctl)(ifp, SIOCSIFFLAGS, (caddr_t)&ifr));
3021 	if (error) {
3022 		ifp->if_flags = oif_flags;
3023 		ifp->if_pcount = oif_pcount;
3024 	}
3025 
3026 	return (error);
3027 }
3028 
3029 void
3030 ifa_add(struct ifnet *ifp, struct ifaddr *ifa)
3031 {
3032 	TAILQ_INSERT_TAIL(&ifp->if_addrlist, ifa, ifa_list);
3033 }
3034 
3035 void
3036 ifa_del(struct ifnet *ifp, struct ifaddr *ifa)
3037 {
3038 	TAILQ_REMOVE(&ifp->if_addrlist, ifa, ifa_list);
3039 }
3040 
3041 void
3042 ifa_update_broadaddr(struct ifnet *ifp, struct ifaddr *ifa, struct sockaddr *sa)
3043 {
3044 	if (ifa->ifa_broadaddr->sa_len != sa->sa_len)
3045 		panic("ifa_update_broadaddr does not support dynamic length");
3046 	bcopy(sa, ifa->ifa_broadaddr, sa->sa_len);
3047 }
3048 
3049 #ifdef DDB
3050 /* debug function, can be called from ddb> */
3051 void
3052 ifa_print_all(void)
3053 {
3054 	struct ifnet *ifp;
3055 	struct ifaddr *ifa;
3056 
3057 	TAILQ_FOREACH(ifp, &ifnet, if_list) {
3058 		TAILQ_FOREACH(ifa, &ifp->if_addrlist, ifa_list) {
3059 			char addr[INET6_ADDRSTRLEN];
3060 
3061 			switch (ifa->ifa_addr->sa_family) {
3062 			case AF_INET:
3063 				printf("%s", inet_ntop(AF_INET,
3064 				    &satosin(ifa->ifa_addr)->sin_addr,
3065 				    addr, sizeof(addr)));
3066 				break;
3067 #ifdef INET6
3068 			case AF_INET6:
3069 				printf("%s", inet_ntop(AF_INET6,
3070 				    &(satosin6(ifa->ifa_addr))->sin6_addr,
3071 				    addr, sizeof(addr)));
3072 				break;
3073 #endif
3074 			}
3075 			printf(" on %s\n", ifp->if_xname);
3076 		}
3077 	}
3078 }
3079 #endif /* DDB */
3080 
3081 void
3082 ifnewlladdr(struct ifnet *ifp)
3083 {
3084 #ifdef INET6
3085 	struct ifaddr *ifa;
3086 #endif
3087 	struct ifreq ifrq;
3088 	short up;
3089 	int s;
3090 
3091 	s = splnet();
3092 	up = ifp->if_flags & IFF_UP;
3093 
3094 	if (up) {
3095 		/* go down for a moment... */
3096 		ifp->if_flags &= ~IFF_UP;
3097 		ifrq.ifr_flags = ifp->if_flags;
3098 		(*ifp->if_ioctl)(ifp, SIOCSIFFLAGS, (caddr_t)&ifrq);
3099 	}
3100 
3101 	ifp->if_flags |= IFF_UP;
3102 	ifrq.ifr_flags = ifp->if_flags;
3103 	(*ifp->if_ioctl)(ifp, SIOCSIFFLAGS, (caddr_t)&ifrq);
3104 
3105 #ifdef INET6
3106 	/*
3107 	 * Update the link-local address.  Don't do it if we're
3108 	 * a router to avoid confusing hosts on the network.
3109 	 */
3110 	if (!ip6_forwarding) {
3111 		ifa = &in6ifa_ifpforlinklocal(ifp, 0)->ia_ifa;
3112 		if (ifa) {
3113 			in6_purgeaddr(ifa);
3114 			if_hooks_run(&ifp->if_addrhooks);
3115 			in6_ifattach(ifp);
3116 		}
3117 	}
3118 #endif
3119 	if (!up) {
3120 		/* go back down */
3121 		ifp->if_flags &= ~IFF_UP;
3122 		ifrq.ifr_flags = ifp->if_flags;
3123 		(*ifp->if_ioctl)(ifp, SIOCSIFFLAGS, (caddr_t)&ifrq);
3124 	}
3125 	splx(s);
3126 }
3127 
3128 void
3129 if_addrhook_add(struct ifnet *ifp, struct task *t)
3130 {
3131 	mtx_enter(&if_hooks_mtx);
3132 	TAILQ_INSERT_TAIL(&ifp->if_addrhooks, t, t_entry);
3133 	mtx_leave(&if_hooks_mtx);
3134 }
3135 
3136 void
3137 if_addrhook_del(struct ifnet *ifp, struct task *t)
3138 {
3139 	mtx_enter(&if_hooks_mtx);
3140 	TAILQ_REMOVE(&ifp->if_addrhooks, t, t_entry);
3141 	mtx_leave(&if_hooks_mtx);
3142 }
3143 
3144 void
3145 if_addrhooks_run(struct ifnet *ifp)
3146 {
3147 	if_hooks_run(&ifp->if_addrhooks);
3148 }
3149 
3150 void
3151 if_rxr_init(struct if_rxring *rxr, u_int lwm, u_int hwm)
3152 {
3153 	extern int ticks;
3154 
3155 	memset(rxr, 0, sizeof(*rxr));
3156 
3157 	rxr->rxr_adjusted = ticks;
3158 	rxr->rxr_cwm = rxr->rxr_lwm = lwm;
3159 	rxr->rxr_hwm = hwm;
3160 }
3161 
3162 static inline void
3163 if_rxr_adjust_cwm(struct if_rxring *rxr)
3164 {
3165 	extern int ticks;
3166 
3167 	if (rxr->rxr_alive >= rxr->rxr_lwm)
3168 		return;
3169 	else if (rxr->rxr_cwm < rxr->rxr_hwm)
3170 		rxr->rxr_cwm++;
3171 
3172 	rxr->rxr_adjusted = ticks;
3173 }
3174 
3175 void
3176 if_rxr_livelocked(struct if_rxring *rxr)
3177 {
3178 	extern int ticks;
3179 
3180 	if (ticks - rxr->rxr_adjusted >= 1) {
3181 		if (rxr->rxr_cwm > rxr->rxr_lwm)
3182 			rxr->rxr_cwm--;
3183 
3184 		rxr->rxr_adjusted = ticks;
3185 	}
3186 }
3187 
3188 u_int
3189 if_rxr_get(struct if_rxring *rxr, u_int max)
3190 {
3191 	extern int ticks;
3192 	u_int diff;
3193 
3194 	if (ticks - rxr->rxr_adjusted >= 1) {
3195 		/* we're free to try for an adjustment */
3196 		if_rxr_adjust_cwm(rxr);
3197 	}
3198 
3199 	if (rxr->rxr_alive >= rxr->rxr_cwm)
3200 		return (0);
3201 
3202 	diff = min(rxr->rxr_cwm - rxr->rxr_alive, max);
3203 	rxr->rxr_alive += diff;
3204 
3205 	return (diff);
3206 }
3207 
3208 int
3209 if_rxr_info_ioctl(struct if_rxrinfo *uifri, u_int t, struct if_rxring_info *e)
3210 {
3211 	struct if_rxrinfo kifri;
3212 	int error;
3213 	u_int n;
3214 
3215 	error = copyin(uifri, &kifri, sizeof(kifri));
3216 	if (error)
3217 		return (error);
3218 
3219 	n = min(t, kifri.ifri_total);
3220 	kifri.ifri_total = t;
3221 
3222 	if (n > 0) {
3223 		error = copyout(e, kifri.ifri_entries, sizeof(*e) * n);
3224 		if (error)
3225 			return (error);
3226 	}
3227 
3228 	return (copyout(&kifri, uifri, sizeof(kifri)));
3229 }
3230 
3231 int
3232 if_rxr_ioctl(struct if_rxrinfo *ifri, const char *name, u_int size,
3233     struct if_rxring *rxr)
3234 {
3235 	struct if_rxring_info ifr;
3236 
3237 	memset(&ifr, 0, sizeof(ifr));
3238 
3239 	if (name != NULL)
3240 		strlcpy(ifr.ifr_name, name, sizeof(ifr.ifr_name));
3241 
3242 	ifr.ifr_size = size;
3243 	ifr.ifr_info = *rxr;
3244 
3245 	return (if_rxr_info_ioctl(ifri, 1, &ifr));
3246 }
3247 
3248 /*
3249  * Network stack input queues.
3250  */
3251 
3252 void
3253 niq_init(struct niqueue *niq, u_int maxlen, u_int isr)
3254 {
3255 	mq_init(&niq->ni_q, maxlen, IPL_NET);
3256 	niq->ni_isr = isr;
3257 }
3258 
3259 int
3260 niq_enqueue(struct niqueue *niq, struct mbuf *m)
3261 {
3262 	int rv;
3263 
3264 	rv = mq_enqueue(&niq->ni_q, m);
3265 	if (rv == 0)
3266 		schednetisr(niq->ni_isr);
3267 	else
3268 		if_congestion();
3269 
3270 	return (rv);
3271 }
3272 
3273 int
3274 niq_enlist(struct niqueue *niq, struct mbuf_list *ml)
3275 {
3276 	int rv;
3277 
3278 	rv = mq_enlist(&niq->ni_q, ml);
3279 	if (rv == 0)
3280 		schednetisr(niq->ni_isr);
3281 	else
3282 		if_congestion();
3283 
3284 	return (rv);
3285 }
3286 
3287 __dead void
3288 unhandled_af(int af)
3289 {
3290 	panic("unhandled af %d", af);
3291 }
3292 
3293 /*
3294  * XXXSMP This tunable is here to work around the fact that IPsec
3295  * globals aren't ready to be accessed by multiple threads in
3296  * parallel.
3297  */
3298 int		 nettaskqs = NET_TASKQ;
3299 
3300 struct taskq *
3301 net_tq(unsigned int ifindex)
3302 {
3303 	struct taskq *t = NULL;
3304 
3305 	t = nettqmp[ifindex % nettaskqs];
3306 
3307 	return (t);
3308 }
3309