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