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