xref: /haiku/headers/posix/net/if.h (revision 01b25646004ff628ecad0281a9795e5e90f71746)
1 /* if.h
2  * Interface definitions for beos
3  */
4 
5 #ifndef _NET_IF_H
6 #define _NET_IF_H
7 
8 /* FIXME: this file is NOT POSIX compliant, and rely on way too much OS-dependent
9    definition.
10    Moving private parts to private headers should help clean up this file
11 
12    POSIX net/if.h spec:
13    http://www.opengroup.org/onlinepubs/007904975/basedefs/net/if.h.html
14 */
15 
16 #include <Drivers.h>	/* FIXME */
17 #include <OS.h>			/* FIXME */
18 #include <net/if_types.h>
19 #include <netinet/in.h>
20 #include <net/route.h>
21 
22 #ifdef __cplusplus
23 extern "C" {
24 #endif
25 
26 /* Forward reference... */
27 struct socket;
28 
29 enum {
30 	IF_GETADDR = B_DEVICE_OP_CODES_END,
31 	IF_INIT,
32 	IF_NONBLOCK,
33 	IF_ADDMULTI,
34 	IF_REMMULTI,
35 	IF_SETPROMISC,
36 	IF_GETFRAMESIZE
37 };
38 
39 /* Media types are now listed in net/if_types.h */
40 
41 /* Interface flags */
42 enum {
43 	IFF_UP          = 0x0001,
44 	IFF_DOWN        = 0x0002,
45 	IFF_PROMISC     = 0x0004,
46 	IFF_RUNNING     = 0x0008,
47 	IFF_MULTICAST   = 0x0010,
48 	IFF_BROADCAST   = 0x0020,
49 	IFF_POINTOPOINT = 0x0040,
50 	IFF_NOARP       = 0x0080,
51 	IFF_LOOPBACK	= 0x0100,
52 	IFF_DEBUG       = 0x0200,
53 	IFF_LINK0       = 0x0400,
54 	IFF_LINK1       = 0x0800,
55 	IFF_LINK2       = 0x1000,
56 	IFF_SIMPLEX     = 0x2000
57 };
58 
59 struct ifq {
60 	struct mbuf *head;
61 	struct mbuf *tail;
62 	int maxlen;
63 	int len;
64 	sem_id lock;
65 	sem_id pop;
66 };
67 
68 #define IFQ_FULL(ifq)   (ifq->len >= ifq->maxlen)
69 
70 #define IFQ_ENQUEUE(ifq, m) { \
71 	acquire_sem((ifq)->lock); \
72 	(m)->m_nextpkt = 0; \
73 	if ((ifq)->tail == 0) \
74 		(ifq)->head = m; \
75 	else \
76 		(ifq)->tail->m_nextpkt = m; \
77 	(ifq)->tail = m; \
78 	(ifq)->len++; \
79 	release_sem((ifq)->lock); \
80 	release_sem((ifq)->pop); \
81 }
82 
83 #define IFQ_DEQUEUE(ifq, m) { \
84 	acquire_sem((ifq)->lock); \
85 	(m) = (ifq)->head; \
86 	if (m) { \
87 		if (((ifq)->head = (m)->m_nextpkt) == 0) \
88 			(ifq)->tail = 0; \
89 		(m)->m_nextpkt = 0; \
90 		(ifq)->len--; \
91 	} \
92 	release_sem((ifq)->lock); \
93 }
94 
95 struct ifaddr {
96 	struct ifaddr 	*ifa_next;      /* the next address for the interface */
97 	struct ifnet 	*ifa_ifp;           /* pointer to the interface structure */
98 
99 	struct sockaddr *ifa_addr;	    /* the address - cast to be a suitable type, so we
100 		                             * use this structure to store any type of address that
101 		                             * we have a struct sockaddr_? for. e.g.
102 		                             * link level address via sockaddr_dl and
103 		                             * ipv4 via sockeddr_in
104 		                             * same for next 2 pointers as well
105 		                             */
106 	struct sockaddr	*ifa_dstaddr;	/* if we're on a point-to-point link this
107 					                 * is the other end */
108 	struct sockaddr	*ifa_netmask;	/* The netmask we're using */
109 
110 	/* check or clean routes... */
111 	void (*ifa_rtrequest)(int, struct rtentry *, struct sockaddr *);
112 	uint8	ifa_flags;              /* flags (mainly routing */
113 	uint16	ifa_refcnt;             /* how many references are there to
114 	                                 * this structure? */
115 	int	ifa_metric;                 /* the metirc for this interface/address */
116 };
117 #define ifa_broadaddr	ifa_dstaddr
118 
119 struct if_data {
120 	uint8	ifi_type;              /* type of media */
121 	uint8	ifi_addrlen;           /* length of media address length */
122 	uint8	ifi_hdrlen;            /* size of media header */
123 	uint32	ifi_mtu;               /* mtu */
124 	uint32	ifi_metric;            /* routing metric */
125 	uint32	ifi_baudrate;          /* baudrate of line */
126 	/* statistics!! */
127 	int32  ifi_ipackets;           /* packets received on interface */
128 	int32  ifi_ierrors;            /* input errors on interface */
129 	int32  ifi_opackets;           /* packets sent on interface */
130 	int32  ifi_oerrors;            /* output errors on interface */
131 	int32  ifi_collisions;         /* collisions on csma interfaces */
132 	int32  ifi_ibytes;             /* total number of octets received */
133 	int32  ifi_obytes;             /* total number of octets sent */
134 	int32  ifi_imcasts;            /* packets received via multicast */
135 	int32  ifi_omcasts;            /* packets sent via multicast */
136 	int32  ifi_iqdrops;            /* dropped on input, this interface */
137 	int32  ifi_noproto;            /* destined for unsupported protocol */
138 };
139 
140 struct ifnet {
141 	struct ifnet *if_next;       /* next device */
142 	struct ifaddr *if_addrlist;  /* linked list of addresses */
143 	int devid;                   /* our device id if we have one... */
144 	int id;                      /* id within the stack's device list */
145 	char *name;                  /* name of driver e.g. tulip */
146 	int if_unit;                 /* number of unit e.g  0 */
147 	char *if_name;               /* full name, e.g. tulip0 */
148 	struct if_data ifd;	         /* if_data structure, shortcuts below */
149 	int if_flags;                /* if flags */
150 	int if_index;                /* our index in ifnet_addrs and interfaces */
151 
152 	struct ifq *rxq;
153 	thread_id rx_thread;
154 	struct ifq *txq;
155 	thread_id tx_thread;
156 	struct ifq *devq;
157 
158 	int	 (*start) (struct ifnet *);
159 	int	 (*stop)  (struct ifnet *);
160 	void (*input) (struct mbuf*);
161 	int	 (*output)(struct ifnet *, struct mbuf*,
162 			  struct sockaddr*, struct rtentry *);
163 	int	 (*ioctl) (struct ifnet *, ulong, caddr_t);
164 };
165 #define if_mtu          ifd.ifi_mtu
166 #define if_type         ifd.ifi_type
167 #define if_addrlen      ifd.ifi_addrlen
168 #define if_hdrlen       ifd.ifi_hdrlen
169 #define if_metric       ifd.ifi_metric
170 #define if_baudrate	    ifd.ifi_baudrate
171 #define if_ipackets     ifd.ifi_ipackets
172 #define if_ierrors      ifd.ifi_ierrors
173 #define if_opackets     ifd.ifi_opackets
174 #define if_oerrors      ifd.ifi_oerrors
175 #define if_collisions   ifd.ifi_collisions
176 #define if_ibytes       ifd.ifi_ibytes
177 #define if_obytes       ifd.ifi_obytes
178 #define if_imcasts      ifd.ifi_imcasts
179 #define if_omcasts      ifd.ifi_omcasts
180 #define if_iqdrops      ifd.ifi_iqdrops
181 #define if_noproto      ifd.ifi_noproto
182 
183 #define IFNAMSIZ	16
184 
185 /* This structure is used for passing interface requests via ioctl */
186 struct ifreq {
187 	char	ifr_name[IFNAMSIZ];	/* name of interface */
188 	union {
189 		struct sockaddr ifru_addr;
190 		struct sockaddr ifru_dstaddr;
191 		struct sockaddr ifru_broadaddr;
192 		uint16 ifru_flags;
193 		int ifru_metric;
194 		char * ifru_data;
195 	} ifr_ifru;
196 };
197 #define ifr_addr        ifr_ifru.ifru_addr
198 #define ifr_dstaddr     ifr_ifru.ifru_dstaddr
199 #define ifr_broadaddr	ifr_ifru.ifru_broadaddr
200 #define ifr_flags       ifr_ifru.ifru_flags
201 #define ifr_metric      ifr_ifru.ifru_metric
202 #define ifr_mtu         ifr_ifru.ifru_metric /* sneaky overload :) */
203 #define	ifr_data        ifr_ifru.ifru_data
204 
205 struct ifconf {
206 	int ifc_len;	/* length of associated buffer */
207 	union {
208 		char * ifcu_buf;
209 		struct ifreq *ifcu_req;
210 	} ifc_ifcu;
211 };
212 #define ifc_buf		ifc_ifcu.ifcu_buf
213 #define ifc_req		ifc_ifcu.ifcu_req
214 
215 
216 #define IFAFREE(ifa) \
217 do { \
218         if ((ifa)->ifa_refcnt <= 0) \
219                 ifafree(ifa); \
220         else \
221                 (ifa)->ifa_refcnt--; \
222 } while (0)
223 
224 /* used to pass in additional information, such as aliases */
225 struct ifaliasreq {
226 	char ifa_name[IFNAMSIZ];
227 	struct sockaddr ifra_addr;
228 	struct sockaddr ifra_broadaddr;
229 #define ifra_dstaddr	ifra_broadaddr
230 	struct sockaddr ifra_mask;
231 };
232 
233 #define IFA_ROUTE	RTF_UP
234 
235 /*
236  * Message format for use in obtaining information about interfaces
237  * from sysctl and the routing socket.
238  */
239 struct if_msghdr {
240         u_short ifm_msglen;     /* to skip over non-understood messages */
241         u_char  ifm_version;    /* future binary compatability */
242         u_char  ifm_type;       /* message type */
243         int     ifm_addrs;      /* like rtm_addrs */
244         int     ifm_flags;      /* value of if_flags */
245         u_short ifm_index;      /* index for associated ifp */
246         struct  if_data ifm_data;/* statistics and other data about if */
247 };
248 
249 /*
250  * Message format for use in obtaining information about interface addresses
251  * from sysctl and the routing socket.
252  */
253 struct ifa_msghdr {
254         u_short ifam_msglen;    /* to skip over non-understood messages */
255         u_char  ifam_version;   /* future binary compatability */
256         u_char  ifam_type;      /* message type */
257         int     ifam_addrs;     /* like rtm_addrs */
258         int     ifam_flags;     /* value of ifa_flags */
259         u_short ifam_index;     /* index for associated ifp */
260         int     ifam_metric;    /* value of ifa_metric */
261 };
262 
263 /* function declaration */
264 struct  ifq    *start_ifq(void);
265 void            stop_ifq(struct ifq *);
266 struct  ifnet  *get_interfaces(void);
267 struct	ifnet  *ifunit(char *name);
268 struct	ifaddr *ifa_ifwithaddr(struct sockaddr *);
269 struct	ifaddr *ifa_ifwithaf(int);
270 struct	ifaddr *ifa_ifwithdstaddr(struct sockaddr *);
271 struct	ifaddr *ifa_ifwithnet(struct sockaddr *);
272 struct	ifaddr *ifa_ifwithroute(int, struct sockaddr *,
273                               struct sockaddr *);
274 struct	ifaddr *ifaof_ifpforaddr(struct sockaddr *, struct ifnet *);
275 void	ifafree(struct ifaddr *);
276 
277 void    if_attach(struct ifnet *ifp);
278 void    if_detach(struct ifnet *ifp);
279 
280 int     ifioctl(struct socket *so, ulong cmd, caddr_t data);
281 int     ifconf(int cmd, char *data);
282 void    if_init(void);
283 
284 #ifdef __cplusplus
285 }
286 #endif
287 
288 #endif /* _NET_IF_H */
289 
290