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