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