xref: /csrg-svn/sys/net/if.h (revision 40789)
123158Smckusick /*
239182Ssklower  * Copyright (c) 1982, 1986, 1989 Regents of the University of California.
333183Sbostic  * All rights reserved.
423158Smckusick  *
533183Sbostic  * Redistribution and use in source and binary forms are permitted
634844Sbostic  * provided that the above copyright notice and this paragraph are
734844Sbostic  * duplicated in all such forms and that any documentation,
834844Sbostic  * advertising materials, and other materials related to such
934844Sbostic  * distribution and use acknowledge that the software was developed
1034844Sbostic  * by the University of California, Berkeley.  The name of the
1134844Sbostic  * University may not be used to endorse or promote products derived
1234844Sbostic  * from this software without specific prior written permission.
1334844Sbostic  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
1434844Sbostic  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
1534844Sbostic  * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
1633183Sbostic  *
17*40789Ssklower  *	@(#)if.h	7.7 (Berkeley) 04/05/90
1823158Smckusick  */
194945Swnj 
204945Swnj /*
215104Swnj  * Structures defining a network interface, providing a packet
225104Swnj  * transport mechanism (ala level 0 of the PUP protocols).
235104Swnj  *
245104Swnj  * Each interface accepts output datagrams of a specified maximum
255104Swnj  * length, and provides higher level routines with input datagrams
265104Swnj  * received from its medium.
275104Swnj  *
285104Swnj  * Output occurs when the routine if_output is called, with three parameters:
296333Ssam  *	(*ifp->if_output)(ifp, m, dst)
306333Ssam  * Here m is the mbuf chain to be sent and dst is the destination address.
316333Ssam  * The output routine encapsulates the supplied datagram if necessary,
326333Ssam  * and then transmits it on its medium.
335104Swnj  *
345104Swnj  * On input, each interface unwraps the data received by it, and either
355104Swnj  * places it on the input queue of a internetwork datagram routine
365104Swnj  * and posts the associated software interrupt, or passes the datagram to a raw
375104Swnj  * packet input routine.
385104Swnj  *
396333Ssam  * Routines exist for locating interfaces by their addresses
405104Swnj  * or for locating a interface on a certain network, as well as more general
415104Swnj  * routing and gateway routines maintaining information used to locate
426333Ssam  * interfaces.  These routines live in the files if.c and route.c
435083Swnj  */
4439182Ssklower #ifndef _TIME_ /*  XXX fast fix for SNMP, going away soon */
4539182Ssklower #ifdef KERNEL
4639182Ssklower #include "../sys/time.h"
4739182Ssklower #else
4839182Ssklower #include <sys/time.h>
4939182Ssklower #endif
5039182Ssklower #endif
515083Swnj 
525083Swnj /*
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'' */
604945Swnj 	short	if_unit;		/* sub-unit for lower level driver */
614945Swnj 	short	if_mtu;			/* maximum transmission unit */
626333Ssam 	short	if_flags;		/* up/down, broadcast, etc. */
637264Ssam 	short	if_timer;		/* time 'til if_watchdog called */
6426092Skarels 	int	if_metric;		/* routing metric (external only) */
6518408Skarels 	struct	ifaddr *if_addrlist;	/* linked list of addresses per if */
665104Swnj 	struct	ifqueue {
675104Swnj 		struct	mbuf *ifq_head;
685104Swnj 		struct	mbuf *ifq_tail;
696207Swnj 		int	ifq_len;
706207Swnj 		int	ifq_maxlen;
716207Swnj 		int	ifq_drops;
725104Swnj 	} if_snd;			/* output queue */
735104Swnj /* procedure handles */
745104Swnj 	int	(*if_init)();		/* init routine */
7535795Skarels 	int	(*if_output)();		/* output routine (enqueue) */
7635795Skarels 	int	(*if_start)();		/* initiate output routine */
7735795Skarels 	int	(*if_done)();		/* output complete routine */
7813049Ssam 	int	(*if_ioctl)();		/* ioctl routine */
798972Sroot 	int	(*if_reset)();		/* bus reset routine */
807264Ssam 	int	(*if_watchdog)();	/* timer routine */
815104Swnj /* generic interface statistics */
825171Swnj 	int	if_ipackets;		/* packets received on interface */
835171Swnj 	int	if_ierrors;		/* input errors on interface */
845171Swnj 	int	if_opackets;		/* packets sent on interface */
855171Swnj 	int	if_oerrors;		/* output errors on interface */
865104Swnj 	int	if_collisions;		/* collisions on csma interfaces */
875104Swnj /* end statistics */
884951Swnj 	struct	ifnet *if_next;
8937472Ssklower 	u_char	if_type;		/* ethernet, tokenring, etc */
9037472Ssklower 	u_char	if_addrlen;		/* media address length */
9137472Ssklower 	u_char	if_hdrlen;		/* media header length */
92*40789Ssklower 	u_char	if_index;		/* numeric abbreviation for this if  */
9339182Ssklower /* more statistics here to avoid recompiling netstat */
9439182Ssklower 	struct	timeval if_lastchange;	/* last updated */
9539182Ssklower 	int	if_ibytes;		/* total number of octets received */
9639182Ssklower 	int	if_obytes;		/* total number of octets sent */
9739182Ssklower 	int	if_imcasts;		/* packets received via multicast */
9839182Ssklower 	int	if_omcasts;		/* packets sent via multicast */
9939182Ssklower 	int	if_iqdrops;		/* dropped on input, this interface */
10039182Ssklower 	int	if_noproto;		/* destined for unsupported protocol */
10139182Ssklower 	int	if_baudrate;		/* linespeed */
1024945Swnj };
1034945Swnj 
1046333Ssam #define	IFF_UP		0x1		/* interface is up */
1056333Ssam #define	IFF_BROADCAST	0x2		/* broadcast address valid */
1066333Ssam #define	IFF_DEBUG	0x4		/* turn on debugging */
10726092Skarels #define	IFF_LOOPBACK	0x8		/* is a loopback net */
1086924Swnj #define	IFF_POINTOPOINT	0x10		/* interface is point-to-point link */
10913049Ssam #define	IFF_NOTRAILERS	0x20		/* avoid use of trailers */
11013053Ssam #define	IFF_RUNNING	0x40		/* resources allocated */
11114866Ssam #define	IFF_NOARP	0x80		/* no address resolution protocol */
11228153Skarels /* next two not supported now, but reserved: */
11328153Skarels #define	IFF_PROMISC	0x100		/* receive all packets */
11428153Skarels #define	IFF_ALLMULTI	0x200		/* receive all multicast packets */
11535795Skarels #define	IFF_OACTIVE	0x400		/* transmission in progress */
11635795Skarels #define	IFF_SIMPLEX	0x800		/* can't hear own transmissions */
11735795Skarels 
11826092Skarels /* flags set internally only: */
11935795Skarels #define	IFF_CANTCHANGE	(IFF_BROADCAST|IFF_POINTOPOINT|IFF_RUNNING|IFF_OACTIVE)
1206333Ssam 
1215104Swnj /*
1225104Swnj  * Output queues (ifp->if_snd) and internetwork datagram level (pup level 1)
1235104Swnj  * input routines have queues of messages stored on ifqueue structures
1245104Swnj  * (defined above).  Entries are added to and deleted from these structures
1255104Swnj  * by these macros, which should be called with ipl raised to splimp().
1265104Swnj  */
1276207Swnj #define	IF_QFULL(ifq)		((ifq)->ifq_len >= (ifq)->ifq_maxlen)
1286207Swnj #define	IF_DROP(ifq)		((ifq)->ifq_drops++)
1295083Swnj #define	IF_ENQUEUE(ifq, m) { \
1305083Swnj 	(m)->m_act = 0; \
1315083Swnj 	if ((ifq)->ifq_tail == 0) \
1325160Swnj 		(ifq)->ifq_head = m; \
1335083Swnj 	else \
1345083Swnj 		(ifq)->ifq_tail->m_act = m; \
1355160Swnj 	(ifq)->ifq_tail = m; \
1366207Swnj 	(ifq)->ifq_len++; \
1375083Swnj }
1385613Swnj #define	IF_PREPEND(ifq, m) { \
1395613Swnj 	(m)->m_act = (ifq)->ifq_head; \
1406087Sroot 	if ((ifq)->ifq_tail == 0) \
1416087Sroot 		(ifq)->ifq_tail = (m); \
1425613Swnj 	(ifq)->ifq_head = (m); \
1436207Swnj 	(ifq)->ifq_len++; \
1445613Swnj }
1455083Swnj #define	IF_DEQUEUE(ifq, m) { \
1465083Swnj 	(m) = (ifq)->ifq_head; \
1475083Swnj 	if (m) { \
1485083Swnj 		if (((ifq)->ifq_head = (m)->m_act) == 0) \
1495083Swnj 			(ifq)->ifq_tail = 0; \
1505083Swnj 		(m)->m_act = 0; \
1516207Swnj 		(ifq)->ifq_len--; \
1525083Swnj 	} \
1535083Swnj }
1544945Swnj 
1556207Swnj #define	IFQ_MAXLEN	50
1567264Ssam #define	IFNET_SLOWHZ	1		/* granularity is 1 second */
1576207Swnj 
15811576Ssam /*
15918408Skarels  * The ifaddr structure contains information about one address
16018408Skarels  * of an interface.  They are maintained by the different address families,
16118408Skarels  * are allocated and attached when an address is set, and are linked
16218408Skarels  * together so all addresses for an interface can be located.
16318408Skarels  */
16418408Skarels struct ifaddr {
16537472Ssklower 	struct	sockaddr *ifa_addr;	/* address of interface */
16637472Ssklower 	struct	sockaddr *ifa_dstaddr;	/* other end of p-to-p link */
16737472Ssklower #define	ifa_broadaddr	ifa_dstaddr	/* broadcast address interface */
16837472Ssklower 	struct	sockaddr *ifa_netmask;	/* used to determine subnet */
16918408Skarels 	struct	ifnet *ifa_ifp;		/* back-pointer to interface */
17018408Skarels 	struct	ifaddr *ifa_next;	/* next address for interface */
171*40789Ssklower 	int	(*ifa_rtrequest)();	/* check or clean routes (+ or -)'d */
172*40789Ssklower 	struct 	rtentry *ifa_rt;	/* ??? for ROUTETOIF */
173*40789Ssklower 	u_short	ifa_flags;		/* mostly rt_flags for cloning */
174*40789Ssklower 	u_short	ifa_llinfolen;		/* extra to malloc for link info */
17518408Skarels };
176*40789Ssklower #define IFA_ROUTE	RTF_UP		/* route installed */
17718408Skarels /*
17813049Ssam  * Interface request structure used for socket
17913049Ssam  * ioctl's.  All interface ioctl's must have parameter
18013049Ssam  * definitions which begin with ifr_name.  The
18113049Ssam  * remainder may be interface specific.
18211576Ssam  */
18311576Ssam struct	ifreq {
18413049Ssam #define	IFNAMSIZ	16
18513049Ssam 	char	ifr_name[IFNAMSIZ];		/* if name, e.g. "en0" */
18611576Ssam 	union {
18711576Ssam 		struct	sockaddr ifru_addr;
18811576Ssam 		struct	sockaddr ifru_dstaddr;
18916387Skarels 		struct	sockaddr ifru_broadaddr;
19011576Ssam 		short	ifru_flags;
19126092Skarels 		int	ifru_metric;
19213049Ssam 		caddr_t	ifru_data;
19311576Ssam 	} ifr_ifru;
19411576Ssam #define	ifr_addr	ifr_ifru.ifru_addr	/* address */
19511576Ssam #define	ifr_dstaddr	ifr_ifru.ifru_dstaddr	/* other end of p-to-p link */
19616387Skarels #define	ifr_broadaddr	ifr_ifru.ifru_broadaddr	/* broadcast address */
19711576Ssam #define	ifr_flags	ifr_ifru.ifru_flags	/* flags */
19826092Skarels #define	ifr_metric	ifr_ifru.ifru_metric	/* metric */
19913049Ssam #define	ifr_data	ifr_ifru.ifru_data	/* for use by interface */
20011576Ssam };
20111576Ssam 
20237472Ssklower struct ifaliasreq {
20337472Ssklower 	char	ifra_name[IFNAMSIZ];		/* if name, e.g. "en0" */
20437472Ssklower 	struct	sockaddr ifra_addr;
20537472Ssklower 	struct	sockaddr ifra_broadaddr;
20637472Ssklower 	struct	sockaddr ifra_mask;
20737472Ssklower };
20837472Ssklower 
20911576Ssam /*
21011576Ssam  * Structure used in SIOCGIFCONF request.
21111576Ssam  * Used to retrieve interface configuration
21211576Ssam  * for machine (useful for programs which
21311576Ssam  * must know all networks accessible).
21411576Ssam  */
21511576Ssam struct	ifconf {
21611576Ssam 	int	ifc_len;		/* size of associated buffer */
21711576Ssam 	union {
21811576Ssam 		caddr_t	ifcu_buf;
21911576Ssam 		struct	ifreq *ifcu_req;
22011576Ssam 	} ifc_ifcu;
22111576Ssam #define	ifc_buf	ifc_ifcu.ifcu_buf	/* buffer address */
22211576Ssam #define	ifc_req	ifc_ifcu.ifcu_req	/* array of structures returned */
22311576Ssam };
22411576Ssam 
2254945Swnj #ifdef KERNEL
22625971Skarels #include "../net/if_arp.h"
2276333Ssam struct	ifqueue rawintrq;		/* raw packet input queue */
2284945Swnj struct	ifnet *ifnet;
22928943Skarels struct	ifaddr *ifa_ifwithaddr(), *ifa_ifwithnet();
23027197Skarels struct	ifaddr *ifa_ifwithdstaddr();
23125971Skarels #else KERNEL
23225971Skarels #include <net/if_arp.h>
23325971Skarels #endif KERNEL
234