xref: /csrg-svn/sys/net/if.h (revision 52026)
123158Smckusick /*
239182Ssklower  * Copyright (c) 1982, 1986, 1989 Regents of the University of California.
333183Sbostic  * All rights reserved.
423158Smckusick  *
544464Sbostic  * %sccs.include.redist.c%
633183Sbostic  *
7*52026Ssklower  *	@(#)if.h	7.13 (Berkeley) 12/19/91
823158Smckusick  */
94945Swnj 
104945Swnj /*
115104Swnj  * Structures defining a network interface, providing a packet
125104Swnj  * transport mechanism (ala level 0 of the PUP protocols).
135104Swnj  *
145104Swnj  * Each interface accepts output datagrams of a specified maximum
155104Swnj  * length, and provides higher level routines with input datagrams
165104Swnj  * received from its medium.
175104Swnj  *
185104Swnj  * Output occurs when the routine if_output is called, with three parameters:
196333Ssam  *	(*ifp->if_output)(ifp, m, dst)
206333Ssam  * Here m is the mbuf chain to be sent and dst is the destination address.
216333Ssam  * The output routine encapsulates the supplied datagram if necessary,
226333Ssam  * and then transmits it on its medium.
235104Swnj  *
245104Swnj  * On input, each interface unwraps the data received by it, and either
255104Swnj  * places it on the input queue of a internetwork datagram routine
265104Swnj  * and posts the associated software interrupt, or passes the datagram to a raw
275104Swnj  * packet input routine.
285104Swnj  *
296333Ssam  * Routines exist for locating interfaces by their addresses
305104Swnj  * or for locating a interface on a certain network, as well as more general
315104Swnj  * routing and gateway routines maintaining information used to locate
326333Ssam  * interfaces.  These routines live in the files if.c and route.c
335083Swnj  */
3439182Ssklower #ifndef _TIME_ /*  XXX fast fix for SNMP, going away soon */
3539182Ssklower #ifdef KERNEL
3639182Ssklower #include "../sys/time.h"
3739182Ssklower #else
3839182Ssklower #include <sys/time.h>
3939182Ssklower #endif
4039182Ssklower #endif
415083Swnj 
425083Swnj /*
435083Swnj  * Structure defining a queue for a network interface.
444945Swnj  *
454945Swnj  * (Would like to call this struct ``if'', but C isn't PL/1.)
464945Swnj  */
4739182Ssklower 
484945Swnj struct ifnet {
495171Swnj 	char	*if_name;		/* name, e.g. ``en'' or ``lo'' */
504945Swnj 	short	if_unit;		/* sub-unit for lower level driver */
514945Swnj 	short	if_mtu;			/* maximum transmission unit */
526333Ssam 	short	if_flags;		/* up/down, broadcast, etc. */
537264Ssam 	short	if_timer;		/* time 'til if_watchdog called */
5426092Skarels 	int	if_metric;		/* routing metric (external only) */
5518408Skarels 	struct	ifaddr *if_addrlist;	/* linked list of addresses per if */
565104Swnj 	struct	ifqueue {
575104Swnj 		struct	mbuf *ifq_head;
585104Swnj 		struct	mbuf *ifq_tail;
596207Swnj 		int	ifq_len;
606207Swnj 		int	ifq_maxlen;
616207Swnj 		int	ifq_drops;
625104Swnj 	} if_snd;			/* output queue */
635104Swnj /* procedure handles */
645104Swnj 	int	(*if_init)();		/* init routine */
6535795Skarels 	int	(*if_output)();		/* output routine (enqueue) */
6635795Skarels 	int	(*if_start)();		/* initiate output routine */
6735795Skarels 	int	(*if_done)();		/* output complete routine */
6813049Ssam 	int	(*if_ioctl)();		/* ioctl routine */
698972Sroot 	int	(*if_reset)();		/* bus reset routine */
707264Ssam 	int	(*if_watchdog)();	/* timer routine */
715104Swnj /* generic interface statistics */
725171Swnj 	int	if_ipackets;		/* packets received on interface */
735171Swnj 	int	if_ierrors;		/* input errors on interface */
745171Swnj 	int	if_opackets;		/* packets sent on interface */
755171Swnj 	int	if_oerrors;		/* output errors on interface */
765104Swnj 	int	if_collisions;		/* collisions on csma interfaces */
775104Swnj /* end statistics */
784951Swnj 	struct	ifnet *if_next;
7937472Ssklower 	u_char	if_type;		/* ethernet, tokenring, etc */
8037472Ssklower 	u_char	if_addrlen;		/* media address length */
8137472Ssklower 	u_char	if_hdrlen;		/* media header length */
8240789Ssklower 	u_char	if_index;		/* numeric abbreviation for this if  */
8339182Ssklower /* more statistics here to avoid recompiling netstat */
8439182Ssklower 	struct	timeval if_lastchange;	/* last updated */
8539182Ssklower 	int	if_ibytes;		/* total number of octets received */
8639182Ssklower 	int	if_obytes;		/* total number of octets sent */
8739182Ssklower 	int	if_imcasts;		/* packets received via multicast */
8839182Ssklower 	int	if_omcasts;		/* packets sent via multicast */
8939182Ssklower 	int	if_iqdrops;		/* dropped on input, this interface */
9039182Ssklower 	int	if_noproto;		/* destined for unsupported protocol */
9139182Ssklower 	int	if_baudrate;		/* linespeed */
9247566Smccanne         int	if_pcount;		/* number of promiscuous listeners */
934945Swnj };
944945Swnj 
956333Ssam #define	IFF_UP		0x1		/* interface is up */
966333Ssam #define	IFF_BROADCAST	0x2		/* broadcast address valid */
976333Ssam #define	IFF_DEBUG	0x4		/* turn on debugging */
9826092Skarels #define	IFF_LOOPBACK	0x8		/* is a loopback net */
996924Swnj #define	IFF_POINTOPOINT	0x10		/* interface is point-to-point link */
10013049Ssam #define	IFF_NOTRAILERS	0x20		/* avoid use of trailers */
10113053Ssam #define	IFF_RUNNING	0x40		/* resources allocated */
10214866Ssam #define	IFF_NOARP	0x80		/* no address resolution protocol */
10328153Skarels /* next two not supported now, but reserved: */
10428153Skarels #define	IFF_PROMISC	0x100		/* receive all packets */
10528153Skarels #define	IFF_ALLMULTI	0x200		/* receive all multicast packets */
10635795Skarels #define	IFF_OACTIVE	0x400		/* transmission in progress */
10735795Skarels #define	IFF_SIMPLEX	0x800		/* can't hear own transmissions */
108*52026Ssklower #define	IFF_LINK0	0x1000		/* per link layer defined bit */
109*52026Ssklower #define	IFF_LINK1	0x2000		/* per link layer defined bit */
110*52026Ssklower #define	IFF_LINK2	0x4000		/* per link layer defined bit */
11135795Skarels 
11226092Skarels /* flags set internally only: */
11345161Skarels #define	IFF_CANTCHANGE \
11445161Skarels 	(IFF_BROADCAST|IFF_POINTOPOINT|IFF_RUNNING|IFF_OACTIVE|IFF_SIMPLEX)
1156333Ssam 
1165104Swnj /*
1175104Swnj  * Output queues (ifp->if_snd) and internetwork datagram level (pup level 1)
1185104Swnj  * input routines have queues of messages stored on ifqueue structures
1195104Swnj  * (defined above).  Entries are added to and deleted from these structures
1205104Swnj  * by these macros, which should be called with ipl raised to splimp().
1215104Swnj  */
1226207Swnj #define	IF_QFULL(ifq)		((ifq)->ifq_len >= (ifq)->ifq_maxlen)
1236207Swnj #define	IF_DROP(ifq)		((ifq)->ifq_drops++)
1245083Swnj #define	IF_ENQUEUE(ifq, m) { \
12543059Skarels 	(m)->m_nextpkt = 0; \
1265083Swnj 	if ((ifq)->ifq_tail == 0) \
1275160Swnj 		(ifq)->ifq_head = m; \
1285083Swnj 	else \
12943059Skarels 		(ifq)->ifq_tail->m_nextpkt = m; \
1305160Swnj 	(ifq)->ifq_tail = m; \
1316207Swnj 	(ifq)->ifq_len++; \
1325083Swnj }
1335613Swnj #define	IF_PREPEND(ifq, m) { \
13443059Skarels 	(m)->m_nextpkt = (ifq)->ifq_head; \
1356087Sroot 	if ((ifq)->ifq_tail == 0) \
1366087Sroot 		(ifq)->ifq_tail = (m); \
1375613Swnj 	(ifq)->ifq_head = (m); \
1386207Swnj 	(ifq)->ifq_len++; \
1395613Swnj }
1405083Swnj #define	IF_DEQUEUE(ifq, m) { \
1415083Swnj 	(m) = (ifq)->ifq_head; \
1425083Swnj 	if (m) { \
14343059Skarels 		if (((ifq)->ifq_head = (m)->m_nextpkt) == 0) \
1445083Swnj 			(ifq)->ifq_tail = 0; \
14543059Skarels 		(m)->m_nextpkt = 0; \
1466207Swnj 		(ifq)->ifq_len--; \
1475083Swnj 	} \
1485083Swnj }
1494945Swnj 
1506207Swnj #define	IFQ_MAXLEN	50
1517264Ssam #define	IFNET_SLOWHZ	1		/* granularity is 1 second */
1526207Swnj 
15311576Ssam /*
15418408Skarels  * The ifaddr structure contains information about one address
15518408Skarels  * of an interface.  They are maintained by the different address families,
15618408Skarels  * are allocated and attached when an address is set, and are linked
15718408Skarels  * together so all addresses for an interface can be located.
15818408Skarels  */
15918408Skarels struct ifaddr {
16037472Ssklower 	struct	sockaddr *ifa_addr;	/* address of interface */
16137472Ssklower 	struct	sockaddr *ifa_dstaddr;	/* other end of p-to-p link */
16237472Ssklower #define	ifa_broadaddr	ifa_dstaddr	/* broadcast address interface */
16337472Ssklower 	struct	sockaddr *ifa_netmask;	/* used to determine subnet */
16418408Skarels 	struct	ifnet *ifa_ifp;		/* back-pointer to interface */
16518408Skarels 	struct	ifaddr *ifa_next;	/* next address for interface */
16640789Ssklower 	int	(*ifa_rtrequest)();	/* check or clean routes (+ or -)'d */
16740789Ssklower 	struct 	rtentry *ifa_rt;	/* ??? for ROUTETOIF */
16840789Ssklower 	u_short	ifa_flags;		/* mostly rt_flags for cloning */
169*52026Ssklower 	u_short	ifa_refcnt;		/* extra to malloc for link info */
17018408Skarels };
17140789Ssklower #define IFA_ROUTE	RTF_UP		/* route installed */
17218408Skarels /*
17313049Ssam  * Interface request structure used for socket
17413049Ssam  * ioctl's.  All interface ioctl's must have parameter
17513049Ssam  * definitions which begin with ifr_name.  The
17613049Ssam  * remainder may be interface specific.
17711576Ssam  */
17811576Ssam struct	ifreq {
17913049Ssam #define	IFNAMSIZ	16
18013049Ssam 	char	ifr_name[IFNAMSIZ];		/* if name, e.g. "en0" */
18111576Ssam 	union {
18211576Ssam 		struct	sockaddr ifru_addr;
18311576Ssam 		struct	sockaddr ifru_dstaddr;
18416387Skarels 		struct	sockaddr ifru_broadaddr;
18511576Ssam 		short	ifru_flags;
18626092Skarels 		int	ifru_metric;
18713049Ssam 		caddr_t	ifru_data;
18811576Ssam 	} ifr_ifru;
18911576Ssam #define	ifr_addr	ifr_ifru.ifru_addr	/* address */
19011576Ssam #define	ifr_dstaddr	ifr_ifru.ifru_dstaddr	/* other end of p-to-p link */
19116387Skarels #define	ifr_broadaddr	ifr_ifru.ifru_broadaddr	/* broadcast address */
19211576Ssam #define	ifr_flags	ifr_ifru.ifru_flags	/* flags */
19326092Skarels #define	ifr_metric	ifr_ifru.ifru_metric	/* metric */
19413049Ssam #define	ifr_data	ifr_ifru.ifru_data	/* for use by interface */
19511576Ssam };
19611576Ssam 
19737472Ssklower struct ifaliasreq {
19837472Ssklower 	char	ifra_name[IFNAMSIZ];		/* if name, e.g. "en0" */
19937472Ssklower 	struct	sockaddr ifra_addr;
20037472Ssklower 	struct	sockaddr ifra_broadaddr;
20137472Ssklower 	struct	sockaddr ifra_mask;
20237472Ssklower };
20337472Ssklower 
20411576Ssam /*
20511576Ssam  * Structure used in SIOCGIFCONF request.
20611576Ssam  * Used to retrieve interface configuration
20711576Ssam  * for machine (useful for programs which
20811576Ssam  * must know all networks accessible).
20911576Ssam  */
21011576Ssam struct	ifconf {
21111576Ssam 	int	ifc_len;		/* size of associated buffer */
21211576Ssam 	union {
21311576Ssam 		caddr_t	ifcu_buf;
21411576Ssam 		struct	ifreq *ifcu_req;
21511576Ssam 	} ifc_ifcu;
21611576Ssam #define	ifc_buf	ifc_ifcu.ifcu_buf	/* buffer address */
21711576Ssam #define	ifc_req	ifc_ifcu.ifcu_req	/* array of structures returned */
21811576Ssam };
21911576Ssam 
2204945Swnj #ifdef KERNEL
221*52026Ssklower #define	IFAFREE(ifa) \
222*52026Ssklower 	if ((ifa)->ifa_refcnt <= 1) \
223*52026Ssklower 		ifafree(ifa); \
224*52026Ssklower 	else \
225*52026Ssklower 		(ifa)->ifa_refcnt--;
226*52026Ssklower 
22725971Skarels #include "../net/if_arp.h"
2286333Ssam struct	ifqueue rawintrq;		/* raw packet input queue */
229*52026Ssklower struct	ifnet	*ifnet;
230*52026Ssklower struct	ifaddr	*ifa_ifwithaddr __P((struct sockaddr *)),
231*52026Ssklower 		*ifa_ifwithnet __P((struct sockaddr *)),
232*52026Ssklower 		*ifa_ifwithdstaddr __P((struct sockaddr *));
233*52026Ssklower void	ifafree __P((struct ifaddr *));
23425971Skarels #else KERNEL
23525971Skarels #include <net/if_arp.h>
23625971Skarels #endif KERNEL
237