xref: /freebsd/sys/net80211/ieee80211_freebsd.h (revision 1669d8afc64812c8d2d1d147ae1fd42ff441e1b1)
1 /*-
2  * Copyright (c) 2003-2007 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 _NET80211_IEEE80211_FREEBSD_H_
28 #define _NET80211_IEEE80211_FREEBSD_H_
29 
30 #ifdef _KERNEL
31 /*
32  * Common state locking definitions.
33  */
34 typedef struct mtx ieee80211_com_lock_t;
35 #define	IEEE80211_LOCK_INIT(_ic, _name) \
36 	mtx_init(&(_ic)->ic_comlock, _name, "802.11 com lock", MTX_DEF)
37 #define	IEEE80211_LOCK_DESTROY(_ic) mtx_destroy(&(_ic)->ic_comlock)
38 #define	IEEE80211_LOCK(_ic)	   mtx_lock(&(_ic)->ic_comlock)
39 #define	IEEE80211_UNLOCK(_ic)	   mtx_unlock(&(_ic)->ic_comlock)
40 #define	IEEE80211_LOCK_ASSERT(_ic) \
41 	mtx_assert(&(_ic)->ic_comlock, MA_OWNED)
42 
43 /*
44  * Beacon locking definitions.
45  */
46 typedef struct mtx ieee80211_beacon_lock_t;
47 #define	IEEE80211_BEACON_LOCK_INIT(_ic, _name) \
48 	mtx_init(&(_ic)->ic_beaconlock, _name, "802.11 beacon lock", MTX_DEF)
49 #define	IEEE80211_BEACON_LOCK_DESTROY(_ic) mtx_destroy(&(_ic)->ic_beaconlock)
50 #define	IEEE80211_BEACON_LOCK(_ic)	   mtx_lock(&(_ic)->ic_beaconlock)
51 #define	IEEE80211_BEACON_UNLOCK(_ic)	   mtx_unlock(&(_ic)->ic_beaconlock)
52 #define	IEEE80211_BEACON_LOCK_ASSERT(_ic) \
53 	mtx_assert(&(_ic)->ic_beaconlock, MA_OWNED)
54 
55 /*
56  * Node locking definitions.
57  * NB: MTX_DUPOK is because we don't generate per-interface strings.
58  */
59 typedef struct mtx ieee80211_node_lock_t;
60 #define	IEEE80211_NODE_LOCK_INIT(_nt, _name) \
61 	mtx_init(&(_nt)->nt_nodelock, _name, "802.11 node table", \
62 		MTX_DEF | MTX_DUPOK)
63 #define	IEEE80211_NODE_LOCK_DESTROY(_nt)	mtx_destroy(&(_nt)->nt_nodelock)
64 #define	IEEE80211_NODE_LOCK(_nt)		mtx_lock(&(_nt)->nt_nodelock)
65 #define	IEEE80211_NODE_IS_LOCKED(_nt)		mtx_owned(&(_nt)->nt_nodelock)
66 #define	IEEE80211_NODE_UNLOCK(_nt)		mtx_unlock(&(_nt)->nt_nodelock)
67 #define	IEEE80211_NODE_LOCK_ASSERT(_nt) \
68 	mtx_assert(&(_nt)->nt_nodelock, MA_OWNED)
69 
70 /*
71  * Node table scangen locking definitions.
72  */
73 typedef struct mtx ieee80211_scan_lock_t;
74 #define	IEEE80211_SCAN_LOCK_INIT(_nt, _name) \
75 	mtx_init(&(_nt)->nt_scanlock, _name, "802.11 node scangen", MTX_DEF)
76 #define	IEEE80211_SCAN_LOCK_DESTROY(_nt)	mtx_destroy(&(_nt)->nt_scanlock)
77 #define	IEEE80211_SCAN_LOCK(_nt)		mtx_lock(&(_nt)->nt_scanlock)
78 #define	IEEE80211_SCAN_UNLOCK(_nt)		mtx_unlock(&(_nt)->nt_scanlock)
79 #define	IEEE80211_SCAN_LOCK_ASSERT(_nt) \
80 	mtx_assert(&(_nt)->nt_scanlock, MA_OWNED)
81 
82 /*
83  * Per-node power-save queue definitions.
84  */
85 #define	IEEE80211_NODE_SAVEQ_INIT(_ni, _name) do {		\
86 	mtx_init(&(_ni)->ni_savedq.ifq_mtx, _name, "802.11 ps queue", MTX_DEF);\
87 	(_ni)->ni_savedq.ifq_maxlen = IEEE80211_PS_MAX_QUEUE;	\
88 } while (0)
89 #define	IEEE80211_NODE_SAVEQ_DESTROY(_ni) \
90 	mtx_destroy(&(_ni)->ni_savedq.ifq_mtx)
91 #define	IEEE80211_NODE_SAVEQ_QLEN(_ni) \
92 	_IF_QLEN(&(_ni)->ni_savedq)
93 #define	IEEE80211_NODE_SAVEQ_LOCK(_ni) do {	\
94 	IF_LOCK(&(_ni)->ni_savedq);				\
95 } while (0)
96 #define	IEEE80211_NODE_SAVEQ_UNLOCK(_ni) do {	\
97 	IF_UNLOCK(&(_ni)->ni_savedq);				\
98 } while (0)
99 #define	IEEE80211_NODE_SAVEQ_DEQUEUE(_ni, _m, _qlen) do {	\
100 	IEEE80211_NODE_SAVEQ_LOCK(_ni);				\
101 	_IF_DEQUEUE(&(_ni)->ni_savedq, _m);			\
102 	(_qlen) = IEEE80211_NODE_SAVEQ_QLEN(_ni);		\
103 	IEEE80211_NODE_SAVEQ_UNLOCK(_ni);			\
104 } while (0)
105 #define	IEEE80211_NODE_SAVEQ_DRAIN(_ni, _qlen) do {		\
106 	IEEE80211_NODE_SAVEQ_LOCK(_ni);				\
107 	(_qlen) = IEEE80211_NODE_SAVEQ_QLEN(_ni);		\
108 	_IF_DRAIN(&(_ni)->ni_savedq);				\
109 	IEEE80211_NODE_SAVEQ_UNLOCK(_ni);			\
110 } while (0)
111 /* XXX could be optimized */
112 #define	_IEEE80211_NODE_SAVEQ_DEQUEUE_HEAD(_ni, _m) do {	\
113 	_IF_DEQUEUE(&(_ni)->ni_savedq, m);			\
114 } while (0)
115 #define	_IEEE80211_NODE_SAVEQ_ENQUEUE(_ni, _m, _qlen, _age) do {\
116 	(_m)->m_nextpkt = NULL;					\
117 	if ((_ni)->ni_savedq.ifq_tail != NULL) { 		\
118 		_age -= M_AGE_GET((_ni)->ni_savedq.ifq_tail);	\
119 		(_ni)->ni_savedq.ifq_tail->m_nextpkt = (_m);	\
120 	} else { 						\
121 		(_ni)->ni_savedq.ifq_head = (_m); 		\
122 	}							\
123 	M_AGE_SET(_m, _age);					\
124 	(_ni)->ni_savedq.ifq_tail = (_m); 			\
125 	(_qlen) = ++(_ni)->ni_savedq.ifq_len; 			\
126 } while (0)
127 
128 #define	IEEE80211_TAPQ_INIT(_tap) do {				\
129 	mtx_init(&(tap)->txa_q.ifq_mtx, "ampdu tx queue", NULL, MTX_DEF); \
130 	(_tap)->txa_q.ifq_maxlen = IEEE80211_AGGR_BAWMAX;	\
131 } while (0)
132 #define	IEEE80211_TAPQ_DESTROY(_tap) \
133 	mtx_destroy(&(_tap)->txa_q.ifq_mtx)
134 
135 #ifndef IF_PREPEND_LIST
136 #define _IF_PREPEND_LIST(ifq, mhead, mtail, mcount) do {	\
137 	(mtail)->m_nextpkt = (ifq)->ifq_head;			\
138 	if ((ifq)->ifq_tail == NULL)				\
139 		(ifq)->ifq_tail = (mtail);			\
140 	(ifq)->ifq_head = (mhead);				\
141 	(ifq)->ifq_len += (mcount);				\
142 } while (0)
143 #define IF_PREPEND_LIST(ifq, mhead, mtail, mcount) do {		\
144 	IF_LOCK(ifq);						\
145 	_IF_PREPEND_LIST(ifq, mhead, mtail, mcount);		\
146 	IF_UNLOCK(ifq);						\
147 } while (0)
148 #endif /* IF_PREPEND_LIST */
149 
150 /*
151  * 802.1x MAC ACL database locking definitions.
152  */
153 typedef struct mtx acl_lock_t;
154 #define	ACL_LOCK_INIT(_as, _name) \
155 	mtx_init(&(_as)->as_lock, _name, "802.11 ACL", MTX_DEF)
156 #define	ACL_LOCK_DESTROY(_as)		mtx_destroy(&(_as)->as_lock)
157 #define	ACL_LOCK(_as)			mtx_lock(&(_as)->as_lock)
158 #define	ACL_UNLOCK(_as)			mtx_unlock(&(_as)->as_lock)
159 #define	ACL_LOCK_ASSERT(_as) \
160 	mtx_assert((&(_as)->as_lock), MA_OWNED)
161 
162 /*
163  * Node reference counting definitions.
164  *
165  * ieee80211_node_initref	initialize the reference count to 1
166  * ieee80211_node_incref	add a reference
167  * ieee80211_node_decref	remove a reference
168  * ieee80211_node_dectestref	remove a reference and return 1 if this
169  *				is the last reference, otherwise 0
170  * ieee80211_node_refcnt	reference count for printing (only)
171  */
172 #include <machine/atomic.h>
173 
174 #define ieee80211_node_initref(_ni) \
175 	do { ((_ni)->ni_refcnt = 1); } while (0)
176 #define ieee80211_node_incref(_ni) \
177 	atomic_add_int(&(_ni)->ni_refcnt, 1)
178 #define	ieee80211_node_decref(_ni) \
179 	atomic_subtract_int(&(_ni)->ni_refcnt, 1)
180 struct ieee80211_node;
181 int	ieee80211_node_dectestref(struct ieee80211_node *ni);
182 #define	ieee80211_node_refcnt(_ni)	(_ni)->ni_refcnt
183 
184 struct ifqueue;
185 void	ieee80211_drain_ifq(struct ifqueue *);
186 
187 #define	msecs_to_ticks(ms)	(((ms)*hz)/1000)
188 #define	ticks_to_msecs(t)	((t) / hz)
189 #define time_after(a,b) 	((long)(b) - (long)(a) < 0)
190 #define time_before(a,b)	time_after(b,a)
191 #define time_after_eq(a,b)	((long)(a) - (long)(b) >= 0)
192 #define time_before_eq(a,b)	time_after_eq(b,a)
193 
194 struct mbuf *ieee80211_getmgtframe(uint8_t **frm, int headroom, int pktlen);
195 
196 /* tx path usage */
197 #define	M_LINK0		M_PROTO1		/* WEP requested */
198 #define	M_PWR_SAV	M_PROTO4		/* bypass PS handling */
199 #define	M_MORE_DATA	M_PROTO5		/* more data frames to follow */
200 #define	M_FF		0x20000			/* fast frame */
201 #define	M_TXCB		0x40000			/* do tx complete callback */
202 #define	M_80211_TX	(0x60000|M_PROTO1|M_WME_AC_MASK|M_PROTO4|M_PROTO5)
203 
204 /* rx path usage */
205 #define	M_AMPDU		M_PROTO1		/* A-MPDU processing done */
206 #define	M_WEP		M_PROTO2		/* WEP done by hardware */
207 #define	M_80211_RX	(M_AMPDU|M_WEP)
208 /*
209  * Encode WME access control bits in the PROTO flags.
210  * This is safe since it's passed directly in to the
211  * driver and there's no chance someone else will clobber
212  * them on us.
213  */
214 #define	M_WME_AC_MASK	(M_PROTO2|M_PROTO3)
215 /* XXX 5 is wrong if M_PROTO* are redefined */
216 #define	M_WME_AC_SHIFT	5
217 
218 #define	M_WME_SETAC(m, ac) \
219 	((m)->m_flags = ((m)->m_flags &~ M_WME_AC_MASK) | \
220 		((ac) << M_WME_AC_SHIFT))
221 #define	M_WME_GETAC(m)	(((m)->m_flags >> M_WME_AC_SHIFT) & 0x3)
222 
223 /*
224  * Mbufs on the power save queue are tagged with an age and
225  * timed out.  We reuse the hardware checksum field in the
226  * mbuf packet header to store this data.
227  */
228 #define	M_AGE_SET(m,v)		(m->m_pkthdr.csum_data = v)
229 #define	M_AGE_GET(m)		(m->m_pkthdr.csum_data)
230 #define	M_AGE_SUB(m,adj)	(m->m_pkthdr.csum_data -= adj)
231 
232 #define	MTAG_ABI_NET80211	1132948340	/* net80211 ABI */
233 
234 struct ieee80211_cb {
235 	void	(*func)(struct ieee80211_node *, void *, int status);
236 	void	*arg;
237 };
238 #define	NET80211_TAG_CALLBACK	0	/* xmit complete callback */
239 int	ieee80211_add_callback(struct mbuf *m,
240 		void (*func)(struct ieee80211_node *, void *, int), void *arg);
241 void	ieee80211_process_callback(struct ieee80211_node *, struct mbuf *, int);
242 
243 void	get_random_bytes(void *, size_t);
244 
245 struct ieee80211com;
246 
247 void	ieee80211_sysctl_attach(struct ieee80211com *);
248 void	ieee80211_sysctl_detach(struct ieee80211com *);
249 
250 void	ieee80211_load_module(const char *);
251 
252 #define	IEEE80211_CRYPTO_MODULE(name, version) \
253 static int								\
254 name##_modevent(module_t mod, int type, void *unused)			\
255 {									\
256 	switch (type) {							\
257 	case MOD_LOAD:							\
258 		ieee80211_crypto_register(&name);			\
259 		return 0;						\
260 	case MOD_UNLOAD:						\
261 	case MOD_QUIESCE:						\
262 		if (nrefs) {						\
263 			printf("wlan_##name: still in use (%u dynamic refs)\n",\
264 				nrefs);					\
265 			return EBUSY;					\
266 		}							\
267 		if (type == MOD_UNLOAD)					\
268 			ieee80211_crypto_unregister(&name);		\
269 		return 0;						\
270 	}								\
271 	return EINVAL;							\
272 }									\
273 static moduledata_t name##_mod = {					\
274 	"wlan_" #name,							\
275 	name##_modevent,						\
276 	0								\
277 };									\
278 DECLARE_MODULE(wlan_##name, name##_mod, SI_SUB_DRIVERS, SI_ORDER_FIRST);\
279 MODULE_VERSION(wlan_##name, version);					\
280 MODULE_DEPEND(wlan_##name, wlan, 1, 1, 1)
281 #endif /* _KERNEL */
282 
283 /* XXX this stuff belongs elsewhere */
284 /*
285  * Message formats for messages from the net80211 layer to user
286  * applications via the routing socket.  These messages are appended
287  * to an if_announcemsghdr structure.
288  */
289 struct ieee80211_join_event {
290 	uint8_t		iev_addr[6];
291 };
292 
293 struct ieee80211_leave_event {
294 	uint8_t		iev_addr[6];
295 };
296 
297 struct ieee80211_replay_event {
298 	uint8_t		iev_src[6];	/* src MAC */
299 	uint8_t		iev_dst[6];	/* dst MAC */
300 	uint8_t		iev_cipher;	/* cipher type */
301 	uint8_t		iev_keyix;	/* key id/index */
302 	uint64_t	iev_keyrsc;	/* RSC from key */
303 	uint64_t	iev_rsc;	/* RSC from frame */
304 };
305 
306 struct ieee80211_michael_event {
307 	uint8_t		iev_src[6];	/* src MAC */
308 	uint8_t		iev_dst[6];	/* dst MAC */
309 	uint8_t		iev_cipher;	/* cipher type */
310 	uint8_t		iev_keyix;	/* key id/index */
311 };
312 
313 #define	RTM_IEEE80211_ASSOC	100	/* station associate (bss mode) */
314 #define	RTM_IEEE80211_REASSOC	101	/* station re-associate (bss mode) */
315 #define	RTM_IEEE80211_DISASSOC	102	/* station disassociate (bss mode) */
316 #define	RTM_IEEE80211_JOIN	103	/* station join (ap mode) */
317 #define	RTM_IEEE80211_LEAVE	104	/* station leave (ap mode) */
318 #define	RTM_IEEE80211_SCAN	105	/* scan complete, results available */
319 #define	RTM_IEEE80211_REPLAY	106	/* sequence counter replay detected */
320 #define	RTM_IEEE80211_MICHAEL	107	/* Michael MIC failure detected */
321 #define	RTM_IEEE80211_REJOIN	108	/* station re-associate (ap mode) */
322 
323 /*
324  * Structure prepended to raw packets sent through the bpf
325  * interface when set to DLT_IEEE802_11_RADIO.  This allows
326  * user applications to specify pretty much everything in
327  * an Atheros tx descriptor.  XXX need to generalize.
328  *
329  * XXX cannot be more than 14 bytes as it is copied to a sockaddr's
330  * XXX sa_data area.
331  */
332 struct ieee80211_bpf_params {
333 	uint8_t		ibp_vers;	/* version */
334 #define	IEEE80211_BPF_VERSION	0
335 	uint8_t		ibp_len;	/* header length in bytes */
336 	uint8_t		ibp_flags;
337 #define	IEEE80211_BPF_SHORTPRE	0x01	/* tx with short preamble */
338 #define	IEEE80211_BPF_NOACK	0x02	/* tx with no ack */
339 #define	IEEE80211_BPF_CRYPTO	0x04	/* tx with h/w encryption */
340 #define	IEEE80211_BPF_FCS	0x10	/* frame incldues FCS */
341 #define	IEEE80211_BPF_DATAPAD	0x20	/* frame includes data padding */
342 #define	IEEE80211_BPF_RTS	0x40	/* tx with RTS/CTS */
343 #define	IEEE80211_BPF_CTS	0x80	/* tx with CTS only */
344 	uint8_t		ibp_pri;	/* WME/WMM AC+tx antenna */
345 	uint8_t		ibp_try0;	/* series 1 try count */
346 	uint8_t		ibp_rate0;	/* series 1 IEEE tx rate */
347 	uint8_t		ibp_power;	/* tx power (device units) */
348 	uint8_t		ibp_ctsrate;	/* IEEE tx rate for CTS */
349 	uint8_t		ibp_try1;	/* series 2 try count */
350 	uint8_t		ibp_rate1;	/* series 2 IEEE tx rate */
351 	uint8_t		ibp_try2;	/* series 3 try count */
352 	uint8_t		ibp_rate2;	/* series 3 IEEE tx rate */
353 	uint8_t		ibp_try3;	/* series 4 try count */
354 	uint8_t		ibp_rate3;	/* series 4 IEEE tx rate */
355 };
356 #endif /* _NET80211_IEEE80211_FREEBSD_H_ */
357