xref: /csrg-svn/sys/net/if.h (revision 52272)
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*52272Ssklower  *	@(#)if.h	7.15 (Berkeley) 01/30/92
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:
1952269Ssklower  *	(*ifp->if_output)(ifp, m, dst, rt)
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 
42*52272Ssklower #ifdef __STDC__
435083Swnj /*
44*52272Ssklower  * Forward structure declarations for function prototypes [sic].
45*52272Ssklower  */
46*52272Ssklower struct rtentry;
47*52272Ssklower #endif
48*52272Ssklower /*
495083Swnj  * Structure defining a queue for a network interface.
504945Swnj  *
514945Swnj  * (Would like to call this struct ``if'', but C isn't PL/1.)
524945Swnj  */
5339182Ssklower 
544945Swnj struct ifnet {
555171Swnj 	char	*if_name;		/* name, e.g. ``en'' or ``lo'' */
564945Swnj 	short	if_unit;		/* sub-unit for lower level driver */
574945Swnj 	short	if_mtu;			/* maximum transmission unit */
586333Ssam 	short	if_flags;		/* up/down, broadcast, etc. */
597264Ssam 	short	if_timer;		/* time 'til if_watchdog called */
6026092Skarels 	int	if_metric;		/* routing metric (external only) */
6118408Skarels 	struct	ifaddr *if_addrlist;	/* linked list of addresses per if */
625104Swnj 	struct	ifqueue {
635104Swnj 		struct	mbuf *ifq_head;
645104Swnj 		struct	mbuf *ifq_tail;
656207Swnj 		int	ifq_len;
666207Swnj 		int	ifq_maxlen;
676207Swnj 		int	ifq_drops;
685104Swnj 	} if_snd;			/* output queue */
695104Swnj /* procedure handles */
7052269Ssklower 	int	(*if_init)		/* init routine */
7152269Ssklower 		__P((int));
7252269Ssklower 	int	(*if_output)		/* output routine (enqueue) */
7352269Ssklower 		__P((struct ifnet *, struct mbuf *, struct sockaddr *,
7452269Ssklower 		     struct rtentry *));
7552269Ssklower 	int	(*if_start)		/* initiate output routine */
7652269Ssklower 		__P((struct ifnet *));
7752269Ssklower 	int	(*if_done)		/* output complete routine */
7852269Ssklower 		__P((struct ifnet *));	/* (XXX not used; fake prototype) */
7952269Ssklower 	int	(*if_ioctl)		/* ioctl routine */
8052269Ssklower 		__P((struct ifnet *, int, caddr_t));
8152269Ssklower 	int	(*if_reset)		/* XXX; Unibus reset routine for vax */
8252269Ssklower 		__P((int, int));	/* new autoconfig will permit removal */
8352269Ssklower 	int	(*if_watchdog)		/* timer routine */
8452269Ssklower 		__P((int));
855104Swnj /* generic interface statistics */
865171Swnj 	int	if_ipackets;		/* packets received on interface */
875171Swnj 	int	if_ierrors;		/* input errors on interface */
885171Swnj 	int	if_opackets;		/* packets sent on interface */
895171Swnj 	int	if_oerrors;		/* output errors on interface */
905104Swnj 	int	if_collisions;		/* collisions on csma interfaces */
915104Swnj /* end statistics */
924951Swnj 	struct	ifnet *if_next;
9337472Ssklower 	u_char	if_type;		/* ethernet, tokenring, etc */
9437472Ssklower 	u_char	if_addrlen;		/* media address length */
9537472Ssklower 	u_char	if_hdrlen;		/* media header length */
9640789Ssklower 	u_char	if_index;		/* numeric abbreviation for this if  */
9739182Ssklower /* more statistics here to avoid recompiling netstat */
9839182Ssklower 	struct	timeval if_lastchange;	/* last updated */
9939182Ssklower 	int	if_ibytes;		/* total number of octets received */
10039182Ssklower 	int	if_obytes;		/* total number of octets sent */
10139182Ssklower 	int	if_imcasts;		/* packets received via multicast */
10239182Ssklower 	int	if_omcasts;		/* packets sent via multicast */
10339182Ssklower 	int	if_iqdrops;		/* dropped on input, this interface */
10439182Ssklower 	int	if_noproto;		/* destined for unsupported protocol */
10539182Ssklower 	int	if_baudrate;		/* linespeed */
10647566Smccanne         int	if_pcount;		/* number of promiscuous listeners */
1074945Swnj };
1084945Swnj 
1096333Ssam #define	IFF_UP		0x1		/* interface is up */
1106333Ssam #define	IFF_BROADCAST	0x2		/* broadcast address valid */
1116333Ssam #define	IFF_DEBUG	0x4		/* turn on debugging */
11226092Skarels #define	IFF_LOOPBACK	0x8		/* is a loopback net */
1136924Swnj #define	IFF_POINTOPOINT	0x10		/* interface is point-to-point link */
11413049Ssam #define	IFF_NOTRAILERS	0x20		/* avoid use of trailers */
11513053Ssam #define	IFF_RUNNING	0x40		/* resources allocated */
11614866Ssam #define	IFF_NOARP	0x80		/* no address resolution protocol */
11728153Skarels /* next two not supported now, but reserved: */
11828153Skarels #define	IFF_PROMISC	0x100		/* receive all packets */
11928153Skarels #define	IFF_ALLMULTI	0x200		/* receive all multicast packets */
12035795Skarels #define	IFF_OACTIVE	0x400		/* transmission in progress */
12135795Skarels #define	IFF_SIMPLEX	0x800		/* can't hear own transmissions */
12252026Ssklower #define	IFF_LINK0	0x1000		/* per link layer defined bit */
12352026Ssklower #define	IFF_LINK1	0x2000		/* per link layer defined bit */
12452026Ssklower #define	IFF_LINK2	0x4000		/* per link layer defined bit */
12535795Skarels 
12626092Skarels /* flags set internally only: */
12745161Skarels #define	IFF_CANTCHANGE \
12845161Skarels 	(IFF_BROADCAST|IFF_POINTOPOINT|IFF_RUNNING|IFF_OACTIVE|IFF_SIMPLEX)
1296333Ssam 
1305104Swnj /*
1315104Swnj  * Output queues (ifp->if_snd) and internetwork datagram level (pup level 1)
1325104Swnj  * input routines have queues of messages stored on ifqueue structures
1335104Swnj  * (defined above).  Entries are added to and deleted from these structures
1345104Swnj  * by these macros, which should be called with ipl raised to splimp().
1355104Swnj  */
1366207Swnj #define	IF_QFULL(ifq)		((ifq)->ifq_len >= (ifq)->ifq_maxlen)
1376207Swnj #define	IF_DROP(ifq)		((ifq)->ifq_drops++)
1385083Swnj #define	IF_ENQUEUE(ifq, m) { \
13943059Skarels 	(m)->m_nextpkt = 0; \
1405083Swnj 	if ((ifq)->ifq_tail == 0) \
1415160Swnj 		(ifq)->ifq_head = m; \
1425083Swnj 	else \
14343059Skarels 		(ifq)->ifq_tail->m_nextpkt = m; \
1445160Swnj 	(ifq)->ifq_tail = m; \
1456207Swnj 	(ifq)->ifq_len++; \
1465083Swnj }
1475613Swnj #define	IF_PREPEND(ifq, m) { \
14843059Skarels 	(m)->m_nextpkt = (ifq)->ifq_head; \
1496087Sroot 	if ((ifq)->ifq_tail == 0) \
1506087Sroot 		(ifq)->ifq_tail = (m); \
1515613Swnj 	(ifq)->ifq_head = (m); \
1526207Swnj 	(ifq)->ifq_len++; \
1535613Swnj }
1545083Swnj #define	IF_DEQUEUE(ifq, m) { \
1555083Swnj 	(m) = (ifq)->ifq_head; \
1565083Swnj 	if (m) { \
15743059Skarels 		if (((ifq)->ifq_head = (m)->m_nextpkt) == 0) \
1585083Swnj 			(ifq)->ifq_tail = 0; \
15943059Skarels 		(m)->m_nextpkt = 0; \
1606207Swnj 		(ifq)->ifq_len--; \
1615083Swnj 	} \
1625083Swnj }
1634945Swnj 
1646207Swnj #define	IFQ_MAXLEN	50
1657264Ssam #define	IFNET_SLOWHZ	1		/* granularity is 1 second */
1666207Swnj 
16711576Ssam /*
16818408Skarels  * The ifaddr structure contains information about one address
16918408Skarels  * of an interface.  They are maintained by the different address families,
17018408Skarels  * are allocated and attached when an address is set, and are linked
17118408Skarels  * together so all addresses for an interface can be located.
17218408Skarels  */
17318408Skarels struct ifaddr {
17437472Ssklower 	struct	sockaddr *ifa_addr;	/* address of interface */
17537472Ssklower 	struct	sockaddr *ifa_dstaddr;	/* other end of p-to-p link */
17637472Ssklower #define	ifa_broadaddr	ifa_dstaddr	/* broadcast address interface */
17737472Ssklower 	struct	sockaddr *ifa_netmask;	/* used to determine subnet */
17818408Skarels 	struct	ifnet *ifa_ifp;		/* back-pointer to interface */
17918408Skarels 	struct	ifaddr *ifa_next;	/* next address for interface */
18040789Ssklower 	int	(*ifa_rtrequest)();	/* check or clean routes (+ or -)'d */
18140789Ssklower 	struct 	rtentry *ifa_rt;	/* ??? for ROUTETOIF */
18240789Ssklower 	u_short	ifa_flags;		/* mostly rt_flags for cloning */
18352269Ssklower 	short	ifa_refcnt;		/* extra to malloc for link info */
18418408Skarels };
18540789Ssklower #define IFA_ROUTE	RTF_UP		/* route installed */
18618408Skarels /*
18713049Ssam  * Interface request structure used for socket
18813049Ssam  * ioctl's.  All interface ioctl's must have parameter
18913049Ssam  * definitions which begin with ifr_name.  The
19013049Ssam  * remainder may be interface specific.
19111576Ssam  */
19211576Ssam struct	ifreq {
19313049Ssam #define	IFNAMSIZ	16
19413049Ssam 	char	ifr_name[IFNAMSIZ];		/* if name, e.g. "en0" */
19511576Ssam 	union {
19611576Ssam 		struct	sockaddr ifru_addr;
19711576Ssam 		struct	sockaddr ifru_dstaddr;
19816387Skarels 		struct	sockaddr ifru_broadaddr;
19911576Ssam 		short	ifru_flags;
20026092Skarels 		int	ifru_metric;
20113049Ssam 		caddr_t	ifru_data;
20211576Ssam 	} ifr_ifru;
20311576Ssam #define	ifr_addr	ifr_ifru.ifru_addr	/* address */
20411576Ssam #define	ifr_dstaddr	ifr_ifru.ifru_dstaddr	/* other end of p-to-p link */
20516387Skarels #define	ifr_broadaddr	ifr_ifru.ifru_broadaddr	/* broadcast address */
20611576Ssam #define	ifr_flags	ifr_ifru.ifru_flags	/* flags */
20726092Skarels #define	ifr_metric	ifr_ifru.ifru_metric	/* metric */
20813049Ssam #define	ifr_data	ifr_ifru.ifru_data	/* for use by interface */
20911576Ssam };
21011576Ssam 
21137472Ssklower struct ifaliasreq {
21237472Ssklower 	char	ifra_name[IFNAMSIZ];		/* if name, e.g. "en0" */
21337472Ssklower 	struct	sockaddr ifra_addr;
21437472Ssklower 	struct	sockaddr ifra_broadaddr;
21537472Ssklower 	struct	sockaddr ifra_mask;
21637472Ssklower };
21737472Ssklower 
21811576Ssam /*
21911576Ssam  * Structure used in SIOCGIFCONF request.
22011576Ssam  * Used to retrieve interface configuration
22111576Ssam  * for machine (useful for programs which
22211576Ssam  * must know all networks accessible).
22311576Ssam  */
22411576Ssam struct	ifconf {
22511576Ssam 	int	ifc_len;		/* size of associated buffer */
22611576Ssam 	union {
22711576Ssam 		caddr_t	ifcu_buf;
22811576Ssam 		struct	ifreq *ifcu_req;
22911576Ssam 	} ifc_ifcu;
23011576Ssam #define	ifc_buf	ifc_ifcu.ifcu_buf	/* buffer address */
23111576Ssam #define	ifc_req	ifc_ifcu.ifcu_req	/* array of structures returned */
23211576Ssam };
23311576Ssam 
2344945Swnj #ifdef KERNEL
23552026Ssklower #define	IFAFREE(ifa) \
23652269Ssklower 	if ((ifa)->ifa_refcnt <= 0) \
23752026Ssklower 		ifafree(ifa); \
23852026Ssklower 	else \
23952026Ssklower 		(ifa)->ifa_refcnt--;
24052026Ssklower 
24125971Skarels #include "../net/if_arp.h"
2426333Ssam struct	ifqueue rawintrq;		/* raw packet input queue */
24352026Ssklower struct	ifnet	*ifnet;
24452026Ssklower struct	ifaddr	*ifa_ifwithaddr __P((struct sockaddr *)),
24552026Ssklower 		*ifa_ifwithnet __P((struct sockaddr *)),
24652026Ssklower 		*ifa_ifwithdstaddr __P((struct sockaddr *));
24752026Ssklower void	ifafree __P((struct ifaddr *));
24852269Ssklower void	if_attach __P((struct ifnet *));
24925971Skarels #else KERNEL
25025971Skarels #include <net/if_arp.h>
25125971Skarels #endif KERNEL
252