2 * Copyright (c) 1980, 1986, 1993
3 * The Regents of the University of California. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 4. Neither the name of the University nor the names of its contributors
14 * may be used to endorse or promote products derived from this software
15 * without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * @(#)if.c 8.5 (Berkeley) 1/9/95
33 #include "opt_compat.h"
34 #include "opt_inet6.h"
37 #include <sys/param.h>
38 #include <sys/types.h>
40 #include <sys/malloc.h>
44 #include <sys/systm.h>
47 #include <sys/socket.h>
48 #include <sys/socketvar.h>
49 #include <sys/protosw.h>
50 #include <sys/kernel.h>
52 #include <sys/refcount.h>
53 #include <sys/module.h>
54 #include <sys/rwlock.h>
55 #include <sys/sockio.h>
56 #include <sys/syslog.h>
57 #include <sys/sysctl.h>
58 #include <sys/taskqueue.h>
59 #include <sys/domain.h>
62 #include <sys/eventhandler.h>
64 #include <machine/stdarg.h>
68 #include <net/ethernet.h>
70 #include <net/if_arp.h>
71 #include <net/if_clone.h>
72 #include <net/if_dl.h>
73 #include <net/if_types.h>
74 #include <net/if_var.h>
75 #include <net/if_media.h>
76 #include <net/if_vlan_var.h>
77 #include <net/radix.h>
78 #include <net/route.h>
81 #if defined(INET) || defined(INET6)
82 #include <net/ethernet.h>
83 #include <netinet/in.h>
84 #include <netinet/in_var.h>
85 #include <netinet/ip.h>
86 #include <netinet/ip_carp.h>
88 #include <netinet/if_ether.h>
91 #include <netinet6/in6_var.h>
92 #include <netinet6/in6_ifattach.h>
94 #endif /* INET || INET6 */
96 #include <security/mac/mac_framework.h>
98 #ifdef COMPAT_FREEBSD32
99 #include <sys/mount.h>
100 #include <compat/freebsd32/freebsd32.h>
103 SYSCTL_NODE(_net, PF_LINK, link, CTLFLAG_RW, 0, "Link layers");
104 SYSCTL_NODE(_net_link, 0, generic, CTLFLAG_RW, 0, "Generic link-management");
106 SYSCTL_INT(_net_link, OID_AUTO, ifqmaxlen, CTLFLAG_RDTUN,
107 &ifqmaxlen, 0, "max send queue size");
109 /* Log link state change events */
110 static int log_link_state_change = 1;
112 SYSCTL_INT(_net_link, OID_AUTO, log_link_state_change, CTLFLAG_RW,
113 &log_link_state_change, 0,
114 "log interface link state change events");
116 /* Log promiscuous mode change events */
117 static int log_promisc_mode_change = 1;
119 SYSCTL_INT(_net_link, OID_AUTO, log_promisc_mode_change, CTLFLAG_RDTUN,
120 &log_promisc_mode_change, 1,
121 "log promiscuous mode change events");
123 /* Interface description */
124 static unsigned int ifdescr_maxlen = 1024;
125 SYSCTL_UINT(_net, OID_AUTO, ifdescr_maxlen, CTLFLAG_RW,
127 "administrative maximum length for interface description");
129 static MALLOC_DEFINE(M_IFDESCR, "ifdescr", "ifnet descriptions");
131 /* global sx for non-critical path ifdescr */
132 static struct sx ifdescr_sx;
133 SX_SYSINIT(ifdescr_sx, &ifdescr_sx, "ifnet descr");
135 void (*bridge_linkstate_p)(struct ifnet *ifp);
136 void (*ng_ether_link_state_p)(struct ifnet *ifp, int state);
137 void (*lagg_linkstate_p)(struct ifnet *ifp, int state);
138 /* These are external hooks for CARP. */
139 void (*carp_linkstate_p)(struct ifnet *ifp);
140 void (*carp_demote_adj_p)(int, char *);
141 int (*carp_master_p)(struct ifaddr *);
142 #if defined(INET) || defined(INET6)
143 int (*carp_forus_p)(struct ifnet *ifp, u_char *dhost);
144 int (*carp_output_p)(struct ifnet *ifp, struct mbuf *m,
145 const struct sockaddr *sa);
146 int (*carp_ioctl_p)(struct ifreq *, u_long, struct thread *);
147 int (*carp_attach_p)(struct ifaddr *, int);
148 void (*carp_detach_p)(struct ifaddr *);
151 int (*carp_iamatch_p)(struct ifaddr *, uint8_t **);
154 struct ifaddr *(*carp_iamatch6_p)(struct ifnet *ifp, struct in6_addr *taddr6);
155 caddr_t (*carp_macmatch6_p)(struct ifnet *ifp, struct mbuf *m,
156 const struct in6_addr *taddr);
159 struct mbuf *(*tbr_dequeue_ptr)(struct ifaltq *, int) = NULL;
162 * XXX: Style; these should be sorted alphabetically, and unprototyped
163 * static functions should be prototyped. Currently they are sorted by
166 static void if_attachdomain(void *);
167 static void if_attachdomain1(struct ifnet *);
168 static int ifconf(u_long, caddr_t);
169 static void if_freemulti(struct ifmultiaddr *);
170 static void if_grow(void);
171 static void if_input_default(struct ifnet *, struct mbuf *);
172 static int if_requestencap_default(struct ifnet *, struct if_encap_req *);
173 static void if_route(struct ifnet *, int flag, int fam);
174 static int if_setflag(struct ifnet *, int, int, int *, int);
175 static int if_transmit(struct ifnet *ifp, struct mbuf *m);
176 static void if_unroute(struct ifnet *, int flag, int fam);
177 static void link_rtrequest(int, struct rtentry *, struct rt_addrinfo *);
178 static int ifhwioctl(u_long, struct ifnet *, caddr_t, struct thread *);
179 static int if_delmulti_locked(struct ifnet *, struct ifmultiaddr *, int);
180 static void do_link_state_change(void *, int);
181 static int if_getgroup(struct ifgroupreq *, struct ifnet *);
182 static int if_getgroupmembers(struct ifgroupreq *);
183 static void if_delgroups(struct ifnet *);
184 static void if_attach_internal(struct ifnet *, int, struct if_clone *);
185 static int if_detach_internal(struct ifnet *, int, struct if_clone **);
187 static void if_vmove(struct ifnet *, struct vnet *);
192 * XXX: declare here to avoid to include many inet6 related files..
193 * should be more generalized?
195 extern void nd6_setmtu(struct ifnet *);
198 /* ipsec helper hooks */
199 VNET_DEFINE(struct hhook_head *, ipsec_hhh_in[HHOOK_IPSEC_COUNT]);
200 VNET_DEFINE(struct hhook_head *, ipsec_hhh_out[HHOOK_IPSEC_COUNT]);
202 VNET_DEFINE(int, if_index);
203 int ifqmaxlen = IFQ_MAXLEN;
204 VNET_DEFINE(struct ifnethead, ifnet); /* depend on static init XXX */
205 VNET_DEFINE(struct ifgrouphead, ifg_head);
207 static VNET_DEFINE(int, if_indexlim) = 8;
209 /* Table of ifnet by index. */
210 VNET_DEFINE(struct ifnet **, ifindex_table);
212 #define V_if_indexlim VNET(if_indexlim)
213 #define V_ifindex_table VNET(ifindex_table)
216 * The global network interface list (V_ifnet) and related state (such as
217 * if_index, if_indexlim, and ifindex_table) are protected by an sxlock and
218 * an rwlock. Either may be acquired shared to stablize the list, but both
219 * must be acquired writable to modify the list. This model allows us to
220 * both stablize the interface list during interrupt thread processing, but
221 * also to stablize it over long-running ioctls, without introducing priority
222 * inversions and deadlocks.
224 struct rwlock ifnet_rwlock;
225 RW_SYSINIT_FLAGS(ifnet_rw, &ifnet_rwlock, "ifnet_rw", RW_RECURSE);
226 struct sx ifnet_sxlock;
227 SX_SYSINIT_FLAGS(ifnet_sx, &ifnet_sxlock, "ifnet_sx", SX_RECURSE);
230 * The allocation of network interfaces is a rather non-atomic affair; we
231 * need to select an index before we are ready to expose the interface for
232 * use, so will use this pointer value to indicate reservation.
234 #define IFNET_HOLD (void *)(uintptr_t)(-1)
236 static if_com_alloc_t *if_com_alloc[256];
237 static if_com_free_t *if_com_free[256];
239 static MALLOC_DEFINE(M_IFNET, "ifnet", "interface internals");
240 MALLOC_DEFINE(M_IFADDR, "ifaddr", "interface address");
241 MALLOC_DEFINE(M_IFMADDR, "ether_multi", "link-level multicast address");
244 ifnet_byindex_locked(u_short idx)
247 if (idx > V_if_index)
249 if (V_ifindex_table[idx] == IFNET_HOLD)
251 return (V_ifindex_table[idx]);
255 ifnet_byindex(u_short idx)
259 IFNET_RLOCK_NOSLEEP();
260 ifp = ifnet_byindex_locked(idx);
261 IFNET_RUNLOCK_NOSLEEP();
266 ifnet_byindex_ref(u_short idx)
270 IFNET_RLOCK_NOSLEEP();
271 ifp = ifnet_byindex_locked(idx);
272 if (ifp == NULL || (ifp->if_flags & IFF_DYING)) {
273 IFNET_RUNLOCK_NOSLEEP();
277 IFNET_RUNLOCK_NOSLEEP();
282 * Allocate an ifindex array entry; return 0 on success or an error on
290 IFNET_WLOCK_ASSERT();
293 * Try to find an empty slot below V_if_index. If we fail, take the
296 for (idx = 1; idx <= V_if_index; idx++) {
297 if (V_ifindex_table[idx] == NULL)
301 /* Catch if_index overflow. */
302 if (idx >= V_if_indexlim) {
306 if (idx > V_if_index)
312 ifindex_free_locked(u_short idx)
315 IFNET_WLOCK_ASSERT();
317 V_ifindex_table[idx] = NULL;
318 while (V_if_index > 0 &&
319 V_ifindex_table[V_if_index] == NULL)
324 ifindex_free(u_short idx)
328 ifindex_free_locked(idx);
333 ifnet_setbyindex_locked(u_short idx, struct ifnet *ifp)
336 IFNET_WLOCK_ASSERT();
338 V_ifindex_table[idx] = ifp;
342 ifnet_setbyindex(u_short idx, struct ifnet *ifp)
346 ifnet_setbyindex_locked(idx, ifp);
351 ifaddr_byindex(u_short idx)
354 struct ifaddr *ifa = NULL;
356 IFNET_RLOCK_NOSLEEP();
357 ifp = ifnet_byindex_locked(idx);
358 if (ifp != NULL && (ifa = ifp->if_addr) != NULL)
360 IFNET_RUNLOCK_NOSLEEP();
365 * Network interface utility routines.
367 * Routines with ifa_ifwith* names take sockaddr *'s as
372 vnet_if_init(const void *unused __unused)
375 TAILQ_INIT(&V_ifnet);
376 TAILQ_INIT(&V_ifg_head);
378 if_grow(); /* create initial table */
380 vnet_if_clone_init();
382 VNET_SYSINIT(vnet_if_init, SI_SUB_INIT_IF, SI_ORDER_SECOND, vnet_if_init,
387 vnet_if_uninit(const void *unused __unused)
390 VNET_ASSERT(TAILQ_EMPTY(&V_ifnet), ("%s:%d tailq &V_ifnet=%p "
391 "not empty", __func__, __LINE__, &V_ifnet));
392 VNET_ASSERT(TAILQ_EMPTY(&V_ifg_head), ("%s:%d tailq &V_ifg_head=%p "
393 "not empty", __func__, __LINE__, &V_ifg_head));
395 free((caddr_t)V_ifindex_table, M_IFNET);
397 VNET_SYSUNINIT(vnet_if_uninit, SI_SUB_INIT_IF, SI_ORDER_FIRST,
398 vnet_if_uninit, NULL);
401 vnet_if_return(const void *unused __unused)
403 struct ifnet *ifp, *nifp;
405 /* Return all inherited interfaces to their parent vnets. */
406 TAILQ_FOREACH_SAFE(ifp, &V_ifnet, if_link, nifp) {
407 if (ifp->if_home_vnet != ifp->if_vnet)
408 if_vmove(ifp, ifp->if_home_vnet);
411 VNET_SYSUNINIT(vnet_if_return, SI_SUB_VNET_DONE, SI_ORDER_ANY,
412 vnet_if_return, NULL);
422 IFNET_WLOCK_ASSERT();
423 oldlim = V_if_indexlim;
425 n = (oldlim << 1) * sizeof(*e);
426 e = malloc(n, M_IFNET, M_WAITOK | M_ZERO);
428 if (V_if_indexlim != oldlim) {
432 if (V_ifindex_table != NULL) {
433 memcpy((caddr_t)e, (caddr_t)V_ifindex_table, n/2);
434 free((caddr_t)V_ifindex_table, M_IFNET);
441 * Allocate a struct ifnet and an index for an interface. A layer 2
442 * common structure will also be allocated if an allocation routine is
443 * registered for the passed type.
446 if_alloc(u_char type)
451 ifp = malloc(sizeof(struct ifnet), M_IFNET, M_WAITOK|M_ZERO);
453 idx = ifindex_alloc();
454 ifnet_setbyindex_locked(idx, IFNET_HOLD);
458 ifp->if_alloctype = type;
460 ifp->if_vnet = curvnet;
462 if (if_com_alloc[type] != NULL) {
463 ifp->if_l2com = if_com_alloc[type](type, ifp);
464 if (ifp->if_l2com == NULL) {
471 IF_ADDR_LOCK_INIT(ifp);
472 TASK_INIT(&ifp->if_linktask, 0, do_link_state_change, ifp);
473 ifp->if_afdata_initialized = 0;
474 IF_AFDATA_LOCK_INIT(ifp);
475 TAILQ_INIT(&ifp->if_addrhead);
476 TAILQ_INIT(&ifp->if_multiaddrs);
477 TAILQ_INIT(&ifp->if_groups);
481 ifq_init(&ifp->if_snd, ifp);
483 refcount_init(&ifp->if_refcount, 1); /* Index reference. */
484 for (int i = 0; i < IFCOUNTERS; i++)
485 ifp->if_counters[i] = counter_u64_alloc(M_WAITOK);
486 ifp->if_get_counter = if_get_counter_default;
487 ifnet_setbyindex(ifp->if_index, ifp);
492 * Do the actual work of freeing a struct ifnet, and layer 2 common
493 * structure. This call is made when the last reference to an
494 * interface is released.
497 if_free_internal(struct ifnet *ifp)
500 KASSERT((ifp->if_flags & IFF_DYING),
501 ("if_free_internal: interface not dying"));
503 if (if_com_free[ifp->if_alloctype] != NULL)
504 if_com_free[ifp->if_alloctype](ifp->if_l2com,
508 mac_ifnet_destroy(ifp);
510 if (ifp->if_description != NULL)
511 free(ifp->if_description, M_IFDESCR);
512 IF_AFDATA_DESTROY(ifp);
513 IF_ADDR_LOCK_DESTROY(ifp);
514 ifq_delete(&ifp->if_snd);
516 for (int i = 0; i < IFCOUNTERS; i++)
517 counter_u64_free(ifp->if_counters[i]);
523 * Deregister an interface and free the associated storage.
526 if_free(struct ifnet *ifp)
529 ifp->if_flags |= IFF_DYING; /* XXX: Locking */
531 CURVNET_SET_QUIET(ifp->if_vnet);
533 KASSERT(ifp == ifnet_byindex_locked(ifp->if_index),
534 ("%s: freeing unallocated ifnet", ifp->if_xname));
536 ifindex_free_locked(ifp->if_index);
539 if (refcount_release(&ifp->if_refcount))
540 if_free_internal(ifp);
545 * Interfaces to keep an ifnet type-stable despite the possibility of the
546 * driver calling if_free(). If there are additional references, we defer
547 * freeing the underlying data structure.
550 if_ref(struct ifnet *ifp)
553 /* We don't assert the ifnet list lock here, but arguably should. */
554 refcount_acquire(&ifp->if_refcount);
558 if_rele(struct ifnet *ifp)
561 if (!refcount_release(&ifp->if_refcount))
563 if_free_internal(ifp);
567 ifq_init(struct ifaltq *ifq, struct ifnet *ifp)
570 mtx_init(&ifq->ifq_mtx, ifp->if_xname, "if send queue", MTX_DEF);
572 if (ifq->ifq_maxlen == 0)
573 ifq->ifq_maxlen = ifqmaxlen;
576 ifq->altq_disc = NULL;
577 ifq->altq_flags &= ALTQF_CANTCHANGE;
578 ifq->altq_tbr = NULL;
583 ifq_delete(struct ifaltq *ifq)
585 mtx_destroy(&ifq->ifq_mtx);
589 * Perform generic interface initialization tasks and attach the interface
590 * to the list of "active" interfaces. If vmove flag is set on entry
591 * to if_attach_internal(), perform only a limited subset of initialization
592 * tasks, given that we are moving from one vnet to another an ifnet which
593 * has already been fully initialized.
595 * Note that if_detach_internal() removes group membership unconditionally
596 * even when vmove flag is set, and if_attach_internal() adds only IFG_ALL.
597 * Thus, when if_vmove() is applied to a cloned interface, group membership
598 * is lost while a cloned one always joins a group whose name is
599 * ifc->ifc_name. To recover this after if_detach_internal() and
600 * if_attach_internal(), the cloner should be specified to
601 * if_attach_internal() via ifc. If it is non-NULL, if_attach_internal()
602 * attempts to join a group whose name is ifc->ifc_name.
605 * - The decision to return void and thus require this function to
606 * succeed is questionable.
607 * - We should probably do more sanity checking. For instance we don't
608 * do anything to insure if_xname is unique or non-empty.
611 if_attach(struct ifnet *ifp)
614 if_attach_internal(ifp, 0, NULL);
618 * Compute the least common TSO limit.
621 if_hw_tsomax_common(if_t ifp, struct ifnet_hw_tsomax *pmax)
624 * 1) If there is no limit currently, take the limit from
625 * the network adapter.
627 * 2) If the network adapter has a limit below the current
630 if (pmax->tsomaxbytes == 0 || (ifp->if_hw_tsomax != 0 &&
631 ifp->if_hw_tsomax < pmax->tsomaxbytes)) {
632 pmax->tsomaxbytes = ifp->if_hw_tsomax;
634 if (pmax->tsomaxsegcount == 0 || (ifp->if_hw_tsomaxsegcount != 0 &&
635 ifp->if_hw_tsomaxsegcount < pmax->tsomaxsegcount)) {
636 pmax->tsomaxsegcount = ifp->if_hw_tsomaxsegcount;
638 if (pmax->tsomaxsegsize == 0 || (ifp->if_hw_tsomaxsegsize != 0 &&
639 ifp->if_hw_tsomaxsegsize < pmax->tsomaxsegsize)) {
640 pmax->tsomaxsegsize = ifp->if_hw_tsomaxsegsize;
645 * Update TSO limit of a network adapter.
647 * Returns zero if no change. Else non-zero.
650 if_hw_tsomax_update(if_t ifp, struct ifnet_hw_tsomax *pmax)
653 if (ifp->if_hw_tsomax != pmax->tsomaxbytes) {
654 ifp->if_hw_tsomax = pmax->tsomaxbytes;
657 if (ifp->if_hw_tsomaxsegsize != pmax->tsomaxsegsize) {
658 ifp->if_hw_tsomaxsegsize = pmax->tsomaxsegsize;
661 if (ifp->if_hw_tsomaxsegcount != pmax->tsomaxsegcount) {
662 ifp->if_hw_tsomaxsegcount = pmax->tsomaxsegcount;
669 if_attach_internal(struct ifnet *ifp, int vmove, struct if_clone *ifc)
671 unsigned socksize, ifasize;
672 int namelen, masklen;
673 struct sockaddr_dl *sdl;
676 if (ifp->if_index == 0 || ifp != ifnet_byindex(ifp->if_index))
677 panic ("%s: BUG: if_attach called without if_alloc'd input()\n",
681 ifp->if_vnet = curvnet;
682 if (ifp->if_home_vnet == NULL)
683 ifp->if_home_vnet = curvnet;
686 if_addgroup(ifp, IFG_ALL);
688 /* Restore group membership for cloned interfaces. */
689 if (vmove && ifc != NULL)
690 if_clone_addgroup(ifp, ifc);
692 getmicrotime(&ifp->if_lastchange);
693 ifp->if_epoch = time_uptime;
695 KASSERT((ifp->if_transmit == NULL && ifp->if_qflush == NULL) ||
696 (ifp->if_transmit != NULL && ifp->if_qflush != NULL),
697 ("transmit and qflush must both either be set or both be NULL"));
698 if (ifp->if_transmit == NULL) {
699 ifp->if_transmit = if_transmit;
700 ifp->if_qflush = if_qflush;
702 if (ifp->if_input == NULL)
703 ifp->if_input = if_input_default;
705 if (ifp->if_requestencap == NULL)
706 ifp->if_requestencap = if_requestencap_default;
710 mac_ifnet_create(ifp);
714 * Create a Link Level name for this device.
716 namelen = strlen(ifp->if_xname);
718 * Always save enough space for any possiable name so we
719 * can do a rename in place later.
721 masklen = offsetof(struct sockaddr_dl, sdl_data[0]) + IFNAMSIZ;
722 socksize = masklen + ifp->if_addrlen;
723 if (socksize < sizeof(*sdl))
724 socksize = sizeof(*sdl);
725 socksize = roundup2(socksize, sizeof(long));
726 ifasize = sizeof(*ifa) + 2 * socksize;
727 ifa = ifa_alloc(ifasize, M_WAITOK);
728 sdl = (struct sockaddr_dl *)(ifa + 1);
729 sdl->sdl_len = socksize;
730 sdl->sdl_family = AF_LINK;
731 bcopy(ifp->if_xname, sdl->sdl_data, namelen);
732 sdl->sdl_nlen = namelen;
733 sdl->sdl_index = ifp->if_index;
734 sdl->sdl_type = ifp->if_type;
737 ifa->ifa_rtrequest = link_rtrequest;
738 ifa->ifa_addr = (struct sockaddr *)sdl;
739 sdl = (struct sockaddr_dl *)(socksize + (caddr_t)sdl);
740 ifa->ifa_netmask = (struct sockaddr *)sdl;
741 sdl->sdl_len = masklen;
743 sdl->sdl_data[--namelen] = 0xff;
744 TAILQ_INSERT_HEAD(&ifp->if_addrhead, ifa, ifa_link);
745 /* Reliably crash if used uninitialized. */
746 ifp->if_broadcastaddr = NULL;
748 #if defined(INET) || defined(INET6)
749 /* Use defaults for TSO, if nothing is set */
750 if (ifp->if_hw_tsomax == 0 &&
751 ifp->if_hw_tsomaxsegcount == 0 &&
752 ifp->if_hw_tsomaxsegsize == 0) {
754 * The TSO defaults needs to be such that an
755 * NFS mbuf list of 35 mbufs totalling just
756 * below 64K works and that a chain of mbufs
757 * can be defragged into at most 32 segments:
759 ifp->if_hw_tsomax = min(IP_MAXPACKET, (32 * MCLBYTES) -
760 (ETHER_HDR_LEN + ETHER_VLAN_ENCAP_LEN));
761 ifp->if_hw_tsomaxsegcount = 35;
762 ifp->if_hw_tsomaxsegsize = 2048; /* 2K */
764 /* XXX some drivers set IFCAP_TSO after ethernet attach */
765 if (ifp->if_capabilities & IFCAP_TSO) {
766 if_printf(ifp, "Using defaults for TSO: %u/%u/%u\n",
768 ifp->if_hw_tsomaxsegcount,
769 ifp->if_hw_tsomaxsegsize);
777 * Update the interface index in the link layer address
780 for (ifa = ifp->if_addr; ifa != NULL;
781 ifa = TAILQ_NEXT(ifa, ifa_link)) {
782 if (ifa->ifa_addr->sa_family == AF_LINK) {
783 sdl = (struct sockaddr_dl *)ifa->ifa_addr;
784 sdl->sdl_index = ifp->if_index;
791 TAILQ_INSERT_TAIL(&V_ifnet, ifp, if_link);
793 curvnet->vnet_ifcnt++;
797 if (domain_init_status >= 2)
798 if_attachdomain1(ifp);
800 EVENTHANDLER_INVOKE(ifnet_arrival_event, ifp);
801 if (IS_DEFAULT_VNET(curvnet))
802 devctl_notify("IFNET", ifp->if_xname, "ATTACH", NULL);
804 /* Announce the interface. */
805 rt_ifannouncemsg(ifp, IFAN_ARRIVAL);
809 if_attachdomain(void *dummy)
813 TAILQ_FOREACH(ifp, &V_ifnet, if_link)
814 if_attachdomain1(ifp);
816 SYSINIT(domainifattach, SI_SUB_PROTO_IFATTACHDOMAIN, SI_ORDER_SECOND,
817 if_attachdomain, NULL);
820 if_attachdomain1(struct ifnet *ifp)
825 * Since dp->dom_ifattach calls malloc() with M_WAITOK, we
826 * cannot lock ifp->if_afdata initialization, entirely.
829 if (ifp->if_afdata_initialized >= domain_init_status) {
830 IF_AFDATA_UNLOCK(ifp);
831 log(LOG_WARNING, "%s called more than once on %s\n",
832 __func__, ifp->if_xname);
835 ifp->if_afdata_initialized = domain_init_status;
836 IF_AFDATA_UNLOCK(ifp);
838 /* address family dependent data region */
839 bzero(ifp->if_afdata, sizeof(ifp->if_afdata));
840 for (dp = domains; dp; dp = dp->dom_next) {
841 if (dp->dom_ifattach)
842 ifp->if_afdata[dp->dom_family] =
843 (*dp->dom_ifattach)(ifp);
848 * Remove any unicast or broadcast network addresses from an interface.
851 if_purgeaddrs(struct ifnet *ifp)
853 struct ifaddr *ifa, *next;
855 /* XXX cannot hold IF_ADDR_WLOCK over called functions. */
856 TAILQ_FOREACH_SAFE(ifa, &ifp->if_addrhead, ifa_link, next) {
857 if (ifa->ifa_addr->sa_family == AF_LINK)
860 /* XXX: Ugly!! ad hoc just for INET */
861 if (ifa->ifa_addr->sa_family == AF_INET) {
862 struct ifaliasreq ifr;
864 bzero(&ifr, sizeof(ifr));
865 ifr.ifra_addr = *ifa->ifa_addr;
866 if (ifa->ifa_dstaddr)
867 ifr.ifra_broadaddr = *ifa->ifa_dstaddr;
868 if (in_control(NULL, SIOCDIFADDR, (caddr_t)&ifr, ifp,
874 if (ifa->ifa_addr->sa_family == AF_INET6) {
876 /* ifp_addrhead is already updated */
881 TAILQ_REMOVE(&ifp->if_addrhead, ifa, ifa_link);
882 IF_ADDR_WUNLOCK(ifp);
888 * Remove any multicast network addresses from an interface when an ifnet
892 if_purgemaddrs(struct ifnet *ifp)
894 struct ifmultiaddr *ifma;
895 struct ifmultiaddr *next;
898 TAILQ_FOREACH_SAFE(ifma, &ifp->if_multiaddrs, ifma_link, next)
899 if_delmulti_locked(ifp, ifma, 1);
900 IF_ADDR_WUNLOCK(ifp);
904 * Detach an interface, removing it from the list of "active" interfaces.
905 * If vmove flag is set on entry to if_detach_internal(), perform only a
906 * limited subset of cleanup tasks, given that we are moving an ifnet from
907 * one vnet to another, where it must be fully operational.
909 * XXXRW: There are some significant questions about event ordering, and
910 * how to prevent things from starting to use the interface during detach.
913 if_detach(struct ifnet *ifp)
916 CURVNET_SET_QUIET(ifp->if_vnet);
917 if_detach_internal(ifp, 0, NULL);
922 * The vmove flag, if set, indicates that we are called from a callpath
923 * that is moving an interface to a different vnet instance.
925 * The shutdown flag, if set, indicates that we are called in the
926 * process of shutting down a vnet instance. Currently only the
927 * vnet_if_return SYSUNINIT function sets it. Note: we can be called
928 * on a vnet instance shutdown without this flag being set, e.g., when
929 * the cloned interfaces are destoyed as first thing of teardown.
932 if_detach_internal(struct ifnet *ifp, int vmove, struct if_clone **ifcp)
942 shutdown = (ifp->if_vnet->vnet_state > SI_SUB_VNET &&
943 ifp->if_vnet->vnet_state < SI_SUB_VNET_DONE) ? 1 : 0;
946 TAILQ_FOREACH(iter, &V_ifnet, if_link)
948 TAILQ_REMOVE(&V_ifnet, ifp, if_link);
955 * While we would want to panic here, we cannot
956 * guarantee that the interface is indeed still on
957 * the list given we don't hold locks all the way.
962 panic("%s: ifp=%p not on the ifnet tailq %p",
963 __func__, ifp, &V_ifnet);
965 return; /* XXX this should panic as well? */
970 * At this point we know the interface still was on the ifnet list
971 * and we removed it so we are in a stable state.
974 curvnet->vnet_ifcnt--;
978 * In any case (destroy or vmove) detach us from the groups
979 * and remove/wait for pending events on the taskq.
980 * XXX-BZ in theory an interface could still enqueue a taskq change?
984 taskqueue_drain(taskqueue_swi, &ifp->if_linktask);
987 * Check if this is a cloned interface or not. Must do even if
988 * shutting down as a if_vmove_reclaim() would move the ifp and
989 * the if_clone_addgroup() will have a corrupted string overwise
990 * from a gibberish pointer.
992 if (vmove && ifcp != NULL)
993 *ifcp = if_clone_findifc(ifp);
999 * On VNET shutdown abort here as the stack teardown will do all
1000 * the work top-down for us.
1004 * In case of a vmove we are done here without error.
1005 * If we would signal an error it would lead to the same
1006 * abort as if we did not find the ifnet anymore.
1007 * if_detach() calls us in void context and does not care
1008 * about an early abort notification, so life is splendid :)
1010 goto finish_vnet_shutdown;
1015 * At this point we are not tearing down a VNET and are either
1016 * going to destroy or vmove the interface and have to cleanup
1021 * Remove routes and flush queues.
1024 if (ALTQ_IS_ENABLED(&ifp->if_snd))
1025 altq_disable(&ifp->if_snd);
1026 if (ALTQ_IS_ATTACHED(&ifp->if_snd))
1027 altq_detach(&ifp->if_snd);
1038 * Remove all IPv6 kernel structs related to ifp. This should be done
1039 * before removing routing entries below, since IPv6 interface direct
1040 * routes are expected to be removed by the IPv6-specific kernel API.
1041 * Otherwise, the kernel will detect some inconsistency and bark it.
1045 if_purgemaddrs(ifp);
1047 /* Announce that the interface is gone. */
1048 rt_ifannouncemsg(ifp, IFAN_DEPARTURE);
1049 EVENTHANDLER_INVOKE(ifnet_departure_event, ifp);
1050 if (IS_DEFAULT_VNET(curvnet))
1051 devctl_notify("IFNET", ifp->if_xname, "DETACH", NULL);
1055 * Prevent further calls into the device driver via ifnet.
1060 * Remove link ifaddr pointer and maybe decrement if_index.
1061 * Clean up all addresses.
1063 ifp->if_addr = NULL;
1065 /* We can now free link ifaddr. */
1067 if (!TAILQ_EMPTY(&ifp->if_addrhead)) {
1068 ifa = TAILQ_FIRST(&ifp->if_addrhead);
1069 TAILQ_REMOVE(&ifp->if_addrhead, ifa, ifa_link);
1070 IF_ADDR_WUNLOCK(ifp);
1073 IF_ADDR_WUNLOCK(ifp);
1076 rt_flushifroutes(ifp);
1079 finish_vnet_shutdown:
1082 * We cannot hold the lock over dom_ifdetach calls as they might
1083 * sleep, for example trying to drain a callout, thus open up the
1084 * theoretical race with re-attaching.
1086 IF_AFDATA_LOCK(ifp);
1087 i = ifp->if_afdata_initialized;
1088 ifp->if_afdata_initialized = 0;
1089 IF_AFDATA_UNLOCK(ifp);
1090 for (dp = domains; i > 0 && dp; dp = dp->dom_next) {
1091 if (dp->dom_ifdetach && ifp->if_afdata[dp->dom_family]) {
1092 (*dp->dom_ifdetach)(ifp,
1093 ifp->if_afdata[dp->dom_family]);
1094 ifp->if_afdata[dp->dom_family] = NULL;
1103 * if_vmove() performs a limited version of if_detach() in current
1104 * vnet and if_attach()es the ifnet to the vnet specified as 2nd arg.
1105 * An attempt is made to shrink if_index in current vnet, find an
1106 * unused if_index in target vnet and calls if_grow() if necessary,
1107 * and finally find an unused if_xname for the target vnet.
1110 if_vmove(struct ifnet *ifp, struct vnet *new_vnet)
1112 struct if_clone *ifc;
1113 u_int bif_dlt, bif_hdrlen;
1117 * if_detach_internal() will call the eventhandler to notify
1118 * interface departure. That will detach if_bpf. We need to
1119 * safe the dlt and hdrlen so we can re-attach it later.
1121 bpf_get_bp_params(ifp->if_bpf, &bif_dlt, &bif_hdrlen);
1124 * Detach from current vnet, but preserve LLADDR info, do not
1125 * mark as dead etc. so that the ifnet can be reattached later.
1126 * If we cannot find it, we lost the race to someone else.
1128 rc = if_detach_internal(ifp, 1, &ifc);
1133 * Unlink the ifnet from ifindex_table[] in current vnet, and shrink
1134 * the if_index for that vnet if possible.
1136 * NOTE: IFNET_WLOCK/IFNET_WUNLOCK() are assumed to be unvirtualized,
1137 * or we'd lock on one vnet and unlock on another.
1140 ifindex_free_locked(ifp->if_index);
1144 * Perform interface-specific reassignment tasks, if provided by
1147 if (ifp->if_reassign != NULL)
1148 ifp->if_reassign(ifp, new_vnet, NULL);
1151 * Switch to the context of the target vnet.
1153 CURVNET_SET_QUIET(new_vnet);
1156 ifp->if_index = ifindex_alloc();
1157 ifnet_setbyindex_locked(ifp->if_index, ifp);
1160 if_attach_internal(ifp, 1, ifc);
1162 if (ifp->if_bpf == NULL)
1163 bpfattach(ifp, bif_dlt, bif_hdrlen);
1169 * Move an ifnet to or from another child prison/vnet, specified by the jail id.
1172 if_vmove_loan(struct thread *td, struct ifnet *ifp, char *ifname, int jid)
1178 /* Try to find the prison within our visibility. */
1179 sx_slock(&allprison_lock);
1180 pr = prison_find_child(td->td_ucred->cr_prison, jid);
1181 sx_sunlock(&allprison_lock);
1184 prison_hold_locked(pr);
1185 mtx_unlock(&pr->pr_mtx);
1187 /* Do not try to move the iface from and to the same prison. */
1188 if (pr->pr_vnet == ifp->if_vnet) {
1193 /* Make sure the named iface does not exists in the dst. prison/vnet. */
1194 /* XXX Lock interfaces to avoid races. */
1195 CURVNET_SET_QUIET(pr->pr_vnet);
1196 difp = ifunit(ifname);
1203 /* Make sure the VNET is stable. */
1204 shutdown = (ifp->if_vnet->vnet_state > SI_SUB_VNET &&
1205 ifp->if_vnet->vnet_state < SI_SUB_VNET_DONE) ? 1 : 0;
1213 /* Move the interface into the child jail/vnet. */
1214 if_vmove(ifp, pr->pr_vnet);
1216 /* Report the new if_xname back to the userland. */
1217 sprintf(ifname, "%s", ifp->if_xname);
1224 if_vmove_reclaim(struct thread *td, char *ifname, int jid)
1227 struct vnet *vnet_dst;
1231 /* Try to find the prison within our visibility. */
1232 sx_slock(&allprison_lock);
1233 pr = prison_find_child(td->td_ucred->cr_prison, jid);
1234 sx_sunlock(&allprison_lock);
1237 prison_hold_locked(pr);
1238 mtx_unlock(&pr->pr_mtx);
1240 /* Make sure the named iface exists in the source prison/vnet. */
1241 CURVNET_SET(pr->pr_vnet);
1242 ifp = ifunit(ifname); /* XXX Lock to avoid races. */
1249 /* Do not try to move the iface from and to the same prison. */
1250 vnet_dst = TD_TO_VNET(td);
1251 if (vnet_dst == ifp->if_vnet) {
1257 /* Make sure the VNET is stable. */
1258 shutdown = (ifp->if_vnet->vnet_state > SI_SUB_VNET &&
1259 ifp->if_vnet->vnet_state < SI_SUB_VNET_DONE) ? 1 : 0;
1266 /* Get interface back from child jail/vnet. */
1267 if_vmove(ifp, vnet_dst);
1270 /* Report the new if_xname back to the userland. */
1271 sprintf(ifname, "%s", ifp->if_xname);
1279 * Add a group to an interface
1282 if_addgroup(struct ifnet *ifp, const char *groupname)
1284 struct ifg_list *ifgl;
1285 struct ifg_group *ifg = NULL;
1286 struct ifg_member *ifgm;
1289 if (groupname[0] && groupname[strlen(groupname) - 1] >= '0' &&
1290 groupname[strlen(groupname) - 1] <= '9')
1294 TAILQ_FOREACH(ifgl, &ifp->if_groups, ifgl_next)
1295 if (!strcmp(ifgl->ifgl_group->ifg_group, groupname)) {
1300 if ((ifgl = (struct ifg_list *)malloc(sizeof(struct ifg_list), M_TEMP,
1301 M_NOWAIT)) == NULL) {
1306 if ((ifgm = (struct ifg_member *)malloc(sizeof(struct ifg_member),
1307 M_TEMP, M_NOWAIT)) == NULL) {
1313 TAILQ_FOREACH(ifg, &V_ifg_head, ifg_next)
1314 if (!strcmp(ifg->ifg_group, groupname))
1318 if ((ifg = (struct ifg_group *)malloc(sizeof(struct ifg_group),
1319 M_TEMP, M_NOWAIT)) == NULL) {
1325 strlcpy(ifg->ifg_group, groupname, sizeof(ifg->ifg_group));
1326 ifg->ifg_refcnt = 0;
1327 TAILQ_INIT(&ifg->ifg_members);
1328 TAILQ_INSERT_TAIL(&V_ifg_head, ifg, ifg_next);
1333 ifgl->ifgl_group = ifg;
1334 ifgm->ifgm_ifp = ifp;
1337 TAILQ_INSERT_TAIL(&ifg->ifg_members, ifgm, ifgm_next);
1338 TAILQ_INSERT_TAIL(&ifp->if_groups, ifgl, ifgl_next);
1339 IF_ADDR_WUNLOCK(ifp);
1344 EVENTHANDLER_INVOKE(group_attach_event, ifg);
1345 EVENTHANDLER_INVOKE(group_change_event, groupname);
1351 * Remove a group from an interface
1354 if_delgroup(struct ifnet *ifp, const char *groupname)
1356 struct ifg_list *ifgl;
1357 struct ifg_member *ifgm;
1360 TAILQ_FOREACH(ifgl, &ifp->if_groups, ifgl_next)
1361 if (!strcmp(ifgl->ifgl_group->ifg_group, groupname))
1369 TAILQ_REMOVE(&ifp->if_groups, ifgl, ifgl_next);
1370 IF_ADDR_WUNLOCK(ifp);
1372 TAILQ_FOREACH(ifgm, &ifgl->ifgl_group->ifg_members, ifgm_next)
1373 if (ifgm->ifgm_ifp == ifp)
1377 TAILQ_REMOVE(&ifgl->ifgl_group->ifg_members, ifgm, ifgm_next);
1381 if (--ifgl->ifgl_group->ifg_refcnt == 0) {
1382 TAILQ_REMOVE(&V_ifg_head, ifgl->ifgl_group, ifg_next);
1384 EVENTHANDLER_INVOKE(group_detach_event, ifgl->ifgl_group);
1385 free(ifgl->ifgl_group, M_TEMP);
1391 EVENTHANDLER_INVOKE(group_change_event, groupname);
1397 * Remove an interface from all groups
1400 if_delgroups(struct ifnet *ifp)
1402 struct ifg_list *ifgl;
1403 struct ifg_member *ifgm;
1404 char groupname[IFNAMSIZ];
1407 while (!TAILQ_EMPTY(&ifp->if_groups)) {
1408 ifgl = TAILQ_FIRST(&ifp->if_groups);
1410 strlcpy(groupname, ifgl->ifgl_group->ifg_group, IFNAMSIZ);
1413 TAILQ_REMOVE(&ifp->if_groups, ifgl, ifgl_next);
1414 IF_ADDR_WUNLOCK(ifp);
1416 TAILQ_FOREACH(ifgm, &ifgl->ifgl_group->ifg_members, ifgm_next)
1417 if (ifgm->ifgm_ifp == ifp)
1421 TAILQ_REMOVE(&ifgl->ifgl_group->ifg_members, ifgm,
1426 if (--ifgl->ifgl_group->ifg_refcnt == 0) {
1427 TAILQ_REMOVE(&V_ifg_head, ifgl->ifgl_group, ifg_next);
1429 EVENTHANDLER_INVOKE(group_detach_event,
1431 free(ifgl->ifgl_group, M_TEMP);
1437 EVENTHANDLER_INVOKE(group_change_event, groupname);
1445 * Stores all groups from an interface in memory pointed
1449 if_getgroup(struct ifgroupreq *data, struct ifnet *ifp)
1452 struct ifg_list *ifgl;
1453 struct ifg_req ifgrq, *ifgp;
1454 struct ifgroupreq *ifgr = data;
1456 if (ifgr->ifgr_len == 0) {
1458 TAILQ_FOREACH(ifgl, &ifp->if_groups, ifgl_next)
1459 ifgr->ifgr_len += sizeof(struct ifg_req);
1460 IF_ADDR_RUNLOCK(ifp);
1464 len = ifgr->ifgr_len;
1465 ifgp = ifgr->ifgr_groups;
1468 TAILQ_FOREACH(ifgl, &ifp->if_groups, ifgl_next) {
1469 if (len < sizeof(ifgrq)) {
1470 IF_ADDR_RUNLOCK(ifp);
1473 bzero(&ifgrq, sizeof ifgrq);
1474 strlcpy(ifgrq.ifgrq_group, ifgl->ifgl_group->ifg_group,
1475 sizeof(ifgrq.ifgrq_group));
1476 if ((error = copyout(&ifgrq, ifgp, sizeof(struct ifg_req)))) {
1477 IF_ADDR_RUNLOCK(ifp);
1480 len -= sizeof(ifgrq);
1483 IF_ADDR_RUNLOCK(ifp);
1489 * Stores all members of a group in memory pointed to by data
1492 if_getgroupmembers(struct ifgroupreq *data)
1494 struct ifgroupreq *ifgr = data;
1495 struct ifg_group *ifg;
1496 struct ifg_member *ifgm;
1497 struct ifg_req ifgrq, *ifgp;
1501 TAILQ_FOREACH(ifg, &V_ifg_head, ifg_next)
1502 if (!strcmp(ifg->ifg_group, ifgr->ifgr_name))
1509 if (ifgr->ifgr_len == 0) {
1510 TAILQ_FOREACH(ifgm, &ifg->ifg_members, ifgm_next)
1511 ifgr->ifgr_len += sizeof(ifgrq);
1516 len = ifgr->ifgr_len;
1517 ifgp = ifgr->ifgr_groups;
1518 TAILQ_FOREACH(ifgm, &ifg->ifg_members, ifgm_next) {
1519 if (len < sizeof(ifgrq)) {
1523 bzero(&ifgrq, sizeof ifgrq);
1524 strlcpy(ifgrq.ifgrq_member, ifgm->ifgm_ifp->if_xname,
1525 sizeof(ifgrq.ifgrq_member));
1526 if ((error = copyout(&ifgrq, ifgp, sizeof(struct ifg_req)))) {
1530 len -= sizeof(ifgrq);
1539 * Return counter values from counter(9)s stored in ifnet.
1542 if_get_counter_default(struct ifnet *ifp, ift_counter cnt)
1545 KASSERT(cnt < IFCOUNTERS, ("%s: invalid cnt %d", __func__, cnt));
1547 return (counter_u64_fetch(ifp->if_counters[cnt]));
1551 * Increase an ifnet counter. Usually used for counters shared
1552 * between the stack and a driver, but function supports them all.
1555 if_inc_counter(struct ifnet *ifp, ift_counter cnt, int64_t inc)
1558 KASSERT(cnt < IFCOUNTERS, ("%s: invalid cnt %d", __func__, cnt));
1560 counter_u64_add(ifp->if_counters[cnt], inc);
1564 * Copy data from ifnet to userland API structure if_data.
1567 if_data_copy(struct ifnet *ifp, struct if_data *ifd)
1570 ifd->ifi_type = ifp->if_type;
1571 ifd->ifi_physical = 0;
1572 ifd->ifi_addrlen = ifp->if_addrlen;
1573 ifd->ifi_hdrlen = ifp->if_hdrlen;
1574 ifd->ifi_link_state = ifp->if_link_state;
1576 ifd->ifi_datalen = sizeof(struct if_data);
1577 ifd->ifi_mtu = ifp->if_mtu;
1578 ifd->ifi_metric = ifp->if_metric;
1579 ifd->ifi_baudrate = ifp->if_baudrate;
1580 ifd->ifi_hwassist = ifp->if_hwassist;
1581 ifd->ifi_epoch = ifp->if_epoch;
1582 ifd->ifi_lastchange = ifp->if_lastchange;
1584 ifd->ifi_ipackets = ifp->if_get_counter(ifp, IFCOUNTER_IPACKETS);
1585 ifd->ifi_ierrors = ifp->if_get_counter(ifp, IFCOUNTER_IERRORS);
1586 ifd->ifi_opackets = ifp->if_get_counter(ifp, IFCOUNTER_OPACKETS);
1587 ifd->ifi_oerrors = ifp->if_get_counter(ifp, IFCOUNTER_OERRORS);
1588 ifd->ifi_collisions = ifp->if_get_counter(ifp, IFCOUNTER_COLLISIONS);
1589 ifd->ifi_ibytes = ifp->if_get_counter(ifp, IFCOUNTER_IBYTES);
1590 ifd->ifi_obytes = ifp->if_get_counter(ifp, IFCOUNTER_OBYTES);
1591 ifd->ifi_imcasts = ifp->if_get_counter(ifp, IFCOUNTER_IMCASTS);
1592 ifd->ifi_omcasts = ifp->if_get_counter(ifp, IFCOUNTER_OMCASTS);
1593 ifd->ifi_iqdrops = ifp->if_get_counter(ifp, IFCOUNTER_IQDROPS);
1594 ifd->ifi_oqdrops = ifp->if_get_counter(ifp, IFCOUNTER_OQDROPS);
1595 ifd->ifi_noproto = ifp->if_get_counter(ifp, IFCOUNTER_NOPROTO);
1599 * Wrapper functions for struct ifnet address list locking macros. These are
1600 * used by kernel modules to avoid encoding programming interface or binary
1601 * interface assumptions that may be violated when kernel-internal locking
1602 * approaches change.
1605 if_addr_rlock(struct ifnet *ifp)
1612 if_addr_runlock(struct ifnet *ifp)
1615 IF_ADDR_RUNLOCK(ifp);
1619 if_maddr_rlock(if_t ifp)
1622 IF_ADDR_RLOCK((struct ifnet *)ifp);
1626 if_maddr_runlock(if_t ifp)
1629 IF_ADDR_RUNLOCK((struct ifnet *)ifp);
1633 * Initialization, destruction and refcounting functions for ifaddrs.
1636 ifa_alloc(size_t size, int flags)
1640 KASSERT(size >= sizeof(struct ifaddr),
1641 ("%s: invalid size %zu", __func__, size));
1643 ifa = malloc(size, M_IFADDR, M_ZERO | flags);
1647 if ((ifa->ifa_opackets = counter_u64_alloc(flags)) == NULL)
1649 if ((ifa->ifa_ipackets = counter_u64_alloc(flags)) == NULL)
1651 if ((ifa->ifa_obytes = counter_u64_alloc(flags)) == NULL)
1653 if ((ifa->ifa_ibytes = counter_u64_alloc(flags)) == NULL)
1656 refcount_init(&ifa->ifa_refcnt, 1);
1661 /* free(NULL) is okay */
1662 counter_u64_free(ifa->ifa_opackets);
1663 counter_u64_free(ifa->ifa_ipackets);
1664 counter_u64_free(ifa->ifa_obytes);
1665 counter_u64_free(ifa->ifa_ibytes);
1666 free(ifa, M_IFADDR);
1672 ifa_ref(struct ifaddr *ifa)
1675 refcount_acquire(&ifa->ifa_refcnt);
1679 ifa_free(struct ifaddr *ifa)
1682 if (refcount_release(&ifa->ifa_refcnt)) {
1683 counter_u64_free(ifa->ifa_opackets);
1684 counter_u64_free(ifa->ifa_ipackets);
1685 counter_u64_free(ifa->ifa_obytes);
1686 counter_u64_free(ifa->ifa_ibytes);
1687 free(ifa, M_IFADDR);
1692 ifa_maintain_loopback_route(int cmd, const char *otype, struct ifaddr *ifa,
1693 struct sockaddr *ia)
1696 struct rt_addrinfo info;
1697 struct sockaddr_dl null_sdl;
1702 bzero(&info, sizeof(info));
1703 if (cmd != RTM_DELETE)
1704 info.rti_ifp = V_loif;
1705 info.rti_flags = ifa->ifa_flags | RTF_HOST | RTF_STATIC;
1706 info.rti_info[RTAX_DST] = ia;
1707 info.rti_info[RTAX_GATEWAY] = (struct sockaddr *)&null_sdl;
1708 link_init_sdl(ifp, (struct sockaddr *)&null_sdl, ifp->if_type);
1710 error = rtrequest1_fib(cmd, &info, NULL, ifp->if_fib);
1713 log(LOG_DEBUG, "%s: %s failed for interface %s: %u\n",
1714 __func__, otype, if_name(ifp), error);
1720 ifa_add_loopback_route(struct ifaddr *ifa, struct sockaddr *ia)
1723 return (ifa_maintain_loopback_route(RTM_ADD, "insertion", ifa, ia));
1727 ifa_del_loopback_route(struct ifaddr *ifa, struct sockaddr *ia)
1730 return (ifa_maintain_loopback_route(RTM_DELETE, "deletion", ifa, ia));
1734 ifa_switch_loopback_route(struct ifaddr *ifa, struct sockaddr *ia)
1737 return (ifa_maintain_loopback_route(RTM_CHANGE, "switch", ifa, ia));
1741 * XXX: Because sockaddr_dl has deeper structure than the sockaddr
1742 * structs used to represent other address families, it is necessary
1743 * to perform a different comparison.
1746 #define sa_dl_equal(a1, a2) \
1747 ((((const struct sockaddr_dl *)(a1))->sdl_len == \
1748 ((const struct sockaddr_dl *)(a2))->sdl_len) && \
1749 (bcmp(CLLADDR((const struct sockaddr_dl *)(a1)), \
1750 CLLADDR((const struct sockaddr_dl *)(a2)), \
1751 ((const struct sockaddr_dl *)(a1))->sdl_alen) == 0))
1754 * Locate an interface based on a complete address.
1757 static struct ifaddr *
1758 ifa_ifwithaddr_internal(const struct sockaddr *addr, int getref)
1763 IFNET_RLOCK_NOSLEEP();
1764 TAILQ_FOREACH(ifp, &V_ifnet, if_link) {
1766 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
1767 if (ifa->ifa_addr->sa_family != addr->sa_family)
1769 if (sa_equal(addr, ifa->ifa_addr)) {
1772 IF_ADDR_RUNLOCK(ifp);
1775 /* IP6 doesn't have broadcast */
1776 if ((ifp->if_flags & IFF_BROADCAST) &&
1777 ifa->ifa_broadaddr &&
1778 ifa->ifa_broadaddr->sa_len != 0 &&
1779 sa_equal(ifa->ifa_broadaddr, addr)) {
1782 IF_ADDR_RUNLOCK(ifp);
1786 IF_ADDR_RUNLOCK(ifp);
1790 IFNET_RUNLOCK_NOSLEEP();
1795 ifa_ifwithaddr(const struct sockaddr *addr)
1798 return (ifa_ifwithaddr_internal(addr, 1));
1802 ifa_ifwithaddr_check(const struct sockaddr *addr)
1805 return (ifa_ifwithaddr_internal(addr, 0) != NULL);
1809 * Locate an interface based on the broadcast address.
1813 ifa_ifwithbroadaddr(const struct sockaddr *addr, int fibnum)
1818 IFNET_RLOCK_NOSLEEP();
1819 TAILQ_FOREACH(ifp, &V_ifnet, if_link) {
1820 if ((fibnum != RT_ALL_FIBS) && (ifp->if_fib != fibnum))
1823 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
1824 if (ifa->ifa_addr->sa_family != addr->sa_family)
1826 if ((ifp->if_flags & IFF_BROADCAST) &&
1827 ifa->ifa_broadaddr &&
1828 ifa->ifa_broadaddr->sa_len != 0 &&
1829 sa_equal(ifa->ifa_broadaddr, addr)) {
1831 IF_ADDR_RUNLOCK(ifp);
1835 IF_ADDR_RUNLOCK(ifp);
1839 IFNET_RUNLOCK_NOSLEEP();
1844 * Locate the point to point interface with a given destination address.
1848 ifa_ifwithdstaddr(const struct sockaddr *addr, int fibnum)
1853 IFNET_RLOCK_NOSLEEP();
1854 TAILQ_FOREACH(ifp, &V_ifnet, if_link) {
1855 if ((ifp->if_flags & IFF_POINTOPOINT) == 0)
1857 if ((fibnum != RT_ALL_FIBS) && (ifp->if_fib != fibnum))
1860 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
1861 if (ifa->ifa_addr->sa_family != addr->sa_family)
1863 if (ifa->ifa_dstaddr != NULL &&
1864 sa_equal(addr, ifa->ifa_dstaddr)) {
1866 IF_ADDR_RUNLOCK(ifp);
1870 IF_ADDR_RUNLOCK(ifp);
1874 IFNET_RUNLOCK_NOSLEEP();
1879 * Find an interface on a specific network. If many, choice
1880 * is most specific found.
1883 ifa_ifwithnet(const struct sockaddr *addr, int ignore_ptp, int fibnum)
1887 struct ifaddr *ifa_maybe = NULL;
1888 u_int af = addr->sa_family;
1889 const char *addr_data = addr->sa_data, *cplim;
1892 * AF_LINK addresses can be looked up directly by their index number,
1893 * so do that if we can.
1895 if (af == AF_LINK) {
1896 const struct sockaddr_dl *sdl = (const struct sockaddr_dl *)addr;
1897 if (sdl->sdl_index && sdl->sdl_index <= V_if_index)
1898 return (ifaddr_byindex(sdl->sdl_index));
1902 * Scan though each interface, looking for ones that have addresses
1903 * in this address family and the requested fib. Maintain a reference
1904 * on ifa_maybe once we find one, as we release the IF_ADDR_RLOCK() that
1905 * kept it stable when we move onto the next interface.
1907 IFNET_RLOCK_NOSLEEP();
1908 TAILQ_FOREACH(ifp, &V_ifnet, if_link) {
1909 if ((fibnum != RT_ALL_FIBS) && (ifp->if_fib != fibnum))
1912 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
1913 const char *cp, *cp2, *cp3;
1915 if (ifa->ifa_addr->sa_family != af)
1917 if (af == AF_INET &&
1918 ifp->if_flags & IFF_POINTOPOINT && !ignore_ptp) {
1920 * This is a bit broken as it doesn't
1921 * take into account that the remote end may
1922 * be a single node in the network we are
1924 * The trouble is that we don't know the
1925 * netmask for the remote end.
1927 if (ifa->ifa_dstaddr != NULL &&
1928 sa_equal(addr, ifa->ifa_dstaddr)) {
1930 IF_ADDR_RUNLOCK(ifp);
1935 * Scan all the bits in the ifa's address.
1936 * If a bit dissagrees with what we are
1937 * looking for, mask it with the netmask
1938 * to see if it really matters.
1939 * (A byte at a time)
1941 if (ifa->ifa_netmask == 0)
1944 cp2 = ifa->ifa_addr->sa_data;
1945 cp3 = ifa->ifa_netmask->sa_data;
1946 cplim = ifa->ifa_netmask->sa_len
1947 + (char *)ifa->ifa_netmask;
1949 if ((*cp++ ^ *cp2++) & *cp3++)
1950 goto next; /* next address! */
1952 * If the netmask of what we just found
1953 * is more specific than what we had before
1954 * (if we had one), or if the virtual status
1955 * of new prefix is better than of the old one,
1956 * then remember the new one before continuing
1957 * to search for an even better one.
1959 if (ifa_maybe == NULL ||
1960 ifa_preferred(ifa_maybe, ifa) ||
1961 rn_refines((caddr_t)ifa->ifa_netmask,
1962 (caddr_t)ifa_maybe->ifa_netmask)) {
1963 if (ifa_maybe != NULL)
1964 ifa_free(ifa_maybe);
1970 IF_ADDR_RUNLOCK(ifp);
1975 IFNET_RUNLOCK_NOSLEEP();
1976 if (ifa_maybe != NULL)
1977 ifa_free(ifa_maybe);
1982 * Find an interface address specific to an interface best matching
1986 ifaof_ifpforaddr(const struct sockaddr *addr, struct ifnet *ifp)
1989 const char *cp, *cp2, *cp3;
1991 struct ifaddr *ifa_maybe = NULL;
1992 u_int af = addr->sa_family;
1997 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
1998 if (ifa->ifa_addr->sa_family != af)
2000 if (ifa_maybe == NULL)
2002 if (ifa->ifa_netmask == 0) {
2003 if (sa_equal(addr, ifa->ifa_addr) ||
2004 (ifa->ifa_dstaddr &&
2005 sa_equal(addr, ifa->ifa_dstaddr)))
2009 if (ifp->if_flags & IFF_POINTOPOINT) {
2010 if (sa_equal(addr, ifa->ifa_dstaddr))
2014 cp2 = ifa->ifa_addr->sa_data;
2015 cp3 = ifa->ifa_netmask->sa_data;
2016 cplim = ifa->ifa_netmask->sa_len + (char *)ifa->ifa_netmask;
2017 for (; cp3 < cplim; cp3++)
2018 if ((*cp++ ^ *cp2++) & *cp3)
2028 IF_ADDR_RUNLOCK(ifp);
2033 * See whether new ifa is better than current one:
2034 * 1) A non-virtual one is preferred over virtual.
2035 * 2) A virtual in master state preferred over any other state.
2037 * Used in several address selecting functions.
2040 ifa_preferred(struct ifaddr *cur, struct ifaddr *next)
2043 return (cur->ifa_carp && (!next->ifa_carp ||
2044 ((*carp_master_p)(next) && !(*carp_master_p)(cur))));
2047 #include <net/if_llatbl.h>
2050 * Default action when installing a route with a Link Level gateway.
2051 * Lookup an appropriate real ifa to point to.
2052 * This should be moved to /sys/net/link.c eventually.
2055 link_rtrequest(int cmd, struct rtentry *rt, struct rt_addrinfo *info)
2057 struct ifaddr *ifa, *oifa;
2058 struct sockaddr *dst;
2061 if (cmd != RTM_ADD || ((ifa = rt->rt_ifa) == NULL) ||
2062 ((ifp = ifa->ifa_ifp) == NULL) || ((dst = rt_key(rt)) == NULL))
2064 ifa = ifaof_ifpforaddr(dst, ifp);
2069 if (ifa->ifa_rtrequest && ifa->ifa_rtrequest != link_rtrequest)
2070 ifa->ifa_rtrequest(cmd, rt, info);
2074 struct sockaddr_dl *
2075 link_alloc_sdl(size_t size, int flags)
2078 return (malloc(size, M_TEMP, flags));
2082 link_free_sdl(struct sockaddr *sa)
2088 * Fills in given sdl with interface basic info.
2089 * Returns pointer to filled sdl.
2091 struct sockaddr_dl *
2092 link_init_sdl(struct ifnet *ifp, struct sockaddr *paddr, u_char iftype)
2094 struct sockaddr_dl *sdl;
2096 sdl = (struct sockaddr_dl *)paddr;
2097 memset(sdl, 0, sizeof(struct sockaddr_dl));
2098 sdl->sdl_len = sizeof(struct sockaddr_dl);
2099 sdl->sdl_family = AF_LINK;
2100 sdl->sdl_index = ifp->if_index;
2101 sdl->sdl_type = iftype;
2107 * Mark an interface down and notify protocols of
2111 if_unroute(struct ifnet *ifp, int flag, int fam)
2115 KASSERT(flag == IFF_UP, ("if_unroute: flag != IFF_UP"));
2117 ifp->if_flags &= ~flag;
2118 getmicrotime(&ifp->if_lastchange);
2119 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link)
2120 if (fam == PF_UNSPEC || (fam == ifa->ifa_addr->sa_family))
2121 pfctlinput(PRC_IFDOWN, ifa->ifa_addr);
2122 ifp->if_qflush(ifp);
2125 (*carp_linkstate_p)(ifp);
2130 * Mark an interface up and notify protocols of
2134 if_route(struct ifnet *ifp, int flag, int fam)
2138 KASSERT(flag == IFF_UP, ("if_route: flag != IFF_UP"));
2140 ifp->if_flags |= flag;
2141 getmicrotime(&ifp->if_lastchange);
2142 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link)
2143 if (fam == PF_UNSPEC || (fam == ifa->ifa_addr->sa_family))
2144 pfctlinput(PRC_IFUP, ifa->ifa_addr);
2146 (*carp_linkstate_p)(ifp);
2153 void (*vlan_link_state_p)(struct ifnet *); /* XXX: private from if_vlan */
2154 void (*vlan_trunk_cap_p)(struct ifnet *); /* XXX: private from if_vlan */
2155 struct ifnet *(*vlan_trunkdev_p)(struct ifnet *);
2156 struct ifnet *(*vlan_devat_p)(struct ifnet *, uint16_t);
2157 int (*vlan_tag_p)(struct ifnet *, uint16_t *);
2158 int (*vlan_setcookie_p)(struct ifnet *, void *);
2159 void *(*vlan_cookie_p)(struct ifnet *);
2162 * Handle a change in the interface link state. To avoid LORs
2163 * between driver lock and upper layer locks, as well as possible
2164 * recursions, we post event to taskqueue, and all job
2165 * is done in static do_link_state_change().
2168 if_link_state_change(struct ifnet *ifp, int link_state)
2170 /* Return if state hasn't changed. */
2171 if (ifp->if_link_state == link_state)
2174 ifp->if_link_state = link_state;
2176 taskqueue_enqueue(taskqueue_swi, &ifp->if_linktask);
2180 do_link_state_change(void *arg, int pending)
2182 struct ifnet *ifp = (struct ifnet *)arg;
2183 int link_state = ifp->if_link_state;
2184 CURVNET_SET(ifp->if_vnet);
2186 /* Notify that the link state has changed. */
2188 if (ifp->if_vlantrunk != NULL)
2189 (*vlan_link_state_p)(ifp);
2191 if ((ifp->if_type == IFT_ETHER || ifp->if_type == IFT_L2VLAN) &&
2192 ifp->if_l2com != NULL)
2193 (*ng_ether_link_state_p)(ifp, link_state);
2195 (*carp_linkstate_p)(ifp);
2197 (*bridge_linkstate_p)(ifp);
2199 (*lagg_linkstate_p)(ifp, link_state);
2201 if (IS_DEFAULT_VNET(curvnet))
2202 devctl_notify("IFNET", ifp->if_xname,
2203 (link_state == LINK_STATE_UP) ? "LINK_UP" : "LINK_DOWN",
2206 if_printf(ifp, "%d link states coalesced\n", pending);
2207 if (log_link_state_change)
2208 log(LOG_NOTICE, "%s: link state changed to %s\n", ifp->if_xname,
2209 (link_state == LINK_STATE_UP) ? "UP" : "DOWN" );
2210 EVENTHANDLER_INVOKE(ifnet_link_event, ifp, link_state);
2215 * Mark an interface down and notify protocols of
2219 if_down(struct ifnet *ifp)
2222 EVENTHANDLER_INVOKE(ifnet_event, ifp, IFNET_EVENT_DOWN);
2223 if_unroute(ifp, IFF_UP, AF_UNSPEC);
2227 * Mark an interface up and notify protocols of
2231 if_up(struct ifnet *ifp)
2234 if_route(ifp, IFF_UP, AF_UNSPEC);
2235 EVENTHANDLER_INVOKE(ifnet_event, ifp, IFNET_EVENT_UP);
2239 * Flush an interface queue.
2242 if_qflush(struct ifnet *ifp)
2250 if (ALTQ_IS_ENABLED(ifq))
2254 while ((m = n) != NULL) {
2265 * Map interface name to interface structure pointer, with or without
2266 * returning a reference.
2269 ifunit_ref(const char *name)
2273 IFNET_RLOCK_NOSLEEP();
2274 TAILQ_FOREACH(ifp, &V_ifnet, if_link) {
2275 if (strncmp(name, ifp->if_xname, IFNAMSIZ) == 0 &&
2276 !(ifp->if_flags & IFF_DYING))
2281 IFNET_RUNLOCK_NOSLEEP();
2286 ifunit(const char *name)
2290 IFNET_RLOCK_NOSLEEP();
2291 TAILQ_FOREACH(ifp, &V_ifnet, if_link) {
2292 if (strncmp(name, ifp->if_xname, IFNAMSIZ) == 0)
2295 IFNET_RUNLOCK_NOSLEEP();
2300 * Hardware specific interface ioctls.
2303 ifhwioctl(u_long cmd, struct ifnet *ifp, caddr_t data, struct thread *td)
2306 int error = 0, do_ifup = 0;
2307 int new_flags, temp_flags;
2308 size_t namelen, onamelen;
2310 char *descrbuf, *odescrbuf;
2311 char new_name[IFNAMSIZ];
2313 struct sockaddr_dl *sdl;
2315 ifr = (struct ifreq *)data;
2318 ifr->ifr_index = ifp->if_index;
2322 temp_flags = ifp->if_flags | ifp->if_drv_flags;
2323 ifr->ifr_flags = temp_flags & 0xffff;
2324 ifr->ifr_flagshigh = temp_flags >> 16;
2328 ifr->ifr_reqcap = ifp->if_capabilities;
2329 ifr->ifr_curcap = ifp->if_capenable;
2334 error = mac_ifnet_ioctl_get(td->td_ucred, ifr, ifp);
2339 ifr->ifr_metric = ifp->if_metric;
2343 ifr->ifr_mtu = ifp->if_mtu;
2347 /* XXXGL: did this ever worked? */
2353 sx_slock(&ifdescr_sx);
2354 if (ifp->if_description == NULL)
2357 /* space for terminating nul */
2358 descrlen = strlen(ifp->if_description) + 1;
2359 if (ifr->ifr_buffer.length < descrlen)
2360 ifr->ifr_buffer.buffer = NULL;
2362 error = copyout(ifp->if_description,
2363 ifr->ifr_buffer.buffer, descrlen);
2364 ifr->ifr_buffer.length = descrlen;
2366 sx_sunlock(&ifdescr_sx);
2370 error = priv_check(td, PRIV_NET_SETIFDESCR);
2375 * Copy only (length-1) bytes to make sure that
2376 * if_description is always nul terminated. The
2377 * length parameter is supposed to count the
2378 * terminating nul in.
2380 if (ifr->ifr_buffer.length > ifdescr_maxlen)
2381 return (ENAMETOOLONG);
2382 else if (ifr->ifr_buffer.length == 0)
2385 descrbuf = malloc(ifr->ifr_buffer.length, M_IFDESCR,
2387 error = copyin(ifr->ifr_buffer.buffer, descrbuf,
2388 ifr->ifr_buffer.length - 1);
2390 free(descrbuf, M_IFDESCR);
2395 sx_xlock(&ifdescr_sx);
2396 odescrbuf = ifp->if_description;
2397 ifp->if_description = descrbuf;
2398 sx_xunlock(&ifdescr_sx);
2400 getmicrotime(&ifp->if_lastchange);
2401 free(odescrbuf, M_IFDESCR);
2405 ifr->ifr_fib = ifp->if_fib;
2409 error = priv_check(td, PRIV_NET_SETIFFIB);
2412 if (ifr->ifr_fib >= rt_numfibs)
2415 ifp->if_fib = ifr->ifr_fib;
2419 error = priv_check(td, PRIV_NET_SETIFFLAGS);
2423 * Currently, no driver owned flags pass the IFF_CANTCHANGE
2424 * check, so we don't need special handling here yet.
2426 new_flags = (ifr->ifr_flags & 0xffff) |
2427 (ifr->ifr_flagshigh << 16);
2428 if (ifp->if_flags & IFF_UP &&
2429 (new_flags & IFF_UP) == 0) {
2431 } else if (new_flags & IFF_UP &&
2432 (ifp->if_flags & IFF_UP) == 0) {
2435 /* See if permanently promiscuous mode bit is about to flip */
2436 if ((ifp->if_flags ^ new_flags) & IFF_PPROMISC) {
2437 if (new_flags & IFF_PPROMISC)
2438 ifp->if_flags |= IFF_PROMISC;
2439 else if (ifp->if_pcount == 0)
2440 ifp->if_flags &= ~IFF_PROMISC;
2441 if (log_promisc_mode_change)
2442 log(LOG_INFO, "%s: permanently promiscuous mode %s\n",
2444 ((new_flags & IFF_PPROMISC) ?
2445 "enabled" : "disabled"));
2447 ifp->if_flags = (ifp->if_flags & IFF_CANTCHANGE) |
2448 (new_flags &~ IFF_CANTCHANGE);
2449 if (ifp->if_ioctl) {
2450 (void) (*ifp->if_ioctl)(ifp, cmd, data);
2454 getmicrotime(&ifp->if_lastchange);
2458 error = priv_check(td, PRIV_NET_SETIFCAP);
2461 if (ifp->if_ioctl == NULL)
2462 return (EOPNOTSUPP);
2463 if (ifr->ifr_reqcap & ~ifp->if_capabilities)
2465 error = (*ifp->if_ioctl)(ifp, cmd, data);
2467 getmicrotime(&ifp->if_lastchange);
2472 error = mac_ifnet_ioctl_set(td->td_ucred, ifr, ifp);
2477 error = priv_check(td, PRIV_NET_SETIFNAME);
2480 error = copyinstr(ifr->ifr_data, new_name, IFNAMSIZ, NULL);
2483 if (new_name[0] == '\0')
2485 if (new_name[IFNAMSIZ-1] != '\0') {
2486 new_name[IFNAMSIZ-1] = '\0';
2487 if (strlen(new_name) == IFNAMSIZ-1)
2490 if (ifunit(new_name) != NULL)
2494 * XXX: Locking. Nothing else seems to lock if_flags,
2495 * and there are numerous other races with the
2496 * ifunit() checks not being atomic with namespace
2497 * changes (renames, vmoves, if_attach, etc).
2499 ifp->if_flags |= IFF_RENAMING;
2501 /* Announce the departure of the interface. */
2502 rt_ifannouncemsg(ifp, IFAN_DEPARTURE);
2503 EVENTHANDLER_INVOKE(ifnet_departure_event, ifp);
2505 log(LOG_INFO, "%s: changing name to '%s'\n",
2506 ifp->if_xname, new_name);
2509 strlcpy(ifp->if_xname, new_name, sizeof(ifp->if_xname));
2511 sdl = (struct sockaddr_dl *)ifa->ifa_addr;
2512 namelen = strlen(new_name);
2513 onamelen = sdl->sdl_nlen;
2515 * Move the address if needed. This is safe because we
2516 * allocate space for a name of length IFNAMSIZ when we
2517 * create this in if_attach().
2519 if (namelen != onamelen) {
2520 bcopy(sdl->sdl_data + onamelen,
2521 sdl->sdl_data + namelen, sdl->sdl_alen);
2523 bcopy(new_name, sdl->sdl_data, namelen);
2524 sdl->sdl_nlen = namelen;
2525 sdl = (struct sockaddr_dl *)ifa->ifa_netmask;
2526 bzero(sdl->sdl_data, onamelen);
2527 while (namelen != 0)
2528 sdl->sdl_data[--namelen] = 0xff;
2529 IF_ADDR_WUNLOCK(ifp);
2531 EVENTHANDLER_INVOKE(ifnet_arrival_event, ifp);
2532 /* Announce the return of the interface. */
2533 rt_ifannouncemsg(ifp, IFAN_ARRIVAL);
2535 ifp->if_flags &= ~IFF_RENAMING;
2540 error = priv_check(td, PRIV_NET_SETIFVNET);
2543 error = if_vmove_loan(td, ifp, ifr->ifr_name, ifr->ifr_jid);
2548 error = priv_check(td, PRIV_NET_SETIFMETRIC);
2551 ifp->if_metric = ifr->ifr_metric;
2552 getmicrotime(&ifp->if_lastchange);
2556 error = priv_check(td, PRIV_NET_SETIFPHYS);
2559 if (ifp->if_ioctl == NULL)
2560 return (EOPNOTSUPP);
2561 error = (*ifp->if_ioctl)(ifp, cmd, data);
2563 getmicrotime(&ifp->if_lastchange);
2568 u_long oldmtu = ifp->if_mtu;
2570 error = priv_check(td, PRIV_NET_SETIFMTU);
2573 if (ifr->ifr_mtu < IF_MINMTU || ifr->ifr_mtu > IF_MAXMTU)
2575 if (ifp->if_ioctl == NULL)
2576 return (EOPNOTSUPP);
2577 error = (*ifp->if_ioctl)(ifp, cmd, data);
2579 getmicrotime(&ifp->if_lastchange);
2583 * If the link MTU changed, do network layer specific procedure.
2585 if (ifp->if_mtu != oldmtu) {
2596 if (cmd == SIOCADDMULTI)
2597 error = priv_check(td, PRIV_NET_ADDMULTI);
2599 error = priv_check(td, PRIV_NET_DELMULTI);
2603 /* Don't allow group membership on non-multicast interfaces. */
2604 if ((ifp->if_flags & IFF_MULTICAST) == 0)
2605 return (EOPNOTSUPP);
2607 /* Don't let users screw up protocols' entries. */
2608 if (ifr->ifr_addr.sa_family != AF_LINK)
2611 if (cmd == SIOCADDMULTI) {
2612 struct ifmultiaddr *ifma;
2615 * Userland is only permitted to join groups once
2616 * via the if_addmulti() KPI, because it cannot hold
2617 * struct ifmultiaddr * between calls. It may also
2618 * lose a race while we check if the membership
2622 ifma = if_findmulti(ifp, &ifr->ifr_addr);
2623 IF_ADDR_RUNLOCK(ifp);
2627 error = if_addmulti(ifp, &ifr->ifr_addr, &ifma);
2629 error = if_delmulti(ifp, &ifr->ifr_addr);
2632 getmicrotime(&ifp->if_lastchange);
2635 case SIOCSIFPHYADDR:
2636 case SIOCDIFPHYADDR:
2638 case SIOCSIFPHYADDR_IN6:
2641 case SIOCSIFGENERIC:
2642 error = priv_check(td, PRIV_NET_HWIOCTL);
2645 if (ifp->if_ioctl == NULL)
2646 return (EOPNOTSUPP);
2647 error = (*ifp->if_ioctl)(ifp, cmd, data);
2649 getmicrotime(&ifp->if_lastchange);
2653 case SIOCGIFPSRCADDR:
2654 case SIOCGIFPDSTADDR:
2657 case SIOCGIFGENERIC:
2658 if (ifp->if_ioctl == NULL)
2659 return (EOPNOTSUPP);
2660 error = (*ifp->if_ioctl)(ifp, cmd, data);
2664 error = priv_check(td, PRIV_NET_SETLLADDR);
2667 error = if_setlladdr(ifp,
2668 ifr->ifr_addr.sa_data, ifr->ifr_addr.sa_len);
2673 struct ifgroupreq *ifgr = (struct ifgroupreq *)ifr;
2675 error = priv_check(td, PRIV_NET_ADDIFGROUP);
2678 if ((error = if_addgroup(ifp, ifgr->ifgr_group)))
2684 if ((error = if_getgroup((struct ifgroupreq *)ifr, ifp)))
2690 struct ifgroupreq *ifgr = (struct ifgroupreq *)ifr;
2692 error = priv_check(td, PRIV_NET_DELIFGROUP);
2695 if ((error = if_delgroup(ifp, ifgr->ifgr_group)))
2708 #define OSIOCGIFCONF _IOWR('i', 20, struct ifconf)
2710 #ifdef COMPAT_FREEBSD32
2718 #define SIOCGIFCONF32 _IOWR('i', 36, struct ifconf32)
2725 ifioctl(struct socket *so, u_long cmd, caddr_t data, struct thread *td)
2735 CURVNET_SET(so->so_vnet);
2737 /* Make sure the VNET is stable. */
2738 shutdown = (so->so_vnet->vnet_state > SI_SUB_VNET &&
2739 so->so_vnet->vnet_state < SI_SUB_VNET_DONE) ? 1 : 0;
2749 case OSIOCGIFCONF: /* COMPAT_SVR4 */
2750 error = ifconf(cmd, data);
2754 #ifdef COMPAT_FREEBSD32
2757 struct ifconf32 *ifc32;
2760 ifc32 = (struct ifconf32 *)data;
2761 ifc.ifc_len = ifc32->ifc_len;
2762 ifc.ifc_buf = PTRIN(ifc32->ifc_buf);
2764 error = ifconf(SIOCGIFCONF, (void *)&ifc);
2767 ifc32->ifc_len = ifc.ifc_len;
2772 ifr = (struct ifreq *)data;
2777 error = priv_check(td, PRIV_NET_SETIFVNET);
2779 error = if_vmove_reclaim(td, ifr->ifr_name,
2786 error = priv_check(td, PRIV_NET_IFCREATE);
2788 error = if_clone_create(ifr->ifr_name,
2789 sizeof(ifr->ifr_name),
2790 cmd == SIOCIFCREATE2 ? ifr->ifr_data : NULL);
2794 error = priv_check(td, PRIV_NET_IFDESTROY);
2796 error = if_clone_destroy(ifr->ifr_name);
2800 case SIOCIFGCLONERS:
2801 error = if_clone_list((struct if_clonereq *)data);
2805 error = if_getgroupmembers((struct ifgroupreq *)data);
2808 #if defined(INET) || defined(INET6)
2811 if (carp_ioctl_p == NULL)
2812 error = EPROTONOSUPPORT;
2814 error = (*carp_ioctl_p)(ifr, cmd, td);
2820 ifp = ifunit_ref(ifr->ifr_name);
2826 error = ifhwioctl(cmd, ifp, data, td);
2827 if (error != ENOIOCTL) {
2833 oif_flags = ifp->if_flags;
2834 if (so->so_proto == NULL) {
2837 return (EOPNOTSUPP);
2841 * Pass the request on to the socket control method, and if the
2842 * latter returns EOPNOTSUPP, directly to the interface.
2844 * Make an exception for the legacy SIOCSIF* requests. Drivers
2845 * trust SIOCSIFADDR et al to come from an already privileged
2846 * layer, and do not perform any credentials checks or input
2849 error = ((*so->so_proto->pr_usrreqs->pru_control)(so, cmd, data,
2851 if (error == EOPNOTSUPP && ifp != NULL && ifp->if_ioctl != NULL &&
2852 cmd != SIOCSIFADDR && cmd != SIOCSIFBRDADDR &&
2853 cmd != SIOCSIFDSTADDR && cmd != SIOCSIFNETMASK)
2854 error = (*ifp->if_ioctl)(ifp, cmd, data);
2856 if ((oif_flags ^ ifp->if_flags) & IFF_UP) {
2858 if (ifp->if_flags & IFF_UP)
2868 * The code common to handling reference counted flags,
2869 * e.g., in ifpromisc() and if_allmulti().
2870 * The "pflag" argument can specify a permanent mode flag to check,
2871 * such as IFF_PPROMISC for promiscuous mode; should be 0 if none.
2873 * Only to be used on stack-owned flags, not driver-owned flags.
2876 if_setflag(struct ifnet *ifp, int flag, int pflag, int *refcount, int onswitch)
2880 int oldflags, oldcount;
2882 /* Sanity checks to catch programming errors */
2883 KASSERT((flag & (IFF_DRV_OACTIVE|IFF_DRV_RUNNING)) == 0,
2884 ("%s: setting driver-owned flag %d", __func__, flag));
2887 KASSERT(*refcount >= 0,
2888 ("%s: increment negative refcount %d for flag %d",
2889 __func__, *refcount, flag));
2891 KASSERT(*refcount > 0,
2892 ("%s: decrement non-positive refcount %d for flag %d",
2893 __func__, *refcount, flag));
2895 /* In case this mode is permanent, just touch refcount */
2896 if (ifp->if_flags & pflag) {
2897 *refcount += onswitch ? 1 : -1;
2901 /* Save ifnet parameters for if_ioctl() may fail */
2902 oldcount = *refcount;
2903 oldflags = ifp->if_flags;
2906 * See if we aren't the only and touching refcount is enough.
2907 * Actually toggle interface flag if we are the first or last.
2912 ifp->if_flags |= flag;
2916 ifp->if_flags &= ~flag;
2919 /* Call down the driver since we've changed interface flags */
2920 if (ifp->if_ioctl == NULL) {
2924 ifr.ifr_flags = ifp->if_flags & 0xffff;
2925 ifr.ifr_flagshigh = ifp->if_flags >> 16;
2926 error = (*ifp->if_ioctl)(ifp, SIOCSIFFLAGS, (caddr_t)&ifr);
2929 /* Notify userland that interface flags have changed */
2934 /* Recover after driver error */
2935 *refcount = oldcount;
2936 ifp->if_flags = oldflags;
2941 * Set/clear promiscuous mode on interface ifp based on the truth value
2942 * of pswitch. The calls are reference counted so that only the first
2943 * "on" request actually has an effect, as does the final "off" request.
2944 * Results are undefined if the "off" and "on" requests are not matched.
2947 ifpromisc(struct ifnet *ifp, int pswitch)
2950 int oldflags = ifp->if_flags;
2952 error = if_setflag(ifp, IFF_PROMISC, IFF_PPROMISC,
2953 &ifp->if_pcount, pswitch);
2954 /* If promiscuous mode status has changed, log a message */
2955 if (error == 0 && ((ifp->if_flags ^ oldflags) & IFF_PROMISC) &&
2956 log_promisc_mode_change)
2957 log(LOG_INFO, "%s: promiscuous mode %s\n",
2959 (ifp->if_flags & IFF_PROMISC) ? "enabled" : "disabled");
2964 * Return interface configuration
2965 * of system. List may be used
2966 * in later ioctl's (above) to get
2967 * other information.
2971 ifconf(u_long cmd, caddr_t data)
2973 struct ifconf *ifc = (struct ifconf *)data;
2978 int error, full = 0, valid_len, max_len;
2980 /* Limit initial buffer size to MAXPHYS to avoid DoS from userspace. */
2981 max_len = MAXPHYS - 1;
2983 /* Prevent hostile input from being able to crash the system */
2984 if (ifc->ifc_len <= 0)
2988 if (ifc->ifc_len <= max_len) {
2989 max_len = ifc->ifc_len;
2992 sb = sbuf_new(NULL, NULL, max_len + 1, SBUF_FIXEDLEN);
2997 TAILQ_FOREACH(ifp, &V_ifnet, if_link) {
3001 * Zero the ifr_name buffer to make sure we don't
3002 * disclose the contents of the stack.
3004 memset(ifr.ifr_name, 0, sizeof(ifr.ifr_name));
3006 if (strlcpy(ifr.ifr_name, ifp->if_xname, sizeof(ifr.ifr_name))
3007 >= sizeof(ifr.ifr_name)) {
3010 return (ENAMETOOLONG);
3015 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
3016 struct sockaddr *sa = ifa->ifa_addr;
3018 if (prison_if(curthread->td_ucred, sa) != 0)
3022 if (cmd == OSIOCGIFCONF) {
3023 struct osockaddr *osa =
3024 (struct osockaddr *)&ifr.ifr_addr;
3026 osa->sa_family = sa->sa_family;
3027 sbuf_bcat(sb, &ifr, sizeof(ifr));
3028 max_len += sizeof(ifr);
3030 if (sa->sa_len <= sizeof(*sa)) {
3032 sbuf_bcat(sb, &ifr, sizeof(ifr));
3033 max_len += sizeof(ifr);
3036 offsetof(struct ifreq, ifr_addr));
3037 max_len += offsetof(struct ifreq, ifr_addr);
3038 sbuf_bcat(sb, sa, sa->sa_len);
3039 max_len += sa->sa_len;
3042 if (sbuf_error(sb) == 0)
3043 valid_len = sbuf_len(sb);
3045 IF_ADDR_RUNLOCK(ifp);
3047 bzero((caddr_t)&ifr.ifr_addr, sizeof(ifr.ifr_addr));
3048 sbuf_bcat(sb, &ifr, sizeof(ifr));
3049 max_len += sizeof(ifr);
3051 if (sbuf_error(sb) == 0)
3052 valid_len = sbuf_len(sb);
3058 * If we didn't allocate enough space (uncommon), try again. If
3059 * we have already allocated as much space as we are allowed,
3060 * return what we've got.
3062 if (valid_len != max_len && !full) {
3067 ifc->ifc_len = valid_len;
3069 error = copyout(sbuf_data(sb), ifc->ifc_req, ifc->ifc_len);
3075 * Just like ifpromisc(), but for all-multicast-reception mode.
3078 if_allmulti(struct ifnet *ifp, int onswitch)
3081 return (if_setflag(ifp, IFF_ALLMULTI, 0, &ifp->if_amcount, onswitch));
3084 struct ifmultiaddr *
3085 if_findmulti(struct ifnet *ifp, const struct sockaddr *sa)
3087 struct ifmultiaddr *ifma;
3089 IF_ADDR_LOCK_ASSERT(ifp);
3091 TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
3092 if (sa->sa_family == AF_LINK) {
3093 if (sa_dl_equal(ifma->ifma_addr, sa))
3096 if (sa_equal(ifma->ifma_addr, sa))
3105 * Allocate a new ifmultiaddr and initialize based on passed arguments. We
3106 * make copies of passed sockaddrs. The ifmultiaddr will not be added to
3107 * the ifnet multicast address list here, so the caller must do that and
3108 * other setup work (such as notifying the device driver). The reference
3109 * count is initialized to 1.
3111 static struct ifmultiaddr *
3112 if_allocmulti(struct ifnet *ifp, struct sockaddr *sa, struct sockaddr *llsa,
3115 struct ifmultiaddr *ifma;
3116 struct sockaddr *dupsa;
3118 ifma = malloc(sizeof *ifma, M_IFMADDR, mflags |
3123 dupsa = malloc(sa->sa_len, M_IFMADDR, mflags);
3124 if (dupsa == NULL) {
3125 free(ifma, M_IFMADDR);
3128 bcopy(sa, dupsa, sa->sa_len);
3129 ifma->ifma_addr = dupsa;
3131 ifma->ifma_ifp = ifp;
3132 ifma->ifma_refcount = 1;
3133 ifma->ifma_protospec = NULL;
3136 ifma->ifma_lladdr = NULL;
3140 dupsa = malloc(llsa->sa_len, M_IFMADDR, mflags);
3141 if (dupsa == NULL) {
3142 free(ifma->ifma_addr, M_IFMADDR);
3143 free(ifma, M_IFMADDR);
3146 bcopy(llsa, dupsa, llsa->sa_len);
3147 ifma->ifma_lladdr = dupsa;
3153 * if_freemulti: free ifmultiaddr structure and possibly attached related
3154 * addresses. The caller is responsible for implementing reference
3155 * counting, notifying the driver, handling routing messages, and releasing
3156 * any dependent link layer state.
3159 if_freemulti(struct ifmultiaddr *ifma)
3162 KASSERT(ifma->ifma_refcount == 0, ("if_freemulti: refcount %d",
3163 ifma->ifma_refcount));
3165 if (ifma->ifma_lladdr != NULL)
3166 free(ifma->ifma_lladdr, M_IFMADDR);
3167 free(ifma->ifma_addr, M_IFMADDR);
3168 free(ifma, M_IFMADDR);
3172 * Register an additional multicast address with a network interface.
3174 * - If the address is already present, bump the reference count on the
3175 * address and return.
3176 * - If the address is not link-layer, look up a link layer address.
3177 * - Allocate address structures for one or both addresses, and attach to the
3178 * multicast address list on the interface. If automatically adding a link
3179 * layer address, the protocol address will own a reference to the link
3180 * layer address, to be freed when it is freed.
3181 * - Notify the network device driver of an addition to the multicast address
3184 * 'sa' points to caller-owned memory with the desired multicast address.
3186 * 'retifma' will be used to return a pointer to the resulting multicast
3187 * address reference, if desired.
3190 if_addmulti(struct ifnet *ifp, struct sockaddr *sa,
3191 struct ifmultiaddr **retifma)
3193 struct ifmultiaddr *ifma, *ll_ifma;
3194 struct sockaddr *llsa;
3195 struct sockaddr_dl sdl;
3199 * If the address is already present, return a new reference to it;
3200 * otherwise, allocate storage and set up a new address.
3203 ifma = if_findmulti(ifp, sa);
3205 ifma->ifma_refcount++;
3206 if (retifma != NULL)
3208 IF_ADDR_WUNLOCK(ifp);
3213 * The address isn't already present; resolve the protocol address
3214 * into a link layer address, and then look that up, bump its
3215 * refcount or allocate an ifma for that also.
3216 * Most link layer resolving functions returns address data which
3217 * fits inside default sockaddr_dl structure. However callback
3218 * can allocate another sockaddr structure, in that case we need to
3223 if (ifp->if_resolvemulti != NULL) {
3224 /* Provide called function with buffer size information */
3225 sdl.sdl_len = sizeof(sdl);
3226 llsa = (struct sockaddr *)&sdl;
3227 error = ifp->if_resolvemulti(ifp, &llsa, sa);
3233 * Allocate the new address. Don't hook it up yet, as we may also
3234 * need to allocate a link layer multicast address.
3236 ifma = if_allocmulti(ifp, sa, llsa, M_NOWAIT);
3243 * If a link layer address is found, we'll need to see if it's
3244 * already present in the address list, or allocate is as well.
3245 * When this block finishes, the link layer address will be on the
3249 ll_ifma = if_findmulti(ifp, llsa);
3250 if (ll_ifma == NULL) {
3251 ll_ifma = if_allocmulti(ifp, llsa, NULL, M_NOWAIT);
3252 if (ll_ifma == NULL) {
3253 --ifma->ifma_refcount;
3258 TAILQ_INSERT_HEAD(&ifp->if_multiaddrs, ll_ifma,
3261 ll_ifma->ifma_refcount++;
3262 ifma->ifma_llifma = ll_ifma;
3266 * We now have a new multicast address, ifma, and possibly a new or
3267 * referenced link layer address. Add the primary address to the
3268 * ifnet address list.
3270 TAILQ_INSERT_HEAD(&ifp->if_multiaddrs, ifma, ifma_link);
3272 if (retifma != NULL)
3276 * Must generate the message while holding the lock so that 'ifma'
3277 * pointer is still valid.
3279 rt_newmaddrmsg(RTM_NEWMADDR, ifma);
3280 IF_ADDR_WUNLOCK(ifp);
3283 * We are certain we have added something, so call down to the
3284 * interface to let them know about it.
3286 if (ifp->if_ioctl != NULL) {
3287 (void) (*ifp->if_ioctl)(ifp, SIOCADDMULTI, 0);
3290 if ((llsa != NULL) && (llsa != (struct sockaddr *)&sdl))
3291 link_free_sdl(llsa);
3296 if ((llsa != NULL) && (llsa != (struct sockaddr *)&sdl))
3297 link_free_sdl(llsa);
3300 IF_ADDR_WUNLOCK(ifp);
3305 * Delete a multicast group membership by network-layer group address.
3307 * Returns ENOENT if the entry could not be found. If ifp no longer
3308 * exists, results are undefined. This entry point should only be used
3309 * from subsystems which do appropriate locking to hold ifp for the
3310 * duration of the call.
3311 * Network-layer protocol domains must use if_delmulti_ifma().
3314 if_delmulti(struct ifnet *ifp, struct sockaddr *sa)
3316 struct ifmultiaddr *ifma;
3321 IFNET_RLOCK_NOSLEEP();
3322 TAILQ_FOREACH(oifp, &V_ifnet, if_link)
3327 IFNET_RUNLOCK_NOSLEEP();
3329 KASSERT(ifp != NULL, ("%s: ifnet went away", __func__));
3336 ifma = if_findmulti(ifp, sa);
3338 lastref = if_delmulti_locked(ifp, ifma, 0);
3339 IF_ADDR_WUNLOCK(ifp);
3344 if (lastref && ifp->if_ioctl != NULL) {
3345 (void)(*ifp->if_ioctl)(ifp, SIOCDELMULTI, 0);
3352 * Delete all multicast group membership for an interface.
3353 * Should be used to quickly flush all multicast filters.
3356 if_delallmulti(struct ifnet *ifp)
3358 struct ifmultiaddr *ifma;
3359 struct ifmultiaddr *next;
3362 TAILQ_FOREACH_SAFE(ifma, &ifp->if_multiaddrs, ifma_link, next)
3363 if_delmulti_locked(ifp, ifma, 0);
3364 IF_ADDR_WUNLOCK(ifp);
3368 * Delete a multicast group membership by group membership pointer.
3369 * Network-layer protocol domains must use this routine.
3371 * It is safe to call this routine if the ifp disappeared.
3374 if_delmulti_ifma(struct ifmultiaddr *ifma)
3379 ifp = ifma->ifma_ifp;
3382 printf("%s: ifma_ifp seems to be detached\n", __func__);
3386 IFNET_RLOCK_NOSLEEP();
3387 TAILQ_FOREACH(oifp, &V_ifnet, if_link)
3391 printf("%s: ifnet %p disappeared\n", __func__, ifp);
3394 IFNET_RUNLOCK_NOSLEEP();
3398 * If and only if the ifnet instance exists: Acquire the address lock.
3403 lastref = if_delmulti_locked(ifp, ifma, 0);
3407 * If and only if the ifnet instance exists:
3408 * Release the address lock.
3409 * If the group was left: update the hardware hash filter.
3411 IF_ADDR_WUNLOCK(ifp);
3412 if (lastref && ifp->if_ioctl != NULL) {
3413 (void)(*ifp->if_ioctl)(ifp, SIOCDELMULTI, 0);
3419 * Perform deletion of network-layer and/or link-layer multicast address.
3421 * Return 0 if the reference count was decremented.
3422 * Return 1 if the final reference was released, indicating that the
3423 * hardware hash filter should be reprogrammed.
3426 if_delmulti_locked(struct ifnet *ifp, struct ifmultiaddr *ifma, int detaching)
3428 struct ifmultiaddr *ll_ifma;
3430 if (ifp != NULL && ifma->ifma_ifp != NULL) {
3431 KASSERT(ifma->ifma_ifp == ifp,
3432 ("%s: inconsistent ifp %p", __func__, ifp));
3433 IF_ADDR_WLOCK_ASSERT(ifp);
3436 ifp = ifma->ifma_ifp;
3439 * If the ifnet is detaching, null out references to ifnet,
3440 * so that upper protocol layers will notice, and not attempt
3441 * to obtain locks for an ifnet which no longer exists. The
3442 * routing socket announcement must happen before the ifnet
3443 * instance is detached from the system.
3447 printf("%s: detaching ifnet instance %p\n", __func__, ifp);
3450 * ifp may already be nulled out if we are being reentered
3451 * to delete the ll_ifma.
3454 rt_newmaddrmsg(RTM_DELMADDR, ifma);
3455 ifma->ifma_ifp = NULL;
3459 if (--ifma->ifma_refcount > 0)
3463 * If this ifma is a network-layer ifma, a link-layer ifma may
3464 * have been associated with it. Release it first if so.
3466 ll_ifma = ifma->ifma_llifma;
3467 if (ll_ifma != NULL) {
3468 KASSERT(ifma->ifma_lladdr != NULL,
3469 ("%s: llifma w/o lladdr", __func__));
3471 ll_ifma->ifma_ifp = NULL; /* XXX */
3472 if (--ll_ifma->ifma_refcount == 0) {
3474 TAILQ_REMOVE(&ifp->if_multiaddrs, ll_ifma,
3477 if_freemulti(ll_ifma);
3482 TAILQ_REMOVE(&ifp->if_multiaddrs, ifma, ifma_link);
3487 * The last reference to this instance of struct ifmultiaddr
3488 * was released; the hardware should be notified of this change.
3494 * Set the link layer address on an interface.
3496 * At this time we only support certain types of interfaces,
3497 * and we don't allow the length of the address to change.
3499 * Set noinline to be dtrace-friendly
3502 if_setlladdr(struct ifnet *ifp, const u_char *lladdr, int len)
3504 struct sockaddr_dl *sdl;
3511 IF_ADDR_RUNLOCK(ifp);
3515 IF_ADDR_RUNLOCK(ifp);
3516 sdl = (struct sockaddr_dl *)ifa->ifa_addr;
3521 if (len != sdl->sdl_alen) { /* don't allow length to change */
3525 switch (ifp->if_type) {
3533 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 * The name argument must be a pointer to storage which will last as
3603 * long as the interface does. For physical devices, the result of
3604 * device_get_name(dev) is a good choice and for pseudo-devices a
3605 * static string works well.
3608 if_initname(struct ifnet *ifp, const char *name, int unit)
3610 ifp->if_dname = name;
3611 ifp->if_dunit = unit;
3612 if (unit != IF_DUNIT_NONE)
3613 snprintf(ifp->if_xname, IFNAMSIZ, "%s%d", name, unit);
3615 strlcpy(ifp->if_xname, name, IFNAMSIZ);
3619 if_printf(struct ifnet *ifp, const char * fmt, ...)
3624 retval = printf("%s: ", ifp->if_xname);
3626 retval += vprintf(fmt, ap);
3632 if_start(struct ifnet *ifp)
3635 (*(ifp)->if_start)(ifp);
3639 * Backwards compatibility interface for drivers
3640 * that have not implemented it
3643 if_transmit(struct ifnet *ifp, struct mbuf *m)
3647 IFQ_HANDOFF(ifp, m, error);
3652 if_input_default(struct ifnet *ifp __unused, struct mbuf *m)
3659 if_handoff(struct ifqueue *ifq, struct mbuf *m, struct ifnet *ifp, int adjust)
3664 if (_IF_QFULL(ifq)) {
3666 if_inc_counter(ifp, IFCOUNTER_OQDROPS, 1);
3671 if_inc_counter(ifp, IFCOUNTER_OBYTES, m->m_pkthdr.len + adjust);
3672 if (m->m_flags & (M_BCAST|M_MCAST))
3673 if_inc_counter(ifp, IFCOUNTER_OMCASTS, 1);
3674 active = ifp->if_drv_flags & IFF_DRV_OACTIVE;
3676 _IF_ENQUEUE(ifq, m);
3678 if (ifp != NULL && !active)
3679 (*(ifp)->if_start)(ifp);
3684 if_register_com_alloc(u_char type,
3685 if_com_alloc_t *a, if_com_free_t *f)
3688 KASSERT(if_com_alloc[type] == NULL,
3689 ("if_register_com_alloc: %d already registered", type));
3690 KASSERT(if_com_free[type] == NULL,
3691 ("if_register_com_alloc: %d free already registered", type));
3693 if_com_alloc[type] = a;
3694 if_com_free[type] = f;
3698 if_deregister_com_alloc(u_char type)
3701 KASSERT(if_com_alloc[type] != NULL,
3702 ("if_deregister_com_alloc: %d not registered", type));
3703 KASSERT(if_com_free[type] != NULL,
3704 ("if_deregister_com_alloc: %d free not registered", type));
3705 if_com_alloc[type] = NULL;
3706 if_com_free[type] = NULL;
3709 /* API for driver access to network stack owned ifnet.*/
3711 if_setbaudrate(struct ifnet *ifp, uint64_t baudrate)
3715 oldbrate = ifp->if_baudrate;
3716 ifp->if_baudrate = baudrate;
3721 if_getbaudrate(if_t ifp)
3724 return (((struct ifnet *)ifp)->if_baudrate);
3728 if_setcapabilities(if_t ifp, int capabilities)
3730 ((struct ifnet *)ifp)->if_capabilities = capabilities;
3735 if_setcapabilitiesbit(if_t ifp, int setbit, int clearbit)
3737 ((struct ifnet *)ifp)->if_capabilities |= setbit;
3738 ((struct ifnet *)ifp)->if_capabilities &= ~clearbit;
3744 if_getcapabilities(if_t ifp)
3746 return ((struct ifnet *)ifp)->if_capabilities;
3750 if_setcapenable(if_t ifp, int capabilities)
3752 ((struct ifnet *)ifp)->if_capenable = capabilities;
3757 if_setcapenablebit(if_t ifp, int setcap, int clearcap)
3760 ((struct ifnet *)ifp)->if_capenable |= setcap;
3762 ((struct ifnet *)ifp)->if_capenable &= ~clearcap;
3768 if_getdname(if_t ifp)
3770 return ((struct ifnet *)ifp)->if_dname;
3774 if_togglecapenable(if_t ifp, int togglecap)
3776 ((struct ifnet *)ifp)->if_capenable ^= togglecap;
3781 if_getcapenable(if_t ifp)
3783 return ((struct ifnet *)ifp)->if_capenable;
3787 * This is largely undesirable because it ties ifnet to a device, but does
3788 * provide flexiblity for an embedded product vendor. Should be used with
3789 * the understanding that it violates the interface boundaries, and should be
3790 * a last resort only.
3793 if_setdev(if_t ifp, void *dev)
3799 if_setdrvflagbits(if_t ifp, int set_flags, int clear_flags)
3801 ((struct ifnet *)ifp)->if_drv_flags |= set_flags;
3802 ((struct ifnet *)ifp)->if_drv_flags &= ~clear_flags;
3808 if_getdrvflags(if_t ifp)
3810 return ((struct ifnet *)ifp)->if_drv_flags;
3814 if_setdrvflags(if_t ifp, int flags)
3816 ((struct ifnet *)ifp)->if_drv_flags = flags;
3822 if_setflags(if_t ifp, int flags)
3824 ((struct ifnet *)ifp)->if_flags = flags;
3829 if_setflagbits(if_t ifp, int set, int clear)
3831 ((struct ifnet *)ifp)->if_flags |= set;
3832 ((struct ifnet *)ifp)->if_flags &= ~clear;
3838 if_getflags(if_t ifp)
3840 return ((struct ifnet *)ifp)->if_flags;
3844 if_clearhwassist(if_t ifp)
3846 ((struct ifnet *)ifp)->if_hwassist = 0;
3851 if_sethwassistbits(if_t ifp, int toset, int toclear)
3853 ((struct ifnet *)ifp)->if_hwassist |= toset;
3854 ((struct ifnet *)ifp)->if_hwassist &= ~toclear;
3860 if_sethwassist(if_t ifp, int hwassist_bit)
3862 ((struct ifnet *)ifp)->if_hwassist = hwassist_bit;
3867 if_gethwassist(if_t ifp)
3869 return ((struct ifnet *)ifp)->if_hwassist;
3873 if_setmtu(if_t ifp, int mtu)
3875 ((struct ifnet *)ifp)->if_mtu = mtu;
3882 return ((struct ifnet *)ifp)->if_mtu;
3886 if_getmtu_family(if_t ifp, int family)
3890 for (dp = domains; dp; dp = dp->dom_next) {
3891 if (dp->dom_family == family && dp->dom_ifmtu != NULL)
3892 return (dp->dom_ifmtu((struct ifnet *)ifp));
3895 return (((struct ifnet *)ifp)->if_mtu);
3899 if_setsoftc(if_t ifp, void *softc)
3901 ((struct ifnet *)ifp)->if_softc = softc;
3906 if_getsoftc(if_t ifp)
3908 return ((struct ifnet *)ifp)->if_softc;
3912 if_setrcvif(struct mbuf *m, if_t ifp)
3914 m->m_pkthdr.rcvif = (struct ifnet *)ifp;
3918 if_setvtag(struct mbuf *m, uint16_t tag)
3920 m->m_pkthdr.ether_vtag = tag;
3924 if_getvtag(struct mbuf *m)
3927 return (m->m_pkthdr.ether_vtag);
3931 if_sendq_empty(if_t ifp)
3933 return IFQ_DRV_IS_EMPTY(&((struct ifnet *)ifp)->if_snd);
3937 if_getifaddr(if_t ifp)
3939 return ((struct ifnet *)ifp)->if_addr;
3943 if_getamcount(if_t ifp)
3945 return ((struct ifnet *)ifp)->if_amcount;
3950 if_setsendqready(if_t ifp)
3952 IFQ_SET_READY(&((struct ifnet *)ifp)->if_snd);
3957 if_setsendqlen(if_t ifp, int tx_desc_count)
3959 IFQ_SET_MAXLEN(&((struct ifnet *)ifp)->if_snd, tx_desc_count);
3960 ((struct ifnet *)ifp)->if_snd.ifq_drv_maxlen = tx_desc_count;
3966 if_vlantrunkinuse(if_t ifp)
3968 return ((struct ifnet *)ifp)->if_vlantrunk != NULL?1:0;
3972 if_input(if_t ifp, struct mbuf* sendmp)
3974 (*((struct ifnet *)ifp)->if_input)((struct ifnet *)ifp, sendmp);
3980 #ifndef ETH_ADDR_LEN
3981 #define ETH_ADDR_LEN 6
3985 if_setupmultiaddr(if_t ifp, void *mta, int *cnt, int max)
3987 struct ifmultiaddr *ifma;
3988 uint8_t *lmta = (uint8_t *)mta;
3991 TAILQ_FOREACH(ifma, &((struct ifnet *)ifp)->if_multiaddrs, ifma_link) {
3992 if (ifma->ifma_addr->sa_family != AF_LINK)
3998 bcopy(LLADDR((struct sockaddr_dl *)ifma->ifma_addr),
3999 &lmta[mcnt * ETH_ADDR_LEN], ETH_ADDR_LEN);
4008 if_multiaddr_array(if_t ifp, void *mta, int *cnt, int max)
4012 if_maddr_rlock(ifp);
4013 error = if_setupmultiaddr(ifp, mta, cnt, max);
4014 if_maddr_runlock(ifp);
4019 if_multiaddr_count(if_t ifp, int max)
4021 struct ifmultiaddr *ifma;
4025 if_maddr_rlock(ifp);
4026 TAILQ_FOREACH(ifma, &((struct ifnet *)ifp)->if_multiaddrs, ifma_link) {
4027 if (ifma->ifma_addr->sa_family != AF_LINK)
4033 if_maddr_runlock(ifp);
4038 if_multi_apply(struct ifnet *ifp, int (*filter)(void *, struct ifmultiaddr *, int), void *arg)
4040 struct ifmultiaddr *ifma;
4043 if_maddr_rlock(ifp);
4044 TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link)
4045 cnt += filter(arg, ifma, cnt);
4046 if_maddr_runlock(ifp);
4051 if_dequeue(if_t ifp)
4054 IFQ_DRV_DEQUEUE(&((struct ifnet *)ifp)->if_snd, m);
4060 if_sendq_prepend(if_t ifp, struct mbuf *m)
4062 IFQ_DRV_PREPEND(&((struct ifnet *)ifp)->if_snd, m);
4067 if_setifheaderlen(if_t ifp, int len)
4069 ((struct ifnet *)ifp)->if_hdrlen = len;
4074 if_getlladdr(if_t ifp)
4076 return (IF_LLADDR((struct ifnet *)ifp));
4080 if_gethandle(u_char type)
4082 return (if_alloc(type));
4086 if_bpfmtap(if_t ifh, struct mbuf *m)
4088 struct ifnet *ifp = (struct ifnet *)ifh;
4094 if_etherbpfmtap(if_t ifh, struct mbuf *m)
4096 struct ifnet *ifp = (struct ifnet *)ifh;
4098 ETHER_BPF_MTAP(ifp, m);
4102 if_vlancap(if_t ifh)
4104 struct ifnet *ifp = (struct ifnet *)ifh;
4105 VLAN_CAPABILITIES(ifp);
4109 if_setinitfn(if_t ifp, void (*init_fn)(void *))
4111 ((struct ifnet *)ifp)->if_init = init_fn;
4115 if_setioctlfn(if_t ifp, int (*ioctl_fn)(if_t, u_long, caddr_t))
4117 ((struct ifnet *)ifp)->if_ioctl = (void *)ioctl_fn;
4121 if_setstartfn(if_t ifp, void (*start_fn)(if_t))
4123 ((struct ifnet *)ifp)->if_start = (void *)start_fn;
4127 if_settransmitfn(if_t ifp, if_transmit_fn_t start_fn)
4129 ((struct ifnet *)ifp)->if_transmit = start_fn;
4132 void if_setqflushfn(if_t ifp, if_qflush_fn_t flush_fn)
4134 ((struct ifnet *)ifp)->if_qflush = flush_fn;
4139 if_setgetcounterfn(if_t ifp, if_get_counter_t fn)
4142 ifp->if_get_counter = fn;
4145 /* Revisit these - These are inline functions originally. */
4147 drbr_inuse_drv(if_t ifh, struct buf_ring *br)
4149 return drbr_inuse(ifh, br);
4153 drbr_dequeue_drv(if_t ifh, struct buf_ring *br)
4155 return drbr_dequeue(ifh, br);
4159 drbr_needs_enqueue_drv(if_t ifh, struct buf_ring *br)
4161 return drbr_needs_enqueue(ifh, br);
4165 drbr_enqueue_drv(if_t ifh, struct buf_ring *br, struct mbuf *m)
4167 return drbr_enqueue(ifh, br, m);