xref: /netbsd-src/sys/net/if.c (revision 8ac07aec990b9d2e483062509d0a9fa5b4f57cf2)
1 /*	$NetBSD: if.c,v 1.220 2008/04/24 13:30:52 martin Exp $	*/
2 
3 /*-
4  * Copyright (c) 1999, 2000, 2001, 2008 The NetBSD Foundation, Inc.
5  * All rights reserved.
6  *
7  * This code is derived from software contributed to The NetBSD Foundation
8  * by William Studenmund and Jason R. Thorpe.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  * 3. All advertising materials mentioning features or use of this software
19  *    must display the following acknowledgement:
20  *	This product includes software developed by the NetBSD
21  *	Foundation, Inc. and its contributors.
22  * 4. Neither the name of The NetBSD Foundation nor the names of its
23  *    contributors may be used to endorse or promote products derived
24  *    from this software without specific prior written permission.
25  *
26  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
27  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
30  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36  * POSSIBILITY OF SUCH DAMAGE.
37  */
38 
39 /*
40  * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
41  * All rights reserved.
42  *
43  * Redistribution and use in source and binary forms, with or without
44  * modification, are permitted provided that the following conditions
45  * are met:
46  * 1. Redistributions of source code must retain the above copyright
47  *    notice, this list of conditions and the following disclaimer.
48  * 2. Redistributions in binary form must reproduce the above copyright
49  *    notice, this list of conditions and the following disclaimer in the
50  *    documentation and/or other materials provided with the distribution.
51  * 3. Neither the name of the project nor the names of its contributors
52  *    may be used to endorse or promote products derived from this software
53  *    without specific prior written permission.
54  *
55  * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
56  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
57  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
58  * ARE DISCLAIMED.  IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
59  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
60  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
61  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
62  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
63  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
64  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
65  * SUCH DAMAGE.
66  */
67 
68 /*
69  * Copyright (c) 1980, 1986, 1993
70  *	The Regents of the University of California.  All rights reserved.
71  *
72  * Redistribution and use in source and binary forms, with or without
73  * modification, are permitted provided that the following conditions
74  * are met:
75  * 1. Redistributions of source code must retain the above copyright
76  *    notice, this list of conditions and the following disclaimer.
77  * 2. Redistributions in binary form must reproduce the above copyright
78  *    notice, this list of conditions and the following disclaimer in the
79  *    documentation and/or other materials provided with the distribution.
80  * 3. Neither the name of the University nor the names of its contributors
81  *    may be used to endorse or promote products derived from this software
82  *    without specific prior written permission.
83  *
84  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
85  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
86  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
87  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
88  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
89  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
90  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
91  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
92  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
93  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
94  * SUCH DAMAGE.
95  *
96  *	@(#)if.c	8.5 (Berkeley) 1/9/95
97  */
98 
99 #include <sys/cdefs.h>
100 __KERNEL_RCSID(0, "$NetBSD: if.c,v 1.220 2008/04/24 13:30:52 martin Exp $");
101 
102 #include "opt_inet.h"
103 
104 #include "opt_atalk.h"
105 #include "opt_natm.h"
106 #include "opt_pfil_hooks.h"
107 
108 #include <sys/param.h>
109 #include <sys/mbuf.h>
110 #include <sys/systm.h>
111 #include <sys/callout.h>
112 #include <sys/proc.h>
113 #include <sys/socket.h>
114 #include <sys/socketvar.h>
115 #include <sys/domain.h>
116 #include <sys/protosw.h>
117 #include <sys/kernel.h>
118 #include <sys/ioctl.h>
119 #include <sys/sysctl.h>
120 #include <sys/syslog.h>
121 #include <sys/kauth.h>
122 
123 #include <net/if.h>
124 #include <net/if_dl.h>
125 #include <net/if_ether.h>
126 #include <net/if_media.h>
127 #include <net80211/ieee80211.h>
128 #include <net80211/ieee80211_ioctl.h>
129 #include <net/if_types.h>
130 #include <net/radix.h>
131 #include <net/route.h>
132 #include <net/netisr.h>
133 #ifdef NETATALK
134 #include <netatalk/at_extern.h>
135 #include <netatalk/at.h>
136 #endif
137 #include <net/pfil.h>
138 
139 #ifdef INET6
140 #include <netinet/in.h>
141 #include <netinet6/in6_var.h>
142 #include <netinet6/nd6.h>
143 #endif
144 
145 #include "carp.h"
146 #if NCARP > 0
147 #include <netinet/ip_carp.h>
148 #endif
149 
150 #include <compat/sys/sockio.h>
151 #include <compat/sys/socket.h>
152 
153 MALLOC_DEFINE(M_IFADDR, "ifaddr", "interface address");
154 MALLOC_DEFINE(M_IFMADDR, "ether_multi", "link-level multicast address");
155 
156 int	ifqmaxlen = IFQ_MAXLEN;
157 callout_t if_slowtimo_ch;
158 
159 int netisr;			/* scheduling bits for network */
160 
161 static int	if_rt_walktree(struct rtentry *, void *);
162 
163 static struct if_clone *if_clone_lookup(const char *, int *);
164 static int	if_clone_list(struct if_clonereq *);
165 
166 static LIST_HEAD(, if_clone) if_cloners = LIST_HEAD_INITIALIZER(if_cloners);
167 static int if_cloners_count;
168 
169 #ifdef PFIL_HOOKS
170 struct pfil_head if_pfil;	/* packet filtering hook for interfaces */
171 #endif
172 
173 static void if_detach_queues(struct ifnet *, struct ifqueue *);
174 
175 /*
176  * Network interface utility routines.
177  *
178  * Routines with ifa_ifwith* names take sockaddr *'s as
179  * parameters.
180  */
181 void
182 ifinit(void)
183 {
184 
185 	callout_init(&if_slowtimo_ch, 0);
186 	if_slowtimo(NULL);
187 #ifdef PFIL_HOOKS
188 	if_pfil.ph_type = PFIL_TYPE_IFNET;
189 	if_pfil.ph_ifnet = NULL;
190 	if (pfil_head_register(&if_pfil) != 0)
191 		printf("WARNING: unable to register pfil hook\n");
192 #endif
193 }
194 
195 /*
196  * Null routines used while an interface is going away.  These routines
197  * just return an error.
198  */
199 
200 int
201 if_nulloutput(struct ifnet *ifp, struct mbuf *m,
202     const struct sockaddr *so, struct rtentry *rt)
203 {
204 
205 	return ENXIO;
206 }
207 
208 void
209 if_nullinput(struct ifnet *ifp, struct mbuf *m)
210 {
211 
212 	/* Nothing. */
213 }
214 
215 void
216 if_nullstart(struct ifnet *ifp)
217 {
218 
219 	/* Nothing. */
220 }
221 
222 int
223 if_nullioctl(struct ifnet *ifp, u_long cmd, void *data)
224 {
225 
226 	return ENXIO;
227 }
228 
229 int
230 if_nullinit(struct ifnet *ifp)
231 {
232 
233 	return ENXIO;
234 }
235 
236 void
237 if_nullstop(struct ifnet *ifp, int disable)
238 {
239 
240 	/* Nothing. */
241 }
242 
243 void
244 if_nullwatchdog(struct ifnet *ifp)
245 {
246 
247 	/* Nothing. */
248 }
249 
250 void
251 if_nulldrain(struct ifnet *ifp)
252 {
253 
254 	/* Nothing. */
255 }
256 
257 static u_int if_index = 1;
258 struct ifnet_head ifnet;
259 size_t if_indexlim = 0;
260 struct ifaddr **ifnet_addrs = NULL;
261 struct ifnet **ifindex2ifnet = NULL;
262 struct ifnet *lo0ifp;
263 
264 void
265 if_set_sadl(struct ifnet *ifp, const void *lla, u_char addrlen)
266 {
267 	struct ifaddr *ifa;
268 	struct sockaddr_dl *sdl;
269 
270 	ifp->if_addrlen = addrlen;
271 	if_alloc_sadl(ifp);
272 	ifa = ifp->if_dl;
273 	sdl = satosdl(ifa->ifa_addr);
274 
275 	(void)sockaddr_dl_setaddr(sdl, sdl->sdl_len, lla, ifp->if_addrlen);
276 }
277 
278 struct ifaddr *
279 if_dl_create(const struct ifnet *ifp, const struct sockaddr_dl **sdlp)
280 {
281 	unsigned socksize, ifasize;
282 	int addrlen, namelen;
283 	struct sockaddr_dl *mask, *sdl;
284 	struct ifaddr *ifa;
285 
286 	namelen = strlen(ifp->if_xname);
287 	addrlen = ifp->if_addrlen;
288 	socksize = roundup(sockaddr_dl_measure(namelen, addrlen), sizeof(long));
289 	ifasize = sizeof(*ifa) + 2 * socksize;
290 	ifa = (struct ifaddr *)malloc(ifasize, M_IFADDR, M_WAITOK|M_ZERO);
291 
292 	sdl = (struct sockaddr_dl *)(ifa + 1);
293 	mask = (struct sockaddr_dl *)(socksize + (char *)sdl);
294 
295 	sockaddr_dl_init(sdl, socksize, ifp->if_index, ifp->if_type,
296 	    ifp->if_xname, namelen, NULL, addrlen);
297 	mask->sdl_len = sockaddr_dl_measure(namelen, 0);
298 	memset(&mask->sdl_data[0], 0xff, namelen);
299 	ifa->ifa_rtrequest = link_rtrequest;
300 	ifa->ifa_addr = (struct sockaddr *)sdl;
301 	ifa->ifa_netmask = (struct sockaddr *)mask;
302 
303 	*sdlp = sdl;
304 
305 	return ifa;
306 }
307 
308 /*
309  * Allocate the link level name for the specified interface.  This
310  * is an attachment helper.  It must be called after ifp->if_addrlen
311  * is initialized, which may not be the case when if_attach() is
312  * called.
313  */
314 void
315 if_alloc_sadl(struct ifnet *ifp)
316 {
317 	struct ifaddr *ifa;
318 	const struct sockaddr_dl *sdl;
319 
320 	/*
321 	 * If the interface already has a link name, release it
322 	 * now.  This is useful for interfaces that can change
323 	 * link types, and thus switch link names often.
324 	 */
325 	if (ifp->if_sadl != NULL)
326 		if_free_sadl(ifp);
327 
328 	ifa = if_dl_create(ifp, &sdl);
329 
330 	ifnet_addrs[ifp->if_index] = ifa;
331 	IFAREF(ifa);
332 	ifa_insert(ifp, ifa);
333 	ifp->if_dl = ifa;
334 	IFAREF(ifa);
335 	ifp->if_sadl = sdl;
336 }
337 
338 /*
339  * Free the link level name for the specified interface.  This is
340  * a detach helper.  This is called from if_detach() or from
341  * link layer type specific detach functions.
342  */
343 void
344 if_free_sadl(struct ifnet *ifp)
345 {
346 	struct ifaddr *ifa;
347 	int s;
348 
349 	ifa = ifnet_addrs[ifp->if_index];
350 	if (ifa == NULL) {
351 		KASSERT(ifp->if_sadl == NULL);
352 		KASSERT(ifp->if_dl == NULL);
353 		return;
354 	}
355 
356 	KASSERT(ifp->if_sadl != NULL);
357 	KASSERT(ifp->if_dl != NULL);
358 
359 	s = splnet();
360 	rtinit(ifa, RTM_DELETE, 0);
361 	ifa_remove(ifp, ifa);
362 
363 	ifp->if_sadl = NULL;
364 
365 	ifnet_addrs[ifp->if_index] = NULL;
366 	IFAFREE(ifa);
367 	ifp->if_dl = NULL;
368 	IFAFREE(ifa);
369 	splx(s);
370 }
371 
372 /*
373  * Attach an interface to the
374  * list of "active" interfaces.
375  */
376 void
377 if_attach(struct ifnet *ifp)
378 {
379 	int indexlim = 0;
380 
381 	if (if_indexlim == 0) {
382 		TAILQ_INIT(&ifnet);
383 		if_indexlim = 8;
384 	}
385 	TAILQ_INIT(&ifp->if_addrlist);
386 	TAILQ_INSERT_TAIL(&ifnet, ifp, if_list);
387 	ifp->if_index = if_index;
388 	if (ifindex2ifnet == NULL)
389 		if_index++;
390 	else
391 		while (ifp->if_index < if_indexlim &&
392 		    ifindex2ifnet[ifp->if_index] != NULL) {
393 			++if_index;
394 			if (if_index == 0)
395 				if_index = 1;
396 			/*
397 			 * If we hit USHRT_MAX, we skip back to 0 since
398 			 * there are a number of places where the value
399 			 * of if_index or if_index itself is compared
400 			 * to or stored in an unsigned short.  By
401 			 * jumping back, we won't botch those assignments
402 			 * or comparisons.
403 			 */
404 			else if (if_index == USHRT_MAX) {
405 				/*
406 				 * However, if we have to jump back to
407 				 * zero *twice* without finding an empty
408 				 * slot in ifindex2ifnet[], then there
409 				 * there are too many (>65535) interfaces.
410 				 */
411 				if (indexlim++)
412 					panic("too many interfaces");
413 				else
414 					if_index = 1;
415 			}
416 			ifp->if_index = if_index;
417 		}
418 
419 	/*
420 	 * We have some arrays that should be indexed by if_index.
421 	 * since if_index will grow dynamically, they should grow too.
422 	 *	struct ifadd **ifnet_addrs
423 	 *	struct ifnet **ifindex2ifnet
424 	 */
425 	if (ifnet_addrs == NULL || ifindex2ifnet == NULL ||
426 	    ifp->if_index >= if_indexlim) {
427 		size_t m, n, oldlim;
428 		void *q;
429 
430 		oldlim = if_indexlim;
431 		while (ifp->if_index >= if_indexlim)
432 			if_indexlim <<= 1;
433 
434 		/* grow ifnet_addrs */
435 		m = oldlim * sizeof(struct ifaddr *);
436 		n = if_indexlim * sizeof(struct ifaddr *);
437 		q = (void *)malloc(n, M_IFADDR, M_WAITOK|M_ZERO);
438 		if (ifnet_addrs != NULL) {
439 			memcpy(q, ifnet_addrs, m);
440 			free((void *)ifnet_addrs, M_IFADDR);
441 		}
442 		ifnet_addrs = (struct ifaddr **)q;
443 
444 		/* grow ifindex2ifnet */
445 		m = oldlim * sizeof(struct ifnet *);
446 		n = if_indexlim * sizeof(struct ifnet *);
447 		q = (void *)malloc(n, M_IFADDR, M_WAITOK|M_ZERO);
448 		if (ifindex2ifnet != NULL) {
449 			memcpy(q, (void *)ifindex2ifnet, m);
450 			free((void *)ifindex2ifnet, M_IFADDR);
451 		}
452 		ifindex2ifnet = (struct ifnet **)q;
453 	}
454 
455 	ifindex2ifnet[ifp->if_index] = ifp;
456 
457 	/*
458 	 * Link level name is allocated later by a separate call to
459 	 * if_alloc_sadl().
460 	 */
461 
462 	if (ifp->if_snd.ifq_maxlen == 0)
463 		ifp->if_snd.ifq_maxlen = ifqmaxlen;
464 	ifp->if_broadcastaddr = 0; /* reliably crash if used uninitialized */
465 
466 	ifp->if_link_state = LINK_STATE_UNKNOWN;
467 
468 	ifp->if_capenable = 0;
469 	ifp->if_csum_flags_tx = 0;
470 	ifp->if_csum_flags_rx = 0;
471 
472 #ifdef ALTQ
473 	ifp->if_snd.altq_type = 0;
474 	ifp->if_snd.altq_disc = NULL;
475 	ifp->if_snd.altq_flags &= ALTQF_CANTCHANGE;
476 	ifp->if_snd.altq_tbr  = NULL;
477 	ifp->if_snd.altq_ifp  = ifp;
478 #endif
479 
480 #ifdef PFIL_HOOKS
481 	ifp->if_pfil.ph_type = PFIL_TYPE_IFNET;
482 	ifp->if_pfil.ph_ifnet = ifp;
483 	if (pfil_head_register(&ifp->if_pfil) != 0)
484 		printf("%s: WARNING: unable to register pfil hook\n",
485 		    ifp->if_xname);
486 	(void)pfil_run_hooks(&if_pfil,
487 	    (struct mbuf **)PFIL_IFNET_ATTACH, ifp, PFIL_IFNET);
488 #endif
489 
490 	if (!STAILQ_EMPTY(&domains))
491 		if_attachdomain1(ifp);
492 
493 	/* Announce the interface. */
494 	rt_ifannouncemsg(ifp, IFAN_ARRIVAL);
495 }
496 
497 void
498 if_attachdomain(void)
499 {
500 	struct ifnet *ifp;
501 	int s;
502 
503 	s = splnet();
504 	IFNET_FOREACH(ifp)
505 		if_attachdomain1(ifp);
506 	splx(s);
507 }
508 
509 void
510 if_attachdomain1(struct ifnet *ifp)
511 {
512 	struct domain *dp;
513 	int s;
514 
515 	s = splnet();
516 
517 	/* address family dependent data region */
518 	memset(ifp->if_afdata, 0, sizeof(ifp->if_afdata));
519 	DOMAIN_FOREACH(dp) {
520 		if (dp->dom_ifattach != NULL)
521 			ifp->if_afdata[dp->dom_family] =
522 			    (*dp->dom_ifattach)(ifp);
523 	}
524 
525 	splx(s);
526 }
527 
528 /*
529  * Deactivate an interface.  This points all of the procedure
530  * handles at error stubs.  May be called from interrupt context.
531  */
532 void
533 if_deactivate(struct ifnet *ifp)
534 {
535 	int s;
536 
537 	s = splnet();
538 
539 	ifp->if_output	 = if_nulloutput;
540 	ifp->if_input	 = if_nullinput;
541 	ifp->if_start	 = if_nullstart;
542 	ifp->if_ioctl	 = if_nullioctl;
543 	ifp->if_init	 = if_nullinit;
544 	ifp->if_stop	 = if_nullstop;
545 	ifp->if_watchdog = if_nullwatchdog;
546 	ifp->if_drain	 = if_nulldrain;
547 
548 	/* No more packets may be enqueued. */
549 	ifp->if_snd.ifq_maxlen = 0;
550 
551 	splx(s);
552 }
553 
554 void
555 if_purgeaddrs(struct ifnet *ifp, int family, void (*purgeaddr)(struct ifaddr *))
556 {
557 	struct ifaddr *ifa, *nifa;
558 
559 	for (ifa = IFADDR_FIRST(ifp); ifa != NULL; ifa = nifa) {
560 		nifa = IFADDR_NEXT(ifa);
561 		if (ifa->ifa_addr->sa_family != family)
562 			continue;
563 		(*purgeaddr)(ifa);
564 	}
565 }
566 
567 /*
568  * Detach an interface from the list of "active" interfaces,
569  * freeing any resources as we go along.
570  *
571  * NOTE: This routine must be called with a valid thread context,
572  * as it may block.
573  */
574 void
575 if_detach(struct ifnet *ifp)
576 {
577 	struct socket so;
578 	struct ifaddr *ifa;
579 #ifdef IFAREF_DEBUG
580 	struct ifaddr *last_ifa = NULL;
581 #endif
582 	struct domain *dp;
583 	const struct protosw *pr;
584 	int s, i, family, purged;
585 
586 	/*
587 	 * XXX It's kind of lame that we have to have the
588 	 * XXX socket structure...
589 	 */
590 	memset(&so, 0, sizeof(so));
591 
592 	s = splnet();
593 
594 	/*
595 	 * Do an if_down() to give protocols a chance to do something.
596 	 */
597 	if_down(ifp);
598 
599 #ifdef ALTQ
600 	if (ALTQ_IS_ENABLED(&ifp->if_snd))
601 		altq_disable(&ifp->if_snd);
602 	if (ALTQ_IS_ATTACHED(&ifp->if_snd))
603 		altq_detach(&ifp->if_snd);
604 #endif
605 
606 
607 #if NCARP > 0
608 	/* Remove the interface from any carp group it is a part of.  */
609 	if (ifp->if_carp != NULL && ifp->if_type != IFT_CARP)
610 		carp_ifdetach(ifp);
611 #endif
612 
613 	/*
614 	 * Rip all the addresses off the interface.  This should make
615 	 * all of the routes go away.
616 	 *
617 	 * pr_usrreq calls can remove an arbitrary number of ifaddrs
618 	 * from the list, including our "cursor", ifa.  For safety,
619 	 * and to honor the TAILQ abstraction, I just restart the
620 	 * loop after each removal.  Note that the loop will exit
621 	 * when all of the remaining ifaddrs belong to the AF_LINK
622 	 * family.  I am counting on the historical fact that at
623 	 * least one pr_usrreq in each address domain removes at
624 	 * least one ifaddr.
625 	 */
626 again:
627 	IFADDR_FOREACH(ifa, ifp) {
628 		family = ifa->ifa_addr->sa_family;
629 #ifdef IFAREF_DEBUG
630 		printf("if_detach: ifaddr %p, family %d, refcnt %d\n",
631 		    ifa, family, ifa->ifa_refcnt);
632 		if (last_ifa != NULL && ifa == last_ifa)
633 			panic("if_detach: loop detected");
634 		last_ifa = ifa;
635 #endif
636 		if (family == AF_LINK)
637 			continue;
638 		dp = pffinddomain(family);
639 #ifdef DIAGNOSTIC
640 		if (dp == NULL)
641 			panic("if_detach: no domain for AF %d",
642 			    family);
643 #endif
644 		/*
645 		 * XXX These PURGEIF calls are redundant with the
646 		 * purge-all-families calls below, but are left in for
647 		 * now both to make a smaller change, and to avoid
648 		 * unplanned interactions with clearing of
649 		 * ifp->if_addrlist.
650 		 */
651 		purged = 0;
652 		for (pr = dp->dom_protosw;
653 		     pr < dp->dom_protoswNPROTOSW; pr++) {
654 			so.so_proto = pr;
655 			if (pr->pr_usrreq != NULL) {
656 				(void) (*pr->pr_usrreq)(&so,
657 				    PRU_PURGEIF, NULL, NULL,
658 				    (struct mbuf *) ifp, curlwp);
659 				purged = 1;
660 			}
661 		}
662 		if (purged == 0) {
663 			/*
664 			 * XXX What's really the best thing to do
665 			 * XXX here?  --thorpej@NetBSD.org
666 			 */
667 			printf("if_detach: WARNING: AF %d not purged\n",
668 			    family);
669 			ifa_remove(ifp, ifa);
670 		}
671 		goto again;
672 	}
673 
674 	if_free_sadl(ifp);
675 
676 	/* Walk the routing table looking for stragglers. */
677 	for (i = 0; i <= AF_MAX; i++)
678 		(void)rt_walktree(i, if_rt_walktree, ifp);
679 
680 	DOMAIN_FOREACH(dp) {
681 		if (dp->dom_ifdetach != NULL && ifp->if_afdata[dp->dom_family])
682 			(*dp->dom_ifdetach)(ifp,
683 			    ifp->if_afdata[dp->dom_family]);
684 
685 		/*
686 		 * One would expect multicast memberships (INET and
687 		 * INET6) on UDP sockets to be purged by the PURGEIF
688 		 * calls above, but if all addresses were removed from
689 		 * the interface prior to destruction, the calls will
690 		 * not be made (e.g. ppp, for which pppd(8) generally
691 		 * removes addresses before destroying the interface).
692 		 * Because there is no invariant that multicast
693 		 * memberships only exist for interfaces with IPv4
694 		 * addresses, we must call PURGEIF regardless of
695 		 * addresses.  (Protocols which might store ifnet
696 		 * pointers are marked with PR_PURGEIF.)
697 		 */
698 		for (pr = dp->dom_protosw; pr < dp->dom_protoswNPROTOSW; pr++) {
699 			so.so_proto = pr;
700 			if (pr->pr_usrreq != NULL && pr->pr_flags & PR_PURGEIF)
701 				(void)(*pr->pr_usrreq)(&so, PRU_PURGEIF, NULL,
702 				    NULL, (struct mbuf *)ifp, curlwp);
703 		}
704 	}
705 
706 #ifdef PFIL_HOOKS
707 	(void)pfil_run_hooks(&if_pfil,
708 	    (struct mbuf **)PFIL_IFNET_DETACH, ifp, PFIL_IFNET);
709 	(void)pfil_head_unregister(&ifp->if_pfil);
710 #endif
711 
712 	/* Announce that the interface is gone. */
713 	rt_ifannouncemsg(ifp, IFAN_DEPARTURE);
714 
715 	ifindex2ifnet[ifp->if_index] = NULL;
716 
717 	TAILQ_REMOVE(&ifnet, ifp, if_list);
718 
719 	/*
720 	 * remove packets that came from ifp, from software interrupt queues.
721 	 */
722 	DOMAIN_FOREACH(dp) {
723 		for (i = 0; i < __arraycount(dp->dom_ifqueues); i++) {
724 			if (dp->dom_ifqueues[i] == NULL)
725 				break;
726 			if_detach_queues(ifp, dp->dom_ifqueues[i]);
727 		}
728 	}
729 
730 	splx(s);
731 }
732 
733 static void
734 if_detach_queues(struct ifnet *ifp, struct ifqueue *q)
735 {
736 	struct mbuf *m, *prev, *next;
737 
738 	prev = NULL;
739 	for (m = q->ifq_head; m != NULL; m = next) {
740 		next = m->m_nextpkt;
741 #ifdef DIAGNOSTIC
742 		if ((m->m_flags & M_PKTHDR) == 0) {
743 			prev = m;
744 			continue;
745 		}
746 #endif
747 		if (m->m_pkthdr.rcvif != ifp) {
748 			prev = m;
749 			continue;
750 		}
751 
752 		if (prev != NULL)
753 			prev->m_nextpkt = m->m_nextpkt;
754 		else
755 			q->ifq_head = m->m_nextpkt;
756 		if (q->ifq_tail == m)
757 			q->ifq_tail = prev;
758 		q->ifq_len--;
759 
760 		m->m_nextpkt = NULL;
761 		m_freem(m);
762 		IF_DROP(q);
763 	}
764 }
765 
766 /*
767  * Callback for a radix tree walk to delete all references to an
768  * ifnet.
769  */
770 static int
771 if_rt_walktree(struct rtentry *rt, void *v)
772 {
773 	struct ifnet *ifp = (struct ifnet *)v;
774 	int error;
775 
776 	if (rt->rt_ifp != ifp)
777 		return 0;
778 
779 	/* Delete the entry. */
780 	++rt->rt_refcnt;
781 	error = rtrequest(RTM_DELETE, rt_getkey(rt), rt->rt_gateway,
782 	    rt_mask(rt), rt->rt_flags, NULL);
783 	KASSERT((rt->rt_flags & RTF_UP) == 0);
784 	rt->rt_ifp = NULL;
785 	RTFREE(rt);
786 	if (error != 0)
787 		printf("%s: warning: unable to delete rtentry @ %p, "
788 		    "error = %d\n", ifp->if_xname, rt, error);
789 	return 0;
790 }
791 
792 /*
793  * Create a clone network interface.
794  */
795 int
796 if_clone_create(const char *name)
797 {
798 	struct if_clone *ifc;
799 	int unit;
800 
801 	ifc = if_clone_lookup(name, &unit);
802 	if (ifc == NULL)
803 		return EINVAL;
804 
805 	if (ifunit(name) != NULL)
806 		return EEXIST;
807 
808 	return (*ifc->ifc_create)(ifc, unit);
809 }
810 
811 /*
812  * Destroy a clone network interface.
813  */
814 int
815 if_clone_destroy(const char *name)
816 {
817 	struct if_clone *ifc;
818 	struct ifnet *ifp;
819 
820 	ifc = if_clone_lookup(name, NULL);
821 	if (ifc == NULL)
822 		return EINVAL;
823 
824 	ifp = ifunit(name);
825 	if (ifp == NULL)
826 		return ENXIO;
827 
828 	if (ifc->ifc_destroy == NULL)
829 		return EOPNOTSUPP;
830 
831 	return (*ifc->ifc_destroy)(ifp);
832 }
833 
834 /*
835  * Look up a network interface cloner.
836  */
837 static struct if_clone *
838 if_clone_lookup(const char *name, int *unitp)
839 {
840 	struct if_clone *ifc;
841 	const char *cp;
842 	int unit;
843 
844 	/* separate interface name from unit */
845 	for (cp = name;
846 	    cp - name < IFNAMSIZ && *cp && (*cp < '0' || *cp > '9');
847 	    cp++)
848 		continue;
849 
850 	if (cp == name || cp - name == IFNAMSIZ || !*cp)
851 		return NULL;	/* No name or unit number */
852 
853 	LIST_FOREACH(ifc, &if_cloners, ifc_list) {
854 		if (strlen(ifc->ifc_name) == cp - name &&
855 		    strncmp(name, ifc->ifc_name, cp - name) == 0)
856 			break;
857 	}
858 
859 	if (ifc == NULL)
860 		return NULL;
861 
862 	unit = 0;
863 	while (cp - name < IFNAMSIZ && *cp) {
864 		if (*cp < '0' || *cp > '9' || unit > INT_MAX / 10) {
865 			/* Bogus unit number. */
866 			return NULL;
867 		}
868 		unit = (unit * 10) + (*cp++ - '0');
869 	}
870 
871 	if (unitp != NULL)
872 		*unitp = unit;
873 	return ifc;
874 }
875 
876 /*
877  * Register a network interface cloner.
878  */
879 void
880 if_clone_attach(struct if_clone *ifc)
881 {
882 
883 	LIST_INSERT_HEAD(&if_cloners, ifc, ifc_list);
884 	if_cloners_count++;
885 }
886 
887 /*
888  * Unregister a network interface cloner.
889  */
890 void
891 if_clone_detach(struct if_clone *ifc)
892 {
893 
894 	LIST_REMOVE(ifc, ifc_list);
895 	if_cloners_count--;
896 }
897 
898 /*
899  * Provide list of interface cloners to userspace.
900  */
901 static int
902 if_clone_list(struct if_clonereq *ifcr)
903 {
904 	char outbuf[IFNAMSIZ], *dst;
905 	struct if_clone *ifc;
906 	int count, error = 0;
907 
908 	ifcr->ifcr_total = if_cloners_count;
909 	if ((dst = ifcr->ifcr_buffer) == NULL) {
910 		/* Just asking how many there are. */
911 		return 0;
912 	}
913 
914 	if (ifcr->ifcr_count < 0)
915 		return EINVAL;
916 
917 	count = (if_cloners_count < ifcr->ifcr_count) ?
918 	    if_cloners_count : ifcr->ifcr_count;
919 
920 	for (ifc = LIST_FIRST(&if_cloners); ifc != NULL && count != 0;
921 	     ifc = LIST_NEXT(ifc, ifc_list), count--, dst += IFNAMSIZ) {
922 		(void)strncpy(outbuf, ifc->ifc_name, sizeof(outbuf));
923 		if (outbuf[sizeof(outbuf) - 1] != '\0')
924 			return ENAMETOOLONG;
925 		error = copyout(outbuf, dst, sizeof(outbuf));
926 		if (error != 0)
927 			break;
928 	}
929 
930 	return error;
931 }
932 
933 void
934 ifa_insert(struct ifnet *ifp, struct ifaddr *ifa)
935 {
936 	ifa->ifa_ifp = ifp;
937 	TAILQ_INSERT_TAIL(&ifp->if_addrlist, ifa, ifa_list);
938 	IFAREF(ifa);
939 }
940 
941 void
942 ifa_remove(struct ifnet *ifp, struct ifaddr *ifa)
943 {
944 	KASSERT(ifa->ifa_ifp == ifp);
945 	TAILQ_REMOVE(&ifp->if_addrlist, ifa, ifa_list);
946 	IFAFREE(ifa);
947 }
948 
949 static inline int
950 equal(const struct sockaddr *sa1, const struct sockaddr *sa2)
951 {
952 	return sockaddr_cmp(sa1, sa2) == 0;
953 }
954 
955 /*
956  * Locate an interface based on a complete address.
957  */
958 /*ARGSUSED*/
959 struct ifaddr *
960 ifa_ifwithaddr(const struct sockaddr *addr)
961 {
962 	struct ifnet *ifp;
963 	struct ifaddr *ifa;
964 
965 	IFNET_FOREACH(ifp) {
966 		if (ifp->if_output == if_nulloutput)
967 			continue;
968 		IFADDR_FOREACH(ifa, ifp) {
969 			if (ifa->ifa_addr->sa_family != addr->sa_family)
970 				continue;
971 			if (equal(addr, ifa->ifa_addr))
972 				return ifa;
973 			if ((ifp->if_flags & IFF_BROADCAST) &&
974 			    ifa->ifa_broadaddr &&
975 			    /* IP6 doesn't have broadcast */
976 			    ifa->ifa_broadaddr->sa_len != 0 &&
977 			    equal(ifa->ifa_broadaddr, addr))
978 				return ifa;
979 		}
980 	}
981 	return NULL;
982 }
983 
984 /*
985  * Locate the point to point interface with a given destination address.
986  */
987 /*ARGSUSED*/
988 struct ifaddr *
989 ifa_ifwithdstaddr(const struct sockaddr *addr)
990 {
991 	struct ifnet *ifp;
992 	struct ifaddr *ifa;
993 
994 	IFNET_FOREACH(ifp) {
995 		if (ifp->if_output == if_nulloutput)
996 			continue;
997 		if ((ifp->if_flags & IFF_POINTOPOINT) == 0)
998 			continue;
999 		IFADDR_FOREACH(ifa, ifp) {
1000 			if (ifa->ifa_addr->sa_family != addr->sa_family ||
1001 			    ifa->ifa_dstaddr == NULL)
1002 				continue;
1003 			if (equal(addr, ifa->ifa_dstaddr))
1004 				return ifa;
1005 		}
1006 	}
1007 	return NULL;
1008 }
1009 
1010 /*
1011  * Find an interface on a specific network.  If many, choice
1012  * is most specific found.
1013  */
1014 struct ifaddr *
1015 ifa_ifwithnet(const struct sockaddr *addr)
1016 {
1017 	struct ifnet *ifp;
1018 	struct ifaddr *ifa;
1019 	const struct sockaddr_dl *sdl;
1020 	struct ifaddr *ifa_maybe = 0;
1021 	u_int af = addr->sa_family;
1022 	const char *addr_data = addr->sa_data, *cplim;
1023 
1024 	if (af == AF_LINK) {
1025 		sdl = satocsdl(addr);
1026 		if (sdl->sdl_index && sdl->sdl_index < if_indexlim &&
1027 		    ifindex2ifnet[sdl->sdl_index] &&
1028 		    ifindex2ifnet[sdl->sdl_index]->if_output != if_nulloutput)
1029 			return ifnet_addrs[sdl->sdl_index];
1030 	}
1031 #ifdef NETATALK
1032 	if (af == AF_APPLETALK) {
1033 		const struct sockaddr_at *sat, *sat2;
1034 		sat = (const struct sockaddr_at *)addr;
1035 		IFNET_FOREACH(ifp) {
1036 			if (ifp->if_output == if_nulloutput)
1037 				continue;
1038 			ifa = at_ifawithnet((const struct sockaddr_at *)addr, ifp);
1039 			if (ifa == NULL)
1040 				continue;
1041 			sat2 = (struct sockaddr_at *)ifa->ifa_addr;
1042 			if (sat2->sat_addr.s_net == sat->sat_addr.s_net)
1043 				return ifa; /* exact match */
1044 			if (ifa_maybe == NULL) {
1045 				/* else keep the if with the right range */
1046 				ifa_maybe = ifa;
1047 			}
1048 		}
1049 		return ifa_maybe;
1050 	}
1051 #endif
1052 	IFNET_FOREACH(ifp) {
1053 		if (ifp->if_output == if_nulloutput)
1054 			continue;
1055 		IFADDR_FOREACH(ifa, ifp) {
1056 			const char *cp, *cp2, *cp3;
1057 
1058 			if (ifa->ifa_addr->sa_family != af ||
1059 			    ifa->ifa_netmask == NULL)
1060  next:				continue;
1061 			cp = addr_data;
1062 			cp2 = ifa->ifa_addr->sa_data;
1063 			cp3 = ifa->ifa_netmask->sa_data;
1064 			cplim = (const char *)ifa->ifa_netmask +
1065 			    ifa->ifa_netmask->sa_len;
1066 			while (cp3 < cplim) {
1067 				if ((*cp++ ^ *cp2++) & *cp3++) {
1068 					/* want to continue for() loop */
1069 					goto next;
1070 				}
1071 			}
1072 			if (ifa_maybe == NULL ||
1073 			    rn_refines((void *)ifa->ifa_netmask,
1074 			    (void *)ifa_maybe->ifa_netmask))
1075 				ifa_maybe = ifa;
1076 		}
1077 	}
1078 	return ifa_maybe;
1079 }
1080 
1081 /*
1082  * Find the interface of the addresss.
1083  */
1084 struct ifaddr *
1085 ifa_ifwithladdr(const struct sockaddr *addr)
1086 {
1087 	struct ifaddr *ia;
1088 
1089 	if ((ia = ifa_ifwithaddr(addr)) || (ia = ifa_ifwithdstaddr(addr)) ||
1090 	    (ia = ifa_ifwithnet(addr)))
1091 		return ia;
1092 	return NULL;
1093 }
1094 
1095 /*
1096  * Find an interface using a specific address family
1097  */
1098 struct ifaddr *
1099 ifa_ifwithaf(int af)
1100 {
1101 	struct ifnet *ifp;
1102 	struct ifaddr *ifa;
1103 
1104 	IFNET_FOREACH(ifp) {
1105 		if (ifp->if_output == if_nulloutput)
1106 			continue;
1107 		IFADDR_FOREACH(ifa, ifp) {
1108 			if (ifa->ifa_addr->sa_family == af)
1109 				return ifa;
1110 		}
1111 	}
1112 	return NULL;
1113 }
1114 
1115 /*
1116  * Find an interface address specific to an interface best matching
1117  * a given address.
1118  */
1119 struct ifaddr *
1120 ifaof_ifpforaddr(const struct sockaddr *addr, struct ifnet *ifp)
1121 {
1122 	struct ifaddr *ifa;
1123 	const char *cp, *cp2, *cp3;
1124 	const char *cplim;
1125 	struct ifaddr *ifa_maybe = 0;
1126 	u_int af = addr->sa_family;
1127 
1128 	if (ifp->if_output == if_nulloutput)
1129 		return NULL;
1130 
1131 	if (af >= AF_MAX)
1132 		return NULL;
1133 
1134 	IFADDR_FOREACH(ifa, ifp) {
1135 		if (ifa->ifa_addr->sa_family != af)
1136 			continue;
1137 		ifa_maybe = ifa;
1138 		if (ifa->ifa_netmask == NULL) {
1139 			if (equal(addr, ifa->ifa_addr) ||
1140 			    (ifa->ifa_dstaddr &&
1141 			     equal(addr, ifa->ifa_dstaddr)))
1142 				return ifa;
1143 			continue;
1144 		}
1145 		cp = addr->sa_data;
1146 		cp2 = ifa->ifa_addr->sa_data;
1147 		cp3 = ifa->ifa_netmask->sa_data;
1148 		cplim = ifa->ifa_netmask->sa_len + (char *)ifa->ifa_netmask;
1149 		for (; cp3 < cplim; cp3++) {
1150 			if ((*cp++ ^ *cp2++) & *cp3)
1151 				break;
1152 		}
1153 		if (cp3 == cplim)
1154 			return ifa;
1155 	}
1156 	return ifa_maybe;
1157 }
1158 
1159 /*
1160  * Default action when installing a route with a Link Level gateway.
1161  * Lookup an appropriate real ifa to point to.
1162  * This should be moved to /sys/net/link.c eventually.
1163  */
1164 void
1165 link_rtrequest(int cmd, struct rtentry *rt, struct rt_addrinfo *info)
1166 {
1167 	struct ifaddr *ifa;
1168 	const struct sockaddr *dst;
1169 	struct ifnet *ifp;
1170 
1171 	if (cmd != RTM_ADD || ((ifa = rt->rt_ifa) == NULL) ||
1172 	    ((ifp = ifa->ifa_ifp) == NULL) || ((dst = rt_getkey(rt)) == NULL))
1173 		return;
1174 	if ((ifa = ifaof_ifpforaddr(dst, ifp)) != NULL) {
1175 		rt_replace_ifa(rt, ifa);
1176 		if (ifa->ifa_rtrequest && ifa->ifa_rtrequest != link_rtrequest)
1177 			ifa->ifa_rtrequest(cmd, rt, info);
1178 	}
1179 }
1180 
1181 /*
1182  * Handle a change in the interface link state.
1183  */
1184 void
1185 if_link_state_change(struct ifnet *ifp, int link_state)
1186 {
1187 	if (ifp->if_link_state == link_state)
1188 		return;
1189 	ifp->if_link_state = link_state;
1190 	/* Notify that the link state has changed. */
1191 	rt_ifmsg(ifp);
1192 #if NCARP > 0
1193 	if (ifp->if_carp)
1194 		carp_carpdev_state(ifp);
1195 #endif
1196 }
1197 
1198 /*
1199  * Mark an interface down and notify protocols of
1200  * the transition.
1201  * NOTE: must be called at splsoftnet or equivalent.
1202  */
1203 void
1204 if_down(struct ifnet *ifp)
1205 {
1206 	struct ifaddr *ifa;
1207 
1208 	ifp->if_flags &= ~IFF_UP;
1209 	microtime(&ifp->if_lastchange);
1210 	IFADDR_FOREACH(ifa, ifp)
1211 		pfctlinput(PRC_IFDOWN, ifa->ifa_addr);
1212 	IFQ_PURGE(&ifp->if_snd);
1213 #if NCARP > 0
1214 	if (ifp->if_carp)
1215 		carp_carpdev_state(ifp);
1216 #endif
1217 	rt_ifmsg(ifp);
1218 }
1219 
1220 /*
1221  * Mark an interface up and notify protocols of
1222  * the transition.
1223  * NOTE: must be called at splsoftnet or equivalent.
1224  */
1225 void
1226 if_up(struct ifnet *ifp)
1227 {
1228 #ifdef notyet
1229 	struct ifaddr *ifa;
1230 #endif
1231 
1232 	ifp->if_flags |= IFF_UP;
1233 	microtime(&ifp->if_lastchange);
1234 #ifdef notyet
1235 	/* this has no effect on IP, and will kill all ISO connections XXX */
1236 	IFADDR_FOREACH(ifa, ifp)
1237 		pfctlinput(PRC_IFUP, ifa->ifa_addr);
1238 #endif
1239 #if NCARP > 0
1240 	if (ifp->if_carp)
1241 		carp_carpdev_state(ifp);
1242 #endif
1243 	rt_ifmsg(ifp);
1244 #ifdef INET6
1245 	in6_if_up(ifp);
1246 #endif
1247 }
1248 
1249 /*
1250  * Handle interface watchdog timer routines.  Called
1251  * from softclock, we decrement timers (if set) and
1252  * call the appropriate interface routine on expiration.
1253  */
1254 void
1255 if_slowtimo(void *arg)
1256 {
1257 	struct ifnet *ifp;
1258 	int s = splnet();
1259 
1260 	IFNET_FOREACH(ifp) {
1261 		if (ifp->if_timer == 0 || --ifp->if_timer)
1262 			continue;
1263 		if (ifp->if_watchdog != NULL)
1264 			(*ifp->if_watchdog)(ifp);
1265 	}
1266 	splx(s);
1267 	callout_reset(&if_slowtimo_ch, hz / IFNET_SLOWHZ, if_slowtimo, NULL);
1268 }
1269 
1270 /*
1271  * Set/clear promiscuous mode on interface ifp based on the truth value
1272  * of pswitch.  The calls are reference counted so that only the first
1273  * "on" request actually has an effect, as does the final "off" request.
1274  * Results are undefined if the "off" and "on" requests are not matched.
1275  */
1276 int
1277 ifpromisc(struct ifnet *ifp, int pswitch)
1278 {
1279 	int pcount, ret;
1280 	short flags;
1281 	struct ifreq ifr;
1282 
1283 	pcount = ifp->if_pcount;
1284 	flags = ifp->if_flags;
1285 	if (pswitch) {
1286 		/*
1287 		 * Allow the device to be "placed" into promiscuous
1288 		 * mode even if it is not configured up.  It will
1289 		 * consult IFF_PROMISC when it is is brought up.
1290 		 */
1291 		if (ifp->if_pcount++ != 0)
1292 			return 0;
1293 		ifp->if_flags |= IFF_PROMISC;
1294 		if ((ifp->if_flags & IFF_UP) == 0)
1295 			return 0;
1296 	} else {
1297 		if (--ifp->if_pcount > 0)
1298 			return 0;
1299 		ifp->if_flags &= ~IFF_PROMISC;
1300 		/*
1301 		 * If the device is not configured up, we should not need to
1302 		 * turn off promiscuous mode (device should have turned it
1303 		 * off when interface went down; and will look at IFF_PROMISC
1304 		 * again next time interface comes up).
1305 		 */
1306 		if ((ifp->if_flags & IFF_UP) == 0)
1307 			return 0;
1308 	}
1309 	memset(&ifr, 0, sizeof(ifr));
1310 	ifr.ifr_flags = ifp->if_flags;
1311 	ret = (*ifp->if_ioctl)(ifp, SIOCSIFFLAGS, (void *) &ifr);
1312 	/* Restore interface state if not successful. */
1313 	if (ret != 0) {
1314 		ifp->if_pcount = pcount;
1315 		ifp->if_flags = flags;
1316 	}
1317 	return ret;
1318 }
1319 
1320 /*
1321  * Map interface name to
1322  * interface structure pointer.
1323  */
1324 struct ifnet *
1325 ifunit(const char *name)
1326 {
1327 	struct ifnet *ifp;
1328 	const char *cp = name;
1329 	u_int unit = 0;
1330 	u_int i;
1331 
1332 	/*
1333 	 * If the entire name is a number, treat it as an ifindex.
1334 	 */
1335 	for (i = 0; i < IFNAMSIZ && *cp >= '0' && *cp <= '9'; i++, cp++) {
1336 		unit = unit * 10 + (*cp - '0');
1337 	}
1338 
1339 	/*
1340 	 * If the number took all of the name, then it's a valid ifindex.
1341 	 */
1342 	if (i == IFNAMSIZ || (cp != name && *cp == '\0')) {
1343 		if (unit >= if_indexlim)
1344 			return NULL;
1345 		ifp = ifindex2ifnet[unit];
1346 		if (ifp == NULL || ifp->if_output == if_nulloutput)
1347 			return NULL;
1348 		return ifp;
1349 	}
1350 
1351 	IFNET_FOREACH(ifp) {
1352 		if (ifp->if_output == if_nulloutput)
1353 			continue;
1354 	 	if (strcmp(ifp->if_xname, name) == 0)
1355 			return ifp;
1356 	}
1357 	return NULL;
1358 }
1359 
1360 /* common */
1361 int
1362 ifioctl_common(struct ifnet *ifp, u_long cmd, void *data)
1363 {
1364 	int s;
1365 	struct ifreq *ifr;
1366 	struct ifcapreq *ifcr;
1367 	struct ifdatareq *ifdr;
1368 
1369 	switch (cmd) {
1370 	case SIOCSIFCAP:
1371 		ifcr = data;
1372 		if ((ifcr->ifcr_capenable & ~ifp->if_capabilities) != 0)
1373 			return EINVAL;
1374 
1375 		if (ifcr->ifcr_capenable == ifp->if_capenable)
1376 			return 0;
1377 
1378 		ifp->if_capenable = ifcr->ifcr_capenable;
1379 
1380 		/* Pre-compute the checksum flags mask. */
1381 		ifp->if_csum_flags_tx = 0;
1382 		ifp->if_csum_flags_rx = 0;
1383 		if (ifp->if_capenable & IFCAP_CSUM_IPv4_Tx) {
1384 			ifp->if_csum_flags_tx |= M_CSUM_IPv4;
1385 		}
1386 		if (ifp->if_capenable & IFCAP_CSUM_IPv4_Rx) {
1387 			ifp->if_csum_flags_rx |= M_CSUM_IPv4;
1388 		}
1389 
1390 		if (ifp->if_capenable & IFCAP_CSUM_TCPv4_Tx) {
1391 			ifp->if_csum_flags_tx |= M_CSUM_TCPv4;
1392 		}
1393 		if (ifp->if_capenable & IFCAP_CSUM_TCPv4_Rx) {
1394 			ifp->if_csum_flags_rx |= M_CSUM_TCPv4;
1395 		}
1396 
1397 		if (ifp->if_capenable & IFCAP_CSUM_UDPv4_Tx) {
1398 			ifp->if_csum_flags_tx |= M_CSUM_UDPv4;
1399 		}
1400 		if (ifp->if_capenable & IFCAP_CSUM_UDPv4_Rx) {
1401 			ifp->if_csum_flags_rx |= M_CSUM_UDPv4;
1402 		}
1403 
1404 		if (ifp->if_capenable & IFCAP_CSUM_TCPv6_Tx) {
1405 			ifp->if_csum_flags_tx |= M_CSUM_TCPv6;
1406 		}
1407 		if (ifp->if_capenable & IFCAP_CSUM_TCPv6_Rx) {
1408 			ifp->if_csum_flags_rx |= M_CSUM_TCPv6;
1409 		}
1410 
1411 		if (ifp->if_capenable & IFCAP_CSUM_UDPv6_Tx) {
1412 			ifp->if_csum_flags_tx |= M_CSUM_UDPv6;
1413 		}
1414 		if (ifp->if_capenable & IFCAP_CSUM_UDPv6_Rx) {
1415 			ifp->if_csum_flags_rx |= M_CSUM_UDPv6;
1416 		}
1417 		if (ifp->if_flags & IFF_UP)
1418 			return ENETRESET;
1419 		return 0;
1420 	case SIOCSIFFLAGS:
1421 		ifr = data;
1422 		if (ifp->if_flags & IFF_UP && (ifr->ifr_flags & IFF_UP) == 0) {
1423 			s = splnet();
1424 			if_down(ifp);
1425 			splx(s);
1426 		}
1427 		if (ifr->ifr_flags & IFF_UP && (ifp->if_flags & IFF_UP) == 0) {
1428 			s = splnet();
1429 			if_up(ifp);
1430 			splx(s);
1431 		}
1432 		ifp->if_flags = (ifp->if_flags & IFF_CANTCHANGE) |
1433 			(ifr->ifr_flags &~ IFF_CANTCHANGE);
1434 		break;
1435 	case SIOCGIFFLAGS:
1436 		ifr = data;
1437 		ifr->ifr_flags = ifp->if_flags;
1438 		break;
1439 
1440 	case SIOCGIFMETRIC:
1441 		ifr = data;
1442 		ifr->ifr_metric = ifp->if_metric;
1443 		break;
1444 
1445 	case SIOCGIFMTU:
1446 		ifr = data;
1447 		ifr->ifr_mtu = ifp->if_mtu;
1448 		break;
1449 
1450 	case SIOCGIFDLT:
1451 		ifr = data;
1452 		ifr->ifr_dlt = ifp->if_dlt;
1453 		break;
1454 
1455 	case SIOCGIFCAP:
1456 		ifcr = data;
1457 		ifcr->ifcr_capabilities = ifp->if_capabilities;
1458 		ifcr->ifcr_capenable = ifp->if_capenable;
1459 		break;
1460 
1461 	case SIOCSIFMETRIC:
1462 		ifr = data;
1463 		ifp->if_metric = ifr->ifr_metric;
1464 		break;
1465 
1466 	case SIOCGIFDATA:
1467 		ifdr = data;
1468 		ifdr->ifdr_data = ifp->if_data;
1469 		break;
1470 
1471 	case SIOCZIFDATA:
1472 		ifdr = data;
1473 		ifdr->ifdr_data = ifp->if_data;
1474 		/*
1475 		 * Assumes that the volatile counters that can be
1476 		 * zero'ed are at the end of if_data.
1477 		 */
1478 		memset(&ifp->if_data.ifi_ipackets, 0, sizeof(ifp->if_data) -
1479 		    offsetof(struct if_data, ifi_ipackets));
1480 		break;
1481 	case SIOCSIFMTU:
1482 		ifr = data;
1483 		if (ifp->if_mtu == ifr->ifr_mtu)
1484 			break;
1485 		ifp->if_mtu = ifr->ifr_mtu;
1486 		/*
1487 		 * If the link MTU changed, do network layer specific procedure.
1488 		 */
1489 #ifdef INET6
1490 		nd6_setmtu(ifp);
1491 #endif
1492 		return ENETRESET;
1493 	default:
1494 		return EOPNOTSUPP;
1495 	}
1496 	return 0;
1497 }
1498 
1499 /*
1500  * Interface ioctls.
1501  */
1502 int
1503 ifioctl(struct socket *so, u_long cmd, void *data, struct lwp *l)
1504 {
1505 	struct ifnet *ifp;
1506 	struct ifreq *ifr;
1507 	struct ifcapreq *ifcr;
1508 	struct ifdatareq *ifdr;
1509 	int error = 0;
1510 #if defined(COMPAT_OSOCK) || defined(COMPAT_OIFREQ)
1511 	u_long ocmd = cmd;
1512 #endif
1513 	short oif_flags;
1514 #ifdef COMPAT_OIFREQ
1515 	struct ifreq ifrb;
1516 	struct oifreq *oifr = NULL;
1517 #endif
1518 
1519 	switch (cmd) {
1520 #ifdef COMPAT_OIFREQ
1521 	case OSIOCGIFCONF:
1522 	case OOSIOCGIFCONF:
1523 		return compat_ifconf(cmd, data);
1524 #endif
1525 	case SIOCGIFCONF:
1526 		return ifconf(cmd, data);
1527 	}
1528 
1529 #ifdef COMPAT_OIFREQ
1530 	cmd = compat_cvtcmd(cmd);
1531 	if (cmd != ocmd) {
1532 		oifr = data;
1533 		data = ifr = &ifrb;
1534 		ifreqo2n(oifr, ifr);
1535 	} else
1536 #endif
1537 		ifr = data;
1538 	ifcr = data;
1539 	ifdr = data;
1540 
1541 	ifp = ifunit(ifr->ifr_name);
1542 
1543 	switch (cmd) {
1544 	case SIOCIFCREATE:
1545 	case SIOCIFDESTROY:
1546 		if (l != NULL) {
1547 			error = kauth_authorize_network(l->l_cred,
1548 			    KAUTH_NETWORK_INTERFACE,
1549 			    KAUTH_REQ_NETWORK_INTERFACE_SETPRIV, ifp,
1550 			    (void *)cmd, NULL);
1551 			if (error != 0)
1552 				return error;
1553 		}
1554 		return (cmd == SIOCIFCREATE) ?
1555 			if_clone_create(ifr->ifr_name) :
1556 			if_clone_destroy(ifr->ifr_name);
1557 
1558 	case SIOCIFGCLONERS:
1559 		return if_clone_list((struct if_clonereq *)data);
1560 	}
1561 
1562 	if (ifp == NULL)
1563 		return ENXIO;
1564 
1565 	switch (cmd) {
1566 	case SIOCSIFFLAGS:
1567 	case SIOCSIFCAP:
1568 	case SIOCSIFMETRIC:
1569 	case SIOCZIFDATA:
1570 	case SIOCSIFMTU:
1571 	case SIOCSIFPHYADDR:
1572 	case SIOCDIFPHYADDR:
1573 #ifdef INET6
1574 	case SIOCSIFPHYADDR_IN6:
1575 #endif
1576 	case SIOCSLIFPHYADDR:
1577 	case SIOCADDMULTI:
1578 	case SIOCDELMULTI:
1579 	case SIOCSIFMEDIA:
1580 	case SIOCSDRVSPEC:
1581 	case SIOCG80211:
1582 	case SIOCS80211:
1583 	case SIOCS80211NWID:
1584 	case SIOCS80211NWKEY:
1585 	case SIOCS80211POWER:
1586 	case SIOCS80211BSSID:
1587 	case SIOCS80211CHANNEL:
1588 		if (l != NULL) {
1589 			error = kauth_authorize_network(l->l_cred,
1590 			    KAUTH_NETWORK_INTERFACE,
1591 			    KAUTH_REQ_NETWORK_INTERFACE_SETPRIV, ifp,
1592 			    (void *)cmd, NULL);
1593 			if (error != 0)
1594 				return error;
1595 		}
1596 	}
1597 
1598 	oif_flags = ifp->if_flags;
1599 	switch (cmd) {
1600 
1601 	case SIOCSIFFLAGS:
1602 		ifioctl_common(ifp, cmd, data);
1603 		if (ifp->if_ioctl)
1604 			(void)(*ifp->if_ioctl)(ifp, cmd, data);
1605 		break;
1606 
1607 	case SIOCSIFPHYADDR:
1608 	case SIOCDIFPHYADDR:
1609 #ifdef INET6
1610 	case SIOCSIFPHYADDR_IN6:
1611 #endif
1612 	case SIOCSLIFPHYADDR:
1613 	case SIOCADDMULTI:
1614 	case SIOCDELMULTI:
1615 	case SIOCSIFMEDIA:
1616 	case SIOCGIFPSRCADDR:
1617 	case SIOCGIFPDSTADDR:
1618 	case SIOCGLIFPHYADDR:
1619 	case SIOCGIFMEDIA:
1620 	case SIOCG80211:
1621 	case SIOCS80211:
1622 	case SIOCS80211NWID:
1623 	case SIOCS80211NWKEY:
1624 	case SIOCS80211POWER:
1625 	case SIOCS80211BSSID:
1626 	case SIOCS80211CHANNEL:
1627 	case SIOCSIFCAP:
1628 	case SIOCSIFMTU:
1629 		if (ifp->if_ioctl == NULL)
1630 			return EOPNOTSUPP;
1631 		error = (*ifp->if_ioctl)(ifp, cmd, data);
1632 		break;
1633 
1634 	default:
1635 		error = ifioctl_common(ifp, cmd, data);
1636 		if (error != EOPNOTSUPP)
1637 			break;
1638 		if (so->so_proto == NULL)
1639 			return EOPNOTSUPP;
1640 		solock(so);
1641 #ifdef COMPAT_OSOCK
1642 		error = compat_ifioctl(so, ocmd, cmd, data, l);
1643 #else
1644 		error = (*so->so_proto->pr_usrreq)(so, PRU_CONTROL,
1645 		    (struct mbuf *)cmd, (struct mbuf *)data,
1646 		    (struct mbuf *)ifp, l);
1647 #endif
1648 		sounlock(so);
1649 		break;
1650 	}
1651 
1652 	if (((oif_flags ^ ifp->if_flags) & IFF_UP) != 0) {
1653 #ifdef INET6
1654 		if ((ifp->if_flags & IFF_UP) != 0) {
1655 			int s = splnet();
1656 			in6_if_up(ifp);
1657 			splx(s);
1658 		}
1659 #endif
1660 	}
1661 #ifdef COMPAT_OIFREQ
1662 	if (cmd != ocmd)
1663 		ifreqn2o(oifr, ifr);
1664 #endif
1665 
1666 	return error;
1667 }
1668 
1669 /*
1670  * Return interface configuration
1671  * of system.  List may be used
1672  * in later ioctl's (above) to get
1673  * other information.
1674  *
1675  * Each record is a struct ifreq.  Before the addition of
1676  * sockaddr_storage, the API rule was that sockaddr flavors that did
1677  * not fit would extend beyond the struct ifreq, with the next struct
1678  * ifreq starting sa_len beyond the struct sockaddr.  Because the
1679  * union in struct ifreq includes struct sockaddr_storage, every kind
1680  * of sockaddr must fit.  Thus, there are no longer any overlength
1681  * records.
1682  *
1683  * Records are added to the user buffer if they fit, and ifc_len is
1684  * adjusted to the length that was written.  Thus, the user is only
1685  * assured of getting the complete list if ifc_len on return is at
1686  * least sizeof(struct ifreq) less than it was on entry.
1687  *
1688  * If the user buffer pointer is NULL, this routine copies no data and
1689  * returns the amount of space that would be needed.
1690  *
1691  * Invariants:
1692  * ifrp points to the next part of the user's buffer to be used.  If
1693  * ifrp != NULL, space holds the number of bytes remaining that we may
1694  * write at ifrp.  Otherwise, space holds the number of bytes that
1695  * would have been written had there been adequate space.
1696  */
1697 /*ARGSUSED*/
1698 int
1699 ifconf(u_long cmd, void *data)
1700 {
1701 	struct ifconf *ifc = (struct ifconf *)data;
1702 	struct ifnet *ifp;
1703 	struct ifaddr *ifa;
1704 	struct ifreq ifr, *ifrp;
1705 	int space, error = 0;
1706 	const int sz = (int)sizeof(struct ifreq);
1707 
1708 	if ((ifrp = ifc->ifc_req) == NULL)
1709 		space = 0;
1710 	else
1711 		space = ifc->ifc_len;
1712 	IFNET_FOREACH(ifp) {
1713 		(void)strncpy(ifr.ifr_name, ifp->if_xname,
1714 		    sizeof(ifr.ifr_name));
1715 		if (ifr.ifr_name[sizeof(ifr.ifr_name) - 1] != '\0')
1716 			return ENAMETOOLONG;
1717 		if (IFADDR_EMPTY(ifp)) {
1718 			/* Interface with no addresses - send zero sockaddr. */
1719 			memset(&ifr.ifr_addr, 0, sizeof(ifr.ifr_addr));
1720 			if (ifrp == NULL) {
1721 				space += sz;
1722 				continue;
1723 			}
1724 			if (space >= sz) {
1725 				error = copyout(&ifr, ifrp, sz);
1726 				if (error != 0)
1727 					return error;
1728 				ifrp++;
1729 				space -= sz;
1730 			}
1731 		}
1732 
1733 		IFADDR_FOREACH(ifa, ifp) {
1734 			struct sockaddr *sa = ifa->ifa_addr;
1735 			/* all sockaddrs must fit in sockaddr_storage */
1736 			KASSERT(sa->sa_len <= sizeof(ifr.ifr_ifru));
1737 
1738 			if (ifrp == NULL) {
1739 				space += sz;
1740 				continue;
1741 			}
1742 			memcpy(&ifr.ifr_space, sa, sa->sa_len);
1743 			if (space >= sz) {
1744 				error = copyout(&ifr, ifrp, sz);
1745 				if (error != 0)
1746 					return (error);
1747 				ifrp++; space -= sz;
1748 			}
1749 		}
1750 	}
1751 	if (ifrp != NULL) {
1752 		KASSERT(0 <= space && space <= ifc->ifc_len);
1753 		ifc->ifc_len -= space;
1754 	} else {
1755 		KASSERT(space >= 0);
1756 		ifc->ifc_len = space;
1757 	}
1758 	return (0);
1759 }
1760 
1761 int
1762 ifreq_setaddr(const u_long cmd, struct ifreq *ifr, const struct sockaddr *sa)
1763 {
1764 	uint8_t len;
1765 	u_long ncmd;
1766 	const uint8_t osockspace = sizeof(ifr->ifr_addr);
1767 	const uint8_t sockspace = sizeof(ifr->ifr_ifru.ifru_space);
1768 
1769 #ifdef INET6
1770 	if (cmd == SIOCGIFPSRCADDR_IN6 || cmd == SIOCGIFPDSTADDR_IN6)
1771 		len = MIN(sizeof(struct sockaddr_in6), sa->sa_len);
1772 	else
1773 #endif /* INET6 */
1774 	if ((ncmd = compat_cvtcmd(cmd)) != cmd)
1775 		len = MIN(osockspace, sa->sa_len);
1776 	else
1777 		len = MIN(sockspace, sa->sa_len);
1778 	if (len < sa->sa_len)
1779 		return EFBIG;
1780 	sockaddr_copy(&ifr->ifr_addr, len, sa);
1781 	return 0;
1782 }
1783 
1784 /*
1785  * Queue message on interface, and start output if interface
1786  * not yet active.
1787  */
1788 int
1789 ifq_enqueue(struct ifnet *ifp, struct mbuf *m
1790     ALTQ_COMMA ALTQ_DECL(struct altq_pktattr *pktattr))
1791 {
1792 	int len = m->m_pkthdr.len;
1793 	int mflags = m->m_flags;
1794 	int s = splnet();
1795 	int error;
1796 
1797 	IFQ_ENQUEUE(&ifp->if_snd, m, pktattr, error);
1798 	if (error != 0)
1799 		goto out;
1800 	ifp->if_obytes += len;
1801 	if (mflags & M_MCAST)
1802 		ifp->if_omcasts++;
1803 	if ((ifp->if_flags & IFF_OACTIVE) == 0)
1804 		(*ifp->if_start)(ifp);
1805 out:
1806 	splx(s);
1807 	return error;
1808 }
1809 
1810 /*
1811  * Queue message on interface, possibly using a second fast queue
1812  */
1813 int
1814 ifq_enqueue2(struct ifnet *ifp, struct ifqueue *ifq, struct mbuf *m
1815     ALTQ_COMMA ALTQ_DECL(struct altq_pktattr *pktattr))
1816 {
1817 	int error = 0;
1818 
1819 	if (ifq != NULL
1820 #ifdef ALTQ
1821 	    && ALTQ_IS_ENABLED(&ifp->if_snd) == 0
1822 #endif
1823 	    ) {
1824 		if (IF_QFULL(ifq)) {
1825 			IF_DROP(&ifp->if_snd);
1826 			m_freem(m);
1827 			if (error == 0)
1828 				error = ENOBUFS;
1829 		} else
1830 			IF_ENQUEUE(ifq, m);
1831 	} else
1832 		IFQ_ENQUEUE(&ifp->if_snd, m, pktattr, error);
1833 	if (error != 0) {
1834 		++ifp->if_oerrors;
1835 		return error;
1836 	}
1837 	return 0;
1838 }
1839 
1840 
1841 #if defined(INET) || defined(INET6)
1842 static void
1843 sysctl_net_ifq_setup(struct sysctllog **clog,
1844 		     int pf, const char *pfname,
1845 		     int ipn, const char *ipname,
1846 		     int qid, struct ifqueue *ifq)
1847 {
1848 
1849 	sysctl_createv(clog, 0, NULL, NULL,
1850 		       CTLFLAG_PERMANENT,
1851 		       CTLTYPE_NODE, "net", NULL,
1852 		       NULL, 0, NULL, 0,
1853 		       CTL_NET, CTL_EOL);
1854 	sysctl_createv(clog, 0, NULL, NULL,
1855 		       CTLFLAG_PERMANENT,
1856 		       CTLTYPE_NODE, pfname, NULL,
1857 		       NULL, 0, NULL, 0,
1858 		       CTL_NET, pf, CTL_EOL);
1859 	sysctl_createv(clog, 0, NULL, NULL,
1860 		       CTLFLAG_PERMANENT,
1861 		       CTLTYPE_NODE, ipname, NULL,
1862 		       NULL, 0, NULL, 0,
1863 		       CTL_NET, pf, ipn, CTL_EOL);
1864 	sysctl_createv(clog, 0, NULL, NULL,
1865 		       CTLFLAG_PERMANENT,
1866 		       CTLTYPE_NODE, "ifq",
1867 		       SYSCTL_DESCR("Protocol input queue controls"),
1868 		       NULL, 0, NULL, 0,
1869 		       CTL_NET, pf, ipn, qid, CTL_EOL);
1870 
1871 	sysctl_createv(clog, 0, NULL, NULL,
1872 		       CTLFLAG_PERMANENT,
1873 		       CTLTYPE_INT, "len",
1874 		       SYSCTL_DESCR("Current input queue length"),
1875 		       NULL, 0, &ifq->ifq_len, 0,
1876 		       CTL_NET, pf, ipn, qid, IFQCTL_LEN, CTL_EOL);
1877 	sysctl_createv(clog, 0, NULL, NULL,
1878 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1879 		       CTLTYPE_INT, "maxlen",
1880 		       SYSCTL_DESCR("Maximum allowed input queue length"),
1881 		       NULL, 0, &ifq->ifq_maxlen, 0,
1882 		       CTL_NET, pf, ipn, qid, IFQCTL_MAXLEN, CTL_EOL);
1883 #ifdef notyet
1884 	sysctl_createv(clog, 0, NULL, NULL,
1885 		       CTLFLAG_PERMANENT,
1886 		       CTLTYPE_INT, "peak",
1887 		       SYSCTL_DESCR("Highest input queue length"),
1888 		       NULL, 0, &ifq->ifq_peak, 0,
1889 		       CTL_NET, pf, ipn, qid, IFQCTL_PEAK, CTL_EOL);
1890 #endif
1891 	sysctl_createv(clog, 0, NULL, NULL,
1892 		       CTLFLAG_PERMANENT,
1893 		       CTLTYPE_INT, "drops",
1894 		       SYSCTL_DESCR("Packets dropped due to full input queue"),
1895 		       NULL, 0, &ifq->ifq_drops, 0,
1896 		       CTL_NET, pf, ipn, qid, IFQCTL_DROPS, CTL_EOL);
1897 }
1898 
1899 #ifdef INET
1900 SYSCTL_SETUP(sysctl_net_inet_ip_ifq_setup,
1901 	     "sysctl net.inet.ip.ifq subtree setup")
1902 {
1903 	extern struct ifqueue ipintrq;
1904 
1905 	sysctl_net_ifq_setup(clog, PF_INET, "inet", IPPROTO_IP, "ip",
1906 			     IPCTL_IFQ, &ipintrq);
1907 }
1908 #endif /* INET */
1909 
1910 #ifdef INET6
1911 SYSCTL_SETUP(sysctl_net_inet6_ip6_ifq_setup,
1912 	     "sysctl net.inet6.ip6.ifq subtree setup")
1913 {
1914 	extern struct ifqueue ip6intrq;
1915 
1916 	sysctl_net_ifq_setup(clog, PF_INET6, "inet6", IPPROTO_IPV6, "ip6",
1917 			     IPV6CTL_IFQ, &ip6intrq);
1918 }
1919 #endif /* INET6 */
1920 #endif /* INET || INET6 */
1921