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