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