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_route(struct ifnet *, int flag, int fam);
164 static int if_setflag(struct ifnet *, int, int, int *, int);
165 static int if_transmit(struct ifnet *ifp, struct mbuf *m);
166 static void if_unroute(struct ifnet *, int flag, int fam);
167 static void link_rtrequest(int, struct rtentry *, struct rt_addrinfo *);
168 static int if_rtdel(struct radix_node *, void *);
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);
176 static void if_detach_internal(struct ifnet *, int);
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)
339 IFNET_RLOCK_NOSLEEP();
340 ifa = ifnet_byindex_locked(idx)->if_addr;
343 IFNET_RUNLOCK_NOSLEEP();
348 * Network interface utility routines.
350 * Routines with ifa_ifwith* names take sockaddr *'s as
355 vnet_if_init(const void *unused __unused)
358 TAILQ_INIT(&V_ifnet);
359 TAILQ_INIT(&V_ifg_head);
361 if_grow(); /* create initial table */
363 vnet_if_clone_init();
365 VNET_SYSINIT(vnet_if_init, SI_SUB_INIT_IF, SI_ORDER_SECOND, vnet_if_init,
370 vnet_if_uninit(const void *unused __unused)
373 VNET_ASSERT(TAILQ_EMPTY(&V_ifnet), ("%s:%d tailq &V_ifnet=%p "
374 "not empty", __func__, __LINE__, &V_ifnet));
375 VNET_ASSERT(TAILQ_EMPTY(&V_ifg_head), ("%s:%d tailq &V_ifg_head=%p "
376 "not empty", __func__, __LINE__, &V_ifg_head));
378 free((caddr_t)V_ifindex_table, M_IFNET);
380 VNET_SYSUNINIT(vnet_if_uninit, SI_SUB_INIT_IF, SI_ORDER_FIRST,
381 vnet_if_uninit, NULL);
391 IFNET_WLOCK_ASSERT();
392 oldlim = V_if_indexlim;
394 n = (oldlim << 1) * sizeof(*e);
395 e = malloc(n, M_IFNET, M_WAITOK | M_ZERO);
397 if (V_if_indexlim != oldlim) {
401 if (V_ifindex_table != NULL) {
402 memcpy((caddr_t)e, (caddr_t)V_ifindex_table, n/2);
403 free((caddr_t)V_ifindex_table, M_IFNET);
410 * Allocate a struct ifnet and an index for an interface. A layer 2
411 * common structure will also be allocated if an allocation routine is
412 * registered for the passed type.
415 if_alloc(u_char type)
420 ifp = malloc(sizeof(struct ifnet), M_IFNET, M_WAITOK|M_ZERO);
422 idx = ifindex_alloc();
423 ifnet_setbyindex_locked(idx, IFNET_HOLD);
427 ifp->if_alloctype = type;
428 if (if_com_alloc[type] != NULL) {
429 ifp->if_l2com = if_com_alloc[type](type, ifp);
430 if (ifp->if_l2com == NULL) {
437 IF_ADDR_LOCK_INIT(ifp);
438 TASK_INIT(&ifp->if_linktask, 0, do_link_state_change, ifp);
439 ifp->if_afdata_initialized = 0;
440 IF_AFDATA_LOCK_INIT(ifp);
441 TAILQ_INIT(&ifp->if_addrhead);
442 TAILQ_INIT(&ifp->if_multiaddrs);
443 TAILQ_INIT(&ifp->if_groups);
447 ifq_init(&ifp->if_snd, ifp);
449 refcount_init(&ifp->if_refcount, 1); /* Index reference. */
450 for (int i = 0; i < IFCOUNTERS; i++)
451 ifp->if_counters[i] = counter_u64_alloc(M_WAITOK);
452 ifp->if_get_counter = if_get_counter_default;
453 ifnet_setbyindex(ifp->if_index, ifp);
458 * Do the actual work of freeing a struct ifnet, and layer 2 common
459 * structure. This call is made when the last reference to an
460 * interface is released.
463 if_free_internal(struct ifnet *ifp)
466 KASSERT((ifp->if_flags & IFF_DYING),
467 ("if_free_internal: interface not dying"));
469 if (if_com_free[ifp->if_alloctype] != NULL)
470 if_com_free[ifp->if_alloctype](ifp->if_l2com,
474 mac_ifnet_destroy(ifp);
476 if (ifp->if_description != NULL)
477 free(ifp->if_description, M_IFDESCR);
478 IF_AFDATA_DESTROY(ifp);
479 IF_ADDR_LOCK_DESTROY(ifp);
480 ifq_delete(&ifp->if_snd);
482 for (int i = 0; i < IFCOUNTERS; i++)
483 counter_u64_free(ifp->if_counters[i]);
489 * Deregister an interface and free the associated storage.
492 if_free(struct ifnet *ifp)
495 ifp->if_flags |= IFF_DYING; /* XXX: Locking */
497 CURVNET_SET_QUIET(ifp->if_vnet);
499 KASSERT(ifp == ifnet_byindex_locked(ifp->if_index),
500 ("%s: freeing unallocated ifnet", ifp->if_xname));
502 ifindex_free_locked(ifp->if_index);
505 if (refcount_release(&ifp->if_refcount))
506 if_free_internal(ifp);
511 * Interfaces to keep an ifnet type-stable despite the possibility of the
512 * driver calling if_free(). If there are additional references, we defer
513 * freeing the underlying data structure.
516 if_ref(struct ifnet *ifp)
519 /* We don't assert the ifnet list lock here, but arguably should. */
520 refcount_acquire(&ifp->if_refcount);
524 if_rele(struct ifnet *ifp)
527 if (!refcount_release(&ifp->if_refcount))
529 if_free_internal(ifp);
533 ifq_init(struct ifaltq *ifq, struct ifnet *ifp)
536 mtx_init(&ifq->ifq_mtx, ifp->if_xname, "if send queue", MTX_DEF);
538 if (ifq->ifq_maxlen == 0)
539 ifq->ifq_maxlen = ifqmaxlen;
542 ifq->altq_disc = NULL;
543 ifq->altq_flags &= ALTQF_CANTCHANGE;
544 ifq->altq_tbr = NULL;
549 ifq_delete(struct ifaltq *ifq)
551 mtx_destroy(&ifq->ifq_mtx);
555 * Perform generic interface initalization tasks and attach the interface
556 * to the list of "active" interfaces. If vmove flag is set on entry
557 * to if_attach_internal(), perform only a limited subset of initialization
558 * tasks, given that we are moving from one vnet to another an ifnet which
559 * has already been fully initialized.
562 * - The decision to return void and thus require this function to
563 * succeed is questionable.
564 * - We should probably do more sanity checking. For instance we don't
565 * do anything to insure if_xname is unique or non-empty.
568 if_attach(struct ifnet *ifp)
571 if_attach_internal(ifp, 0);
575 * Compute the least common TSO limit.
578 if_hw_tsomax_common(if_t ifp, struct ifnet_hw_tsomax *pmax)
581 * 1) If there is no limit currently, take the limit from
582 * the network adapter.
584 * 2) If the network adapter has a limit below the current
587 if (pmax->tsomaxbytes == 0 || (ifp->if_hw_tsomax != 0 &&
588 ifp->if_hw_tsomax < pmax->tsomaxbytes)) {
589 pmax->tsomaxbytes = ifp->if_hw_tsomax;
591 if (pmax->tsomaxsegcount == 0 || (ifp->if_hw_tsomaxsegcount != 0 &&
592 ifp->if_hw_tsomaxsegcount < pmax->tsomaxsegcount)) {
593 pmax->tsomaxsegcount = ifp->if_hw_tsomaxsegcount;
595 if (pmax->tsomaxsegsize == 0 || (ifp->if_hw_tsomaxsegsize != 0 &&
596 ifp->if_hw_tsomaxsegsize < pmax->tsomaxsegsize)) {
597 pmax->tsomaxsegsize = ifp->if_hw_tsomaxsegsize;
602 * Update TSO limit of a network adapter.
604 * Returns zero if no change. Else non-zero.
607 if_hw_tsomax_update(if_t ifp, struct ifnet_hw_tsomax *pmax)
610 if (ifp->if_hw_tsomax != pmax->tsomaxbytes) {
611 ifp->if_hw_tsomax = pmax->tsomaxbytes;
614 if (ifp->if_hw_tsomaxsegsize != pmax->tsomaxsegsize) {
615 ifp->if_hw_tsomaxsegsize = pmax->tsomaxsegsize;
618 if (ifp->if_hw_tsomaxsegcount != pmax->tsomaxsegcount) {
619 ifp->if_hw_tsomaxsegcount = pmax->tsomaxsegcount;
626 if_attach_internal(struct ifnet *ifp, int vmove)
628 unsigned socksize, ifasize;
629 int namelen, masklen;
630 struct sockaddr_dl *sdl;
633 if (ifp->if_index == 0 || ifp != ifnet_byindex(ifp->if_index))
634 panic ("%s: BUG: if_attach called without if_alloc'd input()\n",
638 ifp->if_vnet = curvnet;
639 if (ifp->if_home_vnet == NULL)
640 ifp->if_home_vnet = curvnet;
643 if_addgroup(ifp, IFG_ALL);
645 getmicrotime(&ifp->if_lastchange);
646 ifp->if_epoch = time_uptime;
648 KASSERT((ifp->if_transmit == NULL && ifp->if_qflush == NULL) ||
649 (ifp->if_transmit != NULL && ifp->if_qflush != NULL),
650 ("transmit and qflush must both either be set or both be NULL"));
651 if (ifp->if_transmit == NULL) {
652 ifp->if_transmit = if_transmit;
653 ifp->if_qflush = if_qflush;
658 mac_ifnet_create(ifp);
662 * Create a Link Level name for this device.
664 namelen = strlen(ifp->if_xname);
666 * Always save enough space for any possiable name so we
667 * can do a rename in place later.
669 masklen = offsetof(struct sockaddr_dl, sdl_data[0]) + IFNAMSIZ;
670 socksize = masklen + ifp->if_addrlen;
671 if (socksize < sizeof(*sdl))
672 socksize = sizeof(*sdl);
673 socksize = roundup2(socksize, sizeof(long));
674 ifasize = sizeof(*ifa) + 2 * socksize;
675 ifa = ifa_alloc(ifasize, M_WAITOK);
676 sdl = (struct sockaddr_dl *)(ifa + 1);
677 sdl->sdl_len = socksize;
678 sdl->sdl_family = AF_LINK;
679 bcopy(ifp->if_xname, sdl->sdl_data, namelen);
680 sdl->sdl_nlen = namelen;
681 sdl->sdl_index = ifp->if_index;
682 sdl->sdl_type = ifp->if_type;
685 ifa->ifa_rtrequest = link_rtrequest;
686 ifa->ifa_addr = (struct sockaddr *)sdl;
687 sdl = (struct sockaddr_dl *)(socksize + (caddr_t)sdl);
688 ifa->ifa_netmask = (struct sockaddr *)sdl;
689 sdl->sdl_len = masklen;
691 sdl->sdl_data[--namelen] = 0xff;
692 TAILQ_INSERT_HEAD(&ifp->if_addrhead, ifa, ifa_link);
693 /* Reliably crash if used uninitialized. */
694 ifp->if_broadcastaddr = NULL;
696 #if defined(INET) || defined(INET6)
697 /* Use defaults for TSO, if nothing is set */
698 if (ifp->if_hw_tsomax == 0 &&
699 ifp->if_hw_tsomaxsegcount == 0 &&
700 ifp->if_hw_tsomaxsegsize == 0) {
702 * The TSO defaults needs to be such that an
703 * NFS mbuf list of 35 mbufs totalling just
704 * below 64K works and that a chain of mbufs
705 * can be defragged into at most 32 segments:
707 ifp->if_hw_tsomax = min(IP_MAXPACKET, (32 * MCLBYTES) -
708 (ETHER_HDR_LEN + ETHER_VLAN_ENCAP_LEN));
709 ifp->if_hw_tsomaxsegcount = 35;
710 ifp->if_hw_tsomaxsegsize = 2048; /* 2K */
712 /* XXX some drivers set IFCAP_TSO after ethernet attach */
713 if (ifp->if_capabilities & IFCAP_TSO) {
714 if_printf(ifp, "Using defaults for TSO: %u/%u/%u\n",
716 ifp->if_hw_tsomaxsegcount,
717 ifp->if_hw_tsomaxsegsize);
725 * Update the interface index in the link layer address
728 for (ifa = ifp->if_addr; ifa != NULL;
729 ifa = TAILQ_NEXT(ifa, ifa_link)) {
730 if (ifa->ifa_addr->sa_family == AF_LINK) {
731 sdl = (struct sockaddr_dl *)ifa->ifa_addr;
732 sdl->sdl_index = ifp->if_index;
739 TAILQ_INSERT_TAIL(&V_ifnet, ifp, if_link);
741 curvnet->vnet_ifcnt++;
745 if (domain_init_status >= 2)
746 if_attachdomain1(ifp);
748 EVENTHANDLER_INVOKE(ifnet_arrival_event, ifp);
749 if (IS_DEFAULT_VNET(curvnet))
750 devctl_notify("IFNET", ifp->if_xname, "ATTACH", NULL);
752 /* Announce the interface. */
753 rt_ifannouncemsg(ifp, IFAN_ARRIVAL);
757 if_attachdomain(void *dummy)
761 TAILQ_FOREACH(ifp, &V_ifnet, if_link)
762 if_attachdomain1(ifp);
764 SYSINIT(domainifattach, SI_SUB_PROTO_IFATTACHDOMAIN, SI_ORDER_SECOND,
765 if_attachdomain, NULL);
768 if_attachdomain1(struct ifnet *ifp)
773 * Since dp->dom_ifattach calls malloc() with M_WAITOK, we
774 * cannot lock ifp->if_afdata initialization, entirely.
776 if (IF_AFDATA_TRYLOCK(ifp) == 0)
778 if (ifp->if_afdata_initialized >= domain_init_status) {
779 IF_AFDATA_UNLOCK(ifp);
780 log(LOG_WARNING, "%s called more than once on %s\n",
781 __func__, ifp->if_xname);
784 ifp->if_afdata_initialized = domain_init_status;
785 IF_AFDATA_UNLOCK(ifp);
787 /* address family dependent data region */
788 bzero(ifp->if_afdata, sizeof(ifp->if_afdata));
789 for (dp = domains; dp; dp = dp->dom_next) {
790 if (dp->dom_ifattach)
791 ifp->if_afdata[dp->dom_family] =
792 (*dp->dom_ifattach)(ifp);
797 * Remove any unicast or broadcast network addresses from an interface.
800 if_purgeaddrs(struct ifnet *ifp)
802 struct ifaddr *ifa, *next;
804 TAILQ_FOREACH_SAFE(ifa, &ifp->if_addrhead, ifa_link, next) {
805 if (ifa->ifa_addr->sa_family == AF_LINK)
808 /* XXX: Ugly!! ad hoc just for INET */
809 if (ifa->ifa_addr->sa_family == AF_INET) {
810 struct ifaliasreq ifr;
812 bzero(&ifr, sizeof(ifr));
813 ifr.ifra_addr = *ifa->ifa_addr;
814 if (ifa->ifa_dstaddr)
815 ifr.ifra_broadaddr = *ifa->ifa_dstaddr;
816 if (in_control(NULL, SIOCDIFADDR, (caddr_t)&ifr, ifp,
822 if (ifa->ifa_addr->sa_family == AF_INET6) {
824 /* ifp_addrhead is already updated */
828 TAILQ_REMOVE(&ifp->if_addrhead, ifa, ifa_link);
834 * Remove any multicast network addresses from an interface when an ifnet
838 if_purgemaddrs(struct ifnet *ifp)
840 struct ifmultiaddr *ifma;
841 struct ifmultiaddr *next;
844 TAILQ_FOREACH_SAFE(ifma, &ifp->if_multiaddrs, ifma_link, next)
845 if_delmulti_locked(ifp, ifma, 1);
846 IF_ADDR_WUNLOCK(ifp);
850 * Detach an interface, removing it from the list of "active" interfaces.
851 * If vmove flag is set on entry to if_detach_internal(), perform only a
852 * limited subset of cleanup tasks, given that we are moving an ifnet from
853 * one vnet to another, where it must be fully operational.
855 * XXXRW: There are some significant questions about event ordering, and
856 * how to prevent things from starting to use the interface during detach.
859 if_detach(struct ifnet *ifp)
862 CURVNET_SET_QUIET(ifp->if_vnet);
863 if_detach_internal(ifp, 0);
868 if_detach_internal(struct ifnet *ifp, int vmove)
871 struct radix_node_head *rnh;
878 TAILQ_FOREACH(iter, &V_ifnet, if_link)
880 TAILQ_REMOVE(&V_ifnet, ifp, if_link);
886 curvnet->vnet_ifcnt--;
891 panic("%s: ifp=%p not on the ifnet tailq %p",
892 __func__, ifp, &V_ifnet);
894 return; /* XXX this should panic as well? */
898 * Remove/wait for pending events.
900 taskqueue_drain(taskqueue_swi, &ifp->if_linktask);
903 * Remove routes and flush queues.
907 if (ALTQ_IS_ENABLED(&ifp->if_snd))
908 altq_disable(&ifp->if_snd);
909 if (ALTQ_IS_ATTACHED(&ifp->if_snd))
910 altq_detach(&ifp->if_snd);
921 * Remove all IPv6 kernel structs related to ifp. This should be done
922 * before removing routing entries below, since IPv6 interface direct
923 * routes are expected to be removed by the IPv6-specific kernel API.
924 * Otherwise, the kernel will detect some inconsistency and bark it.
930 /* Announce that the interface is gone. */
931 rt_ifannouncemsg(ifp, IFAN_DEPARTURE);
932 EVENTHANDLER_INVOKE(ifnet_departure_event, ifp);
933 if (IS_DEFAULT_VNET(curvnet))
934 devctl_notify("IFNET", ifp->if_xname, "DETACH", NULL);
938 * Prevent further calls into the device driver via ifnet.
943 * Remove link ifaddr pointer and maybe decrement if_index.
944 * Clean up all addresses.
948 /* We can now free link ifaddr. */
949 if (!TAILQ_EMPTY(&ifp->if_addrhead)) {
950 ifa = TAILQ_FIRST(&ifp->if_addrhead);
951 TAILQ_REMOVE(&ifp->if_addrhead, ifa, ifa_link);
957 * Delete all remaining routes using this interface
958 * Unfortuneatly the only way to do this is to slog through
959 * the entire routing table looking for routes which point
960 * to this interface...oh well...
962 for (i = 1; i <= AF_MAX; i++) {
963 for (j = 0; j < rt_numfibs; j++) {
964 rnh = rt_tables_get_rnh(j, i);
967 RADIX_NODE_HEAD_LOCK(rnh);
968 (void) rnh->rnh_walktree(rnh, if_rtdel, ifp);
969 RADIX_NODE_HEAD_UNLOCK(rnh);
976 * We cannot hold the lock over dom_ifdetach calls as they might
977 * sleep, for example trying to drain a callout, thus open up the
978 * theoretical race with re-attaching.
981 i = ifp->if_afdata_initialized;
982 ifp->if_afdata_initialized = 0;
983 IF_AFDATA_UNLOCK(ifp);
984 for (dp = domains; i > 0 && dp; dp = dp->dom_next) {
985 if (dp->dom_ifdetach && ifp->if_afdata[dp->dom_family])
986 (*dp->dom_ifdetach)(ifp,
987 ifp->if_afdata[dp->dom_family]);
993 * if_vmove() performs a limited version of if_detach() in current
994 * vnet and if_attach()es the ifnet to the vnet specified as 2nd arg.
995 * An attempt is made to shrink if_index in current vnet, find an
996 * unused if_index in target vnet and calls if_grow() if necessary,
997 * and finally find an unused if_xname for the target vnet.
1000 if_vmove(struct ifnet *ifp, struct vnet *new_vnet)
1004 * Detach from current vnet, but preserve LLADDR info, do not
1005 * mark as dead etc. so that the ifnet can be reattached later.
1007 if_detach_internal(ifp, 1);
1010 * Unlink the ifnet from ifindex_table[] in current vnet, and shrink
1011 * the if_index for that vnet if possible.
1013 * NOTE: IFNET_WLOCK/IFNET_WUNLOCK() are assumed to be unvirtualized,
1014 * or we'd lock on one vnet and unlock on another.
1017 ifindex_free_locked(ifp->if_index);
1021 * Perform interface-specific reassignment tasks, if provided by
1024 if (ifp->if_reassign != NULL)
1025 ifp->if_reassign(ifp, new_vnet, NULL);
1028 * Switch to the context of the target vnet.
1030 CURVNET_SET_QUIET(new_vnet);
1033 ifp->if_index = ifindex_alloc();
1034 ifnet_setbyindex_locked(ifp->if_index, ifp);
1037 if_attach_internal(ifp, 1);
1043 * Move an ifnet to or from another child prison/vnet, specified by the jail id.
1046 if_vmove_loan(struct thread *td, struct ifnet *ifp, char *ifname, int jid)
1051 /* Try to find the prison within our visibility. */
1052 sx_slock(&allprison_lock);
1053 pr = prison_find_child(td->td_ucred->cr_prison, jid);
1054 sx_sunlock(&allprison_lock);
1057 prison_hold_locked(pr);
1058 mtx_unlock(&pr->pr_mtx);
1060 /* Do not try to move the iface from and to the same prison. */
1061 if (pr->pr_vnet == ifp->if_vnet) {
1066 /* Make sure the named iface does not exists in the dst. prison/vnet. */
1067 /* XXX Lock interfaces to avoid races. */
1068 CURVNET_SET_QUIET(pr->pr_vnet);
1069 difp = ifunit(ifname);
1076 /* Move the interface into the child jail/vnet. */
1077 if_vmove(ifp, pr->pr_vnet);
1079 /* Report the new if_xname back to the userland. */
1080 sprintf(ifname, "%s", ifp->if_xname);
1087 if_vmove_reclaim(struct thread *td, char *ifname, int jid)
1090 struct vnet *vnet_dst;
1093 /* Try to find the prison within our visibility. */
1094 sx_slock(&allprison_lock);
1095 pr = prison_find_child(td->td_ucred->cr_prison, jid);
1096 sx_sunlock(&allprison_lock);
1099 prison_hold_locked(pr);
1100 mtx_unlock(&pr->pr_mtx);
1102 /* Make sure the named iface exists in the source prison/vnet. */
1103 CURVNET_SET(pr->pr_vnet);
1104 ifp = ifunit(ifname); /* XXX Lock to avoid races. */
1111 /* Do not try to move the iface from and to the same prison. */
1112 vnet_dst = TD_TO_VNET(td);
1113 if (vnet_dst == ifp->if_vnet) {
1119 /* Get interface back from child jail/vnet. */
1120 if_vmove(ifp, vnet_dst);
1123 /* Report the new if_xname back to the userland. */
1124 sprintf(ifname, "%s", ifp->if_xname);
1132 * Add a group to an interface
1135 if_addgroup(struct ifnet *ifp, const char *groupname)
1137 struct ifg_list *ifgl;
1138 struct ifg_group *ifg = NULL;
1139 struct ifg_member *ifgm;
1142 if (groupname[0] && groupname[strlen(groupname) - 1] >= '0' &&
1143 groupname[strlen(groupname) - 1] <= '9')
1147 TAILQ_FOREACH(ifgl, &ifp->if_groups, ifgl_next)
1148 if (!strcmp(ifgl->ifgl_group->ifg_group, groupname)) {
1153 if ((ifgl = (struct ifg_list *)malloc(sizeof(struct ifg_list), M_TEMP,
1154 M_NOWAIT)) == NULL) {
1159 if ((ifgm = (struct ifg_member *)malloc(sizeof(struct ifg_member),
1160 M_TEMP, M_NOWAIT)) == NULL) {
1166 TAILQ_FOREACH(ifg, &V_ifg_head, ifg_next)
1167 if (!strcmp(ifg->ifg_group, groupname))
1171 if ((ifg = (struct ifg_group *)malloc(sizeof(struct ifg_group),
1172 M_TEMP, M_NOWAIT)) == NULL) {
1178 strlcpy(ifg->ifg_group, groupname, sizeof(ifg->ifg_group));
1179 ifg->ifg_refcnt = 0;
1180 TAILQ_INIT(&ifg->ifg_members);
1181 TAILQ_INSERT_TAIL(&V_ifg_head, ifg, ifg_next);
1186 ifgl->ifgl_group = ifg;
1187 ifgm->ifgm_ifp = ifp;
1190 TAILQ_INSERT_TAIL(&ifg->ifg_members, ifgm, ifgm_next);
1191 TAILQ_INSERT_TAIL(&ifp->if_groups, ifgl, ifgl_next);
1192 IF_ADDR_WUNLOCK(ifp);
1197 EVENTHANDLER_INVOKE(group_attach_event, ifg);
1198 EVENTHANDLER_INVOKE(group_change_event, groupname);
1204 * Remove a group from an interface
1207 if_delgroup(struct ifnet *ifp, const char *groupname)
1209 struct ifg_list *ifgl;
1210 struct ifg_member *ifgm;
1213 TAILQ_FOREACH(ifgl, &ifp->if_groups, ifgl_next)
1214 if (!strcmp(ifgl->ifgl_group->ifg_group, groupname))
1222 TAILQ_REMOVE(&ifp->if_groups, ifgl, ifgl_next);
1223 IF_ADDR_WUNLOCK(ifp);
1225 TAILQ_FOREACH(ifgm, &ifgl->ifgl_group->ifg_members, ifgm_next)
1226 if (ifgm->ifgm_ifp == ifp)
1230 TAILQ_REMOVE(&ifgl->ifgl_group->ifg_members, ifgm, ifgm_next);
1234 if (--ifgl->ifgl_group->ifg_refcnt == 0) {
1235 TAILQ_REMOVE(&V_ifg_head, ifgl->ifgl_group, ifg_next);
1237 EVENTHANDLER_INVOKE(group_detach_event, ifgl->ifgl_group);
1238 free(ifgl->ifgl_group, M_TEMP);
1244 EVENTHANDLER_INVOKE(group_change_event, groupname);
1250 * Remove an interface from all groups
1253 if_delgroups(struct ifnet *ifp)
1255 struct ifg_list *ifgl;
1256 struct ifg_member *ifgm;
1257 char groupname[IFNAMSIZ];
1260 while (!TAILQ_EMPTY(&ifp->if_groups)) {
1261 ifgl = TAILQ_FIRST(&ifp->if_groups);
1263 strlcpy(groupname, ifgl->ifgl_group->ifg_group, IFNAMSIZ);
1266 TAILQ_REMOVE(&ifp->if_groups, ifgl, ifgl_next);
1267 IF_ADDR_WUNLOCK(ifp);
1269 TAILQ_FOREACH(ifgm, &ifgl->ifgl_group->ifg_members, ifgm_next)
1270 if (ifgm->ifgm_ifp == ifp)
1274 TAILQ_REMOVE(&ifgl->ifgl_group->ifg_members, ifgm,
1279 if (--ifgl->ifgl_group->ifg_refcnt == 0) {
1280 TAILQ_REMOVE(&V_ifg_head, ifgl->ifgl_group, ifg_next);
1282 EVENTHANDLER_INVOKE(group_detach_event,
1284 free(ifgl->ifgl_group, M_TEMP);
1290 EVENTHANDLER_INVOKE(group_change_event, groupname);
1298 * Stores all groups from an interface in memory pointed
1302 if_getgroup(struct ifgroupreq *data, struct ifnet *ifp)
1305 struct ifg_list *ifgl;
1306 struct ifg_req ifgrq, *ifgp;
1307 struct ifgroupreq *ifgr = data;
1309 if (ifgr->ifgr_len == 0) {
1311 TAILQ_FOREACH(ifgl, &ifp->if_groups, ifgl_next)
1312 ifgr->ifgr_len += sizeof(struct ifg_req);
1313 IF_ADDR_RUNLOCK(ifp);
1317 len = ifgr->ifgr_len;
1318 ifgp = ifgr->ifgr_groups;
1321 TAILQ_FOREACH(ifgl, &ifp->if_groups, ifgl_next) {
1322 if (len < sizeof(ifgrq)) {
1323 IF_ADDR_RUNLOCK(ifp);
1326 bzero(&ifgrq, sizeof ifgrq);
1327 strlcpy(ifgrq.ifgrq_group, ifgl->ifgl_group->ifg_group,
1328 sizeof(ifgrq.ifgrq_group));
1329 if ((error = copyout(&ifgrq, ifgp, sizeof(struct ifg_req)))) {
1330 IF_ADDR_RUNLOCK(ifp);
1333 len -= sizeof(ifgrq);
1336 IF_ADDR_RUNLOCK(ifp);
1342 * Stores all members of a group in memory pointed to by data
1345 if_getgroupmembers(struct ifgroupreq *data)
1347 struct ifgroupreq *ifgr = data;
1348 struct ifg_group *ifg;
1349 struct ifg_member *ifgm;
1350 struct ifg_req ifgrq, *ifgp;
1354 TAILQ_FOREACH(ifg, &V_ifg_head, ifg_next)
1355 if (!strcmp(ifg->ifg_group, ifgr->ifgr_name))
1362 if (ifgr->ifgr_len == 0) {
1363 TAILQ_FOREACH(ifgm, &ifg->ifg_members, ifgm_next)
1364 ifgr->ifgr_len += sizeof(ifgrq);
1369 len = ifgr->ifgr_len;
1370 ifgp = ifgr->ifgr_groups;
1371 TAILQ_FOREACH(ifgm, &ifg->ifg_members, ifgm_next) {
1372 if (len < sizeof(ifgrq)) {
1376 bzero(&ifgrq, sizeof ifgrq);
1377 strlcpy(ifgrq.ifgrq_member, ifgm->ifgm_ifp->if_xname,
1378 sizeof(ifgrq.ifgrq_member));
1379 if ((error = copyout(&ifgrq, ifgp, sizeof(struct ifg_req)))) {
1383 len -= sizeof(ifgrq);
1392 * Delete Routes for a Network Interface
1394 * Called for each routing entry via the rnh->rnh_walktree() call above
1395 * to delete all route entries referencing a detaching network interface.
1398 * rn pointer to node in the routing table
1399 * arg argument passed to rnh->rnh_walktree() - detaching interface
1403 * errno failed - reason indicated
1407 if_rtdel(struct radix_node *rn, void *arg)
1409 struct rtentry *rt = (struct rtentry *)rn;
1410 struct ifnet *ifp = arg;
1413 if (rt->rt_ifp == ifp) {
1416 * Protect (sorta) against walktree recursion problems
1417 * with cloned routes
1419 if ((rt->rt_flags & RTF_UP) == 0)
1422 err = rtrequest_fib(RTM_DELETE, rt_key(rt), rt->rt_gateway,
1424 rt->rt_flags|RTF_RNH_LOCKED|RTF_PINNED,
1425 (struct rtentry **) NULL, rt->rt_fibnum);
1427 log(LOG_WARNING, "if_rtdel: error %d\n", err);
1435 * Return counter values from counter(9)s stored in ifnet.
1438 if_get_counter_default(struct ifnet *ifp, ift_counter cnt)
1441 KASSERT(cnt < IFCOUNTERS, ("%s: invalid cnt %d", __func__, cnt));
1443 return (counter_u64_fetch(ifp->if_counters[cnt]));
1447 * Increase an ifnet counter. Usually used for counters shared
1448 * between the stack and a driver, but function supports them all.
1451 if_inc_counter(struct ifnet *ifp, ift_counter cnt, int64_t inc)
1454 KASSERT(cnt < IFCOUNTERS, ("%s: invalid cnt %d", __func__, cnt));
1456 counter_u64_add(ifp->if_counters[cnt], inc);
1460 * Copy data from ifnet to userland API structure if_data.
1463 if_data_copy(struct ifnet *ifp, struct if_data *ifd)
1466 ifd->ifi_type = ifp->if_type;
1467 ifd->ifi_physical = 0;
1468 ifd->ifi_addrlen = ifp->if_addrlen;
1469 ifd->ifi_hdrlen = ifp->if_hdrlen;
1470 ifd->ifi_link_state = ifp->if_link_state;
1472 ifd->ifi_datalen = sizeof(struct if_data);
1473 ifd->ifi_mtu = ifp->if_mtu;
1474 ifd->ifi_metric = ifp->if_metric;
1475 ifd->ifi_baudrate = ifp->if_baudrate;
1476 ifd->ifi_hwassist = ifp->if_hwassist;
1477 ifd->ifi_epoch = ifp->if_epoch;
1478 ifd->ifi_lastchange = ifp->if_lastchange;
1480 ifd->ifi_ipackets = ifp->if_get_counter(ifp, IFCOUNTER_IPACKETS);
1481 ifd->ifi_ierrors = ifp->if_get_counter(ifp, IFCOUNTER_IERRORS);
1482 ifd->ifi_opackets = ifp->if_get_counter(ifp, IFCOUNTER_OPACKETS);
1483 ifd->ifi_oerrors = ifp->if_get_counter(ifp, IFCOUNTER_OERRORS);
1484 ifd->ifi_collisions = ifp->if_get_counter(ifp, IFCOUNTER_COLLISIONS);
1485 ifd->ifi_ibytes = ifp->if_get_counter(ifp, IFCOUNTER_IBYTES);
1486 ifd->ifi_obytes = ifp->if_get_counter(ifp, IFCOUNTER_OBYTES);
1487 ifd->ifi_imcasts = ifp->if_get_counter(ifp, IFCOUNTER_IMCASTS);
1488 ifd->ifi_omcasts = ifp->if_get_counter(ifp, IFCOUNTER_OMCASTS);
1489 ifd->ifi_iqdrops = ifp->if_get_counter(ifp, IFCOUNTER_IQDROPS);
1490 ifd->ifi_oqdrops = ifp->if_get_counter(ifp, IFCOUNTER_OQDROPS);
1491 ifd->ifi_noproto = ifp->if_get_counter(ifp, IFCOUNTER_NOPROTO);
1495 * Wrapper functions for struct ifnet address list locking macros. These are
1496 * used by kernel modules to avoid encoding programming interface or binary
1497 * interface assumptions that may be violated when kernel-internal locking
1498 * approaches change.
1501 if_addr_rlock(struct ifnet *ifp)
1508 if_addr_runlock(struct ifnet *ifp)
1511 IF_ADDR_RUNLOCK(ifp);
1515 if_maddr_rlock(if_t ifp)
1518 IF_ADDR_RLOCK((struct ifnet *)ifp);
1522 if_maddr_runlock(if_t ifp)
1525 IF_ADDR_RUNLOCK((struct ifnet *)ifp);
1529 * Initialization, destruction and refcounting functions for ifaddrs.
1532 ifa_alloc(size_t size, int flags)
1536 KASSERT(size >= sizeof(struct ifaddr),
1537 ("%s: invalid size %zu", __func__, size));
1539 ifa = malloc(size, M_IFADDR, M_ZERO | flags);
1543 if ((ifa->ifa_opackets = counter_u64_alloc(flags)) == NULL)
1545 if ((ifa->ifa_ipackets = counter_u64_alloc(flags)) == NULL)
1547 if ((ifa->ifa_obytes = counter_u64_alloc(flags)) == NULL)
1549 if ((ifa->ifa_ibytes = counter_u64_alloc(flags)) == NULL)
1552 refcount_init(&ifa->ifa_refcnt, 1);
1557 /* free(NULL) is okay */
1558 counter_u64_free(ifa->ifa_opackets);
1559 counter_u64_free(ifa->ifa_ipackets);
1560 counter_u64_free(ifa->ifa_obytes);
1561 counter_u64_free(ifa->ifa_ibytes);
1562 free(ifa, M_IFADDR);
1568 ifa_ref(struct ifaddr *ifa)
1571 refcount_acquire(&ifa->ifa_refcnt);
1575 ifa_free(struct ifaddr *ifa)
1578 if (refcount_release(&ifa->ifa_refcnt)) {
1579 counter_u64_free(ifa->ifa_opackets);
1580 counter_u64_free(ifa->ifa_ipackets);
1581 counter_u64_free(ifa->ifa_obytes);
1582 counter_u64_free(ifa->ifa_ibytes);
1583 free(ifa, M_IFADDR);
1588 ifa_add_loopback_route(struct ifaddr *ifa, struct sockaddr *ia)
1591 struct rtentry *rt = NULL;
1592 struct rt_addrinfo info;
1593 static struct sockaddr_dl null_sdl = {sizeof(null_sdl), AF_LINK};
1595 bzero(&info, sizeof(info));
1596 info.rti_ifp = V_loif;
1597 info.rti_flags = ifa->ifa_flags | RTF_HOST | RTF_STATIC;
1598 info.rti_info[RTAX_DST] = ia;
1599 info.rti_info[RTAX_GATEWAY] = (struct sockaddr *)&null_sdl;
1600 error = rtrequest1_fib(RTM_ADD, &info, &rt, ifa->ifa_ifp->if_fib);
1602 if (error == 0 && rt != NULL) {
1604 ((struct sockaddr_dl *)rt->rt_gateway)->sdl_type =
1605 ifa->ifa_ifp->if_type;
1606 ((struct sockaddr_dl *)rt->rt_gateway)->sdl_index =
1607 ifa->ifa_ifp->if_index;
1610 } else if (error != 0)
1611 log(LOG_DEBUG, "%s: insertion failed: %u\n", __func__, error);
1617 ifa_del_loopback_route(struct ifaddr *ifa, struct sockaddr *ia)
1620 struct rt_addrinfo info;
1621 struct sockaddr_dl null_sdl;
1623 bzero(&null_sdl, sizeof(null_sdl));
1624 null_sdl.sdl_len = sizeof(null_sdl);
1625 null_sdl.sdl_family = AF_LINK;
1626 null_sdl.sdl_type = ifa->ifa_ifp->if_type;
1627 null_sdl.sdl_index = ifa->ifa_ifp->if_index;
1628 bzero(&info, sizeof(info));
1629 info.rti_flags = ifa->ifa_flags | RTF_HOST | RTF_STATIC;
1630 info.rti_info[RTAX_DST] = ia;
1631 info.rti_info[RTAX_GATEWAY] = (struct sockaddr *)&null_sdl;
1632 error = rtrequest1_fib(RTM_DELETE, &info, NULL, ifa->ifa_ifp->if_fib);
1635 log(LOG_DEBUG, "%s: deletion failed: %u\n", __func__, error);
1641 ifa_switch_loopback_route(struct ifaddr *ifa, struct sockaddr *sa, int fib)
1645 rt = rtalloc1_fib(sa, 0, 0, fib);
1647 log(LOG_DEBUG, "%s: fail", __func__);
1648 return (EHOSTUNREACH);
1650 ((struct sockaddr_dl *)rt->rt_gateway)->sdl_type =
1651 ifa->ifa_ifp->if_type;
1652 ((struct sockaddr_dl *)rt->rt_gateway)->sdl_index =
1653 ifa->ifa_ifp->if_index;
1660 * XXX: Because sockaddr_dl has deeper structure than the sockaddr
1661 * structs used to represent other address families, it is necessary
1662 * to perform a different comparison.
1665 #define sa_dl_equal(a1, a2) \
1666 ((((struct sockaddr_dl *)(a1))->sdl_len == \
1667 ((struct sockaddr_dl *)(a2))->sdl_len) && \
1668 (bcmp(LLADDR((struct sockaddr_dl *)(a1)), \
1669 LLADDR((struct sockaddr_dl *)(a2)), \
1670 ((struct sockaddr_dl *)(a1))->sdl_alen) == 0))
1673 * Locate an interface based on a complete address.
1676 static struct ifaddr *
1677 ifa_ifwithaddr_internal(struct sockaddr *addr, int getref)
1682 IFNET_RLOCK_NOSLEEP();
1683 TAILQ_FOREACH(ifp, &V_ifnet, if_link) {
1685 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
1686 if (ifa->ifa_addr->sa_family != addr->sa_family)
1688 if (sa_equal(addr, ifa->ifa_addr)) {
1691 IF_ADDR_RUNLOCK(ifp);
1694 /* IP6 doesn't have broadcast */
1695 if ((ifp->if_flags & IFF_BROADCAST) &&
1696 ifa->ifa_broadaddr &&
1697 ifa->ifa_broadaddr->sa_len != 0 &&
1698 sa_equal(ifa->ifa_broadaddr, addr)) {
1701 IF_ADDR_RUNLOCK(ifp);
1705 IF_ADDR_RUNLOCK(ifp);
1709 IFNET_RUNLOCK_NOSLEEP();
1714 ifa_ifwithaddr(struct sockaddr *addr)
1717 return (ifa_ifwithaddr_internal(addr, 1));
1721 ifa_ifwithaddr_check(struct sockaddr *addr)
1724 return (ifa_ifwithaddr_internal(addr, 0) != NULL);
1728 * Locate an interface based on the broadcast address.
1732 ifa_ifwithbroadaddr(struct sockaddr *addr, int fibnum)
1737 IFNET_RLOCK_NOSLEEP();
1738 TAILQ_FOREACH(ifp, &V_ifnet, if_link) {
1739 if ((fibnum != RT_ALL_FIBS) && (ifp->if_fib != fibnum))
1742 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
1743 if (ifa->ifa_addr->sa_family != addr->sa_family)
1745 if ((ifp->if_flags & IFF_BROADCAST) &&
1746 ifa->ifa_broadaddr &&
1747 ifa->ifa_broadaddr->sa_len != 0 &&
1748 sa_equal(ifa->ifa_broadaddr, addr)) {
1750 IF_ADDR_RUNLOCK(ifp);
1754 IF_ADDR_RUNLOCK(ifp);
1758 IFNET_RUNLOCK_NOSLEEP();
1763 * Locate the point to point interface with a given destination address.
1767 ifa_ifwithdstaddr(struct sockaddr *addr, int fibnum)
1772 IFNET_RLOCK_NOSLEEP();
1773 TAILQ_FOREACH(ifp, &V_ifnet, if_link) {
1774 if ((ifp->if_flags & IFF_POINTOPOINT) == 0)
1776 if ((fibnum != RT_ALL_FIBS) && (ifp->if_fib != fibnum))
1779 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
1780 if (ifa->ifa_addr->sa_family != addr->sa_family)
1782 if (ifa->ifa_dstaddr != NULL &&
1783 sa_equal(addr, ifa->ifa_dstaddr)) {
1785 IF_ADDR_RUNLOCK(ifp);
1789 IF_ADDR_RUNLOCK(ifp);
1793 IFNET_RUNLOCK_NOSLEEP();
1798 * Find an interface on a specific network. If many, choice
1799 * is most specific found.
1802 ifa_ifwithnet(struct sockaddr *addr, int ignore_ptp, int fibnum)
1806 struct ifaddr *ifa_maybe = NULL;
1807 u_int af = addr->sa_family;
1808 char *addr_data = addr->sa_data, *cplim;
1811 * AF_LINK addresses can be looked up directly by their index number,
1812 * so do that if we can.
1814 if (af == AF_LINK) {
1815 struct sockaddr_dl *sdl = (struct sockaddr_dl *)addr;
1816 if (sdl->sdl_index && sdl->sdl_index <= V_if_index)
1817 return (ifaddr_byindex(sdl->sdl_index));
1821 * Scan though each interface, looking for ones that have addresses
1822 * in this address family and the requested fib. Maintain a reference
1823 * on ifa_maybe once we find one, as we release the IF_ADDR_RLOCK() that
1824 * kept it stable when we move onto the next interface.
1826 IFNET_RLOCK_NOSLEEP();
1827 TAILQ_FOREACH(ifp, &V_ifnet, if_link) {
1828 if ((fibnum != RT_ALL_FIBS) && (ifp->if_fib != fibnum))
1831 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
1832 char *cp, *cp2, *cp3;
1834 if (ifa->ifa_addr->sa_family != af)
1836 if (af == AF_INET &&
1837 ifp->if_flags & IFF_POINTOPOINT && !ignore_ptp) {
1839 * This is a bit broken as it doesn't
1840 * take into account that the remote end may
1841 * be a single node in the network we are
1843 * The trouble is that we don't know the
1844 * netmask for the remote end.
1846 if (ifa->ifa_dstaddr != NULL &&
1847 sa_equal(addr, ifa->ifa_dstaddr)) {
1849 IF_ADDR_RUNLOCK(ifp);
1854 * Scan all the bits in the ifa's address.
1855 * If a bit dissagrees with what we are
1856 * looking for, mask it with the netmask
1857 * to see if it really matters.
1858 * (A byte at a time)
1860 if (ifa->ifa_netmask == 0)
1863 cp2 = ifa->ifa_addr->sa_data;
1864 cp3 = ifa->ifa_netmask->sa_data;
1865 cplim = ifa->ifa_netmask->sa_len
1866 + (char *)ifa->ifa_netmask;
1868 if ((*cp++ ^ *cp2++) & *cp3++)
1869 goto next; /* next address! */
1871 * If the netmask of what we just found
1872 * is more specific than what we had before
1873 * (if we had one), or if the virtual status
1874 * of new prefix is better than of the old one,
1875 * then remember the new one before continuing
1876 * to search for an even better one.
1878 if (ifa_maybe == NULL ||
1879 ifa_preferred(ifa_maybe, ifa) ||
1880 rn_refines((caddr_t)ifa->ifa_netmask,
1881 (caddr_t)ifa_maybe->ifa_netmask)) {
1882 if (ifa_maybe != NULL)
1883 ifa_free(ifa_maybe);
1889 IF_ADDR_RUNLOCK(ifp);
1894 IFNET_RUNLOCK_NOSLEEP();
1895 if (ifa_maybe != NULL)
1896 ifa_free(ifa_maybe);
1901 * Find an interface address specific to an interface best matching
1905 ifaof_ifpforaddr(struct sockaddr *addr, struct ifnet *ifp)
1908 char *cp, *cp2, *cp3;
1910 struct ifaddr *ifa_maybe = NULL;
1911 u_int af = addr->sa_family;
1916 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
1917 if (ifa->ifa_addr->sa_family != af)
1919 if (ifa_maybe == NULL)
1921 if (ifa->ifa_netmask == 0) {
1922 if (sa_equal(addr, ifa->ifa_addr) ||
1923 (ifa->ifa_dstaddr &&
1924 sa_equal(addr, ifa->ifa_dstaddr)))
1928 if (ifp->if_flags & IFF_POINTOPOINT) {
1929 if (sa_equal(addr, ifa->ifa_dstaddr))
1933 cp2 = ifa->ifa_addr->sa_data;
1934 cp3 = ifa->ifa_netmask->sa_data;
1935 cplim = ifa->ifa_netmask->sa_len + (char *)ifa->ifa_netmask;
1936 for (; cp3 < cplim; cp3++)
1937 if ((*cp++ ^ *cp2++) & *cp3)
1947 IF_ADDR_RUNLOCK(ifp);
1952 * See whether new ifa is better than current one:
1953 * 1) A non-virtual one is preferred over virtual.
1954 * 2) A virtual in master state preferred over any other state.
1956 * Used in several address selecting functions.
1959 ifa_preferred(struct ifaddr *cur, struct ifaddr *next)
1962 return (cur->ifa_carp && (!next->ifa_carp ||
1963 ((*carp_master_p)(next) && !(*carp_master_p)(cur))));
1966 #include <net/if_llatbl.h>
1969 * Default action when installing a route with a Link Level gateway.
1970 * Lookup an appropriate real ifa to point to.
1971 * This should be moved to /sys/net/link.c eventually.
1974 link_rtrequest(int cmd, struct rtentry *rt, struct rt_addrinfo *info)
1976 struct ifaddr *ifa, *oifa;
1977 struct sockaddr *dst;
1980 if (cmd != RTM_ADD || ((ifa = rt->rt_ifa) == 0) ||
1981 ((ifp = ifa->ifa_ifp) == 0) || ((dst = rt_key(rt)) == 0))
1983 ifa = ifaof_ifpforaddr(dst, ifp);
1988 if (ifa->ifa_rtrequest && ifa->ifa_rtrequest != link_rtrequest)
1989 ifa->ifa_rtrequest(cmd, rt, info);
1993 struct sockaddr_dl *
1994 link_alloc_sdl(size_t size, int flags)
1997 return (malloc(size, M_TEMP, flags));
2001 link_free_sdl(struct sockaddr *sa)
2007 * Fills in given sdl with interface basic info.
2008 * Returns pointer to filled sdl.
2010 struct sockaddr_dl *
2011 link_init_sdl(struct ifnet *ifp, struct sockaddr *paddr, u_char iftype)
2013 struct sockaddr_dl *sdl;
2015 sdl = (struct sockaddr_dl *)paddr;
2016 memset(sdl, 0, sizeof(struct sockaddr_dl));
2017 sdl->sdl_len = sizeof(struct sockaddr_dl);
2018 sdl->sdl_family = AF_LINK;
2019 sdl->sdl_index = ifp->if_index;
2020 sdl->sdl_type = iftype;
2026 * Mark an interface down and notify protocols of
2030 if_unroute(struct ifnet *ifp, int flag, int fam)
2034 KASSERT(flag == IFF_UP, ("if_unroute: flag != IFF_UP"));
2036 ifp->if_flags &= ~flag;
2037 getmicrotime(&ifp->if_lastchange);
2038 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link)
2039 if (fam == PF_UNSPEC || (fam == ifa->ifa_addr->sa_family))
2040 pfctlinput(PRC_IFDOWN, ifa->ifa_addr);
2041 ifp->if_qflush(ifp);
2044 (*carp_linkstate_p)(ifp);
2049 * Mark an interface up and notify protocols of
2053 if_route(struct ifnet *ifp, int flag, int fam)
2057 KASSERT(flag == IFF_UP, ("if_route: flag != IFF_UP"));
2059 ifp->if_flags |= flag;
2060 getmicrotime(&ifp->if_lastchange);
2061 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link)
2062 if (fam == PF_UNSPEC || (fam == ifa->ifa_addr->sa_family))
2063 pfctlinput(PRC_IFUP, ifa->ifa_addr);
2065 (*carp_linkstate_p)(ifp);
2072 void (*vlan_link_state_p)(struct ifnet *); /* XXX: private from if_vlan */
2073 void (*vlan_trunk_cap_p)(struct ifnet *); /* XXX: private from if_vlan */
2074 struct ifnet *(*vlan_trunkdev_p)(struct ifnet *);
2075 struct ifnet *(*vlan_devat_p)(struct ifnet *, uint16_t);
2076 int (*vlan_tag_p)(struct ifnet *, uint16_t *);
2077 int (*vlan_setcookie_p)(struct ifnet *, void *);
2078 void *(*vlan_cookie_p)(struct ifnet *);
2081 * Handle a change in the interface link state. To avoid LORs
2082 * between driver lock and upper layer locks, as well as possible
2083 * recursions, we post event to taskqueue, and all job
2084 * is done in static do_link_state_change().
2087 if_link_state_change(struct ifnet *ifp, int link_state)
2089 /* Return if state hasn't changed. */
2090 if (ifp->if_link_state == link_state)
2093 ifp->if_link_state = link_state;
2095 taskqueue_enqueue(taskqueue_swi, &ifp->if_linktask);
2099 do_link_state_change(void *arg, int pending)
2101 struct ifnet *ifp = (struct ifnet *)arg;
2102 int link_state = ifp->if_link_state;
2103 CURVNET_SET(ifp->if_vnet);
2105 /* Notify that the link state has changed. */
2107 if (ifp->if_vlantrunk != NULL)
2108 (*vlan_link_state_p)(ifp);
2110 if ((ifp->if_type == IFT_ETHER || ifp->if_type == IFT_L2VLAN) &&
2111 ifp->if_l2com != NULL)
2112 (*ng_ether_link_state_p)(ifp, link_state);
2114 (*carp_linkstate_p)(ifp);
2116 (*bridge_linkstate_p)(ifp);
2118 (*lagg_linkstate_p)(ifp, link_state);
2120 if (IS_DEFAULT_VNET(curvnet))
2121 devctl_notify("IFNET", ifp->if_xname,
2122 (link_state == LINK_STATE_UP) ? "LINK_UP" : "LINK_DOWN",
2125 if_printf(ifp, "%d link states coalesced\n", pending);
2126 if (log_link_state_change)
2127 log(LOG_NOTICE, "%s: link state changed to %s\n", ifp->if_xname,
2128 (link_state == LINK_STATE_UP) ? "UP" : "DOWN" );
2129 EVENTHANDLER_INVOKE(ifnet_link_event, ifp, ifp->if_link_state);
2134 * Mark an interface down and notify protocols of
2138 if_down(struct ifnet *ifp)
2141 if_unroute(ifp, IFF_UP, AF_UNSPEC);
2145 * Mark an interface up and notify protocols of
2149 if_up(struct ifnet *ifp)
2152 if_route(ifp, IFF_UP, AF_UNSPEC);
2156 * Flush an interface queue.
2159 if_qflush(struct ifnet *ifp)
2167 if (ALTQ_IS_ENABLED(ifq))
2171 while ((m = n) != 0) {
2182 * Map interface name to interface structure pointer, with or without
2183 * returning a reference.
2186 ifunit_ref(const char *name)
2190 IFNET_RLOCK_NOSLEEP();
2191 TAILQ_FOREACH(ifp, &V_ifnet, if_link) {
2192 if (strncmp(name, ifp->if_xname, IFNAMSIZ) == 0 &&
2193 !(ifp->if_flags & IFF_DYING))
2198 IFNET_RUNLOCK_NOSLEEP();
2203 ifunit(const char *name)
2207 IFNET_RLOCK_NOSLEEP();
2208 TAILQ_FOREACH(ifp, &V_ifnet, if_link) {
2209 if (strncmp(name, ifp->if_xname, IFNAMSIZ) == 0)
2212 IFNET_RUNLOCK_NOSLEEP();
2217 * Hardware specific interface ioctls.
2220 ifhwioctl(u_long cmd, struct ifnet *ifp, caddr_t data, struct thread *td)
2224 int new_flags, temp_flags;
2225 size_t namelen, onamelen;
2227 char *descrbuf, *odescrbuf;
2228 char new_name[IFNAMSIZ];
2230 struct sockaddr_dl *sdl;
2232 ifr = (struct ifreq *)data;
2235 ifr->ifr_index = ifp->if_index;
2239 temp_flags = ifp->if_flags | ifp->if_drv_flags;
2240 ifr->ifr_flags = temp_flags & 0xffff;
2241 ifr->ifr_flagshigh = temp_flags >> 16;
2245 ifr->ifr_reqcap = ifp->if_capabilities;
2246 ifr->ifr_curcap = ifp->if_capenable;
2251 error = mac_ifnet_ioctl_get(td->td_ucred, ifr, ifp);
2256 ifr->ifr_metric = ifp->if_metric;
2260 ifr->ifr_mtu = ifp->if_mtu;
2264 /* XXXGL: did this ever worked? */
2270 sx_slock(&ifdescr_sx);
2271 if (ifp->if_description == NULL)
2274 /* space for terminating nul */
2275 descrlen = strlen(ifp->if_description) + 1;
2276 if (ifr->ifr_buffer.length < descrlen)
2277 ifr->ifr_buffer.buffer = NULL;
2279 error = copyout(ifp->if_description,
2280 ifr->ifr_buffer.buffer, descrlen);
2281 ifr->ifr_buffer.length = descrlen;
2283 sx_sunlock(&ifdescr_sx);
2287 error = priv_check(td, PRIV_NET_SETIFDESCR);
2292 * Copy only (length-1) bytes to make sure that
2293 * if_description is always nul terminated. The
2294 * length parameter is supposed to count the
2295 * terminating nul in.
2297 if (ifr->ifr_buffer.length > ifdescr_maxlen)
2298 return (ENAMETOOLONG);
2299 else if (ifr->ifr_buffer.length == 0)
2302 descrbuf = malloc(ifr->ifr_buffer.length, M_IFDESCR,
2304 error = copyin(ifr->ifr_buffer.buffer, descrbuf,
2305 ifr->ifr_buffer.length - 1);
2307 free(descrbuf, M_IFDESCR);
2312 sx_xlock(&ifdescr_sx);
2313 odescrbuf = ifp->if_description;
2314 ifp->if_description = descrbuf;
2315 sx_xunlock(&ifdescr_sx);
2317 getmicrotime(&ifp->if_lastchange);
2318 free(odescrbuf, M_IFDESCR);
2322 ifr->ifr_fib = ifp->if_fib;
2326 error = priv_check(td, PRIV_NET_SETIFFIB);
2329 if (ifr->ifr_fib >= rt_numfibs)
2332 ifp->if_fib = ifr->ifr_fib;
2336 error = priv_check(td, PRIV_NET_SETIFFLAGS);
2340 * Currently, no driver owned flags pass the IFF_CANTCHANGE
2341 * check, so we don't need special handling here yet.
2343 new_flags = (ifr->ifr_flags & 0xffff) |
2344 (ifr->ifr_flagshigh << 16);
2345 if (ifp->if_flags & IFF_UP &&
2346 (new_flags & IFF_UP) == 0) {
2348 } else if (new_flags & IFF_UP &&
2349 (ifp->if_flags & IFF_UP) == 0) {
2352 /* See if permanently promiscuous mode bit is about to flip */
2353 if ((ifp->if_flags ^ new_flags) & IFF_PPROMISC) {
2354 if (new_flags & IFF_PPROMISC)
2355 ifp->if_flags |= IFF_PROMISC;
2356 else if (ifp->if_pcount == 0)
2357 ifp->if_flags &= ~IFF_PROMISC;
2358 log(LOG_INFO, "%s: permanently promiscuous mode %s\n",
2360 (new_flags & IFF_PPROMISC) ? "enabled" : "disabled");
2362 ifp->if_flags = (ifp->if_flags & IFF_CANTCHANGE) |
2363 (new_flags &~ IFF_CANTCHANGE);
2364 if (ifp->if_ioctl) {
2365 (void) (*ifp->if_ioctl)(ifp, cmd, data);
2367 getmicrotime(&ifp->if_lastchange);
2371 error = priv_check(td, PRIV_NET_SETIFCAP);
2374 if (ifp->if_ioctl == NULL)
2375 return (EOPNOTSUPP);
2376 if (ifr->ifr_reqcap & ~ifp->if_capabilities)
2378 error = (*ifp->if_ioctl)(ifp, cmd, data);
2380 getmicrotime(&ifp->if_lastchange);
2385 error = mac_ifnet_ioctl_set(td->td_ucred, ifr, ifp);
2390 error = priv_check(td, PRIV_NET_SETIFNAME);
2393 error = copyinstr(ifr->ifr_data, new_name, IFNAMSIZ, NULL);
2396 if (new_name[0] == '\0')
2398 if (ifunit(new_name) != NULL)
2402 * XXX: Locking. Nothing else seems to lock if_flags,
2403 * and there are numerous other races with the
2404 * ifunit() checks not being atomic with namespace
2405 * changes (renames, vmoves, if_attach, etc).
2407 ifp->if_flags |= IFF_RENAMING;
2409 /* Announce the departure of the interface. */
2410 rt_ifannouncemsg(ifp, IFAN_DEPARTURE);
2411 EVENTHANDLER_INVOKE(ifnet_departure_event, ifp);
2413 log(LOG_INFO, "%s: changing name to '%s'\n",
2414 ifp->if_xname, new_name);
2417 strlcpy(ifp->if_xname, new_name, sizeof(ifp->if_xname));
2419 sdl = (struct sockaddr_dl *)ifa->ifa_addr;
2420 namelen = strlen(new_name);
2421 onamelen = sdl->sdl_nlen;
2423 * Move the address if needed. This is safe because we
2424 * allocate space for a name of length IFNAMSIZ when we
2425 * create this in if_attach().
2427 if (namelen != onamelen) {
2428 bcopy(sdl->sdl_data + onamelen,
2429 sdl->sdl_data + namelen, sdl->sdl_alen);
2431 bcopy(new_name, sdl->sdl_data, namelen);
2432 sdl->sdl_nlen = namelen;
2433 sdl = (struct sockaddr_dl *)ifa->ifa_netmask;
2434 bzero(sdl->sdl_data, onamelen);
2435 while (namelen != 0)
2436 sdl->sdl_data[--namelen] = 0xff;
2437 IF_ADDR_WUNLOCK(ifp);
2439 EVENTHANDLER_INVOKE(ifnet_arrival_event, ifp);
2440 /* Announce the return of the interface. */
2441 rt_ifannouncemsg(ifp, IFAN_ARRIVAL);
2443 ifp->if_flags &= ~IFF_RENAMING;
2448 error = priv_check(td, PRIV_NET_SETIFVNET);
2451 error = if_vmove_loan(td, ifp, ifr->ifr_name, ifr->ifr_jid);
2456 error = priv_check(td, PRIV_NET_SETIFMETRIC);
2459 ifp->if_metric = ifr->ifr_metric;
2460 getmicrotime(&ifp->if_lastchange);
2464 error = priv_check(td, PRIV_NET_SETIFPHYS);
2467 if (ifp->if_ioctl == NULL)
2468 return (EOPNOTSUPP);
2469 error = (*ifp->if_ioctl)(ifp, cmd, data);
2471 getmicrotime(&ifp->if_lastchange);
2476 u_long oldmtu = ifp->if_mtu;
2478 error = priv_check(td, PRIV_NET_SETIFMTU);
2481 if (ifr->ifr_mtu < IF_MINMTU || ifr->ifr_mtu > IF_MAXMTU)
2483 if (ifp->if_ioctl == NULL)
2484 return (EOPNOTSUPP);
2485 error = (*ifp->if_ioctl)(ifp, cmd, data);
2487 getmicrotime(&ifp->if_lastchange);
2491 * If the link MTU changed, do network layer specific procedure.
2493 if (ifp->if_mtu != oldmtu) {
2504 if (cmd == SIOCADDMULTI)
2505 error = priv_check(td, PRIV_NET_ADDMULTI);
2507 error = priv_check(td, PRIV_NET_DELMULTI);
2511 /* Don't allow group membership on non-multicast interfaces. */
2512 if ((ifp->if_flags & IFF_MULTICAST) == 0)
2513 return (EOPNOTSUPP);
2515 /* Don't let users screw up protocols' entries. */
2516 if (ifr->ifr_addr.sa_family != AF_LINK)
2519 if (cmd == SIOCADDMULTI) {
2520 struct ifmultiaddr *ifma;
2523 * Userland is only permitted to join groups once
2524 * via the if_addmulti() KPI, because it cannot hold
2525 * struct ifmultiaddr * between calls. It may also
2526 * lose a race while we check if the membership
2530 ifma = if_findmulti(ifp, &ifr->ifr_addr);
2531 IF_ADDR_RUNLOCK(ifp);
2535 error = if_addmulti(ifp, &ifr->ifr_addr, &ifma);
2537 error = if_delmulti(ifp, &ifr->ifr_addr);
2540 getmicrotime(&ifp->if_lastchange);
2543 case SIOCSIFPHYADDR:
2544 case SIOCDIFPHYADDR:
2546 case SIOCSIFPHYADDR_IN6:
2549 case SIOCSIFGENERIC:
2550 error = priv_check(td, PRIV_NET_HWIOCTL);
2553 if (ifp->if_ioctl == NULL)
2554 return (EOPNOTSUPP);
2555 error = (*ifp->if_ioctl)(ifp, cmd, data);
2557 getmicrotime(&ifp->if_lastchange);
2561 case SIOCGIFPSRCADDR:
2562 case SIOCGIFPDSTADDR:
2564 case SIOCGIFGENERIC:
2565 if (ifp->if_ioctl == NULL)
2566 return (EOPNOTSUPP);
2567 error = (*ifp->if_ioctl)(ifp, cmd, data);
2571 error = priv_check(td, PRIV_NET_SETLLADDR);
2574 error = if_setlladdr(ifp,
2575 ifr->ifr_addr.sa_data, ifr->ifr_addr.sa_len);
2576 EVENTHANDLER_INVOKE(iflladdr_event, ifp);
2581 struct ifgroupreq *ifgr = (struct ifgroupreq *)ifr;
2583 error = priv_check(td, PRIV_NET_ADDIFGROUP);
2586 if ((error = if_addgroup(ifp, ifgr->ifgr_group)))
2592 if ((error = if_getgroup((struct ifgroupreq *)ifr, ifp)))
2598 struct ifgroupreq *ifgr = (struct ifgroupreq *)ifr;
2600 error = priv_check(td, PRIV_NET_DELIFGROUP);
2603 if ((error = if_delgroup(ifp, ifgr->ifgr_group)))
2615 #ifdef COMPAT_FREEBSD32
2623 #define SIOCGIFCONF32 _IOWR('i', 36, struct ifconf32)
2630 ifioctl(struct socket *so, u_long cmd, caddr_t data, struct thread *td)
2637 CURVNET_SET(so->so_vnet);
2640 error = ifconf(cmd, data);
2644 #ifdef COMPAT_FREEBSD32
2647 struct ifconf32 *ifc32;
2650 ifc32 = (struct ifconf32 *)data;
2651 ifc.ifc_len = ifc32->ifc_len;
2652 ifc.ifc_buf = PTRIN(ifc32->ifc_buf);
2654 error = ifconf(SIOCGIFCONF, (void *)&ifc);
2657 ifc32->ifc_len = ifc.ifc_len;
2662 ifr = (struct ifreq *)data;
2667 error = priv_check(td, PRIV_NET_SETIFVNET);
2669 error = if_vmove_reclaim(td, ifr->ifr_name,
2676 error = priv_check(td, PRIV_NET_IFCREATE);
2678 error = if_clone_create(ifr->ifr_name,
2679 sizeof(ifr->ifr_name),
2680 cmd == SIOCIFCREATE2 ? ifr->ifr_data : NULL);
2684 error = priv_check(td, PRIV_NET_IFDESTROY);
2686 error = if_clone_destroy(ifr->ifr_name);
2690 case SIOCIFGCLONERS:
2691 error = if_clone_list((struct if_clonereq *)data);
2695 error = if_getgroupmembers((struct ifgroupreq *)data);
2698 #if defined(INET) || defined(INET6)
2701 if (carp_ioctl_p == NULL)
2702 error = EPROTONOSUPPORT;
2704 error = (*carp_ioctl_p)(ifr, cmd, td);
2710 ifp = ifunit_ref(ifr->ifr_name);
2716 error = ifhwioctl(cmd, ifp, data, td);
2717 if (error != ENOIOCTL) {
2723 oif_flags = ifp->if_flags;
2724 if (so->so_proto == NULL) {
2727 return (EOPNOTSUPP);
2731 * Pass the request on to the socket control method, and if the
2732 * latter returns EOPNOTSUPP, directly to the interface.
2734 * Make an exception for the legacy SIOCSIF* requests. Drivers
2735 * trust SIOCSIFADDR et al to come from an already privileged
2736 * layer, and do not perform any credentials checks or input
2739 error = ((*so->so_proto->pr_usrreqs->pru_control)(so, cmd, data,
2741 if (error == EOPNOTSUPP && ifp != NULL && ifp->if_ioctl != NULL &&
2742 cmd != SIOCSIFADDR && cmd != SIOCSIFBRDADDR &&
2743 cmd != SIOCSIFDSTADDR && cmd != SIOCSIFNETMASK)
2744 error = (*ifp->if_ioctl)(ifp, cmd, data);
2746 if ((oif_flags ^ ifp->if_flags) & IFF_UP) {
2748 if (ifp->if_flags & IFF_UP)
2758 * The code common to handling reference counted flags,
2759 * e.g., in ifpromisc() and if_allmulti().
2760 * The "pflag" argument can specify a permanent mode flag to check,
2761 * such as IFF_PPROMISC for promiscuous mode; should be 0 if none.
2763 * Only to be used on stack-owned flags, not driver-owned flags.
2766 if_setflag(struct ifnet *ifp, int flag, int pflag, int *refcount, int onswitch)
2770 int oldflags, oldcount;
2772 /* Sanity checks to catch programming errors */
2773 KASSERT((flag & (IFF_DRV_OACTIVE|IFF_DRV_RUNNING)) == 0,
2774 ("%s: setting driver-owned flag %d", __func__, flag));
2777 KASSERT(*refcount >= 0,
2778 ("%s: increment negative refcount %d for flag %d",
2779 __func__, *refcount, flag));
2781 KASSERT(*refcount > 0,
2782 ("%s: decrement non-positive refcount %d for flag %d",
2783 __func__, *refcount, flag));
2785 /* In case this mode is permanent, just touch refcount */
2786 if (ifp->if_flags & pflag) {
2787 *refcount += onswitch ? 1 : -1;
2791 /* Save ifnet parameters for if_ioctl() may fail */
2792 oldcount = *refcount;
2793 oldflags = ifp->if_flags;
2796 * See if we aren't the only and touching refcount is enough.
2797 * Actually toggle interface flag if we are the first or last.
2802 ifp->if_flags |= flag;
2806 ifp->if_flags &= ~flag;
2809 /* Call down the driver since we've changed interface flags */
2810 if (ifp->if_ioctl == NULL) {
2814 ifr.ifr_flags = ifp->if_flags & 0xffff;
2815 ifr.ifr_flagshigh = ifp->if_flags >> 16;
2816 error = (*ifp->if_ioctl)(ifp, SIOCSIFFLAGS, (caddr_t)&ifr);
2819 /* Notify userland that interface flags have changed */
2824 /* Recover after driver error */
2825 *refcount = oldcount;
2826 ifp->if_flags = oldflags;
2831 * Set/clear promiscuous mode on interface ifp based on the truth value
2832 * of pswitch. The calls are reference counted so that only the first
2833 * "on" request actually has an effect, as does the final "off" request.
2834 * Results are undefined if the "off" and "on" requests are not matched.
2837 ifpromisc(struct ifnet *ifp, int pswitch)
2840 int oldflags = ifp->if_flags;
2842 error = if_setflag(ifp, IFF_PROMISC, IFF_PPROMISC,
2843 &ifp->if_pcount, pswitch);
2844 /* If promiscuous mode status has changed, log a message */
2845 if (error == 0 && ((ifp->if_flags ^ oldflags) & IFF_PROMISC))
2846 log(LOG_INFO, "%s: promiscuous mode %s\n",
2848 (ifp->if_flags & IFF_PROMISC) ? "enabled" : "disabled");
2853 * Return interface configuration
2854 * of system. List may be used
2855 * in later ioctl's (above) to get
2856 * other information.
2860 ifconf(u_long cmd, caddr_t data)
2862 struct ifconf *ifc = (struct ifconf *)data;
2867 int error, full = 0, valid_len, max_len;
2869 /* Limit initial buffer size to MAXPHYS to avoid DoS from userspace. */
2870 max_len = MAXPHYS - 1;
2872 /* Prevent hostile input from being able to crash the system */
2873 if (ifc->ifc_len <= 0)
2877 if (ifc->ifc_len <= max_len) {
2878 max_len = ifc->ifc_len;
2881 sb = sbuf_new(NULL, NULL, max_len + 1, SBUF_FIXEDLEN);
2886 TAILQ_FOREACH(ifp, &V_ifnet, if_link) {
2890 * Zero the ifr_name buffer to make sure we don't
2891 * disclose the contents of the stack.
2893 memset(ifr.ifr_name, 0, sizeof(ifr.ifr_name));
2895 if (strlcpy(ifr.ifr_name, ifp->if_xname, sizeof(ifr.ifr_name))
2896 >= sizeof(ifr.ifr_name)) {
2899 return (ENAMETOOLONG);
2904 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
2905 struct sockaddr *sa = ifa->ifa_addr;
2907 if (prison_if(curthread->td_ucred, sa) != 0)
2910 if (sa->sa_len <= sizeof(*sa)) {
2912 sbuf_bcat(sb, &ifr, sizeof(ifr));
2913 max_len += sizeof(ifr);
2916 offsetof(struct ifreq, ifr_addr));
2917 max_len += offsetof(struct ifreq, ifr_addr);
2918 sbuf_bcat(sb, sa, sa->sa_len);
2919 max_len += sa->sa_len;
2922 if (sbuf_error(sb) == 0)
2923 valid_len = sbuf_len(sb);
2925 IF_ADDR_RUNLOCK(ifp);
2927 bzero((caddr_t)&ifr.ifr_addr, sizeof(ifr.ifr_addr));
2928 sbuf_bcat(sb, &ifr, sizeof(ifr));
2929 max_len += sizeof(ifr);
2931 if (sbuf_error(sb) == 0)
2932 valid_len = sbuf_len(sb);
2938 * If we didn't allocate enough space (uncommon), try again. If
2939 * we have already allocated as much space as we are allowed,
2940 * return what we've got.
2942 if (valid_len != max_len && !full) {
2947 ifc->ifc_len = valid_len;
2949 error = copyout(sbuf_data(sb), ifc->ifc_req, ifc->ifc_len);
2955 * Just like ifpromisc(), but for all-multicast-reception mode.
2958 if_allmulti(struct ifnet *ifp, int onswitch)
2961 return (if_setflag(ifp, IFF_ALLMULTI, 0, &ifp->if_amcount, onswitch));
2964 struct ifmultiaddr *
2965 if_findmulti(struct ifnet *ifp, struct sockaddr *sa)
2967 struct ifmultiaddr *ifma;
2969 IF_ADDR_LOCK_ASSERT(ifp);
2971 TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
2972 if (sa->sa_family == AF_LINK) {
2973 if (sa_dl_equal(ifma->ifma_addr, sa))
2976 if (sa_equal(ifma->ifma_addr, sa))
2985 * Allocate a new ifmultiaddr and initialize based on passed arguments. We
2986 * make copies of passed sockaddrs. The ifmultiaddr will not be added to
2987 * the ifnet multicast address list here, so the caller must do that and
2988 * other setup work (such as notifying the device driver). The reference
2989 * count is initialized to 1.
2991 static struct ifmultiaddr *
2992 if_allocmulti(struct ifnet *ifp, struct sockaddr *sa, struct sockaddr *llsa,
2995 struct ifmultiaddr *ifma;
2996 struct sockaddr *dupsa;
2998 ifma = malloc(sizeof *ifma, M_IFMADDR, mflags |
3003 dupsa = malloc(sa->sa_len, M_IFMADDR, mflags);
3004 if (dupsa == NULL) {
3005 free(ifma, M_IFMADDR);
3008 bcopy(sa, dupsa, sa->sa_len);
3009 ifma->ifma_addr = dupsa;
3011 ifma->ifma_ifp = ifp;
3012 ifma->ifma_refcount = 1;
3013 ifma->ifma_protospec = NULL;
3016 ifma->ifma_lladdr = NULL;
3020 dupsa = malloc(llsa->sa_len, M_IFMADDR, mflags);
3021 if (dupsa == NULL) {
3022 free(ifma->ifma_addr, M_IFMADDR);
3023 free(ifma, M_IFMADDR);
3026 bcopy(llsa, dupsa, llsa->sa_len);
3027 ifma->ifma_lladdr = dupsa;
3033 * if_freemulti: free ifmultiaddr structure and possibly attached related
3034 * addresses. The caller is responsible for implementing reference
3035 * counting, notifying the driver, handling routing messages, and releasing
3036 * any dependent link layer state.
3039 if_freemulti(struct ifmultiaddr *ifma)
3042 KASSERT(ifma->ifma_refcount == 0, ("if_freemulti: refcount %d",
3043 ifma->ifma_refcount));
3045 if (ifma->ifma_lladdr != NULL)
3046 free(ifma->ifma_lladdr, M_IFMADDR);
3047 free(ifma->ifma_addr, M_IFMADDR);
3048 free(ifma, M_IFMADDR);
3052 * Register an additional multicast address with a network interface.
3054 * - If the address is already present, bump the reference count on the
3055 * address and return.
3056 * - If the address is not link-layer, look up a link layer address.
3057 * - Allocate address structures for one or both addresses, and attach to the
3058 * multicast address list on the interface. If automatically adding a link
3059 * layer address, the protocol address will own a reference to the link
3060 * layer address, to be freed when it is freed.
3061 * - Notify the network device driver of an addition to the multicast address
3064 * 'sa' points to caller-owned memory with the desired multicast address.
3066 * 'retifma' will be used to return a pointer to the resulting multicast
3067 * address reference, if desired.
3070 if_addmulti(struct ifnet *ifp, struct sockaddr *sa,
3071 struct ifmultiaddr **retifma)
3073 struct ifmultiaddr *ifma, *ll_ifma;
3074 struct sockaddr *llsa;
3075 struct sockaddr_dl sdl;
3079 * If the address is already present, return a new reference to it;
3080 * otherwise, allocate storage and set up a new address.
3083 ifma = if_findmulti(ifp, sa);
3085 ifma->ifma_refcount++;
3086 if (retifma != NULL)
3088 IF_ADDR_WUNLOCK(ifp);
3093 * The address isn't already present; resolve the protocol address
3094 * into a link layer address, and then look that up, bump its
3095 * refcount or allocate an ifma for that also.
3096 * Most link layer resolving functions returns address data which
3097 * fits inside default sockaddr_dl structure. However callback
3098 * can allocate another sockaddr structure, in that case we need to
3103 if (ifp->if_resolvemulti != NULL) {
3104 /* Provide called function with buffer size information */
3105 sdl.sdl_len = sizeof(sdl);
3106 llsa = (struct sockaddr *)&sdl;
3107 error = ifp->if_resolvemulti(ifp, &llsa, sa);
3113 * Allocate the new address. Don't hook it up yet, as we may also
3114 * need to allocate a link layer multicast address.
3116 ifma = if_allocmulti(ifp, sa, llsa, M_NOWAIT);
3123 * If a link layer address is found, we'll need to see if it's
3124 * already present in the address list, or allocate is as well.
3125 * When this block finishes, the link layer address will be on the
3129 ll_ifma = if_findmulti(ifp, llsa);
3130 if (ll_ifma == NULL) {
3131 ll_ifma = if_allocmulti(ifp, llsa, NULL, M_NOWAIT);
3132 if (ll_ifma == NULL) {
3133 --ifma->ifma_refcount;
3138 TAILQ_INSERT_HEAD(&ifp->if_multiaddrs, ll_ifma,
3141 ll_ifma->ifma_refcount++;
3142 ifma->ifma_llifma = ll_ifma;
3146 * We now have a new multicast address, ifma, and possibly a new or
3147 * referenced link layer address. Add the primary address to the
3148 * ifnet address list.
3150 TAILQ_INSERT_HEAD(&ifp->if_multiaddrs, ifma, ifma_link);
3152 if (retifma != NULL)
3156 * Must generate the message while holding the lock so that 'ifma'
3157 * pointer is still valid.
3159 rt_newmaddrmsg(RTM_NEWMADDR, ifma);
3160 IF_ADDR_WUNLOCK(ifp);
3163 * We are certain we have added something, so call down to the
3164 * interface to let them know about it.
3166 if (ifp->if_ioctl != NULL) {
3167 (void) (*ifp->if_ioctl)(ifp, SIOCADDMULTI, 0);
3170 if ((llsa != NULL) && (llsa != (struct sockaddr *)&sdl))
3171 link_free_sdl(llsa);
3176 if ((llsa != NULL) && (llsa != (struct sockaddr *)&sdl))
3177 link_free_sdl(llsa);
3180 IF_ADDR_WUNLOCK(ifp);
3185 * Delete a multicast group membership by network-layer group address.
3187 * Returns ENOENT if the entry could not be found. If ifp no longer
3188 * exists, results are undefined. This entry point should only be used
3189 * from subsystems which do appropriate locking to hold ifp for the
3190 * duration of the call.
3191 * Network-layer protocol domains must use if_delmulti_ifma().
3194 if_delmulti(struct ifnet *ifp, struct sockaddr *sa)
3196 struct ifmultiaddr *ifma;
3201 IFNET_RLOCK_NOSLEEP();
3202 TAILQ_FOREACH(oifp, &V_ifnet, if_link)
3207 IFNET_RUNLOCK_NOSLEEP();
3209 KASSERT(ifp != NULL, ("%s: ifnet went away", __func__));
3216 ifma = if_findmulti(ifp, sa);
3218 lastref = if_delmulti_locked(ifp, ifma, 0);
3219 IF_ADDR_WUNLOCK(ifp);
3224 if (lastref && ifp->if_ioctl != NULL) {
3225 (void)(*ifp->if_ioctl)(ifp, SIOCDELMULTI, 0);
3232 * Delete all multicast group membership for an interface.
3233 * Should be used to quickly flush all multicast filters.
3236 if_delallmulti(struct ifnet *ifp)
3238 struct ifmultiaddr *ifma;
3239 struct ifmultiaddr *next;
3242 TAILQ_FOREACH_SAFE(ifma, &ifp->if_multiaddrs, ifma_link, next)
3243 if_delmulti_locked(ifp, ifma, 0);
3244 IF_ADDR_WUNLOCK(ifp);
3248 * Delete a multicast group membership by group membership pointer.
3249 * Network-layer protocol domains must use this routine.
3251 * It is safe to call this routine if the ifp disappeared.
3254 if_delmulti_ifma(struct ifmultiaddr *ifma)
3259 ifp = ifma->ifma_ifp;
3262 printf("%s: ifma_ifp seems to be detached\n", __func__);
3266 IFNET_RLOCK_NOSLEEP();
3267 TAILQ_FOREACH(oifp, &V_ifnet, if_link)
3271 printf("%s: ifnet %p disappeared\n", __func__, ifp);
3274 IFNET_RUNLOCK_NOSLEEP();
3278 * If and only if the ifnet instance exists: Acquire the address lock.
3283 lastref = if_delmulti_locked(ifp, ifma, 0);
3287 * If and only if the ifnet instance exists:
3288 * Release the address lock.
3289 * If the group was left: update the hardware hash filter.
3291 IF_ADDR_WUNLOCK(ifp);
3292 if (lastref && ifp->if_ioctl != NULL) {
3293 (void)(*ifp->if_ioctl)(ifp, SIOCDELMULTI, 0);
3299 * Perform deletion of network-layer and/or link-layer multicast address.
3301 * Return 0 if the reference count was decremented.
3302 * Return 1 if the final reference was released, indicating that the
3303 * hardware hash filter should be reprogrammed.
3306 if_delmulti_locked(struct ifnet *ifp, struct ifmultiaddr *ifma, int detaching)
3308 struct ifmultiaddr *ll_ifma;
3310 if (ifp != NULL && ifma->ifma_ifp != NULL) {
3311 KASSERT(ifma->ifma_ifp == ifp,
3312 ("%s: inconsistent ifp %p", __func__, ifp));
3313 IF_ADDR_WLOCK_ASSERT(ifp);
3316 ifp = ifma->ifma_ifp;
3319 * If the ifnet is detaching, null out references to ifnet,
3320 * so that upper protocol layers will notice, and not attempt
3321 * to obtain locks for an ifnet which no longer exists. The
3322 * routing socket announcement must happen before the ifnet
3323 * instance is detached from the system.
3327 printf("%s: detaching ifnet instance %p\n", __func__, ifp);
3330 * ifp may already be nulled out if we are being reentered
3331 * to delete the ll_ifma.
3334 rt_newmaddrmsg(RTM_DELMADDR, ifma);
3335 ifma->ifma_ifp = NULL;
3339 if (--ifma->ifma_refcount > 0)
3343 * If this ifma is a network-layer ifma, a link-layer ifma may
3344 * have been associated with it. Release it first if so.
3346 ll_ifma = ifma->ifma_llifma;
3347 if (ll_ifma != NULL) {
3348 KASSERT(ifma->ifma_lladdr != NULL,
3349 ("%s: llifma w/o lladdr", __func__));
3351 ll_ifma->ifma_ifp = NULL; /* XXX */
3352 if (--ll_ifma->ifma_refcount == 0) {
3354 TAILQ_REMOVE(&ifp->if_multiaddrs, ll_ifma,
3357 if_freemulti(ll_ifma);
3362 TAILQ_REMOVE(&ifp->if_multiaddrs, ifma, ifma_link);
3367 * The last reference to this instance of struct ifmultiaddr
3368 * was released; the hardware should be notified of this change.
3374 * Set the link layer address on an interface.
3376 * At this time we only support certain types of interfaces,
3377 * and we don't allow the length of the address to change.
3380 if_setlladdr(struct ifnet *ifp, const u_char *lladdr, int len)
3382 struct sockaddr_dl *sdl;
3389 IF_ADDR_RUNLOCK(ifp);
3393 IF_ADDR_RUNLOCK(ifp);
3394 sdl = (struct sockaddr_dl *)ifa->ifa_addr;
3399 if (len != sdl->sdl_alen) { /* don't allow length to change */
3403 switch (ifp->if_type) {
3411 case IFT_IEEE8023ADLAG:
3413 bcopy(lladdr, LLADDR(sdl), len);
3422 * If the interface is already up, we need
3423 * to re-init it in order to reprogram its
3426 if ((ifp->if_flags & IFF_UP) != 0) {
3427 if (ifp->if_ioctl) {
3428 ifp->if_flags &= ~IFF_UP;
3429 ifr.ifr_flags = ifp->if_flags & 0xffff;
3430 ifr.ifr_flagshigh = ifp->if_flags >> 16;
3431 (*ifp->if_ioctl)(ifp, SIOCSIFFLAGS, (caddr_t)&ifr);
3432 ifp->if_flags |= IFF_UP;
3433 ifr.ifr_flags = ifp->if_flags & 0xffff;
3434 ifr.ifr_flagshigh = ifp->if_flags >> 16;
3435 (*ifp->if_ioctl)(ifp, SIOCSIFFLAGS, (caddr_t)&ifr);
3439 * Also send gratuitous ARPs to notify other nodes about
3440 * the address change.
3442 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
3443 if (ifa->ifa_addr->sa_family == AF_INET)
3444 arp_ifinit(ifp, ifa);
3452 * The name argument must be a pointer to storage which will last as
3453 * long as the interface does. For physical devices, the result of
3454 * device_get_name(dev) is a good choice and for pseudo-devices a
3455 * static string works well.
3458 if_initname(struct ifnet *ifp, const char *name, int unit)
3460 ifp->if_dname = name;
3461 ifp->if_dunit = unit;
3462 if (unit != IF_DUNIT_NONE)
3463 snprintf(ifp->if_xname, IFNAMSIZ, "%s%d", name, unit);
3465 strlcpy(ifp->if_xname, name, IFNAMSIZ);
3469 if_printf(struct ifnet *ifp, const char * fmt, ...)
3474 retval = printf("%s: ", ifp->if_xname);
3476 retval += vprintf(fmt, ap);
3482 if_start(struct ifnet *ifp)
3485 (*(ifp)->if_start)(ifp);
3489 * Backwards compatibility interface for drivers
3490 * that have not implemented it
3493 if_transmit(struct ifnet *ifp, struct mbuf *m)
3497 IFQ_HANDOFF(ifp, m, error);
3502 if_handoff(struct ifqueue *ifq, struct mbuf *m, struct ifnet *ifp, int adjust)
3507 if (_IF_QFULL(ifq)) {
3509 if_inc_counter(ifp, IFCOUNTER_OQDROPS, 1);
3514 if_inc_counter(ifp, IFCOUNTER_OBYTES, m->m_pkthdr.len + adjust);
3515 if (m->m_flags & (M_BCAST|M_MCAST))
3516 if_inc_counter(ifp, IFCOUNTER_OMCASTS, 1);
3517 active = ifp->if_drv_flags & IFF_DRV_OACTIVE;
3519 _IF_ENQUEUE(ifq, m);
3521 if (ifp != NULL && !active)
3522 (*(ifp)->if_start)(ifp);
3527 if_register_com_alloc(u_char type,
3528 if_com_alloc_t *a, if_com_free_t *f)
3531 KASSERT(if_com_alloc[type] == NULL,
3532 ("if_register_com_alloc: %d already registered", type));
3533 KASSERT(if_com_free[type] == NULL,
3534 ("if_register_com_alloc: %d free already registered", type));
3536 if_com_alloc[type] = a;
3537 if_com_free[type] = f;
3541 if_deregister_com_alloc(u_char type)
3544 KASSERT(if_com_alloc[type] != NULL,
3545 ("if_deregister_com_alloc: %d not registered", type));
3546 KASSERT(if_com_free[type] != NULL,
3547 ("if_deregister_com_alloc: %d free not registered", type));
3548 if_com_alloc[type] = NULL;
3549 if_com_free[type] = NULL;
3552 /* API for driver access to network stack owned ifnet.*/
3554 if_setbaudrate(struct ifnet *ifp, uint64_t baudrate)
3558 oldbrate = ifp->if_baudrate;
3559 ifp->if_baudrate = baudrate;
3564 if_getbaudrate(if_t ifp)
3567 return (((struct ifnet *)ifp)->if_baudrate);
3571 if_setcapabilities(if_t ifp, int capabilities)
3573 ((struct ifnet *)ifp)->if_capabilities = capabilities;
3578 if_setcapabilitiesbit(if_t ifp, int setbit, int clearbit)
3580 ((struct ifnet *)ifp)->if_capabilities |= setbit;
3581 ((struct ifnet *)ifp)->if_capabilities &= ~clearbit;
3587 if_getcapabilities(if_t ifp)
3589 return ((struct ifnet *)ifp)->if_capabilities;
3593 if_setcapenable(if_t ifp, int capabilities)
3595 ((struct ifnet *)ifp)->if_capenable = capabilities;
3600 if_setcapenablebit(if_t ifp, int setcap, int clearcap)
3603 ((struct ifnet *)ifp)->if_capenable |= setcap;
3605 ((struct ifnet *)ifp)->if_capenable &= ~clearcap;
3611 if_getdname(if_t ifp)
3613 return ((struct ifnet *)ifp)->if_dname;
3617 if_togglecapenable(if_t ifp, int togglecap)
3619 ((struct ifnet *)ifp)->if_capenable ^= togglecap;
3624 if_getcapenable(if_t ifp)
3626 return ((struct ifnet *)ifp)->if_capenable;
3630 * This is largely undesirable because it ties ifnet to a device, but does
3631 * provide flexiblity for an embedded product vendor. Should be used with
3632 * the understanding that it violates the interface boundaries, and should be
3633 * a last resort only.
3636 if_setdev(if_t ifp, void *dev)
3642 if_setdrvflagbits(if_t ifp, int set_flags, int clear_flags)
3644 ((struct ifnet *)ifp)->if_drv_flags |= set_flags;
3645 ((struct ifnet *)ifp)->if_drv_flags &= ~clear_flags;
3651 if_getdrvflags(if_t ifp)
3653 return ((struct ifnet *)ifp)->if_drv_flags;
3657 if_setdrvflags(if_t ifp, int flags)
3659 ((struct ifnet *)ifp)->if_drv_flags = flags;
3665 if_setflags(if_t ifp, int flags)
3667 ((struct ifnet *)ifp)->if_flags = flags;
3672 if_setflagbits(if_t ifp, int set, int clear)
3674 ((struct ifnet *)ifp)->if_flags |= set;
3675 ((struct ifnet *)ifp)->if_flags &= ~clear;
3681 if_getflags(if_t ifp)
3683 return ((struct ifnet *)ifp)->if_flags;
3687 if_clearhwassist(if_t ifp)
3689 ((struct ifnet *)ifp)->if_hwassist = 0;
3694 if_sethwassistbits(if_t ifp, int toset, int toclear)
3696 ((struct ifnet *)ifp)->if_hwassist |= toset;
3697 ((struct ifnet *)ifp)->if_hwassist &= ~toclear;
3703 if_sethwassist(if_t ifp, int hwassist_bit)
3705 ((struct ifnet *)ifp)->if_hwassist = hwassist_bit;
3710 if_gethwassist(if_t ifp)
3712 return ((struct ifnet *)ifp)->if_hwassist;
3716 if_setmtu(if_t ifp, int mtu)
3718 ((struct ifnet *)ifp)->if_mtu = mtu;
3725 return ((struct ifnet *)ifp)->if_mtu;
3729 if_getmtu_family(if_t ifp, int family)
3733 for (dp = domains; dp; dp = dp->dom_next) {
3734 if (dp->dom_family == family && dp->dom_ifmtu != NULL)
3735 return (dp->dom_ifmtu((struct ifnet *)ifp));
3738 return (((struct ifnet *)ifp)->if_mtu);
3742 if_setsoftc(if_t ifp, void *softc)
3744 ((struct ifnet *)ifp)->if_softc = softc;
3749 if_getsoftc(if_t ifp)
3751 return ((struct ifnet *)ifp)->if_softc;
3755 if_setrcvif(struct mbuf *m, if_t ifp)
3757 m->m_pkthdr.rcvif = (struct ifnet *)ifp;
3761 if_setvtag(struct mbuf *m, uint16_t tag)
3763 m->m_pkthdr.ether_vtag = tag;
3767 if_getvtag(struct mbuf *m)
3770 return (m->m_pkthdr.ether_vtag);
3774 if_sendq_empty(if_t ifp)
3776 return IFQ_DRV_IS_EMPTY(&((struct ifnet *)ifp)->if_snd);
3780 if_getifaddr(if_t ifp)
3782 return ((struct ifnet *)ifp)->if_addr;
3786 if_getamcount(if_t ifp)
3788 return ((struct ifnet *)ifp)->if_amcount;
3793 if_setsendqready(if_t ifp)
3795 IFQ_SET_READY(&((struct ifnet *)ifp)->if_snd);
3800 if_setsendqlen(if_t ifp, int tx_desc_count)
3802 IFQ_SET_MAXLEN(&((struct ifnet *)ifp)->if_snd, tx_desc_count);
3803 ((struct ifnet *)ifp)->if_snd.ifq_drv_maxlen = tx_desc_count;
3809 if_vlantrunkinuse(if_t ifp)
3811 return ((struct ifnet *)ifp)->if_vlantrunk != NULL?1:0;
3815 if_input(if_t ifp, struct mbuf* sendmp)
3817 (*((struct ifnet *)ifp)->if_input)((struct ifnet *)ifp, sendmp);
3823 #ifndef ETH_ADDR_LEN
3824 #define ETH_ADDR_LEN 6
3828 if_setupmultiaddr(if_t ifp, void *mta, int *cnt, int max)
3830 struct ifmultiaddr *ifma;
3831 uint8_t *lmta = (uint8_t *)mta;
3834 TAILQ_FOREACH(ifma, &((struct ifnet *)ifp)->if_multiaddrs, ifma_link) {
3835 if (ifma->ifma_addr->sa_family != AF_LINK)
3841 bcopy(LLADDR((struct sockaddr_dl *)ifma->ifma_addr),
3842 &lmta[mcnt * ETH_ADDR_LEN], ETH_ADDR_LEN);
3851 if_multiaddr_array(if_t ifp, void *mta, int *cnt, int max)
3855 if_maddr_rlock(ifp);
3856 error = if_setupmultiaddr(ifp, mta, cnt, max);
3857 if_maddr_runlock(ifp);
3862 if_multiaddr_count(if_t ifp, int max)
3864 struct ifmultiaddr *ifma;
3868 if_maddr_rlock(ifp);
3869 TAILQ_FOREACH(ifma, &((struct ifnet *)ifp)->if_multiaddrs, ifma_link) {
3870 if (ifma->ifma_addr->sa_family != AF_LINK)
3876 if_maddr_runlock(ifp);
3881 if_dequeue(if_t ifp)
3884 IFQ_DRV_DEQUEUE(&((struct ifnet *)ifp)->if_snd, m);
3890 if_sendq_prepend(if_t ifp, struct mbuf *m)
3892 IFQ_DRV_PREPEND(&((struct ifnet *)ifp)->if_snd, m);
3897 if_setifheaderlen(if_t ifp, int len)
3899 ((struct ifnet *)ifp)->if_hdrlen = len;
3904 if_getlladdr(if_t ifp)
3906 return (IF_LLADDR((struct ifnet *)ifp));
3910 if_gethandle(u_char type)
3912 return (if_alloc(type));
3916 if_bpfmtap(if_t ifh, struct mbuf *m)
3918 struct ifnet *ifp = (struct ifnet *)ifh;
3924 if_etherbpfmtap(if_t ifh, struct mbuf *m)
3926 struct ifnet *ifp = (struct ifnet *)ifh;
3928 ETHER_BPF_MTAP(ifp, m);
3932 if_vlancap(if_t ifh)
3934 struct ifnet *ifp = (struct ifnet *)ifh;
3935 VLAN_CAPABILITIES(ifp);
3939 if_setinitfn(if_t ifp, void (*init_fn)(void *))
3941 ((struct ifnet *)ifp)->if_init = init_fn;
3945 if_setioctlfn(if_t ifp, int (*ioctl_fn)(if_t, u_long, caddr_t))
3947 ((struct ifnet *)ifp)->if_ioctl = (void *)ioctl_fn;
3951 if_setstartfn(if_t ifp, void (*start_fn)(if_t))
3953 ((struct ifnet *)ifp)->if_start = (void *)start_fn;
3957 if_settransmitfn(if_t ifp, if_transmit_fn_t start_fn)
3959 ((struct ifnet *)ifp)->if_transmit = start_fn;
3962 void if_setqflushfn(if_t ifp, if_qflush_fn_t flush_fn)
3964 ((struct ifnet *)ifp)->if_qflush = flush_fn;
3969 if_setgetcounterfn(if_t ifp, if_get_counter_t fn)
3972 ifp->if_get_counter = fn;
3975 /* Revisit these - These are inline functions originally. */
3977 drbr_inuse_drv(if_t ifh, struct buf_ring *br)
3979 return drbr_inuse_drv(ifh, br);
3983 drbr_dequeue_drv(if_t ifh, struct buf_ring *br)
3985 return drbr_dequeue(ifh, br);
3989 drbr_needs_enqueue_drv(if_t ifh, struct buf_ring *br)
3991 return drbr_needs_enqueue(ifh, br);
3995 drbr_enqueue_drv(if_t ifh, struct buf_ring *br, struct mbuf *m)
3997 return drbr_enqueue(ifh, br, m);