2 * Copyright (c) 1980, 1986, 1993
3 * The Regents of the University of California. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 4. Neither the name of the University nor the names of its contributors
14 * may be used to endorse or promote products derived from this software
15 * without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * @(#)if.c 8.5 (Berkeley) 1/9/95
33 #include "opt_compat.h"
34 #include "opt_inet6.h"
37 #include <sys/param.h>
38 #include <sys/types.h>
40 #include <sys/malloc.h>
44 #include <sys/systm.h>
47 #include <sys/socket.h>
48 #include <sys/socketvar.h>
49 #include <sys/protosw.h>
50 #include <sys/kernel.h>
52 #include <sys/refcount.h>
53 #include <sys/module.h>
54 #include <sys/rwlock.h>
55 #include <sys/sockio.h>
56 #include <sys/syslog.h>
57 #include <sys/sysctl.h>
58 #include <sys/taskqueue.h>
59 #include <sys/domain.h>
63 #include <machine/stdarg.h>
67 #include <net/if_arp.h>
68 #include <net/if_clone.h>
69 #include <net/if_dl.h>
70 #include <net/if_types.h>
71 #include <net/if_var.h>
72 #include <net/radix.h>
73 #include <net/route.h>
76 #if defined(INET) || defined(INET6)
78 #include <netinet/in.h>
79 #include <netinet/in_var.h>
80 #include <netinet/ip_carp.h>
82 #include <netinet6/in6_var.h>
83 #include <netinet6/in6_ifattach.h>
87 #include <netinet/if_ether.h>
90 #include <security/mac/mac_framework.h>
92 #ifdef COMPAT_FREEBSD32
93 #include <sys/mount.h>
94 #include <compat/freebsd32/freebsd32.h>
97 struct ifindex_entry {
98 struct ifnet *ife_ifnet;
101 SYSCTL_NODE(_net, PF_LINK, link, CTLFLAG_RW, 0, "Link layers");
102 SYSCTL_NODE(_net_link, 0, generic, CTLFLAG_RW, 0, "Generic link-management");
104 TUNABLE_INT("net.link.ifqmaxlen", &ifqmaxlen);
105 SYSCTL_INT(_net_link, OID_AUTO, ifqmaxlen, CTLFLAG_RDTUN,
106 &ifqmaxlen, 0, "max send queue size");
108 /* Log link state change events */
109 static int log_link_state_change = 1;
111 SYSCTL_INT(_net_link, OID_AUTO, log_link_state_change, CTLFLAG_RW,
112 &log_link_state_change, 0,
113 "log interface link state change events");
115 /* Interface description */
116 static unsigned int ifdescr_maxlen = 1024;
117 SYSCTL_UINT(_net, OID_AUTO, ifdescr_maxlen, CTLFLAG_RW,
119 "administrative maximum length for interface description");
121 static MALLOC_DEFINE(M_IFDESCR, "ifdescr", "ifnet descriptions");
123 /* global sx for non-critical path ifdescr */
124 static struct sx ifdescr_sx;
125 SX_SYSINIT(ifdescr_sx, &ifdescr_sx, "ifnet descr");
127 void (*bridge_linkstate_p)(struct ifnet *ifp);
128 void (*ng_ether_link_state_p)(struct ifnet *ifp, int state);
129 void (*lagg_linkstate_p)(struct ifnet *ifp, int state);
130 /* These are external hooks for CARP. */
131 void (*carp_linkstate_p)(struct ifnet *ifp);
132 #if defined(INET) || defined(INET6)
133 struct ifnet *(*carp_forus_p)(struct ifnet *ifp, u_char *dhost);
134 int (*carp_output_p)(struct ifnet *ifp, struct mbuf *m,
135 struct sockaddr *sa, struct rtentry *rt);
138 int (*carp_iamatch_p)(struct ifnet *, struct in_ifaddr *, struct in_addr *,
142 struct ifaddr *(*carp_iamatch6_p)(struct ifnet *ifp, struct in6_addr *taddr6);
143 caddr_t (*carp_macmatch6_p)(struct ifnet *ifp, struct mbuf *m,
144 const struct in6_addr *taddr);
147 struct mbuf *(*tbr_dequeue_ptr)(struct ifaltq *, int) = NULL;
150 * XXX: Style; these should be sorted alphabetically, and unprototyped
151 * static functions should be prototyped. Currently they are sorted by
154 static void if_attachdomain(void *);
155 static void if_attachdomain1(struct ifnet *);
156 static int ifconf(u_long, caddr_t);
157 static void if_freemulti(struct ifmultiaddr *);
158 static void if_init(void *);
159 static void if_grow(void);
160 static void if_route(struct ifnet *, int flag, int fam);
161 static int if_setflag(struct ifnet *, int, int, int *, int);
162 static int if_transmit(struct ifnet *ifp, struct mbuf *m);
163 static void if_unroute(struct ifnet *, int flag, int fam);
164 static void link_rtrequest(int, struct rtentry *, struct rt_addrinfo *);
165 static int if_rtdel(struct radix_node *, void *);
166 static int ifhwioctl(u_long, struct ifnet *, caddr_t, struct thread *);
167 static int if_delmulti_locked(struct ifnet *, struct ifmultiaddr *, int);
168 static void do_link_state_change(void *, int);
169 static int if_getgroup(struct ifgroupreq *, struct ifnet *);
170 static int if_getgroupmembers(struct ifgroupreq *);
171 static void if_delgroups(struct ifnet *);
172 static void if_attach_internal(struct ifnet *, int);
173 static void if_detach_internal(struct ifnet *, int);
177 * XXX: declare here to avoid to include many inet6 related files..
178 * should be more generalized?
180 extern void nd6_setmtu(struct ifnet *);
183 VNET_DEFINE(int, if_index);
184 int ifqmaxlen = IFQ_MAXLEN;
185 VNET_DEFINE(struct ifnethead, ifnet); /* depend on static init XXX */
186 VNET_DEFINE(struct ifgrouphead, ifg_head);
188 static VNET_DEFINE(int, if_indexlim) = 8;
190 /* Table of ifnet by index. */
191 VNET_DEFINE(struct ifindex_entry *, ifindex_table);
193 #define V_if_indexlim VNET(if_indexlim)
194 #define V_ifindex_table VNET(ifindex_table)
197 * The global network interface list (V_ifnet) and related state (such as
198 * if_index, if_indexlim, and ifindex_table) are protected by an sxlock and
199 * an rwlock. Either may be acquired shared to stablize the list, but both
200 * must be acquired writable to modify the list. This model allows us to
201 * both stablize the interface list during interrupt thread processing, but
202 * also to stablize it over long-running ioctls, without introducing priority
203 * inversions and deadlocks.
205 struct rwlock ifnet_rwlock;
206 struct sx ifnet_sxlock;
209 * The allocation of network interfaces is a rather non-atomic affair; we
210 * need to select an index before we are ready to expose the interface for
211 * use, so will use this pointer value to indicate reservation.
213 #define IFNET_HOLD (void *)(uintptr_t)(-1)
215 static if_com_alloc_t *if_com_alloc[256];
216 static if_com_free_t *if_com_free[256];
218 static MALLOC_DEFINE(M_IFNET, "ifnet", "interface internals");
219 MALLOC_DEFINE(M_IFADDR, "ifaddr", "interface address");
220 MALLOC_DEFINE(M_IFMADDR, "ether_multi", "link-level multicast address");
223 ifnet_byindex_locked(u_short idx)
226 if (idx > V_if_index)
228 if (V_ifindex_table[idx].ife_ifnet == IFNET_HOLD)
230 return (V_ifindex_table[idx].ife_ifnet);
234 ifnet_byindex(u_short idx)
238 IFNET_RLOCK_NOSLEEP();
239 ifp = ifnet_byindex_locked(idx);
240 IFNET_RUNLOCK_NOSLEEP();
245 ifnet_byindex_ref(u_short idx)
249 IFNET_RLOCK_NOSLEEP();
250 ifp = ifnet_byindex_locked(idx);
251 if (ifp == NULL || (ifp->if_flags & IFF_DYING)) {
252 IFNET_RUNLOCK_NOSLEEP();
256 IFNET_RUNLOCK_NOSLEEP();
261 * Allocate an ifindex array entry; return 0 on success or an error on
265 ifindex_alloc_locked(u_short *idxp)
269 IFNET_WLOCK_ASSERT();
273 * Try to find an empty slot below V_if_index. If we fail, take the
276 for (idx = 1; idx <= V_if_index; idx++) {
277 if (V_ifindex_table[idx].ife_ifnet == NULL)
281 /* Catch if_index overflow. */
284 if (idx >= V_if_indexlim) {
288 if (idx > V_if_index)
295 ifindex_free_locked(u_short idx)
298 IFNET_WLOCK_ASSERT();
300 V_ifindex_table[idx].ife_ifnet = NULL;
301 while (V_if_index > 0 &&
302 V_ifindex_table[V_if_index].ife_ifnet == NULL)
307 ifindex_free(u_short idx)
311 ifindex_free_locked(idx);
316 ifnet_setbyindex_locked(u_short idx, struct ifnet *ifp)
319 IFNET_WLOCK_ASSERT();
321 V_ifindex_table[idx].ife_ifnet = ifp;
325 ifnet_setbyindex(u_short idx, struct ifnet *ifp)
329 ifnet_setbyindex_locked(idx, ifp);
334 ifaddr_byindex(u_short idx)
338 IFNET_RLOCK_NOSLEEP();
339 ifa = ifnet_byindex_locked(idx)->if_addr;
342 IFNET_RUNLOCK_NOSLEEP();
347 * Network interface utility routines.
349 * Routines with ifa_ifwith* names take sockaddr *'s as
354 vnet_if_init(const void *unused __unused)
357 TAILQ_INIT(&V_ifnet);
358 TAILQ_INIT(&V_ifg_head);
360 if_grow(); /* create initial table */
362 vnet_if_clone_init();
364 VNET_SYSINIT(vnet_if_init, SI_SUB_INIT_IF, SI_ORDER_SECOND, vnet_if_init,
369 if_init(void *dummy __unused)
375 SYSINIT(interfaces, SI_SUB_INIT_IF, SI_ORDER_FIRST, if_init, NULL);
380 vnet_if_uninit(const void *unused __unused)
383 VNET_ASSERT(TAILQ_EMPTY(&V_ifnet), ("%s:%d tailq &V_ifnet=%p "
384 "not empty", __func__, __LINE__, &V_ifnet));
385 VNET_ASSERT(TAILQ_EMPTY(&V_ifg_head), ("%s:%d tailq &V_ifg_head=%p "
386 "not empty", __func__, __LINE__, &V_ifg_head));
388 free((caddr_t)V_ifindex_table, M_IFNET);
390 VNET_SYSUNINIT(vnet_if_uninit, SI_SUB_INIT_IF, SI_ORDER_FIRST,
391 vnet_if_uninit, NULL);
399 struct ifindex_entry *e;
401 IFNET_WLOCK_ASSERT();
402 oldlim = V_if_indexlim;
404 n = (oldlim << 1) * sizeof(*e);
405 e = malloc(n, M_IFNET, M_WAITOK | M_ZERO);
407 if (V_if_indexlim != oldlim) {
411 if (V_ifindex_table != NULL) {
412 memcpy((caddr_t)e, (caddr_t)V_ifindex_table, n/2);
413 free((caddr_t)V_ifindex_table, M_IFNET);
420 * Allocate a struct ifnet and an index for an interface. A layer 2
421 * common structure will also be allocated if an allocation routine is
422 * registered for the passed type.
425 if_alloc(u_char type)
430 ifp = malloc(sizeof(struct ifnet), M_IFNET, M_WAITOK|M_ZERO);
432 if (ifindex_alloc_locked(&idx) != 0) {
437 ifnet_setbyindex_locked(idx, IFNET_HOLD);
441 ifp->if_alloctype = type;
442 if (if_com_alloc[type] != NULL) {
443 ifp->if_l2com = if_com_alloc[type](type, ifp);
444 if (ifp->if_l2com == NULL) {
451 IF_ADDR_LOCK_INIT(ifp);
452 TASK_INIT(&ifp->if_linktask, 0, do_link_state_change, ifp);
453 ifp->if_afdata_initialized = 0;
454 IF_AFDATA_LOCK_INIT(ifp);
455 TAILQ_INIT(&ifp->if_addrhead);
456 TAILQ_INIT(&ifp->if_prefixhead);
457 TAILQ_INIT(&ifp->if_multiaddrs);
458 TAILQ_INIT(&ifp->if_groups);
462 ifq_init(&ifp->if_snd, ifp);
464 refcount_init(&ifp->if_refcount, 1); /* Index reference. */
465 ifnet_setbyindex(ifp->if_index, ifp);
470 * Do the actual work of freeing a struct ifnet, and layer 2 common
471 * structure. This call is made when the last reference to an
472 * interface is released.
475 if_free_internal(struct ifnet *ifp)
478 KASSERT((ifp->if_flags & IFF_DYING),
479 ("if_free_internal: interface not dying"));
481 if (if_com_free[ifp->if_alloctype] != NULL)
482 if_com_free[ifp->if_alloctype](ifp->if_l2com,
486 mac_ifnet_destroy(ifp);
488 if (ifp->if_description != NULL)
489 free(ifp->if_description, M_IFDESCR);
490 IF_AFDATA_DESTROY(ifp);
491 IF_ADDR_LOCK_DESTROY(ifp);
492 ifq_delete(&ifp->if_snd);
497 * This version should only be called by intefaces that switch their type
498 * after calling if_alloc(). if_free_type() will go away again now that we
499 * have if_alloctype to cache the original allocation type. For now, assert
500 * that they match, since we require that in practice.
503 if_free_type(struct ifnet *ifp, u_char type)
506 KASSERT(ifp->if_alloctype == type,
507 ("if_free_type: type (%d) != alloctype (%d)", type,
510 ifp->if_flags |= IFF_DYING; /* XXX: Locking */
513 KASSERT(ifp == ifnet_byindex_locked(ifp->if_index),
514 ("%s: freeing unallocated ifnet", ifp->if_xname));
516 ifindex_free_locked(ifp->if_index);
519 if (!refcount_release(&ifp->if_refcount))
521 if_free_internal(ifp);
525 * This is the normal version of if_free(), used by device drivers to free a
526 * detached network interface. The contents of if_free_type() will move into
527 * here when if_free_type() goes away.
530 if_free(struct ifnet *ifp)
533 if_free_type(ifp, ifp->if_alloctype);
537 * Interfaces to keep an ifnet type-stable despite the possibility of the
538 * driver calling if_free(). If there are additional references, we defer
539 * freeing the underlying data structure.
542 if_ref(struct ifnet *ifp)
545 /* We don't assert the ifnet list lock here, but arguably should. */
546 refcount_acquire(&ifp->if_refcount);
550 if_rele(struct ifnet *ifp)
553 if (!refcount_release(&ifp->if_refcount))
555 if_free_internal(ifp);
559 ifq_init(struct ifaltq *ifq, struct ifnet *ifp)
562 mtx_init(&ifq->ifq_mtx, ifp->if_xname, "if send queue", MTX_DEF);
564 if (ifq->ifq_maxlen == 0)
565 ifq->ifq_maxlen = ifqmaxlen;
568 ifq->altq_disc = NULL;
569 ifq->altq_flags &= ALTQF_CANTCHANGE;
570 ifq->altq_tbr = NULL;
575 ifq_delete(struct ifaltq *ifq)
577 mtx_destroy(&ifq->ifq_mtx);
581 * Perform generic interface initalization tasks and attach the interface
582 * to the list of "active" interfaces. If vmove flag is set on entry
583 * to if_attach_internal(), perform only a limited subset of initialization
584 * tasks, given that we are moving from one vnet to another an ifnet which
585 * has already been fully initialized.
588 * - The decision to return void and thus require this function to
589 * succeed is questionable.
590 * - We should probably do more sanity checking. For instance we don't
591 * do anything to insure if_xname is unique or non-empty.
594 if_attach(struct ifnet *ifp)
597 if_attach_internal(ifp, 0);
601 if_attach_internal(struct ifnet *ifp, int vmove)
603 unsigned socksize, ifasize;
604 int namelen, masklen;
605 struct sockaddr_dl *sdl;
608 if (ifp->if_index == 0 || ifp != ifnet_byindex(ifp->if_index))
609 panic ("%s: BUG: if_attach called without if_alloc'd input()\n",
613 ifp->if_vnet = curvnet;
614 if (ifp->if_home_vnet == NULL)
615 ifp->if_home_vnet = curvnet;
618 if_addgroup(ifp, IFG_ALL);
620 getmicrotime(&ifp->if_lastchange);
621 ifp->if_data.ifi_epoch = time_uptime;
622 ifp->if_data.ifi_datalen = sizeof(struct if_data);
624 KASSERT((ifp->if_transmit == NULL && ifp->if_qflush == NULL) ||
625 (ifp->if_transmit != NULL && ifp->if_qflush != NULL),
626 ("transmit and qflush must both either be set or both be NULL"));
627 if (ifp->if_transmit == NULL) {
628 ifp->if_transmit = if_transmit;
629 ifp->if_qflush = if_qflush;
634 mac_ifnet_create(ifp);
638 * Create a Link Level name for this device.
640 namelen = strlen(ifp->if_xname);
642 * Always save enough space for any possiable name so we
643 * can do a rename in place later.
645 masklen = offsetof(struct sockaddr_dl, sdl_data[0]) + IFNAMSIZ;
646 socksize = masklen + ifp->if_addrlen;
647 if (socksize < sizeof(*sdl))
648 socksize = sizeof(*sdl);
649 socksize = roundup2(socksize, sizeof(long));
650 ifasize = sizeof(*ifa) + 2 * socksize;
651 ifa = malloc(ifasize, M_IFADDR, M_WAITOK | M_ZERO);
653 sdl = (struct sockaddr_dl *)(ifa + 1);
654 sdl->sdl_len = socksize;
655 sdl->sdl_family = AF_LINK;
656 bcopy(ifp->if_xname, sdl->sdl_data, namelen);
657 sdl->sdl_nlen = namelen;
658 sdl->sdl_index = ifp->if_index;
659 sdl->sdl_type = ifp->if_type;
662 ifa->ifa_rtrequest = link_rtrequest;
663 ifa->ifa_addr = (struct sockaddr *)sdl;
664 sdl = (struct sockaddr_dl *)(socksize + (caddr_t)sdl);
665 ifa->ifa_netmask = (struct sockaddr *)sdl;
666 sdl->sdl_len = masklen;
668 sdl->sdl_data[--namelen] = 0xff;
669 TAILQ_INSERT_HEAD(&ifp->if_addrhead, ifa, ifa_link);
670 /* Reliably crash if used uninitialized. */
671 ifp->if_broadcastaddr = NULL;
676 * Update the interface index in the link layer address
679 for (ifa = ifp->if_addr; ifa != NULL;
680 ifa = TAILQ_NEXT(ifa, ifa_link)) {
681 if (ifa->ifa_addr->sa_family == AF_LINK) {
682 sdl = (struct sockaddr_dl *)ifa->ifa_addr;
683 sdl->sdl_index = ifp->if_index;
690 TAILQ_INSERT_TAIL(&V_ifnet, ifp, if_link);
692 curvnet->vnet_ifcnt++;
696 if (domain_init_status >= 2)
697 if_attachdomain1(ifp);
699 EVENTHANDLER_INVOKE(ifnet_arrival_event, ifp);
700 if (IS_DEFAULT_VNET(curvnet))
701 devctl_notify("IFNET", ifp->if_xname, "ATTACH", NULL);
703 /* Announce the interface. */
704 rt_ifannouncemsg(ifp, IFAN_ARRIVAL);
708 if_attachdomain(void *dummy)
714 TAILQ_FOREACH(ifp, &V_ifnet, if_link)
715 if_attachdomain1(ifp);
718 SYSINIT(domainifattach, SI_SUB_PROTO_IFATTACHDOMAIN, SI_ORDER_SECOND,
719 if_attachdomain, NULL);
722 if_attachdomain1(struct ifnet *ifp)
730 * Since dp->dom_ifattach calls malloc() with M_WAITOK, we
731 * cannot lock ifp->if_afdata initialization, entirely.
733 if (IF_AFDATA_TRYLOCK(ifp) == 0) {
737 if (ifp->if_afdata_initialized >= domain_init_status) {
738 IF_AFDATA_UNLOCK(ifp);
740 printf("if_attachdomain called more than once on %s\n",
744 ifp->if_afdata_initialized = domain_init_status;
745 IF_AFDATA_UNLOCK(ifp);
747 /* address family dependent data region */
748 bzero(ifp->if_afdata, sizeof(ifp->if_afdata));
749 for (dp = domains; dp; dp = dp->dom_next) {
750 if (dp->dom_ifattach)
751 ifp->if_afdata[dp->dom_family] =
752 (*dp->dom_ifattach)(ifp);
759 * Remove any unicast or broadcast network addresses from an interface.
762 if_purgeaddrs(struct ifnet *ifp)
764 struct ifaddr *ifa, *next;
766 TAILQ_FOREACH_SAFE(ifa, &ifp->if_addrhead, ifa_link, next) {
767 if (ifa->ifa_addr->sa_family == AF_LINK)
770 /* XXX: Ugly!! ad hoc just for INET */
771 if (ifa->ifa_addr->sa_family == AF_INET) {
772 struct ifaliasreq ifr;
774 bzero(&ifr, sizeof(ifr));
775 ifr.ifra_addr = *ifa->ifa_addr;
776 if (ifa->ifa_dstaddr)
777 ifr.ifra_broadaddr = *ifa->ifa_dstaddr;
778 if (in_control(NULL, SIOCDIFADDR, (caddr_t)&ifr, ifp,
784 if (ifa->ifa_addr->sa_family == AF_INET6) {
786 /* ifp_addrhead is already updated */
790 TAILQ_REMOVE(&ifp->if_addrhead, ifa, ifa_link);
796 * Remove any multicast network addresses from an interface when an ifnet
800 if_purgemaddrs(struct ifnet *ifp)
802 struct ifmultiaddr *ifma;
803 struct ifmultiaddr *next;
806 TAILQ_FOREACH_SAFE(ifma, &ifp->if_multiaddrs, ifma_link, next)
807 if_delmulti_locked(ifp, ifma, 1);
808 IF_ADDR_WUNLOCK(ifp);
812 * Detach an interface, removing it from the list of "active" interfaces.
813 * If vmove flag is set on entry to if_detach_internal(), perform only a
814 * limited subset of cleanup tasks, given that we are moving an ifnet from
815 * one vnet to another, where it must be fully operational.
817 * XXXRW: There are some significant questions about event ordering, and
818 * how to prevent things from starting to use the interface during detach.
821 if_detach(struct ifnet *ifp)
824 if_detach_internal(ifp, 0);
828 if_detach_internal(struct ifnet *ifp, int vmove)
831 struct radix_node_head *rnh;
838 TAILQ_FOREACH(iter, &V_ifnet, if_link)
840 TAILQ_REMOVE(&V_ifnet, ifp, if_link);
846 curvnet->vnet_ifcnt--;
851 panic("%s: ifp=%p not on the ifnet tailq %p",
852 __func__, ifp, &V_ifnet);
854 return; /* XXX this should panic as well? */
858 * Remove/wait for pending events.
860 taskqueue_drain(taskqueue_swi, &ifp->if_linktask);
863 * Remove routes and flush queues.
867 if (ALTQ_IS_ENABLED(&ifp->if_snd))
868 altq_disable(&ifp->if_snd);
869 if (ALTQ_IS_ATTACHED(&ifp->if_snd))
870 altq_detach(&ifp->if_snd);
881 * Remove all IPv6 kernel structs related to ifp. This should be done
882 * before removing routing entries below, since IPv6 interface direct
883 * routes are expected to be removed by the IPv6-specific kernel API.
884 * Otherwise, the kernel will detect some inconsistency and bark it.
892 * Prevent further calls into the device driver via ifnet.
897 * Remove link ifaddr pointer and maybe decrement if_index.
898 * Clean up all addresses.
902 /* We can now free link ifaddr. */
903 if (!TAILQ_EMPTY(&ifp->if_addrhead)) {
904 ifa = TAILQ_FIRST(&ifp->if_addrhead);
905 TAILQ_REMOVE(&ifp->if_addrhead, ifa, ifa_link);
911 * Delete all remaining routes using this interface
912 * Unfortuneatly the only way to do this is to slog through
913 * the entire routing table looking for routes which point
914 * to this interface...oh well...
916 for (i = 1; i <= AF_MAX; i++) {
917 for (j = 0; j < rt_numfibs; j++) {
918 rnh = rt_tables_get_rnh(j, i);
921 RADIX_NODE_HEAD_LOCK(rnh);
922 (void) rnh->rnh_walktree(rnh, if_rtdel, ifp);
923 RADIX_NODE_HEAD_UNLOCK(rnh);
927 /* Announce that the interface is gone. */
928 rt_ifannouncemsg(ifp, IFAN_DEPARTURE);
929 EVENTHANDLER_INVOKE(ifnet_departure_event, ifp);
930 if (IS_DEFAULT_VNET(curvnet))
931 devctl_notify("IFNET", ifp->if_xname, "DETACH", NULL);
935 * We cannot hold the lock over dom_ifdetach calls as they might
936 * sleep, for example trying to drain a callout, thus open up the
937 * theoretical race with re-attaching.
940 i = ifp->if_afdata_initialized;
941 ifp->if_afdata_initialized = 0;
942 IF_AFDATA_UNLOCK(ifp);
943 for (dp = domains; i > 0 && dp; dp = dp->dom_next) {
944 if (dp->dom_ifdetach && ifp->if_afdata[dp->dom_family])
945 (*dp->dom_ifdetach)(ifp,
946 ifp->if_afdata[dp->dom_family]);
952 * if_vmove() performs a limited version of if_detach() in current
953 * vnet and if_attach()es the ifnet to the vnet specified as 2nd arg.
954 * An attempt is made to shrink if_index in current vnet, find an
955 * unused if_index in target vnet and calls if_grow() if necessary,
956 * and finally find an unused if_xname for the target vnet.
959 if_vmove(struct ifnet *ifp, struct vnet *new_vnet)
964 * Detach from current vnet, but preserve LLADDR info, do not
965 * mark as dead etc. so that the ifnet can be reattached later.
967 if_detach_internal(ifp, 1);
970 * Unlink the ifnet from ifindex_table[] in current vnet, and shrink
971 * the if_index for that vnet if possible.
973 * NOTE: IFNET_WLOCK/IFNET_WUNLOCK() are assumed to be unvirtualized,
974 * or we'd lock on one vnet and unlock on another.
977 ifindex_free_locked(ifp->if_index);
981 * Perform interface-specific reassignment tasks, if provided by
984 if (ifp->if_reassign != NULL)
985 ifp->if_reassign(ifp, new_vnet, NULL);
988 * Switch to the context of the target vnet.
990 CURVNET_SET_QUIET(new_vnet);
993 if (ifindex_alloc_locked(&idx) != 0) {
995 panic("if_index overflow");
998 ifnet_setbyindex_locked(ifp->if_index, ifp);
1001 if_attach_internal(ifp, 1);
1007 * Move an ifnet to or from another child prison/vnet, specified by the jail id.
1010 if_vmove_loan(struct thread *td, struct ifnet *ifp, char *ifname, int jid)
1015 /* Try to find the prison within our visibility. */
1016 sx_slock(&allprison_lock);
1017 pr = prison_find_child(td->td_ucred->cr_prison, jid);
1018 sx_sunlock(&allprison_lock);
1021 prison_hold_locked(pr);
1022 mtx_unlock(&pr->pr_mtx);
1024 /* Do not try to move the iface from and to the same prison. */
1025 if (pr->pr_vnet == ifp->if_vnet) {
1030 /* Make sure the named iface does not exists in the dst. prison/vnet. */
1031 /* XXX Lock interfaces to avoid races. */
1032 CURVNET_SET_QUIET(pr->pr_vnet);
1033 difp = ifunit(ifname);
1040 /* Move the interface into the child jail/vnet. */
1041 if_vmove(ifp, pr->pr_vnet);
1043 /* Report the new if_xname back to the userland. */
1044 sprintf(ifname, "%s", ifp->if_xname);
1051 if_vmove_reclaim(struct thread *td, char *ifname, int jid)
1054 struct vnet *vnet_dst;
1057 /* Try to find the prison within our visibility. */
1058 sx_slock(&allprison_lock);
1059 pr = prison_find_child(td->td_ucred->cr_prison, jid);
1060 sx_sunlock(&allprison_lock);
1063 prison_hold_locked(pr);
1064 mtx_unlock(&pr->pr_mtx);
1066 /* Make sure the named iface exists in the source prison/vnet. */
1067 CURVNET_SET(pr->pr_vnet);
1068 ifp = ifunit(ifname); /* XXX Lock to avoid races. */
1075 /* Do not try to move the iface from and to the same prison. */
1076 vnet_dst = TD_TO_VNET(td);
1077 if (vnet_dst == ifp->if_vnet) {
1083 /* Get interface back from child jail/vnet. */
1084 if_vmove(ifp, vnet_dst);
1087 /* Report the new if_xname back to the userland. */
1088 sprintf(ifname, "%s", ifp->if_xname);
1096 * Add a group to an interface
1099 if_addgroup(struct ifnet *ifp, const char *groupname)
1101 struct ifg_list *ifgl;
1102 struct ifg_group *ifg = NULL;
1103 struct ifg_member *ifgm;
1105 if (groupname[0] && groupname[strlen(groupname) - 1] >= '0' &&
1106 groupname[strlen(groupname) - 1] <= '9')
1110 TAILQ_FOREACH(ifgl, &ifp->if_groups, ifgl_next)
1111 if (!strcmp(ifgl->ifgl_group->ifg_group, groupname)) {
1116 if ((ifgl = (struct ifg_list *)malloc(sizeof(struct ifg_list), M_TEMP,
1117 M_NOWAIT)) == NULL) {
1122 if ((ifgm = (struct ifg_member *)malloc(sizeof(struct ifg_member),
1123 M_TEMP, M_NOWAIT)) == NULL) {
1129 TAILQ_FOREACH(ifg, &V_ifg_head, ifg_next)
1130 if (!strcmp(ifg->ifg_group, groupname))
1134 if ((ifg = (struct ifg_group *)malloc(sizeof(struct ifg_group),
1135 M_TEMP, M_NOWAIT)) == NULL) {
1141 strlcpy(ifg->ifg_group, groupname, sizeof(ifg->ifg_group));
1142 ifg->ifg_refcnt = 0;
1143 TAILQ_INIT(&ifg->ifg_members);
1144 EVENTHANDLER_INVOKE(group_attach_event, ifg);
1145 TAILQ_INSERT_TAIL(&V_ifg_head, ifg, ifg_next);
1149 ifgl->ifgl_group = ifg;
1150 ifgm->ifgm_ifp = ifp;
1153 TAILQ_INSERT_TAIL(&ifg->ifg_members, ifgm, ifgm_next);
1154 TAILQ_INSERT_TAIL(&ifp->if_groups, ifgl, ifgl_next);
1155 IF_ADDR_WUNLOCK(ifp);
1159 EVENTHANDLER_INVOKE(group_change_event, groupname);
1165 * Remove a group from an interface
1168 if_delgroup(struct ifnet *ifp, const char *groupname)
1170 struct ifg_list *ifgl;
1171 struct ifg_member *ifgm;
1174 TAILQ_FOREACH(ifgl, &ifp->if_groups, ifgl_next)
1175 if (!strcmp(ifgl->ifgl_group->ifg_group, groupname))
1183 TAILQ_REMOVE(&ifp->if_groups, ifgl, ifgl_next);
1184 IF_ADDR_WUNLOCK(ifp);
1186 TAILQ_FOREACH(ifgm, &ifgl->ifgl_group->ifg_members, ifgm_next)
1187 if (ifgm->ifgm_ifp == ifp)
1191 TAILQ_REMOVE(&ifgl->ifgl_group->ifg_members, ifgm, ifgm_next);
1195 if (--ifgl->ifgl_group->ifg_refcnt == 0) {
1196 TAILQ_REMOVE(&V_ifg_head, ifgl->ifgl_group, ifg_next);
1197 EVENTHANDLER_INVOKE(group_detach_event, ifgl->ifgl_group);
1198 free(ifgl->ifgl_group, M_TEMP);
1204 EVENTHANDLER_INVOKE(group_change_event, groupname);
1210 * Remove an interface from all groups
1213 if_delgroups(struct ifnet *ifp)
1215 struct ifg_list *ifgl;
1216 struct ifg_member *ifgm;
1217 char groupname[IFNAMSIZ];
1220 while (!TAILQ_EMPTY(&ifp->if_groups)) {
1221 ifgl = TAILQ_FIRST(&ifp->if_groups);
1223 strlcpy(groupname, ifgl->ifgl_group->ifg_group, IFNAMSIZ);
1226 TAILQ_REMOVE(&ifp->if_groups, ifgl, ifgl_next);
1227 IF_ADDR_WUNLOCK(ifp);
1229 TAILQ_FOREACH(ifgm, &ifgl->ifgl_group->ifg_members, ifgm_next)
1230 if (ifgm->ifgm_ifp == ifp)
1234 TAILQ_REMOVE(&ifgl->ifgl_group->ifg_members, ifgm,
1239 if (--ifgl->ifgl_group->ifg_refcnt == 0) {
1240 TAILQ_REMOVE(&V_ifg_head, ifgl->ifgl_group, ifg_next);
1241 EVENTHANDLER_INVOKE(group_detach_event,
1243 free(ifgl->ifgl_group, M_TEMP);
1249 EVENTHANDLER_INVOKE(group_change_event, groupname);
1257 * Stores all groups from an interface in memory pointed
1261 if_getgroup(struct ifgroupreq *data, struct ifnet *ifp)
1264 struct ifg_list *ifgl;
1265 struct ifg_req ifgrq, *ifgp;
1266 struct ifgroupreq *ifgr = data;
1268 if (ifgr->ifgr_len == 0) {
1270 TAILQ_FOREACH(ifgl, &ifp->if_groups, ifgl_next)
1271 ifgr->ifgr_len += sizeof(struct ifg_req);
1272 IF_ADDR_RUNLOCK(ifp);
1276 len = ifgr->ifgr_len;
1277 ifgp = ifgr->ifgr_groups;
1280 TAILQ_FOREACH(ifgl, &ifp->if_groups, ifgl_next) {
1281 if (len < sizeof(ifgrq)) {
1282 IF_ADDR_RUNLOCK(ifp);
1285 bzero(&ifgrq, sizeof ifgrq);
1286 strlcpy(ifgrq.ifgrq_group, ifgl->ifgl_group->ifg_group,
1287 sizeof(ifgrq.ifgrq_group));
1288 if ((error = copyout(&ifgrq, ifgp, sizeof(struct ifg_req)))) {
1289 IF_ADDR_RUNLOCK(ifp);
1292 len -= sizeof(ifgrq);
1295 IF_ADDR_RUNLOCK(ifp);
1301 * Stores all members of a group in memory pointed to by data
1304 if_getgroupmembers(struct ifgroupreq *data)
1306 struct ifgroupreq *ifgr = data;
1307 struct ifg_group *ifg;
1308 struct ifg_member *ifgm;
1309 struct ifg_req ifgrq, *ifgp;
1313 TAILQ_FOREACH(ifg, &V_ifg_head, ifg_next)
1314 if (!strcmp(ifg->ifg_group, ifgr->ifgr_name))
1321 if (ifgr->ifgr_len == 0) {
1322 TAILQ_FOREACH(ifgm, &ifg->ifg_members, ifgm_next)
1323 ifgr->ifgr_len += sizeof(ifgrq);
1328 len = ifgr->ifgr_len;
1329 ifgp = ifgr->ifgr_groups;
1330 TAILQ_FOREACH(ifgm, &ifg->ifg_members, ifgm_next) {
1331 if (len < sizeof(ifgrq)) {
1335 bzero(&ifgrq, sizeof ifgrq);
1336 strlcpy(ifgrq.ifgrq_member, ifgm->ifgm_ifp->if_xname,
1337 sizeof(ifgrq.ifgrq_member));
1338 if ((error = copyout(&ifgrq, ifgp, sizeof(struct ifg_req)))) {
1342 len -= sizeof(ifgrq);
1351 * Delete Routes for a Network Interface
1353 * Called for each routing entry via the rnh->rnh_walktree() call above
1354 * to delete all route entries referencing a detaching network interface.
1357 * rn pointer to node in the routing table
1358 * arg argument passed to rnh->rnh_walktree() - detaching interface
1362 * errno failed - reason indicated
1366 if_rtdel(struct radix_node *rn, void *arg)
1368 struct rtentry *rt = (struct rtentry *)rn;
1369 struct ifnet *ifp = arg;
1372 if (rt->rt_ifp == ifp) {
1375 * Protect (sorta) against walktree recursion problems
1376 * with cloned routes
1378 if ((rt->rt_flags & RTF_UP) == 0)
1381 err = rtrequest_fib(RTM_DELETE, rt_key(rt), rt->rt_gateway,
1383 rt->rt_flags|RTF_RNH_LOCKED|RTF_PINNED,
1384 (struct rtentry **) NULL, rt->rt_fibnum);
1386 log(LOG_WARNING, "if_rtdel: error %d\n", err);
1394 * Wrapper functions for struct ifnet address list locking macros. These are
1395 * used by kernel modules to avoid encoding programming interface or binary
1396 * interface assumptions that may be violated when kernel-internal locking
1397 * approaches change.
1400 if_addr_rlock(struct ifnet *ifp)
1407 if_addr_runlock(struct ifnet *ifp)
1410 IF_ADDR_RUNLOCK(ifp);
1414 if_maddr_rlock(struct ifnet *ifp)
1421 if_maddr_runlock(struct ifnet *ifp)
1424 IF_ADDR_RUNLOCK(ifp);
1428 * Reference count functions for ifaddrs.
1431 ifa_init(struct ifaddr *ifa)
1434 mtx_init(&ifa->ifa_mtx, "ifaddr", NULL, MTX_DEF);
1435 refcount_init(&ifa->ifa_refcnt, 1);
1439 ifa_ref(struct ifaddr *ifa)
1442 refcount_acquire(&ifa->ifa_refcnt);
1446 ifa_free(struct ifaddr *ifa)
1449 if (refcount_release(&ifa->ifa_refcnt)) {
1450 mtx_destroy(&ifa->ifa_mtx);
1451 free(ifa, M_IFADDR);
1456 ifa_add_loopback_route(struct ifaddr *ifa, struct sockaddr *ia)
1459 struct rtentry *rt = NULL;
1460 struct rt_addrinfo info;
1461 static struct sockaddr_dl null_sdl = {sizeof(null_sdl), AF_LINK};
1463 bzero(&info, sizeof(info));
1464 info.rti_ifp = V_loif;
1465 info.rti_flags = ifa->ifa_flags | RTF_HOST | RTF_STATIC;
1466 info.rti_info[RTAX_DST] = ia;
1467 info.rti_info[RTAX_GATEWAY] = (struct sockaddr *)&null_sdl;
1468 error = rtrequest1_fib(RTM_ADD, &info, &rt, 0);
1470 if (error == 0 && rt != NULL) {
1472 ((struct sockaddr_dl *)rt->rt_gateway)->sdl_type =
1473 ifa->ifa_ifp->if_type;
1474 ((struct sockaddr_dl *)rt->rt_gateway)->sdl_index =
1475 ifa->ifa_ifp->if_index;
1478 } else if (error != 0)
1479 log(LOG_INFO, "ifa_add_loopback_route: insertion failed\n");
1485 ifa_del_loopback_route(struct ifaddr *ifa, struct sockaddr *ia)
1488 struct rt_addrinfo info;
1489 struct sockaddr_dl null_sdl;
1491 bzero(&null_sdl, sizeof(null_sdl));
1492 null_sdl.sdl_len = sizeof(null_sdl);
1493 null_sdl.sdl_family = AF_LINK;
1494 null_sdl.sdl_type = ifa->ifa_ifp->if_type;
1495 null_sdl.sdl_index = ifa->ifa_ifp->if_index;
1496 bzero(&info, sizeof(info));
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_DELETE, &info, NULL, 0);
1503 log(LOG_INFO, "ifa_del_loopback_route: deletion failed\n");
1509 * XXX: Because sockaddr_dl has deeper structure than the sockaddr
1510 * structs used to represent other address families, it is necessary
1511 * to perform a different comparison.
1514 #define sa_equal(a1, a2) \
1515 (bcmp((a1), (a2), ((a1))->sa_len) == 0)
1517 #define sa_dl_equal(a1, a2) \
1518 ((((struct sockaddr_dl *)(a1))->sdl_len == \
1519 ((struct sockaddr_dl *)(a2))->sdl_len) && \
1520 (bcmp(LLADDR((struct sockaddr_dl *)(a1)), \
1521 LLADDR((struct sockaddr_dl *)(a2)), \
1522 ((struct sockaddr_dl *)(a1))->sdl_alen) == 0))
1525 * Locate an interface based on a complete address.
1528 static struct ifaddr *
1529 ifa_ifwithaddr_internal(struct sockaddr *addr, int getref)
1534 IFNET_RLOCK_NOSLEEP();
1535 TAILQ_FOREACH(ifp, &V_ifnet, if_link) {
1537 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
1538 if (ifa->ifa_addr->sa_family != addr->sa_family)
1540 if (sa_equal(addr, ifa->ifa_addr)) {
1543 IF_ADDR_RUNLOCK(ifp);
1546 /* IP6 doesn't have broadcast */
1547 if ((ifp->if_flags & IFF_BROADCAST) &&
1548 ifa->ifa_broadaddr &&
1549 ifa->ifa_broadaddr->sa_len != 0 &&
1550 sa_equal(ifa->ifa_broadaddr, addr)) {
1553 IF_ADDR_RUNLOCK(ifp);
1557 IF_ADDR_RUNLOCK(ifp);
1561 IFNET_RUNLOCK_NOSLEEP();
1566 ifa_ifwithaddr(struct sockaddr *addr)
1569 return (ifa_ifwithaddr_internal(addr, 1));
1573 ifa_ifwithaddr_check(struct sockaddr *addr)
1576 return (ifa_ifwithaddr_internal(addr, 0) != NULL);
1580 * Locate an interface based on the broadcast address.
1584 ifa_ifwithbroadaddr(struct sockaddr *addr)
1589 IFNET_RLOCK_NOSLEEP();
1590 TAILQ_FOREACH(ifp, &V_ifnet, if_link) {
1592 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
1593 if (ifa->ifa_addr->sa_family != addr->sa_family)
1595 if ((ifp->if_flags & IFF_BROADCAST) &&
1596 ifa->ifa_broadaddr &&
1597 ifa->ifa_broadaddr->sa_len != 0 &&
1598 sa_equal(ifa->ifa_broadaddr, addr)) {
1600 IF_ADDR_RUNLOCK(ifp);
1604 IF_ADDR_RUNLOCK(ifp);
1608 IFNET_RUNLOCK_NOSLEEP();
1613 * Locate the point to point interface with a given destination address.
1617 ifa_ifwithdstaddr(struct sockaddr *addr)
1622 IFNET_RLOCK_NOSLEEP();
1623 TAILQ_FOREACH(ifp, &V_ifnet, if_link) {
1624 if ((ifp->if_flags & IFF_POINTOPOINT) == 0)
1627 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
1628 if (ifa->ifa_addr->sa_family != addr->sa_family)
1630 if (ifa->ifa_dstaddr != NULL &&
1631 sa_equal(addr, ifa->ifa_dstaddr)) {
1633 IF_ADDR_RUNLOCK(ifp);
1637 IF_ADDR_RUNLOCK(ifp);
1641 IFNET_RUNLOCK_NOSLEEP();
1646 * Find an interface on a specific network. If many, choice
1647 * is most specific found.
1650 ifa_ifwithnet(struct sockaddr *addr, int ignore_ptp)
1654 struct ifaddr *ifa_maybe = NULL;
1655 u_int af = addr->sa_family;
1656 char *addr_data = addr->sa_data, *cplim;
1659 * AF_LINK addresses can be looked up directly by their index number,
1660 * so do that if we can.
1662 if (af == AF_LINK) {
1663 struct sockaddr_dl *sdl = (struct sockaddr_dl *)addr;
1664 if (sdl->sdl_index && sdl->sdl_index <= V_if_index)
1665 return (ifaddr_byindex(sdl->sdl_index));
1669 * Scan though each interface, looking for ones that have addresses
1670 * in this address family. Maintain a reference on ifa_maybe once
1671 * we find one, as we release the IF_ADDR_RLOCK() that kept it stable
1672 * when we move onto the next interface.
1674 IFNET_RLOCK_NOSLEEP();
1675 TAILQ_FOREACH(ifp, &V_ifnet, if_link) {
1677 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
1678 char *cp, *cp2, *cp3;
1680 if (ifa->ifa_addr->sa_family != af)
1682 if (af == AF_INET &&
1683 ifp->if_flags & IFF_POINTOPOINT && !ignore_ptp) {
1685 * This is a bit broken as it doesn't
1686 * take into account that the remote end may
1687 * be a single node in the network we are
1689 * The trouble is that we don't know the
1690 * netmask for the remote end.
1692 if (ifa->ifa_dstaddr != NULL &&
1693 sa_equal(addr, ifa->ifa_dstaddr)) {
1695 IF_ADDR_RUNLOCK(ifp);
1700 * if we have a special address handler,
1701 * then use it instead of the generic one.
1703 if (ifa->ifa_claim_addr) {
1704 if ((*ifa->ifa_claim_addr)(ifa, addr)) {
1706 IF_ADDR_RUNLOCK(ifp);
1713 * Scan all the bits in the ifa's address.
1714 * If a bit dissagrees with what we are
1715 * looking for, mask it with the netmask
1716 * to see if it really matters.
1717 * (A byte at a time)
1719 if (ifa->ifa_netmask == 0)
1722 cp2 = ifa->ifa_addr->sa_data;
1723 cp3 = ifa->ifa_netmask->sa_data;
1724 cplim = ifa->ifa_netmask->sa_len
1725 + (char *)ifa->ifa_netmask;
1727 if ((*cp++ ^ *cp2++) & *cp3++)
1728 goto next; /* next address! */
1730 * If the netmask of what we just found
1731 * is more specific than what we had before
1732 * (if we had one) then remember the new one
1733 * before continuing to search
1734 * for an even better one.
1736 if (ifa_maybe == NULL ||
1737 rn_refines((caddr_t)ifa->ifa_netmask,
1738 (caddr_t)ifa_maybe->ifa_netmask)) {
1739 if (ifa_maybe != NULL)
1740 ifa_free(ifa_maybe);
1746 IF_ADDR_RUNLOCK(ifp);
1751 IFNET_RUNLOCK_NOSLEEP();
1752 if (ifa_maybe != NULL)
1753 ifa_free(ifa_maybe);
1758 * Find an interface address specific to an interface best matching
1762 ifaof_ifpforaddr(struct sockaddr *addr, struct ifnet *ifp)
1765 char *cp, *cp2, *cp3;
1767 struct ifaddr *ifa_maybe = NULL;
1768 u_int af = addr->sa_family;
1773 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
1774 if (ifa->ifa_addr->sa_family != af)
1776 if (ifa_maybe == NULL)
1778 if (ifa->ifa_netmask == 0) {
1779 if (sa_equal(addr, ifa->ifa_addr) ||
1780 (ifa->ifa_dstaddr &&
1781 sa_equal(addr, ifa->ifa_dstaddr)))
1785 if (ifp->if_flags & IFF_POINTOPOINT) {
1786 if (sa_equal(addr, ifa->ifa_dstaddr))
1790 cp2 = ifa->ifa_addr->sa_data;
1791 cp3 = ifa->ifa_netmask->sa_data;
1792 cplim = ifa->ifa_netmask->sa_len + (char *)ifa->ifa_netmask;
1793 for (; cp3 < cplim; cp3++)
1794 if ((*cp++ ^ *cp2++) & *cp3)
1804 IF_ADDR_RUNLOCK(ifp);
1808 #include <net/if_llatbl.h>
1811 * Default action when installing a route with a Link Level gateway.
1812 * Lookup an appropriate real ifa to point to.
1813 * This should be moved to /sys/net/link.c eventually.
1816 link_rtrequest(int cmd, struct rtentry *rt, struct rt_addrinfo *info)
1818 struct ifaddr *ifa, *oifa;
1819 struct sockaddr *dst;
1824 if (cmd != RTM_ADD || ((ifa = rt->rt_ifa) == 0) ||
1825 ((ifp = ifa->ifa_ifp) == 0) || ((dst = rt_key(rt)) == 0))
1827 ifa = ifaof_ifpforaddr(dst, ifp);
1832 if (ifa->ifa_rtrequest && ifa->ifa_rtrequest != link_rtrequest)
1833 ifa->ifa_rtrequest(cmd, rt, info);
1838 * Mark an interface down and notify protocols of
1840 * NOTE: must be called at splnet or eqivalent.
1843 if_unroute(struct ifnet *ifp, int flag, int fam)
1847 KASSERT(flag == IFF_UP, ("if_unroute: flag != IFF_UP"));
1849 ifp->if_flags &= ~flag;
1850 getmicrotime(&ifp->if_lastchange);
1851 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link)
1852 if (fam == PF_UNSPEC || (fam == ifa->ifa_addr->sa_family))
1853 pfctlinput(PRC_IFDOWN, ifa->ifa_addr);
1854 ifp->if_qflush(ifp);
1857 (*carp_linkstate_p)(ifp);
1862 * Mark an interface up and notify protocols of
1864 * NOTE: must be called at splnet or eqivalent.
1867 if_route(struct ifnet *ifp, int flag, int fam)
1871 KASSERT(flag == IFF_UP, ("if_route: flag != IFF_UP"));
1873 ifp->if_flags |= flag;
1874 getmicrotime(&ifp->if_lastchange);
1875 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link)
1876 if (fam == PF_UNSPEC || (fam == ifa->ifa_addr->sa_family))
1877 pfctlinput(PRC_IFUP, ifa->ifa_addr);
1879 (*carp_linkstate_p)(ifp);
1886 void (*vlan_link_state_p)(struct ifnet *); /* XXX: private from if_vlan */
1887 void (*vlan_trunk_cap_p)(struct ifnet *); /* XXX: private from if_vlan */
1888 struct ifnet *(*vlan_trunkdev_p)(struct ifnet *);
1889 struct ifnet *(*vlan_devat_p)(struct ifnet *, uint16_t);
1890 int (*vlan_tag_p)(struct ifnet *, uint16_t *);
1891 int (*vlan_setcookie_p)(struct ifnet *, void *);
1892 void *(*vlan_cookie_p)(struct ifnet *);
1895 * Handle a change in the interface link state. To avoid LORs
1896 * between driver lock and upper layer locks, as well as possible
1897 * recursions, we post event to taskqueue, and all job
1898 * is done in static do_link_state_change().
1901 if_link_state_change(struct ifnet *ifp, int link_state)
1903 /* Return if state hasn't changed. */
1904 if (ifp->if_link_state == link_state)
1907 ifp->if_link_state = link_state;
1909 taskqueue_enqueue(taskqueue_swi, &ifp->if_linktask);
1913 do_link_state_change(void *arg, int pending)
1915 struct ifnet *ifp = (struct ifnet *)arg;
1916 int link_state = ifp->if_link_state;
1917 CURVNET_SET(ifp->if_vnet);
1919 /* Notify that the link state has changed. */
1921 if (ifp->if_vlantrunk != NULL)
1922 (*vlan_link_state_p)(ifp);
1924 if ((ifp->if_type == IFT_ETHER || ifp->if_type == IFT_L2VLAN) &&
1925 IFP2AC(ifp)->ac_netgraph != NULL)
1926 (*ng_ether_link_state_p)(ifp, link_state);
1928 (*carp_linkstate_p)(ifp);
1930 (*bridge_linkstate_p)(ifp);
1932 (*lagg_linkstate_p)(ifp, link_state);
1934 if (IS_DEFAULT_VNET(curvnet))
1935 devctl_notify("IFNET", ifp->if_xname,
1936 (link_state == LINK_STATE_UP) ? "LINK_UP" : "LINK_DOWN",
1939 if_printf(ifp, "%d link states coalesced\n", pending);
1940 if (log_link_state_change)
1941 log(LOG_NOTICE, "%s: link state changed to %s\n", ifp->if_xname,
1942 (link_state == LINK_STATE_UP) ? "UP" : "DOWN" );
1943 EVENTHANDLER_INVOKE(ifnet_link_event, ifp, ifp->if_link_state);
1948 * Mark an interface down and notify protocols of
1950 * NOTE: must be called at splnet or eqivalent.
1953 if_down(struct ifnet *ifp)
1956 if_unroute(ifp, IFF_UP, AF_UNSPEC);
1960 * Mark an interface up and notify protocols of
1962 * NOTE: must be called at splnet or eqivalent.
1965 if_up(struct ifnet *ifp)
1968 if_route(ifp, IFF_UP, AF_UNSPEC);
1972 * Flush an interface queue.
1975 if_qflush(struct ifnet *ifp)
1983 if (ALTQ_IS_ENABLED(ifq))
1987 while ((m = n) != 0) {
1998 * Map interface name to interface structure pointer, with or without
1999 * returning a reference.
2002 ifunit_ref(const char *name)
2006 IFNET_RLOCK_NOSLEEP();
2007 TAILQ_FOREACH(ifp, &V_ifnet, if_link) {
2008 if (strncmp(name, ifp->if_xname, IFNAMSIZ) == 0 &&
2009 !(ifp->if_flags & IFF_DYING))
2014 IFNET_RUNLOCK_NOSLEEP();
2019 ifunit(const char *name)
2023 IFNET_RLOCK_NOSLEEP();
2024 TAILQ_FOREACH(ifp, &V_ifnet, if_link) {
2025 if (strncmp(name, ifp->if_xname, IFNAMSIZ) == 0)
2028 IFNET_RUNLOCK_NOSLEEP();
2033 * Hardware specific interface ioctls.
2036 ifhwioctl(u_long cmd, struct ifnet *ifp, caddr_t data, struct thread *td)
2041 int new_flags, temp_flags;
2042 size_t namelen, onamelen;
2044 char *descrbuf, *odescrbuf;
2045 char new_name[IFNAMSIZ];
2047 struct sockaddr_dl *sdl;
2049 ifr = (struct ifreq *)data;
2052 ifr->ifr_index = ifp->if_index;
2056 temp_flags = ifp->if_flags | ifp->if_drv_flags;
2057 ifr->ifr_flags = temp_flags & 0xffff;
2058 ifr->ifr_flagshigh = temp_flags >> 16;
2062 ifr->ifr_reqcap = ifp->if_capabilities;
2063 ifr->ifr_curcap = ifp->if_capenable;
2068 error = mac_ifnet_ioctl_get(td->td_ucred, ifr, ifp);
2073 ifr->ifr_metric = ifp->if_metric;
2077 ifr->ifr_mtu = ifp->if_mtu;
2081 ifr->ifr_phys = ifp->if_physical;
2086 sx_slock(&ifdescr_sx);
2087 if (ifp->if_description == NULL)
2090 /* space for terminating nul */
2091 descrlen = strlen(ifp->if_description) + 1;
2092 if (ifr->ifr_buffer.length < descrlen)
2093 ifr->ifr_buffer.buffer = NULL;
2095 error = copyout(ifp->if_description,
2096 ifr->ifr_buffer.buffer, descrlen);
2097 ifr->ifr_buffer.length = descrlen;
2099 sx_sunlock(&ifdescr_sx);
2103 error = priv_check(td, PRIV_NET_SETIFDESCR);
2108 * Copy only (length-1) bytes to make sure that
2109 * if_description is always nul terminated. The
2110 * length parameter is supposed to count the
2111 * terminating nul in.
2113 if (ifr->ifr_buffer.length > ifdescr_maxlen)
2114 return (ENAMETOOLONG);
2115 else if (ifr->ifr_buffer.length == 0)
2118 descrbuf = malloc(ifr->ifr_buffer.length, M_IFDESCR,
2120 error = copyin(ifr->ifr_buffer.buffer, descrbuf,
2121 ifr->ifr_buffer.length - 1);
2123 free(descrbuf, M_IFDESCR);
2128 sx_xlock(&ifdescr_sx);
2129 odescrbuf = ifp->if_description;
2130 ifp->if_description = descrbuf;
2131 sx_xunlock(&ifdescr_sx);
2133 getmicrotime(&ifp->if_lastchange);
2134 free(odescrbuf, M_IFDESCR);
2138 ifr->ifr_fib = ifp->if_fib;
2142 error = priv_check(td, PRIV_NET_SETIFFIB);
2145 if (ifr->ifr_fib >= rt_numfibs)
2148 ifp->if_fib = ifr->ifr_fib;
2152 error = priv_check(td, PRIV_NET_SETIFFLAGS);
2156 * Currently, no driver owned flags pass the IFF_CANTCHANGE
2157 * check, so we don't need special handling here yet.
2159 new_flags = (ifr->ifr_flags & 0xffff) |
2160 (ifr->ifr_flagshigh << 16);
2161 if (ifp->if_flags & IFF_SMART) {
2162 /* Smart drivers twiddle their own routes */
2163 } else if (ifp->if_flags & IFF_UP &&
2164 (new_flags & IFF_UP) == 0) {
2168 } else if (new_flags & IFF_UP &&
2169 (ifp->if_flags & IFF_UP) == 0) {
2174 /* See if permanently promiscuous mode bit is about to flip */
2175 if ((ifp->if_flags ^ new_flags) & IFF_PPROMISC) {
2176 if (new_flags & IFF_PPROMISC)
2177 ifp->if_flags |= IFF_PROMISC;
2178 else if (ifp->if_pcount == 0)
2179 ifp->if_flags &= ~IFF_PROMISC;
2180 log(LOG_INFO, "%s: permanently promiscuous mode %s\n",
2182 (new_flags & IFF_PPROMISC) ? "enabled" : "disabled");
2184 ifp->if_flags = (ifp->if_flags & IFF_CANTCHANGE) |
2185 (new_flags &~ IFF_CANTCHANGE);
2186 if (ifp->if_ioctl) {
2187 (void) (*ifp->if_ioctl)(ifp, cmd, data);
2189 getmicrotime(&ifp->if_lastchange);
2193 error = priv_check(td, PRIV_NET_SETIFCAP);
2196 if (ifp->if_ioctl == NULL)
2197 return (EOPNOTSUPP);
2198 if (ifr->ifr_reqcap & ~ifp->if_capabilities)
2200 error = (*ifp->if_ioctl)(ifp, cmd, data);
2202 getmicrotime(&ifp->if_lastchange);
2207 error = mac_ifnet_ioctl_set(td->td_ucred, ifr, ifp);
2212 error = priv_check(td, PRIV_NET_SETIFNAME);
2215 error = copyinstr(ifr->ifr_data, new_name, IFNAMSIZ, NULL);
2218 if (new_name[0] == '\0')
2220 if (ifunit(new_name) != NULL)
2224 * XXX: Locking. Nothing else seems to lock if_flags,
2225 * and there are numerous other races with the
2226 * ifunit() checks not being atomic with namespace
2227 * changes (renames, vmoves, if_attach, etc).
2229 ifp->if_flags |= IFF_RENAMING;
2231 /* Announce the departure of the interface. */
2232 rt_ifannouncemsg(ifp, IFAN_DEPARTURE);
2233 EVENTHANDLER_INVOKE(ifnet_departure_event, ifp);
2235 log(LOG_INFO, "%s: changing name to '%s'\n",
2236 ifp->if_xname, new_name);
2238 strlcpy(ifp->if_xname, new_name, sizeof(ifp->if_xname));
2241 sdl = (struct sockaddr_dl *)ifa->ifa_addr;
2242 namelen = strlen(new_name);
2243 onamelen = sdl->sdl_nlen;
2245 * Move the address if needed. This is safe because we
2246 * allocate space for a name of length IFNAMSIZ when we
2247 * create this in if_attach().
2249 if (namelen != onamelen) {
2250 bcopy(sdl->sdl_data + onamelen,
2251 sdl->sdl_data + namelen, sdl->sdl_alen);
2253 bcopy(new_name, sdl->sdl_data, namelen);
2254 sdl->sdl_nlen = namelen;
2255 sdl = (struct sockaddr_dl *)ifa->ifa_netmask;
2256 bzero(sdl->sdl_data, onamelen);
2257 while (namelen != 0)
2258 sdl->sdl_data[--namelen] = 0xff;
2261 EVENTHANDLER_INVOKE(ifnet_arrival_event, ifp);
2262 /* Announce the return of the interface. */
2263 rt_ifannouncemsg(ifp, IFAN_ARRIVAL);
2265 ifp->if_flags &= ~IFF_RENAMING;
2270 error = priv_check(td, PRIV_NET_SETIFVNET);
2273 error = if_vmove_loan(td, ifp, ifr->ifr_name, ifr->ifr_jid);
2278 error = priv_check(td, PRIV_NET_SETIFMETRIC);
2281 ifp->if_metric = ifr->ifr_metric;
2282 getmicrotime(&ifp->if_lastchange);
2286 error = priv_check(td, PRIV_NET_SETIFPHYS);
2289 if (ifp->if_ioctl == NULL)
2290 return (EOPNOTSUPP);
2291 error = (*ifp->if_ioctl)(ifp, cmd, data);
2293 getmicrotime(&ifp->if_lastchange);
2298 u_long oldmtu = ifp->if_mtu;
2300 error = priv_check(td, PRIV_NET_SETIFMTU);
2303 if (ifr->ifr_mtu < IF_MINMTU || ifr->ifr_mtu > IF_MAXMTU)
2305 if (ifp->if_ioctl == NULL)
2306 return (EOPNOTSUPP);
2307 error = (*ifp->if_ioctl)(ifp, cmd, data);
2309 getmicrotime(&ifp->if_lastchange);
2313 * If the link MTU changed, do network layer specific procedure.
2315 if (ifp->if_mtu != oldmtu) {
2325 if (cmd == SIOCADDMULTI)
2326 error = priv_check(td, PRIV_NET_ADDMULTI);
2328 error = priv_check(td, PRIV_NET_DELMULTI);
2332 /* Don't allow group membership on non-multicast interfaces. */
2333 if ((ifp->if_flags & IFF_MULTICAST) == 0)
2334 return (EOPNOTSUPP);
2336 /* Don't let users screw up protocols' entries. */
2337 if (ifr->ifr_addr.sa_family != AF_LINK)
2340 if (cmd == SIOCADDMULTI) {
2341 struct ifmultiaddr *ifma;
2344 * Userland is only permitted to join groups once
2345 * via the if_addmulti() KPI, because it cannot hold
2346 * struct ifmultiaddr * between calls. It may also
2347 * lose a race while we check if the membership
2351 ifma = if_findmulti(ifp, &ifr->ifr_addr);
2352 IF_ADDR_RUNLOCK(ifp);
2356 error = if_addmulti(ifp, &ifr->ifr_addr, &ifma);
2358 error = if_delmulti(ifp, &ifr->ifr_addr);
2361 getmicrotime(&ifp->if_lastchange);
2364 case SIOCSIFPHYADDR:
2365 case SIOCDIFPHYADDR:
2367 case SIOCSIFPHYADDR_IN6:
2369 case SIOCSLIFPHYADDR:
2371 case SIOCSIFGENERIC:
2372 error = priv_check(td, PRIV_NET_HWIOCTL);
2375 if (ifp->if_ioctl == NULL)
2376 return (EOPNOTSUPP);
2377 error = (*ifp->if_ioctl)(ifp, cmd, data);
2379 getmicrotime(&ifp->if_lastchange);
2383 ifs = (struct ifstat *)data;
2384 ifs->ascii[0] = '\0';
2386 case SIOCGIFPSRCADDR:
2387 case SIOCGIFPDSTADDR:
2388 case SIOCGLIFPHYADDR:
2390 case SIOCGIFGENERIC:
2391 if (ifp->if_ioctl == NULL)
2392 return (EOPNOTSUPP);
2393 error = (*ifp->if_ioctl)(ifp, cmd, data);
2397 error = priv_check(td, PRIV_NET_SETLLADDR);
2400 error = if_setlladdr(ifp,
2401 ifr->ifr_addr.sa_data, ifr->ifr_addr.sa_len);
2402 EVENTHANDLER_INVOKE(iflladdr_event, ifp);
2407 struct ifgroupreq *ifgr = (struct ifgroupreq *)ifr;
2409 error = priv_check(td, PRIV_NET_ADDIFGROUP);
2412 if ((error = if_addgroup(ifp, ifgr->ifgr_group)))
2418 if ((error = if_getgroup((struct ifgroupreq *)ifr, ifp)))
2424 struct ifgroupreq *ifgr = (struct ifgroupreq *)ifr;
2426 error = priv_check(td, PRIV_NET_DELIFGROUP);
2429 if ((error = if_delgroup(ifp, ifgr->ifgr_group)))
2441 #ifdef COMPAT_FREEBSD32
2449 #define SIOCGIFCONF32 _IOWR('i', 36, struct ifconf32)
2456 ifioctl(struct socket *so, u_long cmd, caddr_t data, struct thread *td)
2463 CURVNET_SET(so->so_vnet);
2467 error = ifconf(cmd, data);
2471 #ifdef COMPAT_FREEBSD32
2474 struct ifconf32 *ifc32;
2477 ifc32 = (struct ifconf32 *)data;
2478 ifc.ifc_len = ifc32->ifc_len;
2479 ifc.ifc_buf = PTRIN(ifc32->ifc_buf);
2481 error = ifconf(SIOCGIFCONF, (void *)&ifc);
2484 ifc32->ifc_len = ifc.ifc_len;
2489 ifr = (struct ifreq *)data;
2494 error = priv_check(td, PRIV_NET_SETIFVNET);
2496 error = if_vmove_reclaim(td, ifr->ifr_name,
2503 error = priv_check(td, PRIV_NET_IFCREATE);
2505 error = if_clone_create(ifr->ifr_name,
2506 sizeof(ifr->ifr_name),
2507 cmd == SIOCIFCREATE2 ? ifr->ifr_data : NULL);
2511 error = priv_check(td, PRIV_NET_IFDESTROY);
2513 error = if_clone_destroy(ifr->ifr_name);
2517 case SIOCIFGCLONERS:
2518 error = if_clone_list((struct if_clonereq *)data);
2522 error = if_getgroupmembers((struct ifgroupreq *)data);
2527 ifp = ifunit_ref(ifr->ifr_name);
2533 error = ifhwioctl(cmd, ifp, data, td);
2534 if (error != ENOIOCTL) {
2540 oif_flags = ifp->if_flags;
2541 if (so->so_proto == NULL) {
2544 return (EOPNOTSUPP);
2547 error = ((*so->so_proto->pr_usrreqs->pru_control)(so, cmd,
2550 if (error == EOPNOTSUPP && ifp != NULL && ifp->if_ioctl != NULL)
2551 error = (*ifp->if_ioctl)(ifp, cmd, data);
2558 case SIOCSIFDSTADDR:
2560 case SIOCSIFBRDADDR:
2561 case SIOCSIFNETMASK:
2562 #if BYTE_ORDER != BIG_ENDIAN
2563 if (ifr->ifr_addr.sa_family == 0 &&
2564 ifr->ifr_addr.sa_len < 16) {
2565 ifr->ifr_addr.sa_family = ifr->ifr_addr.sa_len;
2566 ifr->ifr_addr.sa_len = 16;
2569 if (ifr->ifr_addr.sa_len == 0)
2570 ifr->ifr_addr.sa_len = 16;
2578 case OSIOCGIFDSTADDR:
2579 cmd = SIOCGIFDSTADDR;
2582 case OSIOCGIFBRDADDR:
2583 cmd = SIOCGIFBRDADDR;
2586 case OSIOCGIFNETMASK:
2587 cmd = SIOCGIFNETMASK;
2589 error = ((*so->so_proto->pr_usrreqs->pru_control)(so,
2593 if (error == EOPNOTSUPP && ifp != NULL &&
2594 ifp->if_ioctl != NULL)
2595 error = (*ifp->if_ioctl)(ifp, cmd, data);
2599 case OSIOCGIFDSTADDR:
2600 case OSIOCGIFBRDADDR:
2601 case OSIOCGIFNETMASK:
2602 *(u_short *)&ifr->ifr_addr = ifr->ifr_addr.sa_family;
2606 #endif /* COMPAT_43 */
2608 if ((oif_flags ^ ifp->if_flags) & IFF_UP) {
2610 if (ifp->if_flags & IFF_UP) {
2623 * The code common to handling reference counted flags,
2624 * e.g., in ifpromisc() and if_allmulti().
2625 * The "pflag" argument can specify a permanent mode flag to check,
2626 * such as IFF_PPROMISC for promiscuous mode; should be 0 if none.
2628 * Only to be used on stack-owned flags, not driver-owned flags.
2631 if_setflag(struct ifnet *ifp, int flag, int pflag, int *refcount, int onswitch)
2635 int oldflags, oldcount;
2637 /* Sanity checks to catch programming errors */
2638 KASSERT((flag & (IFF_DRV_OACTIVE|IFF_DRV_RUNNING)) == 0,
2639 ("%s: setting driver-owned flag %d", __func__, flag));
2642 KASSERT(*refcount >= 0,
2643 ("%s: increment negative refcount %d for flag %d",
2644 __func__, *refcount, flag));
2646 KASSERT(*refcount > 0,
2647 ("%s: decrement non-positive refcount %d for flag %d",
2648 __func__, *refcount, flag));
2650 /* In case this mode is permanent, just touch refcount */
2651 if (ifp->if_flags & pflag) {
2652 *refcount += onswitch ? 1 : -1;
2656 /* Save ifnet parameters for if_ioctl() may fail */
2657 oldcount = *refcount;
2658 oldflags = ifp->if_flags;
2661 * See if we aren't the only and touching refcount is enough.
2662 * Actually toggle interface flag if we are the first or last.
2667 ifp->if_flags |= flag;
2671 ifp->if_flags &= ~flag;
2674 /* Call down the driver since we've changed interface flags */
2675 if (ifp->if_ioctl == NULL) {
2679 ifr.ifr_flags = ifp->if_flags & 0xffff;
2680 ifr.ifr_flagshigh = ifp->if_flags >> 16;
2681 error = (*ifp->if_ioctl)(ifp, SIOCSIFFLAGS, (caddr_t)&ifr);
2684 /* Notify userland that interface flags have changed */
2689 /* Recover after driver error */
2690 *refcount = oldcount;
2691 ifp->if_flags = oldflags;
2696 * Set/clear promiscuous mode on interface ifp based on the truth value
2697 * of pswitch. The calls are reference counted so that only the first
2698 * "on" request actually has an effect, as does the final "off" request.
2699 * Results are undefined if the "off" and "on" requests are not matched.
2702 ifpromisc(struct ifnet *ifp, int pswitch)
2705 int oldflags = ifp->if_flags;
2707 error = if_setflag(ifp, IFF_PROMISC, IFF_PPROMISC,
2708 &ifp->if_pcount, pswitch);
2709 /* If promiscuous mode status has changed, log a message */
2710 if (error == 0 && ((ifp->if_flags ^ oldflags) & IFF_PROMISC))
2711 log(LOG_INFO, "%s: promiscuous mode %s\n",
2713 (ifp->if_flags & IFF_PROMISC) ? "enabled" : "disabled");
2718 * Return interface configuration
2719 * of system. List may be used
2720 * in later ioctl's (above) to get
2721 * other information.
2725 ifconf(u_long cmd, caddr_t data)
2727 struct ifconf *ifc = (struct ifconf *)data;
2732 int error, full = 0, valid_len, max_len;
2734 /* Limit initial buffer size to MAXPHYS to avoid DoS from userspace. */
2735 max_len = MAXPHYS - 1;
2737 /* Prevent hostile input from being able to crash the system */
2738 if (ifc->ifc_len <= 0)
2742 if (ifc->ifc_len <= max_len) {
2743 max_len = ifc->ifc_len;
2746 sb = sbuf_new(NULL, NULL, max_len + 1, SBUF_FIXEDLEN);
2751 TAILQ_FOREACH(ifp, &V_ifnet, if_link) {
2755 * Zero the ifr_name buffer to make sure we don't
2756 * disclose the contents of the stack.
2758 memset(ifr.ifr_name, 0, sizeof(ifr.ifr_name));
2760 if (strlcpy(ifr.ifr_name, ifp->if_xname, sizeof(ifr.ifr_name))
2761 >= sizeof(ifr.ifr_name)) {
2764 return (ENAMETOOLONG);
2769 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
2770 struct sockaddr *sa = ifa->ifa_addr;
2772 if (prison_if(curthread->td_ucred, sa) != 0)
2776 if (cmd == OSIOCGIFCONF) {
2777 struct osockaddr *osa =
2778 (struct osockaddr *)&ifr.ifr_addr;
2780 osa->sa_family = sa->sa_family;
2781 sbuf_bcat(sb, &ifr, sizeof(ifr));
2782 max_len += sizeof(ifr);
2785 if (sa->sa_len <= sizeof(*sa)) {
2787 sbuf_bcat(sb, &ifr, sizeof(ifr));
2788 max_len += sizeof(ifr);
2791 offsetof(struct ifreq, ifr_addr));
2792 max_len += offsetof(struct ifreq, ifr_addr);
2793 sbuf_bcat(sb, sa, sa->sa_len);
2794 max_len += sa->sa_len;
2797 if (sbuf_error(sb) == 0)
2798 valid_len = sbuf_len(sb);
2800 IF_ADDR_RUNLOCK(ifp);
2802 bzero((caddr_t)&ifr.ifr_addr, sizeof(ifr.ifr_addr));
2803 sbuf_bcat(sb, &ifr, sizeof(ifr));
2804 max_len += sizeof(ifr);
2806 if (sbuf_error(sb) == 0)
2807 valid_len = sbuf_len(sb);
2813 * If we didn't allocate enough space (uncommon), try again. If
2814 * we have already allocated as much space as we are allowed,
2815 * return what we've got.
2817 if (valid_len != max_len && !full) {
2822 ifc->ifc_len = valid_len;
2824 error = copyout(sbuf_data(sb), ifc->ifc_req, ifc->ifc_len);
2830 * Just like ifpromisc(), but for all-multicast-reception mode.
2833 if_allmulti(struct ifnet *ifp, int onswitch)
2836 return (if_setflag(ifp, IFF_ALLMULTI, 0, &ifp->if_amcount, onswitch));
2839 struct ifmultiaddr *
2840 if_findmulti(struct ifnet *ifp, struct sockaddr *sa)
2842 struct ifmultiaddr *ifma;
2844 IF_ADDR_LOCK_ASSERT(ifp);
2846 TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
2847 if (sa->sa_family == AF_LINK) {
2848 if (sa_dl_equal(ifma->ifma_addr, sa))
2851 if (sa_equal(ifma->ifma_addr, sa))
2860 * Allocate a new ifmultiaddr and initialize based on passed arguments. We
2861 * make copies of passed sockaddrs. The ifmultiaddr will not be added to
2862 * the ifnet multicast address list here, so the caller must do that and
2863 * other setup work (such as notifying the device driver). The reference
2864 * count is initialized to 1.
2866 static struct ifmultiaddr *
2867 if_allocmulti(struct ifnet *ifp, struct sockaddr *sa, struct sockaddr *llsa,
2870 struct ifmultiaddr *ifma;
2871 struct sockaddr *dupsa;
2873 ifma = malloc(sizeof *ifma, M_IFMADDR, mflags |
2878 dupsa = malloc(sa->sa_len, M_IFMADDR, mflags);
2879 if (dupsa == NULL) {
2880 free(ifma, M_IFMADDR);
2883 bcopy(sa, dupsa, sa->sa_len);
2884 ifma->ifma_addr = dupsa;
2886 ifma->ifma_ifp = ifp;
2887 ifma->ifma_refcount = 1;
2888 ifma->ifma_protospec = NULL;
2891 ifma->ifma_lladdr = NULL;
2895 dupsa = malloc(llsa->sa_len, M_IFMADDR, mflags);
2896 if (dupsa == NULL) {
2897 free(ifma->ifma_addr, M_IFMADDR);
2898 free(ifma, M_IFMADDR);
2901 bcopy(llsa, dupsa, llsa->sa_len);
2902 ifma->ifma_lladdr = dupsa;
2908 * if_freemulti: free ifmultiaddr structure and possibly attached related
2909 * addresses. The caller is responsible for implementing reference
2910 * counting, notifying the driver, handling routing messages, and releasing
2911 * any dependent link layer state.
2914 if_freemulti(struct ifmultiaddr *ifma)
2917 KASSERT(ifma->ifma_refcount == 0, ("if_freemulti: refcount %d",
2918 ifma->ifma_refcount));
2919 KASSERT(ifma->ifma_protospec == NULL,
2920 ("if_freemulti: protospec not NULL"));
2922 if (ifma->ifma_lladdr != NULL)
2923 free(ifma->ifma_lladdr, M_IFMADDR);
2924 free(ifma->ifma_addr, M_IFMADDR);
2925 free(ifma, M_IFMADDR);
2929 * Register an additional multicast address with a network interface.
2931 * - If the address is already present, bump the reference count on the
2932 * address and return.
2933 * - If the address is not link-layer, look up a link layer address.
2934 * - Allocate address structures for one or both addresses, and attach to the
2935 * multicast address list on the interface. If automatically adding a link
2936 * layer address, the protocol address will own a reference to the link
2937 * layer address, to be freed when it is freed.
2938 * - Notify the network device driver of an addition to the multicast address
2941 * 'sa' points to caller-owned memory with the desired multicast address.
2943 * 'retifma' will be used to return a pointer to the resulting multicast
2944 * address reference, if desired.
2947 if_addmulti(struct ifnet *ifp, struct sockaddr *sa,
2948 struct ifmultiaddr **retifma)
2950 struct ifmultiaddr *ifma, *ll_ifma;
2951 struct sockaddr *llsa;
2955 * If the address is already present, return a new reference to it;
2956 * otherwise, allocate storage and set up a new address.
2959 ifma = if_findmulti(ifp, sa);
2961 ifma->ifma_refcount++;
2962 if (retifma != NULL)
2964 IF_ADDR_WUNLOCK(ifp);
2969 * The address isn't already present; resolve the protocol address
2970 * into a link layer address, and then look that up, bump its
2971 * refcount or allocate an ifma for that also. If 'llsa' was
2972 * returned, we will need to free it later.
2976 if (ifp->if_resolvemulti != NULL) {
2977 error = ifp->if_resolvemulti(ifp, &llsa, sa);
2983 * Allocate the new address. Don't hook it up yet, as we may also
2984 * need to allocate a link layer multicast address.
2986 ifma = if_allocmulti(ifp, sa, llsa, M_NOWAIT);
2993 * If a link layer address is found, we'll need to see if it's
2994 * already present in the address list, or allocate is as well.
2995 * When this block finishes, the link layer address will be on the
2999 ll_ifma = if_findmulti(ifp, llsa);
3000 if (ll_ifma == NULL) {
3001 ll_ifma = if_allocmulti(ifp, llsa, NULL, M_NOWAIT);
3002 if (ll_ifma == NULL) {
3003 --ifma->ifma_refcount;
3008 TAILQ_INSERT_HEAD(&ifp->if_multiaddrs, ll_ifma,
3011 ll_ifma->ifma_refcount++;
3012 ifma->ifma_llifma = ll_ifma;
3016 * We now have a new multicast address, ifma, and possibly a new or
3017 * referenced link layer address. Add the primary address to the
3018 * ifnet address list.
3020 TAILQ_INSERT_HEAD(&ifp->if_multiaddrs, ifma, ifma_link);
3022 if (retifma != NULL)
3026 * Must generate the message while holding the lock so that 'ifma'
3027 * pointer is still valid.
3029 rt_newmaddrmsg(RTM_NEWMADDR, ifma);
3030 IF_ADDR_WUNLOCK(ifp);
3033 * We are certain we have added something, so call down to the
3034 * interface to let them know about it.
3036 if (ifp->if_ioctl != NULL) {
3037 (void) (*ifp->if_ioctl)(ifp, SIOCADDMULTI, 0);
3041 free(llsa, M_IFMADDR);
3047 free(llsa, M_IFMADDR);
3050 IF_ADDR_WUNLOCK(ifp);
3055 * Delete a multicast group membership by network-layer group address.
3057 * Returns ENOENT if the entry could not be found. If ifp no longer
3058 * exists, results are undefined. This entry point should only be used
3059 * from subsystems which do appropriate locking to hold ifp for the
3060 * duration of the call.
3061 * Network-layer protocol domains must use if_delmulti_ifma().
3064 if_delmulti(struct ifnet *ifp, struct sockaddr *sa)
3066 struct ifmultiaddr *ifma;
3071 IFNET_RLOCK_NOSLEEP();
3072 TAILQ_FOREACH(oifp, &V_ifnet, if_link)
3077 IFNET_RUNLOCK_NOSLEEP();
3079 KASSERT(ifp != NULL, ("%s: ifnet went away", __func__));
3086 ifma = if_findmulti(ifp, sa);
3088 lastref = if_delmulti_locked(ifp, ifma, 0);
3089 IF_ADDR_WUNLOCK(ifp);
3094 if (lastref && ifp->if_ioctl != NULL) {
3095 (void)(*ifp->if_ioctl)(ifp, SIOCDELMULTI, 0);
3102 * Delete all multicast group membership for an interface.
3103 * Should be used to quickly flush all multicast filters.
3106 if_delallmulti(struct ifnet *ifp)
3108 struct ifmultiaddr *ifma;
3109 struct ifmultiaddr *next;
3112 TAILQ_FOREACH_SAFE(ifma, &ifp->if_multiaddrs, ifma_link, next)
3113 if_delmulti_locked(ifp, ifma, 0);
3114 IF_ADDR_WUNLOCK(ifp);
3118 * Delete a multicast group membership by group membership pointer.
3119 * Network-layer protocol domains must use this routine.
3121 * It is safe to call this routine if the ifp disappeared.
3124 if_delmulti_ifma(struct ifmultiaddr *ifma)
3129 ifp = ifma->ifma_ifp;
3132 printf("%s: ifma_ifp seems to be detached\n", __func__);
3136 IFNET_RLOCK_NOSLEEP();
3137 TAILQ_FOREACH(oifp, &V_ifnet, if_link)
3141 printf("%s: ifnet %p disappeared\n", __func__, ifp);
3144 IFNET_RUNLOCK_NOSLEEP();
3148 * If and only if the ifnet instance exists: Acquire the address lock.
3153 lastref = if_delmulti_locked(ifp, ifma, 0);
3157 * If and only if the ifnet instance exists:
3158 * Release the address lock.
3159 * If the group was left: update the hardware hash filter.
3161 IF_ADDR_WUNLOCK(ifp);
3162 if (lastref && ifp->if_ioctl != NULL) {
3163 (void)(*ifp->if_ioctl)(ifp, SIOCDELMULTI, 0);
3169 * Perform deletion of network-layer and/or link-layer multicast address.
3171 * Return 0 if the reference count was decremented.
3172 * Return 1 if the final reference was released, indicating that the
3173 * hardware hash filter should be reprogrammed.
3176 if_delmulti_locked(struct ifnet *ifp, struct ifmultiaddr *ifma, int detaching)
3178 struct ifmultiaddr *ll_ifma;
3180 if (ifp != NULL && ifma->ifma_ifp != NULL) {
3181 KASSERT(ifma->ifma_ifp == ifp,
3182 ("%s: inconsistent ifp %p", __func__, ifp));
3183 IF_ADDR_WLOCK_ASSERT(ifp);
3186 ifp = ifma->ifma_ifp;
3189 * If the ifnet is detaching, null out references to ifnet,
3190 * so that upper protocol layers will notice, and not attempt
3191 * to obtain locks for an ifnet which no longer exists. The
3192 * routing socket announcement must happen before the ifnet
3193 * instance is detached from the system.
3197 printf("%s: detaching ifnet instance %p\n", __func__, ifp);
3200 * ifp may already be nulled out if we are being reentered
3201 * to delete the ll_ifma.
3204 rt_newmaddrmsg(RTM_DELMADDR, ifma);
3205 ifma->ifma_ifp = NULL;
3209 if (--ifma->ifma_refcount > 0)
3213 * If this ifma is a network-layer ifma, a link-layer ifma may
3214 * have been associated with it. Release it first if so.
3216 ll_ifma = ifma->ifma_llifma;
3217 if (ll_ifma != NULL) {
3218 KASSERT(ifma->ifma_lladdr != NULL,
3219 ("%s: llifma w/o lladdr", __func__));
3221 ll_ifma->ifma_ifp = NULL; /* XXX */
3222 if (--ll_ifma->ifma_refcount == 0) {
3224 TAILQ_REMOVE(&ifp->if_multiaddrs, ll_ifma,
3227 if_freemulti(ll_ifma);
3232 TAILQ_REMOVE(&ifp->if_multiaddrs, ifma, ifma_link);
3237 * The last reference to this instance of struct ifmultiaddr
3238 * was released; the hardware should be notified of this change.
3244 * Set the link layer address on an interface.
3246 * At this time we only support certain types of interfaces,
3247 * and we don't allow the length of the address to change.
3250 if_setlladdr(struct ifnet *ifp, const u_char *lladdr, int len)
3252 struct sockaddr_dl *sdl;
3259 IF_ADDR_RUNLOCK(ifp);
3263 IF_ADDR_RUNLOCK(ifp);
3264 sdl = (struct sockaddr_dl *)ifa->ifa_addr;
3269 if (len != sdl->sdl_alen) { /* don't allow length to change */
3273 switch (ifp->if_type) {
3281 case IFT_IEEE8023ADLAG:
3283 bcopy(lladdr, LLADDR(sdl), len);
3292 * If the interface is already up, we need
3293 * to re-init it in order to reprogram its
3296 if ((ifp->if_flags & IFF_UP) != 0) {
3297 if (ifp->if_ioctl) {
3298 ifp->if_flags &= ~IFF_UP;
3299 ifr.ifr_flags = ifp->if_flags & 0xffff;
3300 ifr.ifr_flagshigh = ifp->if_flags >> 16;
3301 (*ifp->if_ioctl)(ifp, SIOCSIFFLAGS, (caddr_t)&ifr);
3302 ifp->if_flags |= IFF_UP;
3303 ifr.ifr_flags = ifp->if_flags & 0xffff;
3304 ifr.ifr_flagshigh = ifp->if_flags >> 16;
3305 (*ifp->if_ioctl)(ifp, SIOCSIFFLAGS, (caddr_t)&ifr);
3309 * Also send gratuitous ARPs to notify other nodes about
3310 * the address change.
3312 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
3313 if (ifa->ifa_addr->sa_family == AF_INET)
3314 arp_ifinit(ifp, ifa);
3322 * The name argument must be a pointer to storage which will last as
3323 * long as the interface does. For physical devices, the result of
3324 * device_get_name(dev) is a good choice and for pseudo-devices a
3325 * static string works well.
3328 if_initname(struct ifnet *ifp, const char *name, int unit)
3330 ifp->if_dname = name;
3331 ifp->if_dunit = unit;
3332 if (unit != IF_DUNIT_NONE)
3333 snprintf(ifp->if_xname, IFNAMSIZ, "%s%d", name, unit);
3335 strlcpy(ifp->if_xname, name, IFNAMSIZ);
3339 if_printf(struct ifnet *ifp, const char * fmt, ...)
3344 retval = printf("%s: ", ifp->if_xname);
3346 retval += vprintf(fmt, ap);
3352 if_start(struct ifnet *ifp)
3355 (*(ifp)->if_start)(ifp);
3359 * Backwards compatibility interface for drivers
3360 * that have not implemented it
3363 if_transmit(struct ifnet *ifp, struct mbuf *m)
3367 IFQ_HANDOFF(ifp, m, error);
3372 if_handoff(struct ifqueue *ifq, struct mbuf *m, struct ifnet *ifp, int adjust)
3377 if (_IF_QFULL(ifq)) {
3384 ifp->if_obytes += m->m_pkthdr.len + adjust;
3385 if (m->m_flags & (M_BCAST|M_MCAST))
3387 active = ifp->if_drv_flags & IFF_DRV_OACTIVE;
3389 _IF_ENQUEUE(ifq, m);
3391 if (ifp != NULL && !active)
3392 (*(ifp)->if_start)(ifp);
3397 if_register_com_alloc(u_char type,
3398 if_com_alloc_t *a, if_com_free_t *f)
3401 KASSERT(if_com_alloc[type] == NULL,
3402 ("if_register_com_alloc: %d already registered", type));
3403 KASSERT(if_com_free[type] == NULL,
3404 ("if_register_com_alloc: %d free already registered", type));
3406 if_com_alloc[type] = a;
3407 if_com_free[type] = f;
3411 if_deregister_com_alloc(u_char type)
3414 KASSERT(if_com_alloc[type] != NULL,
3415 ("if_deregister_com_alloc: %d not registered", type));
3416 KASSERT(if_com_free[type] != NULL,
3417 ("if_deregister_com_alloc: %d free not registered", type));
3418 if_com_alloc[type] = NULL;
3419 if_com_free[type] = NULL;