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