xref: /csrg-svn/sys/net/if.h (revision 45161)
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*45161Skarels  *	@(#)if.h	7.10 (Berkeley) 08/30/90
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 */
924945Swnj };
934945Swnj 
946333Ssam #define	IFF_UP		0x1		/* interface is up */
956333Ssam #define	IFF_BROADCAST	0x2		/* broadcast address valid */
966333Ssam #define	IFF_DEBUG	0x4		/* turn on debugging */
9726092Skarels #define	IFF_LOOPBACK	0x8		/* is a loopback net */
986924Swnj #define	IFF_POINTOPOINT	0x10		/* interface is point-to-point link */
9913049Ssam #define	IFF_NOTRAILERS	0x20		/* avoid use of trailers */
10013053Ssam #define	IFF_RUNNING	0x40		/* resources allocated */
10114866Ssam #define	IFF_NOARP	0x80		/* no address resolution protocol */
10228153Skarels /* next two not supported now, but reserved: */
10328153Skarels #define	IFF_PROMISC	0x100		/* receive all packets */
10428153Skarels #define	IFF_ALLMULTI	0x200		/* receive all multicast packets */
10535795Skarels #define	IFF_OACTIVE	0x400		/* transmission in progress */
10635795Skarels #define	IFF_SIMPLEX	0x800		/* can't hear own transmissions */
10735795Skarels 
10826092Skarels /* flags set internally only: */
109*45161Skarels #define	IFF_CANTCHANGE \
110*45161Skarels 	(IFF_BROADCAST|IFF_POINTOPOINT|IFF_RUNNING|IFF_OACTIVE|IFF_SIMPLEX)
1116333Ssam 
1125104Swnj /*
1135104Swnj  * Output queues (ifp->if_snd) and internetwork datagram level (pup level 1)
1145104Swnj  * input routines have queues of messages stored on ifqueue structures
1155104Swnj  * (defined above).  Entries are added to and deleted from these structures
1165104Swnj  * by these macros, which should be called with ipl raised to splimp().
1175104Swnj  */
1186207Swnj #define	IF_QFULL(ifq)		((ifq)->ifq_len >= (ifq)->ifq_maxlen)
1196207Swnj #define	IF_DROP(ifq)		((ifq)->ifq_drops++)
1205083Swnj #define	IF_ENQUEUE(ifq, m) { \
12143059Skarels 	(m)->m_nextpkt = 0; \
1225083Swnj 	if ((ifq)->ifq_tail == 0) \
1235160Swnj 		(ifq)->ifq_head = m; \
1245083Swnj 	else \
12543059Skarels 		(ifq)->ifq_tail->m_nextpkt = m; \
1265160Swnj 	(ifq)->ifq_tail = m; \
1276207Swnj 	(ifq)->ifq_len++; \
1285083Swnj }
1295613Swnj #define	IF_PREPEND(ifq, m) { \
13043059Skarels 	(m)->m_nextpkt = (ifq)->ifq_head; \
1316087Sroot 	if ((ifq)->ifq_tail == 0) \
1326087Sroot 		(ifq)->ifq_tail = (m); \
1335613Swnj 	(ifq)->ifq_head = (m); \
1346207Swnj 	(ifq)->ifq_len++; \
1355613Swnj }
1365083Swnj #define	IF_DEQUEUE(ifq, m) { \
1375083Swnj 	(m) = (ifq)->ifq_head; \
1385083Swnj 	if (m) { \
13943059Skarels 		if (((ifq)->ifq_head = (m)->m_nextpkt) == 0) \
1405083Swnj 			(ifq)->ifq_tail = 0; \
14143059Skarels 		(m)->m_nextpkt = 0; \
1426207Swnj 		(ifq)->ifq_len--; \
1435083Swnj 	} \
1445083Swnj }
1454945Swnj 
1466207Swnj #define	IFQ_MAXLEN	50
1477264Ssam #define	IFNET_SLOWHZ	1		/* granularity is 1 second */
1486207Swnj 
14911576Ssam /*
15018408Skarels  * The ifaddr structure contains information about one address
15118408Skarels  * of an interface.  They are maintained by the different address families,
15218408Skarels  * are allocated and attached when an address is set, and are linked
15318408Skarels  * together so all addresses for an interface can be located.
15418408Skarels  */
15518408Skarels struct ifaddr {
15637472Ssklower 	struct	sockaddr *ifa_addr;	/* address of interface */
15737472Ssklower 	struct	sockaddr *ifa_dstaddr;	/* other end of p-to-p link */
15837472Ssklower #define	ifa_broadaddr	ifa_dstaddr	/* broadcast address interface */
15937472Ssklower 	struct	sockaddr *ifa_netmask;	/* used to determine subnet */
16018408Skarels 	struct	ifnet *ifa_ifp;		/* back-pointer to interface */
16118408Skarels 	struct	ifaddr *ifa_next;	/* next address for interface */
16240789Ssklower 	int	(*ifa_rtrequest)();	/* check or clean routes (+ or -)'d */
16340789Ssklower 	struct 	rtentry *ifa_rt;	/* ??? for ROUTETOIF */
16440789Ssklower 	u_short	ifa_flags;		/* mostly rt_flags for cloning */
16540789Ssklower 	u_short	ifa_llinfolen;		/* extra to malloc for link info */
16618408Skarels };
16740789Ssklower #define IFA_ROUTE	RTF_UP		/* route installed */
16818408Skarels /*
16913049Ssam  * Interface request structure used for socket
17013049Ssam  * ioctl's.  All interface ioctl's must have parameter
17113049Ssam  * definitions which begin with ifr_name.  The
17213049Ssam  * remainder may be interface specific.
17311576Ssam  */
17411576Ssam struct	ifreq {
17513049Ssam #define	IFNAMSIZ	16
17613049Ssam 	char	ifr_name[IFNAMSIZ];		/* if name, e.g. "en0" */
17711576Ssam 	union {
17811576Ssam 		struct	sockaddr ifru_addr;
17911576Ssam 		struct	sockaddr ifru_dstaddr;
18016387Skarels 		struct	sockaddr ifru_broadaddr;
18111576Ssam 		short	ifru_flags;
18226092Skarels 		int	ifru_metric;
18313049Ssam 		caddr_t	ifru_data;
18411576Ssam 	} ifr_ifru;
18511576Ssam #define	ifr_addr	ifr_ifru.ifru_addr	/* address */
18611576Ssam #define	ifr_dstaddr	ifr_ifru.ifru_dstaddr	/* other end of p-to-p link */
18716387Skarels #define	ifr_broadaddr	ifr_ifru.ifru_broadaddr	/* broadcast address */
18811576Ssam #define	ifr_flags	ifr_ifru.ifru_flags	/* flags */
18926092Skarels #define	ifr_metric	ifr_ifru.ifru_metric	/* metric */
19013049Ssam #define	ifr_data	ifr_ifru.ifru_data	/* for use by interface */
19111576Ssam };
19211576Ssam 
19337472Ssklower struct ifaliasreq {
19437472Ssklower 	char	ifra_name[IFNAMSIZ];		/* if name, e.g. "en0" */
19537472Ssklower 	struct	sockaddr ifra_addr;
19637472Ssklower 	struct	sockaddr ifra_broadaddr;
19737472Ssklower 	struct	sockaddr ifra_mask;
19837472Ssklower };
19937472Ssklower 
20011576Ssam /*
20111576Ssam  * Structure used in SIOCGIFCONF request.
20211576Ssam  * Used to retrieve interface configuration
20311576Ssam  * for machine (useful for programs which
20411576Ssam  * must know all networks accessible).
20511576Ssam  */
20611576Ssam struct	ifconf {
20711576Ssam 	int	ifc_len;		/* size of associated buffer */
20811576Ssam 	union {
20911576Ssam 		caddr_t	ifcu_buf;
21011576Ssam 		struct	ifreq *ifcu_req;
21111576Ssam 	} ifc_ifcu;
21211576Ssam #define	ifc_buf	ifc_ifcu.ifcu_buf	/* buffer address */
21311576Ssam #define	ifc_req	ifc_ifcu.ifcu_req	/* array of structures returned */
21411576Ssam };
21511576Ssam 
2164945Swnj #ifdef KERNEL
21725971Skarels #include "../net/if_arp.h"
2186333Ssam struct	ifqueue rawintrq;		/* raw packet input queue */
2194945Swnj struct	ifnet *ifnet;
22028943Skarels struct	ifaddr *ifa_ifwithaddr(), *ifa_ifwithnet();
22127197Skarels struct	ifaddr *ifa_ifwithdstaddr();
22225971Skarels #else KERNEL
22325971Skarels #include <net/if_arp.h>
22425971Skarels #endif KERNEL
225