xref: /csrg-svn/sys/net/if.h (revision 28153)
123158Smckusick /*
225971Skarels  * Copyright (c) 1982 Regents of the University of California.
323158Smckusick  * All rights reserved.  The Berkeley software License Agreement
423158Smckusick  * specifies the terms and conditions for redistribution.
523158Smckusick  *
6*28153Skarels  *	@(#)if.h	6.13 (Berkeley) 05/14/86
723158Smckusick  */
84945Swnj 
94945Swnj /*
105104Swnj  * Structures defining a network interface, providing a packet
115104Swnj  * transport mechanism (ala level 0 of the PUP protocols).
125104Swnj  *
135104Swnj  * Each interface accepts output datagrams of a specified maximum
145104Swnj  * length, and provides higher level routines with input datagrams
155104Swnj  * received from its medium.
165104Swnj  *
175104Swnj  * Output occurs when the routine if_output is called, with three parameters:
186333Ssam  *	(*ifp->if_output)(ifp, m, dst)
196333Ssam  * Here m is the mbuf chain to be sent and dst is the destination address.
206333Ssam  * The output routine encapsulates the supplied datagram if necessary,
216333Ssam  * and then transmits it on its medium.
225104Swnj  *
235104Swnj  * On input, each interface unwraps the data received by it, and either
245104Swnj  * places it on the input queue of a internetwork datagram routine
255104Swnj  * and posts the associated software interrupt, or passes the datagram to a raw
265104Swnj  * packet input routine.
275104Swnj  *
286333Ssam  * Routines exist for locating interfaces by their addresses
295104Swnj  * or for locating a interface on a certain network, as well as more general
305104Swnj  * routing and gateway routines maintaining information used to locate
316333Ssam  * interfaces.  These routines live in the files if.c and route.c
325083Swnj  */
335083Swnj 
345083Swnj /*
355083Swnj  * Structure defining a queue for a network interface.
364945Swnj  *
374945Swnj  * (Would like to call this struct ``if'', but C isn't PL/1.)
384945Swnj  */
394945Swnj struct ifnet {
405171Swnj 	char	*if_name;		/* name, e.g. ``en'' or ``lo'' */
414945Swnj 	short	if_unit;		/* sub-unit for lower level driver */
424945Swnj 	short	if_mtu;			/* maximum transmission unit */
436333Ssam 	short	if_flags;		/* up/down, broadcast, etc. */
447264Ssam 	short	if_timer;		/* time 'til if_watchdog called */
4526092Skarels 	int	if_metric;		/* routing metric (external only) */
4618408Skarels 	struct	ifaddr *if_addrlist;	/* linked list of addresses per if */
475104Swnj 	struct	ifqueue {
485104Swnj 		struct	mbuf *ifq_head;
495104Swnj 		struct	mbuf *ifq_tail;
506207Swnj 		int	ifq_len;
516207Swnj 		int	ifq_maxlen;
526207Swnj 		int	ifq_drops;
535104Swnj 	} if_snd;			/* output queue */
545104Swnj /* procedure handles */
555104Swnj 	int	(*if_init)();		/* init routine */
565083Swnj 	int	(*if_output)();		/* output routine */
5713049Ssam 	int	(*if_ioctl)();		/* ioctl routine */
588972Sroot 	int	(*if_reset)();		/* bus reset routine */
597264Ssam 	int	(*if_watchdog)();	/* timer routine */
605104Swnj /* generic interface statistics */
615171Swnj 	int	if_ipackets;		/* packets received on interface */
625171Swnj 	int	if_ierrors;		/* input errors on interface */
635171Swnj 	int	if_opackets;		/* packets sent on interface */
645171Swnj 	int	if_oerrors;		/* output errors on interface */
655104Swnj 	int	if_collisions;		/* collisions on csma interfaces */
665104Swnj /* end statistics */
674951Swnj 	struct	ifnet *if_next;
684945Swnj };
694945Swnj 
706333Ssam #define	IFF_UP		0x1		/* interface is up */
716333Ssam #define	IFF_BROADCAST	0x2		/* broadcast address valid */
726333Ssam #define	IFF_DEBUG	0x4		/* turn on debugging */
7326092Skarels #define	IFF_LOOPBACK	0x8		/* is a loopback net */
746924Swnj #define	IFF_POINTOPOINT	0x10		/* interface is point-to-point link */
7513049Ssam #define	IFF_NOTRAILERS	0x20		/* avoid use of trailers */
7613053Ssam #define	IFF_RUNNING	0x40		/* resources allocated */
7714866Ssam #define	IFF_NOARP	0x80		/* no address resolution protocol */
78*28153Skarels /* next two not supported now, but reserved: */
79*28153Skarels #define	IFF_PROMISC	0x100		/* receive all packets */
80*28153Skarels #define	IFF_ALLMULTI	0x200		/* receive all multicast packets */
8126092Skarels /* flags set internally only: */
8218408Skarels #define	IFF_CANTCHANGE	(IFF_BROADCAST | IFF_POINTOPOINT | IFF_RUNNING)
836333Ssam 
845104Swnj /*
855104Swnj  * Output queues (ifp->if_snd) and internetwork datagram level (pup level 1)
865104Swnj  * input routines have queues of messages stored on ifqueue structures
875104Swnj  * (defined above).  Entries are added to and deleted from these structures
885104Swnj  * by these macros, which should be called with ipl raised to splimp().
895104Swnj  */
906207Swnj #define	IF_QFULL(ifq)		((ifq)->ifq_len >= (ifq)->ifq_maxlen)
916207Swnj #define	IF_DROP(ifq)		((ifq)->ifq_drops++)
925083Swnj #define	IF_ENQUEUE(ifq, m) { \
935083Swnj 	(m)->m_act = 0; \
945083Swnj 	if ((ifq)->ifq_tail == 0) \
955160Swnj 		(ifq)->ifq_head = m; \
965083Swnj 	else \
975083Swnj 		(ifq)->ifq_tail->m_act = m; \
985160Swnj 	(ifq)->ifq_tail = m; \
996207Swnj 	(ifq)->ifq_len++; \
1005083Swnj }
1015613Swnj #define	IF_PREPEND(ifq, m) { \
1025613Swnj 	(m)->m_act = (ifq)->ifq_head; \
1036087Sroot 	if ((ifq)->ifq_tail == 0) \
1046087Sroot 		(ifq)->ifq_tail = (m); \
1055613Swnj 	(ifq)->ifq_head = (m); \
1066207Swnj 	(ifq)->ifq_len++; \
1075613Swnj }
10824772Skarels /*
10924772Skarels  * Packets destined for level-1 protocol input routines
11024772Skarels  * have a pointer to the receiving interface prepended to the data.
11124772Skarels  * IF_DEQUEUEIF extracts and returns this pointer when dequeueing the packet.
11224772Skarels  * IF_ADJ should be used otherwise to adjust for its presence.
11324772Skarels  */
11424772Skarels #define	IF_ADJ(m) { \
11524772Skarels 	(m)->m_off += sizeof(struct ifnet *); \
11624772Skarels 	(m)->m_len -= sizeof(struct ifnet *); \
11724772Skarels 	if ((m)->m_len == 0) { \
11824772Skarels 		struct mbuf *n; \
11924772Skarels 		MFREE((m), n); \
12024772Skarels 		(m) = n; \
12124772Skarels 	} \
12224772Skarels }
12324772Skarels #define	IF_DEQUEUEIF(ifq, m, ifp) { \
12424772Skarels 	(m) = (ifq)->ifq_head; \
12524772Skarels 	if (m) { \
12624772Skarels 		if (((ifq)->ifq_head = (m)->m_act) == 0) \
12724772Skarels 			(ifq)->ifq_tail = 0; \
12824772Skarels 		(m)->m_act = 0; \
12924772Skarels 		(ifq)->ifq_len--; \
13024772Skarels 		(ifp) = *(mtod((m), struct ifnet **)); \
13124772Skarels 		IF_ADJ(m); \
13224772Skarels 	} \
13324772Skarels }
1345083Swnj #define	IF_DEQUEUE(ifq, m) { \
1355083Swnj 	(m) = (ifq)->ifq_head; \
1365083Swnj 	if (m) { \
1375083Swnj 		if (((ifq)->ifq_head = (m)->m_act) == 0) \
1385083Swnj 			(ifq)->ifq_tail = 0; \
1395083Swnj 		(m)->m_act = 0; \
1406207Swnj 		(ifq)->ifq_len--; \
1415083Swnj 	} \
1425083Swnj }
1434945Swnj 
1446207Swnj #define	IFQ_MAXLEN	50
1457264Ssam #define	IFNET_SLOWHZ	1		/* granularity is 1 second */
1466207Swnj 
14711576Ssam /*
14818408Skarels  * The ifaddr structure contains information about one address
14918408Skarels  * of an interface.  They are maintained by the different address families,
15018408Skarels  * are allocated and attached when an address is set, and are linked
15118408Skarels  * together so all addresses for an interface can be located.
15218408Skarels  */
15318408Skarels struct ifaddr {
15418408Skarels 	struct	sockaddr ifa_addr;	/* address of interface */
15518408Skarels 	union {
15618408Skarels 		struct	sockaddr ifu_broadaddr;
15718408Skarels 		struct	sockaddr ifu_dstaddr;
15818408Skarels 	} ifa_ifu;
15918408Skarels #define	ifa_broadaddr	ifa_ifu.ifu_broadaddr	/* broadcast address */
16018408Skarels #define	ifa_dstaddr	ifa_ifu.ifu_dstaddr	/* other end of p-to-p link */
16118408Skarels 	struct	ifnet *ifa_ifp;		/* back-pointer to interface */
16218408Skarels 	struct	ifaddr *ifa_next;	/* next address for interface */
16318408Skarels };
16418408Skarels 
16518408Skarels /*
16613049Ssam  * Interface request structure used for socket
16713049Ssam  * ioctl's.  All interface ioctl's must have parameter
16813049Ssam  * definitions which begin with ifr_name.  The
16913049Ssam  * remainder may be interface specific.
17011576Ssam  */
17111576Ssam struct	ifreq {
17213049Ssam #define	IFNAMSIZ	16
17313049Ssam 	char	ifr_name[IFNAMSIZ];		/* if name, e.g. "en0" */
17411576Ssam 	union {
17511576Ssam 		struct	sockaddr ifru_addr;
17611576Ssam 		struct	sockaddr ifru_dstaddr;
17716387Skarels 		struct	sockaddr ifru_broadaddr;
17811576Ssam 		short	ifru_flags;
17926092Skarels 		int	ifru_metric;
18013049Ssam 		caddr_t	ifru_data;
18111576Ssam 	} ifr_ifru;
18211576Ssam #define	ifr_addr	ifr_ifru.ifru_addr	/* address */
18311576Ssam #define	ifr_dstaddr	ifr_ifru.ifru_dstaddr	/* other end of p-to-p link */
18416387Skarels #define	ifr_broadaddr	ifr_ifru.ifru_broadaddr	/* broadcast address */
18511576Ssam #define	ifr_flags	ifr_ifru.ifru_flags	/* flags */
18626092Skarels #define	ifr_metric	ifr_ifru.ifru_metric	/* metric */
18713049Ssam #define	ifr_data	ifr_ifru.ifru_data	/* for use by interface */
18811576Ssam };
18911576Ssam 
19011576Ssam /*
19111576Ssam  * Structure used in SIOCGIFCONF request.
19211576Ssam  * Used to retrieve interface configuration
19311576Ssam  * for machine (useful for programs which
19411576Ssam  * must know all networks accessible).
19511576Ssam  */
19611576Ssam struct	ifconf {
19711576Ssam 	int	ifc_len;		/* size of associated buffer */
19811576Ssam 	union {
19911576Ssam 		caddr_t	ifcu_buf;
20011576Ssam 		struct	ifreq *ifcu_req;
20111576Ssam 	} ifc_ifcu;
20211576Ssam #define	ifc_buf	ifc_ifcu.ifcu_buf	/* buffer address */
20311576Ssam #define	ifc_req	ifc_ifcu.ifcu_req	/* array of structures returned */
20411576Ssam };
20511576Ssam 
2064945Swnj #ifdef KERNEL
20725971Skarels #include "../net/if_arp.h"
20825650Skarels #ifdef INET
2095083Swnj struct	ifqueue	ipintrq;		/* ip packet input queue */
2105083Swnj #endif
2116333Ssam struct	ifqueue rawintrq;		/* raw packet input queue */
2124945Swnj struct	ifnet *ifnet;
21318408Skarels struct	ifaddr *ifa_ifwithaddr(), *ifa_ifwithnet(), *ifa_ifwithaf();
21427197Skarels struct	ifaddr *ifa_ifwithdstaddr();
21525971Skarels #else KERNEL
21625971Skarels #include <net/if_arp.h>
21725971Skarels #endif KERNEL
218