2 * Copyright (c) 1980, 1986, 1993
3 * The Regents of the University of California. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 4. Neither the name of the University nor the names of its contributors
14 * may be used to endorse or promote products derived from this software
15 * without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * @(#)if.c 8.5 (Berkeley) 1/9/95
33 #include "opt_compat.h"
34 #include "opt_inet6.h"
38 #include <sys/param.h>
39 #include <sys/types.h>
41 #include <sys/malloc.h>
45 #include <sys/systm.h>
48 #include <sys/socket.h>
49 #include <sys/socketvar.h>
50 #include <sys/protosw.h>
51 #include <sys/kernel.h>
53 #include <sys/refcount.h>
54 #include <sys/module.h>
55 #include <sys/rwlock.h>
56 #include <sys/sockio.h>
57 #include <sys/syslog.h>
58 #include <sys/sysctl.h>
59 #include <sys/taskqueue.h>
60 #include <sys/domain.h>
62 #include <machine/stdarg.h>
66 #include <net/if_arp.h>
67 #include <net/if_clone.h>
68 #include <net/if_dl.h>
69 #include <net/if_types.h>
70 #include <net/if_var.h>
71 #include <net/radix.h>
72 #include <net/route.h>
75 #if defined(INET) || defined(INET6)
77 #include <netinet/in.h>
78 #include <netinet/in_var.h>
80 #include <netinet6/in6_var.h>
81 #include <netinet6/in6_ifattach.h>
85 #include <netinet/if_ether.h>
87 #if defined(INET) || defined(INET6)
89 #include <netinet/ip_carp.h>
93 #include <security/mac/mac_framework.h>
95 struct ifindex_entry {
96 struct ifnet *ife_ifnet;
99 static int slowtimo_started;
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 /* Log link state change events */
105 static int log_link_state_change = 1;
107 SYSCTL_INT(_net_link, OID_AUTO, log_link_state_change, CTLFLAG_RW,
108 &log_link_state_change, 0,
109 "log interface link state change events");
111 void (*bstp_linkstate_p)(struct ifnet *ifp, int state);
112 void (*ng_ether_link_state_p)(struct ifnet *ifp, int state);
113 void (*lagg_linkstate_p)(struct ifnet *ifp, int state);
115 struct mbuf *(*tbr_dequeue_ptr)(struct ifaltq *, int) = NULL;
118 * XXX: Style; these should be sorted alphabetically, and unprototyped
119 * static functions should be prototyped. Currently they are sorted by
122 static void if_attachdomain(void *);
123 static void if_attachdomain1(struct ifnet *);
124 static int ifconf(u_long, caddr_t);
125 static void if_freemulti(struct ifmultiaddr *);
126 static void if_init(void *);
127 static void if_grow(void);
128 static void if_check(void *);
129 static void if_route(struct ifnet *, int flag, int fam);
130 static int if_setflag(struct ifnet *, int, int, int *, int);
131 static void if_slowtimo(void *);
132 static int if_transmit(struct ifnet *ifp, struct mbuf *m);
133 static void if_unroute(struct ifnet *, int flag, int fam);
134 static void link_rtrequest(int, struct rtentry *, struct rt_addrinfo *);
135 static int if_rtdel(struct radix_node *, void *);
136 static int ifhwioctl(u_long, struct ifnet *, caddr_t, struct thread *);
137 static int if_delmulti_locked(struct ifnet *, struct ifmultiaddr *, int);
138 static void do_link_state_change(void *, int);
139 static int if_getgroup(struct ifgroupreq *, struct ifnet *);
140 static int if_getgroupmembers(struct ifgroupreq *);
141 static void if_delgroups(struct ifnet *);
142 static void if_attach_internal(struct ifnet *, int);
143 static void if_detach_internal(struct ifnet *, int);
147 * XXX: declare here to avoid to include many inet6 related files..
148 * should be more generalized?
150 extern void nd6_setmtu(struct ifnet *);
153 VNET_DEFINE(struct ifnethead, ifnet); /* depend on static init XXX */
154 VNET_DEFINE(struct ifgrouphead, ifg_head);
155 VNET_DEFINE(int, if_index);
156 static VNET_DEFINE(int, if_indexlim) = 8;
158 /* Table of ifnet by index. */
159 static VNET_DEFINE(struct ifindex_entry *, ifindex_table);
161 #define V_if_indexlim VNET(if_indexlim)
162 #define V_ifindex_table VNET(ifindex_table)
164 int ifqmaxlen = IFQ_MAXLEN;
167 * The global network interface list (V_ifnet) and related state (such as
168 * if_index, if_indexlim, and ifindex_table) are protected by an sxlock and
169 * an rwlock. Either may be acquired shared to stablize the list, but both
170 * must be acquired writable to modify the list. This model allows us to
171 * both stablize the interface list during interrupt thread processing, but
172 * also to stablize it over long-running ioctls, without introducing priority
173 * inversions and deadlocks.
175 struct rwlock ifnet_rwlock;
176 struct sx ifnet_sxlock;
179 * The allocation of network interfaces is a rather non-atomic affair; we
180 * need to select an index before we are ready to expose the interface for
181 * use, so will use this pointer value to indicate reservation.
183 #define IFNET_HOLD (void *)(uintptr_t)(-1)
185 static if_com_alloc_t *if_com_alloc[256];
186 static if_com_free_t *if_com_free[256];
189 * System initialization
191 SYSINIT(interface_check, SI_SUB_PROTO_IF, SI_ORDER_FIRST, if_check, NULL);
193 MALLOC_DEFINE(M_IFNET, "ifnet", "interface internals");
194 MALLOC_DEFINE(M_IFADDR, "ifaddr", "interface address");
195 MALLOC_DEFINE(M_IFMADDR, "ether_multi", "link-level multicast address");
198 ifnet_byindex_locked(u_short idx)
201 if (idx > V_if_index)
203 if (V_ifindex_table[idx].ife_ifnet == IFNET_HOLD)
205 return (V_ifindex_table[idx].ife_ifnet);
209 ifnet_byindex(u_short idx)
213 IFNET_RLOCK_NOSLEEP();
214 ifp = ifnet_byindex_locked(idx);
215 IFNET_RUNLOCK_NOSLEEP();
220 ifnet_byindex_ref(u_short idx)
224 IFNET_RLOCK_NOSLEEP();
225 ifp = ifnet_byindex_locked(idx);
226 if (ifp == NULL || (ifp->if_flags & IFF_DYING)) {
227 IFNET_RUNLOCK_NOSLEEP();
231 IFNET_RUNLOCK_NOSLEEP();
236 * Allocate an ifindex array entry; return 0 on success or an error on
240 ifindex_alloc_locked(u_short *idxp)
244 IFNET_WLOCK_ASSERT();
247 * Try to find an empty slot below V_if_index. If we fail, take the
250 for (idx = 1; idx <= V_if_index; idx++) {
251 if (V_ifindex_table[idx].ife_ifnet == NULL)
255 /* Catch if_index overflow. */
258 if (idx > V_if_index)
260 if (V_if_index >= V_if_indexlim)
267 ifindex_free_locked(u_short idx)
270 IFNET_WLOCK_ASSERT();
272 V_ifindex_table[idx].ife_ifnet = NULL;
273 while (V_if_index > 0 &&
274 V_ifindex_table[V_if_index].ife_ifnet == NULL)
279 ifindex_free(u_short idx)
283 ifindex_free_locked(idx);
288 ifnet_setbyindex_locked(u_short idx, struct ifnet *ifp)
291 IFNET_WLOCK_ASSERT();
293 V_ifindex_table[idx].ife_ifnet = ifp;
297 ifnet_setbyindex(u_short idx, struct ifnet *ifp)
301 ifnet_setbyindex_locked(idx, ifp);
306 ifaddr_byindex(u_short idx)
310 IFNET_RLOCK_NOSLEEP();
311 ifa = ifnet_byindex_locked(idx)->if_addr;
314 IFNET_RUNLOCK_NOSLEEP();
319 * Network interface utility routines.
321 * Routines with ifa_ifwith* names take sockaddr *'s as
326 vnet_if_init(const void *unused __unused)
329 TAILQ_INIT(&V_ifnet);
330 TAILQ_INIT(&V_ifg_head);
331 if_grow(); /* create initial table */
332 vnet_if_clone_init();
334 VNET_SYSINIT(vnet_if_init, SI_SUB_INIT_IF, SI_ORDER_FIRST, vnet_if_init,
339 if_init(void *dummy __unused)
345 SYSINIT(interfaces, SI_SUB_INIT_IF, SI_ORDER_SECOND, if_init, NULL);
350 vnet_if_uninit(const void *unused __unused)
353 VNET_ASSERT(TAILQ_EMPTY(&V_ifnet));
354 VNET_ASSERT(TAILQ_EMPTY(&V_ifg_head));
356 free((caddr_t)V_ifindex_table, M_IFNET);
358 VNET_SYSUNINIT(vnet_if_uninit, SI_SUB_INIT_IF, SI_ORDER_FIRST,
359 vnet_if_uninit, NULL);
366 struct ifindex_entry *e;
369 n = V_if_indexlim * sizeof(*e);
370 e = malloc(n, M_IFNET, M_WAITOK | M_ZERO);
371 if (V_ifindex_table != NULL) {
372 memcpy((caddr_t)e, (caddr_t)V_ifindex_table, n/2);
373 free((caddr_t)V_ifindex_table, M_IFNET);
379 if_check(void *dummy __unused)
383 * If at least one interface added during boot uses
384 * if_watchdog then start the timer.
386 if (slowtimo_started)
391 * Allocate a struct ifnet and an index for an interface. A layer 2
392 * common structure will also be allocated if an allocation routine is
393 * registered for the passed type.
396 if_alloc(u_char type)
401 ifp = malloc(sizeof(struct ifnet), M_IFNET, M_WAITOK|M_ZERO);
403 if (ifindex_alloc_locked(&idx) != 0) {
408 ifnet_setbyindex_locked(idx, IFNET_HOLD);
412 ifp->if_alloctype = type;
413 if (if_com_alloc[type] != NULL) {
414 ifp->if_l2com = if_com_alloc[type](type, ifp);
415 if (ifp->if_l2com == NULL) {
422 IF_ADDR_LOCK_INIT(ifp);
423 TASK_INIT(&ifp->if_linktask, 0, do_link_state_change, ifp);
424 ifp->if_afdata_initialized = 0;
425 IF_AFDATA_LOCK_INIT(ifp);
426 TAILQ_INIT(&ifp->if_addrhead);
427 TAILQ_INIT(&ifp->if_prefixhead);
428 TAILQ_INIT(&ifp->if_multiaddrs);
429 TAILQ_INIT(&ifp->if_groups);
433 ifq_init(&ifp->if_snd, ifp);
435 refcount_init(&ifp->if_refcount, 1); /* Index reference. */
436 ifnet_setbyindex(ifp->if_index, ifp);
441 * Do the actual work of freeing a struct ifnet, associated index, and layer
442 * 2 common structure. This call is made when the last reference to an
443 * interface is released.
446 if_free_internal(struct ifnet *ifp)
449 KASSERT((ifp->if_flags & IFF_DYING),
450 ("if_free_internal: interface not dying"));
453 KASSERT(ifp == ifnet_byindex_locked(ifp->if_index),
454 ("%s: freeing unallocated ifnet", ifp->if_xname));
456 ifindex_free_locked(ifp->if_index);
459 if (if_com_free[ifp->if_alloctype] != NULL)
460 if_com_free[ifp->if_alloctype](ifp->if_l2com,
464 mac_ifnet_destroy(ifp);
466 IF_AFDATA_DESTROY(ifp);
467 IF_ADDR_LOCK_DESTROY(ifp);
468 ifq_delete(&ifp->if_snd);
473 * This version should only be called by intefaces that switch their type
474 * after calling if_alloc(). if_free_type() will go away again now that we
475 * have if_alloctype to cache the original allocation type. For now, assert
476 * that they match, since we require that in practice.
479 if_free_type(struct ifnet *ifp, u_char type)
482 KASSERT(ifp->if_alloctype == type,
483 ("if_free_type: type (%d) != alloctype (%d)", type,
486 ifp->if_flags |= IFF_DYING; /* XXX: Locking */
487 if (!refcount_release(&ifp->if_refcount))
489 if_free_internal(ifp);
493 * This is the normal version of if_free(), used by device drivers to free a
494 * detached network interface. The contents of if_free_type() will move into
495 * here when if_free_type() goes away.
498 if_free(struct ifnet *ifp)
501 if_free_type(ifp, ifp->if_alloctype);
505 * Interfaces to keep an ifnet type-stable despite the possibility of the
506 * driver calling if_free(). If there are additional references, we defer
507 * freeing the underlying data structure.
510 if_ref(struct ifnet *ifp)
513 /* We don't assert the ifnet list lock here, but arguably should. */
514 refcount_acquire(&ifp->if_refcount);
518 if_rele(struct ifnet *ifp)
521 if (!refcount_release(&ifp->if_refcount))
523 if_free_internal(ifp);
527 ifq_init(struct ifaltq *ifq, struct ifnet *ifp)
530 mtx_init(&ifq->ifq_mtx, ifp->if_xname, "if send queue", MTX_DEF);
532 if (ifq->ifq_maxlen == 0)
533 ifq->ifq_maxlen = ifqmaxlen;
536 ifq->altq_disc = NULL;
537 ifq->altq_flags &= ALTQF_CANTCHANGE;
538 ifq->altq_tbr = NULL;
543 ifq_delete(struct ifaltq *ifq)
545 mtx_destroy(&ifq->ifq_mtx);
549 * Perform generic interface initalization tasks and attach the interface
550 * to the list of "active" interfaces. If vmove flag is set on entry
551 * to if_attach_internal(), perform only a limited subset of initialization
552 * tasks, given that we are moving from one vnet to another an ifnet which
553 * has already been fully initialized.
556 * - The decision to return void and thus require this function to
557 * succeed is questionable.
558 * - We should probably do more sanity checking. For instance we don't
559 * do anything to insure if_xname is unique or non-empty.
562 if_attach(struct ifnet *ifp)
565 if_attach_internal(ifp, 0);
569 if_attach_internal(struct ifnet *ifp, int vmove)
571 unsigned socksize, ifasize;
572 int namelen, masklen;
573 struct sockaddr_dl *sdl;
576 if (ifp->if_index == 0 || ifp != ifnet_byindex(ifp->if_index))
577 panic ("%s: BUG: if_attach called without if_alloc'd input()\n",
581 ifp->if_vnet = curvnet;
582 if (ifp->if_home_vnet == NULL)
583 ifp->if_home_vnet = curvnet;
586 if_addgroup(ifp, IFG_ALL);
588 getmicrotime(&ifp->if_lastchange);
589 ifp->if_data.ifi_epoch = time_uptime;
590 ifp->if_data.ifi_datalen = sizeof(struct if_data);
592 KASSERT((ifp->if_transmit == NULL && ifp->if_qflush == NULL) ||
593 (ifp->if_transmit != NULL && ifp->if_qflush != NULL),
594 ("transmit and qflush must both either be set or both be NULL"));
595 if (ifp->if_transmit == NULL) {
596 ifp->if_transmit = if_transmit;
597 ifp->if_qflush = if_qflush;
602 mac_ifnet_create(ifp);
606 * Create a Link Level name for this device.
608 namelen = strlen(ifp->if_xname);
610 * Always save enough space for any possiable name so we
611 * can do a rename in place later.
613 masklen = offsetof(struct sockaddr_dl, sdl_data[0]) + IFNAMSIZ;
614 socksize = masklen + ifp->if_addrlen;
615 if (socksize < sizeof(*sdl))
616 socksize = sizeof(*sdl);
617 socksize = roundup2(socksize, sizeof(long));
618 ifasize = sizeof(*ifa) + 2 * socksize;
619 ifa = malloc(ifasize, M_IFADDR, M_WAITOK | M_ZERO);
621 sdl = (struct sockaddr_dl *)(ifa + 1);
622 sdl->sdl_len = socksize;
623 sdl->sdl_family = AF_LINK;
624 bcopy(ifp->if_xname, sdl->sdl_data, namelen);
625 sdl->sdl_nlen = namelen;
626 sdl->sdl_index = ifp->if_index;
627 sdl->sdl_type = ifp->if_type;
630 ifa->ifa_rtrequest = link_rtrequest;
631 ifa->ifa_addr = (struct sockaddr *)sdl;
632 sdl = (struct sockaddr_dl *)(socksize + (caddr_t)sdl);
633 ifa->ifa_netmask = (struct sockaddr *)sdl;
634 sdl->sdl_len = masklen;
636 sdl->sdl_data[--namelen] = 0xff;
637 TAILQ_INSERT_HEAD(&ifp->if_addrhead, ifa, ifa_link);
638 /* Reliably crash if used uninitialized. */
639 ifp->if_broadcastaddr = NULL;
644 * Update the interface index in the link layer address
647 for (ifa = ifp->if_addr; ifa != NULL;
648 ifa = TAILQ_NEXT(ifa, ifa_link)) {
649 if (ifa->ifa_addr->sa_family == AF_LINK) {
650 sdl = (struct sockaddr_dl *)ifa->ifa_addr;
651 sdl->sdl_index = ifp->if_index;
658 TAILQ_INSERT_TAIL(&V_ifnet, ifp, if_link);
660 curvnet->vnet_ifcnt++;
664 if (domain_init_status >= 2)
665 if_attachdomain1(ifp);
667 EVENTHANDLER_INVOKE(ifnet_arrival_event, ifp);
668 if (IS_DEFAULT_VNET(curvnet))
669 devctl_notify("IFNET", ifp->if_xname, "ATTACH", NULL);
671 /* Announce the interface. */
672 rt_ifannouncemsg(ifp, IFAN_ARRIVAL);
674 if (!vmove && ifp->if_watchdog != NULL) {
676 "WARNING: using obsoleted if_watchdog interface\n");
679 * Note that we need if_slowtimo(). If this happens after
680 * boot, then call if_slowtimo() directly.
682 if (atomic_cmpset_int(&slowtimo_started, 0, 1) && !cold)
688 if_attachdomain(void *dummy)
694 TAILQ_FOREACH(ifp, &V_ifnet, if_link)
695 if_attachdomain1(ifp);
698 SYSINIT(domainifattach, SI_SUB_PROTO_IFATTACHDOMAIN, SI_ORDER_SECOND,
699 if_attachdomain, NULL);
702 if_attachdomain1(struct ifnet *ifp)
710 * Since dp->dom_ifattach calls malloc() with M_WAITOK, we
711 * cannot lock ifp->if_afdata initialization, entirely.
713 if (IF_AFDATA_TRYLOCK(ifp) == 0) {
717 if (ifp->if_afdata_initialized >= domain_init_status) {
718 IF_AFDATA_UNLOCK(ifp);
720 printf("if_attachdomain called more than once on %s\n",
724 ifp->if_afdata_initialized = domain_init_status;
725 IF_AFDATA_UNLOCK(ifp);
727 /* address family dependent data region */
728 bzero(ifp->if_afdata, sizeof(ifp->if_afdata));
729 for (dp = domains; dp; dp = dp->dom_next) {
730 if (dp->dom_ifattach)
731 ifp->if_afdata[dp->dom_family] =
732 (*dp->dom_ifattach)(ifp);
739 * Remove any unicast or broadcast network addresses from an interface.
742 if_purgeaddrs(struct ifnet *ifp)
744 struct ifaddr *ifa, *next;
746 TAILQ_FOREACH_SAFE(ifa, &ifp->if_addrhead, ifa_link, next) {
747 if (ifa->ifa_addr->sa_family == AF_LINK)
750 /* XXX: Ugly!! ad hoc just for INET */
751 if (ifa->ifa_addr->sa_family == AF_INET) {
752 struct ifaliasreq ifr;
754 bzero(&ifr, sizeof(ifr));
755 ifr.ifra_addr = *ifa->ifa_addr;
756 if (ifa->ifa_dstaddr)
757 ifr.ifra_broadaddr = *ifa->ifa_dstaddr;
758 if (in_control(NULL, SIOCDIFADDR, (caddr_t)&ifr, ifp,
764 if (ifa->ifa_addr->sa_family == AF_INET6) {
766 /* ifp_addrhead is already updated */
770 TAILQ_REMOVE(&ifp->if_addrhead, ifa, ifa_link);
776 * Remove any multicast network addresses from an interface.
779 if_purgemaddrs(struct ifnet *ifp)
781 struct ifmultiaddr *ifma;
782 struct ifmultiaddr *next;
785 TAILQ_FOREACH_SAFE(ifma, &ifp->if_multiaddrs, ifma_link, next)
786 if_delmulti_locked(ifp, ifma, 1);
791 * Detach an interface, removing it from the list of "active" interfaces.
792 * If vmove flag is set on entry to if_detach_internal(), perform only a
793 * limited subset of cleanup tasks, given that we are moving an ifnet from
794 * one vnet to another, where it must be fully operational.
796 * XXXRW: There are some significant questions about event ordering, and
797 * how to prevent things from starting to use the interface during detach.
800 if_detach(struct ifnet *ifp)
803 if_detach_internal(ifp, 0);
807 if_detach_internal(struct ifnet *ifp, int vmove)
810 struct radix_node_head *rnh;
817 TAILQ_FOREACH(iter, &V_ifnet, if_link)
819 TAILQ_REMOVE(&V_ifnet, ifp, if_link);
825 curvnet->vnet_ifcnt--;
830 panic("interface not in it's own ifnet list");
832 return; /* XXX this should panic as well? */
836 * Remove/wait for pending events.
838 taskqueue_drain(taskqueue_swi, &ifp->if_linktask);
841 * Remove routes and flush queues.
845 if (ALTQ_IS_ENABLED(&ifp->if_snd))
846 altq_disable(&ifp->if_snd);
847 if (ALTQ_IS_ATTACHED(&ifp->if_snd))
848 altq_detach(&ifp->if_snd);
859 * Remove all IPv6 kernel structs related to ifp. This should be done
860 * before removing routing entries below, since IPv6 interface direct
861 * routes are expected to be removed by the IPv6-specific kernel API.
862 * Otherwise, the kernel will detect some inconsistency and bark it.
870 * Prevent further calls into the device driver via ifnet.
875 * Remove link ifaddr pointer and maybe decrement if_index.
876 * Clean up all addresses.
880 /* We can now free link ifaddr. */
881 if (!TAILQ_EMPTY(&ifp->if_addrhead)) {
882 ifa = TAILQ_FIRST(&ifp->if_addrhead);
883 TAILQ_REMOVE(&ifp->if_addrhead, ifa, ifa_link);
889 * Delete all remaining routes using this interface
890 * Unfortuneatly the only way to do this is to slog through
891 * the entire routing table looking for routes which point
892 * to this interface...oh well...
894 for (i = 1; i <= AF_MAX; i++) {
895 for (j = 0; j < rt_numfibs; j++) {
896 rnh = rt_tables_get_rnh(j, i);
899 RADIX_NODE_HEAD_LOCK(rnh);
900 (void) rnh->rnh_walktree(rnh, if_rtdel, ifp);
901 RADIX_NODE_HEAD_UNLOCK(rnh);
905 /* Announce that the interface is gone. */
906 rt_ifannouncemsg(ifp, IFAN_DEPARTURE);
907 EVENTHANDLER_INVOKE(ifnet_departure_event, ifp);
908 if (IS_DEFAULT_VNET(curvnet))
909 devctl_notify("IFNET", ifp->if_xname, "DETACH", NULL);
913 for (dp = domains; dp; dp = dp->dom_next) {
914 if (dp->dom_ifdetach && ifp->if_afdata[dp->dom_family])
915 (*dp->dom_ifdetach)(ifp,
916 ifp->if_afdata[dp->dom_family]);
918 ifp->if_afdata_initialized = 0;
919 IF_AFDATA_UNLOCK(ifp);
924 * if_vmove() performs a limited version of if_detach() in current
925 * vnet and if_attach()es the ifnet to the vnet specified as 2nd arg.
926 * An attempt is made to shrink if_index in current vnet, find an
927 * unused if_index in target vnet and calls if_grow() if necessary,
928 * and finally find an unused if_xname for the target vnet.
931 if_vmove(struct ifnet *ifp, struct vnet *new_vnet)
936 * Detach from current vnet, but preserve LLADDR info, do not
937 * mark as dead etc. so that the ifnet can be reattached later.
939 if_detach_internal(ifp, 1);
942 * Unlink the ifnet from ifindex_table[] in current vnet, and shrink
943 * the if_index for that vnet if possible.
945 * NOTE: IFNET_WLOCK/IFNET_WUNLOCK() are assumed to be unvirtualized,
946 * or we'd lock on one vnet and unlock on another.
949 ifindex_free_locked(ifp->if_index);
952 * Switch to the context of the target vnet.
954 CURVNET_SET_QUIET(new_vnet);
956 if (ifindex_alloc_locked(&idx) != 0) {
958 panic("if_index overflow");
961 ifnet_setbyindex_locked(ifp->if_index, ifp);
964 if_attach_internal(ifp, 1);
970 * Move an ifnet to or from another child prison/vnet, specified by the jail id.
973 if_vmove_loan(struct thread *td, struct ifnet *ifp, char *ifname, int jid)
978 /* Try to find the prison within our visibility. */
979 sx_slock(&allprison_lock);
980 pr = prison_find_child(td->td_ucred->cr_prison, jid);
981 sx_sunlock(&allprison_lock);
984 prison_hold_locked(pr);
985 mtx_unlock(&pr->pr_mtx);
987 /* Do not try to move the iface from and to the same prison. */
988 if (pr->pr_vnet == ifp->if_vnet) {
993 /* Make sure the named iface does not exists in the dst. prison/vnet. */
994 /* XXX Lock interfaces to avoid races. */
995 CURVNET_SET_QUIET(pr->pr_vnet);
996 difp = ifunit(ifname);
1003 /* Move the interface into the child jail/vnet. */
1004 if_vmove(ifp, pr->pr_vnet);
1006 /* Report the new if_xname back to the userland. */
1007 sprintf(ifname, "%s", ifp->if_xname);
1014 if_vmove_reclaim(struct thread *td, char *ifname, int jid)
1017 struct vnet *vnet_dst;
1020 /* Try to find the prison within our visibility. */
1021 sx_slock(&allprison_lock);
1022 pr = prison_find_child(td->td_ucred->cr_prison, jid);
1023 sx_sunlock(&allprison_lock);
1026 prison_hold_locked(pr);
1027 mtx_unlock(&pr->pr_mtx);
1029 /* Make sure the named iface exists in the source prison/vnet. */
1030 CURVNET_SET(pr->pr_vnet);
1031 ifp = ifunit(ifname); /* XXX Lock to avoid races. */
1038 /* Do not try to move the iface from and to the same prison. */
1039 vnet_dst = TD_TO_VNET(td);
1040 if (vnet_dst == ifp->if_vnet) {
1046 /* Get interface back from child jail/vnet. */
1047 if_vmove(ifp, vnet_dst);
1050 /* Report the new if_xname back to the userland. */
1051 sprintf(ifname, "%s", ifp->if_xname);
1059 * Add a group to an interface
1062 if_addgroup(struct ifnet *ifp, const char *groupname)
1064 struct ifg_list *ifgl;
1065 struct ifg_group *ifg = NULL;
1066 struct ifg_member *ifgm;
1068 if (groupname[0] && groupname[strlen(groupname) - 1] >= '0' &&
1069 groupname[strlen(groupname) - 1] <= '9')
1073 TAILQ_FOREACH(ifgl, &ifp->if_groups, ifgl_next)
1074 if (!strcmp(ifgl->ifgl_group->ifg_group, groupname)) {
1079 if ((ifgl = (struct ifg_list *)malloc(sizeof(struct ifg_list), M_TEMP,
1080 M_NOWAIT)) == NULL) {
1085 if ((ifgm = (struct ifg_member *)malloc(sizeof(struct ifg_member),
1086 M_TEMP, M_NOWAIT)) == NULL) {
1092 TAILQ_FOREACH(ifg, &V_ifg_head, ifg_next)
1093 if (!strcmp(ifg->ifg_group, groupname))
1097 if ((ifg = (struct ifg_group *)malloc(sizeof(struct ifg_group),
1098 M_TEMP, M_NOWAIT)) == NULL) {
1104 strlcpy(ifg->ifg_group, groupname, sizeof(ifg->ifg_group));
1105 ifg->ifg_refcnt = 0;
1106 TAILQ_INIT(&ifg->ifg_members);
1107 EVENTHANDLER_INVOKE(group_attach_event, ifg);
1108 TAILQ_INSERT_TAIL(&V_ifg_head, ifg, ifg_next);
1112 ifgl->ifgl_group = ifg;
1113 ifgm->ifgm_ifp = ifp;
1116 TAILQ_INSERT_TAIL(&ifg->ifg_members, ifgm, ifgm_next);
1117 TAILQ_INSERT_TAIL(&ifp->if_groups, ifgl, ifgl_next);
1118 IF_ADDR_UNLOCK(ifp);
1122 EVENTHANDLER_INVOKE(group_change_event, groupname);
1128 * Remove a group from an interface
1131 if_delgroup(struct ifnet *ifp, const char *groupname)
1133 struct ifg_list *ifgl;
1134 struct ifg_member *ifgm;
1137 TAILQ_FOREACH(ifgl, &ifp->if_groups, ifgl_next)
1138 if (!strcmp(ifgl->ifgl_group->ifg_group, groupname))
1146 TAILQ_REMOVE(&ifp->if_groups, ifgl, ifgl_next);
1147 IF_ADDR_UNLOCK(ifp);
1149 TAILQ_FOREACH(ifgm, &ifgl->ifgl_group->ifg_members, ifgm_next)
1150 if (ifgm->ifgm_ifp == ifp)
1154 TAILQ_REMOVE(&ifgl->ifgl_group->ifg_members, ifgm, ifgm_next);
1158 if (--ifgl->ifgl_group->ifg_refcnt == 0) {
1159 TAILQ_REMOVE(&V_ifg_head, ifgl->ifgl_group, ifg_next);
1160 EVENTHANDLER_INVOKE(group_detach_event, ifgl->ifgl_group);
1161 free(ifgl->ifgl_group, M_TEMP);
1167 EVENTHANDLER_INVOKE(group_change_event, groupname);
1173 * Remove an interface from all groups
1176 if_delgroups(struct ifnet *ifp)
1178 struct ifg_list *ifgl;
1179 struct ifg_member *ifgm;
1180 char groupname[IFNAMSIZ];
1183 while (!TAILQ_EMPTY(&ifp->if_groups)) {
1184 ifgl = TAILQ_FIRST(&ifp->if_groups);
1186 strlcpy(groupname, ifgl->ifgl_group->ifg_group, IFNAMSIZ);
1189 TAILQ_REMOVE(&ifp->if_groups, ifgl, ifgl_next);
1190 IF_ADDR_UNLOCK(ifp);
1192 TAILQ_FOREACH(ifgm, &ifgl->ifgl_group->ifg_members, ifgm_next)
1193 if (ifgm->ifgm_ifp == ifp)
1197 TAILQ_REMOVE(&ifgl->ifgl_group->ifg_members, ifgm,
1202 if (--ifgl->ifgl_group->ifg_refcnt == 0) {
1203 TAILQ_REMOVE(&V_ifg_head, ifgl->ifgl_group, ifg_next);
1204 EVENTHANDLER_INVOKE(group_detach_event,
1206 free(ifgl->ifgl_group, M_TEMP);
1212 EVENTHANDLER_INVOKE(group_change_event, groupname);
1220 * Stores all groups from an interface in memory pointed
1224 if_getgroup(struct ifgroupreq *data, struct ifnet *ifp)
1227 struct ifg_list *ifgl;
1228 struct ifg_req ifgrq, *ifgp;
1229 struct ifgroupreq *ifgr = data;
1231 if (ifgr->ifgr_len == 0) {
1233 TAILQ_FOREACH(ifgl, &ifp->if_groups, ifgl_next)
1234 ifgr->ifgr_len += sizeof(struct ifg_req);
1235 IF_ADDR_UNLOCK(ifp);
1239 len = ifgr->ifgr_len;
1240 ifgp = ifgr->ifgr_groups;
1243 TAILQ_FOREACH(ifgl, &ifp->if_groups, ifgl_next) {
1244 if (len < sizeof(ifgrq)) {
1245 IF_ADDR_UNLOCK(ifp);
1248 bzero(&ifgrq, sizeof ifgrq);
1249 strlcpy(ifgrq.ifgrq_group, ifgl->ifgl_group->ifg_group,
1250 sizeof(ifgrq.ifgrq_group));
1251 if ((error = copyout(&ifgrq, ifgp, sizeof(struct ifg_req)))) {
1252 IF_ADDR_UNLOCK(ifp);
1255 len -= sizeof(ifgrq);
1258 IF_ADDR_UNLOCK(ifp);
1264 * Stores all members of a group in memory pointed to by data
1267 if_getgroupmembers(struct ifgroupreq *data)
1269 struct ifgroupreq *ifgr = data;
1270 struct ifg_group *ifg;
1271 struct ifg_member *ifgm;
1272 struct ifg_req ifgrq, *ifgp;
1276 TAILQ_FOREACH(ifg, &V_ifg_head, ifg_next)
1277 if (!strcmp(ifg->ifg_group, ifgr->ifgr_name))
1284 if (ifgr->ifgr_len == 0) {
1285 TAILQ_FOREACH(ifgm, &ifg->ifg_members, ifgm_next)
1286 ifgr->ifgr_len += sizeof(ifgrq);
1291 len = ifgr->ifgr_len;
1292 ifgp = ifgr->ifgr_groups;
1293 TAILQ_FOREACH(ifgm, &ifg->ifg_members, ifgm_next) {
1294 if (len < sizeof(ifgrq)) {
1298 bzero(&ifgrq, sizeof ifgrq);
1299 strlcpy(ifgrq.ifgrq_member, ifgm->ifgm_ifp->if_xname,
1300 sizeof(ifgrq.ifgrq_member));
1301 if ((error = copyout(&ifgrq, ifgp, sizeof(struct ifg_req)))) {
1305 len -= sizeof(ifgrq);
1314 * Delete Routes for a Network Interface
1316 * Called for each routing entry via the rnh->rnh_walktree() call above
1317 * to delete all route entries referencing a detaching network interface.
1320 * rn pointer to node in the routing table
1321 * arg argument passed to rnh->rnh_walktree() - detaching interface
1325 * errno failed - reason indicated
1329 if_rtdel(struct radix_node *rn, void *arg)
1331 struct rtentry *rt = (struct rtentry *)rn;
1332 struct ifnet *ifp = arg;
1335 if (rt->rt_ifp == ifp) {
1338 * Protect (sorta) against walktree recursion problems
1339 * with cloned routes
1341 if ((rt->rt_flags & RTF_UP) == 0)
1344 err = rtrequest_fib(RTM_DELETE, rt_key(rt), rt->rt_gateway,
1345 rt_mask(rt), rt->rt_flags|RTF_RNH_LOCKED,
1346 (struct rtentry **) NULL, rt->rt_fibnum);
1348 log(LOG_WARNING, "if_rtdel: error %d\n", err);
1356 * Wrapper functions for struct ifnet address list locking macros. These are
1357 * used by kernel modules to avoid encoding programming interface or binary
1358 * interface assumptions that may be violated when kernel-internal locking
1359 * approaches change.
1362 if_addr_rlock(struct ifnet *ifp)
1369 if_addr_runlock(struct ifnet *ifp)
1372 IF_ADDR_UNLOCK(ifp);
1376 if_maddr_rlock(struct ifnet *ifp)
1383 if_maddr_runlock(struct ifnet *ifp)
1386 IF_ADDR_UNLOCK(ifp);
1390 * Reference count functions for ifaddrs.
1393 ifa_init(struct ifaddr *ifa)
1396 mtx_init(&ifa->ifa_mtx, "ifaddr", NULL, MTX_DEF);
1397 refcount_init(&ifa->ifa_refcnt, 1);
1401 ifa_ref(struct ifaddr *ifa)
1404 refcount_acquire(&ifa->ifa_refcnt);
1408 ifa_free(struct ifaddr *ifa)
1411 if (refcount_release(&ifa->ifa_refcnt)) {
1412 mtx_destroy(&ifa->ifa_mtx);
1413 free(ifa, M_IFADDR);
1418 ifa_add_loopback_route(struct ifaddr *ifa, struct sockaddr *ia)
1421 struct rtentry *rt = NULL;
1422 struct rt_addrinfo info;
1423 static struct sockaddr_dl null_sdl = {sizeof(null_sdl), AF_LINK};
1425 bzero(&info, sizeof(info));
1426 info.rti_ifp = V_loif;
1427 info.rti_flags = ifa->ifa_flags | RTF_HOST | RTF_STATIC;
1428 info.rti_info[RTAX_DST] = ia;
1429 info.rti_info[RTAX_GATEWAY] = (struct sockaddr *)&null_sdl;
1430 error = rtrequest1_fib(RTM_ADD, &info, &rt, 0);
1432 if (error == 0 && rt != NULL) {
1434 ((struct sockaddr_dl *)rt->rt_gateway)->sdl_type =
1435 ifa->ifa_ifp->if_type;
1436 ((struct sockaddr_dl *)rt->rt_gateway)->sdl_index =
1437 ifa->ifa_ifp->if_index;
1440 } else if (error != 0)
1441 log(LOG_INFO, "ifa_add_loopback_route: insertion failed\n");
1447 ifa_del_loopback_route(struct ifaddr *ifa, struct sockaddr *ia)
1450 struct rt_addrinfo info;
1451 struct sockaddr_dl null_sdl;
1453 bzero(&null_sdl, sizeof(null_sdl));
1454 null_sdl.sdl_len = sizeof(null_sdl);
1455 null_sdl.sdl_family = AF_LINK;
1456 null_sdl.sdl_type = ifa->ifa_ifp->if_type;
1457 null_sdl.sdl_index = ifa->ifa_ifp->if_index;
1458 bzero(&info, sizeof(info));
1459 info.rti_flags = ifa->ifa_flags | RTF_HOST | RTF_STATIC;
1460 info.rti_info[RTAX_DST] = ia;
1461 info.rti_info[RTAX_GATEWAY] = (struct sockaddr *)&null_sdl;
1462 error = rtrequest1_fib(RTM_DELETE, &info, NULL, 0);
1465 log(LOG_INFO, "ifa_del_loopback_route: deletion failed\n");
1471 * XXX: Because sockaddr_dl has deeper structure than the sockaddr
1472 * structs used to represent other address families, it is necessary
1473 * to perform a different comparison.
1476 #define sa_equal(a1, a2) \
1477 (bcmp((a1), (a2), ((a1))->sa_len) == 0)
1479 #define sa_dl_equal(a1, a2) \
1480 ((((struct sockaddr_dl *)(a1))->sdl_len == \
1481 ((struct sockaddr_dl *)(a2))->sdl_len) && \
1482 (bcmp(LLADDR((struct sockaddr_dl *)(a1)), \
1483 LLADDR((struct sockaddr_dl *)(a2)), \
1484 ((struct sockaddr_dl *)(a1))->sdl_alen) == 0))
1487 * Locate an interface based on a complete address.
1490 static struct ifaddr *
1491 ifa_ifwithaddr_internal(struct sockaddr *addr, int getref)
1496 IFNET_RLOCK_NOSLEEP();
1497 TAILQ_FOREACH(ifp, &V_ifnet, if_link) {
1499 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
1500 if (ifa->ifa_addr->sa_family != addr->sa_family)
1502 if (sa_equal(addr, ifa->ifa_addr)) {
1505 IF_ADDR_UNLOCK(ifp);
1508 /* IP6 doesn't have broadcast */
1509 if ((ifp->if_flags & IFF_BROADCAST) &&
1510 ifa->ifa_broadaddr &&
1511 ifa->ifa_broadaddr->sa_len != 0 &&
1512 sa_equal(ifa->ifa_broadaddr, addr)) {
1515 IF_ADDR_UNLOCK(ifp);
1519 IF_ADDR_UNLOCK(ifp);
1523 IFNET_RUNLOCK_NOSLEEP();
1528 ifa_ifwithaddr(struct sockaddr *addr)
1531 return (ifa_ifwithaddr_internal(addr, 1));
1535 ifa_ifwithaddr_check(struct sockaddr *addr)
1538 return (ifa_ifwithaddr_internal(addr, 0) != NULL);
1542 * Locate an interface based on the broadcast address.
1546 ifa_ifwithbroadaddr(struct sockaddr *addr)
1551 IFNET_RLOCK_NOSLEEP();
1552 TAILQ_FOREACH(ifp, &V_ifnet, if_link) {
1554 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
1555 if (ifa->ifa_addr->sa_family != addr->sa_family)
1557 if ((ifp->if_flags & IFF_BROADCAST) &&
1558 ifa->ifa_broadaddr &&
1559 ifa->ifa_broadaddr->sa_len != 0 &&
1560 sa_equal(ifa->ifa_broadaddr, addr)) {
1562 IF_ADDR_UNLOCK(ifp);
1566 IF_ADDR_UNLOCK(ifp);
1570 IFNET_RUNLOCK_NOSLEEP();
1575 * Locate the point to point interface with a given destination address.
1579 ifa_ifwithdstaddr(struct sockaddr *addr)
1584 IFNET_RLOCK_NOSLEEP();
1585 TAILQ_FOREACH(ifp, &V_ifnet, if_link) {
1586 if ((ifp->if_flags & IFF_POINTOPOINT) == 0)
1589 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
1590 if (ifa->ifa_addr->sa_family != addr->sa_family)
1592 if (ifa->ifa_dstaddr != NULL &&
1593 sa_equal(addr, ifa->ifa_dstaddr)) {
1595 IF_ADDR_UNLOCK(ifp);
1599 IF_ADDR_UNLOCK(ifp);
1603 IFNET_RUNLOCK_NOSLEEP();
1608 * Find an interface on a specific network. If many, choice
1609 * is most specific found.
1612 ifa_ifwithnet(struct sockaddr *addr)
1616 struct ifaddr *ifa_maybe = NULL;
1617 u_int af = addr->sa_family;
1618 char *addr_data = addr->sa_data, *cplim;
1621 * AF_LINK addresses can be looked up directly by their index number,
1622 * so do that if we can.
1624 if (af == AF_LINK) {
1625 struct sockaddr_dl *sdl = (struct sockaddr_dl *)addr;
1626 if (sdl->sdl_index && sdl->sdl_index <= V_if_index)
1627 return (ifaddr_byindex(sdl->sdl_index));
1631 * Scan though each interface, looking for ones that have addresses
1632 * in this address family. Maintain a reference on ifa_maybe once
1633 * we find one, as we release the IF_ADDR_LOCK() that kept it stable
1634 * when we move onto the next interface.
1636 IFNET_RLOCK_NOSLEEP();
1637 TAILQ_FOREACH(ifp, &V_ifnet, if_link) {
1639 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
1640 char *cp, *cp2, *cp3;
1642 if (ifa->ifa_addr->sa_family != af)
1644 if (af == AF_INET && ifp->if_flags & IFF_POINTOPOINT) {
1646 * This is a bit broken as it doesn't
1647 * take into account that the remote end may
1648 * be a single node in the network we are
1650 * The trouble is that we don't know the
1651 * netmask for the remote end.
1653 if (ifa->ifa_dstaddr != NULL &&
1654 sa_equal(addr, ifa->ifa_dstaddr)) {
1656 IF_ADDR_UNLOCK(ifp);
1661 * if we have a special address handler,
1662 * then use it instead of the generic one.
1664 if (ifa->ifa_claim_addr) {
1665 if ((*ifa->ifa_claim_addr)(ifa, addr)) {
1667 IF_ADDR_UNLOCK(ifp);
1674 * Scan all the bits in the ifa's address.
1675 * If a bit dissagrees with what we are
1676 * looking for, mask it with the netmask
1677 * to see if it really matters.
1678 * (A byte at a time)
1680 if (ifa->ifa_netmask == 0)
1683 cp2 = ifa->ifa_addr->sa_data;
1684 cp3 = ifa->ifa_netmask->sa_data;
1685 cplim = ifa->ifa_netmask->sa_len
1686 + (char *)ifa->ifa_netmask;
1688 if ((*cp++ ^ *cp2++) & *cp3++)
1689 goto next; /* next address! */
1691 * If the netmask of what we just found
1692 * is more specific than what we had before
1693 * (if we had one) then remember the new one
1694 * before continuing to search
1695 * for an even better one.
1697 if (ifa_maybe == NULL ||
1698 rn_refines((caddr_t)ifa->ifa_netmask,
1699 (caddr_t)ifa_maybe->ifa_netmask)) {
1700 if (ifa_maybe != NULL)
1701 ifa_free(ifa_maybe);
1707 IF_ADDR_UNLOCK(ifp);
1712 IFNET_RUNLOCK_NOSLEEP();
1713 if (ifa_maybe != NULL)
1714 ifa_free(ifa_maybe);
1719 * Find an interface address specific to an interface best matching
1723 ifaof_ifpforaddr(struct sockaddr *addr, struct ifnet *ifp)
1726 char *cp, *cp2, *cp3;
1728 struct ifaddr *ifa_maybe = NULL;
1729 u_int af = addr->sa_family;
1734 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
1735 if (ifa->ifa_addr->sa_family != af)
1737 if (ifa_maybe == NULL)
1739 if (ifa->ifa_netmask == 0) {
1740 if (sa_equal(addr, ifa->ifa_addr) ||
1741 (ifa->ifa_dstaddr &&
1742 sa_equal(addr, ifa->ifa_dstaddr)))
1746 if (ifp->if_flags & IFF_POINTOPOINT) {
1747 if (sa_equal(addr, ifa->ifa_dstaddr))
1751 cp2 = ifa->ifa_addr->sa_data;
1752 cp3 = ifa->ifa_netmask->sa_data;
1753 cplim = ifa->ifa_netmask->sa_len + (char *)ifa->ifa_netmask;
1754 for (; cp3 < cplim; cp3++)
1755 if ((*cp++ ^ *cp2++) & *cp3)
1765 IF_ADDR_UNLOCK(ifp);
1769 #include <net/if_llatbl.h>
1772 * Default action when installing a route with a Link Level gateway.
1773 * Lookup an appropriate real ifa to point to.
1774 * This should be moved to /sys/net/link.c eventually.
1777 link_rtrequest(int cmd, struct rtentry *rt, struct rt_addrinfo *info)
1779 struct ifaddr *ifa, *oifa;
1780 struct sockaddr *dst;
1785 if (cmd != RTM_ADD || ((ifa = rt->rt_ifa) == 0) ||
1786 ((ifp = ifa->ifa_ifp) == 0) || ((dst = rt_key(rt)) == 0))
1788 ifa = ifaof_ifpforaddr(dst, ifp);
1793 if (ifa->ifa_rtrequest && ifa->ifa_rtrequest != link_rtrequest)
1794 ifa->ifa_rtrequest(cmd, rt, info);
1799 * Mark an interface down and notify protocols of
1801 * NOTE: must be called at splnet or eqivalent.
1804 if_unroute(struct ifnet *ifp, int flag, int fam)
1808 KASSERT(flag == IFF_UP, ("if_unroute: flag != IFF_UP"));
1810 ifp->if_flags &= ~flag;
1811 getmicrotime(&ifp->if_lastchange);
1812 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link)
1813 if (fam == PF_UNSPEC || (fam == ifa->ifa_addr->sa_family))
1814 pfctlinput(PRC_IFDOWN, ifa->ifa_addr);
1815 ifp->if_qflush(ifp);
1817 #if defined(INET) || defined(INET6)
1820 carp_carpdev_state(ifp->if_carp);
1827 * Mark an interface up and notify protocols of
1829 * NOTE: must be called at splnet or eqivalent.
1832 if_route(struct ifnet *ifp, int flag, int fam)
1836 KASSERT(flag == IFF_UP, ("if_route: 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_IFUP, ifa->ifa_addr);
1843 #if defined(INET) || defined(INET6)
1846 carp_carpdev_state(ifp->if_carp);
1855 void (*vlan_link_state_p)(struct ifnet *, int); /* XXX: private from if_vlan */
1856 void (*vlan_trunk_cap_p)(struct ifnet *); /* XXX: private from if_vlan */
1859 * Handle a change in the interface link state. To avoid LORs
1860 * between driver lock and upper layer locks, as well as possible
1861 * recursions, we post event to taskqueue, and all job
1862 * is done in static do_link_state_change().
1865 if_link_state_change(struct ifnet *ifp, int link_state)
1867 /* Return if state hasn't changed. */
1868 if (ifp->if_link_state == link_state)
1871 ifp->if_link_state = link_state;
1873 taskqueue_enqueue(taskqueue_swi, &ifp->if_linktask);
1877 do_link_state_change(void *arg, int pending)
1879 struct ifnet *ifp = (struct ifnet *)arg;
1880 int link_state = ifp->if_link_state;
1882 CURVNET_SET(ifp->if_vnet);
1884 /* Notify that the link state has changed. */
1886 if (link_state == LINK_STATE_UP)
1888 else if (link_state == LINK_STATE_DOWN)
1889 link = NOTE_LINKDOWN;
1891 link = NOTE_LINKINV;
1892 if (ifp->if_vlantrunk != NULL)
1893 (*vlan_link_state_p)(ifp, link);
1895 if ((ifp->if_type == IFT_ETHER || ifp->if_type == IFT_L2VLAN) &&
1896 IFP2AC(ifp)->ac_netgraph != NULL)
1897 (*ng_ether_link_state_p)(ifp, link_state);
1898 #if defined(INET) || defined(INET6)
1901 carp_carpdev_state(ifp->if_carp);
1904 if (ifp->if_bridge) {
1905 KASSERT(bstp_linkstate_p != NULL,("if_bridge bstp not loaded!"));
1906 (*bstp_linkstate_p)(ifp, link_state);
1909 KASSERT(lagg_linkstate_p != NULL,("if_lagg not loaded!"));
1910 (*lagg_linkstate_p)(ifp, link_state);
1913 if (IS_DEFAULT_VNET(curvnet))
1914 devctl_notify("IFNET", ifp->if_xname,
1915 (link_state == LINK_STATE_UP) ? "LINK_UP" : "LINK_DOWN",
1918 if_printf(ifp, "%d link states coalesced\n", pending);
1919 if (log_link_state_change)
1920 log(LOG_NOTICE, "%s: link state changed to %s\n", ifp->if_xname,
1921 (link_state == LINK_STATE_UP) ? "UP" : "DOWN" );
1926 * Mark an interface down and notify protocols of
1928 * NOTE: must be called at splnet or eqivalent.
1931 if_down(struct ifnet *ifp)
1934 if_unroute(ifp, IFF_UP, AF_UNSPEC);
1938 * Mark an interface up and notify protocols of
1940 * NOTE: must be called at splnet or eqivalent.
1943 if_up(struct ifnet *ifp)
1946 if_route(ifp, IFF_UP, AF_UNSPEC);
1950 * Flush an interface queue.
1953 if_qflush(struct ifnet *ifp)
1961 if (ALTQ_IS_ENABLED(ifq))
1965 while ((m = n) != 0) {
1976 * Handle interface watchdog timer routines. Called
1977 * from softclock, we decrement timers (if set) and
1978 * call the appropriate interface routine on expiration.
1980 * XXXRW: Note that because timeouts run with Giant, if_watchdog() is called
1984 if_slowtimo(void *arg)
1986 VNET_ITERATOR_DECL(vnet_iter);
1990 VNET_LIST_RLOCK_NOSLEEP();
1991 IFNET_RLOCK_NOSLEEP();
1992 VNET_FOREACH(vnet_iter) {
1993 CURVNET_SET(vnet_iter);
1994 TAILQ_FOREACH(ifp, &V_ifnet, if_link) {
1995 if (ifp->if_timer == 0 || --ifp->if_timer)
1997 if (ifp->if_watchdog)
1998 (*ifp->if_watchdog)(ifp);
2002 IFNET_RUNLOCK_NOSLEEP();
2003 VNET_LIST_RUNLOCK_NOSLEEP();
2005 timeout(if_slowtimo, (void *)0, hz / IFNET_SLOWHZ);
2009 * Map interface name to interface structure pointer, with or without
2010 * returning a reference.
2013 ifunit_ref(const char *name)
2017 IFNET_RLOCK_NOSLEEP();
2018 TAILQ_FOREACH(ifp, &V_ifnet, if_link) {
2019 if (strncmp(name, ifp->if_xname, IFNAMSIZ) == 0 &&
2020 !(ifp->if_flags & IFF_DYING))
2025 IFNET_RUNLOCK_NOSLEEP();
2030 ifunit(const char *name)
2034 IFNET_RLOCK_NOSLEEP();
2035 TAILQ_FOREACH(ifp, &V_ifnet, if_link) {
2036 if (strncmp(name, ifp->if_xname, IFNAMSIZ) == 0)
2039 IFNET_RUNLOCK_NOSLEEP();
2044 * Hardware specific interface ioctls.
2047 ifhwioctl(u_long cmd, struct ifnet *ifp, caddr_t data, struct thread *td)
2052 int new_flags, temp_flags;
2053 size_t namelen, onamelen;
2054 char new_name[IFNAMSIZ];
2056 struct sockaddr_dl *sdl;
2058 ifr = (struct ifreq *)data;
2061 ifr->ifr_index = ifp->if_index;
2065 temp_flags = ifp->if_flags | ifp->if_drv_flags;
2066 ifr->ifr_flags = temp_flags & 0xffff;
2067 ifr->ifr_flagshigh = temp_flags >> 16;
2071 ifr->ifr_reqcap = ifp->if_capabilities;
2072 ifr->ifr_curcap = ifp->if_capenable;
2077 error = mac_ifnet_ioctl_get(td->td_ucred, ifr, ifp);
2082 ifr->ifr_metric = ifp->if_metric;
2086 ifr->ifr_mtu = ifp->if_mtu;
2090 ifr->ifr_phys = ifp->if_physical;
2094 error = priv_check(td, PRIV_NET_SETIFFLAGS);
2098 * Currently, no driver owned flags pass the IFF_CANTCHANGE
2099 * check, so we don't need special handling here yet.
2101 new_flags = (ifr->ifr_flags & 0xffff) |
2102 (ifr->ifr_flagshigh << 16);
2103 if (ifp->if_flags & IFF_SMART) {
2104 /* Smart drivers twiddle their own routes */
2105 } else if (ifp->if_flags & IFF_UP &&
2106 (new_flags & IFF_UP) == 0) {
2110 } else if (new_flags & IFF_UP &&
2111 (ifp->if_flags & IFF_UP) == 0) {
2116 /* See if permanently promiscuous mode bit is about to flip */
2117 if ((ifp->if_flags ^ new_flags) & IFF_PPROMISC) {
2118 if (new_flags & IFF_PPROMISC)
2119 ifp->if_flags |= IFF_PROMISC;
2120 else if (ifp->if_pcount == 0)
2121 ifp->if_flags &= ~IFF_PROMISC;
2122 log(LOG_INFO, "%s: permanently promiscuous mode %s\n",
2124 (new_flags & IFF_PPROMISC) ? "enabled" : "disabled");
2126 ifp->if_flags = (ifp->if_flags & IFF_CANTCHANGE) |
2127 (new_flags &~ IFF_CANTCHANGE);
2128 if (ifp->if_ioctl) {
2129 (void) (*ifp->if_ioctl)(ifp, cmd, data);
2131 getmicrotime(&ifp->if_lastchange);
2135 error = priv_check(td, PRIV_NET_SETIFCAP);
2138 if (ifp->if_ioctl == NULL)
2139 return (EOPNOTSUPP);
2140 if (ifr->ifr_reqcap & ~ifp->if_capabilities)
2142 error = (*ifp->if_ioctl)(ifp, cmd, data);
2144 getmicrotime(&ifp->if_lastchange);
2149 error = mac_ifnet_ioctl_set(td->td_ucred, ifr, ifp);
2154 error = priv_check(td, PRIV_NET_SETIFNAME);
2157 error = copyinstr(ifr->ifr_data, new_name, IFNAMSIZ, NULL);
2160 if (new_name[0] == '\0')
2162 if (ifunit(new_name) != NULL)
2165 /* Announce the departure of the interface. */
2166 rt_ifannouncemsg(ifp, IFAN_DEPARTURE);
2167 EVENTHANDLER_INVOKE(ifnet_departure_event, ifp);
2169 log(LOG_INFO, "%s: changing name to '%s'\n",
2170 ifp->if_xname, new_name);
2172 strlcpy(ifp->if_xname, new_name, sizeof(ifp->if_xname));
2175 sdl = (struct sockaddr_dl *)ifa->ifa_addr;
2176 namelen = strlen(new_name);
2177 onamelen = sdl->sdl_nlen;
2179 * Move the address if needed. This is safe because we
2180 * allocate space for a name of length IFNAMSIZ when we
2181 * create this in if_attach().
2183 if (namelen != onamelen) {
2184 bcopy(sdl->sdl_data + onamelen,
2185 sdl->sdl_data + namelen, sdl->sdl_alen);
2187 bcopy(new_name, sdl->sdl_data, namelen);
2188 sdl->sdl_nlen = namelen;
2189 sdl = (struct sockaddr_dl *)ifa->ifa_netmask;
2190 bzero(sdl->sdl_data, onamelen);
2191 while (namelen != 0)
2192 sdl->sdl_data[--namelen] = 0xff;
2195 EVENTHANDLER_INVOKE(ifnet_arrival_event, ifp);
2196 /* Announce the return of the interface. */
2197 rt_ifannouncemsg(ifp, IFAN_ARRIVAL);
2202 error = priv_check(td, PRIV_NET_SETIFVNET);
2205 error = if_vmove_loan(td, ifp, ifr->ifr_name, ifr->ifr_jid);
2210 error = priv_check(td, PRIV_NET_SETIFMETRIC);
2213 ifp->if_metric = ifr->ifr_metric;
2214 getmicrotime(&ifp->if_lastchange);
2218 error = priv_check(td, PRIV_NET_SETIFPHYS);
2221 if (ifp->if_ioctl == NULL)
2222 return (EOPNOTSUPP);
2223 error = (*ifp->if_ioctl)(ifp, cmd, data);
2225 getmicrotime(&ifp->if_lastchange);
2230 u_long oldmtu = ifp->if_mtu;
2232 error = priv_check(td, PRIV_NET_SETIFMTU);
2235 if (ifr->ifr_mtu < IF_MINMTU || ifr->ifr_mtu > IF_MAXMTU)
2237 if (ifp->if_ioctl == NULL)
2238 return (EOPNOTSUPP);
2239 error = (*ifp->if_ioctl)(ifp, cmd, data);
2241 getmicrotime(&ifp->if_lastchange);
2245 * If the link MTU changed, do network layer specific procedure.
2247 if (ifp->if_mtu != oldmtu) {
2257 if (cmd == SIOCADDMULTI)
2258 error = priv_check(td, PRIV_NET_ADDMULTI);
2260 error = priv_check(td, PRIV_NET_DELMULTI);
2264 /* Don't allow group membership on non-multicast interfaces. */
2265 if ((ifp->if_flags & IFF_MULTICAST) == 0)
2266 return (EOPNOTSUPP);
2268 /* Don't let users screw up protocols' entries. */
2269 if (ifr->ifr_addr.sa_family != AF_LINK)
2272 if (cmd == SIOCADDMULTI) {
2273 struct ifmultiaddr *ifma;
2276 * Userland is only permitted to join groups once
2277 * via the if_addmulti() KPI, because it cannot hold
2278 * struct ifmultiaddr * between calls. It may also
2279 * lose a race while we check if the membership
2283 ifma = if_findmulti(ifp, &ifr->ifr_addr);
2284 IF_ADDR_UNLOCK(ifp);
2288 error = if_addmulti(ifp, &ifr->ifr_addr, &ifma);
2290 error = if_delmulti(ifp, &ifr->ifr_addr);
2293 getmicrotime(&ifp->if_lastchange);
2296 case SIOCSIFPHYADDR:
2297 case SIOCDIFPHYADDR:
2299 case SIOCSIFPHYADDR_IN6:
2301 case SIOCSLIFPHYADDR:
2303 case SIOCSIFGENERIC:
2304 error = priv_check(td, PRIV_NET_HWIOCTL);
2307 if (ifp->if_ioctl == NULL)
2308 return (EOPNOTSUPP);
2309 error = (*ifp->if_ioctl)(ifp, cmd, data);
2311 getmicrotime(&ifp->if_lastchange);
2315 ifs = (struct ifstat *)data;
2316 ifs->ascii[0] = '\0';
2318 case SIOCGIFPSRCADDR:
2319 case SIOCGIFPDSTADDR:
2320 case SIOCGLIFPHYADDR:
2322 case SIOCGIFGENERIC:
2323 if (ifp->if_ioctl == NULL)
2324 return (EOPNOTSUPP);
2325 error = (*ifp->if_ioctl)(ifp, cmd, data);
2329 error = priv_check(td, PRIV_NET_SETLLADDR);
2332 error = if_setlladdr(ifp,
2333 ifr->ifr_addr.sa_data, ifr->ifr_addr.sa_len);
2338 struct ifgroupreq *ifgr = (struct ifgroupreq *)ifr;
2340 error = priv_check(td, PRIV_NET_ADDIFGROUP);
2343 if ((error = if_addgroup(ifp, ifgr->ifgr_group)))
2349 if ((error = if_getgroup((struct ifgroupreq *)ifr, ifp)))
2355 struct ifgroupreq *ifgr = (struct ifgroupreq *)ifr;
2357 error = priv_check(td, PRIV_NET_DELIFGROUP);
2360 if ((error = if_delgroup(ifp, ifgr->ifgr_group)))
2376 ifioctl(struct socket *so, u_long cmd, caddr_t data, struct thread *td)
2389 return (ifconf(cmd, data));
2391 ifr = (struct ifreq *)data;
2396 error = priv_check(td, PRIV_NET_SETIFVNET);
2399 return (if_vmove_reclaim(td, ifr->ifr_name, ifr->ifr_jid));
2403 error = priv_check(td, PRIV_NET_IFCREATE);
2406 return (if_clone_create(ifr->ifr_name, sizeof(ifr->ifr_name),
2407 cmd == SIOCIFCREATE2 ? ifr->ifr_data : NULL));
2409 error = priv_check(td, PRIV_NET_IFDESTROY);
2412 return if_clone_destroy(ifr->ifr_name);
2414 case SIOCIFGCLONERS:
2415 return (if_clone_list((struct if_clonereq *)data));
2417 return (if_getgroupmembers((struct ifgroupreq *)data));
2420 ifp = ifunit_ref(ifr->ifr_name);
2424 error = ifhwioctl(cmd, ifp, data, td);
2425 if (error != ENOIOCTL) {
2430 oif_flags = ifp->if_flags;
2431 if (so->so_proto == NULL) {
2433 return (EOPNOTSUPP);
2436 error = ((*so->so_proto->pr_usrreqs->pru_control)(so, cmd,
2439 if (error == EOPNOTSUPP && ifp != NULL && ifp->if_ioctl != NULL)
2440 error = (*ifp->if_ioctl)(ifp, cmd, data);
2447 case SIOCSIFDSTADDR:
2449 case SIOCSIFBRDADDR:
2450 case SIOCSIFNETMASK:
2451 #if BYTE_ORDER != BIG_ENDIAN
2452 if (ifr->ifr_addr.sa_family == 0 &&
2453 ifr->ifr_addr.sa_len < 16) {
2454 ifr->ifr_addr.sa_family = ifr->ifr_addr.sa_len;
2455 ifr->ifr_addr.sa_len = 16;
2458 if (ifr->ifr_addr.sa_len == 0)
2459 ifr->ifr_addr.sa_len = 16;
2467 case OSIOCGIFDSTADDR:
2468 cmd = SIOCGIFDSTADDR;
2471 case OSIOCGIFBRDADDR:
2472 cmd = SIOCGIFBRDADDR;
2475 case OSIOCGIFNETMASK:
2476 cmd = SIOCGIFNETMASK;
2478 error = ((*so->so_proto->pr_usrreqs->pru_control)(so,
2482 if (error == EOPNOTSUPP && ifp != NULL &&
2483 ifp->if_ioctl != NULL)
2484 error = (*ifp->if_ioctl)(ifp, cmd, data);
2488 case OSIOCGIFDSTADDR:
2489 case OSIOCGIFBRDADDR:
2490 case OSIOCGIFNETMASK:
2491 *(u_short *)&ifr->ifr_addr = ifr->ifr_addr.sa_family;
2495 #endif /* COMPAT_43 */
2497 if ((oif_flags ^ ifp->if_flags) & IFF_UP) {
2499 if (ifp->if_flags & IFF_UP) {
2511 * The code common to handling reference counted flags,
2512 * e.g., in ifpromisc() and if_allmulti().
2513 * The "pflag" argument can specify a permanent mode flag to check,
2514 * such as IFF_PPROMISC for promiscuous mode; should be 0 if none.
2516 * Only to be used on stack-owned flags, not driver-owned flags.
2519 if_setflag(struct ifnet *ifp, int flag, int pflag, int *refcount, int onswitch)
2523 int oldflags, oldcount;
2525 /* Sanity checks to catch programming errors */
2526 KASSERT((flag & (IFF_DRV_OACTIVE|IFF_DRV_RUNNING)) == 0,
2527 ("%s: setting driver-owned flag %d", __func__, flag));
2530 KASSERT(*refcount >= 0,
2531 ("%s: increment negative refcount %d for flag %d",
2532 __func__, *refcount, flag));
2534 KASSERT(*refcount > 0,
2535 ("%s: decrement non-positive refcount %d for flag %d",
2536 __func__, *refcount, flag));
2538 /* In case this mode is permanent, just touch refcount */
2539 if (ifp->if_flags & pflag) {
2540 *refcount += onswitch ? 1 : -1;
2544 /* Save ifnet parameters for if_ioctl() may fail */
2545 oldcount = *refcount;
2546 oldflags = ifp->if_flags;
2549 * See if we aren't the only and touching refcount is enough.
2550 * Actually toggle interface flag if we are the first or last.
2555 ifp->if_flags |= flag;
2559 ifp->if_flags &= ~flag;
2562 /* Call down the driver since we've changed interface flags */
2563 if (ifp->if_ioctl == NULL) {
2567 ifr.ifr_flags = ifp->if_flags & 0xffff;
2568 ifr.ifr_flagshigh = ifp->if_flags >> 16;
2569 error = (*ifp->if_ioctl)(ifp, SIOCSIFFLAGS, (caddr_t)&ifr);
2572 /* Notify userland that interface flags have changed */
2577 /* Recover after driver error */
2578 *refcount = oldcount;
2579 ifp->if_flags = oldflags;
2584 * Set/clear promiscuous mode on interface ifp based on the truth value
2585 * of pswitch. The calls are reference counted so that only the first
2586 * "on" request actually has an effect, as does the final "off" request.
2587 * Results are undefined if the "off" and "on" requests are not matched.
2590 ifpromisc(struct ifnet *ifp, int pswitch)
2593 int oldflags = ifp->if_flags;
2595 error = if_setflag(ifp, IFF_PROMISC, IFF_PPROMISC,
2596 &ifp->if_pcount, pswitch);
2597 /* If promiscuous mode status has changed, log a message */
2598 if (error == 0 && ((ifp->if_flags ^ oldflags) & IFF_PROMISC))
2599 log(LOG_INFO, "%s: promiscuous mode %s\n",
2601 (ifp->if_flags & IFF_PROMISC) ? "enabled" : "disabled");
2606 * Return interface configuration
2607 * of system. List may be used
2608 * in later ioctl's (above) to get
2609 * other information.
2613 ifconf(u_long cmd, caddr_t data)
2615 struct ifconf *ifc = (struct ifconf *)data;
2617 struct ifconf32 *ifc32 = (struct ifconf32 *)data;
2618 struct ifconf ifc_swab;
2624 int error, full = 0, valid_len, max_len;
2627 if (cmd == SIOCGIFCONF32) {
2628 ifc_swab.ifc_len = ifc32->ifc_len;
2629 ifc_swab.ifc_buf = (caddr_t)(uintptr_t)ifc32->ifc_buf;
2633 /* Limit initial buffer size to MAXPHYS to avoid DoS from userspace. */
2634 max_len = MAXPHYS - 1;
2636 /* Prevent hostile input from being able to crash the system */
2637 if (ifc->ifc_len <= 0)
2641 if (ifc->ifc_len <= max_len) {
2642 max_len = ifc->ifc_len;
2645 sb = sbuf_new(NULL, NULL, max_len + 1, SBUF_FIXEDLEN);
2650 TAILQ_FOREACH(ifp, &V_ifnet, if_link) {
2654 * Zero the ifr_name buffer to make sure we don't
2655 * disclose the contents of the stack.
2657 memset(ifr.ifr_name, 0, sizeof(ifr.ifr_name));
2659 if (strlcpy(ifr.ifr_name, ifp->if_xname, sizeof(ifr.ifr_name))
2660 >= sizeof(ifr.ifr_name)) {
2663 return (ENAMETOOLONG);
2668 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
2669 struct sockaddr *sa = ifa->ifa_addr;
2671 if (prison_if(curthread->td_ucred, sa) != 0)
2675 if (cmd == OSIOCGIFCONF) {
2676 struct osockaddr *osa =
2677 (struct osockaddr *)&ifr.ifr_addr;
2679 osa->sa_family = sa->sa_family;
2680 sbuf_bcat(sb, &ifr, sizeof(ifr));
2681 max_len += sizeof(ifr);
2684 if (sa->sa_len <= sizeof(*sa)) {
2686 sbuf_bcat(sb, &ifr, sizeof(ifr));
2687 max_len += sizeof(ifr);
2690 offsetof(struct ifreq, ifr_addr));
2691 max_len += offsetof(struct ifreq, ifr_addr);
2692 sbuf_bcat(sb, sa, sa->sa_len);
2693 max_len += sa->sa_len;
2696 if (!sbuf_overflowed(sb))
2697 valid_len = sbuf_len(sb);
2699 IF_ADDR_UNLOCK(ifp);
2701 bzero((caddr_t)&ifr.ifr_addr, sizeof(ifr.ifr_addr));
2702 sbuf_bcat(sb, &ifr, sizeof(ifr));
2703 max_len += sizeof(ifr);
2705 if (!sbuf_overflowed(sb))
2706 valid_len = sbuf_len(sb);
2712 * If we didn't allocate enough space (uncommon), try again. If
2713 * we have already allocated as much space as we are allowed,
2714 * return what we've got.
2716 if (valid_len != max_len && !full) {
2721 ifc->ifc_len = valid_len;
2723 if (cmd == SIOCGIFCONF32)
2724 ifc32->ifc_len = valid_len;
2727 error = copyout(sbuf_data(sb), ifc->ifc_req, ifc->ifc_len);
2733 * Just like ifpromisc(), but for all-multicast-reception mode.
2736 if_allmulti(struct ifnet *ifp, int onswitch)
2739 return (if_setflag(ifp, IFF_ALLMULTI, 0, &ifp->if_amcount, onswitch));
2742 struct ifmultiaddr *
2743 if_findmulti(struct ifnet *ifp, struct sockaddr *sa)
2745 struct ifmultiaddr *ifma;
2747 IF_ADDR_LOCK_ASSERT(ifp);
2749 TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
2750 if (sa->sa_family == AF_LINK) {
2751 if (sa_dl_equal(ifma->ifma_addr, sa))
2754 if (sa_equal(ifma->ifma_addr, sa))
2763 * Allocate a new ifmultiaddr and initialize based on passed arguments. We
2764 * make copies of passed sockaddrs. The ifmultiaddr will not be added to
2765 * the ifnet multicast address list here, so the caller must do that and
2766 * other setup work (such as notifying the device driver). The reference
2767 * count is initialized to 1.
2769 static struct ifmultiaddr *
2770 if_allocmulti(struct ifnet *ifp, struct sockaddr *sa, struct sockaddr *llsa,
2773 struct ifmultiaddr *ifma;
2774 struct sockaddr *dupsa;
2776 ifma = malloc(sizeof *ifma, M_IFMADDR, mflags |
2781 dupsa = malloc(sa->sa_len, M_IFMADDR, mflags);
2782 if (dupsa == NULL) {
2783 free(ifma, M_IFMADDR);
2786 bcopy(sa, dupsa, sa->sa_len);
2787 ifma->ifma_addr = dupsa;
2789 ifma->ifma_ifp = ifp;
2790 ifma->ifma_refcount = 1;
2791 ifma->ifma_protospec = NULL;
2794 ifma->ifma_lladdr = NULL;
2798 dupsa = malloc(llsa->sa_len, M_IFMADDR, mflags);
2799 if (dupsa == NULL) {
2800 free(ifma->ifma_addr, M_IFMADDR);
2801 free(ifma, M_IFMADDR);
2804 bcopy(llsa, dupsa, llsa->sa_len);
2805 ifma->ifma_lladdr = dupsa;
2811 * if_freemulti: free ifmultiaddr structure and possibly attached related
2812 * addresses. The caller is responsible for implementing reference
2813 * counting, notifying the driver, handling routing messages, and releasing
2814 * any dependent link layer state.
2817 if_freemulti(struct ifmultiaddr *ifma)
2820 KASSERT(ifma->ifma_refcount == 0, ("if_freemulti: refcount %d",
2821 ifma->ifma_refcount));
2822 KASSERT(ifma->ifma_protospec == NULL,
2823 ("if_freemulti: protospec not NULL"));
2825 if (ifma->ifma_lladdr != NULL)
2826 free(ifma->ifma_lladdr, M_IFMADDR);
2827 free(ifma->ifma_addr, M_IFMADDR);
2828 free(ifma, M_IFMADDR);
2832 * Register an additional multicast address with a network interface.
2834 * - If the address is already present, bump the reference count on the
2835 * address and return.
2836 * - If the address is not link-layer, look up a link layer address.
2837 * - Allocate address structures for one or both addresses, and attach to the
2838 * multicast address list on the interface. If automatically adding a link
2839 * layer address, the protocol address will own a reference to the link
2840 * layer address, to be freed when it is freed.
2841 * - Notify the network device driver of an addition to the multicast address
2844 * 'sa' points to caller-owned memory with the desired multicast address.
2846 * 'retifma' will be used to return a pointer to the resulting multicast
2847 * address reference, if desired.
2850 if_addmulti(struct ifnet *ifp, struct sockaddr *sa,
2851 struct ifmultiaddr **retifma)
2853 struct ifmultiaddr *ifma, *ll_ifma;
2854 struct sockaddr *llsa;
2858 * If the address is already present, return a new reference to it;
2859 * otherwise, allocate storage and set up a new address.
2862 ifma = if_findmulti(ifp, sa);
2864 ifma->ifma_refcount++;
2865 if (retifma != NULL)
2867 IF_ADDR_UNLOCK(ifp);
2872 * The address isn't already present; resolve the protocol address
2873 * into a link layer address, and then look that up, bump its
2874 * refcount or allocate an ifma for that also. If 'llsa' was
2875 * returned, we will need to free it later.
2879 if (ifp->if_resolvemulti != NULL) {
2880 error = ifp->if_resolvemulti(ifp, &llsa, sa);
2886 * Allocate the new address. Don't hook it up yet, as we may also
2887 * need to allocate a link layer multicast address.
2889 ifma = if_allocmulti(ifp, sa, llsa, M_NOWAIT);
2896 * If a link layer address is found, we'll need to see if it's
2897 * already present in the address list, or allocate is as well.
2898 * When this block finishes, the link layer address will be on the
2902 ll_ifma = if_findmulti(ifp, llsa);
2903 if (ll_ifma == NULL) {
2904 ll_ifma = if_allocmulti(ifp, llsa, NULL, M_NOWAIT);
2905 if (ll_ifma == NULL) {
2906 --ifma->ifma_refcount;
2911 TAILQ_INSERT_HEAD(&ifp->if_multiaddrs, ll_ifma,
2914 ll_ifma->ifma_refcount++;
2915 ifma->ifma_llifma = ll_ifma;
2919 * We now have a new multicast address, ifma, and possibly a new or
2920 * referenced link layer address. Add the primary address to the
2921 * ifnet address list.
2923 TAILQ_INSERT_HEAD(&ifp->if_multiaddrs, ifma, ifma_link);
2925 if (retifma != NULL)
2929 * Must generate the message while holding the lock so that 'ifma'
2930 * pointer is still valid.
2932 rt_newmaddrmsg(RTM_NEWMADDR, ifma);
2933 IF_ADDR_UNLOCK(ifp);
2936 * We are certain we have added something, so call down to the
2937 * interface to let them know about it.
2939 if (ifp->if_ioctl != NULL) {
2940 (void) (*ifp->if_ioctl)(ifp, SIOCADDMULTI, 0);
2944 free(llsa, M_IFMADDR);
2950 free(llsa, M_IFMADDR);
2953 IF_ADDR_UNLOCK(ifp);
2958 * Delete a multicast group membership by network-layer group address.
2960 * Returns ENOENT if the entry could not be found. If ifp no longer
2961 * exists, results are undefined. This entry point should only be used
2962 * from subsystems which do appropriate locking to hold ifp for the
2963 * duration of the call.
2964 * Network-layer protocol domains must use if_delmulti_ifma().
2967 if_delmulti(struct ifnet *ifp, struct sockaddr *sa)
2969 struct ifmultiaddr *ifma;
2974 IFNET_RLOCK_NOSLEEP();
2975 TAILQ_FOREACH(oifp, &V_ifnet, if_link)
2980 IFNET_RUNLOCK_NOSLEEP();
2982 KASSERT(ifp != NULL, ("%s: ifnet went away", __func__));
2989 ifma = if_findmulti(ifp, sa);
2991 lastref = if_delmulti_locked(ifp, ifma, 0);
2992 IF_ADDR_UNLOCK(ifp);
2997 if (lastref && ifp->if_ioctl != NULL) {
2998 (void)(*ifp->if_ioctl)(ifp, SIOCDELMULTI, 0);
3005 * Delete a multicast group membership by group membership pointer.
3006 * Network-layer protocol domains must use this routine.
3008 * It is safe to call this routine if the ifp disappeared.
3011 if_delmulti_ifma(struct ifmultiaddr *ifma)
3016 ifp = ifma->ifma_ifp;
3019 printf("%s: ifma_ifp seems to be detached\n", __func__);
3023 IFNET_RLOCK_NOSLEEP();
3024 TAILQ_FOREACH(oifp, &V_ifnet, if_link)
3028 printf("%s: ifnet %p disappeared\n", __func__, ifp);
3031 IFNET_RUNLOCK_NOSLEEP();
3035 * If and only if the ifnet instance exists: Acquire the address lock.
3040 lastref = if_delmulti_locked(ifp, ifma, 0);
3044 * If and only if the ifnet instance exists:
3045 * Release the address lock.
3046 * If the group was left: update the hardware hash filter.
3048 IF_ADDR_UNLOCK(ifp);
3049 if (lastref && ifp->if_ioctl != NULL) {
3050 (void)(*ifp->if_ioctl)(ifp, SIOCDELMULTI, 0);
3056 * Perform deletion of network-layer and/or link-layer multicast address.
3058 * Return 0 if the reference count was decremented.
3059 * Return 1 if the final reference was released, indicating that the
3060 * hardware hash filter should be reprogrammed.
3063 if_delmulti_locked(struct ifnet *ifp, struct ifmultiaddr *ifma, int detaching)
3065 struct ifmultiaddr *ll_ifma;
3067 if (ifp != NULL && ifma->ifma_ifp != NULL) {
3068 KASSERT(ifma->ifma_ifp == ifp,
3069 ("%s: inconsistent ifp %p", __func__, ifp));
3070 IF_ADDR_LOCK_ASSERT(ifp);
3073 ifp = ifma->ifma_ifp;
3076 * If the ifnet is detaching, null out references to ifnet,
3077 * so that upper protocol layers will notice, and not attempt
3078 * to obtain locks for an ifnet which no longer exists. The
3079 * routing socket announcement must happen before the ifnet
3080 * instance is detached from the system.
3084 printf("%s: detaching ifnet instance %p\n", __func__, ifp);
3087 * ifp may already be nulled out if we are being reentered
3088 * to delete the ll_ifma.
3091 rt_newmaddrmsg(RTM_DELMADDR, ifma);
3092 ifma->ifma_ifp = NULL;
3096 if (--ifma->ifma_refcount > 0)
3100 * If this ifma is a network-layer ifma, a link-layer ifma may
3101 * have been associated with it. Release it first if so.
3103 ll_ifma = ifma->ifma_llifma;
3104 if (ll_ifma != NULL) {
3105 KASSERT(ifma->ifma_lladdr != NULL,
3106 ("%s: llifma w/o lladdr", __func__));
3108 ll_ifma->ifma_ifp = NULL; /* XXX */
3109 if (--ll_ifma->ifma_refcount == 0) {
3111 TAILQ_REMOVE(&ifp->if_multiaddrs, ll_ifma,
3114 if_freemulti(ll_ifma);
3119 TAILQ_REMOVE(&ifp->if_multiaddrs, ifma, ifma_link);
3124 * The last reference to this instance of struct ifmultiaddr
3125 * was released; the hardware should be notified of this change.
3131 * Set the link layer address on an interface.
3133 * At this time we only support certain types of interfaces,
3134 * and we don't allow the length of the address to change.
3137 if_setlladdr(struct ifnet *ifp, const u_char *lladdr, int len)
3139 struct sockaddr_dl *sdl;
3146 IF_ADDR_UNLOCK(ifp);
3150 IF_ADDR_UNLOCK(ifp);
3151 sdl = (struct sockaddr_dl *)ifa->ifa_addr;
3156 if (len != sdl->sdl_alen) { /* don't allow length to change */
3160 switch (ifp->if_type) {
3168 case IFT_IEEE8023ADLAG:
3170 bcopy(lladdr, LLADDR(sdl), len);
3179 * If the interface is already up, we need
3180 * to re-init it in order to reprogram its
3183 if ((ifp->if_flags & IFF_UP) != 0) {
3184 if (ifp->if_ioctl) {
3185 ifp->if_flags &= ~IFF_UP;
3186 ifr.ifr_flags = ifp->if_flags & 0xffff;
3187 ifr.ifr_flagshigh = ifp->if_flags >> 16;
3188 (*ifp->if_ioctl)(ifp, SIOCSIFFLAGS, (caddr_t)&ifr);
3189 ifp->if_flags |= IFF_UP;
3190 ifr.ifr_flags = ifp->if_flags & 0xffff;
3191 ifr.ifr_flagshigh = ifp->if_flags >> 16;
3192 (*ifp->if_ioctl)(ifp, SIOCSIFFLAGS, (caddr_t)&ifr);
3196 * Also send gratuitous ARPs to notify other nodes about
3197 * the address change.
3199 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
3200 if (ifa->ifa_addr->sa_family == AF_INET)
3201 arp_ifinit(ifp, ifa);
3209 * The name argument must be a pointer to storage which will last as
3210 * long as the interface does. For physical devices, the result of
3211 * device_get_name(dev) is a good choice and for pseudo-devices a
3212 * static string works well.
3215 if_initname(struct ifnet *ifp, const char *name, int unit)
3217 ifp->if_dname = name;
3218 ifp->if_dunit = unit;
3219 if (unit != IF_DUNIT_NONE)
3220 snprintf(ifp->if_xname, IFNAMSIZ, "%s%d", name, unit);
3222 strlcpy(ifp->if_xname, name, IFNAMSIZ);
3226 if_printf(struct ifnet *ifp, const char * fmt, ...)
3231 retval = printf("%s: ", ifp->if_xname);
3233 retval += vprintf(fmt, ap);
3239 if_start(struct ifnet *ifp)
3242 (*(ifp)->if_start)(ifp);
3246 * Backwards compatibility interface for drivers
3247 * that have not implemented it
3250 if_transmit(struct ifnet *ifp, struct mbuf *m)
3254 IFQ_HANDOFF(ifp, m, error);
3259 if_handoff(struct ifqueue *ifq, struct mbuf *m, struct ifnet *ifp, int adjust)
3264 if (_IF_QFULL(ifq)) {
3271 ifp->if_obytes += m->m_pkthdr.len + adjust;
3272 if (m->m_flags & (M_BCAST|M_MCAST))
3274 active = ifp->if_drv_flags & IFF_DRV_OACTIVE;
3276 _IF_ENQUEUE(ifq, m);
3278 if (ifp != NULL && !active)
3279 (*(ifp)->if_start)(ifp);
3284 if_register_com_alloc(u_char type,
3285 if_com_alloc_t *a, if_com_free_t *f)
3288 KASSERT(if_com_alloc[type] == NULL,
3289 ("if_register_com_alloc: %d already registered", type));
3290 KASSERT(if_com_free[type] == NULL,
3291 ("if_register_com_alloc: %d free already registered", type));
3293 if_com_alloc[type] = a;
3294 if_com_free[type] = f;
3298 if_deregister_com_alloc(u_char type)
3301 KASSERT(if_com_alloc[type] != NULL,
3302 ("if_deregister_com_alloc: %d not registered", type));
3303 KASSERT(if_com_free[type] != NULL,
3304 ("if_deregister_com_alloc: %d free not registered", type));
3305 if_com_alloc[type] = NULL;
3306 if_com_free[type] = NULL;