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 <net/ethernet.h>
78 #include <netinet/in.h>
79 #include <netinet/in_var.h>
80 #include <netinet/ip.h>
81 #include <netinet/ip_carp.h>
83 #include <netinet/if_ether.h>
86 #include <netinet6/in6_var.h>
87 #include <netinet6/in6_ifattach.h>
89 #endif /* INET || INET6 */
91 #include <security/mac/mac_framework.h>
93 #ifdef COMPAT_FREEBSD32
94 #include <sys/mount.h>
95 #include <compat/freebsd32/freebsd32.h>
98 struct ifindex_entry {
99 struct ifnet *ife_ifnet;
102 SYSCTL_NODE(_net, PF_LINK, link, CTLFLAG_RW, 0, "Link layers");
103 SYSCTL_NODE(_net_link, 0, generic, CTLFLAG_RW, 0, "Generic link-management");
105 TUNABLE_INT("net.link.ifqmaxlen", &ifqmaxlen);
106 SYSCTL_INT(_net_link, OID_AUTO, ifqmaxlen, CTLFLAG_RDTUN,
107 &ifqmaxlen, 0, "max send queue size");
109 /* Log link state change events */
110 static int log_link_state_change = 1;
112 SYSCTL_INT(_net_link, OID_AUTO, log_link_state_change, CTLFLAG_RW,
113 &log_link_state_change, 0,
114 "log interface link state change events");
116 /* Interface description */
117 static unsigned int ifdescr_maxlen = 1024;
118 SYSCTL_UINT(_net, OID_AUTO, ifdescr_maxlen, CTLFLAG_RW,
120 "administrative maximum length for interface description");
122 static MALLOC_DEFINE(M_IFDESCR, "ifdescr", "ifnet descriptions");
124 /* global sx for non-critical path ifdescr */
125 static struct sx ifdescr_sx;
126 SX_SYSINIT(ifdescr_sx, &ifdescr_sx, "ifnet descr");
128 void (*bridge_linkstate_p)(struct ifnet *ifp);
129 void (*ng_ether_link_state_p)(struct ifnet *ifp, int state);
130 void (*lagg_linkstate_p)(struct ifnet *ifp, int state);
131 /* These are external hooks for CARP. */
132 void (*carp_linkstate_p)(struct ifnet *ifp);
133 #if defined(INET) || defined(INET6)
134 struct ifnet *(*carp_forus_p)(struct ifnet *ifp, u_char *dhost);
135 int (*carp_output_p)(struct ifnet *ifp, struct mbuf *m,
136 struct sockaddr *sa, struct rtentry *rt);
139 int (*carp_iamatch_p)(struct ifnet *, struct in_ifaddr *, struct in_addr *,
143 struct ifaddr *(*carp_iamatch6_p)(struct ifnet *ifp, struct in6_addr *taddr6);
144 caddr_t (*carp_macmatch6_p)(struct ifnet *ifp, struct mbuf *m,
145 const struct in6_addr *taddr);
148 struct mbuf *(*tbr_dequeue_ptr)(struct ifaltq *, int) = NULL;
151 * XXX: Style; these should be sorted alphabetically, and unprototyped
152 * static functions should be prototyped. Currently they are sorted by
155 static void if_attachdomain(void *);
156 static void if_attachdomain1(struct ifnet *);
157 static int ifconf(u_long, caddr_t);
158 static void if_freemulti(struct ifmultiaddr *);
159 static void if_init(void *);
160 static void if_grow(void);
161 static void if_route(struct ifnet *, int flag, int fam);
162 static int if_setflag(struct ifnet *, int, int, int *, int);
163 static int if_transmit(struct ifnet *ifp, struct mbuf *m);
164 static void if_unroute(struct ifnet *, int flag, int fam);
165 static void link_rtrequest(int, struct rtentry *, struct rt_addrinfo *);
166 static int if_rtdel(struct radix_node *, void *);
167 static int ifhwioctl(u_long, struct ifnet *, caddr_t, struct thread *);
168 static int if_delmulti_locked(struct ifnet *, struct ifmultiaddr *, int);
169 static void do_link_state_change(void *, int);
170 static int if_getgroup(struct ifgroupreq *, struct ifnet *);
171 static int if_getgroupmembers(struct ifgroupreq *);
172 static void if_delgroups(struct ifnet *);
173 static void if_attach_internal(struct ifnet *, int);
174 static void if_detach_internal(struct ifnet *, int);
178 * XXX: declare here to avoid to include many inet6 related files..
179 * should be more generalized?
181 extern void nd6_setmtu(struct ifnet *);
184 VNET_DEFINE(int, if_index);
185 int ifqmaxlen = IFQ_MAXLEN;
186 VNET_DEFINE(struct ifnethead, ifnet); /* depend on static init XXX */
187 VNET_DEFINE(struct ifgrouphead, ifg_head);
189 static VNET_DEFINE(int, if_indexlim) = 8;
191 /* Table of ifnet by index. */
192 VNET_DEFINE(struct ifindex_entry *, ifindex_table);
194 #define V_if_indexlim VNET(if_indexlim)
195 #define V_ifindex_table VNET(ifindex_table)
198 * The global network interface list (V_ifnet) and related state (such as
199 * if_index, if_indexlim, and ifindex_table) are protected by an sxlock and
200 * an rwlock. Either may be acquired shared to stablize the list, but both
201 * must be acquired writable to modify the list. This model allows us to
202 * both stablize the interface list during interrupt thread processing, but
203 * also to stablize it over long-running ioctls, without introducing priority
204 * inversions and deadlocks.
206 struct rwlock ifnet_rwlock;
207 struct sx ifnet_sxlock;
210 * The allocation of network interfaces is a rather non-atomic affair; we
211 * need to select an index before we are ready to expose the interface for
212 * use, so will use this pointer value to indicate reservation.
214 #define IFNET_HOLD (void *)(uintptr_t)(-1)
216 static if_com_alloc_t *if_com_alloc[256];
217 static if_com_free_t *if_com_free[256];
219 static MALLOC_DEFINE(M_IFNET, "ifnet", "interface internals");
220 MALLOC_DEFINE(M_IFADDR, "ifaddr", "interface address");
221 MALLOC_DEFINE(M_IFMADDR, "ether_multi", "link-level multicast address");
224 ifnet_byindex_locked(u_short idx)
227 if (idx > V_if_index)
229 if (V_ifindex_table[idx].ife_ifnet == IFNET_HOLD)
231 return (V_ifindex_table[idx].ife_ifnet);
235 ifnet_byindex(u_short idx)
239 IFNET_RLOCK_NOSLEEP();
240 ifp = ifnet_byindex_locked(idx);
241 IFNET_RUNLOCK_NOSLEEP();
246 ifnet_byindex_ref(u_short idx)
250 IFNET_RLOCK_NOSLEEP();
251 ifp = ifnet_byindex_locked(idx);
252 if (ifp == NULL || (ifp->if_flags & IFF_DYING)) {
253 IFNET_RUNLOCK_NOSLEEP();
257 IFNET_RUNLOCK_NOSLEEP();
262 * Allocate an ifindex array entry; return 0 on success or an error on
266 ifindex_alloc_locked(u_short *idxp)
270 IFNET_WLOCK_ASSERT();
274 * Try to find an empty slot below V_if_index. If we fail, take the
277 for (idx = 1; idx <= V_if_index; idx++) {
278 if (V_ifindex_table[idx].ife_ifnet == NULL)
282 /* Catch if_index overflow. */
285 if (idx >= V_if_indexlim) {
289 if (idx > V_if_index)
296 ifindex_free_locked(u_short idx)
299 IFNET_WLOCK_ASSERT();
301 V_ifindex_table[idx].ife_ifnet = NULL;
302 while (V_if_index > 0 &&
303 V_ifindex_table[V_if_index].ife_ifnet == NULL)
308 ifindex_free(u_short idx)
312 ifindex_free_locked(idx);
317 ifnet_setbyindex_locked(u_short idx, struct ifnet *ifp)
320 IFNET_WLOCK_ASSERT();
322 V_ifindex_table[idx].ife_ifnet = ifp;
326 ifnet_setbyindex(u_short idx, struct ifnet *ifp)
330 ifnet_setbyindex_locked(idx, ifp);
335 ifaddr_byindex(u_short idx)
339 IFNET_RLOCK_NOSLEEP();
340 ifa = ifnet_byindex_locked(idx)->if_addr;
343 IFNET_RUNLOCK_NOSLEEP();
348 * Network interface utility routines.
350 * Routines with ifa_ifwith* names take sockaddr *'s as
355 vnet_if_init(const void *unused __unused)
358 TAILQ_INIT(&V_ifnet);
359 TAILQ_INIT(&V_ifg_head);
361 if_grow(); /* create initial table */
363 vnet_if_clone_init();
365 VNET_SYSINIT(vnet_if_init, SI_SUB_INIT_IF, SI_ORDER_SECOND, vnet_if_init,
370 if_init(void *dummy __unused)
376 SYSINIT(interfaces, SI_SUB_INIT_IF, SI_ORDER_FIRST, if_init, NULL);
381 vnet_if_uninit(const void *unused __unused)
384 VNET_ASSERT(TAILQ_EMPTY(&V_ifnet), ("%s:%d tailq &V_ifnet=%p "
385 "not empty", __func__, __LINE__, &V_ifnet));
386 VNET_ASSERT(TAILQ_EMPTY(&V_ifg_head), ("%s:%d tailq &V_ifg_head=%p "
387 "not empty", __func__, __LINE__, &V_ifg_head));
389 free((caddr_t)V_ifindex_table, M_IFNET);
391 VNET_SYSUNINIT(vnet_if_uninit, SI_SUB_INIT_IF, SI_ORDER_FIRST,
392 vnet_if_uninit, NULL);
400 struct ifindex_entry *e;
402 IFNET_WLOCK_ASSERT();
403 oldlim = V_if_indexlim;
405 n = (oldlim << 1) * sizeof(*e);
406 e = malloc(n, M_IFNET, M_WAITOK | M_ZERO);
408 if (V_if_indexlim != oldlim) {
412 if (V_ifindex_table != NULL) {
413 memcpy((caddr_t)e, (caddr_t)V_ifindex_table, n/2);
414 free((caddr_t)V_ifindex_table, M_IFNET);
421 * Allocate a struct ifnet and an index for an interface. A layer 2
422 * common structure will also be allocated if an allocation routine is
423 * registered for the passed type.
426 if_alloc(u_char type)
431 ifp = malloc(sizeof(struct ifnet), M_IFNET, M_WAITOK|M_ZERO);
433 if (ifindex_alloc_locked(&idx) != 0) {
438 ifnet_setbyindex_locked(idx, IFNET_HOLD);
442 ifp->if_alloctype = type;
443 if (if_com_alloc[type] != NULL) {
444 ifp->if_l2com = if_com_alloc[type](type, ifp);
445 if (ifp->if_l2com == NULL) {
452 IF_ADDR_LOCK_INIT(ifp);
453 TASK_INIT(&ifp->if_linktask, 0, do_link_state_change, ifp);
454 ifp->if_afdata_initialized = 0;
455 IF_AFDATA_LOCK_INIT(ifp);
456 TAILQ_INIT(&ifp->if_addrhead);
457 TAILQ_INIT(&ifp->if_prefixhead);
458 TAILQ_INIT(&ifp->if_multiaddrs);
459 TAILQ_INIT(&ifp->if_groups);
463 ifq_init(&ifp->if_snd, ifp);
465 refcount_init(&ifp->if_refcount, 1); /* Index reference. */
466 ifnet_setbyindex(ifp->if_index, ifp);
471 * Do the actual work of freeing a struct ifnet, and layer 2 common
472 * structure. This call is made when the last reference to an
473 * interface is released.
476 if_free_internal(struct ifnet *ifp)
479 KASSERT((ifp->if_flags & IFF_DYING),
480 ("if_free_internal: interface not dying"));
482 if (if_com_free[ifp->if_alloctype] != NULL)
483 if_com_free[ifp->if_alloctype](ifp->if_l2com,
487 mac_ifnet_destroy(ifp);
489 if (ifp->if_description != NULL)
490 free(ifp->if_description, M_IFDESCR);
491 IF_AFDATA_DESTROY(ifp);
492 IF_ADDR_LOCK_DESTROY(ifp);
493 ifq_delete(&ifp->if_snd);
498 * This version should only be called by intefaces that switch their type
499 * after calling if_alloc(). if_free_type() will go away again now that we
500 * have if_alloctype to cache the original allocation type. For now, assert
501 * that they match, since we require that in practice.
504 if_free_type(struct ifnet *ifp, u_char type)
507 KASSERT(ifp->if_alloctype == type,
508 ("if_free_type: type (%d) != alloctype (%d)", type,
511 ifp->if_flags |= IFF_DYING; /* XXX: Locking */
513 CURVNET_SET_QUIET(ifp->if_vnet);
515 KASSERT(ifp == ifnet_byindex_locked(ifp->if_index),
516 ("%s: freeing unallocated ifnet", ifp->if_xname));
518 ifindex_free_locked(ifp->if_index);
521 if (refcount_release(&ifp->if_refcount))
522 if_free_internal(ifp);
527 * This is the normal version of if_free(), used by device drivers to free a
528 * detached network interface. The contents of if_free_type() will move into
529 * here when if_free_type() goes away.
532 if_free(struct ifnet *ifp)
535 if_free_type(ifp, ifp->if_alloctype);
539 * Interfaces to keep an ifnet type-stable despite the possibility of the
540 * driver calling if_free(). If there are additional references, we defer
541 * freeing the underlying data structure.
544 if_ref(struct ifnet *ifp)
547 /* We don't assert the ifnet list lock here, but arguably should. */
548 refcount_acquire(&ifp->if_refcount);
552 if_rele(struct ifnet *ifp)
555 if (!refcount_release(&ifp->if_refcount))
557 if_free_internal(ifp);
561 ifq_init(struct ifaltq *ifq, struct ifnet *ifp)
564 mtx_init(&ifq->ifq_mtx, ifp->if_xname, "if send queue", MTX_DEF);
566 if (ifq->ifq_maxlen == 0)
567 ifq->ifq_maxlen = ifqmaxlen;
570 ifq->altq_disc = NULL;
571 ifq->altq_flags &= ALTQF_CANTCHANGE;
572 ifq->altq_tbr = NULL;
577 ifq_delete(struct ifaltq *ifq)
579 mtx_destroy(&ifq->ifq_mtx);
583 * Perform generic interface initalization tasks and attach the interface
584 * to the list of "active" interfaces. If vmove flag is set on entry
585 * to if_attach_internal(), perform only a limited subset of initialization
586 * tasks, given that we are moving from one vnet to another an ifnet which
587 * has already been fully initialized.
590 * - The decision to return void and thus require this function to
591 * succeed is questionable.
592 * - We should probably do more sanity checking. For instance we don't
593 * do anything to insure if_xname is unique or non-empty.
596 if_attach(struct ifnet *ifp)
599 if_attach_internal(ifp, 0);
603 if_attach_internal(struct ifnet *ifp, int vmove)
605 unsigned socksize, ifasize;
606 int namelen, masklen;
607 struct sockaddr_dl *sdl;
610 if (ifp->if_index == 0 || ifp != ifnet_byindex(ifp->if_index))
611 panic ("%s: BUG: if_attach called without if_alloc'd input()\n",
615 ifp->if_vnet = curvnet;
616 if (ifp->if_home_vnet == NULL)
617 ifp->if_home_vnet = curvnet;
620 if_addgroup(ifp, IFG_ALL);
622 getmicrotime(&ifp->if_lastchange);
623 ifp->if_data.ifi_epoch = time_uptime;
624 ifp->if_data.ifi_datalen = sizeof(struct if_data);
626 KASSERT((ifp->if_transmit == NULL && ifp->if_qflush == NULL) ||
627 (ifp->if_transmit != NULL && ifp->if_qflush != NULL),
628 ("transmit and qflush must both either be set or both be NULL"));
629 if (ifp->if_transmit == NULL) {
630 ifp->if_transmit = if_transmit;
631 ifp->if_qflush = if_qflush;
636 mac_ifnet_create(ifp);
640 * Create a Link Level name for this device.
642 namelen = strlen(ifp->if_xname);
644 * Always save enough space for any possiable name so we
645 * can do a rename in place later.
647 masklen = offsetof(struct sockaddr_dl, sdl_data[0]) + IFNAMSIZ;
648 socksize = masklen + ifp->if_addrlen;
649 if (socksize < sizeof(*sdl))
650 socksize = sizeof(*sdl);
651 socksize = roundup2(socksize, sizeof(long));
652 ifasize = sizeof(*ifa) + 2 * socksize;
653 ifa = malloc(ifasize, M_IFADDR, M_WAITOK | M_ZERO);
655 sdl = (struct sockaddr_dl *)(ifa + 1);
656 sdl->sdl_len = socksize;
657 sdl->sdl_family = AF_LINK;
658 bcopy(ifp->if_xname, sdl->sdl_data, namelen);
659 sdl->sdl_nlen = namelen;
660 sdl->sdl_index = ifp->if_index;
661 sdl->sdl_type = ifp->if_type;
664 ifa->ifa_rtrequest = link_rtrequest;
665 ifa->ifa_addr = (struct sockaddr *)sdl;
666 sdl = (struct sockaddr_dl *)(socksize + (caddr_t)sdl);
667 ifa->ifa_netmask = (struct sockaddr *)sdl;
668 sdl->sdl_len = masklen;
670 sdl->sdl_data[--namelen] = 0xff;
671 TAILQ_INSERT_HEAD(&ifp->if_addrhead, ifa, ifa_link);
672 /* Reliably crash if used uninitialized. */
673 ifp->if_broadcastaddr = NULL;
675 #if defined(INET) || defined(INET6)
676 /* Initialize to max value. */
677 if (ifp->if_hw_tsomax == 0)
678 ifp->if_hw_tsomax = min(IP_MAXPACKET, 32 * MCLBYTES -
679 (ETHER_HDR_LEN + ETHER_VLAN_ENCAP_LEN));
680 KASSERT(ifp->if_hw_tsomax <= IP_MAXPACKET &&
681 ifp->if_hw_tsomax >= IP_MAXPACKET / 8,
682 ("%s: tsomax outside of range", __func__));
688 * Update the interface index in the link layer address
691 for (ifa = ifp->if_addr; ifa != NULL;
692 ifa = TAILQ_NEXT(ifa, ifa_link)) {
693 if (ifa->ifa_addr->sa_family == AF_LINK) {
694 sdl = (struct sockaddr_dl *)ifa->ifa_addr;
695 sdl->sdl_index = ifp->if_index;
702 TAILQ_INSERT_TAIL(&V_ifnet, ifp, if_link);
704 curvnet->vnet_ifcnt++;
708 if (domain_init_status >= 2)
709 if_attachdomain1(ifp);
711 EVENTHANDLER_INVOKE(ifnet_arrival_event, ifp);
712 if (IS_DEFAULT_VNET(curvnet))
713 devctl_notify("IFNET", ifp->if_xname, "ATTACH", NULL);
715 /* Announce the interface. */
716 rt_ifannouncemsg(ifp, IFAN_ARRIVAL);
720 if_attachdomain(void *dummy)
726 TAILQ_FOREACH(ifp, &V_ifnet, if_link)
727 if_attachdomain1(ifp);
730 SYSINIT(domainifattach, SI_SUB_PROTO_IFATTACHDOMAIN, SI_ORDER_SECOND,
731 if_attachdomain, NULL);
734 if_attachdomain1(struct ifnet *ifp)
742 * Since dp->dom_ifattach calls malloc() with M_WAITOK, we
743 * cannot lock ifp->if_afdata initialization, entirely.
745 if (IF_AFDATA_TRYLOCK(ifp) == 0) {
749 if (ifp->if_afdata_initialized >= domain_init_status) {
750 IF_AFDATA_UNLOCK(ifp);
752 printf("if_attachdomain called more than once on %s\n",
756 ifp->if_afdata_initialized = domain_init_status;
757 IF_AFDATA_UNLOCK(ifp);
759 /* address family dependent data region */
760 bzero(ifp->if_afdata, sizeof(ifp->if_afdata));
761 for (dp = domains; dp; dp = dp->dom_next) {
762 if (dp->dom_ifattach)
763 ifp->if_afdata[dp->dom_family] =
764 (*dp->dom_ifattach)(ifp);
771 * Remove any unicast or broadcast network addresses from an interface.
774 if_purgeaddrs(struct ifnet *ifp)
776 struct ifaddr *ifa, *next;
778 TAILQ_FOREACH_SAFE(ifa, &ifp->if_addrhead, ifa_link, next) {
779 if (ifa->ifa_addr->sa_family == AF_LINK)
782 /* XXX: Ugly!! ad hoc just for INET */
783 if (ifa->ifa_addr->sa_family == AF_INET) {
784 struct ifaliasreq ifr;
786 bzero(&ifr, sizeof(ifr));
787 ifr.ifra_addr = *ifa->ifa_addr;
788 if (ifa->ifa_dstaddr)
789 ifr.ifra_broadaddr = *ifa->ifa_dstaddr;
790 if (in_control(NULL, SIOCDIFADDR, (caddr_t)&ifr, ifp,
796 if (ifa->ifa_addr->sa_family == AF_INET6) {
798 /* ifp_addrhead is already updated */
802 TAILQ_REMOVE(&ifp->if_addrhead, ifa, ifa_link);
808 * Remove any multicast network addresses from an interface when an ifnet
812 if_purgemaddrs(struct ifnet *ifp)
814 struct ifmultiaddr *ifma;
815 struct ifmultiaddr *next;
818 TAILQ_FOREACH_SAFE(ifma, &ifp->if_multiaddrs, ifma_link, next)
819 if_delmulti_locked(ifp, ifma, 1);
820 IF_ADDR_WUNLOCK(ifp);
824 * Detach an interface, removing it from the list of "active" interfaces.
825 * If vmove flag is set on entry to if_detach_internal(), perform only a
826 * limited subset of cleanup tasks, given that we are moving an ifnet from
827 * one vnet to another, where it must be fully operational.
829 * XXXRW: There are some significant questions about event ordering, and
830 * how to prevent things from starting to use the interface during detach.
833 if_detach(struct ifnet *ifp)
836 CURVNET_SET_QUIET(ifp->if_vnet);
837 if_detach_internal(ifp, 0);
842 if_detach_internal(struct ifnet *ifp, int vmove)
845 struct radix_node_head *rnh;
852 TAILQ_FOREACH(iter, &V_ifnet, if_link)
854 TAILQ_REMOVE(&V_ifnet, ifp, if_link);
860 curvnet->vnet_ifcnt--;
865 panic("%s: ifp=%p not on the ifnet tailq %p",
866 __func__, ifp, &V_ifnet);
868 return; /* XXX this should panic as well? */
872 * Remove/wait for pending events.
874 taskqueue_drain(taskqueue_swi, &ifp->if_linktask);
877 * Remove routes and flush queues.
881 if (ALTQ_IS_ENABLED(&ifp->if_snd))
882 altq_disable(&ifp->if_snd);
883 if (ALTQ_IS_ATTACHED(&ifp->if_snd))
884 altq_detach(&ifp->if_snd);
895 * Remove all IPv6 kernel structs related to ifp. This should be done
896 * before removing routing entries below, since IPv6 interface direct
897 * routes are expected to be removed by the IPv6-specific kernel API.
898 * Otherwise, the kernel will detect some inconsistency and bark it.
906 * Prevent further calls into the device driver via ifnet.
911 * Remove link ifaddr pointer and maybe decrement if_index.
912 * Clean up all addresses.
916 /* We can now free link ifaddr. */
917 if (!TAILQ_EMPTY(&ifp->if_addrhead)) {
918 ifa = TAILQ_FIRST(&ifp->if_addrhead);
919 TAILQ_REMOVE(&ifp->if_addrhead, ifa, ifa_link);
925 * Delete all remaining routes using this interface
926 * Unfortuneatly the only way to do this is to slog through
927 * the entire routing table looking for routes which point
928 * to this interface...oh well...
930 for (i = 1; i <= AF_MAX; i++) {
931 for (j = 0; j < rt_numfibs; j++) {
932 rnh = rt_tables_get_rnh(j, i);
935 RADIX_NODE_HEAD_LOCK(rnh);
936 (void) rnh->rnh_walktree(rnh, if_rtdel, ifp);
937 RADIX_NODE_HEAD_UNLOCK(rnh);
941 /* Announce that the interface is gone. */
942 rt_ifannouncemsg(ifp, IFAN_DEPARTURE);
943 EVENTHANDLER_INVOKE(ifnet_departure_event, ifp);
944 if (IS_DEFAULT_VNET(curvnet))
945 devctl_notify("IFNET", ifp->if_xname, "DETACH", NULL);
949 * We cannot hold the lock over dom_ifdetach calls as they might
950 * sleep, for example trying to drain a callout, thus open up the
951 * theoretical race with re-attaching.
954 i = ifp->if_afdata_initialized;
955 ifp->if_afdata_initialized = 0;
956 IF_AFDATA_UNLOCK(ifp);
957 for (dp = domains; i > 0 && dp; dp = dp->dom_next) {
958 if (dp->dom_ifdetach && ifp->if_afdata[dp->dom_family])
959 (*dp->dom_ifdetach)(ifp,
960 ifp->if_afdata[dp->dom_family]);
966 * if_vmove() performs a limited version of if_detach() in current
967 * vnet and if_attach()es the ifnet to the vnet specified as 2nd arg.
968 * An attempt is made to shrink if_index in current vnet, find an
969 * unused if_index in target vnet and calls if_grow() if necessary,
970 * and finally find an unused if_xname for the target vnet.
973 if_vmove(struct ifnet *ifp, struct vnet *new_vnet)
978 * Detach from current vnet, but preserve LLADDR info, do not
979 * mark as dead etc. so that the ifnet can be reattached later.
981 if_detach_internal(ifp, 1);
984 * Unlink the ifnet from ifindex_table[] in current vnet, and shrink
985 * the if_index for that vnet if possible.
987 * NOTE: IFNET_WLOCK/IFNET_WUNLOCK() are assumed to be unvirtualized,
988 * or we'd lock on one vnet and unlock on another.
991 ifindex_free_locked(ifp->if_index);
995 * Perform interface-specific reassignment tasks, if provided by
998 if (ifp->if_reassign != NULL)
999 ifp->if_reassign(ifp, new_vnet, NULL);
1002 * Switch to the context of the target vnet.
1004 CURVNET_SET_QUIET(new_vnet);
1007 if (ifindex_alloc_locked(&idx) != 0) {
1009 panic("if_index overflow");
1011 ifp->if_index = idx;
1012 ifnet_setbyindex_locked(ifp->if_index, ifp);
1015 if_attach_internal(ifp, 1);
1021 * Move an ifnet to or from another child prison/vnet, specified by the jail id.
1024 if_vmove_loan(struct thread *td, struct ifnet *ifp, char *ifname, int jid)
1029 /* Try to find the prison within our visibility. */
1030 sx_slock(&allprison_lock);
1031 pr = prison_find_child(td->td_ucred->cr_prison, jid);
1032 sx_sunlock(&allprison_lock);
1035 prison_hold_locked(pr);
1036 mtx_unlock(&pr->pr_mtx);
1038 /* Do not try to move the iface from and to the same prison. */
1039 if (pr->pr_vnet == ifp->if_vnet) {
1044 /* Make sure the named iface does not exists in the dst. prison/vnet. */
1045 /* XXX Lock interfaces to avoid races. */
1046 CURVNET_SET_QUIET(pr->pr_vnet);
1047 difp = ifunit(ifname);
1054 /* Move the interface into the child jail/vnet. */
1055 if_vmove(ifp, pr->pr_vnet);
1057 /* Report the new if_xname back to the userland. */
1058 sprintf(ifname, "%s", ifp->if_xname);
1065 if_vmove_reclaim(struct thread *td, char *ifname, int jid)
1068 struct vnet *vnet_dst;
1071 /* Try to find the prison within our visibility. */
1072 sx_slock(&allprison_lock);
1073 pr = prison_find_child(td->td_ucred->cr_prison, jid);
1074 sx_sunlock(&allprison_lock);
1077 prison_hold_locked(pr);
1078 mtx_unlock(&pr->pr_mtx);
1080 /* Make sure the named iface exists in the source prison/vnet. */
1081 CURVNET_SET(pr->pr_vnet);
1082 ifp = ifunit(ifname); /* XXX Lock to avoid races. */
1089 /* Do not try to move the iface from and to the same prison. */
1090 vnet_dst = TD_TO_VNET(td);
1091 if (vnet_dst == ifp->if_vnet) {
1097 /* Get interface back from child jail/vnet. */
1098 if_vmove(ifp, vnet_dst);
1101 /* Report the new if_xname back to the userland. */
1102 sprintf(ifname, "%s", ifp->if_xname);
1110 * Add a group to an interface
1113 if_addgroup(struct ifnet *ifp, const char *groupname)
1115 struct ifg_list *ifgl;
1116 struct ifg_group *ifg = NULL;
1117 struct ifg_member *ifgm;
1119 if (groupname[0] && groupname[strlen(groupname) - 1] >= '0' &&
1120 groupname[strlen(groupname) - 1] <= '9')
1124 TAILQ_FOREACH(ifgl, &ifp->if_groups, ifgl_next)
1125 if (!strcmp(ifgl->ifgl_group->ifg_group, groupname)) {
1130 if ((ifgl = (struct ifg_list *)malloc(sizeof(struct ifg_list), M_TEMP,
1131 M_NOWAIT)) == NULL) {
1136 if ((ifgm = (struct ifg_member *)malloc(sizeof(struct ifg_member),
1137 M_TEMP, M_NOWAIT)) == NULL) {
1143 TAILQ_FOREACH(ifg, &V_ifg_head, ifg_next)
1144 if (!strcmp(ifg->ifg_group, groupname))
1148 if ((ifg = (struct ifg_group *)malloc(sizeof(struct ifg_group),
1149 M_TEMP, M_NOWAIT)) == NULL) {
1155 strlcpy(ifg->ifg_group, groupname, sizeof(ifg->ifg_group));
1156 ifg->ifg_refcnt = 0;
1157 TAILQ_INIT(&ifg->ifg_members);
1158 EVENTHANDLER_INVOKE(group_attach_event, ifg);
1159 TAILQ_INSERT_TAIL(&V_ifg_head, ifg, ifg_next);
1163 ifgl->ifgl_group = ifg;
1164 ifgm->ifgm_ifp = ifp;
1167 TAILQ_INSERT_TAIL(&ifg->ifg_members, ifgm, ifgm_next);
1168 TAILQ_INSERT_TAIL(&ifp->if_groups, ifgl, ifgl_next);
1169 IF_ADDR_WUNLOCK(ifp);
1173 EVENTHANDLER_INVOKE(group_change_event, groupname);
1179 * Remove a group from an interface
1182 if_delgroup(struct ifnet *ifp, const char *groupname)
1184 struct ifg_list *ifgl;
1185 struct ifg_member *ifgm;
1188 TAILQ_FOREACH(ifgl, &ifp->if_groups, ifgl_next)
1189 if (!strcmp(ifgl->ifgl_group->ifg_group, groupname))
1197 TAILQ_REMOVE(&ifp->if_groups, ifgl, ifgl_next);
1198 IF_ADDR_WUNLOCK(ifp);
1200 TAILQ_FOREACH(ifgm, &ifgl->ifgl_group->ifg_members, ifgm_next)
1201 if (ifgm->ifgm_ifp == ifp)
1205 TAILQ_REMOVE(&ifgl->ifgl_group->ifg_members, ifgm, ifgm_next);
1209 if (--ifgl->ifgl_group->ifg_refcnt == 0) {
1210 TAILQ_REMOVE(&V_ifg_head, ifgl->ifgl_group, ifg_next);
1211 EVENTHANDLER_INVOKE(group_detach_event, ifgl->ifgl_group);
1212 free(ifgl->ifgl_group, M_TEMP);
1218 EVENTHANDLER_INVOKE(group_change_event, groupname);
1224 * Remove an interface from all groups
1227 if_delgroups(struct ifnet *ifp)
1229 struct ifg_list *ifgl;
1230 struct ifg_member *ifgm;
1231 char groupname[IFNAMSIZ];
1234 while (!TAILQ_EMPTY(&ifp->if_groups)) {
1235 ifgl = TAILQ_FIRST(&ifp->if_groups);
1237 strlcpy(groupname, ifgl->ifgl_group->ifg_group, IFNAMSIZ);
1240 TAILQ_REMOVE(&ifp->if_groups, ifgl, ifgl_next);
1241 IF_ADDR_WUNLOCK(ifp);
1243 TAILQ_FOREACH(ifgm, &ifgl->ifgl_group->ifg_members, ifgm_next)
1244 if (ifgm->ifgm_ifp == ifp)
1248 TAILQ_REMOVE(&ifgl->ifgl_group->ifg_members, ifgm,
1253 if (--ifgl->ifgl_group->ifg_refcnt == 0) {
1254 TAILQ_REMOVE(&V_ifg_head, ifgl->ifgl_group, ifg_next);
1255 EVENTHANDLER_INVOKE(group_detach_event,
1257 free(ifgl->ifgl_group, M_TEMP);
1263 EVENTHANDLER_INVOKE(group_change_event, groupname);
1271 * Stores all groups from an interface in memory pointed
1275 if_getgroup(struct ifgroupreq *data, struct ifnet *ifp)
1278 struct ifg_list *ifgl;
1279 struct ifg_req ifgrq, *ifgp;
1280 struct ifgroupreq *ifgr = data;
1282 if (ifgr->ifgr_len == 0) {
1284 TAILQ_FOREACH(ifgl, &ifp->if_groups, ifgl_next)
1285 ifgr->ifgr_len += sizeof(struct ifg_req);
1286 IF_ADDR_RUNLOCK(ifp);
1290 len = ifgr->ifgr_len;
1291 ifgp = ifgr->ifgr_groups;
1294 TAILQ_FOREACH(ifgl, &ifp->if_groups, ifgl_next) {
1295 if (len < sizeof(ifgrq)) {
1296 IF_ADDR_RUNLOCK(ifp);
1299 bzero(&ifgrq, sizeof ifgrq);
1300 strlcpy(ifgrq.ifgrq_group, ifgl->ifgl_group->ifg_group,
1301 sizeof(ifgrq.ifgrq_group));
1302 if ((error = copyout(&ifgrq, ifgp, sizeof(struct ifg_req)))) {
1303 IF_ADDR_RUNLOCK(ifp);
1306 len -= sizeof(ifgrq);
1309 IF_ADDR_RUNLOCK(ifp);
1315 * Stores all members of a group in memory pointed to by data
1318 if_getgroupmembers(struct ifgroupreq *data)
1320 struct ifgroupreq *ifgr = data;
1321 struct ifg_group *ifg;
1322 struct ifg_member *ifgm;
1323 struct ifg_req ifgrq, *ifgp;
1327 TAILQ_FOREACH(ifg, &V_ifg_head, ifg_next)
1328 if (!strcmp(ifg->ifg_group, ifgr->ifgr_name))
1335 if (ifgr->ifgr_len == 0) {
1336 TAILQ_FOREACH(ifgm, &ifg->ifg_members, ifgm_next)
1337 ifgr->ifgr_len += sizeof(ifgrq);
1342 len = ifgr->ifgr_len;
1343 ifgp = ifgr->ifgr_groups;
1344 TAILQ_FOREACH(ifgm, &ifg->ifg_members, ifgm_next) {
1345 if (len < sizeof(ifgrq)) {
1349 bzero(&ifgrq, sizeof ifgrq);
1350 strlcpy(ifgrq.ifgrq_member, ifgm->ifgm_ifp->if_xname,
1351 sizeof(ifgrq.ifgrq_member));
1352 if ((error = copyout(&ifgrq, ifgp, sizeof(struct ifg_req)))) {
1356 len -= sizeof(ifgrq);
1365 * Delete Routes for a Network Interface
1367 * Called for each routing entry via the rnh->rnh_walktree() call above
1368 * to delete all route entries referencing a detaching network interface.
1371 * rn pointer to node in the routing table
1372 * arg argument passed to rnh->rnh_walktree() - detaching interface
1376 * errno failed - reason indicated
1380 if_rtdel(struct radix_node *rn, void *arg)
1382 struct rtentry *rt = (struct rtentry *)rn;
1383 struct ifnet *ifp = arg;
1386 if (rt->rt_ifp == ifp) {
1389 * Protect (sorta) against walktree recursion problems
1390 * with cloned routes
1392 if ((rt->rt_flags & RTF_UP) == 0)
1395 err = rtrequest_fib(RTM_DELETE, rt_key(rt), rt->rt_gateway,
1397 rt->rt_flags|RTF_RNH_LOCKED|RTF_PINNED,
1398 (struct rtentry **) NULL, rt->rt_fibnum);
1400 log(LOG_WARNING, "if_rtdel: error %d\n", err);
1408 * Wrapper functions for struct ifnet address list locking macros. These are
1409 * used by kernel modules to avoid encoding programming interface or binary
1410 * interface assumptions that may be violated when kernel-internal locking
1411 * approaches change.
1414 if_addr_rlock(struct ifnet *ifp)
1421 if_addr_runlock(struct ifnet *ifp)
1424 IF_ADDR_RUNLOCK(ifp);
1428 if_maddr_rlock(struct ifnet *ifp)
1435 if_maddr_runlock(struct ifnet *ifp)
1438 IF_ADDR_RUNLOCK(ifp);
1442 * Reference count functions for ifaddrs.
1445 ifa_init(struct ifaddr *ifa)
1448 mtx_init(&ifa->ifa_mtx, "ifaddr", NULL, MTX_DEF);
1449 refcount_init(&ifa->ifa_refcnt, 1);
1453 ifa_ref(struct ifaddr *ifa)
1456 refcount_acquire(&ifa->ifa_refcnt);
1460 ifa_free(struct ifaddr *ifa)
1463 if (refcount_release(&ifa->ifa_refcnt)) {
1464 mtx_destroy(&ifa->ifa_mtx);
1465 free(ifa, M_IFADDR);
1470 ifa_add_loopback_route(struct ifaddr *ifa, struct sockaddr *ia)
1473 struct rtentry *rt = NULL;
1474 struct rt_addrinfo info;
1475 static struct sockaddr_dl null_sdl = {sizeof(null_sdl), AF_LINK};
1477 bzero(&info, sizeof(info));
1478 info.rti_ifp = V_loif;
1479 info.rti_flags = ifa->ifa_flags | RTF_HOST | RTF_STATIC;
1480 info.rti_info[RTAX_DST] = ia;
1481 info.rti_info[RTAX_GATEWAY] = (struct sockaddr *)&null_sdl;
1482 error = rtrequest1_fib(RTM_ADD, &info, &rt, ifa->ifa_ifp->if_fib);
1484 if (error == 0 && rt != NULL) {
1486 ((struct sockaddr_dl *)rt->rt_gateway)->sdl_type =
1487 ifa->ifa_ifp->if_type;
1488 ((struct sockaddr_dl *)rt->rt_gateway)->sdl_index =
1489 ifa->ifa_ifp->if_index;
1492 } else if (error != 0)
1493 log(LOG_INFO, "ifa_add_loopback_route: insertion failed\n");
1499 ifa_del_loopback_route(struct ifaddr *ifa, struct sockaddr *ia)
1502 struct rt_addrinfo info;
1503 struct sockaddr_dl null_sdl;
1505 bzero(&null_sdl, sizeof(null_sdl));
1506 null_sdl.sdl_len = sizeof(null_sdl);
1507 null_sdl.sdl_family = AF_LINK;
1508 null_sdl.sdl_type = ifa->ifa_ifp->if_type;
1509 null_sdl.sdl_index = ifa->ifa_ifp->if_index;
1510 bzero(&info, sizeof(info));
1511 info.rti_flags = ifa->ifa_flags | RTF_HOST | RTF_STATIC;
1512 info.rti_info[RTAX_DST] = ia;
1513 info.rti_info[RTAX_GATEWAY] = (struct sockaddr *)&null_sdl;
1514 error = rtrequest1_fib(RTM_DELETE, &info, NULL, ifa->ifa_ifp->if_fib);
1517 log(LOG_INFO, "ifa_del_loopback_route: deletion failed\n");
1523 * XXX: Because sockaddr_dl has deeper structure than the sockaddr
1524 * structs used to represent other address families, it is necessary
1525 * to perform a different comparison.
1528 #define sa_equal(a1, a2) \
1529 (bcmp((a1), (a2), ((a1))->sa_len) == 0)
1531 #define sa_dl_equal(a1, a2) \
1532 ((((struct sockaddr_dl *)(a1))->sdl_len == \
1533 ((struct sockaddr_dl *)(a2))->sdl_len) && \
1534 (bcmp(LLADDR((struct sockaddr_dl *)(a1)), \
1535 LLADDR((struct sockaddr_dl *)(a2)), \
1536 ((struct sockaddr_dl *)(a1))->sdl_alen) == 0))
1539 * Locate an interface based on a complete address.
1542 static struct ifaddr *
1543 ifa_ifwithaddr_internal(struct sockaddr *addr, int getref)
1548 IFNET_RLOCK_NOSLEEP();
1549 TAILQ_FOREACH(ifp, &V_ifnet, if_link) {
1551 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
1552 if (ifa->ifa_addr->sa_family != addr->sa_family)
1554 if (sa_equal(addr, ifa->ifa_addr)) {
1557 IF_ADDR_RUNLOCK(ifp);
1560 /* IP6 doesn't have broadcast */
1561 if ((ifp->if_flags & IFF_BROADCAST) &&
1562 ifa->ifa_broadaddr &&
1563 ifa->ifa_broadaddr->sa_len != 0 &&
1564 sa_equal(ifa->ifa_broadaddr, addr)) {
1567 IF_ADDR_RUNLOCK(ifp);
1571 IF_ADDR_RUNLOCK(ifp);
1575 IFNET_RUNLOCK_NOSLEEP();
1580 ifa_ifwithaddr(struct sockaddr *addr)
1583 return (ifa_ifwithaddr_internal(addr, 1));
1587 ifa_ifwithaddr_check(struct sockaddr *addr)
1590 return (ifa_ifwithaddr_internal(addr, 0) != NULL);
1594 * Locate an interface based on the broadcast address.
1598 ifa_ifwithbroadaddr(struct sockaddr *addr)
1603 IFNET_RLOCK_NOSLEEP();
1604 TAILQ_FOREACH(ifp, &V_ifnet, if_link) {
1606 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
1607 if (ifa->ifa_addr->sa_family != addr->sa_family)
1609 if ((ifp->if_flags & IFF_BROADCAST) &&
1610 ifa->ifa_broadaddr &&
1611 ifa->ifa_broadaddr->sa_len != 0 &&
1612 sa_equal(ifa->ifa_broadaddr, addr)) {
1614 IF_ADDR_RUNLOCK(ifp);
1618 IF_ADDR_RUNLOCK(ifp);
1622 IFNET_RUNLOCK_NOSLEEP();
1627 * Locate the point to point interface with a given destination address.
1631 ifa_ifwithdstaddr_fib(struct sockaddr *addr, int fibnum)
1636 IFNET_RLOCK_NOSLEEP();
1637 TAILQ_FOREACH(ifp, &V_ifnet, if_link) {
1638 if ((ifp->if_flags & IFF_POINTOPOINT) == 0)
1640 if ((fibnum != RT_ALL_FIBS) && (ifp->if_fib != fibnum))
1643 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
1644 if (ifa->ifa_addr->sa_family != addr->sa_family)
1646 if (ifa->ifa_dstaddr != NULL &&
1647 sa_equal(addr, ifa->ifa_dstaddr)) {
1649 IF_ADDR_RUNLOCK(ifp);
1653 IF_ADDR_RUNLOCK(ifp);
1657 IFNET_RUNLOCK_NOSLEEP();
1662 ifa_ifwithdstaddr(struct sockaddr *addr)
1665 return (ifa_ifwithdstaddr_fib(addr, RT_ALL_FIBS));
1669 * Find an interface on a specific network. If many, choice
1670 * is most specific found.
1673 ifa_ifwithnet_fib(struct sockaddr *addr, int ignore_ptp, int fibnum)
1677 struct ifaddr *ifa_maybe = NULL;
1678 u_int af = addr->sa_family;
1679 char *addr_data = addr->sa_data, *cplim;
1682 * AF_LINK addresses can be looked up directly by their index number,
1683 * so do that if we can.
1685 if (af == AF_LINK) {
1686 struct sockaddr_dl *sdl = (struct sockaddr_dl *)addr;
1687 if (sdl->sdl_index && sdl->sdl_index <= V_if_index)
1688 return (ifaddr_byindex(sdl->sdl_index));
1692 * Scan though each interface, looking for ones that have addresses
1693 * in this address family and the requested fib. Maintain a reference
1694 * on ifa_maybe once we find one, as we release the IF_ADDR_RLOCK() that
1695 * kept it stable when we move onto the next interface.
1697 IFNET_RLOCK_NOSLEEP();
1698 TAILQ_FOREACH(ifp, &V_ifnet, if_link) {
1699 if ((fibnum != RT_ALL_FIBS) && (ifp->if_fib != fibnum))
1702 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
1703 char *cp, *cp2, *cp3;
1705 if (ifa->ifa_addr->sa_family != af)
1707 if (af == AF_INET &&
1708 ifp->if_flags & IFF_POINTOPOINT && !ignore_ptp) {
1710 * This is a bit broken as it doesn't
1711 * take into account that the remote end may
1712 * be a single node in the network we are
1714 * The trouble is that we don't know the
1715 * netmask for the remote end.
1717 if (ifa->ifa_dstaddr != NULL &&
1718 sa_equal(addr, ifa->ifa_dstaddr)) {
1720 IF_ADDR_RUNLOCK(ifp);
1725 * if we have a special address handler,
1726 * then use it instead of the generic one.
1728 if (ifa->ifa_claim_addr) {
1729 if ((*ifa->ifa_claim_addr)(ifa, addr)) {
1731 IF_ADDR_RUNLOCK(ifp);
1738 * Scan all the bits in the ifa's address.
1739 * If a bit dissagrees with what we are
1740 * looking for, mask it with the netmask
1741 * to see if it really matters.
1742 * (A byte at a time)
1744 if (ifa->ifa_netmask == 0)
1747 cp2 = ifa->ifa_addr->sa_data;
1748 cp3 = ifa->ifa_netmask->sa_data;
1749 cplim = ifa->ifa_netmask->sa_len
1750 + (char *)ifa->ifa_netmask;
1752 if ((*cp++ ^ *cp2++) & *cp3++)
1753 goto next; /* next address! */
1755 * If the netmask of what we just found
1756 * is more specific than what we had before
1757 * (if we had one) then remember the new one
1758 * before continuing to search
1759 * for an even better one.
1761 if (ifa_maybe == NULL ||
1762 rn_refines((caddr_t)ifa->ifa_netmask,
1763 (caddr_t)ifa_maybe->ifa_netmask)) {
1764 if (ifa_maybe != NULL)
1765 ifa_free(ifa_maybe);
1771 IF_ADDR_RUNLOCK(ifp);
1776 IFNET_RUNLOCK_NOSLEEP();
1777 if (ifa_maybe != NULL)
1778 ifa_free(ifa_maybe);
1783 ifa_ifwithnet(struct sockaddr *addr, int ignore_ptp)
1786 return (ifa_ifwithnet_fib(addr, ignore_ptp, RT_ALL_FIBS));
1790 * Find an interface address specific to an interface best matching
1794 ifaof_ifpforaddr(struct sockaddr *addr, struct ifnet *ifp)
1797 char *cp, *cp2, *cp3;
1799 struct ifaddr *ifa_maybe = NULL;
1800 u_int af = addr->sa_family;
1805 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
1806 if (ifa->ifa_addr->sa_family != af)
1808 if (ifa_maybe == NULL)
1810 if (ifa->ifa_netmask == 0) {
1811 if (sa_equal(addr, ifa->ifa_addr) ||
1812 (ifa->ifa_dstaddr &&
1813 sa_equal(addr, ifa->ifa_dstaddr)))
1817 if (ifp->if_flags & IFF_POINTOPOINT) {
1818 if (sa_equal(addr, ifa->ifa_dstaddr))
1822 cp2 = ifa->ifa_addr->sa_data;
1823 cp3 = ifa->ifa_netmask->sa_data;
1824 cplim = ifa->ifa_netmask->sa_len + (char *)ifa->ifa_netmask;
1825 for (; cp3 < cplim; cp3++)
1826 if ((*cp++ ^ *cp2++) & *cp3)
1836 IF_ADDR_RUNLOCK(ifp);
1840 #include <net/if_llatbl.h>
1843 * Default action when installing a route with a Link Level gateway.
1844 * Lookup an appropriate real ifa to point to.
1845 * This should be moved to /sys/net/link.c eventually.
1848 link_rtrequest(int cmd, struct rtentry *rt, struct rt_addrinfo *info)
1850 struct ifaddr *ifa, *oifa;
1851 struct sockaddr *dst;
1856 if (cmd != RTM_ADD || ((ifa = rt->rt_ifa) == 0) ||
1857 ((ifp = ifa->ifa_ifp) == 0) || ((dst = rt_key(rt)) == 0))
1859 ifa = ifaof_ifpforaddr(dst, ifp);
1864 if (ifa->ifa_rtrequest && ifa->ifa_rtrequest != link_rtrequest)
1865 ifa->ifa_rtrequest(cmd, rt, info);
1870 * Mark an interface down and notify protocols of
1872 * NOTE: must be called at splnet or eqivalent.
1875 if_unroute(struct ifnet *ifp, int flag, int fam)
1879 KASSERT(flag == IFF_UP, ("if_unroute: flag != IFF_UP"));
1881 ifp->if_flags &= ~flag;
1882 getmicrotime(&ifp->if_lastchange);
1883 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link)
1884 if (fam == PF_UNSPEC || (fam == ifa->ifa_addr->sa_family))
1885 pfctlinput(PRC_IFDOWN, ifa->ifa_addr);
1886 ifp->if_qflush(ifp);
1889 (*carp_linkstate_p)(ifp);
1894 * Mark an interface up and notify protocols of
1896 * NOTE: must be called at splnet or eqivalent.
1899 if_route(struct ifnet *ifp, int flag, int fam)
1903 KASSERT(flag == IFF_UP, ("if_route: flag != IFF_UP"));
1905 ifp->if_flags |= flag;
1906 getmicrotime(&ifp->if_lastchange);
1907 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link)
1908 if (fam == PF_UNSPEC || (fam == ifa->ifa_addr->sa_family))
1909 pfctlinput(PRC_IFUP, ifa->ifa_addr);
1911 (*carp_linkstate_p)(ifp);
1918 void (*vlan_link_state_p)(struct ifnet *); /* XXX: private from if_vlan */
1919 void (*vlan_trunk_cap_p)(struct ifnet *); /* XXX: private from if_vlan */
1920 struct ifnet *(*vlan_trunkdev_p)(struct ifnet *);
1921 struct ifnet *(*vlan_devat_p)(struct ifnet *, uint16_t);
1922 int (*vlan_tag_p)(struct ifnet *, uint16_t *);
1923 int (*vlan_setcookie_p)(struct ifnet *, void *);
1924 void *(*vlan_cookie_p)(struct ifnet *);
1927 * Handle a change in the interface link state. To avoid LORs
1928 * between driver lock and upper layer locks, as well as possible
1929 * recursions, we post event to taskqueue, and all job
1930 * is done in static do_link_state_change().
1933 if_link_state_change(struct ifnet *ifp, int link_state)
1935 /* Return if state hasn't changed. */
1936 if (ifp->if_link_state == link_state)
1939 ifp->if_link_state = link_state;
1941 taskqueue_enqueue(taskqueue_swi, &ifp->if_linktask);
1945 do_link_state_change(void *arg, int pending)
1947 struct ifnet *ifp = (struct ifnet *)arg;
1948 int link_state = ifp->if_link_state;
1949 CURVNET_SET(ifp->if_vnet);
1951 /* Notify that the link state has changed. */
1953 if (ifp->if_vlantrunk != NULL)
1954 (*vlan_link_state_p)(ifp);
1956 if ((ifp->if_type == IFT_ETHER || ifp->if_type == IFT_L2VLAN) &&
1957 IFP2AC(ifp)->ac_netgraph != NULL)
1958 (*ng_ether_link_state_p)(ifp, link_state);
1960 (*carp_linkstate_p)(ifp);
1962 (*bridge_linkstate_p)(ifp);
1964 (*lagg_linkstate_p)(ifp, link_state);
1966 if (IS_DEFAULT_VNET(curvnet))
1967 devctl_notify("IFNET", ifp->if_xname,
1968 (link_state == LINK_STATE_UP) ? "LINK_UP" : "LINK_DOWN",
1971 if_printf(ifp, "%d link states coalesced\n", pending);
1972 if (log_link_state_change)
1973 log(LOG_NOTICE, "%s: link state changed to %s\n", ifp->if_xname,
1974 (link_state == LINK_STATE_UP) ? "UP" : "DOWN" );
1975 EVENTHANDLER_INVOKE(ifnet_link_event, ifp, ifp->if_link_state);
1980 * Mark an interface down and notify protocols of
1982 * NOTE: must be called at splnet or eqivalent.
1985 if_down(struct ifnet *ifp)
1988 if_unroute(ifp, IFF_UP, AF_UNSPEC);
1992 * Mark an interface up and notify protocols of
1994 * NOTE: must be called at splnet or eqivalent.
1997 if_up(struct ifnet *ifp)
2000 if_route(ifp, IFF_UP, AF_UNSPEC);
2004 * Flush an interface queue.
2007 if_qflush(struct ifnet *ifp)
2015 if (ALTQ_IS_ENABLED(ifq))
2019 while ((m = n) != 0) {
2030 * Map interface name to interface structure pointer, with or without
2031 * returning a reference.
2034 ifunit_ref(const char *name)
2038 IFNET_RLOCK_NOSLEEP();
2039 TAILQ_FOREACH(ifp, &V_ifnet, if_link) {
2040 if (strncmp(name, ifp->if_xname, IFNAMSIZ) == 0 &&
2041 !(ifp->if_flags & IFF_DYING))
2046 IFNET_RUNLOCK_NOSLEEP();
2051 ifunit(const char *name)
2055 IFNET_RLOCK_NOSLEEP();
2056 TAILQ_FOREACH(ifp, &V_ifnet, if_link) {
2057 if (strncmp(name, ifp->if_xname, IFNAMSIZ) == 0)
2060 IFNET_RUNLOCK_NOSLEEP();
2065 * Hardware specific interface ioctls.
2068 ifhwioctl(u_long cmd, struct ifnet *ifp, caddr_t data, struct thread *td)
2073 int new_flags, temp_flags;
2074 size_t namelen, onamelen;
2076 char *descrbuf, *odescrbuf;
2077 char new_name[IFNAMSIZ];
2079 struct sockaddr_dl *sdl;
2081 ifr = (struct ifreq *)data;
2084 ifr->ifr_index = ifp->if_index;
2088 temp_flags = ifp->if_flags | ifp->if_drv_flags;
2089 ifr->ifr_flags = temp_flags & 0xffff;
2090 ifr->ifr_flagshigh = temp_flags >> 16;
2094 ifr->ifr_reqcap = ifp->if_capabilities;
2095 ifr->ifr_curcap = ifp->if_capenable;
2100 error = mac_ifnet_ioctl_get(td->td_ucred, ifr, ifp);
2105 ifr->ifr_metric = ifp->if_metric;
2109 ifr->ifr_mtu = ifp->if_mtu;
2113 ifr->ifr_phys = ifp->if_physical;
2118 sx_slock(&ifdescr_sx);
2119 if (ifp->if_description == NULL)
2122 /* space for terminating nul */
2123 descrlen = strlen(ifp->if_description) + 1;
2124 if (ifr->ifr_buffer.length < descrlen)
2125 ifr->ifr_buffer.buffer = NULL;
2127 error = copyout(ifp->if_description,
2128 ifr->ifr_buffer.buffer, descrlen);
2129 ifr->ifr_buffer.length = descrlen;
2131 sx_sunlock(&ifdescr_sx);
2135 error = priv_check(td, PRIV_NET_SETIFDESCR);
2140 * Copy only (length-1) bytes to make sure that
2141 * if_description is always nul terminated. The
2142 * length parameter is supposed to count the
2143 * terminating nul in.
2145 if (ifr->ifr_buffer.length > ifdescr_maxlen)
2146 return (ENAMETOOLONG);
2147 else if (ifr->ifr_buffer.length == 0)
2150 descrbuf = malloc(ifr->ifr_buffer.length, M_IFDESCR,
2152 error = copyin(ifr->ifr_buffer.buffer, descrbuf,
2153 ifr->ifr_buffer.length - 1);
2155 free(descrbuf, M_IFDESCR);
2160 sx_xlock(&ifdescr_sx);
2161 odescrbuf = ifp->if_description;
2162 ifp->if_description = descrbuf;
2163 sx_xunlock(&ifdescr_sx);
2165 getmicrotime(&ifp->if_lastchange);
2166 free(odescrbuf, M_IFDESCR);
2170 ifr->ifr_fib = ifp->if_fib;
2174 error = priv_check(td, PRIV_NET_SETIFFIB);
2177 if (ifr->ifr_fib >= rt_numfibs)
2180 ifp->if_fib = ifr->ifr_fib;
2184 error = priv_check(td, PRIV_NET_SETIFFLAGS);
2188 * Currently, no driver owned flags pass the IFF_CANTCHANGE
2189 * check, so we don't need special handling here yet.
2191 new_flags = (ifr->ifr_flags & 0xffff) |
2192 (ifr->ifr_flagshigh << 16);
2193 if (ifp->if_flags & IFF_SMART) {
2194 /* Smart drivers twiddle their own routes */
2195 } else if (ifp->if_flags & IFF_UP &&
2196 (new_flags & IFF_UP) == 0) {
2200 } else if (new_flags & IFF_UP &&
2201 (ifp->if_flags & IFF_UP) == 0) {
2206 /* See if permanently promiscuous mode bit is about to flip */
2207 if ((ifp->if_flags ^ new_flags) & IFF_PPROMISC) {
2208 if (new_flags & IFF_PPROMISC)
2209 ifp->if_flags |= IFF_PROMISC;
2210 else if (ifp->if_pcount == 0)
2211 ifp->if_flags &= ~IFF_PROMISC;
2212 log(LOG_INFO, "%s: permanently promiscuous mode %s\n",
2214 (new_flags & IFF_PPROMISC) ? "enabled" : "disabled");
2216 ifp->if_flags = (ifp->if_flags & IFF_CANTCHANGE) |
2217 (new_flags &~ IFF_CANTCHANGE);
2218 if (ifp->if_ioctl) {
2219 (void) (*ifp->if_ioctl)(ifp, cmd, data);
2221 getmicrotime(&ifp->if_lastchange);
2225 error = priv_check(td, PRIV_NET_SETIFCAP);
2228 if (ifp->if_ioctl == NULL)
2229 return (EOPNOTSUPP);
2230 if (ifr->ifr_reqcap & ~ifp->if_capabilities)
2232 error = (*ifp->if_ioctl)(ifp, cmd, data);
2234 getmicrotime(&ifp->if_lastchange);
2239 error = mac_ifnet_ioctl_set(td->td_ucred, ifr, ifp);
2244 error = priv_check(td, PRIV_NET_SETIFNAME);
2247 error = copyinstr(ifr->ifr_data, new_name, IFNAMSIZ, NULL);
2250 if (new_name[0] == '\0')
2252 if (ifunit(new_name) != NULL)
2256 * XXX: Locking. Nothing else seems to lock if_flags,
2257 * and there are numerous other races with the
2258 * ifunit() checks not being atomic with namespace
2259 * changes (renames, vmoves, if_attach, etc).
2261 ifp->if_flags |= IFF_RENAMING;
2263 /* Announce the departure of the interface. */
2264 rt_ifannouncemsg(ifp, IFAN_DEPARTURE);
2265 EVENTHANDLER_INVOKE(ifnet_departure_event, ifp);
2267 log(LOG_INFO, "%s: changing name to '%s'\n",
2268 ifp->if_xname, new_name);
2270 strlcpy(ifp->if_xname, new_name, sizeof(ifp->if_xname));
2273 sdl = (struct sockaddr_dl *)ifa->ifa_addr;
2274 namelen = strlen(new_name);
2275 onamelen = sdl->sdl_nlen;
2277 * Move the address if needed. This is safe because we
2278 * allocate space for a name of length IFNAMSIZ when we
2279 * create this in if_attach().
2281 if (namelen != onamelen) {
2282 bcopy(sdl->sdl_data + onamelen,
2283 sdl->sdl_data + namelen, sdl->sdl_alen);
2285 bcopy(new_name, sdl->sdl_data, namelen);
2286 sdl->sdl_nlen = namelen;
2287 sdl = (struct sockaddr_dl *)ifa->ifa_netmask;
2288 bzero(sdl->sdl_data, onamelen);
2289 while (namelen != 0)
2290 sdl->sdl_data[--namelen] = 0xff;
2293 EVENTHANDLER_INVOKE(ifnet_arrival_event, ifp);
2294 /* Announce the return of the interface. */
2295 rt_ifannouncemsg(ifp, IFAN_ARRIVAL);
2297 ifp->if_flags &= ~IFF_RENAMING;
2302 error = priv_check(td, PRIV_NET_SETIFVNET);
2305 error = if_vmove_loan(td, ifp, ifr->ifr_name, ifr->ifr_jid);
2310 error = priv_check(td, PRIV_NET_SETIFMETRIC);
2313 ifp->if_metric = ifr->ifr_metric;
2314 getmicrotime(&ifp->if_lastchange);
2318 error = priv_check(td, PRIV_NET_SETIFPHYS);
2321 if (ifp->if_ioctl == NULL)
2322 return (EOPNOTSUPP);
2323 error = (*ifp->if_ioctl)(ifp, cmd, data);
2325 getmicrotime(&ifp->if_lastchange);
2330 u_long oldmtu = ifp->if_mtu;
2332 error = priv_check(td, PRIV_NET_SETIFMTU);
2335 if (ifr->ifr_mtu < IF_MINMTU || ifr->ifr_mtu > IF_MAXMTU)
2337 if (ifp->if_ioctl == NULL)
2338 return (EOPNOTSUPP);
2339 error = (*ifp->if_ioctl)(ifp, cmd, data);
2341 getmicrotime(&ifp->if_lastchange);
2345 * If the link MTU changed, do network layer specific procedure.
2347 if (ifp->if_mtu != oldmtu) {
2357 if (cmd == SIOCADDMULTI)
2358 error = priv_check(td, PRIV_NET_ADDMULTI);
2360 error = priv_check(td, PRIV_NET_DELMULTI);
2364 /* Don't allow group membership on non-multicast interfaces. */
2365 if ((ifp->if_flags & IFF_MULTICAST) == 0)
2366 return (EOPNOTSUPP);
2368 /* Don't let users screw up protocols' entries. */
2369 if (ifr->ifr_addr.sa_family != AF_LINK)
2372 if (cmd == SIOCADDMULTI) {
2373 struct ifmultiaddr *ifma;
2376 * Userland is only permitted to join groups once
2377 * via the if_addmulti() KPI, because it cannot hold
2378 * struct ifmultiaddr * between calls. It may also
2379 * lose a race while we check if the membership
2383 ifma = if_findmulti(ifp, &ifr->ifr_addr);
2384 IF_ADDR_RUNLOCK(ifp);
2388 error = if_addmulti(ifp, &ifr->ifr_addr, &ifma);
2390 error = if_delmulti(ifp, &ifr->ifr_addr);
2393 getmicrotime(&ifp->if_lastchange);
2396 case SIOCSIFPHYADDR:
2397 case SIOCDIFPHYADDR:
2399 case SIOCSIFPHYADDR_IN6:
2401 case SIOCSLIFPHYADDR:
2403 case SIOCSIFGENERIC:
2404 error = priv_check(td, PRIV_NET_HWIOCTL);
2407 if (ifp->if_ioctl == NULL)
2408 return (EOPNOTSUPP);
2409 error = (*ifp->if_ioctl)(ifp, cmd, data);
2411 getmicrotime(&ifp->if_lastchange);
2415 ifs = (struct ifstat *)data;
2416 ifs->ascii[0] = '\0';
2418 case SIOCGIFPSRCADDR:
2419 case SIOCGIFPDSTADDR:
2420 case SIOCGLIFPHYADDR:
2422 case SIOCGIFGENERIC:
2423 if (ifp->if_ioctl == NULL)
2424 return (EOPNOTSUPP);
2425 error = (*ifp->if_ioctl)(ifp, cmd, data);
2429 error = priv_check(td, PRIV_NET_SETLLADDR);
2432 error = if_setlladdr(ifp,
2433 ifr->ifr_addr.sa_data, ifr->ifr_addr.sa_len);
2434 EVENTHANDLER_INVOKE(iflladdr_event, ifp);
2439 struct ifgroupreq *ifgr = (struct ifgroupreq *)ifr;
2441 error = priv_check(td, PRIV_NET_ADDIFGROUP);
2444 if ((error = if_addgroup(ifp, ifgr->ifgr_group)))
2450 if ((error = if_getgroup((struct ifgroupreq *)ifr, ifp)))
2456 struct ifgroupreq *ifgr = (struct ifgroupreq *)ifr;
2458 error = priv_check(td, PRIV_NET_DELIFGROUP);
2461 if ((error = if_delgroup(ifp, ifgr->ifgr_group)))
2473 #ifdef COMPAT_FREEBSD32
2481 #define SIOCGIFCONF32 _IOWR('i', 36, struct ifconf32)
2488 ifioctl(struct socket *so, u_long cmd, caddr_t data, struct thread *td)
2495 CURVNET_SET(so->so_vnet);
2499 error = ifconf(cmd, data);
2503 #ifdef COMPAT_FREEBSD32
2506 struct ifconf32 *ifc32;
2509 ifc32 = (struct ifconf32 *)data;
2510 ifc.ifc_len = ifc32->ifc_len;
2511 ifc.ifc_buf = PTRIN(ifc32->ifc_buf);
2513 error = ifconf(SIOCGIFCONF, (void *)&ifc);
2516 ifc32->ifc_len = ifc.ifc_len;
2521 ifr = (struct ifreq *)data;
2526 error = priv_check(td, PRIV_NET_SETIFVNET);
2528 error = if_vmove_reclaim(td, ifr->ifr_name,
2535 error = priv_check(td, PRIV_NET_IFCREATE);
2537 error = if_clone_create(ifr->ifr_name,
2538 sizeof(ifr->ifr_name),
2539 cmd == SIOCIFCREATE2 ? ifr->ifr_data : NULL);
2543 error = priv_check(td, PRIV_NET_IFDESTROY);
2545 error = if_clone_destroy(ifr->ifr_name);
2549 case SIOCIFGCLONERS:
2550 error = if_clone_list((struct if_clonereq *)data);
2554 error = if_getgroupmembers((struct ifgroupreq *)data);
2559 ifp = ifunit_ref(ifr->ifr_name);
2565 error = ifhwioctl(cmd, ifp, data, td);
2566 if (error != ENOIOCTL) {
2572 oif_flags = ifp->if_flags;
2573 if (so->so_proto == NULL) {
2576 return (EOPNOTSUPP);
2580 * Pass the request on to the socket control method, and if the
2581 * latter returns EOPNOTSUPP, directly to the interface.
2583 * Make an exception for the legacy SIOCSIF* requests. Drivers
2584 * trust SIOCSIFADDR et al to come from an already privileged
2585 * layer, and do not perform any credentials checks or input
2589 error = ((*so->so_proto->pr_usrreqs->pru_control)(so, cmd,
2592 if (error == EOPNOTSUPP && ifp != NULL && ifp->if_ioctl != NULL &&
2593 cmd != SIOCSIFADDR && cmd != SIOCSIFBRDADDR &&
2594 cmd != SIOCSIFDSTADDR && cmd != SIOCSIFNETMASK)
2595 error = (*ifp->if_ioctl)(ifp, cmd, data);
2602 case SIOCSIFDSTADDR:
2604 case SIOCSIFBRDADDR:
2605 case SIOCSIFNETMASK:
2606 #if BYTE_ORDER != BIG_ENDIAN
2607 if (ifr->ifr_addr.sa_family == 0 &&
2608 ifr->ifr_addr.sa_len < 16) {
2609 ifr->ifr_addr.sa_family = ifr->ifr_addr.sa_len;
2610 ifr->ifr_addr.sa_len = 16;
2613 if (ifr->ifr_addr.sa_len == 0)
2614 ifr->ifr_addr.sa_len = 16;
2622 case OSIOCGIFDSTADDR:
2623 cmd = SIOCGIFDSTADDR;
2626 case OSIOCGIFBRDADDR:
2627 cmd = SIOCGIFBRDADDR;
2630 case OSIOCGIFNETMASK:
2631 cmd = SIOCGIFNETMASK;
2633 error = ((*so->so_proto->pr_usrreqs->pru_control)(so,
2637 if (error == EOPNOTSUPP && ifp != NULL &&
2638 ifp->if_ioctl != NULL &&
2639 cmd != SIOCSIFADDR && cmd != SIOCSIFBRDADDR &&
2640 cmd != SIOCSIFDSTADDR && cmd != SIOCSIFNETMASK)
2641 error = (*ifp->if_ioctl)(ifp, cmd, data);
2645 case OSIOCGIFDSTADDR:
2646 case OSIOCGIFBRDADDR:
2647 case OSIOCGIFNETMASK:
2648 *(u_short *)&ifr->ifr_addr = ifr->ifr_addr.sa_family;
2652 #endif /* COMPAT_43 */
2654 if ((oif_flags ^ ifp->if_flags) & IFF_UP) {
2656 if (ifp->if_flags & IFF_UP) {
2669 * The code common to handling reference counted flags,
2670 * e.g., in ifpromisc() and if_allmulti().
2671 * The "pflag" argument can specify a permanent mode flag to check,
2672 * such as IFF_PPROMISC for promiscuous mode; should be 0 if none.
2674 * Only to be used on stack-owned flags, not driver-owned flags.
2677 if_setflag(struct ifnet *ifp, int flag, int pflag, int *refcount, int onswitch)
2681 int oldflags, oldcount;
2683 /* Sanity checks to catch programming errors */
2684 KASSERT((flag & (IFF_DRV_OACTIVE|IFF_DRV_RUNNING)) == 0,
2685 ("%s: setting driver-owned flag %d", __func__, flag));
2688 KASSERT(*refcount >= 0,
2689 ("%s: increment negative refcount %d for flag %d",
2690 __func__, *refcount, flag));
2692 KASSERT(*refcount > 0,
2693 ("%s: decrement non-positive refcount %d for flag %d",
2694 __func__, *refcount, flag));
2696 /* In case this mode is permanent, just touch refcount */
2697 if (ifp->if_flags & pflag) {
2698 *refcount += onswitch ? 1 : -1;
2702 /* Save ifnet parameters for if_ioctl() may fail */
2703 oldcount = *refcount;
2704 oldflags = ifp->if_flags;
2707 * See if we aren't the only and touching refcount is enough.
2708 * Actually toggle interface flag if we are the first or last.
2713 ifp->if_flags |= flag;
2717 ifp->if_flags &= ~flag;
2720 /* Call down the driver since we've changed interface flags */
2721 if (ifp->if_ioctl == NULL) {
2725 ifr.ifr_flags = ifp->if_flags & 0xffff;
2726 ifr.ifr_flagshigh = ifp->if_flags >> 16;
2727 error = (*ifp->if_ioctl)(ifp, SIOCSIFFLAGS, (caddr_t)&ifr);
2730 /* Notify userland that interface flags have changed */
2735 /* Recover after driver error */
2736 *refcount = oldcount;
2737 ifp->if_flags = oldflags;
2742 * Set/clear promiscuous mode on interface ifp based on the truth value
2743 * of pswitch. The calls are reference counted so that only the first
2744 * "on" request actually has an effect, as does the final "off" request.
2745 * Results are undefined if the "off" and "on" requests are not matched.
2748 ifpromisc(struct ifnet *ifp, int pswitch)
2751 int oldflags = ifp->if_flags;
2753 error = if_setflag(ifp, IFF_PROMISC, IFF_PPROMISC,
2754 &ifp->if_pcount, pswitch);
2755 /* If promiscuous mode status has changed, log a message */
2756 if (error == 0 && ((ifp->if_flags ^ oldflags) & IFF_PROMISC))
2757 log(LOG_INFO, "%s: promiscuous mode %s\n",
2759 (ifp->if_flags & IFF_PROMISC) ? "enabled" : "disabled");
2764 * Return interface configuration
2765 * of system. List may be used
2766 * in later ioctl's (above) to get
2767 * other information.
2771 ifconf(u_long cmd, caddr_t data)
2773 struct ifconf *ifc = (struct ifconf *)data;
2778 int error, full = 0, valid_len, max_len;
2780 /* Limit initial buffer size to MAXPHYS to avoid DoS from userspace. */
2781 max_len = MAXPHYS - 1;
2783 /* Prevent hostile input from being able to crash the system */
2784 if (ifc->ifc_len <= 0)
2788 if (ifc->ifc_len <= max_len) {
2789 max_len = ifc->ifc_len;
2792 sb = sbuf_new(NULL, NULL, max_len + 1, SBUF_FIXEDLEN);
2797 TAILQ_FOREACH(ifp, &V_ifnet, if_link) {
2801 * Zero the ifr_name buffer to make sure we don't
2802 * disclose the contents of the stack.
2804 memset(ifr.ifr_name, 0, sizeof(ifr.ifr_name));
2806 if (strlcpy(ifr.ifr_name, ifp->if_xname, sizeof(ifr.ifr_name))
2807 >= sizeof(ifr.ifr_name)) {
2810 return (ENAMETOOLONG);
2815 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
2816 struct sockaddr *sa = ifa->ifa_addr;
2818 if (prison_if(curthread->td_ucred, sa) != 0)
2822 if (cmd == OSIOCGIFCONF) {
2823 struct osockaddr *osa =
2824 (struct osockaddr *)&ifr.ifr_addr;
2826 osa->sa_family = sa->sa_family;
2827 sbuf_bcat(sb, &ifr, sizeof(ifr));
2828 max_len += sizeof(ifr);
2831 if (sa->sa_len <= sizeof(*sa)) {
2833 sbuf_bcat(sb, &ifr, sizeof(ifr));
2834 max_len += sizeof(ifr);
2837 offsetof(struct ifreq, ifr_addr));
2838 max_len += offsetof(struct ifreq, ifr_addr);
2839 sbuf_bcat(sb, sa, sa->sa_len);
2840 max_len += sa->sa_len;
2843 if (sbuf_error(sb) == 0)
2844 valid_len = sbuf_len(sb);
2846 IF_ADDR_RUNLOCK(ifp);
2848 bzero((caddr_t)&ifr.ifr_addr, sizeof(ifr.ifr_addr));
2849 sbuf_bcat(sb, &ifr, sizeof(ifr));
2850 max_len += sizeof(ifr);
2852 if (sbuf_error(sb) == 0)
2853 valid_len = sbuf_len(sb);
2859 * If we didn't allocate enough space (uncommon), try again. If
2860 * we have already allocated as much space as we are allowed,
2861 * return what we've got.
2863 if (valid_len != max_len && !full) {
2868 ifc->ifc_len = valid_len;
2870 error = copyout(sbuf_data(sb), ifc->ifc_req, ifc->ifc_len);
2876 * Just like ifpromisc(), but for all-multicast-reception mode.
2879 if_allmulti(struct ifnet *ifp, int onswitch)
2882 return (if_setflag(ifp, IFF_ALLMULTI, 0, &ifp->if_amcount, onswitch));
2885 struct ifmultiaddr *
2886 if_findmulti(struct ifnet *ifp, struct sockaddr *sa)
2888 struct ifmultiaddr *ifma;
2890 IF_ADDR_LOCK_ASSERT(ifp);
2892 TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
2893 if (sa->sa_family == AF_LINK) {
2894 if (sa_dl_equal(ifma->ifma_addr, sa))
2897 if (sa_equal(ifma->ifma_addr, sa))
2906 * Allocate a new ifmultiaddr and initialize based on passed arguments. We
2907 * make copies of passed sockaddrs. The ifmultiaddr will not be added to
2908 * the ifnet multicast address list here, so the caller must do that and
2909 * other setup work (such as notifying the device driver). The reference
2910 * count is initialized to 1.
2912 static struct ifmultiaddr *
2913 if_allocmulti(struct ifnet *ifp, struct sockaddr *sa, struct sockaddr *llsa,
2916 struct ifmultiaddr *ifma;
2917 struct sockaddr *dupsa;
2919 ifma = malloc(sizeof *ifma, M_IFMADDR, mflags |
2924 dupsa = malloc(sa->sa_len, M_IFMADDR, mflags);
2925 if (dupsa == NULL) {
2926 free(ifma, M_IFMADDR);
2929 bcopy(sa, dupsa, sa->sa_len);
2930 ifma->ifma_addr = dupsa;
2932 ifma->ifma_ifp = ifp;
2933 ifma->ifma_refcount = 1;
2934 ifma->ifma_protospec = NULL;
2937 ifma->ifma_lladdr = NULL;
2941 dupsa = malloc(llsa->sa_len, M_IFMADDR, mflags);
2942 if (dupsa == NULL) {
2943 free(ifma->ifma_addr, M_IFMADDR);
2944 free(ifma, M_IFMADDR);
2947 bcopy(llsa, dupsa, llsa->sa_len);
2948 ifma->ifma_lladdr = dupsa;
2954 * if_freemulti: free ifmultiaddr structure and possibly attached related
2955 * addresses. The caller is responsible for implementing reference
2956 * counting, notifying the driver, handling routing messages, and releasing
2957 * any dependent link layer state.
2960 if_freemulti(struct ifmultiaddr *ifma)
2963 KASSERT(ifma->ifma_refcount == 0, ("if_freemulti: refcount %d",
2964 ifma->ifma_refcount));
2965 KASSERT(ifma->ifma_protospec == NULL,
2966 ("if_freemulti: protospec not NULL"));
2968 if (ifma->ifma_lladdr != NULL)
2969 free(ifma->ifma_lladdr, M_IFMADDR);
2970 free(ifma->ifma_addr, M_IFMADDR);
2971 free(ifma, M_IFMADDR);
2975 * Register an additional multicast address with a network interface.
2977 * - If the address is already present, bump the reference count on the
2978 * address and return.
2979 * - If the address is not link-layer, look up a link layer address.
2980 * - Allocate address structures for one or both addresses, and attach to the
2981 * multicast address list on the interface. If automatically adding a link
2982 * layer address, the protocol address will own a reference to the link
2983 * layer address, to be freed when it is freed.
2984 * - Notify the network device driver of an addition to the multicast address
2987 * 'sa' points to caller-owned memory with the desired multicast address.
2989 * 'retifma' will be used to return a pointer to the resulting multicast
2990 * address reference, if desired.
2993 if_addmulti(struct ifnet *ifp, struct sockaddr *sa,
2994 struct ifmultiaddr **retifma)
2996 struct ifmultiaddr *ifma, *ll_ifma;
2997 struct sockaddr *llsa;
3001 * If the address is already present, return a new reference to it;
3002 * otherwise, allocate storage and set up a new address.
3005 ifma = if_findmulti(ifp, sa);
3007 ifma->ifma_refcount++;
3008 if (retifma != NULL)
3010 IF_ADDR_WUNLOCK(ifp);
3015 * The address isn't already present; resolve the protocol address
3016 * into a link layer address, and then look that up, bump its
3017 * refcount or allocate an ifma for that also. If 'llsa' was
3018 * returned, we will need to free it later.
3022 if (ifp->if_resolvemulti != NULL) {
3023 error = ifp->if_resolvemulti(ifp, &llsa, sa);
3029 * Allocate the new address. Don't hook it up yet, as we may also
3030 * need to allocate a link layer multicast address.
3032 ifma = if_allocmulti(ifp, sa, llsa, M_NOWAIT);
3039 * If a link layer address is found, we'll need to see if it's
3040 * already present in the address list, or allocate is as well.
3041 * When this block finishes, the link layer address will be on the
3045 ll_ifma = if_findmulti(ifp, llsa);
3046 if (ll_ifma == NULL) {
3047 ll_ifma = if_allocmulti(ifp, llsa, NULL, M_NOWAIT);
3048 if (ll_ifma == NULL) {
3049 --ifma->ifma_refcount;
3054 TAILQ_INSERT_HEAD(&ifp->if_multiaddrs, ll_ifma,
3057 ll_ifma->ifma_refcount++;
3058 ifma->ifma_llifma = ll_ifma;
3062 * We now have a new multicast address, ifma, and possibly a new or
3063 * referenced link layer address. Add the primary address to the
3064 * ifnet address list.
3066 TAILQ_INSERT_HEAD(&ifp->if_multiaddrs, ifma, ifma_link);
3068 if (retifma != NULL)
3072 * Must generate the message while holding the lock so that 'ifma'
3073 * pointer is still valid.
3075 rt_newmaddrmsg(RTM_NEWMADDR, ifma);
3076 IF_ADDR_WUNLOCK(ifp);
3079 * We are certain we have added something, so call down to the
3080 * interface to let them know about it.
3082 if (ifp->if_ioctl != NULL) {
3083 (void) (*ifp->if_ioctl)(ifp, SIOCADDMULTI, 0);
3087 free(llsa, M_IFMADDR);
3093 free(llsa, M_IFMADDR);
3096 IF_ADDR_WUNLOCK(ifp);
3101 * Delete a multicast group membership by network-layer group address.
3103 * Returns ENOENT if the entry could not be found. If ifp no longer
3104 * exists, results are undefined. This entry point should only be used
3105 * from subsystems which do appropriate locking to hold ifp for the
3106 * duration of the call.
3107 * Network-layer protocol domains must use if_delmulti_ifma().
3110 if_delmulti(struct ifnet *ifp, struct sockaddr *sa)
3112 struct ifmultiaddr *ifma;
3117 IFNET_RLOCK_NOSLEEP();
3118 TAILQ_FOREACH(oifp, &V_ifnet, if_link)
3123 IFNET_RUNLOCK_NOSLEEP();
3125 KASSERT(ifp != NULL, ("%s: ifnet went away", __func__));
3132 ifma = if_findmulti(ifp, sa);
3134 lastref = if_delmulti_locked(ifp, ifma, 0);
3135 IF_ADDR_WUNLOCK(ifp);
3140 if (lastref && ifp->if_ioctl != NULL) {
3141 (void)(*ifp->if_ioctl)(ifp, SIOCDELMULTI, 0);
3148 * Delete all multicast group membership for an interface.
3149 * Should be used to quickly flush all multicast filters.
3152 if_delallmulti(struct ifnet *ifp)
3154 struct ifmultiaddr *ifma;
3155 struct ifmultiaddr *next;
3158 TAILQ_FOREACH_SAFE(ifma, &ifp->if_multiaddrs, ifma_link, next)
3159 if_delmulti_locked(ifp, ifma, 0);
3160 IF_ADDR_WUNLOCK(ifp);
3164 * Delete a multicast group membership by group membership pointer.
3165 * Network-layer protocol domains must use this routine.
3167 * It is safe to call this routine if the ifp disappeared.
3170 if_delmulti_ifma(struct ifmultiaddr *ifma)
3175 ifp = ifma->ifma_ifp;
3178 printf("%s: ifma_ifp seems to be detached\n", __func__);
3182 IFNET_RLOCK_NOSLEEP();
3183 TAILQ_FOREACH(oifp, &V_ifnet, if_link)
3187 printf("%s: ifnet %p disappeared\n", __func__, ifp);
3190 IFNET_RUNLOCK_NOSLEEP();
3194 * If and only if the ifnet instance exists: Acquire the address lock.
3199 lastref = if_delmulti_locked(ifp, ifma, 0);
3203 * If and only if the ifnet instance exists:
3204 * Release the address lock.
3205 * If the group was left: update the hardware hash filter.
3207 IF_ADDR_WUNLOCK(ifp);
3208 if (lastref && ifp->if_ioctl != NULL) {
3209 (void)(*ifp->if_ioctl)(ifp, SIOCDELMULTI, 0);
3215 * Perform deletion of network-layer and/or link-layer multicast address.
3217 * Return 0 if the reference count was decremented.
3218 * Return 1 if the final reference was released, indicating that the
3219 * hardware hash filter should be reprogrammed.
3222 if_delmulti_locked(struct ifnet *ifp, struct ifmultiaddr *ifma, int detaching)
3224 struct ifmultiaddr *ll_ifma;
3226 if (ifp != NULL && ifma->ifma_ifp != NULL) {
3227 KASSERT(ifma->ifma_ifp == ifp,
3228 ("%s: inconsistent ifp %p", __func__, ifp));
3229 IF_ADDR_WLOCK_ASSERT(ifp);
3232 ifp = ifma->ifma_ifp;
3235 * If the ifnet is detaching, null out references to ifnet,
3236 * so that upper protocol layers will notice, and not attempt
3237 * to obtain locks for an ifnet which no longer exists. The
3238 * routing socket announcement must happen before the ifnet
3239 * instance is detached from the system.
3243 printf("%s: detaching ifnet instance %p\n", __func__, ifp);
3246 * ifp may already be nulled out if we are being reentered
3247 * to delete the ll_ifma.
3250 rt_newmaddrmsg(RTM_DELMADDR, ifma);
3251 ifma->ifma_ifp = NULL;
3255 if (--ifma->ifma_refcount > 0)
3259 * If this ifma is a network-layer ifma, a link-layer ifma may
3260 * have been associated with it. Release it first if so.
3262 ll_ifma = ifma->ifma_llifma;
3263 if (ll_ifma != NULL) {
3264 KASSERT(ifma->ifma_lladdr != NULL,
3265 ("%s: llifma w/o lladdr", __func__));
3267 ll_ifma->ifma_ifp = NULL; /* XXX */
3268 if (--ll_ifma->ifma_refcount == 0) {
3270 TAILQ_REMOVE(&ifp->if_multiaddrs, ll_ifma,
3273 if_freemulti(ll_ifma);
3278 TAILQ_REMOVE(&ifp->if_multiaddrs, ifma, ifma_link);
3283 * The last reference to this instance of struct ifmultiaddr
3284 * was released; the hardware should be notified of this change.
3290 * Set the link layer address on an interface.
3292 * At this time we only support certain types of interfaces,
3293 * and we don't allow the length of the address to change.
3296 if_setlladdr(struct ifnet *ifp, const u_char *lladdr, int len)
3298 struct sockaddr_dl *sdl;
3305 IF_ADDR_RUNLOCK(ifp);
3309 IF_ADDR_RUNLOCK(ifp);
3310 sdl = (struct sockaddr_dl *)ifa->ifa_addr;
3315 if (len != sdl->sdl_alen) { /* don't allow length to change */
3319 switch (ifp->if_type) {
3327 case IFT_IEEE8023ADLAG:
3329 bcopy(lladdr, LLADDR(sdl), len);
3338 * If the interface is already up, we need
3339 * to re-init it in order to reprogram its
3342 if ((ifp->if_flags & IFF_UP) != 0) {
3343 if (ifp->if_ioctl) {
3344 ifp->if_flags &= ~IFF_UP;
3345 ifr.ifr_flags = ifp->if_flags & 0xffff;
3346 ifr.ifr_flagshigh = ifp->if_flags >> 16;
3347 (*ifp->if_ioctl)(ifp, SIOCSIFFLAGS, (caddr_t)&ifr);
3348 ifp->if_flags |= IFF_UP;
3349 ifr.ifr_flags = ifp->if_flags & 0xffff;
3350 ifr.ifr_flagshigh = ifp->if_flags >> 16;
3351 (*ifp->if_ioctl)(ifp, SIOCSIFFLAGS, (caddr_t)&ifr);
3355 * Also send gratuitous ARPs to notify other nodes about
3356 * the address change.
3358 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
3359 if (ifa->ifa_addr->sa_family == AF_INET)
3360 arp_ifinit(ifp, ifa);
3368 * The name argument must be a pointer to storage which will last as
3369 * long as the interface does. For physical devices, the result of
3370 * device_get_name(dev) is a good choice and for pseudo-devices a
3371 * static string works well.
3374 if_initname(struct ifnet *ifp, const char *name, int unit)
3376 ifp->if_dname = name;
3377 ifp->if_dunit = unit;
3378 if (unit != IF_DUNIT_NONE)
3379 snprintf(ifp->if_xname, IFNAMSIZ, "%s%d", name, unit);
3381 strlcpy(ifp->if_xname, name, IFNAMSIZ);
3385 if_printf(struct ifnet *ifp, const char * fmt, ...)
3390 retval = printf("%s: ", ifp->if_xname);
3392 retval += vprintf(fmt, ap);
3398 if_start(struct ifnet *ifp)
3401 (*(ifp)->if_start)(ifp);
3405 * Backwards compatibility interface for drivers
3406 * that have not implemented it
3409 if_transmit(struct ifnet *ifp, struct mbuf *m)
3413 IFQ_HANDOFF(ifp, m, error);
3418 if_handoff(struct ifqueue *ifq, struct mbuf *m, struct ifnet *ifp, int adjust)
3423 if (_IF_QFULL(ifq)) {
3430 ifp->if_obytes += m->m_pkthdr.len + adjust;
3431 if (m->m_flags & (M_BCAST|M_MCAST))
3433 active = ifp->if_drv_flags & IFF_DRV_OACTIVE;
3435 _IF_ENQUEUE(ifq, m);
3437 if (ifp != NULL && !active)
3438 (*(ifp)->if_start)(ifp);
3443 if_register_com_alloc(u_char type,
3444 if_com_alloc_t *a, if_com_free_t *f)
3447 KASSERT(if_com_alloc[type] == NULL,
3448 ("if_register_com_alloc: %d already registered", type));
3449 KASSERT(if_com_free[type] == NULL,
3450 ("if_register_com_alloc: %d free already registered", type));
3452 if_com_alloc[type] = a;
3453 if_com_free[type] = f;
3457 if_deregister_com_alloc(u_char type)
3460 KASSERT(if_com_alloc[type] != NULL,
3461 ("if_deregister_com_alloc: %d not registered", type));
3462 KASSERT(if_com_free[type] != NULL,
3463 ("if_deregister_com_alloc: %d free not registered", type));
3464 if_com_alloc[type] = NULL;
3465 if_com_free[type] = NULL;