2 * SPDX-License-Identifier: BSD-3-Clause
4 * Copyright (c) 1980, 1986, 1993
5 * The Regents of the University of California. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. Neither the name of the University nor the names of its contributors
16 * may be used to endorse or promote products derived from this software
17 * without specific prior written permission.
19 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * @(#)if.c 8.5 (Berkeley) 1/9/95
35 #include "opt_inet6.h"
38 #include <sys/param.h>
39 #include <sys/types.h>
41 #include <sys/malloc.h>
45 #include <sys/systm.h>
48 #include <sys/socket.h>
49 #include <sys/socketvar.h>
50 #include <sys/protosw.h>
51 #include <sys/kernel.h>
53 #include <sys/refcount.h>
54 #include <sys/module.h>
55 #include <sys/rwlock.h>
56 #include <sys/sockio.h>
57 #include <sys/syslog.h>
58 #include <sys/sysctl.h>
59 #include <sys/sysent.h>
60 #include <sys/taskqueue.h>
61 #include <sys/domain.h>
65 #include <machine/stdarg.h>
69 #include <net/ethernet.h>
71 #include <net/if_arp.h>
72 #include <net/if_clone.h>
73 #include <net/if_dl.h>
74 #include <net/if_types.h>
75 #include <net/if_var.h>
76 #include <net/if_media.h>
77 #include <net/if_vlan_var.h>
78 #include <net/radix.h>
79 #include <net/route.h>
82 #if defined(INET) || defined(INET6)
83 #include <net/ethernet.h>
84 #include <netinet/in.h>
85 #include <netinet/in_var.h>
86 #include <netinet/ip.h>
87 #include <netinet/ip_carp.h>
89 #include <netinet/if_ether.h>
92 #include <netinet6/in6_var.h>
93 #include <netinet6/in6_ifattach.h>
95 #endif /* INET || INET6 */
97 #include <security/mac/mac_framework.h>
100 * Consumers of struct ifreq such as tcpdump assume no pad between ifr_name
101 * and ifr_ifru when it is used in SIOCGIFCONF.
103 _Static_assert(sizeof(((struct ifreq *)0)->ifr_name) ==
104 offsetof(struct ifreq, ifr_ifru), "gap between ifr_name and ifr_ifru");
106 #ifdef COMPAT_FREEBSD32
107 #include <sys/mount.h>
108 #include <compat/freebsd32/freebsd32.h>
110 struct ifreq_buffer32 {
111 uint32_t length; /* (size_t) */
112 uint32_t buffer; /* (void *) */
116 * Interface request structure used for socket
117 * ioctl's. All interface ioctl's must have parameter
118 * definitions which begin with ifr_name. The
119 * remainder may be interface specific.
122 char ifr_name[IFNAMSIZ]; /* if name, e.g. "en0" */
124 struct sockaddr ifru_addr;
125 struct sockaddr ifru_dstaddr;
126 struct sockaddr ifru_broadaddr;
127 struct ifreq_buffer32 ifru_buffer;
138 u_char ifru_vlan_pcp;
141 CTASSERT(sizeof(struct ifreq) == sizeof(struct ifreq32));
142 CTASSERT(__offsetof(struct ifreq, ifr_ifru) ==
143 __offsetof(struct ifreq32, ifr_ifru));
145 struct ifgroupreq32 {
146 char ifgr_name[IFNAMSIZ];
149 char ifgru_group[IFNAMSIZ];
150 uint32_t ifgru_groups;
153 #define _CASE_IOC_IFGROUPREQ_32(cmd) \
154 case _IOC_NEWTYPE((cmd), struct ifgroupreq32):
156 #define _CASE_IOC_IFGROUPREQ_32(cmd)
157 #endif /* COMPAT_FREEBSD32 */
159 #define CASE_IOC_IFGROUPREQ(cmd) \
160 _CASE_IOC_IFGROUPREQ_32(cmd) \
165 #ifdef COMPAT_FREEBSD32
166 struct ifreq32 ifr32;
170 union ifgroupreq_union {
171 struct ifgroupreq ifgr;
172 #ifdef COMPAT_FREEBSD32
173 struct ifgroupreq32 ifgr32;
177 SYSCTL_NODE(_net, PF_LINK, link, CTLFLAG_RW, 0, "Link layers");
178 SYSCTL_NODE(_net_link, 0, generic, CTLFLAG_RW, 0, "Generic link-management");
180 SYSCTL_INT(_net_link, OID_AUTO, ifqmaxlen, CTLFLAG_RDTUN,
181 &ifqmaxlen, 0, "max send queue size");
183 /* Log link state change events */
184 static int log_link_state_change = 1;
186 SYSCTL_INT(_net_link, OID_AUTO, log_link_state_change, CTLFLAG_RW,
187 &log_link_state_change, 0,
188 "log interface link state change events");
190 /* Log promiscuous mode change events */
191 static int log_promisc_mode_change = 1;
193 SYSCTL_INT(_net_link, OID_AUTO, log_promisc_mode_change, CTLFLAG_RDTUN,
194 &log_promisc_mode_change, 1,
195 "log promiscuous mode change events");
197 /* Interface description */
198 static unsigned int ifdescr_maxlen = 1024;
199 SYSCTL_UINT(_net, OID_AUTO, ifdescr_maxlen, CTLFLAG_RW,
201 "administrative maximum length for interface description");
203 static MALLOC_DEFINE(M_IFDESCR, "ifdescr", "ifnet descriptions");
205 /* global sx for non-critical path ifdescr */
206 static struct sx ifdescr_sx;
207 SX_SYSINIT(ifdescr_sx, &ifdescr_sx, "ifnet descr");
209 void (*bridge_linkstate_p)(struct ifnet *ifp);
210 void (*ng_ether_link_state_p)(struct ifnet *ifp, int state);
211 void (*lagg_linkstate_p)(struct ifnet *ifp, int state);
212 /* These are external hooks for CARP. */
213 void (*carp_linkstate_p)(struct ifnet *ifp);
214 void (*carp_demote_adj_p)(int, char *);
215 int (*carp_master_p)(struct ifaddr *);
216 #if defined(INET) || defined(INET6)
217 int (*carp_forus_p)(struct ifnet *ifp, u_char *dhost);
218 int (*carp_output_p)(struct ifnet *ifp, struct mbuf *m,
219 const struct sockaddr *sa);
220 int (*carp_ioctl_p)(struct ifreq *, u_long, struct thread *);
221 int (*carp_attach_p)(struct ifaddr *, int);
222 void (*carp_detach_p)(struct ifaddr *, bool);
225 int (*carp_iamatch_p)(struct ifaddr *, uint8_t **);
228 struct ifaddr *(*carp_iamatch6_p)(struct ifnet *ifp, struct in6_addr *taddr6);
229 caddr_t (*carp_macmatch6_p)(struct ifnet *ifp, struct mbuf *m,
230 const struct in6_addr *taddr);
233 struct mbuf *(*tbr_dequeue_ptr)(struct ifaltq *, int) = NULL;
236 * XXX: Style; these should be sorted alphabetically, and unprototyped
237 * static functions should be prototyped. Currently they are sorted by
240 static void if_attachdomain(void *);
241 static void if_attachdomain1(struct ifnet *);
242 static int ifconf(u_long, caddr_t);
243 static void if_freemulti(struct ifmultiaddr *);
244 static void if_grow(void);
245 static void if_input_default(struct ifnet *, struct mbuf *);
246 static int if_requestencap_default(struct ifnet *, struct if_encap_req *);
247 static void if_route(struct ifnet *, int flag, int fam);
248 static int if_setflag(struct ifnet *, int, int, int *, int);
249 static int if_transmit(struct ifnet *ifp, struct mbuf *m);
250 static void if_unroute(struct ifnet *, int flag, int fam);
251 static void link_rtrequest(int, struct rtentry *, struct rt_addrinfo *);
252 static int ifhwioctl(u_long, struct ifnet *, caddr_t, struct thread *);
253 static int if_delmulti_locked(struct ifnet *, struct ifmultiaddr *, int);
254 static void do_link_state_change(void *, int);
255 static int if_getgroup(struct ifgroupreq *, struct ifnet *);
256 static int if_getgroupmembers(struct ifgroupreq *);
257 static void if_delgroups(struct ifnet *);
258 static void if_attach_internal(struct ifnet *, int, struct if_clone *);
259 static int if_detach_internal(struct ifnet *, int, struct if_clone **);
261 static void if_vmove(struct ifnet *, struct vnet *);
266 * XXX: declare here to avoid to include many inet6 related files..
267 * should be more generalized?
269 extern void nd6_setmtu(struct ifnet *);
272 /* ipsec helper hooks */
273 VNET_DEFINE(struct hhook_head *, ipsec_hhh_in[HHOOK_IPSEC_COUNT]);
274 VNET_DEFINE(struct hhook_head *, ipsec_hhh_out[HHOOK_IPSEC_COUNT]);
276 VNET_DEFINE(int, if_index);
277 int ifqmaxlen = IFQ_MAXLEN;
278 VNET_DEFINE(struct ifnethead, ifnet); /* depend on static init XXX */
279 VNET_DEFINE(struct ifgrouphead, ifg_head);
281 static VNET_DEFINE(int, if_indexlim) = 8;
283 /* Table of ifnet by index. */
284 VNET_DEFINE(struct ifnet **, ifindex_table);
286 #define V_if_indexlim VNET(if_indexlim)
287 #define V_ifindex_table VNET(ifindex_table)
290 * The global network interface list (V_ifnet) and related state (such as
291 * if_index, if_indexlim, and ifindex_table) are protected by an sxlock and
292 * an rwlock. Either may be acquired shared to stablize the list, but both
293 * must be acquired writable to modify the list. This model allows us to
294 * both stablize the interface list during interrupt thread processing, but
295 * also to stablize it over long-running ioctls, without introducing priority
296 * inversions and deadlocks.
298 struct rwlock ifnet_rwlock;
299 RW_SYSINIT_FLAGS(ifnet_rw, &ifnet_rwlock, "ifnet_rw", RW_RECURSE);
300 struct sx ifnet_sxlock;
301 SX_SYSINIT_FLAGS(ifnet_sx, &ifnet_sxlock, "ifnet_sx", SX_RECURSE);
304 * The allocation of network interfaces is a rather non-atomic affair; we
305 * need to select an index before we are ready to expose the interface for
306 * use, so will use this pointer value to indicate reservation.
308 #define IFNET_HOLD (void *)(uintptr_t)(-1)
310 static if_com_alloc_t *if_com_alloc[256];
311 static if_com_free_t *if_com_free[256];
313 static MALLOC_DEFINE(M_IFNET, "ifnet", "interface internals");
314 MALLOC_DEFINE(M_IFADDR, "ifaddr", "interface address");
315 MALLOC_DEFINE(M_IFMADDR, "ether_multi", "link-level multicast address");
318 ifnet_byindex_locked(u_short idx)
321 if (idx > V_if_index)
323 if (V_ifindex_table[idx] == IFNET_HOLD)
325 return (V_ifindex_table[idx]);
329 ifnet_byindex(u_short idx)
333 IFNET_RLOCK_NOSLEEP();
334 ifp = ifnet_byindex_locked(idx);
335 IFNET_RUNLOCK_NOSLEEP();
340 ifnet_byindex_ref(u_short idx)
344 IFNET_RLOCK_NOSLEEP();
345 ifp = ifnet_byindex_locked(idx);
346 if (ifp == NULL || (ifp->if_flags & IFF_DYING)) {
347 IFNET_RUNLOCK_NOSLEEP();
351 IFNET_RUNLOCK_NOSLEEP();
356 * Allocate an ifindex array entry; return 0 on success or an error on
364 IFNET_WLOCK_ASSERT();
367 * Try to find an empty slot below V_if_index. If we fail, take the
370 for (idx = 1; idx <= V_if_index; idx++) {
371 if (V_ifindex_table[idx] == NULL)
375 /* Catch if_index overflow. */
376 if (idx >= V_if_indexlim) {
380 if (idx > V_if_index)
386 ifindex_free_locked(u_short idx)
389 IFNET_WLOCK_ASSERT();
391 V_ifindex_table[idx] = NULL;
392 while (V_if_index > 0 &&
393 V_ifindex_table[V_if_index] == NULL)
398 ifindex_free(u_short idx)
402 ifindex_free_locked(idx);
407 ifnet_setbyindex_locked(u_short idx, struct ifnet *ifp)
410 IFNET_WLOCK_ASSERT();
412 V_ifindex_table[idx] = ifp;
416 ifnet_setbyindex(u_short idx, struct ifnet *ifp)
420 ifnet_setbyindex_locked(idx, ifp);
425 ifaddr_byindex(u_short idx)
428 struct ifaddr *ifa = NULL;
430 IFNET_RLOCK_NOSLEEP();
431 ifp = ifnet_byindex_locked(idx);
432 if (ifp != NULL && (ifa = ifp->if_addr) != NULL)
434 IFNET_RUNLOCK_NOSLEEP();
439 * Network interface utility routines.
441 * Routines with ifa_ifwith* names take sockaddr *'s as
446 vnet_if_init(const void *unused __unused)
449 TAILQ_INIT(&V_ifnet);
450 TAILQ_INIT(&V_ifg_head);
452 if_grow(); /* create initial table */
454 vnet_if_clone_init();
456 VNET_SYSINIT(vnet_if_init, SI_SUB_INIT_IF, SI_ORDER_SECOND, vnet_if_init,
461 vnet_if_uninit(const void *unused __unused)
464 VNET_ASSERT(TAILQ_EMPTY(&V_ifnet), ("%s:%d tailq &V_ifnet=%p "
465 "not empty", __func__, __LINE__, &V_ifnet));
466 VNET_ASSERT(TAILQ_EMPTY(&V_ifg_head), ("%s:%d tailq &V_ifg_head=%p "
467 "not empty", __func__, __LINE__, &V_ifg_head));
469 free((caddr_t)V_ifindex_table, M_IFNET);
471 VNET_SYSUNINIT(vnet_if_uninit, SI_SUB_INIT_IF, SI_ORDER_FIRST,
472 vnet_if_uninit, NULL);
475 vnet_if_return(const void *unused __unused)
477 struct ifnet *ifp, *nifp;
479 /* Return all inherited interfaces to their parent vnets. */
480 TAILQ_FOREACH_SAFE(ifp, &V_ifnet, if_link, nifp) {
481 if (ifp->if_home_vnet != ifp->if_vnet)
482 if_vmove(ifp, ifp->if_home_vnet);
485 VNET_SYSUNINIT(vnet_if_return, SI_SUB_VNET_DONE, SI_ORDER_ANY,
486 vnet_if_return, NULL);
496 IFNET_WLOCK_ASSERT();
497 oldlim = V_if_indexlim;
499 n = (oldlim << 1) * sizeof(*e);
500 e = malloc(n, M_IFNET, M_WAITOK | M_ZERO);
502 if (V_if_indexlim != oldlim) {
506 if (V_ifindex_table != NULL) {
507 memcpy((caddr_t)e, (caddr_t)V_ifindex_table, n/2);
508 free((caddr_t)V_ifindex_table, M_IFNET);
515 * Allocate a struct ifnet and an index for an interface. A layer 2
516 * common structure will also be allocated if an allocation routine is
517 * registered for the passed type.
520 if_alloc(u_char type)
525 ifp = malloc(sizeof(struct ifnet), M_IFNET, M_WAITOK|M_ZERO);
527 idx = ifindex_alloc();
528 ifnet_setbyindex_locked(idx, IFNET_HOLD);
532 ifp->if_alloctype = type;
534 ifp->if_vnet = curvnet;
536 if (if_com_alloc[type] != NULL) {
537 ifp->if_l2com = if_com_alloc[type](type, ifp);
538 if (ifp->if_l2com == NULL) {
545 IF_ADDR_LOCK_INIT(ifp);
546 TASK_INIT(&ifp->if_linktask, 0, do_link_state_change, ifp);
547 ifp->if_afdata_initialized = 0;
548 IF_AFDATA_LOCK_INIT(ifp);
549 TAILQ_INIT(&ifp->if_addrhead);
550 TAILQ_INIT(&ifp->if_multiaddrs);
551 TAILQ_INIT(&ifp->if_groups);
555 ifq_init(&ifp->if_snd, ifp);
557 refcount_init(&ifp->if_refcount, 1); /* Index reference. */
558 for (int i = 0; i < IFCOUNTERS; i++)
559 ifp->if_counters[i] = counter_u64_alloc(M_WAITOK);
560 ifp->if_get_counter = if_get_counter_default;
561 ifp->if_pcp = IFNET_PCP_NONE;
562 ifnet_setbyindex(ifp->if_index, ifp);
567 * Do the actual work of freeing a struct ifnet, and layer 2 common
568 * structure. This call is made when the last reference to an
569 * interface is released.
572 if_free_internal(struct ifnet *ifp)
575 KASSERT((ifp->if_flags & IFF_DYING),
576 ("if_free_internal: interface not dying"));
578 if (if_com_free[ifp->if_alloctype] != NULL)
579 if_com_free[ifp->if_alloctype](ifp->if_l2com,
583 mac_ifnet_destroy(ifp);
585 if (ifp->if_description != NULL)
586 free(ifp->if_description, M_IFDESCR);
587 IF_AFDATA_DESTROY(ifp);
588 IF_ADDR_LOCK_DESTROY(ifp);
589 ifq_delete(&ifp->if_snd);
591 for (int i = 0; i < IFCOUNTERS; i++)
592 counter_u64_free(ifp->if_counters[i]);
598 * Deregister an interface and free the associated storage.
601 if_free(struct ifnet *ifp)
604 ifp->if_flags |= IFF_DYING; /* XXX: Locking */
606 CURVNET_SET_QUIET(ifp->if_vnet);
608 KASSERT(ifp == ifnet_byindex_locked(ifp->if_index),
609 ("%s: freeing unallocated ifnet", ifp->if_xname));
611 ifindex_free_locked(ifp->if_index);
614 if (refcount_release(&ifp->if_refcount))
615 if_free_internal(ifp);
620 * Interfaces to keep an ifnet type-stable despite the possibility of the
621 * driver calling if_free(). If there are additional references, we defer
622 * freeing the underlying data structure.
625 if_ref(struct ifnet *ifp)
628 /* We don't assert the ifnet list lock here, but arguably should. */
629 refcount_acquire(&ifp->if_refcount);
633 if_rele(struct ifnet *ifp)
636 if (!refcount_release(&ifp->if_refcount))
638 if_free_internal(ifp);
642 ifq_init(struct ifaltq *ifq, struct ifnet *ifp)
645 mtx_init(&ifq->ifq_mtx, ifp->if_xname, "if send queue", MTX_DEF);
647 if (ifq->ifq_maxlen == 0)
648 ifq->ifq_maxlen = ifqmaxlen;
651 ifq->altq_disc = NULL;
652 ifq->altq_flags &= ALTQF_CANTCHANGE;
653 ifq->altq_tbr = NULL;
658 ifq_delete(struct ifaltq *ifq)
660 mtx_destroy(&ifq->ifq_mtx);
664 * Perform generic interface initialization tasks and attach the interface
665 * to the list of "active" interfaces. If vmove flag is set on entry
666 * to if_attach_internal(), perform only a limited subset of initialization
667 * tasks, given that we are moving from one vnet to another an ifnet which
668 * has already been fully initialized.
670 * Note that if_detach_internal() removes group membership unconditionally
671 * even when vmove flag is set, and if_attach_internal() adds only IFG_ALL.
672 * Thus, when if_vmove() is applied to a cloned interface, group membership
673 * is lost while a cloned one always joins a group whose name is
674 * ifc->ifc_name. To recover this after if_detach_internal() and
675 * if_attach_internal(), the cloner should be specified to
676 * if_attach_internal() via ifc. If it is non-NULL, if_attach_internal()
677 * attempts to join a group whose name is ifc->ifc_name.
680 * - The decision to return void and thus require this function to
681 * succeed is questionable.
682 * - We should probably do more sanity checking. For instance we don't
683 * do anything to insure if_xname is unique or non-empty.
686 if_attach(struct ifnet *ifp)
689 if_attach_internal(ifp, 0, NULL);
693 * Compute the least common TSO limit.
696 if_hw_tsomax_common(if_t ifp, struct ifnet_hw_tsomax *pmax)
699 * 1) If there is no limit currently, take the limit from
700 * the network adapter.
702 * 2) If the network adapter has a limit below the current
705 if (pmax->tsomaxbytes == 0 || (ifp->if_hw_tsomax != 0 &&
706 ifp->if_hw_tsomax < pmax->tsomaxbytes)) {
707 pmax->tsomaxbytes = ifp->if_hw_tsomax;
709 if (pmax->tsomaxsegcount == 0 || (ifp->if_hw_tsomaxsegcount != 0 &&
710 ifp->if_hw_tsomaxsegcount < pmax->tsomaxsegcount)) {
711 pmax->tsomaxsegcount = ifp->if_hw_tsomaxsegcount;
713 if (pmax->tsomaxsegsize == 0 || (ifp->if_hw_tsomaxsegsize != 0 &&
714 ifp->if_hw_tsomaxsegsize < pmax->tsomaxsegsize)) {
715 pmax->tsomaxsegsize = ifp->if_hw_tsomaxsegsize;
720 * Update TSO limit of a network adapter.
722 * Returns zero if no change. Else non-zero.
725 if_hw_tsomax_update(if_t ifp, struct ifnet_hw_tsomax *pmax)
728 if (ifp->if_hw_tsomax != pmax->tsomaxbytes) {
729 ifp->if_hw_tsomax = pmax->tsomaxbytes;
732 if (ifp->if_hw_tsomaxsegsize != pmax->tsomaxsegsize) {
733 ifp->if_hw_tsomaxsegsize = pmax->tsomaxsegsize;
736 if (ifp->if_hw_tsomaxsegcount != pmax->tsomaxsegcount) {
737 ifp->if_hw_tsomaxsegcount = pmax->tsomaxsegcount;
744 if_attach_internal(struct ifnet *ifp, int vmove, struct if_clone *ifc)
746 unsigned socksize, ifasize;
747 int namelen, masklen;
748 struct sockaddr_dl *sdl;
751 if (ifp->if_index == 0 || ifp != ifnet_byindex(ifp->if_index))
752 panic ("%s: BUG: if_attach called without if_alloc'd input()\n",
756 ifp->if_vnet = curvnet;
757 if (ifp->if_home_vnet == NULL)
758 ifp->if_home_vnet = curvnet;
761 if_addgroup(ifp, IFG_ALL);
763 /* Restore group membership for cloned interfaces. */
764 if (vmove && ifc != NULL)
765 if_clone_addgroup(ifp, ifc);
767 getmicrotime(&ifp->if_lastchange);
768 ifp->if_epoch = time_uptime;
770 KASSERT((ifp->if_transmit == NULL && ifp->if_qflush == NULL) ||
771 (ifp->if_transmit != NULL && ifp->if_qflush != NULL),
772 ("transmit and qflush must both either be set or both be NULL"));
773 if (ifp->if_transmit == NULL) {
774 ifp->if_transmit = if_transmit;
775 ifp->if_qflush = if_qflush;
777 if (ifp->if_input == NULL)
778 ifp->if_input = if_input_default;
780 if (ifp->if_requestencap == NULL)
781 ifp->if_requestencap = if_requestencap_default;
785 mac_ifnet_create(ifp);
789 * Create a Link Level name for this device.
791 namelen = strlen(ifp->if_xname);
793 * Always save enough space for any possiable name so we
794 * can do a rename in place later.
796 masklen = offsetof(struct sockaddr_dl, sdl_data[0]) + IFNAMSIZ;
797 socksize = masklen + ifp->if_addrlen;
798 if (socksize < sizeof(*sdl))
799 socksize = sizeof(*sdl);
800 socksize = roundup2(socksize, sizeof(long));
801 ifasize = sizeof(*ifa) + 2 * socksize;
802 ifa = ifa_alloc(ifasize, M_WAITOK);
803 sdl = (struct sockaddr_dl *)(ifa + 1);
804 sdl->sdl_len = socksize;
805 sdl->sdl_family = AF_LINK;
806 bcopy(ifp->if_xname, sdl->sdl_data, namelen);
807 sdl->sdl_nlen = namelen;
808 sdl->sdl_index = ifp->if_index;
809 sdl->sdl_type = ifp->if_type;
812 ifa->ifa_rtrequest = link_rtrequest;
813 ifa->ifa_addr = (struct sockaddr *)sdl;
814 sdl = (struct sockaddr_dl *)(socksize + (caddr_t)sdl);
815 ifa->ifa_netmask = (struct sockaddr *)sdl;
816 sdl->sdl_len = masklen;
818 sdl->sdl_data[--namelen] = 0xff;
819 TAILQ_INSERT_HEAD(&ifp->if_addrhead, ifa, ifa_link);
820 /* Reliably crash if used uninitialized. */
821 ifp->if_broadcastaddr = NULL;
823 if (ifp->if_type == IFT_ETHER) {
824 ifp->if_hw_addr = malloc(ifp->if_addrlen, M_IFADDR,
828 #if defined(INET) || defined(INET6)
829 /* Use defaults for TSO, if nothing is set */
830 if (ifp->if_hw_tsomax == 0 &&
831 ifp->if_hw_tsomaxsegcount == 0 &&
832 ifp->if_hw_tsomaxsegsize == 0) {
834 * The TSO defaults needs to be such that an
835 * NFS mbuf list of 35 mbufs totalling just
836 * below 64K works and that a chain of mbufs
837 * can be defragged into at most 32 segments:
839 ifp->if_hw_tsomax = min(IP_MAXPACKET, (32 * MCLBYTES) -
840 (ETHER_HDR_LEN + ETHER_VLAN_ENCAP_LEN));
841 ifp->if_hw_tsomaxsegcount = 35;
842 ifp->if_hw_tsomaxsegsize = 2048; /* 2K */
844 /* XXX some drivers set IFCAP_TSO after ethernet attach */
845 if (ifp->if_capabilities & IFCAP_TSO) {
846 if_printf(ifp, "Using defaults for TSO: %u/%u/%u\n",
848 ifp->if_hw_tsomaxsegcount,
849 ifp->if_hw_tsomaxsegsize);
857 * Update the interface index in the link layer address
860 for (ifa = ifp->if_addr; ifa != NULL;
861 ifa = TAILQ_NEXT(ifa, ifa_link)) {
862 if (ifa->ifa_addr->sa_family == AF_LINK) {
863 sdl = (struct sockaddr_dl *)ifa->ifa_addr;
864 sdl->sdl_index = ifp->if_index;
871 TAILQ_INSERT_TAIL(&V_ifnet, ifp, if_link);
873 curvnet->vnet_ifcnt++;
877 if (domain_init_status >= 2)
878 if_attachdomain1(ifp);
880 EVENTHANDLER_INVOKE(ifnet_arrival_event, ifp);
881 if (IS_DEFAULT_VNET(curvnet))
882 devctl_notify("IFNET", ifp->if_xname, "ATTACH", NULL);
884 /* Announce the interface. */
885 rt_ifannouncemsg(ifp, IFAN_ARRIVAL);
889 if_attachdomain(void *dummy)
893 TAILQ_FOREACH(ifp, &V_ifnet, if_link)
894 if_attachdomain1(ifp);
896 SYSINIT(domainifattach, SI_SUB_PROTO_IFATTACHDOMAIN, SI_ORDER_SECOND,
897 if_attachdomain, NULL);
900 if_attachdomain1(struct ifnet *ifp)
905 * Since dp->dom_ifattach calls malloc() with M_WAITOK, we
906 * cannot lock ifp->if_afdata initialization, entirely.
909 if (ifp->if_afdata_initialized >= domain_init_status) {
910 IF_AFDATA_UNLOCK(ifp);
911 log(LOG_WARNING, "%s called more than once on %s\n",
912 __func__, ifp->if_xname);
915 ifp->if_afdata_initialized = domain_init_status;
916 IF_AFDATA_UNLOCK(ifp);
918 /* address family dependent data region */
919 bzero(ifp->if_afdata, sizeof(ifp->if_afdata));
920 for (dp = domains; dp; dp = dp->dom_next) {
921 if (dp->dom_ifattach)
922 ifp->if_afdata[dp->dom_family] =
923 (*dp->dom_ifattach)(ifp);
928 * Remove any unicast or broadcast network addresses from an interface.
931 if_purgeaddrs(struct ifnet *ifp)
933 struct ifaddr *ifa, *next;
935 /* XXX cannot hold IF_ADDR_WLOCK over called functions. */
936 TAILQ_FOREACH_SAFE(ifa, &ifp->if_addrhead, ifa_link, next) {
937 if (ifa->ifa_addr->sa_family == AF_LINK)
940 /* XXX: Ugly!! ad hoc just for INET */
941 if (ifa->ifa_addr->sa_family == AF_INET) {
942 struct ifaliasreq ifr;
944 bzero(&ifr, sizeof(ifr));
945 ifr.ifra_addr = *ifa->ifa_addr;
946 if (ifa->ifa_dstaddr)
947 ifr.ifra_broadaddr = *ifa->ifa_dstaddr;
948 if (in_control(NULL, SIOCDIFADDR, (caddr_t)&ifr, ifp,
954 if (ifa->ifa_addr->sa_family == AF_INET6) {
956 /* ifp_addrhead is already updated */
961 TAILQ_REMOVE(&ifp->if_addrhead, ifa, ifa_link);
962 IF_ADDR_WUNLOCK(ifp);
968 * Remove any multicast network addresses from an interface when an ifnet
972 if_purgemaddrs(struct ifnet *ifp)
974 struct ifmultiaddr *ifma;
975 struct ifmultiaddr *next;
978 TAILQ_FOREACH_SAFE(ifma, &ifp->if_multiaddrs, ifma_link, next)
979 if_delmulti_locked(ifp, ifma, 1);
980 IF_ADDR_WUNLOCK(ifp);
984 * Detach an interface, removing it from the list of "active" interfaces.
985 * If vmove flag is set on entry to if_detach_internal(), perform only a
986 * limited subset of cleanup tasks, given that we are moving an ifnet from
987 * one vnet to another, where it must be fully operational.
989 * XXXRW: There are some significant questions about event ordering, and
990 * how to prevent things from starting to use the interface during detach.
993 if_detach(struct ifnet *ifp)
996 CURVNET_SET_QUIET(ifp->if_vnet);
997 if_detach_internal(ifp, 0, NULL);
1002 * The vmove flag, if set, indicates that we are called from a callpath
1003 * that is moving an interface to a different vnet instance.
1005 * The shutdown flag, if set, indicates that we are called in the
1006 * process of shutting down a vnet instance. Currently only the
1007 * vnet_if_return SYSUNINIT function sets it. Note: we can be called
1008 * on a vnet instance shutdown without this flag being set, e.g., when
1009 * the cloned interfaces are destoyed as first thing of teardown.
1012 if_detach_internal(struct ifnet *ifp, int vmove, struct if_clone **ifcp)
1022 shutdown = (ifp->if_vnet->vnet_state > SI_SUB_VNET &&
1023 ifp->if_vnet->vnet_state < SI_SUB_VNET_DONE) ? 1 : 0;
1026 TAILQ_FOREACH(iter, &V_ifnet, if_link)
1028 TAILQ_REMOVE(&V_ifnet, ifp, if_link);
1035 * While we would want to panic here, we cannot
1036 * guarantee that the interface is indeed still on
1037 * the list given we don't hold locks all the way.
1042 panic("%s: ifp=%p not on the ifnet tailq %p",
1043 __func__, ifp, &V_ifnet);
1045 return; /* XXX this should panic as well? */
1050 * At this point we know the interface still was on the ifnet list
1051 * and we removed it so we are in a stable state.
1054 curvnet->vnet_ifcnt--;
1058 * In any case (destroy or vmove) detach us from the groups
1059 * and remove/wait for pending events on the taskq.
1060 * XXX-BZ in theory an interface could still enqueue a taskq change?
1064 taskqueue_drain(taskqueue_swi, &ifp->if_linktask);
1067 * Check if this is a cloned interface or not. Must do even if
1068 * shutting down as a if_vmove_reclaim() would move the ifp and
1069 * the if_clone_addgroup() will have a corrupted string overwise
1070 * from a gibberish pointer.
1072 if (vmove && ifcp != NULL)
1073 *ifcp = if_clone_findifc(ifp);
1079 * On VNET shutdown abort here as the stack teardown will do all
1080 * the work top-down for us.
1084 * In case of a vmove we are done here without error.
1085 * If we would signal an error it would lead to the same
1086 * abort as if we did not find the ifnet anymore.
1087 * if_detach() calls us in void context and does not care
1088 * about an early abort notification, so life is splendid :)
1090 goto finish_vnet_shutdown;
1095 * At this point we are not tearing down a VNET and are either
1096 * going to destroy or vmove the interface and have to cleanup
1101 * Remove routes and flush queues.
1104 if (ALTQ_IS_ENABLED(&ifp->if_snd))
1105 altq_disable(&ifp->if_snd);
1106 if (ALTQ_IS_ATTACHED(&ifp->if_snd))
1107 altq_detach(&ifp->if_snd);
1118 * Remove all IPv6 kernel structs related to ifp. This should be done
1119 * before removing routing entries below, since IPv6 interface direct
1120 * routes are expected to be removed by the IPv6-specific kernel API.
1121 * Otherwise, the kernel will detect some inconsistency and bark it.
1125 if_purgemaddrs(ifp);
1127 /* Announce that the interface is gone. */
1128 rt_ifannouncemsg(ifp, IFAN_DEPARTURE);
1129 EVENTHANDLER_INVOKE(ifnet_departure_event, ifp);
1130 if (IS_DEFAULT_VNET(curvnet))
1131 devctl_notify("IFNET", ifp->if_xname, "DETACH", NULL);
1135 * Prevent further calls into the device driver via ifnet.
1140 * Remove link ifaddr pointer and maybe decrement if_index.
1141 * Clean up all addresses.
1143 free(ifp->if_hw_addr, M_IFADDR);
1144 ifp->if_hw_addr = NULL;
1145 ifp->if_addr = NULL;
1147 /* We can now free link ifaddr. */
1149 if (!TAILQ_EMPTY(&ifp->if_addrhead)) {
1150 ifa = TAILQ_FIRST(&ifp->if_addrhead);
1151 TAILQ_REMOVE(&ifp->if_addrhead, ifa, ifa_link);
1152 IF_ADDR_WUNLOCK(ifp);
1155 IF_ADDR_WUNLOCK(ifp);
1158 rt_flushifroutes(ifp);
1161 finish_vnet_shutdown:
1164 * We cannot hold the lock over dom_ifdetach calls as they might
1165 * sleep, for example trying to drain a callout, thus open up the
1166 * theoretical race with re-attaching.
1168 IF_AFDATA_LOCK(ifp);
1169 i = ifp->if_afdata_initialized;
1170 ifp->if_afdata_initialized = 0;
1171 IF_AFDATA_UNLOCK(ifp);
1172 for (dp = domains; i > 0 && dp; dp = dp->dom_next) {
1173 if (dp->dom_ifdetach && ifp->if_afdata[dp->dom_family]) {
1174 (*dp->dom_ifdetach)(ifp,
1175 ifp->if_afdata[dp->dom_family]);
1176 ifp->if_afdata[dp->dom_family] = NULL;
1185 * if_vmove() performs a limited version of if_detach() in current
1186 * vnet and if_attach()es the ifnet to the vnet specified as 2nd arg.
1187 * An attempt is made to shrink if_index in current vnet, find an
1188 * unused if_index in target vnet and calls if_grow() if necessary,
1189 * and finally find an unused if_xname for the target vnet.
1192 if_vmove(struct ifnet *ifp, struct vnet *new_vnet)
1194 struct if_clone *ifc;
1195 u_int bif_dlt, bif_hdrlen;
1199 * if_detach_internal() will call the eventhandler to notify
1200 * interface departure. That will detach if_bpf. We need to
1201 * safe the dlt and hdrlen so we can re-attach it later.
1203 bpf_get_bp_params(ifp->if_bpf, &bif_dlt, &bif_hdrlen);
1206 * Detach from current vnet, but preserve LLADDR info, do not
1207 * mark as dead etc. so that the ifnet can be reattached later.
1208 * If we cannot find it, we lost the race to someone else.
1210 rc = if_detach_internal(ifp, 1, &ifc);
1215 * Unlink the ifnet from ifindex_table[] in current vnet, and shrink
1216 * the if_index for that vnet if possible.
1218 * NOTE: IFNET_WLOCK/IFNET_WUNLOCK() are assumed to be unvirtualized,
1219 * or we'd lock on one vnet and unlock on another.
1222 ifindex_free_locked(ifp->if_index);
1226 * Perform interface-specific reassignment tasks, if provided by
1229 if (ifp->if_reassign != NULL)
1230 ifp->if_reassign(ifp, new_vnet, NULL);
1233 * Switch to the context of the target vnet.
1235 CURVNET_SET_QUIET(new_vnet);
1238 ifp->if_index = ifindex_alloc();
1239 ifnet_setbyindex_locked(ifp->if_index, ifp);
1242 if_attach_internal(ifp, 1, ifc);
1244 if (ifp->if_bpf == NULL)
1245 bpfattach(ifp, bif_dlt, bif_hdrlen);
1251 * Move an ifnet to or from another child prison/vnet, specified by the jail id.
1254 if_vmove_loan(struct thread *td, struct ifnet *ifp, char *ifname, int jid)
1260 /* Try to find the prison within our visibility. */
1261 sx_slock(&allprison_lock);
1262 pr = prison_find_child(td->td_ucred->cr_prison, jid);
1263 sx_sunlock(&allprison_lock);
1266 prison_hold_locked(pr);
1267 mtx_unlock(&pr->pr_mtx);
1269 /* Do not try to move the iface from and to the same prison. */
1270 if (pr->pr_vnet == ifp->if_vnet) {
1275 /* Make sure the named iface does not exists in the dst. prison/vnet. */
1276 /* XXX Lock interfaces to avoid races. */
1277 CURVNET_SET_QUIET(pr->pr_vnet);
1278 difp = ifunit(ifname);
1285 /* Make sure the VNET is stable. */
1286 shutdown = (ifp->if_vnet->vnet_state > SI_SUB_VNET &&
1287 ifp->if_vnet->vnet_state < SI_SUB_VNET_DONE) ? 1 : 0;
1295 /* Move the interface into the child jail/vnet. */
1296 if_vmove(ifp, pr->pr_vnet);
1298 /* Report the new if_xname back to the userland. */
1299 sprintf(ifname, "%s", ifp->if_xname);
1306 if_vmove_reclaim(struct thread *td, char *ifname, int jid)
1309 struct vnet *vnet_dst;
1313 /* Try to find the prison within our visibility. */
1314 sx_slock(&allprison_lock);
1315 pr = prison_find_child(td->td_ucred->cr_prison, jid);
1316 sx_sunlock(&allprison_lock);
1319 prison_hold_locked(pr);
1320 mtx_unlock(&pr->pr_mtx);
1322 /* Make sure the named iface exists in the source prison/vnet. */
1323 CURVNET_SET(pr->pr_vnet);
1324 ifp = ifunit(ifname); /* XXX Lock to avoid races. */
1331 /* Do not try to move the iface from and to the same prison. */
1332 vnet_dst = TD_TO_VNET(td);
1333 if (vnet_dst == ifp->if_vnet) {
1339 /* Make sure the VNET is stable. */
1340 shutdown = (ifp->if_vnet->vnet_state > SI_SUB_VNET &&
1341 ifp->if_vnet->vnet_state < SI_SUB_VNET_DONE) ? 1 : 0;
1348 /* Get interface back from child jail/vnet. */
1349 if_vmove(ifp, vnet_dst);
1352 /* Report the new if_xname back to the userland. */
1353 sprintf(ifname, "%s", ifp->if_xname);
1361 * Add a group to an interface
1364 if_addgroup(struct ifnet *ifp, const char *groupname)
1366 struct ifg_list *ifgl;
1367 struct ifg_group *ifg = NULL;
1368 struct ifg_member *ifgm;
1371 if (groupname[0] && groupname[strlen(groupname) - 1] >= '0' &&
1372 groupname[strlen(groupname) - 1] <= '9')
1376 TAILQ_FOREACH(ifgl, &ifp->if_groups, ifgl_next)
1377 if (!strcmp(ifgl->ifgl_group->ifg_group, groupname)) {
1382 if ((ifgl = (struct ifg_list *)malloc(sizeof(struct ifg_list), M_TEMP,
1383 M_NOWAIT)) == NULL) {
1388 if ((ifgm = (struct ifg_member *)malloc(sizeof(struct ifg_member),
1389 M_TEMP, M_NOWAIT)) == NULL) {
1395 TAILQ_FOREACH(ifg, &V_ifg_head, ifg_next)
1396 if (!strcmp(ifg->ifg_group, groupname))
1400 if ((ifg = (struct ifg_group *)malloc(sizeof(struct ifg_group),
1401 M_TEMP, M_NOWAIT)) == NULL) {
1407 strlcpy(ifg->ifg_group, groupname, sizeof(ifg->ifg_group));
1408 ifg->ifg_refcnt = 0;
1409 TAILQ_INIT(&ifg->ifg_members);
1410 TAILQ_INSERT_TAIL(&V_ifg_head, ifg, ifg_next);
1415 ifgl->ifgl_group = ifg;
1416 ifgm->ifgm_ifp = ifp;
1419 TAILQ_INSERT_TAIL(&ifg->ifg_members, ifgm, ifgm_next);
1420 TAILQ_INSERT_TAIL(&ifp->if_groups, ifgl, ifgl_next);
1421 IF_ADDR_WUNLOCK(ifp);
1426 EVENTHANDLER_INVOKE(group_attach_event, ifg);
1427 EVENTHANDLER_INVOKE(group_change_event, groupname);
1433 * Remove a group from an interface
1436 if_delgroup(struct ifnet *ifp, const char *groupname)
1438 struct ifg_list *ifgl;
1439 struct ifg_member *ifgm;
1442 TAILQ_FOREACH(ifgl, &ifp->if_groups, ifgl_next)
1443 if (!strcmp(ifgl->ifgl_group->ifg_group, groupname))
1451 TAILQ_REMOVE(&ifp->if_groups, ifgl, ifgl_next);
1452 IF_ADDR_WUNLOCK(ifp);
1454 TAILQ_FOREACH(ifgm, &ifgl->ifgl_group->ifg_members, ifgm_next)
1455 if (ifgm->ifgm_ifp == ifp)
1459 TAILQ_REMOVE(&ifgl->ifgl_group->ifg_members, ifgm, ifgm_next);
1463 if (--ifgl->ifgl_group->ifg_refcnt == 0) {
1464 TAILQ_REMOVE(&V_ifg_head, ifgl->ifgl_group, ifg_next);
1466 EVENTHANDLER_INVOKE(group_detach_event, ifgl->ifgl_group);
1467 free(ifgl->ifgl_group, M_TEMP);
1473 EVENTHANDLER_INVOKE(group_change_event, groupname);
1479 * Remove an interface from all groups
1482 if_delgroups(struct ifnet *ifp)
1484 struct ifg_list *ifgl;
1485 struct ifg_member *ifgm;
1486 char groupname[IFNAMSIZ];
1489 while (!TAILQ_EMPTY(&ifp->if_groups)) {
1490 ifgl = TAILQ_FIRST(&ifp->if_groups);
1492 strlcpy(groupname, ifgl->ifgl_group->ifg_group, IFNAMSIZ);
1495 TAILQ_REMOVE(&ifp->if_groups, ifgl, ifgl_next);
1496 IF_ADDR_WUNLOCK(ifp);
1498 TAILQ_FOREACH(ifgm, &ifgl->ifgl_group->ifg_members, ifgm_next)
1499 if (ifgm->ifgm_ifp == ifp)
1503 TAILQ_REMOVE(&ifgl->ifgl_group->ifg_members, ifgm,
1508 if (--ifgl->ifgl_group->ifg_refcnt == 0) {
1509 TAILQ_REMOVE(&V_ifg_head, ifgl->ifgl_group, ifg_next);
1511 EVENTHANDLER_INVOKE(group_detach_event,
1513 free(ifgl->ifgl_group, M_TEMP);
1519 EVENTHANDLER_INVOKE(group_change_event, groupname);
1527 ifgr_group_get(void *ifgrp)
1529 union ifgroupreq_union *ifgrup;
1532 #ifdef COMPAT_FREEBSD32
1533 if (SV_CURPROC_FLAG(SV_ILP32))
1534 return (&ifgrup->ifgr32.ifgr_ifgru.ifgru_group[0]);
1536 return (&ifgrup->ifgr.ifgr_ifgru.ifgru_group[0]);
1539 static struct ifg_req *
1540 ifgr_groups_get(void *ifgrp)
1542 union ifgroupreq_union *ifgrup;
1545 #ifdef COMPAT_FREEBSD32
1546 if (SV_CURPROC_FLAG(SV_ILP32))
1547 return ((struct ifg_req *)(uintptr_t)
1548 ifgrup->ifgr32.ifgr_ifgru.ifgru_groups);
1550 return (ifgrup->ifgr.ifgr_ifgru.ifgru_groups);
1554 * Stores all groups from an interface in memory pointed to by ifgr.
1557 if_getgroup(struct ifgroupreq *ifgr, struct ifnet *ifp)
1560 struct ifg_list *ifgl;
1561 struct ifg_req ifgrq, *ifgp;
1563 if (ifgr->ifgr_len == 0) {
1565 TAILQ_FOREACH(ifgl, &ifp->if_groups, ifgl_next)
1566 ifgr->ifgr_len += sizeof(struct ifg_req);
1567 IF_ADDR_RUNLOCK(ifp);
1571 len = ifgr->ifgr_len;
1572 ifgp = ifgr_groups_get(ifgr);
1575 TAILQ_FOREACH(ifgl, &ifp->if_groups, ifgl_next) {
1576 if (len < sizeof(ifgrq)) {
1577 IF_ADDR_RUNLOCK(ifp);
1580 bzero(&ifgrq, sizeof ifgrq);
1581 strlcpy(ifgrq.ifgrq_group, ifgl->ifgl_group->ifg_group,
1582 sizeof(ifgrq.ifgrq_group));
1583 if ((error = copyout(&ifgrq, ifgp, sizeof(struct ifg_req)))) {
1584 IF_ADDR_RUNLOCK(ifp);
1587 len -= sizeof(ifgrq);
1590 IF_ADDR_RUNLOCK(ifp);
1596 * Stores all members of a group in memory pointed to by igfr
1599 if_getgroupmembers(struct ifgroupreq *ifgr)
1601 struct ifg_group *ifg;
1602 struct ifg_member *ifgm;
1603 struct ifg_req ifgrq, *ifgp;
1607 TAILQ_FOREACH(ifg, &V_ifg_head, ifg_next)
1608 if (!strcmp(ifg->ifg_group, ifgr->ifgr_name))
1615 if (ifgr->ifgr_len == 0) {
1616 TAILQ_FOREACH(ifgm, &ifg->ifg_members, ifgm_next)
1617 ifgr->ifgr_len += sizeof(ifgrq);
1622 len = ifgr->ifgr_len;
1623 ifgp = ifgr_groups_get(ifgr);
1624 TAILQ_FOREACH(ifgm, &ifg->ifg_members, ifgm_next) {
1625 if (len < sizeof(ifgrq)) {
1629 bzero(&ifgrq, sizeof ifgrq);
1630 strlcpy(ifgrq.ifgrq_member, ifgm->ifgm_ifp->if_xname,
1631 sizeof(ifgrq.ifgrq_member));
1632 if ((error = copyout(&ifgrq, ifgp, sizeof(struct ifg_req)))) {
1636 len -= sizeof(ifgrq);
1645 * Return counter values from counter(9)s stored in ifnet.
1648 if_get_counter_default(struct ifnet *ifp, ift_counter cnt)
1651 KASSERT(cnt < IFCOUNTERS, ("%s: invalid cnt %d", __func__, cnt));
1653 return (counter_u64_fetch(ifp->if_counters[cnt]));
1657 * Increase an ifnet counter. Usually used for counters shared
1658 * between the stack and a driver, but function supports them all.
1661 if_inc_counter(struct ifnet *ifp, ift_counter cnt, int64_t inc)
1664 KASSERT(cnt < IFCOUNTERS, ("%s: invalid cnt %d", __func__, cnt));
1666 counter_u64_add(ifp->if_counters[cnt], inc);
1670 * Copy data from ifnet to userland API structure if_data.
1673 if_data_copy(struct ifnet *ifp, struct if_data *ifd)
1676 ifd->ifi_type = ifp->if_type;
1677 ifd->ifi_physical = 0;
1678 ifd->ifi_addrlen = ifp->if_addrlen;
1679 ifd->ifi_hdrlen = ifp->if_hdrlen;
1680 ifd->ifi_link_state = ifp->if_link_state;
1682 ifd->ifi_datalen = sizeof(struct if_data);
1683 ifd->ifi_mtu = ifp->if_mtu;
1684 ifd->ifi_metric = ifp->if_metric;
1685 ifd->ifi_baudrate = ifp->if_baudrate;
1686 ifd->ifi_hwassist = ifp->if_hwassist;
1687 ifd->ifi_epoch = ifp->if_epoch;
1688 ifd->ifi_lastchange = ifp->if_lastchange;
1690 ifd->ifi_ipackets = ifp->if_get_counter(ifp, IFCOUNTER_IPACKETS);
1691 ifd->ifi_ierrors = ifp->if_get_counter(ifp, IFCOUNTER_IERRORS);
1692 ifd->ifi_opackets = ifp->if_get_counter(ifp, IFCOUNTER_OPACKETS);
1693 ifd->ifi_oerrors = ifp->if_get_counter(ifp, IFCOUNTER_OERRORS);
1694 ifd->ifi_collisions = ifp->if_get_counter(ifp, IFCOUNTER_COLLISIONS);
1695 ifd->ifi_ibytes = ifp->if_get_counter(ifp, IFCOUNTER_IBYTES);
1696 ifd->ifi_obytes = ifp->if_get_counter(ifp, IFCOUNTER_OBYTES);
1697 ifd->ifi_imcasts = ifp->if_get_counter(ifp, IFCOUNTER_IMCASTS);
1698 ifd->ifi_omcasts = ifp->if_get_counter(ifp, IFCOUNTER_OMCASTS);
1699 ifd->ifi_iqdrops = ifp->if_get_counter(ifp, IFCOUNTER_IQDROPS);
1700 ifd->ifi_oqdrops = ifp->if_get_counter(ifp, IFCOUNTER_OQDROPS);
1701 ifd->ifi_noproto = ifp->if_get_counter(ifp, IFCOUNTER_NOPROTO);
1705 * Wrapper functions for struct ifnet address list locking macros. These are
1706 * used by kernel modules to avoid encoding programming interface or binary
1707 * interface assumptions that may be violated when kernel-internal locking
1708 * approaches change.
1711 if_addr_rlock(struct ifnet *ifp)
1718 if_addr_runlock(struct ifnet *ifp)
1721 IF_ADDR_RUNLOCK(ifp);
1725 if_maddr_rlock(if_t ifp)
1728 IF_ADDR_RLOCK((struct ifnet *)ifp);
1732 if_maddr_runlock(if_t ifp)
1735 IF_ADDR_RUNLOCK((struct ifnet *)ifp);
1739 * Initialization, destruction and refcounting functions for ifaddrs.
1742 ifa_alloc(size_t size, int flags)
1746 KASSERT(size >= sizeof(struct ifaddr),
1747 ("%s: invalid size %zu", __func__, size));
1749 ifa = malloc(size, M_IFADDR, M_ZERO | flags);
1753 if ((ifa->ifa_opackets = counter_u64_alloc(flags)) == NULL)
1755 if ((ifa->ifa_ipackets = counter_u64_alloc(flags)) == NULL)
1757 if ((ifa->ifa_obytes = counter_u64_alloc(flags)) == NULL)
1759 if ((ifa->ifa_ibytes = counter_u64_alloc(flags)) == NULL)
1762 refcount_init(&ifa->ifa_refcnt, 1);
1767 /* free(NULL) is okay */
1768 counter_u64_free(ifa->ifa_opackets);
1769 counter_u64_free(ifa->ifa_ipackets);
1770 counter_u64_free(ifa->ifa_obytes);
1771 counter_u64_free(ifa->ifa_ibytes);
1772 free(ifa, M_IFADDR);
1778 ifa_ref(struct ifaddr *ifa)
1781 refcount_acquire(&ifa->ifa_refcnt);
1785 ifa_free(struct ifaddr *ifa)
1788 if (refcount_release(&ifa->ifa_refcnt)) {
1789 counter_u64_free(ifa->ifa_opackets);
1790 counter_u64_free(ifa->ifa_ipackets);
1791 counter_u64_free(ifa->ifa_obytes);
1792 counter_u64_free(ifa->ifa_ibytes);
1793 free(ifa, M_IFADDR);
1798 ifa_maintain_loopback_route(int cmd, const char *otype, struct ifaddr *ifa,
1799 struct sockaddr *ia)
1802 struct rt_addrinfo info;
1803 struct sockaddr_dl null_sdl;
1808 bzero(&info, sizeof(info));
1809 if (cmd != RTM_DELETE)
1810 info.rti_ifp = V_loif;
1811 info.rti_flags = ifa->ifa_flags | RTF_HOST | RTF_STATIC | RTF_PINNED;
1812 info.rti_info[RTAX_DST] = ia;
1813 info.rti_info[RTAX_GATEWAY] = (struct sockaddr *)&null_sdl;
1814 link_init_sdl(ifp, (struct sockaddr *)&null_sdl, ifp->if_type);
1816 error = rtrequest1_fib(cmd, &info, NULL, ifp->if_fib);
1819 log(LOG_DEBUG, "%s: %s failed for interface %s: %u\n",
1820 __func__, otype, if_name(ifp), error);
1826 ifa_add_loopback_route(struct ifaddr *ifa, struct sockaddr *ia)
1829 return (ifa_maintain_loopback_route(RTM_ADD, "insertion", ifa, ia));
1833 ifa_del_loopback_route(struct ifaddr *ifa, struct sockaddr *ia)
1836 return (ifa_maintain_loopback_route(RTM_DELETE, "deletion", ifa, ia));
1840 ifa_switch_loopback_route(struct ifaddr *ifa, struct sockaddr *ia)
1843 return (ifa_maintain_loopback_route(RTM_CHANGE, "switch", ifa, ia));
1847 * XXX: Because sockaddr_dl has deeper structure than the sockaddr
1848 * structs used to represent other address families, it is necessary
1849 * to perform a different comparison.
1852 #define sa_dl_equal(a1, a2) \
1853 ((((const struct sockaddr_dl *)(a1))->sdl_len == \
1854 ((const struct sockaddr_dl *)(a2))->sdl_len) && \
1855 (bcmp(CLLADDR((const struct sockaddr_dl *)(a1)), \
1856 CLLADDR((const struct sockaddr_dl *)(a2)), \
1857 ((const struct sockaddr_dl *)(a1))->sdl_alen) == 0))
1860 * Locate an interface based on a complete address.
1863 static struct ifaddr *
1864 ifa_ifwithaddr_internal(const struct sockaddr *addr, int getref)
1869 IFNET_RLOCK_NOSLEEP();
1870 TAILQ_FOREACH(ifp, &V_ifnet, if_link) {
1872 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
1873 if (ifa->ifa_addr->sa_family != addr->sa_family)
1875 if (sa_equal(addr, ifa->ifa_addr)) {
1878 IF_ADDR_RUNLOCK(ifp);
1881 /* IP6 doesn't have broadcast */
1882 if ((ifp->if_flags & IFF_BROADCAST) &&
1883 ifa->ifa_broadaddr &&
1884 ifa->ifa_broadaddr->sa_len != 0 &&
1885 sa_equal(ifa->ifa_broadaddr, addr)) {
1888 IF_ADDR_RUNLOCK(ifp);
1892 IF_ADDR_RUNLOCK(ifp);
1896 IFNET_RUNLOCK_NOSLEEP();
1901 ifa_ifwithaddr(const struct sockaddr *addr)
1904 return (ifa_ifwithaddr_internal(addr, 1));
1908 ifa_ifwithaddr_check(const struct sockaddr *addr)
1911 return (ifa_ifwithaddr_internal(addr, 0) != NULL);
1915 * Locate an interface based on the broadcast address.
1919 ifa_ifwithbroadaddr(const struct sockaddr *addr, int fibnum)
1924 IFNET_RLOCK_NOSLEEP();
1925 TAILQ_FOREACH(ifp, &V_ifnet, if_link) {
1926 if ((fibnum != RT_ALL_FIBS) && (ifp->if_fib != fibnum))
1929 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
1930 if (ifa->ifa_addr->sa_family != addr->sa_family)
1932 if ((ifp->if_flags & IFF_BROADCAST) &&
1933 ifa->ifa_broadaddr &&
1934 ifa->ifa_broadaddr->sa_len != 0 &&
1935 sa_equal(ifa->ifa_broadaddr, addr)) {
1937 IF_ADDR_RUNLOCK(ifp);
1941 IF_ADDR_RUNLOCK(ifp);
1945 IFNET_RUNLOCK_NOSLEEP();
1950 * Locate the point to point interface with a given destination address.
1954 ifa_ifwithdstaddr(const struct sockaddr *addr, int fibnum)
1959 IFNET_RLOCK_NOSLEEP();
1960 TAILQ_FOREACH(ifp, &V_ifnet, if_link) {
1961 if ((ifp->if_flags & IFF_POINTOPOINT) == 0)
1963 if ((fibnum != RT_ALL_FIBS) && (ifp->if_fib != fibnum))
1966 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
1967 if (ifa->ifa_addr->sa_family != addr->sa_family)
1969 if (ifa->ifa_dstaddr != NULL &&
1970 sa_equal(addr, ifa->ifa_dstaddr)) {
1972 IF_ADDR_RUNLOCK(ifp);
1976 IF_ADDR_RUNLOCK(ifp);
1980 IFNET_RUNLOCK_NOSLEEP();
1985 * Find an interface on a specific network. If many, choice
1986 * is most specific found.
1989 ifa_ifwithnet(const struct sockaddr *addr, int ignore_ptp, int fibnum)
1993 struct ifaddr *ifa_maybe = NULL;
1994 u_int af = addr->sa_family;
1995 const char *addr_data = addr->sa_data, *cplim;
1998 * AF_LINK addresses can be looked up directly by their index number,
1999 * so do that if we can.
2001 if (af == AF_LINK) {
2002 const struct sockaddr_dl *sdl = (const struct sockaddr_dl *)addr;
2003 if (sdl->sdl_index && sdl->sdl_index <= V_if_index)
2004 return (ifaddr_byindex(sdl->sdl_index));
2008 * Scan though each interface, looking for ones that have addresses
2009 * in this address family and the requested fib. Maintain a reference
2010 * on ifa_maybe once we find one, as we release the IF_ADDR_RLOCK() that
2011 * kept it stable when we move onto the next interface.
2013 IFNET_RLOCK_NOSLEEP();
2014 TAILQ_FOREACH(ifp, &V_ifnet, if_link) {
2015 if ((fibnum != RT_ALL_FIBS) && (ifp->if_fib != fibnum))
2018 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
2019 const char *cp, *cp2, *cp3;
2021 if (ifa->ifa_addr->sa_family != af)
2023 if (af == AF_INET &&
2024 ifp->if_flags & IFF_POINTOPOINT && !ignore_ptp) {
2026 * This is a bit broken as it doesn't
2027 * take into account that the remote end may
2028 * be a single node in the network we are
2030 * The trouble is that we don't know the
2031 * netmask for the remote end.
2033 if (ifa->ifa_dstaddr != NULL &&
2034 sa_equal(addr, ifa->ifa_dstaddr)) {
2036 IF_ADDR_RUNLOCK(ifp);
2041 * Scan all the bits in the ifa's address.
2042 * If a bit dissagrees with what we are
2043 * looking for, mask it with the netmask
2044 * to see if it really matters.
2045 * (A byte at a time)
2047 if (ifa->ifa_netmask == 0)
2050 cp2 = ifa->ifa_addr->sa_data;
2051 cp3 = ifa->ifa_netmask->sa_data;
2052 cplim = ifa->ifa_netmask->sa_len
2053 + (char *)ifa->ifa_netmask;
2055 if ((*cp++ ^ *cp2++) & *cp3++)
2056 goto next; /* next address! */
2058 * If the netmask of what we just found
2059 * is more specific than what we had before
2060 * (if we had one), or if the virtual status
2061 * of new prefix is better than of the old one,
2062 * then remember the new one before continuing
2063 * to search for an even better one.
2065 if (ifa_maybe == NULL ||
2066 ifa_preferred(ifa_maybe, ifa) ||
2067 rn_refines((caddr_t)ifa->ifa_netmask,
2068 (caddr_t)ifa_maybe->ifa_netmask)) {
2069 if (ifa_maybe != NULL)
2070 ifa_free(ifa_maybe);
2076 IF_ADDR_RUNLOCK(ifp);
2081 IFNET_RUNLOCK_NOSLEEP();
2082 if (ifa_maybe != NULL)
2083 ifa_free(ifa_maybe);
2088 * Find an interface address specific to an interface best matching
2092 ifaof_ifpforaddr(const struct sockaddr *addr, struct ifnet *ifp)
2095 const char *cp, *cp2, *cp3;
2097 struct ifaddr *ifa_maybe = NULL;
2098 u_int af = addr->sa_family;
2103 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
2104 if (ifa->ifa_addr->sa_family != af)
2106 if (ifa_maybe == NULL)
2108 if (ifa->ifa_netmask == 0) {
2109 if (sa_equal(addr, ifa->ifa_addr) ||
2110 (ifa->ifa_dstaddr &&
2111 sa_equal(addr, ifa->ifa_dstaddr)))
2115 if (ifp->if_flags & IFF_POINTOPOINT) {
2116 if (sa_equal(addr, ifa->ifa_dstaddr))
2120 cp2 = ifa->ifa_addr->sa_data;
2121 cp3 = ifa->ifa_netmask->sa_data;
2122 cplim = ifa->ifa_netmask->sa_len + (char *)ifa->ifa_netmask;
2123 for (; cp3 < cplim; cp3++)
2124 if ((*cp++ ^ *cp2++) & *cp3)
2134 IF_ADDR_RUNLOCK(ifp);
2139 * See whether new ifa is better than current one:
2140 * 1) A non-virtual one is preferred over virtual.
2141 * 2) A virtual in master state preferred over any other state.
2143 * Used in several address selecting functions.
2146 ifa_preferred(struct ifaddr *cur, struct ifaddr *next)
2149 return (cur->ifa_carp && (!next->ifa_carp ||
2150 ((*carp_master_p)(next) && !(*carp_master_p)(cur))));
2153 #include <net/if_llatbl.h>
2156 * Default action when installing a route with a Link Level gateway.
2157 * Lookup an appropriate real ifa to point to.
2158 * This should be moved to /sys/net/link.c eventually.
2161 link_rtrequest(int cmd, struct rtentry *rt, struct rt_addrinfo *info)
2163 struct ifaddr *ifa, *oifa;
2164 struct sockaddr *dst;
2167 if (cmd != RTM_ADD || ((ifa = rt->rt_ifa) == NULL) ||
2168 ((ifp = ifa->ifa_ifp) == NULL) || ((dst = rt_key(rt)) == NULL))
2170 ifa = ifaof_ifpforaddr(dst, ifp);
2175 if (ifa->ifa_rtrequest && ifa->ifa_rtrequest != link_rtrequest)
2176 ifa->ifa_rtrequest(cmd, rt, info);
2180 struct sockaddr_dl *
2181 link_alloc_sdl(size_t size, int flags)
2184 return (malloc(size, M_TEMP, flags));
2188 link_free_sdl(struct sockaddr *sa)
2194 * Fills in given sdl with interface basic info.
2195 * Returns pointer to filled sdl.
2197 struct sockaddr_dl *
2198 link_init_sdl(struct ifnet *ifp, struct sockaddr *paddr, u_char iftype)
2200 struct sockaddr_dl *sdl;
2202 sdl = (struct sockaddr_dl *)paddr;
2203 memset(sdl, 0, sizeof(struct sockaddr_dl));
2204 sdl->sdl_len = sizeof(struct sockaddr_dl);
2205 sdl->sdl_family = AF_LINK;
2206 sdl->sdl_index = ifp->if_index;
2207 sdl->sdl_type = iftype;
2213 * Mark an interface down and notify protocols of
2217 if_unroute(struct ifnet *ifp, int flag, int fam)
2221 KASSERT(flag == IFF_UP, ("if_unroute: flag != IFF_UP"));
2223 ifp->if_flags &= ~flag;
2224 getmicrotime(&ifp->if_lastchange);
2225 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link)
2226 if (fam == PF_UNSPEC || (fam == ifa->ifa_addr->sa_family))
2227 pfctlinput(PRC_IFDOWN, ifa->ifa_addr);
2228 ifp->if_qflush(ifp);
2231 (*carp_linkstate_p)(ifp);
2236 * Mark an interface up and notify protocols of
2240 if_route(struct ifnet *ifp, int flag, int fam)
2244 KASSERT(flag == IFF_UP, ("if_route: flag != IFF_UP"));
2246 ifp->if_flags |= flag;
2247 getmicrotime(&ifp->if_lastchange);
2248 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link)
2249 if (fam == PF_UNSPEC || (fam == ifa->ifa_addr->sa_family))
2250 pfctlinput(PRC_IFUP, ifa->ifa_addr);
2252 (*carp_linkstate_p)(ifp);
2259 void (*vlan_link_state_p)(struct ifnet *); /* XXX: private from if_vlan */
2260 void (*vlan_trunk_cap_p)(struct ifnet *); /* XXX: private from if_vlan */
2261 struct ifnet *(*vlan_trunkdev_p)(struct ifnet *);
2262 struct ifnet *(*vlan_devat_p)(struct ifnet *, uint16_t);
2263 int (*vlan_tag_p)(struct ifnet *, uint16_t *);
2264 int (*vlan_setcookie_p)(struct ifnet *, void *);
2265 void *(*vlan_cookie_p)(struct ifnet *);
2268 * Handle a change in the interface link state. To avoid LORs
2269 * between driver lock and upper layer locks, as well as possible
2270 * recursions, we post event to taskqueue, and all job
2271 * is done in static do_link_state_change().
2274 if_link_state_change(struct ifnet *ifp, int link_state)
2276 /* Return if state hasn't changed. */
2277 if (ifp->if_link_state == link_state)
2280 ifp->if_link_state = link_state;
2282 taskqueue_enqueue(taskqueue_swi, &ifp->if_linktask);
2286 do_link_state_change(void *arg, int pending)
2288 struct ifnet *ifp = (struct ifnet *)arg;
2289 int link_state = ifp->if_link_state;
2290 CURVNET_SET(ifp->if_vnet);
2292 /* Notify that the link state has changed. */
2294 if (ifp->if_vlantrunk != NULL)
2295 (*vlan_link_state_p)(ifp);
2297 if ((ifp->if_type == IFT_ETHER || ifp->if_type == IFT_L2VLAN) &&
2298 ifp->if_l2com != NULL)
2299 (*ng_ether_link_state_p)(ifp, link_state);
2301 (*carp_linkstate_p)(ifp);
2303 (*bridge_linkstate_p)(ifp);
2305 (*lagg_linkstate_p)(ifp, link_state);
2307 if (IS_DEFAULT_VNET(curvnet))
2308 devctl_notify("IFNET", ifp->if_xname,
2309 (link_state == LINK_STATE_UP) ? "LINK_UP" : "LINK_DOWN",
2312 if_printf(ifp, "%d link states coalesced\n", pending);
2313 if (log_link_state_change)
2314 log(LOG_NOTICE, "%s: link state changed to %s\n", ifp->if_xname,
2315 (link_state == LINK_STATE_UP) ? "UP" : "DOWN" );
2316 EVENTHANDLER_INVOKE(ifnet_link_event, ifp, link_state);
2321 * Mark an interface down and notify protocols of
2325 if_down(struct ifnet *ifp)
2328 EVENTHANDLER_INVOKE(ifnet_event, ifp, IFNET_EVENT_DOWN);
2329 if_unroute(ifp, IFF_UP, AF_UNSPEC);
2333 * Mark an interface up and notify protocols of
2337 if_up(struct ifnet *ifp)
2340 if_route(ifp, IFF_UP, AF_UNSPEC);
2341 EVENTHANDLER_INVOKE(ifnet_event, ifp, IFNET_EVENT_UP);
2345 * Flush an interface queue.
2348 if_qflush(struct ifnet *ifp)
2356 if (ALTQ_IS_ENABLED(ifq))
2360 while ((m = n) != NULL) {
2371 * Map interface name to interface structure pointer, with or without
2372 * returning a reference.
2375 ifunit_ref(const char *name)
2379 IFNET_RLOCK_NOSLEEP();
2380 TAILQ_FOREACH(ifp, &V_ifnet, if_link) {
2381 if (strncmp(name, ifp->if_xname, IFNAMSIZ) == 0 &&
2382 !(ifp->if_flags & IFF_DYING))
2387 IFNET_RUNLOCK_NOSLEEP();
2392 ifunit(const char *name)
2396 IFNET_RLOCK_NOSLEEP();
2397 TAILQ_FOREACH(ifp, &V_ifnet, if_link) {
2398 if (strncmp(name, ifp->if_xname, IFNAMSIZ) == 0)
2401 IFNET_RUNLOCK_NOSLEEP();
2406 ifr_buffer_get_buffer(void *data)
2408 union ifreq_union *ifrup;
2411 #ifdef COMPAT_FREEBSD32
2412 if (SV_CURPROC_FLAG(SV_ILP32))
2413 return ((void *)(uintptr_t)
2414 ifrup->ifr32.ifr_ifru.ifru_buffer.buffer);
2416 return (ifrup->ifr.ifr_ifru.ifru_buffer.buffer);
2420 ifr_buffer_set_buffer_null(void *data)
2422 union ifreq_union *ifrup;
2425 #ifdef COMPAT_FREEBSD32
2426 if (SV_CURPROC_FLAG(SV_ILP32))
2427 ifrup->ifr32.ifr_ifru.ifru_buffer.buffer = 0;
2430 ifrup->ifr.ifr_ifru.ifru_buffer.buffer = NULL;
2434 ifr_buffer_get_length(void *data)
2436 union ifreq_union *ifrup;
2439 #ifdef COMPAT_FREEBSD32
2440 if (SV_CURPROC_FLAG(SV_ILP32))
2441 return (ifrup->ifr32.ifr_ifru.ifru_buffer.length);
2443 return (ifrup->ifr.ifr_ifru.ifru_buffer.length);
2447 ifr_buffer_set_length(void *data, size_t len)
2449 union ifreq_union *ifrup;
2452 #ifdef COMPAT_FREEBSD32
2453 if (SV_CURPROC_FLAG(SV_ILP32))
2454 ifrup->ifr32.ifr_ifru.ifru_buffer.length = len;
2457 ifrup->ifr.ifr_ifru.ifru_buffer.length = len;
2461 ifr_data_get_ptr(void *ifrp)
2463 union ifreq_union *ifrup;
2466 #ifdef COMPAT_FREEBSD32
2467 if (SV_CURPROC_FLAG(SV_ILP32))
2468 return ((void *)(uintptr_t)
2469 ifrup->ifr32.ifr_ifru.ifru_data);
2471 return (ifrup->ifr.ifr_ifru.ifru_data);
2475 * Hardware specific interface ioctls.
2478 ifhwioctl(u_long cmd, struct ifnet *ifp, caddr_t data, struct thread *td)
2481 int error = 0, do_ifup = 0;
2482 int new_flags, temp_flags;
2483 size_t namelen, onamelen;
2485 char *descrbuf, *odescrbuf;
2486 char new_name[IFNAMSIZ];
2488 struct sockaddr_dl *sdl;
2490 ifr = (struct ifreq *)data;
2493 ifr->ifr_index = ifp->if_index;
2497 temp_flags = ifp->if_flags | ifp->if_drv_flags;
2498 ifr->ifr_flags = temp_flags & 0xffff;
2499 ifr->ifr_flagshigh = temp_flags >> 16;
2503 ifr->ifr_reqcap = ifp->if_capabilities;
2504 ifr->ifr_curcap = ifp->if_capenable;
2509 error = mac_ifnet_ioctl_get(td->td_ucred, ifr, ifp);
2514 ifr->ifr_metric = ifp->if_metric;
2518 ifr->ifr_mtu = ifp->if_mtu;
2522 /* XXXGL: did this ever worked? */
2528 sx_slock(&ifdescr_sx);
2529 if (ifp->if_description == NULL)
2532 /* space for terminating nul */
2533 descrlen = strlen(ifp->if_description) + 1;
2534 if (ifr_buffer_get_length(ifr) < descrlen)
2535 ifr_buffer_set_buffer_null(ifr);
2537 error = copyout(ifp->if_description,
2538 ifr_buffer_get_buffer(ifr), descrlen);
2539 ifr_buffer_set_length(ifr, descrlen);
2541 sx_sunlock(&ifdescr_sx);
2545 error = priv_check(td, PRIV_NET_SETIFDESCR);
2550 * Copy only (length-1) bytes to make sure that
2551 * if_description is always nul terminated. The
2552 * length parameter is supposed to count the
2553 * terminating nul in.
2555 if (ifr_buffer_get_length(ifr) > ifdescr_maxlen)
2556 return (ENAMETOOLONG);
2557 else if (ifr_buffer_get_length(ifr) == 0)
2560 descrbuf = malloc(ifr_buffer_get_length(ifr),
2561 M_IFDESCR, M_WAITOK | M_ZERO);
2562 error = copyin(ifr_buffer_get_buffer(ifr), descrbuf,
2563 ifr_buffer_get_length(ifr) - 1);
2565 free(descrbuf, M_IFDESCR);
2570 sx_xlock(&ifdescr_sx);
2571 odescrbuf = ifp->if_description;
2572 ifp->if_description = descrbuf;
2573 sx_xunlock(&ifdescr_sx);
2575 getmicrotime(&ifp->if_lastchange);
2576 free(odescrbuf, M_IFDESCR);
2580 ifr->ifr_fib = ifp->if_fib;
2584 error = priv_check(td, PRIV_NET_SETIFFIB);
2587 if (ifr->ifr_fib >= rt_numfibs)
2590 ifp->if_fib = ifr->ifr_fib;
2594 error = priv_check(td, PRIV_NET_SETIFFLAGS);
2598 * Currently, no driver owned flags pass the IFF_CANTCHANGE
2599 * check, so we don't need special handling here yet.
2601 new_flags = (ifr->ifr_flags & 0xffff) |
2602 (ifr->ifr_flagshigh << 16);
2603 if (ifp->if_flags & IFF_UP &&
2604 (new_flags & IFF_UP) == 0) {
2606 } else if (new_flags & IFF_UP &&
2607 (ifp->if_flags & IFF_UP) == 0) {
2610 /* See if permanently promiscuous mode bit is about to flip */
2611 if ((ifp->if_flags ^ new_flags) & IFF_PPROMISC) {
2612 if (new_flags & IFF_PPROMISC)
2613 ifp->if_flags |= IFF_PROMISC;
2614 else if (ifp->if_pcount == 0)
2615 ifp->if_flags &= ~IFF_PROMISC;
2616 if (log_promisc_mode_change)
2617 log(LOG_INFO, "%s: permanently promiscuous mode %s\n",
2619 ((new_flags & IFF_PPROMISC) ?
2620 "enabled" : "disabled"));
2622 ifp->if_flags = (ifp->if_flags & IFF_CANTCHANGE) |
2623 (new_flags &~ IFF_CANTCHANGE);
2624 if (ifp->if_ioctl) {
2625 (void) (*ifp->if_ioctl)(ifp, cmd, data);
2629 getmicrotime(&ifp->if_lastchange);
2633 error = priv_check(td, PRIV_NET_SETIFCAP);
2636 if (ifp->if_ioctl == NULL)
2637 return (EOPNOTSUPP);
2638 if (ifr->ifr_reqcap & ~ifp->if_capabilities)
2640 error = (*ifp->if_ioctl)(ifp, cmd, data);
2642 getmicrotime(&ifp->if_lastchange);
2647 error = mac_ifnet_ioctl_set(td->td_ucred, ifr, ifp);
2652 error = priv_check(td, PRIV_NET_SETIFNAME);
2655 error = copyinstr(ifr_data_get_ptr(ifr), new_name, IFNAMSIZ,
2659 if (new_name[0] == '\0')
2661 if (new_name[IFNAMSIZ-1] != '\0') {
2662 new_name[IFNAMSIZ-1] = '\0';
2663 if (strlen(new_name) == IFNAMSIZ-1)
2666 if (ifunit(new_name) != NULL)
2670 * XXX: Locking. Nothing else seems to lock if_flags,
2671 * and there are numerous other races with the
2672 * ifunit() checks not being atomic with namespace
2673 * changes (renames, vmoves, if_attach, etc).
2675 ifp->if_flags |= IFF_RENAMING;
2677 /* Announce the departure of the interface. */
2678 rt_ifannouncemsg(ifp, IFAN_DEPARTURE);
2679 EVENTHANDLER_INVOKE(ifnet_departure_event, ifp);
2681 log(LOG_INFO, "%s: changing name to '%s'\n",
2682 ifp->if_xname, new_name);
2685 strlcpy(ifp->if_xname, new_name, sizeof(ifp->if_xname));
2687 sdl = (struct sockaddr_dl *)ifa->ifa_addr;
2688 namelen = strlen(new_name);
2689 onamelen = sdl->sdl_nlen;
2691 * Move the address if needed. This is safe because we
2692 * allocate space for a name of length IFNAMSIZ when we
2693 * create this in if_attach().
2695 if (namelen != onamelen) {
2696 bcopy(sdl->sdl_data + onamelen,
2697 sdl->sdl_data + namelen, sdl->sdl_alen);
2699 bcopy(new_name, sdl->sdl_data, namelen);
2700 sdl->sdl_nlen = namelen;
2701 sdl = (struct sockaddr_dl *)ifa->ifa_netmask;
2702 bzero(sdl->sdl_data, onamelen);
2703 while (namelen != 0)
2704 sdl->sdl_data[--namelen] = 0xff;
2705 IF_ADDR_WUNLOCK(ifp);
2707 EVENTHANDLER_INVOKE(ifnet_arrival_event, ifp);
2708 /* Announce the return of the interface. */
2709 rt_ifannouncemsg(ifp, IFAN_ARRIVAL);
2711 ifp->if_flags &= ~IFF_RENAMING;
2716 error = priv_check(td, PRIV_NET_SETIFVNET);
2719 error = if_vmove_loan(td, ifp, ifr->ifr_name, ifr->ifr_jid);
2724 error = priv_check(td, PRIV_NET_SETIFMETRIC);
2727 ifp->if_metric = ifr->ifr_metric;
2728 getmicrotime(&ifp->if_lastchange);
2732 error = priv_check(td, PRIV_NET_SETIFPHYS);
2735 if (ifp->if_ioctl == NULL)
2736 return (EOPNOTSUPP);
2737 error = (*ifp->if_ioctl)(ifp, cmd, data);
2739 getmicrotime(&ifp->if_lastchange);
2744 u_long oldmtu = ifp->if_mtu;
2746 error = priv_check(td, PRIV_NET_SETIFMTU);
2749 if (ifr->ifr_mtu < IF_MINMTU || ifr->ifr_mtu > IF_MAXMTU)
2751 if (ifp->if_ioctl == NULL)
2752 return (EOPNOTSUPP);
2753 error = (*ifp->if_ioctl)(ifp, cmd, data);
2755 getmicrotime(&ifp->if_lastchange);
2759 * If the link MTU changed, do network layer specific procedure.
2761 if (ifp->if_mtu != oldmtu) {
2772 if (cmd == SIOCADDMULTI)
2773 error = priv_check(td, PRIV_NET_ADDMULTI);
2775 error = priv_check(td, PRIV_NET_DELMULTI);
2779 /* Don't allow group membership on non-multicast interfaces. */
2780 if ((ifp->if_flags & IFF_MULTICAST) == 0)
2781 return (EOPNOTSUPP);
2783 /* Don't let users screw up protocols' entries. */
2784 if (ifr->ifr_addr.sa_family != AF_LINK)
2787 if (cmd == SIOCADDMULTI) {
2788 struct ifmultiaddr *ifma;
2791 * Userland is only permitted to join groups once
2792 * via the if_addmulti() KPI, because it cannot hold
2793 * struct ifmultiaddr * between calls. It may also
2794 * lose a race while we check if the membership
2798 ifma = if_findmulti(ifp, &ifr->ifr_addr);
2799 IF_ADDR_RUNLOCK(ifp);
2803 error = if_addmulti(ifp, &ifr->ifr_addr, &ifma);
2805 error = if_delmulti(ifp, &ifr->ifr_addr);
2808 getmicrotime(&ifp->if_lastchange);
2811 case SIOCSIFPHYADDR:
2812 case SIOCDIFPHYADDR:
2814 case SIOCSIFPHYADDR_IN6:
2817 case SIOCSIFGENERIC:
2818 error = priv_check(td, PRIV_NET_HWIOCTL);
2821 if (ifp->if_ioctl == NULL)
2822 return (EOPNOTSUPP);
2823 error = (*ifp->if_ioctl)(ifp, cmd, data);
2825 getmicrotime(&ifp->if_lastchange);
2829 case SIOCGIFPSRCADDR:
2830 case SIOCGIFPDSTADDR:
2833 case SIOCGIFGENERIC:
2835 case SIOCGIFRSSHASH:
2836 if (ifp->if_ioctl == NULL)
2837 return (EOPNOTSUPP);
2838 error = (*ifp->if_ioctl)(ifp, cmd, data);
2842 error = priv_check(td, PRIV_NET_SETLLADDR);
2845 error = if_setlladdr(ifp,
2846 ifr->ifr_addr.sa_data, ifr->ifr_addr.sa_len);
2850 error = if_gethwaddr(ifp, ifr);
2853 CASE_IOC_IFGROUPREQ(SIOCAIFGROUP):
2854 error = priv_check(td, PRIV_NET_ADDIFGROUP);
2857 if ((error = if_addgroup(ifp,
2858 ifgr_group_get((struct ifgroupreq *)data))))
2862 CASE_IOC_IFGROUPREQ(SIOCGIFGROUP):
2863 if ((error = if_getgroup((struct ifgroupreq *)data, ifp)))
2867 CASE_IOC_IFGROUPREQ(SIOCDIFGROUP):
2868 error = priv_check(td, PRIV_NET_DELIFGROUP);
2871 if ((error = if_delgroup(ifp,
2872 ifgr_group_get((struct ifgroupreq *)data))))
2883 #ifdef COMPAT_FREEBSD32
2891 #define SIOCGIFCONF32 _IOWR('i', 36, struct ifconf32)
2898 ifioctl(struct socket *so, u_long cmd, caddr_t data, struct thread *td)
2908 CURVNET_SET(so->so_vnet);
2910 /* Make sure the VNET is stable. */
2911 shutdown = (so->so_vnet->vnet_state > SI_SUB_VNET &&
2912 so->so_vnet->vnet_state < SI_SUB_VNET_DONE) ? 1 : 0;
2922 error = ifconf(cmd, data);
2926 #ifdef COMPAT_FREEBSD32
2929 struct ifconf32 *ifc32;
2932 ifc32 = (struct ifconf32 *)data;
2933 ifc.ifc_len = ifc32->ifc_len;
2934 ifc.ifc_buf = PTRIN(ifc32->ifc_buf);
2936 error = ifconf(SIOCGIFCONF, (void *)&ifc);
2939 ifc32->ifc_len = ifc.ifc_len;
2944 ifr = (struct ifreq *)data;
2949 error = priv_check(td, PRIV_NET_SETIFVNET);
2951 error = if_vmove_reclaim(td, ifr->ifr_name,
2958 error = priv_check(td, PRIV_NET_IFCREATE);
2960 error = if_clone_create(ifr->ifr_name,
2961 sizeof(ifr->ifr_name), cmd == SIOCIFCREATE2 ?
2962 ifr_data_get_ptr(ifr) : NULL);
2966 error = priv_check(td, PRIV_NET_IFDESTROY);
2968 error = if_clone_destroy(ifr->ifr_name);
2972 case SIOCIFGCLONERS:
2973 error = if_clone_list((struct if_clonereq *)data);
2976 CASE_IOC_IFGROUPREQ(SIOCGIFGMEMB):
2977 error = if_getgroupmembers((struct ifgroupreq *)data);
2980 #if defined(INET) || defined(INET6)
2983 if (carp_ioctl_p == NULL)
2984 error = EPROTONOSUPPORT;
2986 error = (*carp_ioctl_p)(ifr, cmd, td);
2992 ifp = ifunit_ref(ifr->ifr_name);
2998 error = ifhwioctl(cmd, ifp, data, td);
2999 if (error != ENOIOCTL) {
3005 oif_flags = ifp->if_flags;
3006 if (so->so_proto == NULL) {
3009 return (EOPNOTSUPP);
3013 * Pass the request on to the socket control method, and if the
3014 * latter returns EOPNOTSUPP, directly to the interface.
3016 * Make an exception for the legacy SIOCSIF* requests. Drivers
3017 * trust SIOCSIFADDR et al to come from an already privileged
3018 * layer, and do not perform any credentials checks or input
3021 error = ((*so->so_proto->pr_usrreqs->pru_control)(so, cmd, data,
3023 if (error == EOPNOTSUPP && ifp != NULL && ifp->if_ioctl != NULL &&
3024 cmd != SIOCSIFADDR && cmd != SIOCSIFBRDADDR &&
3025 cmd != SIOCSIFDSTADDR && cmd != SIOCSIFNETMASK)
3026 error = (*ifp->if_ioctl)(ifp, cmd, data);
3028 if ((oif_flags ^ ifp->if_flags) & IFF_UP) {
3030 if (ifp->if_flags & IFF_UP)
3040 * The code common to handling reference counted flags,
3041 * e.g., in ifpromisc() and if_allmulti().
3042 * The "pflag" argument can specify a permanent mode flag to check,
3043 * such as IFF_PPROMISC for promiscuous mode; should be 0 if none.
3045 * Only to be used on stack-owned flags, not driver-owned flags.
3048 if_setflag(struct ifnet *ifp, int flag, int pflag, int *refcount, int onswitch)
3052 int oldflags, oldcount;
3054 /* Sanity checks to catch programming errors */
3055 KASSERT((flag & (IFF_DRV_OACTIVE|IFF_DRV_RUNNING)) == 0,
3056 ("%s: setting driver-owned flag %d", __func__, flag));
3059 KASSERT(*refcount >= 0,
3060 ("%s: increment negative refcount %d for flag %d",
3061 __func__, *refcount, flag));
3063 KASSERT(*refcount > 0,
3064 ("%s: decrement non-positive refcount %d for flag %d",
3065 __func__, *refcount, flag));
3067 /* In case this mode is permanent, just touch refcount */
3068 if (ifp->if_flags & pflag) {
3069 *refcount += onswitch ? 1 : -1;
3073 /* Save ifnet parameters for if_ioctl() may fail */
3074 oldcount = *refcount;
3075 oldflags = ifp->if_flags;
3078 * See if we aren't the only and touching refcount is enough.
3079 * Actually toggle interface flag if we are the first or last.
3084 ifp->if_flags |= flag;
3088 ifp->if_flags &= ~flag;
3091 /* Call down the driver since we've changed interface flags */
3092 if (ifp->if_ioctl == NULL) {
3096 ifr.ifr_flags = ifp->if_flags & 0xffff;
3097 ifr.ifr_flagshigh = ifp->if_flags >> 16;
3098 error = (*ifp->if_ioctl)(ifp, SIOCSIFFLAGS, (caddr_t)&ifr);
3101 /* Notify userland that interface flags have changed */
3106 /* Recover after driver error */
3107 *refcount = oldcount;
3108 ifp->if_flags = oldflags;
3113 * Set/clear promiscuous mode on interface ifp based on the truth value
3114 * of pswitch. The calls are reference counted so that only the first
3115 * "on" request actually has an effect, as does the final "off" request.
3116 * Results are undefined if the "off" and "on" requests are not matched.
3119 ifpromisc(struct ifnet *ifp, int pswitch)
3122 int oldflags = ifp->if_flags;
3124 error = if_setflag(ifp, IFF_PROMISC, IFF_PPROMISC,
3125 &ifp->if_pcount, pswitch);
3126 /* If promiscuous mode status has changed, log a message */
3127 if (error == 0 && ((ifp->if_flags ^ oldflags) & IFF_PROMISC) &&
3128 log_promisc_mode_change)
3129 log(LOG_INFO, "%s: promiscuous mode %s\n",
3131 (ifp->if_flags & IFF_PROMISC) ? "enabled" : "disabled");
3136 * Return interface configuration
3137 * of system. List may be used
3138 * in later ioctl's (above) to get
3139 * other information.
3143 ifconf(u_long cmd, caddr_t data)
3145 struct ifconf *ifc = (struct ifconf *)data;
3150 int error, full = 0, valid_len, max_len;
3152 /* Limit initial buffer size to MAXPHYS to avoid DoS from userspace. */
3153 max_len = MAXPHYS - 1;
3155 /* Prevent hostile input from being able to crash the system */
3156 if (ifc->ifc_len <= 0)
3160 if (ifc->ifc_len <= max_len) {
3161 max_len = ifc->ifc_len;
3164 sb = sbuf_new(NULL, NULL, max_len + 1, SBUF_FIXEDLEN);
3169 TAILQ_FOREACH(ifp, &V_ifnet, if_link) {
3173 * Zero the ifr to make sure we don't disclose the contents
3176 memset(&ifr, 0, sizeof(ifr));
3178 if (strlcpy(ifr.ifr_name, ifp->if_xname, sizeof(ifr.ifr_name))
3179 >= sizeof(ifr.ifr_name)) {
3182 return (ENAMETOOLONG);
3187 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
3188 struct sockaddr *sa = ifa->ifa_addr;
3190 if (prison_if(curthread->td_ucred, sa) != 0)
3193 if (sa->sa_len <= sizeof(*sa)) {
3194 if (sa->sa_len < sizeof(*sa)) {
3195 memset(&ifr.ifr_ifru.ifru_addr, 0,
3196 sizeof(ifr.ifr_ifru.ifru_addr));
3197 memcpy(&ifr.ifr_ifru.ifru_addr, sa,
3200 ifr.ifr_ifru.ifru_addr = *sa;
3201 sbuf_bcat(sb, &ifr, sizeof(ifr));
3202 max_len += sizeof(ifr);
3205 offsetof(struct ifreq, ifr_addr));
3206 max_len += offsetof(struct ifreq, ifr_addr);
3207 sbuf_bcat(sb, sa, sa->sa_len);
3208 max_len += sa->sa_len;
3211 if (sbuf_error(sb) == 0)
3212 valid_len = sbuf_len(sb);
3214 IF_ADDR_RUNLOCK(ifp);
3216 sbuf_bcat(sb, &ifr, sizeof(ifr));
3217 max_len += sizeof(ifr);
3219 if (sbuf_error(sb) == 0)
3220 valid_len = sbuf_len(sb);
3226 * If we didn't allocate enough space (uncommon), try again. If
3227 * we have already allocated as much space as we are allowed,
3228 * return what we've got.
3230 if (valid_len != max_len && !full) {
3235 ifc->ifc_len = valid_len;
3237 error = copyout(sbuf_data(sb), ifc->ifc_req, ifc->ifc_len);
3243 * Just like ifpromisc(), but for all-multicast-reception mode.
3246 if_allmulti(struct ifnet *ifp, int onswitch)
3249 return (if_setflag(ifp, IFF_ALLMULTI, 0, &ifp->if_amcount, onswitch));
3252 struct ifmultiaddr *
3253 if_findmulti(struct ifnet *ifp, const struct sockaddr *sa)
3255 struct ifmultiaddr *ifma;
3257 IF_ADDR_LOCK_ASSERT(ifp);
3259 TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
3260 if (sa->sa_family == AF_LINK) {
3261 if (sa_dl_equal(ifma->ifma_addr, sa))
3264 if (sa_equal(ifma->ifma_addr, sa))
3273 * Allocate a new ifmultiaddr and initialize based on passed arguments. We
3274 * make copies of passed sockaddrs. The ifmultiaddr will not be added to
3275 * the ifnet multicast address list here, so the caller must do that and
3276 * other setup work (such as notifying the device driver). The reference
3277 * count is initialized to 1.
3279 static struct ifmultiaddr *
3280 if_allocmulti(struct ifnet *ifp, struct sockaddr *sa, struct sockaddr *llsa,
3283 struct ifmultiaddr *ifma;
3284 struct sockaddr *dupsa;
3286 ifma = malloc(sizeof *ifma, M_IFMADDR, mflags |
3291 dupsa = malloc(sa->sa_len, M_IFMADDR, mflags);
3292 if (dupsa == NULL) {
3293 free(ifma, M_IFMADDR);
3296 bcopy(sa, dupsa, sa->sa_len);
3297 ifma->ifma_addr = dupsa;
3299 ifma->ifma_ifp = ifp;
3300 ifma->ifma_refcount = 1;
3301 ifma->ifma_protospec = NULL;
3304 ifma->ifma_lladdr = NULL;
3308 dupsa = malloc(llsa->sa_len, M_IFMADDR, mflags);
3309 if (dupsa == NULL) {
3310 free(ifma->ifma_addr, M_IFMADDR);
3311 free(ifma, M_IFMADDR);
3314 bcopy(llsa, dupsa, llsa->sa_len);
3315 ifma->ifma_lladdr = dupsa;
3321 * if_freemulti: free ifmultiaddr structure and possibly attached related
3322 * addresses. The caller is responsible for implementing reference
3323 * counting, notifying the driver, handling routing messages, and releasing
3324 * any dependent link layer state.
3327 if_freemulti(struct ifmultiaddr *ifma)
3330 KASSERT(ifma->ifma_refcount == 0, ("if_freemulti: refcount %d",
3331 ifma->ifma_refcount));
3333 if (ifma->ifma_lladdr != NULL)
3334 free(ifma->ifma_lladdr, M_IFMADDR);
3335 free(ifma->ifma_addr, M_IFMADDR);
3336 free(ifma, M_IFMADDR);
3340 * Register an additional multicast address with a network interface.
3342 * - If the address is already present, bump the reference count on the
3343 * address and return.
3344 * - If the address is not link-layer, look up a link layer address.
3345 * - Allocate address structures for one or both addresses, and attach to the
3346 * multicast address list on the interface. If automatically adding a link
3347 * layer address, the protocol address will own a reference to the link
3348 * layer address, to be freed when it is freed.
3349 * - Notify the network device driver of an addition to the multicast address
3352 * 'sa' points to caller-owned memory with the desired multicast address.
3354 * 'retifma' will be used to return a pointer to the resulting multicast
3355 * address reference, if desired.
3358 if_addmulti(struct ifnet *ifp, struct sockaddr *sa,
3359 struct ifmultiaddr **retifma)
3361 struct ifmultiaddr *ifma, *ll_ifma;
3362 struct sockaddr *llsa;
3363 struct sockaddr_dl sdl;
3367 * If the address is already present, return a new reference to it;
3368 * otherwise, allocate storage and set up a new address.
3371 ifma = if_findmulti(ifp, sa);
3373 ifma->ifma_refcount++;
3374 if (retifma != NULL)
3376 IF_ADDR_WUNLOCK(ifp);
3381 * The address isn't already present; resolve the protocol address
3382 * into a link layer address, and then look that up, bump its
3383 * refcount or allocate an ifma for that also.
3384 * Most link layer resolving functions returns address data which
3385 * fits inside default sockaddr_dl structure. However callback
3386 * can allocate another sockaddr structure, in that case we need to
3391 if (ifp->if_resolvemulti != NULL) {
3392 /* Provide called function with buffer size information */
3393 sdl.sdl_len = sizeof(sdl);
3394 llsa = (struct sockaddr *)&sdl;
3395 error = ifp->if_resolvemulti(ifp, &llsa, sa);
3401 * Allocate the new address. Don't hook it up yet, as we may also
3402 * need to allocate a link layer multicast address.
3404 ifma = if_allocmulti(ifp, sa, llsa, M_NOWAIT);
3411 * If a link layer address is found, we'll need to see if it's
3412 * already present in the address list, or allocate is as well.
3413 * When this block finishes, the link layer address will be on the
3417 ll_ifma = if_findmulti(ifp, llsa);
3418 if (ll_ifma == NULL) {
3419 ll_ifma = if_allocmulti(ifp, llsa, NULL, M_NOWAIT);
3420 if (ll_ifma == NULL) {
3421 --ifma->ifma_refcount;
3426 TAILQ_INSERT_HEAD(&ifp->if_multiaddrs, ll_ifma,
3429 ll_ifma->ifma_refcount++;
3430 ifma->ifma_llifma = ll_ifma;
3434 * We now have a new multicast address, ifma, and possibly a new or
3435 * referenced link layer address. Add the primary address to the
3436 * ifnet address list.
3438 TAILQ_INSERT_HEAD(&ifp->if_multiaddrs, ifma, ifma_link);
3440 if (retifma != NULL)
3444 * Must generate the message while holding the lock so that 'ifma'
3445 * pointer is still valid.
3447 rt_newmaddrmsg(RTM_NEWMADDR, ifma);
3448 IF_ADDR_WUNLOCK(ifp);
3451 * We are certain we have added something, so call down to the
3452 * interface to let them know about it.
3454 if (ifp->if_ioctl != NULL) {
3455 (void) (*ifp->if_ioctl)(ifp, SIOCADDMULTI, 0);
3458 if ((llsa != NULL) && (llsa != (struct sockaddr *)&sdl))
3459 link_free_sdl(llsa);
3464 if ((llsa != NULL) && (llsa != (struct sockaddr *)&sdl))
3465 link_free_sdl(llsa);
3468 IF_ADDR_WUNLOCK(ifp);
3473 * Delete a multicast group membership by network-layer group address.
3475 * Returns ENOENT if the entry could not be found. If ifp no longer
3476 * exists, results are undefined. This entry point should only be used
3477 * from subsystems which do appropriate locking to hold ifp for the
3478 * duration of the call.
3479 * Network-layer protocol domains must use if_delmulti_ifma().
3482 if_delmulti(struct ifnet *ifp, struct sockaddr *sa)
3484 struct ifmultiaddr *ifma;
3489 IFNET_RLOCK_NOSLEEP();
3490 TAILQ_FOREACH(oifp, &V_ifnet, if_link)
3495 IFNET_RUNLOCK_NOSLEEP();
3497 KASSERT(ifp != NULL, ("%s: ifnet went away", __func__));
3504 ifma = if_findmulti(ifp, sa);
3506 lastref = if_delmulti_locked(ifp, ifma, 0);
3507 IF_ADDR_WUNLOCK(ifp);
3512 if (lastref && ifp->if_ioctl != NULL) {
3513 (void)(*ifp->if_ioctl)(ifp, SIOCDELMULTI, 0);
3520 * Delete all multicast group membership for an interface.
3521 * Should be used to quickly flush all multicast filters.
3524 if_delallmulti(struct ifnet *ifp)
3526 struct ifmultiaddr *ifma;
3527 struct ifmultiaddr *next;
3530 TAILQ_FOREACH_SAFE(ifma, &ifp->if_multiaddrs, ifma_link, next)
3531 if_delmulti_locked(ifp, ifma, 0);
3532 IF_ADDR_WUNLOCK(ifp);
3536 * Delete a multicast group membership by group membership pointer.
3537 * Network-layer protocol domains must use this routine.
3539 * It is safe to call this routine if the ifp disappeared.
3542 if_delmulti_ifma(struct ifmultiaddr *ifma)
3547 ifp = ifma->ifma_ifp;
3550 printf("%s: ifma_ifp seems to be detached\n", __func__);
3554 IFNET_RLOCK_NOSLEEP();
3555 TAILQ_FOREACH(oifp, &V_ifnet, if_link)
3559 printf("%s: ifnet %p disappeared\n", __func__, ifp);
3562 IFNET_RUNLOCK_NOSLEEP();
3566 * If and only if the ifnet instance exists: Acquire the address lock.
3571 lastref = if_delmulti_locked(ifp, ifma, 0);
3575 * If and only if the ifnet instance exists:
3576 * Release the address lock.
3577 * If the group was left: update the hardware hash filter.
3579 IF_ADDR_WUNLOCK(ifp);
3580 if (lastref && ifp->if_ioctl != NULL) {
3581 (void)(*ifp->if_ioctl)(ifp, SIOCDELMULTI, 0);
3587 * Perform deletion of network-layer and/or link-layer multicast address.
3589 * Return 0 if the reference count was decremented.
3590 * Return 1 if the final reference was released, indicating that the
3591 * hardware hash filter should be reprogrammed.
3594 if_delmulti_locked(struct ifnet *ifp, struct ifmultiaddr *ifma, int detaching)
3596 struct ifmultiaddr *ll_ifma;
3598 if (ifp != NULL && ifma->ifma_ifp != NULL) {
3599 KASSERT(ifma->ifma_ifp == ifp,
3600 ("%s: inconsistent ifp %p", __func__, ifp));
3601 IF_ADDR_WLOCK_ASSERT(ifp);
3604 ifp = ifma->ifma_ifp;
3607 * If the ifnet is detaching, null out references to ifnet,
3608 * so that upper protocol layers will notice, and not attempt
3609 * to obtain locks for an ifnet which no longer exists. The
3610 * routing socket announcement must happen before the ifnet
3611 * instance is detached from the system.
3615 printf("%s: detaching ifnet instance %p\n", __func__, ifp);
3618 * ifp may already be nulled out if we are being reentered
3619 * to delete the ll_ifma.
3622 rt_newmaddrmsg(RTM_DELMADDR, ifma);
3623 ifma->ifma_ifp = NULL;
3627 if (--ifma->ifma_refcount > 0)
3631 * If this ifma is a network-layer ifma, a link-layer ifma may
3632 * have been associated with it. Release it first if so.
3634 ll_ifma = ifma->ifma_llifma;
3635 if (ll_ifma != NULL) {
3636 KASSERT(ifma->ifma_lladdr != NULL,
3637 ("%s: llifma w/o lladdr", __func__));
3639 ll_ifma->ifma_ifp = NULL; /* XXX */
3640 if (--ll_ifma->ifma_refcount == 0) {
3642 TAILQ_REMOVE(&ifp->if_multiaddrs, ll_ifma,
3645 if_freemulti(ll_ifma);
3650 TAILQ_REMOVE(&ifp->if_multiaddrs, ifma, ifma_link);
3655 * The last reference to this instance of struct ifmultiaddr
3656 * was released; the hardware should be notified of this change.
3662 * Set the link layer address on an interface.
3664 * At this time we only support certain types of interfaces,
3665 * and we don't allow the length of the address to change.
3667 * Set noinline to be dtrace-friendly
3670 if_setlladdr(struct ifnet *ifp, const u_char *lladdr, int len)
3672 struct sockaddr_dl *sdl;
3679 IF_ADDR_RUNLOCK(ifp);
3683 IF_ADDR_RUNLOCK(ifp);
3684 sdl = (struct sockaddr_dl *)ifa->ifa_addr;
3689 if (len != sdl->sdl_alen) { /* don't allow length to change */
3693 switch (ifp->if_type) {
3698 case IFT_IEEE8023ADLAG:
3699 bcopy(lladdr, LLADDR(sdl), len);
3708 * If the interface is already up, we need
3709 * to re-init it in order to reprogram its
3712 if ((ifp->if_flags & IFF_UP) != 0) {
3713 if (ifp->if_ioctl) {
3714 ifp->if_flags &= ~IFF_UP;
3715 ifr.ifr_flags = ifp->if_flags & 0xffff;
3716 ifr.ifr_flagshigh = ifp->if_flags >> 16;
3717 (*ifp->if_ioctl)(ifp, SIOCSIFFLAGS, (caddr_t)&ifr);
3718 ifp->if_flags |= IFF_UP;
3719 ifr.ifr_flags = ifp->if_flags & 0xffff;
3720 ifr.ifr_flagshigh = ifp->if_flags >> 16;
3721 (*ifp->if_ioctl)(ifp, SIOCSIFFLAGS, (caddr_t)&ifr);
3724 EVENTHANDLER_INVOKE(iflladdr_event, ifp);
3729 * Compat function for handling basic encapsulation requests.
3730 * Not converted stacks (FDDI, IB, ..) supports traditional
3731 * output model: ARP (and other similar L2 protocols) are handled
3732 * inside output routine, arpresolve/nd6_resolve() returns MAC
3733 * address instead of full prepend.
3735 * This function creates calculated header==MAC for IPv4/IPv6 and
3736 * returns EAFNOSUPPORT (which is then handled in ARP code) for other
3740 if_requestencap_default(struct ifnet *ifp, struct if_encap_req *req)
3743 if (req->rtype != IFENCAP_LL)
3744 return (EOPNOTSUPP);
3746 if (req->bufsize < req->lladdr_len)
3749 switch (req->family) {
3754 return (EAFNOSUPPORT);
3757 /* Copy lladdr to storage as is */
3758 memmove(req->buf, req->lladdr, req->lladdr_len);
3759 req->bufsize = req->lladdr_len;
3760 req->lladdr_off = 0;
3766 * Get the link layer address that was read from the hardware at attach.
3768 * This is only set by Ethernet NICs (IFT_ETHER), but laggX interfaces re-type
3769 * their component interfaces as IFT_IEEE8023ADLAG.
3772 if_gethwaddr(struct ifnet *ifp, struct ifreq *ifr)
3775 if (ifp->if_hw_addr == NULL)
3778 switch (ifp->if_type) {
3780 case IFT_IEEE8023ADLAG:
3781 bcopy(ifp->if_hw_addr, ifr->ifr_addr.sa_data, ifp->if_addrlen);
3789 * The name argument must be a pointer to storage which will last as
3790 * long as the interface does. For physical devices, the result of
3791 * device_get_name(dev) is a good choice and for pseudo-devices a
3792 * static string works well.
3795 if_initname(struct ifnet *ifp, const char *name, int unit)
3797 ifp->if_dname = name;
3798 ifp->if_dunit = unit;
3799 if (unit != IF_DUNIT_NONE)
3800 snprintf(ifp->if_xname, IFNAMSIZ, "%s%d", name, unit);
3802 strlcpy(ifp->if_xname, name, IFNAMSIZ);
3806 if_printf(struct ifnet *ifp, const char * fmt, ...)
3811 retval = printf("%s: ", ifp->if_xname);
3813 retval += vprintf(fmt, ap);
3819 if_start(struct ifnet *ifp)
3822 (*(ifp)->if_start)(ifp);
3826 * Backwards compatibility interface for drivers
3827 * that have not implemented it
3830 if_transmit(struct ifnet *ifp, struct mbuf *m)
3834 IFQ_HANDOFF(ifp, m, error);
3839 if_input_default(struct ifnet *ifp __unused, struct mbuf *m)
3846 if_handoff(struct ifqueue *ifq, struct mbuf *m, struct ifnet *ifp, int adjust)
3851 if (_IF_QFULL(ifq)) {
3853 if_inc_counter(ifp, IFCOUNTER_OQDROPS, 1);
3858 if_inc_counter(ifp, IFCOUNTER_OBYTES, m->m_pkthdr.len + adjust);
3859 if (m->m_flags & (M_BCAST|M_MCAST))
3860 if_inc_counter(ifp, IFCOUNTER_OMCASTS, 1);
3861 active = ifp->if_drv_flags & IFF_DRV_OACTIVE;
3863 _IF_ENQUEUE(ifq, m);
3865 if (ifp != NULL && !active)
3866 (*(ifp)->if_start)(ifp);
3871 if_register_com_alloc(u_char type,
3872 if_com_alloc_t *a, if_com_free_t *f)
3875 KASSERT(if_com_alloc[type] == NULL,
3876 ("if_register_com_alloc: %d already registered", type));
3877 KASSERT(if_com_free[type] == NULL,
3878 ("if_register_com_alloc: %d free already registered", type));
3880 if_com_alloc[type] = a;
3881 if_com_free[type] = f;
3885 if_deregister_com_alloc(u_char type)
3888 KASSERT(if_com_alloc[type] != NULL,
3889 ("if_deregister_com_alloc: %d not registered", type));
3890 KASSERT(if_com_free[type] != NULL,
3891 ("if_deregister_com_alloc: %d free not registered", type));
3892 if_com_alloc[type] = NULL;
3893 if_com_free[type] = NULL;
3896 /* API for driver access to network stack owned ifnet.*/
3898 if_setbaudrate(struct ifnet *ifp, uint64_t baudrate)
3902 oldbrate = ifp->if_baudrate;
3903 ifp->if_baudrate = baudrate;
3908 if_getbaudrate(if_t ifp)
3911 return (((struct ifnet *)ifp)->if_baudrate);
3915 if_setcapabilities(if_t ifp, int capabilities)
3917 ((struct ifnet *)ifp)->if_capabilities = capabilities;
3922 if_setcapabilitiesbit(if_t ifp, int setbit, int clearbit)
3924 ((struct ifnet *)ifp)->if_capabilities |= setbit;
3925 ((struct ifnet *)ifp)->if_capabilities &= ~clearbit;
3931 if_getcapabilities(if_t ifp)
3933 return ((struct ifnet *)ifp)->if_capabilities;
3937 if_setcapenable(if_t ifp, int capabilities)
3939 ((struct ifnet *)ifp)->if_capenable = capabilities;
3944 if_setcapenablebit(if_t ifp, int setcap, int clearcap)
3947 ((struct ifnet *)ifp)->if_capenable |= setcap;
3949 ((struct ifnet *)ifp)->if_capenable &= ~clearcap;
3955 if_getdname(if_t ifp)
3957 return ((struct ifnet *)ifp)->if_dname;
3961 if_togglecapenable(if_t ifp, int togglecap)
3963 ((struct ifnet *)ifp)->if_capenable ^= togglecap;
3968 if_getcapenable(if_t ifp)
3970 return ((struct ifnet *)ifp)->if_capenable;
3974 * This is largely undesirable because it ties ifnet to a device, but does
3975 * provide flexiblity for an embedded product vendor. Should be used with
3976 * the understanding that it violates the interface boundaries, and should be
3977 * a last resort only.
3980 if_setdev(if_t ifp, void *dev)
3986 if_setdrvflagbits(if_t ifp, int set_flags, int clear_flags)
3988 ((struct ifnet *)ifp)->if_drv_flags |= set_flags;
3989 ((struct ifnet *)ifp)->if_drv_flags &= ~clear_flags;
3995 if_getdrvflags(if_t ifp)
3997 return ((struct ifnet *)ifp)->if_drv_flags;
4001 if_setdrvflags(if_t ifp, int flags)
4003 ((struct ifnet *)ifp)->if_drv_flags = flags;
4009 if_setflags(if_t ifp, int flags)
4011 ((struct ifnet *)ifp)->if_flags = flags;
4016 if_setflagbits(if_t ifp, int set, int clear)
4018 ((struct ifnet *)ifp)->if_flags |= set;
4019 ((struct ifnet *)ifp)->if_flags &= ~clear;
4025 if_getflags(if_t ifp)
4027 return ((struct ifnet *)ifp)->if_flags;
4031 if_clearhwassist(if_t ifp)
4033 ((struct ifnet *)ifp)->if_hwassist = 0;
4038 if_sethwassistbits(if_t ifp, int toset, int toclear)
4040 ((struct ifnet *)ifp)->if_hwassist |= toset;
4041 ((struct ifnet *)ifp)->if_hwassist &= ~toclear;
4047 if_sethwassist(if_t ifp, int hwassist_bit)
4049 ((struct ifnet *)ifp)->if_hwassist = hwassist_bit;
4054 if_gethwassist(if_t ifp)
4056 return ((struct ifnet *)ifp)->if_hwassist;
4060 if_setmtu(if_t ifp, int mtu)
4062 ((struct ifnet *)ifp)->if_mtu = mtu;
4069 return ((struct ifnet *)ifp)->if_mtu;
4073 if_getmtu_family(if_t ifp, int family)
4077 for (dp = domains; dp; dp = dp->dom_next) {
4078 if (dp->dom_family == family && dp->dom_ifmtu != NULL)
4079 return (dp->dom_ifmtu((struct ifnet *)ifp));
4082 return (((struct ifnet *)ifp)->if_mtu);
4086 if_setsoftc(if_t ifp, void *softc)
4088 ((struct ifnet *)ifp)->if_softc = softc;
4093 if_getsoftc(if_t ifp)
4095 return ((struct ifnet *)ifp)->if_softc;
4099 if_setrcvif(struct mbuf *m, if_t ifp)
4101 m->m_pkthdr.rcvif = (struct ifnet *)ifp;
4105 if_setvtag(struct mbuf *m, uint16_t tag)
4107 m->m_pkthdr.ether_vtag = tag;
4111 if_getvtag(struct mbuf *m)
4114 return (m->m_pkthdr.ether_vtag);
4118 if_sendq_empty(if_t ifp)
4120 return IFQ_DRV_IS_EMPTY(&((struct ifnet *)ifp)->if_snd);
4124 if_getifaddr(if_t ifp)
4126 return ((struct ifnet *)ifp)->if_addr;
4130 if_getamcount(if_t ifp)
4132 return ((struct ifnet *)ifp)->if_amcount;
4137 if_setsendqready(if_t ifp)
4139 IFQ_SET_READY(&((struct ifnet *)ifp)->if_snd);
4144 if_setsendqlen(if_t ifp, int tx_desc_count)
4146 IFQ_SET_MAXLEN(&((struct ifnet *)ifp)->if_snd, tx_desc_count);
4147 ((struct ifnet *)ifp)->if_snd.ifq_drv_maxlen = tx_desc_count;
4153 if_vlantrunkinuse(if_t ifp)
4155 return ((struct ifnet *)ifp)->if_vlantrunk != NULL?1:0;
4159 if_input(if_t ifp, struct mbuf* sendmp)
4161 (*((struct ifnet *)ifp)->if_input)((struct ifnet *)ifp, sendmp);
4167 #ifndef ETH_ADDR_LEN
4168 #define ETH_ADDR_LEN 6
4172 if_setupmultiaddr(if_t ifp, void *mta, int *cnt, int max)
4174 struct ifmultiaddr *ifma;
4175 uint8_t *lmta = (uint8_t *)mta;
4178 TAILQ_FOREACH(ifma, &((struct ifnet *)ifp)->if_multiaddrs, ifma_link) {
4179 if (ifma->ifma_addr->sa_family != AF_LINK)
4185 bcopy(LLADDR((struct sockaddr_dl *)ifma->ifma_addr),
4186 &lmta[mcnt * ETH_ADDR_LEN], ETH_ADDR_LEN);
4195 if_multiaddr_array(if_t ifp, void *mta, int *cnt, int max)
4199 if_maddr_rlock(ifp);
4200 error = if_setupmultiaddr(ifp, mta, cnt, max);
4201 if_maddr_runlock(ifp);
4206 if_multiaddr_count(if_t ifp, int max)
4208 struct ifmultiaddr *ifma;
4212 if_maddr_rlock(ifp);
4213 TAILQ_FOREACH(ifma, &((struct ifnet *)ifp)->if_multiaddrs, ifma_link) {
4214 if (ifma->ifma_addr->sa_family != AF_LINK)
4220 if_maddr_runlock(ifp);
4225 if_multi_apply(struct ifnet *ifp, int (*filter)(void *, struct ifmultiaddr *, int), void *arg)
4227 struct ifmultiaddr *ifma;
4230 if_maddr_rlock(ifp);
4231 TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link)
4232 cnt += filter(arg, ifma, cnt);
4233 if_maddr_runlock(ifp);
4238 if_dequeue(if_t ifp)
4241 IFQ_DRV_DEQUEUE(&((struct ifnet *)ifp)->if_snd, m);
4247 if_sendq_prepend(if_t ifp, struct mbuf *m)
4249 IFQ_DRV_PREPEND(&((struct ifnet *)ifp)->if_snd, m);
4254 if_setifheaderlen(if_t ifp, int len)
4256 ((struct ifnet *)ifp)->if_hdrlen = len;
4261 if_getlladdr(if_t ifp)
4263 return (IF_LLADDR((struct ifnet *)ifp));
4267 if_gethandle(u_char type)
4269 return (if_alloc(type));
4273 if_bpfmtap(if_t ifh, struct mbuf *m)
4275 struct ifnet *ifp = (struct ifnet *)ifh;
4281 if_etherbpfmtap(if_t ifh, struct mbuf *m)
4283 struct ifnet *ifp = (struct ifnet *)ifh;
4285 ETHER_BPF_MTAP(ifp, m);
4289 if_vlancap(if_t ifh)
4291 struct ifnet *ifp = (struct ifnet *)ifh;
4292 VLAN_CAPABILITIES(ifp);
4296 if_sethwtsomax(if_t ifp, u_int if_hw_tsomax)
4299 ((struct ifnet *)ifp)->if_hw_tsomax = if_hw_tsomax;
4304 if_sethwtsomaxsegcount(if_t ifp, u_int if_hw_tsomaxsegcount)
4307 ((struct ifnet *)ifp)->if_hw_tsomaxsegcount = if_hw_tsomaxsegcount;
4312 if_sethwtsomaxsegsize(if_t ifp, u_int if_hw_tsomaxsegsize)
4315 ((struct ifnet *)ifp)->if_hw_tsomaxsegsize = if_hw_tsomaxsegsize;
4320 if_gethwtsomax(if_t ifp)
4323 return (((struct ifnet *)ifp)->if_hw_tsomax);
4327 if_gethwtsomaxsegcount(if_t ifp)
4330 return (((struct ifnet *)ifp)->if_hw_tsomaxsegcount);
4334 if_gethwtsomaxsegsize(if_t ifp)
4337 return (((struct ifnet *)ifp)->if_hw_tsomaxsegsize);
4341 if_setinitfn(if_t ifp, void (*init_fn)(void *))
4343 ((struct ifnet *)ifp)->if_init = init_fn;
4347 if_setioctlfn(if_t ifp, int (*ioctl_fn)(if_t, u_long, caddr_t))
4349 ((struct ifnet *)ifp)->if_ioctl = (void *)ioctl_fn;
4353 if_setstartfn(if_t ifp, void (*start_fn)(if_t))
4355 ((struct ifnet *)ifp)->if_start = (void *)start_fn;
4359 if_settransmitfn(if_t ifp, if_transmit_fn_t start_fn)
4361 ((struct ifnet *)ifp)->if_transmit = start_fn;
4364 void if_setqflushfn(if_t ifp, if_qflush_fn_t flush_fn)
4366 ((struct ifnet *)ifp)->if_qflush = flush_fn;
4371 if_setgetcounterfn(if_t ifp, if_get_counter_t fn)
4374 ifp->if_get_counter = fn;
4377 /* Revisit these - These are inline functions originally. */
4379 drbr_inuse_drv(if_t ifh, struct buf_ring *br)
4381 return drbr_inuse(ifh, br);
4385 drbr_dequeue_drv(if_t ifh, struct buf_ring *br)
4387 return drbr_dequeue(ifh, br);
4391 drbr_needs_enqueue_drv(if_t ifh, struct buf_ring *br)
4393 return drbr_needs_enqueue(ifh, br);
4397 drbr_enqueue_drv(if_t ifh, struct buf_ring *br, struct mbuf *m)
4399 return drbr_enqueue(ifh, br, m);