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