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 void (*carp_demote_adj_p)(int, char *);
133 int (*carp_master_p)(struct ifaddr *);
134 #if defined(INET) || defined(INET6)
135 int (*carp_forus_p)(struct ifnet *ifp, u_char *dhost);
136 int (*carp_output_p)(struct ifnet *ifp, struct mbuf *m,
137 const struct sockaddr *sa);
138 int (*carp_ioctl_p)(struct ifreq *, u_long, struct thread *);
139 int (*carp_attach_p)(struct ifaddr *, int);
140 void (*carp_detach_p)(struct ifaddr *);
143 int (*carp_iamatch_p)(struct ifaddr *, uint8_t **);
146 struct ifaddr *(*carp_iamatch6_p)(struct ifnet *ifp, struct in6_addr *taddr6);
147 caddr_t (*carp_macmatch6_p)(struct ifnet *ifp, struct mbuf *m,
148 const struct in6_addr *taddr);
151 struct mbuf *(*tbr_dequeue_ptr)(struct ifaltq *, int) = NULL;
154 * XXX: Style; these should be sorted alphabetically, and unprototyped
155 * static functions should be prototyped. Currently they are sorted by
158 static void if_attachdomain(void *);
159 static void if_attachdomain1(struct ifnet *);
160 static int ifconf(u_long, caddr_t);
161 static void if_freemulti(struct ifmultiaddr *);
162 static void if_init(void *);
163 static void if_grow(void);
164 static void if_route(struct ifnet *, int flag, int fam);
165 static int if_setflag(struct ifnet *, int, int, int *, int);
166 static int if_transmit(struct ifnet *ifp, struct mbuf *m);
167 static void if_unroute(struct ifnet *, int flag, int fam);
168 static void link_rtrequest(int, struct rtentry *, struct rt_addrinfo *);
169 static int if_rtdel(struct radix_node *, void *);
170 static int ifhwioctl(u_long, struct ifnet *, caddr_t, struct thread *);
171 static int if_delmulti_locked(struct ifnet *, struct ifmultiaddr *, int);
172 static void do_link_state_change(void *, int);
173 static int if_getgroup(struct ifgroupreq *, struct ifnet *);
174 static int if_getgroupmembers(struct ifgroupreq *);
175 static void if_delgroups(struct ifnet *);
176 static void if_attach_internal(struct ifnet *, int);
177 static void if_detach_internal(struct ifnet *, int);
181 * XXX: declare here to avoid to include many inet6 related files..
182 * should be more generalized?
184 extern void nd6_setmtu(struct ifnet *);
187 VNET_DEFINE(int, if_index);
188 int ifqmaxlen = IFQ_MAXLEN;
189 VNET_DEFINE(struct ifnethead, ifnet); /* depend on static init XXX */
190 VNET_DEFINE(struct ifgrouphead, ifg_head);
192 static VNET_DEFINE(int, if_indexlim) = 8;
194 /* Table of ifnet by index. */
195 VNET_DEFINE(struct ifindex_entry *, ifindex_table);
197 #define V_if_indexlim VNET(if_indexlim)
198 #define V_ifindex_table VNET(ifindex_table)
201 * The global network interface list (V_ifnet) and related state (such as
202 * if_index, if_indexlim, and ifindex_table) are protected by an sxlock and
203 * an rwlock. Either may be acquired shared to stablize the list, but both
204 * must be acquired writable to modify the list. This model allows us to
205 * both stablize the interface list during interrupt thread processing, but
206 * also to stablize it over long-running ioctls, without introducing priority
207 * inversions and deadlocks.
209 struct rwlock ifnet_rwlock;
210 struct sx ifnet_sxlock;
213 * The allocation of network interfaces is a rather non-atomic affair; we
214 * need to select an index before we are ready to expose the interface for
215 * use, so will use this pointer value to indicate reservation.
217 #define IFNET_HOLD (void *)(uintptr_t)(-1)
219 static if_com_alloc_t *if_com_alloc[256];
220 static if_com_free_t *if_com_free[256];
222 static MALLOC_DEFINE(M_IFNET, "ifnet", "interface internals");
223 MALLOC_DEFINE(M_IFADDR, "ifaddr", "interface address");
224 MALLOC_DEFINE(M_IFMADDR, "ether_multi", "link-level multicast address");
227 ifnet_byindex_locked(u_short idx)
230 if (idx > V_if_index)
232 if (V_ifindex_table[idx].ife_ifnet == IFNET_HOLD)
234 return (V_ifindex_table[idx].ife_ifnet);
238 ifnet_byindex(u_short idx)
242 IFNET_RLOCK_NOSLEEP();
243 ifp = ifnet_byindex_locked(idx);
244 IFNET_RUNLOCK_NOSLEEP();
249 ifnet_byindex_ref(u_short idx)
253 IFNET_RLOCK_NOSLEEP();
254 ifp = ifnet_byindex_locked(idx);
255 if (ifp == NULL || (ifp->if_flags & IFF_DYING)) {
256 IFNET_RUNLOCK_NOSLEEP();
260 IFNET_RUNLOCK_NOSLEEP();
265 * Allocate an ifindex array entry; return 0 on success or an error on
269 ifindex_alloc_locked(u_short *idxp)
273 IFNET_WLOCK_ASSERT();
277 * Try to find an empty slot below V_if_index. If we fail, take the
280 for (idx = 1; idx <= V_if_index; idx++) {
281 if (V_ifindex_table[idx].ife_ifnet == NULL)
285 /* Catch if_index overflow. */
288 if (idx >= V_if_indexlim) {
292 if (idx > V_if_index)
299 ifindex_free_locked(u_short idx)
302 IFNET_WLOCK_ASSERT();
304 V_ifindex_table[idx].ife_ifnet = NULL;
305 while (V_if_index > 0 &&
306 V_ifindex_table[V_if_index].ife_ifnet == NULL)
311 ifindex_free(u_short idx)
315 ifindex_free_locked(idx);
320 ifnet_setbyindex_locked(u_short idx, struct ifnet *ifp)
323 IFNET_WLOCK_ASSERT();
325 V_ifindex_table[idx].ife_ifnet = ifp;
329 ifnet_setbyindex(u_short idx, struct ifnet *ifp)
333 ifnet_setbyindex_locked(idx, ifp);
338 ifaddr_byindex(u_short idx)
342 IFNET_RLOCK_NOSLEEP();
343 ifa = ifnet_byindex_locked(idx)->if_addr;
346 IFNET_RUNLOCK_NOSLEEP();
351 * Network interface utility routines.
353 * Routines with ifa_ifwith* names take sockaddr *'s as
358 vnet_if_init(const void *unused __unused)
361 TAILQ_INIT(&V_ifnet);
362 TAILQ_INIT(&V_ifg_head);
364 if_grow(); /* create initial table */
366 vnet_if_clone_init();
368 VNET_SYSINIT(vnet_if_init, SI_SUB_INIT_IF, SI_ORDER_SECOND, vnet_if_init,
373 if_init(void *dummy __unused)
379 SYSINIT(interfaces, SI_SUB_INIT_IF, SI_ORDER_FIRST, if_init, NULL);
384 vnet_if_uninit(const void *unused __unused)
387 VNET_ASSERT(TAILQ_EMPTY(&V_ifnet), ("%s:%d tailq &V_ifnet=%p "
388 "not empty", __func__, __LINE__, &V_ifnet));
389 VNET_ASSERT(TAILQ_EMPTY(&V_ifg_head), ("%s:%d tailq &V_ifg_head=%p "
390 "not empty", __func__, __LINE__, &V_ifg_head));
392 free((caddr_t)V_ifindex_table, M_IFNET);
394 VNET_SYSUNINIT(vnet_if_uninit, SI_SUB_INIT_IF, SI_ORDER_FIRST,
395 vnet_if_uninit, NULL);
403 struct ifindex_entry *e;
405 IFNET_WLOCK_ASSERT();
406 oldlim = V_if_indexlim;
408 n = (oldlim << 1) * sizeof(*e);
409 e = malloc(n, M_IFNET, M_WAITOK | M_ZERO);
411 if (V_if_indexlim != oldlim) {
415 if (V_ifindex_table != NULL) {
416 memcpy((caddr_t)e, (caddr_t)V_ifindex_table, n/2);
417 free((caddr_t)V_ifindex_table, M_IFNET);
424 * Allocate a struct ifnet and an index for an interface. A layer 2
425 * common structure will also be allocated if an allocation routine is
426 * registered for the passed type.
429 if_alloc(u_char type)
434 ifp = malloc(sizeof(struct ifnet), M_IFNET, M_WAITOK|M_ZERO);
436 if (ifindex_alloc_locked(&idx) != 0) {
441 ifnet_setbyindex_locked(idx, IFNET_HOLD);
445 ifp->if_alloctype = type;
446 if (if_com_alloc[type] != NULL) {
447 ifp->if_l2com = if_com_alloc[type](type, ifp);
448 if (ifp->if_l2com == NULL) {
455 IF_ADDR_LOCK_INIT(ifp);
456 TASK_INIT(&ifp->if_linktask, 0, do_link_state_change, ifp);
457 ifp->if_afdata_initialized = 0;
458 IF_AFDATA_LOCK_INIT(ifp);
459 TAILQ_INIT(&ifp->if_addrhead);
460 TAILQ_INIT(&ifp->if_multiaddrs);
461 TAILQ_INIT(&ifp->if_groups);
465 ifq_init(&ifp->if_snd, ifp);
467 refcount_init(&ifp->if_refcount, 1); /* Index reference. */
468 ifnet_setbyindex(ifp->if_index, ifp);
473 * Do the actual work of freeing a struct ifnet, and layer 2 common
474 * structure. This call is made when the last reference to an
475 * interface is released.
478 if_free_internal(struct ifnet *ifp)
481 KASSERT((ifp->if_flags & IFF_DYING),
482 ("if_free_internal: interface not dying"));
484 if (if_com_free[ifp->if_alloctype] != NULL)
485 if_com_free[ifp->if_alloctype](ifp->if_l2com,
489 mac_ifnet_destroy(ifp);
491 if (ifp->if_description != NULL)
492 free(ifp->if_description, M_IFDESCR);
493 IF_AFDATA_DESTROY(ifp);
494 IF_ADDR_LOCK_DESTROY(ifp);
495 ifq_delete(&ifp->if_snd);
500 * Deregister an interface and free the associated storage.
503 if_free(struct ifnet *ifp)
506 ifp->if_flags |= IFF_DYING; /* XXX: Locking */
508 CURVNET_SET_QUIET(ifp->if_vnet);
510 KASSERT(ifp == ifnet_byindex_locked(ifp->if_index),
511 ("%s: freeing unallocated ifnet", ifp->if_xname));
513 ifindex_free_locked(ifp->if_index);
516 if (refcount_release(&ifp->if_refcount))
517 if_free_internal(ifp);
522 * Interfaces to keep an ifnet type-stable despite the possibility of the
523 * driver calling if_free(). If there are additional references, we defer
524 * freeing the underlying data structure.
527 if_ref(struct ifnet *ifp)
530 /* We don't assert the ifnet list lock here, but arguably should. */
531 refcount_acquire(&ifp->if_refcount);
535 if_rele(struct ifnet *ifp)
538 if (!refcount_release(&ifp->if_refcount))
540 if_free_internal(ifp);
544 ifq_init(struct ifaltq *ifq, struct ifnet *ifp)
547 mtx_init(&ifq->ifq_mtx, ifp->if_xname, "if send queue", MTX_DEF);
549 if (ifq->ifq_maxlen == 0)
550 ifq->ifq_maxlen = ifqmaxlen;
553 ifq->altq_disc = NULL;
554 ifq->altq_flags &= ALTQF_CANTCHANGE;
555 ifq->altq_tbr = NULL;
560 ifq_delete(struct ifaltq *ifq)
562 mtx_destroy(&ifq->ifq_mtx);
566 * Perform generic interface initalization tasks and attach the interface
567 * to the list of "active" interfaces. If vmove flag is set on entry
568 * to if_attach_internal(), perform only a limited subset of initialization
569 * tasks, given that we are moving from one vnet to another an ifnet which
570 * has already been fully initialized.
573 * - The decision to return void and thus require this function to
574 * succeed is questionable.
575 * - We should probably do more sanity checking. For instance we don't
576 * do anything to insure if_xname is unique or non-empty.
579 if_attach(struct ifnet *ifp)
582 if_attach_internal(ifp, 0);
586 if_attach_internal(struct ifnet *ifp, int vmove)
588 unsigned socksize, ifasize;
589 int namelen, masklen;
590 struct sockaddr_dl *sdl;
593 if (ifp->if_index == 0 || ifp != ifnet_byindex(ifp->if_index))
594 panic ("%s: BUG: if_attach called without if_alloc'd input()\n",
598 ifp->if_vnet = curvnet;
599 if (ifp->if_home_vnet == NULL)
600 ifp->if_home_vnet = curvnet;
603 if_addgroup(ifp, IFG_ALL);
605 getmicrotime(&ifp->if_lastchange);
606 ifp->if_data.ifi_epoch = time_uptime;
607 ifp->if_data.ifi_datalen = sizeof(struct if_data);
609 KASSERT((ifp->if_transmit == NULL && ifp->if_qflush == NULL) ||
610 (ifp->if_transmit != NULL && ifp->if_qflush != NULL),
611 ("transmit and qflush must both either be set or both be NULL"));
612 if (ifp->if_transmit == NULL) {
613 ifp->if_transmit = if_transmit;
614 ifp->if_qflush = if_qflush;
619 mac_ifnet_create(ifp);
623 * Create a Link Level name for this device.
625 namelen = strlen(ifp->if_xname);
627 * Always save enough space for any possiable name so we
628 * can do a rename in place later.
630 masklen = offsetof(struct sockaddr_dl, sdl_data[0]) + IFNAMSIZ;
631 socksize = masklen + ifp->if_addrlen;
632 if (socksize < sizeof(*sdl))
633 socksize = sizeof(*sdl);
634 socksize = roundup2(socksize, sizeof(long));
635 ifasize = sizeof(*ifa) + 2 * socksize;
636 ifa = malloc(ifasize, M_IFADDR, M_WAITOK | M_ZERO);
638 sdl = (struct sockaddr_dl *)(ifa + 1);
639 sdl->sdl_len = socksize;
640 sdl->sdl_family = AF_LINK;
641 bcopy(ifp->if_xname, sdl->sdl_data, namelen);
642 sdl->sdl_nlen = namelen;
643 sdl->sdl_index = ifp->if_index;
644 sdl->sdl_type = ifp->if_type;
647 ifa->ifa_rtrequest = link_rtrequest;
648 ifa->ifa_addr = (struct sockaddr *)sdl;
649 sdl = (struct sockaddr_dl *)(socksize + (caddr_t)sdl);
650 ifa->ifa_netmask = (struct sockaddr *)sdl;
651 sdl->sdl_len = masklen;
653 sdl->sdl_data[--namelen] = 0xff;
654 TAILQ_INSERT_HEAD(&ifp->if_addrhead, ifa, ifa_link);
655 /* Reliably crash if used uninitialized. */
656 ifp->if_broadcastaddr = NULL;
658 #if defined(INET) || defined(INET6)
659 /* Initialize to max value. */
660 if (ifp->if_hw_tsomax == 0)
661 ifp->if_hw_tsomax = IP_MAXPACKET;
662 KASSERT(ifp->if_hw_tsomax <= IP_MAXPACKET &&
663 ifp->if_hw_tsomax >= IP_MAXPACKET / 8,
664 ("%s: tsomax outside of range", __func__));
670 * Update the interface index in the link layer address
673 for (ifa = ifp->if_addr; ifa != NULL;
674 ifa = TAILQ_NEXT(ifa, ifa_link)) {
675 if (ifa->ifa_addr->sa_family == AF_LINK) {
676 sdl = (struct sockaddr_dl *)ifa->ifa_addr;
677 sdl->sdl_index = ifp->if_index;
684 TAILQ_INSERT_TAIL(&V_ifnet, ifp, if_link);
686 curvnet->vnet_ifcnt++;
690 if (domain_init_status >= 2)
691 if_attachdomain1(ifp);
693 EVENTHANDLER_INVOKE(ifnet_arrival_event, ifp);
694 if (IS_DEFAULT_VNET(curvnet))
695 devctl_notify("IFNET", ifp->if_xname, "ATTACH", NULL);
697 /* Announce the interface. */
698 rt_ifannouncemsg(ifp, IFAN_ARRIVAL);
702 if_attachdomain(void *dummy)
706 TAILQ_FOREACH(ifp, &V_ifnet, if_link)
707 if_attachdomain1(ifp);
709 SYSINIT(domainifattach, SI_SUB_PROTO_IFATTACHDOMAIN, SI_ORDER_SECOND,
710 if_attachdomain, NULL);
713 if_attachdomain1(struct ifnet *ifp)
718 * Since dp->dom_ifattach calls malloc() with M_WAITOK, we
719 * cannot lock ifp->if_afdata initialization, entirely.
721 if (IF_AFDATA_TRYLOCK(ifp) == 0)
723 if (ifp->if_afdata_initialized >= domain_init_status) {
724 IF_AFDATA_UNLOCK(ifp);
725 log(LOG_WARNING, "%s called more than once on %s\n",
726 __func__, ifp->if_xname);
729 ifp->if_afdata_initialized = domain_init_status;
730 IF_AFDATA_UNLOCK(ifp);
732 /* address family dependent data region */
733 bzero(ifp->if_afdata, sizeof(ifp->if_afdata));
734 for (dp = domains; dp; dp = dp->dom_next) {
735 if (dp->dom_ifattach)
736 ifp->if_afdata[dp->dom_family] =
737 (*dp->dom_ifattach)(ifp);
742 * Remove any unicast or broadcast network addresses from an interface.
745 if_purgeaddrs(struct ifnet *ifp)
747 struct ifaddr *ifa, *next;
749 TAILQ_FOREACH_SAFE(ifa, &ifp->if_addrhead, ifa_link, next) {
750 if (ifa->ifa_addr->sa_family == AF_LINK)
753 /* XXX: Ugly!! ad hoc just for INET */
754 if (ifa->ifa_addr->sa_family == AF_INET) {
755 struct ifaliasreq ifr;
757 bzero(&ifr, sizeof(ifr));
758 ifr.ifra_addr = *ifa->ifa_addr;
759 if (ifa->ifa_dstaddr)
760 ifr.ifra_broadaddr = *ifa->ifa_dstaddr;
761 if (in_control(NULL, SIOCDIFADDR, (caddr_t)&ifr, ifp,
767 if (ifa->ifa_addr->sa_family == AF_INET6) {
769 /* ifp_addrhead is already updated */
773 TAILQ_REMOVE(&ifp->if_addrhead, ifa, ifa_link);
779 * Remove any multicast network addresses from an interface when an ifnet
783 if_purgemaddrs(struct ifnet *ifp)
785 struct ifmultiaddr *ifma;
786 struct ifmultiaddr *next;
789 TAILQ_FOREACH_SAFE(ifma, &ifp->if_multiaddrs, ifma_link, next)
790 if_delmulti_locked(ifp, ifma, 1);
791 IF_ADDR_WUNLOCK(ifp);
795 * Detach an interface, removing it from the list of "active" interfaces.
796 * If vmove flag is set on entry to if_detach_internal(), perform only a
797 * limited subset of cleanup tasks, given that we are moving an ifnet from
798 * one vnet to another, where it must be fully operational.
800 * XXXRW: There are some significant questions about event ordering, and
801 * how to prevent things from starting to use the interface during detach.
804 if_detach(struct ifnet *ifp)
807 CURVNET_SET_QUIET(ifp->if_vnet);
808 if_detach_internal(ifp, 0);
813 if_detach_internal(struct ifnet *ifp, int vmove)
816 struct radix_node_head *rnh;
823 TAILQ_FOREACH(iter, &V_ifnet, if_link)
825 TAILQ_REMOVE(&V_ifnet, ifp, if_link);
831 curvnet->vnet_ifcnt--;
836 panic("%s: ifp=%p not on the ifnet tailq %p",
837 __func__, ifp, &V_ifnet);
839 return; /* XXX this should panic as well? */
843 * Remove/wait for pending events.
845 taskqueue_drain(taskqueue_swi, &ifp->if_linktask);
848 * Remove routes and flush queues.
852 if (ALTQ_IS_ENABLED(&ifp->if_snd))
853 altq_disable(&ifp->if_snd);
854 if (ALTQ_IS_ATTACHED(&ifp->if_snd))
855 altq_detach(&ifp->if_snd);
866 * Remove all IPv6 kernel structs related to ifp. This should be done
867 * before removing routing entries below, since IPv6 interface direct
868 * routes are expected to be removed by the IPv6-specific kernel API.
869 * Otherwise, the kernel will detect some inconsistency and bark it.
877 * Prevent further calls into the device driver via ifnet.
882 * Remove link ifaddr pointer and maybe decrement if_index.
883 * Clean up all addresses.
887 /* We can now free link ifaddr. */
888 if (!TAILQ_EMPTY(&ifp->if_addrhead)) {
889 ifa = TAILQ_FIRST(&ifp->if_addrhead);
890 TAILQ_REMOVE(&ifp->if_addrhead, ifa, ifa_link);
896 * Delete all remaining routes using this interface
897 * Unfortuneatly the only way to do this is to slog through
898 * the entire routing table looking for routes which point
899 * to this interface...oh well...
901 for (i = 1; i <= AF_MAX; i++) {
902 for (j = 0; j < rt_numfibs; j++) {
903 rnh = rt_tables_get_rnh(j, i);
906 RADIX_NODE_HEAD_LOCK(rnh);
907 (void) rnh->rnh_walktree(rnh, if_rtdel, ifp);
908 RADIX_NODE_HEAD_UNLOCK(rnh);
912 /* Announce that the interface is gone. */
913 rt_ifannouncemsg(ifp, IFAN_DEPARTURE);
914 EVENTHANDLER_INVOKE(ifnet_departure_event, ifp);
915 if (IS_DEFAULT_VNET(curvnet))
916 devctl_notify("IFNET", ifp->if_xname, "DETACH", NULL);
920 * We cannot hold the lock over dom_ifdetach calls as they might
921 * sleep, for example trying to drain a callout, thus open up the
922 * theoretical race with re-attaching.
925 i = ifp->if_afdata_initialized;
926 ifp->if_afdata_initialized = 0;
927 IF_AFDATA_UNLOCK(ifp);
928 for (dp = domains; i > 0 && dp; dp = dp->dom_next) {
929 if (dp->dom_ifdetach && ifp->if_afdata[dp->dom_family])
930 (*dp->dom_ifdetach)(ifp,
931 ifp->if_afdata[dp->dom_family]);
937 * if_vmove() performs a limited version of if_detach() in current
938 * vnet and if_attach()es the ifnet to the vnet specified as 2nd arg.
939 * An attempt is made to shrink if_index in current vnet, find an
940 * unused if_index in target vnet and calls if_grow() if necessary,
941 * and finally find an unused if_xname for the target vnet.
944 if_vmove(struct ifnet *ifp, struct vnet *new_vnet)
949 * Detach from current vnet, but preserve LLADDR info, do not
950 * mark as dead etc. so that the ifnet can be reattached later.
952 if_detach_internal(ifp, 1);
955 * Unlink the ifnet from ifindex_table[] in current vnet, and shrink
956 * the if_index for that vnet if possible.
958 * NOTE: IFNET_WLOCK/IFNET_WUNLOCK() are assumed to be unvirtualized,
959 * or we'd lock on one vnet and unlock on another.
962 ifindex_free_locked(ifp->if_index);
966 * Perform interface-specific reassignment tasks, if provided by
969 if (ifp->if_reassign != NULL)
970 ifp->if_reassign(ifp, new_vnet, NULL);
973 * Switch to the context of the target vnet.
975 CURVNET_SET_QUIET(new_vnet);
978 if (ifindex_alloc_locked(&idx) != 0) {
980 panic("if_index overflow");
983 ifnet_setbyindex_locked(ifp->if_index, ifp);
986 if_attach_internal(ifp, 1);
992 * Move an ifnet to or from another child prison/vnet, specified by the jail id.
995 if_vmove_loan(struct thread *td, struct ifnet *ifp, char *ifname, int jid)
1000 /* Try to find the prison within our visibility. */
1001 sx_slock(&allprison_lock);
1002 pr = prison_find_child(td->td_ucred->cr_prison, jid);
1003 sx_sunlock(&allprison_lock);
1006 prison_hold_locked(pr);
1007 mtx_unlock(&pr->pr_mtx);
1009 /* Do not try to move the iface from and to the same prison. */
1010 if (pr->pr_vnet == ifp->if_vnet) {
1015 /* Make sure the named iface does not exists in the dst. prison/vnet. */
1016 /* XXX Lock interfaces to avoid races. */
1017 CURVNET_SET_QUIET(pr->pr_vnet);
1018 difp = ifunit(ifname);
1025 /* Move the interface into the child jail/vnet. */
1026 if_vmove(ifp, pr->pr_vnet);
1028 /* Report the new if_xname back to the userland. */
1029 sprintf(ifname, "%s", ifp->if_xname);
1036 if_vmove_reclaim(struct thread *td, char *ifname, int jid)
1039 struct vnet *vnet_dst;
1042 /* Try to find the prison within our visibility. */
1043 sx_slock(&allprison_lock);
1044 pr = prison_find_child(td->td_ucred->cr_prison, jid);
1045 sx_sunlock(&allprison_lock);
1048 prison_hold_locked(pr);
1049 mtx_unlock(&pr->pr_mtx);
1051 /* Make sure the named iface exists in the source prison/vnet. */
1052 CURVNET_SET(pr->pr_vnet);
1053 ifp = ifunit(ifname); /* XXX Lock to avoid races. */
1060 /* Do not try to move the iface from and to the same prison. */
1061 vnet_dst = TD_TO_VNET(td);
1062 if (vnet_dst == ifp->if_vnet) {
1068 /* Get interface back from child jail/vnet. */
1069 if_vmove(ifp, vnet_dst);
1072 /* Report the new if_xname back to the userland. */
1073 sprintf(ifname, "%s", ifp->if_xname);
1081 * Add a group to an interface
1084 if_addgroup(struct ifnet *ifp, const char *groupname)
1086 struct ifg_list *ifgl;
1087 struct ifg_group *ifg = NULL;
1088 struct ifg_member *ifgm;
1091 if (groupname[0] && groupname[strlen(groupname) - 1] >= '0' &&
1092 groupname[strlen(groupname) - 1] <= '9')
1096 TAILQ_FOREACH(ifgl, &ifp->if_groups, ifgl_next)
1097 if (!strcmp(ifgl->ifgl_group->ifg_group, groupname)) {
1102 if ((ifgl = (struct ifg_list *)malloc(sizeof(struct ifg_list), M_TEMP,
1103 M_NOWAIT)) == NULL) {
1108 if ((ifgm = (struct ifg_member *)malloc(sizeof(struct ifg_member),
1109 M_TEMP, M_NOWAIT)) == NULL) {
1115 TAILQ_FOREACH(ifg, &V_ifg_head, ifg_next)
1116 if (!strcmp(ifg->ifg_group, groupname))
1120 if ((ifg = (struct ifg_group *)malloc(sizeof(struct ifg_group),
1121 M_TEMP, M_NOWAIT)) == NULL) {
1127 strlcpy(ifg->ifg_group, groupname, sizeof(ifg->ifg_group));
1128 ifg->ifg_refcnt = 0;
1129 TAILQ_INIT(&ifg->ifg_members);
1130 TAILQ_INSERT_TAIL(&V_ifg_head, ifg, ifg_next);
1135 ifgl->ifgl_group = ifg;
1136 ifgm->ifgm_ifp = ifp;
1139 TAILQ_INSERT_TAIL(&ifg->ifg_members, ifgm, ifgm_next);
1140 TAILQ_INSERT_TAIL(&ifp->if_groups, ifgl, ifgl_next);
1141 IF_ADDR_WUNLOCK(ifp);
1146 EVENTHANDLER_INVOKE(group_attach_event, ifg);
1147 EVENTHANDLER_INVOKE(group_change_event, groupname);
1153 * Remove a group from an interface
1156 if_delgroup(struct ifnet *ifp, const char *groupname)
1158 struct ifg_list *ifgl;
1159 struct ifg_member *ifgm;
1162 TAILQ_FOREACH(ifgl, &ifp->if_groups, ifgl_next)
1163 if (!strcmp(ifgl->ifgl_group->ifg_group, groupname))
1171 TAILQ_REMOVE(&ifp->if_groups, ifgl, ifgl_next);
1172 IF_ADDR_WUNLOCK(ifp);
1174 TAILQ_FOREACH(ifgm, &ifgl->ifgl_group->ifg_members, ifgm_next)
1175 if (ifgm->ifgm_ifp == ifp)
1179 TAILQ_REMOVE(&ifgl->ifgl_group->ifg_members, ifgm, ifgm_next);
1183 if (--ifgl->ifgl_group->ifg_refcnt == 0) {
1184 TAILQ_REMOVE(&V_ifg_head, ifgl->ifgl_group, ifg_next);
1186 EVENTHANDLER_INVOKE(group_detach_event, ifgl->ifgl_group);
1187 free(ifgl->ifgl_group, M_TEMP);
1193 EVENTHANDLER_INVOKE(group_change_event, groupname);
1199 * Remove an interface from all groups
1202 if_delgroups(struct ifnet *ifp)
1204 struct ifg_list *ifgl;
1205 struct ifg_member *ifgm;
1206 char groupname[IFNAMSIZ];
1209 while (!TAILQ_EMPTY(&ifp->if_groups)) {
1210 ifgl = TAILQ_FIRST(&ifp->if_groups);
1212 strlcpy(groupname, ifgl->ifgl_group->ifg_group, IFNAMSIZ);
1215 TAILQ_REMOVE(&ifp->if_groups, ifgl, ifgl_next);
1216 IF_ADDR_WUNLOCK(ifp);
1218 TAILQ_FOREACH(ifgm, &ifgl->ifgl_group->ifg_members, ifgm_next)
1219 if (ifgm->ifgm_ifp == ifp)
1223 TAILQ_REMOVE(&ifgl->ifgl_group->ifg_members, ifgm,
1228 if (--ifgl->ifgl_group->ifg_refcnt == 0) {
1229 TAILQ_REMOVE(&V_ifg_head, ifgl->ifgl_group, ifg_next);
1231 EVENTHANDLER_INVOKE(group_detach_event,
1233 free(ifgl->ifgl_group, M_TEMP);
1239 EVENTHANDLER_INVOKE(group_change_event, groupname);
1247 * Stores all groups from an interface in memory pointed
1251 if_getgroup(struct ifgroupreq *data, struct ifnet *ifp)
1254 struct ifg_list *ifgl;
1255 struct ifg_req ifgrq, *ifgp;
1256 struct ifgroupreq *ifgr = data;
1258 if (ifgr->ifgr_len == 0) {
1260 TAILQ_FOREACH(ifgl, &ifp->if_groups, ifgl_next)
1261 ifgr->ifgr_len += sizeof(struct ifg_req);
1262 IF_ADDR_RUNLOCK(ifp);
1266 len = ifgr->ifgr_len;
1267 ifgp = ifgr->ifgr_groups;
1270 TAILQ_FOREACH(ifgl, &ifp->if_groups, ifgl_next) {
1271 if (len < sizeof(ifgrq)) {
1272 IF_ADDR_RUNLOCK(ifp);
1275 bzero(&ifgrq, sizeof ifgrq);
1276 strlcpy(ifgrq.ifgrq_group, ifgl->ifgl_group->ifg_group,
1277 sizeof(ifgrq.ifgrq_group));
1278 if ((error = copyout(&ifgrq, ifgp, sizeof(struct ifg_req)))) {
1279 IF_ADDR_RUNLOCK(ifp);
1282 len -= sizeof(ifgrq);
1285 IF_ADDR_RUNLOCK(ifp);
1291 * Stores all members of a group in memory pointed to by data
1294 if_getgroupmembers(struct ifgroupreq *data)
1296 struct ifgroupreq *ifgr = data;
1297 struct ifg_group *ifg;
1298 struct ifg_member *ifgm;
1299 struct ifg_req ifgrq, *ifgp;
1303 TAILQ_FOREACH(ifg, &V_ifg_head, ifg_next)
1304 if (!strcmp(ifg->ifg_group, ifgr->ifgr_name))
1311 if (ifgr->ifgr_len == 0) {
1312 TAILQ_FOREACH(ifgm, &ifg->ifg_members, ifgm_next)
1313 ifgr->ifgr_len += sizeof(ifgrq);
1318 len = ifgr->ifgr_len;
1319 ifgp = ifgr->ifgr_groups;
1320 TAILQ_FOREACH(ifgm, &ifg->ifg_members, ifgm_next) {
1321 if (len < sizeof(ifgrq)) {
1325 bzero(&ifgrq, sizeof ifgrq);
1326 strlcpy(ifgrq.ifgrq_member, ifgm->ifgm_ifp->if_xname,
1327 sizeof(ifgrq.ifgrq_member));
1328 if ((error = copyout(&ifgrq, ifgp, sizeof(struct ifg_req)))) {
1332 len -= sizeof(ifgrq);
1341 * Delete Routes for a Network Interface
1343 * Called for each routing entry via the rnh->rnh_walktree() call above
1344 * to delete all route entries referencing a detaching network interface.
1347 * rn pointer to node in the routing table
1348 * arg argument passed to rnh->rnh_walktree() - detaching interface
1352 * errno failed - reason indicated
1356 if_rtdel(struct radix_node *rn, void *arg)
1358 struct rtentry *rt = (struct rtentry *)rn;
1359 struct ifnet *ifp = arg;
1362 if (rt->rt_ifp == ifp) {
1365 * Protect (sorta) against walktree recursion problems
1366 * with cloned routes
1368 if ((rt->rt_flags & RTF_UP) == 0)
1371 err = rtrequest_fib(RTM_DELETE, rt_key(rt), rt->rt_gateway,
1373 rt->rt_flags|RTF_RNH_LOCKED|RTF_PINNED,
1374 (struct rtentry **) NULL, rt->rt_fibnum);
1376 log(LOG_WARNING, "if_rtdel: error %d\n", err);
1384 * Wrapper functions for struct ifnet address list locking macros. These are
1385 * used by kernel modules to avoid encoding programming interface or binary
1386 * interface assumptions that may be violated when kernel-internal locking
1387 * approaches change.
1390 if_addr_rlock(struct ifnet *ifp)
1397 if_addr_runlock(struct ifnet *ifp)
1400 IF_ADDR_RUNLOCK(ifp);
1404 if_maddr_rlock(struct ifnet *ifp)
1411 if_maddr_runlock(struct ifnet *ifp)
1414 IF_ADDR_RUNLOCK(ifp);
1418 * Initialization, destruction and refcounting functions for ifaddrs.
1421 ifa_init(struct ifaddr *ifa)
1424 mtx_init(&ifa->ifa_mtx, "ifaddr", NULL, MTX_DEF);
1425 refcount_init(&ifa->ifa_refcnt, 1);
1426 ifa->if_data.ifi_datalen = sizeof(ifa->if_data);
1430 ifa_ref(struct ifaddr *ifa)
1433 refcount_acquire(&ifa->ifa_refcnt);
1437 ifa_free(struct ifaddr *ifa)
1440 if (refcount_release(&ifa->ifa_refcnt)) {
1441 mtx_destroy(&ifa->ifa_mtx);
1442 free(ifa, M_IFADDR);
1447 ifa_add_loopback_route(struct ifaddr *ifa, struct sockaddr *ia)
1450 struct rtentry *rt = NULL;
1451 struct rt_addrinfo info;
1452 static struct sockaddr_dl null_sdl = {sizeof(null_sdl), AF_LINK};
1454 bzero(&info, sizeof(info));
1455 info.rti_ifp = V_loif;
1456 info.rti_flags = ifa->ifa_flags | RTF_HOST | RTF_STATIC;
1457 info.rti_info[RTAX_DST] = ia;
1458 info.rti_info[RTAX_GATEWAY] = (struct sockaddr *)&null_sdl;
1459 error = rtrequest1_fib(RTM_ADD, &info, &rt, 0);
1461 if (error == 0 && rt != NULL) {
1463 ((struct sockaddr_dl *)rt->rt_gateway)->sdl_type =
1464 ifa->ifa_ifp->if_type;
1465 ((struct sockaddr_dl *)rt->rt_gateway)->sdl_index =
1466 ifa->ifa_ifp->if_index;
1469 } else if (error != 0)
1470 log(LOG_DEBUG, "%s: insertion failed: %u\n", __func__, error);
1476 ifa_del_loopback_route(struct ifaddr *ifa, struct sockaddr *ia)
1479 struct rt_addrinfo info;
1480 struct sockaddr_dl null_sdl;
1482 bzero(&null_sdl, sizeof(null_sdl));
1483 null_sdl.sdl_len = sizeof(null_sdl);
1484 null_sdl.sdl_family = AF_LINK;
1485 null_sdl.sdl_type = ifa->ifa_ifp->if_type;
1486 null_sdl.sdl_index = ifa->ifa_ifp->if_index;
1487 bzero(&info, sizeof(info));
1488 info.rti_flags = ifa->ifa_flags | RTF_HOST | RTF_STATIC;
1489 info.rti_info[RTAX_DST] = ia;
1490 info.rti_info[RTAX_GATEWAY] = (struct sockaddr *)&null_sdl;
1491 error = rtrequest1_fib(RTM_DELETE, &info, NULL, 0);
1494 log(LOG_DEBUG, "%s: deletion failed: %u\n", __func__, error);
1500 * XXX: Because sockaddr_dl has deeper structure than the sockaddr
1501 * structs used to represent other address families, it is necessary
1502 * to perform a different comparison.
1505 #define sa_equal(a1, a2) \
1506 (bcmp((a1), (a2), ((a1))->sa_len) == 0)
1508 #define sa_dl_equal(a1, a2) \
1509 ((((struct sockaddr_dl *)(a1))->sdl_len == \
1510 ((struct sockaddr_dl *)(a2))->sdl_len) && \
1511 (bcmp(LLADDR((struct sockaddr_dl *)(a1)), \
1512 LLADDR((struct sockaddr_dl *)(a2)), \
1513 ((struct sockaddr_dl *)(a1))->sdl_alen) == 0))
1516 * Locate an interface based on a complete address.
1519 static struct ifaddr *
1520 ifa_ifwithaddr_internal(struct sockaddr *addr, int getref)
1525 IFNET_RLOCK_NOSLEEP();
1526 TAILQ_FOREACH(ifp, &V_ifnet, if_link) {
1528 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
1529 if (ifa->ifa_addr->sa_family != addr->sa_family)
1531 if (sa_equal(addr, ifa->ifa_addr)) {
1534 IF_ADDR_RUNLOCK(ifp);
1537 /* IP6 doesn't have broadcast */
1538 if ((ifp->if_flags & IFF_BROADCAST) &&
1539 ifa->ifa_broadaddr &&
1540 ifa->ifa_broadaddr->sa_len != 0 &&
1541 sa_equal(ifa->ifa_broadaddr, addr)) {
1544 IF_ADDR_RUNLOCK(ifp);
1548 IF_ADDR_RUNLOCK(ifp);
1552 IFNET_RUNLOCK_NOSLEEP();
1557 ifa_ifwithaddr(struct sockaddr *addr)
1560 return (ifa_ifwithaddr_internal(addr, 1));
1564 ifa_ifwithaddr_check(struct sockaddr *addr)
1567 return (ifa_ifwithaddr_internal(addr, 0) != NULL);
1571 * Locate an interface based on the broadcast address.
1575 ifa_ifwithbroadaddr(struct sockaddr *addr)
1580 IFNET_RLOCK_NOSLEEP();
1581 TAILQ_FOREACH(ifp, &V_ifnet, if_link) {
1583 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
1584 if (ifa->ifa_addr->sa_family != addr->sa_family)
1586 if ((ifp->if_flags & IFF_BROADCAST) &&
1587 ifa->ifa_broadaddr &&
1588 ifa->ifa_broadaddr->sa_len != 0 &&
1589 sa_equal(ifa->ifa_broadaddr, addr)) {
1591 IF_ADDR_RUNLOCK(ifp);
1595 IF_ADDR_RUNLOCK(ifp);
1599 IFNET_RUNLOCK_NOSLEEP();
1604 * Locate the point to point interface with a given destination address.
1608 ifa_ifwithdstaddr(struct sockaddr *addr)
1613 IFNET_RLOCK_NOSLEEP();
1614 TAILQ_FOREACH(ifp, &V_ifnet, if_link) {
1615 if ((ifp->if_flags & IFF_POINTOPOINT) == 0)
1618 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
1619 if (ifa->ifa_addr->sa_family != addr->sa_family)
1621 if (ifa->ifa_dstaddr != NULL &&
1622 sa_equal(addr, ifa->ifa_dstaddr)) {
1624 IF_ADDR_RUNLOCK(ifp);
1628 IF_ADDR_RUNLOCK(ifp);
1632 IFNET_RUNLOCK_NOSLEEP();
1637 * Find an interface on a specific network. If many, choice
1638 * is most specific found.
1641 ifa_ifwithnet(struct sockaddr *addr, int ignore_ptp)
1645 struct ifaddr *ifa_maybe = NULL;
1646 u_int af = addr->sa_family;
1647 char *addr_data = addr->sa_data, *cplim;
1650 * AF_LINK addresses can be looked up directly by their index number,
1651 * so do that if we can.
1653 if (af == AF_LINK) {
1654 struct sockaddr_dl *sdl = (struct sockaddr_dl *)addr;
1655 if (sdl->sdl_index && sdl->sdl_index <= V_if_index)
1656 return (ifaddr_byindex(sdl->sdl_index));
1660 * Scan though each interface, looking for ones that have addresses
1661 * in this address family. Maintain a reference on ifa_maybe once
1662 * we find one, as we release the IF_ADDR_RLOCK() that kept it stable
1663 * when we move onto the next interface.
1665 IFNET_RLOCK_NOSLEEP();
1666 TAILQ_FOREACH(ifp, &V_ifnet, if_link) {
1668 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
1669 char *cp, *cp2, *cp3;
1671 if (ifa->ifa_addr->sa_family != af)
1673 if (af == AF_INET &&
1674 ifp->if_flags & IFF_POINTOPOINT && !ignore_ptp) {
1676 * This is a bit broken as it doesn't
1677 * take into account that the remote end may
1678 * be a single node in the network we are
1680 * The trouble is that we don't know the
1681 * netmask for the remote end.
1683 if (ifa->ifa_dstaddr != NULL &&
1684 sa_equal(addr, ifa->ifa_dstaddr)) {
1686 IF_ADDR_RUNLOCK(ifp);
1691 * if we have a special address handler,
1692 * then use it instead of the generic one.
1694 if (ifa->ifa_claim_addr) {
1695 if ((*ifa->ifa_claim_addr)(ifa, addr)) {
1697 IF_ADDR_RUNLOCK(ifp);
1704 * Scan all the bits in the ifa's address.
1705 * If a bit dissagrees with what we are
1706 * looking for, mask it with the netmask
1707 * to see if it really matters.
1708 * (A byte at a time)
1710 if (ifa->ifa_netmask == 0)
1713 cp2 = ifa->ifa_addr->sa_data;
1714 cp3 = ifa->ifa_netmask->sa_data;
1715 cplim = ifa->ifa_netmask->sa_len
1716 + (char *)ifa->ifa_netmask;
1718 if ((*cp++ ^ *cp2++) & *cp3++)
1719 goto next; /* next address! */
1721 * If the netmask of what we just found
1722 * is more specific than what we had before
1723 * (if we had one), or if the virtual status
1724 * of new prefix is better than of the old one,
1725 * then remember the new one before continuing
1726 * to search for an even better one.
1728 if (ifa_maybe == NULL ||
1729 ifa_preferred(ifa_maybe, ifa) ||
1730 rn_refines((caddr_t)ifa->ifa_netmask,
1731 (caddr_t)ifa_maybe->ifa_netmask)) {
1732 if (ifa_maybe != NULL)
1733 ifa_free(ifa_maybe);
1739 IF_ADDR_RUNLOCK(ifp);
1744 IFNET_RUNLOCK_NOSLEEP();
1745 if (ifa_maybe != NULL)
1746 ifa_free(ifa_maybe);
1751 * Find an interface address specific to an interface best matching
1755 ifaof_ifpforaddr(struct sockaddr *addr, struct ifnet *ifp)
1758 char *cp, *cp2, *cp3;
1760 struct ifaddr *ifa_maybe = NULL;
1761 u_int af = addr->sa_family;
1766 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
1767 if (ifa->ifa_addr->sa_family != af)
1769 if (ifa_maybe == NULL)
1771 if (ifa->ifa_netmask == 0) {
1772 if (sa_equal(addr, ifa->ifa_addr) ||
1773 (ifa->ifa_dstaddr &&
1774 sa_equal(addr, ifa->ifa_dstaddr)))
1778 if (ifp->if_flags & IFF_POINTOPOINT) {
1779 if (sa_equal(addr, ifa->ifa_dstaddr))
1783 cp2 = ifa->ifa_addr->sa_data;
1784 cp3 = ifa->ifa_netmask->sa_data;
1785 cplim = ifa->ifa_netmask->sa_len + (char *)ifa->ifa_netmask;
1786 for (; cp3 < cplim; cp3++)
1787 if ((*cp++ ^ *cp2++) & *cp3)
1797 IF_ADDR_RUNLOCK(ifp);
1802 * See whether new ifa is better than current one:
1803 * 1) A non-virtual one is preferred over virtual.
1804 * 2) A virtual in master state preferred over any other state.
1806 * Used in several address selecting functions.
1809 ifa_preferred(struct ifaddr *cur, struct ifaddr *next)
1812 return (cur->ifa_carp && (!next->ifa_carp ||
1813 ((*carp_master_p)(next) && !(*carp_master_p)(cur))));
1816 #include <net/if_llatbl.h>
1819 * Default action when installing a route with a Link Level gateway.
1820 * Lookup an appropriate real ifa to point to.
1821 * This should be moved to /sys/net/link.c eventually.
1824 link_rtrequest(int cmd, struct rtentry *rt, struct rt_addrinfo *info)
1826 struct ifaddr *ifa, *oifa;
1827 struct sockaddr *dst;
1832 if (cmd != RTM_ADD || ((ifa = rt->rt_ifa) == 0) ||
1833 ((ifp = ifa->ifa_ifp) == 0) || ((dst = rt_key(rt)) == 0))
1835 ifa = ifaof_ifpforaddr(dst, ifp);
1840 if (ifa->ifa_rtrequest && ifa->ifa_rtrequest != link_rtrequest)
1841 ifa->ifa_rtrequest(cmd, rt, info);
1846 * Mark an interface down and notify protocols of
1850 if_unroute(struct ifnet *ifp, int flag, int fam)
1854 KASSERT(flag == IFF_UP, ("if_unroute: flag != IFF_UP"));
1856 ifp->if_flags &= ~flag;
1857 getmicrotime(&ifp->if_lastchange);
1858 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link)
1859 if (fam == PF_UNSPEC || (fam == ifa->ifa_addr->sa_family))
1860 pfctlinput(PRC_IFDOWN, ifa->ifa_addr);
1861 ifp->if_qflush(ifp);
1864 (*carp_linkstate_p)(ifp);
1869 * Mark an interface up and notify protocols of
1873 if_route(struct ifnet *ifp, int flag, int fam)
1877 KASSERT(flag == IFF_UP, ("if_route: flag != IFF_UP"));
1879 ifp->if_flags |= flag;
1880 getmicrotime(&ifp->if_lastchange);
1881 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link)
1882 if (fam == PF_UNSPEC || (fam == ifa->ifa_addr->sa_family))
1883 pfctlinput(PRC_IFUP, ifa->ifa_addr);
1885 (*carp_linkstate_p)(ifp);
1892 void (*vlan_link_state_p)(struct ifnet *); /* XXX: private from if_vlan */
1893 void (*vlan_trunk_cap_p)(struct ifnet *); /* XXX: private from if_vlan */
1894 struct ifnet *(*vlan_trunkdev_p)(struct ifnet *);
1895 struct ifnet *(*vlan_devat_p)(struct ifnet *, uint16_t);
1896 int (*vlan_tag_p)(struct ifnet *, uint16_t *);
1897 int (*vlan_setcookie_p)(struct ifnet *, void *);
1898 void *(*vlan_cookie_p)(struct ifnet *);
1901 * Handle a change in the interface link state. To avoid LORs
1902 * between driver lock and upper layer locks, as well as possible
1903 * recursions, we post event to taskqueue, and all job
1904 * is done in static do_link_state_change().
1907 if_link_state_change(struct ifnet *ifp, int link_state)
1909 /* Return if state hasn't changed. */
1910 if (ifp->if_link_state == link_state)
1913 ifp->if_link_state = link_state;
1915 taskqueue_enqueue(taskqueue_swi, &ifp->if_linktask);
1919 do_link_state_change(void *arg, int pending)
1921 struct ifnet *ifp = (struct ifnet *)arg;
1922 int link_state = ifp->if_link_state;
1923 CURVNET_SET(ifp->if_vnet);
1925 /* Notify that the link state has changed. */
1927 if (ifp->if_vlantrunk != NULL)
1928 (*vlan_link_state_p)(ifp);
1930 if ((ifp->if_type == IFT_ETHER || ifp->if_type == IFT_L2VLAN) &&
1931 IFP2AC(ifp)->ac_netgraph != NULL)
1932 (*ng_ether_link_state_p)(ifp, link_state);
1934 (*carp_linkstate_p)(ifp);
1936 (*bridge_linkstate_p)(ifp);
1938 (*lagg_linkstate_p)(ifp, link_state);
1940 if (IS_DEFAULT_VNET(curvnet))
1941 devctl_notify("IFNET", ifp->if_xname,
1942 (link_state == LINK_STATE_UP) ? "LINK_UP" : "LINK_DOWN",
1945 if_printf(ifp, "%d link states coalesced\n", pending);
1946 if (log_link_state_change)
1947 log(LOG_NOTICE, "%s: link state changed to %s\n", ifp->if_xname,
1948 (link_state == LINK_STATE_UP) ? "UP" : "DOWN" );
1949 EVENTHANDLER_INVOKE(ifnet_link_event, ifp, ifp->if_link_state);
1954 * Mark an interface down and notify protocols of
1958 if_down(struct ifnet *ifp)
1961 if_unroute(ifp, IFF_UP, AF_UNSPEC);
1965 * Mark an interface up and notify protocols of
1969 if_up(struct ifnet *ifp)
1972 if_route(ifp, IFF_UP, AF_UNSPEC);
1976 * Flush an interface queue.
1979 if_qflush(struct ifnet *ifp)
1987 if (ALTQ_IS_ENABLED(ifq))
1991 while ((m = n) != 0) {
2002 * Map interface name to interface structure pointer, with or without
2003 * returning a reference.
2006 ifunit_ref(const char *name)
2010 IFNET_RLOCK_NOSLEEP();
2011 TAILQ_FOREACH(ifp, &V_ifnet, if_link) {
2012 if (strncmp(name, ifp->if_xname, IFNAMSIZ) == 0 &&
2013 !(ifp->if_flags & IFF_DYING))
2018 IFNET_RUNLOCK_NOSLEEP();
2023 ifunit(const char *name)
2027 IFNET_RLOCK_NOSLEEP();
2028 TAILQ_FOREACH(ifp, &V_ifnet, if_link) {
2029 if (strncmp(name, ifp->if_xname, IFNAMSIZ) == 0)
2032 IFNET_RUNLOCK_NOSLEEP();
2037 * Hardware specific interface ioctls.
2040 ifhwioctl(u_long cmd, struct ifnet *ifp, caddr_t data, struct thread *td)
2045 int new_flags, temp_flags;
2046 size_t namelen, onamelen;
2048 char *descrbuf, *odescrbuf;
2049 char new_name[IFNAMSIZ];
2051 struct sockaddr_dl *sdl;
2053 ifr = (struct ifreq *)data;
2056 ifr->ifr_index = ifp->if_index;
2060 temp_flags = ifp->if_flags | ifp->if_drv_flags;
2061 ifr->ifr_flags = temp_flags & 0xffff;
2062 ifr->ifr_flagshigh = temp_flags >> 16;
2066 ifr->ifr_reqcap = ifp->if_capabilities;
2067 ifr->ifr_curcap = ifp->if_capenable;
2072 error = mac_ifnet_ioctl_get(td->td_ucred, ifr, ifp);
2077 ifr->ifr_metric = ifp->if_metric;
2081 ifr->ifr_mtu = ifp->if_mtu;
2085 ifr->ifr_phys = ifp->if_physical;
2090 sx_slock(&ifdescr_sx);
2091 if (ifp->if_description == NULL)
2094 /* space for terminating nul */
2095 descrlen = strlen(ifp->if_description) + 1;
2096 if (ifr->ifr_buffer.length < descrlen)
2097 ifr->ifr_buffer.buffer = NULL;
2099 error = copyout(ifp->if_description,
2100 ifr->ifr_buffer.buffer, descrlen);
2101 ifr->ifr_buffer.length = descrlen;
2103 sx_sunlock(&ifdescr_sx);
2107 error = priv_check(td, PRIV_NET_SETIFDESCR);
2112 * Copy only (length-1) bytes to make sure that
2113 * if_description is always nul terminated. The
2114 * length parameter is supposed to count the
2115 * terminating nul in.
2117 if (ifr->ifr_buffer.length > ifdescr_maxlen)
2118 return (ENAMETOOLONG);
2119 else if (ifr->ifr_buffer.length == 0)
2122 descrbuf = malloc(ifr->ifr_buffer.length, M_IFDESCR,
2124 error = copyin(ifr->ifr_buffer.buffer, descrbuf,
2125 ifr->ifr_buffer.length - 1);
2127 free(descrbuf, M_IFDESCR);
2132 sx_xlock(&ifdescr_sx);
2133 odescrbuf = ifp->if_description;
2134 ifp->if_description = descrbuf;
2135 sx_xunlock(&ifdescr_sx);
2137 getmicrotime(&ifp->if_lastchange);
2138 free(odescrbuf, M_IFDESCR);
2142 ifr->ifr_fib = ifp->if_fib;
2146 error = priv_check(td, PRIV_NET_SETIFFIB);
2149 if (ifr->ifr_fib >= rt_numfibs)
2152 ifp->if_fib = ifr->ifr_fib;
2156 error = priv_check(td, PRIV_NET_SETIFFLAGS);
2160 * Currently, no driver owned flags pass the IFF_CANTCHANGE
2161 * check, so we don't need special handling here yet.
2163 new_flags = (ifr->ifr_flags & 0xffff) |
2164 (ifr->ifr_flagshigh << 16);
2165 if (ifp->if_flags & IFF_SMART) {
2166 /* Smart drivers twiddle their own routes */
2167 } else if (ifp->if_flags & IFF_UP &&
2168 (new_flags & IFF_UP) == 0) {
2170 } else if (new_flags & IFF_UP &&
2171 (ifp->if_flags & IFF_UP) == 0) {
2174 /* See if permanently promiscuous mode bit is about to flip */
2175 if ((ifp->if_flags ^ new_flags) & IFF_PPROMISC) {
2176 if (new_flags & IFF_PPROMISC)
2177 ifp->if_flags |= IFF_PROMISC;
2178 else if (ifp->if_pcount == 0)
2179 ifp->if_flags &= ~IFF_PROMISC;
2180 log(LOG_INFO, "%s: permanently promiscuous mode %s\n",
2182 (new_flags & IFF_PPROMISC) ? "enabled" : "disabled");
2184 ifp->if_flags = (ifp->if_flags & IFF_CANTCHANGE) |
2185 (new_flags &~ IFF_CANTCHANGE);
2186 if (ifp->if_ioctl) {
2187 (void) (*ifp->if_ioctl)(ifp, cmd, data);
2189 getmicrotime(&ifp->if_lastchange);
2193 error = priv_check(td, PRIV_NET_SETIFCAP);
2196 if (ifp->if_ioctl == NULL)
2197 return (EOPNOTSUPP);
2198 if (ifr->ifr_reqcap & ~ifp->if_capabilities)
2200 error = (*ifp->if_ioctl)(ifp, cmd, data);
2202 getmicrotime(&ifp->if_lastchange);
2207 error = mac_ifnet_ioctl_set(td->td_ucred, ifr, ifp);
2212 error = priv_check(td, PRIV_NET_SETIFNAME);
2215 error = copyinstr(ifr->ifr_data, new_name, IFNAMSIZ, NULL);
2218 if (new_name[0] == '\0')
2220 if (ifunit(new_name) != NULL)
2224 * XXX: Locking. Nothing else seems to lock if_flags,
2225 * and there are numerous other races with the
2226 * ifunit() checks not being atomic with namespace
2227 * changes (renames, vmoves, if_attach, etc).
2229 ifp->if_flags |= IFF_RENAMING;
2231 /* Announce the departure of the interface. */
2232 rt_ifannouncemsg(ifp, IFAN_DEPARTURE);
2233 EVENTHANDLER_INVOKE(ifnet_departure_event, ifp);
2235 log(LOG_INFO, "%s: changing name to '%s'\n",
2236 ifp->if_xname, new_name);
2238 strlcpy(ifp->if_xname, new_name, sizeof(ifp->if_xname));
2241 sdl = (struct sockaddr_dl *)ifa->ifa_addr;
2242 namelen = strlen(new_name);
2243 onamelen = sdl->sdl_nlen;
2245 * Move the address if needed. This is safe because we
2246 * allocate space for a name of length IFNAMSIZ when we
2247 * create this in if_attach().
2249 if (namelen != onamelen) {
2250 bcopy(sdl->sdl_data + onamelen,
2251 sdl->sdl_data + namelen, sdl->sdl_alen);
2253 bcopy(new_name, sdl->sdl_data, namelen);
2254 sdl->sdl_nlen = namelen;
2255 sdl = (struct sockaddr_dl *)ifa->ifa_netmask;
2256 bzero(sdl->sdl_data, onamelen);
2257 while (namelen != 0)
2258 sdl->sdl_data[--namelen] = 0xff;
2261 EVENTHANDLER_INVOKE(ifnet_arrival_event, ifp);
2262 /* Announce the return of the interface. */
2263 rt_ifannouncemsg(ifp, IFAN_ARRIVAL);
2265 ifp->if_flags &= ~IFF_RENAMING;
2270 error = priv_check(td, PRIV_NET_SETIFVNET);
2273 error = if_vmove_loan(td, ifp, ifr->ifr_name, ifr->ifr_jid);
2278 error = priv_check(td, PRIV_NET_SETIFMETRIC);
2281 ifp->if_metric = ifr->ifr_metric;
2282 getmicrotime(&ifp->if_lastchange);
2286 error = priv_check(td, PRIV_NET_SETIFPHYS);
2289 if (ifp->if_ioctl == NULL)
2290 return (EOPNOTSUPP);
2291 error = (*ifp->if_ioctl)(ifp, cmd, data);
2293 getmicrotime(&ifp->if_lastchange);
2298 u_long oldmtu = ifp->if_mtu;
2300 error = priv_check(td, PRIV_NET_SETIFMTU);
2303 if (ifr->ifr_mtu < IF_MINMTU || ifr->ifr_mtu > IF_MAXMTU)
2305 if (ifp->if_ioctl == NULL)
2306 return (EOPNOTSUPP);
2307 error = (*ifp->if_ioctl)(ifp, cmd, data);
2309 getmicrotime(&ifp->if_lastchange);
2313 * If the link MTU changed, do network layer specific procedure.
2315 if (ifp->if_mtu != oldmtu) {
2325 if (cmd == SIOCADDMULTI)
2326 error = priv_check(td, PRIV_NET_ADDMULTI);
2328 error = priv_check(td, PRIV_NET_DELMULTI);
2332 /* Don't allow group membership on non-multicast interfaces. */
2333 if ((ifp->if_flags & IFF_MULTICAST) == 0)
2334 return (EOPNOTSUPP);
2336 /* Don't let users screw up protocols' entries. */
2337 if (ifr->ifr_addr.sa_family != AF_LINK)
2340 if (cmd == SIOCADDMULTI) {
2341 struct ifmultiaddr *ifma;
2344 * Userland is only permitted to join groups once
2345 * via the if_addmulti() KPI, because it cannot hold
2346 * struct ifmultiaddr * between calls. It may also
2347 * lose a race while we check if the membership
2351 ifma = if_findmulti(ifp, &ifr->ifr_addr);
2352 IF_ADDR_RUNLOCK(ifp);
2356 error = if_addmulti(ifp, &ifr->ifr_addr, &ifma);
2358 error = if_delmulti(ifp, &ifr->ifr_addr);
2361 getmicrotime(&ifp->if_lastchange);
2364 case SIOCSIFPHYADDR:
2365 case SIOCDIFPHYADDR:
2367 case SIOCSIFPHYADDR_IN6:
2369 case SIOCSLIFPHYADDR:
2371 case SIOCSIFGENERIC:
2372 error = priv_check(td, PRIV_NET_HWIOCTL);
2375 if (ifp->if_ioctl == NULL)
2376 return (EOPNOTSUPP);
2377 error = (*ifp->if_ioctl)(ifp, cmd, data);
2379 getmicrotime(&ifp->if_lastchange);
2383 ifs = (struct ifstat *)data;
2384 ifs->ascii[0] = '\0';
2386 case SIOCGIFPSRCADDR:
2387 case SIOCGIFPDSTADDR:
2388 case SIOCGLIFPHYADDR:
2390 case SIOCGIFGENERIC:
2391 if (ifp->if_ioctl == NULL)
2392 return (EOPNOTSUPP);
2393 error = (*ifp->if_ioctl)(ifp, cmd, data);
2397 error = priv_check(td, PRIV_NET_SETLLADDR);
2400 error = if_setlladdr(ifp,
2401 ifr->ifr_addr.sa_data, ifr->ifr_addr.sa_len);
2402 EVENTHANDLER_INVOKE(iflladdr_event, ifp);
2407 struct ifgroupreq *ifgr = (struct ifgroupreq *)ifr;
2409 error = priv_check(td, PRIV_NET_ADDIFGROUP);
2412 if ((error = if_addgroup(ifp, ifgr->ifgr_group)))
2418 if ((error = if_getgroup((struct ifgroupreq *)ifr, ifp)))
2424 struct ifgroupreq *ifgr = (struct ifgroupreq *)ifr;
2426 error = priv_check(td, PRIV_NET_DELIFGROUP);
2429 if ((error = if_delgroup(ifp, ifgr->ifgr_group)))
2441 #ifdef COMPAT_FREEBSD32
2449 #define SIOCGIFCONF32 _IOWR('i', 36, struct ifconf32)
2456 ifioctl(struct socket *so, u_long cmd, caddr_t data, struct thread *td)
2463 CURVNET_SET(so->so_vnet);
2467 error = ifconf(cmd, data);
2471 #ifdef COMPAT_FREEBSD32
2474 struct ifconf32 *ifc32;
2477 ifc32 = (struct ifconf32 *)data;
2478 ifc.ifc_len = ifc32->ifc_len;
2479 ifc.ifc_buf = PTRIN(ifc32->ifc_buf);
2481 error = ifconf(SIOCGIFCONF, (void *)&ifc);
2484 ifc32->ifc_len = ifc.ifc_len;
2489 ifr = (struct ifreq *)data;
2494 error = priv_check(td, PRIV_NET_SETIFVNET);
2496 error = if_vmove_reclaim(td, ifr->ifr_name,
2503 error = priv_check(td, PRIV_NET_IFCREATE);
2505 error = if_clone_create(ifr->ifr_name,
2506 sizeof(ifr->ifr_name),
2507 cmd == SIOCIFCREATE2 ? ifr->ifr_data : NULL);
2511 error = priv_check(td, PRIV_NET_IFDESTROY);
2513 error = if_clone_destroy(ifr->ifr_name);
2517 case SIOCIFGCLONERS:
2518 error = if_clone_list((struct if_clonereq *)data);
2522 error = if_getgroupmembers((struct ifgroupreq *)data);
2525 #if defined(INET) || defined(INET6)
2528 if (carp_ioctl_p == NULL)
2529 error = EPROTONOSUPPORT;
2531 error = (*carp_ioctl_p)(ifr, cmd, td);
2537 ifp = ifunit_ref(ifr->ifr_name);
2543 error = ifhwioctl(cmd, ifp, data, td);
2544 if (error != ENOIOCTL) {
2550 oif_flags = ifp->if_flags;
2551 if (so->so_proto == NULL) {
2554 return (EOPNOTSUPP);
2558 * Pass the request on to the socket control method, and if the
2559 * latter returns EOPNOTSUPP, directly to the interface.
2561 * Make an exception for the legacy SIOCSIF* requests. Drivers
2562 * trust SIOCSIFADDR et al to come from an already privileged
2563 * layer, and do not perform any credentials checks or input
2567 error = ((*so->so_proto->pr_usrreqs->pru_control)(so, cmd,
2570 if (error == EOPNOTSUPP && ifp != NULL && ifp->if_ioctl != NULL &&
2571 cmd != SIOCSIFADDR && cmd != SIOCSIFBRDADDR &&
2572 cmd != SIOCSIFDSTADDR && cmd != SIOCSIFNETMASK)
2573 error = (*ifp->if_ioctl)(ifp, cmd, data);
2580 case SIOCSIFDSTADDR:
2582 case SIOCSIFBRDADDR:
2583 case SIOCSIFNETMASK:
2584 #if BYTE_ORDER != BIG_ENDIAN
2585 if (ifr->ifr_addr.sa_family == 0 &&
2586 ifr->ifr_addr.sa_len < 16) {
2587 ifr->ifr_addr.sa_family = ifr->ifr_addr.sa_len;
2588 ifr->ifr_addr.sa_len = 16;
2591 if (ifr->ifr_addr.sa_len == 0)
2592 ifr->ifr_addr.sa_len = 16;
2600 case OSIOCGIFDSTADDR:
2601 cmd = SIOCGIFDSTADDR;
2604 case OSIOCGIFBRDADDR:
2605 cmd = SIOCGIFBRDADDR;
2608 case OSIOCGIFNETMASK:
2609 cmd = SIOCGIFNETMASK;
2611 error = ((*so->so_proto->pr_usrreqs->pru_control)(so,
2615 if (error == EOPNOTSUPP && ifp != NULL &&
2616 ifp->if_ioctl != NULL &&
2617 cmd != SIOCSIFADDR && cmd != SIOCSIFBRDADDR &&
2618 cmd != SIOCSIFDSTADDR && cmd != SIOCSIFNETMASK)
2619 error = (*ifp->if_ioctl)(ifp, cmd, data);
2623 case OSIOCGIFDSTADDR:
2624 case OSIOCGIFBRDADDR:
2625 case OSIOCGIFNETMASK:
2626 *(u_short *)&ifr->ifr_addr = ifr->ifr_addr.sa_family;
2630 #endif /* COMPAT_43 */
2632 if ((oif_flags ^ ifp->if_flags) & IFF_UP) {
2634 if (ifp->if_flags & IFF_UP)
2644 * The code common to handling reference counted flags,
2645 * e.g., in ifpromisc() and if_allmulti().
2646 * The "pflag" argument can specify a permanent mode flag to check,
2647 * such as IFF_PPROMISC for promiscuous mode; should be 0 if none.
2649 * Only to be used on stack-owned flags, not driver-owned flags.
2652 if_setflag(struct ifnet *ifp, int flag, int pflag, int *refcount, int onswitch)
2656 int oldflags, oldcount;
2658 /* Sanity checks to catch programming errors */
2659 KASSERT((flag & (IFF_DRV_OACTIVE|IFF_DRV_RUNNING)) == 0,
2660 ("%s: setting driver-owned flag %d", __func__, flag));
2663 KASSERT(*refcount >= 0,
2664 ("%s: increment negative refcount %d for flag %d",
2665 __func__, *refcount, flag));
2667 KASSERT(*refcount > 0,
2668 ("%s: decrement non-positive refcount %d for flag %d",
2669 __func__, *refcount, flag));
2671 /* In case this mode is permanent, just touch refcount */
2672 if (ifp->if_flags & pflag) {
2673 *refcount += onswitch ? 1 : -1;
2677 /* Save ifnet parameters for if_ioctl() may fail */
2678 oldcount = *refcount;
2679 oldflags = ifp->if_flags;
2682 * See if we aren't the only and touching refcount is enough.
2683 * Actually toggle interface flag if we are the first or last.
2688 ifp->if_flags |= flag;
2692 ifp->if_flags &= ~flag;
2695 /* Call down the driver since we've changed interface flags */
2696 if (ifp->if_ioctl == NULL) {
2700 ifr.ifr_flags = ifp->if_flags & 0xffff;
2701 ifr.ifr_flagshigh = ifp->if_flags >> 16;
2702 error = (*ifp->if_ioctl)(ifp, SIOCSIFFLAGS, (caddr_t)&ifr);
2705 /* Notify userland that interface flags have changed */
2710 /* Recover after driver error */
2711 *refcount = oldcount;
2712 ifp->if_flags = oldflags;
2717 * Set/clear promiscuous mode on interface ifp based on the truth value
2718 * of pswitch. The calls are reference counted so that only the first
2719 * "on" request actually has an effect, as does the final "off" request.
2720 * Results are undefined if the "off" and "on" requests are not matched.
2723 ifpromisc(struct ifnet *ifp, int pswitch)
2726 int oldflags = ifp->if_flags;
2728 error = if_setflag(ifp, IFF_PROMISC, IFF_PPROMISC,
2729 &ifp->if_pcount, pswitch);
2730 /* If promiscuous mode status has changed, log a message */
2731 if (error == 0 && ((ifp->if_flags ^ oldflags) & IFF_PROMISC))
2732 log(LOG_INFO, "%s: promiscuous mode %s\n",
2734 (ifp->if_flags & IFF_PROMISC) ? "enabled" : "disabled");
2739 * Return interface configuration
2740 * of system. List may be used
2741 * in later ioctl's (above) to get
2742 * other information.
2746 ifconf(u_long cmd, caddr_t data)
2748 struct ifconf *ifc = (struct ifconf *)data;
2753 int error, full = 0, valid_len, max_len;
2755 /* Limit initial buffer size to MAXPHYS to avoid DoS from userspace. */
2756 max_len = MAXPHYS - 1;
2758 /* Prevent hostile input from being able to crash the system */
2759 if (ifc->ifc_len <= 0)
2763 if (ifc->ifc_len <= max_len) {
2764 max_len = ifc->ifc_len;
2767 sb = sbuf_new(NULL, NULL, max_len + 1, SBUF_FIXEDLEN);
2772 TAILQ_FOREACH(ifp, &V_ifnet, if_link) {
2776 * Zero the ifr_name buffer to make sure we don't
2777 * disclose the contents of the stack.
2779 memset(ifr.ifr_name, 0, sizeof(ifr.ifr_name));
2781 if (strlcpy(ifr.ifr_name, ifp->if_xname, sizeof(ifr.ifr_name))
2782 >= sizeof(ifr.ifr_name)) {
2785 return (ENAMETOOLONG);
2790 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
2791 struct sockaddr *sa = ifa->ifa_addr;
2793 if (prison_if(curthread->td_ucred, sa) != 0)
2797 if (cmd == OSIOCGIFCONF) {
2798 struct osockaddr *osa =
2799 (struct osockaddr *)&ifr.ifr_addr;
2801 osa->sa_family = sa->sa_family;
2802 sbuf_bcat(sb, &ifr, sizeof(ifr));
2803 max_len += sizeof(ifr);
2806 if (sa->sa_len <= sizeof(*sa)) {
2808 sbuf_bcat(sb, &ifr, sizeof(ifr));
2809 max_len += sizeof(ifr);
2812 offsetof(struct ifreq, ifr_addr));
2813 max_len += offsetof(struct ifreq, ifr_addr);
2814 sbuf_bcat(sb, sa, sa->sa_len);
2815 max_len += sa->sa_len;
2818 if (sbuf_error(sb) == 0)
2819 valid_len = sbuf_len(sb);
2821 IF_ADDR_RUNLOCK(ifp);
2823 bzero((caddr_t)&ifr.ifr_addr, sizeof(ifr.ifr_addr));
2824 sbuf_bcat(sb, &ifr, sizeof(ifr));
2825 max_len += sizeof(ifr);
2827 if (sbuf_error(sb) == 0)
2828 valid_len = sbuf_len(sb);
2834 * If we didn't allocate enough space (uncommon), try again. If
2835 * we have already allocated as much space as we are allowed,
2836 * return what we've got.
2838 if (valid_len != max_len && !full) {
2843 ifc->ifc_len = valid_len;
2845 error = copyout(sbuf_data(sb), ifc->ifc_req, ifc->ifc_len);
2851 * Just like ifpromisc(), but for all-multicast-reception mode.
2854 if_allmulti(struct ifnet *ifp, int onswitch)
2857 return (if_setflag(ifp, IFF_ALLMULTI, 0, &ifp->if_amcount, onswitch));
2860 struct ifmultiaddr *
2861 if_findmulti(struct ifnet *ifp, struct sockaddr *sa)
2863 struct ifmultiaddr *ifma;
2865 IF_ADDR_LOCK_ASSERT(ifp);
2867 TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
2868 if (sa->sa_family == AF_LINK) {
2869 if (sa_dl_equal(ifma->ifma_addr, sa))
2872 if (sa_equal(ifma->ifma_addr, sa))
2881 * Allocate a new ifmultiaddr and initialize based on passed arguments. We
2882 * make copies of passed sockaddrs. The ifmultiaddr will not be added to
2883 * the ifnet multicast address list here, so the caller must do that and
2884 * other setup work (such as notifying the device driver). The reference
2885 * count is initialized to 1.
2887 static struct ifmultiaddr *
2888 if_allocmulti(struct ifnet *ifp, struct sockaddr *sa, struct sockaddr *llsa,
2891 struct ifmultiaddr *ifma;
2892 struct sockaddr *dupsa;
2894 ifma = malloc(sizeof *ifma, M_IFMADDR, mflags |
2899 dupsa = malloc(sa->sa_len, M_IFMADDR, mflags);
2900 if (dupsa == NULL) {
2901 free(ifma, M_IFMADDR);
2904 bcopy(sa, dupsa, sa->sa_len);
2905 ifma->ifma_addr = dupsa;
2907 ifma->ifma_ifp = ifp;
2908 ifma->ifma_refcount = 1;
2909 ifma->ifma_protospec = NULL;
2912 ifma->ifma_lladdr = NULL;
2916 dupsa = malloc(llsa->sa_len, M_IFMADDR, mflags);
2917 if (dupsa == NULL) {
2918 free(ifma->ifma_addr, M_IFMADDR);
2919 free(ifma, M_IFMADDR);
2922 bcopy(llsa, dupsa, llsa->sa_len);
2923 ifma->ifma_lladdr = dupsa;
2929 * if_freemulti: free ifmultiaddr structure and possibly attached related
2930 * addresses. The caller is responsible for implementing reference
2931 * counting, notifying the driver, handling routing messages, and releasing
2932 * any dependent link layer state.
2935 if_freemulti(struct ifmultiaddr *ifma)
2938 KASSERT(ifma->ifma_refcount == 0, ("if_freemulti: refcount %d",
2939 ifma->ifma_refcount));
2940 KASSERT(ifma->ifma_protospec == NULL,
2941 ("if_freemulti: protospec not NULL"));
2943 if (ifma->ifma_lladdr != NULL)
2944 free(ifma->ifma_lladdr, M_IFMADDR);
2945 free(ifma->ifma_addr, M_IFMADDR);
2946 free(ifma, M_IFMADDR);
2950 * Register an additional multicast address with a network interface.
2952 * - If the address is already present, bump the reference count on the
2953 * address and return.
2954 * - If the address is not link-layer, look up a link layer address.
2955 * - Allocate address structures for one or both addresses, and attach to the
2956 * multicast address list on the interface. If automatically adding a link
2957 * layer address, the protocol address will own a reference to the link
2958 * layer address, to be freed when it is freed.
2959 * - Notify the network device driver of an addition to the multicast address
2962 * 'sa' points to caller-owned memory with the desired multicast address.
2964 * 'retifma' will be used to return a pointer to the resulting multicast
2965 * address reference, if desired.
2968 if_addmulti(struct ifnet *ifp, struct sockaddr *sa,
2969 struct ifmultiaddr **retifma)
2971 struct ifmultiaddr *ifma, *ll_ifma;
2972 struct sockaddr *llsa;
2976 * If the address is already present, return a new reference to it;
2977 * otherwise, allocate storage and set up a new address.
2980 ifma = if_findmulti(ifp, sa);
2982 ifma->ifma_refcount++;
2983 if (retifma != NULL)
2985 IF_ADDR_WUNLOCK(ifp);
2990 * The address isn't already present; resolve the protocol address
2991 * into a link layer address, and then look that up, bump its
2992 * refcount or allocate an ifma for that also. If 'llsa' was
2993 * returned, we will need to free it later.
2997 if (ifp->if_resolvemulti != NULL) {
2998 error = ifp->if_resolvemulti(ifp, &llsa, sa);
3004 * Allocate the new address. Don't hook it up yet, as we may also
3005 * need to allocate a link layer multicast address.
3007 ifma = if_allocmulti(ifp, sa, llsa, M_NOWAIT);
3014 * If a link layer address is found, we'll need to see if it's
3015 * already present in the address list, or allocate is as well.
3016 * When this block finishes, the link layer address will be on the
3020 ll_ifma = if_findmulti(ifp, llsa);
3021 if (ll_ifma == NULL) {
3022 ll_ifma = if_allocmulti(ifp, llsa, NULL, M_NOWAIT);
3023 if (ll_ifma == NULL) {
3024 --ifma->ifma_refcount;
3029 TAILQ_INSERT_HEAD(&ifp->if_multiaddrs, ll_ifma,
3032 ll_ifma->ifma_refcount++;
3033 ifma->ifma_llifma = ll_ifma;
3037 * We now have a new multicast address, ifma, and possibly a new or
3038 * referenced link layer address. Add the primary address to the
3039 * ifnet address list.
3041 TAILQ_INSERT_HEAD(&ifp->if_multiaddrs, ifma, ifma_link);
3043 if (retifma != NULL)
3047 * Must generate the message while holding the lock so that 'ifma'
3048 * pointer is still valid.
3050 rt_newmaddrmsg(RTM_NEWMADDR, ifma);
3051 IF_ADDR_WUNLOCK(ifp);
3054 * We are certain we have added something, so call down to the
3055 * interface to let them know about it.
3057 if (ifp->if_ioctl != NULL) {
3058 (void) (*ifp->if_ioctl)(ifp, SIOCADDMULTI, 0);
3062 free(llsa, M_IFMADDR);
3068 free(llsa, M_IFMADDR);
3071 IF_ADDR_WUNLOCK(ifp);
3076 * Delete a multicast group membership by network-layer group address.
3078 * Returns ENOENT if the entry could not be found. If ifp no longer
3079 * exists, results are undefined. This entry point should only be used
3080 * from subsystems which do appropriate locking to hold ifp for the
3081 * duration of the call.
3082 * Network-layer protocol domains must use if_delmulti_ifma().
3085 if_delmulti(struct ifnet *ifp, struct sockaddr *sa)
3087 struct ifmultiaddr *ifma;
3092 IFNET_RLOCK_NOSLEEP();
3093 TAILQ_FOREACH(oifp, &V_ifnet, if_link)
3098 IFNET_RUNLOCK_NOSLEEP();
3100 KASSERT(ifp != NULL, ("%s: ifnet went away", __func__));
3107 ifma = if_findmulti(ifp, sa);
3109 lastref = if_delmulti_locked(ifp, ifma, 0);
3110 IF_ADDR_WUNLOCK(ifp);
3115 if (lastref && ifp->if_ioctl != NULL) {
3116 (void)(*ifp->if_ioctl)(ifp, SIOCDELMULTI, 0);
3123 * Delete all multicast group membership for an interface.
3124 * Should be used to quickly flush all multicast filters.
3127 if_delallmulti(struct ifnet *ifp)
3129 struct ifmultiaddr *ifma;
3130 struct ifmultiaddr *next;
3133 TAILQ_FOREACH_SAFE(ifma, &ifp->if_multiaddrs, ifma_link, next)
3134 if_delmulti_locked(ifp, ifma, 0);
3135 IF_ADDR_WUNLOCK(ifp);
3139 * Delete a multicast group membership by group membership pointer.
3140 * Network-layer protocol domains must use this routine.
3142 * It is safe to call this routine if the ifp disappeared.
3145 if_delmulti_ifma(struct ifmultiaddr *ifma)
3150 ifp = ifma->ifma_ifp;
3153 printf("%s: ifma_ifp seems to be detached\n", __func__);
3157 IFNET_RLOCK_NOSLEEP();
3158 TAILQ_FOREACH(oifp, &V_ifnet, if_link)
3162 printf("%s: ifnet %p disappeared\n", __func__, ifp);
3165 IFNET_RUNLOCK_NOSLEEP();
3169 * If and only if the ifnet instance exists: Acquire the address lock.
3174 lastref = if_delmulti_locked(ifp, ifma, 0);
3178 * If and only if the ifnet instance exists:
3179 * Release the address lock.
3180 * If the group was left: update the hardware hash filter.
3182 IF_ADDR_WUNLOCK(ifp);
3183 if (lastref && ifp->if_ioctl != NULL) {
3184 (void)(*ifp->if_ioctl)(ifp, SIOCDELMULTI, 0);
3190 * Perform deletion of network-layer and/or link-layer multicast address.
3192 * Return 0 if the reference count was decremented.
3193 * Return 1 if the final reference was released, indicating that the
3194 * hardware hash filter should be reprogrammed.
3197 if_delmulti_locked(struct ifnet *ifp, struct ifmultiaddr *ifma, int detaching)
3199 struct ifmultiaddr *ll_ifma;
3201 if (ifp != NULL && ifma->ifma_ifp != NULL) {
3202 KASSERT(ifma->ifma_ifp == ifp,
3203 ("%s: inconsistent ifp %p", __func__, ifp));
3204 IF_ADDR_WLOCK_ASSERT(ifp);
3207 ifp = ifma->ifma_ifp;
3210 * If the ifnet is detaching, null out references to ifnet,
3211 * so that upper protocol layers will notice, and not attempt
3212 * to obtain locks for an ifnet which no longer exists. The
3213 * routing socket announcement must happen before the ifnet
3214 * instance is detached from the system.
3218 printf("%s: detaching ifnet instance %p\n", __func__, ifp);
3221 * ifp may already be nulled out if we are being reentered
3222 * to delete the ll_ifma.
3225 rt_newmaddrmsg(RTM_DELMADDR, ifma);
3226 ifma->ifma_ifp = NULL;
3230 if (--ifma->ifma_refcount > 0)
3234 * If this ifma is a network-layer ifma, a link-layer ifma may
3235 * have been associated with it. Release it first if so.
3237 ll_ifma = ifma->ifma_llifma;
3238 if (ll_ifma != NULL) {
3239 KASSERT(ifma->ifma_lladdr != NULL,
3240 ("%s: llifma w/o lladdr", __func__));
3242 ll_ifma->ifma_ifp = NULL; /* XXX */
3243 if (--ll_ifma->ifma_refcount == 0) {
3245 TAILQ_REMOVE(&ifp->if_multiaddrs, ll_ifma,
3248 if_freemulti(ll_ifma);
3253 TAILQ_REMOVE(&ifp->if_multiaddrs, ifma, ifma_link);
3258 * The last reference to this instance of struct ifmultiaddr
3259 * was released; the hardware should be notified of this change.
3265 * Set the link layer address on an interface.
3267 * At this time we only support certain types of interfaces,
3268 * and we don't allow the length of the address to change.
3271 if_setlladdr(struct ifnet *ifp, const u_char *lladdr, int len)
3273 struct sockaddr_dl *sdl;
3280 IF_ADDR_RUNLOCK(ifp);
3284 IF_ADDR_RUNLOCK(ifp);
3285 sdl = (struct sockaddr_dl *)ifa->ifa_addr;
3290 if (len != sdl->sdl_alen) { /* don't allow length to change */
3294 switch (ifp->if_type) {
3302 case IFT_IEEE8023ADLAG:
3304 bcopy(lladdr, LLADDR(sdl), len);
3313 * If the interface is already up, we need
3314 * to re-init it in order to reprogram its
3317 if ((ifp->if_flags & IFF_UP) != 0) {
3318 if (ifp->if_ioctl) {
3319 ifp->if_flags &= ~IFF_UP;
3320 ifr.ifr_flags = ifp->if_flags & 0xffff;
3321 ifr.ifr_flagshigh = ifp->if_flags >> 16;
3322 (*ifp->if_ioctl)(ifp, SIOCSIFFLAGS, (caddr_t)&ifr);
3323 ifp->if_flags |= IFF_UP;
3324 ifr.ifr_flags = ifp->if_flags & 0xffff;
3325 ifr.ifr_flagshigh = ifp->if_flags >> 16;
3326 (*ifp->if_ioctl)(ifp, SIOCSIFFLAGS, (caddr_t)&ifr);
3330 * Also send gratuitous ARPs to notify other nodes about
3331 * the address change.
3333 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
3334 if (ifa->ifa_addr->sa_family == AF_INET)
3335 arp_ifinit(ifp, ifa);
3343 * The name argument must be a pointer to storage which will last as
3344 * long as the interface does. For physical devices, the result of
3345 * device_get_name(dev) is a good choice and for pseudo-devices a
3346 * static string works well.
3349 if_initname(struct ifnet *ifp, const char *name, int unit)
3351 ifp->if_dname = name;
3352 ifp->if_dunit = unit;
3353 if (unit != IF_DUNIT_NONE)
3354 snprintf(ifp->if_xname, IFNAMSIZ, "%s%d", name, unit);
3356 strlcpy(ifp->if_xname, name, IFNAMSIZ);
3360 if_printf(struct ifnet *ifp, const char * fmt, ...)
3365 retval = printf("%s: ", ifp->if_xname);
3367 retval += vprintf(fmt, ap);
3373 if_start(struct ifnet *ifp)
3376 (*(ifp)->if_start)(ifp);
3380 * Backwards compatibility interface for drivers
3381 * that have not implemented it
3384 if_transmit(struct ifnet *ifp, struct mbuf *m)
3388 IFQ_HANDOFF(ifp, m, error);
3393 if_handoff(struct ifqueue *ifq, struct mbuf *m, struct ifnet *ifp, int adjust)
3398 if (_IF_QFULL(ifq)) {
3405 ifp->if_obytes += m->m_pkthdr.len + adjust;
3406 if (m->m_flags & (M_BCAST|M_MCAST))
3408 active = ifp->if_drv_flags & IFF_DRV_OACTIVE;
3410 _IF_ENQUEUE(ifq, m);
3412 if (ifp != NULL && !active)
3413 (*(ifp)->if_start)(ifp);
3418 if_register_com_alloc(u_char type,
3419 if_com_alloc_t *a, if_com_free_t *f)
3422 KASSERT(if_com_alloc[type] == NULL,
3423 ("if_register_com_alloc: %d already registered", type));
3424 KASSERT(if_com_free[type] == NULL,
3425 ("if_register_com_alloc: %d free already registered", type));
3427 if_com_alloc[type] = a;
3428 if_com_free[type] = f;
3432 if_deregister_com_alloc(u_char type)
3435 KASSERT(if_com_alloc[type] != NULL,
3436 ("if_deregister_com_alloc: %d not registered", type));
3437 KASSERT(if_com_free[type] != NULL,
3438 ("if_deregister_com_alloc: %d free not registered", type));
3439 if_com_alloc[type] = NULL;
3440 if_com_free[type] = NULL;