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 for (int i = 0; i < IFCOUNTERS; i++)
471 ifp->if_counters[i] = counter_u64_alloc(M_WAITOK);
472 ifp->if_get_counter = if_get_counter_default;
473 ifnet_setbyindex(ifp->if_index, ifp);
478 * Do the actual work of freeing a struct ifnet, and layer 2 common
479 * structure. This call is made when the last reference to an
480 * interface is released.
483 if_free_internal(struct ifnet *ifp)
486 KASSERT((ifp->if_flags & IFF_DYING),
487 ("if_free_internal: interface not dying"));
489 if (if_com_free[ifp->if_alloctype] != NULL)
490 if_com_free[ifp->if_alloctype](ifp->if_l2com,
494 mac_ifnet_destroy(ifp);
496 if (ifp->if_description != NULL)
497 free(ifp->if_description, M_IFDESCR);
498 IF_AFDATA_DESTROY(ifp);
499 IF_ADDR_LOCK_DESTROY(ifp);
500 ifq_delete(&ifp->if_snd);
502 for (int i = 0; i < IFCOUNTERS; i++)
503 counter_u64_free(ifp->if_counters[i]);
509 * Deregister an interface and free the associated storage.
512 if_free(struct ifnet *ifp)
515 ifp->if_flags |= IFF_DYING; /* XXX: Locking */
517 CURVNET_SET_QUIET(ifp->if_vnet);
519 KASSERT(ifp == ifnet_byindex_locked(ifp->if_index),
520 ("%s: freeing unallocated ifnet", ifp->if_xname));
522 ifindex_free_locked(ifp->if_index);
525 if (refcount_release(&ifp->if_refcount))
526 if_free_internal(ifp);
531 * Interfaces to keep an ifnet type-stable despite the possibility of the
532 * driver calling if_free(). If there are additional references, we defer
533 * freeing the underlying data structure.
536 if_ref(struct ifnet *ifp)
539 /* We don't assert the ifnet list lock here, but arguably should. */
540 refcount_acquire(&ifp->if_refcount);
544 if_rele(struct ifnet *ifp)
547 if (!refcount_release(&ifp->if_refcount))
549 if_free_internal(ifp);
553 ifq_init(struct ifaltq *ifq, struct ifnet *ifp)
556 mtx_init(&ifq->ifq_mtx, ifp->if_xname, "if send queue", MTX_DEF);
558 if (ifq->ifq_maxlen == 0)
559 ifq->ifq_maxlen = ifqmaxlen;
562 ifq->altq_disc = NULL;
563 ifq->altq_flags &= ALTQF_CANTCHANGE;
564 ifq->altq_tbr = NULL;
569 ifq_delete(struct ifaltq *ifq)
571 mtx_destroy(&ifq->ifq_mtx);
575 * Perform generic interface initalization tasks and attach the interface
576 * to the list of "active" interfaces. If vmove flag is set on entry
577 * to if_attach_internal(), perform only a limited subset of initialization
578 * tasks, given that we are moving from one vnet to another an ifnet which
579 * has already been fully initialized.
582 * - The decision to return void and thus require this function to
583 * succeed is questionable.
584 * - We should probably do more sanity checking. For instance we don't
585 * do anything to insure if_xname is unique or non-empty.
588 if_attach(struct ifnet *ifp)
591 if_attach_internal(ifp, 0);
595 * Compute the least common TSO limit.
598 if_hw_tsomax_common(if_t ifp, struct ifnet_hw_tsomax *pmax)
601 * 1) If there is no limit currently, take the limit from
602 * the network adapter.
604 * 2) If the network adapter has a limit below the current
607 if (pmax->tsomaxbytes == 0 || (ifp->if_hw_tsomax != 0 &&
608 ifp->if_hw_tsomax < pmax->tsomaxbytes)) {
609 pmax->tsomaxbytes = ifp->if_hw_tsomax;
611 if (pmax->tsomaxsegcount == 0 || (ifp->if_hw_tsomaxsegcount != 0 &&
612 ifp->if_hw_tsomaxsegcount < pmax->tsomaxsegcount)) {
613 pmax->tsomaxsegcount = ifp->if_hw_tsomaxsegcount;
615 if (pmax->tsomaxsegsize == 0 || (ifp->if_hw_tsomaxsegsize != 0 &&
616 ifp->if_hw_tsomaxsegsize < pmax->tsomaxsegsize)) {
617 pmax->tsomaxsegsize = ifp->if_hw_tsomaxsegsize;
622 * Update TSO limit of a network adapter.
624 * Returns zero if no change. Else non-zero.
627 if_hw_tsomax_update(if_t ifp, struct ifnet_hw_tsomax *pmax)
630 if (ifp->if_hw_tsomax != pmax->tsomaxbytes) {
631 ifp->if_hw_tsomax = pmax->tsomaxbytes;
634 if (ifp->if_hw_tsomaxsegsize != pmax->tsomaxsegsize) {
635 ifp->if_hw_tsomaxsegsize = pmax->tsomaxsegsize;
638 if (ifp->if_hw_tsomaxsegcount != pmax->tsomaxsegcount) {
639 ifp->if_hw_tsomaxsegcount = pmax->tsomaxsegcount;
646 if_attach_internal(struct ifnet *ifp, int vmove)
648 unsigned socksize, ifasize;
649 int namelen, masklen;
650 struct sockaddr_dl *sdl;
653 if (ifp->if_index == 0 || ifp != ifnet_byindex(ifp->if_index))
654 panic ("%s: BUG: if_attach called without if_alloc'd input()\n",
658 ifp->if_vnet = curvnet;
659 if (ifp->if_home_vnet == NULL)
660 ifp->if_home_vnet = curvnet;
663 if_addgroup(ifp, IFG_ALL);
665 getmicrotime(&ifp->if_lastchange);
666 ifp->if_epoch = time_uptime;
668 KASSERT((ifp->if_transmit == NULL && ifp->if_qflush == NULL) ||
669 (ifp->if_transmit != NULL && ifp->if_qflush != NULL),
670 ("transmit and qflush must both either be set or both be NULL"));
671 if (ifp->if_transmit == NULL) {
672 ifp->if_transmit = if_transmit;
673 ifp->if_qflush = if_qflush;
678 mac_ifnet_create(ifp);
682 * Create a Link Level name for this device.
684 namelen = strlen(ifp->if_xname);
686 * Always save enough space for any possiable name so we
687 * can do a rename in place later.
689 masklen = offsetof(struct sockaddr_dl, sdl_data[0]) + IFNAMSIZ;
690 socksize = masklen + ifp->if_addrlen;
691 if (socksize < sizeof(*sdl))
692 socksize = sizeof(*sdl);
693 socksize = roundup2(socksize, sizeof(long));
694 ifasize = sizeof(*ifa) + 2 * socksize;
695 ifa = ifa_alloc(ifasize, M_WAITOK);
696 sdl = (struct sockaddr_dl *)(ifa + 1);
697 sdl->sdl_len = socksize;
698 sdl->sdl_family = AF_LINK;
699 bcopy(ifp->if_xname, sdl->sdl_data, namelen);
700 sdl->sdl_nlen = namelen;
701 sdl->sdl_index = ifp->if_index;
702 sdl->sdl_type = ifp->if_type;
705 ifa->ifa_rtrequest = link_rtrequest;
706 ifa->ifa_addr = (struct sockaddr *)sdl;
707 sdl = (struct sockaddr_dl *)(socksize + (caddr_t)sdl);
708 ifa->ifa_netmask = (struct sockaddr *)sdl;
709 sdl->sdl_len = masklen;
711 sdl->sdl_data[--namelen] = 0xff;
712 TAILQ_INSERT_HEAD(&ifp->if_addrhead, ifa, ifa_link);
713 /* Reliably crash if used uninitialized. */
714 ifp->if_broadcastaddr = NULL;
716 #if defined(INET) || defined(INET6)
717 /* Use defaults for TSO, if nothing is set */
718 if (ifp->if_hw_tsomax == 0 &&
719 ifp->if_hw_tsomaxsegcount == 0 &&
720 ifp->if_hw_tsomaxsegsize == 0) {
722 * The TSO defaults needs to be such that an
723 * NFS mbuf list of 35 mbufs totalling just
724 * below 64K works and that a chain of mbufs
725 * can be defragged into at most 32 segments:
727 ifp->if_hw_tsomax = min(IP_MAXPACKET, (32 * MCLBYTES) -
728 (ETHER_HDR_LEN + ETHER_VLAN_ENCAP_LEN));
729 ifp->if_hw_tsomaxsegcount = 35;
730 ifp->if_hw_tsomaxsegsize = 2048; /* 2K */
732 /* XXX some drivers set IFCAP_TSO after ethernet attach */
733 if (ifp->if_capabilities & IFCAP_TSO) {
734 if_printf(ifp, "Using defaults for TSO: %u/%u/%u\n",
736 ifp->if_hw_tsomaxsegcount,
737 ifp->if_hw_tsomaxsegsize);
741 * If the "if_hw_tsomax" limit is set, check if it is
744 KASSERT(ifp->if_hw_tsomax == 0 ||
745 ifp->if_hw_tsomax >= (IP_MAXPACKET / 8),
746 ("%s: if_hw_tsomax is outside of range", __func__));
752 * Update the interface index in the link layer address
755 for (ifa = ifp->if_addr; ifa != NULL;
756 ifa = TAILQ_NEXT(ifa, ifa_link)) {
757 if (ifa->ifa_addr->sa_family == AF_LINK) {
758 sdl = (struct sockaddr_dl *)ifa->ifa_addr;
759 sdl->sdl_index = ifp->if_index;
766 TAILQ_INSERT_TAIL(&V_ifnet, ifp, if_link);
768 curvnet->vnet_ifcnt++;
772 if (domain_init_status >= 2)
773 if_attachdomain1(ifp);
775 EVENTHANDLER_INVOKE(ifnet_arrival_event, ifp);
776 if (IS_DEFAULT_VNET(curvnet))
777 devctl_notify("IFNET", ifp->if_xname, "ATTACH", NULL);
779 /* Announce the interface. */
780 rt_ifannouncemsg(ifp, IFAN_ARRIVAL);
784 if_attachdomain(void *dummy)
788 TAILQ_FOREACH(ifp, &V_ifnet, if_link)
789 if_attachdomain1(ifp);
791 SYSINIT(domainifattach, SI_SUB_PROTO_IFATTACHDOMAIN, SI_ORDER_SECOND,
792 if_attachdomain, NULL);
795 if_attachdomain1(struct ifnet *ifp)
800 * Since dp->dom_ifattach calls malloc() with M_WAITOK, we
801 * cannot lock ifp->if_afdata initialization, entirely.
803 if (IF_AFDATA_TRYLOCK(ifp) == 0)
805 if (ifp->if_afdata_initialized >= domain_init_status) {
806 IF_AFDATA_UNLOCK(ifp);
807 log(LOG_WARNING, "%s called more than once on %s\n",
808 __func__, ifp->if_xname);
811 ifp->if_afdata_initialized = domain_init_status;
812 IF_AFDATA_UNLOCK(ifp);
814 /* address family dependent data region */
815 bzero(ifp->if_afdata, sizeof(ifp->if_afdata));
816 for (dp = domains; dp; dp = dp->dom_next) {
817 if (dp->dom_ifattach)
818 ifp->if_afdata[dp->dom_family] =
819 (*dp->dom_ifattach)(ifp);
824 * Remove any unicast or broadcast network addresses from an interface.
827 if_purgeaddrs(struct ifnet *ifp)
829 struct ifaddr *ifa, *next;
831 TAILQ_FOREACH_SAFE(ifa, &ifp->if_addrhead, ifa_link, next) {
832 if (ifa->ifa_addr->sa_family == AF_LINK)
835 /* XXX: Ugly!! ad hoc just for INET */
836 if (ifa->ifa_addr->sa_family == AF_INET) {
837 struct ifaliasreq ifr;
839 bzero(&ifr, sizeof(ifr));
840 ifr.ifra_addr = *ifa->ifa_addr;
841 if (ifa->ifa_dstaddr)
842 ifr.ifra_broadaddr = *ifa->ifa_dstaddr;
843 if (in_control(NULL, SIOCDIFADDR, (caddr_t)&ifr, ifp,
849 if (ifa->ifa_addr->sa_family == AF_INET6) {
851 /* ifp_addrhead is already updated */
855 TAILQ_REMOVE(&ifp->if_addrhead, ifa, ifa_link);
861 * Remove any multicast network addresses from an interface when an ifnet
865 if_purgemaddrs(struct ifnet *ifp)
867 struct ifmultiaddr *ifma;
868 struct ifmultiaddr *next;
871 TAILQ_FOREACH_SAFE(ifma, &ifp->if_multiaddrs, ifma_link, next)
872 if_delmulti_locked(ifp, ifma, 1);
873 IF_ADDR_WUNLOCK(ifp);
877 * Detach an interface, removing it from the list of "active" interfaces.
878 * If vmove flag is set on entry to if_detach_internal(), perform only a
879 * limited subset of cleanup tasks, given that we are moving an ifnet from
880 * one vnet to another, where it must be fully operational.
882 * XXXRW: There are some significant questions about event ordering, and
883 * how to prevent things from starting to use the interface during detach.
886 if_detach(struct ifnet *ifp)
889 CURVNET_SET_QUIET(ifp->if_vnet);
890 if_detach_internal(ifp, 0);
895 if_detach_internal(struct ifnet *ifp, int vmove)
898 struct radix_node_head *rnh;
905 TAILQ_FOREACH(iter, &V_ifnet, if_link)
907 TAILQ_REMOVE(&V_ifnet, ifp, if_link);
913 curvnet->vnet_ifcnt--;
918 panic("%s: ifp=%p not on the ifnet tailq %p",
919 __func__, ifp, &V_ifnet);
921 return; /* XXX this should panic as well? */
925 * Remove/wait for pending events.
927 taskqueue_drain(taskqueue_swi, &ifp->if_linktask);
930 * Remove routes and flush queues.
934 if (ALTQ_IS_ENABLED(&ifp->if_snd))
935 altq_disable(&ifp->if_snd);
936 if (ALTQ_IS_ATTACHED(&ifp->if_snd))
937 altq_detach(&ifp->if_snd);
948 * Remove all IPv6 kernel structs related to ifp. This should be done
949 * before removing routing entries below, since IPv6 interface direct
950 * routes are expected to be removed by the IPv6-specific kernel API.
951 * Otherwise, the kernel will detect some inconsistency and bark it.
957 /* Announce that the interface is gone. */
958 rt_ifannouncemsg(ifp, IFAN_DEPARTURE);
959 EVENTHANDLER_INVOKE(ifnet_departure_event, ifp);
960 if (IS_DEFAULT_VNET(curvnet))
961 devctl_notify("IFNET", ifp->if_xname, "DETACH", NULL);
965 * Prevent further calls into the device driver via ifnet.
970 * Remove link ifaddr pointer and maybe decrement if_index.
971 * Clean up all addresses.
975 /* We can now free link ifaddr. */
976 if (!TAILQ_EMPTY(&ifp->if_addrhead)) {
977 ifa = TAILQ_FIRST(&ifp->if_addrhead);
978 TAILQ_REMOVE(&ifp->if_addrhead, ifa, ifa_link);
984 * Delete all remaining routes using this interface
985 * Unfortuneatly the only way to do this is to slog through
986 * the entire routing table looking for routes which point
987 * to this interface...oh well...
989 for (i = 1; i <= AF_MAX; i++) {
990 for (j = 0; j < rt_numfibs; j++) {
991 rnh = rt_tables_get_rnh(j, i);
994 RADIX_NODE_HEAD_LOCK(rnh);
995 (void) rnh->rnh_walktree(rnh, if_rtdel, ifp);
996 RADIX_NODE_HEAD_UNLOCK(rnh);
1003 * We cannot hold the lock over dom_ifdetach calls as they might
1004 * sleep, for example trying to drain a callout, thus open up the
1005 * theoretical race with re-attaching.
1007 IF_AFDATA_LOCK(ifp);
1008 i = ifp->if_afdata_initialized;
1009 ifp->if_afdata_initialized = 0;
1010 IF_AFDATA_UNLOCK(ifp);
1011 for (dp = domains; i > 0 && dp; dp = dp->dom_next) {
1012 if (dp->dom_ifdetach && ifp->if_afdata[dp->dom_family])
1013 (*dp->dom_ifdetach)(ifp,
1014 ifp->if_afdata[dp->dom_family]);
1020 * if_vmove() performs a limited version of if_detach() in current
1021 * vnet and if_attach()es the ifnet to the vnet specified as 2nd arg.
1022 * An attempt is made to shrink if_index in current vnet, find an
1023 * unused if_index in target vnet and calls if_grow() if necessary,
1024 * and finally find an unused if_xname for the target vnet.
1027 if_vmove(struct ifnet *ifp, struct vnet *new_vnet)
1032 * Detach from current vnet, but preserve LLADDR info, do not
1033 * mark as dead etc. so that the ifnet can be reattached later.
1035 if_detach_internal(ifp, 1);
1038 * Unlink the ifnet from ifindex_table[] in current vnet, and shrink
1039 * the if_index for that vnet if possible.
1041 * NOTE: IFNET_WLOCK/IFNET_WUNLOCK() are assumed to be unvirtualized,
1042 * or we'd lock on one vnet and unlock on another.
1045 ifindex_free_locked(ifp->if_index);
1049 * Perform interface-specific reassignment tasks, if provided by
1052 if (ifp->if_reassign != NULL)
1053 ifp->if_reassign(ifp, new_vnet, NULL);
1056 * Switch to the context of the target vnet.
1058 CURVNET_SET_QUIET(new_vnet);
1061 if (ifindex_alloc_locked(&idx) != 0) {
1063 panic("if_index overflow");
1065 ifp->if_index = idx;
1066 ifnet_setbyindex_locked(ifp->if_index, ifp);
1069 if_attach_internal(ifp, 1);
1075 * Move an ifnet to or from another child prison/vnet, specified by the jail id.
1078 if_vmove_loan(struct thread *td, struct ifnet *ifp, char *ifname, int jid)
1083 /* Try to find the prison within our visibility. */
1084 sx_slock(&allprison_lock);
1085 pr = prison_find_child(td->td_ucred->cr_prison, jid);
1086 sx_sunlock(&allprison_lock);
1089 prison_hold_locked(pr);
1090 mtx_unlock(&pr->pr_mtx);
1092 /* Do not try to move the iface from and to the same prison. */
1093 if (pr->pr_vnet == ifp->if_vnet) {
1098 /* Make sure the named iface does not exists in the dst. prison/vnet. */
1099 /* XXX Lock interfaces to avoid races. */
1100 CURVNET_SET_QUIET(pr->pr_vnet);
1101 difp = ifunit(ifname);
1108 /* Move the interface into the child jail/vnet. */
1109 if_vmove(ifp, pr->pr_vnet);
1111 /* Report the new if_xname back to the userland. */
1112 sprintf(ifname, "%s", ifp->if_xname);
1119 if_vmove_reclaim(struct thread *td, char *ifname, int jid)
1122 struct vnet *vnet_dst;
1125 /* Try to find the prison within our visibility. */
1126 sx_slock(&allprison_lock);
1127 pr = prison_find_child(td->td_ucred->cr_prison, jid);
1128 sx_sunlock(&allprison_lock);
1131 prison_hold_locked(pr);
1132 mtx_unlock(&pr->pr_mtx);
1134 /* Make sure the named iface exists in the source prison/vnet. */
1135 CURVNET_SET(pr->pr_vnet);
1136 ifp = ifunit(ifname); /* XXX Lock to avoid races. */
1143 /* Do not try to move the iface from and to the same prison. */
1144 vnet_dst = TD_TO_VNET(td);
1145 if (vnet_dst == ifp->if_vnet) {
1151 /* Get interface back from child jail/vnet. */
1152 if_vmove(ifp, vnet_dst);
1155 /* Report the new if_xname back to the userland. */
1156 sprintf(ifname, "%s", ifp->if_xname);
1164 * Add a group to an interface
1167 if_addgroup(struct ifnet *ifp, const char *groupname)
1169 struct ifg_list *ifgl;
1170 struct ifg_group *ifg = NULL;
1171 struct ifg_member *ifgm;
1174 if (groupname[0] && groupname[strlen(groupname) - 1] >= '0' &&
1175 groupname[strlen(groupname) - 1] <= '9')
1179 TAILQ_FOREACH(ifgl, &ifp->if_groups, ifgl_next)
1180 if (!strcmp(ifgl->ifgl_group->ifg_group, groupname)) {
1185 if ((ifgl = (struct ifg_list *)malloc(sizeof(struct ifg_list), M_TEMP,
1186 M_NOWAIT)) == NULL) {
1191 if ((ifgm = (struct ifg_member *)malloc(sizeof(struct ifg_member),
1192 M_TEMP, M_NOWAIT)) == NULL) {
1198 TAILQ_FOREACH(ifg, &V_ifg_head, ifg_next)
1199 if (!strcmp(ifg->ifg_group, groupname))
1203 if ((ifg = (struct ifg_group *)malloc(sizeof(struct ifg_group),
1204 M_TEMP, M_NOWAIT)) == NULL) {
1210 strlcpy(ifg->ifg_group, groupname, sizeof(ifg->ifg_group));
1211 ifg->ifg_refcnt = 0;
1212 TAILQ_INIT(&ifg->ifg_members);
1213 TAILQ_INSERT_TAIL(&V_ifg_head, ifg, ifg_next);
1218 ifgl->ifgl_group = ifg;
1219 ifgm->ifgm_ifp = ifp;
1222 TAILQ_INSERT_TAIL(&ifg->ifg_members, ifgm, ifgm_next);
1223 TAILQ_INSERT_TAIL(&ifp->if_groups, ifgl, ifgl_next);
1224 IF_ADDR_WUNLOCK(ifp);
1229 EVENTHANDLER_INVOKE(group_attach_event, ifg);
1230 EVENTHANDLER_INVOKE(group_change_event, groupname);
1236 * Remove a group from an interface
1239 if_delgroup(struct ifnet *ifp, const char *groupname)
1241 struct ifg_list *ifgl;
1242 struct ifg_member *ifgm;
1245 TAILQ_FOREACH(ifgl, &ifp->if_groups, ifgl_next)
1246 if (!strcmp(ifgl->ifgl_group->ifg_group, groupname))
1254 TAILQ_REMOVE(&ifp->if_groups, ifgl, ifgl_next);
1255 IF_ADDR_WUNLOCK(ifp);
1257 TAILQ_FOREACH(ifgm, &ifgl->ifgl_group->ifg_members, ifgm_next)
1258 if (ifgm->ifgm_ifp == ifp)
1262 TAILQ_REMOVE(&ifgl->ifgl_group->ifg_members, ifgm, ifgm_next);
1266 if (--ifgl->ifgl_group->ifg_refcnt == 0) {
1267 TAILQ_REMOVE(&V_ifg_head, ifgl->ifgl_group, ifg_next);
1269 EVENTHANDLER_INVOKE(group_detach_event, ifgl->ifgl_group);
1270 free(ifgl->ifgl_group, M_TEMP);
1276 EVENTHANDLER_INVOKE(group_change_event, groupname);
1282 * Remove an interface from all groups
1285 if_delgroups(struct ifnet *ifp)
1287 struct ifg_list *ifgl;
1288 struct ifg_member *ifgm;
1289 char groupname[IFNAMSIZ];
1292 while (!TAILQ_EMPTY(&ifp->if_groups)) {
1293 ifgl = TAILQ_FIRST(&ifp->if_groups);
1295 strlcpy(groupname, ifgl->ifgl_group->ifg_group, IFNAMSIZ);
1298 TAILQ_REMOVE(&ifp->if_groups, ifgl, ifgl_next);
1299 IF_ADDR_WUNLOCK(ifp);
1301 TAILQ_FOREACH(ifgm, &ifgl->ifgl_group->ifg_members, ifgm_next)
1302 if (ifgm->ifgm_ifp == ifp)
1306 TAILQ_REMOVE(&ifgl->ifgl_group->ifg_members, ifgm,
1311 if (--ifgl->ifgl_group->ifg_refcnt == 0) {
1312 TAILQ_REMOVE(&V_ifg_head, ifgl->ifgl_group, ifg_next);
1314 EVENTHANDLER_INVOKE(group_detach_event,
1316 free(ifgl->ifgl_group, M_TEMP);
1322 EVENTHANDLER_INVOKE(group_change_event, groupname);
1330 * Stores all groups from an interface in memory pointed
1334 if_getgroup(struct ifgroupreq *data, struct ifnet *ifp)
1337 struct ifg_list *ifgl;
1338 struct ifg_req ifgrq, *ifgp;
1339 struct ifgroupreq *ifgr = data;
1341 if (ifgr->ifgr_len == 0) {
1343 TAILQ_FOREACH(ifgl, &ifp->if_groups, ifgl_next)
1344 ifgr->ifgr_len += sizeof(struct ifg_req);
1345 IF_ADDR_RUNLOCK(ifp);
1349 len = ifgr->ifgr_len;
1350 ifgp = ifgr->ifgr_groups;
1353 TAILQ_FOREACH(ifgl, &ifp->if_groups, ifgl_next) {
1354 if (len < sizeof(ifgrq)) {
1355 IF_ADDR_RUNLOCK(ifp);
1358 bzero(&ifgrq, sizeof ifgrq);
1359 strlcpy(ifgrq.ifgrq_group, ifgl->ifgl_group->ifg_group,
1360 sizeof(ifgrq.ifgrq_group));
1361 if ((error = copyout(&ifgrq, ifgp, sizeof(struct ifg_req)))) {
1362 IF_ADDR_RUNLOCK(ifp);
1365 len -= sizeof(ifgrq);
1368 IF_ADDR_RUNLOCK(ifp);
1374 * Stores all members of a group in memory pointed to by data
1377 if_getgroupmembers(struct ifgroupreq *data)
1379 struct ifgroupreq *ifgr = data;
1380 struct ifg_group *ifg;
1381 struct ifg_member *ifgm;
1382 struct ifg_req ifgrq, *ifgp;
1386 TAILQ_FOREACH(ifg, &V_ifg_head, ifg_next)
1387 if (!strcmp(ifg->ifg_group, ifgr->ifgr_name))
1394 if (ifgr->ifgr_len == 0) {
1395 TAILQ_FOREACH(ifgm, &ifg->ifg_members, ifgm_next)
1396 ifgr->ifgr_len += sizeof(ifgrq);
1401 len = ifgr->ifgr_len;
1402 ifgp = ifgr->ifgr_groups;
1403 TAILQ_FOREACH(ifgm, &ifg->ifg_members, ifgm_next) {
1404 if (len < sizeof(ifgrq)) {
1408 bzero(&ifgrq, sizeof ifgrq);
1409 strlcpy(ifgrq.ifgrq_member, ifgm->ifgm_ifp->if_xname,
1410 sizeof(ifgrq.ifgrq_member));
1411 if ((error = copyout(&ifgrq, ifgp, sizeof(struct ifg_req)))) {
1415 len -= sizeof(ifgrq);
1424 * Delete Routes for a Network Interface
1426 * Called for each routing entry via the rnh->rnh_walktree() call above
1427 * to delete all route entries referencing a detaching network interface.
1430 * rn pointer to node in the routing table
1431 * arg argument passed to rnh->rnh_walktree() - detaching interface
1435 * errno failed - reason indicated
1439 if_rtdel(struct radix_node *rn, void *arg)
1441 struct rtentry *rt = (struct rtentry *)rn;
1442 struct ifnet *ifp = arg;
1445 if (rt->rt_ifp == ifp) {
1448 * Protect (sorta) against walktree recursion problems
1449 * with cloned routes
1451 if ((rt->rt_flags & RTF_UP) == 0)
1454 err = rtrequest_fib(RTM_DELETE, rt_key(rt), rt->rt_gateway,
1456 rt->rt_flags|RTF_RNH_LOCKED|RTF_PINNED,
1457 (struct rtentry **) NULL, rt->rt_fibnum);
1459 log(LOG_WARNING, "if_rtdel: error %d\n", err);
1467 * Return counter values from counter(9)s stored in ifnet.
1470 if_get_counter_default(struct ifnet *ifp, ift_counter cnt)
1473 KASSERT(cnt < IFCOUNTERS, ("%s: invalid cnt %d", __func__, cnt));
1475 return (counter_u64_fetch(ifp->if_counters[cnt]));
1479 * Increase an ifnet counter. Usually used for counters shared
1480 * between the stack and a driver, but function supports them all.
1483 if_inc_counter(struct ifnet *ifp, ift_counter cnt, int64_t inc)
1486 KASSERT(cnt < IFCOUNTERS, ("%s: invalid cnt %d", __func__, cnt));
1488 counter_u64_add(ifp->if_counters[cnt], inc);
1492 * Copy data from ifnet to userland API structure if_data.
1495 if_data_copy(struct ifnet *ifp, struct if_data *ifd)
1498 ifd->ifi_type = ifp->if_type;
1499 ifd->ifi_physical = 0;
1500 ifd->ifi_addrlen = ifp->if_addrlen;
1501 ifd->ifi_hdrlen = ifp->if_hdrlen;
1502 ifd->ifi_link_state = ifp->if_link_state;
1504 ifd->ifi_datalen = sizeof(struct if_data);
1505 ifd->ifi_mtu = ifp->if_mtu;
1506 ifd->ifi_metric = ifp->if_metric;
1507 ifd->ifi_baudrate = ifp->if_baudrate;
1508 ifd->ifi_hwassist = ifp->if_hwassist;
1509 ifd->ifi_epoch = ifp->if_epoch;
1510 ifd->ifi_lastchange = ifp->if_lastchange;
1512 ifd->ifi_ipackets = ifp->if_get_counter(ifp, IFCOUNTER_IPACKETS);
1513 ifd->ifi_ierrors = ifp->if_get_counter(ifp, IFCOUNTER_IERRORS);
1514 ifd->ifi_opackets = ifp->if_get_counter(ifp, IFCOUNTER_OPACKETS);
1515 ifd->ifi_oerrors = ifp->if_get_counter(ifp, IFCOUNTER_OERRORS);
1516 ifd->ifi_collisions = ifp->if_get_counter(ifp, IFCOUNTER_COLLISIONS);
1517 ifd->ifi_ibytes = ifp->if_get_counter(ifp, IFCOUNTER_IBYTES);
1518 ifd->ifi_obytes = ifp->if_get_counter(ifp, IFCOUNTER_OBYTES);
1519 ifd->ifi_imcasts = ifp->if_get_counter(ifp, IFCOUNTER_IMCASTS);
1520 ifd->ifi_omcasts = ifp->if_get_counter(ifp, IFCOUNTER_OMCASTS);
1521 ifd->ifi_iqdrops = ifp->if_get_counter(ifp, IFCOUNTER_IQDROPS);
1522 ifd->ifi_oqdrops = ifp->if_get_counter(ifp, IFCOUNTER_OQDROPS);
1523 ifd->ifi_noproto = ifp->if_get_counter(ifp, IFCOUNTER_NOPROTO);
1527 * Wrapper functions for struct ifnet address list locking macros. These are
1528 * used by kernel modules to avoid encoding programming interface or binary
1529 * interface assumptions that may be violated when kernel-internal locking
1530 * approaches change.
1533 if_addr_rlock(struct ifnet *ifp)
1540 if_addr_runlock(struct ifnet *ifp)
1543 IF_ADDR_RUNLOCK(ifp);
1547 if_maddr_rlock(if_t ifp)
1550 IF_ADDR_RLOCK((struct ifnet *)ifp);
1554 if_maddr_runlock(if_t ifp)
1557 IF_ADDR_RUNLOCK((struct ifnet *)ifp);
1561 * Initialization, destruction and refcounting functions for ifaddrs.
1564 ifa_alloc(size_t size, int flags)
1568 KASSERT(size >= sizeof(struct ifaddr),
1569 ("%s: invalid size %zu", __func__, size));
1571 ifa = malloc(size, M_IFADDR, M_ZERO | flags);
1575 if ((ifa->ifa_opackets = counter_u64_alloc(flags)) == NULL)
1577 if ((ifa->ifa_ipackets = counter_u64_alloc(flags)) == NULL)
1579 if ((ifa->ifa_obytes = counter_u64_alloc(flags)) == NULL)
1581 if ((ifa->ifa_ibytes = counter_u64_alloc(flags)) == NULL)
1584 refcount_init(&ifa->ifa_refcnt, 1);
1589 /* free(NULL) is okay */
1590 counter_u64_free(ifa->ifa_opackets);
1591 counter_u64_free(ifa->ifa_ipackets);
1592 counter_u64_free(ifa->ifa_obytes);
1593 counter_u64_free(ifa->ifa_ibytes);
1594 free(ifa, M_IFADDR);
1600 ifa_ref(struct ifaddr *ifa)
1603 refcount_acquire(&ifa->ifa_refcnt);
1607 ifa_free(struct ifaddr *ifa)
1610 if (refcount_release(&ifa->ifa_refcnt)) {
1611 counter_u64_free(ifa->ifa_opackets);
1612 counter_u64_free(ifa->ifa_ipackets);
1613 counter_u64_free(ifa->ifa_obytes);
1614 counter_u64_free(ifa->ifa_ibytes);
1615 free(ifa, M_IFADDR);
1620 ifa_add_loopback_route(struct ifaddr *ifa, struct sockaddr *ia)
1623 struct rtentry *rt = NULL;
1624 struct rt_addrinfo info;
1625 static struct sockaddr_dl null_sdl = {sizeof(null_sdl), AF_LINK};
1627 bzero(&info, sizeof(info));
1628 info.rti_ifp = V_loif;
1629 info.rti_flags = ifa->ifa_flags | RTF_HOST | RTF_STATIC;
1630 info.rti_info[RTAX_DST] = ia;
1631 info.rti_info[RTAX_GATEWAY] = (struct sockaddr *)&null_sdl;
1632 error = rtrequest1_fib(RTM_ADD, &info, &rt, ifa->ifa_ifp->if_fib);
1634 if (error == 0 && rt != NULL) {
1636 ((struct sockaddr_dl *)rt->rt_gateway)->sdl_type =
1637 ifa->ifa_ifp->if_type;
1638 ((struct sockaddr_dl *)rt->rt_gateway)->sdl_index =
1639 ifa->ifa_ifp->if_index;
1642 } else if (error != 0)
1643 log(LOG_DEBUG, "%s: insertion failed: %u\n", __func__, error);
1649 ifa_del_loopback_route(struct ifaddr *ifa, struct sockaddr *ia)
1652 struct rt_addrinfo info;
1653 struct sockaddr_dl null_sdl;
1655 bzero(&null_sdl, sizeof(null_sdl));
1656 null_sdl.sdl_len = sizeof(null_sdl);
1657 null_sdl.sdl_family = AF_LINK;
1658 null_sdl.sdl_type = ifa->ifa_ifp->if_type;
1659 null_sdl.sdl_index = ifa->ifa_ifp->if_index;
1660 bzero(&info, sizeof(info));
1661 info.rti_flags = ifa->ifa_flags | RTF_HOST | RTF_STATIC;
1662 info.rti_info[RTAX_DST] = ia;
1663 info.rti_info[RTAX_GATEWAY] = (struct sockaddr *)&null_sdl;
1664 error = rtrequest1_fib(RTM_DELETE, &info, NULL, ifa->ifa_ifp->if_fib);
1667 log(LOG_DEBUG, "%s: deletion failed: %u\n", __func__, error);
1673 ifa_switch_loopback_route(struct ifaddr *ifa, struct sockaddr *sa, int fib)
1677 rt = rtalloc1_fib(sa, 0, 0, fib);
1679 log(LOG_DEBUG, "%s: fail", __func__);
1680 return (EHOSTUNREACH);
1682 ((struct sockaddr_dl *)rt->rt_gateway)->sdl_type =
1683 ifa->ifa_ifp->if_type;
1684 ((struct sockaddr_dl *)rt->rt_gateway)->sdl_index =
1685 ifa->ifa_ifp->if_index;
1692 * XXX: Because sockaddr_dl has deeper structure than the sockaddr
1693 * structs used to represent other address families, it is necessary
1694 * to perform a different comparison.
1697 #define sa_dl_equal(a1, a2) \
1698 ((((struct sockaddr_dl *)(a1))->sdl_len == \
1699 ((struct sockaddr_dl *)(a2))->sdl_len) && \
1700 (bcmp(LLADDR((struct sockaddr_dl *)(a1)), \
1701 LLADDR((struct sockaddr_dl *)(a2)), \
1702 ((struct sockaddr_dl *)(a1))->sdl_alen) == 0))
1705 * Locate an interface based on a complete address.
1708 static struct ifaddr *
1709 ifa_ifwithaddr_internal(struct sockaddr *addr, int getref)
1714 IFNET_RLOCK_NOSLEEP();
1715 TAILQ_FOREACH(ifp, &V_ifnet, if_link) {
1717 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
1718 if (ifa->ifa_addr->sa_family != addr->sa_family)
1720 if (sa_equal(addr, ifa->ifa_addr)) {
1723 IF_ADDR_RUNLOCK(ifp);
1726 /* IP6 doesn't have broadcast */
1727 if ((ifp->if_flags & IFF_BROADCAST) &&
1728 ifa->ifa_broadaddr &&
1729 ifa->ifa_broadaddr->sa_len != 0 &&
1730 sa_equal(ifa->ifa_broadaddr, addr)) {
1733 IF_ADDR_RUNLOCK(ifp);
1737 IF_ADDR_RUNLOCK(ifp);
1741 IFNET_RUNLOCK_NOSLEEP();
1746 ifa_ifwithaddr(struct sockaddr *addr)
1749 return (ifa_ifwithaddr_internal(addr, 1));
1753 ifa_ifwithaddr_check(struct sockaddr *addr)
1756 return (ifa_ifwithaddr_internal(addr, 0) != NULL);
1760 * Locate an interface based on the broadcast address.
1764 ifa_ifwithbroadaddr(struct sockaddr *addr, int fibnum)
1769 IFNET_RLOCK_NOSLEEP();
1770 TAILQ_FOREACH(ifp, &V_ifnet, if_link) {
1771 if ((fibnum != RT_ALL_FIBS) && (ifp->if_fib != fibnum))
1774 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
1775 if (ifa->ifa_addr->sa_family != addr->sa_family)
1777 if ((ifp->if_flags & IFF_BROADCAST) &&
1778 ifa->ifa_broadaddr &&
1779 ifa->ifa_broadaddr->sa_len != 0 &&
1780 sa_equal(ifa->ifa_broadaddr, addr)) {
1782 IF_ADDR_RUNLOCK(ifp);
1786 IF_ADDR_RUNLOCK(ifp);
1790 IFNET_RUNLOCK_NOSLEEP();
1795 * Locate the point to point interface with a given destination address.
1799 ifa_ifwithdstaddr(struct sockaddr *addr, int fibnum)
1804 IFNET_RLOCK_NOSLEEP();
1805 TAILQ_FOREACH(ifp, &V_ifnet, if_link) {
1806 if ((ifp->if_flags & IFF_POINTOPOINT) == 0)
1808 if ((fibnum != RT_ALL_FIBS) && (ifp->if_fib != fibnum))
1811 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
1812 if (ifa->ifa_addr->sa_family != addr->sa_family)
1814 if (ifa->ifa_dstaddr != NULL &&
1815 sa_equal(addr, ifa->ifa_dstaddr)) {
1817 IF_ADDR_RUNLOCK(ifp);
1821 IF_ADDR_RUNLOCK(ifp);
1825 IFNET_RUNLOCK_NOSLEEP();
1830 * Find an interface on a specific network. If many, choice
1831 * is most specific found.
1834 ifa_ifwithnet(struct sockaddr *addr, int ignore_ptp, int fibnum)
1838 struct ifaddr *ifa_maybe = NULL;
1839 u_int af = addr->sa_family;
1840 char *addr_data = addr->sa_data, *cplim;
1843 * AF_LINK addresses can be looked up directly by their index number,
1844 * so do that if we can.
1846 if (af == AF_LINK) {
1847 struct sockaddr_dl *sdl = (struct sockaddr_dl *)addr;
1848 if (sdl->sdl_index && sdl->sdl_index <= V_if_index)
1849 return (ifaddr_byindex(sdl->sdl_index));
1853 * Scan though each interface, looking for ones that have addresses
1854 * in this address family and the requested fib. Maintain a reference
1855 * on ifa_maybe once we find one, as we release the IF_ADDR_RLOCK() that
1856 * kept it stable when we move onto the next interface.
1858 IFNET_RLOCK_NOSLEEP();
1859 TAILQ_FOREACH(ifp, &V_ifnet, if_link) {
1860 if ((fibnum != RT_ALL_FIBS) && (ifp->if_fib != fibnum))
1863 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
1864 char *cp, *cp2, *cp3;
1866 if (ifa->ifa_addr->sa_family != af)
1868 if (af == AF_INET &&
1869 ifp->if_flags & IFF_POINTOPOINT && !ignore_ptp) {
1871 * This is a bit broken as it doesn't
1872 * take into account that the remote end may
1873 * be a single node in the network we are
1875 * The trouble is that we don't know the
1876 * netmask for the remote end.
1878 if (ifa->ifa_dstaddr != NULL &&
1879 sa_equal(addr, ifa->ifa_dstaddr)) {
1881 IF_ADDR_RUNLOCK(ifp);
1886 * Scan all the bits in the ifa's address.
1887 * If a bit dissagrees with what we are
1888 * looking for, mask it with the netmask
1889 * to see if it really matters.
1890 * (A byte at a time)
1892 if (ifa->ifa_netmask == 0)
1895 cp2 = ifa->ifa_addr->sa_data;
1896 cp3 = ifa->ifa_netmask->sa_data;
1897 cplim = ifa->ifa_netmask->sa_len
1898 + (char *)ifa->ifa_netmask;
1900 if ((*cp++ ^ *cp2++) & *cp3++)
1901 goto next; /* next address! */
1903 * If the netmask of what we just found
1904 * is more specific than what we had before
1905 * (if we had one), or if the virtual status
1906 * of new prefix is better than of the old one,
1907 * then remember the new one before continuing
1908 * to search for an even better one.
1910 if (ifa_maybe == NULL ||
1911 ifa_preferred(ifa_maybe, ifa) ||
1912 rn_refines((caddr_t)ifa->ifa_netmask,
1913 (caddr_t)ifa_maybe->ifa_netmask)) {
1914 if (ifa_maybe != NULL)
1915 ifa_free(ifa_maybe);
1921 IF_ADDR_RUNLOCK(ifp);
1926 IFNET_RUNLOCK_NOSLEEP();
1927 if (ifa_maybe != NULL)
1928 ifa_free(ifa_maybe);
1933 * Find an interface address specific to an interface best matching
1937 ifaof_ifpforaddr(struct sockaddr *addr, struct ifnet *ifp)
1940 char *cp, *cp2, *cp3;
1942 struct ifaddr *ifa_maybe = NULL;
1943 u_int af = addr->sa_family;
1948 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
1949 if (ifa->ifa_addr->sa_family != af)
1951 if (ifa_maybe == NULL)
1953 if (ifa->ifa_netmask == 0) {
1954 if (sa_equal(addr, ifa->ifa_addr) ||
1955 (ifa->ifa_dstaddr &&
1956 sa_equal(addr, ifa->ifa_dstaddr)))
1960 if (ifp->if_flags & IFF_POINTOPOINT) {
1961 if (sa_equal(addr, ifa->ifa_dstaddr))
1965 cp2 = ifa->ifa_addr->sa_data;
1966 cp3 = ifa->ifa_netmask->sa_data;
1967 cplim = ifa->ifa_netmask->sa_len + (char *)ifa->ifa_netmask;
1968 for (; cp3 < cplim; cp3++)
1969 if ((*cp++ ^ *cp2++) & *cp3)
1979 IF_ADDR_RUNLOCK(ifp);
1984 * See whether new ifa is better than current one:
1985 * 1) A non-virtual one is preferred over virtual.
1986 * 2) A virtual in master state preferred over any other state.
1988 * Used in several address selecting functions.
1991 ifa_preferred(struct ifaddr *cur, struct ifaddr *next)
1994 return (cur->ifa_carp && (!next->ifa_carp ||
1995 ((*carp_master_p)(next) && !(*carp_master_p)(cur))));
1998 #include <net/if_llatbl.h>
2001 * Default action when installing a route with a Link Level gateway.
2002 * Lookup an appropriate real ifa to point to.
2003 * This should be moved to /sys/net/link.c eventually.
2006 link_rtrequest(int cmd, struct rtentry *rt, struct rt_addrinfo *info)
2008 struct ifaddr *ifa, *oifa;
2009 struct sockaddr *dst;
2014 if (cmd != RTM_ADD || ((ifa = rt->rt_ifa) == 0) ||
2015 ((ifp = ifa->ifa_ifp) == 0) || ((dst = rt_key(rt)) == 0))
2017 ifa = ifaof_ifpforaddr(dst, ifp);
2022 if (ifa->ifa_rtrequest && ifa->ifa_rtrequest != link_rtrequest)
2023 ifa->ifa_rtrequest(cmd, rt, info);
2027 struct sockaddr_dl *
2028 link_alloc_sdl(size_t size, int flags)
2031 return (malloc(size, M_TEMP, flags));
2035 link_free_sdl(struct sockaddr *sa)
2041 * Fills in given sdl with interface basic info.
2042 * Returns pointer to filled sdl.
2044 struct sockaddr_dl *
2045 link_init_sdl(struct ifnet *ifp, struct sockaddr *paddr, u_char iftype)
2047 struct sockaddr_dl *sdl;
2049 sdl = (struct sockaddr_dl *)paddr;
2050 memset(sdl, 0, sizeof(struct sockaddr_dl));
2051 sdl->sdl_len = sizeof(struct sockaddr_dl);
2052 sdl->sdl_family = AF_LINK;
2053 sdl->sdl_index = ifp->if_index;
2054 sdl->sdl_type = iftype;
2060 * Mark an interface down and notify protocols of
2064 if_unroute(struct ifnet *ifp, int flag, int fam)
2068 KASSERT(flag == IFF_UP, ("if_unroute: flag != IFF_UP"));
2070 ifp->if_flags &= ~flag;
2071 getmicrotime(&ifp->if_lastchange);
2072 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link)
2073 if (fam == PF_UNSPEC || (fam == ifa->ifa_addr->sa_family))
2074 pfctlinput(PRC_IFDOWN, ifa->ifa_addr);
2075 ifp->if_qflush(ifp);
2078 (*carp_linkstate_p)(ifp);
2083 * Mark an interface up and notify protocols of
2087 if_route(struct ifnet *ifp, int flag, int fam)
2091 KASSERT(flag == IFF_UP, ("if_route: flag != IFF_UP"));
2093 ifp->if_flags |= flag;
2094 getmicrotime(&ifp->if_lastchange);
2095 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link)
2096 if (fam == PF_UNSPEC || (fam == ifa->ifa_addr->sa_family))
2097 pfctlinput(PRC_IFUP, ifa->ifa_addr);
2099 (*carp_linkstate_p)(ifp);
2106 void (*vlan_link_state_p)(struct ifnet *); /* XXX: private from if_vlan */
2107 void (*vlan_trunk_cap_p)(struct ifnet *); /* XXX: private from if_vlan */
2108 struct ifnet *(*vlan_trunkdev_p)(struct ifnet *);
2109 struct ifnet *(*vlan_devat_p)(struct ifnet *, uint16_t);
2110 int (*vlan_tag_p)(struct ifnet *, uint16_t *);
2111 int (*vlan_setcookie_p)(struct ifnet *, void *);
2112 void *(*vlan_cookie_p)(struct ifnet *);
2115 * Handle a change in the interface link state. To avoid LORs
2116 * between driver lock and upper layer locks, as well as possible
2117 * recursions, we post event to taskqueue, and all job
2118 * is done in static do_link_state_change().
2121 if_link_state_change(struct ifnet *ifp, int link_state)
2123 /* Return if state hasn't changed. */
2124 if (ifp->if_link_state == link_state)
2127 ifp->if_link_state = link_state;
2129 taskqueue_enqueue(taskqueue_swi, &ifp->if_linktask);
2133 do_link_state_change(void *arg, int pending)
2135 struct ifnet *ifp = (struct ifnet *)arg;
2136 int link_state = ifp->if_link_state;
2137 CURVNET_SET(ifp->if_vnet);
2139 /* Notify that the link state has changed. */
2141 if (ifp->if_vlantrunk != NULL)
2142 (*vlan_link_state_p)(ifp);
2144 if ((ifp->if_type == IFT_ETHER || ifp->if_type == IFT_L2VLAN) &&
2145 IFP2AC(ifp)->ac_netgraph != NULL)
2146 (*ng_ether_link_state_p)(ifp, link_state);
2148 (*carp_linkstate_p)(ifp);
2150 (*bridge_linkstate_p)(ifp);
2152 (*lagg_linkstate_p)(ifp, link_state);
2154 if (IS_DEFAULT_VNET(curvnet))
2155 devctl_notify("IFNET", ifp->if_xname,
2156 (link_state == LINK_STATE_UP) ? "LINK_UP" : "LINK_DOWN",
2159 if_printf(ifp, "%d link states coalesced\n", pending);
2160 if (log_link_state_change)
2161 log(LOG_NOTICE, "%s: link state changed to %s\n", ifp->if_xname,
2162 (link_state == LINK_STATE_UP) ? "UP" : "DOWN" );
2163 EVENTHANDLER_INVOKE(ifnet_link_event, ifp, ifp->if_link_state);
2168 * Mark an interface down and notify protocols of
2172 if_down(struct ifnet *ifp)
2175 if_unroute(ifp, IFF_UP, AF_UNSPEC);
2179 * Mark an interface up and notify protocols of
2183 if_up(struct ifnet *ifp)
2186 if_route(ifp, IFF_UP, AF_UNSPEC);
2190 * Flush an interface queue.
2193 if_qflush(struct ifnet *ifp)
2201 if (ALTQ_IS_ENABLED(ifq))
2205 while ((m = n) != 0) {
2216 * Map interface name to interface structure pointer, with or without
2217 * returning a reference.
2220 ifunit_ref(const char *name)
2224 IFNET_RLOCK_NOSLEEP();
2225 TAILQ_FOREACH(ifp, &V_ifnet, if_link) {
2226 if (strncmp(name, ifp->if_xname, IFNAMSIZ) == 0 &&
2227 !(ifp->if_flags & IFF_DYING))
2232 IFNET_RUNLOCK_NOSLEEP();
2237 ifunit(const char *name)
2241 IFNET_RLOCK_NOSLEEP();
2242 TAILQ_FOREACH(ifp, &V_ifnet, if_link) {
2243 if (strncmp(name, ifp->if_xname, IFNAMSIZ) == 0)
2246 IFNET_RUNLOCK_NOSLEEP();
2251 * Hardware specific interface ioctls.
2254 ifhwioctl(u_long cmd, struct ifnet *ifp, caddr_t data, struct thread *td)
2258 int new_flags, temp_flags;
2259 size_t namelen, onamelen;
2261 char *descrbuf, *odescrbuf;
2262 char new_name[IFNAMSIZ];
2264 struct sockaddr_dl *sdl;
2266 ifr = (struct ifreq *)data;
2269 ifr->ifr_index = ifp->if_index;
2273 temp_flags = ifp->if_flags | ifp->if_drv_flags;
2274 ifr->ifr_flags = temp_flags & 0xffff;
2275 ifr->ifr_flagshigh = temp_flags >> 16;
2279 ifr->ifr_reqcap = ifp->if_capabilities;
2280 ifr->ifr_curcap = ifp->if_capenable;
2285 error = mac_ifnet_ioctl_get(td->td_ucred, ifr, ifp);
2290 ifr->ifr_metric = ifp->if_metric;
2294 ifr->ifr_mtu = ifp->if_mtu;
2298 /* XXXGL: did this ever worked? */
2304 sx_slock(&ifdescr_sx);
2305 if (ifp->if_description == NULL)
2308 /* space for terminating nul */
2309 descrlen = strlen(ifp->if_description) + 1;
2310 if (ifr->ifr_buffer.length < descrlen)
2311 ifr->ifr_buffer.buffer = NULL;
2313 error = copyout(ifp->if_description,
2314 ifr->ifr_buffer.buffer, descrlen);
2315 ifr->ifr_buffer.length = descrlen;
2317 sx_sunlock(&ifdescr_sx);
2321 error = priv_check(td, PRIV_NET_SETIFDESCR);
2326 * Copy only (length-1) bytes to make sure that
2327 * if_description is always nul terminated. The
2328 * length parameter is supposed to count the
2329 * terminating nul in.
2331 if (ifr->ifr_buffer.length > ifdescr_maxlen)
2332 return (ENAMETOOLONG);
2333 else if (ifr->ifr_buffer.length == 0)
2336 descrbuf = malloc(ifr->ifr_buffer.length, M_IFDESCR,
2338 error = copyin(ifr->ifr_buffer.buffer, descrbuf,
2339 ifr->ifr_buffer.length - 1);
2341 free(descrbuf, M_IFDESCR);
2346 sx_xlock(&ifdescr_sx);
2347 odescrbuf = ifp->if_description;
2348 ifp->if_description = descrbuf;
2349 sx_xunlock(&ifdescr_sx);
2351 getmicrotime(&ifp->if_lastchange);
2352 free(odescrbuf, M_IFDESCR);
2356 ifr->ifr_fib = ifp->if_fib;
2360 error = priv_check(td, PRIV_NET_SETIFFIB);
2363 if (ifr->ifr_fib >= rt_numfibs)
2366 ifp->if_fib = ifr->ifr_fib;
2370 error = priv_check(td, PRIV_NET_SETIFFLAGS);
2374 * Currently, no driver owned flags pass the IFF_CANTCHANGE
2375 * check, so we don't need special handling here yet.
2377 new_flags = (ifr->ifr_flags & 0xffff) |
2378 (ifr->ifr_flagshigh << 16);
2379 if (ifp->if_flags & IFF_UP &&
2380 (new_flags & IFF_UP) == 0) {
2382 } else if (new_flags & IFF_UP &&
2383 (ifp->if_flags & IFF_UP) == 0) {
2386 /* See if permanently promiscuous mode bit is about to flip */
2387 if ((ifp->if_flags ^ new_flags) & IFF_PPROMISC) {
2388 if (new_flags & IFF_PPROMISC)
2389 ifp->if_flags |= IFF_PROMISC;
2390 else if (ifp->if_pcount == 0)
2391 ifp->if_flags &= ~IFF_PROMISC;
2392 log(LOG_INFO, "%s: permanently promiscuous mode %s\n",
2394 (new_flags & IFF_PPROMISC) ? "enabled" : "disabled");
2396 ifp->if_flags = (ifp->if_flags & IFF_CANTCHANGE) |
2397 (new_flags &~ IFF_CANTCHANGE);
2398 if (ifp->if_ioctl) {
2399 (void) (*ifp->if_ioctl)(ifp, cmd, data);
2401 getmicrotime(&ifp->if_lastchange);
2405 error = priv_check(td, PRIV_NET_SETIFCAP);
2408 if (ifp->if_ioctl == NULL)
2409 return (EOPNOTSUPP);
2410 if (ifr->ifr_reqcap & ~ifp->if_capabilities)
2412 error = (*ifp->if_ioctl)(ifp, cmd, data);
2414 getmicrotime(&ifp->if_lastchange);
2419 error = mac_ifnet_ioctl_set(td->td_ucred, ifr, ifp);
2424 error = priv_check(td, PRIV_NET_SETIFNAME);
2427 error = copyinstr(ifr->ifr_data, new_name, IFNAMSIZ, NULL);
2430 if (new_name[0] == '\0')
2432 if (ifunit(new_name) != NULL)
2436 * XXX: Locking. Nothing else seems to lock if_flags,
2437 * and there are numerous other races with the
2438 * ifunit() checks not being atomic with namespace
2439 * changes (renames, vmoves, if_attach, etc).
2441 ifp->if_flags |= IFF_RENAMING;
2443 /* Announce the departure of the interface. */
2444 rt_ifannouncemsg(ifp, IFAN_DEPARTURE);
2445 EVENTHANDLER_INVOKE(ifnet_departure_event, ifp);
2447 log(LOG_INFO, "%s: changing name to '%s'\n",
2448 ifp->if_xname, new_name);
2451 strlcpy(ifp->if_xname, new_name, sizeof(ifp->if_xname));
2453 sdl = (struct sockaddr_dl *)ifa->ifa_addr;
2454 namelen = strlen(new_name);
2455 onamelen = sdl->sdl_nlen;
2457 * Move the address if needed. This is safe because we
2458 * allocate space for a name of length IFNAMSIZ when we
2459 * create this in if_attach().
2461 if (namelen != onamelen) {
2462 bcopy(sdl->sdl_data + onamelen,
2463 sdl->sdl_data + namelen, sdl->sdl_alen);
2465 bcopy(new_name, sdl->sdl_data, namelen);
2466 sdl->sdl_nlen = namelen;
2467 sdl = (struct sockaddr_dl *)ifa->ifa_netmask;
2468 bzero(sdl->sdl_data, onamelen);
2469 while (namelen != 0)
2470 sdl->sdl_data[--namelen] = 0xff;
2471 IF_ADDR_WUNLOCK(ifp);
2473 EVENTHANDLER_INVOKE(ifnet_arrival_event, ifp);
2474 /* Announce the return of the interface. */
2475 rt_ifannouncemsg(ifp, IFAN_ARRIVAL);
2477 ifp->if_flags &= ~IFF_RENAMING;
2482 error = priv_check(td, PRIV_NET_SETIFVNET);
2485 error = if_vmove_loan(td, ifp, ifr->ifr_name, ifr->ifr_jid);
2490 error = priv_check(td, PRIV_NET_SETIFMETRIC);
2493 ifp->if_metric = ifr->ifr_metric;
2494 getmicrotime(&ifp->if_lastchange);
2498 error = priv_check(td, PRIV_NET_SETIFPHYS);
2501 if (ifp->if_ioctl == NULL)
2502 return (EOPNOTSUPP);
2503 error = (*ifp->if_ioctl)(ifp, cmd, data);
2505 getmicrotime(&ifp->if_lastchange);
2510 u_long oldmtu = ifp->if_mtu;
2512 error = priv_check(td, PRIV_NET_SETIFMTU);
2515 if (ifr->ifr_mtu < IF_MINMTU || ifr->ifr_mtu > IF_MAXMTU)
2517 if (ifp->if_ioctl == NULL)
2518 return (EOPNOTSUPP);
2519 error = (*ifp->if_ioctl)(ifp, cmd, data);
2521 getmicrotime(&ifp->if_lastchange);
2525 * If the link MTU changed, do network layer specific procedure.
2527 if (ifp->if_mtu != oldmtu) {
2537 if (cmd == SIOCADDMULTI)
2538 error = priv_check(td, PRIV_NET_ADDMULTI);
2540 error = priv_check(td, PRIV_NET_DELMULTI);
2544 /* Don't allow group membership on non-multicast interfaces. */
2545 if ((ifp->if_flags & IFF_MULTICAST) == 0)
2546 return (EOPNOTSUPP);
2548 /* Don't let users screw up protocols' entries. */
2549 if (ifr->ifr_addr.sa_family != AF_LINK)
2552 if (cmd == SIOCADDMULTI) {
2553 struct ifmultiaddr *ifma;
2556 * Userland is only permitted to join groups once
2557 * via the if_addmulti() KPI, because it cannot hold
2558 * struct ifmultiaddr * between calls. It may also
2559 * lose a race while we check if the membership
2563 ifma = if_findmulti(ifp, &ifr->ifr_addr);
2564 IF_ADDR_RUNLOCK(ifp);
2568 error = if_addmulti(ifp, &ifr->ifr_addr, &ifma);
2570 error = if_delmulti(ifp, &ifr->ifr_addr);
2573 getmicrotime(&ifp->if_lastchange);
2576 case SIOCSIFPHYADDR:
2577 case SIOCDIFPHYADDR:
2579 case SIOCSIFPHYADDR_IN6:
2582 case SIOCSIFGENERIC:
2583 error = priv_check(td, PRIV_NET_HWIOCTL);
2586 if (ifp->if_ioctl == NULL)
2587 return (EOPNOTSUPP);
2588 error = (*ifp->if_ioctl)(ifp, cmd, data);
2590 getmicrotime(&ifp->if_lastchange);
2594 case SIOCGIFPSRCADDR:
2595 case SIOCGIFPDSTADDR:
2597 case SIOCGIFGENERIC:
2598 if (ifp->if_ioctl == NULL)
2599 return (EOPNOTSUPP);
2600 error = (*ifp->if_ioctl)(ifp, cmd, data);
2604 error = priv_check(td, PRIV_NET_SETLLADDR);
2607 error = if_setlladdr(ifp,
2608 ifr->ifr_addr.sa_data, ifr->ifr_addr.sa_len);
2609 EVENTHANDLER_INVOKE(iflladdr_event, ifp);
2614 struct ifgroupreq *ifgr = (struct ifgroupreq *)ifr;
2616 error = priv_check(td, PRIV_NET_ADDIFGROUP);
2619 if ((error = if_addgroup(ifp, ifgr->ifgr_group)))
2625 if ((error = if_getgroup((struct ifgroupreq *)ifr, ifp)))
2631 struct ifgroupreq *ifgr = (struct ifgroupreq *)ifr;
2633 error = priv_check(td, PRIV_NET_DELIFGROUP);
2636 if ((error = if_delgroup(ifp, ifgr->ifgr_group)))
2648 #ifdef COMPAT_FREEBSD32
2656 #define SIOCGIFCONF32 _IOWR('i', 36, struct ifconf32)
2663 ifioctl(struct socket *so, u_long cmd, caddr_t data, struct thread *td)
2670 CURVNET_SET(so->so_vnet);
2673 error = ifconf(cmd, data);
2677 #ifdef COMPAT_FREEBSD32
2680 struct ifconf32 *ifc32;
2683 ifc32 = (struct ifconf32 *)data;
2684 ifc.ifc_len = ifc32->ifc_len;
2685 ifc.ifc_buf = PTRIN(ifc32->ifc_buf);
2687 error = ifconf(SIOCGIFCONF, (void *)&ifc);
2690 ifc32->ifc_len = ifc.ifc_len;
2695 ifr = (struct ifreq *)data;
2700 error = priv_check(td, PRIV_NET_SETIFVNET);
2702 error = if_vmove_reclaim(td, ifr->ifr_name,
2709 error = priv_check(td, PRIV_NET_IFCREATE);
2711 error = if_clone_create(ifr->ifr_name,
2712 sizeof(ifr->ifr_name),
2713 cmd == SIOCIFCREATE2 ? ifr->ifr_data : NULL);
2717 error = priv_check(td, PRIV_NET_IFDESTROY);
2719 error = if_clone_destroy(ifr->ifr_name);
2723 case SIOCIFGCLONERS:
2724 error = if_clone_list((struct if_clonereq *)data);
2728 error = if_getgroupmembers((struct ifgroupreq *)data);
2731 #if defined(INET) || defined(INET6)
2734 if (carp_ioctl_p == NULL)
2735 error = EPROTONOSUPPORT;
2737 error = (*carp_ioctl_p)(ifr, cmd, td);
2743 ifp = ifunit_ref(ifr->ifr_name);
2749 error = ifhwioctl(cmd, ifp, data, td);
2750 if (error != ENOIOCTL) {
2756 oif_flags = ifp->if_flags;
2757 if (so->so_proto == NULL) {
2760 return (EOPNOTSUPP);
2764 * Pass the request on to the socket control method, and if the
2765 * latter returns EOPNOTSUPP, directly to the interface.
2767 * Make an exception for the legacy SIOCSIF* requests. Drivers
2768 * trust SIOCSIFADDR et al to come from an already privileged
2769 * layer, and do not perform any credentials checks or input
2772 error = ((*so->so_proto->pr_usrreqs->pru_control)(so, cmd, data,
2774 if (error == EOPNOTSUPP && ifp != NULL && ifp->if_ioctl != NULL &&
2775 cmd != SIOCSIFADDR && cmd != SIOCSIFBRDADDR &&
2776 cmd != SIOCSIFDSTADDR && cmd != SIOCSIFNETMASK)
2777 error = (*ifp->if_ioctl)(ifp, cmd, data);
2779 if ((oif_flags ^ ifp->if_flags) & IFF_UP) {
2781 if (ifp->if_flags & IFF_UP)
2791 * The code common to handling reference counted flags,
2792 * e.g., in ifpromisc() and if_allmulti().
2793 * The "pflag" argument can specify a permanent mode flag to check,
2794 * such as IFF_PPROMISC for promiscuous mode; should be 0 if none.
2796 * Only to be used on stack-owned flags, not driver-owned flags.
2799 if_setflag(struct ifnet *ifp, int flag, int pflag, int *refcount, int onswitch)
2803 int oldflags, oldcount;
2805 /* Sanity checks to catch programming errors */
2806 KASSERT((flag & (IFF_DRV_OACTIVE|IFF_DRV_RUNNING)) == 0,
2807 ("%s: setting driver-owned flag %d", __func__, flag));
2810 KASSERT(*refcount >= 0,
2811 ("%s: increment negative refcount %d for flag %d",
2812 __func__, *refcount, flag));
2814 KASSERT(*refcount > 0,
2815 ("%s: decrement non-positive refcount %d for flag %d",
2816 __func__, *refcount, flag));
2818 /* In case this mode is permanent, just touch refcount */
2819 if (ifp->if_flags & pflag) {
2820 *refcount += onswitch ? 1 : -1;
2824 /* Save ifnet parameters for if_ioctl() may fail */
2825 oldcount = *refcount;
2826 oldflags = ifp->if_flags;
2829 * See if we aren't the only and touching refcount is enough.
2830 * Actually toggle interface flag if we are the first or last.
2835 ifp->if_flags |= flag;
2839 ifp->if_flags &= ~flag;
2842 /* Call down the driver since we've changed interface flags */
2843 if (ifp->if_ioctl == NULL) {
2847 ifr.ifr_flags = ifp->if_flags & 0xffff;
2848 ifr.ifr_flagshigh = ifp->if_flags >> 16;
2849 error = (*ifp->if_ioctl)(ifp, SIOCSIFFLAGS, (caddr_t)&ifr);
2852 /* Notify userland that interface flags have changed */
2857 /* Recover after driver error */
2858 *refcount = oldcount;
2859 ifp->if_flags = oldflags;
2864 * Set/clear promiscuous mode on interface ifp based on the truth value
2865 * of pswitch. The calls are reference counted so that only the first
2866 * "on" request actually has an effect, as does the final "off" request.
2867 * Results are undefined if the "off" and "on" requests are not matched.
2870 ifpromisc(struct ifnet *ifp, int pswitch)
2873 int oldflags = ifp->if_flags;
2875 error = if_setflag(ifp, IFF_PROMISC, IFF_PPROMISC,
2876 &ifp->if_pcount, pswitch);
2877 /* If promiscuous mode status has changed, log a message */
2878 if (error == 0 && ((ifp->if_flags ^ oldflags) & IFF_PROMISC))
2879 log(LOG_INFO, "%s: promiscuous mode %s\n",
2881 (ifp->if_flags & IFF_PROMISC) ? "enabled" : "disabled");
2886 * Return interface configuration
2887 * of system. List may be used
2888 * in later ioctl's (above) to get
2889 * other information.
2893 ifconf(u_long cmd, caddr_t data)
2895 struct ifconf *ifc = (struct ifconf *)data;
2900 int error, full = 0, valid_len, max_len;
2902 /* Limit initial buffer size to MAXPHYS to avoid DoS from userspace. */
2903 max_len = MAXPHYS - 1;
2905 /* Prevent hostile input from being able to crash the system */
2906 if (ifc->ifc_len <= 0)
2910 if (ifc->ifc_len <= max_len) {
2911 max_len = ifc->ifc_len;
2914 sb = sbuf_new(NULL, NULL, max_len + 1, SBUF_FIXEDLEN);
2919 TAILQ_FOREACH(ifp, &V_ifnet, if_link) {
2923 * Zero the ifr_name buffer to make sure we don't
2924 * disclose the contents of the stack.
2926 memset(ifr.ifr_name, 0, sizeof(ifr.ifr_name));
2928 if (strlcpy(ifr.ifr_name, ifp->if_xname, sizeof(ifr.ifr_name))
2929 >= sizeof(ifr.ifr_name)) {
2932 return (ENAMETOOLONG);
2937 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
2938 struct sockaddr *sa = ifa->ifa_addr;
2940 if (prison_if(curthread->td_ucred, sa) != 0)
2943 if (sa->sa_len <= sizeof(*sa)) {
2945 sbuf_bcat(sb, &ifr, sizeof(ifr));
2946 max_len += sizeof(ifr);
2949 offsetof(struct ifreq, ifr_addr));
2950 max_len += offsetof(struct ifreq, ifr_addr);
2951 sbuf_bcat(sb, sa, sa->sa_len);
2952 max_len += sa->sa_len;
2955 if (sbuf_error(sb) == 0)
2956 valid_len = sbuf_len(sb);
2958 IF_ADDR_RUNLOCK(ifp);
2960 bzero((caddr_t)&ifr.ifr_addr, sizeof(ifr.ifr_addr));
2961 sbuf_bcat(sb, &ifr, sizeof(ifr));
2962 max_len += sizeof(ifr);
2964 if (sbuf_error(sb) == 0)
2965 valid_len = sbuf_len(sb);
2971 * If we didn't allocate enough space (uncommon), try again. If
2972 * we have already allocated as much space as we are allowed,
2973 * return what we've got.
2975 if (valid_len != max_len && !full) {
2980 ifc->ifc_len = valid_len;
2982 error = copyout(sbuf_data(sb), ifc->ifc_req, ifc->ifc_len);
2988 * Just like ifpromisc(), but for all-multicast-reception mode.
2991 if_allmulti(struct ifnet *ifp, int onswitch)
2994 return (if_setflag(ifp, IFF_ALLMULTI, 0, &ifp->if_amcount, onswitch));
2997 struct ifmultiaddr *
2998 if_findmulti(struct ifnet *ifp, struct sockaddr *sa)
3000 struct ifmultiaddr *ifma;
3002 IF_ADDR_LOCK_ASSERT(ifp);
3004 TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
3005 if (sa->sa_family == AF_LINK) {
3006 if (sa_dl_equal(ifma->ifma_addr, sa))
3009 if (sa_equal(ifma->ifma_addr, sa))
3018 * Allocate a new ifmultiaddr and initialize based on passed arguments. We
3019 * make copies of passed sockaddrs. The ifmultiaddr will not be added to
3020 * the ifnet multicast address list here, so the caller must do that and
3021 * other setup work (such as notifying the device driver). The reference
3022 * count is initialized to 1.
3024 static struct ifmultiaddr *
3025 if_allocmulti(struct ifnet *ifp, struct sockaddr *sa, struct sockaddr *llsa,
3028 struct ifmultiaddr *ifma;
3029 struct sockaddr *dupsa;
3031 ifma = malloc(sizeof *ifma, M_IFMADDR, mflags |
3036 dupsa = malloc(sa->sa_len, M_IFMADDR, mflags);
3037 if (dupsa == NULL) {
3038 free(ifma, M_IFMADDR);
3041 bcopy(sa, dupsa, sa->sa_len);
3042 ifma->ifma_addr = dupsa;
3044 ifma->ifma_ifp = ifp;
3045 ifma->ifma_refcount = 1;
3046 ifma->ifma_protospec = NULL;
3049 ifma->ifma_lladdr = NULL;
3053 dupsa = malloc(llsa->sa_len, M_IFMADDR, mflags);
3054 if (dupsa == NULL) {
3055 free(ifma->ifma_addr, M_IFMADDR);
3056 free(ifma, M_IFMADDR);
3059 bcopy(llsa, dupsa, llsa->sa_len);
3060 ifma->ifma_lladdr = dupsa;
3066 * if_freemulti: free ifmultiaddr structure and possibly attached related
3067 * addresses. The caller is responsible for implementing reference
3068 * counting, notifying the driver, handling routing messages, and releasing
3069 * any dependent link layer state.
3072 if_freemulti(struct ifmultiaddr *ifma)
3075 KASSERT(ifma->ifma_refcount == 0, ("if_freemulti: refcount %d",
3076 ifma->ifma_refcount));
3078 if (ifma->ifma_lladdr != NULL)
3079 free(ifma->ifma_lladdr, M_IFMADDR);
3080 free(ifma->ifma_addr, M_IFMADDR);
3081 free(ifma, M_IFMADDR);
3085 * Register an additional multicast address with a network interface.
3087 * - If the address is already present, bump the reference count on the
3088 * address and return.
3089 * - If the address is not link-layer, look up a link layer address.
3090 * - Allocate address structures for one or both addresses, and attach to the
3091 * multicast address list on the interface. If automatically adding a link
3092 * layer address, the protocol address will own a reference to the link
3093 * layer address, to be freed when it is freed.
3094 * - Notify the network device driver of an addition to the multicast address
3097 * 'sa' points to caller-owned memory with the desired multicast address.
3099 * 'retifma' will be used to return a pointer to the resulting multicast
3100 * address reference, if desired.
3103 if_addmulti(struct ifnet *ifp, struct sockaddr *sa,
3104 struct ifmultiaddr **retifma)
3106 struct ifmultiaddr *ifma, *ll_ifma;
3107 struct sockaddr *llsa;
3108 struct sockaddr_dl sdl;
3112 * If the address is already present, return a new reference to it;
3113 * otherwise, allocate storage and set up a new address.
3116 ifma = if_findmulti(ifp, sa);
3118 ifma->ifma_refcount++;
3119 if (retifma != NULL)
3121 IF_ADDR_WUNLOCK(ifp);
3126 * The address isn't already present; resolve the protocol address
3127 * into a link layer address, and then look that up, bump its
3128 * refcount or allocate an ifma for that also.
3129 * Most link layer resolving functions returns address data which
3130 * fits inside default sockaddr_dl structure. However callback
3131 * can allocate another sockaddr structure, in that case we need to
3136 if (ifp->if_resolvemulti != NULL) {
3137 /* Provide called function with buffer size information */
3138 sdl.sdl_len = sizeof(sdl);
3139 llsa = (struct sockaddr *)&sdl;
3140 error = ifp->if_resolvemulti(ifp, &llsa, sa);
3146 * Allocate the new address. Don't hook it up yet, as we may also
3147 * need to allocate a link layer multicast address.
3149 ifma = if_allocmulti(ifp, sa, llsa, M_NOWAIT);
3156 * If a link layer address is found, we'll need to see if it's
3157 * already present in the address list, or allocate is as well.
3158 * When this block finishes, the link layer address will be on the
3162 ll_ifma = if_findmulti(ifp, llsa);
3163 if (ll_ifma == NULL) {
3164 ll_ifma = if_allocmulti(ifp, llsa, NULL, M_NOWAIT);
3165 if (ll_ifma == NULL) {
3166 --ifma->ifma_refcount;
3171 TAILQ_INSERT_HEAD(&ifp->if_multiaddrs, ll_ifma,
3174 ll_ifma->ifma_refcount++;
3175 ifma->ifma_llifma = ll_ifma;
3179 * We now have a new multicast address, ifma, and possibly a new or
3180 * referenced link layer address. Add the primary address to the
3181 * ifnet address list.
3183 TAILQ_INSERT_HEAD(&ifp->if_multiaddrs, ifma, ifma_link);
3185 if (retifma != NULL)
3189 * Must generate the message while holding the lock so that 'ifma'
3190 * pointer is still valid.
3192 rt_newmaddrmsg(RTM_NEWMADDR, ifma);
3193 IF_ADDR_WUNLOCK(ifp);
3196 * We are certain we have added something, so call down to the
3197 * interface to let them know about it.
3199 if (ifp->if_ioctl != NULL) {
3200 (void) (*ifp->if_ioctl)(ifp, SIOCADDMULTI, 0);
3203 if ((llsa != NULL) && (llsa != (struct sockaddr *)&sdl))
3204 link_free_sdl(llsa);
3209 if ((llsa != NULL) && (llsa != (struct sockaddr *)&sdl))
3210 link_free_sdl(llsa);
3213 IF_ADDR_WUNLOCK(ifp);
3218 * Delete a multicast group membership by network-layer group address.
3220 * Returns ENOENT if the entry could not be found. If ifp no longer
3221 * exists, results are undefined. This entry point should only be used
3222 * from subsystems which do appropriate locking to hold ifp for the
3223 * duration of the call.
3224 * Network-layer protocol domains must use if_delmulti_ifma().
3227 if_delmulti(struct ifnet *ifp, struct sockaddr *sa)
3229 struct ifmultiaddr *ifma;
3234 IFNET_RLOCK_NOSLEEP();
3235 TAILQ_FOREACH(oifp, &V_ifnet, if_link)
3240 IFNET_RUNLOCK_NOSLEEP();
3242 KASSERT(ifp != NULL, ("%s: ifnet went away", __func__));
3249 ifma = if_findmulti(ifp, sa);
3251 lastref = if_delmulti_locked(ifp, ifma, 0);
3252 IF_ADDR_WUNLOCK(ifp);
3257 if (lastref && ifp->if_ioctl != NULL) {
3258 (void)(*ifp->if_ioctl)(ifp, SIOCDELMULTI, 0);
3265 * Delete all multicast group membership for an interface.
3266 * Should be used to quickly flush all multicast filters.
3269 if_delallmulti(struct ifnet *ifp)
3271 struct ifmultiaddr *ifma;
3272 struct ifmultiaddr *next;
3275 TAILQ_FOREACH_SAFE(ifma, &ifp->if_multiaddrs, ifma_link, next)
3276 if_delmulti_locked(ifp, ifma, 0);
3277 IF_ADDR_WUNLOCK(ifp);
3281 * Delete a multicast group membership by group membership pointer.
3282 * Network-layer protocol domains must use this routine.
3284 * It is safe to call this routine if the ifp disappeared.
3287 if_delmulti_ifma(struct ifmultiaddr *ifma)
3292 ifp = ifma->ifma_ifp;
3295 printf("%s: ifma_ifp seems to be detached\n", __func__);
3299 IFNET_RLOCK_NOSLEEP();
3300 TAILQ_FOREACH(oifp, &V_ifnet, if_link)
3304 printf("%s: ifnet %p disappeared\n", __func__, ifp);
3307 IFNET_RUNLOCK_NOSLEEP();
3311 * If and only if the ifnet instance exists: Acquire the address lock.
3316 lastref = if_delmulti_locked(ifp, ifma, 0);
3320 * If and only if the ifnet instance exists:
3321 * Release the address lock.
3322 * If the group was left: update the hardware hash filter.
3324 IF_ADDR_WUNLOCK(ifp);
3325 if (lastref && ifp->if_ioctl != NULL) {
3326 (void)(*ifp->if_ioctl)(ifp, SIOCDELMULTI, 0);
3332 * Perform deletion of network-layer and/or link-layer multicast address.
3334 * Return 0 if the reference count was decremented.
3335 * Return 1 if the final reference was released, indicating that the
3336 * hardware hash filter should be reprogrammed.
3339 if_delmulti_locked(struct ifnet *ifp, struct ifmultiaddr *ifma, int detaching)
3341 struct ifmultiaddr *ll_ifma;
3343 if (ifp != NULL && ifma->ifma_ifp != NULL) {
3344 KASSERT(ifma->ifma_ifp == ifp,
3345 ("%s: inconsistent ifp %p", __func__, ifp));
3346 IF_ADDR_WLOCK_ASSERT(ifp);
3349 ifp = ifma->ifma_ifp;
3352 * If the ifnet is detaching, null out references to ifnet,
3353 * so that upper protocol layers will notice, and not attempt
3354 * to obtain locks for an ifnet which no longer exists. The
3355 * routing socket announcement must happen before the ifnet
3356 * instance is detached from the system.
3360 printf("%s: detaching ifnet instance %p\n", __func__, ifp);
3363 * ifp may already be nulled out if we are being reentered
3364 * to delete the ll_ifma.
3367 rt_newmaddrmsg(RTM_DELMADDR, ifma);
3368 ifma->ifma_ifp = NULL;
3372 if (--ifma->ifma_refcount > 0)
3376 * If this ifma is a network-layer ifma, a link-layer ifma may
3377 * have been associated with it. Release it first if so.
3379 ll_ifma = ifma->ifma_llifma;
3380 if (ll_ifma != NULL) {
3381 KASSERT(ifma->ifma_lladdr != NULL,
3382 ("%s: llifma w/o lladdr", __func__));
3384 ll_ifma->ifma_ifp = NULL; /* XXX */
3385 if (--ll_ifma->ifma_refcount == 0) {
3387 TAILQ_REMOVE(&ifp->if_multiaddrs, ll_ifma,
3390 if_freemulti(ll_ifma);
3395 TAILQ_REMOVE(&ifp->if_multiaddrs, ifma, ifma_link);
3400 * The last reference to this instance of struct ifmultiaddr
3401 * was released; the hardware should be notified of this change.
3407 * Set the link layer address on an interface.
3409 * At this time we only support certain types of interfaces,
3410 * and we don't allow the length of the address to change.
3413 if_setlladdr(struct ifnet *ifp, const u_char *lladdr, int len)
3415 struct sockaddr_dl *sdl;
3422 IF_ADDR_RUNLOCK(ifp);
3426 IF_ADDR_RUNLOCK(ifp);
3427 sdl = (struct sockaddr_dl *)ifa->ifa_addr;
3432 if (len != sdl->sdl_alen) { /* don't allow length to change */
3436 switch (ifp->if_type) {
3444 case IFT_IEEE8023ADLAG:
3446 bcopy(lladdr, LLADDR(sdl), len);
3455 * If the interface is already up, we need
3456 * to re-init it in order to reprogram its
3459 if ((ifp->if_flags & IFF_UP) != 0) {
3460 if (ifp->if_ioctl) {
3461 ifp->if_flags &= ~IFF_UP;
3462 ifr.ifr_flags = ifp->if_flags & 0xffff;
3463 ifr.ifr_flagshigh = ifp->if_flags >> 16;
3464 (*ifp->if_ioctl)(ifp, SIOCSIFFLAGS, (caddr_t)&ifr);
3465 ifp->if_flags |= IFF_UP;
3466 ifr.ifr_flags = ifp->if_flags & 0xffff;
3467 ifr.ifr_flagshigh = ifp->if_flags >> 16;
3468 (*ifp->if_ioctl)(ifp, SIOCSIFFLAGS, (caddr_t)&ifr);
3472 * Also send gratuitous ARPs to notify other nodes about
3473 * the address change.
3475 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
3476 if (ifa->ifa_addr->sa_family == AF_INET)
3477 arp_ifinit(ifp, ifa);
3485 * The name argument must be a pointer to storage which will last as
3486 * long as the interface does. For physical devices, the result of
3487 * device_get_name(dev) is a good choice and for pseudo-devices a
3488 * static string works well.
3491 if_initname(struct ifnet *ifp, const char *name, int unit)
3493 ifp->if_dname = name;
3494 ifp->if_dunit = unit;
3495 if (unit != IF_DUNIT_NONE)
3496 snprintf(ifp->if_xname, IFNAMSIZ, "%s%d", name, unit);
3498 strlcpy(ifp->if_xname, name, IFNAMSIZ);
3502 if_printf(struct ifnet *ifp, const char * fmt, ...)
3507 retval = printf("%s: ", ifp->if_xname);
3509 retval += vprintf(fmt, ap);
3515 if_start(struct ifnet *ifp)
3518 (*(ifp)->if_start)(ifp);
3522 * Backwards compatibility interface for drivers
3523 * that have not implemented it
3526 if_transmit(struct ifnet *ifp, struct mbuf *m)
3530 IFQ_HANDOFF(ifp, m, error);
3535 if_handoff(struct ifqueue *ifq, struct mbuf *m, struct ifnet *ifp, int adjust)
3540 if (_IF_QFULL(ifq)) {
3542 if_inc_counter(ifp, IFCOUNTER_OQDROPS, 1);
3547 if_inc_counter(ifp, IFCOUNTER_OBYTES, m->m_pkthdr.len + adjust);
3548 if (m->m_flags & (M_BCAST|M_MCAST))
3549 if_inc_counter(ifp, IFCOUNTER_OMCASTS, 1);
3550 active = ifp->if_drv_flags & IFF_DRV_OACTIVE;
3552 _IF_ENQUEUE(ifq, m);
3554 if (ifp != NULL && !active)
3555 (*(ifp)->if_start)(ifp);
3560 if_register_com_alloc(u_char type,
3561 if_com_alloc_t *a, if_com_free_t *f)
3564 KASSERT(if_com_alloc[type] == NULL,
3565 ("if_register_com_alloc: %d already registered", type));
3566 KASSERT(if_com_free[type] == NULL,
3567 ("if_register_com_alloc: %d free already registered", type));
3569 if_com_alloc[type] = a;
3570 if_com_free[type] = f;
3574 if_deregister_com_alloc(u_char type)
3577 KASSERT(if_com_alloc[type] != NULL,
3578 ("if_deregister_com_alloc: %d not registered", type));
3579 KASSERT(if_com_free[type] != NULL,
3580 ("if_deregister_com_alloc: %d free not registered", type));
3581 if_com_alloc[type] = NULL;
3582 if_com_free[type] = NULL;
3585 /* API for driver access to network stack owned ifnet.*/
3587 if_setbaudrate(struct ifnet *ifp, uint64_t baudrate)
3591 oldbrate = ifp->if_baudrate;
3592 ifp->if_baudrate = baudrate;
3597 if_getbaudrate(if_t ifp)
3600 return (((struct ifnet *)ifp)->if_baudrate);
3604 if_setcapabilities(if_t ifp, int capabilities)
3606 ((struct ifnet *)ifp)->if_capabilities = capabilities;
3611 if_setcapabilitiesbit(if_t ifp, int setbit, int clearbit)
3613 ((struct ifnet *)ifp)->if_capabilities |= setbit;
3614 ((struct ifnet *)ifp)->if_capabilities &= ~clearbit;
3620 if_getcapabilities(if_t ifp)
3622 return ((struct ifnet *)ifp)->if_capabilities;
3626 if_setcapenable(if_t ifp, int capabilities)
3628 ((struct ifnet *)ifp)->if_capenable = capabilities;
3633 if_setcapenablebit(if_t ifp, int setcap, int clearcap)
3636 ((struct ifnet *)ifp)->if_capenable |= setcap;
3638 ((struct ifnet *)ifp)->if_capenable &= ~clearcap;
3644 if_getdname(if_t ifp)
3646 return ((struct ifnet *)ifp)->if_dname;
3650 if_togglecapenable(if_t ifp, int togglecap)
3652 ((struct ifnet *)ifp)->if_capenable ^= togglecap;
3657 if_getcapenable(if_t ifp)
3659 return ((struct ifnet *)ifp)->if_capenable;
3663 * This is largely undesirable because it ties ifnet to a device, but does
3664 * provide flexiblity for an embedded product vendor. Should be used with
3665 * the understanding that it violates the interface boundaries, and should be
3666 * a last resort only.
3669 if_setdev(if_t ifp, void *dev)
3675 if_setdrvflagbits(if_t ifp, int set_flags, int clear_flags)
3677 ((struct ifnet *)ifp)->if_drv_flags |= set_flags;
3678 ((struct ifnet *)ifp)->if_drv_flags &= ~clear_flags;
3684 if_getdrvflags(if_t ifp)
3686 return ((struct ifnet *)ifp)->if_drv_flags;
3690 if_setdrvflags(if_t ifp, int flags)
3692 ((struct ifnet *)ifp)->if_drv_flags = flags;
3698 if_setflags(if_t ifp, int flags)
3700 ((struct ifnet *)ifp)->if_flags = flags;
3705 if_setflagbits(if_t ifp, int set, int clear)
3707 ((struct ifnet *)ifp)->if_flags |= set;
3708 ((struct ifnet *)ifp)->if_flags &= ~clear;
3714 if_getflags(if_t ifp)
3716 return ((struct ifnet *)ifp)->if_flags;
3720 if_clearhwassist(if_t ifp)
3722 ((struct ifnet *)ifp)->if_hwassist = 0;
3727 if_sethwassistbits(if_t ifp, int toset, int toclear)
3729 ((struct ifnet *)ifp)->if_hwassist |= toset;
3730 ((struct ifnet *)ifp)->if_hwassist &= ~toclear;
3736 if_sethwassist(if_t ifp, int hwassist_bit)
3738 ((struct ifnet *)ifp)->if_hwassist = hwassist_bit;
3743 if_gethwassist(if_t ifp)
3745 return ((struct ifnet *)ifp)->if_hwassist;
3749 if_setmtu(if_t ifp, int mtu)
3751 ((struct ifnet *)ifp)->if_mtu = mtu;
3758 return ((struct ifnet *)ifp)->if_mtu;
3762 if_setsoftc(if_t ifp, void *softc)
3764 ((struct ifnet *)ifp)->if_softc = softc;
3769 if_getsoftc(if_t ifp)
3771 return ((struct ifnet *)ifp)->if_softc;
3775 if_setrcvif(struct mbuf *m, if_t ifp)
3777 m->m_pkthdr.rcvif = (struct ifnet *)ifp;
3781 if_setvtag(struct mbuf *m, uint16_t tag)
3783 m->m_pkthdr.ether_vtag = tag;
3787 if_getvtag(struct mbuf *m)
3790 return (m->m_pkthdr.ether_vtag);
3794 if_sendq_empty(if_t ifp)
3796 return IFQ_DRV_IS_EMPTY(&((struct ifnet *)ifp)->if_snd);
3800 if_getifaddr(if_t ifp)
3802 return ((struct ifnet *)ifp)->if_addr;
3806 if_getamcount(if_t ifp)
3808 return ((struct ifnet *)ifp)->if_amcount;
3813 if_setsendqready(if_t ifp)
3815 IFQ_SET_READY(&((struct ifnet *)ifp)->if_snd);
3820 if_setsendqlen(if_t ifp, int tx_desc_count)
3822 IFQ_SET_MAXLEN(&((struct ifnet *)ifp)->if_snd, tx_desc_count);
3823 ((struct ifnet *)ifp)->if_snd.ifq_drv_maxlen = tx_desc_count;
3829 if_vlantrunkinuse(if_t ifp)
3831 return ((struct ifnet *)ifp)->if_vlantrunk != NULL?1:0;
3835 if_input(if_t ifp, struct mbuf* sendmp)
3837 (*((struct ifnet *)ifp)->if_input)((struct ifnet *)ifp, sendmp);
3843 #ifndef ETH_ADDR_LEN
3844 #define ETH_ADDR_LEN 6
3848 if_setupmultiaddr(if_t ifp, void *mta, int *cnt, int max)
3850 struct ifmultiaddr *ifma;
3851 uint8_t *lmta = (uint8_t *)mta;
3854 TAILQ_FOREACH(ifma, &((struct ifnet *)ifp)->if_multiaddrs, ifma_link) {
3855 if (ifma->ifma_addr->sa_family != AF_LINK)
3861 bcopy(LLADDR((struct sockaddr_dl *)ifma->ifma_addr),
3862 &lmta[mcnt * ETH_ADDR_LEN], ETH_ADDR_LEN);
3871 if_multiaddr_array(if_t ifp, void *mta, int *cnt, int max)
3875 if_maddr_rlock(ifp);
3876 error = if_setupmultiaddr(ifp, mta, cnt, max);
3877 if_maddr_runlock(ifp);
3882 if_multiaddr_count(if_t ifp, int max)
3884 struct ifmultiaddr *ifma;
3888 if_maddr_rlock(ifp);
3889 TAILQ_FOREACH(ifma, &((struct ifnet *)ifp)->if_multiaddrs, ifma_link) {
3890 if (ifma->ifma_addr->sa_family != AF_LINK)
3896 if_maddr_runlock(ifp);
3901 if_dequeue(if_t ifp)
3904 IFQ_DRV_DEQUEUE(&((struct ifnet *)ifp)->if_snd, m);
3910 if_sendq_prepend(if_t ifp, struct mbuf *m)
3912 IFQ_DRV_PREPEND(&((struct ifnet *)ifp)->if_snd, m);
3917 if_setifheaderlen(if_t ifp, int len)
3919 ((struct ifnet *)ifp)->if_hdrlen = len;
3924 if_getlladdr(if_t ifp)
3926 return (IF_LLADDR((struct ifnet *)ifp));
3930 if_gethandle(u_char type)
3932 return (if_alloc(type));
3936 if_bpfmtap(if_t ifh, struct mbuf *m)
3938 struct ifnet *ifp = (struct ifnet *)ifh;
3944 if_etherbpfmtap(if_t ifh, struct mbuf *m)
3946 struct ifnet *ifp = (struct ifnet *)ifh;
3948 ETHER_BPF_MTAP(ifp, m);
3952 if_vlancap(if_t ifh)
3954 struct ifnet *ifp = (struct ifnet *)ifh;
3955 VLAN_CAPABILITIES(ifp);
3959 if_setinitfn(if_t ifp, void (*init_fn)(void *))
3961 ((struct ifnet *)ifp)->if_init = init_fn;
3965 if_setioctlfn(if_t ifp, int (*ioctl_fn)(if_t, u_long, caddr_t))
3967 ((struct ifnet *)ifp)->if_ioctl = (void *)ioctl_fn;
3971 if_setstartfn(if_t ifp, void (*start_fn)(if_t))
3973 ((struct ifnet *)ifp)->if_start = (void *)start_fn;
3977 if_settransmitfn(if_t ifp, if_transmit_fn_t start_fn)
3979 ((struct ifnet *)ifp)->if_transmit = start_fn;
3982 void if_setqflushfn(if_t ifp, if_qflush_fn_t flush_fn)
3984 ((struct ifnet *)ifp)->if_qflush = flush_fn;
3989 if_setgetcounterfn(if_t ifp, if_get_counter_t fn)
3992 ifp->if_get_counter = fn;
3995 /* Revisit these - These are inline functions originally. */
3997 drbr_inuse_drv(if_t ifh, struct buf_ring *br)
3999 return drbr_inuse_drv(ifh, br);
4003 drbr_dequeue_drv(if_t ifh, struct buf_ring *br)
4005 return drbr_dequeue(ifh, br);
4009 drbr_needs_enqueue_drv(if_t ifh, struct buf_ring *br)
4011 return drbr_needs_enqueue(ifh, br);
4015 drbr_enqueue_drv(if_t ifh, struct buf_ring *br, struct mbuf *m)
4017 return drbr_enqueue(ifh, br, m);