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"
37 #include <sys/param.h>
38 #include <sys/types.h>
40 #include <sys/malloc.h>
44 #include <sys/systm.h>
47 #include <sys/socket.h>
48 #include <sys/socketvar.h>
49 #include <sys/protosw.h>
50 #include <sys/kernel.h>
52 #include <sys/refcount.h>
53 #include <sys/module.h>
54 #include <sys/rwlock.h>
55 #include <sys/sockio.h>
56 #include <sys/syslog.h>
57 #include <sys/sysctl.h>
58 #include <sys/taskqueue.h>
59 #include <sys/domain.h>
63 #include <machine/stdarg.h>
67 #include <net/if_arp.h>
68 #include <net/if_clone.h>
69 #include <net/if_dl.h>
70 #include <net/if_types.h>
71 #include <net/if_var.h>
72 #include <net/radix.h>
73 #include <net/route.h>
76 #if defined(INET) || defined(INET6)
77 #include <netinet/in.h>
78 #include <netinet/in_var.h>
79 #include <netinet/ip.h>
80 #include <netinet/ip_carp.h>
82 #include <netinet/if_ether.h>
85 #include <netinet6/in6_var.h>
86 #include <netinet6/in6_ifattach.h>
88 #endif /* INET || INET6 */
90 #include <security/mac/mac_framework.h>
92 #ifdef COMPAT_FREEBSD32
93 #include <sys/mount.h>
94 #include <compat/freebsd32/freebsd32.h>
97 struct ifindex_entry {
98 struct ifnet *ife_ifnet;
101 SYSCTL_NODE(_net, PF_LINK, link, CTLFLAG_RW, 0, "Link layers");
102 SYSCTL_NODE(_net_link, 0, generic, CTLFLAG_RW, 0, "Generic link-management");
104 TUNABLE_INT("net.link.ifqmaxlen", &ifqmaxlen);
105 SYSCTL_INT(_net_link, OID_AUTO, ifqmaxlen, CTLFLAG_RDTUN,
106 &ifqmaxlen, 0, "max send queue size");
108 /* Log link state change events */
109 static int log_link_state_change = 1;
111 SYSCTL_INT(_net_link, OID_AUTO, log_link_state_change, CTLFLAG_RW,
112 &log_link_state_change, 0,
113 "log interface link state change events");
115 /* Interface description */
116 static unsigned int ifdescr_maxlen = 1024;
117 SYSCTL_UINT(_net, OID_AUTO, ifdescr_maxlen, CTLFLAG_RW,
119 "administrative maximum length for interface description");
121 static MALLOC_DEFINE(M_IFDESCR, "ifdescr", "ifnet descriptions");
123 /* global sx for non-critical path ifdescr */
124 static struct sx ifdescr_sx;
125 SX_SYSINIT(ifdescr_sx, &ifdescr_sx, "ifnet descr");
127 void (*bridge_linkstate_p)(struct ifnet *ifp);
128 void (*ng_ether_link_state_p)(struct ifnet *ifp, int state);
129 void (*lagg_linkstate_p)(struct ifnet *ifp, int state);
130 /* These are external hooks for CARP. */
131 void (*carp_linkstate_p)(struct ifnet *ifp);
132 #if defined(INET) || defined(INET6)
133 struct ifnet *(*carp_forus_p)(struct ifnet *ifp, u_char *dhost);
134 int (*carp_output_p)(struct ifnet *ifp, struct mbuf *m,
135 struct sockaddr *sa, struct rtentry *rt);
138 int (*carp_iamatch_p)(struct ifnet *, struct in_ifaddr *, struct in_addr *,
142 struct ifaddr *(*carp_iamatch6_p)(struct ifnet *ifp, struct in6_addr *taddr6);
143 caddr_t (*carp_macmatch6_p)(struct ifnet *ifp, struct mbuf *m,
144 const struct in6_addr *taddr);
147 struct mbuf *(*tbr_dequeue_ptr)(struct ifaltq *, int) = NULL;
150 * XXX: Style; these should be sorted alphabetically, and unprototyped
151 * static functions should be prototyped. Currently they are sorted by
154 static void if_attachdomain(void *);
155 static void if_attachdomain1(struct ifnet *);
156 static int ifconf(u_long, caddr_t);
157 static void if_freemulti(struct ifmultiaddr *);
158 static void if_init(void *);
159 static void if_grow(void);
160 static void if_route(struct ifnet *, int flag, int fam);
161 static int if_setflag(struct ifnet *, int, int, int *, int);
162 static int if_transmit(struct ifnet *ifp, struct mbuf *m);
163 static void if_unroute(struct ifnet *, int flag, int fam);
164 static void link_rtrequest(int, struct rtentry *, struct rt_addrinfo *);
165 static int if_rtdel(struct radix_node *, void *);
166 static int ifhwioctl(u_long, struct ifnet *, caddr_t, struct thread *);
167 static int if_delmulti_locked(struct ifnet *, struct ifmultiaddr *, int);
168 static void do_link_state_change(void *, int);
169 static int if_getgroup(struct ifgroupreq *, struct ifnet *);
170 static int if_getgroupmembers(struct ifgroupreq *);
171 static void if_delgroups(struct ifnet *);
172 static void if_attach_internal(struct ifnet *, int);
173 static void if_detach_internal(struct ifnet *, int);
177 * XXX: declare here to avoid to include many inet6 related files..
178 * should be more generalized?
180 extern void nd6_setmtu(struct ifnet *);
183 VNET_DEFINE(int, if_index);
184 int ifqmaxlen = IFQ_MAXLEN;
185 VNET_DEFINE(struct ifnethead, ifnet); /* depend on static init XXX */
186 VNET_DEFINE(struct ifgrouphead, ifg_head);
188 static VNET_DEFINE(int, if_indexlim) = 8;
190 /* Table of ifnet by index. */
191 VNET_DEFINE(struct ifindex_entry *, ifindex_table);
193 #define V_if_indexlim VNET(if_indexlim)
194 #define V_ifindex_table VNET(ifindex_table)
197 * The global network interface list (V_ifnet) and related state (such as
198 * if_index, if_indexlim, and ifindex_table) are protected by an sxlock and
199 * an rwlock. Either may be acquired shared to stablize the list, but both
200 * must be acquired writable to modify the list. This model allows us to
201 * both stablize the interface list during interrupt thread processing, but
202 * also to stablize it over long-running ioctls, without introducing priority
203 * inversions and deadlocks.
205 struct rwlock ifnet_rwlock;
206 struct sx ifnet_sxlock;
209 * The allocation of network interfaces is a rather non-atomic affair; we
210 * need to select an index before we are ready to expose the interface for
211 * use, so will use this pointer value to indicate reservation.
213 #define IFNET_HOLD (void *)(uintptr_t)(-1)
215 static if_com_alloc_t *if_com_alloc[256];
216 static if_com_free_t *if_com_free[256];
218 static MALLOC_DEFINE(M_IFNET, "ifnet", "interface internals");
219 MALLOC_DEFINE(M_IFADDR, "ifaddr", "interface address");
220 MALLOC_DEFINE(M_IFMADDR, "ether_multi", "link-level multicast address");
223 ifnet_byindex_locked(u_short idx)
226 if (idx > V_if_index)
228 if (V_ifindex_table[idx].ife_ifnet == IFNET_HOLD)
230 return (V_ifindex_table[idx].ife_ifnet);
234 ifnet_byindex(u_short idx)
238 IFNET_RLOCK_NOSLEEP();
239 ifp = ifnet_byindex_locked(idx);
240 IFNET_RUNLOCK_NOSLEEP();
245 ifnet_byindex_ref(u_short idx)
249 IFNET_RLOCK_NOSLEEP();
250 ifp = ifnet_byindex_locked(idx);
251 if (ifp == NULL || (ifp->if_flags & IFF_DYING)) {
252 IFNET_RUNLOCK_NOSLEEP();
256 IFNET_RUNLOCK_NOSLEEP();
261 * Allocate an ifindex array entry; return 0 on success or an error on
265 ifindex_alloc_locked(u_short *idxp)
269 IFNET_WLOCK_ASSERT();
273 * Try to find an empty slot below V_if_index. If we fail, take the
276 for (idx = 1; idx <= V_if_index; idx++) {
277 if (V_ifindex_table[idx].ife_ifnet == NULL)
281 /* Catch if_index overflow. */
284 if (idx >= V_if_indexlim) {
288 if (idx > V_if_index)
295 ifindex_free_locked(u_short idx)
298 IFNET_WLOCK_ASSERT();
300 V_ifindex_table[idx].ife_ifnet = NULL;
301 while (V_if_index > 0 &&
302 V_ifindex_table[V_if_index].ife_ifnet == NULL)
307 ifindex_free(u_short idx)
311 ifindex_free_locked(idx);
316 ifnet_setbyindex_locked(u_short idx, struct ifnet *ifp)
319 IFNET_WLOCK_ASSERT();
321 V_ifindex_table[idx].ife_ifnet = ifp;
325 ifnet_setbyindex(u_short idx, struct ifnet *ifp)
329 ifnet_setbyindex_locked(idx, ifp);
334 ifaddr_byindex(u_short idx)
338 IFNET_RLOCK_NOSLEEP();
339 ifa = ifnet_byindex_locked(idx)->if_addr;
342 IFNET_RUNLOCK_NOSLEEP();
347 * Network interface utility routines.
349 * Routines with ifa_ifwith* names take sockaddr *'s as
354 vnet_if_init(const void *unused __unused)
357 TAILQ_INIT(&V_ifnet);
358 TAILQ_INIT(&V_ifg_head);
360 if_grow(); /* create initial table */
362 vnet_if_clone_init();
364 VNET_SYSINIT(vnet_if_init, SI_SUB_INIT_IF, SI_ORDER_SECOND, vnet_if_init,
369 if_init(void *dummy __unused)
375 SYSINIT(interfaces, SI_SUB_INIT_IF, SI_ORDER_FIRST, if_init, NULL);
380 vnet_if_uninit(const void *unused __unused)
383 VNET_ASSERT(TAILQ_EMPTY(&V_ifnet), ("%s:%d tailq &V_ifnet=%p "
384 "not empty", __func__, __LINE__, &V_ifnet));
385 VNET_ASSERT(TAILQ_EMPTY(&V_ifg_head), ("%s:%d tailq &V_ifg_head=%p "
386 "not empty", __func__, __LINE__, &V_ifg_head));
388 free((caddr_t)V_ifindex_table, M_IFNET);
390 VNET_SYSUNINIT(vnet_if_uninit, SI_SUB_INIT_IF, SI_ORDER_FIRST,
391 vnet_if_uninit, NULL);
399 struct ifindex_entry *e;
401 IFNET_WLOCK_ASSERT();
402 oldlim = V_if_indexlim;
404 n = (oldlim << 1) * sizeof(*e);
405 e = malloc(n, M_IFNET, M_WAITOK | M_ZERO);
407 if (V_if_indexlim != oldlim) {
411 if (V_ifindex_table != NULL) {
412 memcpy((caddr_t)e, (caddr_t)V_ifindex_table, n/2);
413 free((caddr_t)V_ifindex_table, M_IFNET);
420 * Allocate a struct ifnet and an index for an interface. A layer 2
421 * common structure will also be allocated if an allocation routine is
422 * registered for the passed type.
425 if_alloc(u_char type)
430 ifp = malloc(sizeof(struct ifnet), M_IFNET, M_WAITOK|M_ZERO);
432 if (ifindex_alloc_locked(&idx) != 0) {
437 ifnet_setbyindex_locked(idx, IFNET_HOLD);
441 ifp->if_alloctype = type;
442 if (if_com_alloc[type] != NULL) {
443 ifp->if_l2com = if_com_alloc[type](type, ifp);
444 if (ifp->if_l2com == NULL) {
451 IF_ADDR_LOCK_INIT(ifp);
452 TASK_INIT(&ifp->if_linktask, 0, do_link_state_change, ifp);
453 ifp->if_afdata_initialized = 0;
454 IF_AFDATA_LOCK_INIT(ifp);
455 TAILQ_INIT(&ifp->if_addrhead);
456 TAILQ_INIT(&ifp->if_prefixhead);
457 TAILQ_INIT(&ifp->if_multiaddrs);
458 TAILQ_INIT(&ifp->if_groups);
462 ifq_init(&ifp->if_snd, ifp);
464 refcount_init(&ifp->if_refcount, 1); /* Index reference. */
465 ifnet_setbyindex(ifp->if_index, ifp);
470 * Do the actual work of freeing a struct ifnet, and layer 2 common
471 * structure. This call is made when the last reference to an
472 * interface is released.
475 if_free_internal(struct ifnet *ifp)
478 KASSERT((ifp->if_flags & IFF_DYING),
479 ("if_free_internal: interface not dying"));
481 if (if_com_free[ifp->if_alloctype] != NULL)
482 if_com_free[ifp->if_alloctype](ifp->if_l2com,
486 mac_ifnet_destroy(ifp);
488 if (ifp->if_description != NULL)
489 free(ifp->if_description, M_IFDESCR);
490 IF_AFDATA_DESTROY(ifp);
491 IF_ADDR_LOCK_DESTROY(ifp);
492 ifq_delete(&ifp->if_snd);
497 * This version should only be called by intefaces that switch their type
498 * after calling if_alloc(). if_free_type() will go away again now that we
499 * have if_alloctype to cache the original allocation type. For now, assert
500 * that they match, since we require that in practice.
503 if_free_type(struct ifnet *ifp, u_char type)
506 KASSERT(ifp->if_alloctype == type,
507 ("if_free_type: type (%d) != alloctype (%d)", type,
510 ifp->if_flags |= IFF_DYING; /* XXX: Locking */
512 CURVNET_SET_QUIET(ifp->if_vnet);
514 KASSERT(ifp == ifnet_byindex_locked(ifp->if_index),
515 ("%s: freeing unallocated ifnet", ifp->if_xname));
517 ifindex_free_locked(ifp->if_index);
520 if (refcount_release(&ifp->if_refcount))
521 if_free_internal(ifp);
526 * This is the normal version of if_free(), used by device drivers to free a
527 * detached network interface. The contents of if_free_type() will move into
528 * here when if_free_type() goes away.
531 if_free(struct ifnet *ifp)
534 if_free_type(ifp, ifp->if_alloctype);
538 * Interfaces to keep an ifnet type-stable despite the possibility of the
539 * driver calling if_free(). If there are additional references, we defer
540 * freeing the underlying data structure.
543 if_ref(struct ifnet *ifp)
546 /* We don't assert the ifnet list lock here, but arguably should. */
547 refcount_acquire(&ifp->if_refcount);
551 if_rele(struct ifnet *ifp)
554 if (!refcount_release(&ifp->if_refcount))
556 if_free_internal(ifp);
560 ifq_init(struct ifaltq *ifq, struct ifnet *ifp)
563 mtx_init(&ifq->ifq_mtx, ifp->if_xname, "if send queue", MTX_DEF);
565 if (ifq->ifq_maxlen == 0)
566 ifq->ifq_maxlen = ifqmaxlen;
569 ifq->altq_disc = NULL;
570 ifq->altq_flags &= ALTQF_CANTCHANGE;
571 ifq->altq_tbr = NULL;
576 ifq_delete(struct ifaltq *ifq)
578 mtx_destroy(&ifq->ifq_mtx);
582 * Perform generic interface initalization tasks and attach the interface
583 * to the list of "active" interfaces. If vmove flag is set on entry
584 * to if_attach_internal(), perform only a limited subset of initialization
585 * tasks, given that we are moving from one vnet to another an ifnet which
586 * has already been fully initialized.
589 * - The decision to return void and thus require this function to
590 * succeed is questionable.
591 * - We should probably do more sanity checking. For instance we don't
592 * do anything to insure if_xname is unique or non-empty.
595 if_attach(struct ifnet *ifp)
598 if_attach_internal(ifp, 0);
602 if_attach_internal(struct ifnet *ifp, int vmove)
604 unsigned socksize, ifasize;
605 int namelen, masklen;
606 struct sockaddr_dl *sdl;
609 if (ifp->if_index == 0 || ifp != ifnet_byindex(ifp->if_index))
610 panic ("%s: BUG: if_attach called without if_alloc'd input()\n",
614 ifp->if_vnet = curvnet;
615 if (ifp->if_home_vnet == NULL)
616 ifp->if_home_vnet = curvnet;
619 if_addgroup(ifp, IFG_ALL);
621 getmicrotime(&ifp->if_lastchange);
622 ifp->if_data.ifi_epoch = time_uptime;
623 ifp->if_data.ifi_datalen = sizeof(struct if_data);
625 KASSERT((ifp->if_transmit == NULL && ifp->if_qflush == NULL) ||
626 (ifp->if_transmit != NULL && ifp->if_qflush != NULL),
627 ("transmit and qflush must both either be set or both be NULL"));
628 if (ifp->if_transmit == NULL) {
629 ifp->if_transmit = if_transmit;
630 ifp->if_qflush = if_qflush;
635 mac_ifnet_create(ifp);
639 * Create a Link Level name for this device.
641 namelen = strlen(ifp->if_xname);
643 * Always save enough space for any possiable name so we
644 * can do a rename in place later.
646 masklen = offsetof(struct sockaddr_dl, sdl_data[0]) + IFNAMSIZ;
647 socksize = masklen + ifp->if_addrlen;
648 if (socksize < sizeof(*sdl))
649 socksize = sizeof(*sdl);
650 socksize = roundup2(socksize, sizeof(long));
651 ifasize = sizeof(*ifa) + 2 * socksize;
652 ifa = malloc(ifasize, M_IFADDR, M_WAITOK | M_ZERO);
654 sdl = (struct sockaddr_dl *)(ifa + 1);
655 sdl->sdl_len = socksize;
656 sdl->sdl_family = AF_LINK;
657 bcopy(ifp->if_xname, sdl->sdl_data, namelen);
658 sdl->sdl_nlen = namelen;
659 sdl->sdl_index = ifp->if_index;
660 sdl->sdl_type = ifp->if_type;
663 ifa->ifa_rtrequest = link_rtrequest;
664 ifa->ifa_addr = (struct sockaddr *)sdl;
665 sdl = (struct sockaddr_dl *)(socksize + (caddr_t)sdl);
666 ifa->ifa_netmask = (struct sockaddr *)sdl;
667 sdl->sdl_len = masklen;
669 sdl->sdl_data[--namelen] = 0xff;
670 TAILQ_INSERT_HEAD(&ifp->if_addrhead, ifa, ifa_link);
671 /* Reliably crash if used uninitialized. */
672 ifp->if_broadcastaddr = NULL;
674 #if defined(INET) || defined(INET6)
675 /* Initialize to max value. */
676 if (ifp->if_hw_tsomax == 0)
677 ifp->if_hw_tsomax = IP_MAXPACKET;
678 KASSERT(ifp->if_hw_tsomax <= IP_MAXPACKET &&
679 ifp->if_hw_tsomax >= IP_MAXPACKET / 8,
680 ("%s: tsomax outside of range", __func__));
686 * Update the interface index in the link layer address
689 for (ifa = ifp->if_addr; ifa != NULL;
690 ifa = TAILQ_NEXT(ifa, ifa_link)) {
691 if (ifa->ifa_addr->sa_family == AF_LINK) {
692 sdl = (struct sockaddr_dl *)ifa->ifa_addr;
693 sdl->sdl_index = ifp->if_index;
700 TAILQ_INSERT_TAIL(&V_ifnet, ifp, if_link);
702 curvnet->vnet_ifcnt++;
706 if (domain_init_status >= 2)
707 if_attachdomain1(ifp);
709 EVENTHANDLER_INVOKE(ifnet_arrival_event, ifp);
710 if (IS_DEFAULT_VNET(curvnet))
711 devctl_notify("IFNET", ifp->if_xname, "ATTACH", NULL);
713 /* Announce the interface. */
714 rt_ifannouncemsg(ifp, IFAN_ARRIVAL);
718 if_attachdomain(void *dummy)
724 TAILQ_FOREACH(ifp, &V_ifnet, if_link)
725 if_attachdomain1(ifp);
728 SYSINIT(domainifattach, SI_SUB_PROTO_IFATTACHDOMAIN, SI_ORDER_SECOND,
729 if_attachdomain, NULL);
732 if_attachdomain1(struct ifnet *ifp)
740 * Since dp->dom_ifattach calls malloc() with M_WAITOK, we
741 * cannot lock ifp->if_afdata initialization, entirely.
743 if (IF_AFDATA_TRYLOCK(ifp) == 0) {
747 if (ifp->if_afdata_initialized >= domain_init_status) {
748 IF_AFDATA_UNLOCK(ifp);
750 printf("if_attachdomain called more than once on %s\n",
754 ifp->if_afdata_initialized = domain_init_status;
755 IF_AFDATA_UNLOCK(ifp);
757 /* address family dependent data region */
758 bzero(ifp->if_afdata, sizeof(ifp->if_afdata));
759 for (dp = domains; dp; dp = dp->dom_next) {
760 if (dp->dom_ifattach)
761 ifp->if_afdata[dp->dom_family] =
762 (*dp->dom_ifattach)(ifp);
769 * Remove any unicast or broadcast network addresses from an interface.
772 if_purgeaddrs(struct ifnet *ifp)
774 struct ifaddr *ifa, *next;
776 TAILQ_FOREACH_SAFE(ifa, &ifp->if_addrhead, ifa_link, next) {
777 if (ifa->ifa_addr->sa_family == AF_LINK)
780 /* XXX: Ugly!! ad hoc just for INET */
781 if (ifa->ifa_addr->sa_family == AF_INET) {
782 struct ifaliasreq ifr;
784 bzero(&ifr, sizeof(ifr));
785 ifr.ifra_addr = *ifa->ifa_addr;
786 if (ifa->ifa_dstaddr)
787 ifr.ifra_broadaddr = *ifa->ifa_dstaddr;
788 if (in_control(NULL, SIOCDIFADDR, (caddr_t)&ifr, ifp,
794 if (ifa->ifa_addr->sa_family == AF_INET6) {
796 /* ifp_addrhead is already updated */
800 TAILQ_REMOVE(&ifp->if_addrhead, ifa, ifa_link);
806 * Remove any multicast network addresses from an interface when an ifnet
810 if_purgemaddrs(struct ifnet *ifp)
812 struct ifmultiaddr *ifma;
813 struct ifmultiaddr *next;
816 TAILQ_FOREACH_SAFE(ifma, &ifp->if_multiaddrs, ifma_link, next)
817 if_delmulti_locked(ifp, ifma, 1);
818 IF_ADDR_WUNLOCK(ifp);
822 * Detach an interface, removing it from the list of "active" interfaces.
823 * If vmove flag is set on entry to if_detach_internal(), perform only a
824 * limited subset of cleanup tasks, given that we are moving an ifnet from
825 * one vnet to another, where it must be fully operational.
827 * XXXRW: There are some significant questions about event ordering, and
828 * how to prevent things from starting to use the interface during detach.
831 if_detach(struct ifnet *ifp)
834 CURVNET_SET_QUIET(ifp->if_vnet);
835 if_detach_internal(ifp, 0);
840 if_detach_internal(struct ifnet *ifp, int vmove)
843 struct radix_node_head *rnh;
850 TAILQ_FOREACH(iter, &V_ifnet, if_link)
852 TAILQ_REMOVE(&V_ifnet, ifp, if_link);
858 curvnet->vnet_ifcnt--;
863 panic("%s: ifp=%p not on the ifnet tailq %p",
864 __func__, ifp, &V_ifnet);
866 return; /* XXX this should panic as well? */
870 * Remove/wait for pending events.
872 taskqueue_drain(taskqueue_swi, &ifp->if_linktask);
875 * Remove routes and flush queues.
879 if (ALTQ_IS_ENABLED(&ifp->if_snd))
880 altq_disable(&ifp->if_snd);
881 if (ALTQ_IS_ATTACHED(&ifp->if_snd))
882 altq_detach(&ifp->if_snd);
893 * Remove all IPv6 kernel structs related to ifp. This should be done
894 * before removing routing entries below, since IPv6 interface direct
895 * routes are expected to be removed by the IPv6-specific kernel API.
896 * Otherwise, the kernel will detect some inconsistency and bark it.
904 * Prevent further calls into the device driver via ifnet.
909 * Remove link ifaddr pointer and maybe decrement if_index.
910 * Clean up all addresses.
914 /* We can now free link ifaddr. */
915 if (!TAILQ_EMPTY(&ifp->if_addrhead)) {
916 ifa = TAILQ_FIRST(&ifp->if_addrhead);
917 TAILQ_REMOVE(&ifp->if_addrhead, ifa, ifa_link);
923 * Delete all remaining routes using this interface
924 * Unfortuneatly the only way to do this is to slog through
925 * the entire routing table looking for routes which point
926 * to this interface...oh well...
928 for (i = 1; i <= AF_MAX; i++) {
929 for (j = 0; j < rt_numfibs; j++) {
930 rnh = rt_tables_get_rnh(j, i);
933 RADIX_NODE_HEAD_LOCK(rnh);
934 (void) rnh->rnh_walktree(rnh, if_rtdel, ifp);
935 RADIX_NODE_HEAD_UNLOCK(rnh);
939 /* Announce that the interface is gone. */
940 rt_ifannouncemsg(ifp, IFAN_DEPARTURE);
941 EVENTHANDLER_INVOKE(ifnet_departure_event, ifp);
942 if (IS_DEFAULT_VNET(curvnet))
943 devctl_notify("IFNET", ifp->if_xname, "DETACH", NULL);
947 * We cannot hold the lock over dom_ifdetach calls as they might
948 * sleep, for example trying to drain a callout, thus open up the
949 * theoretical race with re-attaching.
952 i = ifp->if_afdata_initialized;
953 ifp->if_afdata_initialized = 0;
954 IF_AFDATA_UNLOCK(ifp);
955 for (dp = domains; i > 0 && dp; dp = dp->dom_next) {
956 if (dp->dom_ifdetach && ifp->if_afdata[dp->dom_family])
957 (*dp->dom_ifdetach)(ifp,
958 ifp->if_afdata[dp->dom_family]);
964 * if_vmove() performs a limited version of if_detach() in current
965 * vnet and if_attach()es the ifnet to the vnet specified as 2nd arg.
966 * An attempt is made to shrink if_index in current vnet, find an
967 * unused if_index in target vnet and calls if_grow() if necessary,
968 * and finally find an unused if_xname for the target vnet.
971 if_vmove(struct ifnet *ifp, struct vnet *new_vnet)
976 * Detach from current vnet, but preserve LLADDR info, do not
977 * mark as dead etc. so that the ifnet can be reattached later.
979 if_detach_internal(ifp, 1);
982 * Unlink the ifnet from ifindex_table[] in current vnet, and shrink
983 * the if_index for that vnet if possible.
985 * NOTE: IFNET_WLOCK/IFNET_WUNLOCK() are assumed to be unvirtualized,
986 * or we'd lock on one vnet and unlock on another.
989 ifindex_free_locked(ifp->if_index);
993 * Perform interface-specific reassignment tasks, if provided by
996 if (ifp->if_reassign != NULL)
997 ifp->if_reassign(ifp, new_vnet, NULL);
1000 * Switch to the context of the target vnet.
1002 CURVNET_SET_QUIET(new_vnet);
1005 if (ifindex_alloc_locked(&idx) != 0) {
1007 panic("if_index overflow");
1009 ifp->if_index = idx;
1010 ifnet_setbyindex_locked(ifp->if_index, ifp);
1013 if_attach_internal(ifp, 1);
1019 * Move an ifnet to or from another child prison/vnet, specified by the jail id.
1022 if_vmove_loan(struct thread *td, struct ifnet *ifp, char *ifname, int jid)
1027 /* Try to find the prison within our visibility. */
1028 sx_slock(&allprison_lock);
1029 pr = prison_find_child(td->td_ucred->cr_prison, jid);
1030 sx_sunlock(&allprison_lock);
1033 prison_hold_locked(pr);
1034 mtx_unlock(&pr->pr_mtx);
1036 /* Do not try to move the iface from and to the same prison. */
1037 if (pr->pr_vnet == ifp->if_vnet) {
1042 /* Make sure the named iface does not exists in the dst. prison/vnet. */
1043 /* XXX Lock interfaces to avoid races. */
1044 CURVNET_SET_QUIET(pr->pr_vnet);
1045 difp = ifunit(ifname);
1052 /* Move the interface into the child jail/vnet. */
1053 if_vmove(ifp, pr->pr_vnet);
1055 /* Report the new if_xname back to the userland. */
1056 sprintf(ifname, "%s", ifp->if_xname);
1063 if_vmove_reclaim(struct thread *td, char *ifname, int jid)
1066 struct vnet *vnet_dst;
1069 /* Try to find the prison within our visibility. */
1070 sx_slock(&allprison_lock);
1071 pr = prison_find_child(td->td_ucred->cr_prison, jid);
1072 sx_sunlock(&allprison_lock);
1075 prison_hold_locked(pr);
1076 mtx_unlock(&pr->pr_mtx);
1078 /* Make sure the named iface exists in the source prison/vnet. */
1079 CURVNET_SET(pr->pr_vnet);
1080 ifp = ifunit(ifname); /* XXX Lock to avoid races. */
1087 /* Do not try to move the iface from and to the same prison. */
1088 vnet_dst = TD_TO_VNET(td);
1089 if (vnet_dst == ifp->if_vnet) {
1095 /* Get interface back from child jail/vnet. */
1096 if_vmove(ifp, vnet_dst);
1099 /* Report the new if_xname back to the userland. */
1100 sprintf(ifname, "%s", ifp->if_xname);
1108 * Add a group to an interface
1111 if_addgroup(struct ifnet *ifp, const char *groupname)
1113 struct ifg_list *ifgl;
1114 struct ifg_group *ifg = NULL;
1115 struct ifg_member *ifgm;
1117 if (groupname[0] && groupname[strlen(groupname) - 1] >= '0' &&
1118 groupname[strlen(groupname) - 1] <= '9')
1122 TAILQ_FOREACH(ifgl, &ifp->if_groups, ifgl_next)
1123 if (!strcmp(ifgl->ifgl_group->ifg_group, groupname)) {
1128 if ((ifgl = (struct ifg_list *)malloc(sizeof(struct ifg_list), M_TEMP,
1129 M_NOWAIT)) == NULL) {
1134 if ((ifgm = (struct ifg_member *)malloc(sizeof(struct ifg_member),
1135 M_TEMP, M_NOWAIT)) == NULL) {
1141 TAILQ_FOREACH(ifg, &V_ifg_head, ifg_next)
1142 if (!strcmp(ifg->ifg_group, groupname))
1146 if ((ifg = (struct ifg_group *)malloc(sizeof(struct ifg_group),
1147 M_TEMP, M_NOWAIT)) == NULL) {
1153 strlcpy(ifg->ifg_group, groupname, sizeof(ifg->ifg_group));
1154 ifg->ifg_refcnt = 0;
1155 TAILQ_INIT(&ifg->ifg_members);
1156 EVENTHANDLER_INVOKE(group_attach_event, ifg);
1157 TAILQ_INSERT_TAIL(&V_ifg_head, ifg, ifg_next);
1161 ifgl->ifgl_group = ifg;
1162 ifgm->ifgm_ifp = ifp;
1165 TAILQ_INSERT_TAIL(&ifg->ifg_members, ifgm, ifgm_next);
1166 TAILQ_INSERT_TAIL(&ifp->if_groups, ifgl, ifgl_next);
1167 IF_ADDR_WUNLOCK(ifp);
1171 EVENTHANDLER_INVOKE(group_change_event, groupname);
1177 * Remove a group from an interface
1180 if_delgroup(struct ifnet *ifp, const char *groupname)
1182 struct ifg_list *ifgl;
1183 struct ifg_member *ifgm;
1186 TAILQ_FOREACH(ifgl, &ifp->if_groups, ifgl_next)
1187 if (!strcmp(ifgl->ifgl_group->ifg_group, groupname))
1195 TAILQ_REMOVE(&ifp->if_groups, ifgl, ifgl_next);
1196 IF_ADDR_WUNLOCK(ifp);
1198 TAILQ_FOREACH(ifgm, &ifgl->ifgl_group->ifg_members, ifgm_next)
1199 if (ifgm->ifgm_ifp == ifp)
1203 TAILQ_REMOVE(&ifgl->ifgl_group->ifg_members, ifgm, ifgm_next);
1207 if (--ifgl->ifgl_group->ifg_refcnt == 0) {
1208 TAILQ_REMOVE(&V_ifg_head, ifgl->ifgl_group, ifg_next);
1209 EVENTHANDLER_INVOKE(group_detach_event, ifgl->ifgl_group);
1210 free(ifgl->ifgl_group, M_TEMP);
1216 EVENTHANDLER_INVOKE(group_change_event, groupname);
1222 * Remove an interface from all groups
1225 if_delgroups(struct ifnet *ifp)
1227 struct ifg_list *ifgl;
1228 struct ifg_member *ifgm;
1229 char groupname[IFNAMSIZ];
1232 while (!TAILQ_EMPTY(&ifp->if_groups)) {
1233 ifgl = TAILQ_FIRST(&ifp->if_groups);
1235 strlcpy(groupname, ifgl->ifgl_group->ifg_group, IFNAMSIZ);
1238 TAILQ_REMOVE(&ifp->if_groups, ifgl, ifgl_next);
1239 IF_ADDR_WUNLOCK(ifp);
1241 TAILQ_FOREACH(ifgm, &ifgl->ifgl_group->ifg_members, ifgm_next)
1242 if (ifgm->ifgm_ifp == ifp)
1246 TAILQ_REMOVE(&ifgl->ifgl_group->ifg_members, ifgm,
1251 if (--ifgl->ifgl_group->ifg_refcnt == 0) {
1252 TAILQ_REMOVE(&V_ifg_head, ifgl->ifgl_group, ifg_next);
1253 EVENTHANDLER_INVOKE(group_detach_event,
1255 free(ifgl->ifgl_group, M_TEMP);
1261 EVENTHANDLER_INVOKE(group_change_event, groupname);
1269 * Stores all groups from an interface in memory pointed
1273 if_getgroup(struct ifgroupreq *data, struct ifnet *ifp)
1276 struct ifg_list *ifgl;
1277 struct ifg_req ifgrq, *ifgp;
1278 struct ifgroupreq *ifgr = data;
1280 if (ifgr->ifgr_len == 0) {
1282 TAILQ_FOREACH(ifgl, &ifp->if_groups, ifgl_next)
1283 ifgr->ifgr_len += sizeof(struct ifg_req);
1284 IF_ADDR_RUNLOCK(ifp);
1288 len = ifgr->ifgr_len;
1289 ifgp = ifgr->ifgr_groups;
1292 TAILQ_FOREACH(ifgl, &ifp->if_groups, ifgl_next) {
1293 if (len < sizeof(ifgrq)) {
1294 IF_ADDR_RUNLOCK(ifp);
1297 bzero(&ifgrq, sizeof ifgrq);
1298 strlcpy(ifgrq.ifgrq_group, ifgl->ifgl_group->ifg_group,
1299 sizeof(ifgrq.ifgrq_group));
1300 if ((error = copyout(&ifgrq, ifgp, sizeof(struct ifg_req)))) {
1301 IF_ADDR_RUNLOCK(ifp);
1304 len -= sizeof(ifgrq);
1307 IF_ADDR_RUNLOCK(ifp);
1313 * Stores all members of a group in memory pointed to by data
1316 if_getgroupmembers(struct ifgroupreq *data)
1318 struct ifgroupreq *ifgr = data;
1319 struct ifg_group *ifg;
1320 struct ifg_member *ifgm;
1321 struct ifg_req ifgrq, *ifgp;
1325 TAILQ_FOREACH(ifg, &V_ifg_head, ifg_next)
1326 if (!strcmp(ifg->ifg_group, ifgr->ifgr_name))
1333 if (ifgr->ifgr_len == 0) {
1334 TAILQ_FOREACH(ifgm, &ifg->ifg_members, ifgm_next)
1335 ifgr->ifgr_len += sizeof(ifgrq);
1340 len = ifgr->ifgr_len;
1341 ifgp = ifgr->ifgr_groups;
1342 TAILQ_FOREACH(ifgm, &ifg->ifg_members, ifgm_next) {
1343 if (len < sizeof(ifgrq)) {
1347 bzero(&ifgrq, sizeof ifgrq);
1348 strlcpy(ifgrq.ifgrq_member, ifgm->ifgm_ifp->if_xname,
1349 sizeof(ifgrq.ifgrq_member));
1350 if ((error = copyout(&ifgrq, ifgp, sizeof(struct ifg_req)))) {
1354 len -= sizeof(ifgrq);
1363 * Delete Routes for a Network Interface
1365 * Called for each routing entry via the rnh->rnh_walktree() call above
1366 * to delete all route entries referencing a detaching network interface.
1369 * rn pointer to node in the routing table
1370 * arg argument passed to rnh->rnh_walktree() - detaching interface
1374 * errno failed - reason indicated
1378 if_rtdel(struct radix_node *rn, void *arg)
1380 struct rtentry *rt = (struct rtentry *)rn;
1381 struct ifnet *ifp = arg;
1384 if (rt->rt_ifp == ifp) {
1387 * Protect (sorta) against walktree recursion problems
1388 * with cloned routes
1390 if ((rt->rt_flags & RTF_UP) == 0)
1393 err = rtrequest_fib(RTM_DELETE, rt_key(rt), rt->rt_gateway,
1395 rt->rt_flags|RTF_RNH_LOCKED|RTF_PINNED,
1396 (struct rtentry **) NULL, rt->rt_fibnum);
1398 log(LOG_WARNING, "if_rtdel: error %d\n", err);
1406 * Wrapper functions for struct ifnet address list locking macros. These are
1407 * used by kernel modules to avoid encoding programming interface or binary
1408 * interface assumptions that may be violated when kernel-internal locking
1409 * approaches change.
1412 if_addr_rlock(struct ifnet *ifp)
1419 if_addr_runlock(struct ifnet *ifp)
1422 IF_ADDR_RUNLOCK(ifp);
1426 if_maddr_rlock(struct ifnet *ifp)
1433 if_maddr_runlock(struct ifnet *ifp)
1436 IF_ADDR_RUNLOCK(ifp);
1440 * Reference count functions for ifaddrs.
1443 ifa_init(struct ifaddr *ifa)
1446 mtx_init(&ifa->ifa_mtx, "ifaddr", NULL, MTX_DEF);
1447 refcount_init(&ifa->ifa_refcnt, 1);
1451 ifa_ref(struct ifaddr *ifa)
1454 refcount_acquire(&ifa->ifa_refcnt);
1458 ifa_free(struct ifaddr *ifa)
1461 if (refcount_release(&ifa->ifa_refcnt)) {
1462 mtx_destroy(&ifa->ifa_mtx);
1463 free(ifa, M_IFADDR);
1468 ifa_add_loopback_route(struct ifaddr *ifa, struct sockaddr *ia)
1471 struct rtentry *rt = NULL;
1472 struct rt_addrinfo info;
1473 static struct sockaddr_dl null_sdl = {sizeof(null_sdl), AF_LINK};
1475 bzero(&info, sizeof(info));
1476 info.rti_ifp = V_loif;
1477 info.rti_flags = ifa->ifa_flags | RTF_HOST | RTF_STATIC;
1478 info.rti_info[RTAX_DST] = ia;
1479 info.rti_info[RTAX_GATEWAY] = (struct sockaddr *)&null_sdl;
1480 error = rtrequest1_fib(RTM_ADD, &info, &rt, 0);
1482 if (error == 0 && rt != NULL) {
1484 ((struct sockaddr_dl *)rt->rt_gateway)->sdl_type =
1485 ifa->ifa_ifp->if_type;
1486 ((struct sockaddr_dl *)rt->rt_gateway)->sdl_index =
1487 ifa->ifa_ifp->if_index;
1490 } else if (error != 0)
1491 log(LOG_INFO, "ifa_add_loopback_route: insertion failed\n");
1497 ifa_del_loopback_route(struct ifaddr *ifa, struct sockaddr *ia)
1500 struct rt_addrinfo info;
1501 struct sockaddr_dl null_sdl;
1503 bzero(&null_sdl, sizeof(null_sdl));
1504 null_sdl.sdl_len = sizeof(null_sdl);
1505 null_sdl.sdl_family = AF_LINK;
1506 null_sdl.sdl_type = ifa->ifa_ifp->if_type;
1507 null_sdl.sdl_index = ifa->ifa_ifp->if_index;
1508 bzero(&info, sizeof(info));
1509 info.rti_flags = ifa->ifa_flags | RTF_HOST | RTF_STATIC;
1510 info.rti_info[RTAX_DST] = ia;
1511 info.rti_info[RTAX_GATEWAY] = (struct sockaddr *)&null_sdl;
1512 error = rtrequest1_fib(RTM_DELETE, &info, NULL, 0);
1515 log(LOG_INFO, "ifa_del_loopback_route: deletion failed\n");
1521 * XXX: Because sockaddr_dl has deeper structure than the sockaddr
1522 * structs used to represent other address families, it is necessary
1523 * to perform a different comparison.
1526 #define sa_equal(a1, a2) \
1527 (bcmp((a1), (a2), ((a1))->sa_len) == 0)
1529 #define sa_dl_equal(a1, a2) \
1530 ((((struct sockaddr_dl *)(a1))->sdl_len == \
1531 ((struct sockaddr_dl *)(a2))->sdl_len) && \
1532 (bcmp(LLADDR((struct sockaddr_dl *)(a1)), \
1533 LLADDR((struct sockaddr_dl *)(a2)), \
1534 ((struct sockaddr_dl *)(a1))->sdl_alen) == 0))
1537 * Locate an interface based on a complete address.
1540 static struct ifaddr *
1541 ifa_ifwithaddr_internal(struct sockaddr *addr, int getref)
1546 IFNET_RLOCK_NOSLEEP();
1547 TAILQ_FOREACH(ifp, &V_ifnet, if_link) {
1549 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
1550 if (ifa->ifa_addr->sa_family != addr->sa_family)
1552 if (sa_equal(addr, ifa->ifa_addr)) {
1555 IF_ADDR_RUNLOCK(ifp);
1558 /* IP6 doesn't have broadcast */
1559 if ((ifp->if_flags & IFF_BROADCAST) &&
1560 ifa->ifa_broadaddr &&
1561 ifa->ifa_broadaddr->sa_len != 0 &&
1562 sa_equal(ifa->ifa_broadaddr, addr)) {
1565 IF_ADDR_RUNLOCK(ifp);
1569 IF_ADDR_RUNLOCK(ifp);
1573 IFNET_RUNLOCK_NOSLEEP();
1578 ifa_ifwithaddr(struct sockaddr *addr)
1581 return (ifa_ifwithaddr_internal(addr, 1));
1585 ifa_ifwithaddr_check(struct sockaddr *addr)
1588 return (ifa_ifwithaddr_internal(addr, 0) != NULL);
1592 * Locate an interface based on the broadcast address.
1596 ifa_ifwithbroadaddr(struct sockaddr *addr)
1601 IFNET_RLOCK_NOSLEEP();
1602 TAILQ_FOREACH(ifp, &V_ifnet, if_link) {
1604 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
1605 if (ifa->ifa_addr->sa_family != addr->sa_family)
1607 if ((ifp->if_flags & IFF_BROADCAST) &&
1608 ifa->ifa_broadaddr &&
1609 ifa->ifa_broadaddr->sa_len != 0 &&
1610 sa_equal(ifa->ifa_broadaddr, addr)) {
1612 IF_ADDR_RUNLOCK(ifp);
1616 IF_ADDR_RUNLOCK(ifp);
1620 IFNET_RUNLOCK_NOSLEEP();
1625 * Locate the point to point interface with a given destination address.
1629 ifa_ifwithdstaddr(struct sockaddr *addr)
1634 IFNET_RLOCK_NOSLEEP();
1635 TAILQ_FOREACH(ifp, &V_ifnet, if_link) {
1636 if ((ifp->if_flags & IFF_POINTOPOINT) == 0)
1639 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
1640 if (ifa->ifa_addr->sa_family != addr->sa_family)
1642 if (ifa->ifa_dstaddr != NULL &&
1643 sa_equal(addr, ifa->ifa_dstaddr)) {
1645 IF_ADDR_RUNLOCK(ifp);
1649 IF_ADDR_RUNLOCK(ifp);
1653 IFNET_RUNLOCK_NOSLEEP();
1658 * Find an interface on a specific network. If many, choice
1659 * is most specific found.
1662 ifa_ifwithnet(struct sockaddr *addr, int ignore_ptp)
1666 struct ifaddr *ifa_maybe = NULL;
1667 u_int af = addr->sa_family;
1668 char *addr_data = addr->sa_data, *cplim;
1671 * AF_LINK addresses can be looked up directly by their index number,
1672 * so do that if we can.
1674 if (af == AF_LINK) {
1675 struct sockaddr_dl *sdl = (struct sockaddr_dl *)addr;
1676 if (sdl->sdl_index && sdl->sdl_index <= V_if_index)
1677 return (ifaddr_byindex(sdl->sdl_index));
1681 * Scan though each interface, looking for ones that have addresses
1682 * in this address family. Maintain a reference on ifa_maybe once
1683 * we find one, as we release the IF_ADDR_RLOCK() that kept it stable
1684 * when we move onto the next interface.
1686 IFNET_RLOCK_NOSLEEP();
1687 TAILQ_FOREACH(ifp, &V_ifnet, if_link) {
1689 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
1690 char *cp, *cp2, *cp3;
1692 if (ifa->ifa_addr->sa_family != af)
1694 if (af == AF_INET &&
1695 ifp->if_flags & IFF_POINTOPOINT && !ignore_ptp) {
1697 * This is a bit broken as it doesn't
1698 * take into account that the remote end may
1699 * be a single node in the network we are
1701 * The trouble is that we don't know the
1702 * netmask for the remote end.
1704 if (ifa->ifa_dstaddr != NULL &&
1705 sa_equal(addr, ifa->ifa_dstaddr)) {
1707 IF_ADDR_RUNLOCK(ifp);
1712 * if we have a special address handler,
1713 * then use it instead of the generic one.
1715 if (ifa->ifa_claim_addr) {
1716 if ((*ifa->ifa_claim_addr)(ifa, addr)) {
1718 IF_ADDR_RUNLOCK(ifp);
1725 * Scan all the bits in the ifa's address.
1726 * If a bit dissagrees with what we are
1727 * looking for, mask it with the netmask
1728 * to see if it really matters.
1729 * (A byte at a time)
1731 if (ifa->ifa_netmask == 0)
1734 cp2 = ifa->ifa_addr->sa_data;
1735 cp3 = ifa->ifa_netmask->sa_data;
1736 cplim = ifa->ifa_netmask->sa_len
1737 + (char *)ifa->ifa_netmask;
1739 if ((*cp++ ^ *cp2++) & *cp3++)
1740 goto next; /* next address! */
1742 * If the netmask of what we just found
1743 * is more specific than what we had before
1744 * (if we had one) then remember the new one
1745 * before continuing to search
1746 * for an even better one.
1748 if (ifa_maybe == NULL ||
1749 rn_refines((caddr_t)ifa->ifa_netmask,
1750 (caddr_t)ifa_maybe->ifa_netmask)) {
1751 if (ifa_maybe != NULL)
1752 ifa_free(ifa_maybe);
1758 IF_ADDR_RUNLOCK(ifp);
1763 IFNET_RUNLOCK_NOSLEEP();
1764 if (ifa_maybe != NULL)
1765 ifa_free(ifa_maybe);
1770 * Find an interface address specific to an interface best matching
1774 ifaof_ifpforaddr(struct sockaddr *addr, struct ifnet *ifp)
1777 char *cp, *cp2, *cp3;
1779 struct ifaddr *ifa_maybe = NULL;
1780 u_int af = addr->sa_family;
1785 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
1786 if (ifa->ifa_addr->sa_family != af)
1788 if (ifa_maybe == NULL)
1790 if (ifa->ifa_netmask == 0) {
1791 if (sa_equal(addr, ifa->ifa_addr) ||
1792 (ifa->ifa_dstaddr &&
1793 sa_equal(addr, ifa->ifa_dstaddr)))
1797 if (ifp->if_flags & IFF_POINTOPOINT) {
1798 if (sa_equal(addr, ifa->ifa_dstaddr))
1802 cp2 = ifa->ifa_addr->sa_data;
1803 cp3 = ifa->ifa_netmask->sa_data;
1804 cplim = ifa->ifa_netmask->sa_len + (char *)ifa->ifa_netmask;
1805 for (; cp3 < cplim; cp3++)
1806 if ((*cp++ ^ *cp2++) & *cp3)
1816 IF_ADDR_RUNLOCK(ifp);
1820 #include <net/if_llatbl.h>
1823 * Default action when installing a route with a Link Level gateway.
1824 * Lookup an appropriate real ifa to point to.
1825 * This should be moved to /sys/net/link.c eventually.
1828 link_rtrequest(int cmd, struct rtentry *rt, struct rt_addrinfo *info)
1830 struct ifaddr *ifa, *oifa;
1831 struct sockaddr *dst;
1836 if (cmd != RTM_ADD || ((ifa = rt->rt_ifa) == 0) ||
1837 ((ifp = ifa->ifa_ifp) == 0) || ((dst = rt_key(rt)) == 0))
1839 ifa = ifaof_ifpforaddr(dst, ifp);
1844 if (ifa->ifa_rtrequest && ifa->ifa_rtrequest != link_rtrequest)
1845 ifa->ifa_rtrequest(cmd, rt, info);
1850 * Mark an interface down and notify protocols of
1852 * NOTE: must be called at splnet or eqivalent.
1855 if_unroute(struct ifnet *ifp, int flag, int fam)
1859 KASSERT(flag == IFF_UP, ("if_unroute: flag != IFF_UP"));
1861 ifp->if_flags &= ~flag;
1862 getmicrotime(&ifp->if_lastchange);
1863 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link)
1864 if (fam == PF_UNSPEC || (fam == ifa->ifa_addr->sa_family))
1865 pfctlinput(PRC_IFDOWN, ifa->ifa_addr);
1866 ifp->if_qflush(ifp);
1869 (*carp_linkstate_p)(ifp);
1874 * Mark an interface up and notify protocols of
1876 * NOTE: must be called at splnet or eqivalent.
1879 if_route(struct ifnet *ifp, int flag, int fam)
1883 KASSERT(flag == IFF_UP, ("if_route: flag != IFF_UP"));
1885 ifp->if_flags |= flag;
1886 getmicrotime(&ifp->if_lastchange);
1887 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link)
1888 if (fam == PF_UNSPEC || (fam == ifa->ifa_addr->sa_family))
1889 pfctlinput(PRC_IFUP, ifa->ifa_addr);
1891 (*carp_linkstate_p)(ifp);
1898 void (*vlan_link_state_p)(struct ifnet *); /* XXX: private from if_vlan */
1899 void (*vlan_trunk_cap_p)(struct ifnet *); /* XXX: private from if_vlan */
1900 struct ifnet *(*vlan_trunkdev_p)(struct ifnet *);
1901 struct ifnet *(*vlan_devat_p)(struct ifnet *, uint16_t);
1902 int (*vlan_tag_p)(struct ifnet *, uint16_t *);
1903 int (*vlan_setcookie_p)(struct ifnet *, void *);
1904 void *(*vlan_cookie_p)(struct ifnet *);
1907 * Handle a change in the interface link state. To avoid LORs
1908 * between driver lock and upper layer locks, as well as possible
1909 * recursions, we post event to taskqueue, and all job
1910 * is done in static do_link_state_change().
1913 if_link_state_change(struct ifnet *ifp, int link_state)
1915 /* Return if state hasn't changed. */
1916 if (ifp->if_link_state == link_state)
1919 ifp->if_link_state = link_state;
1921 taskqueue_enqueue(taskqueue_swi, &ifp->if_linktask);
1925 do_link_state_change(void *arg, int pending)
1927 struct ifnet *ifp = (struct ifnet *)arg;
1928 int link_state = ifp->if_link_state;
1929 CURVNET_SET(ifp->if_vnet);
1931 /* Notify that the link state has changed. */
1933 if (ifp->if_vlantrunk != NULL)
1934 (*vlan_link_state_p)(ifp);
1936 if ((ifp->if_type == IFT_ETHER || ifp->if_type == IFT_L2VLAN) &&
1937 IFP2AC(ifp)->ac_netgraph != NULL)
1938 (*ng_ether_link_state_p)(ifp, link_state);
1940 (*carp_linkstate_p)(ifp);
1942 (*bridge_linkstate_p)(ifp);
1944 (*lagg_linkstate_p)(ifp, link_state);
1946 if (IS_DEFAULT_VNET(curvnet))
1947 devctl_notify("IFNET", ifp->if_xname,
1948 (link_state == LINK_STATE_UP) ? "LINK_UP" : "LINK_DOWN",
1951 if_printf(ifp, "%d link states coalesced\n", pending);
1952 if (log_link_state_change)
1953 log(LOG_NOTICE, "%s: link state changed to %s\n", ifp->if_xname,
1954 (link_state == LINK_STATE_UP) ? "UP" : "DOWN" );
1955 EVENTHANDLER_INVOKE(ifnet_link_event, ifp, ifp->if_link_state);
1960 * Mark an interface down and notify protocols of
1962 * NOTE: must be called at splnet or eqivalent.
1965 if_down(struct ifnet *ifp)
1968 if_unroute(ifp, IFF_UP, AF_UNSPEC);
1972 * Mark an interface up and notify protocols of
1974 * NOTE: must be called at splnet or eqivalent.
1977 if_up(struct ifnet *ifp)
1980 if_route(ifp, IFF_UP, AF_UNSPEC);
1984 * Flush an interface queue.
1987 if_qflush(struct ifnet *ifp)
1995 if (ALTQ_IS_ENABLED(ifq))
1999 while ((m = n) != 0) {
2010 * Map interface name to interface structure pointer, with or without
2011 * returning a reference.
2014 ifunit_ref(const char *name)
2018 IFNET_RLOCK_NOSLEEP();
2019 TAILQ_FOREACH(ifp, &V_ifnet, if_link) {
2020 if (strncmp(name, ifp->if_xname, IFNAMSIZ) == 0 &&
2021 !(ifp->if_flags & IFF_DYING))
2026 IFNET_RUNLOCK_NOSLEEP();
2031 ifunit(const char *name)
2035 IFNET_RLOCK_NOSLEEP();
2036 TAILQ_FOREACH(ifp, &V_ifnet, if_link) {
2037 if (strncmp(name, ifp->if_xname, IFNAMSIZ) == 0)
2040 IFNET_RUNLOCK_NOSLEEP();
2045 * Hardware specific interface ioctls.
2048 ifhwioctl(u_long cmd, struct ifnet *ifp, caddr_t data, struct thread *td)
2053 int new_flags, temp_flags;
2054 size_t namelen, onamelen;
2056 char *descrbuf, *odescrbuf;
2057 char new_name[IFNAMSIZ];
2059 struct sockaddr_dl *sdl;
2061 ifr = (struct ifreq *)data;
2064 ifr->ifr_index = ifp->if_index;
2068 temp_flags = ifp->if_flags | ifp->if_drv_flags;
2069 ifr->ifr_flags = temp_flags & 0xffff;
2070 ifr->ifr_flagshigh = temp_flags >> 16;
2074 ifr->ifr_reqcap = ifp->if_capabilities;
2075 ifr->ifr_curcap = ifp->if_capenable;
2080 error = mac_ifnet_ioctl_get(td->td_ucred, ifr, ifp);
2085 ifr->ifr_metric = ifp->if_metric;
2089 ifr->ifr_mtu = ifp->if_mtu;
2093 ifr->ifr_phys = ifp->if_physical;
2098 sx_slock(&ifdescr_sx);
2099 if (ifp->if_description == NULL)
2102 /* space for terminating nul */
2103 descrlen = strlen(ifp->if_description) + 1;
2104 if (ifr->ifr_buffer.length < descrlen)
2105 ifr->ifr_buffer.buffer = NULL;
2107 error = copyout(ifp->if_description,
2108 ifr->ifr_buffer.buffer, descrlen);
2109 ifr->ifr_buffer.length = descrlen;
2111 sx_sunlock(&ifdescr_sx);
2115 error = priv_check(td, PRIV_NET_SETIFDESCR);
2120 * Copy only (length-1) bytes to make sure that
2121 * if_description is always nul terminated. The
2122 * length parameter is supposed to count the
2123 * terminating nul in.
2125 if (ifr->ifr_buffer.length > ifdescr_maxlen)
2126 return (ENAMETOOLONG);
2127 else if (ifr->ifr_buffer.length == 0)
2130 descrbuf = malloc(ifr->ifr_buffer.length, M_IFDESCR,
2132 error = copyin(ifr->ifr_buffer.buffer, descrbuf,
2133 ifr->ifr_buffer.length - 1);
2135 free(descrbuf, M_IFDESCR);
2140 sx_xlock(&ifdescr_sx);
2141 odescrbuf = ifp->if_description;
2142 ifp->if_description = descrbuf;
2143 sx_xunlock(&ifdescr_sx);
2145 getmicrotime(&ifp->if_lastchange);
2146 free(odescrbuf, M_IFDESCR);
2150 ifr->ifr_fib = ifp->if_fib;
2154 error = priv_check(td, PRIV_NET_SETIFFIB);
2157 if (ifr->ifr_fib >= rt_numfibs)
2160 ifp->if_fib = ifr->ifr_fib;
2164 error = priv_check(td, PRIV_NET_SETIFFLAGS);
2168 * Currently, no driver owned flags pass the IFF_CANTCHANGE
2169 * check, so we don't need special handling here yet.
2171 new_flags = (ifr->ifr_flags & 0xffff) |
2172 (ifr->ifr_flagshigh << 16);
2173 if (ifp->if_flags & IFF_SMART) {
2174 /* Smart drivers twiddle their own routes */
2175 } else if (ifp->if_flags & IFF_UP &&
2176 (new_flags & IFF_UP) == 0) {
2180 } else if (new_flags & IFF_UP &&
2181 (ifp->if_flags & IFF_UP) == 0) {
2186 /* See if permanently promiscuous mode bit is about to flip */
2187 if ((ifp->if_flags ^ new_flags) & IFF_PPROMISC) {
2188 if (new_flags & IFF_PPROMISC)
2189 ifp->if_flags |= IFF_PROMISC;
2190 else if (ifp->if_pcount == 0)
2191 ifp->if_flags &= ~IFF_PROMISC;
2192 log(LOG_INFO, "%s: permanently promiscuous mode %s\n",
2194 (new_flags & IFF_PPROMISC) ? "enabled" : "disabled");
2196 ifp->if_flags = (ifp->if_flags & IFF_CANTCHANGE) |
2197 (new_flags &~ IFF_CANTCHANGE);
2198 if (ifp->if_ioctl) {
2199 (void) (*ifp->if_ioctl)(ifp, cmd, data);
2201 getmicrotime(&ifp->if_lastchange);
2205 error = priv_check(td, PRIV_NET_SETIFCAP);
2208 if (ifp->if_ioctl == NULL)
2209 return (EOPNOTSUPP);
2210 if (ifr->ifr_reqcap & ~ifp->if_capabilities)
2212 error = (*ifp->if_ioctl)(ifp, cmd, data);
2214 getmicrotime(&ifp->if_lastchange);
2219 error = mac_ifnet_ioctl_set(td->td_ucred, ifr, ifp);
2224 error = priv_check(td, PRIV_NET_SETIFNAME);
2227 error = copyinstr(ifr->ifr_data, new_name, IFNAMSIZ, NULL);
2230 if (new_name[0] == '\0')
2232 if (ifunit(new_name) != NULL)
2236 * XXX: Locking. Nothing else seems to lock if_flags,
2237 * and there are numerous other races with the
2238 * ifunit() checks not being atomic with namespace
2239 * changes (renames, vmoves, if_attach, etc).
2241 ifp->if_flags |= IFF_RENAMING;
2243 /* Announce the departure of the interface. */
2244 rt_ifannouncemsg(ifp, IFAN_DEPARTURE);
2245 EVENTHANDLER_INVOKE(ifnet_departure_event, ifp);
2247 log(LOG_INFO, "%s: changing name to '%s'\n",
2248 ifp->if_xname, new_name);
2250 strlcpy(ifp->if_xname, new_name, sizeof(ifp->if_xname));
2253 sdl = (struct sockaddr_dl *)ifa->ifa_addr;
2254 namelen = strlen(new_name);
2255 onamelen = sdl->sdl_nlen;
2257 * Move the address if needed. This is safe because we
2258 * allocate space for a name of length IFNAMSIZ when we
2259 * create this in if_attach().
2261 if (namelen != onamelen) {
2262 bcopy(sdl->sdl_data + onamelen,
2263 sdl->sdl_data + namelen, sdl->sdl_alen);
2265 bcopy(new_name, sdl->sdl_data, namelen);
2266 sdl->sdl_nlen = namelen;
2267 sdl = (struct sockaddr_dl *)ifa->ifa_netmask;
2268 bzero(sdl->sdl_data, onamelen);
2269 while (namelen != 0)
2270 sdl->sdl_data[--namelen] = 0xff;
2273 EVENTHANDLER_INVOKE(ifnet_arrival_event, ifp);
2274 /* Announce the return of the interface. */
2275 rt_ifannouncemsg(ifp, IFAN_ARRIVAL);
2277 ifp->if_flags &= ~IFF_RENAMING;
2282 error = priv_check(td, PRIV_NET_SETIFVNET);
2285 error = if_vmove_loan(td, ifp, ifr->ifr_name, ifr->ifr_jid);
2290 error = priv_check(td, PRIV_NET_SETIFMETRIC);
2293 ifp->if_metric = ifr->ifr_metric;
2294 getmicrotime(&ifp->if_lastchange);
2298 error = priv_check(td, PRIV_NET_SETIFPHYS);
2301 if (ifp->if_ioctl == NULL)
2302 return (EOPNOTSUPP);
2303 error = (*ifp->if_ioctl)(ifp, cmd, data);
2305 getmicrotime(&ifp->if_lastchange);
2310 u_long oldmtu = ifp->if_mtu;
2312 error = priv_check(td, PRIV_NET_SETIFMTU);
2315 if (ifr->ifr_mtu < IF_MINMTU || ifr->ifr_mtu > IF_MAXMTU)
2317 if (ifp->if_ioctl == NULL)
2318 return (EOPNOTSUPP);
2319 error = (*ifp->if_ioctl)(ifp, cmd, data);
2321 getmicrotime(&ifp->if_lastchange);
2325 * If the link MTU changed, do network layer specific procedure.
2327 if (ifp->if_mtu != oldmtu) {
2337 if (cmd == SIOCADDMULTI)
2338 error = priv_check(td, PRIV_NET_ADDMULTI);
2340 error = priv_check(td, PRIV_NET_DELMULTI);
2344 /* Don't allow group membership on non-multicast interfaces. */
2345 if ((ifp->if_flags & IFF_MULTICAST) == 0)
2346 return (EOPNOTSUPP);
2348 /* Don't let users screw up protocols' entries. */
2349 if (ifr->ifr_addr.sa_family != AF_LINK)
2352 if (cmd == SIOCADDMULTI) {
2353 struct ifmultiaddr *ifma;
2356 * Userland is only permitted to join groups once
2357 * via the if_addmulti() KPI, because it cannot hold
2358 * struct ifmultiaddr * between calls. It may also
2359 * lose a race while we check if the membership
2363 ifma = if_findmulti(ifp, &ifr->ifr_addr);
2364 IF_ADDR_RUNLOCK(ifp);
2368 error = if_addmulti(ifp, &ifr->ifr_addr, &ifma);
2370 error = if_delmulti(ifp, &ifr->ifr_addr);
2373 getmicrotime(&ifp->if_lastchange);
2376 case SIOCSIFPHYADDR:
2377 case SIOCDIFPHYADDR:
2379 case SIOCSIFPHYADDR_IN6:
2381 case SIOCSLIFPHYADDR:
2383 case SIOCSIFGENERIC:
2384 error = priv_check(td, PRIV_NET_HWIOCTL);
2387 if (ifp->if_ioctl == NULL)
2388 return (EOPNOTSUPP);
2389 error = (*ifp->if_ioctl)(ifp, cmd, data);
2391 getmicrotime(&ifp->if_lastchange);
2395 ifs = (struct ifstat *)data;
2396 ifs->ascii[0] = '\0';
2398 case SIOCGIFPSRCADDR:
2399 case SIOCGIFPDSTADDR:
2400 case SIOCGLIFPHYADDR:
2402 case SIOCGIFGENERIC:
2403 if (ifp->if_ioctl == NULL)
2404 return (EOPNOTSUPP);
2405 error = (*ifp->if_ioctl)(ifp, cmd, data);
2409 error = priv_check(td, PRIV_NET_SETLLADDR);
2412 error = if_setlladdr(ifp,
2413 ifr->ifr_addr.sa_data, ifr->ifr_addr.sa_len);
2414 EVENTHANDLER_INVOKE(iflladdr_event, ifp);
2419 struct ifgroupreq *ifgr = (struct ifgroupreq *)ifr;
2421 error = priv_check(td, PRIV_NET_ADDIFGROUP);
2424 if ((error = if_addgroup(ifp, ifgr->ifgr_group)))
2430 if ((error = if_getgroup((struct ifgroupreq *)ifr, ifp)))
2436 struct ifgroupreq *ifgr = (struct ifgroupreq *)ifr;
2438 error = priv_check(td, PRIV_NET_DELIFGROUP);
2441 if ((error = if_delgroup(ifp, ifgr->ifgr_group)))
2453 #ifdef COMPAT_FREEBSD32
2461 #define SIOCGIFCONF32 _IOWR('i', 36, struct ifconf32)
2468 ifioctl(struct socket *so, u_long cmd, caddr_t data, struct thread *td)
2475 CURVNET_SET(so->so_vnet);
2479 error = ifconf(cmd, data);
2483 #ifdef COMPAT_FREEBSD32
2486 struct ifconf32 *ifc32;
2489 ifc32 = (struct ifconf32 *)data;
2490 ifc.ifc_len = ifc32->ifc_len;
2491 ifc.ifc_buf = PTRIN(ifc32->ifc_buf);
2493 error = ifconf(SIOCGIFCONF, (void *)&ifc);
2496 ifc32->ifc_len = ifc.ifc_len;
2501 ifr = (struct ifreq *)data;
2506 error = priv_check(td, PRIV_NET_SETIFVNET);
2508 error = if_vmove_reclaim(td, ifr->ifr_name,
2515 error = priv_check(td, PRIV_NET_IFCREATE);
2517 error = if_clone_create(ifr->ifr_name,
2518 sizeof(ifr->ifr_name),
2519 cmd == SIOCIFCREATE2 ? ifr->ifr_data : NULL);
2523 error = priv_check(td, PRIV_NET_IFDESTROY);
2525 error = if_clone_destroy(ifr->ifr_name);
2529 case SIOCIFGCLONERS:
2530 error = if_clone_list((struct if_clonereq *)data);
2534 error = if_getgroupmembers((struct ifgroupreq *)data);
2539 ifp = ifunit_ref(ifr->ifr_name);
2545 error = ifhwioctl(cmd, ifp, data, td);
2546 if (error != ENOIOCTL) {
2552 oif_flags = ifp->if_flags;
2553 if (so->so_proto == NULL) {
2556 return (EOPNOTSUPP);
2560 * Pass the request on to the socket control method, and if the
2561 * latter returns EOPNOTSUPP, directly to the interface.
2563 * Make an exception for the legacy SIOCSIF* requests. Drivers
2564 * trust SIOCSIFADDR et al to come from an already privileged
2565 * layer, and do not perform any credentials checks or input
2569 error = ((*so->so_proto->pr_usrreqs->pru_control)(so, cmd,
2572 if (error == EOPNOTSUPP && ifp != NULL && ifp->if_ioctl != NULL &&
2573 cmd != SIOCSIFADDR && cmd != SIOCSIFBRDADDR &&
2574 cmd != SIOCSIFDSTADDR && cmd != SIOCSIFNETMASK)
2575 error = (*ifp->if_ioctl)(ifp, cmd, data);
2582 case SIOCSIFDSTADDR:
2584 case SIOCSIFBRDADDR:
2585 case SIOCSIFNETMASK:
2586 #if BYTE_ORDER != BIG_ENDIAN
2587 if (ifr->ifr_addr.sa_family == 0 &&
2588 ifr->ifr_addr.sa_len < 16) {
2589 ifr->ifr_addr.sa_family = ifr->ifr_addr.sa_len;
2590 ifr->ifr_addr.sa_len = 16;
2593 if (ifr->ifr_addr.sa_len == 0)
2594 ifr->ifr_addr.sa_len = 16;
2602 case OSIOCGIFDSTADDR:
2603 cmd = SIOCGIFDSTADDR;
2606 case OSIOCGIFBRDADDR:
2607 cmd = SIOCGIFBRDADDR;
2610 case OSIOCGIFNETMASK:
2611 cmd = SIOCGIFNETMASK;
2613 error = ((*so->so_proto->pr_usrreqs->pru_control)(so,
2617 if (error == EOPNOTSUPP && ifp != NULL &&
2618 ifp->if_ioctl != NULL &&
2619 cmd != SIOCSIFADDR && cmd != SIOCSIFBRDADDR &&
2620 cmd != SIOCSIFDSTADDR && cmd != SIOCSIFNETMASK)
2621 error = (*ifp->if_ioctl)(ifp, cmd, data);
2625 case OSIOCGIFDSTADDR:
2626 case OSIOCGIFBRDADDR:
2627 case OSIOCGIFNETMASK:
2628 *(u_short *)&ifr->ifr_addr = ifr->ifr_addr.sa_family;
2632 #endif /* COMPAT_43 */
2634 if ((oif_flags ^ ifp->if_flags) & IFF_UP) {
2636 if (ifp->if_flags & IFF_UP) {
2649 * The code common to handling reference counted flags,
2650 * e.g., in ifpromisc() and if_allmulti().
2651 * The "pflag" argument can specify a permanent mode flag to check,
2652 * such as IFF_PPROMISC for promiscuous mode; should be 0 if none.
2654 * Only to be used on stack-owned flags, not driver-owned flags.
2657 if_setflag(struct ifnet *ifp, int flag, int pflag, int *refcount, int onswitch)
2661 int oldflags, oldcount;
2663 /* Sanity checks to catch programming errors */
2664 KASSERT((flag & (IFF_DRV_OACTIVE|IFF_DRV_RUNNING)) == 0,
2665 ("%s: setting driver-owned flag %d", __func__, flag));
2668 KASSERT(*refcount >= 0,
2669 ("%s: increment negative refcount %d for flag %d",
2670 __func__, *refcount, flag));
2672 KASSERT(*refcount > 0,
2673 ("%s: decrement non-positive refcount %d for flag %d",
2674 __func__, *refcount, flag));
2676 /* In case this mode is permanent, just touch refcount */
2677 if (ifp->if_flags & pflag) {
2678 *refcount += onswitch ? 1 : -1;
2682 /* Save ifnet parameters for if_ioctl() may fail */
2683 oldcount = *refcount;
2684 oldflags = ifp->if_flags;
2687 * See if we aren't the only and touching refcount is enough.
2688 * Actually toggle interface flag if we are the first or last.
2693 ifp->if_flags |= flag;
2697 ifp->if_flags &= ~flag;
2700 /* Call down the driver since we've changed interface flags */
2701 if (ifp->if_ioctl == NULL) {
2705 ifr.ifr_flags = ifp->if_flags & 0xffff;
2706 ifr.ifr_flagshigh = ifp->if_flags >> 16;
2707 error = (*ifp->if_ioctl)(ifp, SIOCSIFFLAGS, (caddr_t)&ifr);
2710 /* Notify userland that interface flags have changed */
2715 /* Recover after driver error */
2716 *refcount = oldcount;
2717 ifp->if_flags = oldflags;
2722 * Set/clear promiscuous mode on interface ifp based on the truth value
2723 * of pswitch. The calls are reference counted so that only the first
2724 * "on" request actually has an effect, as does the final "off" request.
2725 * Results are undefined if the "off" and "on" requests are not matched.
2728 ifpromisc(struct ifnet *ifp, int pswitch)
2731 int oldflags = ifp->if_flags;
2733 error = if_setflag(ifp, IFF_PROMISC, IFF_PPROMISC,
2734 &ifp->if_pcount, pswitch);
2735 /* If promiscuous mode status has changed, log a message */
2736 if (error == 0 && ((ifp->if_flags ^ oldflags) & IFF_PROMISC))
2737 log(LOG_INFO, "%s: promiscuous mode %s\n",
2739 (ifp->if_flags & IFF_PROMISC) ? "enabled" : "disabled");
2744 * Return interface configuration
2745 * of system. List may be used
2746 * in later ioctl's (above) to get
2747 * other information.
2751 ifconf(u_long cmd, caddr_t data)
2753 struct ifconf *ifc = (struct ifconf *)data;
2758 int error, full = 0, valid_len, max_len;
2760 /* Limit initial buffer size to MAXPHYS to avoid DoS from userspace. */
2761 max_len = MAXPHYS - 1;
2763 /* Prevent hostile input from being able to crash the system */
2764 if (ifc->ifc_len <= 0)
2768 if (ifc->ifc_len <= max_len) {
2769 max_len = ifc->ifc_len;
2772 sb = sbuf_new(NULL, NULL, max_len + 1, SBUF_FIXEDLEN);
2777 TAILQ_FOREACH(ifp, &V_ifnet, if_link) {
2781 * Zero the ifr_name buffer to make sure we don't
2782 * disclose the contents of the stack.
2784 memset(ifr.ifr_name, 0, sizeof(ifr.ifr_name));
2786 if (strlcpy(ifr.ifr_name, ifp->if_xname, sizeof(ifr.ifr_name))
2787 >= sizeof(ifr.ifr_name)) {
2790 return (ENAMETOOLONG);
2795 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
2796 struct sockaddr *sa = ifa->ifa_addr;
2798 if (prison_if(curthread->td_ucred, sa) != 0)
2802 if (cmd == OSIOCGIFCONF) {
2803 struct osockaddr *osa =
2804 (struct osockaddr *)&ifr.ifr_addr;
2806 osa->sa_family = sa->sa_family;
2807 sbuf_bcat(sb, &ifr, sizeof(ifr));
2808 max_len += sizeof(ifr);
2811 if (sa->sa_len <= sizeof(*sa)) {
2813 sbuf_bcat(sb, &ifr, sizeof(ifr));
2814 max_len += sizeof(ifr);
2817 offsetof(struct ifreq, ifr_addr));
2818 max_len += offsetof(struct ifreq, ifr_addr);
2819 sbuf_bcat(sb, sa, sa->sa_len);
2820 max_len += sa->sa_len;
2823 if (sbuf_error(sb) == 0)
2824 valid_len = sbuf_len(sb);
2826 IF_ADDR_RUNLOCK(ifp);
2828 bzero((caddr_t)&ifr.ifr_addr, sizeof(ifr.ifr_addr));
2829 sbuf_bcat(sb, &ifr, sizeof(ifr));
2830 max_len += sizeof(ifr);
2832 if (sbuf_error(sb) == 0)
2833 valid_len = sbuf_len(sb);
2839 * If we didn't allocate enough space (uncommon), try again. If
2840 * we have already allocated as much space as we are allowed,
2841 * return what we've got.
2843 if (valid_len != max_len && !full) {
2848 ifc->ifc_len = valid_len;
2850 error = copyout(sbuf_data(sb), ifc->ifc_req, ifc->ifc_len);
2856 * Just like ifpromisc(), but for all-multicast-reception mode.
2859 if_allmulti(struct ifnet *ifp, int onswitch)
2862 return (if_setflag(ifp, IFF_ALLMULTI, 0, &ifp->if_amcount, onswitch));
2865 struct ifmultiaddr *
2866 if_findmulti(struct ifnet *ifp, struct sockaddr *sa)
2868 struct ifmultiaddr *ifma;
2870 IF_ADDR_LOCK_ASSERT(ifp);
2872 TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
2873 if (sa->sa_family == AF_LINK) {
2874 if (sa_dl_equal(ifma->ifma_addr, sa))
2877 if (sa_equal(ifma->ifma_addr, sa))
2886 * Allocate a new ifmultiaddr and initialize based on passed arguments. We
2887 * make copies of passed sockaddrs. The ifmultiaddr will not be added to
2888 * the ifnet multicast address list here, so the caller must do that and
2889 * other setup work (such as notifying the device driver). The reference
2890 * count is initialized to 1.
2892 static struct ifmultiaddr *
2893 if_allocmulti(struct ifnet *ifp, struct sockaddr *sa, struct sockaddr *llsa,
2896 struct ifmultiaddr *ifma;
2897 struct sockaddr *dupsa;
2899 ifma = malloc(sizeof *ifma, M_IFMADDR, mflags |
2904 dupsa = malloc(sa->sa_len, M_IFMADDR, mflags);
2905 if (dupsa == NULL) {
2906 free(ifma, M_IFMADDR);
2909 bcopy(sa, dupsa, sa->sa_len);
2910 ifma->ifma_addr = dupsa;
2912 ifma->ifma_ifp = ifp;
2913 ifma->ifma_refcount = 1;
2914 ifma->ifma_protospec = NULL;
2917 ifma->ifma_lladdr = NULL;
2921 dupsa = malloc(llsa->sa_len, M_IFMADDR, mflags);
2922 if (dupsa == NULL) {
2923 free(ifma->ifma_addr, M_IFMADDR);
2924 free(ifma, M_IFMADDR);
2927 bcopy(llsa, dupsa, llsa->sa_len);
2928 ifma->ifma_lladdr = dupsa;
2934 * if_freemulti: free ifmultiaddr structure and possibly attached related
2935 * addresses. The caller is responsible for implementing reference
2936 * counting, notifying the driver, handling routing messages, and releasing
2937 * any dependent link layer state.
2940 if_freemulti(struct ifmultiaddr *ifma)
2943 KASSERT(ifma->ifma_refcount == 0, ("if_freemulti: refcount %d",
2944 ifma->ifma_refcount));
2945 KASSERT(ifma->ifma_protospec == NULL,
2946 ("if_freemulti: protospec not NULL"));
2948 if (ifma->ifma_lladdr != NULL)
2949 free(ifma->ifma_lladdr, M_IFMADDR);
2950 free(ifma->ifma_addr, M_IFMADDR);
2951 free(ifma, M_IFMADDR);
2955 * Register an additional multicast address with a network interface.
2957 * - If the address is already present, bump the reference count on the
2958 * address and return.
2959 * - If the address is not link-layer, look up a link layer address.
2960 * - Allocate address structures for one or both addresses, and attach to the
2961 * multicast address list on the interface. If automatically adding a link
2962 * layer address, the protocol address will own a reference to the link
2963 * layer address, to be freed when it is freed.
2964 * - Notify the network device driver of an addition to the multicast address
2967 * 'sa' points to caller-owned memory with the desired multicast address.
2969 * 'retifma' will be used to return a pointer to the resulting multicast
2970 * address reference, if desired.
2973 if_addmulti(struct ifnet *ifp, struct sockaddr *sa,
2974 struct ifmultiaddr **retifma)
2976 struct ifmultiaddr *ifma, *ll_ifma;
2977 struct sockaddr *llsa;
2981 * If the address is already present, return a new reference to it;
2982 * otherwise, allocate storage and set up a new address.
2985 ifma = if_findmulti(ifp, sa);
2987 ifma->ifma_refcount++;
2988 if (retifma != NULL)
2990 IF_ADDR_WUNLOCK(ifp);
2995 * The address isn't already present; resolve the protocol address
2996 * into a link layer address, and then look that up, bump its
2997 * refcount or allocate an ifma for that also. If 'llsa' was
2998 * returned, we will need to free it later.
3002 if (ifp->if_resolvemulti != NULL) {
3003 error = ifp->if_resolvemulti(ifp, &llsa, sa);
3009 * Allocate the new address. Don't hook it up yet, as we may also
3010 * need to allocate a link layer multicast address.
3012 ifma = if_allocmulti(ifp, sa, llsa, M_NOWAIT);
3019 * If a link layer address is found, we'll need to see if it's
3020 * already present in the address list, or allocate is as well.
3021 * When this block finishes, the link layer address will be on the
3025 ll_ifma = if_findmulti(ifp, llsa);
3026 if (ll_ifma == NULL) {
3027 ll_ifma = if_allocmulti(ifp, llsa, NULL, M_NOWAIT);
3028 if (ll_ifma == NULL) {
3029 --ifma->ifma_refcount;
3034 TAILQ_INSERT_HEAD(&ifp->if_multiaddrs, ll_ifma,
3037 ll_ifma->ifma_refcount++;
3038 ifma->ifma_llifma = ll_ifma;
3042 * We now have a new multicast address, ifma, and possibly a new or
3043 * referenced link layer address. Add the primary address to the
3044 * ifnet address list.
3046 TAILQ_INSERT_HEAD(&ifp->if_multiaddrs, ifma, ifma_link);
3048 if (retifma != NULL)
3052 * Must generate the message while holding the lock so that 'ifma'
3053 * pointer is still valid.
3055 rt_newmaddrmsg(RTM_NEWMADDR, ifma);
3056 IF_ADDR_WUNLOCK(ifp);
3059 * We are certain we have added something, so call down to the
3060 * interface to let them know about it.
3062 if (ifp->if_ioctl != NULL) {
3063 (void) (*ifp->if_ioctl)(ifp, SIOCADDMULTI, 0);
3067 free(llsa, M_IFMADDR);
3073 free(llsa, M_IFMADDR);
3076 IF_ADDR_WUNLOCK(ifp);
3081 * Delete a multicast group membership by network-layer group address.
3083 * Returns ENOENT if the entry could not be found. If ifp no longer
3084 * exists, results are undefined. This entry point should only be used
3085 * from subsystems which do appropriate locking to hold ifp for the
3086 * duration of the call.
3087 * Network-layer protocol domains must use if_delmulti_ifma().
3090 if_delmulti(struct ifnet *ifp, struct sockaddr *sa)
3092 struct ifmultiaddr *ifma;
3097 IFNET_RLOCK_NOSLEEP();
3098 TAILQ_FOREACH(oifp, &V_ifnet, if_link)
3103 IFNET_RUNLOCK_NOSLEEP();
3105 KASSERT(ifp != NULL, ("%s: ifnet went away", __func__));
3112 ifma = if_findmulti(ifp, sa);
3114 lastref = if_delmulti_locked(ifp, ifma, 0);
3115 IF_ADDR_WUNLOCK(ifp);
3120 if (lastref && ifp->if_ioctl != NULL) {
3121 (void)(*ifp->if_ioctl)(ifp, SIOCDELMULTI, 0);
3128 * Delete all multicast group membership for an interface.
3129 * Should be used to quickly flush all multicast filters.
3132 if_delallmulti(struct ifnet *ifp)
3134 struct ifmultiaddr *ifma;
3135 struct ifmultiaddr *next;
3138 TAILQ_FOREACH_SAFE(ifma, &ifp->if_multiaddrs, ifma_link, next)
3139 if_delmulti_locked(ifp, ifma, 0);
3140 IF_ADDR_WUNLOCK(ifp);
3144 * Delete a multicast group membership by group membership pointer.
3145 * Network-layer protocol domains must use this routine.
3147 * It is safe to call this routine if the ifp disappeared.
3150 if_delmulti_ifma(struct ifmultiaddr *ifma)
3155 ifp = ifma->ifma_ifp;
3158 printf("%s: ifma_ifp seems to be detached\n", __func__);
3162 IFNET_RLOCK_NOSLEEP();
3163 TAILQ_FOREACH(oifp, &V_ifnet, if_link)
3167 printf("%s: ifnet %p disappeared\n", __func__, ifp);
3170 IFNET_RUNLOCK_NOSLEEP();
3174 * If and only if the ifnet instance exists: Acquire the address lock.
3179 lastref = if_delmulti_locked(ifp, ifma, 0);
3183 * If and only if the ifnet instance exists:
3184 * Release the address lock.
3185 * If the group was left: update the hardware hash filter.
3187 IF_ADDR_WUNLOCK(ifp);
3188 if (lastref && ifp->if_ioctl != NULL) {
3189 (void)(*ifp->if_ioctl)(ifp, SIOCDELMULTI, 0);
3195 * Perform deletion of network-layer and/or link-layer multicast address.
3197 * Return 0 if the reference count was decremented.
3198 * Return 1 if the final reference was released, indicating that the
3199 * hardware hash filter should be reprogrammed.
3202 if_delmulti_locked(struct ifnet *ifp, struct ifmultiaddr *ifma, int detaching)
3204 struct ifmultiaddr *ll_ifma;
3206 if (ifp != NULL && ifma->ifma_ifp != NULL) {
3207 KASSERT(ifma->ifma_ifp == ifp,
3208 ("%s: inconsistent ifp %p", __func__, ifp));
3209 IF_ADDR_WLOCK_ASSERT(ifp);
3212 ifp = ifma->ifma_ifp;
3215 * If the ifnet is detaching, null out references to ifnet,
3216 * so that upper protocol layers will notice, and not attempt
3217 * to obtain locks for an ifnet which no longer exists. The
3218 * routing socket announcement must happen before the ifnet
3219 * instance is detached from the system.
3223 printf("%s: detaching ifnet instance %p\n", __func__, ifp);
3226 * ifp may already be nulled out if we are being reentered
3227 * to delete the ll_ifma.
3230 rt_newmaddrmsg(RTM_DELMADDR, ifma);
3231 ifma->ifma_ifp = NULL;
3235 if (--ifma->ifma_refcount > 0)
3239 * If this ifma is a network-layer ifma, a link-layer ifma may
3240 * have been associated with it. Release it first if so.
3242 ll_ifma = ifma->ifma_llifma;
3243 if (ll_ifma != NULL) {
3244 KASSERT(ifma->ifma_lladdr != NULL,
3245 ("%s: llifma w/o lladdr", __func__));
3247 ll_ifma->ifma_ifp = NULL; /* XXX */
3248 if (--ll_ifma->ifma_refcount == 0) {
3250 TAILQ_REMOVE(&ifp->if_multiaddrs, ll_ifma,
3253 if_freemulti(ll_ifma);
3258 TAILQ_REMOVE(&ifp->if_multiaddrs, ifma, ifma_link);
3263 * The last reference to this instance of struct ifmultiaddr
3264 * was released; the hardware should be notified of this change.
3270 * Set the link layer address on an interface.
3272 * At this time we only support certain types of interfaces,
3273 * and we don't allow the length of the address to change.
3276 if_setlladdr(struct ifnet *ifp, const u_char *lladdr, int len)
3278 struct sockaddr_dl *sdl;
3285 IF_ADDR_RUNLOCK(ifp);
3289 IF_ADDR_RUNLOCK(ifp);
3290 sdl = (struct sockaddr_dl *)ifa->ifa_addr;
3295 if (len != sdl->sdl_alen) { /* don't allow length to change */
3299 switch (ifp->if_type) {
3307 case IFT_IEEE8023ADLAG:
3309 bcopy(lladdr, LLADDR(sdl), len);
3318 * If the interface is already up, we need
3319 * to re-init it in order to reprogram its
3322 if ((ifp->if_flags & IFF_UP) != 0) {
3323 if (ifp->if_ioctl) {
3324 ifp->if_flags &= ~IFF_UP;
3325 ifr.ifr_flags = ifp->if_flags & 0xffff;
3326 ifr.ifr_flagshigh = ifp->if_flags >> 16;
3327 (*ifp->if_ioctl)(ifp, SIOCSIFFLAGS, (caddr_t)&ifr);
3328 ifp->if_flags |= IFF_UP;
3329 ifr.ifr_flags = ifp->if_flags & 0xffff;
3330 ifr.ifr_flagshigh = ifp->if_flags >> 16;
3331 (*ifp->if_ioctl)(ifp, SIOCSIFFLAGS, (caddr_t)&ifr);
3335 * Also send gratuitous ARPs to notify other nodes about
3336 * the address change.
3338 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
3339 if (ifa->ifa_addr->sa_family == AF_INET)
3340 arp_ifinit(ifp, ifa);
3348 * The name argument must be a pointer to storage which will last as
3349 * long as the interface does. For physical devices, the result of
3350 * device_get_name(dev) is a good choice and for pseudo-devices a
3351 * static string works well.
3354 if_initname(struct ifnet *ifp, const char *name, int unit)
3356 ifp->if_dname = name;
3357 ifp->if_dunit = unit;
3358 if (unit != IF_DUNIT_NONE)
3359 snprintf(ifp->if_xname, IFNAMSIZ, "%s%d", name, unit);
3361 strlcpy(ifp->if_xname, name, IFNAMSIZ);
3365 if_printf(struct ifnet *ifp, const char * fmt, ...)
3370 retval = printf("%s: ", ifp->if_xname);
3372 retval += vprintf(fmt, ap);
3378 if_start(struct ifnet *ifp)
3381 (*(ifp)->if_start)(ifp);
3385 * Backwards compatibility interface for drivers
3386 * that have not implemented it
3389 if_transmit(struct ifnet *ifp, struct mbuf *m)
3393 IFQ_HANDOFF(ifp, m, error);
3398 if_handoff(struct ifqueue *ifq, struct mbuf *m, struct ifnet *ifp, int adjust)
3403 if (_IF_QFULL(ifq)) {
3410 ifp->if_obytes += m->m_pkthdr.len + adjust;
3411 if (m->m_flags & (M_BCAST|M_MCAST))
3413 active = ifp->if_drv_flags & IFF_DRV_OACTIVE;
3415 _IF_ENQUEUE(ifq, m);
3417 if (ifp != NULL && !active)
3418 (*(ifp)->if_start)(ifp);
3423 if_register_com_alloc(u_char type,
3424 if_com_alloc_t *a, if_com_free_t *f)
3427 KASSERT(if_com_alloc[type] == NULL,
3428 ("if_register_com_alloc: %d already registered", type));
3429 KASSERT(if_com_free[type] == NULL,
3430 ("if_register_com_alloc: %d free already registered", type));
3432 if_com_alloc[type] = a;
3433 if_com_free[type] = f;
3437 if_deregister_com_alloc(u_char type)
3440 KASSERT(if_com_alloc[type] != NULL,
3441 ("if_deregister_com_alloc: %d not registered", type));
3442 KASSERT(if_com_free[type] != NULL,
3443 ("if_deregister_com_alloc: %d free not registered", type));
3444 if_com_alloc[type] = NULL;
3445 if_com_free[type] = NULL;