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