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