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 /* Interface description */
116 static unsigned int ifdescr_maxlen = 1024;
117 SYSCTL_UINT(_net, OID_AUTO, ifdescr_maxlen, CTLFLAG_RW,
119 "administrative maximum length for interface description");
121 static MALLOC_DEFINE(M_IFDESCR, "ifdescr", "ifnet descriptions");
123 /* global sx for non-critical path ifdescr */
124 static struct sx ifdescr_sx;
125 SX_SYSINIT(ifdescr_sx, &ifdescr_sx, "ifnet descr");
127 void (*bridge_linkstate_p)(struct ifnet *ifp);
128 void (*ng_ether_link_state_p)(struct ifnet *ifp, int state);
129 void (*lagg_linkstate_p)(struct ifnet *ifp, int state);
130 /* These are external hooks for CARP. */
131 void (*carp_linkstate_p)(struct ifnet *ifp);
132 void (*carp_demote_adj_p)(int, char *);
133 int (*carp_master_p)(struct ifaddr *);
134 #if defined(INET) || defined(INET6)
135 int (*carp_forus_p)(struct ifnet *ifp, u_char *dhost);
136 int (*carp_output_p)(struct ifnet *ifp, struct mbuf *m,
137 const struct sockaddr *sa);
138 int (*carp_ioctl_p)(struct ifreq *, u_long, struct thread *);
139 int (*carp_attach_p)(struct ifaddr *, int);
140 void (*carp_detach_p)(struct ifaddr *);
143 int (*carp_iamatch_p)(struct ifaddr *, uint8_t **);
146 struct ifaddr *(*carp_iamatch6_p)(struct ifnet *ifp, struct in6_addr *taddr6);
147 caddr_t (*carp_macmatch6_p)(struct ifnet *ifp, struct mbuf *m,
148 const struct in6_addr *taddr);
151 struct mbuf *(*tbr_dequeue_ptr)(struct ifaltq *, int) = NULL;
154 * XXX: Style; these should be sorted alphabetically, and unprototyped
155 * static functions should be prototyped. Currently they are sorted by
158 static void if_attachdomain(void *);
159 static void if_attachdomain1(struct ifnet *);
160 static int ifconf(u_long, caddr_t);
161 static void if_freemulti(struct ifmultiaddr *);
162 static void if_grow(void);
163 static void if_input_default(struct ifnet *, struct mbuf *);
164 static void if_route(struct ifnet *, int flag, int fam);
165 static int if_setflag(struct ifnet *, int, int, int *, int);
166 static int if_transmit(struct ifnet *ifp, struct mbuf *m);
167 static void if_unroute(struct ifnet *, int flag, int fam);
168 static void link_rtrequest(int, struct rtentry *, struct rt_addrinfo *);
169 static int ifhwioctl(u_long, struct ifnet *, caddr_t, struct thread *);
170 static int if_delmulti_locked(struct ifnet *, struct ifmultiaddr *, int);
171 static void do_link_state_change(void *, int);
172 static int if_getgroup(struct ifgroupreq *, struct ifnet *);
173 static int if_getgroupmembers(struct ifgroupreq *);
174 static void if_delgroups(struct ifnet *);
175 static void if_attach_internal(struct ifnet *, int, struct if_clone *);
176 static void if_detach_internal(struct ifnet *, int, struct if_clone **);
180 * XXX: declare here to avoid to include many inet6 related files..
181 * should be more generalized?
183 extern void nd6_setmtu(struct ifnet *);
186 VNET_DEFINE(int, if_index);
187 int ifqmaxlen = IFQ_MAXLEN;
188 VNET_DEFINE(struct ifnethead, ifnet); /* depend on static init XXX */
189 VNET_DEFINE(struct ifgrouphead, ifg_head);
191 static VNET_DEFINE(int, if_indexlim) = 8;
193 /* Table of ifnet by index. */
194 VNET_DEFINE(struct ifnet **, ifindex_table);
196 #define V_if_indexlim VNET(if_indexlim)
197 #define V_ifindex_table VNET(ifindex_table)
200 * The global network interface list (V_ifnet) and related state (such as
201 * if_index, if_indexlim, and ifindex_table) are protected by an sxlock and
202 * an rwlock. Either may be acquired shared to stablize the list, but both
203 * must be acquired writable to modify the list. This model allows us to
204 * both stablize the interface list during interrupt thread processing, but
205 * also to stablize it over long-running ioctls, without introducing priority
206 * inversions and deadlocks.
208 struct rwlock ifnet_rwlock;
209 RW_SYSINIT_FLAGS(ifnet_rw, &ifnet_rwlock, "ifnet_rw", RW_RECURSE);
210 struct sx ifnet_sxlock;
211 SX_SYSINIT_FLAGS(ifnet_sx, &ifnet_sxlock, "ifnet_sx", SX_RECURSE);
214 * The allocation of network interfaces is a rather non-atomic affair; we
215 * need to select an index before we are ready to expose the interface for
216 * use, so will use this pointer value to indicate reservation.
218 #define IFNET_HOLD (void *)(uintptr_t)(-1)
220 static if_com_alloc_t *if_com_alloc[256];
221 static if_com_free_t *if_com_free[256];
223 static MALLOC_DEFINE(M_IFNET, "ifnet", "interface internals");
224 MALLOC_DEFINE(M_IFADDR, "ifaddr", "interface address");
225 MALLOC_DEFINE(M_IFMADDR, "ether_multi", "link-level multicast address");
228 ifnet_byindex_locked(u_short idx)
231 if (idx > V_if_index)
233 if (V_ifindex_table[idx] == IFNET_HOLD)
235 return (V_ifindex_table[idx]);
239 ifnet_byindex(u_short idx)
243 IFNET_RLOCK_NOSLEEP();
244 ifp = ifnet_byindex_locked(idx);
245 IFNET_RUNLOCK_NOSLEEP();
250 ifnet_byindex_ref(u_short idx)
254 IFNET_RLOCK_NOSLEEP();
255 ifp = ifnet_byindex_locked(idx);
256 if (ifp == NULL || (ifp->if_flags & IFF_DYING)) {
257 IFNET_RUNLOCK_NOSLEEP();
261 IFNET_RUNLOCK_NOSLEEP();
266 * Allocate an ifindex array entry; return 0 on success or an error on
274 IFNET_WLOCK_ASSERT();
277 * Try to find an empty slot below V_if_index. If we fail, take the
280 for (idx = 1; idx <= V_if_index; idx++) {
281 if (V_ifindex_table[idx] == NULL)
285 /* Catch if_index overflow. */
286 if (idx >= V_if_indexlim) {
290 if (idx > V_if_index)
296 ifindex_free_locked(u_short idx)
299 IFNET_WLOCK_ASSERT();
301 V_ifindex_table[idx] = NULL;
302 while (V_if_index > 0 &&
303 V_ifindex_table[V_if_index] == NULL)
308 ifindex_free(u_short idx)
312 ifindex_free_locked(idx);
317 ifnet_setbyindex_locked(u_short idx, struct ifnet *ifp)
320 IFNET_WLOCK_ASSERT();
322 V_ifindex_table[idx] = ifp;
326 ifnet_setbyindex(u_short idx, struct ifnet *ifp)
330 ifnet_setbyindex_locked(idx, ifp);
335 ifaddr_byindex(u_short idx)
338 struct ifaddr *ifa = NULL;
340 IFNET_RLOCK_NOSLEEP();
341 ifp = ifnet_byindex_locked(idx);
342 if (ifp != NULL && (ifa = ifp->if_addr) != NULL)
344 IFNET_RUNLOCK_NOSLEEP();
349 * Network interface utility routines.
351 * Routines with ifa_ifwith* names take sockaddr *'s as
356 vnet_if_init(const void *unused __unused)
359 TAILQ_INIT(&V_ifnet);
360 TAILQ_INIT(&V_ifg_head);
362 if_grow(); /* create initial table */
364 vnet_if_clone_init();
366 VNET_SYSINIT(vnet_if_init, SI_SUB_INIT_IF, SI_ORDER_SECOND, vnet_if_init,
371 vnet_if_uninit(const void *unused __unused)
374 VNET_ASSERT(TAILQ_EMPTY(&V_ifnet), ("%s:%d tailq &V_ifnet=%p "
375 "not empty", __func__, __LINE__, &V_ifnet));
376 VNET_ASSERT(TAILQ_EMPTY(&V_ifg_head), ("%s:%d tailq &V_ifg_head=%p "
377 "not empty", __func__, __LINE__, &V_ifg_head));
379 free((caddr_t)V_ifindex_table, M_IFNET);
381 VNET_SYSUNINIT(vnet_if_uninit, SI_SUB_INIT_IF, SI_ORDER_FIRST,
382 vnet_if_uninit, NULL);
392 IFNET_WLOCK_ASSERT();
393 oldlim = V_if_indexlim;
395 n = (oldlim << 1) * sizeof(*e);
396 e = malloc(n, M_IFNET, M_WAITOK | M_ZERO);
398 if (V_if_indexlim != oldlim) {
402 if (V_ifindex_table != NULL) {
403 memcpy((caddr_t)e, (caddr_t)V_ifindex_table, n/2);
404 free((caddr_t)V_ifindex_table, M_IFNET);
411 * Allocate a struct ifnet and an index for an interface. A layer 2
412 * common structure will also be allocated if an allocation routine is
413 * registered for the passed type.
416 if_alloc(u_char type)
421 ifp = malloc(sizeof(struct ifnet), M_IFNET, M_WAITOK|M_ZERO);
423 idx = ifindex_alloc();
424 ifnet_setbyindex_locked(idx, IFNET_HOLD);
428 ifp->if_alloctype = type;
429 if (if_com_alloc[type] != NULL) {
430 ifp->if_l2com = if_com_alloc[type](type, ifp);
431 if (ifp->if_l2com == NULL) {
438 IF_ADDR_LOCK_INIT(ifp);
439 TASK_INIT(&ifp->if_linktask, 0, do_link_state_change, ifp);
440 ifp->if_afdata_initialized = 0;
441 IF_AFDATA_LOCK_INIT(ifp);
442 TAILQ_INIT(&ifp->if_addrhead);
443 TAILQ_INIT(&ifp->if_multiaddrs);
444 TAILQ_INIT(&ifp->if_groups);
448 ifq_init(&ifp->if_snd, ifp);
450 refcount_init(&ifp->if_refcount, 1); /* Index reference. */
451 for (int i = 0; i < IFCOUNTERS; i++)
452 ifp->if_counters[i] = counter_u64_alloc(M_WAITOK);
453 ifp->if_get_counter = if_get_counter_default;
454 ifnet_setbyindex(ifp->if_index, ifp);
459 * Do the actual work of freeing a struct ifnet, and layer 2 common
460 * structure. This call is made when the last reference to an
461 * interface is released.
464 if_free_internal(struct ifnet *ifp)
467 KASSERT((ifp->if_flags & IFF_DYING),
468 ("if_free_internal: interface not dying"));
470 if (if_com_free[ifp->if_alloctype] != NULL)
471 if_com_free[ifp->if_alloctype](ifp->if_l2com,
475 mac_ifnet_destroy(ifp);
477 if (ifp->if_description != NULL)
478 free(ifp->if_description, M_IFDESCR);
479 IF_AFDATA_DESTROY(ifp);
480 IF_ADDR_LOCK_DESTROY(ifp);
481 ifq_delete(&ifp->if_snd);
483 for (int i = 0; i < IFCOUNTERS; i++)
484 counter_u64_free(ifp->if_counters[i]);
490 * Deregister an interface and free the associated storage.
493 if_free(struct ifnet *ifp)
496 ifp->if_flags |= IFF_DYING; /* XXX: Locking */
498 CURVNET_SET_QUIET(ifp->if_vnet);
500 KASSERT(ifp == ifnet_byindex_locked(ifp->if_index),
501 ("%s: freeing unallocated ifnet", ifp->if_xname));
503 ifindex_free_locked(ifp->if_index);
506 if (refcount_release(&ifp->if_refcount))
507 if_free_internal(ifp);
512 * Interfaces to keep an ifnet type-stable despite the possibility of the
513 * driver calling if_free(). If there are additional references, we defer
514 * freeing the underlying data structure.
517 if_ref(struct ifnet *ifp)
520 /* We don't assert the ifnet list lock here, but arguably should. */
521 refcount_acquire(&ifp->if_refcount);
525 if_rele(struct ifnet *ifp)
528 if (!refcount_release(&ifp->if_refcount))
530 if_free_internal(ifp);
534 ifq_init(struct ifaltq *ifq, struct ifnet *ifp)
537 mtx_init(&ifq->ifq_mtx, ifp->if_xname, "if send queue", MTX_DEF);
539 if (ifq->ifq_maxlen == 0)
540 ifq->ifq_maxlen = ifqmaxlen;
543 ifq->altq_disc = NULL;
544 ifq->altq_flags &= ALTQF_CANTCHANGE;
545 ifq->altq_tbr = NULL;
550 ifq_delete(struct ifaltq *ifq)
552 mtx_destroy(&ifq->ifq_mtx);
556 * Perform generic interface initalization tasks and attach the interface
557 * to the list of "active" interfaces. If vmove flag is set on entry
558 * to if_attach_internal(), perform only a limited subset of initialization
559 * tasks, given that we are moving from one vnet to another an ifnet which
560 * has already been fully initialized.
562 * Note that if_detach_internal() removes group membership unconditionally
563 * even when vmove flag is set, and if_attach_internal() adds only IFG_ALL.
564 * Thus, when if_vmove() is applied to a cloned interface, group membership
565 * is lost while a cloned one always joins a group whose name is
566 * ifc->ifc_name. To recover this after if_detach_internal() and
567 * if_attach_internal(), the cloner should be specified to
568 * if_attach_internal() via ifc. If it is non-NULL, if_attach_internal()
569 * attempts to join a group whose name is ifc->ifc_name.
572 * - The decision to return void and thus require this function to
573 * succeed is questionable.
574 * - We should probably do more sanity checking. For instance we don't
575 * do anything to insure if_xname is unique or non-empty.
578 if_attach(struct ifnet *ifp)
581 if_attach_internal(ifp, 0, NULL);
585 * Compute the least common TSO limit.
588 if_hw_tsomax_common(if_t ifp, struct ifnet_hw_tsomax *pmax)
591 * 1) If there is no limit currently, take the limit from
592 * the network adapter.
594 * 2) If the network adapter has a limit below the current
597 if (pmax->tsomaxbytes == 0 || (ifp->if_hw_tsomax != 0 &&
598 ifp->if_hw_tsomax < pmax->tsomaxbytes)) {
599 pmax->tsomaxbytes = ifp->if_hw_tsomax;
601 if (pmax->tsomaxsegcount == 0 || (ifp->if_hw_tsomaxsegcount != 0 &&
602 ifp->if_hw_tsomaxsegcount < pmax->tsomaxsegcount)) {
603 pmax->tsomaxsegcount = ifp->if_hw_tsomaxsegcount;
605 if (pmax->tsomaxsegsize == 0 || (ifp->if_hw_tsomaxsegsize != 0 &&
606 ifp->if_hw_tsomaxsegsize < pmax->tsomaxsegsize)) {
607 pmax->tsomaxsegsize = ifp->if_hw_tsomaxsegsize;
612 * Update TSO limit of a network adapter.
614 * Returns zero if no change. Else non-zero.
617 if_hw_tsomax_update(if_t ifp, struct ifnet_hw_tsomax *pmax)
620 if (ifp->if_hw_tsomax != pmax->tsomaxbytes) {
621 ifp->if_hw_tsomax = pmax->tsomaxbytes;
624 if (ifp->if_hw_tsomaxsegsize != pmax->tsomaxsegsize) {
625 ifp->if_hw_tsomaxsegsize = pmax->tsomaxsegsize;
628 if (ifp->if_hw_tsomaxsegcount != pmax->tsomaxsegcount) {
629 ifp->if_hw_tsomaxsegcount = pmax->tsomaxsegcount;
636 if_attach_internal(struct ifnet *ifp, int vmove, struct if_clone *ifc)
638 unsigned socksize, ifasize;
639 int namelen, masklen;
640 struct sockaddr_dl *sdl;
643 if (ifp->if_index == 0 || ifp != ifnet_byindex(ifp->if_index))
644 panic ("%s: BUG: if_attach called without if_alloc'd input()\n",
648 ifp->if_vnet = curvnet;
649 if (ifp->if_home_vnet == NULL)
650 ifp->if_home_vnet = curvnet;
653 if_addgroup(ifp, IFG_ALL);
655 /* Restore group membership for cloned interfaces. */
656 if (vmove && ifc != NULL)
657 if_clone_addgroup(ifp, ifc);
659 getmicrotime(&ifp->if_lastchange);
660 ifp->if_epoch = time_uptime;
662 KASSERT((ifp->if_transmit == NULL && ifp->if_qflush == NULL) ||
663 (ifp->if_transmit != NULL && ifp->if_qflush != NULL),
664 ("transmit and qflush must both either be set or both be NULL"));
665 if (ifp->if_transmit == NULL) {
666 ifp->if_transmit = if_transmit;
667 ifp->if_qflush = if_qflush;
669 if (ifp->if_input == NULL)
670 ifp->if_input = if_input_default;
674 mac_ifnet_create(ifp);
678 * Create a Link Level name for this device.
680 namelen = strlen(ifp->if_xname);
682 * Always save enough space for any possiable name so we
683 * can do a rename in place later.
685 masklen = offsetof(struct sockaddr_dl, sdl_data[0]) + IFNAMSIZ;
686 socksize = masklen + ifp->if_addrlen;
687 if (socksize < sizeof(*sdl))
688 socksize = sizeof(*sdl);
689 socksize = roundup2(socksize, sizeof(long));
690 ifasize = sizeof(*ifa) + 2 * socksize;
691 ifa = ifa_alloc(ifasize, M_WAITOK);
692 sdl = (struct sockaddr_dl *)(ifa + 1);
693 sdl->sdl_len = socksize;
694 sdl->sdl_family = AF_LINK;
695 bcopy(ifp->if_xname, sdl->sdl_data, namelen);
696 sdl->sdl_nlen = namelen;
697 sdl->sdl_index = ifp->if_index;
698 sdl->sdl_type = ifp->if_type;
701 ifa->ifa_rtrequest = link_rtrequest;
702 ifa->ifa_addr = (struct sockaddr *)sdl;
703 sdl = (struct sockaddr_dl *)(socksize + (caddr_t)sdl);
704 ifa->ifa_netmask = (struct sockaddr *)sdl;
705 sdl->sdl_len = masklen;
707 sdl->sdl_data[--namelen] = 0xff;
708 TAILQ_INSERT_HEAD(&ifp->if_addrhead, ifa, ifa_link);
709 /* Reliably crash if used uninitialized. */
710 ifp->if_broadcastaddr = NULL;
712 #if defined(INET) || defined(INET6)
713 /* Use defaults for TSO, if nothing is set */
714 if (ifp->if_hw_tsomax == 0 &&
715 ifp->if_hw_tsomaxsegcount == 0 &&
716 ifp->if_hw_tsomaxsegsize == 0) {
718 * The TSO defaults needs to be such that an
719 * NFS mbuf list of 35 mbufs totalling just
720 * below 64K works and that a chain of mbufs
721 * can be defragged into at most 32 segments:
723 ifp->if_hw_tsomax = min(IP_MAXPACKET, (32 * MCLBYTES) -
724 (ETHER_HDR_LEN + ETHER_VLAN_ENCAP_LEN));
725 ifp->if_hw_tsomaxsegcount = 35;
726 ifp->if_hw_tsomaxsegsize = 2048; /* 2K */
728 /* XXX some drivers set IFCAP_TSO after ethernet attach */
729 if (ifp->if_capabilities & IFCAP_TSO) {
730 if_printf(ifp, "Using defaults for TSO: %u/%u/%u\n",
732 ifp->if_hw_tsomaxsegcount,
733 ifp->if_hw_tsomaxsegsize);
741 * Update the interface index in the link layer address
744 for (ifa = ifp->if_addr; ifa != NULL;
745 ifa = TAILQ_NEXT(ifa, ifa_link)) {
746 if (ifa->ifa_addr->sa_family == AF_LINK) {
747 sdl = (struct sockaddr_dl *)ifa->ifa_addr;
748 sdl->sdl_index = ifp->if_index;
755 TAILQ_INSERT_TAIL(&V_ifnet, ifp, if_link);
757 curvnet->vnet_ifcnt++;
761 if (domain_init_status >= 2)
762 if_attachdomain1(ifp);
764 EVENTHANDLER_INVOKE(ifnet_arrival_event, ifp);
765 if (IS_DEFAULT_VNET(curvnet))
766 devctl_notify("IFNET", ifp->if_xname, "ATTACH", NULL);
768 /* Announce the interface. */
769 rt_ifannouncemsg(ifp, IFAN_ARRIVAL);
773 if_attachdomain(void *dummy)
777 TAILQ_FOREACH(ifp, &V_ifnet, if_link)
778 if_attachdomain1(ifp);
780 SYSINIT(domainifattach, SI_SUB_PROTO_IFATTACHDOMAIN, SI_ORDER_SECOND,
781 if_attachdomain, NULL);
784 if_attachdomain1(struct ifnet *ifp)
789 * Since dp->dom_ifattach calls malloc() with M_WAITOK, we
790 * cannot lock ifp->if_afdata initialization, entirely.
792 if (IF_AFDATA_TRYLOCK(ifp) == 0)
794 if (ifp->if_afdata_initialized >= domain_init_status) {
795 IF_AFDATA_UNLOCK(ifp);
796 log(LOG_WARNING, "%s called more than once on %s\n",
797 __func__, ifp->if_xname);
800 ifp->if_afdata_initialized = domain_init_status;
801 IF_AFDATA_UNLOCK(ifp);
803 /* address family dependent data region */
804 bzero(ifp->if_afdata, sizeof(ifp->if_afdata));
805 for (dp = domains; dp; dp = dp->dom_next) {
806 if (dp->dom_ifattach)
807 ifp->if_afdata[dp->dom_family] =
808 (*dp->dom_ifattach)(ifp);
813 * Remove any unicast or broadcast network addresses from an interface.
816 if_purgeaddrs(struct ifnet *ifp)
818 struct ifaddr *ifa, *next;
820 TAILQ_FOREACH_SAFE(ifa, &ifp->if_addrhead, ifa_link, next) {
821 if (ifa->ifa_addr->sa_family == AF_LINK)
824 /* XXX: Ugly!! ad hoc just for INET */
825 if (ifa->ifa_addr->sa_family == AF_INET) {
826 struct ifaliasreq ifr;
828 bzero(&ifr, sizeof(ifr));
829 ifr.ifra_addr = *ifa->ifa_addr;
830 if (ifa->ifa_dstaddr)
831 ifr.ifra_broadaddr = *ifa->ifa_dstaddr;
832 if (in_control(NULL, SIOCDIFADDR, (caddr_t)&ifr, ifp,
838 if (ifa->ifa_addr->sa_family == AF_INET6) {
840 /* ifp_addrhead is already updated */
844 TAILQ_REMOVE(&ifp->if_addrhead, ifa, ifa_link);
850 * Remove any multicast network addresses from an interface when an ifnet
854 if_purgemaddrs(struct ifnet *ifp)
856 struct ifmultiaddr *ifma;
857 struct ifmultiaddr *next;
860 TAILQ_FOREACH_SAFE(ifma, &ifp->if_multiaddrs, ifma_link, next)
861 if_delmulti_locked(ifp, ifma, 1);
862 IF_ADDR_WUNLOCK(ifp);
866 * Detach an interface, removing it from the list of "active" interfaces.
867 * If vmove flag is set on entry to if_detach_internal(), perform only a
868 * limited subset of cleanup tasks, given that we are moving an ifnet from
869 * one vnet to another, where it must be fully operational.
871 * XXXRW: There are some significant questions about event ordering, and
872 * how to prevent things from starting to use the interface during detach.
875 if_detach(struct ifnet *ifp)
878 CURVNET_SET_QUIET(ifp->if_vnet);
879 if_detach_internal(ifp, 0, NULL);
884 if_detach_internal(struct ifnet *ifp, int vmove, struct if_clone **ifcp)
893 TAILQ_FOREACH(iter, &V_ifnet, if_link)
895 TAILQ_REMOVE(&V_ifnet, ifp, if_link);
901 curvnet->vnet_ifcnt--;
906 panic("%s: ifp=%p not on the ifnet tailq %p",
907 __func__, ifp, &V_ifnet);
909 return; /* XXX this should panic as well? */
912 /* Check if this is a cloned interface or not. */
913 if (vmove && ifcp != NULL)
914 *ifcp = if_clone_findifc(ifp);
917 * Remove/wait for pending events.
919 taskqueue_drain(taskqueue_swi, &ifp->if_linktask);
922 * Remove routes and flush queues.
926 if (ALTQ_IS_ENABLED(&ifp->if_snd))
927 altq_disable(&ifp->if_snd);
928 if (ALTQ_IS_ATTACHED(&ifp->if_snd))
929 altq_detach(&ifp->if_snd);
940 * Remove all IPv6 kernel structs related to ifp. This should be done
941 * before removing routing entries below, since IPv6 interface direct
942 * routes are expected to be removed by the IPv6-specific kernel API.
943 * Otherwise, the kernel will detect some inconsistency and bark it.
949 /* Announce that the interface is gone. */
950 rt_ifannouncemsg(ifp, IFAN_DEPARTURE);
951 EVENTHANDLER_INVOKE(ifnet_departure_event, ifp);
952 if (IS_DEFAULT_VNET(curvnet))
953 devctl_notify("IFNET", ifp->if_xname, "DETACH", NULL);
957 * Prevent further calls into the device driver via ifnet.
962 * Remove link ifaddr pointer and maybe decrement if_index.
963 * Clean up all addresses.
967 /* We can now free link ifaddr. */
968 if (!TAILQ_EMPTY(&ifp->if_addrhead)) {
969 ifa = TAILQ_FIRST(&ifp->if_addrhead);
970 TAILQ_REMOVE(&ifp->if_addrhead, ifa, ifa_link);
975 rt_flushifroutes(ifp);
979 * We cannot hold the lock over dom_ifdetach calls as they might
980 * sleep, for example trying to drain a callout, thus open up the
981 * theoretical race with re-attaching.
984 i = ifp->if_afdata_initialized;
985 ifp->if_afdata_initialized = 0;
986 IF_AFDATA_UNLOCK(ifp);
987 for (dp = domains; i > 0 && dp; dp = dp->dom_next) {
988 if (dp->dom_ifdetach && ifp->if_afdata[dp->dom_family])
989 (*dp->dom_ifdetach)(ifp,
990 ifp->if_afdata[dp->dom_family]);
996 * if_vmove() performs a limited version of if_detach() in current
997 * vnet and if_attach()es the ifnet to the vnet specified as 2nd arg.
998 * An attempt is made to shrink if_index in current vnet, find an
999 * unused if_index in target vnet and calls if_grow() if necessary,
1000 * and finally find an unused if_xname for the target vnet.
1003 if_vmove(struct ifnet *ifp, struct vnet *new_vnet)
1005 struct if_clone *ifc;
1008 * Detach from current vnet, but preserve LLADDR info, do not
1009 * mark as dead etc. so that the ifnet can be reattached later.
1011 if_detach_internal(ifp, 1, &ifc);
1014 * Unlink the ifnet from ifindex_table[] in current vnet, and shrink
1015 * the if_index for that vnet if possible.
1017 * NOTE: IFNET_WLOCK/IFNET_WUNLOCK() are assumed to be unvirtualized,
1018 * or we'd lock on one vnet and unlock on another.
1021 ifindex_free_locked(ifp->if_index);
1025 * Perform interface-specific reassignment tasks, if provided by
1028 if (ifp->if_reassign != NULL)
1029 ifp->if_reassign(ifp, new_vnet, NULL);
1032 * Switch to the context of the target vnet.
1034 CURVNET_SET_QUIET(new_vnet);
1037 ifp->if_index = ifindex_alloc();
1038 ifnet_setbyindex_locked(ifp->if_index, ifp);
1041 if_attach_internal(ifp, 1, ifc);
1047 * Move an ifnet to or from another child prison/vnet, specified by the jail id.
1050 if_vmove_loan(struct thread *td, struct ifnet *ifp, char *ifname, int jid)
1055 /* Try to find the prison within our visibility. */
1056 sx_slock(&allprison_lock);
1057 pr = prison_find_child(td->td_ucred->cr_prison, jid);
1058 sx_sunlock(&allprison_lock);
1061 prison_hold_locked(pr);
1062 mtx_unlock(&pr->pr_mtx);
1064 /* Do not try to move the iface from and to the same prison. */
1065 if (pr->pr_vnet == ifp->if_vnet) {
1070 /* Make sure the named iface does not exists in the dst. prison/vnet. */
1071 /* XXX Lock interfaces to avoid races. */
1072 CURVNET_SET_QUIET(pr->pr_vnet);
1073 difp = ifunit(ifname);
1080 /* Move the interface into the child jail/vnet. */
1081 if_vmove(ifp, pr->pr_vnet);
1083 /* Report the new if_xname back to the userland. */
1084 sprintf(ifname, "%s", ifp->if_xname);
1091 if_vmove_reclaim(struct thread *td, char *ifname, int jid)
1094 struct vnet *vnet_dst;
1097 /* Try to find the prison within our visibility. */
1098 sx_slock(&allprison_lock);
1099 pr = prison_find_child(td->td_ucred->cr_prison, jid);
1100 sx_sunlock(&allprison_lock);
1103 prison_hold_locked(pr);
1104 mtx_unlock(&pr->pr_mtx);
1106 /* Make sure the named iface exists in the source prison/vnet. */
1107 CURVNET_SET(pr->pr_vnet);
1108 ifp = ifunit(ifname); /* XXX Lock to avoid races. */
1115 /* Do not try to move the iface from and to the same prison. */
1116 vnet_dst = TD_TO_VNET(td);
1117 if (vnet_dst == ifp->if_vnet) {
1123 /* Get interface back from child jail/vnet. */
1124 if_vmove(ifp, vnet_dst);
1127 /* Report the new if_xname back to the userland. */
1128 sprintf(ifname, "%s", ifp->if_xname);
1136 * Add a group to an interface
1139 if_addgroup(struct ifnet *ifp, const char *groupname)
1141 struct ifg_list *ifgl;
1142 struct ifg_group *ifg = NULL;
1143 struct ifg_member *ifgm;
1146 if (groupname[0] && groupname[strlen(groupname) - 1] >= '0' &&
1147 groupname[strlen(groupname) - 1] <= '9')
1151 TAILQ_FOREACH(ifgl, &ifp->if_groups, ifgl_next)
1152 if (!strcmp(ifgl->ifgl_group->ifg_group, groupname)) {
1157 if ((ifgl = (struct ifg_list *)malloc(sizeof(struct ifg_list), M_TEMP,
1158 M_NOWAIT)) == NULL) {
1163 if ((ifgm = (struct ifg_member *)malloc(sizeof(struct ifg_member),
1164 M_TEMP, M_NOWAIT)) == NULL) {
1170 TAILQ_FOREACH(ifg, &V_ifg_head, ifg_next)
1171 if (!strcmp(ifg->ifg_group, groupname))
1175 if ((ifg = (struct ifg_group *)malloc(sizeof(struct ifg_group),
1176 M_TEMP, M_NOWAIT)) == NULL) {
1182 strlcpy(ifg->ifg_group, groupname, sizeof(ifg->ifg_group));
1183 ifg->ifg_refcnt = 0;
1184 TAILQ_INIT(&ifg->ifg_members);
1185 TAILQ_INSERT_TAIL(&V_ifg_head, ifg, ifg_next);
1190 ifgl->ifgl_group = ifg;
1191 ifgm->ifgm_ifp = ifp;
1194 TAILQ_INSERT_TAIL(&ifg->ifg_members, ifgm, ifgm_next);
1195 TAILQ_INSERT_TAIL(&ifp->if_groups, ifgl, ifgl_next);
1196 IF_ADDR_WUNLOCK(ifp);
1201 EVENTHANDLER_INVOKE(group_attach_event, ifg);
1202 EVENTHANDLER_INVOKE(group_change_event, groupname);
1208 * Remove a group from an interface
1211 if_delgroup(struct ifnet *ifp, const char *groupname)
1213 struct ifg_list *ifgl;
1214 struct ifg_member *ifgm;
1217 TAILQ_FOREACH(ifgl, &ifp->if_groups, ifgl_next)
1218 if (!strcmp(ifgl->ifgl_group->ifg_group, groupname))
1226 TAILQ_REMOVE(&ifp->if_groups, ifgl, ifgl_next);
1227 IF_ADDR_WUNLOCK(ifp);
1229 TAILQ_FOREACH(ifgm, &ifgl->ifgl_group->ifg_members, ifgm_next)
1230 if (ifgm->ifgm_ifp == ifp)
1234 TAILQ_REMOVE(&ifgl->ifgl_group->ifg_members, ifgm, ifgm_next);
1238 if (--ifgl->ifgl_group->ifg_refcnt == 0) {
1239 TAILQ_REMOVE(&V_ifg_head, ifgl->ifgl_group, ifg_next);
1241 EVENTHANDLER_INVOKE(group_detach_event, ifgl->ifgl_group);
1242 free(ifgl->ifgl_group, M_TEMP);
1248 EVENTHANDLER_INVOKE(group_change_event, groupname);
1254 * Remove an interface from all groups
1257 if_delgroups(struct ifnet *ifp)
1259 struct ifg_list *ifgl;
1260 struct ifg_member *ifgm;
1261 char groupname[IFNAMSIZ];
1264 while (!TAILQ_EMPTY(&ifp->if_groups)) {
1265 ifgl = TAILQ_FIRST(&ifp->if_groups);
1267 strlcpy(groupname, ifgl->ifgl_group->ifg_group, IFNAMSIZ);
1270 TAILQ_REMOVE(&ifp->if_groups, ifgl, ifgl_next);
1271 IF_ADDR_WUNLOCK(ifp);
1273 TAILQ_FOREACH(ifgm, &ifgl->ifgl_group->ifg_members, ifgm_next)
1274 if (ifgm->ifgm_ifp == ifp)
1278 TAILQ_REMOVE(&ifgl->ifgl_group->ifg_members, ifgm,
1283 if (--ifgl->ifgl_group->ifg_refcnt == 0) {
1284 TAILQ_REMOVE(&V_ifg_head, ifgl->ifgl_group, ifg_next);
1286 EVENTHANDLER_INVOKE(group_detach_event,
1288 free(ifgl->ifgl_group, M_TEMP);
1294 EVENTHANDLER_INVOKE(group_change_event, groupname);
1302 * Stores all groups from an interface in memory pointed
1306 if_getgroup(struct ifgroupreq *data, struct ifnet *ifp)
1309 struct ifg_list *ifgl;
1310 struct ifg_req ifgrq, *ifgp;
1311 struct ifgroupreq *ifgr = data;
1313 if (ifgr->ifgr_len == 0) {
1315 TAILQ_FOREACH(ifgl, &ifp->if_groups, ifgl_next)
1316 ifgr->ifgr_len += sizeof(struct ifg_req);
1317 IF_ADDR_RUNLOCK(ifp);
1321 len = ifgr->ifgr_len;
1322 ifgp = ifgr->ifgr_groups;
1325 TAILQ_FOREACH(ifgl, &ifp->if_groups, ifgl_next) {
1326 if (len < sizeof(ifgrq)) {
1327 IF_ADDR_RUNLOCK(ifp);
1330 bzero(&ifgrq, sizeof ifgrq);
1331 strlcpy(ifgrq.ifgrq_group, ifgl->ifgl_group->ifg_group,
1332 sizeof(ifgrq.ifgrq_group));
1333 if ((error = copyout(&ifgrq, ifgp, sizeof(struct ifg_req)))) {
1334 IF_ADDR_RUNLOCK(ifp);
1337 len -= sizeof(ifgrq);
1340 IF_ADDR_RUNLOCK(ifp);
1346 * Stores all members of a group in memory pointed to by data
1349 if_getgroupmembers(struct ifgroupreq *data)
1351 struct ifgroupreq *ifgr = data;
1352 struct ifg_group *ifg;
1353 struct ifg_member *ifgm;
1354 struct ifg_req ifgrq, *ifgp;
1358 TAILQ_FOREACH(ifg, &V_ifg_head, ifg_next)
1359 if (!strcmp(ifg->ifg_group, ifgr->ifgr_name))
1366 if (ifgr->ifgr_len == 0) {
1367 TAILQ_FOREACH(ifgm, &ifg->ifg_members, ifgm_next)
1368 ifgr->ifgr_len += sizeof(ifgrq);
1373 len = ifgr->ifgr_len;
1374 ifgp = ifgr->ifgr_groups;
1375 TAILQ_FOREACH(ifgm, &ifg->ifg_members, ifgm_next) {
1376 if (len < sizeof(ifgrq)) {
1380 bzero(&ifgrq, sizeof ifgrq);
1381 strlcpy(ifgrq.ifgrq_member, ifgm->ifgm_ifp->if_xname,
1382 sizeof(ifgrq.ifgrq_member));
1383 if ((error = copyout(&ifgrq, ifgp, sizeof(struct ifg_req)))) {
1387 len -= sizeof(ifgrq);
1396 * Return counter values from counter(9)s stored in ifnet.
1399 if_get_counter_default(struct ifnet *ifp, ift_counter cnt)
1402 KASSERT(cnt < IFCOUNTERS, ("%s: invalid cnt %d", __func__, cnt));
1404 return (counter_u64_fetch(ifp->if_counters[cnt]));
1408 * Increase an ifnet counter. Usually used for counters shared
1409 * between the stack and a driver, but function supports them all.
1412 if_inc_counter(struct ifnet *ifp, ift_counter cnt, int64_t inc)
1415 KASSERT(cnt < IFCOUNTERS, ("%s: invalid cnt %d", __func__, cnt));
1417 counter_u64_add(ifp->if_counters[cnt], inc);
1421 * Copy data from ifnet to userland API structure if_data.
1424 if_data_copy(struct ifnet *ifp, struct if_data *ifd)
1427 ifd->ifi_type = ifp->if_type;
1428 ifd->ifi_physical = 0;
1429 ifd->ifi_addrlen = ifp->if_addrlen;
1430 ifd->ifi_hdrlen = ifp->if_hdrlen;
1431 ifd->ifi_link_state = ifp->if_link_state;
1433 ifd->ifi_datalen = sizeof(struct if_data);
1434 ifd->ifi_mtu = ifp->if_mtu;
1435 ifd->ifi_metric = ifp->if_metric;
1436 ifd->ifi_baudrate = ifp->if_baudrate;
1437 ifd->ifi_hwassist = ifp->if_hwassist;
1438 ifd->ifi_epoch = ifp->if_epoch;
1439 ifd->ifi_lastchange = ifp->if_lastchange;
1441 ifd->ifi_ipackets = ifp->if_get_counter(ifp, IFCOUNTER_IPACKETS);
1442 ifd->ifi_ierrors = ifp->if_get_counter(ifp, IFCOUNTER_IERRORS);
1443 ifd->ifi_opackets = ifp->if_get_counter(ifp, IFCOUNTER_OPACKETS);
1444 ifd->ifi_oerrors = ifp->if_get_counter(ifp, IFCOUNTER_OERRORS);
1445 ifd->ifi_collisions = ifp->if_get_counter(ifp, IFCOUNTER_COLLISIONS);
1446 ifd->ifi_ibytes = ifp->if_get_counter(ifp, IFCOUNTER_IBYTES);
1447 ifd->ifi_obytes = ifp->if_get_counter(ifp, IFCOUNTER_OBYTES);
1448 ifd->ifi_imcasts = ifp->if_get_counter(ifp, IFCOUNTER_IMCASTS);
1449 ifd->ifi_omcasts = ifp->if_get_counter(ifp, IFCOUNTER_OMCASTS);
1450 ifd->ifi_iqdrops = ifp->if_get_counter(ifp, IFCOUNTER_IQDROPS);
1451 ifd->ifi_oqdrops = ifp->if_get_counter(ifp, IFCOUNTER_OQDROPS);
1452 ifd->ifi_noproto = ifp->if_get_counter(ifp, IFCOUNTER_NOPROTO);
1456 * Wrapper functions for struct ifnet address list locking macros. These are
1457 * used by kernel modules to avoid encoding programming interface or binary
1458 * interface assumptions that may be violated when kernel-internal locking
1459 * approaches change.
1462 if_addr_rlock(struct ifnet *ifp)
1469 if_addr_runlock(struct ifnet *ifp)
1472 IF_ADDR_RUNLOCK(ifp);
1476 if_maddr_rlock(if_t ifp)
1479 IF_ADDR_RLOCK((struct ifnet *)ifp);
1483 if_maddr_runlock(if_t ifp)
1486 IF_ADDR_RUNLOCK((struct ifnet *)ifp);
1490 * Initialization, destruction and refcounting functions for ifaddrs.
1493 ifa_alloc(size_t size, int flags)
1497 KASSERT(size >= sizeof(struct ifaddr),
1498 ("%s: invalid size %zu", __func__, size));
1500 ifa = malloc(size, M_IFADDR, M_ZERO | flags);
1504 if ((ifa->ifa_opackets = counter_u64_alloc(flags)) == NULL)
1506 if ((ifa->ifa_ipackets = counter_u64_alloc(flags)) == NULL)
1508 if ((ifa->ifa_obytes = counter_u64_alloc(flags)) == NULL)
1510 if ((ifa->ifa_ibytes = counter_u64_alloc(flags)) == NULL)
1513 refcount_init(&ifa->ifa_refcnt, 1);
1518 /* free(NULL) is okay */
1519 counter_u64_free(ifa->ifa_opackets);
1520 counter_u64_free(ifa->ifa_ipackets);
1521 counter_u64_free(ifa->ifa_obytes);
1522 counter_u64_free(ifa->ifa_ibytes);
1523 free(ifa, M_IFADDR);
1529 ifa_ref(struct ifaddr *ifa)
1532 refcount_acquire(&ifa->ifa_refcnt);
1536 ifa_free(struct ifaddr *ifa)
1539 if (refcount_release(&ifa->ifa_refcnt)) {
1540 counter_u64_free(ifa->ifa_opackets);
1541 counter_u64_free(ifa->ifa_ipackets);
1542 counter_u64_free(ifa->ifa_obytes);
1543 counter_u64_free(ifa->ifa_ibytes);
1544 free(ifa, M_IFADDR);
1549 ifa_add_loopback_route(struct ifaddr *ifa, struct sockaddr *ia)
1552 struct rtentry *rt = NULL;
1553 struct rt_addrinfo info;
1554 static struct sockaddr_dl null_sdl = {sizeof(null_sdl), AF_LINK};
1556 bzero(&info, sizeof(info));
1557 info.rti_ifp = V_loif;
1558 info.rti_flags = ifa->ifa_flags | RTF_HOST | RTF_STATIC;
1559 info.rti_info[RTAX_DST] = ia;
1560 info.rti_info[RTAX_GATEWAY] = (struct sockaddr *)&null_sdl;
1561 error = rtrequest1_fib(RTM_ADD, &info, &rt, ifa->ifa_ifp->if_fib);
1563 if (error == 0 && rt != NULL) {
1565 ((struct sockaddr_dl *)rt->rt_gateway)->sdl_type =
1566 ifa->ifa_ifp->if_type;
1567 ((struct sockaddr_dl *)rt->rt_gateway)->sdl_index =
1568 ifa->ifa_ifp->if_index;
1571 } else if (error != 0)
1572 log(LOG_DEBUG, "%s: insertion failed: %u\n", __func__, error);
1578 ifa_del_loopback_route(struct ifaddr *ifa, struct sockaddr *ia)
1581 struct rt_addrinfo info;
1582 struct sockaddr_dl null_sdl;
1584 bzero(&null_sdl, sizeof(null_sdl));
1585 null_sdl.sdl_len = sizeof(null_sdl);
1586 null_sdl.sdl_family = AF_LINK;
1587 null_sdl.sdl_type = ifa->ifa_ifp->if_type;
1588 null_sdl.sdl_index = ifa->ifa_ifp->if_index;
1589 bzero(&info, sizeof(info));
1590 info.rti_flags = ifa->ifa_flags | RTF_HOST | RTF_STATIC;
1591 info.rti_info[RTAX_DST] = ia;
1592 info.rti_info[RTAX_GATEWAY] = (struct sockaddr *)&null_sdl;
1593 error = rtrequest1_fib(RTM_DELETE, &info, NULL, ifa->ifa_ifp->if_fib);
1596 log(LOG_DEBUG, "%s: deletion failed: %u\n", __func__, error);
1602 ifa_switch_loopback_route(struct ifaddr *ifa, struct sockaddr *sa, int fib)
1606 rt = rtalloc1_fib(sa, 0, 0, fib);
1608 log(LOG_DEBUG, "%s: fail", __func__);
1609 return (EHOSTUNREACH);
1611 ((struct sockaddr_dl *)rt->rt_gateway)->sdl_type =
1612 ifa->ifa_ifp->if_type;
1613 ((struct sockaddr_dl *)rt->rt_gateway)->sdl_index =
1614 ifa->ifa_ifp->if_index;
1621 * XXX: Because sockaddr_dl has deeper structure than the sockaddr
1622 * structs used to represent other address families, it is necessary
1623 * to perform a different comparison.
1626 #define sa_dl_equal(a1, a2) \
1627 ((((const struct sockaddr_dl *)(a1))->sdl_len == \
1628 ((const struct sockaddr_dl *)(a2))->sdl_len) && \
1629 (bcmp(CLLADDR((const struct sockaddr_dl *)(a1)), \
1630 CLLADDR((const struct sockaddr_dl *)(a2)), \
1631 ((const struct sockaddr_dl *)(a1))->sdl_alen) == 0))
1634 * Locate an interface based on a complete address.
1637 static struct ifaddr *
1638 ifa_ifwithaddr_internal(const struct sockaddr *addr, int getref)
1643 IFNET_RLOCK_NOSLEEP();
1644 TAILQ_FOREACH(ifp, &V_ifnet, if_link) {
1646 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
1647 if (ifa->ifa_addr->sa_family != addr->sa_family)
1649 if (sa_equal(addr, ifa->ifa_addr)) {
1652 IF_ADDR_RUNLOCK(ifp);
1655 /* IP6 doesn't have broadcast */
1656 if ((ifp->if_flags & IFF_BROADCAST) &&
1657 ifa->ifa_broadaddr &&
1658 ifa->ifa_broadaddr->sa_len != 0 &&
1659 sa_equal(ifa->ifa_broadaddr, addr)) {
1662 IF_ADDR_RUNLOCK(ifp);
1666 IF_ADDR_RUNLOCK(ifp);
1670 IFNET_RUNLOCK_NOSLEEP();
1675 ifa_ifwithaddr(const struct sockaddr *addr)
1678 return (ifa_ifwithaddr_internal(addr, 1));
1682 ifa_ifwithaddr_check(const struct sockaddr *addr)
1685 return (ifa_ifwithaddr_internal(addr, 0) != NULL);
1689 * Locate an interface based on the broadcast address.
1693 ifa_ifwithbroadaddr(const struct sockaddr *addr, int fibnum)
1698 IFNET_RLOCK_NOSLEEP();
1699 TAILQ_FOREACH(ifp, &V_ifnet, if_link) {
1700 if ((fibnum != RT_ALL_FIBS) && (ifp->if_fib != fibnum))
1703 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
1704 if (ifa->ifa_addr->sa_family != addr->sa_family)
1706 if ((ifp->if_flags & IFF_BROADCAST) &&
1707 ifa->ifa_broadaddr &&
1708 ifa->ifa_broadaddr->sa_len != 0 &&
1709 sa_equal(ifa->ifa_broadaddr, addr)) {
1711 IF_ADDR_RUNLOCK(ifp);
1715 IF_ADDR_RUNLOCK(ifp);
1719 IFNET_RUNLOCK_NOSLEEP();
1724 * Locate the point to point interface with a given destination address.
1728 ifa_ifwithdstaddr(const struct sockaddr *addr, int fibnum)
1733 IFNET_RLOCK_NOSLEEP();
1734 TAILQ_FOREACH(ifp, &V_ifnet, if_link) {
1735 if ((ifp->if_flags & IFF_POINTOPOINT) == 0)
1737 if ((fibnum != RT_ALL_FIBS) && (ifp->if_fib != fibnum))
1740 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
1741 if (ifa->ifa_addr->sa_family != addr->sa_family)
1743 if (ifa->ifa_dstaddr != NULL &&
1744 sa_equal(addr, ifa->ifa_dstaddr)) {
1746 IF_ADDR_RUNLOCK(ifp);
1750 IF_ADDR_RUNLOCK(ifp);
1754 IFNET_RUNLOCK_NOSLEEP();
1759 * Find an interface on a specific network. If many, choice
1760 * is most specific found.
1763 ifa_ifwithnet(const struct sockaddr *addr, int ignore_ptp, int fibnum)
1767 struct ifaddr *ifa_maybe = NULL;
1768 u_int af = addr->sa_family;
1769 const char *addr_data = addr->sa_data, *cplim;
1772 * AF_LINK addresses can be looked up directly by their index number,
1773 * so do that if we can.
1775 if (af == AF_LINK) {
1776 const struct sockaddr_dl *sdl = (const struct sockaddr_dl *)addr;
1777 if (sdl->sdl_index && sdl->sdl_index <= V_if_index)
1778 return (ifaddr_byindex(sdl->sdl_index));
1782 * Scan though each interface, looking for ones that have addresses
1783 * in this address family and the requested fib. Maintain a reference
1784 * on ifa_maybe once we find one, as we release the IF_ADDR_RLOCK() that
1785 * kept it stable when we move onto the next interface.
1787 IFNET_RLOCK_NOSLEEP();
1788 TAILQ_FOREACH(ifp, &V_ifnet, if_link) {
1789 if ((fibnum != RT_ALL_FIBS) && (ifp->if_fib != fibnum))
1792 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
1793 const char *cp, *cp2, *cp3;
1795 if (ifa->ifa_addr->sa_family != af)
1797 if (af == AF_INET &&
1798 ifp->if_flags & IFF_POINTOPOINT && !ignore_ptp) {
1800 * This is a bit broken as it doesn't
1801 * take into account that the remote end may
1802 * be a single node in the network we are
1804 * The trouble is that we don't know the
1805 * netmask for the remote end.
1807 if (ifa->ifa_dstaddr != NULL &&
1808 sa_equal(addr, ifa->ifa_dstaddr)) {
1810 IF_ADDR_RUNLOCK(ifp);
1815 * Scan all the bits in the ifa's address.
1816 * If a bit dissagrees with what we are
1817 * looking for, mask it with the netmask
1818 * to see if it really matters.
1819 * (A byte at a time)
1821 if (ifa->ifa_netmask == 0)
1824 cp2 = ifa->ifa_addr->sa_data;
1825 cp3 = ifa->ifa_netmask->sa_data;
1826 cplim = ifa->ifa_netmask->sa_len
1827 + (char *)ifa->ifa_netmask;
1829 if ((*cp++ ^ *cp2++) & *cp3++)
1830 goto next; /* next address! */
1832 * If the netmask of what we just found
1833 * is more specific than what we had before
1834 * (if we had one), or if the virtual status
1835 * of new prefix is better than of the old one,
1836 * then remember the new one before continuing
1837 * to search for an even better one.
1839 if (ifa_maybe == NULL ||
1840 ifa_preferred(ifa_maybe, ifa) ||
1841 rn_refines((caddr_t)ifa->ifa_netmask,
1842 (caddr_t)ifa_maybe->ifa_netmask)) {
1843 if (ifa_maybe != NULL)
1844 ifa_free(ifa_maybe);
1850 IF_ADDR_RUNLOCK(ifp);
1855 IFNET_RUNLOCK_NOSLEEP();
1856 if (ifa_maybe != NULL)
1857 ifa_free(ifa_maybe);
1862 * Find an interface address specific to an interface best matching
1866 ifaof_ifpforaddr(const struct sockaddr *addr, struct ifnet *ifp)
1869 const char *cp, *cp2, *cp3;
1871 struct ifaddr *ifa_maybe = NULL;
1872 u_int af = addr->sa_family;
1877 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
1878 if (ifa->ifa_addr->sa_family != af)
1880 if (ifa_maybe == NULL)
1882 if (ifa->ifa_netmask == 0) {
1883 if (sa_equal(addr, ifa->ifa_addr) ||
1884 (ifa->ifa_dstaddr &&
1885 sa_equal(addr, ifa->ifa_dstaddr)))
1889 if (ifp->if_flags & IFF_POINTOPOINT) {
1890 if (sa_equal(addr, ifa->ifa_dstaddr))
1894 cp2 = ifa->ifa_addr->sa_data;
1895 cp3 = ifa->ifa_netmask->sa_data;
1896 cplim = ifa->ifa_netmask->sa_len + (char *)ifa->ifa_netmask;
1897 for (; cp3 < cplim; cp3++)
1898 if ((*cp++ ^ *cp2++) & *cp3)
1908 IF_ADDR_RUNLOCK(ifp);
1913 * See whether new ifa is better than current one:
1914 * 1) A non-virtual one is preferred over virtual.
1915 * 2) A virtual in master state preferred over any other state.
1917 * Used in several address selecting functions.
1920 ifa_preferred(struct ifaddr *cur, struct ifaddr *next)
1923 return (cur->ifa_carp && (!next->ifa_carp ||
1924 ((*carp_master_p)(next) && !(*carp_master_p)(cur))));
1927 #include <net/if_llatbl.h>
1930 * Default action when installing a route with a Link Level gateway.
1931 * Lookup an appropriate real ifa to point to.
1932 * This should be moved to /sys/net/link.c eventually.
1935 link_rtrequest(int cmd, struct rtentry *rt, struct rt_addrinfo *info)
1937 struct ifaddr *ifa, *oifa;
1938 struct sockaddr *dst;
1941 if (cmd != RTM_ADD || ((ifa = rt->rt_ifa) == 0) ||
1942 ((ifp = ifa->ifa_ifp) == 0) || ((dst = rt_key(rt)) == 0))
1944 ifa = ifaof_ifpforaddr(dst, ifp);
1949 if (ifa->ifa_rtrequest && ifa->ifa_rtrequest != link_rtrequest)
1950 ifa->ifa_rtrequest(cmd, rt, info);
1954 struct sockaddr_dl *
1955 link_alloc_sdl(size_t size, int flags)
1958 return (malloc(size, M_TEMP, flags));
1962 link_free_sdl(struct sockaddr *sa)
1968 * Fills in given sdl with interface basic info.
1969 * Returns pointer to filled sdl.
1971 struct sockaddr_dl *
1972 link_init_sdl(struct ifnet *ifp, struct sockaddr *paddr, u_char iftype)
1974 struct sockaddr_dl *sdl;
1976 sdl = (struct sockaddr_dl *)paddr;
1977 memset(sdl, 0, sizeof(struct sockaddr_dl));
1978 sdl->sdl_len = sizeof(struct sockaddr_dl);
1979 sdl->sdl_family = AF_LINK;
1980 sdl->sdl_index = ifp->if_index;
1981 sdl->sdl_type = iftype;
1987 * Mark an interface down and notify protocols of
1991 if_unroute(struct ifnet *ifp, int flag, int fam)
1995 KASSERT(flag == IFF_UP, ("if_unroute: flag != IFF_UP"));
1997 ifp->if_flags &= ~flag;
1998 getmicrotime(&ifp->if_lastchange);
1999 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link)
2000 if (fam == PF_UNSPEC || (fam == ifa->ifa_addr->sa_family))
2001 pfctlinput(PRC_IFDOWN, ifa->ifa_addr);
2002 ifp->if_qflush(ifp);
2005 (*carp_linkstate_p)(ifp);
2010 * Mark an interface up and notify protocols of
2014 if_route(struct ifnet *ifp, int flag, int fam)
2018 KASSERT(flag == IFF_UP, ("if_route: flag != IFF_UP"));
2020 ifp->if_flags |= flag;
2021 getmicrotime(&ifp->if_lastchange);
2022 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link)
2023 if (fam == PF_UNSPEC || (fam == ifa->ifa_addr->sa_family))
2024 pfctlinput(PRC_IFUP, ifa->ifa_addr);
2026 (*carp_linkstate_p)(ifp);
2033 void (*vlan_link_state_p)(struct ifnet *); /* XXX: private from if_vlan */
2034 void (*vlan_trunk_cap_p)(struct ifnet *); /* XXX: private from if_vlan */
2035 struct ifnet *(*vlan_trunkdev_p)(struct ifnet *);
2036 struct ifnet *(*vlan_devat_p)(struct ifnet *, uint16_t);
2037 int (*vlan_tag_p)(struct ifnet *, uint16_t *);
2038 int (*vlan_setcookie_p)(struct ifnet *, void *);
2039 void *(*vlan_cookie_p)(struct ifnet *);
2042 * Handle a change in the interface link state. To avoid LORs
2043 * between driver lock and upper layer locks, as well as possible
2044 * recursions, we post event to taskqueue, and all job
2045 * is done in static do_link_state_change().
2048 if_link_state_change(struct ifnet *ifp, int link_state)
2050 /* Return if state hasn't changed. */
2051 if (ifp->if_link_state == link_state)
2054 ifp->if_link_state = link_state;
2056 taskqueue_enqueue(taskqueue_swi, &ifp->if_linktask);
2060 do_link_state_change(void *arg, int pending)
2062 struct ifnet *ifp = (struct ifnet *)arg;
2063 int link_state = ifp->if_link_state;
2064 CURVNET_SET(ifp->if_vnet);
2066 /* Notify that the link state has changed. */
2068 if (ifp->if_vlantrunk != NULL)
2069 (*vlan_link_state_p)(ifp);
2071 if ((ifp->if_type == IFT_ETHER || ifp->if_type == IFT_L2VLAN) &&
2072 ifp->if_l2com != NULL)
2073 (*ng_ether_link_state_p)(ifp, link_state);
2075 (*carp_linkstate_p)(ifp);
2077 (*bridge_linkstate_p)(ifp);
2079 (*lagg_linkstate_p)(ifp, link_state);
2081 if (IS_DEFAULT_VNET(curvnet))
2082 devctl_notify("IFNET", ifp->if_xname,
2083 (link_state == LINK_STATE_UP) ? "LINK_UP" : "LINK_DOWN",
2086 if_printf(ifp, "%d link states coalesced\n", pending);
2087 if (log_link_state_change)
2088 log(LOG_NOTICE, "%s: link state changed to %s\n", ifp->if_xname,
2089 (link_state == LINK_STATE_UP) ? "UP" : "DOWN" );
2090 EVENTHANDLER_INVOKE(ifnet_link_event, ifp, ifp->if_link_state);
2095 * Mark an interface down and notify protocols of
2099 if_down(struct ifnet *ifp)
2102 if_unroute(ifp, IFF_UP, AF_UNSPEC);
2106 * Mark an interface up and notify protocols of
2110 if_up(struct ifnet *ifp)
2113 if_route(ifp, IFF_UP, AF_UNSPEC);
2117 * Flush an interface queue.
2120 if_qflush(struct ifnet *ifp)
2128 if (ALTQ_IS_ENABLED(ifq))
2132 while ((m = n) != 0) {
2143 * Map interface name to interface structure pointer, with or without
2144 * returning a reference.
2147 ifunit_ref(const char *name)
2151 IFNET_RLOCK_NOSLEEP();
2152 TAILQ_FOREACH(ifp, &V_ifnet, if_link) {
2153 if (strncmp(name, ifp->if_xname, IFNAMSIZ) == 0 &&
2154 !(ifp->if_flags & IFF_DYING))
2159 IFNET_RUNLOCK_NOSLEEP();
2164 ifunit(const char *name)
2168 IFNET_RLOCK_NOSLEEP();
2169 TAILQ_FOREACH(ifp, &V_ifnet, if_link) {
2170 if (strncmp(name, ifp->if_xname, IFNAMSIZ) == 0)
2173 IFNET_RUNLOCK_NOSLEEP();
2178 * Hardware specific interface ioctls.
2181 ifhwioctl(u_long cmd, struct ifnet *ifp, caddr_t data, struct thread *td)
2185 int new_flags, temp_flags;
2186 size_t namelen, onamelen;
2188 char *descrbuf, *odescrbuf;
2189 char new_name[IFNAMSIZ];
2191 struct sockaddr_dl *sdl;
2193 ifr = (struct ifreq *)data;
2196 ifr->ifr_index = ifp->if_index;
2200 temp_flags = ifp->if_flags | ifp->if_drv_flags;
2201 ifr->ifr_flags = temp_flags & 0xffff;
2202 ifr->ifr_flagshigh = temp_flags >> 16;
2206 ifr->ifr_reqcap = ifp->if_capabilities;
2207 ifr->ifr_curcap = ifp->if_capenable;
2212 error = mac_ifnet_ioctl_get(td->td_ucred, ifr, ifp);
2217 ifr->ifr_metric = ifp->if_metric;
2221 ifr->ifr_mtu = ifp->if_mtu;
2225 /* XXXGL: did this ever worked? */
2231 sx_slock(&ifdescr_sx);
2232 if (ifp->if_description == NULL)
2235 /* space for terminating nul */
2236 descrlen = strlen(ifp->if_description) + 1;
2237 if (ifr->ifr_buffer.length < descrlen)
2238 ifr->ifr_buffer.buffer = NULL;
2240 error = copyout(ifp->if_description,
2241 ifr->ifr_buffer.buffer, descrlen);
2242 ifr->ifr_buffer.length = descrlen;
2244 sx_sunlock(&ifdescr_sx);
2248 error = priv_check(td, PRIV_NET_SETIFDESCR);
2253 * Copy only (length-1) bytes to make sure that
2254 * if_description is always nul terminated. The
2255 * length parameter is supposed to count the
2256 * terminating nul in.
2258 if (ifr->ifr_buffer.length > ifdescr_maxlen)
2259 return (ENAMETOOLONG);
2260 else if (ifr->ifr_buffer.length == 0)
2263 descrbuf = malloc(ifr->ifr_buffer.length, M_IFDESCR,
2265 error = copyin(ifr->ifr_buffer.buffer, descrbuf,
2266 ifr->ifr_buffer.length - 1);
2268 free(descrbuf, M_IFDESCR);
2273 sx_xlock(&ifdescr_sx);
2274 odescrbuf = ifp->if_description;
2275 ifp->if_description = descrbuf;
2276 sx_xunlock(&ifdescr_sx);
2278 getmicrotime(&ifp->if_lastchange);
2279 free(odescrbuf, M_IFDESCR);
2283 ifr->ifr_fib = ifp->if_fib;
2287 error = priv_check(td, PRIV_NET_SETIFFIB);
2290 if (ifr->ifr_fib >= rt_numfibs)
2293 ifp->if_fib = ifr->ifr_fib;
2297 error = priv_check(td, PRIV_NET_SETIFFLAGS);
2301 * Currently, no driver owned flags pass the IFF_CANTCHANGE
2302 * check, so we don't need special handling here yet.
2304 new_flags = (ifr->ifr_flags & 0xffff) |
2305 (ifr->ifr_flagshigh << 16);
2306 if (ifp->if_flags & IFF_UP &&
2307 (new_flags & IFF_UP) == 0) {
2309 } else if (new_flags & IFF_UP &&
2310 (ifp->if_flags & IFF_UP) == 0) {
2313 /* See if permanently promiscuous mode bit is about to flip */
2314 if ((ifp->if_flags ^ new_flags) & IFF_PPROMISC) {
2315 if (new_flags & IFF_PPROMISC)
2316 ifp->if_flags |= IFF_PROMISC;
2317 else if (ifp->if_pcount == 0)
2318 ifp->if_flags &= ~IFF_PROMISC;
2319 log(LOG_INFO, "%s: permanently promiscuous mode %s\n",
2321 (new_flags & IFF_PPROMISC) ? "enabled" : "disabled");
2323 ifp->if_flags = (ifp->if_flags & IFF_CANTCHANGE) |
2324 (new_flags &~ IFF_CANTCHANGE);
2325 if (ifp->if_ioctl) {
2326 (void) (*ifp->if_ioctl)(ifp, cmd, data);
2328 getmicrotime(&ifp->if_lastchange);
2332 error = priv_check(td, PRIV_NET_SETIFCAP);
2335 if (ifp->if_ioctl == NULL)
2336 return (EOPNOTSUPP);
2337 if (ifr->ifr_reqcap & ~ifp->if_capabilities)
2339 error = (*ifp->if_ioctl)(ifp, cmd, data);
2341 getmicrotime(&ifp->if_lastchange);
2346 error = mac_ifnet_ioctl_set(td->td_ucred, ifr, ifp);
2351 error = priv_check(td, PRIV_NET_SETIFNAME);
2354 error = copyinstr(ifr->ifr_data, new_name, IFNAMSIZ, NULL);
2357 if (new_name[0] == '\0')
2359 if (ifunit(new_name) != NULL)
2363 * XXX: Locking. Nothing else seems to lock if_flags,
2364 * and there are numerous other races with the
2365 * ifunit() checks not being atomic with namespace
2366 * changes (renames, vmoves, if_attach, etc).
2368 ifp->if_flags |= IFF_RENAMING;
2370 /* Announce the departure of the interface. */
2371 rt_ifannouncemsg(ifp, IFAN_DEPARTURE);
2372 EVENTHANDLER_INVOKE(ifnet_departure_event, ifp);
2374 log(LOG_INFO, "%s: changing name to '%s'\n",
2375 ifp->if_xname, new_name);
2378 strlcpy(ifp->if_xname, new_name, sizeof(ifp->if_xname));
2380 sdl = (struct sockaddr_dl *)ifa->ifa_addr;
2381 namelen = strlen(new_name);
2382 onamelen = sdl->sdl_nlen;
2384 * Move the address if needed. This is safe because we
2385 * allocate space for a name of length IFNAMSIZ when we
2386 * create this in if_attach().
2388 if (namelen != onamelen) {
2389 bcopy(sdl->sdl_data + onamelen,
2390 sdl->sdl_data + namelen, sdl->sdl_alen);
2392 bcopy(new_name, sdl->sdl_data, namelen);
2393 sdl->sdl_nlen = namelen;
2394 sdl = (struct sockaddr_dl *)ifa->ifa_netmask;
2395 bzero(sdl->sdl_data, onamelen);
2396 while (namelen != 0)
2397 sdl->sdl_data[--namelen] = 0xff;
2398 IF_ADDR_WUNLOCK(ifp);
2400 EVENTHANDLER_INVOKE(ifnet_arrival_event, ifp);
2401 /* Announce the return of the interface. */
2402 rt_ifannouncemsg(ifp, IFAN_ARRIVAL);
2404 ifp->if_flags &= ~IFF_RENAMING;
2409 error = priv_check(td, PRIV_NET_SETIFVNET);
2412 error = if_vmove_loan(td, ifp, ifr->ifr_name, ifr->ifr_jid);
2417 error = priv_check(td, PRIV_NET_SETIFMETRIC);
2420 ifp->if_metric = ifr->ifr_metric;
2421 getmicrotime(&ifp->if_lastchange);
2425 error = priv_check(td, PRIV_NET_SETIFPHYS);
2428 if (ifp->if_ioctl == NULL)
2429 return (EOPNOTSUPP);
2430 error = (*ifp->if_ioctl)(ifp, cmd, data);
2432 getmicrotime(&ifp->if_lastchange);
2437 u_long oldmtu = ifp->if_mtu;
2439 error = priv_check(td, PRIV_NET_SETIFMTU);
2442 if (ifr->ifr_mtu < IF_MINMTU || ifr->ifr_mtu > IF_MAXMTU)
2444 if (ifp->if_ioctl == NULL)
2445 return (EOPNOTSUPP);
2446 error = (*ifp->if_ioctl)(ifp, cmd, data);
2448 getmicrotime(&ifp->if_lastchange);
2452 * If the link MTU changed, do network layer specific procedure.
2454 if (ifp->if_mtu != oldmtu) {
2465 if (cmd == SIOCADDMULTI)
2466 error = priv_check(td, PRIV_NET_ADDMULTI);
2468 error = priv_check(td, PRIV_NET_DELMULTI);
2472 /* Don't allow group membership on non-multicast interfaces. */
2473 if ((ifp->if_flags & IFF_MULTICAST) == 0)
2474 return (EOPNOTSUPP);
2476 /* Don't let users screw up protocols' entries. */
2477 if (ifr->ifr_addr.sa_family != AF_LINK)
2480 if (cmd == SIOCADDMULTI) {
2481 struct ifmultiaddr *ifma;
2484 * Userland is only permitted to join groups once
2485 * via the if_addmulti() KPI, because it cannot hold
2486 * struct ifmultiaddr * between calls. It may also
2487 * lose a race while we check if the membership
2491 ifma = if_findmulti(ifp, &ifr->ifr_addr);
2492 IF_ADDR_RUNLOCK(ifp);
2496 error = if_addmulti(ifp, &ifr->ifr_addr, &ifma);
2498 error = if_delmulti(ifp, &ifr->ifr_addr);
2501 getmicrotime(&ifp->if_lastchange);
2504 case SIOCSIFPHYADDR:
2505 case SIOCDIFPHYADDR:
2507 case SIOCSIFPHYADDR_IN6:
2510 case SIOCSIFGENERIC:
2511 error = priv_check(td, PRIV_NET_HWIOCTL);
2514 if (ifp->if_ioctl == NULL)
2515 return (EOPNOTSUPP);
2516 error = (*ifp->if_ioctl)(ifp, cmd, data);
2518 getmicrotime(&ifp->if_lastchange);
2522 case SIOCGIFPSRCADDR:
2523 case SIOCGIFPDSTADDR:
2526 case SIOCGIFGENERIC:
2527 if (ifp->if_ioctl == NULL)
2528 return (EOPNOTSUPP);
2529 error = (*ifp->if_ioctl)(ifp, cmd, data);
2533 error = priv_check(td, PRIV_NET_SETLLADDR);
2536 error = if_setlladdr(ifp,
2537 ifr->ifr_addr.sa_data, ifr->ifr_addr.sa_len);
2538 EVENTHANDLER_INVOKE(iflladdr_event, ifp);
2543 struct ifgroupreq *ifgr = (struct ifgroupreq *)ifr;
2545 error = priv_check(td, PRIV_NET_ADDIFGROUP);
2548 if ((error = if_addgroup(ifp, ifgr->ifgr_group)))
2554 if ((error = if_getgroup((struct ifgroupreq *)ifr, ifp)))
2560 struct ifgroupreq *ifgr = (struct ifgroupreq *)ifr;
2562 error = priv_check(td, PRIV_NET_DELIFGROUP);
2565 if ((error = if_delgroup(ifp, ifgr->ifgr_group)))
2577 #ifdef COMPAT_FREEBSD32
2585 #define SIOCGIFCONF32 _IOWR('i', 36, struct ifconf32)
2592 ifioctl(struct socket *so, u_long cmd, caddr_t data, struct thread *td)
2599 CURVNET_SET(so->so_vnet);
2602 error = ifconf(cmd, data);
2606 #ifdef COMPAT_FREEBSD32
2609 struct ifconf32 *ifc32;
2612 ifc32 = (struct ifconf32 *)data;
2613 ifc.ifc_len = ifc32->ifc_len;
2614 ifc.ifc_buf = PTRIN(ifc32->ifc_buf);
2616 error = ifconf(SIOCGIFCONF, (void *)&ifc);
2619 ifc32->ifc_len = ifc.ifc_len;
2624 ifr = (struct ifreq *)data;
2629 error = priv_check(td, PRIV_NET_SETIFVNET);
2631 error = if_vmove_reclaim(td, ifr->ifr_name,
2638 error = priv_check(td, PRIV_NET_IFCREATE);
2640 error = if_clone_create(ifr->ifr_name,
2641 sizeof(ifr->ifr_name),
2642 cmd == SIOCIFCREATE2 ? ifr->ifr_data : NULL);
2646 error = priv_check(td, PRIV_NET_IFDESTROY);
2648 error = if_clone_destroy(ifr->ifr_name);
2652 case SIOCIFGCLONERS:
2653 error = if_clone_list((struct if_clonereq *)data);
2657 error = if_getgroupmembers((struct ifgroupreq *)data);
2660 #if defined(INET) || defined(INET6)
2663 if (carp_ioctl_p == NULL)
2664 error = EPROTONOSUPPORT;
2666 error = (*carp_ioctl_p)(ifr, cmd, td);
2672 ifp = ifunit_ref(ifr->ifr_name);
2678 error = ifhwioctl(cmd, ifp, data, td);
2679 if (error != ENOIOCTL) {
2685 oif_flags = ifp->if_flags;
2686 if (so->so_proto == NULL) {
2689 return (EOPNOTSUPP);
2693 * Pass the request on to the socket control method, and if the
2694 * latter returns EOPNOTSUPP, directly to the interface.
2696 * Make an exception for the legacy SIOCSIF* requests. Drivers
2697 * trust SIOCSIFADDR et al to come from an already privileged
2698 * layer, and do not perform any credentials checks or input
2701 error = ((*so->so_proto->pr_usrreqs->pru_control)(so, cmd, data,
2703 if (error == EOPNOTSUPP && ifp != NULL && ifp->if_ioctl != NULL &&
2704 cmd != SIOCSIFADDR && cmd != SIOCSIFBRDADDR &&
2705 cmd != SIOCSIFDSTADDR && cmd != SIOCSIFNETMASK)
2706 error = (*ifp->if_ioctl)(ifp, cmd, data);
2708 if ((oif_flags ^ ifp->if_flags) & IFF_UP) {
2710 if (ifp->if_flags & IFF_UP)
2720 * The code common to handling reference counted flags,
2721 * e.g., in ifpromisc() and if_allmulti().
2722 * The "pflag" argument can specify a permanent mode flag to check,
2723 * such as IFF_PPROMISC for promiscuous mode; should be 0 if none.
2725 * Only to be used on stack-owned flags, not driver-owned flags.
2728 if_setflag(struct ifnet *ifp, int flag, int pflag, int *refcount, int onswitch)
2732 int oldflags, oldcount;
2734 /* Sanity checks to catch programming errors */
2735 KASSERT((flag & (IFF_DRV_OACTIVE|IFF_DRV_RUNNING)) == 0,
2736 ("%s: setting driver-owned flag %d", __func__, flag));
2739 KASSERT(*refcount >= 0,
2740 ("%s: increment negative refcount %d for flag %d",
2741 __func__, *refcount, flag));
2743 KASSERT(*refcount > 0,
2744 ("%s: decrement non-positive refcount %d for flag %d",
2745 __func__, *refcount, flag));
2747 /* In case this mode is permanent, just touch refcount */
2748 if (ifp->if_flags & pflag) {
2749 *refcount += onswitch ? 1 : -1;
2753 /* Save ifnet parameters for if_ioctl() may fail */
2754 oldcount = *refcount;
2755 oldflags = ifp->if_flags;
2758 * See if we aren't the only and touching refcount is enough.
2759 * Actually toggle interface flag if we are the first or last.
2764 ifp->if_flags |= flag;
2768 ifp->if_flags &= ~flag;
2771 /* Call down the driver since we've changed interface flags */
2772 if (ifp->if_ioctl == NULL) {
2776 ifr.ifr_flags = ifp->if_flags & 0xffff;
2777 ifr.ifr_flagshigh = ifp->if_flags >> 16;
2778 error = (*ifp->if_ioctl)(ifp, SIOCSIFFLAGS, (caddr_t)&ifr);
2781 /* Notify userland that interface flags have changed */
2786 /* Recover after driver error */
2787 *refcount = oldcount;
2788 ifp->if_flags = oldflags;
2793 * Set/clear promiscuous mode on interface ifp based on the truth value
2794 * of pswitch. The calls are reference counted so that only the first
2795 * "on" request actually has an effect, as does the final "off" request.
2796 * Results are undefined if the "off" and "on" requests are not matched.
2799 ifpromisc(struct ifnet *ifp, int pswitch)
2802 int oldflags = ifp->if_flags;
2804 error = if_setflag(ifp, IFF_PROMISC, IFF_PPROMISC,
2805 &ifp->if_pcount, pswitch);
2806 /* If promiscuous mode status has changed, log a message */
2807 if (error == 0 && ((ifp->if_flags ^ oldflags) & IFF_PROMISC))
2808 log(LOG_INFO, "%s: promiscuous mode %s\n",
2810 (ifp->if_flags & IFF_PROMISC) ? "enabled" : "disabled");
2815 * Return interface configuration
2816 * of system. List may be used
2817 * in later ioctl's (above) to get
2818 * other information.
2822 ifconf(u_long cmd, caddr_t data)
2824 struct ifconf *ifc = (struct ifconf *)data;
2829 int error, full = 0, valid_len, max_len;
2831 /* Limit initial buffer size to MAXPHYS to avoid DoS from userspace. */
2832 max_len = MAXPHYS - 1;
2834 /* Prevent hostile input from being able to crash the system */
2835 if (ifc->ifc_len <= 0)
2839 if (ifc->ifc_len <= max_len) {
2840 max_len = ifc->ifc_len;
2843 sb = sbuf_new(NULL, NULL, max_len + 1, SBUF_FIXEDLEN);
2848 TAILQ_FOREACH(ifp, &V_ifnet, if_link) {
2852 * Zero the ifr_name buffer to make sure we don't
2853 * disclose the contents of the stack.
2855 memset(ifr.ifr_name, 0, sizeof(ifr.ifr_name));
2857 if (strlcpy(ifr.ifr_name, ifp->if_xname, sizeof(ifr.ifr_name))
2858 >= sizeof(ifr.ifr_name)) {
2861 return (ENAMETOOLONG);
2866 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
2867 struct sockaddr *sa = ifa->ifa_addr;
2869 if (prison_if(curthread->td_ucred, sa) != 0)
2872 if (sa->sa_len <= sizeof(*sa)) {
2874 sbuf_bcat(sb, &ifr, sizeof(ifr));
2875 max_len += sizeof(ifr);
2878 offsetof(struct ifreq, ifr_addr));
2879 max_len += offsetof(struct ifreq, ifr_addr);
2880 sbuf_bcat(sb, sa, sa->sa_len);
2881 max_len += sa->sa_len;
2884 if (sbuf_error(sb) == 0)
2885 valid_len = sbuf_len(sb);
2887 IF_ADDR_RUNLOCK(ifp);
2889 bzero((caddr_t)&ifr.ifr_addr, sizeof(ifr.ifr_addr));
2890 sbuf_bcat(sb, &ifr, sizeof(ifr));
2891 max_len += sizeof(ifr);
2893 if (sbuf_error(sb) == 0)
2894 valid_len = sbuf_len(sb);
2900 * If we didn't allocate enough space (uncommon), try again. If
2901 * we have already allocated as much space as we are allowed,
2902 * return what we've got.
2904 if (valid_len != max_len && !full) {
2909 ifc->ifc_len = valid_len;
2911 error = copyout(sbuf_data(sb), ifc->ifc_req, ifc->ifc_len);
2917 * Just like ifpromisc(), but for all-multicast-reception mode.
2920 if_allmulti(struct ifnet *ifp, int onswitch)
2923 return (if_setflag(ifp, IFF_ALLMULTI, 0, &ifp->if_amcount, onswitch));
2926 struct ifmultiaddr *
2927 if_findmulti(struct ifnet *ifp, const struct sockaddr *sa)
2929 struct ifmultiaddr *ifma;
2931 IF_ADDR_LOCK_ASSERT(ifp);
2933 TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
2934 if (sa->sa_family == AF_LINK) {
2935 if (sa_dl_equal(ifma->ifma_addr, sa))
2938 if (sa_equal(ifma->ifma_addr, sa))
2947 * Allocate a new ifmultiaddr and initialize based on passed arguments. We
2948 * make copies of passed sockaddrs. The ifmultiaddr will not be added to
2949 * the ifnet multicast address list here, so the caller must do that and
2950 * other setup work (such as notifying the device driver). The reference
2951 * count is initialized to 1.
2953 static struct ifmultiaddr *
2954 if_allocmulti(struct ifnet *ifp, struct sockaddr *sa, struct sockaddr *llsa,
2957 struct ifmultiaddr *ifma;
2958 struct sockaddr *dupsa;
2960 ifma = malloc(sizeof *ifma, M_IFMADDR, mflags |
2965 dupsa = malloc(sa->sa_len, M_IFMADDR, mflags);
2966 if (dupsa == NULL) {
2967 free(ifma, M_IFMADDR);
2970 bcopy(sa, dupsa, sa->sa_len);
2971 ifma->ifma_addr = dupsa;
2973 ifma->ifma_ifp = ifp;
2974 ifma->ifma_refcount = 1;
2975 ifma->ifma_protospec = NULL;
2978 ifma->ifma_lladdr = NULL;
2982 dupsa = malloc(llsa->sa_len, M_IFMADDR, mflags);
2983 if (dupsa == NULL) {
2984 free(ifma->ifma_addr, M_IFMADDR);
2985 free(ifma, M_IFMADDR);
2988 bcopy(llsa, dupsa, llsa->sa_len);
2989 ifma->ifma_lladdr = dupsa;
2995 * if_freemulti: free ifmultiaddr structure and possibly attached related
2996 * addresses. The caller is responsible for implementing reference
2997 * counting, notifying the driver, handling routing messages, and releasing
2998 * any dependent link layer state.
3001 if_freemulti(struct ifmultiaddr *ifma)
3004 KASSERT(ifma->ifma_refcount == 0, ("if_freemulti: refcount %d",
3005 ifma->ifma_refcount));
3007 if (ifma->ifma_lladdr != NULL)
3008 free(ifma->ifma_lladdr, M_IFMADDR);
3009 free(ifma->ifma_addr, M_IFMADDR);
3010 free(ifma, M_IFMADDR);
3014 * Register an additional multicast address with a network interface.
3016 * - If the address is already present, bump the reference count on the
3017 * address and return.
3018 * - If the address is not link-layer, look up a link layer address.
3019 * - Allocate address structures for one or both addresses, and attach to the
3020 * multicast address list on the interface. If automatically adding a link
3021 * layer address, the protocol address will own a reference to the link
3022 * layer address, to be freed when it is freed.
3023 * - Notify the network device driver of an addition to the multicast address
3026 * 'sa' points to caller-owned memory with the desired multicast address.
3028 * 'retifma' will be used to return a pointer to the resulting multicast
3029 * address reference, if desired.
3032 if_addmulti(struct ifnet *ifp, struct sockaddr *sa,
3033 struct ifmultiaddr **retifma)
3035 struct ifmultiaddr *ifma, *ll_ifma;
3036 struct sockaddr *llsa;
3037 struct sockaddr_dl sdl;
3041 * If the address is already present, return a new reference to it;
3042 * otherwise, allocate storage and set up a new address.
3045 ifma = if_findmulti(ifp, sa);
3047 ifma->ifma_refcount++;
3048 if (retifma != NULL)
3050 IF_ADDR_WUNLOCK(ifp);
3055 * The address isn't already present; resolve the protocol address
3056 * into a link layer address, and then look that up, bump its
3057 * refcount or allocate an ifma for that also.
3058 * Most link layer resolving functions returns address data which
3059 * fits inside default sockaddr_dl structure. However callback
3060 * can allocate another sockaddr structure, in that case we need to
3065 if (ifp->if_resolvemulti != NULL) {
3066 /* Provide called function with buffer size information */
3067 sdl.sdl_len = sizeof(sdl);
3068 llsa = (struct sockaddr *)&sdl;
3069 error = ifp->if_resolvemulti(ifp, &llsa, sa);
3075 * Allocate the new address. Don't hook it up yet, as we may also
3076 * need to allocate a link layer multicast address.
3078 ifma = if_allocmulti(ifp, sa, llsa, M_NOWAIT);
3085 * If a link layer address is found, we'll need to see if it's
3086 * already present in the address list, or allocate is as well.
3087 * When this block finishes, the link layer address will be on the
3091 ll_ifma = if_findmulti(ifp, llsa);
3092 if (ll_ifma == NULL) {
3093 ll_ifma = if_allocmulti(ifp, llsa, NULL, M_NOWAIT);
3094 if (ll_ifma == NULL) {
3095 --ifma->ifma_refcount;
3100 TAILQ_INSERT_HEAD(&ifp->if_multiaddrs, ll_ifma,
3103 ll_ifma->ifma_refcount++;
3104 ifma->ifma_llifma = ll_ifma;
3108 * We now have a new multicast address, ifma, and possibly a new or
3109 * referenced link layer address. Add the primary address to the
3110 * ifnet address list.
3112 TAILQ_INSERT_HEAD(&ifp->if_multiaddrs, ifma, ifma_link);
3114 if (retifma != NULL)
3118 * Must generate the message while holding the lock so that 'ifma'
3119 * pointer is still valid.
3121 rt_newmaddrmsg(RTM_NEWMADDR, ifma);
3122 IF_ADDR_WUNLOCK(ifp);
3125 * We are certain we have added something, so call down to the
3126 * interface to let them know about it.
3128 if (ifp->if_ioctl != NULL) {
3129 (void) (*ifp->if_ioctl)(ifp, SIOCADDMULTI, 0);
3132 if ((llsa != NULL) && (llsa != (struct sockaddr *)&sdl))
3133 link_free_sdl(llsa);
3138 if ((llsa != NULL) && (llsa != (struct sockaddr *)&sdl))
3139 link_free_sdl(llsa);
3142 IF_ADDR_WUNLOCK(ifp);
3147 * Delete a multicast group membership by network-layer group address.
3149 * Returns ENOENT if the entry could not be found. If ifp no longer
3150 * exists, results are undefined. This entry point should only be used
3151 * from subsystems which do appropriate locking to hold ifp for the
3152 * duration of the call.
3153 * Network-layer protocol domains must use if_delmulti_ifma().
3156 if_delmulti(struct ifnet *ifp, struct sockaddr *sa)
3158 struct ifmultiaddr *ifma;
3163 IFNET_RLOCK_NOSLEEP();
3164 TAILQ_FOREACH(oifp, &V_ifnet, if_link)
3169 IFNET_RUNLOCK_NOSLEEP();
3171 KASSERT(ifp != NULL, ("%s: ifnet went away", __func__));
3178 ifma = if_findmulti(ifp, sa);
3180 lastref = if_delmulti_locked(ifp, ifma, 0);
3181 IF_ADDR_WUNLOCK(ifp);
3186 if (lastref && ifp->if_ioctl != NULL) {
3187 (void)(*ifp->if_ioctl)(ifp, SIOCDELMULTI, 0);
3194 * Delete all multicast group membership for an interface.
3195 * Should be used to quickly flush all multicast filters.
3198 if_delallmulti(struct ifnet *ifp)
3200 struct ifmultiaddr *ifma;
3201 struct ifmultiaddr *next;
3204 TAILQ_FOREACH_SAFE(ifma, &ifp->if_multiaddrs, ifma_link, next)
3205 if_delmulti_locked(ifp, ifma, 0);
3206 IF_ADDR_WUNLOCK(ifp);
3210 * Delete a multicast group membership by group membership pointer.
3211 * Network-layer protocol domains must use this routine.
3213 * It is safe to call this routine if the ifp disappeared.
3216 if_delmulti_ifma(struct ifmultiaddr *ifma)
3221 ifp = ifma->ifma_ifp;
3224 printf("%s: ifma_ifp seems to be detached\n", __func__);
3228 IFNET_RLOCK_NOSLEEP();
3229 TAILQ_FOREACH(oifp, &V_ifnet, if_link)
3233 printf("%s: ifnet %p disappeared\n", __func__, ifp);
3236 IFNET_RUNLOCK_NOSLEEP();
3240 * If and only if the ifnet instance exists: Acquire the address lock.
3245 lastref = if_delmulti_locked(ifp, ifma, 0);
3249 * If and only if the ifnet instance exists:
3250 * Release the address lock.
3251 * If the group was left: update the hardware hash filter.
3253 IF_ADDR_WUNLOCK(ifp);
3254 if (lastref && ifp->if_ioctl != NULL) {
3255 (void)(*ifp->if_ioctl)(ifp, SIOCDELMULTI, 0);
3261 * Perform deletion of network-layer and/or link-layer multicast address.
3263 * Return 0 if the reference count was decremented.
3264 * Return 1 if the final reference was released, indicating that the
3265 * hardware hash filter should be reprogrammed.
3268 if_delmulti_locked(struct ifnet *ifp, struct ifmultiaddr *ifma, int detaching)
3270 struct ifmultiaddr *ll_ifma;
3272 if (ifp != NULL && ifma->ifma_ifp != NULL) {
3273 KASSERT(ifma->ifma_ifp == ifp,
3274 ("%s: inconsistent ifp %p", __func__, ifp));
3275 IF_ADDR_WLOCK_ASSERT(ifp);
3278 ifp = ifma->ifma_ifp;
3281 * If the ifnet is detaching, null out references to ifnet,
3282 * so that upper protocol layers will notice, and not attempt
3283 * to obtain locks for an ifnet which no longer exists. The
3284 * routing socket announcement must happen before the ifnet
3285 * instance is detached from the system.
3289 printf("%s: detaching ifnet instance %p\n", __func__, ifp);
3292 * ifp may already be nulled out if we are being reentered
3293 * to delete the ll_ifma.
3296 rt_newmaddrmsg(RTM_DELMADDR, ifma);
3297 ifma->ifma_ifp = NULL;
3301 if (--ifma->ifma_refcount > 0)
3305 * If this ifma is a network-layer ifma, a link-layer ifma may
3306 * have been associated with it. Release it first if so.
3308 ll_ifma = ifma->ifma_llifma;
3309 if (ll_ifma != NULL) {
3310 KASSERT(ifma->ifma_lladdr != NULL,
3311 ("%s: llifma w/o lladdr", __func__));
3313 ll_ifma->ifma_ifp = NULL; /* XXX */
3314 if (--ll_ifma->ifma_refcount == 0) {
3316 TAILQ_REMOVE(&ifp->if_multiaddrs, ll_ifma,
3319 if_freemulti(ll_ifma);
3324 TAILQ_REMOVE(&ifp->if_multiaddrs, ifma, ifma_link);
3329 * The last reference to this instance of struct ifmultiaddr
3330 * was released; the hardware should be notified of this change.
3336 * Set the link layer address on an interface.
3338 * At this time we only support certain types of interfaces,
3339 * and we don't allow the length of the address to change.
3342 if_setlladdr(struct ifnet *ifp, const u_char *lladdr, int len)
3344 struct sockaddr_dl *sdl;
3351 IF_ADDR_RUNLOCK(ifp);
3355 IF_ADDR_RUNLOCK(ifp);
3356 sdl = (struct sockaddr_dl *)ifa->ifa_addr;
3361 if (len != sdl->sdl_alen) { /* don't allow length to change */
3365 switch (ifp->if_type) {
3373 case IFT_IEEE8023ADLAG:
3375 bcopy(lladdr, LLADDR(sdl), len);
3384 * If the interface is already up, we need
3385 * to re-init it in order to reprogram its
3388 if ((ifp->if_flags & IFF_UP) != 0) {
3389 if (ifp->if_ioctl) {
3390 ifp->if_flags &= ~IFF_UP;
3391 ifr.ifr_flags = ifp->if_flags & 0xffff;
3392 ifr.ifr_flagshigh = ifp->if_flags >> 16;
3393 (*ifp->if_ioctl)(ifp, SIOCSIFFLAGS, (caddr_t)&ifr);
3394 ifp->if_flags |= IFF_UP;
3395 ifr.ifr_flags = ifp->if_flags & 0xffff;
3396 ifr.ifr_flagshigh = ifp->if_flags >> 16;
3397 (*ifp->if_ioctl)(ifp, SIOCSIFFLAGS, (caddr_t)&ifr);
3401 * Also send gratuitous ARPs to notify other nodes about
3402 * the address change.
3404 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
3405 if (ifa->ifa_addr->sa_family == AF_INET)
3406 arp_ifinit(ifp, ifa);
3414 * The name argument must be a pointer to storage which will last as
3415 * long as the interface does. For physical devices, the result of
3416 * device_get_name(dev) is a good choice and for pseudo-devices a
3417 * static string works well.
3420 if_initname(struct ifnet *ifp, const char *name, int unit)
3422 ifp->if_dname = name;
3423 ifp->if_dunit = unit;
3424 if (unit != IF_DUNIT_NONE)
3425 snprintf(ifp->if_xname, IFNAMSIZ, "%s%d", name, unit);
3427 strlcpy(ifp->if_xname, name, IFNAMSIZ);
3431 if_printf(struct ifnet *ifp, const char * fmt, ...)
3436 retval = printf("%s: ", ifp->if_xname);
3438 retval += vprintf(fmt, ap);
3444 if_start(struct ifnet *ifp)
3447 (*(ifp)->if_start)(ifp);
3451 * Backwards compatibility interface for drivers
3452 * that have not implemented it
3455 if_transmit(struct ifnet *ifp, struct mbuf *m)
3459 IFQ_HANDOFF(ifp, m, error);
3464 if_input_default(struct ifnet *ifp __unused, struct mbuf *m)
3471 if_handoff(struct ifqueue *ifq, struct mbuf *m, struct ifnet *ifp, int adjust)
3476 if (_IF_QFULL(ifq)) {
3478 if_inc_counter(ifp, IFCOUNTER_OQDROPS, 1);
3483 if_inc_counter(ifp, IFCOUNTER_OBYTES, m->m_pkthdr.len + adjust);
3484 if (m->m_flags & (M_BCAST|M_MCAST))
3485 if_inc_counter(ifp, IFCOUNTER_OMCASTS, 1);
3486 active = ifp->if_drv_flags & IFF_DRV_OACTIVE;
3488 _IF_ENQUEUE(ifq, m);
3490 if (ifp != NULL && !active)
3491 (*(ifp)->if_start)(ifp);
3496 if_register_com_alloc(u_char type,
3497 if_com_alloc_t *a, if_com_free_t *f)
3500 KASSERT(if_com_alloc[type] == NULL,
3501 ("if_register_com_alloc: %d already registered", type));
3502 KASSERT(if_com_free[type] == NULL,
3503 ("if_register_com_alloc: %d free already registered", type));
3505 if_com_alloc[type] = a;
3506 if_com_free[type] = f;
3510 if_deregister_com_alloc(u_char type)
3513 KASSERT(if_com_alloc[type] != NULL,
3514 ("if_deregister_com_alloc: %d not registered", type));
3515 KASSERT(if_com_free[type] != NULL,
3516 ("if_deregister_com_alloc: %d free not registered", type));
3517 if_com_alloc[type] = NULL;
3518 if_com_free[type] = NULL;
3521 /* API for driver access to network stack owned ifnet.*/
3523 if_setbaudrate(struct ifnet *ifp, uint64_t baudrate)
3527 oldbrate = ifp->if_baudrate;
3528 ifp->if_baudrate = baudrate;
3533 if_getbaudrate(if_t ifp)
3536 return (((struct ifnet *)ifp)->if_baudrate);
3540 if_setcapabilities(if_t ifp, int capabilities)
3542 ((struct ifnet *)ifp)->if_capabilities = capabilities;
3547 if_setcapabilitiesbit(if_t ifp, int setbit, int clearbit)
3549 ((struct ifnet *)ifp)->if_capabilities |= setbit;
3550 ((struct ifnet *)ifp)->if_capabilities &= ~clearbit;
3556 if_getcapabilities(if_t ifp)
3558 return ((struct ifnet *)ifp)->if_capabilities;
3562 if_setcapenable(if_t ifp, int capabilities)
3564 ((struct ifnet *)ifp)->if_capenable = capabilities;
3569 if_setcapenablebit(if_t ifp, int setcap, int clearcap)
3572 ((struct ifnet *)ifp)->if_capenable |= setcap;
3574 ((struct ifnet *)ifp)->if_capenable &= ~clearcap;
3580 if_getdname(if_t ifp)
3582 return ((struct ifnet *)ifp)->if_dname;
3586 if_togglecapenable(if_t ifp, int togglecap)
3588 ((struct ifnet *)ifp)->if_capenable ^= togglecap;
3593 if_getcapenable(if_t ifp)
3595 return ((struct ifnet *)ifp)->if_capenable;
3599 * This is largely undesirable because it ties ifnet to a device, but does
3600 * provide flexiblity for an embedded product vendor. Should be used with
3601 * the understanding that it violates the interface boundaries, and should be
3602 * a last resort only.
3605 if_setdev(if_t ifp, void *dev)
3611 if_setdrvflagbits(if_t ifp, int set_flags, int clear_flags)
3613 ((struct ifnet *)ifp)->if_drv_flags |= set_flags;
3614 ((struct ifnet *)ifp)->if_drv_flags &= ~clear_flags;
3620 if_getdrvflags(if_t ifp)
3622 return ((struct ifnet *)ifp)->if_drv_flags;
3626 if_setdrvflags(if_t ifp, int flags)
3628 ((struct ifnet *)ifp)->if_drv_flags = flags;
3634 if_setflags(if_t ifp, int flags)
3636 ((struct ifnet *)ifp)->if_flags = flags;
3641 if_setflagbits(if_t ifp, int set, int clear)
3643 ((struct ifnet *)ifp)->if_flags |= set;
3644 ((struct ifnet *)ifp)->if_flags &= ~clear;
3650 if_getflags(if_t ifp)
3652 return ((struct ifnet *)ifp)->if_flags;
3656 if_clearhwassist(if_t ifp)
3658 ((struct ifnet *)ifp)->if_hwassist = 0;
3663 if_sethwassistbits(if_t ifp, int toset, int toclear)
3665 ((struct ifnet *)ifp)->if_hwassist |= toset;
3666 ((struct ifnet *)ifp)->if_hwassist &= ~toclear;
3672 if_sethwassist(if_t ifp, int hwassist_bit)
3674 ((struct ifnet *)ifp)->if_hwassist = hwassist_bit;
3679 if_gethwassist(if_t ifp)
3681 return ((struct ifnet *)ifp)->if_hwassist;
3685 if_setmtu(if_t ifp, int mtu)
3687 ((struct ifnet *)ifp)->if_mtu = mtu;
3694 return ((struct ifnet *)ifp)->if_mtu;
3698 if_getmtu_family(if_t ifp, int family)
3702 for (dp = domains; dp; dp = dp->dom_next) {
3703 if (dp->dom_family == family && dp->dom_ifmtu != NULL)
3704 return (dp->dom_ifmtu((struct ifnet *)ifp));
3707 return (((struct ifnet *)ifp)->if_mtu);
3711 if_setsoftc(if_t ifp, void *softc)
3713 ((struct ifnet *)ifp)->if_softc = softc;
3718 if_getsoftc(if_t ifp)
3720 return ((struct ifnet *)ifp)->if_softc;
3724 if_setrcvif(struct mbuf *m, if_t ifp)
3726 m->m_pkthdr.rcvif = (struct ifnet *)ifp;
3730 if_setvtag(struct mbuf *m, uint16_t tag)
3732 m->m_pkthdr.ether_vtag = tag;
3736 if_getvtag(struct mbuf *m)
3739 return (m->m_pkthdr.ether_vtag);
3743 if_sendq_empty(if_t ifp)
3745 return IFQ_DRV_IS_EMPTY(&((struct ifnet *)ifp)->if_snd);
3749 if_getifaddr(if_t ifp)
3751 return ((struct ifnet *)ifp)->if_addr;
3755 if_getamcount(if_t ifp)
3757 return ((struct ifnet *)ifp)->if_amcount;
3762 if_setsendqready(if_t ifp)
3764 IFQ_SET_READY(&((struct ifnet *)ifp)->if_snd);
3769 if_setsendqlen(if_t ifp, int tx_desc_count)
3771 IFQ_SET_MAXLEN(&((struct ifnet *)ifp)->if_snd, tx_desc_count);
3772 ((struct ifnet *)ifp)->if_snd.ifq_drv_maxlen = tx_desc_count;
3778 if_vlantrunkinuse(if_t ifp)
3780 return ((struct ifnet *)ifp)->if_vlantrunk != NULL?1:0;
3784 if_input(if_t ifp, struct mbuf* sendmp)
3786 (*((struct ifnet *)ifp)->if_input)((struct ifnet *)ifp, sendmp);
3792 #ifndef ETH_ADDR_LEN
3793 #define ETH_ADDR_LEN 6
3797 if_setupmultiaddr(if_t ifp, void *mta, int *cnt, int max)
3799 struct ifmultiaddr *ifma;
3800 uint8_t *lmta = (uint8_t *)mta;
3803 TAILQ_FOREACH(ifma, &((struct ifnet *)ifp)->if_multiaddrs, ifma_link) {
3804 if (ifma->ifma_addr->sa_family != AF_LINK)
3810 bcopy(LLADDR((struct sockaddr_dl *)ifma->ifma_addr),
3811 &lmta[mcnt * ETH_ADDR_LEN], ETH_ADDR_LEN);
3820 if_multiaddr_array(if_t ifp, void *mta, int *cnt, int max)
3824 if_maddr_rlock(ifp);
3825 error = if_setupmultiaddr(ifp, mta, cnt, max);
3826 if_maddr_runlock(ifp);
3831 if_multiaddr_count(if_t ifp, int max)
3833 struct ifmultiaddr *ifma;
3837 if_maddr_rlock(ifp);
3838 TAILQ_FOREACH(ifma, &((struct ifnet *)ifp)->if_multiaddrs, ifma_link) {
3839 if (ifma->ifma_addr->sa_family != AF_LINK)
3845 if_maddr_runlock(ifp);
3850 if_dequeue(if_t ifp)
3853 IFQ_DRV_DEQUEUE(&((struct ifnet *)ifp)->if_snd, m);
3859 if_sendq_prepend(if_t ifp, struct mbuf *m)
3861 IFQ_DRV_PREPEND(&((struct ifnet *)ifp)->if_snd, m);
3866 if_setifheaderlen(if_t ifp, int len)
3868 ((struct ifnet *)ifp)->if_hdrlen = len;
3873 if_getlladdr(if_t ifp)
3875 return (IF_LLADDR((struct ifnet *)ifp));
3879 if_gethandle(u_char type)
3881 return (if_alloc(type));
3885 if_bpfmtap(if_t ifh, struct mbuf *m)
3887 struct ifnet *ifp = (struct ifnet *)ifh;
3893 if_etherbpfmtap(if_t ifh, struct mbuf *m)
3895 struct ifnet *ifp = (struct ifnet *)ifh;
3897 ETHER_BPF_MTAP(ifp, m);
3901 if_vlancap(if_t ifh)
3903 struct ifnet *ifp = (struct ifnet *)ifh;
3904 VLAN_CAPABILITIES(ifp);
3908 if_setinitfn(if_t ifp, void (*init_fn)(void *))
3910 ((struct ifnet *)ifp)->if_init = init_fn;
3914 if_setioctlfn(if_t ifp, int (*ioctl_fn)(if_t, u_long, caddr_t))
3916 ((struct ifnet *)ifp)->if_ioctl = (void *)ioctl_fn;
3920 if_setstartfn(if_t ifp, void (*start_fn)(if_t))
3922 ((struct ifnet *)ifp)->if_start = (void *)start_fn;
3926 if_settransmitfn(if_t ifp, if_transmit_fn_t start_fn)
3928 ((struct ifnet *)ifp)->if_transmit = start_fn;
3931 void if_setqflushfn(if_t ifp, if_qflush_fn_t flush_fn)
3933 ((struct ifnet *)ifp)->if_qflush = flush_fn;
3938 if_setgetcounterfn(if_t ifp, if_get_counter_t fn)
3941 ifp->if_get_counter = fn;
3944 /* Revisit these - These are inline functions originally. */
3946 drbr_inuse_drv(if_t ifh, struct buf_ring *br)
3948 return drbr_inuse(ifh, br);
3952 drbr_dequeue_drv(if_t ifh, struct buf_ring *br)
3954 return drbr_dequeue(ifh, br);
3958 drbr_needs_enqueue_drv(if_t ifh, struct buf_ring *br)
3960 return drbr_needs_enqueue(ifh, br);
3964 drbr_enqueue_drv(if_t ifh, struct buf_ring *br, struct mbuf *m)
3966 return drbr_enqueue(ifh, br, m);