xref: /csrg-svn/sys/net/if.h (revision 33183)
123158Smckusick /*
229062Smckusick  * Copyright (c) 1982, 1986 Regents of the University of California.
3*33183Sbostic  * All rights reserved.
423158Smckusick  *
5*33183Sbostic  * Redistribution and use in source and binary forms are permitted
6*33183Sbostic  * provided that this notice is preserved and that due credit is given
7*33183Sbostic  * to the University of California at Berkeley. The name of the University
8*33183Sbostic  * may not be used to endorse or promote products derived from this
9*33183Sbostic  * software without specific prior written permission. This software
10*33183Sbostic  * is provided ``as is'' without express or implied warranty.
11*33183Sbostic  *
12*33183Sbostic  *	@(#)if.h	7.2 (Berkeley) 12/30/87
1323158Smckusick  */
144945Swnj 
154945Swnj /*
165104Swnj  * Structures defining a network interface, providing a packet
175104Swnj  * transport mechanism (ala level 0 of the PUP protocols).
185104Swnj  *
195104Swnj  * Each interface accepts output datagrams of a specified maximum
205104Swnj  * length, and provides higher level routines with input datagrams
215104Swnj  * received from its medium.
225104Swnj  *
235104Swnj  * Output occurs when the routine if_output is called, with three parameters:
246333Ssam  *	(*ifp->if_output)(ifp, m, dst)
256333Ssam  * Here m is the mbuf chain to be sent and dst is the destination address.
266333Ssam  * The output routine encapsulates the supplied datagram if necessary,
276333Ssam  * and then transmits it on its medium.
285104Swnj  *
295104Swnj  * On input, each interface unwraps the data received by it, and either
305104Swnj  * places it on the input queue of a internetwork datagram routine
315104Swnj  * and posts the associated software interrupt, or passes the datagram to a raw
325104Swnj  * packet input routine.
335104Swnj  *
346333Ssam  * Routines exist for locating interfaces by their addresses
355104Swnj  * or for locating a interface on a certain network, as well as more general
365104Swnj  * routing and gateway routines maintaining information used to locate
376333Ssam  * interfaces.  These routines live in the files if.c and route.c
385083Swnj  */
395083Swnj 
405083Swnj /*
415083Swnj  * Structure defining a queue for a network interface.
424945Swnj  *
434945Swnj  * (Would like to call this struct ``if'', but C isn't PL/1.)
444945Swnj  */
454945Swnj struct ifnet {
465171Swnj 	char	*if_name;		/* name, e.g. ``en'' or ``lo'' */
474945Swnj 	short	if_unit;		/* sub-unit for lower level driver */
484945Swnj 	short	if_mtu;			/* maximum transmission unit */
496333Ssam 	short	if_flags;		/* up/down, broadcast, etc. */
507264Ssam 	short	if_timer;		/* time 'til if_watchdog called */
5126092Skarels 	int	if_metric;		/* routing metric (external only) */
5218408Skarels 	struct	ifaddr *if_addrlist;	/* linked list of addresses per if */
535104Swnj 	struct	ifqueue {
545104Swnj 		struct	mbuf *ifq_head;
555104Swnj 		struct	mbuf *ifq_tail;
566207Swnj 		int	ifq_len;
576207Swnj 		int	ifq_maxlen;
586207Swnj 		int	ifq_drops;
595104Swnj 	} if_snd;			/* output queue */
605104Swnj /* procedure handles */
615104Swnj 	int	(*if_init)();		/* init routine */
625083Swnj 	int	(*if_output)();		/* output routine */
6313049Ssam 	int	(*if_ioctl)();		/* ioctl routine */
648972Sroot 	int	(*if_reset)();		/* bus reset routine */
657264Ssam 	int	(*if_watchdog)();	/* timer routine */
665104Swnj /* generic interface statistics */
675171Swnj 	int	if_ipackets;		/* packets received on interface */
685171Swnj 	int	if_ierrors;		/* input errors on interface */
695171Swnj 	int	if_opackets;		/* packets sent on interface */
705171Swnj 	int	if_oerrors;		/* output errors on interface */
715104Swnj 	int	if_collisions;		/* collisions on csma interfaces */
725104Swnj /* end statistics */
734951Swnj 	struct	ifnet *if_next;
744945Swnj };
754945Swnj 
766333Ssam #define	IFF_UP		0x1		/* interface is up */
776333Ssam #define	IFF_BROADCAST	0x2		/* broadcast address valid */
786333Ssam #define	IFF_DEBUG	0x4		/* turn on debugging */
7926092Skarels #define	IFF_LOOPBACK	0x8		/* is a loopback net */
806924Swnj #define	IFF_POINTOPOINT	0x10		/* interface is point-to-point link */
8113049Ssam #define	IFF_NOTRAILERS	0x20		/* avoid use of trailers */
8213053Ssam #define	IFF_RUNNING	0x40		/* resources allocated */
8314866Ssam #define	IFF_NOARP	0x80		/* no address resolution protocol */
8428153Skarels /* next two not supported now, but reserved: */
8528153Skarels #define	IFF_PROMISC	0x100		/* receive all packets */
8628153Skarels #define	IFF_ALLMULTI	0x200		/* receive all multicast packets */
8726092Skarels /* flags set internally only: */
8818408Skarels #define	IFF_CANTCHANGE	(IFF_BROADCAST | IFF_POINTOPOINT | IFF_RUNNING)
896333Ssam 
905104Swnj /*
915104Swnj  * Output queues (ifp->if_snd) and internetwork datagram level (pup level 1)
925104Swnj  * input routines have queues of messages stored on ifqueue structures
935104Swnj  * (defined above).  Entries are added to and deleted from these structures
945104Swnj  * by these macros, which should be called with ipl raised to splimp().
955104Swnj  */
966207Swnj #define	IF_QFULL(ifq)		((ifq)->ifq_len >= (ifq)->ifq_maxlen)
976207Swnj #define	IF_DROP(ifq)		((ifq)->ifq_drops++)
985083Swnj #define	IF_ENQUEUE(ifq, m) { \
995083Swnj 	(m)->m_act = 0; \
1005083Swnj 	if ((ifq)->ifq_tail == 0) \
1015160Swnj 		(ifq)->ifq_head = m; \
1025083Swnj 	else \
1035083Swnj 		(ifq)->ifq_tail->m_act = m; \
1045160Swnj 	(ifq)->ifq_tail = m; \
1056207Swnj 	(ifq)->ifq_len++; \
1065083Swnj }
1075613Swnj #define	IF_PREPEND(ifq, m) { \
1085613Swnj 	(m)->m_act = (ifq)->ifq_head; \
1096087Sroot 	if ((ifq)->ifq_tail == 0) \
1106087Sroot 		(ifq)->ifq_tail = (m); \
1115613Swnj 	(ifq)->ifq_head = (m); \
1126207Swnj 	(ifq)->ifq_len++; \
1135613Swnj }
11424772Skarels /*
11524772Skarels  * Packets destined for level-1 protocol input routines
11624772Skarels  * have a pointer to the receiving interface prepended to the data.
11724772Skarels  * IF_DEQUEUEIF extracts and returns this pointer when dequeueing the packet.
11824772Skarels  * IF_ADJ should be used otherwise to adjust for its presence.
11924772Skarels  */
12024772Skarels #define	IF_ADJ(m) { \
12124772Skarels 	(m)->m_off += sizeof(struct ifnet *); \
12224772Skarels 	(m)->m_len -= sizeof(struct ifnet *); \
12324772Skarels 	if ((m)->m_len == 0) { \
12424772Skarels 		struct mbuf *n; \
12524772Skarels 		MFREE((m), n); \
12624772Skarels 		(m) = n; \
12724772Skarels 	} \
12824772Skarels }
12924772Skarels #define	IF_DEQUEUEIF(ifq, m, ifp) { \
13024772Skarels 	(m) = (ifq)->ifq_head; \
13124772Skarels 	if (m) { \
13224772Skarels 		if (((ifq)->ifq_head = (m)->m_act) == 0) \
13324772Skarels 			(ifq)->ifq_tail = 0; \
13424772Skarels 		(m)->m_act = 0; \
13524772Skarels 		(ifq)->ifq_len--; \
13624772Skarels 		(ifp) = *(mtod((m), struct ifnet **)); \
13724772Skarels 		IF_ADJ(m); \
13824772Skarels 	} \
13924772Skarels }
1405083Swnj #define	IF_DEQUEUE(ifq, m) { \
1415083Swnj 	(m) = (ifq)->ifq_head; \
1425083Swnj 	if (m) { \
1435083Swnj 		if (((ifq)->ifq_head = (m)->m_act) == 0) \
1445083Swnj 			(ifq)->ifq_tail = 0; \
1455083Swnj 		(m)->m_act = 0; \
1466207Swnj 		(ifq)->ifq_len--; \
1475083Swnj 	} \
1485083Swnj }
1494945Swnj 
1506207Swnj #define	IFQ_MAXLEN	50
1517264Ssam #define	IFNET_SLOWHZ	1		/* granularity is 1 second */
1526207Swnj 
15311576Ssam /*
15418408Skarels  * The ifaddr structure contains information about one address
15518408Skarels  * of an interface.  They are maintained by the different address families,
15618408Skarels  * are allocated and attached when an address is set, and are linked
15718408Skarels  * together so all addresses for an interface can be located.
15818408Skarels  */
15918408Skarels struct ifaddr {
16018408Skarels 	struct	sockaddr ifa_addr;	/* address of interface */
16118408Skarels 	union {
16218408Skarels 		struct	sockaddr ifu_broadaddr;
16318408Skarels 		struct	sockaddr ifu_dstaddr;
16418408Skarels 	} ifa_ifu;
16518408Skarels #define	ifa_broadaddr	ifa_ifu.ifu_broadaddr	/* broadcast address */
16618408Skarels #define	ifa_dstaddr	ifa_ifu.ifu_dstaddr	/* other end of p-to-p link */
16718408Skarels 	struct	ifnet *ifa_ifp;		/* back-pointer to interface */
16818408Skarels 	struct	ifaddr *ifa_next;	/* next address for interface */
16918408Skarels };
17018408Skarels 
17118408Skarels /*
17213049Ssam  * Interface request structure used for socket
17313049Ssam  * ioctl's.  All interface ioctl's must have parameter
17413049Ssam  * definitions which begin with ifr_name.  The
17513049Ssam  * remainder may be interface specific.
17611576Ssam  */
17711576Ssam struct	ifreq {
17813049Ssam #define	IFNAMSIZ	16
17913049Ssam 	char	ifr_name[IFNAMSIZ];		/* if name, e.g. "en0" */
18011576Ssam 	union {
18111576Ssam 		struct	sockaddr ifru_addr;
18211576Ssam 		struct	sockaddr ifru_dstaddr;
18316387Skarels 		struct	sockaddr ifru_broadaddr;
18411576Ssam 		short	ifru_flags;
18526092Skarels 		int	ifru_metric;
18613049Ssam 		caddr_t	ifru_data;
18711576Ssam 	} ifr_ifru;
18811576Ssam #define	ifr_addr	ifr_ifru.ifru_addr	/* address */
18911576Ssam #define	ifr_dstaddr	ifr_ifru.ifru_dstaddr	/* other end of p-to-p link */
19016387Skarels #define	ifr_broadaddr	ifr_ifru.ifru_broadaddr	/* broadcast address */
19111576Ssam #define	ifr_flags	ifr_ifru.ifru_flags	/* flags */
19226092Skarels #define	ifr_metric	ifr_ifru.ifru_metric	/* metric */
19313049Ssam #define	ifr_data	ifr_ifru.ifru_data	/* for use by interface */
19411576Ssam };
19511576Ssam 
19611576Ssam /*
19711576Ssam  * Structure used in SIOCGIFCONF request.
19811576Ssam  * Used to retrieve interface configuration
19911576Ssam  * for machine (useful for programs which
20011576Ssam  * must know all networks accessible).
20111576Ssam  */
20211576Ssam struct	ifconf {
20311576Ssam 	int	ifc_len;		/* size of associated buffer */
20411576Ssam 	union {
20511576Ssam 		caddr_t	ifcu_buf;
20611576Ssam 		struct	ifreq *ifcu_req;
20711576Ssam 	} ifc_ifcu;
20811576Ssam #define	ifc_buf	ifc_ifcu.ifcu_buf	/* buffer address */
20911576Ssam #define	ifc_req	ifc_ifcu.ifcu_req	/* array of structures returned */
21011576Ssam };
21111576Ssam 
2124945Swnj #ifdef KERNEL
21325971Skarels #include "../net/if_arp.h"
2146333Ssam struct	ifqueue rawintrq;		/* raw packet input queue */
2154945Swnj struct	ifnet *ifnet;
21628943Skarels struct	ifaddr *ifa_ifwithaddr(), *ifa_ifwithnet();
21727197Skarels struct	ifaddr *ifa_ifwithdstaddr();
21825971Skarels #else KERNEL
21925971Skarels #include <net/if_arp.h>
22025971Skarels #endif KERNEL
221