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