if.h revision 1.24
1/*	$OpenBSD: if.h,v 1.24 2001/05/28 05:52:51 angelos 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. All advertising materials mentioning features or use of this software
17 *    must display the following acknowledgement:
18 *	This product includes software developed by the University of
19 *	California, Berkeley and its contributors.
20 * 4. Neither the name of the University nor the names of its contributors
21 *    may be used to endorse or promote products derived from this software
22 *    without specific prior written permission.
23 *
24 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 * SUCH DAMAGE.
35 *
36 *	@(#)if.h	8.1 (Berkeley) 6/10/93
37 */
38
39#include <sys/queue.h>
40
41/*
42 * Structures defining a network interface, providing a packet
43 * transport mechanism (ala level 0 of the PUP protocols).
44 *
45 * Each interface accepts output datagrams of a specified maximum
46 * length, and provides higher level routines with input datagrams
47 * received from its medium.
48 *
49 * Output occurs when the routine if_output is called, with four parameters:
50 *	(*ifp->if_output)(ifp, m, dst, rt)
51 * Here m is the mbuf chain to be sent and dst is the destination address.
52 * The output routine encapsulates the supplied datagram if necessary,
53 * and then transmits it on its medium.
54 *
55 * On input, each interface unwraps the data received by it, and either
56 * places it on the input queue of a internetwork datagram routine
57 * and posts the associated software interrupt, or passes the datagram to a raw
58 * packet input routine.
59 *
60 * Routines exist for locating interfaces by their addresses
61 * or for locating a interface on a certain network, as well as more general
62 * routing and gateway routines maintaining information used to locate
63 * interfaces.  These routines live in the files if.c and route.c
64 */
65/*  XXX fast fix for SNMP, going away soon */
66#include <sys/time.h>
67
68struct mbuf;
69struct proc;
70struct rtentry;
71struct socket;
72struct ether_header;
73struct arpcom;
74struct rt_addrinfo;
75
76/*
77 * Structure defining statistics and other data kept regarding a network
78 * interface.
79 */
80struct	if_data {
81	/* generic interface information */
82	u_char	ifi_type;		/* ethernet, tokenring, etc. */
83	u_char	ifi_addrlen;		/* media address length */
84	u_char	ifi_hdrlen;		/* media header length */
85	u_char	ifi_link_state;		/* current link state */
86	u_long	ifi_mtu;		/* maximum transmission unit */
87	u_long	ifi_metric;		/* routing metric (external only) */
88	u_long	ifi_baudrate;		/* linespeed */
89	/* volatile statistics */
90	u_long	ifi_ipackets;		/* packets received on interface */
91	u_long	ifi_ierrors;		/* input errors on interface */
92	u_long	ifi_opackets;		/* packets sent on interface */
93	u_long	ifi_oerrors;		/* output errors on interface */
94	u_long	ifi_collisions;		/* collisions on csma interfaces */
95	u_long	ifi_ibytes;		/* total number of octets received */
96	u_long	ifi_obytes;		/* total number of octets sent */
97	u_long	ifi_imcasts;		/* packets received via multicast */
98	u_long	ifi_omcasts;		/* packets sent via multicast */
99	u_long	ifi_iqdrops;		/* dropped on input, this interface */
100	u_long	ifi_noproto;		/* destined for unsupported protocol */
101	struct	timeval ifi_lastchange;	/* last updated */
102};
103
104/*
105 * Values for if_link_state.
106 */
107#define	LINK_STATE_UNKNOWN	0	/* link invalid/unknown */
108#define	LINK_STATE_DOWN		1	/* link is down */
109#define	LINK_STATE_UP		2	/* link is up */
110
111/*
112 * Structure defining a queue for a network interface.
113 *
114 * (Would like to call this struct ``if'', but C isn't PL/1.)
115 */
116TAILQ_HEAD(ifnet_head, ifnet);		/* the actual queue head */
117
118/*
119 * Length of interface external name, including terminating '\0'.
120 * Note: this is the same size as a generic device's external name.
121 */
122#define	IFNAMSIZ	16
123#define	IF_NAMESIZE	IFNAMSIZ
124
125struct ifnet {				/* and the entries */
126	void	*if_softc;		/* lower-level data for this if */
127	TAILQ_ENTRY(ifnet) if_list;	/* all struct ifnets are chained */
128	TAILQ_HEAD(, ifaddr) if_addrlist; /* linked list of addresses per if */
129	char	if_xname[IFNAMSIZ];	/* external name (name + unit) */
130	int	if_pcount;		/* number of promiscuous listeners */
131	caddr_t	if_bpf;			/* packet filter structure */
132	caddr_t	if_bridge;		/* bridge structure */
133	u_short	if_index;		/* numeric abbreviation for this if */
134	short	if_timer;		/* time 'til if_watchdog called */
135	short	if_flags;		/* up/down, broadcast, etc. */
136	struct	if_data if_data;	/* stats and other data about if */
137	int	if_capabilities;	/* interface capabilities */
138
139	/* procedure handles */
140	int	(*if_output)		/* output routine (enqueue) */
141		__P((struct ifnet *, struct mbuf *, struct sockaddr *,
142		     struct rtentry *));
143	void	(*if_start)		/* initiate output routine */
144		__P((struct ifnet *));
145	int	(*if_ioctl)		/* ioctl routine */
146		__P((struct ifnet *, u_long, caddr_t));
147	int	(*if_reset)		/* XXX bus reset routine */
148		__P((struct ifnet *));
149	void	(*if_watchdog)		/* timer routine */
150		__P((struct ifnet *));
151	struct	ifqueue {
152		struct	mbuf *ifq_head;
153		struct	mbuf *ifq_tail;
154		int	ifq_len;
155		int	ifq_maxlen;
156		int	ifq_drops;
157	} if_snd;			/* output queue */
158	struct ifprefix *if_prefixlist; /* linked list of prefixes per if */
159};
160#define	if_mtu		if_data.ifi_mtu
161#define	if_type		if_data.ifi_type
162#define	if_addrlen	if_data.ifi_addrlen
163#define	if_hdrlen	if_data.ifi_hdrlen
164#define	if_metric	if_data.ifi_metric
165#define	if_link_state	if_data.ifi_link_state
166#define	if_baudrate	if_data.ifi_baudrate
167#define	if_ipackets	if_data.ifi_ipackets
168#define	if_ierrors	if_data.ifi_ierrors
169#define	if_opackets	if_data.ifi_opackets
170#define	if_oerrors	if_data.ifi_oerrors
171#define	if_collisions	if_data.ifi_collisions
172#define	if_ibytes	if_data.ifi_ibytes
173#define	if_obytes	if_data.ifi_obytes
174#define	if_imcasts	if_data.ifi_imcasts
175#define	if_omcasts	if_data.ifi_omcasts
176#define	if_iqdrops	if_data.ifi_iqdrops
177#define	if_noproto	if_data.ifi_noproto
178#define	if_lastchange	if_data.ifi_lastchange
179
180#define	IFF_UP		0x1		/* interface is up */
181#define	IFF_BROADCAST	0x2		/* broadcast address valid */
182#define	IFF_DEBUG	0x4		/* turn on debugging */
183#define	IFF_LOOPBACK	0x8		/* is a loopback net */
184#define	IFF_POINTOPOINT	0x10		/* interface is point-to-point link */
185#define	IFF_NOTRAILERS	0x20		/* avoid use of trailers */
186#define	IFF_RUNNING	0x40		/* resources allocated */
187#define	IFF_NOARP	0x80		/* no address resolution protocol */
188#define	IFF_PROMISC	0x100		/* receive all packets */
189#define	IFF_ALLMULTI	0x200		/* receive all multicast packets */
190#define	IFF_OACTIVE	0x400		/* transmission in progress */
191#define	IFF_SIMPLEX	0x800		/* can't hear own transmissions */
192#define	IFF_LINK0	0x1000		/* per link layer defined bit */
193#define	IFF_LINK1	0x2000		/* per link layer defined bit */
194#define	IFF_LINK2	0x4000		/* per link layer defined bit */
195#define	IFF_MULTICAST	0x8000		/* supports multicast */
196
197/* flags set internally only: */
198#define	IFF_CANTCHANGE \
199	(IFF_BROADCAST|IFF_POINTOPOINT|IFF_RUNNING|IFF_OACTIVE|\
200	    IFF_SIMPLEX|IFF_MULTICAST|IFF_ALLMULTI)
201
202/*
203 * Some convenience macros used for setting ifi_baudrate.
204 */
205#define	IF_Kbps(x)	((x) * 1000)		/* kilobits/sec. */
206#define	IF_Mbps(x)	(IF_Kbps((x) * 1000))	/* megabits/sec. */
207#define	IF_Gbps(x)	(IF_Mbps((x) * 1000))	/* gigabits/sec. */
208
209/* Capabilities that interfaces can advertise. */
210#define	IFCAP_CSUM_IPv4		0x00000001	/* can do IPv4 header csum */
211#define	IFCAP_CSUM_TCPv4	0x00000002	/* can do IPv4/TCP csum */
212#define	IFCAP_CSUM_UDPv4	0x00000004	/* can do IPv4/UDP csum */
213#define	IFCAP_IPSECv4		0x00000008	/* can do IPv4 IPsec */
214#define	IFCAP_VLAN_MTU		0x00000010	/* VLAN-compatible MTU */
215#define	IFCAP_VLAN_HWTAGGING	0x00000020	/* hardware VLAN tag support */
216
217/*
218 * Output queues (ifp->if_snd) and internetwork datagram level (pup level 1)
219 * input routines have queues of messages stored on ifqueue structures
220 * (defined above).  Entries are added to and deleted from these structures
221 * by these macros, which should be called with ipl raised to splimp().
222 */
223#define	IF_QFULL(ifq)		((ifq)->ifq_len >= (ifq)->ifq_maxlen)
224#define	IF_DROP(ifq)		((ifq)->ifq_drops++)
225#define	IF_ENQUEUE(ifq, m) { \
226	(m)->m_nextpkt = 0; \
227	if ((ifq)->ifq_tail == 0) \
228		(ifq)->ifq_head = m; \
229	else \
230		(ifq)->ifq_tail->m_nextpkt = m; \
231	(ifq)->ifq_tail = m; \
232	(ifq)->ifq_len++; \
233}
234#define	IF_PREPEND(ifq, m) { \
235	(m)->m_nextpkt = (ifq)->ifq_head; \
236	if ((ifq)->ifq_tail == 0) \
237		(ifq)->ifq_tail = (m); \
238	(ifq)->ifq_head = (m); \
239	(ifq)->ifq_len++; \
240}
241#define	IF_DEQUEUE(ifq, m) { \
242	(m) = (ifq)->ifq_head; \
243	if (m) { \
244		if (((ifq)->ifq_head = (m)->m_nextpkt) == 0) \
245			(ifq)->ifq_tail = 0; \
246		(m)->m_nextpkt = 0; \
247		(ifq)->ifq_len--; \
248	} \
249}
250
251#define	IFQ_MAXLEN	50
252#define	IFNET_SLOWHZ	1		/* granularity is 1 second */
253
254/*
255 * The ifaddr structure contains information about one address
256 * of an interface.  They are maintained by the different address families,
257 * are allocated and attached when an address is set, and are linked
258 * together so all addresses for an interface can be located.
259 */
260struct ifaddr {
261	struct	sockaddr *ifa_addr;	/* address of interface */
262	struct	sockaddr *ifa_dstaddr;	/* other end of p-to-p link */
263#define	ifa_broadaddr	ifa_dstaddr	/* broadcast address interface */
264	struct	sockaddr *ifa_netmask;	/* used to determine subnet */
265	struct	ifnet *ifa_ifp;		/* back-pointer to interface */
266	TAILQ_ENTRY(ifaddr) ifa_list;	/* list of addresses for interface */
267	void	(*ifa_rtrequest)	/* check or clean routes (+ or -)'d */
268		    __P((int, struct rtentry *, struct rt_addrinfo *));
269	u_int	ifa_flags;		/* mostly rt_flags for cloning */
270	u_int	ifa_refcnt;		/* count of references */
271	int	ifa_metric;		/* cost of going out this interface */
272};
273#define	IFA_ROUTE	RTF_UP		/* route installed */
274
275/*
276 * The prefix structure contains information about one prefix
277 * of an interface.  They are maintained by the different address families,
278 * are allocated and attached when an prefix or an address is set,
279 * and are linked together so all prfefixes for an interface can be located.
280 */
281struct ifprefix {
282	struct	sockaddr *ifpr_prefix;	/* prefix of interface */
283	struct	ifnet *ifpr_ifp;	/* back-pointer to interface */
284	struct ifprefix *ifpr_next;
285	u_char	ifpr_plen;		/* prefix length in bits */
286	u_char	ifpr_type;		/* protocol dependent prefix type */
287};
288
289/*
290 * Message format for use in obtaining information about interfaces
291 * from sysctl and the routing socket.
292 */
293struct if_msghdr {
294	u_short	ifm_msglen;	/* to skip over non-understood messages */
295	u_char	ifm_version;	/* future binary compatability */
296	u_char	ifm_type;	/* message type */
297	int	ifm_addrs;	/* like rtm_addrs */
298	int	ifm_flags;	/* value of if_flags */
299	u_short	ifm_index;	/* index for associated ifp */
300	struct	if_data ifm_data;/* statistics and other data about if */
301};
302
303/*
304 * Message format for use in obtaining information about interface addresses
305 * from sysctl and the routing socket.
306 */
307struct ifa_msghdr {
308	u_short	ifam_msglen;	/* to skip over non-understood messages */
309	u_char	ifam_version;	/* future binary compatability */
310	u_char	ifam_type;	/* message type */
311	int	ifam_addrs;	/* like rtm_addrs */
312	int	ifam_flags;	/* value of ifa_flags */
313	u_short	ifam_index;	/* index for associated ifp */
314	int	ifam_metric;	/* value of ifa_metric */
315};
316
317/*
318 * Interface request structure used for socket
319 * ioctl's.  All interface ioctl's must have parameter
320 * definitions which begin with ifr_name.  The
321 * remainder may be interface specific.
322 */
323struct	ifreq {
324	char	ifr_name[IFNAMSIZ];		/* if name, e.g. "en0" */
325	union {
326		struct	sockaddr ifru_addr;
327		struct	sockaddr ifru_dstaddr;
328		struct	sockaddr ifru_broadaddr;
329		short	ifru_flags;
330		int	ifru_metric;
331		caddr_t	ifru_data;
332	} ifr_ifru;
333#define	ifr_addr	ifr_ifru.ifru_addr	/* address */
334#define	ifr_dstaddr	ifr_ifru.ifru_dstaddr	/* other end of p-to-p link */
335#define	ifr_broadaddr	ifr_ifru.ifru_broadaddr	/* broadcast address */
336#define	ifr_flags	ifr_ifru.ifru_flags	/* flags */
337#define	ifr_metric	ifr_ifru.ifru_metric	/* metric */
338#define	ifr_mtu		ifr_ifru.ifru_metric	/* mtu (overload) */
339#define	ifr_media	ifr_ifru.ifru_metric	/* media options (overload) */
340#define	ifr_data	ifr_ifru.ifru_data	/* for use by interface */
341};
342
343struct ifaliasreq {
344	char	ifra_name[IFNAMSIZ];		/* if name, e.g. "en0" */
345	struct	sockaddr ifra_addr;
346	struct	sockaddr ifra_dstaddr;
347#define	ifra_broadaddr	ifra_dstaddr
348	struct	sockaddr ifra_mask;
349};
350
351struct ifmediareq {
352	char	ifm_name[IFNAMSIZ];		/* if name, e.g. "en0" */
353	int	ifm_current;			/* current media options */
354	int	ifm_mask;			/* don't care mask */
355	int	ifm_status;			/* media status */
356	int	ifm_active;			/* active options */
357	int	ifm_count;			/* # entries in ifm_ulist
358							array */
359	int	*ifm_ulist;			/* media words */
360};
361
362/*
363 * Structure used in SIOCGIFCONF request.
364 * Used to retrieve interface configuration
365 * for machine (useful for programs which
366 * must know all networks accessible).
367 */
368struct	ifconf {
369	int	ifc_len;		/* size of associated buffer */
370	union {
371		caddr_t	ifcu_buf;
372		struct	ifreq *ifcu_req;
373	} ifc_ifcu;
374#define	ifc_buf	ifc_ifcu.ifcu_buf	/* buffer address */
375#define	ifc_req	ifc_ifcu.ifcu_req	/* array of structures returned */
376};
377
378/*
379 * Structure for SIOC[AGD]LIFADDR
380 */
381struct if_laddrreq {
382	char iflr_name[IFNAMSIZ];
383	unsigned int flags;
384#define IFLR_PREFIX	0x8000	/* in: prefix given  out: kernel fills id */
385	unsigned int prefixlen;		/* in/out */
386	struct sockaddr_storage addr;	/* in/out */
387	struct sockaddr_storage dstaddr; /* out */
388};
389
390struct if_nameindex {
391	unsigned int	if_index;
392	char 		*if_name;
393};
394
395#ifndef _KERNEL
396__BEGIN_DECLS
397unsigned int if_nametoindex __P((const char *));
398char 	*if_indextoname __P((unsigned int, char *));
399struct	if_nameindex *if_nameindex __P((void));
400__END_DECLS
401#define if_freenameindex(x)	free(x)
402#endif
403
404#include <net/if_arp.h>
405
406#ifdef _KERNEL
407#define	IFAFREE(ifa) \
408do { \
409	if ((ifa)->ifa_refcnt <= 0) \
410		ifafree(ifa); \
411	else \
412		(ifa)->ifa_refcnt--; \
413} while (0)
414
415struct ifnet_head ifnet;
416struct ifnet **ifindex2ifnet;
417struct ifnet *lo0ifp;
418int if_index;
419
420void	ether_ifattach __P((struct ifnet *));
421void	ether_ifdetach __P((struct ifnet *));
422int	ether_ioctl __P((struct ifnet *, struct arpcom *, u_long, caddr_t));
423void	ether_input __P((struct ifnet *, struct ether_header *, struct mbuf *));
424int	ether_output __P((struct ifnet *,
425	   struct mbuf *, struct sockaddr *, struct rtentry *));
426char	*ether_sprintf __P((u_char *));
427
428void	if_attach __P((struct ifnet *));
429void	if_attachtail __P((struct ifnet *));
430void	if_attachhead __P((struct ifnet *));
431void	if_detach __P((struct ifnet *));
432void	if_down __P((struct ifnet *));
433void	if_qflush __P((struct ifqueue *));
434void	if_slowtimo __P((void *));
435void	if_up __P((struct ifnet *));
436int	ifconf __P((u_long, caddr_t));
437void	ifinit __P((void));
438int	ifioctl __P((struct socket *, u_long, caddr_t, struct proc *));
439int	ifpromisc __P((struct ifnet *, int));
440struct	ifnet *ifunit __P((char *));
441
442struct	ifaddr *ifa_ifwithaddr __P((struct sockaddr *));
443struct	ifaddr *ifa_ifwithaf __P((int));
444struct	ifaddr *ifa_ifwithdstaddr __P((struct sockaddr *));
445struct	ifaddr *ifa_ifwithnet __P((struct sockaddr *));
446struct	ifaddr *ifa_ifwithroute __P((int, struct sockaddr *,
447					struct sockaddr *));
448struct	ifaddr *ifaof_ifpforaddr __P((struct sockaddr *, struct ifnet *));
449void	ifafree __P((struct ifaddr *));
450void	link_rtrequest __P((int, struct rtentry *, struct rt_addrinfo *));
451
452int	loioctl __P((struct ifnet *, u_long, caddr_t));
453void	loopattach __P((int));
454int	looutput __P((struct ifnet *,
455	   struct mbuf *, struct sockaddr *, struct rtentry *));
456void	lortrequest __P((int, struct rtentry *, struct rt_addrinfo *));
457#endif /* _KERNEL */
458