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>
61 #include <machine/stdarg.h>
65 #include <net/if_arp.h>
66 #include <net/if_clone.h>
67 #include <net/if_dl.h>
68 #include <net/if_types.h>
69 #include <net/if_var.h>
70 #include <net/radix.h>
71 #include <net/route.h>
74 #if defined(INET) || defined(INET6)
76 #include <netinet/in.h>
77 #include <netinet/in_var.h>
78 #include <netinet/ip_carp.h>
80 #include <netinet6/in6_var.h>
81 #include <netinet6/in6_ifattach.h>
85 #include <netinet/if_ether.h>
88 #include <security/mac/mac_framework.h>
90 #ifdef COMPAT_FREEBSD32
91 #include <sys/mount.h>
92 #include <compat/freebsd32/freebsd32.h>
95 struct ifindex_entry {
96 struct ifnet *ife_ifnet;
99 SYSCTL_NODE(_net, PF_LINK, link, CTLFLAG_RW, 0, "Link layers");
100 SYSCTL_NODE(_net_link, 0, generic, CTLFLAG_RW, 0, "Generic link-management");
102 TUNABLE_INT("net.link.ifqmaxlen", &ifqmaxlen);
103 SYSCTL_INT(_net_link, OID_AUTO, ifqmaxlen, CTLFLAG_RDTUN,
104 &ifqmaxlen, 0, "max send queue size");
106 /* Log link state change events */
107 static int log_link_state_change = 1;
109 SYSCTL_INT(_net_link, OID_AUTO, log_link_state_change, CTLFLAG_RW,
110 &log_link_state_change, 0,
111 "log interface link state change events");
113 /* Interface description */
114 static unsigned int ifdescr_maxlen = 1024;
115 SYSCTL_UINT(_net, OID_AUTO, ifdescr_maxlen, CTLFLAG_RW,
117 "administrative maximum length for interface description");
119 MALLOC_DEFINE(M_IFDESCR, "ifdescr", "ifnet descriptions");
121 /* global sx for non-critical path ifdescr */
122 static struct sx ifdescr_sx;
123 SX_SYSINIT(ifdescr_sx, &ifdescr_sx, "ifnet descr");
125 void (*bstp_linkstate_p)(struct ifnet *ifp, int state);
126 void (*ng_ether_link_state_p)(struct ifnet *ifp, int state);
127 void (*lagg_linkstate_p)(struct ifnet *ifp, int state);
128 /* These are external hooks for CARP. */
129 void (*carp_linkstate_p)(struct ifnet *ifp);
130 #if defined(INET) || defined(INET6)
131 struct ifnet *(*carp_forus_p)(struct ifnet *ifp, u_char *dhost);
132 int (*carp_output_p)(struct ifnet *ifp, struct mbuf *m,
133 struct sockaddr *sa, struct rtentry *rt);
136 int (*carp_iamatch_p)(struct ifnet *, struct in_ifaddr *, struct in_addr *,
140 struct ifaddr *(*carp_iamatch6_p)(struct ifnet *ifp, struct in6_addr *taddr6);
141 caddr_t (*carp_macmatch6_p)(struct ifnet *ifp, struct mbuf *m,
142 const struct in6_addr *taddr);
145 struct mbuf *(*tbr_dequeue_ptr)(struct ifaltq *, int) = NULL;
148 * XXX: Style; these should be sorted alphabetically, and unprototyped
149 * static functions should be prototyped. Currently they are sorted by
152 static void if_attachdomain(void *);
153 static void if_attachdomain1(struct ifnet *);
154 static int ifconf(u_long, caddr_t);
155 static void if_freemulti(struct ifmultiaddr *);
156 static void if_init(void *);
157 static void if_grow(void);
158 static void if_route(struct ifnet *, int flag, int fam);
159 static int if_setflag(struct ifnet *, int, int, int *, int);
160 static int if_transmit(struct ifnet *ifp, struct mbuf *m);
161 static void if_unroute(struct ifnet *, int flag, int fam);
162 static void link_rtrequest(int, struct rtentry *, struct rt_addrinfo *);
163 static int if_rtdel(struct radix_node *, void *);
164 static int ifhwioctl(u_long, struct ifnet *, caddr_t, struct thread *);
165 static int if_delmulti_locked(struct ifnet *, struct ifmultiaddr *, int);
166 static void do_link_state_change(void *, int);
167 static int if_getgroup(struct ifgroupreq *, struct ifnet *);
168 static int if_getgroupmembers(struct ifgroupreq *);
169 static void if_delgroups(struct ifnet *);
170 static void if_attach_internal(struct ifnet *, int);
171 static void if_detach_internal(struct ifnet *, int);
175 * XXX: declare here to avoid to include many inet6 related files..
176 * should be more generalized?
178 extern void nd6_setmtu(struct ifnet *);
181 VNET_DEFINE(int, if_index);
182 int ifqmaxlen = IFQ_MAXLEN;
183 VNET_DEFINE(struct ifnethead, ifnet); /* depend on static init XXX */
184 VNET_DEFINE(struct ifgrouphead, ifg_head);
186 static VNET_DEFINE(int, if_indexlim) = 8;
188 /* Table of ifnet by index. */
189 VNET_DEFINE(struct ifindex_entry *, ifindex_table);
191 #define V_if_indexlim VNET(if_indexlim)
192 #define V_ifindex_table VNET(ifindex_table)
195 * The global network interface list (V_ifnet) and related state (such as
196 * if_index, if_indexlim, and ifindex_table) are protected by an sxlock and
197 * an rwlock. Either may be acquired shared to stablize the list, but both
198 * must be acquired writable to modify the list. This model allows us to
199 * both stablize the interface list during interrupt thread processing, but
200 * also to stablize it over long-running ioctls, without introducing priority
201 * inversions and deadlocks.
203 struct rwlock ifnet_rwlock;
204 struct sx ifnet_sxlock;
207 * The allocation of network interfaces is a rather non-atomic affair; we
208 * need to select an index before we are ready to expose the interface for
209 * use, so will use this pointer value to indicate reservation.
211 #define IFNET_HOLD (void *)(uintptr_t)(-1)
213 static if_com_alloc_t *if_com_alloc[256];
214 static if_com_free_t *if_com_free[256];
216 MALLOC_DEFINE(M_IFNET, "ifnet", "interface internals");
217 MALLOC_DEFINE(M_IFADDR, "ifaddr", "interface address");
218 MALLOC_DEFINE(M_IFMADDR, "ether_multi", "link-level multicast address");
221 ifnet_byindex_locked(u_short idx)
224 if (idx > V_if_index)
226 if (V_ifindex_table[idx].ife_ifnet == IFNET_HOLD)
228 return (V_ifindex_table[idx].ife_ifnet);
232 ifnet_byindex(u_short idx)
236 IFNET_RLOCK_NOSLEEP();
237 ifp = ifnet_byindex_locked(idx);
238 IFNET_RUNLOCK_NOSLEEP();
243 ifnet_byindex_ref(u_short idx)
247 IFNET_RLOCK_NOSLEEP();
248 ifp = ifnet_byindex_locked(idx);
249 if (ifp == NULL || (ifp->if_flags & IFF_DYING)) {
250 IFNET_RUNLOCK_NOSLEEP();
254 IFNET_RUNLOCK_NOSLEEP();
259 * Allocate an ifindex array entry; return 0 on success or an error on
263 ifindex_alloc_locked(u_short *idxp)
267 IFNET_WLOCK_ASSERT();
271 * Try to find an empty slot below V_if_index. If we fail, take the
274 for (idx = 1; idx <= V_if_index; idx++) {
275 if (V_ifindex_table[idx].ife_ifnet == NULL)
279 /* Catch if_index overflow. */
282 if (idx >= V_if_indexlim) {
286 if (idx > V_if_index)
293 ifindex_free_locked(u_short idx)
296 IFNET_WLOCK_ASSERT();
298 V_ifindex_table[idx].ife_ifnet = NULL;
299 while (V_if_index > 0 &&
300 V_ifindex_table[V_if_index].ife_ifnet == NULL)
305 ifindex_free(u_short idx)
309 ifindex_free_locked(idx);
314 ifnet_setbyindex_locked(u_short idx, struct ifnet *ifp)
317 IFNET_WLOCK_ASSERT();
319 V_ifindex_table[idx].ife_ifnet = ifp;
323 ifnet_setbyindex(u_short idx, struct ifnet *ifp)
327 ifnet_setbyindex_locked(idx, ifp);
332 ifaddr_byindex(u_short idx)
336 IFNET_RLOCK_NOSLEEP();
337 ifa = ifnet_byindex_locked(idx)->if_addr;
340 IFNET_RUNLOCK_NOSLEEP();
345 * Network interface utility routines.
347 * Routines with ifa_ifwith* names take sockaddr *'s as
352 vnet_if_init(const void *unused __unused)
355 TAILQ_INIT(&V_ifnet);
356 TAILQ_INIT(&V_ifg_head);
358 if_grow(); /* create initial table */
360 vnet_if_clone_init();
362 VNET_SYSINIT(vnet_if_init, SI_SUB_INIT_IF, SI_ORDER_SECOND, vnet_if_init,
367 if_init(void *dummy __unused)
373 SYSINIT(interfaces, SI_SUB_INIT_IF, SI_ORDER_FIRST, 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);
395 struct ifindex_entry *e;
397 IFNET_WLOCK_ASSERT();
398 oldlim = V_if_indexlim;
400 n = (oldlim << 1) * sizeof(*e);
401 e = malloc(n, M_IFNET, M_WAITOK | M_ZERO);
403 if (V_if_indexlim != oldlim) {
407 if (V_ifindex_table != NULL) {
408 memcpy((caddr_t)e, (caddr_t)V_ifindex_table, n/2);
409 free((caddr_t)V_ifindex_table, M_IFNET);
416 * Allocate a struct ifnet and an index for an interface. A layer 2
417 * common structure will also be allocated if an allocation routine is
418 * registered for the passed type.
421 if_alloc(u_char type)
426 ifp = malloc(sizeof(struct ifnet), M_IFNET, M_WAITOK|M_ZERO);
428 if (ifindex_alloc_locked(&idx) != 0) {
433 ifnet_setbyindex_locked(idx, IFNET_HOLD);
437 ifp->if_alloctype = type;
438 if (if_com_alloc[type] != NULL) {
439 ifp->if_l2com = if_com_alloc[type](type, ifp);
440 if (ifp->if_l2com == NULL) {
447 IF_ADDR_LOCK_INIT(ifp);
448 TASK_INIT(&ifp->if_linktask, 0, do_link_state_change, ifp);
449 ifp->if_afdata_initialized = 0;
450 IF_AFDATA_LOCK_INIT(ifp);
451 TAILQ_INIT(&ifp->if_addrhead);
452 TAILQ_INIT(&ifp->if_prefixhead);
453 TAILQ_INIT(&ifp->if_multiaddrs);
454 TAILQ_INIT(&ifp->if_groups);
458 ifq_init(&ifp->if_snd, ifp);
460 refcount_init(&ifp->if_refcount, 1); /* Index reference. */
461 ifnet_setbyindex(ifp->if_index, ifp);
466 * Do the actual work of freeing a struct ifnet, associated index, and layer
467 * 2 common structure. This call is made when the last reference to an
468 * interface is released.
471 if_free_internal(struct ifnet *ifp)
474 KASSERT((ifp->if_flags & IFF_DYING),
475 ("if_free_internal: interface not dying"));
478 KASSERT(ifp == ifnet_byindex_locked(ifp->if_index),
479 ("%s: freeing unallocated ifnet", ifp->if_xname));
481 ifindex_free_locked(ifp->if_index);
484 if (if_com_free[ifp->if_alloctype] != NULL)
485 if_com_free[ifp->if_alloctype](ifp->if_l2com,
489 mac_ifnet_destroy(ifp);
491 if (ifp->if_description != NULL)
492 free(ifp->if_description, M_IFDESCR);
493 IF_AFDATA_DESTROY(ifp);
494 IF_ADDR_LOCK_DESTROY(ifp);
495 ifq_delete(&ifp->if_snd);
500 * This version should only be called by intefaces that switch their type
501 * after calling if_alloc(). if_free_type() will go away again now that we
502 * have if_alloctype to cache the original allocation type. For now, assert
503 * that they match, since we require that in practice.
506 if_free_type(struct ifnet *ifp, u_char type)
509 KASSERT(ifp->if_alloctype == type,
510 ("if_free_type: type (%d) != alloctype (%d)", type,
513 ifp->if_flags |= IFF_DYING; /* XXX: Locking */
514 if (!refcount_release(&ifp->if_refcount))
516 if_free_internal(ifp);
520 * This is the normal version of if_free(), used by device drivers to free a
521 * detached network interface. The contents of if_free_type() will move into
522 * here when if_free_type() goes away.
525 if_free(struct ifnet *ifp)
528 if_free_type(ifp, ifp->if_alloctype);
532 * Interfaces to keep an ifnet type-stable despite the possibility of the
533 * driver calling if_free(). If there are additional references, we defer
534 * freeing the underlying data structure.
537 if_ref(struct ifnet *ifp)
540 /* We don't assert the ifnet list lock here, but arguably should. */
541 refcount_acquire(&ifp->if_refcount);
545 if_rele(struct ifnet *ifp)
548 if (!refcount_release(&ifp->if_refcount))
550 if_free_internal(ifp);
554 ifq_init(struct ifaltq *ifq, struct ifnet *ifp)
557 mtx_init(&ifq->ifq_mtx, ifp->if_xname, "if send queue", MTX_DEF);
559 if (ifq->ifq_maxlen == 0)
560 ifq->ifq_maxlen = ifqmaxlen;
563 ifq->altq_disc = NULL;
564 ifq->altq_flags &= ALTQF_CANTCHANGE;
565 ifq->altq_tbr = NULL;
570 ifq_delete(struct ifaltq *ifq)
572 mtx_destroy(&ifq->ifq_mtx);
576 * Perform generic interface initalization tasks and attach the interface
577 * to the list of "active" interfaces. If vmove flag is set on entry
578 * to if_attach_internal(), perform only a limited subset of initialization
579 * tasks, given that we are moving from one vnet to another an ifnet which
580 * has already been fully initialized.
583 * - The decision to return void and thus require this function to
584 * succeed is questionable.
585 * - We should probably do more sanity checking. For instance we don't
586 * do anything to insure if_xname is unique or non-empty.
589 if_attach(struct ifnet *ifp)
592 if_attach_internal(ifp, 0);
596 if_attach_internal(struct ifnet *ifp, int vmove)
598 unsigned socksize, ifasize;
599 int namelen, masklen;
600 struct sockaddr_dl *sdl;
603 if (ifp->if_index == 0 || ifp != ifnet_byindex(ifp->if_index))
604 panic ("%s: BUG: if_attach called without if_alloc'd input()\n",
608 ifp->if_vnet = curvnet;
609 if (ifp->if_home_vnet == NULL)
610 ifp->if_home_vnet = curvnet;
613 if_addgroup(ifp, IFG_ALL);
615 getmicrotime(&ifp->if_lastchange);
616 ifp->if_data.ifi_epoch = time_uptime;
617 ifp->if_data.ifi_datalen = sizeof(struct if_data);
619 KASSERT((ifp->if_transmit == NULL && ifp->if_qflush == NULL) ||
620 (ifp->if_transmit != NULL && ifp->if_qflush != NULL),
621 ("transmit and qflush must both either be set or both be NULL"));
622 if (ifp->if_transmit == NULL) {
623 ifp->if_transmit = if_transmit;
624 ifp->if_qflush = if_qflush;
629 mac_ifnet_create(ifp);
633 * Create a Link Level name for this device.
635 namelen = strlen(ifp->if_xname);
637 * Always save enough space for any possiable name so we
638 * can do a rename in place later.
640 masklen = offsetof(struct sockaddr_dl, sdl_data[0]) + IFNAMSIZ;
641 socksize = masklen + ifp->if_addrlen;
642 if (socksize < sizeof(*sdl))
643 socksize = sizeof(*sdl);
644 socksize = roundup2(socksize, sizeof(long));
645 ifasize = sizeof(*ifa) + 2 * socksize;
646 ifa = malloc(ifasize, M_IFADDR, M_WAITOK | M_ZERO);
648 sdl = (struct sockaddr_dl *)(ifa + 1);
649 sdl->sdl_len = socksize;
650 sdl->sdl_family = AF_LINK;
651 bcopy(ifp->if_xname, sdl->sdl_data, namelen);
652 sdl->sdl_nlen = namelen;
653 sdl->sdl_index = ifp->if_index;
654 sdl->sdl_type = ifp->if_type;
657 ifa->ifa_rtrequest = link_rtrequest;
658 ifa->ifa_addr = (struct sockaddr *)sdl;
659 sdl = (struct sockaddr_dl *)(socksize + (caddr_t)sdl);
660 ifa->ifa_netmask = (struct sockaddr *)sdl;
661 sdl->sdl_len = masklen;
663 sdl->sdl_data[--namelen] = 0xff;
664 TAILQ_INSERT_HEAD(&ifp->if_addrhead, ifa, ifa_link);
665 /* Reliably crash if used uninitialized. */
666 ifp->if_broadcastaddr = NULL;
671 * Update the interface index in the link layer address
674 for (ifa = ifp->if_addr; ifa != NULL;
675 ifa = TAILQ_NEXT(ifa, ifa_link)) {
676 if (ifa->ifa_addr->sa_family == AF_LINK) {
677 sdl = (struct sockaddr_dl *)ifa->ifa_addr;
678 sdl->sdl_index = ifp->if_index;
685 TAILQ_INSERT_TAIL(&V_ifnet, ifp, if_link);
687 curvnet->vnet_ifcnt++;
691 if (domain_init_status >= 2)
692 if_attachdomain1(ifp);
694 EVENTHANDLER_INVOKE(ifnet_arrival_event, ifp);
695 if (IS_DEFAULT_VNET(curvnet))
696 devctl_notify("IFNET", ifp->if_xname, "ATTACH", NULL);
698 /* Announce the interface. */
699 rt_ifannouncemsg(ifp, IFAN_ARRIVAL);
703 if_attachdomain(void *dummy)
709 TAILQ_FOREACH(ifp, &V_ifnet, if_link)
710 if_attachdomain1(ifp);
713 SYSINIT(domainifattach, SI_SUB_PROTO_IFATTACHDOMAIN, SI_ORDER_SECOND,
714 if_attachdomain, NULL);
717 if_attachdomain1(struct ifnet *ifp)
725 * Since dp->dom_ifattach calls malloc() with M_WAITOK, we
726 * cannot lock ifp->if_afdata initialization, entirely.
728 if (IF_AFDATA_TRYLOCK(ifp) == 0) {
732 if (ifp->if_afdata_initialized >= domain_init_status) {
733 IF_AFDATA_UNLOCK(ifp);
735 printf("if_attachdomain called more than once on %s\n",
739 ifp->if_afdata_initialized = domain_init_status;
740 IF_AFDATA_UNLOCK(ifp);
742 /* address family dependent data region */
743 bzero(ifp->if_afdata, sizeof(ifp->if_afdata));
744 for (dp = domains; dp; dp = dp->dom_next) {
745 if (dp->dom_ifattach)
746 ifp->if_afdata[dp->dom_family] =
747 (*dp->dom_ifattach)(ifp);
754 * Remove any unicast or broadcast network addresses from an interface.
757 if_purgeaddrs(struct ifnet *ifp)
759 struct ifaddr *ifa, *next;
761 TAILQ_FOREACH_SAFE(ifa, &ifp->if_addrhead, ifa_link, next) {
762 if (ifa->ifa_addr->sa_family == AF_LINK)
765 /* XXX: Ugly!! ad hoc just for INET */
766 if (ifa->ifa_addr->sa_family == AF_INET) {
767 struct ifaliasreq ifr;
769 bzero(&ifr, sizeof(ifr));
770 ifr.ifra_addr = *ifa->ifa_addr;
771 if (ifa->ifa_dstaddr)
772 ifr.ifra_broadaddr = *ifa->ifa_dstaddr;
773 if (in_control(NULL, SIOCDIFADDR, (caddr_t)&ifr, ifp,
779 if (ifa->ifa_addr->sa_family == AF_INET6) {
781 /* ifp_addrhead is already updated */
785 TAILQ_REMOVE(&ifp->if_addrhead, ifa, ifa_link);
791 * Remove any multicast network addresses from an interface when an ifnet
795 if_purgemaddrs(struct ifnet *ifp)
797 struct ifmultiaddr *ifma;
798 struct ifmultiaddr *next;
801 TAILQ_FOREACH_SAFE(ifma, &ifp->if_multiaddrs, ifma_link, next)
802 if_delmulti_locked(ifp, ifma, 1);
807 * Detach an interface, removing it from the list of "active" interfaces.
808 * If vmove flag is set on entry to if_detach_internal(), perform only a
809 * limited subset of cleanup tasks, given that we are moving an ifnet from
810 * one vnet to another, where it must be fully operational.
812 * XXXRW: There are some significant questions about event ordering, and
813 * how to prevent things from starting to use the interface during detach.
816 if_detach(struct ifnet *ifp)
819 if_detach_internal(ifp, 0);
823 if_detach_internal(struct ifnet *ifp, int vmove)
826 struct radix_node_head *rnh;
833 TAILQ_FOREACH(iter, &V_ifnet, if_link)
835 TAILQ_REMOVE(&V_ifnet, ifp, if_link);
841 curvnet->vnet_ifcnt--;
846 panic("%s: ifp=%p not on the ifnet tailq %p",
847 __func__, ifp, &V_ifnet);
849 return; /* XXX this should panic as well? */
853 * Remove/wait for pending events.
855 taskqueue_drain(taskqueue_swi, &ifp->if_linktask);
858 * Remove routes and flush queues.
862 if (ALTQ_IS_ENABLED(&ifp->if_snd))
863 altq_disable(&ifp->if_snd);
864 if (ALTQ_IS_ATTACHED(&ifp->if_snd))
865 altq_detach(&ifp->if_snd);
876 * Remove all IPv6 kernel structs related to ifp. This should be done
877 * before removing routing entries below, since IPv6 interface direct
878 * routes are expected to be removed by the IPv6-specific kernel API.
879 * Otherwise, the kernel will detect some inconsistency and bark it.
887 * Prevent further calls into the device driver via ifnet.
892 * Remove link ifaddr pointer and maybe decrement if_index.
893 * Clean up all addresses.
897 /* We can now free link ifaddr. */
898 if (!TAILQ_EMPTY(&ifp->if_addrhead)) {
899 ifa = TAILQ_FIRST(&ifp->if_addrhead);
900 TAILQ_REMOVE(&ifp->if_addrhead, ifa, ifa_link);
906 * Delete all remaining routes using this interface
907 * Unfortuneatly the only way to do this is to slog through
908 * the entire routing table looking for routes which point
909 * to this interface...oh well...
911 for (i = 1; i <= AF_MAX; i++) {
912 for (j = 0; j < rt_numfibs; j++) {
913 rnh = rt_tables_get_rnh(j, i);
916 RADIX_NODE_HEAD_LOCK(rnh);
917 (void) rnh->rnh_walktree(rnh, if_rtdel, ifp);
918 RADIX_NODE_HEAD_UNLOCK(rnh);
922 /* Announce that the interface is gone. */
923 rt_ifannouncemsg(ifp, IFAN_DEPARTURE);
924 EVENTHANDLER_INVOKE(ifnet_departure_event, ifp);
925 if (IS_DEFAULT_VNET(curvnet))
926 devctl_notify("IFNET", ifp->if_xname, "DETACH", NULL);
930 * We cannot hold the lock over dom_ifdetach calls as they might
931 * sleep, for example trying to drain a callout, thus open up the
932 * theoretical race with re-attaching.
935 i = ifp->if_afdata_initialized;
936 ifp->if_afdata_initialized = 0;
937 IF_AFDATA_UNLOCK(ifp);
938 for (dp = domains; i > 0 && dp; dp = dp->dom_next) {
939 if (dp->dom_ifdetach && ifp->if_afdata[dp->dom_family])
940 (*dp->dom_ifdetach)(ifp,
941 ifp->if_afdata[dp->dom_family]);
947 * if_vmove() performs a limited version of if_detach() in current
948 * vnet and if_attach()es the ifnet to the vnet specified as 2nd arg.
949 * An attempt is made to shrink if_index in current vnet, find an
950 * unused if_index in target vnet and calls if_grow() if necessary,
951 * and finally find an unused if_xname for the target vnet.
954 if_vmove(struct ifnet *ifp, struct vnet *new_vnet)
959 * Detach from current vnet, but preserve LLADDR info, do not
960 * mark as dead etc. so that the ifnet can be reattached later.
962 if_detach_internal(ifp, 1);
965 * Unlink the ifnet from ifindex_table[] in current vnet, and shrink
966 * the if_index for that vnet if possible.
968 * NOTE: IFNET_WLOCK/IFNET_WUNLOCK() are assumed to be unvirtualized,
969 * or we'd lock on one vnet and unlock on another.
972 ifindex_free_locked(ifp->if_index);
976 * Perform interface-specific reassignment tasks, if provided by
979 if (ifp->if_reassign != NULL)
980 ifp->if_reassign(ifp, new_vnet, NULL);
983 * Switch to the context of the target vnet.
985 CURVNET_SET_QUIET(new_vnet);
988 if (ifindex_alloc_locked(&idx) != 0) {
990 panic("if_index overflow");
993 ifnet_setbyindex_locked(ifp->if_index, ifp);
996 if_attach_internal(ifp, 1);
1002 * Move an ifnet to or from another child prison/vnet, specified by the jail id.
1005 if_vmove_loan(struct thread *td, struct ifnet *ifp, char *ifname, int jid)
1010 /* Try to find the prison within our visibility. */
1011 sx_slock(&allprison_lock);
1012 pr = prison_find_child(td->td_ucred->cr_prison, jid);
1013 sx_sunlock(&allprison_lock);
1016 prison_hold_locked(pr);
1017 mtx_unlock(&pr->pr_mtx);
1019 /* Do not try to move the iface from and to the same prison. */
1020 if (pr->pr_vnet == ifp->if_vnet) {
1025 /* Make sure the named iface does not exists in the dst. prison/vnet. */
1026 /* XXX Lock interfaces to avoid races. */
1027 CURVNET_SET_QUIET(pr->pr_vnet);
1028 difp = ifunit(ifname);
1035 /* Move the interface into the child jail/vnet. */
1036 if_vmove(ifp, pr->pr_vnet);
1038 /* Report the new if_xname back to the userland. */
1039 sprintf(ifname, "%s", ifp->if_xname);
1046 if_vmove_reclaim(struct thread *td, char *ifname, int jid)
1049 struct vnet *vnet_dst;
1052 /* Try to find the prison within our visibility. */
1053 sx_slock(&allprison_lock);
1054 pr = prison_find_child(td->td_ucred->cr_prison, jid);
1055 sx_sunlock(&allprison_lock);
1058 prison_hold_locked(pr);
1059 mtx_unlock(&pr->pr_mtx);
1061 /* Make sure the named iface exists in the source prison/vnet. */
1062 CURVNET_SET(pr->pr_vnet);
1063 ifp = ifunit(ifname); /* XXX Lock to avoid races. */
1070 /* Do not try to move the iface from and to the same prison. */
1071 vnet_dst = TD_TO_VNET(td);
1072 if (vnet_dst == ifp->if_vnet) {
1078 /* Get interface back from child jail/vnet. */
1079 if_vmove(ifp, vnet_dst);
1082 /* Report the new if_xname back to the userland. */
1083 sprintf(ifname, "%s", ifp->if_xname);
1091 * Add a group to an interface
1094 if_addgroup(struct ifnet *ifp, const char *groupname)
1096 struct ifg_list *ifgl;
1097 struct ifg_group *ifg = NULL;
1098 struct ifg_member *ifgm;
1100 if (groupname[0] && groupname[strlen(groupname) - 1] >= '0' &&
1101 groupname[strlen(groupname) - 1] <= '9')
1105 TAILQ_FOREACH(ifgl, &ifp->if_groups, ifgl_next)
1106 if (!strcmp(ifgl->ifgl_group->ifg_group, groupname)) {
1111 if ((ifgl = (struct ifg_list *)malloc(sizeof(struct ifg_list), M_TEMP,
1112 M_NOWAIT)) == NULL) {
1117 if ((ifgm = (struct ifg_member *)malloc(sizeof(struct ifg_member),
1118 M_TEMP, M_NOWAIT)) == NULL) {
1124 TAILQ_FOREACH(ifg, &V_ifg_head, ifg_next)
1125 if (!strcmp(ifg->ifg_group, groupname))
1129 if ((ifg = (struct ifg_group *)malloc(sizeof(struct ifg_group),
1130 M_TEMP, M_NOWAIT)) == NULL) {
1136 strlcpy(ifg->ifg_group, groupname, sizeof(ifg->ifg_group));
1137 ifg->ifg_refcnt = 0;
1138 TAILQ_INIT(&ifg->ifg_members);
1139 EVENTHANDLER_INVOKE(group_attach_event, ifg);
1140 TAILQ_INSERT_TAIL(&V_ifg_head, ifg, ifg_next);
1144 ifgl->ifgl_group = ifg;
1145 ifgm->ifgm_ifp = ifp;
1148 TAILQ_INSERT_TAIL(&ifg->ifg_members, ifgm, ifgm_next);
1149 TAILQ_INSERT_TAIL(&ifp->if_groups, ifgl, ifgl_next);
1150 IF_ADDR_UNLOCK(ifp);
1154 EVENTHANDLER_INVOKE(group_change_event, groupname);
1160 * Remove a group from an interface
1163 if_delgroup(struct ifnet *ifp, const char *groupname)
1165 struct ifg_list *ifgl;
1166 struct ifg_member *ifgm;
1169 TAILQ_FOREACH(ifgl, &ifp->if_groups, ifgl_next)
1170 if (!strcmp(ifgl->ifgl_group->ifg_group, groupname))
1178 TAILQ_REMOVE(&ifp->if_groups, ifgl, ifgl_next);
1179 IF_ADDR_UNLOCK(ifp);
1181 TAILQ_FOREACH(ifgm, &ifgl->ifgl_group->ifg_members, ifgm_next)
1182 if (ifgm->ifgm_ifp == ifp)
1186 TAILQ_REMOVE(&ifgl->ifgl_group->ifg_members, ifgm, ifgm_next);
1190 if (--ifgl->ifgl_group->ifg_refcnt == 0) {
1191 TAILQ_REMOVE(&V_ifg_head, ifgl->ifgl_group, ifg_next);
1192 EVENTHANDLER_INVOKE(group_detach_event, ifgl->ifgl_group);
1193 free(ifgl->ifgl_group, M_TEMP);
1199 EVENTHANDLER_INVOKE(group_change_event, groupname);
1205 * Remove an interface from all groups
1208 if_delgroups(struct ifnet *ifp)
1210 struct ifg_list *ifgl;
1211 struct ifg_member *ifgm;
1212 char groupname[IFNAMSIZ];
1215 while (!TAILQ_EMPTY(&ifp->if_groups)) {
1216 ifgl = TAILQ_FIRST(&ifp->if_groups);
1218 strlcpy(groupname, ifgl->ifgl_group->ifg_group, IFNAMSIZ);
1221 TAILQ_REMOVE(&ifp->if_groups, ifgl, ifgl_next);
1222 IF_ADDR_UNLOCK(ifp);
1224 TAILQ_FOREACH(ifgm, &ifgl->ifgl_group->ifg_members, ifgm_next)
1225 if (ifgm->ifgm_ifp == ifp)
1229 TAILQ_REMOVE(&ifgl->ifgl_group->ifg_members, ifgm,
1234 if (--ifgl->ifgl_group->ifg_refcnt == 0) {
1235 TAILQ_REMOVE(&V_ifg_head, ifgl->ifgl_group, ifg_next);
1236 EVENTHANDLER_INVOKE(group_detach_event,
1238 free(ifgl->ifgl_group, M_TEMP);
1244 EVENTHANDLER_INVOKE(group_change_event, groupname);
1252 * Stores all groups from an interface in memory pointed
1256 if_getgroup(struct ifgroupreq *data, struct ifnet *ifp)
1259 struct ifg_list *ifgl;
1260 struct ifg_req ifgrq, *ifgp;
1261 struct ifgroupreq *ifgr = data;
1263 if (ifgr->ifgr_len == 0) {
1265 TAILQ_FOREACH(ifgl, &ifp->if_groups, ifgl_next)
1266 ifgr->ifgr_len += sizeof(struct ifg_req);
1267 IF_ADDR_UNLOCK(ifp);
1271 len = ifgr->ifgr_len;
1272 ifgp = ifgr->ifgr_groups;
1275 TAILQ_FOREACH(ifgl, &ifp->if_groups, ifgl_next) {
1276 if (len < sizeof(ifgrq)) {
1277 IF_ADDR_UNLOCK(ifp);
1280 bzero(&ifgrq, sizeof ifgrq);
1281 strlcpy(ifgrq.ifgrq_group, ifgl->ifgl_group->ifg_group,
1282 sizeof(ifgrq.ifgrq_group));
1283 if ((error = copyout(&ifgrq, ifgp, sizeof(struct ifg_req)))) {
1284 IF_ADDR_UNLOCK(ifp);
1287 len -= sizeof(ifgrq);
1290 IF_ADDR_UNLOCK(ifp);
1296 * Stores all members of a group in memory pointed to by data
1299 if_getgroupmembers(struct ifgroupreq *data)
1301 struct ifgroupreq *ifgr = data;
1302 struct ifg_group *ifg;
1303 struct ifg_member *ifgm;
1304 struct ifg_req ifgrq, *ifgp;
1308 TAILQ_FOREACH(ifg, &V_ifg_head, ifg_next)
1309 if (!strcmp(ifg->ifg_group, ifgr->ifgr_name))
1316 if (ifgr->ifgr_len == 0) {
1317 TAILQ_FOREACH(ifgm, &ifg->ifg_members, ifgm_next)
1318 ifgr->ifgr_len += sizeof(ifgrq);
1323 len = ifgr->ifgr_len;
1324 ifgp = ifgr->ifgr_groups;
1325 TAILQ_FOREACH(ifgm, &ifg->ifg_members, ifgm_next) {
1326 if (len < sizeof(ifgrq)) {
1330 bzero(&ifgrq, sizeof ifgrq);
1331 strlcpy(ifgrq.ifgrq_member, ifgm->ifgm_ifp->if_xname,
1332 sizeof(ifgrq.ifgrq_member));
1333 if ((error = copyout(&ifgrq, ifgp, sizeof(struct ifg_req)))) {
1337 len -= sizeof(ifgrq);
1346 * Delete Routes for a Network Interface
1348 * Called for each routing entry via the rnh->rnh_walktree() call above
1349 * to delete all route entries referencing a detaching network interface.
1352 * rn pointer to node in the routing table
1353 * arg argument passed to rnh->rnh_walktree() - detaching interface
1357 * errno failed - reason indicated
1361 if_rtdel(struct radix_node *rn, void *arg)
1363 struct rtentry *rt = (struct rtentry *)rn;
1364 struct ifnet *ifp = arg;
1367 if (rt->rt_ifp == ifp) {
1370 * Protect (sorta) against walktree recursion problems
1371 * with cloned routes
1373 if ((rt->rt_flags & RTF_UP) == 0)
1376 err = rtrequest_fib(RTM_DELETE, rt_key(rt), rt->rt_gateway,
1377 rt_mask(rt), rt->rt_flags|RTF_RNH_LOCKED,
1378 (struct rtentry **) NULL, rt->rt_fibnum);
1380 log(LOG_WARNING, "if_rtdel: error %d\n", err);
1388 * Wrapper functions for struct ifnet address list locking macros. These are
1389 * used by kernel modules to avoid encoding programming interface or binary
1390 * interface assumptions that may be violated when kernel-internal locking
1391 * approaches change.
1394 if_addr_rlock(struct ifnet *ifp)
1401 if_addr_runlock(struct ifnet *ifp)
1404 IF_ADDR_UNLOCK(ifp);
1408 if_maddr_rlock(struct ifnet *ifp)
1415 if_maddr_runlock(struct ifnet *ifp)
1418 IF_ADDR_UNLOCK(ifp);
1422 * Reference count functions for ifaddrs.
1425 ifa_init(struct ifaddr *ifa)
1428 mtx_init(&ifa->ifa_mtx, "ifaddr", NULL, MTX_DEF);
1429 refcount_init(&ifa->ifa_refcnt, 1);
1433 ifa_ref(struct ifaddr *ifa)
1436 refcount_acquire(&ifa->ifa_refcnt);
1440 ifa_free(struct ifaddr *ifa)
1443 if (refcount_release(&ifa->ifa_refcnt)) {
1444 mtx_destroy(&ifa->ifa_mtx);
1445 free(ifa, M_IFADDR);
1450 ifa_add_loopback_route(struct ifaddr *ifa, struct sockaddr *ia)
1453 struct rtentry *rt = NULL;
1454 struct rt_addrinfo info;
1455 static struct sockaddr_dl null_sdl = {sizeof(null_sdl), AF_LINK};
1457 bzero(&info, sizeof(info));
1458 info.rti_ifp = V_loif;
1459 info.rti_flags = ifa->ifa_flags | RTF_HOST | RTF_STATIC;
1460 info.rti_info[RTAX_DST] = ia;
1461 info.rti_info[RTAX_GATEWAY] = (struct sockaddr *)&null_sdl;
1462 error = rtrequest1_fib(RTM_ADD, &info, &rt, 0);
1464 if (error == 0 && rt != NULL) {
1466 ((struct sockaddr_dl *)rt->rt_gateway)->sdl_type =
1467 ifa->ifa_ifp->if_type;
1468 ((struct sockaddr_dl *)rt->rt_gateway)->sdl_index =
1469 ifa->ifa_ifp->if_index;
1472 } else if (error != 0)
1473 log(LOG_INFO, "ifa_add_loopback_route: insertion failed\n");
1479 ifa_del_loopback_route(struct ifaddr *ifa, struct sockaddr *ia)
1482 struct rt_addrinfo info;
1483 struct sockaddr_dl null_sdl;
1485 bzero(&null_sdl, sizeof(null_sdl));
1486 null_sdl.sdl_len = sizeof(null_sdl);
1487 null_sdl.sdl_family = AF_LINK;
1488 null_sdl.sdl_type = ifa->ifa_ifp->if_type;
1489 null_sdl.sdl_index = ifa->ifa_ifp->if_index;
1490 bzero(&info, sizeof(info));
1491 info.rti_flags = ifa->ifa_flags | RTF_HOST | RTF_STATIC;
1492 info.rti_info[RTAX_DST] = ia;
1493 info.rti_info[RTAX_GATEWAY] = (struct sockaddr *)&null_sdl;
1494 error = rtrequest1_fib(RTM_DELETE, &info, NULL, 0);
1497 log(LOG_INFO, "ifa_del_loopback_route: deletion failed\n");
1503 * XXX: Because sockaddr_dl has deeper structure than the sockaddr
1504 * structs used to represent other address families, it is necessary
1505 * to perform a different comparison.
1508 #define sa_equal(a1, a2) \
1509 (bcmp((a1), (a2), ((a1))->sa_len) == 0)
1511 #define sa_dl_equal(a1, a2) \
1512 ((((struct sockaddr_dl *)(a1))->sdl_len == \
1513 ((struct sockaddr_dl *)(a2))->sdl_len) && \
1514 (bcmp(LLADDR((struct sockaddr_dl *)(a1)), \
1515 LLADDR((struct sockaddr_dl *)(a2)), \
1516 ((struct sockaddr_dl *)(a1))->sdl_alen) == 0))
1519 * Locate an interface based on a complete address.
1522 static struct ifaddr *
1523 ifa_ifwithaddr_internal(struct sockaddr *addr, int getref)
1528 IFNET_RLOCK_NOSLEEP();
1529 TAILQ_FOREACH(ifp, &V_ifnet, if_link) {
1531 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
1532 if (ifa->ifa_addr->sa_family != addr->sa_family)
1534 if (sa_equal(addr, ifa->ifa_addr)) {
1537 IF_ADDR_UNLOCK(ifp);
1540 /* IP6 doesn't have broadcast */
1541 if ((ifp->if_flags & IFF_BROADCAST) &&
1542 ifa->ifa_broadaddr &&
1543 ifa->ifa_broadaddr->sa_len != 0 &&
1544 sa_equal(ifa->ifa_broadaddr, addr)) {
1547 IF_ADDR_UNLOCK(ifp);
1551 IF_ADDR_UNLOCK(ifp);
1555 IFNET_RUNLOCK_NOSLEEP();
1560 ifa_ifwithaddr(struct sockaddr *addr)
1563 return (ifa_ifwithaddr_internal(addr, 1));
1567 ifa_ifwithaddr_check(struct sockaddr *addr)
1570 return (ifa_ifwithaddr_internal(addr, 0) != NULL);
1574 * Locate an interface based on the broadcast address.
1578 ifa_ifwithbroadaddr(struct sockaddr *addr)
1583 IFNET_RLOCK_NOSLEEP();
1584 TAILQ_FOREACH(ifp, &V_ifnet, if_link) {
1586 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
1587 if (ifa->ifa_addr->sa_family != addr->sa_family)
1589 if ((ifp->if_flags & IFF_BROADCAST) &&
1590 ifa->ifa_broadaddr &&
1591 ifa->ifa_broadaddr->sa_len != 0 &&
1592 sa_equal(ifa->ifa_broadaddr, addr)) {
1594 IF_ADDR_UNLOCK(ifp);
1598 IF_ADDR_UNLOCK(ifp);
1602 IFNET_RUNLOCK_NOSLEEP();
1607 * Locate the point to point interface with a given destination address.
1611 ifa_ifwithdstaddr(struct sockaddr *addr)
1616 IFNET_RLOCK_NOSLEEP();
1617 TAILQ_FOREACH(ifp, &V_ifnet, if_link) {
1618 if ((ifp->if_flags & IFF_POINTOPOINT) == 0)
1621 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
1622 if (ifa->ifa_addr->sa_family != addr->sa_family)
1624 if (ifa->ifa_dstaddr != NULL &&
1625 sa_equal(addr, ifa->ifa_dstaddr)) {
1627 IF_ADDR_UNLOCK(ifp);
1631 IF_ADDR_UNLOCK(ifp);
1635 IFNET_RUNLOCK_NOSLEEP();
1640 * Find an interface on a specific network. If many, choice
1641 * is most specific found.
1644 ifa_ifwithnet(struct sockaddr *addr, int ignore_ptp)
1648 struct ifaddr *ifa_maybe = NULL;
1649 u_int af = addr->sa_family;
1650 char *addr_data = addr->sa_data, *cplim;
1653 * AF_LINK addresses can be looked up directly by their index number,
1654 * so do that if we can.
1656 if (af == AF_LINK) {
1657 struct sockaddr_dl *sdl = (struct sockaddr_dl *)addr;
1658 if (sdl->sdl_index && sdl->sdl_index <= V_if_index)
1659 return (ifaddr_byindex(sdl->sdl_index));
1663 * Scan though each interface, looking for ones that have addresses
1664 * in this address family. Maintain a reference on ifa_maybe once
1665 * we find one, as we release the IF_ADDR_LOCK() that kept it stable
1666 * when we move onto the next interface.
1668 IFNET_RLOCK_NOSLEEP();
1669 TAILQ_FOREACH(ifp, &V_ifnet, if_link) {
1671 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
1672 char *cp, *cp2, *cp3;
1674 if (ifa->ifa_addr->sa_family != af)
1676 if (af == AF_INET &&
1677 ifp->if_flags & IFF_POINTOPOINT && !ignore_ptp) {
1679 * This is a bit broken as it doesn't
1680 * take into account that the remote end may
1681 * be a single node in the network we are
1683 * The trouble is that we don't know the
1684 * netmask for the remote end.
1686 if (ifa->ifa_dstaddr != NULL &&
1687 sa_equal(addr, ifa->ifa_dstaddr)) {
1689 IF_ADDR_UNLOCK(ifp);
1694 * if we have a special address handler,
1695 * then use it instead of the generic one.
1697 if (ifa->ifa_claim_addr) {
1698 if ((*ifa->ifa_claim_addr)(ifa, addr)) {
1700 IF_ADDR_UNLOCK(ifp);
1707 * Scan all the bits in the ifa's address.
1708 * If a bit dissagrees with what we are
1709 * looking for, mask it with the netmask
1710 * to see if it really matters.
1711 * (A byte at a time)
1713 if (ifa->ifa_netmask == 0)
1716 cp2 = ifa->ifa_addr->sa_data;
1717 cp3 = ifa->ifa_netmask->sa_data;
1718 cplim = ifa->ifa_netmask->sa_len
1719 + (char *)ifa->ifa_netmask;
1721 if ((*cp++ ^ *cp2++) & *cp3++)
1722 goto next; /* next address! */
1724 * If the netmask of what we just found
1725 * is more specific than what we had before
1726 * (if we had one) then remember the new one
1727 * before continuing to search
1728 * for an even better one.
1730 if (ifa_maybe == NULL ||
1731 rn_refines((caddr_t)ifa->ifa_netmask,
1732 (caddr_t)ifa_maybe->ifa_netmask)) {
1733 if (ifa_maybe != NULL)
1734 ifa_free(ifa_maybe);
1740 IF_ADDR_UNLOCK(ifp);
1745 IFNET_RUNLOCK_NOSLEEP();
1746 if (ifa_maybe != NULL)
1747 ifa_free(ifa_maybe);
1752 * Find an interface address specific to an interface best matching
1756 ifaof_ifpforaddr(struct sockaddr *addr, struct ifnet *ifp)
1759 char *cp, *cp2, *cp3;
1761 struct ifaddr *ifa_maybe = NULL;
1762 u_int af = addr->sa_family;
1767 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
1768 if (ifa->ifa_addr->sa_family != af)
1770 if (ifa_maybe == NULL)
1772 if (ifa->ifa_netmask == 0) {
1773 if (sa_equal(addr, ifa->ifa_addr) ||
1774 (ifa->ifa_dstaddr &&
1775 sa_equal(addr, ifa->ifa_dstaddr)))
1779 if (ifp->if_flags & IFF_POINTOPOINT) {
1780 if (sa_equal(addr, ifa->ifa_dstaddr))
1784 cp2 = ifa->ifa_addr->sa_data;
1785 cp3 = ifa->ifa_netmask->sa_data;
1786 cplim = ifa->ifa_netmask->sa_len + (char *)ifa->ifa_netmask;
1787 for (; cp3 < cplim; cp3++)
1788 if ((*cp++ ^ *cp2++) & *cp3)
1798 IF_ADDR_UNLOCK(ifp);
1802 #include <net/if_llatbl.h>
1805 * Default action when installing a route with a Link Level gateway.
1806 * Lookup an appropriate real ifa to point to.
1807 * This should be moved to /sys/net/link.c eventually.
1810 link_rtrequest(int cmd, struct rtentry *rt, struct rt_addrinfo *info)
1812 struct ifaddr *ifa, *oifa;
1813 struct sockaddr *dst;
1818 if (cmd != RTM_ADD || ((ifa = rt->rt_ifa) == 0) ||
1819 ((ifp = ifa->ifa_ifp) == 0) || ((dst = rt_key(rt)) == 0))
1821 ifa = ifaof_ifpforaddr(dst, ifp);
1826 if (ifa->ifa_rtrequest && ifa->ifa_rtrequest != link_rtrequest)
1827 ifa->ifa_rtrequest(cmd, rt, info);
1832 * Mark an interface down and notify protocols of
1834 * NOTE: must be called at splnet or eqivalent.
1837 if_unroute(struct ifnet *ifp, int flag, int fam)
1841 KASSERT(flag == IFF_UP, ("if_unroute: flag != IFF_UP"));
1843 ifp->if_flags &= ~flag;
1844 getmicrotime(&ifp->if_lastchange);
1845 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link)
1846 if (fam == PF_UNSPEC || (fam == ifa->ifa_addr->sa_family))
1847 pfctlinput(PRC_IFDOWN, ifa->ifa_addr);
1848 ifp->if_qflush(ifp);
1851 (*carp_linkstate_p)(ifp);
1856 * Mark an interface up and notify protocols of
1858 * NOTE: must be called at splnet or eqivalent.
1861 if_route(struct ifnet *ifp, int flag, int fam)
1865 KASSERT(flag == IFF_UP, ("if_route: flag != IFF_UP"));
1867 ifp->if_flags |= flag;
1868 getmicrotime(&ifp->if_lastchange);
1869 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link)
1870 if (fam == PF_UNSPEC || (fam == ifa->ifa_addr->sa_family))
1871 pfctlinput(PRC_IFUP, ifa->ifa_addr);
1873 (*carp_linkstate_p)(ifp);
1880 void (*vlan_link_state_p)(struct ifnet *); /* XXX: private from if_vlan */
1881 void (*vlan_trunk_cap_p)(struct ifnet *); /* XXX: private from if_vlan */
1884 * Handle a change in the interface link state. To avoid LORs
1885 * between driver lock and upper layer locks, as well as possible
1886 * recursions, we post event to taskqueue, and all job
1887 * is done in static do_link_state_change().
1890 if_link_state_change(struct ifnet *ifp, int link_state)
1892 /* Return if state hasn't changed. */
1893 if (ifp->if_link_state == link_state)
1896 ifp->if_link_state = link_state;
1898 taskqueue_enqueue(taskqueue_swi, &ifp->if_linktask);
1902 do_link_state_change(void *arg, int pending)
1904 struct ifnet *ifp = (struct ifnet *)arg;
1905 int link_state = ifp->if_link_state;
1906 CURVNET_SET(ifp->if_vnet);
1908 /* Notify that the link state has changed. */
1910 if (ifp->if_vlantrunk != NULL)
1911 (*vlan_link_state_p)(ifp);
1913 if ((ifp->if_type == IFT_ETHER || ifp->if_type == IFT_L2VLAN) &&
1914 IFP2AC(ifp)->ac_netgraph != NULL)
1915 (*ng_ether_link_state_p)(ifp, link_state);
1917 (*carp_linkstate_p)(ifp);
1918 if (ifp->if_bridge) {
1919 KASSERT(bstp_linkstate_p != NULL,("if_bridge bstp not loaded!"));
1920 (*bstp_linkstate_p)(ifp, link_state);
1923 KASSERT(lagg_linkstate_p != NULL,("if_lagg not loaded!"));
1924 (*lagg_linkstate_p)(ifp, link_state);
1927 if (IS_DEFAULT_VNET(curvnet))
1928 devctl_notify("IFNET", ifp->if_xname,
1929 (link_state == LINK_STATE_UP) ? "LINK_UP" : "LINK_DOWN",
1932 if_printf(ifp, "%d link states coalesced\n", pending);
1933 if (log_link_state_change)
1934 log(LOG_NOTICE, "%s: link state changed to %s\n", ifp->if_xname,
1935 (link_state == LINK_STATE_UP) ? "UP" : "DOWN" );
1940 * Mark an interface down and notify protocols of
1942 * NOTE: must be called at splnet or eqivalent.
1945 if_down(struct ifnet *ifp)
1948 if_unroute(ifp, IFF_UP, AF_UNSPEC);
1952 * Mark an interface up and notify protocols of
1954 * NOTE: must be called at splnet or eqivalent.
1957 if_up(struct ifnet *ifp)
1960 if_route(ifp, IFF_UP, AF_UNSPEC);
1964 * Flush an interface queue.
1967 if_qflush(struct ifnet *ifp)
1975 if (ALTQ_IS_ENABLED(ifq))
1979 while ((m = n) != 0) {
1990 * Map interface name to interface structure pointer, with or without
1991 * returning a reference.
1994 ifunit_ref(const char *name)
1998 IFNET_RLOCK_NOSLEEP();
1999 TAILQ_FOREACH(ifp, &V_ifnet, if_link) {
2000 if (strncmp(name, ifp->if_xname, IFNAMSIZ) == 0 &&
2001 !(ifp->if_flags & IFF_DYING))
2006 IFNET_RUNLOCK_NOSLEEP();
2011 ifunit(const char *name)
2015 IFNET_RLOCK_NOSLEEP();
2016 TAILQ_FOREACH(ifp, &V_ifnet, if_link) {
2017 if (strncmp(name, ifp->if_xname, IFNAMSIZ) == 0)
2020 IFNET_RUNLOCK_NOSLEEP();
2025 * Hardware specific interface ioctls.
2028 ifhwioctl(u_long cmd, struct ifnet *ifp, caddr_t data, struct thread *td)
2033 int new_flags, temp_flags;
2034 size_t namelen, onamelen;
2036 char *descrbuf, *odescrbuf;
2037 char new_name[IFNAMSIZ];
2039 struct sockaddr_dl *sdl;
2041 ifr = (struct ifreq *)data;
2044 ifr->ifr_index = ifp->if_index;
2048 temp_flags = ifp->if_flags | ifp->if_drv_flags;
2049 ifr->ifr_flags = temp_flags & 0xffff;
2050 ifr->ifr_flagshigh = temp_flags >> 16;
2054 ifr->ifr_reqcap = ifp->if_capabilities;
2055 ifr->ifr_curcap = ifp->if_capenable;
2060 error = mac_ifnet_ioctl_get(td->td_ucred, ifr, ifp);
2065 ifr->ifr_metric = ifp->if_metric;
2069 ifr->ifr_mtu = ifp->if_mtu;
2073 ifr->ifr_phys = ifp->if_physical;
2078 sx_slock(&ifdescr_sx);
2079 if (ifp->if_description == NULL)
2082 /* space for terminating nul */
2083 descrlen = strlen(ifp->if_description) + 1;
2084 if (ifr->ifr_buffer.length < descrlen)
2085 ifr->ifr_buffer.buffer = NULL;
2087 error = copyout(ifp->if_description,
2088 ifr->ifr_buffer.buffer, descrlen);
2089 ifr->ifr_buffer.length = descrlen;
2091 sx_sunlock(&ifdescr_sx);
2095 error = priv_check(td, PRIV_NET_SETIFDESCR);
2100 * Copy only (length-1) bytes to make sure that
2101 * if_description is always nul terminated. The
2102 * length parameter is supposed to count the
2103 * terminating nul in.
2105 if (ifr->ifr_buffer.length > ifdescr_maxlen)
2106 return (ENAMETOOLONG);
2107 else if (ifr->ifr_buffer.length == 0)
2110 descrbuf = malloc(ifr->ifr_buffer.length, M_IFDESCR,
2112 error = copyin(ifr->ifr_buffer.buffer, descrbuf,
2113 ifr->ifr_buffer.length - 1);
2115 free(descrbuf, M_IFDESCR);
2120 sx_xlock(&ifdescr_sx);
2121 odescrbuf = ifp->if_description;
2122 ifp->if_description = descrbuf;
2123 sx_xunlock(&ifdescr_sx);
2125 getmicrotime(&ifp->if_lastchange);
2126 free(odescrbuf, M_IFDESCR);
2130 error = priv_check(td, PRIV_NET_SETIFFLAGS);
2134 * Currently, no driver owned flags pass the IFF_CANTCHANGE
2135 * check, so we don't need special handling here yet.
2137 new_flags = (ifr->ifr_flags & 0xffff) |
2138 (ifr->ifr_flagshigh << 16);
2139 if (ifp->if_flags & IFF_SMART) {
2140 /* Smart drivers twiddle their own routes */
2141 } else if (ifp->if_flags & IFF_UP &&
2142 (new_flags & IFF_UP) == 0) {
2146 } else if (new_flags & IFF_UP &&
2147 (ifp->if_flags & IFF_UP) == 0) {
2152 /* See if permanently promiscuous mode bit is about to flip */
2153 if ((ifp->if_flags ^ new_flags) & IFF_PPROMISC) {
2154 if (new_flags & IFF_PPROMISC)
2155 ifp->if_flags |= IFF_PROMISC;
2156 else if (ifp->if_pcount == 0)
2157 ifp->if_flags &= ~IFF_PROMISC;
2158 log(LOG_INFO, "%s: permanently promiscuous mode %s\n",
2160 (new_flags & IFF_PPROMISC) ? "enabled" : "disabled");
2162 ifp->if_flags = (ifp->if_flags & IFF_CANTCHANGE) |
2163 (new_flags &~ IFF_CANTCHANGE);
2164 if (ifp->if_ioctl) {
2165 (void) (*ifp->if_ioctl)(ifp, cmd, data);
2167 getmicrotime(&ifp->if_lastchange);
2171 error = priv_check(td, PRIV_NET_SETIFCAP);
2174 if (ifp->if_ioctl == NULL)
2175 return (EOPNOTSUPP);
2176 if (ifr->ifr_reqcap & ~ifp->if_capabilities)
2178 error = (*ifp->if_ioctl)(ifp, cmd, data);
2180 getmicrotime(&ifp->if_lastchange);
2185 error = mac_ifnet_ioctl_set(td->td_ucred, ifr, ifp);
2190 error = priv_check(td, PRIV_NET_SETIFNAME);
2193 error = copyinstr(ifr->ifr_data, new_name, IFNAMSIZ, NULL);
2196 if (new_name[0] == '\0')
2198 if (ifunit(new_name) != NULL)
2202 * XXX: Locking. Nothing else seems to lock if_flags,
2203 * and there are numerous other races with the
2204 * ifunit() checks not being atomic with namespace
2205 * changes (renames, vmoves, if_attach, etc).
2207 ifp->if_flags |= IFF_RENAMING;
2209 /* Announce the departure of the interface. */
2210 rt_ifannouncemsg(ifp, IFAN_DEPARTURE);
2211 EVENTHANDLER_INVOKE(ifnet_departure_event, ifp);
2213 log(LOG_INFO, "%s: changing name to '%s'\n",
2214 ifp->if_xname, new_name);
2216 strlcpy(ifp->if_xname, new_name, sizeof(ifp->if_xname));
2219 sdl = (struct sockaddr_dl *)ifa->ifa_addr;
2220 namelen = strlen(new_name);
2221 onamelen = sdl->sdl_nlen;
2223 * Move the address if needed. This is safe because we
2224 * allocate space for a name of length IFNAMSIZ when we
2225 * create this in if_attach().
2227 if (namelen != onamelen) {
2228 bcopy(sdl->sdl_data + onamelen,
2229 sdl->sdl_data + namelen, sdl->sdl_alen);
2231 bcopy(new_name, sdl->sdl_data, namelen);
2232 sdl->sdl_nlen = namelen;
2233 sdl = (struct sockaddr_dl *)ifa->ifa_netmask;
2234 bzero(sdl->sdl_data, onamelen);
2235 while (namelen != 0)
2236 sdl->sdl_data[--namelen] = 0xff;
2239 EVENTHANDLER_INVOKE(ifnet_arrival_event, ifp);
2240 /* Announce the return of the interface. */
2241 rt_ifannouncemsg(ifp, IFAN_ARRIVAL);
2243 ifp->if_flags &= ~IFF_RENAMING;
2248 error = priv_check(td, PRIV_NET_SETIFVNET);
2251 error = if_vmove_loan(td, ifp, ifr->ifr_name, ifr->ifr_jid);
2256 error = priv_check(td, PRIV_NET_SETIFMETRIC);
2259 ifp->if_metric = ifr->ifr_metric;
2260 getmicrotime(&ifp->if_lastchange);
2264 error = priv_check(td, PRIV_NET_SETIFPHYS);
2267 if (ifp->if_ioctl == NULL)
2268 return (EOPNOTSUPP);
2269 error = (*ifp->if_ioctl)(ifp, cmd, data);
2271 getmicrotime(&ifp->if_lastchange);
2276 u_long oldmtu = ifp->if_mtu;
2278 error = priv_check(td, PRIV_NET_SETIFMTU);
2281 if (ifr->ifr_mtu < IF_MINMTU || ifr->ifr_mtu > IF_MAXMTU)
2283 if (ifp->if_ioctl == NULL)
2284 return (EOPNOTSUPP);
2285 error = (*ifp->if_ioctl)(ifp, cmd, data);
2287 getmicrotime(&ifp->if_lastchange);
2291 * If the link MTU changed, do network layer specific procedure.
2293 if (ifp->if_mtu != oldmtu) {
2303 if (cmd == SIOCADDMULTI)
2304 error = priv_check(td, PRIV_NET_ADDMULTI);
2306 error = priv_check(td, PRIV_NET_DELMULTI);
2310 /* Don't allow group membership on non-multicast interfaces. */
2311 if ((ifp->if_flags & IFF_MULTICAST) == 0)
2312 return (EOPNOTSUPP);
2314 /* Don't let users screw up protocols' entries. */
2315 if (ifr->ifr_addr.sa_family != AF_LINK)
2318 if (cmd == SIOCADDMULTI) {
2319 struct ifmultiaddr *ifma;
2322 * Userland is only permitted to join groups once
2323 * via the if_addmulti() KPI, because it cannot hold
2324 * struct ifmultiaddr * between calls. It may also
2325 * lose a race while we check if the membership
2329 ifma = if_findmulti(ifp, &ifr->ifr_addr);
2330 IF_ADDR_UNLOCK(ifp);
2334 error = if_addmulti(ifp, &ifr->ifr_addr, &ifma);
2336 error = if_delmulti(ifp, &ifr->ifr_addr);
2339 getmicrotime(&ifp->if_lastchange);
2342 case SIOCSIFPHYADDR:
2343 case SIOCDIFPHYADDR:
2345 case SIOCSIFPHYADDR_IN6:
2347 case SIOCSLIFPHYADDR:
2349 case SIOCSIFGENERIC:
2350 error = priv_check(td, PRIV_NET_HWIOCTL);
2353 if (ifp->if_ioctl == NULL)
2354 return (EOPNOTSUPP);
2355 error = (*ifp->if_ioctl)(ifp, cmd, data);
2357 getmicrotime(&ifp->if_lastchange);
2361 ifs = (struct ifstat *)data;
2362 ifs->ascii[0] = '\0';
2364 case SIOCGIFPSRCADDR:
2365 case SIOCGIFPDSTADDR:
2366 case SIOCGLIFPHYADDR:
2368 case SIOCGIFGENERIC:
2369 if (ifp->if_ioctl == NULL)
2370 return (EOPNOTSUPP);
2371 error = (*ifp->if_ioctl)(ifp, cmd, data);
2375 error = priv_check(td, PRIV_NET_SETLLADDR);
2378 error = if_setlladdr(ifp,
2379 ifr->ifr_addr.sa_data, ifr->ifr_addr.sa_len);
2380 EVENTHANDLER_INVOKE(iflladdr_event, ifp);
2385 struct ifgroupreq *ifgr = (struct ifgroupreq *)ifr;
2387 error = priv_check(td, PRIV_NET_ADDIFGROUP);
2390 if ((error = if_addgroup(ifp, ifgr->ifgr_group)))
2396 if ((error = if_getgroup((struct ifgroupreq *)ifr, ifp)))
2402 struct ifgroupreq *ifgr = (struct ifgroupreq *)ifr;
2404 error = priv_check(td, PRIV_NET_DELIFGROUP);
2407 if ((error = if_delgroup(ifp, ifgr->ifgr_group)))
2419 #ifdef COMPAT_FREEBSD32
2427 #define SIOCGIFCONF32 _IOWR('i', 36, struct ifconf32)
2434 ifioctl(struct socket *so, u_long cmd, caddr_t data, struct thread *td)
2444 return (ifconf(cmd, data));
2446 #ifdef COMPAT_FREEBSD32
2449 struct ifconf32 *ifc32;
2452 ifc32 = (struct ifconf32 *)data;
2453 ifc.ifc_len = ifc32->ifc_len;
2454 ifc.ifc_buf = PTRIN(ifc32->ifc_buf);
2456 return (ifconf(SIOCGIFCONF, (void *)&ifc));
2460 ifr = (struct ifreq *)data;
2465 error = priv_check(td, PRIV_NET_SETIFVNET);
2468 return (if_vmove_reclaim(td, ifr->ifr_name, ifr->ifr_jid));
2472 error = priv_check(td, PRIV_NET_IFCREATE);
2475 return (if_clone_create(ifr->ifr_name, sizeof(ifr->ifr_name),
2476 cmd == SIOCIFCREATE2 ? ifr->ifr_data : NULL));
2478 error = priv_check(td, PRIV_NET_IFDESTROY);
2481 return if_clone_destroy(ifr->ifr_name);
2483 case SIOCIFGCLONERS:
2484 return (if_clone_list((struct if_clonereq *)data));
2486 return (if_getgroupmembers((struct ifgroupreq *)data));
2489 ifp = ifunit_ref(ifr->ifr_name);
2493 error = ifhwioctl(cmd, ifp, data, td);
2494 if (error != ENOIOCTL) {
2499 oif_flags = ifp->if_flags;
2500 if (so->so_proto == NULL) {
2502 return (EOPNOTSUPP);
2505 error = ((*so->so_proto->pr_usrreqs->pru_control)(so, cmd,
2508 if (error == EOPNOTSUPP && ifp != NULL && ifp->if_ioctl != NULL)
2509 error = (*ifp->if_ioctl)(ifp, cmd, data);
2516 case SIOCSIFDSTADDR:
2518 case SIOCSIFBRDADDR:
2519 case SIOCSIFNETMASK:
2520 #if BYTE_ORDER != BIG_ENDIAN
2521 if (ifr->ifr_addr.sa_family == 0 &&
2522 ifr->ifr_addr.sa_len < 16) {
2523 ifr->ifr_addr.sa_family = ifr->ifr_addr.sa_len;
2524 ifr->ifr_addr.sa_len = 16;
2527 if (ifr->ifr_addr.sa_len == 0)
2528 ifr->ifr_addr.sa_len = 16;
2536 case OSIOCGIFDSTADDR:
2537 cmd = SIOCGIFDSTADDR;
2540 case OSIOCGIFBRDADDR:
2541 cmd = SIOCGIFBRDADDR;
2544 case OSIOCGIFNETMASK:
2545 cmd = SIOCGIFNETMASK;
2547 error = ((*so->so_proto->pr_usrreqs->pru_control)(so,
2551 if (error == EOPNOTSUPP && ifp != NULL &&
2552 ifp->if_ioctl != NULL)
2553 error = (*ifp->if_ioctl)(ifp, cmd, data);
2557 case OSIOCGIFDSTADDR:
2558 case OSIOCGIFBRDADDR:
2559 case OSIOCGIFNETMASK:
2560 *(u_short *)&ifr->ifr_addr = ifr->ifr_addr.sa_family;
2564 #endif /* COMPAT_43 */
2566 if ((oif_flags ^ ifp->if_flags) & IFF_UP) {
2568 if (ifp->if_flags & IFF_UP) {
2580 * The code common to handling reference counted flags,
2581 * e.g., in ifpromisc() and if_allmulti().
2582 * The "pflag" argument can specify a permanent mode flag to check,
2583 * such as IFF_PPROMISC for promiscuous mode; should be 0 if none.
2585 * Only to be used on stack-owned flags, not driver-owned flags.
2588 if_setflag(struct ifnet *ifp, int flag, int pflag, int *refcount, int onswitch)
2592 int oldflags, oldcount;
2594 /* Sanity checks to catch programming errors */
2595 KASSERT((flag & (IFF_DRV_OACTIVE|IFF_DRV_RUNNING)) == 0,
2596 ("%s: setting driver-owned flag %d", __func__, flag));
2599 KASSERT(*refcount >= 0,
2600 ("%s: increment negative refcount %d for flag %d",
2601 __func__, *refcount, flag));
2603 KASSERT(*refcount > 0,
2604 ("%s: decrement non-positive refcount %d for flag %d",
2605 __func__, *refcount, flag));
2607 /* In case this mode is permanent, just touch refcount */
2608 if (ifp->if_flags & pflag) {
2609 *refcount += onswitch ? 1 : -1;
2613 /* Save ifnet parameters for if_ioctl() may fail */
2614 oldcount = *refcount;
2615 oldflags = ifp->if_flags;
2618 * See if we aren't the only and touching refcount is enough.
2619 * Actually toggle interface flag if we are the first or last.
2624 ifp->if_flags |= flag;
2628 ifp->if_flags &= ~flag;
2631 /* Call down the driver since we've changed interface flags */
2632 if (ifp->if_ioctl == NULL) {
2636 ifr.ifr_flags = ifp->if_flags & 0xffff;
2637 ifr.ifr_flagshigh = ifp->if_flags >> 16;
2638 error = (*ifp->if_ioctl)(ifp, SIOCSIFFLAGS, (caddr_t)&ifr);
2641 /* Notify userland that interface flags have changed */
2646 /* Recover after driver error */
2647 *refcount = oldcount;
2648 ifp->if_flags = oldflags;
2653 * Set/clear promiscuous mode on interface ifp based on the truth value
2654 * of pswitch. The calls are reference counted so that only the first
2655 * "on" request actually has an effect, as does the final "off" request.
2656 * Results are undefined if the "off" and "on" requests are not matched.
2659 ifpromisc(struct ifnet *ifp, int pswitch)
2662 int oldflags = ifp->if_flags;
2664 error = if_setflag(ifp, IFF_PROMISC, IFF_PPROMISC,
2665 &ifp->if_pcount, pswitch);
2666 /* If promiscuous mode status has changed, log a message */
2667 if (error == 0 && ((ifp->if_flags ^ oldflags) & IFF_PROMISC))
2668 log(LOG_INFO, "%s: promiscuous mode %s\n",
2670 (ifp->if_flags & IFF_PROMISC) ? "enabled" : "disabled");
2675 * Return interface configuration
2676 * of system. List may be used
2677 * in later ioctl's (above) to get
2678 * other information.
2682 ifconf(u_long cmd, caddr_t data)
2684 struct ifconf *ifc = (struct ifconf *)data;
2689 int error, full = 0, valid_len, max_len;
2691 /* Limit initial buffer size to MAXPHYS to avoid DoS from userspace. */
2692 max_len = MAXPHYS - 1;
2694 /* Prevent hostile input from being able to crash the system */
2695 if (ifc->ifc_len <= 0)
2699 if (ifc->ifc_len <= max_len) {
2700 max_len = ifc->ifc_len;
2703 sb = sbuf_new(NULL, NULL, max_len + 1, SBUF_FIXEDLEN);
2708 TAILQ_FOREACH(ifp, &V_ifnet, if_link) {
2712 * Zero the ifr_name buffer to make sure we don't
2713 * disclose the contents of the stack.
2715 memset(ifr.ifr_name, 0, sizeof(ifr.ifr_name));
2717 if (strlcpy(ifr.ifr_name, ifp->if_xname, sizeof(ifr.ifr_name))
2718 >= sizeof(ifr.ifr_name)) {
2721 return (ENAMETOOLONG);
2726 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
2727 struct sockaddr *sa = ifa->ifa_addr;
2729 if (prison_if(curthread->td_ucred, sa) != 0)
2733 if (cmd == OSIOCGIFCONF) {
2734 struct osockaddr *osa =
2735 (struct osockaddr *)&ifr.ifr_addr;
2737 osa->sa_family = sa->sa_family;
2738 sbuf_bcat(sb, &ifr, sizeof(ifr));
2739 max_len += sizeof(ifr);
2742 if (sa->sa_len <= sizeof(*sa)) {
2744 sbuf_bcat(sb, &ifr, sizeof(ifr));
2745 max_len += sizeof(ifr);
2748 offsetof(struct ifreq, ifr_addr));
2749 max_len += offsetof(struct ifreq, ifr_addr);
2750 sbuf_bcat(sb, sa, sa->sa_len);
2751 max_len += sa->sa_len;
2754 if (sbuf_error(sb) == 0)
2755 valid_len = sbuf_len(sb);
2757 IF_ADDR_UNLOCK(ifp);
2759 bzero((caddr_t)&ifr.ifr_addr, sizeof(ifr.ifr_addr));
2760 sbuf_bcat(sb, &ifr, sizeof(ifr));
2761 max_len += sizeof(ifr);
2763 if (sbuf_error(sb) == 0)
2764 valid_len = sbuf_len(sb);
2770 * If we didn't allocate enough space (uncommon), try again. If
2771 * we have already allocated as much space as we are allowed,
2772 * return what we've got.
2774 if (valid_len != max_len && !full) {
2779 ifc->ifc_len = valid_len;
2781 error = copyout(sbuf_data(sb), ifc->ifc_req, ifc->ifc_len);
2787 * Just like ifpromisc(), but for all-multicast-reception mode.
2790 if_allmulti(struct ifnet *ifp, int onswitch)
2793 return (if_setflag(ifp, IFF_ALLMULTI, 0, &ifp->if_amcount, onswitch));
2796 struct ifmultiaddr *
2797 if_findmulti(struct ifnet *ifp, struct sockaddr *sa)
2799 struct ifmultiaddr *ifma;
2801 IF_ADDR_LOCK_ASSERT(ifp);
2803 TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
2804 if (sa->sa_family == AF_LINK) {
2805 if (sa_dl_equal(ifma->ifma_addr, sa))
2808 if (sa_equal(ifma->ifma_addr, sa))
2817 * Allocate a new ifmultiaddr and initialize based on passed arguments. We
2818 * make copies of passed sockaddrs. The ifmultiaddr will not be added to
2819 * the ifnet multicast address list here, so the caller must do that and
2820 * other setup work (such as notifying the device driver). The reference
2821 * count is initialized to 1.
2823 static struct ifmultiaddr *
2824 if_allocmulti(struct ifnet *ifp, struct sockaddr *sa, struct sockaddr *llsa,
2827 struct ifmultiaddr *ifma;
2828 struct sockaddr *dupsa;
2830 ifma = malloc(sizeof *ifma, M_IFMADDR, mflags |
2835 dupsa = malloc(sa->sa_len, M_IFMADDR, mflags);
2836 if (dupsa == NULL) {
2837 free(ifma, M_IFMADDR);
2840 bcopy(sa, dupsa, sa->sa_len);
2841 ifma->ifma_addr = dupsa;
2843 ifma->ifma_ifp = ifp;
2844 ifma->ifma_refcount = 1;
2845 ifma->ifma_protospec = NULL;
2848 ifma->ifma_lladdr = NULL;
2852 dupsa = malloc(llsa->sa_len, M_IFMADDR, mflags);
2853 if (dupsa == NULL) {
2854 free(ifma->ifma_addr, M_IFMADDR);
2855 free(ifma, M_IFMADDR);
2858 bcopy(llsa, dupsa, llsa->sa_len);
2859 ifma->ifma_lladdr = dupsa;
2865 * if_freemulti: free ifmultiaddr structure and possibly attached related
2866 * addresses. The caller is responsible for implementing reference
2867 * counting, notifying the driver, handling routing messages, and releasing
2868 * any dependent link layer state.
2871 if_freemulti(struct ifmultiaddr *ifma)
2874 KASSERT(ifma->ifma_refcount == 0, ("if_freemulti: refcount %d",
2875 ifma->ifma_refcount));
2876 KASSERT(ifma->ifma_protospec == NULL,
2877 ("if_freemulti: protospec not NULL"));
2879 if (ifma->ifma_lladdr != NULL)
2880 free(ifma->ifma_lladdr, M_IFMADDR);
2881 free(ifma->ifma_addr, M_IFMADDR);
2882 free(ifma, M_IFMADDR);
2886 * Register an additional multicast address with a network interface.
2888 * - If the address is already present, bump the reference count on the
2889 * address and return.
2890 * - If the address is not link-layer, look up a link layer address.
2891 * - Allocate address structures for one or both addresses, and attach to the
2892 * multicast address list on the interface. If automatically adding a link
2893 * layer address, the protocol address will own a reference to the link
2894 * layer address, to be freed when it is freed.
2895 * - Notify the network device driver of an addition to the multicast address
2898 * 'sa' points to caller-owned memory with the desired multicast address.
2900 * 'retifma' will be used to return a pointer to the resulting multicast
2901 * address reference, if desired.
2904 if_addmulti(struct ifnet *ifp, struct sockaddr *sa,
2905 struct ifmultiaddr **retifma)
2907 struct ifmultiaddr *ifma, *ll_ifma;
2908 struct sockaddr *llsa;
2912 * If the address is already present, return a new reference to it;
2913 * otherwise, allocate storage and set up a new address.
2916 ifma = if_findmulti(ifp, sa);
2918 ifma->ifma_refcount++;
2919 if (retifma != NULL)
2921 IF_ADDR_UNLOCK(ifp);
2926 * The address isn't already present; resolve the protocol address
2927 * into a link layer address, and then look that up, bump its
2928 * refcount or allocate an ifma for that also. If 'llsa' was
2929 * returned, we will need to free it later.
2933 if (ifp->if_resolvemulti != NULL) {
2934 error = ifp->if_resolvemulti(ifp, &llsa, sa);
2940 * Allocate the new address. Don't hook it up yet, as we may also
2941 * need to allocate a link layer multicast address.
2943 ifma = if_allocmulti(ifp, sa, llsa, M_NOWAIT);
2950 * If a link layer address is found, we'll need to see if it's
2951 * already present in the address list, or allocate is as well.
2952 * When this block finishes, the link layer address will be on the
2956 ll_ifma = if_findmulti(ifp, llsa);
2957 if (ll_ifma == NULL) {
2958 ll_ifma = if_allocmulti(ifp, llsa, NULL, M_NOWAIT);
2959 if (ll_ifma == NULL) {
2960 --ifma->ifma_refcount;
2965 TAILQ_INSERT_HEAD(&ifp->if_multiaddrs, ll_ifma,
2968 ll_ifma->ifma_refcount++;
2969 ifma->ifma_llifma = ll_ifma;
2973 * We now have a new multicast address, ifma, and possibly a new or
2974 * referenced link layer address. Add the primary address to the
2975 * ifnet address list.
2977 TAILQ_INSERT_HEAD(&ifp->if_multiaddrs, ifma, ifma_link);
2979 if (retifma != NULL)
2983 * Must generate the message while holding the lock so that 'ifma'
2984 * pointer is still valid.
2986 rt_newmaddrmsg(RTM_NEWMADDR, ifma);
2987 IF_ADDR_UNLOCK(ifp);
2990 * We are certain we have added something, so call down to the
2991 * interface to let them know about it.
2993 if (ifp->if_ioctl != NULL) {
2994 (void) (*ifp->if_ioctl)(ifp, SIOCADDMULTI, 0);
2998 free(llsa, M_IFMADDR);
3004 free(llsa, M_IFMADDR);
3007 IF_ADDR_UNLOCK(ifp);
3012 * Delete a multicast group membership by network-layer group address.
3014 * Returns ENOENT if the entry could not be found. If ifp no longer
3015 * exists, results are undefined. This entry point should only be used
3016 * from subsystems which do appropriate locking to hold ifp for the
3017 * duration of the call.
3018 * Network-layer protocol domains must use if_delmulti_ifma().
3021 if_delmulti(struct ifnet *ifp, struct sockaddr *sa)
3023 struct ifmultiaddr *ifma;
3028 IFNET_RLOCK_NOSLEEP();
3029 TAILQ_FOREACH(oifp, &V_ifnet, if_link)
3034 IFNET_RUNLOCK_NOSLEEP();
3036 KASSERT(ifp != NULL, ("%s: ifnet went away", __func__));
3043 ifma = if_findmulti(ifp, sa);
3045 lastref = if_delmulti_locked(ifp, ifma, 0);
3046 IF_ADDR_UNLOCK(ifp);
3051 if (lastref && ifp->if_ioctl != NULL) {
3052 (void)(*ifp->if_ioctl)(ifp, SIOCDELMULTI, 0);
3059 * Delete all multicast group membership for an interface.
3060 * Should be used to quickly flush all multicast filters.
3063 if_delallmulti(struct ifnet *ifp)
3065 struct ifmultiaddr *ifma;
3066 struct ifmultiaddr *next;
3069 TAILQ_FOREACH_SAFE(ifma, &ifp->if_multiaddrs, ifma_link, next)
3070 if_delmulti_locked(ifp, ifma, 0);
3071 IF_ADDR_UNLOCK(ifp);
3075 * Delete a multicast group membership by group membership pointer.
3076 * Network-layer protocol domains must use this routine.
3078 * It is safe to call this routine if the ifp disappeared.
3081 if_delmulti_ifma(struct ifmultiaddr *ifma)
3086 ifp = ifma->ifma_ifp;
3089 printf("%s: ifma_ifp seems to be detached\n", __func__);
3093 IFNET_RLOCK_NOSLEEP();
3094 TAILQ_FOREACH(oifp, &V_ifnet, if_link)
3098 printf("%s: ifnet %p disappeared\n", __func__, ifp);
3101 IFNET_RUNLOCK_NOSLEEP();
3105 * If and only if the ifnet instance exists: Acquire the address lock.
3110 lastref = if_delmulti_locked(ifp, ifma, 0);
3114 * If and only if the ifnet instance exists:
3115 * Release the address lock.
3116 * If the group was left: update the hardware hash filter.
3118 IF_ADDR_UNLOCK(ifp);
3119 if (lastref && ifp->if_ioctl != NULL) {
3120 (void)(*ifp->if_ioctl)(ifp, SIOCDELMULTI, 0);
3126 * Perform deletion of network-layer and/or link-layer multicast address.
3128 * Return 0 if the reference count was decremented.
3129 * Return 1 if the final reference was released, indicating that the
3130 * hardware hash filter should be reprogrammed.
3133 if_delmulti_locked(struct ifnet *ifp, struct ifmultiaddr *ifma, int detaching)
3135 struct ifmultiaddr *ll_ifma;
3137 if (ifp != NULL && ifma->ifma_ifp != NULL) {
3138 KASSERT(ifma->ifma_ifp == ifp,
3139 ("%s: inconsistent ifp %p", __func__, ifp));
3140 IF_ADDR_LOCK_ASSERT(ifp);
3143 ifp = ifma->ifma_ifp;
3146 * If the ifnet is detaching, null out references to ifnet,
3147 * so that upper protocol layers will notice, and not attempt
3148 * to obtain locks for an ifnet which no longer exists. The
3149 * routing socket announcement must happen before the ifnet
3150 * instance is detached from the system.
3154 printf("%s: detaching ifnet instance %p\n", __func__, ifp);
3157 * ifp may already be nulled out if we are being reentered
3158 * to delete the ll_ifma.
3161 rt_newmaddrmsg(RTM_DELMADDR, ifma);
3162 ifma->ifma_ifp = NULL;
3166 if (--ifma->ifma_refcount > 0)
3170 * If this ifma is a network-layer ifma, a link-layer ifma may
3171 * have been associated with it. Release it first if so.
3173 ll_ifma = ifma->ifma_llifma;
3174 if (ll_ifma != NULL) {
3175 KASSERT(ifma->ifma_lladdr != NULL,
3176 ("%s: llifma w/o lladdr", __func__));
3178 ll_ifma->ifma_ifp = NULL; /* XXX */
3179 if (--ll_ifma->ifma_refcount == 0) {
3181 TAILQ_REMOVE(&ifp->if_multiaddrs, ll_ifma,
3184 if_freemulti(ll_ifma);
3189 TAILQ_REMOVE(&ifp->if_multiaddrs, ifma, ifma_link);
3194 * The last reference to this instance of struct ifmultiaddr
3195 * was released; the hardware should be notified of this change.
3201 * Set the link layer address on an interface.
3203 * At this time we only support certain types of interfaces,
3204 * and we don't allow the length of the address to change.
3207 if_setlladdr(struct ifnet *ifp, const u_char *lladdr, int len)
3209 struct sockaddr_dl *sdl;
3216 IF_ADDR_UNLOCK(ifp);
3220 IF_ADDR_UNLOCK(ifp);
3221 sdl = (struct sockaddr_dl *)ifa->ifa_addr;
3226 if (len != sdl->sdl_alen) { /* don't allow length to change */
3230 switch (ifp->if_type) {
3238 case IFT_IEEE8023ADLAG:
3240 bcopy(lladdr, LLADDR(sdl), len);
3249 * If the interface is already up, we need
3250 * to re-init it in order to reprogram its
3253 if ((ifp->if_flags & IFF_UP) != 0) {
3254 if (ifp->if_ioctl) {
3255 ifp->if_flags &= ~IFF_UP;
3256 ifr.ifr_flags = ifp->if_flags & 0xffff;
3257 ifr.ifr_flagshigh = ifp->if_flags >> 16;
3258 (*ifp->if_ioctl)(ifp, SIOCSIFFLAGS, (caddr_t)&ifr);
3259 ifp->if_flags |= IFF_UP;
3260 ifr.ifr_flags = ifp->if_flags & 0xffff;
3261 ifr.ifr_flagshigh = ifp->if_flags >> 16;
3262 (*ifp->if_ioctl)(ifp, SIOCSIFFLAGS, (caddr_t)&ifr);
3266 * Also send gratuitous ARPs to notify other nodes about
3267 * the address change.
3269 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
3270 if (ifa->ifa_addr->sa_family == AF_INET)
3271 arp_ifinit(ifp, ifa);
3279 * The name argument must be a pointer to storage which will last as
3280 * long as the interface does. For physical devices, the result of
3281 * device_get_name(dev) is a good choice and for pseudo-devices a
3282 * static string works well.
3285 if_initname(struct ifnet *ifp, const char *name, int unit)
3287 ifp->if_dname = name;
3288 ifp->if_dunit = unit;
3289 if (unit != IF_DUNIT_NONE)
3290 snprintf(ifp->if_xname, IFNAMSIZ, "%s%d", name, unit);
3292 strlcpy(ifp->if_xname, name, IFNAMSIZ);
3296 if_printf(struct ifnet *ifp, const char * fmt, ...)
3301 retval = printf("%s: ", ifp->if_xname);
3303 retval += vprintf(fmt, ap);
3309 if_start(struct ifnet *ifp)
3312 (*(ifp)->if_start)(ifp);
3316 * Backwards compatibility interface for drivers
3317 * that have not implemented it
3320 if_transmit(struct ifnet *ifp, struct mbuf *m)
3324 IFQ_HANDOFF(ifp, m, error);
3329 if_handoff(struct ifqueue *ifq, struct mbuf *m, struct ifnet *ifp, int adjust)
3334 if (_IF_QFULL(ifq)) {
3341 ifp->if_obytes += m->m_pkthdr.len + adjust;
3342 if (m->m_flags & (M_BCAST|M_MCAST))
3344 active = ifp->if_drv_flags & IFF_DRV_OACTIVE;
3346 _IF_ENQUEUE(ifq, m);
3348 if (ifp != NULL && !active)
3349 (*(ifp)->if_start)(ifp);
3354 if_register_com_alloc(u_char type,
3355 if_com_alloc_t *a, if_com_free_t *f)
3358 KASSERT(if_com_alloc[type] == NULL,
3359 ("if_register_com_alloc: %d already registered", type));
3360 KASSERT(if_com_free[type] == NULL,
3361 ("if_register_com_alloc: %d free already registered", type));
3363 if_com_alloc[type] = a;
3364 if_com_free[type] = f;
3368 if_deregister_com_alloc(u_char type)
3371 KASSERT(if_com_alloc[type] != NULL,
3372 ("if_deregister_com_alloc: %d not registered", type));
3373 KASSERT(if_com_free[type] != NULL,
3374 ("if_deregister_com_alloc: %d free not registered", type));
3375 if_com_alloc[type] = NULL;
3376 if_com_free[type] = NULL;