xref: /haiku/src/libs/compat/freebsd_wlan/net80211/ieee80211_haiku.h (revision 9760dcae2038d47442f4658c2575844c6cf92c40)
1 /*-
2  * Copyright (c) 2003-2008 Sam Leffler, Errno Consulting
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
15  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
16  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
17  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
18  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
19  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
20  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
21  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
23  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24  *
25  * $FreeBSD$
26  */
27 #ifndef _FBSD_COMPAT_NET80211_IEEE80211_HAIKU_H_
28 #define _FBSD_COMPAT_NET80211_IEEE80211_HAIKU_H_
29 
30 #ifdef _KERNEL
31 
32 #include <sys/mutex.h>
33 #include <sys/sysctl.h>
34 #include <sys/taskqueue.h>
35 
36 
37 #define IEEE80211_CRYPTO_MODULE(name, version) \
38 	void \
39 	ieee80211_crypto_##name##_load() { \
40 		ieee80211_crypto_register(&name); \
41 	} \
42 \
43 \
44 	void \
45 	ieee80211_crypto_##name##_unload() \
46 	{ \
47 		ieee80211_crypto_unregister(&name); \
48 	}
49 
50 
51 /*
52  * Common state locking definitions.
53  */
54 typedef struct {
55 	char		name[16];		/* e.g. "ath0_com_lock" */
56 	struct mtx	mtx;
57 } ieee80211_com_lock_t;
58 #define	IEEE80211_LOCK_INIT(_ic, _name) do {				\
59 	ieee80211_com_lock_t *cl = &(_ic)->ic_comlock;			\
60 	snprintf(cl->name, sizeof(cl->name), "%s_com_lock", _name);	\
61 	mtx_init(&cl->mtx, cl->name, NULL, MTX_DEF | MTX_RECURSE);	\
62 } while (0)
63 #define	IEEE80211_LOCK_OBJ(_ic)	(&(_ic)->ic_comlock.mtx)
64 #define	IEEE80211_LOCK_DESTROY(_ic) mtx_destroy(IEEE80211_LOCK_OBJ(_ic))
65 #define	IEEE80211_LOCK(_ic)	   mtx_lock(IEEE80211_LOCK_OBJ(_ic))
66 #define	IEEE80211_UNLOCK(_ic)	   mtx_unlock(IEEE80211_LOCK_OBJ(_ic))
67 #define	IEEE80211_LOCK_ASSERT(_ic) \
68 	mtx_assert(IEEE80211_LOCK_OBJ(_ic), MA_OWNED)
69 
70 /*
71  * Node locking definitions.
72  */
73 typedef struct {
74 	char		name[16];		/* e.g. "ath0_node_lock" */
75 	struct mtx	mtx;
76 } ieee80211_node_lock_t;
77 #define	IEEE80211_NODE_LOCK_INIT(_nt, _name) do {			\
78 	ieee80211_node_lock_t *nl = &(_nt)->nt_nodelock;		\
79 	snprintf(nl->name, sizeof(nl->name), "%s_node_lock", _name);	\
80 	mtx_init(&nl->mtx, nl->name, NULL, MTX_DEF | MTX_RECURSE);	\
81 } while (0)
82 #define	IEEE80211_NODE_LOCK_OBJ(_nt)	(&(_nt)->nt_nodelock.mtx)
83 #define	IEEE80211_NODE_LOCK_DESTROY(_nt) \
84 	mtx_destroy(IEEE80211_NODE_LOCK_OBJ(_nt))
85 #define	IEEE80211_NODE_LOCK(_nt) \
86 	mtx_lock(IEEE80211_NODE_LOCK_OBJ(_nt))
87 #define	IEEE80211_NODE_IS_LOCKED(_nt) \
88 	mtx_owned(IEEE80211_NODE_LOCK_OBJ(_nt))
89 #define	IEEE80211_NODE_UNLOCK(_nt) \
90 	mtx_unlock(IEEE80211_NODE_LOCK_OBJ(_nt))
91 #define	IEEE80211_NODE_LOCK_ASSERT(_nt)	\
92 	mtx_assert(IEEE80211_NODE_LOCK_OBJ(_nt), MA_OWNED)
93 
94 /*
95  * Node table iteration locking definitions; this protects the
96  * scan generation # used to iterate over the station table
97  * while grabbing+releasing the node lock.
98  */
99 typedef struct {
100 	char		name[16];		/* e.g. "ath0_scan_lock" */
101 	struct mtx	mtx;
102 } ieee80211_scan_lock_t;
103 #define	IEEE80211_NODE_ITERATE_LOCK_INIT(_nt, _name) do {		\
104 	ieee80211_scan_lock_t *sl = &(_nt)->nt_scanlock;		\
105 	snprintf(sl->name, sizeof(sl->name), "%s_scan_lock", _name);	\
106 	mtx_init(&sl->mtx, sl->name, NULL, MTX_DEF);			\
107 } while (0)
108 #define	IEEE80211_NODE_ITERATE_LOCK_OBJ(_nt)	(&(_nt)->nt_scanlock.mtx)
109 #define	IEEE80211_NODE_ITERATE_LOCK_DESTROY(_nt) \
110 	mtx_destroy(IEEE80211_NODE_ITERATE_LOCK_OBJ(_nt))
111 #define	IEEE80211_NODE_ITERATE_LOCK(_nt) \
112 	mtx_lock(IEEE80211_NODE_ITERATE_LOCK_OBJ(_nt))
113 #define	IEEE80211_NODE_ITERATE_UNLOCK(_nt) \
114 	mtx_unlock(IEEE80211_NODE_ITERATE_LOCK_OBJ(_nt))
115 
116 /*
117  * Power-save queue definitions.
118  */
119 typedef struct mtx ieee80211_psq_lock_t;
120 #define	IEEE80211_PSQ_INIT(_psq, _name) \
121 	mtx_init(&(_psq)->psq_lock, _name, "802.11 ps q", MTX_DEF)
122 #define	IEEE80211_PSQ_DESTROY(_psq)	mtx_destroy(&(_psq)->psq_lock)
123 #define	IEEE80211_PSQ_LOCK(_psq)	mtx_lock(&(_psq)->psq_lock)
124 #define	IEEE80211_PSQ_UNLOCK(_psq)	mtx_unlock(&(_psq)->psq_lock)
125 
126 #ifndef IF_PREPEND_LIST
127 #define _IF_PREPEND_LIST(ifq, mhead, mtail, mcount) do {	\
128 	(mtail)->m_nextpkt = (ifq)->ifq_head;			\
129 	if ((ifq)->ifq_tail == NULL)				\
130 		(ifq)->ifq_tail = (mtail);			\
131 	(ifq)->ifq_head = (mhead);				\
132 	(ifq)->ifq_len += (mcount);				\
133 } while (0)
134 #define IF_PREPEND_LIST(ifq, mhead, mtail, mcount) do {		\
135 	IF_LOCK(ifq);						\
136 	_IF_PREPEND_LIST(ifq, mhead, mtail, mcount);		\
137 	IF_UNLOCK(ifq);						\
138 } while (0)
139 #endif /* IF_PREPEND_LIST */
140 
141 /*
142  * Age queue definitions.
143  */
144 typedef struct mtx ieee80211_ageq_lock_t;
145 #define	IEEE80211_AGEQ_INIT(_aq, _name) \
146 	mtx_init(&(_aq)->aq_lock, _name, "802.11 age q", MTX_DEF)
147 #define	IEEE80211_AGEQ_DESTROY(_aq)	mtx_destroy(&(_aq)->aq_lock)
148 #define	IEEE80211_AGEQ_LOCK(_aq)	mtx_lock(&(_aq)->aq_lock)
149 #define	IEEE80211_AGEQ_UNLOCK(_aq)	mtx_unlock(&(_aq)->aq_lock)
150 
151 /*
152  * Node reference counting definitions.
153  *
154  * ieee80211_node_initref	initialize the reference count to 1
155  * ieee80211_node_incref	add a reference
156  * ieee80211_node_decref	remove a reference
157  * ieee80211_node_dectestref	remove a reference and return 1 if this
158  *				is the last reference, otherwise 0
159  * ieee80211_node_refcnt	reference count for printing (only)
160  */
161 #include <machine/atomic.h>
162 
163 #define ieee80211_node_initref(_ni) \
164 	do { ((_ni)->ni_refcnt = 1); } while (0)
165 #define ieee80211_node_incref(_ni) \
166 	atomic_add_int(&(_ni)->ni_refcnt, 1)
167 #define	ieee80211_node_decref(_ni) \
168 	atomic_subtract_int(&(_ni)->ni_refcnt, 1)
169 struct ieee80211_node;
170 int	ieee80211_node_dectestref(struct ieee80211_node *ni);
171 #define	ieee80211_node_refcnt(_ni)	(_ni)->ni_refcnt
172 
173 struct ifqueue;
174 struct ieee80211vap;
175 void	ieee80211_drain_ifq(struct ifqueue *);
176 void	ieee80211_flush_ifq(struct ifqueue *, struct ieee80211vap *);
177 
178 void	ieee80211_vap_destroy(struct ieee80211vap *);
179 
180 #define	IFNET_IS_UP_RUNNING(_ifp) \
181 	(((_ifp)->if_flags & IFF_UP) && \
182 	 ((_ifp)->if_drv_flags & IFF_DRV_RUNNING))
183 
184 #define	msecs_to_ticks(ms)	(((ms)*hz)/1000)
185 #define	ticks_to_msecs(t)	(1000*(t) / hz)
186 #define	ticks_to_secs(t)	((t) / hz)
187 #define time_after(a,b) 	((long long)(b) - (long long)(a) < 0)
188 #define time_before(a,b)	time_after(b,a)
189 #define time_after_eq(a,b)	((long long)(a) - (long long)(b) >= 0)
190 #define time_before_eq(a,b)	time_after_eq(b,a)
191 
192 struct mbuf *ieee80211_getmgtframe(uint8_t **frm, int headroom, int pktlen);
193 
194 /* tx path usage */
195 #define	M_ENCAP		M_PROTO1		/* 802.11 encap done */
196 #define	M_EAPOL		M_PROTO3		/* PAE/EAPOL frame */
197 #define	M_PWR_SAV	M_PROTO4		/* bypass PS handling */
198 #define	M_MORE_DATA	M_PROTO5		/* more data frames to follow */
199 #define	M_FF		M_PROTO6		/* fast frame */
200 #define	M_TXCB		M_PROTO7		/* do tx complete callback */
201 #define	M_AMPDU_MPDU	M_PROTO8		/* ok for A-MPDU aggregation */
202 #define	M_80211_TX \
203 	(M_FRAG|M_FIRSTFRAG|M_LASTFRAG|M_ENCAP|M_EAPOL|M_PWR_SAV|\
204 	 M_MORE_DATA|M_FF|M_TXCB|M_AMPDU_MPDU)
205 
206 /* rx path usage */
207 #define	M_AMPDU		M_PROTO1		/* A-MPDU subframe */
208 #define	M_WEP		M_PROTO2		/* WEP done by hardware */
209 #if 0
210 #define	M_AMPDU_MPDU	M_PROTO8		/* A-MPDU re-order done */
211 #endif
212 #define	M_80211_RX	(M_AMPDU|M_WEP|M_AMPDU_MPDU)
213 
214 /*
215  * Store WME access control bits in the vlan tag.
216  * This is safe since it's done after the packet is classified
217  * (where we use any previous tag) and because it's passed
218  * directly in to the driver and there's no chance someone
219  * else will clobber them on us.
220  */
221 #define	M_WME_SETAC(m, ac) \
222 	((m)->m_pkthdr.ether_vtag = (ac))
223 #define	M_WME_GETAC(m)	((m)->m_pkthdr.ether_vtag)
224 
225 /*
226  * Mbufs on the power save queue are tagged with an age and
227  * timed out.  We reuse the hardware checksum field in the
228  * mbuf packet header to store this data.
229  */
230 #define	M_AGE_SET(m,v)		(m->m_pkthdr.csum_data = v)
231 #define	M_AGE_GET(m)		(m->m_pkthdr.csum_data)
232 #define	M_AGE_SUB(m,adj)	(m->m_pkthdr.csum_data -= adj)
233 
234 /*
235  * Store the sequence number.
236  */
237 #define	M_SEQNO_SET(m, seqno) \
238 	((m)->m_pkthdr.tso_segsz = (seqno))
239 #define	M_SEQNO_GET(m)	((m)->m_pkthdr.tso_segsz)
240 
241 #define	MTAG_ABI_NET80211	1132948340	/* net80211 ABI */
242 
243 struct ieee80211_cb {
244 	void	(*func)(struct ieee80211_node *, void *, int status);
245 	void	*arg;
246 };
247 #define	NET80211_TAG_CALLBACK	0	/* xmit complete callback */
248 
249 int	ieee80211_add_callback(struct mbuf *m,
250 		void (*func)(struct ieee80211_node *, void *, int), void *arg);
251 void	ieee80211_process_callback(struct ieee80211_node *, struct mbuf *, int);
252 
253 void	get_random_bytes(void *, size_t);
254 
255 struct ieee80211com;
256 
257 void	ieee80211_sysctl_attach(struct ieee80211com *);
258 void	ieee80211_sysctl_detach(struct ieee80211com *);
259 void	ieee80211_sysctl_vattach(struct ieee80211vap *);
260 void	ieee80211_sysctl_vdetach(struct ieee80211vap *);
261 
262 void	ieee80211_load_module(const char *);
263 
264 /*
265  * A "policy module" is an adjunct module to net80211 that provides
266  * functionality that typically includes policy decisions.  This
267  * modularity enables extensibility and vendor-supplied functionality.
268  */
269 #define	_IEEE80211_POLICY_MODULE(policy, name, version)			\
270 typedef void (*policy##_setup)(int);					\
271 SET_DECLARE(policy##_set, policy##_setup);
272 
273 /*
274  * Scanner modules provide scanning policy.
275  */
276 #define	IEEE80211_SCANNER_MODULE(name, version)
277 #define	IEEE80211_SCANNER_ALG(name, alg, v)
278 
279 
280 void	ieee80211_scan_sta_init(void);
281 void	ieee80211_scan_sta_uninit(void);
282 
283 
284 struct ieee80211req;
285 typedef int ieee80211_ioctl_getfunc(struct ieee80211vap *,
286     struct ieee80211req *);
287 SET_DECLARE(ieee80211_ioctl_getset, ieee80211_ioctl_getfunc);
288 #define	IEEE80211_IOCTL_GET(_name, _get) TEXT_SET(ieee80211_ioctl_getset, _get)
289 
290 typedef int ieee80211_ioctl_setfunc(struct ieee80211vap *,
291     struct ieee80211req *);
292 SET_DECLARE(ieee80211_ioctl_setset, ieee80211_ioctl_setfunc);
293 #define	IEEE80211_IOCTL_SET(_name, _set) TEXT_SET(ieee80211_ioctl_setset, _set)
294 #endif /* _KERNEL */
295 
296 /*
297  * Structure prepended to raw packets sent through the bpf
298  * interface when set to DLT_IEEE802_11_RADIO.  This allows
299  * user applications to specify pretty much everything in
300  * an Atheros tx descriptor.  XXX need to generalize.
301  *
302  * XXX cannot be more than 14 bytes as it is copied to a sockaddr's
303  * XXX sa_data area.
304  */
305 struct ieee80211_bpf_params {
306 	uint8_t		ibp_vers;	/* version */
307 #define	IEEE80211_BPF_VERSION	0
308 	uint8_t		ibp_len;	/* header length in bytes */
309 	uint8_t		ibp_flags;
310 #define	IEEE80211_BPF_SHORTPRE	0x01	/* tx with short preamble */
311 #define	IEEE80211_BPF_NOACK	0x02	/* tx with no ack */
312 #define	IEEE80211_BPF_CRYPTO	0x04	/* tx with h/w encryption */
313 #define	IEEE80211_BPF_FCS	0x10	/* frame incldues FCS */
314 #define	IEEE80211_BPF_DATAPAD	0x20	/* frame includes data padding */
315 #define	IEEE80211_BPF_RTS	0x40	/* tx with RTS/CTS */
316 #define	IEEE80211_BPF_CTS	0x80	/* tx with CTS only */
317 	uint8_t		ibp_pri;	/* WME/WMM AC+tx antenna */
318 	uint8_t		ibp_try0;	/* series 1 try count */
319 	uint8_t		ibp_rate0;	/* series 1 IEEE tx rate */
320 	uint8_t		ibp_power;	/* tx power (device units) */
321 	uint8_t		ibp_ctsrate;	/* IEEE tx rate for CTS */
322 	uint8_t		ibp_try1;	/* series 2 try count */
323 	uint8_t		ibp_rate1;	/* series 2 IEEE tx rate */
324 	uint8_t		ibp_try2;	/* series 3 try count */
325 	uint8_t		ibp_rate2;	/* series 3 IEEE tx rate */
326 	uint8_t		ibp_try3;	/* series 4 try count */
327 	uint8_t		ibp_rate3;	/* series 4 IEEE tx rate */
328 };
329 
330 #endif /* _FBSD_COMPAT_NET80211_IEEE80211_HAIKU_H_ */
331