Home | History | Annotate | Line # | Download | only in net
if.h revision 1.152
      1 /*	$NetBSD: if.h,v 1.152 2011/10/19 01:34:37 dyoung 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/mutex.h>
     81 #include <sys/condvar.h>
     82 #include <sys/percpu.h>
     83 #include <sys/socket.h>
     84 #include <sys/queue.h>
     85 #include <net/dlt.h>
     86 #include <net/pfil.h>
     87 
     88 /*
     89  * Always include ALTQ glue here -- we use the ALTQ interface queue
     90  * structure even when ALTQ is not configured into the kernel so that
     91  * the size of struct ifnet does not changed based on the option.  The
     92  * ALTQ queue structure is API-compatible with the legacy ifqueue.
     93  */
     94 #include <altq/if_altq.h>
     95 
     96 /*
     97  * Structures defining a network interface, providing a packet
     98  * transport mechanism (ala level 0 of the PUP protocols).
     99  *
    100  * Each interface accepts output datagrams of a specified maximum
    101  * length, and provides higher level routines with input datagrams
    102  * received from its medium.
    103  *
    104  * Output occurs when the routine if_output is called, with four parameters:
    105  *	(*ifp->if_output)(ifp, m, dst, rt)
    106  * Here m is the mbuf chain to be sent and dst is the destination address.
    107  * The output routine encapsulates the supplied datagram if necessary,
    108  * and then transmits it on its medium.
    109  *
    110  * On input, each interface unwraps the data received by it, and either
    111  * places it on the input queue of a internetwork datagram routine
    112  * and posts the associated software interrupt, or passes the datagram to a raw
    113  * packet input routine.
    114  *
    115  * Routines exist for locating interfaces by their addresses
    116  * or for locating a interface on a certain network, as well as more general
    117  * routing and gateway routines maintaining information used to locate
    118  * interfaces.  These routines live in the files if.c and route.c
    119  */
    120 #include <sys/time.h>
    121 
    122 #if defined(_KERNEL_OPT)
    123 #include "opt_compat_netbsd.h"
    124 #endif
    125 
    126 struct mbuf;
    127 struct proc;
    128 struct rtentry;
    129 struct socket;
    130 struct ether_header;
    131 struct ifaddr;
    132 struct ifnet;
    133 struct rt_addrinfo;
    134 
    135 #define	IFNAMSIZ	IF_NAMESIZE
    136 
    137 /*
    138  * Structure describing a `cloning' interface.
    139  */
    140 struct if_clone {
    141 	LIST_ENTRY(if_clone) ifc_list;	/* on list of cloners */
    142 	const char *ifc_name;		/* name of device, e.g. `gif' */
    143 	size_t ifc_namelen;		/* length of name */
    144 
    145 	int	(*ifc_create)(struct if_clone *, int);
    146 	int	(*ifc_destroy)(struct ifnet *);
    147 };
    148 
    149 #define	IF_CLONE_INITIALIZER(name, create, destroy)			\
    150 	{ { NULL, NULL }, name, sizeof(name) - 1, create, destroy }
    151 
    152 /*
    153  * Structure used to query names of interface cloners.
    154  */
    155 struct if_clonereq {
    156 	int	ifcr_total;		/* total cloners (out) */
    157 	int	ifcr_count;		/* room for this many in user buffer */
    158 	char	*ifcr_buffer;		/* buffer for cloner names */
    159 };
    160 
    161 /*
    162  * Structure defining statistics and other data kept regarding a network
    163  * interface.
    164  */
    165 struct if_data {
    166 	/* generic interface information */
    167 	u_char	ifi_type;		/* ethernet, tokenring, etc. */
    168 	u_char	ifi_addrlen;		/* media address length */
    169 	u_char	ifi_hdrlen;		/* media header length */
    170 	int	ifi_link_state;		/* current link state */
    171 	uint64_t ifi_mtu;		/* maximum transmission unit */
    172 	uint64_t ifi_metric;		/* routing metric (external only) */
    173 	uint64_t ifi_baudrate;		/* linespeed */
    174 	/* volatile statistics */
    175 	uint64_t ifi_ipackets;		/* packets received on interface */
    176 	uint64_t ifi_ierrors;		/* input errors on interface */
    177 	uint64_t ifi_opackets;		/* packets sent on interface */
    178 	uint64_t ifi_oerrors;		/* output errors on interface */
    179 	uint64_t ifi_collisions;	/* collisions on csma interfaces */
    180 	uint64_t ifi_ibytes;		/* total number of octets received */
    181 	uint64_t ifi_obytes;		/* total number of octets sent */
    182 	uint64_t ifi_imcasts;		/* packets received via multicast */
    183 	uint64_t ifi_omcasts;		/* packets sent via multicast */
    184 	uint64_t ifi_iqdrops;		/* dropped on input, this interface */
    185 	uint64_t ifi_noproto;		/* destined for unsupported protocol */
    186 	struct	timespec ifi_lastchange;/* last operational state change */
    187 };
    188 
    189 /*
    190  * Values for if_link_state.
    191  */
    192 #define	LINK_STATE_UNKNOWN	0	/* link invalid/unknown */
    193 #define	LINK_STATE_DOWN		1	/* link is down */
    194 #define	LINK_STATE_UP		2	/* link is up */
    195 
    196 /*
    197  * Structure defining a queue for a network interface.
    198  */
    199 struct ifqueue {
    200 	struct	mbuf *ifq_head;
    201 	struct	mbuf *ifq_tail;
    202 	int	ifq_len;
    203 	int	ifq_maxlen;
    204 	int	ifq_drops;
    205 };
    206 
    207 /*
    208  * Structure defining a queue for a network interface.
    209  *
    210  * (Would like to call this struct ``if'', but C isn't PL/1.)
    211  */
    212 TAILQ_HEAD(ifnet_head, ifnet);		/* the actual queue head */
    213 
    214 typedef struct ifnet {
    215 	void	*if_softc;		/* lower-level data for this if */
    216 	TAILQ_ENTRY(ifnet) if_list;	/* all struct ifnets are chained */
    217 	TAILQ_HEAD(, ifaddr) if_addrlist; /* linked list of addresses per if */
    218 	char	if_xname[IFNAMSIZ];	/* external name (name + unit) */
    219 	int	if_pcount;		/* number of promiscuous listeners */
    220 	struct bpf_if *if_bpf;		/* packet filter structure */
    221 	u_short	if_index;		/* numeric abbreviation for this if */
    222 	short	if_timer;		/* time 'til if_watchdog called */
    223 	short	if_flags;		/* up/down, broadcast, etc. */
    224 	short	if__pad1;		/* be nice to m68k ports */
    225 	struct	if_data if_data;	/* statistics and other data about if */
    226 	/*
    227 	 * Procedure handles.  If you add more of these, don't forget the
    228 	 * corresponding NULL stub in if.c.
    229 	 */
    230 	int	(*if_output)		/* output routine (enqueue) */
    231 		    (struct ifnet *, struct mbuf *, const struct sockaddr *,
    232 		     struct rtentry *);
    233 	void	(*if_input)		/* input routine (from h/w driver) */
    234 		    (struct ifnet *, struct mbuf *);
    235 	void	(*if_start)		/* initiate output routine */
    236 		    (struct ifnet *);
    237 	int	(*if_ioctl)		/* ioctl routine */
    238 		    (struct ifnet *, u_long, void *);
    239 	int	(*if_init)		/* init routine */
    240 		    (struct ifnet *);
    241 	void	(*if_stop)		/* stop routine */
    242 		    (struct ifnet *, int);
    243 	void	(*if_watchdog)		/* timer routine */
    244 		    (struct ifnet *);
    245 	void	(*if_drain)		/* routine to release resources */
    246 		    (struct ifnet *);
    247 	struct ifaltq if_snd;		/* output queue (includes altq) */
    248 	struct ifaddr	*if_dl;		/* identity of this interface. */
    249 	const struct	sockaddr_dl *if_sadl;	/* pointer to sockaddr_dl
    250 						 * of if_dl
    251 						 */
    252 	/* if_hwdl: h/w identity
    253 	 *
    254 	 * May be NULL.  If not NULL, it is the address assigned
    255 	 * to the interface by the manufacturer, so it very likely
    256 	 * to be unique.  It MUST NOT be deleted.  It is highly
    257 	 * suitable for deriving the EUI64 for the interface.
    258 	 */
    259 	struct ifaddr	*if_hwdl;
    260 	const uint8_t *if_broadcastaddr;/* linklevel broadcast bytestring */
    261 	void	*if_bridge;		/* bridge glue */
    262 	int	if_dlt;			/* data link type (<net/dlt.h>) */
    263 	struct pfil_head if_pfil;	/* filtering point */
    264 	uint64_t if_capabilities;	/* interface capabilities */
    265 	uint64_t if_capenable;		/* capabilities enabled */
    266 	union {
    267 		void *		carp_s;	/* carp structure (used by !carp ifs) */
    268 		struct ifnet	*carp_d;/* ptr to carpdev (used by carp ifs) */
    269 	} if_carp_ptr;
    270 #define if_carp		if_carp_ptr.carp_s
    271 #define if_carpdev	if_carp_ptr.carp_d
    272 	/*
    273 	 * These are pre-computed based on an interfaces enabled
    274 	 * capabilities, for speed elsewhere.
    275 	 */
    276 	int	if_csum_flags_tx;	/* M_CSUM_* flags for Tx */
    277 	int	if_csum_flags_rx;	/* M_CSUM_* flags for Rx */
    278 
    279 	void	*if_afdata[AF_MAX];
    280 	struct	mowner *if_mowner;	/* who owns mbufs for this interface */
    281 
    282 	void	*if_agrprivate;		/* used only when #if NAGR > 0 */
    283 
    284 	/*
    285 	 * pf specific data, used only when #if NPF > 0.
    286 	 */
    287 	void	*if_pf_kif;		/* pf interface abstraction */
    288 	void	*if_pf_groups;		/* pf interface groups */
    289 	/*
    290 	 * During an ifnet's lifetime, it has only one if_index, but
    291 	 * and if_index is not sufficient to identify an ifnet
    292 	 * because during the lifetime of the system, many ifnets may occupy a
    293 	 * given if_index.  Let us tell different ifnets at the same
    294 	 * if_index apart by their if_index_gen, a unique number that each ifnet
    295 	 * is assigned when it if_attach()s.  Now, the kernel can use the
    296 	 * pair (if_index, if_index_gen) as a weak reference to an ifnet.
    297 	 */
    298 	uint64_t if_index_gen;		/* generation number for the ifnet
    299 					 * at if_index: if two ifnets' index
    300 					 * and generation number are both the
    301 					 * same, they are the same ifnet.
    302 					 */
    303 	struct sysctllog	*if_sysctl_log;
    304 	int (*if_initaddr)(struct ifnet *, struct ifaddr *, bool);
    305 	int (*if_mcastop)(struct ifnet *, const unsigned long,
    306 	    const struct sockaddr *);
    307 	int (*if_setflags)(struct ifnet *, const short);
    308 	kmutex_t if_ioctl_lock;
    309 	uint64_t if_ioctl_nexit;
    310 	percpu_t *if_ioctl_nenter;
    311 	kcondvar_t if_ioctl_emptied;
    312 } ifnet_t;
    313 
    314 #define	if_mtu		if_data.ifi_mtu
    315 #define	if_type		if_data.ifi_type
    316 #define	if_addrlen	if_data.ifi_addrlen
    317 #define	if_hdrlen	if_data.ifi_hdrlen
    318 #define	if_metric	if_data.ifi_metric
    319 #define	if_link_state	if_data.ifi_link_state
    320 #define	if_baudrate	if_data.ifi_baudrate
    321 #define	if_ipackets	if_data.ifi_ipackets
    322 #define	if_ierrors	if_data.ifi_ierrors
    323 #define	if_opackets	if_data.ifi_opackets
    324 #define	if_oerrors	if_data.ifi_oerrors
    325 #define	if_collisions	if_data.ifi_collisions
    326 #define	if_ibytes	if_data.ifi_ibytes
    327 #define	if_obytes	if_data.ifi_obytes
    328 #define	if_imcasts	if_data.ifi_imcasts
    329 #define	if_omcasts	if_data.ifi_omcasts
    330 #define	if_iqdrops	if_data.ifi_iqdrops
    331 #define	if_noproto	if_data.ifi_noproto
    332 #define	if_lastchange	if_data.ifi_lastchange
    333 
    334 #define	IFF_UP		0x0001		/* interface is up */
    335 #define	IFF_BROADCAST	0x0002		/* broadcast address valid */
    336 #define	IFF_DEBUG	0x0004		/* turn on debugging */
    337 #define	IFF_LOOPBACK	0x0008		/* is a loopback net */
    338 #define	IFF_POINTOPOINT	0x0010		/* interface is point-to-point link */
    339 #define	IFF_NOTRAILERS	0x0020		/* avoid use of trailers */
    340 #define	IFF_RUNNING	0x0040		/* resources allocated */
    341 #define	IFF_NOARP	0x0080		/* no address resolution protocol */
    342 #define	IFF_PROMISC	0x0100		/* receive all packets */
    343 #define	IFF_ALLMULTI	0x0200		/* receive all multicast packets */
    344 #define	IFF_OACTIVE	0x0400		/* transmission in progress */
    345 #define	IFF_SIMPLEX	0x0800		/* can't hear own transmissions */
    346 #define	IFF_LINK0	0x1000		/* per link layer defined bit */
    347 #define	IFF_LINK1	0x2000		/* per link layer defined bit */
    348 #define	IFF_LINK2	0x4000		/* per link layer defined bit */
    349 #define	IFF_MULTICAST	0x8000		/* supports multicast */
    350 
    351 #define	IFFBITS \
    352     "\020\1UP\2BROADCAST\3DEBUG\4LOOPBACK\5POINTOPOINT\6NOTRAILERS" \
    353     "\7RUNNING\10NOARP\11PROMISC\12ALLMULTI\13OACTIVE\14SIMPLEX" \
    354     "\15LINK0\16LINK1\17LINK2\20MULTICAST"
    355 
    356 /* flags set internally only: */
    357 #define	IFF_CANTCHANGE \
    358 	(IFF_BROADCAST|IFF_POINTOPOINT|IFF_RUNNING|IFF_OACTIVE|\
    359 	    IFF_SIMPLEX|IFF_MULTICAST|IFF_ALLMULTI|IFF_PROMISC)
    360 
    361 /*
    362  * Some convenience macros used for setting ifi_baudrate.
    363  * XXX 1000 vs. 1024? --thorpej (at) NetBSD.org
    364  */
    365 #define	IF_Kbps(x)	((x) * 1000)		/* kilobits/sec. */
    366 #define	IF_Mbps(x)	(IF_Kbps((x) * 1000))	/* megabits/sec. */
    367 #define	IF_Gbps(x)	(IF_Mbps((x) * 1000))	/* gigabits/sec. */
    368 
    369 /* Capabilities that interfaces can advertise. */
    370 #define	IFCAP_TSOv4		0x00080	/* can do TCPv4 segmentation offload */
    371 #define	IFCAP_CSUM_IPv4_Rx	0x00100	/* can do IPv4 header checksums (Rx) */
    372 #define	IFCAP_CSUM_IPv4_Tx	0x00200	/* can do IPv4 header checksums (Tx) */
    373 #define	IFCAP_CSUM_TCPv4_Rx	0x00400	/* can do IPv4/TCP checksums (Rx) */
    374 #define	IFCAP_CSUM_TCPv4_Tx	0x00800	/* can do IPv4/TCP checksums (Tx) */
    375 #define	IFCAP_CSUM_UDPv4_Rx	0x01000	/* can do IPv4/UDP checksums (Rx) */
    376 #define	IFCAP_CSUM_UDPv4_Tx	0x02000	/* can do IPv4/UDP checksums (Tx) */
    377 #define	IFCAP_CSUM_TCPv6_Rx	0x04000	/* can do IPv6/TCP checksums (Rx) */
    378 #define	IFCAP_CSUM_TCPv6_Tx	0x08000	/* can do IPv6/TCP checksums (Tx) */
    379 #define	IFCAP_CSUM_UDPv6_Rx	0x10000	/* can do IPv6/UDP checksums (Rx) */
    380 #define	IFCAP_CSUM_UDPv6_Tx	0x20000	/* can do IPv6/UDP checksums (Tx) */
    381 #define	IFCAP_TSOv6		0x40000	/* can do TCPv6 segmentation offload */
    382 
    383 #define	IFCAPBITS		\
    384 	"\020"			\
    385 	"\10TSO4"		\
    386 	"\11IP4CSUM_Rx"		\
    387 	"\12IP4CSUM_Tx"		\
    388 	"\13TCP4CSUM_Rx"	\
    389 	"\14TCP4CSUM_Tx"	\
    390 	"\15UDP4CSUM_Rx"	\
    391 	"\16UDP4CSUM_Tx"	\
    392 	"\17TCP6CSUM_Rx"	\
    393 	"\20TCP6CSUM_Tx"	\
    394 	"\21UDP6CSUM_Rx"	\
    395 	"\22UDP6CSUM_Tx"	\
    396 	"\23TSO6"
    397 
    398 /*
    399  * Output queues (ifp->if_snd) and internetwork datagram level (pup level 1)
    400  * input routines have queues of messages stored on ifqueue structures
    401  * (defined above).  Entries are added to and deleted from these structures
    402  * by these macros, which should be called with ipl raised to splnet().
    403  */
    404 #define	IF_QFULL(ifq)		((ifq)->ifq_len >= (ifq)->ifq_maxlen)
    405 #define	IF_DROP(ifq)		((ifq)->ifq_drops++)
    406 #define	IF_ENQUEUE(ifq, m) do { \
    407 	(m)->m_nextpkt = 0; \
    408 	if ((ifq)->ifq_tail == 0) \
    409 		(ifq)->ifq_head = m; \
    410 	else \
    411 		(ifq)->ifq_tail->m_nextpkt = m; \
    412 	(ifq)->ifq_tail = m; \
    413 	(ifq)->ifq_len++; \
    414 } while (/*CONSTCOND*/0)
    415 #define	IF_PREPEND(ifq, m) do { \
    416 	(m)->m_nextpkt = (ifq)->ifq_head; \
    417 	if ((ifq)->ifq_tail == 0) \
    418 		(ifq)->ifq_tail = (m); \
    419 	(ifq)->ifq_head = (m); \
    420 	(ifq)->ifq_len++; \
    421 } while (/*CONSTCOND*/0)
    422 #define	IF_DEQUEUE(ifq, m) do { \
    423 	(m) = (ifq)->ifq_head; \
    424 	if (m) { \
    425 		if (((ifq)->ifq_head = (m)->m_nextpkt) == 0) \
    426 			(ifq)->ifq_tail = 0; \
    427 		(m)->m_nextpkt = 0; \
    428 		(ifq)->ifq_len--; \
    429 	} \
    430 } while (/*CONSTCOND*/0)
    431 #define	IF_POLL(ifq, m)		((m) = (ifq)->ifq_head)
    432 #define	IF_PURGE(ifq)							\
    433 do {									\
    434 	struct mbuf *__m0;						\
    435 									\
    436 	for (;;) {							\
    437 		IF_DEQUEUE((ifq), __m0);				\
    438 		if (__m0 == NULL)					\
    439 			break;						\
    440 		else							\
    441 			m_freem(__m0);					\
    442 	}								\
    443 } while (/*CONSTCOND*/ 0)
    444 #define	IF_IS_EMPTY(ifq)	((ifq)->ifq_len == 0)
    445 
    446 #ifndef IFQ_MAXLEN
    447 #define	IFQ_MAXLEN	256
    448 #endif
    449 #define	IFNET_SLOWHZ	1		/* granularity is 1 second */
    450 
    451 /*
    452  * Structure defining statistics and other data kept regarding an address
    453  * on a network interface.
    454  */
    455 struct ifaddr_data {
    456 	int64_t	ifad_inbytes;
    457 	int64_t	ifad_outbytes;
    458 };
    459 
    460 /*
    461  * The ifaddr structure contains information about one address
    462  * of an interface.  They are maintained by the different address families,
    463  * are allocated and attached when an address is set, and are linked
    464  * together so all addresses for an interface can be located.
    465  */
    466 struct ifaddr {
    467 	struct	sockaddr *ifa_addr;	/* address of interface */
    468 	struct	sockaddr *ifa_dstaddr;	/* other end of p-to-p link */
    469 #define	ifa_broadaddr	ifa_dstaddr	/* broadcast address interface */
    470 	struct	sockaddr *ifa_netmask;	/* used to determine subnet */
    471 	struct	ifnet *ifa_ifp;		/* back-pointer to interface */
    472 	TAILQ_ENTRY(ifaddr) ifa_list;	/* list of addresses for interface */
    473 	struct	ifaddr_data	ifa_data;	/* statistics on the address */
    474 	void	(*ifa_rtrequest)	/* check or clean routes (+ or -)'d */
    475 		        (int, struct rtentry *, const struct rt_addrinfo *);
    476 	u_int	ifa_flags;		/* mostly rt_flags for cloning */
    477 	int	ifa_refcnt;		/* count of references */
    478 	int	ifa_metric;		/* cost of going out this interface */
    479 	struct ifaddr	*(*ifa_getifa)(struct ifaddr *,
    480 			               const struct sockaddr *);
    481 	uint32_t	*ifa_seqno;
    482 	int16_t	ifa_preference;	/* preference level for this address */
    483 };
    484 #define	IFA_ROUTE	RTF_UP	/* (0x01) route installed */
    485 
    486 /*
    487  * Message format for use in obtaining information about interfaces from
    488  * sysctl and the routing socket.  We need to force 64-bit alignment if we
    489  * aren't using compatiblity definitons.
    490  */
    491 #if !defined(_KERNEL) || !defined(COMPAT_RTSOCK)
    492 #define	__align64	__aligned(sizeof(uint64_t))
    493 #else
    494 #define	__align64
    495 #endif
    496 struct if_msghdr {
    497 	u_short	ifm_msglen __align64;
    498 				/* to skip over non-understood messages */
    499 	u_char	ifm_version;	/* future binary compatibility */
    500 	u_char	ifm_type;	/* message type */
    501 	int	ifm_addrs;	/* like rtm_addrs */
    502 	int	ifm_flags;	/* value of if_flags */
    503 	u_short	ifm_index;	/* index for associated ifp */
    504 	struct	if_data ifm_data __align64;
    505 				/* statistics and other data about if */
    506 };
    507 
    508 /*
    509  * Message format for use in obtaining information about interface addresses
    510  * from sysctl and the routing socket.
    511  */
    512 struct ifa_msghdr {
    513 	u_short	ifam_msglen __align64;
    514 				/* to skip over non-understood messages */
    515 	u_char	ifam_version;	/* future binary compatibility */
    516 	u_char	ifam_type;	/* message type */
    517 	int	ifam_addrs;	/* like rtm_addrs */
    518 	int	ifam_flags;	/* value of ifa_flags */
    519 	int	ifam_metric;	/* value of ifa_metric */
    520 	u_short	ifam_index;	/* index for associated ifp */
    521 };
    522 
    523 /*
    524  * Message format announcing the arrival or departure of a network interface.
    525  */
    526 struct if_announcemsghdr {
    527 	u_short	ifan_msglen __align64;
    528 				/* to skip over non-understood messages */
    529 	u_char	ifan_version;	/* future binary compatibility */
    530 	u_char	ifan_type;	/* message type */
    531 	u_short	ifan_index;	/* index for associated ifp */
    532 	char	ifan_name[IFNAMSIZ]; /* if name, e.g. "en0" */
    533 	u_short	ifan_what;	/* what type of announcement */
    534 };
    535 
    536 #define	IFAN_ARRIVAL	0	/* interface arrival */
    537 #define	IFAN_DEPARTURE	1	/* interface departure */
    538 
    539 #undef __align64
    540 
    541 /*
    542  * Interface request structure used for socket
    543  * ioctl's.  All interface ioctl's must have parameter
    544  * definitions which begin with ifr_name.  The
    545  * remainder may be interface specific.
    546  */
    547 struct	ifreq {
    548 	char	ifr_name[IFNAMSIZ];		/* if name, e.g. "en0" */
    549 	union {
    550 		struct	sockaddr ifru_addr;
    551 		struct	sockaddr ifru_dstaddr;
    552 		struct	sockaddr ifru_broadaddr;
    553 		struct	sockaddr_storage ifru_space;
    554 		short	ifru_flags;
    555 		int	ifru_metric;
    556 		int	ifru_mtu;
    557 		int	ifru_dlt;
    558 		u_int	ifru_value;
    559 		void *	ifru_data;
    560 		struct {
    561 			uint32_t	b_buflen;
    562 			void		*b_buf;
    563 		} ifru_b;
    564 	} ifr_ifru;
    565 #define	ifr_addr	ifr_ifru.ifru_addr	/* address */
    566 #define	ifr_dstaddr	ifr_ifru.ifru_dstaddr	/* other end of p-to-p link */
    567 #define	ifr_broadaddr	ifr_ifru.ifru_broadaddr	/* broadcast address */
    568 #define	ifr_space	ifr_ifru.ifru_space	/* sockaddr_storage */
    569 #define	ifr_flags	ifr_ifru.ifru_flags	/* flags */
    570 #define	ifr_metric	ifr_ifru.ifru_metric	/* metric */
    571 #define	ifr_mtu		ifr_ifru.ifru_mtu	/* mtu */
    572 #define	ifr_dlt		ifr_ifru.ifru_dlt	/* data link type (DLT_*) */
    573 #define	ifr_value	ifr_ifru.ifru_value	/* generic value */
    574 #define	ifr_media	ifr_ifru.ifru_metric	/* media options (overload) */
    575 #define	ifr_data	ifr_ifru.ifru_data	/* for use by interface
    576 						 * XXX deprecated
    577 						 */
    578 #define	ifr_buf		ifr_ifru.ifru_b.b_buf	/* new interface ioctls */
    579 #define	ifr_buflen	ifr_ifru.ifru_b.b_buflen
    580 };
    581 
    582 #ifdef _KERNEL
    583 #define	ifreq_setdstaddr	ifreq_setaddr
    584 #define	ifreq_setbroadaddr	ifreq_setaddr
    585 #define	ifreq_getdstaddr	ifreq_getaddr
    586 #define	ifreq_getbroadaddr	ifreq_getaddr
    587 
    588 static inline const struct sockaddr *
    589 ifreq_getaddr(u_long cmd, const struct ifreq *ifr)
    590 {
    591 	return &ifr->ifr_addr;
    592 }
    593 #endif /* _KERNEL */
    594 
    595 struct ifcapreq {
    596 	char		ifcr_name[IFNAMSIZ];	/* if name, e.g. "en0" */
    597 	uint64_t	ifcr_capabilities;	/* supported capabiliites */
    598 	uint64_t	ifcr_capenable;		/* capabilities enabled */
    599 };
    600 
    601 struct ifaliasreq {
    602 	char	ifra_name[IFNAMSIZ];		/* if name, e.g. "en0" */
    603 	struct	sockaddr ifra_addr;
    604 	struct	sockaddr ifra_dstaddr;
    605 #define	ifra_broadaddr	ifra_dstaddr
    606 	struct	sockaddr ifra_mask;
    607 };
    608 
    609 struct ifdatareq {
    610 	char	ifdr_name[IFNAMSIZ];		/* if name, e.g. "en0" */
    611 	struct	if_data ifdr_data;
    612 };
    613 
    614 struct ifmediareq {
    615 	char	ifm_name[IFNAMSIZ];		/* if name, e.g. "en0" */
    616 	int	ifm_current;			/* current media options */
    617 	int	ifm_mask;			/* don't care mask */
    618 	int	ifm_status;			/* media status */
    619 	int	ifm_active;			/* active options */
    620 	int	ifm_count;			/* # entries in ifm_ulist
    621 						   array */
    622 	int	*ifm_ulist;			/* media words */
    623 };
    624 
    625 
    626 struct  ifdrv {
    627 	char		ifd_name[IFNAMSIZ];	/* if name, e.g. "en0" */
    628 	unsigned long	ifd_cmd;
    629 	size_t		ifd_len;
    630 	void		*ifd_data;
    631 };
    632 #define IFLINKSTR_QUERYLEN	0x01
    633 #define IFLINKSTR_UNSET		0x02
    634 
    635 /*
    636  * Structure used in SIOCGIFCONF request.
    637  * Used to retrieve interface configuration
    638  * for machine (useful for programs which
    639  * must know all networks accessible).
    640  */
    641 struct	ifconf {
    642 	int	ifc_len;		/* size of associated buffer */
    643 	union {
    644 		void *	ifcu_buf;
    645 		struct	ifreq *ifcu_req;
    646 	} ifc_ifcu;
    647 #define	ifc_buf	ifc_ifcu.ifcu_buf	/* buffer address */
    648 #define	ifc_req	ifc_ifcu.ifcu_req	/* array of structures returned */
    649 };
    650 
    651 /*
    652  * Structure for SIOC[AGD]LIFADDR
    653  */
    654 struct if_laddrreq {
    655 	char iflr_name[IFNAMSIZ];
    656 	unsigned int flags;
    657 #define IFLR_PREFIX	0x8000	/* in: prefix given  out: kernel fills id */
    658 #define IFLR_ACTIVE	0x4000	/* in/out: link-layer address activation */
    659 #define IFLR_FACTORY	0x2000	/* in/out: factory link-layer address */
    660 	unsigned int prefixlen;		/* in/out */
    661 	struct sockaddr_storage addr;	/* in/out */
    662 	struct sockaddr_storage dstaddr; /* out */
    663 };
    664 
    665 /*
    666  * Structure for SIOC[SG]IFADDRPREF
    667  */
    668 struct if_addrprefreq {
    669 	char			ifap_name[IFNAMSIZ];
    670 	int16_t			ifap_preference;	/* in/out */
    671 	struct sockaddr_storage	ifap_addr;		/* in/out */
    672 };
    673 
    674 #include <net/if_arp.h>
    675 
    676 #endif /* _NETBSD_SOURCE */
    677 
    678 #ifdef _KERNEL
    679 #ifdef IFAREF_DEBUG
    680 #define	IFAREF(ifa)							\
    681 do {									\
    682 	printf("IFAREF: %s:%d %p -> %d\n", __FILE__, __LINE__,		\
    683 	    (ifa), ++(ifa)->ifa_refcnt);				\
    684 } while (/*CONSTCOND*/ 0)
    685 
    686 #define	IFAFREE(ifa)							\
    687 do {									\
    688 	if ((ifa)->ifa_refcnt <= 0)					\
    689 		panic("%s:%d: %p ifa_refcnt <= 0", __FILE__,		\
    690 		    __LINE__, (ifa));					\
    691 	printf("IFAFREE: %s:%d %p -> %d\n", __FILE__, __LINE__,		\
    692 	    (ifa), --(ifa)->ifa_refcnt);				\
    693 	if ((ifa)->ifa_refcnt == 0)					\
    694 		ifafree(ifa);						\
    695 } while (/*CONSTCOND*/ 0)
    696 #else
    697 #define	IFAREF(ifa)	(ifa)->ifa_refcnt++
    698 
    699 #ifdef DIAGNOSTIC
    700 #define	IFAFREE(ifa)							\
    701 do {									\
    702 	if ((ifa)->ifa_refcnt <= 0)					\
    703 		panic("%s:%d: %p ifa_refcnt <= 0", __FILE__,		\
    704 		    __LINE__, (ifa));					\
    705 	if (--(ifa)->ifa_refcnt == 0)					\
    706 		ifafree(ifa);						\
    707 } while (/*CONSTCOND*/ 0)
    708 #else
    709 #define	IFAFREE(ifa)							\
    710 do {									\
    711 	if (--(ifa)->ifa_refcnt == 0)					\
    712 		ifafree(ifa);						\
    713 } while (/*CONSTCOND*/ 0)
    714 #endif /* DIAGNOSTIC */
    715 #endif /* IFAREF_DEBUG */
    716 
    717 #ifdef ALTQ
    718 #define	ALTQ_DECL(x)		x
    719 #define ALTQ_COMMA		,
    720 
    721 #define IFQ_ENQUEUE(ifq, m, pattr, err)					\
    722 do {									\
    723 	if (ALTQ_IS_ENABLED((ifq)))					\
    724 		ALTQ_ENQUEUE((ifq), (m), (pattr), (err));		\
    725 	else {								\
    726 		if (IF_QFULL((ifq))) {					\
    727 			m_freem((m));					\
    728 			(err) = ENOBUFS;				\
    729 		} else {						\
    730 			IF_ENQUEUE((ifq), (m));				\
    731 			(err) = 0;					\
    732 		}							\
    733 	}								\
    734 	if ((err))							\
    735 		(ifq)->ifq_drops++;					\
    736 } while (/*CONSTCOND*/ 0)
    737 
    738 #define IFQ_DEQUEUE(ifq, m)						\
    739 do {									\
    740 	if (TBR_IS_ENABLED((ifq)))					\
    741 		(m) = tbr_dequeue((ifq), ALTDQ_REMOVE);			\
    742 	else if (ALTQ_IS_ENABLED((ifq)))				\
    743 		ALTQ_DEQUEUE((ifq), (m));				\
    744 	else								\
    745 		IF_DEQUEUE((ifq), (m));					\
    746 } while (/*CONSTCOND*/ 0)
    747 
    748 #define	IFQ_POLL(ifq, m)						\
    749 do {									\
    750 	if (TBR_IS_ENABLED((ifq)))					\
    751 		(m) = tbr_dequeue((ifq), ALTDQ_POLL);			\
    752 	else if (ALTQ_IS_ENABLED((ifq)))				\
    753 		ALTQ_POLL((ifq), (m));					\
    754 	else								\
    755 		IF_POLL((ifq), (m));					\
    756 } while (/*CONSTCOND*/ 0)
    757 
    758 #define	IFQ_PURGE(ifq)							\
    759 do {									\
    760 	if (ALTQ_IS_ENABLED((ifq)))					\
    761 		ALTQ_PURGE((ifq));					\
    762 	else								\
    763 		IF_PURGE((ifq));					\
    764 } while (/*CONSTCOND*/ 0)
    765 
    766 #define	IFQ_SET_READY(ifq)						\
    767 do {									\
    768 	(ifq)->altq_flags |= ALTQF_READY;				\
    769 } while (/*CONSTCOND*/ 0)
    770 
    771 #define	IFQ_CLASSIFY(ifq, m, af, pattr)					\
    772 do {									\
    773 	if (ALTQ_IS_ENABLED((ifq))) {					\
    774 		if (ALTQ_NEEDS_CLASSIFY((ifq)))				\
    775 			(pattr)->pattr_class = (*(ifq)->altq_classify)	\
    776 				((ifq)->altq_clfier, (m), (af));	\
    777 		(pattr)->pattr_af = (af);				\
    778 		(pattr)->pattr_hdr = mtod((m), void *);		\
    779 	}								\
    780 } while (/*CONSTCOND*/ 0)
    781 #else /* ! ALTQ */
    782 #define	ALTQ_DECL(x)		/* nothing */
    783 #define ALTQ_COMMA
    784 
    785 #define	IFQ_ENQUEUE(ifq, m, pattr, err)					\
    786 do {									\
    787 	if (IF_QFULL((ifq))) {						\
    788 		m_freem((m));						\
    789 		(err) = ENOBUFS;					\
    790 	} else {							\
    791 		IF_ENQUEUE((ifq), (m));					\
    792 		(err) = 0;						\
    793 	}								\
    794 	if ((err))							\
    795 		(ifq)->ifq_drops++;					\
    796 } while (/*CONSTCOND*/ 0)
    797 
    798 #define	IFQ_DEQUEUE(ifq, m)	IF_DEQUEUE((ifq), (m))
    799 
    800 #define	IFQ_POLL(ifq, m)	IF_POLL((ifq), (m))
    801 
    802 #define	IFQ_PURGE(ifq)		IF_PURGE((ifq))
    803 
    804 #define	IFQ_SET_READY(ifq)	/* nothing */
    805 
    806 #define	IFQ_CLASSIFY(ifq, m, af, pattr) /* nothing */
    807 
    808 #endif /* ALTQ */
    809 
    810 #define	IFQ_IS_EMPTY(ifq)		IF_IS_EMPTY((ifq))
    811 #define	IFQ_INC_LEN(ifq)		((ifq)->ifq_len++)
    812 #define	IFQ_DEC_LEN(ifq)		(--(ifq)->ifq_len)
    813 #define	IFQ_INC_DROPS(ifq)		((ifq)->ifq_drops++)
    814 #define	IFQ_SET_MAXLEN(ifq, len)	((ifq)->ifq_maxlen = (len))
    815 
    816 #include <sys/mallocvar.h>
    817 MALLOC_DECLARE(M_IFADDR);
    818 MALLOC_DECLARE(M_IFMADDR);
    819 
    820 #define	IFNET_FIRST()			TAILQ_FIRST(&ifnet)
    821 #define	IFNET_NEXT(__ifp)		TAILQ_NEXT((__ifp), if_list)
    822 #define	IFNET_FOREACH(__ifp)		TAILQ_FOREACH(__ifp, &ifnet, if_list)
    823 #define	IFADDR_FIRST(__ifp)		TAILQ_FIRST(&(__ifp)->if_addrlist)
    824 #define	IFADDR_NEXT(__ifa)		TAILQ_NEXT((__ifa), ifa_list)
    825 #define	IFADDR_FOREACH(__ifa, __ifp)	TAILQ_FOREACH(__ifa, \
    826 					    &(__ifp)->if_addrlist, ifa_list)
    827 #define	IFADDR_EMPTY(__ifp)		TAILQ_EMPTY(&(__ifp)->if_addrlist)
    828 
    829 extern struct ifnet_head ifnet;
    830 extern struct ifnet **ifindex2ifnet;
    831 extern struct ifnet *lo0ifp;
    832 extern size_t if_indexlim;
    833 
    834 void    ether_input(struct ifnet *, struct mbuf *);
    835 
    836 int ifreq_setaddr(u_long, struct ifreq *, const struct sockaddr *);
    837 
    838 struct ifnet *if_alloc(u_char);
    839 void if_free(struct ifnet *);
    840 void if_initname(struct ifnet *, const char *, int);
    841 struct ifaddr *if_dl_create(const struct ifnet *, const struct sockaddr_dl **);
    842 void if_activate_sadl(struct ifnet *, struct ifaddr *,
    843     const struct sockaddr_dl *);
    844 void	if_set_sadl(struct ifnet *, const void *, u_char, bool);
    845 void	if_alloc_sadl(struct ifnet *);
    846 void	if_free_sadl(struct ifnet *);
    847 void	if_attach(struct ifnet *);
    848 void	if_attachdomain(void);
    849 void	if_attachdomain1(struct ifnet *);
    850 void	if_deactivate(struct ifnet *);
    851 void	if_purgeaddrs(struct ifnet *, int, void (*)(struct ifaddr *));
    852 void	if_detach(struct ifnet *);
    853 void	if_down(struct ifnet *);
    854 void	if_link_state_change(struct ifnet *, int);
    855 void	if_slowtimo(void *);
    856 void	if_up(struct ifnet *);
    857 int	ifconf(u_long, void *);
    858 void	ifinit(void);
    859 void	ifinit1(void);
    860 int	ifaddrpref_ioctl(struct socket *, u_long, void *, struct ifnet *,
    861     lwp_t *);
    862 int	ifioctl(struct socket *, u_long, void *, struct lwp *);
    863 int	ifioctl_common(struct ifnet *, u_long, void *);
    864 int	ifpromisc(struct ifnet *, int);
    865 struct	ifnet *ifunit(const char *);
    866 int	if_addr_init(ifnet_t *, struct ifaddr *, bool);
    867 int	if_mcast_op(ifnet_t *, const unsigned long, const struct sockaddr *);
    868 int	if_flags_set(struct ifnet *, const short);
    869 
    870 void ifa_insert(struct ifnet *, struct ifaddr *);
    871 void ifa_remove(struct ifnet *, struct ifaddr *);
    872 
    873 struct	ifaddr *ifa_ifwithaddr(const struct sockaddr *);
    874 struct	ifaddr *ifa_ifwithaf(int);
    875 struct	ifaddr *ifa_ifwithdstaddr(const struct sockaddr *);
    876 struct	ifaddr *ifa_ifwithnet(const struct sockaddr *);
    877 struct	ifaddr *ifa_ifwithladdr(const struct sockaddr *);
    878 struct	ifaddr *ifa_ifwithroute(int, const struct sockaddr *,
    879 					const struct sockaddr *);
    880 struct	ifaddr *ifaof_ifpforaddr(const struct sockaddr *, struct ifnet *);
    881 void	ifafree(struct ifaddr *);
    882 void	link_rtrequest(int, struct rtentry *, const struct rt_addrinfo *);
    883 
    884 void	if_clone_attach(struct if_clone *);
    885 void	if_clone_detach(struct if_clone *);
    886 
    887 int	if_clone_create(const char *);
    888 int	if_clone_destroy(const char *);
    889 
    890 int	ifq_enqueue(struct ifnet *, struct mbuf * ALTQ_COMMA
    891     ALTQ_DECL(struct altq_pktattr *));
    892 int	ifq_enqueue2(struct ifnet *, struct ifqueue *, struct mbuf * ALTQ_COMMA
    893     ALTQ_DECL(struct altq_pktattr *));
    894 
    895 int	loioctl(struct ifnet *, u_long, void *);
    896 void	loopattach(int);
    897 int	looutput(struct ifnet *,
    898 	   struct mbuf *, const struct sockaddr *, struct rtentry *);
    899 void	lortrequest(int, struct rtentry *, const struct rt_addrinfo *);
    900 
    901 /*
    902  * These are exported because they're an easy way to tell if
    903  * an interface is going away without having to burn a flag.
    904  */
    905 int	if_nulloutput(struct ifnet *, struct mbuf *,
    906 	    const struct sockaddr *, struct rtentry *);
    907 void	if_nullinput(struct ifnet *, struct mbuf *);
    908 void	if_nullstart(struct ifnet *);
    909 int	if_nullioctl(struct ifnet *, u_long, void *);
    910 int	if_nullinit(struct ifnet *);
    911 void	if_nullstop(struct ifnet *, int);
    912 void	if_nullwatchdog(struct ifnet *);
    913 void	if_nulldrain(struct ifnet *);
    914 #else
    915 struct if_nameindex {
    916 	unsigned int	if_index;	/* 1, 2, ... */
    917 	char		*if_name;	/* null terminated name: "le0", ... */
    918 };
    919 
    920 #include <sys/cdefs.h>
    921 __BEGIN_DECLS
    922 unsigned int if_nametoindex(const char *);
    923 char *	if_indextoname(unsigned int, char *);
    924 struct	if_nameindex * if_nameindex(void);
    925 void	if_freenameindex(struct if_nameindex *);
    926 __END_DECLS
    927 #endif /* _KERNEL */ /* XXX really ALTQ? */
    928 
    929 #ifdef _KERNEL
    930 
    931 ifnet_t *	if_byindex(u_int);
    932 
    933 /*
    934  * ifq sysctl support
    935  */
    936 int	sysctl_ifq(int *name, u_int namelen, void *oldp,
    937 		       size_t *oldlenp, void *newp, size_t newlen,
    938 		       struct ifqueue *ifq);
    939 /* symbolic names for terminal (per-protocol) CTL_IFQ_ nodes */
    940 #define IFQCTL_LEN 1
    941 #define IFQCTL_MAXLEN 2
    942 #define IFQCTL_PEAK 3
    943 #define IFQCTL_DROPS 4
    944 #define IFQCTL_MAXID 5
    945 
    946 #endif /* _KERNEL */
    947 
    948 #ifdef _NETBSD_SOURCE
    949 /*
    950  * sysctl for ifq (per-protocol packet input queue variant of ifqueue)
    951  */
    952 #define CTL_IFQ_NAMES  { \
    953 	{ 0, 0 }, \
    954 	{ "len", CTLTYPE_INT }, \
    955 	{ "maxlen", CTLTYPE_INT }, \
    956 	{ "peak", CTLTYPE_INT }, \
    957 	{ "drops", CTLTYPE_INT }, \
    958 }
    959 #endif /* _NETBSD_SOURCE */
    960 #endif /* !_NET_IF_H_ */
    961