2 * Copyright (c) 1980, 1986, 1993
3 * The Regents of the University of California. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 4. Neither the name of the University nor the names of its contributors
14 * may be used to endorse or promote products derived from this software
15 * without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * @(#)if.c 8.5 (Berkeley) 1/9/95
33 #include "opt_compat.h"
34 #include "opt_inet6.h"
37 #include <sys/param.h>
38 #include <sys/types.h>
40 #include <sys/malloc.h>
44 #include <sys/systm.h>
47 #include <sys/socket.h>
48 #include <sys/socketvar.h>
49 #include <sys/protosw.h>
50 #include <sys/kernel.h>
52 #include <sys/refcount.h>
53 #include <sys/module.h>
54 #include <sys/rwlock.h>
55 #include <sys/sockio.h>
56 #include <sys/syslog.h>
57 #include <sys/sysctl.h>
58 #include <sys/taskqueue.h>
59 #include <sys/domain.h>
63 #include <machine/stdarg.h>
67 #include <net/ethernet.h>
69 #include <net/if_arp.h>
70 #include <net/if_clone.h>
71 #include <net/if_dl.h>
72 #include <net/if_types.h>
73 #include <net/if_var.h>
74 #include <net/if_media.h>
75 #include <net/if_vlan_var.h>
76 #include <net/radix.h>
77 #include <net/route.h>
80 #if defined(INET) || defined(INET6)
81 #include <net/ethernet.h>
82 #include <netinet/in.h>
83 #include <netinet/in_var.h>
84 #include <netinet/ip.h>
85 #include <netinet/ip_carp.h>
87 #include <netinet/if_ether.h>
90 #include <netinet6/in6_var.h>
91 #include <netinet6/in6_ifattach.h>
93 #endif /* INET || INET6 */
95 #include <security/mac/mac_framework.h>
97 #ifdef COMPAT_FREEBSD32
98 #include <sys/mount.h>
99 #include <compat/freebsd32/freebsd32.h>
102 struct ifindex_entry {
103 struct ifnet *ife_ifnet;
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 SYSCTL_INT(_net_link, OID_AUTO, ifqmaxlen, CTLFLAG_RDTUN,
110 &ifqmaxlen, 0, "max send queue size");
112 /* Log link state change events */
113 static int log_link_state_change = 1;
115 SYSCTL_INT(_net_link, OID_AUTO, log_link_state_change, CTLFLAG_RW,
116 &log_link_state_change, 0,
117 "log interface link state change events");
119 /* Interface description */
120 static unsigned int ifdescr_maxlen = 1024;
121 SYSCTL_UINT(_net, OID_AUTO, ifdescr_maxlen, CTLFLAG_RW,
123 "administrative maximum length for interface description");
125 static MALLOC_DEFINE(M_IFDESCR, "ifdescr", "ifnet descriptions");
127 /* global sx for non-critical path ifdescr */
128 static struct sx ifdescr_sx;
129 SX_SYSINIT(ifdescr_sx, &ifdescr_sx, "ifnet descr");
131 void (*bridge_linkstate_p)(struct ifnet *ifp);
132 void (*ng_ether_link_state_p)(struct ifnet *ifp, int state);
133 void (*lagg_linkstate_p)(struct ifnet *ifp, int state);
134 /* These are external hooks for CARP. */
135 void (*carp_linkstate_p)(struct ifnet *ifp);
136 void (*carp_demote_adj_p)(int, char *);
137 int (*carp_master_p)(struct ifaddr *);
138 #if defined(INET) || defined(INET6)
139 int (*carp_forus_p)(struct ifnet *ifp, u_char *dhost);
140 int (*carp_output_p)(struct ifnet *ifp, struct mbuf *m,
141 const struct sockaddr *sa);
142 int (*carp_ioctl_p)(struct ifreq *, u_long, struct thread *);
143 int (*carp_attach_p)(struct ifaddr *, int);
144 void (*carp_detach_p)(struct ifaddr *);
147 int (*carp_iamatch_p)(struct ifaddr *, uint8_t **);
150 struct ifaddr *(*carp_iamatch6_p)(struct ifnet *ifp, struct in6_addr *taddr6);
151 caddr_t (*carp_macmatch6_p)(struct ifnet *ifp, struct mbuf *m,
152 const struct in6_addr *taddr);
155 struct mbuf *(*tbr_dequeue_ptr)(struct ifaltq *, int) = NULL;
158 * XXX: Style; these should be sorted alphabetically, and unprototyped
159 * static functions should be prototyped. Currently they are sorted by
162 static void if_attachdomain(void *);
163 static void if_attachdomain1(struct ifnet *);
164 static int ifconf(u_long, caddr_t);
165 static void if_freemulti(struct ifmultiaddr *);
166 static void if_init(void *);
167 static void if_grow(void);
168 static void if_route(struct ifnet *, int flag, int fam);
169 static int if_setflag(struct ifnet *, int, int, int *, int);
170 static int if_transmit(struct ifnet *ifp, struct mbuf *m);
171 static void if_unroute(struct ifnet *, int flag, int fam);
172 static void link_rtrequest(int, struct rtentry *, struct rt_addrinfo *);
173 static int if_rtdel(struct radix_node *, void *);
174 static int ifhwioctl(u_long, struct ifnet *, caddr_t, struct thread *);
175 static int if_delmulti_locked(struct ifnet *, struct ifmultiaddr *, int);
176 static void do_link_state_change(void *, int);
177 static int if_getgroup(struct ifgroupreq *, struct ifnet *);
178 static int if_getgroupmembers(struct ifgroupreq *);
179 static void if_delgroups(struct ifnet *);
180 static void if_attach_internal(struct ifnet *, int);
181 static void if_detach_internal(struct ifnet *, int);
185 * XXX: declare here to avoid to include many inet6 related files..
186 * should be more generalized?
188 extern void nd6_setmtu(struct ifnet *);
191 VNET_DEFINE(int, if_index);
192 int ifqmaxlen = IFQ_MAXLEN;
193 VNET_DEFINE(struct ifnethead, ifnet); /* depend on static init XXX */
194 VNET_DEFINE(struct ifgrouphead, ifg_head);
196 static VNET_DEFINE(int, if_indexlim) = 8;
198 /* Table of ifnet by index. */
199 VNET_DEFINE(struct ifindex_entry *, ifindex_table);
201 #define V_if_indexlim VNET(if_indexlim)
202 #define V_ifindex_table VNET(ifindex_table)
205 * The global network interface list (V_ifnet) and related state (such as
206 * if_index, if_indexlim, and ifindex_table) are protected by an sxlock and
207 * an rwlock. Either may be acquired shared to stablize the list, but both
208 * must be acquired writable to modify the list. This model allows us to
209 * both stablize the interface list during interrupt thread processing, but
210 * also to stablize it over long-running ioctls, without introducing priority
211 * inversions and deadlocks.
213 struct rwlock ifnet_rwlock;
214 struct sx ifnet_sxlock;
217 * The allocation of network interfaces is a rather non-atomic affair; we
218 * need to select an index before we are ready to expose the interface for
219 * use, so will use this pointer value to indicate reservation.
221 #define IFNET_HOLD (void *)(uintptr_t)(-1)
223 static if_com_alloc_t *if_com_alloc[256];
224 static if_com_free_t *if_com_free[256];
226 static MALLOC_DEFINE(M_IFNET, "ifnet", "interface internals");
227 MALLOC_DEFINE(M_IFADDR, "ifaddr", "interface address");
228 MALLOC_DEFINE(M_IFMADDR, "ether_multi", "link-level multicast address");
231 ifnet_byindex_locked(u_short idx)
234 if (idx > V_if_index)
236 if (V_ifindex_table[idx].ife_ifnet == IFNET_HOLD)
238 return (V_ifindex_table[idx].ife_ifnet);
242 ifnet_byindex(u_short idx)
246 IFNET_RLOCK_NOSLEEP();
247 ifp = ifnet_byindex_locked(idx);
248 IFNET_RUNLOCK_NOSLEEP();
253 ifnet_byindex_ref(u_short idx)
257 IFNET_RLOCK_NOSLEEP();
258 ifp = ifnet_byindex_locked(idx);
259 if (ifp == NULL || (ifp->if_flags & IFF_DYING)) {
260 IFNET_RUNLOCK_NOSLEEP();
264 IFNET_RUNLOCK_NOSLEEP();
269 * Allocate an ifindex array entry; return 0 on success or an error on
273 ifindex_alloc_locked(u_short *idxp)
277 IFNET_WLOCK_ASSERT();
281 * Try to find an empty slot below V_if_index. If we fail, take the
284 for (idx = 1; idx <= V_if_index; idx++) {
285 if (V_ifindex_table[idx].ife_ifnet == NULL)
289 /* Catch if_index overflow. */
290 if (idx >= V_if_indexlim) {
294 if (idx > V_if_index)
301 ifindex_free_locked(u_short idx)
304 IFNET_WLOCK_ASSERT();
306 V_ifindex_table[idx].ife_ifnet = NULL;
307 while (V_if_index > 0 &&
308 V_ifindex_table[V_if_index].ife_ifnet == NULL)
313 ifindex_free(u_short idx)
317 ifindex_free_locked(idx);
322 ifnet_setbyindex_locked(u_short idx, struct ifnet *ifp)
325 IFNET_WLOCK_ASSERT();
327 V_ifindex_table[idx].ife_ifnet = ifp;
331 ifnet_setbyindex(u_short idx, struct ifnet *ifp)
335 ifnet_setbyindex_locked(idx, ifp);
340 ifaddr_byindex(u_short idx)
344 IFNET_RLOCK_NOSLEEP();
345 ifa = ifnet_byindex_locked(idx)->if_addr;
348 IFNET_RUNLOCK_NOSLEEP();
353 * Network interface utility routines.
355 * Routines with ifa_ifwith* names take sockaddr *'s as
360 vnet_if_init(const void *unused __unused)
363 TAILQ_INIT(&V_ifnet);
364 TAILQ_INIT(&V_ifg_head);
366 if_grow(); /* create initial table */
368 vnet_if_clone_init();
370 VNET_SYSINIT(vnet_if_init, SI_SUB_INIT_IF, SI_ORDER_SECOND, vnet_if_init,
375 if_init(void *dummy __unused)
381 SYSINIT(interfaces, SI_SUB_INIT_IF, SI_ORDER_FIRST, if_init, NULL);
386 vnet_if_uninit(const void *unused __unused)
389 VNET_ASSERT(TAILQ_EMPTY(&V_ifnet), ("%s:%d tailq &V_ifnet=%p "
390 "not empty", __func__, __LINE__, &V_ifnet));
391 VNET_ASSERT(TAILQ_EMPTY(&V_ifg_head), ("%s:%d tailq &V_ifg_head=%p "
392 "not empty", __func__, __LINE__, &V_ifg_head));
394 free((caddr_t)V_ifindex_table, M_IFNET);
396 VNET_SYSUNINIT(vnet_if_uninit, SI_SUB_INIT_IF, SI_ORDER_FIRST,
397 vnet_if_uninit, NULL);
405 struct ifindex_entry *e;
407 IFNET_WLOCK_ASSERT();
408 oldlim = V_if_indexlim;
410 n = (oldlim << 1) * sizeof(*e);
411 e = malloc(n, M_IFNET, M_WAITOK | M_ZERO);
413 if (V_if_indexlim != oldlim) {
417 if (V_ifindex_table != NULL) {
418 memcpy((caddr_t)e, (caddr_t)V_ifindex_table, n/2);
419 free((caddr_t)V_ifindex_table, M_IFNET);
426 * Allocate a struct ifnet and an index for an interface. A layer 2
427 * common structure will also be allocated if an allocation routine is
428 * registered for the passed type.
431 if_alloc(u_char type)
436 ifp = malloc(sizeof(struct ifnet), M_IFNET, M_WAITOK|M_ZERO);
438 if (ifindex_alloc_locked(&idx) != 0) {
443 ifnet_setbyindex_locked(idx, IFNET_HOLD);
447 ifp->if_alloctype = type;
448 if (if_com_alloc[type] != NULL) {
449 ifp->if_l2com = if_com_alloc[type](type, ifp);
450 if (ifp->if_l2com == NULL) {
457 IF_ADDR_LOCK_INIT(ifp);
458 TASK_INIT(&ifp->if_linktask, 0, do_link_state_change, ifp);
459 ifp->if_afdata_initialized = 0;
460 IF_AFDATA_LOCK_INIT(ifp);
461 TAILQ_INIT(&ifp->if_addrhead);
462 TAILQ_INIT(&ifp->if_multiaddrs);
463 TAILQ_INIT(&ifp->if_groups);
467 ifq_init(&ifp->if_snd, ifp);
469 refcount_init(&ifp->if_refcount, 1); /* Index reference. */
470 ifnet_setbyindex(ifp->if_index, ifp);
475 * Do the actual work of freeing a struct ifnet, and layer 2 common
476 * structure. This call is made when the last reference to an
477 * interface is released.
480 if_free_internal(struct ifnet *ifp)
483 KASSERT((ifp->if_flags & IFF_DYING),
484 ("if_free_internal: interface not dying"));
486 if (if_com_free[ifp->if_alloctype] != NULL)
487 if_com_free[ifp->if_alloctype](ifp->if_l2com,
491 mac_ifnet_destroy(ifp);
493 if (ifp->if_description != NULL)
494 free(ifp->if_description, M_IFDESCR);
495 IF_AFDATA_DESTROY(ifp);
496 IF_ADDR_LOCK_DESTROY(ifp);
497 ifq_delete(&ifp->if_snd);
502 * Deregister an interface and free the associated storage.
505 if_free(struct ifnet *ifp)
508 ifp->if_flags |= IFF_DYING; /* XXX: Locking */
510 CURVNET_SET_QUIET(ifp->if_vnet);
512 KASSERT(ifp == ifnet_byindex_locked(ifp->if_index),
513 ("%s: freeing unallocated ifnet", ifp->if_xname));
515 ifindex_free_locked(ifp->if_index);
518 if (refcount_release(&ifp->if_refcount))
519 if_free_internal(ifp);
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_epoch = time_uptime;
610 KASSERT((ifp->if_transmit == NULL && ifp->if_qflush == NULL) ||
611 (ifp->if_transmit != NULL && ifp->if_qflush != NULL),
612 ("transmit and qflush must both either be set or both be NULL"));
613 if (ifp->if_transmit == NULL) {
614 ifp->if_transmit = if_transmit;
615 ifp->if_qflush = if_qflush;
618 if (ifp->if_get_counter == NULL)
619 ifp->if_get_counter = if_get_counter_default;
623 mac_ifnet_create(ifp);
627 * Create a Link Level name for this device.
629 namelen = strlen(ifp->if_xname);
631 * Always save enough space for any possiable name so we
632 * can do a rename in place later.
634 masklen = offsetof(struct sockaddr_dl, sdl_data[0]) + IFNAMSIZ;
635 socksize = masklen + ifp->if_addrlen;
636 if (socksize < sizeof(*sdl))
637 socksize = sizeof(*sdl);
638 socksize = roundup2(socksize, sizeof(long));
639 ifasize = sizeof(*ifa) + 2 * socksize;
640 ifa = ifa_alloc(ifasize, M_WAITOK);
641 sdl = (struct sockaddr_dl *)(ifa + 1);
642 sdl->sdl_len = socksize;
643 sdl->sdl_family = AF_LINK;
644 bcopy(ifp->if_xname, sdl->sdl_data, namelen);
645 sdl->sdl_nlen = namelen;
646 sdl->sdl_index = ifp->if_index;
647 sdl->sdl_type = ifp->if_type;
650 ifa->ifa_rtrequest = link_rtrequest;
651 ifa->ifa_addr = (struct sockaddr *)sdl;
652 sdl = (struct sockaddr_dl *)(socksize + (caddr_t)sdl);
653 ifa->ifa_netmask = (struct sockaddr *)sdl;
654 sdl->sdl_len = masklen;
656 sdl->sdl_data[--namelen] = 0xff;
657 TAILQ_INSERT_HEAD(&ifp->if_addrhead, ifa, ifa_link);
658 /* Reliably crash if used uninitialized. */
659 ifp->if_broadcastaddr = NULL;
661 #if defined(INET) || defined(INET6)
662 /* Initialize to max value. */
663 if (ifp->if_hw_tsomax == 0)
664 ifp->if_hw_tsomax = min(IP_MAXPACKET, 32 * MCLBYTES -
665 (ETHER_HDR_LEN + ETHER_VLAN_ENCAP_LEN));
666 KASSERT(ifp->if_hw_tsomax <= IP_MAXPACKET &&
667 ifp->if_hw_tsomax >= IP_MAXPACKET / 8,
668 ("%s: tsomax outside of range", __func__));
674 * Update the interface index in the link layer address
677 for (ifa = ifp->if_addr; ifa != NULL;
678 ifa = TAILQ_NEXT(ifa, ifa_link)) {
679 if (ifa->ifa_addr->sa_family == AF_LINK) {
680 sdl = (struct sockaddr_dl *)ifa->ifa_addr;
681 sdl->sdl_index = ifp->if_index;
688 TAILQ_INSERT_TAIL(&V_ifnet, ifp, if_link);
690 curvnet->vnet_ifcnt++;
694 if (domain_init_status >= 2)
695 if_attachdomain1(ifp);
697 EVENTHANDLER_INVOKE(ifnet_arrival_event, ifp);
698 if (IS_DEFAULT_VNET(curvnet))
699 devctl_notify("IFNET", ifp->if_xname, "ATTACH", NULL);
701 /* Announce the interface. */
702 rt_ifannouncemsg(ifp, IFAN_ARRIVAL);
706 if_attachdomain(void *dummy)
710 TAILQ_FOREACH(ifp, &V_ifnet, if_link)
711 if_attachdomain1(ifp);
713 SYSINIT(domainifattach, SI_SUB_PROTO_IFATTACHDOMAIN, SI_ORDER_SECOND,
714 if_attachdomain, NULL);
717 if_attachdomain1(struct ifnet *ifp)
722 * Since dp->dom_ifattach calls malloc() with M_WAITOK, we
723 * cannot lock ifp->if_afdata initialization, entirely.
725 if (IF_AFDATA_TRYLOCK(ifp) == 0)
727 if (ifp->if_afdata_initialized >= domain_init_status) {
728 IF_AFDATA_UNLOCK(ifp);
729 log(LOG_WARNING, "%s called more than once on %s\n",
730 __func__, ifp->if_xname);
733 ifp->if_afdata_initialized = domain_init_status;
734 IF_AFDATA_UNLOCK(ifp);
736 /* address family dependent data region */
737 bzero(ifp->if_afdata, sizeof(ifp->if_afdata));
738 for (dp = domains; dp; dp = dp->dom_next) {
739 if (dp->dom_ifattach)
740 ifp->if_afdata[dp->dom_family] =
741 (*dp->dom_ifattach)(ifp);
746 * Remove any unicast or broadcast network addresses from an interface.
749 if_purgeaddrs(struct ifnet *ifp)
751 struct ifaddr *ifa, *next;
753 TAILQ_FOREACH_SAFE(ifa, &ifp->if_addrhead, ifa_link, next) {
754 if (ifa->ifa_addr->sa_family == AF_LINK)
757 /* XXX: Ugly!! ad hoc just for INET */
758 if (ifa->ifa_addr->sa_family == AF_INET) {
759 struct ifaliasreq ifr;
761 bzero(&ifr, sizeof(ifr));
762 ifr.ifra_addr = *ifa->ifa_addr;
763 if (ifa->ifa_dstaddr)
764 ifr.ifra_broadaddr = *ifa->ifa_dstaddr;
765 if (in_control(NULL, SIOCDIFADDR, (caddr_t)&ifr, ifp,
771 if (ifa->ifa_addr->sa_family == AF_INET6) {
773 /* ifp_addrhead is already updated */
777 TAILQ_REMOVE(&ifp->if_addrhead, ifa, ifa_link);
783 * Remove any multicast network addresses from an interface when an ifnet
787 if_purgemaddrs(struct ifnet *ifp)
789 struct ifmultiaddr *ifma;
790 struct ifmultiaddr *next;
793 TAILQ_FOREACH_SAFE(ifma, &ifp->if_multiaddrs, ifma_link, next)
794 if_delmulti_locked(ifp, ifma, 1);
795 IF_ADDR_WUNLOCK(ifp);
799 * Detach an interface, removing it from the list of "active" interfaces.
800 * If vmove flag is set on entry to if_detach_internal(), perform only a
801 * limited subset of cleanup tasks, given that we are moving an ifnet from
802 * one vnet to another, where it must be fully operational.
804 * XXXRW: There are some significant questions about event ordering, and
805 * how to prevent things from starting to use the interface during detach.
808 if_detach(struct ifnet *ifp)
811 CURVNET_SET_QUIET(ifp->if_vnet);
812 if_detach_internal(ifp, 0);
817 if_detach_internal(struct ifnet *ifp, int vmove)
820 struct radix_node_head *rnh;
827 TAILQ_FOREACH(iter, &V_ifnet, if_link)
829 TAILQ_REMOVE(&V_ifnet, ifp, if_link);
835 curvnet->vnet_ifcnt--;
840 panic("%s: ifp=%p not on the ifnet tailq %p",
841 __func__, ifp, &V_ifnet);
843 return; /* XXX this should panic as well? */
847 * Remove/wait for pending events.
849 taskqueue_drain(taskqueue_swi, &ifp->if_linktask);
852 * Remove routes and flush queues.
856 if (ALTQ_IS_ENABLED(&ifp->if_snd))
857 altq_disable(&ifp->if_snd);
858 if (ALTQ_IS_ATTACHED(&ifp->if_snd))
859 altq_detach(&ifp->if_snd);
870 * Remove all IPv6 kernel structs related to ifp. This should be done
871 * before removing routing entries below, since IPv6 interface direct
872 * routes are expected to be removed by the IPv6-specific kernel API.
873 * Otherwise, the kernel will detect some inconsistency and bark it.
879 /* Announce that the interface is gone. */
880 rt_ifannouncemsg(ifp, IFAN_DEPARTURE);
881 EVENTHANDLER_INVOKE(ifnet_departure_event, ifp);
882 if (IS_DEFAULT_VNET(curvnet))
883 devctl_notify("IFNET", ifp->if_xname, "DETACH", NULL);
887 * Prevent further calls into the device driver via ifnet.
892 * Remove link ifaddr pointer and maybe decrement if_index.
893 * Clean up all addresses.
897 /* We can now free link ifaddr. */
898 if (!TAILQ_EMPTY(&ifp->if_addrhead)) {
899 ifa = TAILQ_FIRST(&ifp->if_addrhead);
900 TAILQ_REMOVE(&ifp->if_addrhead, ifa, ifa_link);
906 * Delete all remaining routes using this interface
907 * Unfortuneatly the only way to do this is to slog through
908 * the entire routing table looking for routes which point
909 * to this interface...oh well...
911 for (i = 1; i <= AF_MAX; i++) {
912 for (j = 0; j < rt_numfibs; j++) {
913 rnh = rt_tables_get_rnh(j, i);
916 RADIX_NODE_HEAD_LOCK(rnh);
917 (void) rnh->rnh_walktree(rnh, if_rtdel, ifp);
918 RADIX_NODE_HEAD_UNLOCK(rnh);
925 * We cannot hold the lock over dom_ifdetach calls as they might
926 * sleep, for example trying to drain a callout, thus open up the
927 * theoretical race with re-attaching.
930 i = ifp->if_afdata_initialized;
931 ifp->if_afdata_initialized = 0;
932 IF_AFDATA_UNLOCK(ifp);
933 for (dp = domains; i > 0 && dp; dp = dp->dom_next) {
934 if (dp->dom_ifdetach && ifp->if_afdata[dp->dom_family])
935 (*dp->dom_ifdetach)(ifp,
936 ifp->if_afdata[dp->dom_family]);
942 * if_vmove() performs a limited version of if_detach() in current
943 * vnet and if_attach()es the ifnet to the vnet specified as 2nd arg.
944 * An attempt is made to shrink if_index in current vnet, find an
945 * unused if_index in target vnet and calls if_grow() if necessary,
946 * and finally find an unused if_xname for the target vnet.
949 if_vmove(struct ifnet *ifp, struct vnet *new_vnet)
954 * Detach from current vnet, but preserve LLADDR info, do not
955 * mark as dead etc. so that the ifnet can be reattached later.
957 if_detach_internal(ifp, 1);
960 * Unlink the ifnet from ifindex_table[] in current vnet, and shrink
961 * the if_index for that vnet if possible.
963 * NOTE: IFNET_WLOCK/IFNET_WUNLOCK() are assumed to be unvirtualized,
964 * or we'd lock on one vnet and unlock on another.
967 ifindex_free_locked(ifp->if_index);
971 * Perform interface-specific reassignment tasks, if provided by
974 if (ifp->if_reassign != NULL)
975 ifp->if_reassign(ifp, new_vnet, NULL);
978 * Switch to the context of the target vnet.
980 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;
1096 if (groupname[0] && groupname[strlen(groupname) - 1] >= '0' &&
1097 groupname[strlen(groupname) - 1] <= '9')
1101 TAILQ_FOREACH(ifgl, &ifp->if_groups, ifgl_next)
1102 if (!strcmp(ifgl->ifgl_group->ifg_group, groupname)) {
1107 if ((ifgl = (struct ifg_list *)malloc(sizeof(struct ifg_list), M_TEMP,
1108 M_NOWAIT)) == NULL) {
1113 if ((ifgm = (struct ifg_member *)malloc(sizeof(struct ifg_member),
1114 M_TEMP, M_NOWAIT)) == NULL) {
1120 TAILQ_FOREACH(ifg, &V_ifg_head, ifg_next)
1121 if (!strcmp(ifg->ifg_group, groupname))
1125 if ((ifg = (struct ifg_group *)malloc(sizeof(struct ifg_group),
1126 M_TEMP, M_NOWAIT)) == NULL) {
1132 strlcpy(ifg->ifg_group, groupname, sizeof(ifg->ifg_group));
1133 ifg->ifg_refcnt = 0;
1134 TAILQ_INIT(&ifg->ifg_members);
1135 TAILQ_INSERT_TAIL(&V_ifg_head, ifg, ifg_next);
1140 ifgl->ifgl_group = ifg;
1141 ifgm->ifgm_ifp = ifp;
1144 TAILQ_INSERT_TAIL(&ifg->ifg_members, ifgm, ifgm_next);
1145 TAILQ_INSERT_TAIL(&ifp->if_groups, ifgl, ifgl_next);
1146 IF_ADDR_WUNLOCK(ifp);
1151 EVENTHANDLER_INVOKE(group_attach_event, ifg);
1152 EVENTHANDLER_INVOKE(group_change_event, groupname);
1158 * Remove a group from an interface
1161 if_delgroup(struct ifnet *ifp, const char *groupname)
1163 struct ifg_list *ifgl;
1164 struct ifg_member *ifgm;
1167 TAILQ_FOREACH(ifgl, &ifp->if_groups, ifgl_next)
1168 if (!strcmp(ifgl->ifgl_group->ifg_group, groupname))
1176 TAILQ_REMOVE(&ifp->if_groups, ifgl, ifgl_next);
1177 IF_ADDR_WUNLOCK(ifp);
1179 TAILQ_FOREACH(ifgm, &ifgl->ifgl_group->ifg_members, ifgm_next)
1180 if (ifgm->ifgm_ifp == ifp)
1184 TAILQ_REMOVE(&ifgl->ifgl_group->ifg_members, ifgm, ifgm_next);
1188 if (--ifgl->ifgl_group->ifg_refcnt == 0) {
1189 TAILQ_REMOVE(&V_ifg_head, ifgl->ifgl_group, ifg_next);
1191 EVENTHANDLER_INVOKE(group_detach_event, ifgl->ifgl_group);
1192 free(ifgl->ifgl_group, M_TEMP);
1198 EVENTHANDLER_INVOKE(group_change_event, groupname);
1204 * Remove an interface from all groups
1207 if_delgroups(struct ifnet *ifp)
1209 struct ifg_list *ifgl;
1210 struct ifg_member *ifgm;
1211 char groupname[IFNAMSIZ];
1214 while (!TAILQ_EMPTY(&ifp->if_groups)) {
1215 ifgl = TAILQ_FIRST(&ifp->if_groups);
1217 strlcpy(groupname, ifgl->ifgl_group->ifg_group, IFNAMSIZ);
1220 TAILQ_REMOVE(&ifp->if_groups, ifgl, ifgl_next);
1221 IF_ADDR_WUNLOCK(ifp);
1223 TAILQ_FOREACH(ifgm, &ifgl->ifgl_group->ifg_members, ifgm_next)
1224 if (ifgm->ifgm_ifp == ifp)
1228 TAILQ_REMOVE(&ifgl->ifgl_group->ifg_members, ifgm,
1233 if (--ifgl->ifgl_group->ifg_refcnt == 0) {
1234 TAILQ_REMOVE(&V_ifg_head, ifgl->ifgl_group, ifg_next);
1236 EVENTHANDLER_INVOKE(group_detach_event,
1238 free(ifgl->ifgl_group, M_TEMP);
1244 EVENTHANDLER_INVOKE(group_change_event, groupname);
1252 * Stores all groups from an interface in memory pointed
1256 if_getgroup(struct ifgroupreq *data, struct ifnet *ifp)
1259 struct ifg_list *ifgl;
1260 struct ifg_req ifgrq, *ifgp;
1261 struct ifgroupreq *ifgr = data;
1263 if (ifgr->ifgr_len == 0) {
1265 TAILQ_FOREACH(ifgl, &ifp->if_groups, ifgl_next)
1266 ifgr->ifgr_len += sizeof(struct ifg_req);
1267 IF_ADDR_RUNLOCK(ifp);
1271 len = ifgr->ifgr_len;
1272 ifgp = ifgr->ifgr_groups;
1275 TAILQ_FOREACH(ifgl, &ifp->if_groups, ifgl_next) {
1276 if (len < sizeof(ifgrq)) {
1277 IF_ADDR_RUNLOCK(ifp);
1280 bzero(&ifgrq, sizeof ifgrq);
1281 strlcpy(ifgrq.ifgrq_group, ifgl->ifgl_group->ifg_group,
1282 sizeof(ifgrq.ifgrq_group));
1283 if ((error = copyout(&ifgrq, ifgp, sizeof(struct ifg_req)))) {
1284 IF_ADDR_RUNLOCK(ifp);
1287 len -= sizeof(ifgrq);
1290 IF_ADDR_RUNLOCK(ifp);
1296 * Stores all members of a group in memory pointed to by data
1299 if_getgroupmembers(struct ifgroupreq *data)
1301 struct ifgroupreq *ifgr = data;
1302 struct ifg_group *ifg;
1303 struct ifg_member *ifgm;
1304 struct ifg_req ifgrq, *ifgp;
1308 TAILQ_FOREACH(ifg, &V_ifg_head, ifg_next)
1309 if (!strcmp(ifg->ifg_group, ifgr->ifgr_name))
1316 if (ifgr->ifgr_len == 0) {
1317 TAILQ_FOREACH(ifgm, &ifg->ifg_members, ifgm_next)
1318 ifgr->ifgr_len += sizeof(ifgrq);
1323 len = ifgr->ifgr_len;
1324 ifgp = ifgr->ifgr_groups;
1325 TAILQ_FOREACH(ifgm, &ifg->ifg_members, ifgm_next) {
1326 if (len < sizeof(ifgrq)) {
1330 bzero(&ifgrq, sizeof ifgrq);
1331 strlcpy(ifgrq.ifgrq_member, ifgm->ifgm_ifp->if_xname,
1332 sizeof(ifgrq.ifgrq_member));
1333 if ((error = copyout(&ifgrq, ifgp, sizeof(struct ifg_req)))) {
1337 len -= sizeof(ifgrq);
1346 * Delete Routes for a Network Interface
1348 * Called for each routing entry via the rnh->rnh_walktree() call above
1349 * to delete all route entries referencing a detaching network interface.
1352 * rn pointer to node in the routing table
1353 * arg argument passed to rnh->rnh_walktree() - detaching interface
1357 * errno failed - reason indicated
1361 if_rtdel(struct radix_node *rn, void *arg)
1363 struct rtentry *rt = (struct rtentry *)rn;
1364 struct ifnet *ifp = arg;
1367 if (rt->rt_ifp == ifp) {
1370 * Protect (sorta) against walktree recursion problems
1371 * with cloned routes
1373 if ((rt->rt_flags & RTF_UP) == 0)
1376 err = rtrequest_fib(RTM_DELETE, rt_key(rt), rt->rt_gateway,
1378 rt->rt_flags|RTF_RNH_LOCKED|RTF_PINNED,
1379 (struct rtentry **) NULL, rt->rt_fibnum);
1381 log(LOG_WARNING, "if_rtdel: error %d\n", err);
1389 * Return counter values from old racy non-pcpu counters.
1392 if_get_counter_default(struct ifnet *ifp, ift_counter cnt)
1396 case IFCOUNTER_IPACKETS:
1397 return (ifp->if_ipackets);
1398 case IFCOUNTER_IERRORS:
1399 return (ifp->if_ierrors);
1400 case IFCOUNTER_OPACKETS:
1401 return (ifp->if_opackets);
1402 case IFCOUNTER_OERRORS:
1403 return (ifp->if_oerrors);
1404 case IFCOUNTER_COLLISIONS:
1405 return (ifp->if_collisions);
1406 case IFCOUNTER_IBYTES:
1407 return (ifp->if_ibytes);
1408 case IFCOUNTER_OBYTES:
1409 return (ifp->if_obytes);
1410 case IFCOUNTER_IMCASTS:
1411 return (ifp->if_imcasts);
1412 case IFCOUNTER_OMCASTS:
1413 return (ifp->if_omcasts);
1414 case IFCOUNTER_IQDROPS:
1415 return (ifp->if_iqdrops);
1416 case IFCOUNTER_OQDROPS:
1417 return (ifp->if_oqdrops);
1418 case IFCOUNTER_NOPROTO:
1419 return (ifp->if_noproto);
1421 panic("%s: unknown counter %d", __func__, cnt);
1425 * Increase an ifnet counter. Usually used for counters shared
1426 * between the stack and a driver, but function supports them all.
1429 if_inc_counter(struct ifnet *ifp, ift_counter cnt, int64_t inc)
1433 case IFCOUNTER_IPACKETS:
1434 ifp->if_ipackets += inc;
1436 case IFCOUNTER_IERRORS:
1437 ifp->if_ierrors += inc;
1439 case IFCOUNTER_OPACKETS:
1440 ifp->if_opackets += inc;
1442 case IFCOUNTER_OERRORS:
1443 ifp->if_oerrors += inc;
1445 case IFCOUNTER_COLLISIONS:
1446 ifp->if_collisions += inc;
1448 case IFCOUNTER_IBYTES:
1449 ifp->if_ibytes += inc;
1451 case IFCOUNTER_OBYTES:
1452 ifp->if_obytes += inc;
1454 case IFCOUNTER_IMCASTS:
1455 ifp->if_imcasts += inc;
1457 case IFCOUNTER_OMCASTS:
1458 ifp->if_omcasts += inc;
1460 case IFCOUNTER_IQDROPS:
1461 ifp->if_iqdrops += inc;
1463 case IFCOUNTER_OQDROPS:
1464 ifp->if_oqdrops += inc;
1466 case IFCOUNTER_NOPROTO:
1467 ifp->if_noproto += inc;
1470 panic("%s: unknown counter %d", __func__, cnt);
1475 * Copy data from ifnet to userland API structure if_data.
1478 if_data_copy(struct ifnet *ifp, struct if_data *ifd)
1481 ifd->ifi_type = ifp->if_type;
1482 ifd->ifi_physical = 0;
1483 ifd->ifi_addrlen = ifp->if_addrlen;
1484 ifd->ifi_hdrlen = ifp->if_hdrlen;
1485 ifd->ifi_link_state = ifp->if_link_state;
1487 ifd->ifi_datalen = sizeof(struct if_data);
1488 ifd->ifi_mtu = ifp->if_mtu;
1489 ifd->ifi_metric = ifp->if_metric;
1490 ifd->ifi_baudrate = ifp->if_baudrate;
1491 ifd->ifi_hwassist = ifp->if_hwassist;
1492 ifd->ifi_epoch = ifp->if_epoch;
1493 ifd->ifi_lastchange = ifp->if_lastchange;
1495 ifd->ifi_ipackets = ifp->if_get_counter(ifp, IFCOUNTER_IPACKETS);
1496 ifd->ifi_ierrors = ifp->if_get_counter(ifp, IFCOUNTER_IERRORS);
1497 ifd->ifi_opackets = ifp->if_get_counter(ifp, IFCOUNTER_OPACKETS);
1498 ifd->ifi_oerrors = ifp->if_get_counter(ifp, IFCOUNTER_OERRORS);
1499 ifd->ifi_collisions = ifp->if_get_counter(ifp, IFCOUNTER_COLLISIONS);
1500 ifd->ifi_ibytes = ifp->if_get_counter(ifp, IFCOUNTER_IBYTES);
1501 ifd->ifi_obytes = ifp->if_get_counter(ifp, IFCOUNTER_OBYTES);
1502 ifd->ifi_imcasts = ifp->if_get_counter(ifp, IFCOUNTER_IMCASTS);
1503 ifd->ifi_omcasts = ifp->if_get_counter(ifp, IFCOUNTER_OMCASTS);
1504 ifd->ifi_iqdrops = ifp->if_get_counter(ifp, IFCOUNTER_IQDROPS);
1505 ifd->ifi_oqdrops = ifp->if_get_counter(ifp, IFCOUNTER_OQDROPS);
1506 ifd->ifi_noproto = ifp->if_get_counter(ifp, IFCOUNTER_NOPROTO);
1510 * Wrapper functions for struct ifnet address list locking macros. These are
1511 * used by kernel modules to avoid encoding programming interface or binary
1512 * interface assumptions that may be violated when kernel-internal locking
1513 * approaches change.
1516 if_addr_rlock(struct ifnet *ifp)
1523 if_addr_runlock(struct ifnet *ifp)
1526 IF_ADDR_RUNLOCK(ifp);
1530 if_maddr_rlock(if_t ifp)
1533 IF_ADDR_RLOCK((struct ifnet *)ifp);
1537 if_maddr_runlock(if_t ifp)
1540 IF_ADDR_RUNLOCK((struct ifnet *)ifp);
1544 * Initialization, destruction and refcounting functions for ifaddrs.
1547 ifa_alloc(size_t size, int flags)
1551 KASSERT(size >= sizeof(struct ifaddr),
1552 ("%s: invalid size %zu", __func__, size));
1554 ifa = malloc(size, M_IFADDR, M_ZERO | flags);
1558 if ((ifa->ifa_opackets = counter_u64_alloc(flags)) == NULL)
1560 if ((ifa->ifa_ipackets = counter_u64_alloc(flags)) == NULL)
1562 if ((ifa->ifa_obytes = counter_u64_alloc(flags)) == NULL)
1564 if ((ifa->ifa_ibytes = counter_u64_alloc(flags)) == NULL)
1567 refcount_init(&ifa->ifa_refcnt, 1);
1572 /* free(NULL) is okay */
1573 counter_u64_free(ifa->ifa_opackets);
1574 counter_u64_free(ifa->ifa_ipackets);
1575 counter_u64_free(ifa->ifa_obytes);
1576 counter_u64_free(ifa->ifa_ibytes);
1577 free(ifa, M_IFADDR);
1583 ifa_ref(struct ifaddr *ifa)
1586 refcount_acquire(&ifa->ifa_refcnt);
1590 ifa_free(struct ifaddr *ifa)
1593 if (refcount_release(&ifa->ifa_refcnt)) {
1594 counter_u64_free(ifa->ifa_opackets);
1595 counter_u64_free(ifa->ifa_ipackets);
1596 counter_u64_free(ifa->ifa_obytes);
1597 counter_u64_free(ifa->ifa_ibytes);
1598 free(ifa, M_IFADDR);
1603 ifa_add_loopback_route(struct ifaddr *ifa, struct sockaddr *ia)
1606 struct rtentry *rt = NULL;
1607 struct rt_addrinfo info;
1608 static struct sockaddr_dl null_sdl = {sizeof(null_sdl), AF_LINK};
1610 bzero(&info, sizeof(info));
1611 info.rti_ifp = V_loif;
1612 info.rti_flags = ifa->ifa_flags | RTF_HOST | RTF_STATIC;
1613 info.rti_info[RTAX_DST] = ia;
1614 info.rti_info[RTAX_GATEWAY] = (struct sockaddr *)&null_sdl;
1615 error = rtrequest1_fib(RTM_ADD, &info, &rt, ifa->ifa_ifp->if_fib);
1617 if (error == 0 && rt != NULL) {
1619 ((struct sockaddr_dl *)rt->rt_gateway)->sdl_type =
1620 ifa->ifa_ifp->if_type;
1621 ((struct sockaddr_dl *)rt->rt_gateway)->sdl_index =
1622 ifa->ifa_ifp->if_index;
1625 } else if (error != 0)
1626 log(LOG_DEBUG, "%s: insertion failed: %u\n", __func__, error);
1632 ifa_del_loopback_route(struct ifaddr *ifa, struct sockaddr *ia)
1635 struct rt_addrinfo info;
1636 struct sockaddr_dl null_sdl;
1638 bzero(&null_sdl, sizeof(null_sdl));
1639 null_sdl.sdl_len = sizeof(null_sdl);
1640 null_sdl.sdl_family = AF_LINK;
1641 null_sdl.sdl_type = ifa->ifa_ifp->if_type;
1642 null_sdl.sdl_index = ifa->ifa_ifp->if_index;
1643 bzero(&info, sizeof(info));
1644 info.rti_flags = ifa->ifa_flags | RTF_HOST | RTF_STATIC;
1645 info.rti_info[RTAX_DST] = ia;
1646 info.rti_info[RTAX_GATEWAY] = (struct sockaddr *)&null_sdl;
1647 error = rtrequest1_fib(RTM_DELETE, &info, NULL, ifa->ifa_ifp->if_fib);
1650 log(LOG_DEBUG, "%s: deletion failed: %u\n", __func__, error);
1656 ifa_switch_loopback_route(struct ifaddr *ifa, struct sockaddr *sa, int fib)
1660 rt = rtalloc1_fib(sa, 0, 0, fib);
1662 log(LOG_DEBUG, "%s: fail", __func__);
1663 return (EHOSTUNREACH);
1665 ((struct sockaddr_dl *)rt->rt_gateway)->sdl_type =
1666 ifa->ifa_ifp->if_type;
1667 ((struct sockaddr_dl *)rt->rt_gateway)->sdl_index =
1668 ifa->ifa_ifp->if_index;
1675 * XXX: Because sockaddr_dl has deeper structure than the sockaddr
1676 * structs used to represent other address families, it is necessary
1677 * to perform a different comparison.
1680 #define sa_dl_equal(a1, a2) \
1681 ((((struct sockaddr_dl *)(a1))->sdl_len == \
1682 ((struct sockaddr_dl *)(a2))->sdl_len) && \
1683 (bcmp(LLADDR((struct sockaddr_dl *)(a1)), \
1684 LLADDR((struct sockaddr_dl *)(a2)), \
1685 ((struct sockaddr_dl *)(a1))->sdl_alen) == 0))
1688 * Locate an interface based on a complete address.
1691 static struct ifaddr *
1692 ifa_ifwithaddr_internal(struct sockaddr *addr, int getref)
1697 IFNET_RLOCK_NOSLEEP();
1698 TAILQ_FOREACH(ifp, &V_ifnet, if_link) {
1700 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
1701 if (ifa->ifa_addr->sa_family != addr->sa_family)
1703 if (sa_equal(addr, ifa->ifa_addr)) {
1706 IF_ADDR_RUNLOCK(ifp);
1709 /* IP6 doesn't have broadcast */
1710 if ((ifp->if_flags & IFF_BROADCAST) &&
1711 ifa->ifa_broadaddr &&
1712 ifa->ifa_broadaddr->sa_len != 0 &&
1713 sa_equal(ifa->ifa_broadaddr, addr)) {
1716 IF_ADDR_RUNLOCK(ifp);
1720 IF_ADDR_RUNLOCK(ifp);
1724 IFNET_RUNLOCK_NOSLEEP();
1729 ifa_ifwithaddr(struct sockaddr *addr)
1732 return (ifa_ifwithaddr_internal(addr, 1));
1736 ifa_ifwithaddr_check(struct sockaddr *addr)
1739 return (ifa_ifwithaddr_internal(addr, 0) != NULL);
1743 * Locate an interface based on the broadcast address.
1747 ifa_ifwithbroadaddr(struct sockaddr *addr, int fibnum)
1752 IFNET_RLOCK_NOSLEEP();
1753 TAILQ_FOREACH(ifp, &V_ifnet, if_link) {
1754 if ((fibnum != RT_ALL_FIBS) && (ifp->if_fib != fibnum))
1757 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
1758 if (ifa->ifa_addr->sa_family != addr->sa_family)
1760 if ((ifp->if_flags & IFF_BROADCAST) &&
1761 ifa->ifa_broadaddr &&
1762 ifa->ifa_broadaddr->sa_len != 0 &&
1763 sa_equal(ifa->ifa_broadaddr, addr)) {
1765 IF_ADDR_RUNLOCK(ifp);
1769 IF_ADDR_RUNLOCK(ifp);
1773 IFNET_RUNLOCK_NOSLEEP();
1778 * Locate the point to point interface with a given destination address.
1782 ifa_ifwithdstaddr(struct sockaddr *addr, int fibnum)
1787 IFNET_RLOCK_NOSLEEP();
1788 TAILQ_FOREACH(ifp, &V_ifnet, if_link) {
1789 if ((ifp->if_flags & IFF_POINTOPOINT) == 0)
1791 if ((fibnum != RT_ALL_FIBS) && (ifp->if_fib != fibnum))
1794 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
1795 if (ifa->ifa_addr->sa_family != addr->sa_family)
1797 if (ifa->ifa_dstaddr != NULL &&
1798 sa_equal(addr, ifa->ifa_dstaddr)) {
1800 IF_ADDR_RUNLOCK(ifp);
1804 IF_ADDR_RUNLOCK(ifp);
1808 IFNET_RUNLOCK_NOSLEEP();
1813 * Find an interface on a specific network. If many, choice
1814 * is most specific found.
1817 ifa_ifwithnet(struct sockaddr *addr, int ignore_ptp, int fibnum)
1821 struct ifaddr *ifa_maybe = NULL;
1822 u_int af = addr->sa_family;
1823 char *addr_data = addr->sa_data, *cplim;
1826 * AF_LINK addresses can be looked up directly by their index number,
1827 * so do that if we can.
1829 if (af == AF_LINK) {
1830 struct sockaddr_dl *sdl = (struct sockaddr_dl *)addr;
1831 if (sdl->sdl_index && sdl->sdl_index <= V_if_index)
1832 return (ifaddr_byindex(sdl->sdl_index));
1836 * Scan though each interface, looking for ones that have addresses
1837 * in this address family and the requested fib. Maintain a reference
1838 * on ifa_maybe once we find one, as we release the IF_ADDR_RLOCK() that
1839 * kept it stable when we move onto the next interface.
1841 IFNET_RLOCK_NOSLEEP();
1842 TAILQ_FOREACH(ifp, &V_ifnet, if_link) {
1843 if ((fibnum != RT_ALL_FIBS) && (ifp->if_fib != fibnum))
1846 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
1847 char *cp, *cp2, *cp3;
1849 if (ifa->ifa_addr->sa_family != af)
1851 if (af == AF_INET &&
1852 ifp->if_flags & IFF_POINTOPOINT && !ignore_ptp) {
1854 * This is a bit broken as it doesn't
1855 * take into account that the remote end may
1856 * be a single node in the network we are
1858 * The trouble is that we don't know the
1859 * netmask for the remote end.
1861 if (ifa->ifa_dstaddr != NULL &&
1862 sa_equal(addr, ifa->ifa_dstaddr)) {
1864 IF_ADDR_RUNLOCK(ifp);
1869 * Scan all the bits in the ifa's address.
1870 * If a bit dissagrees with what we are
1871 * looking for, mask it with the netmask
1872 * to see if it really matters.
1873 * (A byte at a time)
1875 if (ifa->ifa_netmask == 0)
1878 cp2 = ifa->ifa_addr->sa_data;
1879 cp3 = ifa->ifa_netmask->sa_data;
1880 cplim = ifa->ifa_netmask->sa_len
1881 + (char *)ifa->ifa_netmask;
1883 if ((*cp++ ^ *cp2++) & *cp3++)
1884 goto next; /* next address! */
1886 * If the netmask of what we just found
1887 * is more specific than what we had before
1888 * (if we had one), or if the virtual status
1889 * of new prefix is better than of the old one,
1890 * then remember the new one before continuing
1891 * to search for an even better one.
1893 if (ifa_maybe == NULL ||
1894 ifa_preferred(ifa_maybe, ifa) ||
1895 rn_refines((caddr_t)ifa->ifa_netmask,
1896 (caddr_t)ifa_maybe->ifa_netmask)) {
1897 if (ifa_maybe != NULL)
1898 ifa_free(ifa_maybe);
1904 IF_ADDR_RUNLOCK(ifp);
1909 IFNET_RUNLOCK_NOSLEEP();
1910 if (ifa_maybe != NULL)
1911 ifa_free(ifa_maybe);
1916 * Find an interface address specific to an interface best matching
1920 ifaof_ifpforaddr(struct sockaddr *addr, struct ifnet *ifp)
1923 char *cp, *cp2, *cp3;
1925 struct ifaddr *ifa_maybe = NULL;
1926 u_int af = addr->sa_family;
1931 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
1932 if (ifa->ifa_addr->sa_family != af)
1934 if (ifa_maybe == NULL)
1936 if (ifa->ifa_netmask == 0) {
1937 if (sa_equal(addr, ifa->ifa_addr) ||
1938 (ifa->ifa_dstaddr &&
1939 sa_equal(addr, ifa->ifa_dstaddr)))
1943 if (ifp->if_flags & IFF_POINTOPOINT) {
1944 if (sa_equal(addr, ifa->ifa_dstaddr))
1948 cp2 = ifa->ifa_addr->sa_data;
1949 cp3 = ifa->ifa_netmask->sa_data;
1950 cplim = ifa->ifa_netmask->sa_len + (char *)ifa->ifa_netmask;
1951 for (; cp3 < cplim; cp3++)
1952 if ((*cp++ ^ *cp2++) & *cp3)
1962 IF_ADDR_RUNLOCK(ifp);
1967 * See whether new ifa is better than current one:
1968 * 1) A non-virtual one is preferred over virtual.
1969 * 2) A virtual in master state preferred over any other state.
1971 * Used in several address selecting functions.
1974 ifa_preferred(struct ifaddr *cur, struct ifaddr *next)
1977 return (cur->ifa_carp && (!next->ifa_carp ||
1978 ((*carp_master_p)(next) && !(*carp_master_p)(cur))));
1981 #include <net/if_llatbl.h>
1984 * Default action when installing a route with a Link Level gateway.
1985 * Lookup an appropriate real ifa to point to.
1986 * This should be moved to /sys/net/link.c eventually.
1989 link_rtrequest(int cmd, struct rtentry *rt, struct rt_addrinfo *info)
1991 struct ifaddr *ifa, *oifa;
1992 struct sockaddr *dst;
1997 if (cmd != RTM_ADD || ((ifa = rt->rt_ifa) == 0) ||
1998 ((ifp = ifa->ifa_ifp) == 0) || ((dst = rt_key(rt)) == 0))
2000 ifa = ifaof_ifpforaddr(dst, ifp);
2005 if (ifa->ifa_rtrequest && ifa->ifa_rtrequest != link_rtrequest)
2006 ifa->ifa_rtrequest(cmd, rt, info);
2010 struct sockaddr_dl *
2011 link_alloc_sdl(size_t size, int flags)
2014 return (malloc(size, M_TEMP, flags));
2018 link_free_sdl(struct sockaddr *sa)
2024 * Fills in given sdl with interface basic info.
2025 * Returns pointer to filled sdl.
2027 struct sockaddr_dl *
2028 link_init_sdl(struct ifnet *ifp, struct sockaddr *paddr, u_char iftype)
2030 struct sockaddr_dl *sdl;
2032 sdl = (struct sockaddr_dl *)paddr;
2033 memset(sdl, 0, sizeof(struct sockaddr_dl));
2034 sdl->sdl_len = sizeof(struct sockaddr_dl);
2035 sdl->sdl_family = AF_LINK;
2036 sdl->sdl_index = ifp->if_index;
2037 sdl->sdl_type = iftype;
2043 * Mark an interface down and notify protocols of
2047 if_unroute(struct ifnet *ifp, int flag, int fam)
2051 KASSERT(flag == IFF_UP, ("if_unroute: flag != IFF_UP"));
2053 ifp->if_flags &= ~flag;
2054 getmicrotime(&ifp->if_lastchange);
2055 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link)
2056 if (fam == PF_UNSPEC || (fam == ifa->ifa_addr->sa_family))
2057 pfctlinput(PRC_IFDOWN, ifa->ifa_addr);
2058 ifp->if_qflush(ifp);
2061 (*carp_linkstate_p)(ifp);
2066 * Mark an interface up and notify protocols of
2070 if_route(struct ifnet *ifp, int flag, int fam)
2074 KASSERT(flag == IFF_UP, ("if_route: flag != IFF_UP"));
2076 ifp->if_flags |= flag;
2077 getmicrotime(&ifp->if_lastchange);
2078 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link)
2079 if (fam == PF_UNSPEC || (fam == ifa->ifa_addr->sa_family))
2080 pfctlinput(PRC_IFUP, ifa->ifa_addr);
2082 (*carp_linkstate_p)(ifp);
2089 void (*vlan_link_state_p)(struct ifnet *); /* XXX: private from if_vlan */
2090 void (*vlan_trunk_cap_p)(struct ifnet *); /* XXX: private from if_vlan */
2091 struct ifnet *(*vlan_trunkdev_p)(struct ifnet *);
2092 struct ifnet *(*vlan_devat_p)(struct ifnet *, uint16_t);
2093 int (*vlan_tag_p)(struct ifnet *, uint16_t *);
2094 int (*vlan_setcookie_p)(struct ifnet *, void *);
2095 void *(*vlan_cookie_p)(struct ifnet *);
2098 * Handle a change in the interface link state. To avoid LORs
2099 * between driver lock and upper layer locks, as well as possible
2100 * recursions, we post event to taskqueue, and all job
2101 * is done in static do_link_state_change().
2104 if_link_state_change(struct ifnet *ifp, int link_state)
2106 /* Return if state hasn't changed. */
2107 if (ifp->if_link_state == link_state)
2110 ifp->if_link_state = link_state;
2112 taskqueue_enqueue(taskqueue_swi, &ifp->if_linktask);
2116 do_link_state_change(void *arg, int pending)
2118 struct ifnet *ifp = (struct ifnet *)arg;
2119 int link_state = ifp->if_link_state;
2120 CURVNET_SET(ifp->if_vnet);
2122 /* Notify that the link state has changed. */
2124 if (ifp->if_vlantrunk != NULL)
2125 (*vlan_link_state_p)(ifp);
2127 if ((ifp->if_type == IFT_ETHER || ifp->if_type == IFT_L2VLAN) &&
2128 IFP2AC(ifp)->ac_netgraph != NULL)
2129 (*ng_ether_link_state_p)(ifp, link_state);
2131 (*carp_linkstate_p)(ifp);
2133 (*bridge_linkstate_p)(ifp);
2135 (*lagg_linkstate_p)(ifp, link_state);
2137 if (IS_DEFAULT_VNET(curvnet))
2138 devctl_notify("IFNET", ifp->if_xname,
2139 (link_state == LINK_STATE_UP) ? "LINK_UP" : "LINK_DOWN",
2142 if_printf(ifp, "%d link states coalesced\n", pending);
2143 if (log_link_state_change)
2144 log(LOG_NOTICE, "%s: link state changed to %s\n", ifp->if_xname,
2145 (link_state == LINK_STATE_UP) ? "UP" : "DOWN" );
2146 EVENTHANDLER_INVOKE(ifnet_link_event, ifp, ifp->if_link_state);
2151 * Mark an interface down and notify protocols of
2155 if_down(struct ifnet *ifp)
2158 if_unroute(ifp, IFF_UP, AF_UNSPEC);
2162 * Mark an interface up and notify protocols of
2166 if_up(struct ifnet *ifp)
2169 if_route(ifp, IFF_UP, AF_UNSPEC);
2173 * Flush an interface queue.
2176 if_qflush(struct ifnet *ifp)
2184 if (ALTQ_IS_ENABLED(ifq))
2188 while ((m = n) != 0) {
2199 * Map interface name to interface structure pointer, with or without
2200 * returning a reference.
2203 ifunit_ref(const char *name)
2207 IFNET_RLOCK_NOSLEEP();
2208 TAILQ_FOREACH(ifp, &V_ifnet, if_link) {
2209 if (strncmp(name, ifp->if_xname, IFNAMSIZ) == 0 &&
2210 !(ifp->if_flags & IFF_DYING))
2215 IFNET_RUNLOCK_NOSLEEP();
2220 ifunit(const char *name)
2224 IFNET_RLOCK_NOSLEEP();
2225 TAILQ_FOREACH(ifp, &V_ifnet, if_link) {
2226 if (strncmp(name, ifp->if_xname, IFNAMSIZ) == 0)
2229 IFNET_RUNLOCK_NOSLEEP();
2234 * Hardware specific interface ioctls.
2237 ifhwioctl(u_long cmd, struct ifnet *ifp, caddr_t data, struct thread *td)
2241 int new_flags, temp_flags;
2242 size_t namelen, onamelen;
2244 char *descrbuf, *odescrbuf;
2245 char new_name[IFNAMSIZ];
2247 struct sockaddr_dl *sdl;
2249 ifr = (struct ifreq *)data;
2252 ifr->ifr_index = ifp->if_index;
2256 temp_flags = ifp->if_flags | ifp->if_drv_flags;
2257 ifr->ifr_flags = temp_flags & 0xffff;
2258 ifr->ifr_flagshigh = temp_flags >> 16;
2262 ifr->ifr_reqcap = ifp->if_capabilities;
2263 ifr->ifr_curcap = ifp->if_capenable;
2268 error = mac_ifnet_ioctl_get(td->td_ucred, ifr, ifp);
2273 ifr->ifr_metric = ifp->if_metric;
2277 ifr->ifr_mtu = ifp->if_mtu;
2281 /* XXXGL: did this ever worked? */
2287 sx_slock(&ifdescr_sx);
2288 if (ifp->if_description == NULL)
2291 /* space for terminating nul */
2292 descrlen = strlen(ifp->if_description) + 1;
2293 if (ifr->ifr_buffer.length < descrlen)
2294 ifr->ifr_buffer.buffer = NULL;
2296 error = copyout(ifp->if_description,
2297 ifr->ifr_buffer.buffer, descrlen);
2298 ifr->ifr_buffer.length = descrlen;
2300 sx_sunlock(&ifdescr_sx);
2304 error = priv_check(td, PRIV_NET_SETIFDESCR);
2309 * Copy only (length-1) bytes to make sure that
2310 * if_description is always nul terminated. The
2311 * length parameter is supposed to count the
2312 * terminating nul in.
2314 if (ifr->ifr_buffer.length > ifdescr_maxlen)
2315 return (ENAMETOOLONG);
2316 else if (ifr->ifr_buffer.length == 0)
2319 descrbuf = malloc(ifr->ifr_buffer.length, M_IFDESCR,
2321 error = copyin(ifr->ifr_buffer.buffer, descrbuf,
2322 ifr->ifr_buffer.length - 1);
2324 free(descrbuf, M_IFDESCR);
2329 sx_xlock(&ifdescr_sx);
2330 odescrbuf = ifp->if_description;
2331 ifp->if_description = descrbuf;
2332 sx_xunlock(&ifdescr_sx);
2334 getmicrotime(&ifp->if_lastchange);
2335 free(odescrbuf, M_IFDESCR);
2339 ifr->ifr_fib = ifp->if_fib;
2343 error = priv_check(td, PRIV_NET_SETIFFIB);
2346 if (ifr->ifr_fib >= rt_numfibs)
2349 ifp->if_fib = ifr->ifr_fib;
2353 error = priv_check(td, PRIV_NET_SETIFFLAGS);
2357 * Currently, no driver owned flags pass the IFF_CANTCHANGE
2358 * check, so we don't need special handling here yet.
2360 new_flags = (ifr->ifr_flags & 0xffff) |
2361 (ifr->ifr_flagshigh << 16);
2362 if (ifp->if_flags & IFF_UP &&
2363 (new_flags & IFF_UP) == 0) {
2365 } else if (new_flags & IFF_UP &&
2366 (ifp->if_flags & IFF_UP) == 0) {
2369 /* See if permanently promiscuous mode bit is about to flip */
2370 if ((ifp->if_flags ^ new_flags) & IFF_PPROMISC) {
2371 if (new_flags & IFF_PPROMISC)
2372 ifp->if_flags |= IFF_PROMISC;
2373 else if (ifp->if_pcount == 0)
2374 ifp->if_flags &= ~IFF_PROMISC;
2375 log(LOG_INFO, "%s: permanently promiscuous mode %s\n",
2377 (new_flags & IFF_PPROMISC) ? "enabled" : "disabled");
2379 ifp->if_flags = (ifp->if_flags & IFF_CANTCHANGE) |
2380 (new_flags &~ IFF_CANTCHANGE);
2381 if (ifp->if_ioctl) {
2382 (void) (*ifp->if_ioctl)(ifp, cmd, data);
2384 getmicrotime(&ifp->if_lastchange);
2388 error = priv_check(td, PRIV_NET_SETIFCAP);
2391 if (ifp->if_ioctl == NULL)
2392 return (EOPNOTSUPP);
2393 if (ifr->ifr_reqcap & ~ifp->if_capabilities)
2395 error = (*ifp->if_ioctl)(ifp, cmd, data);
2397 getmicrotime(&ifp->if_lastchange);
2402 error = mac_ifnet_ioctl_set(td->td_ucred, ifr, ifp);
2407 error = priv_check(td, PRIV_NET_SETIFNAME);
2410 error = copyinstr(ifr->ifr_data, new_name, IFNAMSIZ, NULL);
2413 if (new_name[0] == '\0')
2415 if (ifunit(new_name) != NULL)
2419 * XXX: Locking. Nothing else seems to lock if_flags,
2420 * and there are numerous other races with the
2421 * ifunit() checks not being atomic with namespace
2422 * changes (renames, vmoves, if_attach, etc).
2424 ifp->if_flags |= IFF_RENAMING;
2426 /* Announce the departure of the interface. */
2427 rt_ifannouncemsg(ifp, IFAN_DEPARTURE);
2428 EVENTHANDLER_INVOKE(ifnet_departure_event, ifp);
2430 log(LOG_INFO, "%s: changing name to '%s'\n",
2431 ifp->if_xname, new_name);
2434 strlcpy(ifp->if_xname, new_name, sizeof(ifp->if_xname));
2436 sdl = (struct sockaddr_dl *)ifa->ifa_addr;
2437 namelen = strlen(new_name);
2438 onamelen = sdl->sdl_nlen;
2440 * Move the address if needed. This is safe because we
2441 * allocate space for a name of length IFNAMSIZ when we
2442 * create this in if_attach().
2444 if (namelen != onamelen) {
2445 bcopy(sdl->sdl_data + onamelen,
2446 sdl->sdl_data + namelen, sdl->sdl_alen);
2448 bcopy(new_name, sdl->sdl_data, namelen);
2449 sdl->sdl_nlen = namelen;
2450 sdl = (struct sockaddr_dl *)ifa->ifa_netmask;
2451 bzero(sdl->sdl_data, onamelen);
2452 while (namelen != 0)
2453 sdl->sdl_data[--namelen] = 0xff;
2454 IF_ADDR_WUNLOCK(ifp);
2456 EVENTHANDLER_INVOKE(ifnet_arrival_event, ifp);
2457 /* Announce the return of the interface. */
2458 rt_ifannouncemsg(ifp, IFAN_ARRIVAL);
2460 ifp->if_flags &= ~IFF_RENAMING;
2465 error = priv_check(td, PRIV_NET_SETIFVNET);
2468 error = if_vmove_loan(td, ifp, ifr->ifr_name, ifr->ifr_jid);
2473 error = priv_check(td, PRIV_NET_SETIFMETRIC);
2476 ifp->if_metric = ifr->ifr_metric;
2477 getmicrotime(&ifp->if_lastchange);
2481 error = priv_check(td, PRIV_NET_SETIFPHYS);
2484 if (ifp->if_ioctl == NULL)
2485 return (EOPNOTSUPP);
2486 error = (*ifp->if_ioctl)(ifp, cmd, data);
2488 getmicrotime(&ifp->if_lastchange);
2493 u_long oldmtu = ifp->if_mtu;
2495 error = priv_check(td, PRIV_NET_SETIFMTU);
2498 if (ifr->ifr_mtu < IF_MINMTU || ifr->ifr_mtu > IF_MAXMTU)
2500 if (ifp->if_ioctl == NULL)
2501 return (EOPNOTSUPP);
2502 error = (*ifp->if_ioctl)(ifp, cmd, data);
2504 getmicrotime(&ifp->if_lastchange);
2508 * If the link MTU changed, do network layer specific procedure.
2510 if (ifp->if_mtu != oldmtu) {
2520 if (cmd == SIOCADDMULTI)
2521 error = priv_check(td, PRIV_NET_ADDMULTI);
2523 error = priv_check(td, PRIV_NET_DELMULTI);
2527 /* Don't allow group membership on non-multicast interfaces. */
2528 if ((ifp->if_flags & IFF_MULTICAST) == 0)
2529 return (EOPNOTSUPP);
2531 /* Don't let users screw up protocols' entries. */
2532 if (ifr->ifr_addr.sa_family != AF_LINK)
2535 if (cmd == SIOCADDMULTI) {
2536 struct ifmultiaddr *ifma;
2539 * Userland is only permitted to join groups once
2540 * via the if_addmulti() KPI, because it cannot hold
2541 * struct ifmultiaddr * between calls. It may also
2542 * lose a race while we check if the membership
2546 ifma = if_findmulti(ifp, &ifr->ifr_addr);
2547 IF_ADDR_RUNLOCK(ifp);
2551 error = if_addmulti(ifp, &ifr->ifr_addr, &ifma);
2553 error = if_delmulti(ifp, &ifr->ifr_addr);
2556 getmicrotime(&ifp->if_lastchange);
2559 case SIOCSIFPHYADDR:
2560 case SIOCDIFPHYADDR:
2562 case SIOCSIFPHYADDR_IN6:
2565 case SIOCSIFGENERIC:
2566 error = priv_check(td, PRIV_NET_HWIOCTL);
2569 if (ifp->if_ioctl == NULL)
2570 return (EOPNOTSUPP);
2571 error = (*ifp->if_ioctl)(ifp, cmd, data);
2573 getmicrotime(&ifp->if_lastchange);
2577 case SIOCGIFPSRCADDR:
2578 case SIOCGIFPDSTADDR:
2580 case SIOCGIFGENERIC:
2581 if (ifp->if_ioctl == NULL)
2582 return (EOPNOTSUPP);
2583 error = (*ifp->if_ioctl)(ifp, cmd, data);
2587 error = priv_check(td, PRIV_NET_SETLLADDR);
2590 error = if_setlladdr(ifp,
2591 ifr->ifr_addr.sa_data, ifr->ifr_addr.sa_len);
2592 EVENTHANDLER_INVOKE(iflladdr_event, ifp);
2597 struct ifgroupreq *ifgr = (struct ifgroupreq *)ifr;
2599 error = priv_check(td, PRIV_NET_ADDIFGROUP);
2602 if ((error = if_addgroup(ifp, ifgr->ifgr_group)))
2608 if ((error = if_getgroup((struct ifgroupreq *)ifr, ifp)))
2614 struct ifgroupreq *ifgr = (struct ifgroupreq *)ifr;
2616 error = priv_check(td, PRIV_NET_DELIFGROUP);
2619 if ((error = if_delgroup(ifp, ifgr->ifgr_group)))
2631 #ifdef COMPAT_FREEBSD32
2639 #define SIOCGIFCONF32 _IOWR('i', 36, struct ifconf32)
2646 ifioctl(struct socket *so, u_long cmd, caddr_t data, struct thread *td)
2653 CURVNET_SET(so->so_vnet);
2656 error = ifconf(cmd, data);
2660 #ifdef COMPAT_FREEBSD32
2663 struct ifconf32 *ifc32;
2666 ifc32 = (struct ifconf32 *)data;
2667 ifc.ifc_len = ifc32->ifc_len;
2668 ifc.ifc_buf = PTRIN(ifc32->ifc_buf);
2670 error = ifconf(SIOCGIFCONF, (void *)&ifc);
2673 ifc32->ifc_len = ifc.ifc_len;
2678 ifr = (struct ifreq *)data;
2683 error = priv_check(td, PRIV_NET_SETIFVNET);
2685 error = if_vmove_reclaim(td, ifr->ifr_name,
2692 error = priv_check(td, PRIV_NET_IFCREATE);
2694 error = if_clone_create(ifr->ifr_name,
2695 sizeof(ifr->ifr_name),
2696 cmd == SIOCIFCREATE2 ? ifr->ifr_data : NULL);
2700 error = priv_check(td, PRIV_NET_IFDESTROY);
2702 error = if_clone_destroy(ifr->ifr_name);
2706 case SIOCIFGCLONERS:
2707 error = if_clone_list((struct if_clonereq *)data);
2711 error = if_getgroupmembers((struct ifgroupreq *)data);
2714 #if defined(INET) || defined(INET6)
2717 if (carp_ioctl_p == NULL)
2718 error = EPROTONOSUPPORT;
2720 error = (*carp_ioctl_p)(ifr, cmd, td);
2726 ifp = ifunit_ref(ifr->ifr_name);
2732 error = ifhwioctl(cmd, ifp, data, td);
2733 if (error != ENOIOCTL) {
2739 oif_flags = ifp->if_flags;
2740 if (so->so_proto == NULL) {
2743 return (EOPNOTSUPP);
2747 * Pass the request on to the socket control method, and if the
2748 * latter returns EOPNOTSUPP, directly to the interface.
2750 * Make an exception for the legacy SIOCSIF* requests. Drivers
2751 * trust SIOCSIFADDR et al to come from an already privileged
2752 * layer, and do not perform any credentials checks or input
2755 error = ((*so->so_proto->pr_usrreqs->pru_control)(so, cmd, data,
2757 if (error == EOPNOTSUPP && ifp != NULL && ifp->if_ioctl != NULL &&
2758 cmd != SIOCSIFADDR && cmd != SIOCSIFBRDADDR &&
2759 cmd != SIOCSIFDSTADDR && cmd != SIOCSIFNETMASK)
2760 error = (*ifp->if_ioctl)(ifp, cmd, data);
2762 if ((oif_flags ^ ifp->if_flags) & IFF_UP) {
2764 if (ifp->if_flags & IFF_UP)
2774 * The code common to handling reference counted flags,
2775 * e.g., in ifpromisc() and if_allmulti().
2776 * The "pflag" argument can specify a permanent mode flag to check,
2777 * such as IFF_PPROMISC for promiscuous mode; should be 0 if none.
2779 * Only to be used on stack-owned flags, not driver-owned flags.
2782 if_setflag(struct ifnet *ifp, int flag, int pflag, int *refcount, int onswitch)
2786 int oldflags, oldcount;
2788 /* Sanity checks to catch programming errors */
2789 KASSERT((flag & (IFF_DRV_OACTIVE|IFF_DRV_RUNNING)) == 0,
2790 ("%s: setting driver-owned flag %d", __func__, flag));
2793 KASSERT(*refcount >= 0,
2794 ("%s: increment negative refcount %d for flag %d",
2795 __func__, *refcount, flag));
2797 KASSERT(*refcount > 0,
2798 ("%s: decrement non-positive refcount %d for flag %d",
2799 __func__, *refcount, flag));
2801 /* In case this mode is permanent, just touch refcount */
2802 if (ifp->if_flags & pflag) {
2803 *refcount += onswitch ? 1 : -1;
2807 /* Save ifnet parameters for if_ioctl() may fail */
2808 oldcount = *refcount;
2809 oldflags = ifp->if_flags;
2812 * See if we aren't the only and touching refcount is enough.
2813 * Actually toggle interface flag if we are the first or last.
2818 ifp->if_flags |= flag;
2822 ifp->if_flags &= ~flag;
2825 /* Call down the driver since we've changed interface flags */
2826 if (ifp->if_ioctl == NULL) {
2830 ifr.ifr_flags = ifp->if_flags & 0xffff;
2831 ifr.ifr_flagshigh = ifp->if_flags >> 16;
2832 error = (*ifp->if_ioctl)(ifp, SIOCSIFFLAGS, (caddr_t)&ifr);
2835 /* Notify userland that interface flags have changed */
2840 /* Recover after driver error */
2841 *refcount = oldcount;
2842 ifp->if_flags = oldflags;
2847 * Set/clear promiscuous mode on interface ifp based on the truth value
2848 * of pswitch. The calls are reference counted so that only the first
2849 * "on" request actually has an effect, as does the final "off" request.
2850 * Results are undefined if the "off" and "on" requests are not matched.
2853 ifpromisc(struct ifnet *ifp, int pswitch)
2856 int oldflags = ifp->if_flags;
2858 error = if_setflag(ifp, IFF_PROMISC, IFF_PPROMISC,
2859 &ifp->if_pcount, pswitch);
2860 /* If promiscuous mode status has changed, log a message */
2861 if (error == 0 && ((ifp->if_flags ^ oldflags) & IFF_PROMISC))
2862 log(LOG_INFO, "%s: promiscuous mode %s\n",
2864 (ifp->if_flags & IFF_PROMISC) ? "enabled" : "disabled");
2869 * Return interface configuration
2870 * of system. List may be used
2871 * in later ioctl's (above) to get
2872 * other information.
2876 ifconf(u_long cmd, caddr_t data)
2878 struct ifconf *ifc = (struct ifconf *)data;
2883 int error, full = 0, valid_len, max_len;
2885 /* Limit initial buffer size to MAXPHYS to avoid DoS from userspace. */
2886 max_len = MAXPHYS - 1;
2888 /* Prevent hostile input from being able to crash the system */
2889 if (ifc->ifc_len <= 0)
2893 if (ifc->ifc_len <= max_len) {
2894 max_len = ifc->ifc_len;
2897 sb = sbuf_new(NULL, NULL, max_len + 1, SBUF_FIXEDLEN);
2902 TAILQ_FOREACH(ifp, &V_ifnet, if_link) {
2906 * Zero the ifr_name buffer to make sure we don't
2907 * disclose the contents of the stack.
2909 memset(ifr.ifr_name, 0, sizeof(ifr.ifr_name));
2911 if (strlcpy(ifr.ifr_name, ifp->if_xname, sizeof(ifr.ifr_name))
2912 >= sizeof(ifr.ifr_name)) {
2915 return (ENAMETOOLONG);
2920 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
2921 struct sockaddr *sa = ifa->ifa_addr;
2923 if (prison_if(curthread->td_ucred, sa) != 0)
2926 if (sa->sa_len <= sizeof(*sa)) {
2928 sbuf_bcat(sb, &ifr, sizeof(ifr));
2929 max_len += sizeof(ifr);
2932 offsetof(struct ifreq, ifr_addr));
2933 max_len += offsetof(struct ifreq, ifr_addr);
2934 sbuf_bcat(sb, sa, sa->sa_len);
2935 max_len += sa->sa_len;
2938 if (sbuf_error(sb) == 0)
2939 valid_len = sbuf_len(sb);
2941 IF_ADDR_RUNLOCK(ifp);
2943 bzero((caddr_t)&ifr.ifr_addr, sizeof(ifr.ifr_addr));
2944 sbuf_bcat(sb, &ifr, sizeof(ifr));
2945 max_len += sizeof(ifr);
2947 if (sbuf_error(sb) == 0)
2948 valid_len = sbuf_len(sb);
2954 * If we didn't allocate enough space (uncommon), try again. If
2955 * we have already allocated as much space as we are allowed,
2956 * return what we've got.
2958 if (valid_len != max_len && !full) {
2963 ifc->ifc_len = valid_len;
2965 error = copyout(sbuf_data(sb), ifc->ifc_req, ifc->ifc_len);
2971 * Just like ifpromisc(), but for all-multicast-reception mode.
2974 if_allmulti(struct ifnet *ifp, int onswitch)
2977 return (if_setflag(ifp, IFF_ALLMULTI, 0, &ifp->if_amcount, onswitch));
2980 struct ifmultiaddr *
2981 if_findmulti(struct ifnet *ifp, struct sockaddr *sa)
2983 struct ifmultiaddr *ifma;
2985 IF_ADDR_LOCK_ASSERT(ifp);
2987 TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
2988 if (sa->sa_family == AF_LINK) {
2989 if (sa_dl_equal(ifma->ifma_addr, sa))
2992 if (sa_equal(ifma->ifma_addr, sa))
3001 * Allocate a new ifmultiaddr and initialize based on passed arguments. We
3002 * make copies of passed sockaddrs. The ifmultiaddr will not be added to
3003 * the ifnet multicast address list here, so the caller must do that and
3004 * other setup work (such as notifying the device driver). The reference
3005 * count is initialized to 1.
3007 static struct ifmultiaddr *
3008 if_allocmulti(struct ifnet *ifp, struct sockaddr *sa, struct sockaddr *llsa,
3011 struct ifmultiaddr *ifma;
3012 struct sockaddr *dupsa;
3014 ifma = malloc(sizeof *ifma, M_IFMADDR, mflags |
3019 dupsa = malloc(sa->sa_len, M_IFMADDR, mflags);
3020 if (dupsa == NULL) {
3021 free(ifma, M_IFMADDR);
3024 bcopy(sa, dupsa, sa->sa_len);
3025 ifma->ifma_addr = dupsa;
3027 ifma->ifma_ifp = ifp;
3028 ifma->ifma_refcount = 1;
3029 ifma->ifma_protospec = NULL;
3032 ifma->ifma_lladdr = NULL;
3036 dupsa = malloc(llsa->sa_len, M_IFMADDR, mflags);
3037 if (dupsa == NULL) {
3038 free(ifma->ifma_addr, M_IFMADDR);
3039 free(ifma, M_IFMADDR);
3042 bcopy(llsa, dupsa, llsa->sa_len);
3043 ifma->ifma_lladdr = dupsa;
3049 * if_freemulti: free ifmultiaddr structure and possibly attached related
3050 * addresses. The caller is responsible for implementing reference
3051 * counting, notifying the driver, handling routing messages, and releasing
3052 * any dependent link layer state.
3055 if_freemulti(struct ifmultiaddr *ifma)
3058 KASSERT(ifma->ifma_refcount == 0, ("if_freemulti: refcount %d",
3059 ifma->ifma_refcount));
3061 if (ifma->ifma_lladdr != NULL)
3062 free(ifma->ifma_lladdr, M_IFMADDR);
3063 free(ifma->ifma_addr, M_IFMADDR);
3064 free(ifma, M_IFMADDR);
3068 * Register an additional multicast address with a network interface.
3070 * - If the address is already present, bump the reference count on the
3071 * address and return.
3072 * - If the address is not link-layer, look up a link layer address.
3073 * - Allocate address structures for one or both addresses, and attach to the
3074 * multicast address list on the interface. If automatically adding a link
3075 * layer address, the protocol address will own a reference to the link
3076 * layer address, to be freed when it is freed.
3077 * - Notify the network device driver of an addition to the multicast address
3080 * 'sa' points to caller-owned memory with the desired multicast address.
3082 * 'retifma' will be used to return a pointer to the resulting multicast
3083 * address reference, if desired.
3086 if_addmulti(struct ifnet *ifp, struct sockaddr *sa,
3087 struct ifmultiaddr **retifma)
3089 struct ifmultiaddr *ifma, *ll_ifma;
3090 struct sockaddr *llsa;
3091 struct sockaddr_dl sdl;
3095 * If the address is already present, return a new reference to it;
3096 * otherwise, allocate storage and set up a new address.
3099 ifma = if_findmulti(ifp, sa);
3101 ifma->ifma_refcount++;
3102 if (retifma != NULL)
3104 IF_ADDR_WUNLOCK(ifp);
3109 * The address isn't already present; resolve the protocol address
3110 * into a link layer address, and then look that up, bump its
3111 * refcount or allocate an ifma for that also.
3112 * Most link layer resolving functions returns address data which
3113 * fits inside default sockaddr_dl structure. However callback
3114 * can allocate another sockaddr structure, in that case we need to
3119 if (ifp->if_resolvemulti != NULL) {
3120 /* Provide called function with buffer size information */
3121 sdl.sdl_len = sizeof(sdl);
3122 llsa = (struct sockaddr *)&sdl;
3123 error = ifp->if_resolvemulti(ifp, &llsa, sa);
3129 * Allocate the new address. Don't hook it up yet, as we may also
3130 * need to allocate a link layer multicast address.
3132 ifma = if_allocmulti(ifp, sa, llsa, M_NOWAIT);
3139 * If a link layer address is found, we'll need to see if it's
3140 * already present in the address list, or allocate is as well.
3141 * When this block finishes, the link layer address will be on the
3145 ll_ifma = if_findmulti(ifp, llsa);
3146 if (ll_ifma == NULL) {
3147 ll_ifma = if_allocmulti(ifp, llsa, NULL, M_NOWAIT);
3148 if (ll_ifma == NULL) {
3149 --ifma->ifma_refcount;
3154 TAILQ_INSERT_HEAD(&ifp->if_multiaddrs, ll_ifma,
3157 ll_ifma->ifma_refcount++;
3158 ifma->ifma_llifma = ll_ifma;
3162 * We now have a new multicast address, ifma, and possibly a new or
3163 * referenced link layer address. Add the primary address to the
3164 * ifnet address list.
3166 TAILQ_INSERT_HEAD(&ifp->if_multiaddrs, ifma, ifma_link);
3168 if (retifma != NULL)
3172 * Must generate the message while holding the lock so that 'ifma'
3173 * pointer is still valid.
3175 rt_newmaddrmsg(RTM_NEWMADDR, ifma);
3176 IF_ADDR_WUNLOCK(ifp);
3179 * We are certain we have added something, so call down to the
3180 * interface to let them know about it.
3182 if (ifp->if_ioctl != NULL) {
3183 (void) (*ifp->if_ioctl)(ifp, SIOCADDMULTI, 0);
3186 if ((llsa != NULL) && (llsa != (struct sockaddr *)&sdl))
3187 link_free_sdl(llsa);
3192 if ((llsa != NULL) && (llsa != (struct sockaddr *)&sdl))
3193 link_free_sdl(llsa);
3196 IF_ADDR_WUNLOCK(ifp);
3201 * Delete a multicast group membership by network-layer group address.
3203 * Returns ENOENT if the entry could not be found. If ifp no longer
3204 * exists, results are undefined. This entry point should only be used
3205 * from subsystems which do appropriate locking to hold ifp for the
3206 * duration of the call.
3207 * Network-layer protocol domains must use if_delmulti_ifma().
3210 if_delmulti(struct ifnet *ifp, struct sockaddr *sa)
3212 struct ifmultiaddr *ifma;
3217 IFNET_RLOCK_NOSLEEP();
3218 TAILQ_FOREACH(oifp, &V_ifnet, if_link)
3223 IFNET_RUNLOCK_NOSLEEP();
3225 KASSERT(ifp != NULL, ("%s: ifnet went away", __func__));
3232 ifma = if_findmulti(ifp, sa);
3234 lastref = if_delmulti_locked(ifp, ifma, 0);
3235 IF_ADDR_WUNLOCK(ifp);
3240 if (lastref && ifp->if_ioctl != NULL) {
3241 (void)(*ifp->if_ioctl)(ifp, SIOCDELMULTI, 0);
3248 * Delete all multicast group membership for an interface.
3249 * Should be used to quickly flush all multicast filters.
3252 if_delallmulti(struct ifnet *ifp)
3254 struct ifmultiaddr *ifma;
3255 struct ifmultiaddr *next;
3258 TAILQ_FOREACH_SAFE(ifma, &ifp->if_multiaddrs, ifma_link, next)
3259 if_delmulti_locked(ifp, ifma, 0);
3260 IF_ADDR_WUNLOCK(ifp);
3264 * Delete a multicast group membership by group membership pointer.
3265 * Network-layer protocol domains must use this routine.
3267 * It is safe to call this routine if the ifp disappeared.
3270 if_delmulti_ifma(struct ifmultiaddr *ifma)
3275 ifp = ifma->ifma_ifp;
3278 printf("%s: ifma_ifp seems to be detached\n", __func__);
3282 IFNET_RLOCK_NOSLEEP();
3283 TAILQ_FOREACH(oifp, &V_ifnet, if_link)
3287 printf("%s: ifnet %p disappeared\n", __func__, ifp);
3290 IFNET_RUNLOCK_NOSLEEP();
3294 * If and only if the ifnet instance exists: Acquire the address lock.
3299 lastref = if_delmulti_locked(ifp, ifma, 0);
3303 * If and only if the ifnet instance exists:
3304 * Release the address lock.
3305 * If the group was left: update the hardware hash filter.
3307 IF_ADDR_WUNLOCK(ifp);
3308 if (lastref && ifp->if_ioctl != NULL) {
3309 (void)(*ifp->if_ioctl)(ifp, SIOCDELMULTI, 0);
3315 * Perform deletion of network-layer and/or link-layer multicast address.
3317 * Return 0 if the reference count was decremented.
3318 * Return 1 if the final reference was released, indicating that the
3319 * hardware hash filter should be reprogrammed.
3322 if_delmulti_locked(struct ifnet *ifp, struct ifmultiaddr *ifma, int detaching)
3324 struct ifmultiaddr *ll_ifma;
3326 if (ifp != NULL && ifma->ifma_ifp != NULL) {
3327 KASSERT(ifma->ifma_ifp == ifp,
3328 ("%s: inconsistent ifp %p", __func__, ifp));
3329 IF_ADDR_WLOCK_ASSERT(ifp);
3332 ifp = ifma->ifma_ifp;
3335 * If the ifnet is detaching, null out references to ifnet,
3336 * so that upper protocol layers will notice, and not attempt
3337 * to obtain locks for an ifnet which no longer exists. The
3338 * routing socket announcement must happen before the ifnet
3339 * instance is detached from the system.
3343 printf("%s: detaching ifnet instance %p\n", __func__, ifp);
3346 * ifp may already be nulled out if we are being reentered
3347 * to delete the ll_ifma.
3350 rt_newmaddrmsg(RTM_DELMADDR, ifma);
3351 ifma->ifma_ifp = NULL;
3355 if (--ifma->ifma_refcount > 0)
3359 * If this ifma is a network-layer ifma, a link-layer ifma may
3360 * have been associated with it. Release it first if so.
3362 ll_ifma = ifma->ifma_llifma;
3363 if (ll_ifma != NULL) {
3364 KASSERT(ifma->ifma_lladdr != NULL,
3365 ("%s: llifma w/o lladdr", __func__));
3367 ll_ifma->ifma_ifp = NULL; /* XXX */
3368 if (--ll_ifma->ifma_refcount == 0) {
3370 TAILQ_REMOVE(&ifp->if_multiaddrs, ll_ifma,
3373 if_freemulti(ll_ifma);
3378 TAILQ_REMOVE(&ifp->if_multiaddrs, ifma, ifma_link);
3383 * The last reference to this instance of struct ifmultiaddr
3384 * was released; the hardware should be notified of this change.
3390 * Set the link layer address on an interface.
3392 * At this time we only support certain types of interfaces,
3393 * and we don't allow the length of the address to change.
3396 if_setlladdr(struct ifnet *ifp, const u_char *lladdr, int len)
3398 struct sockaddr_dl *sdl;
3405 IF_ADDR_RUNLOCK(ifp);
3409 IF_ADDR_RUNLOCK(ifp);
3410 sdl = (struct sockaddr_dl *)ifa->ifa_addr;
3415 if (len != sdl->sdl_alen) { /* don't allow length to change */
3419 switch (ifp->if_type) {
3427 case IFT_IEEE8023ADLAG:
3429 bcopy(lladdr, LLADDR(sdl), len);
3438 * If the interface is already up, we need
3439 * to re-init it in order to reprogram its
3442 if ((ifp->if_flags & IFF_UP) != 0) {
3443 if (ifp->if_ioctl) {
3444 ifp->if_flags &= ~IFF_UP;
3445 ifr.ifr_flags = ifp->if_flags & 0xffff;
3446 ifr.ifr_flagshigh = ifp->if_flags >> 16;
3447 (*ifp->if_ioctl)(ifp, SIOCSIFFLAGS, (caddr_t)&ifr);
3448 ifp->if_flags |= IFF_UP;
3449 ifr.ifr_flags = ifp->if_flags & 0xffff;
3450 ifr.ifr_flagshigh = ifp->if_flags >> 16;
3451 (*ifp->if_ioctl)(ifp, SIOCSIFFLAGS, (caddr_t)&ifr);
3455 * Also send gratuitous ARPs to notify other nodes about
3456 * the address change.
3458 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
3459 if (ifa->ifa_addr->sa_family == AF_INET)
3460 arp_ifinit(ifp, ifa);
3468 * The name argument must be a pointer to storage which will last as
3469 * long as the interface does. For physical devices, the result of
3470 * device_get_name(dev) is a good choice and for pseudo-devices a
3471 * static string works well.
3474 if_initname(struct ifnet *ifp, const char *name, int unit)
3476 ifp->if_dname = name;
3477 ifp->if_dunit = unit;
3478 if (unit != IF_DUNIT_NONE)
3479 snprintf(ifp->if_xname, IFNAMSIZ, "%s%d", name, unit);
3481 strlcpy(ifp->if_xname, name, IFNAMSIZ);
3485 if_printf(struct ifnet *ifp, const char * fmt, ...)
3490 retval = printf("%s: ", ifp->if_xname);
3492 retval += vprintf(fmt, ap);
3498 if_start(struct ifnet *ifp)
3501 (*(ifp)->if_start)(ifp);
3505 * Backwards compatibility interface for drivers
3506 * that have not implemented it
3509 if_transmit(struct ifnet *ifp, struct mbuf *m)
3513 IFQ_HANDOFF(ifp, m, error);
3518 if_handoff(struct ifqueue *ifq, struct mbuf *m, struct ifnet *ifp, int adjust)
3523 if (_IF_QFULL(ifq)) {
3530 ifp->if_obytes += m->m_pkthdr.len + adjust;
3531 if (m->m_flags & (M_BCAST|M_MCAST))
3533 active = ifp->if_drv_flags & IFF_DRV_OACTIVE;
3535 _IF_ENQUEUE(ifq, m);
3537 if (ifp != NULL && !active)
3538 (*(ifp)->if_start)(ifp);
3543 if_register_com_alloc(u_char type,
3544 if_com_alloc_t *a, if_com_free_t *f)
3547 KASSERT(if_com_alloc[type] == NULL,
3548 ("if_register_com_alloc: %d already registered", type));
3549 KASSERT(if_com_free[type] == NULL,
3550 ("if_register_com_alloc: %d free already registered", type));
3552 if_com_alloc[type] = a;
3553 if_com_free[type] = f;
3557 if_deregister_com_alloc(u_char type)
3560 KASSERT(if_com_alloc[type] != NULL,
3561 ("if_deregister_com_alloc: %d not registered", type));
3562 KASSERT(if_com_free[type] != NULL,
3563 ("if_deregister_com_alloc: %d free not registered", type));
3564 if_com_alloc[type] = NULL;
3565 if_com_free[type] = NULL;
3568 /* API for driver access to network stack owned ifnet.*/
3570 if_setbaudrate(struct ifnet *ifp, uint64_t baudrate)
3574 oldbrate = ifp->if_baudrate;
3575 ifp->if_baudrate = baudrate;
3580 if_getbaudrate(if_t ifp)
3583 return (((struct ifnet *)ifp)->if_baudrate);
3587 if_setcapabilities(if_t ifp, int capabilities)
3589 ((struct ifnet *)ifp)->if_capabilities = capabilities;
3594 if_setcapabilitiesbit(if_t ifp, int setbit, int clearbit)
3596 ((struct ifnet *)ifp)->if_capabilities |= setbit;
3597 ((struct ifnet *)ifp)->if_capabilities &= ~clearbit;
3603 if_getcapabilities(if_t ifp)
3605 return ((struct ifnet *)ifp)->if_capabilities;
3609 if_setcapenable(if_t ifp, int capabilities)
3611 ((struct ifnet *)ifp)->if_capenable = capabilities;
3616 if_setcapenablebit(if_t ifp, int setcap, int clearcap)
3619 ((struct ifnet *)ifp)->if_capenable |= setcap;
3621 ((struct ifnet *)ifp)->if_capenable &= ~clearcap;
3627 if_getdname(if_t ifp)
3629 return ((struct ifnet *)ifp)->if_dname;
3633 if_togglecapenable(if_t ifp, int togglecap)
3635 ((struct ifnet *)ifp)->if_capenable ^= togglecap;
3640 if_getcapenable(if_t ifp)
3642 return ((struct ifnet *)ifp)->if_capenable;
3646 * This is largely undesirable because it ties ifnet to a device, but does
3647 * provide flexiblity for an embedded product vendor. Should be used with
3648 * the understanding that it violates the interface boundaries, and should be
3649 * a last resort only.
3652 if_setdev(if_t ifp, void *dev)
3658 if_setdrvflagbits(if_t ifp, int set_flags, int clear_flags)
3660 ((struct ifnet *)ifp)->if_drv_flags |= set_flags;
3661 ((struct ifnet *)ifp)->if_drv_flags &= ~clear_flags;
3667 if_getdrvflags(if_t ifp)
3669 return ((struct ifnet *)ifp)->if_drv_flags;
3673 if_setdrvflags(if_t ifp, int flags)
3675 ((struct ifnet *)ifp)->if_drv_flags = flags;
3681 if_setflags(if_t ifp, int flags)
3683 ((struct ifnet *)ifp)->if_flags = flags;
3688 if_setflagbits(if_t ifp, int set, int clear)
3690 ((struct ifnet *)ifp)->if_flags |= set;
3691 ((struct ifnet *)ifp)->if_flags &= ~clear;
3697 if_getflags(if_t ifp)
3699 return ((struct ifnet *)ifp)->if_flags;
3703 if_clearhwassist(if_t ifp)
3705 ((struct ifnet *)ifp)->if_hwassist = 0;
3710 if_sethwassistbits(if_t ifp, int toset, int toclear)
3712 ((struct ifnet *)ifp)->if_hwassist |= toset;
3713 ((struct ifnet *)ifp)->if_hwassist &= ~toclear;
3719 if_sethwassist(if_t ifp, int hwassist_bit)
3721 ((struct ifnet *)ifp)->if_hwassist = hwassist_bit;
3726 if_gethwassist(if_t ifp)
3728 return ((struct ifnet *)ifp)->if_hwassist;
3732 if_setmtu(if_t ifp, int mtu)
3734 ((struct ifnet *)ifp)->if_mtu = mtu;
3741 return ((struct ifnet *)ifp)->if_mtu;
3745 if_setsoftc(if_t ifp, void *softc)
3747 ((struct ifnet *)ifp)->if_softc = softc;
3752 if_getsoftc(if_t ifp)
3754 return ((struct ifnet *)ifp)->if_softc;
3758 if_setrcvif(struct mbuf *m, if_t ifp)
3760 m->m_pkthdr.rcvif = (struct ifnet *)ifp;
3764 if_setvtag(struct mbuf *m, uint16_t tag)
3766 m->m_pkthdr.ether_vtag = tag;
3770 if_getvtag(struct mbuf *m)
3773 return (m->m_pkthdr.ether_vtag);
3777 if_sendq_empty(if_t ifp)
3779 return IFQ_DRV_IS_EMPTY(&((struct ifnet *)ifp)->if_snd);
3783 if_getifaddr(if_t ifp)
3785 return ((struct ifnet *)ifp)->if_addr;
3789 if_getamcount(if_t ifp)
3791 return ((struct ifnet *)ifp)->if_amcount;
3796 if_setsendqready(if_t ifp)
3798 IFQ_SET_READY(&((struct ifnet *)ifp)->if_snd);
3803 if_setsendqlen(if_t ifp, int tx_desc_count)
3805 IFQ_SET_MAXLEN(&((struct ifnet *)ifp)->if_snd, tx_desc_count);
3806 ((struct ifnet *)ifp)->if_snd.ifq_drv_maxlen = tx_desc_count;
3812 if_vlantrunkinuse(if_t ifp)
3814 return ((struct ifnet *)ifp)->if_vlantrunk != NULL?1:0;
3818 if_input(if_t ifp, struct mbuf* sendmp)
3820 (*((struct ifnet *)ifp)->if_input)((struct ifnet *)ifp, sendmp);
3826 #ifndef ETH_ADDR_LEN
3827 #define ETH_ADDR_LEN 6
3831 if_setupmultiaddr(if_t ifp, void *mta, int *cnt, int max)
3833 struct ifmultiaddr *ifma;
3834 uint8_t *lmta = (uint8_t *)mta;
3837 TAILQ_FOREACH(ifma, &((struct ifnet *)ifp)->if_multiaddrs, ifma_link) {
3838 if (ifma->ifma_addr->sa_family != AF_LINK)
3844 bcopy(LLADDR((struct sockaddr_dl *)ifma->ifma_addr),
3845 &lmta[mcnt * ETH_ADDR_LEN], ETH_ADDR_LEN);
3854 if_multiaddr_array(if_t ifp, void *mta, int *cnt, int max)
3858 if_maddr_rlock(ifp);
3859 error = if_setupmultiaddr(ifp, mta, cnt, max);
3860 if_maddr_runlock(ifp);
3865 if_multiaddr_count(if_t ifp, int max)
3867 struct ifmultiaddr *ifma;
3871 if_maddr_rlock(ifp);
3872 TAILQ_FOREACH(ifma, &((struct ifnet *)ifp)->if_multiaddrs, ifma_link) {
3873 if (ifma->ifma_addr->sa_family != AF_LINK)
3879 if_maddr_runlock(ifp);
3884 if_dequeue(if_t ifp)
3887 IFQ_DRV_DEQUEUE(&((struct ifnet *)ifp)->if_snd, m);
3893 if_sendq_prepend(if_t ifp, struct mbuf *m)
3895 IFQ_DRV_PREPEND(&((struct ifnet *)ifp)->if_snd, m);
3900 if_setifheaderlen(if_t ifp, int len)
3902 ((struct ifnet *)ifp)->if_hdrlen = len;
3907 if_getlladdr(if_t ifp)
3909 return (IF_LLADDR((struct ifnet *)ifp));
3913 if_gethandle(u_char type)
3915 return (if_alloc(type));
3919 if_bpfmtap(if_t ifh, struct mbuf *m)
3921 struct ifnet *ifp = (struct ifnet *)ifh;
3927 if_etherbpfmtap(if_t ifh, struct mbuf *m)
3929 struct ifnet *ifp = (struct ifnet *)ifh;
3931 ETHER_BPF_MTAP(ifp, m);
3935 if_vlancap(if_t ifh)
3937 struct ifnet *ifp = (struct ifnet *)ifh;
3938 VLAN_CAPABILITIES(ifp);
3942 if_setinitfn(if_t ifp, void (*init_fn)(void *))
3944 ((struct ifnet *)ifp)->if_init = init_fn;
3948 if_setioctlfn(if_t ifp, int (*ioctl_fn)(if_t, u_long, caddr_t))
3950 ((struct ifnet *)ifp)->if_ioctl = (void *)ioctl_fn;
3954 if_setstartfn(if_t ifp, void (*start_fn)(if_t))
3956 ((struct ifnet *)ifp)->if_start = (void *)start_fn;
3960 if_settransmitfn(if_t ifp, if_transmit_fn_t start_fn)
3962 ((struct ifnet *)ifp)->if_transmit = start_fn;
3965 void if_setqflushfn(if_t ifp, if_qflush_fn_t flush_fn)
3967 ((struct ifnet *)ifp)->if_qflush = flush_fn;
3972 if_setgetcounterfn(if_t ifp, if_get_counter_t fn)
3975 ifp->if_get_counter = fn;
3978 /* Revisit these - These are inline functions originally. */
3980 drbr_inuse_drv(if_t ifh, struct buf_ring *br)
3982 return drbr_inuse_drv(ifh, br);
3986 drbr_dequeue_drv(if_t ifh, struct buf_ring *br)
3988 return drbr_dequeue(ifh, br);
3992 drbr_needs_enqueue_drv(if_t ifh, struct buf_ring *br)
3994 return drbr_needs_enqueue(ifh, br);
3998 drbr_enqueue_drv(if_t ifh, struct buf_ring *br, struct mbuf *m)
4000 return drbr_enqueue(ifh, br, m);