xref: /openbsd/sys/net/if.h (revision 6845379f)
1 /*	$OpenBSD: if.h,v 1.195 2018/09/12 09:20:34 krw 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/types.h>
63 #include <sys/time.h>
64 #include <sys/socket.h>
65 
66 /*
67  * Structure used to query names of interface cloners.
68  */
69 struct if_clonereq {
70 	int	ifcr_total;		/* total cloners (out) */
71 	int	ifcr_count;		/* room for this many in user buffer */
72 	char	*ifcr_buffer;		/* buffer for cloner names */
73 };
74 
75 #define MCLPOOLS	8		/* number of cluster pools */
76 
77 struct if_rxring {
78 	int     rxr_adjusted;
79 	u_int	rxr_alive;
80 	u_int	rxr_cwm;
81 	u_int	rxr_lwm;
82 	u_int	rxr_hwm;
83 };
84 
85 struct if_rxring_info {
86 	char	ifr_name[16];		/* name of the ring */
87 	u_int	ifr_size;		/* size of the packets on the ring */
88 	struct if_rxring ifr_info;
89 };
90 
91 /* Structure used in SIOCGIFRXR request. */
92 struct if_rxrinfo {
93 	u_int	ifri_total;
94 	struct if_rxring_info *ifri_entries;
95 };
96 
97 /*
98  * Structure defining statistics and other data kept regarding a network
99  * interface.
100  */
101 struct	if_data {
102 	/* generic interface information */
103 	u_char		ifi_type;		/* ethernet, tokenring, etc. */
104 	u_char		ifi_addrlen;		/* media address length */
105 	u_char		ifi_hdrlen;		/* media header length */
106 	u_char		ifi_link_state;		/* current link state */
107 	u_int32_t	ifi_mtu;		/* maximum transmission unit */
108 	u_int32_t	ifi_metric;		/* routing metric (external only) */
109 	u_int32_t	ifi_rdomain;		/* routing instance */
110 	u_int64_t	ifi_baudrate;		/* linespeed */
111 	/* volatile statistics */
112 	u_int64_t	ifi_ipackets;		/* packets received on interface */
113 	u_int64_t	ifi_ierrors;		/* input errors on interface */
114 	u_int64_t	ifi_opackets;		/* packets sent on interface */
115 	u_int64_t	ifi_oerrors;		/* output errors on interface */
116 	u_int64_t	ifi_collisions;		/* collisions on csma interfaces */
117 	u_int64_t	ifi_ibytes;		/* total number of octets received */
118 	u_int64_t	ifi_obytes;		/* total number of octets sent */
119 	u_int64_t	ifi_imcasts;		/* packets received via multicast */
120 	u_int64_t	ifi_omcasts;		/* packets sent via multicast */
121 	u_int64_t	ifi_iqdrops;		/* dropped on input, this interface */
122 	u_int64_t	ifi_oqdrops;		/* dropped on output, this interface */
123 	u_int64_t	ifi_noproto;		/* destined for unsupported protocol */
124 	u_int32_t	ifi_capabilities;	/* interface capabilities */
125 	struct	timeval ifi_lastchange;	/* last operational state change */
126 };
127 
128 #define IFQ_NQUEUES	8
129 #define IFQ_MINPRIO	0
130 #define IFQ_MAXPRIO	IFQ_NQUEUES - 1
131 #define IFQ_DEFPRIO	3
132 
133 /*
134  * Values for if_link_state.
135  */
136 #define LINK_STATE_UNKNOWN	0	/* link unknown */
137 #define LINK_STATE_INVALID	1	/* link invalid */
138 #define LINK_STATE_DOWN		2	/* link is down */
139 #define LINK_STATE_KALIVE_DOWN	3	/* keepalive reports down */
140 #define LINK_STATE_UP		4	/* link is up */
141 #define LINK_STATE_HALF_DUPLEX	5	/* link is up and half duplex */
142 #define LINK_STATE_FULL_DUPLEX	6	/* link is up and full duplex */
143 
144 #define LINK_STATE_IS_UP(_s)	\
145 		((_s) >= LINK_STATE_UP || (_s) == LINK_STATE_UNKNOWN)
146 
147 /*
148  * Status bit descriptions for the various interface types.
149  */
150 struct if_status_description {
151 	u_char	ifs_type;
152 	u_char	ifs_state;
153 	const char *ifs_string;
154 };
155 
156 #define LINK_STATE_DESC_MATCH(_ifs, _t, _s)				\
157 	(((_ifs)->ifs_type == (_t) || (_ifs)->ifs_type == 0) &&		\
158 	    (_ifs)->ifs_state == (_s))
159 
160 
161 #define LINK_STATE_DESCRIPTIONS {					\
162 	{ IFT_ETHER, LINK_STATE_DOWN, "no carrier" },			\
163 									\
164 	{ IFT_IEEE80211, LINK_STATE_DOWN, "no network" },		\
165 									\
166 	{ IFT_PPP, LINK_STATE_DOWN, "no carrier" },			\
167 									\
168 	{ IFT_CARP, LINK_STATE_DOWN, "backup" },			\
169 	{ IFT_CARP, LINK_STATE_UP, "master" },				\
170 	{ IFT_CARP, LINK_STATE_HALF_DUPLEX, "master" },			\
171 	{ IFT_CARP, LINK_STATE_FULL_DUPLEX, "master" },			\
172 									\
173 	{ 0, LINK_STATE_UP, "active" },					\
174 	{ 0, LINK_STATE_HALF_DUPLEX, "active" },			\
175 	{ 0, LINK_STATE_FULL_DUPLEX, "active" },			\
176 									\
177 	{ 0, LINK_STATE_UNKNOWN, "unknown" },				\
178 	{ 0, LINK_STATE_INVALID, "invalid" },				\
179 	{ 0, LINK_STATE_DOWN, "down" },					\
180 	{ 0, LINK_STATE_KALIVE_DOWN, "keepalive down" },		\
181 	{ 0, 0, NULL }							\
182 }
183 
184 /* Traditional BSD name for length of interface external name. */
185 #define	IFNAMSIZ	IF_NAMESIZE
186 
187 /*
188  * Length of interface description, including terminating '\0'.
189  */
190 #define	IFDESCRSIZE	64
191 
192 /*
193  * Interface flags can be either owned by the stack or the driver.  The
194  * symbols below document who is toggling which flag.
195  *
196  *	I	immutable after creation
197  *	N	written by the stack (upon user request)
198  *	d	written by the driver
199  *	c	for userland compatibility only
200  */
201 #define	IFF_UP		0x1		/* [N] interface is up */
202 #define	IFF_BROADCAST	0x2		/* [I] broadcast address valid */
203 #define	IFF_DEBUG	0x4		/* [N] turn on debugging */
204 #define	IFF_LOOPBACK	0x8		/* [I] is a loopback net */
205 #define	IFF_POINTOPOINT	0x10		/* [I] is point-to-point link */
206 #define	IFF_STATICARP	0x20		/* [N] only static ARP */
207 #define	IFF_RUNNING	0x40		/* [d] resources allocated */
208 #define	IFF_NOARP	0x80		/* [N] no address resolution protocol */
209 #define	IFF_PROMISC	0x100		/* [N] receive all packets */
210 #define	IFF_ALLMULTI	0x200		/* [d] receive all multicast packets */
211 #define	IFF_OACTIVE	0x400		/* [c] transmission in progress */
212 #define	IFF_SIMPLEX	0x800		/* [I] can't hear own transmissions */
213 #define	IFF_LINK0	0x1000		/* [N] per link layer defined bit */
214 #define	IFF_LINK1	0x2000		/* [N] per link layer defined bit */
215 #define	IFF_LINK2	0x4000		/* [N] per link layer defined bit */
216 #define	IFF_MULTICAST	0x8000		/* [I] supports multicast */
217 
218 /* flags set internally only: */
219 #define	IFF_CANTCHANGE \
220 	(IFF_BROADCAST|IFF_POINTOPOINT|IFF_RUNNING|IFF_OACTIVE|\
221 	    IFF_SIMPLEX|IFF_MULTICAST|IFF_ALLMULTI)
222 
223 #define	IFXF_MPSAFE		0x1	/* [I] if_start is mpsafe */
224 #define	IFXF_CLONED		0x2	/* [I] pseudo interface */
225 #define	IFXF_INET6_NOPRIVACY	0x4	/* [N] don't autoconf privacy */
226 #define	IFXF_MPLS		0x8	/* [N] supports MPLS */
227 #define	IFXF_WOL		0x10	/* [N] wake on lan enabled */
228 #define	IFXF_AUTOCONF6		0x20	/* [N] v6 autoconf enabled */
229 #define IFXF_INET6_NOSOII	0x40	/* [N] don't do RFC 7217 */
230 
231 #define	IFXF_CANTCHANGE \
232 	(IFXF_MPSAFE|IFXF_CLONED)
233 
234 /*
235  * Some convenience macros used for setting ifi_baudrate.
236  */
237 #define	IF_Kbps(x)	((x) * 1000ULL)			/* kilobits/sec. */
238 #define	IF_Mbps(x)	(IF_Kbps((x) * 1000ULL))	/* megabits/sec. */
239 #define	IF_Gbps(x)	(IF_Mbps((x) * 1000ULL))	/* gigabits/sec. */
240 
241 /* Capabilities that interfaces can advertise. */
242 #define	IFCAP_CSUM_IPv4		0x00000001	/* can do IPv4 header csum */
243 #define	IFCAP_CSUM_TCPv4	0x00000002	/* can do IPv4/TCP csum */
244 #define	IFCAP_CSUM_UDPv4	0x00000004	/* can do IPv4/UDP csum */
245 #define	IFCAP_VLAN_MTU		0x00000010	/* VLAN-compatible MTU */
246 #define	IFCAP_VLAN_HWTAGGING	0x00000020	/* hardware VLAN tag support */
247 #define	IFCAP_CSUM_TCPv6	0x00000080	/* can do IPv6/TCP checksums */
248 #define	IFCAP_CSUM_UDPv6	0x00000100	/* can do IPv6/UDP checksums */
249 #define	IFCAP_WOL		0x00008000	/* can do wake on lan */
250 
251 #define IFCAP_CSUM_MASK		(IFCAP_CSUM_IPv4 | IFCAP_CSUM_TCPv4 | \
252     IFCAP_CSUM_UDPv4 | IFCAP_CSUM_TCPv6 | IFCAP_CSUM_UDPv6)
253 
254 /* symbolic names for terminal (per-protocol) CTL_IFQ_ nodes */
255 #define IFQCTL_LEN 1
256 #define IFQCTL_MAXLEN 2
257 #define IFQCTL_DROPS 3
258 #define IFQCTL_CONGESTION 4
259 #define IFQCTL_MAXID 5
260 
261 /* sysctl for ifq (per-protocol packet input queue variant of ifqueue) */
262 #define CTL_IFQ_NAMES  { \
263 	{ 0, 0 }, \
264 	{ "len", CTLTYPE_INT }, \
265 	{ "maxlen", CTLTYPE_INT }, \
266 	{ "drops", CTLTYPE_INT }, \
267 	{ "congestion", CTLTYPE_INT }, \
268 }
269 
270 /*
271  * Message format for use in obtaining information about interfaces
272  * from sysctl and the routing socket.
273  */
274 struct if_msghdr {
275 	u_short	ifm_msglen;	/* to skip over non-understood messages */
276 	u_char	ifm_version;	/* future binary compatibility */
277 	u_char	ifm_type;	/* message type */
278 	u_short ifm_hdrlen;	/* sizeof(if_msghdr) to skip over the header */
279 	u_short	ifm_index;	/* index for associated ifp */
280 	u_short	ifm_tableid;	/* routing table id */
281 	u_char	ifm_pad1;
282 	u_char	ifm_pad2;
283 	int	ifm_addrs;	/* like rtm_addrs */
284 	int	ifm_flags;	/* value of if_flags */
285 	int	ifm_xflags;
286 	struct	if_data ifm_data;/* statistics and other data about if */
287 };
288 
289 /*
290  * Message format for use in obtaining information about interface addresses
291  * from sysctl and the routing socket.
292  */
293 struct ifa_msghdr {
294 	u_short	ifam_msglen;	/* to skip over non-understood messages */
295 	u_char	ifam_version;	/* future binary compatibility */
296 	u_char	ifam_type;	/* message type */
297 	u_short ifam_hdrlen;	/* sizeof(ifa_msghdr) to skip over the header */
298 	u_short	ifam_index;	/* index for associated ifp */
299 	u_short	ifam_tableid;	/* routing table id */
300 	u_char	ifam_pad1;
301 	u_char	ifam_pad2;
302 	int	ifam_addrs;	/* like rtm_addrs */
303 	int	ifam_flags;	/* value of ifa_flags */
304 	int	ifam_metric;	/* value of ifa_metric */
305 };
306 
307 /*
308  * Message format announcing the arrival or departure of a network interface.
309  */
310 struct if_announcemsghdr {
311 	u_short	ifan_msglen;	/* to skip over non-understood messages */
312 	u_char	ifan_version;	/* future binary compatibility */
313 	u_char	ifan_type;	/* message type */
314 	u_short ifan_hdrlen;	/* sizeof(if_announcemsghdr) to skip header */
315 	u_short	ifan_index;	/* index for associated ifp */
316 	u_short	ifan_what;	/* what type of announcement */
317 	char	ifan_name[IFNAMSIZ];	/* if name, e.g. "en0" */
318 };
319 
320 #define IFAN_ARRIVAL	0	/* interface arrival */
321 #define IFAN_DEPARTURE	1	/* interface departure */
322 
323 /* message format used to pass interface name to index mappings */
324 struct if_nameindex_msg {
325 	unsigned int	if_index;
326 	char		if_name[IFNAMSIZ];
327 };
328 
329 /*
330  * interface groups
331  */
332 
333 #define	IFG_ALL		"all"		/* group contains all interfaces */
334 #define	IFG_EGRESS	"egress"	/* if(s) default route(s) point to */
335 
336 struct ifg_req {
337 	union {
338 		char			 ifgrqu_group[IFNAMSIZ];
339 		char			 ifgrqu_member[IFNAMSIZ];
340 	} ifgrq_ifgrqu;
341 #define	ifgrq_group	ifgrq_ifgrqu.ifgrqu_group
342 #define	ifgrq_member	ifgrq_ifgrqu.ifgrqu_member
343 };
344 
345 struct ifg_attrib {
346 	int	ifg_carp_demoted;
347 };
348 
349 /*
350  * Used to lookup groups for an interface
351  */
352 struct ifgroupreq {
353 	char	ifgr_name[IFNAMSIZ];
354 	u_int	ifgr_len;
355 	union {
356 		char			 ifgru_group[IFNAMSIZ];
357 		struct	ifg_req		*ifgru_groups;
358 		struct	ifg_attrib	 ifgru_attrib;
359 	} ifgr_ifgru;
360 #define ifgr_group	ifgr_ifgru.ifgru_group
361 #define ifgr_groups	ifgr_ifgru.ifgru_groups
362 #define ifgr_attrib	ifgr_ifgru.ifgru_attrib
363 };
364 
365 /*
366  * Interface request structure used for socket
367  * ioctl's.  All interface ioctl's must have parameter
368  * definitions which begin with ifr_name.  The
369  * remainder may be interface specific.
370  */
371 struct	ifreq {
372 	char	ifr_name[IFNAMSIZ];		/* if name, e.g. "en0" */
373 	union {
374 		struct	sockaddr	ifru_addr;
375 		struct	sockaddr	ifru_dstaddr;
376 		struct	sockaddr	ifru_broadaddr;
377 		short			ifru_flags;
378 		int			ifru_metric;
379 		int64_t			ifru_vnetid;
380 		uint64_t		ifru_media;
381 		caddr_t			ifru_data;
382 		unsigned int		ifru_index;
383 	} ifr_ifru;
384 #define	ifr_addr	ifr_ifru.ifru_addr	/* address */
385 #define	ifr_dstaddr	ifr_ifru.ifru_dstaddr	/* other end of p-to-p link */
386 #define	ifr_broadaddr	ifr_ifru.ifru_broadaddr	/* broadcast address */
387 #define	ifr_flags	ifr_ifru.ifru_flags	/* flags */
388 #define	ifr_metric	ifr_ifru.ifru_metric	/* metric */
389 #define	ifr_mtu		ifr_ifru.ifru_metric	/* mtu (overload) */
390 #define	ifr_hardmtu	ifr_ifru.ifru_metric	/* hardmtu (overload) */
391 #define	ifr_media	ifr_ifru.ifru_media	/* media options */
392 #define	ifr_rdomainid	ifr_ifru.ifru_metric	/* VRF instance (overload) */
393 #define ifr_vnetid	ifr_ifru.ifru_vnetid	/* Virtual Net Id */
394 #define ifr_ttl		ifr_ifru.ifru_metric	/* tunnel TTL (overload) */
395 #define ifr_df		ifr_ifru.ifru_metric	/* tunnel DF (overload) */
396 #define	ifr_data	ifr_ifru.ifru_data	/* for use by interface */
397 #define ifr_index	ifr_ifru.ifru_index	/* interface index */
398 #define ifr_llprio	ifr_ifru.ifru_metric	/* link layer priority */
399 };
400 
401 struct ifaliasreq {
402 	char	ifra_name[IFNAMSIZ];		/* if name, e.g. "en0" */
403 	union {
404 		struct	sockaddr ifrau_addr;
405 		int	ifrau_align;
406 	 } ifra_ifrau;
407 #ifndef ifra_addr
408 #define ifra_addr	ifra_ifrau.ifrau_addr
409 #endif
410 	struct	sockaddr ifra_dstaddr;
411 #define	ifra_broadaddr	ifra_dstaddr
412 	struct	sockaddr ifra_mask;
413 };
414 
415 struct ifmediareq {
416 	char		ifm_name[IFNAMSIZ];	/* if name, e.g. "en0" */
417 	uint64_t	ifm_current;		/* get/set current media options */
418 	uint64_t	ifm_mask;		/* don't care mask */
419 	uint64_t	ifm_status;		/* media status */
420 	uint64_t	ifm_active;		/* active options */
421 	int		ifm_count;		/* # entries in ifm_ulist array */
422 	uint64_t	*ifm_ulist;		/* media words */
423 };
424 
425 struct ifkalivereq {
426 	char	ikar_name[IFNAMSIZ];		/* if name, e.g. "en0" */
427 	int	ikar_timeo;
428 	int	ikar_cnt;
429 };
430 
431 /*
432  * Structure used in SIOCGIFCONF request.
433  * Used to retrieve interface configuration
434  * for machine (useful for programs which
435  * must know all networks accessible).
436  */
437 struct	ifconf {
438 	int	ifc_len;		/* size of associated buffer */
439 	union {
440 		caddr_t	ifcu_buf;
441 		struct	ifreq *ifcu_req;
442 	} ifc_ifcu;
443 #define	ifc_buf	ifc_ifcu.ifcu_buf	/* buffer address */
444 #define	ifc_req	ifc_ifcu.ifcu_req	/* array of structures returned */
445 };
446 
447 /*
448  * Structure for SIOC[AGD]LIFADDR
449  */
450 struct if_laddrreq {
451 	char iflr_name[IFNAMSIZ];
452 	unsigned int flags;
453 #define IFLR_PREFIX	0x8000	/* in: prefix given  out: kernel fills id */
454 	unsigned int prefixlen;		/* in/out */
455 	struct sockaddr_storage addr;	/* in/out */
456 	struct sockaddr_storage dstaddr; /* out */
457 };
458 
459 /* SIOCIFAFDETACH */
460 struct if_afreq {
461 	char		ifar_name[IFNAMSIZ];
462 	sa_family_t	ifar_af;
463 };
464 
465 /* SIOC[SG]IFPARENT */
466 struct if_parent {
467 	char		ifp_name[IFNAMSIZ];
468 	char		ifp_parent[IFNAMSIZ];
469 };
470 
471 #include <net/if_arp.h>
472 
473 #ifdef _KERNEL
474 struct socket;
475 struct ifnet;
476 struct ifq_ops;
477 
478 void	if_alloc_sadl(struct ifnet *);
479 void	if_free_sadl(struct ifnet *);
480 void	if_attach(struct ifnet *);
481 void	if_attach_queues(struct ifnet *, unsigned int);
482 void	if_attach_iqueues(struct ifnet *, unsigned int);
483 void	if_attach_ifq(struct ifnet *, const struct ifq_ops *, void *);
484 void	if_attachtail(struct ifnet *);
485 void	if_attachhead(struct ifnet *);
486 void	if_deactivate(struct ifnet *);
487 void	if_detach(struct ifnet *);
488 void	if_down(struct ifnet *);
489 void	if_downall(void);
490 void	if_link_state_change(struct ifnet *);
491 void	if_up(struct ifnet *);
492 void	if_getdata(struct ifnet *, struct if_data *);
493 void	ifinit(void);
494 int	ifioctl(struct socket *, u_long, caddr_t, struct proc *);
495 int	ifpromisc(struct ifnet *, int);
496 struct	ifg_group *if_creategroup(const char *);
497 int	if_addgroup(struct ifnet *, const char *);
498 int	if_delgroup(struct ifnet *, const char *);
499 void	if_group_routechange(struct sockaddr *, struct sockaddr *);
500 struct	ifnet *ifunit(const char *);
501 struct	ifnet *if_get(unsigned int);
502 void	if_put(struct ifnet *);
503 void	ifnewlladdr(struct ifnet *);
504 void	if_congestion(void);
505 int	if_congested(void);
506 __dead void	unhandled_af(int);
507 int	if_setlladdr(struct ifnet *, const uint8_t *);
508 struct taskq * net_tq(unsigned int);
509 
510 #endif /* _KERNEL */
511 
512 #endif /* __BSD_VISIBLE */
513 
514 #endif /* _NET_IF_H_ */
515