2 * Copyright (c) 1980, 1986, 1993
3 * The Regents of the University of California. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 4. Neither the name of the University nor the names of its contributors
14 * may be used to endorse or promote products derived from this software
15 * without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * @(#)if.c 8.5 (Berkeley) 1/9/95
33 #include "opt_compat.h"
34 #include "opt_inet6.h"
39 #include <sys/param.h>
40 #include <sys/types.h>
42 #include <sys/malloc.h>
46 #include <sys/systm.h>
49 #include <sys/socket.h>
50 #include <sys/socketvar.h>
51 #include <sys/protosw.h>
52 #include <sys/kernel.h>
54 #include <sys/refcount.h>
55 #include <sys/module.h>
56 #include <sys/rwlock.h>
57 #include <sys/sockio.h>
58 #include <sys/syslog.h>
59 #include <sys/sysctl.h>
60 #include <sys/taskqueue.h>
61 #include <sys/domain.h>
63 #include <machine/stdarg.h>
71 #include <net/if_arp.h>
72 #include <net/if_clone.h>
73 #include <net/if_dl.h>
74 #include <net/if_types.h>
75 #include <net/if_var.h>
76 #include <net/radix.h>
77 #include <net/route.h>
80 #if defined(INET) || defined(INET6)
82 #include <netinet/in.h>
83 #include <netinet/in_var.h>
85 #include <netinet6/in6_var.h>
86 #include <netinet6/in6_ifattach.h>
90 #include <netinet/if_ether.h>
92 #if defined(INET) || defined(INET6)
94 #include <netinet/ip_carp.h>
98 #include <security/mac/mac_framework.h>
100 struct ifindex_entry {
101 struct ifnet *ife_ifnet;
104 SYSCTL_NODE(_net, PF_LINK, link, CTLFLAG_RW, 0, "Link layers");
105 SYSCTL_NODE(_net_link, 0, generic, CTLFLAG_RW, 0, "Generic link-management");
107 TUNABLE_INT("net.link.ifqmaxlen", &ifqmaxlen);
108 SYSCTL_UINT(_net_link, OID_AUTO, ifqmaxlen, CTLFLAG_RDTUN,
109 &ifqmaxlen, 0, "max send queue size");
111 /* Log link state change events */
112 static int log_link_state_change = 1;
114 SYSCTL_INT(_net_link, OID_AUTO, log_link_state_change, CTLFLAG_RW,
115 &log_link_state_change, 0,
116 "log interface link state change events");
118 /* Interface description */
119 static unsigned int ifdescr_maxlen = 1024;
120 SYSCTL_UINT(_net, OID_AUTO, ifdescr_maxlen, CTLFLAG_RW,
122 "administrative maximum length for interface description");
124 MALLOC_DEFINE(M_IFDESCR, "ifdescr", "ifnet descriptions");
126 /* global sx for non-critical path ifdescr */
127 static struct sx ifdescr_sx;
128 SX_SYSINIT(ifdescr_sx, &ifdescr_sx, "ifnet descr");
130 void (*bstp_linkstate_p)(struct ifnet *ifp, int state);
131 void (*ng_ether_link_state_p)(struct ifnet *ifp, int state);
132 void (*lagg_linkstate_p)(struct ifnet *ifp, int state);
134 struct mbuf *(*tbr_dequeue_ptr)(struct ifaltq *, int) = NULL;
137 * XXX: Style; these should be sorted alphabetically, and unprototyped
138 * static functions should be prototyped. Currently they are sorted by
141 static void if_attachdomain(void *);
142 static void if_attachdomain1(struct ifnet *);
143 static int ifconf(u_long, caddr_t);
144 static void if_freemulti(struct ifmultiaddr *);
145 static void if_init(void *);
146 static void if_grow(void);
147 static void if_route(struct ifnet *, int flag, int fam);
148 static int if_setflag(struct ifnet *, int, int, int *, int);
149 static int if_transmit(struct ifnet *ifp, struct mbuf *m);
150 static void if_unroute(struct ifnet *, int flag, int fam);
151 static void link_rtrequest(int, struct rtentry *, struct rt_addrinfo *);
152 static int if_rtdel(struct radix_node *, void *);
153 static int ifhwioctl(u_long, struct ifnet *, caddr_t, struct thread *);
154 static int if_delmulti_locked(struct ifnet *, struct ifmultiaddr *, int);
155 static void do_link_state_change(void *, int);
156 static int if_getgroup(struct ifgroupreq *, struct ifnet *);
157 static int if_getgroupmembers(struct ifgroupreq *);
158 static void if_delgroups(struct ifnet *);
159 static void if_attach_internal(struct ifnet *, int);
160 static void if_detach_internal(struct ifnet *, int);
164 * XXX: declare here to avoid to include many inet6 related files..
165 * should be more generalized?
167 extern void nd6_setmtu(struct ifnet *);
170 VNET_DEFINE(int, if_index);
171 int ifqmaxlen = IFQ_MAXLEN;
172 VNET_DEFINE(struct ifnethead, ifnet); /* depend on static init XXX */
173 VNET_DEFINE(struct ifgrouphead, ifg_head);
175 static VNET_DEFINE(int, if_indexlim) = 8;
177 /* Table of ifnet by index. */
178 static VNET_DEFINE(struct ifindex_entry *, ifindex_table);
180 #define V_if_indexlim VNET(if_indexlim)
181 #define V_ifindex_table VNET(ifindex_table)
184 * The global network interface list (V_ifnet) and related state (such as
185 * if_index, if_indexlim, and ifindex_table) are protected by an sxlock and
186 * an rwlock. Either may be acquired shared to stablize the list, but both
187 * must be acquired writable to modify the list. This model allows us to
188 * both stablize the interface list during interrupt thread processing, but
189 * also to stablize it over long-running ioctls, without introducing priority
190 * inversions and deadlocks.
192 struct rwlock ifnet_rwlock;
193 struct sx ifnet_sxlock;
196 * The allocation of network interfaces is a rather non-atomic affair; we
197 * need to select an index before we are ready to expose the interface for
198 * use, so will use this pointer value to indicate reservation.
200 #define IFNET_HOLD (void *)(uintptr_t)(-1)
202 static if_com_alloc_t *if_com_alloc[256];
203 static if_com_free_t *if_com_free[256];
205 MALLOC_DEFINE(M_IFNET, "ifnet", "interface internals");
206 MALLOC_DEFINE(M_IFADDR, "ifaddr", "interface address");
207 MALLOC_DEFINE(M_IFMADDR, "ether_multi", "link-level multicast address");
210 ifnet_byindex_locked(u_short idx)
213 if (idx > V_if_index)
215 if (V_ifindex_table[idx].ife_ifnet == IFNET_HOLD)
217 return (V_ifindex_table[idx].ife_ifnet);
221 ifnet_byindex(u_short idx)
225 IFNET_RLOCK_NOSLEEP();
226 ifp = ifnet_byindex_locked(idx);
227 IFNET_RUNLOCK_NOSLEEP();
232 ifnet_byindex_ref(u_short idx)
236 IFNET_RLOCK_NOSLEEP();
237 ifp = ifnet_byindex_locked(idx);
238 if (ifp == NULL || (ifp->if_flags & IFF_DYING)) {
239 IFNET_RUNLOCK_NOSLEEP();
243 IFNET_RUNLOCK_NOSLEEP();
248 * Allocate an ifindex array entry; return 0 on success or an error on
252 ifindex_alloc_locked(u_short *idxp)
256 IFNET_WLOCK_ASSERT();
259 * Try to find an empty slot below V_if_index. If we fail, take the
262 for (idx = 1; idx <= V_if_index; idx++) {
263 if (V_ifindex_table[idx].ife_ifnet == NULL)
267 /* Catch if_index overflow. */
270 if (idx > V_if_index)
272 if (V_if_index >= V_if_indexlim)
279 ifindex_free_locked(u_short idx)
282 IFNET_WLOCK_ASSERT();
284 V_ifindex_table[idx].ife_ifnet = NULL;
285 while (V_if_index > 0 &&
286 V_ifindex_table[V_if_index].ife_ifnet == NULL)
291 ifindex_free(u_short idx)
295 ifindex_free_locked(idx);
300 ifnet_setbyindex_locked(u_short idx, struct ifnet *ifp)
303 IFNET_WLOCK_ASSERT();
305 V_ifindex_table[idx].ife_ifnet = ifp;
309 ifnet_setbyindex(u_short idx, struct ifnet *ifp)
313 ifnet_setbyindex_locked(idx, ifp);
318 ifaddr_byindex(u_short idx)
322 IFNET_RLOCK_NOSLEEP();
323 ifa = ifnet_byindex_locked(idx)->if_addr;
326 IFNET_RUNLOCK_NOSLEEP();
331 * Network interface utility routines.
333 * Routines with ifa_ifwith* names take sockaddr *'s as
338 vnet_if_init(const void *unused __unused)
341 TAILQ_INIT(&V_ifnet);
342 TAILQ_INIT(&V_ifg_head);
343 if_grow(); /* create initial table */
344 vnet_if_clone_init();
346 VNET_SYSINIT(vnet_if_init, SI_SUB_INIT_IF, SI_ORDER_FIRST, vnet_if_init,
351 if_init(void *dummy __unused)
357 SYSINIT(interfaces, SI_SUB_INIT_IF, SI_ORDER_SECOND, if_init, NULL);
362 vnet_if_uninit(const void *unused __unused)
365 VNET_ASSERT(TAILQ_EMPTY(&V_ifnet));
366 VNET_ASSERT(TAILQ_EMPTY(&V_ifg_head));
368 free((caddr_t)V_ifindex_table, M_IFNET);
370 VNET_SYSUNINIT(vnet_if_uninit, SI_SUB_INIT_IF, SI_ORDER_FIRST,
371 vnet_if_uninit, NULL);
378 struct ifindex_entry *e;
381 n = V_if_indexlim * sizeof(*e);
382 e = malloc(n, M_IFNET, M_WAITOK | M_ZERO);
383 if (V_ifindex_table != NULL) {
384 memcpy((caddr_t)e, (caddr_t)V_ifindex_table, n/2);
385 free((caddr_t)V_ifindex_table, M_IFNET);
391 * Allocate a struct ifnet and an index for an interface. A layer 2
392 * common structure will also be allocated if an allocation routine is
393 * registered for the passed type.
396 if_alloc(u_char type)
401 ifp = malloc(sizeof(struct ifnet), M_IFNET, M_WAITOK|M_ZERO);
403 if (ifindex_alloc_locked(&idx) != 0) {
408 ifnet_setbyindex_locked(idx, IFNET_HOLD);
412 ifp->if_alloctype = type;
413 if (if_com_alloc[type] != NULL) {
414 ifp->if_l2com = if_com_alloc[type](type, ifp);
415 if (ifp->if_l2com == NULL) {
422 IF_ADDR_LOCK_INIT(ifp);
423 TASK_INIT(&ifp->if_linktask, 0, do_link_state_change, ifp);
424 ifp->if_afdata_initialized = 0;
425 IF_AFDATA_LOCK_INIT(ifp);
426 TAILQ_INIT(&ifp->if_addrhead);
427 TAILQ_INIT(&ifp->if_prefixhead);
428 TAILQ_INIT(&ifp->if_multiaddrs);
429 TAILQ_INIT(&ifp->if_groups);
433 ifq_init(&ifp->if_snd, ifp);
435 refcount_init(&ifp->if_refcount, 1); /* Index reference. */
436 ifnet_setbyindex(ifp->if_index, ifp);
441 * Do the actual work of freeing a struct ifnet, associated index, and layer
442 * 2 common structure. This call is made when the last reference to an
443 * interface is released.
446 if_free_internal(struct ifnet *ifp)
449 KASSERT((ifp->if_flags & IFF_DYING),
450 ("if_free_internal: interface not dying"));
453 KASSERT(ifp == ifnet_byindex_locked(ifp->if_index),
454 ("%s: freeing unallocated ifnet", ifp->if_xname));
456 ifindex_free_locked(ifp->if_index);
459 if (if_com_free[ifp->if_alloctype] != NULL)
460 if_com_free[ifp->if_alloctype](ifp->if_l2com,
464 mac_ifnet_destroy(ifp);
466 if (ifp->if_description != NULL)
467 free(ifp->if_description, M_IFDESCR);
468 IF_AFDATA_DESTROY(ifp);
469 IF_ADDR_LOCK_DESTROY(ifp);
470 ifq_delete(&ifp->if_snd);
475 * This version should only be called by intefaces that switch their type
476 * after calling if_alloc(). if_free_type() will go away again now that we
477 * have if_alloctype to cache the original allocation type. For now, assert
478 * that they match, since we require that in practice.
481 if_free_type(struct ifnet *ifp, u_char type)
484 KASSERT(ifp->if_alloctype == type,
485 ("if_free_type: type (%d) != alloctype (%d)", type,
488 ifp->if_flags |= IFF_DYING; /* XXX: Locking */
489 if (!refcount_release(&ifp->if_refcount))
491 if_free_internal(ifp);
495 * This is the normal version of if_free(), used by device drivers to free a
496 * detached network interface. The contents of if_free_type() will move into
497 * here when if_free_type() goes away.
500 if_free(struct ifnet *ifp)
503 if_free_type(ifp, ifp->if_alloctype);
507 * Interfaces to keep an ifnet type-stable despite the possibility of the
508 * driver calling if_free(). If there are additional references, we defer
509 * freeing the underlying data structure.
512 if_ref(struct ifnet *ifp)
515 /* We don't assert the ifnet list lock here, but arguably should. */
516 refcount_acquire(&ifp->if_refcount);
520 if_rele(struct ifnet *ifp)
523 if (!refcount_release(&ifp->if_refcount))
525 if_free_internal(ifp);
529 ifq_init(struct ifaltq *ifq, struct ifnet *ifp)
532 mtx_init(&ifq->ifq_mtx, ifp->if_xname, "if send queue", MTX_DEF);
534 if (ifq->ifq_maxlen == 0)
535 ifq->ifq_maxlen = ifqmaxlen;
538 ifq->altq_disc = NULL;
539 ifq->altq_flags &= ALTQF_CANTCHANGE;
540 ifq->altq_tbr = NULL;
545 ifq_delete(struct ifaltq *ifq)
547 mtx_destroy(&ifq->ifq_mtx);
551 * Perform generic interface initalization tasks and attach the interface
552 * to the list of "active" interfaces. If vmove flag is set on entry
553 * to if_attach_internal(), perform only a limited subset of initialization
554 * tasks, given that we are moving from one vnet to another an ifnet which
555 * has already been fully initialized.
558 * - The decision to return void and thus require this function to
559 * succeed is questionable.
560 * - We should probably do more sanity checking. For instance we don't
561 * do anything to insure if_xname is unique or non-empty.
564 if_attach(struct ifnet *ifp)
567 if_attach_internal(ifp, 0);
571 if_attach_internal(struct ifnet *ifp, int vmove)
573 unsigned socksize, ifasize;
574 int namelen, masklen;
575 struct sockaddr_dl *sdl;
578 if (ifp->if_index == 0 || ifp != ifnet_byindex(ifp->if_index))
579 panic ("%s: BUG: if_attach called without if_alloc'd input()\n",
583 ifp->if_vnet = curvnet;
584 if (ifp->if_home_vnet == NULL)
585 ifp->if_home_vnet = curvnet;
588 if_addgroup(ifp, IFG_ALL);
590 getmicrotime(&ifp->if_lastchange);
591 ifp->if_data.ifi_epoch = time_uptime;
592 ifp->if_data.ifi_datalen = sizeof(struct if_data);
594 KASSERT((ifp->if_transmit == NULL && ifp->if_qflush == NULL) ||
595 (ifp->if_transmit != NULL && ifp->if_qflush != NULL),
596 ("transmit and qflush must both either be set or both be NULL"));
597 if (ifp->if_transmit == NULL) {
598 ifp->if_transmit = if_transmit;
599 ifp->if_qflush = if_qflush;
604 mac_ifnet_create(ifp);
608 * Create a Link Level name for this device.
610 namelen = strlen(ifp->if_xname);
612 * Always save enough space for any possiable name so we
613 * can do a rename in place later.
615 masklen = offsetof(struct sockaddr_dl, sdl_data[0]) + IFNAMSIZ;
616 socksize = masklen + ifp->if_addrlen;
617 if (socksize < sizeof(*sdl))
618 socksize = sizeof(*sdl);
619 socksize = roundup2(socksize, sizeof(long));
620 ifasize = sizeof(*ifa) + 2 * socksize;
621 ifa = malloc(ifasize, M_IFADDR, M_WAITOK | M_ZERO);
623 sdl = (struct sockaddr_dl *)(ifa + 1);
624 sdl->sdl_len = socksize;
625 sdl->sdl_family = AF_LINK;
626 bcopy(ifp->if_xname, sdl->sdl_data, namelen);
627 sdl->sdl_nlen = namelen;
628 sdl->sdl_index = ifp->if_index;
629 sdl->sdl_type = ifp->if_type;
632 ifa->ifa_rtrequest = link_rtrequest;
633 ifa->ifa_addr = (struct sockaddr *)sdl;
634 sdl = (struct sockaddr_dl *)(socksize + (caddr_t)sdl);
635 ifa->ifa_netmask = (struct sockaddr *)sdl;
636 sdl->sdl_len = masklen;
638 sdl->sdl_data[--namelen] = 0xff;
639 TAILQ_INSERT_HEAD(&ifp->if_addrhead, ifa, ifa_link);
640 /* Reliably crash if used uninitialized. */
641 ifp->if_broadcastaddr = NULL;
646 * Update the interface index in the link layer address
649 for (ifa = ifp->if_addr; ifa != NULL;
650 ifa = TAILQ_NEXT(ifa, ifa_link)) {
651 if (ifa->ifa_addr->sa_family == AF_LINK) {
652 sdl = (struct sockaddr_dl *)ifa->ifa_addr;
653 sdl->sdl_index = ifp->if_index;
660 TAILQ_INSERT_TAIL(&V_ifnet, ifp, if_link);
662 curvnet->vnet_ifcnt++;
666 if (domain_init_status >= 2)
667 if_attachdomain1(ifp);
669 EVENTHANDLER_INVOKE(ifnet_arrival_event, ifp);
670 if (IS_DEFAULT_VNET(curvnet))
671 devctl_notify("IFNET", ifp->if_xname, "ATTACH", NULL);
673 /* Announce the interface. */
674 rt_ifannouncemsg(ifp, IFAN_ARRIVAL);
678 if_attachdomain(void *dummy)
684 TAILQ_FOREACH(ifp, &V_ifnet, if_link)
685 if_attachdomain1(ifp);
688 SYSINIT(domainifattach, SI_SUB_PROTO_IFATTACHDOMAIN, SI_ORDER_SECOND,
689 if_attachdomain, NULL);
692 if_attachdomain1(struct ifnet *ifp)
700 * Since dp->dom_ifattach calls malloc() with M_WAITOK, we
701 * cannot lock ifp->if_afdata initialization, entirely.
703 if (IF_AFDATA_TRYLOCK(ifp) == 0) {
707 if (ifp->if_afdata_initialized >= domain_init_status) {
708 IF_AFDATA_UNLOCK(ifp);
710 printf("if_attachdomain called more than once on %s\n",
714 ifp->if_afdata_initialized = domain_init_status;
715 IF_AFDATA_UNLOCK(ifp);
717 /* address family dependent data region */
718 bzero(ifp->if_afdata, sizeof(ifp->if_afdata));
719 for (dp = domains; dp; dp = dp->dom_next) {
720 if (dp->dom_ifattach)
721 ifp->if_afdata[dp->dom_family] =
722 (*dp->dom_ifattach)(ifp);
729 * Remove any unicast or broadcast network addresses from an interface.
732 if_purgeaddrs(struct ifnet *ifp)
734 struct ifaddr *ifa, *next;
736 TAILQ_FOREACH_SAFE(ifa, &ifp->if_addrhead, ifa_link, next) {
737 if (ifa->ifa_addr->sa_family == AF_LINK)
740 /* XXX: Ugly!! ad hoc just for INET */
741 if (ifa->ifa_addr->sa_family == AF_INET) {
742 struct ifaliasreq ifr;
744 bzero(&ifr, sizeof(ifr));
745 ifr.ifra_addr = *ifa->ifa_addr;
746 if (ifa->ifa_dstaddr)
747 ifr.ifra_broadaddr = *ifa->ifa_dstaddr;
748 if (in_control(NULL, SIOCDIFADDR, (caddr_t)&ifr, ifp,
754 if (ifa->ifa_addr->sa_family == AF_INET6) {
756 /* ifp_addrhead is already updated */
760 TAILQ_REMOVE(&ifp->if_addrhead, ifa, ifa_link);
766 * Remove any multicast network addresses from an interface when an ifnet
770 if_purgemaddrs(struct ifnet *ifp)
772 struct ifmultiaddr *ifma;
773 struct ifmultiaddr *next;
776 TAILQ_FOREACH_SAFE(ifma, &ifp->if_multiaddrs, ifma_link, next)
777 if_delmulti_locked(ifp, ifma, 1);
782 * Detach an interface, removing it from the list of "active" interfaces.
783 * If vmove flag is set on entry to if_detach_internal(), perform only a
784 * limited subset of cleanup tasks, given that we are moving an ifnet from
785 * one vnet to another, where it must be fully operational.
787 * XXXRW: There are some significant questions about event ordering, and
788 * how to prevent things from starting to use the interface during detach.
791 if_detach(struct ifnet *ifp)
794 if_detach_internal(ifp, 0);
798 if_detach_internal(struct ifnet *ifp, int vmove)
801 struct radix_node_head *rnh;
808 TAILQ_FOREACH(iter, &V_ifnet, if_link)
810 TAILQ_REMOVE(&V_ifnet, ifp, if_link);
816 curvnet->vnet_ifcnt--;
821 panic("%s: ifp=%p not on the ifnet tailq %p",
822 __func__, ifp, &V_ifnet);
824 return; /* XXX this should panic as well? */
828 * Remove/wait for pending events.
830 taskqueue_drain(taskqueue_swi, &ifp->if_linktask);
833 * Remove routes and flush queues.
837 if (ALTQ_IS_ENABLED(&ifp->if_snd))
838 altq_disable(&ifp->if_snd);
839 if (ALTQ_IS_ATTACHED(&ifp->if_snd))
840 altq_detach(&ifp->if_snd);
851 * Remove all IPv6 kernel structs related to ifp. This should be done
852 * before removing routing entries below, since IPv6 interface direct
853 * routes are expected to be removed by the IPv6-specific kernel API.
854 * Otherwise, the kernel will detect some inconsistency and bark it.
862 * Prevent further calls into the device driver via ifnet.
867 * Remove link ifaddr pointer and maybe decrement if_index.
868 * Clean up all addresses.
872 /* We can now free link ifaddr. */
873 if (!TAILQ_EMPTY(&ifp->if_addrhead)) {
874 ifa = TAILQ_FIRST(&ifp->if_addrhead);
875 TAILQ_REMOVE(&ifp->if_addrhead, ifa, ifa_link);
881 * Delete all remaining routes using this interface
882 * Unfortuneatly the only way to do this is to slog through
883 * the entire routing table looking for routes which point
884 * to this interface...oh well...
886 for (i = 1; i <= AF_MAX; i++) {
887 for (j = 0; j < rt_numfibs; j++) {
888 rnh = rt_tables_get_rnh(j, i);
891 RADIX_NODE_HEAD_LOCK(rnh);
892 (void) rnh->rnh_walktree(rnh, if_rtdel, ifp);
893 RADIX_NODE_HEAD_UNLOCK(rnh);
897 /* Announce that the interface is gone. */
898 rt_ifannouncemsg(ifp, IFAN_DEPARTURE);
899 EVENTHANDLER_INVOKE(ifnet_departure_event, ifp);
900 if (IS_DEFAULT_VNET(curvnet))
901 devctl_notify("IFNET", ifp->if_xname, "DETACH", NULL);
905 * We cannot hold the lock over dom_ifdetach calls as they might
906 * sleep, for example trying to drain a callout, thus open up the
907 * theoretical race with re-attaching.
910 i = ifp->if_afdata_initialized;
911 ifp->if_afdata_initialized = 0;
912 IF_AFDATA_UNLOCK(ifp);
913 for (dp = domains; i > 0 && dp; dp = dp->dom_next) {
914 if (dp->dom_ifdetach && ifp->if_afdata[dp->dom_family])
915 (*dp->dom_ifdetach)(ifp,
916 ifp->if_afdata[dp->dom_family]);
922 * if_vmove() performs a limited version of if_detach() in current
923 * vnet and if_attach()es the ifnet to the vnet specified as 2nd arg.
924 * An attempt is made to shrink if_index in current vnet, find an
925 * unused if_index in target vnet and calls if_grow() if necessary,
926 * and finally find an unused if_xname for the target vnet.
929 if_vmove(struct ifnet *ifp, struct vnet *new_vnet)
934 * Detach from current vnet, but preserve LLADDR info, do not
935 * mark as dead etc. so that the ifnet can be reattached later.
937 if_detach_internal(ifp, 1);
940 * Unlink the ifnet from ifindex_table[] in current vnet, and shrink
941 * the if_index for that vnet if possible.
943 * NOTE: IFNET_WLOCK/IFNET_WUNLOCK() are assumed to be unvirtualized,
944 * or we'd lock on one vnet and unlock on another.
947 ifindex_free_locked(ifp->if_index);
950 * Switch to the context of the target vnet.
952 CURVNET_SET_QUIET(new_vnet);
954 if (ifindex_alloc_locked(&idx) != 0) {
956 panic("if_index overflow");
959 ifnet_setbyindex_locked(ifp->if_index, ifp);
962 if_attach_internal(ifp, 1);
968 * Move an ifnet to or from another child prison/vnet, specified by the jail id.
971 if_vmove_loan(struct thread *td, struct ifnet *ifp, char *ifname, int jid)
976 /* Try to find the prison within our visibility. */
977 sx_slock(&allprison_lock);
978 pr = prison_find_child(td->td_ucred->cr_prison, jid);
979 sx_sunlock(&allprison_lock);
982 prison_hold_locked(pr);
983 mtx_unlock(&pr->pr_mtx);
985 /* Do not try to move the iface from and to the same prison. */
986 if (pr->pr_vnet == ifp->if_vnet) {
991 /* Make sure the named iface does not exists in the dst. prison/vnet. */
992 /* XXX Lock interfaces to avoid races. */
993 CURVNET_SET_QUIET(pr->pr_vnet);
994 difp = ifunit(ifname);
1001 /* Move the interface into the child jail/vnet. */
1002 if_vmove(ifp, pr->pr_vnet);
1004 /* Report the new if_xname back to the userland. */
1005 sprintf(ifname, "%s", ifp->if_xname);
1012 if_vmove_reclaim(struct thread *td, char *ifname, int jid)
1015 struct vnet *vnet_dst;
1018 /* Try to find the prison within our visibility. */
1019 sx_slock(&allprison_lock);
1020 pr = prison_find_child(td->td_ucred->cr_prison, jid);
1021 sx_sunlock(&allprison_lock);
1024 prison_hold_locked(pr);
1025 mtx_unlock(&pr->pr_mtx);
1027 /* Make sure the named iface exists in the source prison/vnet. */
1028 CURVNET_SET(pr->pr_vnet);
1029 ifp = ifunit(ifname); /* XXX Lock to avoid races. */
1036 /* Do not try to move the iface from and to the same prison. */
1037 vnet_dst = TD_TO_VNET(td);
1038 if (vnet_dst == ifp->if_vnet) {
1044 /* Get interface back from child jail/vnet. */
1045 if_vmove(ifp, vnet_dst);
1048 /* Report the new if_xname back to the userland. */
1049 sprintf(ifname, "%s", ifp->if_xname);
1057 * Add a group to an interface
1060 if_addgroup(struct ifnet *ifp, const char *groupname)
1062 struct ifg_list *ifgl;
1063 struct ifg_group *ifg = NULL;
1064 struct ifg_member *ifgm;
1066 if (groupname[0] && groupname[strlen(groupname) - 1] >= '0' &&
1067 groupname[strlen(groupname) - 1] <= '9')
1071 TAILQ_FOREACH(ifgl, &ifp->if_groups, ifgl_next)
1072 if (!strcmp(ifgl->ifgl_group->ifg_group, groupname)) {
1077 if ((ifgl = (struct ifg_list *)malloc(sizeof(struct ifg_list), M_TEMP,
1078 M_NOWAIT)) == NULL) {
1083 if ((ifgm = (struct ifg_member *)malloc(sizeof(struct ifg_member),
1084 M_TEMP, M_NOWAIT)) == NULL) {
1090 TAILQ_FOREACH(ifg, &V_ifg_head, ifg_next)
1091 if (!strcmp(ifg->ifg_group, groupname))
1095 if ((ifg = (struct ifg_group *)malloc(sizeof(struct ifg_group),
1096 M_TEMP, M_NOWAIT)) == NULL) {
1102 strlcpy(ifg->ifg_group, groupname, sizeof(ifg->ifg_group));
1103 ifg->ifg_refcnt = 0;
1104 TAILQ_INIT(&ifg->ifg_members);
1105 EVENTHANDLER_INVOKE(group_attach_event, ifg);
1106 TAILQ_INSERT_TAIL(&V_ifg_head, ifg, ifg_next);
1110 ifgl->ifgl_group = ifg;
1111 ifgm->ifgm_ifp = ifp;
1114 TAILQ_INSERT_TAIL(&ifg->ifg_members, ifgm, ifgm_next);
1115 TAILQ_INSERT_TAIL(&ifp->if_groups, ifgl, ifgl_next);
1116 IF_ADDR_UNLOCK(ifp);
1120 EVENTHANDLER_INVOKE(group_change_event, groupname);
1126 * Remove a group from an interface
1129 if_delgroup(struct ifnet *ifp, const char *groupname)
1131 struct ifg_list *ifgl;
1132 struct ifg_member *ifgm;
1135 TAILQ_FOREACH(ifgl, &ifp->if_groups, ifgl_next)
1136 if (!strcmp(ifgl->ifgl_group->ifg_group, groupname))
1144 TAILQ_REMOVE(&ifp->if_groups, ifgl, ifgl_next);
1145 IF_ADDR_UNLOCK(ifp);
1147 TAILQ_FOREACH(ifgm, &ifgl->ifgl_group->ifg_members, ifgm_next)
1148 if (ifgm->ifgm_ifp == ifp)
1152 TAILQ_REMOVE(&ifgl->ifgl_group->ifg_members, ifgm, ifgm_next);
1156 if (--ifgl->ifgl_group->ifg_refcnt == 0) {
1157 TAILQ_REMOVE(&V_ifg_head, ifgl->ifgl_group, ifg_next);
1158 EVENTHANDLER_INVOKE(group_detach_event, ifgl->ifgl_group);
1159 free(ifgl->ifgl_group, M_TEMP);
1165 EVENTHANDLER_INVOKE(group_change_event, groupname);
1171 * Remove an interface from all groups
1174 if_delgroups(struct ifnet *ifp)
1176 struct ifg_list *ifgl;
1177 struct ifg_member *ifgm;
1178 char groupname[IFNAMSIZ];
1181 while (!TAILQ_EMPTY(&ifp->if_groups)) {
1182 ifgl = TAILQ_FIRST(&ifp->if_groups);
1184 strlcpy(groupname, ifgl->ifgl_group->ifg_group, IFNAMSIZ);
1187 TAILQ_REMOVE(&ifp->if_groups, ifgl, ifgl_next);
1188 IF_ADDR_UNLOCK(ifp);
1190 TAILQ_FOREACH(ifgm, &ifgl->ifgl_group->ifg_members, ifgm_next)
1191 if (ifgm->ifgm_ifp == ifp)
1195 TAILQ_REMOVE(&ifgl->ifgl_group->ifg_members, ifgm,
1200 if (--ifgl->ifgl_group->ifg_refcnt == 0) {
1201 TAILQ_REMOVE(&V_ifg_head, ifgl->ifgl_group, ifg_next);
1202 EVENTHANDLER_INVOKE(group_detach_event,
1204 free(ifgl->ifgl_group, M_TEMP);
1210 EVENTHANDLER_INVOKE(group_change_event, groupname);
1218 * Stores all groups from an interface in memory pointed
1222 if_getgroup(struct ifgroupreq *data, struct ifnet *ifp)
1225 struct ifg_list *ifgl;
1226 struct ifg_req ifgrq, *ifgp;
1227 struct ifgroupreq *ifgr = data;
1229 if (ifgr->ifgr_len == 0) {
1231 TAILQ_FOREACH(ifgl, &ifp->if_groups, ifgl_next)
1232 ifgr->ifgr_len += sizeof(struct ifg_req);
1233 IF_ADDR_UNLOCK(ifp);
1237 len = ifgr->ifgr_len;
1238 ifgp = ifgr->ifgr_groups;
1241 TAILQ_FOREACH(ifgl, &ifp->if_groups, ifgl_next) {
1242 if (len < sizeof(ifgrq)) {
1243 IF_ADDR_UNLOCK(ifp);
1246 bzero(&ifgrq, sizeof ifgrq);
1247 strlcpy(ifgrq.ifgrq_group, ifgl->ifgl_group->ifg_group,
1248 sizeof(ifgrq.ifgrq_group));
1249 if ((error = copyout(&ifgrq, ifgp, sizeof(struct ifg_req)))) {
1250 IF_ADDR_UNLOCK(ifp);
1253 len -= sizeof(ifgrq);
1256 IF_ADDR_UNLOCK(ifp);
1262 * Stores all members of a group in memory pointed to by data
1265 if_getgroupmembers(struct ifgroupreq *data)
1267 struct ifgroupreq *ifgr = data;
1268 struct ifg_group *ifg;
1269 struct ifg_member *ifgm;
1270 struct ifg_req ifgrq, *ifgp;
1274 TAILQ_FOREACH(ifg, &V_ifg_head, ifg_next)
1275 if (!strcmp(ifg->ifg_group, ifgr->ifgr_name))
1282 if (ifgr->ifgr_len == 0) {
1283 TAILQ_FOREACH(ifgm, &ifg->ifg_members, ifgm_next)
1284 ifgr->ifgr_len += sizeof(ifgrq);
1289 len = ifgr->ifgr_len;
1290 ifgp = ifgr->ifgr_groups;
1291 TAILQ_FOREACH(ifgm, &ifg->ifg_members, ifgm_next) {
1292 if (len < sizeof(ifgrq)) {
1296 bzero(&ifgrq, sizeof ifgrq);
1297 strlcpy(ifgrq.ifgrq_member, ifgm->ifgm_ifp->if_xname,
1298 sizeof(ifgrq.ifgrq_member));
1299 if ((error = copyout(&ifgrq, ifgp, sizeof(struct ifg_req)))) {
1303 len -= sizeof(ifgrq);
1312 * Delete Routes for a Network Interface
1314 * Called for each routing entry via the rnh->rnh_walktree() call above
1315 * to delete all route entries referencing a detaching network interface.
1318 * rn pointer to node in the routing table
1319 * arg argument passed to rnh->rnh_walktree() - detaching interface
1323 * errno failed - reason indicated
1327 if_rtdel(struct radix_node *rn, void *arg)
1329 struct rtentry *rt = (struct rtentry *)rn;
1330 struct ifnet *ifp = arg;
1333 if (rt->rt_ifp == ifp) {
1336 * Protect (sorta) against walktree recursion problems
1337 * with cloned routes
1339 if ((rt->rt_flags & RTF_UP) == 0)
1342 err = rtrequest_fib(RTM_DELETE, rt_key(rt), rt->rt_gateway,
1343 rt_mask(rt), rt->rt_flags|RTF_RNH_LOCKED,
1344 (struct rtentry **) NULL, rt->rt_fibnum);
1346 log(LOG_WARNING, "if_rtdel: error %d\n", err);
1354 * Wrapper functions for struct ifnet address list locking macros. These are
1355 * used by kernel modules to avoid encoding programming interface or binary
1356 * interface assumptions that may be violated when kernel-internal locking
1357 * approaches change.
1360 if_addr_rlock(struct ifnet *ifp)
1367 if_addr_runlock(struct ifnet *ifp)
1370 IF_ADDR_UNLOCK(ifp);
1374 if_maddr_rlock(struct ifnet *ifp)
1381 if_maddr_runlock(struct ifnet *ifp)
1384 IF_ADDR_UNLOCK(ifp);
1388 * Reference count functions for ifaddrs.
1391 ifa_init(struct ifaddr *ifa)
1394 mtx_init(&ifa->ifa_mtx, "ifaddr", NULL, MTX_DEF);
1395 refcount_init(&ifa->ifa_refcnt, 1);
1399 ifa_ref(struct ifaddr *ifa)
1402 refcount_acquire(&ifa->ifa_refcnt);
1406 ifa_free(struct ifaddr *ifa)
1409 if (refcount_release(&ifa->ifa_refcnt)) {
1410 mtx_destroy(&ifa->ifa_mtx);
1411 free(ifa, M_IFADDR);
1416 ifa_add_loopback_route(struct ifaddr *ifa, struct sockaddr *ia)
1419 struct rtentry *rt = NULL;
1420 struct rt_addrinfo info;
1421 static struct sockaddr_dl null_sdl = {sizeof(null_sdl), AF_LINK};
1423 bzero(&info, sizeof(info));
1424 info.rti_ifp = V_loif;
1425 info.rti_flags = ifa->ifa_flags | RTF_HOST | RTF_STATIC;
1426 info.rti_info[RTAX_DST] = ia;
1427 info.rti_info[RTAX_GATEWAY] = (struct sockaddr *)&null_sdl;
1428 error = rtrequest1_fib(RTM_ADD, &info, &rt, 0);
1430 if (error == 0 && rt != NULL) {
1432 ((struct sockaddr_dl *)rt->rt_gateway)->sdl_type =
1433 ifa->ifa_ifp->if_type;
1434 ((struct sockaddr_dl *)rt->rt_gateway)->sdl_index =
1435 ifa->ifa_ifp->if_index;
1438 } else if (error != 0)
1439 log(LOG_INFO, "ifa_add_loopback_route: insertion failed\n");
1445 ifa_del_loopback_route(struct ifaddr *ifa, struct sockaddr *ia)
1448 struct rt_addrinfo info;
1449 struct sockaddr_dl null_sdl;
1451 bzero(&null_sdl, sizeof(null_sdl));
1452 null_sdl.sdl_len = sizeof(null_sdl);
1453 null_sdl.sdl_family = AF_LINK;
1454 null_sdl.sdl_type = ifa->ifa_ifp->if_type;
1455 null_sdl.sdl_index = ifa->ifa_ifp->if_index;
1456 bzero(&info, sizeof(info));
1457 info.rti_flags = ifa->ifa_flags | RTF_HOST | RTF_STATIC;
1458 info.rti_info[RTAX_DST] = ia;
1459 info.rti_info[RTAX_GATEWAY] = (struct sockaddr *)&null_sdl;
1460 error = rtrequest1_fib(RTM_DELETE, &info, NULL, 0);
1463 log(LOG_INFO, "ifa_del_loopback_route: deletion failed\n");
1469 * XXX: Because sockaddr_dl has deeper structure than the sockaddr
1470 * structs used to represent other address families, it is necessary
1471 * to perform a different comparison.
1474 #define sa_equal(a1, a2) \
1475 (bcmp((a1), (a2), ((a1))->sa_len) == 0)
1477 #define sa_dl_equal(a1, a2) \
1478 ((((struct sockaddr_dl *)(a1))->sdl_len == \
1479 ((struct sockaddr_dl *)(a2))->sdl_len) && \
1480 (bcmp(LLADDR((struct sockaddr_dl *)(a1)), \
1481 LLADDR((struct sockaddr_dl *)(a2)), \
1482 ((struct sockaddr_dl *)(a1))->sdl_alen) == 0))
1485 * Locate an interface based on a complete address.
1488 static struct ifaddr *
1489 ifa_ifwithaddr_internal(struct sockaddr *addr, int getref)
1494 IFNET_RLOCK_NOSLEEP();
1495 TAILQ_FOREACH(ifp, &V_ifnet, if_link) {
1497 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
1498 if (ifa->ifa_addr->sa_family != addr->sa_family)
1500 if (sa_equal(addr, ifa->ifa_addr)) {
1503 IF_ADDR_UNLOCK(ifp);
1506 /* IP6 doesn't have broadcast */
1507 if ((ifp->if_flags & IFF_BROADCAST) &&
1508 ifa->ifa_broadaddr &&
1509 ifa->ifa_broadaddr->sa_len != 0 &&
1510 sa_equal(ifa->ifa_broadaddr, addr)) {
1513 IF_ADDR_UNLOCK(ifp);
1517 IF_ADDR_UNLOCK(ifp);
1521 IFNET_RUNLOCK_NOSLEEP();
1526 ifa_ifwithaddr(struct sockaddr *addr)
1529 return (ifa_ifwithaddr_internal(addr, 1));
1533 ifa_ifwithaddr_check(struct sockaddr *addr)
1536 return (ifa_ifwithaddr_internal(addr, 0) != NULL);
1540 * Locate an interface based on the broadcast address.
1544 ifa_ifwithbroadaddr(struct sockaddr *addr)
1549 IFNET_RLOCK_NOSLEEP();
1550 TAILQ_FOREACH(ifp, &V_ifnet, if_link) {
1552 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
1553 if (ifa->ifa_addr->sa_family != addr->sa_family)
1555 if ((ifp->if_flags & IFF_BROADCAST) &&
1556 ifa->ifa_broadaddr &&
1557 ifa->ifa_broadaddr->sa_len != 0 &&
1558 sa_equal(ifa->ifa_broadaddr, addr)) {
1560 IF_ADDR_UNLOCK(ifp);
1564 IF_ADDR_UNLOCK(ifp);
1568 IFNET_RUNLOCK_NOSLEEP();
1573 * Locate the point to point interface with a given destination address.
1577 ifa_ifwithdstaddr(struct sockaddr *addr)
1582 IFNET_RLOCK_NOSLEEP();
1583 TAILQ_FOREACH(ifp, &V_ifnet, if_link) {
1584 if ((ifp->if_flags & IFF_POINTOPOINT) == 0)
1587 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
1588 if (ifa->ifa_addr->sa_family != addr->sa_family)
1590 if (ifa->ifa_dstaddr != NULL &&
1591 sa_equal(addr, ifa->ifa_dstaddr)) {
1593 IF_ADDR_UNLOCK(ifp);
1597 IF_ADDR_UNLOCK(ifp);
1601 IFNET_RUNLOCK_NOSLEEP();
1606 * Find an interface on a specific network. If many, choice
1607 * is most specific found.
1610 ifa_ifwithnet(struct sockaddr *addr)
1614 struct ifaddr *ifa_maybe = NULL;
1615 u_int af = addr->sa_family;
1616 char *addr_data = addr->sa_data, *cplim;
1619 * AF_LINK addresses can be looked up directly by their index number,
1620 * so do that if we can.
1622 if (af == AF_LINK) {
1623 struct sockaddr_dl *sdl = (struct sockaddr_dl *)addr;
1624 if (sdl->sdl_index && sdl->sdl_index <= V_if_index)
1625 return (ifaddr_byindex(sdl->sdl_index));
1629 * Scan though each interface, looking for ones that have addresses
1630 * in this address family. Maintain a reference on ifa_maybe once
1631 * we find one, as we release the IF_ADDR_LOCK() that kept it stable
1632 * when we move onto the next interface.
1634 IFNET_RLOCK_NOSLEEP();
1635 TAILQ_FOREACH(ifp, &V_ifnet, if_link) {
1637 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
1638 char *cp, *cp2, *cp3;
1640 if (ifa->ifa_addr->sa_family != af)
1642 if (af == AF_INET && ifp->if_flags & IFF_POINTOPOINT) {
1644 * This is a bit broken as it doesn't
1645 * take into account that the remote end may
1646 * be a single node in the network we are
1648 * The trouble is that we don't know the
1649 * netmask for the remote end.
1651 if (ifa->ifa_dstaddr != NULL &&
1652 sa_equal(addr, ifa->ifa_dstaddr)) {
1654 IF_ADDR_UNLOCK(ifp);
1659 * if we have a special address handler,
1660 * then use it instead of the generic one.
1662 if (ifa->ifa_claim_addr) {
1663 if ((*ifa->ifa_claim_addr)(ifa, addr)) {
1665 IF_ADDR_UNLOCK(ifp);
1672 * Scan all the bits in the ifa's address.
1673 * If a bit dissagrees with what we are
1674 * looking for, mask it with the netmask
1675 * to see if it really matters.
1676 * (A byte at a time)
1678 if (ifa->ifa_netmask == 0)
1681 cp2 = ifa->ifa_addr->sa_data;
1682 cp3 = ifa->ifa_netmask->sa_data;
1683 cplim = ifa->ifa_netmask->sa_len
1684 + (char *)ifa->ifa_netmask;
1686 if ((*cp++ ^ *cp2++) & *cp3++)
1687 goto next; /* next address! */
1689 * If the netmask of what we just found
1690 * is more specific than what we had before
1691 * (if we had one) then remember the new one
1692 * before continuing to search
1693 * for an even better one.
1695 if (ifa_maybe == NULL ||
1696 rn_refines((caddr_t)ifa->ifa_netmask,
1697 (caddr_t)ifa_maybe->ifa_netmask)) {
1698 if (ifa_maybe != NULL)
1699 ifa_free(ifa_maybe);
1705 IF_ADDR_UNLOCK(ifp);
1710 IFNET_RUNLOCK_NOSLEEP();
1711 if (ifa_maybe != NULL)
1712 ifa_free(ifa_maybe);
1717 * Find an interface address specific to an interface best matching
1721 ifaof_ifpforaddr(struct sockaddr *addr, struct ifnet *ifp)
1724 char *cp, *cp2, *cp3;
1726 struct ifaddr *ifa_maybe = NULL;
1727 u_int af = addr->sa_family;
1732 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
1733 if (ifa->ifa_addr->sa_family != af)
1735 if (ifa_maybe == NULL)
1737 if (ifa->ifa_netmask == 0) {
1738 if (sa_equal(addr, ifa->ifa_addr) ||
1739 (ifa->ifa_dstaddr &&
1740 sa_equal(addr, ifa->ifa_dstaddr)))
1744 if (ifp->if_flags & IFF_POINTOPOINT) {
1745 if (sa_equal(addr, ifa->ifa_dstaddr))
1749 cp2 = ifa->ifa_addr->sa_data;
1750 cp3 = ifa->ifa_netmask->sa_data;
1751 cplim = ifa->ifa_netmask->sa_len + (char *)ifa->ifa_netmask;
1752 for (; cp3 < cplim; cp3++)
1753 if ((*cp++ ^ *cp2++) & *cp3)
1763 IF_ADDR_UNLOCK(ifp);
1767 #include <net/if_llatbl.h>
1770 * Default action when installing a route with a Link Level gateway.
1771 * Lookup an appropriate real ifa to point to.
1772 * This should be moved to /sys/net/link.c eventually.
1775 link_rtrequest(int cmd, struct rtentry *rt, struct rt_addrinfo *info)
1777 struct ifaddr *ifa, *oifa;
1778 struct sockaddr *dst;
1783 if (cmd != RTM_ADD || ((ifa = rt->rt_ifa) == 0) ||
1784 ((ifp = ifa->ifa_ifp) == 0) || ((dst = rt_key(rt)) == 0))
1786 ifa = ifaof_ifpforaddr(dst, ifp);
1791 if (ifa->ifa_rtrequest && ifa->ifa_rtrequest != link_rtrequest)
1792 ifa->ifa_rtrequest(cmd, rt, info);
1797 * Mark an interface down and notify protocols of
1799 * NOTE: must be called at splnet or eqivalent.
1802 if_unroute(struct ifnet *ifp, int flag, int fam)
1806 KASSERT(flag == IFF_UP, ("if_unroute: flag != IFF_UP"));
1808 ifp->if_flags &= ~flag;
1809 getmicrotime(&ifp->if_lastchange);
1810 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link)
1811 if (fam == PF_UNSPEC || (fam == ifa->ifa_addr->sa_family))
1812 pfctlinput(PRC_IFDOWN, ifa->ifa_addr);
1813 ifp->if_qflush(ifp);
1815 #if defined(INET) || defined(INET6)
1818 carp_carpdev_state(ifp->if_carp);
1825 * Mark an interface up and notify protocols of
1827 * NOTE: must be called at splnet or eqivalent.
1830 if_route(struct ifnet *ifp, int flag, int fam)
1834 KASSERT(flag == IFF_UP, ("if_route: flag != IFF_UP"));
1836 ifp->if_flags |= flag;
1837 getmicrotime(&ifp->if_lastchange);
1838 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link)
1839 if (fam == PF_UNSPEC || (fam == ifa->ifa_addr->sa_family))
1840 pfctlinput(PRC_IFUP, ifa->ifa_addr);
1841 #if defined(INET) || defined(INET6)
1844 carp_carpdev_state(ifp->if_carp);
1853 void (*vlan_link_state_p)(struct ifnet *); /* XXX: private from if_vlan */
1854 void (*vlan_trunk_cap_p)(struct ifnet *); /* XXX: private from if_vlan */
1857 * Handle a change in the interface link state. To avoid LORs
1858 * between driver lock and upper layer locks, as well as possible
1859 * recursions, we post event to taskqueue, and all job
1860 * is done in static do_link_state_change().
1863 if_link_state_change(struct ifnet *ifp, int link_state)
1865 /* Return if state hasn't changed. */
1866 if (ifp->if_link_state == link_state)
1869 ifp->if_link_state = link_state;
1871 taskqueue_enqueue(taskqueue_swi, &ifp->if_linktask);
1875 do_link_state_change(void *arg, int pending)
1877 struct ifnet *ifp = (struct ifnet *)arg;
1878 int link_state = ifp->if_link_state;
1879 CURVNET_SET(ifp->if_vnet);
1881 /* Notify that the link state has changed. */
1883 if (ifp->if_vlantrunk != NULL)
1884 (*vlan_link_state_p)(ifp);
1886 if ((ifp->if_type == IFT_ETHER || ifp->if_type == IFT_L2VLAN) &&
1887 IFP2AC(ifp)->ac_netgraph != NULL)
1888 (*ng_ether_link_state_p)(ifp, link_state);
1889 #if defined(INET) || defined(INET6)
1892 carp_carpdev_state(ifp->if_carp);
1895 if (ifp->if_bridge) {
1896 KASSERT(bstp_linkstate_p != NULL,("if_bridge bstp not loaded!"));
1897 (*bstp_linkstate_p)(ifp, link_state);
1900 KASSERT(lagg_linkstate_p != NULL,("if_lagg not loaded!"));
1901 (*lagg_linkstate_p)(ifp, link_state);
1904 if (IS_DEFAULT_VNET(curvnet))
1905 devctl_notify("IFNET", ifp->if_xname,
1906 (link_state == LINK_STATE_UP) ? "LINK_UP" : "LINK_DOWN",
1909 if_printf(ifp, "%d link states coalesced\n", pending);
1910 if (log_link_state_change)
1911 log(LOG_NOTICE, "%s: link state changed to %s\n", ifp->if_xname,
1912 (link_state == LINK_STATE_UP) ? "UP" : "DOWN" );
1917 * Mark an interface down and notify protocols of
1919 * NOTE: must be called at splnet or eqivalent.
1922 if_down(struct ifnet *ifp)
1925 if_unroute(ifp, IFF_UP, AF_UNSPEC);
1929 * Mark an interface up and notify protocols of
1931 * NOTE: must be called at splnet or eqivalent.
1934 if_up(struct ifnet *ifp)
1937 if_route(ifp, IFF_UP, AF_UNSPEC);
1941 * Flush an interface queue.
1944 if_qflush(struct ifnet *ifp)
1952 if (ALTQ_IS_ENABLED(ifq))
1956 while ((m = n) != 0) {
1967 * Map interface name to interface structure pointer, with or without
1968 * returning a reference.
1971 ifunit_ref(const char *name)
1975 IFNET_RLOCK_NOSLEEP();
1976 TAILQ_FOREACH(ifp, &V_ifnet, if_link) {
1977 if (strncmp(name, ifp->if_xname, IFNAMSIZ) == 0 &&
1978 !(ifp->if_flags & IFF_DYING))
1983 IFNET_RUNLOCK_NOSLEEP();
1988 ifunit(const char *name)
1992 IFNET_RLOCK_NOSLEEP();
1993 TAILQ_FOREACH(ifp, &V_ifnet, if_link) {
1994 if (strncmp(name, ifp->if_xname, IFNAMSIZ) == 0)
1997 IFNET_RUNLOCK_NOSLEEP();
2002 * Hardware specific interface ioctls.
2005 ifhwioctl(u_long cmd, struct ifnet *ifp, caddr_t data, struct thread *td)
2010 int new_flags, temp_flags;
2011 size_t namelen, onamelen;
2013 char *descrbuf, *odescrbuf;
2014 char new_name[IFNAMSIZ];
2016 struct sockaddr_dl *sdl;
2018 ifr = (struct ifreq *)data;
2021 ifr->ifr_index = ifp->if_index;
2025 temp_flags = ifp->if_flags | ifp->if_drv_flags;
2026 ifr->ifr_flags = temp_flags & 0xffff;
2027 ifr->ifr_flagshigh = temp_flags >> 16;
2031 ifr->ifr_reqcap = ifp->if_capabilities;
2032 ifr->ifr_curcap = ifp->if_capenable;
2037 error = mac_ifnet_ioctl_get(td->td_ucred, ifr, ifp);
2042 ifr->ifr_metric = ifp->if_metric;
2046 ifr->ifr_mtu = ifp->if_mtu;
2050 ifr->ifr_phys = ifp->if_physical;
2055 sx_slock(&ifdescr_sx);
2056 if (ifp->if_description == NULL)
2059 /* space for terminating nul */
2060 descrlen = strlen(ifp->if_description) + 1;
2061 if (ifr->ifr_buffer.length < descrlen)
2062 ifr->ifr_buffer.buffer = NULL;
2064 error = copyout(ifp->if_description,
2065 ifr->ifr_buffer.buffer, descrlen);
2066 ifr->ifr_buffer.length = descrlen;
2068 sx_sunlock(&ifdescr_sx);
2072 error = priv_check(td, PRIV_NET_SETIFDESCR);
2077 * Copy only (length-1) bytes to make sure that
2078 * if_description is always nul terminated. The
2079 * length parameter is supposed to count the
2080 * terminating nul in.
2082 if (ifr->ifr_buffer.length > ifdescr_maxlen)
2083 return (ENAMETOOLONG);
2084 else if (ifr->ifr_buffer.length == 0)
2087 descrbuf = malloc(ifr->ifr_buffer.length, M_IFDESCR,
2089 error = copyin(ifr->ifr_buffer.buffer, descrbuf,
2090 ifr->ifr_buffer.length - 1);
2092 free(descrbuf, M_IFDESCR);
2097 sx_xlock(&ifdescr_sx);
2098 odescrbuf = ifp->if_description;
2099 ifp->if_description = descrbuf;
2100 sx_xunlock(&ifdescr_sx);
2102 getmicrotime(&ifp->if_lastchange);
2103 free(odescrbuf, M_IFDESCR);
2107 error = priv_check(td, PRIV_NET_SETIFFLAGS);
2111 * Currently, no driver owned flags pass the IFF_CANTCHANGE
2112 * check, so we don't need special handling here yet.
2114 new_flags = (ifr->ifr_flags & 0xffff) |
2115 (ifr->ifr_flagshigh << 16);
2116 if (ifp->if_flags & IFF_SMART) {
2117 /* Smart drivers twiddle their own routes */
2118 } else if (ifp->if_flags & IFF_UP &&
2119 (new_flags & IFF_UP) == 0) {
2123 } else if (new_flags & IFF_UP &&
2124 (ifp->if_flags & IFF_UP) == 0) {
2129 /* See if permanently promiscuous mode bit is about to flip */
2130 if ((ifp->if_flags ^ new_flags) & IFF_PPROMISC) {
2131 if (new_flags & IFF_PPROMISC)
2132 ifp->if_flags |= IFF_PROMISC;
2133 else if (ifp->if_pcount == 0)
2134 ifp->if_flags &= ~IFF_PROMISC;
2135 log(LOG_INFO, "%s: permanently promiscuous mode %s\n",
2137 (new_flags & IFF_PPROMISC) ? "enabled" : "disabled");
2139 ifp->if_flags = (ifp->if_flags & IFF_CANTCHANGE) |
2140 (new_flags &~ IFF_CANTCHANGE);
2141 if (ifp->if_ioctl) {
2142 (void) (*ifp->if_ioctl)(ifp, cmd, data);
2144 getmicrotime(&ifp->if_lastchange);
2148 error = priv_check(td, PRIV_NET_SETIFCAP);
2151 if (ifp->if_ioctl == NULL)
2152 return (EOPNOTSUPP);
2153 if (ifr->ifr_reqcap & ~ifp->if_capabilities)
2155 error = (*ifp->if_ioctl)(ifp, cmd, data);
2157 getmicrotime(&ifp->if_lastchange);
2162 error = mac_ifnet_ioctl_set(td->td_ucred, ifr, ifp);
2167 error = priv_check(td, PRIV_NET_SETIFNAME);
2170 error = copyinstr(ifr->ifr_data, new_name, IFNAMSIZ, NULL);
2173 if (new_name[0] == '\0')
2175 if (ifunit(new_name) != NULL)
2179 * XXX: Locking. Nothing else seems to lock if_flags,
2180 * and there are numerous other races with the
2181 * ifunit() checks not being atomic with namespace
2182 * changes (renames, vmoves, if_attach, etc).
2184 ifp->if_flags |= IFF_RENAMING;
2186 /* Announce the departure of the interface. */
2187 rt_ifannouncemsg(ifp, IFAN_DEPARTURE);
2188 EVENTHANDLER_INVOKE(ifnet_departure_event, ifp);
2190 log(LOG_INFO, "%s: changing name to '%s'\n",
2191 ifp->if_xname, new_name);
2193 strlcpy(ifp->if_xname, new_name, sizeof(ifp->if_xname));
2196 sdl = (struct sockaddr_dl *)ifa->ifa_addr;
2197 namelen = strlen(new_name);
2198 onamelen = sdl->sdl_nlen;
2200 * Move the address if needed. This is safe because we
2201 * allocate space for a name of length IFNAMSIZ when we
2202 * create this in if_attach().
2204 if (namelen != onamelen) {
2205 bcopy(sdl->sdl_data + onamelen,
2206 sdl->sdl_data + namelen, sdl->sdl_alen);
2208 bcopy(new_name, sdl->sdl_data, namelen);
2209 sdl->sdl_nlen = namelen;
2210 sdl = (struct sockaddr_dl *)ifa->ifa_netmask;
2211 bzero(sdl->sdl_data, onamelen);
2212 while (namelen != 0)
2213 sdl->sdl_data[--namelen] = 0xff;
2216 EVENTHANDLER_INVOKE(ifnet_arrival_event, ifp);
2217 /* Announce the return of the interface. */
2218 rt_ifannouncemsg(ifp, IFAN_ARRIVAL);
2220 ifp->if_flags &= ~IFF_RENAMING;
2225 error = priv_check(td, PRIV_NET_SETIFVNET);
2228 error = if_vmove_loan(td, ifp, ifr->ifr_name, ifr->ifr_jid);
2233 error = priv_check(td, PRIV_NET_SETIFMETRIC);
2236 ifp->if_metric = ifr->ifr_metric;
2237 getmicrotime(&ifp->if_lastchange);
2241 error = priv_check(td, PRIV_NET_SETIFPHYS);
2244 if (ifp->if_ioctl == NULL)
2245 return (EOPNOTSUPP);
2246 error = (*ifp->if_ioctl)(ifp, cmd, data);
2248 getmicrotime(&ifp->if_lastchange);
2253 u_long oldmtu = ifp->if_mtu;
2255 error = priv_check(td, PRIV_NET_SETIFMTU);
2258 if (ifr->ifr_mtu < IF_MINMTU || ifr->ifr_mtu > IF_MAXMTU)
2260 if (ifp->if_ioctl == NULL)
2261 return (EOPNOTSUPP);
2262 error = (*ifp->if_ioctl)(ifp, cmd, data);
2264 getmicrotime(&ifp->if_lastchange);
2268 * If the link MTU changed, do network layer specific procedure.
2270 if (ifp->if_mtu != oldmtu) {
2280 if (cmd == SIOCADDMULTI)
2281 error = priv_check(td, PRIV_NET_ADDMULTI);
2283 error = priv_check(td, PRIV_NET_DELMULTI);
2287 /* Don't allow group membership on non-multicast interfaces. */
2288 if ((ifp->if_flags & IFF_MULTICAST) == 0)
2289 return (EOPNOTSUPP);
2291 /* Don't let users screw up protocols' entries. */
2292 if (ifr->ifr_addr.sa_family != AF_LINK)
2295 if (cmd == SIOCADDMULTI) {
2296 struct ifmultiaddr *ifma;
2299 * Userland is only permitted to join groups once
2300 * via the if_addmulti() KPI, because it cannot hold
2301 * struct ifmultiaddr * between calls. It may also
2302 * lose a race while we check if the membership
2306 ifma = if_findmulti(ifp, &ifr->ifr_addr);
2307 IF_ADDR_UNLOCK(ifp);
2311 error = if_addmulti(ifp, &ifr->ifr_addr, &ifma);
2313 error = if_delmulti(ifp, &ifr->ifr_addr);
2316 getmicrotime(&ifp->if_lastchange);
2319 case SIOCSIFPHYADDR:
2320 case SIOCDIFPHYADDR:
2322 case SIOCSIFPHYADDR_IN6:
2324 case SIOCSLIFPHYADDR:
2326 case SIOCSIFGENERIC:
2327 error = priv_check(td, PRIV_NET_HWIOCTL);
2330 if (ifp->if_ioctl == NULL)
2331 return (EOPNOTSUPP);
2332 error = (*ifp->if_ioctl)(ifp, cmd, data);
2334 getmicrotime(&ifp->if_lastchange);
2338 ifs = (struct ifstat *)data;
2339 ifs->ascii[0] = '\0';
2341 case SIOCGIFPSRCADDR:
2342 case SIOCGIFPDSTADDR:
2343 case SIOCGLIFPHYADDR:
2345 case SIOCGIFGENERIC:
2346 if (ifp->if_ioctl == NULL)
2347 return (EOPNOTSUPP);
2348 error = (*ifp->if_ioctl)(ifp, cmd, data);
2352 error = priv_check(td, PRIV_NET_SETLLADDR);
2355 error = if_setlladdr(ifp,
2356 ifr->ifr_addr.sa_data, ifr->ifr_addr.sa_len);
2357 EVENTHANDLER_INVOKE(iflladdr_event, ifp);
2362 struct ifgroupreq *ifgr = (struct ifgroupreq *)ifr;
2364 error = priv_check(td, PRIV_NET_ADDIFGROUP);
2367 if ((error = if_addgroup(ifp, ifgr->ifgr_group)))
2373 if ((error = if_getgroup((struct ifgroupreq *)ifr, ifp)))
2379 struct ifgroupreq *ifgr = (struct ifgroupreq *)ifr;
2381 error = priv_check(td, PRIV_NET_DELIFGROUP);
2384 if ((error = if_delgroup(ifp, ifgr->ifgr_group)))
2400 ifioctl(struct socket *so, u_long cmd, caddr_t data, struct thread *td)
2413 return (ifconf(cmd, data));
2415 ifr = (struct ifreq *)data;
2420 error = priv_check(td, PRIV_NET_SETIFVNET);
2423 return (if_vmove_reclaim(td, ifr->ifr_name, ifr->ifr_jid));
2427 error = priv_check(td, PRIV_NET_IFCREATE);
2430 return (if_clone_create(ifr->ifr_name, sizeof(ifr->ifr_name),
2431 cmd == SIOCIFCREATE2 ? ifr->ifr_data : NULL));
2433 error = priv_check(td, PRIV_NET_IFDESTROY);
2436 return if_clone_destroy(ifr->ifr_name);
2438 case SIOCIFGCLONERS:
2439 return (if_clone_list((struct if_clonereq *)data));
2441 return (if_getgroupmembers((struct ifgroupreq *)data));
2444 ifp = ifunit_ref(ifr->ifr_name);
2448 error = ifhwioctl(cmd, ifp, data, td);
2449 if (error != ENOIOCTL) {
2454 oif_flags = ifp->if_flags;
2455 if (so->so_proto == NULL) {
2457 return (EOPNOTSUPP);
2460 error = ((*so->so_proto->pr_usrreqs->pru_control)(so, cmd,
2463 if (error == EOPNOTSUPP && ifp != NULL && ifp->if_ioctl != NULL)
2464 error = (*ifp->if_ioctl)(ifp, cmd, data);
2471 case SIOCSIFDSTADDR:
2473 case SIOCSIFBRDADDR:
2474 case SIOCSIFNETMASK:
2475 #if BYTE_ORDER != BIG_ENDIAN
2476 if (ifr->ifr_addr.sa_family == 0 &&
2477 ifr->ifr_addr.sa_len < 16) {
2478 ifr->ifr_addr.sa_family = ifr->ifr_addr.sa_len;
2479 ifr->ifr_addr.sa_len = 16;
2482 if (ifr->ifr_addr.sa_len == 0)
2483 ifr->ifr_addr.sa_len = 16;
2491 case OSIOCGIFDSTADDR:
2492 cmd = SIOCGIFDSTADDR;
2495 case OSIOCGIFBRDADDR:
2496 cmd = SIOCGIFBRDADDR;
2499 case OSIOCGIFNETMASK:
2500 cmd = SIOCGIFNETMASK;
2502 error = ((*so->so_proto->pr_usrreqs->pru_control)(so,
2506 if (error == EOPNOTSUPP && ifp != NULL &&
2507 ifp->if_ioctl != NULL)
2508 error = (*ifp->if_ioctl)(ifp, cmd, data);
2512 case OSIOCGIFDSTADDR:
2513 case OSIOCGIFBRDADDR:
2514 case OSIOCGIFNETMASK:
2515 *(u_short *)&ifr->ifr_addr = ifr->ifr_addr.sa_family;
2519 #endif /* COMPAT_43 */
2521 if ((oif_flags ^ ifp->if_flags) & IFF_UP) {
2523 if (ifp->if_flags & IFF_UP) {
2535 * The code common to handling reference counted flags,
2536 * e.g., in ifpromisc() and if_allmulti().
2537 * The "pflag" argument can specify a permanent mode flag to check,
2538 * such as IFF_PPROMISC for promiscuous mode; should be 0 if none.
2540 * Only to be used on stack-owned flags, not driver-owned flags.
2543 if_setflag(struct ifnet *ifp, int flag, int pflag, int *refcount, int onswitch)
2547 int oldflags, oldcount;
2549 /* Sanity checks to catch programming errors */
2550 KASSERT((flag & (IFF_DRV_OACTIVE|IFF_DRV_RUNNING)) == 0,
2551 ("%s: setting driver-owned flag %d", __func__, flag));
2554 KASSERT(*refcount >= 0,
2555 ("%s: increment negative refcount %d for flag %d",
2556 __func__, *refcount, flag));
2558 KASSERT(*refcount > 0,
2559 ("%s: decrement non-positive refcount %d for flag %d",
2560 __func__, *refcount, flag));
2562 /* In case this mode is permanent, just touch refcount */
2563 if (ifp->if_flags & pflag) {
2564 *refcount += onswitch ? 1 : -1;
2568 /* Save ifnet parameters for if_ioctl() may fail */
2569 oldcount = *refcount;
2570 oldflags = ifp->if_flags;
2573 * See if we aren't the only and touching refcount is enough.
2574 * Actually toggle interface flag if we are the first or last.
2579 ifp->if_flags |= flag;
2583 ifp->if_flags &= ~flag;
2586 /* Call down the driver since we've changed interface flags */
2587 if (ifp->if_ioctl == NULL) {
2591 ifr.ifr_flags = ifp->if_flags & 0xffff;
2592 ifr.ifr_flagshigh = ifp->if_flags >> 16;
2593 error = (*ifp->if_ioctl)(ifp, SIOCSIFFLAGS, (caddr_t)&ifr);
2596 /* Notify userland that interface flags have changed */
2601 /* Recover after driver error */
2602 *refcount = oldcount;
2603 ifp->if_flags = oldflags;
2608 * Set/clear promiscuous mode on interface ifp based on the truth value
2609 * of pswitch. The calls are reference counted so that only the first
2610 * "on" request actually has an effect, as does the final "off" request.
2611 * Results are undefined if the "off" and "on" requests are not matched.
2614 ifpromisc(struct ifnet *ifp, int pswitch)
2617 int oldflags = ifp->if_flags;
2619 error = if_setflag(ifp, IFF_PROMISC, IFF_PPROMISC,
2620 &ifp->if_pcount, pswitch);
2621 /* If promiscuous mode status has changed, log a message */
2622 if (error == 0 && ((ifp->if_flags ^ oldflags) & IFF_PROMISC))
2623 log(LOG_INFO, "%s: promiscuous mode %s\n",
2625 (ifp->if_flags & IFF_PROMISC) ? "enabled" : "disabled");
2630 * Return interface configuration
2631 * of system. List may be used
2632 * in later ioctl's (above) to get
2633 * other information.
2637 ifconf(u_long cmd, caddr_t data)
2639 struct ifconf *ifc = (struct ifconf *)data;
2641 struct ifconf32 *ifc32 = (struct ifconf32 *)data;
2642 struct ifconf ifc_swab;
2648 int error, full = 0, valid_len, max_len;
2651 if (cmd == SIOCGIFCONF32) {
2652 ifc_swab.ifc_len = ifc32->ifc_len;
2653 ifc_swab.ifc_buf = (caddr_t)(uintptr_t)ifc32->ifc_buf;
2657 /* Limit initial buffer size to MAXPHYS to avoid DoS from userspace. */
2658 max_len = MAXPHYS - 1;
2660 /* Prevent hostile input from being able to crash the system */
2661 if (ifc->ifc_len <= 0)
2665 if (ifc->ifc_len <= max_len) {
2666 max_len = ifc->ifc_len;
2669 sb = sbuf_new(NULL, NULL, max_len + 1, SBUF_FIXEDLEN);
2674 TAILQ_FOREACH(ifp, &V_ifnet, if_link) {
2678 * Zero the ifr_name buffer to make sure we don't
2679 * disclose the contents of the stack.
2681 memset(ifr.ifr_name, 0, sizeof(ifr.ifr_name));
2683 if (strlcpy(ifr.ifr_name, ifp->if_xname, sizeof(ifr.ifr_name))
2684 >= sizeof(ifr.ifr_name)) {
2687 return (ENAMETOOLONG);
2692 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
2693 struct sockaddr *sa = ifa->ifa_addr;
2695 if (prison_if(curthread->td_ucred, sa) != 0)
2699 if (cmd == OSIOCGIFCONF) {
2700 struct osockaddr *osa =
2701 (struct osockaddr *)&ifr.ifr_addr;
2703 osa->sa_family = sa->sa_family;
2704 sbuf_bcat(sb, &ifr, sizeof(ifr));
2705 max_len += sizeof(ifr);
2708 if (sa->sa_len <= sizeof(*sa)) {
2710 sbuf_bcat(sb, &ifr, sizeof(ifr));
2711 max_len += sizeof(ifr);
2714 offsetof(struct ifreq, ifr_addr));
2715 max_len += offsetof(struct ifreq, ifr_addr);
2716 sbuf_bcat(sb, sa, sa->sa_len);
2717 max_len += sa->sa_len;
2720 if (!sbuf_overflowed(sb))
2721 valid_len = sbuf_len(sb);
2723 IF_ADDR_UNLOCK(ifp);
2725 bzero((caddr_t)&ifr.ifr_addr, sizeof(ifr.ifr_addr));
2726 sbuf_bcat(sb, &ifr, sizeof(ifr));
2727 max_len += sizeof(ifr);
2729 if (!sbuf_overflowed(sb))
2730 valid_len = sbuf_len(sb);
2736 * If we didn't allocate enough space (uncommon), try again. If
2737 * we have already allocated as much space as we are allowed,
2738 * return what we've got.
2740 if (valid_len != max_len && !full) {
2745 ifc->ifc_len = valid_len;
2747 if (cmd == SIOCGIFCONF32)
2748 ifc32->ifc_len = valid_len;
2751 error = copyout(sbuf_data(sb), ifc->ifc_req, ifc->ifc_len);
2757 * Just like ifpromisc(), but for all-multicast-reception mode.
2760 if_allmulti(struct ifnet *ifp, int onswitch)
2763 return (if_setflag(ifp, IFF_ALLMULTI, 0, &ifp->if_amcount, onswitch));
2766 struct ifmultiaddr *
2767 if_findmulti(struct ifnet *ifp, struct sockaddr *sa)
2769 struct ifmultiaddr *ifma;
2771 IF_ADDR_LOCK_ASSERT(ifp);
2773 TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
2774 if (sa->sa_family == AF_LINK) {
2775 if (sa_dl_equal(ifma->ifma_addr, sa))
2778 if (sa_equal(ifma->ifma_addr, sa))
2787 * Allocate a new ifmultiaddr and initialize based on passed arguments. We
2788 * make copies of passed sockaddrs. The ifmultiaddr will not be added to
2789 * the ifnet multicast address list here, so the caller must do that and
2790 * other setup work (such as notifying the device driver). The reference
2791 * count is initialized to 1.
2793 static struct ifmultiaddr *
2794 if_allocmulti(struct ifnet *ifp, struct sockaddr *sa, struct sockaddr *llsa,
2797 struct ifmultiaddr *ifma;
2798 struct sockaddr *dupsa;
2800 ifma = malloc(sizeof *ifma, M_IFMADDR, mflags |
2805 dupsa = malloc(sa->sa_len, M_IFMADDR, mflags);
2806 if (dupsa == NULL) {
2807 free(ifma, M_IFMADDR);
2810 bcopy(sa, dupsa, sa->sa_len);
2811 ifma->ifma_addr = dupsa;
2813 ifma->ifma_ifp = ifp;
2814 ifma->ifma_refcount = 1;
2815 ifma->ifma_protospec = NULL;
2818 ifma->ifma_lladdr = NULL;
2822 dupsa = malloc(llsa->sa_len, M_IFMADDR, mflags);
2823 if (dupsa == NULL) {
2824 free(ifma->ifma_addr, M_IFMADDR);
2825 free(ifma, M_IFMADDR);
2828 bcopy(llsa, dupsa, llsa->sa_len);
2829 ifma->ifma_lladdr = dupsa;
2835 * if_freemulti: free ifmultiaddr structure and possibly attached related
2836 * addresses. The caller is responsible for implementing reference
2837 * counting, notifying the driver, handling routing messages, and releasing
2838 * any dependent link layer state.
2841 if_freemulti(struct ifmultiaddr *ifma)
2844 KASSERT(ifma->ifma_refcount == 0, ("if_freemulti: refcount %d",
2845 ifma->ifma_refcount));
2846 KASSERT(ifma->ifma_protospec == NULL,
2847 ("if_freemulti: protospec not NULL"));
2849 if (ifma->ifma_lladdr != NULL)
2850 free(ifma->ifma_lladdr, M_IFMADDR);
2851 free(ifma->ifma_addr, M_IFMADDR);
2852 free(ifma, M_IFMADDR);
2856 * Register an additional multicast address with a network interface.
2858 * - If the address is already present, bump the reference count on the
2859 * address and return.
2860 * - If the address is not link-layer, look up a link layer address.
2861 * - Allocate address structures for one or both addresses, and attach to the
2862 * multicast address list on the interface. If automatically adding a link
2863 * layer address, the protocol address will own a reference to the link
2864 * layer address, to be freed when it is freed.
2865 * - Notify the network device driver of an addition to the multicast address
2868 * 'sa' points to caller-owned memory with the desired multicast address.
2870 * 'retifma' will be used to return a pointer to the resulting multicast
2871 * address reference, if desired.
2874 if_addmulti(struct ifnet *ifp, struct sockaddr *sa,
2875 struct ifmultiaddr **retifma)
2877 struct ifmultiaddr *ifma, *ll_ifma;
2878 struct sockaddr *llsa;
2882 * If the address is already present, return a new reference to it;
2883 * otherwise, allocate storage and set up a new address.
2886 ifma = if_findmulti(ifp, sa);
2888 ifma->ifma_refcount++;
2889 if (retifma != NULL)
2891 IF_ADDR_UNLOCK(ifp);
2896 * The address isn't already present; resolve the protocol address
2897 * into a link layer address, and then look that up, bump its
2898 * refcount or allocate an ifma for that also. If 'llsa' was
2899 * returned, we will need to free it later.
2903 if (ifp->if_resolvemulti != NULL) {
2904 error = ifp->if_resolvemulti(ifp, &llsa, sa);
2910 * Allocate the new address. Don't hook it up yet, as we may also
2911 * need to allocate a link layer multicast address.
2913 ifma = if_allocmulti(ifp, sa, llsa, M_NOWAIT);
2920 * If a link layer address is found, we'll need to see if it's
2921 * already present in the address list, or allocate is as well.
2922 * When this block finishes, the link layer address will be on the
2926 ll_ifma = if_findmulti(ifp, llsa);
2927 if (ll_ifma == NULL) {
2928 ll_ifma = if_allocmulti(ifp, llsa, NULL, M_NOWAIT);
2929 if (ll_ifma == NULL) {
2930 --ifma->ifma_refcount;
2935 TAILQ_INSERT_HEAD(&ifp->if_multiaddrs, ll_ifma,
2938 ll_ifma->ifma_refcount++;
2939 ifma->ifma_llifma = ll_ifma;
2943 * We now have a new multicast address, ifma, and possibly a new or
2944 * referenced link layer address. Add the primary address to the
2945 * ifnet address list.
2947 TAILQ_INSERT_HEAD(&ifp->if_multiaddrs, ifma, ifma_link);
2949 if (retifma != NULL)
2953 * Must generate the message while holding the lock so that 'ifma'
2954 * pointer is still valid.
2956 rt_newmaddrmsg(RTM_NEWMADDR, ifma);
2957 IF_ADDR_UNLOCK(ifp);
2960 * We are certain we have added something, so call down to the
2961 * interface to let them know about it.
2963 if (ifp->if_ioctl != NULL) {
2964 (void) (*ifp->if_ioctl)(ifp, SIOCADDMULTI, 0);
2968 free(llsa, M_IFMADDR);
2974 free(llsa, M_IFMADDR);
2977 IF_ADDR_UNLOCK(ifp);
2982 * Delete a multicast group membership by network-layer group address.
2984 * Returns ENOENT if the entry could not be found. If ifp no longer
2985 * exists, results are undefined. This entry point should only be used
2986 * from subsystems which do appropriate locking to hold ifp for the
2987 * duration of the call.
2988 * Network-layer protocol domains must use if_delmulti_ifma().
2991 if_delmulti(struct ifnet *ifp, struct sockaddr *sa)
2993 struct ifmultiaddr *ifma;
2998 IFNET_RLOCK_NOSLEEP();
2999 TAILQ_FOREACH(oifp, &V_ifnet, if_link)
3004 IFNET_RUNLOCK_NOSLEEP();
3006 KASSERT(ifp != NULL, ("%s: ifnet went away", __func__));
3013 ifma = if_findmulti(ifp, sa);
3015 lastref = if_delmulti_locked(ifp, ifma, 0);
3016 IF_ADDR_UNLOCK(ifp);
3021 if (lastref && ifp->if_ioctl != NULL) {
3022 (void)(*ifp->if_ioctl)(ifp, SIOCDELMULTI, 0);
3029 * Delete all multicast group membership for an interface.
3030 * Should be used to quickly flush all multicast filters.
3033 if_delallmulti(struct ifnet *ifp)
3035 struct ifmultiaddr *ifma;
3036 struct ifmultiaddr *next;
3039 TAILQ_FOREACH_SAFE(ifma, &ifp->if_multiaddrs, ifma_link, next)
3040 if_delmulti_locked(ifp, ifma, 0);
3041 IF_ADDR_UNLOCK(ifp);
3045 * Delete a multicast group membership by group membership pointer.
3046 * Network-layer protocol domains must use this routine.
3048 * It is safe to call this routine if the ifp disappeared.
3051 if_delmulti_ifma(struct ifmultiaddr *ifma)
3056 ifp = ifma->ifma_ifp;
3059 printf("%s: ifma_ifp seems to be detached\n", __func__);
3063 IFNET_RLOCK_NOSLEEP();
3064 TAILQ_FOREACH(oifp, &V_ifnet, if_link)
3068 printf("%s: ifnet %p disappeared\n", __func__, ifp);
3071 IFNET_RUNLOCK_NOSLEEP();
3075 * If and only if the ifnet instance exists: Acquire the address lock.
3080 lastref = if_delmulti_locked(ifp, ifma, 0);
3084 * If and only if the ifnet instance exists:
3085 * Release the address lock.
3086 * If the group was left: update the hardware hash filter.
3088 IF_ADDR_UNLOCK(ifp);
3089 if (lastref && ifp->if_ioctl != NULL) {
3090 (void)(*ifp->if_ioctl)(ifp, SIOCDELMULTI, 0);
3096 * Perform deletion of network-layer and/or link-layer multicast address.
3098 * Return 0 if the reference count was decremented.
3099 * Return 1 if the final reference was released, indicating that the
3100 * hardware hash filter should be reprogrammed.
3103 if_delmulti_locked(struct ifnet *ifp, struct ifmultiaddr *ifma, int detaching)
3105 struct ifmultiaddr *ll_ifma;
3107 if (ifp != NULL && ifma->ifma_ifp != NULL) {
3108 KASSERT(ifma->ifma_ifp == ifp,
3109 ("%s: inconsistent ifp %p", __func__, ifp));
3110 IF_ADDR_LOCK_ASSERT(ifp);
3113 ifp = ifma->ifma_ifp;
3116 * If the ifnet is detaching, null out references to ifnet,
3117 * so that upper protocol layers will notice, and not attempt
3118 * to obtain locks for an ifnet which no longer exists. The
3119 * routing socket announcement must happen before the ifnet
3120 * instance is detached from the system.
3124 printf("%s: detaching ifnet instance %p\n", __func__, ifp);
3127 * ifp may already be nulled out if we are being reentered
3128 * to delete the ll_ifma.
3131 rt_newmaddrmsg(RTM_DELMADDR, ifma);
3132 ifma->ifma_ifp = NULL;
3136 if (--ifma->ifma_refcount > 0)
3140 * If this ifma is a network-layer ifma, a link-layer ifma may
3141 * have been associated with it. Release it first if so.
3143 ll_ifma = ifma->ifma_llifma;
3144 if (ll_ifma != NULL) {
3145 KASSERT(ifma->ifma_lladdr != NULL,
3146 ("%s: llifma w/o lladdr", __func__));
3148 ll_ifma->ifma_ifp = NULL; /* XXX */
3149 if (--ll_ifma->ifma_refcount == 0) {
3151 TAILQ_REMOVE(&ifp->if_multiaddrs, ll_ifma,
3154 if_freemulti(ll_ifma);
3159 TAILQ_REMOVE(&ifp->if_multiaddrs, ifma, ifma_link);
3164 * The last reference to this instance of struct ifmultiaddr
3165 * was released; the hardware should be notified of this change.
3171 * Set the link layer address on an interface.
3173 * At this time we only support certain types of interfaces,
3174 * and we don't allow the length of the address to change.
3177 if_setlladdr(struct ifnet *ifp, const u_char *lladdr, int len)
3179 struct sockaddr_dl *sdl;
3186 IF_ADDR_UNLOCK(ifp);
3190 IF_ADDR_UNLOCK(ifp);
3191 sdl = (struct sockaddr_dl *)ifa->ifa_addr;
3196 if (len != sdl->sdl_alen) { /* don't allow length to change */
3200 switch (ifp->if_type) {
3208 case IFT_IEEE8023ADLAG:
3210 bcopy(lladdr, LLADDR(sdl), len);
3219 * If the interface is already up, we need
3220 * to re-init it in order to reprogram its
3223 if ((ifp->if_flags & IFF_UP) != 0) {
3224 if (ifp->if_ioctl) {
3225 ifp->if_flags &= ~IFF_UP;
3226 ifr.ifr_flags = ifp->if_flags & 0xffff;
3227 ifr.ifr_flagshigh = ifp->if_flags >> 16;
3228 (*ifp->if_ioctl)(ifp, SIOCSIFFLAGS, (caddr_t)&ifr);
3229 ifp->if_flags |= IFF_UP;
3230 ifr.ifr_flags = ifp->if_flags & 0xffff;
3231 ifr.ifr_flagshigh = ifp->if_flags >> 16;
3232 (*ifp->if_ioctl)(ifp, SIOCSIFFLAGS, (caddr_t)&ifr);
3236 * Also send gratuitous ARPs to notify other nodes about
3237 * the address change.
3239 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
3240 if (ifa->ifa_addr->sa_family == AF_INET)
3241 arp_ifinit(ifp, ifa);
3249 * The name argument must be a pointer to storage which will last as
3250 * long as the interface does. For physical devices, the result of
3251 * device_get_name(dev) is a good choice and for pseudo-devices a
3252 * static string works well.
3255 if_initname(struct ifnet *ifp, const char *name, int unit)
3257 ifp->if_dname = name;
3258 ifp->if_dunit = unit;
3259 if (unit != IF_DUNIT_NONE)
3260 snprintf(ifp->if_xname, IFNAMSIZ, "%s%d", name, unit);
3262 strlcpy(ifp->if_xname, name, IFNAMSIZ);
3266 if_printf(struct ifnet *ifp, const char * fmt, ...)
3271 retval = printf("%s: ", ifp->if_xname);
3273 retval += vprintf(fmt, ap);
3279 if_start(struct ifnet *ifp)
3282 (*(ifp)->if_start)(ifp);
3286 * Backwards compatibility interface for drivers
3287 * that have not implemented it
3290 if_transmit(struct ifnet *ifp, struct mbuf *m)
3294 IFQ_HANDOFF(ifp, m, error);
3299 if_handoff(struct ifqueue *ifq, struct mbuf *m, struct ifnet *ifp, int adjust)
3304 if (_IF_QFULL(ifq)) {
3311 ifp->if_obytes += m->m_pkthdr.len + adjust;
3312 if (m->m_flags & (M_BCAST|M_MCAST))
3314 active = ifp->if_drv_flags & IFF_DRV_OACTIVE;
3316 _IF_ENQUEUE(ifq, m);
3318 if (ifp != NULL && !active)
3319 (*(ifp)->if_start)(ifp);
3324 if_register_com_alloc(u_char type,
3325 if_com_alloc_t *a, if_com_free_t *f)
3328 KASSERT(if_com_alloc[type] == NULL,
3329 ("if_register_com_alloc: %d already registered", type));
3330 KASSERT(if_com_free[type] == NULL,
3331 ("if_register_com_alloc: %d free already registered", type));
3333 if_com_alloc[type] = a;
3334 if_com_free[type] = f;
3338 if_deregister_com_alloc(u_char type)
3341 KASSERT(if_com_alloc[type] != NULL,
3342 ("if_deregister_com_alloc: %d not registered", type));
3343 KASSERT(if_com_free[type] != NULL,
3344 ("if_deregister_com_alloc: %d free not registered", type));
3345 if_com_alloc[type] = NULL;
3346 if_com_free[type] = NULL;
3351 if_show_ifnet(struct ifnet *ifp)
3356 db_printf("%s:\n", ifp->if_xname);
3357 #define IF_DB_PRINTF(f, e) db_printf(" %s = " f "\n", #e, ifp->e);
3358 IF_DB_PRINTF("%s", if_dname);
3359 IF_DB_PRINTF("%d", if_dunit);
3360 IF_DB_PRINTF("%s", if_description);
3361 IF_DB_PRINTF("%u", if_index);
3362 IF_DB_PRINTF("%u", if_refcount);
3363 IF_DB_PRINTF("%d", if_index_reserved);
3364 IF_DB_PRINTF("%p", if_softc);
3365 IF_DB_PRINTF("%p", if_l2com);
3366 IF_DB_PRINTF("%p", if_vnet);
3367 IF_DB_PRINTF("%p", if_home_vnet);
3368 IF_DB_PRINTF("%p", if_addr);
3369 IF_DB_PRINTF("%p", if_llsoftc);
3370 IF_DB_PRINTF("%p", if_label);
3371 IF_DB_PRINTF("%u", if_pcount);
3372 IF_DB_PRINTF("0x%08x", if_flags);
3373 IF_DB_PRINTF("0x%08x", if_drv_flags);
3374 IF_DB_PRINTF("0x%08x", if_capabilities);
3375 IF_DB_PRINTF("0x%08x", if_capenable);
3376 IF_DB_PRINTF("%p", if_snd.ifq_head);
3377 IF_DB_PRINTF("%p", if_snd.ifq_tail);
3378 IF_DB_PRINTF("%d", if_snd.ifq_len);
3379 IF_DB_PRINTF("%d", if_snd.ifq_maxlen);
3380 IF_DB_PRINTF("%d", if_snd.ifq_drops);
3381 IF_DB_PRINTF("%p", if_snd.ifq_drv_head);
3382 IF_DB_PRINTF("%p", if_snd.ifq_drv_tail);
3383 IF_DB_PRINTF("%d", if_snd.ifq_drv_len);
3384 IF_DB_PRINTF("%d", if_snd.ifq_drv_maxlen);
3385 IF_DB_PRINTF("%d", if_snd.altq_type);
3386 IF_DB_PRINTF("%x", if_snd.altq_flags);
3390 DB_SHOW_COMMAND(ifnet, db_show_ifnet)
3394 db_printf("usage: show ifnet <struct ifnet *>\n");
3398 if_show_ifnet((struct ifnet *)addr);
3401 DB_SHOW_ALL_COMMAND(ifnets, db_show_all_ifnets)
3403 VNET_ITERATOR_DECL(vnet_iter);
3407 VNET_FOREACH(vnet_iter) {
3408 CURVNET_SET_QUIET(vnet_iter);
3410 db_printf("vnet=%p\n", curvnet);
3412 for (idx = 1; idx <= V_if_index; idx++) {
3413 ifp = V_ifindex_table[idx].ife_ifnet;
3416 db_printf( "%20s ifp=%p\n", ifp->if_xname, ifp);