xref: /csrg-svn/sys/net/if.h (revision 68254)
123158Smckusick /*
263211Sbostic  * Copyright (c) 1982, 1986, 1989, 1993
363211Sbostic  *	The Regents of the University of California.  All rights reserved.
423158Smckusick  *
544464Sbostic  * %sccs.include.redist.c%
633183Sbostic  *
7*68254Scgd  *	@(#)if.h	8.3 (Berkeley) 02/09/95
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 #include <sys/time.h>
3639182Ssklower #endif
375083Swnj 
3852272Ssklower #ifdef __STDC__
395083Swnj /*
4052272Ssklower  * Forward structure declarations for function prototypes [sic].
4152272Ssklower  */
4261343Sbostic struct	mbuf;
4361343Sbostic struct	proc;
4452572Ssklower struct	rtentry;
4561343Sbostic struct	socket;
4661343Sbostic struct	ether_header;
4752272Ssklower #endif
4852272Ssklower /*
4952566Ssklower  * Structure describing information about an interface
5052566Ssklower  * which may be of interest to management entities.
5152566Ssklower  */
5252566Ssklower /*
535083Swnj  * Structure defining a queue for a network interface.
544945Swnj  *
554945Swnj  * (Would like to call this struct ``if'', but C isn't PL/1.)
564945Swnj  */
5739182Ssklower 
584945Swnj struct ifnet {
595171Swnj 	char	*if_name;		/* name, e.g. ``en'' or ``lo'' */
6052566Ssklower 	struct	ifnet *if_next;		/* all struct ifnets are chained */
6152566Ssklower 	struct	ifaddr *if_addrlist;	/* linked list of addresses per if */
6252566Ssklower         int	if_pcount;		/* number of promiscuous listeners */
6354718Ssklower 	caddr_t	if_bpf;			/* packet filter structure */
6452566Ssklower 	u_short	if_index;		/* numeric abbreviation for this if  */
654945Swnj 	short	if_unit;		/* sub-unit for lower level driver */
6652566Ssklower 	short	if_timer;		/* time 'til if_watchdog called */
676333Ssam 	short	if_flags;		/* up/down, broadcast, etc. */
6852566Ssklower 	struct	if_data {
6952566Ssklower /* generic interface information */
7052566Ssklower 		u_char	ifi_type;	/* ethernet, tokenring, etc */
7152566Ssklower 		u_char	ifi_addrlen;	/* media address length */
7252566Ssklower 		u_char	ifi_hdrlen;	/* media header length */
7354718Ssklower 		u_long	ifi_mtu;	/* maximum transmission unit */
7454718Ssklower 		u_long	ifi_metric;	/* routing metric (external only) */
7554718Ssklower 		u_long	ifi_baudrate;	/* linespeed */
7652566Ssklower /* volatile statistics */
7754718Ssklower 		u_long	ifi_ipackets;	/* packets received on interface */
7854718Ssklower 		u_long	ifi_ierrors;	/* input errors on interface */
7954718Ssklower 		u_long	ifi_opackets;	/* packets sent on interface */
8054718Ssklower 		u_long	ifi_oerrors;	/* output errors on interface */
8154718Ssklower 		u_long	ifi_collisions;	/* collisions on csma interfaces */
8254718Ssklower 		u_long	ifi_ibytes;	/* total number of octets received */
8354718Ssklower 		u_long	ifi_obytes;	/* total number of octets sent */
8454718Ssklower 		u_long	ifi_imcasts;	/* packets received via multicast */
8554718Ssklower 		u_long	ifi_omcasts;	/* packets sent via multicast */
8654718Ssklower 		u_long	ifi_iqdrops;	/* dropped on input, this interface */
8754718Ssklower 		u_long	ifi_noproto;	/* destined for unsupported protocol */
8852566Ssklower 		struct	timeval ifi_lastchange;/* last updated */
8952566Ssklower 	}	if_data;
905104Swnj /* procedure handles */
9152269Ssklower 	int	(*if_init)		/* init routine */
9252269Ssklower 		__P((int));
9352269Ssklower 	int	(*if_output)		/* output routine (enqueue) */
9452269Ssklower 		__P((struct ifnet *, struct mbuf *, struct sockaddr *,
9552269Ssklower 		     struct rtentry *));
9652269Ssklower 	int	(*if_start)		/* initiate output routine */
9752269Ssklower 		__P((struct ifnet *));
9852269Ssklower 	int	(*if_done)		/* output complete routine */
9952269Ssklower 		__P((struct ifnet *));	/* (XXX not used; fake prototype) */
10052269Ssklower 	int	(*if_ioctl)		/* ioctl routine */
101*68254Scgd 		__P((struct ifnet *, u_long, caddr_t));
10254718Ssklower 	int	(*if_reset)
10354718Ssklower 		__P((int));		/* new autoconfig will permit removal */
10452269Ssklower 	int	(*if_watchdog)		/* timer routine */
10552269Ssklower 		__P((int));
10652566Ssklower 	struct	ifqueue {
10752566Ssklower 		struct	mbuf *ifq_head;
10852566Ssklower 		struct	mbuf *ifq_tail;
10952566Ssklower 		int	ifq_len;
11052566Ssklower 		int	ifq_maxlen;
11152566Ssklower 		int	ifq_drops;
11252566Ssklower 	} if_snd;			/* output queue */
1134945Swnj };
11452566Ssklower #define	if_mtu		if_data.ifi_mtu
11552566Ssklower #define	if_type		if_data.ifi_type
11652566Ssklower #define	if_addrlen	if_data.ifi_addrlen
11752566Ssklower #define	if_hdrlen	if_data.ifi_hdrlen
11852566Ssklower #define	if_metric	if_data.ifi_metric
11952566Ssklower #define	if_baudrate	if_data.ifi_baudrate
12052566Ssklower #define	if_ipackets	if_data.ifi_ipackets
12152566Ssklower #define	if_ierrors	if_data.ifi_ierrors
12252566Ssklower #define	if_opackets	if_data.ifi_opackets
12352566Ssklower #define	if_oerrors	if_data.ifi_oerrors
12452566Ssklower #define	if_collisions	if_data.ifi_collisions
12552566Ssklower #define	if_ibytes	if_data.ifi_ibytes
12652566Ssklower #define	if_obytes	if_data.ifi_obytes
12752566Ssklower #define	if_imcasts	if_data.ifi_imcasts
12852566Ssklower #define	if_omcasts	if_data.ifi_omcasts
12952566Ssklower #define	if_iqdrops	if_data.ifi_iqdrops
13052566Ssklower #define	if_noproto	if_data.ifi_noproto
13152566Ssklower #define	if_lastchange	if_data.ifi_lastchange
1324945Swnj 
1336333Ssam #define	IFF_UP		0x1		/* interface is up */
1346333Ssam #define	IFF_BROADCAST	0x2		/* broadcast address valid */
1356333Ssam #define	IFF_DEBUG	0x4		/* turn on debugging */
13626092Skarels #define	IFF_LOOPBACK	0x8		/* is a loopback net */
1376924Swnj #define	IFF_POINTOPOINT	0x10		/* interface is point-to-point link */
13813049Ssam #define	IFF_NOTRAILERS	0x20		/* avoid use of trailers */
13913053Ssam #define	IFF_RUNNING	0x40		/* resources allocated */
14014866Ssam #define	IFF_NOARP	0x80		/* no address resolution protocol */
14128153Skarels #define	IFF_PROMISC	0x100		/* receive all packets */
14228153Skarels #define	IFF_ALLMULTI	0x200		/* receive all multicast packets */
14335795Skarels #define	IFF_OACTIVE	0x400		/* transmission in progress */
14435795Skarels #define	IFF_SIMPLEX	0x800		/* can't hear own transmissions */
14552026Ssklower #define	IFF_LINK0	0x1000		/* per link layer defined bit */
14652026Ssklower #define	IFF_LINK1	0x2000		/* per link layer defined bit */
14752026Ssklower #define	IFF_LINK2	0x4000		/* per link layer defined bit */
14854718Ssklower #define	IFF_MULTICAST	0x8000		/* supports multicast */
14935795Skarels 
15026092Skarels /* flags set internally only: */
15145161Skarels #define	IFF_CANTCHANGE \
15254718Ssklower 	(IFF_BROADCAST|IFF_POINTOPOINT|IFF_RUNNING|IFF_OACTIVE|\
15359008Ssklower 	    IFF_SIMPLEX|IFF_MULTICAST|IFF_ALLMULTI)
1546333Ssam 
1555104Swnj /*
1565104Swnj  * Output queues (ifp->if_snd) and internetwork datagram level (pup level 1)
1575104Swnj  * input routines have queues of messages stored on ifqueue structures
1585104Swnj  * (defined above).  Entries are added to and deleted from these structures
1595104Swnj  * by these macros, which should be called with ipl raised to splimp().
1605104Swnj  */
1616207Swnj #define	IF_QFULL(ifq)		((ifq)->ifq_len >= (ifq)->ifq_maxlen)
1626207Swnj #define	IF_DROP(ifq)		((ifq)->ifq_drops++)
1635083Swnj #define	IF_ENQUEUE(ifq, m) { \
16443059Skarels 	(m)->m_nextpkt = 0; \
1655083Swnj 	if ((ifq)->ifq_tail == 0) \
1665160Swnj 		(ifq)->ifq_head = m; \
1675083Swnj 	else \
16843059Skarels 		(ifq)->ifq_tail->m_nextpkt = m; \
1695160Swnj 	(ifq)->ifq_tail = m; \
1706207Swnj 	(ifq)->ifq_len++; \
1715083Swnj }
1725613Swnj #define	IF_PREPEND(ifq, m) { \
17343059Skarels 	(m)->m_nextpkt = (ifq)->ifq_head; \
1746087Sroot 	if ((ifq)->ifq_tail == 0) \
1756087Sroot 		(ifq)->ifq_tail = (m); \
1765613Swnj 	(ifq)->ifq_head = (m); \
1776207Swnj 	(ifq)->ifq_len++; \
1785613Swnj }
1795083Swnj #define	IF_DEQUEUE(ifq, m) { \
1805083Swnj 	(m) = (ifq)->ifq_head; \
1815083Swnj 	if (m) { \
18243059Skarels 		if (((ifq)->ifq_head = (m)->m_nextpkt) == 0) \
1835083Swnj 			(ifq)->ifq_tail = 0; \
18443059Skarels 		(m)->m_nextpkt = 0; \
1856207Swnj 		(ifq)->ifq_len--; \
1865083Swnj 	} \
1875083Swnj }
1884945Swnj 
1896207Swnj #define	IFQ_MAXLEN	50
1907264Ssam #define	IFNET_SLOWHZ	1		/* granularity is 1 second */
1916207Swnj 
19211576Ssam /*
19318408Skarels  * The ifaddr structure contains information about one address
19418408Skarels  * of an interface.  They are maintained by the different address families,
19518408Skarels  * are allocated and attached when an address is set, and are linked
19618408Skarels  * together so all addresses for an interface can be located.
19718408Skarels  */
19818408Skarels struct ifaddr {
19937472Ssklower 	struct	sockaddr *ifa_addr;	/* address of interface */
20037472Ssklower 	struct	sockaddr *ifa_dstaddr;	/* other end of p-to-p link */
20137472Ssklower #define	ifa_broadaddr	ifa_dstaddr	/* broadcast address interface */
20237472Ssklower 	struct	sockaddr *ifa_netmask;	/* used to determine subnet */
20318408Skarels 	struct	ifnet *ifa_ifp;		/* back-pointer to interface */
20418408Skarels 	struct	ifaddr *ifa_next;	/* next address for interface */
20554718Ssklower 	void	(*ifa_rtrequest)();	/* check or clean routes (+ or -)'d */
20640789Ssklower 	u_short	ifa_flags;		/* mostly rt_flags for cloning */
20752269Ssklower 	short	ifa_refcnt;		/* extra to malloc for link info */
20852566Ssklower 	int	ifa_metric;		/* cost of going out this interface */
20954718Ssklower #ifdef notdef
21054718Ssklower 	struct	rtentry *ifa_rt;	/* XXXX for ROUTETOIF ????? */
21154718Ssklower #endif
21218408Skarels };
21354718Ssklower #define	IFA_ROUTE	RTF_UP		/* route installed */
21452566Ssklower 
21518408Skarels /*
21652566Ssklower  * Message format for use in obtaining information about interfaces
21752566Ssklower  * from getkerninfo and the routing socket
21852566Ssklower  */
21952566Ssklower struct if_msghdr {
22052566Ssklower 	u_short	ifm_msglen;	/* to skip over non-understood messages */
22152566Ssklower 	u_char	ifm_version;	/* future binary compatability */
22252566Ssklower 	u_char	ifm_type;	/* message type */
22352566Ssklower 	int	ifm_addrs;	/* like rtm_addrs */
22452566Ssklower 	int	ifm_flags;	/* value of if_flags */
22552566Ssklower 	u_short	ifm_index;	/* index for associated ifp */
22652566Ssklower 	struct	if_data ifm_data;/* statistics and other data about if */
22752566Ssklower };
22852566Ssklower 
22952566Ssklower /*
23052566Ssklower  * Message format for use in obtaining information about interface addresses
23152566Ssklower  * from getkerninfo and the routing socket
23252566Ssklower  */
23352566Ssklower struct ifa_msghdr {
23452566Ssklower 	u_short	ifam_msglen;	/* to skip over non-understood messages */
23552566Ssklower 	u_char	ifam_version;	/* future binary compatability */
23652566Ssklower 	u_char	ifam_type;	/* message type */
23752566Ssklower 	int	ifam_addrs;	/* like rtm_addrs */
23852566Ssklower 	int	ifam_flags;	/* value of ifa_flags */
23952566Ssklower 	u_short	ifam_index;	/* index for associated ifp */
24052566Ssklower 	int	ifam_metric;	/* value of ifa_metric */
24152566Ssklower };
24252566Ssklower 
24352566Ssklower /*
24413049Ssam  * Interface request structure used for socket
24513049Ssam  * ioctl's.  All interface ioctl's must have parameter
24613049Ssam  * definitions which begin with ifr_name.  The
24713049Ssam  * remainder may be interface specific.
24811576Ssam  */
24911576Ssam struct	ifreq {
25013049Ssam #define	IFNAMSIZ	16
25113049Ssam 	char	ifr_name[IFNAMSIZ];		/* if name, e.g. "en0" */
25211576Ssam 	union {
25311576Ssam 		struct	sockaddr ifru_addr;
25411576Ssam 		struct	sockaddr ifru_dstaddr;
25516387Skarels 		struct	sockaddr ifru_broadaddr;
25611576Ssam 		short	ifru_flags;
25726092Skarels 		int	ifru_metric;
25813049Ssam 		caddr_t	ifru_data;
25911576Ssam 	} ifr_ifru;
26011576Ssam #define	ifr_addr	ifr_ifru.ifru_addr	/* address */
26111576Ssam #define	ifr_dstaddr	ifr_ifru.ifru_dstaddr	/* other end of p-to-p link */
26216387Skarels #define	ifr_broadaddr	ifr_ifru.ifru_broadaddr	/* broadcast address */
26311576Ssam #define	ifr_flags	ifr_ifru.ifru_flags	/* flags */
26426092Skarels #define	ifr_metric	ifr_ifru.ifru_metric	/* metric */
26513049Ssam #define	ifr_data	ifr_ifru.ifru_data	/* for use by interface */
26611576Ssam };
26711576Ssam 
26837472Ssklower struct ifaliasreq {
26937472Ssklower 	char	ifra_name[IFNAMSIZ];		/* if name, e.g. "en0" */
27037472Ssklower 	struct	sockaddr ifra_addr;
27137472Ssklower 	struct	sockaddr ifra_broadaddr;
27237472Ssklower 	struct	sockaddr ifra_mask;
27337472Ssklower };
27437472Ssklower 
27511576Ssam /*
27611576Ssam  * Structure used in SIOCGIFCONF request.
27711576Ssam  * Used to retrieve interface configuration
27811576Ssam  * for machine (useful for programs which
27911576Ssam  * must know all networks accessible).
28011576Ssam  */
28111576Ssam struct	ifconf {
28211576Ssam 	int	ifc_len;		/* size of associated buffer */
28311576Ssam 	union {
28411576Ssam 		caddr_t	ifcu_buf;
28511576Ssam 		struct	ifreq *ifcu_req;
28611576Ssam 	} ifc_ifcu;
28711576Ssam #define	ifc_buf	ifc_ifcu.ifcu_buf	/* buffer address */
28811576Ssam #define	ifc_req	ifc_ifcu.ifcu_req	/* array of structures returned */
28911576Ssam };
29011576Ssam 
29156529Sbostic #include <net/if_arp.h>
29256529Sbostic 
2934945Swnj #ifdef KERNEL
29452026Ssklower #define	IFAFREE(ifa) \
29552269Ssklower 	if ((ifa)->ifa_refcnt <= 0) \
29652026Ssklower 		ifafree(ifa); \
29752026Ssklower 	else \
29852026Ssklower 		(ifa)->ifa_refcnt--;
29952026Ssklower 
30052026Ssklower struct	ifnet	*ifnet;
30161343Sbostic 
30261343Sbostic void	ether_ifattach __P((struct ifnet *));
30361343Sbostic void	ether_input __P((struct ifnet *, struct ether_header *, struct mbuf *));
30461343Sbostic int	ether_output __P((struct ifnet *,
30561343Sbostic 	   struct mbuf *, struct sockaddr *, struct rtentry *));
30661343Sbostic char	*ether_sprintf __P((u_char *));
30761343Sbostic 
30852269Ssklower void	if_attach __P((struct ifnet *));
30961343Sbostic void	if_down __P((struct ifnet *));
31061343Sbostic void	if_qflush __P((struct ifqueue *));
31161343Sbostic void	if_slowtimo __P((void *));
31261343Sbostic void	if_up __P((struct ifnet *));
31361343Sbostic #ifdef vax
31461343Sbostic void	ifubareset __P((int));
31556529Sbostic #endif
31661343Sbostic int	ifconf __P((int, caddr_t));
31761343Sbostic void	ifinit __P((void));
31868168Scgd int	ifioctl __P((struct socket *, u_long, caddr_t, struct proc *));
31961343Sbostic int	ifpromisc __P((struct ifnet *, int));
32061343Sbostic struct	ifnet *ifunit __P((char *));
32161343Sbostic 
32261343Sbostic struct	ifaddr *ifa_ifwithaddr __P((struct sockaddr *));
32361343Sbostic struct	ifaddr *ifa_ifwithaf __P((int));
32461343Sbostic struct	ifaddr *ifa_ifwithdstaddr __P((struct sockaddr *));
32561343Sbostic struct	ifaddr *ifa_ifwithnet __P((struct sockaddr *));
32661343Sbostic struct	ifaddr *ifa_ifwithroute __P((int, struct sockaddr *,
32761343Sbostic 					struct sockaddr *));
32861343Sbostic struct	ifaddr *ifaof_ifpforaddr __P((struct sockaddr *, struct ifnet *));
32961343Sbostic void	ifafree __P((struct ifaddr *));
33061343Sbostic void	link_rtrequest __P((int, struct rtentry *, struct sockaddr *));
33161343Sbostic 
33268168Scgd int	loioctl __P((struct ifnet *, u_long, caddr_t));
33361343Sbostic void	loopattach __P((int));
33461343Sbostic int	looutput __P((struct ifnet *,
33561343Sbostic 	   struct mbuf *, struct sockaddr *, struct rtentry *));
33661343Sbostic void	lortrequest __P((int, struct rtentry *, struct sockaddr *));
33761343Sbostic #endif
338