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)
78 #include <netinet/in.h>
79 #include <netinet/in_var.h>
80 #include <netinet/ip_carp.h>
82 #include <netinet6/in6_var.h>
83 #include <netinet6/in6_ifattach.h>
87 #include <netinet/if_ether.h>
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 #if defined(INET) || defined(INET6)
134 int (*carp_forus_p)(struct ifnet *ifp, u_char *dhost);
135 int (*carp_output_p)(struct ifnet *ifp, struct mbuf *m,
136 struct sockaddr *sa);
137 int (*carp_ioctl_p)(struct ifreq *, u_long, struct thread *);
138 int (*carp_attach_p)(struct ifaddr *, int);
139 void (*carp_detach_p)(struct ifaddr *);
142 int (*carp_iamatch_p)(struct ifaddr *, uint8_t **);
145 struct ifaddr *(*carp_iamatch6_p)(struct ifnet *ifp, struct in6_addr *taddr6);
146 caddr_t (*carp_macmatch6_p)(struct ifnet *ifp, struct mbuf *m,
147 const struct in6_addr *taddr);
150 struct mbuf *(*tbr_dequeue_ptr)(struct ifaltq *, int) = NULL;
153 * XXX: Style; these should be sorted alphabetically, and unprototyped
154 * static functions should be prototyped. Currently they are sorted by
157 static void if_attachdomain(void *);
158 static void if_attachdomain1(struct ifnet *);
159 static int ifconf(u_long, caddr_t);
160 static void if_freemulti(struct ifmultiaddr *);
161 static void if_init(void *);
162 static void if_grow(void);
163 static void if_route(struct ifnet *, int flag, int fam);
164 static int if_setflag(struct ifnet *, int, int, int *, int);
165 static int if_transmit(struct ifnet *ifp, struct mbuf *m);
166 static void if_unroute(struct ifnet *, int flag, int fam);
167 static void link_rtrequest(int, struct rtentry *, struct rt_addrinfo *);
168 static int if_rtdel(struct radix_node *, void *);
169 static int ifhwioctl(u_long, struct ifnet *, caddr_t, struct thread *);
170 static int if_delmulti_locked(struct ifnet *, struct ifmultiaddr *, int);
171 static void do_link_state_change(void *, int);
172 static int if_getgroup(struct ifgroupreq *, struct ifnet *);
173 static int if_getgroupmembers(struct ifgroupreq *);
174 static void if_delgroups(struct ifnet *);
175 static void if_attach_internal(struct ifnet *, int);
176 static void if_detach_internal(struct ifnet *, int);
180 * XXX: declare here to avoid to include many inet6 related files..
181 * should be more generalized?
183 extern void nd6_setmtu(struct ifnet *);
186 VNET_DEFINE(int, if_index);
187 int ifqmaxlen = IFQ_MAXLEN;
188 VNET_DEFINE(struct ifnethead, ifnet); /* depend on static init XXX */
189 VNET_DEFINE(struct ifgrouphead, ifg_head);
191 static VNET_DEFINE(int, if_indexlim) = 8;
193 /* Table of ifnet by index. */
194 VNET_DEFINE(struct ifindex_entry *, ifindex_table);
196 #define V_if_indexlim VNET(if_indexlim)
197 #define V_ifindex_table VNET(ifindex_table)
200 * The global network interface list (V_ifnet) and related state (such as
201 * if_index, if_indexlim, and ifindex_table) are protected by an sxlock and
202 * an rwlock. Either may be acquired shared to stablize the list, but both
203 * must be acquired writable to modify the list. This model allows us to
204 * both stablize the interface list during interrupt thread processing, but
205 * also to stablize it over long-running ioctls, without introducing priority
206 * inversions and deadlocks.
208 struct rwlock ifnet_rwlock;
209 struct sx ifnet_sxlock;
212 * The allocation of network interfaces is a rather non-atomic affair; we
213 * need to select an index before we are ready to expose the interface for
214 * use, so will use this pointer value to indicate reservation.
216 #define IFNET_HOLD (void *)(uintptr_t)(-1)
218 static if_com_alloc_t *if_com_alloc[256];
219 static if_com_free_t *if_com_free[256];
221 static MALLOC_DEFINE(M_IFNET, "ifnet", "interface internals");
222 MALLOC_DEFINE(M_IFADDR, "ifaddr", "interface address");
223 MALLOC_DEFINE(M_IFMADDR, "ether_multi", "link-level multicast address");
226 ifnet_byindex_locked(u_short idx)
229 if (idx > V_if_index)
231 if (V_ifindex_table[idx].ife_ifnet == IFNET_HOLD)
233 return (V_ifindex_table[idx].ife_ifnet);
237 ifnet_byindex(u_short idx)
241 IFNET_RLOCK_NOSLEEP();
242 ifp = ifnet_byindex_locked(idx);
243 IFNET_RUNLOCK_NOSLEEP();
248 ifnet_byindex_ref(u_short idx)
252 IFNET_RLOCK_NOSLEEP();
253 ifp = ifnet_byindex_locked(idx);
254 if (ifp == NULL || (ifp->if_flags & IFF_DYING)) {
255 IFNET_RUNLOCK_NOSLEEP();
259 IFNET_RUNLOCK_NOSLEEP();
264 * Allocate an ifindex array entry; return 0 on success or an error on
268 ifindex_alloc_locked(u_short *idxp)
272 IFNET_WLOCK_ASSERT();
276 * Try to find an empty slot below V_if_index. If we fail, take the
279 for (idx = 1; idx <= V_if_index; idx++) {
280 if (V_ifindex_table[idx].ife_ifnet == NULL)
284 /* Catch if_index overflow. */
287 if (idx >= V_if_indexlim) {
291 if (idx > V_if_index)
298 ifindex_free_locked(u_short idx)
301 IFNET_WLOCK_ASSERT();
303 V_ifindex_table[idx].ife_ifnet = NULL;
304 while (V_if_index > 0 &&
305 V_ifindex_table[V_if_index].ife_ifnet == NULL)
310 ifindex_free(u_short idx)
314 ifindex_free_locked(idx);
319 ifnet_setbyindex_locked(u_short idx, struct ifnet *ifp)
322 IFNET_WLOCK_ASSERT();
324 V_ifindex_table[idx].ife_ifnet = ifp;
328 ifnet_setbyindex(u_short idx, struct ifnet *ifp)
332 ifnet_setbyindex_locked(idx, ifp);
337 ifaddr_byindex(u_short idx)
341 IFNET_RLOCK_NOSLEEP();
342 ifa = ifnet_byindex_locked(idx)->if_addr;
345 IFNET_RUNLOCK_NOSLEEP();
350 * Network interface utility routines.
352 * Routines with ifa_ifwith* names take sockaddr *'s as
357 vnet_if_init(const void *unused __unused)
360 TAILQ_INIT(&V_ifnet);
361 TAILQ_INIT(&V_ifg_head);
363 if_grow(); /* create initial table */
365 vnet_if_clone_init();
367 VNET_SYSINIT(vnet_if_init, SI_SUB_INIT_IF, SI_ORDER_SECOND, vnet_if_init,
372 if_init(void *dummy __unused)
378 SYSINIT(interfaces, SI_SUB_INIT_IF, SI_ORDER_FIRST, if_init, NULL);
383 vnet_if_uninit(const void *unused __unused)
386 VNET_ASSERT(TAILQ_EMPTY(&V_ifnet), ("%s:%d tailq &V_ifnet=%p "
387 "not empty", __func__, __LINE__, &V_ifnet));
388 VNET_ASSERT(TAILQ_EMPTY(&V_ifg_head), ("%s:%d tailq &V_ifg_head=%p "
389 "not empty", __func__, __LINE__, &V_ifg_head));
391 free((caddr_t)V_ifindex_table, M_IFNET);
393 VNET_SYSUNINIT(vnet_if_uninit, SI_SUB_INIT_IF, SI_ORDER_FIRST,
394 vnet_if_uninit, NULL);
402 struct ifindex_entry *e;
404 IFNET_WLOCK_ASSERT();
405 oldlim = V_if_indexlim;
407 n = (oldlim << 1) * sizeof(*e);
408 e = malloc(n, M_IFNET, M_WAITOK | M_ZERO);
410 if (V_if_indexlim != oldlim) {
414 if (V_ifindex_table != NULL) {
415 memcpy((caddr_t)e, (caddr_t)V_ifindex_table, n/2);
416 free((caddr_t)V_ifindex_table, M_IFNET);
423 * Allocate a struct ifnet and an index for an interface. A layer 2
424 * common structure will also be allocated if an allocation routine is
425 * registered for the passed type.
428 if_alloc(u_char type)
433 ifp = malloc(sizeof(struct ifnet), M_IFNET, M_WAITOK|M_ZERO);
435 if (ifindex_alloc_locked(&idx) != 0) {
440 ifnet_setbyindex_locked(idx, IFNET_HOLD);
444 ifp->if_alloctype = type;
445 if (if_com_alloc[type] != NULL) {
446 ifp->if_l2com = if_com_alloc[type](type, ifp);
447 if (ifp->if_l2com == NULL) {
454 IF_ADDR_LOCK_INIT(ifp);
455 TASK_INIT(&ifp->if_linktask, 0, do_link_state_change, ifp);
456 ifp->if_afdata_initialized = 0;
457 IF_AFDATA_LOCK_INIT(ifp);
458 TAILQ_INIT(&ifp->if_addrhead);
459 TAILQ_INIT(&ifp->if_multiaddrs);
460 TAILQ_INIT(&ifp->if_groups);
464 ifq_init(&ifp->if_snd, ifp);
466 refcount_init(&ifp->if_refcount, 1); /* Index reference. */
467 ifnet_setbyindex(ifp->if_index, ifp);
472 * Do the actual work of freeing a struct ifnet, and layer 2 common
473 * structure. This call is made when the last reference to an
474 * interface is released.
477 if_free_internal(struct ifnet *ifp)
480 KASSERT((ifp->if_flags & IFF_DYING),
481 ("if_free_internal: interface not dying"));
483 if (if_com_free[ifp->if_alloctype] != NULL)
484 if_com_free[ifp->if_alloctype](ifp->if_l2com,
488 mac_ifnet_destroy(ifp);
490 if (ifp->if_description != NULL)
491 free(ifp->if_description, M_IFDESCR);
492 IF_AFDATA_DESTROY(ifp);
493 IF_ADDR_LOCK_DESTROY(ifp);
494 ifq_delete(&ifp->if_snd);
499 * Deregister an interface and free the associated storage.
502 if_free(struct ifnet *ifp)
505 ifp->if_flags |= IFF_DYING; /* XXX: Locking */
508 KASSERT(ifp == ifnet_byindex_locked(ifp->if_index),
509 ("%s: freeing unallocated ifnet", ifp->if_xname));
511 ifindex_free_locked(ifp->if_index);
514 if (!refcount_release(&ifp->if_refcount))
516 if_free_internal(ifp);
520 * Interfaces to keep an ifnet type-stable despite the possibility of the
521 * driver calling if_free(). If there are additional references, we defer
522 * freeing the underlying data structure.
525 if_ref(struct ifnet *ifp)
528 /* We don't assert the ifnet list lock here, but arguably should. */
529 refcount_acquire(&ifp->if_refcount);
533 if_rele(struct ifnet *ifp)
536 if (!refcount_release(&ifp->if_refcount))
538 if_free_internal(ifp);
542 ifq_init(struct ifaltq *ifq, struct ifnet *ifp)
545 mtx_init(&ifq->ifq_mtx, ifp->if_xname, "if send queue", MTX_DEF);
547 if (ifq->ifq_maxlen == 0)
548 ifq->ifq_maxlen = ifqmaxlen;
551 ifq->altq_disc = NULL;
552 ifq->altq_flags &= ALTQF_CANTCHANGE;
553 ifq->altq_tbr = NULL;
558 ifq_delete(struct ifaltq *ifq)
560 mtx_destroy(&ifq->ifq_mtx);
564 * Perform generic interface initalization tasks and attach the interface
565 * to the list of "active" interfaces. If vmove flag is set on entry
566 * to if_attach_internal(), perform only a limited subset of initialization
567 * tasks, given that we are moving from one vnet to another an ifnet which
568 * has already been fully initialized.
571 * - The decision to return void and thus require this function to
572 * succeed is questionable.
573 * - We should probably do more sanity checking. For instance we don't
574 * do anything to insure if_xname is unique or non-empty.
577 if_attach(struct ifnet *ifp)
580 if_attach_internal(ifp, 0);
584 if_attach_internal(struct ifnet *ifp, int vmove)
586 unsigned socksize, ifasize;
587 int namelen, masklen;
588 struct sockaddr_dl *sdl;
591 if (ifp->if_index == 0 || ifp != ifnet_byindex(ifp->if_index))
592 panic ("%s: BUG: if_attach called without if_alloc'd input()\n",
596 ifp->if_vnet = curvnet;
597 if (ifp->if_home_vnet == NULL)
598 ifp->if_home_vnet = curvnet;
601 if_addgroup(ifp, IFG_ALL);
603 getmicrotime(&ifp->if_lastchange);
604 ifp->if_data.ifi_epoch = time_uptime;
605 ifp->if_data.ifi_datalen = sizeof(struct if_data);
607 KASSERT((ifp->if_transmit == NULL && ifp->if_qflush == NULL) ||
608 (ifp->if_transmit != NULL && ifp->if_qflush != NULL),
609 ("transmit and qflush must both either be set or both be NULL"));
610 if (ifp->if_transmit == NULL) {
611 ifp->if_transmit = if_transmit;
612 ifp->if_qflush = if_qflush;
617 mac_ifnet_create(ifp);
621 * Create a Link Level name for this device.
623 namelen = strlen(ifp->if_xname);
625 * Always save enough space for any possiable name so we
626 * can do a rename in place later.
628 masklen = offsetof(struct sockaddr_dl, sdl_data[0]) + IFNAMSIZ;
629 socksize = masklen + ifp->if_addrlen;
630 if (socksize < sizeof(*sdl))
631 socksize = sizeof(*sdl);
632 socksize = roundup2(socksize, sizeof(long));
633 ifasize = sizeof(*ifa) + 2 * socksize;
634 ifa = malloc(ifasize, M_IFADDR, M_WAITOK | M_ZERO);
636 sdl = (struct sockaddr_dl *)(ifa + 1);
637 sdl->sdl_len = socksize;
638 sdl->sdl_family = AF_LINK;
639 bcopy(ifp->if_xname, sdl->sdl_data, namelen);
640 sdl->sdl_nlen = namelen;
641 sdl->sdl_index = ifp->if_index;
642 sdl->sdl_type = ifp->if_type;
645 ifa->ifa_rtrequest = link_rtrequest;
646 ifa->ifa_addr = (struct sockaddr *)sdl;
647 sdl = (struct sockaddr_dl *)(socksize + (caddr_t)sdl);
648 ifa->ifa_netmask = (struct sockaddr *)sdl;
649 sdl->sdl_len = masklen;
651 sdl->sdl_data[--namelen] = 0xff;
652 TAILQ_INSERT_HEAD(&ifp->if_addrhead, ifa, ifa_link);
653 /* Reliably crash if used uninitialized. */
654 ifp->if_broadcastaddr = NULL;
659 * Update the interface index in the link layer address
662 for (ifa = ifp->if_addr; ifa != NULL;
663 ifa = TAILQ_NEXT(ifa, ifa_link)) {
664 if (ifa->ifa_addr->sa_family == AF_LINK) {
665 sdl = (struct sockaddr_dl *)ifa->ifa_addr;
666 sdl->sdl_index = ifp->if_index;
673 TAILQ_INSERT_TAIL(&V_ifnet, ifp, if_link);
675 curvnet->vnet_ifcnt++;
679 if (domain_init_status >= 2)
680 if_attachdomain1(ifp);
682 EVENTHANDLER_INVOKE(ifnet_arrival_event, ifp);
683 if (IS_DEFAULT_VNET(curvnet))
684 devctl_notify("IFNET", ifp->if_xname, "ATTACH", NULL);
686 /* Announce the interface. */
687 rt_ifannouncemsg(ifp, IFAN_ARRIVAL);
691 if_attachdomain(void *dummy)
697 TAILQ_FOREACH(ifp, &V_ifnet, if_link)
698 if_attachdomain1(ifp);
701 SYSINIT(domainifattach, SI_SUB_PROTO_IFATTACHDOMAIN, SI_ORDER_SECOND,
702 if_attachdomain, NULL);
705 if_attachdomain1(struct ifnet *ifp)
713 * Since dp->dom_ifattach calls malloc() with M_WAITOK, we
714 * cannot lock ifp->if_afdata initialization, entirely.
716 if (IF_AFDATA_TRYLOCK(ifp) == 0) {
720 if (ifp->if_afdata_initialized >= domain_init_status) {
721 IF_AFDATA_UNLOCK(ifp);
723 printf("if_attachdomain called more than once on %s\n",
727 ifp->if_afdata_initialized = domain_init_status;
728 IF_AFDATA_UNLOCK(ifp);
730 /* address family dependent data region */
731 bzero(ifp->if_afdata, sizeof(ifp->if_afdata));
732 for (dp = domains; dp; dp = dp->dom_next) {
733 if (dp->dom_ifattach)
734 ifp->if_afdata[dp->dom_family] =
735 (*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 if_detach_internal(ifp, 0);
811 if_detach_internal(struct ifnet *ifp, int vmove)
814 struct radix_node_head *rnh;
821 TAILQ_FOREACH(iter, &V_ifnet, if_link)
823 TAILQ_REMOVE(&V_ifnet, ifp, if_link);
829 curvnet->vnet_ifcnt--;
834 panic("%s: ifp=%p not on the ifnet tailq %p",
835 __func__, ifp, &V_ifnet);
837 return; /* XXX this should panic as well? */
841 * Remove/wait for pending events.
843 taskqueue_drain(taskqueue_swi, &ifp->if_linktask);
846 * Remove routes and flush queues.
850 if (ALTQ_IS_ENABLED(&ifp->if_snd))
851 altq_disable(&ifp->if_snd);
852 if (ALTQ_IS_ATTACHED(&ifp->if_snd))
853 altq_detach(&ifp->if_snd);
864 * Remove all IPv6 kernel structs related to ifp. This should be done
865 * before removing routing entries below, since IPv6 interface direct
866 * routes are expected to be removed by the IPv6-specific kernel API.
867 * Otherwise, the kernel will detect some inconsistency and bark it.
875 * Prevent further calls into the device driver via ifnet.
880 * Remove link ifaddr pointer and maybe decrement if_index.
881 * Clean up all addresses.
885 /* We can now free link ifaddr. */
886 if (!TAILQ_EMPTY(&ifp->if_addrhead)) {
887 ifa = TAILQ_FIRST(&ifp->if_addrhead);
888 TAILQ_REMOVE(&ifp->if_addrhead, ifa, ifa_link);
894 * Delete all remaining routes using this interface
895 * Unfortuneatly the only way to do this is to slog through
896 * the entire routing table looking for routes which point
897 * to this interface...oh well...
899 for (i = 1; i <= AF_MAX; i++) {
900 for (j = 0; j < rt_numfibs; j++) {
901 rnh = rt_tables_get_rnh(j, i);
904 RADIX_NODE_HEAD_LOCK(rnh);
905 (void) rnh->rnh_walktree(rnh, if_rtdel, ifp);
906 RADIX_NODE_HEAD_UNLOCK(rnh);
910 /* Announce that the interface is gone. */
911 rt_ifannouncemsg(ifp, IFAN_DEPARTURE);
912 EVENTHANDLER_INVOKE(ifnet_departure_event, ifp);
913 if (IS_DEFAULT_VNET(curvnet))
914 devctl_notify("IFNET", ifp->if_xname, "DETACH", NULL);
918 * We cannot hold the lock over dom_ifdetach calls as they might
919 * sleep, for example trying to drain a callout, thus open up the
920 * theoretical race with re-attaching.
923 i = ifp->if_afdata_initialized;
924 ifp->if_afdata_initialized = 0;
925 IF_AFDATA_UNLOCK(ifp);
926 for (dp = domains; i > 0 && dp; dp = dp->dom_next) {
927 if (dp->dom_ifdetach && ifp->if_afdata[dp->dom_family])
928 (*dp->dom_ifdetach)(ifp,
929 ifp->if_afdata[dp->dom_family]);
935 * if_vmove() performs a limited version of if_detach() in current
936 * vnet and if_attach()es the ifnet to the vnet specified as 2nd arg.
937 * An attempt is made to shrink if_index in current vnet, find an
938 * unused if_index in target vnet and calls if_grow() if necessary,
939 * and finally find an unused if_xname for the target vnet.
942 if_vmove(struct ifnet *ifp, struct vnet *new_vnet)
947 * Detach from current vnet, but preserve LLADDR info, do not
948 * mark as dead etc. so that the ifnet can be reattached later.
950 if_detach_internal(ifp, 1);
953 * Unlink the ifnet from ifindex_table[] in current vnet, and shrink
954 * the if_index for that vnet if possible.
956 * NOTE: IFNET_WLOCK/IFNET_WUNLOCK() are assumed to be unvirtualized,
957 * or we'd lock on one vnet and unlock on another.
960 ifindex_free_locked(ifp->if_index);
964 * Perform interface-specific reassignment tasks, if provided by
967 if (ifp->if_reassign != NULL)
968 ifp->if_reassign(ifp, new_vnet, NULL);
971 * Switch to the context of the target vnet.
973 CURVNET_SET_QUIET(new_vnet);
976 if (ifindex_alloc_locked(&idx) != 0) {
978 panic("if_index overflow");
981 ifnet_setbyindex_locked(ifp->if_index, ifp);
984 if_attach_internal(ifp, 1);
990 * Move an ifnet to or from another child prison/vnet, specified by the jail id.
993 if_vmove_loan(struct thread *td, struct ifnet *ifp, char *ifname, int jid)
998 /* Try to find the prison within our visibility. */
999 sx_slock(&allprison_lock);
1000 pr = prison_find_child(td->td_ucred->cr_prison, jid);
1001 sx_sunlock(&allprison_lock);
1004 prison_hold_locked(pr);
1005 mtx_unlock(&pr->pr_mtx);
1007 /* Do not try to move the iface from and to the same prison. */
1008 if (pr->pr_vnet == ifp->if_vnet) {
1013 /* Make sure the named iface does not exists in the dst. prison/vnet. */
1014 /* XXX Lock interfaces to avoid races. */
1015 CURVNET_SET_QUIET(pr->pr_vnet);
1016 difp = ifunit(ifname);
1023 /* Move the interface into the child jail/vnet. */
1024 if_vmove(ifp, pr->pr_vnet);
1026 /* Report the new if_xname back to the userland. */
1027 sprintf(ifname, "%s", ifp->if_xname);
1034 if_vmove_reclaim(struct thread *td, char *ifname, int jid)
1037 struct vnet *vnet_dst;
1040 /* Try to find the prison within our visibility. */
1041 sx_slock(&allprison_lock);
1042 pr = prison_find_child(td->td_ucred->cr_prison, jid);
1043 sx_sunlock(&allprison_lock);
1046 prison_hold_locked(pr);
1047 mtx_unlock(&pr->pr_mtx);
1049 /* Make sure the named iface exists in the source prison/vnet. */
1050 CURVNET_SET(pr->pr_vnet);
1051 ifp = ifunit(ifname); /* XXX Lock to avoid races. */
1058 /* Do not try to move the iface from and to the same prison. */
1059 vnet_dst = TD_TO_VNET(td);
1060 if (vnet_dst == ifp->if_vnet) {
1066 /* Get interface back from child jail/vnet. */
1067 if_vmove(ifp, vnet_dst);
1070 /* Report the new if_xname back to the userland. */
1071 sprintf(ifname, "%s", ifp->if_xname);
1079 * Add a group to an interface
1082 if_addgroup(struct ifnet *ifp, const char *groupname)
1084 struct ifg_list *ifgl;
1085 struct ifg_group *ifg = NULL;
1086 struct ifg_member *ifgm;
1088 if (groupname[0] && groupname[strlen(groupname) - 1] >= '0' &&
1089 groupname[strlen(groupname) - 1] <= '9')
1093 TAILQ_FOREACH(ifgl, &ifp->if_groups, ifgl_next)
1094 if (!strcmp(ifgl->ifgl_group->ifg_group, groupname)) {
1099 if ((ifgl = (struct ifg_list *)malloc(sizeof(struct ifg_list), M_TEMP,
1100 M_NOWAIT)) == NULL) {
1105 if ((ifgm = (struct ifg_member *)malloc(sizeof(struct ifg_member),
1106 M_TEMP, M_NOWAIT)) == NULL) {
1112 TAILQ_FOREACH(ifg, &V_ifg_head, ifg_next)
1113 if (!strcmp(ifg->ifg_group, groupname))
1117 if ((ifg = (struct ifg_group *)malloc(sizeof(struct ifg_group),
1118 M_TEMP, M_NOWAIT)) == NULL) {
1124 strlcpy(ifg->ifg_group, groupname, sizeof(ifg->ifg_group));
1125 ifg->ifg_refcnt = 0;
1126 TAILQ_INIT(&ifg->ifg_members);
1127 EVENTHANDLER_INVOKE(group_attach_event, ifg);
1128 TAILQ_INSERT_TAIL(&V_ifg_head, ifg, ifg_next);
1132 ifgl->ifgl_group = ifg;
1133 ifgm->ifgm_ifp = ifp;
1136 TAILQ_INSERT_TAIL(&ifg->ifg_members, ifgm, ifgm_next);
1137 TAILQ_INSERT_TAIL(&ifp->if_groups, ifgl, ifgl_next);
1138 IF_ADDR_WUNLOCK(ifp);
1142 EVENTHANDLER_INVOKE(group_change_event, groupname);
1148 * Remove a group from an interface
1151 if_delgroup(struct ifnet *ifp, const char *groupname)
1153 struct ifg_list *ifgl;
1154 struct ifg_member *ifgm;
1157 TAILQ_FOREACH(ifgl, &ifp->if_groups, ifgl_next)
1158 if (!strcmp(ifgl->ifgl_group->ifg_group, groupname))
1166 TAILQ_REMOVE(&ifp->if_groups, ifgl, ifgl_next);
1167 IF_ADDR_WUNLOCK(ifp);
1169 TAILQ_FOREACH(ifgm, &ifgl->ifgl_group->ifg_members, ifgm_next)
1170 if (ifgm->ifgm_ifp == ifp)
1174 TAILQ_REMOVE(&ifgl->ifgl_group->ifg_members, ifgm, ifgm_next);
1178 if (--ifgl->ifgl_group->ifg_refcnt == 0) {
1179 TAILQ_REMOVE(&V_ifg_head, ifgl->ifgl_group, ifg_next);
1180 EVENTHANDLER_INVOKE(group_detach_event, ifgl->ifgl_group);
1181 free(ifgl->ifgl_group, M_TEMP);
1187 EVENTHANDLER_INVOKE(group_change_event, groupname);
1193 * Remove an interface from all groups
1196 if_delgroups(struct ifnet *ifp)
1198 struct ifg_list *ifgl;
1199 struct ifg_member *ifgm;
1200 char groupname[IFNAMSIZ];
1203 while (!TAILQ_EMPTY(&ifp->if_groups)) {
1204 ifgl = TAILQ_FIRST(&ifp->if_groups);
1206 strlcpy(groupname, ifgl->ifgl_group->ifg_group, IFNAMSIZ);
1209 TAILQ_REMOVE(&ifp->if_groups, ifgl, ifgl_next);
1210 IF_ADDR_WUNLOCK(ifp);
1212 TAILQ_FOREACH(ifgm, &ifgl->ifgl_group->ifg_members, ifgm_next)
1213 if (ifgm->ifgm_ifp == ifp)
1217 TAILQ_REMOVE(&ifgl->ifgl_group->ifg_members, ifgm,
1222 if (--ifgl->ifgl_group->ifg_refcnt == 0) {
1223 TAILQ_REMOVE(&V_ifg_head, ifgl->ifgl_group, ifg_next);
1224 EVENTHANDLER_INVOKE(group_detach_event,
1226 free(ifgl->ifgl_group, M_TEMP);
1232 EVENTHANDLER_INVOKE(group_change_event, groupname);
1240 * Stores all groups from an interface in memory pointed
1244 if_getgroup(struct ifgroupreq *data, struct ifnet *ifp)
1247 struct ifg_list *ifgl;
1248 struct ifg_req ifgrq, *ifgp;
1249 struct ifgroupreq *ifgr = data;
1251 if (ifgr->ifgr_len == 0) {
1253 TAILQ_FOREACH(ifgl, &ifp->if_groups, ifgl_next)
1254 ifgr->ifgr_len += sizeof(struct ifg_req);
1255 IF_ADDR_RUNLOCK(ifp);
1259 len = ifgr->ifgr_len;
1260 ifgp = ifgr->ifgr_groups;
1263 TAILQ_FOREACH(ifgl, &ifp->if_groups, ifgl_next) {
1264 if (len < sizeof(ifgrq)) {
1265 IF_ADDR_RUNLOCK(ifp);
1268 bzero(&ifgrq, sizeof ifgrq);
1269 strlcpy(ifgrq.ifgrq_group, ifgl->ifgl_group->ifg_group,
1270 sizeof(ifgrq.ifgrq_group));
1271 if ((error = copyout(&ifgrq, ifgp, sizeof(struct ifg_req)))) {
1272 IF_ADDR_RUNLOCK(ifp);
1275 len -= sizeof(ifgrq);
1278 IF_ADDR_RUNLOCK(ifp);
1284 * Stores all members of a group in memory pointed to by data
1287 if_getgroupmembers(struct ifgroupreq *data)
1289 struct ifgroupreq *ifgr = data;
1290 struct ifg_group *ifg;
1291 struct ifg_member *ifgm;
1292 struct ifg_req ifgrq, *ifgp;
1296 TAILQ_FOREACH(ifg, &V_ifg_head, ifg_next)
1297 if (!strcmp(ifg->ifg_group, ifgr->ifgr_name))
1304 if (ifgr->ifgr_len == 0) {
1305 TAILQ_FOREACH(ifgm, &ifg->ifg_members, ifgm_next)
1306 ifgr->ifgr_len += sizeof(ifgrq);
1311 len = ifgr->ifgr_len;
1312 ifgp = ifgr->ifgr_groups;
1313 TAILQ_FOREACH(ifgm, &ifg->ifg_members, ifgm_next) {
1314 if (len < sizeof(ifgrq)) {
1318 bzero(&ifgrq, sizeof ifgrq);
1319 strlcpy(ifgrq.ifgrq_member, ifgm->ifgm_ifp->if_xname,
1320 sizeof(ifgrq.ifgrq_member));
1321 if ((error = copyout(&ifgrq, ifgp, sizeof(struct ifg_req)))) {
1325 len -= sizeof(ifgrq);
1334 * Delete Routes for a Network Interface
1336 * Called for each routing entry via the rnh->rnh_walktree() call above
1337 * to delete all route entries referencing a detaching network interface.
1340 * rn pointer to node in the routing table
1341 * arg argument passed to rnh->rnh_walktree() - detaching interface
1345 * errno failed - reason indicated
1349 if_rtdel(struct radix_node *rn, void *arg)
1351 struct rtentry *rt = (struct rtentry *)rn;
1352 struct ifnet *ifp = arg;
1355 if (rt->rt_ifp == ifp) {
1358 * Protect (sorta) against walktree recursion problems
1359 * with cloned routes
1361 if ((rt->rt_flags & RTF_UP) == 0)
1364 err = rtrequest_fib(RTM_DELETE, rt_key(rt), rt->rt_gateway,
1365 rt_mask(rt), rt->rt_flags|RTF_RNH_LOCKED,
1366 (struct rtentry **) NULL, rt->rt_fibnum);
1368 log(LOG_WARNING, "if_rtdel: error %d\n", err);
1376 * Wrapper functions for struct ifnet address list locking macros. These are
1377 * used by kernel modules to avoid encoding programming interface or binary
1378 * interface assumptions that may be violated when kernel-internal locking
1379 * approaches change.
1382 if_addr_rlock(struct ifnet *ifp)
1389 if_addr_runlock(struct ifnet *ifp)
1392 IF_ADDR_RUNLOCK(ifp);
1396 if_maddr_rlock(struct ifnet *ifp)
1403 if_maddr_runlock(struct ifnet *ifp)
1406 IF_ADDR_RUNLOCK(ifp);
1410 * Initialization, destruction and refcounting functions for ifaddrs.
1413 ifa_init(struct ifaddr *ifa)
1416 mtx_init(&ifa->ifa_mtx, "ifaddr", NULL, MTX_DEF);
1417 refcount_init(&ifa->ifa_refcnt, 1);
1418 ifa->if_data.ifi_datalen = sizeof(ifa->if_data);
1422 ifa_ref(struct ifaddr *ifa)
1425 refcount_acquire(&ifa->ifa_refcnt);
1429 ifa_free(struct ifaddr *ifa)
1432 if (refcount_release(&ifa->ifa_refcnt)) {
1433 mtx_destroy(&ifa->ifa_mtx);
1434 free(ifa, M_IFADDR);
1439 ifa_add_loopback_route(struct ifaddr *ifa, struct sockaddr *ia)
1442 struct rtentry *rt = NULL;
1443 struct rt_addrinfo info;
1444 static struct sockaddr_dl null_sdl = {sizeof(null_sdl), AF_LINK};
1446 bzero(&info, sizeof(info));
1447 info.rti_ifp = V_loif;
1448 info.rti_flags = ifa->ifa_flags | RTF_HOST | RTF_STATIC;
1449 info.rti_info[RTAX_DST] = ia;
1450 info.rti_info[RTAX_GATEWAY] = (struct sockaddr *)&null_sdl;
1451 error = rtrequest1_fib(RTM_ADD, &info, &rt, 0);
1453 if (error == 0 && rt != NULL) {
1455 ((struct sockaddr_dl *)rt->rt_gateway)->sdl_type =
1456 ifa->ifa_ifp->if_type;
1457 ((struct sockaddr_dl *)rt->rt_gateway)->sdl_index =
1458 ifa->ifa_ifp->if_index;
1461 } else if (error != 0)
1462 log(LOG_DEBUG, "%s: insertion failed: %u\n", __func__, error);
1468 ifa_del_loopback_route(struct ifaddr *ifa, struct sockaddr *ia)
1471 struct rt_addrinfo info;
1472 struct sockaddr_dl null_sdl;
1474 bzero(&null_sdl, sizeof(null_sdl));
1475 null_sdl.sdl_len = sizeof(null_sdl);
1476 null_sdl.sdl_family = AF_LINK;
1477 null_sdl.sdl_type = ifa->ifa_ifp->if_type;
1478 null_sdl.sdl_index = ifa->ifa_ifp->if_index;
1479 bzero(&info, sizeof(info));
1480 info.rti_flags = ifa->ifa_flags | RTF_HOST | RTF_STATIC;
1481 info.rti_info[RTAX_DST] = ia;
1482 info.rti_info[RTAX_GATEWAY] = (struct sockaddr *)&null_sdl;
1483 error = rtrequest1_fib(RTM_DELETE, &info, NULL, 0);
1486 log(LOG_DEBUG, "%s: deletion failed: %u\n", __func__, error);
1492 * XXX: Because sockaddr_dl has deeper structure than the sockaddr
1493 * structs used to represent other address families, it is necessary
1494 * to perform a different comparison.
1497 #define sa_equal(a1, a2) \
1498 (bcmp((a1), (a2), ((a1))->sa_len) == 0)
1500 #define sa_dl_equal(a1, a2) \
1501 ((((struct sockaddr_dl *)(a1))->sdl_len == \
1502 ((struct sockaddr_dl *)(a2))->sdl_len) && \
1503 (bcmp(LLADDR((struct sockaddr_dl *)(a1)), \
1504 LLADDR((struct sockaddr_dl *)(a2)), \
1505 ((struct sockaddr_dl *)(a1))->sdl_alen) == 0))
1508 * Locate an interface based on a complete address.
1511 static struct ifaddr *
1512 ifa_ifwithaddr_internal(struct sockaddr *addr, int getref)
1517 IFNET_RLOCK_NOSLEEP();
1518 TAILQ_FOREACH(ifp, &V_ifnet, if_link) {
1520 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
1521 if (ifa->ifa_addr->sa_family != addr->sa_family)
1523 if (sa_equal(addr, ifa->ifa_addr)) {
1526 IF_ADDR_RUNLOCK(ifp);
1529 /* IP6 doesn't have broadcast */
1530 if ((ifp->if_flags & IFF_BROADCAST) &&
1531 ifa->ifa_broadaddr &&
1532 ifa->ifa_broadaddr->sa_len != 0 &&
1533 sa_equal(ifa->ifa_broadaddr, addr)) {
1536 IF_ADDR_RUNLOCK(ifp);
1540 IF_ADDR_RUNLOCK(ifp);
1544 IFNET_RUNLOCK_NOSLEEP();
1549 ifa_ifwithaddr(struct sockaddr *addr)
1552 return (ifa_ifwithaddr_internal(addr, 1));
1556 ifa_ifwithaddr_check(struct sockaddr *addr)
1559 return (ifa_ifwithaddr_internal(addr, 0) != NULL);
1563 * Locate an interface based on the broadcast address.
1567 ifa_ifwithbroadaddr(struct sockaddr *addr)
1572 IFNET_RLOCK_NOSLEEP();
1573 TAILQ_FOREACH(ifp, &V_ifnet, if_link) {
1575 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
1576 if (ifa->ifa_addr->sa_family != addr->sa_family)
1578 if ((ifp->if_flags & IFF_BROADCAST) &&
1579 ifa->ifa_broadaddr &&
1580 ifa->ifa_broadaddr->sa_len != 0 &&
1581 sa_equal(ifa->ifa_broadaddr, addr)) {
1583 IF_ADDR_RUNLOCK(ifp);
1587 IF_ADDR_RUNLOCK(ifp);
1591 IFNET_RUNLOCK_NOSLEEP();
1596 * Locate the point to point interface with a given destination address.
1600 ifa_ifwithdstaddr(struct sockaddr *addr)
1605 IFNET_RLOCK_NOSLEEP();
1606 TAILQ_FOREACH(ifp, &V_ifnet, if_link) {
1607 if ((ifp->if_flags & IFF_POINTOPOINT) == 0)
1610 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
1611 if (ifa->ifa_addr->sa_family != addr->sa_family)
1613 if (ifa->ifa_dstaddr != NULL &&
1614 sa_equal(addr, ifa->ifa_dstaddr)) {
1616 IF_ADDR_RUNLOCK(ifp);
1620 IF_ADDR_RUNLOCK(ifp);
1624 IFNET_RUNLOCK_NOSLEEP();
1629 * Find an interface on a specific network. If many, choice
1630 * is most specific found.
1633 ifa_ifwithnet(struct sockaddr *addr, int ignore_ptp)
1637 struct ifaddr *ifa_maybe = NULL;
1638 u_int af = addr->sa_family;
1639 char *addr_data = addr->sa_data, *cplim;
1642 * AF_LINK addresses can be looked up directly by their index number,
1643 * so do that if we can.
1645 if (af == AF_LINK) {
1646 struct sockaddr_dl *sdl = (struct sockaddr_dl *)addr;
1647 if (sdl->sdl_index && sdl->sdl_index <= V_if_index)
1648 return (ifaddr_byindex(sdl->sdl_index));
1652 * Scan though each interface, looking for ones that have addresses
1653 * in this address family. Maintain a reference on ifa_maybe once
1654 * we find one, as we release the IF_ADDR_RLOCK() that kept it stable
1655 * when we move onto the next interface.
1657 IFNET_RLOCK_NOSLEEP();
1658 TAILQ_FOREACH(ifp, &V_ifnet, if_link) {
1660 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
1661 char *cp, *cp2, *cp3;
1663 if (ifa->ifa_addr->sa_family != af)
1665 if (af == AF_INET &&
1666 ifp->if_flags & IFF_POINTOPOINT && !ignore_ptp) {
1668 * This is a bit broken as it doesn't
1669 * take into account that the remote end may
1670 * be a single node in the network we are
1672 * The trouble is that we don't know the
1673 * netmask for the remote end.
1675 if (ifa->ifa_dstaddr != NULL &&
1676 sa_equal(addr, ifa->ifa_dstaddr)) {
1678 IF_ADDR_RUNLOCK(ifp);
1683 * if we have a special address handler,
1684 * then use it instead of the generic one.
1686 if (ifa->ifa_claim_addr) {
1687 if ((*ifa->ifa_claim_addr)(ifa, addr)) {
1689 IF_ADDR_RUNLOCK(ifp);
1696 * Scan all the bits in the ifa's address.
1697 * If a bit dissagrees with what we are
1698 * looking for, mask it with the netmask
1699 * to see if it really matters.
1700 * (A byte at a time)
1702 if (ifa->ifa_netmask == 0)
1705 cp2 = ifa->ifa_addr->sa_data;
1706 cp3 = ifa->ifa_netmask->sa_data;
1707 cplim = ifa->ifa_netmask->sa_len
1708 + (char *)ifa->ifa_netmask;
1710 if ((*cp++ ^ *cp2++) & *cp3++)
1711 goto next; /* next address! */
1713 * If the netmask of what we just found
1714 * is more specific than what we had before
1715 * (if we had one) then remember the new one
1716 * before continuing to search
1717 * for an even better one.
1719 if (ifa_maybe == NULL ||
1720 rn_refines((caddr_t)ifa->ifa_netmask,
1721 (caddr_t)ifa_maybe->ifa_netmask)) {
1722 if (ifa_maybe != NULL)
1723 ifa_free(ifa_maybe);
1729 IF_ADDR_RUNLOCK(ifp);
1734 IFNET_RUNLOCK_NOSLEEP();
1735 if (ifa_maybe != NULL)
1736 ifa_free(ifa_maybe);
1741 * Find an interface address specific to an interface best matching
1745 ifaof_ifpforaddr(struct sockaddr *addr, struct ifnet *ifp)
1748 char *cp, *cp2, *cp3;
1750 struct ifaddr *ifa_maybe = NULL;
1751 u_int af = addr->sa_family;
1756 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
1757 if (ifa->ifa_addr->sa_family != af)
1759 if (ifa_maybe == NULL)
1761 if (ifa->ifa_netmask == 0) {
1762 if (sa_equal(addr, ifa->ifa_addr) ||
1763 (ifa->ifa_dstaddr &&
1764 sa_equal(addr, ifa->ifa_dstaddr)))
1768 if (ifp->if_flags & IFF_POINTOPOINT) {
1769 if (sa_equal(addr, ifa->ifa_dstaddr))
1773 cp2 = ifa->ifa_addr->sa_data;
1774 cp3 = ifa->ifa_netmask->sa_data;
1775 cplim = ifa->ifa_netmask->sa_len + (char *)ifa->ifa_netmask;
1776 for (; cp3 < cplim; cp3++)
1777 if ((*cp++ ^ *cp2++) & *cp3)
1787 IF_ADDR_RUNLOCK(ifp);
1791 #include <net/if_llatbl.h>
1794 * Default action when installing a route with a Link Level gateway.
1795 * Lookup an appropriate real ifa to point to.
1796 * This should be moved to /sys/net/link.c eventually.
1799 link_rtrequest(int cmd, struct rtentry *rt, struct rt_addrinfo *info)
1801 struct ifaddr *ifa, *oifa;
1802 struct sockaddr *dst;
1807 if (cmd != RTM_ADD || ((ifa = rt->rt_ifa) == 0) ||
1808 ((ifp = ifa->ifa_ifp) == 0) || ((dst = rt_key(rt)) == 0))
1810 ifa = ifaof_ifpforaddr(dst, ifp);
1815 if (ifa->ifa_rtrequest && ifa->ifa_rtrequest != link_rtrequest)
1816 ifa->ifa_rtrequest(cmd, rt, info);
1821 * Mark an interface down and notify protocols of
1823 * NOTE: must be called at splnet or eqivalent.
1826 if_unroute(struct ifnet *ifp, int flag, int fam)
1830 KASSERT(flag == IFF_UP, ("if_unroute: flag != IFF_UP"));
1832 ifp->if_flags &= ~flag;
1833 getmicrotime(&ifp->if_lastchange);
1834 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link)
1835 if (fam == PF_UNSPEC || (fam == ifa->ifa_addr->sa_family))
1836 pfctlinput(PRC_IFDOWN, ifa->ifa_addr);
1837 ifp->if_qflush(ifp);
1840 (*carp_linkstate_p)(ifp);
1845 * Mark an interface up and notify protocols of
1847 * NOTE: must be called at splnet or eqivalent.
1850 if_route(struct ifnet *ifp, int flag, int fam)
1854 KASSERT(flag == IFF_UP, ("if_route: 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_IFUP, ifa->ifa_addr);
1862 (*carp_linkstate_p)(ifp);
1869 void (*vlan_link_state_p)(struct ifnet *); /* XXX: private from if_vlan */
1870 void (*vlan_trunk_cap_p)(struct ifnet *); /* XXX: private from if_vlan */
1871 struct ifnet *(*vlan_trunkdev_p)(struct ifnet *);
1872 struct ifnet *(*vlan_devat_p)(struct ifnet *, uint16_t);
1873 int (*vlan_tag_p)(struct ifnet *, uint16_t *);
1874 int (*vlan_setcookie_p)(struct ifnet *, void *);
1875 void *(*vlan_cookie_p)(struct ifnet *);
1878 * Handle a change in the interface link state. To avoid LORs
1879 * between driver lock and upper layer locks, as well as possible
1880 * recursions, we post event to taskqueue, and all job
1881 * is done in static do_link_state_change().
1884 if_link_state_change(struct ifnet *ifp, int link_state)
1886 /* Return if state hasn't changed. */
1887 if (ifp->if_link_state == link_state)
1890 ifp->if_link_state = link_state;
1892 taskqueue_enqueue(taskqueue_swi, &ifp->if_linktask);
1896 do_link_state_change(void *arg, int pending)
1898 struct ifnet *ifp = (struct ifnet *)arg;
1899 int link_state = ifp->if_link_state;
1900 CURVNET_SET(ifp->if_vnet);
1902 /* Notify that the link state has changed. */
1904 if (ifp->if_vlantrunk != NULL)
1905 (*vlan_link_state_p)(ifp);
1907 if ((ifp->if_type == IFT_ETHER || ifp->if_type == IFT_L2VLAN) &&
1908 IFP2AC(ifp)->ac_netgraph != NULL)
1909 (*ng_ether_link_state_p)(ifp, link_state);
1911 (*carp_linkstate_p)(ifp);
1913 (*bridge_linkstate_p)(ifp);
1915 (*lagg_linkstate_p)(ifp, link_state);
1917 if (IS_DEFAULT_VNET(curvnet))
1918 devctl_notify("IFNET", ifp->if_xname,
1919 (link_state == LINK_STATE_UP) ? "LINK_UP" : "LINK_DOWN",
1922 if_printf(ifp, "%d link states coalesced\n", pending);
1923 if (log_link_state_change)
1924 log(LOG_NOTICE, "%s: link state changed to %s\n", ifp->if_xname,
1925 (link_state == LINK_STATE_UP) ? "UP" : "DOWN" );
1926 EVENTHANDLER_INVOKE(ifnet_link_event, ifp, ifp->if_link_state);
1931 * Mark an interface down and notify protocols of
1933 * NOTE: must be called at splnet or eqivalent.
1936 if_down(struct ifnet *ifp)
1939 if_unroute(ifp, IFF_UP, AF_UNSPEC);
1943 * Mark an interface up and notify protocols of
1945 * NOTE: must be called at splnet or eqivalent.
1948 if_up(struct ifnet *ifp)
1951 if_route(ifp, IFF_UP, AF_UNSPEC);
1955 * Flush an interface queue.
1958 if_qflush(struct ifnet *ifp)
1966 if (ALTQ_IS_ENABLED(ifq))
1970 while ((m = n) != 0) {
1981 * Map interface name to interface structure pointer, with or without
1982 * returning a reference.
1985 ifunit_ref(const char *name)
1989 IFNET_RLOCK_NOSLEEP();
1990 TAILQ_FOREACH(ifp, &V_ifnet, if_link) {
1991 if (strncmp(name, ifp->if_xname, IFNAMSIZ) == 0 &&
1992 !(ifp->if_flags & IFF_DYING))
1997 IFNET_RUNLOCK_NOSLEEP();
2002 ifunit(const char *name)
2006 IFNET_RLOCK_NOSLEEP();
2007 TAILQ_FOREACH(ifp, &V_ifnet, if_link) {
2008 if (strncmp(name, ifp->if_xname, IFNAMSIZ) == 0)
2011 IFNET_RUNLOCK_NOSLEEP();
2016 * Hardware specific interface ioctls.
2019 ifhwioctl(u_long cmd, struct ifnet *ifp, caddr_t data, struct thread *td)
2024 int new_flags, temp_flags;
2025 size_t namelen, onamelen;
2027 char *descrbuf, *odescrbuf;
2028 char new_name[IFNAMSIZ];
2030 struct sockaddr_dl *sdl;
2032 ifr = (struct ifreq *)data;
2035 ifr->ifr_index = ifp->if_index;
2039 temp_flags = ifp->if_flags | ifp->if_drv_flags;
2040 ifr->ifr_flags = temp_flags & 0xffff;
2041 ifr->ifr_flagshigh = temp_flags >> 16;
2045 ifr->ifr_reqcap = ifp->if_capabilities;
2046 ifr->ifr_curcap = ifp->if_capenable;
2051 error = mac_ifnet_ioctl_get(td->td_ucred, ifr, ifp);
2056 ifr->ifr_metric = ifp->if_metric;
2060 ifr->ifr_mtu = ifp->if_mtu;
2064 ifr->ifr_phys = ifp->if_physical;
2069 sx_slock(&ifdescr_sx);
2070 if (ifp->if_description == NULL)
2073 /* space for terminating nul */
2074 descrlen = strlen(ifp->if_description) + 1;
2075 if (ifr->ifr_buffer.length < descrlen)
2076 ifr->ifr_buffer.buffer = NULL;
2078 error = copyout(ifp->if_description,
2079 ifr->ifr_buffer.buffer, descrlen);
2080 ifr->ifr_buffer.length = descrlen;
2082 sx_sunlock(&ifdescr_sx);
2086 error = priv_check(td, PRIV_NET_SETIFDESCR);
2091 * Copy only (length-1) bytes to make sure that
2092 * if_description is always nul terminated. The
2093 * length parameter is supposed to count the
2094 * terminating nul in.
2096 if (ifr->ifr_buffer.length > ifdescr_maxlen)
2097 return (ENAMETOOLONG);
2098 else if (ifr->ifr_buffer.length == 0)
2101 descrbuf = malloc(ifr->ifr_buffer.length, M_IFDESCR,
2103 error = copyin(ifr->ifr_buffer.buffer, descrbuf,
2104 ifr->ifr_buffer.length - 1);
2106 free(descrbuf, M_IFDESCR);
2111 sx_xlock(&ifdescr_sx);
2112 odescrbuf = ifp->if_description;
2113 ifp->if_description = descrbuf;
2114 sx_xunlock(&ifdescr_sx);
2116 getmicrotime(&ifp->if_lastchange);
2117 free(odescrbuf, M_IFDESCR);
2121 ifr->ifr_fib = ifp->if_fib;
2125 error = priv_check(td, PRIV_NET_SETIFFIB);
2128 if (ifr->ifr_fib >= rt_numfibs)
2131 ifp->if_fib = ifr->ifr_fib;
2135 error = priv_check(td, PRIV_NET_SETIFFLAGS);
2139 * Currently, no driver owned flags pass the IFF_CANTCHANGE
2140 * check, so we don't need special handling here yet.
2142 new_flags = (ifr->ifr_flags & 0xffff) |
2143 (ifr->ifr_flagshigh << 16);
2144 if (ifp->if_flags & IFF_SMART) {
2145 /* Smart drivers twiddle their own routes */
2146 } else if (ifp->if_flags & IFF_UP &&
2147 (new_flags & IFF_UP) == 0) {
2151 } else if (new_flags & IFF_UP &&
2152 (ifp->if_flags & IFF_UP) == 0) {
2157 /* See if permanently promiscuous mode bit is about to flip */
2158 if ((ifp->if_flags ^ new_flags) & IFF_PPROMISC) {
2159 if (new_flags & IFF_PPROMISC)
2160 ifp->if_flags |= IFF_PROMISC;
2161 else if (ifp->if_pcount == 0)
2162 ifp->if_flags &= ~IFF_PROMISC;
2163 log(LOG_INFO, "%s: permanently promiscuous mode %s\n",
2165 (new_flags & IFF_PPROMISC) ? "enabled" : "disabled");
2167 ifp->if_flags = (ifp->if_flags & IFF_CANTCHANGE) |
2168 (new_flags &~ IFF_CANTCHANGE);
2169 if (ifp->if_ioctl) {
2170 (void) (*ifp->if_ioctl)(ifp, cmd, data);
2172 getmicrotime(&ifp->if_lastchange);
2176 error = priv_check(td, PRIV_NET_SETIFCAP);
2179 if (ifp->if_ioctl == NULL)
2180 return (EOPNOTSUPP);
2181 if (ifr->ifr_reqcap & ~ifp->if_capabilities)
2183 error = (*ifp->if_ioctl)(ifp, cmd, data);
2185 getmicrotime(&ifp->if_lastchange);
2190 error = mac_ifnet_ioctl_set(td->td_ucred, ifr, ifp);
2195 error = priv_check(td, PRIV_NET_SETIFNAME);
2198 error = copyinstr(ifr->ifr_data, new_name, IFNAMSIZ, NULL);
2201 if (new_name[0] == '\0')
2203 if (ifunit(new_name) != NULL)
2207 * XXX: Locking. Nothing else seems to lock if_flags,
2208 * and there are numerous other races with the
2209 * ifunit() checks not being atomic with namespace
2210 * changes (renames, vmoves, if_attach, etc).
2212 ifp->if_flags |= IFF_RENAMING;
2214 /* Announce the departure of the interface. */
2215 rt_ifannouncemsg(ifp, IFAN_DEPARTURE);
2216 EVENTHANDLER_INVOKE(ifnet_departure_event, ifp);
2218 log(LOG_INFO, "%s: changing name to '%s'\n",
2219 ifp->if_xname, new_name);
2221 strlcpy(ifp->if_xname, new_name, sizeof(ifp->if_xname));
2224 sdl = (struct sockaddr_dl *)ifa->ifa_addr;
2225 namelen = strlen(new_name);
2226 onamelen = sdl->sdl_nlen;
2228 * Move the address if needed. This is safe because we
2229 * allocate space for a name of length IFNAMSIZ when we
2230 * create this in if_attach().
2232 if (namelen != onamelen) {
2233 bcopy(sdl->sdl_data + onamelen,
2234 sdl->sdl_data + namelen, sdl->sdl_alen);
2236 bcopy(new_name, sdl->sdl_data, namelen);
2237 sdl->sdl_nlen = namelen;
2238 sdl = (struct sockaddr_dl *)ifa->ifa_netmask;
2239 bzero(sdl->sdl_data, onamelen);
2240 while (namelen != 0)
2241 sdl->sdl_data[--namelen] = 0xff;
2244 EVENTHANDLER_INVOKE(ifnet_arrival_event, ifp);
2245 /* Announce the return of the interface. */
2246 rt_ifannouncemsg(ifp, IFAN_ARRIVAL);
2248 ifp->if_flags &= ~IFF_RENAMING;
2253 error = priv_check(td, PRIV_NET_SETIFVNET);
2256 error = if_vmove_loan(td, ifp, ifr->ifr_name, ifr->ifr_jid);
2261 error = priv_check(td, PRIV_NET_SETIFMETRIC);
2264 ifp->if_metric = ifr->ifr_metric;
2265 getmicrotime(&ifp->if_lastchange);
2269 error = priv_check(td, PRIV_NET_SETIFPHYS);
2272 if (ifp->if_ioctl == NULL)
2273 return (EOPNOTSUPP);
2274 error = (*ifp->if_ioctl)(ifp, cmd, data);
2276 getmicrotime(&ifp->if_lastchange);
2281 u_long oldmtu = ifp->if_mtu;
2283 error = priv_check(td, PRIV_NET_SETIFMTU);
2286 if (ifr->ifr_mtu < IF_MINMTU || ifr->ifr_mtu > IF_MAXMTU)
2288 if (ifp->if_ioctl == NULL)
2289 return (EOPNOTSUPP);
2290 error = (*ifp->if_ioctl)(ifp, cmd, data);
2292 getmicrotime(&ifp->if_lastchange);
2296 * If the link MTU changed, do network layer specific procedure.
2298 if (ifp->if_mtu != oldmtu) {
2308 if (cmd == SIOCADDMULTI)
2309 error = priv_check(td, PRIV_NET_ADDMULTI);
2311 error = priv_check(td, PRIV_NET_DELMULTI);
2315 /* Don't allow group membership on non-multicast interfaces. */
2316 if ((ifp->if_flags & IFF_MULTICAST) == 0)
2317 return (EOPNOTSUPP);
2319 /* Don't let users screw up protocols' entries. */
2320 if (ifr->ifr_addr.sa_family != AF_LINK)
2323 if (cmd == SIOCADDMULTI) {
2324 struct ifmultiaddr *ifma;
2327 * Userland is only permitted to join groups once
2328 * via the if_addmulti() KPI, because it cannot hold
2329 * struct ifmultiaddr * between calls. It may also
2330 * lose a race while we check if the membership
2334 ifma = if_findmulti(ifp, &ifr->ifr_addr);
2335 IF_ADDR_RUNLOCK(ifp);
2339 error = if_addmulti(ifp, &ifr->ifr_addr, &ifma);
2341 error = if_delmulti(ifp, &ifr->ifr_addr);
2344 getmicrotime(&ifp->if_lastchange);
2347 case SIOCSIFPHYADDR:
2348 case SIOCDIFPHYADDR:
2350 case SIOCSIFPHYADDR_IN6:
2352 case SIOCSLIFPHYADDR:
2354 case SIOCSIFGENERIC:
2355 error = priv_check(td, PRIV_NET_HWIOCTL);
2358 if (ifp->if_ioctl == NULL)
2359 return (EOPNOTSUPP);
2360 error = (*ifp->if_ioctl)(ifp, cmd, data);
2362 getmicrotime(&ifp->if_lastchange);
2366 ifs = (struct ifstat *)data;
2367 ifs->ascii[0] = '\0';
2369 case SIOCGIFPSRCADDR:
2370 case SIOCGIFPDSTADDR:
2371 case SIOCGLIFPHYADDR:
2373 case SIOCGIFGENERIC:
2374 if (ifp->if_ioctl == NULL)
2375 return (EOPNOTSUPP);
2376 error = (*ifp->if_ioctl)(ifp, cmd, data);
2380 error = priv_check(td, PRIV_NET_SETLLADDR);
2383 error = if_setlladdr(ifp,
2384 ifr->ifr_addr.sa_data, ifr->ifr_addr.sa_len);
2385 EVENTHANDLER_INVOKE(iflladdr_event, ifp);
2390 struct ifgroupreq *ifgr = (struct ifgroupreq *)ifr;
2392 error = priv_check(td, PRIV_NET_ADDIFGROUP);
2395 if ((error = if_addgroup(ifp, ifgr->ifgr_group)))
2401 if ((error = if_getgroup((struct ifgroupreq *)ifr, ifp)))
2407 struct ifgroupreq *ifgr = (struct ifgroupreq *)ifr;
2409 error = priv_check(td, PRIV_NET_DELIFGROUP);
2412 if ((error = if_delgroup(ifp, ifgr->ifgr_group)))
2424 #ifdef COMPAT_FREEBSD32
2432 #define SIOCGIFCONF32 _IOWR('i', 36, struct ifconf32)
2439 ifioctl(struct socket *so, u_long cmd, caddr_t data, struct thread *td)
2446 CURVNET_SET(so->so_vnet);
2450 error = ifconf(cmd, data);
2454 #ifdef COMPAT_FREEBSD32
2457 struct ifconf32 *ifc32;
2460 ifc32 = (struct ifconf32 *)data;
2461 ifc.ifc_len = ifc32->ifc_len;
2462 ifc.ifc_buf = PTRIN(ifc32->ifc_buf);
2464 error = ifconf(SIOCGIFCONF, (void *)&ifc);
2467 ifc32->ifc_len = ifc.ifc_len;
2472 ifr = (struct ifreq *)data;
2477 error = priv_check(td, PRIV_NET_SETIFVNET);
2479 error = if_vmove_reclaim(td, ifr->ifr_name,
2486 error = priv_check(td, PRIV_NET_IFCREATE);
2488 error = if_clone_create(ifr->ifr_name,
2489 sizeof(ifr->ifr_name),
2490 cmd == SIOCIFCREATE2 ? ifr->ifr_data : NULL);
2494 error = priv_check(td, PRIV_NET_IFDESTROY);
2496 error = if_clone_destroy(ifr->ifr_name);
2500 case SIOCIFGCLONERS:
2501 error = if_clone_list((struct if_clonereq *)data);
2505 error = if_getgroupmembers((struct ifgroupreq *)data);
2508 #if defined(INET) || defined(INET6)
2511 if (carp_ioctl_p == NULL)
2512 error = EPROTONOSUPPORT;
2514 error = (*carp_ioctl_p)(ifr, cmd, td);
2520 ifp = ifunit_ref(ifr->ifr_name);
2526 error = ifhwioctl(cmd, ifp, data, td);
2527 if (error != ENOIOCTL) {
2533 oif_flags = ifp->if_flags;
2534 if (so->so_proto == NULL) {
2537 return (EOPNOTSUPP);
2540 error = ((*so->so_proto->pr_usrreqs->pru_control)(so, cmd,
2543 if (error == EOPNOTSUPP && ifp != NULL && ifp->if_ioctl != NULL)
2544 error = (*ifp->if_ioctl)(ifp, cmd, data);
2551 case SIOCSIFDSTADDR:
2553 case SIOCSIFBRDADDR:
2554 case SIOCSIFNETMASK:
2555 #if BYTE_ORDER != BIG_ENDIAN
2556 if (ifr->ifr_addr.sa_family == 0 &&
2557 ifr->ifr_addr.sa_len < 16) {
2558 ifr->ifr_addr.sa_family = ifr->ifr_addr.sa_len;
2559 ifr->ifr_addr.sa_len = 16;
2562 if (ifr->ifr_addr.sa_len == 0)
2563 ifr->ifr_addr.sa_len = 16;
2571 case OSIOCGIFDSTADDR:
2572 cmd = SIOCGIFDSTADDR;
2575 case OSIOCGIFBRDADDR:
2576 cmd = SIOCGIFBRDADDR;
2579 case OSIOCGIFNETMASK:
2580 cmd = SIOCGIFNETMASK;
2582 error = ((*so->so_proto->pr_usrreqs->pru_control)(so,
2586 if (error == EOPNOTSUPP && ifp != NULL &&
2587 ifp->if_ioctl != NULL)
2588 error = (*ifp->if_ioctl)(ifp, cmd, data);
2592 case OSIOCGIFDSTADDR:
2593 case OSIOCGIFBRDADDR:
2594 case OSIOCGIFNETMASK:
2595 *(u_short *)&ifr->ifr_addr = ifr->ifr_addr.sa_family;
2599 #endif /* COMPAT_43 */
2601 if ((oif_flags ^ ifp->if_flags) & IFF_UP) {
2603 if (ifp->if_flags & IFF_UP) {
2616 * The code common to handling reference counted flags,
2617 * e.g., in ifpromisc() and if_allmulti().
2618 * The "pflag" argument can specify a permanent mode flag to check,
2619 * such as IFF_PPROMISC for promiscuous mode; should be 0 if none.
2621 * Only to be used on stack-owned flags, not driver-owned flags.
2624 if_setflag(struct ifnet *ifp, int flag, int pflag, int *refcount, int onswitch)
2628 int oldflags, oldcount;
2630 /* Sanity checks to catch programming errors */
2631 KASSERT((flag & (IFF_DRV_OACTIVE|IFF_DRV_RUNNING)) == 0,
2632 ("%s: setting driver-owned flag %d", __func__, flag));
2635 KASSERT(*refcount >= 0,
2636 ("%s: increment negative refcount %d for flag %d",
2637 __func__, *refcount, flag));
2639 KASSERT(*refcount > 0,
2640 ("%s: decrement non-positive refcount %d for flag %d",
2641 __func__, *refcount, flag));
2643 /* In case this mode is permanent, just touch refcount */
2644 if (ifp->if_flags & pflag) {
2645 *refcount += onswitch ? 1 : -1;
2649 /* Save ifnet parameters for if_ioctl() may fail */
2650 oldcount = *refcount;
2651 oldflags = ifp->if_flags;
2654 * See if we aren't the only and touching refcount is enough.
2655 * Actually toggle interface flag if we are the first or last.
2660 ifp->if_flags |= flag;
2664 ifp->if_flags &= ~flag;
2667 /* Call down the driver since we've changed interface flags */
2668 if (ifp->if_ioctl == NULL) {
2672 ifr.ifr_flags = ifp->if_flags & 0xffff;
2673 ifr.ifr_flagshigh = ifp->if_flags >> 16;
2674 error = (*ifp->if_ioctl)(ifp, SIOCSIFFLAGS, (caddr_t)&ifr);
2677 /* Notify userland that interface flags have changed */
2682 /* Recover after driver error */
2683 *refcount = oldcount;
2684 ifp->if_flags = oldflags;
2689 * Set/clear promiscuous mode on interface ifp based on the truth value
2690 * of pswitch. The calls are reference counted so that only the first
2691 * "on" request actually has an effect, as does the final "off" request.
2692 * Results are undefined if the "off" and "on" requests are not matched.
2695 ifpromisc(struct ifnet *ifp, int pswitch)
2698 int oldflags = ifp->if_flags;
2700 error = if_setflag(ifp, IFF_PROMISC, IFF_PPROMISC,
2701 &ifp->if_pcount, pswitch);
2702 /* If promiscuous mode status has changed, log a message */
2703 if (error == 0 && ((ifp->if_flags ^ oldflags) & IFF_PROMISC))
2704 log(LOG_INFO, "%s: promiscuous mode %s\n",
2706 (ifp->if_flags & IFF_PROMISC) ? "enabled" : "disabled");
2711 * Return interface configuration
2712 * of system. List may be used
2713 * in later ioctl's (above) to get
2714 * other information.
2718 ifconf(u_long cmd, caddr_t data)
2720 struct ifconf *ifc = (struct ifconf *)data;
2725 int error, full = 0, valid_len, max_len;
2727 /* Limit initial buffer size to MAXPHYS to avoid DoS from userspace. */
2728 max_len = MAXPHYS - 1;
2730 /* Prevent hostile input from being able to crash the system */
2731 if (ifc->ifc_len <= 0)
2735 if (ifc->ifc_len <= max_len) {
2736 max_len = ifc->ifc_len;
2739 sb = sbuf_new(NULL, NULL, max_len + 1, SBUF_FIXEDLEN);
2744 TAILQ_FOREACH(ifp, &V_ifnet, if_link) {
2748 * Zero the ifr_name buffer to make sure we don't
2749 * disclose the contents of the stack.
2751 memset(ifr.ifr_name, 0, sizeof(ifr.ifr_name));
2753 if (strlcpy(ifr.ifr_name, ifp->if_xname, sizeof(ifr.ifr_name))
2754 >= sizeof(ifr.ifr_name)) {
2757 return (ENAMETOOLONG);
2762 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
2763 struct sockaddr *sa = ifa->ifa_addr;
2765 if (prison_if(curthread->td_ucred, sa) != 0)
2769 if (cmd == OSIOCGIFCONF) {
2770 struct osockaddr *osa =
2771 (struct osockaddr *)&ifr.ifr_addr;
2773 osa->sa_family = sa->sa_family;
2774 sbuf_bcat(sb, &ifr, sizeof(ifr));
2775 max_len += sizeof(ifr);
2778 if (sa->sa_len <= sizeof(*sa)) {
2780 sbuf_bcat(sb, &ifr, sizeof(ifr));
2781 max_len += sizeof(ifr);
2784 offsetof(struct ifreq, ifr_addr));
2785 max_len += offsetof(struct ifreq, ifr_addr);
2786 sbuf_bcat(sb, sa, sa->sa_len);
2787 max_len += sa->sa_len;
2790 if (sbuf_error(sb) == 0)
2791 valid_len = sbuf_len(sb);
2793 IF_ADDR_RUNLOCK(ifp);
2795 bzero((caddr_t)&ifr.ifr_addr, sizeof(ifr.ifr_addr));
2796 sbuf_bcat(sb, &ifr, sizeof(ifr));
2797 max_len += sizeof(ifr);
2799 if (sbuf_error(sb) == 0)
2800 valid_len = sbuf_len(sb);
2806 * If we didn't allocate enough space (uncommon), try again. If
2807 * we have already allocated as much space as we are allowed,
2808 * return what we've got.
2810 if (valid_len != max_len && !full) {
2815 ifc->ifc_len = valid_len;
2817 error = copyout(sbuf_data(sb), ifc->ifc_req, ifc->ifc_len);
2823 * Just like ifpromisc(), but for all-multicast-reception mode.
2826 if_allmulti(struct ifnet *ifp, int onswitch)
2829 return (if_setflag(ifp, IFF_ALLMULTI, 0, &ifp->if_amcount, onswitch));
2832 struct ifmultiaddr *
2833 if_findmulti(struct ifnet *ifp, struct sockaddr *sa)
2835 struct ifmultiaddr *ifma;
2837 IF_ADDR_LOCK_ASSERT(ifp);
2839 TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
2840 if (sa->sa_family == AF_LINK) {
2841 if (sa_dl_equal(ifma->ifma_addr, sa))
2844 if (sa_equal(ifma->ifma_addr, sa))
2853 * Allocate a new ifmultiaddr and initialize based on passed arguments. We
2854 * make copies of passed sockaddrs. The ifmultiaddr will not be added to
2855 * the ifnet multicast address list here, so the caller must do that and
2856 * other setup work (such as notifying the device driver). The reference
2857 * count is initialized to 1.
2859 static struct ifmultiaddr *
2860 if_allocmulti(struct ifnet *ifp, struct sockaddr *sa, struct sockaddr *llsa,
2863 struct ifmultiaddr *ifma;
2864 struct sockaddr *dupsa;
2866 ifma = malloc(sizeof *ifma, M_IFMADDR, mflags |
2871 dupsa = malloc(sa->sa_len, M_IFMADDR, mflags);
2872 if (dupsa == NULL) {
2873 free(ifma, M_IFMADDR);
2876 bcopy(sa, dupsa, sa->sa_len);
2877 ifma->ifma_addr = dupsa;
2879 ifma->ifma_ifp = ifp;
2880 ifma->ifma_refcount = 1;
2881 ifma->ifma_protospec = NULL;
2884 ifma->ifma_lladdr = NULL;
2888 dupsa = malloc(llsa->sa_len, M_IFMADDR, mflags);
2889 if (dupsa == NULL) {
2890 free(ifma->ifma_addr, M_IFMADDR);
2891 free(ifma, M_IFMADDR);
2894 bcopy(llsa, dupsa, llsa->sa_len);
2895 ifma->ifma_lladdr = dupsa;
2901 * if_freemulti: free ifmultiaddr structure and possibly attached related
2902 * addresses. The caller is responsible for implementing reference
2903 * counting, notifying the driver, handling routing messages, and releasing
2904 * any dependent link layer state.
2907 if_freemulti(struct ifmultiaddr *ifma)
2910 KASSERT(ifma->ifma_refcount == 0, ("if_freemulti: refcount %d",
2911 ifma->ifma_refcount));
2912 KASSERT(ifma->ifma_protospec == NULL,
2913 ("if_freemulti: protospec not NULL"));
2915 if (ifma->ifma_lladdr != NULL)
2916 free(ifma->ifma_lladdr, M_IFMADDR);
2917 free(ifma->ifma_addr, M_IFMADDR);
2918 free(ifma, M_IFMADDR);
2922 * Register an additional multicast address with a network interface.
2924 * - If the address is already present, bump the reference count on the
2925 * address and return.
2926 * - If the address is not link-layer, look up a link layer address.
2927 * - Allocate address structures for one or both addresses, and attach to the
2928 * multicast address list on the interface. If automatically adding a link
2929 * layer address, the protocol address will own a reference to the link
2930 * layer address, to be freed when it is freed.
2931 * - Notify the network device driver of an addition to the multicast address
2934 * 'sa' points to caller-owned memory with the desired multicast address.
2936 * 'retifma' will be used to return a pointer to the resulting multicast
2937 * address reference, if desired.
2940 if_addmulti(struct ifnet *ifp, struct sockaddr *sa,
2941 struct ifmultiaddr **retifma)
2943 struct ifmultiaddr *ifma, *ll_ifma;
2944 struct sockaddr *llsa;
2948 * If the address is already present, return a new reference to it;
2949 * otherwise, allocate storage and set up a new address.
2952 ifma = if_findmulti(ifp, sa);
2954 ifma->ifma_refcount++;
2955 if (retifma != NULL)
2957 IF_ADDR_WUNLOCK(ifp);
2962 * The address isn't already present; resolve the protocol address
2963 * into a link layer address, and then look that up, bump its
2964 * refcount or allocate an ifma for that also. If 'llsa' was
2965 * returned, we will need to free it later.
2969 if (ifp->if_resolvemulti != NULL) {
2970 error = ifp->if_resolvemulti(ifp, &llsa, sa);
2976 * Allocate the new address. Don't hook it up yet, as we may also
2977 * need to allocate a link layer multicast address.
2979 ifma = if_allocmulti(ifp, sa, llsa, M_NOWAIT);
2986 * If a link layer address is found, we'll need to see if it's
2987 * already present in the address list, or allocate is as well.
2988 * When this block finishes, the link layer address will be on the
2992 ll_ifma = if_findmulti(ifp, llsa);
2993 if (ll_ifma == NULL) {
2994 ll_ifma = if_allocmulti(ifp, llsa, NULL, M_NOWAIT);
2995 if (ll_ifma == NULL) {
2996 --ifma->ifma_refcount;
3001 TAILQ_INSERT_HEAD(&ifp->if_multiaddrs, ll_ifma,
3004 ll_ifma->ifma_refcount++;
3005 ifma->ifma_llifma = ll_ifma;
3009 * We now have a new multicast address, ifma, and possibly a new or
3010 * referenced link layer address. Add the primary address to the
3011 * ifnet address list.
3013 TAILQ_INSERT_HEAD(&ifp->if_multiaddrs, ifma, ifma_link);
3015 if (retifma != NULL)
3019 * Must generate the message while holding the lock so that 'ifma'
3020 * pointer is still valid.
3022 rt_newmaddrmsg(RTM_NEWMADDR, ifma);
3023 IF_ADDR_WUNLOCK(ifp);
3026 * We are certain we have added something, so call down to the
3027 * interface to let them know about it.
3029 if (ifp->if_ioctl != NULL) {
3030 (void) (*ifp->if_ioctl)(ifp, SIOCADDMULTI, 0);
3034 free(llsa, M_IFMADDR);
3040 free(llsa, M_IFMADDR);
3043 IF_ADDR_WUNLOCK(ifp);
3048 * Delete a multicast group membership by network-layer group address.
3050 * Returns ENOENT if the entry could not be found. If ifp no longer
3051 * exists, results are undefined. This entry point should only be used
3052 * from subsystems which do appropriate locking to hold ifp for the
3053 * duration of the call.
3054 * Network-layer protocol domains must use if_delmulti_ifma().
3057 if_delmulti(struct ifnet *ifp, struct sockaddr *sa)
3059 struct ifmultiaddr *ifma;
3064 IFNET_RLOCK_NOSLEEP();
3065 TAILQ_FOREACH(oifp, &V_ifnet, if_link)
3070 IFNET_RUNLOCK_NOSLEEP();
3072 KASSERT(ifp != NULL, ("%s: ifnet went away", __func__));
3079 ifma = if_findmulti(ifp, sa);
3081 lastref = if_delmulti_locked(ifp, ifma, 0);
3082 IF_ADDR_WUNLOCK(ifp);
3087 if (lastref && ifp->if_ioctl != NULL) {
3088 (void)(*ifp->if_ioctl)(ifp, SIOCDELMULTI, 0);
3095 * Delete all multicast group membership for an interface.
3096 * Should be used to quickly flush all multicast filters.
3099 if_delallmulti(struct ifnet *ifp)
3101 struct ifmultiaddr *ifma;
3102 struct ifmultiaddr *next;
3105 TAILQ_FOREACH_SAFE(ifma, &ifp->if_multiaddrs, ifma_link, next)
3106 if_delmulti_locked(ifp, ifma, 0);
3107 IF_ADDR_WUNLOCK(ifp);
3111 * Delete a multicast group membership by group membership pointer.
3112 * Network-layer protocol domains must use this routine.
3114 * It is safe to call this routine if the ifp disappeared.
3117 if_delmulti_ifma(struct ifmultiaddr *ifma)
3122 ifp = ifma->ifma_ifp;
3125 printf("%s: ifma_ifp seems to be detached\n", __func__);
3129 IFNET_RLOCK_NOSLEEP();
3130 TAILQ_FOREACH(oifp, &V_ifnet, if_link)
3134 printf("%s: ifnet %p disappeared\n", __func__, ifp);
3137 IFNET_RUNLOCK_NOSLEEP();
3141 * If and only if the ifnet instance exists: Acquire the address lock.
3146 lastref = if_delmulti_locked(ifp, ifma, 0);
3150 * If and only if the ifnet instance exists:
3151 * Release the address lock.
3152 * If the group was left: update the hardware hash filter.
3154 IF_ADDR_WUNLOCK(ifp);
3155 if (lastref && ifp->if_ioctl != NULL) {
3156 (void)(*ifp->if_ioctl)(ifp, SIOCDELMULTI, 0);
3162 * Perform deletion of network-layer and/or link-layer multicast address.
3164 * Return 0 if the reference count was decremented.
3165 * Return 1 if the final reference was released, indicating that the
3166 * hardware hash filter should be reprogrammed.
3169 if_delmulti_locked(struct ifnet *ifp, struct ifmultiaddr *ifma, int detaching)
3171 struct ifmultiaddr *ll_ifma;
3173 if (ifp != NULL && ifma->ifma_ifp != NULL) {
3174 KASSERT(ifma->ifma_ifp == ifp,
3175 ("%s: inconsistent ifp %p", __func__, ifp));
3176 IF_ADDR_WLOCK_ASSERT(ifp);
3179 ifp = ifma->ifma_ifp;
3182 * If the ifnet is detaching, null out references to ifnet,
3183 * so that upper protocol layers will notice, and not attempt
3184 * to obtain locks for an ifnet which no longer exists. The
3185 * routing socket announcement must happen before the ifnet
3186 * instance is detached from the system.
3190 printf("%s: detaching ifnet instance %p\n", __func__, ifp);
3193 * ifp may already be nulled out if we are being reentered
3194 * to delete the ll_ifma.
3197 rt_newmaddrmsg(RTM_DELMADDR, ifma);
3198 ifma->ifma_ifp = NULL;
3202 if (--ifma->ifma_refcount > 0)
3206 * If this ifma is a network-layer ifma, a link-layer ifma may
3207 * have been associated with it. Release it first if so.
3209 ll_ifma = ifma->ifma_llifma;
3210 if (ll_ifma != NULL) {
3211 KASSERT(ifma->ifma_lladdr != NULL,
3212 ("%s: llifma w/o lladdr", __func__));
3214 ll_ifma->ifma_ifp = NULL; /* XXX */
3215 if (--ll_ifma->ifma_refcount == 0) {
3217 TAILQ_REMOVE(&ifp->if_multiaddrs, ll_ifma,
3220 if_freemulti(ll_ifma);
3225 TAILQ_REMOVE(&ifp->if_multiaddrs, ifma, ifma_link);
3230 * The last reference to this instance of struct ifmultiaddr
3231 * was released; the hardware should be notified of this change.
3237 * Set the link layer address on an interface.
3239 * At this time we only support certain types of interfaces,
3240 * and we don't allow the length of the address to change.
3243 if_setlladdr(struct ifnet *ifp, const u_char *lladdr, int len)
3245 struct sockaddr_dl *sdl;
3252 IF_ADDR_RUNLOCK(ifp);
3256 IF_ADDR_RUNLOCK(ifp);
3257 sdl = (struct sockaddr_dl *)ifa->ifa_addr;
3262 if (len != sdl->sdl_alen) { /* don't allow length to change */
3266 switch (ifp->if_type) {
3274 case IFT_IEEE8023ADLAG:
3276 bcopy(lladdr, LLADDR(sdl), len);
3285 * If the interface is already up, we need
3286 * to re-init it in order to reprogram its
3289 if ((ifp->if_flags & IFF_UP) != 0) {
3290 if (ifp->if_ioctl) {
3291 ifp->if_flags &= ~IFF_UP;
3292 ifr.ifr_flags = ifp->if_flags & 0xffff;
3293 ifr.ifr_flagshigh = ifp->if_flags >> 16;
3294 (*ifp->if_ioctl)(ifp, SIOCSIFFLAGS, (caddr_t)&ifr);
3295 ifp->if_flags |= IFF_UP;
3296 ifr.ifr_flags = ifp->if_flags & 0xffff;
3297 ifr.ifr_flagshigh = ifp->if_flags >> 16;
3298 (*ifp->if_ioctl)(ifp, SIOCSIFFLAGS, (caddr_t)&ifr);
3302 * Also send gratuitous ARPs to notify other nodes about
3303 * the address change.
3305 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
3306 if (ifa->ifa_addr->sa_family == AF_INET)
3307 arp_ifinit(ifp, ifa);
3315 * The name argument must be a pointer to storage which will last as
3316 * long as the interface does. For physical devices, the result of
3317 * device_get_name(dev) is a good choice and for pseudo-devices a
3318 * static string works well.
3321 if_initname(struct ifnet *ifp, const char *name, int unit)
3323 ifp->if_dname = name;
3324 ifp->if_dunit = unit;
3325 if (unit != IF_DUNIT_NONE)
3326 snprintf(ifp->if_xname, IFNAMSIZ, "%s%d", name, unit);
3328 strlcpy(ifp->if_xname, name, IFNAMSIZ);
3332 if_printf(struct ifnet *ifp, const char * fmt, ...)
3337 retval = printf("%s: ", ifp->if_xname);
3339 retval += vprintf(fmt, ap);
3345 if_start(struct ifnet *ifp)
3348 (*(ifp)->if_start)(ifp);
3352 * Backwards compatibility interface for drivers
3353 * that have not implemented it
3356 if_transmit(struct ifnet *ifp, struct mbuf *m)
3360 IFQ_HANDOFF(ifp, m, error);
3365 if_handoff(struct ifqueue *ifq, struct mbuf *m, struct ifnet *ifp, int adjust)
3370 if (_IF_QFULL(ifq)) {
3377 ifp->if_obytes += m->m_pkthdr.len + adjust;
3378 if (m->m_flags & (M_BCAST|M_MCAST))
3380 active = ifp->if_drv_flags & IFF_DRV_OACTIVE;
3382 _IF_ENQUEUE(ifq, m);
3384 if (ifp != NULL && !active)
3385 (*(ifp)->if_start)(ifp);
3390 if_register_com_alloc(u_char type,
3391 if_com_alloc_t *a, if_com_free_t *f)
3394 KASSERT(if_com_alloc[type] == NULL,
3395 ("if_register_com_alloc: %d already registered", type));
3396 KASSERT(if_com_free[type] == NULL,
3397 ("if_register_com_alloc: %d free already registered", type));
3399 if_com_alloc[type] = a;
3400 if_com_free[type] = f;
3404 if_deregister_com_alloc(u_char type)
3407 KASSERT(if_com_alloc[type] != NULL,
3408 ("if_deregister_com_alloc: %d not registered", type));
3409 KASSERT(if_com_free[type] != NULL,
3410 ("if_deregister_com_alloc: %d free not registered", type));
3411 if_com_alloc[type] = NULL;
3412 if_com_free[type] = NULL;