xref: /openbsd/sys/net/if.h (revision 73e65003)
1 /*	$OpenBSD: if.h,v 1.140 2013/03/20 10:34:12 mpi Exp $	*/
2 /*	$NetBSD: if.h,v 1.23 1996/05/07 02:40:27 thorpej Exp $	*/
3 
4 /*
5  * Copyright (c) 1982, 1986, 1989, 1993
6  *	The Regents of the University of California.  All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  * 3. Neither the name of the University nor the names of its contributors
17  *    may be used to endorse or promote products derived from this software
18  *    without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30  * SUCH DAMAGE.
31  *
32  *	@(#)if.h	8.1 (Berkeley) 6/10/93
33  */
34 
35 #ifndef _NET_IF_H_
36 #define _NET_IF_H_
37 
38 #include <sys/cdefs.h>
39 
40 /*
41  * Length of interface external name, including terminating '\0'.
42  * Note: this is the same size as a generic device's external name.
43  */
44 #define	IF_NAMESIZE	16
45 
46 struct if_nameindex {
47 	unsigned int	if_index;
48 	char		*if_name;
49 };
50 
51 #ifndef _KERNEL
52 __BEGIN_DECLS
53 unsigned int if_nametoindex(const char *);
54 char	*if_indextoname(unsigned int, char *);
55 struct	if_nameindex *if_nameindex(void);
56 void	if_freenameindex(struct if_nameindex *);
57 __END_DECLS
58 #endif
59 
60 #if __BSD_VISIBLE
61 
62 #include <sys/queue.h>
63 #include <sys/tree.h>
64 
65 /*
66  * Always include ALTQ glue here -- we use the ALTQ interface queue
67  * structure even when ALTQ is not configured into the kernel so that
68  * the size of struct ifnet does not changed based on the option.  The
69  * ALTQ queue structure is API-compatible with the legacy ifqueue.
70  */
71 #include <altq/if_altq.h>
72 
73 /*
74  * Structures defining a network interface, providing a packet
75  * transport mechanism (ala level 0 of the PUP protocols).
76  *
77  * Each interface accepts output datagrams of a specified maximum
78  * length, and provides higher level routines with input datagrams
79  * received from its medium.
80  *
81  * Output occurs when the routine if_output is called, with four parameters:
82  *	(*ifp->if_output)(ifp, m, dst, rt)
83  * Here m is the mbuf chain to be sent and dst is the destination address.
84  * The output routine encapsulates the supplied datagram if necessary,
85  * and then transmits it on its medium.
86  *
87  * On input, each interface unwraps the data received by it, and either
88  * places it on the input queue of an internetwork datagram routine
89  * and posts the associated software interrupt, or passes the datagram to a raw
90  * packet input routine.
91  *
92  * Routines exist for locating interfaces by their addresses
93  * or for locating an interface on a certain network, as well as more general
94  * routing and gateway routines maintaining information used to locate
95  * interfaces.  These routines live in the files if.c and route.c
96  */
97 /*  XXX fast fix for SNMP, going away soon */
98 #include <sys/time.h>
99 
100 struct mbuf;
101 struct proc;
102 struct rtentry;
103 struct socket;
104 struct ether_header;
105 struct arpcom;
106 struct rt_addrinfo;
107 struct ifnet;
108 
109 /*
110  * Structure describing a `cloning' interface.
111  */
112 struct if_clone {
113 	LIST_ENTRY(if_clone) ifc_list;	/* on list of cloners */
114 	const char *ifc_name;		/* name of device, e.g. `gif' */
115 	size_t ifc_namelen;		/* length of name */
116 
117 	int	(*ifc_create)(struct if_clone *, int);
118 	int	(*ifc_destroy)(struct ifnet *);
119 };
120 
121 #define	IF_CLONE_INITIALIZER(name, create, destroy)			\
122 	{ { 0 }, name, sizeof(name) - 1, create, destroy }
123 
124 /*
125  * Structure used to query names of interface cloners.
126  */
127 struct if_clonereq {
128 	int	ifcr_total;		/* total cloners (out) */
129 	int	ifcr_count;		/* room for this many in user buffer */
130 	char	*ifcr_buffer;		/* buffer for cloner names */
131 };
132 
133 #define MCLPOOLS	7		/* number of cluster pools */
134 
135 struct mclpool {
136 	u_int	mcl_grown;
137 	u_short	mcl_alive;
138 	u_short mcl_hwm;
139 	u_short mcl_cwm;
140 	u_short mcl_lwm;
141 };
142 
143 /*
144  * Structure defining statistics and other data kept regarding a network
145  * interface.
146  */
147 struct	if_data {
148 	/* generic interface information */
149 	u_char		ifi_type;		/* ethernet, tokenring, etc. */
150 	u_char		ifi_addrlen;		/* media address length */
151 	u_char		ifi_hdrlen;		/* media header length */
152 	u_char		ifi_link_state;		/* current link state */
153 	u_int32_t	ifi_mtu;		/* maximum transmission unit */
154 	u_int32_t	ifi_metric;		/* routing metric (external only) */
155 	u_int32_t	ifi_pad;
156 	u_int64_t	ifi_baudrate;		/* linespeed */
157 	/* volatile statistics */
158 	u_int64_t	ifi_ipackets;		/* packets received on interface */
159 	u_int64_t	ifi_ierrors;		/* input errors on interface */
160 	u_int64_t	ifi_opackets;		/* packets sent on interface */
161 	u_int64_t	ifi_oerrors;		/* output errors on interface */
162 	u_int64_t	ifi_collisions;		/* collisions on csma interfaces */
163 	u_int64_t	ifi_ibytes;		/* total number of octets received */
164 	u_int64_t	ifi_obytes;		/* total number of octets sent */
165 	u_int64_t	ifi_imcasts;		/* packets received via multicast */
166 	u_int64_t	ifi_omcasts;		/* packets sent via multicast */
167 	u_int64_t	ifi_iqdrops;		/* dropped on input, this interface */
168 	u_int64_t	ifi_noproto;		/* destined for unsupported protocol */
169 	u_int32_t	ifi_capabilities;	/* interface capabilities */
170 	struct	timeval ifi_lastchange;	/* last operational state change */
171 
172 	struct mclpool	ifi_mclpool[MCLPOOLS];
173 };
174 
175 #define IFQ_NQUEUES	ALTQ_IFQ_NQUEUES
176 #define IFQ_MAXPRIO	IFQ_NQUEUES - 1
177 #define IFQ_DEFPRIO	3
178 
179 /*
180  * Structure defining a queue for a network interface.
181  * XXX keep in sync with struct ifaltq.
182  */
183 struct	ifqueue {
184 	struct {
185 		struct	mbuf *head;
186 		struct	mbuf *tail;
187 	}	ifq_q[IFQ_NQUEUES];
188 	int	ifq_len;
189 	int	ifq_maxlen;
190 	int	ifq_drops;
191 	struct	timeout *ifq_congestion;
192 };
193 
194 /*
195  * Values for if_link_state.
196  */
197 #define LINK_STATE_UNKNOWN	0	/* link unknown */
198 #define LINK_STATE_INVALID	1	/* link invalid */
199 #define LINK_STATE_DOWN		2	/* link is down */
200 #define LINK_STATE_KALIVE_DOWN	3	/* keepalive reports down */
201 #define LINK_STATE_UP		4	/* link is up */
202 #define LINK_STATE_HALF_DUPLEX	5	/* link is up and half duplex */
203 #define LINK_STATE_FULL_DUPLEX	6	/* link is up and full duplex */
204 
205 #define LINK_STATE_IS_UP(_s)	\
206 		((_s) >= LINK_STATE_UP || (_s) == LINK_STATE_UNKNOWN)
207 
208 /*
209  * Status bit descriptions for the various interface types.
210  */
211 struct if_status_description {
212 	u_char	ifs_type;
213 	u_char	ifs_state;
214 	const char *ifs_string;
215 };
216 
217 #define LINK_STATE_DESC_MATCH(_ifs, _t, _s)				\
218 	(((_ifs)->ifs_type == (_t) || (_ifs)->ifs_type == 0) &&		\
219 	    (_ifs)->ifs_state == (_s))
220 
221 
222 
223 #define LINK_STATE_DESCRIPTIONS {					\
224 	{ IFT_ETHER, LINK_STATE_DOWN, "no carrier" },			\
225 									\
226 	{ IFT_IEEE80211, LINK_STATE_DOWN, "no network" },		\
227 									\
228 	{ IFT_PPP, LINK_STATE_DOWN, "no carrier" },			\
229 									\
230 	{ IFT_CARP, LINK_STATE_DOWN, "backup" },			\
231 	{ IFT_CARP, LINK_STATE_UP, "master" },				\
232 	{ IFT_CARP, LINK_STATE_HALF_DUPLEX, "master" },			\
233 	{ IFT_CARP, LINK_STATE_FULL_DUPLEX, "master" },			\
234 									\
235 	{ 0, LINK_STATE_UP, "active" },					\
236 	{ 0, LINK_STATE_HALF_DUPLEX, "active" },			\
237 	{ 0, LINK_STATE_FULL_DUPLEX, "active" },			\
238 									\
239 	{ 0, LINK_STATE_UNKNOWN, "unknown" },				\
240 	{ 0, LINK_STATE_INVALID, "invalid" },				\
241 	{ 0, LINK_STATE_DOWN, "down" },					\
242 	{ 0, LINK_STATE_KALIVE_DOWN, "keepalive down" },		\
243 	{ 0, 0, NULL }							\
244 }
245 
246 /*
247  * Structure defining a queue for a network interface.
248  *
249  * (Would like to call this struct ``if'', but C isn't PL/1.)
250  */
251 TAILQ_HEAD(ifnet_head, ifnet);		/* the actual queue head */
252 
253 /* Traditional BSD name for length of interface external name. */
254 #define	IFNAMSIZ	IF_NAMESIZE
255 
256 /*
257  * Length of interface description, including terminating '\0'.
258  */
259 #define	IFDESCRSIZE	64
260 
261 struct ifnet {				/* and the entries */
262 	void	*if_softc;		/* lower-level data for this if */
263 	TAILQ_ENTRY(ifnet) if_list;	/* all struct ifnets are chained */
264 	TAILQ_ENTRY(ifnet) if_txlist;	/* list of ifnets ready to tx */
265 	TAILQ_HEAD(, ifaddr) if_addrlist; /* linked list of addresses per if */
266 	TAILQ_HEAD(, ifg_list) if_groups; /* linked list of groups per if */
267 	struct hook_desc_head *if_addrhooks; /* address change callbacks */
268 	struct hook_desc_head *if_linkstatehooks; /* link change callbacks */
269 	struct hook_desc_head *if_detachhooks; /* detach callbacks */
270 	char	if_xname[IFNAMSIZ];	/* external name (name + unit) */
271 	int	if_pcount;		/* number of promiscuous listeners */
272 	caddr_t	if_bpf;			/* packet filter structure */
273 	caddr_t if_bridgeport;		/* used by bridge ports */
274 	caddr_t	if_tp;			/* used by trunk ports */
275 	caddr_t	if_pf_kif;		/* pf interface abstraction */
276 	union {
277 		caddr_t	carp_s;		/* carp structure (used by !carp ifs) */
278 		struct ifnet *carp_d;	/* ptr to carpdev (used by carp ifs) */
279 	} if_carp_ptr;
280 #define if_carp		if_carp_ptr.carp_s
281 #define if_carpdev	if_carp_ptr.carp_d
282 	u_short	if_index;		/* numeric abbreviation for this if */
283 	short	if_timer;		/* time 'til if_watchdog called */
284 	short	if_flags;		/* up/down, broadcast, etc. */
285 	int	if_xflags;		/* extra softnet flags */
286 	struct	if_data if_data;	/* stats and other data about if */
287 	u_int32_t if_hardmtu;		/* maximum MTU device supports */
288 	u_int	if_rdomain;		/* routing instance */
289 	char	if_description[IFDESCRSIZE]; /* interface description */
290 	u_short	if_rtlabelid;		/* next route label */
291 	u_int8_t if_priority;
292 
293 	/* procedure handles */
294 					/* output routine (enqueue) */
295 	int	(*if_output)(struct ifnet *, struct mbuf *, struct sockaddr *,
296 		     struct rtentry *);
297 
298 					/* link level output function */
299 	int	(*if_ll_output)(struct ifnet *, struct mbuf *,
300 		    struct sockaddr *, struct rtentry *);
301 					/* initiate output routine */
302 	void	(*if_start)(struct ifnet *);
303 					/* ioctl routine */
304 	int	(*if_ioctl)(struct ifnet *, u_long, caddr_t);
305 					/* stop routine */
306 	int	(*if_stop)(struct ifnet *, int);
307 					/* timer routine */
308 	void	(*if_watchdog)(struct ifnet *);
309 	int	(*if_wol)(struct ifnet *, int);
310 	struct	ifaltq if_snd;		/* output queue (includes altq) */
311 	struct sockaddr_dl *if_sadl;	/* pointer to our sockaddr_dl */
312 
313 	void	*if_afdata[AF_MAX];
314 };
315 #define	if_mtu		if_data.ifi_mtu
316 #define	if_type		if_data.ifi_type
317 #define	if_addrlen	if_data.ifi_addrlen
318 #define	if_hdrlen	if_data.ifi_hdrlen
319 #define	if_metric	if_data.ifi_metric
320 #define	if_link_state	if_data.ifi_link_state
321 #define	if_baudrate	if_data.ifi_baudrate
322 #define	if_ipackets	if_data.ifi_ipackets
323 #define	if_ierrors	if_data.ifi_ierrors
324 #define	if_opackets	if_data.ifi_opackets
325 #define	if_oerrors	if_data.ifi_oerrors
326 #define	if_collisions	if_data.ifi_collisions
327 #define	if_ibytes	if_data.ifi_ibytes
328 #define	if_obytes	if_data.ifi_obytes
329 #define	if_imcasts	if_data.ifi_imcasts
330 #define	if_omcasts	if_data.ifi_omcasts
331 #define	if_iqdrops	if_data.ifi_iqdrops
332 #define	if_noproto	if_data.ifi_noproto
333 #define	if_lastchange	if_data.ifi_lastchange
334 #define	if_capabilities	if_data.ifi_capabilities
335 
336 #define	IFF_UP		0x1		/* interface is up */
337 #define	IFF_BROADCAST	0x2		/* broadcast address valid */
338 #define	IFF_DEBUG	0x4		/* turn on debugging */
339 #define	IFF_LOOPBACK	0x8		/* is a loopback net */
340 #define	IFF_POINTOPOINT	0x10		/* interface is point-to-point link */
341 #define	IFF_NOTRAILERS	0x20		/* avoid use of trailers */
342 #define	IFF_RUNNING	0x40		/* resources allocated */
343 #define	IFF_NOARP	0x80		/* no address resolution protocol */
344 #define	IFF_PROMISC	0x100		/* receive all packets */
345 #define	IFF_ALLMULTI	0x200		/* receive all multicast packets */
346 #define	IFF_OACTIVE	0x400		/* transmission in progress */
347 #define	IFF_SIMPLEX	0x800		/* can't hear own transmissions */
348 #define	IFF_LINK0	0x1000		/* per link layer defined bit */
349 #define	IFF_LINK1	0x2000		/* per link layer defined bit */
350 #define	IFF_LINK2	0x4000		/* per link layer defined bit */
351 #define	IFF_MULTICAST	0x8000		/* supports multicast */
352 
353 /* flags set internally only: */
354 #define	IFF_CANTCHANGE \
355 	(IFF_BROADCAST|IFF_POINTOPOINT|IFF_RUNNING|IFF_OACTIVE|\
356 	    IFF_SIMPLEX|IFF_MULTICAST|IFF_ALLMULTI)
357 
358 #define IFXF_TXREADY		0x1		/* interface is ready to tx */
359 #define	IFXF_NOINET6		0x2		/* don't do inet6 */
360 #define	IFXF_INET6_NOPRIVACY	0x4		/* don't autoconf privacy */
361 #define	IFXF_MPLS		0x8		/* supports MPLS */
362 #define	IFXF_WOL		0x10		/* wake on lan enabled */
363 
364 #define	IFXF_CANTCHANGE \
365 	(IFXF_TXREADY)
366 
367 /*
368  * Some convenience macros used for setting ifi_baudrate.
369  */
370 #define	IF_Kbps(x)	((x) * 1000ULL)			/* kilobits/sec. */
371 #define	IF_Mbps(x)	(IF_Kbps((x) * 1000ULL))	/* megabits/sec. */
372 #define	IF_Gbps(x)	(IF_Mbps((x) * 1000ULL))	/* gigabits/sec. */
373 
374 /* Capabilities that interfaces can advertise. */
375 #define	IFCAP_CSUM_IPv4		0x00000001	/* can do IPv4 header csum */
376 #define	IFCAP_CSUM_TCPv4	0x00000002	/* can do IPv4/TCP csum */
377 #define	IFCAP_CSUM_UDPv4	0x00000004	/* can do IPv4/UDP csum */
378 #define	IFCAP_VLAN_MTU		0x00000010	/* VLAN-compatible MTU */
379 #define	IFCAP_VLAN_HWTAGGING	0x00000020	/* hardware VLAN tag support */
380 #define	IFCAP_CSUM_TCPv6	0x00000080	/* can do IPv6/TCP checksums */
381 #define	IFCAP_CSUM_UDPv6	0x00000100	/* can do IPv6/UDP checksums */
382 #define	IFCAP_WOL		0x00008000	/* can do wake on lan */
383 
384 #define IFCAP_CSUM_MASK		(IFCAP_CSUM_IPv4 | IFCAP_CSUM_TCPv4 | \
385     IFCAP_CSUM_UDPv4 | IFCAP_CSUM_TCPv6 | IFCAP_CSUM_UDPv6)
386 
387 /*
388  * Output queues (ifp->if_snd) and internetwork datagram level (pup level 1)
389  * input routines have queues of messages stored on ifqueue structures
390  * (defined above).  Entries are added to and deleted from these structures
391  * by these macros, which should be called with ipl raised to splnet().
392  */
393 #define	IF_QFULL(ifq)		((ifq)->ifq_len >= (ifq)->ifq_maxlen)
394 #define	IF_DROP(ifq)		((ifq)->ifq_drops++)
395 #define	IF_ENQUEUE(ifq, m)						\
396 do {									\
397 	(m)->m_nextpkt = NULL;						\
398 	if ((ifq)->ifq_q[(m)->m_pkthdr.pf.prio].tail == NULL)		\
399 		(ifq)->ifq_q[(m)->m_pkthdr.pf.prio].head = m;		\
400 	else								\
401 		(ifq)->ifq_q[(m)->m_pkthdr.pf.prio].tail->m_nextpkt = m; \
402 	(ifq)->ifq_q[(m)->m_pkthdr.pf.prio].tail = m;			\
403 	(ifq)->ifq_len++;						\
404 } while (/* CONSTCOND */0)
405 #define	IF_PREPEND(ifq, m)						\
406 do {									\
407 	(m)->m_nextpkt = (ifq)->ifq_q[(m)->m_pkthdr.pf.prio].head;	\
408 	if ((ifq)->ifq_q[(m)->m_pkthdr.pf.prio].tail == NULL)		\
409 		(ifq)->ifq_q[(m)->m_pkthdr.pf.prio].tail = (m);		\
410 	(ifq)->ifq_q[(m)->m_pkthdr.pf.prio].head = (m);			\
411 	(ifq)->ifq_len++;						\
412 } while (/* CONSTCOND */0)
413 
414 #define	IF_POLL(ifq, m)							\
415 do {									\
416 	int	if_dequeue_prio = IFQ_MAXPRIO;				\
417 	do {								\
418 		(m) = (ifq)->ifq_q[if_dequeue_prio].head;		\
419 	} while (!(m) && --if_dequeue_prio >= 0); 			\
420 } while (/* CONSTCOND */0)
421 
422 #define	IF_DEQUEUE(ifq, m)						\
423 do {									\
424 	int	if_dequeue_prio = IFQ_MAXPRIO;				\
425 	do {								\
426 		(m) = (ifq)->ifq_q[if_dequeue_prio].head;		\
427 		if (m) {						\
428 			if (((ifq)->ifq_q[if_dequeue_prio].head =	\
429 			    (m)->m_nextpkt) == NULL)			\
430 				(ifq)->ifq_q[if_dequeue_prio].tail = NULL; \
431 			(m)->m_nextpkt = NULL;				\
432 			(ifq)->ifq_len--;				\
433 		}							\
434 	} while (!(m) && --if_dequeue_prio >= 0);			\
435 } while (/* CONSTCOND */0)
436 
437 #define	IF_INPUT_ENQUEUE(ifq, m)					\
438 do {									\
439 	if (IF_QFULL(ifq)) {						\
440 		IF_DROP(ifq);						\
441 		m_freem(m);						\
442 		if (!(ifq)->ifq_congestion)				\
443 			if_congestion(ifq);				\
444 	} else								\
445 		IF_ENQUEUE(ifq, m);					\
446 } while (/* CONSTCOND */0)
447 
448 #define	IF_PURGE(ifq)							\
449 do {									\
450 	struct mbuf *__m0;						\
451 									\
452 	for (;;) {							\
453 		IF_DEQUEUE((ifq), __m0);				\
454 		if (__m0 == NULL)					\
455 			break;						\
456 		else							\
457 			m_freem(__m0);					\
458 	}								\
459 } while (/* CONSTCOND */0)
460 #define	IF_LEN(ifq)		((ifq)->ifq_len)
461 #define	IF_IS_EMPTY(ifq)	((ifq)->ifq_len == 0)
462 
463 #define	IFQ_MAXLEN	256
464 #define	IFNET_SLOWHZ	1		/* granularity is 1 second */
465 
466 /* symbolic names for terminal (per-protocol) CTL_IFQ_ nodes */
467 #define IFQCTL_LEN 1
468 #define IFQCTL_MAXLEN 2
469 #define IFQCTL_DROPS 3
470 #define IFQCTL_CONGESTION 4
471 #define IFQCTL_MAXID 5
472 
473 /* sysctl for ifq (per-protocol packet input queue variant of ifqueue) */
474 #define CTL_IFQ_NAMES  { \
475 	{ 0, 0 }, \
476 	{ "len", CTLTYPE_INT }, \
477 	{ "maxlen", CTLTYPE_INT }, \
478 	{ "drops", CTLTYPE_INT }, \
479 	{ "congestion", CTLTYPE_INT }, \
480 }
481 
482 /*
483  * The ifaddr structure contains information about one address
484  * of an interface.  They are maintained by the different address families,
485  * are allocated and attached when an address is set, and are linked
486  * together so all addresses for an interface can be located.
487  */
488 struct ifaddr {
489 	struct	sockaddr *ifa_addr;	/* address of interface */
490 	struct	sockaddr *ifa_dstaddr;	/* other end of p-to-p link */
491 #define	ifa_broadaddr	ifa_dstaddr	/* broadcast address interface */
492 	struct	sockaddr *ifa_netmask;	/* used to determine subnet */
493 	struct	ifnet *ifa_ifp;		/* back-pointer to interface */
494 	TAILQ_ENTRY(ifaddr) ifa_list;	/* list of addresses for interface */
495 					/* check or clean routes (+ or -)'d */
496 	void	(*ifa_rtrequest)(int, struct rtentry *, struct rt_addrinfo *);
497 	u_int	ifa_flags;		/* mostly rt_flags for cloning */
498 	u_int	ifa_refcnt;		/* count of references */
499 	int	ifa_metric;		/* cost of going out this interface */
500 };
501 #define	IFA_ROUTE	RTF_UP		/* route installed */
502 
503 struct ifaddr_item {
504 	RB_ENTRY(ifaddr_item)	 ifai_entry;
505 	struct sockaddr		*ifai_addr;
506 	struct ifaddr		*ifai_ifa;
507 	struct ifaddr_item	*ifai_next;
508 	u_int			 ifai_rdomain;
509 };
510 
511 /*
512  * Message format for use in obtaining information about interfaces
513  * from sysctl and the routing socket.
514  */
515 struct if_msghdr {
516 	u_short	ifm_msglen;	/* to skip over non-understood messages */
517 	u_char	ifm_version;	/* future binary compatibility */
518 	u_char	ifm_type;	/* message type */
519 	u_short ifm_hdrlen;	/* sizeof(if_msghdr) to skip over the header */
520 	u_short	ifm_index;	/* index for associated ifp */
521 	u_short	ifm_tableid;	/* routing table id */
522 	u_char	ifm_pad1;
523 	u_char	ifm_pad2;
524 	int	ifm_addrs;	/* like rtm_addrs */
525 	int	ifm_flags;	/* value of if_flags */
526 	int	ifm_xflags;
527 	struct	if_data ifm_data;/* statistics and other data about if */
528 };
529 
530 /*
531  * Message format for use in obtaining information about interface addresses
532  * from sysctl and the routing socket.
533  */
534 struct ifa_msghdr {
535 	u_short	ifam_msglen;	/* to skip over non-understood messages */
536 	u_char	ifam_version;	/* future binary compatibility */
537 	u_char	ifam_type;	/* message type */
538 	u_short ifam_hdrlen;	/* sizeof(ifa_msghdr) to skip over the header */
539 	u_short	ifam_index;	/* index for associated ifp */
540 	u_short	ifam_tableid;	/* routing table id */
541 	u_char	ifam_pad1;
542 	u_char	ifam_pad2;
543 	int	ifam_addrs;	/* like rtm_addrs */
544 	int	ifam_flags;	/* value of ifa_flags */
545 	int	ifam_metric;	/* value of ifa_metric */
546 };
547 
548 
549 /*
550  * Message format announcing the arrival or departure of a network interface.
551  */
552 struct if_announcemsghdr {
553 	u_short	ifan_msglen;	/* to skip over non-understood messages */
554 	u_char	ifan_version;	/* future binary compatibility */
555 	u_char	ifan_type;	/* message type */
556 	u_short ifan_hdrlen;	/* sizeof(ifa_msghdr) to skip over the header */
557 	u_short	ifan_index;	/* index for associated ifp */
558 	u_short	ifan_what;	/* what type of announcement */
559 	char	ifan_name[IFNAMSIZ];	/* if name, e.g. "en0" */
560 };
561 
562 #define IFAN_ARRIVAL	0	/* interface arrival */
563 #define IFAN_DEPARTURE	1	/* interface departure */
564 
565 /*
566  * interface groups
567  */
568 
569 #define	IFG_ALL		"all"		/* group contains all interfaces */
570 #define	IFG_EGRESS	"egress"	/* if(s) default route(s) point to */
571 
572 struct ifg_group {
573 	char				 ifg_group[IFNAMSIZ];
574 	u_int				 ifg_refcnt;
575 	caddr_t				 ifg_pf_kif;
576 	int				 ifg_carp_demoted;
577 	TAILQ_HEAD(, ifg_member)	 ifg_members;
578 	TAILQ_ENTRY(ifg_group)		 ifg_next;
579 };
580 
581 struct ifg_member {
582 	TAILQ_ENTRY(ifg_member)	 ifgm_next;
583 	struct ifnet		*ifgm_ifp;
584 };
585 
586 struct ifg_list {
587 	struct ifg_group	*ifgl_group;
588 	TAILQ_ENTRY(ifg_list)	 ifgl_next;
589 };
590 
591 struct ifg_req {
592 	union {
593 		char			 ifgrqu_group[IFNAMSIZ];
594 		char			 ifgrqu_member[IFNAMSIZ];
595 	} ifgrq_ifgrqu;
596 #define	ifgrq_group	ifgrq_ifgrqu.ifgrqu_group
597 #define	ifgrq_member	ifgrq_ifgrqu.ifgrqu_member
598 };
599 
600 struct ifg_attrib {
601 	int	ifg_carp_demoted;
602 };
603 
604 /*
605  * Used to lookup groups for an interface
606  */
607 struct ifgroupreq {
608 	char	ifgr_name[IFNAMSIZ];
609 	u_int	ifgr_len;
610 	union {
611 		char			 ifgru_group[IFNAMSIZ];
612 		struct	ifg_req		*ifgru_groups;
613 		struct	ifg_attrib	 ifgru_attrib;
614 	} ifgr_ifgru;
615 #define ifgr_group	ifgr_ifgru.ifgru_group
616 #define ifgr_groups	ifgr_ifgru.ifgru_groups
617 #define ifgr_attrib	ifgr_ifgru.ifgru_attrib
618 };
619 
620 /*
621  * Interface request structure used for socket
622  * ioctl's.  All interface ioctl's must have parameter
623  * definitions which begin with ifr_name.  The
624  * remainder may be interface specific.
625  */
626 struct	ifreq {
627 	char	ifr_name[IFNAMSIZ];		/* if name, e.g. "en0" */
628 	union {
629 		struct	sockaddr ifru_addr;
630 		struct	sockaddr ifru_dstaddr;
631 		struct	sockaddr ifru_broadaddr;
632 		short	ifru_flags;
633 		int	ifru_metric;
634 		caddr_t	ifru_data;
635 	} ifr_ifru;
636 #define	ifr_addr	ifr_ifru.ifru_addr	/* address */
637 #define	ifr_dstaddr	ifr_ifru.ifru_dstaddr	/* other end of p-to-p link */
638 #define	ifr_broadaddr	ifr_ifru.ifru_broadaddr	/* broadcast address */
639 #define	ifr_flags	ifr_ifru.ifru_flags	/* flags */
640 #define	ifr_metric	ifr_ifru.ifru_metric	/* metric */
641 #define	ifr_mtu		ifr_ifru.ifru_metric	/* mtu (overload) */
642 #define	ifr_hardmtu	ifr_ifru.ifru_metric	/* hardmtu (overload) */
643 #define	ifr_media	ifr_ifru.ifru_metric	/* media options (overload) */
644 #define	ifr_rdomainid	ifr_ifru.ifru_metric	/* VRF instance (overload) */
645 #define	ifr_data	ifr_ifru.ifru_data	/* for use by interface */
646 };
647 
648 struct ifaliasreq {
649 	char	ifra_name[IFNAMSIZ];		/* if name, e.g. "en0" */
650 	union {
651 		struct	sockaddr ifrau_addr;
652 		int	ifrau_align;
653 	 } ifra_ifrau;
654 #ifndef ifra_addr
655 #define ifra_addr	ifra_ifrau.ifrau_addr
656 #endif
657 	struct	sockaddr ifra_dstaddr;
658 #define	ifra_broadaddr	ifra_dstaddr
659 	struct	sockaddr ifra_mask;
660 };
661 
662 struct ifmediareq {
663 	char	ifm_name[IFNAMSIZ];		/* if name, e.g. "en0" */
664 	int	ifm_current;			/* current media options */
665 	int	ifm_mask;			/* don't care mask */
666 	int	ifm_status;			/* media status */
667 	int	ifm_active;			/* active options */
668 	int	ifm_count;			/* # entries in ifm_ulist
669 							array */
670 	int	*ifm_ulist;			/* media words */
671 };
672 
673 struct ifkalivereq {
674 	char	ikar_name[IFNAMSIZ];		/* if name, e.g. "en0" */
675 	int	ikar_timeo;
676 	int	ikar_cnt;
677 };
678 
679 /*
680  * Structure used in SIOCGIFCONF request.
681  * Used to retrieve interface configuration
682  * for machine (useful for programs which
683  * must know all networks accessible).
684  */
685 struct	ifconf {
686 	int	ifc_len;		/* size of associated buffer */
687 	union {
688 		caddr_t	ifcu_buf;
689 		struct	ifreq *ifcu_req;
690 	} ifc_ifcu;
691 #define	ifc_buf	ifc_ifcu.ifcu_buf	/* buffer address */
692 #define	ifc_req	ifc_ifcu.ifcu_req	/* array of structures returned */
693 };
694 
695 /*
696  * Structure for SIOC[AGD]LIFADDR
697  */
698 struct if_laddrreq {
699 	char iflr_name[IFNAMSIZ];
700 	unsigned int flags;
701 #define IFLR_PREFIX	0x8000	/* in: prefix given  out: kernel fills id */
702 	unsigned int prefixlen;		/* in/out */
703 	struct sockaddr_storage addr;	/* in/out */
704 	struct sockaddr_storage dstaddr; /* out */
705 };
706 
707 #include <net/if_arp.h>
708 
709 #ifdef _KERNEL
710 #ifdef ALTQ
711 
712 #define	IFQ_ENQUEUE(ifq, m, pattr, err)					\
713 do {									\
714 	if (ALTQ_IS_ENABLED((ifq))) {					\
715 		m->m_pkthdr.pf.prio = IFQ_MAXPRIO;			\
716 		ALTQ_ENQUEUE((ifq), (m), (pattr), (err));		\
717 	} else {							\
718 		if (IF_QFULL((ifq))) {					\
719 			m_freem((m));					\
720 			(err) = ENOBUFS;				\
721 		} else {						\
722 			IF_ENQUEUE((ifq), (m));				\
723 			(err) = 0;					\
724 		}							\
725 	}								\
726 	if ((err))							\
727 		(ifq)->ifq_drops++;					\
728 } while (/* CONSTCOND */0)
729 
730 #define	IFQ_DEQUEUE(ifq, m)						\
731 do {									\
732 	if (OLDTBR_IS_ENABLED((ifq)))					\
733 		(m) = oldtbr_dequeue((ifq), ALTDQ_REMOVE);			\
734 	else if (ALTQ_IS_ENABLED((ifq)))				\
735 		ALTQ_DEQUEUE((ifq), (m));				\
736 	else								\
737 		IF_DEQUEUE((ifq), (m));					\
738 } while (/* CONSTCOND */0)
739 
740 #define	IFQ_POLL(ifq, m)						\
741 do {									\
742 	if (TBR_IS_ENABLED((ifq)))					\
743 		(m) = oldtbr_dequeue((ifq), ALTDQ_POLL);			\
744 	else if (ALTQ_IS_ENABLED((ifq)))				\
745 		ALTQ_POLL((ifq), (m));					\
746 	else								\
747 		IF_POLL((ifq), (m));					\
748 } while (/* CONSTCOND */0)
749 
750 #define	IFQ_PURGE(ifq)							\
751 do {									\
752 	if (ALTQ_IS_ENABLED((ifq)))					\
753 		ALTQ_PURGE((ifq));					\
754 	else								\
755 		IF_PURGE((ifq));					\
756 } while (/* CONSTCOND */0)
757 
758 #define	IFQ_SET_READY(ifq)						\
759 do {									\
760 	((ifq)->altq_flags |= ALTQF_READY);				\
761 } while (/* CONSTCOND */0)
762 
763 #else /* !ALTQ */
764 
765 #define	IFQ_ENQUEUE(ifq, m, pattr, err)					\
766 do {									\
767 	if (IF_QFULL((ifq))) {						\
768 		m_freem((m));						\
769 		(err) = ENOBUFS;					\
770 	} else {							\
771 		IF_ENQUEUE((ifq), (m));					\
772 		(err) = 0;						\
773 	}								\
774 	if ((err))							\
775 		(ifq)->ifq_drops++;					\
776 } while (/* CONSTCOND */0)
777 
778 #define	IFQ_DEQUEUE(ifq, m)	IF_DEQUEUE((ifq), (m))
779 
780 #define	IFQ_POLL(ifq, m)	IF_POLL((ifq), (m))
781 
782 #define	IFQ_PURGE(ifq)		IF_PURGE((ifq))
783 
784 #define	IFQ_SET_READY(ifq)	/* nothing */
785 
786 #endif /* ALTQ */
787 
788 #define	IFQ_LEN(ifq)			IF_LEN(ifq)
789 #define	IFQ_IS_EMPTY(ifq)		((ifq)->ifq_len == 0)
790 #define	IFQ_INC_LEN(ifq)		((ifq)->ifq_len++)
791 #define	IFQ_DEC_LEN(ifq)		(--(ifq)->ifq_len)
792 #define	IFQ_INC_DROPS(ifq)		((ifq)->ifq_drops++)
793 #define	IFQ_SET_MAXLEN(ifq, len)	((ifq)->ifq_maxlen = (len))
794 
795 /* default interface priorities */
796 #define IF_WIRED_DEFAULT_PRIORITY 0
797 #define IF_WIRELESS_DEFAULT_PRIORITY 4
798 
799 extern int ifqmaxlen;
800 extern struct ifnet_head ifnet;
801 extern struct ifnet *lo0ifp;
802 
803 #define	ether_input_mbuf(ifp, m)        ether_input((ifp), NULL, (m))
804 
805 void	ether_ifattach(struct ifnet *);
806 void	ether_ifdetach(struct ifnet *);
807 int	ether_ioctl(struct ifnet *, struct arpcom *, u_long, caddr_t);
808 void	ether_input(struct ifnet *, struct ether_header *, struct mbuf *);
809 int	ether_output(struct ifnet *,
810 	    struct mbuf *, struct sockaddr *, struct rtentry *);
811 char	*ether_sprintf(u_char *);
812 
813 void	if_alloc_sadl(struct ifnet *);
814 void	if_free_sadl(struct ifnet *);
815 void	if_attach(struct ifnet *);
816 void	if_attachdomain(void);
817 void	if_attachtail(struct ifnet *);
818 void	if_attachhead(struct ifnet *);
819 void	if_detach(struct ifnet *);
820 void	if_down(struct ifnet *);
821 void	if_downall(void);
822 void	if_link_state_change(struct ifnet *);
823 void	if_slowtimo(void *);
824 void	if_up(struct ifnet *);
825 int	ifconf(u_long, caddr_t);
826 void	ifinit(void);
827 int	ifioctl(struct socket *, u_long, caddr_t, struct proc *);
828 int	ifpromisc(struct ifnet *, int);
829 struct	ifg_group *if_creategroup(const char *);
830 int	if_addgroup(struct ifnet *, const char *);
831 int	if_delgroup(struct ifnet *, const char *);
832 void	if_group_routechange(struct sockaddr *, struct sockaddr *);
833 struct	ifnet *ifunit(const char *);
834 struct	ifnet *if_get(unsigned int);
835 void	if_start(struct ifnet *);
836 void	ifnewlladdr(struct ifnet *);
837 
838 struct	ifaddr *ifa_ifwithaddr(struct sockaddr *, u_int);
839 struct	ifaddr *ifa_ifwithdstaddr(struct sockaddr *, u_int);
840 struct	ifaddr *ifa_ifwithnet(struct sockaddr *, u_int);
841 struct	ifaddr *ifa_ifwithroute(int, struct sockaddr *,
842 					struct sockaddr *, u_int);
843 struct	ifaddr *ifaof_ifpforaddr(struct sockaddr *, struct ifnet *);
844 void	ifafree(struct ifaddr *);
845 void	link_rtrequest(int, struct rtentry *, struct rt_addrinfo *);
846 
847 void	if_clone_attach(struct if_clone *);
848 void	if_clone_detach(struct if_clone *);
849 
850 int	if_clone_create(const char *);
851 int	if_clone_destroy(const char *);
852 
853 void	if_congestion(struct ifqueue *);
854 int     sysctl_ifq(int *, u_int, void *, size_t *, void *, size_t,
855 	    struct ifqueue *);
856 
857 int	loioctl(struct ifnet *, u_long, caddr_t);
858 void	loopattach(int);
859 int	looutput(struct ifnet *,
860 	    struct mbuf *, struct sockaddr *, struct rtentry *);
861 void	lortrequest(int, struct rtentry *, struct rt_addrinfo *);
862 void	ifa_add(struct ifnet *, struct ifaddr *);
863 void	ifa_del(struct ifnet *, struct ifaddr *);
864 void	ifa_update_broadaddr(struct ifnet *, struct ifaddr *,
865 	    struct sockaddr *);
866 #endif /* _KERNEL */
867 
868 #endif /* __BSD_VISIBLE */
869 
870 #endif /* _NET_IF_H_ */
871