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_compat.h"
36 #include "opt_inet6.h"
39 #include <sys/param.h>
40 #include <sys/types.h>
42 #include <sys/malloc.h>
46 #include <sys/systm.h>
49 #include <sys/socket.h>
50 #include <sys/socketvar.h>
51 #include <sys/protosw.h>
52 #include <sys/kernel.h>
54 #include <sys/refcount.h>
55 #include <sys/module.h>
56 #include <sys/rwlock.h>
57 #include <sys/sockio.h>
58 #include <sys/syslog.h>
59 #include <sys/sysctl.h>
60 #include <sys/sysent.h>
61 #include <sys/taskqueue.h>
62 #include <sys/domain.h>
66 #include <machine/stdarg.h>
70 #include <net/ethernet.h>
72 #include <net/if_arp.h>
73 #include <net/if_clone.h>
74 #include <net/if_dl.h>
75 #include <net/if_types.h>
76 #include <net/if_var.h>
77 #include <net/if_media.h>
78 #include <net/if_vlan_var.h>
79 #include <net/radix.h>
80 #include <net/route.h>
83 #if defined(INET) || defined(INET6)
84 #include <net/ethernet.h>
85 #include <netinet/in.h>
86 #include <netinet/in_var.h>
87 #include <netinet/ip.h>
88 #include <netinet/ip_carp.h>
90 #include <netinet/if_ether.h>
93 #include <netinet6/in6_var.h>
94 #include <netinet6/in6_ifattach.h>
96 #endif /* INET || INET6 */
98 #include <security/mac/mac_framework.h>
100 #ifdef COMPAT_FREEBSD32
101 #include <sys/mount.h>
102 #include <compat/freebsd32/freebsd32.h>
104 struct ifreq_buffer32 {
105 uint32_t length; /* (size_t) */
106 uint32_t buffer; /* (void *) */
110 * Interface request structure used for socket
111 * ioctl's. All interface ioctl's must have parameter
112 * definitions which begin with ifr_name. The
113 * remainder may be interface specific.
116 char ifr_name[IFNAMSIZ]; /* if name, e.g. "en0" */
118 struct sockaddr ifru_addr;
119 struct sockaddr ifru_dstaddr;
120 struct sockaddr ifru_broadaddr;
121 struct ifreq_buffer32 ifru_buffer;
132 u_char ifru_vlan_pcp;
135 CTASSERT(sizeof(struct ifreq) == sizeof(struct ifreq32));
136 CTASSERT(__offsetof(struct ifreq, ifr_ifru) ==
137 __offsetof(struct ifreq32, ifr_ifru));
142 #ifdef COMPAT_FREEBSD32
143 struct ifreq32 ifr32;
147 SYSCTL_NODE(_net, PF_LINK, link, CTLFLAG_RW, 0, "Link layers");
148 SYSCTL_NODE(_net_link, 0, generic, CTLFLAG_RW, 0, "Generic link-management");
150 SYSCTL_INT(_net_link, OID_AUTO, ifqmaxlen, CTLFLAG_RDTUN,
151 &ifqmaxlen, 0, "max send queue size");
153 /* Log link state change events */
154 static int log_link_state_change = 1;
156 SYSCTL_INT(_net_link, OID_AUTO, log_link_state_change, CTLFLAG_RW,
157 &log_link_state_change, 0,
158 "log interface link state change events");
160 /* Log promiscuous mode change events */
161 static int log_promisc_mode_change = 1;
163 SYSCTL_INT(_net_link, OID_AUTO, log_promisc_mode_change, CTLFLAG_RDTUN,
164 &log_promisc_mode_change, 1,
165 "log promiscuous mode change events");
167 /* Interface description */
168 static unsigned int ifdescr_maxlen = 1024;
169 SYSCTL_UINT(_net, OID_AUTO, ifdescr_maxlen, CTLFLAG_RW,
171 "administrative maximum length for interface description");
173 static MALLOC_DEFINE(M_IFDESCR, "ifdescr", "ifnet descriptions");
175 /* global sx for non-critical path ifdescr */
176 static struct sx ifdescr_sx;
177 SX_SYSINIT(ifdescr_sx, &ifdescr_sx, "ifnet descr");
179 void (*bridge_linkstate_p)(struct ifnet *ifp);
180 void (*ng_ether_link_state_p)(struct ifnet *ifp, int state);
181 void (*lagg_linkstate_p)(struct ifnet *ifp, int state);
182 /* These are external hooks for CARP. */
183 void (*carp_linkstate_p)(struct ifnet *ifp);
184 void (*carp_demote_adj_p)(int, char *);
185 int (*carp_master_p)(struct ifaddr *);
186 #if defined(INET) || defined(INET6)
187 int (*carp_forus_p)(struct ifnet *ifp, u_char *dhost);
188 int (*carp_output_p)(struct ifnet *ifp, struct mbuf *m,
189 const struct sockaddr *sa);
190 int (*carp_ioctl_p)(struct ifreq *, u_long, struct thread *);
191 int (*carp_attach_p)(struct ifaddr *, int);
192 void (*carp_detach_p)(struct ifaddr *, bool);
195 int (*carp_iamatch_p)(struct ifaddr *, uint8_t **);
198 struct ifaddr *(*carp_iamatch6_p)(struct ifnet *ifp, struct in6_addr *taddr6);
199 caddr_t (*carp_macmatch6_p)(struct ifnet *ifp, struct mbuf *m,
200 const struct in6_addr *taddr);
203 struct mbuf *(*tbr_dequeue_ptr)(struct ifaltq *, int) = NULL;
206 * XXX: Style; these should be sorted alphabetically, and unprototyped
207 * static functions should be prototyped. Currently they are sorted by
210 static void if_attachdomain(void *);
211 static void if_attachdomain1(struct ifnet *);
212 static int ifconf(u_long, caddr_t);
213 static void if_freemulti(struct ifmultiaddr *);
214 static void if_grow(void);
215 static void if_input_default(struct ifnet *, struct mbuf *);
216 static int if_requestencap_default(struct ifnet *, struct if_encap_req *);
217 static void if_route(struct ifnet *, int flag, int fam);
218 static int if_setflag(struct ifnet *, int, int, int *, int);
219 static int if_transmit(struct ifnet *ifp, struct mbuf *m);
220 static void if_unroute(struct ifnet *, int flag, int fam);
221 static void link_rtrequest(int, struct rtentry *, struct rt_addrinfo *);
222 static int ifhwioctl(u_long, struct ifnet *, caddr_t, struct thread *);
223 static int if_delmulti_locked(struct ifnet *, struct ifmultiaddr *, int);
224 static void do_link_state_change(void *, int);
225 static int if_getgroup(struct ifgroupreq *, struct ifnet *);
226 static int if_getgroupmembers(struct ifgroupreq *);
227 static void if_delgroups(struct ifnet *);
228 static void if_attach_internal(struct ifnet *, int, struct if_clone *);
229 static int if_detach_internal(struct ifnet *, int, struct if_clone **);
231 static void if_vmove(struct ifnet *, struct vnet *);
236 * XXX: declare here to avoid to include many inet6 related files..
237 * should be more generalized?
239 extern void nd6_setmtu(struct ifnet *);
242 /* ipsec helper hooks */
243 VNET_DEFINE(struct hhook_head *, ipsec_hhh_in[HHOOK_IPSEC_COUNT]);
244 VNET_DEFINE(struct hhook_head *, ipsec_hhh_out[HHOOK_IPSEC_COUNT]);
246 VNET_DEFINE(int, if_index);
247 int ifqmaxlen = IFQ_MAXLEN;
248 VNET_DEFINE(struct ifnethead, ifnet); /* depend on static init XXX */
249 VNET_DEFINE(struct ifgrouphead, ifg_head);
251 static VNET_DEFINE(int, if_indexlim) = 8;
253 /* Table of ifnet by index. */
254 VNET_DEFINE(struct ifnet **, ifindex_table);
256 #define V_if_indexlim VNET(if_indexlim)
257 #define V_ifindex_table VNET(ifindex_table)
260 * The global network interface list (V_ifnet) and related state (such as
261 * if_index, if_indexlim, and ifindex_table) are protected by an sxlock and
262 * an rwlock. Either may be acquired shared to stablize the list, but both
263 * must be acquired writable to modify the list. This model allows us to
264 * both stablize the interface list during interrupt thread processing, but
265 * also to stablize it over long-running ioctls, without introducing priority
266 * inversions and deadlocks.
268 struct rwlock ifnet_rwlock;
269 RW_SYSINIT_FLAGS(ifnet_rw, &ifnet_rwlock, "ifnet_rw", RW_RECURSE);
270 struct sx ifnet_sxlock;
271 SX_SYSINIT_FLAGS(ifnet_sx, &ifnet_sxlock, "ifnet_sx", SX_RECURSE);
274 * The allocation of network interfaces is a rather non-atomic affair; we
275 * need to select an index before we are ready to expose the interface for
276 * use, so will use this pointer value to indicate reservation.
278 #define IFNET_HOLD (void *)(uintptr_t)(-1)
280 static if_com_alloc_t *if_com_alloc[256];
281 static if_com_free_t *if_com_free[256];
283 static MALLOC_DEFINE(M_IFNET, "ifnet", "interface internals");
284 MALLOC_DEFINE(M_IFADDR, "ifaddr", "interface address");
285 MALLOC_DEFINE(M_IFMADDR, "ether_multi", "link-level multicast address");
288 ifnet_byindex_locked(u_short idx)
291 if (idx > V_if_index)
293 if (V_ifindex_table[idx] == IFNET_HOLD)
295 return (V_ifindex_table[idx]);
299 ifnet_byindex(u_short idx)
303 IFNET_RLOCK_NOSLEEP();
304 ifp = ifnet_byindex_locked(idx);
305 IFNET_RUNLOCK_NOSLEEP();
310 ifnet_byindex_ref(u_short idx)
314 IFNET_RLOCK_NOSLEEP();
315 ifp = ifnet_byindex_locked(idx);
316 if (ifp == NULL || (ifp->if_flags & IFF_DYING)) {
317 IFNET_RUNLOCK_NOSLEEP();
321 IFNET_RUNLOCK_NOSLEEP();
326 * Allocate an ifindex array entry; return 0 on success or an error on
334 IFNET_WLOCK_ASSERT();
337 * Try to find an empty slot below V_if_index. If we fail, take the
340 for (idx = 1; idx <= V_if_index; idx++) {
341 if (V_ifindex_table[idx] == NULL)
345 /* Catch if_index overflow. */
346 if (idx >= V_if_indexlim) {
350 if (idx > V_if_index)
356 ifindex_free_locked(u_short idx)
359 IFNET_WLOCK_ASSERT();
361 V_ifindex_table[idx] = NULL;
362 while (V_if_index > 0 &&
363 V_ifindex_table[V_if_index] == NULL)
368 ifindex_free(u_short idx)
372 ifindex_free_locked(idx);
377 ifnet_setbyindex_locked(u_short idx, struct ifnet *ifp)
380 IFNET_WLOCK_ASSERT();
382 V_ifindex_table[idx] = ifp;
386 ifnet_setbyindex(u_short idx, struct ifnet *ifp)
390 ifnet_setbyindex_locked(idx, ifp);
395 ifaddr_byindex(u_short idx)
398 struct ifaddr *ifa = NULL;
400 IFNET_RLOCK_NOSLEEP();
401 ifp = ifnet_byindex_locked(idx);
402 if (ifp != NULL && (ifa = ifp->if_addr) != NULL)
404 IFNET_RUNLOCK_NOSLEEP();
409 * Network interface utility routines.
411 * Routines with ifa_ifwith* names take sockaddr *'s as
416 vnet_if_init(const void *unused __unused)
419 TAILQ_INIT(&V_ifnet);
420 TAILQ_INIT(&V_ifg_head);
422 if_grow(); /* create initial table */
424 vnet_if_clone_init();
426 VNET_SYSINIT(vnet_if_init, SI_SUB_INIT_IF, SI_ORDER_SECOND, vnet_if_init,
431 vnet_if_uninit(const void *unused __unused)
434 VNET_ASSERT(TAILQ_EMPTY(&V_ifnet), ("%s:%d tailq &V_ifnet=%p "
435 "not empty", __func__, __LINE__, &V_ifnet));
436 VNET_ASSERT(TAILQ_EMPTY(&V_ifg_head), ("%s:%d tailq &V_ifg_head=%p "
437 "not empty", __func__, __LINE__, &V_ifg_head));
439 free((caddr_t)V_ifindex_table, M_IFNET);
441 VNET_SYSUNINIT(vnet_if_uninit, SI_SUB_INIT_IF, SI_ORDER_FIRST,
442 vnet_if_uninit, NULL);
445 vnet_if_return(const void *unused __unused)
447 struct ifnet *ifp, *nifp;
449 /* Return all inherited interfaces to their parent vnets. */
450 TAILQ_FOREACH_SAFE(ifp, &V_ifnet, if_link, nifp) {
451 if (ifp->if_home_vnet != ifp->if_vnet)
452 if_vmove(ifp, ifp->if_home_vnet);
455 VNET_SYSUNINIT(vnet_if_return, SI_SUB_VNET_DONE, SI_ORDER_ANY,
456 vnet_if_return, NULL);
466 IFNET_WLOCK_ASSERT();
467 oldlim = V_if_indexlim;
469 n = (oldlim << 1) * sizeof(*e);
470 e = malloc(n, M_IFNET, M_WAITOK | M_ZERO);
472 if (V_if_indexlim != oldlim) {
476 if (V_ifindex_table != NULL) {
477 memcpy((caddr_t)e, (caddr_t)V_ifindex_table, n/2);
478 free((caddr_t)V_ifindex_table, M_IFNET);
485 * Allocate a struct ifnet and an index for an interface. A layer 2
486 * common structure will also be allocated if an allocation routine is
487 * registered for the passed type.
490 if_alloc(u_char type)
495 ifp = malloc(sizeof(struct ifnet), M_IFNET, M_WAITOK|M_ZERO);
497 idx = ifindex_alloc();
498 ifnet_setbyindex_locked(idx, IFNET_HOLD);
502 ifp->if_alloctype = type;
504 ifp->if_vnet = curvnet;
506 if (if_com_alloc[type] != NULL) {
507 ifp->if_l2com = if_com_alloc[type](type, ifp);
508 if (ifp->if_l2com == NULL) {
515 IF_ADDR_LOCK_INIT(ifp);
516 TASK_INIT(&ifp->if_linktask, 0, do_link_state_change, ifp);
517 ifp->if_afdata_initialized = 0;
518 IF_AFDATA_LOCK_INIT(ifp);
519 TAILQ_INIT(&ifp->if_addrhead);
520 TAILQ_INIT(&ifp->if_multiaddrs);
521 TAILQ_INIT(&ifp->if_groups);
525 ifq_init(&ifp->if_snd, ifp);
527 refcount_init(&ifp->if_refcount, 1); /* Index reference. */
528 for (int i = 0; i < IFCOUNTERS; i++)
529 ifp->if_counters[i] = counter_u64_alloc(M_WAITOK);
530 ifp->if_get_counter = if_get_counter_default;
531 ifp->if_pcp = IFNET_PCP_NONE;
532 ifnet_setbyindex(ifp->if_index, ifp);
537 * Do the actual work of freeing a struct ifnet, and layer 2 common
538 * structure. This call is made when the last reference to an
539 * interface is released.
542 if_free_internal(struct ifnet *ifp)
545 KASSERT((ifp->if_flags & IFF_DYING),
546 ("if_free_internal: interface not dying"));
548 if (if_com_free[ifp->if_alloctype] != NULL)
549 if_com_free[ifp->if_alloctype](ifp->if_l2com,
553 mac_ifnet_destroy(ifp);
555 if (ifp->if_description != NULL)
556 free(ifp->if_description, M_IFDESCR);
557 IF_AFDATA_DESTROY(ifp);
558 IF_ADDR_LOCK_DESTROY(ifp);
559 ifq_delete(&ifp->if_snd);
561 for (int i = 0; i < IFCOUNTERS; i++)
562 counter_u64_free(ifp->if_counters[i]);
568 * Deregister an interface and free the associated storage.
571 if_free(struct ifnet *ifp)
574 ifp->if_flags |= IFF_DYING; /* XXX: Locking */
576 CURVNET_SET_QUIET(ifp->if_vnet);
578 KASSERT(ifp == ifnet_byindex_locked(ifp->if_index),
579 ("%s: freeing unallocated ifnet", ifp->if_xname));
581 ifindex_free_locked(ifp->if_index);
584 if (refcount_release(&ifp->if_refcount))
585 if_free_internal(ifp);
590 * Interfaces to keep an ifnet type-stable despite the possibility of the
591 * driver calling if_free(). If there are additional references, we defer
592 * freeing the underlying data structure.
595 if_ref(struct ifnet *ifp)
598 /* We don't assert the ifnet list lock here, but arguably should. */
599 refcount_acquire(&ifp->if_refcount);
603 if_rele(struct ifnet *ifp)
606 if (!refcount_release(&ifp->if_refcount))
608 if_free_internal(ifp);
612 ifq_init(struct ifaltq *ifq, struct ifnet *ifp)
615 mtx_init(&ifq->ifq_mtx, ifp->if_xname, "if send queue", MTX_DEF);
617 if (ifq->ifq_maxlen == 0)
618 ifq->ifq_maxlen = ifqmaxlen;
621 ifq->altq_disc = NULL;
622 ifq->altq_flags &= ALTQF_CANTCHANGE;
623 ifq->altq_tbr = NULL;
628 ifq_delete(struct ifaltq *ifq)
630 mtx_destroy(&ifq->ifq_mtx);
634 * Perform generic interface initialization tasks and attach the interface
635 * to the list of "active" interfaces. If vmove flag is set on entry
636 * to if_attach_internal(), perform only a limited subset of initialization
637 * tasks, given that we are moving from one vnet to another an ifnet which
638 * has already been fully initialized.
640 * Note that if_detach_internal() removes group membership unconditionally
641 * even when vmove flag is set, and if_attach_internal() adds only IFG_ALL.
642 * Thus, when if_vmove() is applied to a cloned interface, group membership
643 * is lost while a cloned one always joins a group whose name is
644 * ifc->ifc_name. To recover this after if_detach_internal() and
645 * if_attach_internal(), the cloner should be specified to
646 * if_attach_internal() via ifc. If it is non-NULL, if_attach_internal()
647 * attempts to join a group whose name is ifc->ifc_name.
650 * - The decision to return void and thus require this function to
651 * succeed is questionable.
652 * - We should probably do more sanity checking. For instance we don't
653 * do anything to insure if_xname is unique or non-empty.
656 if_attach(struct ifnet *ifp)
659 if_attach_internal(ifp, 0, NULL);
663 * Compute the least common TSO limit.
666 if_hw_tsomax_common(if_t ifp, struct ifnet_hw_tsomax *pmax)
669 * 1) If there is no limit currently, take the limit from
670 * the network adapter.
672 * 2) If the network adapter has a limit below the current
675 if (pmax->tsomaxbytes == 0 || (ifp->if_hw_tsomax != 0 &&
676 ifp->if_hw_tsomax < pmax->tsomaxbytes)) {
677 pmax->tsomaxbytes = ifp->if_hw_tsomax;
679 if (pmax->tsomaxsegcount == 0 || (ifp->if_hw_tsomaxsegcount != 0 &&
680 ifp->if_hw_tsomaxsegcount < pmax->tsomaxsegcount)) {
681 pmax->tsomaxsegcount = ifp->if_hw_tsomaxsegcount;
683 if (pmax->tsomaxsegsize == 0 || (ifp->if_hw_tsomaxsegsize != 0 &&
684 ifp->if_hw_tsomaxsegsize < pmax->tsomaxsegsize)) {
685 pmax->tsomaxsegsize = ifp->if_hw_tsomaxsegsize;
690 * Update TSO limit of a network adapter.
692 * Returns zero if no change. Else non-zero.
695 if_hw_tsomax_update(if_t ifp, struct ifnet_hw_tsomax *pmax)
698 if (ifp->if_hw_tsomax != pmax->tsomaxbytes) {
699 ifp->if_hw_tsomax = pmax->tsomaxbytes;
702 if (ifp->if_hw_tsomaxsegsize != pmax->tsomaxsegsize) {
703 ifp->if_hw_tsomaxsegsize = pmax->tsomaxsegsize;
706 if (ifp->if_hw_tsomaxsegcount != pmax->tsomaxsegcount) {
707 ifp->if_hw_tsomaxsegcount = pmax->tsomaxsegcount;
714 if_attach_internal(struct ifnet *ifp, int vmove, struct if_clone *ifc)
716 unsigned socksize, ifasize;
717 int namelen, masklen;
718 struct sockaddr_dl *sdl;
721 if (ifp->if_index == 0 || ifp != ifnet_byindex(ifp->if_index))
722 panic ("%s: BUG: if_attach called without if_alloc'd input()\n",
726 ifp->if_vnet = curvnet;
727 if (ifp->if_home_vnet == NULL)
728 ifp->if_home_vnet = curvnet;
731 if_addgroup(ifp, IFG_ALL);
733 /* Restore group membership for cloned interfaces. */
734 if (vmove && ifc != NULL)
735 if_clone_addgroup(ifp, ifc);
737 getmicrotime(&ifp->if_lastchange);
738 ifp->if_epoch = time_uptime;
740 KASSERT((ifp->if_transmit == NULL && ifp->if_qflush == NULL) ||
741 (ifp->if_transmit != NULL && ifp->if_qflush != NULL),
742 ("transmit and qflush must both either be set or both be NULL"));
743 if (ifp->if_transmit == NULL) {
744 ifp->if_transmit = if_transmit;
745 ifp->if_qflush = if_qflush;
747 if (ifp->if_input == NULL)
748 ifp->if_input = if_input_default;
750 if (ifp->if_requestencap == NULL)
751 ifp->if_requestencap = if_requestencap_default;
755 mac_ifnet_create(ifp);
759 * Create a Link Level name for this device.
761 namelen = strlen(ifp->if_xname);
763 * Always save enough space for any possiable name so we
764 * can do a rename in place later.
766 masklen = offsetof(struct sockaddr_dl, sdl_data[0]) + IFNAMSIZ;
767 socksize = masklen + ifp->if_addrlen;
768 if (socksize < sizeof(*sdl))
769 socksize = sizeof(*sdl);
770 socksize = roundup2(socksize, sizeof(long));
771 ifasize = sizeof(*ifa) + 2 * socksize;
772 ifa = ifa_alloc(ifasize, M_WAITOK);
773 sdl = (struct sockaddr_dl *)(ifa + 1);
774 sdl->sdl_len = socksize;
775 sdl->sdl_family = AF_LINK;
776 bcopy(ifp->if_xname, sdl->sdl_data, namelen);
777 sdl->sdl_nlen = namelen;
778 sdl->sdl_index = ifp->if_index;
779 sdl->sdl_type = ifp->if_type;
782 ifa->ifa_rtrequest = link_rtrequest;
783 ifa->ifa_addr = (struct sockaddr *)sdl;
784 sdl = (struct sockaddr_dl *)(socksize + (caddr_t)sdl);
785 ifa->ifa_netmask = (struct sockaddr *)sdl;
786 sdl->sdl_len = masklen;
788 sdl->sdl_data[--namelen] = 0xff;
789 TAILQ_INSERT_HEAD(&ifp->if_addrhead, ifa, ifa_link);
790 /* Reliably crash if used uninitialized. */
791 ifp->if_broadcastaddr = NULL;
793 if (ifp->if_type == IFT_ETHER) {
794 ifp->if_hw_addr = malloc(ifp->if_addrlen, M_IFADDR,
798 #if defined(INET) || defined(INET6)
799 /* Use defaults for TSO, if nothing is set */
800 if (ifp->if_hw_tsomax == 0 &&
801 ifp->if_hw_tsomaxsegcount == 0 &&
802 ifp->if_hw_tsomaxsegsize == 0) {
804 * The TSO defaults needs to be such that an
805 * NFS mbuf list of 35 mbufs totalling just
806 * below 64K works and that a chain of mbufs
807 * can be defragged into at most 32 segments:
809 ifp->if_hw_tsomax = min(IP_MAXPACKET, (32 * MCLBYTES) -
810 (ETHER_HDR_LEN + ETHER_VLAN_ENCAP_LEN));
811 ifp->if_hw_tsomaxsegcount = 35;
812 ifp->if_hw_tsomaxsegsize = 2048; /* 2K */
814 /* XXX some drivers set IFCAP_TSO after ethernet attach */
815 if (ifp->if_capabilities & IFCAP_TSO) {
816 if_printf(ifp, "Using defaults for TSO: %u/%u/%u\n",
818 ifp->if_hw_tsomaxsegcount,
819 ifp->if_hw_tsomaxsegsize);
827 * Update the interface index in the link layer address
830 for (ifa = ifp->if_addr; ifa != NULL;
831 ifa = TAILQ_NEXT(ifa, ifa_link)) {
832 if (ifa->ifa_addr->sa_family == AF_LINK) {
833 sdl = (struct sockaddr_dl *)ifa->ifa_addr;
834 sdl->sdl_index = ifp->if_index;
841 TAILQ_INSERT_TAIL(&V_ifnet, ifp, if_link);
843 curvnet->vnet_ifcnt++;
847 if (domain_init_status >= 2)
848 if_attachdomain1(ifp);
850 EVENTHANDLER_INVOKE(ifnet_arrival_event, ifp);
851 if (IS_DEFAULT_VNET(curvnet))
852 devctl_notify("IFNET", ifp->if_xname, "ATTACH", NULL);
854 /* Announce the interface. */
855 rt_ifannouncemsg(ifp, IFAN_ARRIVAL);
859 if_attachdomain(void *dummy)
863 TAILQ_FOREACH(ifp, &V_ifnet, if_link)
864 if_attachdomain1(ifp);
866 SYSINIT(domainifattach, SI_SUB_PROTO_IFATTACHDOMAIN, SI_ORDER_SECOND,
867 if_attachdomain, NULL);
870 if_attachdomain1(struct ifnet *ifp)
875 * Since dp->dom_ifattach calls malloc() with M_WAITOK, we
876 * cannot lock ifp->if_afdata initialization, entirely.
879 if (ifp->if_afdata_initialized >= domain_init_status) {
880 IF_AFDATA_UNLOCK(ifp);
881 log(LOG_WARNING, "%s called more than once on %s\n",
882 __func__, ifp->if_xname);
885 ifp->if_afdata_initialized = domain_init_status;
886 IF_AFDATA_UNLOCK(ifp);
888 /* address family dependent data region */
889 bzero(ifp->if_afdata, sizeof(ifp->if_afdata));
890 for (dp = domains; dp; dp = dp->dom_next) {
891 if (dp->dom_ifattach)
892 ifp->if_afdata[dp->dom_family] =
893 (*dp->dom_ifattach)(ifp);
898 * Remove any unicast or broadcast network addresses from an interface.
901 if_purgeaddrs(struct ifnet *ifp)
903 struct ifaddr *ifa, *next;
905 /* XXX cannot hold IF_ADDR_WLOCK over called functions. */
906 TAILQ_FOREACH_SAFE(ifa, &ifp->if_addrhead, ifa_link, next) {
907 if (ifa->ifa_addr->sa_family == AF_LINK)
910 /* XXX: Ugly!! ad hoc just for INET */
911 if (ifa->ifa_addr->sa_family == AF_INET) {
912 struct ifaliasreq ifr;
914 bzero(&ifr, sizeof(ifr));
915 ifr.ifra_addr = *ifa->ifa_addr;
916 if (ifa->ifa_dstaddr)
917 ifr.ifra_broadaddr = *ifa->ifa_dstaddr;
918 if (in_control(NULL, SIOCDIFADDR, (caddr_t)&ifr, ifp,
924 if (ifa->ifa_addr->sa_family == AF_INET6) {
926 /* ifp_addrhead is already updated */
931 TAILQ_REMOVE(&ifp->if_addrhead, ifa, ifa_link);
932 IF_ADDR_WUNLOCK(ifp);
938 * Remove any multicast network addresses from an interface when an ifnet
942 if_purgemaddrs(struct ifnet *ifp)
944 struct ifmultiaddr *ifma;
945 struct ifmultiaddr *next;
948 TAILQ_FOREACH_SAFE(ifma, &ifp->if_multiaddrs, ifma_link, next)
949 if_delmulti_locked(ifp, ifma, 1);
950 IF_ADDR_WUNLOCK(ifp);
954 * Detach an interface, removing it from the list of "active" interfaces.
955 * If vmove flag is set on entry to if_detach_internal(), perform only a
956 * limited subset of cleanup tasks, given that we are moving an ifnet from
957 * one vnet to another, where it must be fully operational.
959 * XXXRW: There are some significant questions about event ordering, and
960 * how to prevent things from starting to use the interface during detach.
963 if_detach(struct ifnet *ifp)
966 CURVNET_SET_QUIET(ifp->if_vnet);
967 if_detach_internal(ifp, 0, NULL);
972 * The vmove flag, if set, indicates that we are called from a callpath
973 * that is moving an interface to a different vnet instance.
975 * The shutdown flag, if set, indicates that we are called in the
976 * process of shutting down a vnet instance. Currently only the
977 * vnet_if_return SYSUNINIT function sets it. Note: we can be called
978 * on a vnet instance shutdown without this flag being set, e.g., when
979 * the cloned interfaces are destoyed as first thing of teardown.
982 if_detach_internal(struct ifnet *ifp, int vmove, struct if_clone **ifcp)
992 shutdown = (ifp->if_vnet->vnet_state > SI_SUB_VNET &&
993 ifp->if_vnet->vnet_state < SI_SUB_VNET_DONE) ? 1 : 0;
996 TAILQ_FOREACH(iter, &V_ifnet, if_link)
998 TAILQ_REMOVE(&V_ifnet, ifp, if_link);
1005 * While we would want to panic here, we cannot
1006 * guarantee that the interface is indeed still on
1007 * the list given we don't hold locks all the way.
1012 panic("%s: ifp=%p not on the ifnet tailq %p",
1013 __func__, ifp, &V_ifnet);
1015 return; /* XXX this should panic as well? */
1020 * At this point we know the interface still was on the ifnet list
1021 * and we removed it so we are in a stable state.
1024 curvnet->vnet_ifcnt--;
1028 * In any case (destroy or vmove) detach us from the groups
1029 * and remove/wait for pending events on the taskq.
1030 * XXX-BZ in theory an interface could still enqueue a taskq change?
1034 taskqueue_drain(taskqueue_swi, &ifp->if_linktask);
1037 * Check if this is a cloned interface or not. Must do even if
1038 * shutting down as a if_vmove_reclaim() would move the ifp and
1039 * the if_clone_addgroup() will have a corrupted string overwise
1040 * from a gibberish pointer.
1042 if (vmove && ifcp != NULL)
1043 *ifcp = if_clone_findifc(ifp);
1049 * On VNET shutdown abort here as the stack teardown will do all
1050 * the work top-down for us.
1054 * In case of a vmove we are done here without error.
1055 * If we would signal an error it would lead to the same
1056 * abort as if we did not find the ifnet anymore.
1057 * if_detach() calls us in void context and does not care
1058 * about an early abort notification, so life is splendid :)
1060 goto finish_vnet_shutdown;
1065 * At this point we are not tearing down a VNET and are either
1066 * going to destroy or vmove the interface and have to cleanup
1071 * Remove routes and flush queues.
1074 if (ALTQ_IS_ENABLED(&ifp->if_snd))
1075 altq_disable(&ifp->if_snd);
1076 if (ALTQ_IS_ATTACHED(&ifp->if_snd))
1077 altq_detach(&ifp->if_snd);
1088 * Remove all IPv6 kernel structs related to ifp. This should be done
1089 * before removing routing entries below, since IPv6 interface direct
1090 * routes are expected to be removed by the IPv6-specific kernel API.
1091 * Otherwise, the kernel will detect some inconsistency and bark it.
1095 if_purgemaddrs(ifp);
1097 /* Announce that the interface is gone. */
1098 rt_ifannouncemsg(ifp, IFAN_DEPARTURE);
1099 EVENTHANDLER_INVOKE(ifnet_departure_event, ifp);
1100 if (IS_DEFAULT_VNET(curvnet))
1101 devctl_notify("IFNET", ifp->if_xname, "DETACH", NULL);
1105 * Prevent further calls into the device driver via ifnet.
1110 * Remove link ifaddr pointer and maybe decrement if_index.
1111 * Clean up all addresses.
1113 free(ifp->if_hw_addr, M_IFADDR);
1114 ifp->if_hw_addr = NULL;
1115 ifp->if_addr = NULL;
1117 /* We can now free link ifaddr. */
1119 if (!TAILQ_EMPTY(&ifp->if_addrhead)) {
1120 ifa = TAILQ_FIRST(&ifp->if_addrhead);
1121 TAILQ_REMOVE(&ifp->if_addrhead, ifa, ifa_link);
1122 IF_ADDR_WUNLOCK(ifp);
1125 IF_ADDR_WUNLOCK(ifp);
1128 rt_flushifroutes(ifp);
1131 finish_vnet_shutdown:
1134 * We cannot hold the lock over dom_ifdetach calls as they might
1135 * sleep, for example trying to drain a callout, thus open up the
1136 * theoretical race with re-attaching.
1138 IF_AFDATA_LOCK(ifp);
1139 i = ifp->if_afdata_initialized;
1140 ifp->if_afdata_initialized = 0;
1141 IF_AFDATA_UNLOCK(ifp);
1142 for (dp = domains; i > 0 && dp; dp = dp->dom_next) {
1143 if (dp->dom_ifdetach && ifp->if_afdata[dp->dom_family]) {
1144 (*dp->dom_ifdetach)(ifp,
1145 ifp->if_afdata[dp->dom_family]);
1146 ifp->if_afdata[dp->dom_family] = NULL;
1155 * if_vmove() performs a limited version of if_detach() in current
1156 * vnet and if_attach()es the ifnet to the vnet specified as 2nd arg.
1157 * An attempt is made to shrink if_index in current vnet, find an
1158 * unused if_index in target vnet and calls if_grow() if necessary,
1159 * and finally find an unused if_xname for the target vnet.
1162 if_vmove(struct ifnet *ifp, struct vnet *new_vnet)
1164 struct if_clone *ifc;
1165 u_int bif_dlt, bif_hdrlen;
1169 * if_detach_internal() will call the eventhandler to notify
1170 * interface departure. That will detach if_bpf. We need to
1171 * safe the dlt and hdrlen so we can re-attach it later.
1173 bpf_get_bp_params(ifp->if_bpf, &bif_dlt, &bif_hdrlen);
1176 * Detach from current vnet, but preserve LLADDR info, do not
1177 * mark as dead etc. so that the ifnet can be reattached later.
1178 * If we cannot find it, we lost the race to someone else.
1180 rc = if_detach_internal(ifp, 1, &ifc);
1185 * Unlink the ifnet from ifindex_table[] in current vnet, and shrink
1186 * the if_index for that vnet if possible.
1188 * NOTE: IFNET_WLOCK/IFNET_WUNLOCK() are assumed to be unvirtualized,
1189 * or we'd lock on one vnet and unlock on another.
1192 ifindex_free_locked(ifp->if_index);
1196 * Perform interface-specific reassignment tasks, if provided by
1199 if (ifp->if_reassign != NULL)
1200 ifp->if_reassign(ifp, new_vnet, NULL);
1203 * Switch to the context of the target vnet.
1205 CURVNET_SET_QUIET(new_vnet);
1208 ifp->if_index = ifindex_alloc();
1209 ifnet_setbyindex_locked(ifp->if_index, ifp);
1212 if_attach_internal(ifp, 1, ifc);
1214 if (ifp->if_bpf == NULL)
1215 bpfattach(ifp, bif_dlt, bif_hdrlen);
1221 * Move an ifnet to or from another child prison/vnet, specified by the jail id.
1224 if_vmove_loan(struct thread *td, struct ifnet *ifp, char *ifname, int jid)
1230 /* Try to find the prison within our visibility. */
1231 sx_slock(&allprison_lock);
1232 pr = prison_find_child(td->td_ucred->cr_prison, jid);
1233 sx_sunlock(&allprison_lock);
1236 prison_hold_locked(pr);
1237 mtx_unlock(&pr->pr_mtx);
1239 /* Do not try to move the iface from and to the same prison. */
1240 if (pr->pr_vnet == ifp->if_vnet) {
1245 /* Make sure the named iface does not exists in the dst. prison/vnet. */
1246 /* XXX Lock interfaces to avoid races. */
1247 CURVNET_SET_QUIET(pr->pr_vnet);
1248 difp = ifunit(ifname);
1255 /* Make sure the VNET is stable. */
1256 shutdown = (ifp->if_vnet->vnet_state > SI_SUB_VNET &&
1257 ifp->if_vnet->vnet_state < SI_SUB_VNET_DONE) ? 1 : 0;
1265 /* Move the interface into the child jail/vnet. */
1266 if_vmove(ifp, pr->pr_vnet);
1268 /* Report the new if_xname back to the userland. */
1269 sprintf(ifname, "%s", ifp->if_xname);
1276 if_vmove_reclaim(struct thread *td, char *ifname, int jid)
1279 struct vnet *vnet_dst;
1283 /* Try to find the prison within our visibility. */
1284 sx_slock(&allprison_lock);
1285 pr = prison_find_child(td->td_ucred->cr_prison, jid);
1286 sx_sunlock(&allprison_lock);
1289 prison_hold_locked(pr);
1290 mtx_unlock(&pr->pr_mtx);
1292 /* Make sure the named iface exists in the source prison/vnet. */
1293 CURVNET_SET(pr->pr_vnet);
1294 ifp = ifunit(ifname); /* XXX Lock to avoid races. */
1301 /* Do not try to move the iface from and to the same prison. */
1302 vnet_dst = TD_TO_VNET(td);
1303 if (vnet_dst == ifp->if_vnet) {
1309 /* Make sure the VNET is stable. */
1310 shutdown = (ifp->if_vnet->vnet_state > SI_SUB_VNET &&
1311 ifp->if_vnet->vnet_state < SI_SUB_VNET_DONE) ? 1 : 0;
1318 /* Get interface back from child jail/vnet. */
1319 if_vmove(ifp, vnet_dst);
1322 /* Report the new if_xname back to the userland. */
1323 sprintf(ifname, "%s", ifp->if_xname);
1331 * Add a group to an interface
1334 if_addgroup(struct ifnet *ifp, const char *groupname)
1336 struct ifg_list *ifgl;
1337 struct ifg_group *ifg = NULL;
1338 struct ifg_member *ifgm;
1341 if (groupname[0] && groupname[strlen(groupname) - 1] >= '0' &&
1342 groupname[strlen(groupname) - 1] <= '9')
1346 TAILQ_FOREACH(ifgl, &ifp->if_groups, ifgl_next)
1347 if (!strcmp(ifgl->ifgl_group->ifg_group, groupname)) {
1352 if ((ifgl = (struct ifg_list *)malloc(sizeof(struct ifg_list), M_TEMP,
1353 M_NOWAIT)) == NULL) {
1358 if ((ifgm = (struct ifg_member *)malloc(sizeof(struct ifg_member),
1359 M_TEMP, M_NOWAIT)) == NULL) {
1365 TAILQ_FOREACH(ifg, &V_ifg_head, ifg_next)
1366 if (!strcmp(ifg->ifg_group, groupname))
1370 if ((ifg = (struct ifg_group *)malloc(sizeof(struct ifg_group),
1371 M_TEMP, M_NOWAIT)) == NULL) {
1377 strlcpy(ifg->ifg_group, groupname, sizeof(ifg->ifg_group));
1378 ifg->ifg_refcnt = 0;
1379 TAILQ_INIT(&ifg->ifg_members);
1380 TAILQ_INSERT_TAIL(&V_ifg_head, ifg, ifg_next);
1385 ifgl->ifgl_group = ifg;
1386 ifgm->ifgm_ifp = ifp;
1389 TAILQ_INSERT_TAIL(&ifg->ifg_members, ifgm, ifgm_next);
1390 TAILQ_INSERT_TAIL(&ifp->if_groups, ifgl, ifgl_next);
1391 IF_ADDR_WUNLOCK(ifp);
1396 EVENTHANDLER_INVOKE(group_attach_event, ifg);
1397 EVENTHANDLER_INVOKE(group_change_event, groupname);
1403 * Remove a group from an interface
1406 if_delgroup(struct ifnet *ifp, const char *groupname)
1408 struct ifg_list *ifgl;
1409 struct ifg_member *ifgm;
1412 TAILQ_FOREACH(ifgl, &ifp->if_groups, ifgl_next)
1413 if (!strcmp(ifgl->ifgl_group->ifg_group, groupname))
1421 TAILQ_REMOVE(&ifp->if_groups, ifgl, ifgl_next);
1422 IF_ADDR_WUNLOCK(ifp);
1424 TAILQ_FOREACH(ifgm, &ifgl->ifgl_group->ifg_members, ifgm_next)
1425 if (ifgm->ifgm_ifp == ifp)
1429 TAILQ_REMOVE(&ifgl->ifgl_group->ifg_members, ifgm, ifgm_next);
1433 if (--ifgl->ifgl_group->ifg_refcnt == 0) {
1434 TAILQ_REMOVE(&V_ifg_head, ifgl->ifgl_group, ifg_next);
1436 EVENTHANDLER_INVOKE(group_detach_event, ifgl->ifgl_group);
1437 free(ifgl->ifgl_group, M_TEMP);
1443 EVENTHANDLER_INVOKE(group_change_event, groupname);
1449 * Remove an interface from all groups
1452 if_delgroups(struct ifnet *ifp)
1454 struct ifg_list *ifgl;
1455 struct ifg_member *ifgm;
1456 char groupname[IFNAMSIZ];
1459 while (!TAILQ_EMPTY(&ifp->if_groups)) {
1460 ifgl = TAILQ_FIRST(&ifp->if_groups);
1462 strlcpy(groupname, ifgl->ifgl_group->ifg_group, IFNAMSIZ);
1465 TAILQ_REMOVE(&ifp->if_groups, ifgl, ifgl_next);
1466 IF_ADDR_WUNLOCK(ifp);
1468 TAILQ_FOREACH(ifgm, &ifgl->ifgl_group->ifg_members, ifgm_next)
1469 if (ifgm->ifgm_ifp == ifp)
1473 TAILQ_REMOVE(&ifgl->ifgl_group->ifg_members, ifgm,
1478 if (--ifgl->ifgl_group->ifg_refcnt == 0) {
1479 TAILQ_REMOVE(&V_ifg_head, ifgl->ifgl_group, ifg_next);
1481 EVENTHANDLER_INVOKE(group_detach_event,
1483 free(ifgl->ifgl_group, M_TEMP);
1489 EVENTHANDLER_INVOKE(group_change_event, groupname);
1497 * Stores all groups from an interface in memory pointed
1501 if_getgroup(struct ifgroupreq *data, struct ifnet *ifp)
1504 struct ifg_list *ifgl;
1505 struct ifg_req ifgrq, *ifgp;
1506 struct ifgroupreq *ifgr = data;
1508 if (ifgr->ifgr_len == 0) {
1510 TAILQ_FOREACH(ifgl, &ifp->if_groups, ifgl_next)
1511 ifgr->ifgr_len += sizeof(struct ifg_req);
1512 IF_ADDR_RUNLOCK(ifp);
1516 len = ifgr->ifgr_len;
1517 ifgp = ifgr->ifgr_groups;
1520 TAILQ_FOREACH(ifgl, &ifp->if_groups, ifgl_next) {
1521 if (len < sizeof(ifgrq)) {
1522 IF_ADDR_RUNLOCK(ifp);
1525 bzero(&ifgrq, sizeof ifgrq);
1526 strlcpy(ifgrq.ifgrq_group, ifgl->ifgl_group->ifg_group,
1527 sizeof(ifgrq.ifgrq_group));
1528 if ((error = copyout(&ifgrq, ifgp, sizeof(struct ifg_req)))) {
1529 IF_ADDR_RUNLOCK(ifp);
1532 len -= sizeof(ifgrq);
1535 IF_ADDR_RUNLOCK(ifp);
1541 * Stores all members of a group in memory pointed to by data
1544 if_getgroupmembers(struct ifgroupreq *data)
1546 struct ifgroupreq *ifgr = data;
1547 struct ifg_group *ifg;
1548 struct ifg_member *ifgm;
1549 struct ifg_req ifgrq, *ifgp;
1553 TAILQ_FOREACH(ifg, &V_ifg_head, ifg_next)
1554 if (!strcmp(ifg->ifg_group, ifgr->ifgr_name))
1561 if (ifgr->ifgr_len == 0) {
1562 TAILQ_FOREACH(ifgm, &ifg->ifg_members, ifgm_next)
1563 ifgr->ifgr_len += sizeof(ifgrq);
1568 len = ifgr->ifgr_len;
1569 ifgp = ifgr->ifgr_groups;
1570 TAILQ_FOREACH(ifgm, &ifg->ifg_members, ifgm_next) {
1571 if (len < sizeof(ifgrq)) {
1575 bzero(&ifgrq, sizeof ifgrq);
1576 strlcpy(ifgrq.ifgrq_member, ifgm->ifgm_ifp->if_xname,
1577 sizeof(ifgrq.ifgrq_member));
1578 if ((error = copyout(&ifgrq, ifgp, sizeof(struct ifg_req)))) {
1582 len -= sizeof(ifgrq);
1591 * Return counter values from counter(9)s stored in ifnet.
1594 if_get_counter_default(struct ifnet *ifp, ift_counter cnt)
1597 KASSERT(cnt < IFCOUNTERS, ("%s: invalid cnt %d", __func__, cnt));
1599 return (counter_u64_fetch(ifp->if_counters[cnt]));
1603 * Increase an ifnet counter. Usually used for counters shared
1604 * between the stack and a driver, but function supports them all.
1607 if_inc_counter(struct ifnet *ifp, ift_counter cnt, int64_t inc)
1610 KASSERT(cnt < IFCOUNTERS, ("%s: invalid cnt %d", __func__, cnt));
1612 counter_u64_add(ifp->if_counters[cnt], inc);
1616 * Copy data from ifnet to userland API structure if_data.
1619 if_data_copy(struct ifnet *ifp, struct if_data *ifd)
1622 ifd->ifi_type = ifp->if_type;
1623 ifd->ifi_physical = 0;
1624 ifd->ifi_addrlen = ifp->if_addrlen;
1625 ifd->ifi_hdrlen = ifp->if_hdrlen;
1626 ifd->ifi_link_state = ifp->if_link_state;
1628 ifd->ifi_datalen = sizeof(struct if_data);
1629 ifd->ifi_mtu = ifp->if_mtu;
1630 ifd->ifi_metric = ifp->if_metric;
1631 ifd->ifi_baudrate = ifp->if_baudrate;
1632 ifd->ifi_hwassist = ifp->if_hwassist;
1633 ifd->ifi_epoch = ifp->if_epoch;
1634 ifd->ifi_lastchange = ifp->if_lastchange;
1636 ifd->ifi_ipackets = ifp->if_get_counter(ifp, IFCOUNTER_IPACKETS);
1637 ifd->ifi_ierrors = ifp->if_get_counter(ifp, IFCOUNTER_IERRORS);
1638 ifd->ifi_opackets = ifp->if_get_counter(ifp, IFCOUNTER_OPACKETS);
1639 ifd->ifi_oerrors = ifp->if_get_counter(ifp, IFCOUNTER_OERRORS);
1640 ifd->ifi_collisions = ifp->if_get_counter(ifp, IFCOUNTER_COLLISIONS);
1641 ifd->ifi_ibytes = ifp->if_get_counter(ifp, IFCOUNTER_IBYTES);
1642 ifd->ifi_obytes = ifp->if_get_counter(ifp, IFCOUNTER_OBYTES);
1643 ifd->ifi_imcasts = ifp->if_get_counter(ifp, IFCOUNTER_IMCASTS);
1644 ifd->ifi_omcasts = ifp->if_get_counter(ifp, IFCOUNTER_OMCASTS);
1645 ifd->ifi_iqdrops = ifp->if_get_counter(ifp, IFCOUNTER_IQDROPS);
1646 ifd->ifi_oqdrops = ifp->if_get_counter(ifp, IFCOUNTER_OQDROPS);
1647 ifd->ifi_noproto = ifp->if_get_counter(ifp, IFCOUNTER_NOPROTO);
1651 * Wrapper functions for struct ifnet address list locking macros. These are
1652 * used by kernel modules to avoid encoding programming interface or binary
1653 * interface assumptions that may be violated when kernel-internal locking
1654 * approaches change.
1657 if_addr_rlock(struct ifnet *ifp)
1664 if_addr_runlock(struct ifnet *ifp)
1667 IF_ADDR_RUNLOCK(ifp);
1671 if_maddr_rlock(if_t ifp)
1674 IF_ADDR_RLOCK((struct ifnet *)ifp);
1678 if_maddr_runlock(if_t ifp)
1681 IF_ADDR_RUNLOCK((struct ifnet *)ifp);
1685 * Initialization, destruction and refcounting functions for ifaddrs.
1688 ifa_alloc(size_t size, int flags)
1692 KASSERT(size >= sizeof(struct ifaddr),
1693 ("%s: invalid size %zu", __func__, size));
1695 ifa = malloc(size, M_IFADDR, M_ZERO | flags);
1699 if ((ifa->ifa_opackets = counter_u64_alloc(flags)) == NULL)
1701 if ((ifa->ifa_ipackets = counter_u64_alloc(flags)) == NULL)
1703 if ((ifa->ifa_obytes = counter_u64_alloc(flags)) == NULL)
1705 if ((ifa->ifa_ibytes = counter_u64_alloc(flags)) == NULL)
1708 refcount_init(&ifa->ifa_refcnt, 1);
1713 /* free(NULL) is okay */
1714 counter_u64_free(ifa->ifa_opackets);
1715 counter_u64_free(ifa->ifa_ipackets);
1716 counter_u64_free(ifa->ifa_obytes);
1717 counter_u64_free(ifa->ifa_ibytes);
1718 free(ifa, M_IFADDR);
1724 ifa_ref(struct ifaddr *ifa)
1727 refcount_acquire(&ifa->ifa_refcnt);
1731 ifa_free(struct ifaddr *ifa)
1734 if (refcount_release(&ifa->ifa_refcnt)) {
1735 counter_u64_free(ifa->ifa_opackets);
1736 counter_u64_free(ifa->ifa_ipackets);
1737 counter_u64_free(ifa->ifa_obytes);
1738 counter_u64_free(ifa->ifa_ibytes);
1739 free(ifa, M_IFADDR);
1744 ifa_maintain_loopback_route(int cmd, const char *otype, struct ifaddr *ifa,
1745 struct sockaddr *ia)
1748 struct rt_addrinfo info;
1749 struct sockaddr_dl null_sdl;
1754 bzero(&info, sizeof(info));
1755 if (cmd != RTM_DELETE)
1756 info.rti_ifp = V_loif;
1757 info.rti_flags = ifa->ifa_flags | RTF_HOST | RTF_STATIC | RTF_PINNED;
1758 info.rti_info[RTAX_DST] = ia;
1759 info.rti_info[RTAX_GATEWAY] = (struct sockaddr *)&null_sdl;
1760 link_init_sdl(ifp, (struct sockaddr *)&null_sdl, ifp->if_type);
1762 error = rtrequest1_fib(cmd, &info, NULL, ifp->if_fib);
1765 log(LOG_DEBUG, "%s: %s failed for interface %s: %u\n",
1766 __func__, otype, if_name(ifp), error);
1772 ifa_add_loopback_route(struct ifaddr *ifa, struct sockaddr *ia)
1775 return (ifa_maintain_loopback_route(RTM_ADD, "insertion", ifa, ia));
1779 ifa_del_loopback_route(struct ifaddr *ifa, struct sockaddr *ia)
1782 return (ifa_maintain_loopback_route(RTM_DELETE, "deletion", ifa, ia));
1786 ifa_switch_loopback_route(struct ifaddr *ifa, struct sockaddr *ia)
1789 return (ifa_maintain_loopback_route(RTM_CHANGE, "switch", ifa, ia));
1793 * XXX: Because sockaddr_dl has deeper structure than the sockaddr
1794 * structs used to represent other address families, it is necessary
1795 * to perform a different comparison.
1798 #define sa_dl_equal(a1, a2) \
1799 ((((const struct sockaddr_dl *)(a1))->sdl_len == \
1800 ((const struct sockaddr_dl *)(a2))->sdl_len) && \
1801 (bcmp(CLLADDR((const struct sockaddr_dl *)(a1)), \
1802 CLLADDR((const struct sockaddr_dl *)(a2)), \
1803 ((const struct sockaddr_dl *)(a1))->sdl_alen) == 0))
1806 * Locate an interface based on a complete address.
1809 static struct ifaddr *
1810 ifa_ifwithaddr_internal(const struct sockaddr *addr, int getref)
1815 IFNET_RLOCK_NOSLEEP();
1816 TAILQ_FOREACH(ifp, &V_ifnet, if_link) {
1818 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
1819 if (ifa->ifa_addr->sa_family != addr->sa_family)
1821 if (sa_equal(addr, ifa->ifa_addr)) {
1824 IF_ADDR_RUNLOCK(ifp);
1827 /* IP6 doesn't have broadcast */
1828 if ((ifp->if_flags & IFF_BROADCAST) &&
1829 ifa->ifa_broadaddr &&
1830 ifa->ifa_broadaddr->sa_len != 0 &&
1831 sa_equal(ifa->ifa_broadaddr, addr)) {
1834 IF_ADDR_RUNLOCK(ifp);
1838 IF_ADDR_RUNLOCK(ifp);
1842 IFNET_RUNLOCK_NOSLEEP();
1847 ifa_ifwithaddr(const struct sockaddr *addr)
1850 return (ifa_ifwithaddr_internal(addr, 1));
1854 ifa_ifwithaddr_check(const struct sockaddr *addr)
1857 return (ifa_ifwithaddr_internal(addr, 0) != NULL);
1861 * Locate an interface based on the broadcast address.
1865 ifa_ifwithbroadaddr(const struct sockaddr *addr, int fibnum)
1870 IFNET_RLOCK_NOSLEEP();
1871 TAILQ_FOREACH(ifp, &V_ifnet, if_link) {
1872 if ((fibnum != RT_ALL_FIBS) && (ifp->if_fib != fibnum))
1875 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
1876 if (ifa->ifa_addr->sa_family != addr->sa_family)
1878 if ((ifp->if_flags & IFF_BROADCAST) &&
1879 ifa->ifa_broadaddr &&
1880 ifa->ifa_broadaddr->sa_len != 0 &&
1881 sa_equal(ifa->ifa_broadaddr, addr)) {
1883 IF_ADDR_RUNLOCK(ifp);
1887 IF_ADDR_RUNLOCK(ifp);
1891 IFNET_RUNLOCK_NOSLEEP();
1896 * Locate the point to point interface with a given destination address.
1900 ifa_ifwithdstaddr(const struct sockaddr *addr, int fibnum)
1905 IFNET_RLOCK_NOSLEEP();
1906 TAILQ_FOREACH(ifp, &V_ifnet, if_link) {
1907 if ((ifp->if_flags & IFF_POINTOPOINT) == 0)
1909 if ((fibnum != RT_ALL_FIBS) && (ifp->if_fib != fibnum))
1912 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
1913 if (ifa->ifa_addr->sa_family != addr->sa_family)
1915 if (ifa->ifa_dstaddr != NULL &&
1916 sa_equal(addr, ifa->ifa_dstaddr)) {
1918 IF_ADDR_RUNLOCK(ifp);
1922 IF_ADDR_RUNLOCK(ifp);
1926 IFNET_RUNLOCK_NOSLEEP();
1931 * Find an interface on a specific network. If many, choice
1932 * is most specific found.
1935 ifa_ifwithnet(const struct sockaddr *addr, int ignore_ptp, int fibnum)
1939 struct ifaddr *ifa_maybe = NULL;
1940 u_int af = addr->sa_family;
1941 const char *addr_data = addr->sa_data, *cplim;
1944 * AF_LINK addresses can be looked up directly by their index number,
1945 * so do that if we can.
1947 if (af == AF_LINK) {
1948 const struct sockaddr_dl *sdl = (const struct sockaddr_dl *)addr;
1949 if (sdl->sdl_index && sdl->sdl_index <= V_if_index)
1950 return (ifaddr_byindex(sdl->sdl_index));
1954 * Scan though each interface, looking for ones that have addresses
1955 * in this address family and the requested fib. Maintain a reference
1956 * on ifa_maybe once we find one, as we release the IF_ADDR_RLOCK() that
1957 * kept it stable when we move onto the next interface.
1959 IFNET_RLOCK_NOSLEEP();
1960 TAILQ_FOREACH(ifp, &V_ifnet, if_link) {
1961 if ((fibnum != RT_ALL_FIBS) && (ifp->if_fib != fibnum))
1964 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
1965 const char *cp, *cp2, *cp3;
1967 if (ifa->ifa_addr->sa_family != af)
1969 if (af == AF_INET &&
1970 ifp->if_flags & IFF_POINTOPOINT && !ignore_ptp) {
1972 * This is a bit broken as it doesn't
1973 * take into account that the remote end may
1974 * be a single node in the network we are
1976 * The trouble is that we don't know the
1977 * netmask for the remote end.
1979 if (ifa->ifa_dstaddr != NULL &&
1980 sa_equal(addr, ifa->ifa_dstaddr)) {
1982 IF_ADDR_RUNLOCK(ifp);
1987 * Scan all the bits in the ifa's address.
1988 * If a bit dissagrees with what we are
1989 * looking for, mask it with the netmask
1990 * to see if it really matters.
1991 * (A byte at a time)
1993 if (ifa->ifa_netmask == 0)
1996 cp2 = ifa->ifa_addr->sa_data;
1997 cp3 = ifa->ifa_netmask->sa_data;
1998 cplim = ifa->ifa_netmask->sa_len
1999 + (char *)ifa->ifa_netmask;
2001 if ((*cp++ ^ *cp2++) & *cp3++)
2002 goto next; /* next address! */
2004 * If the netmask of what we just found
2005 * is more specific than what we had before
2006 * (if we had one), or if the virtual status
2007 * of new prefix is better than of the old one,
2008 * then remember the new one before continuing
2009 * to search for an even better one.
2011 if (ifa_maybe == NULL ||
2012 ifa_preferred(ifa_maybe, ifa) ||
2013 rn_refines((caddr_t)ifa->ifa_netmask,
2014 (caddr_t)ifa_maybe->ifa_netmask)) {
2015 if (ifa_maybe != NULL)
2016 ifa_free(ifa_maybe);
2022 IF_ADDR_RUNLOCK(ifp);
2027 IFNET_RUNLOCK_NOSLEEP();
2028 if (ifa_maybe != NULL)
2029 ifa_free(ifa_maybe);
2034 * Find an interface address specific to an interface best matching
2038 ifaof_ifpforaddr(const struct sockaddr *addr, struct ifnet *ifp)
2041 const char *cp, *cp2, *cp3;
2043 struct ifaddr *ifa_maybe = NULL;
2044 u_int af = addr->sa_family;
2049 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
2050 if (ifa->ifa_addr->sa_family != af)
2052 if (ifa_maybe == NULL)
2054 if (ifa->ifa_netmask == 0) {
2055 if (sa_equal(addr, ifa->ifa_addr) ||
2056 (ifa->ifa_dstaddr &&
2057 sa_equal(addr, ifa->ifa_dstaddr)))
2061 if (ifp->if_flags & IFF_POINTOPOINT) {
2062 if (sa_equal(addr, ifa->ifa_dstaddr))
2066 cp2 = ifa->ifa_addr->sa_data;
2067 cp3 = ifa->ifa_netmask->sa_data;
2068 cplim = ifa->ifa_netmask->sa_len + (char *)ifa->ifa_netmask;
2069 for (; cp3 < cplim; cp3++)
2070 if ((*cp++ ^ *cp2++) & *cp3)
2080 IF_ADDR_RUNLOCK(ifp);
2085 * See whether new ifa is better than current one:
2086 * 1) A non-virtual one is preferred over virtual.
2087 * 2) A virtual in master state preferred over any other state.
2089 * Used in several address selecting functions.
2092 ifa_preferred(struct ifaddr *cur, struct ifaddr *next)
2095 return (cur->ifa_carp && (!next->ifa_carp ||
2096 ((*carp_master_p)(next) && !(*carp_master_p)(cur))));
2099 #include <net/if_llatbl.h>
2102 * Default action when installing a route with a Link Level gateway.
2103 * Lookup an appropriate real ifa to point to.
2104 * This should be moved to /sys/net/link.c eventually.
2107 link_rtrequest(int cmd, struct rtentry *rt, struct rt_addrinfo *info)
2109 struct ifaddr *ifa, *oifa;
2110 struct sockaddr *dst;
2113 if (cmd != RTM_ADD || ((ifa = rt->rt_ifa) == NULL) ||
2114 ((ifp = ifa->ifa_ifp) == NULL) || ((dst = rt_key(rt)) == NULL))
2116 ifa = ifaof_ifpforaddr(dst, ifp);
2121 if (ifa->ifa_rtrequest && ifa->ifa_rtrequest != link_rtrequest)
2122 ifa->ifa_rtrequest(cmd, rt, info);
2126 struct sockaddr_dl *
2127 link_alloc_sdl(size_t size, int flags)
2130 return (malloc(size, M_TEMP, flags));
2134 link_free_sdl(struct sockaddr *sa)
2140 * Fills in given sdl with interface basic info.
2141 * Returns pointer to filled sdl.
2143 struct sockaddr_dl *
2144 link_init_sdl(struct ifnet *ifp, struct sockaddr *paddr, u_char iftype)
2146 struct sockaddr_dl *sdl;
2148 sdl = (struct sockaddr_dl *)paddr;
2149 memset(sdl, 0, sizeof(struct sockaddr_dl));
2150 sdl->sdl_len = sizeof(struct sockaddr_dl);
2151 sdl->sdl_family = AF_LINK;
2152 sdl->sdl_index = ifp->if_index;
2153 sdl->sdl_type = iftype;
2159 * Mark an interface down and notify protocols of
2163 if_unroute(struct ifnet *ifp, int flag, int fam)
2167 KASSERT(flag == IFF_UP, ("if_unroute: flag != IFF_UP"));
2169 ifp->if_flags &= ~flag;
2170 getmicrotime(&ifp->if_lastchange);
2171 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link)
2172 if (fam == PF_UNSPEC || (fam == ifa->ifa_addr->sa_family))
2173 pfctlinput(PRC_IFDOWN, ifa->ifa_addr);
2174 ifp->if_qflush(ifp);
2177 (*carp_linkstate_p)(ifp);
2182 * Mark an interface up and notify protocols of
2186 if_route(struct ifnet *ifp, int flag, int fam)
2190 KASSERT(flag == IFF_UP, ("if_route: flag != IFF_UP"));
2192 ifp->if_flags |= flag;
2193 getmicrotime(&ifp->if_lastchange);
2194 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link)
2195 if (fam == PF_UNSPEC || (fam == ifa->ifa_addr->sa_family))
2196 pfctlinput(PRC_IFUP, ifa->ifa_addr);
2198 (*carp_linkstate_p)(ifp);
2205 void (*vlan_link_state_p)(struct ifnet *); /* XXX: private from if_vlan */
2206 void (*vlan_trunk_cap_p)(struct ifnet *); /* XXX: private from if_vlan */
2207 struct ifnet *(*vlan_trunkdev_p)(struct ifnet *);
2208 struct ifnet *(*vlan_devat_p)(struct ifnet *, uint16_t);
2209 int (*vlan_tag_p)(struct ifnet *, uint16_t *);
2210 int (*vlan_setcookie_p)(struct ifnet *, void *);
2211 void *(*vlan_cookie_p)(struct ifnet *);
2214 * Handle a change in the interface link state. To avoid LORs
2215 * between driver lock and upper layer locks, as well as possible
2216 * recursions, we post event to taskqueue, and all job
2217 * is done in static do_link_state_change().
2220 if_link_state_change(struct ifnet *ifp, int link_state)
2222 /* Return if state hasn't changed. */
2223 if (ifp->if_link_state == link_state)
2226 ifp->if_link_state = link_state;
2228 taskqueue_enqueue(taskqueue_swi, &ifp->if_linktask);
2232 do_link_state_change(void *arg, int pending)
2234 struct ifnet *ifp = (struct ifnet *)arg;
2235 int link_state = ifp->if_link_state;
2236 CURVNET_SET(ifp->if_vnet);
2238 /* Notify that the link state has changed. */
2240 if (ifp->if_vlantrunk != NULL)
2241 (*vlan_link_state_p)(ifp);
2243 if ((ifp->if_type == IFT_ETHER || ifp->if_type == IFT_L2VLAN) &&
2244 ifp->if_l2com != NULL)
2245 (*ng_ether_link_state_p)(ifp, link_state);
2247 (*carp_linkstate_p)(ifp);
2249 (*bridge_linkstate_p)(ifp);
2251 (*lagg_linkstate_p)(ifp, link_state);
2253 if (IS_DEFAULT_VNET(curvnet))
2254 devctl_notify("IFNET", ifp->if_xname,
2255 (link_state == LINK_STATE_UP) ? "LINK_UP" : "LINK_DOWN",
2258 if_printf(ifp, "%d link states coalesced\n", pending);
2259 if (log_link_state_change)
2260 log(LOG_NOTICE, "%s: link state changed to %s\n", ifp->if_xname,
2261 (link_state == LINK_STATE_UP) ? "UP" : "DOWN" );
2262 EVENTHANDLER_INVOKE(ifnet_link_event, ifp, link_state);
2267 * Mark an interface down and notify protocols of
2271 if_down(struct ifnet *ifp)
2274 EVENTHANDLER_INVOKE(ifnet_event, ifp, IFNET_EVENT_DOWN);
2275 if_unroute(ifp, IFF_UP, AF_UNSPEC);
2279 * Mark an interface up and notify protocols of
2283 if_up(struct ifnet *ifp)
2286 if_route(ifp, IFF_UP, AF_UNSPEC);
2287 EVENTHANDLER_INVOKE(ifnet_event, ifp, IFNET_EVENT_UP);
2291 * Flush an interface queue.
2294 if_qflush(struct ifnet *ifp)
2302 if (ALTQ_IS_ENABLED(ifq))
2306 while ((m = n) != NULL) {
2317 * Map interface name to interface structure pointer, with or without
2318 * returning a reference.
2321 ifunit_ref(const char *name)
2325 IFNET_RLOCK_NOSLEEP();
2326 TAILQ_FOREACH(ifp, &V_ifnet, if_link) {
2327 if (strncmp(name, ifp->if_xname, IFNAMSIZ) == 0 &&
2328 !(ifp->if_flags & IFF_DYING))
2333 IFNET_RUNLOCK_NOSLEEP();
2338 ifunit(const char *name)
2342 IFNET_RLOCK_NOSLEEP();
2343 TAILQ_FOREACH(ifp, &V_ifnet, if_link) {
2344 if (strncmp(name, ifp->if_xname, IFNAMSIZ) == 0)
2347 IFNET_RUNLOCK_NOSLEEP();
2352 ifr_buffer_get_buffer(struct thread *td, void *data)
2354 union ifreq_union *ifrup;
2357 #ifdef COMPAT_FREEBSD32
2358 if (SV_PROC_FLAG(td->td_proc, SV_ILP32))
2359 return ((void *)(uintptr_t)
2360 ifrup->ifr32.ifr_ifru.ifru_buffer.buffer);
2362 return (ifrup->ifr.ifr_ifru.ifru_buffer.buffer);
2366 ifr_buffer_set_buffer_null(struct thread *td, void *data)
2368 union ifreq_union *ifrup;
2371 #ifdef COMPAT_FREEBSD32
2372 if (SV_PROC_FLAG(td->td_proc, SV_ILP32))
2373 ifrup->ifr32.ifr_ifru.ifru_buffer.buffer = 0;
2376 ifrup->ifr.ifr_ifru.ifru_buffer.buffer = NULL;
2380 ifr_buffer_get_length(struct thread *td, void *data)
2382 union ifreq_union *ifrup;
2385 #ifdef COMPAT_FREEBSD32
2386 if (SV_PROC_FLAG(td->td_proc, SV_ILP32))
2387 return (ifrup->ifr32.ifr_ifru.ifru_buffer.length);
2389 return (ifrup->ifr.ifr_ifru.ifru_buffer.length);
2393 ifr_buffer_set_length(struct thread *td, void *data, size_t len)
2395 union ifreq_union *ifrup;
2398 #ifdef COMPAT_FREEBSD32
2399 if (SV_PROC_FLAG(td->td_proc, SV_ILP32))
2400 ifrup->ifr32.ifr_ifru.ifru_buffer.length = len;
2403 ifrup->ifr.ifr_ifru.ifru_buffer.length = len;
2407 * Hardware specific interface ioctls.
2410 ifhwioctl(u_long cmd, struct ifnet *ifp, caddr_t data, struct thread *td)
2413 int error = 0, do_ifup = 0;
2414 int new_flags, temp_flags;
2415 size_t namelen, onamelen;
2417 char *descrbuf, *odescrbuf;
2418 char new_name[IFNAMSIZ];
2420 struct sockaddr_dl *sdl;
2422 ifr = (struct ifreq *)data;
2425 ifr->ifr_index = ifp->if_index;
2429 temp_flags = ifp->if_flags | ifp->if_drv_flags;
2430 ifr->ifr_flags = temp_flags & 0xffff;
2431 ifr->ifr_flagshigh = temp_flags >> 16;
2435 ifr->ifr_reqcap = ifp->if_capabilities;
2436 ifr->ifr_curcap = ifp->if_capenable;
2441 error = mac_ifnet_ioctl_get(td->td_ucred, ifr, ifp);
2446 ifr->ifr_metric = ifp->if_metric;
2450 ifr->ifr_mtu = ifp->if_mtu;
2454 /* XXXGL: did this ever worked? */
2460 sx_slock(&ifdescr_sx);
2461 if (ifp->if_description == NULL)
2464 /* space for terminating nul */
2465 descrlen = strlen(ifp->if_description) + 1;
2466 if (ifr_buffer_get_length(td, ifr) < descrlen)
2467 ifr_buffer_set_buffer_null(td, ifr);
2469 error = copyout(ifp->if_description,
2470 ifr_buffer_get_buffer(td, ifr), descrlen);
2471 ifr_buffer_set_length(td, ifr, descrlen);
2473 sx_sunlock(&ifdescr_sx);
2477 error = priv_check(td, PRIV_NET_SETIFDESCR);
2482 * Copy only (length-1) bytes to make sure that
2483 * if_description is always nul terminated. The
2484 * length parameter is supposed to count the
2485 * terminating nul in.
2487 if (ifr_buffer_get_length(td, ifr) > ifdescr_maxlen)
2488 return (ENAMETOOLONG);
2489 else if (ifr_buffer_get_length(td, ifr) == 0)
2492 descrbuf = malloc(ifr_buffer_get_length(td, ifr),
2493 M_IFDESCR, M_WAITOK | M_ZERO);
2494 error = copyin(ifr_buffer_get_buffer(td, ifr), descrbuf,
2495 ifr_buffer_get_length(td, ifr) - 1);
2497 free(descrbuf, M_IFDESCR);
2502 sx_xlock(&ifdescr_sx);
2503 odescrbuf = ifp->if_description;
2504 ifp->if_description = descrbuf;
2505 sx_xunlock(&ifdescr_sx);
2507 getmicrotime(&ifp->if_lastchange);
2508 free(odescrbuf, M_IFDESCR);
2512 ifr->ifr_fib = ifp->if_fib;
2516 error = priv_check(td, PRIV_NET_SETIFFIB);
2519 if (ifr->ifr_fib >= rt_numfibs)
2522 ifp->if_fib = ifr->ifr_fib;
2526 error = priv_check(td, PRIV_NET_SETIFFLAGS);
2530 * Currently, no driver owned flags pass the IFF_CANTCHANGE
2531 * check, so we don't need special handling here yet.
2533 new_flags = (ifr->ifr_flags & 0xffff) |
2534 (ifr->ifr_flagshigh << 16);
2535 if (ifp->if_flags & IFF_UP &&
2536 (new_flags & IFF_UP) == 0) {
2538 } else if (new_flags & IFF_UP &&
2539 (ifp->if_flags & IFF_UP) == 0) {
2542 /* See if permanently promiscuous mode bit is about to flip */
2543 if ((ifp->if_flags ^ new_flags) & IFF_PPROMISC) {
2544 if (new_flags & IFF_PPROMISC)
2545 ifp->if_flags |= IFF_PROMISC;
2546 else if (ifp->if_pcount == 0)
2547 ifp->if_flags &= ~IFF_PROMISC;
2548 if (log_promisc_mode_change)
2549 log(LOG_INFO, "%s: permanently promiscuous mode %s\n",
2551 ((new_flags & IFF_PPROMISC) ?
2552 "enabled" : "disabled"));
2554 ifp->if_flags = (ifp->if_flags & IFF_CANTCHANGE) |
2555 (new_flags &~ IFF_CANTCHANGE);
2556 if (ifp->if_ioctl) {
2557 (void) (*ifp->if_ioctl)(ifp, cmd, data);
2561 getmicrotime(&ifp->if_lastchange);
2565 error = priv_check(td, PRIV_NET_SETIFCAP);
2568 if (ifp->if_ioctl == NULL)
2569 return (EOPNOTSUPP);
2570 if (ifr->ifr_reqcap & ~ifp->if_capabilities)
2572 error = (*ifp->if_ioctl)(ifp, cmd, data);
2574 getmicrotime(&ifp->if_lastchange);
2579 error = mac_ifnet_ioctl_set(td->td_ucred, ifr, ifp);
2584 error = priv_check(td, PRIV_NET_SETIFNAME);
2587 error = copyinstr(ifr->ifr_data, new_name, IFNAMSIZ, NULL);
2590 if (new_name[0] == '\0')
2592 if (new_name[IFNAMSIZ-1] != '\0') {
2593 new_name[IFNAMSIZ-1] = '\0';
2594 if (strlen(new_name) == IFNAMSIZ-1)
2597 if (ifunit(new_name) != NULL)
2601 * XXX: Locking. Nothing else seems to lock if_flags,
2602 * and there are numerous other races with the
2603 * ifunit() checks not being atomic with namespace
2604 * changes (renames, vmoves, if_attach, etc).
2606 ifp->if_flags |= IFF_RENAMING;
2608 /* Announce the departure of the interface. */
2609 rt_ifannouncemsg(ifp, IFAN_DEPARTURE);
2610 EVENTHANDLER_INVOKE(ifnet_departure_event, ifp);
2612 log(LOG_INFO, "%s: changing name to '%s'\n",
2613 ifp->if_xname, new_name);
2616 strlcpy(ifp->if_xname, new_name, sizeof(ifp->if_xname));
2618 sdl = (struct sockaddr_dl *)ifa->ifa_addr;
2619 namelen = strlen(new_name);
2620 onamelen = sdl->sdl_nlen;
2622 * Move the address if needed. This is safe because we
2623 * allocate space for a name of length IFNAMSIZ when we
2624 * create this in if_attach().
2626 if (namelen != onamelen) {
2627 bcopy(sdl->sdl_data + onamelen,
2628 sdl->sdl_data + namelen, sdl->sdl_alen);
2630 bcopy(new_name, sdl->sdl_data, namelen);
2631 sdl->sdl_nlen = namelen;
2632 sdl = (struct sockaddr_dl *)ifa->ifa_netmask;
2633 bzero(sdl->sdl_data, onamelen);
2634 while (namelen != 0)
2635 sdl->sdl_data[--namelen] = 0xff;
2636 IF_ADDR_WUNLOCK(ifp);
2638 EVENTHANDLER_INVOKE(ifnet_arrival_event, ifp);
2639 /* Announce the return of the interface. */
2640 rt_ifannouncemsg(ifp, IFAN_ARRIVAL);
2642 ifp->if_flags &= ~IFF_RENAMING;
2647 error = priv_check(td, PRIV_NET_SETIFVNET);
2650 error = if_vmove_loan(td, ifp, ifr->ifr_name, ifr->ifr_jid);
2655 error = priv_check(td, PRIV_NET_SETIFMETRIC);
2658 ifp->if_metric = ifr->ifr_metric;
2659 getmicrotime(&ifp->if_lastchange);
2663 error = priv_check(td, PRIV_NET_SETIFPHYS);
2666 if (ifp->if_ioctl == NULL)
2667 return (EOPNOTSUPP);
2668 error = (*ifp->if_ioctl)(ifp, cmd, data);
2670 getmicrotime(&ifp->if_lastchange);
2675 u_long oldmtu = ifp->if_mtu;
2677 error = priv_check(td, PRIV_NET_SETIFMTU);
2680 if (ifr->ifr_mtu < IF_MINMTU || ifr->ifr_mtu > IF_MAXMTU)
2682 if (ifp->if_ioctl == NULL)
2683 return (EOPNOTSUPP);
2684 error = (*ifp->if_ioctl)(ifp, cmd, data);
2686 getmicrotime(&ifp->if_lastchange);
2690 * If the link MTU changed, do network layer specific procedure.
2692 if (ifp->if_mtu != oldmtu) {
2703 if (cmd == SIOCADDMULTI)
2704 error = priv_check(td, PRIV_NET_ADDMULTI);
2706 error = priv_check(td, PRIV_NET_DELMULTI);
2710 /* Don't allow group membership on non-multicast interfaces. */
2711 if ((ifp->if_flags & IFF_MULTICAST) == 0)
2712 return (EOPNOTSUPP);
2714 /* Don't let users screw up protocols' entries. */
2715 if (ifr->ifr_addr.sa_family != AF_LINK)
2718 if (cmd == SIOCADDMULTI) {
2719 struct ifmultiaddr *ifma;
2722 * Userland is only permitted to join groups once
2723 * via the if_addmulti() KPI, because it cannot hold
2724 * struct ifmultiaddr * between calls. It may also
2725 * lose a race while we check if the membership
2729 ifma = if_findmulti(ifp, &ifr->ifr_addr);
2730 IF_ADDR_RUNLOCK(ifp);
2734 error = if_addmulti(ifp, &ifr->ifr_addr, &ifma);
2736 error = if_delmulti(ifp, &ifr->ifr_addr);
2739 getmicrotime(&ifp->if_lastchange);
2742 case SIOCSIFPHYADDR:
2743 case SIOCDIFPHYADDR:
2745 case SIOCSIFPHYADDR_IN6:
2748 case SIOCSIFGENERIC:
2749 error = priv_check(td, PRIV_NET_HWIOCTL);
2752 if (ifp->if_ioctl == NULL)
2753 return (EOPNOTSUPP);
2754 error = (*ifp->if_ioctl)(ifp, cmd, data);
2756 getmicrotime(&ifp->if_lastchange);
2760 case SIOCGIFPSRCADDR:
2761 case SIOCGIFPDSTADDR:
2764 case SIOCGIFGENERIC:
2766 case SIOCGIFRSSHASH:
2767 if (ifp->if_ioctl == NULL)
2768 return (EOPNOTSUPP);
2769 error = (*ifp->if_ioctl)(ifp, cmd, data);
2773 error = priv_check(td, PRIV_NET_SETLLADDR);
2776 error = if_setlladdr(ifp,
2777 ifr->ifr_addr.sa_data, ifr->ifr_addr.sa_len);
2781 error = if_gethwaddr(ifp, ifr);
2786 struct ifgroupreq *ifgr = (struct ifgroupreq *)ifr;
2788 error = priv_check(td, PRIV_NET_ADDIFGROUP);
2791 if ((error = if_addgroup(ifp, ifgr->ifgr_group)))
2797 if ((error = if_getgroup((struct ifgroupreq *)ifr, ifp)))
2803 struct ifgroupreq *ifgr = (struct ifgroupreq *)ifr;
2805 error = priv_check(td, PRIV_NET_DELIFGROUP);
2808 if ((error = if_delgroup(ifp, ifgr->ifgr_group)))
2820 #ifdef COMPAT_FREEBSD32
2828 #define SIOCGIFCONF32 _IOWR('i', 36, struct ifconf32)
2835 ifioctl(struct socket *so, u_long cmd, caddr_t data, struct thread *td)
2845 CURVNET_SET(so->so_vnet);
2847 /* Make sure the VNET is stable. */
2848 shutdown = (so->so_vnet->vnet_state > SI_SUB_VNET &&
2849 so->so_vnet->vnet_state < SI_SUB_VNET_DONE) ? 1 : 0;
2859 error = ifconf(cmd, data);
2863 #ifdef COMPAT_FREEBSD32
2866 struct ifconf32 *ifc32;
2869 ifc32 = (struct ifconf32 *)data;
2870 ifc.ifc_len = ifc32->ifc_len;
2871 ifc.ifc_buf = PTRIN(ifc32->ifc_buf);
2873 error = ifconf(SIOCGIFCONF, (void *)&ifc);
2876 ifc32->ifc_len = ifc.ifc_len;
2881 ifr = (struct ifreq *)data;
2886 error = priv_check(td, PRIV_NET_SETIFVNET);
2888 error = if_vmove_reclaim(td, ifr->ifr_name,
2895 error = priv_check(td, PRIV_NET_IFCREATE);
2897 error = if_clone_create(ifr->ifr_name,
2898 sizeof(ifr->ifr_name),
2899 cmd == SIOCIFCREATE2 ? ifr->ifr_data : NULL);
2903 error = priv_check(td, PRIV_NET_IFDESTROY);
2905 error = if_clone_destroy(ifr->ifr_name);
2909 case SIOCIFGCLONERS:
2910 error = if_clone_list((struct if_clonereq *)data);
2914 error = if_getgroupmembers((struct ifgroupreq *)data);
2917 #if defined(INET) || defined(INET6)
2920 if (carp_ioctl_p == NULL)
2921 error = EPROTONOSUPPORT;
2923 error = (*carp_ioctl_p)(ifr, cmd, td);
2929 ifp = ifunit_ref(ifr->ifr_name);
2935 error = ifhwioctl(cmd, ifp, data, td);
2936 if (error != ENOIOCTL) {
2942 oif_flags = ifp->if_flags;
2943 if (so->so_proto == NULL) {
2946 return (EOPNOTSUPP);
2950 * Pass the request on to the socket control method, and if the
2951 * latter returns EOPNOTSUPP, directly to the interface.
2953 * Make an exception for the legacy SIOCSIF* requests. Drivers
2954 * trust SIOCSIFADDR et al to come from an already privileged
2955 * layer, and do not perform any credentials checks or input
2958 error = ((*so->so_proto->pr_usrreqs->pru_control)(so, cmd, data,
2960 if (error == EOPNOTSUPP && ifp != NULL && ifp->if_ioctl != NULL &&
2961 cmd != SIOCSIFADDR && cmd != SIOCSIFBRDADDR &&
2962 cmd != SIOCSIFDSTADDR && cmd != SIOCSIFNETMASK)
2963 error = (*ifp->if_ioctl)(ifp, cmd, data);
2965 if ((oif_flags ^ ifp->if_flags) & IFF_UP) {
2967 if (ifp->if_flags & IFF_UP)
2977 * The code common to handling reference counted flags,
2978 * e.g., in ifpromisc() and if_allmulti().
2979 * The "pflag" argument can specify a permanent mode flag to check,
2980 * such as IFF_PPROMISC for promiscuous mode; should be 0 if none.
2982 * Only to be used on stack-owned flags, not driver-owned flags.
2985 if_setflag(struct ifnet *ifp, int flag, int pflag, int *refcount, int onswitch)
2989 int oldflags, oldcount;
2991 /* Sanity checks to catch programming errors */
2992 KASSERT((flag & (IFF_DRV_OACTIVE|IFF_DRV_RUNNING)) == 0,
2993 ("%s: setting driver-owned flag %d", __func__, flag));
2996 KASSERT(*refcount >= 0,
2997 ("%s: increment negative refcount %d for flag %d",
2998 __func__, *refcount, flag));
3000 KASSERT(*refcount > 0,
3001 ("%s: decrement non-positive refcount %d for flag %d",
3002 __func__, *refcount, flag));
3004 /* In case this mode is permanent, just touch refcount */
3005 if (ifp->if_flags & pflag) {
3006 *refcount += onswitch ? 1 : -1;
3010 /* Save ifnet parameters for if_ioctl() may fail */
3011 oldcount = *refcount;
3012 oldflags = ifp->if_flags;
3015 * See if we aren't the only and touching refcount is enough.
3016 * Actually toggle interface flag if we are the first or last.
3021 ifp->if_flags |= flag;
3025 ifp->if_flags &= ~flag;
3028 /* Call down the driver since we've changed interface flags */
3029 if (ifp->if_ioctl == NULL) {
3033 ifr.ifr_flags = ifp->if_flags & 0xffff;
3034 ifr.ifr_flagshigh = ifp->if_flags >> 16;
3035 error = (*ifp->if_ioctl)(ifp, SIOCSIFFLAGS, (caddr_t)&ifr);
3038 /* Notify userland that interface flags have changed */
3043 /* Recover after driver error */
3044 *refcount = oldcount;
3045 ifp->if_flags = oldflags;
3050 * Set/clear promiscuous mode on interface ifp based on the truth value
3051 * of pswitch. The calls are reference counted so that only the first
3052 * "on" request actually has an effect, as does the final "off" request.
3053 * Results are undefined if the "off" and "on" requests are not matched.
3056 ifpromisc(struct ifnet *ifp, int pswitch)
3059 int oldflags = ifp->if_flags;
3061 error = if_setflag(ifp, IFF_PROMISC, IFF_PPROMISC,
3062 &ifp->if_pcount, pswitch);
3063 /* If promiscuous mode status has changed, log a message */
3064 if (error == 0 && ((ifp->if_flags ^ oldflags) & IFF_PROMISC) &&
3065 log_promisc_mode_change)
3066 log(LOG_INFO, "%s: promiscuous mode %s\n",
3068 (ifp->if_flags & IFF_PROMISC) ? "enabled" : "disabled");
3073 * Return interface configuration
3074 * of system. List may be used
3075 * in later ioctl's (above) to get
3076 * other information.
3080 ifconf(u_long cmd, caddr_t data)
3082 struct ifconf *ifc = (struct ifconf *)data;
3087 int error, full = 0, valid_len, max_len;
3089 /* Limit initial buffer size to MAXPHYS to avoid DoS from userspace. */
3090 max_len = MAXPHYS - 1;
3092 /* Prevent hostile input from being able to crash the system */
3093 if (ifc->ifc_len <= 0)
3097 if (ifc->ifc_len <= max_len) {
3098 max_len = ifc->ifc_len;
3101 sb = sbuf_new(NULL, NULL, max_len + 1, SBUF_FIXEDLEN);
3106 TAILQ_FOREACH(ifp, &V_ifnet, if_link) {
3110 * Zero the ifr_name buffer to make sure we don't
3111 * disclose the contents of the stack.
3113 memset(ifr.ifr_name, 0, sizeof(ifr.ifr_name));
3115 if (strlcpy(ifr.ifr_name, ifp->if_xname, sizeof(ifr.ifr_name))
3116 >= sizeof(ifr.ifr_name)) {
3119 return (ENAMETOOLONG);
3124 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
3125 struct sockaddr *sa = ifa->ifa_addr;
3127 if (prison_if(curthread->td_ucred, sa) != 0)
3130 if (sa->sa_len <= sizeof(*sa)) {
3132 sbuf_bcat(sb, &ifr, sizeof(ifr));
3133 max_len += sizeof(ifr);
3136 offsetof(struct ifreq, ifr_addr));
3137 max_len += offsetof(struct ifreq, ifr_addr);
3138 sbuf_bcat(sb, sa, sa->sa_len);
3139 max_len += sa->sa_len;
3142 if (sbuf_error(sb) == 0)
3143 valid_len = sbuf_len(sb);
3145 IF_ADDR_RUNLOCK(ifp);
3147 bzero((caddr_t)&ifr.ifr_addr, sizeof(ifr.ifr_addr));
3148 sbuf_bcat(sb, &ifr, sizeof(ifr));
3149 max_len += sizeof(ifr);
3151 if (sbuf_error(sb) == 0)
3152 valid_len = sbuf_len(sb);
3158 * If we didn't allocate enough space (uncommon), try again. If
3159 * we have already allocated as much space as we are allowed,
3160 * return what we've got.
3162 if (valid_len != max_len && !full) {
3167 ifc->ifc_len = valid_len;
3169 error = copyout(sbuf_data(sb), ifc->ifc_req, ifc->ifc_len);
3175 * Just like ifpromisc(), but for all-multicast-reception mode.
3178 if_allmulti(struct ifnet *ifp, int onswitch)
3181 return (if_setflag(ifp, IFF_ALLMULTI, 0, &ifp->if_amcount, onswitch));
3184 struct ifmultiaddr *
3185 if_findmulti(struct ifnet *ifp, const struct sockaddr *sa)
3187 struct ifmultiaddr *ifma;
3189 IF_ADDR_LOCK_ASSERT(ifp);
3191 TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
3192 if (sa->sa_family == AF_LINK) {
3193 if (sa_dl_equal(ifma->ifma_addr, sa))
3196 if (sa_equal(ifma->ifma_addr, sa))
3205 * Allocate a new ifmultiaddr and initialize based on passed arguments. We
3206 * make copies of passed sockaddrs. The ifmultiaddr will not be added to
3207 * the ifnet multicast address list here, so the caller must do that and
3208 * other setup work (such as notifying the device driver). The reference
3209 * count is initialized to 1.
3211 static struct ifmultiaddr *
3212 if_allocmulti(struct ifnet *ifp, struct sockaddr *sa, struct sockaddr *llsa,
3215 struct ifmultiaddr *ifma;
3216 struct sockaddr *dupsa;
3218 ifma = malloc(sizeof *ifma, M_IFMADDR, mflags |
3223 dupsa = malloc(sa->sa_len, M_IFMADDR, mflags);
3224 if (dupsa == NULL) {
3225 free(ifma, M_IFMADDR);
3228 bcopy(sa, dupsa, sa->sa_len);
3229 ifma->ifma_addr = dupsa;
3231 ifma->ifma_ifp = ifp;
3232 ifma->ifma_refcount = 1;
3233 ifma->ifma_protospec = NULL;
3236 ifma->ifma_lladdr = NULL;
3240 dupsa = malloc(llsa->sa_len, M_IFMADDR, mflags);
3241 if (dupsa == NULL) {
3242 free(ifma->ifma_addr, M_IFMADDR);
3243 free(ifma, M_IFMADDR);
3246 bcopy(llsa, dupsa, llsa->sa_len);
3247 ifma->ifma_lladdr = dupsa;
3253 * if_freemulti: free ifmultiaddr structure and possibly attached related
3254 * addresses. The caller is responsible for implementing reference
3255 * counting, notifying the driver, handling routing messages, and releasing
3256 * any dependent link layer state.
3259 if_freemulti(struct ifmultiaddr *ifma)
3262 KASSERT(ifma->ifma_refcount == 0, ("if_freemulti: refcount %d",
3263 ifma->ifma_refcount));
3265 if (ifma->ifma_lladdr != NULL)
3266 free(ifma->ifma_lladdr, M_IFMADDR);
3267 free(ifma->ifma_addr, M_IFMADDR);
3268 free(ifma, M_IFMADDR);
3272 * Register an additional multicast address with a network interface.
3274 * - If the address is already present, bump the reference count on the
3275 * address and return.
3276 * - If the address is not link-layer, look up a link layer address.
3277 * - Allocate address structures for one or both addresses, and attach to the
3278 * multicast address list on the interface. If automatically adding a link
3279 * layer address, the protocol address will own a reference to the link
3280 * layer address, to be freed when it is freed.
3281 * - Notify the network device driver of an addition to the multicast address
3284 * 'sa' points to caller-owned memory with the desired multicast address.
3286 * 'retifma' will be used to return a pointer to the resulting multicast
3287 * address reference, if desired.
3290 if_addmulti(struct ifnet *ifp, struct sockaddr *sa,
3291 struct ifmultiaddr **retifma)
3293 struct ifmultiaddr *ifma, *ll_ifma;
3294 struct sockaddr *llsa;
3295 struct sockaddr_dl sdl;
3299 * If the address is already present, return a new reference to it;
3300 * otherwise, allocate storage and set up a new address.
3303 ifma = if_findmulti(ifp, sa);
3305 ifma->ifma_refcount++;
3306 if (retifma != NULL)
3308 IF_ADDR_WUNLOCK(ifp);
3313 * The address isn't already present; resolve the protocol address
3314 * into a link layer address, and then look that up, bump its
3315 * refcount or allocate an ifma for that also.
3316 * Most link layer resolving functions returns address data which
3317 * fits inside default sockaddr_dl structure. However callback
3318 * can allocate another sockaddr structure, in that case we need to
3323 if (ifp->if_resolvemulti != NULL) {
3324 /* Provide called function with buffer size information */
3325 sdl.sdl_len = sizeof(sdl);
3326 llsa = (struct sockaddr *)&sdl;
3327 error = ifp->if_resolvemulti(ifp, &llsa, sa);
3333 * Allocate the new address. Don't hook it up yet, as we may also
3334 * need to allocate a link layer multicast address.
3336 ifma = if_allocmulti(ifp, sa, llsa, M_NOWAIT);
3343 * If a link layer address is found, we'll need to see if it's
3344 * already present in the address list, or allocate is as well.
3345 * When this block finishes, the link layer address will be on the
3349 ll_ifma = if_findmulti(ifp, llsa);
3350 if (ll_ifma == NULL) {
3351 ll_ifma = if_allocmulti(ifp, llsa, NULL, M_NOWAIT);
3352 if (ll_ifma == NULL) {
3353 --ifma->ifma_refcount;
3358 TAILQ_INSERT_HEAD(&ifp->if_multiaddrs, ll_ifma,
3361 ll_ifma->ifma_refcount++;
3362 ifma->ifma_llifma = ll_ifma;
3366 * We now have a new multicast address, ifma, and possibly a new or
3367 * referenced link layer address. Add the primary address to the
3368 * ifnet address list.
3370 TAILQ_INSERT_HEAD(&ifp->if_multiaddrs, ifma, ifma_link);
3372 if (retifma != NULL)
3376 * Must generate the message while holding the lock so that 'ifma'
3377 * pointer is still valid.
3379 rt_newmaddrmsg(RTM_NEWMADDR, ifma);
3380 IF_ADDR_WUNLOCK(ifp);
3383 * We are certain we have added something, so call down to the
3384 * interface to let them know about it.
3386 if (ifp->if_ioctl != NULL) {
3387 (void) (*ifp->if_ioctl)(ifp, SIOCADDMULTI, 0);
3390 if ((llsa != NULL) && (llsa != (struct sockaddr *)&sdl))
3391 link_free_sdl(llsa);
3396 if ((llsa != NULL) && (llsa != (struct sockaddr *)&sdl))
3397 link_free_sdl(llsa);
3400 IF_ADDR_WUNLOCK(ifp);
3405 * Delete a multicast group membership by network-layer group address.
3407 * Returns ENOENT if the entry could not be found. If ifp no longer
3408 * exists, results are undefined. This entry point should only be used
3409 * from subsystems which do appropriate locking to hold ifp for the
3410 * duration of the call.
3411 * Network-layer protocol domains must use if_delmulti_ifma().
3414 if_delmulti(struct ifnet *ifp, struct sockaddr *sa)
3416 struct ifmultiaddr *ifma;
3421 IFNET_RLOCK_NOSLEEP();
3422 TAILQ_FOREACH(oifp, &V_ifnet, if_link)
3427 IFNET_RUNLOCK_NOSLEEP();
3429 KASSERT(ifp != NULL, ("%s: ifnet went away", __func__));
3436 ifma = if_findmulti(ifp, sa);
3438 lastref = if_delmulti_locked(ifp, ifma, 0);
3439 IF_ADDR_WUNLOCK(ifp);
3444 if (lastref && ifp->if_ioctl != NULL) {
3445 (void)(*ifp->if_ioctl)(ifp, SIOCDELMULTI, 0);
3452 * Delete all multicast group membership for an interface.
3453 * Should be used to quickly flush all multicast filters.
3456 if_delallmulti(struct ifnet *ifp)
3458 struct ifmultiaddr *ifma;
3459 struct ifmultiaddr *next;
3462 TAILQ_FOREACH_SAFE(ifma, &ifp->if_multiaddrs, ifma_link, next)
3463 if_delmulti_locked(ifp, ifma, 0);
3464 IF_ADDR_WUNLOCK(ifp);
3468 * Delete a multicast group membership by group membership pointer.
3469 * Network-layer protocol domains must use this routine.
3471 * It is safe to call this routine if the ifp disappeared.
3474 if_delmulti_ifma(struct ifmultiaddr *ifma)
3479 ifp = ifma->ifma_ifp;
3482 printf("%s: ifma_ifp seems to be detached\n", __func__);
3486 IFNET_RLOCK_NOSLEEP();
3487 TAILQ_FOREACH(oifp, &V_ifnet, if_link)
3491 printf("%s: ifnet %p disappeared\n", __func__, ifp);
3494 IFNET_RUNLOCK_NOSLEEP();
3498 * If and only if the ifnet instance exists: Acquire the address lock.
3503 lastref = if_delmulti_locked(ifp, ifma, 0);
3507 * If and only if the ifnet instance exists:
3508 * Release the address lock.
3509 * If the group was left: update the hardware hash filter.
3511 IF_ADDR_WUNLOCK(ifp);
3512 if (lastref && ifp->if_ioctl != NULL) {
3513 (void)(*ifp->if_ioctl)(ifp, SIOCDELMULTI, 0);
3519 * Perform deletion of network-layer and/or link-layer multicast address.
3521 * Return 0 if the reference count was decremented.
3522 * Return 1 if the final reference was released, indicating that the
3523 * hardware hash filter should be reprogrammed.
3526 if_delmulti_locked(struct ifnet *ifp, struct ifmultiaddr *ifma, int detaching)
3528 struct ifmultiaddr *ll_ifma;
3530 if (ifp != NULL && ifma->ifma_ifp != NULL) {
3531 KASSERT(ifma->ifma_ifp == ifp,
3532 ("%s: inconsistent ifp %p", __func__, ifp));
3533 IF_ADDR_WLOCK_ASSERT(ifp);
3536 ifp = ifma->ifma_ifp;
3539 * If the ifnet is detaching, null out references to ifnet,
3540 * so that upper protocol layers will notice, and not attempt
3541 * to obtain locks for an ifnet which no longer exists. The
3542 * routing socket announcement must happen before the ifnet
3543 * instance is detached from the system.
3547 printf("%s: detaching ifnet instance %p\n", __func__, ifp);
3550 * ifp may already be nulled out if we are being reentered
3551 * to delete the ll_ifma.
3554 rt_newmaddrmsg(RTM_DELMADDR, ifma);
3555 ifma->ifma_ifp = NULL;
3559 if (--ifma->ifma_refcount > 0)
3563 * If this ifma is a network-layer ifma, a link-layer ifma may
3564 * have been associated with it. Release it first if so.
3566 ll_ifma = ifma->ifma_llifma;
3567 if (ll_ifma != NULL) {
3568 KASSERT(ifma->ifma_lladdr != NULL,
3569 ("%s: llifma w/o lladdr", __func__));
3571 ll_ifma->ifma_ifp = NULL; /* XXX */
3572 if (--ll_ifma->ifma_refcount == 0) {
3574 TAILQ_REMOVE(&ifp->if_multiaddrs, ll_ifma,
3577 if_freemulti(ll_ifma);
3582 TAILQ_REMOVE(&ifp->if_multiaddrs, ifma, ifma_link);
3587 * The last reference to this instance of struct ifmultiaddr
3588 * was released; the hardware should be notified of this change.
3594 * Set the link layer address on an interface.
3596 * At this time we only support certain types of interfaces,
3597 * and we don't allow the length of the address to change.
3599 * Set noinline to be dtrace-friendly
3602 if_setlladdr(struct ifnet *ifp, const u_char *lladdr, int len)
3604 struct sockaddr_dl *sdl;
3611 IF_ADDR_RUNLOCK(ifp);
3615 IF_ADDR_RUNLOCK(ifp);
3616 sdl = (struct sockaddr_dl *)ifa->ifa_addr;
3621 if (len != sdl->sdl_alen) { /* don't allow length to change */
3625 switch (ifp->if_type) {
3633 case IFT_IEEE8023ADLAG:
3634 bcopy(lladdr, LLADDR(sdl), len);
3643 * If the interface is already up, we need
3644 * to re-init it in order to reprogram its
3647 if ((ifp->if_flags & IFF_UP) != 0) {
3648 if (ifp->if_ioctl) {
3649 ifp->if_flags &= ~IFF_UP;
3650 ifr.ifr_flags = ifp->if_flags & 0xffff;
3651 ifr.ifr_flagshigh = ifp->if_flags >> 16;
3652 (*ifp->if_ioctl)(ifp, SIOCSIFFLAGS, (caddr_t)&ifr);
3653 ifp->if_flags |= IFF_UP;
3654 ifr.ifr_flags = ifp->if_flags & 0xffff;
3655 ifr.ifr_flagshigh = ifp->if_flags >> 16;
3656 (*ifp->if_ioctl)(ifp, SIOCSIFFLAGS, (caddr_t)&ifr);
3659 EVENTHANDLER_INVOKE(iflladdr_event, ifp);
3664 * Compat function for handling basic encapsulation requests.
3665 * Not converted stacks (FDDI, IB, ..) supports traditional
3666 * output model: ARP (and other similar L2 protocols) are handled
3667 * inside output routine, arpresolve/nd6_resolve() returns MAC
3668 * address instead of full prepend.
3670 * This function creates calculated header==MAC for IPv4/IPv6 and
3671 * returns EAFNOSUPPORT (which is then handled in ARP code) for other
3675 if_requestencap_default(struct ifnet *ifp, struct if_encap_req *req)
3678 if (req->rtype != IFENCAP_LL)
3679 return (EOPNOTSUPP);
3681 if (req->bufsize < req->lladdr_len)
3684 switch (req->family) {
3689 return (EAFNOSUPPORT);
3692 /* Copy lladdr to storage as is */
3693 memmove(req->buf, req->lladdr, req->lladdr_len);
3694 req->bufsize = req->lladdr_len;
3695 req->lladdr_off = 0;
3701 * Get the link layer address that was read from the hardware at attach.
3703 * This is only set by Ethernet NICs (IFT_ETHER), but laggX interfaces re-type
3704 * their component interfaces as IFT_IEEE8023ADLAG.
3707 if_gethwaddr(struct ifnet *ifp, struct ifreq *ifr)
3710 if (ifp->if_hw_addr == NULL)
3713 switch (ifp->if_type) {
3715 case IFT_IEEE8023ADLAG:
3716 bcopy(ifp->if_hw_addr, ifr->ifr_addr.sa_data, ifp->if_addrlen);
3724 * The name argument must be a pointer to storage which will last as
3725 * long as the interface does. For physical devices, the result of
3726 * device_get_name(dev) is a good choice and for pseudo-devices a
3727 * static string works well.
3730 if_initname(struct ifnet *ifp, const char *name, int unit)
3732 ifp->if_dname = name;
3733 ifp->if_dunit = unit;
3734 if (unit != IF_DUNIT_NONE)
3735 snprintf(ifp->if_xname, IFNAMSIZ, "%s%d", name, unit);
3737 strlcpy(ifp->if_xname, name, IFNAMSIZ);
3741 if_printf(struct ifnet *ifp, const char * fmt, ...)
3746 retval = printf("%s: ", ifp->if_xname);
3748 retval += vprintf(fmt, ap);
3754 if_start(struct ifnet *ifp)
3757 (*(ifp)->if_start)(ifp);
3761 * Backwards compatibility interface for drivers
3762 * that have not implemented it
3765 if_transmit(struct ifnet *ifp, struct mbuf *m)
3769 IFQ_HANDOFF(ifp, m, error);
3774 if_input_default(struct ifnet *ifp __unused, struct mbuf *m)
3781 if_handoff(struct ifqueue *ifq, struct mbuf *m, struct ifnet *ifp, int adjust)
3786 if (_IF_QFULL(ifq)) {
3788 if_inc_counter(ifp, IFCOUNTER_OQDROPS, 1);
3793 if_inc_counter(ifp, IFCOUNTER_OBYTES, m->m_pkthdr.len + adjust);
3794 if (m->m_flags & (M_BCAST|M_MCAST))
3795 if_inc_counter(ifp, IFCOUNTER_OMCASTS, 1);
3796 active = ifp->if_drv_flags & IFF_DRV_OACTIVE;
3798 _IF_ENQUEUE(ifq, m);
3800 if (ifp != NULL && !active)
3801 (*(ifp)->if_start)(ifp);
3806 if_register_com_alloc(u_char type,
3807 if_com_alloc_t *a, if_com_free_t *f)
3810 KASSERT(if_com_alloc[type] == NULL,
3811 ("if_register_com_alloc: %d already registered", type));
3812 KASSERT(if_com_free[type] == NULL,
3813 ("if_register_com_alloc: %d free already registered", type));
3815 if_com_alloc[type] = a;
3816 if_com_free[type] = f;
3820 if_deregister_com_alloc(u_char type)
3823 KASSERT(if_com_alloc[type] != NULL,
3824 ("if_deregister_com_alloc: %d not registered", type));
3825 KASSERT(if_com_free[type] != NULL,
3826 ("if_deregister_com_alloc: %d free not registered", type));
3827 if_com_alloc[type] = NULL;
3828 if_com_free[type] = NULL;
3831 /* API for driver access to network stack owned ifnet.*/
3833 if_setbaudrate(struct ifnet *ifp, uint64_t baudrate)
3837 oldbrate = ifp->if_baudrate;
3838 ifp->if_baudrate = baudrate;
3843 if_getbaudrate(if_t ifp)
3846 return (((struct ifnet *)ifp)->if_baudrate);
3850 if_setcapabilities(if_t ifp, int capabilities)
3852 ((struct ifnet *)ifp)->if_capabilities = capabilities;
3857 if_setcapabilitiesbit(if_t ifp, int setbit, int clearbit)
3859 ((struct ifnet *)ifp)->if_capabilities |= setbit;
3860 ((struct ifnet *)ifp)->if_capabilities &= ~clearbit;
3866 if_getcapabilities(if_t ifp)
3868 return ((struct ifnet *)ifp)->if_capabilities;
3872 if_setcapenable(if_t ifp, int capabilities)
3874 ((struct ifnet *)ifp)->if_capenable = capabilities;
3879 if_setcapenablebit(if_t ifp, int setcap, int clearcap)
3882 ((struct ifnet *)ifp)->if_capenable |= setcap;
3884 ((struct ifnet *)ifp)->if_capenable &= ~clearcap;
3890 if_getdname(if_t ifp)
3892 return ((struct ifnet *)ifp)->if_dname;
3896 if_togglecapenable(if_t ifp, int togglecap)
3898 ((struct ifnet *)ifp)->if_capenable ^= togglecap;
3903 if_getcapenable(if_t ifp)
3905 return ((struct ifnet *)ifp)->if_capenable;
3909 * This is largely undesirable because it ties ifnet to a device, but does
3910 * provide flexiblity for an embedded product vendor. Should be used with
3911 * the understanding that it violates the interface boundaries, and should be
3912 * a last resort only.
3915 if_setdev(if_t ifp, void *dev)
3921 if_setdrvflagbits(if_t ifp, int set_flags, int clear_flags)
3923 ((struct ifnet *)ifp)->if_drv_flags |= set_flags;
3924 ((struct ifnet *)ifp)->if_drv_flags &= ~clear_flags;
3930 if_getdrvflags(if_t ifp)
3932 return ((struct ifnet *)ifp)->if_drv_flags;
3936 if_setdrvflags(if_t ifp, int flags)
3938 ((struct ifnet *)ifp)->if_drv_flags = flags;
3944 if_setflags(if_t ifp, int flags)
3946 ((struct ifnet *)ifp)->if_flags = flags;
3951 if_setflagbits(if_t ifp, int set, int clear)
3953 ((struct ifnet *)ifp)->if_flags |= set;
3954 ((struct ifnet *)ifp)->if_flags &= ~clear;
3960 if_getflags(if_t ifp)
3962 return ((struct ifnet *)ifp)->if_flags;
3966 if_clearhwassist(if_t ifp)
3968 ((struct ifnet *)ifp)->if_hwassist = 0;
3973 if_sethwassistbits(if_t ifp, int toset, int toclear)
3975 ((struct ifnet *)ifp)->if_hwassist |= toset;
3976 ((struct ifnet *)ifp)->if_hwassist &= ~toclear;
3982 if_sethwassist(if_t ifp, int hwassist_bit)
3984 ((struct ifnet *)ifp)->if_hwassist = hwassist_bit;
3989 if_gethwassist(if_t ifp)
3991 return ((struct ifnet *)ifp)->if_hwassist;
3995 if_setmtu(if_t ifp, int mtu)
3997 ((struct ifnet *)ifp)->if_mtu = mtu;
4004 return ((struct ifnet *)ifp)->if_mtu;
4008 if_getmtu_family(if_t ifp, int family)
4012 for (dp = domains; dp; dp = dp->dom_next) {
4013 if (dp->dom_family == family && dp->dom_ifmtu != NULL)
4014 return (dp->dom_ifmtu((struct ifnet *)ifp));
4017 return (((struct ifnet *)ifp)->if_mtu);
4021 if_setsoftc(if_t ifp, void *softc)
4023 ((struct ifnet *)ifp)->if_softc = softc;
4028 if_getsoftc(if_t ifp)
4030 return ((struct ifnet *)ifp)->if_softc;
4034 if_setrcvif(struct mbuf *m, if_t ifp)
4036 m->m_pkthdr.rcvif = (struct ifnet *)ifp;
4040 if_setvtag(struct mbuf *m, uint16_t tag)
4042 m->m_pkthdr.ether_vtag = tag;
4046 if_getvtag(struct mbuf *m)
4049 return (m->m_pkthdr.ether_vtag);
4053 if_sendq_empty(if_t ifp)
4055 return IFQ_DRV_IS_EMPTY(&((struct ifnet *)ifp)->if_snd);
4059 if_getifaddr(if_t ifp)
4061 return ((struct ifnet *)ifp)->if_addr;
4065 if_getamcount(if_t ifp)
4067 return ((struct ifnet *)ifp)->if_amcount;
4072 if_setsendqready(if_t ifp)
4074 IFQ_SET_READY(&((struct ifnet *)ifp)->if_snd);
4079 if_setsendqlen(if_t ifp, int tx_desc_count)
4081 IFQ_SET_MAXLEN(&((struct ifnet *)ifp)->if_snd, tx_desc_count);
4082 ((struct ifnet *)ifp)->if_snd.ifq_drv_maxlen = tx_desc_count;
4088 if_vlantrunkinuse(if_t ifp)
4090 return ((struct ifnet *)ifp)->if_vlantrunk != NULL?1:0;
4094 if_input(if_t ifp, struct mbuf* sendmp)
4096 (*((struct ifnet *)ifp)->if_input)((struct ifnet *)ifp, sendmp);
4102 #ifndef ETH_ADDR_LEN
4103 #define ETH_ADDR_LEN 6
4107 if_setupmultiaddr(if_t ifp, void *mta, int *cnt, int max)
4109 struct ifmultiaddr *ifma;
4110 uint8_t *lmta = (uint8_t *)mta;
4113 TAILQ_FOREACH(ifma, &((struct ifnet *)ifp)->if_multiaddrs, ifma_link) {
4114 if (ifma->ifma_addr->sa_family != AF_LINK)
4120 bcopy(LLADDR((struct sockaddr_dl *)ifma->ifma_addr),
4121 &lmta[mcnt * ETH_ADDR_LEN], ETH_ADDR_LEN);
4130 if_multiaddr_array(if_t ifp, void *mta, int *cnt, int max)
4134 if_maddr_rlock(ifp);
4135 error = if_setupmultiaddr(ifp, mta, cnt, max);
4136 if_maddr_runlock(ifp);
4141 if_multiaddr_count(if_t ifp, int max)
4143 struct ifmultiaddr *ifma;
4147 if_maddr_rlock(ifp);
4148 TAILQ_FOREACH(ifma, &((struct ifnet *)ifp)->if_multiaddrs, ifma_link) {
4149 if (ifma->ifma_addr->sa_family != AF_LINK)
4155 if_maddr_runlock(ifp);
4160 if_multi_apply(struct ifnet *ifp, int (*filter)(void *, struct ifmultiaddr *, int), void *arg)
4162 struct ifmultiaddr *ifma;
4165 if_maddr_rlock(ifp);
4166 TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link)
4167 cnt += filter(arg, ifma, cnt);
4168 if_maddr_runlock(ifp);
4173 if_dequeue(if_t ifp)
4176 IFQ_DRV_DEQUEUE(&((struct ifnet *)ifp)->if_snd, m);
4182 if_sendq_prepend(if_t ifp, struct mbuf *m)
4184 IFQ_DRV_PREPEND(&((struct ifnet *)ifp)->if_snd, m);
4189 if_setifheaderlen(if_t ifp, int len)
4191 ((struct ifnet *)ifp)->if_hdrlen = len;
4196 if_getlladdr(if_t ifp)
4198 return (IF_LLADDR((struct ifnet *)ifp));
4202 if_gethandle(u_char type)
4204 return (if_alloc(type));
4208 if_bpfmtap(if_t ifh, struct mbuf *m)
4210 struct ifnet *ifp = (struct ifnet *)ifh;
4216 if_etherbpfmtap(if_t ifh, struct mbuf *m)
4218 struct ifnet *ifp = (struct ifnet *)ifh;
4220 ETHER_BPF_MTAP(ifp, m);
4224 if_vlancap(if_t ifh)
4226 struct ifnet *ifp = (struct ifnet *)ifh;
4227 VLAN_CAPABILITIES(ifp);
4231 if_sethwtsomax(if_t ifp, u_int if_hw_tsomax)
4234 ((struct ifnet *)ifp)->if_hw_tsomax = if_hw_tsomax;
4239 if_sethwtsomaxsegcount(if_t ifp, u_int if_hw_tsomaxsegcount)
4242 ((struct ifnet *)ifp)->if_hw_tsomaxsegcount = if_hw_tsomaxsegcount;
4247 if_sethwtsomaxsegsize(if_t ifp, u_int if_hw_tsomaxsegsize)
4250 ((struct ifnet *)ifp)->if_hw_tsomaxsegsize = if_hw_tsomaxsegsize;
4255 if_gethwtsomax(if_t ifp)
4258 return (((struct ifnet *)ifp)->if_hw_tsomax);
4262 if_gethwtsomaxsegcount(if_t ifp)
4265 return (((struct ifnet *)ifp)->if_hw_tsomaxsegcount);
4269 if_gethwtsomaxsegsize(if_t ifp)
4272 return (((struct ifnet *)ifp)->if_hw_tsomaxsegsize);
4276 if_setinitfn(if_t ifp, void (*init_fn)(void *))
4278 ((struct ifnet *)ifp)->if_init = init_fn;
4282 if_setioctlfn(if_t ifp, int (*ioctl_fn)(if_t, u_long, caddr_t))
4284 ((struct ifnet *)ifp)->if_ioctl = (void *)ioctl_fn;
4288 if_setstartfn(if_t ifp, void (*start_fn)(if_t))
4290 ((struct ifnet *)ifp)->if_start = (void *)start_fn;
4294 if_settransmitfn(if_t ifp, if_transmit_fn_t start_fn)
4296 ((struct ifnet *)ifp)->if_transmit = start_fn;
4299 void if_setqflushfn(if_t ifp, if_qflush_fn_t flush_fn)
4301 ((struct ifnet *)ifp)->if_qflush = flush_fn;
4306 if_setgetcounterfn(if_t ifp, if_get_counter_t fn)
4309 ifp->if_get_counter = fn;
4312 /* Revisit these - These are inline functions originally. */
4314 drbr_inuse_drv(if_t ifh, struct buf_ring *br)
4316 return drbr_inuse(ifh, br);
4320 drbr_dequeue_drv(if_t ifh, struct buf_ring *br)
4322 return drbr_dequeue(ifh, br);
4326 drbr_needs_enqueue_drv(if_t ifh, struct buf_ring *br)
4328 return drbr_needs_enqueue(ifh, br);
4332 drbr_enqueue_drv(if_t ifh, struct buf_ring *br, struct mbuf *m)
4334 return drbr_enqueue(ifh, br, m);