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"
39 #include <sys/param.h>
40 #include <sys/types.h>
42 #include <sys/malloc.h>
46 #include <sys/systm.h>
49 #include <sys/socket.h>
50 #include <sys/socketvar.h>
51 #include <sys/protosw.h>
52 #include <sys/kernel.h>
54 #include <sys/refcount.h>
55 #include <sys/module.h>
56 #include <sys/rwlock.h>
57 #include <sys/sockio.h>
58 #include <sys/syslog.h>
59 #include <sys/sysctl.h>
60 #include <sys/taskqueue.h>
61 #include <sys/domain.h>
63 #include <machine/stdarg.h>
71 #include <net/if_arp.h>
72 #include <net/if_clone.h>
73 #include <net/if_dl.h>
74 #include <net/if_types.h>
75 #include <net/if_var.h>
76 #include <net/radix.h>
77 #include <net/route.h>
80 #if defined(INET) || defined(INET6)
82 #include <netinet/in.h>
83 #include <netinet/in_var.h>
85 #include <netinet6/in6_var.h>
86 #include <netinet6/in6_ifattach.h>
90 #include <netinet/if_ether.h>
92 #if defined(INET) || defined(INET6)
94 #include <netinet/ip_carp.h>
98 #include <security/mac/mac_framework.h>
100 struct ifindex_entry {
101 struct ifnet *ife_ifnet;
104 static int slowtimo_started;
106 SYSCTL_NODE(_net, PF_LINK, link, CTLFLAG_RW, 0, "Link layers");
107 SYSCTL_NODE(_net_link, 0, generic, CTLFLAG_RW, 0, "Generic link-management");
109 /* Log link state change events */
110 static int log_link_state_change = 1;
112 SYSCTL_INT(_net_link, OID_AUTO, log_link_state_change, CTLFLAG_RW,
113 &log_link_state_change, 0,
114 "log interface link state change events");
116 /* Interface description */
117 static unsigned int ifdescr_maxlen = 1024;
118 SYSCTL_UINT(_net, OID_AUTO, ifdescr_maxlen, CTLFLAG_RW,
120 "administrative maximum length for interface description");
122 MALLOC_DEFINE(M_IFDESCR, "ifdescr", "ifnet descriptions");
124 /* global sx for non-critical path ifdescr */
125 static struct sx ifdescr_sx;
126 SX_SYSINIT(ifdescr_sx, &ifdescr_sx, "ifnet descr");
128 void (*bstp_linkstate_p)(struct ifnet *ifp, int state);
129 void (*ng_ether_link_state_p)(struct ifnet *ifp, int state);
130 void (*lagg_linkstate_p)(struct ifnet *ifp, int state);
132 struct mbuf *(*tbr_dequeue_ptr)(struct ifaltq *, int) = NULL;
135 * XXX: Style; these should be sorted alphabetically, and unprototyped
136 * static functions should be prototyped. Currently they are sorted by
139 static void if_attachdomain(void *);
140 static void if_attachdomain1(struct ifnet *);
141 static int ifconf(u_long, caddr_t);
142 static void if_freemulti(struct ifmultiaddr *);
143 static void if_init(void *);
144 static void if_grow(void);
145 static void if_check(void *);
146 static void if_route(struct ifnet *, int flag, int fam);
147 static int if_setflag(struct ifnet *, int, int, int *, int);
148 static void if_slowtimo(void *);
149 static int if_transmit(struct ifnet *ifp, struct mbuf *m);
150 static void if_unroute(struct ifnet *, int flag, int fam);
151 static void link_rtrequest(int, struct rtentry *, struct rt_addrinfo *);
152 static int if_rtdel(struct radix_node *, void *);
153 static int ifhwioctl(u_long, struct ifnet *, caddr_t, struct thread *);
154 static int if_delmulti_locked(struct ifnet *, struct ifmultiaddr *, int);
155 static void do_link_state_change(void *, int);
156 static int if_getgroup(struct ifgroupreq *, struct ifnet *);
157 static int if_getgroupmembers(struct ifgroupreq *);
158 static void if_delgroups(struct ifnet *);
159 static void if_attach_internal(struct ifnet *, int);
160 static void if_detach_internal(struct ifnet *, int);
164 * XXX: declare here to avoid to include many inet6 related files..
165 * should be more generalized?
167 extern void nd6_setmtu(struct ifnet *);
170 VNET_DEFINE(int, if_index);
171 int ifqmaxlen = IFQ_MAXLEN;
172 VNET_DEFINE(struct ifnethead, ifnet); /* depend on static init XXX */
173 VNET_DEFINE(struct ifgrouphead, ifg_head);
175 static VNET_DEFINE(int, if_indexlim) = 8;
177 /* Table of ifnet by index. */
178 static VNET_DEFINE(struct ifindex_entry *, ifindex_table);
180 #define V_if_indexlim VNET(if_indexlim)
181 #define V_ifindex_table VNET(ifindex_table)
184 * The global network interface list (V_ifnet) and related state (such as
185 * if_index, if_indexlim, and ifindex_table) are protected by an sxlock and
186 * an rwlock. Either may be acquired shared to stablize the list, but both
187 * must be acquired writable to modify the list. This model allows us to
188 * both stablize the interface list during interrupt thread processing, but
189 * also to stablize it over long-running ioctls, without introducing priority
190 * inversions and deadlocks.
192 struct rwlock ifnet_rwlock;
193 struct sx ifnet_sxlock;
196 * The allocation of network interfaces is a rather non-atomic affair; we
197 * need to select an index before we are ready to expose the interface for
198 * use, so will use this pointer value to indicate reservation.
200 #define IFNET_HOLD (void *)(uintptr_t)(-1)
202 static if_com_alloc_t *if_com_alloc[256];
203 static if_com_free_t *if_com_free[256];
206 * System initialization
208 SYSINIT(interface_check, SI_SUB_PROTO_IF, SI_ORDER_FIRST, if_check, NULL);
210 MALLOC_DEFINE(M_IFNET, "ifnet", "interface internals");
211 MALLOC_DEFINE(M_IFADDR, "ifaddr", "interface address");
212 MALLOC_DEFINE(M_IFMADDR, "ether_multi", "link-level multicast address");
215 ifnet_byindex_locked(u_short idx)
218 if (idx > V_if_index)
220 if (V_ifindex_table[idx].ife_ifnet == IFNET_HOLD)
222 return (V_ifindex_table[idx].ife_ifnet);
226 ifnet_byindex(u_short idx)
230 IFNET_RLOCK_NOSLEEP();
231 ifp = ifnet_byindex_locked(idx);
232 IFNET_RUNLOCK_NOSLEEP();
237 ifnet_byindex_ref(u_short idx)
241 IFNET_RLOCK_NOSLEEP();
242 ifp = ifnet_byindex_locked(idx);
243 if (ifp == NULL || (ifp->if_flags & IFF_DYING)) {
244 IFNET_RUNLOCK_NOSLEEP();
248 IFNET_RUNLOCK_NOSLEEP();
253 * Allocate an ifindex array entry; return 0 on success or an error on
257 ifindex_alloc_locked(u_short *idxp)
261 IFNET_WLOCK_ASSERT();
264 * Try to find an empty slot below V_if_index. If we fail, take the
267 for (idx = 1; idx <= V_if_index; idx++) {
268 if (V_ifindex_table[idx].ife_ifnet == NULL)
272 /* Catch if_index overflow. */
275 if (idx > V_if_index)
277 if (V_if_index >= V_if_indexlim)
284 ifindex_free_locked(u_short idx)
287 IFNET_WLOCK_ASSERT();
289 V_ifindex_table[idx].ife_ifnet = NULL;
290 while (V_if_index > 0 &&
291 V_ifindex_table[V_if_index].ife_ifnet == NULL)
296 ifindex_free(u_short idx)
300 ifindex_free_locked(idx);
305 ifnet_setbyindex_locked(u_short idx, struct ifnet *ifp)
308 IFNET_WLOCK_ASSERT();
310 V_ifindex_table[idx].ife_ifnet = ifp;
314 ifnet_setbyindex(u_short idx, struct ifnet *ifp)
318 ifnet_setbyindex_locked(idx, ifp);
323 ifaddr_byindex(u_short idx)
327 IFNET_RLOCK_NOSLEEP();
328 ifa = ifnet_byindex_locked(idx)->if_addr;
331 IFNET_RUNLOCK_NOSLEEP();
336 * Network interface utility routines.
338 * Routines with ifa_ifwith* names take sockaddr *'s as
343 vnet_if_init(const void *unused __unused)
346 TAILQ_INIT(&V_ifnet);
347 TAILQ_INIT(&V_ifg_head);
348 if_grow(); /* create initial table */
349 vnet_if_clone_init();
351 VNET_SYSINIT(vnet_if_init, SI_SUB_INIT_IF, SI_ORDER_FIRST, vnet_if_init,
356 if_init(void *dummy __unused)
362 SYSINIT(interfaces, SI_SUB_INIT_IF, SI_ORDER_SECOND, if_init, NULL);
367 vnet_if_uninit(const void *unused __unused)
370 VNET_ASSERT(TAILQ_EMPTY(&V_ifnet));
371 VNET_ASSERT(TAILQ_EMPTY(&V_ifg_head));
373 free((caddr_t)V_ifindex_table, M_IFNET);
375 VNET_SYSUNINIT(vnet_if_uninit, SI_SUB_INIT_IF, SI_ORDER_FIRST,
376 vnet_if_uninit, NULL);
383 struct ifindex_entry *e;
386 n = V_if_indexlim * sizeof(*e);
387 e = malloc(n, M_IFNET, M_WAITOK | M_ZERO);
388 if (V_ifindex_table != NULL) {
389 memcpy((caddr_t)e, (caddr_t)V_ifindex_table, n/2);
390 free((caddr_t)V_ifindex_table, M_IFNET);
396 if_check(void *dummy __unused)
400 * If at least one interface added during boot uses
401 * if_watchdog then start the timer.
403 if (slowtimo_started)
408 * Allocate a struct ifnet and an index for an interface. A layer 2
409 * common structure will also be allocated if an allocation routine is
410 * registered for the passed type.
413 if_alloc(u_char type)
418 ifp = malloc(sizeof(struct ifnet), M_IFNET, M_WAITOK|M_ZERO);
420 if (ifindex_alloc_locked(&idx) != 0) {
425 ifnet_setbyindex_locked(idx, IFNET_HOLD);
429 ifp->if_alloctype = type;
430 if (if_com_alloc[type] != NULL) {
431 ifp->if_l2com = if_com_alloc[type](type, ifp);
432 if (ifp->if_l2com == NULL) {
439 IF_ADDR_LOCK_INIT(ifp);
440 TASK_INIT(&ifp->if_linktask, 0, do_link_state_change, ifp);
441 ifp->if_afdata_initialized = 0;
442 IF_AFDATA_LOCK_INIT(ifp);
443 TAILQ_INIT(&ifp->if_addrhead);
444 TAILQ_INIT(&ifp->if_prefixhead);
445 TAILQ_INIT(&ifp->if_multiaddrs);
446 TAILQ_INIT(&ifp->if_groups);
450 ifq_init(&ifp->if_snd, ifp);
452 refcount_init(&ifp->if_refcount, 1); /* Index reference. */
453 ifnet_setbyindex(ifp->if_index, ifp);
458 * Do the actual work of freeing a struct ifnet, associated index, and layer
459 * 2 common structure. This call is made when the last reference to an
460 * interface is released.
463 if_free_internal(struct ifnet *ifp)
466 KASSERT((ifp->if_flags & IFF_DYING),
467 ("if_free_internal: interface not dying"));
470 KASSERT(ifp == ifnet_byindex_locked(ifp->if_index),
471 ("%s: freeing unallocated ifnet", ifp->if_xname));
473 ifindex_free_locked(ifp->if_index);
476 if (if_com_free[ifp->if_alloctype] != NULL)
477 if_com_free[ifp->if_alloctype](ifp->if_l2com,
481 mac_ifnet_destroy(ifp);
483 if (ifp->if_description != NULL)
484 free(ifp->if_description, M_IFDESCR);
485 IF_AFDATA_DESTROY(ifp);
486 IF_ADDR_LOCK_DESTROY(ifp);
487 ifq_delete(&ifp->if_snd);
492 * This version should only be called by intefaces that switch their type
493 * after calling if_alloc(). if_free_type() will go away again now that we
494 * have if_alloctype to cache the original allocation type. For now, assert
495 * that they match, since we require that in practice.
498 if_free_type(struct ifnet *ifp, u_char type)
501 KASSERT(ifp->if_alloctype == type,
502 ("if_free_type: type (%d) != alloctype (%d)", type,
505 ifp->if_flags |= IFF_DYING; /* XXX: Locking */
506 if (!refcount_release(&ifp->if_refcount))
508 if_free_internal(ifp);
512 * This is the normal version of if_free(), used by device drivers to free a
513 * detached network interface. The contents of if_free_type() will move into
514 * here when if_free_type() goes away.
517 if_free(struct ifnet *ifp)
520 if_free_type(ifp, ifp->if_alloctype);
524 * Interfaces to keep an ifnet type-stable despite the possibility of the
525 * driver calling if_free(). If there are additional references, we defer
526 * freeing the underlying data structure.
529 if_ref(struct ifnet *ifp)
532 /* We don't assert the ifnet list lock here, but arguably should. */
533 refcount_acquire(&ifp->if_refcount);
537 if_rele(struct ifnet *ifp)
540 if (!refcount_release(&ifp->if_refcount))
542 if_free_internal(ifp);
546 ifq_init(struct ifaltq *ifq, struct ifnet *ifp)
549 mtx_init(&ifq->ifq_mtx, ifp->if_xname, "if send queue", MTX_DEF);
551 if (ifq->ifq_maxlen == 0)
552 ifq->ifq_maxlen = ifqmaxlen;
555 ifq->altq_disc = NULL;
556 ifq->altq_flags &= ALTQF_CANTCHANGE;
557 ifq->altq_tbr = NULL;
562 ifq_delete(struct ifaltq *ifq)
564 mtx_destroy(&ifq->ifq_mtx);
568 * Perform generic interface initalization tasks and attach the interface
569 * to the list of "active" interfaces. If vmove flag is set on entry
570 * to if_attach_internal(), perform only a limited subset of initialization
571 * tasks, given that we are moving from one vnet to another an ifnet which
572 * has already been fully initialized.
575 * - The decision to return void and thus require this function to
576 * succeed is questionable.
577 * - We should probably do more sanity checking. For instance we don't
578 * do anything to insure if_xname is unique or non-empty.
581 if_attach(struct ifnet *ifp)
584 if_attach_internal(ifp, 0);
588 if_attach_internal(struct ifnet *ifp, int vmove)
590 unsigned socksize, ifasize;
591 int namelen, masklen;
592 struct sockaddr_dl *sdl;
595 if (ifp->if_index == 0 || ifp != ifnet_byindex(ifp->if_index))
596 panic ("%s: BUG: if_attach called without if_alloc'd input()\n",
600 ifp->if_vnet = curvnet;
601 if (ifp->if_home_vnet == NULL)
602 ifp->if_home_vnet = curvnet;
605 if_addgroup(ifp, IFG_ALL);
607 getmicrotime(&ifp->if_lastchange);
608 ifp->if_data.ifi_epoch = time_uptime;
609 ifp->if_data.ifi_datalen = sizeof(struct if_data);
611 KASSERT((ifp->if_transmit == NULL && ifp->if_qflush == NULL) ||
612 (ifp->if_transmit != NULL && ifp->if_qflush != NULL),
613 ("transmit and qflush must both either be set or both be NULL"));
614 if (ifp->if_transmit == NULL) {
615 ifp->if_transmit = if_transmit;
616 ifp->if_qflush = if_qflush;
621 mac_ifnet_create(ifp);
625 * Create a Link Level name for this device.
627 namelen = strlen(ifp->if_xname);
629 * Always save enough space for any possiable name so we
630 * can do a rename in place later.
632 masklen = offsetof(struct sockaddr_dl, sdl_data[0]) + IFNAMSIZ;
633 socksize = masklen + ifp->if_addrlen;
634 if (socksize < sizeof(*sdl))
635 socksize = sizeof(*sdl);
636 socksize = roundup2(socksize, sizeof(long));
637 ifasize = sizeof(*ifa) + 2 * socksize;
638 ifa = malloc(ifasize, M_IFADDR, M_WAITOK | M_ZERO);
640 sdl = (struct sockaddr_dl *)(ifa + 1);
641 sdl->sdl_len = socksize;
642 sdl->sdl_family = AF_LINK;
643 bcopy(ifp->if_xname, sdl->sdl_data, namelen);
644 sdl->sdl_nlen = namelen;
645 sdl->sdl_index = ifp->if_index;
646 sdl->sdl_type = ifp->if_type;
649 ifa->ifa_rtrequest = link_rtrequest;
650 ifa->ifa_addr = (struct sockaddr *)sdl;
651 sdl = (struct sockaddr_dl *)(socksize + (caddr_t)sdl);
652 ifa->ifa_netmask = (struct sockaddr *)sdl;
653 sdl->sdl_len = masklen;
655 sdl->sdl_data[--namelen] = 0xff;
656 TAILQ_INSERT_HEAD(&ifp->if_addrhead, ifa, ifa_link);
657 /* Reliably crash if used uninitialized. */
658 ifp->if_broadcastaddr = NULL;
663 * Update the interface index in the link layer address
666 for (ifa = ifp->if_addr; ifa != NULL;
667 ifa = TAILQ_NEXT(ifa, ifa_link)) {
668 if (ifa->ifa_addr->sa_family == AF_LINK) {
669 sdl = (struct sockaddr_dl *)ifa->ifa_addr;
670 sdl->sdl_index = ifp->if_index;
677 TAILQ_INSERT_TAIL(&V_ifnet, ifp, if_link);
679 curvnet->vnet_ifcnt++;
683 if (domain_init_status >= 2)
684 if_attachdomain1(ifp);
686 EVENTHANDLER_INVOKE(ifnet_arrival_event, ifp);
687 if (IS_DEFAULT_VNET(curvnet))
688 devctl_notify("IFNET", ifp->if_xname, "ATTACH", NULL);
690 /* Announce the interface. */
691 rt_ifannouncemsg(ifp, IFAN_ARRIVAL);
693 if (!vmove && ifp->if_watchdog != NULL) {
695 "WARNING: using obsoleted if_watchdog interface\n");
698 * Note that we need if_slowtimo(). If this happens after
699 * boot, then call if_slowtimo() directly.
701 if (atomic_cmpset_int(&slowtimo_started, 0, 1) && !cold)
707 if_attachdomain(void *dummy)
713 TAILQ_FOREACH(ifp, &V_ifnet, if_link)
714 if_attachdomain1(ifp);
717 SYSINIT(domainifattach, SI_SUB_PROTO_IFATTACHDOMAIN, SI_ORDER_SECOND,
718 if_attachdomain, NULL);
721 if_attachdomain1(struct ifnet *ifp)
729 * Since dp->dom_ifattach calls malloc() with M_WAITOK, we
730 * cannot lock ifp->if_afdata initialization, entirely.
732 if (IF_AFDATA_TRYLOCK(ifp) == 0) {
736 if (ifp->if_afdata_initialized >= domain_init_status) {
737 IF_AFDATA_UNLOCK(ifp);
739 printf("if_attachdomain called more than once on %s\n",
743 ifp->if_afdata_initialized = domain_init_status;
744 IF_AFDATA_UNLOCK(ifp);
746 /* address family dependent data region */
747 bzero(ifp->if_afdata, sizeof(ifp->if_afdata));
748 for (dp = domains; dp; dp = dp->dom_next) {
749 if (dp->dom_ifattach)
750 ifp->if_afdata[dp->dom_family] =
751 (*dp->dom_ifattach)(ifp);
758 * Remove any unicast or broadcast network addresses from an interface.
761 if_purgeaddrs(struct ifnet *ifp)
763 struct ifaddr *ifa, *next;
765 TAILQ_FOREACH_SAFE(ifa, &ifp->if_addrhead, ifa_link, next) {
766 if (ifa->ifa_addr->sa_family == AF_LINK)
769 /* XXX: Ugly!! ad hoc just for INET */
770 if (ifa->ifa_addr->sa_family == AF_INET) {
771 struct ifaliasreq ifr;
773 bzero(&ifr, sizeof(ifr));
774 ifr.ifra_addr = *ifa->ifa_addr;
775 if (ifa->ifa_dstaddr)
776 ifr.ifra_broadaddr = *ifa->ifa_dstaddr;
777 if (in_control(NULL, SIOCDIFADDR, (caddr_t)&ifr, ifp,
783 if (ifa->ifa_addr->sa_family == AF_INET6) {
785 /* ifp_addrhead is already updated */
789 TAILQ_REMOVE(&ifp->if_addrhead, ifa, ifa_link);
795 * Remove any multicast network addresses from an interface when an ifnet
799 if_purgemaddrs(struct ifnet *ifp)
801 struct ifmultiaddr *ifma;
802 struct ifmultiaddr *next;
805 TAILQ_FOREACH_SAFE(ifma, &ifp->if_multiaddrs, ifma_link, next)
806 if_delmulti_locked(ifp, ifma, 1);
811 * Detach an interface, removing it from the list of "active" interfaces.
812 * If vmove flag is set on entry to if_detach_internal(), perform only a
813 * limited subset of cleanup tasks, given that we are moving an ifnet from
814 * one vnet to another, where it must be fully operational.
816 * XXXRW: There are some significant questions about event ordering, and
817 * how to prevent things from starting to use the interface during detach.
820 if_detach(struct ifnet *ifp)
823 if_detach_internal(ifp, 0);
827 if_detach_internal(struct ifnet *ifp, int vmove)
830 struct radix_node_head *rnh;
837 TAILQ_FOREACH(iter, &V_ifnet, if_link)
839 TAILQ_REMOVE(&V_ifnet, ifp, if_link);
845 curvnet->vnet_ifcnt--;
850 panic("%s: ifp=%p not on the ifnet tailq %p",
851 __func__, ifp, &V_ifnet);
853 return; /* XXX this should panic as well? */
857 * Remove/wait for pending events.
859 taskqueue_drain(taskqueue_swi, &ifp->if_linktask);
862 * Remove routes and flush queues.
866 if (ALTQ_IS_ENABLED(&ifp->if_snd))
867 altq_disable(&ifp->if_snd);
868 if (ALTQ_IS_ATTACHED(&ifp->if_snd))
869 altq_detach(&ifp->if_snd);
880 * Remove all IPv6 kernel structs related to ifp. This should be done
881 * before removing routing entries below, since IPv6 interface direct
882 * routes are expected to be removed by the IPv6-specific kernel API.
883 * Otherwise, the kernel will detect some inconsistency and bark it.
891 * Prevent further calls into the device driver via ifnet.
896 * Remove link ifaddr pointer and maybe decrement if_index.
897 * Clean up all addresses.
901 /* We can now free link ifaddr. */
902 if (!TAILQ_EMPTY(&ifp->if_addrhead)) {
903 ifa = TAILQ_FIRST(&ifp->if_addrhead);
904 TAILQ_REMOVE(&ifp->if_addrhead, ifa, ifa_link);
910 * Delete all remaining routes using this interface
911 * Unfortuneatly the only way to do this is to slog through
912 * the entire routing table looking for routes which point
913 * to this interface...oh well...
915 for (i = 1; i <= AF_MAX; i++) {
916 for (j = 0; j < rt_numfibs; j++) {
917 rnh = rt_tables_get_rnh(j, i);
920 RADIX_NODE_HEAD_LOCK(rnh);
921 (void) rnh->rnh_walktree(rnh, if_rtdel, ifp);
922 RADIX_NODE_HEAD_UNLOCK(rnh);
926 /* Announce that the interface is gone. */
927 rt_ifannouncemsg(ifp, IFAN_DEPARTURE);
928 EVENTHANDLER_INVOKE(ifnet_departure_event, ifp);
929 if (IS_DEFAULT_VNET(curvnet))
930 devctl_notify("IFNET", ifp->if_xname, "DETACH", NULL);
934 * We cannot hold the lock over dom_ifdetach calls as they might
935 * sleep, for example trying to drain a callout, thus open up the
936 * theoretical race with re-attaching.
939 i = ifp->if_afdata_initialized;
940 ifp->if_afdata_initialized = 0;
941 IF_AFDATA_UNLOCK(ifp);
942 for (dp = domains; i > 0 && dp; dp = dp->dom_next) {
943 if (dp->dom_ifdetach && ifp->if_afdata[dp->dom_family])
944 (*dp->dom_ifdetach)(ifp,
945 ifp->if_afdata[dp->dom_family]);
951 * if_vmove() performs a limited version of if_detach() in current
952 * vnet and if_attach()es the ifnet to the vnet specified as 2nd arg.
953 * An attempt is made to shrink if_index in current vnet, find an
954 * unused if_index in target vnet and calls if_grow() if necessary,
955 * and finally find an unused if_xname for the target vnet.
958 if_vmove(struct ifnet *ifp, struct vnet *new_vnet)
963 * Detach from current vnet, but preserve LLADDR info, do not
964 * mark as dead etc. so that the ifnet can be reattached later.
966 if_detach_internal(ifp, 1);
969 * Unlink the ifnet from ifindex_table[] in current vnet, and shrink
970 * the if_index for that vnet if possible.
972 * NOTE: IFNET_WLOCK/IFNET_WUNLOCK() are assumed to be unvirtualized,
973 * or we'd lock on one vnet and unlock on another.
976 ifindex_free_locked(ifp->if_index);
979 * Switch to the context of the target vnet.
981 CURVNET_SET_QUIET(new_vnet);
983 if (ifindex_alloc_locked(&idx) != 0) {
985 panic("if_index overflow");
988 ifnet_setbyindex_locked(ifp->if_index, ifp);
991 if_attach_internal(ifp, 1);
997 * Move an ifnet to or from another child prison/vnet, specified by the jail id.
1000 if_vmove_loan(struct thread *td, struct ifnet *ifp, char *ifname, int jid)
1005 /* Try to find the prison within our visibility. */
1006 sx_slock(&allprison_lock);
1007 pr = prison_find_child(td->td_ucred->cr_prison, jid);
1008 sx_sunlock(&allprison_lock);
1011 prison_hold_locked(pr);
1012 mtx_unlock(&pr->pr_mtx);
1014 /* Do not try to move the iface from and to the same prison. */
1015 if (pr->pr_vnet == ifp->if_vnet) {
1020 /* Make sure the named iface does not exists in the dst. prison/vnet. */
1021 /* XXX Lock interfaces to avoid races. */
1022 CURVNET_SET_QUIET(pr->pr_vnet);
1023 difp = ifunit(ifname);
1030 /* Move the interface into the child jail/vnet. */
1031 if_vmove(ifp, pr->pr_vnet);
1033 /* Report the new if_xname back to the userland. */
1034 sprintf(ifname, "%s", ifp->if_xname);
1041 if_vmove_reclaim(struct thread *td, char *ifname, int jid)
1044 struct vnet *vnet_dst;
1047 /* Try to find the prison within our visibility. */
1048 sx_slock(&allprison_lock);
1049 pr = prison_find_child(td->td_ucred->cr_prison, jid);
1050 sx_sunlock(&allprison_lock);
1053 prison_hold_locked(pr);
1054 mtx_unlock(&pr->pr_mtx);
1056 /* Make sure the named iface exists in the source prison/vnet. */
1057 CURVNET_SET(pr->pr_vnet);
1058 ifp = ifunit(ifname); /* XXX Lock to avoid races. */
1065 /* Do not try to move the iface from and to the same prison. */
1066 vnet_dst = TD_TO_VNET(td);
1067 if (vnet_dst == ifp->if_vnet) {
1073 /* Get interface back from child jail/vnet. */
1074 if_vmove(ifp, vnet_dst);
1077 /* Report the new if_xname back to the userland. */
1078 sprintf(ifname, "%s", ifp->if_xname);
1086 * Add a group to an interface
1089 if_addgroup(struct ifnet *ifp, const char *groupname)
1091 struct ifg_list *ifgl;
1092 struct ifg_group *ifg = NULL;
1093 struct ifg_member *ifgm;
1095 if (groupname[0] && groupname[strlen(groupname) - 1] >= '0' &&
1096 groupname[strlen(groupname) - 1] <= '9')
1100 TAILQ_FOREACH(ifgl, &ifp->if_groups, ifgl_next)
1101 if (!strcmp(ifgl->ifgl_group->ifg_group, groupname)) {
1106 if ((ifgl = (struct ifg_list *)malloc(sizeof(struct ifg_list), M_TEMP,
1107 M_NOWAIT)) == NULL) {
1112 if ((ifgm = (struct ifg_member *)malloc(sizeof(struct ifg_member),
1113 M_TEMP, M_NOWAIT)) == NULL) {
1119 TAILQ_FOREACH(ifg, &V_ifg_head, ifg_next)
1120 if (!strcmp(ifg->ifg_group, groupname))
1124 if ((ifg = (struct ifg_group *)malloc(sizeof(struct ifg_group),
1125 M_TEMP, M_NOWAIT)) == NULL) {
1131 strlcpy(ifg->ifg_group, groupname, sizeof(ifg->ifg_group));
1132 ifg->ifg_refcnt = 0;
1133 TAILQ_INIT(&ifg->ifg_members);
1134 EVENTHANDLER_INVOKE(group_attach_event, ifg);
1135 TAILQ_INSERT_TAIL(&V_ifg_head, ifg, ifg_next);
1139 ifgl->ifgl_group = ifg;
1140 ifgm->ifgm_ifp = ifp;
1143 TAILQ_INSERT_TAIL(&ifg->ifg_members, ifgm, ifgm_next);
1144 TAILQ_INSERT_TAIL(&ifp->if_groups, ifgl, ifgl_next);
1145 IF_ADDR_UNLOCK(ifp);
1149 EVENTHANDLER_INVOKE(group_change_event, groupname);
1155 * Remove a group from an interface
1158 if_delgroup(struct ifnet *ifp, const char *groupname)
1160 struct ifg_list *ifgl;
1161 struct ifg_member *ifgm;
1164 TAILQ_FOREACH(ifgl, &ifp->if_groups, ifgl_next)
1165 if (!strcmp(ifgl->ifgl_group->ifg_group, groupname))
1173 TAILQ_REMOVE(&ifp->if_groups, ifgl, ifgl_next);
1174 IF_ADDR_UNLOCK(ifp);
1176 TAILQ_FOREACH(ifgm, &ifgl->ifgl_group->ifg_members, ifgm_next)
1177 if (ifgm->ifgm_ifp == ifp)
1181 TAILQ_REMOVE(&ifgl->ifgl_group->ifg_members, ifgm, ifgm_next);
1185 if (--ifgl->ifgl_group->ifg_refcnt == 0) {
1186 TAILQ_REMOVE(&V_ifg_head, ifgl->ifgl_group, ifg_next);
1187 EVENTHANDLER_INVOKE(group_detach_event, ifgl->ifgl_group);
1188 free(ifgl->ifgl_group, M_TEMP);
1194 EVENTHANDLER_INVOKE(group_change_event, groupname);
1200 * Remove an interface from all groups
1203 if_delgroups(struct ifnet *ifp)
1205 struct ifg_list *ifgl;
1206 struct ifg_member *ifgm;
1207 char groupname[IFNAMSIZ];
1210 while (!TAILQ_EMPTY(&ifp->if_groups)) {
1211 ifgl = TAILQ_FIRST(&ifp->if_groups);
1213 strlcpy(groupname, ifgl->ifgl_group->ifg_group, IFNAMSIZ);
1216 TAILQ_REMOVE(&ifp->if_groups, ifgl, ifgl_next);
1217 IF_ADDR_UNLOCK(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,
1229 if (--ifgl->ifgl_group->ifg_refcnt == 0) {
1230 TAILQ_REMOVE(&V_ifg_head, ifgl->ifgl_group, ifg_next);
1231 EVENTHANDLER_INVOKE(group_detach_event,
1233 free(ifgl->ifgl_group, M_TEMP);
1239 EVENTHANDLER_INVOKE(group_change_event, groupname);
1247 * Stores all groups from an interface in memory pointed
1251 if_getgroup(struct ifgroupreq *data, struct ifnet *ifp)
1254 struct ifg_list *ifgl;
1255 struct ifg_req ifgrq, *ifgp;
1256 struct ifgroupreq *ifgr = data;
1258 if (ifgr->ifgr_len == 0) {
1260 TAILQ_FOREACH(ifgl, &ifp->if_groups, ifgl_next)
1261 ifgr->ifgr_len += sizeof(struct ifg_req);
1262 IF_ADDR_UNLOCK(ifp);
1266 len = ifgr->ifgr_len;
1267 ifgp = ifgr->ifgr_groups;
1270 TAILQ_FOREACH(ifgl, &ifp->if_groups, ifgl_next) {
1271 if (len < sizeof(ifgrq)) {
1272 IF_ADDR_UNLOCK(ifp);
1275 bzero(&ifgrq, sizeof ifgrq);
1276 strlcpy(ifgrq.ifgrq_group, ifgl->ifgl_group->ifg_group,
1277 sizeof(ifgrq.ifgrq_group));
1278 if ((error = copyout(&ifgrq, ifgp, sizeof(struct ifg_req)))) {
1279 IF_ADDR_UNLOCK(ifp);
1282 len -= sizeof(ifgrq);
1285 IF_ADDR_UNLOCK(ifp);
1291 * Stores all members of a group in memory pointed to by data
1294 if_getgroupmembers(struct ifgroupreq *data)
1296 struct ifgroupreq *ifgr = data;
1297 struct ifg_group *ifg;
1298 struct ifg_member *ifgm;
1299 struct ifg_req ifgrq, *ifgp;
1303 TAILQ_FOREACH(ifg, &V_ifg_head, ifg_next)
1304 if (!strcmp(ifg->ifg_group, ifgr->ifgr_name))
1311 if (ifgr->ifgr_len == 0) {
1312 TAILQ_FOREACH(ifgm, &ifg->ifg_members, ifgm_next)
1313 ifgr->ifgr_len += sizeof(ifgrq);
1318 len = ifgr->ifgr_len;
1319 ifgp = ifgr->ifgr_groups;
1320 TAILQ_FOREACH(ifgm, &ifg->ifg_members, ifgm_next) {
1321 if (len < sizeof(ifgrq)) {
1325 bzero(&ifgrq, sizeof ifgrq);
1326 strlcpy(ifgrq.ifgrq_member, ifgm->ifgm_ifp->if_xname,
1327 sizeof(ifgrq.ifgrq_member));
1328 if ((error = copyout(&ifgrq, ifgp, sizeof(struct ifg_req)))) {
1332 len -= sizeof(ifgrq);
1341 * Delete Routes for a Network Interface
1343 * Called for each routing entry via the rnh->rnh_walktree() call above
1344 * to delete all route entries referencing a detaching network interface.
1347 * rn pointer to node in the routing table
1348 * arg argument passed to rnh->rnh_walktree() - detaching interface
1352 * errno failed - reason indicated
1356 if_rtdel(struct radix_node *rn, void *arg)
1358 struct rtentry *rt = (struct rtentry *)rn;
1359 struct ifnet *ifp = arg;
1362 if (rt->rt_ifp == ifp) {
1365 * Protect (sorta) against walktree recursion problems
1366 * with cloned routes
1368 if ((rt->rt_flags & RTF_UP) == 0)
1371 err = rtrequest_fib(RTM_DELETE, rt_key(rt), rt->rt_gateway,
1372 rt_mask(rt), rt->rt_flags|RTF_RNH_LOCKED,
1373 (struct rtentry **) NULL, rt->rt_fibnum);
1375 log(LOG_WARNING, "if_rtdel: error %d\n", err);
1383 * Wrapper functions for struct ifnet address list locking macros. These are
1384 * used by kernel modules to avoid encoding programming interface or binary
1385 * interface assumptions that may be violated when kernel-internal locking
1386 * approaches change.
1389 if_addr_rlock(struct ifnet *ifp)
1396 if_addr_runlock(struct ifnet *ifp)
1399 IF_ADDR_UNLOCK(ifp);
1403 if_maddr_rlock(struct ifnet *ifp)
1410 if_maddr_runlock(struct ifnet *ifp)
1413 IF_ADDR_UNLOCK(ifp);
1417 * Reference count functions for ifaddrs.
1420 ifa_init(struct ifaddr *ifa)
1423 mtx_init(&ifa->ifa_mtx, "ifaddr", NULL, MTX_DEF);
1424 refcount_init(&ifa->ifa_refcnt, 1);
1428 ifa_ref(struct ifaddr *ifa)
1431 refcount_acquire(&ifa->ifa_refcnt);
1435 ifa_free(struct ifaddr *ifa)
1438 if (refcount_release(&ifa->ifa_refcnt)) {
1439 mtx_destroy(&ifa->ifa_mtx);
1440 free(ifa, M_IFADDR);
1445 ifa_add_loopback_route(struct ifaddr *ifa, struct sockaddr *ia)
1448 struct rtentry *rt = NULL;
1449 struct rt_addrinfo info;
1450 static struct sockaddr_dl null_sdl = {sizeof(null_sdl), AF_LINK};
1452 bzero(&info, sizeof(info));
1453 info.rti_ifp = V_loif;
1454 info.rti_flags = ifa->ifa_flags | RTF_HOST | RTF_STATIC;
1455 info.rti_info[RTAX_DST] = ia;
1456 info.rti_info[RTAX_GATEWAY] = (struct sockaddr *)&null_sdl;
1457 error = rtrequest1_fib(RTM_ADD, &info, &rt, 0);
1459 if (error == 0 && rt != NULL) {
1461 ((struct sockaddr_dl *)rt->rt_gateway)->sdl_type =
1462 ifa->ifa_ifp->if_type;
1463 ((struct sockaddr_dl *)rt->rt_gateway)->sdl_index =
1464 ifa->ifa_ifp->if_index;
1467 } else if (error != 0)
1468 log(LOG_INFO, "ifa_add_loopback_route: insertion failed\n");
1474 ifa_del_loopback_route(struct ifaddr *ifa, struct sockaddr *ia)
1477 struct rt_addrinfo info;
1478 struct sockaddr_dl null_sdl;
1480 bzero(&null_sdl, sizeof(null_sdl));
1481 null_sdl.sdl_len = sizeof(null_sdl);
1482 null_sdl.sdl_family = AF_LINK;
1483 null_sdl.sdl_type = ifa->ifa_ifp->if_type;
1484 null_sdl.sdl_index = ifa->ifa_ifp->if_index;
1485 bzero(&info, sizeof(info));
1486 info.rti_flags = ifa->ifa_flags | RTF_HOST | RTF_STATIC;
1487 info.rti_info[RTAX_DST] = ia;
1488 info.rti_info[RTAX_GATEWAY] = (struct sockaddr *)&null_sdl;
1489 error = rtrequest1_fib(RTM_DELETE, &info, NULL, 0);
1492 log(LOG_INFO, "ifa_del_loopback_route: deletion failed\n");
1498 * XXX: Because sockaddr_dl has deeper structure than the sockaddr
1499 * structs used to represent other address families, it is necessary
1500 * to perform a different comparison.
1503 #define sa_equal(a1, a2) \
1504 (bcmp((a1), (a2), ((a1))->sa_len) == 0)
1506 #define sa_dl_equal(a1, a2) \
1507 ((((struct sockaddr_dl *)(a1))->sdl_len == \
1508 ((struct sockaddr_dl *)(a2))->sdl_len) && \
1509 (bcmp(LLADDR((struct sockaddr_dl *)(a1)), \
1510 LLADDR((struct sockaddr_dl *)(a2)), \
1511 ((struct sockaddr_dl *)(a1))->sdl_alen) == 0))
1514 * Locate an interface based on a complete address.
1517 static struct ifaddr *
1518 ifa_ifwithaddr_internal(struct sockaddr *addr, int getref)
1523 IFNET_RLOCK_NOSLEEP();
1524 TAILQ_FOREACH(ifp, &V_ifnet, if_link) {
1526 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
1527 if (ifa->ifa_addr->sa_family != addr->sa_family)
1529 if (sa_equal(addr, ifa->ifa_addr)) {
1532 IF_ADDR_UNLOCK(ifp);
1535 /* IP6 doesn't have broadcast */
1536 if ((ifp->if_flags & IFF_BROADCAST) &&
1537 ifa->ifa_broadaddr &&
1538 ifa->ifa_broadaddr->sa_len != 0 &&
1539 sa_equal(ifa->ifa_broadaddr, addr)) {
1542 IF_ADDR_UNLOCK(ifp);
1546 IF_ADDR_UNLOCK(ifp);
1550 IFNET_RUNLOCK_NOSLEEP();
1555 ifa_ifwithaddr(struct sockaddr *addr)
1558 return (ifa_ifwithaddr_internal(addr, 1));
1562 ifa_ifwithaddr_check(struct sockaddr *addr)
1565 return (ifa_ifwithaddr_internal(addr, 0) != NULL);
1569 * Locate an interface based on the broadcast address.
1573 ifa_ifwithbroadaddr(struct sockaddr *addr)
1578 IFNET_RLOCK_NOSLEEP();
1579 TAILQ_FOREACH(ifp, &V_ifnet, if_link) {
1581 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
1582 if (ifa->ifa_addr->sa_family != addr->sa_family)
1584 if ((ifp->if_flags & IFF_BROADCAST) &&
1585 ifa->ifa_broadaddr &&
1586 ifa->ifa_broadaddr->sa_len != 0 &&
1587 sa_equal(ifa->ifa_broadaddr, addr)) {
1589 IF_ADDR_UNLOCK(ifp);
1593 IF_ADDR_UNLOCK(ifp);
1597 IFNET_RUNLOCK_NOSLEEP();
1602 * Locate the point to point interface with a given destination address.
1606 ifa_ifwithdstaddr(struct sockaddr *addr)
1611 IFNET_RLOCK_NOSLEEP();
1612 TAILQ_FOREACH(ifp, &V_ifnet, if_link) {
1613 if ((ifp->if_flags & IFF_POINTOPOINT) == 0)
1616 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
1617 if (ifa->ifa_addr->sa_family != addr->sa_family)
1619 if (ifa->ifa_dstaddr != NULL &&
1620 sa_equal(addr, ifa->ifa_dstaddr)) {
1622 IF_ADDR_UNLOCK(ifp);
1626 IF_ADDR_UNLOCK(ifp);
1630 IFNET_RUNLOCK_NOSLEEP();
1635 * Find an interface on a specific network. If many, choice
1636 * is most specific found.
1639 ifa_ifwithnet(struct sockaddr *addr, int ignore_ptp)
1643 struct ifaddr *ifa_maybe = NULL;
1644 u_int af = addr->sa_family;
1645 char *addr_data = addr->sa_data, *cplim;
1648 * AF_LINK addresses can be looked up directly by their index number,
1649 * so do that if we can.
1651 if (af == AF_LINK) {
1652 struct sockaddr_dl *sdl = (struct sockaddr_dl *)addr;
1653 if (sdl->sdl_index && sdl->sdl_index <= V_if_index)
1654 return (ifaddr_byindex(sdl->sdl_index));
1658 * Scan though each interface, looking for ones that have addresses
1659 * in this address family. Maintain a reference on ifa_maybe once
1660 * we find one, as we release the IF_ADDR_LOCK() that kept it stable
1661 * when we move onto the next interface.
1663 IFNET_RLOCK_NOSLEEP();
1664 TAILQ_FOREACH(ifp, &V_ifnet, if_link) {
1666 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
1667 char *cp, *cp2, *cp3;
1669 if (ifa->ifa_addr->sa_family != af)
1671 if (af == AF_INET &&
1672 ifp->if_flags & IFF_POINTOPOINT && !ignore_ptp) {
1674 * This is a bit broken as it doesn't
1675 * take into account that the remote end may
1676 * be a single node in the network we are
1678 * The trouble is that we don't know the
1679 * netmask for the remote end.
1681 if (ifa->ifa_dstaddr != NULL &&
1682 sa_equal(addr, ifa->ifa_dstaddr)) {
1684 IF_ADDR_UNLOCK(ifp);
1689 * if we have a special address handler,
1690 * then use it instead of the generic one.
1692 if (ifa->ifa_claim_addr) {
1693 if ((*ifa->ifa_claim_addr)(ifa, addr)) {
1695 IF_ADDR_UNLOCK(ifp);
1702 * Scan all the bits in the ifa's address.
1703 * If a bit dissagrees with what we are
1704 * looking for, mask it with the netmask
1705 * to see if it really matters.
1706 * (A byte at a time)
1708 if (ifa->ifa_netmask == 0)
1711 cp2 = ifa->ifa_addr->sa_data;
1712 cp3 = ifa->ifa_netmask->sa_data;
1713 cplim = ifa->ifa_netmask->sa_len
1714 + (char *)ifa->ifa_netmask;
1716 if ((*cp++ ^ *cp2++) & *cp3++)
1717 goto next; /* next address! */
1719 * If the netmask of what we just found
1720 * is more specific than what we had before
1721 * (if we had one) then remember the new one
1722 * before continuing to search
1723 * for an even better one.
1725 if (ifa_maybe == NULL ||
1726 rn_refines((caddr_t)ifa->ifa_netmask,
1727 (caddr_t)ifa_maybe->ifa_netmask)) {
1728 if (ifa_maybe != NULL)
1729 ifa_free(ifa_maybe);
1735 IF_ADDR_UNLOCK(ifp);
1740 IFNET_RUNLOCK_NOSLEEP();
1741 if (ifa_maybe != NULL)
1742 ifa_free(ifa_maybe);
1747 * Find an interface address specific to an interface best matching
1751 ifaof_ifpforaddr(struct sockaddr *addr, struct ifnet *ifp)
1754 char *cp, *cp2, *cp3;
1756 struct ifaddr *ifa_maybe = NULL;
1757 u_int af = addr->sa_family;
1762 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
1763 if (ifa->ifa_addr->sa_family != af)
1765 if (ifa_maybe == NULL)
1767 if (ifa->ifa_netmask == 0) {
1768 if (sa_equal(addr, ifa->ifa_addr) ||
1769 (ifa->ifa_dstaddr &&
1770 sa_equal(addr, ifa->ifa_dstaddr)))
1774 if (ifp->if_flags & IFF_POINTOPOINT) {
1775 if (sa_equal(addr, ifa->ifa_dstaddr))
1779 cp2 = ifa->ifa_addr->sa_data;
1780 cp3 = ifa->ifa_netmask->sa_data;
1781 cplim = ifa->ifa_netmask->sa_len + (char *)ifa->ifa_netmask;
1782 for (; cp3 < cplim; cp3++)
1783 if ((*cp++ ^ *cp2++) & *cp3)
1793 IF_ADDR_UNLOCK(ifp);
1797 #include <net/if_llatbl.h>
1800 * Default action when installing a route with a Link Level gateway.
1801 * Lookup an appropriate real ifa to point to.
1802 * This should be moved to /sys/net/link.c eventually.
1805 link_rtrequest(int cmd, struct rtentry *rt, struct rt_addrinfo *info)
1807 struct ifaddr *ifa, *oifa;
1808 struct sockaddr *dst;
1813 if (cmd != RTM_ADD || ((ifa = rt->rt_ifa) == 0) ||
1814 ((ifp = ifa->ifa_ifp) == 0) || ((dst = rt_key(rt)) == 0))
1816 ifa = ifaof_ifpforaddr(dst, ifp);
1821 if (ifa->ifa_rtrequest && ifa->ifa_rtrequest != link_rtrequest)
1822 ifa->ifa_rtrequest(cmd, rt, info);
1827 * Mark an interface down and notify protocols of
1829 * NOTE: must be called at splnet or eqivalent.
1832 if_unroute(struct ifnet *ifp, int flag, int fam)
1836 KASSERT(flag == IFF_UP, ("if_unroute: flag != IFF_UP"));
1838 ifp->if_flags &= ~flag;
1839 getmicrotime(&ifp->if_lastchange);
1840 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link)
1841 if (fam == PF_UNSPEC || (fam == ifa->ifa_addr->sa_family))
1842 pfctlinput(PRC_IFDOWN, ifa->ifa_addr);
1843 ifp->if_qflush(ifp);
1845 #if defined(INET) || defined(INET6)
1848 carp_carpdev_state(ifp->if_carp);
1855 * Mark an interface up and notify protocols of
1857 * NOTE: must be called at splnet or eqivalent.
1860 if_route(struct ifnet *ifp, int flag, int fam)
1864 KASSERT(flag == IFF_UP, ("if_route: flag != IFF_UP"));
1866 ifp->if_flags |= flag;
1867 getmicrotime(&ifp->if_lastchange);
1868 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link)
1869 if (fam == PF_UNSPEC || (fam == ifa->ifa_addr->sa_family))
1870 pfctlinput(PRC_IFUP, ifa->ifa_addr);
1871 #if defined(INET) || defined(INET6)
1874 carp_carpdev_state(ifp->if_carp);
1883 void (*vlan_link_state_p)(struct ifnet *, int); /* XXX: private from if_vlan */
1884 void (*vlan_trunk_cap_p)(struct ifnet *); /* XXX: private from if_vlan */
1887 * Handle a change in the interface link state. To avoid LORs
1888 * between driver lock and upper layer locks, as well as possible
1889 * recursions, we post event to taskqueue, and all job
1890 * is done in static do_link_state_change().
1893 if_link_state_change(struct ifnet *ifp, int link_state)
1895 /* Return if state hasn't changed. */
1896 if (ifp->if_link_state == link_state)
1899 ifp->if_link_state = link_state;
1901 taskqueue_enqueue(taskqueue_swi, &ifp->if_linktask);
1905 do_link_state_change(void *arg, int pending)
1907 struct ifnet *ifp = (struct ifnet *)arg;
1908 int link_state = ifp->if_link_state;
1909 CURVNET_SET(ifp->if_vnet);
1911 /* Notify that the link state has changed. */
1913 if (ifp->if_vlantrunk != NULL)
1914 (*vlan_link_state_p)(ifp, 0);
1916 if ((ifp->if_type == IFT_ETHER || ifp->if_type == IFT_L2VLAN) &&
1917 IFP2AC(ifp)->ac_netgraph != NULL)
1918 (*ng_ether_link_state_p)(ifp, link_state);
1919 #if defined(INET) || defined(INET6)
1922 carp_carpdev_state(ifp->if_carp);
1925 if (ifp->if_bridge) {
1926 KASSERT(bstp_linkstate_p != NULL,("if_bridge bstp not loaded!"));
1927 (*bstp_linkstate_p)(ifp, link_state);
1930 KASSERT(lagg_linkstate_p != NULL,("if_lagg not loaded!"));
1931 (*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" );
1947 * Mark an interface down and notify protocols of
1949 * NOTE: must be called at splnet or eqivalent.
1952 if_down(struct ifnet *ifp)
1955 if_unroute(ifp, IFF_UP, AF_UNSPEC);
1959 * Mark an interface up and notify protocols of
1961 * NOTE: must be called at splnet or eqivalent.
1964 if_up(struct ifnet *ifp)
1967 if_route(ifp, IFF_UP, AF_UNSPEC);
1971 * Flush an interface queue.
1974 if_qflush(struct ifnet *ifp)
1982 if (ALTQ_IS_ENABLED(ifq))
1986 while ((m = n) != 0) {
1997 * Handle interface watchdog timer routines. Called
1998 * from softclock, we decrement timers (if set) and
1999 * call the appropriate interface routine on expiration.
2001 * XXXRW: Note that because timeouts run with Giant, if_watchdog() is called
2005 if_slowtimo(void *arg)
2007 VNET_ITERATOR_DECL(vnet_iter);
2011 VNET_LIST_RLOCK_NOSLEEP();
2012 IFNET_RLOCK_NOSLEEP();
2013 VNET_FOREACH(vnet_iter) {
2014 CURVNET_SET(vnet_iter);
2015 TAILQ_FOREACH(ifp, &V_ifnet, if_link) {
2016 if (ifp->if_timer == 0 || --ifp->if_timer)
2018 if (ifp->if_watchdog)
2019 (*ifp->if_watchdog)(ifp);
2023 IFNET_RUNLOCK_NOSLEEP();
2024 VNET_LIST_RUNLOCK_NOSLEEP();
2026 timeout(if_slowtimo, (void *)0, hz / IFNET_SLOWHZ);
2030 * Map interface name to interface structure pointer, with or without
2031 * returning a reference.
2034 ifunit_ref(const char *name)
2038 IFNET_RLOCK_NOSLEEP();
2039 TAILQ_FOREACH(ifp, &V_ifnet, if_link) {
2040 if (strncmp(name, ifp->if_xname, IFNAMSIZ) == 0 &&
2041 !(ifp->if_flags & IFF_DYING))
2046 IFNET_RUNLOCK_NOSLEEP();
2051 ifunit(const char *name)
2055 IFNET_RLOCK_NOSLEEP();
2056 TAILQ_FOREACH(ifp, &V_ifnet, if_link) {
2057 if (strncmp(name, ifp->if_xname, IFNAMSIZ) == 0)
2060 IFNET_RUNLOCK_NOSLEEP();
2065 * Hardware specific interface ioctls.
2068 ifhwioctl(u_long cmd, struct ifnet *ifp, caddr_t data, struct thread *td)
2073 int new_flags, temp_flags;
2074 size_t namelen, onamelen;
2076 char *descrbuf, *odescrbuf;
2077 char new_name[IFNAMSIZ];
2079 struct sockaddr_dl *sdl;
2081 ifr = (struct ifreq *)data;
2084 ifr->ifr_index = ifp->if_index;
2088 temp_flags = ifp->if_flags | ifp->if_drv_flags;
2089 ifr->ifr_flags = temp_flags & 0xffff;
2090 ifr->ifr_flagshigh = temp_flags >> 16;
2094 ifr->ifr_reqcap = ifp->if_capabilities;
2095 ifr->ifr_curcap = ifp->if_capenable;
2100 error = mac_ifnet_ioctl_get(td->td_ucred, ifr, ifp);
2105 ifr->ifr_metric = ifp->if_metric;
2109 ifr->ifr_mtu = ifp->if_mtu;
2113 ifr->ifr_phys = ifp->if_physical;
2118 sx_slock(&ifdescr_sx);
2119 if (ifp->if_description == NULL)
2122 /* space for terminating nul */
2123 descrlen = strlen(ifp->if_description) + 1;
2124 if (ifr->ifr_buffer.length < descrlen)
2125 ifr->ifr_buffer.buffer = NULL;
2127 error = copyout(ifp->if_description,
2128 ifr->ifr_buffer.buffer, descrlen);
2129 ifr->ifr_buffer.length = descrlen;
2131 sx_sunlock(&ifdescr_sx);
2135 error = priv_check(td, PRIV_NET_SETIFDESCR);
2140 * Copy only (length-1) bytes to make sure that
2141 * if_description is always nul terminated. The
2142 * length parameter is supposed to count the
2143 * terminating nul in.
2145 if (ifr->ifr_buffer.length > ifdescr_maxlen)
2146 return (ENAMETOOLONG);
2147 else if (ifr->ifr_buffer.length == 0)
2150 descrbuf = malloc(ifr->ifr_buffer.length, M_IFDESCR,
2152 error = copyin(ifr->ifr_buffer.buffer, descrbuf,
2153 ifr->ifr_buffer.length - 1);
2155 free(descrbuf, M_IFDESCR);
2160 sx_xlock(&ifdescr_sx);
2161 odescrbuf = ifp->if_description;
2162 ifp->if_description = descrbuf;
2163 sx_xunlock(&ifdescr_sx);
2165 getmicrotime(&ifp->if_lastchange);
2166 free(odescrbuf, M_IFDESCR);
2170 error = priv_check(td, PRIV_NET_SETIFFLAGS);
2174 * Currently, no driver owned flags pass the IFF_CANTCHANGE
2175 * check, so we don't need special handling here yet.
2177 new_flags = (ifr->ifr_flags & 0xffff) |
2178 (ifr->ifr_flagshigh << 16);
2179 if (ifp->if_flags & IFF_SMART) {
2180 /* Smart drivers twiddle their own routes */
2181 } else if (ifp->if_flags & IFF_UP &&
2182 (new_flags & IFF_UP) == 0) {
2186 } else if (new_flags & IFF_UP &&
2187 (ifp->if_flags & IFF_UP) == 0) {
2192 /* See if permanently promiscuous mode bit is about to flip */
2193 if ((ifp->if_flags ^ new_flags) & IFF_PPROMISC) {
2194 if (new_flags & IFF_PPROMISC)
2195 ifp->if_flags |= IFF_PROMISC;
2196 else if (ifp->if_pcount == 0)
2197 ifp->if_flags &= ~IFF_PROMISC;
2198 log(LOG_INFO, "%s: permanently promiscuous mode %s\n",
2200 (new_flags & IFF_PPROMISC) ? "enabled" : "disabled");
2202 ifp->if_flags = (ifp->if_flags & IFF_CANTCHANGE) |
2203 (new_flags &~ IFF_CANTCHANGE);
2204 if (ifp->if_ioctl) {
2205 (void) (*ifp->if_ioctl)(ifp, cmd, data);
2207 getmicrotime(&ifp->if_lastchange);
2211 error = priv_check(td, PRIV_NET_SETIFCAP);
2214 if (ifp->if_ioctl == NULL)
2215 return (EOPNOTSUPP);
2216 if (ifr->ifr_reqcap & ~ifp->if_capabilities)
2218 error = (*ifp->if_ioctl)(ifp, cmd, data);
2220 getmicrotime(&ifp->if_lastchange);
2225 error = mac_ifnet_ioctl_set(td->td_ucred, ifr, ifp);
2230 error = priv_check(td, PRIV_NET_SETIFNAME);
2233 error = copyinstr(ifr->ifr_data, new_name, IFNAMSIZ, NULL);
2236 if (new_name[0] == '\0')
2238 if (ifunit(new_name) != NULL)
2242 * XXX: Locking. Nothing else seems to lock if_flags,
2243 * and there are numerous other races with the
2244 * ifunit() checks not being atomic with namespace
2245 * changes (renames, vmoves, if_attach, etc).
2247 ifp->if_flags |= IFF_RENAMING;
2249 /* Announce the departure of the interface. */
2250 rt_ifannouncemsg(ifp, IFAN_DEPARTURE);
2251 EVENTHANDLER_INVOKE(ifnet_departure_event, ifp);
2253 log(LOG_INFO, "%s: changing name to '%s'\n",
2254 ifp->if_xname, new_name);
2256 strlcpy(ifp->if_xname, new_name, sizeof(ifp->if_xname));
2259 sdl = (struct sockaddr_dl *)ifa->ifa_addr;
2260 namelen = strlen(new_name);
2261 onamelen = sdl->sdl_nlen;
2263 * Move the address if needed. This is safe because we
2264 * allocate space for a name of length IFNAMSIZ when we
2265 * create this in if_attach().
2267 if (namelen != onamelen) {
2268 bcopy(sdl->sdl_data + onamelen,
2269 sdl->sdl_data + namelen, sdl->sdl_alen);
2271 bcopy(new_name, sdl->sdl_data, namelen);
2272 sdl->sdl_nlen = namelen;
2273 sdl = (struct sockaddr_dl *)ifa->ifa_netmask;
2274 bzero(sdl->sdl_data, onamelen);
2275 while (namelen != 0)
2276 sdl->sdl_data[--namelen] = 0xff;
2279 EVENTHANDLER_INVOKE(ifnet_arrival_event, ifp);
2280 /* Announce the return of the interface. */
2281 rt_ifannouncemsg(ifp, IFAN_ARRIVAL);
2283 ifp->if_flags &= ~IFF_RENAMING;
2288 error = priv_check(td, PRIV_NET_SETIFVNET);
2291 error = if_vmove_loan(td, ifp, ifr->ifr_name, ifr->ifr_jid);
2296 error = priv_check(td, PRIV_NET_SETIFMETRIC);
2299 ifp->if_metric = ifr->ifr_metric;
2300 getmicrotime(&ifp->if_lastchange);
2304 error = priv_check(td, PRIV_NET_SETIFPHYS);
2307 if (ifp->if_ioctl == NULL)
2308 return (EOPNOTSUPP);
2309 error = (*ifp->if_ioctl)(ifp, cmd, data);
2311 getmicrotime(&ifp->if_lastchange);
2316 u_long oldmtu = ifp->if_mtu;
2318 error = priv_check(td, PRIV_NET_SETIFMTU);
2321 if (ifr->ifr_mtu < IF_MINMTU || ifr->ifr_mtu > IF_MAXMTU)
2323 if (ifp->if_ioctl == NULL)
2324 return (EOPNOTSUPP);
2325 error = (*ifp->if_ioctl)(ifp, cmd, data);
2327 getmicrotime(&ifp->if_lastchange);
2331 * If the link MTU changed, do network layer specific procedure.
2333 if (ifp->if_mtu != oldmtu) {
2343 if (cmd == SIOCADDMULTI)
2344 error = priv_check(td, PRIV_NET_ADDMULTI);
2346 error = priv_check(td, PRIV_NET_DELMULTI);
2350 /* Don't allow group membership on non-multicast interfaces. */
2351 if ((ifp->if_flags & IFF_MULTICAST) == 0)
2352 return (EOPNOTSUPP);
2354 /* Don't let users screw up protocols' entries. */
2355 if (ifr->ifr_addr.sa_family != AF_LINK)
2358 if (cmd == SIOCADDMULTI) {
2359 struct ifmultiaddr *ifma;
2362 * Userland is only permitted to join groups once
2363 * via the if_addmulti() KPI, because it cannot hold
2364 * struct ifmultiaddr * between calls. It may also
2365 * lose a race while we check if the membership
2369 ifma = if_findmulti(ifp, &ifr->ifr_addr);
2370 IF_ADDR_UNLOCK(ifp);
2374 error = if_addmulti(ifp, &ifr->ifr_addr, &ifma);
2376 error = if_delmulti(ifp, &ifr->ifr_addr);
2379 getmicrotime(&ifp->if_lastchange);
2382 case SIOCSIFPHYADDR:
2383 case SIOCDIFPHYADDR:
2385 case SIOCSIFPHYADDR_IN6:
2387 case SIOCSLIFPHYADDR:
2389 case SIOCSIFGENERIC:
2390 error = priv_check(td, PRIV_NET_HWIOCTL);
2393 if (ifp->if_ioctl == NULL)
2394 return (EOPNOTSUPP);
2395 error = (*ifp->if_ioctl)(ifp, cmd, data);
2397 getmicrotime(&ifp->if_lastchange);
2401 ifs = (struct ifstat *)data;
2402 ifs->ascii[0] = '\0';
2404 case SIOCGIFPSRCADDR:
2405 case SIOCGIFPDSTADDR:
2406 case SIOCGLIFPHYADDR:
2408 case SIOCGIFGENERIC:
2409 if (ifp->if_ioctl == NULL)
2410 return (EOPNOTSUPP);
2411 error = (*ifp->if_ioctl)(ifp, cmd, data);
2415 error = priv_check(td, PRIV_NET_SETLLADDR);
2418 error = if_setlladdr(ifp,
2419 ifr->ifr_addr.sa_data, ifr->ifr_addr.sa_len);
2420 EVENTHANDLER_INVOKE(iflladdr_event, ifp);
2425 struct ifgroupreq *ifgr = (struct ifgroupreq *)ifr;
2427 error = priv_check(td, PRIV_NET_ADDIFGROUP);
2430 if ((error = if_addgroup(ifp, ifgr->ifgr_group)))
2436 if ((error = if_getgroup((struct ifgroupreq *)ifr, ifp)))
2442 struct ifgroupreq *ifgr = (struct ifgroupreq *)ifr;
2444 error = priv_check(td, PRIV_NET_DELIFGROUP);
2447 if ((error = if_delgroup(ifp, ifgr->ifgr_group)))
2463 ifioctl(struct socket *so, u_long cmd, caddr_t data, struct thread *td)
2476 return (ifconf(cmd, data));
2478 ifr = (struct ifreq *)data;
2483 error = priv_check(td, PRIV_NET_SETIFVNET);
2486 return (if_vmove_reclaim(td, ifr->ifr_name, ifr->ifr_jid));
2490 error = priv_check(td, PRIV_NET_IFCREATE);
2493 return (if_clone_create(ifr->ifr_name, sizeof(ifr->ifr_name),
2494 cmd == SIOCIFCREATE2 ? ifr->ifr_data : NULL));
2496 error = priv_check(td, PRIV_NET_IFDESTROY);
2499 return if_clone_destroy(ifr->ifr_name);
2501 case SIOCIFGCLONERS:
2502 return (if_clone_list((struct if_clonereq *)data));
2504 return (if_getgroupmembers((struct ifgroupreq *)data));
2507 ifp = ifunit_ref(ifr->ifr_name);
2511 error = ifhwioctl(cmd, ifp, data, td);
2512 if (error != ENOIOCTL) {
2517 oif_flags = ifp->if_flags;
2518 if (so->so_proto == NULL) {
2520 return (EOPNOTSUPP);
2523 error = ((*so->so_proto->pr_usrreqs->pru_control)(so, cmd,
2526 if (error == EOPNOTSUPP && ifp != NULL && ifp->if_ioctl != NULL)
2527 error = (*ifp->if_ioctl)(ifp, cmd, data);
2534 case SIOCSIFDSTADDR:
2536 case SIOCSIFBRDADDR:
2537 case SIOCSIFNETMASK:
2538 #if BYTE_ORDER != BIG_ENDIAN
2539 if (ifr->ifr_addr.sa_family == 0 &&
2540 ifr->ifr_addr.sa_len < 16) {
2541 ifr->ifr_addr.sa_family = ifr->ifr_addr.sa_len;
2542 ifr->ifr_addr.sa_len = 16;
2545 if (ifr->ifr_addr.sa_len == 0)
2546 ifr->ifr_addr.sa_len = 16;
2554 case OSIOCGIFDSTADDR:
2555 cmd = SIOCGIFDSTADDR;
2558 case OSIOCGIFBRDADDR:
2559 cmd = SIOCGIFBRDADDR;
2562 case OSIOCGIFNETMASK:
2563 cmd = SIOCGIFNETMASK;
2565 error = ((*so->so_proto->pr_usrreqs->pru_control)(so,
2569 if (error == EOPNOTSUPP && ifp != NULL &&
2570 ifp->if_ioctl != NULL)
2571 error = (*ifp->if_ioctl)(ifp, cmd, data);
2575 case OSIOCGIFDSTADDR:
2576 case OSIOCGIFBRDADDR:
2577 case OSIOCGIFNETMASK:
2578 *(u_short *)&ifr->ifr_addr = ifr->ifr_addr.sa_family;
2582 #endif /* COMPAT_43 */
2584 if ((oif_flags ^ ifp->if_flags) & IFF_UP) {
2586 if (ifp->if_flags & IFF_UP) {
2598 * The code common to handling reference counted flags,
2599 * e.g., in ifpromisc() and if_allmulti().
2600 * The "pflag" argument can specify a permanent mode flag to check,
2601 * such as IFF_PPROMISC for promiscuous mode; should be 0 if none.
2603 * Only to be used on stack-owned flags, not driver-owned flags.
2606 if_setflag(struct ifnet *ifp, int flag, int pflag, int *refcount, int onswitch)
2610 int oldflags, oldcount;
2612 /* Sanity checks to catch programming errors */
2613 KASSERT((flag & (IFF_DRV_OACTIVE|IFF_DRV_RUNNING)) == 0,
2614 ("%s: setting driver-owned flag %d", __func__, flag));
2617 KASSERT(*refcount >= 0,
2618 ("%s: increment negative refcount %d for flag %d",
2619 __func__, *refcount, flag));
2621 KASSERT(*refcount > 0,
2622 ("%s: decrement non-positive refcount %d for flag %d",
2623 __func__, *refcount, flag));
2625 /* In case this mode is permanent, just touch refcount */
2626 if (ifp->if_flags & pflag) {
2627 *refcount += onswitch ? 1 : -1;
2631 /* Save ifnet parameters for if_ioctl() may fail */
2632 oldcount = *refcount;
2633 oldflags = ifp->if_flags;
2636 * See if we aren't the only and touching refcount is enough.
2637 * Actually toggle interface flag if we are the first or last.
2642 ifp->if_flags |= flag;
2646 ifp->if_flags &= ~flag;
2649 /* Call down the driver since we've changed interface flags */
2650 if (ifp->if_ioctl == NULL) {
2654 ifr.ifr_flags = ifp->if_flags & 0xffff;
2655 ifr.ifr_flagshigh = ifp->if_flags >> 16;
2656 error = (*ifp->if_ioctl)(ifp, SIOCSIFFLAGS, (caddr_t)&ifr);
2659 /* Notify userland that interface flags have changed */
2664 /* Recover after driver error */
2665 *refcount = oldcount;
2666 ifp->if_flags = oldflags;
2671 * Set/clear promiscuous mode on interface ifp based on the truth value
2672 * of pswitch. The calls are reference counted so that only the first
2673 * "on" request actually has an effect, as does the final "off" request.
2674 * Results are undefined if the "off" and "on" requests are not matched.
2677 ifpromisc(struct ifnet *ifp, int pswitch)
2680 int oldflags = ifp->if_flags;
2682 error = if_setflag(ifp, IFF_PROMISC, IFF_PPROMISC,
2683 &ifp->if_pcount, pswitch);
2684 /* If promiscuous mode status has changed, log a message */
2685 if (error == 0 && ((ifp->if_flags ^ oldflags) & IFF_PROMISC))
2686 log(LOG_INFO, "%s: promiscuous mode %s\n",
2688 (ifp->if_flags & IFF_PROMISC) ? "enabled" : "disabled");
2693 * Return interface configuration
2694 * of system. List may be used
2695 * in later ioctl's (above) to get
2696 * other information.
2700 ifconf(u_long cmd, caddr_t data)
2702 struct ifconf *ifc = (struct ifconf *)data;
2704 struct ifconf32 *ifc32 = (struct ifconf32 *)data;
2705 struct ifconf ifc_swab;
2711 int error, full = 0, valid_len, max_len;
2714 if (cmd == SIOCGIFCONF32) {
2715 ifc_swab.ifc_len = ifc32->ifc_len;
2716 ifc_swab.ifc_buf = (caddr_t)(uintptr_t)ifc32->ifc_buf;
2720 /* Limit initial buffer size to MAXPHYS to avoid DoS from userspace. */
2721 max_len = MAXPHYS - 1;
2723 /* Prevent hostile input from being able to crash the system */
2724 if (ifc->ifc_len <= 0)
2728 if (ifc->ifc_len <= max_len) {
2729 max_len = ifc->ifc_len;
2732 sb = sbuf_new(NULL, NULL, max_len + 1, SBUF_FIXEDLEN);
2737 TAILQ_FOREACH(ifp, &V_ifnet, if_link) {
2741 * Zero the ifr_name buffer to make sure we don't
2742 * disclose the contents of the stack.
2744 memset(ifr.ifr_name, 0, sizeof(ifr.ifr_name));
2746 if (strlcpy(ifr.ifr_name, ifp->if_xname, sizeof(ifr.ifr_name))
2747 >= sizeof(ifr.ifr_name)) {
2750 return (ENAMETOOLONG);
2755 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
2756 struct sockaddr *sa = ifa->ifa_addr;
2758 if (prison_if(curthread->td_ucred, sa) != 0)
2762 if (cmd == OSIOCGIFCONF) {
2763 struct osockaddr *osa =
2764 (struct osockaddr *)&ifr.ifr_addr;
2766 osa->sa_family = sa->sa_family;
2767 sbuf_bcat(sb, &ifr, sizeof(ifr));
2768 max_len += sizeof(ifr);
2771 if (sa->sa_len <= sizeof(*sa)) {
2773 sbuf_bcat(sb, &ifr, sizeof(ifr));
2774 max_len += sizeof(ifr);
2777 offsetof(struct ifreq, ifr_addr));
2778 max_len += offsetof(struct ifreq, ifr_addr);
2779 sbuf_bcat(sb, sa, sa->sa_len);
2780 max_len += sa->sa_len;
2783 if (!sbuf_overflowed(sb))
2784 valid_len = sbuf_len(sb);
2786 IF_ADDR_UNLOCK(ifp);
2788 bzero((caddr_t)&ifr.ifr_addr, sizeof(ifr.ifr_addr));
2789 sbuf_bcat(sb, &ifr, sizeof(ifr));
2790 max_len += sizeof(ifr);
2792 if (!sbuf_overflowed(sb))
2793 valid_len = sbuf_len(sb);
2799 * If we didn't allocate enough space (uncommon), try again. If
2800 * we have already allocated as much space as we are allowed,
2801 * return what we've got.
2803 if (valid_len != max_len && !full) {
2808 ifc->ifc_len = valid_len;
2810 if (cmd == SIOCGIFCONF32)
2811 ifc32->ifc_len = valid_len;
2814 error = copyout(sbuf_data(sb), ifc->ifc_req, ifc->ifc_len);
2820 * Just like ifpromisc(), but for all-multicast-reception mode.
2823 if_allmulti(struct ifnet *ifp, int onswitch)
2826 return (if_setflag(ifp, IFF_ALLMULTI, 0, &ifp->if_amcount, onswitch));
2829 struct ifmultiaddr *
2830 if_findmulti(struct ifnet *ifp, struct sockaddr *sa)
2832 struct ifmultiaddr *ifma;
2834 IF_ADDR_LOCK_ASSERT(ifp);
2836 TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
2837 if (sa->sa_family == AF_LINK) {
2838 if (sa_dl_equal(ifma->ifma_addr, sa))
2841 if (sa_equal(ifma->ifma_addr, sa))
2850 * Allocate a new ifmultiaddr and initialize based on passed arguments. We
2851 * make copies of passed sockaddrs. The ifmultiaddr will not be added to
2852 * the ifnet multicast address list here, so the caller must do that and
2853 * other setup work (such as notifying the device driver). The reference
2854 * count is initialized to 1.
2856 static struct ifmultiaddr *
2857 if_allocmulti(struct ifnet *ifp, struct sockaddr *sa, struct sockaddr *llsa,
2860 struct ifmultiaddr *ifma;
2861 struct sockaddr *dupsa;
2863 ifma = malloc(sizeof *ifma, M_IFMADDR, mflags |
2868 dupsa = malloc(sa->sa_len, M_IFMADDR, mflags);
2869 if (dupsa == NULL) {
2870 free(ifma, M_IFMADDR);
2873 bcopy(sa, dupsa, sa->sa_len);
2874 ifma->ifma_addr = dupsa;
2876 ifma->ifma_ifp = ifp;
2877 ifma->ifma_refcount = 1;
2878 ifma->ifma_protospec = NULL;
2881 ifma->ifma_lladdr = NULL;
2885 dupsa = malloc(llsa->sa_len, M_IFMADDR, mflags);
2886 if (dupsa == NULL) {
2887 free(ifma->ifma_addr, M_IFMADDR);
2888 free(ifma, M_IFMADDR);
2891 bcopy(llsa, dupsa, llsa->sa_len);
2892 ifma->ifma_lladdr = dupsa;
2898 * if_freemulti: free ifmultiaddr structure and possibly attached related
2899 * addresses. The caller is responsible for implementing reference
2900 * counting, notifying the driver, handling routing messages, and releasing
2901 * any dependent link layer state.
2904 if_freemulti(struct ifmultiaddr *ifma)
2907 KASSERT(ifma->ifma_refcount == 0, ("if_freemulti: refcount %d",
2908 ifma->ifma_refcount));
2909 KASSERT(ifma->ifma_protospec == NULL,
2910 ("if_freemulti: protospec not NULL"));
2912 if (ifma->ifma_lladdr != NULL)
2913 free(ifma->ifma_lladdr, M_IFMADDR);
2914 free(ifma->ifma_addr, M_IFMADDR);
2915 free(ifma, M_IFMADDR);
2919 * Register an additional multicast address with a network interface.
2921 * - If the address is already present, bump the reference count on the
2922 * address and return.
2923 * - If the address is not link-layer, look up a link layer address.
2924 * - Allocate address structures for one or both addresses, and attach to the
2925 * multicast address list on the interface. If automatically adding a link
2926 * layer address, the protocol address will own a reference to the link
2927 * layer address, to be freed when it is freed.
2928 * - Notify the network device driver of an addition to the multicast address
2931 * 'sa' points to caller-owned memory with the desired multicast address.
2933 * 'retifma' will be used to return a pointer to the resulting multicast
2934 * address reference, if desired.
2937 if_addmulti(struct ifnet *ifp, struct sockaddr *sa,
2938 struct ifmultiaddr **retifma)
2940 struct ifmultiaddr *ifma, *ll_ifma;
2941 struct sockaddr *llsa;
2945 * If the address is already present, return a new reference to it;
2946 * otherwise, allocate storage and set up a new address.
2949 ifma = if_findmulti(ifp, sa);
2951 ifma->ifma_refcount++;
2952 if (retifma != NULL)
2954 IF_ADDR_UNLOCK(ifp);
2959 * The address isn't already present; resolve the protocol address
2960 * into a link layer address, and then look that up, bump its
2961 * refcount or allocate an ifma for that also. If 'llsa' was
2962 * returned, we will need to free it later.
2966 if (ifp->if_resolvemulti != NULL) {
2967 error = ifp->if_resolvemulti(ifp, &llsa, sa);
2973 * Allocate the new address. Don't hook it up yet, as we may also
2974 * need to allocate a link layer multicast address.
2976 ifma = if_allocmulti(ifp, sa, llsa, M_NOWAIT);
2983 * If a link layer address is found, we'll need to see if it's
2984 * already present in the address list, or allocate is as well.
2985 * When this block finishes, the link layer address will be on the
2989 ll_ifma = if_findmulti(ifp, llsa);
2990 if (ll_ifma == NULL) {
2991 ll_ifma = if_allocmulti(ifp, llsa, NULL, M_NOWAIT);
2992 if (ll_ifma == NULL) {
2993 --ifma->ifma_refcount;
2998 TAILQ_INSERT_HEAD(&ifp->if_multiaddrs, ll_ifma,
3001 ll_ifma->ifma_refcount++;
3002 ifma->ifma_llifma = ll_ifma;
3006 * We now have a new multicast address, ifma, and possibly a new or
3007 * referenced link layer address. Add the primary address to the
3008 * ifnet address list.
3010 TAILQ_INSERT_HEAD(&ifp->if_multiaddrs, ifma, ifma_link);
3012 if (retifma != NULL)
3016 * Must generate the message while holding the lock so that 'ifma'
3017 * pointer is still valid.
3019 rt_newmaddrmsg(RTM_NEWMADDR, ifma);
3020 IF_ADDR_UNLOCK(ifp);
3023 * We are certain we have added something, so call down to the
3024 * interface to let them know about it.
3026 if (ifp->if_ioctl != NULL) {
3027 (void) (*ifp->if_ioctl)(ifp, SIOCADDMULTI, 0);
3031 free(llsa, M_IFMADDR);
3037 free(llsa, M_IFMADDR);
3040 IF_ADDR_UNLOCK(ifp);
3045 * Delete a multicast group membership by network-layer group address.
3047 * Returns ENOENT if the entry could not be found. If ifp no longer
3048 * exists, results are undefined. This entry point should only be used
3049 * from subsystems which do appropriate locking to hold ifp for the
3050 * duration of the call.
3051 * Network-layer protocol domains must use if_delmulti_ifma().
3054 if_delmulti(struct ifnet *ifp, struct sockaddr *sa)
3056 struct ifmultiaddr *ifma;
3061 IFNET_RLOCK_NOSLEEP();
3062 TAILQ_FOREACH(oifp, &V_ifnet, if_link)
3067 IFNET_RUNLOCK_NOSLEEP();
3069 KASSERT(ifp != NULL, ("%s: ifnet went away", __func__));
3076 ifma = if_findmulti(ifp, sa);
3078 lastref = if_delmulti_locked(ifp, ifma, 0);
3079 IF_ADDR_UNLOCK(ifp);
3084 if (lastref && ifp->if_ioctl != NULL) {
3085 (void)(*ifp->if_ioctl)(ifp, SIOCDELMULTI, 0);
3092 * Delete all multicast group membership for an interface.
3093 * Should be used to quickly flush all multicast filters.
3096 if_delallmulti(struct ifnet *ifp)
3098 struct ifmultiaddr *ifma;
3099 struct ifmultiaddr *next;
3102 TAILQ_FOREACH_SAFE(ifma, &ifp->if_multiaddrs, ifma_link, next)
3103 if_delmulti_locked(ifp, ifma, 0);
3104 IF_ADDR_UNLOCK(ifp);
3108 * Delete a multicast group membership by group membership pointer.
3109 * Network-layer protocol domains must use this routine.
3111 * It is safe to call this routine if the ifp disappeared.
3114 if_delmulti_ifma(struct ifmultiaddr *ifma)
3119 ifp = ifma->ifma_ifp;
3122 printf("%s: ifma_ifp seems to be detached\n", __func__);
3126 IFNET_RLOCK_NOSLEEP();
3127 TAILQ_FOREACH(oifp, &V_ifnet, if_link)
3131 printf("%s: ifnet %p disappeared\n", __func__, ifp);
3134 IFNET_RUNLOCK_NOSLEEP();
3138 * If and only if the ifnet instance exists: Acquire the address lock.
3143 lastref = if_delmulti_locked(ifp, ifma, 0);
3147 * If and only if the ifnet instance exists:
3148 * Release the address lock.
3149 * If the group was left: update the hardware hash filter.
3151 IF_ADDR_UNLOCK(ifp);
3152 if (lastref && ifp->if_ioctl != NULL) {
3153 (void)(*ifp->if_ioctl)(ifp, SIOCDELMULTI, 0);
3159 * Perform deletion of network-layer and/or link-layer multicast address.
3161 * Return 0 if the reference count was decremented.
3162 * Return 1 if the final reference was released, indicating that the
3163 * hardware hash filter should be reprogrammed.
3166 if_delmulti_locked(struct ifnet *ifp, struct ifmultiaddr *ifma, int detaching)
3168 struct ifmultiaddr *ll_ifma;
3170 if (ifp != NULL && ifma->ifma_ifp != NULL) {
3171 KASSERT(ifma->ifma_ifp == ifp,
3172 ("%s: inconsistent ifp %p", __func__, ifp));
3173 IF_ADDR_LOCK_ASSERT(ifp);
3176 ifp = ifma->ifma_ifp;
3179 * If the ifnet is detaching, null out references to ifnet,
3180 * so that upper protocol layers will notice, and not attempt
3181 * to obtain locks for an ifnet which no longer exists. The
3182 * routing socket announcement must happen before the ifnet
3183 * instance is detached from the system.
3187 printf("%s: detaching ifnet instance %p\n", __func__, ifp);
3190 * ifp may already be nulled out if we are being reentered
3191 * to delete the ll_ifma.
3194 rt_newmaddrmsg(RTM_DELMADDR, ifma);
3195 ifma->ifma_ifp = NULL;
3199 if (--ifma->ifma_refcount > 0)
3203 * If this ifma is a network-layer ifma, a link-layer ifma may
3204 * have been associated with it. Release it first if so.
3206 ll_ifma = ifma->ifma_llifma;
3207 if (ll_ifma != NULL) {
3208 KASSERT(ifma->ifma_lladdr != NULL,
3209 ("%s: llifma w/o lladdr", __func__));
3211 ll_ifma->ifma_ifp = NULL; /* XXX */
3212 if (--ll_ifma->ifma_refcount == 0) {
3214 TAILQ_REMOVE(&ifp->if_multiaddrs, ll_ifma,
3217 if_freemulti(ll_ifma);
3222 TAILQ_REMOVE(&ifp->if_multiaddrs, ifma, ifma_link);
3227 * The last reference to this instance of struct ifmultiaddr
3228 * was released; the hardware should be notified of this change.
3234 * Set the link layer address on an interface.
3236 * At this time we only support certain types of interfaces,
3237 * and we don't allow the length of the address to change.
3240 if_setlladdr(struct ifnet *ifp, const u_char *lladdr, int len)
3242 struct sockaddr_dl *sdl;
3249 IF_ADDR_UNLOCK(ifp);
3253 IF_ADDR_UNLOCK(ifp);
3254 sdl = (struct sockaddr_dl *)ifa->ifa_addr;
3259 if (len != sdl->sdl_alen) { /* don't allow length to change */
3263 switch (ifp->if_type) {
3271 case IFT_IEEE8023ADLAG:
3273 bcopy(lladdr, LLADDR(sdl), len);
3282 * If the interface is already up, we need
3283 * to re-init it in order to reprogram its
3286 if ((ifp->if_flags & IFF_UP) != 0) {
3287 if (ifp->if_ioctl) {
3288 ifp->if_flags &= ~IFF_UP;
3289 ifr.ifr_flags = ifp->if_flags & 0xffff;
3290 ifr.ifr_flagshigh = ifp->if_flags >> 16;
3291 (*ifp->if_ioctl)(ifp, SIOCSIFFLAGS, (caddr_t)&ifr);
3292 ifp->if_flags |= IFF_UP;
3293 ifr.ifr_flags = ifp->if_flags & 0xffff;
3294 ifr.ifr_flagshigh = ifp->if_flags >> 16;
3295 (*ifp->if_ioctl)(ifp, SIOCSIFFLAGS, (caddr_t)&ifr);
3299 * Also send gratuitous ARPs to notify other nodes about
3300 * the address change.
3302 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
3303 if (ifa->ifa_addr->sa_family == AF_INET)
3304 arp_ifinit(ifp, ifa);
3312 * The name argument must be a pointer to storage which will last as
3313 * long as the interface does. For physical devices, the result of
3314 * device_get_name(dev) is a good choice and for pseudo-devices a
3315 * static string works well.
3318 if_initname(struct ifnet *ifp, const char *name, int unit)
3320 ifp->if_dname = name;
3321 ifp->if_dunit = unit;
3322 if (unit != IF_DUNIT_NONE)
3323 snprintf(ifp->if_xname, IFNAMSIZ, "%s%d", name, unit);
3325 strlcpy(ifp->if_xname, name, IFNAMSIZ);
3329 if_printf(struct ifnet *ifp, const char * fmt, ...)
3334 retval = printf("%s: ", ifp->if_xname);
3336 retval += vprintf(fmt, ap);
3342 if_start(struct ifnet *ifp)
3345 (*(ifp)->if_start)(ifp);
3349 * Backwards compatibility interface for drivers
3350 * that have not implemented it
3353 if_transmit(struct ifnet *ifp, struct mbuf *m)
3357 IFQ_HANDOFF(ifp, m, error);
3362 if_handoff(struct ifqueue *ifq, struct mbuf *m, struct ifnet *ifp, int adjust)
3367 if (_IF_QFULL(ifq)) {
3374 ifp->if_obytes += m->m_pkthdr.len + adjust;
3375 if (m->m_flags & (M_BCAST|M_MCAST))
3377 active = ifp->if_drv_flags & IFF_DRV_OACTIVE;
3379 _IF_ENQUEUE(ifq, m);
3381 if (ifp != NULL && !active)
3382 (*(ifp)->if_start)(ifp);
3387 if_register_com_alloc(u_char type,
3388 if_com_alloc_t *a, if_com_free_t *f)
3391 KASSERT(if_com_alloc[type] == NULL,
3392 ("if_register_com_alloc: %d already registered", type));
3393 KASSERT(if_com_free[type] == NULL,
3394 ("if_register_com_alloc: %d free already registered", type));
3396 if_com_alloc[type] = a;
3397 if_com_free[type] = f;
3401 if_deregister_com_alloc(u_char type)
3404 KASSERT(if_com_alloc[type] != NULL,
3405 ("if_deregister_com_alloc: %d not registered", type));
3406 KASSERT(if_com_free[type] != NULL,
3407 ("if_deregister_com_alloc: %d free not registered", type));
3408 if_com_alloc[type] = NULL;
3409 if_com_free[type] = NULL;
3414 if_show_ifnet(struct ifnet *ifp)
3419 db_printf("%s:\n", ifp->if_xname);
3420 #define IF_DB_PRINTF(f, e) db_printf(" %s = " f "\n", #e, ifp->e);
3421 IF_DB_PRINTF("%s", if_dname);
3422 IF_DB_PRINTF("%d", if_dunit);
3423 IF_DB_PRINTF("%s", if_description);
3424 IF_DB_PRINTF("%u", if_index);
3425 IF_DB_PRINTF("%u", if_refcount);
3426 IF_DB_PRINTF("%p", if_softc);
3427 IF_DB_PRINTF("%p", if_l2com);
3428 IF_DB_PRINTF("%p", if_vnet);
3429 IF_DB_PRINTF("%p", if_home_vnet);
3430 IF_DB_PRINTF("%p", if_addr);
3431 IF_DB_PRINTF("%p", if_llsoftc);
3432 IF_DB_PRINTF("%p", if_label);
3433 IF_DB_PRINTF("%u", if_pcount);
3434 IF_DB_PRINTF("0x%08x", if_flags);
3435 IF_DB_PRINTF("0x%08x", if_drv_flags);
3436 IF_DB_PRINTF("0x%08x", if_capabilities);
3437 IF_DB_PRINTF("0x%08x", if_capenable);
3438 IF_DB_PRINTF("%p", if_snd.ifq_head);
3439 IF_DB_PRINTF("%p", if_snd.ifq_tail);
3440 IF_DB_PRINTF("%d", if_snd.ifq_len);
3441 IF_DB_PRINTF("%d", if_snd.ifq_maxlen);
3442 IF_DB_PRINTF("%d", if_snd.ifq_drops);
3443 IF_DB_PRINTF("%p", if_snd.ifq_drv_head);
3444 IF_DB_PRINTF("%p", if_snd.ifq_drv_tail);
3445 IF_DB_PRINTF("%d", if_snd.ifq_drv_len);
3446 IF_DB_PRINTF("%d", if_snd.ifq_drv_maxlen);
3447 IF_DB_PRINTF("%d", if_snd.altq_type);
3448 IF_DB_PRINTF("%x", if_snd.altq_flags);
3452 DB_SHOW_COMMAND(ifnet, db_show_ifnet)
3456 db_printf("usage: show ifnet <struct ifnet *>\n");
3460 if_show_ifnet((struct ifnet *)addr);
3463 DB_SHOW_ALL_COMMAND(ifnets, db_show_all_ifnets)
3465 VNET_ITERATOR_DECL(vnet_iter);
3469 VNET_FOREACH(vnet_iter) {
3470 CURVNET_SET_QUIET(vnet_iter);
3472 db_printf("vnet=%p\n", curvnet);
3474 for (idx = 1; idx <= V_if_index; idx++) {
3475 ifp = V_ifindex_table[idx].ife_ifnet;
3478 db_printf( "%20s ifp=%p\n", ifp->if_xname, ifp);