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>
63 #include <machine/stdarg.h>
67 #include <net/ethernet.h>
69 #include <net/if_arp.h>
70 #include <net/if_clone.h>
71 #include <net/if_dl.h>
72 #include <net/if_types.h>
73 #include <net/if_var.h>
74 #include <net/if_media.h>
75 #include <net/if_vlan_var.h>
76 #include <net/radix.h>
77 #include <net/route.h>
80 #if defined(INET) || defined(INET6)
81 #include <net/ethernet.h>
82 #include <netinet/in.h>
83 #include <netinet/in_var.h>
84 #include <netinet/ip.h>
85 #include <netinet/ip_carp.h>
87 #include <netinet/if_ether.h>
90 #include <netinet6/in6_var.h>
91 #include <netinet6/in6_ifattach.h>
93 #endif /* INET || INET6 */
95 #include <security/mac/mac_framework.h>
97 #ifdef COMPAT_FREEBSD32
98 #include <sys/mount.h>
99 #include <compat/freebsd32/freebsd32.h>
102 SYSCTL_NODE(_net, PF_LINK, link, CTLFLAG_RW, 0, "Link layers");
103 SYSCTL_NODE(_net_link, 0, generic, CTLFLAG_RW, 0, "Generic link-management");
105 SYSCTL_INT(_net_link, OID_AUTO, ifqmaxlen, CTLFLAG_RDTUN,
106 &ifqmaxlen, 0, "max send queue size");
108 /* Log link state change events */
109 static int log_link_state_change = 1;
111 SYSCTL_INT(_net_link, OID_AUTO, log_link_state_change, CTLFLAG_RW,
112 &log_link_state_change, 0,
113 "log interface link state change events");
115 /* Log promiscuous mode change events */
116 static int log_promisc_mode_change = 1;
118 SYSCTL_INT(_net_link, OID_AUTO, log_promisc_mode_change, CTLFLAG_RDTUN,
119 &log_promisc_mode_change, 1,
120 "log promiscuous mode change events");
122 /* Interface description */
123 static unsigned int ifdescr_maxlen = 1024;
124 SYSCTL_UINT(_net, OID_AUTO, ifdescr_maxlen, CTLFLAG_RW,
126 "administrative maximum length for interface description");
128 static MALLOC_DEFINE(M_IFDESCR, "ifdescr", "ifnet descriptions");
130 /* global sx for non-critical path ifdescr */
131 static struct sx ifdescr_sx;
132 SX_SYSINIT(ifdescr_sx, &ifdescr_sx, "ifnet descr");
134 void (*bridge_linkstate_p)(struct ifnet *ifp);
135 void (*ng_ether_link_state_p)(struct ifnet *ifp, int state);
136 void (*lagg_linkstate_p)(struct ifnet *ifp, int state);
137 /* These are external hooks for CARP. */
138 void (*carp_linkstate_p)(struct ifnet *ifp);
139 void (*carp_demote_adj_p)(int, char *);
140 int (*carp_master_p)(struct ifaddr *);
141 #if defined(INET) || defined(INET6)
142 int (*carp_forus_p)(struct ifnet *ifp, u_char *dhost);
143 int (*carp_output_p)(struct ifnet *ifp, struct mbuf *m,
144 const struct sockaddr *sa);
145 int (*carp_ioctl_p)(struct ifreq *, u_long, struct thread *);
146 int (*carp_attach_p)(struct ifaddr *, int);
147 void (*carp_detach_p)(struct ifaddr *);
150 int (*carp_iamatch_p)(struct ifaddr *, uint8_t **);
153 struct ifaddr *(*carp_iamatch6_p)(struct ifnet *ifp, struct in6_addr *taddr6);
154 caddr_t (*carp_macmatch6_p)(struct ifnet *ifp, struct mbuf *m,
155 const struct in6_addr *taddr);
158 struct mbuf *(*tbr_dequeue_ptr)(struct ifaltq *, int) = NULL;
161 * XXX: Style; these should be sorted alphabetically, and unprototyped
162 * static functions should be prototyped. Currently they are sorted by
165 static void if_attachdomain(void *);
166 static void if_attachdomain1(struct ifnet *);
167 static int ifconf(u_long, caddr_t);
168 static void if_freemulti(struct ifmultiaddr *);
169 static void if_grow(void);
170 static void if_input_default(struct ifnet *, struct mbuf *);
171 static int if_requestencap_default(struct ifnet *, struct if_encap_req *);
172 static void if_route(struct ifnet *, int flag, int fam);
173 static int if_setflag(struct ifnet *, int, int, int *, int);
174 static int if_transmit(struct ifnet *ifp, struct mbuf *m);
175 static void if_unroute(struct ifnet *, int flag, int fam);
176 static void link_rtrequest(int, struct rtentry *, struct rt_addrinfo *);
177 static int ifhwioctl(u_long, struct ifnet *, caddr_t, struct thread *);
178 static int if_delmulti_locked(struct ifnet *, struct ifmultiaddr *, int);
179 static void do_link_state_change(void *, int);
180 static int if_getgroup(struct ifgroupreq *, struct ifnet *);
181 static int if_getgroupmembers(struct ifgroupreq *);
182 static void if_delgroups(struct ifnet *);
183 static void if_attach_internal(struct ifnet *, int, struct if_clone *);
184 static int if_detach_internal(struct ifnet *, int, struct if_clone **);
186 static void if_vmove(struct ifnet *, struct vnet *);
191 * XXX: declare here to avoid to include many inet6 related files..
192 * should be more generalized?
194 extern void nd6_setmtu(struct ifnet *);
197 /* ipsec helper hooks */
198 VNET_DEFINE(struct hhook_head *, ipsec_hhh_in[HHOOK_IPSEC_COUNT]);
199 VNET_DEFINE(struct hhook_head *, ipsec_hhh_out[HHOOK_IPSEC_COUNT]);
201 VNET_DEFINE(int, if_index);
202 int ifqmaxlen = IFQ_MAXLEN;
203 VNET_DEFINE(struct ifnethead, ifnet); /* depend on static init XXX */
204 VNET_DEFINE(struct ifgrouphead, ifg_head);
206 static VNET_DEFINE(int, if_indexlim) = 8;
208 /* Table of ifnet by index. */
209 VNET_DEFINE(struct ifnet **, ifindex_table);
211 #define V_if_indexlim VNET(if_indexlim)
212 #define V_ifindex_table VNET(ifindex_table)
215 * The global network interface list (V_ifnet) and related state (such as
216 * if_index, if_indexlim, and ifindex_table) are protected by an sxlock and
217 * an rwlock. Either may be acquired shared to stablize the list, but both
218 * must be acquired writable to modify the list. This model allows us to
219 * both stablize the interface list during interrupt thread processing, but
220 * also to stablize it over long-running ioctls, without introducing priority
221 * inversions and deadlocks.
223 struct rwlock ifnet_rwlock;
224 RW_SYSINIT_FLAGS(ifnet_rw, &ifnet_rwlock, "ifnet_rw", RW_RECURSE);
225 struct sx ifnet_sxlock;
226 SX_SYSINIT_FLAGS(ifnet_sx, &ifnet_sxlock, "ifnet_sx", SX_RECURSE);
229 * The allocation of network interfaces is a rather non-atomic affair; we
230 * need to select an index before we are ready to expose the interface for
231 * use, so will use this pointer value to indicate reservation.
233 #define IFNET_HOLD (void *)(uintptr_t)(-1)
235 static if_com_alloc_t *if_com_alloc[256];
236 static if_com_free_t *if_com_free[256];
238 static MALLOC_DEFINE(M_IFNET, "ifnet", "interface internals");
239 MALLOC_DEFINE(M_IFADDR, "ifaddr", "interface address");
240 MALLOC_DEFINE(M_IFMADDR, "ether_multi", "link-level multicast address");
243 ifnet_byindex_locked(u_short idx)
246 if (idx > V_if_index)
248 if (V_ifindex_table[idx] == IFNET_HOLD)
250 return (V_ifindex_table[idx]);
254 ifnet_byindex(u_short idx)
258 IFNET_RLOCK_NOSLEEP();
259 ifp = ifnet_byindex_locked(idx);
260 IFNET_RUNLOCK_NOSLEEP();
265 ifnet_byindex_ref(u_short idx)
269 IFNET_RLOCK_NOSLEEP();
270 ifp = ifnet_byindex_locked(idx);
271 if (ifp == NULL || (ifp->if_flags & IFF_DYING)) {
272 IFNET_RUNLOCK_NOSLEEP();
276 IFNET_RUNLOCK_NOSLEEP();
281 * Allocate an ifindex array entry; return 0 on success or an error on
289 IFNET_WLOCK_ASSERT();
292 * Try to find an empty slot below V_if_index. If we fail, take the
295 for (idx = 1; idx <= V_if_index; idx++) {
296 if (V_ifindex_table[idx] == NULL)
300 /* Catch if_index overflow. */
301 if (idx >= V_if_indexlim) {
305 if (idx > V_if_index)
311 ifindex_free_locked(u_short idx)
314 IFNET_WLOCK_ASSERT();
316 V_ifindex_table[idx] = NULL;
317 while (V_if_index > 0 &&
318 V_ifindex_table[V_if_index] == NULL)
323 ifindex_free(u_short idx)
327 ifindex_free_locked(idx);
332 ifnet_setbyindex_locked(u_short idx, struct ifnet *ifp)
335 IFNET_WLOCK_ASSERT();
337 V_ifindex_table[idx] = ifp;
341 ifnet_setbyindex(u_short idx, struct ifnet *ifp)
345 ifnet_setbyindex_locked(idx, ifp);
350 ifaddr_byindex(u_short idx)
353 struct ifaddr *ifa = NULL;
355 IFNET_RLOCK_NOSLEEP();
356 ifp = ifnet_byindex_locked(idx);
357 if (ifp != NULL && (ifa = ifp->if_addr) != NULL)
359 IFNET_RUNLOCK_NOSLEEP();
364 * Network interface utility routines.
366 * Routines with ifa_ifwith* names take sockaddr *'s as
371 vnet_if_init(const void *unused __unused)
374 TAILQ_INIT(&V_ifnet);
375 TAILQ_INIT(&V_ifg_head);
377 if_grow(); /* create initial table */
379 vnet_if_clone_init();
381 VNET_SYSINIT(vnet_if_init, SI_SUB_INIT_IF, SI_ORDER_SECOND, vnet_if_init,
386 vnet_if_uninit(const void *unused __unused)
389 VNET_ASSERT(TAILQ_EMPTY(&V_ifnet), ("%s:%d tailq &V_ifnet=%p "
390 "not empty", __func__, __LINE__, &V_ifnet));
391 VNET_ASSERT(TAILQ_EMPTY(&V_ifg_head), ("%s:%d tailq &V_ifg_head=%p "
392 "not empty", __func__, __LINE__, &V_ifg_head));
394 free((caddr_t)V_ifindex_table, M_IFNET);
396 VNET_SYSUNINIT(vnet_if_uninit, SI_SUB_INIT_IF, SI_ORDER_FIRST,
397 vnet_if_uninit, NULL);
400 vnet_if_return(const void *unused __unused)
402 struct ifnet *ifp, *nifp;
404 /* Return all inherited interfaces to their parent vnets. */
405 TAILQ_FOREACH_SAFE(ifp, &V_ifnet, if_link, nifp) {
406 if (ifp->if_home_vnet != ifp->if_vnet)
407 if_vmove(ifp, ifp->if_home_vnet);
410 VNET_SYSUNINIT(vnet_if_return, SI_SUB_VNET_DONE, SI_ORDER_ANY,
411 vnet_if_return, NULL);
421 IFNET_WLOCK_ASSERT();
422 oldlim = V_if_indexlim;
424 n = (oldlim << 1) * sizeof(*e);
425 e = malloc(n, M_IFNET, M_WAITOK | M_ZERO);
427 if (V_if_indexlim != oldlim) {
431 if (V_ifindex_table != NULL) {
432 memcpy((caddr_t)e, (caddr_t)V_ifindex_table, n/2);
433 free((caddr_t)V_ifindex_table, M_IFNET);
440 * Allocate a struct ifnet and an index for an interface. A layer 2
441 * common structure will also be allocated if an allocation routine is
442 * registered for the passed type.
445 if_alloc(u_char type)
450 ifp = malloc(sizeof(struct ifnet), M_IFNET, M_WAITOK|M_ZERO);
452 idx = ifindex_alloc();
453 ifnet_setbyindex_locked(idx, IFNET_HOLD);
457 ifp->if_alloctype = type;
458 if (if_com_alloc[type] != NULL) {
459 ifp->if_l2com = if_com_alloc[type](type, ifp);
460 if (ifp->if_l2com == NULL) {
467 IF_ADDR_LOCK_INIT(ifp);
468 TASK_INIT(&ifp->if_linktask, 0, do_link_state_change, ifp);
469 ifp->if_afdata_initialized = 0;
470 IF_AFDATA_LOCK_INIT(ifp);
471 TAILQ_INIT(&ifp->if_addrhead);
472 TAILQ_INIT(&ifp->if_multiaddrs);
473 TAILQ_INIT(&ifp->if_groups);
477 ifq_init(&ifp->if_snd, ifp);
479 refcount_init(&ifp->if_refcount, 1); /* Index reference. */
480 for (int i = 0; i < IFCOUNTERS; i++)
481 ifp->if_counters[i] = counter_u64_alloc(M_WAITOK);
482 ifp->if_get_counter = if_get_counter_default;
483 ifnet_setbyindex(ifp->if_index, ifp);
488 * Do the actual work of freeing a struct ifnet, and layer 2 common
489 * structure. This call is made when the last reference to an
490 * interface is released.
493 if_free_internal(struct ifnet *ifp)
496 KASSERT((ifp->if_flags & IFF_DYING),
497 ("if_free_internal: interface not dying"));
499 if (if_com_free[ifp->if_alloctype] != NULL)
500 if_com_free[ifp->if_alloctype](ifp->if_l2com,
504 mac_ifnet_destroy(ifp);
506 if (ifp->if_description != NULL)
507 free(ifp->if_description, M_IFDESCR);
508 IF_AFDATA_DESTROY(ifp);
509 IF_ADDR_LOCK_DESTROY(ifp);
510 ifq_delete(&ifp->if_snd);
512 for (int i = 0; i < IFCOUNTERS; i++)
513 counter_u64_free(ifp->if_counters[i]);
519 * Deregister an interface and free the associated storage.
522 if_free(struct ifnet *ifp)
525 ifp->if_flags |= IFF_DYING; /* XXX: Locking */
527 CURVNET_SET_QUIET(ifp->if_vnet);
529 KASSERT(ifp == ifnet_byindex_locked(ifp->if_index),
530 ("%s: freeing unallocated ifnet", ifp->if_xname));
532 ifindex_free_locked(ifp->if_index);
535 if (refcount_release(&ifp->if_refcount))
536 if_free_internal(ifp);
541 * Interfaces to keep an ifnet type-stable despite the possibility of the
542 * driver calling if_free(). If there are additional references, we defer
543 * freeing the underlying data structure.
546 if_ref(struct ifnet *ifp)
549 /* We don't assert the ifnet list lock here, but arguably should. */
550 refcount_acquire(&ifp->if_refcount);
554 if_rele(struct ifnet *ifp)
557 if (!refcount_release(&ifp->if_refcount))
559 if_free_internal(ifp);
563 ifq_init(struct ifaltq *ifq, struct ifnet *ifp)
566 mtx_init(&ifq->ifq_mtx, ifp->if_xname, "if send queue", MTX_DEF);
568 if (ifq->ifq_maxlen == 0)
569 ifq->ifq_maxlen = ifqmaxlen;
572 ifq->altq_disc = NULL;
573 ifq->altq_flags &= ALTQF_CANTCHANGE;
574 ifq->altq_tbr = NULL;
579 ifq_delete(struct ifaltq *ifq)
581 mtx_destroy(&ifq->ifq_mtx);
585 * Perform generic interface initialization tasks and attach the interface
586 * to the list of "active" interfaces. If vmove flag is set on entry
587 * to if_attach_internal(), perform only a limited subset of initialization
588 * tasks, given that we are moving from one vnet to another an ifnet which
589 * has already been fully initialized.
591 * Note that if_detach_internal() removes group membership unconditionally
592 * even when vmove flag is set, and if_attach_internal() adds only IFG_ALL.
593 * Thus, when if_vmove() is applied to a cloned interface, group membership
594 * is lost while a cloned one always joins a group whose name is
595 * ifc->ifc_name. To recover this after if_detach_internal() and
596 * if_attach_internal(), the cloner should be specified to
597 * if_attach_internal() via ifc. If it is non-NULL, if_attach_internal()
598 * attempts to join a group whose name is ifc->ifc_name.
601 * - The decision to return void and thus require this function to
602 * succeed is questionable.
603 * - We should probably do more sanity checking. For instance we don't
604 * do anything to insure if_xname is unique or non-empty.
607 if_attach(struct ifnet *ifp)
610 if_attach_internal(ifp, 0, NULL);
614 * Compute the least common TSO limit.
617 if_hw_tsomax_common(if_t ifp, struct ifnet_hw_tsomax *pmax)
620 * 1) If there is no limit currently, take the limit from
621 * the network adapter.
623 * 2) If the network adapter has a limit below the current
626 if (pmax->tsomaxbytes == 0 || (ifp->if_hw_tsomax != 0 &&
627 ifp->if_hw_tsomax < pmax->tsomaxbytes)) {
628 pmax->tsomaxbytes = ifp->if_hw_tsomax;
630 if (pmax->tsomaxsegcount == 0 || (ifp->if_hw_tsomaxsegcount != 0 &&
631 ifp->if_hw_tsomaxsegcount < pmax->tsomaxsegcount)) {
632 pmax->tsomaxsegcount = ifp->if_hw_tsomaxsegcount;
634 if (pmax->tsomaxsegsize == 0 || (ifp->if_hw_tsomaxsegsize != 0 &&
635 ifp->if_hw_tsomaxsegsize < pmax->tsomaxsegsize)) {
636 pmax->tsomaxsegsize = ifp->if_hw_tsomaxsegsize;
641 * Update TSO limit of a network adapter.
643 * Returns zero if no change. Else non-zero.
646 if_hw_tsomax_update(if_t ifp, struct ifnet_hw_tsomax *pmax)
649 if (ifp->if_hw_tsomax != pmax->tsomaxbytes) {
650 ifp->if_hw_tsomax = pmax->tsomaxbytes;
653 if (ifp->if_hw_tsomaxsegsize != pmax->tsomaxsegsize) {
654 ifp->if_hw_tsomaxsegsize = pmax->tsomaxsegsize;
657 if (ifp->if_hw_tsomaxsegcount != pmax->tsomaxsegcount) {
658 ifp->if_hw_tsomaxsegcount = pmax->tsomaxsegcount;
665 if_attach_internal(struct ifnet *ifp, int vmove, struct if_clone *ifc)
667 unsigned socksize, ifasize;
668 int namelen, masklen;
669 struct sockaddr_dl *sdl;
672 if (ifp->if_index == 0 || ifp != ifnet_byindex(ifp->if_index))
673 panic ("%s: BUG: if_attach called without if_alloc'd input()\n",
677 ifp->if_vnet = curvnet;
678 if (ifp->if_home_vnet == NULL)
679 ifp->if_home_vnet = curvnet;
682 if_addgroup(ifp, IFG_ALL);
684 /* Restore group membership for cloned interfaces. */
685 if (vmove && ifc != NULL)
686 if_clone_addgroup(ifp, ifc);
688 getmicrotime(&ifp->if_lastchange);
689 ifp->if_epoch = time_uptime;
691 KASSERT((ifp->if_transmit == NULL && ifp->if_qflush == NULL) ||
692 (ifp->if_transmit != NULL && ifp->if_qflush != NULL),
693 ("transmit and qflush must both either be set or both be NULL"));
694 if (ifp->if_transmit == NULL) {
695 ifp->if_transmit = if_transmit;
696 ifp->if_qflush = if_qflush;
698 if (ifp->if_input == NULL)
699 ifp->if_input = if_input_default;
701 if (ifp->if_requestencap == NULL)
702 ifp->if_requestencap = if_requestencap_default;
706 mac_ifnet_create(ifp);
710 * Create a Link Level name for this device.
712 namelen = strlen(ifp->if_xname);
714 * Always save enough space for any possiable name so we
715 * can do a rename in place later.
717 masklen = offsetof(struct sockaddr_dl, sdl_data[0]) + IFNAMSIZ;
718 socksize = masklen + ifp->if_addrlen;
719 if (socksize < sizeof(*sdl))
720 socksize = sizeof(*sdl);
721 socksize = roundup2(socksize, sizeof(long));
722 ifasize = sizeof(*ifa) + 2 * socksize;
723 ifa = ifa_alloc(ifasize, M_WAITOK);
724 sdl = (struct sockaddr_dl *)(ifa + 1);
725 sdl->sdl_len = socksize;
726 sdl->sdl_family = AF_LINK;
727 bcopy(ifp->if_xname, sdl->sdl_data, namelen);
728 sdl->sdl_nlen = namelen;
729 sdl->sdl_index = ifp->if_index;
730 sdl->sdl_type = ifp->if_type;
733 ifa->ifa_rtrequest = link_rtrequest;
734 ifa->ifa_addr = (struct sockaddr *)sdl;
735 sdl = (struct sockaddr_dl *)(socksize + (caddr_t)sdl);
736 ifa->ifa_netmask = (struct sockaddr *)sdl;
737 sdl->sdl_len = masklen;
739 sdl->sdl_data[--namelen] = 0xff;
740 TAILQ_INSERT_HEAD(&ifp->if_addrhead, ifa, ifa_link);
741 /* Reliably crash if used uninitialized. */
742 ifp->if_broadcastaddr = NULL;
744 #if defined(INET) || defined(INET6)
745 /* Use defaults for TSO, if nothing is set */
746 if (ifp->if_hw_tsomax == 0 &&
747 ifp->if_hw_tsomaxsegcount == 0 &&
748 ifp->if_hw_tsomaxsegsize == 0) {
750 * The TSO defaults needs to be such that an
751 * NFS mbuf list of 35 mbufs totalling just
752 * below 64K works and that a chain of mbufs
753 * can be defragged into at most 32 segments:
755 ifp->if_hw_tsomax = min(IP_MAXPACKET, (32 * MCLBYTES) -
756 (ETHER_HDR_LEN + ETHER_VLAN_ENCAP_LEN));
757 ifp->if_hw_tsomaxsegcount = 35;
758 ifp->if_hw_tsomaxsegsize = 2048; /* 2K */
760 /* XXX some drivers set IFCAP_TSO after ethernet attach */
761 if (ifp->if_capabilities & IFCAP_TSO) {
762 if_printf(ifp, "Using defaults for TSO: %u/%u/%u\n",
764 ifp->if_hw_tsomaxsegcount,
765 ifp->if_hw_tsomaxsegsize);
773 * Update the interface index in the link layer address
776 for (ifa = ifp->if_addr; ifa != NULL;
777 ifa = TAILQ_NEXT(ifa, ifa_link)) {
778 if (ifa->ifa_addr->sa_family == AF_LINK) {
779 sdl = (struct sockaddr_dl *)ifa->ifa_addr;
780 sdl->sdl_index = ifp->if_index;
787 TAILQ_INSERT_TAIL(&V_ifnet, ifp, if_link);
789 curvnet->vnet_ifcnt++;
793 if (domain_init_status >= 2)
794 if_attachdomain1(ifp);
796 EVENTHANDLER_INVOKE(ifnet_arrival_event, ifp);
797 if (IS_DEFAULT_VNET(curvnet))
798 devctl_notify("IFNET", ifp->if_xname, "ATTACH", NULL);
800 /* Announce the interface. */
801 rt_ifannouncemsg(ifp, IFAN_ARRIVAL);
805 if_attachdomain(void *dummy)
809 TAILQ_FOREACH(ifp, &V_ifnet, if_link)
810 if_attachdomain1(ifp);
812 SYSINIT(domainifattach, SI_SUB_PROTO_IFATTACHDOMAIN, SI_ORDER_SECOND,
813 if_attachdomain, NULL);
816 if_attachdomain1(struct ifnet *ifp)
821 * Since dp->dom_ifattach calls malloc() with M_WAITOK, we
822 * cannot lock ifp->if_afdata initialization, entirely.
825 if (ifp->if_afdata_initialized >= domain_init_status) {
826 IF_AFDATA_UNLOCK(ifp);
827 log(LOG_WARNING, "%s called more than once on %s\n",
828 __func__, ifp->if_xname);
831 ifp->if_afdata_initialized = domain_init_status;
832 IF_AFDATA_UNLOCK(ifp);
834 /* address family dependent data region */
835 bzero(ifp->if_afdata, sizeof(ifp->if_afdata));
836 for (dp = domains; dp; dp = dp->dom_next) {
837 if (dp->dom_ifattach)
838 ifp->if_afdata[dp->dom_family] =
839 (*dp->dom_ifattach)(ifp);
844 * Remove any unicast or broadcast network addresses from an interface.
847 if_purgeaddrs(struct ifnet *ifp)
849 struct ifaddr *ifa, *next;
851 /* XXX cannot hold IF_ADDR_WLOCK over called functions. */
852 TAILQ_FOREACH_SAFE(ifa, &ifp->if_addrhead, ifa_link, next) {
853 if (ifa->ifa_addr->sa_family == AF_LINK)
856 /* XXX: Ugly!! ad hoc just for INET */
857 if (ifa->ifa_addr->sa_family == AF_INET) {
858 struct ifaliasreq ifr;
860 bzero(&ifr, sizeof(ifr));
861 ifr.ifra_addr = *ifa->ifa_addr;
862 if (ifa->ifa_dstaddr)
863 ifr.ifra_broadaddr = *ifa->ifa_dstaddr;
864 if (in_control(NULL, SIOCDIFADDR, (caddr_t)&ifr, ifp,
870 if (ifa->ifa_addr->sa_family == AF_INET6) {
872 /* ifp_addrhead is already updated */
877 TAILQ_REMOVE(&ifp->if_addrhead, ifa, ifa_link);
878 IF_ADDR_WUNLOCK(ifp);
884 * Remove any multicast network addresses from an interface when an ifnet
888 if_purgemaddrs(struct ifnet *ifp)
890 struct ifmultiaddr *ifma;
891 struct ifmultiaddr *next;
894 TAILQ_FOREACH_SAFE(ifma, &ifp->if_multiaddrs, ifma_link, next)
895 if_delmulti_locked(ifp, ifma, 1);
896 IF_ADDR_WUNLOCK(ifp);
900 * Detach an interface, removing it from the list of "active" interfaces.
901 * If vmove flag is set on entry to if_detach_internal(), perform only a
902 * limited subset of cleanup tasks, given that we are moving an ifnet from
903 * one vnet to another, where it must be fully operational.
905 * XXXRW: There are some significant questions about event ordering, and
906 * how to prevent things from starting to use the interface during detach.
909 if_detach(struct ifnet *ifp)
912 CURVNET_SET_QUIET(ifp->if_vnet);
913 if_detach_internal(ifp, 0, NULL);
918 * The vmove flag, if set, indicates that we are called from a callpath
919 * that is moving an interface to a different vnet instance.
921 * The shutdown flag, if set, indicates that we are called in the
922 * process of shutting down a vnet instance. Currently only the
923 * vnet_if_return SYSUNINIT function sets it. Note: we can be called
924 * on a vnet instance shutdown without this flag being set, e.g., when
925 * the cloned interfaces are destoyed as first thing of teardown.
928 if_detach_internal(struct ifnet *ifp, int vmove, struct if_clone **ifcp)
938 shutdown = (ifp->if_vnet->vnet_state > SI_SUB_VNET &&
939 ifp->if_vnet->vnet_state < SI_SUB_VNET_DONE) ? 1 : 0;
942 TAILQ_FOREACH(iter, &V_ifnet, if_link)
944 TAILQ_REMOVE(&V_ifnet, ifp, if_link);
951 * While we would want to panic here, we cannot
952 * guarantee that the interface is indeed still on
953 * the list given we don't hold locks all the way.
958 panic("%s: ifp=%p not on the ifnet tailq %p",
959 __func__, ifp, &V_ifnet);
961 return; /* XXX this should panic as well? */
966 * At this point we know the interface still was on the ifnet list
967 * and we removed it so we are in a stable state.
970 curvnet->vnet_ifcnt--;
974 * In any case (destroy or vmove) detach us from the groups
975 * and remove/wait for pending events on the taskq.
976 * XXX-BZ in theory an interface could still enqueue a taskq change?
980 taskqueue_drain(taskqueue_swi, &ifp->if_linktask);
983 * Check if this is a cloned interface or not. Must do even if
984 * shutting down as a if_vmove_reclaim() would move the ifp and
985 * the if_clone_addgroup() will have a corrupted string overwise
986 * from a gibberish pointer.
988 if (vmove && ifcp != NULL)
989 *ifcp = if_clone_findifc(ifp);
995 * On VNET shutdown abort here as the stack teardown will do all
996 * the work top-down for us.
1000 * In case of a vmove we are done here without error.
1001 * If we would signal an error it would lead to the same
1002 * abort as if we did not find the ifnet anymore.
1003 * if_detach() calls us in void context and does not care
1004 * about an early abort notification, so life is splendid :)
1006 goto finish_vnet_shutdown;
1011 * At this point we are not tearing down a VNET and are either
1012 * going to destroy or vmove the interface and have to cleanup
1017 * Remove routes and flush queues.
1020 if (ALTQ_IS_ENABLED(&ifp->if_snd))
1021 altq_disable(&ifp->if_snd);
1022 if (ALTQ_IS_ATTACHED(&ifp->if_snd))
1023 altq_detach(&ifp->if_snd);
1034 * Remove all IPv6 kernel structs related to ifp. This should be done
1035 * before removing routing entries below, since IPv6 interface direct
1036 * routes are expected to be removed by the IPv6-specific kernel API.
1037 * Otherwise, the kernel will detect some inconsistency and bark it.
1041 if_purgemaddrs(ifp);
1043 /* Announce that the interface is gone. */
1044 rt_ifannouncemsg(ifp, IFAN_DEPARTURE);
1045 EVENTHANDLER_INVOKE(ifnet_departure_event, ifp);
1046 if (IS_DEFAULT_VNET(curvnet))
1047 devctl_notify("IFNET", ifp->if_xname, "DETACH", NULL);
1051 * Prevent further calls into the device driver via ifnet.
1056 * Remove link ifaddr pointer and maybe decrement if_index.
1057 * Clean up all addresses.
1059 ifp->if_addr = NULL;
1061 /* We can now free link ifaddr. */
1063 if (!TAILQ_EMPTY(&ifp->if_addrhead)) {
1064 ifa = TAILQ_FIRST(&ifp->if_addrhead);
1065 TAILQ_REMOVE(&ifp->if_addrhead, ifa, ifa_link);
1066 IF_ADDR_WUNLOCK(ifp);
1069 IF_ADDR_WUNLOCK(ifp);
1072 rt_flushifroutes(ifp);
1075 finish_vnet_shutdown:
1078 * We cannot hold the lock over dom_ifdetach calls as they might
1079 * sleep, for example trying to drain a callout, thus open up the
1080 * theoretical race with re-attaching.
1082 IF_AFDATA_LOCK(ifp);
1083 i = ifp->if_afdata_initialized;
1084 ifp->if_afdata_initialized = 0;
1085 IF_AFDATA_UNLOCK(ifp);
1086 for (dp = domains; i > 0 && dp; dp = dp->dom_next) {
1087 if (dp->dom_ifdetach && ifp->if_afdata[dp->dom_family]) {
1088 (*dp->dom_ifdetach)(ifp,
1089 ifp->if_afdata[dp->dom_family]);
1090 ifp->if_afdata[dp->dom_family] = NULL;
1099 * if_vmove() performs a limited version of if_detach() in current
1100 * vnet and if_attach()es the ifnet to the vnet specified as 2nd arg.
1101 * An attempt is made to shrink if_index in current vnet, find an
1102 * unused if_index in target vnet and calls if_grow() if necessary,
1103 * and finally find an unused if_xname for the target vnet.
1106 if_vmove(struct ifnet *ifp, struct vnet *new_vnet)
1108 struct if_clone *ifc;
1109 u_int bif_dlt, bif_hdrlen;
1113 * if_detach_internal() will call the eventhandler to notify
1114 * interface departure. That will detach if_bpf. We need to
1115 * safe the dlt and hdrlen so we can re-attach it later.
1117 bpf_get_bp_params(ifp->if_bpf, &bif_dlt, &bif_hdrlen);
1120 * Detach from current vnet, but preserve LLADDR info, do not
1121 * mark as dead etc. so that the ifnet can be reattached later.
1122 * If we cannot find it, we lost the race to someone else.
1124 rc = if_detach_internal(ifp, 1, &ifc);
1129 * Unlink the ifnet from ifindex_table[] in current vnet, and shrink
1130 * the if_index for that vnet if possible.
1132 * NOTE: IFNET_WLOCK/IFNET_WUNLOCK() are assumed to be unvirtualized,
1133 * or we'd lock on one vnet and unlock on another.
1136 ifindex_free_locked(ifp->if_index);
1140 * Perform interface-specific reassignment tasks, if provided by
1143 if (ifp->if_reassign != NULL)
1144 ifp->if_reassign(ifp, new_vnet, NULL);
1147 * Switch to the context of the target vnet.
1149 CURVNET_SET_QUIET(new_vnet);
1152 ifp->if_index = ifindex_alloc();
1153 ifnet_setbyindex_locked(ifp->if_index, ifp);
1156 if_attach_internal(ifp, 1, ifc);
1158 if (ifp->if_bpf == NULL)
1159 bpfattach(ifp, bif_dlt, bif_hdrlen);
1165 * Move an ifnet to or from another child prison/vnet, specified by the jail id.
1168 if_vmove_loan(struct thread *td, struct ifnet *ifp, char *ifname, int jid)
1174 /* Try to find the prison within our visibility. */
1175 sx_slock(&allprison_lock);
1176 pr = prison_find_child(td->td_ucred->cr_prison, jid);
1177 sx_sunlock(&allprison_lock);
1180 prison_hold_locked(pr);
1181 mtx_unlock(&pr->pr_mtx);
1183 /* Do not try to move the iface from and to the same prison. */
1184 if (pr->pr_vnet == ifp->if_vnet) {
1189 /* Make sure the named iface does not exists in the dst. prison/vnet. */
1190 /* XXX Lock interfaces to avoid races. */
1191 CURVNET_SET_QUIET(pr->pr_vnet);
1192 difp = ifunit(ifname);
1199 /* Make sure the VNET is stable. */
1200 shutdown = (ifp->if_vnet->vnet_state > SI_SUB_VNET &&
1201 ifp->if_vnet->vnet_state < SI_SUB_VNET_DONE) ? 1 : 0;
1209 /* Move the interface into the child jail/vnet. */
1210 if_vmove(ifp, pr->pr_vnet);
1212 /* Report the new if_xname back to the userland. */
1213 sprintf(ifname, "%s", ifp->if_xname);
1220 if_vmove_reclaim(struct thread *td, char *ifname, int jid)
1223 struct vnet *vnet_dst;
1227 /* Try to find the prison within our visibility. */
1228 sx_slock(&allprison_lock);
1229 pr = prison_find_child(td->td_ucred->cr_prison, jid);
1230 sx_sunlock(&allprison_lock);
1233 prison_hold_locked(pr);
1234 mtx_unlock(&pr->pr_mtx);
1236 /* Make sure the named iface exists in the source prison/vnet. */
1237 CURVNET_SET(pr->pr_vnet);
1238 ifp = ifunit(ifname); /* XXX Lock to avoid races. */
1245 /* Do not try to move the iface from and to the same prison. */
1246 vnet_dst = TD_TO_VNET(td);
1247 if (vnet_dst == ifp->if_vnet) {
1253 /* Make sure the VNET is stable. */
1254 shutdown = (ifp->if_vnet->vnet_state > SI_SUB_VNET &&
1255 ifp->if_vnet->vnet_state < SI_SUB_VNET_DONE) ? 1 : 0;
1262 /* Get interface back from child jail/vnet. */
1263 if_vmove(ifp, vnet_dst);
1266 /* Report the new if_xname back to the userland. */
1267 sprintf(ifname, "%s", ifp->if_xname);
1275 * Add a group to an interface
1278 if_addgroup(struct ifnet *ifp, const char *groupname)
1280 struct ifg_list *ifgl;
1281 struct ifg_group *ifg = NULL;
1282 struct ifg_member *ifgm;
1285 if (groupname[0] && groupname[strlen(groupname) - 1] >= '0' &&
1286 groupname[strlen(groupname) - 1] <= '9')
1290 TAILQ_FOREACH(ifgl, &ifp->if_groups, ifgl_next)
1291 if (!strcmp(ifgl->ifgl_group->ifg_group, groupname)) {
1296 if ((ifgl = (struct ifg_list *)malloc(sizeof(struct ifg_list), M_TEMP,
1297 M_NOWAIT)) == NULL) {
1302 if ((ifgm = (struct ifg_member *)malloc(sizeof(struct ifg_member),
1303 M_TEMP, M_NOWAIT)) == NULL) {
1309 TAILQ_FOREACH(ifg, &V_ifg_head, ifg_next)
1310 if (!strcmp(ifg->ifg_group, groupname))
1314 if ((ifg = (struct ifg_group *)malloc(sizeof(struct ifg_group),
1315 M_TEMP, M_NOWAIT)) == NULL) {
1321 strlcpy(ifg->ifg_group, groupname, sizeof(ifg->ifg_group));
1322 ifg->ifg_refcnt = 0;
1323 TAILQ_INIT(&ifg->ifg_members);
1324 TAILQ_INSERT_TAIL(&V_ifg_head, ifg, ifg_next);
1329 ifgl->ifgl_group = ifg;
1330 ifgm->ifgm_ifp = ifp;
1333 TAILQ_INSERT_TAIL(&ifg->ifg_members, ifgm, ifgm_next);
1334 TAILQ_INSERT_TAIL(&ifp->if_groups, ifgl, ifgl_next);
1335 IF_ADDR_WUNLOCK(ifp);
1340 EVENTHANDLER_INVOKE(group_attach_event, ifg);
1341 EVENTHANDLER_INVOKE(group_change_event, groupname);
1347 * Remove a group from an interface
1350 if_delgroup(struct ifnet *ifp, const char *groupname)
1352 struct ifg_list *ifgl;
1353 struct ifg_member *ifgm;
1356 TAILQ_FOREACH(ifgl, &ifp->if_groups, ifgl_next)
1357 if (!strcmp(ifgl->ifgl_group->ifg_group, groupname))
1365 TAILQ_REMOVE(&ifp->if_groups, ifgl, ifgl_next);
1366 IF_ADDR_WUNLOCK(ifp);
1368 TAILQ_FOREACH(ifgm, &ifgl->ifgl_group->ifg_members, ifgm_next)
1369 if (ifgm->ifgm_ifp == ifp)
1373 TAILQ_REMOVE(&ifgl->ifgl_group->ifg_members, ifgm, ifgm_next);
1377 if (--ifgl->ifgl_group->ifg_refcnt == 0) {
1378 TAILQ_REMOVE(&V_ifg_head, ifgl->ifgl_group, ifg_next);
1380 EVENTHANDLER_INVOKE(group_detach_event, ifgl->ifgl_group);
1381 free(ifgl->ifgl_group, M_TEMP);
1387 EVENTHANDLER_INVOKE(group_change_event, groupname);
1393 * Remove an interface from all groups
1396 if_delgroups(struct ifnet *ifp)
1398 struct ifg_list *ifgl;
1399 struct ifg_member *ifgm;
1400 char groupname[IFNAMSIZ];
1403 while (!TAILQ_EMPTY(&ifp->if_groups)) {
1404 ifgl = TAILQ_FIRST(&ifp->if_groups);
1406 strlcpy(groupname, ifgl->ifgl_group->ifg_group, IFNAMSIZ);
1409 TAILQ_REMOVE(&ifp->if_groups, ifgl, ifgl_next);
1410 IF_ADDR_WUNLOCK(ifp);
1412 TAILQ_FOREACH(ifgm, &ifgl->ifgl_group->ifg_members, ifgm_next)
1413 if (ifgm->ifgm_ifp == ifp)
1417 TAILQ_REMOVE(&ifgl->ifgl_group->ifg_members, ifgm,
1422 if (--ifgl->ifgl_group->ifg_refcnt == 0) {
1423 TAILQ_REMOVE(&V_ifg_head, ifgl->ifgl_group, ifg_next);
1425 EVENTHANDLER_INVOKE(group_detach_event,
1427 free(ifgl->ifgl_group, M_TEMP);
1433 EVENTHANDLER_INVOKE(group_change_event, groupname);
1441 * Stores all groups from an interface in memory pointed
1445 if_getgroup(struct ifgroupreq *data, struct ifnet *ifp)
1448 struct ifg_list *ifgl;
1449 struct ifg_req ifgrq, *ifgp;
1450 struct ifgroupreq *ifgr = data;
1452 if (ifgr->ifgr_len == 0) {
1454 TAILQ_FOREACH(ifgl, &ifp->if_groups, ifgl_next)
1455 ifgr->ifgr_len += sizeof(struct ifg_req);
1456 IF_ADDR_RUNLOCK(ifp);
1460 len = ifgr->ifgr_len;
1461 ifgp = ifgr->ifgr_groups;
1464 TAILQ_FOREACH(ifgl, &ifp->if_groups, ifgl_next) {
1465 if (len < sizeof(ifgrq)) {
1466 IF_ADDR_RUNLOCK(ifp);
1469 bzero(&ifgrq, sizeof ifgrq);
1470 strlcpy(ifgrq.ifgrq_group, ifgl->ifgl_group->ifg_group,
1471 sizeof(ifgrq.ifgrq_group));
1472 if ((error = copyout(&ifgrq, ifgp, sizeof(struct ifg_req)))) {
1473 IF_ADDR_RUNLOCK(ifp);
1476 len -= sizeof(ifgrq);
1479 IF_ADDR_RUNLOCK(ifp);
1485 * Stores all members of a group in memory pointed to by data
1488 if_getgroupmembers(struct ifgroupreq *data)
1490 struct ifgroupreq *ifgr = data;
1491 struct ifg_group *ifg;
1492 struct ifg_member *ifgm;
1493 struct ifg_req ifgrq, *ifgp;
1497 TAILQ_FOREACH(ifg, &V_ifg_head, ifg_next)
1498 if (!strcmp(ifg->ifg_group, ifgr->ifgr_name))
1505 if (ifgr->ifgr_len == 0) {
1506 TAILQ_FOREACH(ifgm, &ifg->ifg_members, ifgm_next)
1507 ifgr->ifgr_len += sizeof(ifgrq);
1512 len = ifgr->ifgr_len;
1513 ifgp = ifgr->ifgr_groups;
1514 TAILQ_FOREACH(ifgm, &ifg->ifg_members, ifgm_next) {
1515 if (len < sizeof(ifgrq)) {
1519 bzero(&ifgrq, sizeof ifgrq);
1520 strlcpy(ifgrq.ifgrq_member, ifgm->ifgm_ifp->if_xname,
1521 sizeof(ifgrq.ifgrq_member));
1522 if ((error = copyout(&ifgrq, ifgp, sizeof(struct ifg_req)))) {
1526 len -= sizeof(ifgrq);
1535 * Return counter values from counter(9)s stored in ifnet.
1538 if_get_counter_default(struct ifnet *ifp, ift_counter cnt)
1541 KASSERT(cnt < IFCOUNTERS, ("%s: invalid cnt %d", __func__, cnt));
1543 return (counter_u64_fetch(ifp->if_counters[cnt]));
1547 * Increase an ifnet counter. Usually used for counters shared
1548 * between the stack and a driver, but function supports them all.
1551 if_inc_counter(struct ifnet *ifp, ift_counter cnt, int64_t inc)
1554 KASSERT(cnt < IFCOUNTERS, ("%s: invalid cnt %d", __func__, cnt));
1556 counter_u64_add(ifp->if_counters[cnt], inc);
1560 * Copy data from ifnet to userland API structure if_data.
1563 if_data_copy(struct ifnet *ifp, struct if_data *ifd)
1566 ifd->ifi_type = ifp->if_type;
1567 ifd->ifi_physical = 0;
1568 ifd->ifi_addrlen = ifp->if_addrlen;
1569 ifd->ifi_hdrlen = ifp->if_hdrlen;
1570 ifd->ifi_link_state = ifp->if_link_state;
1572 ifd->ifi_datalen = sizeof(struct if_data);
1573 ifd->ifi_mtu = ifp->if_mtu;
1574 ifd->ifi_metric = ifp->if_metric;
1575 ifd->ifi_baudrate = ifp->if_baudrate;
1576 ifd->ifi_hwassist = ifp->if_hwassist;
1577 ifd->ifi_epoch = ifp->if_epoch;
1578 ifd->ifi_lastchange = ifp->if_lastchange;
1580 ifd->ifi_ipackets = ifp->if_get_counter(ifp, IFCOUNTER_IPACKETS);
1581 ifd->ifi_ierrors = ifp->if_get_counter(ifp, IFCOUNTER_IERRORS);
1582 ifd->ifi_opackets = ifp->if_get_counter(ifp, IFCOUNTER_OPACKETS);
1583 ifd->ifi_oerrors = ifp->if_get_counter(ifp, IFCOUNTER_OERRORS);
1584 ifd->ifi_collisions = ifp->if_get_counter(ifp, IFCOUNTER_COLLISIONS);
1585 ifd->ifi_ibytes = ifp->if_get_counter(ifp, IFCOUNTER_IBYTES);
1586 ifd->ifi_obytes = ifp->if_get_counter(ifp, IFCOUNTER_OBYTES);
1587 ifd->ifi_imcasts = ifp->if_get_counter(ifp, IFCOUNTER_IMCASTS);
1588 ifd->ifi_omcasts = ifp->if_get_counter(ifp, IFCOUNTER_OMCASTS);
1589 ifd->ifi_iqdrops = ifp->if_get_counter(ifp, IFCOUNTER_IQDROPS);
1590 ifd->ifi_oqdrops = ifp->if_get_counter(ifp, IFCOUNTER_OQDROPS);
1591 ifd->ifi_noproto = ifp->if_get_counter(ifp, IFCOUNTER_NOPROTO);
1595 * Wrapper functions for struct ifnet address list locking macros. These are
1596 * used by kernel modules to avoid encoding programming interface or binary
1597 * interface assumptions that may be violated when kernel-internal locking
1598 * approaches change.
1601 if_addr_rlock(struct ifnet *ifp)
1608 if_addr_runlock(struct ifnet *ifp)
1611 IF_ADDR_RUNLOCK(ifp);
1615 if_maddr_rlock(if_t ifp)
1618 IF_ADDR_RLOCK((struct ifnet *)ifp);
1622 if_maddr_runlock(if_t ifp)
1625 IF_ADDR_RUNLOCK((struct ifnet *)ifp);
1629 * Initialization, destruction and refcounting functions for ifaddrs.
1632 ifa_alloc(size_t size, int flags)
1636 KASSERT(size >= sizeof(struct ifaddr),
1637 ("%s: invalid size %zu", __func__, size));
1639 ifa = malloc(size, M_IFADDR, M_ZERO | flags);
1643 if ((ifa->ifa_opackets = counter_u64_alloc(flags)) == NULL)
1645 if ((ifa->ifa_ipackets = counter_u64_alloc(flags)) == NULL)
1647 if ((ifa->ifa_obytes = counter_u64_alloc(flags)) == NULL)
1649 if ((ifa->ifa_ibytes = counter_u64_alloc(flags)) == NULL)
1652 refcount_init(&ifa->ifa_refcnt, 1);
1657 /* free(NULL) is okay */
1658 counter_u64_free(ifa->ifa_opackets);
1659 counter_u64_free(ifa->ifa_ipackets);
1660 counter_u64_free(ifa->ifa_obytes);
1661 counter_u64_free(ifa->ifa_ibytes);
1662 free(ifa, M_IFADDR);
1668 ifa_ref(struct ifaddr *ifa)
1671 refcount_acquire(&ifa->ifa_refcnt);
1675 ifa_free(struct ifaddr *ifa)
1678 if (refcount_release(&ifa->ifa_refcnt)) {
1679 counter_u64_free(ifa->ifa_opackets);
1680 counter_u64_free(ifa->ifa_ipackets);
1681 counter_u64_free(ifa->ifa_obytes);
1682 counter_u64_free(ifa->ifa_ibytes);
1683 free(ifa, M_IFADDR);
1688 ifa_maintain_loopback_route(int cmd, const char *otype, struct ifaddr *ifa,
1689 struct sockaddr *ia)
1692 struct rt_addrinfo info;
1693 struct sockaddr_dl null_sdl;
1698 bzero(&info, sizeof(info));
1699 if (cmd != RTM_DELETE)
1700 info.rti_ifp = V_loif;
1701 info.rti_flags = ifa->ifa_flags | RTF_HOST | RTF_STATIC;
1702 info.rti_info[RTAX_DST] = ia;
1703 info.rti_info[RTAX_GATEWAY] = (struct sockaddr *)&null_sdl;
1704 link_init_sdl(ifp, (struct sockaddr *)&null_sdl, ifp->if_type);
1706 error = rtrequest1_fib(cmd, &info, NULL, ifp->if_fib);
1709 log(LOG_DEBUG, "%s: %s failed for interface %s: %u\n",
1710 __func__, otype, if_name(ifp), error);
1716 ifa_add_loopback_route(struct ifaddr *ifa, struct sockaddr *ia)
1719 return (ifa_maintain_loopback_route(RTM_ADD, "insertion", ifa, ia));
1723 ifa_del_loopback_route(struct ifaddr *ifa, struct sockaddr *ia)
1726 return (ifa_maintain_loopback_route(RTM_DELETE, "deletion", ifa, ia));
1730 ifa_switch_loopback_route(struct ifaddr *ifa, struct sockaddr *ia)
1733 return (ifa_maintain_loopback_route(RTM_CHANGE, "switch", ifa, ia));
1737 * XXX: Because sockaddr_dl has deeper structure than the sockaddr
1738 * structs used to represent other address families, it is necessary
1739 * to perform a different comparison.
1742 #define sa_dl_equal(a1, a2) \
1743 ((((const struct sockaddr_dl *)(a1))->sdl_len == \
1744 ((const struct sockaddr_dl *)(a2))->sdl_len) && \
1745 (bcmp(CLLADDR((const struct sockaddr_dl *)(a1)), \
1746 CLLADDR((const struct sockaddr_dl *)(a2)), \
1747 ((const struct sockaddr_dl *)(a1))->sdl_alen) == 0))
1750 * Locate an interface based on a complete address.
1753 static struct ifaddr *
1754 ifa_ifwithaddr_internal(const struct sockaddr *addr, int getref)
1759 IFNET_RLOCK_NOSLEEP();
1760 TAILQ_FOREACH(ifp, &V_ifnet, if_link) {
1762 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
1763 if (ifa->ifa_addr->sa_family != addr->sa_family)
1765 if (sa_equal(addr, ifa->ifa_addr)) {
1768 IF_ADDR_RUNLOCK(ifp);
1771 /* IP6 doesn't have broadcast */
1772 if ((ifp->if_flags & IFF_BROADCAST) &&
1773 ifa->ifa_broadaddr &&
1774 ifa->ifa_broadaddr->sa_len != 0 &&
1775 sa_equal(ifa->ifa_broadaddr, addr)) {
1778 IF_ADDR_RUNLOCK(ifp);
1782 IF_ADDR_RUNLOCK(ifp);
1786 IFNET_RUNLOCK_NOSLEEP();
1791 ifa_ifwithaddr(const struct sockaddr *addr)
1794 return (ifa_ifwithaddr_internal(addr, 1));
1798 ifa_ifwithaddr_check(const struct sockaddr *addr)
1801 return (ifa_ifwithaddr_internal(addr, 0) != NULL);
1805 * Locate an interface based on the broadcast address.
1809 ifa_ifwithbroadaddr(const struct sockaddr *addr, int fibnum)
1814 IFNET_RLOCK_NOSLEEP();
1815 TAILQ_FOREACH(ifp, &V_ifnet, if_link) {
1816 if ((fibnum != RT_ALL_FIBS) && (ifp->if_fib != fibnum))
1819 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
1820 if (ifa->ifa_addr->sa_family != addr->sa_family)
1822 if ((ifp->if_flags & IFF_BROADCAST) &&
1823 ifa->ifa_broadaddr &&
1824 ifa->ifa_broadaddr->sa_len != 0 &&
1825 sa_equal(ifa->ifa_broadaddr, addr)) {
1827 IF_ADDR_RUNLOCK(ifp);
1831 IF_ADDR_RUNLOCK(ifp);
1835 IFNET_RUNLOCK_NOSLEEP();
1840 * Locate the point to point interface with a given destination address.
1844 ifa_ifwithdstaddr(const struct sockaddr *addr, int fibnum)
1849 IFNET_RLOCK_NOSLEEP();
1850 TAILQ_FOREACH(ifp, &V_ifnet, if_link) {
1851 if ((ifp->if_flags & IFF_POINTOPOINT) == 0)
1853 if ((fibnum != RT_ALL_FIBS) && (ifp->if_fib != fibnum))
1856 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
1857 if (ifa->ifa_addr->sa_family != addr->sa_family)
1859 if (ifa->ifa_dstaddr != NULL &&
1860 sa_equal(addr, ifa->ifa_dstaddr)) {
1862 IF_ADDR_RUNLOCK(ifp);
1866 IF_ADDR_RUNLOCK(ifp);
1870 IFNET_RUNLOCK_NOSLEEP();
1875 * Find an interface on a specific network. If many, choice
1876 * is most specific found.
1879 ifa_ifwithnet(const struct sockaddr *addr, int ignore_ptp, int fibnum)
1883 struct ifaddr *ifa_maybe = NULL;
1884 u_int af = addr->sa_family;
1885 const char *addr_data = addr->sa_data, *cplim;
1888 * AF_LINK addresses can be looked up directly by their index number,
1889 * so do that if we can.
1891 if (af == AF_LINK) {
1892 const struct sockaddr_dl *sdl = (const struct sockaddr_dl *)addr;
1893 if (sdl->sdl_index && sdl->sdl_index <= V_if_index)
1894 return (ifaddr_byindex(sdl->sdl_index));
1898 * Scan though each interface, looking for ones that have addresses
1899 * in this address family and the requested fib. Maintain a reference
1900 * on ifa_maybe once we find one, as we release the IF_ADDR_RLOCK() that
1901 * kept it stable when we move onto the next interface.
1903 IFNET_RLOCK_NOSLEEP();
1904 TAILQ_FOREACH(ifp, &V_ifnet, if_link) {
1905 if ((fibnum != RT_ALL_FIBS) && (ifp->if_fib != fibnum))
1908 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
1909 const char *cp, *cp2, *cp3;
1911 if (ifa->ifa_addr->sa_family != af)
1913 if (af == AF_INET &&
1914 ifp->if_flags & IFF_POINTOPOINT && !ignore_ptp) {
1916 * This is a bit broken as it doesn't
1917 * take into account that the remote end may
1918 * be a single node in the network we are
1920 * The trouble is that we don't know the
1921 * netmask for the remote end.
1923 if (ifa->ifa_dstaddr != NULL &&
1924 sa_equal(addr, ifa->ifa_dstaddr)) {
1926 IF_ADDR_RUNLOCK(ifp);
1931 * Scan all the bits in the ifa's address.
1932 * If a bit dissagrees with what we are
1933 * looking for, mask it with the netmask
1934 * to see if it really matters.
1935 * (A byte at a time)
1937 if (ifa->ifa_netmask == 0)
1940 cp2 = ifa->ifa_addr->sa_data;
1941 cp3 = ifa->ifa_netmask->sa_data;
1942 cplim = ifa->ifa_netmask->sa_len
1943 + (char *)ifa->ifa_netmask;
1945 if ((*cp++ ^ *cp2++) & *cp3++)
1946 goto next; /* next address! */
1948 * If the netmask of what we just found
1949 * is more specific than what we had before
1950 * (if we had one), or if the virtual status
1951 * of new prefix is better than of the old one,
1952 * then remember the new one before continuing
1953 * to search for an even better one.
1955 if (ifa_maybe == NULL ||
1956 ifa_preferred(ifa_maybe, ifa) ||
1957 rn_refines((caddr_t)ifa->ifa_netmask,
1958 (caddr_t)ifa_maybe->ifa_netmask)) {
1959 if (ifa_maybe != NULL)
1960 ifa_free(ifa_maybe);
1966 IF_ADDR_RUNLOCK(ifp);
1971 IFNET_RUNLOCK_NOSLEEP();
1972 if (ifa_maybe != NULL)
1973 ifa_free(ifa_maybe);
1978 * Find an interface address specific to an interface best matching
1982 ifaof_ifpforaddr(const struct sockaddr *addr, struct ifnet *ifp)
1985 const char *cp, *cp2, *cp3;
1987 struct ifaddr *ifa_maybe = NULL;
1988 u_int af = addr->sa_family;
1993 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
1994 if (ifa->ifa_addr->sa_family != af)
1996 if (ifa_maybe == NULL)
1998 if (ifa->ifa_netmask == 0) {
1999 if (sa_equal(addr, ifa->ifa_addr) ||
2000 (ifa->ifa_dstaddr &&
2001 sa_equal(addr, ifa->ifa_dstaddr)))
2005 if (ifp->if_flags & IFF_POINTOPOINT) {
2006 if (sa_equal(addr, ifa->ifa_dstaddr))
2010 cp2 = ifa->ifa_addr->sa_data;
2011 cp3 = ifa->ifa_netmask->sa_data;
2012 cplim = ifa->ifa_netmask->sa_len + (char *)ifa->ifa_netmask;
2013 for (; cp3 < cplim; cp3++)
2014 if ((*cp++ ^ *cp2++) & *cp3)
2024 IF_ADDR_RUNLOCK(ifp);
2029 * See whether new ifa is better than current one:
2030 * 1) A non-virtual one is preferred over virtual.
2031 * 2) A virtual in master state preferred over any other state.
2033 * Used in several address selecting functions.
2036 ifa_preferred(struct ifaddr *cur, struct ifaddr *next)
2039 return (cur->ifa_carp && (!next->ifa_carp ||
2040 ((*carp_master_p)(next) && !(*carp_master_p)(cur))));
2043 #include <net/if_llatbl.h>
2046 * Default action when installing a route with a Link Level gateway.
2047 * Lookup an appropriate real ifa to point to.
2048 * This should be moved to /sys/net/link.c eventually.
2051 link_rtrequest(int cmd, struct rtentry *rt, struct rt_addrinfo *info)
2053 struct ifaddr *ifa, *oifa;
2054 struct sockaddr *dst;
2057 if (cmd != RTM_ADD || ((ifa = rt->rt_ifa) == NULL) ||
2058 ((ifp = ifa->ifa_ifp) == NULL) || ((dst = rt_key(rt)) == NULL))
2060 ifa = ifaof_ifpforaddr(dst, ifp);
2065 if (ifa->ifa_rtrequest && ifa->ifa_rtrequest != link_rtrequest)
2066 ifa->ifa_rtrequest(cmd, rt, info);
2070 struct sockaddr_dl *
2071 link_alloc_sdl(size_t size, int flags)
2074 return (malloc(size, M_TEMP, flags));
2078 link_free_sdl(struct sockaddr *sa)
2084 * Fills in given sdl with interface basic info.
2085 * Returns pointer to filled sdl.
2087 struct sockaddr_dl *
2088 link_init_sdl(struct ifnet *ifp, struct sockaddr *paddr, u_char iftype)
2090 struct sockaddr_dl *sdl;
2092 sdl = (struct sockaddr_dl *)paddr;
2093 memset(sdl, 0, sizeof(struct sockaddr_dl));
2094 sdl->sdl_len = sizeof(struct sockaddr_dl);
2095 sdl->sdl_family = AF_LINK;
2096 sdl->sdl_index = ifp->if_index;
2097 sdl->sdl_type = iftype;
2103 * Mark an interface down and notify protocols of
2107 if_unroute(struct ifnet *ifp, int flag, int fam)
2111 KASSERT(flag == IFF_UP, ("if_unroute: flag != IFF_UP"));
2113 ifp->if_flags &= ~flag;
2114 getmicrotime(&ifp->if_lastchange);
2115 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link)
2116 if (fam == PF_UNSPEC || (fam == ifa->ifa_addr->sa_family))
2117 pfctlinput(PRC_IFDOWN, ifa->ifa_addr);
2118 ifp->if_qflush(ifp);
2121 (*carp_linkstate_p)(ifp);
2126 * Mark an interface up and notify protocols of
2130 if_route(struct ifnet *ifp, int flag, int fam)
2134 KASSERT(flag == IFF_UP, ("if_route: flag != IFF_UP"));
2136 ifp->if_flags |= flag;
2137 getmicrotime(&ifp->if_lastchange);
2138 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link)
2139 if (fam == PF_UNSPEC || (fam == ifa->ifa_addr->sa_family))
2140 pfctlinput(PRC_IFUP, ifa->ifa_addr);
2142 (*carp_linkstate_p)(ifp);
2149 void (*vlan_link_state_p)(struct ifnet *); /* XXX: private from if_vlan */
2150 void (*vlan_trunk_cap_p)(struct ifnet *); /* XXX: private from if_vlan */
2151 struct ifnet *(*vlan_trunkdev_p)(struct ifnet *);
2152 struct ifnet *(*vlan_devat_p)(struct ifnet *, uint16_t);
2153 int (*vlan_tag_p)(struct ifnet *, uint16_t *);
2154 int (*vlan_setcookie_p)(struct ifnet *, void *);
2155 void *(*vlan_cookie_p)(struct ifnet *);
2158 * Handle a change in the interface link state. To avoid LORs
2159 * between driver lock and upper layer locks, as well as possible
2160 * recursions, we post event to taskqueue, and all job
2161 * is done in static do_link_state_change().
2164 if_link_state_change(struct ifnet *ifp, int link_state)
2166 /* Return if state hasn't changed. */
2167 if (ifp->if_link_state == link_state)
2170 ifp->if_link_state = link_state;
2172 taskqueue_enqueue(taskqueue_swi, &ifp->if_linktask);
2176 do_link_state_change(void *arg, int pending)
2178 struct ifnet *ifp = (struct ifnet *)arg;
2179 int link_state = ifp->if_link_state;
2180 CURVNET_SET(ifp->if_vnet);
2182 /* Notify that the link state has changed. */
2184 if (ifp->if_vlantrunk != NULL)
2185 (*vlan_link_state_p)(ifp);
2187 if ((ifp->if_type == IFT_ETHER || ifp->if_type == IFT_L2VLAN) &&
2188 ifp->if_l2com != NULL)
2189 (*ng_ether_link_state_p)(ifp, link_state);
2191 (*carp_linkstate_p)(ifp);
2193 (*bridge_linkstate_p)(ifp);
2195 (*lagg_linkstate_p)(ifp, link_state);
2197 if (IS_DEFAULT_VNET(curvnet))
2198 devctl_notify("IFNET", ifp->if_xname,
2199 (link_state == LINK_STATE_UP) ? "LINK_UP" : "LINK_DOWN",
2202 if_printf(ifp, "%d link states coalesced\n", pending);
2203 if (log_link_state_change)
2204 log(LOG_NOTICE, "%s: link state changed to %s\n", ifp->if_xname,
2205 (link_state == LINK_STATE_UP) ? "UP" : "DOWN" );
2206 EVENTHANDLER_INVOKE(ifnet_link_event, ifp, ifp->if_link_state);
2211 * Mark an interface down and notify protocols of
2215 if_down(struct ifnet *ifp)
2218 if_unroute(ifp, IFF_UP, AF_UNSPEC);
2222 * Mark an interface up and notify protocols of
2226 if_up(struct ifnet *ifp)
2229 if_route(ifp, IFF_UP, AF_UNSPEC);
2233 * Flush an interface queue.
2236 if_qflush(struct ifnet *ifp)
2244 if (ALTQ_IS_ENABLED(ifq))
2248 while ((m = n) != NULL) {
2259 * Map interface name to interface structure pointer, with or without
2260 * returning a reference.
2263 ifunit_ref(const char *name)
2267 IFNET_RLOCK_NOSLEEP();
2268 TAILQ_FOREACH(ifp, &V_ifnet, if_link) {
2269 if (strncmp(name, ifp->if_xname, IFNAMSIZ) == 0 &&
2270 !(ifp->if_flags & IFF_DYING))
2275 IFNET_RUNLOCK_NOSLEEP();
2280 ifunit(const char *name)
2284 IFNET_RLOCK_NOSLEEP();
2285 TAILQ_FOREACH(ifp, &V_ifnet, if_link) {
2286 if (strncmp(name, ifp->if_xname, IFNAMSIZ) == 0)
2289 IFNET_RUNLOCK_NOSLEEP();
2294 * Hardware specific interface ioctls.
2297 ifhwioctl(u_long cmd, struct ifnet *ifp, caddr_t data, struct thread *td)
2301 int new_flags, temp_flags;
2302 size_t namelen, onamelen;
2304 char *descrbuf, *odescrbuf;
2305 char new_name[IFNAMSIZ];
2307 struct sockaddr_dl *sdl;
2309 ifr = (struct ifreq *)data;
2312 ifr->ifr_index = ifp->if_index;
2316 temp_flags = ifp->if_flags | ifp->if_drv_flags;
2317 ifr->ifr_flags = temp_flags & 0xffff;
2318 ifr->ifr_flagshigh = temp_flags >> 16;
2322 ifr->ifr_reqcap = ifp->if_capabilities;
2323 ifr->ifr_curcap = ifp->if_capenable;
2328 error = mac_ifnet_ioctl_get(td->td_ucred, ifr, ifp);
2333 ifr->ifr_metric = ifp->if_metric;
2337 ifr->ifr_mtu = ifp->if_mtu;
2341 /* XXXGL: did this ever worked? */
2347 sx_slock(&ifdescr_sx);
2348 if (ifp->if_description == NULL)
2351 /* space for terminating nul */
2352 descrlen = strlen(ifp->if_description) + 1;
2353 if (ifr->ifr_buffer.length < descrlen)
2354 ifr->ifr_buffer.buffer = NULL;
2356 error = copyout(ifp->if_description,
2357 ifr->ifr_buffer.buffer, descrlen);
2358 ifr->ifr_buffer.length = descrlen;
2360 sx_sunlock(&ifdescr_sx);
2364 error = priv_check(td, PRIV_NET_SETIFDESCR);
2369 * Copy only (length-1) bytes to make sure that
2370 * if_description is always nul terminated. The
2371 * length parameter is supposed to count the
2372 * terminating nul in.
2374 if (ifr->ifr_buffer.length > ifdescr_maxlen)
2375 return (ENAMETOOLONG);
2376 else if (ifr->ifr_buffer.length == 0)
2379 descrbuf = malloc(ifr->ifr_buffer.length, M_IFDESCR,
2381 error = copyin(ifr->ifr_buffer.buffer, descrbuf,
2382 ifr->ifr_buffer.length - 1);
2384 free(descrbuf, M_IFDESCR);
2389 sx_xlock(&ifdescr_sx);
2390 odescrbuf = ifp->if_description;
2391 ifp->if_description = descrbuf;
2392 sx_xunlock(&ifdescr_sx);
2394 getmicrotime(&ifp->if_lastchange);
2395 free(odescrbuf, M_IFDESCR);
2399 ifr->ifr_fib = ifp->if_fib;
2403 error = priv_check(td, PRIV_NET_SETIFFIB);
2406 if (ifr->ifr_fib >= rt_numfibs)
2409 ifp->if_fib = ifr->ifr_fib;
2413 error = priv_check(td, PRIV_NET_SETIFFLAGS);
2417 * Currently, no driver owned flags pass the IFF_CANTCHANGE
2418 * check, so we don't need special handling here yet.
2420 new_flags = (ifr->ifr_flags & 0xffff) |
2421 (ifr->ifr_flagshigh << 16);
2422 if (ifp->if_flags & IFF_UP &&
2423 (new_flags & IFF_UP) == 0) {
2425 } else if (new_flags & IFF_UP &&
2426 (ifp->if_flags & IFF_UP) == 0) {
2429 /* See if permanently promiscuous mode bit is about to flip */
2430 if ((ifp->if_flags ^ new_flags) & IFF_PPROMISC) {
2431 if (new_flags & IFF_PPROMISC)
2432 ifp->if_flags |= IFF_PROMISC;
2433 else if (ifp->if_pcount == 0)
2434 ifp->if_flags &= ~IFF_PROMISC;
2435 if (log_promisc_mode_change)
2436 log(LOG_INFO, "%s: permanently promiscuous mode %s\n",
2438 ((new_flags & IFF_PPROMISC) ?
2439 "enabled" : "disabled"));
2441 ifp->if_flags = (ifp->if_flags & IFF_CANTCHANGE) |
2442 (new_flags &~ IFF_CANTCHANGE);
2443 if (ifp->if_ioctl) {
2444 (void) (*ifp->if_ioctl)(ifp, cmd, data);
2446 getmicrotime(&ifp->if_lastchange);
2450 error = priv_check(td, PRIV_NET_SETIFCAP);
2453 if (ifp->if_ioctl == NULL)
2454 return (EOPNOTSUPP);
2455 if (ifr->ifr_reqcap & ~ifp->if_capabilities)
2457 error = (*ifp->if_ioctl)(ifp, cmd, data);
2459 getmicrotime(&ifp->if_lastchange);
2464 error = mac_ifnet_ioctl_set(td->td_ucred, ifr, ifp);
2469 error = priv_check(td, PRIV_NET_SETIFNAME);
2472 error = copyinstr(ifr->ifr_data, new_name, IFNAMSIZ, NULL);
2475 if (new_name[0] == '\0')
2477 if (new_name[IFNAMSIZ-1] != '\0') {
2478 new_name[IFNAMSIZ-1] = '\0';
2479 if (strlen(new_name) == IFNAMSIZ-1)
2482 if (ifunit(new_name) != NULL)
2486 * XXX: Locking. Nothing else seems to lock if_flags,
2487 * and there are numerous other races with the
2488 * ifunit() checks not being atomic with namespace
2489 * changes (renames, vmoves, if_attach, etc).
2491 ifp->if_flags |= IFF_RENAMING;
2493 /* Announce the departure of the interface. */
2494 rt_ifannouncemsg(ifp, IFAN_DEPARTURE);
2495 EVENTHANDLER_INVOKE(ifnet_departure_event, ifp);
2497 log(LOG_INFO, "%s: changing name to '%s'\n",
2498 ifp->if_xname, new_name);
2501 strlcpy(ifp->if_xname, new_name, sizeof(ifp->if_xname));
2503 sdl = (struct sockaddr_dl *)ifa->ifa_addr;
2504 namelen = strlen(new_name);
2505 onamelen = sdl->sdl_nlen;
2507 * Move the address if needed. This is safe because we
2508 * allocate space for a name of length IFNAMSIZ when we
2509 * create this in if_attach().
2511 if (namelen != onamelen) {
2512 bcopy(sdl->sdl_data + onamelen,
2513 sdl->sdl_data + namelen, sdl->sdl_alen);
2515 bcopy(new_name, sdl->sdl_data, namelen);
2516 sdl->sdl_nlen = namelen;
2517 sdl = (struct sockaddr_dl *)ifa->ifa_netmask;
2518 bzero(sdl->sdl_data, onamelen);
2519 while (namelen != 0)
2520 sdl->sdl_data[--namelen] = 0xff;
2521 IF_ADDR_WUNLOCK(ifp);
2523 EVENTHANDLER_INVOKE(ifnet_arrival_event, ifp);
2524 /* Announce the return of the interface. */
2525 rt_ifannouncemsg(ifp, IFAN_ARRIVAL);
2527 ifp->if_flags &= ~IFF_RENAMING;
2532 error = priv_check(td, PRIV_NET_SETIFVNET);
2535 error = if_vmove_loan(td, ifp, ifr->ifr_name, ifr->ifr_jid);
2540 error = priv_check(td, PRIV_NET_SETIFMETRIC);
2543 ifp->if_metric = ifr->ifr_metric;
2544 getmicrotime(&ifp->if_lastchange);
2548 error = priv_check(td, PRIV_NET_SETIFPHYS);
2551 if (ifp->if_ioctl == NULL)
2552 return (EOPNOTSUPP);
2553 error = (*ifp->if_ioctl)(ifp, cmd, data);
2555 getmicrotime(&ifp->if_lastchange);
2560 u_long oldmtu = ifp->if_mtu;
2562 error = priv_check(td, PRIV_NET_SETIFMTU);
2565 if (ifr->ifr_mtu < IF_MINMTU || ifr->ifr_mtu > IF_MAXMTU)
2567 if (ifp->if_ioctl == NULL)
2568 return (EOPNOTSUPP);
2569 error = (*ifp->if_ioctl)(ifp, cmd, data);
2571 getmicrotime(&ifp->if_lastchange);
2575 * If the link MTU changed, do network layer specific procedure.
2577 if (ifp->if_mtu != oldmtu) {
2588 if (cmd == SIOCADDMULTI)
2589 error = priv_check(td, PRIV_NET_ADDMULTI);
2591 error = priv_check(td, PRIV_NET_DELMULTI);
2595 /* Don't allow group membership on non-multicast interfaces. */
2596 if ((ifp->if_flags & IFF_MULTICAST) == 0)
2597 return (EOPNOTSUPP);
2599 /* Don't let users screw up protocols' entries. */
2600 if (ifr->ifr_addr.sa_family != AF_LINK)
2603 if (cmd == SIOCADDMULTI) {
2604 struct ifmultiaddr *ifma;
2607 * Userland is only permitted to join groups once
2608 * via the if_addmulti() KPI, because it cannot hold
2609 * struct ifmultiaddr * between calls. It may also
2610 * lose a race while we check if the membership
2614 ifma = if_findmulti(ifp, &ifr->ifr_addr);
2615 IF_ADDR_RUNLOCK(ifp);
2619 error = if_addmulti(ifp, &ifr->ifr_addr, &ifma);
2621 error = if_delmulti(ifp, &ifr->ifr_addr);
2624 getmicrotime(&ifp->if_lastchange);
2627 case SIOCSIFPHYADDR:
2628 case SIOCDIFPHYADDR:
2630 case SIOCSIFPHYADDR_IN6:
2633 case SIOCSIFGENERIC:
2634 error = priv_check(td, PRIV_NET_HWIOCTL);
2637 if (ifp->if_ioctl == NULL)
2638 return (EOPNOTSUPP);
2639 error = (*ifp->if_ioctl)(ifp, cmd, data);
2641 getmicrotime(&ifp->if_lastchange);
2645 case SIOCGIFPSRCADDR:
2646 case SIOCGIFPDSTADDR:
2649 case SIOCGIFGENERIC:
2650 if (ifp->if_ioctl == NULL)
2651 return (EOPNOTSUPP);
2652 error = (*ifp->if_ioctl)(ifp, cmd, data);
2656 error = priv_check(td, PRIV_NET_SETLLADDR);
2659 error = if_setlladdr(ifp,
2660 ifr->ifr_addr.sa_data, ifr->ifr_addr.sa_len);
2665 struct ifgroupreq *ifgr = (struct ifgroupreq *)ifr;
2667 error = priv_check(td, PRIV_NET_ADDIFGROUP);
2670 if ((error = if_addgroup(ifp, ifgr->ifgr_group)))
2676 if ((error = if_getgroup((struct ifgroupreq *)ifr, ifp)))
2682 struct ifgroupreq *ifgr = (struct ifgroupreq *)ifr;
2684 error = priv_check(td, PRIV_NET_DELIFGROUP);
2687 if ((error = if_delgroup(ifp, ifgr->ifgr_group)))
2699 #ifdef COMPAT_FREEBSD32
2707 #define SIOCGIFCONF32 _IOWR('i', 36, struct ifconf32)
2714 ifioctl(struct socket *so, u_long cmd, caddr_t data, struct thread *td)
2724 CURVNET_SET(so->so_vnet);
2726 /* Make sure the VNET is stable. */
2727 shutdown = (so->so_vnet->vnet_state > SI_SUB_VNET &&
2728 so->so_vnet->vnet_state < SI_SUB_VNET_DONE) ? 1 : 0;
2738 error = ifconf(cmd, data);
2742 #ifdef COMPAT_FREEBSD32
2745 struct ifconf32 *ifc32;
2748 ifc32 = (struct ifconf32 *)data;
2749 ifc.ifc_len = ifc32->ifc_len;
2750 ifc.ifc_buf = PTRIN(ifc32->ifc_buf);
2752 error = ifconf(SIOCGIFCONF, (void *)&ifc);
2755 ifc32->ifc_len = ifc.ifc_len;
2760 ifr = (struct ifreq *)data;
2765 error = priv_check(td, PRIV_NET_SETIFVNET);
2767 error = if_vmove_reclaim(td, ifr->ifr_name,
2774 error = priv_check(td, PRIV_NET_IFCREATE);
2776 error = if_clone_create(ifr->ifr_name,
2777 sizeof(ifr->ifr_name),
2778 cmd == SIOCIFCREATE2 ? ifr->ifr_data : NULL);
2782 error = priv_check(td, PRIV_NET_IFDESTROY);
2784 error = if_clone_destroy(ifr->ifr_name);
2788 case SIOCIFGCLONERS:
2789 error = if_clone_list((struct if_clonereq *)data);
2793 error = if_getgroupmembers((struct ifgroupreq *)data);
2796 #if defined(INET) || defined(INET6)
2799 if (carp_ioctl_p == NULL)
2800 error = EPROTONOSUPPORT;
2802 error = (*carp_ioctl_p)(ifr, cmd, td);
2808 ifp = ifunit_ref(ifr->ifr_name);
2814 error = ifhwioctl(cmd, ifp, data, td);
2815 if (error != ENOIOCTL) {
2821 oif_flags = ifp->if_flags;
2822 if (so->so_proto == NULL) {
2825 return (EOPNOTSUPP);
2829 * Pass the request on to the socket control method, and if the
2830 * latter returns EOPNOTSUPP, directly to the interface.
2832 * Make an exception for the legacy SIOCSIF* requests. Drivers
2833 * trust SIOCSIFADDR et al to come from an already privileged
2834 * layer, and do not perform any credentials checks or input
2837 error = ((*so->so_proto->pr_usrreqs->pru_control)(so, cmd, data,
2839 if (error == EOPNOTSUPP && ifp != NULL && ifp->if_ioctl != NULL &&
2840 cmd != SIOCSIFADDR && cmd != SIOCSIFBRDADDR &&
2841 cmd != SIOCSIFDSTADDR && cmd != SIOCSIFNETMASK)
2842 error = (*ifp->if_ioctl)(ifp, cmd, data);
2844 if ((oif_flags ^ ifp->if_flags) & IFF_UP) {
2846 if (ifp->if_flags & IFF_UP)
2856 * The code common to handling reference counted flags,
2857 * e.g., in ifpromisc() and if_allmulti().
2858 * The "pflag" argument can specify a permanent mode flag to check,
2859 * such as IFF_PPROMISC for promiscuous mode; should be 0 if none.
2861 * Only to be used on stack-owned flags, not driver-owned flags.
2864 if_setflag(struct ifnet *ifp, int flag, int pflag, int *refcount, int onswitch)
2868 int oldflags, oldcount;
2870 /* Sanity checks to catch programming errors */
2871 KASSERT((flag & (IFF_DRV_OACTIVE|IFF_DRV_RUNNING)) == 0,
2872 ("%s: setting driver-owned flag %d", __func__, flag));
2875 KASSERT(*refcount >= 0,
2876 ("%s: increment negative refcount %d for flag %d",
2877 __func__, *refcount, flag));
2879 KASSERT(*refcount > 0,
2880 ("%s: decrement non-positive refcount %d for flag %d",
2881 __func__, *refcount, flag));
2883 /* In case this mode is permanent, just touch refcount */
2884 if (ifp->if_flags & pflag) {
2885 *refcount += onswitch ? 1 : -1;
2889 /* Save ifnet parameters for if_ioctl() may fail */
2890 oldcount = *refcount;
2891 oldflags = ifp->if_flags;
2894 * See if we aren't the only and touching refcount is enough.
2895 * Actually toggle interface flag if we are the first or last.
2900 ifp->if_flags |= flag;
2904 ifp->if_flags &= ~flag;
2907 /* Call down the driver since we've changed interface flags */
2908 if (ifp->if_ioctl == NULL) {
2912 ifr.ifr_flags = ifp->if_flags & 0xffff;
2913 ifr.ifr_flagshigh = ifp->if_flags >> 16;
2914 error = (*ifp->if_ioctl)(ifp, SIOCSIFFLAGS, (caddr_t)&ifr);
2917 /* Notify userland that interface flags have changed */
2922 /* Recover after driver error */
2923 *refcount = oldcount;
2924 ifp->if_flags = oldflags;
2929 * Set/clear promiscuous mode on interface ifp based on the truth value
2930 * of pswitch. The calls are reference counted so that only the first
2931 * "on" request actually has an effect, as does the final "off" request.
2932 * Results are undefined if the "off" and "on" requests are not matched.
2935 ifpromisc(struct ifnet *ifp, int pswitch)
2938 int oldflags = ifp->if_flags;
2940 error = if_setflag(ifp, IFF_PROMISC, IFF_PPROMISC,
2941 &ifp->if_pcount, pswitch);
2942 /* If promiscuous mode status has changed, log a message */
2943 if (error == 0 && ((ifp->if_flags ^ oldflags) & IFF_PROMISC) &&
2944 log_promisc_mode_change)
2945 log(LOG_INFO, "%s: promiscuous mode %s\n",
2947 (ifp->if_flags & IFF_PROMISC) ? "enabled" : "disabled");
2952 * Return interface configuration
2953 * of system. List may be used
2954 * in later ioctl's (above) to get
2955 * other information.
2959 ifconf(u_long cmd, caddr_t data)
2961 struct ifconf *ifc = (struct ifconf *)data;
2966 int error, full = 0, valid_len, max_len;
2968 /* Limit initial buffer size to MAXPHYS to avoid DoS from userspace. */
2969 max_len = MAXPHYS - 1;
2971 /* Prevent hostile input from being able to crash the system */
2972 if (ifc->ifc_len <= 0)
2976 if (ifc->ifc_len <= max_len) {
2977 max_len = ifc->ifc_len;
2980 sb = sbuf_new(NULL, NULL, max_len + 1, SBUF_FIXEDLEN);
2985 TAILQ_FOREACH(ifp, &V_ifnet, if_link) {
2989 * Zero the ifr_name buffer to make sure we don't
2990 * disclose the contents of the stack.
2992 memset(ifr.ifr_name, 0, sizeof(ifr.ifr_name));
2994 if (strlcpy(ifr.ifr_name, ifp->if_xname, sizeof(ifr.ifr_name))
2995 >= sizeof(ifr.ifr_name)) {
2998 return (ENAMETOOLONG);
3003 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
3004 struct sockaddr *sa = ifa->ifa_addr;
3006 if (prison_if(curthread->td_ucred, sa) != 0)
3009 if (sa->sa_len <= sizeof(*sa)) {
3011 sbuf_bcat(sb, &ifr, sizeof(ifr));
3012 max_len += sizeof(ifr);
3015 offsetof(struct ifreq, ifr_addr));
3016 max_len += offsetof(struct ifreq, ifr_addr);
3017 sbuf_bcat(sb, sa, sa->sa_len);
3018 max_len += sa->sa_len;
3021 if (sbuf_error(sb) == 0)
3022 valid_len = sbuf_len(sb);
3024 IF_ADDR_RUNLOCK(ifp);
3026 bzero((caddr_t)&ifr.ifr_addr, sizeof(ifr.ifr_addr));
3027 sbuf_bcat(sb, &ifr, sizeof(ifr));
3028 max_len += sizeof(ifr);
3030 if (sbuf_error(sb) == 0)
3031 valid_len = sbuf_len(sb);
3037 * If we didn't allocate enough space (uncommon), try again. If
3038 * we have already allocated as much space as we are allowed,
3039 * return what we've got.
3041 if (valid_len != max_len && !full) {
3046 ifc->ifc_len = valid_len;
3048 error = copyout(sbuf_data(sb), ifc->ifc_req, ifc->ifc_len);
3054 * Just like ifpromisc(), but for all-multicast-reception mode.
3057 if_allmulti(struct ifnet *ifp, int onswitch)
3060 return (if_setflag(ifp, IFF_ALLMULTI, 0, &ifp->if_amcount, onswitch));
3063 struct ifmultiaddr *
3064 if_findmulti(struct ifnet *ifp, const struct sockaddr *sa)
3066 struct ifmultiaddr *ifma;
3068 IF_ADDR_LOCK_ASSERT(ifp);
3070 TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
3071 if (sa->sa_family == AF_LINK) {
3072 if (sa_dl_equal(ifma->ifma_addr, sa))
3075 if (sa_equal(ifma->ifma_addr, sa))
3084 * Allocate a new ifmultiaddr and initialize based on passed arguments. We
3085 * make copies of passed sockaddrs. The ifmultiaddr will not be added to
3086 * the ifnet multicast address list here, so the caller must do that and
3087 * other setup work (such as notifying the device driver). The reference
3088 * count is initialized to 1.
3090 static struct ifmultiaddr *
3091 if_allocmulti(struct ifnet *ifp, struct sockaddr *sa, struct sockaddr *llsa,
3094 struct ifmultiaddr *ifma;
3095 struct sockaddr *dupsa;
3097 ifma = malloc(sizeof *ifma, M_IFMADDR, mflags |
3102 dupsa = malloc(sa->sa_len, M_IFMADDR, mflags);
3103 if (dupsa == NULL) {
3104 free(ifma, M_IFMADDR);
3107 bcopy(sa, dupsa, sa->sa_len);
3108 ifma->ifma_addr = dupsa;
3110 ifma->ifma_ifp = ifp;
3111 ifma->ifma_refcount = 1;
3112 ifma->ifma_protospec = NULL;
3115 ifma->ifma_lladdr = NULL;
3119 dupsa = malloc(llsa->sa_len, M_IFMADDR, mflags);
3120 if (dupsa == NULL) {
3121 free(ifma->ifma_addr, M_IFMADDR);
3122 free(ifma, M_IFMADDR);
3125 bcopy(llsa, dupsa, llsa->sa_len);
3126 ifma->ifma_lladdr = dupsa;
3132 * if_freemulti: free ifmultiaddr structure and possibly attached related
3133 * addresses. The caller is responsible for implementing reference
3134 * counting, notifying the driver, handling routing messages, and releasing
3135 * any dependent link layer state.
3138 if_freemulti(struct ifmultiaddr *ifma)
3141 KASSERT(ifma->ifma_refcount == 0, ("if_freemulti: refcount %d",
3142 ifma->ifma_refcount));
3144 if (ifma->ifma_lladdr != NULL)
3145 free(ifma->ifma_lladdr, M_IFMADDR);
3146 free(ifma->ifma_addr, M_IFMADDR);
3147 free(ifma, M_IFMADDR);
3151 * Register an additional multicast address with a network interface.
3153 * - If the address is already present, bump the reference count on the
3154 * address and return.
3155 * - If the address is not link-layer, look up a link layer address.
3156 * - Allocate address structures for one or both addresses, and attach to the
3157 * multicast address list on the interface. If automatically adding a link
3158 * layer address, the protocol address will own a reference to the link
3159 * layer address, to be freed when it is freed.
3160 * - Notify the network device driver of an addition to the multicast address
3163 * 'sa' points to caller-owned memory with the desired multicast address.
3165 * 'retifma' will be used to return a pointer to the resulting multicast
3166 * address reference, if desired.
3169 if_addmulti(struct ifnet *ifp, struct sockaddr *sa,
3170 struct ifmultiaddr **retifma)
3172 struct ifmultiaddr *ifma, *ll_ifma;
3173 struct sockaddr *llsa;
3174 struct sockaddr_dl sdl;
3178 * If the address is already present, return a new reference to it;
3179 * otherwise, allocate storage and set up a new address.
3182 ifma = if_findmulti(ifp, sa);
3184 ifma->ifma_refcount++;
3185 if (retifma != NULL)
3187 IF_ADDR_WUNLOCK(ifp);
3192 * The address isn't already present; resolve the protocol address
3193 * into a link layer address, and then look that up, bump its
3194 * refcount or allocate an ifma for that also.
3195 * Most link layer resolving functions returns address data which
3196 * fits inside default sockaddr_dl structure. However callback
3197 * can allocate another sockaddr structure, in that case we need to
3202 if (ifp->if_resolvemulti != NULL) {
3203 /* Provide called function with buffer size information */
3204 sdl.sdl_len = sizeof(sdl);
3205 llsa = (struct sockaddr *)&sdl;
3206 error = ifp->if_resolvemulti(ifp, &llsa, sa);
3212 * Allocate the new address. Don't hook it up yet, as we may also
3213 * need to allocate a link layer multicast address.
3215 ifma = if_allocmulti(ifp, sa, llsa, M_NOWAIT);
3222 * If a link layer address is found, we'll need to see if it's
3223 * already present in the address list, or allocate is as well.
3224 * When this block finishes, the link layer address will be on the
3228 ll_ifma = if_findmulti(ifp, llsa);
3229 if (ll_ifma == NULL) {
3230 ll_ifma = if_allocmulti(ifp, llsa, NULL, M_NOWAIT);
3231 if (ll_ifma == NULL) {
3232 --ifma->ifma_refcount;
3237 TAILQ_INSERT_HEAD(&ifp->if_multiaddrs, ll_ifma,
3240 ll_ifma->ifma_refcount++;
3241 ifma->ifma_llifma = ll_ifma;
3245 * We now have a new multicast address, ifma, and possibly a new or
3246 * referenced link layer address. Add the primary address to the
3247 * ifnet address list.
3249 TAILQ_INSERT_HEAD(&ifp->if_multiaddrs, ifma, ifma_link);
3251 if (retifma != NULL)
3255 * Must generate the message while holding the lock so that 'ifma'
3256 * pointer is still valid.
3258 rt_newmaddrmsg(RTM_NEWMADDR, ifma);
3259 IF_ADDR_WUNLOCK(ifp);
3262 * We are certain we have added something, so call down to the
3263 * interface to let them know about it.
3265 if (ifp->if_ioctl != NULL) {
3266 (void) (*ifp->if_ioctl)(ifp, SIOCADDMULTI, 0);
3269 if ((llsa != NULL) && (llsa != (struct sockaddr *)&sdl))
3270 link_free_sdl(llsa);
3275 if ((llsa != NULL) && (llsa != (struct sockaddr *)&sdl))
3276 link_free_sdl(llsa);
3279 IF_ADDR_WUNLOCK(ifp);
3284 * Delete a multicast group membership by network-layer group address.
3286 * Returns ENOENT if the entry could not be found. If ifp no longer
3287 * exists, results are undefined. This entry point should only be used
3288 * from subsystems which do appropriate locking to hold ifp for the
3289 * duration of the call.
3290 * Network-layer protocol domains must use if_delmulti_ifma().
3293 if_delmulti(struct ifnet *ifp, struct sockaddr *sa)
3295 struct ifmultiaddr *ifma;
3300 IFNET_RLOCK_NOSLEEP();
3301 TAILQ_FOREACH(oifp, &V_ifnet, if_link)
3306 IFNET_RUNLOCK_NOSLEEP();
3308 KASSERT(ifp != NULL, ("%s: ifnet went away", __func__));
3315 ifma = if_findmulti(ifp, sa);
3317 lastref = if_delmulti_locked(ifp, ifma, 0);
3318 IF_ADDR_WUNLOCK(ifp);
3323 if (lastref && ifp->if_ioctl != NULL) {
3324 (void)(*ifp->if_ioctl)(ifp, SIOCDELMULTI, 0);
3331 * Delete all multicast group membership for an interface.
3332 * Should be used to quickly flush all multicast filters.
3335 if_delallmulti(struct ifnet *ifp)
3337 struct ifmultiaddr *ifma;
3338 struct ifmultiaddr *next;
3341 TAILQ_FOREACH_SAFE(ifma, &ifp->if_multiaddrs, ifma_link, next)
3342 if_delmulti_locked(ifp, ifma, 0);
3343 IF_ADDR_WUNLOCK(ifp);
3347 * Delete a multicast group membership by group membership pointer.
3348 * Network-layer protocol domains must use this routine.
3350 * It is safe to call this routine if the ifp disappeared.
3353 if_delmulti_ifma(struct ifmultiaddr *ifma)
3358 ifp = ifma->ifma_ifp;
3361 printf("%s: ifma_ifp seems to be detached\n", __func__);
3365 IFNET_RLOCK_NOSLEEP();
3366 TAILQ_FOREACH(oifp, &V_ifnet, if_link)
3370 printf("%s: ifnet %p disappeared\n", __func__, ifp);
3373 IFNET_RUNLOCK_NOSLEEP();
3377 * If and only if the ifnet instance exists: Acquire the address lock.
3382 lastref = if_delmulti_locked(ifp, ifma, 0);
3386 * If and only if the ifnet instance exists:
3387 * Release the address lock.
3388 * If the group was left: update the hardware hash filter.
3390 IF_ADDR_WUNLOCK(ifp);
3391 if (lastref && ifp->if_ioctl != NULL) {
3392 (void)(*ifp->if_ioctl)(ifp, SIOCDELMULTI, 0);
3398 * Perform deletion of network-layer and/or link-layer multicast address.
3400 * Return 0 if the reference count was decremented.
3401 * Return 1 if the final reference was released, indicating that the
3402 * hardware hash filter should be reprogrammed.
3405 if_delmulti_locked(struct ifnet *ifp, struct ifmultiaddr *ifma, int detaching)
3407 struct ifmultiaddr *ll_ifma;
3409 if (ifp != NULL && ifma->ifma_ifp != NULL) {
3410 KASSERT(ifma->ifma_ifp == ifp,
3411 ("%s: inconsistent ifp %p", __func__, ifp));
3412 IF_ADDR_WLOCK_ASSERT(ifp);
3415 ifp = ifma->ifma_ifp;
3418 * If the ifnet is detaching, null out references to ifnet,
3419 * so that upper protocol layers will notice, and not attempt
3420 * to obtain locks for an ifnet which no longer exists. The
3421 * routing socket announcement must happen before the ifnet
3422 * instance is detached from the system.
3426 printf("%s: detaching ifnet instance %p\n", __func__, ifp);
3429 * ifp may already be nulled out if we are being reentered
3430 * to delete the ll_ifma.
3433 rt_newmaddrmsg(RTM_DELMADDR, ifma);
3434 ifma->ifma_ifp = NULL;
3438 if (--ifma->ifma_refcount > 0)
3442 * If this ifma is a network-layer ifma, a link-layer ifma may
3443 * have been associated with it. Release it first if so.
3445 ll_ifma = ifma->ifma_llifma;
3446 if (ll_ifma != NULL) {
3447 KASSERT(ifma->ifma_lladdr != NULL,
3448 ("%s: llifma w/o lladdr", __func__));
3450 ll_ifma->ifma_ifp = NULL; /* XXX */
3451 if (--ll_ifma->ifma_refcount == 0) {
3453 TAILQ_REMOVE(&ifp->if_multiaddrs, ll_ifma,
3456 if_freemulti(ll_ifma);
3461 TAILQ_REMOVE(&ifp->if_multiaddrs, ifma, ifma_link);
3466 * The last reference to this instance of struct ifmultiaddr
3467 * was released; the hardware should be notified of this change.
3473 * Set the link layer address on an interface.
3475 * At this time we only support certain types of interfaces,
3476 * and we don't allow the length of the address to change.
3478 * Set noinline to be dtrace-friendly
3481 if_setlladdr(struct ifnet *ifp, const u_char *lladdr, int len)
3483 struct sockaddr_dl *sdl;
3490 IF_ADDR_RUNLOCK(ifp);
3494 IF_ADDR_RUNLOCK(ifp);
3495 sdl = (struct sockaddr_dl *)ifa->ifa_addr;
3500 if (len != sdl->sdl_alen) { /* don't allow length to change */
3504 switch (ifp->if_type) {
3512 case IFT_IEEE8023ADLAG:
3514 bcopy(lladdr, LLADDR(sdl), len);
3523 * If the interface is already up, we need
3524 * to re-init it in order to reprogram its
3527 if ((ifp->if_flags & IFF_UP) != 0) {
3528 if (ifp->if_ioctl) {
3529 ifp->if_flags &= ~IFF_UP;
3530 ifr.ifr_flags = ifp->if_flags & 0xffff;
3531 ifr.ifr_flagshigh = ifp->if_flags >> 16;
3532 (*ifp->if_ioctl)(ifp, SIOCSIFFLAGS, (caddr_t)&ifr);
3533 ifp->if_flags |= IFF_UP;
3534 ifr.ifr_flags = ifp->if_flags & 0xffff;
3535 ifr.ifr_flagshigh = ifp->if_flags >> 16;
3536 (*ifp->if_ioctl)(ifp, SIOCSIFFLAGS, (caddr_t)&ifr);
3539 EVENTHANDLER_INVOKE(iflladdr_event, ifp);
3544 * Compat function for handling basic encapsulation requests.
3545 * Not converted stacks (FDDI, IB, ..) supports traditional
3546 * output model: ARP (and other similar L2 protocols) are handled
3547 * inside output routine, arpresolve/nd6_resolve() returns MAC
3548 * address instead of full prepend.
3550 * This function creates calculated header==MAC for IPv4/IPv6 and
3551 * returns EAFNOSUPPORT (which is then handled in ARP code) for other
3555 if_requestencap_default(struct ifnet *ifp, struct if_encap_req *req)
3558 if (req->rtype != IFENCAP_LL)
3559 return (EOPNOTSUPP);
3561 if (req->bufsize < req->lladdr_len)
3564 switch (req->family) {
3569 return (EAFNOSUPPORT);
3572 /* Copy lladdr to storage as is */
3573 memmove(req->buf, req->lladdr, req->lladdr_len);
3574 req->bufsize = req->lladdr_len;
3575 req->lladdr_off = 0;
3581 * The name argument must be a pointer to storage which will last as
3582 * long as the interface does. For physical devices, the result of
3583 * device_get_name(dev) is a good choice and for pseudo-devices a
3584 * static string works well.
3587 if_initname(struct ifnet *ifp, const char *name, int unit)
3589 ifp->if_dname = name;
3590 ifp->if_dunit = unit;
3591 if (unit != IF_DUNIT_NONE)
3592 snprintf(ifp->if_xname, IFNAMSIZ, "%s%d", name, unit);
3594 strlcpy(ifp->if_xname, name, IFNAMSIZ);
3598 if_printf(struct ifnet *ifp, const char * fmt, ...)
3603 retval = printf("%s: ", ifp->if_xname);
3605 retval += vprintf(fmt, ap);
3611 if_start(struct ifnet *ifp)
3614 (*(ifp)->if_start)(ifp);
3618 * Backwards compatibility interface for drivers
3619 * that have not implemented it
3622 if_transmit(struct ifnet *ifp, struct mbuf *m)
3626 IFQ_HANDOFF(ifp, m, error);
3631 if_input_default(struct ifnet *ifp __unused, struct mbuf *m)
3638 if_handoff(struct ifqueue *ifq, struct mbuf *m, struct ifnet *ifp, int adjust)
3643 if (_IF_QFULL(ifq)) {
3645 if_inc_counter(ifp, IFCOUNTER_OQDROPS, 1);
3650 if_inc_counter(ifp, IFCOUNTER_OBYTES, m->m_pkthdr.len + adjust);
3651 if (m->m_flags & (M_BCAST|M_MCAST))
3652 if_inc_counter(ifp, IFCOUNTER_OMCASTS, 1);
3653 active = ifp->if_drv_flags & IFF_DRV_OACTIVE;
3655 _IF_ENQUEUE(ifq, m);
3657 if (ifp != NULL && !active)
3658 (*(ifp)->if_start)(ifp);
3663 if_register_com_alloc(u_char type,
3664 if_com_alloc_t *a, if_com_free_t *f)
3667 KASSERT(if_com_alloc[type] == NULL,
3668 ("if_register_com_alloc: %d already registered", type));
3669 KASSERT(if_com_free[type] == NULL,
3670 ("if_register_com_alloc: %d free already registered", type));
3672 if_com_alloc[type] = a;
3673 if_com_free[type] = f;
3677 if_deregister_com_alloc(u_char type)
3680 KASSERT(if_com_alloc[type] != NULL,
3681 ("if_deregister_com_alloc: %d not registered", type));
3682 KASSERT(if_com_free[type] != NULL,
3683 ("if_deregister_com_alloc: %d free not registered", type));
3684 if_com_alloc[type] = NULL;
3685 if_com_free[type] = NULL;
3688 /* API for driver access to network stack owned ifnet.*/
3690 if_setbaudrate(struct ifnet *ifp, uint64_t baudrate)
3694 oldbrate = ifp->if_baudrate;
3695 ifp->if_baudrate = baudrate;
3700 if_getbaudrate(if_t ifp)
3703 return (((struct ifnet *)ifp)->if_baudrate);
3707 if_setcapabilities(if_t ifp, int capabilities)
3709 ((struct ifnet *)ifp)->if_capabilities = capabilities;
3714 if_setcapabilitiesbit(if_t ifp, int setbit, int clearbit)
3716 ((struct ifnet *)ifp)->if_capabilities |= setbit;
3717 ((struct ifnet *)ifp)->if_capabilities &= ~clearbit;
3723 if_getcapabilities(if_t ifp)
3725 return ((struct ifnet *)ifp)->if_capabilities;
3729 if_setcapenable(if_t ifp, int capabilities)
3731 ((struct ifnet *)ifp)->if_capenable = capabilities;
3736 if_setcapenablebit(if_t ifp, int setcap, int clearcap)
3739 ((struct ifnet *)ifp)->if_capenable |= setcap;
3741 ((struct ifnet *)ifp)->if_capenable &= ~clearcap;
3747 if_getdname(if_t ifp)
3749 return ((struct ifnet *)ifp)->if_dname;
3753 if_togglecapenable(if_t ifp, int togglecap)
3755 ((struct ifnet *)ifp)->if_capenable ^= togglecap;
3760 if_getcapenable(if_t ifp)
3762 return ((struct ifnet *)ifp)->if_capenable;
3766 * This is largely undesirable because it ties ifnet to a device, but does
3767 * provide flexiblity for an embedded product vendor. Should be used with
3768 * the understanding that it violates the interface boundaries, and should be
3769 * a last resort only.
3772 if_setdev(if_t ifp, void *dev)
3778 if_setdrvflagbits(if_t ifp, int set_flags, int clear_flags)
3780 ((struct ifnet *)ifp)->if_drv_flags |= set_flags;
3781 ((struct ifnet *)ifp)->if_drv_flags &= ~clear_flags;
3787 if_getdrvflags(if_t ifp)
3789 return ((struct ifnet *)ifp)->if_drv_flags;
3793 if_setdrvflags(if_t ifp, int flags)
3795 ((struct ifnet *)ifp)->if_drv_flags = flags;
3801 if_setflags(if_t ifp, int flags)
3803 ((struct ifnet *)ifp)->if_flags = flags;
3808 if_setflagbits(if_t ifp, int set, int clear)
3810 ((struct ifnet *)ifp)->if_flags |= set;
3811 ((struct ifnet *)ifp)->if_flags &= ~clear;
3817 if_getflags(if_t ifp)
3819 return ((struct ifnet *)ifp)->if_flags;
3823 if_clearhwassist(if_t ifp)
3825 ((struct ifnet *)ifp)->if_hwassist = 0;
3830 if_sethwassistbits(if_t ifp, int toset, int toclear)
3832 ((struct ifnet *)ifp)->if_hwassist |= toset;
3833 ((struct ifnet *)ifp)->if_hwassist &= ~toclear;
3839 if_sethwassist(if_t ifp, int hwassist_bit)
3841 ((struct ifnet *)ifp)->if_hwassist = hwassist_bit;
3846 if_gethwassist(if_t ifp)
3848 return ((struct ifnet *)ifp)->if_hwassist;
3852 if_setmtu(if_t ifp, int mtu)
3854 ((struct ifnet *)ifp)->if_mtu = mtu;
3861 return ((struct ifnet *)ifp)->if_mtu;
3865 if_getmtu_family(if_t ifp, int family)
3869 for (dp = domains; dp; dp = dp->dom_next) {
3870 if (dp->dom_family == family && dp->dom_ifmtu != NULL)
3871 return (dp->dom_ifmtu((struct ifnet *)ifp));
3874 return (((struct ifnet *)ifp)->if_mtu);
3878 if_setsoftc(if_t ifp, void *softc)
3880 ((struct ifnet *)ifp)->if_softc = softc;
3885 if_getsoftc(if_t ifp)
3887 return ((struct ifnet *)ifp)->if_softc;
3891 if_setrcvif(struct mbuf *m, if_t ifp)
3893 m->m_pkthdr.rcvif = (struct ifnet *)ifp;
3897 if_setvtag(struct mbuf *m, uint16_t tag)
3899 m->m_pkthdr.ether_vtag = tag;
3903 if_getvtag(struct mbuf *m)
3906 return (m->m_pkthdr.ether_vtag);
3910 if_sendq_empty(if_t ifp)
3912 return IFQ_DRV_IS_EMPTY(&((struct ifnet *)ifp)->if_snd);
3916 if_getifaddr(if_t ifp)
3918 return ((struct ifnet *)ifp)->if_addr;
3922 if_getamcount(if_t ifp)
3924 return ((struct ifnet *)ifp)->if_amcount;
3929 if_setsendqready(if_t ifp)
3931 IFQ_SET_READY(&((struct ifnet *)ifp)->if_snd);
3936 if_setsendqlen(if_t ifp, int tx_desc_count)
3938 IFQ_SET_MAXLEN(&((struct ifnet *)ifp)->if_snd, tx_desc_count);
3939 ((struct ifnet *)ifp)->if_snd.ifq_drv_maxlen = tx_desc_count;
3945 if_vlantrunkinuse(if_t ifp)
3947 return ((struct ifnet *)ifp)->if_vlantrunk != NULL?1:0;
3951 if_input(if_t ifp, struct mbuf* sendmp)
3953 (*((struct ifnet *)ifp)->if_input)((struct ifnet *)ifp, sendmp);
3959 #ifndef ETH_ADDR_LEN
3960 #define ETH_ADDR_LEN 6
3964 if_setupmultiaddr(if_t ifp, void *mta, int *cnt, int max)
3966 struct ifmultiaddr *ifma;
3967 uint8_t *lmta = (uint8_t *)mta;
3970 TAILQ_FOREACH(ifma, &((struct ifnet *)ifp)->if_multiaddrs, ifma_link) {
3971 if (ifma->ifma_addr->sa_family != AF_LINK)
3977 bcopy(LLADDR((struct sockaddr_dl *)ifma->ifma_addr),
3978 &lmta[mcnt * ETH_ADDR_LEN], ETH_ADDR_LEN);
3987 if_multiaddr_array(if_t ifp, void *mta, int *cnt, int max)
3991 if_maddr_rlock(ifp);
3992 error = if_setupmultiaddr(ifp, mta, cnt, max);
3993 if_maddr_runlock(ifp);
3998 if_multiaddr_count(if_t ifp, int max)
4000 struct ifmultiaddr *ifma;
4004 if_maddr_rlock(ifp);
4005 TAILQ_FOREACH(ifma, &((struct ifnet *)ifp)->if_multiaddrs, ifma_link) {
4006 if (ifma->ifma_addr->sa_family != AF_LINK)
4012 if_maddr_runlock(ifp);
4017 if_multi_apply(struct ifnet *ifp, int (*filter)(void *, struct ifmultiaddr *, int), void *arg)
4019 struct ifmultiaddr *ifma;
4022 if_maddr_rlock(ifp);
4023 TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link)
4024 cnt += filter(arg, ifma, cnt);
4025 if_maddr_runlock(ifp);
4030 if_dequeue(if_t ifp)
4033 IFQ_DRV_DEQUEUE(&((struct ifnet *)ifp)->if_snd, m);
4039 if_sendq_prepend(if_t ifp, struct mbuf *m)
4041 IFQ_DRV_PREPEND(&((struct ifnet *)ifp)->if_snd, m);
4046 if_setifheaderlen(if_t ifp, int len)
4048 ((struct ifnet *)ifp)->if_hdrlen = len;
4053 if_getlladdr(if_t ifp)
4055 return (IF_LLADDR((struct ifnet *)ifp));
4059 if_gethandle(u_char type)
4061 return (if_alloc(type));
4065 if_bpfmtap(if_t ifh, struct mbuf *m)
4067 struct ifnet *ifp = (struct ifnet *)ifh;
4073 if_etherbpfmtap(if_t ifh, struct mbuf *m)
4075 struct ifnet *ifp = (struct ifnet *)ifh;
4077 ETHER_BPF_MTAP(ifp, m);
4081 if_vlancap(if_t ifh)
4083 struct ifnet *ifp = (struct ifnet *)ifh;
4084 VLAN_CAPABILITIES(ifp);
4088 if_setinitfn(if_t ifp, void (*init_fn)(void *))
4090 ((struct ifnet *)ifp)->if_init = init_fn;
4094 if_setioctlfn(if_t ifp, int (*ioctl_fn)(if_t, u_long, caddr_t))
4096 ((struct ifnet *)ifp)->if_ioctl = (void *)ioctl_fn;
4100 if_setstartfn(if_t ifp, void (*start_fn)(if_t))
4102 ((struct ifnet *)ifp)->if_start = (void *)start_fn;
4106 if_settransmitfn(if_t ifp, if_transmit_fn_t start_fn)
4108 ((struct ifnet *)ifp)->if_transmit = start_fn;
4111 void if_setqflushfn(if_t ifp, if_qflush_fn_t flush_fn)
4113 ((struct ifnet *)ifp)->if_qflush = flush_fn;
4118 if_setgetcounterfn(if_t ifp, if_get_counter_t fn)
4121 ifp->if_get_counter = fn;
4124 /* Revisit these - These are inline functions originally. */
4126 drbr_inuse_drv(if_t ifh, struct buf_ring *br)
4128 return drbr_inuse(ifh, br);
4132 drbr_dequeue_drv(if_t ifh, struct buf_ring *br)
4134 return drbr_dequeue(ifh, br);
4138 drbr_needs_enqueue_drv(if_t ifh, struct buf_ring *br)
4140 return drbr_needs_enqueue(ifh, br);
4144 drbr_enqueue_drv(if_t ifh, struct buf_ring *br, struct mbuf *m)
4146 return drbr_enqueue(ifh, br, m);