xref: /openbsd-src/sys/net/if.c (revision fb8aa7497fded39583f40e800732f9c046411717)
1 /*	$OpenBSD: if.c,v 1.434 2016/06/10 20:33:29 vgross 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 "trunk.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/sysctl.h>
85 #include <sys/task.h>
86 #include <sys/atomic.h>
87 #include <sys/proc.h>
88 
89 #include <dev/rndvar.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 void	if_attachsetup(struct ifnet *);
133 void	if_attachdomain(struct ifnet *);
134 void	if_attach_common(struct ifnet *);
135 
136 void	if_detached_start(struct ifnet *);
137 int	if_detached_ioctl(struct ifnet *, u_long, caddr_t);
138 
139 int	if_getgroup(caddr_t, struct ifnet *);
140 int	if_getgroupmembers(caddr_t);
141 int	if_getgroupattribs(caddr_t);
142 int	if_setgroupattribs(caddr_t);
143 
144 void	if_linkstate(void *);
145 
146 int	if_clone_list(struct if_clonereq *);
147 struct if_clone	*if_clone_lookup(const char *, int *);
148 
149 int	if_group_egress_build(void);
150 
151 void	if_watchdog_task(void *);
152 
153 void	if_input_process(void *);
154 void	if_netisr(void *);
155 
156 #ifdef DDB
157 void	ifa_print_all(void);
158 #endif
159 
160 void	if_start_locked(struct ifnet *ifp);
161 
162 /*
163  * interface index map
164  *
165  * the kernel maintains a mapping of interface indexes to struct ifnet
166  * pointers.
167  *
168  * the map is an array of struct ifnet pointers prefixed by an if_map
169  * structure. the if_map structure stores the length of its array.
170  *
171  * as interfaces are attached to the system, the map is grown on demand
172  * up to USHRT_MAX entries.
173  *
174  * interface index 0 is reserved and represents no interface. this
175  * supports the use of the interface index as the scope for IPv6 link
176  * local addresses, where scope 0 means no scope has been specified.
177  * it also supports the use of interface index as the unique identifier
178  * for network interfaces in SNMP applications as per RFC2863. therefore
179  * if_get(0) returns NULL.
180  */
181 
182 void if_ifp_dtor(void *, void *);
183 void if_map_dtor(void *, void *);
184 struct ifnet *if_ref(struct ifnet *);
185 
186 /*
187  * struct if_map
188  *
189  * bounded array of ifnet srp pointers used to fetch references of live
190  * interfaces with if_get().
191  */
192 
193 struct if_map {
194 	unsigned long		 limit;
195 	/* followed by limit ifnet srp pointers */
196 };
197 
198 /*
199  * struct if_idxmap
200  *
201  * infrastructure to manage updates and accesses to the current if_map.
202  */
203 
204 struct if_idxmap {
205 	unsigned int		 serial;
206 	unsigned int		 count;
207 	struct srp		 map;
208 };
209 
210 void	if_idxmap_init(unsigned int);
211 void	if_idxmap_insert(struct ifnet *);
212 void	if_idxmap_remove(struct ifnet *);
213 
214 TAILQ_HEAD(, ifg_group) ifg_head = TAILQ_HEAD_INITIALIZER(ifg_head);
215 LIST_HEAD(, if_clone) if_cloners = LIST_HEAD_INITIALIZER(if_cloners);
216 int if_cloners_count;
217 
218 struct timeout net_tick_to;
219 void	net_tick(void *);
220 int	net_livelocked(void);
221 int	ifq_congestion;
222 
223 int		 netisr;
224 struct taskq	*softnettq;
225 
226 struct mbuf_queue if_input_queue = MBUF_QUEUE_INITIALIZER(8192, IPL_NET);
227 struct task if_input_task = TASK_INITIALIZER(if_input_process, &if_input_queue);
228 struct task if_input_task_locked = TASK_INITIALIZER(if_netisr, NULL);
229 
230 /*
231  * Network interface utility routines.
232  */
233 void
234 ifinit(void)
235 {
236 	/*
237 	 * most machines boot with 4 or 5 interfaces, so size the initial map
238 	 * to accomodate this
239 	 */
240 	if_idxmap_init(8);
241 
242 	timeout_set(&net_tick_to, net_tick, &net_tick_to);
243 
244 	softnettq = taskq_create("softnet", 1, IPL_NET,
245 	    TASKQ_MPSAFE | TASKQ_CANTSLEEP);
246 	if (softnettq == NULL)
247 		panic("unable to create softnet taskq");
248 
249 	net_tick(&net_tick_to);
250 }
251 
252 static struct if_idxmap if_idxmap = {
253 	0,
254 	0,
255 	SRP_INITIALIZER()
256 };
257 
258 struct srp_gc if_ifp_gc = SRP_GC_INITIALIZER(if_ifp_dtor, NULL);
259 struct srp_gc if_map_gc = SRP_GC_INITIALIZER(if_map_dtor, NULL);
260 
261 struct ifnet_head ifnet = TAILQ_HEAD_INITIALIZER(ifnet);
262 unsigned int lo0ifidx;
263 
264 void
265 if_idxmap_init(unsigned int limit)
266 {
267 	struct if_map *if_map;
268 	struct srp *map;
269 	unsigned int i;
270 
271 	if_idxmap.serial = 1; /* skip ifidx 0 so it can return NULL */
272 
273 	if_map = malloc(sizeof(*if_map) + limit * sizeof(*map),
274 	    M_IFADDR, M_WAITOK);
275 
276 	if_map->limit = limit;
277 	map = (struct srp *)(if_map + 1);
278 	for (i = 0; i < limit; i++)
279 		srp_init(&map[i]);
280 
281 	/* this is called early so there's nothing to race with */
282 	srp_update_locked(&if_map_gc, &if_idxmap.map, if_map);
283 }
284 
285 void
286 if_idxmap_insert(struct ifnet *ifp)
287 {
288 	struct if_map *if_map;
289 	struct srp *map;
290 	unsigned int index, i;
291 
292 	refcnt_init(&ifp->if_refcnt);
293 
294 	/* the kernel lock guarantees serialised modifications to if_idxmap */
295 	KERNEL_ASSERT_LOCKED();
296 
297 	if (++if_idxmap.count > USHRT_MAX)
298 		panic("too many interfaces");
299 
300 	if_map = srp_get_locked(&if_idxmap.map);
301 	map = (struct srp *)(if_map + 1);
302 
303 	index = if_idxmap.serial++ & USHRT_MAX;
304 
305 	if (index >= if_map->limit) {
306 		struct if_map *nif_map;
307 		struct srp *nmap;
308 		unsigned int nlimit;
309 		struct ifnet *nifp;
310 
311 		nlimit = if_map->limit * 2;
312 		nif_map = malloc(sizeof(*nif_map) + nlimit * sizeof(*nmap),
313 		    M_IFADDR, M_WAITOK);
314 		nmap = (struct srp *)(nif_map + 1);
315 
316 		nif_map->limit = nlimit;
317 		for (i = 0; i < if_map->limit; i++) {
318 			srp_init(&nmap[i]);
319 			nifp = srp_get_locked(&map[i]);
320 			if (nifp != NULL) {
321 				srp_update_locked(&if_ifp_gc, &nmap[i],
322 				    if_ref(nifp));
323 			}
324 		}
325 
326 		while (i < nlimit) {
327 			srp_init(&nmap[i]);
328 			i++;
329 		}
330 
331 		srp_update_locked(&if_map_gc, &if_idxmap.map, nif_map);
332 		if_map = nif_map;
333 		map = nmap;
334 	}
335 
336 	/* pick the next free index */
337 	for (i = 0; i < USHRT_MAX; i++) {
338 		if (index != 0 && srp_get_locked(&map[index]) == NULL)
339 			break;
340 
341 		index = if_idxmap.serial++ & USHRT_MAX;
342 	}
343 
344 	/* commit */
345 	ifp->if_index = index;
346 	srp_update_locked(&if_ifp_gc, &map[index], if_ref(ifp));
347 }
348 
349 void
350 if_idxmap_remove(struct ifnet *ifp)
351 {
352 	struct if_map *if_map;
353 	struct srp *map;
354 	unsigned int index;
355 
356 	index = ifp->if_index;
357 
358 	/* the kernel lock guarantees serialised modifications to if_idxmap */
359 	KERNEL_ASSERT_LOCKED();
360 
361 	if_map = srp_get_locked(&if_idxmap.map);
362 	KASSERT(index < if_map->limit);
363 
364 	map = (struct srp *)(if_map + 1);
365 	KASSERT(ifp == (struct ifnet *)srp_get_locked(&map[index]));
366 
367 	srp_update_locked(&if_ifp_gc, &map[index], NULL);
368 	if_idxmap.count--;
369 	/* end of if_idxmap modifications */
370 
371 	/* sleep until the last reference is released */
372 	refcnt_finalize(&ifp->if_refcnt, "ifidxrm");
373 }
374 
375 void
376 if_ifp_dtor(void *null, void *ifp)
377 {
378 	if_put(ifp);
379 }
380 
381 void
382 if_map_dtor(void *null, void *m)
383 {
384 	struct if_map *if_map = m;
385 	struct srp *map = (struct srp *)(if_map + 1);
386 	unsigned int i;
387 
388 	/*
389 	 * dont need to serialize the use of update_locked since this is
390 	 * the last reference to this map. there's nothing to race against.
391 	 */
392 	for (i = 0; i < if_map->limit; i++)
393 		srp_update_locked(&if_ifp_gc, &map[i], NULL);
394 
395 	free(if_map, M_IFADDR, sizeof(*if_map) + if_map->limit * sizeof(*map));
396 }
397 
398 /*
399  * Attach an interface to the
400  * list of "active" interfaces.
401  */
402 void
403 if_attachsetup(struct ifnet *ifp)
404 {
405 	TAILQ_INIT(&ifp->if_groups);
406 
407 	if_addgroup(ifp, IFG_ALL);
408 
409 	if_attachdomain(ifp);
410 #if NPF > 0
411 	pfi_attach_ifnet(ifp);
412 #endif
413 
414 	task_set(ifp->if_watchdogtask, if_watchdog_task, ifp);
415 	timeout_set(ifp->if_slowtimo, if_slowtimo, ifp);
416 	if_slowtimo(ifp);
417 
418 	task_set(ifp->if_linkstatetask, if_linkstate, ifp);
419 
420 	if_idxmap_insert(ifp);
421 	KASSERT(if_get(0) == NULL);
422 
423 	/* Announce the interface. */
424 	rt_ifannouncemsg(ifp, IFAN_ARRIVAL);
425 }
426 
427 /*
428  * Allocate the link level name for the specified interface.  This
429  * is an attachment helper.  It must be called after ifp->if_addrlen
430  * is initialized, which may not be the case when if_attach() is
431  * called.
432  */
433 void
434 if_alloc_sadl(struct ifnet *ifp)
435 {
436 	unsigned int socksize;
437 	int namelen, masklen;
438 	struct sockaddr_dl *sdl;
439 
440 	/*
441 	 * If the interface already has a link name, release it
442 	 * now.  This is useful for interfaces that can change
443 	 * link types, and thus switch link names often.
444 	 */
445 	if (ifp->if_sadl != NULL)
446 		if_free_sadl(ifp);
447 
448 	namelen = strlen(ifp->if_xname);
449 	masklen = offsetof(struct sockaddr_dl, sdl_data[0]) + namelen;
450 	socksize = masklen + ifp->if_addrlen;
451 #define ROUNDUP(a) (1 + (((a) - 1) | (sizeof(long) - 1)))
452 	if (socksize < sizeof(*sdl))
453 		socksize = sizeof(*sdl);
454 	socksize = ROUNDUP(socksize);
455 	sdl = malloc(socksize, M_IFADDR, M_WAITOK|M_ZERO);
456 	sdl->sdl_len = socksize;
457 	sdl->sdl_family = AF_LINK;
458 	bcopy(ifp->if_xname, sdl->sdl_data, namelen);
459 	sdl->sdl_nlen = namelen;
460 	sdl->sdl_alen = ifp->if_addrlen;
461 	sdl->sdl_index = ifp->if_index;
462 	sdl->sdl_type = ifp->if_type;
463 	ifp->if_sadl = sdl;
464 }
465 
466 /*
467  * Free the link level name for the specified interface.  This is
468  * a detach helper.  This is called from if_detach() or from
469  * link layer type specific detach functions.
470  */
471 void
472 if_free_sadl(struct ifnet *ifp)
473 {
474 	free(ifp->if_sadl, M_IFADDR, 0);
475 	ifp->if_sadl = NULL;
476 }
477 
478 void
479 if_attachdomain(struct ifnet *ifp)
480 {
481 	struct domain *dp;
482 	int i, s;
483 
484 	s = splnet();
485 
486 	/* address family dependent data region */
487 	bzero(ifp->if_afdata, sizeof(ifp->if_afdata));
488 	for (i = 0; (dp = domains[i]) != NULL; i++) {
489 		if (dp->dom_ifattach)
490 			ifp->if_afdata[dp->dom_family] =
491 			    (*dp->dom_ifattach)(ifp);
492 	}
493 
494 	splx(s);
495 }
496 
497 void
498 if_attachhead(struct ifnet *ifp)
499 {
500 	if_attach_common(ifp);
501 	TAILQ_INSERT_HEAD(&ifnet, ifp, if_list);
502 	if_attachsetup(ifp);
503 }
504 
505 void
506 if_attach(struct ifnet *ifp)
507 {
508 	if_attach_common(ifp);
509 	TAILQ_INSERT_TAIL(&ifnet, ifp, if_list);
510 	if_attachsetup(ifp);
511 }
512 
513 void
514 if_attach_common(struct ifnet *ifp)
515 {
516 	TAILQ_INIT(&ifp->if_addrlist);
517 	TAILQ_INIT(&ifp->if_maddrlist);
518 
519 	ifq_init(&ifp->if_snd, ifp);
520 
521 	ifp->if_addrhooks = malloc(sizeof(*ifp->if_addrhooks),
522 	    M_TEMP, M_WAITOK);
523 	TAILQ_INIT(ifp->if_addrhooks);
524 	ifp->if_linkstatehooks = malloc(sizeof(*ifp->if_linkstatehooks),
525 	    M_TEMP, M_WAITOK);
526 	TAILQ_INIT(ifp->if_linkstatehooks);
527 	ifp->if_detachhooks = malloc(sizeof(*ifp->if_detachhooks),
528 	    M_TEMP, M_WAITOK);
529 	TAILQ_INIT(ifp->if_detachhooks);
530 
531 	if (ifp->if_rtrequest == NULL)
532 		ifp->if_rtrequest = if_rtrequest_dummy;
533 	ifp->if_slowtimo = malloc(sizeof(*ifp->if_slowtimo), M_TEMP,
534 	    M_WAITOK|M_ZERO);
535 	ifp->if_watchdogtask = malloc(sizeof(*ifp->if_watchdogtask),
536 	    M_TEMP, M_WAITOK|M_ZERO);
537 	ifp->if_linkstatetask = malloc(sizeof(*ifp->if_linkstatetask),
538 	    M_TEMP, M_WAITOK|M_ZERO);
539 	ifp->if_llprio = IFQ_DEFPRIO;
540 
541 	SRPL_INIT(&ifp->if_inputs);
542 }
543 
544 void
545 if_start(struct ifnet *ifp)
546 {
547 	if (ISSET(ifp->if_xflags, IFXF_MPSAFE))
548 		ifq_start(&ifp->if_snd);
549 	else
550 		if_start_locked(ifp);
551 }
552 
553 void
554 if_start_locked(struct ifnet *ifp)
555 {
556 	int s;
557 
558 	KERNEL_LOCK();
559 	s = splnet();
560 	ifp->if_start(ifp);
561 	splx(s);
562 	KERNEL_UNLOCK();
563 }
564 
565 int
566 if_enqueue(struct ifnet *ifp, struct mbuf *m)
567 {
568 	int length, error = 0;
569 	unsigned short mflags;
570 
571 #if NBRIDGE > 0
572 	if (ifp->if_bridgeport && (m->m_flags & M_PROTO1) == 0) {
573 		KERNEL_LOCK();
574 		error = bridge_output(ifp, m, NULL, NULL);
575 		KERNEL_UNLOCK();
576 		return (error);
577 	}
578 #endif
579 
580 	length = m->m_pkthdr.len;
581 	mflags = m->m_flags;
582 
583 	/*
584 	 * Queue message on interface, and start output if interface
585 	 * not yet active.
586 	 */
587 	IFQ_ENQUEUE(&ifp->if_snd, m, error);
588 	if (error)
589 		return (error);
590 
591 	ifp->if_obytes += length;
592 	if (mflags & M_MCAST)
593 		ifp->if_omcasts++;
594 
595 	if_start(ifp);
596 
597 	return (0);
598 }
599 
600 void
601 if_input(struct ifnet *ifp, struct mbuf_list *ml)
602 {
603 	struct mbuf *m;
604 	size_t ibytes = 0;
605 #if NBPFILTER > 0
606 	caddr_t if_bpf;
607 #endif
608 
609 	MBUF_LIST_FOREACH(ml, m) {
610 		m->m_pkthdr.ph_ifidx = ifp->if_index;
611 		m->m_pkthdr.ph_rtableid = ifp->if_rdomain;
612 		ibytes += m->m_pkthdr.len;
613 	}
614 
615 	ifp->if_ipackets += ml_len(ml);
616 	ifp->if_ibytes += ibytes;
617 
618 #if NBPFILTER > 0
619 	if_bpf = ifp->if_bpf;
620 	if (if_bpf) {
621 		MBUF_LIST_FOREACH(ml, m)
622 			if (bpf_mtap_ether(if_bpf, m, BPF_DIRECTION_IN) != 0)
623 				m->m_flags |= M_FILDROP;
624 	}
625 #endif
626 
627 	mq_enlist(&if_input_queue, ml);
628 	task_add(softnettq, &if_input_task);
629 }
630 
631 int
632 if_input_local(struct ifnet *ifp, struct mbuf *m, sa_family_t af)
633 {
634 	struct niqueue *ifq = NULL;
635 
636 #if NBPFILTER > 0
637 	/*
638 	 * Only send packets to bpf if they are destinated to local
639 	 * addresses.
640 	 *
641 	 * if_input_local() is also called for SIMPLEX interfaces to
642 	 * duplicate packets for local use.  But don't dup them to bpf.
643 	 */
644 	if (ifp->if_flags & IFF_LOOPBACK) {
645 		caddr_t if_bpf = ifp->if_bpf;
646 
647 		if (if_bpf)
648 			bpf_mtap_af(if_bpf, af, m, BPF_DIRECTION_OUT);
649 	}
650 #endif
651 	m->m_pkthdr.ph_ifidx = ifp->if_index;
652 
653 	ifp->if_opackets++;
654 	ifp->if_obytes += m->m_pkthdr.len;
655 
656 	switch (af) {
657 	case AF_INET:
658 		ifq = &ipintrq;
659 		break;
660 #ifdef INET6
661 	case AF_INET6:
662 		ifq = &ip6intrq;
663 		break;
664 #endif /* INET6 */
665 #ifdef MPLS
666 	case AF_MPLS:
667 		ifp->if_ipackets++;
668 		ifp->if_ibytes += m->m_pkthdr.len;
669 		mpls_input(m);
670 		return (0);
671 #endif /* MPLS */
672 	default:
673 		printf("%s: can't handle af%d\n", ifp->if_xname, af);
674 		m_freem(m);
675 		return (EAFNOSUPPORT);
676 	}
677 
678 	if (niq_enqueue(ifq, m) != 0)
679 		return (ENOBUFS);
680 
681 	ifp->if_ipackets++;
682 	ifp->if_ibytes += m->m_pkthdr.len;
683 
684 	return (0);
685 }
686 
687 struct ifih {
688 	SRPL_ENTRY(ifih)	  ifih_next;
689 	int			(*ifih_input)(struct ifnet *, struct mbuf *,
690 				      void *);
691 	void			 *ifih_cookie;
692 	int			  ifih_refcnt;
693 	struct refcnt		  ifih_srpcnt;
694 };
695 
696 void	if_ih_ref(void *, void *);
697 void	if_ih_unref(void *, void *);
698 
699 struct srpl_rc ifih_rc = SRPL_RC_INITIALIZER(if_ih_ref, if_ih_unref, NULL);
700 
701 void
702 if_ih_insert(struct ifnet *ifp, int (*input)(struct ifnet *, struct mbuf *,
703     void *), void *cookie)
704 {
705 	struct ifih *ifih;
706 
707 	/* the kernel lock guarantees serialised modifications to if_inputs */
708 	KERNEL_ASSERT_LOCKED();
709 
710 	SRPL_FOREACH_LOCKED(ifih, &ifp->if_inputs, ifih_next) {
711 		if (ifih->ifih_input == input && ifih->ifih_cookie == cookie) {
712 			ifih->ifih_refcnt++;
713 			break;
714 		}
715 	}
716 
717 	if (ifih == NULL) {
718 		ifih = malloc(sizeof(*ifih), M_DEVBUF, M_WAITOK);
719 
720 		ifih->ifih_input = input;
721 		ifih->ifih_cookie = cookie;
722 		ifih->ifih_refcnt = 1;
723 		refcnt_init(&ifih->ifih_srpcnt);
724 		SRPL_INSERT_HEAD_LOCKED(&ifih_rc, &ifp->if_inputs,
725 		    ifih, ifih_next);
726 	}
727 }
728 
729 void
730 if_ih_ref(void *null, void *i)
731 {
732 	struct ifih *ifih = i;
733 
734 	refcnt_take(&ifih->ifih_srpcnt);
735 }
736 
737 void
738 if_ih_unref(void *null, void *i)
739 {
740 	struct ifih *ifih = i;
741 
742 	refcnt_rele_wake(&ifih->ifih_srpcnt);
743 }
744 
745 void
746 if_ih_remove(struct ifnet *ifp, int (*input)(struct ifnet *, struct mbuf *,
747     void *), void *cookie)
748 {
749 	struct ifih *ifih;
750 
751 	/* the kernel lock guarantees serialised modifications to if_inputs */
752 	KERNEL_ASSERT_LOCKED();
753 
754 	SRPL_FOREACH_LOCKED(ifih, &ifp->if_inputs, ifih_next) {
755 		if (ifih->ifih_input == input && ifih->ifih_cookie == cookie)
756 			break;
757 	}
758 
759 	KASSERT(ifih != NULL);
760 
761 	if (--ifih->ifih_refcnt == 0) {
762 		SRPL_REMOVE_LOCKED(&ifih_rc, &ifp->if_inputs, ifih,
763 		    ifih, ifih_next);
764 
765 		refcnt_finalize(&ifih->ifih_srpcnt, "ifihrm");
766 		free(ifih, M_DEVBUF, sizeof(*ifih));
767 	}
768 }
769 
770 void
771 if_input_process(void *xmq)
772 {
773 	struct mbuf_queue *mq = xmq;
774 	struct mbuf_list ml;
775 	struct mbuf *m;
776 	struct ifnet *ifp;
777 	struct ifih *ifih;
778 	struct srp_ref sr;
779 	int s;
780 
781 	mq_delist(mq, &ml);
782 	if (ml_empty(&ml))
783 		return;
784 
785 	add_net_randomness(ml_len(&ml));
786 
787 	s = splnet();
788 	while ((m = ml_dequeue(&ml)) != NULL) {
789 		ifp = if_get(m->m_pkthdr.ph_ifidx);
790 		if (ifp == NULL) {
791 			m_freem(m);
792 			continue;
793 		}
794 
795 		/*
796 		 * Pass this mbuf to all input handlers of its
797 		 * interface until it is consumed.
798 		 */
799 		SRPL_FOREACH(ifih, &sr, &ifp->if_inputs, ifih_next) {
800 			if ((*ifih->ifih_input)(ifp, m, ifih->ifih_cookie))
801 				break;
802 		}
803 		SRPL_LEAVE(&sr);
804 
805 		if (ifih == NULL)
806 			m_freem(m);
807 
808 		if_put(ifp);
809 	}
810 	splx(s);
811 }
812 
813 void
814 if_netisr(void *unused)
815 {
816 	int n, t = 0;
817 	int s;
818 
819 	KERNEL_LOCK();
820 	s = splsoftnet();
821 
822 	while ((n = netisr) != 0) {
823 		sched_pause();
824 
825 		atomic_clearbits_int(&netisr, n);
826 
827 		if (n & (1 << NETISR_IP))
828 			ipintr();
829 #ifdef INET6
830 		if (n & (1 << NETISR_IPV6))
831 			ip6intr();
832 #endif
833 #if NPPP > 0
834 		if (n & (1 << NETISR_PPP))
835 			pppintr();
836 #endif
837 #if NBRIDGE > 0
838 		if (n & (1 << NETISR_BRIDGE))
839 			bridgeintr();
840 #endif
841 #if NPPPOE > 0
842 		if (n & (1 << NETISR_PPPOE))
843 			pppoeintr();
844 #endif
845 		t |= n;
846 	}
847 
848 #if NPFSYNC > 0
849 	if (t & (1 << NETISR_PFSYNC))
850 		pfsyncintr();
851 #endif
852 
853 	splx(s);
854 	KERNEL_UNLOCK();
855 }
856 
857 void
858 if_deactivate(struct ifnet *ifp)
859 {
860 	int s;
861 
862 	s = splnet();
863 
864 	/*
865 	 * Call detach hooks from head to tail.  To make sure detach
866 	 * hooks are executed in the reverse order they were added, all
867 	 * the hooks have to be added to the head!
868 	 */
869 	dohooks(ifp->if_detachhooks, HOOK_REMOVE | HOOK_FREE);
870 
871 #if NBRIDGE > 0
872 	/* Remove the interface from any bridge it is part of.  */
873 	if (ifp->if_bridgeport)
874 		bridge_ifdetach(ifp);
875 #endif
876 
877 #if NCARP > 0
878 	/* Remove the interface from any carp group it is a part of.  */
879 	if (ifp->if_carp && ifp->if_type != IFT_CARP)
880 		carp_ifdetach(ifp);
881 #endif
882 
883 	splx(s);
884 }
885 
886 /*
887  * Detach an interface from everything in the kernel.  Also deallocate
888  * private resources.
889  */
890 void
891 if_detach(struct ifnet *ifp)
892 {
893 	struct ifaddr *ifa;
894 	struct ifg_list *ifg;
895 	struct domain *dp;
896 	int i, s;
897 
898 	/* Undo pseudo-driver changes. */
899 	if_deactivate(ifp);
900 
901 	ifq_clr_oactive(&ifp->if_snd);
902 
903 	s = splnet();
904 	/* Other CPUs must not have a reference before we start destroying. */
905 	if_idxmap_remove(ifp);
906 
907 	ifp->if_start = if_detached_start;
908 	ifp->if_ioctl = if_detached_ioctl;
909 	ifp->if_watchdog = NULL;
910 
911 	/* Remove the watchdog timeout & task */
912 	timeout_del(ifp->if_slowtimo);
913 	task_del(systq, ifp->if_watchdogtask);
914 
915 	/* Remove the link state task */
916 	task_del(systq, ifp->if_linkstatetask);
917 
918 #if NBPFILTER > 0
919 	bpfdetach(ifp);
920 #endif
921 	rt_if_remove(ifp);
922 	rti_delete(ifp);
923 #if NETHER > 0 && defined(NFSCLIENT)
924 	if (ifp->if_index == revarp_ifidx)
925 		revarp_ifidx = 0;
926 #endif
927 #ifdef MROUTING
928 	vif_delete(ifp);
929 #endif
930 	in_ifdetach(ifp);
931 #ifdef INET6
932 	in6_ifdetach(ifp);
933 #endif
934 #if NPF > 0
935 	pfi_detach_ifnet(ifp);
936 #endif
937 
938 	/* Remove the interface from the list of all interfaces.  */
939 	TAILQ_REMOVE(&ifnet, ifp, if_list);
940 
941 	while ((ifg = TAILQ_FIRST(&ifp->if_groups)) != NULL)
942 		if_delgroup(ifp, ifg->ifgl_group->ifg_group);
943 
944 	if_free_sadl(ifp);
945 
946 	/* We should not have any address left at this point. */
947 	if (!TAILQ_EMPTY(&ifp->if_addrlist)) {
948 #ifdef DIAGNOSTIC
949 		printf("%s: address list non empty\n", ifp->if_xname);
950 #endif
951 		while ((ifa = TAILQ_FIRST(&ifp->if_addrlist)) != NULL) {
952 			ifa_del(ifp, ifa);
953 			ifa->ifa_ifp = NULL;
954 			ifafree(ifa);
955 		}
956 	}
957 
958 	free(ifp->if_addrhooks, M_TEMP, 0);
959 	free(ifp->if_linkstatehooks, M_TEMP, 0);
960 	free(ifp->if_detachhooks, M_TEMP, 0);
961 
962 	free(ifp->if_slowtimo, M_TEMP, sizeof(*ifp->if_slowtimo));
963 	free(ifp->if_watchdogtask, M_TEMP, sizeof(*ifp->if_watchdogtask));
964 	free(ifp->if_linkstatetask, M_TEMP, sizeof(*ifp->if_linkstatetask));
965 
966 	for (i = 0; (dp = domains[i]) != NULL; i++) {
967 		if (dp->dom_ifdetach && ifp->if_afdata[dp->dom_family])
968 			(*dp->dom_ifdetach)(ifp,
969 			    ifp->if_afdata[dp->dom_family]);
970 	}
971 
972 	/* Announce that the interface is gone. */
973 	rt_ifannouncemsg(ifp, IFAN_DEPARTURE);
974 	splx(s);
975 
976 	ifq_destroy(&ifp->if_snd);
977 }
978 
979 /*
980  * Returns true if ``ifp0'' is connected to the interface with index ``ifidx''.
981  */
982 int
983 if_isconnected(const struct ifnet *ifp0, unsigned int ifidx)
984 {
985 	struct ifnet *ifp;
986 	int connected = 0;
987 
988 	ifp = if_get(ifidx);
989 	if (ifp == NULL)
990 		return (0);
991 
992 	if (ifp0->if_index == ifp->if_index)
993 		connected = 1;
994 
995 #if NBRIDGE > 0
996 	if (SAME_BRIDGE(ifp0->if_bridgeport, ifp->if_bridgeport))
997 		connected = 1;
998 #endif
999 #if NCARP > 0
1000 	if ((ifp0->if_type == IFT_CARP && ifp0->if_carpdev == ifp) ||
1001 	    (ifp->if_type == IFT_CARP && ifp->if_carpdev == ifp0))
1002 	    	connected = 1;
1003 #endif
1004 
1005 	if_put(ifp);
1006 	return (connected);
1007 }
1008 
1009 /*
1010  * Create a clone network interface.
1011  */
1012 int
1013 if_clone_create(const char *name)
1014 {
1015 	struct if_clone *ifc;
1016 	struct ifnet *ifp;
1017 	int unit, ret;
1018 
1019 	ifc = if_clone_lookup(name, &unit);
1020 	if (ifc == NULL)
1021 		return (EINVAL);
1022 
1023 	if (ifunit(name) != NULL)
1024 		return (EEXIST);
1025 
1026 	if ((ret = (*ifc->ifc_create)(ifc, unit)) == 0 &&
1027 	    (ifp = ifunit(name)) != NULL)
1028 		if_addgroup(ifp, ifc->ifc_name);
1029 
1030 	return (ret);
1031 }
1032 
1033 /*
1034  * Destroy a clone network interface.
1035  */
1036 int
1037 if_clone_destroy(const char *name)
1038 {
1039 	struct if_clone *ifc;
1040 	struct ifnet *ifp;
1041 	int s;
1042 
1043 	ifc = if_clone_lookup(name, NULL);
1044 	if (ifc == NULL)
1045 		return (EINVAL);
1046 
1047 	ifp = ifunit(name);
1048 	if (ifp == NULL)
1049 		return (ENXIO);
1050 
1051 	if (ifc->ifc_destroy == NULL)
1052 		return (EOPNOTSUPP);
1053 
1054 	if (ifp->if_flags & IFF_UP) {
1055 		s = splnet();
1056 		if_down(ifp);
1057 		splx(s);
1058 	}
1059 
1060 	return ((*ifc->ifc_destroy)(ifp));
1061 }
1062 
1063 /*
1064  * Look up a network interface cloner.
1065  */
1066 struct if_clone *
1067 if_clone_lookup(const char *name, int *unitp)
1068 {
1069 	struct if_clone *ifc;
1070 	const char *cp;
1071 	int unit;
1072 
1073 	/* separate interface name from unit */
1074 	for (cp = name;
1075 	    cp - name < IFNAMSIZ && *cp && (*cp < '0' || *cp > '9');
1076 	    cp++)
1077 		continue;
1078 
1079 	if (cp == name || cp - name == IFNAMSIZ || !*cp)
1080 		return (NULL);	/* No name or unit number */
1081 
1082 	if (cp - name < IFNAMSIZ-1 && *cp == '0' && cp[1] != '\0')
1083 		return (NULL);	/* unit number 0 padded */
1084 
1085 	LIST_FOREACH(ifc, &if_cloners, ifc_list) {
1086 		if (strlen(ifc->ifc_name) == cp - name &&
1087 		    !strncmp(name, ifc->ifc_name, cp - name))
1088 			break;
1089 	}
1090 
1091 	if (ifc == NULL)
1092 		return (NULL);
1093 
1094 	unit = 0;
1095 	while (cp - name < IFNAMSIZ && *cp) {
1096 		if (*cp < '0' || *cp > '9' ||
1097 		    unit > (INT_MAX - (*cp - '0')) / 10) {
1098 			/* Bogus unit number. */
1099 			return (NULL);
1100 		}
1101 		unit = (unit * 10) + (*cp++ - '0');
1102 	}
1103 
1104 	if (unitp != NULL)
1105 		*unitp = unit;
1106 	return (ifc);
1107 }
1108 
1109 /*
1110  * Register a network interface cloner.
1111  */
1112 void
1113 if_clone_attach(struct if_clone *ifc)
1114 {
1115 	LIST_INSERT_HEAD(&if_cloners, ifc, ifc_list);
1116 	if_cloners_count++;
1117 }
1118 
1119 /*
1120  * Unregister a network interface cloner.
1121  */
1122 void
1123 if_clone_detach(struct if_clone *ifc)
1124 {
1125 
1126 	LIST_REMOVE(ifc, ifc_list);
1127 	if_cloners_count--;
1128 }
1129 
1130 /*
1131  * Provide list of interface cloners to userspace.
1132  */
1133 int
1134 if_clone_list(struct if_clonereq *ifcr)
1135 {
1136 	char outbuf[IFNAMSIZ], *dst;
1137 	struct if_clone *ifc;
1138 	int count, error = 0;
1139 
1140 	ifcr->ifcr_total = if_cloners_count;
1141 	if ((dst = ifcr->ifcr_buffer) == NULL) {
1142 		/* Just asking how many there are. */
1143 		return (0);
1144 	}
1145 
1146 	if (ifcr->ifcr_count < 0)
1147 		return (EINVAL);
1148 
1149 	count = (if_cloners_count < ifcr->ifcr_count) ?
1150 	    if_cloners_count : ifcr->ifcr_count;
1151 
1152 	LIST_FOREACH(ifc, &if_cloners, ifc_list) {
1153 		if (count == 0)
1154 			break;
1155 		bzero(outbuf, sizeof outbuf);
1156 		strlcpy(outbuf, ifc->ifc_name, IFNAMSIZ);
1157 		error = copyout(outbuf, dst, IFNAMSIZ);
1158 		if (error)
1159 			break;
1160 		count--;
1161 		dst += IFNAMSIZ;
1162 	}
1163 
1164 	return (error);
1165 }
1166 
1167 /*
1168  * set queue congestion marker
1169  */
1170 void
1171 if_congestion(void)
1172 {
1173 	extern int ticks;
1174 
1175 	ifq_congestion = ticks;
1176 }
1177 
1178 int
1179 if_congested(void)
1180 {
1181 	extern int ticks;
1182 	int diff;
1183 
1184 	diff = ticks - ifq_congestion;
1185 	if (diff < 0) {
1186 		ifq_congestion = ticks - hz;
1187 		return (0);
1188 	}
1189 
1190 	return (diff <= (hz / 100));
1191 }
1192 
1193 #define	equal(a1, a2)	\
1194 	(bcmp((caddr_t)(a1), (caddr_t)(a2),	\
1195 	((struct sockaddr *)(a1))->sa_len) == 0)
1196 
1197 /*
1198  * Locate an interface based on a complete address.
1199  */
1200 struct ifaddr *
1201 ifa_ifwithaddr(struct sockaddr *addr, u_int rtableid)
1202 {
1203 	struct ifnet *ifp;
1204 	struct ifaddr *ifa;
1205 	u_int rdomain;
1206 
1207 	KERNEL_ASSERT_LOCKED();
1208 	rdomain = rtable_l2(rtableid);
1209 	TAILQ_FOREACH(ifp, &ifnet, if_list) {
1210 		if (ifp->if_rdomain != rdomain)
1211 			continue;
1212 
1213 		TAILQ_FOREACH(ifa, &ifp->if_addrlist, ifa_list) {
1214 			if (ifa->ifa_addr->sa_family != addr->sa_family)
1215 				continue;
1216 
1217 			if (equal(addr, ifa->ifa_addr))
1218 				return (ifa);
1219 		}
1220 	}
1221 	return (NULL);
1222 }
1223 
1224 /*
1225  * Locate the point to point interface with a given destination address.
1226  */
1227 struct ifaddr *
1228 ifa_ifwithdstaddr(struct sockaddr *addr, u_int rdomain)
1229 {
1230 	struct ifnet *ifp;
1231 	struct ifaddr *ifa;
1232 
1233 	KERNEL_ASSERT_LOCKED();
1234 	rdomain = rtable_l2(rdomain);
1235 	TAILQ_FOREACH(ifp, &ifnet, if_list) {
1236 		if (ifp->if_rdomain != rdomain)
1237 			continue;
1238 		if (ifp->if_flags & IFF_POINTOPOINT)
1239 			TAILQ_FOREACH(ifa, &ifp->if_addrlist, ifa_list) {
1240 				if (ifa->ifa_addr->sa_family !=
1241 				    addr->sa_family || ifa->ifa_dstaddr == NULL)
1242 					continue;
1243 				if (equal(addr, ifa->ifa_dstaddr))
1244 					return (ifa);
1245 			}
1246 	}
1247 	return (NULL);
1248 }
1249 
1250 /*
1251  * Find an interface on a specific network.  If many, choice
1252  * is most specific found.
1253  */
1254 struct ifaddr *
1255 ifa_ifwithnet(struct sockaddr *sa, u_int rtableid)
1256 {
1257 	struct ifnet *ifp;
1258 	struct ifaddr *ifa, *ifa_maybe = NULL;
1259 	char *cplim, *addr_data = sa->sa_data;
1260 	u_int rdomain;
1261 
1262 	KERNEL_ASSERT_LOCKED();
1263 	rdomain = rtable_l2(rtableid);
1264 	TAILQ_FOREACH(ifp, &ifnet, if_list) {
1265 		if (ifp->if_rdomain != rdomain)
1266 			continue;
1267 		TAILQ_FOREACH(ifa, &ifp->if_addrlist, ifa_list) {
1268 			char *cp, *cp2, *cp3;
1269 
1270 			if (ifa->ifa_addr->sa_family != sa->sa_family ||
1271 			    ifa->ifa_netmask == 0)
1272 				next: continue;
1273 			cp = addr_data;
1274 			cp2 = ifa->ifa_addr->sa_data;
1275 			cp3 = ifa->ifa_netmask->sa_data;
1276 			cplim = (char *)ifa->ifa_netmask +
1277 				ifa->ifa_netmask->sa_len;
1278 			while (cp3 < cplim)
1279 				if ((*cp++ ^ *cp2++) & *cp3++)
1280 				    /* want to continue for() loop */
1281 					goto next;
1282 			if (ifa_maybe == 0 ||
1283 			    rn_refines((caddr_t)ifa->ifa_netmask,
1284 			    (caddr_t)ifa_maybe->ifa_netmask))
1285 				ifa_maybe = ifa;
1286 		}
1287 	}
1288 	return (ifa_maybe);
1289 }
1290 
1291 /*
1292  * Find an interface address specific to an interface best matching
1293  * a given address.
1294  */
1295 struct ifaddr *
1296 ifaof_ifpforaddr(struct sockaddr *addr, struct ifnet *ifp)
1297 {
1298 	struct ifaddr *ifa;
1299 	char *cp, *cp2, *cp3;
1300 	char *cplim;
1301 	struct ifaddr *ifa_maybe = NULL;
1302 	u_int af = addr->sa_family;
1303 
1304 	if (af >= AF_MAX)
1305 		return (NULL);
1306 	TAILQ_FOREACH(ifa, &ifp->if_addrlist, ifa_list) {
1307 		if (ifa->ifa_addr->sa_family != af)
1308 			continue;
1309 		if (ifa_maybe == NULL)
1310 			ifa_maybe = ifa;
1311 		if (ifa->ifa_netmask == 0 || ifp->if_flags & IFF_POINTOPOINT) {
1312 			if (equal(addr, ifa->ifa_addr) ||
1313 			    (ifa->ifa_dstaddr && equal(addr, ifa->ifa_dstaddr)))
1314 				return (ifa);
1315 			continue;
1316 		}
1317 		cp = addr->sa_data;
1318 		cp2 = ifa->ifa_addr->sa_data;
1319 		cp3 = ifa->ifa_netmask->sa_data;
1320 		cplim = ifa->ifa_netmask->sa_len + (char *)ifa->ifa_netmask;
1321 		for (; cp3 < cplim; cp3++)
1322 			if ((*cp++ ^ *cp2++) & *cp3)
1323 				break;
1324 		if (cp3 == cplim)
1325 			return (ifa);
1326 	}
1327 	return (ifa_maybe);
1328 }
1329 
1330 void
1331 if_rtrequest_dummy(struct ifnet *ifp, int req, struct rtentry *rt)
1332 {
1333 }
1334 
1335 /*
1336  * Default action when installing a local route on a point-to-point
1337  * interface.
1338  */
1339 void
1340 p2p_rtrequest(struct ifnet *ifp, int req, struct rtentry *rt)
1341 {
1342 	struct ifnet *lo0ifp;
1343 	struct ifaddr *ifa, *lo0ifa;
1344 
1345 	switch (req) {
1346 	case RTM_ADD:
1347 		if (!ISSET(rt->rt_flags, RTF_LOCAL))
1348 			break;
1349 
1350 		TAILQ_FOREACH(ifa, &ifp->if_addrlist, ifa_list) {
1351 			if (memcmp(rt_key(rt), ifa->ifa_addr,
1352 			    rt_key(rt)->sa_len) == 0)
1353 				break;
1354 		}
1355 
1356 		if (ifa == NULL)
1357 			break;
1358 
1359 		KASSERT(ifa == rt->rt_ifa);
1360 
1361 		/*
1362 		 * XXX Since lo0 is in the default rdomain we should not
1363 		 * (ab)use it for any route related to an interface of a
1364 		 * different rdomain.
1365 		 */
1366 		lo0ifp = if_get(lo0ifidx);
1367 		KASSERT(lo0ifp != NULL);
1368 		TAILQ_FOREACH(lo0ifa, &lo0ifp->if_addrlist, ifa_list) {
1369 			if (lo0ifa->ifa_addr->sa_family ==
1370 			    ifa->ifa_addr->sa_family)
1371 				break;
1372 		}
1373 		if_put(lo0ifp);
1374 
1375 		if (lo0ifa == NULL)
1376 			break;
1377 
1378 		rt->rt_flags &= ~RTF_LLINFO;
1379 		break;
1380 	case RTM_DELETE:
1381 	case RTM_RESOLVE:
1382 	default:
1383 		break;
1384 	}
1385 }
1386 
1387 
1388 /*
1389  * Bring down all interfaces
1390  */
1391 void
1392 if_downall(void)
1393 {
1394 	struct ifreq ifrq;	/* XXX only partly built */
1395 	struct ifnet *ifp;
1396 	int s;
1397 
1398 	s = splnet();
1399 	TAILQ_FOREACH(ifp, &ifnet, if_list) {
1400 		if ((ifp->if_flags & IFF_UP) == 0)
1401 			continue;
1402 		if_down(ifp);
1403 		ifp->if_flags &= ~IFF_UP;
1404 
1405 		if (ifp->if_ioctl) {
1406 			ifrq.ifr_flags = ifp->if_flags;
1407 			(void) (*ifp->if_ioctl)(ifp, SIOCSIFFLAGS,
1408 			    (caddr_t)&ifrq);
1409 		}
1410 	}
1411 	splx(s);
1412 }
1413 
1414 /*
1415  * Mark an interface down and notify protocols of
1416  * the transition.
1417  */
1418 void
1419 if_down(struct ifnet *ifp)
1420 {
1421 	struct ifaddr *ifa;
1422 
1423 	splsoftassert(IPL_SOFTNET);
1424 
1425 	ifp->if_flags &= ~IFF_UP;
1426 	microtime(&ifp->if_lastchange);
1427 	TAILQ_FOREACH(ifa, &ifp->if_addrlist, ifa_list) {
1428 		pfctlinput(PRC_IFDOWN, ifa->ifa_addr);
1429 	}
1430 	IFQ_PURGE(&ifp->if_snd);
1431 
1432 	if_linkstate(ifp);
1433 }
1434 
1435 /*
1436  * Mark an interface up and notify protocols of
1437  * the transition.
1438  */
1439 void
1440 if_up(struct ifnet *ifp)
1441 {
1442 	splsoftassert(IPL_SOFTNET);
1443 
1444 	ifp->if_flags |= IFF_UP;
1445 	microtime(&ifp->if_lastchange);
1446 
1447 #ifdef INET6
1448 	/* Userland expects the kernel to set ::1 on lo0. */
1449 	if (ifp->if_index == lo0ifidx)
1450 		in6_ifattach(ifp);
1451 #endif
1452 
1453 	if_linkstate(ifp);
1454 }
1455 
1456 /*
1457  * Notify userland, the routing table and hooks owner of
1458  * a link-state transition.
1459  */
1460 void
1461 if_linkstate(void *xifp)
1462 {
1463 	struct ifnet *ifp = xifp;
1464 	int s;
1465 
1466 	s = splsoftnet();
1467 	rt_ifmsg(ifp);
1468 #ifndef SMALL_KERNEL
1469 	rt_if_track(ifp);
1470 #endif
1471 	dohooks(ifp->if_linkstatehooks, 0);
1472 	splx(s);
1473 }
1474 
1475 /*
1476  * Schedule a link state change task.
1477  */
1478 void
1479 if_link_state_change(struct ifnet *ifp)
1480 {
1481 	task_add(systq, ifp->if_linkstatetask);
1482 }
1483 
1484 /*
1485  * Handle interface watchdog timer routine.  Called
1486  * from softclock, we decrement timer (if set) and
1487  * call the appropriate interface routine on expiration.
1488  */
1489 void
1490 if_slowtimo(void *arg)
1491 {
1492 	struct ifnet *ifp = arg;
1493 	int s = splnet();
1494 
1495 	if (ifp->if_watchdog) {
1496 		if (ifp->if_timer > 0 && --ifp->if_timer == 0)
1497 			task_add(systq, ifp->if_watchdogtask);
1498 		timeout_add(ifp->if_slowtimo, hz / IFNET_SLOWHZ);
1499 	}
1500 	splx(s);
1501 }
1502 
1503 void
1504 if_watchdog_task(void *arg)
1505 {
1506 	struct ifnet *ifp = arg;
1507 	int s;
1508 
1509 	s = splnet();
1510 	if (ifp->if_watchdog)
1511 		(*ifp->if_watchdog)(ifp);
1512 	splx(s);
1513 }
1514 
1515 /*
1516  * Map interface name to interface structure pointer.
1517  */
1518 struct ifnet *
1519 ifunit(const char *name)
1520 {
1521 	struct ifnet *ifp;
1522 
1523 	TAILQ_FOREACH(ifp, &ifnet, if_list) {
1524 		if (strcmp(ifp->if_xname, name) == 0)
1525 			return (ifp);
1526 	}
1527 	return (NULL);
1528 }
1529 
1530 /*
1531  * Map interface index to interface structure pointer.
1532  */
1533 struct ifnet *
1534 if_get(unsigned int index)
1535 {
1536 	struct srp_ref sr;
1537 	struct if_map *if_map;
1538 	struct srp *map;
1539 	struct ifnet *ifp = NULL;
1540 
1541 	if_map = srp_enter(&sr, &if_idxmap.map);
1542 	if (index < if_map->limit) {
1543 		map = (struct srp *)(if_map + 1);
1544 
1545 		ifp = srp_follow(&sr, &map[index]);
1546 		if (ifp != NULL) {
1547 			KASSERT(ifp->if_index == index);
1548 			if_ref(ifp);
1549 		}
1550 	}
1551 	srp_leave(&sr);
1552 
1553 	return (ifp);
1554 }
1555 
1556 struct ifnet *
1557 if_ref(struct ifnet *ifp)
1558 {
1559 	refcnt_take(&ifp->if_refcnt);
1560 
1561 	return (ifp);
1562 }
1563 
1564 void
1565 if_put(struct ifnet *ifp)
1566 {
1567 	if (ifp == NULL)
1568 		return;
1569 
1570 	refcnt_rele_wake(&ifp->if_refcnt);
1571 }
1572 
1573 int
1574 if_setlladdr(struct ifnet *ifp, const uint8_t *lladdr)
1575 {
1576 	if (ifp->if_sadl == NULL)
1577 		return (EINVAL);
1578 
1579 	memcpy(((struct arpcom *)ifp)->ac_enaddr, lladdr, ETHER_ADDR_LEN);
1580 	memcpy(LLADDR(ifp->if_sadl), lladdr, ETHER_ADDR_LEN);
1581 
1582 	return (0);
1583 }
1584 
1585 /*
1586  * Interface ioctls.
1587  */
1588 int
1589 ifioctl(struct socket *so, u_long cmd, caddr_t data, struct proc *p)
1590 {
1591 	struct ifnet *ifp;
1592 	struct ifreq *ifr;
1593 	struct sockaddr_dl *sdl;
1594 	struct ifgroupreq *ifgr;
1595 	struct if_afreq *ifar;
1596 	char ifdescrbuf[IFDESCRSIZE];
1597 	char ifrtlabelbuf[RTLABEL_LEN];
1598 	int s, error = 0;
1599 	size_t bytesdone;
1600 	short oif_flags;
1601 	const char *label;
1602 	short up = 0;
1603 
1604 	switch (cmd) {
1605 
1606 	case SIOCGIFCONF:
1607 		return (ifconf(cmd, data));
1608 	}
1609 	ifr = (struct ifreq *)data;
1610 
1611 	switch (cmd) {
1612 	case SIOCIFCREATE:
1613 	case SIOCIFDESTROY:
1614 		if ((error = suser(p, 0)) != 0)
1615 			return (error);
1616 		return ((cmd == SIOCIFCREATE) ?
1617 		    if_clone_create(ifr->ifr_name) :
1618 		    if_clone_destroy(ifr->ifr_name));
1619 	case SIOCIFGCLONERS:
1620 		return (if_clone_list((struct if_clonereq *)data));
1621 	case SIOCGIFGMEMB:
1622 		return (if_getgroupmembers(data));
1623 	case SIOCGIFGATTR:
1624 		return (if_getgroupattribs(data));
1625 	case SIOCSIFGATTR:
1626 		if ((error = suser(p, 0)) != 0)
1627 			return (error);
1628 		return (if_setgroupattribs(data));
1629 	case SIOCIFAFATTACH:
1630 	case SIOCIFAFDETACH:
1631 		if ((error = suser(p, 0)) != 0)
1632 			return (error);
1633 		ifar = (struct if_afreq *)data;
1634 		if ((ifp = ifunit(ifar->ifar_name)) == NULL)
1635 			return (ENXIO);
1636 		switch (ifar->ifar_af) {
1637 		case AF_INET:
1638 			/* attach is a noop for AF_INET */
1639 			if (cmd == SIOCIFAFDETACH) {
1640 				s = splsoftnet();
1641 				in_ifdetach(ifp);
1642 				splx(s);
1643 			}
1644 			return (0);
1645 #ifdef INET6
1646 		case AF_INET6:
1647 			s = splsoftnet();
1648 			if (cmd == SIOCIFAFATTACH)
1649 				error = in6_ifattach(ifp);
1650 			else
1651 				in6_ifdetach(ifp);
1652 			splx(s);
1653 			return (error);
1654 #endif /* INET6 */
1655 		default:
1656 			return (EAFNOSUPPORT);
1657 		}
1658 	}
1659 
1660 	ifp = ifunit(ifr->ifr_name);
1661 	if (ifp == 0)
1662 		return (ENXIO);
1663 	oif_flags = ifp->if_flags;
1664 	switch (cmd) {
1665 
1666 	case SIOCGIFFLAGS:
1667 		ifr->ifr_flags = ifp->if_flags;
1668 		if (ifq_is_oactive(&ifp->if_snd))
1669 			ifr->ifr_flags |= IFF_OACTIVE;
1670 		break;
1671 
1672 	case SIOCGIFXFLAGS:
1673 		ifr->ifr_flags = ifp->if_xflags & ~IFXF_MPSAFE;
1674 		break;
1675 
1676 	case SIOCGIFMETRIC:
1677 		ifr->ifr_metric = ifp->if_metric;
1678 		break;
1679 
1680 	case SIOCGIFMTU:
1681 		ifr->ifr_mtu = ifp->if_mtu;
1682 		break;
1683 
1684 	case SIOCGIFHARDMTU:
1685 		ifr->ifr_hardmtu = ifp->if_hardmtu;
1686 		break;
1687 
1688 	case SIOCGIFDATA:
1689 		error = copyout((caddr_t)&ifp->if_data, ifr->ifr_data,
1690 		    sizeof(ifp->if_data));
1691 		break;
1692 
1693 	case SIOCSIFFLAGS:
1694 		if ((error = suser(p, 0)) != 0)
1695 			return (error);
1696 		if (ifp->if_flags & IFF_UP && (ifr->ifr_flags & IFF_UP) == 0) {
1697 			s = splnet();
1698 			if_down(ifp);
1699 			splx(s);
1700 		}
1701 		if (ifr->ifr_flags & IFF_UP && (ifp->if_flags & IFF_UP) == 0) {
1702 			s = splnet();
1703 			if_up(ifp);
1704 			splx(s);
1705 		}
1706 		ifp->if_flags = (ifp->if_flags & IFF_CANTCHANGE) |
1707 			(ifr->ifr_flags & ~IFF_CANTCHANGE);
1708 		if (ifp->if_ioctl)
1709 			(void) (*ifp->if_ioctl)(ifp, cmd, data);
1710 		break;
1711 
1712 	case SIOCSIFXFLAGS:
1713 		if ((error = suser(p, 0)) != 0)
1714 			return (error);
1715 
1716 #ifdef INET6
1717 		if (ISSET(ifr->ifr_flags, IFXF_AUTOCONF6)) {
1718 			s = splsoftnet();
1719 			error = in6_ifattach(ifp);
1720 			splx(s);
1721 			if (error != 0)
1722 				return (error);
1723 		}
1724 
1725 		if ((ifr->ifr_flags & IFXF_AUTOCONF6) &&
1726 		    !(ifp->if_xflags & IFXF_AUTOCONF6)) {
1727 			nd6_rs_attach(ifp);
1728 		}
1729 
1730 		if ((ifp->if_xflags & IFXF_AUTOCONF6) &&
1731 		    !(ifr->ifr_flags & IFXF_AUTOCONF6)) {
1732 			nd6_rs_detach(ifp);
1733 		}
1734 #endif	/* INET6 */
1735 
1736 #ifdef MPLS
1737 		if (ISSET(ifr->ifr_flags, IFXF_MPLS) &&
1738 		    !ISSET(ifp->if_xflags, IFXF_MPLS)) {
1739 			s = splnet();
1740 			ifp->if_xflags |= IFXF_MPLS;
1741 			ifp->if_ll_output = ifp->if_output;
1742 			ifp->if_output = mpls_output;
1743 			splx(s);
1744 		}
1745 		if (ISSET(ifp->if_xflags, IFXF_MPLS) &&
1746 		    !ISSET(ifr->ifr_flags, IFXF_MPLS)) {
1747 			s = splnet();
1748 			ifp->if_xflags &= ~IFXF_MPLS;
1749 			ifp->if_output = ifp->if_ll_output;
1750 			ifp->if_ll_output = NULL;
1751 			splx(s);
1752 		}
1753 #endif	/* MPLS */
1754 
1755 #ifndef SMALL_KERNEL
1756 		if (ifp->if_capabilities & IFCAP_WOL) {
1757 			if (ISSET(ifr->ifr_flags, IFXF_WOL) &&
1758 			    !ISSET(ifp->if_xflags, IFXF_WOL)) {
1759 				s = splnet();
1760 				ifp->if_xflags |= IFXF_WOL;
1761 				error = ifp->if_wol(ifp, 1);
1762 				splx(s);
1763 				if (error)
1764 					return (error);
1765 			}
1766 			if (ISSET(ifp->if_xflags, IFXF_WOL) &&
1767 			    !ISSET(ifr->ifr_flags, IFXF_WOL)) {
1768 				s = splnet();
1769 				ifp->if_xflags &= ~IFXF_WOL;
1770 				error = ifp->if_wol(ifp, 0);
1771 				splx(s);
1772 				if (error)
1773 					return (error);
1774 			}
1775 		} else if (ISSET(ifr->ifr_flags, IFXF_WOL)) {
1776 			ifr->ifr_flags &= ~IFXF_WOL;
1777 			error = ENOTSUP;
1778 		}
1779 #endif
1780 
1781 		ifp->if_xflags = (ifp->if_xflags & IFXF_CANTCHANGE) |
1782 			(ifr->ifr_flags & ~IFXF_CANTCHANGE);
1783 		rt_ifmsg(ifp);
1784 		break;
1785 
1786 	case SIOCSIFMETRIC:
1787 		if ((error = suser(p, 0)) != 0)
1788 			return (error);
1789 		ifp->if_metric = ifr->ifr_metric;
1790 		break;
1791 
1792 	case SIOCSIFMTU:
1793 		if ((error = suser(p, 0)) != 0)
1794 			return (error);
1795 		if (ifp->if_ioctl == NULL)
1796 			return (EOPNOTSUPP);
1797 		error = (*ifp->if_ioctl)(ifp, cmd, data);
1798 		break;
1799 
1800 	case SIOCSIFPHYADDR:
1801 	case SIOCDIFPHYADDR:
1802 #ifdef INET6
1803 	case SIOCSIFPHYADDR_IN6:
1804 #endif
1805 	case SIOCSLIFPHYADDR:
1806 	case SIOCSLIFPHYRTABLE:
1807 	case SIOCSLIFPHYTTL:
1808 	case SIOCADDMULTI:
1809 	case SIOCDELMULTI:
1810 	case SIOCSIFMEDIA:
1811 	case SIOCSVNETID:
1812 	case SIOCSIFPAIR:
1813 	case SIOCSIFPARENT:
1814 	case SIOCDIFPARENT:
1815 		if ((error = suser(p, 0)) != 0)
1816 			return (error);
1817 		/* FALLTHROUGH */
1818 	case SIOCGIFPSRCADDR:
1819 	case SIOCGIFPDSTADDR:
1820 	case SIOCGLIFPHYADDR:
1821 	case SIOCGLIFPHYRTABLE:
1822 	case SIOCGLIFPHYTTL:
1823 	case SIOCGIFMEDIA:
1824 	case SIOCGVNETID:
1825 	case SIOCGIFPAIR:
1826 	case SIOCGIFPARENT:
1827 		if (ifp->if_ioctl == 0)
1828 			return (EOPNOTSUPP);
1829 		error = (*ifp->if_ioctl)(ifp, cmd, data);
1830 		break;
1831 
1832 	case SIOCGIFDESCR:
1833 		strlcpy(ifdescrbuf, ifp->if_description, IFDESCRSIZE);
1834 		error = copyoutstr(ifdescrbuf, ifr->ifr_data, IFDESCRSIZE,
1835 		    &bytesdone);
1836 		break;
1837 
1838 	case SIOCSIFDESCR:
1839 		if ((error = suser(p, 0)) != 0)
1840 			return (error);
1841 		error = copyinstr(ifr->ifr_data, ifdescrbuf,
1842 		    IFDESCRSIZE, &bytesdone);
1843 		if (error == 0) {
1844 			(void)memset(ifp->if_description, 0, IFDESCRSIZE);
1845 			strlcpy(ifp->if_description, ifdescrbuf, IFDESCRSIZE);
1846 		}
1847 		break;
1848 
1849 	case SIOCGIFRTLABEL:
1850 		if (ifp->if_rtlabelid &&
1851 		    (label = rtlabel_id2name(ifp->if_rtlabelid)) != NULL) {
1852 			strlcpy(ifrtlabelbuf, label, RTLABEL_LEN);
1853 			error = copyoutstr(ifrtlabelbuf, ifr->ifr_data,
1854 			    RTLABEL_LEN, &bytesdone);
1855 		} else
1856 			error = ENOENT;
1857 		break;
1858 
1859 	case SIOCSIFRTLABEL:
1860 		if ((error = suser(p, 0)) != 0)
1861 			return (error);
1862 		error = copyinstr(ifr->ifr_data, ifrtlabelbuf,
1863 		    RTLABEL_LEN, &bytesdone);
1864 		if (error == 0) {
1865 			rtlabel_unref(ifp->if_rtlabelid);
1866 			ifp->if_rtlabelid = rtlabel_name2id(ifrtlabelbuf);
1867 		}
1868 		break;
1869 
1870 	case SIOCGIFPRIORITY:
1871 		ifr->ifr_metric = ifp->if_priority;
1872 		break;
1873 
1874 	case SIOCSIFPRIORITY:
1875 		if ((error = suser(p, 0)) != 0)
1876 			return (error);
1877 		if (ifr->ifr_metric < 0 || ifr->ifr_metric > 15)
1878 			return (EINVAL);
1879 		ifp->if_priority = ifr->ifr_metric;
1880 		break;
1881 
1882 	case SIOCGIFRDOMAIN:
1883 		ifr->ifr_rdomainid = ifp->if_rdomain;
1884 		break;
1885 
1886 	case SIOCSIFRDOMAIN:
1887 		if ((error = suser(p, 0)) != 0)
1888 			return (error);
1889 		if (ifr->ifr_rdomainid < 0 ||
1890 		    ifr->ifr_rdomainid > RT_TABLEID_MAX)
1891 			return (EINVAL);
1892 
1893 		/* make sure that the routing table exists */
1894 		if (!rtable_exists(ifr->ifr_rdomainid)) {
1895 			s = splsoftnet();
1896 			if ((error = rtable_add(ifr->ifr_rdomainid)) == 0)
1897 				rtable_l2set(ifr->ifr_rdomainid, ifr->ifr_rdomainid);
1898 			splx(s);
1899 			if (error)
1900 				return (error);
1901 		}
1902 
1903 		/* make sure that the routing table is a real rdomain */
1904 		if (ifr->ifr_rdomainid != rtable_l2(ifr->ifr_rdomainid))
1905 			return (EINVAL);
1906 
1907 		/* remove all routing entries when switching domains */
1908 		/* XXX hell this is ugly */
1909 		if (ifr->ifr_rdomainid != ifp->if_rdomain) {
1910 			s = splnet();
1911 			if (ifp->if_flags & IFF_UP)
1912 				up = 1;
1913 			/*
1914 			 * We are tearing down the world.
1915 			 * Take down the IF so:
1916 			 * 1. everything that cares gets a message
1917 			 * 2. the automagic IPv6 bits are recreated
1918 			 */
1919 			if (up)
1920 				if_down(ifp);
1921 			rt_if_remove(ifp);
1922 			rti_delete(ifp);
1923 #ifdef MROUTING
1924 			vif_delete(ifp);
1925 #endif
1926 #ifdef INET6
1927 			in6_ifdetach(ifp);
1928 #endif
1929 			in_ifdetach(ifp);
1930 			splx(s);
1931 		}
1932 
1933 		/* Let devices like enc(4) or mpe(4) know about the change */
1934 		if ((error = (*ifp->if_ioctl)(ifp, cmd, data)) != ENOTTY)
1935 			return (error);
1936 		error = 0;
1937 
1938 		/* Add interface to the specified rdomain */
1939 		ifp->if_rdomain = ifr->ifr_rdomainid;
1940 		break;
1941 
1942 	case SIOCAIFGROUP:
1943 		if ((error = suser(p, 0)))
1944 			return (error);
1945 		ifgr = (struct ifgroupreq *)data;
1946 		if ((error = if_addgroup(ifp, ifgr->ifgr_group)))
1947 			return (error);
1948 		(*ifp->if_ioctl)(ifp, cmd, data); /* XXX error check */
1949 		break;
1950 
1951 	case SIOCGIFGROUP:
1952 		if ((error = if_getgroup(data, ifp)))
1953 			return (error);
1954 		break;
1955 
1956 	case SIOCDIFGROUP:
1957 		if ((error = suser(p, 0)))
1958 			return (error);
1959 		(*ifp->if_ioctl)(ifp, cmd, data); /* XXX error check */
1960 		ifgr = (struct ifgroupreq *)data;
1961 		if ((error = if_delgroup(ifp, ifgr->ifgr_group)))
1962 			return (error);
1963 		break;
1964 
1965 	case SIOCSIFLLADDR:
1966 		if ((error = suser(p, 0)))
1967 			return (error);
1968 		sdl = ifp->if_sadl;
1969 		if (sdl == NULL)
1970 			return (EINVAL);
1971 		if (ifr->ifr_addr.sa_len != ETHER_ADDR_LEN)
1972 			return (EINVAL);
1973 		if (ETHER_IS_MULTICAST(ifr->ifr_addr.sa_data))
1974 			return (EINVAL);
1975 		switch (ifp->if_type) {
1976 		case IFT_ETHER:
1977 		case IFT_CARP:
1978 		case IFT_XETHER:
1979 		case IFT_ISO88025:
1980 			if_setlladdr(ifp, ifr->ifr_addr.sa_data);
1981 			error = (*ifp->if_ioctl)(ifp, cmd, data);
1982 			if (error == ENOTTY)
1983 				error = 0;
1984 			break;
1985 		default:
1986 			return (ENODEV);
1987 		}
1988 
1989 		ifnewlladdr(ifp);
1990 		break;
1991 
1992 	case SIOCGIFLLPRIO:
1993 		ifr->ifr_llprio = ifp->if_llprio;
1994 		break;
1995 
1996 	case SIOCSIFLLPRIO:
1997 		if ((error = suser(p, 0)))
1998 			return (error);
1999 		if (ifr->ifr_llprio > UCHAR_MAX)
2000 			return (EINVAL);
2001 		ifp->if_llprio = ifr->ifr_llprio;
2002 		break;
2003 
2004 	default:
2005 		if (so->so_proto == 0)
2006 			return (EOPNOTSUPP);
2007 		error = ((*so->so_proto->pr_usrreq)(so, PRU_CONTROL,
2008 			(struct mbuf *) cmd, (struct mbuf *) data,
2009 			(struct mbuf *) ifp, p));
2010 		break;
2011 	}
2012 
2013 	if (((oif_flags ^ ifp->if_flags) & IFF_UP) != 0)
2014 		microtime(&ifp->if_lastchange);
2015 
2016 	/* If we took down the IF, bring it back */
2017 	if (up) {
2018 		s = splnet();
2019 		if_up(ifp);
2020 		splx(s);
2021 	}
2022 	return (error);
2023 }
2024 
2025 /*
2026  * Return interface configuration
2027  * of system.  List may be used
2028  * in later ioctl's (above) to get
2029  * other information.
2030  */
2031 int
2032 ifconf(u_long cmd, caddr_t data)
2033 {
2034 	struct ifconf *ifc = (struct ifconf *)data;
2035 	struct ifnet *ifp;
2036 	struct ifaddr *ifa;
2037 	struct ifreq ifr, *ifrp;
2038 	int space = ifc->ifc_len, error = 0;
2039 
2040 	/* If ifc->ifc_len is 0, fill it in with the needed size and return. */
2041 	if (space == 0) {
2042 		TAILQ_FOREACH(ifp, &ifnet, if_list) {
2043 			struct sockaddr *sa;
2044 
2045 			if (TAILQ_EMPTY(&ifp->if_addrlist))
2046 				space += sizeof (ifr);
2047 			else
2048 				TAILQ_FOREACH(ifa,
2049 				    &ifp->if_addrlist, ifa_list) {
2050 					sa = ifa->ifa_addr;
2051 					if (sa->sa_len > sizeof(*sa))
2052 						space += sa->sa_len -
2053 						    sizeof(*sa);
2054 					space += sizeof(ifr);
2055 				}
2056 		}
2057 		ifc->ifc_len = space;
2058 		return (0);
2059 	}
2060 
2061 	ifrp = ifc->ifc_req;
2062 	TAILQ_FOREACH(ifp, &ifnet, if_list) {
2063 		if (space < sizeof(ifr))
2064 			break;
2065 		bcopy(ifp->if_xname, ifr.ifr_name, IFNAMSIZ);
2066 		if (TAILQ_EMPTY(&ifp->if_addrlist)) {
2067 			bzero((caddr_t)&ifr.ifr_addr, sizeof(ifr.ifr_addr));
2068 			error = copyout((caddr_t)&ifr, (caddr_t)ifrp,
2069 			    sizeof(ifr));
2070 			if (error)
2071 				break;
2072 			space -= sizeof (ifr), ifrp++;
2073 		} else
2074 			TAILQ_FOREACH(ifa, &ifp->if_addrlist, ifa_list) {
2075 				struct sockaddr *sa = ifa->ifa_addr;
2076 
2077 				if (space < sizeof(ifr))
2078 					break;
2079 				if (sa->sa_len <= sizeof(*sa)) {
2080 					ifr.ifr_addr = *sa;
2081 					error = copyout((caddr_t)&ifr,
2082 					    (caddr_t)ifrp, sizeof (ifr));
2083 					ifrp++;
2084 				} else {
2085 					space -= sa->sa_len - sizeof(*sa);
2086 					if (space < sizeof (ifr))
2087 						break;
2088 					error = copyout((caddr_t)&ifr,
2089 					    (caddr_t)ifrp,
2090 					    sizeof(ifr.ifr_name));
2091 					if (error == 0)
2092 						error = copyout((caddr_t)sa,
2093 						    (caddr_t)&ifrp->ifr_addr,
2094 						    sa->sa_len);
2095 					ifrp = (struct ifreq *)(sa->sa_len +
2096 					    (caddr_t)&ifrp->ifr_addr);
2097 				}
2098 				if (error)
2099 					break;
2100 				space -= sizeof (ifr);
2101 			}
2102 	}
2103 	ifc->ifc_len -= space;
2104 	return (error);
2105 }
2106 
2107 /*
2108  * Dummy functions replaced in ifnet during detach (if protocols decide to
2109  * fiddle with the if during detach.
2110  */
2111 void
2112 if_detached_start(struct ifnet *ifp)
2113 {
2114 	IFQ_PURGE(&ifp->if_snd);
2115 }
2116 
2117 int
2118 if_detached_ioctl(struct ifnet *ifp, u_long a, caddr_t b)
2119 {
2120 	return ENODEV;
2121 }
2122 
2123 /*
2124  * Create interface group without members
2125  */
2126 struct ifg_group *
2127 if_creategroup(const char *groupname)
2128 {
2129 	struct ifg_group	*ifg;
2130 
2131 	if ((ifg = malloc(sizeof(*ifg), M_TEMP, M_NOWAIT)) == NULL)
2132 		return (NULL);
2133 
2134 	strlcpy(ifg->ifg_group, groupname, sizeof(ifg->ifg_group));
2135 	ifg->ifg_refcnt = 0;
2136 	ifg->ifg_carp_demoted = 0;
2137 	TAILQ_INIT(&ifg->ifg_members);
2138 #if NPF > 0
2139 	pfi_attach_ifgroup(ifg);
2140 #endif
2141 	TAILQ_INSERT_TAIL(&ifg_head, ifg, ifg_next);
2142 
2143 	return (ifg);
2144 }
2145 
2146 /*
2147  * Add a group to an interface
2148  */
2149 int
2150 if_addgroup(struct ifnet *ifp, const char *groupname)
2151 {
2152 	struct ifg_list		*ifgl;
2153 	struct ifg_group	*ifg = NULL;
2154 	struct ifg_member	*ifgm;
2155 
2156 	if (groupname[0] && groupname[strlen(groupname) - 1] >= '0' &&
2157 	    groupname[strlen(groupname) - 1] <= '9')
2158 		return (EINVAL);
2159 
2160 	TAILQ_FOREACH(ifgl, &ifp->if_groups, ifgl_next)
2161 		if (!strcmp(ifgl->ifgl_group->ifg_group, groupname))
2162 			return (EEXIST);
2163 
2164 	if ((ifgl = malloc(sizeof(*ifgl), M_TEMP, M_NOWAIT)) == NULL)
2165 		return (ENOMEM);
2166 
2167 	if ((ifgm = malloc(sizeof(*ifgm), M_TEMP, M_NOWAIT)) == NULL) {
2168 		free(ifgl, M_TEMP, sizeof(*ifgl));
2169 		return (ENOMEM);
2170 	}
2171 
2172 	TAILQ_FOREACH(ifg, &ifg_head, ifg_next)
2173 		if (!strcmp(ifg->ifg_group, groupname))
2174 			break;
2175 
2176 	if (ifg == NULL && (ifg = if_creategroup(groupname)) == NULL) {
2177 		free(ifgl, M_TEMP, sizeof(*ifgl));
2178 		free(ifgm, M_TEMP, sizeof(*ifgm));
2179 		return (ENOMEM);
2180 	}
2181 
2182 	ifg->ifg_refcnt++;
2183 	ifgl->ifgl_group = ifg;
2184 	ifgm->ifgm_ifp = ifp;
2185 
2186 	TAILQ_INSERT_TAIL(&ifg->ifg_members, ifgm, ifgm_next);
2187 	TAILQ_INSERT_TAIL(&ifp->if_groups, ifgl, ifgl_next);
2188 
2189 #if NPF > 0
2190 	pfi_group_change(groupname);
2191 #endif
2192 
2193 	return (0);
2194 }
2195 
2196 /*
2197  * Remove a group from an interface
2198  */
2199 int
2200 if_delgroup(struct ifnet *ifp, const char *groupname)
2201 {
2202 	struct ifg_list		*ifgl;
2203 	struct ifg_member	*ifgm;
2204 
2205 	TAILQ_FOREACH(ifgl, &ifp->if_groups, ifgl_next)
2206 		if (!strcmp(ifgl->ifgl_group->ifg_group, groupname))
2207 			break;
2208 	if (ifgl == NULL)
2209 		return (ENOENT);
2210 
2211 	TAILQ_REMOVE(&ifp->if_groups, ifgl, ifgl_next);
2212 
2213 	TAILQ_FOREACH(ifgm, &ifgl->ifgl_group->ifg_members, ifgm_next)
2214 		if (ifgm->ifgm_ifp == ifp)
2215 			break;
2216 
2217 	if (ifgm != NULL) {
2218 		TAILQ_REMOVE(&ifgl->ifgl_group->ifg_members, ifgm, ifgm_next);
2219 		free(ifgm, M_TEMP, sizeof(*ifgm));
2220 	}
2221 
2222 	if (--ifgl->ifgl_group->ifg_refcnt == 0) {
2223 		TAILQ_REMOVE(&ifg_head, ifgl->ifgl_group, ifg_next);
2224 #if NPF > 0
2225 		pfi_detach_ifgroup(ifgl->ifgl_group);
2226 #endif
2227 		free(ifgl->ifgl_group, M_TEMP, 0);
2228 	}
2229 
2230 	free(ifgl, M_TEMP, sizeof(*ifgl));
2231 
2232 #if NPF > 0
2233 	pfi_group_change(groupname);
2234 #endif
2235 
2236 	return (0);
2237 }
2238 
2239 /*
2240  * Stores all groups from an interface in memory pointed
2241  * to by data
2242  */
2243 int
2244 if_getgroup(caddr_t data, struct ifnet *ifp)
2245 {
2246 	int			 len, error;
2247 	struct ifg_list		*ifgl;
2248 	struct ifg_req		 ifgrq, *ifgp;
2249 	struct ifgroupreq	*ifgr = (struct ifgroupreq *)data;
2250 
2251 	if (ifgr->ifgr_len == 0) {
2252 		TAILQ_FOREACH(ifgl, &ifp->if_groups, ifgl_next)
2253 			ifgr->ifgr_len += sizeof(struct ifg_req);
2254 		return (0);
2255 	}
2256 
2257 	len = ifgr->ifgr_len;
2258 	ifgp = ifgr->ifgr_groups;
2259 	TAILQ_FOREACH(ifgl, &ifp->if_groups, ifgl_next) {
2260 		if (len < sizeof(ifgrq))
2261 			return (EINVAL);
2262 		bzero(&ifgrq, sizeof ifgrq);
2263 		strlcpy(ifgrq.ifgrq_group, ifgl->ifgl_group->ifg_group,
2264 		    sizeof(ifgrq.ifgrq_group));
2265 		if ((error = copyout((caddr_t)&ifgrq, (caddr_t)ifgp,
2266 		    sizeof(struct ifg_req))))
2267 			return (error);
2268 		len -= sizeof(ifgrq);
2269 		ifgp++;
2270 	}
2271 
2272 	return (0);
2273 }
2274 
2275 /*
2276  * Stores all members of a group in memory pointed to by data
2277  */
2278 int
2279 if_getgroupmembers(caddr_t data)
2280 {
2281 	struct ifgroupreq	*ifgr = (struct ifgroupreq *)data;
2282 	struct ifg_group	*ifg;
2283 	struct ifg_member	*ifgm;
2284 	struct ifg_req		 ifgrq, *ifgp;
2285 	int			 len, error;
2286 
2287 	TAILQ_FOREACH(ifg, &ifg_head, ifg_next)
2288 		if (!strcmp(ifg->ifg_group, ifgr->ifgr_name))
2289 			break;
2290 	if (ifg == NULL)
2291 		return (ENOENT);
2292 
2293 	if (ifgr->ifgr_len == 0) {
2294 		TAILQ_FOREACH(ifgm, &ifg->ifg_members, ifgm_next)
2295 			ifgr->ifgr_len += sizeof(ifgrq);
2296 		return (0);
2297 	}
2298 
2299 	len = ifgr->ifgr_len;
2300 	ifgp = ifgr->ifgr_groups;
2301 	TAILQ_FOREACH(ifgm, &ifg->ifg_members, ifgm_next) {
2302 		if (len < sizeof(ifgrq))
2303 			return (EINVAL);
2304 		bzero(&ifgrq, sizeof ifgrq);
2305 		strlcpy(ifgrq.ifgrq_member, ifgm->ifgm_ifp->if_xname,
2306 		    sizeof(ifgrq.ifgrq_member));
2307 		if ((error = copyout((caddr_t)&ifgrq, (caddr_t)ifgp,
2308 		    sizeof(struct ifg_req))))
2309 			return (error);
2310 		len -= sizeof(ifgrq);
2311 		ifgp++;
2312 	}
2313 
2314 	return (0);
2315 }
2316 
2317 int
2318 if_getgroupattribs(caddr_t data)
2319 {
2320 	struct ifgroupreq	*ifgr = (struct ifgroupreq *)data;
2321 	struct ifg_group	*ifg;
2322 
2323 	TAILQ_FOREACH(ifg, &ifg_head, ifg_next)
2324 		if (!strcmp(ifg->ifg_group, ifgr->ifgr_name))
2325 			break;
2326 	if (ifg == NULL)
2327 		return (ENOENT);
2328 
2329 	ifgr->ifgr_attrib.ifg_carp_demoted = ifg->ifg_carp_demoted;
2330 
2331 	return (0);
2332 }
2333 
2334 int
2335 if_setgroupattribs(caddr_t data)
2336 {
2337 	struct ifgroupreq	*ifgr = (struct ifgroupreq *)data;
2338 	struct ifg_group	*ifg;
2339 	struct ifg_member	*ifgm;
2340 	int			 demote;
2341 
2342 	TAILQ_FOREACH(ifg, &ifg_head, ifg_next)
2343 		if (!strcmp(ifg->ifg_group, ifgr->ifgr_name))
2344 			break;
2345 	if (ifg == NULL)
2346 		return (ENOENT);
2347 
2348 	demote = ifgr->ifgr_attrib.ifg_carp_demoted;
2349 	if (demote + ifg->ifg_carp_demoted > 0xff ||
2350 	    demote + ifg->ifg_carp_demoted < 0)
2351 		return (EINVAL);
2352 
2353 	ifg->ifg_carp_demoted += demote;
2354 
2355 	TAILQ_FOREACH(ifgm, &ifg->ifg_members, ifgm_next)
2356 		if (ifgm->ifgm_ifp->if_ioctl)
2357 			ifgm->ifgm_ifp->if_ioctl(ifgm->ifgm_ifp,
2358 			    SIOCSIFGATTR, data);
2359 	return (0);
2360 }
2361 
2362 void
2363 if_group_routechange(struct sockaddr *dst, struct sockaddr *mask)
2364 {
2365 	switch (dst->sa_family) {
2366 	case AF_INET:
2367 		if (satosin(dst)->sin_addr.s_addr == INADDR_ANY &&
2368 		    mask && (mask->sa_len == 0 ||
2369 		    satosin(mask)->sin_addr.s_addr == INADDR_ANY))
2370 			if_group_egress_build();
2371 		break;
2372 #ifdef INET6
2373 	case AF_INET6:
2374 		if (IN6_ARE_ADDR_EQUAL(&(satosin6(dst))->sin6_addr,
2375 		    &in6addr_any) && mask && (mask->sa_len == 0 ||
2376 		    IN6_ARE_ADDR_EQUAL(&(satosin6(mask))->sin6_addr,
2377 		    &in6addr_any)))
2378 			if_group_egress_build();
2379 		break;
2380 #endif
2381 	}
2382 }
2383 
2384 int
2385 if_group_egress_build(void)
2386 {
2387 	struct ifnet		*ifp;
2388 	struct ifg_group	*ifg;
2389 	struct ifg_member	*ifgm, *next;
2390 	struct sockaddr_in	 sa_in;
2391 #ifdef INET6
2392 	struct sockaddr_in6	 sa_in6;
2393 #endif
2394 	struct rtentry		*rt0, *rt;
2395 
2396 	TAILQ_FOREACH(ifg, &ifg_head, ifg_next)
2397 		if (!strcmp(ifg->ifg_group, IFG_EGRESS))
2398 			break;
2399 
2400 	if (ifg != NULL)
2401 		TAILQ_FOREACH_SAFE(ifgm, &ifg->ifg_members, ifgm_next, next)
2402 			if_delgroup(ifgm->ifgm_ifp, IFG_EGRESS);
2403 
2404 	bzero(&sa_in, sizeof(sa_in));
2405 	sa_in.sin_len = sizeof(sa_in);
2406 	sa_in.sin_family = AF_INET;
2407 	rt0 = rtable_lookup(0, sintosa(&sa_in), sintosa(&sa_in), NULL, RTP_ANY);
2408 	if (rt0 != NULL) {
2409 		rt = rt0;
2410 		do {
2411 			ifp = if_get(rt->rt_ifidx);
2412 			if (ifp != NULL) {
2413 				if_addgroup(ifp, IFG_EGRESS);
2414 				if_put(ifp);
2415 			}
2416 #ifndef SMALL_KERNEL
2417 			rt = rtable_mpath_next(rt);
2418 #else
2419 			rt = NULL;
2420 #endif
2421 		} while (rt != NULL);
2422 	}
2423 	rtfree(rt0);
2424 
2425 #ifdef INET6
2426 	bcopy(&sa6_any, &sa_in6, sizeof(sa_in6));
2427 	rt0 = rtable_lookup(0, sin6tosa(&sa_in6), sin6tosa(&sa_in6), NULL,
2428 	    RTP_ANY);
2429 	if (rt0 != NULL) {
2430 		rt = rt0;
2431 		do {
2432 			ifp = if_get(rt->rt_ifidx);
2433 			if (ifp != NULL) {
2434 				if_addgroup(ifp, IFG_EGRESS);
2435 				if_put(ifp);
2436 			}
2437 #ifndef SMALL_KERNEL
2438 			rt = rtable_mpath_next(rt);
2439 #else
2440 			rt = NULL;
2441 #endif
2442 		} while (rt != NULL);
2443 	}
2444 	rtfree(rt0);
2445 #endif /* INET6 */
2446 
2447 	return (0);
2448 }
2449 
2450 /*
2451  * Set/clear promiscuous mode on interface ifp based on the truth value
2452  * of pswitch.  The calls are reference counted so that only the first
2453  * "on" request actually has an effect, as does the final "off" request.
2454  * Results are undefined if the "off" and "on" requests are not matched.
2455  */
2456 int
2457 ifpromisc(struct ifnet *ifp, int pswitch)
2458 {
2459 	struct ifreq ifr;
2460 
2461 	if (pswitch) {
2462 		/*
2463 		 * If the device is not configured up, we cannot put it in
2464 		 * promiscuous mode.
2465 		 */
2466 		if ((ifp->if_flags & IFF_UP) == 0)
2467 			return (ENETDOWN);
2468 		if (ifp->if_pcount++ != 0)
2469 			return (0);
2470 		ifp->if_flags |= IFF_PROMISC;
2471 	} else {
2472 		if (--ifp->if_pcount > 0)
2473 			return (0);
2474 		ifp->if_flags &= ~IFF_PROMISC;
2475 		/*
2476 		 * If the device is not configured up, we should not need to
2477 		 * turn off promiscuous mode (device should have turned it
2478 		 * off when interface went down; and will look at IFF_PROMISC
2479 		 * again next time interface comes up).
2480 		 */
2481 		if ((ifp->if_flags & IFF_UP) == 0)
2482 			return (0);
2483 	}
2484 	ifr.ifr_flags = ifp->if_flags;
2485 	return ((*ifp->if_ioctl)(ifp, SIOCSIFFLAGS, (caddr_t)&ifr));
2486 }
2487 
2488 int
2489 sysctl_mq(int *name, u_int namelen, void *oldp, size_t *oldlenp,
2490     void *newp, size_t newlen, struct mbuf_queue *mq)
2491 {
2492 	/* All sysctl names at this level are terminal. */
2493 	if (namelen != 1)
2494 		return (ENOTDIR);
2495 
2496 	switch (name[0]) {
2497 	case IFQCTL_LEN:
2498 		return (sysctl_rdint(oldp, oldlenp, newp, mq_len(mq)));
2499 	case IFQCTL_MAXLEN:
2500 		return (sysctl_int(oldp, oldlenp, newp, newlen,
2501 		    &mq->mq_maxlen)); /* XXX directly accessing maxlen */
2502 	case IFQCTL_DROPS:
2503 		return (sysctl_rdint(oldp, oldlenp, newp, mq_drops(mq)));
2504 	default:
2505 		return (EOPNOTSUPP);
2506 	}
2507 	/* NOTREACHED */
2508 }
2509 
2510 void
2511 ifa_add(struct ifnet *ifp, struct ifaddr *ifa)
2512 {
2513 	TAILQ_INSERT_TAIL(&ifp->if_addrlist, ifa, ifa_list);
2514 }
2515 
2516 void
2517 ifa_del(struct ifnet *ifp, struct ifaddr *ifa)
2518 {
2519 	TAILQ_REMOVE(&ifp->if_addrlist, ifa, ifa_list);
2520 }
2521 
2522 void
2523 ifa_update_broadaddr(struct ifnet *ifp, struct ifaddr *ifa, struct sockaddr *sa)
2524 {
2525 	if (ifa->ifa_broadaddr->sa_len != sa->sa_len)
2526 		panic("ifa_update_broadaddr does not support dynamic length");
2527 	bcopy(sa, ifa->ifa_broadaddr, sa->sa_len);
2528 }
2529 
2530 #ifdef DDB
2531 /* debug function, can be called from ddb> */
2532 void
2533 ifa_print_all(void)
2534 {
2535 	struct ifnet *ifp;
2536 	struct ifaddr *ifa;
2537 
2538 	TAILQ_FOREACH(ifp, &ifnet, if_list) {
2539 		TAILQ_FOREACH(ifa, &ifp->if_addrlist, ifa_list) {
2540 			char addr[INET6_ADDRSTRLEN];
2541 
2542 			switch (ifa->ifa_addr->sa_family) {
2543 			case AF_INET:
2544 				printf("%s", inet_ntop(AF_INET,
2545 				    &satosin(ifa->ifa_addr)->sin_addr,
2546 				    addr, sizeof(addr)));
2547 				break;
2548 #ifdef INET6
2549 			case AF_INET6:
2550 				printf("%s", inet_ntop(AF_INET6,
2551 				    &(satosin6(ifa->ifa_addr))->sin6_addr,
2552 				    addr, sizeof(addr)));
2553 				break;
2554 #endif
2555 			}
2556 			printf(" on %s\n", ifp->if_xname);
2557 		}
2558 	}
2559 }
2560 #endif /* DDB */
2561 
2562 void
2563 ifnewlladdr(struct ifnet *ifp)
2564 {
2565 #ifdef INET6
2566 	struct ifaddr *ifa;
2567 #endif
2568 	struct ifreq ifrq;
2569 	short up;
2570 	int s;
2571 
2572 	s = splnet();
2573 	up = ifp->if_flags & IFF_UP;
2574 
2575 	if (up) {
2576 		/* go down for a moment... */
2577 		ifp->if_flags &= ~IFF_UP;
2578 		ifrq.ifr_flags = ifp->if_flags;
2579 		(*ifp->if_ioctl)(ifp, SIOCSIFFLAGS, (caddr_t)&ifrq);
2580 	}
2581 
2582 	ifp->if_flags |= IFF_UP;
2583 	ifrq.ifr_flags = ifp->if_flags;
2584 	(*ifp->if_ioctl)(ifp, SIOCSIFFLAGS, (caddr_t)&ifrq);
2585 
2586 #ifdef INET6
2587 	/*
2588 	 * Update the link-local address.  Don't do it if we're
2589 	 * a router to avoid confusing hosts on the network.
2590 	 */
2591 	if (!ip6_forwarding) {
2592 		ifa = &in6ifa_ifpforlinklocal(ifp, 0)->ia_ifa;
2593 		if (ifa) {
2594 			in6_purgeaddr(ifa);
2595 			dohooks(ifp->if_addrhooks, 0);
2596 			in6_ifattach(ifp);
2597 		}
2598 	}
2599 #endif
2600 	if (!up) {
2601 		/* go back down */
2602 		ifp->if_flags &= ~IFF_UP;
2603 		ifrq.ifr_flags = ifp->if_flags;
2604 		(*ifp->if_ioctl)(ifp, SIOCSIFFLAGS, (caddr_t)&ifrq);
2605 	}
2606 	splx(s);
2607 }
2608 
2609 int net_ticks;
2610 u_int net_livelocks;
2611 
2612 void
2613 net_tick(void *null)
2614 {
2615 	extern int ticks;
2616 
2617 	if (ticks - net_ticks > 1)
2618 		net_livelocks++;
2619 
2620 	net_ticks = ticks;
2621 
2622 	timeout_add(&net_tick_to, 1);
2623 }
2624 
2625 int
2626 net_livelocked(void)
2627 {
2628 	extern int ticks;
2629 
2630 	return (ticks - net_ticks > 1);
2631 }
2632 
2633 void
2634 if_rxr_init(struct if_rxring *rxr, u_int lwm, u_int hwm)
2635 {
2636 	extern int ticks;
2637 
2638 	memset(rxr, 0, sizeof(*rxr));
2639 
2640 	rxr->rxr_adjusted = ticks;
2641 	rxr->rxr_cwm = rxr->rxr_lwm = lwm;
2642 	rxr->rxr_hwm = hwm;
2643 }
2644 
2645 static inline void
2646 if_rxr_adjust_cwm(struct if_rxring *rxr)
2647 {
2648 	extern int ticks;
2649 
2650 	if (net_livelocked()) {
2651 		if (rxr->rxr_cwm > rxr->rxr_lwm)
2652 			rxr->rxr_cwm--;
2653 		else
2654 			return;
2655 	} else if (rxr->rxr_alive >= rxr->rxr_lwm)
2656 		return;
2657 	else if (rxr->rxr_cwm < rxr->rxr_hwm)
2658 		rxr->rxr_cwm++;
2659 
2660 	rxr->rxr_adjusted = ticks;
2661 }
2662 
2663 u_int
2664 if_rxr_get(struct if_rxring *rxr, u_int max)
2665 {
2666 	extern int ticks;
2667 	u_int diff;
2668 
2669 	if (ticks - rxr->rxr_adjusted >= 1) {
2670 		/* we're free to try for an adjustment */
2671 		if_rxr_adjust_cwm(rxr);
2672 	}
2673 
2674 	if (rxr->rxr_alive >= rxr->rxr_cwm)
2675 		return (0);
2676 
2677 	diff = min(rxr->rxr_cwm - rxr->rxr_alive, max);
2678 	rxr->rxr_alive += diff;
2679 
2680 	return (diff);
2681 }
2682 
2683 int
2684 if_rxr_info_ioctl(struct if_rxrinfo *uifri, u_int t, struct if_rxring_info *e)
2685 {
2686 	struct if_rxrinfo kifri;
2687 	int error;
2688 	u_int n;
2689 
2690 	error = copyin(uifri, &kifri, sizeof(kifri));
2691 	if (error)
2692 		return (error);
2693 
2694 	n = min(t, kifri.ifri_total);
2695 	kifri.ifri_total = t;
2696 
2697 	if (n > 0) {
2698 		error = copyout(e, kifri.ifri_entries, sizeof(*e) * n);
2699 		if (error)
2700 			return (error);
2701 	}
2702 
2703 	return (copyout(&kifri, uifri, sizeof(kifri)));
2704 }
2705 
2706 int
2707 if_rxr_ioctl(struct if_rxrinfo *ifri, const char *name, u_int size,
2708     struct if_rxring *rxr)
2709 {
2710 	struct if_rxring_info ifr;
2711 
2712 	memset(&ifr, 0, sizeof(ifr));
2713 
2714 	if (name != NULL)
2715 		strlcpy(ifr.ifr_name, name, sizeof(ifr.ifr_name));
2716 
2717 	ifr.ifr_size = size;
2718 	ifr.ifr_info = *rxr;
2719 
2720 	return (if_rxr_info_ioctl(ifri, 1, &ifr));
2721 }
2722 
2723 /*
2724  * Network stack input queues.
2725  */
2726 
2727 void
2728 niq_init(struct niqueue *niq, u_int maxlen, u_int isr)
2729 {
2730 	mq_init(&niq->ni_q, maxlen, IPL_NET);
2731 	niq->ni_isr = isr;
2732 }
2733 
2734 int
2735 niq_enqueue(struct niqueue *niq, struct mbuf *m)
2736 {
2737 	int rv;
2738 
2739 	rv = mq_enqueue(&niq->ni_q, m);
2740 	if (rv == 0)
2741 		schednetisr(niq->ni_isr);
2742 	else
2743 		if_congestion();
2744 
2745 	return (rv);
2746 }
2747 
2748 int
2749 niq_enlist(struct niqueue *niq, struct mbuf_list *ml)
2750 {
2751 	int rv;
2752 
2753 	rv = mq_enlist(&niq->ni_q, ml);
2754 	if (rv == 0)
2755 		schednetisr(niq->ni_isr);
2756 	else
2757 		if_congestion();
2758 
2759 	return (rv);
2760 }
2761 
2762 __dead void
2763 unhandled_af(int af)
2764 {
2765 	panic("unhandled af %d", af);
2766 }
2767