aboutsummaryrefslogtreecommitdiffstats
path: root/net/mac80211
diff options
context:
space:
mode:
authorJohannes Berg <johannes.berg@intel.com>2011-07-07 16:28:01 -0400
committerJohn W. Linville <linville@tuxdriver.com>2011-07-08 11:11:19 -0400
commit523b02ea23b175dd3e46e3daf1bc9354376640a3 (patch)
treea11f69f05cdfb457a42b2866e33e73937c35ec1e /net/mac80211
parent397915c30731340ee3f348d1be597b22467acbdf (diff)
mac80211: fix TKIP races, make API easier to use
Our current TKIP code races against itself on TX since we can process multiple packets at the same time on different ACs, but they all share the TX context for TKIP. This can lead to bad IVs etc. Also, the crypto offload helper code just obtains the P1K/P2K from the cache, and can update it as well, but there's no guarantee that packets are really processed in order. To fix these issues, first introduce a spinlock that will protect the IV16/IV32 values in the TX context. This first step makes sure that we don't assign the same IV multiple times or get confused in other ways. Secondly, change the way the P1K cache works. I add a field "p1k_iv32" that stores the value of the IV32 when the P1K was last recomputed, and if different from the last time, then a new P1K is recomputed. This can cause the P1K computation to flip back and forth if packets are processed out of order. All this also happens under the new spinlock. Finally, because there are argument differences, split up the ieee80211_get_tkip_key() API into ieee80211_get_tkip_p1k() and ieee80211_get_tkip_p2k() and give them the correct arguments. Signed-off-by: Johannes Berg <johannes.berg@intel.com> Signed-off-by: John W. Linville <linville@tuxdriver.com>
Diffstat (limited to 'net/mac80211')
-rw-r--r--net/mac80211/key.c1
-rw-r--r--net/mac80211/key.h10
-rw-r--r--net/mac80211/tkip.c111
-rw-r--r--net/mac80211/tkip.h8
-rw-r--r--net/mac80211/wpa.c9
5 files changed, 78 insertions, 61 deletions
diff --git a/net/mac80211/key.c b/net/mac80211/key.c
index 1208a7878bfd..d930d4d4876d 100644
--- a/net/mac80211/key.c
+++ b/net/mac80211/key.c
@@ -369,6 +369,7 @@ struct ieee80211_key *ieee80211_key_alloc(u32 cipher, int idx, size_t key_len,
369 get_unaligned_le16(seq); 369 get_unaligned_le16(seq);
370 } 370 }
371 } 371 }
372 spin_lock_init(&key->u.tkip.txlock);
372 break; 373 break;
373 case WLAN_CIPHER_SUITE_CCMP: 374 case WLAN_CIPHER_SUITE_CCMP:
374 key->conf.iv_len = CCMP_HDR_LEN; 375 key->conf.iv_len = CCMP_HDR_LEN;
diff --git a/net/mac80211/key.h b/net/mac80211/key.h
index d801d5351336..1493c3e56b9f 100644
--- a/net/mac80211/key.h
+++ b/net/mac80211/key.h
@@ -52,9 +52,10 @@ enum ieee80211_internal_tkip_state {
52}; 52};
53 53
54struct tkip_ctx { 54struct tkip_ctx {
55 u32 iv32; 55 u32 iv32; /* current iv32 */
56 u16 iv16; 56 u16 iv16; /* current iv16 */
57 u16 p1k[5]; 57 u16 p1k[5]; /* p1k cache */
58 u32 p1k_iv32; /* iv32 for which p1k computed */
58 enum ieee80211_internal_tkip_state state; 59 enum ieee80211_internal_tkip_state state;
59}; 60};
60 61
@@ -71,6 +72,9 @@ struct ieee80211_key {
71 72
72 union { 73 union {
73 struct { 74 struct {
75 /* protects tx context */
76 spinlock_t txlock;
77
74 /* last used TSC */ 78 /* last used TSC */
75 struct tkip_ctx tx; 79 struct tkip_ctx tx;
76 80
diff --git a/net/mac80211/tkip.c b/net/mac80211/tkip.c
index 757e4eb2baf7..de570b38460f 100644
--- a/net/mac80211/tkip.c
+++ b/net/mac80211/tkip.c
@@ -101,6 +101,7 @@ static void tkip_mixing_phase1(const u8 *tk, struct tkip_ctx *ctx,
101 p1k[4] += tkipS(p1k[3] ^ get_unaligned_le16(tk + 0 + j)) + i; 101 p1k[4] += tkipS(p1k[3] ^ get_unaligned_le16(tk + 0 + j)) + i;
102 } 102 }
103 ctx->state = TKIP_STATE_PHASE1_DONE; 103 ctx->state = TKIP_STATE_PHASE1_DONE;
104 ctx->p1k_iv32 = tsc_IV32;
104} 105}
105 106
106static void tkip_mixing_phase2(const u8 *tk, struct tkip_ctx *ctx, 107static void tkip_mixing_phase2(const u8 *tk, struct tkip_ctx *ctx,
@@ -140,60 +141,72 @@ static void tkip_mixing_phase2(const u8 *tk, struct tkip_ctx *ctx,
140/* Add TKIP IV and Ext. IV at @pos. @iv0, @iv1, and @iv2 are the first octets 141/* Add TKIP IV and Ext. IV at @pos. @iv0, @iv1, and @iv2 are the first octets
141 * of the IV. Returns pointer to the octet following IVs (i.e., beginning of 142 * of the IV. Returns pointer to the octet following IVs (i.e., beginning of
142 * the packet payload). */ 143 * the packet payload). */
143u8 *ieee80211_tkip_add_iv(u8 *pos, struct ieee80211_key *key, u16 iv16) 144u8 *ieee80211_tkip_add_iv(u8 *pos, struct ieee80211_key *key)
144{ 145{
145 pos = write_tkip_iv(pos, iv16); 146 lockdep_assert_held(&key->u.tkip.txlock);
147
148 pos = write_tkip_iv(pos, key->u.tkip.tx.iv16);
146 *pos++ = (key->conf.keyidx << 6) | (1 << 5) /* Ext IV */; 149 *pos++ = (key->conf.keyidx << 6) | (1 << 5) /* Ext IV */;
147 put_unaligned_le32(key->u.tkip.tx.iv32, pos); 150 put_unaligned_le32(key->u.tkip.tx.iv32, pos);
148 return pos + 4; 151 return pos + 4;
149} 152}
150 153
151void ieee80211_get_tkip_key(struct ieee80211_key_conf *keyconf, 154static void ieee80211_compute_tkip_p1k(struct ieee80211_key *key, u32 iv32)
152 struct sk_buff *skb, enum ieee80211_tkip_key_type type, 155{
153 u8 *outkey) 156 struct ieee80211_sub_if_data *sdata = key->sdata;
157 struct tkip_ctx *ctx = &key->u.tkip.tx;
158 const u8 *tk = &key->conf.key[NL80211_TKIP_DATA_OFFSET_ENCR_KEY];
159
160 lockdep_assert_held(&key->u.tkip.txlock);
161
162 /*
163 * Update the P1K when the IV32 is different from the value it
164 * had when we last computed it (or when not initialised yet).
165 * This might flip-flop back and forth if packets are processed
166 * out-of-order due to the different ACs, but then we have to
167 * just compute the P1K more often.
168 */
169 if (ctx->p1k_iv32 != iv32 || ctx->state == TKIP_STATE_NOT_INIT)
170 tkip_mixing_phase1(tk, ctx, sdata->vif.addr, iv32);
171}
172
173void ieee80211_get_tkip_p1k(struct ieee80211_key_conf *keyconf,
174 struct sk_buff *skb, u16 *p1k)
154{ 175{
155 struct ieee80211_key *key = (struct ieee80211_key *) 176 struct ieee80211_key *key = (struct ieee80211_key *)
156 container_of(keyconf, struct ieee80211_key, conf); 177 container_of(keyconf, struct ieee80211_key, conf);
178 struct tkip_ctx *ctx = &key->u.tkip.tx;
157 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data; 179 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
158 u8 *data; 180 const u8 *data = (u8 *)hdr + ieee80211_hdrlen(hdr->frame_control);
159 const u8 *tk; 181 u32 iv32 = get_unaligned_le32(&data[4]);
160 struct tkip_ctx *ctx; 182 unsigned long flags;
161 u16 iv16; 183
162 u32 iv32; 184 spin_lock_irqsave(&key->u.tkip.txlock, flags);
163 185 ieee80211_compute_tkip_p1k(key, iv32);
164 data = (u8 *)hdr + ieee80211_hdrlen(hdr->frame_control); 186 memcpy(p1k, ctx->p1k, sizeof(ctx->p1k));
165 iv16 = data[2] | (data[0] << 8); 187 spin_unlock_irqrestore(&key->u.tkip.txlock, flags);
166 iv32 = get_unaligned_le32(&data[4]); 188}
167 189EXPORT_SYMBOL(ieee80211_get_tkip_p1k);
168 tk = &key->conf.key[NL80211_TKIP_DATA_OFFSET_ENCR_KEY];
169 ctx = &key->u.tkip.tx;
170
171#ifdef CONFIG_MAC80211_TKIP_DEBUG
172 printk(KERN_DEBUG "TKIP encrypt: iv16 = 0x%04x, iv32 = 0x%08x\n",
173 iv16, iv32);
174
175 if (iv32 != ctx->iv32) {
176 printk(KERN_DEBUG "skb: iv32 = 0x%08x key: iv32 = 0x%08x\n",
177 iv32, ctx->iv32);
178 printk(KERN_DEBUG "Wrap around of iv16 in the middle of a "
179 "fragmented packet\n");
180 }
181#endif
182
183 /* Update the p1k only when the iv16 in the packet wraps around, this
184 * might occur after the wrap around of iv16 in the key in case of
185 * fragmented packets. */
186 if (iv16 == 0 || ctx->state == TKIP_STATE_NOT_INIT)
187 tkip_mixing_phase1(tk, ctx, hdr->addr2, iv32);
188
189 if (type == IEEE80211_TKIP_P1_KEY) {
190 memcpy(outkey, ctx->p1k, sizeof(u16) * 5);
191 return;
192 }
193 190
194 tkip_mixing_phase2(tk, ctx, iv16, outkey); 191void ieee80211_get_tkip_p2k(struct ieee80211_key_conf *keyconf,
192 struct sk_buff *skb, u8 *p2k)
193{
194 struct ieee80211_key *key = (struct ieee80211_key *)
195 container_of(keyconf, struct ieee80211_key, conf);
196 const u8 *tk = &key->conf.key[NL80211_TKIP_DATA_OFFSET_ENCR_KEY];
197 struct tkip_ctx *ctx = &key->u.tkip.tx;
198 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
199 const u8 *data = (u8 *)hdr + ieee80211_hdrlen(hdr->frame_control);
200 u32 iv32 = get_unaligned_le32(&data[4]);
201 u16 iv16 = data[2] | (data[0] << 8);
202 unsigned long flags;
203
204 spin_lock_irqsave(&key->u.tkip.txlock, flags);
205 ieee80211_compute_tkip_p1k(key, iv32);
206 tkip_mixing_phase2(tk, ctx, iv16, p2k);
207 spin_unlock_irqrestore(&key->u.tkip.txlock, flags);
195} 208}
196EXPORT_SYMBOL(ieee80211_get_tkip_key); 209EXPORT_SYMBOL(ieee80211_get_tkip_p2k);
197 210
198/* 211/*
199 * Encrypt packet payload with TKIP using @key. @pos is a pointer to the 212 * Encrypt packet payload with TKIP using @key. @pos is a pointer to the
@@ -204,19 +217,15 @@ EXPORT_SYMBOL(ieee80211_get_tkip_key);
204 */ 217 */
205int ieee80211_tkip_encrypt_data(struct crypto_cipher *tfm, 218int ieee80211_tkip_encrypt_data(struct crypto_cipher *tfm,
206 struct ieee80211_key *key, 219 struct ieee80211_key *key,
207 u8 *pos, size_t payload_len, u8 *ta) 220 struct sk_buff *skb,
221 u8 *payload, size_t payload_len)
208{ 222{
209 u8 rc4key[16]; 223 u8 rc4key[16];
210 struct tkip_ctx *ctx = &key->u.tkip.tx;
211 const u8 *tk = &key->conf.key[NL80211_TKIP_DATA_OFFSET_ENCR_KEY];
212
213 /* Calculate per-packet key */
214 if (ctx->iv16 == 0 || ctx->state == TKIP_STATE_NOT_INIT)
215 tkip_mixing_phase1(tk, ctx, ta, ctx->iv32);
216 224
217 tkip_mixing_phase2(tk, ctx, ctx->iv16, rc4key); 225 ieee80211_get_tkip_p2k(&key->conf, skb, rc4key);
218 226
219 return ieee80211_wep_encrypt_data(tfm, rc4key, 16, pos, payload_len); 227 return ieee80211_wep_encrypt_data(tfm, rc4key, 16,
228 payload, payload_len);
220} 229}
221 230
222/* Decrypt packet payload with TKIP using @key. @pos is a pointer to the 231/* Decrypt packet payload with TKIP using @key. @pos is a pointer to the
diff --git a/net/mac80211/tkip.h b/net/mac80211/tkip.h
index 1cab9c86978f..e3ecb659b90a 100644
--- a/net/mac80211/tkip.h
+++ b/net/mac80211/tkip.h
@@ -13,11 +13,13 @@
13#include <linux/crypto.h> 13#include <linux/crypto.h>
14#include "key.h" 14#include "key.h"
15 15
16u8 *ieee80211_tkip_add_iv(u8 *pos, struct ieee80211_key *key, u16 iv16); 16u8 *ieee80211_tkip_add_iv(u8 *pos, struct ieee80211_key *key);
17 17
18int ieee80211_tkip_encrypt_data(struct crypto_cipher *tfm, 18int ieee80211_tkip_encrypt_data(struct crypto_cipher *tfm,
19 struct ieee80211_key *key, 19 struct ieee80211_key *key,
20 u8 *pos, size_t payload_len, u8 *ta); 20 struct sk_buff *skb,
21 u8 *payload, size_t payload_len);
22
21enum { 23enum {
22 TKIP_DECRYPT_OK = 0, 24 TKIP_DECRYPT_OK = 0,
23 TKIP_DECRYPT_NO_EXT_IV = -1, 25 TKIP_DECRYPT_NO_EXT_IV = -1,
diff --git a/net/mac80211/wpa.c b/net/mac80211/wpa.c
index d91c1a26630d..4ded2ae48a5f 100644
--- a/net/mac80211/wpa.c
+++ b/net/mac80211/wpa.c
@@ -171,6 +171,7 @@ static int tkip_encrypt_skb(struct ieee80211_tx_data *tx, struct sk_buff *skb)
171 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data; 171 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
172 struct ieee80211_key *key = tx->key; 172 struct ieee80211_key *key = tx->key;
173 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb); 173 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
174 unsigned long flags;
174 unsigned int hdrlen; 175 unsigned int hdrlen;
175 int len, tail; 176 int len, tail;
176 u8 *pos; 177 u8 *pos;
@@ -198,11 +199,12 @@ static int tkip_encrypt_skb(struct ieee80211_tx_data *tx, struct sk_buff *skb)
198 pos += hdrlen; 199 pos += hdrlen;
199 200
200 /* Increase IV for the frame */ 201 /* Increase IV for the frame */
202 spin_lock_irqsave(&key->u.tkip.txlock, flags);
201 key->u.tkip.tx.iv16++; 203 key->u.tkip.tx.iv16++;
202 if (key->u.tkip.tx.iv16 == 0) 204 if (key->u.tkip.tx.iv16 == 0)
203 key->u.tkip.tx.iv32++; 205 key->u.tkip.tx.iv32++;
204 206 pos = ieee80211_tkip_add_iv(pos, key);
205 pos = ieee80211_tkip_add_iv(pos, key, key->u.tkip.tx.iv16); 207 spin_unlock_irqrestore(&key->u.tkip.txlock, flags);
206 208
207 /* hwaccel - with software IV */ 209 /* hwaccel - with software IV */
208 if (info->control.hw_key) 210 if (info->control.hw_key)
@@ -211,9 +213,8 @@ static int tkip_encrypt_skb(struct ieee80211_tx_data *tx, struct sk_buff *skb)
211 /* Add room for ICV */ 213 /* Add room for ICV */
212 skb_put(skb, TKIP_ICV_LEN); 214 skb_put(skb, TKIP_ICV_LEN);
213 215
214 hdr = (struct ieee80211_hdr *) skb->data;
215 return ieee80211_tkip_encrypt_data(tx->local->wep_tx_tfm, 216 return ieee80211_tkip_encrypt_data(tx->local->wep_tx_tfm,
216 key, pos, len, hdr->addr2); 217 key, skb, pos, len);
217} 218}
218 219
219 220