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