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