xref: /titanic_51/usr/src/lib/libbc/inc/include/net/if.h (revision f045d8d6fec1759551cc2bce1d26628931f14fce)
1 /*
2  * Copyright 2010 Sun Microsystems, Inc.  All rights reserved.
3  * Use is subject to license terms.
4  */
5 
6 /*
7  * Copyright (c) 1982, 1986 Regents of the University of California.
8  * All rights reserved.  The Berkeley software License Agreement
9  * specifies the terms and conditions for redistribution.
10  */
11 
12 #ifndef _net_if_h
13 #define	_net_if_h
14 
15 /*
16  * Structures defining a network interface, providing a packet
17  * transport mechanism (ala level 0 of the PUP protocols).
18  *
19  * Each interface accepts output datagrams of a specified maximum
20  * length, and provides higher level routines with input datagrams
21  * received from its medium.
22  *
23  * Output occurs when the routine if_output is called, with three parameters:
24  *	(*ifp->if_output)(ifp, m, dst)
25  * Here m is the mbuf chain to be sent and dst is the destination address.
26  * The output routine encapsulates the supplied datagram if necessary,
27  * and then transmits it on its medium.
28  *
29  * On input, each interface unwraps the data received by it, and either
30  * places it on the input queue of a internetwork datagram routine
31  * and posts the associated software interrupt, or passes the datagram to a raw
32  * packet input routine.
33  *
34  * Routines exist for locating interfaces by their addresses
35  * or for locating a interface on a certain network, as well as more general
36  * routing and gateway routines maintaining information used to locate
37  * interfaces.  These routines live in the files if.c and route.c
38  */
39 
40 /*
41  * Structure defining a queue for a network interface.
42  *
43  * (Would like to call this struct ``if'', but C isn't PL/1.)
44  */
45 struct ifnet {
46 	char	*if_name;		/* name, e.g. ``en'' or ``lo'' */
47 	short	if_unit;		/* sub-unit for lower level driver */
48 	short	if_mtu;			/* maximum transmission unit */
49 	short	if_flags;		/* up/down, broadcast, etc. */
50 	short	if_timer;		/* time 'til if_watchdog called */
51 	u_short	if_promisc;		/* net # of requests for promisc mode */
52 	int	if_metric;		/* routing metric (external only) */
53 	struct	ifaddr *if_addrlist;	/* linked list of addresses per if */
54 	struct	ifqueue {
55 		struct	mbuf *ifq_head;
56 		struct	mbuf *ifq_tail;
57 		int	ifq_len;
58 		int	ifq_maxlen;
59 		int	ifq_drops;
60 	} if_snd;			/* output queue */
61 /* procedure handles */
62 	int	(*if_init)();		/* init routine */
63 	int	(*if_output)();		/* output routine */
64 	int	(*if_ioctl)();		/* ioctl routine */
65 	int	(*if_reset)();		/* bus reset routine */
66 	int	(*if_watchdog)();	/* timer routine */
67 /* generic interface statistics */
68 	int	if_ipackets;		/* packets received on interface */
69 	int	if_ierrors;		/* input errors on interface */
70 	int	if_opackets;		/* packets sent on interface */
71 	int	if_oerrors;		/* output errors on interface */
72 	int	if_collisions;		/* collisions on csma interfaces */
73 /* end statistics */
74 	struct	ifnet *if_next;
75 	struct	ifnet *if_upper;	/* next layer up */
76 	struct	ifnet *if_lower;	/* next layer down */
77 	int	(*if_input)();		/* input routine */
78 	int	(*if_ctlin)();		/* control input routine */
79 	int	(*if_ctlout)();		/* control output routine */
80 #ifdef sun
81 	struct map *if_memmap;		/* rmap for interface specific memory */
82 #endif
83 };
84 
85 #define	IFF_UP		0x1		/* interface is up */
86 #define	IFF_BROADCAST	0x2		/* broadcast address valid */
87 #define	IFF_DEBUG	0x4		/* turn on debugging */
88 #define	IFF_LOOPBACK	0x8		/* is a loopback net */
89 #define	IFF_POINTOPOINT	0x10		/* interface is point-to-point link */
90 #define	IFF_NOTRAILERS	0x20		/* avoid use of trailers */
91 #define	IFF_RUNNING	0x40		/* resources allocated */
92 #define	IFF_NOARP	0x80		/* no address resolution protocol */
93 #define	IFF_PROMISC	0x100		/* receive all packets */
94 #define	IFF_ALLMULTI	0x200		/* receive all multicast packets */
95 #define	IFF_PRIVATE	0x8000		/* do not advertise */
96 
97 /* flags set internally only: */
98 #define	IFF_CANTCHANGE \
99 		(IFF_BROADCAST | IFF_POINTOPOINT | IFF_RUNNING | IFF_PROMISC)
100 
101 /*
102  * Output queues (ifp->if_snd) and internetwork datagram level (pup level 1)
103  * input routines have queues of messages stored on ifqueue structures
104  * (defined above).  Entries are added to and deleted from these structures
105  * by these macros, which should be called with ipl raised to splimp().
106  */
107 #define	IF_QFULL(ifq)		((ifq)->ifq_len >= (ifq)->ifq_maxlen)
108 #define	IF_DROP(ifq)		((ifq)->ifq_drops++)
109 #define	IF_ENQUEUE(ifq, m) { \
110 	(m)->m_act = 0; \
111 	if ((ifq)->ifq_tail == 0) \
112 		(ifq)->ifq_head = m; \
113 	else \
114 		(ifq)->ifq_tail->m_act = m; \
115 	(ifq)->ifq_tail = m; \
116 	(ifq)->ifq_len++; \
117 }
118 #define	IF_PREPEND(ifq, m) { \
119 	(m)->m_act = (ifq)->ifq_head; \
120 	if ((ifq)->ifq_tail == 0) \
121 		(ifq)->ifq_tail = (m); \
122 	(ifq)->ifq_head = (m); \
123 	(ifq)->ifq_len++; \
124 }
125 /*
126  * Packets destined for level-1 protocol input routines
127  * have a pointer to the receiving interface prepended to the data.
128  * IF_DEQUEUEIF extracts and returns this pointer when dequeueing the packet.
129  * IF_ADJ should be used otherwise to adjust for its presence.
130  */
131 #define	IF_ADJ(m) { \
132 	(m)->m_off += sizeof (struct ifnet *); \
133 	(m)->m_len -= sizeof (struct ifnet *); \
134 	if ((m)->m_len == 0) { \
135 		struct mbuf *n; \
136 		MFREE((m), n); \
137 		(m) = n; \
138 	} \
139 }
140 #define	IF_DEQUEUEIF(ifq, m, ifp) { \
141 	(m) = (ifq)->ifq_head; \
142 	if (m) { \
143 		if (((ifq)->ifq_head = (m)->m_act) == 0) \
144 			(ifq)->ifq_tail = 0; \
145 		(m)->m_act = 0; \
146 		(ifq)->ifq_len--; \
147 		(ifp) = *(mtod((m), struct ifnet **)); \
148 		IF_ADJ(m); \
149 	} \
150 }
151 #define	IF_DEQUEUE(ifq, m) { \
152 	(m) = (ifq)->ifq_head; \
153 	if (m) { \
154 		if (((ifq)->ifq_head = (m)->m_act) == 0) \
155 			(ifq)->ifq_tail = 0; \
156 		(m)->m_act = 0; \
157 		(ifq)->ifq_len--; \
158 	} \
159 }
160 
161 #define	IFQ_MAXLEN	50
162 #define	IFNET_SLOWHZ	1		/* granularity is 1 second */
163 
164 /*
165  * The ifaddr structure contains information about one address
166  * of an interface.  They are maintained by the different address families,
167  * are allocated and attached when an address is set, and are linked
168  * together so all addresses for an interface can be located.
169  */
170 struct ifaddr {
171 	struct	sockaddr ifa_addr;	/* address of interface */
172 	union {
173 		struct	sockaddr ifu_broadaddr;
174 		struct	sockaddr ifu_dstaddr;
175 	} ifa_ifu;
176 #ifndef ifa_broadaddr
177 #define	ifa_broadaddr	ifa_ifu.ifu_broadaddr	/* broadcast address */
178 #endif
179 #ifndef ifa_dstaddr
180 #define	ifa_dstaddr	ifa_ifu.ifu_dstaddr	/* other end of p-to-p link */
181 #endif
182 	struct	ifnet *ifa_ifp;		/* back-pointer to interface */
183 	struct	ifaddr *ifa_next;	/* next address for interface */
184 };
185 
186 /*
187  * Interface request structure used for socket
188  * ioctl's.  All interface ioctl's must have parameter
189  * definitions which begin with ifr_name.  The
190  * remainder may be interface specific.
191  */
192 struct	ifreq {
193 #define	IFNAMSIZ	16
194 	char	ifr_name[IFNAMSIZ];		/* if name, e.g. "en0" */
195 	union {
196 		struct	sockaddr ifru_addr;
197 		struct	sockaddr ifru_dstaddr;
198 		char	ifru_oname[IFNAMSIZ];	/* other if name */
199 		struct	sockaddr ifru_broadaddr;
200 		short	ifru_flags;
201 		int	ifru_metric;
202 		char	ifru_data[1];		/* interface dependent data */
203 
204 		/* Struct for FDDI ioctl's */
205 		struct ifr_dnld_reqs {
206 			caddr_t	v_addr;
207 			caddr_t	m_addr;
208 			caddr_t	ex_addr;
209 			u_int	size;
210 		} ifru_dnld_req;
211 
212 		/* Struct for FDDI stats */
213 		struct ifr_fddi_stats {
214 			u_int	stat_size;
215 			caddr_t	fddi_stats;
216 		} ifru_fddi_stat;
217 
218 		struct ifr_netmapents {
219 			u_int	map_ent_size,	/* size of netmap structure */
220 				entry_number;	/* index into netmap list */
221 			caddr_t	fddi_map_ent;	/* pointer to user structure */
222 		} ifru_netmapent;
223 
224 		/* Field for generic ioctl for fddi */
225 
226 		struct ifr_fddi_gen_struct {
227 			int	ifru_fddi_gioctl; /* field for gen ioctl */
228 			caddr_t ifru_fddi_gaddr; /* Generic ptr to a field */
229 		} ifru_fddi_gstruct;
230 
231 	} ifr_ifru;
232 
233 #define	ifr_addr	ifr_ifru.ifru_addr	/* address */
234 #define	ifr_dstaddr	ifr_ifru.ifru_dstaddr	/* other end of p-to-p link */
235 #define	ifr_oname	ifr_ifru.ifru_oname	/* other if name */
236 #define	ifr_broadaddr	ifr_ifru.ifru_broadaddr	/* broadcast address */
237 #define	ifr_flags	ifr_ifru.ifru_flags	/* flags */
238 #define	ifr_metric	ifr_ifru.ifru_metric	/* metric */
239 #define	ifr_data	ifr_ifru.ifru_data	/* for use by interface */
240 
241 /* FDDI specific */
242 #define	ifr_dnld_req	ifr_ifru.ifru_dnld_req
243 #define	ifr_fddi_stat	ifr_ifru.ifru_fddi_stat
244 #define	ifr_fddi_netmap	ifr_ifru.ifru_netmapent	/* FDDI network map entries */
245 #define	ifr_fddi_gstruct ifr_ifru.ifru_fddi_gstruct
246 
247 };
248 
249 /*
250  * Structure used in SIOCGIFCONF request.
251  * Used to retrieve interface configuration
252  * for machine (useful for programs which
253  * must know all networks accessible).
254  */
255 struct	ifconf {
256 	int	ifc_len;		/* size of associated buffer */
257 	union {
258 		caddr_t	ifcu_buf;
259 		struct	ifreq *ifcu_req;
260 	} ifc_ifcu;
261 #define	ifc_buf	ifc_ifcu.ifcu_buf	/* buffer address */
262 #define	ifc_req	ifc_ifcu.ifcu_req	/* array of structures returned */
263 };
264 
265 #endif /* !_net_if_h */
266