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