Home | History | Annotate | Line # | Download | only in net
if.h revision 1.119
      1 /*	$NetBSD: if.h,v 1.119 2006/08/30 16:41:08 christos 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/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 /*  XXX fast fix for SNMP, going away soon */
    121 #include <sys/time.h>
    122 
    123 #if defined(_KERNEL_OPT)
    124 #include "opt_compat_netbsd.h"
    125 #endif
    126 
    127 struct mbuf;
    128 struct proc;
    129 struct rtentry;
    130 struct socket;
    131 struct ether_header;
    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 	u_quad_t ifi_mtu;		/* maximum transmission unit */
    172 	u_quad_t ifi_metric;		/* routing metric (external only) */
    173 	u_quad_t ifi_baudrate;		/* linespeed */
    174 	/* volatile statistics */
    175 	u_quad_t ifi_ipackets;		/* packets received on interface */
    176 	u_quad_t ifi_ierrors;		/* input errors on interface */
    177 	u_quad_t ifi_opackets;		/* packets sent on interface */
    178 	u_quad_t ifi_oerrors;		/* output errors on interface */
    179 	u_quad_t ifi_collisions;	/* collisions on csma interfaces */
    180 	u_quad_t ifi_ibytes;		/* total number of octets received */
    181 	u_quad_t ifi_obytes;		/* total number of octets sent */
    182 	u_quad_t ifi_imcasts;		/* packets received via multicast */
    183 	u_quad_t ifi_omcasts;		/* packets sent via multicast */
    184 	u_quad_t ifi_iqdrops;		/* dropped on input, this interface */
    185 	u_quad_t ifi_noproto;		/* destined for unsupported protocol */
    186 	struct	timeval 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 #if defined(_KERNEL) && defined(COMPAT_14)
    197 /* Pre-1.5 if_data struct */
    198 struct if_data14 {
    199 	/* generic interface information */
    200 	u_char	ifi_type;		/* ethernet, tokenring, etc. */
    201 	u_char	ifi_addrlen;		/* media address length */
    202 	u_char	ifi_hdrlen;		/* media header length */
    203 	u_long	ifi_mtu;		/* maximum transmission unit */
    204 	u_long	ifi_metric;		/* routing metric (external only) */
    205 	u_long	ifi_baudrate;		/* linespeed */
    206 	/* volatile statistics */
    207 	u_long	ifi_ipackets;		/* packets received on interface */
    208 	u_long	ifi_ierrors;		/* input errors on interface */
    209 	u_long	ifi_opackets;		/* packets sent on interface */
    210 	u_long	ifi_oerrors;		/* output errors on interface */
    211 	u_long	ifi_collisions;		/* collisions on csma interfaces */
    212 	u_long	ifi_ibytes;		/* total number of octets received */
    213 	u_long	ifi_obytes;		/* total number of octets sent */
    214 	u_long	ifi_imcasts;		/* packets received via multicast */
    215 	u_long	ifi_omcasts;		/* packets sent via multicast */
    216 	u_long	ifi_iqdrops;		/* dropped on input, this interface */
    217 	u_long	ifi_noproto;		/* destined for unsupported protocol */
    218 	struct	timeval ifi_lastchange;	/* last operational state change */
    219 };
    220 #endif /* _KERNEL && COMPAT_14 */
    221 
    222 /*
    223  * Structure defining a queue for a network interface.
    224  */
    225 struct ifqueue {
    226 	struct	mbuf *ifq_head;
    227 	struct	mbuf *ifq_tail;
    228 	int	ifq_len;
    229 	int	ifq_maxlen;
    230 	int	ifq_drops;
    231 };
    232 
    233 /*
    234  * Structure defining a queue for a network interface.
    235  *
    236  * (Would like to call this struct ``if'', but C isn't PL/1.)
    237  */
    238 TAILQ_HEAD(ifnet_head, ifnet);		/* the actual queue head */
    239 
    240 struct ifnet {				/* and the entries */
    241 	void	*if_softc;		/* lower-level data for this if */
    242 	TAILQ_ENTRY(ifnet) if_list;	/* all struct ifnets are chained */
    243 	TAILQ_HEAD(, ifaddr) if_addrlist; /* linked list of addresses per if */
    244 	char	if_xname[IFNAMSIZ];	/* external name (name + unit) */
    245 	int	if_pcount;		/* number of promiscuous listeners */
    246 	caddr_t	if_bpf;			/* packet filter structure */
    247 	u_short	if_index;		/* numeric abbreviation for this if */
    248 	short	if_timer;		/* time 'til if_watchdog called */
    249 	short	if_flags;		/* up/down, broadcast, etc. */
    250 	short	if__pad1;		/* be nice to m68k ports */
    251 	struct	if_data if_data;	/* statistics and other data about if */
    252 	/*
    253 	 * Procedure handles.  If you add more of these, don't forget the
    254 	 * corresponding NULL stub in if.c.
    255 	 */
    256 	int	(*if_output)		/* output routine (enqueue) */
    257 		    (struct ifnet *, struct mbuf *, struct sockaddr *,
    258 		     struct rtentry *);
    259 	void	(*if_input)		/* input routine (from h/w driver) */
    260 		    (struct ifnet *, struct mbuf *);
    261 	void	(*if_start)		/* initiate output routine */
    262 		    (struct ifnet *);
    263 	int	(*if_ioctl)		/* ioctl routine */
    264 		    (struct ifnet *, u_long, caddr_t);
    265 	int	(*if_init)		/* init routine */
    266 		    (struct ifnet *);
    267 	void	(*if_stop)		/* stop routine */
    268 		    (struct ifnet *, int);
    269 	void	(*if_watchdog)		/* timer routine */
    270 		    (struct ifnet *);
    271 	void	(*if_drain)		/* routine to release resources */
    272 		    (struct ifnet *);
    273 	struct ifaltq if_snd;		/* output queue (includes altq) */
    274 	struct	sockaddr_dl *if_sadl;	/* pointer to our sockaddr_dl */
    275 	const uint8_t *if_broadcastaddr;/* linklevel broadcast bytestring */
    276 	void	*if_bridge;		/* bridge glue */
    277 	int	if_dlt;			/* data link type (<net/dlt.h>) */
    278 	struct pfil_head if_pfil;	/* filtering point */
    279 	uint64_t if_capabilities;	/* interface capabilities */
    280 	uint64_t if_capenable;		/* capabilities enabled */
    281 	union {
    282 		caddr_t		carp_s;	/* carp structure (used by !carp ifs) */
    283 		struct ifnet	*carp_d;/* ptr to carpdev (used by carp ifs) */
    284 	} if_carp_ptr;
    285 #define if_carp		if_carp_ptr.carp_s
    286 #define if_carpdev	if_carp_ptr.carp_d
    287 	/*
    288 	 * These are pre-computed based on an interfaces enabled
    289 	 * capabilities, for speed elsewhere.
    290 	 */
    291 	int	if_csum_flags_tx;	/* M_CSUM_* flags for Tx */
    292 	int	if_csum_flags_rx;	/* M_CSUM_* flags for Rx */
    293 
    294 	void	*if_afdata[AF_MAX];
    295 	struct	mowner *if_mowner;	/* who owns mbufs for this interface */
    296 
    297 	void	*if_agrprivate;		/* used only when #if NAGR > 0 */
    298 };
    299 #define	if_mtu		if_data.ifi_mtu
    300 #define	if_type		if_data.ifi_type
    301 #define	if_addrlen	if_data.ifi_addrlen
    302 #define	if_hdrlen	if_data.ifi_hdrlen
    303 #define	if_metric	if_data.ifi_metric
    304 #define	if_link_state	if_data.ifi_link_state
    305 #define	if_baudrate	if_data.ifi_baudrate
    306 #define	if_ipackets	if_data.ifi_ipackets
    307 #define	if_ierrors	if_data.ifi_ierrors
    308 #define	if_opackets	if_data.ifi_opackets
    309 #define	if_oerrors	if_data.ifi_oerrors
    310 #define	if_collisions	if_data.ifi_collisions
    311 #define	if_ibytes	if_data.ifi_ibytes
    312 #define	if_obytes	if_data.ifi_obytes
    313 #define	if_imcasts	if_data.ifi_imcasts
    314 #define	if_omcasts	if_data.ifi_omcasts
    315 #define	if_iqdrops	if_data.ifi_iqdrops
    316 #define	if_noproto	if_data.ifi_noproto
    317 #define	if_lastchange	if_data.ifi_lastchange
    318 
    319 #define	IFF_UP		0x0001		/* interface is up */
    320 #define	IFF_BROADCAST	0x0002		/* broadcast address valid */
    321 #define	IFF_DEBUG	0x0004		/* turn on debugging */
    322 #define	IFF_LOOPBACK	0x0008		/* is a loopback net */
    323 #define	IFF_POINTOPOINT	0x0010		/* interface is point-to-point link */
    324 #define	IFF_NOTRAILERS	0x0020		/* avoid use of trailers */
    325 #define	IFF_RUNNING	0x0040		/* resources allocated */
    326 #define	IFF_NOARP	0x0080		/* no address resolution protocol */
    327 #define	IFF_PROMISC	0x0100		/* receive all packets */
    328 #define	IFF_ALLMULTI	0x0200		/* receive all multicast packets */
    329 #define	IFF_OACTIVE	0x0400		/* transmission in progress */
    330 #define	IFF_SIMPLEX	0x0800		/* can't hear own transmissions */
    331 #define	IFF_LINK0	0x1000		/* per link layer defined bit */
    332 #define	IFF_LINK1	0x2000		/* per link layer defined bit */
    333 #define	IFF_LINK2	0x4000		/* per link layer defined bit */
    334 #define	IFF_MULTICAST	0x8000		/* supports multicast */
    335 
    336 #define	IFFBITS \
    337     "\020\1UP\2BROADCAST\3DEBUG\4LOOPBACK\5POINTOPOINT\6NOTRAILERS" \
    338     "\7RUNNING\10NOARP\11PROMISC\12ALLMULTI\13OACTIVE\14SIMPLEX" \
    339     "\15LINK0\16LINK1\17LINK2\20MULTICAST"
    340 
    341 /* flags set internally only: */
    342 #define	IFF_CANTCHANGE \
    343 	(IFF_BROADCAST|IFF_POINTOPOINT|IFF_RUNNING|IFF_OACTIVE|\
    344 	    IFF_SIMPLEX|IFF_MULTICAST|IFF_ALLMULTI|IFF_PROMISC)
    345 
    346 /*
    347  * Some convenience macros used for setting ifi_baudrate.
    348  * XXX 1000 vs. 1024? --thorpej (at) NetBSD.org
    349  */
    350 #define	IF_Kbps(x)	((x) * 1000)		/* kilobits/sec. */
    351 #define	IF_Mbps(x)	(IF_Kbps((x) * 1000))	/* megabits/sec. */
    352 #define	IF_Gbps(x)	(IF_Mbps((x) * 1000))	/* gigabits/sec. */
    353 
    354 /* Capabilities that interfaces can advertise. */
    355 #define	IFCAP_TSOv4		0x00080	/* can do TCPv4 segmentation offload */
    356 #define	IFCAP_CSUM_IPv4_Rx	0x00100	/* can do IPv4 header checksums (Rx) */
    357 #define	IFCAP_CSUM_IPv4_Tx	0x00200	/* can do IPv4 header checksums (Tx) */
    358 #define	IFCAP_CSUM_TCPv4_Rx	0x00400	/* can do IPv4/TCP checksums (Rx) */
    359 #define	IFCAP_CSUM_TCPv4_Tx	0x00800	/* can do IPv4/TCP checksums (Tx) */
    360 #define	IFCAP_CSUM_UDPv4_Rx	0x01000	/* can do IPv4/UDP checksums (Rx) */
    361 #define	IFCAP_CSUM_UDPv4_Tx	0x02000	/* can do IPv4/UDP checksums (Tx) */
    362 #define	IFCAP_CSUM_TCPv6_Rx	0x04000	/* can do IPv6/TCP checksums (Rx) */
    363 #define	IFCAP_CSUM_TCPv6_Tx	0x08000	/* can do IPv6/TCP checksums (Tx) */
    364 #define	IFCAP_CSUM_UDPv6_Rx	0x10000	/* can do IPv6/UDP checksums (Rx) */
    365 #define	IFCAP_CSUM_UDPv6_Tx	0x20000	/* can do IPv6/UDP checksums (Tx) */
    366 
    367 #define	IFCAPBITS		\
    368 	"\020"			\
    369 	"\10TSO4"		\
    370 	"\11IP4CSUM_Rx"		\
    371 	"\12IP4CSUM_Tx"		\
    372 	"\13TCP4CSUM_Rx"	\
    373 	"\14TCP4CSUM_Tx"	\
    374 	"\15UDP4CSUM_Rx"	\
    375 	"\16UDP4CSUM_Tx"	\
    376 	"\17TCP6CSUM_Rx"	\
    377 	"\20TCP6CSUM_Tx"	\
    378 	"\21UDP6CSUM_Rx"	\
    379 	"\22UDP6CSUM_Tx"
    380 
    381 /*
    382  * Output queues (ifp->if_snd) and internetwork datagram level (pup level 1)
    383  * input routines have queues of messages stored on ifqueue structures
    384  * (defined above).  Entries are added to and deleted from these structures
    385  * by these macros, which should be called with ipl raised to splnet().
    386  */
    387 #define	IF_QFULL(ifq)		((ifq)->ifq_len >= (ifq)->ifq_maxlen)
    388 #define	IF_DROP(ifq)		((ifq)->ifq_drops++)
    389 #define	IF_ENQUEUE(ifq, m) do { \
    390 	(m)->m_nextpkt = 0; \
    391 	if ((ifq)->ifq_tail == 0) \
    392 		(ifq)->ifq_head = m; \
    393 	else \
    394 		(ifq)->ifq_tail->m_nextpkt = m; \
    395 	(ifq)->ifq_tail = m; \
    396 	(ifq)->ifq_len++; \
    397 } while (/*CONSTCOND*/0)
    398 #define	IF_PREPEND(ifq, m) do { \
    399 	(m)->m_nextpkt = (ifq)->ifq_head; \
    400 	if ((ifq)->ifq_tail == 0) \
    401 		(ifq)->ifq_tail = (m); \
    402 	(ifq)->ifq_head = (m); \
    403 	(ifq)->ifq_len++; \
    404 } while (/*CONSTCOND*/0)
    405 #define	IF_DEQUEUE(ifq, m) do { \
    406 	(m) = (ifq)->ifq_head; \
    407 	if (m) { \
    408 		if (((ifq)->ifq_head = (m)->m_nextpkt) == 0) \
    409 			(ifq)->ifq_tail = 0; \
    410 		(m)->m_nextpkt = 0; \
    411 		(ifq)->ifq_len--; \
    412 	} \
    413 } while (/*CONSTCOND*/0)
    414 #define	IF_POLL(ifq, m)		((m) = (ifq)->ifq_head)
    415 #define	IF_PURGE(ifq)							\
    416 do {									\
    417 	struct mbuf *__m0;						\
    418 									\
    419 	for (;;) {							\
    420 		IF_DEQUEUE((ifq), __m0);				\
    421 		if (__m0 == NULL)					\
    422 			break;						\
    423 		else							\
    424 			m_freem(__m0);					\
    425 	}								\
    426 } while (/*CONSTCOND*/ 0)
    427 #define	IF_IS_EMPTY(ifq)	((ifq)->ifq_len == 0)
    428 
    429 #ifndef IFQ_MAXLEN
    430 #define	IFQ_MAXLEN	256
    431 #endif
    432 #define	IFNET_SLOWHZ	1		/* granularity is 1 second */
    433 
    434 /*
    435  * Structure defining statistics and other data kept regarding an address
    436  * on a network interface.
    437  */
    438 struct ifaddr_data {
    439 	int64_t	ifad_inbytes;
    440 	int64_t	ifad_outbytes;
    441 };
    442 
    443 /*
    444  * The ifaddr structure contains information about one address
    445  * of an interface.  They are maintained by the different address families,
    446  * are allocated and attached when an address is set, and are linked
    447  * together so all addresses for an interface can be located.
    448  */
    449 struct ifaddr {
    450 	struct	sockaddr *ifa_addr;	/* address of interface */
    451 	struct	sockaddr *ifa_dstaddr;	/* other end of p-to-p link */
    452 #define	ifa_broadaddr	ifa_dstaddr	/* broadcast address interface */
    453 	struct	sockaddr *ifa_netmask;	/* used to determine subnet */
    454 	struct	ifnet *ifa_ifp;		/* back-pointer to interface */
    455 	TAILQ_ENTRY(ifaddr) ifa_list;	/* list of addresses for interface */
    456 	struct	ifaddr_data	ifa_data;	/* statistics on the address */
    457 	void	(*ifa_rtrequest)	/* check or clean routes (+ or -)'d */
    458 		        (int, struct rtentry *, struct rt_addrinfo *);
    459 	u_int	ifa_flags;		/* mostly rt_flags for cloning */
    460 	int	ifa_refcnt;		/* count of references */
    461 	int	ifa_metric;		/* cost of going out this interface */
    462 };
    463 #define	IFA_ROUTE	RTF_UP /* 0x01 *//* route installed */
    464 
    465 /*
    466  * Message format for use in obtaining information about interfaces
    467  * from sysctl and the routing socket.
    468  */
    469 struct if_msghdr {
    470 	u_short	ifm_msglen;	/* to skip over non-understood messages */
    471 	u_char	ifm_version;	/* future binary compatibility */
    472 	u_char	ifm_type;	/* message type */
    473 	int	ifm_addrs;	/* like rtm_addrs */
    474 	int	ifm_flags;	/* value of if_flags */
    475 	u_short	ifm_index;	/* index for associated ifp */
    476 	struct	if_data ifm_data;/* statistics and other data about if */
    477 };
    478 
    479 #if defined(_KERNEL) && defined(COMPAT_14)
    480 /* pre-1.5 if_msghdr (ifm_data changed) */
    481 struct if_msghdr14 {
    482 	u_short	ifm_msglen;	/* to skip over non-understood messages */
    483 	u_char	ifm_version;	/* future binary compatibility */
    484 	u_char	ifm_type;	/* message type */
    485 	int	ifm_addrs;	/* like rtm_addrs */
    486 	int	ifm_flags;	/* value of if_flags */
    487 	u_short	ifm_index;	/* index for associated ifp */
    488 	struct	if_data14 ifm_data; /* statistics and other data about if */
    489 };
    490 #endif /* _KERNEL && COMPAT_14 */
    491 
    492 /*
    493  * Message format for use in obtaining information about interface addresses
    494  * from sysctl and the routing socket.
    495  */
    496 struct ifa_msghdr {
    497 	u_short	ifam_msglen;	/* to skip over non-understood messages */
    498 	u_char	ifam_version;	/* future binary compatibility */
    499 	u_char	ifam_type;	/* message type */
    500 	int	ifam_addrs;	/* like rtm_addrs */
    501 	int	ifam_flags;	/* value of ifa_flags */
    502 	u_short	ifam_index;	/* index for associated ifp */
    503 	int	ifam_metric;	/* value of ifa_metric */
    504 };
    505 
    506 /*
    507  * Message format announcing the arrival or departure of a network interface.
    508  */
    509 struct if_announcemsghdr {
    510 	u_short	ifan_msglen;	/* to skip over non-understood messages */
    511 	u_char	ifan_version;	/* future binary compatibility */
    512 	u_char	ifan_type;	/* message type */
    513 	u_short	ifan_index;	/* index for associated ifp */
    514 	char	ifan_name[IFNAMSIZ]; /* if name, e.g. "en0" */
    515 	u_short	ifan_what;	/* what type of announcement */
    516 };
    517 
    518 #define	IFAN_ARRIVAL	0	/* interface arrival */
    519 #define	IFAN_DEPARTURE	1	/* interface departure */
    520 
    521 /*
    522  * Interface request structure used for socket
    523  * ioctl's.  All interface ioctl's must have parameter
    524  * definitions which begin with ifr_name.  The
    525  * remainder may be interface specific.
    526  */
    527 struct	ifreq {
    528 	char	ifr_name[IFNAMSIZ];		/* if name, e.g. "en0" */
    529 	union {
    530 		struct	sockaddr ifru_addr;
    531 		struct	sockaddr ifru_dstaddr;
    532 		struct	sockaddr ifru_broadaddr;
    533 		short	ifru_flags;
    534 		int	ifru_metric;
    535 		int	ifru_mtu;
    536 		int	ifru_dlt;
    537 		u_int	ifru_value;
    538 		caddr_t	ifru_data;
    539 		struct {
    540 			uint32_t	b_buflen;
    541 			void		*b_buf;
    542 		} ifru_b;
    543 	} ifr_ifru;
    544 #define	ifr_addr	ifr_ifru.ifru_addr	/* address */
    545 #define	ifr_dstaddr	ifr_ifru.ifru_dstaddr	/* other end of p-to-p link */
    546 #define	ifr_broadaddr	ifr_ifru.ifru_broadaddr	/* broadcast address */
    547 #define	ifr_flags	ifr_ifru.ifru_flags	/* flags */
    548 #define	ifr_metric	ifr_ifru.ifru_metric	/* metric */
    549 #define	ifr_mtu		ifr_ifru.ifru_mtu	/* mtu */
    550 #define	ifr_dlt		ifr_ifru.ifru_dlt	/* data link type (DLT_*) */
    551 #define	ifr_value	ifr_ifru.ifru_value	/* generic value */
    552 #define	ifr_media	ifr_ifru.ifru_metric	/* media options (overload) */
    553 #define	ifr_data	ifr_ifru.ifru_data	/* for use by interface
    554 						 * XXX deprecated
    555 						 */
    556 #define	ifr_buf		ifr_ifru.ifru_b.b_buf	/* new interface ioctls */
    557 #define	ifr_buflen	ifr_ifru.ifru_b.b_buflen
    558 };
    559 
    560 struct ifcapreq {
    561 	char		ifcr_name[IFNAMSIZ];	/* if name, e.g. "en0" */
    562 	uint64_t	ifcr_capabilities;	/* supported capabiliites */
    563 	uint64_t	ifcr_capenable;		/* capabilities enabled */
    564 };
    565 
    566 struct ifaliasreq {
    567 	char	ifra_name[IFNAMSIZ];		/* if name, e.g. "en0" */
    568 	struct	sockaddr ifra_addr;
    569 	struct	sockaddr ifra_dstaddr;
    570 #define	ifra_broadaddr	ifra_dstaddr
    571 	struct	sockaddr ifra_mask;
    572 };
    573 
    574 struct ifdatareq {
    575 	char	ifdr_name[IFNAMSIZ];		/* if name, e.g. "en0" */
    576 	struct	if_data ifdr_data;
    577 };
    578 
    579 struct ifmediareq {
    580 	char	ifm_name[IFNAMSIZ];		/* if name, e.g. "en0" */
    581 	int	ifm_current;			/* current media options */
    582 	int	ifm_mask;			/* don't care mask */
    583 	int	ifm_status;			/* media status */
    584 	int	ifm_active;			/* active options */
    585 	int	ifm_count;			/* # entries in ifm_ulist
    586 						   array */
    587 	int	*ifm_ulist;			/* media words */
    588 };
    589 
    590 
    591 struct  ifdrv {
    592 	char		ifd_name[IFNAMSIZ];	/* if name, e.g. "en0" */
    593 	unsigned long	ifd_cmd;
    594 	size_t		ifd_len;
    595 	void		*ifd_data;
    596 };
    597 
    598 /*
    599  * Structure used in SIOCGIFCONF request.
    600  * Used to retrieve interface configuration
    601  * for machine (useful for programs which
    602  * must know all networks accessible).
    603  */
    604 struct	ifconf {
    605 	int	ifc_len;		/* size of associated buffer */
    606 	union {
    607 		caddr_t	ifcu_buf;
    608 		struct	ifreq *ifcu_req;
    609 	} ifc_ifcu;
    610 #define	ifc_buf	ifc_ifcu.ifcu_buf	/* buffer address */
    611 #define	ifc_req	ifc_ifcu.ifcu_req	/* array of structures returned */
    612 };
    613 
    614 /*
    615  * Structure for SIOC[AGD]LIFADDR
    616  */
    617 struct if_laddrreq {
    618 	char iflr_name[IFNAMSIZ];
    619 	unsigned int flags;
    620 #define IFLR_PREFIX	0x8000	/* in: prefix given  out: kernel fills id */
    621 	unsigned int prefixlen;		/* in/out */
    622 	struct sockaddr_storage addr;	/* in/out */
    623 	struct sockaddr_storage dstaddr; /* out */
    624 };
    625 
    626 #include <net/if_arp.h>
    627 
    628 #endif /* _NETBSD_SOURCE */
    629 
    630 #ifdef _KERNEL
    631 #ifdef IFAREF_DEBUG
    632 #define	IFAREF(ifa)							\
    633 do {									\
    634 	printf("IFAREF: %s:%d %p -> %d\n", __FILE__, __LINE__,		\
    635 	    (ifa), ++(ifa)->ifa_refcnt);				\
    636 } while (/*CONSTCOND*/ 0)
    637 
    638 #define	IFAFREE(ifa)							\
    639 do {									\
    640 	if ((ifa)->ifa_refcnt <= 0)					\
    641 		panic("%s:%d: %p ifa_refcnt <= 0", __FILE__,		\
    642 		    __LINE__, (ifa));					\
    643 	printf("IFAFREE: %s:%d %p -> %d\n", __FILE__, __LINE__,		\
    644 	    (ifa), --(ifa)->ifa_refcnt);				\
    645 	if ((ifa)->ifa_refcnt == 0)					\
    646 		ifafree(ifa);						\
    647 } while (/*CONSTCOND*/ 0)
    648 #else
    649 #define	IFAREF(ifa)	(ifa)->ifa_refcnt++
    650 
    651 #ifdef DIAGNOSTIC
    652 #define	IFAFREE(ifa)							\
    653 do {									\
    654 	if ((ifa)->ifa_refcnt <= 0)					\
    655 		panic("%s:%d: %p ifa_refcnt <= 0", __FILE__,		\
    656 		    __LINE__, (ifa));					\
    657 	if (--(ifa)->ifa_refcnt == 0)					\
    658 		ifafree(ifa);						\
    659 } while (/*CONSTCOND*/ 0)
    660 #else
    661 #define	IFAFREE(ifa)							\
    662 do {									\
    663 	if (--(ifa)->ifa_refcnt == 0)					\
    664 		ifafree(ifa);						\
    665 } while (/*CONSTCOND*/ 0)
    666 #endif /* DIAGNOSTIC */
    667 #endif /* IFAREF_DEBUG */
    668 
    669 #ifdef ALTQ
    670 #define	ALTQ_DECL(x)		x
    671 #define ALTQ_COMMA		,
    672 
    673 #define IFQ_ENQUEUE(ifq, m, pattr, err)					\
    674 do {									\
    675 	if (ALTQ_IS_ENABLED((ifq)))					\
    676 		ALTQ_ENQUEUE((ifq), (m), (pattr), (err));		\
    677 	else {								\
    678 		if (IF_QFULL((ifq))) {					\
    679 			m_freem((m));					\
    680 			(err) = ENOBUFS;				\
    681 		} else {						\
    682 			IF_ENQUEUE((ifq), (m));				\
    683 			(err) = 0;					\
    684 		}							\
    685 	}								\
    686 	if ((err))							\
    687 		(ifq)->ifq_drops++;					\
    688 } while (/*CONSTCOND*/ 0)
    689 
    690 #define IFQ_DEQUEUE(ifq, m)						\
    691 do {									\
    692 	if (TBR_IS_ENABLED((ifq)))					\
    693 		(m) = tbr_dequeue((ifq), ALTDQ_REMOVE);			\
    694 	else if (ALTQ_IS_ENABLED((ifq)))				\
    695 		ALTQ_DEQUEUE((ifq), (m));				\
    696 	else								\
    697 		IF_DEQUEUE((ifq), (m));					\
    698 } while (/*CONSTCOND*/ 0)
    699 
    700 #define	IFQ_POLL(ifq, m)						\
    701 do {									\
    702 	if (TBR_IS_ENABLED((ifq)))					\
    703 		(m) = tbr_dequeue((ifq), ALTDQ_POLL);			\
    704 	else if (ALTQ_IS_ENABLED((ifq)))				\
    705 		ALTQ_POLL((ifq), (m));					\
    706 	else								\
    707 		IF_POLL((ifq), (m));					\
    708 } while (/*CONSTCOND*/ 0)
    709 
    710 #define	IFQ_PURGE(ifq)							\
    711 do {									\
    712 	if (ALTQ_IS_ENABLED((ifq)))					\
    713 		ALTQ_PURGE((ifq));					\
    714 	else								\
    715 		IF_PURGE((ifq));					\
    716 } while (/*CONSTCOND*/ 0)
    717 
    718 #define	IFQ_SET_READY(ifq)						\
    719 do {									\
    720 	(ifq)->altq_flags |= ALTQF_READY;				\
    721 } while (/*CONSTCOND*/ 0)
    722 
    723 #define	IFQ_CLASSIFY(ifq, m, af, pattr)					\
    724 do {									\
    725 	if (ALTQ_IS_ENABLED((ifq))) {					\
    726 		if (ALTQ_NEEDS_CLASSIFY((ifq)))				\
    727 			(pattr)->pattr_class = (*(ifq)->altq_classify)	\
    728 				((ifq)->altq_clfier, (m), (af));	\
    729 		(pattr)->pattr_af = (af);				\
    730 		(pattr)->pattr_hdr = mtod((m), caddr_t);		\
    731 	}								\
    732 } while (/*CONSTCOND*/ 0)
    733 #else /* ! ALTQ */
    734 #define	ALTQ_DECL(x)		/* nothing */
    735 #define ALTQ_COMMA
    736 
    737 #define	IFQ_ENQUEUE(ifq, m, pattr, err)					\
    738 do {									\
    739 	if (IF_QFULL((ifq))) {						\
    740 		m_freem((m));						\
    741 		(err) = ENOBUFS;					\
    742 	} else {							\
    743 		IF_ENQUEUE((ifq), (m));					\
    744 		(err) = 0;						\
    745 	}								\
    746 	if ((err))							\
    747 		(ifq)->ifq_drops++;					\
    748 } while (/*CONSTCOND*/ 0)
    749 
    750 #define	IFQ_DEQUEUE(ifq, m)	IF_DEQUEUE((ifq), (m))
    751 
    752 #define	IFQ_POLL(ifq, m)	IF_POLL((ifq), (m))
    753 
    754 #define	IFQ_PURGE(ifq)		IF_PURGE((ifq))
    755 
    756 #define	IFQ_SET_READY(ifq)	/* nothing */
    757 
    758 #define	IFQ_CLASSIFY(ifq, m, af, pattr) /* nothing */
    759 
    760 #endif /* ALTQ */
    761 
    762 #define	IFQ_IS_EMPTY(ifq)		IF_IS_EMPTY((ifq))
    763 #define	IFQ_INC_LEN(ifq)		((ifq)->ifq_len++)
    764 #define	IFQ_DEC_LEN(ifq)		(--(ifq)->ifq_len)
    765 #define	IFQ_INC_DROPS(ifq)		((ifq)->ifq_drops++)
    766 #define	IFQ_SET_MAXLEN(ifq, len)	((ifq)->ifq_maxlen = (len))
    767 
    768 #include <sys/mallocvar.h>
    769 MALLOC_DECLARE(M_IFADDR);
    770 MALLOC_DECLARE(M_IFMADDR);
    771 
    772 #define	IFNET_FOREACH(ifp)		TAILQ_FOREACH(ifp, &ifnet, if_list)
    773 #define	IFADDR_FOREACH(ifa, ifp)	TAILQ_FOREACH(ifa, \
    774 					    &(ifp)->if_addrlist, ifa_list)
    775 
    776 extern struct ifnet_head ifnet;
    777 extern struct ifnet **ifindex2ifnet;
    778 extern struct ifnet *lo0ifp;
    779 extern size_t if_indexlim;
    780 
    781 void    ether_input(struct ifnet *, struct mbuf *);
    782 
    783 void	if_alloc_sadl(struct ifnet *);
    784 void	if_free_sadl(struct ifnet *);
    785 void	if_attach(struct ifnet *);
    786 void	if_attachdomain(void);
    787 void	if_attachdomain1(struct ifnet *);
    788 void	if_deactivate(struct ifnet *);
    789 void	if_detach(struct ifnet *);
    790 void	if_down(struct ifnet *);
    791 void	if_link_state_change(struct ifnet *, int);
    792 void	if_slowtimo(void *);
    793 void	if_up(struct ifnet *);
    794 int	ifconf(u_long, caddr_t);
    795 void	ifinit(void);
    796 int	ifioctl(struct socket *, u_long, caddr_t, struct lwp *);
    797 int	ifpromisc(struct ifnet *, int);
    798 struct	ifnet *ifunit(const char *);
    799 
    800 struct	ifaddr *ifa_ifwithaddr(const struct sockaddr *);
    801 struct	ifaddr *ifa_ifwithaf(int);
    802 struct	ifaddr *ifa_ifwithdstaddr(const struct sockaddr *);
    803 struct	ifaddr *ifa_ifwithnet(const struct sockaddr *);
    804 struct	ifaddr *ifa_ifwithladdr(const struct sockaddr *);
    805 struct	ifaddr *ifa_ifwithroute(int, const struct sockaddr *,
    806 					const struct sockaddr *);
    807 struct	ifaddr *ifaof_ifpforaddr(const struct sockaddr *, struct ifnet *);
    808 void	ifafree(struct ifaddr *);
    809 void	link_rtrequest(int, struct rtentry *, struct rt_addrinfo *);
    810 
    811 void	if_clone_attach(struct if_clone *);
    812 void	if_clone_detach(struct if_clone *);
    813 
    814 int	if_clone_create(const char *);
    815 int	if_clone_destroy(const char *);
    816 
    817 int	ifq_enqueue(struct ifnet *, struct mbuf * ALTQ_COMMA
    818     ALTQ_DECL(struct altq_pktattr *));
    819 int	ifq_enqueue2(struct ifnet *, struct ifqueue *, struct mbuf * ALTQ_COMMA
    820     ALTQ_DECL(struct altq_pktattr *));
    821 
    822 int	loioctl(struct ifnet *, u_long, caddr_t);
    823 void	loopattach(int);
    824 int	looutput(struct ifnet *,
    825 	   struct mbuf *, struct sockaddr *, struct rtentry *);
    826 void	lortrequest(int, struct rtentry *, struct rt_addrinfo *);
    827 
    828 /*
    829  * These are exported because they're an easy way to tell if
    830  * an interface is going away without having to burn a flag.
    831  */
    832 int	if_nulloutput(struct ifnet *, struct mbuf *,
    833 	    struct sockaddr *, struct rtentry *);
    834 void	if_nullinput(struct ifnet *, struct mbuf *);
    835 void	if_nullstart(struct ifnet *);
    836 int	if_nullioctl(struct ifnet *, u_long, caddr_t);
    837 int	if_nullinit(struct ifnet *);
    838 void	if_nullstop(struct ifnet *, int);
    839 void	if_nullwatchdog(struct ifnet *);
    840 void	if_nulldrain(struct ifnet *);
    841 #else
    842 struct if_nameindex {
    843 	unsigned int	if_index;	/* 1, 2, ... */
    844 	char		*if_name;	/* null terminated name: "le0", ... */
    845 };
    846 
    847 #include <sys/cdefs.h>
    848 __BEGIN_DECLS
    849 unsigned int if_nametoindex(const char *);
    850 char *	if_indextoname(unsigned int, char *);
    851 struct	if_nameindex * if_nameindex(void);
    852 void	if_freenameindex(struct if_nameindex *);
    853 __END_DECLS
    854 #endif /* _KERNEL */ /* XXX really ALTQ? */
    855 
    856 #ifdef _KERNEL
    857 /*
    858  * ifq sysctl support
    859  */
    860 int	sysctl_ifq(int *name, u_int namelen, void *oldp,
    861 		       size_t *oldlenp, void *newp, size_t newlen,
    862 		       struct ifqueue *ifq);
    863 /* symbolic names for terminal (per-protocol) CTL_IFQ_ nodes */
    864 #define IFQCTL_LEN 1
    865 #define IFQCTL_MAXLEN 2
    866 #define IFQCTL_PEAK 3
    867 #define IFQCTL_DROPS 4
    868 #define IFQCTL_MAXID 5
    869 
    870 #endif /* _KERNEL */
    871 
    872 #ifdef _NETBSD_SOURCE
    873 /*
    874  * sysctl for ifq (per-protocol packet input queue variant of ifqueue)
    875  */
    876 #define CTL_IFQ_NAMES  { \
    877 	{ 0, 0 }, \
    878 	{ "len", CTLTYPE_INT }, \
    879 	{ "maxlen", CTLTYPE_INT }, \
    880 	{ "peak", CTLTYPE_INT }, \
    881 	{ "drops", CTLTYPE_INT }, \
    882 }
    883 #endif /* _NETBSD_SOURCE */
    884 #endif /* !_NET_IF_H_ */
    885