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"
38 #include <sys/param.h>
39 #include <sys/types.h>
41 #include <sys/malloc.h>
45 #include <sys/systm.h>
48 #include <sys/socket.h>
49 #include <sys/socketvar.h>
50 #include <sys/protosw.h>
51 #include <sys/kernel.h>
53 #include <sys/refcount.h>
54 #include <sys/module.h>
55 #include <sys/rwlock.h>
56 #include <sys/sockio.h>
57 #include <sys/syslog.h>
58 #include <sys/sysctl.h>
59 #include <sys/taskqueue.h>
60 #include <sys/domain.h>
62 #include <machine/stdarg.h>
70 #include <net/if_arp.h>
71 #include <net/if_clone.h>
72 #include <net/if_dl.h>
73 #include <net/if_types.h>
74 #include <net/if_var.h>
75 #include <net/radix.h>
76 #include <net/route.h>
79 #if defined(INET) || defined(INET6)
81 #include <netinet/in.h>
82 #include <netinet/in_var.h>
83 #include <netinet/ip_carp.h>
85 #include <netinet6/in6_var.h>
86 #include <netinet6/in6_ifattach.h>
90 #include <netinet/if_ether.h>
93 #include <security/mac/mac_framework.h>
95 #ifdef COMPAT_FREEBSD32
96 #include <sys/mount.h>
97 #include <compat/freebsd32/freebsd32.h>
100 struct ifindex_entry {
101 struct ifnet *ife_ifnet;
104 SYSCTL_NODE(_net, PF_LINK, link, CTLFLAG_RW, 0, "Link layers");
105 SYSCTL_NODE(_net_link, 0, generic, CTLFLAG_RW, 0, "Generic link-management");
107 TUNABLE_INT("net.link.ifqmaxlen", &ifqmaxlen);
108 SYSCTL_UINT(_net_link, OID_AUTO, ifqmaxlen, CTLFLAG_RDTUN,
109 &ifqmaxlen, 0, "max send queue size");
111 /* Log link state change events */
112 static int log_link_state_change = 1;
114 SYSCTL_INT(_net_link, OID_AUTO, log_link_state_change, CTLFLAG_RW,
115 &log_link_state_change, 0,
116 "log interface link state change events");
118 /* Interface description */
119 static unsigned int ifdescr_maxlen = 1024;
120 SYSCTL_UINT(_net, OID_AUTO, ifdescr_maxlen, CTLFLAG_RW,
122 "administrative maximum length for interface description");
124 MALLOC_DEFINE(M_IFDESCR, "ifdescr", "ifnet descriptions");
126 /* global sx for non-critical path ifdescr */
127 static struct sx ifdescr_sx;
128 SX_SYSINIT(ifdescr_sx, &ifdescr_sx, "ifnet descr");
130 void (*bstp_linkstate_p)(struct ifnet *ifp, int state);
131 void (*ng_ether_link_state_p)(struct ifnet *ifp, int state);
132 void (*lagg_linkstate_p)(struct ifnet *ifp, int state);
133 /* These are external hooks for CARP. */
134 void (*carp_linkstate_p)(struct ifnet *ifp);
135 #if defined(INET) || defined(INET6)
136 struct ifnet *(*carp_forus_p)(struct ifnet *ifp, u_char *dhost);
137 int (*carp_output_p)(struct ifnet *ifp, struct mbuf *m,
138 struct sockaddr *sa, struct rtentry *rt);
141 int (*carp_iamatch_p)(struct ifnet *, struct in_ifaddr *, struct in_addr *,
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 static 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 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();
275 * Try to find an empty slot below V_if_index. If we fail, take the
278 for (idx = 1; idx <= V_if_index; idx++) {
279 if (V_ifindex_table[idx].ife_ifnet == NULL)
283 /* Catch if_index overflow. */
286 if (idx > V_if_index)
288 if (V_if_index >= V_if_indexlim)
295 ifindex_free_locked(u_short idx)
298 IFNET_WLOCK_ASSERT();
300 V_ifindex_table[idx].ife_ifnet = NULL;
301 while (V_if_index > 0 &&
302 V_ifindex_table[V_if_index].ife_ifnet == NULL)
307 ifindex_free(u_short idx)
311 ifindex_free_locked(idx);
316 ifnet_setbyindex_locked(u_short idx, struct ifnet *ifp)
319 IFNET_WLOCK_ASSERT();
321 V_ifindex_table[idx].ife_ifnet = ifp;
325 ifnet_setbyindex(u_short idx, struct ifnet *ifp)
329 ifnet_setbyindex_locked(idx, ifp);
334 ifaddr_byindex(u_short idx)
338 IFNET_RLOCK_NOSLEEP();
339 ifa = ifnet_byindex_locked(idx)->if_addr;
342 IFNET_RUNLOCK_NOSLEEP();
347 * Network interface utility routines.
349 * Routines with ifa_ifwith* names take sockaddr *'s as
354 vnet_if_init(const void *unused __unused)
357 TAILQ_INIT(&V_ifnet);
358 TAILQ_INIT(&V_ifg_head);
359 if_grow(); /* create initial table */
360 vnet_if_clone_init();
362 VNET_SYSINIT(vnet_if_init, SI_SUB_INIT_IF, SI_ORDER_FIRST, vnet_if_init,
367 if_init(void *dummy __unused)
373 SYSINIT(interfaces, SI_SUB_INIT_IF, SI_ORDER_SECOND, if_init, NULL);
378 vnet_if_uninit(const void *unused __unused)
381 VNET_ASSERT(TAILQ_EMPTY(&V_ifnet));
382 VNET_ASSERT(TAILQ_EMPTY(&V_ifg_head));
384 free((caddr_t)V_ifindex_table, M_IFNET);
386 VNET_SYSUNINIT(vnet_if_uninit, SI_SUB_INIT_IF, SI_ORDER_FIRST,
387 vnet_if_uninit, NULL);
394 struct ifindex_entry *e;
397 n = V_if_indexlim * sizeof(*e);
398 e = malloc(n, M_IFNET, M_WAITOK | M_ZERO);
399 if (V_ifindex_table != NULL) {
400 memcpy((caddr_t)e, (caddr_t)V_ifindex_table, n/2);
401 free((caddr_t)V_ifindex_table, M_IFNET);
407 * Allocate a struct ifnet and an index for an interface. A layer 2
408 * common structure will also be allocated if an allocation routine is
409 * registered for the passed type.
412 if_alloc(u_char type)
417 ifp = malloc(sizeof(struct ifnet), M_IFNET, M_WAITOK|M_ZERO);
419 if (ifindex_alloc_locked(&idx) != 0) {
424 ifnet_setbyindex_locked(idx, IFNET_HOLD);
428 ifp->if_alloctype = type;
429 if (if_com_alloc[type] != NULL) {
430 ifp->if_l2com = if_com_alloc[type](type, ifp);
431 if (ifp->if_l2com == NULL) {
438 IF_ADDR_LOCK_INIT(ifp);
439 TASK_INIT(&ifp->if_linktask, 0, do_link_state_change, ifp);
440 ifp->if_afdata_initialized = 0;
441 IF_AFDATA_LOCK_INIT(ifp);
442 TAILQ_INIT(&ifp->if_addrhead);
443 TAILQ_INIT(&ifp->if_prefixhead);
444 TAILQ_INIT(&ifp->if_multiaddrs);
445 TAILQ_INIT(&ifp->if_groups);
449 ifq_init(&ifp->if_snd, ifp);
451 refcount_init(&ifp->if_refcount, 1); /* Index reference. */
452 ifnet_setbyindex(ifp->if_index, ifp);
457 * Do the actual work of freeing a struct ifnet, associated index, and layer
458 * 2 common structure. This call is made when the last reference to an
459 * interface is released.
462 if_free_internal(struct ifnet *ifp)
465 KASSERT((ifp->if_flags & IFF_DYING),
466 ("if_free_internal: interface not dying"));
469 KASSERT(ifp == ifnet_byindex_locked(ifp->if_index),
470 ("%s: freeing unallocated ifnet", ifp->if_xname));
472 ifindex_free_locked(ifp->if_index);
475 if (if_com_free[ifp->if_alloctype] != NULL)
476 if_com_free[ifp->if_alloctype](ifp->if_l2com,
480 mac_ifnet_destroy(ifp);
482 if (ifp->if_description != NULL)
483 free(ifp->if_description, M_IFDESCR);
484 IF_AFDATA_DESTROY(ifp);
485 IF_ADDR_LOCK_DESTROY(ifp);
486 ifq_delete(&ifp->if_snd);
491 * This version should only be called by intefaces that switch their type
492 * after calling if_alloc(). if_free_type() will go away again now that we
493 * have if_alloctype to cache the original allocation type. For now, assert
494 * that they match, since we require that in practice.
497 if_free_type(struct ifnet *ifp, u_char type)
500 KASSERT(ifp->if_alloctype == type,
501 ("if_free_type: type (%d) != alloctype (%d)", type,
504 ifp->if_flags |= IFF_DYING; /* XXX: Locking */
505 if (!refcount_release(&ifp->if_refcount))
507 if_free_internal(ifp);
511 * This is the normal version of if_free(), used by device drivers to free a
512 * detached network interface. The contents of if_free_type() will move into
513 * here when if_free_type() goes away.
516 if_free(struct ifnet *ifp)
519 if_free_type(ifp, ifp->if_alloctype);
523 * Interfaces to keep an ifnet type-stable despite the possibility of the
524 * driver calling if_free(). If there are additional references, we defer
525 * freeing the underlying data structure.
528 if_ref(struct ifnet *ifp)
531 /* We don't assert the ifnet list lock here, but arguably should. */
532 refcount_acquire(&ifp->if_refcount);
536 if_rele(struct ifnet *ifp)
539 if (!refcount_release(&ifp->if_refcount))
541 if_free_internal(ifp);
545 ifq_init(struct ifaltq *ifq, struct ifnet *ifp)
548 mtx_init(&ifq->ifq_mtx, ifp->if_xname, "if send queue", MTX_DEF);
550 if (ifq->ifq_maxlen == 0)
551 ifq->ifq_maxlen = ifqmaxlen;
554 ifq->altq_disc = NULL;
555 ifq->altq_flags &= ALTQF_CANTCHANGE;
556 ifq->altq_tbr = NULL;
561 ifq_delete(struct ifaltq *ifq)
563 mtx_destroy(&ifq->ifq_mtx);
567 * Perform generic interface initalization tasks and attach the interface
568 * to the list of "active" interfaces. If vmove flag is set on entry
569 * to if_attach_internal(), perform only a limited subset of initialization
570 * tasks, given that we are moving from one vnet to another an ifnet which
571 * has already been fully initialized.
574 * - The decision to return void and thus require this function to
575 * succeed is questionable.
576 * - We should probably do more sanity checking. For instance we don't
577 * do anything to insure if_xname is unique or non-empty.
580 if_attach(struct ifnet *ifp)
583 if_attach_internal(ifp, 0);
587 if_attach_internal(struct ifnet *ifp, int vmove)
589 unsigned socksize, ifasize;
590 int namelen, masklen;
591 struct sockaddr_dl *sdl;
594 if (ifp->if_index == 0 || ifp != ifnet_byindex(ifp->if_index))
595 panic ("%s: BUG: if_attach called without if_alloc'd input()\n",
599 ifp->if_vnet = curvnet;
600 if (ifp->if_home_vnet == NULL)
601 ifp->if_home_vnet = curvnet;
604 if_addgroup(ifp, IFG_ALL);
606 getmicrotime(&ifp->if_lastchange);
607 ifp->if_data.ifi_epoch = time_uptime;
608 ifp->if_data.ifi_datalen = sizeof(struct if_data);
610 KASSERT((ifp->if_transmit == NULL && ifp->if_qflush == NULL) ||
611 (ifp->if_transmit != NULL && ifp->if_qflush != NULL),
612 ("transmit and qflush must both either be set or both be NULL"));
613 if (ifp->if_transmit == NULL) {
614 ifp->if_transmit = if_transmit;
615 ifp->if_qflush = if_qflush;
620 mac_ifnet_create(ifp);
624 * Create a Link Level name for this device.
626 namelen = strlen(ifp->if_xname);
628 * Always save enough space for any possiable name so we
629 * can do a rename in place later.
631 masklen = offsetof(struct sockaddr_dl, sdl_data[0]) + IFNAMSIZ;
632 socksize = masklen + ifp->if_addrlen;
633 if (socksize < sizeof(*sdl))
634 socksize = sizeof(*sdl);
635 socksize = roundup2(socksize, sizeof(long));
636 ifasize = sizeof(*ifa) + 2 * socksize;
637 ifa = malloc(ifasize, M_IFADDR, M_WAITOK | M_ZERO);
639 sdl = (struct sockaddr_dl *)(ifa + 1);
640 sdl->sdl_len = socksize;
641 sdl->sdl_family = AF_LINK;
642 bcopy(ifp->if_xname, sdl->sdl_data, namelen);
643 sdl->sdl_nlen = namelen;
644 sdl->sdl_index = ifp->if_index;
645 sdl->sdl_type = ifp->if_type;
648 ifa->ifa_rtrequest = link_rtrequest;
649 ifa->ifa_addr = (struct sockaddr *)sdl;
650 sdl = (struct sockaddr_dl *)(socksize + (caddr_t)sdl);
651 ifa->ifa_netmask = (struct sockaddr *)sdl;
652 sdl->sdl_len = masklen;
654 sdl->sdl_data[--namelen] = 0xff;
655 TAILQ_INSERT_HEAD(&ifp->if_addrhead, ifa, ifa_link);
656 /* Reliably crash if used uninitialized. */
657 ifp->if_broadcastaddr = NULL;
662 * Update the interface index in the link layer address
665 for (ifa = ifp->if_addr; ifa != NULL;
666 ifa = TAILQ_NEXT(ifa, ifa_link)) {
667 if (ifa->ifa_addr->sa_family == AF_LINK) {
668 sdl = (struct sockaddr_dl *)ifa->ifa_addr;
669 sdl->sdl_index = ifp->if_index;
676 TAILQ_INSERT_TAIL(&V_ifnet, ifp, if_link);
678 curvnet->vnet_ifcnt++;
682 if (domain_init_status >= 2)
683 if_attachdomain1(ifp);
685 EVENTHANDLER_INVOKE(ifnet_arrival_event, ifp);
686 if (IS_DEFAULT_VNET(curvnet))
687 devctl_notify("IFNET", ifp->if_xname, "ATTACH", NULL);
689 /* Announce the interface. */
690 rt_ifannouncemsg(ifp, IFAN_ARRIVAL);
694 if_attachdomain(void *dummy)
700 TAILQ_FOREACH(ifp, &V_ifnet, if_link)
701 if_attachdomain1(ifp);
704 SYSINIT(domainifattach, SI_SUB_PROTO_IFATTACHDOMAIN, SI_ORDER_SECOND,
705 if_attachdomain, NULL);
708 if_attachdomain1(struct ifnet *ifp)
716 * Since dp->dom_ifattach calls malloc() with M_WAITOK, we
717 * cannot lock ifp->if_afdata initialization, entirely.
719 if (IF_AFDATA_TRYLOCK(ifp) == 0) {
723 if (ifp->if_afdata_initialized >= domain_init_status) {
724 IF_AFDATA_UNLOCK(ifp);
726 printf("if_attachdomain called more than once on %s\n",
730 ifp->if_afdata_initialized = domain_init_status;
731 IF_AFDATA_UNLOCK(ifp);
733 /* address family dependent data region */
734 bzero(ifp->if_afdata, sizeof(ifp->if_afdata));
735 for (dp = domains; dp; dp = dp->dom_next) {
736 if (dp->dom_ifattach)
737 ifp->if_afdata[dp->dom_family] =
738 (*dp->dom_ifattach)(ifp);
745 * Remove any unicast or broadcast network addresses from an interface.
748 if_purgeaddrs(struct ifnet *ifp)
750 struct ifaddr *ifa, *next;
752 TAILQ_FOREACH_SAFE(ifa, &ifp->if_addrhead, ifa_link, next) {
753 if (ifa->ifa_addr->sa_family == AF_LINK)
756 /* XXX: Ugly!! ad hoc just for INET */
757 if (ifa->ifa_addr->sa_family == AF_INET) {
758 struct ifaliasreq ifr;
760 bzero(&ifr, sizeof(ifr));
761 ifr.ifra_addr = *ifa->ifa_addr;
762 if (ifa->ifa_dstaddr)
763 ifr.ifra_broadaddr = *ifa->ifa_dstaddr;
764 if (in_control(NULL, SIOCDIFADDR, (caddr_t)&ifr, ifp,
770 if (ifa->ifa_addr->sa_family == AF_INET6) {
772 /* ifp_addrhead is already updated */
776 TAILQ_REMOVE(&ifp->if_addrhead, ifa, ifa_link);
782 * Remove any multicast network addresses from an interface when an ifnet
786 if_purgemaddrs(struct ifnet *ifp)
788 struct ifmultiaddr *ifma;
789 struct ifmultiaddr *next;
792 TAILQ_FOREACH_SAFE(ifma, &ifp->if_multiaddrs, ifma_link, next)
793 if_delmulti_locked(ifp, ifma, 1);
798 * Detach an interface, removing it from the list of "active" interfaces.
799 * If vmove flag is set on entry to if_detach_internal(), perform only a
800 * limited subset of cleanup tasks, given that we are moving an ifnet from
801 * one vnet to another, where it must be fully operational.
803 * XXXRW: There are some significant questions about event ordering, and
804 * how to prevent things from starting to use the interface during detach.
807 if_detach(struct ifnet *ifp)
810 if_detach_internal(ifp, 0);
814 if_detach_internal(struct ifnet *ifp, int vmove)
817 struct radix_node_head *rnh;
824 TAILQ_FOREACH(iter, &V_ifnet, if_link)
826 TAILQ_REMOVE(&V_ifnet, ifp, if_link);
832 curvnet->vnet_ifcnt--;
837 panic("%s: ifp=%p not on the ifnet tailq %p",
838 __func__, ifp, &V_ifnet);
840 return; /* XXX this should panic as well? */
844 * Remove/wait for pending events.
846 taskqueue_drain(taskqueue_swi, &ifp->if_linktask);
849 * Remove routes and flush queues.
853 if (ALTQ_IS_ENABLED(&ifp->if_snd))
854 altq_disable(&ifp->if_snd);
855 if (ALTQ_IS_ATTACHED(&ifp->if_snd))
856 altq_detach(&ifp->if_snd);
867 * Remove all IPv6 kernel structs related to ifp. This should be done
868 * before removing routing entries below, since IPv6 interface direct
869 * routes are expected to be removed by the IPv6-specific kernel API.
870 * Otherwise, the kernel will detect some inconsistency and bark it.
878 * Prevent further calls into the device driver via ifnet.
883 * Remove link ifaddr pointer and maybe decrement if_index.
884 * Clean up all addresses.
888 /* We can now free link ifaddr. */
889 if (!TAILQ_EMPTY(&ifp->if_addrhead)) {
890 ifa = TAILQ_FIRST(&ifp->if_addrhead);
891 TAILQ_REMOVE(&ifp->if_addrhead, ifa, ifa_link);
897 * Delete all remaining routes using this interface
898 * Unfortuneatly the only way to do this is to slog through
899 * the entire routing table looking for routes which point
900 * to this interface...oh well...
902 for (i = 1; i <= AF_MAX; i++) {
903 for (j = 0; j < rt_numfibs; j++) {
904 rnh = rt_tables_get_rnh(j, i);
907 RADIX_NODE_HEAD_LOCK(rnh);
908 (void) rnh->rnh_walktree(rnh, if_rtdel, ifp);
909 RADIX_NODE_HEAD_UNLOCK(rnh);
913 /* Announce that the interface is gone. */
914 rt_ifannouncemsg(ifp, IFAN_DEPARTURE);
915 EVENTHANDLER_INVOKE(ifnet_departure_event, ifp);
916 if (IS_DEFAULT_VNET(curvnet))
917 devctl_notify("IFNET", ifp->if_xname, "DETACH", NULL);
921 * We cannot hold the lock over dom_ifdetach calls as they might
922 * sleep, for example trying to drain a callout, thus open up the
923 * theoretical race with re-attaching.
926 i = ifp->if_afdata_initialized;
927 ifp->if_afdata_initialized = 0;
928 IF_AFDATA_UNLOCK(ifp);
929 for (dp = domains; i > 0 && dp; dp = dp->dom_next) {
930 if (dp->dom_ifdetach && ifp->if_afdata[dp->dom_family])
931 (*dp->dom_ifdetach)(ifp,
932 ifp->if_afdata[dp->dom_family]);
938 * if_vmove() performs a limited version of if_detach() in current
939 * vnet and if_attach()es the ifnet to the vnet specified as 2nd arg.
940 * An attempt is made to shrink if_index in current vnet, find an
941 * unused if_index in target vnet and calls if_grow() if necessary,
942 * and finally find an unused if_xname for the target vnet.
945 if_vmove(struct ifnet *ifp, struct vnet *new_vnet)
950 * Detach from current vnet, but preserve LLADDR info, do not
951 * mark as dead etc. so that the ifnet can be reattached later.
953 if_detach_internal(ifp, 1);
956 * Unlink the ifnet from ifindex_table[] in current vnet, and shrink
957 * the if_index for that vnet if possible.
959 * NOTE: IFNET_WLOCK/IFNET_WUNLOCK() are assumed to be unvirtualized,
960 * or we'd lock on one vnet and unlock on another.
963 ifindex_free_locked(ifp->if_index);
967 * Perform interface-specific reassignment tasks, if provided by
970 if (ifp->if_reassign != NULL)
971 ifp->if_reassign(ifp, new_vnet, NULL);
974 * Switch to the context of the target vnet.
976 CURVNET_SET_QUIET(new_vnet);
979 if (ifindex_alloc_locked(&idx) != 0) {
981 panic("if_index overflow");
984 ifnet_setbyindex_locked(ifp->if_index, ifp);
987 if_attach_internal(ifp, 1);
993 * Move an ifnet to or from another child prison/vnet, specified by the jail id.
996 if_vmove_loan(struct thread *td, struct ifnet *ifp, char *ifname, int jid)
1001 /* Try to find the prison within our visibility. */
1002 sx_slock(&allprison_lock);
1003 pr = prison_find_child(td->td_ucred->cr_prison, jid);
1004 sx_sunlock(&allprison_lock);
1007 prison_hold_locked(pr);
1008 mtx_unlock(&pr->pr_mtx);
1010 /* Do not try to move the iface from and to the same prison. */
1011 if (pr->pr_vnet == ifp->if_vnet) {
1016 /* Make sure the named iface does not exists in the dst. prison/vnet. */
1017 /* XXX Lock interfaces to avoid races. */
1018 CURVNET_SET_QUIET(pr->pr_vnet);
1019 difp = ifunit(ifname);
1026 /* Move the interface into the child jail/vnet. */
1027 if_vmove(ifp, pr->pr_vnet);
1029 /* Report the new if_xname back to the userland. */
1030 sprintf(ifname, "%s", ifp->if_xname);
1037 if_vmove_reclaim(struct thread *td, char *ifname, int jid)
1040 struct vnet *vnet_dst;
1043 /* Try to find the prison within our visibility. */
1044 sx_slock(&allprison_lock);
1045 pr = prison_find_child(td->td_ucred->cr_prison, jid);
1046 sx_sunlock(&allprison_lock);
1049 prison_hold_locked(pr);
1050 mtx_unlock(&pr->pr_mtx);
1052 /* Make sure the named iface exists in the source prison/vnet. */
1053 CURVNET_SET(pr->pr_vnet);
1054 ifp = ifunit(ifname); /* XXX Lock to avoid races. */
1061 /* Do not try to move the iface from and to the same prison. */
1062 vnet_dst = TD_TO_VNET(td);
1063 if (vnet_dst == ifp->if_vnet) {
1069 /* Get interface back from child jail/vnet. */
1070 if_vmove(ifp, vnet_dst);
1073 /* Report the new if_xname back to the userland. */
1074 sprintf(ifname, "%s", ifp->if_xname);
1082 * Add a group to an interface
1085 if_addgroup(struct ifnet *ifp, const char *groupname)
1087 struct ifg_list *ifgl;
1088 struct ifg_group *ifg = NULL;
1089 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 EVENTHANDLER_INVOKE(group_attach_event, ifg);
1131 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_UNLOCK(ifp);
1145 EVENTHANDLER_INVOKE(group_change_event, groupname);
1151 * Remove a group from an interface
1154 if_delgroup(struct ifnet *ifp, const char *groupname)
1156 struct ifg_list *ifgl;
1157 struct ifg_member *ifgm;
1160 TAILQ_FOREACH(ifgl, &ifp->if_groups, ifgl_next)
1161 if (!strcmp(ifgl->ifgl_group->ifg_group, groupname))
1169 TAILQ_REMOVE(&ifp->if_groups, ifgl, ifgl_next);
1170 IF_ADDR_UNLOCK(ifp);
1172 TAILQ_FOREACH(ifgm, &ifgl->ifgl_group->ifg_members, ifgm_next)
1173 if (ifgm->ifgm_ifp == ifp)
1177 TAILQ_REMOVE(&ifgl->ifgl_group->ifg_members, ifgm, ifgm_next);
1181 if (--ifgl->ifgl_group->ifg_refcnt == 0) {
1182 TAILQ_REMOVE(&V_ifg_head, ifgl->ifgl_group, ifg_next);
1183 EVENTHANDLER_INVOKE(group_detach_event, ifgl->ifgl_group);
1184 free(ifgl->ifgl_group, M_TEMP);
1190 EVENTHANDLER_INVOKE(group_change_event, groupname);
1196 * Remove an interface from all groups
1199 if_delgroups(struct ifnet *ifp)
1201 struct ifg_list *ifgl;
1202 struct ifg_member *ifgm;
1203 char groupname[IFNAMSIZ];
1206 while (!TAILQ_EMPTY(&ifp->if_groups)) {
1207 ifgl = TAILQ_FIRST(&ifp->if_groups);
1209 strlcpy(groupname, ifgl->ifgl_group->ifg_group, IFNAMSIZ);
1212 TAILQ_REMOVE(&ifp->if_groups, ifgl, ifgl_next);
1213 IF_ADDR_UNLOCK(ifp);
1215 TAILQ_FOREACH(ifgm, &ifgl->ifgl_group->ifg_members, ifgm_next)
1216 if (ifgm->ifgm_ifp == ifp)
1220 TAILQ_REMOVE(&ifgl->ifgl_group->ifg_members, ifgm,
1225 if (--ifgl->ifgl_group->ifg_refcnt == 0) {
1226 TAILQ_REMOVE(&V_ifg_head, ifgl->ifgl_group, ifg_next);
1227 EVENTHANDLER_INVOKE(group_detach_event,
1229 free(ifgl->ifgl_group, M_TEMP);
1235 EVENTHANDLER_INVOKE(group_change_event, groupname);
1243 * Stores all groups from an interface in memory pointed
1247 if_getgroup(struct ifgroupreq *data, struct ifnet *ifp)
1250 struct ifg_list *ifgl;
1251 struct ifg_req ifgrq, *ifgp;
1252 struct ifgroupreq *ifgr = data;
1254 if (ifgr->ifgr_len == 0) {
1256 TAILQ_FOREACH(ifgl, &ifp->if_groups, ifgl_next)
1257 ifgr->ifgr_len += sizeof(struct ifg_req);
1258 IF_ADDR_UNLOCK(ifp);
1262 len = ifgr->ifgr_len;
1263 ifgp = ifgr->ifgr_groups;
1266 TAILQ_FOREACH(ifgl, &ifp->if_groups, ifgl_next) {
1267 if (len < sizeof(ifgrq)) {
1268 IF_ADDR_UNLOCK(ifp);
1271 bzero(&ifgrq, sizeof ifgrq);
1272 strlcpy(ifgrq.ifgrq_group, ifgl->ifgl_group->ifg_group,
1273 sizeof(ifgrq.ifgrq_group));
1274 if ((error = copyout(&ifgrq, ifgp, sizeof(struct ifg_req)))) {
1275 IF_ADDR_UNLOCK(ifp);
1278 len -= sizeof(ifgrq);
1281 IF_ADDR_UNLOCK(ifp);
1287 * Stores all members of a group in memory pointed to by data
1290 if_getgroupmembers(struct ifgroupreq *data)
1292 struct ifgroupreq *ifgr = data;
1293 struct ifg_group *ifg;
1294 struct ifg_member *ifgm;
1295 struct ifg_req ifgrq, *ifgp;
1299 TAILQ_FOREACH(ifg, &V_ifg_head, ifg_next)
1300 if (!strcmp(ifg->ifg_group, ifgr->ifgr_name))
1307 if (ifgr->ifgr_len == 0) {
1308 TAILQ_FOREACH(ifgm, &ifg->ifg_members, ifgm_next)
1309 ifgr->ifgr_len += sizeof(ifgrq);
1314 len = ifgr->ifgr_len;
1315 ifgp = ifgr->ifgr_groups;
1316 TAILQ_FOREACH(ifgm, &ifg->ifg_members, ifgm_next) {
1317 if (len < sizeof(ifgrq)) {
1321 bzero(&ifgrq, sizeof ifgrq);
1322 strlcpy(ifgrq.ifgrq_member, ifgm->ifgm_ifp->if_xname,
1323 sizeof(ifgrq.ifgrq_member));
1324 if ((error = copyout(&ifgrq, ifgp, sizeof(struct ifg_req)))) {
1328 len -= sizeof(ifgrq);
1337 * Delete Routes for a Network Interface
1339 * Called for each routing entry via the rnh->rnh_walktree() call above
1340 * to delete all route entries referencing a detaching network interface.
1343 * rn pointer to node in the routing table
1344 * arg argument passed to rnh->rnh_walktree() - detaching interface
1348 * errno failed - reason indicated
1352 if_rtdel(struct radix_node *rn, void *arg)
1354 struct rtentry *rt = (struct rtentry *)rn;
1355 struct ifnet *ifp = arg;
1358 if (rt->rt_ifp == ifp) {
1361 * Protect (sorta) against walktree recursion problems
1362 * with cloned routes
1364 if ((rt->rt_flags & RTF_UP) == 0)
1367 err = rtrequest_fib(RTM_DELETE, rt_key(rt), rt->rt_gateway,
1368 rt_mask(rt), rt->rt_flags|RTF_RNH_LOCKED,
1369 (struct rtentry **) NULL, rt->rt_fibnum);
1371 log(LOG_WARNING, "if_rtdel: error %d\n", err);
1379 * Wrapper functions for struct ifnet address list locking macros. These are
1380 * used by kernel modules to avoid encoding programming interface or binary
1381 * interface assumptions that may be violated when kernel-internal locking
1382 * approaches change.
1385 if_addr_rlock(struct ifnet *ifp)
1392 if_addr_runlock(struct ifnet *ifp)
1395 IF_ADDR_UNLOCK(ifp);
1399 if_maddr_rlock(struct ifnet *ifp)
1406 if_maddr_runlock(struct ifnet *ifp)
1409 IF_ADDR_UNLOCK(ifp);
1413 * Reference count functions for ifaddrs.
1416 ifa_init(struct ifaddr *ifa)
1419 mtx_init(&ifa->ifa_mtx, "ifaddr", NULL, MTX_DEF);
1420 refcount_init(&ifa->ifa_refcnt, 1);
1424 ifa_ref(struct ifaddr *ifa)
1427 refcount_acquire(&ifa->ifa_refcnt);
1431 ifa_free(struct ifaddr *ifa)
1434 if (refcount_release(&ifa->ifa_refcnt)) {
1435 mtx_destroy(&ifa->ifa_mtx);
1436 free(ifa, M_IFADDR);
1441 ifa_add_loopback_route(struct ifaddr *ifa, struct sockaddr *ia)
1444 struct rtentry *rt = NULL;
1445 struct rt_addrinfo info;
1446 static struct sockaddr_dl null_sdl = {sizeof(null_sdl), AF_LINK};
1448 bzero(&info, sizeof(info));
1449 info.rti_ifp = V_loif;
1450 info.rti_flags = ifa->ifa_flags | RTF_HOST | RTF_STATIC;
1451 info.rti_info[RTAX_DST] = ia;
1452 info.rti_info[RTAX_GATEWAY] = (struct sockaddr *)&null_sdl;
1453 error = rtrequest1_fib(RTM_ADD, &info, &rt, 0);
1455 if (error == 0 && rt != NULL) {
1457 ((struct sockaddr_dl *)rt->rt_gateway)->sdl_type =
1458 ifa->ifa_ifp->if_type;
1459 ((struct sockaddr_dl *)rt->rt_gateway)->sdl_index =
1460 ifa->ifa_ifp->if_index;
1463 } else if (error != 0)
1464 log(LOG_INFO, "ifa_add_loopback_route: insertion failed\n");
1470 ifa_del_loopback_route(struct ifaddr *ifa, struct sockaddr *ia)
1473 struct rt_addrinfo info;
1474 struct sockaddr_dl null_sdl;
1476 bzero(&null_sdl, sizeof(null_sdl));
1477 null_sdl.sdl_len = sizeof(null_sdl);
1478 null_sdl.sdl_family = AF_LINK;
1479 null_sdl.sdl_type = ifa->ifa_ifp->if_type;
1480 null_sdl.sdl_index = ifa->ifa_ifp->if_index;
1481 bzero(&info, sizeof(info));
1482 info.rti_flags = ifa->ifa_flags | RTF_HOST | RTF_STATIC;
1483 info.rti_info[RTAX_DST] = ia;
1484 info.rti_info[RTAX_GATEWAY] = (struct sockaddr *)&null_sdl;
1485 error = rtrequest1_fib(RTM_DELETE, &info, NULL, 0);
1488 log(LOG_INFO, "ifa_del_loopback_route: deletion failed\n");
1494 * XXX: Because sockaddr_dl has deeper structure than the sockaddr
1495 * structs used to represent other address families, it is necessary
1496 * to perform a different comparison.
1499 #define sa_equal(a1, a2) \
1500 (bcmp((a1), (a2), ((a1))->sa_len) == 0)
1502 #define sa_dl_equal(a1, a2) \
1503 ((((struct sockaddr_dl *)(a1))->sdl_len == \
1504 ((struct sockaddr_dl *)(a2))->sdl_len) && \
1505 (bcmp(LLADDR((struct sockaddr_dl *)(a1)), \
1506 LLADDR((struct sockaddr_dl *)(a2)), \
1507 ((struct sockaddr_dl *)(a1))->sdl_alen) == 0))
1510 * Locate an interface based on a complete address.
1513 static struct ifaddr *
1514 ifa_ifwithaddr_internal(struct sockaddr *addr, int getref)
1519 IFNET_RLOCK_NOSLEEP();
1520 TAILQ_FOREACH(ifp, &V_ifnet, if_link) {
1522 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
1523 if (ifa->ifa_addr->sa_family != addr->sa_family)
1525 if (sa_equal(addr, ifa->ifa_addr)) {
1528 IF_ADDR_UNLOCK(ifp);
1531 /* IP6 doesn't have broadcast */
1532 if ((ifp->if_flags & IFF_BROADCAST) &&
1533 ifa->ifa_broadaddr &&
1534 ifa->ifa_broadaddr->sa_len != 0 &&
1535 sa_equal(ifa->ifa_broadaddr, addr)) {
1538 IF_ADDR_UNLOCK(ifp);
1542 IF_ADDR_UNLOCK(ifp);
1546 IFNET_RUNLOCK_NOSLEEP();
1551 ifa_ifwithaddr(struct sockaddr *addr)
1554 return (ifa_ifwithaddr_internal(addr, 1));
1558 ifa_ifwithaddr_check(struct sockaddr *addr)
1561 return (ifa_ifwithaddr_internal(addr, 0) != NULL);
1565 * Locate an interface based on the broadcast address.
1569 ifa_ifwithbroadaddr(struct sockaddr *addr)
1574 IFNET_RLOCK_NOSLEEP();
1575 TAILQ_FOREACH(ifp, &V_ifnet, if_link) {
1577 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
1578 if (ifa->ifa_addr->sa_family != addr->sa_family)
1580 if ((ifp->if_flags & IFF_BROADCAST) &&
1581 ifa->ifa_broadaddr &&
1582 ifa->ifa_broadaddr->sa_len != 0 &&
1583 sa_equal(ifa->ifa_broadaddr, addr)) {
1585 IF_ADDR_UNLOCK(ifp);
1589 IF_ADDR_UNLOCK(ifp);
1593 IFNET_RUNLOCK_NOSLEEP();
1598 * Locate the point to point interface with a given destination address.
1602 ifa_ifwithdstaddr(struct sockaddr *addr)
1607 IFNET_RLOCK_NOSLEEP();
1608 TAILQ_FOREACH(ifp, &V_ifnet, if_link) {
1609 if ((ifp->if_flags & IFF_POINTOPOINT) == 0)
1612 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
1613 if (ifa->ifa_addr->sa_family != addr->sa_family)
1615 if (ifa->ifa_dstaddr != NULL &&
1616 sa_equal(addr, ifa->ifa_dstaddr)) {
1618 IF_ADDR_UNLOCK(ifp);
1622 IF_ADDR_UNLOCK(ifp);
1626 IFNET_RUNLOCK_NOSLEEP();
1631 * Find an interface on a specific network. If many, choice
1632 * is most specific found.
1635 ifa_ifwithnet(struct sockaddr *addr, int ignore_ptp)
1639 struct ifaddr *ifa_maybe = NULL;
1640 u_int af = addr->sa_family;
1641 char *addr_data = addr->sa_data, *cplim;
1644 * AF_LINK addresses can be looked up directly by their index number,
1645 * so do that if we can.
1647 if (af == AF_LINK) {
1648 struct sockaddr_dl *sdl = (struct sockaddr_dl *)addr;
1649 if (sdl->sdl_index && sdl->sdl_index <= V_if_index)
1650 return (ifaddr_byindex(sdl->sdl_index));
1654 * Scan though each interface, looking for ones that have addresses
1655 * in this address family. Maintain a reference on ifa_maybe once
1656 * we find one, as we release the IF_ADDR_LOCK() that kept it stable
1657 * when we move onto the next interface.
1659 IFNET_RLOCK_NOSLEEP();
1660 TAILQ_FOREACH(ifp, &V_ifnet, if_link) {
1662 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
1663 char *cp, *cp2, *cp3;
1665 if (ifa->ifa_addr->sa_family != af)
1667 if (af == AF_INET &&
1668 ifp->if_flags & IFF_POINTOPOINT && !ignore_ptp) {
1670 * This is a bit broken as it doesn't
1671 * take into account that the remote end may
1672 * be a single node in the network we are
1674 * The trouble is that we don't know the
1675 * netmask for the remote end.
1677 if (ifa->ifa_dstaddr != NULL &&
1678 sa_equal(addr, ifa->ifa_dstaddr)) {
1680 IF_ADDR_UNLOCK(ifp);
1685 * if we have a special address handler,
1686 * then use it instead of the generic one.
1688 if (ifa->ifa_claim_addr) {
1689 if ((*ifa->ifa_claim_addr)(ifa, addr)) {
1691 IF_ADDR_UNLOCK(ifp);
1698 * Scan all the bits in the ifa's address.
1699 * If a bit dissagrees with what we are
1700 * looking for, mask it with the netmask
1701 * to see if it really matters.
1702 * (A byte at a time)
1704 if (ifa->ifa_netmask == 0)
1707 cp2 = ifa->ifa_addr->sa_data;
1708 cp3 = ifa->ifa_netmask->sa_data;
1709 cplim = ifa->ifa_netmask->sa_len
1710 + (char *)ifa->ifa_netmask;
1712 if ((*cp++ ^ *cp2++) & *cp3++)
1713 goto next; /* next address! */
1715 * If the netmask of what we just found
1716 * is more specific than what we had before
1717 * (if we had one) then remember the new one
1718 * before continuing to search
1719 * for an even better one.
1721 if (ifa_maybe == NULL ||
1722 rn_refines((caddr_t)ifa->ifa_netmask,
1723 (caddr_t)ifa_maybe->ifa_netmask)) {
1724 if (ifa_maybe != NULL)
1725 ifa_free(ifa_maybe);
1731 IF_ADDR_UNLOCK(ifp);
1736 IFNET_RUNLOCK_NOSLEEP();
1737 if (ifa_maybe != NULL)
1738 ifa_free(ifa_maybe);
1743 * Find an interface address specific to an interface best matching
1747 ifaof_ifpforaddr(struct sockaddr *addr, struct ifnet *ifp)
1750 char *cp, *cp2, *cp3;
1752 struct ifaddr *ifa_maybe = NULL;
1753 u_int af = addr->sa_family;
1758 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
1759 if (ifa->ifa_addr->sa_family != af)
1761 if (ifa_maybe == NULL)
1763 if (ifa->ifa_netmask == 0) {
1764 if (sa_equal(addr, ifa->ifa_addr) ||
1765 (ifa->ifa_dstaddr &&
1766 sa_equal(addr, ifa->ifa_dstaddr)))
1770 if (ifp->if_flags & IFF_POINTOPOINT) {
1771 if (sa_equal(addr, ifa->ifa_dstaddr))
1775 cp2 = ifa->ifa_addr->sa_data;
1776 cp3 = ifa->ifa_netmask->sa_data;
1777 cplim = ifa->ifa_netmask->sa_len + (char *)ifa->ifa_netmask;
1778 for (; cp3 < cplim; cp3++)
1779 if ((*cp++ ^ *cp2++) & *cp3)
1789 IF_ADDR_UNLOCK(ifp);
1793 #include <net/if_llatbl.h>
1796 * Default action when installing a route with a Link Level gateway.
1797 * Lookup an appropriate real ifa to point to.
1798 * This should be moved to /sys/net/link.c eventually.
1801 link_rtrequest(int cmd, struct rtentry *rt, struct rt_addrinfo *info)
1803 struct ifaddr *ifa, *oifa;
1804 struct sockaddr *dst;
1809 if (cmd != RTM_ADD || ((ifa = rt->rt_ifa) == 0) ||
1810 ((ifp = ifa->ifa_ifp) == 0) || ((dst = rt_key(rt)) == 0))
1812 ifa = ifaof_ifpforaddr(dst, ifp);
1817 if (ifa->ifa_rtrequest && ifa->ifa_rtrequest != link_rtrequest)
1818 ifa->ifa_rtrequest(cmd, rt, info);
1823 * Mark an interface down and notify protocols of
1825 * NOTE: must be called at splnet or eqivalent.
1828 if_unroute(struct ifnet *ifp, int flag, int fam)
1832 KASSERT(flag == IFF_UP, ("if_unroute: flag != IFF_UP"));
1834 ifp->if_flags &= ~flag;
1835 getmicrotime(&ifp->if_lastchange);
1836 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link)
1837 if (fam == PF_UNSPEC || (fam == ifa->ifa_addr->sa_family))
1838 pfctlinput(PRC_IFDOWN, ifa->ifa_addr);
1839 ifp->if_qflush(ifp);
1842 (*carp_linkstate_p)(ifp);
1847 * Mark an interface up and notify protocols of
1849 * NOTE: must be called at splnet or eqivalent.
1852 if_route(struct ifnet *ifp, int flag, int fam)
1856 KASSERT(flag == IFF_UP, ("if_route: flag != IFF_UP"));
1858 ifp->if_flags |= flag;
1859 getmicrotime(&ifp->if_lastchange);
1860 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link)
1861 if (fam == PF_UNSPEC || (fam == ifa->ifa_addr->sa_family))
1862 pfctlinput(PRC_IFUP, ifa->ifa_addr);
1864 (*carp_linkstate_p)(ifp);
1871 void (*vlan_link_state_p)(struct ifnet *); /* XXX: private from if_vlan */
1872 void (*vlan_trunk_cap_p)(struct ifnet *); /* XXX: private from if_vlan */
1875 * Handle a change in the interface link state. To avoid LORs
1876 * between driver lock and upper layer locks, as well as possible
1877 * recursions, we post event to taskqueue, and all job
1878 * is done in static do_link_state_change().
1881 if_link_state_change(struct ifnet *ifp, int link_state)
1883 /* Return if state hasn't changed. */
1884 if (ifp->if_link_state == link_state)
1887 ifp->if_link_state = link_state;
1889 taskqueue_enqueue(taskqueue_swi, &ifp->if_linktask);
1893 do_link_state_change(void *arg, int pending)
1895 struct ifnet *ifp = (struct ifnet *)arg;
1896 int link_state = ifp->if_link_state;
1897 CURVNET_SET(ifp->if_vnet);
1899 /* Notify that the link state has changed. */
1901 if (ifp->if_vlantrunk != NULL)
1902 (*vlan_link_state_p)(ifp);
1904 if ((ifp->if_type == IFT_ETHER || ifp->if_type == IFT_L2VLAN) &&
1905 IFP2AC(ifp)->ac_netgraph != NULL)
1906 (*ng_ether_link_state_p)(ifp, link_state);
1908 (*carp_linkstate_p)(ifp);
1909 if (ifp->if_bridge) {
1910 KASSERT(bstp_linkstate_p != NULL,("if_bridge bstp not loaded!"));
1911 (*bstp_linkstate_p)(ifp, link_state);
1914 KASSERT(lagg_linkstate_p != NULL,("if_lagg not loaded!"));
1915 (*lagg_linkstate_p)(ifp, link_state);
1918 if (IS_DEFAULT_VNET(curvnet))
1919 devctl_notify("IFNET", ifp->if_xname,
1920 (link_state == LINK_STATE_UP) ? "LINK_UP" : "LINK_DOWN",
1923 if_printf(ifp, "%d link states coalesced\n", pending);
1924 if (log_link_state_change)
1925 log(LOG_NOTICE, "%s: link state changed to %s\n", ifp->if_xname,
1926 (link_state == LINK_STATE_UP) ? "UP" : "DOWN" );
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 error = priv_check(td, PRIV_NET_SETIFFLAGS);
2125 * Currently, no driver owned flags pass the IFF_CANTCHANGE
2126 * check, so we don't need special handling here yet.
2128 new_flags = (ifr->ifr_flags & 0xffff) |
2129 (ifr->ifr_flagshigh << 16);
2130 if (ifp->if_flags & IFF_SMART) {
2131 /* Smart drivers twiddle their own routes */
2132 } else if (ifp->if_flags & IFF_UP &&
2133 (new_flags & IFF_UP) == 0) {
2137 } else if (new_flags & IFF_UP &&
2138 (ifp->if_flags & IFF_UP) == 0) {
2143 /* See if permanently promiscuous mode bit is about to flip */
2144 if ((ifp->if_flags ^ new_flags) & IFF_PPROMISC) {
2145 if (new_flags & IFF_PPROMISC)
2146 ifp->if_flags |= IFF_PROMISC;
2147 else if (ifp->if_pcount == 0)
2148 ifp->if_flags &= ~IFF_PROMISC;
2149 log(LOG_INFO, "%s: permanently promiscuous mode %s\n",
2151 (new_flags & IFF_PPROMISC) ? "enabled" : "disabled");
2153 ifp->if_flags = (ifp->if_flags & IFF_CANTCHANGE) |
2154 (new_flags &~ IFF_CANTCHANGE);
2155 if (ifp->if_ioctl) {
2156 (void) (*ifp->if_ioctl)(ifp, cmd, data);
2158 getmicrotime(&ifp->if_lastchange);
2162 error = priv_check(td, PRIV_NET_SETIFCAP);
2165 if (ifp->if_ioctl == NULL)
2166 return (EOPNOTSUPP);
2167 if (ifr->ifr_reqcap & ~ifp->if_capabilities)
2169 error = (*ifp->if_ioctl)(ifp, cmd, data);
2171 getmicrotime(&ifp->if_lastchange);
2176 error = mac_ifnet_ioctl_set(td->td_ucred, ifr, ifp);
2181 error = priv_check(td, PRIV_NET_SETIFNAME);
2184 error = copyinstr(ifr->ifr_data, new_name, IFNAMSIZ, NULL);
2187 if (new_name[0] == '\0')
2189 if (ifunit(new_name) != NULL)
2193 * XXX: Locking. Nothing else seems to lock if_flags,
2194 * and there are numerous other races with the
2195 * ifunit() checks not being atomic with namespace
2196 * changes (renames, vmoves, if_attach, etc).
2198 ifp->if_flags |= IFF_RENAMING;
2200 /* Announce the departure of the interface. */
2201 rt_ifannouncemsg(ifp, IFAN_DEPARTURE);
2202 EVENTHANDLER_INVOKE(ifnet_departure_event, ifp);
2204 log(LOG_INFO, "%s: changing name to '%s'\n",
2205 ifp->if_xname, new_name);
2207 strlcpy(ifp->if_xname, new_name, sizeof(ifp->if_xname));
2210 sdl = (struct sockaddr_dl *)ifa->ifa_addr;
2211 namelen = strlen(new_name);
2212 onamelen = sdl->sdl_nlen;
2214 * Move the address if needed. This is safe because we
2215 * allocate space for a name of length IFNAMSIZ when we
2216 * create this in if_attach().
2218 if (namelen != onamelen) {
2219 bcopy(sdl->sdl_data + onamelen,
2220 sdl->sdl_data + namelen, sdl->sdl_alen);
2222 bcopy(new_name, sdl->sdl_data, namelen);
2223 sdl->sdl_nlen = namelen;
2224 sdl = (struct sockaddr_dl *)ifa->ifa_netmask;
2225 bzero(sdl->sdl_data, onamelen);
2226 while (namelen != 0)
2227 sdl->sdl_data[--namelen] = 0xff;
2230 EVENTHANDLER_INVOKE(ifnet_arrival_event, ifp);
2231 /* Announce the return of the interface. */
2232 rt_ifannouncemsg(ifp, IFAN_ARRIVAL);
2234 ifp->if_flags &= ~IFF_RENAMING;
2239 error = priv_check(td, PRIV_NET_SETIFVNET);
2242 error = if_vmove_loan(td, ifp, ifr->ifr_name, ifr->ifr_jid);
2247 error = priv_check(td, PRIV_NET_SETIFMETRIC);
2250 ifp->if_metric = ifr->ifr_metric;
2251 getmicrotime(&ifp->if_lastchange);
2255 error = priv_check(td, PRIV_NET_SETIFPHYS);
2258 if (ifp->if_ioctl == NULL)
2259 return (EOPNOTSUPP);
2260 error = (*ifp->if_ioctl)(ifp, cmd, data);
2262 getmicrotime(&ifp->if_lastchange);
2267 u_long oldmtu = ifp->if_mtu;
2269 error = priv_check(td, PRIV_NET_SETIFMTU);
2272 if (ifr->ifr_mtu < IF_MINMTU || ifr->ifr_mtu > IF_MAXMTU)
2274 if (ifp->if_ioctl == NULL)
2275 return (EOPNOTSUPP);
2276 error = (*ifp->if_ioctl)(ifp, cmd, data);
2278 getmicrotime(&ifp->if_lastchange);
2282 * If the link MTU changed, do network layer specific procedure.
2284 if (ifp->if_mtu != oldmtu) {
2294 if (cmd == SIOCADDMULTI)
2295 error = priv_check(td, PRIV_NET_ADDMULTI);
2297 error = priv_check(td, PRIV_NET_DELMULTI);
2301 /* Don't allow group membership on non-multicast interfaces. */
2302 if ((ifp->if_flags & IFF_MULTICAST) == 0)
2303 return (EOPNOTSUPP);
2305 /* Don't let users screw up protocols' entries. */
2306 if (ifr->ifr_addr.sa_family != AF_LINK)
2309 if (cmd == SIOCADDMULTI) {
2310 struct ifmultiaddr *ifma;
2313 * Userland is only permitted to join groups once
2314 * via the if_addmulti() KPI, because it cannot hold
2315 * struct ifmultiaddr * between calls. It may also
2316 * lose a race while we check if the membership
2320 ifma = if_findmulti(ifp, &ifr->ifr_addr);
2321 IF_ADDR_UNLOCK(ifp);
2325 error = if_addmulti(ifp, &ifr->ifr_addr, &ifma);
2327 error = if_delmulti(ifp, &ifr->ifr_addr);
2330 getmicrotime(&ifp->if_lastchange);
2333 case SIOCSIFPHYADDR:
2334 case SIOCDIFPHYADDR:
2336 case SIOCSIFPHYADDR_IN6:
2338 case SIOCSLIFPHYADDR:
2340 case SIOCSIFGENERIC:
2341 error = priv_check(td, PRIV_NET_HWIOCTL);
2344 if (ifp->if_ioctl == NULL)
2345 return (EOPNOTSUPP);
2346 error = (*ifp->if_ioctl)(ifp, cmd, data);
2348 getmicrotime(&ifp->if_lastchange);
2352 ifs = (struct ifstat *)data;
2353 ifs->ascii[0] = '\0';
2355 case SIOCGIFPSRCADDR:
2356 case SIOCGIFPDSTADDR:
2357 case SIOCGLIFPHYADDR:
2359 case SIOCGIFGENERIC:
2360 if (ifp->if_ioctl == NULL)
2361 return (EOPNOTSUPP);
2362 error = (*ifp->if_ioctl)(ifp, cmd, data);
2366 error = priv_check(td, PRIV_NET_SETLLADDR);
2369 error = if_setlladdr(ifp,
2370 ifr->ifr_addr.sa_data, ifr->ifr_addr.sa_len);
2371 EVENTHANDLER_INVOKE(iflladdr_event, ifp);
2376 struct ifgroupreq *ifgr = (struct ifgroupreq *)ifr;
2378 error = priv_check(td, PRIV_NET_ADDIFGROUP);
2381 if ((error = if_addgroup(ifp, ifgr->ifgr_group)))
2387 if ((error = if_getgroup((struct ifgroupreq *)ifr, ifp)))
2393 struct ifgroupreq *ifgr = (struct ifgroupreq *)ifr;
2395 error = priv_check(td, PRIV_NET_DELIFGROUP);
2398 if ((error = if_delgroup(ifp, ifgr->ifgr_group)))
2410 #ifdef COMPAT_FREEBSD32
2418 #define SIOCGIFCONF32 _IOWR('i', 36, struct ifconf32)
2425 ifioctl(struct socket *so, u_long cmd, caddr_t data, struct thread *td)
2435 return (ifconf(cmd, data));
2437 #ifdef COMPAT_FREEBSD32
2440 struct ifconf32 *ifc32;
2443 ifc32 = (struct ifconf32 *)data;
2444 ifc.ifc_len = ifc32->ifc_len;
2445 ifc.ifc_buf = PTRIN(ifc32->ifc_buf);
2447 return (ifconf(SIOCGIFCONF, (void *)&ifc));
2451 ifr = (struct ifreq *)data;
2456 error = priv_check(td, PRIV_NET_SETIFVNET);
2459 return (if_vmove_reclaim(td, ifr->ifr_name, ifr->ifr_jid));
2463 error = priv_check(td, PRIV_NET_IFCREATE);
2466 return (if_clone_create(ifr->ifr_name, sizeof(ifr->ifr_name),
2467 cmd == SIOCIFCREATE2 ? ifr->ifr_data : NULL));
2469 error = priv_check(td, PRIV_NET_IFDESTROY);
2472 return if_clone_destroy(ifr->ifr_name);
2474 case SIOCIFGCLONERS:
2475 return (if_clone_list((struct if_clonereq *)data));
2477 return (if_getgroupmembers((struct ifgroupreq *)data));
2480 ifp = ifunit_ref(ifr->ifr_name);
2484 error = ifhwioctl(cmd, ifp, data, td);
2485 if (error != ENOIOCTL) {
2490 oif_flags = ifp->if_flags;
2491 if (so->so_proto == NULL) {
2493 return (EOPNOTSUPP);
2496 error = ((*so->so_proto->pr_usrreqs->pru_control)(so, cmd,
2499 if (error == EOPNOTSUPP && ifp != NULL && ifp->if_ioctl != NULL)
2500 error = (*ifp->if_ioctl)(ifp, cmd, data);
2507 case SIOCSIFDSTADDR:
2509 case SIOCSIFBRDADDR:
2510 case SIOCSIFNETMASK:
2511 #if BYTE_ORDER != BIG_ENDIAN
2512 if (ifr->ifr_addr.sa_family == 0 &&
2513 ifr->ifr_addr.sa_len < 16) {
2514 ifr->ifr_addr.sa_family = ifr->ifr_addr.sa_len;
2515 ifr->ifr_addr.sa_len = 16;
2518 if (ifr->ifr_addr.sa_len == 0)
2519 ifr->ifr_addr.sa_len = 16;
2527 case OSIOCGIFDSTADDR:
2528 cmd = SIOCGIFDSTADDR;
2531 case OSIOCGIFBRDADDR:
2532 cmd = SIOCGIFBRDADDR;
2535 case OSIOCGIFNETMASK:
2536 cmd = SIOCGIFNETMASK;
2538 error = ((*so->so_proto->pr_usrreqs->pru_control)(so,
2542 if (error == EOPNOTSUPP && ifp != NULL &&
2543 ifp->if_ioctl != NULL)
2544 error = (*ifp->if_ioctl)(ifp, cmd, data);
2548 case OSIOCGIFDSTADDR:
2549 case OSIOCGIFBRDADDR:
2550 case OSIOCGIFNETMASK:
2551 *(u_short *)&ifr->ifr_addr = ifr->ifr_addr.sa_family;
2555 #endif /* COMPAT_43 */
2557 if ((oif_flags ^ ifp->if_flags) & IFF_UP) {
2559 if (ifp->if_flags & IFF_UP) {
2571 * The code common to handling reference counted flags,
2572 * e.g., in ifpromisc() and if_allmulti().
2573 * The "pflag" argument can specify a permanent mode flag to check,
2574 * such as IFF_PPROMISC for promiscuous mode; should be 0 if none.
2576 * Only to be used on stack-owned flags, not driver-owned flags.
2579 if_setflag(struct ifnet *ifp, int flag, int pflag, int *refcount, int onswitch)
2583 int oldflags, oldcount;
2585 /* Sanity checks to catch programming errors */
2586 KASSERT((flag & (IFF_DRV_OACTIVE|IFF_DRV_RUNNING)) == 0,
2587 ("%s: setting driver-owned flag %d", __func__, flag));
2590 KASSERT(*refcount >= 0,
2591 ("%s: increment negative refcount %d for flag %d",
2592 __func__, *refcount, flag));
2594 KASSERT(*refcount > 0,
2595 ("%s: decrement non-positive refcount %d for flag %d",
2596 __func__, *refcount, flag));
2598 /* In case this mode is permanent, just touch refcount */
2599 if (ifp->if_flags & pflag) {
2600 *refcount += onswitch ? 1 : -1;
2604 /* Save ifnet parameters for if_ioctl() may fail */
2605 oldcount = *refcount;
2606 oldflags = ifp->if_flags;
2609 * See if we aren't the only and touching refcount is enough.
2610 * Actually toggle interface flag if we are the first or last.
2615 ifp->if_flags |= flag;
2619 ifp->if_flags &= ~flag;
2622 /* Call down the driver since we've changed interface flags */
2623 if (ifp->if_ioctl == NULL) {
2627 ifr.ifr_flags = ifp->if_flags & 0xffff;
2628 ifr.ifr_flagshigh = ifp->if_flags >> 16;
2629 error = (*ifp->if_ioctl)(ifp, SIOCSIFFLAGS, (caddr_t)&ifr);
2632 /* Notify userland that interface flags have changed */
2637 /* Recover after driver error */
2638 *refcount = oldcount;
2639 ifp->if_flags = oldflags;
2644 * Set/clear promiscuous mode on interface ifp based on the truth value
2645 * of pswitch. The calls are reference counted so that only the first
2646 * "on" request actually has an effect, as does the final "off" request.
2647 * Results are undefined if the "off" and "on" requests are not matched.
2650 ifpromisc(struct ifnet *ifp, int pswitch)
2653 int oldflags = ifp->if_flags;
2655 error = if_setflag(ifp, IFF_PROMISC, IFF_PPROMISC,
2656 &ifp->if_pcount, pswitch);
2657 /* If promiscuous mode status has changed, log a message */
2658 if (error == 0 && ((ifp->if_flags ^ oldflags) & IFF_PROMISC))
2659 log(LOG_INFO, "%s: promiscuous mode %s\n",
2661 (ifp->if_flags & IFF_PROMISC) ? "enabled" : "disabled");
2666 * Return interface configuration
2667 * of system. List may be used
2668 * in later ioctl's (above) to get
2669 * other information.
2673 ifconf(u_long cmd, caddr_t data)
2675 struct ifconf *ifc = (struct ifconf *)data;
2680 int error, full = 0, valid_len, max_len;
2682 /* Limit initial buffer size to MAXPHYS to avoid DoS from userspace. */
2683 max_len = MAXPHYS - 1;
2685 /* Prevent hostile input from being able to crash the system */
2686 if (ifc->ifc_len <= 0)
2690 if (ifc->ifc_len <= max_len) {
2691 max_len = ifc->ifc_len;
2694 sb = sbuf_new(NULL, NULL, max_len + 1, SBUF_FIXEDLEN);
2699 TAILQ_FOREACH(ifp, &V_ifnet, if_link) {
2703 * Zero the ifr_name buffer to make sure we don't
2704 * disclose the contents of the stack.
2706 memset(ifr.ifr_name, 0, sizeof(ifr.ifr_name));
2708 if (strlcpy(ifr.ifr_name, ifp->if_xname, sizeof(ifr.ifr_name))
2709 >= sizeof(ifr.ifr_name)) {
2712 return (ENAMETOOLONG);
2717 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
2718 struct sockaddr *sa = ifa->ifa_addr;
2720 if (prison_if(curthread->td_ucred, sa) != 0)
2724 if (cmd == OSIOCGIFCONF) {
2725 struct osockaddr *osa =
2726 (struct osockaddr *)&ifr.ifr_addr;
2728 osa->sa_family = sa->sa_family;
2729 sbuf_bcat(sb, &ifr, sizeof(ifr));
2730 max_len += sizeof(ifr);
2733 if (sa->sa_len <= sizeof(*sa)) {
2735 sbuf_bcat(sb, &ifr, sizeof(ifr));
2736 max_len += sizeof(ifr);
2739 offsetof(struct ifreq, ifr_addr));
2740 max_len += offsetof(struct ifreq, ifr_addr);
2741 sbuf_bcat(sb, sa, sa->sa_len);
2742 max_len += sa->sa_len;
2745 if (sbuf_error(sb) == 0)
2746 valid_len = sbuf_len(sb);
2748 IF_ADDR_UNLOCK(ifp);
2750 bzero((caddr_t)&ifr.ifr_addr, sizeof(ifr.ifr_addr));
2751 sbuf_bcat(sb, &ifr, sizeof(ifr));
2752 max_len += sizeof(ifr);
2754 if (sbuf_error(sb) == 0)
2755 valid_len = sbuf_len(sb);
2761 * If we didn't allocate enough space (uncommon), try again. If
2762 * we have already allocated as much space as we are allowed,
2763 * return what we've got.
2765 if (valid_len != max_len && !full) {
2770 ifc->ifc_len = valid_len;
2772 error = copyout(sbuf_data(sb), ifc->ifc_req, ifc->ifc_len);
2778 * Just like ifpromisc(), but for all-multicast-reception mode.
2781 if_allmulti(struct ifnet *ifp, int onswitch)
2784 return (if_setflag(ifp, IFF_ALLMULTI, 0, &ifp->if_amcount, onswitch));
2787 struct ifmultiaddr *
2788 if_findmulti(struct ifnet *ifp, struct sockaddr *sa)
2790 struct ifmultiaddr *ifma;
2792 IF_ADDR_LOCK_ASSERT(ifp);
2794 TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
2795 if (sa->sa_family == AF_LINK) {
2796 if (sa_dl_equal(ifma->ifma_addr, sa))
2799 if (sa_equal(ifma->ifma_addr, sa))
2808 * Allocate a new ifmultiaddr and initialize based on passed arguments. We
2809 * make copies of passed sockaddrs. The ifmultiaddr will not be added to
2810 * the ifnet multicast address list here, so the caller must do that and
2811 * other setup work (such as notifying the device driver). The reference
2812 * count is initialized to 1.
2814 static struct ifmultiaddr *
2815 if_allocmulti(struct ifnet *ifp, struct sockaddr *sa, struct sockaddr *llsa,
2818 struct ifmultiaddr *ifma;
2819 struct sockaddr *dupsa;
2821 ifma = malloc(sizeof *ifma, M_IFMADDR, mflags |
2826 dupsa = malloc(sa->sa_len, M_IFMADDR, mflags);
2827 if (dupsa == NULL) {
2828 free(ifma, M_IFMADDR);
2831 bcopy(sa, dupsa, sa->sa_len);
2832 ifma->ifma_addr = dupsa;
2834 ifma->ifma_ifp = ifp;
2835 ifma->ifma_refcount = 1;
2836 ifma->ifma_protospec = NULL;
2839 ifma->ifma_lladdr = NULL;
2843 dupsa = malloc(llsa->sa_len, M_IFMADDR, mflags);
2844 if (dupsa == NULL) {
2845 free(ifma->ifma_addr, M_IFMADDR);
2846 free(ifma, M_IFMADDR);
2849 bcopy(llsa, dupsa, llsa->sa_len);
2850 ifma->ifma_lladdr = dupsa;
2856 * if_freemulti: free ifmultiaddr structure and possibly attached related
2857 * addresses. The caller is responsible for implementing reference
2858 * counting, notifying the driver, handling routing messages, and releasing
2859 * any dependent link layer state.
2862 if_freemulti(struct ifmultiaddr *ifma)
2865 KASSERT(ifma->ifma_refcount == 0, ("if_freemulti: refcount %d",
2866 ifma->ifma_refcount));
2867 KASSERT(ifma->ifma_protospec == NULL,
2868 ("if_freemulti: protospec not NULL"));
2870 if (ifma->ifma_lladdr != NULL)
2871 free(ifma->ifma_lladdr, M_IFMADDR);
2872 free(ifma->ifma_addr, M_IFMADDR);
2873 free(ifma, M_IFMADDR);
2877 * Register an additional multicast address with a network interface.
2879 * - If the address is already present, bump the reference count on the
2880 * address and return.
2881 * - If the address is not link-layer, look up a link layer address.
2882 * - Allocate address structures for one or both addresses, and attach to the
2883 * multicast address list on the interface. If automatically adding a link
2884 * layer address, the protocol address will own a reference to the link
2885 * layer address, to be freed when it is freed.
2886 * - Notify the network device driver of an addition to the multicast address
2889 * 'sa' points to caller-owned memory with the desired multicast address.
2891 * 'retifma' will be used to return a pointer to the resulting multicast
2892 * address reference, if desired.
2895 if_addmulti(struct ifnet *ifp, struct sockaddr *sa,
2896 struct ifmultiaddr **retifma)
2898 struct ifmultiaddr *ifma, *ll_ifma;
2899 struct sockaddr *llsa;
2903 * If the address is already present, return a new reference to it;
2904 * otherwise, allocate storage and set up a new address.
2907 ifma = if_findmulti(ifp, sa);
2909 ifma->ifma_refcount++;
2910 if (retifma != NULL)
2912 IF_ADDR_UNLOCK(ifp);
2917 * The address isn't already present; resolve the protocol address
2918 * into a link layer address, and then look that up, bump its
2919 * refcount or allocate an ifma for that also. If 'llsa' was
2920 * returned, we will need to free it later.
2924 if (ifp->if_resolvemulti != NULL) {
2925 error = ifp->if_resolvemulti(ifp, &llsa, sa);
2931 * Allocate the new address. Don't hook it up yet, as we may also
2932 * need to allocate a link layer multicast address.
2934 ifma = if_allocmulti(ifp, sa, llsa, M_NOWAIT);
2941 * If a link layer address is found, we'll need to see if it's
2942 * already present in the address list, or allocate is as well.
2943 * When this block finishes, the link layer address will be on the
2947 ll_ifma = if_findmulti(ifp, llsa);
2948 if (ll_ifma == NULL) {
2949 ll_ifma = if_allocmulti(ifp, llsa, NULL, M_NOWAIT);
2950 if (ll_ifma == NULL) {
2951 --ifma->ifma_refcount;
2956 TAILQ_INSERT_HEAD(&ifp->if_multiaddrs, ll_ifma,
2959 ll_ifma->ifma_refcount++;
2960 ifma->ifma_llifma = ll_ifma;
2964 * We now have a new multicast address, ifma, and possibly a new or
2965 * referenced link layer address. Add the primary address to the
2966 * ifnet address list.
2968 TAILQ_INSERT_HEAD(&ifp->if_multiaddrs, ifma, ifma_link);
2970 if (retifma != NULL)
2974 * Must generate the message while holding the lock so that 'ifma'
2975 * pointer is still valid.
2977 rt_newmaddrmsg(RTM_NEWMADDR, ifma);
2978 IF_ADDR_UNLOCK(ifp);
2981 * We are certain we have added something, so call down to the
2982 * interface to let them know about it.
2984 if (ifp->if_ioctl != NULL) {
2985 (void) (*ifp->if_ioctl)(ifp, SIOCADDMULTI, 0);
2989 free(llsa, M_IFMADDR);
2995 free(llsa, M_IFMADDR);
2998 IF_ADDR_UNLOCK(ifp);
3003 * Delete a multicast group membership by network-layer group address.
3005 * Returns ENOENT if the entry could not be found. If ifp no longer
3006 * exists, results are undefined. This entry point should only be used
3007 * from subsystems which do appropriate locking to hold ifp for the
3008 * duration of the call.
3009 * Network-layer protocol domains must use if_delmulti_ifma().
3012 if_delmulti(struct ifnet *ifp, struct sockaddr *sa)
3014 struct ifmultiaddr *ifma;
3019 IFNET_RLOCK_NOSLEEP();
3020 TAILQ_FOREACH(oifp, &V_ifnet, if_link)
3025 IFNET_RUNLOCK_NOSLEEP();
3027 KASSERT(ifp != NULL, ("%s: ifnet went away", __func__));
3034 ifma = if_findmulti(ifp, sa);
3036 lastref = if_delmulti_locked(ifp, ifma, 0);
3037 IF_ADDR_UNLOCK(ifp);
3042 if (lastref && ifp->if_ioctl != NULL) {
3043 (void)(*ifp->if_ioctl)(ifp, SIOCDELMULTI, 0);
3050 * Delete all multicast group membership for an interface.
3051 * Should be used to quickly flush all multicast filters.
3054 if_delallmulti(struct ifnet *ifp)
3056 struct ifmultiaddr *ifma;
3057 struct ifmultiaddr *next;
3060 TAILQ_FOREACH_SAFE(ifma, &ifp->if_multiaddrs, ifma_link, next)
3061 if_delmulti_locked(ifp, ifma, 0);
3062 IF_ADDR_UNLOCK(ifp);
3066 * Delete a multicast group membership by group membership pointer.
3067 * Network-layer protocol domains must use this routine.
3069 * It is safe to call this routine if the ifp disappeared.
3072 if_delmulti_ifma(struct ifmultiaddr *ifma)
3077 ifp = ifma->ifma_ifp;
3080 printf("%s: ifma_ifp seems to be detached\n", __func__);
3084 IFNET_RLOCK_NOSLEEP();
3085 TAILQ_FOREACH(oifp, &V_ifnet, if_link)
3089 printf("%s: ifnet %p disappeared\n", __func__, ifp);
3092 IFNET_RUNLOCK_NOSLEEP();
3096 * If and only if the ifnet instance exists: Acquire the address lock.
3101 lastref = if_delmulti_locked(ifp, ifma, 0);
3105 * If and only if the ifnet instance exists:
3106 * Release the address lock.
3107 * If the group was left: update the hardware hash filter.
3109 IF_ADDR_UNLOCK(ifp);
3110 if (lastref && ifp->if_ioctl != NULL) {
3111 (void)(*ifp->if_ioctl)(ifp, SIOCDELMULTI, 0);
3117 * Perform deletion of network-layer and/or link-layer multicast address.
3119 * Return 0 if the reference count was decremented.
3120 * Return 1 if the final reference was released, indicating that the
3121 * hardware hash filter should be reprogrammed.
3124 if_delmulti_locked(struct ifnet *ifp, struct ifmultiaddr *ifma, int detaching)
3126 struct ifmultiaddr *ll_ifma;
3128 if (ifp != NULL && ifma->ifma_ifp != NULL) {
3129 KASSERT(ifma->ifma_ifp == ifp,
3130 ("%s: inconsistent ifp %p", __func__, ifp));
3131 IF_ADDR_LOCK_ASSERT(ifp);
3134 ifp = ifma->ifma_ifp;
3137 * If the ifnet is detaching, null out references to ifnet,
3138 * so that upper protocol layers will notice, and not attempt
3139 * to obtain locks for an ifnet which no longer exists. The
3140 * routing socket announcement must happen before the ifnet
3141 * instance is detached from the system.
3145 printf("%s: detaching ifnet instance %p\n", __func__, ifp);
3148 * ifp may already be nulled out if we are being reentered
3149 * to delete the ll_ifma.
3152 rt_newmaddrmsg(RTM_DELMADDR, ifma);
3153 ifma->ifma_ifp = NULL;
3157 if (--ifma->ifma_refcount > 0)
3161 * If this ifma is a network-layer ifma, a link-layer ifma may
3162 * have been associated with it. Release it first if so.
3164 ll_ifma = ifma->ifma_llifma;
3165 if (ll_ifma != NULL) {
3166 KASSERT(ifma->ifma_lladdr != NULL,
3167 ("%s: llifma w/o lladdr", __func__));
3169 ll_ifma->ifma_ifp = NULL; /* XXX */
3170 if (--ll_ifma->ifma_refcount == 0) {
3172 TAILQ_REMOVE(&ifp->if_multiaddrs, ll_ifma,
3175 if_freemulti(ll_ifma);
3180 TAILQ_REMOVE(&ifp->if_multiaddrs, ifma, ifma_link);
3185 * The last reference to this instance of struct ifmultiaddr
3186 * was released; the hardware should be notified of this change.
3192 * Set the link layer address on an interface.
3194 * At this time we only support certain types of interfaces,
3195 * and we don't allow the length of the address to change.
3198 if_setlladdr(struct ifnet *ifp, const u_char *lladdr, int len)
3200 struct sockaddr_dl *sdl;
3207 IF_ADDR_UNLOCK(ifp);
3211 IF_ADDR_UNLOCK(ifp);
3212 sdl = (struct sockaddr_dl *)ifa->ifa_addr;
3217 if (len != sdl->sdl_alen) { /* don't allow length to change */
3221 switch (ifp->if_type) {
3229 case IFT_IEEE8023ADLAG:
3231 bcopy(lladdr, LLADDR(sdl), len);
3240 * If the interface is already up, we need
3241 * to re-init it in order to reprogram its
3244 if ((ifp->if_flags & IFF_UP) != 0) {
3245 if (ifp->if_ioctl) {
3246 ifp->if_flags &= ~IFF_UP;
3247 ifr.ifr_flags = ifp->if_flags & 0xffff;
3248 ifr.ifr_flagshigh = ifp->if_flags >> 16;
3249 (*ifp->if_ioctl)(ifp, SIOCSIFFLAGS, (caddr_t)&ifr);
3250 ifp->if_flags |= IFF_UP;
3251 ifr.ifr_flags = ifp->if_flags & 0xffff;
3252 ifr.ifr_flagshigh = ifp->if_flags >> 16;
3253 (*ifp->if_ioctl)(ifp, SIOCSIFFLAGS, (caddr_t)&ifr);
3257 * Also send gratuitous ARPs to notify other nodes about
3258 * the address change.
3260 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
3261 if (ifa->ifa_addr->sa_family == AF_INET)
3262 arp_ifinit(ifp, ifa);
3270 * The name argument must be a pointer to storage which will last as
3271 * long as the interface does. For physical devices, the result of
3272 * device_get_name(dev) is a good choice and for pseudo-devices a
3273 * static string works well.
3276 if_initname(struct ifnet *ifp, const char *name, int unit)
3278 ifp->if_dname = name;
3279 ifp->if_dunit = unit;
3280 if (unit != IF_DUNIT_NONE)
3281 snprintf(ifp->if_xname, IFNAMSIZ, "%s%d", name, unit);
3283 strlcpy(ifp->if_xname, name, IFNAMSIZ);
3287 if_printf(struct ifnet *ifp, const char * fmt, ...)
3292 retval = printf("%s: ", ifp->if_xname);
3294 retval += vprintf(fmt, ap);
3300 if_start(struct ifnet *ifp)
3303 (*(ifp)->if_start)(ifp);
3307 * Backwards compatibility interface for drivers
3308 * that have not implemented it
3311 if_transmit(struct ifnet *ifp, struct mbuf *m)
3315 IFQ_HANDOFF(ifp, m, error);
3320 if_handoff(struct ifqueue *ifq, struct mbuf *m, struct ifnet *ifp, int adjust)
3325 if (_IF_QFULL(ifq)) {
3332 ifp->if_obytes += m->m_pkthdr.len + adjust;
3333 if (m->m_flags & (M_BCAST|M_MCAST))
3335 active = ifp->if_drv_flags & IFF_DRV_OACTIVE;
3337 _IF_ENQUEUE(ifq, m);
3339 if (ifp != NULL && !active)
3340 (*(ifp)->if_start)(ifp);
3345 if_register_com_alloc(u_char type,
3346 if_com_alloc_t *a, if_com_free_t *f)
3349 KASSERT(if_com_alloc[type] == NULL,
3350 ("if_register_com_alloc: %d already registered", type));
3351 KASSERT(if_com_free[type] == NULL,
3352 ("if_register_com_alloc: %d free already registered", type));
3354 if_com_alloc[type] = a;
3355 if_com_free[type] = f;
3359 if_deregister_com_alloc(u_char type)
3362 KASSERT(if_com_alloc[type] != NULL,
3363 ("if_deregister_com_alloc: %d not registered", type));
3364 KASSERT(if_com_free[type] != NULL,
3365 ("if_deregister_com_alloc: %d free not registered", type));
3366 if_com_alloc[type] = NULL;
3367 if_com_free[type] = NULL;
3372 if_show_ifnet(struct ifnet *ifp)
3377 db_printf("%s:\n", ifp->if_xname);
3378 #define IF_DB_PRINTF(f, e) db_printf(" %s = " f "\n", #e, ifp->e);
3379 IF_DB_PRINTF("%s", if_dname);
3380 IF_DB_PRINTF("%d", if_dunit);
3381 IF_DB_PRINTF("%s", if_description);
3382 IF_DB_PRINTF("%u", if_index);
3383 IF_DB_PRINTF("%u", if_refcount);
3384 IF_DB_PRINTF("%d", if_index_reserved);
3385 IF_DB_PRINTF("%p", if_softc);
3386 IF_DB_PRINTF("%p", if_l2com);
3387 IF_DB_PRINTF("%p", if_vnet);
3388 IF_DB_PRINTF("%p", if_home_vnet);
3389 IF_DB_PRINTF("%p", if_addr);
3390 IF_DB_PRINTF("%p", if_llsoftc);
3391 IF_DB_PRINTF("%p", if_label);
3392 IF_DB_PRINTF("%u", if_pcount);
3393 IF_DB_PRINTF("0x%08x", if_flags);
3394 IF_DB_PRINTF("0x%08x", if_drv_flags);
3395 IF_DB_PRINTF("0x%08x", if_capabilities);
3396 IF_DB_PRINTF("0x%08x", if_capenable);
3397 IF_DB_PRINTF("%p", if_snd.ifq_head);
3398 IF_DB_PRINTF("%p", if_snd.ifq_tail);
3399 IF_DB_PRINTF("%d", if_snd.ifq_len);
3400 IF_DB_PRINTF("%d", if_snd.ifq_maxlen);
3401 IF_DB_PRINTF("%d", if_snd.ifq_drops);
3402 IF_DB_PRINTF("%p", if_snd.ifq_drv_head);
3403 IF_DB_PRINTF("%p", if_snd.ifq_drv_tail);
3404 IF_DB_PRINTF("%d", if_snd.ifq_drv_len);
3405 IF_DB_PRINTF("%d", if_snd.ifq_drv_maxlen);
3406 IF_DB_PRINTF("%d", if_snd.altq_type);
3407 IF_DB_PRINTF("%x", if_snd.altq_flags);
3411 DB_SHOW_COMMAND(ifnet, db_show_ifnet)
3415 db_printf("usage: show ifnet <struct ifnet *>\n");
3419 if_show_ifnet((struct ifnet *)addr);
3422 DB_SHOW_ALL_COMMAND(ifnets, db_show_all_ifnets)
3424 VNET_ITERATOR_DECL(vnet_iter);
3428 VNET_FOREACH(vnet_iter) {
3429 CURVNET_SET_QUIET(vnet_iter);
3431 db_printf("vnet=%p\n", curvnet);
3433 for (idx = 1; idx <= V_if_index; idx++) {
3434 ifp = V_ifindex_table[idx].ife_ifnet;
3437 db_printf( "%20s ifp=%p\n", ifp->if_xname, ifp);