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: head/sys/net80211/ieee80211_freebsd.h 195618 2009-07-11 15:02:45Z rpaulo $
26  */
27 #ifndef _NET80211_IEEE80211_DRAGONFLY_H_
28 #define _NET80211_IEEE80211_DRAGONFLY_H_
29 
30 #ifdef _KERNEL
31 
32 #include <sys/param.h>
33 #include <sys/types.h>
34 #include <sys/serialize.h>
35 #include <sys/sysctl.h>
36 #include <sys/condvar.h>
37 #include <sys/taskqueue.h>
38 
39 #include <sys/mutex2.h>
40 #include <sys/serialize2.h>
41 
42 #ifndef IF_PREPEND_LIST
43 
44 #define _IF_PREPEND_LIST(ifq, mhead, mtail, mcount) do {	\
45 	(mtail)->m_nextpkt = (ifq)->ifq_head;			\
46 	if ((ifq)->ifq_tail == NULL)				\
47 		(ifq)->ifq_tail = (mtail);			\
48 	(ifq)->ifq_head = (mhead);				\
49 	(ifq)->ifq_len += (mcount);				\
50 } while (0)
51 
52 #define IF_PREPEND_LIST(ifq, mhead, mtail, mcount) do {		\
53 	wlan_assert_serialized();				\
54 	_IF_PREPEND_LIST(ifq, mhead, mtail, mcount);		\
55 } while (0)
56 
57 #endif /* IF_PREPEND_LIST */
58 
59 /*
60  * Global serializer (operates like a non-reentrant lockmgr lock)
61  */
62 extern struct lwkt_serialize wlan_global_serializer;
63 extern int ieee80211_force_swcrypto;
64 
65 #define wlan_serialize_enter()	_wlan_serialize_enter(__FUNCTION__)
66 #define wlan_serialize_exit()	_wlan_serialize_exit(__FUNCTION__)
67 void _wlan_serialize_enter(const char *funcname);
68 void _wlan_serialize_exit(const char *funcname);
69 int wlan_serialize_sleep(void *ident, int flags, const char *wmesg, int timo);
70 
71 static __inline void
72 wlan_assert_serialized(void)
73 {
74 	ASSERT_SERIALIZED(&wlan_global_serializer);
75 }
76 
77 /*
78  * wlan condition variables.  Assume the global serializer is held.
79  */
80 void wlan_cv_init(struct cv *cv, const char *desc);
81 int wlan_cv_timedwait(struct cv *cv, int ticks);
82 void wlan_cv_wait(struct cv *cv);
83 void wlan_cv_signal(struct cv *cv, int broadcast);
84 
85 /*
86  * Node reference counting definitions.
87  *
88  * ieee80211_node_initref	initialize the reference count to 1
89  * ieee80211_node_incref	add a reference
90  * ieee80211_node_decref	remove a reference
91  * ieee80211_node_dectestref	remove a reference and return 1 if this
92  *				is the last reference, otherwise 0
93  * ieee80211_node_refcnt	reference count for printing (only)
94  */
95 #include <machine/atomic.h>
96 
97 #define ieee80211_node_initref(_ni) \
98 	do { ((_ni)->ni_refcnt = 1); } while (0)
99 #define ieee80211_node_incref(_ni) \
100 	atomic_add_int(&(_ni)->ni_refcnt, 1)
101 #define	ieee80211_node_decref(_ni) \
102 	atomic_subtract_int(&(_ni)->ni_refcnt, 1)
103 struct ieee80211_node;
104 int	ieee80211_node_dectestref(struct ieee80211_node *ni);
105 #define	ieee80211_node_refcnt(_ni)	(_ni)->ni_refcnt
106 
107 struct ifqueue;
108 struct ieee80211vap;
109 void	ieee80211_drain_ifq(struct ifqueue *);
110 void	ieee80211_flush_ifq(struct ifqueue *, struct ieee80211vap *);
111 
112 void	ieee80211_vap_destroy(struct ieee80211vap *);
113 int	ieee80211_handoff(struct ifnet *, struct mbuf *);
114 uint16_t ieee80211_txtime(struct ieee80211_node *, u_int, uint8_t, uint32_t);
115 
116 #define	IFNET_IS_UP_RUNNING(_ifp) \
117 	(((_ifp)->if_flags & IFF_UP) && \
118 	 ((_ifp)->if_flags & IFF_RUNNING))
119 
120 #define	msecs_to_ticks(ms)	(((ms)*hz)/1000)
121 #define	ticks_to_msecs(t)	(1000*(t) / hz)
122 #define	ticks_to_secs(t)	((t) / hz)
123 #define time_after(a,b) 	((long)(b) - (long)(a) < 0)
124 #define time_before(a,b)	time_after(b,a)
125 #define time_after_eq(a,b)	((long)(a) - (long)(b) >= 0)
126 #define time_before_eq(a,b)	time_after_eq(b,a)
127 
128 struct mbuf *ieee80211_getmgtframe(uint8_t **frm, int headroom, int pktlen);
129 
130 /* tx path usage */
131 #define	M_ENCAP		M_PROTO1		/* 802.11 encap done */
132 #define	M_EAPOL		M_PROTO3		/* PAE/EAPOL frame */
133 #define	M_PWR_SAV	M_PROTO4		/* bypass PS handling */
134 #define	M_MORE_DATA	M_PROTO5		/* more data frames to follow */
135 #define	M_FF		M_PROTO6		/* fast frame */
136 #define	M_TXCB		M_PROTO7		/* do tx complete callback */
137 #define	M_AMPDU_MPDU	M_PROTO8		/* ok for A-MPDU aggregation */
138 #define	M_80211_TX \
139 	(M_FRAG|M_FIRSTFRAG|M_LASTFRAG|M_ENCAP|M_EAPOL|M_PWR_SAV|\
140 	 M_MORE_DATA|M_FF|M_TXCB|M_AMPDU_MPDU)
141 
142 /* rx path usage */
143 #define	M_AMPDU		M_PROTO1		/* A-MPDU subframe */
144 #define	M_WEP		M_PROTO2		/* WEP done by hardware */
145 #if 0
146 #define	M_AMPDU_MPDU	M_PROTO8		/* A-MPDU re-order done */
147 #endif
148 #define	M_80211_RX	(M_AMPDU|M_WEP|M_AMPDU_MPDU)
149 
150 #define	IEEE80211_MBUF_TX_FLAG_BITS \
151 	"\20\1M_EXT\2M_PKTHDR\3M_EOR\4M_RDONLY\5M_ENCAP\6M_WEP\7M_EAPOL" \
152 	"\10M_PWR_SAV\11M_MORE_DATA\12M_BCAST\13M_MCAST\14M_FRAG\15M_FIRSTFRAG" \
153 	"\16M_LASTFRAG\17M_SKIP_FIREWALL\20M_FREELIST\21M_VLANTAG\22M_PROMISC" \
154 	"\23M_NOFREE\24M_FF\25M_TXCB\26M_AMPDU_MPDU\27M_FLOWID"
155 
156 #define	IEEE80211_MBUF_RX_FLAG_BITS \
157 	"\20\1M_EXT\2M_PKTHDR\3M_EOR\4M_RDONLY\5M_AMPDU\6M_WEP\7M_PROTO3" \
158 	"\10M_PROTO4\11M_PROTO5\12M_BCAST\13M_MCAST\14M_FRAG\15M_FIRSTFRAG" \
159 	"\16M_LASTFRAG\17M_SKIP_FIREWALL\20M_FREELIST\21M_VLANTAG\22M_PROMISC" \
160 	"\23M_NOFREE\24M_PROTO6\25M_PROTO7\26M_AMPDU_MPDU\27M_FLOWID"
161 
162 /*
163  * Store WME access control bits in the vlan tag.
164  * This is safe since it's done after the packet is classified
165  * (where we use any previous tag) and because it's passed
166  * directly in to the driver and there's no chance someone
167  * else will clobber them on us.
168  */
169 #define	M_WME_SETAC(m, ac) \
170 	((m)->m_pkthdr.ether_vlantag = (ac))
171 #define	M_WME_GETAC(m)	((m)->m_pkthdr.ether_vlantag)
172 
173 /*
174  * Mbufs on the power save queue are tagged with an age and
175  * timed out.  We reuse the hardware checksum field in the
176  * mbuf packet header to store this data.
177  */
178 #define	M_AGE_SET(m,v)		(m->m_pkthdr.csum_data = v)
179 #define	M_AGE_GET(m)		(m->m_pkthdr.csum_data)
180 #define	M_AGE_SUB(m,adj)	(m->m_pkthdr.csum_data -= adj)
181 
182 /*
183  * Store the sequence number.
184  */
185 #define	M_SEQNO_SET(m, seqno) \
186 	((m)->m_pkthdr.wlan_seqno = (seqno))
187 #define	M_SEQNO_GET(m)	((m)->m_pkthdr.wlan_seqno)
188 
189 #define	MTAG_ABI_NET80211	1132948340	/* net80211 ABI */
190 
191 struct ieee80211_cb {
192 	void	(*func)(struct ieee80211_node *, void *, int status);
193 	void	*arg;
194 };
195 #define	NET80211_TAG_CALLBACK	0	/* xmit complete callback */
196 int	ieee80211_add_callback(struct mbuf *m,
197 		void (*func)(struct ieee80211_node *, void *, int), void *arg);
198 void	ieee80211_process_callback(struct ieee80211_node *, struct mbuf *, int);
199 
200 void	get_random_bytes(void *, size_t);
201 
202 struct ieee80211com;
203 
204 void	ieee80211_sysctl_attach(struct ieee80211com *);
205 void	ieee80211_sysctl_detach(struct ieee80211com *);
206 void	ieee80211_sysctl_vattach(struct ieee80211vap *);
207 void	ieee80211_sysctl_vdetach(struct ieee80211vap *);
208 
209 SYSCTL_DECL(_net_wlan);
210 int	ieee80211_sysctl_msecs_ticks(SYSCTL_HANDLER_ARGS);
211 
212 void	ieee80211_load_module(const char *);
213 
214 /*
215  * A "policy module" is an adjunct module to net80211 that provides
216  * functionality that typically includes policy decisions.  This
217  * modularity enables extensibility and vendor-supplied functionality.
218  */
219 #define	_IEEE80211_POLICY_MODULE(policy, name, version)			\
220 typedef void (*policy##_setup)(int);					\
221 SET_DECLARE(policy##_set, policy##_setup);				\
222 static int								\
223 wlan_##name##_modevent(module_t mod, int type, void *unused)		\
224 {									\
225 	policy##_setup * const *iter, f;				\
226 	int error;							\
227 									\
228 	wlan_serialize_enter();						\
229 									\
230 	switch (type) {							\
231 	case MOD_LOAD:							\
232 		SET_FOREACH(iter, policy##_set) {			\
233 			f = (void*) *iter;				\
234 			f(type);					\
235 		}							\
236 		error = 0;						\
237 		break;							\
238 	case MOD_UNLOAD:						\
239 		error = 0;						\
240 		if (nrefs) {						\
241 			kprintf("wlan_##name: still in use (%u "	\
242 				"dynamic refs)\n",			\
243 				nrefs);					\
244 			error = EBUSY;					\
245 		} else if (type == MOD_UNLOAD) {			\
246 			SET_FOREACH(iter, policy##_set) {		\
247 				f = (void*) *iter;			\
248 				f(type);				\
249 			}						\
250 		}							\
251 		break;							\
252 	default:							\
253 		error = EINVAL;						\
254 		break;							\
255 	}								\
256 									\
257 	wlan_serialize_exit();						\
258 									\
259 	return error;							\
260 }									\
261 static moduledata_t name##_mod = {					\
262 	"wlan_" #name,							\
263 	wlan_##name##_modevent,						\
264 	0								\
265 };									\
266 DECLARE_MODULE(wlan_##name, name##_mod, SI_SUB_DRIVERS, SI_ORDER_FIRST);\
267 MODULE_VERSION(wlan_##name, version);					\
268 MODULE_DEPEND(wlan_##name, wlan, 1, 1, 1)
269 
270 /*
271  * Crypto modules implement cipher support.
272  */
273 #define	IEEE80211_CRYPTO_MODULE(name, version)				\
274 _IEEE80211_POLICY_MODULE(crypto, name, version);			\
275 static void								\
276 name##_modevent(int type)						\
277 {									\
278 	/* wlan already serialized! */					\
279 	if (type == MOD_LOAD)						\
280 		ieee80211_crypto_register(&name);			\
281 	else								\
282 		ieee80211_crypto_unregister(&name);			\
283 }									\
284 TEXT_SET(crypto##_set, name##_modevent)
285 
286 /*
287  * Scanner modules provide scanning policy.
288  */
289 #define	IEEE80211_SCANNER_MODULE(name, version)				\
290 	_IEEE80211_POLICY_MODULE(scanner, name, version)
291 
292 #define	IEEE80211_SCANNER_ALG(name, alg, v)				\
293 static void								\
294 name##_modevent(int type)						\
295 {									\
296 	/* wlan already serialized! */					\
297 	if (type == MOD_LOAD)						\
298 		ieee80211_scanner_register(alg, &v);			\
299 	else								\
300 		ieee80211_scanner_unregister(alg, &v);			\
301 }									\
302 TEXT_SET(scanner_set, name##_modevent);					\
303 
304 /*
305  * ACL modules implement acl policy.
306  */
307 #define	IEEE80211_ACL_MODULE(name, alg, version)			\
308 _IEEE80211_POLICY_MODULE(acl, name, version);				\
309 static void								\
310 alg##_modevent(int type)						\
311 {									\
312 	/* wlan already serialized! */					\
313 	if (type == MOD_LOAD)						\
314 		ieee80211_aclator_register(&alg);			\
315 	else								\
316 		ieee80211_aclator_unregister(&alg);			\
317 }									\
318 TEXT_SET(acl_set, alg##_modevent);					\
319 
320 /*
321  * Authenticator modules handle 802.1x/WPA authentication.
322  */
323 #define	IEEE80211_AUTH_MODULE(name, version)				\
324 	_IEEE80211_POLICY_MODULE(auth, name, version)
325 
326 #define	IEEE80211_AUTH_ALG(name, alg, v)				\
327 static void								\
328 name##_modevent(int type)						\
329 {									\
330 	/* wlan already serialized! */					\
331 	if (type == MOD_LOAD)						\
332 		ieee80211_authenticator_register(alg, &v);		\
333 	else								\
334 		ieee80211_authenticator_unregister(alg);		\
335 }									\
336 TEXT_SET(auth_set, name##_modevent)
337 
338 /*
339  * Rate control modules provide tx rate control support.
340  */
341 #define	IEEE80211_RATECTL_MODULE(alg, version)				\
342 	_IEEE80211_POLICY_MODULE(ratectl, alg, version);		\
343 
344 #define	IEEE80211_RATECTL_ALG(name, alg, v)				\
345 static void								\
346 alg##_modevent(int type)						\
347 {									\
348 	/* wlan already serialized! */					\
349 	if (type == MOD_LOAD)						\
350 		ieee80211_ratectl_register(alg, &v);			\
351 	else								\
352 		ieee80211_ratectl_unregister(alg);			\
353 }									\
354 TEXT_SET(ratectl##_set, alg##_modevent)
355 
356 struct ieee80211req;
357 typedef int ieee80211_ioctl_getfunc(struct ieee80211vap *,
358     struct ieee80211req *);
359 SET_DECLARE(ieee80211_ioctl_getset, ieee80211_ioctl_getfunc);
360 #define	IEEE80211_IOCTL_GET(_name, _get) TEXT_SET(ieee80211_ioctl_getset, _get)
361 
362 typedef int ieee80211_ioctl_setfunc(struct ieee80211vap *,
363     struct ieee80211req *);
364 SET_DECLARE(ieee80211_ioctl_setset, ieee80211_ioctl_setfunc);
365 #define	IEEE80211_IOCTL_SET(_name, _set) TEXT_SET(ieee80211_ioctl_setset, _set)
366 #endif /* _KERNEL */
367 
368 /* XXX this stuff belongs elsewhere */
369 /*
370  * Message formats for messages from the net80211 layer to user
371  * applications via the routing socket.  These messages are appended
372  * to an if_announcemsghdr structure.
373  */
374 struct ieee80211_join_event {
375 	uint8_t		iev_addr[6];
376 };
377 
378 struct ieee80211_leave_event {
379 	uint8_t		iev_addr[6];
380 };
381 
382 struct ieee80211_replay_event {
383 	uint8_t		iev_src[6];	/* src MAC */
384 	uint8_t		iev_dst[6];	/* dst MAC */
385 	uint8_t		iev_cipher;	/* cipher type */
386 	uint8_t		iev_keyix;	/* key id/index */
387 	uint64_t	iev_keyrsc;	/* RSC from key */
388 	uint64_t	iev_rsc;	/* RSC from frame */
389 };
390 
391 struct ieee80211_michael_event {
392 	uint8_t		iev_src[6];	/* src MAC */
393 	uint8_t		iev_dst[6];	/* dst MAC */
394 	uint8_t		iev_cipher;	/* cipher type */
395 	uint8_t		iev_keyix;	/* key id/index */
396 };
397 
398 struct ieee80211_wds_event {
399 	uint8_t		iev_addr[6];
400 };
401 
402 struct ieee80211_csa_event {
403 	uint32_t	iev_flags;	/* channel flags */
404 	uint16_t	iev_freq;	/* setting in Mhz */
405 	uint8_t		iev_ieee;	/* IEEE channel number */
406 	uint8_t		iev_mode;	/* CSA mode */
407 	uint8_t		iev_count;	/* CSA count */
408 };
409 
410 struct ieee80211_cac_event {
411 	uint32_t	iev_flags;	/* channel flags */
412 	uint16_t	iev_freq;	/* setting in Mhz */
413 	uint8_t		iev_ieee;	/* IEEE channel number */
414 	/* XXX timestamp? */
415 	uint8_t		iev_type;	/* IEEE80211_NOTIFY_CAC_* */
416 };
417 
418 struct ieee80211_radar_event {
419 	uint32_t	iev_flags;	/* channel flags */
420 	uint16_t	iev_freq;	/* setting in Mhz */
421 	uint8_t		iev_ieee;	/* IEEE channel number */
422 	/* XXX timestamp? */
423 };
424 
425 struct ieee80211_auth_event {
426 	uint8_t		iev_addr[6];
427 };
428 
429 struct ieee80211_deauth_event {
430 	uint8_t		iev_addr[6];
431 };
432 
433 struct ieee80211_country_event {
434 	uint8_t		iev_addr[6];
435 	uint8_t		iev_cc[2];	/* ISO country code */
436 };
437 
438 struct ieee80211_radio_event {
439 	uint8_t		iev_state;	/* 1 on, 0 off */
440 };
441 
442 #define	RTM_IEEE80211_ASSOC	100	/* station associate (bss mode) */
443 #define	RTM_IEEE80211_REASSOC	101	/* station re-associate (bss mode) */
444 #define	RTM_IEEE80211_DISASSOC	102	/* station disassociate (bss mode) */
445 #define	RTM_IEEE80211_JOIN	103	/* station join (ap mode) */
446 #define	RTM_IEEE80211_LEAVE	104	/* station leave (ap mode) */
447 #define	RTM_IEEE80211_SCAN	105	/* scan complete, results available */
448 #define	RTM_IEEE80211_REPLAY	106	/* sequence counter replay detected */
449 #define	RTM_IEEE80211_MICHAEL	107	/* Michael MIC failure detected */
450 #define	RTM_IEEE80211_REJOIN	108	/* station re-associate (ap mode) */
451 #define	RTM_IEEE80211_WDS	109	/* WDS discovery (ap mode) */
452 #define	RTM_IEEE80211_CSA	110	/* Channel Switch Announcement event */
453 #define	RTM_IEEE80211_RADAR	111	/* radar event */
454 #define	RTM_IEEE80211_CAC	112	/* Channel Availability Check event */
455 #define	RTM_IEEE80211_DEAUTH	113	/* station deauthenticate */
456 #define	RTM_IEEE80211_AUTH	114	/* station authenticate (ap mode) */
457 #define	RTM_IEEE80211_COUNTRY	115	/* discovered country code (sta mode) */
458 #define	RTM_IEEE80211_RADIO	116	/* RF kill switch state change */
459 
460 /*
461  * Structure prepended to raw packets sent through the bpf
462  * interface when set to DLT_IEEE802_11_RADIO.  This allows
463  * user applications to specify pretty much everything in
464  * an Atheros tx descriptor.  XXX need to generalize.
465  *
466  * XXX cannot be more than 14 bytes as it is copied to a sockaddr's
467  * XXX sa_data area.
468  */
469 struct ieee80211_bpf_params {
470 	uint8_t		ibp_vers;	/* version */
471 #define	IEEE80211_BPF_VERSION	0
472 	uint8_t		ibp_len;	/* header length in bytes */
473 	uint8_t		ibp_flags;
474 #define	IEEE80211_BPF_SHORTPRE	0x01	/* tx with short preamble */
475 #define	IEEE80211_BPF_NOACK	0x02	/* tx with no ack */
476 #define	IEEE80211_BPF_CRYPTO	0x04	/* tx with h/w encryption */
477 #define	IEEE80211_BPF_FCS	0x10	/* frame incldues FCS */
478 #define	IEEE80211_BPF_DATAPAD	0x20	/* frame includes data padding */
479 #define	IEEE80211_BPF_RTS	0x40	/* tx with RTS/CTS */
480 #define	IEEE80211_BPF_CTS	0x80	/* tx with CTS only */
481 	uint8_t		ibp_pri;	/* WME/WMM AC+tx antenna */
482 	uint8_t		ibp_try0;	/* series 1 try count */
483 	uint8_t		ibp_rate0;	/* series 1 IEEE tx rate */
484 	uint8_t		ibp_power;	/* tx power (device units) */
485 	uint8_t		ibp_ctsrate;	/* IEEE tx rate for CTS */
486 	uint8_t		ibp_try1;	/* series 2 try count */
487 	uint8_t		ibp_rate1;	/* series 2 IEEE tx rate */
488 	uint8_t		ibp_try2;	/* series 3 try count */
489 	uint8_t		ibp_rate2;	/* series 3 IEEE tx rate */
490 	uint8_t		ibp_try3;	/* series 4 try count */
491 	uint8_t		ibp_rate3;	/* series 4 IEEE tx rate */
492 };
493 #endif /* _NET80211_IEEE80211_DRAGONFLY_H_ */
494