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