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/if_arp.h>
68 #include <net/if_clone.h>
69 #include <net/if_dl.h>
70 #include <net/if_types.h>
71 #include <net/if_var.h>
72 #include <net/radix.h>
73 #include <net/route.h>
76 #if defined(INET) || defined(INET6)
77 #include <net/ethernet.h>
78 #include <netinet/in.h>
79 #include <netinet/in_var.h>
80 #include <netinet/ip.h>
81 #include <netinet/ip_carp.h>
83 #include <netinet/if_ether.h>
86 #include <netinet6/in6_var.h>
87 #include <netinet6/in6_ifattach.h>
89 #endif /* INET || INET6 */
91 #include <security/mac/mac_framework.h>
93 #ifdef COMPAT_FREEBSD32
94 #include <sys/mount.h>
95 #include <compat/freebsd32/freebsd32.h>
98 struct ifindex_entry {
99 struct ifnet *ife_ifnet;
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 TUNABLE_INT("net.link.ifqmaxlen", &ifqmaxlen);
106 SYSCTL_INT(_net_link, OID_AUTO, ifqmaxlen, CTLFLAG_RDTUN,
107 &ifqmaxlen, 0, "max send queue size");
109 /* Log link state change events */
110 static int log_link_state_change = 1;
112 SYSCTL_INT(_net_link, OID_AUTO, log_link_state_change, CTLFLAG_RW,
113 &log_link_state_change, 0,
114 "log interface link state change events");
116 /* Interface description */
117 static unsigned int ifdescr_maxlen = 1024;
118 SYSCTL_UINT(_net, OID_AUTO, ifdescr_maxlen, CTLFLAG_RW,
120 "administrative maximum length for interface description");
122 static MALLOC_DEFINE(M_IFDESCR, "ifdescr", "ifnet descriptions");
124 /* global sx for non-critical path ifdescr */
125 static struct sx ifdescr_sx;
126 SX_SYSINIT(ifdescr_sx, &ifdescr_sx, "ifnet descr");
128 void (*bridge_linkstate_p)(struct ifnet *ifp);
129 void (*ng_ether_link_state_p)(struct ifnet *ifp, int state);
130 void (*lagg_linkstate_p)(struct ifnet *ifp, int state);
131 /* These are external hooks for CARP. */
132 void (*carp_linkstate_p)(struct ifnet *ifp);
133 void (*carp_demote_adj_p)(int, char *);
134 int (*carp_master_p)(struct ifaddr *);
135 #if defined(INET) || defined(INET6)
136 int (*carp_forus_p)(struct ifnet *ifp, u_char *dhost);
137 int (*carp_output_p)(struct ifnet *ifp, struct mbuf *m,
138 const struct sockaddr *sa);
139 int (*carp_ioctl_p)(struct ifreq *, u_long, struct thread *);
140 int (*carp_attach_p)(struct ifaddr *, int);
141 void (*carp_detach_p)(struct ifaddr *);
144 int (*carp_iamatch_p)(struct ifaddr *, uint8_t **);
147 struct ifaddr *(*carp_iamatch6_p)(struct ifnet *ifp, struct in6_addr *taddr6);
148 caddr_t (*carp_macmatch6_p)(struct ifnet *ifp, struct mbuf *m,
149 const struct in6_addr *taddr);
152 struct mbuf *(*tbr_dequeue_ptr)(struct ifaltq *, int) = NULL;
155 * XXX: Style; these should be sorted alphabetically, and unprototyped
156 * static functions should be prototyped. Currently they are sorted by
159 static void if_attachdomain(void *);
160 static void if_attachdomain1(struct ifnet *);
161 static int ifconf(u_long, caddr_t);
162 static void if_freemulti(struct ifmultiaddr *);
163 static void if_init(void *);
164 static void if_grow(void);
165 static void if_input_default(struct ifnet *, struct mbuf *);
166 static void if_route(struct ifnet *, int flag, int fam);
167 static int if_setflag(struct ifnet *, int, int, int *, int);
168 static int if_transmit(struct ifnet *ifp, struct mbuf *m);
169 static void if_unroute(struct ifnet *, int flag, int fam);
170 static void link_rtrequest(int, struct rtentry *, struct rt_addrinfo *);
171 static int if_rtdel(struct radix_node *, void *);
172 static int ifhwioctl(u_long, struct ifnet *, caddr_t, struct thread *);
173 static int if_delmulti_locked(struct ifnet *, struct ifmultiaddr *, int);
174 static void do_link_state_change(void *, int);
175 static int if_getgroup(struct ifgroupreq *, struct ifnet *);
176 static int if_getgroupmembers(struct ifgroupreq *);
177 static void if_delgroups(struct ifnet *);
178 static void if_attach_internal(struct ifnet *, int);
179 static void if_detach_internal(struct ifnet *, int);
183 * XXX: declare here to avoid to include many inet6 related files..
184 * should be more generalized?
186 extern void nd6_setmtu(struct ifnet *);
189 VNET_DEFINE(int, if_index);
190 int ifqmaxlen = IFQ_MAXLEN;
191 VNET_DEFINE(struct ifnethead, ifnet); /* depend on static init XXX */
192 VNET_DEFINE(struct ifgrouphead, ifg_head);
194 static VNET_DEFINE(int, if_indexlim) = 8;
196 /* Table of ifnet by index. */
197 VNET_DEFINE(struct ifindex_entry *, ifindex_table);
199 #define V_if_indexlim VNET(if_indexlim)
200 #define V_ifindex_table VNET(ifindex_table)
203 * The global network interface list (V_ifnet) and related state (such as
204 * if_index, if_indexlim, and ifindex_table) are protected by an sxlock and
205 * an rwlock. Either may be acquired shared to stablize the list, but both
206 * must be acquired writable to modify the list. This model allows us to
207 * both stablize the interface list during interrupt thread processing, but
208 * also to stablize it over long-running ioctls, without introducing priority
209 * inversions and deadlocks.
211 struct rwlock ifnet_rwlock;
212 struct sx ifnet_sxlock;
215 * The allocation of network interfaces is a rather non-atomic affair; we
216 * need to select an index before we are ready to expose the interface for
217 * use, so will use this pointer value to indicate reservation.
219 #define IFNET_HOLD (void *)(uintptr_t)(-1)
221 static if_com_alloc_t *if_com_alloc[256];
222 static if_com_free_t *if_com_free[256];
224 static MALLOC_DEFINE(M_IFNET, "ifnet", "interface internals");
225 MALLOC_DEFINE(M_IFADDR, "ifaddr", "interface address");
226 MALLOC_DEFINE(M_IFMADDR, "ether_multi", "link-level multicast address");
229 ifnet_byindex_locked(u_short idx)
232 if (idx > V_if_index)
234 if (V_ifindex_table[idx].ife_ifnet == IFNET_HOLD)
236 return (V_ifindex_table[idx].ife_ifnet);
240 ifnet_byindex(u_short idx)
244 IFNET_RLOCK_NOSLEEP();
245 ifp = ifnet_byindex_locked(idx);
246 IFNET_RUNLOCK_NOSLEEP();
251 ifnet_byindex_ref(u_short idx)
255 IFNET_RLOCK_NOSLEEP();
256 ifp = ifnet_byindex_locked(idx);
257 if (ifp == NULL || (ifp->if_flags & IFF_DYING)) {
258 IFNET_RUNLOCK_NOSLEEP();
262 IFNET_RUNLOCK_NOSLEEP();
267 * Allocate an ifindex array entry; return 0 on success or an error on
271 ifindex_alloc_locked(u_short *idxp)
275 IFNET_WLOCK_ASSERT();
279 * Try to find an empty slot below V_if_index. If we fail, take the
282 for (idx = 1; idx <= V_if_index; idx++) {
283 if (V_ifindex_table[idx].ife_ifnet == NULL)
287 /* Catch if_index overflow. */
290 if (idx >= V_if_indexlim) {
294 if (idx > V_if_index)
301 ifindex_free_locked(u_short idx)
304 IFNET_WLOCK_ASSERT();
306 V_ifindex_table[idx].ife_ifnet = NULL;
307 while (V_if_index > 0 &&
308 V_ifindex_table[V_if_index].ife_ifnet == NULL)
313 ifindex_free(u_short idx)
317 ifindex_free_locked(idx);
322 ifnet_setbyindex_locked(u_short idx, struct ifnet *ifp)
325 IFNET_WLOCK_ASSERT();
327 V_ifindex_table[idx].ife_ifnet = ifp;
331 ifnet_setbyindex(u_short idx, struct ifnet *ifp)
335 ifnet_setbyindex_locked(idx, ifp);
340 ifaddr_byindex(u_short idx)
344 IFNET_RLOCK_NOSLEEP();
345 ifa = ifnet_byindex_locked(idx)->if_addr;
348 IFNET_RUNLOCK_NOSLEEP();
353 * Network interface utility routines.
355 * Routines with ifa_ifwith* names take sockaddr *'s as
360 vnet_if_init(const void *unused __unused)
363 TAILQ_INIT(&V_ifnet);
364 TAILQ_INIT(&V_ifg_head);
366 if_grow(); /* create initial table */
368 vnet_if_clone_init();
370 VNET_SYSINIT(vnet_if_init, SI_SUB_INIT_IF, SI_ORDER_SECOND, vnet_if_init,
375 if_init(void *dummy __unused)
381 SYSINIT(interfaces, SI_SUB_INIT_IF, SI_ORDER_FIRST, if_init, NULL);
386 vnet_if_uninit(const void *unused __unused)
389 VNET_ASSERT(TAILQ_EMPTY(&V_ifnet), ("%s:%d tailq &V_ifnet=%p "
390 "not empty", __func__, __LINE__, &V_ifnet));
391 VNET_ASSERT(TAILQ_EMPTY(&V_ifg_head), ("%s:%d tailq &V_ifg_head=%p "
392 "not empty", __func__, __LINE__, &V_ifg_head));
394 free((caddr_t)V_ifindex_table, M_IFNET);
396 VNET_SYSUNINIT(vnet_if_uninit, SI_SUB_INIT_IF, SI_ORDER_FIRST,
397 vnet_if_uninit, NULL);
405 struct ifindex_entry *e;
407 IFNET_WLOCK_ASSERT();
408 oldlim = V_if_indexlim;
410 n = (oldlim << 1) * sizeof(*e);
411 e = malloc(n, M_IFNET, M_WAITOK | M_ZERO);
413 if (V_if_indexlim != oldlim) {
417 if (V_ifindex_table != NULL) {
418 memcpy((caddr_t)e, (caddr_t)V_ifindex_table, n/2);
419 free((caddr_t)V_ifindex_table, M_IFNET);
426 * Allocate a struct ifnet and an index for an interface. A layer 2
427 * common structure will also be allocated if an allocation routine is
428 * registered for the passed type.
431 if_alloc(u_char type)
436 ifp = malloc(sizeof(struct ifnet), M_IFNET, M_WAITOK|M_ZERO);
438 if (ifindex_alloc_locked(&idx) != 0) {
443 ifnet_setbyindex_locked(idx, IFNET_HOLD);
447 ifp->if_alloctype = type;
448 if (if_com_alloc[type] != NULL) {
449 ifp->if_l2com = if_com_alloc[type](type, ifp);
450 if (ifp->if_l2com == NULL) {
457 IF_ADDR_LOCK_INIT(ifp);
458 TASK_INIT(&ifp->if_linktask, 0, do_link_state_change, ifp);
459 ifp->if_afdata_initialized = 0;
460 IF_AFDATA_LOCK_INIT(ifp);
461 TAILQ_INIT(&ifp->if_addrhead);
462 TAILQ_INIT(&ifp->if_multiaddrs);
463 TAILQ_INIT(&ifp->if_groups);
467 ifq_init(&ifp->if_snd, ifp);
469 refcount_init(&ifp->if_refcount, 1); /* Index reference. */
470 ifnet_setbyindex(ifp->if_index, ifp);
475 * Do the actual work of freeing a struct ifnet, and layer 2 common
476 * structure. This call is made when the last reference to an
477 * interface is released.
480 if_free_internal(struct ifnet *ifp)
483 KASSERT((ifp->if_flags & IFF_DYING),
484 ("if_free_internal: interface not dying"));
486 if (if_com_free[ifp->if_alloctype] != NULL)
487 if_com_free[ifp->if_alloctype](ifp->if_l2com,
491 mac_ifnet_destroy(ifp);
493 if (ifp->if_description != NULL)
494 free(ifp->if_description, M_IFDESCR);
495 IF_AFDATA_DESTROY(ifp);
496 IF_ADDR_LOCK_DESTROY(ifp);
497 ifq_delete(&ifp->if_snd);
502 * Deregister an interface and free the associated storage.
505 if_free(struct ifnet *ifp)
508 ifp->if_flags |= IFF_DYING; /* XXX: Locking */
510 CURVNET_SET_QUIET(ifp->if_vnet);
512 KASSERT(ifp == ifnet_byindex_locked(ifp->if_index),
513 ("%s: freeing unallocated ifnet", ifp->if_xname));
515 ifindex_free_locked(ifp->if_index);
518 if (refcount_release(&ifp->if_refcount))
519 if_free_internal(ifp);
524 * Interfaces to keep an ifnet type-stable despite the possibility of the
525 * driver calling if_free(). If there are additional references, we defer
526 * freeing the underlying data structure.
529 if_ref(struct ifnet *ifp)
532 /* We don't assert the ifnet list lock here, but arguably should. */
533 refcount_acquire(&ifp->if_refcount);
537 if_rele(struct ifnet *ifp)
540 if (!refcount_release(&ifp->if_refcount))
542 if_free_internal(ifp);
546 ifq_init(struct ifaltq *ifq, struct ifnet *ifp)
549 mtx_init(&ifq->ifq_mtx, ifp->if_xname, "if send queue", MTX_DEF);
551 if (ifq->ifq_maxlen == 0)
552 ifq->ifq_maxlen = ifqmaxlen;
555 ifq->altq_disc = NULL;
556 ifq->altq_flags &= ALTQF_CANTCHANGE;
557 ifq->altq_tbr = NULL;
562 ifq_delete(struct ifaltq *ifq)
564 mtx_destroy(&ifq->ifq_mtx);
568 * Perform generic interface initalization tasks and attach the interface
569 * to the list of "active" interfaces. If vmove flag is set on entry
570 * to if_attach_internal(), perform only a limited subset of initialization
571 * tasks, given that we are moving from one vnet to another an ifnet which
572 * has already been fully initialized.
575 * - The decision to return void and thus require this function to
576 * succeed is questionable.
577 * - We should probably do more sanity checking. For instance we don't
578 * do anything to insure if_xname is unique or non-empty.
581 if_attach(struct ifnet *ifp)
584 if_attach_internal(ifp, 0);
588 * Compute the least common TSO limit.
591 if_hw_tsomax_common(struct ifnet *ifp, struct ifnet_hw_tsomax *pmax)
594 * 1) If there is no limit currently, take the limit from
595 * the network adapter.
597 * 2) If the network adapter has a limit below the current
600 if (pmax->tsomaxbytes == 0 || (ifp->if_hw_tsomax != 0 &&
601 ifp->if_hw_tsomax < pmax->tsomaxbytes)) {
602 pmax->tsomaxbytes = ifp->if_hw_tsomax;
604 if (pmax->tsomaxsegcount == 0 || (ifp->if_hw_tsomaxsegcount != 0 &&
605 ifp->if_hw_tsomaxsegcount < pmax->tsomaxsegcount)) {
606 pmax->tsomaxsegcount = ifp->if_hw_tsomaxsegcount;
608 if (pmax->tsomaxsegsize == 0 || (ifp->if_hw_tsomaxsegsize != 0 &&
609 ifp->if_hw_tsomaxsegsize < pmax->tsomaxsegsize)) {
610 pmax->tsomaxsegsize = ifp->if_hw_tsomaxsegsize;
615 * Update TSO limit of a network adapter.
617 * Returns zero if no change. Else non-zero.
620 if_hw_tsomax_update(struct ifnet *ifp, struct ifnet_hw_tsomax *pmax)
623 if (ifp->if_hw_tsomax != pmax->tsomaxbytes) {
624 ifp->if_hw_tsomax = pmax->tsomaxbytes;
627 if (ifp->if_hw_tsomaxsegsize != pmax->tsomaxsegsize) {
628 ifp->if_hw_tsomaxsegsize = pmax->tsomaxsegsize;
631 if (ifp->if_hw_tsomaxsegcount != pmax->tsomaxsegcount) {
632 ifp->if_hw_tsomaxsegcount = pmax->tsomaxsegcount;
639 if_attach_internal(struct ifnet *ifp, int vmove)
641 unsigned socksize, ifasize;
642 int namelen, masklen;
643 struct sockaddr_dl *sdl;
646 if (ifp->if_index == 0 || ifp != ifnet_byindex(ifp->if_index))
647 panic ("%s: BUG: if_attach called without if_alloc'd input()\n",
651 ifp->if_vnet = curvnet;
652 if (ifp->if_home_vnet == NULL)
653 ifp->if_home_vnet = curvnet;
656 if_addgroup(ifp, IFG_ALL);
658 getmicrotime(&ifp->if_lastchange);
659 ifp->if_data.ifi_epoch = time_uptime;
660 ifp->if_data.ifi_datalen = sizeof(struct if_data);
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 = malloc(ifasize, M_IFADDR, M_WAITOK | M_ZERO);
693 sdl = (struct sockaddr_dl *)(ifa + 1);
694 sdl->sdl_len = socksize;
695 sdl->sdl_family = AF_LINK;
696 bcopy(ifp->if_xname, sdl->sdl_data, namelen);
697 sdl->sdl_nlen = namelen;
698 sdl->sdl_index = ifp->if_index;
699 sdl->sdl_type = ifp->if_type;
702 ifa->ifa_rtrequest = link_rtrequest;
703 ifa->ifa_addr = (struct sockaddr *)sdl;
704 sdl = (struct sockaddr_dl *)(socksize + (caddr_t)sdl);
705 ifa->ifa_netmask = (struct sockaddr *)sdl;
706 sdl->sdl_len = masklen;
708 sdl->sdl_data[--namelen] = 0xff;
709 TAILQ_INSERT_HEAD(&ifp->if_addrhead, ifa, ifa_link);
710 /* Reliably crash if used uninitialized. */
711 ifp->if_broadcastaddr = NULL;
713 #if defined(INET) || defined(INET6)
714 /* Use defaults for TSO, if nothing is set */
715 if (ifp->if_hw_tsomax == 0 &&
716 ifp->if_hw_tsomaxsegcount == 0 &&
717 ifp->if_hw_tsomaxsegsize == 0) {
719 * The TSO defaults needs to be such that an
720 * NFS mbuf list of 35 mbufs totalling just
721 * below 64K works and that a chain of mbufs
722 * can be defragged into at most 32 segments:
724 ifp->if_hw_tsomax = min(IP_MAXPACKET, (32 * MCLBYTES) -
725 (ETHER_HDR_LEN + ETHER_VLAN_ENCAP_LEN));
726 ifp->if_hw_tsomaxsegcount = 35;
727 ifp->if_hw_tsomaxsegsize = 2048; /* 2K */
729 /* XXX some drivers set IFCAP_TSO after ethernet attach */
730 if (ifp->if_capabilities & IFCAP_TSO) {
731 if_printf(ifp, "Using defaults for TSO: %u/%u/%u\n",
733 ifp->if_hw_tsomaxsegcount,
734 ifp->if_hw_tsomaxsegsize);
742 * Update the interface index in the link layer address
745 for (ifa = ifp->if_addr; ifa != NULL;
746 ifa = TAILQ_NEXT(ifa, ifa_link)) {
747 if (ifa->ifa_addr->sa_family == AF_LINK) {
748 sdl = (struct sockaddr_dl *)ifa->ifa_addr;
749 sdl->sdl_index = ifp->if_index;
756 TAILQ_INSERT_TAIL(&V_ifnet, ifp, if_link);
758 curvnet->vnet_ifcnt++;
762 if (domain_init_status >= 2)
763 if_attachdomain1(ifp);
765 EVENTHANDLER_INVOKE(ifnet_arrival_event, ifp);
766 if (IS_DEFAULT_VNET(curvnet))
767 devctl_notify("IFNET", ifp->if_xname, "ATTACH", NULL);
769 /* Announce the interface. */
770 rt_ifannouncemsg(ifp, IFAN_ARRIVAL);
774 if_attachdomain(void *dummy)
778 TAILQ_FOREACH(ifp, &V_ifnet, if_link)
779 if_attachdomain1(ifp);
781 SYSINIT(domainifattach, SI_SUB_PROTO_IFATTACHDOMAIN, SI_ORDER_SECOND,
782 if_attachdomain, NULL);
785 if_attachdomain1(struct ifnet *ifp)
790 * Since dp->dom_ifattach calls malloc() with M_WAITOK, we
791 * cannot lock ifp->if_afdata initialization, entirely.
793 if (IF_AFDATA_TRYLOCK(ifp) == 0)
795 if (ifp->if_afdata_initialized >= domain_init_status) {
796 IF_AFDATA_UNLOCK(ifp);
797 log(LOG_WARNING, "%s called more than once on %s\n",
798 __func__, ifp->if_xname);
801 ifp->if_afdata_initialized = domain_init_status;
802 IF_AFDATA_UNLOCK(ifp);
804 /* address family dependent data region */
805 bzero(ifp->if_afdata, sizeof(ifp->if_afdata));
806 for (dp = domains; dp; dp = dp->dom_next) {
807 if (dp->dom_ifattach)
808 ifp->if_afdata[dp->dom_family] =
809 (*dp->dom_ifattach)(ifp);
814 * Remove any unicast or broadcast network addresses from an interface.
817 if_purgeaddrs(struct ifnet *ifp)
819 struct ifaddr *ifa, *next;
821 TAILQ_FOREACH_SAFE(ifa, &ifp->if_addrhead, ifa_link, next) {
822 if (ifa->ifa_addr->sa_family == AF_LINK)
825 /* XXX: Ugly!! ad hoc just for INET */
826 if (ifa->ifa_addr->sa_family == AF_INET) {
827 struct ifaliasreq ifr;
829 bzero(&ifr, sizeof(ifr));
830 ifr.ifra_addr = *ifa->ifa_addr;
831 if (ifa->ifa_dstaddr)
832 ifr.ifra_broadaddr = *ifa->ifa_dstaddr;
833 if (in_control(NULL, SIOCDIFADDR, (caddr_t)&ifr, ifp,
839 if (ifa->ifa_addr->sa_family == AF_INET6) {
841 /* ifp_addrhead is already updated */
845 TAILQ_REMOVE(&ifp->if_addrhead, ifa, ifa_link);
851 * Remove any multicast network addresses from an interface when an ifnet
855 if_purgemaddrs(struct ifnet *ifp)
857 struct ifmultiaddr *ifma;
858 struct ifmultiaddr *next;
861 TAILQ_FOREACH_SAFE(ifma, &ifp->if_multiaddrs, ifma_link, next)
862 if_delmulti_locked(ifp, ifma, 1);
863 IF_ADDR_WUNLOCK(ifp);
867 * Detach an interface, removing it from the list of "active" interfaces.
868 * If vmove flag is set on entry to if_detach_internal(), perform only a
869 * limited subset of cleanup tasks, given that we are moving an ifnet from
870 * one vnet to another, where it must be fully operational.
872 * XXXRW: There are some significant questions about event ordering, and
873 * how to prevent things from starting to use the interface during detach.
876 if_detach(struct ifnet *ifp)
879 CURVNET_SET_QUIET(ifp->if_vnet);
880 if_detach_internal(ifp, 0);
885 if_detach_internal(struct ifnet *ifp, int vmove)
888 struct radix_node_head *rnh;
895 TAILQ_FOREACH(iter, &V_ifnet, if_link)
897 TAILQ_REMOVE(&V_ifnet, ifp, if_link);
903 curvnet->vnet_ifcnt--;
908 panic("%s: ifp=%p not on the ifnet tailq %p",
909 __func__, ifp, &V_ifnet);
911 return; /* XXX this should panic as well? */
915 * Remove/wait for pending events.
917 taskqueue_drain(taskqueue_swi, &ifp->if_linktask);
920 * Remove routes and flush queues.
924 if (ALTQ_IS_ENABLED(&ifp->if_snd))
925 altq_disable(&ifp->if_snd);
926 if (ALTQ_IS_ATTACHED(&ifp->if_snd))
927 altq_detach(&ifp->if_snd);
938 * Remove all IPv6 kernel structs related to ifp. This should be done
939 * before removing routing entries below, since IPv6 interface direct
940 * routes are expected to be removed by the IPv6-specific kernel API.
941 * Otherwise, the kernel will detect some inconsistency and bark it.
949 * Prevent further calls into the device driver via ifnet.
954 * Remove link ifaddr pointer and maybe decrement if_index.
955 * Clean up all addresses.
959 /* We can now free link ifaddr. */
960 if (!TAILQ_EMPTY(&ifp->if_addrhead)) {
961 ifa = TAILQ_FIRST(&ifp->if_addrhead);
962 TAILQ_REMOVE(&ifp->if_addrhead, ifa, ifa_link);
968 * Delete all remaining routes using this interface
969 * Unfortuneatly the only way to do this is to slog through
970 * the entire routing table looking for routes which point
971 * to this interface...oh well...
973 for (i = 1; i <= AF_MAX; i++) {
974 for (j = 0; j < rt_numfibs; j++) {
975 rnh = rt_tables_get_rnh(j, i);
978 RADIX_NODE_HEAD_LOCK(rnh);
979 (void) rnh->rnh_walktree(rnh, if_rtdel, ifp);
980 RADIX_NODE_HEAD_UNLOCK(rnh);
984 /* Announce that the interface is gone. */
985 rt_ifannouncemsg(ifp, IFAN_DEPARTURE);
986 EVENTHANDLER_INVOKE(ifnet_departure_event, ifp);
987 if (IS_DEFAULT_VNET(curvnet))
988 devctl_notify("IFNET", ifp->if_xname, "DETACH", NULL);
992 * We cannot hold the lock over dom_ifdetach calls as they might
993 * sleep, for example trying to drain a callout, thus open up the
994 * theoretical race with re-attaching.
997 i = ifp->if_afdata_initialized;
998 ifp->if_afdata_initialized = 0;
999 IF_AFDATA_UNLOCK(ifp);
1000 for (dp = domains; i > 0 && dp; dp = dp->dom_next) {
1001 if (dp->dom_ifdetach && ifp->if_afdata[dp->dom_family])
1002 (*dp->dom_ifdetach)(ifp,
1003 ifp->if_afdata[dp->dom_family]);
1009 * if_vmove() performs a limited version of if_detach() in current
1010 * vnet and if_attach()es the ifnet to the vnet specified as 2nd arg.
1011 * An attempt is made to shrink if_index in current vnet, find an
1012 * unused if_index in target vnet and calls if_grow() if necessary,
1013 * and finally find an unused if_xname for the target vnet.
1016 if_vmove(struct ifnet *ifp, struct vnet *new_vnet)
1021 * Detach from current vnet, but preserve LLADDR info, do not
1022 * mark as dead etc. so that the ifnet can be reattached later.
1024 if_detach_internal(ifp, 1);
1027 * Unlink the ifnet from ifindex_table[] in current vnet, and shrink
1028 * the if_index for that vnet if possible.
1030 * NOTE: IFNET_WLOCK/IFNET_WUNLOCK() are assumed to be unvirtualized,
1031 * or we'd lock on one vnet and unlock on another.
1034 ifindex_free_locked(ifp->if_index);
1038 * Perform interface-specific reassignment tasks, if provided by
1041 if (ifp->if_reassign != NULL)
1042 ifp->if_reassign(ifp, new_vnet, NULL);
1045 * Switch to the context of the target vnet.
1047 CURVNET_SET_QUIET(new_vnet);
1050 if (ifindex_alloc_locked(&idx) != 0) {
1052 panic("if_index overflow");
1054 ifp->if_index = idx;
1055 ifnet_setbyindex_locked(ifp->if_index, ifp);
1058 if_attach_internal(ifp, 1);
1064 * Move an ifnet to or from another child prison/vnet, specified by the jail id.
1067 if_vmove_loan(struct thread *td, struct ifnet *ifp, char *ifname, int jid)
1072 /* Try to find the prison within our visibility. */
1073 sx_slock(&allprison_lock);
1074 pr = prison_find_child(td->td_ucred->cr_prison, jid);
1075 sx_sunlock(&allprison_lock);
1078 prison_hold_locked(pr);
1079 mtx_unlock(&pr->pr_mtx);
1081 /* Do not try to move the iface from and to the same prison. */
1082 if (pr->pr_vnet == ifp->if_vnet) {
1087 /* Make sure the named iface does not exists in the dst. prison/vnet. */
1088 /* XXX Lock interfaces to avoid races. */
1089 CURVNET_SET_QUIET(pr->pr_vnet);
1090 difp = ifunit(ifname);
1097 /* Move the interface into the child jail/vnet. */
1098 if_vmove(ifp, pr->pr_vnet);
1100 /* Report the new if_xname back to the userland. */
1101 sprintf(ifname, "%s", ifp->if_xname);
1108 if_vmove_reclaim(struct thread *td, char *ifname, int jid)
1111 struct vnet *vnet_dst;
1114 /* Try to find the prison within our visibility. */
1115 sx_slock(&allprison_lock);
1116 pr = prison_find_child(td->td_ucred->cr_prison, jid);
1117 sx_sunlock(&allprison_lock);
1120 prison_hold_locked(pr);
1121 mtx_unlock(&pr->pr_mtx);
1123 /* Make sure the named iface exists in the source prison/vnet. */
1124 CURVNET_SET(pr->pr_vnet);
1125 ifp = ifunit(ifname); /* XXX Lock to avoid races. */
1132 /* Do not try to move the iface from and to the same prison. */
1133 vnet_dst = TD_TO_VNET(td);
1134 if (vnet_dst == ifp->if_vnet) {
1140 /* Get interface back from child jail/vnet. */
1141 if_vmove(ifp, vnet_dst);
1144 /* Report the new if_xname back to the userland. */
1145 sprintf(ifname, "%s", ifp->if_xname);
1153 * Add a group to an interface
1156 if_addgroup(struct ifnet *ifp, const char *groupname)
1158 struct ifg_list *ifgl;
1159 struct ifg_group *ifg = NULL;
1160 struct ifg_member *ifgm;
1163 if (groupname[0] && groupname[strlen(groupname) - 1] >= '0' &&
1164 groupname[strlen(groupname) - 1] <= '9')
1168 TAILQ_FOREACH(ifgl, &ifp->if_groups, ifgl_next)
1169 if (!strcmp(ifgl->ifgl_group->ifg_group, groupname)) {
1174 if ((ifgl = (struct ifg_list *)malloc(sizeof(struct ifg_list), M_TEMP,
1175 M_NOWAIT)) == NULL) {
1180 if ((ifgm = (struct ifg_member *)malloc(sizeof(struct ifg_member),
1181 M_TEMP, M_NOWAIT)) == NULL) {
1187 TAILQ_FOREACH(ifg, &V_ifg_head, ifg_next)
1188 if (!strcmp(ifg->ifg_group, groupname))
1192 if ((ifg = (struct ifg_group *)malloc(sizeof(struct ifg_group),
1193 M_TEMP, M_NOWAIT)) == NULL) {
1199 strlcpy(ifg->ifg_group, groupname, sizeof(ifg->ifg_group));
1200 ifg->ifg_refcnt = 0;
1201 TAILQ_INIT(&ifg->ifg_members);
1202 TAILQ_INSERT_TAIL(&V_ifg_head, ifg, ifg_next);
1207 ifgl->ifgl_group = ifg;
1208 ifgm->ifgm_ifp = ifp;
1211 TAILQ_INSERT_TAIL(&ifg->ifg_members, ifgm, ifgm_next);
1212 TAILQ_INSERT_TAIL(&ifp->if_groups, ifgl, ifgl_next);
1213 IF_ADDR_WUNLOCK(ifp);
1218 EVENTHANDLER_INVOKE(group_attach_event, ifg);
1219 EVENTHANDLER_INVOKE(group_change_event, groupname);
1225 * Remove a group from an interface
1228 if_delgroup(struct ifnet *ifp, const char *groupname)
1230 struct ifg_list *ifgl;
1231 struct ifg_member *ifgm;
1234 TAILQ_FOREACH(ifgl, &ifp->if_groups, ifgl_next)
1235 if (!strcmp(ifgl->ifgl_group->ifg_group, groupname))
1243 TAILQ_REMOVE(&ifp->if_groups, ifgl, ifgl_next);
1244 IF_ADDR_WUNLOCK(ifp);
1246 TAILQ_FOREACH(ifgm, &ifgl->ifgl_group->ifg_members, ifgm_next)
1247 if (ifgm->ifgm_ifp == ifp)
1251 TAILQ_REMOVE(&ifgl->ifgl_group->ifg_members, ifgm, ifgm_next);
1255 if (--ifgl->ifgl_group->ifg_refcnt == 0) {
1256 TAILQ_REMOVE(&V_ifg_head, ifgl->ifgl_group, ifg_next);
1258 EVENTHANDLER_INVOKE(group_detach_event, ifgl->ifgl_group);
1259 free(ifgl->ifgl_group, M_TEMP);
1265 EVENTHANDLER_INVOKE(group_change_event, groupname);
1271 * Remove an interface from all groups
1274 if_delgroups(struct ifnet *ifp)
1276 struct ifg_list *ifgl;
1277 struct ifg_member *ifgm;
1278 char groupname[IFNAMSIZ];
1281 while (!TAILQ_EMPTY(&ifp->if_groups)) {
1282 ifgl = TAILQ_FIRST(&ifp->if_groups);
1284 strlcpy(groupname, ifgl->ifgl_group->ifg_group, IFNAMSIZ);
1287 TAILQ_REMOVE(&ifp->if_groups, ifgl, ifgl_next);
1288 IF_ADDR_WUNLOCK(ifp);
1290 TAILQ_FOREACH(ifgm, &ifgl->ifgl_group->ifg_members, ifgm_next)
1291 if (ifgm->ifgm_ifp == ifp)
1295 TAILQ_REMOVE(&ifgl->ifgl_group->ifg_members, ifgm,
1300 if (--ifgl->ifgl_group->ifg_refcnt == 0) {
1301 TAILQ_REMOVE(&V_ifg_head, ifgl->ifgl_group, ifg_next);
1303 EVENTHANDLER_INVOKE(group_detach_event,
1305 free(ifgl->ifgl_group, M_TEMP);
1311 EVENTHANDLER_INVOKE(group_change_event, groupname);
1319 * Stores all groups from an interface in memory pointed
1323 if_getgroup(struct ifgroupreq *data, struct ifnet *ifp)
1326 struct ifg_list *ifgl;
1327 struct ifg_req ifgrq, *ifgp;
1328 struct ifgroupreq *ifgr = data;
1330 if (ifgr->ifgr_len == 0) {
1332 TAILQ_FOREACH(ifgl, &ifp->if_groups, ifgl_next)
1333 ifgr->ifgr_len += sizeof(struct ifg_req);
1334 IF_ADDR_RUNLOCK(ifp);
1338 len = ifgr->ifgr_len;
1339 ifgp = ifgr->ifgr_groups;
1342 TAILQ_FOREACH(ifgl, &ifp->if_groups, ifgl_next) {
1343 if (len < sizeof(ifgrq)) {
1344 IF_ADDR_RUNLOCK(ifp);
1347 bzero(&ifgrq, sizeof ifgrq);
1348 strlcpy(ifgrq.ifgrq_group, ifgl->ifgl_group->ifg_group,
1349 sizeof(ifgrq.ifgrq_group));
1350 if ((error = copyout(&ifgrq, ifgp, sizeof(struct ifg_req)))) {
1351 IF_ADDR_RUNLOCK(ifp);
1354 len -= sizeof(ifgrq);
1357 IF_ADDR_RUNLOCK(ifp);
1363 * Stores all members of a group in memory pointed to by data
1366 if_getgroupmembers(struct ifgroupreq *data)
1368 struct ifgroupreq *ifgr = data;
1369 struct ifg_group *ifg;
1370 struct ifg_member *ifgm;
1371 struct ifg_req ifgrq, *ifgp;
1375 TAILQ_FOREACH(ifg, &V_ifg_head, ifg_next)
1376 if (!strcmp(ifg->ifg_group, ifgr->ifgr_name))
1383 if (ifgr->ifgr_len == 0) {
1384 TAILQ_FOREACH(ifgm, &ifg->ifg_members, ifgm_next)
1385 ifgr->ifgr_len += sizeof(ifgrq);
1390 len = ifgr->ifgr_len;
1391 ifgp = ifgr->ifgr_groups;
1392 TAILQ_FOREACH(ifgm, &ifg->ifg_members, ifgm_next) {
1393 if (len < sizeof(ifgrq)) {
1397 bzero(&ifgrq, sizeof ifgrq);
1398 strlcpy(ifgrq.ifgrq_member, ifgm->ifgm_ifp->if_xname,
1399 sizeof(ifgrq.ifgrq_member));
1400 if ((error = copyout(&ifgrq, ifgp, sizeof(struct ifg_req)))) {
1404 len -= sizeof(ifgrq);
1413 * Delete Routes for a Network Interface
1415 * Called for each routing entry via the rnh->rnh_walktree() call above
1416 * to delete all route entries referencing a detaching network interface.
1419 * rn pointer to node in the routing table
1420 * arg argument passed to rnh->rnh_walktree() - detaching interface
1424 * errno failed - reason indicated
1428 if_rtdel(struct radix_node *rn, void *arg)
1430 struct rtentry *rt = (struct rtentry *)rn;
1431 struct ifnet *ifp = arg;
1434 if (rt->rt_ifp == ifp) {
1437 * Protect (sorta) against walktree recursion problems
1438 * with cloned routes
1440 if ((rt->rt_flags & RTF_UP) == 0)
1443 err = rtrequest_fib(RTM_DELETE, rt_key(rt), rt->rt_gateway,
1445 rt->rt_flags|RTF_RNH_LOCKED|RTF_PINNED,
1446 (struct rtentry **) NULL, rt->rt_fibnum);
1448 log(LOG_WARNING, "if_rtdel: error %d\n", err);
1456 * A compatibility function returns ifnet counter values.
1459 if_get_counter_default(struct ifnet *ifp, ift_counter cnt)
1462 KASSERT(cnt < IFCOUNTERS, ("%s: invalid cnt %d", __func__, cnt));
1464 case IFCOUNTER_IPACKETS:
1465 return (ifp->if_ipackets);
1466 case IFCOUNTER_IERRORS:
1467 return (ifp->if_ierrors);
1468 case IFCOUNTER_OPACKETS:
1469 return (ifp->if_opackets);
1470 case IFCOUNTER_OERRORS:
1471 return (ifp->if_oerrors);
1472 case IFCOUNTER_COLLISIONS:
1473 return (ifp->if_collisions);
1474 case IFCOUNTER_IBYTES:
1475 return (ifp->if_ibytes);
1476 case IFCOUNTER_OBYTES:
1477 return (ifp->if_obytes);
1478 case IFCOUNTER_IMCASTS:
1479 return (ifp->if_imcasts);
1480 case IFCOUNTER_OMCASTS:
1481 return (ifp->if_omcasts);
1482 case IFCOUNTER_IQDROPS:
1483 return (ifp->if_iqdrops);
1485 case IFCOUNTER_OQDROPS:
1486 return (ifp->if_oqdrops);
1488 case IFCOUNTER_NOPROTO:
1489 return (ifp->if_noproto);
1497 * Increase an ifnet counter. Usually used for counters shared
1498 * between the stack and a driver, but function supports them all.
1501 if_inc_counter(struct ifnet *ifp, ift_counter cnt, int64_t inc)
1504 KASSERT(cnt < IFCOUNTERS, ("%s: invalid cnt %d", __func__, cnt));
1506 case IFCOUNTER_IPACKETS:
1507 ifp->if_ipackets += inc;
1509 case IFCOUNTER_IERRORS:
1510 ifp->if_ierrors += inc;
1512 case IFCOUNTER_OPACKETS:
1513 ifp->if_opackets += inc;
1515 case IFCOUNTER_OERRORS:
1516 ifp->if_oerrors += inc;
1518 case IFCOUNTER_COLLISIONS:
1519 ifp->if_collisions += inc;
1521 case IFCOUNTER_IBYTES:
1522 ifp->if_ibytes += inc;
1524 case IFCOUNTER_OBYTES:
1525 ifp->if_obytes += inc;
1527 case IFCOUNTER_IMCASTS:
1528 ifp->if_imcasts += inc;
1530 case IFCOUNTER_OMCASTS:
1531 ifp->if_omcasts += inc;
1533 case IFCOUNTER_IQDROPS:
1534 ifp->if_iqdrops += inc;
1537 case IFCOUNTER_OQDROPS:
1538 ifp->if_oqdrops += inc;
1541 case IFCOUNTER_NOPROTO:
1542 ifp->if_noproto += inc;
1550 * Wrapper functions for struct ifnet address list locking macros. These are
1551 * used by kernel modules to avoid encoding programming interface or binary
1552 * interface assumptions that may be violated when kernel-internal locking
1553 * approaches change.
1556 if_addr_rlock(struct ifnet *ifp)
1563 if_addr_runlock(struct ifnet *ifp)
1566 IF_ADDR_RUNLOCK(ifp);
1570 if_maddr_rlock(struct ifnet *ifp)
1577 if_maddr_runlock(struct ifnet *ifp)
1580 IF_ADDR_RUNLOCK(ifp);
1584 * Initialization, destruction and refcounting functions for ifaddrs.
1587 ifa_init(struct ifaddr *ifa)
1590 mtx_init(&ifa->ifa_mtx, "ifaddr", NULL, MTX_DEF);
1591 refcount_init(&ifa->ifa_refcnt, 1);
1592 ifa->if_data.ifi_datalen = sizeof(ifa->if_data);
1596 ifa_ref(struct ifaddr *ifa)
1599 refcount_acquire(&ifa->ifa_refcnt);
1603 ifa_free(struct ifaddr *ifa)
1606 if (refcount_release(&ifa->ifa_refcnt)) {
1607 mtx_destroy(&ifa->ifa_mtx);
1608 free(ifa, M_IFADDR);
1613 ifa_add_loopback_route(struct ifaddr *ifa, struct sockaddr *ia)
1616 struct rtentry *rt = NULL;
1617 struct rt_addrinfo info;
1618 static struct sockaddr_dl null_sdl = {sizeof(null_sdl), AF_LINK};
1620 bzero(&info, sizeof(info));
1621 info.rti_ifp = V_loif;
1622 info.rti_flags = ifa->ifa_flags | RTF_HOST | RTF_STATIC;
1623 info.rti_info[RTAX_DST] = ia;
1624 info.rti_info[RTAX_GATEWAY] = (struct sockaddr *)&null_sdl;
1625 error = rtrequest1_fib(RTM_ADD, &info, &rt, ifa->ifa_ifp->if_fib);
1627 if (error == 0 && rt != NULL) {
1629 ((struct sockaddr_dl *)rt->rt_gateway)->sdl_type =
1630 ifa->ifa_ifp->if_type;
1631 ((struct sockaddr_dl *)rt->rt_gateway)->sdl_index =
1632 ifa->ifa_ifp->if_index;
1635 } else if (error != 0)
1636 log(LOG_DEBUG, "%s: insertion failed: %u\n", __func__, error);
1642 ifa_del_loopback_route(struct ifaddr *ifa, struct sockaddr *ia)
1645 struct rt_addrinfo info;
1646 struct sockaddr_dl null_sdl;
1648 bzero(&null_sdl, sizeof(null_sdl));
1649 null_sdl.sdl_len = sizeof(null_sdl);
1650 null_sdl.sdl_family = AF_LINK;
1651 null_sdl.sdl_type = ifa->ifa_ifp->if_type;
1652 null_sdl.sdl_index = ifa->ifa_ifp->if_index;
1653 bzero(&info, sizeof(info));
1654 info.rti_flags = ifa->ifa_flags | RTF_HOST | RTF_STATIC;
1655 info.rti_info[RTAX_DST] = ia;
1656 info.rti_info[RTAX_GATEWAY] = (struct sockaddr *)&null_sdl;
1657 error = rtrequest1_fib(RTM_DELETE, &info, NULL, ifa->ifa_ifp->if_fib);
1660 log(LOG_DEBUG, "%s: deletion failed: %u\n", __func__, error);
1666 * XXX: Because sockaddr_dl has deeper structure than the sockaddr
1667 * structs used to represent other address families, it is necessary
1668 * to perform a different comparison.
1671 #define sa_equal(a1, a2) \
1672 (bcmp((a1), (a2), ((a1))->sa_len) == 0)
1674 #define sa_dl_equal(a1, a2) \
1675 ((((struct sockaddr_dl *)(a1))->sdl_len == \
1676 ((struct sockaddr_dl *)(a2))->sdl_len) && \
1677 (bcmp(LLADDR((struct sockaddr_dl *)(a1)), \
1678 LLADDR((struct sockaddr_dl *)(a2)), \
1679 ((struct sockaddr_dl *)(a1))->sdl_alen) == 0))
1682 * Locate an interface based on a complete address.
1685 static struct ifaddr *
1686 ifa_ifwithaddr_internal(struct sockaddr *addr, int getref)
1691 IFNET_RLOCK_NOSLEEP();
1692 TAILQ_FOREACH(ifp, &V_ifnet, if_link) {
1694 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
1695 if (ifa->ifa_addr->sa_family != addr->sa_family)
1697 if (sa_equal(addr, ifa->ifa_addr)) {
1700 IF_ADDR_RUNLOCK(ifp);
1703 /* IP6 doesn't have broadcast */
1704 if ((ifp->if_flags & IFF_BROADCAST) &&
1705 ifa->ifa_broadaddr &&
1706 ifa->ifa_broadaddr->sa_len != 0 &&
1707 sa_equal(ifa->ifa_broadaddr, addr)) {
1710 IF_ADDR_RUNLOCK(ifp);
1714 IF_ADDR_RUNLOCK(ifp);
1718 IFNET_RUNLOCK_NOSLEEP();
1723 ifa_ifwithaddr(struct sockaddr *addr)
1726 return (ifa_ifwithaddr_internal(addr, 1));
1730 ifa_ifwithaddr_check(struct sockaddr *addr)
1733 return (ifa_ifwithaddr_internal(addr, 0) != NULL);
1737 * Locate an interface based on the broadcast address.
1741 ifa_ifwithbroadaddr(struct sockaddr *addr)
1746 IFNET_RLOCK_NOSLEEP();
1747 TAILQ_FOREACH(ifp, &V_ifnet, if_link) {
1749 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
1750 if (ifa->ifa_addr->sa_family != addr->sa_family)
1752 if ((ifp->if_flags & IFF_BROADCAST) &&
1753 ifa->ifa_broadaddr &&
1754 ifa->ifa_broadaddr->sa_len != 0 &&
1755 sa_equal(ifa->ifa_broadaddr, addr)) {
1757 IF_ADDR_RUNLOCK(ifp);
1761 IF_ADDR_RUNLOCK(ifp);
1765 IFNET_RUNLOCK_NOSLEEP();
1770 * Locate the point to point interface with a given destination address.
1774 ifa_ifwithdstaddr_fib(struct sockaddr *addr, int fibnum)
1779 IFNET_RLOCK_NOSLEEP();
1780 TAILQ_FOREACH(ifp, &V_ifnet, if_link) {
1781 if ((ifp->if_flags & IFF_POINTOPOINT) == 0)
1783 if ((fibnum != RT_ALL_FIBS) && (ifp->if_fib != fibnum))
1786 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
1787 if (ifa->ifa_addr->sa_family != addr->sa_family)
1789 if (ifa->ifa_dstaddr != NULL &&
1790 sa_equal(addr, ifa->ifa_dstaddr)) {
1792 IF_ADDR_RUNLOCK(ifp);
1796 IF_ADDR_RUNLOCK(ifp);
1800 IFNET_RUNLOCK_NOSLEEP();
1805 ifa_ifwithdstaddr(struct sockaddr *addr)
1808 return (ifa_ifwithdstaddr_fib(addr, RT_ALL_FIBS));
1812 * Find an interface on a specific network. If many, choice
1813 * is most specific found.
1816 ifa_ifwithnet_fib(struct sockaddr *addr, int ignore_ptp, int fibnum)
1820 struct ifaddr *ifa_maybe = NULL;
1821 u_int af = addr->sa_family;
1822 char *addr_data = addr->sa_data, *cplim;
1825 * AF_LINK addresses can be looked up directly by their index number,
1826 * so do that if we can.
1828 if (af == AF_LINK) {
1829 struct sockaddr_dl *sdl = (struct sockaddr_dl *)addr;
1830 if (sdl->sdl_index && sdl->sdl_index <= V_if_index)
1831 return (ifaddr_byindex(sdl->sdl_index));
1835 * Scan though each interface, looking for ones that have addresses
1836 * in this address family and the requested fib. Maintain a reference
1837 * on ifa_maybe once we find one, as we release the IF_ADDR_RLOCK() that
1838 * kept it stable when we move onto the next interface.
1840 IFNET_RLOCK_NOSLEEP();
1841 TAILQ_FOREACH(ifp, &V_ifnet, if_link) {
1842 if ((fibnum != RT_ALL_FIBS) && (ifp->if_fib != fibnum))
1845 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
1846 char *cp, *cp2, *cp3;
1848 if (ifa->ifa_addr->sa_family != af)
1850 if (af == AF_INET &&
1851 ifp->if_flags & IFF_POINTOPOINT && !ignore_ptp) {
1853 * This is a bit broken as it doesn't
1854 * take into account that the remote end may
1855 * be a single node in the network we are
1857 * The trouble is that we don't know the
1858 * netmask for the remote end.
1860 if (ifa->ifa_dstaddr != NULL &&
1861 sa_equal(addr, ifa->ifa_dstaddr)) {
1863 IF_ADDR_RUNLOCK(ifp);
1868 * if we have a special address handler,
1869 * then use it instead of the generic one.
1871 if (ifa->ifa_claim_addr) {
1872 if ((*ifa->ifa_claim_addr)(ifa, addr)) {
1874 IF_ADDR_RUNLOCK(ifp);
1881 * Scan all the bits in the ifa's address.
1882 * If a bit dissagrees with what we are
1883 * looking for, mask it with the netmask
1884 * to see if it really matters.
1885 * (A byte at a time)
1887 if (ifa->ifa_netmask == 0)
1890 cp2 = ifa->ifa_addr->sa_data;
1891 cp3 = ifa->ifa_netmask->sa_data;
1892 cplim = ifa->ifa_netmask->sa_len
1893 + (char *)ifa->ifa_netmask;
1895 if ((*cp++ ^ *cp2++) & *cp3++)
1896 goto next; /* next address! */
1898 * If the netmask of what we just found
1899 * is more specific than what we had before
1900 * (if we had one), or if the virtual status
1901 * of new prefix is better than of the old one,
1902 * then remember the new one before continuing
1903 * to search for an even better one.
1905 if (ifa_maybe == NULL ||
1906 ifa_preferred(ifa_maybe, ifa) ||
1907 rn_refines((caddr_t)ifa->ifa_netmask,
1908 (caddr_t)ifa_maybe->ifa_netmask)) {
1909 if (ifa_maybe != NULL)
1910 ifa_free(ifa_maybe);
1916 IF_ADDR_RUNLOCK(ifp);
1921 IFNET_RUNLOCK_NOSLEEP();
1922 if (ifa_maybe != NULL)
1923 ifa_free(ifa_maybe);
1928 ifa_ifwithnet(struct sockaddr *addr, int ignore_ptp)
1931 return (ifa_ifwithnet_fib(addr, ignore_ptp, RT_ALL_FIBS));
1935 * Find an interface address specific to an interface best matching
1939 ifaof_ifpforaddr(struct sockaddr *addr, struct ifnet *ifp)
1942 char *cp, *cp2, *cp3;
1944 struct ifaddr *ifa_maybe = NULL;
1945 u_int af = addr->sa_family;
1950 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
1951 if (ifa->ifa_addr->sa_family != af)
1953 if (ifa_maybe == NULL)
1955 if (ifa->ifa_netmask == 0) {
1956 if (sa_equal(addr, ifa->ifa_addr) ||
1957 (ifa->ifa_dstaddr &&
1958 sa_equal(addr, ifa->ifa_dstaddr)))
1962 if (ifp->if_flags & IFF_POINTOPOINT) {
1963 if (sa_equal(addr, ifa->ifa_dstaddr))
1967 cp2 = ifa->ifa_addr->sa_data;
1968 cp3 = ifa->ifa_netmask->sa_data;
1969 cplim = ifa->ifa_netmask->sa_len + (char *)ifa->ifa_netmask;
1970 for (; cp3 < cplim; cp3++)
1971 if ((*cp++ ^ *cp2++) & *cp3)
1981 IF_ADDR_RUNLOCK(ifp);
1986 * See whether new ifa is better than current one:
1987 * 1) A non-virtual one is preferred over virtual.
1988 * 2) A virtual in master state preferred over any other state.
1990 * Used in several address selecting functions.
1993 ifa_preferred(struct ifaddr *cur, struct ifaddr *next)
1996 return (cur->ifa_carp && (!next->ifa_carp ||
1997 ((*carp_master_p)(next) && !(*carp_master_p)(cur))));
2000 #include <net/if_llatbl.h>
2003 * Default action when installing a route with a Link Level gateway.
2004 * Lookup an appropriate real ifa to point to.
2005 * This should be moved to /sys/net/link.c eventually.
2008 link_rtrequest(int cmd, struct rtentry *rt, struct rt_addrinfo *info)
2010 struct ifaddr *ifa, *oifa;
2011 struct sockaddr *dst;
2016 if (cmd != RTM_ADD || ((ifa = rt->rt_ifa) == 0) ||
2017 ((ifp = ifa->ifa_ifp) == 0) || ((dst = rt_key(rt)) == 0))
2019 ifa = ifaof_ifpforaddr(dst, ifp);
2024 if (ifa->ifa_rtrequest && ifa->ifa_rtrequest != link_rtrequest)
2025 ifa->ifa_rtrequest(cmd, rt, info);
2030 * Mark an interface down and notify protocols of
2034 if_unroute(struct ifnet *ifp, int flag, int fam)
2038 KASSERT(flag == IFF_UP, ("if_unroute: flag != IFF_UP"));
2040 ifp->if_flags &= ~flag;
2041 getmicrotime(&ifp->if_lastchange);
2042 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link)
2043 if (fam == PF_UNSPEC || (fam == ifa->ifa_addr->sa_family))
2044 pfctlinput(PRC_IFDOWN, ifa->ifa_addr);
2045 ifp->if_qflush(ifp);
2048 (*carp_linkstate_p)(ifp);
2053 * Mark an interface up and notify protocols of
2057 if_route(struct ifnet *ifp, int flag, int fam)
2061 KASSERT(flag == IFF_UP, ("if_route: flag != IFF_UP"));
2063 ifp->if_flags |= flag;
2064 getmicrotime(&ifp->if_lastchange);
2065 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link)
2066 if (fam == PF_UNSPEC || (fam == ifa->ifa_addr->sa_family))
2067 pfctlinput(PRC_IFUP, ifa->ifa_addr);
2069 (*carp_linkstate_p)(ifp);
2076 void (*vlan_link_state_p)(struct ifnet *); /* XXX: private from if_vlan */
2077 void (*vlan_trunk_cap_p)(struct ifnet *); /* XXX: private from if_vlan */
2078 struct ifnet *(*vlan_trunkdev_p)(struct ifnet *);
2079 struct ifnet *(*vlan_devat_p)(struct ifnet *, uint16_t);
2080 int (*vlan_tag_p)(struct ifnet *, uint16_t *);
2081 int (*vlan_setcookie_p)(struct ifnet *, void *);
2082 void *(*vlan_cookie_p)(struct ifnet *);
2085 * Handle a change in the interface link state. To avoid LORs
2086 * between driver lock and upper layer locks, as well as possible
2087 * recursions, we post event to taskqueue, and all job
2088 * is done in static do_link_state_change().
2091 if_link_state_change(struct ifnet *ifp, int link_state)
2093 /* Return if state hasn't changed. */
2094 if (ifp->if_link_state == link_state)
2097 ifp->if_link_state = link_state;
2099 taskqueue_enqueue(taskqueue_swi, &ifp->if_linktask);
2103 do_link_state_change(void *arg, int pending)
2105 struct ifnet *ifp = (struct ifnet *)arg;
2106 int link_state = ifp->if_link_state;
2107 CURVNET_SET(ifp->if_vnet);
2109 /* Notify that the link state has changed. */
2111 if (ifp->if_vlantrunk != NULL)
2112 (*vlan_link_state_p)(ifp);
2114 if ((ifp->if_type == IFT_ETHER || ifp->if_type == IFT_L2VLAN) &&
2115 IFP2AC(ifp)->ac_netgraph != NULL)
2116 (*ng_ether_link_state_p)(ifp, link_state);
2118 (*carp_linkstate_p)(ifp);
2120 (*bridge_linkstate_p)(ifp);
2122 (*lagg_linkstate_p)(ifp, link_state);
2124 if (IS_DEFAULT_VNET(curvnet))
2125 devctl_notify("IFNET", ifp->if_xname,
2126 (link_state == LINK_STATE_UP) ? "LINK_UP" : "LINK_DOWN",
2129 if_printf(ifp, "%d link states coalesced\n", pending);
2130 if (log_link_state_change)
2131 log(LOG_NOTICE, "%s: link state changed to %s\n", ifp->if_xname,
2132 (link_state == LINK_STATE_UP) ? "UP" : "DOWN" );
2133 EVENTHANDLER_INVOKE(ifnet_link_event, ifp, ifp->if_link_state);
2138 * Mark an interface down and notify protocols of
2142 if_down(struct ifnet *ifp)
2145 if_unroute(ifp, IFF_UP, AF_UNSPEC);
2149 * Mark an interface up and notify protocols of
2153 if_up(struct ifnet *ifp)
2156 if_route(ifp, IFF_UP, AF_UNSPEC);
2160 * Flush an interface queue.
2163 if_qflush(struct ifnet *ifp)
2171 if (ALTQ_IS_ENABLED(ifq))
2175 while ((m = n) != 0) {
2186 * Map interface name to interface structure pointer, with or without
2187 * returning a reference.
2190 ifunit_ref(const char *name)
2194 IFNET_RLOCK_NOSLEEP();
2195 TAILQ_FOREACH(ifp, &V_ifnet, if_link) {
2196 if (strncmp(name, ifp->if_xname, IFNAMSIZ) == 0 &&
2197 !(ifp->if_flags & IFF_DYING))
2202 IFNET_RUNLOCK_NOSLEEP();
2207 ifunit(const char *name)
2211 IFNET_RLOCK_NOSLEEP();
2212 TAILQ_FOREACH(ifp, &V_ifnet, if_link) {
2213 if (strncmp(name, ifp->if_xname, IFNAMSIZ) == 0)
2216 IFNET_RUNLOCK_NOSLEEP();
2221 * Hardware specific interface ioctls.
2224 ifhwioctl(u_long cmd, struct ifnet *ifp, caddr_t data, struct thread *td)
2229 int new_flags, temp_flags;
2230 size_t namelen, onamelen;
2232 char *descrbuf, *odescrbuf;
2233 char new_name[IFNAMSIZ];
2235 struct sockaddr_dl *sdl;
2237 ifr = (struct ifreq *)data;
2240 ifr->ifr_index = ifp->if_index;
2244 temp_flags = ifp->if_flags | ifp->if_drv_flags;
2245 ifr->ifr_flags = temp_flags & 0xffff;
2246 ifr->ifr_flagshigh = temp_flags >> 16;
2250 ifr->ifr_reqcap = ifp->if_capabilities;
2251 ifr->ifr_curcap = ifp->if_capenable;
2256 error = mac_ifnet_ioctl_get(td->td_ucred, ifr, ifp);
2261 ifr->ifr_metric = ifp->if_metric;
2265 ifr->ifr_mtu = ifp->if_mtu;
2269 ifr->ifr_phys = ifp->if_physical;
2274 sx_slock(&ifdescr_sx);
2275 if (ifp->if_description == NULL)
2278 /* space for terminating nul */
2279 descrlen = strlen(ifp->if_description) + 1;
2280 if (ifr->ifr_buffer.length < descrlen)
2281 ifr->ifr_buffer.buffer = NULL;
2283 error = copyout(ifp->if_description,
2284 ifr->ifr_buffer.buffer, descrlen);
2285 ifr->ifr_buffer.length = descrlen;
2287 sx_sunlock(&ifdescr_sx);
2291 error = priv_check(td, PRIV_NET_SETIFDESCR);
2296 * Copy only (length-1) bytes to make sure that
2297 * if_description is always nul terminated. The
2298 * length parameter is supposed to count the
2299 * terminating nul in.
2301 if (ifr->ifr_buffer.length > ifdescr_maxlen)
2302 return (ENAMETOOLONG);
2303 else if (ifr->ifr_buffer.length == 0)
2306 descrbuf = malloc(ifr->ifr_buffer.length, M_IFDESCR,
2308 error = copyin(ifr->ifr_buffer.buffer, descrbuf,
2309 ifr->ifr_buffer.length - 1);
2311 free(descrbuf, M_IFDESCR);
2316 sx_xlock(&ifdescr_sx);
2317 odescrbuf = ifp->if_description;
2318 ifp->if_description = descrbuf;
2319 sx_xunlock(&ifdescr_sx);
2321 getmicrotime(&ifp->if_lastchange);
2322 free(odescrbuf, M_IFDESCR);
2326 ifr->ifr_fib = ifp->if_fib;
2330 error = priv_check(td, PRIV_NET_SETIFFIB);
2333 if (ifr->ifr_fib >= rt_numfibs)
2336 ifp->if_fib = ifr->ifr_fib;
2340 error = priv_check(td, PRIV_NET_SETIFFLAGS);
2344 * Currently, no driver owned flags pass the IFF_CANTCHANGE
2345 * check, so we don't need special handling here yet.
2347 new_flags = (ifr->ifr_flags & 0xffff) |
2348 (ifr->ifr_flagshigh << 16);
2349 if (ifp->if_flags & IFF_SMART) {
2350 /* Smart drivers twiddle their own routes */
2351 } else if (ifp->if_flags & IFF_UP &&
2352 (new_flags & IFF_UP) == 0) {
2354 } else if (new_flags & IFF_UP &&
2355 (ifp->if_flags & IFF_UP) == 0) {
2358 /* See if permanently promiscuous mode bit is about to flip */
2359 if ((ifp->if_flags ^ new_flags) & IFF_PPROMISC) {
2360 if (new_flags & IFF_PPROMISC)
2361 ifp->if_flags |= IFF_PROMISC;
2362 else if (ifp->if_pcount == 0)
2363 ifp->if_flags &= ~IFF_PROMISC;
2364 log(LOG_INFO, "%s: permanently promiscuous mode %s\n",
2366 (new_flags & IFF_PPROMISC) ? "enabled" : "disabled");
2368 ifp->if_flags = (ifp->if_flags & IFF_CANTCHANGE) |
2369 (new_flags &~ IFF_CANTCHANGE);
2370 if (ifp->if_ioctl) {
2371 (void) (*ifp->if_ioctl)(ifp, cmd, data);
2373 getmicrotime(&ifp->if_lastchange);
2377 error = priv_check(td, PRIV_NET_SETIFCAP);
2380 if (ifp->if_ioctl == NULL)
2381 return (EOPNOTSUPP);
2382 if (ifr->ifr_reqcap & ~ifp->if_capabilities)
2384 error = (*ifp->if_ioctl)(ifp, cmd, data);
2386 getmicrotime(&ifp->if_lastchange);
2391 error = mac_ifnet_ioctl_set(td->td_ucred, ifr, ifp);
2396 error = priv_check(td, PRIV_NET_SETIFNAME);
2399 error = copyinstr(ifr->ifr_data, new_name, IFNAMSIZ, NULL);
2402 if (new_name[0] == '\0')
2404 if (ifunit(new_name) != NULL)
2408 * XXX: Locking. Nothing else seems to lock if_flags,
2409 * and there are numerous other races with the
2410 * ifunit() checks not being atomic with namespace
2411 * changes (renames, vmoves, if_attach, etc).
2413 ifp->if_flags |= IFF_RENAMING;
2415 /* Announce the departure of the interface. */
2416 rt_ifannouncemsg(ifp, IFAN_DEPARTURE);
2417 EVENTHANDLER_INVOKE(ifnet_departure_event, ifp);
2419 log(LOG_INFO, "%s: changing name to '%s'\n",
2420 ifp->if_xname, new_name);
2422 strlcpy(ifp->if_xname, new_name, sizeof(ifp->if_xname));
2425 sdl = (struct sockaddr_dl *)ifa->ifa_addr;
2426 namelen = strlen(new_name);
2427 onamelen = sdl->sdl_nlen;
2429 * Move the address if needed. This is safe because we
2430 * allocate space for a name of length IFNAMSIZ when we
2431 * create this in if_attach().
2433 if (namelen != onamelen) {
2434 bcopy(sdl->sdl_data + onamelen,
2435 sdl->sdl_data + namelen, sdl->sdl_alen);
2437 bcopy(new_name, sdl->sdl_data, namelen);
2438 sdl->sdl_nlen = namelen;
2439 sdl = (struct sockaddr_dl *)ifa->ifa_netmask;
2440 bzero(sdl->sdl_data, onamelen);
2441 while (namelen != 0)
2442 sdl->sdl_data[--namelen] = 0xff;
2445 EVENTHANDLER_INVOKE(ifnet_arrival_event, ifp);
2446 /* Announce the return of the interface. */
2447 rt_ifannouncemsg(ifp, IFAN_ARRIVAL);
2449 ifp->if_flags &= ~IFF_RENAMING;
2454 error = priv_check(td, PRIV_NET_SETIFVNET);
2457 error = if_vmove_loan(td, ifp, ifr->ifr_name, ifr->ifr_jid);
2462 error = priv_check(td, PRIV_NET_SETIFMETRIC);
2465 ifp->if_metric = ifr->ifr_metric;
2466 getmicrotime(&ifp->if_lastchange);
2470 error = priv_check(td, PRIV_NET_SETIFPHYS);
2473 if (ifp->if_ioctl == NULL)
2474 return (EOPNOTSUPP);
2475 error = (*ifp->if_ioctl)(ifp, cmd, data);
2477 getmicrotime(&ifp->if_lastchange);
2482 u_long oldmtu = ifp->if_mtu;
2484 error = priv_check(td, PRIV_NET_SETIFMTU);
2487 if (ifr->ifr_mtu < IF_MINMTU || ifr->ifr_mtu > IF_MAXMTU)
2489 if (ifp->if_ioctl == NULL)
2490 return (EOPNOTSUPP);
2491 error = (*ifp->if_ioctl)(ifp, cmd, data);
2493 getmicrotime(&ifp->if_lastchange);
2497 * If the link MTU changed, do network layer specific procedure.
2499 if (ifp->if_mtu != oldmtu) {
2509 if (cmd == SIOCADDMULTI)
2510 error = priv_check(td, PRIV_NET_ADDMULTI);
2512 error = priv_check(td, PRIV_NET_DELMULTI);
2516 /* Don't allow group membership on non-multicast interfaces. */
2517 if ((ifp->if_flags & IFF_MULTICAST) == 0)
2518 return (EOPNOTSUPP);
2520 /* Don't let users screw up protocols' entries. */
2521 if (ifr->ifr_addr.sa_family != AF_LINK)
2524 if (cmd == SIOCADDMULTI) {
2525 struct ifmultiaddr *ifma;
2528 * Userland is only permitted to join groups once
2529 * via the if_addmulti() KPI, because it cannot hold
2530 * struct ifmultiaddr * between calls. It may also
2531 * lose a race while we check if the membership
2535 ifma = if_findmulti(ifp, &ifr->ifr_addr);
2536 IF_ADDR_RUNLOCK(ifp);
2540 error = if_addmulti(ifp, &ifr->ifr_addr, &ifma);
2542 error = if_delmulti(ifp, &ifr->ifr_addr);
2545 getmicrotime(&ifp->if_lastchange);
2548 case SIOCSIFPHYADDR:
2549 case SIOCDIFPHYADDR:
2551 case SIOCSIFPHYADDR_IN6:
2553 case SIOCSLIFPHYADDR:
2555 case SIOCSIFGENERIC:
2556 error = priv_check(td, PRIV_NET_HWIOCTL);
2559 if (ifp->if_ioctl == NULL)
2560 return (EOPNOTSUPP);
2561 error = (*ifp->if_ioctl)(ifp, cmd, data);
2563 getmicrotime(&ifp->if_lastchange);
2567 ifs = (struct ifstat *)data;
2568 ifs->ascii[0] = '\0';
2570 case SIOCGIFPSRCADDR:
2571 case SIOCGIFPDSTADDR:
2572 case SIOCGLIFPHYADDR:
2575 case SIOCGIFGENERIC:
2576 if (ifp->if_ioctl == NULL)
2577 return (EOPNOTSUPP);
2578 error = (*ifp->if_ioctl)(ifp, cmd, data);
2582 error = priv_check(td, PRIV_NET_SETLLADDR);
2585 error = if_setlladdr(ifp,
2586 ifr->ifr_addr.sa_data, ifr->ifr_addr.sa_len);
2587 EVENTHANDLER_INVOKE(iflladdr_event, ifp);
2592 struct ifgroupreq *ifgr = (struct ifgroupreq *)ifr;
2594 error = priv_check(td, PRIV_NET_ADDIFGROUP);
2597 if ((error = if_addgroup(ifp, ifgr->ifgr_group)))
2603 if ((error = if_getgroup((struct ifgroupreq *)ifr, ifp)))
2609 struct ifgroupreq *ifgr = (struct ifgroupreq *)ifr;
2611 error = priv_check(td, PRIV_NET_DELIFGROUP);
2614 if ((error = if_delgroup(ifp, ifgr->ifgr_group)))
2626 #ifdef COMPAT_FREEBSD32
2634 #define SIOCGIFCONF32 _IOWR('i', 36, struct ifconf32)
2641 ifioctl(struct socket *so, u_long cmd, caddr_t data, struct thread *td)
2648 CURVNET_SET(so->so_vnet);
2652 error = ifconf(cmd, data);
2656 #ifdef COMPAT_FREEBSD32
2659 struct ifconf32 *ifc32;
2662 ifc32 = (struct ifconf32 *)data;
2663 ifc.ifc_len = ifc32->ifc_len;
2664 ifc.ifc_buf = PTRIN(ifc32->ifc_buf);
2666 error = ifconf(SIOCGIFCONF, (void *)&ifc);
2669 ifc32->ifc_len = ifc.ifc_len;
2674 ifr = (struct ifreq *)data;
2679 error = priv_check(td, PRIV_NET_SETIFVNET);
2681 error = if_vmove_reclaim(td, ifr->ifr_name,
2688 error = priv_check(td, PRIV_NET_IFCREATE);
2690 error = if_clone_create(ifr->ifr_name,
2691 sizeof(ifr->ifr_name),
2692 cmd == SIOCIFCREATE2 ? ifr->ifr_data : NULL);
2696 error = priv_check(td, PRIV_NET_IFDESTROY);
2698 error = if_clone_destroy(ifr->ifr_name);
2702 case SIOCIFGCLONERS:
2703 error = if_clone_list((struct if_clonereq *)data);
2707 error = if_getgroupmembers((struct ifgroupreq *)data);
2710 #if defined(INET) || defined(INET6)
2713 if (carp_ioctl_p == NULL)
2714 error = EPROTONOSUPPORT;
2716 error = (*carp_ioctl_p)(ifr, cmd, td);
2722 ifp = ifunit_ref(ifr->ifr_name);
2728 error = ifhwioctl(cmd, ifp, data, td);
2729 if (error != ENOIOCTL) {
2735 oif_flags = ifp->if_flags;
2736 if (so->so_proto == NULL) {
2739 return (EOPNOTSUPP);
2743 * Pass the request on to the socket control method, and if the
2744 * latter returns EOPNOTSUPP, directly to the interface.
2746 * Make an exception for the legacy SIOCSIF* requests. Drivers
2747 * trust SIOCSIFADDR et al to come from an already privileged
2748 * layer, and do not perform any credentials checks or input
2752 error = ((*so->so_proto->pr_usrreqs->pru_control)(so, cmd,
2755 if (error == EOPNOTSUPP && ifp != NULL && ifp->if_ioctl != NULL &&
2756 cmd != SIOCSIFADDR && cmd != SIOCSIFBRDADDR &&
2757 cmd != SIOCSIFDSTADDR && cmd != SIOCSIFNETMASK)
2758 error = (*ifp->if_ioctl)(ifp, cmd, data);
2765 case SIOCSIFDSTADDR:
2767 case SIOCSIFBRDADDR:
2768 case SIOCSIFNETMASK:
2769 #if BYTE_ORDER != BIG_ENDIAN
2770 if (ifr->ifr_addr.sa_family == 0 &&
2771 ifr->ifr_addr.sa_len < 16) {
2772 ifr->ifr_addr.sa_family = ifr->ifr_addr.sa_len;
2773 ifr->ifr_addr.sa_len = 16;
2776 if (ifr->ifr_addr.sa_len == 0)
2777 ifr->ifr_addr.sa_len = 16;
2785 case OSIOCGIFDSTADDR:
2786 cmd = SIOCGIFDSTADDR;
2789 case OSIOCGIFBRDADDR:
2790 cmd = SIOCGIFBRDADDR;
2793 case OSIOCGIFNETMASK:
2794 cmd = SIOCGIFNETMASK;
2796 error = ((*so->so_proto->pr_usrreqs->pru_control)(so,
2800 if (error == EOPNOTSUPP && ifp != NULL &&
2801 ifp->if_ioctl != NULL &&
2802 cmd != SIOCSIFADDR && cmd != SIOCSIFBRDADDR &&
2803 cmd != SIOCSIFDSTADDR && cmd != SIOCSIFNETMASK)
2804 error = (*ifp->if_ioctl)(ifp, cmd, data);
2808 case OSIOCGIFDSTADDR:
2809 case OSIOCGIFBRDADDR:
2810 case OSIOCGIFNETMASK:
2811 *(u_short *)&ifr->ifr_addr = ifr->ifr_addr.sa_family;
2815 #endif /* COMPAT_43 */
2817 if ((oif_flags ^ ifp->if_flags) & IFF_UP) {
2819 if (ifp->if_flags & IFF_UP)
2829 * The code common to handling reference counted flags,
2830 * e.g., in ifpromisc() and if_allmulti().
2831 * The "pflag" argument can specify a permanent mode flag to check,
2832 * such as IFF_PPROMISC for promiscuous mode; should be 0 if none.
2834 * Only to be used on stack-owned flags, not driver-owned flags.
2837 if_setflag(struct ifnet *ifp, int flag, int pflag, int *refcount, int onswitch)
2841 int oldflags, oldcount;
2843 /* Sanity checks to catch programming errors */
2844 KASSERT((flag & (IFF_DRV_OACTIVE|IFF_DRV_RUNNING)) == 0,
2845 ("%s: setting driver-owned flag %d", __func__, flag));
2848 KASSERT(*refcount >= 0,
2849 ("%s: increment negative refcount %d for flag %d",
2850 __func__, *refcount, flag));
2852 KASSERT(*refcount > 0,
2853 ("%s: decrement non-positive refcount %d for flag %d",
2854 __func__, *refcount, flag));
2856 /* In case this mode is permanent, just touch refcount */
2857 if (ifp->if_flags & pflag) {
2858 *refcount += onswitch ? 1 : -1;
2862 /* Save ifnet parameters for if_ioctl() may fail */
2863 oldcount = *refcount;
2864 oldflags = ifp->if_flags;
2867 * See if we aren't the only and touching refcount is enough.
2868 * Actually toggle interface flag if we are the first or last.
2873 ifp->if_flags |= flag;
2877 ifp->if_flags &= ~flag;
2880 /* Call down the driver since we've changed interface flags */
2881 if (ifp->if_ioctl == NULL) {
2885 ifr.ifr_flags = ifp->if_flags & 0xffff;
2886 ifr.ifr_flagshigh = ifp->if_flags >> 16;
2887 error = (*ifp->if_ioctl)(ifp, SIOCSIFFLAGS, (caddr_t)&ifr);
2890 /* Notify userland that interface flags have changed */
2895 /* Recover after driver error */
2896 *refcount = oldcount;
2897 ifp->if_flags = oldflags;
2902 * Set/clear promiscuous mode on interface ifp based on the truth value
2903 * of pswitch. The calls are reference counted so that only the first
2904 * "on" request actually has an effect, as does the final "off" request.
2905 * Results are undefined if the "off" and "on" requests are not matched.
2908 ifpromisc(struct ifnet *ifp, int pswitch)
2911 int oldflags = ifp->if_flags;
2913 error = if_setflag(ifp, IFF_PROMISC, IFF_PPROMISC,
2914 &ifp->if_pcount, pswitch);
2915 /* If promiscuous mode status has changed, log a message */
2916 if (error == 0 && ((ifp->if_flags ^ oldflags) & IFF_PROMISC))
2917 log(LOG_INFO, "%s: promiscuous mode %s\n",
2919 (ifp->if_flags & IFF_PROMISC) ? "enabled" : "disabled");
2924 * Return interface configuration
2925 * of system. List may be used
2926 * in later ioctl's (above) to get
2927 * other information.
2931 ifconf(u_long cmd, caddr_t data)
2933 struct ifconf *ifc = (struct ifconf *)data;
2938 int error, full = 0, valid_len, max_len;
2940 /* Limit initial buffer size to MAXPHYS to avoid DoS from userspace. */
2941 max_len = MAXPHYS - 1;
2943 /* Prevent hostile input from being able to crash the system */
2944 if (ifc->ifc_len <= 0)
2948 if (ifc->ifc_len <= max_len) {
2949 max_len = ifc->ifc_len;
2952 sb = sbuf_new(NULL, NULL, max_len + 1, SBUF_FIXEDLEN);
2957 TAILQ_FOREACH(ifp, &V_ifnet, if_link) {
2961 * Zero the ifr_name buffer to make sure we don't
2962 * disclose the contents of the stack.
2964 memset(ifr.ifr_name, 0, sizeof(ifr.ifr_name));
2966 if (strlcpy(ifr.ifr_name, ifp->if_xname, sizeof(ifr.ifr_name))
2967 >= sizeof(ifr.ifr_name)) {
2970 return (ENAMETOOLONG);
2975 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
2976 struct sockaddr *sa = ifa->ifa_addr;
2978 if (prison_if(curthread->td_ucred, sa) != 0)
2982 if (cmd == OSIOCGIFCONF) {
2983 struct osockaddr *osa =
2984 (struct osockaddr *)&ifr.ifr_addr;
2986 osa->sa_family = sa->sa_family;
2987 sbuf_bcat(sb, &ifr, sizeof(ifr));
2988 max_len += sizeof(ifr);
2991 if (sa->sa_len <= sizeof(*sa)) {
2993 sbuf_bcat(sb, &ifr, sizeof(ifr));
2994 max_len += sizeof(ifr);
2997 offsetof(struct ifreq, ifr_addr));
2998 max_len += offsetof(struct ifreq, ifr_addr);
2999 sbuf_bcat(sb, sa, sa->sa_len);
3000 max_len += sa->sa_len;
3003 if (sbuf_error(sb) == 0)
3004 valid_len = sbuf_len(sb);
3006 IF_ADDR_RUNLOCK(ifp);
3008 bzero((caddr_t)&ifr.ifr_addr, sizeof(ifr.ifr_addr));
3009 sbuf_bcat(sb, &ifr, sizeof(ifr));
3010 max_len += sizeof(ifr);
3012 if (sbuf_error(sb) == 0)
3013 valid_len = sbuf_len(sb);
3019 * If we didn't allocate enough space (uncommon), try again. If
3020 * we have already allocated as much space as we are allowed,
3021 * return what we've got.
3023 if (valid_len != max_len && !full) {
3028 ifc->ifc_len = valid_len;
3030 error = copyout(sbuf_data(sb), ifc->ifc_req, ifc->ifc_len);
3036 * Just like ifpromisc(), but for all-multicast-reception mode.
3039 if_allmulti(struct ifnet *ifp, int onswitch)
3042 return (if_setflag(ifp, IFF_ALLMULTI, 0, &ifp->if_amcount, onswitch));
3045 struct ifmultiaddr *
3046 if_findmulti(struct ifnet *ifp, struct sockaddr *sa)
3048 struct ifmultiaddr *ifma;
3050 IF_ADDR_LOCK_ASSERT(ifp);
3052 TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
3053 if (sa->sa_family == AF_LINK) {
3054 if (sa_dl_equal(ifma->ifma_addr, sa))
3057 if (sa_equal(ifma->ifma_addr, sa))
3066 * Allocate a new ifmultiaddr and initialize based on passed arguments. We
3067 * make copies of passed sockaddrs. The ifmultiaddr will not be added to
3068 * the ifnet multicast address list here, so the caller must do that and
3069 * other setup work (such as notifying the device driver). The reference
3070 * count is initialized to 1.
3072 static struct ifmultiaddr *
3073 if_allocmulti(struct ifnet *ifp, struct sockaddr *sa, struct sockaddr *llsa,
3076 struct ifmultiaddr *ifma;
3077 struct sockaddr *dupsa;
3079 ifma = malloc(sizeof *ifma, M_IFMADDR, mflags |
3084 dupsa = malloc(sa->sa_len, M_IFMADDR, mflags);
3085 if (dupsa == NULL) {
3086 free(ifma, M_IFMADDR);
3089 bcopy(sa, dupsa, sa->sa_len);
3090 ifma->ifma_addr = dupsa;
3092 ifma->ifma_ifp = ifp;
3093 ifma->ifma_refcount = 1;
3094 ifma->ifma_protospec = NULL;
3097 ifma->ifma_lladdr = NULL;
3101 dupsa = malloc(llsa->sa_len, M_IFMADDR, mflags);
3102 if (dupsa == NULL) {
3103 free(ifma->ifma_addr, M_IFMADDR);
3104 free(ifma, M_IFMADDR);
3107 bcopy(llsa, dupsa, llsa->sa_len);
3108 ifma->ifma_lladdr = dupsa;
3114 * if_freemulti: free ifmultiaddr structure and possibly attached related
3115 * addresses. The caller is responsible for implementing reference
3116 * counting, notifying the driver, handling routing messages, and releasing
3117 * any dependent link layer state.
3120 if_freemulti(struct ifmultiaddr *ifma)
3123 KASSERT(ifma->ifma_refcount == 0, ("if_freemulti: refcount %d",
3124 ifma->ifma_refcount));
3125 KASSERT(ifma->ifma_protospec == NULL,
3126 ("if_freemulti: protospec not NULL"));
3128 if (ifma->ifma_lladdr != NULL)
3129 free(ifma->ifma_lladdr, M_IFMADDR);
3130 free(ifma->ifma_addr, M_IFMADDR);
3131 free(ifma, M_IFMADDR);
3135 * Register an additional multicast address with a network interface.
3137 * - If the address is already present, bump the reference count on the
3138 * address and return.
3139 * - If the address is not link-layer, look up a link layer address.
3140 * - Allocate address structures for one or both addresses, and attach to the
3141 * multicast address list on the interface. If automatically adding a link
3142 * layer address, the protocol address will own a reference to the link
3143 * layer address, to be freed when it is freed.
3144 * - Notify the network device driver of an addition to the multicast address
3147 * 'sa' points to caller-owned memory with the desired multicast address.
3149 * 'retifma' will be used to return a pointer to the resulting multicast
3150 * address reference, if desired.
3153 if_addmulti(struct ifnet *ifp, struct sockaddr *sa,
3154 struct ifmultiaddr **retifma)
3156 struct ifmultiaddr *ifma, *ll_ifma;
3157 struct sockaddr *llsa;
3161 * If the address is already present, return a new reference to it;
3162 * otherwise, allocate storage and set up a new address.
3165 ifma = if_findmulti(ifp, sa);
3167 ifma->ifma_refcount++;
3168 if (retifma != NULL)
3170 IF_ADDR_WUNLOCK(ifp);
3175 * The address isn't already present; resolve the protocol address
3176 * into a link layer address, and then look that up, bump its
3177 * refcount or allocate an ifma for that also. If 'llsa' was
3178 * returned, we will need to free it later.
3182 if (ifp->if_resolvemulti != NULL) {
3183 error = ifp->if_resolvemulti(ifp, &llsa, sa);
3189 * Allocate the new address. Don't hook it up yet, as we may also
3190 * need to allocate a link layer multicast address.
3192 ifma = if_allocmulti(ifp, sa, llsa, M_NOWAIT);
3199 * If a link layer address is found, we'll need to see if it's
3200 * already present in the address list, or allocate is as well.
3201 * When this block finishes, the link layer address will be on the
3205 ll_ifma = if_findmulti(ifp, llsa);
3206 if (ll_ifma == NULL) {
3207 ll_ifma = if_allocmulti(ifp, llsa, NULL, M_NOWAIT);
3208 if (ll_ifma == NULL) {
3209 --ifma->ifma_refcount;
3214 TAILQ_INSERT_HEAD(&ifp->if_multiaddrs, ll_ifma,
3217 ll_ifma->ifma_refcount++;
3218 ifma->ifma_llifma = ll_ifma;
3222 * We now have a new multicast address, ifma, and possibly a new or
3223 * referenced link layer address. Add the primary address to the
3224 * ifnet address list.
3226 TAILQ_INSERT_HEAD(&ifp->if_multiaddrs, ifma, ifma_link);
3228 if (retifma != NULL)
3232 * Must generate the message while holding the lock so that 'ifma'
3233 * pointer is still valid.
3235 rt_newmaddrmsg(RTM_NEWMADDR, ifma);
3236 IF_ADDR_WUNLOCK(ifp);
3239 * We are certain we have added something, so call down to the
3240 * interface to let them know about it.
3242 if (ifp->if_ioctl != NULL) {
3243 (void) (*ifp->if_ioctl)(ifp, SIOCADDMULTI, 0);
3247 free(llsa, M_IFMADDR);
3253 free(llsa, M_IFMADDR);
3256 IF_ADDR_WUNLOCK(ifp);
3261 * Delete a multicast group membership by network-layer group address.
3263 * Returns ENOENT if the entry could not be found. If ifp no longer
3264 * exists, results are undefined. This entry point should only be used
3265 * from subsystems which do appropriate locking to hold ifp for the
3266 * duration of the call.
3267 * Network-layer protocol domains must use if_delmulti_ifma().
3270 if_delmulti(struct ifnet *ifp, struct sockaddr *sa)
3272 struct ifmultiaddr *ifma;
3277 IFNET_RLOCK_NOSLEEP();
3278 TAILQ_FOREACH(oifp, &V_ifnet, if_link)
3283 IFNET_RUNLOCK_NOSLEEP();
3285 KASSERT(ifp != NULL, ("%s: ifnet went away", __func__));
3292 ifma = if_findmulti(ifp, sa);
3294 lastref = if_delmulti_locked(ifp, ifma, 0);
3295 IF_ADDR_WUNLOCK(ifp);
3300 if (lastref && ifp->if_ioctl != NULL) {
3301 (void)(*ifp->if_ioctl)(ifp, SIOCDELMULTI, 0);
3308 * Delete all multicast group membership for an interface.
3309 * Should be used to quickly flush all multicast filters.
3312 if_delallmulti(struct ifnet *ifp)
3314 struct ifmultiaddr *ifma;
3315 struct ifmultiaddr *next;
3318 TAILQ_FOREACH_SAFE(ifma, &ifp->if_multiaddrs, ifma_link, next)
3319 if_delmulti_locked(ifp, ifma, 0);
3320 IF_ADDR_WUNLOCK(ifp);
3324 * Delete a multicast group membership by group membership pointer.
3325 * Network-layer protocol domains must use this routine.
3327 * It is safe to call this routine if the ifp disappeared.
3330 if_delmulti_ifma(struct ifmultiaddr *ifma)
3335 ifp = ifma->ifma_ifp;
3338 printf("%s: ifma_ifp seems to be detached\n", __func__);
3342 IFNET_RLOCK_NOSLEEP();
3343 TAILQ_FOREACH(oifp, &V_ifnet, if_link)
3347 printf("%s: ifnet %p disappeared\n", __func__, ifp);
3350 IFNET_RUNLOCK_NOSLEEP();
3354 * If and only if the ifnet instance exists: Acquire the address lock.
3359 lastref = if_delmulti_locked(ifp, ifma, 0);
3363 * If and only if the ifnet instance exists:
3364 * Release the address lock.
3365 * If the group was left: update the hardware hash filter.
3367 IF_ADDR_WUNLOCK(ifp);
3368 if (lastref && ifp->if_ioctl != NULL) {
3369 (void)(*ifp->if_ioctl)(ifp, SIOCDELMULTI, 0);
3375 * Perform deletion of network-layer and/or link-layer multicast address.
3377 * Return 0 if the reference count was decremented.
3378 * Return 1 if the final reference was released, indicating that the
3379 * hardware hash filter should be reprogrammed.
3382 if_delmulti_locked(struct ifnet *ifp, struct ifmultiaddr *ifma, int detaching)
3384 struct ifmultiaddr *ll_ifma;
3386 if (ifp != NULL && ifma->ifma_ifp != NULL) {
3387 KASSERT(ifma->ifma_ifp == ifp,
3388 ("%s: inconsistent ifp %p", __func__, ifp));
3389 IF_ADDR_WLOCK_ASSERT(ifp);
3392 ifp = ifma->ifma_ifp;
3395 * If the ifnet is detaching, null out references to ifnet,
3396 * so that upper protocol layers will notice, and not attempt
3397 * to obtain locks for an ifnet which no longer exists. The
3398 * routing socket announcement must happen before the ifnet
3399 * instance is detached from the system.
3403 printf("%s: detaching ifnet instance %p\n", __func__, ifp);
3406 * ifp may already be nulled out if we are being reentered
3407 * to delete the ll_ifma.
3410 rt_newmaddrmsg(RTM_DELMADDR, ifma);
3411 ifma->ifma_ifp = NULL;
3415 if (--ifma->ifma_refcount > 0)
3419 * If this ifma is a network-layer ifma, a link-layer ifma may
3420 * have been associated with it. Release it first if so.
3422 ll_ifma = ifma->ifma_llifma;
3423 if (ll_ifma != NULL) {
3424 KASSERT(ifma->ifma_lladdr != NULL,
3425 ("%s: llifma w/o lladdr", __func__));
3427 ll_ifma->ifma_ifp = NULL; /* XXX */
3428 if (--ll_ifma->ifma_refcount == 0) {
3430 TAILQ_REMOVE(&ifp->if_multiaddrs, ll_ifma,
3433 if_freemulti(ll_ifma);
3438 TAILQ_REMOVE(&ifp->if_multiaddrs, ifma, ifma_link);
3443 * The last reference to this instance of struct ifmultiaddr
3444 * was released; the hardware should be notified of this change.
3450 * Set the link layer address on an interface.
3452 * At this time we only support certain types of interfaces,
3453 * and we don't allow the length of the address to change.
3456 if_setlladdr(struct ifnet *ifp, const u_char *lladdr, int len)
3458 struct sockaddr_dl *sdl;
3465 IF_ADDR_RUNLOCK(ifp);
3469 IF_ADDR_RUNLOCK(ifp);
3470 sdl = (struct sockaddr_dl *)ifa->ifa_addr;
3475 if (len != sdl->sdl_alen) { /* don't allow length to change */
3479 switch (ifp->if_type) {
3487 case IFT_IEEE8023ADLAG:
3489 bcopy(lladdr, LLADDR(sdl), len);
3498 * If the interface is already up, we need
3499 * to re-init it in order to reprogram its
3502 if ((ifp->if_flags & IFF_UP) != 0) {
3503 if (ifp->if_ioctl) {
3504 ifp->if_flags &= ~IFF_UP;
3505 ifr.ifr_flags = ifp->if_flags & 0xffff;
3506 ifr.ifr_flagshigh = ifp->if_flags >> 16;
3507 (*ifp->if_ioctl)(ifp, SIOCSIFFLAGS, (caddr_t)&ifr);
3508 ifp->if_flags |= IFF_UP;
3509 ifr.ifr_flags = ifp->if_flags & 0xffff;
3510 ifr.ifr_flagshigh = ifp->if_flags >> 16;
3511 (*ifp->if_ioctl)(ifp, SIOCSIFFLAGS, (caddr_t)&ifr);
3515 * Also send gratuitous ARPs to notify other nodes about
3516 * the address change.
3518 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
3519 if (ifa->ifa_addr->sa_family == AF_INET)
3520 arp_ifinit(ifp, ifa);
3528 * The name argument must be a pointer to storage which will last as
3529 * long as the interface does. For physical devices, the result of
3530 * device_get_name(dev) is a good choice and for pseudo-devices a
3531 * static string works well.
3534 if_initname(struct ifnet *ifp, const char *name, int unit)
3536 ifp->if_dname = name;
3537 ifp->if_dunit = unit;
3538 if (unit != IF_DUNIT_NONE)
3539 snprintf(ifp->if_xname, IFNAMSIZ, "%s%d", name, unit);
3541 strlcpy(ifp->if_xname, name, IFNAMSIZ);
3545 if_printf(struct ifnet *ifp, const char * fmt, ...)
3550 retval = printf("%s: ", ifp->if_xname);
3552 retval += vprintf(fmt, ap);
3558 if_start(struct ifnet *ifp)
3561 (*(ifp)->if_start)(ifp);
3565 * Backwards compatibility interface for drivers
3566 * that have not implemented it
3569 if_transmit(struct ifnet *ifp, struct mbuf *m)
3573 IFQ_HANDOFF(ifp, m, error);
3578 if_input_default(struct ifnet *ifp __unused, struct mbuf *m)
3585 if_handoff(struct ifqueue *ifq, struct mbuf *m, struct ifnet *ifp, int adjust)
3590 if (_IF_QFULL(ifq)) {
3597 ifp->if_obytes += m->m_pkthdr.len + adjust;
3598 if (m->m_flags & (M_BCAST|M_MCAST))
3600 active = ifp->if_drv_flags & IFF_DRV_OACTIVE;
3602 _IF_ENQUEUE(ifq, m);
3604 if (ifp != NULL && !active)
3605 (*(ifp)->if_start)(ifp);
3610 if_register_com_alloc(u_char type,
3611 if_com_alloc_t *a, if_com_free_t *f)
3614 KASSERT(if_com_alloc[type] == NULL,
3615 ("if_register_com_alloc: %d already registered", type));
3616 KASSERT(if_com_free[type] == NULL,
3617 ("if_register_com_alloc: %d free already registered", type));
3619 if_com_alloc[type] = a;
3620 if_com_free[type] = f;
3624 if_deregister_com_alloc(u_char type)
3627 KASSERT(if_com_alloc[type] != NULL,
3628 ("if_deregister_com_alloc: %d not registered", type));
3629 KASSERT(if_com_free[type] != NULL,
3630 ("if_deregister_com_alloc: %d free not registered", type));
3631 if_com_alloc[type] = NULL;
3632 if_com_free[type] = NULL;