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