xref: /freebsd/sys/net/if.c (revision b5c46895fdddcdb7dd1994598925d6989ea7c8f2)
1 /*-
2  * SPDX-License-Identifier: BSD-3-Clause
3  *
4  * Copyright (c) 2010 Bjoern A. Zeeb <bz@FreeBSD.org>
5  * Copyright (c) 1980, 1986, 1993
6  *	The Regents of the University of California.  All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  * 3. Neither the name of the University nor the names of its contributors
17  *    may be used to endorse or promote products derived from this software
18  *    without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30  * SUCH DAMAGE.
31  */
32 
33 #include "opt_bpf.h"
34 #include "opt_inet6.h"
35 #include "opt_inet.h"
36 #include "opt_ddb.h"
37 
38 #include <sys/param.h>
39 #include <sys/capsicum.h>
40 #include <sys/conf.h>
41 #include <sys/eventhandler.h>
42 #include <sys/malloc.h>
43 #include <sys/domainset.h>
44 #include <sys/sbuf.h>
45 #include <sys/bus.h>
46 #include <sys/epoch.h>
47 #include <sys/mbuf.h>
48 #include <sys/systm.h>
49 #include <sys/priv.h>
50 #include <sys/proc.h>
51 #include <sys/socket.h>
52 #include <sys/socketvar.h>
53 #include <sys/protosw.h>
54 #include <sys/kernel.h>
55 #include <sys/lock.h>
56 #include <sys/refcount.h>
57 #include <sys/module.h>
58 #include <sys/nv.h>
59 #include <sys/rwlock.h>
60 #include <sys/sockio.h>
61 #include <sys/stdarg.h>
62 #include <sys/syslog.h>
63 #include <sys/sysctl.h>
64 #include <sys/sysent.h>
65 #include <sys/taskqueue.h>
66 #include <sys/domain.h>
67 #include <sys/jail.h>
68 #include <sys/priv.h>
69 
70 #ifdef DDB
71 #include <ddb/ddb.h>
72 #endif
73 
74 #include <vm/uma.h>
75 
76 #include <net/bpf.h>
77 #include <net/if.h>
78 #include <net/if_arp.h>
79 #include <net/if_clone.h>
80 #include <net/if_dl.h>
81 #include <net/if_strings.h>
82 #include <net/if_types.h>
83 #include <net/if_var.h>
84 #include <net/if_media.h>
85 #include <net/if_mib.h>
86 #include <net/if_private.h>
87 #include <net/if_vlan_var.h>
88 #include <net/radix.h>
89 #include <net/route.h>
90 #include <net/route/route_ctl.h>
91 #include <net/vnet.h>
92 
93 #if defined(INET) || defined(INET6)
94 #include <net/ethernet.h>
95 #include <netinet/in.h>
96 #include <netinet/in_var.h>
97 #include <netinet/ip.h>
98 #include <netinet/ip_carp.h>
99 #ifdef INET
100 #include <net/debugnet.h>
101 #include <netinet/if_ether.h>
102 #endif /* INET */
103 #ifdef INET6
104 #include <netinet6/in6_var.h>
105 #include <netinet6/in6_ifattach.h>
106 #endif /* INET6 */
107 #endif /* INET || INET6 */
108 
109 #include <security/mac/mac_framework.h>
110 
111 /*
112  * Consumers of struct ifreq such as tcpdump assume no pad between ifr_name
113  * and ifr_ifru when it is used in SIOCGIFCONF.
114  */
115 _Static_assert(sizeof(((struct ifreq *)0)->ifr_name) ==
116     offsetof(struct ifreq, ifr_ifru), "gap between ifr_name and ifr_ifru");
117 
118 __read_mostly epoch_t net_epoch_preempt;
119 #ifdef COMPAT_FREEBSD32
120 #include <sys/mount.h>
121 #include <compat/freebsd32/freebsd32.h>
122 
123 struct ifreq_buffer32 {
124 	uint32_t	length;		/* (size_t) */
125 	uint32_t	buffer;		/* (void *) */
126 };
127 
128 /*
129  * Interface request structure used for socket
130  * ioctl's.  All interface ioctl's must have parameter
131  * definitions which begin with ifr_name.  The
132  * remainder may be interface specific.
133  */
134 struct ifreq32 {
135 	char	ifr_name[IFNAMSIZ];		/* if name, e.g. "en0" */
136 	union {
137 		struct sockaddr	ifru_addr;
138 		struct sockaddr	ifru_dstaddr;
139 		struct sockaddr	ifru_broadaddr;
140 		struct ifreq_buffer32 ifru_buffer;
141 		short		ifru_flags[2];
142 		short		ifru_index;
143 		int		ifru_jid;
144 		int		ifru_metric;
145 		int		ifru_mtu;
146 		int		ifru_phys;
147 		int		ifru_media;
148 		uint32_t	ifru_data;
149 		int		ifru_cap[2];
150 		u_int		ifru_fib;
151 		u_char		ifru_vlan_pcp;
152 	} ifr_ifru;
153 };
154 CTASSERT(sizeof(struct ifreq) == sizeof(struct ifreq32));
155 CTASSERT(__offsetof(struct ifreq, ifr_ifru) ==
156     __offsetof(struct ifreq32, ifr_ifru));
157 
158 struct ifconf32 {
159 	int32_t	ifc_len;
160 	union {
161 		uint32_t	ifcu_buf;
162 		uint32_t	ifcu_req;
163 	} ifc_ifcu;
164 };
165 #define	SIOCGIFCONF32	_IOWR('i', 36, struct ifconf32)
166 
167 struct ifdrv32 {
168 	char		ifd_name[IFNAMSIZ];
169 	uint32_t	ifd_cmd;
170 	uint32_t	ifd_len;
171 	uint32_t	ifd_data;
172 };
173 #define SIOCSDRVSPEC32	_IOC_NEWTYPE(SIOCSDRVSPEC, struct ifdrv32)
174 #define SIOCGDRVSPEC32	_IOC_NEWTYPE(SIOCGDRVSPEC, struct ifdrv32)
175 
176 struct ifgroupreq32 {
177 	char	ifgr_name[IFNAMSIZ];
178 	u_int	ifgr_len;
179 	union {
180 		char		ifgru_group[IFNAMSIZ];
181 		uint32_t	ifgru_groups;
182 	} ifgr_ifgru;
183 };
184 #define	SIOCAIFGROUP32	_IOC_NEWTYPE(SIOCAIFGROUP, struct ifgroupreq32)
185 #define	SIOCGIFGROUP32	_IOC_NEWTYPE(SIOCGIFGROUP, struct ifgroupreq32)
186 #define	SIOCDIFGROUP32	_IOC_NEWTYPE(SIOCDIFGROUP, struct ifgroupreq32)
187 #define	SIOCGIFGMEMB32	_IOC_NEWTYPE(SIOCGIFGMEMB, struct ifgroupreq32)
188 
189 struct ifmediareq32 {
190 	char		ifm_name[IFNAMSIZ];
191 	int		ifm_current;
192 	int		ifm_mask;
193 	int		ifm_status;
194 	int		ifm_active;
195 	int		ifm_count;
196 	uint32_t	ifm_ulist;	/* (int *) */
197 };
198 #define	SIOCGIFMEDIA32	_IOC_NEWTYPE(SIOCGIFMEDIA, struct ifmediareq32)
199 #define	SIOCGIFXMEDIA32	_IOC_NEWTYPE(SIOCGIFXMEDIA, struct ifmediareq32)
200 #endif /* COMPAT_FREEBSD32 */
201 
202 union ifreq_union {
203 	struct ifreq	ifr;
204 #ifdef COMPAT_FREEBSD32
205 	struct ifreq32	ifr32;
206 #endif
207 };
208 
209 SYSCTL_NODE(_net, PF_LINK, link, CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
210     "Link layers");
211 SYSCTL_NODE(_net_link, 0, generic, CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
212     "Generic link-management");
213 
214 SYSCTL_INT(_net_link, OID_AUTO, ifqmaxlen, CTLFLAG_RDTUN,
215     &ifqmaxlen, 0, "max send queue size");
216 
217 /* Log link state change events */
218 static int log_link_state_change = 1;
219 
220 SYSCTL_INT(_net_link, OID_AUTO, log_link_state_change, CTLFLAG_RW,
221 	&log_link_state_change, 0,
222 	"log interface link state change events");
223 
224 /* Log promiscuous mode change events */
225 static int log_promisc_mode_change = 1;
226 
227 SYSCTL_INT(_net_link, OID_AUTO, log_promisc_mode_change, CTLFLAG_RDTUN,
228 	&log_promisc_mode_change, 1,
229 	"log promiscuous mode change events");
230 
231 /* Interface description */
232 static unsigned int ifdescr_maxlen = 1024;
233 SYSCTL_UINT(_net, OID_AUTO, ifdescr_maxlen, CTLFLAG_RW,
234 	&ifdescr_maxlen, 0,
235 	"administrative maximum length for interface description");
236 
237 static MALLOC_DEFINE(M_IFDESCR, "ifdescr", "ifnet descriptions");
238 
239 /* global sx for non-critical path ifdescr */
240 static struct sx ifdescr_sx;
241 SX_SYSINIT(ifdescr_sx, &ifdescr_sx, "ifnet descr");
242 
243 void	(*ng_ether_link_state_p)(struct ifnet *ifp, int state);
244 void	(*lagg_linkstate_p)(struct ifnet *ifp, int state);
245 /* These are external hooks for CARP. */
246 void	(*carp_linkstate_p)(struct ifnet *ifp);
247 void	(*carp_demote_adj_p)(int, char *);
248 int	(*carp_master_p)(struct ifaddr *);
249 #if defined(INET) || defined(INET6)
250 int	(*carp_forus_p)(struct ifnet *ifp, u_char *dhost);
251 int	(*carp_output_p)(struct ifnet *ifp, struct mbuf *m,
252     const struct sockaddr *sa);
253 int	(*carp_ioctl_p)(struct ifreq *, u_long, struct thread *);
254 int	(*carp_attach_p)(struct ifaddr *, int);
255 void	(*carp_detach_p)(struct ifaddr *, bool);
256 #endif
257 #ifdef INET
258 int	(*carp_iamatch_p)(struct ifaddr *, uint8_t **);
259 #endif
260 #ifdef INET6
261 struct ifaddr *(*carp_iamatch6_p)(struct ifnet *ifp, struct in6_addr *taddr6);
262 caddr_t	(*carp_macmatch6_p)(struct ifnet *ifp, struct mbuf *m,
263     const struct in6_addr *taddr);
264 #endif
265 
266 struct mbuf *(*tbr_dequeue_ptr)(struct ifaltq *, int) = NULL;
267 
268 /*
269  * XXX: Style; these should be sorted alphabetically, and unprototyped
270  * static functions should be prototyped. Currently they are sorted by
271  * declaration order.
272  */
273 static void	if_attachdomain(void *);
274 static void	if_attachdomain1(struct ifnet *);
275 static int	ifconf(u_long, caddr_t);
276 static void	if_input_default(struct ifnet *, struct mbuf *);
277 static int	if_requestencap_default(struct ifnet *, struct if_encap_req *);
278 static int	if_setflag(struct ifnet *, int, int, int *, int);
279 static int	if_transmit_default(struct ifnet *ifp, struct mbuf *m);
280 static int	if_delmulti_locked(struct ifnet *, struct ifmultiaddr *, int);
281 static void	do_link_state_change(void *, int);
282 static int	if_getgroup(struct ifgroupreq *, struct ifnet *);
283 static int	if_getgroupmembers(struct ifgroupreq *);
284 static void	if_delgroups(struct ifnet *);
285 static void	if_attach_internal(struct ifnet *, bool);
286 static void	if_detach_internal(struct ifnet *, bool);
287 static void	if_siocaddmulti(void *, int);
288 static void	if_link_ifnet(struct ifnet *);
289 static bool	if_unlink_ifnet(struct ifnet *, bool);
290 #ifdef VIMAGE
291 static void	if_vmove(struct ifnet *, struct vnet *);
292 #endif
293 
294 #ifdef INET6
295 /*
296  * XXX: declare here to avoid to include many inet6 related files..
297  * should be more generalized?
298  */
299 extern void	nd6_setmtu(struct ifnet *);
300 #endif
301 
302 /* ipsec helper hooks */
303 VNET_DEFINE(struct hhook_head *, ipsec_hhh_in[HHOOK_IPSEC_COUNT]);
304 VNET_DEFINE(struct hhook_head *, ipsec_hhh_out[HHOOK_IPSEC_COUNT]);
305 
306 int	ifqmaxlen = IFQ_MAXLEN;
307 VNET_DEFINE(struct ifnethead, ifnet);	/* depend on static init XXX */
308 VNET_DEFINE(struct ifgrouphead, ifg_head);
309 
310 /* Table of ifnet by index. */
311 static int if_index;
312 static int if_indexlim = 8;
313 static struct ifindex_entry {
314 	struct ifnet	*ife_ifnet;
315 	uint16_t	ife_gencnt;
316 } *ifindex_table;
317 
318 SYSCTL_NODE(_net_link_generic, IFMIB_SYSTEM, system,
319     CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
320     "Variables global to all interfaces");
321 static int
sysctl_ifcount(SYSCTL_HANDLER_ARGS)322 sysctl_ifcount(SYSCTL_HANDLER_ARGS)
323 {
324 	int rv = 0;
325 
326 	IFNET_RLOCK();
327 	for (int i = 1; i <= if_index; i++)
328 		if (ifindex_table[i].ife_ifnet != NULL &&
329 		    ifindex_table[i].ife_ifnet->if_vnet == curvnet)
330 			rv = i;
331 	IFNET_RUNLOCK();
332 
333 	return (sysctl_handle_int(oidp, &rv, 0, req));
334 }
335 SYSCTL_PROC(_net_link_generic_system, IFMIB_IFCOUNT, ifcount,
336     CTLTYPE_INT | CTLFLAG_VNET | CTLFLAG_RD, NULL, 0, sysctl_ifcount, "I",
337     "Maximum known interface index");
338 
339 /*
340  * The global network interface list (V_ifnet) and related state (such as
341  * if_index, if_indexlim, and ifindex_table) are protected by an sxlock.
342  * This may be acquired to stabilise the list, or we may rely on NET_EPOCH.
343  */
344 struct sx ifnet_sxlock;
345 SX_SYSINIT_FLAGS(ifnet_sx, &ifnet_sxlock, "ifnet_sx", SX_RECURSE);
346 
347 struct sx ifnet_detach_sxlock;
348 SX_SYSINIT_FLAGS(ifnet_detach, &ifnet_detach_sxlock, "ifnet_detach_sx",
349     SX_RECURSE);
350 
351 #ifdef VIMAGE
352 #define	VNET_IS_SHUTTING_DOWN(_vnet)					\
353     ((_vnet)->vnet_shutdown && (_vnet)->vnet_state < SI_SUB_VNET_DONE)
354 #endif
355 
356 static	if_com_alloc_t *if_com_alloc[256];
357 static	if_com_free_t *if_com_free[256];
358 
359 static MALLOC_DEFINE(M_IFNET, "ifnet", "interface internals");
360 MALLOC_DEFINE(M_IFADDR, "ifaddr", "interface address");
361 MALLOC_DEFINE(M_IFMADDR, "ether_multi", "link-level multicast address");
362 
363 struct ifnet *
ifnet_byindex(u_int idx)364 ifnet_byindex(u_int idx)
365 {
366 	struct ifnet *ifp;
367 
368 	NET_EPOCH_ASSERT();
369 
370 	if (__predict_false(idx > if_index))
371 		return (NULL);
372 
373 	ifp = ck_pr_load_ptr(&ifindex_table[idx].ife_ifnet);
374 
375 	if (curvnet != NULL && ifp != NULL && ifp->if_vnet != curvnet)
376 		ifp = NULL;
377 
378 	return (ifp);
379 }
380 
381 struct ifnet *
ifnet_byindex_ref(u_int idx)382 ifnet_byindex_ref(u_int idx)
383 {
384 	struct ifnet *ifp;
385 
386 	ifp = ifnet_byindex(idx);
387 	if (ifp == NULL || (ifp->if_flags & IFF_DYING))
388 		return (NULL);
389 	if (!if_try_ref(ifp))
390 		return (NULL);
391 	return (ifp);
392 }
393 
394 struct ifnet *
ifnet_byindexgen(uint16_t idx,uint16_t gen)395 ifnet_byindexgen(uint16_t idx, uint16_t gen)
396 {
397 	struct ifnet *ifp;
398 
399 	NET_EPOCH_ASSERT();
400 
401 	if (__predict_false(idx > if_index))
402 		return (NULL);
403 
404 	ifp = ck_pr_load_ptr(&ifindex_table[idx].ife_ifnet);
405 
406 	if (ifindex_table[idx].ife_gencnt == gen)
407 		return (ifp);
408 	else
409 		return (NULL);
410 }
411 
412 /*
413  * Network interface utility routines.
414  *
415  * Routines with ifa_ifwith* names take sockaddr *'s as
416  * parameters.
417  */
418 
419 static void
if_init_idxtable(void * arg __unused)420 if_init_idxtable(void *arg __unused)
421 {
422 
423 	ifindex_table = malloc(if_indexlim * sizeof(*ifindex_table),
424 	    M_IFNET, M_WAITOK | M_ZERO);
425 }
426 SYSINIT(if_init, SI_SUB_INIT_IF, SI_ORDER_SECOND, if_init_idxtable, NULL);
427 
428 static void
vnet_if_init(const void * unused __unused)429 vnet_if_init(const void *unused __unused)
430 {
431 
432 	CK_STAILQ_INIT(&V_ifnet);
433 	CK_STAILQ_INIT(&V_ifg_head);
434 }
435 VNET_SYSINIT(vnet_if_init, SI_SUB_INIT_IF, SI_ORDER_SECOND, vnet_if_init,
436     NULL);
437 
438 static void
if_link_ifnet(struct ifnet * ifp)439 if_link_ifnet(struct ifnet *ifp)
440 {
441 
442 	IFNET_WLOCK();
443 	CK_STAILQ_INSERT_TAIL(&V_ifnet, ifp, if_link);
444 #ifdef VIMAGE
445 	curvnet->vnet_ifcnt++;
446 #endif
447 	IFNET_WUNLOCK();
448 }
449 
450 static bool
if_unlink_ifnet(struct ifnet * ifp,bool vmove)451 if_unlink_ifnet(struct ifnet *ifp, bool vmove)
452 {
453 	struct ifnet *iter;
454 	int found = 0;
455 
456 	IFNET_WLOCK();
457 	CK_STAILQ_FOREACH(iter, &V_ifnet, if_link)
458 		if (iter == ifp) {
459 			CK_STAILQ_REMOVE(&V_ifnet, ifp, ifnet, if_link);
460 			if (!vmove)
461 				ifp->if_flags |= IFF_DYING;
462 			found = 1;
463 			break;
464 		}
465 #ifdef VIMAGE
466 	curvnet->vnet_ifcnt--;
467 #endif
468 	IFNET_WUNLOCK();
469 
470 	return (found);
471 }
472 
473 #ifdef VIMAGE
474 static void
vnet_if_return(const void * unused __unused)475 vnet_if_return(const void *unused __unused)
476 {
477 	struct ifnet *ifp, *nifp;
478 	struct ifnet **pending;
479 	int found __diagused;
480 	int i;
481 
482 	i = 0;
483 
484 	/*
485 	 * We need to protect our access to the V_ifnet tailq. Ordinarily we'd
486 	 * enter NET_EPOCH, but that's not possible, because if_vmove() calls
487 	 * if_detach_internal(), which waits for NET_EPOCH callbacks to
488 	 * complete. We can't do that from within NET_EPOCH.
489 	 *
490 	 * However, we can also use the IFNET_xLOCK, which is the V_ifnet
491 	 * read/write lock. We cannot hold the lock as we call if_vmove()
492 	 * though, as that presents LOR w.r.t ifnet_sx, in_multi_sx and iflib
493 	 * ctx lock.
494 	 */
495 	IFNET_WLOCK();
496 
497 	pending = malloc(sizeof(struct ifnet *) * curvnet->vnet_ifcnt,
498 	    M_IFNET, M_WAITOK | M_ZERO);
499 
500 	/* Return all inherited interfaces to their parent vnets. */
501 	CK_STAILQ_FOREACH_SAFE(ifp, &V_ifnet, if_link, nifp) {
502 		if (ifp->if_home_vnet != ifp->if_vnet) {
503 			found = if_unlink_ifnet(ifp, true);
504 			MPASS(found);
505 
506 			pending[i++] = ifp;
507 		}
508 	}
509 	IFNET_WUNLOCK();
510 
511 	for (int j = 0; j < i; j++) {
512 		sx_xlock(&ifnet_detach_sxlock);
513 		if_vmove(pending[j], pending[j]->if_home_vnet);
514 		sx_xunlock(&ifnet_detach_sxlock);
515 	}
516 
517 	free(pending, M_IFNET);
518 }
519 VNET_SYSUNINIT(vnet_if_return, SI_SUB_VNET_DONE, SI_ORDER_ANY,
520     vnet_if_return, NULL);
521 #endif
522 
523 /*
524  * Allocate a struct ifnet and an index for an interface.  A layer 2
525  * common structure will also be allocated if an allocation routine is
526  * registered for the passed type.
527  */
528 static struct ifnet *
if_alloc_domain(u_char type,int numa_domain)529 if_alloc_domain(u_char type, int numa_domain)
530 {
531 	struct ifnet *ifp;
532 	u_short idx;
533 
534 	KASSERT(numa_domain <= IF_NODOM, ("numa_domain too large"));
535 	if (numa_domain == IF_NODOM)
536 		ifp = malloc(sizeof(struct ifnet), M_IFNET,
537 		    M_WAITOK | M_ZERO);
538 	else
539 		ifp = malloc_domainset(sizeof(struct ifnet), M_IFNET,
540 		    DOMAINSET_PREF(numa_domain), M_WAITOK | M_ZERO);
541 	ifp->if_type = type;
542 	ifp->if_alloctype = type;
543 	ifp->if_numa_domain = numa_domain;
544 #ifdef VIMAGE
545 	ifp->if_vnet = curvnet;
546 #endif
547 	if (if_com_alloc[type] != NULL) {
548 		ifp->if_l2com = if_com_alloc[type](type, ifp);
549 		KASSERT(ifp->if_l2com, ("%s: if_com_alloc[%u] failed", __func__,
550 		    type));
551 	}
552 
553 	IF_ADDR_LOCK_INIT(ifp);
554 	TASK_INIT(&ifp->if_linktask, 0, do_link_state_change, ifp);
555 	TASK_INIT(&ifp->if_addmultitask, 0, if_siocaddmulti, ifp);
556 	ifp->if_afdata_initialized = 0;
557 	IF_AFDATA_LOCK_INIT(ifp);
558 	CK_STAILQ_INIT(&ifp->if_addrhead);
559 	CK_STAILQ_INIT(&ifp->if_multiaddrs);
560 	CK_STAILQ_INIT(&ifp->if_groups);
561 #ifdef MAC
562 	mac_ifnet_init(ifp);
563 #endif
564 	ifq_init(&ifp->if_snd, ifp);
565 
566 	refcount_init(&ifp->if_refcount, 1);	/* Index reference. */
567 	for (int i = 0; i < IFCOUNTERS; i++)
568 		ifp->if_counters[i] = counter_u64_alloc(M_WAITOK);
569 	ifp->if_get_counter = if_get_counter_default;
570 	ifp->if_pcp = IFNET_PCP_NONE;
571 
572 	/* Allocate an ifindex array entry. */
573 	IFNET_WLOCK();
574 	/*
575 	 * Try to find an empty slot below if_index.  If we fail, take the
576 	 * next slot.
577 	 */
578 	for (idx = 1; idx <= if_index; idx++) {
579 		if (ifindex_table[idx].ife_ifnet == NULL)
580 			break;
581 	}
582 
583 	/* Catch if_index overflow. */
584 	if (idx >= if_indexlim) {
585 		struct ifindex_entry *new, *old;
586 		int newlim;
587 
588 		newlim = if_indexlim * 2;
589 		new = malloc(newlim * sizeof(*new), M_IFNET, M_WAITOK | M_ZERO);
590 		memcpy(new, ifindex_table, if_indexlim * sizeof(*new));
591 		old = ifindex_table;
592 		ck_pr_store_ptr(&ifindex_table, new);
593 		if_indexlim = newlim;
594 		NET_EPOCH_WAIT();
595 		free(old, M_IFNET);
596 	}
597 	if (idx > if_index)
598 		if_index = idx;
599 
600 	ifp->if_index = idx;
601 	ifp->if_idxgen = ifindex_table[idx].ife_gencnt;
602 	ck_pr_store_ptr(&ifindex_table[idx].ife_ifnet, ifp);
603 	IFNET_WUNLOCK();
604 
605 	return (ifp);
606 }
607 
608 struct ifnet *
if_alloc_dev(u_char type,device_t dev)609 if_alloc_dev(u_char type, device_t dev)
610 {
611 	int numa_domain;
612 
613 	if (dev == NULL || bus_get_domain(dev, &numa_domain) != 0)
614 		return (if_alloc_domain(type, IF_NODOM));
615 	return (if_alloc_domain(type, numa_domain));
616 }
617 
618 struct ifnet *
if_alloc(u_char type)619 if_alloc(u_char type)
620 {
621 
622 	return (if_alloc_domain(type, IF_NODOM));
623 }
624 /*
625  * Do the actual work of freeing a struct ifnet, and layer 2 common
626  * structure.  This call is made when the network epoch guarantees
627  * us that nobody holds a pointer to the interface.
628  */
629 static void
if_free_deferred(epoch_context_t ctx)630 if_free_deferred(epoch_context_t ctx)
631 {
632 	struct ifnet *ifp = __containerof(ctx, struct ifnet, if_epoch_ctx);
633 
634 	KASSERT((ifp->if_flags & IFF_DYING),
635 	    ("%s: interface not dying", __func__));
636 
637 	if (if_com_free[ifp->if_alloctype] != NULL)
638 		if_com_free[ifp->if_alloctype](ifp->if_l2com,
639 		    ifp->if_alloctype);
640 
641 #ifdef MAC
642 	mac_ifnet_destroy(ifp);
643 #endif /* MAC */
644 	IF_AFDATA_DESTROY(ifp);
645 	IF_ADDR_LOCK_DESTROY(ifp);
646 	ifq_delete(&ifp->if_snd);
647 
648 	for (int i = 0; i < IFCOUNTERS; i++)
649 		counter_u64_free(ifp->if_counters[i]);
650 
651 	if_freedescr(ifp->if_description);
652 	free(ifp->if_hw_addr, M_IFADDR);
653 	free(ifp, M_IFNET);
654 }
655 
656 /*
657  * Deregister an interface and free the associated storage.
658  */
659 void
if_free(struct ifnet * ifp)660 if_free(struct ifnet *ifp)
661 {
662 
663 	ifp->if_flags |= IFF_DYING;			/* XXX: Locking */
664 
665 	/*
666 	 * XXXGL: An interface index is really an alias to ifp pointer.
667 	 * Why would we clear the alias now, and not in the deferred
668 	 * context?  Indeed there is nothing wrong with some network
669 	 * thread obtaining ifp via ifnet_byindex() inside the network
670 	 * epoch and then dereferencing ifp while we perform if_free(),
671 	 * and after if_free() finished, too.
672 	 *
673 	 * This early index freeing was important back when ifindex was
674 	 * virtualized and interface would outlive the vnet.
675 	 */
676 	IFNET_WLOCK();
677 	MPASS(ifindex_table[ifp->if_index].ife_ifnet == ifp);
678 	ck_pr_store_ptr(&ifindex_table[ifp->if_index].ife_ifnet, NULL);
679 	ifindex_table[ifp->if_index].ife_gencnt++;
680 	while (if_index > 0 && ifindex_table[if_index].ife_ifnet == NULL)
681 		if_index--;
682 	IFNET_WUNLOCK();
683 
684 	if (refcount_release(&ifp->if_refcount))
685 		NET_EPOCH_CALL(if_free_deferred, &ifp->if_epoch_ctx);
686 }
687 
688 /*
689  * Interfaces to keep an ifnet type-stable despite the possibility of the
690  * driver calling if_free().  If there are additional references, we defer
691  * freeing the underlying data structure.
692  */
693 void
if_ref(struct ifnet * ifp)694 if_ref(struct ifnet *ifp)
695 {
696 	u_int old __diagused;
697 
698 	/* We don't assert the ifnet list lock here, but arguably should. */
699 	old = refcount_acquire(&ifp->if_refcount);
700 	KASSERT(old > 0, ("%s: ifp %p has 0 refs", __func__, ifp));
701 }
702 
703 bool
if_try_ref(struct ifnet * ifp)704 if_try_ref(struct ifnet *ifp)
705 {
706 	NET_EPOCH_ASSERT();
707 	return (refcount_acquire_if_not_zero(&ifp->if_refcount));
708 }
709 
710 void
if_rele(struct ifnet * ifp)711 if_rele(struct ifnet *ifp)
712 {
713 
714 	if (!refcount_release(&ifp->if_refcount))
715 		return;
716 	NET_EPOCH_CALL(if_free_deferred, &ifp->if_epoch_ctx);
717 }
718 
719 void
ifq_init(struct ifaltq * ifq,struct ifnet * ifp)720 ifq_init(struct ifaltq *ifq, struct ifnet *ifp)
721 {
722 
723 	mtx_init(&ifq->ifq_mtx, ifp->if_xname, "if send queue", MTX_DEF);
724 
725 	if (ifq->ifq_maxlen == 0)
726 		ifq->ifq_maxlen = ifqmaxlen;
727 
728 	ifq->altq_type = 0;
729 	ifq->altq_disc = NULL;
730 	ifq->altq_flags &= ALTQF_CANTCHANGE;
731 	ifq->altq_tbr  = NULL;
732 	ifq->altq_ifp  = ifp;
733 }
734 
735 void
ifq_delete(struct ifaltq * ifq)736 ifq_delete(struct ifaltq *ifq)
737 {
738 	mtx_destroy(&ifq->ifq_mtx);
739 }
740 
741 /*
742  * Perform generic interface initialization tasks and attach the interface
743  * to the list of "active" interfaces.  If vmove flag is set on entry
744  * to if_attach_internal(), perform only a limited subset of initialization
745  * tasks, given that we are moving from one vnet to another an ifnet which
746  * has already been fully initialized.
747  *
748  * Note that if_detach_internal() removes group membership unconditionally
749  * even when vmove flag is set, and if_attach_internal() adds only IFG_ALL.
750  * Thus, when if_vmove() is applied to a cloned interface, group membership
751  * is lost while a cloned one always joins a group whose name is
752  * ifc->ifc_name.  To recover this after if_detach_internal() and
753  * if_attach_internal(), the cloner should be specified to
754  * if_attach_internal() via ifc.  If it is non-NULL, if_attach_internal()
755  * attempts to join a group whose name is ifc->ifc_name.
756  *
757  * XXX:
758  *  - The decision to return void and thus require this function to
759  *    succeed is questionable.
760  *  - We should probably do more sanity checking.  For instance we don't
761  *    do anything to insure if_xname is unique or non-empty.
762  */
763 void
if_attach(struct ifnet * ifp)764 if_attach(struct ifnet *ifp)
765 {
766 
767 	if_attach_internal(ifp, false);
768 }
769 
770 /*
771  * Compute the least common TSO limit.
772  */
773 void
if_hw_tsomax_common(if_t ifp,struct ifnet_hw_tsomax * pmax)774 if_hw_tsomax_common(if_t ifp, struct ifnet_hw_tsomax *pmax)
775 {
776 	/*
777 	 * 1) If there is no limit currently, take the limit from
778 	 * the network adapter.
779 	 *
780 	 * 2) If the network adapter has a limit below the current
781 	 * limit, apply it.
782 	 */
783 	if (pmax->tsomaxbytes == 0 || (ifp->if_hw_tsomax != 0 &&
784 	    ifp->if_hw_tsomax < pmax->tsomaxbytes)) {
785 		pmax->tsomaxbytes = ifp->if_hw_tsomax;
786 	}
787 	if (pmax->tsomaxsegcount == 0 || (ifp->if_hw_tsomaxsegcount != 0 &&
788 	    ifp->if_hw_tsomaxsegcount < pmax->tsomaxsegcount)) {
789 		pmax->tsomaxsegcount = ifp->if_hw_tsomaxsegcount;
790 	}
791 	if (pmax->tsomaxsegsize == 0 || (ifp->if_hw_tsomaxsegsize != 0 &&
792 	    ifp->if_hw_tsomaxsegsize < pmax->tsomaxsegsize)) {
793 		pmax->tsomaxsegsize = ifp->if_hw_tsomaxsegsize;
794 	}
795 }
796 
797 /*
798  * Update TSO limit of a network adapter.
799  *
800  * Returns zero if no change. Else non-zero.
801  */
802 int
if_hw_tsomax_update(if_t ifp,struct ifnet_hw_tsomax * pmax)803 if_hw_tsomax_update(if_t ifp, struct ifnet_hw_tsomax *pmax)
804 {
805 	int retval = 0;
806 	if (ifp->if_hw_tsomax != pmax->tsomaxbytes) {
807 		ifp->if_hw_tsomax = pmax->tsomaxbytes;
808 		retval++;
809 	}
810 	if (ifp->if_hw_tsomaxsegsize != pmax->tsomaxsegsize) {
811 		ifp->if_hw_tsomaxsegsize = pmax->tsomaxsegsize;
812 		retval++;
813 	}
814 	if (ifp->if_hw_tsomaxsegcount != pmax->tsomaxsegcount) {
815 		ifp->if_hw_tsomaxsegcount = pmax->tsomaxsegcount;
816 		retval++;
817 	}
818 	return (retval);
819 }
820 
821 static void
if_attach_internal(struct ifnet * ifp,bool vmove)822 if_attach_internal(struct ifnet *ifp, bool vmove)
823 {
824 	unsigned socksize, ifasize;
825 	int namelen, masklen;
826 	struct sockaddr_dl *sdl;
827 	struct ifaddr *ifa;
828 
829 	MPASS(ifindex_table[ifp->if_index].ife_ifnet == ifp);
830 
831 #ifdef VIMAGE
832 	CURVNET_ASSERT_SET();
833 	ifp->if_vnet = curvnet;
834 	if (ifp->if_home_vnet == NULL)
835 		ifp->if_home_vnet = curvnet;
836 #endif
837 
838 	if_addgroup(ifp, IFG_ALL);
839 
840 #ifdef VIMAGE
841 	/* Restore group membership for cloned interface. */
842 	if (vmove)
843 		if_clone_restoregroup(ifp);
844 #endif
845 
846 	getmicrotime(&ifp->if_lastchange);
847 	ifp->if_epoch = time_uptime;
848 
849 	KASSERT((ifp->if_transmit == NULL && ifp->if_qflush == NULL) ||
850 	    (ifp->if_transmit != NULL && ifp->if_qflush != NULL),
851 	    ("transmit and qflush must both either be set or both be NULL"));
852 	if (ifp->if_transmit == NULL) {
853 		ifp->if_transmit = if_transmit_default;
854 		ifp->if_qflush = if_qflush;
855 	}
856 	if (ifp->if_input == NULL)
857 		ifp->if_input = if_input_default;
858 
859 	if (ifp->if_requestencap == NULL)
860 		ifp->if_requestencap = if_requestencap_default;
861 
862 	if (!vmove) {
863 #ifdef MAC
864 		mac_ifnet_create(ifp);
865 #endif
866 
867 		/*
868 		 * Create a Link Level name for this device.
869 		 */
870 		namelen = strlen(ifp->if_xname);
871 		/*
872 		 * Always save enough space for any possible name so we
873 		 * can do a rename in place later.
874 		 */
875 		masklen = offsetof(struct sockaddr_dl, sdl_data[0]) + IFNAMSIZ;
876 		socksize = masklen + ifp->if_addrlen;
877 		if (socksize < sizeof(*sdl))
878 			socksize = sizeof(*sdl);
879 		socksize = roundup2(socksize, sizeof(long));
880 		ifasize = sizeof(*ifa) + 2 * socksize;
881 		ifa = ifa_alloc(ifasize, M_WAITOK);
882 		sdl = (struct sockaddr_dl *)(ifa + 1);
883 		sdl->sdl_len = socksize;
884 		sdl->sdl_family = AF_LINK;
885 		bcopy(ifp->if_xname, sdl->sdl_data, namelen);
886 		sdl->sdl_nlen = namelen;
887 		sdl->sdl_index = ifp->if_index;
888 		sdl->sdl_type = ifp->if_type;
889 		ifp->if_addr = ifa;
890 		ifa->ifa_ifp = ifp;
891 		ifa->ifa_addr = (struct sockaddr *)sdl;
892 		sdl = (struct sockaddr_dl *)(socksize + (caddr_t)sdl);
893 		ifa->ifa_netmask = (struct sockaddr *)sdl;
894 		sdl->sdl_len = masklen;
895 		while (namelen != 0)
896 			sdl->sdl_data[--namelen] = 0xff;
897 		CK_STAILQ_INSERT_HEAD(&ifp->if_addrhead, ifa, ifa_link);
898 		/* Reliably crash if used uninitialized. */
899 		ifp->if_broadcastaddr = NULL;
900 
901 		if (ifp->if_type == IFT_ETHER) {
902 			ifp->if_hw_addr = malloc(ifp->if_addrlen, M_IFADDR,
903 			    M_WAITOK | M_ZERO);
904 		}
905 
906 #if defined(INET) || defined(INET6)
907 		/* Use defaults for TSO, if nothing is set */
908 		if (ifp->if_hw_tsomax == 0 &&
909 		    ifp->if_hw_tsomaxsegcount == 0 &&
910 		    ifp->if_hw_tsomaxsegsize == 0) {
911 			/*
912 			 * The TSO defaults needs to be such that an
913 			 * NFS mbuf list of 35 mbufs totalling just
914 			 * below 64K works and that a chain of mbufs
915 			 * can be defragged into at most 32 segments:
916 			 */
917 			ifp->if_hw_tsomax = min(IP_MAXPACKET, (32 * MCLBYTES) -
918 			    (ETHER_HDR_LEN + ETHER_VLAN_ENCAP_LEN));
919 			ifp->if_hw_tsomaxsegcount = 35;
920 			ifp->if_hw_tsomaxsegsize = 2048;	/* 2K */
921 
922 			/* XXX some drivers set IFCAP_TSO after ethernet attach */
923 			if (ifp->if_capabilities & IFCAP_TSO) {
924 				if_printf(ifp, "Using defaults for TSO: %u/%u/%u\n",
925 				    ifp->if_hw_tsomax,
926 				    ifp->if_hw_tsomaxsegcount,
927 				    ifp->if_hw_tsomaxsegsize);
928 			}
929 		}
930 #endif
931 	}
932 
933 	if (domain_init_status >= 2)
934 		if_attachdomain1(ifp);
935 
936 	if_link_ifnet(ifp);
937 
938 	EVENTHANDLER_INVOKE(ifnet_arrival_event, ifp);
939 	if (IS_DEFAULT_VNET(curvnet))
940 		devctl_notify("IFNET", ifp->if_xname, "ATTACH", NULL);
941 }
942 
943 static void
if_epochalloc(void * dummy __unused)944 if_epochalloc(void *dummy __unused)
945 {
946 
947 	net_epoch_preempt = epoch_alloc("Net preemptible", EPOCH_PREEMPT);
948 }
949 SYSINIT(ifepochalloc, SI_SUB_EPOCH, SI_ORDER_ANY, if_epochalloc, NULL);
950 
951 static void
if_attachdomain(void * dummy)952 if_attachdomain(void *dummy)
953 {
954 	struct ifnet *ifp;
955 
956 	CK_STAILQ_FOREACH(ifp, &V_ifnet, if_link)
957 		if_attachdomain1(ifp);
958 }
959 SYSINIT(domainifattach, SI_SUB_PROTO_IFATTACHDOMAIN, SI_ORDER_SECOND,
960     if_attachdomain, NULL);
961 
962 static void
if_attachdomain1(struct ifnet * ifp)963 if_attachdomain1(struct ifnet *ifp)
964 {
965 	struct domain *dp;
966 
967 	/*
968 	 * Since dp->dom_ifattach calls malloc() with M_WAITOK, we
969 	 * cannot lock ifp->if_afdata initialization, entirely.
970 	 */
971 	IF_AFDATA_LOCK(ifp);
972 	if (ifp->if_afdata_initialized >= domain_init_status) {
973 		IF_AFDATA_UNLOCK(ifp);
974 		log(LOG_WARNING, "%s called more than once on %s\n",
975 		    __func__, ifp->if_xname);
976 		return;
977 	}
978 	ifp->if_afdata_initialized = domain_init_status;
979 	IF_AFDATA_UNLOCK(ifp);
980 
981 	/* address family dependent data region */
982 	bzero(ifp->if_afdata, sizeof(ifp->if_afdata));
983 	SLIST_FOREACH(dp, &domains, dom_next) {
984 		if (dp->dom_ifattach)
985 			ifp->if_afdata[dp->dom_family] =
986 			    (*dp->dom_ifattach)(ifp);
987 	}
988 }
989 
990 /*
991  * Remove any unicast or broadcast network addresses from an interface.
992  */
993 void
if_purgeaddrs(struct ifnet * ifp)994 if_purgeaddrs(struct ifnet *ifp)
995 {
996 	struct ifaddr *ifa;
997 
998 #ifdef INET6
999 	/*
1000 	 * Need to leave multicast addresses of proxy NDP llentries
1001 	 * before in6_purgeifaddr() because the llentries are keys
1002 	 * for in6_multi objects of proxy NDP entries.
1003 	 * in6_purgeifaddr()s clean up llentries including proxy NDPs
1004 	 * then we would lose the keys if they are called earlier.
1005 	 */
1006 	in6_purge_proxy_ndp(ifp);
1007 #endif
1008 	while (1) {
1009 		struct epoch_tracker et;
1010 
1011 		NET_EPOCH_ENTER(et);
1012 		CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
1013 			if (ifa->ifa_addr->sa_family != AF_LINK)
1014 				break;
1015 		}
1016 		NET_EPOCH_EXIT(et);
1017 
1018 		if (ifa == NULL)
1019 			break;
1020 #ifdef INET
1021 		/* XXX: Ugly!! ad hoc just for INET */
1022 		if (ifa->ifa_addr->sa_family == AF_INET) {
1023 			struct ifreq ifr;
1024 
1025 			bzero(&ifr, sizeof(ifr));
1026 			ifr.ifr_addr = *ifa->ifa_addr;
1027 			if (in_control(NULL, SIOCDIFADDR, (caddr_t)&ifr, ifp,
1028 			    NULL) == 0)
1029 				continue;
1030 		}
1031 #endif /* INET */
1032 #ifdef INET6
1033 		if (ifa->ifa_addr->sa_family == AF_INET6) {
1034 			in6_purgeifaddr((struct in6_ifaddr *)ifa);
1035 			/* ifp_addrhead is already updated */
1036 			continue;
1037 		}
1038 #endif /* INET6 */
1039 		IF_ADDR_WLOCK(ifp);
1040 		CK_STAILQ_REMOVE(&ifp->if_addrhead, ifa, ifaddr, ifa_link);
1041 		IF_ADDR_WUNLOCK(ifp);
1042 		ifa_free(ifa);
1043 	}
1044 }
1045 
1046 /*
1047  * Remove any multicast network addresses from an interface when an ifnet
1048  * is going away.
1049  */
1050 static void
if_purgemaddrs(struct ifnet * ifp)1051 if_purgemaddrs(struct ifnet *ifp)
1052 {
1053 	struct ifmultiaddr *ifma;
1054 
1055 	IF_ADDR_WLOCK(ifp);
1056 	while (!CK_STAILQ_EMPTY(&ifp->if_multiaddrs)) {
1057 		ifma = CK_STAILQ_FIRST(&ifp->if_multiaddrs);
1058 		CK_STAILQ_REMOVE(&ifp->if_multiaddrs, ifma, ifmultiaddr, ifma_link);
1059 		if_delmulti_locked(ifp, ifma, 1);
1060 	}
1061 	IF_ADDR_WUNLOCK(ifp);
1062 }
1063 
1064 /*
1065  * Detach an interface, removing it from the list of "active" interfaces.
1066  * If vmove flag is set on entry to if_detach_internal(), perform only a
1067  * limited subset of cleanup tasks, given that we are moving an ifnet from
1068  * one vnet to another, where it must be fully operational.
1069  *
1070  * XXXRW: There are some significant questions about event ordering, and
1071  * how to prevent things from starting to use the interface during detach.
1072  */
1073 void
if_detach(struct ifnet * ifp)1074 if_detach(struct ifnet *ifp)
1075 {
1076 	bool found;
1077 
1078 	CURVNET_SET_QUIET(ifp->if_vnet);
1079 	found = if_unlink_ifnet(ifp, false);
1080 	if (found) {
1081 		sx_xlock(&ifnet_detach_sxlock);
1082 		if_detach_internal(ifp, false);
1083 		sx_xunlock(&ifnet_detach_sxlock);
1084 	}
1085 	CURVNET_RESTORE();
1086 }
1087 
1088 /*
1089  * The vmove flag, if set, indicates that we are called from a callpath
1090  * that is moving an interface to a different vnet instance.
1091  *
1092  * The shutdown flag, if set, indicates that we are called in the
1093  * process of shutting down a vnet instance.  Currently only the
1094  * vnet_if_return SYSUNINIT function sets it.  Note: we can be called
1095  * on a vnet instance shutdown without this flag being set, e.g., when
1096  * the cloned interfaces are destoyed as first thing of teardown.
1097  */
1098 static void
if_detach_internal(struct ifnet * ifp,bool vmove)1099 if_detach_internal(struct ifnet *ifp, bool vmove)
1100 {
1101 	struct ifaddr *ifa;
1102 	int i;
1103 	struct domain *dp;
1104 	void *if_afdata[AF_MAX];
1105 #ifdef VIMAGE
1106 	bool shutdown;
1107 
1108 	shutdown = VNET_IS_SHUTTING_DOWN(ifp->if_vnet);
1109 #endif
1110 
1111 	sx_assert(&ifnet_detach_sxlock, SX_XLOCKED);
1112 
1113 	/*
1114 	 * At this point we know the interface still was on the ifnet list
1115 	 * and we removed it so we are in a stable state.
1116 	 */
1117 	NET_EPOCH_WAIT();
1118 
1119 	/*
1120 	 * Ensure all pending EPOCH(9) callbacks have been executed. This
1121 	 * fixes issues about late destruction of multicast options
1122 	 * which lead to leave group calls, which in turn access the
1123 	 * belonging ifnet structure:
1124 	 */
1125 	NET_EPOCH_DRAIN_CALLBACKS();
1126 
1127 	/*
1128 	 * In any case (destroy or vmove) detach us from the groups
1129 	 * and remove/wait for pending events on the taskq.
1130 	 * XXX-BZ in theory an interface could still enqueue a taskq change?
1131 	 */
1132 	if_delgroups(ifp);
1133 
1134 	taskqueue_drain(taskqueue_swi, &ifp->if_linktask);
1135 	taskqueue_drain(taskqueue_swi, &ifp->if_addmultitask);
1136 
1137 	if_down(ifp);
1138 
1139 #ifdef VIMAGE
1140 	/*
1141 	 * On VNET shutdown abort here as the stack teardown will do all
1142 	 * the work top-down for us.
1143 	 */
1144 	if (shutdown) {
1145 		/* Give interface users the chance to clean up. */
1146 		EVENTHANDLER_INVOKE(ifnet_departure_event, ifp);
1147 
1148 		/*
1149 		 * In case of a vmove we are done here without error.
1150 		 * If we would signal an error it would lead to the same
1151 		 * abort as if we did not find the ifnet anymore.
1152 		 * if_detach() calls us in void context and does not care
1153 		 * about an early abort notification, so life is splendid :)
1154 		 */
1155 		goto finish_vnet_shutdown;
1156 	}
1157 #endif
1158 
1159 	/*
1160 	 * At this point we are not tearing down a VNET and are either
1161 	 * going to destroy or vmove the interface and have to cleanup
1162 	 * accordingly.
1163 	 */
1164 
1165 	/*
1166 	 * Remove routes and flush queues.
1167 	 */
1168 #ifdef ALTQ
1169 	if (ALTQ_IS_ENABLED(&ifp->if_snd))
1170 		altq_disable(&ifp->if_snd);
1171 	if (ALTQ_IS_ATTACHED(&ifp->if_snd))
1172 		altq_detach(&ifp->if_snd);
1173 #endif
1174 
1175 	if_purgeaddrs(ifp);
1176 
1177 #ifdef INET
1178 	in_ifdetach(ifp);
1179 #endif
1180 
1181 #ifdef INET6
1182 	/*
1183 	 * Remove all IPv6 kernel structs related to ifp.  This should be done
1184 	 * before removing routing entries below, since IPv6 interface direct
1185 	 * routes are expected to be removed by the IPv6-specific kernel API.
1186 	 * Otherwise, the kernel will detect some inconsistency and bark it.
1187 	 */
1188 	in6_ifdetach(ifp);
1189 #endif
1190 	if_purgemaddrs(ifp);
1191 
1192 	EVENTHANDLER_INVOKE(ifnet_departure_event, ifp);
1193 	if (IS_DEFAULT_VNET(curvnet))
1194 		devctl_notify("IFNET", ifp->if_xname, "DETACH", NULL);
1195 
1196 	if (!vmove) {
1197 		/*
1198 		 * Prevent further calls into the device driver via ifnet.
1199 		 */
1200 		if_dead(ifp);
1201 
1202 		/*
1203 		 * Clean up all addresses.
1204 		 */
1205 		IF_ADDR_WLOCK(ifp);
1206 		if (!CK_STAILQ_EMPTY(&ifp->if_addrhead)) {
1207 			ifa = CK_STAILQ_FIRST(&ifp->if_addrhead);
1208 			CK_STAILQ_REMOVE(&ifp->if_addrhead, ifa, ifaddr, ifa_link);
1209 			IF_ADDR_WUNLOCK(ifp);
1210 			ifa_free(ifa);
1211 		} else
1212 			IF_ADDR_WUNLOCK(ifp);
1213 	}
1214 
1215 	rt_flushifroutes(ifp);
1216 
1217 #ifdef VIMAGE
1218 finish_vnet_shutdown:
1219 #endif
1220 	/*
1221 	 * We cannot hold the lock over dom_ifdetach calls as they might
1222 	 * sleep, for example trying to drain a callout, thus open up the
1223 	 * theoretical race with re-attaching.
1224 	 */
1225 	IF_AFDATA_LOCK(ifp);
1226 	i = ifp->if_afdata_initialized;
1227 	ifp->if_afdata_initialized = 0;
1228 	if (i != 0) {
1229 		/*
1230 		 * Defer the dom_ifdetach call.
1231 		 */
1232 		_Static_assert(sizeof(if_afdata) == sizeof(ifp->if_afdata),
1233 		    "array size mismatch");
1234 		memcpy(if_afdata, ifp->if_afdata, sizeof(if_afdata));
1235 		memset(ifp->if_afdata, 0, sizeof(ifp->if_afdata));
1236 	}
1237 	IF_AFDATA_UNLOCK(ifp);
1238 	if (i == 0)
1239 		return;
1240 	/*
1241 	 * XXXZL: This net epoch wait is not necessary if we have done right.
1242 	 * But if we do not, at least we can make a guarantee that threads those
1243 	 * enter net epoch will see NULL address family dependent data,
1244 	 * e.g. if_afdata[AF_INET6]. A clear NULL pointer derefence is much
1245 	 * better than writing to freed memory.
1246 	 */
1247 	NET_EPOCH_WAIT();
1248 	SLIST_FOREACH(dp, &domains, dom_next) {
1249 		if (dp->dom_ifdetach != NULL &&
1250 		    if_afdata[dp->dom_family] != NULL)
1251 			(*dp->dom_ifdetach)(ifp, if_afdata[dp->dom_family]);
1252 	}
1253 }
1254 
1255 #ifdef VIMAGE
1256 /*
1257  * if_vmove() performs a limited version of if_detach() in current
1258  * vnet and if_attach()es the ifnet to the vnet specified as 2nd arg.
1259  */
1260 static void
if_vmove(struct ifnet * ifp,struct vnet * new_vnet)1261 if_vmove(struct ifnet *ifp, struct vnet *new_vnet)
1262 {
1263 #ifdef DEV_BPF
1264 	/*
1265 	 * Detach BPF file descriptors from its interface.
1266 	 */
1267 	bpf_ifdetach(ifp);
1268 #endif
1269 
1270 	/*
1271 	 * Detach from current vnet, but preserve LLADDR info, do not
1272 	 * mark as dead etc. so that the ifnet can be reattached later.
1273 	 */
1274 	if_detach_internal(ifp, true);
1275 
1276 	/*
1277 	 * Perform interface-specific reassignment tasks, if provided by
1278 	 * the driver.
1279 	 */
1280 	if (ifp->if_reassign != NULL)
1281 		ifp->if_reassign(ifp, new_vnet, NULL);
1282 
1283 	/*
1284 	 * Switch to the context of the target vnet.
1285 	 */
1286 	CURVNET_SET_QUIET(new_vnet);
1287 	if_attach_internal(ifp, true);
1288 	CURVNET_RESTORE();
1289 }
1290 
1291 /*
1292  * Move an ifnet to or from another child prison/vnet, specified by the jail id.
1293  */
1294 static int
if_vmove_loan(struct thread * td,struct ifnet * ifp,char * ifname,int jid)1295 if_vmove_loan(struct thread *td, struct ifnet *ifp, char *ifname, int jid)
1296 {
1297 	struct prison *pr;
1298 	struct ifnet *difp;
1299 	bool found;
1300 	bool shutdown;
1301 
1302 	MPASS(ifindex_table[ifp->if_index].ife_ifnet == ifp);
1303 
1304 	/* Try to find the prison within our visibility. */
1305 	sx_slock(&allprison_lock);
1306 	pr = prison_find_child(td->td_ucred->cr_prison, jid);
1307 	sx_sunlock(&allprison_lock);
1308 	if (pr == NULL)
1309 		return (ENXIO);
1310 	prison_hold_locked(pr);
1311 	mtx_unlock(&pr->pr_mtx);
1312 
1313 	/* Do not try to move the iface from and to the same prison. */
1314 	if (pr->pr_vnet == ifp->if_vnet) {
1315 		prison_free(pr);
1316 		return (EEXIST);
1317 	}
1318 
1319 	/* Make sure the named iface does not exists in the dst. prison/vnet. */
1320 	/* XXX Lock interfaces to avoid races. */
1321 	CURVNET_SET_QUIET(pr->pr_vnet);
1322 	difp = ifunit(ifname);
1323 	CURVNET_RESTORE();
1324 	if (difp != NULL) {
1325 		prison_free(pr);
1326 		return (EEXIST);
1327 	}
1328 	sx_xlock(&ifnet_detach_sxlock);
1329 
1330 	/* Make sure the VNET is stable. */
1331 	shutdown = VNET_IS_SHUTTING_DOWN(ifp->if_vnet);
1332 	if (shutdown) {
1333 		sx_xunlock(&ifnet_detach_sxlock);
1334 		prison_free(pr);
1335 		return (EBUSY);
1336 	}
1337 
1338 	found = if_unlink_ifnet(ifp, true);
1339 	if (! found) {
1340 		sx_xunlock(&ifnet_detach_sxlock);
1341 		prison_free(pr);
1342 		return (ENODEV);
1343 	}
1344 
1345 	/* Move the interface into the child jail/vnet. */
1346 	if_vmove(ifp, pr->pr_vnet);
1347 
1348 	/* Report the new if_xname back to the userland. */
1349 	sprintf(ifname, "%s", ifp->if_xname);
1350 
1351 	sx_xunlock(&ifnet_detach_sxlock);
1352 
1353 	prison_free(pr);
1354 	return (0);
1355 }
1356 
1357 static int
if_vmove_reclaim(struct thread * td,char * ifname,int jid)1358 if_vmove_reclaim(struct thread *td, char *ifname, int jid)
1359 {
1360 	struct prison *pr;
1361 	struct vnet *vnet_dst;
1362 	struct ifnet *ifp;
1363 	int found __diagused;
1364  	bool shutdown;
1365 
1366 	/* Try to find the prison within our visibility. */
1367 	sx_slock(&allprison_lock);
1368 	pr = prison_find_child(td->td_ucred->cr_prison, jid);
1369 	sx_sunlock(&allprison_lock);
1370 	if (pr == NULL)
1371 		return (ENXIO);
1372 	prison_hold_locked(pr);
1373 	mtx_unlock(&pr->pr_mtx);
1374 
1375 	/* Make sure the named iface exists in the source prison/vnet. */
1376 	CURVNET_SET(pr->pr_vnet);
1377 	ifp = ifunit(ifname);		/* XXX Lock to avoid races. */
1378 	if (ifp == NULL) {
1379 		CURVNET_RESTORE();
1380 		prison_free(pr);
1381 		return (ENXIO);
1382 	}
1383 
1384 	/* Do not try to move the iface from and to the same prison. */
1385 	vnet_dst = TD_TO_VNET(td);
1386 	if (vnet_dst == ifp->if_vnet) {
1387 		CURVNET_RESTORE();
1388 		prison_free(pr);
1389 		return (EEXIST);
1390 	}
1391 
1392 	/* Make sure the VNET is stable. */
1393 	shutdown = VNET_IS_SHUTTING_DOWN(ifp->if_vnet);
1394 	if (shutdown) {
1395 		CURVNET_RESTORE();
1396 		prison_free(pr);
1397 		return (EBUSY);
1398 	}
1399 
1400 	/* Get interface back from child jail/vnet. */
1401 	found = if_unlink_ifnet(ifp, true);
1402 	MPASS(found);
1403 	sx_xlock(&ifnet_detach_sxlock);
1404 	if_vmove(ifp, vnet_dst);
1405 	sx_xunlock(&ifnet_detach_sxlock);
1406 	CURVNET_RESTORE();
1407 
1408 	/* Report the new if_xname back to the userland. */
1409 	sprintf(ifname, "%s", ifp->if_xname);
1410 
1411 	prison_free(pr);
1412 	return (0);
1413 }
1414 #endif /* VIMAGE */
1415 
1416 /*
1417  * Add a group to an interface
1418  */
1419 int
if_addgroup(struct ifnet * ifp,const char * groupname)1420 if_addgroup(struct ifnet *ifp, const char *groupname)
1421 {
1422 	struct ifg_list		*ifgl;
1423 	struct ifg_group	*ifg = NULL;
1424 	struct ifg_member	*ifgm;
1425 	int 			 new = 0;
1426 
1427 	if (groupname[0] && groupname[strlen(groupname) - 1] >= '0' &&
1428 	    groupname[strlen(groupname) - 1] <= '9')
1429 		return (EINVAL);
1430 
1431 	IFNET_WLOCK();
1432 	CK_STAILQ_FOREACH(ifgl, &ifp->if_groups, ifgl_next)
1433 		if (!strcmp(ifgl->ifgl_group->ifg_group, groupname)) {
1434 			IFNET_WUNLOCK();
1435 			return (EEXIST);
1436 		}
1437 
1438 	if ((ifgl = malloc(sizeof(*ifgl), M_TEMP, M_NOWAIT)) == NULL) {
1439 	    	IFNET_WUNLOCK();
1440 		return (ENOMEM);
1441 	}
1442 
1443 	if ((ifgm = malloc(sizeof(*ifgm), M_TEMP, M_NOWAIT)) == NULL) {
1444 		free(ifgl, M_TEMP);
1445 		IFNET_WUNLOCK();
1446 		return (ENOMEM);
1447 	}
1448 
1449 	CK_STAILQ_FOREACH(ifg, &V_ifg_head, ifg_next)
1450 		if (!strcmp(ifg->ifg_group, groupname))
1451 			break;
1452 
1453 	if (ifg == NULL) {
1454 		if ((ifg = malloc(sizeof(*ifg), M_TEMP, M_NOWAIT)) == NULL) {
1455 			free(ifgl, M_TEMP);
1456 			free(ifgm, M_TEMP);
1457 			IFNET_WUNLOCK();
1458 			return (ENOMEM);
1459 		}
1460 		strlcpy(ifg->ifg_group, groupname, sizeof(ifg->ifg_group));
1461 		ifg->ifg_refcnt = 0;
1462 		CK_STAILQ_INIT(&ifg->ifg_members);
1463 		CK_STAILQ_INSERT_TAIL(&V_ifg_head, ifg, ifg_next);
1464 		new = 1;
1465 	}
1466 
1467 	ifg->ifg_refcnt++;
1468 	ifgl->ifgl_group = ifg;
1469 	ifgm->ifgm_ifp = ifp;
1470 
1471 	IF_ADDR_WLOCK(ifp);
1472 	CK_STAILQ_INSERT_TAIL(&ifg->ifg_members, ifgm, ifgm_next);
1473 	CK_STAILQ_INSERT_TAIL(&ifp->if_groups, ifgl, ifgl_next);
1474 	IF_ADDR_WUNLOCK(ifp);
1475 
1476 	IFNET_WUNLOCK();
1477 
1478 	if (new)
1479 		EVENTHANDLER_INVOKE(group_attach_event, ifg);
1480 	EVENTHANDLER_INVOKE(group_change_event, groupname);
1481 
1482 	return (0);
1483 }
1484 
1485 /*
1486  * Helper function to remove a group out of an interface.  Expects the global
1487  * ifnet lock to be write-locked, and drops it before returning.
1488  */
1489 static void
_if_delgroup_locked(struct ifnet * ifp,struct ifg_list * ifgl,const char * groupname)1490 _if_delgroup_locked(struct ifnet *ifp, struct ifg_list *ifgl,
1491     const char *groupname)
1492 {
1493 	struct ifg_member *ifgm;
1494 	bool freeifgl;
1495 
1496 	IFNET_WLOCK_ASSERT();
1497 
1498 	IF_ADDR_WLOCK(ifp);
1499 	CK_STAILQ_REMOVE(&ifp->if_groups, ifgl, ifg_list, ifgl_next);
1500 	IF_ADDR_WUNLOCK(ifp);
1501 
1502 	CK_STAILQ_FOREACH(ifgm, &ifgl->ifgl_group->ifg_members, ifgm_next) {
1503 		if (ifgm->ifgm_ifp == ifp) {
1504 			CK_STAILQ_REMOVE(&ifgl->ifgl_group->ifg_members, ifgm,
1505 			    ifg_member, ifgm_next);
1506 			break;
1507 		}
1508 	}
1509 
1510 	if (--ifgl->ifgl_group->ifg_refcnt == 0) {
1511 		CK_STAILQ_REMOVE(&V_ifg_head, ifgl->ifgl_group, ifg_group,
1512 		    ifg_next);
1513 		freeifgl = true;
1514 	} else {
1515 		freeifgl = false;
1516 	}
1517 	IFNET_WUNLOCK();
1518 
1519 	NET_EPOCH_WAIT();
1520 	EVENTHANDLER_INVOKE(group_change_event, groupname);
1521 	if (freeifgl) {
1522 		EVENTHANDLER_INVOKE(group_detach_event, ifgl->ifgl_group);
1523 		free(ifgl->ifgl_group, M_TEMP);
1524 	}
1525 	free(ifgm, M_TEMP);
1526 	free(ifgl, M_TEMP);
1527 }
1528 
1529 /*
1530  * Remove a group from an interface
1531  */
1532 int
if_delgroup(struct ifnet * ifp,const char * groupname)1533 if_delgroup(struct ifnet *ifp, const char *groupname)
1534 {
1535 	struct ifg_list *ifgl;
1536 
1537 	IFNET_WLOCK();
1538 	CK_STAILQ_FOREACH(ifgl, &ifp->if_groups, ifgl_next)
1539 		if (strcmp(ifgl->ifgl_group->ifg_group, groupname) == 0)
1540 			break;
1541 	if (ifgl == NULL) {
1542 		IFNET_WUNLOCK();
1543 		return (ENOENT);
1544 	}
1545 
1546 	_if_delgroup_locked(ifp, ifgl, groupname);
1547 
1548 	return (0);
1549 }
1550 
1551 /*
1552  * Remove an interface from all groups
1553  */
1554 static void
if_delgroups(struct ifnet * ifp)1555 if_delgroups(struct ifnet *ifp)
1556 {
1557 	struct ifg_list *ifgl;
1558 	char groupname[IFNAMSIZ];
1559 
1560 	IFNET_WLOCK();
1561 	while ((ifgl = CK_STAILQ_FIRST(&ifp->if_groups)) != NULL) {
1562 		strlcpy(groupname, ifgl->ifgl_group->ifg_group, IFNAMSIZ);
1563 		_if_delgroup_locked(ifp, ifgl, groupname);
1564 		IFNET_WLOCK();
1565 	}
1566 	IFNET_WUNLOCK();
1567 }
1568 
1569 /*
1570  * Stores all groups from an interface in memory pointed to by ifgr.
1571  */
1572 static int
if_getgroup(struct ifgroupreq * ifgr,struct ifnet * ifp)1573 if_getgroup(struct ifgroupreq *ifgr, struct ifnet *ifp)
1574 {
1575 	int			 len, error;
1576 	struct ifg_list		*ifgl;
1577 	struct ifg_req		 ifgrq, *ifgp;
1578 
1579 	NET_EPOCH_ASSERT();
1580 
1581 	if (ifgr->ifgr_len == 0) {
1582 		CK_STAILQ_FOREACH(ifgl, &ifp->if_groups, ifgl_next)
1583 			ifgr->ifgr_len += sizeof(struct ifg_req);
1584 		return (0);
1585 	}
1586 
1587 	len = ifgr->ifgr_len;
1588 	ifgp = ifgr->ifgr_groups;
1589 	/* XXX: wire */
1590 	CK_STAILQ_FOREACH(ifgl, &ifp->if_groups, ifgl_next) {
1591 		if (len < sizeof(ifgrq))
1592 			return (EINVAL);
1593 		bzero(&ifgrq, sizeof ifgrq);
1594 		strlcpy(ifgrq.ifgrq_group, ifgl->ifgl_group->ifg_group,
1595 		    sizeof(ifgrq.ifgrq_group));
1596 		if ((error = copyout(&ifgrq, ifgp, sizeof(struct ifg_req))))
1597 			return (error);
1598 		len -= sizeof(ifgrq);
1599 		ifgp++;
1600 	}
1601 
1602 	return (0);
1603 }
1604 
1605 /*
1606  * Stores all members of a group in memory pointed to by igfr
1607  */
1608 static int
if_getgroupmembers(struct ifgroupreq * ifgr)1609 if_getgroupmembers(struct ifgroupreq *ifgr)
1610 {
1611 	struct ifg_group	*ifg;
1612 	struct ifg_member	*ifgm;
1613 	struct ifg_req		 ifgrq, *ifgp;
1614 	int			 len, error;
1615 
1616 	IFNET_RLOCK();
1617 	CK_STAILQ_FOREACH(ifg, &V_ifg_head, ifg_next)
1618 		if (strcmp(ifg->ifg_group, ifgr->ifgr_name) == 0)
1619 			break;
1620 	if (ifg == NULL) {
1621 		IFNET_RUNLOCK();
1622 		return (ENOENT);
1623 	}
1624 
1625 	if (ifgr->ifgr_len == 0) {
1626 		CK_STAILQ_FOREACH(ifgm, &ifg->ifg_members, ifgm_next)
1627 			ifgr->ifgr_len += sizeof(ifgrq);
1628 		IFNET_RUNLOCK();
1629 		return (0);
1630 	}
1631 
1632 	len = ifgr->ifgr_len;
1633 	ifgp = ifgr->ifgr_groups;
1634 	CK_STAILQ_FOREACH(ifgm, &ifg->ifg_members, ifgm_next) {
1635 		if (len < sizeof(ifgrq)) {
1636 			IFNET_RUNLOCK();
1637 			return (EINVAL);
1638 		}
1639 		bzero(&ifgrq, sizeof ifgrq);
1640 		strlcpy(ifgrq.ifgrq_member, ifgm->ifgm_ifp->if_xname,
1641 		    sizeof(ifgrq.ifgrq_member));
1642 		if ((error = copyout(&ifgrq, ifgp, sizeof(struct ifg_req)))) {
1643 			IFNET_RUNLOCK();
1644 			return (error);
1645 		}
1646 		len -= sizeof(ifgrq);
1647 		ifgp++;
1648 	}
1649 	IFNET_RUNLOCK();
1650 
1651 	return (0);
1652 }
1653 
1654 /*
1655  * Return counter values from counter(9)s stored in ifnet.
1656  */
1657 uint64_t
if_get_counter_default(struct ifnet * ifp,ift_counter cnt)1658 if_get_counter_default(struct ifnet *ifp, ift_counter cnt)
1659 {
1660 
1661 	KASSERT(cnt < IFCOUNTERS, ("%s: invalid cnt %d", __func__, cnt));
1662 
1663 	return (counter_u64_fetch(ifp->if_counters[cnt]));
1664 }
1665 
1666 /*
1667  * Increase an ifnet counter. Usually used for counters shared
1668  * between the stack and a driver, but function supports them all.
1669  */
1670 void
if_inc_counter(struct ifnet * ifp,ift_counter cnt,int64_t inc)1671 if_inc_counter(struct ifnet *ifp, ift_counter cnt, int64_t inc)
1672 {
1673 
1674 	KASSERT(cnt < IFCOUNTERS, ("%s: invalid cnt %d", __func__, cnt));
1675 
1676 	counter_u64_add(ifp->if_counters[cnt], inc);
1677 }
1678 
1679 /*
1680  * Copy data from ifnet to userland API structure if_data.
1681  */
1682 void
if_data_copy(struct ifnet * ifp,struct if_data * ifd)1683 if_data_copy(struct ifnet *ifp, struct if_data *ifd)
1684 {
1685 
1686 	ifd->ifi_type = ifp->if_type;
1687 	ifd->ifi_physical = 0;
1688 	ifd->ifi_addrlen = ifp->if_addrlen;
1689 	ifd->ifi_hdrlen = ifp->if_hdrlen;
1690 	ifd->ifi_link_state = ifp->if_link_state;
1691 	ifd->ifi_vhid = 0;
1692 	ifd->ifi_datalen = sizeof(struct if_data);
1693 	ifd->ifi_mtu = ifp->if_mtu;
1694 	ifd->ifi_metric = ifp->if_metric;
1695 	ifd->ifi_baudrate = ifp->if_baudrate;
1696 	ifd->ifi_hwassist = ifp->if_hwassist;
1697 	ifd->ifi_epoch = ifp->if_epoch;
1698 	ifd->ifi_lastchange = ifp->if_lastchange;
1699 
1700 	ifd->ifi_ipackets = ifp->if_get_counter(ifp, IFCOUNTER_IPACKETS);
1701 	ifd->ifi_ierrors = ifp->if_get_counter(ifp, IFCOUNTER_IERRORS);
1702 	ifd->ifi_opackets = ifp->if_get_counter(ifp, IFCOUNTER_OPACKETS);
1703 	ifd->ifi_oerrors = ifp->if_get_counter(ifp, IFCOUNTER_OERRORS);
1704 	ifd->ifi_collisions = ifp->if_get_counter(ifp, IFCOUNTER_COLLISIONS);
1705 	ifd->ifi_ibytes = ifp->if_get_counter(ifp, IFCOUNTER_IBYTES);
1706 	ifd->ifi_obytes = ifp->if_get_counter(ifp, IFCOUNTER_OBYTES);
1707 	ifd->ifi_imcasts = ifp->if_get_counter(ifp, IFCOUNTER_IMCASTS);
1708 	ifd->ifi_omcasts = ifp->if_get_counter(ifp, IFCOUNTER_OMCASTS);
1709 	ifd->ifi_iqdrops = ifp->if_get_counter(ifp, IFCOUNTER_IQDROPS);
1710 	ifd->ifi_oqdrops = ifp->if_get_counter(ifp, IFCOUNTER_OQDROPS);
1711 	ifd->ifi_noproto = ifp->if_get_counter(ifp, IFCOUNTER_NOPROTO);
1712 }
1713 
1714 /*
1715  * Initialization, destruction and refcounting functions for ifaddrs.
1716  */
1717 struct ifaddr *
ifa_alloc(size_t size,int flags)1718 ifa_alloc(size_t size, int flags)
1719 {
1720 	struct ifaddr *ifa;
1721 
1722 	KASSERT(size >= sizeof(struct ifaddr),
1723 	    ("%s: invalid size %zu", __func__, size));
1724 
1725 	ifa = malloc(size, M_IFADDR, M_ZERO | flags);
1726 	if (ifa == NULL)
1727 		return (NULL);
1728 
1729 	if ((ifa->ifa_opackets = counter_u64_alloc(flags)) == NULL)
1730 		goto fail;
1731 	if ((ifa->ifa_ipackets = counter_u64_alloc(flags)) == NULL)
1732 		goto fail;
1733 	if ((ifa->ifa_obytes = counter_u64_alloc(flags)) == NULL)
1734 		goto fail;
1735 	if ((ifa->ifa_ibytes = counter_u64_alloc(flags)) == NULL)
1736 		goto fail;
1737 
1738 	refcount_init(&ifa->ifa_refcnt, 1);
1739 
1740 	return (ifa);
1741 
1742 fail:
1743 	/* free(NULL) is okay */
1744 	counter_u64_free(ifa->ifa_opackets);
1745 	counter_u64_free(ifa->ifa_ipackets);
1746 	counter_u64_free(ifa->ifa_obytes);
1747 	counter_u64_free(ifa->ifa_ibytes);
1748 	free(ifa, M_IFADDR);
1749 
1750 	return (NULL);
1751 }
1752 
1753 void
ifa_ref(struct ifaddr * ifa)1754 ifa_ref(struct ifaddr *ifa)
1755 {
1756 	u_int old __diagused;
1757 
1758 	old = refcount_acquire(&ifa->ifa_refcnt);
1759 	KASSERT(old > 0, ("%s: ifa %p has 0 refs", __func__, ifa));
1760 }
1761 
1762 int
ifa_try_ref(struct ifaddr * ifa)1763 ifa_try_ref(struct ifaddr *ifa)
1764 {
1765 
1766 	NET_EPOCH_ASSERT();
1767 	return (refcount_acquire_if_not_zero(&ifa->ifa_refcnt));
1768 }
1769 
1770 static void
ifa_destroy(epoch_context_t ctx)1771 ifa_destroy(epoch_context_t ctx)
1772 {
1773 	struct ifaddr *ifa;
1774 
1775 	ifa = __containerof(ctx, struct ifaddr, ifa_epoch_ctx);
1776 	counter_u64_free(ifa->ifa_opackets);
1777 	counter_u64_free(ifa->ifa_ipackets);
1778 	counter_u64_free(ifa->ifa_obytes);
1779 	counter_u64_free(ifa->ifa_ibytes);
1780 	free(ifa, M_IFADDR);
1781 }
1782 
1783 void
ifa_free(struct ifaddr * ifa)1784 ifa_free(struct ifaddr *ifa)
1785 {
1786 
1787 	if (refcount_release(&ifa->ifa_refcnt))
1788 		NET_EPOCH_CALL(ifa_destroy, &ifa->ifa_epoch_ctx);
1789 }
1790 
1791 /*
1792  * XXX: Because sockaddr_dl has deeper structure than the sockaddr
1793  * structs used to represent other address families, it is necessary
1794  * to perform a different comparison.
1795  */
1796 static bool
sa_dl_equal(const struct sockaddr * a,const struct sockaddr * b)1797 sa_dl_equal(const struct sockaddr *a, const struct sockaddr *b)
1798 {
1799 	const struct sockaddr_dl *sdl1 = (const struct sockaddr_dl *)a;
1800 	const struct sockaddr_dl *sdl2 = (const struct sockaddr_dl *)b;
1801 
1802 	return (sdl1->sdl_len == sdl2->sdl_len &&
1803 	    bcmp(sdl1->sdl_data + sdl1->sdl_nlen,
1804 	    sdl2->sdl_data + sdl2->sdl_nlen, sdl1->sdl_alen) == 0);
1805 }
1806 
1807 /*
1808  * Locate an interface based on a complete address.
1809  */
1810 /*ARGSUSED*/
1811 struct ifaddr *
ifa_ifwithaddr(const struct sockaddr * addr)1812 ifa_ifwithaddr(const struct sockaddr *addr)
1813 {
1814 	struct ifnet *ifp;
1815 	struct ifaddr *ifa;
1816 
1817 	NET_EPOCH_ASSERT();
1818 
1819 	CK_STAILQ_FOREACH(ifp, &V_ifnet, if_link) {
1820 		CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
1821 			if (ifa->ifa_addr->sa_family != addr->sa_family)
1822 				continue;
1823 			if (sa_equal(addr, ifa->ifa_addr)) {
1824 				goto done;
1825 			}
1826 			/* IP6 doesn't have broadcast */
1827 			if ((ifp->if_flags & IFF_BROADCAST) &&
1828 			    ifa->ifa_broadaddr &&
1829 			    ifa->ifa_broadaddr->sa_len != 0 &&
1830 			    sa_equal(ifa->ifa_broadaddr, addr)) {
1831 				goto done;
1832 			}
1833 		}
1834 	}
1835 	ifa = NULL;
1836 done:
1837 	return (ifa);
1838 }
1839 
1840 int
ifa_ifwithaddr_check(const struct sockaddr * addr)1841 ifa_ifwithaddr_check(const struct sockaddr *addr)
1842 {
1843 	struct epoch_tracker et;
1844 	int rc;
1845 
1846 	NET_EPOCH_ENTER(et);
1847 	rc = (ifa_ifwithaddr(addr) != NULL);
1848 	NET_EPOCH_EXIT(et);
1849 	return (rc);
1850 }
1851 
1852 /*
1853  * Locate an interface based on the broadcast address.
1854  */
1855 /* ARGSUSED */
1856 struct ifaddr *
ifa_ifwithbroadaddr(const struct sockaddr * addr,int fibnum)1857 ifa_ifwithbroadaddr(const struct sockaddr *addr, int fibnum)
1858 {
1859 	struct ifnet *ifp;
1860 	struct ifaddr *ifa;
1861 
1862 	NET_EPOCH_ASSERT();
1863 	CK_STAILQ_FOREACH(ifp, &V_ifnet, if_link) {
1864 		if ((fibnum != RT_ALL_FIBS) && (ifp->if_fib != fibnum))
1865 			continue;
1866 		CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
1867 			if (ifa->ifa_addr->sa_family != addr->sa_family)
1868 				continue;
1869 			if ((ifp->if_flags & IFF_BROADCAST) &&
1870 			    ifa->ifa_broadaddr &&
1871 			    ifa->ifa_broadaddr->sa_len != 0 &&
1872 			    sa_equal(ifa->ifa_broadaddr, addr)) {
1873 				goto done;
1874 			}
1875 		}
1876 	}
1877 	ifa = NULL;
1878 done:
1879 	return (ifa);
1880 }
1881 
1882 /*
1883  * Locate the point to point interface with a given destination address.
1884  */
1885 /*ARGSUSED*/
1886 struct ifaddr *
ifa_ifwithdstaddr(const struct sockaddr * addr,int fibnum)1887 ifa_ifwithdstaddr(const struct sockaddr *addr, int fibnum)
1888 {
1889 	struct ifnet *ifp;
1890 	struct ifaddr *ifa;
1891 
1892 	NET_EPOCH_ASSERT();
1893 	CK_STAILQ_FOREACH(ifp, &V_ifnet, if_link) {
1894 		if ((ifp->if_flags & IFF_POINTOPOINT) == 0)
1895 			continue;
1896 		if ((fibnum != RT_ALL_FIBS) && (ifp->if_fib != fibnum))
1897 			continue;
1898 		CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
1899 			if (ifa->ifa_addr->sa_family != addr->sa_family)
1900 				continue;
1901 			if (ifa->ifa_dstaddr != NULL &&
1902 			    sa_equal(addr, ifa->ifa_dstaddr)) {
1903 				goto done;
1904 			}
1905 		}
1906 	}
1907 	ifa = NULL;
1908 done:
1909 	return (ifa);
1910 }
1911 
1912 /*
1913  * Find an interface on a specific network.  If many, choice
1914  * is most specific found.
1915  */
1916 struct ifaddr *
ifa_ifwithnet(const struct sockaddr * addr,int ignore_ptp,int fibnum)1917 ifa_ifwithnet(const struct sockaddr *addr, int ignore_ptp, int fibnum)
1918 {
1919 	struct ifnet *ifp;
1920 	struct ifaddr *ifa;
1921 	struct ifaddr *ifa_maybe = NULL;
1922 	u_int af = addr->sa_family;
1923 	const char *addr_data = addr->sa_data, *cplim;
1924 
1925 	NET_EPOCH_ASSERT();
1926 	/*
1927 	 * AF_LINK addresses can be looked up directly by their index number,
1928 	 * so do that if we can.
1929 	 */
1930 	if (af == AF_LINK) {
1931 		ifp = ifnet_byindex(
1932 		    ((const struct sockaddr_dl *)addr)->sdl_index);
1933 		return (ifp ? ifp->if_addr : NULL);
1934 	}
1935 
1936 	/*
1937 	 * Scan though each interface, looking for ones that have addresses
1938 	 * in this address family and the requested fib.
1939 	 */
1940 	CK_STAILQ_FOREACH(ifp, &V_ifnet, if_link) {
1941 		if ((fibnum != RT_ALL_FIBS) && (ifp->if_fib != fibnum))
1942 			continue;
1943 		CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
1944 			const char *cp, *cp2, *cp3;
1945 
1946 			if (ifa->ifa_addr->sa_family != af)
1947 next:				continue;
1948 			if (af == AF_INET &&
1949 			    ifp->if_flags & IFF_POINTOPOINT && !ignore_ptp) {
1950 				/*
1951 				 * This is a bit broken as it doesn't
1952 				 * take into account that the remote end may
1953 				 * be a single node in the network we are
1954 				 * looking for.
1955 				 * The trouble is that we don't know the
1956 				 * netmask for the remote end.
1957 				 */
1958 				if (ifa->ifa_dstaddr != NULL &&
1959 				    sa_equal(addr, ifa->ifa_dstaddr)) {
1960 					goto done;
1961 				}
1962 			} else {
1963 				/*
1964 				 * Scan all the bits in the ifa's address.
1965 				 * If a bit dissagrees with what we are
1966 				 * looking for, mask it with the netmask
1967 				 * to see if it really matters.
1968 				 * (A byte at a time)
1969 				 */
1970 				if (ifa->ifa_netmask == 0)
1971 					continue;
1972 				cp = addr_data;
1973 				cp2 = ifa->ifa_addr->sa_data;
1974 				cp3 = ifa->ifa_netmask->sa_data;
1975 				cplim = ifa->ifa_netmask->sa_len
1976 					+ (char *)ifa->ifa_netmask;
1977 				while (cp3 < cplim)
1978 					if ((*cp++ ^ *cp2++) & *cp3++)
1979 						goto next; /* next address! */
1980 				/*
1981 				 * If the netmask of what we just found
1982 				 * is more specific than what we had before
1983 				 * (if we had one), or if the virtual status
1984 				 * of new prefix is better than of the old one,
1985 				 * then remember the new one before continuing
1986 				 * to search for an even better one.
1987 				 */
1988 				if (ifa_maybe == NULL ||
1989 				    ifa_preferred(ifa_maybe, ifa) ||
1990 				    rn_refines((caddr_t)ifa->ifa_netmask,
1991 				    (caddr_t)ifa_maybe->ifa_netmask)) {
1992 					ifa_maybe = ifa;
1993 				}
1994 			}
1995 		}
1996 	}
1997 	ifa = ifa_maybe;
1998 	ifa_maybe = NULL;
1999 done:
2000 	return (ifa);
2001 }
2002 
2003 /*
2004  * Find an interface address specific to an interface best matching
2005  * a given address.
2006  */
2007 struct ifaddr *
ifaof_ifpforaddr(const struct sockaddr * addr,struct ifnet * ifp)2008 ifaof_ifpforaddr(const struct sockaddr *addr, struct ifnet *ifp)
2009 {
2010 	struct ifaddr *ifa;
2011 	const char *cp, *cp2, *cp3;
2012 	char *cplim;
2013 	struct ifaddr *ifa_maybe = NULL;
2014 	u_int af = addr->sa_family;
2015 
2016 	if (af >= AF_MAX)
2017 		return (NULL);
2018 
2019 	NET_EPOCH_ASSERT();
2020 	CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
2021 		if (ifa->ifa_addr->sa_family != af)
2022 			continue;
2023 		if (ifa_maybe == NULL)
2024 			ifa_maybe = ifa;
2025 		if (ifa->ifa_netmask == 0) {
2026 			if (sa_equal(addr, ifa->ifa_addr) ||
2027 			    (ifa->ifa_dstaddr &&
2028 			    sa_equal(addr, ifa->ifa_dstaddr)))
2029 				goto done;
2030 			continue;
2031 		}
2032 		if (ifp->if_flags & IFF_POINTOPOINT) {
2033 			if (ifa->ifa_dstaddr && sa_equal(addr, ifa->ifa_dstaddr))
2034 				goto done;
2035 		} else {
2036 			cp = addr->sa_data;
2037 			cp2 = ifa->ifa_addr->sa_data;
2038 			cp3 = ifa->ifa_netmask->sa_data;
2039 			cplim = ifa->ifa_netmask->sa_len + (char *)ifa->ifa_netmask;
2040 			for (; cp3 < cplim; cp3++)
2041 				if ((*cp++ ^ *cp2++) & *cp3)
2042 					break;
2043 			if (cp3 == cplim)
2044 				goto done;
2045 		}
2046 	}
2047 	ifa = ifa_maybe;
2048 done:
2049 	return (ifa);
2050 }
2051 
2052 /*
2053  * See whether new ifa is better than current one:
2054  * 1) A non-virtual one is preferred over virtual.
2055  * 2) A virtual in master state preferred over any other state.
2056  *
2057  * Used in several address selecting functions.
2058  */
2059 int
ifa_preferred(struct ifaddr * cur,struct ifaddr * next)2060 ifa_preferred(struct ifaddr *cur, struct ifaddr *next)
2061 {
2062 
2063 	return (cur->ifa_carp && (!next->ifa_carp ||
2064 	    ((*carp_master_p)(next) && !(*carp_master_p)(cur))));
2065 }
2066 
2067 struct sockaddr_dl *
link_alloc_sdl(size_t size,int flags)2068 link_alloc_sdl(size_t size, int flags)
2069 {
2070 
2071 	return (malloc(size, M_TEMP, flags));
2072 }
2073 
2074 void
link_free_sdl(struct sockaddr * sa)2075 link_free_sdl(struct sockaddr *sa)
2076 {
2077 	free(sa, M_TEMP);
2078 }
2079 
2080 /*
2081  * Fills in given sdl with interface basic info.
2082  * Returns pointer to filled sdl.
2083  */
2084 struct sockaddr_dl *
link_init_sdl(struct ifnet * ifp,struct sockaddr * paddr,u_char iftype)2085 link_init_sdl(struct ifnet *ifp, struct sockaddr *paddr, u_char iftype)
2086 {
2087 	struct sockaddr_dl *sdl;
2088 
2089 	sdl = (struct sockaddr_dl *)paddr;
2090 	memset(sdl, 0, sizeof(struct sockaddr_dl));
2091 	sdl->sdl_len = sizeof(struct sockaddr_dl);
2092 	sdl->sdl_family = AF_LINK;
2093 	sdl->sdl_index = ifp->if_index;
2094 	sdl->sdl_type = iftype;
2095 
2096 	return (sdl);
2097 }
2098 
2099 void	(*vlan_link_state_p)(struct ifnet *);	/* XXX: private from if_vlan */
2100 void	(*vlan_trunk_cap_p)(struct ifnet *);		/* XXX: private from if_vlan */
2101 struct ifnet *(*vlan_trunkdev_p)(struct ifnet *);
2102 struct	ifnet *(*vlan_devat_p)(struct ifnet *, uint16_t);
2103 int	(*vlan_tag_p)(struct ifnet *, uint16_t *);
2104 int	(*vlan_pcp_p)(struct ifnet *, uint16_t *);
2105 int	(*vlan_setcookie_p)(struct ifnet *, void *);
2106 void	*(*vlan_cookie_p)(struct ifnet *);
2107 void	(*vlan_input_p)(struct ifnet *, struct mbuf *);
2108 
2109 /*
2110  * Handle a change in the interface link state. To avoid LORs
2111  * between driver lock and upper layer locks, as well as possible
2112  * recursions, we post event to taskqueue, and all job
2113  * is done in static do_link_state_change().
2114  */
2115 void
if_link_state_change(struct ifnet * ifp,int link_state)2116 if_link_state_change(struct ifnet *ifp, int link_state)
2117 {
2118 	/* Return if state hasn't changed. */
2119 	if (ifp->if_link_state == link_state)
2120 		return;
2121 
2122 	ifp->if_link_state = link_state;
2123 
2124 	/* XXXGL: reference ifp? */
2125 	taskqueue_enqueue(taskqueue_swi, &ifp->if_linktask);
2126 }
2127 
2128 static void
do_link_state_change(void * arg,int pending)2129 do_link_state_change(void *arg, int pending)
2130 {
2131 	struct ifnet *ifp;
2132 	int link_state;
2133 
2134 	ifp = arg;
2135 	link_state = ifp->if_link_state;
2136 
2137 	CURVNET_SET(ifp->if_vnet);
2138 	rt_ifmsg(ifp, 0);
2139 	if (ifp->if_vlantrunk != NULL)
2140 		(*vlan_link_state_p)(ifp);
2141 
2142 	if ((ifp->if_type == IFT_ETHER || ifp->if_type == IFT_L2VLAN) &&
2143 	    ifp->if_l2com != NULL)
2144 		(*ng_ether_link_state_p)(ifp, link_state);
2145 	if (ifp->if_carp)
2146 		(*carp_linkstate_p)(ifp);
2147 	if (ifp->if_bridge)
2148 		ifp->if_bridge_linkstate(ifp);
2149 	if (ifp->if_lagg)
2150 		(*lagg_linkstate_p)(ifp, link_state);
2151 
2152 	if (IS_DEFAULT_VNET(curvnet))
2153 		devctl_notify("IFNET", ifp->if_xname,
2154 		    (link_state == LINK_STATE_UP) ? "LINK_UP" : "LINK_DOWN",
2155 		    NULL);
2156 	if (pending > 1)
2157 		if_printf(ifp, "%d link states coalesced\n", pending);
2158 	if (log_link_state_change)
2159 		if_printf(ifp, "link state changed to %s\n",
2160 		    (link_state == LINK_STATE_UP) ? "UP" : "DOWN" );
2161 	EVENTHANDLER_INVOKE(ifnet_link_event, ifp, link_state);
2162 	CURVNET_RESTORE();
2163 }
2164 
2165 /*
2166  * Mark an interface down and notify protocols of
2167  * the transition.
2168  */
2169 void
if_down(struct ifnet * ifp)2170 if_down(struct ifnet *ifp)
2171 {
2172 
2173 	EVENTHANDLER_INVOKE(ifnet_event, ifp, IFNET_EVENT_DOWN);
2174 
2175 	ifp->if_flags &= ~IFF_UP;
2176 	getmicrotime(&ifp->if_lastchange);
2177 	ifp->if_qflush(ifp);
2178 
2179 	if (ifp->if_carp)
2180 		(*carp_linkstate_p)(ifp);
2181 	rt_ifmsg(ifp, IFF_UP);
2182 }
2183 
2184 /*
2185  * Mark an interface up and notify protocols of
2186  * the transition.
2187  */
2188 void
if_up(struct ifnet * ifp)2189 if_up(struct ifnet *ifp)
2190 {
2191 
2192 	ifp->if_flags |= IFF_UP;
2193 	getmicrotime(&ifp->if_lastchange);
2194 	if (ifp->if_carp)
2195 		(*carp_linkstate_p)(ifp);
2196 	rt_ifmsg(ifp, IFF_UP);
2197 	EVENTHANDLER_INVOKE(ifnet_event, ifp, IFNET_EVENT_UP);
2198 }
2199 
2200 /*
2201  * Flush an interface queue.
2202  */
2203 void
if_qflush(struct ifnet * ifp)2204 if_qflush(struct ifnet *ifp)
2205 {
2206 	struct mbuf *m, *n;
2207 	struct ifaltq *ifq;
2208 
2209 	ifq = &ifp->if_snd;
2210 	IFQ_LOCK(ifq);
2211 #ifdef ALTQ
2212 	if (ALTQ_IS_ENABLED(ifq))
2213 		ALTQ_PURGE(ifq);
2214 #endif
2215 	n = ifq->ifq_head;
2216 	while ((m = n) != NULL) {
2217 		n = m->m_nextpkt;
2218 		m_freem(m);
2219 	}
2220 	ifq->ifq_head = 0;
2221 	ifq->ifq_tail = 0;
2222 	ifq->ifq_len = 0;
2223 	IFQ_UNLOCK(ifq);
2224 }
2225 
2226 /*
2227  * Map interface name to interface structure pointer, with or without
2228  * returning a reference.
2229  */
2230 struct ifnet *
ifunit_ref(const char * name)2231 ifunit_ref(const char *name)
2232 {
2233 	struct epoch_tracker et;
2234 	struct ifnet *ifp;
2235 
2236 	NET_EPOCH_ENTER(et);
2237 	CK_STAILQ_FOREACH(ifp, &V_ifnet, if_link) {
2238 		if (strncmp(name, ifp->if_xname, IFNAMSIZ) == 0 &&
2239 		    !(ifp->if_flags & IFF_DYING))
2240 			break;
2241 	}
2242 	if (ifp != NULL) {
2243 		if_ref(ifp);
2244 		MPASS(ifindex_table[ifp->if_index].ife_ifnet == ifp);
2245 	}
2246 
2247 	NET_EPOCH_EXIT(et);
2248 	return (ifp);
2249 }
2250 
2251 struct ifnet *
ifunit(const char * name)2252 ifunit(const char *name)
2253 {
2254 	struct epoch_tracker et;
2255 	struct ifnet *ifp;
2256 
2257 	NET_EPOCH_ENTER(et);
2258 	CK_STAILQ_FOREACH(ifp, &V_ifnet, if_link) {
2259 		if (strncmp(name, ifp->if_xname, IFNAMSIZ) == 0)
2260 			break;
2261 	}
2262 	NET_EPOCH_EXIT(et);
2263 	return (ifp);
2264 }
2265 
2266 void *
ifr_buffer_get_buffer(void * data)2267 ifr_buffer_get_buffer(void *data)
2268 {
2269 	union ifreq_union *ifrup;
2270 
2271 	ifrup = data;
2272 #ifdef COMPAT_FREEBSD32
2273 	if (SV_CURPROC_FLAG(SV_ILP32))
2274 		return ((void *)(uintptr_t)
2275 		    ifrup->ifr32.ifr_ifru.ifru_buffer.buffer);
2276 #endif
2277 	return (ifrup->ifr.ifr_ifru.ifru_buffer.buffer);
2278 }
2279 
2280 static void
ifr_buffer_set_buffer_null(void * data)2281 ifr_buffer_set_buffer_null(void *data)
2282 {
2283 	union ifreq_union *ifrup;
2284 
2285 	ifrup = data;
2286 #ifdef COMPAT_FREEBSD32
2287 	if (SV_CURPROC_FLAG(SV_ILP32))
2288 		ifrup->ifr32.ifr_ifru.ifru_buffer.buffer = 0;
2289 	else
2290 #endif
2291 		ifrup->ifr.ifr_ifru.ifru_buffer.buffer = NULL;
2292 }
2293 
2294 size_t
ifr_buffer_get_length(void * data)2295 ifr_buffer_get_length(void *data)
2296 {
2297 	union ifreq_union *ifrup;
2298 
2299 	ifrup = data;
2300 #ifdef COMPAT_FREEBSD32
2301 	if (SV_CURPROC_FLAG(SV_ILP32))
2302 		return (ifrup->ifr32.ifr_ifru.ifru_buffer.length);
2303 #endif
2304 	return (ifrup->ifr.ifr_ifru.ifru_buffer.length);
2305 }
2306 
2307 static void
ifr_buffer_set_length(void * data,size_t len)2308 ifr_buffer_set_length(void *data, size_t len)
2309 {
2310 	union ifreq_union *ifrup;
2311 
2312 	ifrup = data;
2313 #ifdef COMPAT_FREEBSD32
2314 	if (SV_CURPROC_FLAG(SV_ILP32))
2315 		ifrup->ifr32.ifr_ifru.ifru_buffer.length = len;
2316 	else
2317 #endif
2318 		ifrup->ifr.ifr_ifru.ifru_buffer.length = len;
2319 }
2320 
2321 void *
ifr_data_get_ptr(void * ifrp)2322 ifr_data_get_ptr(void *ifrp)
2323 {
2324 	union ifreq_union *ifrup;
2325 
2326 	ifrup = ifrp;
2327 #ifdef COMPAT_FREEBSD32
2328 	if (SV_CURPROC_FLAG(SV_ILP32))
2329 		return ((void *)(uintptr_t)
2330 		    ifrup->ifr32.ifr_ifru.ifru_data);
2331 #endif
2332 		return (ifrup->ifr.ifr_ifru.ifru_data);
2333 }
2334 
2335 struct ifcap_nv_bit_name {
2336 	uint64_t cap_bit;
2337 	const char *cap_name;
2338 };
2339 #define CAPNV(x) {.cap_bit = IFCAP_##x, \
2340     .cap_name = __CONCAT(IFCAP_, __CONCAT(x, _NAME)) }
2341 const struct ifcap_nv_bit_name ifcap_nv_bit_names[] = {
2342 	CAPNV(RXCSUM),
2343 	CAPNV(TXCSUM),
2344 	CAPNV(NETCONS),
2345 	CAPNV(VLAN_MTU),
2346 	CAPNV(VLAN_HWTAGGING),
2347 	CAPNV(JUMBO_MTU),
2348 	CAPNV(POLLING),
2349 	CAPNV(VLAN_HWCSUM),
2350 	CAPNV(TSO4),
2351 	CAPNV(TSO6),
2352 	CAPNV(LRO),
2353 	CAPNV(WOL_UCAST),
2354 	CAPNV(WOL_MCAST),
2355 	CAPNV(WOL_MAGIC),
2356 	CAPNV(TOE4),
2357 	CAPNV(TOE6),
2358 	CAPNV(VLAN_HWFILTER),
2359 	CAPNV(VLAN_HWTSO),
2360 	CAPNV(LINKSTATE),
2361 	CAPNV(NETMAP),
2362 	CAPNV(RXCSUM_IPV6),
2363 	CAPNV(TXCSUM_IPV6),
2364 	CAPNV(HWSTATS),
2365 	CAPNV(TXRTLMT),
2366 	CAPNV(HWRXTSTMP),
2367 	CAPNV(MEXTPG),
2368 	CAPNV(TXTLS4),
2369 	CAPNV(TXTLS6),
2370 	CAPNV(VXLAN_HWCSUM),
2371 	CAPNV(VXLAN_HWTSO),
2372 	CAPNV(TXTLS_RTLMT),
2373 	{0, NULL}
2374 };
2375 #define CAP2NV(x) {.cap_bit = IFCAP2_BIT(IFCAP2_##x), \
2376     .cap_name = __CONCAT(IFCAP2_, __CONCAT(x, _NAME)) }
2377 const struct ifcap_nv_bit_name ifcap2_nv_bit_names[] = {
2378 	CAP2NV(RXTLS4),
2379 	CAP2NV(RXTLS6),
2380 	CAP2NV(IPSEC_OFFLOAD),
2381 	{0, NULL}
2382 };
2383 #undef CAPNV
2384 #undef CAP2NV
2385 
2386 int
if_capnv_to_capint(const nvlist_t * nv,int * old_cap,const struct ifcap_nv_bit_name * nn,bool all)2387 if_capnv_to_capint(const nvlist_t *nv, int *old_cap,
2388     const struct ifcap_nv_bit_name *nn, bool all)
2389 {
2390 	int i, res;
2391 
2392 	res = 0;
2393 	for (i = 0; nn[i].cap_name != NULL; i++) {
2394 		if (nvlist_exists_bool(nv, nn[i].cap_name)) {
2395 			if (all || nvlist_get_bool(nv, nn[i].cap_name))
2396 				res |= nn[i].cap_bit;
2397 		} else {
2398 			res |= *old_cap & nn[i].cap_bit;
2399 		}
2400 	}
2401 	return (res);
2402 }
2403 
2404 void
if_capint_to_capnv(nvlist_t * nv,const struct ifcap_nv_bit_name * nn,int ifr_cap,int ifr_req)2405 if_capint_to_capnv(nvlist_t *nv, const struct ifcap_nv_bit_name *nn,
2406     int ifr_cap, int ifr_req)
2407 {
2408 	int i;
2409 
2410 	for (i = 0; nn[i].cap_name != NULL; i++) {
2411 		if ((nn[i].cap_bit & ifr_cap) != 0) {
2412 			nvlist_add_bool(nv, nn[i].cap_name,
2413 			    (nn[i].cap_bit & ifr_req) != 0);
2414 		}
2415 	}
2416 }
2417 
2418 /*
2419  * Hardware specific interface ioctls.
2420  */
2421 int
ifhwioctl(u_long cmd,struct ifnet * ifp,caddr_t data,struct thread * td)2422 ifhwioctl(u_long cmd, struct ifnet *ifp, caddr_t data, struct thread *td)
2423 {
2424 	struct ifreq *ifr;
2425 	int error = 0, do_ifup = 0;
2426 	int new_flags, temp_flags;
2427 	size_t descrlen, nvbuflen;
2428 	char *descrbuf;
2429 	char new_name[IFNAMSIZ];
2430 	void *buf;
2431 	nvlist_t *nvcap;
2432 	struct siocsifcapnv_driver_data drv_ioctl_data;
2433 
2434 	ifr = (struct ifreq *)data;
2435 	switch (cmd) {
2436 	case SIOCGIFINDEX:
2437 		ifr->ifr_index = ifp->if_index;
2438 		break;
2439 
2440 	case SIOCGIFFLAGS:
2441 		temp_flags = ifp->if_flags | ifp->if_drv_flags;
2442 		ifr->ifr_flags = temp_flags & 0xffff;
2443 		ifr->ifr_flagshigh = temp_flags >> 16;
2444 		break;
2445 
2446 	case SIOCGIFCAP:
2447 		ifr->ifr_reqcap = ifp->if_capabilities;
2448 		ifr->ifr_curcap = ifp->if_capenable;
2449 		break;
2450 
2451 	case SIOCGIFCAPNV:
2452 		if ((ifp->if_capabilities & IFCAP_NV) == 0) {
2453 			error = EINVAL;
2454 			break;
2455 		}
2456 		buf = NULL;
2457 		nvcap = nvlist_create(0);
2458 		for (;;) {
2459 			if_capint_to_capnv(nvcap, ifcap_nv_bit_names,
2460 			    ifp->if_capabilities, ifp->if_capenable);
2461 			if_capint_to_capnv(nvcap, ifcap2_nv_bit_names,
2462 			    ifp->if_capabilities2, ifp->if_capenable2);
2463 			error = (*ifp->if_ioctl)(ifp, SIOCGIFCAPNV,
2464 			    __DECONST(caddr_t, nvcap));
2465 			if (error != 0) {
2466 				if_printf(ifp,
2467 			    "SIOCGIFCAPNV driver mistake: nvlist error %d\n",
2468 				    error);
2469 				break;
2470 			}
2471 			buf = nvlist_pack(nvcap, &nvbuflen);
2472 			if (buf == NULL) {
2473 				error = nvlist_error(nvcap);
2474 				if (error == 0)
2475 					error = EDOOFUS;
2476 				break;
2477 			}
2478 			if (nvbuflen > ifr->ifr_cap_nv.buf_length) {
2479 				ifr->ifr_cap_nv.length = nvbuflen;
2480 				ifr->ifr_cap_nv.buffer = NULL;
2481 				error = EFBIG;
2482 				break;
2483 			}
2484 			ifr->ifr_cap_nv.length = nvbuflen;
2485 			error = copyout(buf, ifr->ifr_cap_nv.buffer, nvbuflen);
2486 			break;
2487 		}
2488 		free(buf, M_NVLIST);
2489 		nvlist_destroy(nvcap);
2490 		break;
2491 
2492 	case SIOCGIFDATA:
2493 	{
2494 		struct if_data ifd;
2495 
2496 		/* Ensure uninitialised padding is not leaked. */
2497 		memset(&ifd, 0, sizeof(ifd));
2498 
2499 		if_data_copy(ifp, &ifd);
2500 		error = copyout(&ifd, ifr_data_get_ptr(ifr), sizeof(ifd));
2501 		break;
2502 	}
2503 
2504 #ifdef MAC
2505 	case SIOCGIFMAC:
2506 		error = mac_ifnet_ioctl_get(td->td_ucred, ifr, ifp);
2507 		break;
2508 #endif
2509 
2510 	case SIOCGIFMETRIC:
2511 		ifr->ifr_metric = ifp->if_metric;
2512 		break;
2513 
2514 	case SIOCGIFMTU:
2515 		ifr->ifr_mtu = ifp->if_mtu;
2516 		break;
2517 
2518 	case SIOCGIFPHYS:
2519 		/* XXXGL: did this ever worked? */
2520 		ifr->ifr_phys = 0;
2521 		break;
2522 
2523 	case SIOCGIFDESCR:
2524 		error = 0;
2525 		sx_slock(&ifdescr_sx);
2526 		if (ifp->if_description == NULL)
2527 			error = ENOMSG;
2528 		else {
2529 			/* space for terminating nul */
2530 			descrlen = strlen(ifp->if_description) + 1;
2531 			if (ifr_buffer_get_length(ifr) < descrlen)
2532 				ifr_buffer_set_buffer_null(ifr);
2533 			else
2534 				error = copyout(ifp->if_description,
2535 				    ifr_buffer_get_buffer(ifr), descrlen);
2536 			ifr_buffer_set_length(ifr, descrlen);
2537 		}
2538 		sx_sunlock(&ifdescr_sx);
2539 		break;
2540 
2541 	case SIOCSIFDESCR:
2542 		error = priv_check(td, PRIV_NET_SETIFDESCR);
2543 		if (error)
2544 			return (error);
2545 
2546 		/*
2547 		 * Copy only (length-1) bytes to make sure that
2548 		 * if_description is always nul terminated.  The
2549 		 * length parameter is supposed to count the
2550 		 * terminating nul in.
2551 		 */
2552 		if (ifr_buffer_get_length(ifr) > ifdescr_maxlen)
2553 			return (ENAMETOOLONG);
2554 		else if (ifr_buffer_get_length(ifr) == 0)
2555 			descrbuf = NULL;
2556 		else {
2557 			descrbuf = if_allocdescr(ifr_buffer_get_length(ifr), M_WAITOK);
2558 			error = copyin(ifr_buffer_get_buffer(ifr), descrbuf,
2559 			    ifr_buffer_get_length(ifr) - 1);
2560 			if (error) {
2561 				if_freedescr(descrbuf);
2562 				break;
2563 			}
2564 		}
2565 
2566 		if_setdescr(ifp, descrbuf);
2567 		getmicrotime(&ifp->if_lastchange);
2568 		break;
2569 
2570 	case SIOCGIFFIB:
2571 		ifr->ifr_fib = ifp->if_fib;
2572 		break;
2573 
2574 	case SIOCSIFFIB:
2575 		error = priv_check(td, PRIV_NET_SETIFFIB);
2576 		if (error)
2577 			return (error);
2578 		if (ifr->ifr_fib >= rt_numfibs)
2579 			return (EINVAL);
2580 
2581 		ifp->if_fib = ifr->ifr_fib;
2582 		break;
2583 
2584 	case SIOCSIFFLAGS:
2585 		error = priv_check(td, PRIV_NET_SETIFFLAGS);
2586 		if (error)
2587 			return (error);
2588 		/*
2589 		 * Currently, no driver owned flags pass the IFF_CANTCHANGE
2590 		 * check, so we don't need special handling here yet.
2591 		 */
2592 		new_flags = (ifr->ifr_flags & 0xffff) |
2593 		    (ifr->ifr_flagshigh << 16);
2594 		if (ifp->if_flags & IFF_UP &&
2595 		    (new_flags & IFF_UP) == 0) {
2596 			if_down(ifp);
2597 		} else if (new_flags & IFF_UP &&
2598 		    (ifp->if_flags & IFF_UP) == 0) {
2599 			do_ifup = 1;
2600 		}
2601 
2602 		/*
2603 		 * See if the promiscuous mode or allmulti bits are about to
2604 		 * flip.  They require special handling because in-kernel
2605 		 * consumers may indepdently toggle them.
2606 		 */
2607 		if_setppromisc(ifp, new_flags & IFF_PPROMISC);
2608 		if ((ifp->if_flags ^ new_flags) & IFF_PALLMULTI) {
2609 			if (new_flags & IFF_PALLMULTI)
2610 				ifp->if_flags |= IFF_ALLMULTI;
2611 			else if (ifp->if_amcount == 0)
2612 				ifp->if_flags &= ~IFF_ALLMULTI;
2613 		}
2614 		ifp->if_flags = (ifp->if_flags & IFF_CANTCHANGE) |
2615 			(new_flags &~ IFF_CANTCHANGE);
2616 		if (ifp->if_ioctl) {
2617 			(void) (*ifp->if_ioctl)(ifp, cmd, data);
2618 		}
2619 		if (do_ifup)
2620 			if_up(ifp);
2621 		getmicrotime(&ifp->if_lastchange);
2622 		break;
2623 
2624 	case SIOCSIFCAP:
2625 		error = priv_check(td, PRIV_NET_SETIFCAP);
2626 		if (error != 0)
2627 			return (error);
2628 		if (ifp->if_ioctl == NULL)
2629 			return (EOPNOTSUPP);
2630 		if (ifr->ifr_reqcap & ~ifp->if_capabilities)
2631 			return (EINVAL);
2632 		error = (*ifp->if_ioctl)(ifp, cmd, data);
2633 		if (error == 0)
2634 			getmicrotime(&ifp->if_lastchange);
2635 		break;
2636 
2637 	case SIOCSIFCAPNV:
2638 		error = priv_check(td, PRIV_NET_SETIFCAP);
2639 		if (error != 0)
2640 			return (error);
2641 		if (ifp->if_ioctl == NULL)
2642 			return (EOPNOTSUPP);
2643 		if ((ifp->if_capabilities & IFCAP_NV) == 0)
2644 			return (EINVAL);
2645 		if (ifr->ifr_cap_nv.length > IFR_CAP_NV_MAXBUFSIZE)
2646 			return (EINVAL);
2647 		nvcap = NULL;
2648 		buf = malloc(ifr->ifr_cap_nv.length, M_TEMP, M_WAITOK);
2649 		for (;;) {
2650 			error = copyin(ifr->ifr_cap_nv.buffer, buf,
2651 			    ifr->ifr_cap_nv.length);
2652 			if (error != 0)
2653 				break;
2654 			nvcap = nvlist_unpack(buf, ifr->ifr_cap_nv.length, 0);
2655 			if (nvcap == NULL) {
2656 				error = EINVAL;
2657 				break;
2658 			}
2659 			drv_ioctl_data.reqcap = if_capnv_to_capint(nvcap,
2660 			    &ifp->if_capenable, ifcap_nv_bit_names, false);
2661 			if ((drv_ioctl_data.reqcap &
2662 			    ~ifp->if_capabilities) != 0) {
2663 				error = EINVAL;
2664 				break;
2665 			}
2666 			drv_ioctl_data.reqcap2 = if_capnv_to_capint(nvcap,
2667 			    &ifp->if_capenable2, ifcap2_nv_bit_names, false);
2668 			if ((drv_ioctl_data.reqcap2 &
2669 			    ~ifp->if_capabilities2) != 0) {
2670 				error = EINVAL;
2671 				break;
2672 			}
2673 			drv_ioctl_data.nvcap = nvcap;
2674 			error = (*ifp->if_ioctl)(ifp, SIOCSIFCAPNV,
2675 			    (caddr_t)&drv_ioctl_data);
2676 			break;
2677 		}
2678 		nvlist_destroy(nvcap);
2679 		free(buf, M_TEMP);
2680 		if (error == 0)
2681 			getmicrotime(&ifp->if_lastchange);
2682 		break;
2683 
2684 #ifdef MAC
2685 	case SIOCSIFMAC:
2686 		error = mac_ifnet_ioctl_set(td->td_ucred, ifr, ifp);
2687 		break;
2688 #endif
2689 
2690 	case SIOCSIFNAME:
2691 		error = priv_check(td, PRIV_NET_SETIFNAME);
2692 		if (error)
2693 			return (error);
2694 		error = copyinstr(ifr_data_get_ptr(ifr), new_name, IFNAMSIZ,
2695 		    NULL);
2696 		if (error != 0)
2697 			return (error);
2698 		error = if_rename(ifp, new_name);
2699 		break;
2700 
2701 #ifdef VIMAGE
2702 	case SIOCSIFVNET:
2703 		error = priv_check(td, PRIV_NET_SETIFVNET);
2704 		if (error)
2705 			return (error);
2706 		error = if_vmove_loan(td, ifp, ifr->ifr_name, ifr->ifr_jid);
2707 		break;
2708 #endif
2709 
2710 	case SIOCSIFMETRIC:
2711 		error = priv_check(td, PRIV_NET_SETIFMETRIC);
2712 		if (error)
2713 			return (error);
2714 		ifp->if_metric = ifr->ifr_metric;
2715 		getmicrotime(&ifp->if_lastchange);
2716 		break;
2717 
2718 	case SIOCSIFPHYS:
2719 		error = priv_check(td, PRIV_NET_SETIFPHYS);
2720 		if (error)
2721 			return (error);
2722 		if (ifp->if_ioctl == NULL)
2723 			return (EOPNOTSUPP);
2724 		error = (*ifp->if_ioctl)(ifp, cmd, data);
2725 		if (error == 0)
2726 			getmicrotime(&ifp->if_lastchange);
2727 		break;
2728 
2729 	case SIOCSIFMTU:
2730 	{
2731 		u_long oldmtu = ifp->if_mtu;
2732 
2733 		error = priv_check(td, PRIV_NET_SETIFMTU);
2734 		if (error)
2735 			return (error);
2736 		if (ifr->ifr_mtu < IF_MINMTU || ifr->ifr_mtu > IF_MAXMTU)
2737 			return (EINVAL);
2738 		if (ifp->if_ioctl == NULL)
2739 			return (EOPNOTSUPP);
2740 		/* Disallow MTU changes on bridge member interfaces. */
2741 		if (ifp->if_bridge)
2742 			return (EOPNOTSUPP);
2743 		error = (*ifp->if_ioctl)(ifp, cmd, data);
2744 		if (error == 0) {
2745 			getmicrotime(&ifp->if_lastchange);
2746 			rt_ifmsg(ifp, 0);
2747 #ifdef INET
2748 			DEBUGNET_NOTIFY_MTU(ifp);
2749 #endif
2750 		}
2751 		/*
2752 		 * If the link MTU changed, do network layer specific procedure.
2753 		 */
2754 		if (ifp->if_mtu != oldmtu)
2755 			if_notifymtu(ifp);
2756 		break;
2757 	}
2758 
2759 	case SIOCADDMULTI:
2760 	case SIOCDELMULTI:
2761 		if (cmd == SIOCADDMULTI)
2762 			error = priv_check(td, PRIV_NET_ADDMULTI);
2763 		else
2764 			error = priv_check(td, PRIV_NET_DELMULTI);
2765 		if (error)
2766 			return (error);
2767 
2768 		/* Don't allow group membership on non-multicast interfaces. */
2769 		if ((ifp->if_flags & IFF_MULTICAST) == 0)
2770 			return (EOPNOTSUPP);
2771 
2772 		/* Don't let users screw up protocols' entries. */
2773 		if (ifr->ifr_addr.sa_family != AF_LINK)
2774 			return (EINVAL);
2775 
2776 		if (cmd == SIOCADDMULTI) {
2777 			struct epoch_tracker et;
2778 			struct ifmultiaddr *ifma;
2779 
2780 			/*
2781 			 * Userland is only permitted to join groups once
2782 			 * via the if_addmulti() KPI, because it cannot hold
2783 			 * struct ifmultiaddr * between calls. It may also
2784 			 * lose a race while we check if the membership
2785 			 * already exists.
2786 			 */
2787 			NET_EPOCH_ENTER(et);
2788 			ifma = if_findmulti(ifp, &ifr->ifr_addr);
2789 			NET_EPOCH_EXIT(et);
2790 			if (ifma != NULL)
2791 				error = EADDRINUSE;
2792 			else
2793 				error = if_addmulti(ifp, &ifr->ifr_addr, &ifma);
2794 		} else {
2795 			error = if_delmulti(ifp, &ifr->ifr_addr);
2796 		}
2797 		if (error == 0)
2798 			getmicrotime(&ifp->if_lastchange);
2799 		break;
2800 
2801 	case SIOCSIFPHYADDR:
2802 	case SIOCDIFPHYADDR:
2803 #ifdef INET6
2804 	case SIOCSIFPHYADDR_IN6:
2805 #endif
2806 	case SIOCSIFMEDIA:
2807 	case SIOCSIFGENERIC:
2808 		error = priv_check(td, PRIV_NET_HWIOCTL);
2809 		if (error)
2810 			return (error);
2811 		if (ifp->if_ioctl == NULL)
2812 			return (EOPNOTSUPP);
2813 		error = (*ifp->if_ioctl)(ifp, cmd, data);
2814 		if (error == 0)
2815 			getmicrotime(&ifp->if_lastchange);
2816 		break;
2817 
2818 	case SIOCGIFSTATUS:
2819 	case SIOCGIFPSRCADDR:
2820 	case SIOCGIFPDSTADDR:
2821 	case SIOCGIFMEDIA:
2822 	case SIOCGIFXMEDIA:
2823 	case SIOCGIFGENERIC:
2824 	case SIOCGIFRSSKEY:
2825 	case SIOCGIFRSSHASH:
2826 	case SIOCGIFDOWNREASON:
2827 		if (ifp->if_ioctl == NULL)
2828 			return (EOPNOTSUPP);
2829 		error = (*ifp->if_ioctl)(ifp, cmd, data);
2830 		break;
2831 
2832 	case SIOCSIFLLADDR:
2833 		error = priv_check(td, PRIV_NET_SETLLADDR);
2834 		if (error)
2835 			return (error);
2836 		error = if_setlladdr(ifp,
2837 		    ifr->ifr_addr.sa_data, ifr->ifr_addr.sa_len);
2838 		break;
2839 
2840 	case SIOCGHWADDR:
2841 		error = if_gethwaddr(ifp, ifr);
2842 		break;
2843 
2844 	case SIOCAIFGROUP:
2845 		error = priv_check(td, PRIV_NET_ADDIFGROUP);
2846 		if (error)
2847 			return (error);
2848 		error = if_addgroup(ifp,
2849 		    ((struct ifgroupreq *)data)->ifgr_group);
2850 		if (error != 0)
2851 			return (error);
2852 		break;
2853 
2854 	case SIOCGIFGROUP:
2855 	{
2856 		struct epoch_tracker et;
2857 
2858 		NET_EPOCH_ENTER(et);
2859 		error = if_getgroup((struct ifgroupreq *)data, ifp);
2860 		NET_EPOCH_EXIT(et);
2861 		break;
2862 	}
2863 
2864 	case SIOCDIFGROUP:
2865 		error = priv_check(td, PRIV_NET_DELIFGROUP);
2866 		if (error)
2867 			return (error);
2868 		error = if_delgroup(ifp,
2869 		    ((struct ifgroupreq *)data)->ifgr_group);
2870 		if (error != 0)
2871 			return (error);
2872 		break;
2873 
2874 	default:
2875 		error = ENOIOCTL;
2876 		break;
2877 	}
2878 	return (error);
2879 }
2880 
2881 /*
2882  * Interface ioctls.
2883  */
2884 int
ifioctl(struct socket * so,u_long cmd,caddr_t data,struct thread * td)2885 ifioctl(struct socket *so, u_long cmd, caddr_t data, struct thread *td)
2886 {
2887 #ifdef COMPAT_FREEBSD32
2888 	union {
2889 		struct ifconf ifc;
2890 		struct ifdrv ifd;
2891 		struct ifgroupreq ifgr;
2892 		struct ifmediareq ifmr;
2893 	} thunk;
2894 	u_long saved_cmd;
2895 	struct ifconf32 *ifc32;
2896 	struct ifdrv32 *ifd32;
2897 	struct ifgroupreq32 *ifgr32;
2898 	struct ifmediareq32 *ifmr32;
2899 #endif
2900 	struct ifnet *ifp;
2901 	struct ifreq *ifr;
2902 	int error;
2903 	int oif_flags;
2904 #ifdef VIMAGE
2905 	bool shutdown;
2906 #endif
2907 
2908 	CURVNET_SET(so->so_vnet);
2909 #ifdef VIMAGE
2910 	/* Make sure the VNET is stable. */
2911 	shutdown = VNET_IS_SHUTTING_DOWN(so->so_vnet);
2912 	if (shutdown) {
2913 		CURVNET_RESTORE();
2914 		return (EBUSY);
2915 	}
2916 #endif
2917 
2918 #ifdef COMPAT_FREEBSD32
2919 	saved_cmd = cmd;
2920 	switch (cmd) {
2921 	case SIOCGIFCONF32:
2922 		ifc32 = (struct ifconf32 *)data;
2923 		thunk.ifc.ifc_len = ifc32->ifc_len;
2924 		thunk.ifc.ifc_buf = PTRIN(ifc32->ifc_buf);
2925 		data = (caddr_t)&thunk.ifc;
2926 		cmd = SIOCGIFCONF;
2927 		break;
2928 	case SIOCGDRVSPEC32:
2929 	case SIOCSDRVSPEC32:
2930 		ifd32 = (struct ifdrv32 *)data;
2931 		memcpy(thunk.ifd.ifd_name, ifd32->ifd_name,
2932 		    sizeof(thunk.ifd.ifd_name));
2933 		thunk.ifd.ifd_cmd = ifd32->ifd_cmd;
2934 		thunk.ifd.ifd_len = ifd32->ifd_len;
2935 		thunk.ifd.ifd_data = PTRIN(ifd32->ifd_data);
2936 		data = (caddr_t)&thunk.ifd;
2937 		cmd = _IOC_NEWTYPE(cmd, struct ifdrv);
2938 		break;
2939 	case SIOCAIFGROUP32:
2940 	case SIOCGIFGROUP32:
2941 	case SIOCDIFGROUP32:
2942 	case SIOCGIFGMEMB32:
2943 		ifgr32 = (struct ifgroupreq32 *)data;
2944 		memcpy(thunk.ifgr.ifgr_name, ifgr32->ifgr_name,
2945 		    sizeof(thunk.ifgr.ifgr_name));
2946 		thunk.ifgr.ifgr_len = ifgr32->ifgr_len;
2947 		switch (cmd) {
2948 		case SIOCAIFGROUP32:
2949 		case SIOCDIFGROUP32:
2950 			memcpy(thunk.ifgr.ifgr_group, ifgr32->ifgr_group,
2951 			    sizeof(thunk.ifgr.ifgr_group));
2952 			break;
2953 		case SIOCGIFGROUP32:
2954 		case SIOCGIFGMEMB32:
2955 			thunk.ifgr.ifgr_groups = PTRIN(ifgr32->ifgr_groups);
2956 			break;
2957 		}
2958 		data = (caddr_t)&thunk.ifgr;
2959 		cmd = _IOC_NEWTYPE(cmd, struct ifgroupreq);
2960 		break;
2961 	case SIOCGIFMEDIA32:
2962 	case SIOCGIFXMEDIA32:
2963 		ifmr32 = (struct ifmediareq32 *)data;
2964 		memcpy(thunk.ifmr.ifm_name, ifmr32->ifm_name,
2965 		    sizeof(thunk.ifmr.ifm_name));
2966 		thunk.ifmr.ifm_current = ifmr32->ifm_current;
2967 		thunk.ifmr.ifm_mask = ifmr32->ifm_mask;
2968 		thunk.ifmr.ifm_status = ifmr32->ifm_status;
2969 		thunk.ifmr.ifm_active = ifmr32->ifm_active;
2970 		thunk.ifmr.ifm_count = ifmr32->ifm_count;
2971 		thunk.ifmr.ifm_ulist = PTRIN(ifmr32->ifm_ulist);
2972 		data = (caddr_t)&thunk.ifmr;
2973 		cmd = _IOC_NEWTYPE(cmd, struct ifmediareq);
2974 		break;
2975 	}
2976 #endif
2977 
2978 	switch (cmd) {
2979 	case SIOCGIFCONF:
2980 		error = ifconf(cmd, data);
2981 		goto out_noref;
2982 	}
2983 
2984 	ifr = (struct ifreq *)data;
2985 	switch (cmd) {
2986 #ifdef VIMAGE
2987 	case SIOCSIFRVNET:
2988 		error = priv_check(td, PRIV_NET_SETIFVNET);
2989 		if (error == 0)
2990 			error = if_vmove_reclaim(td, ifr->ifr_name,
2991 			    ifr->ifr_jid);
2992 		goto out_noref;
2993 #endif
2994 	case SIOCIFCREATE:
2995 	case SIOCIFCREATE2:
2996 		error = priv_check(td, PRIV_NET_IFCREATE);
2997 		if (error == 0)
2998 			error = if_clone_create(ifr->ifr_name,
2999 			    sizeof(ifr->ifr_name), cmd == SIOCIFCREATE2 ?
3000 			    ifr_data_get_ptr(ifr) : NULL);
3001 		goto out_noref;
3002 	case SIOCIFDESTROY:
3003 		error = priv_check(td, PRIV_NET_IFDESTROY);
3004 
3005 		if (error == 0) {
3006 			sx_xlock(&ifnet_detach_sxlock);
3007 			error = if_clone_destroy(ifr->ifr_name);
3008 			sx_xunlock(&ifnet_detach_sxlock);
3009 		}
3010 		goto out_noref;
3011 
3012 	case SIOCIFGCLONERS:
3013 		error = if_clone_list((struct if_clonereq *)data);
3014 		goto out_noref;
3015 
3016 	case SIOCGIFGMEMB:
3017 		error = if_getgroupmembers((struct ifgroupreq *)data);
3018 		goto out_noref;
3019 
3020 #if defined(INET) || defined(INET6)
3021 	case SIOCSVH:
3022 	case SIOCGVH:
3023 		if (carp_ioctl_p == NULL)
3024 			error = EPROTONOSUPPORT;
3025 		else
3026 			error = (*carp_ioctl_p)(ifr, cmd, td);
3027 		goto out_noref;
3028 #endif
3029 	}
3030 
3031 	ifp = ifunit_ref(ifr->ifr_name);
3032 	if (ifp == NULL) {
3033 		error = ENXIO;
3034 		goto out_noref;
3035 	}
3036 
3037 	error = ifhwioctl(cmd, ifp, data, td);
3038 	if (error != ENOIOCTL)
3039 		goto out_ref;
3040 
3041 	oif_flags = ifp->if_flags;
3042 	if (so->so_proto == NULL) {
3043 		error = EOPNOTSUPP;
3044 		goto out_ref;
3045 	}
3046 
3047 	/*
3048 	 * Pass the request on to the socket control method, and if the
3049 	 * latter returns EOPNOTSUPP, directly to the interface.
3050 	 *
3051 	 * Make an exception for the legacy SIOCSIF* requests.  Drivers
3052 	 * trust SIOCSIFADDR et al to come from an already privileged
3053 	 * layer, and do not perform any credentials checks or input
3054 	 * validation.
3055 	 */
3056 	error = so->so_proto->pr_control(so, cmd, data, ifp, td);
3057 	if (error == EOPNOTSUPP && ifp != NULL && ifp->if_ioctl != NULL &&
3058 	    cmd != SIOCSIFADDR && cmd != SIOCSIFBRDADDR &&
3059 	    cmd != SIOCSIFDSTADDR && cmd != SIOCSIFNETMASK)
3060 		error = (*ifp->if_ioctl)(ifp, cmd, data);
3061 
3062 	if (!(oif_flags & IFF_UP) && (ifp->if_flags & IFF_UP))
3063 		if_up(ifp);
3064 out_ref:
3065 	if_rele(ifp);
3066 out_noref:
3067 	CURVNET_RESTORE();
3068 #ifdef COMPAT_FREEBSD32
3069 	if (error != 0)
3070 		return (error);
3071 	switch (saved_cmd) {
3072 	case SIOCGIFCONF32:
3073 		ifc32->ifc_len = thunk.ifc.ifc_len;
3074 		break;
3075 	case SIOCGDRVSPEC32:
3076 		/*
3077 		 * SIOCGDRVSPEC is IOWR, but nothing actually touches
3078 		 * the struct so just assert that ifd_len (the only
3079 		 * field it might make sense to update) hasn't
3080 		 * changed.
3081 		 */
3082 		KASSERT(thunk.ifd.ifd_len == ifd32->ifd_len,
3083 		    ("ifd_len was updated %u -> %zu", ifd32->ifd_len,
3084 			thunk.ifd.ifd_len));
3085 		break;
3086 	case SIOCGIFGROUP32:
3087 	case SIOCGIFGMEMB32:
3088 		ifgr32->ifgr_len = thunk.ifgr.ifgr_len;
3089 		break;
3090 	case SIOCGIFMEDIA32:
3091 	case SIOCGIFXMEDIA32:
3092 		ifmr32->ifm_current = thunk.ifmr.ifm_current;
3093 		ifmr32->ifm_mask = thunk.ifmr.ifm_mask;
3094 		ifmr32->ifm_status = thunk.ifmr.ifm_status;
3095 		ifmr32->ifm_active = thunk.ifmr.ifm_active;
3096 		ifmr32->ifm_count = thunk.ifmr.ifm_count;
3097 		break;
3098 	}
3099 #endif
3100 	return (error);
3101 }
3102 
3103 int
if_rename(struct ifnet * ifp,char * new_name)3104 if_rename(struct ifnet *ifp, char *new_name)
3105 {
3106 	struct ifaddr *ifa;
3107 	struct sockaddr_dl *sdl;
3108 	size_t namelen, onamelen;
3109 	char old_name[IFNAMSIZ];
3110 	char strbuf[IFNAMSIZ + 8];
3111 
3112 	if (new_name[0] == '\0')
3113 		return (EINVAL);
3114 	if (strcmp(new_name, ifp->if_xname) == 0)
3115 		return (0);
3116 	if (ifunit(new_name) != NULL)
3117 		return (EEXIST);
3118 
3119 	/*
3120 	 * XXX: Locking.  Nothing else seems to lock if_flags,
3121 	 * and there are numerous other races with the
3122 	 * ifunit() checks not being atomic with namespace
3123 	 * changes (renames, vmoves, if_attach, etc).
3124 	 */
3125 	ifp->if_flags |= IFF_RENAMING;
3126 
3127 	EVENTHANDLER_INVOKE(ifnet_departure_event, ifp);
3128 
3129 	if_printf(ifp, "changing name to '%s'\n", new_name);
3130 
3131 	IF_ADDR_WLOCK(ifp);
3132 	strlcpy(old_name, ifp->if_xname, sizeof(old_name));
3133 	strlcpy(ifp->if_xname, new_name, sizeof(ifp->if_xname));
3134 	ifa = ifp->if_addr;
3135 	sdl = (struct sockaddr_dl *)ifa->ifa_addr;
3136 	namelen = strlen(new_name);
3137 	onamelen = sdl->sdl_nlen;
3138 	/*
3139 	 * Move the address if needed.  This is safe because we
3140 	 * allocate space for a name of length IFNAMSIZ when we
3141 	 * create this in if_attach().
3142 	 */
3143 	if (namelen != onamelen) {
3144 		bcopy(sdl->sdl_data + onamelen,
3145 		    sdl->sdl_data + namelen, sdl->sdl_alen);
3146 	}
3147 	bcopy(new_name, sdl->sdl_data, namelen);
3148 	sdl->sdl_nlen = namelen;
3149 	sdl = (struct sockaddr_dl *)ifa->ifa_netmask;
3150 	bzero(sdl->sdl_data, onamelen);
3151 	while (namelen != 0)
3152 		sdl->sdl_data[--namelen] = 0xff;
3153 	IF_ADDR_WUNLOCK(ifp);
3154 
3155 	EVENTHANDLER_INVOKE(ifnet_arrival_event, ifp);
3156 
3157 	ifp->if_flags &= ~IFF_RENAMING;
3158 
3159 	snprintf(strbuf, sizeof(strbuf), "name=%s", new_name);
3160 	devctl_notify("IFNET", old_name, "RENAME", strbuf);
3161 
3162 	return (0);
3163 }
3164 
3165 /*
3166  * The code common to handling reference counted flags,
3167  * e.g., in ifpromisc() and if_allmulti().
3168  * The "pflag" argument can specify a permanent mode flag to check,
3169  * such as IFF_PPROMISC for promiscuous mode; should be 0 if none.
3170  *
3171  * Only to be used on stack-owned flags, not driver-owned flags.
3172  */
3173 static int
if_setflag(struct ifnet * ifp,int flag,int pflag,int * refcount,int onswitch)3174 if_setflag(struct ifnet *ifp, int flag, int pflag, int *refcount, int onswitch)
3175 {
3176 	struct ifreq ifr;
3177 	int error;
3178 	int oldflags, oldcount;
3179 
3180 	/* Sanity checks to catch programming errors */
3181 	KASSERT((flag & (IFF_DRV_OACTIVE|IFF_DRV_RUNNING)) == 0,
3182 	    ("%s: setting driver-owned flag %d", __func__, flag));
3183 
3184 	if (onswitch)
3185 		KASSERT(*refcount >= 0,
3186 		    ("%s: increment negative refcount %d for flag %d",
3187 		    __func__, *refcount, flag));
3188 	else
3189 		KASSERT(*refcount > 0,
3190 		    ("%s: decrement non-positive refcount %d for flag %d",
3191 		    __func__, *refcount, flag));
3192 
3193 	/* In case this mode is permanent, just touch refcount */
3194 	if (ifp->if_flags & pflag) {
3195 		*refcount += onswitch ? 1 : -1;
3196 		return (0);
3197 	}
3198 
3199 	/* Save ifnet parameters for if_ioctl() may fail */
3200 	oldcount = *refcount;
3201 	oldflags = ifp->if_flags;
3202 
3203 	/*
3204 	 * See if we aren't the only and touching refcount is enough.
3205 	 * Actually toggle interface flag if we are the first or last.
3206 	 */
3207 	if (onswitch) {
3208 		if ((*refcount)++)
3209 			return (0);
3210 		ifp->if_flags |= flag;
3211 	} else {
3212 		if (--(*refcount))
3213 			return (0);
3214 		ifp->if_flags &= ~flag;
3215 	}
3216 
3217 	/* Call down the driver since we've changed interface flags */
3218 	if (ifp->if_ioctl == NULL) {
3219 		error = EOPNOTSUPP;
3220 		goto recover;
3221 	}
3222 	ifr.ifr_flags = ifp->if_flags & 0xffff;
3223 	ifr.ifr_flagshigh = ifp->if_flags >> 16;
3224 	error = (*ifp->if_ioctl)(ifp, SIOCSIFFLAGS, (caddr_t)&ifr);
3225 	if (error)
3226 		goto recover;
3227 	/* Notify userland that interface flags have changed */
3228 	rt_ifmsg(ifp, flag);
3229 	return (0);
3230 
3231 recover:
3232 	/* Recover after driver error */
3233 	*refcount = oldcount;
3234 	ifp->if_flags = oldflags;
3235 	return (error);
3236 }
3237 
3238 /*
3239  * Set/clear promiscuous mode on interface ifp based on the truth value
3240  * of pswitch.  The calls are reference counted so that only the first
3241  * "on" request actually has an effect, as does the final "off" request.
3242  * Results are undefined if the "off" and "on" requests are not matched.
3243  */
3244 int
ifpromisc(struct ifnet * ifp,int pswitch)3245 ifpromisc(struct ifnet *ifp, int pswitch)
3246 {
3247 	int error;
3248 	int oldflags = ifp->if_flags;
3249 
3250 	error = if_setflag(ifp, IFF_PROMISC, IFF_PPROMISC,
3251 			   &ifp->if_pcount, pswitch);
3252 	/* If promiscuous mode status has changed, log a message */
3253 	if (error == 0 && ((ifp->if_flags ^ oldflags) & IFF_PROMISC) &&
3254             log_promisc_mode_change)
3255 		if_printf(ifp, "promiscuous mode %s\n",
3256 		    (ifp->if_flags & IFF_PROMISC) ? "enabled" : "disabled");
3257 	return (error);
3258 }
3259 
3260 /*
3261  * Return interface configuration
3262  * of system.  List may be used
3263  * in later ioctl's (above) to get
3264  * other information.
3265  */
3266 /*ARGSUSED*/
3267 static int
ifconf(u_long cmd,caddr_t data)3268 ifconf(u_long cmd, caddr_t data)
3269 {
3270 	struct ifconf *ifc = (struct ifconf *)data;
3271 	struct ifnet *ifp;
3272 	struct ifaddr *ifa;
3273 	struct ifreq ifr;
3274 	struct sbuf *sb;
3275 	int error, full = 0, valid_len, max_len;
3276 
3277 	/* Limit initial buffer size to maxphys to avoid DoS from userspace. */
3278 	max_len = maxphys - 1;
3279 
3280 	/* Prevent hostile input from being able to crash the system */
3281 	if (ifc->ifc_len <= 0)
3282 		return (EINVAL);
3283 
3284 again:
3285 	if (ifc->ifc_len <= max_len) {
3286 		max_len = ifc->ifc_len;
3287 		full = 1;
3288 	}
3289 	sb = sbuf_new(NULL, NULL, max_len + 1, SBUF_FIXEDLEN);
3290 	max_len = 0;
3291 	valid_len = 0;
3292 
3293 	IFNET_RLOCK();
3294 	CK_STAILQ_FOREACH(ifp, &V_ifnet, if_link) {
3295 		struct epoch_tracker et;
3296 		int addrs;
3297 
3298 		/*
3299 		 * Zero the ifr to make sure we don't disclose the contents
3300 		 * of the stack.
3301 		 */
3302 		memset(&ifr, 0, sizeof(ifr));
3303 
3304 		if (strlcpy(ifr.ifr_name, ifp->if_xname, sizeof(ifr.ifr_name))
3305 		    >= sizeof(ifr.ifr_name)) {
3306 			sbuf_delete(sb);
3307 			IFNET_RUNLOCK();
3308 			return (ENAMETOOLONG);
3309 		}
3310 
3311 		addrs = 0;
3312 		NET_EPOCH_ENTER(et);
3313 		CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
3314 			struct sockaddr *sa = ifa->ifa_addr;
3315 
3316 			if (prison_if(curthread->td_ucred, sa) != 0)
3317 				continue;
3318 			addrs++;
3319 			if (sa->sa_len <= sizeof(*sa)) {
3320 				if (sa->sa_len < sizeof(*sa)) {
3321 					memset(&ifr.ifr_ifru.ifru_addr, 0,
3322 					    sizeof(ifr.ifr_ifru.ifru_addr));
3323 					memcpy(&ifr.ifr_ifru.ifru_addr, sa,
3324 					    sa->sa_len);
3325 				} else
3326 					ifr.ifr_ifru.ifru_addr = *sa;
3327 				sbuf_bcat(sb, &ifr, sizeof(ifr));
3328 				max_len += sizeof(ifr);
3329 			} else {
3330 				sbuf_bcat(sb, &ifr,
3331 				    offsetof(struct ifreq, ifr_addr));
3332 				max_len += offsetof(struct ifreq, ifr_addr);
3333 				sbuf_bcat(sb, sa, sa->sa_len);
3334 				max_len += sa->sa_len;
3335 			}
3336 
3337 			if (sbuf_error(sb) == 0)
3338 				valid_len = sbuf_len(sb);
3339 		}
3340 		NET_EPOCH_EXIT(et);
3341 		if (addrs == 0) {
3342 			sbuf_bcat(sb, &ifr, sizeof(ifr));
3343 			max_len += sizeof(ifr);
3344 
3345 			if (sbuf_error(sb) == 0)
3346 				valid_len = sbuf_len(sb);
3347 		}
3348 	}
3349 	IFNET_RUNLOCK();
3350 
3351 	/*
3352 	 * If we didn't allocate enough space (uncommon), try again.  If
3353 	 * we have already allocated as much space as we are allowed,
3354 	 * return what we've got.
3355 	 */
3356 	if (valid_len != max_len && !full) {
3357 		sbuf_delete(sb);
3358 		goto again;
3359 	}
3360 
3361 	ifc->ifc_len = valid_len;
3362 	sbuf_finish(sb);
3363 	error = copyout(sbuf_data(sb), ifc->ifc_req, ifc->ifc_len);
3364 	sbuf_delete(sb);
3365 	return (error);
3366 }
3367 
3368 /*
3369  * Just like ifpromisc(), but for all-multicast-reception mode.
3370  */
3371 int
if_allmulti(struct ifnet * ifp,int onswitch)3372 if_allmulti(struct ifnet *ifp, int onswitch)
3373 {
3374 
3375 	return (if_setflag(ifp, IFF_ALLMULTI, IFF_PALLMULTI, &ifp->if_amcount,
3376 	    onswitch));
3377 }
3378 
3379 struct ifmultiaddr *
if_findmulti(struct ifnet * ifp,const struct sockaddr * sa)3380 if_findmulti(struct ifnet *ifp, const struct sockaddr *sa)
3381 {
3382 	struct ifmultiaddr *ifma;
3383 
3384 	IF_ADDR_LOCK_ASSERT(ifp);
3385 
3386 	CK_STAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
3387 		if (sa->sa_family == AF_LINK) {
3388 			if (sa_dl_equal(ifma->ifma_addr, sa))
3389 				break;
3390 		} else {
3391 			if (sa_equal(ifma->ifma_addr, sa))
3392 				break;
3393 		}
3394 	}
3395 
3396 	return ifma;
3397 }
3398 
3399 /*
3400  * Allocate a new ifmultiaddr and initialize based on passed arguments.  We
3401  * make copies of passed sockaddrs.  The ifmultiaddr will not be added to
3402  * the ifnet multicast address list here, so the caller must do that and
3403  * other setup work (such as notifying the device driver).  The reference
3404  * count is initialized to 1.
3405  */
3406 static struct ifmultiaddr *
if_allocmulti(struct ifnet * ifp,struct sockaddr * sa,struct sockaddr * llsa,int mflags)3407 if_allocmulti(struct ifnet *ifp, struct sockaddr *sa, struct sockaddr *llsa,
3408     int mflags)
3409 {
3410 	struct ifmultiaddr *ifma;
3411 	struct sockaddr *dupsa;
3412 
3413 	ifma = malloc(sizeof *ifma, M_IFMADDR, mflags |
3414 	    M_ZERO);
3415 	if (ifma == NULL)
3416 		return (NULL);
3417 
3418 	dupsa = malloc(sa->sa_len, M_IFMADDR, mflags);
3419 	if (dupsa == NULL) {
3420 		free(ifma, M_IFMADDR);
3421 		return (NULL);
3422 	}
3423 	bcopy(sa, dupsa, sa->sa_len);
3424 	ifma->ifma_addr = dupsa;
3425 
3426 	ifma->ifma_ifp = ifp;
3427 	ifma->ifma_refcount = 1;
3428 	ifma->ifma_protospec = NULL;
3429 
3430 	if (llsa == NULL) {
3431 		ifma->ifma_lladdr = NULL;
3432 		return (ifma);
3433 	}
3434 
3435 	dupsa = malloc(llsa->sa_len, M_IFMADDR, mflags);
3436 	if (dupsa == NULL) {
3437 		free(ifma->ifma_addr, M_IFMADDR);
3438 		free(ifma, M_IFMADDR);
3439 		return (NULL);
3440 	}
3441 	bcopy(llsa, dupsa, llsa->sa_len);
3442 	ifma->ifma_lladdr = dupsa;
3443 
3444 	return (ifma);
3445 }
3446 
3447 /*
3448  * if_freemulti: free ifmultiaddr structure and possibly attached related
3449  * addresses.  The caller is responsible for implementing reference
3450  * counting, notifying the driver, handling routing messages, and releasing
3451  * any dependent link layer state.
3452  */
3453 #ifdef MCAST_VERBOSE
3454 extern void kdb_backtrace(void);
3455 #endif
3456 static void
if_freemulti_internal(struct ifmultiaddr * ifma)3457 if_freemulti_internal(struct ifmultiaddr *ifma)
3458 {
3459 
3460 	KASSERT(ifma->ifma_refcount == 0, ("if_freemulti: refcount %d",
3461 	    ifma->ifma_refcount));
3462 
3463 	if (ifma->ifma_lladdr != NULL)
3464 		free(ifma->ifma_lladdr, M_IFMADDR);
3465 #ifdef MCAST_VERBOSE
3466 	kdb_backtrace();
3467 	printf("%s freeing ifma: %p\n", __func__, ifma);
3468 #endif
3469 	free(ifma->ifma_addr, M_IFMADDR);
3470 	free(ifma, M_IFMADDR);
3471 }
3472 
3473 static void
if_destroymulti(epoch_context_t ctx)3474 if_destroymulti(epoch_context_t ctx)
3475 {
3476 	struct ifmultiaddr *ifma;
3477 
3478 	ifma = __containerof(ctx, struct ifmultiaddr, ifma_epoch_ctx);
3479 	if_freemulti_internal(ifma);
3480 }
3481 
3482 void
if_freemulti(struct ifmultiaddr * ifma)3483 if_freemulti(struct ifmultiaddr *ifma)
3484 {
3485 	KASSERT(ifma->ifma_refcount == 0, ("if_freemulti_epoch: refcount %d",
3486 	    ifma->ifma_refcount));
3487 
3488 	NET_EPOCH_CALL(if_destroymulti, &ifma->ifma_epoch_ctx);
3489 }
3490 
3491 /*
3492  * Register an additional multicast address with a network interface.
3493  *
3494  * - If the address is already present, bump the reference count on the
3495  *   address and return.
3496  * - If the address is not link-layer, look up a link layer address.
3497  * - Allocate address structures for one or both addresses, and attach to the
3498  *   multicast address list on the interface.  If automatically adding a link
3499  *   layer address, the protocol address will own a reference to the link
3500  *   layer address, to be freed when it is freed.
3501  * - Notify the network device driver of an addition to the multicast address
3502  *   list.
3503  *
3504  * 'sa' points to caller-owned memory with the desired multicast address.
3505  *
3506  * 'retifma' will be used to return a pointer to the resulting multicast
3507  * address reference, if desired.
3508  */
3509 int
if_addmulti(struct ifnet * ifp,struct sockaddr * sa,struct ifmultiaddr ** retifma)3510 if_addmulti(struct ifnet *ifp, struct sockaddr *sa,
3511     struct ifmultiaddr **retifma)
3512 {
3513 	struct ifmultiaddr *ifma, *ll_ifma;
3514 	struct sockaddr *llsa;
3515 	struct sockaddr_dl sdl;
3516 	int error;
3517 
3518 #ifdef INET
3519 	IN_MULTI_LIST_UNLOCK_ASSERT();
3520 #endif
3521 #ifdef INET6
3522 	IN6_MULTI_LIST_UNLOCK_ASSERT();
3523 #endif
3524 	/*
3525 	 * If the address is already present, return a new reference to it;
3526 	 * otherwise, allocate storage and set up a new address.
3527 	 */
3528 	IF_ADDR_WLOCK(ifp);
3529 	ifma = if_findmulti(ifp, sa);
3530 	if (ifma != NULL) {
3531 		ifma->ifma_refcount++;
3532 		if (retifma != NULL)
3533 			*retifma = ifma;
3534 		IF_ADDR_WUNLOCK(ifp);
3535 		return (0);
3536 	}
3537 
3538 	/*
3539 	 * The address isn't already present; resolve the protocol address
3540 	 * into a link layer address, and then look that up, bump its
3541 	 * refcount or allocate an ifma for that also.
3542 	 * Most link layer resolving functions returns address data which
3543 	 * fits inside default sockaddr_dl structure. However callback
3544 	 * can allocate another sockaddr structure, in that case we need to
3545 	 * free it later.
3546 	 */
3547 	llsa = NULL;
3548 	ll_ifma = NULL;
3549 	if (ifp->if_resolvemulti != NULL) {
3550 		/* Provide called function with buffer size information */
3551 		sdl.sdl_len = sizeof(sdl);
3552 		llsa = (struct sockaddr *)&sdl;
3553 		error = ifp->if_resolvemulti(ifp, &llsa, sa);
3554 		if (error)
3555 			goto unlock_out;
3556 	}
3557 
3558 	/*
3559 	 * Allocate the new address.  Don't hook it up yet, as we may also
3560 	 * need to allocate a link layer multicast address.
3561 	 */
3562 	ifma = if_allocmulti(ifp, sa, llsa, M_NOWAIT);
3563 	if (ifma == NULL) {
3564 		error = ENOMEM;
3565 		goto free_llsa_out;
3566 	}
3567 
3568 	/*
3569 	 * If a link layer address is found, we'll need to see if it's
3570 	 * already present in the address list, or allocate is as well.
3571 	 * When this block finishes, the link layer address will be on the
3572 	 * list.
3573 	 */
3574 	if (llsa != NULL) {
3575 		ll_ifma = if_findmulti(ifp, llsa);
3576 		if (ll_ifma == NULL) {
3577 			ll_ifma = if_allocmulti(ifp, llsa, NULL, M_NOWAIT);
3578 			if (ll_ifma == NULL) {
3579 				--ifma->ifma_refcount;
3580 				if_freemulti(ifma);
3581 				error = ENOMEM;
3582 				goto free_llsa_out;
3583 			}
3584 			ll_ifma->ifma_flags |= IFMA_F_ENQUEUED;
3585 			CK_STAILQ_INSERT_HEAD(&ifp->if_multiaddrs, ll_ifma,
3586 			    ifma_link);
3587 		} else
3588 			ll_ifma->ifma_refcount++;
3589 		ifma->ifma_llifma = ll_ifma;
3590 	}
3591 
3592 	/*
3593 	 * We now have a new multicast address, ifma, and possibly a new or
3594 	 * referenced link layer address.  Add the primary address to the
3595 	 * ifnet address list.
3596 	 */
3597 	ifma->ifma_flags |= IFMA_F_ENQUEUED;
3598 	CK_STAILQ_INSERT_HEAD(&ifp->if_multiaddrs, ifma, ifma_link);
3599 
3600 	if (retifma != NULL)
3601 		*retifma = ifma;
3602 
3603 	/*
3604 	 * Must generate the message while holding the lock so that 'ifma'
3605 	 * pointer is still valid.
3606 	 */
3607 	rt_newmaddrmsg(RTM_NEWMADDR, ifma);
3608 	IF_ADDR_WUNLOCK(ifp);
3609 
3610 	/*
3611 	 * We are certain we have added something, so call down to the
3612 	 * interface to let them know about it.
3613 	 */
3614 	if (ifp->if_ioctl != NULL) {
3615 		if (THREAD_CAN_SLEEP())
3616 			(void )(*ifp->if_ioctl)(ifp, SIOCADDMULTI, 0);
3617 		else
3618 			taskqueue_enqueue(taskqueue_swi, &ifp->if_addmultitask);
3619 	}
3620 
3621 	if ((llsa != NULL) && (llsa != (struct sockaddr *)&sdl))
3622 		link_free_sdl(llsa);
3623 
3624 	return (0);
3625 
3626 free_llsa_out:
3627 	if ((llsa != NULL) && (llsa != (struct sockaddr *)&sdl))
3628 		link_free_sdl(llsa);
3629 
3630 unlock_out:
3631 	IF_ADDR_WUNLOCK(ifp);
3632 	return (error);
3633 }
3634 
3635 static void
if_siocaddmulti(void * arg,int pending)3636 if_siocaddmulti(void *arg, int pending)
3637 {
3638 	struct ifnet *ifp;
3639 
3640 	ifp = arg;
3641 #ifdef DIAGNOSTIC
3642 	if (pending > 1)
3643 		if_printf(ifp, "%d SIOCADDMULTI coalesced\n", pending);
3644 #endif
3645 	CURVNET_SET(ifp->if_vnet);
3646 	(void )(*ifp->if_ioctl)(ifp, SIOCADDMULTI, 0);
3647 	CURVNET_RESTORE();
3648 }
3649 
3650 /*
3651  * Delete a multicast group membership by network-layer group address.
3652  *
3653  * Returns ENOENT if the entry could not be found. If ifp no longer
3654  * exists, results are undefined. This entry point should only be used
3655  * from subsystems which do appropriate locking to hold ifp for the
3656  * duration of the call.
3657  * Network-layer protocol domains must use if_delmulti_ifma().
3658  */
3659 int
if_delmulti(struct ifnet * ifp,struct sockaddr * sa)3660 if_delmulti(struct ifnet *ifp, struct sockaddr *sa)
3661 {
3662 	struct ifmultiaddr *ifma;
3663 	int lastref;
3664 
3665 	KASSERT(ifp, ("%s: NULL ifp", __func__));
3666 
3667 	IF_ADDR_WLOCK(ifp);
3668 	lastref = 0;
3669 	ifma = if_findmulti(ifp, sa);
3670 	if (ifma != NULL)
3671 		lastref = if_delmulti_locked(ifp, ifma, 0);
3672 	IF_ADDR_WUNLOCK(ifp);
3673 
3674 	if (ifma == NULL)
3675 		return (ENOENT);
3676 
3677 	if (lastref && ifp->if_ioctl != NULL) {
3678 		(void)(*ifp->if_ioctl)(ifp, SIOCDELMULTI, 0);
3679 	}
3680 
3681 	return (0);
3682 }
3683 
3684 /*
3685  * Delete all multicast group membership for an interface.
3686  * Should be used to quickly flush all multicast filters.
3687  */
3688 void
if_delallmulti(struct ifnet * ifp)3689 if_delallmulti(struct ifnet *ifp)
3690 {
3691 	struct ifmultiaddr *ifma;
3692 	struct ifmultiaddr *next;
3693 
3694 	IF_ADDR_WLOCK(ifp);
3695 	CK_STAILQ_FOREACH_SAFE(ifma, &ifp->if_multiaddrs, ifma_link, next)
3696 		if_delmulti_locked(ifp, ifma, 0);
3697 	IF_ADDR_WUNLOCK(ifp);
3698 }
3699 
3700 void
if_delmulti_ifma(struct ifmultiaddr * ifma)3701 if_delmulti_ifma(struct ifmultiaddr *ifma)
3702 {
3703 	if_delmulti_ifma_flags(ifma, 0);
3704 }
3705 
3706 /*
3707  * Delete a multicast group membership by group membership pointer.
3708  * Network-layer protocol domains must use this routine.
3709  *
3710  * It is safe to call this routine if the ifp disappeared.
3711  */
3712 void
if_delmulti_ifma_flags(struct ifmultiaddr * ifma,int flags)3713 if_delmulti_ifma_flags(struct ifmultiaddr *ifma, int flags)
3714 {
3715 	struct ifnet *ifp;
3716 	int lastref;
3717 	MCDPRINTF("%s freeing ifma: %p\n", __func__, ifma);
3718 #ifdef INET
3719 	IN_MULTI_LIST_UNLOCK_ASSERT();
3720 #endif
3721 	ifp = ifma->ifma_ifp;
3722 #ifdef DIAGNOSTIC
3723 	if (ifp == NULL) {
3724 		printf("%s: ifma_ifp seems to be detached\n", __func__);
3725 	} else {
3726 		struct epoch_tracker et;
3727 		struct ifnet *oifp;
3728 
3729 		NET_EPOCH_ENTER(et);
3730 		CK_STAILQ_FOREACH(oifp, &V_ifnet, if_link)
3731 			if (ifp == oifp)
3732 				break;
3733 		NET_EPOCH_EXIT(et);
3734 		if (ifp != oifp)
3735 			ifp = NULL;
3736 	}
3737 #endif
3738 	/*
3739 	 * If and only if the ifnet instance exists: Acquire the address lock.
3740 	 */
3741 	if (ifp != NULL)
3742 		IF_ADDR_WLOCK(ifp);
3743 
3744 	lastref = if_delmulti_locked(ifp, ifma, flags);
3745 
3746 	if (ifp != NULL) {
3747 		/*
3748 		 * If and only if the ifnet instance exists:
3749 		 *  Release the address lock.
3750 		 *  If the group was left: update the hardware hash filter.
3751 		 */
3752 		IF_ADDR_WUNLOCK(ifp);
3753 		if (lastref && ifp->if_ioctl != NULL) {
3754 			(void)(*ifp->if_ioctl)(ifp, SIOCDELMULTI, 0);
3755 		}
3756 	}
3757 }
3758 
3759 /*
3760  * Perform deletion of network-layer and/or link-layer multicast address.
3761  *
3762  * Return 0 if the reference count was decremented.
3763  * Return 1 if the final reference was released, indicating that the
3764  * hardware hash filter should be reprogrammed.
3765  */
3766 static int
if_delmulti_locked(struct ifnet * ifp,struct ifmultiaddr * ifma,int detaching)3767 if_delmulti_locked(struct ifnet *ifp, struct ifmultiaddr *ifma, int detaching)
3768 {
3769 	struct ifmultiaddr *ll_ifma;
3770 
3771 	if (ifp != NULL && ifma->ifma_ifp != NULL) {
3772 		KASSERT(ifma->ifma_ifp == ifp,
3773 		    ("%s: inconsistent ifp %p", __func__, ifp));
3774 		IF_ADDR_WLOCK_ASSERT(ifp);
3775 	}
3776 
3777 	ifp = ifma->ifma_ifp;
3778 	MCDPRINTF("%s freeing %p from %s \n", __func__, ifma, ifp ? ifp->if_xname : "");
3779 
3780 	/*
3781 	 * If the ifnet is detaching, null out references to ifnet,
3782 	 * so that upper protocol layers will notice, and not attempt
3783 	 * to obtain locks for an ifnet which no longer exists. The
3784 	 * routing socket announcement must happen before the ifnet
3785 	 * instance is detached from the system.
3786 	 */
3787 	if (detaching) {
3788 #ifdef DIAGNOSTIC
3789 		printf("%s: detaching ifnet instance %p\n", __func__, ifp);
3790 #endif
3791 		/*
3792 		 * ifp may already be nulled out if we are being reentered
3793 		 * to delete the ll_ifma.
3794 		 */
3795 		if (ifp != NULL) {
3796 			rt_newmaddrmsg(RTM_DELMADDR, ifma);
3797 			ifma->ifma_ifp = NULL;
3798 		}
3799 	}
3800 
3801 	if (--ifma->ifma_refcount > 0)
3802 		return 0;
3803 
3804 	if (ifp != NULL && detaching == 0 && (ifma->ifma_flags & IFMA_F_ENQUEUED)) {
3805 		CK_STAILQ_REMOVE(&ifp->if_multiaddrs, ifma, ifmultiaddr, ifma_link);
3806 		ifma->ifma_flags &= ~IFMA_F_ENQUEUED;
3807 	}
3808 	/*
3809 	 * If this ifma is a network-layer ifma, a link-layer ifma may
3810 	 * have been associated with it. Release it first if so.
3811 	 */
3812 	ll_ifma = ifma->ifma_llifma;
3813 	if (ll_ifma != NULL) {
3814 		KASSERT(ifma->ifma_lladdr != NULL,
3815 		    ("%s: llifma w/o lladdr", __func__));
3816 		if (detaching)
3817 			ll_ifma->ifma_ifp = NULL;	/* XXX */
3818 		if (--ll_ifma->ifma_refcount == 0) {
3819 			if (ifp != NULL) {
3820 				if (ll_ifma->ifma_flags & IFMA_F_ENQUEUED) {
3821 					CK_STAILQ_REMOVE(&ifp->if_multiaddrs, ll_ifma, ifmultiaddr,
3822 						ifma_link);
3823 					ll_ifma->ifma_flags &= ~IFMA_F_ENQUEUED;
3824 				}
3825 			}
3826 			if_freemulti(ll_ifma);
3827 		}
3828 	}
3829 #ifdef INVARIANTS
3830 	if (ifp) {
3831 		struct ifmultiaddr *ifmatmp;
3832 
3833 		CK_STAILQ_FOREACH(ifmatmp, &ifp->if_multiaddrs, ifma_link)
3834 			MPASS(ifma != ifmatmp);
3835 	}
3836 #endif
3837 	if_freemulti(ifma);
3838 	/*
3839 	 * The last reference to this instance of struct ifmultiaddr
3840 	 * was released; the hardware should be notified of this change.
3841 	 */
3842 	return 1;
3843 }
3844 
3845 /*
3846  * Set the link layer address on an interface.
3847  *
3848  * At this time we only support certain types of interfaces,
3849  * and we don't allow the length of the address to change.
3850  *
3851  * Set noinline to be dtrace-friendly
3852  */
3853 __noinline int
if_setlladdr(struct ifnet * ifp,const u_char * lladdr,int len)3854 if_setlladdr(struct ifnet *ifp, const u_char *lladdr, int len)
3855 {
3856 	struct sockaddr_dl *sdl;
3857 	struct ifaddr *ifa;
3858 	struct ifreq ifr;
3859 
3860 	ifa = ifp->if_addr;
3861 	if (ifa == NULL)
3862 		return (EINVAL);
3863 
3864 	sdl = (struct sockaddr_dl *)ifa->ifa_addr;
3865 	if (sdl == NULL)
3866 		return (EINVAL);
3867 
3868 	if (len != sdl->sdl_alen)	/* don't allow length to change */
3869 		return (EINVAL);
3870 
3871 	switch (ifp->if_type) {
3872 	case IFT_ETHER:
3873 	case IFT_XETHER:
3874 	case IFT_L2VLAN:
3875 	case IFT_BRIDGE:
3876 	case IFT_IEEE8023ADLAG:
3877 		bcopy(lladdr, LLADDR(sdl), len);
3878 		break;
3879 	default:
3880 		return (ENODEV);
3881 	}
3882 
3883 	/*
3884 	 * If the interface is already up, we need
3885 	 * to re-init it in order to reprogram its
3886 	 * address filter.
3887 	 */
3888 	if ((ifp->if_flags & IFF_UP) != 0) {
3889 		if (ifp->if_ioctl) {
3890 			ifp->if_flags &= ~IFF_UP;
3891 			ifr.ifr_flags = ifp->if_flags & 0xffff;
3892 			ifr.ifr_flagshigh = ifp->if_flags >> 16;
3893 			(*ifp->if_ioctl)(ifp, SIOCSIFFLAGS, (caddr_t)&ifr);
3894 			ifp->if_flags |= IFF_UP;
3895 			ifr.ifr_flags = ifp->if_flags & 0xffff;
3896 			ifr.ifr_flagshigh = ifp->if_flags >> 16;
3897 			(*ifp->if_ioctl)(ifp, SIOCSIFFLAGS, (caddr_t)&ifr);
3898 		}
3899 	}
3900 	EVENTHANDLER_INVOKE(iflladdr_event, ifp);
3901 
3902 	return (0);
3903 }
3904 
3905 /*
3906  * Compat function for handling basic encapsulation requests.
3907  * Not converted stacks (FDDI, IB, ..) supports traditional
3908  * output model: ARP (and other similar L2 protocols) are handled
3909  * inside output routine, arpresolve/nd6_resolve() returns MAC
3910  * address instead of full prepend.
3911  *
3912  * This function creates calculated header==MAC for IPv4/IPv6 and
3913  * returns EAFNOSUPPORT (which is then handled in ARP code) for other
3914  * address families.
3915  */
3916 static int
if_requestencap_default(struct ifnet * ifp,struct if_encap_req * req)3917 if_requestencap_default(struct ifnet *ifp, struct if_encap_req *req)
3918 {
3919 	if (req->rtype != IFENCAP_LL)
3920 		return (EOPNOTSUPP);
3921 
3922 	if (req->bufsize < req->lladdr_len)
3923 		return (ENOMEM);
3924 
3925 	switch (req->family) {
3926 	case AF_INET:
3927 	case AF_INET6:
3928 		break;
3929 	default:
3930 		return (EAFNOSUPPORT);
3931 	}
3932 
3933 	/* Copy lladdr to storage as is */
3934 	memmove(req->buf, req->lladdr, req->lladdr_len);
3935 	req->bufsize = req->lladdr_len;
3936 	req->lladdr_off = 0;
3937 
3938 	return (0);
3939 }
3940 
3941 /*
3942  * Tunnel interfaces can nest, also they may cause infinite recursion
3943  * calls when misconfigured. We'll prevent this by detecting loops.
3944  * High nesting level may cause stack exhaustion. We'll prevent this
3945  * by introducing upper limit.
3946  *
3947  * Return 0, if tunnel nesting count is equal or less than limit.
3948  */
3949 int
if_tunnel_check_nesting(struct ifnet * ifp,struct mbuf * m,uint32_t cookie,int limit)3950 if_tunnel_check_nesting(struct ifnet *ifp, struct mbuf *m, uint32_t cookie,
3951     int limit)
3952 {
3953 	struct m_tag *mtag;
3954 	int count;
3955 
3956 	count = 1;
3957 	mtag = NULL;
3958 	while ((mtag = m_tag_locate(m, cookie, 0, mtag)) != NULL) {
3959 		if (*(struct ifnet **)(mtag + 1) == ifp) {
3960 			log(LOG_NOTICE, "%s: loop detected\n", if_name(ifp));
3961 			return (EIO);
3962 		}
3963 		count++;
3964 	}
3965 	if (count > limit) {
3966 		log(LOG_NOTICE,
3967 		    "%s: if_output recursively called too many times(%d)\n",
3968 		    if_name(ifp), count);
3969 		return (EIO);
3970 	}
3971 	mtag = m_tag_alloc(cookie, 0, sizeof(struct ifnet *), M_NOWAIT);
3972 	if (mtag == NULL)
3973 		return (ENOMEM);
3974 	*(struct ifnet **)(mtag + 1) = ifp;
3975 	m_tag_prepend(m, mtag);
3976 	return (0);
3977 }
3978 
3979 /*
3980  * Get the link layer address that was read from the hardware at attach.
3981  *
3982  * This is only set by Ethernet NICs (IFT_ETHER), but laggX interfaces re-type
3983  * their component interfaces as IFT_IEEE8023ADLAG.
3984  */
3985 int
if_gethwaddr(struct ifnet * ifp,struct ifreq * ifr)3986 if_gethwaddr(struct ifnet *ifp, struct ifreq *ifr)
3987 {
3988 	if (ifp->if_hw_addr == NULL)
3989 		return (ENODEV);
3990 
3991 	switch (ifp->if_type) {
3992 	case IFT_ETHER:
3993 	case IFT_IEEE8023ADLAG:
3994 		bcopy(ifp->if_hw_addr, ifr->ifr_addr.sa_data, ifp->if_addrlen);
3995 		return (0);
3996 	default:
3997 		return (ENODEV);
3998 	}
3999 }
4000 
4001 /*
4002  * The name argument must be a pointer to storage which will last as
4003  * long as the interface does.  For physical devices, the result of
4004  * device_get_name(dev) is a good choice and for pseudo-devices a
4005  * static string works well.
4006  */
4007 void
if_initname(struct ifnet * ifp,const char * name,int unit)4008 if_initname(struct ifnet *ifp, const char *name, int unit)
4009 {
4010 	ifp->if_dname = name;
4011 	ifp->if_dunit = unit;
4012 	if (unit != IF_DUNIT_NONE)
4013 		snprintf(ifp->if_xname, IFNAMSIZ, "%s%d", name, unit);
4014 	else
4015 		strlcpy(ifp->if_xname, name, IFNAMSIZ);
4016 }
4017 
4018 static int
if_vlog(struct ifnet * ifp,int pri,const char * fmt,va_list ap)4019 if_vlog(struct ifnet *ifp, int pri, const char *fmt, va_list ap)
4020 {
4021 	char if_fmt[256];
4022 
4023 	snprintf(if_fmt, sizeof(if_fmt), "%s: %s", ifp->if_xname, fmt);
4024 	vlog(pri, if_fmt, ap);
4025 	return (0);
4026 }
4027 
4028 
4029 int
if_printf(struct ifnet * ifp,const char * fmt,...)4030 if_printf(struct ifnet *ifp, const char *fmt, ...)
4031 {
4032 	va_list ap;
4033 
4034 	va_start(ap, fmt);
4035 	if_vlog(ifp, LOG_INFO, fmt, ap);
4036 	va_end(ap);
4037 	return (0);
4038 }
4039 
4040 int
if_log(struct ifnet * ifp,int pri,const char * fmt,...)4041 if_log(struct ifnet *ifp, int pri, const char *fmt, ...)
4042 {
4043 	va_list ap;
4044 
4045 	va_start(ap, fmt);
4046 	if_vlog(ifp, pri, fmt, ap);
4047 	va_end(ap);
4048 	return (0);
4049 }
4050 
4051 void
if_start(struct ifnet * ifp)4052 if_start(struct ifnet *ifp)
4053 {
4054 
4055 	(*(ifp)->if_start)(ifp);
4056 }
4057 
4058 /*
4059  * Backwards compatibility interface for drivers
4060  * that have not implemented it
4061  */
4062 static int
if_transmit_default(struct ifnet * ifp,struct mbuf * m)4063 if_transmit_default(struct ifnet *ifp, struct mbuf *m)
4064 {
4065 	int error;
4066 
4067 	IFQ_HANDOFF(ifp, m, error);
4068 	return (error);
4069 }
4070 
4071 static void
if_input_default(struct ifnet * ifp __unused,struct mbuf * m)4072 if_input_default(struct ifnet *ifp __unused, struct mbuf *m)
4073 {
4074 	m_freem(m);
4075 }
4076 
4077 int
if_handoff(struct ifqueue * ifq,struct mbuf * m,struct ifnet * ifp,int adjust)4078 if_handoff(struct ifqueue *ifq, struct mbuf *m, struct ifnet *ifp, int adjust)
4079 {
4080 	int active = 0;
4081 
4082 	IF_LOCK(ifq);
4083 	if (_IF_QFULL(ifq)) {
4084 		IF_UNLOCK(ifq);
4085 		if_inc_counter(ifp, IFCOUNTER_OQDROPS, 1);
4086 		m_freem(m);
4087 		return (0);
4088 	}
4089 	if (ifp != NULL) {
4090 		if_inc_counter(ifp, IFCOUNTER_OBYTES, m->m_pkthdr.len + adjust);
4091 		if (m->m_flags & (M_BCAST|M_MCAST))
4092 			if_inc_counter(ifp, IFCOUNTER_OMCASTS, 1);
4093 		active = ifp->if_drv_flags & IFF_DRV_OACTIVE;
4094 	}
4095 	_IF_ENQUEUE(ifq, m);
4096 	IF_UNLOCK(ifq);
4097 	if (ifp != NULL && !active)
4098 		(*(ifp)->if_start)(ifp);
4099 	return (1);
4100 }
4101 
4102 void
if_register_com_alloc(u_char type,if_com_alloc_t * a,if_com_free_t * f)4103 if_register_com_alloc(u_char type,
4104     if_com_alloc_t *a, if_com_free_t *f)
4105 {
4106 
4107 	KASSERT(if_com_alloc[type] == NULL,
4108 	    ("if_register_com_alloc: %d already registered", type));
4109 	KASSERT(if_com_free[type] == NULL,
4110 	    ("if_register_com_alloc: %d free already registered", type));
4111 
4112 	if_com_alloc[type] = a;
4113 	if_com_free[type] = f;
4114 }
4115 
4116 void
if_deregister_com_alloc(u_char type)4117 if_deregister_com_alloc(u_char type)
4118 {
4119 
4120 	KASSERT(if_com_alloc[type] != NULL,
4121 	    ("if_deregister_com_alloc: %d not registered", type));
4122 	KASSERT(if_com_free[type] != NULL,
4123 	    ("if_deregister_com_alloc: %d free not registered", type));
4124 
4125 	/*
4126 	 * Ensure all pending EPOCH(9) callbacks have been executed. This
4127 	 * fixes issues about late invocation of if_destroy(), which leads
4128 	 * to memory leak from if_com_alloc[type] allocated if_l2com.
4129 	 */
4130 	NET_EPOCH_DRAIN_CALLBACKS();
4131 
4132 	if_com_alloc[type] = NULL;
4133 	if_com_free[type] = NULL;
4134 }
4135 
4136 /* API for driver access to network stack owned ifnet.*/
4137 uint64_t
if_setbaudrate(struct ifnet * ifp,uint64_t baudrate)4138 if_setbaudrate(struct ifnet *ifp, uint64_t baudrate)
4139 {
4140 	uint64_t oldbrate;
4141 
4142 	oldbrate = ifp->if_baudrate;
4143 	ifp->if_baudrate = baudrate;
4144 	return (oldbrate);
4145 }
4146 
4147 uint64_t
if_getbaudrate(const if_t ifp)4148 if_getbaudrate(const if_t ifp)
4149 {
4150 	return (ifp->if_baudrate);
4151 }
4152 
4153 int
if_setcapabilities(if_t ifp,int capabilities)4154 if_setcapabilities(if_t ifp, int capabilities)
4155 {
4156 	ifp->if_capabilities = capabilities;
4157 	return (0);
4158 }
4159 
4160 int
if_setcapabilitiesbit(if_t ifp,int setbit,int clearbit)4161 if_setcapabilitiesbit(if_t ifp, int setbit, int clearbit)
4162 {
4163 	ifp->if_capabilities &= ~clearbit;
4164 	ifp->if_capabilities |= setbit;
4165 	return (0);
4166 }
4167 
4168 int
if_getcapabilities(const if_t ifp)4169 if_getcapabilities(const if_t ifp)
4170 {
4171 	return (ifp->if_capabilities);
4172 }
4173 
4174 int
if_setcapenable(if_t ifp,int capabilities)4175 if_setcapenable(if_t ifp, int capabilities)
4176 {
4177 	ifp->if_capenable = capabilities;
4178 	return (0);
4179 }
4180 
4181 int
if_setcapenablebit(if_t ifp,int setcap,int clearcap)4182 if_setcapenablebit(if_t ifp, int setcap, int clearcap)
4183 {
4184 	ifp->if_capenable &= ~clearcap;
4185 	ifp->if_capenable |= setcap;
4186 	return (0);
4187 }
4188 
4189 int
if_setcapabilities2(if_t ifp,int capabilities)4190 if_setcapabilities2(if_t ifp, int capabilities)
4191 {
4192 	ifp->if_capabilities2 = capabilities;
4193 	return (0);
4194 }
4195 
4196 int
if_setcapabilities2bit(if_t ifp,int setbit,int clearbit)4197 if_setcapabilities2bit(if_t ifp, int setbit, int clearbit)
4198 {
4199 	ifp->if_capabilities2 &= ~clearbit;
4200 	ifp->if_capabilities2 |= setbit;
4201 	return (0);
4202 }
4203 
4204 int
if_getcapabilities2(const if_t ifp)4205 if_getcapabilities2(const if_t ifp)
4206 {
4207 	return (ifp->if_capabilities2);
4208 }
4209 
4210 int
if_setcapenable2(if_t ifp,int capabilities2)4211 if_setcapenable2(if_t ifp, int capabilities2)
4212 {
4213 	ifp->if_capenable2 = capabilities2;
4214 	return (0);
4215 }
4216 
4217 int
if_setcapenable2bit(if_t ifp,int setcap,int clearcap)4218 if_setcapenable2bit(if_t ifp, int setcap, int clearcap)
4219 {
4220 	ifp->if_capenable2 &= ~clearcap;
4221 	ifp->if_capenable2 |= setcap;
4222 	return (0);
4223 }
4224 
4225 const char *
if_getdname(const if_t ifp)4226 if_getdname(const if_t ifp)
4227 {
4228 	return (ifp->if_dname);
4229 }
4230 
4231 void
if_setdname(if_t ifp,const char * dname)4232 if_setdname(if_t ifp, const char *dname)
4233 {
4234 	ifp->if_dname = dname;
4235 }
4236 
4237 const char *
if_name(if_t ifp)4238 if_name(if_t ifp)
4239 {
4240 	return (ifp->if_xname);
4241 }
4242 
4243 int
if_setname(if_t ifp,const char * name)4244 if_setname(if_t ifp, const char *name)
4245 {
4246 	if (strlen(name) > sizeof(ifp->if_xname) - 1)
4247 		return (ENAMETOOLONG);
4248 	strcpy(ifp->if_xname, name);
4249 
4250 	return (0);
4251 }
4252 
4253 int
if_togglecapenable(if_t ifp,int togglecap)4254 if_togglecapenable(if_t ifp, int togglecap)
4255 {
4256 	ifp->if_capenable ^= togglecap;
4257 	return (0);
4258 }
4259 
4260 int
if_getcapenable(const if_t ifp)4261 if_getcapenable(const if_t ifp)
4262 {
4263 	return (ifp->if_capenable);
4264 }
4265 
4266 int
if_togglecapenable2(if_t ifp,int togglecap)4267 if_togglecapenable2(if_t ifp, int togglecap)
4268 {
4269 	ifp->if_capenable2 ^= togglecap;
4270 	return (0);
4271 }
4272 
4273 int
if_getcapenable2(const if_t ifp)4274 if_getcapenable2(const if_t ifp)
4275 {
4276 	return (ifp->if_capenable2);
4277 }
4278 
4279 int
if_getdunit(const if_t ifp)4280 if_getdunit(const if_t ifp)
4281 {
4282 	return (ifp->if_dunit);
4283 }
4284 
4285 int
if_getindex(const if_t ifp)4286 if_getindex(const if_t ifp)
4287 {
4288 	return (ifp->if_index);
4289 }
4290 
4291 int
if_getidxgen(const if_t ifp)4292 if_getidxgen(const if_t ifp)
4293 {
4294 	return (ifp->if_idxgen);
4295 }
4296 
4297 const char *
if_getdescr(if_t ifp)4298 if_getdescr(if_t ifp)
4299 {
4300 	return (ifp->if_description);
4301 }
4302 
4303 void
if_setdescr(if_t ifp,char * descrbuf)4304 if_setdescr(if_t ifp, char *descrbuf)
4305 {
4306 	sx_xlock(&ifdescr_sx);
4307 	char *odescrbuf = ifp->if_description;
4308 	ifp->if_description = descrbuf;
4309 	sx_xunlock(&ifdescr_sx);
4310 
4311 	if_freedescr(odescrbuf);
4312 }
4313 
4314 char *
if_allocdescr(size_t sz,int malloc_flag)4315 if_allocdescr(size_t sz, int malloc_flag)
4316 {
4317 	malloc_flag &= (M_WAITOK | M_NOWAIT);
4318 	return (malloc(sz, M_IFDESCR, M_ZERO | malloc_flag));
4319 }
4320 
4321 void
if_freedescr(char * descrbuf)4322 if_freedescr(char *descrbuf)
4323 {
4324 	free(descrbuf, M_IFDESCR);
4325 }
4326 
4327 int
if_getalloctype(const if_t ifp)4328 if_getalloctype(const if_t ifp)
4329 {
4330 	return (ifp->if_alloctype);
4331 }
4332 
4333 void
if_setlastchange(if_t ifp)4334 if_setlastchange(if_t ifp)
4335 {
4336 	getmicrotime(&ifp->if_lastchange);
4337 }
4338 
4339 /*
4340  * This is largely undesirable because it ties ifnet to a device, but does
4341  * provide flexiblity for an embedded product vendor. Should be used with
4342  * the understanding that it violates the interface boundaries, and should be
4343  * a last resort only.
4344  */
4345 int
if_setdev(if_t ifp,void * dev)4346 if_setdev(if_t ifp, void *dev)
4347 {
4348 	return (0);
4349 }
4350 
4351 int
if_setdrvflagbits(if_t ifp,int set_flags,int clear_flags)4352 if_setdrvflagbits(if_t ifp, int set_flags, int clear_flags)
4353 {
4354 	ifp->if_drv_flags &= ~clear_flags;
4355 	ifp->if_drv_flags |= set_flags;
4356 
4357 	return (0);
4358 }
4359 
4360 int
if_getdrvflags(const if_t ifp)4361 if_getdrvflags(const if_t ifp)
4362 {
4363 	return (ifp->if_drv_flags);
4364 }
4365 
4366 int
if_setdrvflags(if_t ifp,int flags)4367 if_setdrvflags(if_t ifp, int flags)
4368 {
4369 	ifp->if_drv_flags = flags;
4370 	return (0);
4371 }
4372 
4373 int
if_setflags(if_t ifp,int flags)4374 if_setflags(if_t ifp, int flags)
4375 {
4376 	ifp->if_flags = flags;
4377 	return (0);
4378 }
4379 
4380 int
if_setflagbits(if_t ifp,int set,int clear)4381 if_setflagbits(if_t ifp, int set, int clear)
4382 {
4383 	ifp->if_flags &= ~clear;
4384 	ifp->if_flags |= set;
4385 	return (0);
4386 }
4387 
4388 int
if_getflags(const if_t ifp)4389 if_getflags(const if_t ifp)
4390 {
4391 	return (ifp->if_flags);
4392 }
4393 
4394 int
if_clearhwassist(if_t ifp)4395 if_clearhwassist(if_t ifp)
4396 {
4397 	ifp->if_hwassist = 0;
4398 	return (0);
4399 }
4400 
4401 int
if_sethwassistbits(if_t ifp,int toset,int toclear)4402 if_sethwassistbits(if_t ifp, int toset, int toclear)
4403 {
4404 	ifp->if_hwassist &= ~toclear;
4405 	ifp->if_hwassist |= toset;
4406 
4407 	return (0);
4408 }
4409 
4410 int
if_sethwassist(if_t ifp,int hwassist_bit)4411 if_sethwassist(if_t ifp, int hwassist_bit)
4412 {
4413 	ifp->if_hwassist = hwassist_bit;
4414 	return (0);
4415 }
4416 
4417 int
if_gethwassist(const if_t ifp)4418 if_gethwassist(const if_t ifp)
4419 {
4420 	return (ifp->if_hwassist);
4421 }
4422 
4423 int
if_togglehwassist(if_t ifp,int toggle_bits)4424 if_togglehwassist(if_t ifp, int toggle_bits)
4425 {
4426 	ifp->if_hwassist ^= toggle_bits;
4427 	return (0);
4428 }
4429 
4430 int
if_setmtu(if_t ifp,int mtu)4431 if_setmtu(if_t ifp, int mtu)
4432 {
4433 	ifp->if_mtu = mtu;
4434 	return (0);
4435 }
4436 
4437 void
if_notifymtu(if_t ifp)4438 if_notifymtu(if_t ifp)
4439 {
4440 #ifdef INET6
4441 	nd6_setmtu(ifp);
4442 #endif
4443 	rt_updatemtu(ifp);
4444 }
4445 
4446 int
if_getmtu(const if_t ifp)4447 if_getmtu(const if_t ifp)
4448 {
4449 	return (ifp->if_mtu);
4450 }
4451 
4452 int
if_getmtu_family(const if_t ifp,int family)4453 if_getmtu_family(const if_t ifp, int family)
4454 {
4455 	struct domain *dp;
4456 
4457 	SLIST_FOREACH(dp, &domains, dom_next) {
4458 		if (dp->dom_family == family && dp->dom_ifmtu != NULL)
4459 			return (dp->dom_ifmtu(ifp));
4460 	}
4461 
4462 	return (ifp->if_mtu);
4463 }
4464 
4465 void
if_setppromisc(if_t ifp,bool ppromisc)4466 if_setppromisc(if_t ifp, bool ppromisc)
4467 {
4468 	int new_flags;
4469 
4470 	if (ppromisc)
4471 		new_flags = ifp->if_flags | IFF_PPROMISC;
4472 	else
4473 		new_flags = ifp->if_flags & ~IFF_PPROMISC;
4474 	if ((ifp->if_flags ^ new_flags) & IFF_PPROMISC) {
4475 		if (new_flags & IFF_PPROMISC)
4476 			new_flags |= IFF_PROMISC;
4477 		/*
4478 		 * Only unset IFF_PROMISC if there are no more consumers of
4479 		 * promiscuity, i.e. the ifp->if_pcount refcount is 0.
4480 		 */
4481 		else if (ifp->if_pcount == 0)
4482 			new_flags &= ~IFF_PROMISC;
4483 		if (log_promisc_mode_change)
4484                         if_printf(ifp, "permanently promiscuous mode %s\n",
4485                             ((new_flags & IFF_PPROMISC) ?
4486                              "enabled" : "disabled"));
4487 	}
4488 	ifp->if_flags = new_flags;
4489 }
4490 
4491 /*
4492  * Methods for drivers to access interface unicast and multicast
4493  * link level addresses.  Driver shall not know 'struct ifaddr' neither
4494  * 'struct ifmultiaddr'.
4495  */
4496 u_int
if_lladdr_count(if_t ifp)4497 if_lladdr_count(if_t ifp)
4498 {
4499 	struct epoch_tracker et;
4500 	struct ifaddr *ifa;
4501 	u_int count;
4502 
4503 	count = 0;
4504 	NET_EPOCH_ENTER(et);
4505 	CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link)
4506 		if (ifa->ifa_addr->sa_family == AF_LINK)
4507 			count++;
4508 	NET_EPOCH_EXIT(et);
4509 
4510 	return (count);
4511 }
4512 
4513 int
if_foreach(if_foreach_cb_t cb,void * cb_arg)4514 if_foreach(if_foreach_cb_t cb, void *cb_arg)
4515 {
4516 	if_t ifp;
4517 	int error;
4518 
4519 	NET_EPOCH_ASSERT();
4520 	MPASS(cb);
4521 
4522 	error = 0;
4523 	CK_STAILQ_FOREACH(ifp, &V_ifnet, if_link) {
4524 		error = cb(ifp, cb_arg);
4525 		if (error != 0)
4526 			break;
4527 	}
4528 
4529 	return (error);
4530 }
4531 
4532 /*
4533  * Iterates over the list of interfaces, permitting callback function @cb to sleep.
4534  * Stops iteration if @cb returns non-zero error code.
4535  * Returns the last error code from @cb.
4536  * @match_cb: optional match callback limiting the iteration to only matched interfaces
4537  * @match_arg: argument to pass to @match_cb
4538  * @cb: iteration callback
4539  * @cb_arg: argument to pass to @cb
4540  */
4541 int
if_foreach_sleep(if_foreach_match_t match_cb,void * match_arg,if_foreach_cb_t cb,void * cb_arg)4542 if_foreach_sleep(if_foreach_match_t match_cb, void *match_arg, if_foreach_cb_t cb,
4543     void *cb_arg)
4544 {
4545 	int match_count = 0, array_size = 16; /* 128 bytes for malloc */
4546 	struct ifnet **match_array = NULL;
4547 	int error = 0;
4548 
4549 	MPASS(cb);
4550 
4551 	while (true) {
4552 		struct ifnet **new_array;
4553 		int new_size = array_size;
4554 		struct epoch_tracker et;
4555 		struct ifnet *ifp;
4556 
4557 		while (new_size < match_count)
4558 			new_size *= 2;
4559 		new_array = malloc(new_size * sizeof(void *), M_TEMP, M_WAITOK);
4560 		if (match_array != NULL)
4561 			memcpy(new_array, match_array, array_size * sizeof(void *));
4562 		free(match_array, M_TEMP);
4563 		match_array = new_array;
4564 		array_size = new_size;
4565 
4566 		match_count = 0;
4567 		NET_EPOCH_ENTER(et);
4568 		CK_STAILQ_FOREACH(ifp, &V_ifnet, if_link) {
4569 			if (match_cb != NULL && !match_cb(ifp, match_arg))
4570 				continue;
4571 			if (match_count < array_size) {
4572 				if (if_try_ref(ifp))
4573 					match_array[match_count++] = ifp;
4574 			} else
4575 				match_count++;
4576 		}
4577 		NET_EPOCH_EXIT(et);
4578 
4579 		if (match_count > array_size) {
4580 			for (int i = 0; i < array_size; i++)
4581 				if_rele(match_array[i]);
4582 			continue;
4583 		} else {
4584 			for (int i = 0; i < match_count; i++) {
4585 				if (error == 0)
4586 					error = cb(match_array[i], cb_arg);
4587 				if_rele(match_array[i]);
4588 			}
4589 			free(match_array, M_TEMP);
4590 			break;
4591 		}
4592 	}
4593 
4594 	return (error);
4595 }
4596 
4597 
4598 /*
4599  * Uses just 1 pointer of the 4 available in the public struct.
4600  */
4601 if_t
if_iter_start(struct if_iter * iter)4602 if_iter_start(struct if_iter *iter)
4603 {
4604 	if_t ifp;
4605 
4606 	NET_EPOCH_ASSERT();
4607 
4608 	bzero(iter, sizeof(*iter));
4609 	ifp = CK_STAILQ_FIRST(&V_ifnet);
4610 	if (ifp != NULL)
4611 		iter->context[0] = CK_STAILQ_NEXT(ifp, if_link);
4612 	else
4613 		iter->context[0] = NULL;
4614 	return (ifp);
4615 }
4616 
4617 if_t
if_iter_next(struct if_iter * iter)4618 if_iter_next(struct if_iter *iter)
4619 {
4620 	if_t cur_ifp = iter->context[0];
4621 
4622 	if (cur_ifp != NULL)
4623 		iter->context[0] = CK_STAILQ_NEXT(cur_ifp, if_link);
4624 	return (cur_ifp);
4625 }
4626 
4627 void
if_iter_finish(struct if_iter * iter)4628 if_iter_finish(struct if_iter *iter)
4629 {
4630 	/* Nothing to do here for now. */
4631 }
4632 
4633 u_int
if_foreach_lladdr(if_t ifp,iflladdr_cb_t cb,void * cb_arg)4634 if_foreach_lladdr(if_t ifp, iflladdr_cb_t cb, void *cb_arg)
4635 {
4636 	struct epoch_tracker et;
4637 	struct ifaddr *ifa;
4638 	u_int count;
4639 
4640 	MPASS(cb);
4641 
4642 	count = 0;
4643 	NET_EPOCH_ENTER(et);
4644 	CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
4645 		if (ifa->ifa_addr->sa_family != AF_LINK)
4646 			continue;
4647 		count += (*cb)(cb_arg, (struct sockaddr_dl *)ifa->ifa_addr,
4648 		    count);
4649 	}
4650 	NET_EPOCH_EXIT(et);
4651 
4652 	return (count);
4653 }
4654 
4655 u_int
if_llmaddr_count(if_t ifp)4656 if_llmaddr_count(if_t ifp)
4657 {
4658 	struct epoch_tracker et;
4659 	struct ifmultiaddr *ifma;
4660 	int count;
4661 
4662 	count = 0;
4663 	NET_EPOCH_ENTER(et);
4664 	CK_STAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link)
4665 		if (ifma->ifma_addr->sa_family == AF_LINK)
4666 			count++;
4667 	NET_EPOCH_EXIT(et);
4668 
4669 	return (count);
4670 }
4671 
4672 bool
if_maddr_empty(if_t ifp)4673 if_maddr_empty(if_t ifp)
4674 {
4675 
4676 	return (CK_STAILQ_EMPTY(&ifp->if_multiaddrs));
4677 }
4678 
4679 u_int
if_foreach_llmaddr(if_t ifp,iflladdr_cb_t cb,void * cb_arg)4680 if_foreach_llmaddr(if_t ifp, iflladdr_cb_t cb, void *cb_arg)
4681 {
4682 	struct epoch_tracker et;
4683 	struct ifmultiaddr *ifma;
4684 	u_int count;
4685 
4686 	MPASS(cb);
4687 
4688 	count = 0;
4689 	NET_EPOCH_ENTER(et);
4690 	CK_STAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
4691 		if (ifma->ifma_addr->sa_family != AF_LINK)
4692 			continue;
4693 		count += (*cb)(cb_arg, (struct sockaddr_dl *)ifma->ifma_addr,
4694 		    count);
4695 	}
4696 	NET_EPOCH_EXIT(et);
4697 
4698 	return (count);
4699 }
4700 
4701 u_int
if_foreach_addr_type(if_t ifp,int type,if_addr_cb_t cb,void * cb_arg)4702 if_foreach_addr_type(if_t ifp, int type, if_addr_cb_t cb, void *cb_arg)
4703 {
4704 	struct epoch_tracker et;
4705 	struct ifaddr *ifa;
4706 	u_int count;
4707 
4708 	MPASS(cb);
4709 
4710 	count = 0;
4711 	NET_EPOCH_ENTER(et);
4712 	CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
4713 		if (ifa->ifa_addr->sa_family != type)
4714 			continue;
4715 		count += (*cb)(cb_arg, ifa, count);
4716 	}
4717 	NET_EPOCH_EXIT(et);
4718 
4719 	return (count);
4720 }
4721 
4722 struct ifaddr *
ifa_iter_start(if_t ifp,struct ifa_iter * iter)4723 ifa_iter_start(if_t ifp, struct ifa_iter *iter)
4724 {
4725 	struct ifaddr *ifa;
4726 
4727 	NET_EPOCH_ASSERT();
4728 
4729 	bzero(iter, sizeof(*iter));
4730 	ifa = CK_STAILQ_FIRST(&ifp->if_addrhead);
4731 	if (ifa != NULL)
4732 		iter->context[0] = CK_STAILQ_NEXT(ifa, ifa_link);
4733 	else
4734 		iter->context[0] = NULL;
4735 	return (ifa);
4736 }
4737 
4738 struct ifaddr *
ifa_iter_next(struct ifa_iter * iter)4739 ifa_iter_next(struct ifa_iter *iter)
4740 {
4741 	struct ifaddr *ifa = iter->context[0];
4742 
4743 	if (ifa != NULL)
4744 		iter->context[0] = CK_STAILQ_NEXT(ifa, ifa_link);
4745 	return (ifa);
4746 }
4747 
4748 void
ifa_iter_finish(struct ifa_iter * iter)4749 ifa_iter_finish(struct ifa_iter *iter)
4750 {
4751 	/* Nothing to do here for now. */
4752 }
4753 
4754 int
if_setsoftc(if_t ifp,void * softc)4755 if_setsoftc(if_t ifp, void *softc)
4756 {
4757 	ifp->if_softc = softc;
4758 	return (0);
4759 }
4760 
4761 void *
if_getsoftc(const if_t ifp)4762 if_getsoftc(const if_t ifp)
4763 {
4764 	return (ifp->if_softc);
4765 }
4766 
4767 void
if_setrcvif(struct mbuf * m,if_t ifp)4768 if_setrcvif(struct mbuf *m, if_t ifp)
4769 {
4770 
4771 	MPASS((m->m_pkthdr.csum_flags & CSUM_SND_TAG) == 0);
4772 	m->m_pkthdr.rcvif = (struct ifnet *)ifp;
4773 }
4774 
4775 void
if_setvtag(struct mbuf * m,uint16_t tag)4776 if_setvtag(struct mbuf *m, uint16_t tag)
4777 {
4778 	m->m_pkthdr.ether_vtag = tag;
4779 }
4780 
4781 uint16_t
if_getvtag(struct mbuf * m)4782 if_getvtag(struct mbuf *m)
4783 {
4784 	return (m->m_pkthdr.ether_vtag);
4785 }
4786 
4787 int
if_sendq_empty(if_t ifp)4788 if_sendq_empty(if_t ifp)
4789 {
4790 	return (IFQ_DRV_IS_EMPTY(&ifp->if_snd));
4791 }
4792 
4793 struct ifaddr *
if_getifaddr(const if_t ifp)4794 if_getifaddr(const if_t ifp)
4795 {
4796 	return (ifp->if_addr);
4797 }
4798 
4799 int
if_setsendqready(if_t ifp)4800 if_setsendqready(if_t ifp)
4801 {
4802 	IFQ_SET_READY(&ifp->if_snd);
4803 	return (0);
4804 }
4805 
4806 int
if_setsendqlen(if_t ifp,int tx_desc_count)4807 if_setsendqlen(if_t ifp, int tx_desc_count)
4808 {
4809 	IFQ_SET_MAXLEN(&ifp->if_snd, tx_desc_count);
4810 	ifp->if_snd.ifq_drv_maxlen = tx_desc_count;
4811 	return (0);
4812 }
4813 
4814 void
if_setnetmapadapter(if_t ifp,struct netmap_adapter * na)4815 if_setnetmapadapter(if_t ifp, struct netmap_adapter *na)
4816 {
4817 	ifp->if_netmap = na;
4818 }
4819 
4820 struct netmap_adapter *
if_getnetmapadapter(if_t ifp)4821 if_getnetmapadapter(if_t ifp)
4822 {
4823 	return (ifp->if_netmap);
4824 }
4825 
4826 int
if_vlantrunkinuse(if_t ifp)4827 if_vlantrunkinuse(if_t ifp)
4828 {
4829 	return (ifp->if_vlantrunk != NULL);
4830 }
4831 
4832 void
if_init(if_t ifp,void * ctx)4833 if_init(if_t ifp, void *ctx)
4834 {
4835 	(*ifp->if_init)(ctx);
4836 }
4837 
4838 void
if_input(if_t ifp,struct mbuf * sendmp)4839 if_input(if_t ifp, struct mbuf* sendmp)
4840 {
4841 	(*ifp->if_input)(ifp, sendmp);
4842 }
4843 
4844 int
if_transmit(if_t ifp,struct mbuf * m)4845 if_transmit(if_t ifp, struct mbuf *m)
4846 {
4847 	return ((*ifp->if_transmit)(ifp, m));
4848 }
4849 
4850 int
if_resolvemulti(if_t ifp,struct sockaddr ** srcs,struct sockaddr * dst)4851 if_resolvemulti(if_t ifp, struct sockaddr **srcs, struct sockaddr *dst)
4852 {
4853 	if (ifp->if_resolvemulti == NULL)
4854 		return (EOPNOTSUPP);
4855 
4856 	return (ifp->if_resolvemulti(ifp, srcs, dst));
4857 }
4858 
4859 int
if_ioctl(if_t ifp,u_long cmd,void * data)4860 if_ioctl(if_t ifp, u_long cmd, void *data)
4861 {
4862 	if (ifp->if_ioctl == NULL)
4863 		return (EOPNOTSUPP);
4864 
4865 	return (ifp->if_ioctl(ifp, cmd, data));
4866 }
4867 
4868 struct mbuf *
if_dequeue(if_t ifp)4869 if_dequeue(if_t ifp)
4870 {
4871 	struct mbuf *m;
4872 
4873 	IFQ_DRV_DEQUEUE(&ifp->if_snd, m);
4874 	return (m);
4875 }
4876 
4877 int
if_sendq_prepend(if_t ifp,struct mbuf * m)4878 if_sendq_prepend(if_t ifp, struct mbuf *m)
4879 {
4880 	IFQ_DRV_PREPEND(&ifp->if_snd, m);
4881 	return (0);
4882 }
4883 
4884 int
if_setifheaderlen(if_t ifp,int len)4885 if_setifheaderlen(if_t ifp, int len)
4886 {
4887 	ifp->if_hdrlen = len;
4888 	return (0);
4889 }
4890 
4891 char *
if_getlladdr(const if_t ifp)4892 if_getlladdr(const if_t ifp)
4893 {
4894 	return (IF_LLADDR(ifp));
4895 }
4896 
4897 void *
if_gethandle(u_char type)4898 if_gethandle(u_char type)
4899 {
4900 	return (if_alloc(type));
4901 }
4902 
4903 void
if_vlancap(if_t ifp)4904 if_vlancap(if_t ifp)
4905 {
4906 	VLAN_CAPABILITIES(ifp);
4907 }
4908 
4909 int
if_sethwtsomax(if_t ifp,u_int if_hw_tsomax)4910 if_sethwtsomax(if_t ifp, u_int if_hw_tsomax)
4911 {
4912 	ifp->if_hw_tsomax = if_hw_tsomax;
4913         return (0);
4914 }
4915 
4916 int
if_sethwtsomaxsegcount(if_t ifp,u_int if_hw_tsomaxsegcount)4917 if_sethwtsomaxsegcount(if_t ifp, u_int if_hw_tsomaxsegcount)
4918 {
4919 	ifp->if_hw_tsomaxsegcount = if_hw_tsomaxsegcount;
4920         return (0);
4921 }
4922 
4923 int
if_sethwtsomaxsegsize(if_t ifp,u_int if_hw_tsomaxsegsize)4924 if_sethwtsomaxsegsize(if_t ifp, u_int if_hw_tsomaxsegsize)
4925 {
4926 	ifp->if_hw_tsomaxsegsize = if_hw_tsomaxsegsize;
4927         return (0);
4928 }
4929 
4930 u_int
if_gethwtsomax(const if_t ifp)4931 if_gethwtsomax(const if_t ifp)
4932 {
4933 	return (ifp->if_hw_tsomax);
4934 }
4935 
4936 u_int
if_gethwtsomaxsegcount(const if_t ifp)4937 if_gethwtsomaxsegcount(const if_t ifp)
4938 {
4939 	return (ifp->if_hw_tsomaxsegcount);
4940 }
4941 
4942 u_int
if_gethwtsomaxsegsize(const if_t ifp)4943 if_gethwtsomaxsegsize(const if_t ifp)
4944 {
4945 	return (ifp->if_hw_tsomaxsegsize);
4946 }
4947 
4948 void
if_setinitfn(if_t ifp,if_init_fn_t init_fn)4949 if_setinitfn(if_t ifp, if_init_fn_t init_fn)
4950 {
4951 	ifp->if_init = init_fn;
4952 }
4953 
4954 void
if_setinputfn(if_t ifp,if_input_fn_t input_fn)4955 if_setinputfn(if_t ifp, if_input_fn_t input_fn)
4956 {
4957 	ifp->if_input = input_fn;
4958 }
4959 
4960 if_input_fn_t
if_getinputfn(if_t ifp)4961 if_getinputfn(if_t ifp)
4962 {
4963 	return (ifp->if_input);
4964 }
4965 
4966 void
if_setioctlfn(if_t ifp,if_ioctl_fn_t ioctl_fn)4967 if_setioctlfn(if_t ifp, if_ioctl_fn_t ioctl_fn)
4968 {
4969 	ifp->if_ioctl = ioctl_fn;
4970 }
4971 
4972 void
if_setoutputfn(if_t ifp,if_output_fn_t output_fn)4973 if_setoutputfn(if_t ifp, if_output_fn_t output_fn)
4974 {
4975 	ifp->if_output = output_fn;
4976 }
4977 
4978 void
if_setstartfn(if_t ifp,if_start_fn_t start_fn)4979 if_setstartfn(if_t ifp, if_start_fn_t start_fn)
4980 {
4981 	ifp->if_start = start_fn;
4982 }
4983 
4984 if_start_fn_t
if_getstartfn(if_t ifp)4985 if_getstartfn(if_t ifp)
4986 {
4987 	return (ifp->if_start);
4988 }
4989 
4990 void
if_settransmitfn(if_t ifp,if_transmit_fn_t start_fn)4991 if_settransmitfn(if_t ifp, if_transmit_fn_t start_fn)
4992 {
4993 	ifp->if_transmit = start_fn;
4994 }
4995 
4996 if_transmit_fn_t
if_gettransmitfn(if_t ifp)4997 if_gettransmitfn(if_t ifp)
4998 {
4999 	return (ifp->if_transmit);
5000 }
5001 
5002 void
if_setqflushfn(if_t ifp,if_qflush_fn_t flush_fn)5003 if_setqflushfn(if_t ifp, if_qflush_fn_t flush_fn)
5004 {
5005 	ifp->if_qflush = flush_fn;
5006 }
5007 
5008 void
if_setsndtagallocfn(if_t ifp,if_snd_tag_alloc_t alloc_fn)5009 if_setsndtagallocfn(if_t ifp, if_snd_tag_alloc_t alloc_fn)
5010 {
5011 	ifp->if_snd_tag_alloc = alloc_fn;
5012 }
5013 
5014 int
if_snd_tag_alloc(if_t ifp,union if_snd_tag_alloc_params * params,struct m_snd_tag ** mstp)5015 if_snd_tag_alloc(if_t ifp, union if_snd_tag_alloc_params *params,
5016     struct m_snd_tag **mstp)
5017 {
5018 	if (ifp->if_snd_tag_alloc == NULL)
5019 		return (EOPNOTSUPP);
5020 	return (ifp->if_snd_tag_alloc(ifp, params, mstp));
5021 }
5022 
5023 void
if_setgetcounterfn(if_t ifp,if_get_counter_t fn)5024 if_setgetcounterfn(if_t ifp, if_get_counter_t fn)
5025 {
5026 	ifp->if_get_counter = fn;
5027 }
5028 
5029 void
if_setreassignfn(if_t ifp,if_reassign_fn_t fn)5030 if_setreassignfn(if_t ifp, if_reassign_fn_t fn)
5031 {
5032 	ifp->if_reassign = fn;
5033 }
5034 
5035 void
if_setratelimitqueryfn(if_t ifp,if_ratelimit_query_t fn)5036 if_setratelimitqueryfn(if_t ifp, if_ratelimit_query_t fn)
5037 {
5038 	ifp->if_ratelimit_query = fn;
5039 }
5040 
5041 void
if_setdebugnet_methods(if_t ifp,struct debugnet_methods * m)5042 if_setdebugnet_methods(if_t ifp, struct debugnet_methods *m)
5043 {
5044 	ifp->if_debugnet_methods = m;
5045 }
5046 
5047 struct label *
if_getmaclabel(if_t ifp)5048 if_getmaclabel(if_t ifp)
5049 {
5050 	return (ifp->if_label);
5051 }
5052 
5053 void
if_setmaclabel(if_t ifp,struct label * label)5054 if_setmaclabel(if_t ifp, struct label *label)
5055 {
5056 	ifp->if_label = label;
5057 }
5058 
5059 int
if_gettype(if_t ifp)5060 if_gettype(if_t ifp)
5061 {
5062 	return (ifp->if_type);
5063 }
5064 
5065 void *
if_getllsoftc(if_t ifp)5066 if_getllsoftc(if_t ifp)
5067 {
5068 	return (ifp->if_llsoftc);
5069 }
5070 
5071 void
if_setllsoftc(if_t ifp,void * llsoftc)5072 if_setllsoftc(if_t ifp, void *llsoftc)
5073 {
5074 	ifp->if_llsoftc = llsoftc;
5075 };
5076 
5077 int
if_getlinkstate(if_t ifp)5078 if_getlinkstate(if_t ifp)
5079 {
5080 	return (ifp->if_link_state);
5081 }
5082 
5083 const uint8_t *
if_getbroadcastaddr(if_t ifp)5084 if_getbroadcastaddr(if_t ifp)
5085 {
5086 	return (ifp->if_broadcastaddr);
5087 }
5088 
5089 void
if_setbroadcastaddr(if_t ifp,const uint8_t * addr)5090 if_setbroadcastaddr(if_t ifp, const uint8_t *addr)
5091 {
5092 	ifp->if_broadcastaddr = addr;
5093 }
5094 
5095 int
if_getnumadomain(if_t ifp)5096 if_getnumadomain(if_t ifp)
5097 {
5098 	return (ifp->if_numa_domain);
5099 }
5100 
5101 uint64_t
if_getcounter(if_t ifp,ift_counter counter)5102 if_getcounter(if_t ifp, ift_counter counter)
5103 {
5104 	return (ifp->if_get_counter(ifp, counter));
5105 }
5106 
5107 bool
if_altq_is_enabled(if_t ifp)5108 if_altq_is_enabled(if_t ifp)
5109 {
5110 	return (ALTQ_IS_ENABLED(&ifp->if_snd));
5111 }
5112 
5113 struct vnet *
if_getvnet(if_t ifp)5114 if_getvnet(if_t ifp)
5115 {
5116 	return (ifp->if_vnet);
5117 }
5118 
5119 void *
if_getafdata(if_t ifp,int af)5120 if_getafdata(if_t ifp, int af)
5121 {
5122 	return (ifp->if_afdata[af]);
5123 }
5124 
5125 u_int
if_getfib(if_t ifp)5126 if_getfib(if_t ifp)
5127 {
5128 	return (ifp->if_fib);
5129 }
5130 
5131 uint8_t
if_getaddrlen(if_t ifp)5132 if_getaddrlen(if_t ifp)
5133 {
5134 	return (ifp->if_addrlen);
5135 }
5136 
5137 struct bpf_if *
if_getbpf(if_t ifp)5138 if_getbpf(if_t ifp)
5139 {
5140 	return (ifp->if_bpf);
5141 }
5142 
5143 struct ifvlantrunk *
if_getvlantrunk(if_t ifp)5144 if_getvlantrunk(if_t ifp)
5145 {
5146 	return (ifp->if_vlantrunk);
5147 }
5148 
5149 uint8_t
if_getpcp(if_t ifp)5150 if_getpcp(if_t ifp)
5151 {
5152 	return (ifp->if_pcp);
5153 }
5154 
5155 void *
if_getl2com(if_t ifp)5156 if_getl2com(if_t ifp)
5157 {
5158 	return (ifp->if_l2com);
5159 }
5160 
5161 void
if_setipsec_accel_methods(if_t ifp,const struct if_ipsec_accel_methods * m)5162 if_setipsec_accel_methods(if_t ifp, const struct if_ipsec_accel_methods *m)
5163 {
5164 	ifp->if_ipsec_accel_m = m;
5165 }
5166 
5167 #ifdef DDB
5168 static void
if_show_ifnet(struct ifnet * ifp)5169 if_show_ifnet(struct ifnet *ifp)
5170 {
5171 	if (ifp == NULL)
5172 		return;
5173 	db_printf("%s:\n", ifp->if_xname);
5174 #define	IF_DB_PRINTF(f, e)	db_printf("   %s = " f "\n", #e, ifp->e);
5175 	IF_DB_PRINTF("%s", if_dname);
5176 	IF_DB_PRINTF("%d", if_dunit);
5177 	IF_DB_PRINTF("%s", if_description);
5178 	IF_DB_PRINTF("%u", if_index);
5179 	IF_DB_PRINTF("%d", if_idxgen);
5180 	IF_DB_PRINTF("%u", if_refcount);
5181 	IF_DB_PRINTF("%p", if_softc);
5182 	IF_DB_PRINTF("%p", if_l2com);
5183 	IF_DB_PRINTF("%p", if_llsoftc);
5184 	IF_DB_PRINTF("%d", if_amcount);
5185 	IF_DB_PRINTF("%p", if_addr);
5186 	IF_DB_PRINTF("%p", if_broadcastaddr);
5187 	IF_DB_PRINTF("%p", if_afdata);
5188 	IF_DB_PRINTF("%d", if_afdata_initialized);
5189 	IF_DB_PRINTF("%u", if_fib);
5190 	IF_DB_PRINTF("%p", if_vnet);
5191 	IF_DB_PRINTF("%p", if_home_vnet);
5192 	IF_DB_PRINTF("%p", if_vlantrunk);
5193 	IF_DB_PRINTF("%p", if_bpf);
5194 	IF_DB_PRINTF("%u", if_pcount);
5195 	IF_DB_PRINTF("%p", if_bridge);
5196 	IF_DB_PRINTF("%p", if_lagg);
5197 	IF_DB_PRINTF("%p", if_pf_kif);
5198 	IF_DB_PRINTF("%p", if_carp);
5199 	IF_DB_PRINTF("%p", if_label);
5200 	IF_DB_PRINTF("%p", if_netmap);
5201 	IF_DB_PRINTF("0x%08x", if_flags);
5202 	IF_DB_PRINTF("0x%08x", if_drv_flags);
5203 	IF_DB_PRINTF("0x%08x", if_capabilities);
5204 	IF_DB_PRINTF("0x%08x", if_capenable);
5205 	IF_DB_PRINTF("%p", if_snd.ifq_head);
5206 	IF_DB_PRINTF("%p", if_snd.ifq_tail);
5207 	IF_DB_PRINTF("%d", if_snd.ifq_len);
5208 	IF_DB_PRINTF("%d", if_snd.ifq_maxlen);
5209 	IF_DB_PRINTF("%p", if_snd.ifq_drv_head);
5210 	IF_DB_PRINTF("%p", if_snd.ifq_drv_tail);
5211 	IF_DB_PRINTF("%d", if_snd.ifq_drv_len);
5212 	IF_DB_PRINTF("%d", if_snd.ifq_drv_maxlen);
5213 	IF_DB_PRINTF("%d", if_snd.altq_type);
5214 	IF_DB_PRINTF("%x", if_snd.altq_flags);
5215 #undef IF_DB_PRINTF
5216 }
5217 
DB_SHOW_COMMAND(ifnet,db_show_ifnet)5218 DB_SHOW_COMMAND(ifnet, db_show_ifnet)
5219 {
5220 	if (!have_addr) {
5221 		db_printf("usage: show ifnet <struct ifnet *>\n");
5222 		return;
5223 	}
5224 
5225 	if_show_ifnet((struct ifnet *)addr);
5226 }
5227 
DB_SHOW_ALL_COMMAND(ifnets,db_show_all_ifnets)5228 DB_SHOW_ALL_COMMAND(ifnets, db_show_all_ifnets)
5229 {
5230 	struct ifnet *ifp;
5231 	u_short idx;
5232 
5233 	for (idx = 1; idx <= if_index; idx++) {
5234 		ifp = ifindex_table[idx].ife_ifnet;
5235 		if (ifp == NULL)
5236 			continue;
5237 		db_printf( "%20s ifp=%p\n", ifp->if_xname, ifp);
5238 		if (db_pager_quit)
5239 			break;
5240 	}
5241 }
5242 #endif	/* DDB */
5243