xref: /netbsd-src/sys/net/if.h (revision 181254a7b1bdde6873432bffef2d2decc4b5c22f)
1 /*	$NetBSD: if.h,v 1.284 2020/08/28 06:23:42 ozaki-r Exp $	*/
2 
3 /*-
4  * Copyright (c) 1999, 2000, 2001 The NetBSD Foundation, Inc.
5  * All rights reserved.
6  *
7  * This code is derived from software contributed to The NetBSD Foundation
8  * by William Studenmund and Jason R. Thorpe.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29  * POSSIBILITY OF SUCH DAMAGE.
30  */
31 
32 /*
33  * Copyright (c) 1982, 1986, 1989, 1993
34  *	The Regents of the University of California.  All rights reserved.
35  *
36  * Redistribution and use in source and binary forms, with or without
37  * modification, are permitted provided that the following conditions
38  * are met:
39  * 1. Redistributions of source code must retain the above copyright
40  *    notice, this list of conditions and the following disclaimer.
41  * 2. Redistributions in binary form must reproduce the above copyright
42  *    notice, this list of conditions and the following disclaimer in the
43  *    documentation and/or other materials provided with the distribution.
44  * 3. Neither the name of the University nor the names of its contributors
45  *    may be used to endorse or promote products derived from this software
46  *    without specific prior written permission.
47  *
48  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
49  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
50  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
51  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
52  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
53  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
54  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
55  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
56  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
57  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
58  * SUCH DAMAGE.
59  *
60  *	@(#)if.h	8.3 (Berkeley) 2/9/95
61  */
62 
63 #ifndef _NET_IF_H_
64 #define _NET_IF_H_
65 
66 #if !defined(_KERNEL) && !defined(_STANDALONE)
67 #include <stdbool.h>
68 #endif
69 
70 #include <sys/featuretest.h>
71 
72 /*
73  * Length of interface external name, including terminating '\0'.
74  * Note: this is the same size as a generic device's external name.
75  */
76 #define IF_NAMESIZE 16
77 
78 /*
79  * Length of interface description, including terminating '\0'.
80  */
81 #define	IFDESCRSIZE	64
82 
83 #if defined(_NETBSD_SOURCE)
84 
85 #include <sys/socket.h>
86 #include <sys/queue.h>
87 #include <sys/mutex.h>
88 
89 #include <net/dlt.h>
90 #include <net/pfil.h>
91 #ifdef _KERNEL
92 #include <net/pktqueue.h>
93 #include <sys/pslist.h>
94 #include <sys/pserialize.h>
95 #include <sys/psref.h>
96 #include <sys/module_hook.h>
97 #endif
98 
99 /*
100  * Always include ALTQ glue here -- we use the ALTQ interface queue
101  * structure even when ALTQ is not configured into the kernel so that
102  * the size of struct ifnet does not changed based on the option.  The
103  * ALTQ queue structure is API-compatible with the legacy ifqueue.
104  */
105 #include <altq/if_altq.h>
106 
107 /*
108  * Structures defining a network interface, providing a packet
109  * transport mechanism (ala level 0 of the PUP protocols).
110  *
111  * Each interface accepts output datagrams of a specified maximum
112  * length, and provides higher level routines with input datagrams
113  * received from its medium.
114  *
115  * Output occurs when the routine if_output is called, with four parameters:
116  *	(*ifp->if_output)(ifp, m, dst, rt)
117  * Here m is the mbuf chain to be sent and dst is the destination address.
118  * The output routine encapsulates the supplied datagram if necessary,
119  * and then transmits it on its medium.
120  *
121  * On input, each interface unwraps the data received by it, and either
122  * places it on the input queue of a internetwork datagram routine
123  * and posts the associated software interrupt, or passes the datagram to a raw
124  * packet input routine.
125  *
126  * Routines exist for locating interfaces by their addresses
127  * or for locating a interface on a certain network, as well as more general
128  * routing and gateway routines maintaining information used to locate
129  * interfaces.  These routines live in the files if.c and route.c
130  */
131 #include <sys/time.h>
132 
133 #if defined(_KERNEL_OPT)
134 #include "opt_compat_netbsd.h"
135 #include "opt_gateway.h"
136 #endif
137 
138 struct mbuf;
139 struct proc;
140 struct rtentry;
141 struct socket;
142 struct ether_header;
143 struct ifaddr;
144 struct ifnet;
145 struct rt_addrinfo;
146 
147 #define	IFNAMSIZ	IF_NAMESIZE
148 
149 /*
150  * Structure describing a `cloning' interface.
151  */
152 struct if_clone {
153 	LIST_ENTRY(if_clone) ifc_list;	/* on list of cloners */
154 	const char *ifc_name;		/* name of device, e.g. `gif' */
155 	size_t ifc_namelen;		/* length of name */
156 
157 	int	(*ifc_create)(struct if_clone *, int);
158 	int	(*ifc_destroy)(struct ifnet *);
159 };
160 
161 #define	IF_CLONE_INITIALIZER(name, create, destroy)			\
162 	{ { NULL, NULL }, name, sizeof(name) - 1, create, destroy }
163 
164 /*
165  * Structure used to query names of interface cloners.
166  */
167 struct if_clonereq {
168 	int	ifcr_total;		/* total cloners (out) */
169 	int	ifcr_count;		/* room for this many in user buffer */
170 	char	*ifcr_buffer;		/* buffer for cloner names */
171 };
172 
173 /*
174  * Structure defining statistics and other data kept regarding a network
175  * interface.
176  *
177  * Only used for exporting data from the interface.
178  */
179 struct if_data {
180 	/* generic interface information */
181 	u_char	ifi_type;		/* ethernet, tokenring, etc. */
182 	u_char	ifi_addrlen;		/* media address length */
183 	u_char	ifi_hdrlen;		/* media header length */
184 	int	ifi_link_state;		/* current link state */
185 	uint64_t ifi_mtu;		/* maximum transmission unit */
186 	uint64_t ifi_metric;		/* routing metric (external only) */
187 	uint64_t ifi_baudrate;		/* linespeed */
188 	/* volatile statistics */
189 	uint64_t ifi_ipackets;		/* packets received on interface */
190 	uint64_t ifi_ierrors;		/* input errors on interface */
191 	uint64_t ifi_opackets;		/* packets sent on interface */
192 	uint64_t ifi_oerrors;		/* output errors on interface */
193 	uint64_t ifi_collisions;	/* collisions on csma interfaces */
194 	uint64_t ifi_ibytes;		/* total number of octets received */
195 	uint64_t ifi_obytes;		/* total number of octets sent */
196 	uint64_t ifi_imcasts;		/* packets received via multicast */
197 	uint64_t ifi_omcasts;		/* packets sent via multicast */
198 	uint64_t ifi_iqdrops;		/* dropped on input, this interface */
199 	uint64_t ifi_noproto;		/* destined for unsupported protocol */
200 	struct	timespec ifi_lastchange;/* last operational state change */
201 };
202 
203 /*
204  * Values for if_link_state.
205  */
206 #define	LINK_STATE_UNKNOWN	0	/* link invalid/unknown */
207 #define	LINK_STATE_DOWN		1	/* link is down */
208 #define	LINK_STATE_UP		2	/* link is up */
209 
210 /*
211  * Structure defining a queue for a network interface.
212  */
213 struct ifqueue {
214 	struct		mbuf *ifq_head;
215 	struct		mbuf *ifq_tail;
216 	int		ifq_len;
217 	int		ifq_maxlen;
218 	int		ifq_drops;
219 	kmutex_t	*ifq_lock;
220 };
221 
222 #ifdef _KERNEL
223 #include <sys/percpu.h>
224 #include <sys/callout.h>
225 #include <sys/rwlock.h>
226 #include <sys/workqueue.h>
227 
228 #endif /* _KERNEL */
229 
230 /*
231  * Structure defining a queue for a network interface.
232  *
233  * (Would like to call this struct ``if'', but C isn't PL/1.)
234  */
235 TAILQ_HEAD(ifnet_head, ifnet);		/* the actual queue head */
236 
237 struct bridge_softc;
238 struct bridge_iflist;
239 struct callout;
240 struct krwlock;
241 struct if_percpuq;
242 struct if_deferred_start;
243 struct in6_multi;
244 
245 typedef unsigned short if_index_t;
246 
247 /*
248  * Interface.  Field markings and the corresponding locks:
249  *
250  * i:	IFNET_LOCK (a.k.a., if_ioctl_lock)
251  * q:	ifq_lock (struct ifaltq)
252  * a:	if_afdata_lock
253  * 6:	in6_multilock (global lock)
254  * ::	unlocked, stable
255  * ?:	unknown, maybe unsafe
256  *
257  * Lock order: IFNET_LOCK => in6_multilock => if_afdata_lock => ifq_lock
258  *   Note that currently if_afdata_lock and ifq_lock aren't held
259  *   at the same time, but define the order anyway.
260  *
261  * Lock order of IFNET_LOCK with other locks:
262  *     softnet_lock => solock => IFNET_LOCK => ND6_LOCK, in_multilock
263  */
264 typedef struct ifnet {
265 	void		*if_softc;	/* :: lower-level data for this if */
266 	/* DEPRECATED. Keep it to avoid breaking kvm(3) users */
267 	TAILQ_ENTRY(ifnet)
268 			if_list;	/* i: all struct ifnets are chained */
269 	TAILQ_HEAD(, ifaddr)
270 			if_addrlist;	/* i: linked list of addresses per if */
271 	char		if_xname[IFNAMSIZ];
272 					/* :: external name (name + unit) */
273 	int		if_pcount;	/* i: number of promiscuous listeners */
274 	struct bpf_if	*if_bpf;	/* :: packet filter structure */
275 	if_index_t	if_index;	/* :: numeric abbreviation for this if */
276 	short		if_timer;	/* ?: time 'til if_slowtimo called */
277 	unsigned short	if_flags;	/* i: up/down, broadcast, etc. */
278 	short		if_extflags;	/* :: if_output MP-safe, etc. */
279 	u_char		if_type;	/* :: ethernet, tokenring, etc. */
280 	u_char		if_addrlen;	/* :: media address length */
281 	u_char		if_hdrlen;	/* :: media header length */
282 	/* XXX audit :? fields here. */
283 	int		if_link_state;	/* :? current link state */
284 	uint64_t	if_mtu;		/* :? maximum transmission unit */
285 	uint64_t	if_metric;	/* :? routing metric (external only) */
286 	uint64_t	if_baudrate;	/* :? linespeed */
287 	struct timespec	if_lastchange;	/* :? last operational state change */
288 #ifdef _KERNEL
289 	percpu_t	*if_stats;	/* :: statistics */
290 #else
291 	void		*if_stats;	/* opaque to user-space */
292 #endif /* _KERNEL */
293 	/*
294 	 * Procedure handles.  If you add more of these, don't forget the
295 	 * corresponding NULL stub in if.c.
296 	 */
297 	int		(*if_output)	/* :: output routine (enqueue) */
298 			    (struct ifnet *, struct mbuf *, const struct sockaddr *,
299 			     const struct rtentry *);
300 	void		(*_if_input)	/* :: input routine (from h/w driver) */
301 			    (struct ifnet *, struct mbuf *);
302 	void		(*if_start)	/* :: initiate output routine */
303 			    (struct ifnet *);
304 	int		(*if_transmit)	/* :: output routine, must be MP-safe */
305 			    (struct ifnet *, struct mbuf *);
306 	int		(*if_ioctl)	/* :: ioctl routine */
307 			    (struct ifnet *, u_long, void *);
308 	int		(*if_init)	/* :: init routine */
309 			    (struct ifnet *);
310 	void		(*if_stop)	/* :: stop routine */
311 			    (struct ifnet *, int);
312 	void		(*if_slowtimo)	/* :: timer routine */
313 			    (struct ifnet *);
314 #define	if_watchdog	if_slowtimo
315 	void		(*if_drain)	/* :: routine to release resources */
316 			    (struct ifnet *);
317 	struct ifaltq	if_snd;		/* q: output queue (includes altq) */
318 	struct ifaddr	*if_dl;		/* i: identity of this interface. */
319 	const struct sockaddr_dl
320 			*if_sadl;	/* i: pointer to sockaddr_dl of if_dl */
321 	/*
322 	 * May be NULL.  If not NULL, it is the address assigned
323 	 * to the interface by the manufacturer, so it very likely
324 	 * to be unique.  It MUST NOT be deleted.  It is highly
325 	 * suitable for deriving the EUI64 for the interface.
326 	 */
327 	struct ifaddr	*if_hwdl;	/* i: h/w identity */
328 	const uint8_t	*if_broadcastaddr;
329 					/* :: linklevel broadcast bytestring */
330 	struct bridge_softc
331 			*if_bridge;	/* i: bridge glue */
332 	struct bridge_iflist
333 			*if_bridgeif;	/* i: shortcut to interface list entry */
334 	int		if_dlt;		/* :: data link type (<net/dlt.h>) */
335 	pfil_head_t *	if_pfil;	/* :: filtering point */
336 	uint64_t	if_capabilities;
337 					/* i: interface capabilities */
338 	uint64_t	if_capenable;	/* i: capabilities enabled */
339 	union {
340 		void *		carp_s;	/* carp structure (used by !carp ifs) */
341 		struct ifnet	*carp_d;/* ptr to carpdev (used by carp ifs) */
342 	}		if_carp_ptr;	/* ?: */
343 #define if_carp		if_carp_ptr.carp_s
344 #define if_carpdev	if_carp_ptr.carp_d
345 	/*
346 	 * These are pre-computed based on an interfaces enabled
347 	 * capabilities, for speed elsewhere.
348 	 */
349 	int		if_csum_flags_tx;
350 					/* i: M_CSUM_* flags for Tx */
351 	int		if_csum_flags_rx;
352 					/* i: M_CSUM_* flags for Rx */
353 
354 	void		*if_afdata[AF_MAX];
355 					/* a: */
356 	struct mowner	*if_mowner;	/* ?: who owns mbufs for this interface */
357 
358 	void		*if_agrprivate;	/* ?: used only when #if NAGR > 0 */
359 	void		*if_npf_private;/* ?: associated NPF context */
360 
361 	/*
362 	 * pf specific data, used only when #if NPF > 0.
363 	 */
364 	void		*if_pf_kif;	/* ?: pf interface abstraction */
365 	void		*if_pf_groups;	/* ?: pf interface groups */
366 	/*
367 	 * During an ifnet's lifetime, it has only one if_index, but
368 	 * and if_index is not sufficient to identify an ifnet
369 	 * because during the lifetime of the system, many ifnets may occupy a
370 	 * given if_index.  Let us tell different ifnets at the same
371 	 * if_index apart by their if_index_gen, a unique number that each ifnet
372 	 * is assigned when it if_attach()s.  Now, the kernel can use the
373 	 * pair (if_index, if_index_gen) as a weak reference to an ifnet.
374 	 */
375 	uint64_t	if_index_gen;	/* :: generation number for the ifnet
376 					 * at if_index: if two ifnets' index
377 					 * and generation number are both the
378 					 * same, they are the same ifnet.
379 					 */
380 	struct sysctllog
381 			*if_sysctl_log;	/* :: */
382 	int		(*if_initaddr)  /* :: */
383 			    (struct ifnet *, struct ifaddr *, bool);
384 	int		(*if_setflags)	/* :: */
385 			    (struct ifnet *, const u_short);
386 	kmutex_t	*if_ioctl_lock;	/* :: */
387 	char		*if_description;	/* i: interface description */
388 #ifdef _KERNEL /* XXX kvm(3) */
389 	struct callout	*if_slowtimo_ch;/* :: */
390 	struct krwlock	*if_afdata_lock;/* :: */
391 	struct if_percpuq
392 			*if_percpuq;	/* :: we should remove it in the future */
393 	struct work	if_link_work;	/* q: linkage on link state work queue */
394 	uint16_t	if_link_queue;	/* q: masked link state change queue */
395 					/* q: is link state work scheduled? */
396 	bool		if_link_scheduled;
397 					/* q: can link state work be scheduled? */
398 	bool		if_link_cansched;
399 	struct pslist_entry
400 			if_pslist_entry;/* i: */
401 	struct psref_target
402 			if_psref;	/* :: */
403 	struct pslist_head
404 			if_addr_pslist;	/* i: */
405 	struct if_deferred_start
406 			*if_deferred_start;
407 					/* :: */
408 	/* XXX should be protocol independent */
409 	LIST_HEAD(, in6_multi)
410 			if_multiaddrs;	/* 6: */
411 #endif
412 } ifnet_t;
413 
414 #include <net/if_stats.h>
415 
416 #define	if_name(ifp)	((ifp)->if_xname)
417 
418 #define	IFF_UP		0x0001		/* interface is up */
419 #define	IFF_BROADCAST	0x0002		/* broadcast address valid */
420 #define	IFF_DEBUG	0x0004		/* turn on debugging */
421 #define	IFF_LOOPBACK	0x0008		/* is a loopback net */
422 #define	IFF_POINTOPOINT	0x0010		/* interface is point-to-point link */
423 /*			0x0020		   was IFF_NOTRAILERS */
424 #define	IFF_RUNNING	0x0040		/* resources allocated */
425 #define	IFF_NOARP	0x0080		/* no address resolution protocol */
426 #define	IFF_PROMISC	0x0100		/* receive all packets */
427 #define	IFF_ALLMULTI	0x0200		/* receive all multicast packets */
428 #define	IFF_OACTIVE	0x0400		/* transmission in progress */
429 #define	IFF_SIMPLEX	0x0800		/* can't hear own transmissions */
430 #define	IFF_LINK0	0x1000		/* per link layer defined bit */
431 #define	IFF_LINK1	0x2000		/* per link layer defined bit */
432 #define	IFF_LINK2	0x4000		/* per link layer defined bit */
433 #define	IFF_MULTICAST	0x8000		/* supports multicast */
434 
435 #define	IFEF_MPSAFE			__BIT(0)	/* handlers can run in parallel (see below) */
436 #define	IFEF_NO_LINK_STATE_CHANGE	__BIT(1)	/* doesn't use link state interrupts */
437 
438 /*
439  * The guidelines for converting an interface to IFEF_MPSAFE are as follows
440  *
441  * Enabling IFEF_MPSAFE on an interface suppresses taking KERNEL_LOCK when
442  * calling the following handlers:
443  * - if_start
444  *   - Note that if_transmit is always called without KERNEL_LOCK
445  * - if_output
446  * - if_ioctl
447  * - if_init
448  * - if_stop
449  *
450  * This means that an interface with IFEF_MPSAFE must make the above handlers
451  * MP-safe or take KERNEL_LOCK by itself inside handlers that aren't MP-safe
452  * yet.
453  *
454  * There are some additional restrictions to access member variables of struct
455  * ifnet:
456  * - if_flags
457  *   - Must be updated with holding IFNET_LOCK
458  *   - You cannot use the flag in Tx/Rx paths anymore because there is no
459  *     synchronization on the flag except for IFNET_LOCK
460  *   - Note that IFNET_LOCK can't be taken in softint because it's known
461  *     that it causes a deadlock
462  *     - Some synchronization mechanisms such as pserialize_perform are called
463  *       with IFNET_LOCK and also require context switches on every CPUs
464  *       that mean softints finish so trying to take IFNET_LOCK in softint
465  *       might block on IFNET_LOCK and prevent such synchronization mechanisms
466  *       from being completed
467  *     - Currently the deadlock occurs only if NET_MPSAFE is enabled, however,
468  *       we should deal with the restriction because NET_MPSAFE will be enabled
469  *       by default in the future
470  * - if_watchdog and if_timer
471  *   - The watchdog framework works only for non-IFEF_MPSAFE interfaces
472  *     that rely on KERNEL_LOCK
473  *   - Interfaces with IFEF_MPSAFE have to provide its own watchdog mechanism
474  *     if needed
475  *     - Keep if_watchdog NULL when calling if_attach
476  */
477 
478 #ifdef _KERNEL
479 static __inline bool
480 if_is_mpsafe(struct ifnet *ifp)
481 {
482 
483 	return ((ifp->if_extflags & IFEF_MPSAFE) != 0);
484 }
485 
486 static __inline int
487 if_output_lock(struct ifnet *cifp, struct ifnet *ifp, struct mbuf *m,
488     const struct sockaddr *dst, const struct rtentry *rt)
489 {
490 
491 	if (if_is_mpsafe(cifp)) {
492 		return (*cifp->if_output)(ifp, m, dst, rt);
493 	} else {
494 		int ret;
495 
496 		KERNEL_LOCK(1, NULL);
497 		ret = (*cifp->if_output)(ifp, m, dst, rt);
498 		KERNEL_UNLOCK_ONE(NULL);
499 		return ret;
500 	}
501 }
502 
503 static __inline void
504 if_start_lock(struct ifnet *ifp)
505 {
506 
507 	if (if_is_mpsafe(ifp)) {
508 		(*ifp->if_start)(ifp);
509 	} else {
510 		KERNEL_LOCK(1, NULL);
511 		(*ifp->if_start)(ifp);
512 		KERNEL_UNLOCK_ONE(NULL);
513 	}
514 }
515 
516 static __inline bool
517 if_is_link_state_changeable(struct ifnet *ifp)
518 {
519 
520 	return ((ifp->if_extflags & IFEF_NO_LINK_STATE_CHANGE) == 0);
521 }
522 
523 #define KERNEL_LOCK_IF_IFP_MPSAFE(ifp)					\
524 	do { if (if_is_mpsafe(ifp)) { KERNEL_LOCK(1, NULL); } } while (0)
525 #define KERNEL_UNLOCK_IF_IFP_MPSAFE(ifp)				\
526 	do { if (if_is_mpsafe(ifp)) { KERNEL_UNLOCK_ONE(NULL); } } while (0)
527 
528 #define KERNEL_LOCK_UNLESS_IFP_MPSAFE(ifp)				\
529 	do { if (!if_is_mpsafe(ifp)) { KERNEL_LOCK(1, NULL); } } while (0)
530 #define KERNEL_UNLOCK_UNLESS_IFP_MPSAFE(ifp)				\
531 	do { if (!if_is_mpsafe(ifp)) { KERNEL_UNLOCK_ONE(NULL); } } while (0)
532 
533 #ifdef _KERNEL_OPT
534 #include "opt_net_mpsafe.h"
535 #endif
536 
537 /* XXX explore a better place to define */
538 #ifdef NET_MPSAFE
539 
540 #define KERNEL_LOCK_UNLESS_NET_MPSAFE()		do { } while (0)
541 #define KERNEL_UNLOCK_UNLESS_NET_MPSAFE()	do { } while (0)
542 
543 #define SOFTNET_LOCK_UNLESS_NET_MPSAFE()	do { } while (0)
544 #define SOFTNET_UNLOCK_UNLESS_NET_MPSAFE()	do { } while (0)
545 
546 #define SOFTNET_LOCK_IF_NET_MPSAFE()					\
547 	do { mutex_enter(softnet_lock); } while (0)
548 #define SOFTNET_UNLOCK_IF_NET_MPSAFE()					\
549 	do { mutex_exit(softnet_lock); } while (0)
550 
551 #else /* NET_MPSAFE */
552 
553 #define KERNEL_LOCK_UNLESS_NET_MPSAFE()					\
554 	do { KERNEL_LOCK(1, NULL); } while (0)
555 #define KERNEL_UNLOCK_UNLESS_NET_MPSAFE()				\
556 	do { KERNEL_UNLOCK_ONE(NULL); } while (0)
557 
558 #define SOFTNET_LOCK_UNLESS_NET_MPSAFE()				\
559 	do { mutex_enter(softnet_lock); } while (0)
560 #define SOFTNET_UNLOCK_UNLESS_NET_MPSAFE()				\
561 	do { mutex_exit(softnet_lock); } while (0)
562 
563 #define SOFTNET_LOCK_IF_NET_MPSAFE()		do { } while (0)
564 #define SOFTNET_UNLOCK_IF_NET_MPSAFE()		do { } while (0)
565 
566 #endif /* NET_MPSAFE */
567 
568 #define SOFTNET_KERNEL_LOCK_UNLESS_NET_MPSAFE()				\
569 	do {								\
570 		SOFTNET_LOCK_UNLESS_NET_MPSAFE();			\
571 		KERNEL_LOCK_UNLESS_NET_MPSAFE();			\
572 	} while (0)
573 
574 #define SOFTNET_KERNEL_UNLOCK_UNLESS_NET_MPSAFE()			\
575 	do {								\
576 		KERNEL_UNLOCK_UNLESS_NET_MPSAFE();			\
577 		SOFTNET_UNLOCK_UNLESS_NET_MPSAFE();			\
578 	} while (0)
579 
580 #endif /* _KERNEL */
581 
582 #define	IFFBITS \
583     "\020\1UP\2BROADCAST\3DEBUG\4LOOPBACK\5POINTOPOINT" \
584     "\7RUNNING\10NOARP\11PROMISC\12ALLMULTI\13OACTIVE\14SIMPLEX" \
585     "\15LINK0\16LINK1\17LINK2\20MULTICAST"
586 
587 /* flags set internally only: */
588 #define	IFF_CANTCHANGE \
589 	(IFF_BROADCAST|IFF_POINTOPOINT|IFF_RUNNING|IFF_OACTIVE|\
590 	    IFF_SIMPLEX|IFF_MULTICAST|IFF_ALLMULTI|IFF_PROMISC)
591 
592 /*
593  * Some convenience macros used for setting ifi_baudrate.
594  */
595 #define	IF_Kbps(x)	((x) * 1000ULL)			/* kilobits/sec. */
596 #define	IF_Mbps(x)	(IF_Kbps((x) * 1000ULL))	/* megabits/sec. */
597 #define	IF_Gbps(x)	(IF_Mbps((x) * 1000ULL))	/* gigabits/sec. */
598 
599 /* Capabilities that interfaces can advertise. */
600 					/* 0x01 .. 0x40 were previously used */
601 #define	IFCAP_TSOv4		0x00080	/* can do TCPv4 segmentation offload */
602 #define	IFCAP_CSUM_IPv4_Rx	0x00100	/* can do IPv4 header checksums (Rx) */
603 #define	IFCAP_CSUM_IPv4_Tx	0x00200	/* can do IPv4 header checksums (Tx) */
604 #define	IFCAP_CSUM_TCPv4_Rx	0x00400	/* can do IPv4/TCP checksums (Rx) */
605 #define	IFCAP_CSUM_TCPv4_Tx	0x00800	/* can do IPv4/TCP checksums (Tx) */
606 #define	IFCAP_CSUM_UDPv4_Rx	0x01000	/* can do IPv4/UDP checksums (Rx) */
607 #define	IFCAP_CSUM_UDPv4_Tx	0x02000	/* can do IPv4/UDP checksums (Tx) */
608 #define	IFCAP_CSUM_TCPv6_Rx	0x04000	/* can do IPv6/TCP checksums (Rx) */
609 #define	IFCAP_CSUM_TCPv6_Tx	0x08000	/* can do IPv6/TCP checksums (Tx) */
610 #define	IFCAP_CSUM_UDPv6_Rx	0x10000	/* can do IPv6/UDP checksums (Rx) */
611 #define	IFCAP_CSUM_UDPv6_Tx	0x20000	/* can do IPv6/UDP checksums (Tx) */
612 #define	IFCAP_TSOv6		0x40000	/* can do TCPv6 segmentation offload */
613 #define	IFCAP_LRO		0x80000	/* can do Large Receive Offload */
614 #define	IFCAP_MASK		0xfff80 /* currently valid capabilities */
615 
616 #define	IFCAPBITS		\
617 	"\020"			\
618 	"\10TSO4"		\
619 	"\11IP4CSUM_Rx"		\
620 	"\12IP4CSUM_Tx"		\
621 	"\13TCP4CSUM_Rx"	\
622 	"\14TCP4CSUM_Tx"	\
623 	"\15UDP4CSUM_Rx"	\
624 	"\16UDP4CSUM_Tx"	\
625 	"\17TCP6CSUM_Rx"	\
626 	"\20TCP6CSUM_Tx"	\
627 	"\21UDP6CSUM_Rx"	\
628 	"\22UDP6CSUM_Tx"	\
629 	"\23TSO6"		\
630 	"\24LRO"		\
631 
632 #define	IF_AFDATA_LOCK_INIT(ifp)	\
633 	do {(ifp)->if_afdata_lock = rw_obj_alloc();} while (0)
634 
635 #define	IF_AFDATA_LOCK_DESTROY(ifp)	rw_obj_free((ifp)->if_afdata_lock)
636 
637 #define	IF_AFDATA_WLOCK(ifp)	rw_enter((ifp)->if_afdata_lock, RW_WRITER)
638 #define	IF_AFDATA_RLOCK(ifp)	rw_enter((ifp)->if_afdata_lock, RW_READER)
639 #define	IF_AFDATA_WUNLOCK(ifp)	rw_exit((ifp)->if_afdata_lock)
640 #define	IF_AFDATA_RUNLOCK(ifp)	rw_exit((ifp)->if_afdata_lock)
641 #define	IF_AFDATA_LOCK(ifp)	IF_AFDATA_WLOCK(ifp)
642 #define	IF_AFDATA_UNLOCK(ifp)	IF_AFDATA_WUNLOCK(ifp)
643 #define	IF_AFDATA_TRYLOCK(ifp)	rw_tryenter((ifp)->if_afdata_lock, RW_WRITER)
644 
645 #define	IF_AFDATA_LOCK_ASSERT(ifp)	\
646 	KASSERT(rw_lock_held((ifp)->if_afdata_lock))
647 #define	IF_AFDATA_RLOCK_ASSERT(ifp)	\
648 	KASSERT(rw_read_held((ifp)->if_afdata_lock))
649 #define	IF_AFDATA_WLOCK_ASSERT(ifp)	\
650 	KASSERT(rw_write_held((ifp)->if_afdata_lock))
651 
652 /*
653  * Output queues (ifp->if_snd) and internetwork datagram level (pup level 1)
654  * input routines have queues of messages stored on ifqueue structures
655  * (defined above).  Entries are added to and deleted from these structures
656  * by these macros, which should be called with ipl raised to splnet().
657  */
658 #define	IF_QFULL(ifq)		((ifq)->ifq_len >= (ifq)->ifq_maxlen)
659 #define	IF_DROP(ifq)		((ifq)->ifq_drops++)
660 #define	IF_ENQUEUE(ifq, m) do { \
661 	(m)->m_nextpkt = 0; \
662 	if ((ifq)->ifq_tail == 0) \
663 		(ifq)->ifq_head = m; \
664 	else \
665 		(ifq)->ifq_tail->m_nextpkt = m; \
666 	(ifq)->ifq_tail = m; \
667 	(ifq)->ifq_len++; \
668 } while (/*CONSTCOND*/0)
669 #define	IF_PREPEND(ifq, m) do { \
670 	(m)->m_nextpkt = (ifq)->ifq_head; \
671 	if ((ifq)->ifq_tail == 0) \
672 		(ifq)->ifq_tail = (m); \
673 	(ifq)->ifq_head = (m); \
674 	(ifq)->ifq_len++; \
675 } while (/*CONSTCOND*/0)
676 #define	IF_DEQUEUE(ifq, m) do { \
677 	(m) = (ifq)->ifq_head; \
678 	if (m) { \
679 		if (((ifq)->ifq_head = (m)->m_nextpkt) == 0) \
680 			(ifq)->ifq_tail = 0; \
681 		(m)->m_nextpkt = 0; \
682 		(ifq)->ifq_len--; \
683 	} \
684 } while (/*CONSTCOND*/0)
685 #define	IF_POLL(ifq, m)		((m) = (ifq)->ifq_head)
686 #define	IF_PURGE(ifq)							\
687 do {									\
688 	struct mbuf *__m0;						\
689 									\
690 	for (;;) {							\
691 		IF_DEQUEUE((ifq), __m0);				\
692 		if (__m0 == NULL)					\
693 			break;						\
694 		else							\
695 			m_freem(__m0);					\
696 	}								\
697 } while (/*CONSTCOND*/ 0)
698 #define	IF_IS_EMPTY(ifq)	((ifq)->ifq_len == 0)
699 
700 #ifndef IFQ_MAXLEN
701 #define	IFQ_MAXLEN	256
702 #endif
703 #define	IFNET_SLOWHZ	1		/* granularity is 1 second */
704 
705 /*
706  * Structure defining statistics and other data kept regarding an address
707  * on a network interface.
708  */
709 struct ifaddr_data {
710 	int64_t	ifad_inbytes;
711 	int64_t	ifad_outbytes;
712 };
713 
714 /*
715  * The ifaddr structure contains information about one address
716  * of an interface.  They are maintained by the different address families,
717  * are allocated and attached when an address is set, and are linked
718  * together so all addresses for an interface can be located.
719  */
720 struct ifaddr {
721 	struct	sockaddr *ifa_addr;	/* address of interface */
722 	struct	sockaddr *ifa_dstaddr;	/* other end of p-to-p link */
723 #define	ifa_broadaddr	ifa_dstaddr	/* broadcast address interface */
724 	struct	sockaddr *ifa_netmask;	/* used to determine subnet */
725 	struct	ifnet *ifa_ifp;		/* back-pointer to interface */
726 	TAILQ_ENTRY(ifaddr) ifa_list;	/* list of addresses for interface */
727 	struct	ifaddr_data	ifa_data;	/* statistics on the address */
728 	void	(*ifa_rtrequest)	/* check or clean routes (+ or -)'d */
729 		        (int, struct rtentry *, const struct rt_addrinfo *);
730 	u_int	ifa_flags;		/* mostly rt_flags for cloning */
731 	int	ifa_refcnt;		/* count of references */
732 	int	ifa_metric;		/* cost of going out this interface */
733 	struct ifaddr	*(*ifa_getifa)(struct ifaddr *,
734 			               const struct sockaddr *);
735 	uint32_t	*ifa_seqno;
736 	int16_t	ifa_preference;	/* preference level for this address */
737 #ifdef _KERNEL
738 	struct pslist_entry     ifa_pslist_entry;
739 	struct psref_target	ifa_psref;
740 #endif
741 };
742 #define	IFA_ROUTE	RTF_UP	/* (0x01) route installed */
743 #define	IFA_DESTROYING	0x2
744 
745 /*
746  * Message format for use in obtaining information about interfaces from
747  * sysctl and the routing socket.  We need to force 64-bit alignment if we
748  * aren't using compatiblity definitons.
749  */
750 #if !defined(_KERNEL) || !defined(COMPAT_RTSOCK)
751 #define	__align64	__aligned(sizeof(uint64_t))
752 #else
753 #define	__align64
754 #endif
755 struct if_msghdr {
756 	u_short	ifm_msglen __align64;
757 				/* to skip over non-understood messages */
758 	u_char	ifm_version;	/* future binary compatibility */
759 	u_char	ifm_type;	/* message type */
760 	int	ifm_addrs;	/* like rtm_addrs */
761 	int	ifm_flags;	/* value of if_flags */
762 	u_short	ifm_index;	/* index for associated ifp */
763 	struct	if_data ifm_data __align64;
764 				/* statistics and other data about if */
765 };
766 
767 /*
768  * Message format for use in obtaining information about interface addresses
769  * from sysctl and the routing socket.
770  */
771 struct ifa_msghdr {
772 	u_short	ifam_msglen __align64;
773 				/* to skip over non-understood messages */
774 	u_char	ifam_version;	/* future binary compatibility */
775 	u_char	ifam_type;	/* message type */
776 	u_short	ifam_index;	/* index for associated ifp */
777 	int	ifam_flags;	/* value of ifa_flags */
778 	int	ifam_addrs;	/* like rtm_addrs */
779 	pid_t	ifam_pid;	/* identify sender */
780 	int	ifam_addrflags;	/* family specific address flags */
781 	int	ifam_metric;	/* value of ifa_metric */
782 };
783 
784 /*
785  * Message format announcing the arrival or departure of a network interface.
786  */
787 struct if_announcemsghdr {
788 	u_short	ifan_msglen __align64;
789 				/* to skip over non-understood messages */
790 	u_char	ifan_version;	/* future binary compatibility */
791 	u_char	ifan_type;	/* message type */
792 	u_short	ifan_index;	/* index for associated ifp */
793 	char	ifan_name[IFNAMSIZ]; /* if name, e.g. "en0" */
794 	u_short	ifan_what;	/* what type of announcement */
795 };
796 
797 #define	IFAN_ARRIVAL	0	/* interface arrival */
798 #define	IFAN_DEPARTURE	1	/* interface departure */
799 
800 #undef __align64
801 
802 /*
803  * Interface request structure used for socket
804  * ioctl's.  All interface ioctl's must have parameter
805  * definitions which begin with ifr_name.  The
806  * remainder may be interface specific.
807  */
808 struct	ifreq {
809 	char	ifr_name[IFNAMSIZ];		/* if name, e.g. "en0" */
810 	union {
811 		struct	sockaddr ifru_addr;
812 		struct	sockaddr ifru_dstaddr;
813 		struct	sockaddr ifru_broadaddr;
814 		struct	sockaddr_storage ifru_space;
815 		short	ifru_flags;
816 		int	ifru_addrflags;
817 		int	ifru_metric;
818 		int	ifru_mtu;
819 		int	ifru_dlt;
820 		u_int	ifru_value;
821 		void *	ifru_data;
822 		struct {
823 			uint32_t	b_buflen;
824 			void		*b_buf;
825 		} ifru_b;
826 	} ifr_ifru;
827 #define	ifr_addr	ifr_ifru.ifru_addr	/* address */
828 #define	ifr_dstaddr	ifr_ifru.ifru_dstaddr	/* other end of p-to-p link */
829 #define	ifr_broadaddr	ifr_ifru.ifru_broadaddr	/* broadcast address */
830 #define	ifr_space	ifr_ifru.ifru_space	/* sockaddr_storage */
831 #define	ifr_flags	ifr_ifru.ifru_flags	/* flags */
832 #define	ifr_addrflags	ifr_ifru.ifru_addrflags	/* addr flags */
833 #define	ifr_metric	ifr_ifru.ifru_metric	/* metric */
834 #define	ifr_mtu		ifr_ifru.ifru_mtu	/* mtu */
835 #define	ifr_dlt		ifr_ifru.ifru_dlt	/* data link type (DLT_*) */
836 #define	ifr_value	ifr_ifru.ifru_value	/* generic value */
837 #define	ifr_media	ifr_ifru.ifru_metric	/* media options (overload) */
838 #define	ifr_data	ifr_ifru.ifru_data	/* for use by interface
839 						 * XXX deprecated
840 						 */
841 #define	ifr_buf		ifr_ifru.ifru_b.b_buf	/* new interface ioctls */
842 #define	ifr_buflen	ifr_ifru.ifru_b.b_buflen
843 #define	ifr_index	ifr_ifru.ifru_value	/* interface index, BSD */
844 #define	ifr_ifindex	ifr_index		/* interface index, linux */
845 };
846 
847 #ifdef _KERNEL
848 #define	ifreq_setdstaddr	ifreq_setaddr
849 #define	ifreq_setbroadaddr	ifreq_setaddr
850 #define	ifreq_getdstaddr	ifreq_getaddr
851 #define	ifreq_getbroadaddr	ifreq_getaddr
852 
853 static __inline const struct sockaddr *
854 /*ARGSUSED*/
855 ifreq_getaddr(u_long cmd, const struct ifreq *ifr)
856 {
857 	return &ifr->ifr_addr;
858 }
859 #endif /* _KERNEL */
860 
861 struct ifcapreq {
862 	char		ifcr_name[IFNAMSIZ];	/* if name, e.g. "en0" */
863 	uint64_t	ifcr_capabilities;	/* supported capabiliites */
864 	uint64_t	ifcr_capenable;		/* capabilities enabled */
865 };
866 
867 struct ifaliasreq {
868 	char	ifra_name[IFNAMSIZ];		/* if name, e.g. "en0" */
869 	struct	sockaddr ifra_addr;
870 	struct	sockaddr ifra_dstaddr;
871 #define	ifra_broadaddr	ifra_dstaddr
872 	struct	sockaddr ifra_mask;
873 };
874 
875 struct ifdatareq {
876 	char	ifdr_name[IFNAMSIZ];		/* if name, e.g. "en0" */
877 	struct	if_data ifdr_data;
878 };
879 
880 struct ifmediareq {
881 	char	ifm_name[IFNAMSIZ];	/* if name, e.g. "en0" */
882 	int	ifm_current;		/* IFMWD: current media options */
883 	int	ifm_mask;		/* IFMWD: don't care mask */
884 	int	ifm_status;		/* media status */
885 	int	ifm_active;		/* IFMWD: active options */
886 	int	ifm_count;		/* # entries in ifm_ulist
887 					   array */
888 	int	*ifm_ulist;		/* array of ifmedia word */
889 };
890 
891 
892 struct  ifdrv {
893 	char		ifd_name[IFNAMSIZ];	/* if name, e.g. "en0" */
894 	unsigned long	ifd_cmd;
895 	size_t		ifd_len;
896 	void		*ifd_data;
897 };
898 #define IFLINKSTR_QUERYLEN	0x01
899 #define IFLINKSTR_UNSET		0x02
900 
901 /*
902  * Structure used in SIOCGIFCONF request.
903  * Used to retrieve interface configuration
904  * for machine (useful for programs which
905  * must know all networks accessible).
906  */
907 struct	ifconf {
908 	int	ifc_len;		/* size of associated buffer */
909 	union {
910 		void *	ifcu_buf;
911 		struct	ifreq *ifcu_req;
912 	} ifc_ifcu;
913 #define	ifc_buf	ifc_ifcu.ifcu_buf	/* buffer address */
914 #define	ifc_req	ifc_ifcu.ifcu_req	/* array of structures returned */
915 };
916 
917 /*
918  * Structure for SIOC[AGD]LIFADDR
919  */
920 struct if_laddrreq {
921 	char iflr_name[IFNAMSIZ];
922 	unsigned int flags;
923 #define IFLR_PREFIX	0x8000	/* in: prefix given  out: kernel fills id */
924 #define IFLR_ACTIVE	0x4000	/* in/out: link-layer address activation */
925 #define IFLR_FACTORY	0x2000	/* in/out: factory link-layer address */
926 	unsigned int prefixlen;		/* in/out */
927 	struct sockaddr_storage addr;	/* in/out */
928 	struct sockaddr_storage dstaddr; /* out */
929 };
930 
931 /*
932  * Structure for SIOC[SG]IFADDRPREF
933  */
934 struct if_addrprefreq {
935 	char			ifap_name[IFNAMSIZ];
936 	int16_t			ifap_preference;	/* in/out */
937 	struct sockaddr_storage	ifap_addr;		/* in/out */
938 };
939 
940 #include <net/if_arp.h>
941 
942 #endif /* _NETBSD_SOURCE */
943 
944 #ifdef _KERNEL
945 #ifdef ALTQ
946 #define IFQ_ENQUEUE(ifq, m, err)					\
947 do {									\
948 	mutex_enter((ifq)->ifq_lock);					\
949 	if (ALTQ_IS_ENABLED(ifq))					\
950 		ALTQ_ENQUEUE((ifq), (m), (err));			\
951 	else {								\
952 		if (IF_QFULL(ifq)) {					\
953 			m_freem(m);					\
954 			(err) = ENOBUFS;				\
955 		} else {						\
956 			IF_ENQUEUE((ifq), (m));				\
957 			(err) = 0;					\
958 		}							\
959 	}								\
960 	if ((err))							\
961 		(ifq)->ifq_drops++;					\
962 	mutex_exit((ifq)->ifq_lock);					\
963 } while (/*CONSTCOND*/ 0)
964 
965 #define IFQ_DEQUEUE(ifq, m)						\
966 do {									\
967 	mutex_enter((ifq)->ifq_lock);					\
968 	if (TBR_IS_ENABLED(ifq))					\
969 		(m) = tbr_dequeue((ifq), ALTDQ_REMOVE);			\
970 	else if (ALTQ_IS_ENABLED(ifq))					\
971 		ALTQ_DEQUEUE((ifq), (m));				\
972 	else								\
973 		IF_DEQUEUE((ifq), (m));					\
974 	mutex_exit((ifq)->ifq_lock);					\
975 } while (/*CONSTCOND*/ 0)
976 
977 #define	IFQ_POLL(ifq, m)						\
978 do {									\
979 	mutex_enter((ifq)->ifq_lock);					\
980 	if (TBR_IS_ENABLED(ifq))					\
981 		(m) = tbr_dequeue((ifq), ALTDQ_POLL);			\
982 	else if (ALTQ_IS_ENABLED(ifq))					\
983 		ALTQ_POLL((ifq), (m));					\
984 	else								\
985 		IF_POLL((ifq), (m));					\
986 	mutex_exit((ifq)->ifq_lock);					\
987 } while (/*CONSTCOND*/ 0)
988 
989 #define	IFQ_PURGE(ifq)							\
990 do {									\
991 	mutex_enter((ifq)->ifq_lock);					\
992 	if (ALTQ_IS_ENABLED(ifq))					\
993 		ALTQ_PURGE(ifq);					\
994 	else								\
995 		IF_PURGE(ifq);						\
996 	mutex_exit((ifq)->ifq_lock);					\
997 } while (/*CONSTCOND*/ 0)
998 
999 #define	IFQ_SET_READY(ifq)						\
1000 do {									\
1001 	(ifq)->altq_flags |= ALTQF_READY;				\
1002 } while (/*CONSTCOND*/ 0)
1003 
1004 #define	IFQ_CLASSIFY(ifq, m, af)					\
1005 do {									\
1006 	KASSERT(((m)->m_flags & M_PKTHDR) != 0);			\
1007 	mutex_enter((ifq)->ifq_lock);					\
1008 	if (ALTQ_IS_ENABLED(ifq)) {					\
1009 		if (ALTQ_NEEDS_CLASSIFY(ifq))				\
1010 			(m)->m_pkthdr.pattr_class = (*(ifq)->altq_classify) \
1011 				((ifq)->altq_clfier, (m), (af));	\
1012 		(m)->m_pkthdr.pattr_af = (af);				\
1013 		(m)->m_pkthdr.pattr_hdr = mtod((m), void *);		\
1014 	}								\
1015 	mutex_exit((ifq)->ifq_lock);					\
1016 } while (/*CONSTCOND*/ 0)
1017 #else /* ! ALTQ */
1018 #define	IFQ_ENQUEUE(ifq, m, err)					\
1019 do {									\
1020 	mutex_enter((ifq)->ifq_lock);					\
1021 	if (IF_QFULL(ifq)) {						\
1022 		m_freem(m);						\
1023 		(err) = ENOBUFS;					\
1024 	} else {							\
1025 		IF_ENQUEUE((ifq), (m));					\
1026 		(err) = 0;						\
1027 	}								\
1028 	if (err)							\
1029 		(ifq)->ifq_drops++;					\
1030 	mutex_exit((ifq)->ifq_lock);					\
1031 } while (/*CONSTCOND*/ 0)
1032 
1033 #define	IFQ_DEQUEUE(ifq, m)						\
1034 do {									\
1035 	mutex_enter((ifq)->ifq_lock);					\
1036 	IF_DEQUEUE((ifq), (m));						\
1037 	mutex_exit((ifq)->ifq_lock);					\
1038 } while (/*CONSTCOND*/ 0)
1039 
1040 #define	IFQ_POLL(ifq, m)						\
1041 do {									\
1042 	mutex_enter((ifq)->ifq_lock);					\
1043 	IF_POLL((ifq), (m));						\
1044 	mutex_exit((ifq)->ifq_lock);					\
1045 } while (/*CONSTCOND*/ 0)
1046 
1047 #define	IFQ_PURGE(ifq)							\
1048 do {									\
1049 	mutex_enter((ifq)->ifq_lock);					\
1050 	IF_PURGE(ifq);							\
1051 	mutex_exit((ifq)->ifq_lock);					\
1052 } while (/*CONSTCOND*/ 0)
1053 
1054 #define	IFQ_SET_READY(ifq)	/* nothing */
1055 
1056 #define	IFQ_CLASSIFY(ifq, m, af) /* nothing */
1057 
1058 #endif /* ALTQ */
1059 
1060 #define IFQ_LOCK_INIT(ifq)	(ifq)->ifq_lock =			\
1061 	    mutex_obj_alloc(MUTEX_DEFAULT, IPL_NET)
1062 #define IFQ_LOCK_DESTROY(ifq)	mutex_obj_free((ifq)->ifq_lock)
1063 #define IFQ_LOCK(ifq)		mutex_enter((ifq)->ifq_lock)
1064 #define IFQ_UNLOCK(ifq)		mutex_exit((ifq)->ifq_lock)
1065 
1066 #define	IFQ_IS_EMPTY(ifq)		IF_IS_EMPTY(ifq)
1067 #define	IFQ_INC_LEN(ifq)		((ifq)->ifq_len++)
1068 #define	IFQ_DEC_LEN(ifq)		(--(ifq)->ifq_len)
1069 #define	IFQ_INC_DROPS(ifq)		((ifq)->ifq_drops++)
1070 #define	IFQ_SET_MAXLEN(ifq, len)	((ifq)->ifq_maxlen = (len))
1071 
1072 #define	IFQ_ENQUEUE_ISR(ifq, m, isr)					\
1073 do {									\
1074 	IFQ_LOCK(inq);							\
1075 	if (IF_QFULL(inq)) {						\
1076 		IF_DROP(inq);						\
1077 		IFQ_UNLOCK(inq);					\
1078 		m_freem(m);						\
1079 	} else {							\
1080 		IF_ENQUEUE(inq, m);					\
1081 		IFQ_UNLOCK(inq);					\
1082 		schednetisr(isr);					\
1083 	}								\
1084 } while (/*CONSTCOND*/ 0)
1085 
1086 #include <sys/mallocvar.h>
1087 MALLOC_DECLARE(M_IFADDR);
1088 MALLOC_DECLARE(M_IFMADDR);
1089 
1090 int ifreq_setaddr(u_long, struct ifreq *, const struct sockaddr *);
1091 
1092 struct ifnet *if_alloc(u_char);
1093 void if_free(struct ifnet *);
1094 void if_initname(struct ifnet *, const char *, int);
1095 struct ifaddr *if_dl_create(const struct ifnet *, const struct sockaddr_dl **);
1096 void if_activate_sadl(struct ifnet *, struct ifaddr *,
1097     const struct sockaddr_dl *);
1098 void	if_set_sadl(struct ifnet *, const void *, u_char, bool);
1099 void	if_alloc_sadl(struct ifnet *);
1100 void	if_free_sadl(struct ifnet *, int);
1101 int	if_initialize(struct ifnet *);
1102 void	if_register(struct ifnet *);
1103 int	if_attach(struct ifnet *); /* Deprecated. Use if_initialize and if_register */
1104 void	if_attachdomain(void);
1105 void	if_deactivate(struct ifnet *);
1106 bool	if_is_deactivated(const struct ifnet *);
1107 void	if_export_if_data(struct ifnet *, struct if_data *, bool);
1108 void	if_purgeaddrs(struct ifnet *, int, void (*)(struct ifaddr *));
1109 void	if_detach(struct ifnet *);
1110 void	if_down(struct ifnet *);
1111 void	if_down_locked(struct ifnet *);
1112 void	if_link_state_change(struct ifnet *, int);
1113 void	if_up(struct ifnet *);
1114 void	ifinit(void);
1115 void	ifinit1(void);
1116 void	ifinit_post(void);
1117 int	ifaddrpref_ioctl(struct socket *, u_long, void *, struct ifnet *);
1118 extern int (*ifioctl)(struct socket *, u_long, void *, struct lwp *);
1119 int	ifioctl_common(struct ifnet *, u_long, void *);
1120 int	ifpromisc(struct ifnet *, int);
1121 int	ifpromisc_locked(struct ifnet *, int);
1122 int	if_addr_init(ifnet_t *, struct ifaddr *, bool);
1123 int	if_do_dad(struct ifnet *);
1124 int	if_mcast_op(ifnet_t *, const unsigned long, const struct sockaddr *);
1125 int	if_flags_set(struct ifnet *, const u_short);
1126 int	if_clone_list(int, char *, int *);
1127 
1128 struct	ifnet *ifunit(const char *);
1129 struct	ifnet *if_get(const char *, struct psref *);
1130 ifnet_t *if_byindex(u_int);
1131 ifnet_t *_if_byindex(u_int);
1132 ifnet_t *if_get_byindex(u_int, struct psref *);
1133 ifnet_t *if_get_bylla(const void *, unsigned char, struct psref *);
1134 void	if_put(const struct ifnet *, struct psref *);
1135 void	if_acquire(struct ifnet *, struct psref *);
1136 #define	if_release	if_put
1137 
1138 int if_tunnel_check_nesting(struct ifnet *, struct mbuf *, int);
1139 percpu_t *if_tunnel_alloc_ro_percpu(void);
1140 void if_tunnel_free_ro_percpu(percpu_t *);
1141 void if_tunnel_ro_percpu_rtcache_free(percpu_t *);
1142 
1143 struct tunnel_ro {
1144 	struct route *tr_ro;
1145 	kmutex_t *tr_lock;
1146 };
1147 
1148 static inline void
1149 if_tunnel_get_ro(percpu_t *ro_percpu, struct route **ro, kmutex_t **lock)
1150 {
1151 	struct tunnel_ro *tro;
1152 
1153 	tro = percpu_getref(ro_percpu);
1154 	*ro = tro->tr_ro;
1155 	*lock = tro->tr_lock;
1156 	mutex_enter(*lock);
1157 }
1158 
1159 static inline void
1160 if_tunnel_put_ro(percpu_t *ro_percpu, kmutex_t *lock)
1161 {
1162 
1163 	mutex_exit(lock);
1164 	percpu_putref(ro_percpu);
1165 }
1166 
1167 static __inline if_index_t
1168 if_get_index(const struct ifnet *ifp)
1169 {
1170 
1171 	return ifp != NULL ? ifp->if_index : 0;
1172 }
1173 
1174 bool	if_held(struct ifnet *);
1175 
1176 void	if_input(struct ifnet *, struct mbuf *);
1177 
1178 struct if_percpuq *
1179 	if_percpuq_create(struct ifnet *);
1180 void	if_percpuq_destroy(struct if_percpuq *);
1181 void
1182 	if_percpuq_enqueue(struct if_percpuq *, struct mbuf *);
1183 
1184 void	if_deferred_start_init(struct ifnet *, void (*)(struct ifnet *));
1185 void	if_schedule_deferred_start(struct ifnet *);
1186 
1187 void ifa_insert(struct ifnet *, struct ifaddr *);
1188 void ifa_remove(struct ifnet *, struct ifaddr *);
1189 
1190 void	ifa_psref_init(struct ifaddr *);
1191 void	ifa_acquire(struct ifaddr *, struct psref *);
1192 void	ifa_release(struct ifaddr *, struct psref *);
1193 bool	ifa_held(struct ifaddr *);
1194 bool	ifa_is_destroying(struct ifaddr *);
1195 
1196 void	ifaref(struct ifaddr *);
1197 void	ifafree(struct ifaddr *);
1198 
1199 struct	ifaddr *ifa_ifwithaddr(const struct sockaddr *);
1200 struct	ifaddr *ifa_ifwithaddr_psref(const struct sockaddr *, struct psref *);
1201 struct	ifaddr *ifa_ifwithaf(int);
1202 struct	ifaddr *ifa_ifwithdstaddr(const struct sockaddr *);
1203 struct	ifaddr *ifa_ifwithdstaddr_psref(const struct sockaddr *,
1204 	    struct psref *);
1205 struct	ifaddr *ifa_ifwithnet(const struct sockaddr *);
1206 struct	ifaddr *ifa_ifwithnet_psref(const struct sockaddr *, struct psref *);
1207 struct	ifaddr *ifa_ifwithladdr(const struct sockaddr *);
1208 struct	ifaddr *ifa_ifwithladdr_psref(const struct sockaddr *, struct psref *);
1209 struct	ifaddr *ifaof_ifpforaddr(const struct sockaddr *, struct ifnet *);
1210 struct	ifaddr *ifaof_ifpforaddr_psref(const struct sockaddr *, struct ifnet *,
1211 	    struct psref *);
1212 void	link_rtrequest(int, struct rtentry *, const struct rt_addrinfo *);
1213 void	p2p_rtrequest(int, struct rtentry *, const struct rt_addrinfo *);
1214 
1215 void	if_clone_attach(struct if_clone *);
1216 void	if_clone_detach(struct if_clone *);
1217 
1218 int	if_transmit_lock(struct ifnet *, struct mbuf *);
1219 
1220 int	ifq_enqueue(struct ifnet *, struct mbuf *);
1221 int	ifq_enqueue2(struct ifnet *, struct ifqueue *, struct mbuf *);
1222 
1223 int	loioctl(struct ifnet *, u_long, void *);
1224 void	loopattach(int);
1225 void	loopinit(void);
1226 int	looutput(struct ifnet *,
1227 	   struct mbuf *, const struct sockaddr *, const struct rtentry *);
1228 
1229 /*
1230  * These are exported because they're an easy way to tell if
1231  * an interface is going away without having to burn a flag.
1232  */
1233 int	if_nulloutput(struct ifnet *, struct mbuf *,
1234 	    const struct sockaddr *, const struct rtentry *);
1235 void	if_nullinput(struct ifnet *, struct mbuf *);
1236 void	if_nullstart(struct ifnet *);
1237 int	if_nulltransmit(struct ifnet *, struct mbuf *);
1238 int	if_nullioctl(struct ifnet *, u_long, void *);
1239 int	if_nullinit(struct ifnet *);
1240 void	if_nullstop(struct ifnet *, int);
1241 void	if_nullslowtimo(struct ifnet *);
1242 #define	if_nullwatchdog	if_nullslowtimo
1243 void	if_nulldrain(struct ifnet *);
1244 #else
1245 struct if_nameindex {
1246 	unsigned int	if_index;	/* 1, 2, ... */
1247 	char		*if_name;	/* null terminated name: "le0", ... */
1248 };
1249 
1250 #include <sys/cdefs.h>
1251 __BEGIN_DECLS
1252 unsigned int if_nametoindex(const char *);
1253 char *	if_indextoname(unsigned int, char *);
1254 struct	if_nameindex * if_nameindex(void);
1255 void	if_freenameindex(struct if_nameindex *);
1256 __END_DECLS
1257 #endif /* _KERNEL */ /* XXX really ALTQ? */
1258 
1259 #ifdef _KERNEL
1260 
1261 #define	IFADDR_FIRST(__ifp)		TAILQ_FIRST(&(__ifp)->if_addrlist)
1262 #define	IFADDR_NEXT(__ifa)		TAILQ_NEXT((__ifa), ifa_list)
1263 #define	IFADDR_FOREACH(__ifa, __ifp)	TAILQ_FOREACH(__ifa, \
1264 					    &(__ifp)->if_addrlist, ifa_list)
1265 #define	IFADDR_FOREACH_SAFE(__ifa, __ifp, __nifa) \
1266 					    TAILQ_FOREACH_SAFE(__ifa, \
1267 					    &(__ifp)->if_addrlist, ifa_list, __nifa)
1268 #define	IFADDR_EMPTY(__ifp)		TAILQ_EMPTY(&(__ifp)->if_addrlist)
1269 
1270 #define IFADDR_ENTRY_INIT(__ifa)					\
1271 	PSLIST_ENTRY_INIT((__ifa), ifa_pslist_entry)
1272 #define IFADDR_ENTRY_DESTROY(__ifa)					\
1273 	PSLIST_ENTRY_DESTROY((__ifa), ifa_pslist_entry)
1274 #define IFADDR_READER_EMPTY(__ifp)					\
1275 	(PSLIST_READER_FIRST(&(__ifp)->if_addr_pslist, struct ifaddr,	\
1276 	                     ifa_pslist_entry) == NULL)
1277 #define IFADDR_READER_FIRST(__ifp)					\
1278 	PSLIST_READER_FIRST(&(__ifp)->if_addr_pslist, struct ifaddr,	\
1279 	                    ifa_pslist_entry)
1280 #define IFADDR_READER_NEXT(__ifa)					\
1281 	PSLIST_READER_NEXT((__ifa), struct ifaddr, ifa_pslist_entry)
1282 #define IFADDR_READER_FOREACH(__ifa, __ifp)				\
1283 	PSLIST_READER_FOREACH((__ifa), &(__ifp)->if_addr_pslist, struct ifaddr,\
1284 	                      ifa_pslist_entry)
1285 #define IFADDR_WRITER_INSERT_HEAD(__ifp, __ifa)				\
1286 	PSLIST_WRITER_INSERT_HEAD(&(__ifp)->if_addr_pslist, (__ifa),	\
1287 	                          ifa_pslist_entry)
1288 #define IFADDR_WRITER_REMOVE(__ifa)					\
1289 	PSLIST_WRITER_REMOVE((__ifa), ifa_pslist_entry)
1290 #define IFADDR_WRITER_FOREACH(__ifa, __ifp)				\
1291 	PSLIST_WRITER_FOREACH((__ifa), &(__ifp)->if_addr_pslist, struct ifaddr,\
1292 	                      ifa_pslist_entry)
1293 #define IFADDR_WRITER_NEXT(__ifp)					\
1294 	PSLIST_WRITER_NEXT((__ifp), struct ifaddr, ifa_pslist_entry)
1295 #define IFADDR_WRITER_INSERT_AFTER(__ifp, __new)			\
1296 	PSLIST_WRITER_INSERT_AFTER((__ifp), (__new), ifa_pslist_entry)
1297 #define IFADDR_WRITER_EMPTY(__ifp)					\
1298 	(PSLIST_WRITER_FIRST(&(__ifp)->if_addr_pslist, struct ifaddr,	\
1299 	                     ifa_pslist_entry) == NULL)
1300 #define IFADDR_WRITER_INSERT_TAIL(__ifp, __new)				\
1301 	do {								\
1302 		if (IFADDR_WRITER_EMPTY(__ifp)) {			\
1303 			IFADDR_WRITER_INSERT_HEAD((__ifp), (__new));	\
1304 		} else {						\
1305 			struct ifaddr *__ifa;				\
1306 			IFADDR_WRITER_FOREACH(__ifa, (__ifp)) {		\
1307 				if (IFADDR_WRITER_NEXT(__ifa) == NULL) {\
1308 					IFADDR_WRITER_INSERT_AFTER(__ifa,\
1309 					    (__new));			\
1310 					break;				\
1311 				}					\
1312 			}						\
1313 		}							\
1314 	} while (0)
1315 
1316 #define	IFNET_GLOBAL_LOCK()			mutex_enter(&ifnet_mtx)
1317 #define	IFNET_GLOBAL_UNLOCK()			mutex_exit(&ifnet_mtx)
1318 #define	IFNET_GLOBAL_LOCKED()			mutex_owned(&ifnet_mtx)
1319 
1320 #define IFNET_READER_EMPTY() \
1321 	(PSLIST_READER_FIRST(&ifnet_pslist, struct ifnet, if_pslist_entry) == NULL)
1322 #define IFNET_READER_FIRST() \
1323 	PSLIST_READER_FIRST(&ifnet_pslist, struct ifnet, if_pslist_entry)
1324 #define IFNET_READER_NEXT(__ifp) \
1325 	PSLIST_READER_NEXT((__ifp), struct ifnet, if_pslist_entry)
1326 #define IFNET_READER_FOREACH(__ifp) \
1327 	PSLIST_READER_FOREACH((__ifp), &ifnet_pslist, struct ifnet, \
1328 	                      if_pslist_entry)
1329 #define IFNET_WRITER_INSERT_HEAD(__ifp) \
1330 	PSLIST_WRITER_INSERT_HEAD(&ifnet_pslist, (__ifp), if_pslist_entry)
1331 #define IFNET_WRITER_REMOVE(__ifp) \
1332 	PSLIST_WRITER_REMOVE((__ifp), if_pslist_entry)
1333 #define IFNET_WRITER_FOREACH(__ifp) \
1334 	PSLIST_WRITER_FOREACH((__ifp), &ifnet_pslist, struct ifnet, \
1335 	                      if_pslist_entry)
1336 #define IFNET_WRITER_NEXT(__ifp) \
1337 	PSLIST_WRITER_NEXT((__ifp), struct ifnet, if_pslist_entry)
1338 #define IFNET_WRITER_INSERT_AFTER(__ifp, __new) \
1339 	PSLIST_WRITER_INSERT_AFTER((__ifp), (__new), if_pslist_entry)
1340 #define IFNET_WRITER_EMPTY() \
1341 	(PSLIST_WRITER_FIRST(&ifnet_pslist, struct ifnet, if_pslist_entry) == NULL)
1342 #define IFNET_WRITER_INSERT_TAIL(__new)					\
1343 	do {								\
1344 		if (IFNET_WRITER_EMPTY()) {				\
1345 			IFNET_WRITER_INSERT_HEAD(__new);		\
1346 		} else {						\
1347 			struct ifnet *__ifp;				\
1348 			IFNET_WRITER_FOREACH(__ifp) {			\
1349 				if (IFNET_WRITER_NEXT(__ifp) == NULL) {	\
1350 					IFNET_WRITER_INSERT_AFTER(__ifp,\
1351 					    (__new));			\
1352 					break;				\
1353 				}					\
1354 			}						\
1355 		}							\
1356 	} while (0)
1357 
1358 #define IFNET_LOCK(ifp)		mutex_enter((ifp)->if_ioctl_lock)
1359 #define IFNET_UNLOCK(ifp)	mutex_exit((ifp)->if_ioctl_lock)
1360 #define IFNET_LOCKED(ifp)	mutex_owned((ifp)->if_ioctl_lock)
1361 
1362 #define IFNET_ASSERT_UNLOCKED(ifp)	\
1363 	KDASSERT(mutex_ownable((ifp)->if_ioctl_lock))
1364 
1365 extern struct pslist_head ifnet_pslist;
1366 extern kmutex_t ifnet_mtx;
1367 
1368 extern struct ifnet *lo0ifp;
1369 
1370 /*
1371  * ifq sysctl support
1372  */
1373 int	sysctl_ifq(int *name, u_int namelen, void *oldp,
1374 		       size_t *oldlenp, void *newp, size_t newlen,
1375 		       struct ifqueue *ifq);
1376 /* symbolic names for terminal (per-protocol) CTL_IFQ_ nodes */
1377 #define IFQCTL_LEN	1
1378 #define IFQCTL_MAXLEN	2
1379 #define IFQCTL_PEAK	3
1380 #define IFQCTL_DROPS	4
1381 
1382 /*
1383  * Hook for if_vlan - needed by if_agr
1384  */
1385 MODULE_HOOK(if_vlan_vlan_input_hook, void, (struct ifnet *, struct mbuf *));
1386 
1387 #endif /* _KERNEL */
1388 
1389 #endif /* !_NET_IF_H_ */
1390