2 * Copyright (c) 1980, 1986, 1993
3 * The Regents of the University of California. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 4. Neither the name of the University nor the names of its contributors
14 * may be used to endorse or promote products derived from this software
15 * without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * @(#)if.c 8.5 (Berkeley) 1/9/95
33 #include "opt_compat.h"
34 #include "opt_inet6.h"
37 #include <sys/param.h>
38 #include <sys/types.h>
40 #include <sys/malloc.h>
44 #include <sys/systm.h>
47 #include <sys/socket.h>
48 #include <sys/socketvar.h>
49 #include <sys/protosw.h>
50 #include <sys/kernel.h>
52 #include <sys/refcount.h>
53 #include <sys/module.h>
54 #include <sys/rwlock.h>
55 #include <sys/sockio.h>
56 #include <sys/syslog.h>
57 #include <sys/sysctl.h>
58 #include <sys/taskqueue.h>
59 #include <sys/domain.h>
63 #include <machine/stdarg.h>
67 #include <net/if_arp.h>
68 #include <net/if_clone.h>
69 #include <net/if_dl.h>
70 #include <net/if_types.h>
71 #include <net/if_var.h>
72 #include <net/radix.h>
73 #include <net/route.h>
76 #if defined(INET) || defined(INET6)
78 #include <netinet/in.h>
79 #include <netinet/in_var.h>
80 #include <netinet/ip_carp.h>
82 #include <netinet6/in6_var.h>
83 #include <netinet6/in6_ifattach.h>
87 #include <netinet/if_ether.h>
90 #include <security/mac/mac_framework.h>
92 #ifdef COMPAT_FREEBSD32
93 #include <sys/mount.h>
94 #include <compat/freebsd32/freebsd32.h>
97 struct ifindex_entry {
98 struct ifnet *ife_ifnet;
101 static int slowtimo_started;
103 SYSCTL_NODE(_net, PF_LINK, link, CTLFLAG_RW, 0, "Link layers");
104 SYSCTL_NODE(_net_link, 0, generic, CTLFLAG_RW, 0, "Generic link-management");
106 TUNABLE_INT("net.link.ifqmaxlen", &ifqmaxlen);
107 SYSCTL_UINT(_net_link, OID_AUTO, ifqmaxlen, CTLFLAG_RDTUN,
108 &ifqmaxlen, 0, "max send queue size");
110 /* Log link state change events */
111 static int log_link_state_change = 1;
113 SYSCTL_INT(_net_link, OID_AUTO, log_link_state_change, CTLFLAG_RW,
114 &log_link_state_change, 0,
115 "log interface link state change events");
117 /* Interface description */
118 static unsigned int ifdescr_maxlen = 1024;
119 SYSCTL_UINT(_net, OID_AUTO, ifdescr_maxlen, CTLFLAG_RW,
121 "administrative maximum length for interface description");
123 MALLOC_DEFINE(M_IFDESCR, "ifdescr", "ifnet descriptions");
125 /* global sx for non-critical path ifdescr */
126 static struct sx ifdescr_sx;
127 SX_SYSINIT(ifdescr_sx, &ifdescr_sx, "ifnet descr");
129 void (*bstp_linkstate_p)(struct ifnet *ifp, int state);
130 void (*ng_ether_link_state_p)(struct ifnet *ifp, int state);
131 void (*lagg_linkstate_p)(struct ifnet *ifp, int state);
132 /* These are external hooks for CARP. */
133 void (*carp_linkstate_p)(struct ifnet *ifp);
134 #if defined(INET) || defined(INET6)
135 struct ifnet *(*carp_forus_p)(struct ifnet *ifp, u_char *dhost);
136 int (*carp_output_p)(struct ifnet *ifp, struct mbuf *m,
137 struct sockaddr *sa, struct rtentry *rt);
140 int (*carp_iamatch_p)(struct ifnet *, struct in_ifaddr *, struct in_addr *,
144 struct ifaddr *(*carp_iamatch6_p)(struct ifnet *ifp, struct in6_addr *taddr6);
145 caddr_t (*carp_macmatch6_p)(struct ifnet *ifp, struct mbuf *m,
146 const struct in6_addr *taddr);
149 struct mbuf *(*tbr_dequeue_ptr)(struct ifaltq *, int) = NULL;
152 * XXX: Style; these should be sorted alphabetically, and unprototyped
153 * static functions should be prototyped. Currently they are sorted by
156 static void if_attachdomain(void *);
157 static void if_attachdomain1(struct ifnet *);
158 static int ifconf(u_long, caddr_t);
159 static void if_freemulti(struct ifmultiaddr *);
160 static void if_init(void *);
161 static void if_grow(void);
162 static void if_check(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 void if_slowtimo(void *);
166 static int if_transmit(struct ifnet *ifp, struct mbuf *m);
167 static void if_unroute(struct ifnet *, int flag, int fam);
168 static void link_rtrequest(int, struct rtentry *, struct rt_addrinfo *);
169 static int if_rtdel(struct radix_node *, void *);
170 static int ifhwioctl(u_long, struct ifnet *, caddr_t, struct thread *);
171 static int if_delmulti_locked(struct ifnet *, struct ifmultiaddr *, int);
172 static void do_link_state_change(void *, int);
173 static int if_getgroup(struct ifgroupreq *, struct ifnet *);
174 static int if_getgroupmembers(struct ifgroupreq *);
175 static void if_delgroups(struct ifnet *);
176 static void if_attach_internal(struct ifnet *, int);
177 static void if_detach_internal(struct ifnet *, int);
181 * XXX: declare here to avoid to include many inet6 related files..
182 * should be more generalized?
184 extern void nd6_setmtu(struct ifnet *);
187 VNET_DEFINE(int, if_index);
188 int ifqmaxlen = IFQ_MAXLEN;
189 VNET_DEFINE(struct ifnethead, ifnet); /* depend on static init XXX */
190 VNET_DEFINE(struct ifgrouphead, ifg_head);
192 static VNET_DEFINE(int, if_indexlim) = 8;
194 /* Table of ifnet by index. */
195 VNET_DEFINE(struct ifindex_entry *, ifindex_table);
197 #define V_if_indexlim VNET(if_indexlim)
198 #define V_ifindex_table VNET(ifindex_table)
201 * The global network interface list (V_ifnet) and related state (such as
202 * if_index, if_indexlim, and ifindex_table) are protected by an sxlock and
203 * an rwlock. Either may be acquired shared to stablize the list, but both
204 * must be acquired writable to modify the list. This model allows us to
205 * both stablize the interface list during interrupt thread processing, but
206 * also to stablize it over long-running ioctls, without introducing priority
207 * inversions and deadlocks.
209 struct rwlock ifnet_rwlock;
210 struct sx ifnet_sxlock;
213 * The allocation of network interfaces is a rather non-atomic affair; we
214 * need to select an index before we are ready to expose the interface for
215 * use, so will use this pointer value to indicate reservation.
217 #define IFNET_HOLD (void *)(uintptr_t)(-1)
219 static if_com_alloc_t *if_com_alloc[256];
220 static if_com_free_t *if_com_free[256];
223 * System initialization
225 SYSINIT(interface_check, SI_SUB_PROTO_IF, SI_ORDER_FIRST, if_check, NULL);
227 MALLOC_DEFINE(M_IFNET, "ifnet", "interface internals");
228 MALLOC_DEFINE(M_IFADDR, "ifaddr", "interface address");
229 MALLOC_DEFINE(M_IFMADDR, "ether_multi", "link-level multicast address");
232 ifnet_byindex_locked(u_short idx)
235 if (idx > V_if_index)
237 if (V_ifindex_table[idx].ife_ifnet == IFNET_HOLD)
239 return (V_ifindex_table[idx].ife_ifnet);
243 ifnet_byindex(u_short idx)
247 IFNET_RLOCK_NOSLEEP();
248 ifp = ifnet_byindex_locked(idx);
249 IFNET_RUNLOCK_NOSLEEP();
254 ifnet_byindex_ref(u_short idx)
258 IFNET_RLOCK_NOSLEEP();
259 ifp = ifnet_byindex_locked(idx);
260 if (ifp == NULL || (ifp->if_flags & IFF_DYING)) {
261 IFNET_RUNLOCK_NOSLEEP();
265 IFNET_RUNLOCK_NOSLEEP();
270 * Allocate an ifindex array entry; return 0 on success or an error on
274 ifindex_alloc_locked(u_short *idxp)
278 IFNET_WLOCK_ASSERT();
282 * Try to find an empty slot below V_if_index. If we fail, take the
285 for (idx = 1; idx <= V_if_index; idx++) {
286 if (V_ifindex_table[idx].ife_ifnet == NULL)
290 /* Catch if_index overflow. */
293 if (idx >= V_if_indexlim) {
297 if (idx > V_if_index)
304 ifindex_free_locked(u_short idx)
307 IFNET_WLOCK_ASSERT();
309 V_ifindex_table[idx].ife_ifnet = NULL;
310 while (V_if_index > 0 &&
311 V_ifindex_table[V_if_index].ife_ifnet == NULL)
316 ifindex_free(u_short idx)
320 ifindex_free_locked(idx);
325 ifnet_setbyindex_locked(u_short idx, struct ifnet *ifp)
328 IFNET_WLOCK_ASSERT();
330 V_ifindex_table[idx].ife_ifnet = ifp;
334 ifnet_setbyindex(u_short idx, struct ifnet *ifp)
338 ifnet_setbyindex_locked(idx, ifp);
343 ifaddr_byindex(u_short idx)
347 IFNET_RLOCK_NOSLEEP();
348 ifa = ifnet_byindex_locked(idx)->if_addr;
351 IFNET_RUNLOCK_NOSLEEP();
356 * Network interface utility routines.
358 * Routines with ifa_ifwith* names take sockaddr *'s as
363 vnet_if_init(const void *unused __unused)
366 TAILQ_INIT(&V_ifnet);
367 TAILQ_INIT(&V_ifg_head);
369 if_grow(); /* create initial table */
371 vnet_if_clone_init();
373 VNET_SYSINIT(vnet_if_init, SI_SUB_INIT_IF, SI_ORDER_SECOND, vnet_if_init,
378 if_init(void *dummy __unused)
384 SYSINIT(interfaces, SI_SUB_INIT_IF, SI_ORDER_FIRST, if_init, NULL);
389 vnet_if_uninit(const void *unused __unused)
392 VNET_ASSERT(TAILQ_EMPTY(&V_ifnet), ("%s:%d tailq &V_ifnet=%p "
393 "not empty", __func__, __LINE__, &V_ifnet));
394 VNET_ASSERT(TAILQ_EMPTY(&V_ifg_head), ("%s:%d tailq &V_ifg_head=%p "
395 "not empty", __func__, __LINE__, &V_ifg_head));
397 free((caddr_t)V_ifindex_table, M_IFNET);
399 VNET_SYSUNINIT(vnet_if_uninit, SI_SUB_INIT_IF, SI_ORDER_FIRST,
400 vnet_if_uninit, NULL);
408 struct ifindex_entry *e;
410 IFNET_WLOCK_ASSERT();
411 oldlim = V_if_indexlim;
413 n = (oldlim << 1) * sizeof(*e);
414 e = malloc(n, M_IFNET, M_WAITOK | M_ZERO);
416 if (V_if_indexlim != oldlim) {
420 if (V_ifindex_table != NULL) {
421 memcpy((caddr_t)e, (caddr_t)V_ifindex_table, n/2);
422 free((caddr_t)V_ifindex_table, M_IFNET);
429 if_check(void *dummy __unused)
433 * If at least one interface added during boot uses
434 * if_watchdog then start the timer.
436 if (slowtimo_started)
441 * Allocate a struct ifnet and an index for an interface. A layer 2
442 * common structure will also be allocated if an allocation routine is
443 * registered for the passed type.
446 if_alloc(u_char type)
451 ifp = malloc(sizeof(struct ifnet), M_IFNET, M_WAITOK|M_ZERO);
453 if (ifindex_alloc_locked(&idx) != 0) {
458 ifnet_setbyindex_locked(idx, IFNET_HOLD);
462 ifp->if_alloctype = type;
463 if (if_com_alloc[type] != NULL) {
464 ifp->if_l2com = if_com_alloc[type](type, ifp);
465 if (ifp->if_l2com == NULL) {
472 IF_ADDR_LOCK_INIT(ifp);
473 TASK_INIT(&ifp->if_linktask, 0, do_link_state_change, ifp);
474 ifp->if_afdata_initialized = 0;
475 IF_AFDATA_LOCK_INIT(ifp);
476 TAILQ_INIT(&ifp->if_addrhead);
477 TAILQ_INIT(&ifp->if_prefixhead);
478 TAILQ_INIT(&ifp->if_multiaddrs);
479 TAILQ_INIT(&ifp->if_groups);
483 ifq_init(&ifp->if_snd, ifp);
485 refcount_init(&ifp->if_refcount, 1); /* Index reference. */
486 ifnet_setbyindex(ifp->if_index, ifp);
491 * Do the actual work of freeing a struct ifnet, and layer 2 common
492 * structure. This call is made when the last reference to an
493 * interface is released.
496 if_free_internal(struct ifnet *ifp)
499 KASSERT((ifp->if_flags & IFF_DYING),
500 ("if_free_internal: interface not dying"));
502 if (if_com_free[ifp->if_alloctype] != NULL)
503 if_com_free[ifp->if_alloctype](ifp->if_l2com,
507 mac_ifnet_destroy(ifp);
509 if (ifp->if_description != NULL)
510 free(ifp->if_description, M_IFDESCR);
511 IF_AFDATA_DESTROY(ifp);
512 IF_ADDR_LOCK_DESTROY(ifp);
513 ifq_delete(&ifp->if_snd);
518 * This version should only be called by intefaces that switch their type
519 * after calling if_alloc(). if_free_type() will go away again now that we
520 * have if_alloctype to cache the original allocation type. For now, assert
521 * that they match, since we require that in practice.
524 if_free_type(struct ifnet *ifp, u_char type)
527 KASSERT(ifp->if_alloctype == type,
528 ("if_free_type: type (%d) != alloctype (%d)", type,
531 ifp->if_flags |= IFF_DYING; /* XXX: Locking */
534 KASSERT(ifp == ifnet_byindex_locked(ifp->if_index),
535 ("%s: freeing unallocated ifnet", ifp->if_xname));
537 ifindex_free_locked(ifp->if_index);
540 if (!refcount_release(&ifp->if_refcount))
542 if_free_internal(ifp);
546 * This is the normal version of if_free(), used by device drivers to free a
547 * detached network interface. The contents of if_free_type() will move into
548 * here when if_free_type() goes away.
551 if_free(struct ifnet *ifp)
554 if_free_type(ifp, ifp->if_alloctype);
558 * Interfaces to keep an ifnet type-stable despite the possibility of the
559 * driver calling if_free(). If there are additional references, we defer
560 * freeing the underlying data structure.
563 if_ref(struct ifnet *ifp)
566 /* We don't assert the ifnet list lock here, but arguably should. */
567 refcount_acquire(&ifp->if_refcount);
571 if_rele(struct ifnet *ifp)
574 if (!refcount_release(&ifp->if_refcount))
576 if_free_internal(ifp);
580 ifq_init(struct ifaltq *ifq, struct ifnet *ifp)
583 mtx_init(&ifq->ifq_mtx, ifp->if_xname, "if send queue", MTX_DEF);
585 if (ifq->ifq_maxlen == 0)
586 ifq->ifq_maxlen = ifqmaxlen;
589 ifq->altq_disc = NULL;
590 ifq->altq_flags &= ALTQF_CANTCHANGE;
591 ifq->altq_tbr = NULL;
596 ifq_delete(struct ifaltq *ifq)
598 mtx_destroy(&ifq->ifq_mtx);
602 * Perform generic interface initalization tasks and attach the interface
603 * to the list of "active" interfaces. If vmove flag is set on entry
604 * to if_attach_internal(), perform only a limited subset of initialization
605 * tasks, given that we are moving from one vnet to another an ifnet which
606 * has already been fully initialized.
609 * - The decision to return void and thus require this function to
610 * succeed is questionable.
611 * - We should probably do more sanity checking. For instance we don't
612 * do anything to insure if_xname is unique or non-empty.
615 if_attach(struct ifnet *ifp)
618 if_attach_internal(ifp, 0);
622 if_attach_internal(struct ifnet *ifp, int vmove)
624 unsigned socksize, ifasize;
625 int namelen, masklen;
626 struct sockaddr_dl *sdl;
629 if (ifp->if_index == 0 || ifp != ifnet_byindex(ifp->if_index))
630 panic ("%s: BUG: if_attach called without if_alloc'd input()\n",
634 ifp->if_vnet = curvnet;
635 if (ifp->if_home_vnet == NULL)
636 ifp->if_home_vnet = curvnet;
639 if_addgroup(ifp, IFG_ALL);
641 getmicrotime(&ifp->if_lastchange);
642 ifp->if_data.ifi_epoch = time_uptime;
643 ifp->if_data.ifi_datalen = sizeof(struct if_data);
645 KASSERT((ifp->if_transmit == NULL && ifp->if_qflush == NULL) ||
646 (ifp->if_transmit != NULL && ifp->if_qflush != NULL),
647 ("transmit and qflush must both either be set or both be NULL"));
648 if (ifp->if_transmit == NULL) {
649 ifp->if_transmit = if_transmit;
650 ifp->if_qflush = if_qflush;
655 mac_ifnet_create(ifp);
659 * Create a Link Level name for this device.
661 namelen = strlen(ifp->if_xname);
663 * Always save enough space for any possiable name so we
664 * can do a rename in place later.
666 masklen = offsetof(struct sockaddr_dl, sdl_data[0]) + IFNAMSIZ;
667 socksize = masklen + ifp->if_addrlen;
668 if (socksize < sizeof(*sdl))
669 socksize = sizeof(*sdl);
670 socksize = roundup2(socksize, sizeof(long));
671 ifasize = sizeof(*ifa) + 2 * socksize;
672 ifa = malloc(ifasize, M_IFADDR, M_WAITOK | M_ZERO);
674 sdl = (struct sockaddr_dl *)(ifa + 1);
675 sdl->sdl_len = socksize;
676 sdl->sdl_family = AF_LINK;
677 bcopy(ifp->if_xname, sdl->sdl_data, namelen);
678 sdl->sdl_nlen = namelen;
679 sdl->sdl_index = ifp->if_index;
680 sdl->sdl_type = ifp->if_type;
683 ifa->ifa_rtrequest = link_rtrequest;
684 ifa->ifa_addr = (struct sockaddr *)sdl;
685 sdl = (struct sockaddr_dl *)(socksize + (caddr_t)sdl);
686 ifa->ifa_netmask = (struct sockaddr *)sdl;
687 sdl->sdl_len = masklen;
689 sdl->sdl_data[--namelen] = 0xff;
690 TAILQ_INSERT_HEAD(&ifp->if_addrhead, ifa, ifa_link);
691 /* Reliably crash if used uninitialized. */
692 ifp->if_broadcastaddr = NULL;
697 * Update the interface index in the link layer address
700 for (ifa = ifp->if_addr; ifa != NULL;
701 ifa = TAILQ_NEXT(ifa, ifa_link)) {
702 if (ifa->ifa_addr->sa_family == AF_LINK) {
703 sdl = (struct sockaddr_dl *)ifa->ifa_addr;
704 sdl->sdl_index = ifp->if_index;
711 TAILQ_INSERT_TAIL(&V_ifnet, ifp, if_link);
713 curvnet->vnet_ifcnt++;
717 if (domain_init_status >= 2)
718 if_attachdomain1(ifp);
720 EVENTHANDLER_INVOKE(ifnet_arrival_event, ifp);
721 if (IS_DEFAULT_VNET(curvnet))
722 devctl_notify("IFNET", ifp->if_xname, "ATTACH", NULL);
724 /* Announce the interface. */
725 rt_ifannouncemsg(ifp, IFAN_ARRIVAL);
727 if (!vmove && ifp->if_watchdog != NULL) {
729 "WARNING: using obsoleted if_watchdog interface\n");
732 * Note that we need if_slowtimo(). If this happens after
733 * boot, then call if_slowtimo() directly.
735 if (atomic_cmpset_int(&slowtimo_started, 0, 1) && !cold)
741 if_attachdomain(void *dummy)
747 TAILQ_FOREACH(ifp, &V_ifnet, if_link)
748 if_attachdomain1(ifp);
751 SYSINIT(domainifattach, SI_SUB_PROTO_IFATTACHDOMAIN, SI_ORDER_SECOND,
752 if_attachdomain, NULL);
755 if_attachdomain1(struct ifnet *ifp)
763 * Since dp->dom_ifattach calls malloc() with M_WAITOK, we
764 * cannot lock ifp->if_afdata initialization, entirely.
766 if (IF_AFDATA_TRYLOCK(ifp) == 0) {
770 if (ifp->if_afdata_initialized >= domain_init_status) {
771 IF_AFDATA_UNLOCK(ifp);
773 printf("if_attachdomain called more than once on %s\n",
777 ifp->if_afdata_initialized = domain_init_status;
778 IF_AFDATA_UNLOCK(ifp);
780 /* address family dependent data region */
781 bzero(ifp->if_afdata, sizeof(ifp->if_afdata));
782 for (dp = domains; dp; dp = dp->dom_next) {
783 if (dp->dom_ifattach)
784 ifp->if_afdata[dp->dom_family] =
785 (*dp->dom_ifattach)(ifp);
792 * Remove any unicast or broadcast network addresses from an interface.
795 if_purgeaddrs(struct ifnet *ifp)
797 struct ifaddr *ifa, *next;
799 TAILQ_FOREACH_SAFE(ifa, &ifp->if_addrhead, ifa_link, next) {
800 if (ifa->ifa_addr->sa_family == AF_LINK)
803 /* XXX: Ugly!! ad hoc just for INET */
804 if (ifa->ifa_addr->sa_family == AF_INET) {
805 struct ifaliasreq ifr;
807 bzero(&ifr, sizeof(ifr));
808 ifr.ifra_addr = *ifa->ifa_addr;
809 if (ifa->ifa_dstaddr)
810 ifr.ifra_broadaddr = *ifa->ifa_dstaddr;
811 if (in_control(NULL, SIOCDIFADDR, (caddr_t)&ifr, ifp,
817 if (ifa->ifa_addr->sa_family == AF_INET6) {
819 /* ifp_addrhead is already updated */
823 TAILQ_REMOVE(&ifp->if_addrhead, ifa, ifa_link);
829 * Remove any multicast network addresses from an interface when an ifnet
833 if_purgemaddrs(struct ifnet *ifp)
835 struct ifmultiaddr *ifma;
836 struct ifmultiaddr *next;
839 TAILQ_FOREACH_SAFE(ifma, &ifp->if_multiaddrs, ifma_link, next)
840 if_delmulti_locked(ifp, ifma, 1);
841 IF_ADDR_WUNLOCK(ifp);
845 * Detach an interface, removing it from the list of "active" interfaces.
846 * If vmove flag is set on entry to if_detach_internal(), perform only a
847 * limited subset of cleanup tasks, given that we are moving an ifnet from
848 * one vnet to another, where it must be fully operational.
850 * XXXRW: There are some significant questions about event ordering, and
851 * how to prevent things from starting to use the interface during detach.
854 if_detach(struct ifnet *ifp)
857 if_detach_internal(ifp, 0);
861 if_detach_internal(struct ifnet *ifp, int vmove)
864 struct radix_node_head *rnh;
871 TAILQ_FOREACH(iter, &V_ifnet, if_link)
873 TAILQ_REMOVE(&V_ifnet, ifp, if_link);
879 curvnet->vnet_ifcnt--;
884 panic("%s: ifp=%p not on the ifnet tailq %p",
885 __func__, ifp, &V_ifnet);
887 return; /* XXX this should panic as well? */
891 * Remove/wait for pending events.
893 taskqueue_drain(taskqueue_swi, &ifp->if_linktask);
896 * Remove routes and flush queues.
900 if (ALTQ_IS_ENABLED(&ifp->if_snd))
901 altq_disable(&ifp->if_snd);
902 if (ALTQ_IS_ATTACHED(&ifp->if_snd))
903 altq_detach(&ifp->if_snd);
914 * Remove all IPv6 kernel structs related to ifp. This should be done
915 * before removing routing entries below, since IPv6 interface direct
916 * routes are expected to be removed by the IPv6-specific kernel API.
917 * Otherwise, the kernel will detect some inconsistency and bark it.
925 * Prevent further calls into the device driver via ifnet.
930 * Remove link ifaddr pointer and maybe decrement if_index.
931 * Clean up all addresses.
935 /* We can now free link ifaddr. */
936 if (!TAILQ_EMPTY(&ifp->if_addrhead)) {
937 ifa = TAILQ_FIRST(&ifp->if_addrhead);
938 TAILQ_REMOVE(&ifp->if_addrhead, ifa, ifa_link);
944 * Delete all remaining routes using this interface
945 * Unfortuneatly the only way to do this is to slog through
946 * the entire routing table looking for routes which point
947 * to this interface...oh well...
949 for (i = 1; i <= AF_MAX; i++) {
950 for (j = 0; j < rt_numfibs; j++) {
951 rnh = rt_tables_get_rnh(j, i);
954 RADIX_NODE_HEAD_LOCK(rnh);
955 (void) rnh->rnh_walktree(rnh, if_rtdel, ifp);
956 RADIX_NODE_HEAD_UNLOCK(rnh);
960 /* Announce that the interface is gone. */
961 rt_ifannouncemsg(ifp, IFAN_DEPARTURE);
962 EVENTHANDLER_INVOKE(ifnet_departure_event, ifp);
963 if (IS_DEFAULT_VNET(curvnet))
964 devctl_notify("IFNET", ifp->if_xname, "DETACH", NULL);
968 * We cannot hold the lock over dom_ifdetach calls as they might
969 * sleep, for example trying to drain a callout, thus open up the
970 * theoretical race with re-attaching.
973 i = ifp->if_afdata_initialized;
974 ifp->if_afdata_initialized = 0;
975 IF_AFDATA_UNLOCK(ifp);
976 for (dp = domains; i > 0 && dp; dp = dp->dom_next) {
977 if (dp->dom_ifdetach && ifp->if_afdata[dp->dom_family])
978 (*dp->dom_ifdetach)(ifp,
979 ifp->if_afdata[dp->dom_family]);
985 * if_vmove() performs a limited version of if_detach() in current
986 * vnet and if_attach()es the ifnet to the vnet specified as 2nd arg.
987 * An attempt is made to shrink if_index in current vnet, find an
988 * unused if_index in target vnet and calls if_grow() if necessary,
989 * and finally find an unused if_xname for the target vnet.
992 if_vmove(struct ifnet *ifp, struct vnet *new_vnet)
997 * Detach from current vnet, but preserve LLADDR info, do not
998 * mark as dead etc. so that the ifnet can be reattached later.
1000 if_detach_internal(ifp, 1);
1003 * Unlink the ifnet from ifindex_table[] in current vnet, and shrink
1004 * the if_index for that vnet if possible.
1006 * NOTE: IFNET_WLOCK/IFNET_WUNLOCK() are assumed to be unvirtualized,
1007 * or we'd lock on one vnet and unlock on another.
1010 ifindex_free_locked(ifp->if_index);
1014 * Perform interface-specific reassignment tasks, if provided by
1017 if (ifp->if_reassign != NULL)
1018 ifp->if_reassign(ifp, new_vnet, NULL);
1021 * Switch to the context of the target vnet.
1023 CURVNET_SET_QUIET(new_vnet);
1026 if (ifindex_alloc_locked(&idx) != 0) {
1028 panic("if_index overflow");
1030 ifp->if_index = idx;
1031 ifnet_setbyindex_locked(ifp->if_index, ifp);
1034 if_attach_internal(ifp, 1);
1040 * Move an ifnet to or from another child prison/vnet, specified by the jail id.
1043 if_vmove_loan(struct thread *td, struct ifnet *ifp, char *ifname, int jid)
1048 /* Try to find the prison within our visibility. */
1049 sx_slock(&allprison_lock);
1050 pr = prison_find_child(td->td_ucred->cr_prison, jid);
1051 sx_sunlock(&allprison_lock);
1054 prison_hold_locked(pr);
1055 mtx_unlock(&pr->pr_mtx);
1057 /* Do not try to move the iface from and to the same prison. */
1058 if (pr->pr_vnet == ifp->if_vnet) {
1063 /* Make sure the named iface does not exists in the dst. prison/vnet. */
1064 /* XXX Lock interfaces to avoid races. */
1065 CURVNET_SET_QUIET(pr->pr_vnet);
1066 difp = ifunit(ifname);
1073 /* Move the interface into the child jail/vnet. */
1074 if_vmove(ifp, pr->pr_vnet);
1076 /* Report the new if_xname back to the userland. */
1077 sprintf(ifname, "%s", ifp->if_xname);
1084 if_vmove_reclaim(struct thread *td, char *ifname, int jid)
1087 struct vnet *vnet_dst;
1090 /* Try to find the prison within our visibility. */
1091 sx_slock(&allprison_lock);
1092 pr = prison_find_child(td->td_ucred->cr_prison, jid);
1093 sx_sunlock(&allprison_lock);
1096 prison_hold_locked(pr);
1097 mtx_unlock(&pr->pr_mtx);
1099 /* Make sure the named iface exists in the source prison/vnet. */
1100 CURVNET_SET(pr->pr_vnet);
1101 ifp = ifunit(ifname); /* XXX Lock to avoid races. */
1108 /* Do not try to move the iface from and to the same prison. */
1109 vnet_dst = TD_TO_VNET(td);
1110 if (vnet_dst == ifp->if_vnet) {
1116 /* Get interface back from child jail/vnet. */
1117 if_vmove(ifp, vnet_dst);
1120 /* Report the new if_xname back to the userland. */
1121 sprintf(ifname, "%s", ifp->if_xname);
1129 * Add a group to an interface
1132 if_addgroup(struct ifnet *ifp, const char *groupname)
1134 struct ifg_list *ifgl;
1135 struct ifg_group *ifg = NULL;
1136 struct ifg_member *ifgm;
1138 if (groupname[0] && groupname[strlen(groupname) - 1] >= '0' &&
1139 groupname[strlen(groupname) - 1] <= '9')
1143 TAILQ_FOREACH(ifgl, &ifp->if_groups, ifgl_next)
1144 if (!strcmp(ifgl->ifgl_group->ifg_group, groupname)) {
1149 if ((ifgl = (struct ifg_list *)malloc(sizeof(struct ifg_list), M_TEMP,
1150 M_NOWAIT)) == NULL) {
1155 if ((ifgm = (struct ifg_member *)malloc(sizeof(struct ifg_member),
1156 M_TEMP, M_NOWAIT)) == NULL) {
1162 TAILQ_FOREACH(ifg, &V_ifg_head, ifg_next)
1163 if (!strcmp(ifg->ifg_group, groupname))
1167 if ((ifg = (struct ifg_group *)malloc(sizeof(struct ifg_group),
1168 M_TEMP, M_NOWAIT)) == NULL) {
1174 strlcpy(ifg->ifg_group, groupname, sizeof(ifg->ifg_group));
1175 ifg->ifg_refcnt = 0;
1176 TAILQ_INIT(&ifg->ifg_members);
1177 EVENTHANDLER_INVOKE(group_attach_event, ifg);
1178 TAILQ_INSERT_TAIL(&V_ifg_head, ifg, ifg_next);
1182 ifgl->ifgl_group = ifg;
1183 ifgm->ifgm_ifp = ifp;
1186 TAILQ_INSERT_TAIL(&ifg->ifg_members, ifgm, ifgm_next);
1187 TAILQ_INSERT_TAIL(&ifp->if_groups, ifgl, ifgl_next);
1188 IF_ADDR_WUNLOCK(ifp);
1192 EVENTHANDLER_INVOKE(group_change_event, groupname);
1198 * Remove a group from an interface
1201 if_delgroup(struct ifnet *ifp, const char *groupname)
1203 struct ifg_list *ifgl;
1204 struct ifg_member *ifgm;
1207 TAILQ_FOREACH(ifgl, &ifp->if_groups, ifgl_next)
1208 if (!strcmp(ifgl->ifgl_group->ifg_group, groupname))
1216 TAILQ_REMOVE(&ifp->if_groups, ifgl, ifgl_next);
1217 IF_ADDR_WUNLOCK(ifp);
1219 TAILQ_FOREACH(ifgm, &ifgl->ifgl_group->ifg_members, ifgm_next)
1220 if (ifgm->ifgm_ifp == ifp)
1224 TAILQ_REMOVE(&ifgl->ifgl_group->ifg_members, ifgm, ifgm_next);
1228 if (--ifgl->ifgl_group->ifg_refcnt == 0) {
1229 TAILQ_REMOVE(&V_ifg_head, ifgl->ifgl_group, ifg_next);
1230 EVENTHANDLER_INVOKE(group_detach_event, ifgl->ifgl_group);
1231 free(ifgl->ifgl_group, M_TEMP);
1237 EVENTHANDLER_INVOKE(group_change_event, groupname);
1243 * Remove an interface from all groups
1246 if_delgroups(struct ifnet *ifp)
1248 struct ifg_list *ifgl;
1249 struct ifg_member *ifgm;
1250 char groupname[IFNAMSIZ];
1253 while (!TAILQ_EMPTY(&ifp->if_groups)) {
1254 ifgl = TAILQ_FIRST(&ifp->if_groups);
1256 strlcpy(groupname, ifgl->ifgl_group->ifg_group, IFNAMSIZ);
1259 TAILQ_REMOVE(&ifp->if_groups, ifgl, ifgl_next);
1260 IF_ADDR_WUNLOCK(ifp);
1262 TAILQ_FOREACH(ifgm, &ifgl->ifgl_group->ifg_members, ifgm_next)
1263 if (ifgm->ifgm_ifp == ifp)
1267 TAILQ_REMOVE(&ifgl->ifgl_group->ifg_members, ifgm,
1272 if (--ifgl->ifgl_group->ifg_refcnt == 0) {
1273 TAILQ_REMOVE(&V_ifg_head, ifgl->ifgl_group, ifg_next);
1274 EVENTHANDLER_INVOKE(group_detach_event,
1276 free(ifgl->ifgl_group, M_TEMP);
1282 EVENTHANDLER_INVOKE(group_change_event, groupname);
1290 * Stores all groups from an interface in memory pointed
1294 if_getgroup(struct ifgroupreq *data, struct ifnet *ifp)
1297 struct ifg_list *ifgl;
1298 struct ifg_req ifgrq, *ifgp;
1299 struct ifgroupreq *ifgr = data;
1301 if (ifgr->ifgr_len == 0) {
1303 TAILQ_FOREACH(ifgl, &ifp->if_groups, ifgl_next)
1304 ifgr->ifgr_len += sizeof(struct ifg_req);
1305 IF_ADDR_RUNLOCK(ifp);
1309 len = ifgr->ifgr_len;
1310 ifgp = ifgr->ifgr_groups;
1313 TAILQ_FOREACH(ifgl, &ifp->if_groups, ifgl_next) {
1314 if (len < sizeof(ifgrq)) {
1315 IF_ADDR_RUNLOCK(ifp);
1318 bzero(&ifgrq, sizeof ifgrq);
1319 strlcpy(ifgrq.ifgrq_group, ifgl->ifgl_group->ifg_group,
1320 sizeof(ifgrq.ifgrq_group));
1321 if ((error = copyout(&ifgrq, ifgp, sizeof(struct ifg_req)))) {
1322 IF_ADDR_RUNLOCK(ifp);
1325 len -= sizeof(ifgrq);
1328 IF_ADDR_RUNLOCK(ifp);
1334 * Stores all members of a group in memory pointed to by data
1337 if_getgroupmembers(struct ifgroupreq *data)
1339 struct ifgroupreq *ifgr = data;
1340 struct ifg_group *ifg;
1341 struct ifg_member *ifgm;
1342 struct ifg_req ifgrq, *ifgp;
1346 TAILQ_FOREACH(ifg, &V_ifg_head, ifg_next)
1347 if (!strcmp(ifg->ifg_group, ifgr->ifgr_name))
1354 if (ifgr->ifgr_len == 0) {
1355 TAILQ_FOREACH(ifgm, &ifg->ifg_members, ifgm_next)
1356 ifgr->ifgr_len += sizeof(ifgrq);
1361 len = ifgr->ifgr_len;
1362 ifgp = ifgr->ifgr_groups;
1363 TAILQ_FOREACH(ifgm, &ifg->ifg_members, ifgm_next) {
1364 if (len < sizeof(ifgrq)) {
1368 bzero(&ifgrq, sizeof ifgrq);
1369 strlcpy(ifgrq.ifgrq_member, ifgm->ifgm_ifp->if_xname,
1370 sizeof(ifgrq.ifgrq_member));
1371 if ((error = copyout(&ifgrq, ifgp, sizeof(struct ifg_req)))) {
1375 len -= sizeof(ifgrq);
1384 * Delete Routes for a Network Interface
1386 * Called for each routing entry via the rnh->rnh_walktree() call above
1387 * to delete all route entries referencing a detaching network interface.
1390 * rn pointer to node in the routing table
1391 * arg argument passed to rnh->rnh_walktree() - detaching interface
1395 * errno failed - reason indicated
1399 if_rtdel(struct radix_node *rn, void *arg)
1401 struct rtentry *rt = (struct rtentry *)rn;
1402 struct ifnet *ifp = arg;
1405 if (rt->rt_ifp == ifp) {
1408 * Protect (sorta) against walktree recursion problems
1409 * with cloned routes
1411 if ((rt->rt_flags & RTF_UP) == 0)
1414 err = rtrequest_fib(RTM_DELETE, rt_key(rt), rt->rt_gateway,
1415 rt_mask(rt), rt->rt_flags|RTF_RNH_LOCKED,
1416 (struct rtentry **) NULL, rt->rt_fibnum);
1418 log(LOG_WARNING, "if_rtdel: error %d\n", err);
1426 * Wrapper functions for struct ifnet address list locking macros. These are
1427 * used by kernel modules to avoid encoding programming interface or binary
1428 * interface assumptions that may be violated when kernel-internal locking
1429 * approaches change.
1432 if_addr_rlock(struct ifnet *ifp)
1439 if_addr_runlock(struct ifnet *ifp)
1442 IF_ADDR_RUNLOCK(ifp);
1446 if_maddr_rlock(struct ifnet *ifp)
1453 if_maddr_runlock(struct ifnet *ifp)
1456 IF_ADDR_RUNLOCK(ifp);
1460 * Reference count functions for ifaddrs.
1463 ifa_init(struct ifaddr *ifa)
1466 mtx_init(&ifa->ifa_mtx, "ifaddr", NULL, MTX_DEF);
1467 refcount_init(&ifa->ifa_refcnt, 1);
1471 ifa_ref(struct ifaddr *ifa)
1474 refcount_acquire(&ifa->ifa_refcnt);
1478 ifa_free(struct ifaddr *ifa)
1481 if (refcount_release(&ifa->ifa_refcnt)) {
1482 mtx_destroy(&ifa->ifa_mtx);
1483 free(ifa, M_IFADDR);
1488 ifa_add_loopback_route(struct ifaddr *ifa, struct sockaddr *ia)
1491 struct rtentry *rt = NULL;
1492 struct rt_addrinfo info;
1493 static struct sockaddr_dl null_sdl = {sizeof(null_sdl), AF_LINK};
1495 bzero(&info, sizeof(info));
1496 info.rti_ifp = V_loif;
1497 info.rti_flags = ifa->ifa_flags | RTF_HOST | RTF_STATIC;
1498 info.rti_info[RTAX_DST] = ia;
1499 info.rti_info[RTAX_GATEWAY] = (struct sockaddr *)&null_sdl;
1500 error = rtrequest1_fib(RTM_ADD, &info, &rt, 0);
1502 if (error == 0 && rt != NULL) {
1504 ((struct sockaddr_dl *)rt->rt_gateway)->sdl_type =
1505 ifa->ifa_ifp->if_type;
1506 ((struct sockaddr_dl *)rt->rt_gateway)->sdl_index =
1507 ifa->ifa_ifp->if_index;
1510 } else if (error != 0)
1511 log(LOG_INFO, "ifa_add_loopback_route: insertion failed\n");
1517 ifa_del_loopback_route(struct ifaddr *ifa, struct sockaddr *ia)
1520 struct rt_addrinfo info;
1521 struct sockaddr_dl null_sdl;
1523 bzero(&null_sdl, sizeof(null_sdl));
1524 null_sdl.sdl_len = sizeof(null_sdl);
1525 null_sdl.sdl_family = AF_LINK;
1526 null_sdl.sdl_type = ifa->ifa_ifp->if_type;
1527 null_sdl.sdl_index = ifa->ifa_ifp->if_index;
1528 bzero(&info, sizeof(info));
1529 info.rti_flags = ifa->ifa_flags | RTF_HOST | RTF_STATIC;
1530 info.rti_info[RTAX_DST] = ia;
1531 info.rti_info[RTAX_GATEWAY] = (struct sockaddr *)&null_sdl;
1532 error = rtrequest1_fib(RTM_DELETE, &info, NULL, 0);
1535 log(LOG_INFO, "ifa_del_loopback_route: deletion failed\n");
1541 * XXX: Because sockaddr_dl has deeper structure than the sockaddr
1542 * structs used to represent other address families, it is necessary
1543 * to perform a different comparison.
1546 #define sa_equal(a1, a2) \
1547 (bcmp((a1), (a2), ((a1))->sa_len) == 0)
1549 #define sa_dl_equal(a1, a2) \
1550 ((((struct sockaddr_dl *)(a1))->sdl_len == \
1551 ((struct sockaddr_dl *)(a2))->sdl_len) && \
1552 (bcmp(LLADDR((struct sockaddr_dl *)(a1)), \
1553 LLADDR((struct sockaddr_dl *)(a2)), \
1554 ((struct sockaddr_dl *)(a1))->sdl_alen) == 0))
1557 * Locate an interface based on a complete address.
1560 static struct ifaddr *
1561 ifa_ifwithaddr_internal(struct sockaddr *addr, int getref)
1566 IFNET_RLOCK_NOSLEEP();
1567 TAILQ_FOREACH(ifp, &V_ifnet, if_link) {
1569 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
1570 if (ifa->ifa_addr->sa_family != addr->sa_family)
1572 if (sa_equal(addr, ifa->ifa_addr)) {
1575 IF_ADDR_RUNLOCK(ifp);
1578 /* IP6 doesn't have broadcast */
1579 if ((ifp->if_flags & IFF_BROADCAST) &&
1580 ifa->ifa_broadaddr &&
1581 ifa->ifa_broadaddr->sa_len != 0 &&
1582 sa_equal(ifa->ifa_broadaddr, addr)) {
1585 IF_ADDR_RUNLOCK(ifp);
1589 IF_ADDR_RUNLOCK(ifp);
1593 IFNET_RUNLOCK_NOSLEEP();
1598 ifa_ifwithaddr(struct sockaddr *addr)
1601 return (ifa_ifwithaddr_internal(addr, 1));
1605 ifa_ifwithaddr_check(struct sockaddr *addr)
1608 return (ifa_ifwithaddr_internal(addr, 0) != NULL);
1612 * Locate an interface based on the broadcast address.
1616 ifa_ifwithbroadaddr(struct sockaddr *addr)
1621 IFNET_RLOCK_NOSLEEP();
1622 TAILQ_FOREACH(ifp, &V_ifnet, if_link) {
1624 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
1625 if (ifa->ifa_addr->sa_family != addr->sa_family)
1627 if ((ifp->if_flags & IFF_BROADCAST) &&
1628 ifa->ifa_broadaddr &&
1629 ifa->ifa_broadaddr->sa_len != 0 &&
1630 sa_equal(ifa->ifa_broadaddr, addr)) {
1632 IF_ADDR_RUNLOCK(ifp);
1636 IF_ADDR_RUNLOCK(ifp);
1640 IFNET_RUNLOCK_NOSLEEP();
1645 * Locate the point to point interface with a given destination address.
1649 ifa_ifwithdstaddr(struct sockaddr *addr)
1654 IFNET_RLOCK_NOSLEEP();
1655 TAILQ_FOREACH(ifp, &V_ifnet, if_link) {
1656 if ((ifp->if_flags & IFF_POINTOPOINT) == 0)
1659 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
1660 if (ifa->ifa_addr->sa_family != addr->sa_family)
1662 if (ifa->ifa_dstaddr != NULL &&
1663 sa_equal(addr, ifa->ifa_dstaddr)) {
1665 IF_ADDR_RUNLOCK(ifp);
1669 IF_ADDR_RUNLOCK(ifp);
1673 IFNET_RUNLOCK_NOSLEEP();
1678 * Find an interface on a specific network. If many, choice
1679 * is most specific found.
1682 ifa_ifwithnet(struct sockaddr *addr, int ignore_ptp)
1686 struct ifaddr *ifa_maybe = NULL;
1687 u_int af = addr->sa_family;
1688 char *addr_data = addr->sa_data, *cplim;
1691 * AF_LINK addresses can be looked up directly by their index number,
1692 * so do that if we can.
1694 if (af == AF_LINK) {
1695 struct sockaddr_dl *sdl = (struct sockaddr_dl *)addr;
1696 if (sdl->sdl_index && sdl->sdl_index <= V_if_index)
1697 return (ifaddr_byindex(sdl->sdl_index));
1701 * Scan though each interface, looking for ones that have addresses
1702 * in this address family. Maintain a reference on ifa_maybe once
1703 * we find one, as we release the IF_ADDR_RLOCK() that kept it stable
1704 * when we move onto the next interface.
1706 IFNET_RLOCK_NOSLEEP();
1707 TAILQ_FOREACH(ifp, &V_ifnet, if_link) {
1709 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
1710 char *cp, *cp2, *cp3;
1712 if (ifa->ifa_addr->sa_family != af)
1714 if (af == AF_INET &&
1715 ifp->if_flags & IFF_POINTOPOINT && !ignore_ptp) {
1717 * This is a bit broken as it doesn't
1718 * take into account that the remote end may
1719 * be a single node in the network we are
1721 * The trouble is that we don't know the
1722 * netmask for the remote end.
1724 if (ifa->ifa_dstaddr != NULL &&
1725 sa_equal(addr, ifa->ifa_dstaddr)) {
1727 IF_ADDR_RUNLOCK(ifp);
1732 * if we have a special address handler,
1733 * then use it instead of the generic one.
1735 if (ifa->ifa_claim_addr) {
1736 if ((*ifa->ifa_claim_addr)(ifa, addr)) {
1738 IF_ADDR_RUNLOCK(ifp);
1745 * Scan all the bits in the ifa's address.
1746 * If a bit dissagrees with what we are
1747 * looking for, mask it with the netmask
1748 * to see if it really matters.
1749 * (A byte at a time)
1751 if (ifa->ifa_netmask == 0)
1754 cp2 = ifa->ifa_addr->sa_data;
1755 cp3 = ifa->ifa_netmask->sa_data;
1756 cplim = ifa->ifa_netmask->sa_len
1757 + (char *)ifa->ifa_netmask;
1759 if ((*cp++ ^ *cp2++) & *cp3++)
1760 goto next; /* next address! */
1762 * If the netmask of what we just found
1763 * is more specific than what we had before
1764 * (if we had one) then remember the new one
1765 * before continuing to search
1766 * for an even better one.
1768 if (ifa_maybe == NULL ||
1769 rn_refines((caddr_t)ifa->ifa_netmask,
1770 (caddr_t)ifa_maybe->ifa_netmask)) {
1771 if (ifa_maybe != NULL)
1772 ifa_free(ifa_maybe);
1778 IF_ADDR_RUNLOCK(ifp);
1783 IFNET_RUNLOCK_NOSLEEP();
1784 if (ifa_maybe != NULL)
1785 ifa_free(ifa_maybe);
1790 * Find an interface address specific to an interface best matching
1794 ifaof_ifpforaddr(struct sockaddr *addr, struct ifnet *ifp)
1797 char *cp, *cp2, *cp3;
1799 struct ifaddr *ifa_maybe = NULL;
1800 u_int af = addr->sa_family;
1805 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
1806 if (ifa->ifa_addr->sa_family != af)
1808 if (ifa_maybe == NULL)
1810 if (ifa->ifa_netmask == 0) {
1811 if (sa_equal(addr, ifa->ifa_addr) ||
1812 (ifa->ifa_dstaddr &&
1813 sa_equal(addr, ifa->ifa_dstaddr)))
1817 if (ifp->if_flags & IFF_POINTOPOINT) {
1818 if (sa_equal(addr, ifa->ifa_dstaddr))
1822 cp2 = ifa->ifa_addr->sa_data;
1823 cp3 = ifa->ifa_netmask->sa_data;
1824 cplim = ifa->ifa_netmask->sa_len + (char *)ifa->ifa_netmask;
1825 for (; cp3 < cplim; cp3++)
1826 if ((*cp++ ^ *cp2++) & *cp3)
1836 IF_ADDR_RUNLOCK(ifp);
1840 #include <net/if_llatbl.h>
1843 * Default action when installing a route with a Link Level gateway.
1844 * Lookup an appropriate real ifa to point to.
1845 * This should be moved to /sys/net/link.c eventually.
1848 link_rtrequest(int cmd, struct rtentry *rt, struct rt_addrinfo *info)
1850 struct ifaddr *ifa, *oifa;
1851 struct sockaddr *dst;
1856 if (cmd != RTM_ADD || ((ifa = rt->rt_ifa) == 0) ||
1857 ((ifp = ifa->ifa_ifp) == 0) || ((dst = rt_key(rt)) == 0))
1859 ifa = ifaof_ifpforaddr(dst, ifp);
1864 if (ifa->ifa_rtrequest && ifa->ifa_rtrequest != link_rtrequest)
1865 ifa->ifa_rtrequest(cmd, rt, info);
1870 * Mark an interface down and notify protocols of
1872 * NOTE: must be called at splnet or eqivalent.
1875 if_unroute(struct ifnet *ifp, int flag, int fam)
1879 KASSERT(flag == IFF_UP, ("if_unroute: flag != IFF_UP"));
1881 ifp->if_flags &= ~flag;
1882 getmicrotime(&ifp->if_lastchange);
1883 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link)
1884 if (fam == PF_UNSPEC || (fam == ifa->ifa_addr->sa_family))
1885 pfctlinput(PRC_IFDOWN, ifa->ifa_addr);
1886 ifp->if_qflush(ifp);
1889 (*carp_linkstate_p)(ifp);
1894 * Mark an interface up and notify protocols of
1896 * NOTE: must be called at splnet or eqivalent.
1899 if_route(struct ifnet *ifp, int flag, int fam)
1903 KASSERT(flag == IFF_UP, ("if_route: flag != IFF_UP"));
1905 ifp->if_flags |= flag;
1906 getmicrotime(&ifp->if_lastchange);
1907 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link)
1908 if (fam == PF_UNSPEC || (fam == ifa->ifa_addr->sa_family))
1909 pfctlinput(PRC_IFUP, ifa->ifa_addr);
1911 (*carp_linkstate_p)(ifp);
1918 void (*vlan_link_state_p)(struct ifnet *, int); /* XXX: private from if_vlan */
1919 void (*vlan_trunk_cap_p)(struct ifnet *); /* XXX: private from if_vlan */
1922 * Handle a change in the interface link state. To avoid LORs
1923 * between driver lock and upper layer locks, as well as possible
1924 * recursions, we post event to taskqueue, and all job
1925 * is done in static do_link_state_change().
1928 if_link_state_change(struct ifnet *ifp, int link_state)
1930 /* Return if state hasn't changed. */
1931 if (ifp->if_link_state == link_state)
1934 ifp->if_link_state = link_state;
1936 taskqueue_enqueue(taskqueue_swi, &ifp->if_linktask);
1940 do_link_state_change(void *arg, int pending)
1942 struct ifnet *ifp = (struct ifnet *)arg;
1943 int link_state = ifp->if_link_state;
1944 CURVNET_SET(ifp->if_vnet);
1946 /* Notify that the link state has changed. */
1948 if (ifp->if_vlantrunk != NULL)
1949 (*vlan_link_state_p)(ifp, 0);
1951 if ((ifp->if_type == IFT_ETHER || ifp->if_type == IFT_L2VLAN) &&
1952 IFP2AC(ifp)->ac_netgraph != NULL)
1953 (*ng_ether_link_state_p)(ifp, link_state);
1955 (*carp_linkstate_p)(ifp);
1956 if (ifp->if_bridge) {
1957 KASSERT(bstp_linkstate_p != NULL,("if_bridge bstp not loaded!"));
1958 (*bstp_linkstate_p)(ifp, link_state);
1961 KASSERT(lagg_linkstate_p != NULL,("if_lagg not loaded!"));
1962 (*lagg_linkstate_p)(ifp, link_state);
1965 if (IS_DEFAULT_VNET(curvnet))
1966 devctl_notify("IFNET", ifp->if_xname,
1967 (link_state == LINK_STATE_UP) ? "LINK_UP" : "LINK_DOWN",
1970 if_printf(ifp, "%d link states coalesced\n", pending);
1971 if (log_link_state_change)
1972 log(LOG_NOTICE, "%s: link state changed to %s\n", ifp->if_xname,
1973 (link_state == LINK_STATE_UP) ? "UP" : "DOWN" );
1978 * Mark an interface down and notify protocols of
1980 * NOTE: must be called at splnet or eqivalent.
1983 if_down(struct ifnet *ifp)
1986 if_unroute(ifp, IFF_UP, AF_UNSPEC);
1990 * Mark an interface up and notify protocols of
1992 * NOTE: must be called at splnet or eqivalent.
1995 if_up(struct ifnet *ifp)
1998 if_route(ifp, IFF_UP, AF_UNSPEC);
2002 * Flush an interface queue.
2005 if_qflush(struct ifnet *ifp)
2013 if (ALTQ_IS_ENABLED(ifq))
2017 while ((m = n) != 0) {
2028 * Handle interface watchdog timer routines. Called
2029 * from softclock, we decrement timers (if set) and
2030 * call the appropriate interface routine on expiration.
2032 * XXXRW: Note that because timeouts run with Giant, if_watchdog() is called
2036 if_slowtimo(void *arg)
2038 VNET_ITERATOR_DECL(vnet_iter);
2042 VNET_LIST_RLOCK_NOSLEEP();
2043 IFNET_RLOCK_NOSLEEP();
2044 VNET_FOREACH(vnet_iter) {
2045 CURVNET_SET(vnet_iter);
2046 TAILQ_FOREACH(ifp, &V_ifnet, if_link) {
2047 if (ifp->if_timer == 0 || --ifp->if_timer)
2049 if (ifp->if_watchdog)
2050 (*ifp->if_watchdog)(ifp);
2054 IFNET_RUNLOCK_NOSLEEP();
2055 VNET_LIST_RUNLOCK_NOSLEEP();
2057 timeout(if_slowtimo, (void *)0, hz / IFNET_SLOWHZ);
2061 * Map interface name to interface structure pointer, with or without
2062 * returning a reference.
2065 ifunit_ref(const char *name)
2069 IFNET_RLOCK_NOSLEEP();
2070 TAILQ_FOREACH(ifp, &V_ifnet, if_link) {
2071 if (strncmp(name, ifp->if_xname, IFNAMSIZ) == 0 &&
2072 !(ifp->if_flags & IFF_DYING))
2077 IFNET_RUNLOCK_NOSLEEP();
2082 ifunit(const char *name)
2086 IFNET_RLOCK_NOSLEEP();
2087 TAILQ_FOREACH(ifp, &V_ifnet, if_link) {
2088 if (strncmp(name, ifp->if_xname, IFNAMSIZ) == 0)
2091 IFNET_RUNLOCK_NOSLEEP();
2096 * Hardware specific interface ioctls.
2099 ifhwioctl(u_long cmd, struct ifnet *ifp, caddr_t data, struct thread *td)
2104 int new_flags, temp_flags;
2105 size_t namelen, onamelen;
2107 char *descrbuf, *odescrbuf;
2108 char new_name[IFNAMSIZ];
2110 struct sockaddr_dl *sdl;
2112 ifr = (struct ifreq *)data;
2115 ifr->ifr_index = ifp->if_index;
2119 temp_flags = ifp->if_flags | ifp->if_drv_flags;
2120 ifr->ifr_flags = temp_flags & 0xffff;
2121 ifr->ifr_flagshigh = temp_flags >> 16;
2125 ifr->ifr_reqcap = ifp->if_capabilities;
2126 ifr->ifr_curcap = ifp->if_capenable;
2131 error = mac_ifnet_ioctl_get(td->td_ucred, ifr, ifp);
2136 ifr->ifr_metric = ifp->if_metric;
2140 ifr->ifr_mtu = ifp->if_mtu;
2144 ifr->ifr_phys = ifp->if_physical;
2149 sx_slock(&ifdescr_sx);
2150 if (ifp->if_description == NULL)
2153 /* space for terminating nul */
2154 descrlen = strlen(ifp->if_description) + 1;
2155 if (ifr->ifr_buffer.length < descrlen)
2156 ifr->ifr_buffer.buffer = NULL;
2158 error = copyout(ifp->if_description,
2159 ifr->ifr_buffer.buffer, descrlen);
2160 ifr->ifr_buffer.length = descrlen;
2162 sx_sunlock(&ifdescr_sx);
2166 error = priv_check(td, PRIV_NET_SETIFDESCR);
2171 * Copy only (length-1) bytes to make sure that
2172 * if_description is always nul terminated. The
2173 * length parameter is supposed to count the
2174 * terminating nul in.
2176 if (ifr->ifr_buffer.length > ifdescr_maxlen)
2177 return (ENAMETOOLONG);
2178 else if (ifr->ifr_buffer.length == 0)
2181 descrbuf = malloc(ifr->ifr_buffer.length, M_IFDESCR,
2183 error = copyin(ifr->ifr_buffer.buffer, descrbuf,
2184 ifr->ifr_buffer.length - 1);
2186 free(descrbuf, M_IFDESCR);
2191 sx_xlock(&ifdescr_sx);
2192 odescrbuf = ifp->if_description;
2193 ifp->if_description = descrbuf;
2194 sx_xunlock(&ifdescr_sx);
2196 getmicrotime(&ifp->if_lastchange);
2197 free(odescrbuf, M_IFDESCR);
2201 ifr->ifr_fib = ifp->if_fib;
2205 error = priv_check(td, PRIV_NET_SETIFFIB);
2208 if (ifr->ifr_fib >= rt_numfibs)
2211 ifp->if_fib = ifr->ifr_fib;
2215 error = priv_check(td, PRIV_NET_SETIFFLAGS);
2219 * Currently, no driver owned flags pass the IFF_CANTCHANGE
2220 * check, so we don't need special handling here yet.
2222 new_flags = (ifr->ifr_flags & 0xffff) |
2223 (ifr->ifr_flagshigh << 16);
2224 if (ifp->if_flags & IFF_SMART) {
2225 /* Smart drivers twiddle their own routes */
2226 } else if (ifp->if_flags & IFF_UP &&
2227 (new_flags & IFF_UP) == 0) {
2231 } else if (new_flags & IFF_UP &&
2232 (ifp->if_flags & IFF_UP) == 0) {
2237 /* See if permanently promiscuous mode bit is about to flip */
2238 if ((ifp->if_flags ^ new_flags) & IFF_PPROMISC) {
2239 if (new_flags & IFF_PPROMISC)
2240 ifp->if_flags |= IFF_PROMISC;
2241 else if (ifp->if_pcount == 0)
2242 ifp->if_flags &= ~IFF_PROMISC;
2243 log(LOG_INFO, "%s: permanently promiscuous mode %s\n",
2245 (new_flags & IFF_PPROMISC) ? "enabled" : "disabled");
2247 ifp->if_flags = (ifp->if_flags & IFF_CANTCHANGE) |
2248 (new_flags &~ IFF_CANTCHANGE);
2249 if (ifp->if_ioctl) {
2250 (void) (*ifp->if_ioctl)(ifp, cmd, data);
2252 getmicrotime(&ifp->if_lastchange);
2256 error = priv_check(td, PRIV_NET_SETIFCAP);
2259 if (ifp->if_ioctl == NULL)
2260 return (EOPNOTSUPP);
2261 if (ifr->ifr_reqcap & ~ifp->if_capabilities)
2263 error = (*ifp->if_ioctl)(ifp, cmd, data);
2265 getmicrotime(&ifp->if_lastchange);
2270 error = mac_ifnet_ioctl_set(td->td_ucred, ifr, ifp);
2275 error = priv_check(td, PRIV_NET_SETIFNAME);
2278 error = copyinstr(ifr->ifr_data, new_name, IFNAMSIZ, NULL);
2281 if (new_name[0] == '\0')
2283 if (ifunit(new_name) != NULL)
2287 * XXX: Locking. Nothing else seems to lock if_flags,
2288 * and there are numerous other races with the
2289 * ifunit() checks not being atomic with namespace
2290 * changes (renames, vmoves, if_attach, etc).
2292 ifp->if_flags |= IFF_RENAMING;
2294 /* Announce the departure of the interface. */
2295 rt_ifannouncemsg(ifp, IFAN_DEPARTURE);
2296 EVENTHANDLER_INVOKE(ifnet_departure_event, ifp);
2298 log(LOG_INFO, "%s: changing name to '%s'\n",
2299 ifp->if_xname, new_name);
2301 strlcpy(ifp->if_xname, new_name, sizeof(ifp->if_xname));
2304 sdl = (struct sockaddr_dl *)ifa->ifa_addr;
2305 namelen = strlen(new_name);
2306 onamelen = sdl->sdl_nlen;
2308 * Move the address if needed. This is safe because we
2309 * allocate space for a name of length IFNAMSIZ when we
2310 * create this in if_attach().
2312 if (namelen != onamelen) {
2313 bcopy(sdl->sdl_data + onamelen,
2314 sdl->sdl_data + namelen, sdl->sdl_alen);
2316 bcopy(new_name, sdl->sdl_data, namelen);
2317 sdl->sdl_nlen = namelen;
2318 sdl = (struct sockaddr_dl *)ifa->ifa_netmask;
2319 bzero(sdl->sdl_data, onamelen);
2320 while (namelen != 0)
2321 sdl->sdl_data[--namelen] = 0xff;
2324 EVENTHANDLER_INVOKE(ifnet_arrival_event, ifp);
2325 /* Announce the return of the interface. */
2326 rt_ifannouncemsg(ifp, IFAN_ARRIVAL);
2328 ifp->if_flags &= ~IFF_RENAMING;
2333 error = priv_check(td, PRIV_NET_SETIFVNET);
2336 error = if_vmove_loan(td, ifp, ifr->ifr_name, ifr->ifr_jid);
2341 error = priv_check(td, PRIV_NET_SETIFMETRIC);
2344 ifp->if_metric = ifr->ifr_metric;
2345 getmicrotime(&ifp->if_lastchange);
2349 error = priv_check(td, PRIV_NET_SETIFPHYS);
2352 if (ifp->if_ioctl == NULL)
2353 return (EOPNOTSUPP);
2354 error = (*ifp->if_ioctl)(ifp, cmd, data);
2356 getmicrotime(&ifp->if_lastchange);
2361 u_long oldmtu = ifp->if_mtu;
2363 error = priv_check(td, PRIV_NET_SETIFMTU);
2366 if (ifr->ifr_mtu < IF_MINMTU || ifr->ifr_mtu > IF_MAXMTU)
2368 if (ifp->if_ioctl == NULL)
2369 return (EOPNOTSUPP);
2370 error = (*ifp->if_ioctl)(ifp, cmd, data);
2372 getmicrotime(&ifp->if_lastchange);
2376 * If the link MTU changed, do network layer specific procedure.
2378 if (ifp->if_mtu != oldmtu) {
2388 if (cmd == SIOCADDMULTI)
2389 error = priv_check(td, PRIV_NET_ADDMULTI);
2391 error = priv_check(td, PRIV_NET_DELMULTI);
2395 /* Don't allow group membership on non-multicast interfaces. */
2396 if ((ifp->if_flags & IFF_MULTICAST) == 0)
2397 return (EOPNOTSUPP);
2399 /* Don't let users screw up protocols' entries. */
2400 if (ifr->ifr_addr.sa_family != AF_LINK)
2403 if (cmd == SIOCADDMULTI) {
2404 struct ifmultiaddr *ifma;
2407 * Userland is only permitted to join groups once
2408 * via the if_addmulti() KPI, because it cannot hold
2409 * struct ifmultiaddr * between calls. It may also
2410 * lose a race while we check if the membership
2414 ifma = if_findmulti(ifp, &ifr->ifr_addr);
2415 IF_ADDR_RUNLOCK(ifp);
2419 error = if_addmulti(ifp, &ifr->ifr_addr, &ifma);
2421 error = if_delmulti(ifp, &ifr->ifr_addr);
2424 getmicrotime(&ifp->if_lastchange);
2427 case SIOCSIFPHYADDR:
2428 case SIOCDIFPHYADDR:
2430 case SIOCSIFPHYADDR_IN6:
2432 case SIOCSLIFPHYADDR:
2434 case SIOCSIFGENERIC:
2435 error = priv_check(td, PRIV_NET_HWIOCTL);
2438 if (ifp->if_ioctl == NULL)
2439 return (EOPNOTSUPP);
2440 error = (*ifp->if_ioctl)(ifp, cmd, data);
2442 getmicrotime(&ifp->if_lastchange);
2446 ifs = (struct ifstat *)data;
2447 ifs->ascii[0] = '\0';
2449 case SIOCGIFPSRCADDR:
2450 case SIOCGIFPDSTADDR:
2451 case SIOCGLIFPHYADDR:
2453 case SIOCGIFGENERIC:
2454 if (ifp->if_ioctl == NULL)
2455 return (EOPNOTSUPP);
2456 error = (*ifp->if_ioctl)(ifp, cmd, data);
2460 error = priv_check(td, PRIV_NET_SETLLADDR);
2463 error = if_setlladdr(ifp,
2464 ifr->ifr_addr.sa_data, ifr->ifr_addr.sa_len);
2465 EVENTHANDLER_INVOKE(iflladdr_event, ifp);
2470 struct ifgroupreq *ifgr = (struct ifgroupreq *)ifr;
2472 error = priv_check(td, PRIV_NET_ADDIFGROUP);
2475 if ((error = if_addgroup(ifp, ifgr->ifgr_group)))
2481 if ((error = if_getgroup((struct ifgroupreq *)ifr, ifp)))
2487 struct ifgroupreq *ifgr = (struct ifgroupreq *)ifr;
2489 error = priv_check(td, PRIV_NET_DELIFGROUP);
2492 if ((error = if_delgroup(ifp, ifgr->ifgr_group)))
2504 #ifdef COMPAT_FREEBSD32
2512 #define SIOCGIFCONF32 _IOWR('i', 36, struct ifconf32)
2519 ifioctl(struct socket *so, u_long cmd, caddr_t data, struct thread *td)
2526 CURVNET_SET(so->so_vnet);
2530 error = ifconf(cmd, data);
2534 #ifdef COMPAT_FREEBSD32
2537 struct ifconf32 *ifc32;
2540 ifc32 = (struct ifconf32 *)data;
2541 ifc.ifc_len = ifc32->ifc_len;
2542 ifc.ifc_buf = PTRIN(ifc32->ifc_buf);
2544 error = ifconf(SIOCGIFCONF, (void *)&ifc);
2547 ifc32->ifc_len = ifc.ifc_len;
2552 ifr = (struct ifreq *)data;
2557 error = priv_check(td, PRIV_NET_SETIFVNET);
2559 error = if_vmove_reclaim(td, ifr->ifr_name,
2566 error = priv_check(td, PRIV_NET_IFCREATE);
2568 error = if_clone_create(ifr->ifr_name,
2569 sizeof(ifr->ifr_name),
2570 cmd == SIOCIFCREATE2 ? ifr->ifr_data : NULL);
2574 error = priv_check(td, PRIV_NET_IFDESTROY);
2576 error = if_clone_destroy(ifr->ifr_name);
2580 case SIOCIFGCLONERS:
2581 error = if_clone_list((struct if_clonereq *)data);
2585 error = if_getgroupmembers((struct ifgroupreq *)data);
2590 ifp = ifunit_ref(ifr->ifr_name);
2596 error = ifhwioctl(cmd, ifp, data, td);
2597 if (error != ENOIOCTL) {
2603 oif_flags = ifp->if_flags;
2604 if (so->so_proto == NULL) {
2607 return (EOPNOTSUPP);
2610 error = ((*so->so_proto->pr_usrreqs->pru_control)(so, cmd,
2613 if (error == EOPNOTSUPP && ifp != NULL && ifp->if_ioctl != NULL)
2614 error = (*ifp->if_ioctl)(ifp, cmd, data);
2621 case SIOCSIFDSTADDR:
2623 case SIOCSIFBRDADDR:
2624 case SIOCSIFNETMASK:
2625 #if BYTE_ORDER != BIG_ENDIAN
2626 if (ifr->ifr_addr.sa_family == 0 &&
2627 ifr->ifr_addr.sa_len < 16) {
2628 ifr->ifr_addr.sa_family = ifr->ifr_addr.sa_len;
2629 ifr->ifr_addr.sa_len = 16;
2632 if (ifr->ifr_addr.sa_len == 0)
2633 ifr->ifr_addr.sa_len = 16;
2641 case OSIOCGIFDSTADDR:
2642 cmd = SIOCGIFDSTADDR;
2645 case OSIOCGIFBRDADDR:
2646 cmd = SIOCGIFBRDADDR;
2649 case OSIOCGIFNETMASK:
2650 cmd = SIOCGIFNETMASK;
2652 error = ((*so->so_proto->pr_usrreqs->pru_control)(so,
2656 if (error == EOPNOTSUPP && ifp != NULL &&
2657 ifp->if_ioctl != NULL)
2658 error = (*ifp->if_ioctl)(ifp, cmd, data);
2662 case OSIOCGIFDSTADDR:
2663 case OSIOCGIFBRDADDR:
2664 case OSIOCGIFNETMASK:
2665 *(u_short *)&ifr->ifr_addr = ifr->ifr_addr.sa_family;
2669 #endif /* COMPAT_43 */
2671 if ((oif_flags ^ ifp->if_flags) & IFF_UP) {
2673 if (ifp->if_flags & IFF_UP) {
2686 * The code common to handling reference counted flags,
2687 * e.g., in ifpromisc() and if_allmulti().
2688 * The "pflag" argument can specify a permanent mode flag to check,
2689 * such as IFF_PPROMISC for promiscuous mode; should be 0 if none.
2691 * Only to be used on stack-owned flags, not driver-owned flags.
2694 if_setflag(struct ifnet *ifp, int flag, int pflag, int *refcount, int onswitch)
2698 int oldflags, oldcount;
2700 /* Sanity checks to catch programming errors */
2701 KASSERT((flag & (IFF_DRV_OACTIVE|IFF_DRV_RUNNING)) == 0,
2702 ("%s: setting driver-owned flag %d", __func__, flag));
2705 KASSERT(*refcount >= 0,
2706 ("%s: increment negative refcount %d for flag %d",
2707 __func__, *refcount, flag));
2709 KASSERT(*refcount > 0,
2710 ("%s: decrement non-positive refcount %d for flag %d",
2711 __func__, *refcount, flag));
2713 /* In case this mode is permanent, just touch refcount */
2714 if (ifp->if_flags & pflag) {
2715 *refcount += onswitch ? 1 : -1;
2719 /* Save ifnet parameters for if_ioctl() may fail */
2720 oldcount = *refcount;
2721 oldflags = ifp->if_flags;
2724 * See if we aren't the only and touching refcount is enough.
2725 * Actually toggle interface flag if we are the first or last.
2730 ifp->if_flags |= flag;
2734 ifp->if_flags &= ~flag;
2737 /* Call down the driver since we've changed interface flags */
2738 if (ifp->if_ioctl == NULL) {
2742 ifr.ifr_flags = ifp->if_flags & 0xffff;
2743 ifr.ifr_flagshigh = ifp->if_flags >> 16;
2744 error = (*ifp->if_ioctl)(ifp, SIOCSIFFLAGS, (caddr_t)&ifr);
2747 /* Notify userland that interface flags have changed */
2752 /* Recover after driver error */
2753 *refcount = oldcount;
2754 ifp->if_flags = oldflags;
2759 * Set/clear promiscuous mode on interface ifp based on the truth value
2760 * of pswitch. The calls are reference counted so that only the first
2761 * "on" request actually has an effect, as does the final "off" request.
2762 * Results are undefined if the "off" and "on" requests are not matched.
2765 ifpromisc(struct ifnet *ifp, int pswitch)
2768 int oldflags = ifp->if_flags;
2770 error = if_setflag(ifp, IFF_PROMISC, IFF_PPROMISC,
2771 &ifp->if_pcount, pswitch);
2772 /* If promiscuous mode status has changed, log a message */
2773 if (error == 0 && ((ifp->if_flags ^ oldflags) & IFF_PROMISC))
2774 log(LOG_INFO, "%s: promiscuous mode %s\n",
2776 (ifp->if_flags & IFF_PROMISC) ? "enabled" : "disabled");
2781 * Return interface configuration
2782 * of system. List may be used
2783 * in later ioctl's (above) to get
2784 * other information.
2788 ifconf(u_long cmd, caddr_t data)
2790 struct ifconf *ifc = (struct ifconf *)data;
2795 int error, full = 0, valid_len, max_len;
2797 /* Limit initial buffer size to MAXPHYS to avoid DoS from userspace. */
2798 max_len = MAXPHYS - 1;
2800 /* Prevent hostile input from being able to crash the system */
2801 if (ifc->ifc_len <= 0)
2805 if (ifc->ifc_len <= max_len) {
2806 max_len = ifc->ifc_len;
2809 sb = sbuf_new(NULL, NULL, max_len + 1, SBUF_FIXEDLEN);
2814 TAILQ_FOREACH(ifp, &V_ifnet, if_link) {
2818 * Zero the ifr_name buffer to make sure we don't
2819 * disclose the contents of the stack.
2821 memset(ifr.ifr_name, 0, sizeof(ifr.ifr_name));
2823 if (strlcpy(ifr.ifr_name, ifp->if_xname, sizeof(ifr.ifr_name))
2824 >= sizeof(ifr.ifr_name)) {
2827 return (ENAMETOOLONG);
2832 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
2833 struct sockaddr *sa = ifa->ifa_addr;
2835 if (prison_if(curthread->td_ucred, sa) != 0)
2839 if (cmd == OSIOCGIFCONF) {
2840 struct osockaddr *osa =
2841 (struct osockaddr *)&ifr.ifr_addr;
2843 osa->sa_family = sa->sa_family;
2844 sbuf_bcat(sb, &ifr, sizeof(ifr));
2845 max_len += sizeof(ifr);
2848 if (sa->sa_len <= sizeof(*sa)) {
2850 sbuf_bcat(sb, &ifr, sizeof(ifr));
2851 max_len += sizeof(ifr);
2854 offsetof(struct ifreq, ifr_addr));
2855 max_len += offsetof(struct ifreq, ifr_addr);
2856 sbuf_bcat(sb, sa, sa->sa_len);
2857 max_len += sa->sa_len;
2860 if (!sbuf_overflowed(sb))
2861 valid_len = sbuf_len(sb);
2863 IF_ADDR_RUNLOCK(ifp);
2865 bzero((caddr_t)&ifr.ifr_addr, sizeof(ifr.ifr_addr));
2866 sbuf_bcat(sb, &ifr, sizeof(ifr));
2867 max_len += sizeof(ifr);
2869 if (!sbuf_overflowed(sb))
2870 valid_len = sbuf_len(sb);
2876 * If we didn't allocate enough space (uncommon), try again. If
2877 * we have already allocated as much space as we are allowed,
2878 * return what we've got.
2880 if (valid_len != max_len && !full) {
2885 ifc->ifc_len = valid_len;
2887 error = copyout(sbuf_data(sb), ifc->ifc_req, ifc->ifc_len);
2893 * Just like ifpromisc(), but for all-multicast-reception mode.
2896 if_allmulti(struct ifnet *ifp, int onswitch)
2899 return (if_setflag(ifp, IFF_ALLMULTI, 0, &ifp->if_amcount, onswitch));
2902 struct ifmultiaddr *
2903 if_findmulti(struct ifnet *ifp, struct sockaddr *sa)
2905 struct ifmultiaddr *ifma;
2907 IF_ADDR_LOCK_ASSERT(ifp);
2909 TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
2910 if (sa->sa_family == AF_LINK) {
2911 if (sa_dl_equal(ifma->ifma_addr, sa))
2914 if (sa_equal(ifma->ifma_addr, sa))
2923 * Allocate a new ifmultiaddr and initialize based on passed arguments. We
2924 * make copies of passed sockaddrs. The ifmultiaddr will not be added to
2925 * the ifnet multicast address list here, so the caller must do that and
2926 * other setup work (such as notifying the device driver). The reference
2927 * count is initialized to 1.
2929 static struct ifmultiaddr *
2930 if_allocmulti(struct ifnet *ifp, struct sockaddr *sa, struct sockaddr *llsa,
2933 struct ifmultiaddr *ifma;
2934 struct sockaddr *dupsa;
2936 ifma = malloc(sizeof *ifma, M_IFMADDR, mflags |
2941 dupsa = malloc(sa->sa_len, M_IFMADDR, mflags);
2942 if (dupsa == NULL) {
2943 free(ifma, M_IFMADDR);
2946 bcopy(sa, dupsa, sa->sa_len);
2947 ifma->ifma_addr = dupsa;
2949 ifma->ifma_ifp = ifp;
2950 ifma->ifma_refcount = 1;
2951 ifma->ifma_protospec = NULL;
2954 ifma->ifma_lladdr = NULL;
2958 dupsa = malloc(llsa->sa_len, M_IFMADDR, mflags);
2959 if (dupsa == NULL) {
2960 free(ifma->ifma_addr, M_IFMADDR);
2961 free(ifma, M_IFMADDR);
2964 bcopy(llsa, dupsa, llsa->sa_len);
2965 ifma->ifma_lladdr = dupsa;
2971 * if_freemulti: free ifmultiaddr structure and possibly attached related
2972 * addresses. The caller is responsible for implementing reference
2973 * counting, notifying the driver, handling routing messages, and releasing
2974 * any dependent link layer state.
2977 if_freemulti(struct ifmultiaddr *ifma)
2980 KASSERT(ifma->ifma_refcount == 0, ("if_freemulti: refcount %d",
2981 ifma->ifma_refcount));
2982 KASSERT(ifma->ifma_protospec == NULL,
2983 ("if_freemulti: protospec not NULL"));
2985 if (ifma->ifma_lladdr != NULL)
2986 free(ifma->ifma_lladdr, M_IFMADDR);
2987 free(ifma->ifma_addr, M_IFMADDR);
2988 free(ifma, M_IFMADDR);
2992 * Register an additional multicast address with a network interface.
2994 * - If the address is already present, bump the reference count on the
2995 * address and return.
2996 * - If the address is not link-layer, look up a link layer address.
2997 * - Allocate address structures for one or both addresses, and attach to the
2998 * multicast address list on the interface. If automatically adding a link
2999 * layer address, the protocol address will own a reference to the link
3000 * layer address, to be freed when it is freed.
3001 * - Notify the network device driver of an addition to the multicast address
3004 * 'sa' points to caller-owned memory with the desired multicast address.
3006 * 'retifma' will be used to return a pointer to the resulting multicast
3007 * address reference, if desired.
3010 if_addmulti(struct ifnet *ifp, struct sockaddr *sa,
3011 struct ifmultiaddr **retifma)
3013 struct ifmultiaddr *ifma, *ll_ifma;
3014 struct sockaddr *llsa;
3018 * If the address is already present, return a new reference to it;
3019 * otherwise, allocate storage and set up a new address.
3022 ifma = if_findmulti(ifp, sa);
3024 ifma->ifma_refcount++;
3025 if (retifma != NULL)
3027 IF_ADDR_WUNLOCK(ifp);
3032 * The address isn't already present; resolve the protocol address
3033 * into a link layer address, and then look that up, bump its
3034 * refcount or allocate an ifma for that also. If 'llsa' was
3035 * returned, we will need to free it later.
3039 if (ifp->if_resolvemulti != NULL) {
3040 error = ifp->if_resolvemulti(ifp, &llsa, sa);
3046 * Allocate the new address. Don't hook it up yet, as we may also
3047 * need to allocate a link layer multicast address.
3049 ifma = if_allocmulti(ifp, sa, llsa, M_NOWAIT);
3056 * If a link layer address is found, we'll need to see if it's
3057 * already present in the address list, or allocate is as well.
3058 * When this block finishes, the link layer address will be on the
3062 ll_ifma = if_findmulti(ifp, llsa);
3063 if (ll_ifma == NULL) {
3064 ll_ifma = if_allocmulti(ifp, llsa, NULL, M_NOWAIT);
3065 if (ll_ifma == NULL) {
3066 --ifma->ifma_refcount;
3071 TAILQ_INSERT_HEAD(&ifp->if_multiaddrs, ll_ifma,
3074 ll_ifma->ifma_refcount++;
3075 ifma->ifma_llifma = ll_ifma;
3079 * We now have a new multicast address, ifma, and possibly a new or
3080 * referenced link layer address. Add the primary address to the
3081 * ifnet address list.
3083 TAILQ_INSERT_HEAD(&ifp->if_multiaddrs, ifma, ifma_link);
3085 if (retifma != NULL)
3089 * Must generate the message while holding the lock so that 'ifma'
3090 * pointer is still valid.
3092 rt_newmaddrmsg(RTM_NEWMADDR, ifma);
3093 IF_ADDR_WUNLOCK(ifp);
3096 * We are certain we have added something, so call down to the
3097 * interface to let them know about it.
3099 if (ifp->if_ioctl != NULL) {
3100 (void) (*ifp->if_ioctl)(ifp, SIOCADDMULTI, 0);
3104 free(llsa, M_IFMADDR);
3110 free(llsa, M_IFMADDR);
3113 IF_ADDR_WUNLOCK(ifp);
3118 * Delete a multicast group membership by network-layer group address.
3120 * Returns ENOENT if the entry could not be found. If ifp no longer
3121 * exists, results are undefined. This entry point should only be used
3122 * from subsystems which do appropriate locking to hold ifp for the
3123 * duration of the call.
3124 * Network-layer protocol domains must use if_delmulti_ifma().
3127 if_delmulti(struct ifnet *ifp, struct sockaddr *sa)
3129 struct ifmultiaddr *ifma;
3134 IFNET_RLOCK_NOSLEEP();
3135 TAILQ_FOREACH(oifp, &V_ifnet, if_link)
3140 IFNET_RUNLOCK_NOSLEEP();
3142 KASSERT(ifp != NULL, ("%s: ifnet went away", __func__));
3149 ifma = if_findmulti(ifp, sa);
3151 lastref = if_delmulti_locked(ifp, ifma, 0);
3152 IF_ADDR_WUNLOCK(ifp);
3157 if (lastref && ifp->if_ioctl != NULL) {
3158 (void)(*ifp->if_ioctl)(ifp, SIOCDELMULTI, 0);
3165 * Delete all multicast group membership for an interface.
3166 * Should be used to quickly flush all multicast filters.
3169 if_delallmulti(struct ifnet *ifp)
3171 struct ifmultiaddr *ifma;
3172 struct ifmultiaddr *next;
3175 TAILQ_FOREACH_SAFE(ifma, &ifp->if_multiaddrs, ifma_link, next)
3176 if_delmulti_locked(ifp, ifma, 0);
3177 IF_ADDR_WUNLOCK(ifp);
3181 * Delete a multicast group membership by group membership pointer.
3182 * Network-layer protocol domains must use this routine.
3184 * It is safe to call this routine if the ifp disappeared.
3187 if_delmulti_ifma(struct ifmultiaddr *ifma)
3192 ifp = ifma->ifma_ifp;
3195 printf("%s: ifma_ifp seems to be detached\n", __func__);
3199 IFNET_RLOCK_NOSLEEP();
3200 TAILQ_FOREACH(oifp, &V_ifnet, if_link)
3204 printf("%s: ifnet %p disappeared\n", __func__, ifp);
3207 IFNET_RUNLOCK_NOSLEEP();
3211 * If and only if the ifnet instance exists: Acquire the address lock.
3216 lastref = if_delmulti_locked(ifp, ifma, 0);
3220 * If and only if the ifnet instance exists:
3221 * Release the address lock.
3222 * If the group was left: update the hardware hash filter.
3224 IF_ADDR_WUNLOCK(ifp);
3225 if (lastref && ifp->if_ioctl != NULL) {
3226 (void)(*ifp->if_ioctl)(ifp, SIOCDELMULTI, 0);
3232 * Perform deletion of network-layer and/or link-layer multicast address.
3234 * Return 0 if the reference count was decremented.
3235 * Return 1 if the final reference was released, indicating that the
3236 * hardware hash filter should be reprogrammed.
3239 if_delmulti_locked(struct ifnet *ifp, struct ifmultiaddr *ifma, int detaching)
3241 struct ifmultiaddr *ll_ifma;
3243 if (ifp != NULL && ifma->ifma_ifp != NULL) {
3244 KASSERT(ifma->ifma_ifp == ifp,
3245 ("%s: inconsistent ifp %p", __func__, ifp));
3246 IF_ADDR_WLOCK_ASSERT(ifp);
3249 ifp = ifma->ifma_ifp;
3252 * If the ifnet is detaching, null out references to ifnet,
3253 * so that upper protocol layers will notice, and not attempt
3254 * to obtain locks for an ifnet which no longer exists. The
3255 * routing socket announcement must happen before the ifnet
3256 * instance is detached from the system.
3260 printf("%s: detaching ifnet instance %p\n", __func__, ifp);
3263 * ifp may already be nulled out if we are being reentered
3264 * to delete the ll_ifma.
3267 rt_newmaddrmsg(RTM_DELMADDR, ifma);
3268 ifma->ifma_ifp = NULL;
3272 if (--ifma->ifma_refcount > 0)
3276 * If this ifma is a network-layer ifma, a link-layer ifma may
3277 * have been associated with it. Release it first if so.
3279 ll_ifma = ifma->ifma_llifma;
3280 if (ll_ifma != NULL) {
3281 KASSERT(ifma->ifma_lladdr != NULL,
3282 ("%s: llifma w/o lladdr", __func__));
3284 ll_ifma->ifma_ifp = NULL; /* XXX */
3285 if (--ll_ifma->ifma_refcount == 0) {
3287 TAILQ_REMOVE(&ifp->if_multiaddrs, ll_ifma,
3290 if_freemulti(ll_ifma);
3295 TAILQ_REMOVE(&ifp->if_multiaddrs, ifma, ifma_link);
3300 * The last reference to this instance of struct ifmultiaddr
3301 * was released; the hardware should be notified of this change.
3307 * Set the link layer address on an interface.
3309 * At this time we only support certain types of interfaces,
3310 * and we don't allow the length of the address to change.
3313 if_setlladdr(struct ifnet *ifp, const u_char *lladdr, int len)
3315 struct sockaddr_dl *sdl;
3322 IF_ADDR_RUNLOCK(ifp);
3326 IF_ADDR_RUNLOCK(ifp);
3327 sdl = (struct sockaddr_dl *)ifa->ifa_addr;
3332 if (len != sdl->sdl_alen) { /* don't allow length to change */
3336 switch (ifp->if_type) {
3344 case IFT_IEEE8023ADLAG:
3346 bcopy(lladdr, LLADDR(sdl), len);
3355 * If the interface is already up, we need
3356 * to re-init it in order to reprogram its
3359 if ((ifp->if_flags & IFF_UP) != 0) {
3360 if (ifp->if_ioctl) {
3361 ifp->if_flags &= ~IFF_UP;
3362 ifr.ifr_flags = ifp->if_flags & 0xffff;
3363 ifr.ifr_flagshigh = ifp->if_flags >> 16;
3364 (*ifp->if_ioctl)(ifp, SIOCSIFFLAGS, (caddr_t)&ifr);
3365 ifp->if_flags |= IFF_UP;
3366 ifr.ifr_flags = ifp->if_flags & 0xffff;
3367 ifr.ifr_flagshigh = ifp->if_flags >> 16;
3368 (*ifp->if_ioctl)(ifp, SIOCSIFFLAGS, (caddr_t)&ifr);
3372 * Also send gratuitous ARPs to notify other nodes about
3373 * the address change.
3375 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
3376 if (ifa->ifa_addr->sa_family == AF_INET)
3377 arp_ifinit(ifp, ifa);
3385 * The name argument must be a pointer to storage which will last as
3386 * long as the interface does. For physical devices, the result of
3387 * device_get_name(dev) is a good choice and for pseudo-devices a
3388 * static string works well.
3391 if_initname(struct ifnet *ifp, const char *name, int unit)
3393 ifp->if_dname = name;
3394 ifp->if_dunit = unit;
3395 if (unit != IF_DUNIT_NONE)
3396 snprintf(ifp->if_xname, IFNAMSIZ, "%s%d", name, unit);
3398 strlcpy(ifp->if_xname, name, IFNAMSIZ);
3402 if_printf(struct ifnet *ifp, const char * fmt, ...)
3407 retval = printf("%s: ", ifp->if_xname);
3409 retval += vprintf(fmt, ap);
3415 if_start(struct ifnet *ifp)
3418 (*(ifp)->if_start)(ifp);
3422 * Backwards compatibility interface for drivers
3423 * that have not implemented it
3426 if_transmit(struct ifnet *ifp, struct mbuf *m)
3430 IFQ_HANDOFF(ifp, m, error);
3435 if_handoff(struct ifqueue *ifq, struct mbuf *m, struct ifnet *ifp, int adjust)
3440 if (_IF_QFULL(ifq)) {
3447 ifp->if_obytes += m->m_pkthdr.len + adjust;
3448 if (m->m_flags & (M_BCAST|M_MCAST))
3450 active = ifp->if_drv_flags & IFF_DRV_OACTIVE;
3452 _IF_ENQUEUE(ifq, m);
3454 if (ifp != NULL && !active)
3455 (*(ifp)->if_start)(ifp);
3460 if_register_com_alloc(u_char type,
3461 if_com_alloc_t *a, if_com_free_t *f)
3464 KASSERT(if_com_alloc[type] == NULL,
3465 ("if_register_com_alloc: %d already registered", type));
3466 KASSERT(if_com_free[type] == NULL,
3467 ("if_register_com_alloc: %d free already registered", type));
3469 if_com_alloc[type] = a;
3470 if_com_free[type] = f;
3474 if_deregister_com_alloc(u_char type)
3477 KASSERT(if_com_alloc[type] != NULL,
3478 ("if_deregister_com_alloc: %d not registered", type));
3479 KASSERT(if_com_free[type] != NULL,
3480 ("if_deregister_com_alloc: %d free not registered", type));
3481 if_com_alloc[type] = NULL;
3482 if_com_free[type] = NULL;