if.h revision 148894
1138568Ssam/*-
2186904Ssam * Copyright (c) 1982, 1986, 1989, 1993
3138568Ssam *	The Regents of the University of California.  All rights reserved.
4138568Ssam *
5138568Ssam * Redistribution and use in source and binary forms, with or without
6138568Ssam * modification, are permitted provided that the following conditions
7138568Ssam * are met:
8138568Ssam * 1. Redistributions of source code must retain the above copyright
9138568Ssam *    notice, this list of conditions and the following disclaimer.
10138568Ssam * 2. Redistributions in binary form must reproduce the above copyright
11138568Ssam *    notice, this list of conditions and the following disclaimer in the
12138568Ssam *    documentation and/or other materials provided with the distribution.
13138568Ssam * 4. Neither the name of the University nor the names of its contributors
14138568Ssam *    may be used to endorse or promote products derived from this software
15138568Ssam *    without specific prior written permission.
16138568Ssam *
17138568Ssam * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18138568Ssam * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19138568Ssam * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20138568Ssam * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21138568Ssam * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22138568Ssam * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23138568Ssam * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24138568Ssam * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25138568Ssam * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26138568Ssam * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27138568Ssam * SUCH DAMAGE.
28138568Ssam *
29138568Ssam *	@(#)if.h	8.1 (Berkeley) 6/10/93
30138568Ssam * $FreeBSD: head/sys/net/if.h 148894 2005-08-09 12:56:20Z rwatson $
31138568Ssam */
32178354Ssam
33178354Ssam#ifndef _NET_IF_H_
34138568Ssam#define	_NET_IF_H_
35138568Ssam
36138568Ssam#include <sys/cdefs.h>
37138568Ssam
38138568Ssam#ifdef _KERNEL
39138568Ssam#include <sys/queue.h>
40138568Ssam#endif
41138568Ssam
42138568Ssam#if __BSD_VISIBLE
43138568Ssam/*
44138568Ssam * <net/if.h> does not depend on <sys/time.h> on most other systems.  This
45192468Ssam * helps userland compatibility.  (struct timeval ifi_lastchange)
46138568Ssam */
47190526Ssam#ifndef _KERNEL
48178354Ssam#include <sys/time.h>
49138568Ssam#endif
50178354Ssam
51138568Ssamstruct ifnet;
52138568Ssam#endif
53196019Srwatson
54138568Ssam/*
55138568Ssam * Length of interface external name, including terminating '\0'.
56195757Ssam * Note: this is the same size as a generic device's external name.
57138568Ssam */
58138568Ssam#define		IF_NAMESIZE	16
59138568Ssam#if __BSD_VISIBLE
60138568Ssam#define		IFNAMSIZ	IF_NAMESIZE
61138568Ssam#define		IF_MAXUNIT	0x7fff	/* historical value */
62138568Ssam#endif
63138568Ssam#if __BSD_VISIBLE
64138568Ssam
65138568Ssam/*
66178354Ssam * Structure used to query names of interface cloners.
67178354Ssam */
68178354Ssam
69178354Ssamstruct if_clonereq {
70178354Ssam	int	ifcr_total;		/* total cloners (out) */
71178354Ssam	int	ifcr_count;		/* room for this many in user buffer */
72178354Ssam	char	*ifcr_buffer;		/* buffer for cloner names */
73178354Ssam};
74178354Ssam
75178354Ssam/*
76178354Ssam * Structure describing information about an interface
77178354Ssam * which may be of interest to management entities.
78178354Ssam */
79178354Ssamstruct if_data {
80178354Ssam	/* generic interface information */
81178354Ssam	u_char	ifi_type;		/* ethernet, tokenring, etc */
82178354Ssam	u_char	ifi_physical;		/* e.g., AUI, Thinnet, 10base-T, etc */
83178354Ssam	u_char	ifi_addrlen;		/* media address length */
84178354Ssam	u_char	ifi_hdrlen;		/* media header length */
85178354Ssam	u_char	ifi_link_state;		/* current link state */
86178354Ssam	u_char	ifi_recvquota;		/* polling quota for receive intrs */
87178354Ssam	u_char	ifi_xmitquota;		/* polling quota for xmit intrs */
88178354Ssam	u_char	ifi_datalen;		/* length of this data struct */
89178354Ssam	u_long	ifi_mtu;		/* maximum transmission unit */
90178354Ssam	u_long	ifi_metric;		/* routing metric (external only) */
91138568Ssam	u_long	ifi_baudrate;		/* linespeed */
92178354Ssam	/* volatile statistics */
93173273Ssam	u_long	ifi_ipackets;		/* packets received on interface */
94178354Ssam	u_long	ifi_ierrors;		/* input errors on interface */
95178354Ssam	u_long	ifi_opackets;		/* packets sent on interface */
96178354Ssam	u_long	ifi_oerrors;		/* output errors on interface */
97178354Ssam	u_long	ifi_collisions;		/* collisions on csma interfaces */
98173273Ssam	u_long	ifi_ibytes;		/* total number of octets received */
99173273Ssam	u_long	ifi_obytes;		/* total number of octets sent */
100178354Ssam	u_long	ifi_imcasts;		/* packets received via multicast */
101178354Ssam	u_long	ifi_omcasts;		/* packets sent via multicast */
102178354Ssam	u_long	ifi_iqdrops;		/* dropped on input, this interface */
103178354Ssam	u_long	ifi_noproto;		/* destined for unsupported protocol */
104178354Ssam	u_long	ifi_hwassist;		/* HW offload capabilities */
105178354Ssam	time_t	ifi_epoch;		/* uptime at attach or stat reset */
106178957Ssam#ifdef __alpha__
107178354Ssam	u_int	ifi_timepad;		/* time_t is int, not long on alpha */
108178354Ssam#endif
109178957Ssam	struct	timeval ifi_lastchange;	/* time of last administrative change */
110178957Ssam};
111178957Ssam
112178957Ssam/*-
113178957Ssam * Interface flags are of two types: network stack owned flags, and driver
114178354Ssam * owned flags.  Historically, these values were stored in the same ifnet
115178354Ssam * flags field, but with the advent of fine-grained locking, they have been
116178354Ssam * broken out such that the network stack is responsible for synchronizing
117178957Ssam * the stack-owned fields, and the device driver the device-owned fields.
118178957Ssam * Both halves can perform lockless reads of the other half's field, subject
119178957Ssam * to accepting the involved races.
120178957Ssam *
121178957Ssam * Both sets of flags come from the same number space, and should not be
122186904Ssam * permitted to conflict, as they are exposed to user space via a single
123186904Ssam * field.
124186904Ssam *
125186904Ssam * The following symbols identify read and write requirements for fields:
126186904Ssam *
127186904Ssam * (i) if_flags field set by device driver before attach, read-only there
128186904Ssam *     after.
129186904Ssam * (n) if_flags field written only by the network stack, read by either the
130186904Ssam *     stack or driver.
131186904Ssam * (d) if_drv_flags field written only by the device driver, read by either
132178354Ssam *     the stack or driver.
133178354Ssam */
134178354Ssam#define	IFF_UP		0x1		/* (n) interface is up */
135190526Ssam#define	IFF_BROADCAST	0x2		/* (i) broadcast address valid */
136178354Ssam#define	IFF_DEBUG	0x4		/* (n) turn on debugging */
137178354Ssam#define	IFF_LOOPBACK	0x8		/* (i) is a loopback net */
138178354Ssam#define	IFF_POINTOPOINT	0x10		/* (i) is a point-to-point link */
139178354Ssam#define	IFF_SMART	0x20		/* (i) interface manages own routes */
140178354Ssam#define	IFF_DRV_RUNNING	0x40		/* (d) resources allocated */
141178354Ssam#define	IFF_NOARP	0x80		/* (n) no address resolution protocol */
142178354Ssam#define	IFF_PROMISC	0x100		/* (n) receive all packets */
143178354Ssam#define	IFF_ALLMULTI	0x200		/* (n) receive all multicast packets */
144178354Ssam#define	IFF_DRV_OACTIVE	0x400		/* (d) tx hardware queue is full */
145178354Ssam#define	IFF_SIMPLEX	0x800		/* (i) can't hear own transmissions */
146178354Ssam#define	IFF_LINK0	0x1000		/* per link layer defined bit */
147178354Ssam#define	IFF_LINK1	0x2000		/* per link layer defined bit */
148178354Ssam#define	IFF_LINK2	0x4000		/* per link layer defined bit */
149178354Ssam#define	IFF_ALTPHYS	IFF_LINK2	/* use alternate physical connection */
150178354Ssam#define	IFF_MULTICAST	0x8000		/* (i) supports multicast */
151178354Ssam#define	IFF_POLLING	0x10000		/* (n) Interface is in polling mode. */
152183210Sthompsa#define	IFF_PPROMISC	0x20000		/* (n) user-requested promisc mode */
153178354Ssam#define	IFF_MONITOR	0x40000		/* (n) user-requested monitor mode */
154178354Ssam#define	IFF_STATICARP	0x80000		/* (n) static ARP */
155192473Ssam#define	IFF_NEEDSGIANT	0x100000	/* (i) hold Giant over if_start calls */
156178354Ssam
157178354Ssam/*
158178354Ssam * Old names for driver flags so that user space tools can continue to use
159178354Ssam * the old (portable) names.
160178354Ssam */
161178354Ssam#ifndef _KERNEL
162173273Ssam#define	IFF_RUNNING	IFF_DRV_RUNNING
163173273Ssam#define	IFF_OACTIVE	IFF_DRV_OACTIVE
164178354Ssam#endif
165178354Ssam
166173273Ssam/* flags set internally only: */
167173273Ssam#define	IFF_CANTCHANGE \
168178354Ssam	(IFF_BROADCAST|IFF_POINTOPOINT|IFF_DRV_RUNNING|IFF_DRV_OACTIVE|\
169173273Ssam	    IFF_SIMPLEX|IFF_MULTICAST|IFF_ALLMULTI|IFF_SMART|IFF_PROMISC|\
170138568Ssam	    IFF_POLLING)
171138568Ssam
172138568Ssam/*
173138568Ssam * Values for if_link_state.
174138568Ssam */
175138568Ssam#define	LINK_STATE_UNKNOWN	0	/* link invalid/unknown */
176138568Ssam#define	LINK_STATE_DOWN		1	/* link is down */
177138568Ssam#define	LINK_STATE_UP		2	/* link is up */
178138568Ssam
179138568Ssam/*
180138568Ssam * Some convenience macros used for setting ifi_baudrate.
181138568Ssam * XXX 1000 vs. 1024? --thorpej@netbsd.org
182138568Ssam */
183138568Ssam#define	IF_Kbps(x)	((x) * 1000)		/* kilobits/sec. */
184138568Ssam#define	IF_Mbps(x)	(IF_Kbps((x) * 1000))	/* megabits/sec. */
185138568Ssam#define	IF_Gbps(x)	(IF_Mbps((x) * 1000))	/* gigabits/sec. */
186138568Ssam
187138568Ssam/* Capabilities that interfaces can advertise. */
188138568Ssam#define IFCAP_RXCSUM		0x0001  /* can offload checksum on RX */
189138568Ssam#define IFCAP_TXCSUM		0x0002  /* can offload checksum on TX */
190138568Ssam#define IFCAP_NETCONS		0x0004  /* can be a network console */
191181194Ssam#define	IFCAP_VLAN_MTU		0x0008	/* VLAN-compatible MTU */
192181194Ssam#define	IFCAP_VLAN_HWTAGGING	0x0010	/* hardware VLAN tag support */
193181194Ssam#define	IFCAP_JUMBO_MTU		0x0020	/* 9000 byte MTU supported */
194181194Ssam#define	IFCAP_POLLING		0x0040	/* driver supports polling */
195181194Ssam
196181194Ssam#define IFCAP_HWCSUM		(IFCAP_RXCSUM | IFCAP_TXCSUM)
197181194Ssam
198181194Ssam#define	IFQ_MAXLEN	50
199181194Ssam#define	IFNET_SLOWHZ	1		/* granularity is 1 second */
200181194Ssam
201181194Ssam/*
202181194Ssam * Message format for use in obtaining information about interfaces
203181194Ssam * from getkerninfo and the routing socket
204181194Ssam */
205181194Ssamstruct if_msghdr {
206138568Ssam	u_short	ifm_msglen;	/* to skip over non-understood messages */
207138568Ssam	u_char	ifm_version;	/* future binary compatibility */
208138568Ssam	u_char	ifm_type;	/* message type */
209178354Ssam	int	ifm_addrs;	/* like rtm_addrs */
210178354Ssam	int	ifm_flags;	/* value of if_flags */
211178354Ssam	u_short	ifm_index;	/* index for associated ifp */
212178354Ssam	struct	if_data ifm_data;/* statistics and other data about if */
213178354Ssam};
214178354Ssam
215178354Ssam/*
216178354Ssam * Message format for use in obtaining information about interface addresses
217178354Ssam * from getkerninfo and the routing socket
218178354Ssam */
219178354Ssamstruct ifa_msghdr {
220138568Ssam	u_short	ifam_msglen;	/* to skip over non-understood messages */
221138568Ssam	u_char	ifam_version;	/* future binary compatibility */
222138568Ssam	u_char	ifam_type;	/* message type */
223138568Ssam	int	ifam_addrs;	/* like rtm_addrs */
224186302Ssam	int	ifam_flags;	/* value of ifa_flags */
225138568Ssam	u_short	ifam_index;	/* index for associated ifp */
226138568Ssam	int	ifam_metric;	/* value of ifa_metric */
227178354Ssam};
228138568Ssam
229138568Ssam/*
230138568Ssam * Message format for use in obtaining information about multicast addresses
231138568Ssam * from the routing socket
232178354Ssam */
233138568Ssamstruct ifma_msghdr {
234138568Ssam	u_short	ifmam_msglen;	/* to skip over non-understood messages */
235138568Ssam	u_char	ifmam_version;	/* future binary compatibility */
236178354Ssam	u_char	ifmam_type;	/* message type */
237178354Ssam	int	ifmam_addrs;	/* like rtm_addrs */
238178354Ssam	int	ifmam_flags;	/* value of ifa_flags */
239178354Ssam	u_short	ifmam_index;	/* index for associated ifp */
240178354Ssam};
241138568Ssam
242178354Ssam/*
243138568Ssam * Message format announcing the arrival or departure of a network interface.
244178354Ssam */
245138568Ssamstruct if_announcemsghdr {
246138568Ssam	u_short	ifan_msglen;	/* to skip over non-understood messages */
247178354Ssam	u_char	ifan_version;	/* future binary compatibility */
248178354Ssam	u_char	ifan_type;	/* message type */
249178354Ssam	u_short	ifan_index;	/* index for associated ifp */
250138568Ssam	char	ifan_name[IFNAMSIZ]; /* if name, e.g. "en0" */
251138568Ssam	u_short	ifan_what;	/* what type of announcement */
252178354Ssam};
253138568Ssam
254138568Ssam#define	IFAN_ARRIVAL	0	/* interface arrival */
255138568Ssam#define	IFAN_DEPARTURE	1	/* interface departure */
256178354Ssam
257138568Ssam/*
258138568Ssam * Interface request structure used for socket
259138568Ssam * ioctl's.  All interface ioctl's must have parameter
260178354Ssam * definitions which begin with ifr_name.  The
261138568Ssam * remainder may be interface specific.
262138568Ssam */
263138568Ssamstruct	ifreq {
264178354Ssam	char	ifr_name[IFNAMSIZ];		/* if name, e.g. "en0" */
265138568Ssam	union {
266138568Ssam		struct	sockaddr ifru_addr;
267178354Ssam		struct	sockaddr ifru_dstaddr;
268178354Ssam		struct	sockaddr ifru_broadaddr;
269178354Ssam		short	ifru_flags[2];
270178354Ssam		short	ifru_index;
271178354Ssam		int	ifru_metric;
272178354Ssam		int	ifru_mtu;
273178354Ssam		int	ifru_phys;
274178354Ssam		int	ifru_media;
275178354Ssam		caddr_t	ifru_data;
276178354Ssam		int	ifru_cap[2];
277178354Ssam	} ifr_ifru;
278178354Ssam#define	ifr_addr	ifr_ifru.ifru_addr	/* address */
279178354Ssam#define	ifr_dstaddr	ifr_ifru.ifru_dstaddr	/* other end of p-to-p link */
280178354Ssam#define	ifr_broadaddr	ifr_ifru.ifru_broadaddr	/* broadcast address */
281178354Ssam#define	ifr_flags	ifr_ifru.ifru_flags[0]	/* flags (low 16 bits) */
282178354Ssam#define	ifr_flagshigh	ifr_ifru.ifru_flags[1]	/* flags (high 16 bits) */
283178354Ssam#define	ifr_metric	ifr_ifru.ifru_metric	/* metric */
284178354Ssam#define	ifr_mtu		ifr_ifru.ifru_mtu	/* mtu */
285181194Ssam#define ifr_phys	ifr_ifru.ifru_phys	/* physical wire */
286181194Ssam#define ifr_media	ifr_ifru.ifru_media	/* physical media */
287181194Ssam#define	ifr_data	ifr_ifru.ifru_data	/* for use by interface */
288193115Ssam#define	ifr_reqcap	ifr_ifru.ifru_cap[0]	/* requested capabilities */
289181194Ssam#define	ifr_curcap	ifr_ifru.ifru_cap[1]	/* current capabilities */
290178354Ssam#define	ifr_index	ifr_ifru.ifru_index	/* interface index */
291178354Ssam};
292138568Ssam
293138568Ssam#define	_SIZEOF_ADDR_IFREQ(ifr) \
294138568Ssam	((ifr).ifr_addr.sa_len > sizeof(struct sockaddr) ? \
295178354Ssam	 (sizeof(struct ifreq) - sizeof(struct sockaddr) + \
296138568Ssam	  (ifr).ifr_addr.sa_len) : sizeof(struct ifreq))
297138568Ssam
298178354Ssamstruct ifaliasreq {
299178354Ssam	char	ifra_name[IFNAMSIZ];		/* if name, e.g. "en0" */
300186302Ssam	struct	sockaddr ifra_addr;
301178354Ssam	struct	sockaddr ifra_broadaddr;
302138568Ssam	struct	sockaddr ifra_mask;
303138568Ssam};
304138568Ssam
305138568Ssamstruct ifmediareq {
306138568Ssam	char	ifm_name[IFNAMSIZ];	/* if name, e.g. "en0" */
307138568Ssam	int	ifm_current;		/* current media options */
308138568Ssam	int	ifm_mask;		/* don't care mask */
309138568Ssam	int	ifm_status;		/* media status */
310138568Ssam	int	ifm_active;		/* active options */
311138568Ssam	int	ifm_count;		/* # entries in ifm_ulist array */
312138568Ssam	int	*ifm_ulist;		/* media words */
313165894Ssam};
314165894Ssam
315165894Ssamstruct  ifdrv {
316165894Ssam	char            ifd_name[IFNAMSIZ];     /* if name, e.g. "en0" */
317165894Ssam	unsigned long   ifd_cmd;
318165894Ssam	size_t          ifd_len;
319165894Ssam	void            *ifd_data;
320165894Ssam};
321165894Ssam
322165894Ssam/*
323165894Ssam * Structure used to retrieve aux status data from interfaces.
324165894Ssam * Kernel suppliers to this interface should respect the formatting
325165894Ssam * needed by ifconfig(8): each line starts with a TAB and ends with
326165894Ssam * a newline.  The canonical example to copy and paste is in if_tun.c.
327165894Ssam */
328165894Ssam
329165894Ssam#define	IFSTATMAX	800		/* 10 lines of text */
330165894Ssamstruct ifstat {
331165894Ssam	char	ifs_name[IFNAMSIZ];	/* if name, e.g. "en0" */
332165894Ssam	char	ascii[IFSTATMAX + 1];
333178354Ssam};
334178354Ssam
335178354Ssam/*
336178354Ssam * Structure used in SIOCGIFCONF request.
337178354Ssam * Used to retrieve interface configuration
338178354Ssam * for machine (useful for programs which
339178354Ssam * must know all networks accessible).
340178354Ssam */
341178354Ssamstruct	ifconf {
342178354Ssam	int	ifc_len;		/* size of associated buffer */
343178354Ssam	union {
344178354Ssam		caddr_t	ifcu_buf;
345178354Ssam		struct	ifreq *ifcu_req;
346178354Ssam	} ifc_ifcu;
347178354Ssam#define	ifc_buf	ifc_ifcu.ifcu_buf	/* buffer address */
348178354Ssam#define	ifc_req	ifc_ifcu.ifcu_req	/* array of structures returned */
349178354Ssam};
350178354Ssam
351178354Ssam
352178354Ssam/*
353178354Ssam * Structure for SIOC[AGD]LIFADDR
354178354Ssam */
355178354Ssamstruct if_laddrreq {
356178354Ssam	char	iflr_name[IFNAMSIZ];
357178354Ssam	u_int	flags;
358178354Ssam#define	IFLR_PREFIX	0x8000  /* in: prefix given  out: kernel fills id */
359178354Ssam	u_int	prefixlen;         /* in/out */
360138568Ssam	struct	sockaddr_storage addr;   /* in/out */
361170530Ssam	struct	sockaddr_storage dstaddr; /* out */
362170530Ssam};
363170530Ssam
364170530Ssam#endif /* __BSD_VISIBLE */
365170530Ssam
366170530Ssam#ifdef _KERNEL
367170530Ssam#ifdef MALLOC_DECLARE
368170530SsamMALLOC_DECLARE(M_IFADDR);
369170530SsamMALLOC_DECLARE(M_IFMADDR);
370138568Ssam#endif
371138568Ssam#endif
372138568Ssam
373138568Ssam#ifndef _KERNEL
374138568Ssamstruct if_nameindex {
375138568Ssam	unsigned int	if_index;	/* 1, 2, ... */
376138568Ssam	char		*if_name;	/* null terminated name: "le0", ... */
377138568Ssam};
378138568Ssam
379170530Ssam__BEGIN_DECLS
380138568Ssamvoid			 if_freenameindex(struct if_nameindex *);
381138568Ssamchar			*if_indextoname(unsigned int, char *);
382138568Ssamstruct if_nameindex	*if_nameindex(void);
383138568Ssamunsigned int		 if_nametoindex(const char *);
384138568Ssam__END_DECLS
385138568Ssam#endif
386138568Ssam
387138568Ssam#ifdef _KERNEL
388170530Ssamstruct thread;
389138568Ssam
390138568Ssam/* XXX - this should go away soon. */
391151967Sandre#include <net/if_var.h>
392138568Ssam#endif
393138568Ssam
394138568Ssam#endif /* !_NET_IF_H_ */
395138568Ssam