wlan - Rip out all wlan locks part 1/2
[dragonfly.git] / sys / netproto / 802_11 / ieee80211_crypto.h
1 /*-
2  * Copyright (c) 2001 Atsushi Onoe
3  * Copyright (c) 2002-2008 Sam Leffler, Errno Consulting
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25  *
26  * $FreeBSD: head/sys/net80211/ieee80211_crypto.h 195812 2009-07-21 19:36:32Z sam $
27  * $DragonFly$
28  */
29 #ifndef _NET80211_IEEE80211_CRYPTO_H_
30 #define _NET80211_IEEE80211_CRYPTO_H_
31
32 /*
33  * 802.11 protocol crypto-related definitions.
34  */
35 #define IEEE80211_KEYBUF_SIZE   16
36 #define IEEE80211_MICBUF_SIZE   (8+8)   /* space for both tx+rx keys */
37
38 /*
39  * Old WEP-style key.  Deprecated.
40  */
41 struct ieee80211_wepkey {
42         u_int           wk_len;         /* key length in bytes */
43         uint8_t         wk_key[IEEE80211_KEYBUF_SIZE];
44 };
45
46 struct ieee80211_rsnparms {
47         uint8_t         rsn_mcastcipher;        /* mcast/group cipher */
48         uint8_t         rsn_mcastkeylen;        /* mcast key length */
49         uint8_t         rsn_ucastcipher;        /* selected unicast cipher */
50         uint8_t         rsn_ucastkeylen;        /* unicast key length */
51         uint8_t         rsn_keymgmt;            /* selected key mgmt algo */
52         uint16_t        rsn_caps;               /* capabilities */
53 };
54
55 struct ieee80211_cipher;
56
57 /*
58  * Crypto key state.  There is sufficient room for all supported
59  * ciphers (see below).  The underlying ciphers are handled
60  * separately through loadable cipher modules that register with
61  * the generic crypto support.  A key has a reference to an instance
62  * of the cipher; any per-key state is hung off wk_private by the
63  * cipher when it is attached.  Ciphers are automatically called
64  * to detach and cleanup any such state when the key is deleted.
65  *
66  * The generic crypto support handles encap/decap of cipher-related
67  * frame contents for both hardware- and software-based implementations.
68  * A key requiring software crypto support is automatically flagged and
69  * the cipher is expected to honor this and do the necessary work.
70  * Ciphers such as TKIP may also support mixed hardware/software
71  * encrypt/decrypt and MIC processing.
72  */
73 typedef uint16_t ieee80211_keyix;       /* h/w key index */
74
75 struct ieee80211_key {
76         uint8_t         wk_keylen;      /* key length in bytes */
77         uint8_t         wk_pad;
78         uint16_t        wk_flags;
79 #define IEEE80211_KEY_XMIT      0x0001  /* key used for xmit */
80 #define IEEE80211_KEY_RECV      0x0002  /* key used for recv */
81 #define IEEE80211_KEY_GROUP     0x0004  /* key used for WPA group operation */
82 #define IEEE80211_KEY_SWENCRYPT 0x0010  /* host-based encrypt */
83 #define IEEE80211_KEY_SWDECRYPT 0x0020  /* host-based decrypt */
84 #define IEEE80211_KEY_SWENMIC   0x0040  /* host-based enmic */
85 #define IEEE80211_KEY_SWDEMIC   0x0080  /* host-based demic */
86 #define IEEE80211_KEY_DEVKEY    0x0100  /* device key request completed */
87 #define IEEE80211_KEY_CIPHER0   0x1000  /* cipher-specific action 0 */
88 #define IEEE80211_KEY_CIPHER1   0x2000  /* cipher-specific action 1 */
89         ieee80211_keyix wk_keyix;       /* h/w key index */
90         ieee80211_keyix wk_rxkeyix;     /* optional h/w rx key index */
91         uint8_t         wk_key[IEEE80211_KEYBUF_SIZE+IEEE80211_MICBUF_SIZE];
92 #define wk_txmic        wk_key+IEEE80211_KEYBUF_SIZE+0  /* XXX can't () right */
93 #define wk_rxmic        wk_key+IEEE80211_KEYBUF_SIZE+8  /* XXX can't () right */
94                                         /* key receive sequence counter */
95         uint64_t        wk_keyrsc[IEEE80211_TID_SIZE];
96         uint64_t        wk_keytsc;      /* key transmit sequence counter */
97         const struct ieee80211_cipher *wk_cipher;
98         void            *wk_private;    /* private cipher state */
99         uint8_t         wk_macaddr[IEEE80211_ADDR_LEN];
100 };
101 #define IEEE80211_KEY_COMMON            /* common flags passed in by apps */\
102         (IEEE80211_KEY_XMIT | IEEE80211_KEY_RECV | IEEE80211_KEY_GROUP)
103 #define IEEE80211_KEY_DEVICE            /* flags owned by device driver */\
104         (IEEE80211_KEY_DEVKEY|IEEE80211_KEY_CIPHER0|IEEE80211_KEY_CIPHER1)
105
106 #define IEEE80211_KEY_SWCRYPT \
107         (IEEE80211_KEY_SWENCRYPT | IEEE80211_KEY_SWDECRYPT)
108 #define IEEE80211_KEY_SWMIC     (IEEE80211_KEY_SWENMIC | IEEE80211_KEY_SWDEMIC)
109
110 #define IEEE80211_KEY_BITS \
111         "\20\1XMIT\2RECV\3GROUP\4SWENCRYPT\5SWDECRYPT\6SWENMIC\7SWDEMIC" \
112         "\10DEVKEY\11CIPHER0\12CIPHER1"
113
114 #define IEEE80211_KEYIX_NONE    ((ieee80211_keyix) -1)
115
116 /*
117  * NB: these values are ordered carefully; there are lots of
118  * of implications in any reordering.  Beware that 4 is used
119  * only to indicate h/w TKIP MIC support in driver capabilities;
120  * there is no separate cipher support (it's rolled into the
121  * TKIP cipher support).
122  */
123 #define IEEE80211_CIPHER_WEP            0
124 #define IEEE80211_CIPHER_TKIP           1
125 #define IEEE80211_CIPHER_AES_OCB        2
126 #define IEEE80211_CIPHER_AES_CCM        3
127 #define IEEE80211_CIPHER_TKIPMIC        4       /* TKIP MIC capability */
128 #define IEEE80211_CIPHER_CKIP           5
129 #define IEEE80211_CIPHER_NONE           6       /* pseudo value */
130
131 #define IEEE80211_CIPHER_MAX            (IEEE80211_CIPHER_NONE+1)
132
133 /* capability bits in ic_cryptocaps/iv_cryptocaps */
134 #define IEEE80211_CRYPTO_WEP            (1<<IEEE80211_CIPHER_WEP)
135 #define IEEE80211_CRYPTO_TKIP           (1<<IEEE80211_CIPHER_TKIP)
136 #define IEEE80211_CRYPTO_AES_OCB        (1<<IEEE80211_CIPHER_AES_OCB)
137 #define IEEE80211_CRYPTO_AES_CCM        (1<<IEEE80211_CIPHER_AES_CCM)
138 #define IEEE80211_CRYPTO_TKIPMIC        (1<<IEEE80211_CIPHER_TKIPMIC)
139 #define IEEE80211_CRYPTO_CKIP           (1<<IEEE80211_CIPHER_CKIP)
140
141 #define IEEE80211_CRYPTO_BITS \
142         "\20\1WEP\2TKIP\3AES\4AES_CCM\5TKIPMIC\6CKIP"
143
144 #if defined(__KERNEL__) || defined(_KERNEL)
145
146 struct ieee80211com;
147 struct ieee80211vap;
148 struct ieee80211_node;
149 struct mbuf;
150
151 MALLOC_DECLARE(M_80211_CRYPTO);
152
153 void    ieee80211_crypto_attach(struct ieee80211com *);
154 void    ieee80211_crypto_detach(struct ieee80211com *);
155 void    ieee80211_crypto_vattach(struct ieee80211vap *);
156 void    ieee80211_crypto_vdetach(struct ieee80211vap *);
157 int     ieee80211_crypto_newkey(struct ieee80211vap *,
158                 int cipher, int flags, struct ieee80211_key *);
159 int     ieee80211_crypto_delkey(struct ieee80211vap *,
160                 struct ieee80211_key *);
161 int     ieee80211_crypto_setkey(struct ieee80211vap *, struct ieee80211_key *);
162 void    ieee80211_crypto_delglobalkeys(struct ieee80211vap *);
163 void    ieee80211_crypto_reload_keys(struct ieee80211com *);
164
165 /*
166  * Template for a supported cipher.  Ciphers register with the
167  * crypto code and are typically loaded as separate modules
168  * (the null cipher is always present).
169  * XXX may need refcnts
170  */
171 struct ieee80211_cipher {
172         const char *ic_name;            /* printable name */
173         u_int   ic_cipher;              /* IEEE80211_CIPHER_* */
174         u_int   ic_header;              /* size of privacy header (bytes) */
175         u_int   ic_trailer;             /* size of privacy trailer (bytes) */
176         u_int   ic_miclen;              /* size of mic trailer (bytes) */
177         void*   (*ic_attach)(struct ieee80211vap *, struct ieee80211_key *);
178         void    (*ic_detach)(struct ieee80211_key *);
179         int     (*ic_setkey)(struct ieee80211_key *);
180         int     (*ic_encap)(struct ieee80211_key *, struct mbuf *,
181                         uint8_t keyid);
182         int     (*ic_decap)(struct ieee80211_key *, struct mbuf *, int);
183         int     (*ic_enmic)(struct ieee80211_key *, struct mbuf *, int);
184         int     (*ic_demic)(struct ieee80211_key *, struct mbuf *, int);
185 };
186 extern  const struct ieee80211_cipher ieee80211_cipher_none;
187
188 #define IEEE80211_KEY_UNDEFINED(k) \
189         ((k)->wk_cipher == &ieee80211_cipher_none)
190
191 void    ieee80211_crypto_register(const struct ieee80211_cipher *);
192 void    ieee80211_crypto_unregister(const struct ieee80211_cipher *);
193 int     ieee80211_crypto_available(u_int cipher);
194
195 struct ieee80211_key *ieee80211_crypto_encap(struct ieee80211_node *,
196                 struct mbuf *);
197 struct ieee80211_key *ieee80211_crypto_decap(struct ieee80211_node *,
198                 struct mbuf *, int);
199
200 /*
201  * Check and remove any MIC.
202  */
203 static __inline int
204 ieee80211_crypto_demic(struct ieee80211vap *vap, struct ieee80211_key *k,
205         struct mbuf *m, int force)
206 {
207         const struct ieee80211_cipher *cip = k->wk_cipher;
208         return (cip->ic_miclen > 0 ? cip->ic_demic(k, m, force) : 1);
209 }
210
211 /*
212  * Add any MIC.
213  */
214 static __inline int
215 ieee80211_crypto_enmic(struct ieee80211vap *vap,
216         struct ieee80211_key *k, struct mbuf *m, int force)
217 {
218         const struct ieee80211_cipher *cip = k->wk_cipher;
219         return (cip->ic_miclen > 0 ? cip->ic_enmic(k, m, force) : 1);
220 }
221
222 /* 
223  * Reset key state to an unused state.  The crypto
224  * key allocation mechanism insures other state (e.g.
225  * key data) is properly setup before a key is used.
226  */
227 static __inline void
228 ieee80211_crypto_resetkey(struct ieee80211vap *vap,
229         struct ieee80211_key *k, ieee80211_keyix ix)
230 {
231         k->wk_cipher = &ieee80211_cipher_none;
232         k->wk_private = k->wk_cipher->ic_attach(vap, k);
233         k->wk_keyix = k->wk_rxkeyix = ix;
234         k->wk_flags = IEEE80211_KEY_XMIT | IEEE80211_KEY_RECV;
235 }
236
237 /*
238  * Crypt-related notification methods.
239  */
240 void    ieee80211_notify_replay_failure(struct ieee80211vap *,
241                 const struct ieee80211_frame *, const struct ieee80211_key *,
242                 uint64_t rsc, int tid);
243 void    ieee80211_notify_michael_failure(struct ieee80211vap *,
244                 const struct ieee80211_frame *, u_int keyix);
245 #endif /* defined(__KERNEL__) || defined(_KERNEL) */
246 #endif /* _NET80211_IEEE80211_CRYPTO_H_ */