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