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