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 *);
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 ifhwioctl(u_long, struct ifnet *, caddr_t, struct thread *);
258 static int if_delmulti_locked(struct ifnet *, struct ifmultiaddr *, int);
259 static void do_link_state_change(void *, int);
260 static int if_getgroup(struct ifgroupreq *, struct ifnet *);
261 static int if_getgroupmembers(struct ifgroupreq *);
262 static void if_delgroups(struct ifnet *);
263 static void if_attach_internal(struct ifnet *, int, struct if_clone *);
264 static int if_detach_internal(struct ifnet *, int, struct if_clone **);
266 static void if_vmove(struct ifnet *, struct vnet *);
271 * XXX: declare here to avoid to include many inet6 related files..
272 * should be more generalized?
274 extern void nd6_setmtu(struct ifnet *);
277 /* ipsec helper hooks */
278 VNET_DEFINE(struct hhook_head *, ipsec_hhh_in[HHOOK_IPSEC_COUNT]);
279 VNET_DEFINE(struct hhook_head *, ipsec_hhh_out[HHOOK_IPSEC_COUNT]);
281 VNET_DEFINE(int, if_index);
282 int ifqmaxlen = IFQ_MAXLEN;
283 VNET_DEFINE(struct ifnethead, ifnet); /* depend on static init XXX */
284 VNET_DEFINE(struct ifgrouphead, ifg_head);
286 static VNET_DEFINE(int, if_indexlim) = 8;
288 /* Table of ifnet by index. */
289 VNET_DEFINE(struct ifnet **, ifindex_table);
291 #define V_if_indexlim VNET(if_indexlim)
292 #define V_ifindex_table VNET(ifindex_table)
295 * The global network interface list (V_ifnet) and related state (such as
296 * if_index, if_indexlim, and ifindex_table) are protected by an sxlock and
297 * an rwlock. Either may be acquired shared to stablize the list, but both
298 * must be acquired writable to modify the list. This model allows us to
299 * both stablize the interface list during interrupt thread processing, but
300 * also to stablize it over long-running ioctls, without introducing priority
301 * inversions and deadlocks.
303 struct rwlock ifnet_rwlock;
304 RW_SYSINIT_FLAGS(ifnet_rw, &ifnet_rwlock, "ifnet_rw", RW_RECURSE);
305 struct sx ifnet_sxlock;
306 SX_SYSINIT_FLAGS(ifnet_sx, &ifnet_sxlock, "ifnet_sx", SX_RECURSE);
309 * The allocation of network interfaces is a rather non-atomic affair; we
310 * need to select an index before we are ready to expose the interface for
311 * use, so will use this pointer value to indicate reservation.
313 #define IFNET_HOLD (void *)(uintptr_t)(-1)
315 static if_com_alloc_t *if_com_alloc[256];
316 static if_com_free_t *if_com_free[256];
318 static MALLOC_DEFINE(M_IFNET, "ifnet", "interface internals");
319 MALLOC_DEFINE(M_IFADDR, "ifaddr", "interface address");
320 MALLOC_DEFINE(M_IFMADDR, "ether_multi", "link-level multicast address");
323 ifnet_byindex_locked(u_short idx)
326 if (idx > V_if_index)
328 if (V_ifindex_table[idx] == IFNET_HOLD)
330 return (V_ifindex_table[idx]);
334 ifnet_byindex(u_short idx)
338 IFNET_RLOCK_NOSLEEP();
339 ifp = ifnet_byindex_locked(idx);
340 IFNET_RUNLOCK_NOSLEEP();
345 ifnet_byindex_ref(u_short idx)
349 IFNET_RLOCK_NOSLEEP();
350 ifp = ifnet_byindex_locked(idx);
351 if (ifp == NULL || (ifp->if_flags & IFF_DYING)) {
352 IFNET_RUNLOCK_NOSLEEP();
356 IFNET_RUNLOCK_NOSLEEP();
361 * Allocate an ifindex array entry; return 0 on success or an error on
369 IFNET_WLOCK_ASSERT();
372 * Try to find an empty slot below V_if_index. If we fail, take the
375 for (idx = 1; idx <= V_if_index; idx++) {
376 if (V_ifindex_table[idx] == NULL)
380 /* Catch if_index overflow. */
381 if (idx >= V_if_indexlim) {
385 if (idx > V_if_index)
391 ifindex_free_locked(u_short idx)
394 IFNET_WLOCK_ASSERT();
396 V_ifindex_table[idx] = NULL;
397 while (V_if_index > 0 &&
398 V_ifindex_table[V_if_index] == NULL)
403 ifindex_free(u_short idx)
407 ifindex_free_locked(idx);
412 ifnet_setbyindex_locked(u_short idx, struct ifnet *ifp)
415 IFNET_WLOCK_ASSERT();
417 V_ifindex_table[idx] = ifp;
421 ifnet_setbyindex(u_short idx, struct ifnet *ifp)
425 ifnet_setbyindex_locked(idx, ifp);
430 ifaddr_byindex(u_short idx)
433 struct ifaddr *ifa = NULL;
435 IFNET_RLOCK_NOSLEEP();
436 ifp = ifnet_byindex_locked(idx);
437 if (ifp != NULL && (ifa = ifp->if_addr) != NULL)
439 IFNET_RUNLOCK_NOSLEEP();
444 * Network interface utility routines.
446 * Routines with ifa_ifwith* names take sockaddr *'s as
451 vnet_if_init(const void *unused __unused)
454 TAILQ_INIT(&V_ifnet);
455 TAILQ_INIT(&V_ifg_head);
457 if_grow(); /* create initial table */
459 vnet_if_clone_init();
461 VNET_SYSINIT(vnet_if_init, SI_SUB_INIT_IF, SI_ORDER_SECOND, vnet_if_init,
466 vnet_if_uninit(const void *unused __unused)
469 VNET_ASSERT(TAILQ_EMPTY(&V_ifnet), ("%s:%d tailq &V_ifnet=%p "
470 "not empty", __func__, __LINE__, &V_ifnet));
471 VNET_ASSERT(TAILQ_EMPTY(&V_ifg_head), ("%s:%d tailq &V_ifg_head=%p "
472 "not empty", __func__, __LINE__, &V_ifg_head));
474 free((caddr_t)V_ifindex_table, M_IFNET);
476 VNET_SYSUNINIT(vnet_if_uninit, SI_SUB_INIT_IF, SI_ORDER_FIRST,
477 vnet_if_uninit, NULL);
480 vnet_if_return(const void *unused __unused)
482 struct ifnet *ifp, *nifp;
484 /* Return all inherited interfaces to their parent vnets. */
485 TAILQ_FOREACH_SAFE(ifp, &V_ifnet, if_link, nifp) {
486 if (ifp->if_home_vnet != ifp->if_vnet)
487 if_vmove(ifp, ifp->if_home_vnet);
490 VNET_SYSUNINIT(vnet_if_return, SI_SUB_VNET_DONE, SI_ORDER_ANY,
491 vnet_if_return, NULL);
501 IFNET_WLOCK_ASSERT();
502 oldlim = V_if_indexlim;
504 n = (oldlim << 1) * sizeof(*e);
505 e = malloc(n, M_IFNET, M_WAITOK | M_ZERO);
507 if (V_if_indexlim != oldlim) {
511 if (V_ifindex_table != NULL) {
512 memcpy((caddr_t)e, (caddr_t)V_ifindex_table, n/2);
513 free((caddr_t)V_ifindex_table, M_IFNET);
520 * Allocate a struct ifnet and an index for an interface. A layer 2
521 * common structure will also be allocated if an allocation routine is
522 * registered for the passed type.
525 if_alloc(u_char type)
530 ifp = malloc(sizeof(struct ifnet), M_IFNET, M_WAITOK|M_ZERO);
532 idx = ifindex_alloc();
533 ifnet_setbyindex_locked(idx, IFNET_HOLD);
537 ifp->if_alloctype = type;
539 ifp->if_vnet = curvnet;
541 if (if_com_alloc[type] != NULL) {
542 ifp->if_l2com = if_com_alloc[type](type, ifp);
543 if (ifp->if_l2com == NULL) {
550 IF_ADDR_LOCK_INIT(ifp);
551 TASK_INIT(&ifp->if_linktask, 0, do_link_state_change, ifp);
552 ifp->if_afdata_initialized = 0;
553 IF_AFDATA_LOCK_INIT(ifp);
554 TAILQ_INIT(&ifp->if_addrhead);
555 TAILQ_INIT(&ifp->if_multiaddrs);
556 TAILQ_INIT(&ifp->if_groups);
560 ifq_init(&ifp->if_snd, ifp);
562 refcount_init(&ifp->if_refcount, 1); /* Index reference. */
563 for (int i = 0; i < IFCOUNTERS; i++)
564 ifp->if_counters[i] = counter_u64_alloc(M_WAITOK);
565 ifp->if_get_counter = if_get_counter_default;
566 ifp->if_pcp = IFNET_PCP_NONE;
567 ifnet_setbyindex(ifp->if_index, ifp);
572 * Do the actual work of freeing a struct ifnet, and layer 2 common
573 * structure. This call is made when the last reference to an
574 * interface is released.
577 if_free_internal(struct ifnet *ifp)
580 KASSERT((ifp->if_flags & IFF_DYING),
581 ("if_free_internal: interface not dying"));
583 if (if_com_free[ifp->if_alloctype] != NULL)
584 if_com_free[ifp->if_alloctype](ifp->if_l2com,
588 mac_ifnet_destroy(ifp);
590 if (ifp->if_description != NULL)
591 free(ifp->if_description, M_IFDESCR);
592 IF_AFDATA_DESTROY(ifp);
593 IF_ADDR_LOCK_DESTROY(ifp);
594 ifq_delete(&ifp->if_snd);
596 for (int i = 0; i < IFCOUNTERS; i++)
597 counter_u64_free(ifp->if_counters[i]);
603 * Deregister an interface and free the associated storage.
606 if_free(struct ifnet *ifp)
609 ifp->if_flags |= IFF_DYING; /* XXX: Locking */
611 CURVNET_SET_QUIET(ifp->if_vnet);
613 KASSERT(ifp == ifnet_byindex_locked(ifp->if_index),
614 ("%s: freeing unallocated ifnet", ifp->if_xname));
616 ifindex_free_locked(ifp->if_index);
619 if (refcount_release(&ifp->if_refcount))
620 if_free_internal(ifp);
625 * Interfaces to keep an ifnet type-stable despite the possibility of the
626 * driver calling if_free(). If there are additional references, we defer
627 * freeing the underlying data structure.
630 if_ref(struct ifnet *ifp)
633 /* We don't assert the ifnet list lock here, but arguably should. */
634 refcount_acquire(&ifp->if_refcount);
638 if_rele(struct ifnet *ifp)
641 if (!refcount_release(&ifp->if_refcount))
643 if_free_internal(ifp);
647 ifq_init(struct ifaltq *ifq, struct ifnet *ifp)
650 mtx_init(&ifq->ifq_mtx, ifp->if_xname, "if send queue", MTX_DEF);
652 if (ifq->ifq_maxlen == 0)
653 ifq->ifq_maxlen = ifqmaxlen;
656 ifq->altq_disc = NULL;
657 ifq->altq_flags &= ALTQF_CANTCHANGE;
658 ifq->altq_tbr = NULL;
663 ifq_delete(struct ifaltq *ifq)
665 mtx_destroy(&ifq->ifq_mtx);
669 * Perform generic interface initialization tasks and attach the interface
670 * to the list of "active" interfaces. If vmove flag is set on entry
671 * to if_attach_internal(), perform only a limited subset of initialization
672 * tasks, given that we are moving from one vnet to another an ifnet which
673 * has already been fully initialized.
675 * Note that if_detach_internal() removes group membership unconditionally
676 * even when vmove flag is set, and if_attach_internal() adds only IFG_ALL.
677 * Thus, when if_vmove() is applied to a cloned interface, group membership
678 * is lost while a cloned one always joins a group whose name is
679 * ifc->ifc_name. To recover this after if_detach_internal() and
680 * if_attach_internal(), the cloner should be specified to
681 * if_attach_internal() via ifc. If it is non-NULL, if_attach_internal()
682 * attempts to join a group whose name is ifc->ifc_name.
685 * - The decision to return void and thus require this function to
686 * succeed is questionable.
687 * - We should probably do more sanity checking. For instance we don't
688 * do anything to insure if_xname is unique or non-empty.
691 if_attach(struct ifnet *ifp)
694 if_attach_internal(ifp, 0, NULL);
698 * Compute the least common TSO limit.
701 if_hw_tsomax_common(if_t ifp, struct ifnet_hw_tsomax *pmax)
704 * 1) If there is no limit currently, take the limit from
705 * the network adapter.
707 * 2) If the network adapter has a limit below the current
710 if (pmax->tsomaxbytes == 0 || (ifp->if_hw_tsomax != 0 &&
711 ifp->if_hw_tsomax < pmax->tsomaxbytes)) {
712 pmax->tsomaxbytes = ifp->if_hw_tsomax;
714 if (pmax->tsomaxsegcount == 0 || (ifp->if_hw_tsomaxsegcount != 0 &&
715 ifp->if_hw_tsomaxsegcount < pmax->tsomaxsegcount)) {
716 pmax->tsomaxsegcount = ifp->if_hw_tsomaxsegcount;
718 if (pmax->tsomaxsegsize == 0 || (ifp->if_hw_tsomaxsegsize != 0 &&
719 ifp->if_hw_tsomaxsegsize < pmax->tsomaxsegsize)) {
720 pmax->tsomaxsegsize = ifp->if_hw_tsomaxsegsize;
725 * Update TSO limit of a network adapter.
727 * Returns zero if no change. Else non-zero.
730 if_hw_tsomax_update(if_t ifp, struct ifnet_hw_tsomax *pmax)
733 if (ifp->if_hw_tsomax != pmax->tsomaxbytes) {
734 ifp->if_hw_tsomax = pmax->tsomaxbytes;
737 if (ifp->if_hw_tsomaxsegsize != pmax->tsomaxsegsize) {
738 ifp->if_hw_tsomaxsegsize = pmax->tsomaxsegsize;
741 if (ifp->if_hw_tsomaxsegcount != pmax->tsomaxsegcount) {
742 ifp->if_hw_tsomaxsegcount = pmax->tsomaxsegcount;
749 if_attach_internal(struct ifnet *ifp, int vmove, struct if_clone *ifc)
751 unsigned socksize, ifasize;
752 int namelen, masklen;
753 struct sockaddr_dl *sdl;
756 if (ifp->if_index == 0 || ifp != ifnet_byindex(ifp->if_index))
757 panic ("%s: BUG: if_attach called without if_alloc'd input()\n",
761 ifp->if_vnet = curvnet;
762 if (ifp->if_home_vnet == NULL)
763 ifp->if_home_vnet = curvnet;
766 if_addgroup(ifp, IFG_ALL);
768 /* Restore group membership for cloned interfaces. */
769 if (vmove && ifc != NULL)
770 if_clone_addgroup(ifp, ifc);
772 getmicrotime(&ifp->if_lastchange);
773 ifp->if_epoch = time_uptime;
775 KASSERT((ifp->if_transmit == NULL && ifp->if_qflush == NULL) ||
776 (ifp->if_transmit != NULL && ifp->if_qflush != NULL),
777 ("transmit and qflush must both either be set or both be NULL"));
778 if (ifp->if_transmit == NULL) {
779 ifp->if_transmit = if_transmit;
780 ifp->if_qflush = if_qflush;
782 if (ifp->if_input == NULL)
783 ifp->if_input = if_input_default;
785 if (ifp->if_requestencap == NULL)
786 ifp->if_requestencap = if_requestencap_default;
790 mac_ifnet_create(ifp);
794 * Create a Link Level name for this device.
796 namelen = strlen(ifp->if_xname);
798 * Always save enough space for any possiable name so we
799 * can do a rename in place later.
801 masklen = offsetof(struct sockaddr_dl, sdl_data[0]) + IFNAMSIZ;
802 socksize = masklen + ifp->if_addrlen;
803 if (socksize < sizeof(*sdl))
804 socksize = sizeof(*sdl);
805 socksize = roundup2(socksize, sizeof(long));
806 ifasize = sizeof(*ifa) + 2 * socksize;
807 ifa = ifa_alloc(ifasize, M_WAITOK);
808 sdl = (struct sockaddr_dl *)(ifa + 1);
809 sdl->sdl_len = socksize;
810 sdl->sdl_family = AF_LINK;
811 bcopy(ifp->if_xname, sdl->sdl_data, namelen);
812 sdl->sdl_nlen = namelen;
813 sdl->sdl_index = ifp->if_index;
814 sdl->sdl_type = ifp->if_type;
817 ifa->ifa_rtrequest = link_rtrequest;
818 ifa->ifa_addr = (struct sockaddr *)sdl;
819 sdl = (struct sockaddr_dl *)(socksize + (caddr_t)sdl);
820 ifa->ifa_netmask = (struct sockaddr *)sdl;
821 sdl->sdl_len = masklen;
823 sdl->sdl_data[--namelen] = 0xff;
824 TAILQ_INSERT_HEAD(&ifp->if_addrhead, ifa, ifa_link);
825 /* Reliably crash if used uninitialized. */
826 ifp->if_broadcastaddr = NULL;
828 if (ifp->if_type == IFT_ETHER) {
829 ifp->if_hw_addr = malloc(ifp->if_addrlen, M_IFADDR,
833 #if defined(INET) || defined(INET6)
834 /* Use defaults for TSO, if nothing is set */
835 if (ifp->if_hw_tsomax == 0 &&
836 ifp->if_hw_tsomaxsegcount == 0 &&
837 ifp->if_hw_tsomaxsegsize == 0) {
839 * The TSO defaults needs to be such that an
840 * NFS mbuf list of 35 mbufs totalling just
841 * below 64K works and that a chain of mbufs
842 * can be defragged into at most 32 segments:
844 ifp->if_hw_tsomax = min(IP_MAXPACKET, (32 * MCLBYTES) -
845 (ETHER_HDR_LEN + ETHER_VLAN_ENCAP_LEN));
846 ifp->if_hw_tsomaxsegcount = 35;
847 ifp->if_hw_tsomaxsegsize = 2048; /* 2K */
849 /* XXX some drivers set IFCAP_TSO after ethernet attach */
850 if (ifp->if_capabilities & IFCAP_TSO) {
851 if_printf(ifp, "Using defaults for TSO: %u/%u/%u\n",
853 ifp->if_hw_tsomaxsegcount,
854 ifp->if_hw_tsomaxsegsize);
862 * Update the interface index in the link layer address
865 for (ifa = ifp->if_addr; ifa != NULL;
866 ifa = TAILQ_NEXT(ifa, ifa_link)) {
867 if (ifa->ifa_addr->sa_family == AF_LINK) {
868 sdl = (struct sockaddr_dl *)ifa->ifa_addr;
869 sdl->sdl_index = ifp->if_index;
876 TAILQ_INSERT_TAIL(&V_ifnet, ifp, if_link);
878 curvnet->vnet_ifcnt++;
882 if (domain_init_status >= 2)
883 if_attachdomain1(ifp);
885 EVENTHANDLER_INVOKE(ifnet_arrival_event, ifp);
886 if (IS_DEFAULT_VNET(curvnet))
887 devctl_notify("IFNET", ifp->if_xname, "ATTACH", NULL);
889 /* Announce the interface. */
890 rt_ifannouncemsg(ifp, IFAN_ARRIVAL);
894 if_attachdomain(void *dummy)
898 TAILQ_FOREACH(ifp, &V_ifnet, if_link)
899 if_attachdomain1(ifp);
901 SYSINIT(domainifattach, SI_SUB_PROTO_IFATTACHDOMAIN, SI_ORDER_SECOND,
902 if_attachdomain, NULL);
905 if_attachdomain1(struct ifnet *ifp)
910 * Since dp->dom_ifattach calls malloc() with M_WAITOK, we
911 * cannot lock ifp->if_afdata initialization, entirely.
914 if (ifp->if_afdata_initialized >= domain_init_status) {
915 IF_AFDATA_UNLOCK(ifp);
916 log(LOG_WARNING, "%s called more than once on %s\n",
917 __func__, ifp->if_xname);
920 ifp->if_afdata_initialized = domain_init_status;
921 IF_AFDATA_UNLOCK(ifp);
923 /* address family dependent data region */
924 bzero(ifp->if_afdata, sizeof(ifp->if_afdata));
925 for (dp = domains; dp; dp = dp->dom_next) {
926 if (dp->dom_ifattach)
927 ifp->if_afdata[dp->dom_family] =
928 (*dp->dom_ifattach)(ifp);
933 * Remove any unicast or broadcast network addresses from an interface.
936 if_purgeaddrs(struct ifnet *ifp)
938 struct ifaddr *ifa, *next;
940 /* XXX cannot hold IF_ADDR_WLOCK over called functions. */
941 TAILQ_FOREACH_SAFE(ifa, &ifp->if_addrhead, ifa_link, next) {
942 if (ifa->ifa_addr->sa_family == AF_LINK)
945 /* XXX: Ugly!! ad hoc just for INET */
946 if (ifa->ifa_addr->sa_family == AF_INET) {
947 struct ifaliasreq ifr;
949 bzero(&ifr, sizeof(ifr));
950 ifr.ifra_addr = *ifa->ifa_addr;
951 if (ifa->ifa_dstaddr)
952 ifr.ifra_broadaddr = *ifa->ifa_dstaddr;
953 if (in_control(NULL, SIOCDIFADDR, (caddr_t)&ifr, ifp,
959 if (ifa->ifa_addr->sa_family == AF_INET6) {
961 /* ifp_addrhead is already updated */
966 TAILQ_REMOVE(&ifp->if_addrhead, ifa, ifa_link);
967 IF_ADDR_WUNLOCK(ifp);
973 * Remove any multicast network addresses from an interface when an ifnet
977 if_purgemaddrs(struct ifnet *ifp)
979 struct ifmultiaddr *ifma;
980 struct ifmultiaddr *next;
983 TAILQ_FOREACH_SAFE(ifma, &ifp->if_multiaddrs, ifma_link, next)
984 if_delmulti_locked(ifp, ifma, 1);
985 IF_ADDR_WUNLOCK(ifp);
989 * Detach an interface, removing it from the list of "active" interfaces.
990 * If vmove flag is set on entry to if_detach_internal(), perform only a
991 * limited subset of cleanup tasks, given that we are moving an ifnet from
992 * one vnet to another, where it must be fully operational.
994 * XXXRW: There are some significant questions about event ordering, and
995 * how to prevent things from starting to use the interface during detach.
998 if_detach(struct ifnet *ifp)
1001 CURVNET_SET_QUIET(ifp->if_vnet);
1002 if_detach_internal(ifp, 0, NULL);
1007 * The vmove flag, if set, indicates that we are called from a callpath
1008 * that is moving an interface to a different vnet instance.
1010 * The shutdown flag, if set, indicates that we are called in the
1011 * process of shutting down a vnet instance. Currently only the
1012 * vnet_if_return SYSUNINIT function sets it. Note: we can be called
1013 * on a vnet instance shutdown without this flag being set, e.g., when
1014 * the cloned interfaces are destoyed as first thing of teardown.
1017 if_detach_internal(struct ifnet *ifp, int vmove, struct if_clone **ifcp)
1027 shutdown = (ifp->if_vnet->vnet_state > SI_SUB_VNET &&
1028 ifp->if_vnet->vnet_state < SI_SUB_VNET_DONE) ? 1 : 0;
1031 TAILQ_FOREACH(iter, &V_ifnet, if_link)
1033 TAILQ_REMOVE(&V_ifnet, ifp, if_link);
1040 * While we would want to panic here, we cannot
1041 * guarantee that the interface is indeed still on
1042 * the list given we don't hold locks all the way.
1047 panic("%s: ifp=%p not on the ifnet tailq %p",
1048 __func__, ifp, &V_ifnet);
1050 return; /* XXX this should panic as well? */
1055 * At this point we know the interface still was on the ifnet list
1056 * and we removed it so we are in a stable state.
1059 curvnet->vnet_ifcnt--;
1063 * In any case (destroy or vmove) detach us from the groups
1064 * and remove/wait for pending events on the taskq.
1065 * XXX-BZ in theory an interface could still enqueue a taskq change?
1069 taskqueue_drain(taskqueue_swi, &ifp->if_linktask);
1072 * Check if this is a cloned interface or not. Must do even if
1073 * shutting down as a if_vmove_reclaim() would move the ifp and
1074 * the if_clone_addgroup() will have a corrupted string overwise
1075 * from a gibberish pointer.
1077 if (vmove && ifcp != NULL)
1078 *ifcp = if_clone_findifc(ifp);
1084 * On VNET shutdown abort here as the stack teardown will do all
1085 * the work top-down for us.
1089 * In case of a vmove we are done here without error.
1090 * If we would signal an error it would lead to the same
1091 * abort as if we did not find the ifnet anymore.
1092 * if_detach() calls us in void context and does not care
1093 * about an early abort notification, so life is splendid :)
1095 goto finish_vnet_shutdown;
1100 * At this point we are not tearing down a VNET and are either
1101 * going to destroy or vmove the interface and have to cleanup
1106 * Remove routes and flush queues.
1109 if (ALTQ_IS_ENABLED(&ifp->if_snd))
1110 altq_disable(&ifp->if_snd);
1111 if (ALTQ_IS_ATTACHED(&ifp->if_snd))
1112 altq_detach(&ifp->if_snd);
1123 * Remove all IPv6 kernel structs related to ifp. This should be done
1124 * before removing routing entries below, since IPv6 interface direct
1125 * routes are expected to be removed by the IPv6-specific kernel API.
1126 * Otherwise, the kernel will detect some inconsistency and bark it.
1130 if_purgemaddrs(ifp);
1132 /* Announce that the interface is gone. */
1133 rt_ifannouncemsg(ifp, IFAN_DEPARTURE);
1134 EVENTHANDLER_INVOKE(ifnet_departure_event, ifp);
1135 if (IS_DEFAULT_VNET(curvnet))
1136 devctl_notify("IFNET", ifp->if_xname, "DETACH", NULL);
1140 * Prevent further calls into the device driver via ifnet.
1145 * Remove link ifaddr pointer and maybe decrement if_index.
1146 * Clean up all addresses.
1148 free(ifp->if_hw_addr, M_IFADDR);
1149 ifp->if_hw_addr = NULL;
1150 ifp->if_addr = NULL;
1152 /* We can now free link ifaddr. */
1154 if (!TAILQ_EMPTY(&ifp->if_addrhead)) {
1155 ifa = TAILQ_FIRST(&ifp->if_addrhead);
1156 TAILQ_REMOVE(&ifp->if_addrhead, ifa, ifa_link);
1157 IF_ADDR_WUNLOCK(ifp);
1160 IF_ADDR_WUNLOCK(ifp);
1163 rt_flushifroutes(ifp);
1166 finish_vnet_shutdown:
1169 * We cannot hold the lock over dom_ifdetach calls as they might
1170 * sleep, for example trying to drain a callout, thus open up the
1171 * theoretical race with re-attaching.
1173 IF_AFDATA_LOCK(ifp);
1174 i = ifp->if_afdata_initialized;
1175 ifp->if_afdata_initialized = 0;
1176 IF_AFDATA_UNLOCK(ifp);
1177 for (dp = domains; i > 0 && dp; dp = dp->dom_next) {
1178 if (dp->dom_ifdetach && ifp->if_afdata[dp->dom_family]) {
1179 (*dp->dom_ifdetach)(ifp,
1180 ifp->if_afdata[dp->dom_family]);
1181 ifp->if_afdata[dp->dom_family] = NULL;
1190 * if_vmove() performs a limited version of if_detach() in current
1191 * vnet and if_attach()es the ifnet to the vnet specified as 2nd arg.
1192 * An attempt is made to shrink if_index in current vnet, find an
1193 * unused if_index in target vnet and calls if_grow() if necessary,
1194 * and finally find an unused if_xname for the target vnet.
1197 if_vmove(struct ifnet *ifp, struct vnet *new_vnet)
1199 struct if_clone *ifc;
1200 u_int bif_dlt, bif_hdrlen;
1204 * if_detach_internal() will call the eventhandler to notify
1205 * interface departure. That will detach if_bpf. We need to
1206 * safe the dlt and hdrlen so we can re-attach it later.
1208 bpf_get_bp_params(ifp->if_bpf, &bif_dlt, &bif_hdrlen);
1211 * Detach from current vnet, but preserve LLADDR info, do not
1212 * mark as dead etc. so that the ifnet can be reattached later.
1213 * If we cannot find it, we lost the race to someone else.
1215 rc = if_detach_internal(ifp, 1, &ifc);
1220 * Unlink the ifnet from ifindex_table[] in current vnet, and shrink
1221 * the if_index for that vnet if possible.
1223 * NOTE: IFNET_WLOCK/IFNET_WUNLOCK() are assumed to be unvirtualized,
1224 * or we'd lock on one vnet and unlock on another.
1227 ifindex_free_locked(ifp->if_index);
1231 * Perform interface-specific reassignment tasks, if provided by
1234 if (ifp->if_reassign != NULL)
1235 ifp->if_reassign(ifp, new_vnet, NULL);
1238 * Switch to the context of the target vnet.
1240 CURVNET_SET_QUIET(new_vnet);
1243 ifp->if_index = ifindex_alloc();
1244 ifnet_setbyindex_locked(ifp->if_index, ifp);
1247 if_attach_internal(ifp, 1, ifc);
1249 if (ifp->if_bpf == NULL)
1250 bpfattach(ifp, bif_dlt, bif_hdrlen);
1256 * Move an ifnet to or from another child prison/vnet, specified by the jail id.
1259 if_vmove_loan(struct thread *td, struct ifnet *ifp, char *ifname, int jid)
1265 /* Try to find the prison within our visibility. */
1266 sx_slock(&allprison_lock);
1267 pr = prison_find_child(td->td_ucred->cr_prison, jid);
1268 sx_sunlock(&allprison_lock);
1271 prison_hold_locked(pr);
1272 mtx_unlock(&pr->pr_mtx);
1274 /* Do not try to move the iface from and to the same prison. */
1275 if (pr->pr_vnet == ifp->if_vnet) {
1280 /* Make sure the named iface does not exists in the dst. prison/vnet. */
1281 /* XXX Lock interfaces to avoid races. */
1282 CURVNET_SET_QUIET(pr->pr_vnet);
1283 difp = ifunit(ifname);
1290 /* Make sure the VNET is stable. */
1291 shutdown = (ifp->if_vnet->vnet_state > SI_SUB_VNET &&
1292 ifp->if_vnet->vnet_state < SI_SUB_VNET_DONE) ? 1 : 0;
1300 /* Move the interface into the child jail/vnet. */
1301 if_vmove(ifp, pr->pr_vnet);
1303 /* Report the new if_xname back to the userland. */
1304 sprintf(ifname, "%s", ifp->if_xname);
1311 if_vmove_reclaim(struct thread *td, char *ifname, int jid)
1314 struct vnet *vnet_dst;
1318 /* Try to find the prison within our visibility. */
1319 sx_slock(&allprison_lock);
1320 pr = prison_find_child(td->td_ucred->cr_prison, jid);
1321 sx_sunlock(&allprison_lock);
1324 prison_hold_locked(pr);
1325 mtx_unlock(&pr->pr_mtx);
1327 /* Make sure the named iface exists in the source prison/vnet. */
1328 CURVNET_SET(pr->pr_vnet);
1329 ifp = ifunit(ifname); /* XXX Lock to avoid races. */
1336 /* Do not try to move the iface from and to the same prison. */
1337 vnet_dst = TD_TO_VNET(td);
1338 if (vnet_dst == ifp->if_vnet) {
1344 /* Make sure the VNET is stable. */
1345 shutdown = (ifp->if_vnet->vnet_state > SI_SUB_VNET &&
1346 ifp->if_vnet->vnet_state < SI_SUB_VNET_DONE) ? 1 : 0;
1353 /* Get interface back from child jail/vnet. */
1354 if_vmove(ifp, vnet_dst);
1357 /* Report the new if_xname back to the userland. */
1358 sprintf(ifname, "%s", ifp->if_xname);
1366 * Add a group to an interface
1369 if_addgroup(struct ifnet *ifp, const char *groupname)
1371 struct ifg_list *ifgl;
1372 struct ifg_group *ifg = NULL;
1373 struct ifg_member *ifgm;
1376 if (groupname[0] && groupname[strlen(groupname) - 1] >= '0' &&
1377 groupname[strlen(groupname) - 1] <= '9')
1381 TAILQ_FOREACH(ifgl, &ifp->if_groups, ifgl_next)
1382 if (!strcmp(ifgl->ifgl_group->ifg_group, groupname)) {
1387 if ((ifgl = (struct ifg_list *)malloc(sizeof(struct ifg_list), M_TEMP,
1388 M_NOWAIT)) == NULL) {
1393 if ((ifgm = (struct ifg_member *)malloc(sizeof(struct ifg_member),
1394 M_TEMP, M_NOWAIT)) == NULL) {
1400 TAILQ_FOREACH(ifg, &V_ifg_head, ifg_next)
1401 if (!strcmp(ifg->ifg_group, groupname))
1405 if ((ifg = (struct ifg_group *)malloc(sizeof(struct ifg_group),
1406 M_TEMP, M_NOWAIT)) == NULL) {
1412 strlcpy(ifg->ifg_group, groupname, sizeof(ifg->ifg_group));
1413 ifg->ifg_refcnt = 0;
1414 TAILQ_INIT(&ifg->ifg_members);
1415 TAILQ_INSERT_TAIL(&V_ifg_head, ifg, ifg_next);
1420 ifgl->ifgl_group = ifg;
1421 ifgm->ifgm_ifp = ifp;
1424 TAILQ_INSERT_TAIL(&ifg->ifg_members, ifgm, ifgm_next);
1425 TAILQ_INSERT_TAIL(&ifp->if_groups, ifgl, ifgl_next);
1426 IF_ADDR_WUNLOCK(ifp);
1431 EVENTHANDLER_INVOKE(group_attach_event, ifg);
1432 EVENTHANDLER_INVOKE(group_change_event, groupname);
1438 * Remove a group from an interface
1441 if_delgroup(struct ifnet *ifp, const char *groupname)
1443 struct ifg_list *ifgl;
1444 struct ifg_member *ifgm;
1447 TAILQ_FOREACH(ifgl, &ifp->if_groups, ifgl_next)
1448 if (!strcmp(ifgl->ifgl_group->ifg_group, groupname))
1456 TAILQ_REMOVE(&ifp->if_groups, ifgl, ifgl_next);
1457 IF_ADDR_WUNLOCK(ifp);
1459 TAILQ_FOREACH(ifgm, &ifgl->ifgl_group->ifg_members, ifgm_next)
1460 if (ifgm->ifgm_ifp == ifp)
1464 TAILQ_REMOVE(&ifgl->ifgl_group->ifg_members, ifgm, ifgm_next);
1468 if (--ifgl->ifgl_group->ifg_refcnt == 0) {
1469 TAILQ_REMOVE(&V_ifg_head, ifgl->ifgl_group, ifg_next);
1471 EVENTHANDLER_INVOKE(group_detach_event, ifgl->ifgl_group);
1472 free(ifgl->ifgl_group, M_TEMP);
1478 EVENTHANDLER_INVOKE(group_change_event, groupname);
1484 * Remove an interface from all groups
1487 if_delgroups(struct ifnet *ifp)
1489 struct ifg_list *ifgl;
1490 struct ifg_member *ifgm;
1491 char groupname[IFNAMSIZ];
1494 while (!TAILQ_EMPTY(&ifp->if_groups)) {
1495 ifgl = TAILQ_FIRST(&ifp->if_groups);
1497 strlcpy(groupname, ifgl->ifgl_group->ifg_group, IFNAMSIZ);
1500 TAILQ_REMOVE(&ifp->if_groups, ifgl, ifgl_next);
1501 IF_ADDR_WUNLOCK(ifp);
1503 TAILQ_FOREACH(ifgm, &ifgl->ifgl_group->ifg_members, ifgm_next)
1504 if (ifgm->ifgm_ifp == ifp)
1508 TAILQ_REMOVE(&ifgl->ifgl_group->ifg_members, ifgm,
1513 if (--ifgl->ifgl_group->ifg_refcnt == 0) {
1514 TAILQ_REMOVE(&V_ifg_head, ifgl->ifgl_group, ifg_next);
1516 EVENTHANDLER_INVOKE(group_detach_event,
1518 free(ifgl->ifgl_group, M_TEMP);
1524 EVENTHANDLER_INVOKE(group_change_event, groupname);
1532 ifgr_group_get(void *ifgrp)
1534 union ifgroupreq_union *ifgrup;
1537 #ifdef COMPAT_FREEBSD32
1538 if (SV_CURPROC_FLAG(SV_ILP32))
1539 return (&ifgrup->ifgr32.ifgr_ifgru.ifgru_group[0]);
1541 return (&ifgrup->ifgr.ifgr_ifgru.ifgru_group[0]);
1544 static struct ifg_req *
1545 ifgr_groups_get(void *ifgrp)
1547 union ifgroupreq_union *ifgrup;
1550 #ifdef COMPAT_FREEBSD32
1551 if (SV_CURPROC_FLAG(SV_ILP32))
1552 return ((struct ifg_req *)(uintptr_t)
1553 ifgrup->ifgr32.ifgr_ifgru.ifgru_groups);
1555 return (ifgrup->ifgr.ifgr_ifgru.ifgru_groups);
1559 * Stores all groups from an interface in memory pointed to by ifgr.
1562 if_getgroup(struct ifgroupreq *ifgr, struct ifnet *ifp)
1565 struct ifg_list *ifgl;
1566 struct ifg_req ifgrq, *ifgp;
1568 if (ifgr->ifgr_len == 0) {
1570 TAILQ_FOREACH(ifgl, &ifp->if_groups, ifgl_next)
1571 ifgr->ifgr_len += sizeof(struct ifg_req);
1572 IF_ADDR_RUNLOCK(ifp);
1576 len = ifgr->ifgr_len;
1577 ifgp = ifgr_groups_get(ifgr);
1580 TAILQ_FOREACH(ifgl, &ifp->if_groups, ifgl_next) {
1581 if (len < sizeof(ifgrq)) {
1582 IF_ADDR_RUNLOCK(ifp);
1585 bzero(&ifgrq, sizeof ifgrq);
1586 strlcpy(ifgrq.ifgrq_group, ifgl->ifgl_group->ifg_group,
1587 sizeof(ifgrq.ifgrq_group));
1588 if ((error = copyout(&ifgrq, ifgp, sizeof(struct ifg_req)))) {
1589 IF_ADDR_RUNLOCK(ifp);
1592 len -= sizeof(ifgrq);
1595 IF_ADDR_RUNLOCK(ifp);
1601 * Stores all members of a group in memory pointed to by igfr
1604 if_getgroupmembers(struct ifgroupreq *ifgr)
1606 struct ifg_group *ifg;
1607 struct ifg_member *ifgm;
1608 struct ifg_req ifgrq, *ifgp;
1612 TAILQ_FOREACH(ifg, &V_ifg_head, ifg_next)
1613 if (!strcmp(ifg->ifg_group, ifgr->ifgr_name))
1620 if (ifgr->ifgr_len == 0) {
1621 TAILQ_FOREACH(ifgm, &ifg->ifg_members, ifgm_next)
1622 ifgr->ifgr_len += sizeof(ifgrq);
1627 len = ifgr->ifgr_len;
1628 ifgp = ifgr_groups_get(ifgr);
1629 TAILQ_FOREACH(ifgm, &ifg->ifg_members, ifgm_next) {
1630 if (len < sizeof(ifgrq)) {
1634 bzero(&ifgrq, sizeof ifgrq);
1635 strlcpy(ifgrq.ifgrq_member, ifgm->ifgm_ifp->if_xname,
1636 sizeof(ifgrq.ifgrq_member));
1637 if ((error = copyout(&ifgrq, ifgp, sizeof(struct ifg_req)))) {
1641 len -= sizeof(ifgrq);
1650 * Return counter values from counter(9)s stored in ifnet.
1653 if_get_counter_default(struct ifnet *ifp, ift_counter cnt)
1656 KASSERT(cnt < IFCOUNTERS, ("%s: invalid cnt %d", __func__, cnt));
1658 return (counter_u64_fetch(ifp->if_counters[cnt]));
1662 * Increase an ifnet counter. Usually used for counters shared
1663 * between the stack and a driver, but function supports them all.
1666 if_inc_counter(struct ifnet *ifp, ift_counter cnt, int64_t inc)
1669 KASSERT(cnt < IFCOUNTERS, ("%s: invalid cnt %d", __func__, cnt));
1671 counter_u64_add(ifp->if_counters[cnt], inc);
1675 * Copy data from ifnet to userland API structure if_data.
1678 if_data_copy(struct ifnet *ifp, struct if_data *ifd)
1681 ifd->ifi_type = ifp->if_type;
1682 ifd->ifi_physical = 0;
1683 ifd->ifi_addrlen = ifp->if_addrlen;
1684 ifd->ifi_hdrlen = ifp->if_hdrlen;
1685 ifd->ifi_link_state = ifp->if_link_state;
1687 ifd->ifi_datalen = sizeof(struct if_data);
1688 ifd->ifi_mtu = ifp->if_mtu;
1689 ifd->ifi_metric = ifp->if_metric;
1690 ifd->ifi_baudrate = ifp->if_baudrate;
1691 ifd->ifi_hwassist = ifp->if_hwassist;
1692 ifd->ifi_epoch = ifp->if_epoch;
1693 ifd->ifi_lastchange = ifp->if_lastchange;
1695 ifd->ifi_ipackets = ifp->if_get_counter(ifp, IFCOUNTER_IPACKETS);
1696 ifd->ifi_ierrors = ifp->if_get_counter(ifp, IFCOUNTER_IERRORS);
1697 ifd->ifi_opackets = ifp->if_get_counter(ifp, IFCOUNTER_OPACKETS);
1698 ifd->ifi_oerrors = ifp->if_get_counter(ifp, IFCOUNTER_OERRORS);
1699 ifd->ifi_collisions = ifp->if_get_counter(ifp, IFCOUNTER_COLLISIONS);
1700 ifd->ifi_ibytes = ifp->if_get_counter(ifp, IFCOUNTER_IBYTES);
1701 ifd->ifi_obytes = ifp->if_get_counter(ifp, IFCOUNTER_OBYTES);
1702 ifd->ifi_imcasts = ifp->if_get_counter(ifp, IFCOUNTER_IMCASTS);
1703 ifd->ifi_omcasts = ifp->if_get_counter(ifp, IFCOUNTER_OMCASTS);
1704 ifd->ifi_iqdrops = ifp->if_get_counter(ifp, IFCOUNTER_IQDROPS);
1705 ifd->ifi_oqdrops = ifp->if_get_counter(ifp, IFCOUNTER_OQDROPS);
1706 ifd->ifi_noproto = ifp->if_get_counter(ifp, IFCOUNTER_NOPROTO);
1710 * Wrapper functions for struct ifnet address list locking macros. These are
1711 * used by kernel modules to avoid encoding programming interface or binary
1712 * interface assumptions that may be violated when kernel-internal locking
1713 * approaches change.
1716 if_addr_rlock(struct ifnet *ifp)
1723 if_addr_runlock(struct ifnet *ifp)
1726 IF_ADDR_RUNLOCK(ifp);
1730 if_maddr_rlock(if_t ifp)
1733 IF_ADDR_RLOCK((struct ifnet *)ifp);
1737 if_maddr_runlock(if_t ifp)
1740 IF_ADDR_RUNLOCK((struct ifnet *)ifp);
1744 * Initialization, destruction and refcounting functions for ifaddrs.
1747 ifa_alloc(size_t size, int flags)
1751 KASSERT(size >= sizeof(struct ifaddr),
1752 ("%s: invalid size %zu", __func__, size));
1754 ifa = malloc(size, M_IFADDR, M_ZERO | flags);
1758 if ((ifa->ifa_opackets = counter_u64_alloc(flags)) == NULL)
1760 if ((ifa->ifa_ipackets = counter_u64_alloc(flags)) == NULL)
1762 if ((ifa->ifa_obytes = counter_u64_alloc(flags)) == NULL)
1764 if ((ifa->ifa_ibytes = counter_u64_alloc(flags)) == NULL)
1767 refcount_init(&ifa->ifa_refcnt, 1);
1772 /* free(NULL) is okay */
1773 counter_u64_free(ifa->ifa_opackets);
1774 counter_u64_free(ifa->ifa_ipackets);
1775 counter_u64_free(ifa->ifa_obytes);
1776 counter_u64_free(ifa->ifa_ibytes);
1777 free(ifa, M_IFADDR);
1783 ifa_ref(struct ifaddr *ifa)
1786 refcount_acquire(&ifa->ifa_refcnt);
1790 ifa_free(struct ifaddr *ifa)
1793 if (refcount_release(&ifa->ifa_refcnt)) {
1794 counter_u64_free(ifa->ifa_opackets);
1795 counter_u64_free(ifa->ifa_ipackets);
1796 counter_u64_free(ifa->ifa_obytes);
1797 counter_u64_free(ifa->ifa_ibytes);
1798 free(ifa, M_IFADDR);
1803 ifa_maintain_loopback_route(int cmd, const char *otype, struct ifaddr *ifa,
1804 struct sockaddr *ia)
1807 struct rt_addrinfo info;
1808 struct sockaddr_dl null_sdl;
1813 bzero(&info, sizeof(info));
1814 if (cmd != RTM_DELETE)
1815 info.rti_ifp = V_loif;
1816 info.rti_flags = ifa->ifa_flags | RTF_HOST | RTF_STATIC | RTF_PINNED;
1817 info.rti_info[RTAX_DST] = ia;
1818 info.rti_info[RTAX_GATEWAY] = (struct sockaddr *)&null_sdl;
1819 link_init_sdl(ifp, (struct sockaddr *)&null_sdl, ifp->if_type);
1821 error = rtrequest1_fib(cmd, &info, NULL, ifp->if_fib);
1824 log(LOG_DEBUG, "%s: %s failed for interface %s: %u\n",
1825 __func__, otype, if_name(ifp), error);
1831 ifa_add_loopback_route(struct ifaddr *ifa, struct sockaddr *ia)
1834 return (ifa_maintain_loopback_route(RTM_ADD, "insertion", ifa, ia));
1838 ifa_del_loopback_route(struct ifaddr *ifa, struct sockaddr *ia)
1841 return (ifa_maintain_loopback_route(RTM_DELETE, "deletion", ifa, ia));
1845 ifa_switch_loopback_route(struct ifaddr *ifa, struct sockaddr *ia)
1848 return (ifa_maintain_loopback_route(RTM_CHANGE, "switch", ifa, ia));
1852 * XXX: Because sockaddr_dl has deeper structure than the sockaddr
1853 * structs used to represent other address families, it is necessary
1854 * to perform a different comparison.
1857 #define sa_dl_equal(a1, a2) \
1858 ((((const struct sockaddr_dl *)(a1))->sdl_len == \
1859 ((const struct sockaddr_dl *)(a2))->sdl_len) && \
1860 (bcmp(CLLADDR((const struct sockaddr_dl *)(a1)), \
1861 CLLADDR((const struct sockaddr_dl *)(a2)), \
1862 ((const struct sockaddr_dl *)(a1))->sdl_alen) == 0))
1865 * Locate an interface based on a complete address.
1868 static struct ifaddr *
1869 ifa_ifwithaddr_internal(const struct sockaddr *addr, int getref)
1874 IFNET_RLOCK_NOSLEEP();
1875 TAILQ_FOREACH(ifp, &V_ifnet, if_link) {
1877 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
1878 if (ifa->ifa_addr->sa_family != addr->sa_family)
1880 if (sa_equal(addr, ifa->ifa_addr)) {
1883 IF_ADDR_RUNLOCK(ifp);
1886 /* IP6 doesn't have broadcast */
1887 if ((ifp->if_flags & IFF_BROADCAST) &&
1888 ifa->ifa_broadaddr &&
1889 ifa->ifa_broadaddr->sa_len != 0 &&
1890 sa_equal(ifa->ifa_broadaddr, addr)) {
1893 IF_ADDR_RUNLOCK(ifp);
1897 IF_ADDR_RUNLOCK(ifp);
1901 IFNET_RUNLOCK_NOSLEEP();
1906 ifa_ifwithaddr(const struct sockaddr *addr)
1909 return (ifa_ifwithaddr_internal(addr, 1));
1913 ifa_ifwithaddr_check(const struct sockaddr *addr)
1916 return (ifa_ifwithaddr_internal(addr, 0) != NULL);
1920 * Locate an interface based on the broadcast address.
1924 ifa_ifwithbroadaddr(const struct sockaddr *addr, int fibnum)
1929 IFNET_RLOCK_NOSLEEP();
1930 TAILQ_FOREACH(ifp, &V_ifnet, if_link) {
1931 if ((fibnum != RT_ALL_FIBS) && (ifp->if_fib != fibnum))
1934 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
1935 if (ifa->ifa_addr->sa_family != addr->sa_family)
1937 if ((ifp->if_flags & IFF_BROADCAST) &&
1938 ifa->ifa_broadaddr &&
1939 ifa->ifa_broadaddr->sa_len != 0 &&
1940 sa_equal(ifa->ifa_broadaddr, addr)) {
1942 IF_ADDR_RUNLOCK(ifp);
1946 IF_ADDR_RUNLOCK(ifp);
1950 IFNET_RUNLOCK_NOSLEEP();
1955 * Locate the point to point interface with a given destination address.
1959 ifa_ifwithdstaddr(const struct sockaddr *addr, int fibnum)
1964 IFNET_RLOCK_NOSLEEP();
1965 TAILQ_FOREACH(ifp, &V_ifnet, if_link) {
1966 if ((ifp->if_flags & IFF_POINTOPOINT) == 0)
1968 if ((fibnum != RT_ALL_FIBS) && (ifp->if_fib != fibnum))
1971 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
1972 if (ifa->ifa_addr->sa_family != addr->sa_family)
1974 if (ifa->ifa_dstaddr != NULL &&
1975 sa_equal(addr, ifa->ifa_dstaddr)) {
1977 IF_ADDR_RUNLOCK(ifp);
1981 IF_ADDR_RUNLOCK(ifp);
1985 IFNET_RUNLOCK_NOSLEEP();
1990 * Find an interface on a specific network. If many, choice
1991 * is most specific found.
1994 ifa_ifwithnet(const struct sockaddr *addr, int ignore_ptp, int fibnum)
1998 struct ifaddr *ifa_maybe = NULL;
1999 u_int af = addr->sa_family;
2000 const char *addr_data = addr->sa_data, *cplim;
2003 * AF_LINK addresses can be looked up directly by their index number,
2004 * so do that if we can.
2006 if (af == AF_LINK) {
2007 const struct sockaddr_dl *sdl = (const struct sockaddr_dl *)addr;
2008 if (sdl->sdl_index && sdl->sdl_index <= V_if_index)
2009 return (ifaddr_byindex(sdl->sdl_index));
2013 * Scan though each interface, looking for ones that have addresses
2014 * in this address family and the requested fib. Maintain a reference
2015 * on ifa_maybe once we find one, as we release the IF_ADDR_RLOCK() that
2016 * kept it stable when we move onto the next interface.
2018 IFNET_RLOCK_NOSLEEP();
2019 TAILQ_FOREACH(ifp, &V_ifnet, if_link) {
2020 if ((fibnum != RT_ALL_FIBS) && (ifp->if_fib != fibnum))
2023 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
2024 const char *cp, *cp2, *cp3;
2026 if (ifa->ifa_addr->sa_family != af)
2028 if (af == AF_INET &&
2029 ifp->if_flags & IFF_POINTOPOINT && !ignore_ptp) {
2031 * This is a bit broken as it doesn't
2032 * take into account that the remote end may
2033 * be a single node in the network we are
2035 * The trouble is that we don't know the
2036 * netmask for the remote end.
2038 if (ifa->ifa_dstaddr != NULL &&
2039 sa_equal(addr, ifa->ifa_dstaddr)) {
2041 IF_ADDR_RUNLOCK(ifp);
2046 * Scan all the bits in the ifa's address.
2047 * If a bit dissagrees with what we are
2048 * looking for, mask it with the netmask
2049 * to see if it really matters.
2050 * (A byte at a time)
2052 if (ifa->ifa_netmask == 0)
2055 cp2 = ifa->ifa_addr->sa_data;
2056 cp3 = ifa->ifa_netmask->sa_data;
2057 cplim = ifa->ifa_netmask->sa_len
2058 + (char *)ifa->ifa_netmask;
2060 if ((*cp++ ^ *cp2++) & *cp3++)
2061 goto next; /* next address! */
2063 * If the netmask of what we just found
2064 * is more specific than what we had before
2065 * (if we had one), or if the virtual status
2066 * of new prefix is better than of the old one,
2067 * then remember the new one before continuing
2068 * to search for an even better one.
2070 if (ifa_maybe == NULL ||
2071 ifa_preferred(ifa_maybe, ifa) ||
2072 rn_refines((caddr_t)ifa->ifa_netmask,
2073 (caddr_t)ifa_maybe->ifa_netmask)) {
2074 if (ifa_maybe != NULL)
2075 ifa_free(ifa_maybe);
2081 IF_ADDR_RUNLOCK(ifp);
2086 IFNET_RUNLOCK_NOSLEEP();
2087 if (ifa_maybe != NULL)
2088 ifa_free(ifa_maybe);
2093 * Find an interface address specific to an interface best matching
2097 ifaof_ifpforaddr(const struct sockaddr *addr, struct ifnet *ifp)
2100 const char *cp, *cp2, *cp3;
2102 struct ifaddr *ifa_maybe = NULL;
2103 u_int af = addr->sa_family;
2108 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
2109 if (ifa->ifa_addr->sa_family != af)
2111 if (ifa_maybe == NULL)
2113 if (ifa->ifa_netmask == 0) {
2114 if (sa_equal(addr, ifa->ifa_addr) ||
2115 (ifa->ifa_dstaddr &&
2116 sa_equal(addr, ifa->ifa_dstaddr)))
2120 if (ifp->if_flags & IFF_POINTOPOINT) {
2121 if (sa_equal(addr, ifa->ifa_dstaddr))
2125 cp2 = ifa->ifa_addr->sa_data;
2126 cp3 = ifa->ifa_netmask->sa_data;
2127 cplim = ifa->ifa_netmask->sa_len + (char *)ifa->ifa_netmask;
2128 for (; cp3 < cplim; cp3++)
2129 if ((*cp++ ^ *cp2++) & *cp3)
2139 IF_ADDR_RUNLOCK(ifp);
2144 * See whether new ifa is better than current one:
2145 * 1) A non-virtual one is preferred over virtual.
2146 * 2) A virtual in master state preferred over any other state.
2148 * Used in several address selecting functions.
2151 ifa_preferred(struct ifaddr *cur, struct ifaddr *next)
2154 return (cur->ifa_carp && (!next->ifa_carp ||
2155 ((*carp_master_p)(next) && !(*carp_master_p)(cur))));
2158 #include <net/if_llatbl.h>
2161 * Default action when installing a route with a Link Level gateway.
2162 * Lookup an appropriate real ifa to point to.
2163 * This should be moved to /sys/net/link.c eventually.
2166 link_rtrequest(int cmd, struct rtentry *rt, struct rt_addrinfo *info)
2168 struct ifaddr *ifa, *oifa;
2169 struct sockaddr *dst;
2172 if (cmd != RTM_ADD || ((ifa = rt->rt_ifa) == NULL) ||
2173 ((ifp = ifa->ifa_ifp) == NULL) || ((dst = rt_key(rt)) == NULL))
2175 ifa = ifaof_ifpforaddr(dst, ifp);
2180 if (ifa->ifa_rtrequest && ifa->ifa_rtrequest != link_rtrequest)
2181 ifa->ifa_rtrequest(cmd, rt, info);
2185 struct sockaddr_dl *
2186 link_alloc_sdl(size_t size, int flags)
2189 return (malloc(size, M_TEMP, flags));
2193 link_free_sdl(struct sockaddr *sa)
2199 * Fills in given sdl with interface basic info.
2200 * Returns pointer to filled sdl.
2202 struct sockaddr_dl *
2203 link_init_sdl(struct ifnet *ifp, struct sockaddr *paddr, u_char iftype)
2205 struct sockaddr_dl *sdl;
2207 sdl = (struct sockaddr_dl *)paddr;
2208 memset(sdl, 0, sizeof(struct sockaddr_dl));
2209 sdl->sdl_len = sizeof(struct sockaddr_dl);
2210 sdl->sdl_family = AF_LINK;
2211 sdl->sdl_index = ifp->if_index;
2212 sdl->sdl_type = iftype;
2218 * Mark an interface down and notify protocols of
2222 if_unroute(struct ifnet *ifp, int flag, int fam)
2226 KASSERT(flag == IFF_UP, ("if_unroute: flag != IFF_UP"));
2228 ifp->if_flags &= ~flag;
2229 getmicrotime(&ifp->if_lastchange);
2230 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link)
2231 if (fam == PF_UNSPEC || (fam == ifa->ifa_addr->sa_family))
2232 pfctlinput(PRC_IFDOWN, ifa->ifa_addr);
2233 ifp->if_qflush(ifp);
2236 (*carp_linkstate_p)(ifp);
2241 * Mark an interface up and notify protocols of
2245 if_route(struct ifnet *ifp, int flag, int fam)
2249 KASSERT(flag == IFF_UP, ("if_route: flag != IFF_UP"));
2251 ifp->if_flags |= flag;
2252 getmicrotime(&ifp->if_lastchange);
2253 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link)
2254 if (fam == PF_UNSPEC || (fam == ifa->ifa_addr->sa_family))
2255 pfctlinput(PRC_IFUP, ifa->ifa_addr);
2257 (*carp_linkstate_p)(ifp);
2264 void (*vlan_link_state_p)(struct ifnet *); /* XXX: private from if_vlan */
2265 void (*vlan_trunk_cap_p)(struct ifnet *); /* XXX: private from if_vlan */
2266 struct ifnet *(*vlan_trunkdev_p)(struct ifnet *);
2267 struct ifnet *(*vlan_devat_p)(struct ifnet *, uint16_t);
2268 int (*vlan_tag_p)(struct ifnet *, uint16_t *);
2269 int (*vlan_setcookie_p)(struct ifnet *, void *);
2270 void *(*vlan_cookie_p)(struct ifnet *);
2273 * Handle a change in the interface link state. To avoid LORs
2274 * between driver lock and upper layer locks, as well as possible
2275 * recursions, we post event to taskqueue, and all job
2276 * is done in static do_link_state_change().
2279 if_link_state_change(struct ifnet *ifp, int link_state)
2281 /* Return if state hasn't changed. */
2282 if (ifp->if_link_state == link_state)
2285 ifp->if_link_state = link_state;
2287 taskqueue_enqueue(taskqueue_swi, &ifp->if_linktask);
2291 do_link_state_change(void *arg, int pending)
2293 struct ifnet *ifp = (struct ifnet *)arg;
2294 int link_state = ifp->if_link_state;
2295 CURVNET_SET(ifp->if_vnet);
2297 /* Notify that the link state has changed. */
2299 if (ifp->if_vlantrunk != NULL)
2300 (*vlan_link_state_p)(ifp);
2302 if ((ifp->if_type == IFT_ETHER || ifp->if_type == IFT_L2VLAN) &&
2303 ifp->if_l2com != NULL)
2304 (*ng_ether_link_state_p)(ifp, link_state);
2306 (*carp_linkstate_p)(ifp);
2308 (*bridge_linkstate_p)(ifp);
2310 (*lagg_linkstate_p)(ifp, link_state);
2312 if (IS_DEFAULT_VNET(curvnet))
2313 devctl_notify("IFNET", ifp->if_xname,
2314 (link_state == LINK_STATE_UP) ? "LINK_UP" : "LINK_DOWN",
2317 if_printf(ifp, "%d link states coalesced\n", pending);
2318 if (log_link_state_change)
2319 log(LOG_NOTICE, "%s: link state changed to %s\n", ifp->if_xname,
2320 (link_state == LINK_STATE_UP) ? "UP" : "DOWN" );
2321 EVENTHANDLER_INVOKE(ifnet_link_event, ifp, link_state);
2326 * Mark an interface down and notify protocols of
2330 if_down(struct ifnet *ifp)
2333 EVENTHANDLER_INVOKE(ifnet_event, ifp, IFNET_EVENT_DOWN);
2334 if_unroute(ifp, IFF_UP, AF_UNSPEC);
2338 * Mark an interface up and notify protocols of
2342 if_up(struct ifnet *ifp)
2345 if_route(ifp, IFF_UP, AF_UNSPEC);
2346 EVENTHANDLER_INVOKE(ifnet_event, ifp, IFNET_EVENT_UP);
2350 * Flush an interface queue.
2353 if_qflush(struct ifnet *ifp)
2361 if (ALTQ_IS_ENABLED(ifq))
2365 while ((m = n) != NULL) {
2376 * Map interface name to interface structure pointer, with or without
2377 * returning a reference.
2380 ifunit_ref(const char *name)
2384 IFNET_RLOCK_NOSLEEP();
2385 TAILQ_FOREACH(ifp, &V_ifnet, if_link) {
2386 if (strncmp(name, ifp->if_xname, IFNAMSIZ) == 0 &&
2387 !(ifp->if_flags & IFF_DYING))
2392 IFNET_RUNLOCK_NOSLEEP();
2397 ifunit(const char *name)
2401 IFNET_RLOCK_NOSLEEP();
2402 TAILQ_FOREACH(ifp, &V_ifnet, if_link) {
2403 if (strncmp(name, ifp->if_xname, IFNAMSIZ) == 0)
2406 IFNET_RUNLOCK_NOSLEEP();
2411 ifr_buffer_get_buffer(void *data)
2413 union ifreq_union *ifrup;
2416 #ifdef COMPAT_FREEBSD32
2417 if (SV_CURPROC_FLAG(SV_ILP32))
2418 return ((void *)(uintptr_t)
2419 ifrup->ifr32.ifr_ifru.ifru_buffer.buffer);
2421 return (ifrup->ifr.ifr_ifru.ifru_buffer.buffer);
2425 ifr_buffer_set_buffer_null(void *data)
2427 union ifreq_union *ifrup;
2430 #ifdef COMPAT_FREEBSD32
2431 if (SV_CURPROC_FLAG(SV_ILP32))
2432 ifrup->ifr32.ifr_ifru.ifru_buffer.buffer = 0;
2435 ifrup->ifr.ifr_ifru.ifru_buffer.buffer = NULL;
2439 ifr_buffer_get_length(void *data)
2441 union ifreq_union *ifrup;
2444 #ifdef COMPAT_FREEBSD32
2445 if (SV_CURPROC_FLAG(SV_ILP32))
2446 return (ifrup->ifr32.ifr_ifru.ifru_buffer.length);
2448 return (ifrup->ifr.ifr_ifru.ifru_buffer.length);
2452 ifr_buffer_set_length(void *data, size_t len)
2454 union ifreq_union *ifrup;
2457 #ifdef COMPAT_FREEBSD32
2458 if (SV_CURPROC_FLAG(SV_ILP32))
2459 ifrup->ifr32.ifr_ifru.ifru_buffer.length = len;
2462 ifrup->ifr.ifr_ifru.ifru_buffer.length = len;
2466 ifr_data_get_ptr(void *ifrp)
2468 union ifreq_union *ifrup;
2471 #ifdef COMPAT_FREEBSD32
2472 if (SV_CURPROC_FLAG(SV_ILP32))
2473 return ((void *)(uintptr_t)
2474 ifrup->ifr32.ifr_ifru.ifru_data);
2476 return (ifrup->ifr.ifr_ifru.ifru_data);
2480 * Hardware specific interface ioctls.
2483 ifhwioctl(u_long cmd, struct ifnet *ifp, caddr_t data, struct thread *td)
2486 int error = 0, do_ifup = 0;
2487 int new_flags, temp_flags;
2488 size_t namelen, onamelen;
2490 char *descrbuf, *odescrbuf;
2491 char new_name[IFNAMSIZ];
2493 struct sockaddr_dl *sdl;
2495 ifr = (struct ifreq *)data;
2498 ifr->ifr_index = ifp->if_index;
2502 temp_flags = ifp->if_flags | ifp->if_drv_flags;
2503 ifr->ifr_flags = temp_flags & 0xffff;
2504 ifr->ifr_flagshigh = temp_flags >> 16;
2508 ifr->ifr_reqcap = ifp->if_capabilities;
2509 ifr->ifr_curcap = ifp->if_capenable;
2514 error = mac_ifnet_ioctl_get(td->td_ucred, ifr, ifp);
2519 ifr->ifr_metric = ifp->if_metric;
2523 ifr->ifr_mtu = ifp->if_mtu;
2527 /* XXXGL: did this ever worked? */
2533 sx_slock(&ifdescr_sx);
2534 if (ifp->if_description == NULL)
2537 /* space for terminating nul */
2538 descrlen = strlen(ifp->if_description) + 1;
2539 if (ifr_buffer_get_length(ifr) < descrlen)
2540 ifr_buffer_set_buffer_null(ifr);
2542 error = copyout(ifp->if_description,
2543 ifr_buffer_get_buffer(ifr), descrlen);
2544 ifr_buffer_set_length(ifr, descrlen);
2546 sx_sunlock(&ifdescr_sx);
2550 error = priv_check(td, PRIV_NET_SETIFDESCR);
2555 * Copy only (length-1) bytes to make sure that
2556 * if_description is always nul terminated. The
2557 * length parameter is supposed to count the
2558 * terminating nul in.
2560 if (ifr_buffer_get_length(ifr) > ifdescr_maxlen)
2561 return (ENAMETOOLONG);
2562 else if (ifr_buffer_get_length(ifr) == 0)
2565 descrbuf = malloc(ifr_buffer_get_length(ifr),
2566 M_IFDESCR, M_WAITOK | M_ZERO);
2567 error = copyin(ifr_buffer_get_buffer(ifr), descrbuf,
2568 ifr_buffer_get_length(ifr) - 1);
2570 free(descrbuf, M_IFDESCR);
2575 sx_xlock(&ifdescr_sx);
2576 odescrbuf = ifp->if_description;
2577 ifp->if_description = descrbuf;
2578 sx_xunlock(&ifdescr_sx);
2580 getmicrotime(&ifp->if_lastchange);
2581 free(odescrbuf, M_IFDESCR);
2585 ifr->ifr_fib = ifp->if_fib;
2589 error = priv_check(td, PRIV_NET_SETIFFIB);
2592 if (ifr->ifr_fib >= rt_numfibs)
2595 ifp->if_fib = ifr->ifr_fib;
2599 error = priv_check(td, PRIV_NET_SETIFFLAGS);
2603 * Currently, no driver owned flags pass the IFF_CANTCHANGE
2604 * check, so we don't need special handling here yet.
2606 new_flags = (ifr->ifr_flags & 0xffff) |
2607 (ifr->ifr_flagshigh << 16);
2608 if (ifp->if_flags & IFF_UP &&
2609 (new_flags & IFF_UP) == 0) {
2611 } else if (new_flags & IFF_UP &&
2612 (ifp->if_flags & IFF_UP) == 0) {
2615 /* See if permanently promiscuous mode bit is about to flip */
2616 if ((ifp->if_flags ^ new_flags) & IFF_PPROMISC) {
2617 if (new_flags & IFF_PPROMISC)
2618 ifp->if_flags |= IFF_PROMISC;
2619 else if (ifp->if_pcount == 0)
2620 ifp->if_flags &= ~IFF_PROMISC;
2621 if (log_promisc_mode_change)
2622 log(LOG_INFO, "%s: permanently promiscuous mode %s\n",
2624 ((new_flags & IFF_PPROMISC) ?
2625 "enabled" : "disabled"));
2627 ifp->if_flags = (ifp->if_flags & IFF_CANTCHANGE) |
2628 (new_flags &~ IFF_CANTCHANGE);
2629 if (ifp->if_ioctl) {
2630 (void) (*ifp->if_ioctl)(ifp, cmd, data);
2634 getmicrotime(&ifp->if_lastchange);
2638 error = priv_check(td, PRIV_NET_SETIFCAP);
2641 if (ifp->if_ioctl == NULL)
2642 return (EOPNOTSUPP);
2643 if (ifr->ifr_reqcap & ~ifp->if_capabilities)
2645 error = (*ifp->if_ioctl)(ifp, cmd, data);
2647 getmicrotime(&ifp->if_lastchange);
2652 error = mac_ifnet_ioctl_set(td->td_ucred, ifr, ifp);
2657 error = priv_check(td, PRIV_NET_SETIFNAME);
2660 error = copyinstr(ifr_data_get_ptr(ifr), new_name, IFNAMSIZ,
2664 if (new_name[0] == '\0')
2666 if (new_name[IFNAMSIZ-1] != '\0') {
2667 new_name[IFNAMSIZ-1] = '\0';
2668 if (strlen(new_name) == IFNAMSIZ-1)
2671 if (ifunit(new_name) != NULL)
2675 * XXX: Locking. Nothing else seems to lock if_flags,
2676 * and there are numerous other races with the
2677 * ifunit() checks not being atomic with namespace
2678 * changes (renames, vmoves, if_attach, etc).
2680 ifp->if_flags |= IFF_RENAMING;
2682 /* Announce the departure of the interface. */
2683 rt_ifannouncemsg(ifp, IFAN_DEPARTURE);
2684 EVENTHANDLER_INVOKE(ifnet_departure_event, ifp);
2686 log(LOG_INFO, "%s: changing name to '%s'\n",
2687 ifp->if_xname, new_name);
2690 strlcpy(ifp->if_xname, new_name, sizeof(ifp->if_xname));
2692 sdl = (struct sockaddr_dl *)ifa->ifa_addr;
2693 namelen = strlen(new_name);
2694 onamelen = sdl->sdl_nlen;
2696 * Move the address if needed. This is safe because we
2697 * allocate space for a name of length IFNAMSIZ when we
2698 * create this in if_attach().
2700 if (namelen != onamelen) {
2701 bcopy(sdl->sdl_data + onamelen,
2702 sdl->sdl_data + namelen, sdl->sdl_alen);
2704 bcopy(new_name, sdl->sdl_data, namelen);
2705 sdl->sdl_nlen = namelen;
2706 sdl = (struct sockaddr_dl *)ifa->ifa_netmask;
2707 bzero(sdl->sdl_data, onamelen);
2708 while (namelen != 0)
2709 sdl->sdl_data[--namelen] = 0xff;
2710 IF_ADDR_WUNLOCK(ifp);
2712 EVENTHANDLER_INVOKE(ifnet_arrival_event, ifp);
2713 /* Announce the return of the interface. */
2714 rt_ifannouncemsg(ifp, IFAN_ARRIVAL);
2716 ifp->if_flags &= ~IFF_RENAMING;
2721 error = priv_check(td, PRIV_NET_SETIFVNET);
2724 error = if_vmove_loan(td, ifp, ifr->ifr_name, ifr->ifr_jid);
2729 error = priv_check(td, PRIV_NET_SETIFMETRIC);
2732 ifp->if_metric = ifr->ifr_metric;
2733 getmicrotime(&ifp->if_lastchange);
2737 error = priv_check(td, PRIV_NET_SETIFPHYS);
2740 if (ifp->if_ioctl == NULL)
2741 return (EOPNOTSUPP);
2742 error = (*ifp->if_ioctl)(ifp, cmd, data);
2744 getmicrotime(&ifp->if_lastchange);
2749 u_long oldmtu = ifp->if_mtu;
2751 error = priv_check(td, PRIV_NET_SETIFMTU);
2754 if (ifr->ifr_mtu < IF_MINMTU || ifr->ifr_mtu > IF_MAXMTU)
2756 if (ifp->if_ioctl == NULL)
2757 return (EOPNOTSUPP);
2758 error = (*ifp->if_ioctl)(ifp, cmd, data);
2760 getmicrotime(&ifp->if_lastchange);
2764 * If the link MTU changed, do network layer specific procedure.
2766 if (ifp->if_mtu != oldmtu) {
2777 if (cmd == SIOCADDMULTI)
2778 error = priv_check(td, PRIV_NET_ADDMULTI);
2780 error = priv_check(td, PRIV_NET_DELMULTI);
2784 /* Don't allow group membership on non-multicast interfaces. */
2785 if ((ifp->if_flags & IFF_MULTICAST) == 0)
2786 return (EOPNOTSUPP);
2788 /* Don't let users screw up protocols' entries. */
2789 if (ifr->ifr_addr.sa_family != AF_LINK)
2792 if (cmd == SIOCADDMULTI) {
2793 struct ifmultiaddr *ifma;
2796 * Userland is only permitted to join groups once
2797 * via the if_addmulti() KPI, because it cannot hold
2798 * struct ifmultiaddr * between calls. It may also
2799 * lose a race while we check if the membership
2803 ifma = if_findmulti(ifp, &ifr->ifr_addr);
2804 IF_ADDR_RUNLOCK(ifp);
2808 error = if_addmulti(ifp, &ifr->ifr_addr, &ifma);
2810 error = if_delmulti(ifp, &ifr->ifr_addr);
2813 getmicrotime(&ifp->if_lastchange);
2816 case SIOCSIFPHYADDR:
2817 case SIOCDIFPHYADDR:
2819 case SIOCSIFPHYADDR_IN6:
2822 case SIOCSIFGENERIC:
2823 error = priv_check(td, PRIV_NET_HWIOCTL);
2826 if (ifp->if_ioctl == NULL)
2827 return (EOPNOTSUPP);
2828 error = (*ifp->if_ioctl)(ifp, cmd, data);
2830 getmicrotime(&ifp->if_lastchange);
2834 case SIOCGIFPSRCADDR:
2835 case SIOCGIFPDSTADDR:
2838 case SIOCGIFGENERIC:
2840 case SIOCGIFRSSHASH:
2841 if (ifp->if_ioctl == NULL)
2842 return (EOPNOTSUPP);
2843 error = (*ifp->if_ioctl)(ifp, cmd, data);
2847 error = priv_check(td, PRIV_NET_SETLLADDR);
2850 error = if_setlladdr(ifp,
2851 ifr->ifr_addr.sa_data, ifr->ifr_addr.sa_len);
2855 error = if_gethwaddr(ifp, ifr);
2858 CASE_IOC_IFGROUPREQ(SIOCAIFGROUP):
2859 error = priv_check(td, PRIV_NET_ADDIFGROUP);
2862 if ((error = if_addgroup(ifp,
2863 ifgr_group_get((struct ifgroupreq *)data))))
2867 CASE_IOC_IFGROUPREQ(SIOCGIFGROUP):
2868 if ((error = if_getgroup((struct ifgroupreq *)data, ifp)))
2872 CASE_IOC_IFGROUPREQ(SIOCDIFGROUP):
2873 error = priv_check(td, PRIV_NET_DELIFGROUP);
2876 if ((error = if_delgroup(ifp,
2877 ifgr_group_get((struct ifgroupreq *)data))))
2889 #define OSIOCGIFCONF _IOWR('i', 20, struct ifconf)
2891 #ifdef COMPAT_FREEBSD32
2899 #define SIOCGIFCONF32 _IOWR('i', 36, struct ifconf32)
2902 #ifdef COMPAT_FREEBSD32
2904 ifmr_init(struct ifmediareq *ifmr, caddr_t data)
2906 struct ifmediareq32 *ifmr32;
2908 ifmr32 = (struct ifmediareq32 *)data;
2909 memcpy(ifmr->ifm_name, ifmr32->ifm_name,
2910 sizeof(ifmr->ifm_name));
2911 ifmr->ifm_current = ifmr32->ifm_current;
2912 ifmr->ifm_mask = ifmr32->ifm_mask;
2913 ifmr->ifm_status = ifmr32->ifm_status;
2914 ifmr->ifm_active = ifmr32->ifm_active;
2915 ifmr->ifm_count = ifmr32->ifm_count;
2916 ifmr->ifm_ulist = (int *)(uintptr_t)ifmr32->ifm_ulist;
2920 ifmr_update(const struct ifmediareq *ifmr, caddr_t data)
2922 struct ifmediareq32 *ifmr32;
2924 ifmr32 = (struct ifmediareq32 *)data;
2925 ifmr32->ifm_current = ifmr->ifm_current;
2926 ifmr32->ifm_mask = ifmr->ifm_mask;
2927 ifmr32->ifm_status = ifmr->ifm_status;
2928 ifmr32->ifm_active = ifmr->ifm_active;
2929 ifmr32->ifm_count = ifmr->ifm_count;
2937 ifioctl(struct socket *so, u_long cmd, caddr_t data, struct thread *td)
2939 #ifdef COMPAT_FREEBSD32
2941 struct ifmediareq ifmr;
2943 struct ifmediareq *ifmrp;
2952 CURVNET_SET(so->so_vnet);
2954 /* Make sure the VNET is stable. */
2955 shutdown = (so->so_vnet->vnet_state > SI_SUB_VNET &&
2956 so->so_vnet->vnet_state < SI_SUB_VNET_DONE) ? 1 : 0;
2966 case OSIOCGIFCONF: /* COMPAT_SVR4 */
2967 error = ifconf(cmd, data);
2971 #ifdef COMPAT_FREEBSD32
2974 struct ifconf32 *ifc32;
2977 ifc32 = (struct ifconf32 *)data;
2978 ifc.ifc_len = ifc32->ifc_len;
2979 ifc.ifc_buf = PTRIN(ifc32->ifc_buf);
2981 error = ifconf(SIOCGIFCONF, (void *)&ifc);
2984 ifc32->ifc_len = ifc.ifc_len;
2991 #ifdef COMPAT_FREEBSD32
2993 case SIOCGIFMEDIA32:
2994 case SIOCGIFXMEDIA32:
2996 ifmr_init(ifmrp, data);
2997 cmd = _IOC_NEWTYPE(cmd, struct ifmediareq);
2999 data = (caddr_t)ifmrp;
3003 ifr = (struct ifreq *)data;
3007 error = priv_check(td, PRIV_NET_SETIFVNET);
3009 error = if_vmove_reclaim(td, ifr->ifr_name,
3015 error = priv_check(td, PRIV_NET_IFCREATE);
3017 error = if_clone_create(ifr->ifr_name,
3018 sizeof(ifr->ifr_name), cmd == SIOCIFCREATE2 ?
3019 ifr_data_get_ptr(ifr) : NULL);
3022 error = priv_check(td, PRIV_NET_IFDESTROY);
3024 error = if_clone_destroy(ifr->ifr_name);
3027 case SIOCIFGCLONERS:
3028 error = if_clone_list((struct if_clonereq *)data);
3031 CASE_IOC_IFGROUPREQ(SIOCGIFGMEMB):
3032 error = if_getgroupmembers((struct ifgroupreq *)data);
3035 #if defined(INET) || defined(INET6)
3038 if (carp_ioctl_p == NULL)
3039 error = EPROTONOSUPPORT;
3041 error = (*carp_ioctl_p)(ifr, cmd, td);
3046 ifp = ifunit_ref(ifr->ifr_name);
3052 error = ifhwioctl(cmd, ifp, data, td);
3053 if (error != ENOIOCTL)
3056 oif_flags = ifp->if_flags;
3057 if (so->so_proto == NULL) {
3063 * Pass the request on to the socket control method, and if the
3064 * latter returns EOPNOTSUPP, directly to the interface.
3066 * Make an exception for the legacy SIOCSIF* requests. Drivers
3067 * trust SIOCSIFADDR et al to come from an already privileged
3068 * layer, and do not perform any credentials checks or input
3071 error = ((*so->so_proto->pr_usrreqs->pru_control)(so, cmd, data,
3073 if (error == EOPNOTSUPP && ifp != NULL && ifp->if_ioctl != NULL &&
3074 cmd != SIOCSIFADDR && cmd != SIOCSIFBRDADDR &&
3075 cmd != SIOCSIFDSTADDR && cmd != SIOCSIFNETMASK)
3076 error = (*ifp->if_ioctl)(ifp, cmd, data);
3078 if ((oif_flags ^ ifp->if_flags) & IFF_UP) {
3080 if (ifp->if_flags & IFF_UP)
3088 #ifdef COMPAT_FREEBSD32
3089 if (ifmrp != NULL) {
3090 KASSERT((cmd == SIOCGIFMEDIA || cmd == SIOCGIFXMEDIA),
3091 ("ifmrp non-NULL, but cmd is not an ifmedia req 0x%lx",
3094 ifmr_update(ifmrp, data);
3102 * The code common to handling reference counted flags,
3103 * e.g., in ifpromisc() and if_allmulti().
3104 * The "pflag" argument can specify a permanent mode flag to check,
3105 * such as IFF_PPROMISC for promiscuous mode; should be 0 if none.
3107 * Only to be used on stack-owned flags, not driver-owned flags.
3110 if_setflag(struct ifnet *ifp, int flag, int pflag, int *refcount, int onswitch)
3114 int oldflags, oldcount;
3116 /* Sanity checks to catch programming errors */
3117 KASSERT((flag & (IFF_DRV_OACTIVE|IFF_DRV_RUNNING)) == 0,
3118 ("%s: setting driver-owned flag %d", __func__, flag));
3121 KASSERT(*refcount >= 0,
3122 ("%s: increment negative refcount %d for flag %d",
3123 __func__, *refcount, flag));
3125 KASSERT(*refcount > 0,
3126 ("%s: decrement non-positive refcount %d for flag %d",
3127 __func__, *refcount, flag));
3129 /* In case this mode is permanent, just touch refcount */
3130 if (ifp->if_flags & pflag) {
3131 *refcount += onswitch ? 1 : -1;
3135 /* Save ifnet parameters for if_ioctl() may fail */
3136 oldcount = *refcount;
3137 oldflags = ifp->if_flags;
3140 * See if we aren't the only and touching refcount is enough.
3141 * Actually toggle interface flag if we are the first or last.
3146 ifp->if_flags |= flag;
3150 ifp->if_flags &= ~flag;
3153 /* Call down the driver since we've changed interface flags */
3154 if (ifp->if_ioctl == NULL) {
3158 ifr.ifr_flags = ifp->if_flags & 0xffff;
3159 ifr.ifr_flagshigh = ifp->if_flags >> 16;
3160 error = (*ifp->if_ioctl)(ifp, SIOCSIFFLAGS, (caddr_t)&ifr);
3163 /* Notify userland that interface flags have changed */
3168 /* Recover after driver error */
3169 *refcount = oldcount;
3170 ifp->if_flags = oldflags;
3175 * Set/clear promiscuous mode on interface ifp based on the truth value
3176 * of pswitch. The calls are reference counted so that only the first
3177 * "on" request actually has an effect, as does the final "off" request.
3178 * Results are undefined if the "off" and "on" requests are not matched.
3181 ifpromisc(struct ifnet *ifp, int pswitch)
3184 int oldflags = ifp->if_flags;
3186 error = if_setflag(ifp, IFF_PROMISC, IFF_PPROMISC,
3187 &ifp->if_pcount, pswitch);
3188 /* If promiscuous mode status has changed, log a message */
3189 if (error == 0 && ((ifp->if_flags ^ oldflags) & IFF_PROMISC) &&
3190 log_promisc_mode_change)
3191 log(LOG_INFO, "%s: promiscuous mode %s\n",
3193 (ifp->if_flags & IFF_PROMISC) ? "enabled" : "disabled");
3198 * Return interface configuration
3199 * of system. List may be used
3200 * in later ioctl's (above) to get
3201 * other information.
3205 ifconf(u_long cmd, caddr_t data)
3207 struct ifconf *ifc = (struct ifconf *)data;
3212 int error, full = 0, valid_len, max_len;
3214 /* Limit initial buffer size to MAXPHYS to avoid DoS from userspace. */
3215 max_len = MAXPHYS - 1;
3217 /* Prevent hostile input from being able to crash the system */
3218 if (ifc->ifc_len <= 0)
3222 if (ifc->ifc_len <= max_len) {
3223 max_len = ifc->ifc_len;
3226 sb = sbuf_new(NULL, NULL, max_len + 1, SBUF_FIXEDLEN);
3231 TAILQ_FOREACH(ifp, &V_ifnet, if_link) {
3235 * Zero the ifr to make sure we don't disclose the contents
3238 memset(&ifr, 0, sizeof(ifr));
3240 if (strlcpy(ifr.ifr_name, ifp->if_xname, sizeof(ifr.ifr_name))
3241 >= sizeof(ifr.ifr_name)) {
3244 return (ENAMETOOLONG);
3249 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
3250 struct sockaddr *sa = ifa->ifa_addr;
3252 if (prison_if(curthread->td_ucred, sa) != 0)
3256 if (cmd == OSIOCGIFCONF) {
3257 struct osockaddr *osa =
3258 (struct osockaddr *)&ifr.ifr_addr;
3260 osa->sa_family = sa->sa_family;
3261 sbuf_bcat(sb, &ifr, sizeof(ifr));
3262 max_len += sizeof(ifr);
3264 if (sa->sa_len <= sizeof(*sa)) {
3265 if (sa->sa_len < sizeof(*sa)) {
3266 memset(&ifr.ifr_ifru.ifru_addr, 0,
3267 sizeof(ifr.ifr_ifru.ifru_addr));
3268 memcpy(&ifr.ifr_ifru.ifru_addr, sa,
3271 ifr.ifr_ifru.ifru_addr = *sa;
3272 sbuf_bcat(sb, &ifr, sizeof(ifr));
3273 max_len += sizeof(ifr);
3276 offsetof(struct ifreq, ifr_addr));
3277 max_len += offsetof(struct ifreq, ifr_addr);
3278 sbuf_bcat(sb, sa, sa->sa_len);
3279 max_len += sa->sa_len;
3282 if (sbuf_error(sb) == 0)
3283 valid_len = sbuf_len(sb);
3285 IF_ADDR_RUNLOCK(ifp);
3287 sbuf_bcat(sb, &ifr, sizeof(ifr));
3288 max_len += sizeof(ifr);
3290 if (sbuf_error(sb) == 0)
3291 valid_len = sbuf_len(sb);
3297 * If we didn't allocate enough space (uncommon), try again. If
3298 * we have already allocated as much space as we are allowed,
3299 * return what we've got.
3301 if (valid_len != max_len && !full) {
3306 ifc->ifc_len = valid_len;
3308 error = copyout(sbuf_data(sb), ifc->ifc_req, ifc->ifc_len);
3314 * Just like ifpromisc(), but for all-multicast-reception mode.
3317 if_allmulti(struct ifnet *ifp, int onswitch)
3320 return (if_setflag(ifp, IFF_ALLMULTI, 0, &ifp->if_amcount, onswitch));
3323 struct ifmultiaddr *
3324 if_findmulti(struct ifnet *ifp, const struct sockaddr *sa)
3326 struct ifmultiaddr *ifma;
3328 IF_ADDR_LOCK_ASSERT(ifp);
3330 TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
3331 if (sa->sa_family == AF_LINK) {
3332 if (sa_dl_equal(ifma->ifma_addr, sa))
3335 if (sa_equal(ifma->ifma_addr, sa))
3344 * Allocate a new ifmultiaddr and initialize based on passed arguments. We
3345 * make copies of passed sockaddrs. The ifmultiaddr will not be added to
3346 * the ifnet multicast address list here, so the caller must do that and
3347 * other setup work (such as notifying the device driver). The reference
3348 * count is initialized to 1.
3350 static struct ifmultiaddr *
3351 if_allocmulti(struct ifnet *ifp, struct sockaddr *sa, struct sockaddr *llsa,
3354 struct ifmultiaddr *ifma;
3355 struct sockaddr *dupsa;
3357 ifma = malloc(sizeof *ifma, M_IFMADDR, mflags |
3362 dupsa = malloc(sa->sa_len, M_IFMADDR, mflags);
3363 if (dupsa == NULL) {
3364 free(ifma, M_IFMADDR);
3367 bcopy(sa, dupsa, sa->sa_len);
3368 ifma->ifma_addr = dupsa;
3370 ifma->ifma_ifp = ifp;
3371 ifma->ifma_refcount = 1;
3372 ifma->ifma_protospec = NULL;
3375 ifma->ifma_lladdr = NULL;
3379 dupsa = malloc(llsa->sa_len, M_IFMADDR, mflags);
3380 if (dupsa == NULL) {
3381 free(ifma->ifma_addr, M_IFMADDR);
3382 free(ifma, M_IFMADDR);
3385 bcopy(llsa, dupsa, llsa->sa_len);
3386 ifma->ifma_lladdr = dupsa;
3392 * if_freemulti: free ifmultiaddr structure and possibly attached related
3393 * addresses. The caller is responsible for implementing reference
3394 * counting, notifying the driver, handling routing messages, and releasing
3395 * any dependent link layer state.
3398 if_freemulti(struct ifmultiaddr *ifma)
3401 KASSERT(ifma->ifma_refcount == 0, ("if_freemulti: refcount %d",
3402 ifma->ifma_refcount));
3404 if (ifma->ifma_lladdr != NULL)
3405 free(ifma->ifma_lladdr, M_IFMADDR);
3406 free(ifma->ifma_addr, M_IFMADDR);
3407 free(ifma, M_IFMADDR);
3411 * Register an additional multicast address with a network interface.
3413 * - If the address is already present, bump the reference count on the
3414 * address and return.
3415 * - If the address is not link-layer, look up a link layer address.
3416 * - Allocate address structures for one or both addresses, and attach to the
3417 * multicast address list on the interface. If automatically adding a link
3418 * layer address, the protocol address will own a reference to the link
3419 * layer address, to be freed when it is freed.
3420 * - Notify the network device driver of an addition to the multicast address
3423 * 'sa' points to caller-owned memory with the desired multicast address.
3425 * 'retifma' will be used to return a pointer to the resulting multicast
3426 * address reference, if desired.
3429 if_addmulti(struct ifnet *ifp, struct sockaddr *sa,
3430 struct ifmultiaddr **retifma)
3432 struct ifmultiaddr *ifma, *ll_ifma;
3433 struct sockaddr *llsa;
3434 struct sockaddr_dl sdl;
3438 * If the address is already present, return a new reference to it;
3439 * otherwise, allocate storage and set up a new address.
3442 ifma = if_findmulti(ifp, sa);
3444 ifma->ifma_refcount++;
3445 if (retifma != NULL)
3447 IF_ADDR_WUNLOCK(ifp);
3452 * The address isn't already present; resolve the protocol address
3453 * into a link layer address, and then look that up, bump its
3454 * refcount or allocate an ifma for that also.
3455 * Most link layer resolving functions returns address data which
3456 * fits inside default sockaddr_dl structure. However callback
3457 * can allocate another sockaddr structure, in that case we need to
3462 if (ifp->if_resolvemulti != NULL) {
3463 /* Provide called function with buffer size information */
3464 sdl.sdl_len = sizeof(sdl);
3465 llsa = (struct sockaddr *)&sdl;
3466 error = ifp->if_resolvemulti(ifp, &llsa, sa);
3472 * Allocate the new address. Don't hook it up yet, as we may also
3473 * need to allocate a link layer multicast address.
3475 ifma = if_allocmulti(ifp, sa, llsa, M_NOWAIT);
3482 * If a link layer address is found, we'll need to see if it's
3483 * already present in the address list, or allocate is as well.
3484 * When this block finishes, the link layer address will be on the
3488 ll_ifma = if_findmulti(ifp, llsa);
3489 if (ll_ifma == NULL) {
3490 ll_ifma = if_allocmulti(ifp, llsa, NULL, M_NOWAIT);
3491 if (ll_ifma == NULL) {
3492 --ifma->ifma_refcount;
3497 TAILQ_INSERT_HEAD(&ifp->if_multiaddrs, ll_ifma,
3500 ll_ifma->ifma_refcount++;
3501 ifma->ifma_llifma = ll_ifma;
3505 * We now have a new multicast address, ifma, and possibly a new or
3506 * referenced link layer address. Add the primary address to the
3507 * ifnet address list.
3509 TAILQ_INSERT_HEAD(&ifp->if_multiaddrs, ifma, ifma_link);
3511 if (retifma != NULL)
3515 * Must generate the message while holding the lock so that 'ifma'
3516 * pointer is still valid.
3518 rt_newmaddrmsg(RTM_NEWMADDR, ifma);
3519 IF_ADDR_WUNLOCK(ifp);
3522 * We are certain we have added something, so call down to the
3523 * interface to let them know about it.
3525 if (ifp->if_ioctl != NULL) {
3526 (void) (*ifp->if_ioctl)(ifp, SIOCADDMULTI, 0);
3529 if ((llsa != NULL) && (llsa != (struct sockaddr *)&sdl))
3530 link_free_sdl(llsa);
3535 if ((llsa != NULL) && (llsa != (struct sockaddr *)&sdl))
3536 link_free_sdl(llsa);
3539 IF_ADDR_WUNLOCK(ifp);
3544 * Delete a multicast group membership by network-layer group address.
3546 * Returns ENOENT if the entry could not be found. If ifp no longer
3547 * exists, results are undefined. This entry point should only be used
3548 * from subsystems which do appropriate locking to hold ifp for the
3549 * duration of the call.
3550 * Network-layer protocol domains must use if_delmulti_ifma().
3553 if_delmulti(struct ifnet *ifp, struct sockaddr *sa)
3555 struct ifmultiaddr *ifma;
3560 IFNET_RLOCK_NOSLEEP();
3561 TAILQ_FOREACH(oifp, &V_ifnet, if_link)
3566 IFNET_RUNLOCK_NOSLEEP();
3568 KASSERT(ifp != NULL, ("%s: ifnet went away", __func__));
3575 ifma = if_findmulti(ifp, sa);
3577 lastref = if_delmulti_locked(ifp, ifma, 0);
3578 IF_ADDR_WUNLOCK(ifp);
3583 if (lastref && ifp->if_ioctl != NULL) {
3584 (void)(*ifp->if_ioctl)(ifp, SIOCDELMULTI, 0);
3591 * Delete all multicast group membership for an interface.
3592 * Should be used to quickly flush all multicast filters.
3595 if_delallmulti(struct ifnet *ifp)
3597 struct ifmultiaddr *ifma;
3598 struct ifmultiaddr *next;
3601 TAILQ_FOREACH_SAFE(ifma, &ifp->if_multiaddrs, ifma_link, next)
3602 if_delmulti_locked(ifp, ifma, 0);
3603 IF_ADDR_WUNLOCK(ifp);
3607 * Delete a multicast group membership by group membership pointer.
3608 * Network-layer protocol domains must use this routine.
3610 * It is safe to call this routine if the ifp disappeared.
3613 if_delmulti_ifma(struct ifmultiaddr *ifma)
3618 ifp = ifma->ifma_ifp;
3621 printf("%s: ifma_ifp seems to be detached\n", __func__);
3625 IFNET_RLOCK_NOSLEEP();
3626 TAILQ_FOREACH(oifp, &V_ifnet, if_link)
3630 printf("%s: ifnet %p disappeared\n", __func__, ifp);
3633 IFNET_RUNLOCK_NOSLEEP();
3637 * If and only if the ifnet instance exists: Acquire the address lock.
3642 lastref = if_delmulti_locked(ifp, ifma, 0);
3646 * If and only if the ifnet instance exists:
3647 * Release the address lock.
3648 * If the group was left: update the hardware hash filter.
3650 IF_ADDR_WUNLOCK(ifp);
3651 if (lastref && ifp->if_ioctl != NULL) {
3652 (void)(*ifp->if_ioctl)(ifp, SIOCDELMULTI, 0);
3658 * Perform deletion of network-layer and/or link-layer multicast address.
3660 * Return 0 if the reference count was decremented.
3661 * Return 1 if the final reference was released, indicating that the
3662 * hardware hash filter should be reprogrammed.
3665 if_delmulti_locked(struct ifnet *ifp, struct ifmultiaddr *ifma, int detaching)
3667 struct ifmultiaddr *ll_ifma;
3669 if (ifp != NULL && ifma->ifma_ifp != NULL) {
3670 KASSERT(ifma->ifma_ifp == ifp,
3671 ("%s: inconsistent ifp %p", __func__, ifp));
3672 IF_ADDR_WLOCK_ASSERT(ifp);
3675 ifp = ifma->ifma_ifp;
3678 * If the ifnet is detaching, null out references to ifnet,
3679 * so that upper protocol layers will notice, and not attempt
3680 * to obtain locks for an ifnet which no longer exists. The
3681 * routing socket announcement must happen before the ifnet
3682 * instance is detached from the system.
3686 printf("%s: detaching ifnet instance %p\n", __func__, ifp);
3689 * ifp may already be nulled out if we are being reentered
3690 * to delete the ll_ifma.
3693 rt_newmaddrmsg(RTM_DELMADDR, ifma);
3694 ifma->ifma_ifp = NULL;
3698 if (--ifma->ifma_refcount > 0)
3702 * If this ifma is a network-layer ifma, a link-layer ifma may
3703 * have been associated with it. Release it first if so.
3705 ll_ifma = ifma->ifma_llifma;
3706 if (ll_ifma != NULL) {
3707 KASSERT(ifma->ifma_lladdr != NULL,
3708 ("%s: llifma w/o lladdr", __func__));
3710 ll_ifma->ifma_ifp = NULL; /* XXX */
3711 if (--ll_ifma->ifma_refcount == 0) {
3713 TAILQ_REMOVE(&ifp->if_multiaddrs, ll_ifma,
3716 if_freemulti(ll_ifma);
3721 TAILQ_REMOVE(&ifp->if_multiaddrs, ifma, ifma_link);
3726 * The last reference to this instance of struct ifmultiaddr
3727 * was released; the hardware should be notified of this change.
3733 * Set the link layer address on an interface.
3735 * At this time we only support certain types of interfaces,
3736 * and we don't allow the length of the address to change.
3738 * Set noinline to be dtrace-friendly
3741 if_setlladdr(struct ifnet *ifp, const u_char *lladdr, int len)
3743 struct sockaddr_dl *sdl;
3750 IF_ADDR_RUNLOCK(ifp);
3754 IF_ADDR_RUNLOCK(ifp);
3755 sdl = (struct sockaddr_dl *)ifa->ifa_addr;
3760 if (len != sdl->sdl_alen) { /* don't allow length to change */
3764 switch (ifp->if_type) {
3772 case IFT_IEEE8023ADLAG:
3774 bcopy(lladdr, LLADDR(sdl), len);
3783 * If the interface is already up, we need
3784 * to re-init it in order to reprogram its
3787 if ((ifp->if_flags & IFF_UP) != 0) {
3788 if (ifp->if_ioctl) {
3789 ifp->if_flags &= ~IFF_UP;
3790 ifr.ifr_flags = ifp->if_flags & 0xffff;
3791 ifr.ifr_flagshigh = ifp->if_flags >> 16;
3792 (*ifp->if_ioctl)(ifp, SIOCSIFFLAGS, (caddr_t)&ifr);
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);
3799 EVENTHANDLER_INVOKE(iflladdr_event, ifp);
3804 * Compat function for handling basic encapsulation requests.
3805 * Not converted stacks (FDDI, IB, ..) supports traditional
3806 * output model: ARP (and other similar L2 protocols) are handled
3807 * inside output routine, arpresolve/nd6_resolve() returns MAC
3808 * address instead of full prepend.
3810 * This function creates calculated header==MAC for IPv4/IPv6 and
3811 * returns EAFNOSUPPORT (which is then handled in ARP code) for other
3815 if_requestencap_default(struct ifnet *ifp, struct if_encap_req *req)
3818 if (req->rtype != IFENCAP_LL)
3819 return (EOPNOTSUPP);
3821 if (req->bufsize < req->lladdr_len)
3824 switch (req->family) {
3829 return (EAFNOSUPPORT);
3832 /* Copy lladdr to storage as is */
3833 memmove(req->buf, req->lladdr, req->lladdr_len);
3834 req->bufsize = req->lladdr_len;
3835 req->lladdr_off = 0;
3841 * Get the link layer address that was read from the hardware at attach.
3843 * This is only set by Ethernet NICs (IFT_ETHER), but laggX interfaces re-type
3844 * their component interfaces as IFT_IEEE8023ADLAG.
3847 if_gethwaddr(struct ifnet *ifp, struct ifreq *ifr)
3850 if (ifp->if_hw_addr == NULL)
3853 switch (ifp->if_type) {
3855 case IFT_IEEE8023ADLAG:
3856 bcopy(ifp->if_hw_addr, ifr->ifr_addr.sa_data, ifp->if_addrlen);
3864 * The name argument must be a pointer to storage which will last as
3865 * long as the interface does. For physical devices, the result of
3866 * device_get_name(dev) is a good choice and for pseudo-devices a
3867 * static string works well.
3870 if_initname(struct ifnet *ifp, const char *name, int unit)
3872 ifp->if_dname = name;
3873 ifp->if_dunit = unit;
3874 if (unit != IF_DUNIT_NONE)
3875 snprintf(ifp->if_xname, IFNAMSIZ, "%s%d", name, unit);
3877 strlcpy(ifp->if_xname, name, IFNAMSIZ);
3881 if_printf(struct ifnet *ifp, const char * fmt, ...)
3886 retval = printf("%s: ", ifp->if_xname);
3888 retval += vprintf(fmt, ap);
3894 if_start(struct ifnet *ifp)
3897 (*(ifp)->if_start)(ifp);
3901 * Backwards compatibility interface for drivers
3902 * that have not implemented it
3905 if_transmit(struct ifnet *ifp, struct mbuf *m)
3909 IFQ_HANDOFF(ifp, m, error);
3914 if_input_default(struct ifnet *ifp __unused, struct mbuf *m)
3921 if_handoff(struct ifqueue *ifq, struct mbuf *m, struct ifnet *ifp, int adjust)
3926 if (_IF_QFULL(ifq)) {
3928 if_inc_counter(ifp, IFCOUNTER_OQDROPS, 1);
3933 if_inc_counter(ifp, IFCOUNTER_OBYTES, m->m_pkthdr.len + adjust);
3934 if (m->m_flags & (M_BCAST|M_MCAST))
3935 if_inc_counter(ifp, IFCOUNTER_OMCASTS, 1);
3936 active = ifp->if_drv_flags & IFF_DRV_OACTIVE;
3938 _IF_ENQUEUE(ifq, m);
3940 if (ifp != NULL && !active)
3941 (*(ifp)->if_start)(ifp);
3946 if_register_com_alloc(u_char type,
3947 if_com_alloc_t *a, if_com_free_t *f)
3950 KASSERT(if_com_alloc[type] == NULL,
3951 ("if_register_com_alloc: %d already registered", type));
3952 KASSERT(if_com_free[type] == NULL,
3953 ("if_register_com_alloc: %d free already registered", type));
3955 if_com_alloc[type] = a;
3956 if_com_free[type] = f;
3960 if_deregister_com_alloc(u_char type)
3963 KASSERT(if_com_alloc[type] != NULL,
3964 ("if_deregister_com_alloc: %d not registered", type));
3965 KASSERT(if_com_free[type] != NULL,
3966 ("if_deregister_com_alloc: %d free not registered", type));
3967 if_com_alloc[type] = NULL;
3968 if_com_free[type] = NULL;
3971 /* API for driver access to network stack owned ifnet.*/
3973 if_setbaudrate(struct ifnet *ifp, uint64_t baudrate)
3977 oldbrate = ifp->if_baudrate;
3978 ifp->if_baudrate = baudrate;
3983 if_getbaudrate(if_t ifp)
3986 return (((struct ifnet *)ifp)->if_baudrate);
3990 if_setcapabilities(if_t ifp, int capabilities)
3992 ((struct ifnet *)ifp)->if_capabilities = capabilities;
3997 if_setcapabilitiesbit(if_t ifp, int setbit, int clearbit)
3999 ((struct ifnet *)ifp)->if_capabilities |= setbit;
4000 ((struct ifnet *)ifp)->if_capabilities &= ~clearbit;
4006 if_getcapabilities(if_t ifp)
4008 return ((struct ifnet *)ifp)->if_capabilities;
4012 if_setcapenable(if_t ifp, int capabilities)
4014 ((struct ifnet *)ifp)->if_capenable = capabilities;
4019 if_setcapenablebit(if_t ifp, int setcap, int clearcap)
4022 ((struct ifnet *)ifp)->if_capenable |= setcap;
4024 ((struct ifnet *)ifp)->if_capenable &= ~clearcap;
4030 if_getdname(if_t ifp)
4032 return ((struct ifnet *)ifp)->if_dname;
4036 if_togglecapenable(if_t ifp, int togglecap)
4038 ((struct ifnet *)ifp)->if_capenable ^= togglecap;
4043 if_getcapenable(if_t ifp)
4045 return ((struct ifnet *)ifp)->if_capenable;
4049 * This is largely undesirable because it ties ifnet to a device, but does
4050 * provide flexiblity for an embedded product vendor. Should be used with
4051 * the understanding that it violates the interface boundaries, and should be
4052 * a last resort only.
4055 if_setdev(if_t ifp, void *dev)
4061 if_setdrvflagbits(if_t ifp, int set_flags, int clear_flags)
4063 ((struct ifnet *)ifp)->if_drv_flags |= set_flags;
4064 ((struct ifnet *)ifp)->if_drv_flags &= ~clear_flags;
4070 if_getdrvflags(if_t ifp)
4072 return ((struct ifnet *)ifp)->if_drv_flags;
4076 if_setdrvflags(if_t ifp, int flags)
4078 ((struct ifnet *)ifp)->if_drv_flags = flags;
4084 if_setflags(if_t ifp, int flags)
4086 ((struct ifnet *)ifp)->if_flags = flags;
4091 if_setflagbits(if_t ifp, int set, int clear)
4093 ((struct ifnet *)ifp)->if_flags |= set;
4094 ((struct ifnet *)ifp)->if_flags &= ~clear;
4100 if_getflags(if_t ifp)
4102 return ((struct ifnet *)ifp)->if_flags;
4106 if_clearhwassist(if_t ifp)
4108 ((struct ifnet *)ifp)->if_hwassist = 0;
4113 if_sethwassistbits(if_t ifp, int toset, int toclear)
4115 ((struct ifnet *)ifp)->if_hwassist |= toset;
4116 ((struct ifnet *)ifp)->if_hwassist &= ~toclear;
4122 if_sethwassist(if_t ifp, int hwassist_bit)
4124 ((struct ifnet *)ifp)->if_hwassist = hwassist_bit;
4129 if_gethwassist(if_t ifp)
4131 return ((struct ifnet *)ifp)->if_hwassist;
4135 if_setmtu(if_t ifp, int mtu)
4137 ((struct ifnet *)ifp)->if_mtu = mtu;
4144 return ((struct ifnet *)ifp)->if_mtu;
4148 if_getmtu_family(if_t ifp, int family)
4152 for (dp = domains; dp; dp = dp->dom_next) {
4153 if (dp->dom_family == family && dp->dom_ifmtu != NULL)
4154 return (dp->dom_ifmtu((struct ifnet *)ifp));
4157 return (((struct ifnet *)ifp)->if_mtu);
4161 if_setsoftc(if_t ifp, void *softc)
4163 ((struct ifnet *)ifp)->if_softc = softc;
4168 if_getsoftc(if_t ifp)
4170 return ((struct ifnet *)ifp)->if_softc;
4174 if_setrcvif(struct mbuf *m, if_t ifp)
4176 m->m_pkthdr.rcvif = (struct ifnet *)ifp;
4180 if_setvtag(struct mbuf *m, uint16_t tag)
4182 m->m_pkthdr.ether_vtag = tag;
4186 if_getvtag(struct mbuf *m)
4189 return (m->m_pkthdr.ether_vtag);
4193 if_sendq_empty(if_t ifp)
4195 return IFQ_DRV_IS_EMPTY(&((struct ifnet *)ifp)->if_snd);
4199 if_getifaddr(if_t ifp)
4201 return ((struct ifnet *)ifp)->if_addr;
4205 if_getamcount(if_t ifp)
4207 return ((struct ifnet *)ifp)->if_amcount;
4212 if_setsendqready(if_t ifp)
4214 IFQ_SET_READY(&((struct ifnet *)ifp)->if_snd);
4219 if_setsendqlen(if_t ifp, int tx_desc_count)
4221 IFQ_SET_MAXLEN(&((struct ifnet *)ifp)->if_snd, tx_desc_count);
4222 ((struct ifnet *)ifp)->if_snd.ifq_drv_maxlen = tx_desc_count;
4228 if_vlantrunkinuse(if_t ifp)
4230 return ((struct ifnet *)ifp)->if_vlantrunk != NULL?1:0;
4234 if_input(if_t ifp, struct mbuf* sendmp)
4236 (*((struct ifnet *)ifp)->if_input)((struct ifnet *)ifp, sendmp);
4242 #ifndef ETH_ADDR_LEN
4243 #define ETH_ADDR_LEN 6
4247 if_setupmultiaddr(if_t ifp, void *mta, int *cnt, int max)
4249 struct ifmultiaddr *ifma;
4250 uint8_t *lmta = (uint8_t *)mta;
4253 TAILQ_FOREACH(ifma, &((struct ifnet *)ifp)->if_multiaddrs, ifma_link) {
4254 if (ifma->ifma_addr->sa_family != AF_LINK)
4260 bcopy(LLADDR((struct sockaddr_dl *)ifma->ifma_addr),
4261 &lmta[mcnt * ETH_ADDR_LEN], ETH_ADDR_LEN);
4270 if_multiaddr_array(if_t ifp, void *mta, int *cnt, int max)
4274 if_maddr_rlock(ifp);
4275 error = if_setupmultiaddr(ifp, mta, cnt, max);
4276 if_maddr_runlock(ifp);
4281 if_multiaddr_count(if_t ifp, int max)
4283 struct ifmultiaddr *ifma;
4287 if_maddr_rlock(ifp);
4288 TAILQ_FOREACH(ifma, &((struct ifnet *)ifp)->if_multiaddrs, ifma_link) {
4289 if (ifma->ifma_addr->sa_family != AF_LINK)
4295 if_maddr_runlock(ifp);
4300 if_multi_apply(struct ifnet *ifp, int (*filter)(void *, struct ifmultiaddr *, int), void *arg)
4302 struct ifmultiaddr *ifma;
4305 if_maddr_rlock(ifp);
4306 TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link)
4307 cnt += filter(arg, ifma, cnt);
4308 if_maddr_runlock(ifp);
4313 if_dequeue(if_t ifp)
4316 IFQ_DRV_DEQUEUE(&((struct ifnet *)ifp)->if_snd, m);
4322 if_sendq_prepend(if_t ifp, struct mbuf *m)
4324 IFQ_DRV_PREPEND(&((struct ifnet *)ifp)->if_snd, m);
4329 if_setifheaderlen(if_t ifp, int len)
4331 ((struct ifnet *)ifp)->if_hdrlen = len;
4336 if_getlladdr(if_t ifp)
4338 return (IF_LLADDR((struct ifnet *)ifp));
4342 if_gethandle(u_char type)
4344 return (if_alloc(type));
4348 if_bpfmtap(if_t ifh, struct mbuf *m)
4350 struct ifnet *ifp = (struct ifnet *)ifh;
4356 if_etherbpfmtap(if_t ifh, struct mbuf *m)
4358 struct ifnet *ifp = (struct ifnet *)ifh;
4360 ETHER_BPF_MTAP(ifp, m);
4364 if_vlancap(if_t ifh)
4366 struct ifnet *ifp = (struct ifnet *)ifh;
4367 VLAN_CAPABILITIES(ifp);
4371 if_setinitfn(if_t ifp, void (*init_fn)(void *))
4373 ((struct ifnet *)ifp)->if_init = init_fn;
4377 if_setioctlfn(if_t ifp, int (*ioctl_fn)(if_t, u_long, caddr_t))
4379 ((struct ifnet *)ifp)->if_ioctl = (void *)ioctl_fn;
4383 if_setstartfn(if_t ifp, void (*start_fn)(if_t))
4385 ((struct ifnet *)ifp)->if_start = (void *)start_fn;
4389 if_settransmitfn(if_t ifp, if_transmit_fn_t start_fn)
4391 ((struct ifnet *)ifp)->if_transmit = start_fn;
4394 void if_setqflushfn(if_t ifp, if_qflush_fn_t flush_fn)
4396 ((struct ifnet *)ifp)->if_qflush = flush_fn;
4401 if_setgetcounterfn(if_t ifp, if_get_counter_t fn)
4404 ifp->if_get_counter = fn;
4407 /* Revisit these - These are inline functions originally. */
4409 drbr_inuse_drv(if_t ifh, struct buf_ring *br)
4411 return drbr_inuse(ifh, br);
4415 drbr_dequeue_drv(if_t ifh, struct buf_ring *br)
4417 return drbr_dequeue(ifh, br);
4421 drbr_needs_enqueue_drv(if_t ifh, struct buf_ring *br)
4423 return drbr_needs_enqueue(ifh, br);
4427 drbr_enqueue_drv(if_t ifh, struct buf_ring *br, struct mbuf *m)
4429 return drbr_enqueue(ifh, br, m);