No empty .Rs/.Re
[netbsd-mini2440.git] / sys / net / if.c
blobdf33428de626fdeec8df8c8ec2c47db9db3837cf
1 /* $NetBSD: if.c,v 1.240 2009/10/26 16:41:35 cegger Exp $ */
3 /*-
4 * Copyright (c) 1999, 2000, 2001, 2008 The NetBSD Foundation, Inc.
5 * All rights reserved.
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by William Studenmund and Jason R. Thorpe.
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.
19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
33 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
34 * All rights reserved.
36 * Redistribution and use in source and binary forms, with or without
37 * modification, are permitted provided that the following conditions
38 * are met:
39 * 1. Redistributions of source code must retain the above copyright
40 * notice, this list of conditions and the following disclaimer.
41 * 2. Redistributions in binary form must reproduce the above copyright
42 * notice, this list of conditions and the following disclaimer in the
43 * documentation and/or other materials provided with the distribution.
44 * 3. Neither the name of the project nor the names of its contributors
45 * may be used to endorse or promote products derived from this software
46 * without specific prior written permission.
48 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
49 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
50 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
51 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
52 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
53 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
54 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
55 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
56 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
57 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
58 * SUCH DAMAGE.
62 * Copyright (c) 1980, 1986, 1993
63 * The Regents of the University of California. All rights reserved.
65 * Redistribution and use in source and binary forms, with or without
66 * modification, are permitted provided that the following conditions
67 * are met:
68 * 1. Redistributions of source code must retain the above copyright
69 * notice, this list of conditions and the following disclaimer.
70 * 2. Redistributions in binary form must reproduce the above copyright
71 * notice, this list of conditions and the following disclaimer in the
72 * documentation and/or other materials provided with the distribution.
73 * 3. Neither the name of the University nor the names of its contributors
74 * may be used to endorse or promote products derived from this software
75 * without specific prior written permission.
77 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
78 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
79 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
80 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
81 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
82 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
83 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
84 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
85 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
86 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
87 * SUCH DAMAGE.
89 * @(#)if.c 8.5 (Berkeley) 1/9/95
92 #include <sys/cdefs.h>
93 __KERNEL_RCSID(0, "$NetBSD: if.c,v 1.240 2009/10/26 16:41:35 cegger Exp $");
95 #include "opt_inet.h"
97 #include "opt_atalk.h"
98 #include "opt_natm.h"
99 #include "opt_pfil_hooks.h"
101 #include <sys/param.h>
102 #include <sys/mbuf.h>
103 #include <sys/systm.h>
104 #include <sys/callout.h>
105 #include <sys/proc.h>
106 #include <sys/socket.h>
107 #include <sys/socketvar.h>
108 #include <sys/domain.h>
109 #include <sys/protosw.h>
110 #include <sys/kernel.h>
111 #include <sys/ioctl.h>
112 #include <sys/sysctl.h>
113 #include <sys/syslog.h>
114 #include <sys/kauth.h>
116 #include <net/if.h>
117 #include <net/if_dl.h>
118 #include <net/if_ether.h>
119 #include <net/if_media.h>
120 #include <net80211/ieee80211.h>
121 #include <net80211/ieee80211_ioctl.h>
122 #include <net/if_types.h>
123 #include <net/radix.h>
124 #include <net/route.h>
125 #include <net/netisr.h>
126 #ifdef NETATALK
127 #include <netatalk/at_extern.h>
128 #include <netatalk/at.h>
129 #endif
130 #include <net/pfil.h>
132 #ifdef INET6
133 #include <netinet/in.h>
134 #include <netinet6/in6_var.h>
135 #include <netinet6/nd6.h>
136 #endif
138 #include "carp.h"
139 #if NCARP > 0
140 #include <netinet/ip_carp.h>
141 #endif
143 #include <compat/sys/sockio.h>
144 #include <compat/sys/socket.h>
146 MALLOC_DEFINE(M_IFADDR, "ifaddr", "interface address");
147 MALLOC_DEFINE(M_IFMADDR, "ether_multi", "link-level multicast address");
149 int ifqmaxlen = IFQ_MAXLEN;
150 callout_t if_slowtimo_ch;
152 int netisr; /* scheduling bits for network */
154 static int if_rt_walktree(struct rtentry *, void *);
156 static struct if_clone *if_clone_lookup(const char *, int *);
157 static int if_clone_list(struct if_clonereq *);
159 static LIST_HEAD(, if_clone) if_cloners = LIST_HEAD_INITIALIZER(if_cloners);
160 static int if_cloners_count;
162 static uint64_t index_gen;
163 static kmutex_t index_gen_mtx;
165 #ifdef PFIL_HOOKS
166 struct pfil_head if_pfil; /* packet filtering hook for interfaces */
167 #endif
169 static kauth_listener_t if_listener;
171 static void if_detach_queues(struct ifnet *, struct ifqueue *);
172 static void sysctl_sndq_setup(struct sysctllog **, const char *,
173 struct ifaltq *);
175 #if defined(INET) || defined(INET6)
176 static void sysctl_net_ifq_setup(struct sysctllog **, int, const char *,
177 int, const char *, int, struct ifqueue *);
178 #endif
180 static int
181 if_listener_cb(kauth_cred_t cred, kauth_action_t action, void *cookie,
182 void *arg0, void *arg1, void *arg2, void *arg3)
184 int result;
185 enum kauth_network_req req;
187 result = KAUTH_RESULT_DEFER;
188 req = (enum kauth_network_req)arg1;
190 if (action != KAUTH_NETWORK_INTERFACE)
191 return result;
193 if ((req == KAUTH_REQ_NETWORK_INTERFACE_GET) ||
194 (req == KAUTH_REQ_NETWORK_INTERFACE_SET))
195 result = KAUTH_RESULT_ALLOW;
197 return result;
201 * Network interface utility routines.
203 * Routines with ifa_ifwith* names take sockaddr *'s as
204 * parameters.
206 void
207 ifinit(void)
209 #ifdef INET
210 {extern struct ifqueue ipintrq;
211 sysctl_net_ifq_setup(NULL, PF_INET, "inet", IPPROTO_IP, "ip",
212 IPCTL_IFQ, &ipintrq);}
213 #endif /* INET */
214 #ifdef INET6
215 {extern struct ifqueue ip6intrq;
216 sysctl_net_ifq_setup(NULL, PF_INET6, "inet6", IPPROTO_IPV6, "ip6",
217 IPV6CTL_IFQ, &ip6intrq);}
218 #endif /* INET6 */
220 callout_init(&if_slowtimo_ch, 0);
221 if_slowtimo(NULL);
223 if_listener = kauth_listen_scope(KAUTH_SCOPE_NETWORK,
224 if_listener_cb, NULL);
228 * XXX Initialization before configure().
229 * XXX hack to get pfil_add_hook working in autoconf.
231 void
232 ifinit1(void)
235 mutex_init(&index_gen_mtx, MUTEX_DEFAULT, IPL_NONE);
237 #ifdef PFIL_HOOKS
238 if_pfil.ph_type = PFIL_TYPE_IFNET;
239 if_pfil.ph_ifnet = NULL;
240 if (pfil_head_register(&if_pfil) != 0)
241 printf("WARNING: unable to register pfil hook\n");
242 #endif
245 struct ifnet *
246 if_alloc(u_char type)
248 return malloc(sizeof(struct ifnet), M_DEVBUF, M_WAITOK|M_ZERO);
251 void
252 if_initname(struct ifnet *ifp, const char *name, int unit)
254 (void)snprintf(ifp->if_xname, sizeof(ifp->if_xname),
255 "%s%d", name, unit);
259 * Null routines used while an interface is going away. These routines
260 * just return an error.
264 if_nulloutput(struct ifnet *ifp, struct mbuf *m,
265 const struct sockaddr *so, struct rtentry *rt)
268 return ENXIO;
271 void
272 if_nullinput(struct ifnet *ifp, struct mbuf *m)
275 /* Nothing. */
278 void
279 if_nullstart(struct ifnet *ifp)
282 /* Nothing. */
286 if_nullioctl(struct ifnet *ifp, u_long cmd, void *data)
289 return ENXIO;
293 if_nullinit(struct ifnet *ifp)
296 return ENXIO;
299 void
300 if_nullstop(struct ifnet *ifp, int disable)
303 /* Nothing. */
306 void
307 if_nullwatchdog(struct ifnet *ifp)
310 /* Nothing. */
313 void
314 if_nulldrain(struct ifnet *ifp)
317 /* Nothing. */
320 static u_int if_index = 1;
321 struct ifnet_head ifnet;
322 size_t if_indexlim = 0;
323 struct ifaddr **ifnet_addrs = NULL;
324 struct ifnet **ifindex2ifnet = NULL;
325 struct ifnet *lo0ifp;
327 void
328 if_set_sadl(struct ifnet *ifp, const void *lla, u_char addrlen, bool factory)
330 struct ifaddr *ifa;
331 struct sockaddr_dl *sdl;
333 ifp->if_addrlen = addrlen;
334 if_alloc_sadl(ifp);
335 ifa = ifp->if_dl;
336 sdl = satosdl(ifa->ifa_addr);
338 (void)sockaddr_dl_setaddr(sdl, sdl->sdl_len, lla, ifp->if_addrlen);
339 if (factory) {
340 ifp->if_hwdl = ifp->if_dl;
341 IFAREF(ifp->if_hwdl);
343 /* TBD routing socket */
346 struct ifaddr *
347 if_dl_create(const struct ifnet *ifp, const struct sockaddr_dl **sdlp)
349 unsigned socksize, ifasize;
350 int addrlen, namelen;
351 struct sockaddr_dl *mask, *sdl;
352 struct ifaddr *ifa;
354 namelen = strlen(ifp->if_xname);
355 addrlen = ifp->if_addrlen;
356 socksize = roundup(sockaddr_dl_measure(namelen, addrlen), sizeof(long));
357 ifasize = sizeof(*ifa) + 2 * socksize;
358 ifa = (struct ifaddr *)malloc(ifasize, M_IFADDR, M_WAITOK|M_ZERO);
360 sdl = (struct sockaddr_dl *)(ifa + 1);
361 mask = (struct sockaddr_dl *)(socksize + (char *)sdl);
363 sockaddr_dl_init(sdl, socksize, ifp->if_index, ifp->if_type,
364 ifp->if_xname, namelen, NULL, addrlen);
365 mask->sdl_len = sockaddr_dl_measure(namelen, 0);
366 memset(&mask->sdl_data[0], 0xff, namelen);
367 ifa->ifa_rtrequest = link_rtrequest;
368 ifa->ifa_addr = (struct sockaddr *)sdl;
369 ifa->ifa_netmask = (struct sockaddr *)mask;
371 *sdlp = sdl;
373 return ifa;
376 static void
377 if_sadl_setrefs(struct ifnet *ifp, struct ifaddr *ifa)
379 const struct sockaddr_dl *sdl;
380 ifnet_addrs[ifp->if_index] = ifa;
381 IFAREF(ifa);
382 ifp->if_dl = ifa;
383 IFAREF(ifa);
384 sdl = satosdl(ifa->ifa_addr);
385 ifp->if_sadl = sdl;
389 * Allocate the link level name for the specified interface. This
390 * is an attachment helper. It must be called after ifp->if_addrlen
391 * is initialized, which may not be the case when if_attach() is
392 * called.
394 void
395 if_alloc_sadl(struct ifnet *ifp)
397 struct ifaddr *ifa;
398 const struct sockaddr_dl *sdl;
401 * If the interface already has a link name, release it
402 * now. This is useful for interfaces that can change
403 * link types, and thus switch link names often.
405 if (ifp->if_sadl != NULL)
406 if_free_sadl(ifp);
408 ifa = if_dl_create(ifp, &sdl);
410 ifa_insert(ifp, ifa);
411 if_sadl_setrefs(ifp, ifa);
414 static void
415 if_deactivate_sadl(struct ifnet *ifp)
417 struct ifaddr *ifa;
419 KASSERT(ifp->if_dl != NULL);
421 ifa = ifp->if_dl;
423 ifp->if_sadl = NULL;
425 ifnet_addrs[ifp->if_index] = NULL;
426 IFAFREE(ifa);
427 ifp->if_dl = NULL;
428 IFAFREE(ifa);
431 void
432 if_activate_sadl(struct ifnet *ifp, struct ifaddr *ifa,
433 const struct sockaddr_dl *sdl)
435 int s;
437 s = splnet();
439 if_deactivate_sadl(ifp);
441 if_sadl_setrefs(ifp, ifa);
442 IFADDR_FOREACH(ifa, ifp)
443 rtinit(ifa, RTM_LLINFO_UPD, 0);
444 splx(s);
448 * Free the link level name for the specified interface. This is
449 * a detach helper. This is called from if_detach() or from
450 * link layer type specific detach functions.
452 void
453 if_free_sadl(struct ifnet *ifp)
455 struct ifaddr *ifa;
456 int s;
458 ifa = ifnet_addrs[ifp->if_index];
459 if (ifa == NULL) {
460 KASSERT(ifp->if_sadl == NULL);
461 KASSERT(ifp->if_dl == NULL);
462 return;
465 KASSERT(ifp->if_sadl != NULL);
466 KASSERT(ifp->if_dl != NULL);
468 s = splnet();
469 rtinit(ifa, RTM_DELETE, 0);
470 ifa_remove(ifp, ifa);
471 if_deactivate_sadl(ifp);
472 if (ifp->if_hwdl == ifa) {
473 IFAFREE(ifa);
474 ifp->if_hwdl = NULL;
476 splx(s);
480 * Attach an interface to the
481 * list of "active" interfaces.
483 void
484 if_attach(struct ifnet *ifp)
486 int indexlim = 0;
488 if (if_indexlim == 0) {
489 TAILQ_INIT(&ifnet);
490 if_indexlim = 8;
492 TAILQ_INIT(&ifp->if_addrlist);
493 TAILQ_INSERT_TAIL(&ifnet, ifp, if_list);
494 if (ifp->if_ioctl == NULL)
495 ifp->if_ioctl = ifioctl_common;
497 mutex_enter(&index_gen_mtx);
498 ifp->if_index_gen = index_gen++;
499 mutex_exit(&index_gen_mtx);
501 ifp->if_index = if_index;
502 if (ifindex2ifnet == NULL)
503 if_index++;
504 else
505 while (ifp->if_index < if_indexlim &&
506 ifindex2ifnet[ifp->if_index] != NULL) {
507 ++if_index;
508 if (if_index == 0)
509 if_index = 1;
511 * If we hit USHRT_MAX, we skip back to 0 since
512 * there are a number of places where the value
513 * of if_index or if_index itself is compared
514 * to or stored in an unsigned short. By
515 * jumping back, we won't botch those assignments
516 * or comparisons.
518 else if (if_index == USHRT_MAX) {
520 * However, if we have to jump back to
521 * zero *twice* without finding an empty
522 * slot in ifindex2ifnet[], then there
523 * there are too many (>65535) interfaces.
525 if (indexlim++)
526 panic("too many interfaces");
527 else
528 if_index = 1;
530 ifp->if_index = if_index;
534 * We have some arrays that should be indexed by if_index.
535 * since if_index will grow dynamically, they should grow too.
536 * struct ifadd **ifnet_addrs
537 * struct ifnet **ifindex2ifnet
539 if (ifnet_addrs == NULL || ifindex2ifnet == NULL ||
540 ifp->if_index >= if_indexlim) {
541 size_t m, n, oldlim;
542 void *q;
544 oldlim = if_indexlim;
545 while (ifp->if_index >= if_indexlim)
546 if_indexlim <<= 1;
548 /* grow ifnet_addrs */
549 m = oldlim * sizeof(struct ifaddr *);
550 n = if_indexlim * sizeof(struct ifaddr *);
551 q = malloc(n, M_IFADDR, M_WAITOK|M_ZERO);
552 if (ifnet_addrs != NULL) {
553 memcpy(q, ifnet_addrs, m);
554 free(ifnet_addrs, M_IFADDR);
556 ifnet_addrs = (struct ifaddr **)q;
558 /* grow ifindex2ifnet */
559 m = oldlim * sizeof(struct ifnet *);
560 n = if_indexlim * sizeof(struct ifnet *);
561 q = malloc(n, M_IFADDR, M_WAITOK|M_ZERO);
562 if (ifindex2ifnet != NULL) {
563 memcpy(q, ifindex2ifnet, m);
564 free(ifindex2ifnet, M_IFADDR);
566 ifindex2ifnet = (struct ifnet **)q;
569 ifindex2ifnet[ifp->if_index] = ifp;
572 * Link level name is allocated later by a separate call to
573 * if_alloc_sadl().
576 if (ifp->if_snd.ifq_maxlen == 0)
577 ifp->if_snd.ifq_maxlen = ifqmaxlen;
579 sysctl_sndq_setup(&ifp->if_sysctl_log, ifp->if_xname, &ifp->if_snd);
581 ifp->if_broadcastaddr = 0; /* reliably crash if used uninitialized */
583 ifp->if_link_state = LINK_STATE_UNKNOWN;
585 ifp->if_capenable = 0;
586 ifp->if_csum_flags_tx = 0;
587 ifp->if_csum_flags_rx = 0;
589 #ifdef ALTQ
590 ifp->if_snd.altq_type = 0;
591 ifp->if_snd.altq_disc = NULL;
592 ifp->if_snd.altq_flags &= ALTQF_CANTCHANGE;
593 ifp->if_snd.altq_tbr = NULL;
594 ifp->if_snd.altq_ifp = ifp;
595 #endif
597 #ifdef PFIL_HOOKS
598 ifp->if_pfil.ph_type = PFIL_TYPE_IFNET;
599 ifp->if_pfil.ph_ifnet = ifp;
600 if (pfil_head_register(&ifp->if_pfil) != 0)
601 printf("%s: WARNING: unable to register pfil hook\n",
602 ifp->if_xname);
603 (void)pfil_run_hooks(&if_pfil,
604 (struct mbuf **)PFIL_IFNET_ATTACH, ifp, PFIL_IFNET);
605 #endif
607 if (!STAILQ_EMPTY(&domains))
608 if_attachdomain1(ifp);
610 /* Announce the interface. */
611 rt_ifannouncemsg(ifp, IFAN_ARRIVAL);
614 void
615 if_attachdomain(void)
617 struct ifnet *ifp;
618 int s;
620 s = splnet();
621 IFNET_FOREACH(ifp)
622 if_attachdomain1(ifp);
623 splx(s);
626 void
627 if_attachdomain1(struct ifnet *ifp)
629 struct domain *dp;
630 int s;
632 s = splnet();
634 /* address family dependent data region */
635 memset(ifp->if_afdata, 0, sizeof(ifp->if_afdata));
636 DOMAIN_FOREACH(dp) {
637 if (dp->dom_ifattach != NULL)
638 ifp->if_afdata[dp->dom_family] =
639 (*dp->dom_ifattach)(ifp);
642 splx(s);
646 * Deactivate an interface. This points all of the procedure
647 * handles at error stubs. May be called from interrupt context.
649 void
650 if_deactivate(struct ifnet *ifp)
652 int s;
654 s = splnet();
656 ifp->if_output = if_nulloutput;
657 ifp->if_input = if_nullinput;
658 ifp->if_start = if_nullstart;
659 ifp->if_ioctl = if_nullioctl;
660 ifp->if_init = if_nullinit;
661 ifp->if_stop = if_nullstop;
662 ifp->if_watchdog = if_nullwatchdog;
663 ifp->if_drain = if_nulldrain;
665 /* No more packets may be enqueued. */
666 ifp->if_snd.ifq_maxlen = 0;
668 splx(s);
671 void
672 if_purgeaddrs(struct ifnet *ifp, int family, void (*purgeaddr)(struct ifaddr *))
674 struct ifaddr *ifa, *nifa;
676 for (ifa = IFADDR_FIRST(ifp); ifa != NULL; ifa = nifa) {
677 nifa = IFADDR_NEXT(ifa);
678 if (ifa->ifa_addr->sa_family != family)
679 continue;
680 (*purgeaddr)(ifa);
685 * Detach an interface from the list of "active" interfaces,
686 * freeing any resources as we go along.
688 * NOTE: This routine must be called with a valid thread context,
689 * as it may block.
691 void
692 if_detach(struct ifnet *ifp)
694 struct socket so;
695 struct ifaddr *ifa;
696 #ifdef IFAREF_DEBUG
697 struct ifaddr *last_ifa = NULL;
698 #endif
699 struct domain *dp;
700 const struct protosw *pr;
701 int s, i, family, purged;
704 * XXX It's kind of lame that we have to have the
705 * XXX socket structure...
707 memset(&so, 0, sizeof(so));
709 s = splnet();
712 * Do an if_down() to give protocols a chance to do something.
714 if_down(ifp);
716 #ifdef ALTQ
717 if (ALTQ_IS_ENABLED(&ifp->if_snd))
718 altq_disable(&ifp->if_snd);
719 if (ALTQ_IS_ATTACHED(&ifp->if_snd))
720 altq_detach(&ifp->if_snd);
721 #endif
723 sysctl_teardown(&ifp->if_sysctl_log);
725 #if NCARP > 0
726 /* Remove the interface from any carp group it is a part of. */
727 if (ifp->if_carp != NULL && ifp->if_type != IFT_CARP)
728 carp_ifdetach(ifp);
729 #endif
732 * Rip all the addresses off the interface. This should make
733 * all of the routes go away.
735 * pr_usrreq calls can remove an arbitrary number of ifaddrs
736 * from the list, including our "cursor", ifa. For safety,
737 * and to honor the TAILQ abstraction, I just restart the
738 * loop after each removal. Note that the loop will exit
739 * when all of the remaining ifaddrs belong to the AF_LINK
740 * family. I am counting on the historical fact that at
741 * least one pr_usrreq in each address domain removes at
742 * least one ifaddr.
744 again:
745 IFADDR_FOREACH(ifa, ifp) {
746 family = ifa->ifa_addr->sa_family;
747 #ifdef IFAREF_DEBUG
748 printf("if_detach: ifaddr %p, family %d, refcnt %d\n",
749 ifa, family, ifa->ifa_refcnt);
750 if (last_ifa != NULL && ifa == last_ifa)
751 panic("if_detach: loop detected");
752 last_ifa = ifa;
753 #endif
754 if (family == AF_LINK)
755 continue;
756 dp = pffinddomain(family);
757 #ifdef DIAGNOSTIC
758 if (dp == NULL)
759 panic("if_detach: no domain for AF %d",
760 family);
761 #endif
763 * XXX These PURGEIF calls are redundant with the
764 * purge-all-families calls below, but are left in for
765 * now both to make a smaller change, and to avoid
766 * unplanned interactions with clearing of
767 * ifp->if_addrlist.
769 purged = 0;
770 for (pr = dp->dom_protosw;
771 pr < dp->dom_protoswNPROTOSW; pr++) {
772 so.so_proto = pr;
773 if (pr->pr_usrreq != NULL) {
774 (void) (*pr->pr_usrreq)(&so,
775 PRU_PURGEIF, NULL, NULL,
776 (struct mbuf *) ifp, curlwp);
777 purged = 1;
780 if (purged == 0) {
782 * XXX What's really the best thing to do
783 * XXX here? --thorpej@NetBSD.org
785 printf("if_detach: WARNING: AF %d not purged\n",
786 family);
787 ifa_remove(ifp, ifa);
789 goto again;
792 if_free_sadl(ifp);
794 /* Walk the routing table looking for stragglers. */
795 for (i = 0; i <= AF_MAX; i++)
796 (void)rt_walktree(i, if_rt_walktree, ifp);
798 DOMAIN_FOREACH(dp) {
799 if (dp->dom_ifdetach != NULL && ifp->if_afdata[dp->dom_family])
800 (*dp->dom_ifdetach)(ifp,
801 ifp->if_afdata[dp->dom_family]);
804 * One would expect multicast memberships (INET and
805 * INET6) on UDP sockets to be purged by the PURGEIF
806 * calls above, but if all addresses were removed from
807 * the interface prior to destruction, the calls will
808 * not be made (e.g. ppp, for which pppd(8) generally
809 * removes addresses before destroying the interface).
810 * Because there is no invariant that multicast
811 * memberships only exist for interfaces with IPv4
812 * addresses, we must call PURGEIF regardless of
813 * addresses. (Protocols which might store ifnet
814 * pointers are marked with PR_PURGEIF.)
816 for (pr = dp->dom_protosw; pr < dp->dom_protoswNPROTOSW; pr++) {
817 so.so_proto = pr;
818 if (pr->pr_usrreq != NULL && pr->pr_flags & PR_PURGEIF)
819 (void)(*pr->pr_usrreq)(&so, PRU_PURGEIF, NULL,
820 NULL, (struct mbuf *)ifp, curlwp);
824 #ifdef PFIL_HOOKS
825 (void)pfil_run_hooks(&if_pfil,
826 (struct mbuf **)PFIL_IFNET_DETACH, ifp, PFIL_IFNET);
827 (void)pfil_head_unregister(&ifp->if_pfil);
828 #endif
830 /* Announce that the interface is gone. */
831 rt_ifannouncemsg(ifp, IFAN_DEPARTURE);
833 ifindex2ifnet[ifp->if_index] = NULL;
835 TAILQ_REMOVE(&ifnet, ifp, if_list);
838 * remove packets that came from ifp, from software interrupt queues.
840 DOMAIN_FOREACH(dp) {
841 for (i = 0; i < __arraycount(dp->dom_ifqueues); i++) {
842 if (dp->dom_ifqueues[i] == NULL)
843 break;
844 if_detach_queues(ifp, dp->dom_ifqueues[i]);
848 splx(s);
851 static void
852 if_detach_queues(struct ifnet *ifp, struct ifqueue *q)
854 struct mbuf *m, *prev, *next;
856 prev = NULL;
857 for (m = q->ifq_head; m != NULL; m = next) {
858 next = m->m_nextpkt;
859 #ifdef DIAGNOSTIC
860 if ((m->m_flags & M_PKTHDR) == 0) {
861 prev = m;
862 continue;
864 #endif
865 if (m->m_pkthdr.rcvif != ifp) {
866 prev = m;
867 continue;
870 if (prev != NULL)
871 prev->m_nextpkt = m->m_nextpkt;
872 else
873 q->ifq_head = m->m_nextpkt;
874 if (q->ifq_tail == m)
875 q->ifq_tail = prev;
876 q->ifq_len--;
878 m->m_nextpkt = NULL;
879 m_freem(m);
880 IF_DROP(q);
885 * Callback for a radix tree walk to delete all references to an
886 * ifnet.
888 static int
889 if_rt_walktree(struct rtentry *rt, void *v)
891 struct ifnet *ifp = (struct ifnet *)v;
892 int error;
894 if (rt->rt_ifp != ifp)
895 return 0;
897 /* Delete the entry. */
898 ++rt->rt_refcnt;
899 error = rtrequest(RTM_DELETE, rt_getkey(rt), rt->rt_gateway,
900 rt_mask(rt), rt->rt_flags, NULL);
901 KASSERT((rt->rt_flags & RTF_UP) == 0);
902 rt->rt_ifp = NULL;
903 RTFREE(rt);
904 if (error != 0)
905 printf("%s: warning: unable to delete rtentry @ %p, "
906 "error = %d\n", ifp->if_xname, rt, error);
907 return 0;
911 * Create a clone network interface.
914 if_clone_create(const char *name)
916 struct if_clone *ifc;
917 int unit;
919 ifc = if_clone_lookup(name, &unit);
920 if (ifc == NULL)
921 return EINVAL;
923 if (ifunit(name) != NULL)
924 return EEXIST;
926 return (*ifc->ifc_create)(ifc, unit);
930 * Destroy a clone network interface.
933 if_clone_destroy(const char *name)
935 struct if_clone *ifc;
936 struct ifnet *ifp;
938 ifc = if_clone_lookup(name, NULL);
939 if (ifc == NULL)
940 return EINVAL;
942 ifp = ifunit(name);
943 if (ifp == NULL)
944 return ENXIO;
946 if (ifc->ifc_destroy == NULL)
947 return EOPNOTSUPP;
949 return (*ifc->ifc_destroy)(ifp);
953 * Look up a network interface cloner.
955 static struct if_clone *
956 if_clone_lookup(const char *name, int *unitp)
958 struct if_clone *ifc;
959 const char *cp;
960 int unit;
962 /* separate interface name from unit */
963 for (cp = name;
964 cp - name < IFNAMSIZ && *cp && (*cp < '0' || *cp > '9');
965 cp++)
966 continue;
968 if (cp == name || cp - name == IFNAMSIZ || !*cp)
969 return NULL; /* No name or unit number */
971 LIST_FOREACH(ifc, &if_cloners, ifc_list) {
972 if (strlen(ifc->ifc_name) == cp - name &&
973 strncmp(name, ifc->ifc_name, cp - name) == 0)
974 break;
977 if (ifc == NULL)
978 return NULL;
980 unit = 0;
981 while (cp - name < IFNAMSIZ && *cp) {
982 if (*cp < '0' || *cp > '9' || unit > INT_MAX / 10) {
983 /* Bogus unit number. */
984 return NULL;
986 unit = (unit * 10) + (*cp++ - '0');
989 if (unitp != NULL)
990 *unitp = unit;
991 return ifc;
995 * Register a network interface cloner.
997 void
998 if_clone_attach(struct if_clone *ifc)
1001 LIST_INSERT_HEAD(&if_cloners, ifc, ifc_list);
1002 if_cloners_count++;
1006 * Unregister a network interface cloner.
1008 void
1009 if_clone_detach(struct if_clone *ifc)
1012 LIST_REMOVE(ifc, ifc_list);
1013 if_cloners_count--;
1017 * Provide list of interface cloners to userspace.
1019 static int
1020 if_clone_list(struct if_clonereq *ifcr)
1022 char outbuf[IFNAMSIZ], *dst;
1023 struct if_clone *ifc;
1024 int count, error = 0;
1026 ifcr->ifcr_total = if_cloners_count;
1027 if ((dst = ifcr->ifcr_buffer) == NULL) {
1028 /* Just asking how many there are. */
1029 return 0;
1032 if (ifcr->ifcr_count < 0)
1033 return EINVAL;
1035 count = (if_cloners_count < ifcr->ifcr_count) ?
1036 if_cloners_count : ifcr->ifcr_count;
1038 for (ifc = LIST_FIRST(&if_cloners); ifc != NULL && count != 0;
1039 ifc = LIST_NEXT(ifc, ifc_list), count--, dst += IFNAMSIZ) {
1040 (void)strncpy(outbuf, ifc->ifc_name, sizeof(outbuf));
1041 if (outbuf[sizeof(outbuf) - 1] != '\0')
1042 return ENAMETOOLONG;
1043 error = copyout(outbuf, dst, sizeof(outbuf));
1044 if (error != 0)
1045 break;
1048 return error;
1051 void
1052 ifa_insert(struct ifnet *ifp, struct ifaddr *ifa)
1054 ifa->ifa_ifp = ifp;
1055 TAILQ_INSERT_TAIL(&ifp->if_addrlist, ifa, ifa_list);
1056 IFAREF(ifa);
1059 void
1060 ifa_remove(struct ifnet *ifp, struct ifaddr *ifa)
1062 KASSERT(ifa->ifa_ifp == ifp);
1063 TAILQ_REMOVE(&ifp->if_addrlist, ifa, ifa_list);
1064 IFAFREE(ifa);
1067 static inline int
1068 equal(const struct sockaddr *sa1, const struct sockaddr *sa2)
1070 return sockaddr_cmp(sa1, sa2) == 0;
1074 * Locate an interface based on a complete address.
1076 /*ARGSUSED*/
1077 struct ifaddr *
1078 ifa_ifwithaddr(const struct sockaddr *addr)
1080 struct ifnet *ifp;
1081 struct ifaddr *ifa;
1083 IFNET_FOREACH(ifp) {
1084 if (ifp->if_output == if_nulloutput)
1085 continue;
1086 IFADDR_FOREACH(ifa, ifp) {
1087 if (ifa->ifa_addr->sa_family != addr->sa_family)
1088 continue;
1089 if (equal(addr, ifa->ifa_addr))
1090 return ifa;
1091 if ((ifp->if_flags & IFF_BROADCAST) &&
1092 ifa->ifa_broadaddr &&
1093 /* IP6 doesn't have broadcast */
1094 ifa->ifa_broadaddr->sa_len != 0 &&
1095 equal(ifa->ifa_broadaddr, addr))
1096 return ifa;
1099 return NULL;
1103 * Locate the point to point interface with a given destination address.
1105 /*ARGSUSED*/
1106 struct ifaddr *
1107 ifa_ifwithdstaddr(const struct sockaddr *addr)
1109 struct ifnet *ifp;
1110 struct ifaddr *ifa;
1112 IFNET_FOREACH(ifp) {
1113 if (ifp->if_output == if_nulloutput)
1114 continue;
1115 if ((ifp->if_flags & IFF_POINTOPOINT) == 0)
1116 continue;
1117 IFADDR_FOREACH(ifa, ifp) {
1118 if (ifa->ifa_addr->sa_family != addr->sa_family ||
1119 ifa->ifa_dstaddr == NULL)
1120 continue;
1121 if (equal(addr, ifa->ifa_dstaddr))
1122 return ifa;
1125 return NULL;
1129 * Find an interface on a specific network. If many, choice
1130 * is most specific found.
1132 struct ifaddr *
1133 ifa_ifwithnet(const struct sockaddr *addr)
1135 struct ifnet *ifp;
1136 struct ifaddr *ifa;
1137 const struct sockaddr_dl *sdl;
1138 struct ifaddr *ifa_maybe = 0;
1139 u_int af = addr->sa_family;
1140 const char *addr_data = addr->sa_data, *cplim;
1142 if (af == AF_LINK) {
1143 sdl = satocsdl(addr);
1144 if (sdl->sdl_index && sdl->sdl_index < if_indexlim &&
1145 ifindex2ifnet[sdl->sdl_index] &&
1146 ifindex2ifnet[sdl->sdl_index]->if_output != if_nulloutput)
1147 return ifnet_addrs[sdl->sdl_index];
1149 #ifdef NETATALK
1150 if (af == AF_APPLETALK) {
1151 const struct sockaddr_at *sat, *sat2;
1152 sat = (const struct sockaddr_at *)addr;
1153 IFNET_FOREACH(ifp) {
1154 if (ifp->if_output == if_nulloutput)
1155 continue;
1156 ifa = at_ifawithnet((const struct sockaddr_at *)addr, ifp);
1157 if (ifa == NULL)
1158 continue;
1159 sat2 = (struct sockaddr_at *)ifa->ifa_addr;
1160 if (sat2->sat_addr.s_net == sat->sat_addr.s_net)
1161 return ifa; /* exact match */
1162 if (ifa_maybe == NULL) {
1163 /* else keep the if with the right range */
1164 ifa_maybe = ifa;
1167 return ifa_maybe;
1169 #endif
1170 IFNET_FOREACH(ifp) {
1171 if (ifp->if_output == if_nulloutput)
1172 continue;
1173 IFADDR_FOREACH(ifa, ifp) {
1174 const char *cp, *cp2, *cp3;
1176 if (ifa->ifa_addr->sa_family != af ||
1177 ifa->ifa_netmask == NULL)
1178 next: continue;
1179 cp = addr_data;
1180 cp2 = ifa->ifa_addr->sa_data;
1181 cp3 = ifa->ifa_netmask->sa_data;
1182 cplim = (const char *)ifa->ifa_netmask +
1183 ifa->ifa_netmask->sa_len;
1184 while (cp3 < cplim) {
1185 if ((*cp++ ^ *cp2++) & *cp3++) {
1186 /* want to continue for() loop */
1187 goto next;
1190 if (ifa_maybe == NULL ||
1191 rn_refines((void *)ifa->ifa_netmask,
1192 (void *)ifa_maybe->ifa_netmask))
1193 ifa_maybe = ifa;
1196 return ifa_maybe;
1200 * Find the interface of the addresss.
1202 struct ifaddr *
1203 ifa_ifwithladdr(const struct sockaddr *addr)
1205 struct ifaddr *ia;
1207 if ((ia = ifa_ifwithaddr(addr)) || (ia = ifa_ifwithdstaddr(addr)) ||
1208 (ia = ifa_ifwithnet(addr)))
1209 return ia;
1210 return NULL;
1214 * Find an interface using a specific address family
1216 struct ifaddr *
1217 ifa_ifwithaf(int af)
1219 struct ifnet *ifp;
1220 struct ifaddr *ifa;
1222 IFNET_FOREACH(ifp) {
1223 if (ifp->if_output == if_nulloutput)
1224 continue;
1225 IFADDR_FOREACH(ifa, ifp) {
1226 if (ifa->ifa_addr->sa_family == af)
1227 return ifa;
1230 return NULL;
1234 * Find an interface address specific to an interface best matching
1235 * a given address.
1237 struct ifaddr *
1238 ifaof_ifpforaddr(const struct sockaddr *addr, struct ifnet *ifp)
1240 struct ifaddr *ifa;
1241 const char *cp, *cp2, *cp3;
1242 const char *cplim;
1243 struct ifaddr *ifa_maybe = 0;
1244 u_int af = addr->sa_family;
1246 if (ifp->if_output == if_nulloutput)
1247 return NULL;
1249 if (af >= AF_MAX)
1250 return NULL;
1252 IFADDR_FOREACH(ifa, ifp) {
1253 if (ifa->ifa_addr->sa_family != af)
1254 continue;
1255 ifa_maybe = ifa;
1256 if (ifa->ifa_netmask == NULL) {
1257 if (equal(addr, ifa->ifa_addr) ||
1258 (ifa->ifa_dstaddr &&
1259 equal(addr, ifa->ifa_dstaddr)))
1260 return ifa;
1261 continue;
1263 cp = addr->sa_data;
1264 cp2 = ifa->ifa_addr->sa_data;
1265 cp3 = ifa->ifa_netmask->sa_data;
1266 cplim = ifa->ifa_netmask->sa_len + (char *)ifa->ifa_netmask;
1267 for (; cp3 < cplim; cp3++) {
1268 if ((*cp++ ^ *cp2++) & *cp3)
1269 break;
1271 if (cp3 == cplim)
1272 return ifa;
1274 return ifa_maybe;
1278 * Default action when installing a route with a Link Level gateway.
1279 * Lookup an appropriate real ifa to point to.
1280 * This should be moved to /sys/net/link.c eventually.
1282 void
1283 link_rtrequest(int cmd, struct rtentry *rt, const struct rt_addrinfo *info)
1285 struct ifaddr *ifa;
1286 const struct sockaddr *dst;
1287 struct ifnet *ifp;
1289 if (cmd != RTM_ADD || (ifa = rt->rt_ifa) == NULL ||
1290 (ifp = ifa->ifa_ifp) == NULL || (dst = rt_getkey(rt)) == NULL)
1291 return;
1292 if ((ifa = ifaof_ifpforaddr(dst, ifp)) != NULL) {
1293 rt_replace_ifa(rt, ifa);
1294 if (ifa->ifa_rtrequest && ifa->ifa_rtrequest != link_rtrequest)
1295 ifa->ifa_rtrequest(cmd, rt, info);
1300 * Handle a change in the interface link state.
1302 void
1303 if_link_state_change(struct ifnet *ifp, int link_state)
1305 if (ifp->if_link_state == link_state)
1306 return;
1307 ifp->if_link_state = link_state;
1308 /* Notify that the link state has changed. */
1309 rt_ifmsg(ifp);
1310 #if NCARP > 0
1311 if (ifp->if_carp)
1312 carp_carpdev_state(ifp);
1313 #endif
1317 * Mark an interface down and notify protocols of
1318 * the transition.
1319 * NOTE: must be called at splsoftnet or equivalent.
1321 void
1322 if_down(struct ifnet *ifp)
1324 struct ifaddr *ifa;
1326 ifp->if_flags &= ~IFF_UP;
1327 nanotime(&ifp->if_lastchange);
1328 IFADDR_FOREACH(ifa, ifp)
1329 pfctlinput(PRC_IFDOWN, ifa->ifa_addr);
1330 IFQ_PURGE(&ifp->if_snd);
1331 #if NCARP > 0
1332 if (ifp->if_carp)
1333 carp_carpdev_state(ifp);
1334 #endif
1335 rt_ifmsg(ifp);
1339 * Mark an interface up and notify protocols of
1340 * the transition.
1341 * NOTE: must be called at splsoftnet or equivalent.
1343 void
1344 if_up(struct ifnet *ifp)
1346 #ifdef notyet
1347 struct ifaddr *ifa;
1348 #endif
1350 ifp->if_flags |= IFF_UP;
1351 nanotime(&ifp->if_lastchange);
1352 #ifdef notyet
1353 /* this has no effect on IP, and will kill all ISO connections XXX */
1354 IFADDR_FOREACH(ifa, ifp)
1355 pfctlinput(PRC_IFUP, ifa->ifa_addr);
1356 #endif
1357 #if NCARP > 0
1358 if (ifp->if_carp)
1359 carp_carpdev_state(ifp);
1360 #endif
1361 rt_ifmsg(ifp);
1362 #ifdef INET6
1363 in6_if_up(ifp);
1364 #endif
1368 * Handle interface watchdog timer routines. Called
1369 * from softclock, we decrement timers (if set) and
1370 * call the appropriate interface routine on expiration.
1372 void
1373 if_slowtimo(void *arg)
1375 struct ifnet *ifp;
1376 int s = splnet();
1378 IFNET_FOREACH(ifp) {
1379 if (ifp->if_timer == 0 || --ifp->if_timer)
1380 continue;
1381 if (ifp->if_watchdog != NULL)
1382 (*ifp->if_watchdog)(ifp);
1384 splx(s);
1385 callout_reset(&if_slowtimo_ch, hz / IFNET_SLOWHZ, if_slowtimo, NULL);
1389 * Set/clear promiscuous mode on interface ifp based on the truth value
1390 * of pswitch. The calls are reference counted so that only the first
1391 * "on" request actually has an effect, as does the final "off" request.
1392 * Results are undefined if the "off" and "on" requests are not matched.
1395 ifpromisc(struct ifnet *ifp, int pswitch)
1397 int pcount, ret;
1398 short flags;
1399 struct ifreq ifr;
1401 pcount = ifp->if_pcount;
1402 flags = ifp->if_flags;
1403 if (pswitch) {
1405 * Allow the device to be "placed" into promiscuous
1406 * mode even if it is not configured up. It will
1407 * consult IFF_PROMISC when it is is brought up.
1409 if (ifp->if_pcount++ != 0)
1410 return 0;
1411 ifp->if_flags |= IFF_PROMISC;
1412 if ((ifp->if_flags & IFF_UP) == 0)
1413 return 0;
1414 } else {
1415 if (--ifp->if_pcount > 0)
1416 return 0;
1417 ifp->if_flags &= ~IFF_PROMISC;
1419 * If the device is not configured up, we should not need to
1420 * turn off promiscuous mode (device should have turned it
1421 * off when interface went down; and will look at IFF_PROMISC
1422 * again next time interface comes up).
1424 if ((ifp->if_flags & IFF_UP) == 0)
1425 return 0;
1427 memset(&ifr, 0, sizeof(ifr));
1428 ifr.ifr_flags = ifp->if_flags;
1429 ret = (*ifp->if_ioctl)(ifp, SIOCSIFFLAGS, &ifr);
1430 /* Restore interface state if not successful. */
1431 if (ret != 0) {
1432 ifp->if_pcount = pcount;
1433 ifp->if_flags = flags;
1435 return ret;
1439 * Map interface name to
1440 * interface structure pointer.
1442 struct ifnet *
1443 ifunit(const char *name)
1445 struct ifnet *ifp;
1446 const char *cp = name;
1447 u_int unit = 0;
1448 u_int i;
1451 * If the entire name is a number, treat it as an ifindex.
1453 for (i = 0; i < IFNAMSIZ && *cp >= '0' && *cp <= '9'; i++, cp++) {
1454 unit = unit * 10 + (*cp - '0');
1458 * If the number took all of the name, then it's a valid ifindex.
1460 if (i == IFNAMSIZ || (cp != name && *cp == '\0')) {
1461 if (unit >= if_indexlim)
1462 return NULL;
1463 ifp = ifindex2ifnet[unit];
1464 if (ifp == NULL || ifp->if_output == if_nulloutput)
1465 return NULL;
1466 return ifp;
1469 IFNET_FOREACH(ifp) {
1470 if (ifp->if_output == if_nulloutput)
1471 continue;
1472 if (strcmp(ifp->if_xname, name) == 0)
1473 return ifp;
1475 return NULL;
1478 /* common */
1480 ifioctl_common(struct ifnet *ifp, u_long cmd, void *data)
1482 int s;
1483 struct ifreq *ifr;
1484 struct ifcapreq *ifcr;
1485 struct ifdatareq *ifdr;
1487 switch (cmd) {
1488 case SIOCSIFCAP:
1489 ifcr = data;
1490 if ((ifcr->ifcr_capenable & ~ifp->if_capabilities) != 0)
1491 return EINVAL;
1493 if (ifcr->ifcr_capenable == ifp->if_capenable)
1494 return 0;
1496 ifp->if_capenable = ifcr->ifcr_capenable;
1498 /* Pre-compute the checksum flags mask. */
1499 ifp->if_csum_flags_tx = 0;
1500 ifp->if_csum_flags_rx = 0;
1501 if (ifp->if_capenable & IFCAP_CSUM_IPv4_Tx) {
1502 ifp->if_csum_flags_tx |= M_CSUM_IPv4;
1504 if (ifp->if_capenable & IFCAP_CSUM_IPv4_Rx) {
1505 ifp->if_csum_flags_rx |= M_CSUM_IPv4;
1508 if (ifp->if_capenable & IFCAP_CSUM_TCPv4_Tx) {
1509 ifp->if_csum_flags_tx |= M_CSUM_TCPv4;
1511 if (ifp->if_capenable & IFCAP_CSUM_TCPv4_Rx) {
1512 ifp->if_csum_flags_rx |= M_CSUM_TCPv4;
1515 if (ifp->if_capenable & IFCAP_CSUM_UDPv4_Tx) {
1516 ifp->if_csum_flags_tx |= M_CSUM_UDPv4;
1518 if (ifp->if_capenable & IFCAP_CSUM_UDPv4_Rx) {
1519 ifp->if_csum_flags_rx |= M_CSUM_UDPv4;
1522 if (ifp->if_capenable & IFCAP_CSUM_TCPv6_Tx) {
1523 ifp->if_csum_flags_tx |= M_CSUM_TCPv6;
1525 if (ifp->if_capenable & IFCAP_CSUM_TCPv6_Rx) {
1526 ifp->if_csum_flags_rx |= M_CSUM_TCPv6;
1529 if (ifp->if_capenable & IFCAP_CSUM_UDPv6_Tx) {
1530 ifp->if_csum_flags_tx |= M_CSUM_UDPv6;
1532 if (ifp->if_capenable & IFCAP_CSUM_UDPv6_Rx) {
1533 ifp->if_csum_flags_rx |= M_CSUM_UDPv6;
1535 if (ifp->if_flags & IFF_UP)
1536 return ENETRESET;
1537 return 0;
1538 case SIOCSIFFLAGS:
1539 ifr = data;
1540 if (ifp->if_flags & IFF_UP && (ifr->ifr_flags & IFF_UP) == 0) {
1541 s = splnet();
1542 if_down(ifp);
1543 splx(s);
1545 if (ifr->ifr_flags & IFF_UP && (ifp->if_flags & IFF_UP) == 0) {
1546 s = splnet();
1547 if_up(ifp);
1548 splx(s);
1550 ifp->if_flags = (ifp->if_flags & IFF_CANTCHANGE) |
1551 (ifr->ifr_flags &~ IFF_CANTCHANGE);
1552 break;
1553 case SIOCGIFFLAGS:
1554 ifr = data;
1555 ifr->ifr_flags = ifp->if_flags;
1556 break;
1558 case SIOCGIFMETRIC:
1559 ifr = data;
1560 ifr->ifr_metric = ifp->if_metric;
1561 break;
1563 case SIOCGIFMTU:
1564 ifr = data;
1565 ifr->ifr_mtu = ifp->if_mtu;
1566 break;
1568 case SIOCGIFDLT:
1569 ifr = data;
1570 ifr->ifr_dlt = ifp->if_dlt;
1571 break;
1573 case SIOCGIFCAP:
1574 ifcr = data;
1575 ifcr->ifcr_capabilities = ifp->if_capabilities;
1576 ifcr->ifcr_capenable = ifp->if_capenable;
1577 break;
1579 case SIOCSIFMETRIC:
1580 ifr = data;
1581 ifp->if_metric = ifr->ifr_metric;
1582 break;
1584 case SIOCGIFDATA:
1585 ifdr = data;
1586 ifdr->ifdr_data = ifp->if_data;
1587 break;
1589 case SIOCZIFDATA:
1590 ifdr = data;
1591 ifdr->ifdr_data = ifp->if_data;
1593 * Assumes that the volatile counters that can be
1594 * zero'ed are at the end of if_data.
1596 memset(&ifp->if_data.ifi_ipackets, 0, sizeof(ifp->if_data) -
1597 offsetof(struct if_data, ifi_ipackets));
1598 break;
1599 case SIOCSIFMTU:
1600 ifr = data;
1601 if (ifp->if_mtu == ifr->ifr_mtu)
1602 break;
1603 ifp->if_mtu = ifr->ifr_mtu;
1605 * If the link MTU changed, do network layer specific procedure.
1607 #ifdef INET6
1608 nd6_setmtu(ifp);
1609 #endif
1610 return ENETRESET;
1611 default:
1612 return ENOTTY;
1614 return 0;
1618 ifaddrpref_ioctl(struct socket *so, u_long cmd, void *data, struct ifnet *ifp,
1619 lwp_t *l)
1621 struct if_addrprefreq *ifap = (struct if_addrprefreq *)data;
1622 struct ifaddr *ifa;
1623 const struct sockaddr *any, *sa;
1624 union {
1625 struct sockaddr sa;
1626 struct sockaddr_storage ss;
1627 } u, v;
1629 switch (cmd) {
1630 case SIOCSIFADDRPREF:
1631 if (kauth_authorize_network(l->l_cred, KAUTH_NETWORK_INTERFACE,
1632 KAUTH_REQ_NETWORK_INTERFACE_SETPRIV, ifp, (void *)cmd,
1633 NULL) != 0)
1634 return EPERM;
1635 case SIOCGIFADDRPREF:
1636 break;
1637 default:
1638 return EOPNOTSUPP;
1641 /* sanity checks */
1642 if (data == NULL || ifp == NULL) {
1643 panic("invalid argument to %s", __func__);
1644 /*NOTREACHED*/
1647 /* address must be specified on ADD and DELETE */
1648 sa = sstocsa(&ifap->ifap_addr);
1649 if (sa->sa_family != sofamily(so))
1650 return EINVAL;
1651 if ((any = sockaddr_any(sa)) == NULL || sa->sa_len != any->sa_len)
1652 return EINVAL;
1654 sockaddr_externalize(&v.sa, sizeof(v.ss), sa);
1656 IFADDR_FOREACH(ifa, ifp) {
1657 if (ifa->ifa_addr->sa_family != sa->sa_family)
1658 continue;
1659 sockaddr_externalize(&u.sa, sizeof(u.ss), ifa->ifa_addr);
1660 if (sockaddr_cmp(&u.sa, &v.sa) == 0)
1661 break;
1663 if (ifa == NULL)
1664 return EADDRNOTAVAIL;
1666 switch (cmd) {
1667 case SIOCSIFADDRPREF:
1668 ifa->ifa_preference = ifap->ifap_preference;
1669 return 0;
1670 case SIOCGIFADDRPREF:
1671 /* fill in the if_laddrreq structure */
1672 (void)sockaddr_copy(sstosa(&ifap->ifap_addr),
1673 sizeof(ifap->ifap_addr), ifa->ifa_addr);
1674 ifap->ifap_preference = ifa->ifa_preference;
1675 return 0;
1676 default:
1677 return EOPNOTSUPP;
1682 * Interface ioctls.
1685 ifioctl(struct socket *so, u_long cmd, void *data, struct lwp *l)
1687 struct ifnet *ifp;
1688 struct ifreq *ifr;
1689 struct ifcapreq *ifcr;
1690 struct ifdatareq *ifdr;
1691 int error = 0;
1692 #if defined(COMPAT_OSOCK) || defined(COMPAT_OIFREQ)
1693 u_long ocmd = cmd;
1694 #endif
1695 short oif_flags;
1696 #ifdef COMPAT_OIFREQ
1697 struct ifreq ifrb;
1698 struct oifreq *oifr = NULL;
1699 #endif
1701 switch (cmd) {
1702 #ifdef COMPAT_OIFREQ
1703 case OSIOCGIFCONF:
1704 case OOSIOCGIFCONF:
1705 return compat_ifconf(cmd, data);
1706 #endif
1707 #ifdef COMPAT_OIFDATA
1708 case OSIOCGIFDATA:
1709 case OSIOCZIFDATA:
1710 return compat_ifdatareq(l, cmd, data);
1711 #endif
1712 case SIOCGIFCONF:
1713 return ifconf(cmd, data);
1714 case SIOCINITIFADDR:
1715 return EPERM;
1718 #ifdef COMPAT_OIFREQ
1719 cmd = compat_cvtcmd(cmd);
1720 if (cmd != ocmd) {
1721 oifr = data;
1722 data = ifr = &ifrb;
1723 ifreqo2n(oifr, ifr);
1724 } else
1725 #endif
1726 ifr = data;
1727 ifcr = data;
1728 ifdr = data;
1730 ifp = ifunit(ifr->ifr_name);
1732 switch (cmd) {
1733 case SIOCIFCREATE:
1734 case SIOCIFDESTROY:
1735 if (l != NULL) {
1736 error = kauth_authorize_network(l->l_cred,
1737 KAUTH_NETWORK_INTERFACE,
1738 KAUTH_REQ_NETWORK_INTERFACE_SETPRIV, ifp,
1739 (void *)cmd, NULL);
1740 if (error != 0)
1741 return error;
1743 return (cmd == SIOCIFCREATE) ?
1744 if_clone_create(ifr->ifr_name) :
1745 if_clone_destroy(ifr->ifr_name);
1747 case SIOCIFGCLONERS:
1748 return if_clone_list((struct if_clonereq *)data);
1751 if (ifp == NULL)
1752 return ENXIO;
1754 switch (cmd) {
1755 case SIOCALIFADDR:
1756 case SIOCDLIFADDR:
1757 case SIOCSIFADDRPREF:
1758 case SIOCSIFFLAGS:
1759 case SIOCSIFCAP:
1760 case SIOCSIFMETRIC:
1761 case SIOCZIFDATA:
1762 case SIOCSIFMTU:
1763 case SIOCSIFPHYADDR:
1764 case SIOCDIFPHYADDR:
1765 #ifdef INET6
1766 case SIOCSIFPHYADDR_IN6:
1767 #endif
1768 case SIOCSLIFPHYADDR:
1769 case SIOCADDMULTI:
1770 case SIOCDELMULTI:
1771 case SIOCSIFMEDIA:
1772 case SIOCSDRVSPEC:
1773 case SIOCG80211:
1774 case SIOCS80211:
1775 case SIOCS80211NWID:
1776 case SIOCS80211NWKEY:
1777 case SIOCS80211POWER:
1778 case SIOCS80211BSSID:
1779 case SIOCS80211CHANNEL:
1780 if (l != NULL) {
1781 error = kauth_authorize_network(l->l_cred,
1782 KAUTH_NETWORK_INTERFACE,
1783 KAUTH_REQ_NETWORK_INTERFACE_SETPRIV, ifp,
1784 (void *)cmd, NULL);
1785 if (error != 0)
1786 return error;
1790 oif_flags = ifp->if_flags;
1792 error = (*ifp->if_ioctl)(ifp, cmd, data);
1793 if (error != ENOTTY)
1795 else if (so->so_proto == NULL)
1796 return EOPNOTSUPP;
1797 else {
1798 #ifdef COMPAT_OSOCK
1799 error = compat_ifioctl(so, ocmd, cmd, data, l);
1800 #else
1801 error = (*so->so_proto->pr_usrreq)(so, PRU_CONTROL,
1802 (struct mbuf *)cmd, (struct mbuf *)data,
1803 (struct mbuf *)ifp, l);
1804 #endif
1807 if (((oif_flags ^ ifp->if_flags) & IFF_UP) != 0) {
1808 #ifdef INET6
1809 if ((ifp->if_flags & IFF_UP) != 0) {
1810 int s = splnet();
1811 in6_if_up(ifp);
1812 splx(s);
1814 #endif
1816 #ifdef COMPAT_OIFREQ
1817 if (cmd != ocmd)
1818 ifreqn2o(oifr, ifr);
1819 #endif
1821 return error;
1825 * Return interface configuration
1826 * of system. List may be used
1827 * in later ioctl's (above) to get
1828 * other information.
1830 * Each record is a struct ifreq. Before the addition of
1831 * sockaddr_storage, the API rule was that sockaddr flavors that did
1832 * not fit would extend beyond the struct ifreq, with the next struct
1833 * ifreq starting sa_len beyond the struct sockaddr. Because the
1834 * union in struct ifreq includes struct sockaddr_storage, every kind
1835 * of sockaddr must fit. Thus, there are no longer any overlength
1836 * records.
1838 * Records are added to the user buffer if they fit, and ifc_len is
1839 * adjusted to the length that was written. Thus, the user is only
1840 * assured of getting the complete list if ifc_len on return is at
1841 * least sizeof(struct ifreq) less than it was on entry.
1843 * If the user buffer pointer is NULL, this routine copies no data and
1844 * returns the amount of space that would be needed.
1846 * Invariants:
1847 * ifrp points to the next part of the user's buffer to be used. If
1848 * ifrp != NULL, space holds the number of bytes remaining that we may
1849 * write at ifrp. Otherwise, space holds the number of bytes that
1850 * would have been written had there been adequate space.
1852 /*ARGSUSED*/
1854 ifconf(u_long cmd, void *data)
1856 struct ifconf *ifc = (struct ifconf *)data;
1857 struct ifnet *ifp;
1858 struct ifaddr *ifa;
1859 struct ifreq ifr, *ifrp;
1860 int space, error = 0;
1861 const int sz = (int)sizeof(struct ifreq);
1863 if ((ifrp = ifc->ifc_req) == NULL)
1864 space = 0;
1865 else
1866 space = ifc->ifc_len;
1867 IFNET_FOREACH(ifp) {
1868 (void)strncpy(ifr.ifr_name, ifp->if_xname,
1869 sizeof(ifr.ifr_name));
1870 if (ifr.ifr_name[sizeof(ifr.ifr_name) - 1] != '\0')
1871 return ENAMETOOLONG;
1872 if (IFADDR_EMPTY(ifp)) {
1873 /* Interface with no addresses - send zero sockaddr. */
1874 memset(&ifr.ifr_addr, 0, sizeof(ifr.ifr_addr));
1875 if (ifrp == NULL) {
1876 space += sz;
1877 continue;
1879 if (space >= sz) {
1880 error = copyout(&ifr, ifrp, sz);
1881 if (error != 0)
1882 return error;
1883 ifrp++;
1884 space -= sz;
1888 IFADDR_FOREACH(ifa, ifp) {
1889 struct sockaddr *sa = ifa->ifa_addr;
1890 /* all sockaddrs must fit in sockaddr_storage */
1891 KASSERT(sa->sa_len <= sizeof(ifr.ifr_ifru));
1893 if (ifrp == NULL) {
1894 space += sz;
1895 continue;
1897 memcpy(&ifr.ifr_space, sa, sa->sa_len);
1898 if (space >= sz) {
1899 error = copyout(&ifr, ifrp, sz);
1900 if (error != 0)
1901 return (error);
1902 ifrp++; space -= sz;
1906 if (ifrp != NULL) {
1907 KASSERT(0 <= space && space <= ifc->ifc_len);
1908 ifc->ifc_len -= space;
1909 } else {
1910 KASSERT(space >= 0);
1911 ifc->ifc_len = space;
1913 return (0);
1917 ifreq_setaddr(const u_long cmd, struct ifreq *ifr, const struct sockaddr *sa)
1919 uint8_t len;
1920 u_long ncmd;
1922 if ((ncmd = compat_cvtcmd(cmd)) != cmd)
1923 len = sizeof(ifr->ifr_addr);
1924 else
1925 len = sizeof(ifr->ifr_ifru.ifru_space);
1926 if (len < sa->sa_len)
1927 return EFBIG;
1928 memset(&ifr->ifr_addr, 0, len);
1929 sockaddr_copy(&ifr->ifr_addr, len, sa);
1930 return 0;
1934 * Queue message on interface, and start output if interface
1935 * not yet active.
1938 ifq_enqueue(struct ifnet *ifp, struct mbuf *m
1939 ALTQ_COMMA ALTQ_DECL(struct altq_pktattr *pktattr))
1941 int len = m->m_pkthdr.len;
1942 int mflags = m->m_flags;
1943 int s = splnet();
1944 int error;
1946 IFQ_ENQUEUE(&ifp->if_snd, m, pktattr, error);
1947 if (error != 0)
1948 goto out;
1949 ifp->if_obytes += len;
1950 if (mflags & M_MCAST)
1951 ifp->if_omcasts++;
1952 if ((ifp->if_flags & IFF_OACTIVE) == 0)
1953 (*ifp->if_start)(ifp);
1954 out:
1955 splx(s);
1956 return error;
1960 * Queue message on interface, possibly using a second fast queue
1963 ifq_enqueue2(struct ifnet *ifp, struct ifqueue *ifq, struct mbuf *m
1964 ALTQ_COMMA ALTQ_DECL(struct altq_pktattr *pktattr))
1966 int error = 0;
1968 if (ifq != NULL
1969 #ifdef ALTQ
1970 && ALTQ_IS_ENABLED(&ifp->if_snd) == 0
1971 #endif
1973 if (IF_QFULL(ifq)) {
1974 IF_DROP(&ifp->if_snd);
1975 m_freem(m);
1976 if (error == 0)
1977 error = ENOBUFS;
1978 } else
1979 IF_ENQUEUE(ifq, m);
1980 } else
1981 IFQ_ENQUEUE(&ifp->if_snd, m, pktattr, error);
1982 if (error != 0) {
1983 ++ifp->if_oerrors;
1984 return error;
1986 return 0;
1990 static void
1991 sysctl_sndq_setup(struct sysctllog **clog, const char *ifname,
1992 struct ifaltq *ifq)
1994 const struct sysctlnode *cnode, *rnode;
1996 if (sysctl_createv(clog, 0, NULL, &rnode,
1997 CTLFLAG_PERMANENT,
1998 CTLTYPE_NODE, "net", NULL,
1999 NULL, 0, NULL, 0,
2000 CTL_NET, CTL_EOL) != 0)
2001 goto bad;
2003 if (sysctl_createv(clog, 0, &rnode, &rnode,
2004 CTLFLAG_PERMANENT,
2005 CTLTYPE_NODE, "interfaces",
2006 SYSCTL_DESCR("Per-interface controls"),
2007 NULL, 0, NULL, 0,
2008 CTL_CREATE, CTL_EOL) != 0)
2009 goto bad;
2011 if (sysctl_createv(clog, 0, &rnode, &rnode,
2012 CTLFLAG_PERMANENT,
2013 CTLTYPE_NODE, ifname,
2014 SYSCTL_DESCR("Interface controls"),
2015 NULL, 0, NULL, 0,
2016 CTL_CREATE, CTL_EOL) != 0)
2017 goto bad;
2019 if (sysctl_createv(clog, 0, &rnode, &rnode,
2020 CTLFLAG_PERMANENT,
2021 CTLTYPE_NODE, "sndq",
2022 SYSCTL_DESCR("Interface output queue controls"),
2023 NULL, 0, NULL, 0,
2024 CTL_CREATE, CTL_EOL) != 0)
2025 goto bad;
2027 if (sysctl_createv(clog, 0, &rnode, &cnode,
2028 CTLFLAG_PERMANENT,
2029 CTLTYPE_INT, "len",
2030 SYSCTL_DESCR("Current output queue length"),
2031 NULL, 0, &ifq->ifq_len, 0,
2032 CTL_CREATE, CTL_EOL) != 0)
2033 goto bad;
2035 if (sysctl_createv(clog, 0, &rnode, &cnode,
2036 CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
2037 CTLTYPE_INT, "maxlen",
2038 SYSCTL_DESCR("Maximum allowed output queue length"),
2039 NULL, 0, &ifq->ifq_maxlen, 0,
2040 CTL_CREATE, CTL_EOL) != 0)
2041 goto bad;
2043 if (sysctl_createv(clog, 0, &rnode, &cnode,
2044 CTLFLAG_PERMANENT,
2045 CTLTYPE_INT, "drops",
2046 SYSCTL_DESCR("Packets dropped due to full output queue"),
2047 NULL, 0, &ifq->ifq_drops, 0,
2048 CTL_CREATE, CTL_EOL) != 0)
2049 goto bad;
2051 return;
2052 bad:
2053 printf("%s: could not attach sysctl nodes\n", ifname);
2054 return;
2057 #if defined(INET) || defined(INET6)
2058 static void
2059 sysctl_net_ifq_setup(struct sysctllog **clog,
2060 int pf, const char *pfname,
2061 int ipn, const char *ipname,
2062 int qid, struct ifqueue *ifq)
2065 sysctl_createv(clog, 0, NULL, NULL,
2066 CTLFLAG_PERMANENT,
2067 CTLTYPE_NODE, "net", NULL,
2068 NULL, 0, NULL, 0,
2069 CTL_NET, CTL_EOL);
2070 sysctl_createv(clog, 0, NULL, NULL,
2071 CTLFLAG_PERMANENT,
2072 CTLTYPE_NODE, pfname, NULL,
2073 NULL, 0, NULL, 0,
2074 CTL_NET, pf, CTL_EOL);
2075 sysctl_createv(clog, 0, NULL, NULL,
2076 CTLFLAG_PERMANENT,
2077 CTLTYPE_NODE, ipname, NULL,
2078 NULL, 0, NULL, 0,
2079 CTL_NET, pf, ipn, CTL_EOL);
2080 sysctl_createv(clog, 0, NULL, NULL,
2081 CTLFLAG_PERMANENT,
2082 CTLTYPE_NODE, "ifq",
2083 SYSCTL_DESCR("Protocol input queue controls"),
2084 NULL, 0, NULL, 0,
2085 CTL_NET, pf, ipn, qid, CTL_EOL);
2087 sysctl_createv(clog, 0, NULL, NULL,
2088 CTLFLAG_PERMANENT,
2089 CTLTYPE_INT, "len",
2090 SYSCTL_DESCR("Current input queue length"),
2091 NULL, 0, &ifq->ifq_len, 0,
2092 CTL_NET, pf, ipn, qid, IFQCTL_LEN, CTL_EOL);
2093 sysctl_createv(clog, 0, NULL, NULL,
2094 CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
2095 CTLTYPE_INT, "maxlen",
2096 SYSCTL_DESCR("Maximum allowed input queue length"),
2097 NULL, 0, &ifq->ifq_maxlen, 0,
2098 CTL_NET, pf, ipn, qid, IFQCTL_MAXLEN, CTL_EOL);
2099 #ifdef notyet
2100 sysctl_createv(clog, 0, NULL, NULL,
2101 CTLFLAG_PERMANENT,
2102 CTLTYPE_INT, "peak",
2103 SYSCTL_DESCR("Highest input queue length"),
2104 NULL, 0, &ifq->ifq_peak, 0,
2105 CTL_NET, pf, ipn, qid, IFQCTL_PEAK, CTL_EOL);
2106 #endif
2107 sysctl_createv(clog, 0, NULL, NULL,
2108 CTLFLAG_PERMANENT,
2109 CTLTYPE_INT, "drops",
2110 SYSCTL_DESCR("Packets dropped due to full input queue"),
2111 NULL, 0, &ifq->ifq_drops, 0,
2112 CTL_NET, pf, ipn, qid, IFQCTL_DROPS, CTL_EOL);
2114 #endif /* INET || INET6 */