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