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/sysent.h>
59 #include <sys/taskqueue.h>
60 #include <sys/domain.h>
64 #include <machine/stdarg.h>
68 #include <net/ethernet.h>
70 #include <net/if_arp.h>
71 #include <net/if_clone.h>
72 #include <net/if_dl.h>
73 #include <net/if_types.h>
74 #include <net/if_var.h>
75 #include <net/if_media.h>
76 #include <net/if_vlan_var.h>
77 #include <net/radix.h>
78 #include <net/route.h>
81 #if defined(INET) || defined(INET6)
82 #include <net/ethernet.h>
83 #include <netinet/in.h>
84 #include <netinet/in_var.h>
85 #include <netinet/ip.h>
86 #include <netinet/ip_carp.h>
88 #include <netinet/if_ether.h>
91 #include <netinet6/in6_var.h>
92 #include <netinet6/in6_ifattach.h>
94 #endif /* INET || INET6 */
96 #include <security/mac/mac_framework.h>
98 #ifdef COMPAT_FREEBSD32
99 #include <sys/mount.h>
100 #include <compat/freebsd32/freebsd32.h>
102 struct ifreq_buffer32 {
103 uint32_t length; /* (size_t) */
104 uint32_t buffer; /* (void *) */
108 * Interface request structure used for socket
109 * ioctl's. All interface ioctl's must have parameter
110 * definitions which begin with ifr_name. The
111 * remainder may be interface specific.
114 char ifr_name[IFNAMSIZ]; /* if name, e.g. "en0" */
116 struct sockaddr ifru_addr;
117 struct sockaddr ifru_dstaddr;
118 struct sockaddr ifru_broadaddr;
119 struct ifreq_buffer32 ifru_buffer;
130 u_char ifru_vlan_pcp;
133 CTASSERT(sizeof(struct ifreq) == sizeof(struct ifreq32));
134 CTASSERT(__offsetof(struct ifreq, ifr_ifru) ==
135 __offsetof(struct ifreq32, ifr_ifru));
137 struct ifgroupreq32 {
138 char ifgr_name[IFNAMSIZ];
141 char ifgru_group[IFNAMSIZ];
142 uint32_t ifgru_groups;
146 struct ifmediareq32 {
147 char ifm_name[IFNAMSIZ];
153 uint32_t ifm_ulist; /* (int *) */
155 #define SIOCGIFMEDIA32 _IOC_NEWTYPE(SIOCGIFMEDIA, struct ifmediareq32)
156 #define SIOCGIFXMEDIA32 _IOC_NEWTYPE(SIOCGIFXMEDIA, struct ifmediareq32)
158 #define _CASE_IOC_IFGROUPREQ_32(cmd) \
159 case _IOC_NEWTYPE((cmd), struct ifgroupreq32):
160 #else /* !COMPAT_FREEBSD32 */
161 #define _CASE_IOC_IFGROUPREQ_32(cmd)
162 #endif /* !COMPAT_FREEBSD32 */
164 #define CASE_IOC_IFGROUPREQ(cmd) \
165 _CASE_IOC_IFGROUPREQ_32(cmd) \
170 #ifdef COMPAT_FREEBSD32
171 struct ifreq32 ifr32;
175 union ifgroupreq_union {
176 struct ifgroupreq ifgr;
177 #ifdef COMPAT_FREEBSD32
178 struct ifgroupreq32 ifgr32;
182 SYSCTL_NODE(_net, PF_LINK, link, CTLFLAG_RW, 0, "Link layers");
183 SYSCTL_NODE(_net_link, 0, generic, CTLFLAG_RW, 0, "Generic link-management");
185 SYSCTL_INT(_net_link, OID_AUTO, ifqmaxlen, CTLFLAG_RDTUN,
186 &ifqmaxlen, 0, "max send queue size");
188 /* Log link state change events */
189 static int log_link_state_change = 1;
191 SYSCTL_INT(_net_link, OID_AUTO, log_link_state_change, CTLFLAG_RW,
192 &log_link_state_change, 0,
193 "log interface link state change events");
195 /* Log promiscuous mode change events */
196 static int log_promisc_mode_change = 1;
198 SYSCTL_INT(_net_link, OID_AUTO, log_promisc_mode_change, CTLFLAG_RDTUN,
199 &log_promisc_mode_change, 1,
200 "log promiscuous mode change events");
202 /* Interface description */
203 static unsigned int ifdescr_maxlen = 1024;
204 SYSCTL_UINT(_net, OID_AUTO, ifdescr_maxlen, CTLFLAG_RW,
206 "administrative maximum length for interface description");
208 static MALLOC_DEFINE(M_IFDESCR, "ifdescr", "ifnet descriptions");
210 /* global sx for non-critical path ifdescr */
211 static struct sx ifdescr_sx;
212 SX_SYSINIT(ifdescr_sx, &ifdescr_sx, "ifnet descr");
214 void (*bridge_linkstate_p)(struct ifnet *ifp);
215 void (*ng_ether_link_state_p)(struct ifnet *ifp, int state);
216 void (*lagg_linkstate_p)(struct ifnet *ifp, int state);
217 /* These are external hooks for CARP. */
218 void (*carp_linkstate_p)(struct ifnet *ifp);
219 void (*carp_demote_adj_p)(int, char *);
220 int (*carp_master_p)(struct ifaddr *);
221 #if defined(INET) || defined(INET6)
222 int (*carp_forus_p)(struct ifnet *ifp, u_char *dhost);
223 int (*carp_output_p)(struct ifnet *ifp, struct mbuf *m,
224 const struct sockaddr *sa);
225 int (*carp_ioctl_p)(struct ifreq *, u_long, struct thread *);
226 int (*carp_attach_p)(struct ifaddr *, int);
227 void (*carp_detach_p)(struct ifaddr *, bool);
230 int (*carp_iamatch_p)(struct ifaddr *, uint8_t **);
233 struct ifaddr *(*carp_iamatch6_p)(struct ifnet *ifp, struct in6_addr *taddr6);
234 caddr_t (*carp_macmatch6_p)(struct ifnet *ifp, struct mbuf *m,
235 const struct in6_addr *taddr);
238 struct mbuf *(*tbr_dequeue_ptr)(struct ifaltq *, int) = NULL;
241 * XXX: Style; these should be sorted alphabetically, and unprototyped
242 * static functions should be prototyped. Currently they are sorted by
245 static void if_attachdomain(void *);
246 static void if_attachdomain1(struct ifnet *);
247 static int ifconf(u_long, caddr_t);
248 static void if_freemulti(struct ifmultiaddr *);
249 static void if_grow(void);
250 static void if_input_default(struct ifnet *, struct mbuf *);
251 static int if_requestencap_default(struct ifnet *, struct if_encap_req *);
252 static void if_route(struct ifnet *, int flag, int fam);
253 static int if_setflag(struct ifnet *, int, int, int *, int);
254 static int if_transmit(struct ifnet *ifp, struct mbuf *m);
255 static void if_unroute(struct ifnet *, int flag, int fam);
256 static void link_rtrequest(int, struct rtentry *, struct rt_addrinfo *);
257 static int if_delmulti_locked(struct ifnet *, struct ifmultiaddr *, int);
258 static void do_link_state_change(void *, int);
259 static int if_getgroup(struct ifgroupreq *, struct ifnet *);
260 static int if_getgroupmembers(struct ifgroupreq *);
261 static void if_delgroups(struct ifnet *);
262 static void if_attach_internal(struct ifnet *, int, struct if_clone *);
263 static int if_detach_internal(struct ifnet *, int, struct if_clone **);
265 static void if_vmove(struct ifnet *, struct vnet *);
270 * XXX: declare here to avoid to include many inet6 related files..
271 * should be more generalized?
273 extern void nd6_setmtu(struct ifnet *);
276 /* ipsec helper hooks */
277 VNET_DEFINE(struct hhook_head *, ipsec_hhh_in[HHOOK_IPSEC_COUNT]);
278 VNET_DEFINE(struct hhook_head *, ipsec_hhh_out[HHOOK_IPSEC_COUNT]);
280 VNET_DEFINE(int, if_index);
281 int ifqmaxlen = IFQ_MAXLEN;
282 VNET_DEFINE(struct ifnethead, ifnet); /* depend on static init XXX */
283 VNET_DEFINE(struct ifgrouphead, ifg_head);
285 static VNET_DEFINE(int, if_indexlim) = 8;
287 /* Table of ifnet by index. */
288 VNET_DEFINE(struct ifnet **, ifindex_table);
290 #define V_if_indexlim VNET(if_indexlim)
291 #define V_ifindex_table VNET(ifindex_table)
294 * The global network interface list (V_ifnet) and related state (such as
295 * if_index, if_indexlim, and ifindex_table) are protected by an sxlock and
296 * an rwlock. Either may be acquired shared to stablize the list, but both
297 * must be acquired writable to modify the list. This model allows us to
298 * both stablize the interface list during interrupt thread processing, but
299 * also to stablize it over long-running ioctls, without introducing priority
300 * inversions and deadlocks.
302 struct rwlock ifnet_rwlock;
303 RW_SYSINIT_FLAGS(ifnet_rw, &ifnet_rwlock, "ifnet_rw", RW_RECURSE);
304 struct sx ifnet_sxlock;
305 SX_SYSINIT_FLAGS(ifnet_sx, &ifnet_sxlock, "ifnet_sx", SX_RECURSE);
308 * The allocation of network interfaces is a rather non-atomic affair; we
309 * need to select an index before we are ready to expose the interface for
310 * use, so will use this pointer value to indicate reservation.
312 #define IFNET_HOLD (void *)(uintptr_t)(-1)
314 static if_com_alloc_t *if_com_alloc[256];
315 static if_com_free_t *if_com_free[256];
317 static MALLOC_DEFINE(M_IFNET, "ifnet", "interface internals");
318 MALLOC_DEFINE(M_IFADDR, "ifaddr", "interface address");
319 MALLOC_DEFINE(M_IFMADDR, "ether_multi", "link-level multicast address");
322 ifnet_byindex_locked(u_short idx)
325 if (idx > V_if_index)
327 if (V_ifindex_table[idx] == IFNET_HOLD)
329 return (V_ifindex_table[idx]);
333 ifnet_byindex(u_short idx)
337 IFNET_RLOCK_NOSLEEP();
338 ifp = ifnet_byindex_locked(idx);
339 IFNET_RUNLOCK_NOSLEEP();
344 ifnet_byindex_ref(u_short idx)
348 IFNET_RLOCK_NOSLEEP();
349 ifp = ifnet_byindex_locked(idx);
350 if (ifp == NULL || (ifp->if_flags & IFF_DYING)) {
351 IFNET_RUNLOCK_NOSLEEP();
355 IFNET_RUNLOCK_NOSLEEP();
360 * Allocate an ifindex array entry; return 0 on success or an error on
368 IFNET_WLOCK_ASSERT();
371 * Try to find an empty slot below V_if_index. If we fail, take the
374 for (idx = 1; idx <= V_if_index; idx++) {
375 if (V_ifindex_table[idx] == NULL)
379 /* Catch if_index overflow. */
380 if (idx >= V_if_indexlim) {
384 if (idx > V_if_index)
390 ifindex_free_locked(u_short idx)
393 IFNET_WLOCK_ASSERT();
395 V_ifindex_table[idx] = NULL;
396 while (V_if_index > 0 &&
397 V_ifindex_table[V_if_index] == NULL)
402 ifindex_free(u_short idx)
406 ifindex_free_locked(idx);
411 ifnet_setbyindex_locked(u_short idx, struct ifnet *ifp)
414 IFNET_WLOCK_ASSERT();
416 V_ifindex_table[idx] = ifp;
420 ifnet_setbyindex(u_short idx, struct ifnet *ifp)
424 ifnet_setbyindex_locked(idx, ifp);
429 ifaddr_byindex(u_short idx)
432 struct ifaddr *ifa = NULL;
434 IFNET_RLOCK_NOSLEEP();
435 ifp = ifnet_byindex_locked(idx);
436 if (ifp != NULL && (ifa = ifp->if_addr) != NULL)
438 IFNET_RUNLOCK_NOSLEEP();
443 * Network interface utility routines.
445 * Routines with ifa_ifwith* names take sockaddr *'s as
450 vnet_if_init(const void *unused __unused)
453 TAILQ_INIT(&V_ifnet);
454 TAILQ_INIT(&V_ifg_head);
456 if_grow(); /* create initial table */
458 vnet_if_clone_init();
460 VNET_SYSINIT(vnet_if_init, SI_SUB_INIT_IF, SI_ORDER_SECOND, vnet_if_init,
465 vnet_if_uninit(const void *unused __unused)
468 VNET_ASSERT(TAILQ_EMPTY(&V_ifnet), ("%s:%d tailq &V_ifnet=%p "
469 "not empty", __func__, __LINE__, &V_ifnet));
470 VNET_ASSERT(TAILQ_EMPTY(&V_ifg_head), ("%s:%d tailq &V_ifg_head=%p "
471 "not empty", __func__, __LINE__, &V_ifg_head));
473 free((caddr_t)V_ifindex_table, M_IFNET);
475 VNET_SYSUNINIT(vnet_if_uninit, SI_SUB_INIT_IF, SI_ORDER_FIRST,
476 vnet_if_uninit, NULL);
479 vnet_if_return(const void *unused __unused)
481 struct ifnet *ifp, *nifp;
483 /* Return all inherited interfaces to their parent vnets. */
484 TAILQ_FOREACH_SAFE(ifp, &V_ifnet, if_link, nifp) {
485 if (ifp->if_home_vnet != ifp->if_vnet)
486 if_vmove(ifp, ifp->if_home_vnet);
489 VNET_SYSUNINIT(vnet_if_return, SI_SUB_VNET_DONE, SI_ORDER_ANY,
490 vnet_if_return, NULL);
500 IFNET_WLOCK_ASSERT();
501 oldlim = V_if_indexlim;
503 n = (oldlim << 1) * sizeof(*e);
504 e = malloc(n, M_IFNET, M_WAITOK | M_ZERO);
506 if (V_if_indexlim != oldlim) {
510 if (V_ifindex_table != NULL) {
511 memcpy((caddr_t)e, (caddr_t)V_ifindex_table, n/2);
512 free((caddr_t)V_ifindex_table, M_IFNET);
519 * Allocate a struct ifnet and an index for an interface. A layer 2
520 * common structure will also be allocated if an allocation routine is
521 * registered for the passed type.
524 if_alloc(u_char type)
529 ifp = malloc(sizeof(struct ifnet), M_IFNET, M_WAITOK|M_ZERO);
531 idx = ifindex_alloc();
532 ifnet_setbyindex_locked(idx, IFNET_HOLD);
536 ifp->if_alloctype = type;
538 ifp->if_vnet = curvnet;
540 if (if_com_alloc[type] != NULL) {
541 ifp->if_l2com = if_com_alloc[type](type, ifp);
542 if (ifp->if_l2com == NULL) {
549 IF_ADDR_LOCK_INIT(ifp);
550 TASK_INIT(&ifp->if_linktask, 0, do_link_state_change, ifp);
551 ifp->if_afdata_initialized = 0;
552 IF_AFDATA_LOCK_INIT(ifp);
553 TAILQ_INIT(&ifp->if_addrhead);
554 TAILQ_INIT(&ifp->if_multiaddrs);
555 TAILQ_INIT(&ifp->if_groups);
559 ifq_init(&ifp->if_snd, ifp);
561 refcount_init(&ifp->if_refcount, 1); /* Index reference. */
562 for (int i = 0; i < IFCOUNTERS; i++)
563 ifp->if_counters[i] = counter_u64_alloc(M_WAITOK);
564 ifp->if_get_counter = if_get_counter_default;
565 ifp->if_pcp = IFNET_PCP_NONE;
566 ifnet_setbyindex(ifp->if_index, ifp);
571 * Do the actual work of freeing a struct ifnet, and layer 2 common
572 * structure. This call is made when the last reference to an
573 * interface is released.
576 if_free_internal(struct ifnet *ifp)
579 KASSERT((ifp->if_flags & IFF_DYING),
580 ("if_free_internal: interface not dying"));
582 if (if_com_free[ifp->if_alloctype] != NULL)
583 if_com_free[ifp->if_alloctype](ifp->if_l2com,
587 mac_ifnet_destroy(ifp);
589 if (ifp->if_description != NULL)
590 free(ifp->if_description, M_IFDESCR);
591 IF_AFDATA_DESTROY(ifp);
592 IF_ADDR_LOCK_DESTROY(ifp);
593 ifq_delete(&ifp->if_snd);
595 for (int i = 0; i < IFCOUNTERS; i++)
596 counter_u64_free(ifp->if_counters[i]);
602 * Deregister an interface and free the associated storage.
605 if_free(struct ifnet *ifp)
608 ifp->if_flags |= IFF_DYING; /* XXX: Locking */
610 CURVNET_SET_QUIET(ifp->if_vnet);
612 KASSERT(ifp == ifnet_byindex_locked(ifp->if_index),
613 ("%s: freeing unallocated ifnet", ifp->if_xname));
615 ifindex_free_locked(ifp->if_index);
618 if (refcount_release(&ifp->if_refcount))
619 if_free_internal(ifp);
624 * Interfaces to keep an ifnet type-stable despite the possibility of the
625 * driver calling if_free(). If there are additional references, we defer
626 * freeing the underlying data structure.
629 if_ref(struct ifnet *ifp)
632 /* We don't assert the ifnet list lock here, but arguably should. */
633 refcount_acquire(&ifp->if_refcount);
637 if_rele(struct ifnet *ifp)
640 if (!refcount_release(&ifp->if_refcount))
642 if_free_internal(ifp);
646 ifq_init(struct ifaltq *ifq, struct ifnet *ifp)
649 mtx_init(&ifq->ifq_mtx, ifp->if_xname, "if send queue", MTX_DEF);
651 if (ifq->ifq_maxlen == 0)
652 ifq->ifq_maxlen = ifqmaxlen;
655 ifq->altq_disc = NULL;
656 ifq->altq_flags &= ALTQF_CANTCHANGE;
657 ifq->altq_tbr = NULL;
662 ifq_delete(struct ifaltq *ifq)
664 mtx_destroy(&ifq->ifq_mtx);
668 * Perform generic interface initialization tasks and attach the interface
669 * to the list of "active" interfaces. If vmove flag is set on entry
670 * to if_attach_internal(), perform only a limited subset of initialization
671 * tasks, given that we are moving from one vnet to another an ifnet which
672 * has already been fully initialized.
674 * Note that if_detach_internal() removes group membership unconditionally
675 * even when vmove flag is set, and if_attach_internal() adds only IFG_ALL.
676 * Thus, when if_vmove() is applied to a cloned interface, group membership
677 * is lost while a cloned one always joins a group whose name is
678 * ifc->ifc_name. To recover this after if_detach_internal() and
679 * if_attach_internal(), the cloner should be specified to
680 * if_attach_internal() via ifc. If it is non-NULL, if_attach_internal()
681 * attempts to join a group whose name is ifc->ifc_name.
684 * - The decision to return void and thus require this function to
685 * succeed is questionable.
686 * - We should probably do more sanity checking. For instance we don't
687 * do anything to insure if_xname is unique or non-empty.
690 if_attach(struct ifnet *ifp)
693 if_attach_internal(ifp, 0, NULL);
697 * Compute the least common TSO limit.
700 if_hw_tsomax_common(if_t ifp, struct ifnet_hw_tsomax *pmax)
703 * 1) If there is no limit currently, take the limit from
704 * the network adapter.
706 * 2) If the network adapter has a limit below the current
709 if (pmax->tsomaxbytes == 0 || (ifp->if_hw_tsomax != 0 &&
710 ifp->if_hw_tsomax < pmax->tsomaxbytes)) {
711 pmax->tsomaxbytes = ifp->if_hw_tsomax;
713 if (pmax->tsomaxsegcount == 0 || (ifp->if_hw_tsomaxsegcount != 0 &&
714 ifp->if_hw_tsomaxsegcount < pmax->tsomaxsegcount)) {
715 pmax->tsomaxsegcount = ifp->if_hw_tsomaxsegcount;
717 if (pmax->tsomaxsegsize == 0 || (ifp->if_hw_tsomaxsegsize != 0 &&
718 ifp->if_hw_tsomaxsegsize < pmax->tsomaxsegsize)) {
719 pmax->tsomaxsegsize = ifp->if_hw_tsomaxsegsize;
724 * Update TSO limit of a network adapter.
726 * Returns zero if no change. Else non-zero.
729 if_hw_tsomax_update(if_t ifp, struct ifnet_hw_tsomax *pmax)
732 if (ifp->if_hw_tsomax != pmax->tsomaxbytes) {
733 ifp->if_hw_tsomax = pmax->tsomaxbytes;
736 if (ifp->if_hw_tsomaxsegsize != pmax->tsomaxsegsize) {
737 ifp->if_hw_tsomaxsegsize = pmax->tsomaxsegsize;
740 if (ifp->if_hw_tsomaxsegcount != pmax->tsomaxsegcount) {
741 ifp->if_hw_tsomaxsegcount = pmax->tsomaxsegcount;
748 if_attach_internal(struct ifnet *ifp, int vmove, struct if_clone *ifc)
750 unsigned socksize, ifasize;
751 int namelen, masklen;
752 struct sockaddr_dl *sdl;
755 if (ifp->if_index == 0 || ifp != ifnet_byindex(ifp->if_index))
756 panic ("%s: BUG: if_attach called without if_alloc'd input()\n",
760 ifp->if_vnet = curvnet;
761 if (ifp->if_home_vnet == NULL)
762 ifp->if_home_vnet = curvnet;
765 if_addgroup(ifp, IFG_ALL);
767 /* Restore group membership for cloned interfaces. */
768 if (vmove && ifc != NULL)
769 if_clone_addgroup(ifp, ifc);
771 getmicrotime(&ifp->if_lastchange);
772 ifp->if_epoch = time_uptime;
774 KASSERT((ifp->if_transmit == NULL && ifp->if_qflush == NULL) ||
775 (ifp->if_transmit != NULL && ifp->if_qflush != NULL),
776 ("transmit and qflush must both either be set or both be NULL"));
777 if (ifp->if_transmit == NULL) {
778 ifp->if_transmit = if_transmit;
779 ifp->if_qflush = if_qflush;
781 if (ifp->if_input == NULL)
782 ifp->if_input = if_input_default;
784 if (ifp->if_requestencap == NULL)
785 ifp->if_requestencap = if_requestencap_default;
789 mac_ifnet_create(ifp);
793 * Create a Link Level name for this device.
795 namelen = strlen(ifp->if_xname);
797 * Always save enough space for any possiable name so we
798 * can do a rename in place later.
800 masklen = offsetof(struct sockaddr_dl, sdl_data[0]) + IFNAMSIZ;
801 socksize = masklen + ifp->if_addrlen;
802 if (socksize < sizeof(*sdl))
803 socksize = sizeof(*sdl);
804 socksize = roundup2(socksize, sizeof(long));
805 ifasize = sizeof(*ifa) + 2 * socksize;
806 ifa = ifa_alloc(ifasize, M_WAITOK);
807 sdl = (struct sockaddr_dl *)(ifa + 1);
808 sdl->sdl_len = socksize;
809 sdl->sdl_family = AF_LINK;
810 bcopy(ifp->if_xname, sdl->sdl_data, namelen);
811 sdl->sdl_nlen = namelen;
812 sdl->sdl_index = ifp->if_index;
813 sdl->sdl_type = ifp->if_type;
816 ifa->ifa_rtrequest = link_rtrequest;
817 ifa->ifa_addr = (struct sockaddr *)sdl;
818 sdl = (struct sockaddr_dl *)(socksize + (caddr_t)sdl);
819 ifa->ifa_netmask = (struct sockaddr *)sdl;
820 sdl->sdl_len = masklen;
822 sdl->sdl_data[--namelen] = 0xff;
823 TAILQ_INSERT_HEAD(&ifp->if_addrhead, ifa, ifa_link);
824 /* Reliably crash if used uninitialized. */
825 ifp->if_broadcastaddr = NULL;
827 if (ifp->if_type == IFT_ETHER) {
828 ifp->if_hw_addr = malloc(ifp->if_addrlen, M_IFADDR,
832 #if defined(INET) || defined(INET6)
833 /* Use defaults for TSO, if nothing is set */
834 if (ifp->if_hw_tsomax == 0 &&
835 ifp->if_hw_tsomaxsegcount == 0 &&
836 ifp->if_hw_tsomaxsegsize == 0) {
838 * The TSO defaults needs to be such that an
839 * NFS mbuf list of 35 mbufs totalling just
840 * below 64K works and that a chain of mbufs
841 * can be defragged into at most 32 segments:
843 ifp->if_hw_tsomax = min(IP_MAXPACKET, (32 * MCLBYTES) -
844 (ETHER_HDR_LEN + ETHER_VLAN_ENCAP_LEN));
845 ifp->if_hw_tsomaxsegcount = 35;
846 ifp->if_hw_tsomaxsegsize = 2048; /* 2K */
848 /* XXX some drivers set IFCAP_TSO after ethernet attach */
849 if (ifp->if_capabilities & IFCAP_TSO) {
850 if_printf(ifp, "Using defaults for TSO: %u/%u/%u\n",
852 ifp->if_hw_tsomaxsegcount,
853 ifp->if_hw_tsomaxsegsize);
861 * Update the interface index in the link layer address
864 for (ifa = ifp->if_addr; ifa != NULL;
865 ifa = TAILQ_NEXT(ifa, ifa_link)) {
866 if (ifa->ifa_addr->sa_family == AF_LINK) {
867 sdl = (struct sockaddr_dl *)ifa->ifa_addr;
868 sdl->sdl_index = ifp->if_index;
875 TAILQ_INSERT_TAIL(&V_ifnet, ifp, if_link);
877 curvnet->vnet_ifcnt++;
881 if (domain_init_status >= 2)
882 if_attachdomain1(ifp);
884 EVENTHANDLER_INVOKE(ifnet_arrival_event, ifp);
885 if (IS_DEFAULT_VNET(curvnet))
886 devctl_notify("IFNET", ifp->if_xname, "ATTACH", NULL);
888 /* Announce the interface. */
889 rt_ifannouncemsg(ifp, IFAN_ARRIVAL);
893 if_attachdomain(void *dummy)
897 TAILQ_FOREACH(ifp, &V_ifnet, if_link)
898 if_attachdomain1(ifp);
900 SYSINIT(domainifattach, SI_SUB_PROTO_IFATTACHDOMAIN, SI_ORDER_SECOND,
901 if_attachdomain, NULL);
904 if_attachdomain1(struct ifnet *ifp)
909 * Since dp->dom_ifattach calls malloc() with M_WAITOK, we
910 * cannot lock ifp->if_afdata initialization, entirely.
913 if (ifp->if_afdata_initialized >= domain_init_status) {
914 IF_AFDATA_UNLOCK(ifp);
915 log(LOG_WARNING, "%s called more than once on %s\n",
916 __func__, ifp->if_xname);
919 ifp->if_afdata_initialized = domain_init_status;
920 IF_AFDATA_UNLOCK(ifp);
922 /* address family dependent data region */
923 bzero(ifp->if_afdata, sizeof(ifp->if_afdata));
924 for (dp = domains; dp; dp = dp->dom_next) {
925 if (dp->dom_ifattach)
926 ifp->if_afdata[dp->dom_family] =
927 (*dp->dom_ifattach)(ifp);
932 * Remove any unicast or broadcast network addresses from an interface.
935 if_purgeaddrs(struct ifnet *ifp)
937 struct ifaddr *ifa, *next;
939 /* XXX cannot hold IF_ADDR_WLOCK over called functions. */
940 TAILQ_FOREACH_SAFE(ifa, &ifp->if_addrhead, ifa_link, next) {
941 if (ifa->ifa_addr->sa_family == AF_LINK)
944 /* XXX: Ugly!! ad hoc just for INET */
945 if (ifa->ifa_addr->sa_family == AF_INET) {
946 struct ifaliasreq ifr;
948 bzero(&ifr, sizeof(ifr));
949 ifr.ifra_addr = *ifa->ifa_addr;
950 if (ifa->ifa_dstaddr)
951 ifr.ifra_broadaddr = *ifa->ifa_dstaddr;
952 if (in_control(NULL, SIOCDIFADDR, (caddr_t)&ifr, ifp,
958 if (ifa->ifa_addr->sa_family == AF_INET6) {
960 /* ifp_addrhead is already updated */
965 TAILQ_REMOVE(&ifp->if_addrhead, ifa, ifa_link);
966 IF_ADDR_WUNLOCK(ifp);
972 * Remove any multicast network addresses from an interface when an ifnet
976 if_purgemaddrs(struct ifnet *ifp)
978 struct ifmultiaddr *ifma;
979 struct ifmultiaddr *next;
982 TAILQ_FOREACH_SAFE(ifma, &ifp->if_multiaddrs, ifma_link, next)
983 if_delmulti_locked(ifp, ifma, 1);
984 IF_ADDR_WUNLOCK(ifp);
988 * Detach an interface, removing it from the list of "active" interfaces.
989 * If vmove flag is set on entry to if_detach_internal(), perform only a
990 * limited subset of cleanup tasks, given that we are moving an ifnet from
991 * one vnet to another, where it must be fully operational.
993 * XXXRW: There are some significant questions about event ordering, and
994 * how to prevent things from starting to use the interface during detach.
997 if_detach(struct ifnet *ifp)
1000 CURVNET_SET_QUIET(ifp->if_vnet);
1001 if_detach_internal(ifp, 0, NULL);
1006 * The vmove flag, if set, indicates that we are called from a callpath
1007 * that is moving an interface to a different vnet instance.
1009 * The shutdown flag, if set, indicates that we are called in the
1010 * process of shutting down a vnet instance. Currently only the
1011 * vnet_if_return SYSUNINIT function sets it. Note: we can be called
1012 * on a vnet instance shutdown without this flag being set, e.g., when
1013 * the cloned interfaces are destoyed as first thing of teardown.
1016 if_detach_internal(struct ifnet *ifp, int vmove, struct if_clone **ifcp)
1026 shutdown = (ifp->if_vnet->vnet_state > SI_SUB_VNET &&
1027 ifp->if_vnet->vnet_state < SI_SUB_VNET_DONE) ? 1 : 0;
1030 TAILQ_FOREACH(iter, &V_ifnet, if_link)
1032 TAILQ_REMOVE(&V_ifnet, ifp, if_link);
1034 ifp->if_flags |= IFF_DYING;
1041 * While we would want to panic here, we cannot
1042 * guarantee that the interface is indeed still on
1043 * the list given we don't hold locks all the way.
1048 panic("%s: ifp=%p not on the ifnet tailq %p",
1049 __func__, ifp, &V_ifnet);
1051 return; /* XXX this should panic as well? */
1056 * At this point we know the interface still was on the ifnet list
1057 * and we removed it so we are in a stable state.
1060 curvnet->vnet_ifcnt--;
1064 * In any case (destroy or vmove) detach us from the groups
1065 * and remove/wait for pending events on the taskq.
1066 * XXX-BZ in theory an interface could still enqueue a taskq change?
1070 taskqueue_drain(taskqueue_swi, &ifp->if_linktask);
1073 * Check if this is a cloned interface or not. Must do even if
1074 * shutting down as a if_vmove_reclaim() would move the ifp and
1075 * the if_clone_addgroup() will have a corrupted string overwise
1076 * from a gibberish pointer.
1078 if (vmove && ifcp != NULL)
1079 *ifcp = if_clone_findifc(ifp);
1085 * On VNET shutdown abort here as the stack teardown will do all
1086 * the work top-down for us.
1089 /* Give interface users the chance to clean up. */
1090 EVENTHANDLER_INVOKE(ifnet_departure_event, ifp);
1093 * In case of a vmove we are done here without error.
1094 * If we would signal an error it would lead to the same
1095 * abort as if we did not find the ifnet anymore.
1096 * if_detach() calls us in void context and does not care
1097 * about an early abort notification, so life is splendid :)
1099 goto finish_vnet_shutdown;
1104 * At this point we are not tearing down a VNET and are either
1105 * going to destroy or vmove the interface and have to cleanup
1110 * Remove routes and flush queues.
1113 if (ALTQ_IS_ENABLED(&ifp->if_snd))
1114 altq_disable(&ifp->if_snd);
1115 if (ALTQ_IS_ATTACHED(&ifp->if_snd))
1116 altq_detach(&ifp->if_snd);
1127 * Remove all IPv6 kernel structs related to ifp. This should be done
1128 * before removing routing entries below, since IPv6 interface direct
1129 * routes are expected to be removed by the IPv6-specific kernel API.
1130 * Otherwise, the kernel will detect some inconsistency and bark it.
1134 if_purgemaddrs(ifp);
1136 /* Announce that the interface is gone. */
1137 rt_ifannouncemsg(ifp, IFAN_DEPARTURE);
1138 EVENTHANDLER_INVOKE(ifnet_departure_event, ifp);
1139 if (IS_DEFAULT_VNET(curvnet))
1140 devctl_notify("IFNET", ifp->if_xname, "DETACH", NULL);
1144 * Prevent further calls into the device driver via ifnet.
1149 * Remove link ifaddr pointer and maybe decrement if_index.
1150 * Clean up all addresses.
1152 free(ifp->if_hw_addr, M_IFADDR);
1153 ifp->if_hw_addr = NULL;
1154 ifp->if_addr = NULL;
1156 /* We can now free link ifaddr. */
1158 if (!TAILQ_EMPTY(&ifp->if_addrhead)) {
1159 ifa = TAILQ_FIRST(&ifp->if_addrhead);
1160 TAILQ_REMOVE(&ifp->if_addrhead, ifa, ifa_link);
1161 IF_ADDR_WUNLOCK(ifp);
1164 IF_ADDR_WUNLOCK(ifp);
1167 rt_flushifroutes(ifp);
1170 finish_vnet_shutdown:
1173 * We cannot hold the lock over dom_ifdetach calls as they might
1174 * sleep, for example trying to drain a callout, thus open up the
1175 * theoretical race with re-attaching.
1177 IF_AFDATA_LOCK(ifp);
1178 i = ifp->if_afdata_initialized;
1179 ifp->if_afdata_initialized = 0;
1180 IF_AFDATA_UNLOCK(ifp);
1181 for (dp = domains; i > 0 && dp; dp = dp->dom_next) {
1182 if (dp->dom_ifdetach && ifp->if_afdata[dp->dom_family]) {
1183 (*dp->dom_ifdetach)(ifp,
1184 ifp->if_afdata[dp->dom_family]);
1185 ifp->if_afdata[dp->dom_family] = NULL;
1194 * if_vmove() performs a limited version of if_detach() in current
1195 * vnet and if_attach()es the ifnet to the vnet specified as 2nd arg.
1196 * An attempt is made to shrink if_index in current vnet, find an
1197 * unused if_index in target vnet and calls if_grow() if necessary,
1198 * and finally find an unused if_xname for the target vnet.
1201 if_vmove(struct ifnet *ifp, struct vnet *new_vnet)
1203 struct if_clone *ifc;
1204 u_int bif_dlt, bif_hdrlen;
1208 * if_detach_internal() will call the eventhandler to notify
1209 * interface departure. That will detach if_bpf. We need to
1210 * safe the dlt and hdrlen so we can re-attach it later.
1212 bpf_get_bp_params(ifp->if_bpf, &bif_dlt, &bif_hdrlen);
1215 * Detach from current vnet, but preserve LLADDR info, do not
1216 * mark as dead etc. so that the ifnet can be reattached later.
1217 * If we cannot find it, we lost the race to someone else.
1219 rc = if_detach_internal(ifp, 1, &ifc);
1224 * Unlink the ifnet from ifindex_table[] in current vnet, and shrink
1225 * the if_index for that vnet if possible.
1227 * NOTE: IFNET_WLOCK/IFNET_WUNLOCK() are assumed to be unvirtualized,
1228 * or we'd lock on one vnet and unlock on another.
1231 ifindex_free_locked(ifp->if_index);
1235 * Perform interface-specific reassignment tasks, if provided by
1238 if (ifp->if_reassign != NULL)
1239 ifp->if_reassign(ifp, new_vnet, NULL);
1242 * Switch to the context of the target vnet.
1244 CURVNET_SET_QUIET(new_vnet);
1247 ifp->if_index = ifindex_alloc();
1248 ifnet_setbyindex_locked(ifp->if_index, ifp);
1251 if_attach_internal(ifp, 1, ifc);
1253 if (ifp->if_bpf == NULL)
1254 bpfattach(ifp, bif_dlt, bif_hdrlen);
1260 * Move an ifnet to or from another child prison/vnet, specified by the jail id.
1263 if_vmove_loan(struct thread *td, struct ifnet *ifp, char *ifname, int jid)
1269 /* Try to find the prison within our visibility. */
1270 sx_slock(&allprison_lock);
1271 pr = prison_find_child(td->td_ucred->cr_prison, jid);
1272 sx_sunlock(&allprison_lock);
1275 prison_hold_locked(pr);
1276 mtx_unlock(&pr->pr_mtx);
1278 /* Do not try to move the iface from and to the same prison. */
1279 if (pr->pr_vnet == ifp->if_vnet) {
1284 /* Make sure the named iface does not exists in the dst. prison/vnet. */
1285 /* XXX Lock interfaces to avoid races. */
1286 CURVNET_SET_QUIET(pr->pr_vnet);
1287 difp = ifunit(ifname);
1294 /* Make sure the VNET is stable. */
1295 shutdown = (ifp->if_vnet->vnet_state > SI_SUB_VNET &&
1296 ifp->if_vnet->vnet_state < SI_SUB_VNET_DONE) ? 1 : 0;
1304 /* Move the interface into the child jail/vnet. */
1305 if_vmove(ifp, pr->pr_vnet);
1307 /* Report the new if_xname back to the userland. */
1308 sprintf(ifname, "%s", ifp->if_xname);
1315 if_vmove_reclaim(struct thread *td, char *ifname, int jid)
1318 struct vnet *vnet_dst;
1322 /* Try to find the prison within our visibility. */
1323 sx_slock(&allprison_lock);
1324 pr = prison_find_child(td->td_ucred->cr_prison, jid);
1325 sx_sunlock(&allprison_lock);
1328 prison_hold_locked(pr);
1329 mtx_unlock(&pr->pr_mtx);
1331 /* Make sure the named iface exists in the source prison/vnet. */
1332 CURVNET_SET(pr->pr_vnet);
1333 ifp = ifunit(ifname); /* XXX Lock to avoid races. */
1340 /* Do not try to move the iface from and to the same prison. */
1341 vnet_dst = TD_TO_VNET(td);
1342 if (vnet_dst == ifp->if_vnet) {
1348 /* Make sure the VNET is stable. */
1349 shutdown = (ifp->if_vnet->vnet_state > SI_SUB_VNET &&
1350 ifp->if_vnet->vnet_state < SI_SUB_VNET_DONE) ? 1 : 0;
1357 /* Get interface back from child jail/vnet. */
1358 if_vmove(ifp, vnet_dst);
1361 /* Report the new if_xname back to the userland. */
1362 sprintf(ifname, "%s", ifp->if_xname);
1370 * Add a group to an interface
1373 if_addgroup(struct ifnet *ifp, const char *groupname)
1375 struct ifg_list *ifgl;
1376 struct ifg_group *ifg = NULL;
1377 struct ifg_member *ifgm;
1380 if (groupname[0] && groupname[strlen(groupname) - 1] >= '0' &&
1381 groupname[strlen(groupname) - 1] <= '9')
1385 TAILQ_FOREACH(ifgl, &ifp->if_groups, ifgl_next)
1386 if (!strcmp(ifgl->ifgl_group->ifg_group, groupname)) {
1391 if ((ifgl = (struct ifg_list *)malloc(sizeof(struct ifg_list), M_TEMP,
1392 M_NOWAIT)) == NULL) {
1397 if ((ifgm = (struct ifg_member *)malloc(sizeof(struct ifg_member),
1398 M_TEMP, M_NOWAIT)) == NULL) {
1404 TAILQ_FOREACH(ifg, &V_ifg_head, ifg_next)
1405 if (!strcmp(ifg->ifg_group, groupname))
1409 if ((ifg = (struct ifg_group *)malloc(sizeof(struct ifg_group),
1410 M_TEMP, M_NOWAIT)) == NULL) {
1416 strlcpy(ifg->ifg_group, groupname, sizeof(ifg->ifg_group));
1417 ifg->ifg_refcnt = 0;
1418 TAILQ_INIT(&ifg->ifg_members);
1419 TAILQ_INSERT_TAIL(&V_ifg_head, ifg, ifg_next);
1424 ifgl->ifgl_group = ifg;
1425 ifgm->ifgm_ifp = ifp;
1428 TAILQ_INSERT_TAIL(&ifg->ifg_members, ifgm, ifgm_next);
1429 TAILQ_INSERT_TAIL(&ifp->if_groups, ifgl, ifgl_next);
1430 IF_ADDR_WUNLOCK(ifp);
1435 EVENTHANDLER_INVOKE(group_attach_event, ifg);
1436 EVENTHANDLER_INVOKE(group_change_event, groupname);
1442 * Remove a group from an interface
1445 if_delgroup(struct ifnet *ifp, const char *groupname)
1447 struct ifg_list *ifgl;
1448 struct ifg_member *ifgm;
1451 TAILQ_FOREACH(ifgl, &ifp->if_groups, ifgl_next)
1452 if (!strcmp(ifgl->ifgl_group->ifg_group, groupname))
1460 TAILQ_REMOVE(&ifp->if_groups, ifgl, ifgl_next);
1461 IF_ADDR_WUNLOCK(ifp);
1463 TAILQ_FOREACH(ifgm, &ifgl->ifgl_group->ifg_members, ifgm_next)
1464 if (ifgm->ifgm_ifp == ifp)
1468 TAILQ_REMOVE(&ifgl->ifgl_group->ifg_members, ifgm, ifgm_next);
1472 if (--ifgl->ifgl_group->ifg_refcnt == 0) {
1473 TAILQ_REMOVE(&V_ifg_head, ifgl->ifgl_group, ifg_next);
1475 EVENTHANDLER_INVOKE(group_detach_event, ifgl->ifgl_group);
1476 free(ifgl->ifgl_group, M_TEMP);
1482 EVENTHANDLER_INVOKE(group_change_event, groupname);
1488 * Remove an interface from all groups
1491 if_delgroups(struct ifnet *ifp)
1493 struct ifg_list *ifgl;
1494 struct ifg_member *ifgm;
1495 char groupname[IFNAMSIZ];
1498 while (!TAILQ_EMPTY(&ifp->if_groups)) {
1499 ifgl = TAILQ_FIRST(&ifp->if_groups);
1501 strlcpy(groupname, ifgl->ifgl_group->ifg_group, IFNAMSIZ);
1504 TAILQ_REMOVE(&ifp->if_groups, ifgl, ifgl_next);
1505 IF_ADDR_WUNLOCK(ifp);
1507 TAILQ_FOREACH(ifgm, &ifgl->ifgl_group->ifg_members, ifgm_next)
1508 if (ifgm->ifgm_ifp == ifp)
1512 TAILQ_REMOVE(&ifgl->ifgl_group->ifg_members, ifgm,
1517 if (--ifgl->ifgl_group->ifg_refcnt == 0) {
1518 TAILQ_REMOVE(&V_ifg_head, ifgl->ifgl_group, ifg_next);
1520 EVENTHANDLER_INVOKE(group_detach_event,
1522 free(ifgl->ifgl_group, M_TEMP);
1528 EVENTHANDLER_INVOKE(group_change_event, groupname);
1536 ifgr_group_get(void *ifgrp)
1538 union ifgroupreq_union *ifgrup;
1541 #ifdef COMPAT_FREEBSD32
1542 if (SV_CURPROC_FLAG(SV_ILP32))
1543 return (&ifgrup->ifgr32.ifgr_ifgru.ifgru_group[0]);
1545 return (&ifgrup->ifgr.ifgr_ifgru.ifgru_group[0]);
1548 static struct ifg_req *
1549 ifgr_groups_get(void *ifgrp)
1551 union ifgroupreq_union *ifgrup;
1554 #ifdef COMPAT_FREEBSD32
1555 if (SV_CURPROC_FLAG(SV_ILP32))
1556 return ((struct ifg_req *)(uintptr_t)
1557 ifgrup->ifgr32.ifgr_ifgru.ifgru_groups);
1559 return (ifgrup->ifgr.ifgr_ifgru.ifgru_groups);
1563 * Stores all groups from an interface in memory pointed to by ifgr.
1566 if_getgroup(struct ifgroupreq *ifgr, struct ifnet *ifp)
1569 struct ifg_list *ifgl;
1570 struct ifg_req ifgrq, *ifgp;
1572 if (ifgr->ifgr_len == 0) {
1574 TAILQ_FOREACH(ifgl, &ifp->if_groups, ifgl_next)
1575 ifgr->ifgr_len += sizeof(struct ifg_req);
1576 IF_ADDR_RUNLOCK(ifp);
1580 len = ifgr->ifgr_len;
1581 ifgp = ifgr_groups_get(ifgr);
1584 TAILQ_FOREACH(ifgl, &ifp->if_groups, ifgl_next) {
1585 if (len < sizeof(ifgrq)) {
1586 IF_ADDR_RUNLOCK(ifp);
1589 bzero(&ifgrq, sizeof ifgrq);
1590 strlcpy(ifgrq.ifgrq_group, ifgl->ifgl_group->ifg_group,
1591 sizeof(ifgrq.ifgrq_group));
1592 if ((error = copyout(&ifgrq, ifgp, sizeof(struct ifg_req)))) {
1593 IF_ADDR_RUNLOCK(ifp);
1596 len -= sizeof(ifgrq);
1599 IF_ADDR_RUNLOCK(ifp);
1605 * Stores all members of a group in memory pointed to by igfr
1608 if_getgroupmembers(struct ifgroupreq *ifgr)
1610 struct ifg_group *ifg;
1611 struct ifg_member *ifgm;
1612 struct ifg_req ifgrq, *ifgp;
1616 TAILQ_FOREACH(ifg, &V_ifg_head, ifg_next)
1617 if (!strcmp(ifg->ifg_group, ifgr->ifgr_name))
1624 if (ifgr->ifgr_len == 0) {
1625 TAILQ_FOREACH(ifgm, &ifg->ifg_members, ifgm_next)
1626 ifgr->ifgr_len += sizeof(ifgrq);
1631 len = ifgr->ifgr_len;
1632 ifgp = ifgr_groups_get(ifgr);
1633 TAILQ_FOREACH(ifgm, &ifg->ifg_members, ifgm_next) {
1634 if (len < sizeof(ifgrq)) {
1638 bzero(&ifgrq, sizeof ifgrq);
1639 strlcpy(ifgrq.ifgrq_member, ifgm->ifgm_ifp->if_xname,
1640 sizeof(ifgrq.ifgrq_member));
1641 if ((error = copyout(&ifgrq, ifgp, sizeof(struct ifg_req)))) {
1645 len -= sizeof(ifgrq);
1654 * Return counter values from counter(9)s stored in ifnet.
1657 if_get_counter_default(struct ifnet *ifp, ift_counter cnt)
1660 KASSERT(cnt < IFCOUNTERS, ("%s: invalid cnt %d", __func__, cnt));
1662 return (counter_u64_fetch(ifp->if_counters[cnt]));
1666 * Increase an ifnet counter. Usually used for counters shared
1667 * between the stack and a driver, but function supports them all.
1670 if_inc_counter(struct ifnet *ifp, ift_counter cnt, int64_t inc)
1673 KASSERT(cnt < IFCOUNTERS, ("%s: invalid cnt %d", __func__, cnt));
1675 counter_u64_add(ifp->if_counters[cnt], inc);
1679 * Copy data from ifnet to userland API structure if_data.
1682 if_data_copy(struct ifnet *ifp, struct if_data *ifd)
1685 ifd->ifi_type = ifp->if_type;
1686 ifd->ifi_physical = 0;
1687 ifd->ifi_addrlen = ifp->if_addrlen;
1688 ifd->ifi_hdrlen = ifp->if_hdrlen;
1689 ifd->ifi_link_state = ifp->if_link_state;
1691 ifd->ifi_datalen = sizeof(struct if_data);
1692 ifd->ifi_mtu = ifp->if_mtu;
1693 ifd->ifi_metric = ifp->if_metric;
1694 ifd->ifi_baudrate = ifp->if_baudrate;
1695 ifd->ifi_hwassist = ifp->if_hwassist;
1696 ifd->ifi_epoch = ifp->if_epoch;
1697 ifd->ifi_lastchange = ifp->if_lastchange;
1699 ifd->ifi_ipackets = ifp->if_get_counter(ifp, IFCOUNTER_IPACKETS);
1700 ifd->ifi_ierrors = ifp->if_get_counter(ifp, IFCOUNTER_IERRORS);
1701 ifd->ifi_opackets = ifp->if_get_counter(ifp, IFCOUNTER_OPACKETS);
1702 ifd->ifi_oerrors = ifp->if_get_counter(ifp, IFCOUNTER_OERRORS);
1703 ifd->ifi_collisions = ifp->if_get_counter(ifp, IFCOUNTER_COLLISIONS);
1704 ifd->ifi_ibytes = ifp->if_get_counter(ifp, IFCOUNTER_IBYTES);
1705 ifd->ifi_obytes = ifp->if_get_counter(ifp, IFCOUNTER_OBYTES);
1706 ifd->ifi_imcasts = ifp->if_get_counter(ifp, IFCOUNTER_IMCASTS);
1707 ifd->ifi_omcasts = ifp->if_get_counter(ifp, IFCOUNTER_OMCASTS);
1708 ifd->ifi_iqdrops = ifp->if_get_counter(ifp, IFCOUNTER_IQDROPS);
1709 ifd->ifi_oqdrops = ifp->if_get_counter(ifp, IFCOUNTER_OQDROPS);
1710 ifd->ifi_noproto = ifp->if_get_counter(ifp, IFCOUNTER_NOPROTO);
1714 * Wrapper functions for struct ifnet address list locking macros. These are
1715 * used by kernel modules to avoid encoding programming interface or binary
1716 * interface assumptions that may be violated when kernel-internal locking
1717 * approaches change.
1720 if_addr_rlock(struct ifnet *ifp)
1727 if_addr_runlock(struct ifnet *ifp)
1730 IF_ADDR_RUNLOCK(ifp);
1734 if_maddr_rlock(if_t ifp)
1737 IF_ADDR_RLOCK((struct ifnet *)ifp);
1741 if_maddr_runlock(if_t ifp)
1744 IF_ADDR_RUNLOCK((struct ifnet *)ifp);
1748 * Initialization, destruction and refcounting functions for ifaddrs.
1751 ifa_alloc(size_t size, int flags)
1755 KASSERT(size >= sizeof(struct ifaddr),
1756 ("%s: invalid size %zu", __func__, size));
1758 ifa = malloc(size, M_IFADDR, M_ZERO | flags);
1762 if ((ifa->ifa_opackets = counter_u64_alloc(flags)) == NULL)
1764 if ((ifa->ifa_ipackets = counter_u64_alloc(flags)) == NULL)
1766 if ((ifa->ifa_obytes = counter_u64_alloc(flags)) == NULL)
1768 if ((ifa->ifa_ibytes = counter_u64_alloc(flags)) == NULL)
1771 refcount_init(&ifa->ifa_refcnt, 1);
1776 /* free(NULL) is okay */
1777 counter_u64_free(ifa->ifa_opackets);
1778 counter_u64_free(ifa->ifa_ipackets);
1779 counter_u64_free(ifa->ifa_obytes);
1780 counter_u64_free(ifa->ifa_ibytes);
1781 free(ifa, M_IFADDR);
1787 ifa_ref(struct ifaddr *ifa)
1790 refcount_acquire(&ifa->ifa_refcnt);
1794 ifa_free(struct ifaddr *ifa)
1797 if (refcount_release(&ifa->ifa_refcnt)) {
1798 counter_u64_free(ifa->ifa_opackets);
1799 counter_u64_free(ifa->ifa_ipackets);
1800 counter_u64_free(ifa->ifa_obytes);
1801 counter_u64_free(ifa->ifa_ibytes);
1802 free(ifa, M_IFADDR);
1807 ifa_maintain_loopback_route(int cmd, const char *otype, struct ifaddr *ifa,
1808 struct sockaddr *ia)
1811 struct rt_addrinfo info;
1812 struct sockaddr_dl null_sdl;
1817 bzero(&info, sizeof(info));
1818 if (cmd != RTM_DELETE)
1819 info.rti_ifp = V_loif;
1820 info.rti_flags = ifa->ifa_flags | RTF_HOST | RTF_STATIC | RTF_PINNED;
1821 info.rti_info[RTAX_DST] = ia;
1822 info.rti_info[RTAX_GATEWAY] = (struct sockaddr *)&null_sdl;
1823 link_init_sdl(ifp, (struct sockaddr *)&null_sdl, ifp->if_type);
1825 error = rtrequest1_fib(cmd, &info, NULL, ifp->if_fib);
1828 log(LOG_DEBUG, "%s: %s failed for interface %s: %u\n",
1829 __func__, otype, if_name(ifp), error);
1835 ifa_add_loopback_route(struct ifaddr *ifa, struct sockaddr *ia)
1838 return (ifa_maintain_loopback_route(RTM_ADD, "insertion", ifa, ia));
1842 ifa_del_loopback_route(struct ifaddr *ifa, struct sockaddr *ia)
1845 return (ifa_maintain_loopback_route(RTM_DELETE, "deletion", ifa, ia));
1849 ifa_switch_loopback_route(struct ifaddr *ifa, struct sockaddr *ia)
1852 return (ifa_maintain_loopback_route(RTM_CHANGE, "switch", ifa, ia));
1856 * XXX: Because sockaddr_dl has deeper structure than the sockaddr
1857 * structs used to represent other address families, it is necessary
1858 * to perform a different comparison.
1861 #define sa_dl_equal(a1, a2) \
1862 ((((const struct sockaddr_dl *)(a1))->sdl_len == \
1863 ((const struct sockaddr_dl *)(a2))->sdl_len) && \
1864 (bcmp(CLLADDR((const struct sockaddr_dl *)(a1)), \
1865 CLLADDR((const struct sockaddr_dl *)(a2)), \
1866 ((const struct sockaddr_dl *)(a1))->sdl_alen) == 0))
1869 * Locate an interface based on a complete address.
1872 static struct ifaddr *
1873 ifa_ifwithaddr_internal(const struct sockaddr *addr, int getref)
1878 IFNET_RLOCK_NOSLEEP();
1879 TAILQ_FOREACH(ifp, &V_ifnet, if_link) {
1881 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
1882 if (ifa->ifa_addr->sa_family != addr->sa_family)
1884 if (sa_equal(addr, ifa->ifa_addr)) {
1887 IF_ADDR_RUNLOCK(ifp);
1890 /* IP6 doesn't have broadcast */
1891 if ((ifp->if_flags & IFF_BROADCAST) &&
1892 ifa->ifa_broadaddr &&
1893 ifa->ifa_broadaddr->sa_len != 0 &&
1894 sa_equal(ifa->ifa_broadaddr, addr)) {
1897 IF_ADDR_RUNLOCK(ifp);
1901 IF_ADDR_RUNLOCK(ifp);
1905 IFNET_RUNLOCK_NOSLEEP();
1910 ifa_ifwithaddr(const struct sockaddr *addr)
1913 return (ifa_ifwithaddr_internal(addr, 1));
1917 ifa_ifwithaddr_check(const struct sockaddr *addr)
1920 return (ifa_ifwithaddr_internal(addr, 0) != NULL);
1924 * Locate an interface based on the broadcast address.
1928 ifa_ifwithbroadaddr(const struct sockaddr *addr, int fibnum)
1933 IFNET_RLOCK_NOSLEEP();
1934 TAILQ_FOREACH(ifp, &V_ifnet, if_link) {
1935 if ((fibnum != RT_ALL_FIBS) && (ifp->if_fib != fibnum))
1938 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
1939 if (ifa->ifa_addr->sa_family != addr->sa_family)
1941 if ((ifp->if_flags & IFF_BROADCAST) &&
1942 ifa->ifa_broadaddr &&
1943 ifa->ifa_broadaddr->sa_len != 0 &&
1944 sa_equal(ifa->ifa_broadaddr, addr)) {
1946 IF_ADDR_RUNLOCK(ifp);
1950 IF_ADDR_RUNLOCK(ifp);
1954 IFNET_RUNLOCK_NOSLEEP();
1959 * Locate the point to point interface with a given destination address.
1963 ifa_ifwithdstaddr(const struct sockaddr *addr, int fibnum)
1968 IFNET_RLOCK_NOSLEEP();
1969 TAILQ_FOREACH(ifp, &V_ifnet, if_link) {
1970 if ((ifp->if_flags & IFF_POINTOPOINT) == 0)
1972 if ((fibnum != RT_ALL_FIBS) && (ifp->if_fib != fibnum))
1975 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
1976 if (ifa->ifa_addr->sa_family != addr->sa_family)
1978 if (ifa->ifa_dstaddr != NULL &&
1979 sa_equal(addr, ifa->ifa_dstaddr)) {
1981 IF_ADDR_RUNLOCK(ifp);
1985 IF_ADDR_RUNLOCK(ifp);
1989 IFNET_RUNLOCK_NOSLEEP();
1994 * Find an interface on a specific network. If many, choice
1995 * is most specific found.
1998 ifa_ifwithnet(const struct sockaddr *addr, int ignore_ptp, int fibnum)
2002 struct ifaddr *ifa_maybe = NULL;
2003 u_int af = addr->sa_family;
2004 const char *addr_data = addr->sa_data, *cplim;
2007 * AF_LINK addresses can be looked up directly by their index number,
2008 * so do that if we can.
2010 if (af == AF_LINK) {
2011 const struct sockaddr_dl *sdl = (const struct sockaddr_dl *)addr;
2012 if (sdl->sdl_index && sdl->sdl_index <= V_if_index)
2013 return (ifaddr_byindex(sdl->sdl_index));
2017 * Scan though each interface, looking for ones that have addresses
2018 * in this address family and the requested fib. Maintain a reference
2019 * on ifa_maybe once we find one, as we release the IF_ADDR_RLOCK() that
2020 * kept it stable when we move onto the next interface.
2022 IFNET_RLOCK_NOSLEEP();
2023 TAILQ_FOREACH(ifp, &V_ifnet, if_link) {
2024 if ((fibnum != RT_ALL_FIBS) && (ifp->if_fib != fibnum))
2027 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
2028 const char *cp, *cp2, *cp3;
2030 if (ifa->ifa_addr->sa_family != af)
2032 if (af == AF_INET &&
2033 ifp->if_flags & IFF_POINTOPOINT && !ignore_ptp) {
2035 * This is a bit broken as it doesn't
2036 * take into account that the remote end may
2037 * be a single node in the network we are
2039 * The trouble is that we don't know the
2040 * netmask for the remote end.
2042 if (ifa->ifa_dstaddr != NULL &&
2043 sa_equal(addr, ifa->ifa_dstaddr)) {
2045 IF_ADDR_RUNLOCK(ifp);
2050 * Scan all the bits in the ifa's address.
2051 * If a bit dissagrees with what we are
2052 * looking for, mask it with the netmask
2053 * to see if it really matters.
2054 * (A byte at a time)
2056 if (ifa->ifa_netmask == 0)
2059 cp2 = ifa->ifa_addr->sa_data;
2060 cp3 = ifa->ifa_netmask->sa_data;
2061 cplim = ifa->ifa_netmask->sa_len
2062 + (char *)ifa->ifa_netmask;
2064 if ((*cp++ ^ *cp2++) & *cp3++)
2065 goto next; /* next address! */
2067 * If the netmask of what we just found
2068 * is more specific than what we had before
2069 * (if we had one), or if the virtual status
2070 * of new prefix is better than of the old one,
2071 * then remember the new one before continuing
2072 * to search for an even better one.
2074 if (ifa_maybe == NULL ||
2075 ifa_preferred(ifa_maybe, ifa) ||
2076 rn_refines((caddr_t)ifa->ifa_netmask,
2077 (caddr_t)ifa_maybe->ifa_netmask)) {
2078 if (ifa_maybe != NULL)
2079 ifa_free(ifa_maybe);
2085 IF_ADDR_RUNLOCK(ifp);
2090 IFNET_RUNLOCK_NOSLEEP();
2091 if (ifa_maybe != NULL)
2092 ifa_free(ifa_maybe);
2097 * Find an interface address specific to an interface best matching
2101 ifaof_ifpforaddr(const struct sockaddr *addr, struct ifnet *ifp)
2104 const char *cp, *cp2, *cp3;
2106 struct ifaddr *ifa_maybe = NULL;
2107 u_int af = addr->sa_family;
2112 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
2113 if (ifa->ifa_addr->sa_family != af)
2115 if (ifa_maybe == NULL)
2117 if (ifa->ifa_netmask == 0) {
2118 if (sa_equal(addr, ifa->ifa_addr) ||
2119 (ifa->ifa_dstaddr &&
2120 sa_equal(addr, ifa->ifa_dstaddr)))
2124 if (ifp->if_flags & IFF_POINTOPOINT) {
2125 if (sa_equal(addr, ifa->ifa_dstaddr))
2129 cp2 = ifa->ifa_addr->sa_data;
2130 cp3 = ifa->ifa_netmask->sa_data;
2131 cplim = ifa->ifa_netmask->sa_len + (char *)ifa->ifa_netmask;
2132 for (; cp3 < cplim; cp3++)
2133 if ((*cp++ ^ *cp2++) & *cp3)
2143 IF_ADDR_RUNLOCK(ifp);
2148 * See whether new ifa is better than current one:
2149 * 1) A non-virtual one is preferred over virtual.
2150 * 2) A virtual in master state preferred over any other state.
2152 * Used in several address selecting functions.
2155 ifa_preferred(struct ifaddr *cur, struct ifaddr *next)
2158 return (cur->ifa_carp && (!next->ifa_carp ||
2159 ((*carp_master_p)(next) && !(*carp_master_p)(cur))));
2162 #include <net/if_llatbl.h>
2165 * Default action when installing a route with a Link Level gateway.
2166 * Lookup an appropriate real ifa to point to.
2167 * This should be moved to /sys/net/link.c eventually.
2170 link_rtrequest(int cmd, struct rtentry *rt, struct rt_addrinfo *info)
2172 struct ifaddr *ifa, *oifa;
2173 struct sockaddr *dst;
2176 if (cmd != RTM_ADD || ((ifa = rt->rt_ifa) == NULL) ||
2177 ((ifp = ifa->ifa_ifp) == NULL) || ((dst = rt_key(rt)) == NULL))
2179 ifa = ifaof_ifpforaddr(dst, ifp);
2184 if (ifa->ifa_rtrequest && ifa->ifa_rtrequest != link_rtrequest)
2185 ifa->ifa_rtrequest(cmd, rt, info);
2189 struct sockaddr_dl *
2190 link_alloc_sdl(size_t size, int flags)
2193 return (malloc(size, M_TEMP, flags));
2197 link_free_sdl(struct sockaddr *sa)
2203 * Fills in given sdl with interface basic info.
2204 * Returns pointer to filled sdl.
2206 struct sockaddr_dl *
2207 link_init_sdl(struct ifnet *ifp, struct sockaddr *paddr, u_char iftype)
2209 struct sockaddr_dl *sdl;
2211 sdl = (struct sockaddr_dl *)paddr;
2212 memset(sdl, 0, sizeof(struct sockaddr_dl));
2213 sdl->sdl_len = sizeof(struct sockaddr_dl);
2214 sdl->sdl_family = AF_LINK;
2215 sdl->sdl_index = ifp->if_index;
2216 sdl->sdl_type = iftype;
2222 * Mark an interface down and notify protocols of
2226 if_unroute(struct ifnet *ifp, int flag, int fam)
2230 KASSERT(flag == IFF_UP, ("if_unroute: flag != IFF_UP"));
2232 ifp->if_flags &= ~flag;
2233 getmicrotime(&ifp->if_lastchange);
2234 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link)
2235 if (fam == PF_UNSPEC || (fam == ifa->ifa_addr->sa_family))
2236 pfctlinput(PRC_IFDOWN, ifa->ifa_addr);
2237 ifp->if_qflush(ifp);
2240 (*carp_linkstate_p)(ifp);
2245 * Mark an interface up and notify protocols of
2249 if_route(struct ifnet *ifp, int flag, int fam)
2253 KASSERT(flag == IFF_UP, ("if_route: flag != IFF_UP"));
2255 ifp->if_flags |= flag;
2256 getmicrotime(&ifp->if_lastchange);
2257 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link)
2258 if (fam == PF_UNSPEC || (fam == ifa->ifa_addr->sa_family))
2259 pfctlinput(PRC_IFUP, ifa->ifa_addr);
2261 (*carp_linkstate_p)(ifp);
2268 void (*vlan_link_state_p)(struct ifnet *); /* XXX: private from if_vlan */
2269 void (*vlan_trunk_cap_p)(struct ifnet *); /* XXX: private from if_vlan */
2270 struct ifnet *(*vlan_trunkdev_p)(struct ifnet *);
2271 struct ifnet *(*vlan_devat_p)(struct ifnet *, uint16_t);
2272 int (*vlan_tag_p)(struct ifnet *, uint16_t *);
2273 int (*vlan_setcookie_p)(struct ifnet *, void *);
2274 void *(*vlan_cookie_p)(struct ifnet *);
2277 * Handle a change in the interface link state. To avoid LORs
2278 * between driver lock and upper layer locks, as well as possible
2279 * recursions, we post event to taskqueue, and all job
2280 * is done in static do_link_state_change().
2283 if_link_state_change(struct ifnet *ifp, int link_state)
2285 /* Return if state hasn't changed. */
2286 if (ifp->if_link_state == link_state)
2289 ifp->if_link_state = link_state;
2291 taskqueue_enqueue(taskqueue_swi, &ifp->if_linktask);
2295 do_link_state_change(void *arg, int pending)
2297 struct ifnet *ifp = (struct ifnet *)arg;
2298 int link_state = ifp->if_link_state;
2299 CURVNET_SET(ifp->if_vnet);
2301 /* Notify that the link state has changed. */
2303 if (ifp->if_vlantrunk != NULL)
2304 (*vlan_link_state_p)(ifp);
2306 if ((ifp->if_type == IFT_ETHER || ifp->if_type == IFT_L2VLAN) &&
2307 ifp->if_l2com != NULL)
2308 (*ng_ether_link_state_p)(ifp, link_state);
2310 (*carp_linkstate_p)(ifp);
2312 (*bridge_linkstate_p)(ifp);
2314 (*lagg_linkstate_p)(ifp, link_state);
2316 if (IS_DEFAULT_VNET(curvnet))
2317 devctl_notify("IFNET", ifp->if_xname,
2318 (link_state == LINK_STATE_UP) ? "LINK_UP" : "LINK_DOWN",
2321 if_printf(ifp, "%d link states coalesced\n", pending);
2322 if (log_link_state_change)
2323 log(LOG_NOTICE, "%s: link state changed to %s\n", ifp->if_xname,
2324 (link_state == LINK_STATE_UP) ? "UP" : "DOWN" );
2325 EVENTHANDLER_INVOKE(ifnet_link_event, ifp, link_state);
2330 * Mark an interface down and notify protocols of
2334 if_down(struct ifnet *ifp)
2337 EVENTHANDLER_INVOKE(ifnet_event, ifp, IFNET_EVENT_DOWN);
2338 if_unroute(ifp, IFF_UP, AF_UNSPEC);
2342 * Mark an interface up and notify protocols of
2346 if_up(struct ifnet *ifp)
2349 if_route(ifp, IFF_UP, AF_UNSPEC);
2350 EVENTHANDLER_INVOKE(ifnet_event, ifp, IFNET_EVENT_UP);
2354 * Flush an interface queue.
2357 if_qflush(struct ifnet *ifp)
2365 if (ALTQ_IS_ENABLED(ifq))
2369 while ((m = n) != NULL) {
2380 * Map interface name to interface structure pointer, with or without
2381 * returning a reference.
2384 ifunit_ref(const char *name)
2388 IFNET_RLOCK_NOSLEEP();
2389 TAILQ_FOREACH(ifp, &V_ifnet, if_link) {
2390 if (strncmp(name, ifp->if_xname, IFNAMSIZ) == 0 &&
2391 !(ifp->if_flags & IFF_DYING))
2396 IFNET_RUNLOCK_NOSLEEP();
2401 ifunit(const char *name)
2405 IFNET_RLOCK_NOSLEEP();
2406 TAILQ_FOREACH(ifp, &V_ifnet, if_link) {
2407 if (strncmp(name, ifp->if_xname, IFNAMSIZ) == 0)
2410 IFNET_RUNLOCK_NOSLEEP();
2415 ifr_buffer_get_buffer(void *data)
2417 union ifreq_union *ifrup;
2420 #ifdef COMPAT_FREEBSD32
2421 if (SV_CURPROC_FLAG(SV_ILP32))
2422 return ((void *)(uintptr_t)
2423 ifrup->ifr32.ifr_ifru.ifru_buffer.buffer);
2425 return (ifrup->ifr.ifr_ifru.ifru_buffer.buffer);
2429 ifr_buffer_set_buffer_null(void *data)
2431 union ifreq_union *ifrup;
2434 #ifdef COMPAT_FREEBSD32
2435 if (SV_CURPROC_FLAG(SV_ILP32))
2436 ifrup->ifr32.ifr_ifru.ifru_buffer.buffer = 0;
2439 ifrup->ifr.ifr_ifru.ifru_buffer.buffer = NULL;
2443 ifr_buffer_get_length(void *data)
2445 union ifreq_union *ifrup;
2448 #ifdef COMPAT_FREEBSD32
2449 if (SV_CURPROC_FLAG(SV_ILP32))
2450 return (ifrup->ifr32.ifr_ifru.ifru_buffer.length);
2452 return (ifrup->ifr.ifr_ifru.ifru_buffer.length);
2456 ifr_buffer_set_length(void *data, size_t len)
2458 union ifreq_union *ifrup;
2461 #ifdef COMPAT_FREEBSD32
2462 if (SV_CURPROC_FLAG(SV_ILP32))
2463 ifrup->ifr32.ifr_ifru.ifru_buffer.length = len;
2466 ifrup->ifr.ifr_ifru.ifru_buffer.length = len;
2470 ifr_data_get_ptr(void *ifrp)
2472 union ifreq_union *ifrup;
2475 #ifdef COMPAT_FREEBSD32
2476 if (SV_CURPROC_FLAG(SV_ILP32))
2477 return ((void *)(uintptr_t)
2478 ifrup->ifr32.ifr_ifru.ifru_data);
2480 return (ifrup->ifr.ifr_ifru.ifru_data);
2484 * Hardware specific interface ioctls.
2487 ifhwioctl(u_long cmd, struct ifnet *ifp, caddr_t data, struct thread *td)
2490 int error = 0, do_ifup = 0;
2491 int new_flags, temp_flags;
2492 size_t namelen, onamelen;
2494 char *descrbuf, *odescrbuf;
2495 char new_name[IFNAMSIZ];
2497 struct sockaddr_dl *sdl;
2499 ifr = (struct ifreq *)data;
2502 ifr->ifr_index = ifp->if_index;
2506 temp_flags = ifp->if_flags | ifp->if_drv_flags;
2507 ifr->ifr_flags = temp_flags & 0xffff;
2508 ifr->ifr_flagshigh = temp_flags >> 16;
2512 ifr->ifr_reqcap = ifp->if_capabilities;
2513 ifr->ifr_curcap = ifp->if_capenable;
2518 error = mac_ifnet_ioctl_get(td->td_ucred, ifr, ifp);
2523 ifr->ifr_metric = ifp->if_metric;
2527 ifr->ifr_mtu = ifp->if_mtu;
2531 /* XXXGL: did this ever worked? */
2537 sx_slock(&ifdescr_sx);
2538 if (ifp->if_description == NULL)
2541 /* space for terminating nul */
2542 descrlen = strlen(ifp->if_description) + 1;
2543 if (ifr_buffer_get_length(ifr) < descrlen)
2544 ifr_buffer_set_buffer_null(ifr);
2546 error = copyout(ifp->if_description,
2547 ifr_buffer_get_buffer(ifr), descrlen);
2548 ifr_buffer_set_length(ifr, descrlen);
2550 sx_sunlock(&ifdescr_sx);
2554 error = priv_check(td, PRIV_NET_SETIFDESCR);
2559 * Copy only (length-1) bytes to make sure that
2560 * if_description is always nul terminated. The
2561 * length parameter is supposed to count the
2562 * terminating nul in.
2564 if (ifr_buffer_get_length(ifr) > ifdescr_maxlen)
2565 return (ENAMETOOLONG);
2566 else if (ifr_buffer_get_length(ifr) == 0)
2569 descrbuf = malloc(ifr_buffer_get_length(ifr),
2570 M_IFDESCR, M_WAITOK | M_ZERO);
2571 error = copyin(ifr_buffer_get_buffer(ifr), descrbuf,
2572 ifr_buffer_get_length(ifr) - 1);
2574 free(descrbuf, M_IFDESCR);
2579 sx_xlock(&ifdescr_sx);
2580 odescrbuf = ifp->if_description;
2581 ifp->if_description = descrbuf;
2582 sx_xunlock(&ifdescr_sx);
2584 getmicrotime(&ifp->if_lastchange);
2585 free(odescrbuf, M_IFDESCR);
2589 ifr->ifr_fib = ifp->if_fib;
2593 error = priv_check(td, PRIV_NET_SETIFFIB);
2596 if (ifr->ifr_fib >= rt_numfibs)
2599 ifp->if_fib = ifr->ifr_fib;
2603 error = priv_check(td, PRIV_NET_SETIFFLAGS);
2607 * Currently, no driver owned flags pass the IFF_CANTCHANGE
2608 * check, so we don't need special handling here yet.
2610 new_flags = (ifr->ifr_flags & 0xffff) |
2611 (ifr->ifr_flagshigh << 16);
2612 if (ifp->if_flags & IFF_UP &&
2613 (new_flags & IFF_UP) == 0) {
2615 } else if (new_flags & IFF_UP &&
2616 (ifp->if_flags & IFF_UP) == 0) {
2619 /* See if permanently promiscuous mode bit is about to flip */
2620 if ((ifp->if_flags ^ new_flags) & IFF_PPROMISC) {
2621 if (new_flags & IFF_PPROMISC)
2622 ifp->if_flags |= IFF_PROMISC;
2623 else if (ifp->if_pcount == 0)
2624 ifp->if_flags &= ~IFF_PROMISC;
2625 if (log_promisc_mode_change)
2626 log(LOG_INFO, "%s: permanently promiscuous mode %s\n",
2628 ((new_flags & IFF_PPROMISC) ?
2629 "enabled" : "disabled"));
2631 ifp->if_flags = (ifp->if_flags & IFF_CANTCHANGE) |
2632 (new_flags &~ IFF_CANTCHANGE);
2633 if (ifp->if_ioctl) {
2634 (void) (*ifp->if_ioctl)(ifp, cmd, data);
2638 getmicrotime(&ifp->if_lastchange);
2642 error = priv_check(td, PRIV_NET_SETIFCAP);
2645 if (ifp->if_ioctl == NULL)
2646 return (EOPNOTSUPP);
2647 if (ifr->ifr_reqcap & ~ifp->if_capabilities)
2649 error = (*ifp->if_ioctl)(ifp, cmd, data);
2651 getmicrotime(&ifp->if_lastchange);
2656 error = mac_ifnet_ioctl_set(td->td_ucred, ifr, ifp);
2661 error = priv_check(td, PRIV_NET_SETIFNAME);
2664 error = copyinstr(ifr_data_get_ptr(ifr), new_name, IFNAMSIZ,
2668 if (new_name[0] == '\0')
2670 if (new_name[IFNAMSIZ-1] != '\0') {
2671 new_name[IFNAMSIZ-1] = '\0';
2672 if (strlen(new_name) == IFNAMSIZ-1)
2675 if (ifunit(new_name) != NULL)
2679 * XXX: Locking. Nothing else seems to lock if_flags,
2680 * and there are numerous other races with the
2681 * ifunit() checks not being atomic with namespace
2682 * changes (renames, vmoves, if_attach, etc).
2684 ifp->if_flags |= IFF_RENAMING;
2686 /* Announce the departure of the interface. */
2687 rt_ifannouncemsg(ifp, IFAN_DEPARTURE);
2688 EVENTHANDLER_INVOKE(ifnet_departure_event, ifp);
2690 log(LOG_INFO, "%s: changing name to '%s'\n",
2691 ifp->if_xname, new_name);
2694 strlcpy(ifp->if_xname, new_name, sizeof(ifp->if_xname));
2696 sdl = (struct sockaddr_dl *)ifa->ifa_addr;
2697 namelen = strlen(new_name);
2698 onamelen = sdl->sdl_nlen;
2700 * Move the address if needed. This is safe because we
2701 * allocate space for a name of length IFNAMSIZ when we
2702 * create this in if_attach().
2704 if (namelen != onamelen) {
2705 bcopy(sdl->sdl_data + onamelen,
2706 sdl->sdl_data + namelen, sdl->sdl_alen);
2708 bcopy(new_name, sdl->sdl_data, namelen);
2709 sdl->sdl_nlen = namelen;
2710 sdl = (struct sockaddr_dl *)ifa->ifa_netmask;
2711 bzero(sdl->sdl_data, onamelen);
2712 while (namelen != 0)
2713 sdl->sdl_data[--namelen] = 0xff;
2714 IF_ADDR_WUNLOCK(ifp);
2716 EVENTHANDLER_INVOKE(ifnet_arrival_event, ifp);
2717 /* Announce the return of the interface. */
2718 rt_ifannouncemsg(ifp, IFAN_ARRIVAL);
2720 ifp->if_flags &= ~IFF_RENAMING;
2725 error = priv_check(td, PRIV_NET_SETIFVNET);
2728 error = if_vmove_loan(td, ifp, ifr->ifr_name, ifr->ifr_jid);
2733 error = priv_check(td, PRIV_NET_SETIFMETRIC);
2736 ifp->if_metric = ifr->ifr_metric;
2737 getmicrotime(&ifp->if_lastchange);
2741 error = priv_check(td, PRIV_NET_SETIFPHYS);
2744 if (ifp->if_ioctl == NULL)
2745 return (EOPNOTSUPP);
2746 error = (*ifp->if_ioctl)(ifp, cmd, data);
2748 getmicrotime(&ifp->if_lastchange);
2753 u_long oldmtu = ifp->if_mtu;
2755 error = priv_check(td, PRIV_NET_SETIFMTU);
2758 if (ifr->ifr_mtu < IF_MINMTU || ifr->ifr_mtu > IF_MAXMTU)
2760 if (ifp->if_ioctl == NULL)
2761 return (EOPNOTSUPP);
2762 error = (*ifp->if_ioctl)(ifp, cmd, data);
2764 getmicrotime(&ifp->if_lastchange);
2768 * If the link MTU changed, do network layer specific procedure.
2770 if (ifp->if_mtu != oldmtu) {
2781 if (cmd == SIOCADDMULTI)
2782 error = priv_check(td, PRIV_NET_ADDMULTI);
2784 error = priv_check(td, PRIV_NET_DELMULTI);
2788 /* Don't allow group membership on non-multicast interfaces. */
2789 if ((ifp->if_flags & IFF_MULTICAST) == 0)
2790 return (EOPNOTSUPP);
2792 /* Don't let users screw up protocols' entries. */
2793 if (ifr->ifr_addr.sa_family != AF_LINK)
2796 if (cmd == SIOCADDMULTI) {
2797 struct ifmultiaddr *ifma;
2800 * Userland is only permitted to join groups once
2801 * via the if_addmulti() KPI, because it cannot hold
2802 * struct ifmultiaddr * between calls. It may also
2803 * lose a race while we check if the membership
2807 ifma = if_findmulti(ifp, &ifr->ifr_addr);
2808 IF_ADDR_RUNLOCK(ifp);
2812 error = if_addmulti(ifp, &ifr->ifr_addr, &ifma);
2814 error = if_delmulti(ifp, &ifr->ifr_addr);
2817 getmicrotime(&ifp->if_lastchange);
2820 case SIOCSIFPHYADDR:
2821 case SIOCDIFPHYADDR:
2823 case SIOCSIFPHYADDR_IN6:
2826 case SIOCSIFGENERIC:
2827 error = priv_check(td, PRIV_NET_HWIOCTL);
2830 if (ifp->if_ioctl == NULL)
2831 return (EOPNOTSUPP);
2832 error = (*ifp->if_ioctl)(ifp, cmd, data);
2834 getmicrotime(&ifp->if_lastchange);
2838 case SIOCGIFPSRCADDR:
2839 case SIOCGIFPDSTADDR:
2842 case SIOCGIFGENERIC:
2844 case SIOCGIFRSSHASH:
2845 if (ifp->if_ioctl == NULL)
2846 return (EOPNOTSUPP);
2847 error = (*ifp->if_ioctl)(ifp, cmd, data);
2851 error = priv_check(td, PRIV_NET_SETLLADDR);
2854 error = if_setlladdr(ifp,
2855 ifr->ifr_addr.sa_data, ifr->ifr_addr.sa_len);
2859 error = if_gethwaddr(ifp, ifr);
2862 CASE_IOC_IFGROUPREQ(SIOCAIFGROUP):
2863 error = priv_check(td, PRIV_NET_ADDIFGROUP);
2866 if ((error = if_addgroup(ifp,
2867 ifgr_group_get((struct ifgroupreq *)data))))
2871 CASE_IOC_IFGROUPREQ(SIOCGIFGROUP):
2872 if ((error = if_getgroup((struct ifgroupreq *)data, ifp)))
2876 CASE_IOC_IFGROUPREQ(SIOCDIFGROUP):
2877 error = priv_check(td, PRIV_NET_DELIFGROUP);
2880 if ((error = if_delgroup(ifp,
2881 ifgr_group_get((struct ifgroupreq *)data))))
2893 #define OSIOCGIFCONF _IOWR('i', 20, struct ifconf)
2895 #ifdef COMPAT_FREEBSD32
2903 #define SIOCGIFCONF32 _IOWR('i', 36, struct ifconf32)
2906 #ifdef COMPAT_FREEBSD32
2908 ifmr_init(struct ifmediareq *ifmr, caddr_t data)
2910 struct ifmediareq32 *ifmr32;
2912 ifmr32 = (struct ifmediareq32 *)data;
2913 memcpy(ifmr->ifm_name, ifmr32->ifm_name,
2914 sizeof(ifmr->ifm_name));
2915 ifmr->ifm_current = ifmr32->ifm_current;
2916 ifmr->ifm_mask = ifmr32->ifm_mask;
2917 ifmr->ifm_status = ifmr32->ifm_status;
2918 ifmr->ifm_active = ifmr32->ifm_active;
2919 ifmr->ifm_count = ifmr32->ifm_count;
2920 ifmr->ifm_ulist = (int *)(uintptr_t)ifmr32->ifm_ulist;
2924 ifmr_update(const struct ifmediareq *ifmr, caddr_t data)
2926 struct ifmediareq32 *ifmr32;
2928 ifmr32 = (struct ifmediareq32 *)data;
2929 ifmr32->ifm_current = ifmr->ifm_current;
2930 ifmr32->ifm_mask = ifmr->ifm_mask;
2931 ifmr32->ifm_status = ifmr->ifm_status;
2932 ifmr32->ifm_active = ifmr->ifm_active;
2933 ifmr32->ifm_count = ifmr->ifm_count;
2941 ifioctl(struct socket *so, u_long cmd, caddr_t data, struct thread *td)
2943 #ifdef COMPAT_FREEBSD32
2945 struct ifmediareq ifmr;
2947 struct ifmediareq *ifmrp;
2956 CURVNET_SET(so->so_vnet);
2958 /* Make sure the VNET is stable. */
2959 shutdown = (so->so_vnet->vnet_state > SI_SUB_VNET &&
2960 so->so_vnet->vnet_state < SI_SUB_VNET_DONE) ? 1 : 0;
2970 case OSIOCGIFCONF: /* COMPAT_SVR4 */
2971 error = ifconf(cmd, data);
2975 #ifdef COMPAT_FREEBSD32
2978 struct ifconf32 *ifc32;
2981 ifc32 = (struct ifconf32 *)data;
2982 ifc.ifc_len = ifc32->ifc_len;
2983 ifc.ifc_buf = PTRIN(ifc32->ifc_buf);
2985 error = ifconf(SIOCGIFCONF, (void *)&ifc);
2988 ifc32->ifc_len = ifc.ifc_len;
2995 #ifdef COMPAT_FREEBSD32
2997 case SIOCGIFMEDIA32:
2998 case SIOCGIFXMEDIA32:
3000 ifmr_init(ifmrp, data);
3001 cmd = _IOC_NEWTYPE(cmd, struct ifmediareq);
3003 data = (caddr_t)ifmrp;
3007 ifr = (struct ifreq *)data;
3011 error = priv_check(td, PRIV_NET_SETIFVNET);
3013 error = if_vmove_reclaim(td, ifr->ifr_name,
3019 error = priv_check(td, PRIV_NET_IFCREATE);
3021 error = if_clone_create(ifr->ifr_name,
3022 sizeof(ifr->ifr_name), cmd == SIOCIFCREATE2 ?
3023 ifr_data_get_ptr(ifr) : NULL);
3026 error = priv_check(td, PRIV_NET_IFDESTROY);
3028 error = if_clone_destroy(ifr->ifr_name);
3031 case SIOCIFGCLONERS:
3032 error = if_clone_list((struct if_clonereq *)data);
3035 CASE_IOC_IFGROUPREQ(SIOCGIFGMEMB):
3036 error = if_getgroupmembers((struct ifgroupreq *)data);
3039 #if defined(INET) || defined(INET6)
3042 if (carp_ioctl_p == NULL)
3043 error = EPROTONOSUPPORT;
3045 error = (*carp_ioctl_p)(ifr, cmd, td);
3050 ifp = ifunit_ref(ifr->ifr_name);
3056 error = ifhwioctl(cmd, ifp, data, td);
3057 if (error != ENOIOCTL)
3060 oif_flags = ifp->if_flags;
3061 if (so->so_proto == NULL) {
3067 * Pass the request on to the socket control method, and if the
3068 * latter returns EOPNOTSUPP, directly to the interface.
3070 * Make an exception for the legacy SIOCSIF* requests. Drivers
3071 * trust SIOCSIFADDR et al to come from an already privileged
3072 * layer, and do not perform any credentials checks or input
3075 error = ((*so->so_proto->pr_usrreqs->pru_control)(so, cmd, data,
3077 if (error == EOPNOTSUPP && ifp != NULL && ifp->if_ioctl != NULL &&
3078 cmd != SIOCSIFADDR && cmd != SIOCSIFBRDADDR &&
3079 cmd != SIOCSIFDSTADDR && cmd != SIOCSIFNETMASK)
3080 error = (*ifp->if_ioctl)(ifp, cmd, data);
3082 if ((oif_flags ^ ifp->if_flags) & IFF_UP) {
3084 if (ifp->if_flags & IFF_UP)
3092 #ifdef COMPAT_FREEBSD32
3093 if (ifmrp != NULL) {
3094 KASSERT((cmd == SIOCGIFMEDIA || cmd == SIOCGIFXMEDIA),
3095 ("ifmrp non-NULL, but cmd is not an ifmedia req 0x%lx",
3098 ifmr_update(ifmrp, data);
3106 * The code common to handling reference counted flags,
3107 * e.g., in ifpromisc() and if_allmulti().
3108 * The "pflag" argument can specify a permanent mode flag to check,
3109 * such as IFF_PPROMISC for promiscuous mode; should be 0 if none.
3111 * Only to be used on stack-owned flags, not driver-owned flags.
3114 if_setflag(struct ifnet *ifp, int flag, int pflag, int *refcount, int onswitch)
3118 int oldflags, oldcount;
3120 /* Sanity checks to catch programming errors */
3121 KASSERT((flag & (IFF_DRV_OACTIVE|IFF_DRV_RUNNING)) == 0,
3122 ("%s: setting driver-owned flag %d", __func__, flag));
3125 KASSERT(*refcount >= 0,
3126 ("%s: increment negative refcount %d for flag %d",
3127 __func__, *refcount, flag));
3129 KASSERT(*refcount > 0,
3130 ("%s: decrement non-positive refcount %d for flag %d",
3131 __func__, *refcount, flag));
3133 /* In case this mode is permanent, just touch refcount */
3134 if (ifp->if_flags & pflag) {
3135 *refcount += onswitch ? 1 : -1;
3139 /* Save ifnet parameters for if_ioctl() may fail */
3140 oldcount = *refcount;
3141 oldflags = ifp->if_flags;
3144 * See if we aren't the only and touching refcount is enough.
3145 * Actually toggle interface flag if we are the first or last.
3150 ifp->if_flags |= flag;
3154 ifp->if_flags &= ~flag;
3157 /* Call down the driver since we've changed interface flags */
3158 if (ifp->if_ioctl == NULL) {
3162 ifr.ifr_flags = ifp->if_flags & 0xffff;
3163 ifr.ifr_flagshigh = ifp->if_flags >> 16;
3164 error = (*ifp->if_ioctl)(ifp, SIOCSIFFLAGS, (caddr_t)&ifr);
3167 /* Notify userland that interface flags have changed */
3172 /* Recover after driver error */
3173 *refcount = oldcount;
3174 ifp->if_flags = oldflags;
3179 * Set/clear promiscuous mode on interface ifp based on the truth value
3180 * of pswitch. The calls are reference counted so that only the first
3181 * "on" request actually has an effect, as does the final "off" request.
3182 * Results are undefined if the "off" and "on" requests are not matched.
3185 ifpromisc(struct ifnet *ifp, int pswitch)
3188 int oldflags = ifp->if_flags;
3190 error = if_setflag(ifp, IFF_PROMISC, IFF_PPROMISC,
3191 &ifp->if_pcount, pswitch);
3192 /* If promiscuous mode status has changed, log a message */
3193 if (error == 0 && ((ifp->if_flags ^ oldflags) & IFF_PROMISC) &&
3194 log_promisc_mode_change)
3195 log(LOG_INFO, "%s: promiscuous mode %s\n",
3197 (ifp->if_flags & IFF_PROMISC) ? "enabled" : "disabled");
3202 * Return interface configuration
3203 * of system. List may be used
3204 * in later ioctl's (above) to get
3205 * other information.
3209 ifconf(u_long cmd, caddr_t data)
3211 struct ifconf *ifc = (struct ifconf *)data;
3216 int error, full = 0, valid_len, max_len;
3218 /* Limit initial buffer size to MAXPHYS to avoid DoS from userspace. */
3219 max_len = MAXPHYS - 1;
3221 /* Prevent hostile input from being able to crash the system */
3222 if (ifc->ifc_len <= 0)
3226 if (ifc->ifc_len <= max_len) {
3227 max_len = ifc->ifc_len;
3230 sb = sbuf_new(NULL, NULL, max_len + 1, SBUF_FIXEDLEN);
3235 TAILQ_FOREACH(ifp, &V_ifnet, if_link) {
3239 * Zero the ifr to make sure we don't disclose the contents
3242 memset(&ifr, 0, sizeof(ifr));
3244 if (strlcpy(ifr.ifr_name, ifp->if_xname, sizeof(ifr.ifr_name))
3245 >= sizeof(ifr.ifr_name)) {
3248 return (ENAMETOOLONG);
3253 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
3254 struct sockaddr *sa = ifa->ifa_addr;
3256 if (prison_if(curthread->td_ucred, sa) != 0)
3260 if (cmd == OSIOCGIFCONF) {
3261 struct osockaddr *osa =
3262 (struct osockaddr *)&ifr.ifr_addr;
3264 osa->sa_family = sa->sa_family;
3265 sbuf_bcat(sb, &ifr, sizeof(ifr));
3266 max_len += sizeof(ifr);
3268 if (sa->sa_len <= sizeof(*sa)) {
3269 if (sa->sa_len < sizeof(*sa)) {
3270 memset(&ifr.ifr_ifru.ifru_addr, 0,
3271 sizeof(ifr.ifr_ifru.ifru_addr));
3272 memcpy(&ifr.ifr_ifru.ifru_addr, sa,
3275 ifr.ifr_ifru.ifru_addr = *sa;
3276 sbuf_bcat(sb, &ifr, sizeof(ifr));
3277 max_len += sizeof(ifr);
3280 offsetof(struct ifreq, ifr_addr));
3281 max_len += offsetof(struct ifreq, ifr_addr);
3282 sbuf_bcat(sb, sa, sa->sa_len);
3283 max_len += sa->sa_len;
3286 if (sbuf_error(sb) == 0)
3287 valid_len = sbuf_len(sb);
3289 IF_ADDR_RUNLOCK(ifp);
3291 sbuf_bcat(sb, &ifr, sizeof(ifr));
3292 max_len += sizeof(ifr);
3294 if (sbuf_error(sb) == 0)
3295 valid_len = sbuf_len(sb);
3301 * If we didn't allocate enough space (uncommon), try again. If
3302 * we have already allocated as much space as we are allowed,
3303 * return what we've got.
3305 if (valid_len != max_len && !full) {
3310 ifc->ifc_len = valid_len;
3312 error = copyout(sbuf_data(sb), ifc->ifc_req, ifc->ifc_len);
3318 * Just like ifpromisc(), but for all-multicast-reception mode.
3321 if_allmulti(struct ifnet *ifp, int onswitch)
3324 return (if_setflag(ifp, IFF_ALLMULTI, 0, &ifp->if_amcount, onswitch));
3327 struct ifmultiaddr *
3328 if_findmulti(struct ifnet *ifp, const struct sockaddr *sa)
3330 struct ifmultiaddr *ifma;
3332 IF_ADDR_LOCK_ASSERT(ifp);
3334 TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
3335 if (sa->sa_family == AF_LINK) {
3336 if (sa_dl_equal(ifma->ifma_addr, sa))
3339 if (sa_equal(ifma->ifma_addr, sa))
3348 * Allocate a new ifmultiaddr and initialize based on passed arguments. We
3349 * make copies of passed sockaddrs. The ifmultiaddr will not be added to
3350 * the ifnet multicast address list here, so the caller must do that and
3351 * other setup work (such as notifying the device driver). The reference
3352 * count is initialized to 1.
3354 static struct ifmultiaddr *
3355 if_allocmulti(struct ifnet *ifp, struct sockaddr *sa, struct sockaddr *llsa,
3358 struct ifmultiaddr *ifma;
3359 struct sockaddr *dupsa;
3361 ifma = malloc(sizeof *ifma, M_IFMADDR, mflags |
3366 dupsa = malloc(sa->sa_len, M_IFMADDR, mflags);
3367 if (dupsa == NULL) {
3368 free(ifma, M_IFMADDR);
3371 bcopy(sa, dupsa, sa->sa_len);
3372 ifma->ifma_addr = dupsa;
3374 ifma->ifma_ifp = ifp;
3375 ifma->ifma_refcount = 1;
3376 ifma->ifma_protospec = NULL;
3379 ifma->ifma_lladdr = NULL;
3383 dupsa = malloc(llsa->sa_len, M_IFMADDR, mflags);
3384 if (dupsa == NULL) {
3385 free(ifma->ifma_addr, M_IFMADDR);
3386 free(ifma, M_IFMADDR);
3389 bcopy(llsa, dupsa, llsa->sa_len);
3390 ifma->ifma_lladdr = dupsa;
3396 * if_freemulti: free ifmultiaddr structure and possibly attached related
3397 * addresses. The caller is responsible for implementing reference
3398 * counting, notifying the driver, handling routing messages, and releasing
3399 * any dependent link layer state.
3402 if_freemulti(struct ifmultiaddr *ifma)
3405 KASSERT(ifma->ifma_refcount == 0, ("if_freemulti: refcount %d",
3406 ifma->ifma_refcount));
3408 if (ifma->ifma_lladdr != NULL)
3409 free(ifma->ifma_lladdr, M_IFMADDR);
3410 free(ifma->ifma_addr, M_IFMADDR);
3411 free(ifma, M_IFMADDR);
3415 * Register an additional multicast address with a network interface.
3417 * - If the address is already present, bump the reference count on the
3418 * address and return.
3419 * - If the address is not link-layer, look up a link layer address.
3420 * - Allocate address structures for one or both addresses, and attach to the
3421 * multicast address list on the interface. If automatically adding a link
3422 * layer address, the protocol address will own a reference to the link
3423 * layer address, to be freed when it is freed.
3424 * - Notify the network device driver of an addition to the multicast address
3427 * 'sa' points to caller-owned memory with the desired multicast address.
3429 * 'retifma' will be used to return a pointer to the resulting multicast
3430 * address reference, if desired.
3433 if_addmulti(struct ifnet *ifp, struct sockaddr *sa,
3434 struct ifmultiaddr **retifma)
3436 struct ifmultiaddr *ifma, *ll_ifma;
3437 struct sockaddr *llsa;
3438 struct sockaddr_dl sdl;
3442 * If the address is already present, return a new reference to it;
3443 * otherwise, allocate storage and set up a new address.
3446 ifma = if_findmulti(ifp, sa);
3448 ifma->ifma_refcount++;
3449 if (retifma != NULL)
3451 IF_ADDR_WUNLOCK(ifp);
3456 * The address isn't already present; resolve the protocol address
3457 * into a link layer address, and then look that up, bump its
3458 * refcount or allocate an ifma for that also.
3459 * Most link layer resolving functions returns address data which
3460 * fits inside default sockaddr_dl structure. However callback
3461 * can allocate another sockaddr structure, in that case we need to
3466 if (ifp->if_resolvemulti != NULL) {
3467 /* Provide called function with buffer size information */
3468 sdl.sdl_len = sizeof(sdl);
3469 llsa = (struct sockaddr *)&sdl;
3470 error = ifp->if_resolvemulti(ifp, &llsa, sa);
3476 * Allocate the new address. Don't hook it up yet, as we may also
3477 * need to allocate a link layer multicast address.
3479 ifma = if_allocmulti(ifp, sa, llsa, M_NOWAIT);
3486 * If a link layer address is found, we'll need to see if it's
3487 * already present in the address list, or allocate is as well.
3488 * When this block finishes, the link layer address will be on the
3492 ll_ifma = if_findmulti(ifp, llsa);
3493 if (ll_ifma == NULL) {
3494 ll_ifma = if_allocmulti(ifp, llsa, NULL, M_NOWAIT);
3495 if (ll_ifma == NULL) {
3496 --ifma->ifma_refcount;
3501 TAILQ_INSERT_HEAD(&ifp->if_multiaddrs, ll_ifma,
3504 ll_ifma->ifma_refcount++;
3505 ifma->ifma_llifma = ll_ifma;
3509 * We now have a new multicast address, ifma, and possibly a new or
3510 * referenced link layer address. Add the primary address to the
3511 * ifnet address list.
3513 TAILQ_INSERT_HEAD(&ifp->if_multiaddrs, ifma, ifma_link);
3515 if (retifma != NULL)
3519 * Must generate the message while holding the lock so that 'ifma'
3520 * pointer is still valid.
3522 rt_newmaddrmsg(RTM_NEWMADDR, ifma);
3523 IF_ADDR_WUNLOCK(ifp);
3526 * We are certain we have added something, so call down to the
3527 * interface to let them know about it.
3529 if (ifp->if_ioctl != NULL) {
3530 (void) (*ifp->if_ioctl)(ifp, SIOCADDMULTI, 0);
3533 if ((llsa != NULL) && (llsa != (struct sockaddr *)&sdl))
3534 link_free_sdl(llsa);
3539 if ((llsa != NULL) && (llsa != (struct sockaddr *)&sdl))
3540 link_free_sdl(llsa);
3543 IF_ADDR_WUNLOCK(ifp);
3548 * Delete a multicast group membership by network-layer group address.
3550 * Returns ENOENT if the entry could not be found. If ifp no longer
3551 * exists, results are undefined. This entry point should only be used
3552 * from subsystems which do appropriate locking to hold ifp for the
3553 * duration of the call.
3554 * Network-layer protocol domains must use if_delmulti_ifma().
3557 if_delmulti(struct ifnet *ifp, struct sockaddr *sa)
3559 struct ifmultiaddr *ifma;
3564 IFNET_RLOCK_NOSLEEP();
3565 TAILQ_FOREACH(oifp, &V_ifnet, if_link)
3570 IFNET_RUNLOCK_NOSLEEP();
3572 KASSERT(ifp != NULL, ("%s: ifnet went away", __func__));
3579 ifma = if_findmulti(ifp, sa);
3581 lastref = if_delmulti_locked(ifp, ifma, 0);
3582 IF_ADDR_WUNLOCK(ifp);
3587 if (lastref && ifp->if_ioctl != NULL) {
3588 (void)(*ifp->if_ioctl)(ifp, SIOCDELMULTI, 0);
3595 * Delete all multicast group membership for an interface.
3596 * Should be used to quickly flush all multicast filters.
3599 if_delallmulti(struct ifnet *ifp)
3601 struct ifmultiaddr *ifma;
3602 struct ifmultiaddr *next;
3605 TAILQ_FOREACH_SAFE(ifma, &ifp->if_multiaddrs, ifma_link, next)
3606 if_delmulti_locked(ifp, ifma, 0);
3607 IF_ADDR_WUNLOCK(ifp);
3611 * Delete a multicast group membership by group membership pointer.
3612 * Network-layer protocol domains must use this routine.
3614 * It is safe to call this routine if the ifp disappeared.
3617 if_delmulti_ifma(struct ifmultiaddr *ifma)
3622 ifp = ifma->ifma_ifp;
3625 printf("%s: ifma_ifp seems to be detached\n", __func__);
3629 IFNET_RLOCK_NOSLEEP();
3630 TAILQ_FOREACH(oifp, &V_ifnet, if_link)
3634 printf("%s: ifnet %p disappeared\n", __func__, ifp);
3637 IFNET_RUNLOCK_NOSLEEP();
3641 * If and only if the ifnet instance exists: Acquire the address lock.
3646 lastref = if_delmulti_locked(ifp, ifma, 0);
3650 * If and only if the ifnet instance exists:
3651 * Release the address lock.
3652 * If the group was left: update the hardware hash filter.
3654 IF_ADDR_WUNLOCK(ifp);
3655 if (lastref && ifp->if_ioctl != NULL) {
3656 (void)(*ifp->if_ioctl)(ifp, SIOCDELMULTI, 0);
3662 * Perform deletion of network-layer and/or link-layer multicast address.
3664 * Return 0 if the reference count was decremented.
3665 * Return 1 if the final reference was released, indicating that the
3666 * hardware hash filter should be reprogrammed.
3669 if_delmulti_locked(struct ifnet *ifp, struct ifmultiaddr *ifma, int detaching)
3671 struct ifmultiaddr *ll_ifma;
3673 if (ifp != NULL && ifma->ifma_ifp != NULL) {
3674 KASSERT(ifma->ifma_ifp == ifp,
3675 ("%s: inconsistent ifp %p", __func__, ifp));
3676 IF_ADDR_WLOCK_ASSERT(ifp);
3679 ifp = ifma->ifma_ifp;
3682 * If the ifnet is detaching, null out references to ifnet,
3683 * so that upper protocol layers will notice, and not attempt
3684 * to obtain locks for an ifnet which no longer exists. The
3685 * routing socket announcement must happen before the ifnet
3686 * instance is detached from the system.
3690 printf("%s: detaching ifnet instance %p\n", __func__, ifp);
3693 * ifp may already be nulled out if we are being reentered
3694 * to delete the ll_ifma.
3697 rt_newmaddrmsg(RTM_DELMADDR, ifma);
3698 ifma->ifma_ifp = NULL;
3702 if (--ifma->ifma_refcount > 0)
3706 * If this ifma is a network-layer ifma, a link-layer ifma may
3707 * have been associated with it. Release it first if so.
3709 ll_ifma = ifma->ifma_llifma;
3710 if (ll_ifma != NULL) {
3711 KASSERT(ifma->ifma_lladdr != NULL,
3712 ("%s: llifma w/o lladdr", __func__));
3714 ll_ifma->ifma_ifp = NULL; /* XXX */
3715 if (--ll_ifma->ifma_refcount == 0) {
3717 TAILQ_REMOVE(&ifp->if_multiaddrs, ll_ifma,
3720 if_freemulti(ll_ifma);
3725 TAILQ_REMOVE(&ifp->if_multiaddrs, ifma, ifma_link);
3730 * The last reference to this instance of struct ifmultiaddr
3731 * was released; the hardware should be notified of this change.
3737 * Set the link layer address on an interface.
3739 * At this time we only support certain types of interfaces,
3740 * and we don't allow the length of the address to change.
3742 * Set noinline to be dtrace-friendly
3745 if_setlladdr(struct ifnet *ifp, const u_char *lladdr, int len)
3747 struct sockaddr_dl *sdl;
3754 IF_ADDR_RUNLOCK(ifp);
3758 IF_ADDR_RUNLOCK(ifp);
3759 sdl = (struct sockaddr_dl *)ifa->ifa_addr;
3764 if (len != sdl->sdl_alen) { /* don't allow length to change */
3768 switch (ifp->if_type) {
3776 case IFT_IEEE8023ADLAG:
3778 bcopy(lladdr, LLADDR(sdl), len);
3787 * If the interface is already up, we need
3788 * to re-init it in order to reprogram its
3791 if ((ifp->if_flags & IFF_UP) != 0) {
3792 if (ifp->if_ioctl) {
3793 ifp->if_flags &= ~IFF_UP;
3794 ifr.ifr_flags = ifp->if_flags & 0xffff;
3795 ifr.ifr_flagshigh = ifp->if_flags >> 16;
3796 (*ifp->if_ioctl)(ifp, SIOCSIFFLAGS, (caddr_t)&ifr);
3797 ifp->if_flags |= IFF_UP;
3798 ifr.ifr_flags = ifp->if_flags & 0xffff;
3799 ifr.ifr_flagshigh = ifp->if_flags >> 16;
3800 (*ifp->if_ioctl)(ifp, SIOCSIFFLAGS, (caddr_t)&ifr);
3803 EVENTHANDLER_INVOKE(iflladdr_event, ifp);
3808 * Compat function for handling basic encapsulation requests.
3809 * Not converted stacks (FDDI, IB, ..) supports traditional
3810 * output model: ARP (and other similar L2 protocols) are handled
3811 * inside output routine, arpresolve/nd6_resolve() returns MAC
3812 * address instead of full prepend.
3814 * This function creates calculated header==MAC for IPv4/IPv6 and
3815 * returns EAFNOSUPPORT (which is then handled in ARP code) for other
3819 if_requestencap_default(struct ifnet *ifp, struct if_encap_req *req)
3822 if (req->rtype != IFENCAP_LL)
3823 return (EOPNOTSUPP);
3825 if (req->bufsize < req->lladdr_len)
3828 switch (req->family) {
3833 return (EAFNOSUPPORT);
3836 /* Copy lladdr to storage as is */
3837 memmove(req->buf, req->lladdr, req->lladdr_len);
3838 req->bufsize = req->lladdr_len;
3839 req->lladdr_off = 0;
3845 * Get the link layer address that was read from the hardware at attach.
3847 * This is only set by Ethernet NICs (IFT_ETHER), but laggX interfaces re-type
3848 * their component interfaces as IFT_IEEE8023ADLAG.
3851 if_gethwaddr(struct ifnet *ifp, struct ifreq *ifr)
3854 if (ifp->if_hw_addr == NULL)
3857 switch (ifp->if_type) {
3859 case IFT_IEEE8023ADLAG:
3860 bcopy(ifp->if_hw_addr, ifr->ifr_addr.sa_data, ifp->if_addrlen);
3868 * The name argument must be a pointer to storage which will last as
3869 * long as the interface does. For physical devices, the result of
3870 * device_get_name(dev) is a good choice and for pseudo-devices a
3871 * static string works well.
3874 if_initname(struct ifnet *ifp, const char *name, int unit)
3876 ifp->if_dname = name;
3877 ifp->if_dunit = unit;
3878 if (unit != IF_DUNIT_NONE)
3879 snprintf(ifp->if_xname, IFNAMSIZ, "%s%d", name, unit);
3881 strlcpy(ifp->if_xname, name, IFNAMSIZ);
3885 if_printf(struct ifnet *ifp, const char * fmt, ...)
3890 retval = printf("%s: ", ifp->if_xname);
3892 retval += vprintf(fmt, ap);
3898 if_start(struct ifnet *ifp)
3901 (*(ifp)->if_start)(ifp);
3905 * Backwards compatibility interface for drivers
3906 * that have not implemented it
3909 if_transmit(struct ifnet *ifp, struct mbuf *m)
3913 IFQ_HANDOFF(ifp, m, error);
3918 if_input_default(struct ifnet *ifp __unused, struct mbuf *m)
3925 if_handoff(struct ifqueue *ifq, struct mbuf *m, struct ifnet *ifp, int adjust)
3930 if (_IF_QFULL(ifq)) {
3932 if_inc_counter(ifp, IFCOUNTER_OQDROPS, 1);
3937 if_inc_counter(ifp, IFCOUNTER_OBYTES, m->m_pkthdr.len + adjust);
3938 if (m->m_flags & (M_BCAST|M_MCAST))
3939 if_inc_counter(ifp, IFCOUNTER_OMCASTS, 1);
3940 active = ifp->if_drv_flags & IFF_DRV_OACTIVE;
3942 _IF_ENQUEUE(ifq, m);
3944 if (ifp != NULL && !active)
3945 (*(ifp)->if_start)(ifp);
3950 if_register_com_alloc(u_char type,
3951 if_com_alloc_t *a, if_com_free_t *f)
3954 KASSERT(if_com_alloc[type] == NULL,
3955 ("if_register_com_alloc: %d already registered", type));
3956 KASSERT(if_com_free[type] == NULL,
3957 ("if_register_com_alloc: %d free already registered", type));
3959 if_com_alloc[type] = a;
3960 if_com_free[type] = f;
3964 if_deregister_com_alloc(u_char type)
3967 KASSERT(if_com_alloc[type] != NULL,
3968 ("if_deregister_com_alloc: %d not registered", type));
3969 KASSERT(if_com_free[type] != NULL,
3970 ("if_deregister_com_alloc: %d free not registered", type));
3971 if_com_alloc[type] = NULL;
3972 if_com_free[type] = NULL;
3975 /* API for driver access to network stack owned ifnet.*/
3977 if_setbaudrate(struct ifnet *ifp, uint64_t baudrate)
3981 oldbrate = ifp->if_baudrate;
3982 ifp->if_baudrate = baudrate;
3987 if_getbaudrate(if_t ifp)
3990 return (((struct ifnet *)ifp)->if_baudrate);
3994 if_setcapabilities(if_t ifp, int capabilities)
3996 ((struct ifnet *)ifp)->if_capabilities = capabilities;
4001 if_setcapabilitiesbit(if_t ifp, int setbit, int clearbit)
4003 ((struct ifnet *)ifp)->if_capabilities |= setbit;
4004 ((struct ifnet *)ifp)->if_capabilities &= ~clearbit;
4010 if_getcapabilities(if_t ifp)
4012 return ((struct ifnet *)ifp)->if_capabilities;
4016 if_setcapenable(if_t ifp, int capabilities)
4018 ((struct ifnet *)ifp)->if_capenable = capabilities;
4023 if_setcapenablebit(if_t ifp, int setcap, int clearcap)
4026 ((struct ifnet *)ifp)->if_capenable |= setcap;
4028 ((struct ifnet *)ifp)->if_capenable &= ~clearcap;
4034 if_getdname(if_t ifp)
4036 return ((struct ifnet *)ifp)->if_dname;
4040 if_togglecapenable(if_t ifp, int togglecap)
4042 ((struct ifnet *)ifp)->if_capenable ^= togglecap;
4047 if_getcapenable(if_t ifp)
4049 return ((struct ifnet *)ifp)->if_capenable;
4053 * This is largely undesirable because it ties ifnet to a device, but does
4054 * provide flexiblity for an embedded product vendor. Should be used with
4055 * the understanding that it violates the interface boundaries, and should be
4056 * a last resort only.
4059 if_setdev(if_t ifp, void *dev)
4065 if_setdrvflagbits(if_t ifp, int set_flags, int clear_flags)
4067 ((struct ifnet *)ifp)->if_drv_flags |= set_flags;
4068 ((struct ifnet *)ifp)->if_drv_flags &= ~clear_flags;
4074 if_getdrvflags(if_t ifp)
4076 return ((struct ifnet *)ifp)->if_drv_flags;
4080 if_setdrvflags(if_t ifp, int flags)
4082 ((struct ifnet *)ifp)->if_drv_flags = flags;
4088 if_setflags(if_t ifp, int flags)
4090 ((struct ifnet *)ifp)->if_flags = flags;
4095 if_setflagbits(if_t ifp, int set, int clear)
4097 ((struct ifnet *)ifp)->if_flags |= set;
4098 ((struct ifnet *)ifp)->if_flags &= ~clear;
4104 if_getflags(if_t ifp)
4106 return ((struct ifnet *)ifp)->if_flags;
4110 if_clearhwassist(if_t ifp)
4112 ((struct ifnet *)ifp)->if_hwassist = 0;
4117 if_sethwassistbits(if_t ifp, int toset, int toclear)
4119 ((struct ifnet *)ifp)->if_hwassist |= toset;
4120 ((struct ifnet *)ifp)->if_hwassist &= ~toclear;
4126 if_sethwassist(if_t ifp, int hwassist_bit)
4128 ((struct ifnet *)ifp)->if_hwassist = hwassist_bit;
4133 if_gethwassist(if_t ifp)
4135 return ((struct ifnet *)ifp)->if_hwassist;
4139 if_setmtu(if_t ifp, int mtu)
4141 ((struct ifnet *)ifp)->if_mtu = mtu;
4148 return ((struct ifnet *)ifp)->if_mtu;
4152 if_getmtu_family(if_t ifp, int family)
4156 for (dp = domains; dp; dp = dp->dom_next) {
4157 if (dp->dom_family == family && dp->dom_ifmtu != NULL)
4158 return (dp->dom_ifmtu((struct ifnet *)ifp));
4161 return (((struct ifnet *)ifp)->if_mtu);
4165 if_setsoftc(if_t ifp, void *softc)
4167 ((struct ifnet *)ifp)->if_softc = softc;
4172 if_getsoftc(if_t ifp)
4174 return ((struct ifnet *)ifp)->if_softc;
4178 if_setrcvif(struct mbuf *m, if_t ifp)
4180 m->m_pkthdr.rcvif = (struct ifnet *)ifp;
4184 if_setvtag(struct mbuf *m, uint16_t tag)
4186 m->m_pkthdr.ether_vtag = tag;
4190 if_getvtag(struct mbuf *m)
4193 return (m->m_pkthdr.ether_vtag);
4197 if_sendq_empty(if_t ifp)
4199 return IFQ_DRV_IS_EMPTY(&((struct ifnet *)ifp)->if_snd);
4203 if_getifaddr(if_t ifp)
4205 return ((struct ifnet *)ifp)->if_addr;
4209 if_getamcount(if_t ifp)
4211 return ((struct ifnet *)ifp)->if_amcount;
4216 if_setsendqready(if_t ifp)
4218 IFQ_SET_READY(&((struct ifnet *)ifp)->if_snd);
4223 if_setsendqlen(if_t ifp, int tx_desc_count)
4225 IFQ_SET_MAXLEN(&((struct ifnet *)ifp)->if_snd, tx_desc_count);
4226 ((struct ifnet *)ifp)->if_snd.ifq_drv_maxlen = tx_desc_count;
4232 if_vlantrunkinuse(if_t ifp)
4234 return ((struct ifnet *)ifp)->if_vlantrunk != NULL?1:0;
4238 if_input(if_t ifp, struct mbuf* sendmp)
4240 (*((struct ifnet *)ifp)->if_input)((struct ifnet *)ifp, sendmp);
4246 #ifndef ETH_ADDR_LEN
4247 #define ETH_ADDR_LEN 6
4251 if_setupmultiaddr(if_t ifp, void *mta, int *cnt, int max)
4253 struct ifmultiaddr *ifma;
4254 uint8_t *lmta = (uint8_t *)mta;
4257 TAILQ_FOREACH(ifma, &((struct ifnet *)ifp)->if_multiaddrs, ifma_link) {
4258 if (ifma->ifma_addr->sa_family != AF_LINK)
4264 bcopy(LLADDR((struct sockaddr_dl *)ifma->ifma_addr),
4265 &lmta[mcnt * ETH_ADDR_LEN], ETH_ADDR_LEN);
4274 if_multiaddr_array(if_t ifp, void *mta, int *cnt, int max)
4278 if_maddr_rlock(ifp);
4279 error = if_setupmultiaddr(ifp, mta, cnt, max);
4280 if_maddr_runlock(ifp);
4285 if_multiaddr_count(if_t ifp, int max)
4287 struct ifmultiaddr *ifma;
4291 if_maddr_rlock(ifp);
4292 TAILQ_FOREACH(ifma, &((struct ifnet *)ifp)->if_multiaddrs, ifma_link) {
4293 if (ifma->ifma_addr->sa_family != AF_LINK)
4299 if_maddr_runlock(ifp);
4304 if_multi_apply(struct ifnet *ifp, int (*filter)(void *, struct ifmultiaddr *, int), void *arg)
4306 struct ifmultiaddr *ifma;
4309 if_maddr_rlock(ifp);
4310 TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link)
4311 cnt += filter(arg, ifma, cnt);
4312 if_maddr_runlock(ifp);
4317 if_dequeue(if_t ifp)
4320 IFQ_DRV_DEQUEUE(&((struct ifnet *)ifp)->if_snd, m);
4326 if_sendq_prepend(if_t ifp, struct mbuf *m)
4328 IFQ_DRV_PREPEND(&((struct ifnet *)ifp)->if_snd, m);
4333 if_setifheaderlen(if_t ifp, int len)
4335 ((struct ifnet *)ifp)->if_hdrlen = len;
4340 if_getlladdr(if_t ifp)
4342 return (IF_LLADDR((struct ifnet *)ifp));
4346 if_gethandle(u_char type)
4348 return (if_alloc(type));
4352 if_bpfmtap(if_t ifh, struct mbuf *m)
4354 struct ifnet *ifp = (struct ifnet *)ifh;
4360 if_etherbpfmtap(if_t ifh, struct mbuf *m)
4362 struct ifnet *ifp = (struct ifnet *)ifh;
4364 ETHER_BPF_MTAP(ifp, m);
4368 if_vlancap(if_t ifh)
4370 struct ifnet *ifp = (struct ifnet *)ifh;
4371 VLAN_CAPABILITIES(ifp);
4375 if_setinitfn(if_t ifp, void (*init_fn)(void *))
4377 ((struct ifnet *)ifp)->if_init = init_fn;
4381 if_setioctlfn(if_t ifp, int (*ioctl_fn)(if_t, u_long, caddr_t))
4383 ((struct ifnet *)ifp)->if_ioctl = (void *)ioctl_fn;
4387 if_setstartfn(if_t ifp, void (*start_fn)(if_t))
4389 ((struct ifnet *)ifp)->if_start = (void *)start_fn;
4393 if_settransmitfn(if_t ifp, if_transmit_fn_t start_fn)
4395 ((struct ifnet *)ifp)->if_transmit = start_fn;
4398 void if_setqflushfn(if_t ifp, if_qflush_fn_t flush_fn)
4400 ((struct ifnet *)ifp)->if_qflush = flush_fn;
4405 if_setgetcounterfn(if_t ifp, if_get_counter_t fn)
4408 ifp->if_get_counter = fn;
4411 /* Revisit these - These are inline functions originally. */
4413 drbr_inuse_drv(if_t ifh, struct buf_ring *br)
4415 return drbr_inuse(ifh, br);
4419 drbr_dequeue_drv(if_t ifh, struct buf_ring *br)
4421 return drbr_dequeue(ifh, br);
4425 drbr_needs_enqueue_drv(if_t ifh, struct buf_ring *br)
4427 return drbr_needs_enqueue(ifh, br);
4431 drbr_enqueue_drv(if_t ifh, struct buf_ring *br, struct mbuf *m)
4433 return drbr_enqueue(ifh, br, m);