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/taskqueue.h>
61 #include <sys/domain.h>
65 #include <machine/stdarg.h>
69 #include <net/ethernet.h>
71 #include <net/if_arp.h>
72 #include <net/if_clone.h>
73 #include <net/if_dl.h>
74 #include <net/if_types.h>
75 #include <net/if_var.h>
76 #include <net/if_media.h>
77 #include <net/if_vlan_var.h>
78 #include <net/radix.h>
79 #include <net/route.h>
82 #if defined(INET) || defined(INET6)
83 #include <net/ethernet.h>
84 #include <netinet/in.h>
85 #include <netinet/in_var.h>
86 #include <netinet/ip.h>
87 #include <netinet/ip_carp.h>
89 #include <netinet/if_ether.h>
92 #include <netinet6/in6_var.h>
93 #include <netinet6/in6_ifattach.h>
95 #endif /* INET || INET6 */
97 #include <security/mac/mac_framework.h>
99 #ifdef COMPAT_FREEBSD32
100 #include <sys/mount.h>
101 #include <compat/freebsd32/freebsd32.h>
104 SYSCTL_NODE(_net, PF_LINK, link, CTLFLAG_RW, 0, "Link layers");
105 SYSCTL_NODE(_net_link, 0, generic, CTLFLAG_RW, 0, "Generic link-management");
107 SYSCTL_INT(_net_link, OID_AUTO, ifqmaxlen, CTLFLAG_RDTUN,
108 &ifqmaxlen, 0, "max send queue size");
110 /* Log link state change events */
111 static int log_link_state_change = 1;
113 SYSCTL_INT(_net_link, OID_AUTO, log_link_state_change, CTLFLAG_RW,
114 &log_link_state_change, 0,
115 "log interface link state change events");
117 /* Log promiscuous mode change events */
118 static int log_promisc_mode_change = 1;
120 SYSCTL_INT(_net_link, OID_AUTO, log_promisc_mode_change, CTLFLAG_RDTUN,
121 &log_promisc_mode_change, 1,
122 "log promiscuous mode change events");
124 /* Interface description */
125 static unsigned int ifdescr_maxlen = 1024;
126 SYSCTL_UINT(_net, OID_AUTO, ifdescr_maxlen, CTLFLAG_RW,
128 "administrative maximum length for interface description");
130 static MALLOC_DEFINE(M_IFDESCR, "ifdescr", "ifnet descriptions");
132 /* global sx for non-critical path ifdescr */
133 static struct sx ifdescr_sx;
134 SX_SYSINIT(ifdescr_sx, &ifdescr_sx, "ifnet descr");
136 void (*bridge_linkstate_p)(struct ifnet *ifp);
137 void (*ng_ether_link_state_p)(struct ifnet *ifp, int state);
138 void (*lagg_linkstate_p)(struct ifnet *ifp, int state);
139 /* These are external hooks for CARP. */
140 void (*carp_linkstate_p)(struct ifnet *ifp);
141 void (*carp_demote_adj_p)(int, char *);
142 int (*carp_master_p)(struct ifaddr *);
143 #if defined(INET) || defined(INET6)
144 int (*carp_forus_p)(struct ifnet *ifp, u_char *dhost);
145 int (*carp_output_p)(struct ifnet *ifp, struct mbuf *m,
146 const struct sockaddr *sa);
147 int (*carp_ioctl_p)(struct ifreq *, u_long, struct thread *);
148 int (*carp_attach_p)(struct ifaddr *, int);
149 void (*carp_detach_p)(struct ifaddr *, bool);
152 int (*carp_iamatch_p)(struct ifaddr *, uint8_t **);
155 struct ifaddr *(*carp_iamatch6_p)(struct ifnet *ifp, struct in6_addr *taddr6);
156 caddr_t (*carp_macmatch6_p)(struct ifnet *ifp, struct mbuf *m,
157 const struct in6_addr *taddr);
160 struct mbuf *(*tbr_dequeue_ptr)(struct ifaltq *, int) = NULL;
163 * XXX: Style; these should be sorted alphabetically, and unprototyped
164 * static functions should be prototyped. Currently they are sorted by
167 static void if_attachdomain(void *);
168 static void if_attachdomain1(struct ifnet *);
169 static int ifconf(u_long, caddr_t);
170 static void if_freemulti(struct ifmultiaddr *);
171 static void if_grow(void);
172 static void if_input_default(struct ifnet *, struct mbuf *);
173 static int if_requestencap_default(struct ifnet *, struct if_encap_req *);
174 static void if_route(struct ifnet *, int flag, int fam);
175 static int if_setflag(struct ifnet *, int, int, int *, int);
176 static int if_transmit(struct ifnet *ifp, struct mbuf *m);
177 static void if_unroute(struct ifnet *, int flag, int fam);
178 static void link_rtrequest(int, struct rtentry *, struct rt_addrinfo *);
179 static int ifhwioctl(u_long, struct ifnet *, caddr_t, struct thread *);
180 static int if_delmulti_locked(struct ifnet *, struct ifmultiaddr *, int);
181 static void do_link_state_change(void *, int);
182 static int if_getgroup(struct ifgroupreq *, struct ifnet *);
183 static int if_getgroupmembers(struct ifgroupreq *);
184 static void if_delgroups(struct ifnet *);
185 static void if_attach_internal(struct ifnet *, int, struct if_clone *);
186 static int if_detach_internal(struct ifnet *, int, struct if_clone **);
188 static void if_vmove(struct ifnet *, struct vnet *);
193 * XXX: declare here to avoid to include many inet6 related files..
194 * should be more generalized?
196 extern void nd6_setmtu(struct ifnet *);
199 /* ipsec helper hooks */
200 VNET_DEFINE(struct hhook_head *, ipsec_hhh_in[HHOOK_IPSEC_COUNT]);
201 VNET_DEFINE(struct hhook_head *, ipsec_hhh_out[HHOOK_IPSEC_COUNT]);
203 VNET_DEFINE(int, if_index);
204 int ifqmaxlen = IFQ_MAXLEN;
205 VNET_DEFINE(struct ifnethead, ifnet); /* depend on static init XXX */
206 VNET_DEFINE(struct ifgrouphead, ifg_head);
208 static VNET_DEFINE(int, if_indexlim) = 8;
210 /* Table of ifnet by index. */
211 VNET_DEFINE(struct ifnet **, ifindex_table);
213 #define V_if_indexlim VNET(if_indexlim)
214 #define V_ifindex_table VNET(ifindex_table)
217 * The global network interface list (V_ifnet) and related state (such as
218 * if_index, if_indexlim, and ifindex_table) are protected by an sxlock and
219 * an rwlock. Either may be acquired shared to stablize the list, but both
220 * must be acquired writable to modify the list. This model allows us to
221 * both stablize the interface list during interrupt thread processing, but
222 * also to stablize it over long-running ioctls, without introducing priority
223 * inversions and deadlocks.
225 struct rwlock ifnet_rwlock;
226 RW_SYSINIT_FLAGS(ifnet_rw, &ifnet_rwlock, "ifnet_rw", RW_RECURSE);
227 struct sx ifnet_sxlock;
228 SX_SYSINIT_FLAGS(ifnet_sx, &ifnet_sxlock, "ifnet_sx", SX_RECURSE);
231 * The allocation of network interfaces is a rather non-atomic affair; we
232 * need to select an index before we are ready to expose the interface for
233 * use, so will use this pointer value to indicate reservation.
235 #define IFNET_HOLD (void *)(uintptr_t)(-1)
237 static if_com_alloc_t *if_com_alloc[256];
238 static if_com_free_t *if_com_free[256];
240 static MALLOC_DEFINE(M_IFNET, "ifnet", "interface internals");
241 MALLOC_DEFINE(M_IFADDR, "ifaddr", "interface address");
242 MALLOC_DEFINE(M_IFMADDR, "ether_multi", "link-level multicast address");
245 ifnet_byindex_locked(u_short idx)
248 if (idx > V_if_index)
250 if (V_ifindex_table[idx] == IFNET_HOLD)
252 return (V_ifindex_table[idx]);
256 ifnet_byindex(u_short idx)
260 IFNET_RLOCK_NOSLEEP();
261 ifp = ifnet_byindex_locked(idx);
262 IFNET_RUNLOCK_NOSLEEP();
267 ifnet_byindex_ref(u_short idx)
271 IFNET_RLOCK_NOSLEEP();
272 ifp = ifnet_byindex_locked(idx);
273 if (ifp == NULL || (ifp->if_flags & IFF_DYING)) {
274 IFNET_RUNLOCK_NOSLEEP();
278 IFNET_RUNLOCK_NOSLEEP();
283 * Allocate an ifindex array entry; return 0 on success or an error on
291 IFNET_WLOCK_ASSERT();
294 * Try to find an empty slot below V_if_index. If we fail, take the
297 for (idx = 1; idx <= V_if_index; idx++) {
298 if (V_ifindex_table[idx] == NULL)
302 /* Catch if_index overflow. */
303 if (idx >= V_if_indexlim) {
307 if (idx > V_if_index)
313 ifindex_free_locked(u_short idx)
316 IFNET_WLOCK_ASSERT();
318 V_ifindex_table[idx] = NULL;
319 while (V_if_index > 0 &&
320 V_ifindex_table[V_if_index] == NULL)
325 ifindex_free(u_short idx)
329 ifindex_free_locked(idx);
334 ifnet_setbyindex_locked(u_short idx, struct ifnet *ifp)
337 IFNET_WLOCK_ASSERT();
339 V_ifindex_table[idx] = ifp;
343 ifnet_setbyindex(u_short idx, struct ifnet *ifp)
347 ifnet_setbyindex_locked(idx, ifp);
352 ifaddr_byindex(u_short idx)
355 struct ifaddr *ifa = NULL;
357 IFNET_RLOCK_NOSLEEP();
358 ifp = ifnet_byindex_locked(idx);
359 if (ifp != NULL && (ifa = ifp->if_addr) != NULL)
361 IFNET_RUNLOCK_NOSLEEP();
366 * Network interface utility routines.
368 * Routines with ifa_ifwith* names take sockaddr *'s as
373 vnet_if_init(const void *unused __unused)
376 TAILQ_INIT(&V_ifnet);
377 TAILQ_INIT(&V_ifg_head);
379 if_grow(); /* create initial table */
381 vnet_if_clone_init();
383 VNET_SYSINIT(vnet_if_init, SI_SUB_INIT_IF, SI_ORDER_SECOND, vnet_if_init,
388 vnet_if_uninit(const void *unused __unused)
391 VNET_ASSERT(TAILQ_EMPTY(&V_ifnet), ("%s:%d tailq &V_ifnet=%p "
392 "not empty", __func__, __LINE__, &V_ifnet));
393 VNET_ASSERT(TAILQ_EMPTY(&V_ifg_head), ("%s:%d tailq &V_ifg_head=%p "
394 "not empty", __func__, __LINE__, &V_ifg_head));
396 free((caddr_t)V_ifindex_table, M_IFNET);
398 VNET_SYSUNINIT(vnet_if_uninit, SI_SUB_INIT_IF, SI_ORDER_FIRST,
399 vnet_if_uninit, NULL);
402 vnet_if_return(const void *unused __unused)
404 struct ifnet *ifp, *nifp;
406 /* Return all inherited interfaces to their parent vnets. */
407 TAILQ_FOREACH_SAFE(ifp, &V_ifnet, if_link, nifp) {
408 if (ifp->if_home_vnet != ifp->if_vnet)
409 if_vmove(ifp, ifp->if_home_vnet);
412 VNET_SYSUNINIT(vnet_if_return, SI_SUB_VNET_DONE, SI_ORDER_ANY,
413 vnet_if_return, NULL);
423 IFNET_WLOCK_ASSERT();
424 oldlim = V_if_indexlim;
426 n = (oldlim << 1) * sizeof(*e);
427 e = malloc(n, M_IFNET, M_WAITOK | M_ZERO);
429 if (V_if_indexlim != oldlim) {
433 if (V_ifindex_table != NULL) {
434 memcpy((caddr_t)e, (caddr_t)V_ifindex_table, n/2);
435 free((caddr_t)V_ifindex_table, M_IFNET);
442 * Allocate a struct ifnet and an index for an interface. A layer 2
443 * common structure will also be allocated if an allocation routine is
444 * registered for the passed type.
447 if_alloc(u_char type)
452 ifp = malloc(sizeof(struct ifnet), M_IFNET, M_WAITOK|M_ZERO);
454 idx = ifindex_alloc();
455 ifnet_setbyindex_locked(idx, IFNET_HOLD);
459 ifp->if_alloctype = type;
461 ifp->if_vnet = curvnet;
463 if (if_com_alloc[type] != NULL) {
464 ifp->if_l2com = if_com_alloc[type](type, ifp);
465 if (ifp->if_l2com == NULL) {
472 IF_ADDR_LOCK_INIT(ifp);
473 TASK_INIT(&ifp->if_linktask, 0, do_link_state_change, ifp);
474 ifp->if_afdata_initialized = 0;
475 IF_AFDATA_LOCK_INIT(ifp);
476 TAILQ_INIT(&ifp->if_addrhead);
477 TAILQ_INIT(&ifp->if_multiaddrs);
478 TAILQ_INIT(&ifp->if_groups);
482 ifq_init(&ifp->if_snd, ifp);
484 refcount_init(&ifp->if_refcount, 1); /* Index reference. */
485 for (int i = 0; i < IFCOUNTERS; i++)
486 ifp->if_counters[i] = counter_u64_alloc(M_WAITOK);
487 ifp->if_get_counter = if_get_counter_default;
488 ifnet_setbyindex(ifp->if_index, ifp);
493 * Do the actual work of freeing a struct ifnet, and layer 2 common
494 * structure. This call is made when the last reference to an
495 * interface is released.
498 if_free_internal(struct ifnet *ifp)
501 KASSERT((ifp->if_flags & IFF_DYING),
502 ("if_free_internal: interface not dying"));
504 if (if_com_free[ifp->if_alloctype] != NULL)
505 if_com_free[ifp->if_alloctype](ifp->if_l2com,
509 mac_ifnet_destroy(ifp);
511 if (ifp->if_description != NULL)
512 free(ifp->if_description, M_IFDESCR);
513 IF_AFDATA_DESTROY(ifp);
514 IF_ADDR_LOCK_DESTROY(ifp);
515 ifq_delete(&ifp->if_snd);
517 for (int i = 0; i < IFCOUNTERS; i++)
518 counter_u64_free(ifp->if_counters[i]);
524 * Deregister an interface and free the associated storage.
527 if_free(struct ifnet *ifp)
530 ifp->if_flags |= IFF_DYING; /* XXX: Locking */
532 CURVNET_SET_QUIET(ifp->if_vnet);
534 KASSERT(ifp == ifnet_byindex_locked(ifp->if_index),
535 ("%s: freeing unallocated ifnet", ifp->if_xname));
537 ifindex_free_locked(ifp->if_index);
540 if (refcount_release(&ifp->if_refcount))
541 if_free_internal(ifp);
546 * Interfaces to keep an ifnet type-stable despite the possibility of the
547 * driver calling if_free(). If there are additional references, we defer
548 * freeing the underlying data structure.
551 if_ref(struct ifnet *ifp)
554 /* We don't assert the ifnet list lock here, but arguably should. */
555 refcount_acquire(&ifp->if_refcount);
559 if_rele(struct ifnet *ifp)
562 if (!refcount_release(&ifp->if_refcount))
564 if_free_internal(ifp);
568 ifq_init(struct ifaltq *ifq, struct ifnet *ifp)
571 mtx_init(&ifq->ifq_mtx, ifp->if_xname, "if send queue", MTX_DEF);
573 if (ifq->ifq_maxlen == 0)
574 ifq->ifq_maxlen = ifqmaxlen;
577 ifq->altq_disc = NULL;
578 ifq->altq_flags &= ALTQF_CANTCHANGE;
579 ifq->altq_tbr = NULL;
584 ifq_delete(struct ifaltq *ifq)
586 mtx_destroy(&ifq->ifq_mtx);
590 * Perform generic interface initialization tasks and attach the interface
591 * to the list of "active" interfaces. If vmove flag is set on entry
592 * to if_attach_internal(), perform only a limited subset of initialization
593 * tasks, given that we are moving from one vnet to another an ifnet which
594 * has already been fully initialized.
596 * Note that if_detach_internal() removes group membership unconditionally
597 * even when vmove flag is set, and if_attach_internal() adds only IFG_ALL.
598 * Thus, when if_vmove() is applied to a cloned interface, group membership
599 * is lost while a cloned one always joins a group whose name is
600 * ifc->ifc_name. To recover this after if_detach_internal() and
601 * if_attach_internal(), the cloner should be specified to
602 * if_attach_internal() via ifc. If it is non-NULL, if_attach_internal()
603 * attempts to join a group whose name is ifc->ifc_name.
606 * - The decision to return void and thus require this function to
607 * succeed is questionable.
608 * - We should probably do more sanity checking. For instance we don't
609 * do anything to insure if_xname is unique or non-empty.
612 if_attach(struct ifnet *ifp)
615 if_attach_internal(ifp, 0, NULL);
619 * Compute the least common TSO limit.
622 if_hw_tsomax_common(if_t ifp, struct ifnet_hw_tsomax *pmax)
625 * 1) If there is no limit currently, take the limit from
626 * the network adapter.
628 * 2) If the network adapter has a limit below the current
631 if (pmax->tsomaxbytes == 0 || (ifp->if_hw_tsomax != 0 &&
632 ifp->if_hw_tsomax < pmax->tsomaxbytes)) {
633 pmax->tsomaxbytes = ifp->if_hw_tsomax;
635 if (pmax->tsomaxsegcount == 0 || (ifp->if_hw_tsomaxsegcount != 0 &&
636 ifp->if_hw_tsomaxsegcount < pmax->tsomaxsegcount)) {
637 pmax->tsomaxsegcount = ifp->if_hw_tsomaxsegcount;
639 if (pmax->tsomaxsegsize == 0 || (ifp->if_hw_tsomaxsegsize != 0 &&
640 ifp->if_hw_tsomaxsegsize < pmax->tsomaxsegsize)) {
641 pmax->tsomaxsegsize = ifp->if_hw_tsomaxsegsize;
646 * Update TSO limit of a network adapter.
648 * Returns zero if no change. Else non-zero.
651 if_hw_tsomax_update(if_t ifp, struct ifnet_hw_tsomax *pmax)
654 if (ifp->if_hw_tsomax != pmax->tsomaxbytes) {
655 ifp->if_hw_tsomax = pmax->tsomaxbytes;
658 if (ifp->if_hw_tsomaxsegsize != pmax->tsomaxsegsize) {
659 ifp->if_hw_tsomaxsegsize = pmax->tsomaxsegsize;
662 if (ifp->if_hw_tsomaxsegcount != pmax->tsomaxsegcount) {
663 ifp->if_hw_tsomaxsegcount = pmax->tsomaxsegcount;
670 if_attach_internal(struct ifnet *ifp, int vmove, struct if_clone *ifc)
672 unsigned socksize, ifasize;
673 int namelen, masklen;
674 struct sockaddr_dl *sdl;
677 if (ifp->if_index == 0 || ifp != ifnet_byindex(ifp->if_index))
678 panic ("%s: BUG: if_attach called without if_alloc'd input()\n",
682 ifp->if_vnet = curvnet;
683 if (ifp->if_home_vnet == NULL)
684 ifp->if_home_vnet = curvnet;
687 if_addgroup(ifp, IFG_ALL);
689 /* Restore group membership for cloned interfaces. */
690 if (vmove && ifc != NULL)
691 if_clone_addgroup(ifp, ifc);
693 getmicrotime(&ifp->if_lastchange);
694 ifp->if_epoch = time_uptime;
696 KASSERT((ifp->if_transmit == NULL && ifp->if_qflush == NULL) ||
697 (ifp->if_transmit != NULL && ifp->if_qflush != NULL),
698 ("transmit and qflush must both either be set or both be NULL"));
699 if (ifp->if_transmit == NULL) {
700 ifp->if_transmit = if_transmit;
701 ifp->if_qflush = if_qflush;
703 if (ifp->if_input == NULL)
704 ifp->if_input = if_input_default;
706 if (ifp->if_requestencap == NULL)
707 ifp->if_requestencap = if_requestencap_default;
711 mac_ifnet_create(ifp);
715 * Create a Link Level name for this device.
717 namelen = strlen(ifp->if_xname);
719 * Always save enough space for any possiable name so we
720 * can do a rename in place later.
722 masklen = offsetof(struct sockaddr_dl, sdl_data[0]) + IFNAMSIZ;
723 socksize = masklen + ifp->if_addrlen;
724 if (socksize < sizeof(*sdl))
725 socksize = sizeof(*sdl);
726 socksize = roundup2(socksize, sizeof(long));
727 ifasize = sizeof(*ifa) + 2 * socksize;
728 ifa = ifa_alloc(ifasize, M_WAITOK);
729 sdl = (struct sockaddr_dl *)(ifa + 1);
730 sdl->sdl_len = socksize;
731 sdl->sdl_family = AF_LINK;
732 bcopy(ifp->if_xname, sdl->sdl_data, namelen);
733 sdl->sdl_nlen = namelen;
734 sdl->sdl_index = ifp->if_index;
735 sdl->sdl_type = ifp->if_type;
738 ifa->ifa_rtrequest = link_rtrequest;
739 ifa->ifa_addr = (struct sockaddr *)sdl;
740 sdl = (struct sockaddr_dl *)(socksize + (caddr_t)sdl);
741 ifa->ifa_netmask = (struct sockaddr *)sdl;
742 sdl->sdl_len = masklen;
744 sdl->sdl_data[--namelen] = 0xff;
745 TAILQ_INSERT_HEAD(&ifp->if_addrhead, ifa, ifa_link);
746 /* Reliably crash if used uninitialized. */
747 ifp->if_broadcastaddr = NULL;
749 if (ifp->if_type == IFT_ETHER) {
750 ifp->if_hw_addr = malloc(ifp->if_addrlen, M_IFADDR,
754 #if defined(INET) || defined(INET6)
755 /* Use defaults for TSO, if nothing is set */
756 if (ifp->if_hw_tsomax == 0 &&
757 ifp->if_hw_tsomaxsegcount == 0 &&
758 ifp->if_hw_tsomaxsegsize == 0) {
760 * The TSO defaults needs to be such that an
761 * NFS mbuf list of 35 mbufs totalling just
762 * below 64K works and that a chain of mbufs
763 * can be defragged into at most 32 segments:
765 ifp->if_hw_tsomax = min(IP_MAXPACKET, (32 * MCLBYTES) -
766 (ETHER_HDR_LEN + ETHER_VLAN_ENCAP_LEN));
767 ifp->if_hw_tsomaxsegcount = 35;
768 ifp->if_hw_tsomaxsegsize = 2048; /* 2K */
770 /* XXX some drivers set IFCAP_TSO after ethernet attach */
771 if (ifp->if_capabilities & IFCAP_TSO) {
772 if_printf(ifp, "Using defaults for TSO: %u/%u/%u\n",
774 ifp->if_hw_tsomaxsegcount,
775 ifp->if_hw_tsomaxsegsize);
783 * Update the interface index in the link layer address
786 for (ifa = ifp->if_addr; ifa != NULL;
787 ifa = TAILQ_NEXT(ifa, ifa_link)) {
788 if (ifa->ifa_addr->sa_family == AF_LINK) {
789 sdl = (struct sockaddr_dl *)ifa->ifa_addr;
790 sdl->sdl_index = ifp->if_index;
797 TAILQ_INSERT_TAIL(&V_ifnet, ifp, if_link);
799 curvnet->vnet_ifcnt++;
803 if (domain_init_status >= 2)
804 if_attachdomain1(ifp);
806 EVENTHANDLER_INVOKE(ifnet_arrival_event, ifp);
807 if (IS_DEFAULT_VNET(curvnet))
808 devctl_notify("IFNET", ifp->if_xname, "ATTACH", NULL);
810 /* Announce the interface. */
811 rt_ifannouncemsg(ifp, IFAN_ARRIVAL);
815 if_attachdomain(void *dummy)
819 TAILQ_FOREACH(ifp, &V_ifnet, if_link)
820 if_attachdomain1(ifp);
822 SYSINIT(domainifattach, SI_SUB_PROTO_IFATTACHDOMAIN, SI_ORDER_SECOND,
823 if_attachdomain, NULL);
826 if_attachdomain1(struct ifnet *ifp)
831 * Since dp->dom_ifattach calls malloc() with M_WAITOK, we
832 * cannot lock ifp->if_afdata initialization, entirely.
835 if (ifp->if_afdata_initialized >= domain_init_status) {
836 IF_AFDATA_UNLOCK(ifp);
837 log(LOG_WARNING, "%s called more than once on %s\n",
838 __func__, ifp->if_xname);
841 ifp->if_afdata_initialized = domain_init_status;
842 IF_AFDATA_UNLOCK(ifp);
844 /* address family dependent data region */
845 bzero(ifp->if_afdata, sizeof(ifp->if_afdata));
846 for (dp = domains; dp; dp = dp->dom_next) {
847 if (dp->dom_ifattach)
848 ifp->if_afdata[dp->dom_family] =
849 (*dp->dom_ifattach)(ifp);
854 * Remove any unicast or broadcast network addresses from an interface.
857 if_purgeaddrs(struct ifnet *ifp)
859 struct ifaddr *ifa, *next;
861 /* XXX cannot hold IF_ADDR_WLOCK over called functions. */
862 TAILQ_FOREACH_SAFE(ifa, &ifp->if_addrhead, ifa_link, next) {
863 if (ifa->ifa_addr->sa_family == AF_LINK)
866 /* XXX: Ugly!! ad hoc just for INET */
867 if (ifa->ifa_addr->sa_family == AF_INET) {
868 struct ifaliasreq ifr;
870 bzero(&ifr, sizeof(ifr));
871 ifr.ifra_addr = *ifa->ifa_addr;
872 if (ifa->ifa_dstaddr)
873 ifr.ifra_broadaddr = *ifa->ifa_dstaddr;
874 if (in_control(NULL, SIOCDIFADDR, (caddr_t)&ifr, ifp,
880 if (ifa->ifa_addr->sa_family == AF_INET6) {
882 /* ifp_addrhead is already updated */
887 TAILQ_REMOVE(&ifp->if_addrhead, ifa, ifa_link);
888 IF_ADDR_WUNLOCK(ifp);
894 * Remove any multicast network addresses from an interface when an ifnet
898 if_purgemaddrs(struct ifnet *ifp)
900 struct ifmultiaddr *ifma;
901 struct ifmultiaddr *next;
904 TAILQ_FOREACH_SAFE(ifma, &ifp->if_multiaddrs, ifma_link, next)
905 if_delmulti_locked(ifp, ifma, 1);
906 IF_ADDR_WUNLOCK(ifp);
910 * Detach an interface, removing it from the list of "active" interfaces.
911 * If vmove flag is set on entry to if_detach_internal(), perform only a
912 * limited subset of cleanup tasks, given that we are moving an ifnet from
913 * one vnet to another, where it must be fully operational.
915 * XXXRW: There are some significant questions about event ordering, and
916 * how to prevent things from starting to use the interface during detach.
919 if_detach(struct ifnet *ifp)
922 CURVNET_SET_QUIET(ifp->if_vnet);
923 if_detach_internal(ifp, 0, NULL);
928 * The vmove flag, if set, indicates that we are called from a callpath
929 * that is moving an interface to a different vnet instance.
931 * The shutdown flag, if set, indicates that we are called in the
932 * process of shutting down a vnet instance. Currently only the
933 * vnet_if_return SYSUNINIT function sets it. Note: we can be called
934 * on a vnet instance shutdown without this flag being set, e.g., when
935 * the cloned interfaces are destoyed as first thing of teardown.
938 if_detach_internal(struct ifnet *ifp, int vmove, struct if_clone **ifcp)
948 shutdown = (ifp->if_vnet->vnet_state > SI_SUB_VNET &&
949 ifp->if_vnet->vnet_state < SI_SUB_VNET_DONE) ? 1 : 0;
952 TAILQ_FOREACH(iter, &V_ifnet, if_link)
954 TAILQ_REMOVE(&V_ifnet, ifp, if_link);
961 * While we would want to panic here, we cannot
962 * guarantee that the interface is indeed still on
963 * the list given we don't hold locks all the way.
968 panic("%s: ifp=%p not on the ifnet tailq %p",
969 __func__, ifp, &V_ifnet);
971 return; /* XXX this should panic as well? */
976 * At this point we know the interface still was on the ifnet list
977 * and we removed it so we are in a stable state.
980 curvnet->vnet_ifcnt--;
984 * In any case (destroy or vmove) detach us from the groups
985 * and remove/wait for pending events on the taskq.
986 * XXX-BZ in theory an interface could still enqueue a taskq change?
990 taskqueue_drain(taskqueue_swi, &ifp->if_linktask);
993 * Check if this is a cloned interface or not. Must do even if
994 * shutting down as a if_vmove_reclaim() would move the ifp and
995 * the if_clone_addgroup() will have a corrupted string overwise
996 * from a gibberish pointer.
998 if (vmove && ifcp != NULL)
999 *ifcp = if_clone_findifc(ifp);
1005 * On VNET shutdown abort here as the stack teardown will do all
1006 * the work top-down for us.
1010 * In case of a vmove we are done here without error.
1011 * If we would signal an error it would lead to the same
1012 * abort as if we did not find the ifnet anymore.
1013 * if_detach() calls us in void context and does not care
1014 * about an early abort notification, so life is splendid :)
1016 goto finish_vnet_shutdown;
1021 * At this point we are not tearing down a VNET and are either
1022 * going to destroy or vmove the interface and have to cleanup
1027 * Remove routes and flush queues.
1030 if (ALTQ_IS_ENABLED(&ifp->if_snd))
1031 altq_disable(&ifp->if_snd);
1032 if (ALTQ_IS_ATTACHED(&ifp->if_snd))
1033 altq_detach(&ifp->if_snd);
1044 * Remove all IPv6 kernel structs related to ifp. This should be done
1045 * before removing routing entries below, since IPv6 interface direct
1046 * routes are expected to be removed by the IPv6-specific kernel API.
1047 * Otherwise, the kernel will detect some inconsistency and bark it.
1051 if_purgemaddrs(ifp);
1053 /* Announce that the interface is gone. */
1054 rt_ifannouncemsg(ifp, IFAN_DEPARTURE);
1055 EVENTHANDLER_INVOKE(ifnet_departure_event, ifp);
1056 if (IS_DEFAULT_VNET(curvnet))
1057 devctl_notify("IFNET", ifp->if_xname, "DETACH", NULL);
1061 * Prevent further calls into the device driver via ifnet.
1066 * Remove link ifaddr pointer and maybe decrement if_index.
1067 * Clean up all addresses.
1069 free(ifp->if_hw_addr, M_IFADDR);
1070 ifp->if_hw_addr = NULL;
1071 ifp->if_addr = NULL;
1073 /* We can now free link ifaddr. */
1075 if (!TAILQ_EMPTY(&ifp->if_addrhead)) {
1076 ifa = TAILQ_FIRST(&ifp->if_addrhead);
1077 TAILQ_REMOVE(&ifp->if_addrhead, ifa, ifa_link);
1078 IF_ADDR_WUNLOCK(ifp);
1081 IF_ADDR_WUNLOCK(ifp);
1084 rt_flushifroutes(ifp);
1087 finish_vnet_shutdown:
1090 * We cannot hold the lock over dom_ifdetach calls as they might
1091 * sleep, for example trying to drain a callout, thus open up the
1092 * theoretical race with re-attaching.
1094 IF_AFDATA_LOCK(ifp);
1095 i = ifp->if_afdata_initialized;
1096 ifp->if_afdata_initialized = 0;
1097 IF_AFDATA_UNLOCK(ifp);
1098 for (dp = domains; i > 0 && dp; dp = dp->dom_next) {
1099 if (dp->dom_ifdetach && ifp->if_afdata[dp->dom_family]) {
1100 (*dp->dom_ifdetach)(ifp,
1101 ifp->if_afdata[dp->dom_family]);
1102 ifp->if_afdata[dp->dom_family] = NULL;
1111 * if_vmove() performs a limited version of if_detach() in current
1112 * vnet and if_attach()es the ifnet to the vnet specified as 2nd arg.
1113 * An attempt is made to shrink if_index in current vnet, find an
1114 * unused if_index in target vnet and calls if_grow() if necessary,
1115 * and finally find an unused if_xname for the target vnet.
1118 if_vmove(struct ifnet *ifp, struct vnet *new_vnet)
1120 struct if_clone *ifc;
1121 u_int bif_dlt, bif_hdrlen;
1125 * if_detach_internal() will call the eventhandler to notify
1126 * interface departure. That will detach if_bpf. We need to
1127 * safe the dlt and hdrlen so we can re-attach it later.
1129 bpf_get_bp_params(ifp->if_bpf, &bif_dlt, &bif_hdrlen);
1132 * Detach from current vnet, but preserve LLADDR info, do not
1133 * mark as dead etc. so that the ifnet can be reattached later.
1134 * If we cannot find it, we lost the race to someone else.
1136 rc = if_detach_internal(ifp, 1, &ifc);
1141 * Unlink the ifnet from ifindex_table[] in current vnet, and shrink
1142 * the if_index for that vnet if possible.
1144 * NOTE: IFNET_WLOCK/IFNET_WUNLOCK() are assumed to be unvirtualized,
1145 * or we'd lock on one vnet and unlock on another.
1148 ifindex_free_locked(ifp->if_index);
1152 * Perform interface-specific reassignment tasks, if provided by
1155 if (ifp->if_reassign != NULL)
1156 ifp->if_reassign(ifp, new_vnet, NULL);
1159 * Switch to the context of the target vnet.
1161 CURVNET_SET_QUIET(new_vnet);
1164 ifp->if_index = ifindex_alloc();
1165 ifnet_setbyindex_locked(ifp->if_index, ifp);
1168 if_attach_internal(ifp, 1, ifc);
1170 if (ifp->if_bpf == NULL)
1171 bpfattach(ifp, bif_dlt, bif_hdrlen);
1177 * Move an ifnet to or from another child prison/vnet, specified by the jail id.
1180 if_vmove_loan(struct thread *td, struct ifnet *ifp, char *ifname, int jid)
1186 /* Try to find the prison within our visibility. */
1187 sx_slock(&allprison_lock);
1188 pr = prison_find_child(td->td_ucred->cr_prison, jid);
1189 sx_sunlock(&allprison_lock);
1192 prison_hold_locked(pr);
1193 mtx_unlock(&pr->pr_mtx);
1195 /* Do not try to move the iface from and to the same prison. */
1196 if (pr->pr_vnet == ifp->if_vnet) {
1201 /* Make sure the named iface does not exists in the dst. prison/vnet. */
1202 /* XXX Lock interfaces to avoid races. */
1203 CURVNET_SET_QUIET(pr->pr_vnet);
1204 difp = ifunit(ifname);
1211 /* Make sure the VNET is stable. */
1212 shutdown = (ifp->if_vnet->vnet_state > SI_SUB_VNET &&
1213 ifp->if_vnet->vnet_state < SI_SUB_VNET_DONE) ? 1 : 0;
1221 /* Move the interface into the child jail/vnet. */
1222 if_vmove(ifp, pr->pr_vnet);
1224 /* Report the new if_xname back to the userland. */
1225 sprintf(ifname, "%s", ifp->if_xname);
1232 if_vmove_reclaim(struct thread *td, char *ifname, int jid)
1235 struct vnet *vnet_dst;
1239 /* Try to find the prison within our visibility. */
1240 sx_slock(&allprison_lock);
1241 pr = prison_find_child(td->td_ucred->cr_prison, jid);
1242 sx_sunlock(&allprison_lock);
1245 prison_hold_locked(pr);
1246 mtx_unlock(&pr->pr_mtx);
1248 /* Make sure the named iface exists in the source prison/vnet. */
1249 CURVNET_SET(pr->pr_vnet);
1250 ifp = ifunit(ifname); /* XXX Lock to avoid races. */
1257 /* Do not try to move the iface from and to the same prison. */
1258 vnet_dst = TD_TO_VNET(td);
1259 if (vnet_dst == ifp->if_vnet) {
1265 /* Make sure the VNET is stable. */
1266 shutdown = (ifp->if_vnet->vnet_state > SI_SUB_VNET &&
1267 ifp->if_vnet->vnet_state < SI_SUB_VNET_DONE) ? 1 : 0;
1274 /* Get interface back from child jail/vnet. */
1275 if_vmove(ifp, vnet_dst);
1278 /* Report the new if_xname back to the userland. */
1279 sprintf(ifname, "%s", ifp->if_xname);
1287 * Add a group to an interface
1290 if_addgroup(struct ifnet *ifp, const char *groupname)
1292 struct ifg_list *ifgl;
1293 struct ifg_group *ifg = NULL;
1294 struct ifg_member *ifgm;
1297 if (groupname[0] && groupname[strlen(groupname) - 1] >= '0' &&
1298 groupname[strlen(groupname) - 1] <= '9')
1302 TAILQ_FOREACH(ifgl, &ifp->if_groups, ifgl_next)
1303 if (!strcmp(ifgl->ifgl_group->ifg_group, groupname)) {
1308 if ((ifgl = (struct ifg_list *)malloc(sizeof(struct ifg_list), M_TEMP,
1309 M_NOWAIT)) == NULL) {
1314 if ((ifgm = (struct ifg_member *)malloc(sizeof(struct ifg_member),
1315 M_TEMP, M_NOWAIT)) == NULL) {
1321 TAILQ_FOREACH(ifg, &V_ifg_head, ifg_next)
1322 if (!strcmp(ifg->ifg_group, groupname))
1326 if ((ifg = (struct ifg_group *)malloc(sizeof(struct ifg_group),
1327 M_TEMP, M_NOWAIT)) == NULL) {
1333 strlcpy(ifg->ifg_group, groupname, sizeof(ifg->ifg_group));
1334 ifg->ifg_refcnt = 0;
1335 TAILQ_INIT(&ifg->ifg_members);
1336 TAILQ_INSERT_TAIL(&V_ifg_head, ifg, ifg_next);
1341 ifgl->ifgl_group = ifg;
1342 ifgm->ifgm_ifp = ifp;
1345 TAILQ_INSERT_TAIL(&ifg->ifg_members, ifgm, ifgm_next);
1346 TAILQ_INSERT_TAIL(&ifp->if_groups, ifgl, ifgl_next);
1347 IF_ADDR_WUNLOCK(ifp);
1352 EVENTHANDLER_INVOKE(group_attach_event, ifg);
1353 EVENTHANDLER_INVOKE(group_change_event, groupname);
1359 * Remove a group from an interface
1362 if_delgroup(struct ifnet *ifp, const char *groupname)
1364 struct ifg_list *ifgl;
1365 struct ifg_member *ifgm;
1368 TAILQ_FOREACH(ifgl, &ifp->if_groups, ifgl_next)
1369 if (!strcmp(ifgl->ifgl_group->ifg_group, groupname))
1377 TAILQ_REMOVE(&ifp->if_groups, ifgl, ifgl_next);
1378 IF_ADDR_WUNLOCK(ifp);
1380 TAILQ_FOREACH(ifgm, &ifgl->ifgl_group->ifg_members, ifgm_next)
1381 if (ifgm->ifgm_ifp == ifp)
1385 TAILQ_REMOVE(&ifgl->ifgl_group->ifg_members, ifgm, ifgm_next);
1389 if (--ifgl->ifgl_group->ifg_refcnt == 0) {
1390 TAILQ_REMOVE(&V_ifg_head, ifgl->ifgl_group, ifg_next);
1392 EVENTHANDLER_INVOKE(group_detach_event, ifgl->ifgl_group);
1393 free(ifgl->ifgl_group, M_TEMP);
1399 EVENTHANDLER_INVOKE(group_change_event, groupname);
1405 * Remove an interface from all groups
1408 if_delgroups(struct ifnet *ifp)
1410 struct ifg_list *ifgl;
1411 struct ifg_member *ifgm;
1412 char groupname[IFNAMSIZ];
1415 while (!TAILQ_EMPTY(&ifp->if_groups)) {
1416 ifgl = TAILQ_FIRST(&ifp->if_groups);
1418 strlcpy(groupname, ifgl->ifgl_group->ifg_group, IFNAMSIZ);
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,
1434 if (--ifgl->ifgl_group->ifg_refcnt == 0) {
1435 TAILQ_REMOVE(&V_ifg_head, ifgl->ifgl_group, ifg_next);
1437 EVENTHANDLER_INVOKE(group_detach_event,
1439 free(ifgl->ifgl_group, M_TEMP);
1445 EVENTHANDLER_INVOKE(group_change_event, groupname);
1453 * Stores all groups from an interface in memory pointed
1457 if_getgroup(struct ifgroupreq *data, struct ifnet *ifp)
1460 struct ifg_list *ifgl;
1461 struct ifg_req ifgrq, *ifgp;
1462 struct ifgroupreq *ifgr = data;
1464 if (ifgr->ifgr_len == 0) {
1466 TAILQ_FOREACH(ifgl, &ifp->if_groups, ifgl_next)
1467 ifgr->ifgr_len += sizeof(struct ifg_req);
1468 IF_ADDR_RUNLOCK(ifp);
1472 len = ifgr->ifgr_len;
1473 ifgp = ifgr->ifgr_groups;
1476 TAILQ_FOREACH(ifgl, &ifp->if_groups, ifgl_next) {
1477 if (len < sizeof(ifgrq)) {
1478 IF_ADDR_RUNLOCK(ifp);
1481 bzero(&ifgrq, sizeof ifgrq);
1482 strlcpy(ifgrq.ifgrq_group, ifgl->ifgl_group->ifg_group,
1483 sizeof(ifgrq.ifgrq_group));
1484 if ((error = copyout(&ifgrq, ifgp, sizeof(struct ifg_req)))) {
1485 IF_ADDR_RUNLOCK(ifp);
1488 len -= sizeof(ifgrq);
1491 IF_ADDR_RUNLOCK(ifp);
1497 * Stores all members of a group in memory pointed to by data
1500 if_getgroupmembers(struct ifgroupreq *data)
1502 struct ifgroupreq *ifgr = data;
1503 struct ifg_group *ifg;
1504 struct ifg_member *ifgm;
1505 struct ifg_req ifgrq, *ifgp;
1509 TAILQ_FOREACH(ifg, &V_ifg_head, ifg_next)
1510 if (!strcmp(ifg->ifg_group, ifgr->ifgr_name))
1517 if (ifgr->ifgr_len == 0) {
1518 TAILQ_FOREACH(ifgm, &ifg->ifg_members, ifgm_next)
1519 ifgr->ifgr_len += sizeof(ifgrq);
1524 len = ifgr->ifgr_len;
1525 ifgp = ifgr->ifgr_groups;
1526 TAILQ_FOREACH(ifgm, &ifg->ifg_members, ifgm_next) {
1527 if (len < sizeof(ifgrq)) {
1531 bzero(&ifgrq, sizeof ifgrq);
1532 strlcpy(ifgrq.ifgrq_member, ifgm->ifgm_ifp->if_xname,
1533 sizeof(ifgrq.ifgrq_member));
1534 if ((error = copyout(&ifgrq, ifgp, sizeof(struct ifg_req)))) {
1538 len -= sizeof(ifgrq);
1547 * Return counter values from counter(9)s stored in ifnet.
1550 if_get_counter_default(struct ifnet *ifp, ift_counter cnt)
1553 KASSERT(cnt < IFCOUNTERS, ("%s: invalid cnt %d", __func__, cnt));
1555 return (counter_u64_fetch(ifp->if_counters[cnt]));
1559 * Increase an ifnet counter. Usually used for counters shared
1560 * between the stack and a driver, but function supports them all.
1563 if_inc_counter(struct ifnet *ifp, ift_counter cnt, int64_t inc)
1566 KASSERT(cnt < IFCOUNTERS, ("%s: invalid cnt %d", __func__, cnt));
1568 counter_u64_add(ifp->if_counters[cnt], inc);
1572 * Copy data from ifnet to userland API structure if_data.
1575 if_data_copy(struct ifnet *ifp, struct if_data *ifd)
1578 ifd->ifi_type = ifp->if_type;
1579 ifd->ifi_physical = 0;
1580 ifd->ifi_addrlen = ifp->if_addrlen;
1581 ifd->ifi_hdrlen = ifp->if_hdrlen;
1582 ifd->ifi_link_state = ifp->if_link_state;
1584 ifd->ifi_datalen = sizeof(struct if_data);
1585 ifd->ifi_mtu = ifp->if_mtu;
1586 ifd->ifi_metric = ifp->if_metric;
1587 ifd->ifi_baudrate = ifp->if_baudrate;
1588 ifd->ifi_hwassist = ifp->if_hwassist;
1589 ifd->ifi_epoch = ifp->if_epoch;
1590 ifd->ifi_lastchange = ifp->if_lastchange;
1592 ifd->ifi_ipackets = ifp->if_get_counter(ifp, IFCOUNTER_IPACKETS);
1593 ifd->ifi_ierrors = ifp->if_get_counter(ifp, IFCOUNTER_IERRORS);
1594 ifd->ifi_opackets = ifp->if_get_counter(ifp, IFCOUNTER_OPACKETS);
1595 ifd->ifi_oerrors = ifp->if_get_counter(ifp, IFCOUNTER_OERRORS);
1596 ifd->ifi_collisions = ifp->if_get_counter(ifp, IFCOUNTER_COLLISIONS);
1597 ifd->ifi_ibytes = ifp->if_get_counter(ifp, IFCOUNTER_IBYTES);
1598 ifd->ifi_obytes = ifp->if_get_counter(ifp, IFCOUNTER_OBYTES);
1599 ifd->ifi_imcasts = ifp->if_get_counter(ifp, IFCOUNTER_IMCASTS);
1600 ifd->ifi_omcasts = ifp->if_get_counter(ifp, IFCOUNTER_OMCASTS);
1601 ifd->ifi_iqdrops = ifp->if_get_counter(ifp, IFCOUNTER_IQDROPS);
1602 ifd->ifi_oqdrops = ifp->if_get_counter(ifp, IFCOUNTER_OQDROPS);
1603 ifd->ifi_noproto = ifp->if_get_counter(ifp, IFCOUNTER_NOPROTO);
1607 * Wrapper functions for struct ifnet address list locking macros. These are
1608 * used by kernel modules to avoid encoding programming interface or binary
1609 * interface assumptions that may be violated when kernel-internal locking
1610 * approaches change.
1613 if_addr_rlock(struct ifnet *ifp)
1620 if_addr_runlock(struct ifnet *ifp)
1623 IF_ADDR_RUNLOCK(ifp);
1627 if_maddr_rlock(if_t ifp)
1630 IF_ADDR_RLOCK((struct ifnet *)ifp);
1634 if_maddr_runlock(if_t ifp)
1637 IF_ADDR_RUNLOCK((struct ifnet *)ifp);
1641 * Initialization, destruction and refcounting functions for ifaddrs.
1644 ifa_alloc(size_t size, int flags)
1648 KASSERT(size >= sizeof(struct ifaddr),
1649 ("%s: invalid size %zu", __func__, size));
1651 ifa = malloc(size, M_IFADDR, M_ZERO | flags);
1655 if ((ifa->ifa_opackets = counter_u64_alloc(flags)) == NULL)
1657 if ((ifa->ifa_ipackets = counter_u64_alloc(flags)) == NULL)
1659 if ((ifa->ifa_obytes = counter_u64_alloc(flags)) == NULL)
1661 if ((ifa->ifa_ibytes = counter_u64_alloc(flags)) == NULL)
1664 refcount_init(&ifa->ifa_refcnt, 1);
1669 /* free(NULL) is okay */
1670 counter_u64_free(ifa->ifa_opackets);
1671 counter_u64_free(ifa->ifa_ipackets);
1672 counter_u64_free(ifa->ifa_obytes);
1673 counter_u64_free(ifa->ifa_ibytes);
1674 free(ifa, M_IFADDR);
1680 ifa_ref(struct ifaddr *ifa)
1683 refcount_acquire(&ifa->ifa_refcnt);
1687 ifa_free(struct ifaddr *ifa)
1690 if (refcount_release(&ifa->ifa_refcnt)) {
1691 counter_u64_free(ifa->ifa_opackets);
1692 counter_u64_free(ifa->ifa_ipackets);
1693 counter_u64_free(ifa->ifa_obytes);
1694 counter_u64_free(ifa->ifa_ibytes);
1695 free(ifa, M_IFADDR);
1700 ifa_maintain_loopback_route(int cmd, const char *otype, struct ifaddr *ifa,
1701 struct sockaddr *ia)
1704 struct rt_addrinfo info;
1705 struct sockaddr_dl null_sdl;
1710 bzero(&info, sizeof(info));
1711 if (cmd != RTM_DELETE)
1712 info.rti_ifp = V_loif;
1713 info.rti_flags = ifa->ifa_flags | RTF_HOST | RTF_STATIC | RTF_PINNED;
1714 info.rti_info[RTAX_DST] = ia;
1715 info.rti_info[RTAX_GATEWAY] = (struct sockaddr *)&null_sdl;
1716 link_init_sdl(ifp, (struct sockaddr *)&null_sdl, ifp->if_type);
1718 error = rtrequest1_fib(cmd, &info, NULL, ifp->if_fib);
1721 log(LOG_DEBUG, "%s: %s failed for interface %s: %u\n",
1722 __func__, otype, if_name(ifp), error);
1728 ifa_add_loopback_route(struct ifaddr *ifa, struct sockaddr *ia)
1731 return (ifa_maintain_loopback_route(RTM_ADD, "insertion", ifa, ia));
1735 ifa_del_loopback_route(struct ifaddr *ifa, struct sockaddr *ia)
1738 return (ifa_maintain_loopback_route(RTM_DELETE, "deletion", ifa, ia));
1742 ifa_switch_loopback_route(struct ifaddr *ifa, struct sockaddr *ia)
1745 return (ifa_maintain_loopback_route(RTM_CHANGE, "switch", ifa, ia));
1749 * XXX: Because sockaddr_dl has deeper structure than the sockaddr
1750 * structs used to represent other address families, it is necessary
1751 * to perform a different comparison.
1754 #define sa_dl_equal(a1, a2) \
1755 ((((const struct sockaddr_dl *)(a1))->sdl_len == \
1756 ((const struct sockaddr_dl *)(a2))->sdl_len) && \
1757 (bcmp(CLLADDR((const struct sockaddr_dl *)(a1)), \
1758 CLLADDR((const struct sockaddr_dl *)(a2)), \
1759 ((const struct sockaddr_dl *)(a1))->sdl_alen) == 0))
1762 * Locate an interface based on a complete address.
1765 static struct ifaddr *
1766 ifa_ifwithaddr_internal(const struct sockaddr *addr, int getref)
1771 IFNET_RLOCK_NOSLEEP();
1772 TAILQ_FOREACH(ifp, &V_ifnet, if_link) {
1774 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
1775 if (ifa->ifa_addr->sa_family != addr->sa_family)
1777 if (sa_equal(addr, ifa->ifa_addr)) {
1780 IF_ADDR_RUNLOCK(ifp);
1783 /* IP6 doesn't have broadcast */
1784 if ((ifp->if_flags & IFF_BROADCAST) &&
1785 ifa->ifa_broadaddr &&
1786 ifa->ifa_broadaddr->sa_len != 0 &&
1787 sa_equal(ifa->ifa_broadaddr, addr)) {
1790 IF_ADDR_RUNLOCK(ifp);
1794 IF_ADDR_RUNLOCK(ifp);
1798 IFNET_RUNLOCK_NOSLEEP();
1803 ifa_ifwithaddr(const struct sockaddr *addr)
1806 return (ifa_ifwithaddr_internal(addr, 1));
1810 ifa_ifwithaddr_check(const struct sockaddr *addr)
1813 return (ifa_ifwithaddr_internal(addr, 0) != NULL);
1817 * Locate an interface based on the broadcast address.
1821 ifa_ifwithbroadaddr(const struct sockaddr *addr, int fibnum)
1826 IFNET_RLOCK_NOSLEEP();
1827 TAILQ_FOREACH(ifp, &V_ifnet, if_link) {
1828 if ((fibnum != RT_ALL_FIBS) && (ifp->if_fib != fibnum))
1831 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
1832 if (ifa->ifa_addr->sa_family != addr->sa_family)
1834 if ((ifp->if_flags & IFF_BROADCAST) &&
1835 ifa->ifa_broadaddr &&
1836 ifa->ifa_broadaddr->sa_len != 0 &&
1837 sa_equal(ifa->ifa_broadaddr, addr)) {
1839 IF_ADDR_RUNLOCK(ifp);
1843 IF_ADDR_RUNLOCK(ifp);
1847 IFNET_RUNLOCK_NOSLEEP();
1852 * Locate the point to point interface with a given destination address.
1856 ifa_ifwithdstaddr(const struct sockaddr *addr, int fibnum)
1861 IFNET_RLOCK_NOSLEEP();
1862 TAILQ_FOREACH(ifp, &V_ifnet, if_link) {
1863 if ((ifp->if_flags & IFF_POINTOPOINT) == 0)
1865 if ((fibnum != RT_ALL_FIBS) && (ifp->if_fib != fibnum))
1868 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
1869 if (ifa->ifa_addr->sa_family != addr->sa_family)
1871 if (ifa->ifa_dstaddr != NULL &&
1872 sa_equal(addr, ifa->ifa_dstaddr)) {
1874 IF_ADDR_RUNLOCK(ifp);
1878 IF_ADDR_RUNLOCK(ifp);
1882 IFNET_RUNLOCK_NOSLEEP();
1887 * Find an interface on a specific network. If many, choice
1888 * is most specific found.
1891 ifa_ifwithnet(const struct sockaddr *addr, int ignore_ptp, int fibnum)
1895 struct ifaddr *ifa_maybe = NULL;
1896 u_int af = addr->sa_family;
1897 const char *addr_data = addr->sa_data, *cplim;
1900 * AF_LINK addresses can be looked up directly by their index number,
1901 * so do that if we can.
1903 if (af == AF_LINK) {
1904 const struct sockaddr_dl *sdl = (const struct sockaddr_dl *)addr;
1905 if (sdl->sdl_index && sdl->sdl_index <= V_if_index)
1906 return (ifaddr_byindex(sdl->sdl_index));
1910 * Scan though each interface, looking for ones that have addresses
1911 * in this address family and the requested fib. Maintain a reference
1912 * on ifa_maybe once we find one, as we release the IF_ADDR_RLOCK() that
1913 * kept it stable when we move onto the next interface.
1915 IFNET_RLOCK_NOSLEEP();
1916 TAILQ_FOREACH(ifp, &V_ifnet, if_link) {
1917 if ((fibnum != RT_ALL_FIBS) && (ifp->if_fib != fibnum))
1920 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
1921 const char *cp, *cp2, *cp3;
1923 if (ifa->ifa_addr->sa_family != af)
1925 if (af == AF_INET &&
1926 ifp->if_flags & IFF_POINTOPOINT && !ignore_ptp) {
1928 * This is a bit broken as it doesn't
1929 * take into account that the remote end may
1930 * be a single node in the network we are
1932 * The trouble is that we don't know the
1933 * netmask for the remote end.
1935 if (ifa->ifa_dstaddr != NULL &&
1936 sa_equal(addr, ifa->ifa_dstaddr)) {
1938 IF_ADDR_RUNLOCK(ifp);
1943 * Scan all the bits in the ifa's address.
1944 * If a bit dissagrees with what we are
1945 * looking for, mask it with the netmask
1946 * to see if it really matters.
1947 * (A byte at a time)
1949 if (ifa->ifa_netmask == 0)
1952 cp2 = ifa->ifa_addr->sa_data;
1953 cp3 = ifa->ifa_netmask->sa_data;
1954 cplim = ifa->ifa_netmask->sa_len
1955 + (char *)ifa->ifa_netmask;
1957 if ((*cp++ ^ *cp2++) & *cp3++)
1958 goto next; /* next address! */
1960 * If the netmask of what we just found
1961 * is more specific than what we had before
1962 * (if we had one), or if the virtual status
1963 * of new prefix is better than of the old one,
1964 * then remember the new one before continuing
1965 * to search for an even better one.
1967 if (ifa_maybe == NULL ||
1968 ifa_preferred(ifa_maybe, ifa) ||
1969 rn_refines((caddr_t)ifa->ifa_netmask,
1970 (caddr_t)ifa_maybe->ifa_netmask)) {
1971 if (ifa_maybe != NULL)
1972 ifa_free(ifa_maybe);
1978 IF_ADDR_RUNLOCK(ifp);
1983 IFNET_RUNLOCK_NOSLEEP();
1984 if (ifa_maybe != NULL)
1985 ifa_free(ifa_maybe);
1990 * Find an interface address specific to an interface best matching
1994 ifaof_ifpforaddr(const struct sockaddr *addr, struct ifnet *ifp)
1997 const char *cp, *cp2, *cp3;
1999 struct ifaddr *ifa_maybe = NULL;
2000 u_int af = addr->sa_family;
2005 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
2006 if (ifa->ifa_addr->sa_family != af)
2008 if (ifa_maybe == NULL)
2010 if (ifa->ifa_netmask == 0) {
2011 if (sa_equal(addr, ifa->ifa_addr) ||
2012 (ifa->ifa_dstaddr &&
2013 sa_equal(addr, ifa->ifa_dstaddr)))
2017 if (ifp->if_flags & IFF_POINTOPOINT) {
2018 if (sa_equal(addr, ifa->ifa_dstaddr))
2022 cp2 = ifa->ifa_addr->sa_data;
2023 cp3 = ifa->ifa_netmask->sa_data;
2024 cplim = ifa->ifa_netmask->sa_len + (char *)ifa->ifa_netmask;
2025 for (; cp3 < cplim; cp3++)
2026 if ((*cp++ ^ *cp2++) & *cp3)
2036 IF_ADDR_RUNLOCK(ifp);
2041 * See whether new ifa is better than current one:
2042 * 1) A non-virtual one is preferred over virtual.
2043 * 2) A virtual in master state preferred over any other state.
2045 * Used in several address selecting functions.
2048 ifa_preferred(struct ifaddr *cur, struct ifaddr *next)
2051 return (cur->ifa_carp && (!next->ifa_carp ||
2052 ((*carp_master_p)(next) && !(*carp_master_p)(cur))));
2055 #include <net/if_llatbl.h>
2058 * Default action when installing a route with a Link Level gateway.
2059 * Lookup an appropriate real ifa to point to.
2060 * This should be moved to /sys/net/link.c eventually.
2063 link_rtrequest(int cmd, struct rtentry *rt, struct rt_addrinfo *info)
2065 struct ifaddr *ifa, *oifa;
2066 struct sockaddr *dst;
2069 if (cmd != RTM_ADD || ((ifa = rt->rt_ifa) == NULL) ||
2070 ((ifp = ifa->ifa_ifp) == NULL) || ((dst = rt_key(rt)) == NULL))
2072 ifa = ifaof_ifpforaddr(dst, ifp);
2077 if (ifa->ifa_rtrequest && ifa->ifa_rtrequest != link_rtrequest)
2078 ifa->ifa_rtrequest(cmd, rt, info);
2082 struct sockaddr_dl *
2083 link_alloc_sdl(size_t size, int flags)
2086 return (malloc(size, M_TEMP, flags));
2090 link_free_sdl(struct sockaddr *sa)
2096 * Fills in given sdl with interface basic info.
2097 * Returns pointer to filled sdl.
2099 struct sockaddr_dl *
2100 link_init_sdl(struct ifnet *ifp, struct sockaddr *paddr, u_char iftype)
2102 struct sockaddr_dl *sdl;
2104 sdl = (struct sockaddr_dl *)paddr;
2105 memset(sdl, 0, sizeof(struct sockaddr_dl));
2106 sdl->sdl_len = sizeof(struct sockaddr_dl);
2107 sdl->sdl_family = AF_LINK;
2108 sdl->sdl_index = ifp->if_index;
2109 sdl->sdl_type = iftype;
2115 * Mark an interface down and notify protocols of
2119 if_unroute(struct ifnet *ifp, int flag, int fam)
2123 KASSERT(flag == IFF_UP, ("if_unroute: flag != IFF_UP"));
2125 ifp->if_flags &= ~flag;
2126 getmicrotime(&ifp->if_lastchange);
2127 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link)
2128 if (fam == PF_UNSPEC || (fam == ifa->ifa_addr->sa_family))
2129 pfctlinput(PRC_IFDOWN, ifa->ifa_addr);
2130 ifp->if_qflush(ifp);
2133 (*carp_linkstate_p)(ifp);
2138 * Mark an interface up and notify protocols of
2142 if_route(struct ifnet *ifp, int flag, int fam)
2146 KASSERT(flag == IFF_UP, ("if_route: flag != IFF_UP"));
2148 ifp->if_flags |= flag;
2149 getmicrotime(&ifp->if_lastchange);
2150 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link)
2151 if (fam == PF_UNSPEC || (fam == ifa->ifa_addr->sa_family))
2152 pfctlinput(PRC_IFUP, ifa->ifa_addr);
2154 (*carp_linkstate_p)(ifp);
2161 void (*vlan_link_state_p)(struct ifnet *); /* XXX: private from if_vlan */
2162 void (*vlan_trunk_cap_p)(struct ifnet *); /* XXX: private from if_vlan */
2163 struct ifnet *(*vlan_trunkdev_p)(struct ifnet *);
2164 struct ifnet *(*vlan_devat_p)(struct ifnet *, uint16_t);
2165 int (*vlan_tag_p)(struct ifnet *, uint16_t *);
2166 int (*vlan_setcookie_p)(struct ifnet *, void *);
2167 void *(*vlan_cookie_p)(struct ifnet *);
2170 * Handle a change in the interface link state. To avoid LORs
2171 * between driver lock and upper layer locks, as well as possible
2172 * recursions, we post event to taskqueue, and all job
2173 * is done in static do_link_state_change().
2176 if_link_state_change(struct ifnet *ifp, int link_state)
2178 /* Return if state hasn't changed. */
2179 if (ifp->if_link_state == link_state)
2182 ifp->if_link_state = link_state;
2184 taskqueue_enqueue(taskqueue_swi, &ifp->if_linktask);
2188 do_link_state_change(void *arg, int pending)
2190 struct ifnet *ifp = (struct ifnet *)arg;
2191 int link_state = ifp->if_link_state;
2192 CURVNET_SET(ifp->if_vnet);
2194 /* Notify that the link state has changed. */
2196 if (ifp->if_vlantrunk != NULL)
2197 (*vlan_link_state_p)(ifp);
2199 if ((ifp->if_type == IFT_ETHER || ifp->if_type == IFT_L2VLAN) &&
2200 ifp->if_l2com != NULL)
2201 (*ng_ether_link_state_p)(ifp, link_state);
2203 (*carp_linkstate_p)(ifp);
2205 (*bridge_linkstate_p)(ifp);
2207 (*lagg_linkstate_p)(ifp, link_state);
2209 if (IS_DEFAULT_VNET(curvnet))
2210 devctl_notify("IFNET", ifp->if_xname,
2211 (link_state == LINK_STATE_UP) ? "LINK_UP" : "LINK_DOWN",
2214 if_printf(ifp, "%d link states coalesced\n", pending);
2215 if (log_link_state_change)
2216 log(LOG_NOTICE, "%s: link state changed to %s\n", ifp->if_xname,
2217 (link_state == LINK_STATE_UP) ? "UP" : "DOWN" );
2218 EVENTHANDLER_INVOKE(ifnet_link_event, ifp, link_state);
2223 * Mark an interface down and notify protocols of
2227 if_down(struct ifnet *ifp)
2230 EVENTHANDLER_INVOKE(ifnet_event, ifp, IFNET_EVENT_DOWN);
2231 if_unroute(ifp, IFF_UP, AF_UNSPEC);
2235 * Mark an interface up and notify protocols of
2239 if_up(struct ifnet *ifp)
2242 if_route(ifp, IFF_UP, AF_UNSPEC);
2243 EVENTHANDLER_INVOKE(ifnet_event, ifp, IFNET_EVENT_UP);
2247 * Flush an interface queue.
2250 if_qflush(struct ifnet *ifp)
2258 if (ALTQ_IS_ENABLED(ifq))
2262 while ((m = n) != NULL) {
2273 * Map interface name to interface structure pointer, with or without
2274 * returning a reference.
2277 ifunit_ref(const char *name)
2281 IFNET_RLOCK_NOSLEEP();
2282 TAILQ_FOREACH(ifp, &V_ifnet, if_link) {
2283 if (strncmp(name, ifp->if_xname, IFNAMSIZ) == 0 &&
2284 !(ifp->if_flags & IFF_DYING))
2289 IFNET_RUNLOCK_NOSLEEP();
2294 ifunit(const char *name)
2298 IFNET_RLOCK_NOSLEEP();
2299 TAILQ_FOREACH(ifp, &V_ifnet, if_link) {
2300 if (strncmp(name, ifp->if_xname, IFNAMSIZ) == 0)
2303 IFNET_RUNLOCK_NOSLEEP();
2308 * Hardware specific interface ioctls.
2311 ifhwioctl(u_long cmd, struct ifnet *ifp, caddr_t data, struct thread *td)
2314 int error = 0, do_ifup = 0;
2315 int new_flags, temp_flags;
2316 size_t namelen, onamelen;
2318 char *descrbuf, *odescrbuf;
2319 char new_name[IFNAMSIZ];
2321 struct sockaddr_dl *sdl;
2323 ifr = (struct ifreq *)data;
2326 ifr->ifr_index = ifp->if_index;
2330 temp_flags = ifp->if_flags | ifp->if_drv_flags;
2331 ifr->ifr_flags = temp_flags & 0xffff;
2332 ifr->ifr_flagshigh = temp_flags >> 16;
2336 ifr->ifr_reqcap = ifp->if_capabilities;
2337 ifr->ifr_curcap = ifp->if_capenable;
2342 error = mac_ifnet_ioctl_get(td->td_ucred, ifr, ifp);
2347 ifr->ifr_metric = ifp->if_metric;
2351 ifr->ifr_mtu = ifp->if_mtu;
2355 /* XXXGL: did this ever worked? */
2361 sx_slock(&ifdescr_sx);
2362 if (ifp->if_description == NULL)
2365 /* space for terminating nul */
2366 descrlen = strlen(ifp->if_description) + 1;
2367 if (ifr->ifr_buffer.length < descrlen)
2368 ifr->ifr_buffer.buffer = NULL;
2370 error = copyout(ifp->if_description,
2371 ifr->ifr_buffer.buffer, descrlen);
2372 ifr->ifr_buffer.length = descrlen;
2374 sx_sunlock(&ifdescr_sx);
2378 error = priv_check(td, PRIV_NET_SETIFDESCR);
2383 * Copy only (length-1) bytes to make sure that
2384 * if_description is always nul terminated. The
2385 * length parameter is supposed to count the
2386 * terminating nul in.
2388 if (ifr->ifr_buffer.length > ifdescr_maxlen)
2389 return (ENAMETOOLONG);
2390 else if (ifr->ifr_buffer.length == 0)
2393 descrbuf = malloc(ifr->ifr_buffer.length, M_IFDESCR,
2395 error = copyin(ifr->ifr_buffer.buffer, descrbuf,
2396 ifr->ifr_buffer.length - 1);
2398 free(descrbuf, M_IFDESCR);
2403 sx_xlock(&ifdescr_sx);
2404 odescrbuf = ifp->if_description;
2405 ifp->if_description = descrbuf;
2406 sx_xunlock(&ifdescr_sx);
2408 getmicrotime(&ifp->if_lastchange);
2409 free(odescrbuf, M_IFDESCR);
2413 ifr->ifr_fib = ifp->if_fib;
2417 error = priv_check(td, PRIV_NET_SETIFFIB);
2420 if (ifr->ifr_fib >= rt_numfibs)
2423 ifp->if_fib = ifr->ifr_fib;
2427 error = priv_check(td, PRIV_NET_SETIFFLAGS);
2431 * Currently, no driver owned flags pass the IFF_CANTCHANGE
2432 * check, so we don't need special handling here yet.
2434 new_flags = (ifr->ifr_flags & 0xffff) |
2435 (ifr->ifr_flagshigh << 16);
2436 if (ifp->if_flags & IFF_UP &&
2437 (new_flags & IFF_UP) == 0) {
2439 } else if (new_flags & IFF_UP &&
2440 (ifp->if_flags & IFF_UP) == 0) {
2443 /* See if permanently promiscuous mode bit is about to flip */
2444 if ((ifp->if_flags ^ new_flags) & IFF_PPROMISC) {
2445 if (new_flags & IFF_PPROMISC)
2446 ifp->if_flags |= IFF_PROMISC;
2447 else if (ifp->if_pcount == 0)
2448 ifp->if_flags &= ~IFF_PROMISC;
2449 if (log_promisc_mode_change)
2450 log(LOG_INFO, "%s: permanently promiscuous mode %s\n",
2452 ((new_flags & IFF_PPROMISC) ?
2453 "enabled" : "disabled"));
2455 ifp->if_flags = (ifp->if_flags & IFF_CANTCHANGE) |
2456 (new_flags &~ IFF_CANTCHANGE);
2457 if (ifp->if_ioctl) {
2458 (void) (*ifp->if_ioctl)(ifp, cmd, data);
2462 getmicrotime(&ifp->if_lastchange);
2466 error = priv_check(td, PRIV_NET_SETIFCAP);
2469 if (ifp->if_ioctl == NULL)
2470 return (EOPNOTSUPP);
2471 if (ifr->ifr_reqcap & ~ifp->if_capabilities)
2473 error = (*ifp->if_ioctl)(ifp, cmd, data);
2475 getmicrotime(&ifp->if_lastchange);
2480 error = mac_ifnet_ioctl_set(td->td_ucred, ifr, ifp);
2485 error = priv_check(td, PRIV_NET_SETIFNAME);
2488 error = copyinstr(ifr->ifr_data, new_name, IFNAMSIZ, NULL);
2491 if (new_name[0] == '\0')
2493 if (new_name[IFNAMSIZ-1] != '\0') {
2494 new_name[IFNAMSIZ-1] = '\0';
2495 if (strlen(new_name) == IFNAMSIZ-1)
2498 if (ifunit(new_name) != NULL)
2502 * XXX: Locking. Nothing else seems to lock if_flags,
2503 * and there are numerous other races with the
2504 * ifunit() checks not being atomic with namespace
2505 * changes (renames, vmoves, if_attach, etc).
2507 ifp->if_flags |= IFF_RENAMING;
2509 /* Announce the departure of the interface. */
2510 rt_ifannouncemsg(ifp, IFAN_DEPARTURE);
2511 EVENTHANDLER_INVOKE(ifnet_departure_event, ifp);
2513 log(LOG_INFO, "%s: changing name to '%s'\n",
2514 ifp->if_xname, new_name);
2517 strlcpy(ifp->if_xname, new_name, sizeof(ifp->if_xname));
2519 sdl = (struct sockaddr_dl *)ifa->ifa_addr;
2520 namelen = strlen(new_name);
2521 onamelen = sdl->sdl_nlen;
2523 * Move the address if needed. This is safe because we
2524 * allocate space for a name of length IFNAMSIZ when we
2525 * create this in if_attach().
2527 if (namelen != onamelen) {
2528 bcopy(sdl->sdl_data + onamelen,
2529 sdl->sdl_data + namelen, sdl->sdl_alen);
2531 bcopy(new_name, sdl->sdl_data, namelen);
2532 sdl->sdl_nlen = namelen;
2533 sdl = (struct sockaddr_dl *)ifa->ifa_netmask;
2534 bzero(sdl->sdl_data, onamelen);
2535 while (namelen != 0)
2536 sdl->sdl_data[--namelen] = 0xff;
2537 IF_ADDR_WUNLOCK(ifp);
2539 EVENTHANDLER_INVOKE(ifnet_arrival_event, ifp);
2540 /* Announce the return of the interface. */
2541 rt_ifannouncemsg(ifp, IFAN_ARRIVAL);
2543 ifp->if_flags &= ~IFF_RENAMING;
2548 error = priv_check(td, PRIV_NET_SETIFVNET);
2551 error = if_vmove_loan(td, ifp, ifr->ifr_name, ifr->ifr_jid);
2556 error = priv_check(td, PRIV_NET_SETIFMETRIC);
2559 ifp->if_metric = ifr->ifr_metric;
2560 getmicrotime(&ifp->if_lastchange);
2564 error = priv_check(td, PRIV_NET_SETIFPHYS);
2567 if (ifp->if_ioctl == NULL)
2568 return (EOPNOTSUPP);
2569 error = (*ifp->if_ioctl)(ifp, cmd, data);
2571 getmicrotime(&ifp->if_lastchange);
2576 u_long oldmtu = ifp->if_mtu;
2578 error = priv_check(td, PRIV_NET_SETIFMTU);
2581 if (ifr->ifr_mtu < IF_MINMTU || ifr->ifr_mtu > IF_MAXMTU)
2583 if (ifp->if_ioctl == NULL)
2584 return (EOPNOTSUPP);
2585 error = (*ifp->if_ioctl)(ifp, cmd, data);
2587 getmicrotime(&ifp->if_lastchange);
2591 * If the link MTU changed, do network layer specific procedure.
2593 if (ifp->if_mtu != oldmtu) {
2604 if (cmd == SIOCADDMULTI)
2605 error = priv_check(td, PRIV_NET_ADDMULTI);
2607 error = priv_check(td, PRIV_NET_DELMULTI);
2611 /* Don't allow group membership on non-multicast interfaces. */
2612 if ((ifp->if_flags & IFF_MULTICAST) == 0)
2613 return (EOPNOTSUPP);
2615 /* Don't let users screw up protocols' entries. */
2616 if (ifr->ifr_addr.sa_family != AF_LINK)
2619 if (cmd == SIOCADDMULTI) {
2620 struct ifmultiaddr *ifma;
2623 * Userland is only permitted to join groups once
2624 * via the if_addmulti() KPI, because it cannot hold
2625 * struct ifmultiaddr * between calls. It may also
2626 * lose a race while we check if the membership
2630 ifma = if_findmulti(ifp, &ifr->ifr_addr);
2631 IF_ADDR_RUNLOCK(ifp);
2635 error = if_addmulti(ifp, &ifr->ifr_addr, &ifma);
2637 error = if_delmulti(ifp, &ifr->ifr_addr);
2640 getmicrotime(&ifp->if_lastchange);
2643 case SIOCSIFPHYADDR:
2644 case SIOCDIFPHYADDR:
2646 case SIOCSIFPHYADDR_IN6:
2649 case SIOCSIFGENERIC:
2650 error = priv_check(td, PRIV_NET_HWIOCTL);
2653 if (ifp->if_ioctl == NULL)
2654 return (EOPNOTSUPP);
2655 error = (*ifp->if_ioctl)(ifp, cmd, data);
2657 getmicrotime(&ifp->if_lastchange);
2661 case SIOCGIFPSRCADDR:
2662 case SIOCGIFPDSTADDR:
2665 case SIOCGIFGENERIC:
2667 case SIOCGIFRSSHASH:
2668 if (ifp->if_ioctl == NULL)
2669 return (EOPNOTSUPP);
2670 error = (*ifp->if_ioctl)(ifp, cmd, data);
2674 error = priv_check(td, PRIV_NET_SETLLADDR);
2677 error = if_setlladdr(ifp,
2678 ifr->ifr_addr.sa_data, ifr->ifr_addr.sa_len);
2682 error = if_gethwaddr(ifp, ifr);
2687 struct ifgroupreq *ifgr = (struct ifgroupreq *)ifr;
2689 error = priv_check(td, PRIV_NET_ADDIFGROUP);
2692 if ((error = if_addgroup(ifp, ifgr->ifgr_group)))
2698 if ((error = if_getgroup((struct ifgroupreq *)ifr, ifp)))
2704 struct ifgroupreq *ifgr = (struct ifgroupreq *)ifr;
2706 error = priv_check(td, PRIV_NET_DELIFGROUP);
2709 if ((error = if_delgroup(ifp, ifgr->ifgr_group)))
2721 #ifdef COMPAT_FREEBSD32
2729 #define SIOCGIFCONF32 _IOWR('i', 36, struct ifconf32)
2736 ifioctl(struct socket *so, u_long cmd, caddr_t data, struct thread *td)
2746 CURVNET_SET(so->so_vnet);
2748 /* Make sure the VNET is stable. */
2749 shutdown = (so->so_vnet->vnet_state > SI_SUB_VNET &&
2750 so->so_vnet->vnet_state < SI_SUB_VNET_DONE) ? 1 : 0;
2760 error = ifconf(cmd, data);
2764 #ifdef COMPAT_FREEBSD32
2767 struct ifconf32 *ifc32;
2770 ifc32 = (struct ifconf32 *)data;
2771 ifc.ifc_len = ifc32->ifc_len;
2772 ifc.ifc_buf = PTRIN(ifc32->ifc_buf);
2774 error = ifconf(SIOCGIFCONF, (void *)&ifc);
2777 ifc32->ifc_len = ifc.ifc_len;
2782 ifr = (struct ifreq *)data;
2787 error = priv_check(td, PRIV_NET_SETIFVNET);
2789 error = if_vmove_reclaim(td, ifr->ifr_name,
2796 error = priv_check(td, PRIV_NET_IFCREATE);
2798 error = if_clone_create(ifr->ifr_name,
2799 sizeof(ifr->ifr_name),
2800 cmd == SIOCIFCREATE2 ? ifr->ifr_data : NULL);
2804 error = priv_check(td, PRIV_NET_IFDESTROY);
2806 error = if_clone_destroy(ifr->ifr_name);
2810 case SIOCIFGCLONERS:
2811 error = if_clone_list((struct if_clonereq *)data);
2815 error = if_getgroupmembers((struct ifgroupreq *)data);
2818 #if defined(INET) || defined(INET6)
2821 if (carp_ioctl_p == NULL)
2822 error = EPROTONOSUPPORT;
2824 error = (*carp_ioctl_p)(ifr, cmd, td);
2830 ifp = ifunit_ref(ifr->ifr_name);
2836 error = ifhwioctl(cmd, ifp, data, td);
2837 if (error != ENOIOCTL) {
2843 oif_flags = ifp->if_flags;
2844 if (so->so_proto == NULL) {
2847 return (EOPNOTSUPP);
2851 * Pass the request on to the socket control method, and if the
2852 * latter returns EOPNOTSUPP, directly to the interface.
2854 * Make an exception for the legacy SIOCSIF* requests. Drivers
2855 * trust SIOCSIFADDR et al to come from an already privileged
2856 * layer, and do not perform any credentials checks or input
2859 error = ((*so->so_proto->pr_usrreqs->pru_control)(so, cmd, data,
2861 if (error == EOPNOTSUPP && ifp != NULL && ifp->if_ioctl != NULL &&
2862 cmd != SIOCSIFADDR && cmd != SIOCSIFBRDADDR &&
2863 cmd != SIOCSIFDSTADDR && cmd != SIOCSIFNETMASK)
2864 error = (*ifp->if_ioctl)(ifp, cmd, data);
2866 if ((oif_flags ^ ifp->if_flags) & IFF_UP) {
2868 if (ifp->if_flags & IFF_UP)
2878 * The code common to handling reference counted flags,
2879 * e.g., in ifpromisc() and if_allmulti().
2880 * The "pflag" argument can specify a permanent mode flag to check,
2881 * such as IFF_PPROMISC for promiscuous mode; should be 0 if none.
2883 * Only to be used on stack-owned flags, not driver-owned flags.
2886 if_setflag(struct ifnet *ifp, int flag, int pflag, int *refcount, int onswitch)
2890 int oldflags, oldcount;
2892 /* Sanity checks to catch programming errors */
2893 KASSERT((flag & (IFF_DRV_OACTIVE|IFF_DRV_RUNNING)) == 0,
2894 ("%s: setting driver-owned flag %d", __func__, flag));
2897 KASSERT(*refcount >= 0,
2898 ("%s: increment negative refcount %d for flag %d",
2899 __func__, *refcount, flag));
2901 KASSERT(*refcount > 0,
2902 ("%s: decrement non-positive refcount %d for flag %d",
2903 __func__, *refcount, flag));
2905 /* In case this mode is permanent, just touch refcount */
2906 if (ifp->if_flags & pflag) {
2907 *refcount += onswitch ? 1 : -1;
2911 /* Save ifnet parameters for if_ioctl() may fail */
2912 oldcount = *refcount;
2913 oldflags = ifp->if_flags;
2916 * See if we aren't the only and touching refcount is enough.
2917 * Actually toggle interface flag if we are the first or last.
2922 ifp->if_flags |= flag;
2926 ifp->if_flags &= ~flag;
2929 /* Call down the driver since we've changed interface flags */
2930 if (ifp->if_ioctl == NULL) {
2934 ifr.ifr_flags = ifp->if_flags & 0xffff;
2935 ifr.ifr_flagshigh = ifp->if_flags >> 16;
2936 error = (*ifp->if_ioctl)(ifp, SIOCSIFFLAGS, (caddr_t)&ifr);
2939 /* Notify userland that interface flags have changed */
2944 /* Recover after driver error */
2945 *refcount = oldcount;
2946 ifp->if_flags = oldflags;
2951 * Set/clear promiscuous mode on interface ifp based on the truth value
2952 * of pswitch. The calls are reference counted so that only the first
2953 * "on" request actually has an effect, as does the final "off" request.
2954 * Results are undefined if the "off" and "on" requests are not matched.
2957 ifpromisc(struct ifnet *ifp, int pswitch)
2960 int oldflags = ifp->if_flags;
2962 error = if_setflag(ifp, IFF_PROMISC, IFF_PPROMISC,
2963 &ifp->if_pcount, pswitch);
2964 /* If promiscuous mode status has changed, log a message */
2965 if (error == 0 && ((ifp->if_flags ^ oldflags) & IFF_PROMISC) &&
2966 log_promisc_mode_change)
2967 log(LOG_INFO, "%s: promiscuous mode %s\n",
2969 (ifp->if_flags & IFF_PROMISC) ? "enabled" : "disabled");
2974 * Return interface configuration
2975 * of system. List may be used
2976 * in later ioctl's (above) to get
2977 * other information.
2981 ifconf(u_long cmd, caddr_t data)
2983 struct ifconf *ifc = (struct ifconf *)data;
2988 int error, full = 0, valid_len, max_len;
2990 /* Limit initial buffer size to MAXPHYS to avoid DoS from userspace. */
2991 max_len = MAXPHYS - 1;
2993 /* Prevent hostile input from being able to crash the system */
2994 if (ifc->ifc_len <= 0)
2998 if (ifc->ifc_len <= max_len) {
2999 max_len = ifc->ifc_len;
3002 sb = sbuf_new(NULL, NULL, max_len + 1, SBUF_FIXEDLEN);
3007 TAILQ_FOREACH(ifp, &V_ifnet, if_link) {
3011 * Zero the ifr_name buffer to make sure we don't
3012 * disclose the contents of the stack.
3014 memset(ifr.ifr_name, 0, sizeof(ifr.ifr_name));
3016 if (strlcpy(ifr.ifr_name, ifp->if_xname, sizeof(ifr.ifr_name))
3017 >= sizeof(ifr.ifr_name)) {
3020 return (ENAMETOOLONG);
3025 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
3026 struct sockaddr *sa = ifa->ifa_addr;
3028 if (prison_if(curthread->td_ucred, sa) != 0)
3031 if (sa->sa_len <= sizeof(*sa)) {
3033 sbuf_bcat(sb, &ifr, sizeof(ifr));
3034 max_len += sizeof(ifr);
3037 offsetof(struct ifreq, ifr_addr));
3038 max_len += offsetof(struct ifreq, ifr_addr);
3039 sbuf_bcat(sb, sa, sa->sa_len);
3040 max_len += sa->sa_len;
3043 if (sbuf_error(sb) == 0)
3044 valid_len = sbuf_len(sb);
3046 IF_ADDR_RUNLOCK(ifp);
3048 bzero((caddr_t)&ifr.ifr_addr, sizeof(ifr.ifr_addr));
3049 sbuf_bcat(sb, &ifr, sizeof(ifr));
3050 max_len += sizeof(ifr);
3052 if (sbuf_error(sb) == 0)
3053 valid_len = sbuf_len(sb);
3059 * If we didn't allocate enough space (uncommon), try again. If
3060 * we have already allocated as much space as we are allowed,
3061 * return what we've got.
3063 if (valid_len != max_len && !full) {
3068 ifc->ifc_len = valid_len;
3070 error = copyout(sbuf_data(sb), ifc->ifc_req, ifc->ifc_len);
3076 * Just like ifpromisc(), but for all-multicast-reception mode.
3079 if_allmulti(struct ifnet *ifp, int onswitch)
3082 return (if_setflag(ifp, IFF_ALLMULTI, 0, &ifp->if_amcount, onswitch));
3085 struct ifmultiaddr *
3086 if_findmulti(struct ifnet *ifp, const struct sockaddr *sa)
3088 struct ifmultiaddr *ifma;
3090 IF_ADDR_LOCK_ASSERT(ifp);
3092 TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
3093 if (sa->sa_family == AF_LINK) {
3094 if (sa_dl_equal(ifma->ifma_addr, sa))
3097 if (sa_equal(ifma->ifma_addr, sa))
3106 * Allocate a new ifmultiaddr and initialize based on passed arguments. We
3107 * make copies of passed sockaddrs. The ifmultiaddr will not be added to
3108 * the ifnet multicast address list here, so the caller must do that and
3109 * other setup work (such as notifying the device driver). The reference
3110 * count is initialized to 1.
3112 static struct ifmultiaddr *
3113 if_allocmulti(struct ifnet *ifp, struct sockaddr *sa, struct sockaddr *llsa,
3116 struct ifmultiaddr *ifma;
3117 struct sockaddr *dupsa;
3119 ifma = malloc(sizeof *ifma, M_IFMADDR, mflags |
3124 dupsa = malloc(sa->sa_len, M_IFMADDR, mflags);
3125 if (dupsa == NULL) {
3126 free(ifma, M_IFMADDR);
3129 bcopy(sa, dupsa, sa->sa_len);
3130 ifma->ifma_addr = dupsa;
3132 ifma->ifma_ifp = ifp;
3133 ifma->ifma_refcount = 1;
3134 ifma->ifma_protospec = NULL;
3137 ifma->ifma_lladdr = NULL;
3141 dupsa = malloc(llsa->sa_len, M_IFMADDR, mflags);
3142 if (dupsa == NULL) {
3143 free(ifma->ifma_addr, M_IFMADDR);
3144 free(ifma, M_IFMADDR);
3147 bcopy(llsa, dupsa, llsa->sa_len);
3148 ifma->ifma_lladdr = dupsa;
3154 * if_freemulti: free ifmultiaddr structure and possibly attached related
3155 * addresses. The caller is responsible for implementing reference
3156 * counting, notifying the driver, handling routing messages, and releasing
3157 * any dependent link layer state.
3160 if_freemulti(struct ifmultiaddr *ifma)
3163 KASSERT(ifma->ifma_refcount == 0, ("if_freemulti: refcount %d",
3164 ifma->ifma_refcount));
3166 if (ifma->ifma_lladdr != NULL)
3167 free(ifma->ifma_lladdr, M_IFMADDR);
3168 free(ifma->ifma_addr, M_IFMADDR);
3169 free(ifma, M_IFMADDR);
3173 * Register an additional multicast address with a network interface.
3175 * - If the address is already present, bump the reference count on the
3176 * address and return.
3177 * - If the address is not link-layer, look up a link layer address.
3178 * - Allocate address structures for one or both addresses, and attach to the
3179 * multicast address list on the interface. If automatically adding a link
3180 * layer address, the protocol address will own a reference to the link
3181 * layer address, to be freed when it is freed.
3182 * - Notify the network device driver of an addition to the multicast address
3185 * 'sa' points to caller-owned memory with the desired multicast address.
3187 * 'retifma' will be used to return a pointer to the resulting multicast
3188 * address reference, if desired.
3191 if_addmulti(struct ifnet *ifp, struct sockaddr *sa,
3192 struct ifmultiaddr **retifma)
3194 struct ifmultiaddr *ifma, *ll_ifma;
3195 struct sockaddr *llsa;
3196 struct sockaddr_dl sdl;
3200 * If the address is already present, return a new reference to it;
3201 * otherwise, allocate storage and set up a new address.
3204 ifma = if_findmulti(ifp, sa);
3206 ifma->ifma_refcount++;
3207 if (retifma != NULL)
3209 IF_ADDR_WUNLOCK(ifp);
3214 * The address isn't already present; resolve the protocol address
3215 * into a link layer address, and then look that up, bump its
3216 * refcount or allocate an ifma for that also.
3217 * Most link layer resolving functions returns address data which
3218 * fits inside default sockaddr_dl structure. However callback
3219 * can allocate another sockaddr structure, in that case we need to
3224 if (ifp->if_resolvemulti != NULL) {
3225 /* Provide called function with buffer size information */
3226 sdl.sdl_len = sizeof(sdl);
3227 llsa = (struct sockaddr *)&sdl;
3228 error = ifp->if_resolvemulti(ifp, &llsa, sa);
3234 * Allocate the new address. Don't hook it up yet, as we may also
3235 * need to allocate a link layer multicast address.
3237 ifma = if_allocmulti(ifp, sa, llsa, M_NOWAIT);
3244 * If a link layer address is found, we'll need to see if it's
3245 * already present in the address list, or allocate is as well.
3246 * When this block finishes, the link layer address will be on the
3250 ll_ifma = if_findmulti(ifp, llsa);
3251 if (ll_ifma == NULL) {
3252 ll_ifma = if_allocmulti(ifp, llsa, NULL, M_NOWAIT);
3253 if (ll_ifma == NULL) {
3254 --ifma->ifma_refcount;
3259 TAILQ_INSERT_HEAD(&ifp->if_multiaddrs, ll_ifma,
3262 ll_ifma->ifma_refcount++;
3263 ifma->ifma_llifma = ll_ifma;
3267 * We now have a new multicast address, ifma, and possibly a new or
3268 * referenced link layer address. Add the primary address to the
3269 * ifnet address list.
3271 TAILQ_INSERT_HEAD(&ifp->if_multiaddrs, ifma, ifma_link);
3273 if (retifma != NULL)
3277 * Must generate the message while holding the lock so that 'ifma'
3278 * pointer is still valid.
3280 rt_newmaddrmsg(RTM_NEWMADDR, ifma);
3281 IF_ADDR_WUNLOCK(ifp);
3284 * We are certain we have added something, so call down to the
3285 * interface to let them know about it.
3287 if (ifp->if_ioctl != NULL) {
3288 (void) (*ifp->if_ioctl)(ifp, SIOCADDMULTI, 0);
3291 if ((llsa != NULL) && (llsa != (struct sockaddr *)&sdl))
3292 link_free_sdl(llsa);
3297 if ((llsa != NULL) && (llsa != (struct sockaddr *)&sdl))
3298 link_free_sdl(llsa);
3301 IF_ADDR_WUNLOCK(ifp);
3306 * Delete a multicast group membership by network-layer group address.
3308 * Returns ENOENT if the entry could not be found. If ifp no longer
3309 * exists, results are undefined. This entry point should only be used
3310 * from subsystems which do appropriate locking to hold ifp for the
3311 * duration of the call.
3312 * Network-layer protocol domains must use if_delmulti_ifma().
3315 if_delmulti(struct ifnet *ifp, struct sockaddr *sa)
3317 struct ifmultiaddr *ifma;
3322 IFNET_RLOCK_NOSLEEP();
3323 TAILQ_FOREACH(oifp, &V_ifnet, if_link)
3328 IFNET_RUNLOCK_NOSLEEP();
3330 KASSERT(ifp != NULL, ("%s: ifnet went away", __func__));
3337 ifma = if_findmulti(ifp, sa);
3339 lastref = if_delmulti_locked(ifp, ifma, 0);
3340 IF_ADDR_WUNLOCK(ifp);
3345 if (lastref && ifp->if_ioctl != NULL) {
3346 (void)(*ifp->if_ioctl)(ifp, SIOCDELMULTI, 0);
3353 * Delete all multicast group membership for an interface.
3354 * Should be used to quickly flush all multicast filters.
3357 if_delallmulti(struct ifnet *ifp)
3359 struct ifmultiaddr *ifma;
3360 struct ifmultiaddr *next;
3363 TAILQ_FOREACH_SAFE(ifma, &ifp->if_multiaddrs, ifma_link, next)
3364 if_delmulti_locked(ifp, ifma, 0);
3365 IF_ADDR_WUNLOCK(ifp);
3369 * Delete a multicast group membership by group membership pointer.
3370 * Network-layer protocol domains must use this routine.
3372 * It is safe to call this routine if the ifp disappeared.
3375 if_delmulti_ifma(struct ifmultiaddr *ifma)
3380 ifp = ifma->ifma_ifp;
3383 printf("%s: ifma_ifp seems to be detached\n", __func__);
3387 IFNET_RLOCK_NOSLEEP();
3388 TAILQ_FOREACH(oifp, &V_ifnet, if_link)
3392 printf("%s: ifnet %p disappeared\n", __func__, ifp);
3395 IFNET_RUNLOCK_NOSLEEP();
3399 * If and only if the ifnet instance exists: Acquire the address lock.
3404 lastref = if_delmulti_locked(ifp, ifma, 0);
3408 * If and only if the ifnet instance exists:
3409 * Release the address lock.
3410 * If the group was left: update the hardware hash filter.
3412 IF_ADDR_WUNLOCK(ifp);
3413 if (lastref && ifp->if_ioctl != NULL) {
3414 (void)(*ifp->if_ioctl)(ifp, SIOCDELMULTI, 0);
3420 * Perform deletion of network-layer and/or link-layer multicast address.
3422 * Return 0 if the reference count was decremented.
3423 * Return 1 if the final reference was released, indicating that the
3424 * hardware hash filter should be reprogrammed.
3427 if_delmulti_locked(struct ifnet *ifp, struct ifmultiaddr *ifma, int detaching)
3429 struct ifmultiaddr *ll_ifma;
3431 if (ifp != NULL && ifma->ifma_ifp != NULL) {
3432 KASSERT(ifma->ifma_ifp == ifp,
3433 ("%s: inconsistent ifp %p", __func__, ifp));
3434 IF_ADDR_WLOCK_ASSERT(ifp);
3437 ifp = ifma->ifma_ifp;
3440 * If the ifnet is detaching, null out references to ifnet,
3441 * so that upper protocol layers will notice, and not attempt
3442 * to obtain locks for an ifnet which no longer exists. The
3443 * routing socket announcement must happen before the ifnet
3444 * instance is detached from the system.
3448 printf("%s: detaching ifnet instance %p\n", __func__, ifp);
3451 * ifp may already be nulled out if we are being reentered
3452 * to delete the ll_ifma.
3455 rt_newmaddrmsg(RTM_DELMADDR, ifma);
3456 ifma->ifma_ifp = NULL;
3460 if (--ifma->ifma_refcount > 0)
3464 * If this ifma is a network-layer ifma, a link-layer ifma may
3465 * have been associated with it. Release it first if so.
3467 ll_ifma = ifma->ifma_llifma;
3468 if (ll_ifma != NULL) {
3469 KASSERT(ifma->ifma_lladdr != NULL,
3470 ("%s: llifma w/o lladdr", __func__));
3472 ll_ifma->ifma_ifp = NULL; /* XXX */
3473 if (--ll_ifma->ifma_refcount == 0) {
3475 TAILQ_REMOVE(&ifp->if_multiaddrs, ll_ifma,
3478 if_freemulti(ll_ifma);
3483 TAILQ_REMOVE(&ifp->if_multiaddrs, ifma, ifma_link);
3488 * The last reference to this instance of struct ifmultiaddr
3489 * was released; the hardware should be notified of this change.
3495 * Set the link layer address on an interface.
3497 * At this time we only support certain types of interfaces,
3498 * and we don't allow the length of the address to change.
3500 * Set noinline to be dtrace-friendly
3503 if_setlladdr(struct ifnet *ifp, const u_char *lladdr, int len)
3505 struct sockaddr_dl *sdl;
3512 IF_ADDR_RUNLOCK(ifp);
3516 IF_ADDR_RUNLOCK(ifp);
3517 sdl = (struct sockaddr_dl *)ifa->ifa_addr;
3522 if (len != sdl->sdl_alen) { /* don't allow length to change */
3526 switch (ifp->if_type) {
3534 case IFT_IEEE8023ADLAG:
3535 bcopy(lladdr, LLADDR(sdl), len);
3544 * If the interface is already up, we need
3545 * to re-init it in order to reprogram its
3548 if ((ifp->if_flags & IFF_UP) != 0) {
3549 if (ifp->if_ioctl) {
3550 ifp->if_flags &= ~IFF_UP;
3551 ifr.ifr_flags = ifp->if_flags & 0xffff;
3552 ifr.ifr_flagshigh = ifp->if_flags >> 16;
3553 (*ifp->if_ioctl)(ifp, SIOCSIFFLAGS, (caddr_t)&ifr);
3554 ifp->if_flags |= IFF_UP;
3555 ifr.ifr_flags = ifp->if_flags & 0xffff;
3556 ifr.ifr_flagshigh = ifp->if_flags >> 16;
3557 (*ifp->if_ioctl)(ifp, SIOCSIFFLAGS, (caddr_t)&ifr);
3560 EVENTHANDLER_INVOKE(iflladdr_event, ifp);
3565 * Compat function for handling basic encapsulation requests.
3566 * Not converted stacks (FDDI, IB, ..) supports traditional
3567 * output model: ARP (and other similar L2 protocols) are handled
3568 * inside output routine, arpresolve/nd6_resolve() returns MAC
3569 * address instead of full prepend.
3571 * This function creates calculated header==MAC for IPv4/IPv6 and
3572 * returns EAFNOSUPPORT (which is then handled in ARP code) for other
3576 if_requestencap_default(struct ifnet *ifp, struct if_encap_req *req)
3579 if (req->rtype != IFENCAP_LL)
3580 return (EOPNOTSUPP);
3582 if (req->bufsize < req->lladdr_len)
3585 switch (req->family) {
3590 return (EAFNOSUPPORT);
3593 /* Copy lladdr to storage as is */
3594 memmove(req->buf, req->lladdr, req->lladdr_len);
3595 req->bufsize = req->lladdr_len;
3596 req->lladdr_off = 0;
3602 * Get the link layer address that was read from the hardware at attach.
3604 * This is only set by Ethernet NICs (IFT_ETHER), but laggX interfaces re-type
3605 * their component interfaces as IFT_IEEE8023ADLAG.
3608 if_gethwaddr(struct ifnet *ifp, struct ifreq *ifr)
3611 if (ifp->if_hw_addr == NULL)
3614 switch (ifp->if_type) {
3616 case IFT_IEEE8023ADLAG:
3617 bcopy(ifp->if_hw_addr, ifr->ifr_addr.sa_data, ifp->if_addrlen);
3625 * The name argument must be a pointer to storage which will last as
3626 * long as the interface does. For physical devices, the result of
3627 * device_get_name(dev) is a good choice and for pseudo-devices a
3628 * static string works well.
3631 if_initname(struct ifnet *ifp, const char *name, int unit)
3633 ifp->if_dname = name;
3634 ifp->if_dunit = unit;
3635 if (unit != IF_DUNIT_NONE)
3636 snprintf(ifp->if_xname, IFNAMSIZ, "%s%d", name, unit);
3638 strlcpy(ifp->if_xname, name, IFNAMSIZ);
3642 if_printf(struct ifnet *ifp, const char * fmt, ...)
3647 retval = printf("%s: ", ifp->if_xname);
3649 retval += vprintf(fmt, ap);
3655 if_start(struct ifnet *ifp)
3658 (*(ifp)->if_start)(ifp);
3662 * Backwards compatibility interface for drivers
3663 * that have not implemented it
3666 if_transmit(struct ifnet *ifp, struct mbuf *m)
3670 IFQ_HANDOFF(ifp, m, error);
3675 if_input_default(struct ifnet *ifp __unused, struct mbuf *m)
3682 if_handoff(struct ifqueue *ifq, struct mbuf *m, struct ifnet *ifp, int adjust)
3687 if (_IF_QFULL(ifq)) {
3689 if_inc_counter(ifp, IFCOUNTER_OQDROPS, 1);
3694 if_inc_counter(ifp, IFCOUNTER_OBYTES, m->m_pkthdr.len + adjust);
3695 if (m->m_flags & (M_BCAST|M_MCAST))
3696 if_inc_counter(ifp, IFCOUNTER_OMCASTS, 1);
3697 active = ifp->if_drv_flags & IFF_DRV_OACTIVE;
3699 _IF_ENQUEUE(ifq, m);
3701 if (ifp != NULL && !active)
3702 (*(ifp)->if_start)(ifp);
3707 if_register_com_alloc(u_char type,
3708 if_com_alloc_t *a, if_com_free_t *f)
3711 KASSERT(if_com_alloc[type] == NULL,
3712 ("if_register_com_alloc: %d already registered", type));
3713 KASSERT(if_com_free[type] == NULL,
3714 ("if_register_com_alloc: %d free already registered", type));
3716 if_com_alloc[type] = a;
3717 if_com_free[type] = f;
3721 if_deregister_com_alloc(u_char type)
3724 KASSERT(if_com_alloc[type] != NULL,
3725 ("if_deregister_com_alloc: %d not registered", type));
3726 KASSERT(if_com_free[type] != NULL,
3727 ("if_deregister_com_alloc: %d free not registered", type));
3728 if_com_alloc[type] = NULL;
3729 if_com_free[type] = NULL;
3732 /* API for driver access to network stack owned ifnet.*/
3734 if_setbaudrate(struct ifnet *ifp, uint64_t baudrate)
3738 oldbrate = ifp->if_baudrate;
3739 ifp->if_baudrate = baudrate;
3744 if_getbaudrate(if_t ifp)
3747 return (((struct ifnet *)ifp)->if_baudrate);
3751 if_setcapabilities(if_t ifp, int capabilities)
3753 ((struct ifnet *)ifp)->if_capabilities = capabilities;
3758 if_setcapabilitiesbit(if_t ifp, int setbit, int clearbit)
3760 ((struct ifnet *)ifp)->if_capabilities |= setbit;
3761 ((struct ifnet *)ifp)->if_capabilities &= ~clearbit;
3767 if_getcapabilities(if_t ifp)
3769 return ((struct ifnet *)ifp)->if_capabilities;
3773 if_setcapenable(if_t ifp, int capabilities)
3775 ((struct ifnet *)ifp)->if_capenable = capabilities;
3780 if_setcapenablebit(if_t ifp, int setcap, int clearcap)
3783 ((struct ifnet *)ifp)->if_capenable |= setcap;
3785 ((struct ifnet *)ifp)->if_capenable &= ~clearcap;
3791 if_getdname(if_t ifp)
3793 return ((struct ifnet *)ifp)->if_dname;
3797 if_togglecapenable(if_t ifp, int togglecap)
3799 ((struct ifnet *)ifp)->if_capenable ^= togglecap;
3804 if_getcapenable(if_t ifp)
3806 return ((struct ifnet *)ifp)->if_capenable;
3810 * This is largely undesirable because it ties ifnet to a device, but does
3811 * provide flexiblity for an embedded product vendor. Should be used with
3812 * the understanding that it violates the interface boundaries, and should be
3813 * a last resort only.
3816 if_setdev(if_t ifp, void *dev)
3822 if_setdrvflagbits(if_t ifp, int set_flags, int clear_flags)
3824 ((struct ifnet *)ifp)->if_drv_flags |= set_flags;
3825 ((struct ifnet *)ifp)->if_drv_flags &= ~clear_flags;
3831 if_getdrvflags(if_t ifp)
3833 return ((struct ifnet *)ifp)->if_drv_flags;
3837 if_setdrvflags(if_t ifp, int flags)
3839 ((struct ifnet *)ifp)->if_drv_flags = flags;
3845 if_setflags(if_t ifp, int flags)
3847 ((struct ifnet *)ifp)->if_flags = flags;
3852 if_setflagbits(if_t ifp, int set, int clear)
3854 ((struct ifnet *)ifp)->if_flags |= set;
3855 ((struct ifnet *)ifp)->if_flags &= ~clear;
3861 if_getflags(if_t ifp)
3863 return ((struct ifnet *)ifp)->if_flags;
3867 if_clearhwassist(if_t ifp)
3869 ((struct ifnet *)ifp)->if_hwassist = 0;
3874 if_sethwassistbits(if_t ifp, int toset, int toclear)
3876 ((struct ifnet *)ifp)->if_hwassist |= toset;
3877 ((struct ifnet *)ifp)->if_hwassist &= ~toclear;
3883 if_sethwassist(if_t ifp, int hwassist_bit)
3885 ((struct ifnet *)ifp)->if_hwassist = hwassist_bit;
3890 if_gethwassist(if_t ifp)
3892 return ((struct ifnet *)ifp)->if_hwassist;
3896 if_setmtu(if_t ifp, int mtu)
3898 ((struct ifnet *)ifp)->if_mtu = mtu;
3905 return ((struct ifnet *)ifp)->if_mtu;
3909 if_getmtu_family(if_t ifp, int family)
3913 for (dp = domains; dp; dp = dp->dom_next) {
3914 if (dp->dom_family == family && dp->dom_ifmtu != NULL)
3915 return (dp->dom_ifmtu((struct ifnet *)ifp));
3918 return (((struct ifnet *)ifp)->if_mtu);
3922 if_setsoftc(if_t ifp, void *softc)
3924 ((struct ifnet *)ifp)->if_softc = softc;
3929 if_getsoftc(if_t ifp)
3931 return ((struct ifnet *)ifp)->if_softc;
3935 if_setrcvif(struct mbuf *m, if_t ifp)
3937 m->m_pkthdr.rcvif = (struct ifnet *)ifp;
3941 if_setvtag(struct mbuf *m, uint16_t tag)
3943 m->m_pkthdr.ether_vtag = tag;
3947 if_getvtag(struct mbuf *m)
3950 return (m->m_pkthdr.ether_vtag);
3954 if_sendq_empty(if_t ifp)
3956 return IFQ_DRV_IS_EMPTY(&((struct ifnet *)ifp)->if_snd);
3960 if_getifaddr(if_t ifp)
3962 return ((struct ifnet *)ifp)->if_addr;
3966 if_getamcount(if_t ifp)
3968 return ((struct ifnet *)ifp)->if_amcount;
3973 if_setsendqready(if_t ifp)
3975 IFQ_SET_READY(&((struct ifnet *)ifp)->if_snd);
3980 if_setsendqlen(if_t ifp, int tx_desc_count)
3982 IFQ_SET_MAXLEN(&((struct ifnet *)ifp)->if_snd, tx_desc_count);
3983 ((struct ifnet *)ifp)->if_snd.ifq_drv_maxlen = tx_desc_count;
3989 if_vlantrunkinuse(if_t ifp)
3991 return ((struct ifnet *)ifp)->if_vlantrunk != NULL?1:0;
3995 if_input(if_t ifp, struct mbuf* sendmp)
3997 (*((struct ifnet *)ifp)->if_input)((struct ifnet *)ifp, sendmp);
4003 #ifndef ETH_ADDR_LEN
4004 #define ETH_ADDR_LEN 6
4008 if_setupmultiaddr(if_t ifp, void *mta, int *cnt, int max)
4010 struct ifmultiaddr *ifma;
4011 uint8_t *lmta = (uint8_t *)mta;
4014 TAILQ_FOREACH(ifma, &((struct ifnet *)ifp)->if_multiaddrs, ifma_link) {
4015 if (ifma->ifma_addr->sa_family != AF_LINK)
4021 bcopy(LLADDR((struct sockaddr_dl *)ifma->ifma_addr),
4022 &lmta[mcnt * ETH_ADDR_LEN], ETH_ADDR_LEN);
4031 if_multiaddr_array(if_t ifp, void *mta, int *cnt, int max)
4035 if_maddr_rlock(ifp);
4036 error = if_setupmultiaddr(ifp, mta, cnt, max);
4037 if_maddr_runlock(ifp);
4042 if_multiaddr_count(if_t ifp, int max)
4044 struct ifmultiaddr *ifma;
4048 if_maddr_rlock(ifp);
4049 TAILQ_FOREACH(ifma, &((struct ifnet *)ifp)->if_multiaddrs, ifma_link) {
4050 if (ifma->ifma_addr->sa_family != AF_LINK)
4056 if_maddr_runlock(ifp);
4061 if_multi_apply(struct ifnet *ifp, int (*filter)(void *, struct ifmultiaddr *, int), void *arg)
4063 struct ifmultiaddr *ifma;
4066 if_maddr_rlock(ifp);
4067 TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link)
4068 cnt += filter(arg, ifma, cnt);
4069 if_maddr_runlock(ifp);
4074 if_dequeue(if_t ifp)
4077 IFQ_DRV_DEQUEUE(&((struct ifnet *)ifp)->if_snd, m);
4083 if_sendq_prepend(if_t ifp, struct mbuf *m)
4085 IFQ_DRV_PREPEND(&((struct ifnet *)ifp)->if_snd, m);
4090 if_setifheaderlen(if_t ifp, int len)
4092 ((struct ifnet *)ifp)->if_hdrlen = len;
4097 if_getlladdr(if_t ifp)
4099 return (IF_LLADDR((struct ifnet *)ifp));
4103 if_gethandle(u_char type)
4105 return (if_alloc(type));
4109 if_bpfmtap(if_t ifh, struct mbuf *m)
4111 struct ifnet *ifp = (struct ifnet *)ifh;
4117 if_etherbpfmtap(if_t ifh, struct mbuf *m)
4119 struct ifnet *ifp = (struct ifnet *)ifh;
4121 ETHER_BPF_MTAP(ifp, m);
4125 if_vlancap(if_t ifh)
4127 struct ifnet *ifp = (struct ifnet *)ifh;
4128 VLAN_CAPABILITIES(ifp);
4132 if_sethwtsomax(if_t ifp, u_int if_hw_tsomax)
4135 ((struct ifnet *)ifp)->if_hw_tsomax = if_hw_tsomax;
4140 if_sethwtsomaxsegcount(if_t ifp, u_int if_hw_tsomaxsegcount)
4143 ((struct ifnet *)ifp)->if_hw_tsomaxsegcount = if_hw_tsomaxsegcount;
4148 if_sethwtsomaxsegsize(if_t ifp, u_int if_hw_tsomaxsegsize)
4151 ((struct ifnet *)ifp)->if_hw_tsomaxsegsize = if_hw_tsomaxsegsize;
4156 if_gethwtsomax(if_t ifp)
4159 return (((struct ifnet *)ifp)->if_hw_tsomax);
4163 if_gethwtsomaxsegcount(if_t ifp)
4166 return (((struct ifnet *)ifp)->if_hw_tsomaxsegcount);
4170 if_gethwtsomaxsegsize(if_t ifp)
4173 return (((struct ifnet *)ifp)->if_hw_tsomaxsegsize);
4177 if_setinitfn(if_t ifp, void (*init_fn)(void *))
4179 ((struct ifnet *)ifp)->if_init = init_fn;
4183 if_setioctlfn(if_t ifp, int (*ioctl_fn)(if_t, u_long, caddr_t))
4185 ((struct ifnet *)ifp)->if_ioctl = (void *)ioctl_fn;
4189 if_setstartfn(if_t ifp, void (*start_fn)(if_t))
4191 ((struct ifnet *)ifp)->if_start = (void *)start_fn;
4195 if_settransmitfn(if_t ifp, if_transmit_fn_t start_fn)
4197 ((struct ifnet *)ifp)->if_transmit = start_fn;
4200 void if_setqflushfn(if_t ifp, if_qflush_fn_t flush_fn)
4202 ((struct ifnet *)ifp)->if_qflush = flush_fn;
4207 if_setgetcounterfn(if_t ifp, if_get_counter_t fn)
4210 ifp->if_get_counter = fn;
4213 /* Revisit these - These are inline functions originally. */
4215 drbr_inuse_drv(if_t ifh, struct buf_ring *br)
4217 return drbr_inuse(ifh, br);
4221 drbr_dequeue_drv(if_t ifh, struct buf_ring *br)
4223 return drbr_dequeue(ifh, br);
4227 drbr_needs_enqueue_drv(if_t ifh, struct buf_ring *br)
4229 return drbr_needs_enqueue(ifh, br);
4233 drbr_enqueue_drv(if_t ifh, struct buf_ring *br, struct mbuf *m)
4235 return drbr_enqueue(ifh, br, m);