xref: /netbsd-src/sys/net/if.h (revision e6c7e151de239c49d2e38720a061ed9d1fa99309)
1 /*	$NetBSD: if.h,v 1.282 2020/02/14 22:04:12 thorpej 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_mcastop)	/* :: */
385 			    (struct ifnet *, const unsigned long,
386 			    const struct sockaddr *);
387 	int		(*if_setflags)	/* :: */
388 			    (struct ifnet *, const u_short);
389 	kmutex_t	*if_ioctl_lock;	/* :: */
390 	char		*if_description;	/* i: interface description */
391 #ifdef _KERNEL /* XXX kvm(3) */
392 	struct callout	*if_slowtimo_ch;/* :: */
393 	struct krwlock	*if_afdata_lock;/* :: */
394 	struct if_percpuq
395 			*if_percpuq;	/* :: we should remove it in the future */
396 	struct work	if_link_work;	/* q: linkage on link state work queue */
397 	uint16_t	if_link_queue;	/* q: masked link state change queue */
398 					/* q: is link state work scheduled? */
399 	bool		if_link_scheduled;
400 					/* q: can link state work be scheduled? */
401 	bool		if_link_cansched;
402 	struct pslist_entry
403 			if_pslist_entry;/* i: */
404 	struct psref_target
405 			if_psref;	/* :: */
406 	struct pslist_head
407 			if_addr_pslist;	/* i: */
408 	struct if_deferred_start
409 			*if_deferred_start;
410 					/* :: */
411 	/* XXX should be protocol independent */
412 	LIST_HEAD(, in6_multi)
413 			if_multiaddrs;	/* 6: */
414 #endif
415 } ifnet_t;
416 
417 #include <net/if_stats.h>
418 
419 #define	if_name(ifp)	((ifp)->if_xname)
420 
421 #define	IFF_UP		0x0001		/* interface is up */
422 #define	IFF_BROADCAST	0x0002		/* broadcast address valid */
423 #define	IFF_DEBUG	0x0004		/* turn on debugging */
424 #define	IFF_LOOPBACK	0x0008		/* is a loopback net */
425 #define	IFF_POINTOPOINT	0x0010		/* interface is point-to-point link */
426 /*			0x0020		   was IFF_NOTRAILERS */
427 #define	IFF_RUNNING	0x0040		/* resources allocated */
428 #define	IFF_NOARP	0x0080		/* no address resolution protocol */
429 #define	IFF_PROMISC	0x0100		/* receive all packets */
430 #define	IFF_ALLMULTI	0x0200		/* receive all multicast packets */
431 #define	IFF_OACTIVE	0x0400		/* transmission in progress */
432 #define	IFF_SIMPLEX	0x0800		/* can't hear own transmissions */
433 #define	IFF_LINK0	0x1000		/* per link layer defined bit */
434 #define	IFF_LINK1	0x2000		/* per link layer defined bit */
435 #define	IFF_LINK2	0x4000		/* per link layer defined bit */
436 #define	IFF_MULTICAST	0x8000		/* supports multicast */
437 
438 #define	IFEF_MPSAFE			__BIT(0)	/* handlers can run in parallel (see below) */
439 #define	IFEF_NO_LINK_STATE_CHANGE	__BIT(1)	/* doesn't use link state interrupts */
440 
441 /*
442  * The guidelines for converting an interface to IFEF_MPSAFE are as follows
443  *
444  * Enabling IFEF_MPSAFE on an interface suppresses taking KERNEL_LOCK when
445  * calling the following handlers:
446  * - if_start
447  *   - Note that if_transmit is always called without KERNEL_LOCK
448  * - if_output
449  * - if_ioctl
450  * - if_init
451  * - if_stop
452  *
453  * This means that an interface with IFEF_MPSAFE must make the above handlers
454  * MP-safe or take KERNEL_LOCK by itself inside handlers that aren't MP-safe
455  * yet.
456  *
457  * There are some additional restrictions to access member variables of struct
458  * ifnet:
459  * - if_flags
460  *   - Must be updated with holding IFNET_LOCK
461  *   - You cannot use the flag in Tx/Rx paths anymore because there is no
462  *     synchronization on the flag except for IFNET_LOCK
463  *   - Note that IFNET_LOCK can't be taken in softint because it's known
464  *     that it causes a deadlock
465  *     - Some synchronization mechanisms such as pserialize_perform are called
466  *       with IFNET_LOCK and also require context switches on every CPUs
467  *       that mean softints finish so trying to take IFNET_LOCK in softint
468  *       might block on IFNET_LOCK and prevent such synchronization mechanisms
469  *       from being completed
470  *     - Currently the deadlock occurs only if NET_MPSAFE is enabled, however,
471  *       we should deal with the restriction because NET_MPSAFE will be enabled
472  *       by default in the future
473  * - if_watchdog and if_timer
474  *   - The watchdog framework works only for non-IFEF_MPSAFE interfaces
475  *     that rely on KERNEL_LOCK
476  *   - Interfaces with IFEF_MPSAFE have to provide its own watchdog mechanism
477  *     if needed
478  *     - Keep if_watchdog NULL when calling if_attach
479  */
480 
481 #ifdef _KERNEL
482 static __inline bool
483 if_is_mpsafe(struct ifnet *ifp)
484 {
485 
486 	return ((ifp->if_extflags & IFEF_MPSAFE) != 0);
487 }
488 
489 static __inline int
490 if_output_lock(struct ifnet *cifp, struct ifnet *ifp, struct mbuf *m,
491     const struct sockaddr *dst, const struct rtentry *rt)
492 {
493 
494 	if (if_is_mpsafe(cifp)) {
495 		return (*cifp->if_output)(ifp, m, dst, rt);
496 	} else {
497 		int ret;
498 
499 		KERNEL_LOCK(1, NULL);
500 		ret = (*cifp->if_output)(ifp, m, dst, rt);
501 		KERNEL_UNLOCK_ONE(NULL);
502 		return ret;
503 	}
504 }
505 
506 static __inline void
507 if_start_lock(struct ifnet *ifp)
508 {
509 
510 	if (if_is_mpsafe(ifp)) {
511 		(*ifp->if_start)(ifp);
512 	} else {
513 		KERNEL_LOCK(1, NULL);
514 		(*ifp->if_start)(ifp);
515 		KERNEL_UNLOCK_ONE(NULL);
516 	}
517 }
518 
519 static __inline bool
520 if_is_link_state_changeable(struct ifnet *ifp)
521 {
522 
523 	return ((ifp->if_extflags & IFEF_NO_LINK_STATE_CHANGE) == 0);
524 }
525 
526 #define KERNEL_LOCK_IF_IFP_MPSAFE(ifp)					\
527 	do { if (if_is_mpsafe(ifp)) { KERNEL_LOCK(1, NULL); } } while (0)
528 #define KERNEL_UNLOCK_IF_IFP_MPSAFE(ifp)				\
529 	do { if (if_is_mpsafe(ifp)) { KERNEL_UNLOCK_ONE(NULL); } } while (0)
530 
531 #define KERNEL_LOCK_UNLESS_IFP_MPSAFE(ifp)				\
532 	do { if (!if_is_mpsafe(ifp)) { KERNEL_LOCK(1, NULL); } } while (0)
533 #define KERNEL_UNLOCK_UNLESS_IFP_MPSAFE(ifp)				\
534 	do { if (!if_is_mpsafe(ifp)) { KERNEL_UNLOCK_ONE(NULL); } } while (0)
535 
536 #ifdef _KERNEL_OPT
537 #include "opt_net_mpsafe.h"
538 #endif
539 
540 /* XXX explore a better place to define */
541 #ifdef NET_MPSAFE
542 
543 #define KERNEL_LOCK_UNLESS_NET_MPSAFE()		do { } while (0)
544 #define KERNEL_UNLOCK_UNLESS_NET_MPSAFE()	do { } while (0)
545 
546 #define SOFTNET_LOCK_UNLESS_NET_MPSAFE()	do { } while (0)
547 #define SOFTNET_UNLOCK_UNLESS_NET_MPSAFE()	do { } while (0)
548 
549 #define SOFTNET_LOCK_IF_NET_MPSAFE()					\
550 	do { mutex_enter(softnet_lock); } while (0)
551 #define SOFTNET_UNLOCK_IF_NET_MPSAFE()					\
552 	do { mutex_exit(softnet_lock); } while (0)
553 
554 #else /* NET_MPSAFE */
555 
556 #define KERNEL_LOCK_UNLESS_NET_MPSAFE()					\
557 	do { KERNEL_LOCK(1, NULL); } while (0)
558 #define KERNEL_UNLOCK_UNLESS_NET_MPSAFE()				\
559 	do { KERNEL_UNLOCK_ONE(NULL); } while (0)
560 
561 #define SOFTNET_LOCK_UNLESS_NET_MPSAFE()				\
562 	do { mutex_enter(softnet_lock); } while (0)
563 #define SOFTNET_UNLOCK_UNLESS_NET_MPSAFE()				\
564 	do { mutex_exit(softnet_lock); } while (0)
565 
566 #define SOFTNET_LOCK_IF_NET_MPSAFE()		do { } while (0)
567 #define SOFTNET_UNLOCK_IF_NET_MPSAFE()		do { } while (0)
568 
569 #endif /* NET_MPSAFE */
570 
571 #define SOFTNET_KERNEL_LOCK_UNLESS_NET_MPSAFE()				\
572 	do {								\
573 		SOFTNET_LOCK_UNLESS_NET_MPSAFE();			\
574 		KERNEL_LOCK_UNLESS_NET_MPSAFE();			\
575 	} while (0)
576 
577 #define SOFTNET_KERNEL_UNLOCK_UNLESS_NET_MPSAFE()			\
578 	do {								\
579 		KERNEL_UNLOCK_UNLESS_NET_MPSAFE();			\
580 		SOFTNET_UNLOCK_UNLESS_NET_MPSAFE();			\
581 	} while (0)
582 
583 #endif /* _KERNEL */
584 
585 #define	IFFBITS \
586     "\020\1UP\2BROADCAST\3DEBUG\4LOOPBACK\5POINTOPOINT" \
587     "\7RUNNING\10NOARP\11PROMISC\12ALLMULTI\13OACTIVE\14SIMPLEX" \
588     "\15LINK0\16LINK1\17LINK2\20MULTICAST"
589 
590 /* flags set internally only: */
591 #define	IFF_CANTCHANGE \
592 	(IFF_BROADCAST|IFF_POINTOPOINT|IFF_RUNNING|IFF_OACTIVE|\
593 	    IFF_SIMPLEX|IFF_MULTICAST|IFF_ALLMULTI|IFF_PROMISC)
594 
595 /*
596  * Some convenience macros used for setting ifi_baudrate.
597  */
598 #define	IF_Kbps(x)	((x) * 1000ULL)			/* kilobits/sec. */
599 #define	IF_Mbps(x)	(IF_Kbps((x) * 1000ULL))	/* megabits/sec. */
600 #define	IF_Gbps(x)	(IF_Mbps((x) * 1000ULL))	/* gigabits/sec. */
601 
602 /* Capabilities that interfaces can advertise. */
603 					/* 0x01 .. 0x40 were previously used */
604 #define	IFCAP_TSOv4		0x00080	/* can do TCPv4 segmentation offload */
605 #define	IFCAP_CSUM_IPv4_Rx	0x00100	/* can do IPv4 header checksums (Rx) */
606 #define	IFCAP_CSUM_IPv4_Tx	0x00200	/* can do IPv4 header checksums (Tx) */
607 #define	IFCAP_CSUM_TCPv4_Rx	0x00400	/* can do IPv4/TCP checksums (Rx) */
608 #define	IFCAP_CSUM_TCPv4_Tx	0x00800	/* can do IPv4/TCP checksums (Tx) */
609 #define	IFCAP_CSUM_UDPv4_Rx	0x01000	/* can do IPv4/UDP checksums (Rx) */
610 #define	IFCAP_CSUM_UDPv4_Tx	0x02000	/* can do IPv4/UDP checksums (Tx) */
611 #define	IFCAP_CSUM_TCPv6_Rx	0x04000	/* can do IPv6/TCP checksums (Rx) */
612 #define	IFCAP_CSUM_TCPv6_Tx	0x08000	/* can do IPv6/TCP checksums (Tx) */
613 #define	IFCAP_CSUM_UDPv6_Rx	0x10000	/* can do IPv6/UDP checksums (Rx) */
614 #define	IFCAP_CSUM_UDPv6_Tx	0x20000	/* can do IPv6/UDP checksums (Tx) */
615 #define	IFCAP_TSOv6		0x40000	/* can do TCPv6 segmentation offload */
616 #define	IFCAP_LRO		0x80000	/* can do Large Receive Offload */
617 #define	IFCAP_MASK		0xfff80 /* currently valid capabilities */
618 
619 #define	IFCAPBITS		\
620 	"\020"			\
621 	"\10TSO4"		\
622 	"\11IP4CSUM_Rx"		\
623 	"\12IP4CSUM_Tx"		\
624 	"\13TCP4CSUM_Rx"	\
625 	"\14TCP4CSUM_Tx"	\
626 	"\15UDP4CSUM_Rx"	\
627 	"\16UDP4CSUM_Tx"	\
628 	"\17TCP6CSUM_Rx"	\
629 	"\20TCP6CSUM_Tx"	\
630 	"\21UDP6CSUM_Rx"	\
631 	"\22UDP6CSUM_Tx"	\
632 	"\23TSO6"		\
633 	"\24LRO"		\
634 
635 #define	IF_AFDATA_LOCK_INIT(ifp)	\
636 	do {(ifp)->if_afdata_lock = rw_obj_alloc();} while (0)
637 
638 #define	IF_AFDATA_LOCK_DESTROY(ifp)	rw_obj_free((ifp)->if_afdata_lock)
639 
640 #define	IF_AFDATA_WLOCK(ifp)	rw_enter((ifp)->if_afdata_lock, RW_WRITER)
641 #define	IF_AFDATA_RLOCK(ifp)	rw_enter((ifp)->if_afdata_lock, RW_READER)
642 #define	IF_AFDATA_WUNLOCK(ifp)	rw_exit((ifp)->if_afdata_lock)
643 #define	IF_AFDATA_RUNLOCK(ifp)	rw_exit((ifp)->if_afdata_lock)
644 #define	IF_AFDATA_LOCK(ifp)	IF_AFDATA_WLOCK(ifp)
645 #define	IF_AFDATA_UNLOCK(ifp)	IF_AFDATA_WUNLOCK(ifp)
646 #define	IF_AFDATA_TRYLOCK(ifp)	rw_tryenter((ifp)->if_afdata_lock, RW_WRITER)
647 
648 #define	IF_AFDATA_LOCK_ASSERT(ifp)	\
649 	KASSERT(rw_lock_held((ifp)->if_afdata_lock))
650 #define	IF_AFDATA_RLOCK_ASSERT(ifp)	\
651 	KASSERT(rw_read_held((ifp)->if_afdata_lock))
652 #define	IF_AFDATA_WLOCK_ASSERT(ifp)	\
653 	KASSERT(rw_write_held((ifp)->if_afdata_lock))
654 
655 /*
656  * Output queues (ifp->if_snd) and internetwork datagram level (pup level 1)
657  * input routines have queues of messages stored on ifqueue structures
658  * (defined above).  Entries are added to and deleted from these structures
659  * by these macros, which should be called with ipl raised to splnet().
660  */
661 #define	IF_QFULL(ifq)		((ifq)->ifq_len >= (ifq)->ifq_maxlen)
662 #define	IF_DROP(ifq)		((ifq)->ifq_drops++)
663 #define	IF_ENQUEUE(ifq, m) do { \
664 	(m)->m_nextpkt = 0; \
665 	if ((ifq)->ifq_tail == 0) \
666 		(ifq)->ifq_head = m; \
667 	else \
668 		(ifq)->ifq_tail->m_nextpkt = m; \
669 	(ifq)->ifq_tail = m; \
670 	(ifq)->ifq_len++; \
671 } while (/*CONSTCOND*/0)
672 #define	IF_PREPEND(ifq, m) do { \
673 	(m)->m_nextpkt = (ifq)->ifq_head; \
674 	if ((ifq)->ifq_tail == 0) \
675 		(ifq)->ifq_tail = (m); \
676 	(ifq)->ifq_head = (m); \
677 	(ifq)->ifq_len++; \
678 } while (/*CONSTCOND*/0)
679 #define	IF_DEQUEUE(ifq, m) do { \
680 	(m) = (ifq)->ifq_head; \
681 	if (m) { \
682 		if (((ifq)->ifq_head = (m)->m_nextpkt) == 0) \
683 			(ifq)->ifq_tail = 0; \
684 		(m)->m_nextpkt = 0; \
685 		(ifq)->ifq_len--; \
686 	} \
687 } while (/*CONSTCOND*/0)
688 #define	IF_POLL(ifq, m)		((m) = (ifq)->ifq_head)
689 #define	IF_PURGE(ifq)							\
690 do {									\
691 	struct mbuf *__m0;						\
692 									\
693 	for (;;) {							\
694 		IF_DEQUEUE((ifq), __m0);				\
695 		if (__m0 == NULL)					\
696 			break;						\
697 		else							\
698 			m_freem(__m0);					\
699 	}								\
700 } while (/*CONSTCOND*/ 0)
701 #define	IF_IS_EMPTY(ifq)	((ifq)->ifq_len == 0)
702 
703 #ifndef IFQ_MAXLEN
704 #define	IFQ_MAXLEN	256
705 #endif
706 #define	IFNET_SLOWHZ	1		/* granularity is 1 second */
707 
708 /*
709  * Structure defining statistics and other data kept regarding an address
710  * on a network interface.
711  */
712 struct ifaddr_data {
713 	int64_t	ifad_inbytes;
714 	int64_t	ifad_outbytes;
715 };
716 
717 /*
718  * The ifaddr structure contains information about one address
719  * of an interface.  They are maintained by the different address families,
720  * are allocated and attached when an address is set, and are linked
721  * together so all addresses for an interface can be located.
722  */
723 struct ifaddr {
724 	struct	sockaddr *ifa_addr;	/* address of interface */
725 	struct	sockaddr *ifa_dstaddr;	/* other end of p-to-p link */
726 #define	ifa_broadaddr	ifa_dstaddr	/* broadcast address interface */
727 	struct	sockaddr *ifa_netmask;	/* used to determine subnet */
728 	struct	ifnet *ifa_ifp;		/* back-pointer to interface */
729 	TAILQ_ENTRY(ifaddr) ifa_list;	/* list of addresses for interface */
730 	struct	ifaddr_data	ifa_data;	/* statistics on the address */
731 	void	(*ifa_rtrequest)	/* check or clean routes (+ or -)'d */
732 		        (int, struct rtentry *, const struct rt_addrinfo *);
733 	u_int	ifa_flags;		/* mostly rt_flags for cloning */
734 	int	ifa_refcnt;		/* count of references */
735 	int	ifa_metric;		/* cost of going out this interface */
736 	struct ifaddr	*(*ifa_getifa)(struct ifaddr *,
737 			               const struct sockaddr *);
738 	uint32_t	*ifa_seqno;
739 	int16_t	ifa_preference;	/* preference level for this address */
740 #ifdef _KERNEL
741 	struct pslist_entry     ifa_pslist_entry;
742 	struct psref_target	ifa_psref;
743 #endif
744 };
745 #define	IFA_ROUTE	RTF_UP	/* (0x01) route installed */
746 #define	IFA_DESTROYING	0x2
747 
748 /*
749  * Message format for use in obtaining information about interfaces from
750  * sysctl and the routing socket.  We need to force 64-bit alignment if we
751  * aren't using compatiblity definitons.
752  */
753 #if !defined(_KERNEL) || !defined(COMPAT_RTSOCK)
754 #define	__align64	__aligned(sizeof(uint64_t))
755 #else
756 #define	__align64
757 #endif
758 struct if_msghdr {
759 	u_short	ifm_msglen __align64;
760 				/* to skip over non-understood messages */
761 	u_char	ifm_version;	/* future binary compatibility */
762 	u_char	ifm_type;	/* message type */
763 	int	ifm_addrs;	/* like rtm_addrs */
764 	int	ifm_flags;	/* value of if_flags */
765 	u_short	ifm_index;	/* index for associated ifp */
766 	struct	if_data ifm_data __align64;
767 				/* statistics and other data about if */
768 };
769 
770 /*
771  * Message format for use in obtaining information about interface addresses
772  * from sysctl and the routing socket.
773  */
774 struct ifa_msghdr {
775 	u_short	ifam_msglen __align64;
776 				/* to skip over non-understood messages */
777 	u_char	ifam_version;	/* future binary compatibility */
778 	u_char	ifam_type;	/* message type */
779 	u_short	ifam_index;	/* index for associated ifp */
780 	int	ifam_flags;	/* value of ifa_flags */
781 	int	ifam_addrs;	/* like rtm_addrs */
782 	pid_t	ifam_pid;	/* identify sender */
783 	int	ifam_addrflags;	/* family specific address flags */
784 	int	ifam_metric;	/* value of ifa_metric */
785 };
786 
787 /*
788  * Message format announcing the arrival or departure of a network interface.
789  */
790 struct if_announcemsghdr {
791 	u_short	ifan_msglen __align64;
792 				/* to skip over non-understood messages */
793 	u_char	ifan_version;	/* future binary compatibility */
794 	u_char	ifan_type;	/* message type */
795 	u_short	ifan_index;	/* index for associated ifp */
796 	char	ifan_name[IFNAMSIZ]; /* if name, e.g. "en0" */
797 	u_short	ifan_what;	/* what type of announcement */
798 };
799 
800 #define	IFAN_ARRIVAL	0	/* interface arrival */
801 #define	IFAN_DEPARTURE	1	/* interface departure */
802 
803 #undef __align64
804 
805 /*
806  * Interface request structure used for socket
807  * ioctl's.  All interface ioctl's must have parameter
808  * definitions which begin with ifr_name.  The
809  * remainder may be interface specific.
810  */
811 struct	ifreq {
812 	char	ifr_name[IFNAMSIZ];		/* if name, e.g. "en0" */
813 	union {
814 		struct	sockaddr ifru_addr;
815 		struct	sockaddr ifru_dstaddr;
816 		struct	sockaddr ifru_broadaddr;
817 		struct	sockaddr_storage ifru_space;
818 		short	ifru_flags;
819 		int	ifru_addrflags;
820 		int	ifru_metric;
821 		int	ifru_mtu;
822 		int	ifru_dlt;
823 		u_int	ifru_value;
824 		void *	ifru_data;
825 		struct {
826 			uint32_t	b_buflen;
827 			void		*b_buf;
828 		} ifru_b;
829 	} ifr_ifru;
830 #define	ifr_addr	ifr_ifru.ifru_addr	/* address */
831 #define	ifr_dstaddr	ifr_ifru.ifru_dstaddr	/* other end of p-to-p link */
832 #define	ifr_broadaddr	ifr_ifru.ifru_broadaddr	/* broadcast address */
833 #define	ifr_space	ifr_ifru.ifru_space	/* sockaddr_storage */
834 #define	ifr_flags	ifr_ifru.ifru_flags	/* flags */
835 #define	ifr_addrflags	ifr_ifru.ifru_addrflags	/* addr flags */
836 #define	ifr_metric	ifr_ifru.ifru_metric	/* metric */
837 #define	ifr_mtu		ifr_ifru.ifru_mtu	/* mtu */
838 #define	ifr_dlt		ifr_ifru.ifru_dlt	/* data link type (DLT_*) */
839 #define	ifr_value	ifr_ifru.ifru_value	/* generic value */
840 #define	ifr_media	ifr_ifru.ifru_metric	/* media options (overload) */
841 #define	ifr_data	ifr_ifru.ifru_data	/* for use by interface
842 						 * XXX deprecated
843 						 */
844 #define	ifr_buf		ifr_ifru.ifru_b.b_buf	/* new interface ioctls */
845 #define	ifr_buflen	ifr_ifru.ifru_b.b_buflen
846 #define	ifr_index	ifr_ifru.ifru_value	/* interface index, BSD */
847 #define	ifr_ifindex	ifr_index		/* interface index, linux */
848 };
849 
850 #ifdef _KERNEL
851 #define	ifreq_setdstaddr	ifreq_setaddr
852 #define	ifreq_setbroadaddr	ifreq_setaddr
853 #define	ifreq_getdstaddr	ifreq_getaddr
854 #define	ifreq_getbroadaddr	ifreq_getaddr
855 
856 static __inline const struct sockaddr *
857 /*ARGSUSED*/
858 ifreq_getaddr(u_long cmd, const struct ifreq *ifr)
859 {
860 	return &ifr->ifr_addr;
861 }
862 #endif /* _KERNEL */
863 
864 struct ifcapreq {
865 	char		ifcr_name[IFNAMSIZ];	/* if name, e.g. "en0" */
866 	uint64_t	ifcr_capabilities;	/* supported capabiliites */
867 	uint64_t	ifcr_capenable;		/* capabilities enabled */
868 };
869 
870 struct ifaliasreq {
871 	char	ifra_name[IFNAMSIZ];		/* if name, e.g. "en0" */
872 	struct	sockaddr ifra_addr;
873 	struct	sockaddr ifra_dstaddr;
874 #define	ifra_broadaddr	ifra_dstaddr
875 	struct	sockaddr ifra_mask;
876 };
877 
878 struct ifdatareq {
879 	char	ifdr_name[IFNAMSIZ];		/* if name, e.g. "en0" */
880 	struct	if_data ifdr_data;
881 };
882 
883 struct ifmediareq {
884 	char	ifm_name[IFNAMSIZ];	/* if name, e.g. "en0" */
885 	int	ifm_current;		/* IFMWD: current media options */
886 	int	ifm_mask;		/* IFMWD: don't care mask */
887 	int	ifm_status;		/* media status */
888 	int	ifm_active;		/* IFMWD: active options */
889 	int	ifm_count;		/* # entries in ifm_ulist
890 					   array */
891 	int	*ifm_ulist;		/* array of ifmedia word */
892 };
893 
894 
895 struct  ifdrv {
896 	char		ifd_name[IFNAMSIZ];	/* if name, e.g. "en0" */
897 	unsigned long	ifd_cmd;
898 	size_t		ifd_len;
899 	void		*ifd_data;
900 };
901 #define IFLINKSTR_QUERYLEN	0x01
902 #define IFLINKSTR_UNSET		0x02
903 
904 /*
905  * Structure used in SIOCGIFCONF request.
906  * Used to retrieve interface configuration
907  * for machine (useful for programs which
908  * must know all networks accessible).
909  */
910 struct	ifconf {
911 	int	ifc_len;		/* size of associated buffer */
912 	union {
913 		void *	ifcu_buf;
914 		struct	ifreq *ifcu_req;
915 	} ifc_ifcu;
916 #define	ifc_buf	ifc_ifcu.ifcu_buf	/* buffer address */
917 #define	ifc_req	ifc_ifcu.ifcu_req	/* array of structures returned */
918 };
919 
920 /*
921  * Structure for SIOC[AGD]LIFADDR
922  */
923 struct if_laddrreq {
924 	char iflr_name[IFNAMSIZ];
925 	unsigned int flags;
926 #define IFLR_PREFIX	0x8000	/* in: prefix given  out: kernel fills id */
927 #define IFLR_ACTIVE	0x4000	/* in/out: link-layer address activation */
928 #define IFLR_FACTORY	0x2000	/* in/out: factory link-layer address */
929 	unsigned int prefixlen;		/* in/out */
930 	struct sockaddr_storage addr;	/* in/out */
931 	struct sockaddr_storage dstaddr; /* out */
932 };
933 
934 /*
935  * Structure for SIOC[SG]IFADDRPREF
936  */
937 struct if_addrprefreq {
938 	char			ifap_name[IFNAMSIZ];
939 	int16_t			ifap_preference;	/* in/out */
940 	struct sockaddr_storage	ifap_addr;		/* in/out */
941 };
942 
943 #include <net/if_arp.h>
944 
945 #endif /* _NETBSD_SOURCE */
946 
947 #ifdef _KERNEL
948 #ifdef ALTQ
949 #define IFQ_ENQUEUE(ifq, m, err)					\
950 do {									\
951 	mutex_enter((ifq)->ifq_lock);					\
952 	if (ALTQ_IS_ENABLED(ifq))					\
953 		ALTQ_ENQUEUE((ifq), (m), (err));			\
954 	else {								\
955 		if (IF_QFULL(ifq)) {					\
956 			m_freem(m);					\
957 			(err) = ENOBUFS;				\
958 		} else {						\
959 			IF_ENQUEUE((ifq), (m));				\
960 			(err) = 0;					\
961 		}							\
962 	}								\
963 	if ((err))							\
964 		(ifq)->ifq_drops++;					\
965 	mutex_exit((ifq)->ifq_lock);					\
966 } while (/*CONSTCOND*/ 0)
967 
968 #define IFQ_DEQUEUE(ifq, m)						\
969 do {									\
970 	mutex_enter((ifq)->ifq_lock);					\
971 	if (TBR_IS_ENABLED(ifq))					\
972 		(m) = tbr_dequeue((ifq), ALTDQ_REMOVE);			\
973 	else if (ALTQ_IS_ENABLED(ifq))					\
974 		ALTQ_DEQUEUE((ifq), (m));				\
975 	else								\
976 		IF_DEQUEUE((ifq), (m));					\
977 	mutex_exit((ifq)->ifq_lock);					\
978 } while (/*CONSTCOND*/ 0)
979 
980 #define	IFQ_POLL(ifq, m)						\
981 do {									\
982 	mutex_enter((ifq)->ifq_lock);					\
983 	if (TBR_IS_ENABLED(ifq))					\
984 		(m) = tbr_dequeue((ifq), ALTDQ_POLL);			\
985 	else if (ALTQ_IS_ENABLED(ifq))					\
986 		ALTQ_POLL((ifq), (m));					\
987 	else								\
988 		IF_POLL((ifq), (m));					\
989 	mutex_exit((ifq)->ifq_lock);					\
990 } while (/*CONSTCOND*/ 0)
991 
992 #define	IFQ_PURGE(ifq)							\
993 do {									\
994 	mutex_enter((ifq)->ifq_lock);					\
995 	if (ALTQ_IS_ENABLED(ifq))					\
996 		ALTQ_PURGE(ifq);					\
997 	else								\
998 		IF_PURGE(ifq);						\
999 	mutex_exit((ifq)->ifq_lock);					\
1000 } while (/*CONSTCOND*/ 0)
1001 
1002 #define	IFQ_SET_READY(ifq)						\
1003 do {									\
1004 	(ifq)->altq_flags |= ALTQF_READY;				\
1005 } while (/*CONSTCOND*/ 0)
1006 
1007 #define	IFQ_CLASSIFY(ifq, m, af)					\
1008 do {									\
1009 	KASSERT(((m)->m_flags & M_PKTHDR) != 0);			\
1010 	mutex_enter((ifq)->ifq_lock);					\
1011 	if (ALTQ_IS_ENABLED(ifq)) {					\
1012 		if (ALTQ_NEEDS_CLASSIFY(ifq))				\
1013 			(m)->m_pkthdr.pattr_class = (*(ifq)->altq_classify) \
1014 				((ifq)->altq_clfier, (m), (af));	\
1015 		(m)->m_pkthdr.pattr_af = (af);				\
1016 		(m)->m_pkthdr.pattr_hdr = mtod((m), void *);		\
1017 	}								\
1018 	mutex_exit((ifq)->ifq_lock);					\
1019 } while (/*CONSTCOND*/ 0)
1020 #else /* ! ALTQ */
1021 #define	IFQ_ENQUEUE(ifq, m, err)					\
1022 do {									\
1023 	mutex_enter((ifq)->ifq_lock);					\
1024 	if (IF_QFULL(ifq)) {						\
1025 		m_freem(m);						\
1026 		(err) = ENOBUFS;					\
1027 	} else {							\
1028 		IF_ENQUEUE((ifq), (m));					\
1029 		(err) = 0;						\
1030 	}								\
1031 	if (err)							\
1032 		(ifq)->ifq_drops++;					\
1033 	mutex_exit((ifq)->ifq_lock);					\
1034 } while (/*CONSTCOND*/ 0)
1035 
1036 #define	IFQ_DEQUEUE(ifq, m)						\
1037 do {									\
1038 	mutex_enter((ifq)->ifq_lock);					\
1039 	IF_DEQUEUE((ifq), (m));						\
1040 	mutex_exit((ifq)->ifq_lock);					\
1041 } while (/*CONSTCOND*/ 0)
1042 
1043 #define	IFQ_POLL(ifq, m)						\
1044 do {									\
1045 	mutex_enter((ifq)->ifq_lock);					\
1046 	IF_POLL((ifq), (m));						\
1047 	mutex_exit((ifq)->ifq_lock);					\
1048 } while (/*CONSTCOND*/ 0)
1049 
1050 #define	IFQ_PURGE(ifq)							\
1051 do {									\
1052 	mutex_enter((ifq)->ifq_lock);					\
1053 	IF_PURGE(ifq);							\
1054 	mutex_exit((ifq)->ifq_lock);					\
1055 } while (/*CONSTCOND*/ 0)
1056 
1057 #define	IFQ_SET_READY(ifq)	/* nothing */
1058 
1059 #define	IFQ_CLASSIFY(ifq, m, af) /* nothing */
1060 
1061 #endif /* ALTQ */
1062 
1063 #define IFQ_LOCK_INIT(ifq)	(ifq)->ifq_lock =			\
1064 	    mutex_obj_alloc(MUTEX_DEFAULT, IPL_NET)
1065 #define IFQ_LOCK_DESTROY(ifq)	mutex_obj_free((ifq)->ifq_lock)
1066 #define IFQ_LOCK(ifq)		mutex_enter((ifq)->ifq_lock)
1067 #define IFQ_UNLOCK(ifq)		mutex_exit((ifq)->ifq_lock)
1068 
1069 #define	IFQ_IS_EMPTY(ifq)		IF_IS_EMPTY(ifq)
1070 #define	IFQ_INC_LEN(ifq)		((ifq)->ifq_len++)
1071 #define	IFQ_DEC_LEN(ifq)		(--(ifq)->ifq_len)
1072 #define	IFQ_INC_DROPS(ifq)		((ifq)->ifq_drops++)
1073 #define	IFQ_SET_MAXLEN(ifq, len)	((ifq)->ifq_maxlen = (len))
1074 
1075 #include <sys/mallocvar.h>
1076 MALLOC_DECLARE(M_IFADDR);
1077 MALLOC_DECLARE(M_IFMADDR);
1078 
1079 int ifreq_setaddr(u_long, struct ifreq *, const struct sockaddr *);
1080 
1081 struct ifnet *if_alloc(u_char);
1082 void if_free(struct ifnet *);
1083 void if_initname(struct ifnet *, const char *, int);
1084 struct ifaddr *if_dl_create(const struct ifnet *, const struct sockaddr_dl **);
1085 void if_activate_sadl(struct ifnet *, struct ifaddr *,
1086     const struct sockaddr_dl *);
1087 void	if_set_sadl(struct ifnet *, const void *, u_char, bool);
1088 void	if_alloc_sadl(struct ifnet *);
1089 void	if_free_sadl(struct ifnet *, int);
1090 int	if_initialize(struct ifnet *);
1091 void	if_register(struct ifnet *);
1092 int	if_attach(struct ifnet *); /* Deprecated. Use if_initialize and if_register */
1093 void	if_attachdomain(void);
1094 void	if_deactivate(struct ifnet *);
1095 bool	if_is_deactivated(const struct ifnet *);
1096 void	if_export_if_data(struct ifnet *, struct if_data *, bool);
1097 void	if_purgeaddrs(struct ifnet *, int, void (*)(struct ifaddr *));
1098 void	if_detach(struct ifnet *);
1099 void	if_down(struct ifnet *);
1100 void	if_down_locked(struct ifnet *);
1101 void	if_link_state_change(struct ifnet *, int);
1102 void	if_up(struct ifnet *);
1103 void	ifinit(void);
1104 void	ifinit1(void);
1105 void	ifinit_post(void);
1106 int	ifaddrpref_ioctl(struct socket *, u_long, void *, struct ifnet *);
1107 extern int (*ifioctl)(struct socket *, u_long, void *, struct lwp *);
1108 int	ifioctl_common(struct ifnet *, u_long, void *);
1109 int	ifpromisc(struct ifnet *, int);
1110 int	ifpromisc_locked(struct ifnet *, int);
1111 int	if_addr_init(ifnet_t *, struct ifaddr *, bool);
1112 int	if_do_dad(struct ifnet *);
1113 int	if_mcast_op(ifnet_t *, const unsigned long, const struct sockaddr *);
1114 int	if_flags_set(struct ifnet *, const u_short);
1115 int	if_clone_list(int, char *, int *);
1116 
1117 struct	ifnet *ifunit(const char *);
1118 struct	ifnet *if_get(const char *, struct psref *);
1119 ifnet_t *if_byindex(u_int);
1120 ifnet_t *_if_byindex(u_int);
1121 ifnet_t *if_get_byindex(u_int, struct psref *);
1122 ifnet_t *if_get_bylla(const void *, unsigned char, struct psref *);
1123 void	if_put(const struct ifnet *, struct psref *);
1124 void	if_acquire(struct ifnet *, struct psref *);
1125 #define	if_release	if_put
1126 
1127 int if_tunnel_check_nesting(struct ifnet *, struct mbuf *, int);
1128 percpu_t *if_tunnel_alloc_ro_percpu(void);
1129 void if_tunnel_free_ro_percpu(percpu_t *);
1130 void if_tunnel_ro_percpu_rtcache_free(percpu_t *);
1131 
1132 struct tunnel_ro {
1133 	struct route *tr_ro;
1134 	kmutex_t *tr_lock;
1135 };
1136 
1137 static inline void
1138 if_tunnel_get_ro(percpu_t *ro_percpu, struct route **ro, kmutex_t **lock)
1139 {
1140 	struct tunnel_ro *tro;
1141 
1142 	tro = percpu_getref(ro_percpu);
1143 	*ro = tro->tr_ro;
1144 	*lock = tro->tr_lock;
1145 	mutex_enter(*lock);
1146 }
1147 
1148 static inline void
1149 if_tunnel_put_ro(percpu_t *ro_percpu, kmutex_t *lock)
1150 {
1151 
1152 	mutex_exit(lock);
1153 	percpu_putref(ro_percpu);
1154 }
1155 
1156 static __inline if_index_t
1157 if_get_index(const struct ifnet *ifp)
1158 {
1159 
1160 	return ifp != NULL ? ifp->if_index : 0;
1161 }
1162 
1163 bool	if_held(struct ifnet *);
1164 
1165 void	if_input(struct ifnet *, struct mbuf *);
1166 
1167 struct if_percpuq *
1168 	if_percpuq_create(struct ifnet *);
1169 void	if_percpuq_destroy(struct if_percpuq *);
1170 void
1171 	if_percpuq_enqueue(struct if_percpuq *, struct mbuf *);
1172 
1173 void	if_deferred_start_init(struct ifnet *, void (*)(struct ifnet *));
1174 void	if_schedule_deferred_start(struct ifnet *);
1175 
1176 void ifa_insert(struct ifnet *, struct ifaddr *);
1177 void ifa_remove(struct ifnet *, struct ifaddr *);
1178 
1179 void	ifa_psref_init(struct ifaddr *);
1180 void	ifa_acquire(struct ifaddr *, struct psref *);
1181 void	ifa_release(struct ifaddr *, struct psref *);
1182 bool	ifa_held(struct ifaddr *);
1183 bool	ifa_is_destroying(struct ifaddr *);
1184 
1185 void	ifaref(struct ifaddr *);
1186 void	ifafree(struct ifaddr *);
1187 
1188 struct	ifaddr *ifa_ifwithaddr(const struct sockaddr *);
1189 struct	ifaddr *ifa_ifwithaddr_psref(const struct sockaddr *, struct psref *);
1190 struct	ifaddr *ifa_ifwithaf(int);
1191 struct	ifaddr *ifa_ifwithdstaddr(const struct sockaddr *);
1192 struct	ifaddr *ifa_ifwithdstaddr_psref(const struct sockaddr *,
1193 	    struct psref *);
1194 struct	ifaddr *ifa_ifwithnet(const struct sockaddr *);
1195 struct	ifaddr *ifa_ifwithnet_psref(const struct sockaddr *, struct psref *);
1196 struct	ifaddr *ifa_ifwithladdr(const struct sockaddr *);
1197 struct	ifaddr *ifa_ifwithladdr_psref(const struct sockaddr *, struct psref *);
1198 struct	ifaddr *ifaof_ifpforaddr(const struct sockaddr *, struct ifnet *);
1199 struct	ifaddr *ifaof_ifpforaddr_psref(const struct sockaddr *, struct ifnet *,
1200 	    struct psref *);
1201 void	link_rtrequest(int, struct rtentry *, const struct rt_addrinfo *);
1202 void	p2p_rtrequest(int, struct rtentry *, const struct rt_addrinfo *);
1203 
1204 void	if_clone_attach(struct if_clone *);
1205 void	if_clone_detach(struct if_clone *);
1206 
1207 int	if_transmit_lock(struct ifnet *, struct mbuf *);
1208 
1209 int	ifq_enqueue(struct ifnet *, struct mbuf *);
1210 int	ifq_enqueue2(struct ifnet *, struct ifqueue *, struct mbuf *);
1211 
1212 int	loioctl(struct ifnet *, u_long, void *);
1213 void	loopattach(int);
1214 void	loopinit(void);
1215 int	looutput(struct ifnet *,
1216 	   struct mbuf *, const struct sockaddr *, const struct rtentry *);
1217 
1218 /*
1219  * These are exported because they're an easy way to tell if
1220  * an interface is going away without having to burn a flag.
1221  */
1222 int	if_nulloutput(struct ifnet *, struct mbuf *,
1223 	    const struct sockaddr *, const struct rtentry *);
1224 void	if_nullinput(struct ifnet *, struct mbuf *);
1225 void	if_nullstart(struct ifnet *);
1226 int	if_nulltransmit(struct ifnet *, struct mbuf *);
1227 int	if_nullioctl(struct ifnet *, u_long, void *);
1228 int	if_nullinit(struct ifnet *);
1229 void	if_nullstop(struct ifnet *, int);
1230 void	if_nullslowtimo(struct ifnet *);
1231 #define	if_nullwatchdog	if_nullslowtimo
1232 void	if_nulldrain(struct ifnet *);
1233 #else
1234 struct if_nameindex {
1235 	unsigned int	if_index;	/* 1, 2, ... */
1236 	char		*if_name;	/* null terminated name: "le0", ... */
1237 };
1238 
1239 #include <sys/cdefs.h>
1240 __BEGIN_DECLS
1241 unsigned int if_nametoindex(const char *);
1242 char *	if_indextoname(unsigned int, char *);
1243 struct	if_nameindex * if_nameindex(void);
1244 void	if_freenameindex(struct if_nameindex *);
1245 __END_DECLS
1246 #endif /* _KERNEL */ /* XXX really ALTQ? */
1247 
1248 #ifdef _KERNEL
1249 
1250 #define	IFADDR_FIRST(__ifp)		TAILQ_FIRST(&(__ifp)->if_addrlist)
1251 #define	IFADDR_NEXT(__ifa)		TAILQ_NEXT((__ifa), ifa_list)
1252 #define	IFADDR_FOREACH(__ifa, __ifp)	TAILQ_FOREACH(__ifa, \
1253 					    &(__ifp)->if_addrlist, ifa_list)
1254 #define	IFADDR_FOREACH_SAFE(__ifa, __ifp, __nifa) \
1255 					    TAILQ_FOREACH_SAFE(__ifa, \
1256 					    &(__ifp)->if_addrlist, ifa_list, __nifa)
1257 #define	IFADDR_EMPTY(__ifp)		TAILQ_EMPTY(&(__ifp)->if_addrlist)
1258 
1259 #define IFADDR_ENTRY_INIT(__ifa)					\
1260 	PSLIST_ENTRY_INIT((__ifa), ifa_pslist_entry)
1261 #define IFADDR_ENTRY_DESTROY(__ifa)					\
1262 	PSLIST_ENTRY_DESTROY((__ifa), ifa_pslist_entry)
1263 #define IFADDR_READER_EMPTY(__ifp)					\
1264 	(PSLIST_READER_FIRST(&(__ifp)->if_addr_pslist, struct ifaddr,	\
1265 	                     ifa_pslist_entry) == NULL)
1266 #define IFADDR_READER_FIRST(__ifp)					\
1267 	PSLIST_READER_FIRST(&(__ifp)->if_addr_pslist, struct ifaddr,	\
1268 	                    ifa_pslist_entry)
1269 #define IFADDR_READER_NEXT(__ifa)					\
1270 	PSLIST_READER_NEXT((__ifa), struct ifaddr, ifa_pslist_entry)
1271 #define IFADDR_READER_FOREACH(__ifa, __ifp)				\
1272 	PSLIST_READER_FOREACH((__ifa), &(__ifp)->if_addr_pslist, struct ifaddr,\
1273 	                      ifa_pslist_entry)
1274 #define IFADDR_WRITER_INSERT_HEAD(__ifp, __ifa)				\
1275 	PSLIST_WRITER_INSERT_HEAD(&(__ifp)->if_addr_pslist, (__ifa),	\
1276 	                          ifa_pslist_entry)
1277 #define IFADDR_WRITER_REMOVE(__ifa)					\
1278 	PSLIST_WRITER_REMOVE((__ifa), ifa_pslist_entry)
1279 #define IFADDR_WRITER_FOREACH(__ifa, __ifp)				\
1280 	PSLIST_WRITER_FOREACH((__ifa), &(__ifp)->if_addr_pslist, struct ifaddr,\
1281 	                      ifa_pslist_entry)
1282 #define IFADDR_WRITER_NEXT(__ifp)					\
1283 	PSLIST_WRITER_NEXT((__ifp), struct ifaddr, ifa_pslist_entry)
1284 #define IFADDR_WRITER_INSERT_AFTER(__ifp, __new)			\
1285 	PSLIST_WRITER_INSERT_AFTER((__ifp), (__new), ifa_pslist_entry)
1286 #define IFADDR_WRITER_EMPTY(__ifp)					\
1287 	(PSLIST_WRITER_FIRST(&(__ifp)->if_addr_pslist, struct ifaddr,	\
1288 	                     ifa_pslist_entry) == NULL)
1289 #define IFADDR_WRITER_INSERT_TAIL(__ifp, __new)				\
1290 	do {								\
1291 		if (IFADDR_WRITER_EMPTY(__ifp)) {			\
1292 			IFADDR_WRITER_INSERT_HEAD((__ifp), (__new));	\
1293 		} else {						\
1294 			struct ifaddr *__ifa;				\
1295 			IFADDR_WRITER_FOREACH(__ifa, (__ifp)) {		\
1296 				if (IFADDR_WRITER_NEXT(__ifa) == NULL) {\
1297 					IFADDR_WRITER_INSERT_AFTER(__ifa,\
1298 					    (__new));			\
1299 					break;				\
1300 				}					\
1301 			}						\
1302 		}							\
1303 	} while (0)
1304 
1305 #define	IFNET_GLOBAL_LOCK()			mutex_enter(&ifnet_mtx)
1306 #define	IFNET_GLOBAL_UNLOCK()			mutex_exit(&ifnet_mtx)
1307 #define	IFNET_GLOBAL_LOCKED()			mutex_owned(&ifnet_mtx)
1308 
1309 #define IFNET_READER_EMPTY() \
1310 	(PSLIST_READER_FIRST(&ifnet_pslist, struct ifnet, if_pslist_entry) == NULL)
1311 #define IFNET_READER_FIRST() \
1312 	PSLIST_READER_FIRST(&ifnet_pslist, struct ifnet, if_pslist_entry)
1313 #define IFNET_READER_NEXT(__ifp) \
1314 	PSLIST_READER_NEXT((__ifp), struct ifnet, if_pslist_entry)
1315 #define IFNET_READER_FOREACH(__ifp) \
1316 	PSLIST_READER_FOREACH((__ifp), &ifnet_pslist, struct ifnet, \
1317 	                      if_pslist_entry)
1318 #define IFNET_WRITER_INSERT_HEAD(__ifp) \
1319 	PSLIST_WRITER_INSERT_HEAD(&ifnet_pslist, (__ifp), if_pslist_entry)
1320 #define IFNET_WRITER_REMOVE(__ifp) \
1321 	PSLIST_WRITER_REMOVE((__ifp), if_pslist_entry)
1322 #define IFNET_WRITER_FOREACH(__ifp) \
1323 	PSLIST_WRITER_FOREACH((__ifp), &ifnet_pslist, struct ifnet, \
1324 	                      if_pslist_entry)
1325 #define IFNET_WRITER_NEXT(__ifp) \
1326 	PSLIST_WRITER_NEXT((__ifp), struct ifnet, if_pslist_entry)
1327 #define IFNET_WRITER_INSERT_AFTER(__ifp, __new) \
1328 	PSLIST_WRITER_INSERT_AFTER((__ifp), (__new), if_pslist_entry)
1329 #define IFNET_WRITER_EMPTY() \
1330 	(PSLIST_WRITER_FIRST(&ifnet_pslist, struct ifnet, if_pslist_entry) == NULL)
1331 #define IFNET_WRITER_INSERT_TAIL(__new)					\
1332 	do {								\
1333 		if (IFNET_WRITER_EMPTY()) {				\
1334 			IFNET_WRITER_INSERT_HEAD(__new);		\
1335 		} else {						\
1336 			struct ifnet *__ifp;				\
1337 			IFNET_WRITER_FOREACH(__ifp) {			\
1338 				if (IFNET_WRITER_NEXT(__ifp) == NULL) {	\
1339 					IFNET_WRITER_INSERT_AFTER(__ifp,\
1340 					    (__new));			\
1341 					break;				\
1342 				}					\
1343 			}						\
1344 		}							\
1345 	} while (0)
1346 
1347 #define IFNET_LOCK(ifp)		mutex_enter((ifp)->if_ioctl_lock)
1348 #define IFNET_UNLOCK(ifp)	mutex_exit((ifp)->if_ioctl_lock)
1349 #define IFNET_LOCKED(ifp)	mutex_owned((ifp)->if_ioctl_lock)
1350 
1351 #define IFNET_ASSERT_UNLOCKED(ifp)	\
1352 	KDASSERT(mutex_ownable((ifp)->if_ioctl_lock))
1353 
1354 extern struct pslist_head ifnet_pslist;
1355 extern kmutex_t ifnet_mtx;
1356 
1357 extern struct ifnet *lo0ifp;
1358 
1359 /*
1360  * ifq sysctl support
1361  */
1362 int	sysctl_ifq(int *name, u_int namelen, void *oldp,
1363 		       size_t *oldlenp, void *newp, size_t newlen,
1364 		       struct ifqueue *ifq);
1365 /* symbolic names for terminal (per-protocol) CTL_IFQ_ nodes */
1366 #define IFQCTL_LEN	1
1367 #define IFQCTL_MAXLEN	2
1368 #define IFQCTL_PEAK	3
1369 #define IFQCTL_DROPS	4
1370 
1371 /*
1372  * Hook for if_vlan - needed by if_agr
1373  */
1374 MODULE_HOOK(if_vlan_vlan_input_hook, void, (struct ifnet *, struct mbuf *));
1375 
1376 #endif /* _KERNEL */
1377 
1378 #endif /* !_NET_IF_H_ */
1379