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 #if defined(INET) || defined(INET6)
134 struct ifnet *(*carp_forus_p)(struct ifnet *ifp, u_char *dhost);
135 int (*carp_output_p)(struct ifnet *ifp, struct mbuf *m,
136 struct sockaddr *sa, struct rtentry *rt);
139 int (*carp_iamatch_p)(struct ifnet *, struct in_ifaddr *, struct in_addr *,
143 struct ifaddr *(*carp_iamatch6_p)(struct ifnet *ifp, struct in6_addr *taddr6);
144 caddr_t (*carp_macmatch6_p)(struct ifnet *ifp, struct mbuf *m,
145 const struct in6_addr *taddr);
148 struct mbuf *(*tbr_dequeue_ptr)(struct ifaltq *, int) = NULL;
151 * XXX: Style; these should be sorted alphabetically, and unprototyped
152 * static functions should be prototyped. Currently they are sorted by
155 static void if_attachdomain(void *);
156 static void if_attachdomain1(struct ifnet *);
157 static int ifconf(u_long, caddr_t);
158 static void if_freemulti(struct ifmultiaddr *);
159 static void if_init(void *);
160 static void if_grow(void);
161 static void if_input_default(struct ifnet *, struct mbuf *);
162 static void if_route(struct ifnet *, int flag, int fam);
163 static int if_setflag(struct ifnet *, int, int, int *, int);
164 static int if_transmit(struct ifnet *ifp, struct mbuf *m);
165 static void if_unroute(struct ifnet *, int flag, int fam);
166 static void link_rtrequest(int, struct rtentry *, struct rt_addrinfo *);
167 static int if_rtdel(struct radix_node *, void *);
168 static int ifhwioctl(u_long, struct ifnet *, caddr_t, struct thread *);
169 static int if_delmulti_locked(struct ifnet *, struct ifmultiaddr *, int);
170 static void do_link_state_change(void *, int);
171 static int if_getgroup(struct ifgroupreq *, struct ifnet *);
172 static int if_getgroupmembers(struct ifgroupreq *);
173 static void if_delgroups(struct ifnet *);
174 static void if_attach_internal(struct ifnet *, int);
175 static void if_detach_internal(struct ifnet *, int);
179 * XXX: declare here to avoid to include many inet6 related files..
180 * should be more generalized?
182 extern void nd6_setmtu(struct ifnet *);
185 VNET_DEFINE(int, if_index);
186 int ifqmaxlen = IFQ_MAXLEN;
187 VNET_DEFINE(struct ifnethead, ifnet); /* depend on static init XXX */
188 VNET_DEFINE(struct ifgrouphead, ifg_head);
190 static VNET_DEFINE(int, if_indexlim) = 8;
192 /* Table of ifnet by index. */
193 VNET_DEFINE(struct ifindex_entry *, ifindex_table);
195 #define V_if_indexlim VNET(if_indexlim)
196 #define V_ifindex_table VNET(ifindex_table)
199 * The global network interface list (V_ifnet) and related state (such as
200 * if_index, if_indexlim, and ifindex_table) are protected by an sxlock and
201 * an rwlock. Either may be acquired shared to stablize the list, but both
202 * must be acquired writable to modify the list. This model allows us to
203 * both stablize the interface list during interrupt thread processing, but
204 * also to stablize it over long-running ioctls, without introducing priority
205 * inversions and deadlocks.
207 struct rwlock ifnet_rwlock;
208 struct sx ifnet_sxlock;
211 * The allocation of network interfaces is a rather non-atomic affair; we
212 * need to select an index before we are ready to expose the interface for
213 * use, so will use this pointer value to indicate reservation.
215 #define IFNET_HOLD (void *)(uintptr_t)(-1)
217 static if_com_alloc_t *if_com_alloc[256];
218 static if_com_free_t *if_com_free[256];
220 static MALLOC_DEFINE(M_IFNET, "ifnet", "interface internals");
221 MALLOC_DEFINE(M_IFADDR, "ifaddr", "interface address");
222 MALLOC_DEFINE(M_IFMADDR, "ether_multi", "link-level multicast address");
225 ifnet_byindex_locked(u_short idx)
228 if (idx > V_if_index)
230 if (V_ifindex_table[idx].ife_ifnet == IFNET_HOLD)
232 return (V_ifindex_table[idx].ife_ifnet);
236 ifnet_byindex(u_short idx)
240 IFNET_RLOCK_NOSLEEP();
241 ifp = ifnet_byindex_locked(idx);
242 IFNET_RUNLOCK_NOSLEEP();
247 ifnet_byindex_ref(u_short idx)
251 IFNET_RLOCK_NOSLEEP();
252 ifp = ifnet_byindex_locked(idx);
253 if (ifp == NULL || (ifp->if_flags & IFF_DYING)) {
254 IFNET_RUNLOCK_NOSLEEP();
258 IFNET_RUNLOCK_NOSLEEP();
263 * Allocate an ifindex array entry; return 0 on success or an error on
267 ifindex_alloc_locked(u_short *idxp)
271 IFNET_WLOCK_ASSERT();
275 * Try to find an empty slot below V_if_index. If we fail, take the
278 for (idx = 1; idx <= V_if_index; idx++) {
279 if (V_ifindex_table[idx].ife_ifnet == NULL)
283 /* Catch if_index overflow. */
286 if (idx >= V_if_indexlim) {
290 if (idx > V_if_index)
297 ifindex_free_locked(u_short idx)
300 IFNET_WLOCK_ASSERT();
302 V_ifindex_table[idx].ife_ifnet = NULL;
303 while (V_if_index > 0 &&
304 V_ifindex_table[V_if_index].ife_ifnet == NULL)
309 ifindex_free(u_short idx)
313 ifindex_free_locked(idx);
318 ifnet_setbyindex_locked(u_short idx, struct ifnet *ifp)
321 IFNET_WLOCK_ASSERT();
323 V_ifindex_table[idx].ife_ifnet = ifp;
327 ifnet_setbyindex(u_short idx, struct ifnet *ifp)
331 ifnet_setbyindex_locked(idx, ifp);
336 ifaddr_byindex(u_short idx)
340 IFNET_RLOCK_NOSLEEP();
341 ifa = ifnet_byindex_locked(idx)->if_addr;
344 IFNET_RUNLOCK_NOSLEEP();
349 * Network interface utility routines.
351 * Routines with ifa_ifwith* names take sockaddr *'s as
356 vnet_if_init(const void *unused __unused)
359 TAILQ_INIT(&V_ifnet);
360 TAILQ_INIT(&V_ifg_head);
362 if_grow(); /* create initial table */
364 vnet_if_clone_init();
366 VNET_SYSINIT(vnet_if_init, SI_SUB_INIT_IF, SI_ORDER_SECOND, vnet_if_init,
371 if_init(void *dummy __unused)
377 SYSINIT(interfaces, SI_SUB_INIT_IF, SI_ORDER_FIRST, if_init, NULL);
382 vnet_if_uninit(const void *unused __unused)
385 VNET_ASSERT(TAILQ_EMPTY(&V_ifnet), ("%s:%d tailq &V_ifnet=%p "
386 "not empty", __func__, __LINE__, &V_ifnet));
387 VNET_ASSERT(TAILQ_EMPTY(&V_ifg_head), ("%s:%d tailq &V_ifg_head=%p "
388 "not empty", __func__, __LINE__, &V_ifg_head));
390 free((caddr_t)V_ifindex_table, M_IFNET);
392 VNET_SYSUNINIT(vnet_if_uninit, SI_SUB_INIT_IF, SI_ORDER_FIRST,
393 vnet_if_uninit, NULL);
401 struct ifindex_entry *e;
403 IFNET_WLOCK_ASSERT();
404 oldlim = V_if_indexlim;
406 n = (oldlim << 1) * sizeof(*e);
407 e = malloc(n, M_IFNET, M_WAITOK | M_ZERO);
409 if (V_if_indexlim != oldlim) {
413 if (V_ifindex_table != NULL) {
414 memcpy((caddr_t)e, (caddr_t)V_ifindex_table, n/2);
415 free((caddr_t)V_ifindex_table, M_IFNET);
422 * Allocate a struct ifnet and an index for an interface. A layer 2
423 * common structure will also be allocated if an allocation routine is
424 * registered for the passed type.
427 if_alloc(u_char type)
432 ifp = malloc(sizeof(struct ifnet), M_IFNET, M_WAITOK|M_ZERO);
434 if (ifindex_alloc_locked(&idx) != 0) {
439 ifnet_setbyindex_locked(idx, IFNET_HOLD);
443 ifp->if_alloctype = type;
444 if (if_com_alloc[type] != NULL) {
445 ifp->if_l2com = if_com_alloc[type](type, ifp);
446 if (ifp->if_l2com == NULL) {
453 IF_ADDR_LOCK_INIT(ifp);
454 TASK_INIT(&ifp->if_linktask, 0, do_link_state_change, ifp);
455 ifp->if_afdata_initialized = 0;
456 IF_AFDATA_LOCK_INIT(ifp);
457 TAILQ_INIT(&ifp->if_addrhead);
458 TAILQ_INIT(&ifp->if_prefixhead);
459 TAILQ_INIT(&ifp->if_multiaddrs);
460 TAILQ_INIT(&ifp->if_groups);
464 ifq_init(&ifp->if_snd, ifp);
466 refcount_init(&ifp->if_refcount, 1); /* Index reference. */
467 ifnet_setbyindex(ifp->if_index, ifp);
472 * Do the actual work of freeing a struct ifnet, and layer 2 common
473 * structure. This call is made when the last reference to an
474 * interface is released.
477 if_free_internal(struct ifnet *ifp)
480 KASSERT((ifp->if_flags & IFF_DYING),
481 ("if_free_internal: interface not dying"));
483 if (if_com_free[ifp->if_alloctype] != NULL)
484 if_com_free[ifp->if_alloctype](ifp->if_l2com,
488 mac_ifnet_destroy(ifp);
490 if (ifp->if_description != NULL)
491 free(ifp->if_description, M_IFDESCR);
492 IF_AFDATA_DESTROY(ifp);
493 IF_ADDR_LOCK_DESTROY(ifp);
494 ifq_delete(&ifp->if_snd);
499 * This version should only be called by intefaces that switch their type
500 * after calling if_alloc(). if_free_type() will go away again now that we
501 * have if_alloctype to cache the original allocation type. For now, assert
502 * that they match, since we require that in practice.
505 if_free_type(struct ifnet *ifp, u_char type)
508 KASSERT(ifp->if_alloctype == type,
509 ("if_free_type: type (%d) != alloctype (%d)", type,
512 ifp->if_flags |= IFF_DYING; /* XXX: Locking */
514 CURVNET_SET_QUIET(ifp->if_vnet);
516 KASSERT(ifp == ifnet_byindex_locked(ifp->if_index),
517 ("%s: freeing unallocated ifnet", ifp->if_xname));
519 ifindex_free_locked(ifp->if_index);
522 if (refcount_release(&ifp->if_refcount))
523 if_free_internal(ifp);
528 * This is the normal version of if_free(), used by device drivers to free a
529 * detached network interface. The contents of if_free_type() will move into
530 * here when if_free_type() goes away.
533 if_free(struct ifnet *ifp)
536 if_free_type(ifp, ifp->if_alloctype);
540 * Interfaces to keep an ifnet type-stable despite the possibility of the
541 * driver calling if_free(). If there are additional references, we defer
542 * freeing the underlying data structure.
545 if_ref(struct ifnet *ifp)
548 /* We don't assert the ifnet list lock here, but arguably should. */
549 refcount_acquire(&ifp->if_refcount);
553 if_rele(struct ifnet *ifp)
556 if (!refcount_release(&ifp->if_refcount))
558 if_free_internal(ifp);
562 ifq_init(struct ifaltq *ifq, struct ifnet *ifp)
565 mtx_init(&ifq->ifq_mtx, ifp->if_xname, "if send queue", MTX_DEF);
567 if (ifq->ifq_maxlen == 0)
568 ifq->ifq_maxlen = ifqmaxlen;
571 ifq->altq_disc = NULL;
572 ifq->altq_flags &= ALTQF_CANTCHANGE;
573 ifq->altq_tbr = NULL;
578 ifq_delete(struct ifaltq *ifq)
580 mtx_destroy(&ifq->ifq_mtx);
584 * Perform generic interface initalization tasks and attach the interface
585 * to the list of "active" interfaces. If vmove flag is set on entry
586 * to if_attach_internal(), perform only a limited subset of initialization
587 * tasks, given that we are moving from one vnet to another an ifnet which
588 * has already been fully initialized.
591 * - The decision to return void and thus require this function to
592 * succeed is questionable.
593 * - We should probably do more sanity checking. For instance we don't
594 * do anything to insure if_xname is unique or non-empty.
597 if_attach(struct ifnet *ifp)
600 if_attach_internal(ifp, 0);
604 * Compute the least common TSO limit.
607 if_hw_tsomax_common(struct ifnet *ifp, struct ifnet_hw_tsomax *pmax)
610 * 1) If there is no limit currently, take the limit from
611 * the network adapter.
613 * 2) If the network adapter has a limit below the current
616 if (pmax->tsomaxbytes == 0 || (ifp->if_hw_tsomax != 0 &&
617 ifp->if_hw_tsomax < pmax->tsomaxbytes)) {
618 pmax->tsomaxbytes = ifp->if_hw_tsomax;
620 if (pmax->tsomaxsegcount == 0 || (ifp->if_hw_tsomaxsegcount != 0 &&
621 ifp->if_hw_tsomaxsegcount < pmax->tsomaxsegcount)) {
622 pmax->tsomaxsegcount = ifp->if_hw_tsomaxsegcount;
624 if (pmax->tsomaxsegsize == 0 || (ifp->if_hw_tsomaxsegsize != 0 &&
625 ifp->if_hw_tsomaxsegsize < pmax->tsomaxsegsize)) {
626 pmax->tsomaxsegsize = ifp->if_hw_tsomaxsegsize;
631 * Update TSO limit of a network adapter.
633 * Returns zero if no change. Else non-zero.
636 if_hw_tsomax_update(struct ifnet *ifp, struct ifnet_hw_tsomax *pmax)
639 if (ifp->if_hw_tsomax != pmax->tsomaxbytes) {
640 ifp->if_hw_tsomax = pmax->tsomaxbytes;
643 if (ifp->if_hw_tsomaxsegsize != pmax->tsomaxsegsize) {
644 ifp->if_hw_tsomaxsegsize = pmax->tsomaxsegsize;
647 if (ifp->if_hw_tsomaxsegcount != pmax->tsomaxsegcount) {
648 ifp->if_hw_tsomaxsegcount = pmax->tsomaxsegcount;
655 if_attach_internal(struct ifnet *ifp, int vmove)
657 unsigned socksize, ifasize;
658 int namelen, masklen;
659 struct sockaddr_dl *sdl;
662 if (ifp->if_index == 0 || ifp != ifnet_byindex(ifp->if_index))
663 panic ("%s: BUG: if_attach called without if_alloc'd input()\n",
667 ifp->if_vnet = curvnet;
668 if (ifp->if_home_vnet == NULL)
669 ifp->if_home_vnet = curvnet;
672 if_addgroup(ifp, IFG_ALL);
674 getmicrotime(&ifp->if_lastchange);
675 ifp->if_data.ifi_epoch = time_uptime;
676 ifp->if_data.ifi_datalen = sizeof(struct if_data);
678 KASSERT((ifp->if_transmit == NULL && ifp->if_qflush == NULL) ||
679 (ifp->if_transmit != NULL && ifp->if_qflush != NULL),
680 ("transmit and qflush must both either be set or both be NULL"));
681 if (ifp->if_transmit == NULL) {
682 ifp->if_transmit = if_transmit;
683 ifp->if_qflush = if_qflush;
685 if (ifp->if_input == NULL)
686 ifp->if_input = if_input_default;
690 mac_ifnet_create(ifp);
694 * Create a Link Level name for this device.
696 namelen = strlen(ifp->if_xname);
698 * Always save enough space for any possiable name so we
699 * can do a rename in place later.
701 masklen = offsetof(struct sockaddr_dl, sdl_data[0]) + IFNAMSIZ;
702 socksize = masklen + ifp->if_addrlen;
703 if (socksize < sizeof(*sdl))
704 socksize = sizeof(*sdl);
705 socksize = roundup2(socksize, sizeof(long));
706 ifasize = sizeof(*ifa) + 2 * socksize;
707 ifa = malloc(ifasize, M_IFADDR, M_WAITOK | M_ZERO);
709 sdl = (struct sockaddr_dl *)(ifa + 1);
710 sdl->sdl_len = socksize;
711 sdl->sdl_family = AF_LINK;
712 bcopy(ifp->if_xname, sdl->sdl_data, namelen);
713 sdl->sdl_nlen = namelen;
714 sdl->sdl_index = ifp->if_index;
715 sdl->sdl_type = ifp->if_type;
718 ifa->ifa_rtrequest = link_rtrequest;
719 ifa->ifa_addr = (struct sockaddr *)sdl;
720 sdl = (struct sockaddr_dl *)(socksize + (caddr_t)sdl);
721 ifa->ifa_netmask = (struct sockaddr *)sdl;
722 sdl->sdl_len = masklen;
724 sdl->sdl_data[--namelen] = 0xff;
725 TAILQ_INSERT_HEAD(&ifp->if_addrhead, ifa, ifa_link);
726 /* Reliably crash if used uninitialized. */
727 ifp->if_broadcastaddr = NULL;
729 #if defined(INET) || defined(INET6)
730 /* Use defaults for TSO, if nothing is set */
731 if (ifp->if_hw_tsomax == 0 &&
732 ifp->if_hw_tsomaxsegcount == 0 &&
733 ifp->if_hw_tsomaxsegsize == 0) {
735 * The TSO defaults needs to be such that an
736 * NFS mbuf list of 35 mbufs totalling just
737 * below 64K works and that a chain of mbufs
738 * can be defragged into at most 32 segments:
740 ifp->if_hw_tsomax = min(IP_MAXPACKET, (32 * MCLBYTES) -
741 (ETHER_HDR_LEN + ETHER_VLAN_ENCAP_LEN));
742 ifp->if_hw_tsomaxsegcount = 35;
743 ifp->if_hw_tsomaxsegsize = 2048; /* 2K */
745 /* XXX some drivers set IFCAP_TSO after ethernet attach */
746 if (ifp->if_capabilities & IFCAP_TSO) {
747 if_printf(ifp, "Using defaults for TSO: %u/%u/%u\n",
749 ifp->if_hw_tsomaxsegcount,
750 ifp->if_hw_tsomaxsegsize);
758 * Update the interface index in the link layer address
761 for (ifa = ifp->if_addr; ifa != NULL;
762 ifa = TAILQ_NEXT(ifa, ifa_link)) {
763 if (ifa->ifa_addr->sa_family == AF_LINK) {
764 sdl = (struct sockaddr_dl *)ifa->ifa_addr;
765 sdl->sdl_index = ifp->if_index;
772 TAILQ_INSERT_TAIL(&V_ifnet, ifp, if_link);
774 curvnet->vnet_ifcnt++;
778 if (domain_init_status >= 2)
779 if_attachdomain1(ifp);
781 EVENTHANDLER_INVOKE(ifnet_arrival_event, ifp);
782 if (IS_DEFAULT_VNET(curvnet))
783 devctl_notify("IFNET", ifp->if_xname, "ATTACH", NULL);
785 /* Announce the interface. */
786 rt_ifannouncemsg(ifp, IFAN_ARRIVAL);
790 if_attachdomain(void *dummy)
796 TAILQ_FOREACH(ifp, &V_ifnet, if_link)
797 if_attachdomain1(ifp);
800 SYSINIT(domainifattach, SI_SUB_PROTO_IFATTACHDOMAIN, SI_ORDER_SECOND,
801 if_attachdomain, NULL);
804 if_attachdomain1(struct ifnet *ifp)
812 * Since dp->dom_ifattach calls malloc() with M_WAITOK, we
813 * cannot lock ifp->if_afdata initialization, entirely.
815 if (IF_AFDATA_TRYLOCK(ifp) == 0) {
819 if (ifp->if_afdata_initialized >= domain_init_status) {
820 IF_AFDATA_UNLOCK(ifp);
822 printf("if_attachdomain called more than once on %s\n",
826 ifp->if_afdata_initialized = domain_init_status;
827 IF_AFDATA_UNLOCK(ifp);
829 /* address family dependent data region */
830 bzero(ifp->if_afdata, sizeof(ifp->if_afdata));
831 for (dp = domains; dp; dp = dp->dom_next) {
832 if (dp->dom_ifattach)
833 ifp->if_afdata[dp->dom_family] =
834 (*dp->dom_ifattach)(ifp);
841 * Remove any unicast or broadcast network addresses from an interface.
844 if_purgeaddrs(struct ifnet *ifp)
846 struct ifaddr *ifa, *next;
848 TAILQ_FOREACH_SAFE(ifa, &ifp->if_addrhead, ifa_link, next) {
849 if (ifa->ifa_addr->sa_family == AF_LINK)
852 /* XXX: Ugly!! ad hoc just for INET */
853 if (ifa->ifa_addr->sa_family == AF_INET) {
854 struct ifaliasreq ifr;
856 bzero(&ifr, sizeof(ifr));
857 ifr.ifra_addr = *ifa->ifa_addr;
858 if (ifa->ifa_dstaddr)
859 ifr.ifra_broadaddr = *ifa->ifa_dstaddr;
860 if (in_control(NULL, SIOCDIFADDR, (caddr_t)&ifr, ifp,
866 if (ifa->ifa_addr->sa_family == AF_INET6) {
868 /* ifp_addrhead is already updated */
872 TAILQ_REMOVE(&ifp->if_addrhead, ifa, ifa_link);
878 * Remove any multicast network addresses from an interface when an ifnet
882 if_purgemaddrs(struct ifnet *ifp)
884 struct ifmultiaddr *ifma;
885 struct ifmultiaddr *next;
888 TAILQ_FOREACH_SAFE(ifma, &ifp->if_multiaddrs, ifma_link, next)
889 if_delmulti_locked(ifp, ifma, 1);
890 IF_ADDR_WUNLOCK(ifp);
894 * Detach an interface, removing it from the list of "active" interfaces.
895 * If vmove flag is set on entry to if_detach_internal(), perform only a
896 * limited subset of cleanup tasks, given that we are moving an ifnet from
897 * one vnet to another, where it must be fully operational.
899 * XXXRW: There are some significant questions about event ordering, and
900 * how to prevent things from starting to use the interface during detach.
903 if_detach(struct ifnet *ifp)
906 CURVNET_SET_QUIET(ifp->if_vnet);
907 if_detach_internal(ifp, 0);
912 if_detach_internal(struct ifnet *ifp, int vmove)
915 struct radix_node_head *rnh;
922 TAILQ_FOREACH(iter, &V_ifnet, if_link)
924 TAILQ_REMOVE(&V_ifnet, ifp, if_link);
930 curvnet->vnet_ifcnt--;
935 panic("%s: ifp=%p not on the ifnet tailq %p",
936 __func__, ifp, &V_ifnet);
938 return; /* XXX this should panic as well? */
942 * Remove/wait for pending events.
944 taskqueue_drain(taskqueue_swi, &ifp->if_linktask);
947 * Remove routes and flush queues.
951 if (ALTQ_IS_ENABLED(&ifp->if_snd))
952 altq_disable(&ifp->if_snd);
953 if (ALTQ_IS_ATTACHED(&ifp->if_snd))
954 altq_detach(&ifp->if_snd);
965 * Remove all IPv6 kernel structs related to ifp. This should be done
966 * before removing routing entries below, since IPv6 interface direct
967 * routes are expected to be removed by the IPv6-specific kernel API.
968 * Otherwise, the kernel will detect some inconsistency and bark it.
976 * Prevent further calls into the device driver via ifnet.
981 * Remove link ifaddr pointer and maybe decrement if_index.
982 * Clean up all addresses.
986 /* We can now free link ifaddr. */
987 if (!TAILQ_EMPTY(&ifp->if_addrhead)) {
988 ifa = TAILQ_FIRST(&ifp->if_addrhead);
989 TAILQ_REMOVE(&ifp->if_addrhead, ifa, ifa_link);
995 * Delete all remaining routes using this interface
996 * Unfortuneatly the only way to do this is to slog through
997 * the entire routing table looking for routes which point
998 * to this interface...oh well...
1000 for (i = 1; i <= AF_MAX; i++) {
1001 for (j = 0; j < rt_numfibs; j++) {
1002 rnh = rt_tables_get_rnh(j, i);
1005 RADIX_NODE_HEAD_LOCK(rnh);
1006 (void) rnh->rnh_walktree(rnh, if_rtdel, ifp);
1007 RADIX_NODE_HEAD_UNLOCK(rnh);
1011 /* Announce that the interface is gone. */
1012 rt_ifannouncemsg(ifp, IFAN_DEPARTURE);
1013 EVENTHANDLER_INVOKE(ifnet_departure_event, ifp);
1014 if (IS_DEFAULT_VNET(curvnet))
1015 devctl_notify("IFNET", ifp->if_xname, "DETACH", NULL);
1019 * We cannot hold the lock over dom_ifdetach calls as they might
1020 * sleep, for example trying to drain a callout, thus open up the
1021 * theoretical race with re-attaching.
1023 IF_AFDATA_LOCK(ifp);
1024 i = ifp->if_afdata_initialized;
1025 ifp->if_afdata_initialized = 0;
1026 IF_AFDATA_UNLOCK(ifp);
1027 for (dp = domains; i > 0 && dp; dp = dp->dom_next) {
1028 if (dp->dom_ifdetach && ifp->if_afdata[dp->dom_family])
1029 (*dp->dom_ifdetach)(ifp,
1030 ifp->if_afdata[dp->dom_family]);
1036 * if_vmove() performs a limited version of if_detach() in current
1037 * vnet and if_attach()es the ifnet to the vnet specified as 2nd arg.
1038 * An attempt is made to shrink if_index in current vnet, find an
1039 * unused if_index in target vnet and calls if_grow() if necessary,
1040 * and finally find an unused if_xname for the target vnet.
1043 if_vmove(struct ifnet *ifp, struct vnet *new_vnet)
1048 * Detach from current vnet, but preserve LLADDR info, do not
1049 * mark as dead etc. so that the ifnet can be reattached later.
1051 if_detach_internal(ifp, 1);
1054 * Unlink the ifnet from ifindex_table[] in current vnet, and shrink
1055 * the if_index for that vnet if possible.
1057 * NOTE: IFNET_WLOCK/IFNET_WUNLOCK() are assumed to be unvirtualized,
1058 * or we'd lock on one vnet and unlock on another.
1061 ifindex_free_locked(ifp->if_index);
1065 * Perform interface-specific reassignment tasks, if provided by
1068 if (ifp->if_reassign != NULL)
1069 ifp->if_reassign(ifp, new_vnet, NULL);
1072 * Switch to the context of the target vnet.
1074 CURVNET_SET_QUIET(new_vnet);
1077 if (ifindex_alloc_locked(&idx) != 0) {
1079 panic("if_index overflow");
1081 ifp->if_index = idx;
1082 ifnet_setbyindex_locked(ifp->if_index, ifp);
1085 if_attach_internal(ifp, 1);
1091 * Move an ifnet to or from another child prison/vnet, specified by the jail id.
1094 if_vmove_loan(struct thread *td, struct ifnet *ifp, char *ifname, int jid)
1099 /* Try to find the prison within our visibility. */
1100 sx_slock(&allprison_lock);
1101 pr = prison_find_child(td->td_ucred->cr_prison, jid);
1102 sx_sunlock(&allprison_lock);
1105 prison_hold_locked(pr);
1106 mtx_unlock(&pr->pr_mtx);
1108 /* Do not try to move the iface from and to the same prison. */
1109 if (pr->pr_vnet == ifp->if_vnet) {
1114 /* Make sure the named iface does not exists in the dst. prison/vnet. */
1115 /* XXX Lock interfaces to avoid races. */
1116 CURVNET_SET_QUIET(pr->pr_vnet);
1117 difp = ifunit(ifname);
1124 /* Move the interface into the child jail/vnet. */
1125 if_vmove(ifp, pr->pr_vnet);
1127 /* Report the new if_xname back to the userland. */
1128 sprintf(ifname, "%s", ifp->if_xname);
1135 if_vmove_reclaim(struct thread *td, char *ifname, int jid)
1138 struct vnet *vnet_dst;
1141 /* Try to find the prison within our visibility. */
1142 sx_slock(&allprison_lock);
1143 pr = prison_find_child(td->td_ucred->cr_prison, jid);
1144 sx_sunlock(&allprison_lock);
1147 prison_hold_locked(pr);
1148 mtx_unlock(&pr->pr_mtx);
1150 /* Make sure the named iface exists in the source prison/vnet. */
1151 CURVNET_SET(pr->pr_vnet);
1152 ifp = ifunit(ifname); /* XXX Lock to avoid races. */
1159 /* Do not try to move the iface from and to the same prison. */
1160 vnet_dst = TD_TO_VNET(td);
1161 if (vnet_dst == ifp->if_vnet) {
1167 /* Get interface back from child jail/vnet. */
1168 if_vmove(ifp, vnet_dst);
1171 /* Report the new if_xname back to the userland. */
1172 sprintf(ifname, "%s", ifp->if_xname);
1180 * Add a group to an interface
1183 if_addgroup(struct ifnet *ifp, const char *groupname)
1185 struct ifg_list *ifgl;
1186 struct ifg_group *ifg = NULL;
1187 struct ifg_member *ifgm;
1189 if (groupname[0] && groupname[strlen(groupname) - 1] >= '0' &&
1190 groupname[strlen(groupname) - 1] <= '9')
1194 TAILQ_FOREACH(ifgl, &ifp->if_groups, ifgl_next)
1195 if (!strcmp(ifgl->ifgl_group->ifg_group, groupname)) {
1200 if ((ifgl = (struct ifg_list *)malloc(sizeof(struct ifg_list), M_TEMP,
1201 M_NOWAIT)) == NULL) {
1206 if ((ifgm = (struct ifg_member *)malloc(sizeof(struct ifg_member),
1207 M_TEMP, M_NOWAIT)) == NULL) {
1213 TAILQ_FOREACH(ifg, &V_ifg_head, ifg_next)
1214 if (!strcmp(ifg->ifg_group, groupname))
1218 if ((ifg = (struct ifg_group *)malloc(sizeof(struct ifg_group),
1219 M_TEMP, M_NOWAIT)) == NULL) {
1225 strlcpy(ifg->ifg_group, groupname, sizeof(ifg->ifg_group));
1226 ifg->ifg_refcnt = 0;
1227 TAILQ_INIT(&ifg->ifg_members);
1228 EVENTHANDLER_INVOKE(group_attach_event, ifg);
1229 TAILQ_INSERT_TAIL(&V_ifg_head, ifg, ifg_next);
1233 ifgl->ifgl_group = ifg;
1234 ifgm->ifgm_ifp = ifp;
1237 TAILQ_INSERT_TAIL(&ifg->ifg_members, ifgm, ifgm_next);
1238 TAILQ_INSERT_TAIL(&ifp->if_groups, ifgl, ifgl_next);
1239 IF_ADDR_WUNLOCK(ifp);
1243 EVENTHANDLER_INVOKE(group_change_event, groupname);
1249 * Remove a group from an interface
1252 if_delgroup(struct ifnet *ifp, const char *groupname)
1254 struct ifg_list *ifgl;
1255 struct ifg_member *ifgm;
1258 TAILQ_FOREACH(ifgl, &ifp->if_groups, ifgl_next)
1259 if (!strcmp(ifgl->ifgl_group->ifg_group, groupname))
1267 TAILQ_REMOVE(&ifp->if_groups, ifgl, ifgl_next);
1268 IF_ADDR_WUNLOCK(ifp);
1270 TAILQ_FOREACH(ifgm, &ifgl->ifgl_group->ifg_members, ifgm_next)
1271 if (ifgm->ifgm_ifp == ifp)
1275 TAILQ_REMOVE(&ifgl->ifgl_group->ifg_members, ifgm, ifgm_next);
1279 if (--ifgl->ifgl_group->ifg_refcnt == 0) {
1280 TAILQ_REMOVE(&V_ifg_head, ifgl->ifgl_group, ifg_next);
1281 EVENTHANDLER_INVOKE(group_detach_event, ifgl->ifgl_group);
1282 free(ifgl->ifgl_group, M_TEMP);
1288 EVENTHANDLER_INVOKE(group_change_event, groupname);
1294 * Remove an interface from all groups
1297 if_delgroups(struct ifnet *ifp)
1299 struct ifg_list *ifgl;
1300 struct ifg_member *ifgm;
1301 char groupname[IFNAMSIZ];
1304 while (!TAILQ_EMPTY(&ifp->if_groups)) {
1305 ifgl = TAILQ_FIRST(&ifp->if_groups);
1307 strlcpy(groupname, ifgl->ifgl_group->ifg_group, IFNAMSIZ);
1310 TAILQ_REMOVE(&ifp->if_groups, ifgl, ifgl_next);
1311 IF_ADDR_WUNLOCK(ifp);
1313 TAILQ_FOREACH(ifgm, &ifgl->ifgl_group->ifg_members, ifgm_next)
1314 if (ifgm->ifgm_ifp == ifp)
1318 TAILQ_REMOVE(&ifgl->ifgl_group->ifg_members, ifgm,
1323 if (--ifgl->ifgl_group->ifg_refcnt == 0) {
1324 TAILQ_REMOVE(&V_ifg_head, ifgl->ifgl_group, ifg_next);
1325 EVENTHANDLER_INVOKE(group_detach_event,
1327 free(ifgl->ifgl_group, M_TEMP);
1333 EVENTHANDLER_INVOKE(group_change_event, groupname);
1341 * Stores all groups from an interface in memory pointed
1345 if_getgroup(struct ifgroupreq *data, struct ifnet *ifp)
1348 struct ifg_list *ifgl;
1349 struct ifg_req ifgrq, *ifgp;
1350 struct ifgroupreq *ifgr = data;
1352 if (ifgr->ifgr_len == 0) {
1354 TAILQ_FOREACH(ifgl, &ifp->if_groups, ifgl_next)
1355 ifgr->ifgr_len += sizeof(struct ifg_req);
1356 IF_ADDR_RUNLOCK(ifp);
1360 len = ifgr->ifgr_len;
1361 ifgp = ifgr->ifgr_groups;
1364 TAILQ_FOREACH(ifgl, &ifp->if_groups, ifgl_next) {
1365 if (len < sizeof(ifgrq)) {
1366 IF_ADDR_RUNLOCK(ifp);
1369 bzero(&ifgrq, sizeof ifgrq);
1370 strlcpy(ifgrq.ifgrq_group, ifgl->ifgl_group->ifg_group,
1371 sizeof(ifgrq.ifgrq_group));
1372 if ((error = copyout(&ifgrq, ifgp, sizeof(struct ifg_req)))) {
1373 IF_ADDR_RUNLOCK(ifp);
1376 len -= sizeof(ifgrq);
1379 IF_ADDR_RUNLOCK(ifp);
1385 * Stores all members of a group in memory pointed to by data
1388 if_getgroupmembers(struct ifgroupreq *data)
1390 struct ifgroupreq *ifgr = data;
1391 struct ifg_group *ifg;
1392 struct ifg_member *ifgm;
1393 struct ifg_req ifgrq, *ifgp;
1397 TAILQ_FOREACH(ifg, &V_ifg_head, ifg_next)
1398 if (!strcmp(ifg->ifg_group, ifgr->ifgr_name))
1405 if (ifgr->ifgr_len == 0) {
1406 TAILQ_FOREACH(ifgm, &ifg->ifg_members, ifgm_next)
1407 ifgr->ifgr_len += sizeof(ifgrq);
1412 len = ifgr->ifgr_len;
1413 ifgp = ifgr->ifgr_groups;
1414 TAILQ_FOREACH(ifgm, &ifg->ifg_members, ifgm_next) {
1415 if (len < sizeof(ifgrq)) {
1419 bzero(&ifgrq, sizeof ifgrq);
1420 strlcpy(ifgrq.ifgrq_member, ifgm->ifgm_ifp->if_xname,
1421 sizeof(ifgrq.ifgrq_member));
1422 if ((error = copyout(&ifgrq, ifgp, sizeof(struct ifg_req)))) {
1426 len -= sizeof(ifgrq);
1435 * Delete Routes for a Network Interface
1437 * Called for each routing entry via the rnh->rnh_walktree() call above
1438 * to delete all route entries referencing a detaching network interface.
1441 * rn pointer to node in the routing table
1442 * arg argument passed to rnh->rnh_walktree() - detaching interface
1446 * errno failed - reason indicated
1450 if_rtdel(struct radix_node *rn, void *arg)
1452 struct rtentry *rt = (struct rtentry *)rn;
1453 struct ifnet *ifp = arg;
1456 if (rt->rt_ifp == ifp) {
1459 * Protect (sorta) against walktree recursion problems
1460 * with cloned routes
1462 if ((rt->rt_flags & RTF_UP) == 0)
1465 err = rtrequest_fib(RTM_DELETE, rt_key(rt), rt->rt_gateway,
1467 rt->rt_flags|RTF_RNH_LOCKED|RTF_PINNED,
1468 (struct rtentry **) NULL, rt->rt_fibnum);
1470 log(LOG_WARNING, "if_rtdel: error %d\n", err);
1478 * Wrapper functions for struct ifnet address list locking macros. These are
1479 * used by kernel modules to avoid encoding programming interface or binary
1480 * interface assumptions that may be violated when kernel-internal locking
1481 * approaches change.
1484 if_addr_rlock(struct ifnet *ifp)
1491 if_addr_runlock(struct ifnet *ifp)
1494 IF_ADDR_RUNLOCK(ifp);
1498 if_maddr_rlock(struct ifnet *ifp)
1505 if_maddr_runlock(struct ifnet *ifp)
1508 IF_ADDR_RUNLOCK(ifp);
1512 * Reference count functions for ifaddrs.
1515 ifa_init(struct ifaddr *ifa)
1518 mtx_init(&ifa->ifa_mtx, "ifaddr", NULL, MTX_DEF);
1519 refcount_init(&ifa->ifa_refcnt, 1);
1523 ifa_ref(struct ifaddr *ifa)
1526 refcount_acquire(&ifa->ifa_refcnt);
1530 ifa_free(struct ifaddr *ifa)
1533 if (refcount_release(&ifa->ifa_refcnt)) {
1534 mtx_destroy(&ifa->ifa_mtx);
1535 free(ifa, M_IFADDR);
1540 ifa_add_loopback_route(struct ifaddr *ifa, struct sockaddr *ia)
1543 struct rtentry *rt = NULL;
1544 struct rt_addrinfo info;
1545 static struct sockaddr_dl null_sdl = {sizeof(null_sdl), AF_LINK};
1547 bzero(&info, sizeof(info));
1548 info.rti_ifp = V_loif;
1549 info.rti_flags = ifa->ifa_flags | RTF_HOST | RTF_STATIC;
1550 info.rti_info[RTAX_DST] = ia;
1551 info.rti_info[RTAX_GATEWAY] = (struct sockaddr *)&null_sdl;
1552 error = rtrequest1_fib(RTM_ADD, &info, &rt, ifa->ifa_ifp->if_fib);
1554 if (error == 0 && rt != NULL) {
1556 ((struct sockaddr_dl *)rt->rt_gateway)->sdl_type =
1557 ifa->ifa_ifp->if_type;
1558 ((struct sockaddr_dl *)rt->rt_gateway)->sdl_index =
1559 ifa->ifa_ifp->if_index;
1562 } else if (error != 0)
1563 log(LOG_INFO, "ifa_add_loopback_route: insertion failed\n");
1569 ifa_del_loopback_route(struct ifaddr *ifa, struct sockaddr *ia)
1572 struct rt_addrinfo info;
1573 struct sockaddr_dl null_sdl;
1575 bzero(&null_sdl, sizeof(null_sdl));
1576 null_sdl.sdl_len = sizeof(null_sdl);
1577 null_sdl.sdl_family = AF_LINK;
1578 null_sdl.sdl_type = ifa->ifa_ifp->if_type;
1579 null_sdl.sdl_index = ifa->ifa_ifp->if_index;
1580 bzero(&info, sizeof(info));
1581 info.rti_flags = ifa->ifa_flags | RTF_HOST | RTF_STATIC;
1582 info.rti_info[RTAX_DST] = ia;
1583 info.rti_info[RTAX_GATEWAY] = (struct sockaddr *)&null_sdl;
1584 error = rtrequest1_fib(RTM_DELETE, &info, NULL, ifa->ifa_ifp->if_fib);
1587 log(LOG_INFO, "ifa_del_loopback_route: deletion failed\n");
1593 * XXX: Because sockaddr_dl has deeper structure than the sockaddr
1594 * structs used to represent other address families, it is necessary
1595 * to perform a different comparison.
1598 #define sa_equal(a1, a2) \
1599 (bcmp((a1), (a2), ((a1))->sa_len) == 0)
1601 #define sa_dl_equal(a1, a2) \
1602 ((((struct sockaddr_dl *)(a1))->sdl_len == \
1603 ((struct sockaddr_dl *)(a2))->sdl_len) && \
1604 (bcmp(LLADDR((struct sockaddr_dl *)(a1)), \
1605 LLADDR((struct sockaddr_dl *)(a2)), \
1606 ((struct sockaddr_dl *)(a1))->sdl_alen) == 0))
1609 * Locate an interface based on a complete address.
1612 static struct ifaddr *
1613 ifa_ifwithaddr_internal(struct sockaddr *addr, int getref)
1618 IFNET_RLOCK_NOSLEEP();
1619 TAILQ_FOREACH(ifp, &V_ifnet, if_link) {
1621 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
1622 if (ifa->ifa_addr->sa_family != addr->sa_family)
1624 if (sa_equal(addr, ifa->ifa_addr)) {
1627 IF_ADDR_RUNLOCK(ifp);
1630 /* IP6 doesn't have broadcast */
1631 if ((ifp->if_flags & IFF_BROADCAST) &&
1632 ifa->ifa_broadaddr &&
1633 ifa->ifa_broadaddr->sa_len != 0 &&
1634 sa_equal(ifa->ifa_broadaddr, addr)) {
1637 IF_ADDR_RUNLOCK(ifp);
1641 IF_ADDR_RUNLOCK(ifp);
1645 IFNET_RUNLOCK_NOSLEEP();
1650 ifa_ifwithaddr(struct sockaddr *addr)
1653 return (ifa_ifwithaddr_internal(addr, 1));
1657 ifa_ifwithaddr_check(struct sockaddr *addr)
1660 return (ifa_ifwithaddr_internal(addr, 0) != NULL);
1664 * Locate an interface based on the broadcast address.
1668 ifa_ifwithbroadaddr(struct sockaddr *addr)
1673 IFNET_RLOCK_NOSLEEP();
1674 TAILQ_FOREACH(ifp, &V_ifnet, if_link) {
1676 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
1677 if (ifa->ifa_addr->sa_family != addr->sa_family)
1679 if ((ifp->if_flags & IFF_BROADCAST) &&
1680 ifa->ifa_broadaddr &&
1681 ifa->ifa_broadaddr->sa_len != 0 &&
1682 sa_equal(ifa->ifa_broadaddr, addr)) {
1684 IF_ADDR_RUNLOCK(ifp);
1688 IF_ADDR_RUNLOCK(ifp);
1692 IFNET_RUNLOCK_NOSLEEP();
1697 * Locate the point to point interface with a given destination address.
1701 ifa_ifwithdstaddr_fib(struct sockaddr *addr, int fibnum)
1706 IFNET_RLOCK_NOSLEEP();
1707 TAILQ_FOREACH(ifp, &V_ifnet, if_link) {
1708 if ((ifp->if_flags & IFF_POINTOPOINT) == 0)
1710 if ((fibnum != RT_ALL_FIBS) && (ifp->if_fib != fibnum))
1713 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
1714 if (ifa->ifa_addr->sa_family != addr->sa_family)
1716 if (ifa->ifa_dstaddr != NULL &&
1717 sa_equal(addr, ifa->ifa_dstaddr)) {
1719 IF_ADDR_RUNLOCK(ifp);
1723 IF_ADDR_RUNLOCK(ifp);
1727 IFNET_RUNLOCK_NOSLEEP();
1732 ifa_ifwithdstaddr(struct sockaddr *addr)
1735 return (ifa_ifwithdstaddr_fib(addr, RT_ALL_FIBS));
1739 * Find an interface on a specific network. If many, choice
1740 * is most specific found.
1743 ifa_ifwithnet_fib(struct sockaddr *addr, int ignore_ptp, int fibnum)
1747 struct ifaddr *ifa_maybe = NULL;
1748 u_int af = addr->sa_family;
1749 char *addr_data = addr->sa_data, *cplim;
1752 * AF_LINK addresses can be looked up directly by their index number,
1753 * so do that if we can.
1755 if (af == AF_LINK) {
1756 struct sockaddr_dl *sdl = (struct sockaddr_dl *)addr;
1757 if (sdl->sdl_index && sdl->sdl_index <= V_if_index)
1758 return (ifaddr_byindex(sdl->sdl_index));
1762 * Scan though each interface, looking for ones that have addresses
1763 * in this address family and the requested fib. Maintain a reference
1764 * on ifa_maybe once we find one, as we release the IF_ADDR_RLOCK() that
1765 * kept it stable when we move onto the next interface.
1767 IFNET_RLOCK_NOSLEEP();
1768 TAILQ_FOREACH(ifp, &V_ifnet, if_link) {
1769 if ((fibnum != RT_ALL_FIBS) && (ifp->if_fib != fibnum))
1772 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
1773 char *cp, *cp2, *cp3;
1775 if (ifa->ifa_addr->sa_family != af)
1777 if (af == AF_INET &&
1778 ifp->if_flags & IFF_POINTOPOINT && !ignore_ptp) {
1780 * This is a bit broken as it doesn't
1781 * take into account that the remote end may
1782 * be a single node in the network we are
1784 * The trouble is that we don't know the
1785 * netmask for the remote end.
1787 if (ifa->ifa_dstaddr != NULL &&
1788 sa_equal(addr, ifa->ifa_dstaddr)) {
1790 IF_ADDR_RUNLOCK(ifp);
1795 * if we have a special address handler,
1796 * then use it instead of the generic one.
1798 if (ifa->ifa_claim_addr) {
1799 if ((*ifa->ifa_claim_addr)(ifa, addr)) {
1801 IF_ADDR_RUNLOCK(ifp);
1808 * Scan all the bits in the ifa's address.
1809 * If a bit dissagrees with what we are
1810 * looking for, mask it with the netmask
1811 * to see if it really matters.
1812 * (A byte at a time)
1814 if (ifa->ifa_netmask == 0)
1817 cp2 = ifa->ifa_addr->sa_data;
1818 cp3 = ifa->ifa_netmask->sa_data;
1819 cplim = ifa->ifa_netmask->sa_len
1820 + (char *)ifa->ifa_netmask;
1822 if ((*cp++ ^ *cp2++) & *cp3++)
1823 goto next; /* next address! */
1825 * If the netmask of what we just found
1826 * is more specific than what we had before
1827 * (if we had one) then remember the new one
1828 * before continuing to search
1829 * for an even better one.
1831 if (ifa_maybe == NULL ||
1832 rn_refines((caddr_t)ifa->ifa_netmask,
1833 (caddr_t)ifa_maybe->ifa_netmask)) {
1834 if (ifa_maybe != NULL)
1835 ifa_free(ifa_maybe);
1841 IF_ADDR_RUNLOCK(ifp);
1846 IFNET_RUNLOCK_NOSLEEP();
1847 if (ifa_maybe != NULL)
1848 ifa_free(ifa_maybe);
1853 ifa_ifwithnet(struct sockaddr *addr, int ignore_ptp)
1856 return (ifa_ifwithnet_fib(addr, ignore_ptp, RT_ALL_FIBS));
1860 * Find an interface address specific to an interface best matching
1864 ifaof_ifpforaddr(struct sockaddr *addr, struct ifnet *ifp)
1867 char *cp, *cp2, *cp3;
1869 struct ifaddr *ifa_maybe = NULL;
1870 u_int af = addr->sa_family;
1875 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
1876 if (ifa->ifa_addr->sa_family != af)
1878 if (ifa_maybe == NULL)
1880 if (ifa->ifa_netmask == 0) {
1881 if (sa_equal(addr, ifa->ifa_addr) ||
1882 (ifa->ifa_dstaddr &&
1883 sa_equal(addr, ifa->ifa_dstaddr)))
1887 if (ifp->if_flags & IFF_POINTOPOINT) {
1888 if (sa_equal(addr, ifa->ifa_dstaddr))
1892 cp2 = ifa->ifa_addr->sa_data;
1893 cp3 = ifa->ifa_netmask->sa_data;
1894 cplim = ifa->ifa_netmask->sa_len + (char *)ifa->ifa_netmask;
1895 for (; cp3 < cplim; cp3++)
1896 if ((*cp++ ^ *cp2++) & *cp3)
1906 IF_ADDR_RUNLOCK(ifp);
1910 #include <net/if_llatbl.h>
1913 * Default action when installing a route with a Link Level gateway.
1914 * Lookup an appropriate real ifa to point to.
1915 * This should be moved to /sys/net/link.c eventually.
1918 link_rtrequest(int cmd, struct rtentry *rt, struct rt_addrinfo *info)
1920 struct ifaddr *ifa, *oifa;
1921 struct sockaddr *dst;
1926 if (cmd != RTM_ADD || ((ifa = rt->rt_ifa) == 0) ||
1927 ((ifp = ifa->ifa_ifp) == 0) || ((dst = rt_key(rt)) == 0))
1929 ifa = ifaof_ifpforaddr(dst, ifp);
1934 if (ifa->ifa_rtrequest && ifa->ifa_rtrequest != link_rtrequest)
1935 ifa->ifa_rtrequest(cmd, rt, info);
1940 * Mark an interface down and notify protocols of
1942 * NOTE: must be called at splnet or eqivalent.
1945 if_unroute(struct ifnet *ifp, int flag, int fam)
1949 KASSERT(flag == IFF_UP, ("if_unroute: flag != IFF_UP"));
1951 ifp->if_flags &= ~flag;
1952 getmicrotime(&ifp->if_lastchange);
1953 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link)
1954 if (fam == PF_UNSPEC || (fam == ifa->ifa_addr->sa_family))
1955 pfctlinput(PRC_IFDOWN, ifa->ifa_addr);
1956 ifp->if_qflush(ifp);
1959 (*carp_linkstate_p)(ifp);
1964 * Mark an interface up and notify protocols of
1966 * NOTE: must be called at splnet or eqivalent.
1969 if_route(struct ifnet *ifp, int flag, int fam)
1973 KASSERT(flag == IFF_UP, ("if_route: flag != IFF_UP"));
1975 ifp->if_flags |= flag;
1976 getmicrotime(&ifp->if_lastchange);
1977 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link)
1978 if (fam == PF_UNSPEC || (fam == ifa->ifa_addr->sa_family))
1979 pfctlinput(PRC_IFUP, ifa->ifa_addr);
1981 (*carp_linkstate_p)(ifp);
1988 void (*vlan_link_state_p)(struct ifnet *); /* XXX: private from if_vlan */
1989 void (*vlan_trunk_cap_p)(struct ifnet *); /* XXX: private from if_vlan */
1990 struct ifnet *(*vlan_trunkdev_p)(struct ifnet *);
1991 struct ifnet *(*vlan_devat_p)(struct ifnet *, uint16_t);
1992 int (*vlan_tag_p)(struct ifnet *, uint16_t *);
1993 int (*vlan_setcookie_p)(struct ifnet *, void *);
1994 void *(*vlan_cookie_p)(struct ifnet *);
1997 * Handle a change in the interface link state. To avoid LORs
1998 * between driver lock and upper layer locks, as well as possible
1999 * recursions, we post event to taskqueue, and all job
2000 * is done in static do_link_state_change().
2003 if_link_state_change(struct ifnet *ifp, int link_state)
2005 /* Return if state hasn't changed. */
2006 if (ifp->if_link_state == link_state)
2009 ifp->if_link_state = link_state;
2011 taskqueue_enqueue(taskqueue_swi, &ifp->if_linktask);
2015 do_link_state_change(void *arg, int pending)
2017 struct ifnet *ifp = (struct ifnet *)arg;
2018 int link_state = ifp->if_link_state;
2019 CURVNET_SET(ifp->if_vnet);
2021 /* Notify that the link state has changed. */
2023 if (ifp->if_vlantrunk != NULL)
2024 (*vlan_link_state_p)(ifp);
2026 if ((ifp->if_type == IFT_ETHER || ifp->if_type == IFT_L2VLAN) &&
2027 IFP2AC(ifp)->ac_netgraph != NULL)
2028 (*ng_ether_link_state_p)(ifp, link_state);
2030 (*carp_linkstate_p)(ifp);
2032 (*bridge_linkstate_p)(ifp);
2034 (*lagg_linkstate_p)(ifp, link_state);
2036 if (IS_DEFAULT_VNET(curvnet))
2037 devctl_notify("IFNET", ifp->if_xname,
2038 (link_state == LINK_STATE_UP) ? "LINK_UP" : "LINK_DOWN",
2041 if_printf(ifp, "%d link states coalesced\n", pending);
2042 if (log_link_state_change)
2043 log(LOG_NOTICE, "%s: link state changed to %s\n", ifp->if_xname,
2044 (link_state == LINK_STATE_UP) ? "UP" : "DOWN" );
2045 EVENTHANDLER_INVOKE(ifnet_link_event, ifp, ifp->if_link_state);
2050 * Mark an interface down and notify protocols of
2052 * NOTE: must be called at splnet or eqivalent.
2055 if_down(struct ifnet *ifp)
2058 if_unroute(ifp, IFF_UP, AF_UNSPEC);
2062 * Mark an interface up and notify protocols of
2064 * NOTE: must be called at splnet or eqivalent.
2067 if_up(struct ifnet *ifp)
2070 if_route(ifp, IFF_UP, AF_UNSPEC);
2074 * Flush an interface queue.
2077 if_qflush(struct ifnet *ifp)
2085 if (ALTQ_IS_ENABLED(ifq))
2089 while ((m = n) != 0) {
2100 * Map interface name to interface structure pointer, with or without
2101 * returning a reference.
2104 ifunit_ref(const char *name)
2108 IFNET_RLOCK_NOSLEEP();
2109 TAILQ_FOREACH(ifp, &V_ifnet, if_link) {
2110 if (strncmp(name, ifp->if_xname, IFNAMSIZ) == 0 &&
2111 !(ifp->if_flags & IFF_DYING))
2116 IFNET_RUNLOCK_NOSLEEP();
2121 ifunit(const char *name)
2125 IFNET_RLOCK_NOSLEEP();
2126 TAILQ_FOREACH(ifp, &V_ifnet, if_link) {
2127 if (strncmp(name, ifp->if_xname, IFNAMSIZ) == 0)
2130 IFNET_RUNLOCK_NOSLEEP();
2135 * Hardware specific interface ioctls.
2138 ifhwioctl(u_long cmd, struct ifnet *ifp, caddr_t data, struct thread *td)
2143 int new_flags, temp_flags;
2144 size_t namelen, onamelen;
2146 char *descrbuf, *odescrbuf;
2147 char new_name[IFNAMSIZ];
2149 struct sockaddr_dl *sdl;
2151 ifr = (struct ifreq *)data;
2154 ifr->ifr_index = ifp->if_index;
2158 temp_flags = ifp->if_flags | ifp->if_drv_flags;
2159 ifr->ifr_flags = temp_flags & 0xffff;
2160 ifr->ifr_flagshigh = temp_flags >> 16;
2164 ifr->ifr_reqcap = ifp->if_capabilities;
2165 ifr->ifr_curcap = ifp->if_capenable;
2170 error = mac_ifnet_ioctl_get(td->td_ucred, ifr, ifp);
2175 ifr->ifr_metric = ifp->if_metric;
2179 ifr->ifr_mtu = ifp->if_mtu;
2183 ifr->ifr_phys = ifp->if_physical;
2188 sx_slock(&ifdescr_sx);
2189 if (ifp->if_description == NULL)
2192 /* space for terminating nul */
2193 descrlen = strlen(ifp->if_description) + 1;
2194 if (ifr->ifr_buffer.length < descrlen)
2195 ifr->ifr_buffer.buffer = NULL;
2197 error = copyout(ifp->if_description,
2198 ifr->ifr_buffer.buffer, descrlen);
2199 ifr->ifr_buffer.length = descrlen;
2201 sx_sunlock(&ifdescr_sx);
2205 error = priv_check(td, PRIV_NET_SETIFDESCR);
2210 * Copy only (length-1) bytes to make sure that
2211 * if_description is always nul terminated. The
2212 * length parameter is supposed to count the
2213 * terminating nul in.
2215 if (ifr->ifr_buffer.length > ifdescr_maxlen)
2216 return (ENAMETOOLONG);
2217 else if (ifr->ifr_buffer.length == 0)
2220 descrbuf = malloc(ifr->ifr_buffer.length, M_IFDESCR,
2222 error = copyin(ifr->ifr_buffer.buffer, descrbuf,
2223 ifr->ifr_buffer.length - 1);
2225 free(descrbuf, M_IFDESCR);
2230 sx_xlock(&ifdescr_sx);
2231 odescrbuf = ifp->if_description;
2232 ifp->if_description = descrbuf;
2233 sx_xunlock(&ifdescr_sx);
2235 getmicrotime(&ifp->if_lastchange);
2236 free(odescrbuf, M_IFDESCR);
2240 ifr->ifr_fib = ifp->if_fib;
2244 error = priv_check(td, PRIV_NET_SETIFFIB);
2247 if (ifr->ifr_fib >= rt_numfibs)
2250 ifp->if_fib = ifr->ifr_fib;
2254 error = priv_check(td, PRIV_NET_SETIFFLAGS);
2258 * Currently, no driver owned flags pass the IFF_CANTCHANGE
2259 * check, so we don't need special handling here yet.
2261 new_flags = (ifr->ifr_flags & 0xffff) |
2262 (ifr->ifr_flagshigh << 16);
2263 if (ifp->if_flags & IFF_SMART) {
2264 /* Smart drivers twiddle their own routes */
2265 } else if (ifp->if_flags & IFF_UP &&
2266 (new_flags & IFF_UP) == 0) {
2270 } else if (new_flags & IFF_UP &&
2271 (ifp->if_flags & IFF_UP) == 0) {
2276 /* See if permanently promiscuous mode bit is about to flip */
2277 if ((ifp->if_flags ^ new_flags) & IFF_PPROMISC) {
2278 if (new_flags & IFF_PPROMISC)
2279 ifp->if_flags |= IFF_PROMISC;
2280 else if (ifp->if_pcount == 0)
2281 ifp->if_flags &= ~IFF_PROMISC;
2282 log(LOG_INFO, "%s: permanently promiscuous mode %s\n",
2284 (new_flags & IFF_PPROMISC) ? "enabled" : "disabled");
2286 ifp->if_flags = (ifp->if_flags & IFF_CANTCHANGE) |
2287 (new_flags &~ IFF_CANTCHANGE);
2288 if (ifp->if_ioctl) {
2289 (void) (*ifp->if_ioctl)(ifp, cmd, data);
2291 getmicrotime(&ifp->if_lastchange);
2295 error = priv_check(td, PRIV_NET_SETIFCAP);
2298 if (ifp->if_ioctl == NULL)
2299 return (EOPNOTSUPP);
2300 if (ifr->ifr_reqcap & ~ifp->if_capabilities)
2302 error = (*ifp->if_ioctl)(ifp, cmd, data);
2304 getmicrotime(&ifp->if_lastchange);
2309 error = mac_ifnet_ioctl_set(td->td_ucred, ifr, ifp);
2314 error = priv_check(td, PRIV_NET_SETIFNAME);
2317 error = copyinstr(ifr->ifr_data, new_name, IFNAMSIZ, NULL);
2320 if (new_name[0] == '\0')
2322 if (ifunit(new_name) != NULL)
2326 * XXX: Locking. Nothing else seems to lock if_flags,
2327 * and there are numerous other races with the
2328 * ifunit() checks not being atomic with namespace
2329 * changes (renames, vmoves, if_attach, etc).
2331 ifp->if_flags |= IFF_RENAMING;
2333 /* Announce the departure of the interface. */
2334 rt_ifannouncemsg(ifp, IFAN_DEPARTURE);
2335 EVENTHANDLER_INVOKE(ifnet_departure_event, ifp);
2337 log(LOG_INFO, "%s: changing name to '%s'\n",
2338 ifp->if_xname, new_name);
2340 strlcpy(ifp->if_xname, new_name, sizeof(ifp->if_xname));
2343 sdl = (struct sockaddr_dl *)ifa->ifa_addr;
2344 namelen = strlen(new_name);
2345 onamelen = sdl->sdl_nlen;
2347 * Move the address if needed. This is safe because we
2348 * allocate space for a name of length IFNAMSIZ when we
2349 * create this in if_attach().
2351 if (namelen != onamelen) {
2352 bcopy(sdl->sdl_data + onamelen,
2353 sdl->sdl_data + namelen, sdl->sdl_alen);
2355 bcopy(new_name, sdl->sdl_data, namelen);
2356 sdl->sdl_nlen = namelen;
2357 sdl = (struct sockaddr_dl *)ifa->ifa_netmask;
2358 bzero(sdl->sdl_data, onamelen);
2359 while (namelen != 0)
2360 sdl->sdl_data[--namelen] = 0xff;
2363 EVENTHANDLER_INVOKE(ifnet_arrival_event, ifp);
2364 /* Announce the return of the interface. */
2365 rt_ifannouncemsg(ifp, IFAN_ARRIVAL);
2367 ifp->if_flags &= ~IFF_RENAMING;
2372 error = priv_check(td, PRIV_NET_SETIFVNET);
2375 error = if_vmove_loan(td, ifp, ifr->ifr_name, ifr->ifr_jid);
2380 error = priv_check(td, PRIV_NET_SETIFMETRIC);
2383 ifp->if_metric = ifr->ifr_metric;
2384 getmicrotime(&ifp->if_lastchange);
2388 error = priv_check(td, PRIV_NET_SETIFPHYS);
2391 if (ifp->if_ioctl == NULL)
2392 return (EOPNOTSUPP);
2393 error = (*ifp->if_ioctl)(ifp, cmd, data);
2395 getmicrotime(&ifp->if_lastchange);
2400 u_long oldmtu = ifp->if_mtu;
2402 error = priv_check(td, PRIV_NET_SETIFMTU);
2405 if (ifr->ifr_mtu < IF_MINMTU || ifr->ifr_mtu > IF_MAXMTU)
2407 if (ifp->if_ioctl == NULL)
2408 return (EOPNOTSUPP);
2409 error = (*ifp->if_ioctl)(ifp, cmd, data);
2411 getmicrotime(&ifp->if_lastchange);
2415 * If the link MTU changed, do network layer specific procedure.
2417 if (ifp->if_mtu != oldmtu) {
2427 if (cmd == SIOCADDMULTI)
2428 error = priv_check(td, PRIV_NET_ADDMULTI);
2430 error = priv_check(td, PRIV_NET_DELMULTI);
2434 /* Don't allow group membership on non-multicast interfaces. */
2435 if ((ifp->if_flags & IFF_MULTICAST) == 0)
2436 return (EOPNOTSUPP);
2438 /* Don't let users screw up protocols' entries. */
2439 if (ifr->ifr_addr.sa_family != AF_LINK)
2442 if (cmd == SIOCADDMULTI) {
2443 struct ifmultiaddr *ifma;
2446 * Userland is only permitted to join groups once
2447 * via the if_addmulti() KPI, because it cannot hold
2448 * struct ifmultiaddr * between calls. It may also
2449 * lose a race while we check if the membership
2453 ifma = if_findmulti(ifp, &ifr->ifr_addr);
2454 IF_ADDR_RUNLOCK(ifp);
2458 error = if_addmulti(ifp, &ifr->ifr_addr, &ifma);
2460 error = if_delmulti(ifp, &ifr->ifr_addr);
2463 getmicrotime(&ifp->if_lastchange);
2466 case SIOCSIFPHYADDR:
2467 case SIOCDIFPHYADDR:
2469 case SIOCSIFPHYADDR_IN6:
2471 case SIOCSLIFPHYADDR:
2473 case SIOCSIFGENERIC:
2474 error = priv_check(td, PRIV_NET_HWIOCTL);
2477 if (ifp->if_ioctl == NULL)
2478 return (EOPNOTSUPP);
2479 error = (*ifp->if_ioctl)(ifp, cmd, data);
2481 getmicrotime(&ifp->if_lastchange);
2485 ifs = (struct ifstat *)data;
2486 ifs->ascii[0] = '\0';
2488 case SIOCGIFPSRCADDR:
2489 case SIOCGIFPDSTADDR:
2490 case SIOCGLIFPHYADDR:
2492 case SIOCGIFGENERIC:
2493 if (ifp->if_ioctl == NULL)
2494 return (EOPNOTSUPP);
2495 error = (*ifp->if_ioctl)(ifp, cmd, data);
2499 error = priv_check(td, PRIV_NET_SETLLADDR);
2502 error = if_setlladdr(ifp,
2503 ifr->ifr_addr.sa_data, ifr->ifr_addr.sa_len);
2504 EVENTHANDLER_INVOKE(iflladdr_event, ifp);
2509 struct ifgroupreq *ifgr = (struct ifgroupreq *)ifr;
2511 error = priv_check(td, PRIV_NET_ADDIFGROUP);
2514 if ((error = if_addgroup(ifp, ifgr->ifgr_group)))
2520 if ((error = if_getgroup((struct ifgroupreq *)ifr, ifp)))
2526 struct ifgroupreq *ifgr = (struct ifgroupreq *)ifr;
2528 error = priv_check(td, PRIV_NET_DELIFGROUP);
2531 if ((error = if_delgroup(ifp, ifgr->ifgr_group)))
2543 #ifdef COMPAT_FREEBSD32
2551 #define SIOCGIFCONF32 _IOWR('i', 36, struct ifconf32)
2558 ifioctl(struct socket *so, u_long cmd, caddr_t data, struct thread *td)
2565 CURVNET_SET(so->so_vnet);
2569 error = ifconf(cmd, data);
2573 #ifdef COMPAT_FREEBSD32
2576 struct ifconf32 *ifc32;
2579 ifc32 = (struct ifconf32 *)data;
2580 ifc.ifc_len = ifc32->ifc_len;
2581 ifc.ifc_buf = PTRIN(ifc32->ifc_buf);
2583 error = ifconf(SIOCGIFCONF, (void *)&ifc);
2586 ifc32->ifc_len = ifc.ifc_len;
2591 ifr = (struct ifreq *)data;
2596 error = priv_check(td, PRIV_NET_SETIFVNET);
2598 error = if_vmove_reclaim(td, ifr->ifr_name,
2605 error = priv_check(td, PRIV_NET_IFCREATE);
2607 error = if_clone_create(ifr->ifr_name,
2608 sizeof(ifr->ifr_name),
2609 cmd == SIOCIFCREATE2 ? ifr->ifr_data : NULL);
2613 error = priv_check(td, PRIV_NET_IFDESTROY);
2615 error = if_clone_destroy(ifr->ifr_name);
2619 case SIOCIFGCLONERS:
2620 error = if_clone_list((struct if_clonereq *)data);
2624 error = if_getgroupmembers((struct ifgroupreq *)data);
2629 ifp = ifunit_ref(ifr->ifr_name);
2635 error = ifhwioctl(cmd, ifp, data, td);
2636 if (error != ENOIOCTL) {
2642 oif_flags = ifp->if_flags;
2643 if (so->so_proto == NULL) {
2646 return (EOPNOTSUPP);
2650 * Pass the request on to the socket control method, and if the
2651 * latter returns EOPNOTSUPP, directly to the interface.
2653 * Make an exception for the legacy SIOCSIF* requests. Drivers
2654 * trust SIOCSIFADDR et al to come from an already privileged
2655 * layer, and do not perform any credentials checks or input
2659 error = ((*so->so_proto->pr_usrreqs->pru_control)(so, cmd,
2662 if (error == EOPNOTSUPP && ifp != NULL && ifp->if_ioctl != NULL &&
2663 cmd != SIOCSIFADDR && cmd != SIOCSIFBRDADDR &&
2664 cmd != SIOCSIFDSTADDR && cmd != SIOCSIFNETMASK)
2665 error = (*ifp->if_ioctl)(ifp, cmd, data);
2672 case SIOCSIFDSTADDR:
2674 case SIOCSIFBRDADDR:
2675 case SIOCSIFNETMASK:
2676 #if BYTE_ORDER != BIG_ENDIAN
2677 if (ifr->ifr_addr.sa_family == 0 &&
2678 ifr->ifr_addr.sa_len < 16) {
2679 ifr->ifr_addr.sa_family = ifr->ifr_addr.sa_len;
2680 ifr->ifr_addr.sa_len = 16;
2683 if (ifr->ifr_addr.sa_len == 0)
2684 ifr->ifr_addr.sa_len = 16;
2692 case OSIOCGIFDSTADDR:
2693 cmd = SIOCGIFDSTADDR;
2696 case OSIOCGIFBRDADDR:
2697 cmd = SIOCGIFBRDADDR;
2700 case OSIOCGIFNETMASK:
2701 cmd = SIOCGIFNETMASK;
2703 error = ((*so->so_proto->pr_usrreqs->pru_control)(so,
2707 if (error == EOPNOTSUPP && ifp != NULL &&
2708 ifp->if_ioctl != NULL &&
2709 cmd != SIOCSIFADDR && cmd != SIOCSIFBRDADDR &&
2710 cmd != SIOCSIFDSTADDR && cmd != SIOCSIFNETMASK)
2711 error = (*ifp->if_ioctl)(ifp, cmd, data);
2715 case OSIOCGIFDSTADDR:
2716 case OSIOCGIFBRDADDR:
2717 case OSIOCGIFNETMASK:
2718 *(u_short *)&ifr->ifr_addr = ifr->ifr_addr.sa_family;
2722 #endif /* COMPAT_43 */
2724 if ((oif_flags ^ ifp->if_flags) & IFF_UP) {
2726 if (ifp->if_flags & IFF_UP) {
2739 * The code common to handling reference counted flags,
2740 * e.g., in ifpromisc() and if_allmulti().
2741 * The "pflag" argument can specify a permanent mode flag to check,
2742 * such as IFF_PPROMISC for promiscuous mode; should be 0 if none.
2744 * Only to be used on stack-owned flags, not driver-owned flags.
2747 if_setflag(struct ifnet *ifp, int flag, int pflag, int *refcount, int onswitch)
2751 int oldflags, oldcount;
2753 /* Sanity checks to catch programming errors */
2754 KASSERT((flag & (IFF_DRV_OACTIVE|IFF_DRV_RUNNING)) == 0,
2755 ("%s: setting driver-owned flag %d", __func__, flag));
2758 KASSERT(*refcount >= 0,
2759 ("%s: increment negative refcount %d for flag %d",
2760 __func__, *refcount, flag));
2762 KASSERT(*refcount > 0,
2763 ("%s: decrement non-positive refcount %d for flag %d",
2764 __func__, *refcount, flag));
2766 /* In case this mode is permanent, just touch refcount */
2767 if (ifp->if_flags & pflag) {
2768 *refcount += onswitch ? 1 : -1;
2772 /* Save ifnet parameters for if_ioctl() may fail */
2773 oldcount = *refcount;
2774 oldflags = ifp->if_flags;
2777 * See if we aren't the only and touching refcount is enough.
2778 * Actually toggle interface flag if we are the first or last.
2783 ifp->if_flags |= flag;
2787 ifp->if_flags &= ~flag;
2790 /* Call down the driver since we've changed interface flags */
2791 if (ifp->if_ioctl == NULL) {
2795 ifr.ifr_flags = ifp->if_flags & 0xffff;
2796 ifr.ifr_flagshigh = ifp->if_flags >> 16;
2797 error = (*ifp->if_ioctl)(ifp, SIOCSIFFLAGS, (caddr_t)&ifr);
2800 /* Notify userland that interface flags have changed */
2805 /* Recover after driver error */
2806 *refcount = oldcount;
2807 ifp->if_flags = oldflags;
2812 * Set/clear promiscuous mode on interface ifp based on the truth value
2813 * of pswitch. The calls are reference counted so that only the first
2814 * "on" request actually has an effect, as does the final "off" request.
2815 * Results are undefined if the "off" and "on" requests are not matched.
2818 ifpromisc(struct ifnet *ifp, int pswitch)
2821 int oldflags = ifp->if_flags;
2823 error = if_setflag(ifp, IFF_PROMISC, IFF_PPROMISC,
2824 &ifp->if_pcount, pswitch);
2825 /* If promiscuous mode status has changed, log a message */
2826 if (error == 0 && ((ifp->if_flags ^ oldflags) & IFF_PROMISC))
2827 log(LOG_INFO, "%s: promiscuous mode %s\n",
2829 (ifp->if_flags & IFF_PROMISC) ? "enabled" : "disabled");
2834 * Return interface configuration
2835 * of system. List may be used
2836 * in later ioctl's (above) to get
2837 * other information.
2841 ifconf(u_long cmd, caddr_t data)
2843 struct ifconf *ifc = (struct ifconf *)data;
2848 int error, full = 0, valid_len, max_len;
2850 /* Limit initial buffer size to MAXPHYS to avoid DoS from userspace. */
2851 max_len = MAXPHYS - 1;
2853 /* Prevent hostile input from being able to crash the system */
2854 if (ifc->ifc_len <= 0)
2858 if (ifc->ifc_len <= max_len) {
2859 max_len = ifc->ifc_len;
2862 sb = sbuf_new(NULL, NULL, max_len + 1, SBUF_FIXEDLEN);
2867 TAILQ_FOREACH(ifp, &V_ifnet, if_link) {
2871 * Zero the ifr_name buffer to make sure we don't
2872 * disclose the contents of the stack.
2874 memset(ifr.ifr_name, 0, sizeof(ifr.ifr_name));
2876 if (strlcpy(ifr.ifr_name, ifp->if_xname, sizeof(ifr.ifr_name))
2877 >= sizeof(ifr.ifr_name)) {
2880 return (ENAMETOOLONG);
2885 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
2886 struct sockaddr *sa = ifa->ifa_addr;
2888 if (prison_if(curthread->td_ucred, sa) != 0)
2892 if (cmd == OSIOCGIFCONF) {
2893 struct osockaddr *osa =
2894 (struct osockaddr *)&ifr.ifr_addr;
2896 osa->sa_family = sa->sa_family;
2897 sbuf_bcat(sb, &ifr, sizeof(ifr));
2898 max_len += sizeof(ifr);
2901 if (sa->sa_len <= sizeof(*sa)) {
2903 sbuf_bcat(sb, &ifr, sizeof(ifr));
2904 max_len += sizeof(ifr);
2907 offsetof(struct ifreq, ifr_addr));
2908 max_len += offsetof(struct ifreq, ifr_addr);
2909 sbuf_bcat(sb, sa, sa->sa_len);
2910 max_len += sa->sa_len;
2913 if (sbuf_error(sb) == 0)
2914 valid_len = sbuf_len(sb);
2916 IF_ADDR_RUNLOCK(ifp);
2918 bzero((caddr_t)&ifr.ifr_addr, sizeof(ifr.ifr_addr));
2919 sbuf_bcat(sb, &ifr, sizeof(ifr));
2920 max_len += sizeof(ifr);
2922 if (sbuf_error(sb) == 0)
2923 valid_len = sbuf_len(sb);
2929 * If we didn't allocate enough space (uncommon), try again. If
2930 * we have already allocated as much space as we are allowed,
2931 * return what we've got.
2933 if (valid_len != max_len && !full) {
2938 ifc->ifc_len = valid_len;
2940 error = copyout(sbuf_data(sb), ifc->ifc_req, ifc->ifc_len);
2946 * Just like ifpromisc(), but for all-multicast-reception mode.
2949 if_allmulti(struct ifnet *ifp, int onswitch)
2952 return (if_setflag(ifp, IFF_ALLMULTI, 0, &ifp->if_amcount, onswitch));
2955 struct ifmultiaddr *
2956 if_findmulti(struct ifnet *ifp, struct sockaddr *sa)
2958 struct ifmultiaddr *ifma;
2960 IF_ADDR_LOCK_ASSERT(ifp);
2962 TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
2963 if (sa->sa_family == AF_LINK) {
2964 if (sa_dl_equal(ifma->ifma_addr, sa))
2967 if (sa_equal(ifma->ifma_addr, sa))
2976 * Allocate a new ifmultiaddr and initialize based on passed arguments. We
2977 * make copies of passed sockaddrs. The ifmultiaddr will not be added to
2978 * the ifnet multicast address list here, so the caller must do that and
2979 * other setup work (such as notifying the device driver). The reference
2980 * count is initialized to 1.
2982 static struct ifmultiaddr *
2983 if_allocmulti(struct ifnet *ifp, struct sockaddr *sa, struct sockaddr *llsa,
2986 struct ifmultiaddr *ifma;
2987 struct sockaddr *dupsa;
2989 ifma = malloc(sizeof *ifma, M_IFMADDR, mflags |
2994 dupsa = malloc(sa->sa_len, M_IFMADDR, mflags);
2995 if (dupsa == NULL) {
2996 free(ifma, M_IFMADDR);
2999 bcopy(sa, dupsa, sa->sa_len);
3000 ifma->ifma_addr = dupsa;
3002 ifma->ifma_ifp = ifp;
3003 ifma->ifma_refcount = 1;
3004 ifma->ifma_protospec = NULL;
3007 ifma->ifma_lladdr = NULL;
3011 dupsa = malloc(llsa->sa_len, M_IFMADDR, mflags);
3012 if (dupsa == NULL) {
3013 free(ifma->ifma_addr, M_IFMADDR);
3014 free(ifma, M_IFMADDR);
3017 bcopy(llsa, dupsa, llsa->sa_len);
3018 ifma->ifma_lladdr = dupsa;
3024 * if_freemulti: free ifmultiaddr structure and possibly attached related
3025 * addresses. The caller is responsible for implementing reference
3026 * counting, notifying the driver, handling routing messages, and releasing
3027 * any dependent link layer state.
3030 if_freemulti(struct ifmultiaddr *ifma)
3033 KASSERT(ifma->ifma_refcount == 0, ("if_freemulti: refcount %d",
3034 ifma->ifma_refcount));
3035 KASSERT(ifma->ifma_protospec == NULL,
3036 ("if_freemulti: protospec not NULL"));
3038 if (ifma->ifma_lladdr != NULL)
3039 free(ifma->ifma_lladdr, M_IFMADDR);
3040 free(ifma->ifma_addr, M_IFMADDR);
3041 free(ifma, M_IFMADDR);
3045 * Register an additional multicast address with a network interface.
3047 * - If the address is already present, bump the reference count on the
3048 * address and return.
3049 * - If the address is not link-layer, look up a link layer address.
3050 * - Allocate address structures for one or both addresses, and attach to the
3051 * multicast address list on the interface. If automatically adding a link
3052 * layer address, the protocol address will own a reference to the link
3053 * layer address, to be freed when it is freed.
3054 * - Notify the network device driver of an addition to the multicast address
3057 * 'sa' points to caller-owned memory with the desired multicast address.
3059 * 'retifma' will be used to return a pointer to the resulting multicast
3060 * address reference, if desired.
3063 if_addmulti(struct ifnet *ifp, struct sockaddr *sa,
3064 struct ifmultiaddr **retifma)
3066 struct ifmultiaddr *ifma, *ll_ifma;
3067 struct sockaddr *llsa;
3071 * If the address is already present, return a new reference to it;
3072 * otherwise, allocate storage and set up a new address.
3075 ifma = if_findmulti(ifp, sa);
3077 ifma->ifma_refcount++;
3078 if (retifma != NULL)
3080 IF_ADDR_WUNLOCK(ifp);
3085 * The address isn't already present; resolve the protocol address
3086 * into a link layer address, and then look that up, bump its
3087 * refcount or allocate an ifma for that also. If 'llsa' was
3088 * returned, we will need to free it later.
3092 if (ifp->if_resolvemulti != NULL) {
3093 error = ifp->if_resolvemulti(ifp, &llsa, sa);
3099 * Allocate the new address. Don't hook it up yet, as we may also
3100 * need to allocate a link layer multicast address.
3102 ifma = if_allocmulti(ifp, sa, llsa, M_NOWAIT);
3109 * If a link layer address is found, we'll need to see if it's
3110 * already present in the address list, or allocate is as well.
3111 * When this block finishes, the link layer address will be on the
3115 ll_ifma = if_findmulti(ifp, llsa);
3116 if (ll_ifma == NULL) {
3117 ll_ifma = if_allocmulti(ifp, llsa, NULL, M_NOWAIT);
3118 if (ll_ifma == NULL) {
3119 --ifma->ifma_refcount;
3124 TAILQ_INSERT_HEAD(&ifp->if_multiaddrs, ll_ifma,
3127 ll_ifma->ifma_refcount++;
3128 ifma->ifma_llifma = ll_ifma;
3132 * We now have a new multicast address, ifma, and possibly a new or
3133 * referenced link layer address. Add the primary address to the
3134 * ifnet address list.
3136 TAILQ_INSERT_HEAD(&ifp->if_multiaddrs, ifma, ifma_link);
3138 if (retifma != NULL)
3142 * Must generate the message while holding the lock so that 'ifma'
3143 * pointer is still valid.
3145 rt_newmaddrmsg(RTM_NEWMADDR, ifma);
3146 IF_ADDR_WUNLOCK(ifp);
3149 * We are certain we have added something, so call down to the
3150 * interface to let them know about it.
3152 if (ifp->if_ioctl != NULL) {
3153 (void) (*ifp->if_ioctl)(ifp, SIOCADDMULTI, 0);
3157 free(llsa, M_IFMADDR);
3163 free(llsa, M_IFMADDR);
3166 IF_ADDR_WUNLOCK(ifp);
3171 * Delete a multicast group membership by network-layer group address.
3173 * Returns ENOENT if the entry could not be found. If ifp no longer
3174 * exists, results are undefined. This entry point should only be used
3175 * from subsystems which do appropriate locking to hold ifp for the
3176 * duration of the call.
3177 * Network-layer protocol domains must use if_delmulti_ifma().
3180 if_delmulti(struct ifnet *ifp, struct sockaddr *sa)
3182 struct ifmultiaddr *ifma;
3187 IFNET_RLOCK_NOSLEEP();
3188 TAILQ_FOREACH(oifp, &V_ifnet, if_link)
3193 IFNET_RUNLOCK_NOSLEEP();
3195 KASSERT(ifp != NULL, ("%s: ifnet went away", __func__));
3202 ifma = if_findmulti(ifp, sa);
3204 lastref = if_delmulti_locked(ifp, ifma, 0);
3205 IF_ADDR_WUNLOCK(ifp);
3210 if (lastref && ifp->if_ioctl != NULL) {
3211 (void)(*ifp->if_ioctl)(ifp, SIOCDELMULTI, 0);
3218 * Delete all multicast group membership for an interface.
3219 * Should be used to quickly flush all multicast filters.
3222 if_delallmulti(struct ifnet *ifp)
3224 struct ifmultiaddr *ifma;
3225 struct ifmultiaddr *next;
3228 TAILQ_FOREACH_SAFE(ifma, &ifp->if_multiaddrs, ifma_link, next)
3229 if_delmulti_locked(ifp, ifma, 0);
3230 IF_ADDR_WUNLOCK(ifp);
3234 * Delete a multicast group membership by group membership pointer.
3235 * Network-layer protocol domains must use this routine.
3237 * It is safe to call this routine if the ifp disappeared.
3240 if_delmulti_ifma(struct ifmultiaddr *ifma)
3245 ifp = ifma->ifma_ifp;
3248 printf("%s: ifma_ifp seems to be detached\n", __func__);
3252 IFNET_RLOCK_NOSLEEP();
3253 TAILQ_FOREACH(oifp, &V_ifnet, if_link)
3257 printf("%s: ifnet %p disappeared\n", __func__, ifp);
3260 IFNET_RUNLOCK_NOSLEEP();
3264 * If and only if the ifnet instance exists: Acquire the address lock.
3269 lastref = if_delmulti_locked(ifp, ifma, 0);
3273 * If and only if the ifnet instance exists:
3274 * Release the address lock.
3275 * If the group was left: update the hardware hash filter.
3277 IF_ADDR_WUNLOCK(ifp);
3278 if (lastref && ifp->if_ioctl != NULL) {
3279 (void)(*ifp->if_ioctl)(ifp, SIOCDELMULTI, 0);
3285 * Perform deletion of network-layer and/or link-layer multicast address.
3287 * Return 0 if the reference count was decremented.
3288 * Return 1 if the final reference was released, indicating that the
3289 * hardware hash filter should be reprogrammed.
3292 if_delmulti_locked(struct ifnet *ifp, struct ifmultiaddr *ifma, int detaching)
3294 struct ifmultiaddr *ll_ifma;
3296 if (ifp != NULL && ifma->ifma_ifp != NULL) {
3297 KASSERT(ifma->ifma_ifp == ifp,
3298 ("%s: inconsistent ifp %p", __func__, ifp));
3299 IF_ADDR_WLOCK_ASSERT(ifp);
3302 ifp = ifma->ifma_ifp;
3305 * If the ifnet is detaching, null out references to ifnet,
3306 * so that upper protocol layers will notice, and not attempt
3307 * to obtain locks for an ifnet which no longer exists. The
3308 * routing socket announcement must happen before the ifnet
3309 * instance is detached from the system.
3313 printf("%s: detaching ifnet instance %p\n", __func__, ifp);
3316 * ifp may already be nulled out if we are being reentered
3317 * to delete the ll_ifma.
3320 rt_newmaddrmsg(RTM_DELMADDR, ifma);
3321 ifma->ifma_ifp = NULL;
3325 if (--ifma->ifma_refcount > 0)
3329 * If this ifma is a network-layer ifma, a link-layer ifma may
3330 * have been associated with it. Release it first if so.
3332 ll_ifma = ifma->ifma_llifma;
3333 if (ll_ifma != NULL) {
3334 KASSERT(ifma->ifma_lladdr != NULL,
3335 ("%s: llifma w/o lladdr", __func__));
3337 ll_ifma->ifma_ifp = NULL; /* XXX */
3338 if (--ll_ifma->ifma_refcount == 0) {
3340 TAILQ_REMOVE(&ifp->if_multiaddrs, ll_ifma,
3343 if_freemulti(ll_ifma);
3348 TAILQ_REMOVE(&ifp->if_multiaddrs, ifma, ifma_link);
3353 * The last reference to this instance of struct ifmultiaddr
3354 * was released; the hardware should be notified of this change.
3360 * Set the link layer address on an interface.
3362 * At this time we only support certain types of interfaces,
3363 * and we don't allow the length of the address to change.
3366 if_setlladdr(struct ifnet *ifp, const u_char *lladdr, int len)
3368 struct sockaddr_dl *sdl;
3375 IF_ADDR_RUNLOCK(ifp);
3379 IF_ADDR_RUNLOCK(ifp);
3380 sdl = (struct sockaddr_dl *)ifa->ifa_addr;
3385 if (len != sdl->sdl_alen) { /* don't allow length to change */
3389 switch (ifp->if_type) {
3397 case IFT_IEEE8023ADLAG:
3399 bcopy(lladdr, LLADDR(sdl), len);
3408 * If the interface is already up, we need
3409 * to re-init it in order to reprogram its
3412 if ((ifp->if_flags & IFF_UP) != 0) {
3413 if (ifp->if_ioctl) {
3414 ifp->if_flags &= ~IFF_UP;
3415 ifr.ifr_flags = ifp->if_flags & 0xffff;
3416 ifr.ifr_flagshigh = ifp->if_flags >> 16;
3417 (*ifp->if_ioctl)(ifp, SIOCSIFFLAGS, (caddr_t)&ifr);
3418 ifp->if_flags |= IFF_UP;
3419 ifr.ifr_flags = ifp->if_flags & 0xffff;
3420 ifr.ifr_flagshigh = ifp->if_flags >> 16;
3421 (*ifp->if_ioctl)(ifp, SIOCSIFFLAGS, (caddr_t)&ifr);
3425 * Also send gratuitous ARPs to notify other nodes about
3426 * the address change.
3428 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
3429 if (ifa->ifa_addr->sa_family == AF_INET)
3430 arp_ifinit(ifp, ifa);
3438 * The name argument must be a pointer to storage which will last as
3439 * long as the interface does. For physical devices, the result of
3440 * device_get_name(dev) is a good choice and for pseudo-devices a
3441 * static string works well.
3444 if_initname(struct ifnet *ifp, const char *name, int unit)
3446 ifp->if_dname = name;
3447 ifp->if_dunit = unit;
3448 if (unit != IF_DUNIT_NONE)
3449 snprintf(ifp->if_xname, IFNAMSIZ, "%s%d", name, unit);
3451 strlcpy(ifp->if_xname, name, IFNAMSIZ);
3455 if_printf(struct ifnet *ifp, const char * fmt, ...)
3460 retval = printf("%s: ", ifp->if_xname);
3462 retval += vprintf(fmt, ap);
3468 if_start(struct ifnet *ifp)
3471 (*(ifp)->if_start)(ifp);
3475 * Backwards compatibility interface for drivers
3476 * that have not implemented it
3479 if_transmit(struct ifnet *ifp, struct mbuf *m)
3483 IFQ_HANDOFF(ifp, m, error);
3488 if_input_default(struct ifnet *ifp __unused, struct mbuf *m)
3495 if_handoff(struct ifqueue *ifq, struct mbuf *m, struct ifnet *ifp, int adjust)
3500 if (_IF_QFULL(ifq)) {
3507 ifp->if_obytes += m->m_pkthdr.len + adjust;
3508 if (m->m_flags & (M_BCAST|M_MCAST))
3510 active = ifp->if_drv_flags & IFF_DRV_OACTIVE;
3512 _IF_ENQUEUE(ifq, m);
3514 if (ifp != NULL && !active)
3515 (*(ifp)->if_start)(ifp);
3520 if_register_com_alloc(u_char type,
3521 if_com_alloc_t *a, if_com_free_t *f)
3524 KASSERT(if_com_alloc[type] == NULL,
3525 ("if_register_com_alloc: %d already registered", type));
3526 KASSERT(if_com_free[type] == NULL,
3527 ("if_register_com_alloc: %d free already registered", type));
3529 if_com_alloc[type] = a;
3530 if_com_free[type] = f;
3534 if_deregister_com_alloc(u_char type)
3537 KASSERT(if_com_alloc[type] != NULL,
3538 ("if_deregister_com_alloc: %d not registered", type));
3539 KASSERT(if_com_free[type] != NULL,
3540 ("if_deregister_com_alloc: %d free not registered", type));
3541 if_com_alloc[type] = NULL;
3542 if_com_free[type] = NULL;